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