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