Commit | Line | Data |
---|---|---|
024fafba JC |
1 | /* |
2 | * Driver for the VIA Chrome integrated camera controller. | |
3 | * | |
4 | * Copyright 2009,2010 Jonathan Corbet <corbet@lwn.net> | |
5 | * Distributable under the terms of the GNU General Public License, version 2 | |
6 | * | |
7 | * This work was supported by the One Laptop Per Child project | |
8 | */ | |
9 | #include <linux/kernel.h> | |
10 | #include <linux/module.h> | |
11 | #include <linux/device.h> | |
12 | #include <linux/list.h> | |
13 | #include <linux/pci.h> | |
14 | #include <linux/gpio.h> | |
15 | #include <linux/interrupt.h> | |
024fafba JC |
16 | #include <linux/platform_device.h> |
17 | #include <linux/videodev2.h> | |
18 | #include <media/v4l2-device.h> | |
19 | #include <media/v4l2-ioctl.h> | |
dbf8f4e5 | 20 | #include <media/v4l2-ctrls.h> |
c0d04f40 | 21 | #include <media/v4l2-image-sizes.h> |
b5dcee22 | 22 | #include <media/i2c/ov7670.h> |
024fafba | 23 | #include <media/videobuf-dma-sg.h> |
024fafba JC |
24 | #include <linux/delay.h> |
25 | #include <linux/dma-mapping.h> | |
e8db0be1 | 26 | #include <linux/pm_qos.h> |
024fafba JC |
27 | #include <linux/via-core.h> |
28 | #include <linux/via-gpio.h> | |
29 | #include <linux/via_i2c.h> | |
c6384c88 | 30 | #include <asm/olpc.h> |
024fafba JC |
31 | |
32 | #include "via-camera.h" | |
33 | ||
027e99ab | 34 | MODULE_ALIAS("platform:viafb-camera"); |
024fafba JC |
35 | MODULE_AUTHOR("Jonathan Corbet <corbet@lwn.net>"); |
36 | MODULE_DESCRIPTION("VIA framebuffer-based camera controller driver"); | |
37 | MODULE_LICENSE("GPL"); | |
38 | ||
90ab5ee9 | 39 | static bool flip_image; |
024fafba JC |
40 | module_param(flip_image, bool, 0444); |
41 | MODULE_PARM_DESC(flip_image, | |
42 | "If set, the sensor will be instructed to flip the image " | |
43 | "vertically."); | |
44 | ||
90ab5ee9 | 45 | static bool override_serial; |
024fafba JC |
46 | module_param(override_serial, bool, 0444); |
47 | MODULE_PARM_DESC(override_serial, | |
48 | "The camera driver will normally refuse to load if " | |
49 | "the XO 1.5 serial port is enabled. Set this option " | |
c6384c88 | 50 | "to force-enable the camera."); |
024fafba | 51 | |
024fafba JC |
52 | /* |
53 | * The structure describing our camera. | |
54 | */ | |
55 | enum viacam_opstate { S_IDLE = 0, S_RUNNING = 1 }; | |
56 | ||
57 | struct via_camera { | |
58 | struct v4l2_device v4l2_dev; | |
dbf8f4e5 | 59 | struct v4l2_ctrl_handler ctrl_handler; |
024fafba JC |
60 | struct video_device vdev; |
61 | struct v4l2_subdev *sensor; | |
62 | struct platform_device *platdev; | |
63 | struct viafb_dev *viadev; | |
64 | struct mutex lock; | |
65 | enum viacam_opstate opstate; | |
66 | unsigned long flags; | |
cc749986 | 67 | struct pm_qos_request qos_request; |
024fafba JC |
68 | /* |
69 | * GPIO info for power/reset management | |
70 | */ | |
71 | int power_gpio; | |
72 | int reset_gpio; | |
73 | /* | |
74 | * I/O memory stuff. | |
75 | */ | |
76 | void __iomem *mmio; /* Where the registers live */ | |
77 | void __iomem *fbmem; /* Frame buffer memory */ | |
78 | u32 fb_offset; /* Reserved memory offset (FB) */ | |
79 | /* | |
80 | * Capture buffers and related. The controller supports | |
81 | * up to three, so that's what we have here. These buffers | |
82 | * live in frame buffer memory, so we don't call them "DMA". | |
83 | */ | |
84 | unsigned int cb_offsets[3]; /* offsets into fb mem */ | |
a7547af7 | 85 | u8 __iomem *cb_addrs[3]; /* Kernel-space addresses */ |
024fafba JC |
86 | int n_cap_bufs; /* How many are we using? */ |
87 | int next_buf; | |
88 | struct videobuf_queue vb_queue; | |
89 | struct list_head buffer_queue; /* prot. by reg_lock */ | |
90 | /* | |
91 | * User tracking. | |
92 | */ | |
93 | int users; | |
94 | struct file *owner; | |
95 | /* | |
96 | * Video format information. sensor_format is kept in a form | |
97 | * that we can use to pass to the sensor. We always run the | |
98 | * sensor in VGA resolution, though, and let the controller | |
99 | * downscale things if need be. So we keep the "real* | |
100 | * dimensions separately. | |
101 | */ | |
102 | struct v4l2_pix_format sensor_format; | |
103 | struct v4l2_pix_format user_format; | |
27ffaeb0 | 104 | u32 mbus_code; |
024fafba JC |
105 | }; |
106 | ||
107 | /* | |
108 | * Yes, this is a hack, but there's only going to be one of these | |
109 | * on any system we know of. | |
110 | */ | |
111 | static struct via_camera *via_cam_info; | |
112 | ||
113 | /* | |
114 | * Flag values, manipulated with bitops | |
115 | */ | |
116 | #define CF_DMA_ACTIVE 0 /* A frame is incoming */ | |
117 | #define CF_CONFIG_NEEDED 1 /* Must configure hardware */ | |
118 | ||
119 | ||
120 | /* | |
121 | * Nasty ugly v4l2 boilerplate. | |
122 | */ | |
123 | #define sensor_call(cam, optype, func, args...) \ | |
124 | v4l2_subdev_call(cam->sensor, optype, func, ##args) | |
125 | ||
126 | /* | |
127 | * Debugging and related. | |
128 | */ | |
129 | #define cam_err(cam, fmt, arg...) \ | |
130 | dev_err(&(cam)->platdev->dev, fmt, ##arg); | |
131 | #define cam_warn(cam, fmt, arg...) \ | |
132 | dev_warn(&(cam)->platdev->dev, fmt, ##arg); | |
133 | #define cam_dbg(cam, fmt, arg...) \ | |
134 | dev_dbg(&(cam)->platdev->dev, fmt, ##arg); | |
135 | ||
136 | /* | |
137 | * Format handling. This is ripped almost directly from Hans's changes | |
138 | * to cafe_ccic.c. It's a little unfortunate; until this change, we | |
139 | * didn't need to know anything about the format except its byte depth; | |
140 | * now this information must be managed at this level too. | |
141 | */ | |
142 | static struct via_format { | |
143 | __u8 *desc; | |
144 | __u32 pixelformat; | |
145 | int bpp; /* Bytes per pixel */ | |
27ffaeb0 | 146 | u32 mbus_code; |
024fafba JC |
147 | } via_formats[] = { |
148 | { | |
149 | .desc = "YUYV 4:2:2", | |
150 | .pixelformat = V4L2_PIX_FMT_YUYV, | |
27ffaeb0 | 151 | .mbus_code = MEDIA_BUS_FMT_YUYV8_2X8, |
024fafba JC |
152 | .bpp = 2, |
153 | }, | |
024fafba | 154 | /* RGB444 and Bayer should be doable, but have never been |
9c900f02 DD |
155 | tested with this driver. RGB565 seems to work at the default |
156 | resolution, but results in color corruption when being scaled by | |
157 | viacam_set_scaled(), and is disabled as a result. */ | |
024fafba JC |
158 | }; |
159 | #define N_VIA_FMTS ARRAY_SIZE(via_formats) | |
160 | ||
161 | static struct via_format *via_find_format(u32 pixelformat) | |
162 | { | |
163 | unsigned i; | |
164 | ||
165 | for (i = 0; i < N_VIA_FMTS; i++) | |
166 | if (via_formats[i].pixelformat == pixelformat) | |
167 | return via_formats + i; | |
168 | /* Not found? Then return the first format. */ | |
169 | return via_formats; | |
170 | } | |
171 | ||
172 | ||
173 | /*--------------------------------------------------------------------------*/ | |
174 | /* | |
175 | * Sensor power/reset management. This piece is OLPC-specific for | |
176 | * sure; other configurations will have things connected differently. | |
177 | */ | |
178 | static int via_sensor_power_setup(struct via_camera *cam) | |
179 | { | |
180 | int ret; | |
181 | ||
182 | cam->power_gpio = viafb_gpio_lookup("VGPIO3"); | |
183 | cam->reset_gpio = viafb_gpio_lookup("VGPIO2"); | |
184 | if (cam->power_gpio < 0 || cam->reset_gpio < 0) { | |
185 | dev_err(&cam->platdev->dev, "Unable to find GPIO lines\n"); | |
186 | return -EINVAL; | |
187 | } | |
188 | ret = gpio_request(cam->power_gpio, "viafb-camera"); | |
189 | if (ret) { | |
190 | dev_err(&cam->platdev->dev, "Unable to request power GPIO\n"); | |
191 | return ret; | |
192 | } | |
193 | ret = gpio_request(cam->reset_gpio, "viafb-camera"); | |
194 | if (ret) { | |
195 | dev_err(&cam->platdev->dev, "Unable to request reset GPIO\n"); | |
196 | gpio_free(cam->power_gpio); | |
197 | return ret; | |
198 | } | |
199 | gpio_direction_output(cam->power_gpio, 0); | |
200 | gpio_direction_output(cam->reset_gpio, 0); | |
201 | return 0; | |
202 | } | |
203 | ||
204 | /* | |
205 | * Power up the sensor and perform the reset dance. | |
206 | */ | |
207 | static void via_sensor_power_up(struct via_camera *cam) | |
208 | { | |
209 | gpio_set_value(cam->power_gpio, 1); | |
210 | gpio_set_value(cam->reset_gpio, 0); | |
211 | msleep(20); /* Probably excessive */ | |
212 | gpio_set_value(cam->reset_gpio, 1); | |
213 | msleep(20); | |
214 | } | |
215 | ||
216 | static void via_sensor_power_down(struct via_camera *cam) | |
217 | { | |
218 | gpio_set_value(cam->power_gpio, 0); | |
219 | gpio_set_value(cam->reset_gpio, 0); | |
220 | } | |
221 | ||
222 | ||
223 | static void via_sensor_power_release(struct via_camera *cam) | |
224 | { | |
225 | via_sensor_power_down(cam); | |
226 | gpio_free(cam->power_gpio); | |
227 | gpio_free(cam->reset_gpio); | |
228 | } | |
229 | ||
230 | /* --------------------------------------------------------------------------*/ | |
231 | /* Sensor ops */ | |
232 | ||
233 | /* | |
234 | * Manage the ov7670 "flip" bit, which needs special help. | |
235 | */ | |
236 | static int viacam_set_flip(struct via_camera *cam) | |
237 | { | |
238 | struct v4l2_control ctrl; | |
239 | ||
240 | memset(&ctrl, 0, sizeof(ctrl)); | |
241 | ctrl.id = V4L2_CID_VFLIP; | |
242 | ctrl.value = flip_image; | |
243 | return sensor_call(cam, core, s_ctrl, &ctrl); | |
244 | } | |
245 | ||
246 | /* | |
247 | * Configure the sensor. It's up to the caller to ensure | |
248 | * that the camera is in the correct operating state. | |
249 | */ | |
250 | static int viacam_configure_sensor(struct via_camera *cam) | |
251 | { | |
ebf984bb HV |
252 | struct v4l2_subdev_format format = { |
253 | .which = V4L2_SUBDEV_FORMAT_ACTIVE, | |
254 | }; | |
024fafba JC |
255 | int ret; |
256 | ||
ebf984bb | 257 | v4l2_fill_mbus_format(&format.format, &cam->sensor_format, cam->mbus_code); |
024fafba JC |
258 | ret = sensor_call(cam, core, init, 0); |
259 | if (ret == 0) | |
ebf984bb | 260 | ret = sensor_call(cam, pad, set_fmt, NULL, &format); |
024fafba JC |
261 | /* |
262 | * OV7670 does weird things if flip is set *before* format... | |
263 | */ | |
264 | if (ret == 0) | |
265 | ret = viacam_set_flip(cam); | |
266 | return ret; | |
267 | } | |
268 | ||
269 | ||
270 | ||
271 | /* --------------------------------------------------------------------------*/ | |
272 | /* | |
273 | * Some simple register accessors; they assume that the lock is held. | |
274 | * | |
275 | * Should we want to support the second capture engine, we could | |
276 | * hide the register difference by adding 0x1000 to registers in the | |
277 | * 0x300-350 range. | |
278 | */ | |
279 | static inline void viacam_write_reg(struct via_camera *cam, | |
280 | int reg, int value) | |
281 | { | |
282 | iowrite32(value, cam->mmio + reg); | |
283 | } | |
284 | ||
285 | static inline int viacam_read_reg(struct via_camera *cam, int reg) | |
286 | { | |
287 | return ioread32(cam->mmio + reg); | |
288 | } | |
289 | ||
290 | static inline void viacam_write_reg_mask(struct via_camera *cam, | |
291 | int reg, int value, int mask) | |
292 | { | |
293 | int tmp = viacam_read_reg(cam, reg); | |
294 | ||
295 | tmp = (tmp & ~mask) | (value & mask); | |
296 | viacam_write_reg(cam, reg, tmp); | |
297 | } | |
298 | ||
299 | ||
300 | /* --------------------------------------------------------------------------*/ | |
301 | /* Interrupt management and handling */ | |
302 | ||
303 | static irqreturn_t viacam_quick_irq(int irq, void *data) | |
304 | { | |
305 | struct via_camera *cam = data; | |
306 | irqreturn_t ret = IRQ_NONE; | |
307 | int icv; | |
308 | ||
309 | /* | |
310 | * All we do here is to clear the interrupts and tell | |
311 | * the handler thread to wake up. | |
312 | */ | |
313 | spin_lock(&cam->viadev->reg_lock); | |
314 | icv = viacam_read_reg(cam, VCR_INTCTRL); | |
315 | if (icv & VCR_IC_EAV) { | |
316 | icv |= VCR_IC_EAV|VCR_IC_EVBI|VCR_IC_FFULL; | |
317 | viacam_write_reg(cam, VCR_INTCTRL, icv); | |
318 | ret = IRQ_WAKE_THREAD; | |
319 | } | |
320 | spin_unlock(&cam->viadev->reg_lock); | |
321 | return ret; | |
322 | } | |
323 | ||
324 | /* | |
325 | * Find the next videobuf buffer which has somebody waiting on it. | |
326 | */ | |
327 | static struct videobuf_buffer *viacam_next_buffer(struct via_camera *cam) | |
328 | { | |
329 | unsigned long flags; | |
330 | struct videobuf_buffer *buf = NULL; | |
331 | ||
332 | spin_lock_irqsave(&cam->viadev->reg_lock, flags); | |
333 | if (cam->opstate != S_RUNNING) | |
334 | goto out; | |
335 | if (list_empty(&cam->buffer_queue)) | |
336 | goto out; | |
337 | buf = list_entry(cam->buffer_queue.next, struct videobuf_buffer, queue); | |
338 | if (!waitqueue_active(&buf->done)) {/* Nobody waiting */ | |
339 | buf = NULL; | |
340 | goto out; | |
341 | } | |
342 | list_del(&buf->queue); | |
343 | buf->state = VIDEOBUF_ACTIVE; | |
344 | out: | |
345 | spin_unlock_irqrestore(&cam->viadev->reg_lock, flags); | |
346 | return buf; | |
347 | } | |
348 | ||
349 | /* | |
350 | * The threaded IRQ handler. | |
351 | */ | |
352 | static irqreturn_t viacam_irq(int irq, void *data) | |
353 | { | |
354 | int bufn; | |
355 | struct videobuf_buffer *vb; | |
356 | struct via_camera *cam = data; | |
357 | struct videobuf_dmabuf *vdma; | |
358 | ||
359 | /* | |
360 | * If there is no place to put the data frame, don't bother | |
361 | * with anything else. | |
362 | */ | |
363 | vb = viacam_next_buffer(cam); | |
364 | if (vb == NULL) | |
365 | goto done; | |
366 | /* | |
367 | * Figure out which buffer we just completed. | |
368 | */ | |
369 | bufn = (viacam_read_reg(cam, VCR_INTCTRL) & VCR_IC_ACTBUF) >> 3; | |
370 | bufn -= 1; | |
371 | if (bufn < 0) | |
372 | bufn = cam->n_cap_bufs - 1; | |
373 | /* | |
374 | * Copy over the data and let any waiters know. | |
375 | */ | |
376 | vdma = videobuf_to_dma(vb); | |
377 | viafb_dma_copy_out_sg(cam->cb_offsets[bufn], vdma->sglist, vdma->sglen); | |
378 | vb->state = VIDEOBUF_DONE; | |
379 | vb->size = cam->user_format.sizeimage; | |
380 | wake_up(&vb->done); | |
381 | done: | |
382 | return IRQ_HANDLED; | |
383 | } | |
384 | ||
385 | ||
386 | /* | |
387 | * These functions must mess around with the general interrupt | |
388 | * control register, which is relevant to much more than just the | |
389 | * camera. Nothing else uses interrupts, though, as of this writing. | |
390 | * Should that situation change, we'll have to improve support at | |
391 | * the via-core level. | |
392 | */ | |
393 | static void viacam_int_enable(struct via_camera *cam) | |
394 | { | |
395 | viacam_write_reg(cam, VCR_INTCTRL, | |
396 | VCR_IC_INTEN|VCR_IC_EAV|VCR_IC_EVBI|VCR_IC_FFULL); | |
397 | viafb_irq_enable(VDE_I_C0AVEN); | |
398 | } | |
399 | ||
400 | static void viacam_int_disable(struct via_camera *cam) | |
401 | { | |
402 | viafb_irq_disable(VDE_I_C0AVEN); | |
403 | viacam_write_reg(cam, VCR_INTCTRL, 0); | |
404 | } | |
405 | ||
406 | ||
407 | ||
408 | /* --------------------------------------------------------------------------*/ | |
409 | /* Controller operations */ | |
410 | ||
411 | /* | |
412 | * Set up our capture buffers in framebuffer memory. | |
413 | */ | |
414 | static int viacam_ctlr_cbufs(struct via_camera *cam) | |
415 | { | |
416 | int nbuf = cam->viadev->camera_fbmem_size/cam->sensor_format.sizeimage; | |
417 | int i; | |
418 | unsigned int offset; | |
419 | ||
420 | /* | |
421 | * See how many buffers we can work with. | |
422 | */ | |
423 | if (nbuf >= 3) { | |
424 | cam->n_cap_bufs = 3; | |
425 | viacam_write_reg_mask(cam, VCR_CAPINTC, VCR_CI_3BUFS, | |
426 | VCR_CI_3BUFS); | |
427 | } else if (nbuf == 2) { | |
428 | cam->n_cap_bufs = 2; | |
429 | viacam_write_reg_mask(cam, VCR_CAPINTC, 0, VCR_CI_3BUFS); | |
430 | } else { | |
431 | cam_warn(cam, "Insufficient frame buffer memory\n"); | |
432 | return -ENOMEM; | |
433 | } | |
434 | /* | |
435 | * Set them up. | |
436 | */ | |
437 | offset = cam->fb_offset; | |
438 | for (i = 0; i < cam->n_cap_bufs; i++) { | |
439 | cam->cb_offsets[i] = offset; | |
440 | cam->cb_addrs[i] = cam->fbmem + offset; | |
441 | viacam_write_reg(cam, VCR_VBUF1 + i*4, offset & VCR_VBUF_MASK); | |
442 | offset += cam->sensor_format.sizeimage; | |
443 | } | |
444 | return 0; | |
445 | } | |
446 | ||
447 | /* | |
448 | * Set the scaling register for downscaling the image. | |
449 | * | |
450 | * This register works like this... Vertical scaling is enabled | |
451 | * by bit 26; if that bit is set, downscaling is controlled by the | |
452 | * value in bits 16:25. Those bits are divided by 1024 to get | |
453 | * the scaling factor; setting just bit 25 thus cuts the height | |
454 | * in half. | |
455 | * | |
456 | * Horizontal scaling works about the same, but it's enabled by | |
457 | * bit 11, with bits 0:10 giving the numerator of a fraction | |
458 | * (over 2048) for the scaling value. | |
459 | * | |
460 | * This function is naive in that, if the user departs from | |
461 | * the 3x4 VGA scaling factor, the image will distort. We | |
462 | * could work around that if it really seemed important. | |
463 | */ | |
464 | static void viacam_set_scale(struct via_camera *cam) | |
465 | { | |
466 | unsigned int avscale; | |
467 | int sf; | |
468 | ||
469 | if (cam->user_format.width == VGA_WIDTH) | |
470 | avscale = 0; | |
471 | else { | |
472 | sf = (cam->user_format.width*2048)/VGA_WIDTH; | |
473 | avscale = VCR_AVS_HEN | sf; | |
474 | } | |
475 | if (cam->user_format.height < VGA_HEIGHT) { | |
476 | sf = (1024*cam->user_format.height)/VGA_HEIGHT; | |
477 | avscale |= VCR_AVS_VEN | (sf << 16); | |
478 | } | |
479 | viacam_write_reg(cam, VCR_AVSCALE, avscale); | |
480 | } | |
481 | ||
482 | ||
483 | /* | |
484 | * Configure image-related information into the capture engine. | |
485 | */ | |
486 | static void viacam_ctlr_image(struct via_camera *cam) | |
487 | { | |
488 | int cicreg; | |
489 | ||
490 | /* | |
491 | * Disable clock before messing with stuff - from the via | |
492 | * sample driver. | |
493 | */ | |
494 | viacam_write_reg(cam, VCR_CAPINTC, ~(VCR_CI_ENABLE|VCR_CI_CLKEN)); | |
495 | /* | |
496 | * Set up the controller for VGA resolution, modulo magic | |
497 | * offsets from the via sample driver. | |
498 | */ | |
499 | viacam_write_reg(cam, VCR_HORRANGE, 0x06200120); | |
500 | viacam_write_reg(cam, VCR_VERTRANGE, 0x01de0000); | |
501 | viacam_set_scale(cam); | |
502 | /* | |
503 | * Image size info. | |
504 | */ | |
505 | viacam_write_reg(cam, VCR_MAXDATA, | |
506 | (cam->sensor_format.height << 16) | | |
507 | (cam->sensor_format.bytesperline >> 3)); | |
508 | viacam_write_reg(cam, VCR_MAXVBI, 0); | |
509 | viacam_write_reg(cam, VCR_VSTRIDE, | |
510 | cam->user_format.bytesperline & VCR_VS_STRIDE); | |
511 | /* | |
512 | * Set up the capture interface control register, | |
513 | * everything but the "go" bit. | |
514 | * | |
515 | * The FIFO threshold is a bit of a magic number; 8 is what | |
516 | * VIA's sample code uses. | |
517 | */ | |
518 | cicreg = VCR_CI_CLKEN | | |
519 | 0x08000000 | /* FIFO threshold */ | |
520 | VCR_CI_FLDINV | /* OLPC-specific? */ | |
521 | VCR_CI_VREFINV | /* OLPC-specific? */ | |
522 | VCR_CI_DIBOTH | /* Capture both fields */ | |
523 | VCR_CI_CCIR601_8; | |
524 | if (cam->n_cap_bufs == 3) | |
525 | cicreg |= VCR_CI_3BUFS; | |
526 | /* | |
527 | * YUV formats need different byte swapping than RGB. | |
528 | */ | |
529 | if (cam->user_format.pixelformat == V4L2_PIX_FMT_YUYV) | |
530 | cicreg |= VCR_CI_YUYV; | |
531 | else | |
532 | cicreg |= VCR_CI_UYVY; | |
533 | viacam_write_reg(cam, VCR_CAPINTC, cicreg); | |
534 | } | |
535 | ||
536 | ||
537 | static int viacam_config_controller(struct via_camera *cam) | |
538 | { | |
539 | int ret; | |
540 | unsigned long flags; | |
541 | ||
542 | spin_lock_irqsave(&cam->viadev->reg_lock, flags); | |
543 | ret = viacam_ctlr_cbufs(cam); | |
544 | if (!ret) | |
545 | viacam_ctlr_image(cam); | |
546 | spin_unlock_irqrestore(&cam->viadev->reg_lock, flags); | |
547 | clear_bit(CF_CONFIG_NEEDED, &cam->flags); | |
548 | return ret; | |
549 | } | |
550 | ||
551 | /* | |
552 | * Make it start grabbing data. | |
553 | */ | |
554 | static void viacam_start_engine(struct via_camera *cam) | |
555 | { | |
556 | spin_lock_irq(&cam->viadev->reg_lock); | |
557 | cam->next_buf = 0; | |
558 | viacam_write_reg_mask(cam, VCR_CAPINTC, VCR_CI_ENABLE, VCR_CI_ENABLE); | |
559 | viacam_int_enable(cam); | |
560 | (void) viacam_read_reg(cam, VCR_CAPINTC); /* Force post */ | |
561 | cam->opstate = S_RUNNING; | |
562 | spin_unlock_irq(&cam->viadev->reg_lock); | |
563 | } | |
564 | ||
565 | ||
566 | static void viacam_stop_engine(struct via_camera *cam) | |
567 | { | |
568 | spin_lock_irq(&cam->viadev->reg_lock); | |
569 | viacam_int_disable(cam); | |
570 | viacam_write_reg_mask(cam, VCR_CAPINTC, 0, VCR_CI_ENABLE); | |
571 | (void) viacam_read_reg(cam, VCR_CAPINTC); /* Force post */ | |
572 | cam->opstate = S_IDLE; | |
573 | spin_unlock_irq(&cam->viadev->reg_lock); | |
574 | } | |
575 | ||
576 | ||
577 | /* --------------------------------------------------------------------------*/ | |
578 | /* Videobuf callback ops */ | |
579 | ||
580 | /* | |
581 | * buffer_setup. The purpose of this one would appear to be to tell | |
582 | * videobuf how big a single image is. It's also evidently up to us | |
583 | * to put some sort of limit on the maximum number of buffers allowed. | |
584 | */ | |
585 | static int viacam_vb_buf_setup(struct videobuf_queue *q, | |
586 | unsigned int *count, unsigned int *size) | |
587 | { | |
588 | struct via_camera *cam = q->priv_data; | |
589 | ||
590 | *size = cam->user_format.sizeimage; | |
591 | if (*count == 0 || *count > 6) /* Arbitrary number */ | |
592 | *count = 6; | |
593 | return 0; | |
594 | } | |
595 | ||
596 | /* | |
597 | * Prepare a buffer. | |
598 | */ | |
599 | static int viacam_vb_buf_prepare(struct videobuf_queue *q, | |
600 | struct videobuf_buffer *vb, enum v4l2_field field) | |
601 | { | |
602 | struct via_camera *cam = q->priv_data; | |
603 | ||
604 | vb->size = cam->user_format.sizeimage; | |
605 | vb->width = cam->user_format.width; /* bytesperline???? */ | |
606 | vb->height = cam->user_format.height; | |
607 | vb->field = field; | |
608 | if (vb->state == VIDEOBUF_NEEDS_INIT) { | |
609 | int ret = videobuf_iolock(q, vb, NULL); | |
610 | if (ret) | |
611 | return ret; | |
612 | } | |
613 | vb->state = VIDEOBUF_PREPARED; | |
614 | return 0; | |
615 | } | |
616 | ||
617 | /* | |
618 | * We've got a buffer to put data into. | |
619 | * | |
620 | * FIXME: check for a running engine and valid buffers? | |
621 | */ | |
622 | static void viacam_vb_buf_queue(struct videobuf_queue *q, | |
623 | struct videobuf_buffer *vb) | |
624 | { | |
625 | struct via_camera *cam = q->priv_data; | |
626 | ||
627 | /* | |
628 | * Note that videobuf holds the lock when it calls | |
629 | * us, so we need not (indeed, cannot) take it here. | |
630 | */ | |
631 | vb->state = VIDEOBUF_QUEUED; | |
632 | list_add_tail(&vb->queue, &cam->buffer_queue); | |
633 | } | |
634 | ||
635 | /* | |
636 | * Free a buffer. | |
637 | */ | |
638 | static void viacam_vb_buf_release(struct videobuf_queue *q, | |
639 | struct videobuf_buffer *vb) | |
640 | { | |
641 | struct via_camera *cam = q->priv_data; | |
642 | ||
643 | videobuf_dma_unmap(&cam->platdev->dev, videobuf_to_dma(vb)); | |
644 | videobuf_dma_free(videobuf_to_dma(vb)); | |
645 | vb->state = VIDEOBUF_NEEDS_INIT; | |
646 | } | |
647 | ||
648 | static const struct videobuf_queue_ops viacam_vb_ops = { | |
649 | .buf_setup = viacam_vb_buf_setup, | |
650 | .buf_prepare = viacam_vb_buf_prepare, | |
651 | .buf_queue = viacam_vb_buf_queue, | |
652 | .buf_release = viacam_vb_buf_release, | |
653 | }; | |
654 | ||
655 | /* --------------------------------------------------------------------------*/ | |
656 | /* File operations */ | |
657 | ||
658 | static int viacam_open(struct file *filp) | |
659 | { | |
660 | struct via_camera *cam = video_drvdata(filp); | |
661 | ||
662 | filp->private_data = cam; | |
663 | /* | |
664 | * Note the new user. If this is the first one, we'll also | |
665 | * need to power up the sensor. | |
666 | */ | |
667 | mutex_lock(&cam->lock); | |
668 | if (cam->users == 0) { | |
669 | int ret = viafb_request_dma(); | |
670 | ||
671 | if (ret) { | |
672 | mutex_unlock(&cam->lock); | |
673 | return ret; | |
674 | } | |
675 | via_sensor_power_up(cam); | |
676 | set_bit(CF_CONFIG_NEEDED, &cam->flags); | |
677 | /* | |
678 | * Hook into videobuf. Evidently this cannot fail. | |
679 | */ | |
680 | videobuf_queue_sg_init(&cam->vb_queue, &viacam_vb_ops, | |
681 | &cam->platdev->dev, &cam->viadev->reg_lock, | |
682 | V4L2_BUF_TYPE_VIDEO_CAPTURE, V4L2_FIELD_NONE, | |
683 | sizeof(struct videobuf_buffer), cam, NULL); | |
684 | } | |
685 | (cam->users)++; | |
686 | mutex_unlock(&cam->lock); | |
687 | return 0; | |
688 | } | |
689 | ||
690 | static int viacam_release(struct file *filp) | |
691 | { | |
692 | struct via_camera *cam = video_drvdata(filp); | |
693 | ||
694 | mutex_lock(&cam->lock); | |
695 | (cam->users)--; | |
696 | /* | |
697 | * If the "owner" is closing, shut down any ongoing | |
698 | * operations. | |
699 | */ | |
700 | if (filp == cam->owner) { | |
701 | videobuf_stop(&cam->vb_queue); | |
702 | /* | |
703 | * We don't hold the spinlock here, but, if release() | |
704 | * is being called by the owner, nobody else will | |
705 | * be changing the state. And an extra stop would | |
706 | * not hurt anyway. | |
707 | */ | |
708 | if (cam->opstate != S_IDLE) | |
709 | viacam_stop_engine(cam); | |
710 | cam->owner = NULL; | |
711 | } | |
712 | /* | |
713 | * Last one out needs to turn out the lights. | |
714 | */ | |
715 | if (cam->users == 0) { | |
716 | videobuf_mmap_free(&cam->vb_queue); | |
717 | via_sensor_power_down(cam); | |
718 | viafb_release_dma(); | |
719 | } | |
720 | mutex_unlock(&cam->lock); | |
721 | return 0; | |
722 | } | |
723 | ||
724 | /* | |
725 | * Read a frame from the device. | |
726 | */ | |
727 | static ssize_t viacam_read(struct file *filp, char __user *buffer, | |
728 | size_t len, loff_t *pos) | |
729 | { | |
730 | struct via_camera *cam = video_drvdata(filp); | |
731 | int ret; | |
732 | ||
733 | mutex_lock(&cam->lock); | |
734 | /* | |
735 | * Enforce the V4l2 "only one owner gets to read data" rule. | |
736 | */ | |
737 | if (cam->owner && cam->owner != filp) { | |
738 | ret = -EBUSY; | |
739 | goto out_unlock; | |
740 | } | |
741 | cam->owner = filp; | |
742 | /* | |
743 | * Do we need to configure the hardware? | |
744 | */ | |
745 | if (test_bit(CF_CONFIG_NEEDED, &cam->flags)) { | |
746 | ret = viacam_configure_sensor(cam); | |
747 | if (!ret) | |
748 | ret = viacam_config_controller(cam); | |
749 | if (ret) | |
750 | goto out_unlock; | |
751 | } | |
752 | /* | |
753 | * Fire up the capture engine, then have videobuf do | |
754 | * the heavy lifting. Someday it would be good to avoid | |
755 | * stopping and restarting the engine each time. | |
756 | */ | |
757 | INIT_LIST_HEAD(&cam->buffer_queue); | |
758 | viacam_start_engine(cam); | |
759 | ret = videobuf_read_stream(&cam->vb_queue, buffer, len, pos, 0, | |
760 | filp->f_flags & O_NONBLOCK); | |
761 | viacam_stop_engine(cam); | |
762 | /* videobuf_stop() ?? */ | |
763 | ||
764 | out_unlock: | |
765 | mutex_unlock(&cam->lock); | |
766 | return ret; | |
767 | } | |
768 | ||
769 | ||
770 | static unsigned int viacam_poll(struct file *filp, struct poll_table_struct *pt) | |
771 | { | |
772 | struct via_camera *cam = video_drvdata(filp); | |
773 | ||
774 | return videobuf_poll_stream(filp, &cam->vb_queue, pt); | |
775 | } | |
776 | ||
777 | ||
778 | static int viacam_mmap(struct file *filp, struct vm_area_struct *vma) | |
779 | { | |
780 | struct via_camera *cam = video_drvdata(filp); | |
781 | ||
782 | return videobuf_mmap_mapper(&cam->vb_queue, vma); | |
783 | } | |
784 | ||
785 | ||
786 | ||
787 | static const struct v4l2_file_operations viacam_fops = { | |
788 | .owner = THIS_MODULE, | |
789 | .open = viacam_open, | |
790 | .release = viacam_release, | |
791 | .read = viacam_read, | |
792 | .poll = viacam_poll, | |
793 | .mmap = viacam_mmap, | |
794 | .unlocked_ioctl = video_ioctl2, | |
795 | }; | |
796 | ||
797 | /*----------------------------------------------------------------------------*/ | |
798 | /* | |
799 | * The long list of v4l2 ioctl ops | |
800 | */ | |
801 | ||
024fafba JC |
802 | /* |
803 | * Only one input. | |
804 | */ | |
805 | static int viacam_enum_input(struct file *filp, void *priv, | |
806 | struct v4l2_input *input) | |
807 | { | |
808 | if (input->index != 0) | |
809 | return -EINVAL; | |
810 | ||
811 | input->type = V4L2_INPUT_TYPE_CAMERA; | |
812 | input->std = V4L2_STD_ALL; /* Not sure what should go here */ | |
813 | strcpy(input->name, "Camera"); | |
814 | return 0; | |
815 | } | |
816 | ||
817 | static int viacam_g_input(struct file *filp, void *priv, unsigned int *i) | |
818 | { | |
819 | *i = 0; | |
820 | return 0; | |
821 | } | |
822 | ||
823 | static int viacam_s_input(struct file *filp, void *priv, unsigned int i) | |
824 | { | |
825 | if (i != 0) | |
826 | return -EINVAL; | |
827 | return 0; | |
828 | } | |
829 | ||
314527ac | 830 | static int viacam_s_std(struct file *filp, void *priv, v4l2_std_id std) |
024fafba JC |
831 | { |
832 | return 0; | |
833 | } | |
834 | ||
d31e545b HV |
835 | static int viacam_g_std(struct file *filp, void *priv, v4l2_std_id *std) |
836 | { | |
837 | *std = V4L2_STD_NTSC_M; | |
838 | return 0; | |
839 | } | |
840 | ||
024fafba JC |
841 | /* |
842 | * Video format stuff. Here is our default format until | |
843 | * user space messes with things. | |
844 | */ | |
845 | static const struct v4l2_pix_format viacam_def_pix_format = { | |
846 | .width = VGA_WIDTH, | |
847 | .height = VGA_HEIGHT, | |
848 | .pixelformat = V4L2_PIX_FMT_YUYV, | |
849 | .field = V4L2_FIELD_NONE, | |
850 | .bytesperline = VGA_WIDTH * 2, | |
851 | .sizeimage = VGA_WIDTH * VGA_HEIGHT * 2, | |
852 | }; | |
853 | ||
27ffaeb0 | 854 | static const u32 via_def_mbus_code = MEDIA_BUS_FMT_YUYV8_2X8; |
024fafba JC |
855 | |
856 | static int viacam_enum_fmt_vid_cap(struct file *filp, void *priv, | |
857 | struct v4l2_fmtdesc *fmt) | |
858 | { | |
859 | if (fmt->index >= N_VIA_FMTS) | |
860 | return -EINVAL; | |
861 | strlcpy(fmt->description, via_formats[fmt->index].desc, | |
862 | sizeof(fmt->description)); | |
863 | fmt->pixelformat = via_formats[fmt->index].pixelformat; | |
864 | return 0; | |
865 | } | |
866 | ||
867 | /* | |
868 | * Figure out proper image dimensions, but always force the | |
869 | * sensor to VGA. | |
870 | */ | |
871 | static void viacam_fmt_pre(struct v4l2_pix_format *userfmt, | |
872 | struct v4l2_pix_format *sensorfmt) | |
873 | { | |
874 | *sensorfmt = *userfmt; | |
875 | if (userfmt->width < QCIF_WIDTH || userfmt->height < QCIF_HEIGHT) { | |
876 | userfmt->width = QCIF_WIDTH; | |
877 | userfmt->height = QCIF_HEIGHT; | |
878 | } | |
879 | if (userfmt->width > VGA_WIDTH || userfmt->height > VGA_HEIGHT) { | |
880 | userfmt->width = VGA_WIDTH; | |
881 | userfmt->height = VGA_HEIGHT; | |
882 | } | |
883 | sensorfmt->width = VGA_WIDTH; | |
884 | sensorfmt->height = VGA_HEIGHT; | |
885 | } | |
886 | ||
887 | static void viacam_fmt_post(struct v4l2_pix_format *userfmt, | |
888 | struct v4l2_pix_format *sensorfmt) | |
889 | { | |
890 | struct via_format *f = via_find_format(userfmt->pixelformat); | |
891 | ||
892 | sensorfmt->bytesperline = sensorfmt->width * f->bpp; | |
893 | sensorfmt->sizeimage = sensorfmt->height * sensorfmt->bytesperline; | |
894 | userfmt->pixelformat = sensorfmt->pixelformat; | |
895 | userfmt->field = sensorfmt->field; | |
896 | userfmt->bytesperline = 2 * userfmt->width; | |
897 | userfmt->sizeimage = userfmt->bytesperline * userfmt->height; | |
898 | } | |
899 | ||
900 | ||
901 | /* | |
902 | * The real work of figuring out a workable format. | |
903 | */ | |
904 | static int viacam_do_try_fmt(struct via_camera *cam, | |
905 | struct v4l2_pix_format *upix, struct v4l2_pix_format *spix) | |
906 | { | |
907 | int ret; | |
5eab4983 HV |
908 | struct v4l2_subdev_pad_config pad_cfg; |
909 | struct v4l2_subdev_format format = { | |
910 | .which = V4L2_SUBDEV_FORMAT_TRY, | |
911 | }; | |
024fafba JC |
912 | struct via_format *f = via_find_format(upix->pixelformat); |
913 | ||
914 | upix->pixelformat = f->pixelformat; | |
915 | viacam_fmt_pre(upix, spix); | |
5eab4983 HV |
916 | v4l2_fill_mbus_format(&format.format, spix, f->mbus_code); |
917 | ret = sensor_call(cam, pad, set_fmt, &pad_cfg, &format); | |
918 | v4l2_fill_pix_format(spix, &format.format); | |
024fafba JC |
919 | viacam_fmt_post(upix, spix); |
920 | return ret; | |
921 | } | |
922 | ||
923 | ||
924 | ||
925 | static int viacam_try_fmt_vid_cap(struct file *filp, void *priv, | |
926 | struct v4l2_format *fmt) | |
927 | { | |
928 | struct via_camera *cam = priv; | |
929 | struct v4l2_format sfmt; | |
930 | int ret; | |
931 | ||
932 | mutex_lock(&cam->lock); | |
933 | ret = viacam_do_try_fmt(cam, &fmt->fmt.pix, &sfmt.fmt.pix); | |
934 | mutex_unlock(&cam->lock); | |
935 | return ret; | |
936 | } | |
937 | ||
938 | ||
939 | static int viacam_g_fmt_vid_cap(struct file *filp, void *priv, | |
940 | struct v4l2_format *fmt) | |
941 | { | |
942 | struct via_camera *cam = priv; | |
943 | ||
944 | mutex_lock(&cam->lock); | |
945 | fmt->fmt.pix = cam->user_format; | |
946 | mutex_unlock(&cam->lock); | |
947 | return 0; | |
948 | } | |
949 | ||
950 | static int viacam_s_fmt_vid_cap(struct file *filp, void *priv, | |
951 | struct v4l2_format *fmt) | |
952 | { | |
953 | struct via_camera *cam = priv; | |
954 | int ret; | |
955 | struct v4l2_format sfmt; | |
956 | struct via_format *f = via_find_format(fmt->fmt.pix.pixelformat); | |
957 | ||
958 | /* | |
959 | * Camera must be idle or we can't mess with the | |
960 | * video setup. | |
961 | */ | |
962 | mutex_lock(&cam->lock); | |
963 | if (cam->opstate != S_IDLE) { | |
964 | ret = -EBUSY; | |
965 | goto out; | |
966 | } | |
967 | /* | |
968 | * Let the sensor code look over and tweak the | |
969 | * requested formatting. | |
970 | */ | |
971 | ret = viacam_do_try_fmt(cam, &fmt->fmt.pix, &sfmt.fmt.pix); | |
972 | if (ret) | |
973 | goto out; | |
974 | /* | |
975 | * OK, let's commit to the new format. | |
976 | */ | |
977 | cam->user_format = fmt->fmt.pix; | |
978 | cam->sensor_format = sfmt.fmt.pix; | |
979 | cam->mbus_code = f->mbus_code; | |
980 | ret = viacam_configure_sensor(cam); | |
981 | if (!ret) | |
982 | ret = viacam_config_controller(cam); | |
983 | out: | |
984 | mutex_unlock(&cam->lock); | |
985 | return ret; | |
986 | } | |
987 | ||
988 | static int viacam_querycap(struct file *filp, void *priv, | |
989 | struct v4l2_capability *cap) | |
990 | { | |
991 | strcpy(cap->driver, "via-camera"); | |
992 | strcpy(cap->card, "via-camera"); | |
a020c747 | 993 | cap->device_caps = V4L2_CAP_VIDEO_CAPTURE | |
024fafba | 994 | V4L2_CAP_READWRITE | V4L2_CAP_STREAMING; |
a020c747 | 995 | cap->capabilities = cap->device_caps | V4L2_CAP_DEVICE_CAPS; |
024fafba JC |
996 | return 0; |
997 | } | |
998 | ||
999 | /* | |
1000 | * Streaming operations - pure videobuf stuff. | |
1001 | */ | |
1002 | static int viacam_reqbufs(struct file *filp, void *priv, | |
1003 | struct v4l2_requestbuffers *rb) | |
1004 | { | |
1005 | struct via_camera *cam = priv; | |
1006 | ||
1007 | return videobuf_reqbufs(&cam->vb_queue, rb); | |
1008 | } | |
1009 | ||
1010 | static int viacam_querybuf(struct file *filp, void *priv, | |
1011 | struct v4l2_buffer *buf) | |
1012 | { | |
1013 | struct via_camera *cam = priv; | |
1014 | ||
1015 | return videobuf_querybuf(&cam->vb_queue, buf); | |
1016 | } | |
1017 | ||
1018 | static int viacam_qbuf(struct file *filp, void *priv, struct v4l2_buffer *buf) | |
1019 | { | |
1020 | struct via_camera *cam = priv; | |
1021 | ||
1022 | return videobuf_qbuf(&cam->vb_queue, buf); | |
1023 | } | |
1024 | ||
1025 | static int viacam_dqbuf(struct file *filp, void *priv, struct v4l2_buffer *buf) | |
1026 | { | |
1027 | struct via_camera *cam = priv; | |
1028 | ||
1029 | return videobuf_dqbuf(&cam->vb_queue, buf, filp->f_flags & O_NONBLOCK); | |
1030 | } | |
1031 | ||
1032 | static int viacam_streamon(struct file *filp, void *priv, enum v4l2_buf_type t) | |
1033 | { | |
1034 | struct via_camera *cam = priv; | |
1035 | int ret = 0; | |
1036 | ||
1037 | if (t != V4L2_BUF_TYPE_VIDEO_CAPTURE) | |
1038 | return -EINVAL; | |
1039 | ||
1040 | mutex_lock(&cam->lock); | |
1041 | if (cam->opstate != S_IDLE) { | |
1042 | ret = -EBUSY; | |
1043 | goto out; | |
1044 | } | |
1045 | /* | |
1046 | * Enforce the V4l2 "only one owner gets to read data" rule. | |
1047 | */ | |
1048 | if (cam->owner && cam->owner != filp) { | |
1049 | ret = -EBUSY; | |
1050 | goto out; | |
1051 | } | |
1052 | cam->owner = filp; | |
1053 | /* | |
1054 | * Configure things if need be. | |
1055 | */ | |
1056 | if (test_bit(CF_CONFIG_NEEDED, &cam->flags)) { | |
1057 | ret = viacam_configure_sensor(cam); | |
1058 | if (ret) | |
1059 | goto out; | |
1060 | ret = viacam_config_controller(cam); | |
1061 | if (ret) | |
1062 | goto out; | |
1063 | } | |
1064 | /* | |
1065 | * If the CPU goes into C3, the DMA transfer gets corrupted and | |
1066 | * users start filing unsightly bug reports. Put in a "latency" | |
1067 | * requirement which will keep the CPU out of the deeper sleep | |
1068 | * states. | |
1069 | */ | |
1070 | pm_qos_add_request(&cam->qos_request, PM_QOS_CPU_DMA_LATENCY, 50); | |
1071 | /* | |
1072 | * Fire things up. | |
1073 | */ | |
1074 | INIT_LIST_HEAD(&cam->buffer_queue); | |
1075 | ret = videobuf_streamon(&cam->vb_queue); | |
1076 | if (!ret) | |
1077 | viacam_start_engine(cam); | |
1078 | out: | |
1079 | mutex_unlock(&cam->lock); | |
1080 | return ret; | |
1081 | } | |
1082 | ||
1083 | static int viacam_streamoff(struct file *filp, void *priv, enum v4l2_buf_type t) | |
1084 | { | |
1085 | struct via_camera *cam = priv; | |
1086 | int ret; | |
1087 | ||
1088 | if (t != V4L2_BUF_TYPE_VIDEO_CAPTURE) | |
1089 | return -EINVAL; | |
1090 | mutex_lock(&cam->lock); | |
1091 | if (cam->opstate != S_RUNNING) { | |
1092 | ret = -EINVAL; | |
1093 | goto out; | |
1094 | } | |
1095 | pm_qos_remove_request(&cam->qos_request); | |
1096 | viacam_stop_engine(cam); | |
1097 | /* | |
1098 | * Videobuf will recycle all of the outstanding buffers, but | |
1099 | * we should be sure we don't retain any references to | |
1100 | * any of them. | |
1101 | */ | |
1102 | ret = videobuf_streamoff(&cam->vb_queue); | |
1103 | INIT_LIST_HEAD(&cam->buffer_queue); | |
1104 | out: | |
1105 | mutex_unlock(&cam->lock); | |
1106 | return ret; | |
1107 | } | |
1108 | ||
024fafba JC |
1109 | /* G/S_PARM */ |
1110 | ||
1111 | static int viacam_g_parm(struct file *filp, void *priv, | |
1112 | struct v4l2_streamparm *parm) | |
1113 | { | |
1114 | struct via_camera *cam = priv; | |
1115 | int ret; | |
1116 | ||
1117 | mutex_lock(&cam->lock); | |
1118 | ret = sensor_call(cam, video, g_parm, parm); | |
1119 | mutex_unlock(&cam->lock); | |
1120 | parm->parm.capture.readbuffers = cam->n_cap_bufs; | |
1121 | return ret; | |
1122 | } | |
1123 | ||
1124 | static int viacam_s_parm(struct file *filp, void *priv, | |
1125 | struct v4l2_streamparm *parm) | |
1126 | { | |
1127 | struct via_camera *cam = priv; | |
1128 | int ret; | |
1129 | ||
1130 | mutex_lock(&cam->lock); | |
1131 | ret = sensor_call(cam, video, s_parm, parm); | |
1132 | mutex_unlock(&cam->lock); | |
1133 | parm->parm.capture.readbuffers = cam->n_cap_bufs; | |
1134 | return ret; | |
1135 | } | |
1136 | ||
1137 | static int viacam_enum_framesizes(struct file *filp, void *priv, | |
1138 | struct v4l2_frmsizeenum *sizes) | |
1139 | { | |
1140 | if (sizes->index != 0) | |
1141 | return -EINVAL; | |
1142 | sizes->type = V4L2_FRMSIZE_TYPE_CONTINUOUS; | |
1143 | sizes->stepwise.min_width = QCIF_WIDTH; | |
1144 | sizes->stepwise.min_height = QCIF_HEIGHT; | |
1145 | sizes->stepwise.max_width = VGA_WIDTH; | |
1146 | sizes->stepwise.max_height = VGA_HEIGHT; | |
1147 | sizes->stepwise.step_width = sizes->stepwise.step_height = 1; | |
1148 | return 0; | |
1149 | } | |
1150 | ||
1151 | static int viacam_enum_frameintervals(struct file *filp, void *priv, | |
1152 | struct v4l2_frmivalenum *interval) | |
1153 | { | |
1154 | struct via_camera *cam = priv; | |
17bef885 HV |
1155 | struct v4l2_subdev_frame_interval_enum fie = { |
1156 | .index = interval->index, | |
1157 | .code = cam->mbus_code, | |
1158 | .width = cam->sensor_format.width, | |
1159 | .height = cam->sensor_format.height, | |
1160 | .which = V4L2_SUBDEV_FORMAT_ACTIVE, | |
1161 | }; | |
024fafba JC |
1162 | int ret; |
1163 | ||
1164 | mutex_lock(&cam->lock); | |
17bef885 | 1165 | ret = sensor_call(cam, pad, enum_frame_interval, NULL, &fie); |
024fafba | 1166 | mutex_unlock(&cam->lock); |
17bef885 HV |
1167 | if (ret) |
1168 | return ret; | |
1169 | interval->type = V4L2_FRMIVAL_TYPE_DISCRETE; | |
1170 | interval->discrete = fie.interval; | |
1171 | return 0; | |
024fafba JC |
1172 | } |
1173 | ||
1174 | ||
1175 | ||
1176 | static const struct v4l2_ioctl_ops viacam_ioctl_ops = { | |
024fafba JC |
1177 | .vidioc_enum_input = viacam_enum_input, |
1178 | .vidioc_g_input = viacam_g_input, | |
1179 | .vidioc_s_input = viacam_s_input, | |
1180 | .vidioc_s_std = viacam_s_std, | |
d31e545b | 1181 | .vidioc_g_std = viacam_g_std, |
024fafba JC |
1182 | .vidioc_enum_fmt_vid_cap = viacam_enum_fmt_vid_cap, |
1183 | .vidioc_try_fmt_vid_cap = viacam_try_fmt_vid_cap, | |
1184 | .vidioc_g_fmt_vid_cap = viacam_g_fmt_vid_cap, | |
1185 | .vidioc_s_fmt_vid_cap = viacam_s_fmt_vid_cap, | |
1186 | .vidioc_querycap = viacam_querycap, | |
1187 | .vidioc_reqbufs = viacam_reqbufs, | |
1188 | .vidioc_querybuf = viacam_querybuf, | |
1189 | .vidioc_qbuf = viacam_qbuf, | |
1190 | .vidioc_dqbuf = viacam_dqbuf, | |
1191 | .vidioc_streamon = viacam_streamon, | |
1192 | .vidioc_streamoff = viacam_streamoff, | |
1193 | .vidioc_g_parm = viacam_g_parm, | |
1194 | .vidioc_s_parm = viacam_s_parm, | |
1195 | .vidioc_enum_framesizes = viacam_enum_framesizes, | |
1196 | .vidioc_enum_frameintervals = viacam_enum_frameintervals, | |
024fafba JC |
1197 | }; |
1198 | ||
1199 | /*----------------------------------------------------------------------------*/ | |
1200 | ||
1201 | /* | |
1202 | * Power management. | |
1203 | */ | |
0522921c DD |
1204 | #ifdef CONFIG_PM |
1205 | ||
1206 | static int viacam_suspend(void *priv) | |
1207 | { | |
1208 | struct via_camera *cam = priv; | |
1209 | enum viacam_opstate state = cam->opstate; | |
1210 | ||
1211 | if (cam->opstate != S_IDLE) { | |
1212 | viacam_stop_engine(cam); | |
1213 | cam->opstate = state; /* So resume restarts */ | |
1214 | } | |
1215 | ||
1216 | return 0; | |
1217 | } | |
1218 | ||
1219 | static int viacam_resume(void *priv) | |
1220 | { | |
1221 | struct via_camera *cam = priv; | |
1222 | int ret = 0; | |
1223 | ||
1224 | /* | |
1225 | * Get back to a reasonable operating state. | |
1226 | */ | |
1227 | via_write_reg_mask(VIASR, 0x78, 0, 0x80); | |
1228 | via_write_reg_mask(VIASR, 0x1e, 0xc0, 0xc0); | |
1229 | viacam_int_disable(cam); | |
1230 | set_bit(CF_CONFIG_NEEDED, &cam->flags); | |
1231 | /* | |
1232 | * Make sure the sensor's power state is correct | |
1233 | */ | |
1234 | if (cam->users > 0) | |
1235 | via_sensor_power_up(cam); | |
1236 | else | |
1237 | via_sensor_power_down(cam); | |
1238 | /* | |
1239 | * If it was operating, try to restart it. | |
1240 | */ | |
1241 | if (cam->opstate != S_IDLE) { | |
1242 | mutex_lock(&cam->lock); | |
1243 | ret = viacam_configure_sensor(cam); | |
1244 | if (!ret) | |
1245 | ret = viacam_config_controller(cam); | |
1246 | mutex_unlock(&cam->lock); | |
1247 | if (!ret) | |
1248 | viacam_start_engine(cam); | |
1249 | } | |
1250 | ||
1251 | return ret; | |
1252 | } | |
1253 | ||
1254 | static struct viafb_pm_hooks viacam_pm_hooks = { | |
1255 | .suspend = viacam_suspend, | |
1256 | .resume = viacam_resume | |
1257 | }; | |
1258 | ||
1259 | #endif /* CONFIG_PM */ | |
024fafba JC |
1260 | |
1261 | /* | |
1262 | * Setup stuff. | |
1263 | */ | |
1264 | ||
1265 | static struct video_device viacam_v4l_template = { | |
1266 | .name = "via-camera", | |
1267 | .minor = -1, | |
1268 | .tvnorms = V4L2_STD_NTSC_M, | |
024fafba JC |
1269 | .fops = &viacam_fops, |
1270 | .ioctl_ops = &viacam_ioctl_ops, | |
1271 | .release = video_device_release_empty, /* Check this */ | |
1272 | }; | |
1273 | ||
c6384c88 DD |
1274 | /* |
1275 | * The OLPC folks put the serial port on the same pin as | |
1276 | * the camera. They also get grumpy if we break the | |
1277 | * serial port and keep them from using it. So we have | |
1278 | * to check the serial enable bit and not step on it. | |
1279 | */ | |
1280 | #define VIACAM_SERIAL_DEVFN 0x88 | |
1281 | #define VIACAM_SERIAL_CREG 0x46 | |
1282 | #define VIACAM_SERIAL_BIT 0x40 | |
1283 | ||
4c62e976 | 1284 | static bool viacam_serial_is_enabled(void) |
c6384c88 DD |
1285 | { |
1286 | struct pci_bus *pbus = pci_find_bus(0, 0); | |
1287 | u8 cbyte; | |
1288 | ||
c8814df3 JJ |
1289 | if (!pbus) |
1290 | return false; | |
c6384c88 DD |
1291 | pci_bus_read_config_byte(pbus, VIACAM_SERIAL_DEVFN, |
1292 | VIACAM_SERIAL_CREG, &cbyte); | |
1293 | if ((cbyte & VIACAM_SERIAL_BIT) == 0) | |
1294 | return false; /* Not enabled */ | |
b2617dc3 | 1295 | if (!override_serial) { |
c6384c88 DD |
1296 | printk(KERN_NOTICE "Via camera: serial port is enabled, " \ |
1297 | "refusing to load.\n"); | |
1298 | printk(KERN_NOTICE "Specify override_serial=1 to force " \ | |
1299 | "module loading.\n"); | |
1300 | return true; | |
1301 | } | |
1302 | printk(KERN_NOTICE "Via camera: overriding serial port\n"); | |
1303 | pci_bus_write_config_byte(pbus, VIACAM_SERIAL_DEVFN, | |
1304 | VIACAM_SERIAL_CREG, cbyte & ~VIACAM_SERIAL_BIT); | |
1305 | return false; | |
1306 | } | |
024fafba | 1307 | |
a39fbb1d DD |
1308 | static struct ov7670_config sensor_cfg = { |
1309 | /* The XO-1.5 (only known user) clocks the camera at 90MHz. */ | |
1310 | .clock_speed = 90, | |
1311 | }; | |
1312 | ||
4c62e976 | 1313 | static int viacam_probe(struct platform_device *pdev) |
024fafba JC |
1314 | { |
1315 | int ret; | |
1316 | struct i2c_adapter *sensor_adapter; | |
1317 | struct viafb_dev *viadev = pdev->dev.platform_data; | |
a39fbb1d DD |
1318 | struct i2c_board_info ov7670_info = { |
1319 | .type = "ov7670", | |
1320 | .addr = 0x42 >> 1, | |
1321 | .platform_data = &sensor_cfg, | |
1322 | }; | |
024fafba JC |
1323 | |
1324 | /* | |
1325 | * Note that there are actually two capture channels on | |
1326 | * the device. We only deal with one for now. That | |
1327 | * is encoded here; nothing else assumes it's dealing with | |
1328 | * a unique capture device. | |
1329 | */ | |
1330 | struct via_camera *cam; | |
1331 | ||
1332 | /* | |
1333 | * Ensure that frame buffer memory has been set aside for | |
1334 | * this purpose. As an arbitrary limit, refuse to work | |
1335 | * with less than two frames of VGA 16-bit data. | |
1336 | * | |
1337 | * If we ever support the second port, we'll need to set | |
1338 | * aside more memory. | |
1339 | */ | |
1340 | if (viadev->camera_fbmem_size < (VGA_HEIGHT*VGA_WIDTH*4)) { | |
1341 | printk(KERN_ERR "viacam: insufficient FB memory reserved\n"); | |
1342 | return -ENOMEM; | |
1343 | } | |
1344 | if (viadev->engine_mmio == NULL) { | |
1345 | printk(KERN_ERR "viacam: No I/O memory, so no pictures\n"); | |
1346 | return -ENOMEM; | |
1347 | } | |
c6384c88 DD |
1348 | |
1349 | if (machine_is_olpc() && viacam_serial_is_enabled()) | |
1350 | return -EBUSY; | |
1351 | ||
024fafba JC |
1352 | /* |
1353 | * Basic structure initialization. | |
1354 | */ | |
1355 | cam = kzalloc (sizeof(struct via_camera), GFP_KERNEL); | |
1356 | if (cam == NULL) | |
1357 | return -ENOMEM; | |
1358 | via_cam_info = cam; | |
1359 | cam->platdev = pdev; | |
1360 | cam->viadev = viadev; | |
1361 | cam->users = 0; | |
1362 | cam->owner = NULL; | |
1363 | cam->opstate = S_IDLE; | |
1364 | cam->user_format = cam->sensor_format = viacam_def_pix_format; | |
1365 | mutex_init(&cam->lock); | |
1366 | INIT_LIST_HEAD(&cam->buffer_queue); | |
1367 | cam->mmio = viadev->engine_mmio; | |
1368 | cam->fbmem = viadev->fbmem; | |
1369 | cam->fb_offset = viadev->camera_fbmem_offset; | |
1370 | cam->flags = 1 << CF_CONFIG_NEEDED; | |
1371 | cam->mbus_code = via_def_mbus_code; | |
1372 | /* | |
1373 | * Tell V4L that we exist. | |
1374 | */ | |
1375 | ret = v4l2_device_register(&pdev->dev, &cam->v4l2_dev); | |
1376 | if (ret) { | |
1377 | dev_err(&pdev->dev, "Unable to register v4l2 device\n"); | |
dbf8f4e5 | 1378 | goto out_free; |
024fafba | 1379 | } |
dbf8f4e5 JM |
1380 | ret = v4l2_ctrl_handler_init(&cam->ctrl_handler, 10); |
1381 | if (ret) | |
1382 | goto out_unregister; | |
1383 | cam->v4l2_dev.ctrl_handler = &cam->ctrl_handler; | |
024fafba JC |
1384 | /* |
1385 | * Convince the system that we can do DMA. | |
1386 | */ | |
1387 | pdev->dev.dma_mask = &viadev->pdev->dma_mask; | |
1388 | dma_set_mask(&pdev->dev, 0xffffffff); | |
1389 | /* | |
1390 | * Fire up the capture port. The write to 0x78 looks purely | |
1391 | * OLPCish; any system will need to tweak 0x1e. | |
1392 | */ | |
1393 | via_write_reg_mask(VIASR, 0x78, 0, 0x80); | |
1394 | via_write_reg_mask(VIASR, 0x1e, 0xc0, 0xc0); | |
1395 | /* | |
1396 | * Get the sensor powered up. | |
1397 | */ | |
1398 | ret = via_sensor_power_setup(cam); | |
1399 | if (ret) | |
dbf8f4e5 | 1400 | goto out_ctrl_hdl_free; |
024fafba JC |
1401 | via_sensor_power_up(cam); |
1402 | ||
1403 | /* | |
1404 | * See if we can't find it on the bus. The VIA_PORT_31 assumption | |
1405 | * is OLPC-specific. 0x42 assumption is ov7670-specific. | |
1406 | */ | |
1407 | sensor_adapter = viafb_find_i2c_adapter(VIA_PORT_31); | |
a39fbb1d DD |
1408 | cam->sensor = v4l2_i2c_new_subdev_board(&cam->v4l2_dev, sensor_adapter, |
1409 | &ov7670_info, NULL); | |
024fafba JC |
1410 | if (cam->sensor == NULL) { |
1411 | dev_err(&pdev->dev, "Unable to find the sensor!\n"); | |
1412 | ret = -ENODEV; | |
1413 | goto out_power_down; | |
1414 | } | |
1415 | /* | |
1416 | * Get the IRQ. | |
1417 | */ | |
1418 | viacam_int_disable(cam); | |
1419 | ret = request_threaded_irq(viadev->pdev->irq, viacam_quick_irq, | |
1420 | viacam_irq, IRQF_SHARED, "via-camera", cam); | |
1421 | if (ret) | |
1422 | goto out_power_down; | |
1423 | /* | |
1424 | * Tell V4l2 that we exist. | |
1425 | */ | |
1426 | cam->vdev = viacam_v4l_template; | |
1427 | cam->vdev.v4l2_dev = &cam->v4l2_dev; | |
1428 | ret = video_register_device(&cam->vdev, VFL_TYPE_GRABBER, -1); | |
1429 | if (ret) | |
1430 | goto out_irq; | |
1431 | video_set_drvdata(&cam->vdev, cam); | |
1432 | ||
0522921c DD |
1433 | #ifdef CONFIG_PM |
1434 | /* | |
1435 | * Hook into PM events | |
1436 | */ | |
1437 | viacam_pm_hooks.private = cam; | |
1438 | viafb_pm_register(&viacam_pm_hooks); | |
1439 | #endif | |
1440 | ||
024fafba JC |
1441 | /* Power the sensor down until somebody opens the device */ |
1442 | via_sensor_power_down(cam); | |
1443 | return 0; | |
1444 | ||
1445 | out_irq: | |
1446 | free_irq(viadev->pdev->irq, cam); | |
1447 | out_power_down: | |
1448 | via_sensor_power_release(cam); | |
dbf8f4e5 JM |
1449 | out_ctrl_hdl_free: |
1450 | v4l2_ctrl_handler_free(&cam->ctrl_handler); | |
024fafba JC |
1451 | out_unregister: |
1452 | v4l2_device_unregister(&cam->v4l2_dev); | |
dbf8f4e5 JM |
1453 | out_free: |
1454 | kfree(cam); | |
024fafba JC |
1455 | return ret; |
1456 | } | |
1457 | ||
4c62e976 | 1458 | static int viacam_remove(struct platform_device *pdev) |
024fafba JC |
1459 | { |
1460 | struct via_camera *cam = via_cam_info; | |
1461 | struct viafb_dev *viadev = pdev->dev.platform_data; | |
1462 | ||
1463 | video_unregister_device(&cam->vdev); | |
1464 | v4l2_device_unregister(&cam->v4l2_dev); | |
1465 | free_irq(viadev->pdev->irq, cam); | |
1466 | via_sensor_power_release(cam); | |
dbf8f4e5 JM |
1467 | v4l2_ctrl_handler_free(&cam->ctrl_handler); |
1468 | kfree(cam); | |
024fafba JC |
1469 | via_cam_info = NULL; |
1470 | return 0; | |
1471 | } | |
1472 | ||
024fafba JC |
1473 | static struct platform_driver viacam_driver = { |
1474 | .driver = { | |
1475 | .name = "viafb-camera", | |
1476 | }, | |
1477 | .probe = viacam_probe, | |
1478 | .remove = viacam_remove, | |
1479 | }; | |
1480 | ||
1d6629b1 | 1481 | module_platform_driver(viacam_driver); |