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