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