2 * Driver for the VIA Chrome integrated camera controller.
4 * Copyright 2009,2010 Jonathan Corbet <corbet@lwn.net>
5 * Distributable under the terms of the GNU General Public License, version 2
7 * This work was supported by the One Laptop Per Child project
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/v4l2-ctrls.h>
22 #include <media/ov7670.h>
23 #include <media/videobuf-dma-sg.h>
24 #include <linux/delay.h>
25 #include <linux/dma-mapping.h>
26 #include <linux/pm_qos.h>
27 #include <linux/via-core.h>
28 #include <linux/via-gpio.h>
29 #include <linux/via_i2c.h>
32 #include "via-camera.h"
34 MODULE_ALIAS("platform:viafb-camera");
35 MODULE_AUTHOR("Jonathan Corbet <corbet@lwn.net>");
36 MODULE_DESCRIPTION("VIA framebuffer-based camera controller driver");
37 MODULE_LICENSE("GPL");
39 static bool flip_image;
40 module_param(flip_image, bool, 0444);
41 MODULE_PARM_DESC(flip_image,
42 "If set, the sensor will be instructed to flip the image "
45 static bool override_serial;
46 module_param(override_serial, bool, 0444);
47 MODULE_PARM_DESC(override_serial,
48 "The camera driver will normally refuse to load if "
49 "the XO 1.5 serial port is enabled. Set this option "
50 "to force-enable the camera.");
56 #define VGA_HEIGHT 480
57 #define QCIF_WIDTH 176
58 #define QCIF_HEIGHT 144
61 * The structure describing our camera.
63 enum viacam_opstate { S_IDLE = 0, S_RUNNING = 1 };
66 struct v4l2_device v4l2_dev;
67 struct v4l2_ctrl_handler ctrl_handler;
68 struct video_device vdev;
69 struct v4l2_subdev *sensor;
70 struct platform_device *platdev;
71 struct viafb_dev *viadev;
73 enum viacam_opstate opstate;
75 struct pm_qos_request qos_request;
77 * GPIO info for power/reset management
84 void __iomem *mmio; /* Where the registers live */
85 void __iomem *fbmem; /* Frame buffer memory */
86 u32 fb_offset; /* Reserved memory offset (FB) */
88 * Capture buffers and related. The controller supports
89 * up to three, so that's what we have here. These buffers
90 * live in frame buffer memory, so we don't call them "DMA".
92 unsigned int cb_offsets[3]; /* offsets into fb mem */
93 u8 *cb_addrs[3]; /* Kernel-space addresses */
94 int n_cap_bufs; /* How many are we using? */
96 struct videobuf_queue vb_queue;
97 struct list_head buffer_queue; /* prot. by reg_lock */
104 * Video format information. sensor_format is kept in a form
105 * that we can use to pass to the sensor. We always run the
106 * sensor in VGA resolution, though, and let the controller
107 * downscale things if need be. So we keep the "real*
108 * dimensions separately.
110 struct v4l2_pix_format sensor_format;
111 struct v4l2_pix_format user_format;
112 enum v4l2_mbus_pixelcode mbus_code;
116 * Yes, this is a hack, but there's only going to be one of these
117 * on any system we know of.
119 static struct via_camera *via_cam_info;
122 * Flag values, manipulated with bitops
124 #define CF_DMA_ACTIVE 0 /* A frame is incoming */
125 #define CF_CONFIG_NEEDED 1 /* Must configure hardware */
129 * Nasty ugly v4l2 boilerplate.
131 #define sensor_call(cam, optype, func, args...) \
132 v4l2_subdev_call(cam->sensor, optype, func, ##args)
135 * Debugging and related.
137 #define cam_err(cam, fmt, arg...) \
138 dev_err(&(cam)->platdev->dev, fmt, ##arg);
139 #define cam_warn(cam, fmt, arg...) \
140 dev_warn(&(cam)->platdev->dev, fmt, ##arg);
141 #define cam_dbg(cam, fmt, arg...) \
142 dev_dbg(&(cam)->platdev->dev, fmt, ##arg);
145 * Format handling. This is ripped almost directly from Hans's changes
146 * to cafe_ccic.c. It's a little unfortunate; until this change, we
147 * didn't need to know anything about the format except its byte depth;
148 * now this information must be managed at this level too.
150 static struct via_format {
153 int bpp; /* Bytes per pixel */
154 enum v4l2_mbus_pixelcode mbus_code;
157 .desc = "YUYV 4:2:2",
158 .pixelformat = V4L2_PIX_FMT_YUYV,
159 .mbus_code = V4L2_MBUS_FMT_YUYV8_2X8,
162 /* RGB444 and Bayer should be doable, but have never been
163 tested with this driver. RGB565 seems to work at the default
164 resolution, but results in color corruption when being scaled by
165 viacam_set_scaled(), and is disabled as a result. */
167 #define N_VIA_FMTS ARRAY_SIZE(via_formats)
169 static struct via_format *via_find_format(u32 pixelformat)
173 for (i = 0; i < N_VIA_FMTS; i++)
174 if (via_formats[i].pixelformat == pixelformat)
175 return via_formats + i;
176 /* Not found? Then return the first format. */
181 /*--------------------------------------------------------------------------*/
183 * Sensor power/reset management. This piece is OLPC-specific for
184 * sure; other configurations will have things connected differently.
186 static int via_sensor_power_setup(struct via_camera *cam)
190 cam->power_gpio = viafb_gpio_lookup("VGPIO3");
191 cam->reset_gpio = viafb_gpio_lookup("VGPIO2");
192 if (cam->power_gpio < 0 || cam->reset_gpio < 0) {
193 dev_err(&cam->platdev->dev, "Unable to find GPIO lines\n");
196 ret = gpio_request(cam->power_gpio, "viafb-camera");
198 dev_err(&cam->platdev->dev, "Unable to request power GPIO\n");
201 ret = gpio_request(cam->reset_gpio, "viafb-camera");
203 dev_err(&cam->platdev->dev, "Unable to request reset GPIO\n");
204 gpio_free(cam->power_gpio);
207 gpio_direction_output(cam->power_gpio, 0);
208 gpio_direction_output(cam->reset_gpio, 0);
213 * Power up the sensor and perform the reset dance.
215 static void via_sensor_power_up(struct via_camera *cam)
217 gpio_set_value(cam->power_gpio, 1);
218 gpio_set_value(cam->reset_gpio, 0);
219 msleep(20); /* Probably excessive */
220 gpio_set_value(cam->reset_gpio, 1);
224 static void via_sensor_power_down(struct via_camera *cam)
226 gpio_set_value(cam->power_gpio, 0);
227 gpio_set_value(cam->reset_gpio, 0);
231 static void via_sensor_power_release(struct via_camera *cam)
233 via_sensor_power_down(cam);
234 gpio_free(cam->power_gpio);
235 gpio_free(cam->reset_gpio);
238 /* --------------------------------------------------------------------------*/
242 * Manage the ov7670 "flip" bit, which needs special help.
244 static int viacam_set_flip(struct via_camera *cam)
246 struct v4l2_control ctrl;
248 memset(&ctrl, 0, sizeof(ctrl));
249 ctrl.id = V4L2_CID_VFLIP;
250 ctrl.value = flip_image;
251 return sensor_call(cam, core, s_ctrl, &ctrl);
255 * Configure the sensor. It's up to the caller to ensure
256 * that the camera is in the correct operating state.
258 static int viacam_configure_sensor(struct via_camera *cam)
260 struct v4l2_mbus_framefmt mbus_fmt;
263 v4l2_fill_mbus_format(&mbus_fmt, &cam->sensor_format, cam->mbus_code);
264 ret = sensor_call(cam, core, init, 0);
266 ret = sensor_call(cam, video, s_mbus_fmt, &mbus_fmt);
268 * OV7670 does weird things if flip is set *before* format...
271 ret = viacam_set_flip(cam);
277 /* --------------------------------------------------------------------------*/
279 * Some simple register accessors; they assume that the lock is held.
281 * Should we want to support the second capture engine, we could
282 * hide the register difference by adding 0x1000 to registers in the
285 static inline void viacam_write_reg(struct via_camera *cam,
288 iowrite32(value, cam->mmio + reg);
291 static inline int viacam_read_reg(struct via_camera *cam, int reg)
293 return ioread32(cam->mmio + reg);
296 static inline void viacam_write_reg_mask(struct via_camera *cam,
297 int reg, int value, int mask)
299 int tmp = viacam_read_reg(cam, reg);
301 tmp = (tmp & ~mask) | (value & mask);
302 viacam_write_reg(cam, reg, tmp);
306 /* --------------------------------------------------------------------------*/
307 /* Interrupt management and handling */
309 static irqreturn_t viacam_quick_irq(int irq, void *data)
311 struct via_camera *cam = data;
312 irqreturn_t ret = IRQ_NONE;
316 * All we do here is to clear the interrupts and tell
317 * the handler thread to wake up.
319 spin_lock(&cam->viadev->reg_lock);
320 icv = viacam_read_reg(cam, VCR_INTCTRL);
321 if (icv & VCR_IC_EAV) {
322 icv |= VCR_IC_EAV|VCR_IC_EVBI|VCR_IC_FFULL;
323 viacam_write_reg(cam, VCR_INTCTRL, icv);
324 ret = IRQ_WAKE_THREAD;
326 spin_unlock(&cam->viadev->reg_lock);
331 * Find the next videobuf buffer which has somebody waiting on it.
333 static struct videobuf_buffer *viacam_next_buffer(struct via_camera *cam)
336 struct videobuf_buffer *buf = NULL;
338 spin_lock_irqsave(&cam->viadev->reg_lock, flags);
339 if (cam->opstate != S_RUNNING)
341 if (list_empty(&cam->buffer_queue))
343 buf = list_entry(cam->buffer_queue.next, struct videobuf_buffer, queue);
344 if (!waitqueue_active(&buf->done)) {/* Nobody waiting */
348 list_del(&buf->queue);
349 buf->state = VIDEOBUF_ACTIVE;
351 spin_unlock_irqrestore(&cam->viadev->reg_lock, flags);
356 * The threaded IRQ handler.
358 static irqreturn_t viacam_irq(int irq, void *data)
361 struct videobuf_buffer *vb;
362 struct via_camera *cam = data;
363 struct videobuf_dmabuf *vdma;
366 * If there is no place to put the data frame, don't bother
367 * with anything else.
369 vb = viacam_next_buffer(cam);
373 * Figure out which buffer we just completed.
375 bufn = (viacam_read_reg(cam, VCR_INTCTRL) & VCR_IC_ACTBUF) >> 3;
378 bufn = cam->n_cap_bufs - 1;
380 * Copy over the data and let any waiters know.
382 vdma = videobuf_to_dma(vb);
383 viafb_dma_copy_out_sg(cam->cb_offsets[bufn], vdma->sglist, vdma->sglen);
384 vb->state = VIDEOBUF_DONE;
385 vb->size = cam->user_format.sizeimage;
393 * These functions must mess around with the general interrupt
394 * control register, which is relevant to much more than just the
395 * camera. Nothing else uses interrupts, though, as of this writing.
396 * Should that situation change, we'll have to improve support at
397 * the via-core level.
399 static void viacam_int_enable(struct via_camera *cam)
401 viacam_write_reg(cam, VCR_INTCTRL,
402 VCR_IC_INTEN|VCR_IC_EAV|VCR_IC_EVBI|VCR_IC_FFULL);
403 viafb_irq_enable(VDE_I_C0AVEN);
406 static void viacam_int_disable(struct via_camera *cam)
408 viafb_irq_disable(VDE_I_C0AVEN);
409 viacam_write_reg(cam, VCR_INTCTRL, 0);
414 /* --------------------------------------------------------------------------*/
415 /* Controller operations */
418 * Set up our capture buffers in framebuffer memory.
420 static int viacam_ctlr_cbufs(struct via_camera *cam)
422 int nbuf = cam->viadev->camera_fbmem_size/cam->sensor_format.sizeimage;
427 * See how many buffers we can work with.
431 viacam_write_reg_mask(cam, VCR_CAPINTC, VCR_CI_3BUFS,
433 } else if (nbuf == 2) {
435 viacam_write_reg_mask(cam, VCR_CAPINTC, 0, VCR_CI_3BUFS);
437 cam_warn(cam, "Insufficient frame buffer memory\n");
443 offset = cam->fb_offset;
444 for (i = 0; i < cam->n_cap_bufs; i++) {
445 cam->cb_offsets[i] = offset;
446 cam->cb_addrs[i] = cam->fbmem + offset;
447 viacam_write_reg(cam, VCR_VBUF1 + i*4, offset & VCR_VBUF_MASK);
448 offset += cam->sensor_format.sizeimage;
454 * Set the scaling register for downscaling the image.
456 * This register works like this... Vertical scaling is enabled
457 * by bit 26; if that bit is set, downscaling is controlled by the
458 * value in bits 16:25. Those bits are divided by 1024 to get
459 * the scaling factor; setting just bit 25 thus cuts the height
462 * Horizontal scaling works about the same, but it's enabled by
463 * bit 11, with bits 0:10 giving the numerator of a fraction
464 * (over 2048) for the scaling value.
466 * This function is naive in that, if the user departs from
467 * the 3x4 VGA scaling factor, the image will distort. We
468 * could work around that if it really seemed important.
470 static void viacam_set_scale(struct via_camera *cam)
472 unsigned int avscale;
475 if (cam->user_format.width == VGA_WIDTH)
478 sf = (cam->user_format.width*2048)/VGA_WIDTH;
479 avscale = VCR_AVS_HEN | sf;
481 if (cam->user_format.height < VGA_HEIGHT) {
482 sf = (1024*cam->user_format.height)/VGA_HEIGHT;
483 avscale |= VCR_AVS_VEN | (sf << 16);
485 viacam_write_reg(cam, VCR_AVSCALE, avscale);
490 * Configure image-related information into the capture engine.
492 static void viacam_ctlr_image(struct via_camera *cam)
497 * Disable clock before messing with stuff - from the via
500 viacam_write_reg(cam, VCR_CAPINTC, ~(VCR_CI_ENABLE|VCR_CI_CLKEN));
502 * Set up the controller for VGA resolution, modulo magic
503 * offsets from the via sample driver.
505 viacam_write_reg(cam, VCR_HORRANGE, 0x06200120);
506 viacam_write_reg(cam, VCR_VERTRANGE, 0x01de0000);
507 viacam_set_scale(cam);
511 viacam_write_reg(cam, VCR_MAXDATA,
512 (cam->sensor_format.height << 16) |
513 (cam->sensor_format.bytesperline >> 3));
514 viacam_write_reg(cam, VCR_MAXVBI, 0);
515 viacam_write_reg(cam, VCR_VSTRIDE,
516 cam->user_format.bytesperline & VCR_VS_STRIDE);
518 * Set up the capture interface control register,
519 * everything but the "go" bit.
521 * The FIFO threshold is a bit of a magic number; 8 is what
522 * VIA's sample code uses.
524 cicreg = VCR_CI_CLKEN |
525 0x08000000 | /* FIFO threshold */
526 VCR_CI_FLDINV | /* OLPC-specific? */
527 VCR_CI_VREFINV | /* OLPC-specific? */
528 VCR_CI_DIBOTH | /* Capture both fields */
530 if (cam->n_cap_bufs == 3)
531 cicreg |= VCR_CI_3BUFS;
533 * YUV formats need different byte swapping than RGB.
535 if (cam->user_format.pixelformat == V4L2_PIX_FMT_YUYV)
536 cicreg |= VCR_CI_YUYV;
538 cicreg |= VCR_CI_UYVY;
539 viacam_write_reg(cam, VCR_CAPINTC, cicreg);
543 static int viacam_config_controller(struct via_camera *cam)
548 spin_lock_irqsave(&cam->viadev->reg_lock, flags);
549 ret = viacam_ctlr_cbufs(cam);
551 viacam_ctlr_image(cam);
552 spin_unlock_irqrestore(&cam->viadev->reg_lock, flags);
553 clear_bit(CF_CONFIG_NEEDED, &cam->flags);
558 * Make it start grabbing data.
560 static void viacam_start_engine(struct via_camera *cam)
562 spin_lock_irq(&cam->viadev->reg_lock);
564 viacam_write_reg_mask(cam, VCR_CAPINTC, VCR_CI_ENABLE, VCR_CI_ENABLE);
565 viacam_int_enable(cam);
566 (void) viacam_read_reg(cam, VCR_CAPINTC); /* Force post */
567 cam->opstate = S_RUNNING;
568 spin_unlock_irq(&cam->viadev->reg_lock);
572 static void viacam_stop_engine(struct via_camera *cam)
574 spin_lock_irq(&cam->viadev->reg_lock);
575 viacam_int_disable(cam);
576 viacam_write_reg_mask(cam, VCR_CAPINTC, 0, VCR_CI_ENABLE);
577 (void) viacam_read_reg(cam, VCR_CAPINTC); /* Force post */
578 cam->opstate = S_IDLE;
579 spin_unlock_irq(&cam->viadev->reg_lock);
583 /* --------------------------------------------------------------------------*/
584 /* Videobuf callback ops */
587 * buffer_setup. The purpose of this one would appear to be to tell
588 * videobuf how big a single image is. It's also evidently up to us
589 * to put some sort of limit on the maximum number of buffers allowed.
591 static int viacam_vb_buf_setup(struct videobuf_queue *q,
592 unsigned int *count, unsigned int *size)
594 struct via_camera *cam = q->priv_data;
596 *size = cam->user_format.sizeimage;
597 if (*count == 0 || *count > 6) /* Arbitrary number */
605 static int viacam_vb_buf_prepare(struct videobuf_queue *q,
606 struct videobuf_buffer *vb, enum v4l2_field field)
608 struct via_camera *cam = q->priv_data;
610 vb->size = cam->user_format.sizeimage;
611 vb->width = cam->user_format.width; /* bytesperline???? */
612 vb->height = cam->user_format.height;
614 if (vb->state == VIDEOBUF_NEEDS_INIT) {
615 int ret = videobuf_iolock(q, vb, NULL);
619 vb->state = VIDEOBUF_PREPARED;
624 * We've got a buffer to put data into.
626 * FIXME: check for a running engine and valid buffers?
628 static void viacam_vb_buf_queue(struct videobuf_queue *q,
629 struct videobuf_buffer *vb)
631 struct via_camera *cam = q->priv_data;
634 * Note that videobuf holds the lock when it calls
635 * us, so we need not (indeed, cannot) take it here.
637 vb->state = VIDEOBUF_QUEUED;
638 list_add_tail(&vb->queue, &cam->buffer_queue);
644 static void viacam_vb_buf_release(struct videobuf_queue *q,
645 struct videobuf_buffer *vb)
647 struct via_camera *cam = q->priv_data;
649 videobuf_dma_unmap(&cam->platdev->dev, videobuf_to_dma(vb));
650 videobuf_dma_free(videobuf_to_dma(vb));
651 vb->state = VIDEOBUF_NEEDS_INIT;
654 static const struct videobuf_queue_ops viacam_vb_ops = {
655 .buf_setup = viacam_vb_buf_setup,
656 .buf_prepare = viacam_vb_buf_prepare,
657 .buf_queue = viacam_vb_buf_queue,
658 .buf_release = viacam_vb_buf_release,
661 /* --------------------------------------------------------------------------*/
662 /* File operations */
664 static int viacam_open(struct file *filp)
666 struct via_camera *cam = video_drvdata(filp);
668 filp->private_data = cam;
670 * Note the new user. If this is the first one, we'll also
671 * need to power up the sensor.
673 mutex_lock(&cam->lock);
674 if (cam->users == 0) {
675 int ret = viafb_request_dma();
678 mutex_unlock(&cam->lock);
681 via_sensor_power_up(cam);
682 set_bit(CF_CONFIG_NEEDED, &cam->flags);
684 * Hook into videobuf. Evidently this cannot fail.
686 videobuf_queue_sg_init(&cam->vb_queue, &viacam_vb_ops,
687 &cam->platdev->dev, &cam->viadev->reg_lock,
688 V4L2_BUF_TYPE_VIDEO_CAPTURE, V4L2_FIELD_NONE,
689 sizeof(struct videobuf_buffer), cam, NULL);
692 mutex_unlock(&cam->lock);
696 static int viacam_release(struct file *filp)
698 struct via_camera *cam = video_drvdata(filp);
700 mutex_lock(&cam->lock);
703 * If the "owner" is closing, shut down any ongoing
706 if (filp == cam->owner) {
707 videobuf_stop(&cam->vb_queue);
709 * We don't hold the spinlock here, but, if release()
710 * is being called by the owner, nobody else will
711 * be changing the state. And an extra stop would
714 if (cam->opstate != S_IDLE)
715 viacam_stop_engine(cam);
719 * Last one out needs to turn out the lights.
721 if (cam->users == 0) {
722 videobuf_mmap_free(&cam->vb_queue);
723 via_sensor_power_down(cam);
726 mutex_unlock(&cam->lock);
731 * Read a frame from the device.
733 static ssize_t viacam_read(struct file *filp, char __user *buffer,
734 size_t len, loff_t *pos)
736 struct via_camera *cam = video_drvdata(filp);
739 mutex_lock(&cam->lock);
741 * Enforce the V4l2 "only one owner gets to read data" rule.
743 if (cam->owner && cam->owner != filp) {
749 * Do we need to configure the hardware?
751 if (test_bit(CF_CONFIG_NEEDED, &cam->flags)) {
752 ret = viacam_configure_sensor(cam);
754 ret = viacam_config_controller(cam);
759 * Fire up the capture engine, then have videobuf do
760 * the heavy lifting. Someday it would be good to avoid
761 * stopping and restarting the engine each time.
763 INIT_LIST_HEAD(&cam->buffer_queue);
764 viacam_start_engine(cam);
765 ret = videobuf_read_stream(&cam->vb_queue, buffer, len, pos, 0,
766 filp->f_flags & O_NONBLOCK);
767 viacam_stop_engine(cam);
768 /* videobuf_stop() ?? */
771 mutex_unlock(&cam->lock);
776 static unsigned int viacam_poll(struct file *filp, struct poll_table_struct *pt)
778 struct via_camera *cam = video_drvdata(filp);
780 return videobuf_poll_stream(filp, &cam->vb_queue, pt);
784 static int viacam_mmap(struct file *filp, struct vm_area_struct *vma)
786 struct via_camera *cam = video_drvdata(filp);
788 return videobuf_mmap_mapper(&cam->vb_queue, vma);
793 static const struct v4l2_file_operations viacam_fops = {
794 .owner = THIS_MODULE,
796 .release = viacam_release,
800 .unlocked_ioctl = video_ioctl2,
803 /*----------------------------------------------------------------------------*/
805 * The long list of v4l2 ioctl ops
808 static int viacam_g_chip_ident(struct file *file, void *priv,
809 struct v4l2_dbg_chip_ident *ident)
811 struct via_camera *cam = priv;
813 ident->ident = V4L2_IDENT_NONE;
815 if (v4l2_chip_match_host(&ident->match)) {
816 ident->ident = V4L2_IDENT_VIA_VX855;
819 return sensor_call(cam, core, g_chip_ident, ident);
825 static int viacam_enum_input(struct file *filp, void *priv,
826 struct v4l2_input *input)
828 if (input->index != 0)
831 input->type = V4L2_INPUT_TYPE_CAMERA;
832 input->std = V4L2_STD_ALL; /* Not sure what should go here */
833 strcpy(input->name, "Camera");
837 static int viacam_g_input(struct file *filp, void *priv, unsigned int *i)
843 static int viacam_s_input(struct file *filp, void *priv, unsigned int i)
850 static int viacam_s_std(struct file *filp, void *priv, v4l2_std_id *std)
856 * Video format stuff. Here is our default format until
857 * user space messes with things.
859 static const struct v4l2_pix_format viacam_def_pix_format = {
861 .height = VGA_HEIGHT,
862 .pixelformat = V4L2_PIX_FMT_YUYV,
863 .field = V4L2_FIELD_NONE,
864 .bytesperline = VGA_WIDTH * 2,
865 .sizeimage = VGA_WIDTH * VGA_HEIGHT * 2,
868 static const enum v4l2_mbus_pixelcode via_def_mbus_code = V4L2_MBUS_FMT_YUYV8_2X8;
870 static int viacam_enum_fmt_vid_cap(struct file *filp, void *priv,
871 struct v4l2_fmtdesc *fmt)
873 if (fmt->index >= N_VIA_FMTS)
875 strlcpy(fmt->description, via_formats[fmt->index].desc,
876 sizeof(fmt->description));
877 fmt->pixelformat = via_formats[fmt->index].pixelformat;
882 * Figure out proper image dimensions, but always force the
885 static void viacam_fmt_pre(struct v4l2_pix_format *userfmt,
886 struct v4l2_pix_format *sensorfmt)
888 *sensorfmt = *userfmt;
889 if (userfmt->width < QCIF_WIDTH || userfmt->height < QCIF_HEIGHT) {
890 userfmt->width = QCIF_WIDTH;
891 userfmt->height = QCIF_HEIGHT;
893 if (userfmt->width > VGA_WIDTH || userfmt->height > VGA_HEIGHT) {
894 userfmt->width = VGA_WIDTH;
895 userfmt->height = VGA_HEIGHT;
897 sensorfmt->width = VGA_WIDTH;
898 sensorfmt->height = VGA_HEIGHT;
901 static void viacam_fmt_post(struct v4l2_pix_format *userfmt,
902 struct v4l2_pix_format *sensorfmt)
904 struct via_format *f = via_find_format(userfmt->pixelformat);
906 sensorfmt->bytesperline = sensorfmt->width * f->bpp;
907 sensorfmt->sizeimage = sensorfmt->height * sensorfmt->bytesperline;
908 userfmt->pixelformat = sensorfmt->pixelformat;
909 userfmt->field = sensorfmt->field;
910 userfmt->bytesperline = 2 * userfmt->width;
911 userfmt->sizeimage = userfmt->bytesperline * userfmt->height;
916 * The real work of figuring out a workable format.
918 static int viacam_do_try_fmt(struct via_camera *cam,
919 struct v4l2_pix_format *upix, struct v4l2_pix_format *spix)
922 struct v4l2_mbus_framefmt mbus_fmt;
923 struct via_format *f = via_find_format(upix->pixelformat);
925 upix->pixelformat = f->pixelformat;
926 viacam_fmt_pre(upix, spix);
927 v4l2_fill_mbus_format(&mbus_fmt, spix, f->mbus_code);
928 ret = sensor_call(cam, video, try_mbus_fmt, &mbus_fmt);
929 v4l2_fill_pix_format(spix, &mbus_fmt);
930 viacam_fmt_post(upix, spix);
936 static int viacam_try_fmt_vid_cap(struct file *filp, void *priv,
937 struct v4l2_format *fmt)
939 struct via_camera *cam = priv;
940 struct v4l2_format sfmt;
943 mutex_lock(&cam->lock);
944 ret = viacam_do_try_fmt(cam, &fmt->fmt.pix, &sfmt.fmt.pix);
945 mutex_unlock(&cam->lock);
950 static int viacam_g_fmt_vid_cap(struct file *filp, void *priv,
951 struct v4l2_format *fmt)
953 struct via_camera *cam = priv;
955 mutex_lock(&cam->lock);
956 fmt->fmt.pix = cam->user_format;
957 mutex_unlock(&cam->lock);
961 static int viacam_s_fmt_vid_cap(struct file *filp, void *priv,
962 struct v4l2_format *fmt)
964 struct via_camera *cam = priv;
966 struct v4l2_format sfmt;
967 struct via_format *f = via_find_format(fmt->fmt.pix.pixelformat);
970 * Camera must be idle or we can't mess with the
973 mutex_lock(&cam->lock);
974 if (cam->opstate != S_IDLE) {
979 * Let the sensor code look over and tweak the
980 * requested formatting.
982 ret = viacam_do_try_fmt(cam, &fmt->fmt.pix, &sfmt.fmt.pix);
986 * OK, let's commit to the new format.
988 cam->user_format = fmt->fmt.pix;
989 cam->sensor_format = sfmt.fmt.pix;
990 cam->mbus_code = f->mbus_code;
991 ret = viacam_configure_sensor(cam);
993 ret = viacam_config_controller(cam);
995 mutex_unlock(&cam->lock);
999 static int viacam_querycap(struct file *filp, void *priv,
1000 struct v4l2_capability *cap)
1002 strcpy(cap->driver, "via-camera");
1003 strcpy(cap->card, "via-camera");
1005 cap->capabilities = V4L2_CAP_VIDEO_CAPTURE |
1006 V4L2_CAP_READWRITE | V4L2_CAP_STREAMING;
1011 * Streaming operations - pure videobuf stuff.
1013 static int viacam_reqbufs(struct file *filp, void *priv,
1014 struct v4l2_requestbuffers *rb)
1016 struct via_camera *cam = priv;
1018 return videobuf_reqbufs(&cam->vb_queue, rb);
1021 static int viacam_querybuf(struct file *filp, void *priv,
1022 struct v4l2_buffer *buf)
1024 struct via_camera *cam = priv;
1026 return videobuf_querybuf(&cam->vb_queue, buf);
1029 static int viacam_qbuf(struct file *filp, void *priv, struct v4l2_buffer *buf)
1031 struct via_camera *cam = priv;
1033 return videobuf_qbuf(&cam->vb_queue, buf);
1036 static int viacam_dqbuf(struct file *filp, void *priv, struct v4l2_buffer *buf)
1038 struct via_camera *cam = priv;
1040 return videobuf_dqbuf(&cam->vb_queue, buf, filp->f_flags & O_NONBLOCK);
1043 static int viacam_streamon(struct file *filp, void *priv, enum v4l2_buf_type t)
1045 struct via_camera *cam = priv;
1048 if (t != V4L2_BUF_TYPE_VIDEO_CAPTURE)
1051 mutex_lock(&cam->lock);
1052 if (cam->opstate != S_IDLE) {
1057 * Enforce the V4l2 "only one owner gets to read data" rule.
1059 if (cam->owner && cam->owner != filp) {
1065 * Configure things if need be.
1067 if (test_bit(CF_CONFIG_NEEDED, &cam->flags)) {
1068 ret = viacam_configure_sensor(cam);
1071 ret = viacam_config_controller(cam);
1076 * If the CPU goes into C3, the DMA transfer gets corrupted and
1077 * users start filing unsightly bug reports. Put in a "latency"
1078 * requirement which will keep the CPU out of the deeper sleep
1081 pm_qos_add_request(&cam->qos_request, PM_QOS_CPU_DMA_LATENCY, 50);
1085 INIT_LIST_HEAD(&cam->buffer_queue);
1086 ret = videobuf_streamon(&cam->vb_queue);
1088 viacam_start_engine(cam);
1090 mutex_unlock(&cam->lock);
1094 static int viacam_streamoff(struct file *filp, void *priv, enum v4l2_buf_type t)
1096 struct via_camera *cam = priv;
1099 if (t != V4L2_BUF_TYPE_VIDEO_CAPTURE)
1101 mutex_lock(&cam->lock);
1102 if (cam->opstate != S_RUNNING) {
1106 pm_qos_remove_request(&cam->qos_request);
1107 viacam_stop_engine(cam);
1109 * Videobuf will recycle all of the outstanding buffers, but
1110 * we should be sure we don't retain any references to
1113 ret = videobuf_streamoff(&cam->vb_queue);
1114 INIT_LIST_HEAD(&cam->buffer_queue);
1116 mutex_unlock(&cam->lock);
1122 static int viacam_g_parm(struct file *filp, void *priv,
1123 struct v4l2_streamparm *parm)
1125 struct via_camera *cam = priv;
1128 mutex_lock(&cam->lock);
1129 ret = sensor_call(cam, video, g_parm, parm);
1130 mutex_unlock(&cam->lock);
1131 parm->parm.capture.readbuffers = cam->n_cap_bufs;
1135 static int viacam_s_parm(struct file *filp, void *priv,
1136 struct v4l2_streamparm *parm)
1138 struct via_camera *cam = priv;
1141 mutex_lock(&cam->lock);
1142 ret = sensor_call(cam, video, s_parm, parm);
1143 mutex_unlock(&cam->lock);
1144 parm->parm.capture.readbuffers = cam->n_cap_bufs;
1148 static int viacam_enum_framesizes(struct file *filp, void *priv,
1149 struct v4l2_frmsizeenum *sizes)
1151 if (sizes->index != 0)
1153 sizes->type = V4L2_FRMSIZE_TYPE_CONTINUOUS;
1154 sizes->stepwise.min_width = QCIF_WIDTH;
1155 sizes->stepwise.min_height = QCIF_HEIGHT;
1156 sizes->stepwise.max_width = VGA_WIDTH;
1157 sizes->stepwise.max_height = VGA_HEIGHT;
1158 sizes->stepwise.step_width = sizes->stepwise.step_height = 1;
1162 static int viacam_enum_frameintervals(struct file *filp, void *priv,
1163 struct v4l2_frmivalenum *interval)
1165 struct via_camera *cam = priv;
1168 mutex_lock(&cam->lock);
1169 ret = sensor_call(cam, video, enum_frameintervals, interval);
1170 mutex_unlock(&cam->lock);
1176 static const struct v4l2_ioctl_ops viacam_ioctl_ops = {
1177 .vidioc_g_chip_ident = viacam_g_chip_ident,
1178 .vidioc_enum_input = viacam_enum_input,
1179 .vidioc_g_input = viacam_g_input,
1180 .vidioc_s_input = viacam_s_input,
1181 .vidioc_s_std = viacam_s_std,
1182 .vidioc_enum_fmt_vid_cap = viacam_enum_fmt_vid_cap,
1183 .vidioc_try_fmt_vid_cap = viacam_try_fmt_vid_cap,
1184 .vidioc_g_fmt_vid_cap = viacam_g_fmt_vid_cap,
1185 .vidioc_s_fmt_vid_cap = viacam_s_fmt_vid_cap,
1186 .vidioc_querycap = viacam_querycap,
1187 .vidioc_reqbufs = viacam_reqbufs,
1188 .vidioc_querybuf = viacam_querybuf,
1189 .vidioc_qbuf = viacam_qbuf,
1190 .vidioc_dqbuf = viacam_dqbuf,
1191 .vidioc_streamon = viacam_streamon,
1192 .vidioc_streamoff = viacam_streamoff,
1193 .vidioc_g_parm = viacam_g_parm,
1194 .vidioc_s_parm = viacam_s_parm,
1195 .vidioc_enum_framesizes = viacam_enum_framesizes,
1196 .vidioc_enum_frameintervals = viacam_enum_frameintervals,
1199 /*----------------------------------------------------------------------------*/
1206 static int viacam_suspend(void *priv)
1208 struct via_camera *cam = priv;
1209 enum viacam_opstate state = cam->opstate;
1211 if (cam->opstate != S_IDLE) {
1212 viacam_stop_engine(cam);
1213 cam->opstate = state; /* So resume restarts */
1219 static int viacam_resume(void *priv)
1221 struct via_camera *cam = priv;
1225 * Get back to a reasonable operating state.
1227 via_write_reg_mask(VIASR, 0x78, 0, 0x80);
1228 via_write_reg_mask(VIASR, 0x1e, 0xc0, 0xc0);
1229 viacam_int_disable(cam);
1230 set_bit(CF_CONFIG_NEEDED, &cam->flags);
1232 * Make sure the sensor's power state is correct
1235 via_sensor_power_up(cam);
1237 via_sensor_power_down(cam);
1239 * If it was operating, try to restart it.
1241 if (cam->opstate != S_IDLE) {
1242 mutex_lock(&cam->lock);
1243 ret = viacam_configure_sensor(cam);
1245 ret = viacam_config_controller(cam);
1246 mutex_unlock(&cam->lock);
1248 viacam_start_engine(cam);
1254 static struct viafb_pm_hooks viacam_pm_hooks = {
1255 .suspend = viacam_suspend,
1256 .resume = viacam_resume
1259 #endif /* CONFIG_PM */
1265 static struct video_device viacam_v4l_template = {
1266 .name = "via-camera",
1268 .tvnorms = V4L2_STD_NTSC_M,
1269 .current_norm = V4L2_STD_NTSC_M,
1270 .fops = &viacam_fops,
1271 .ioctl_ops = &viacam_ioctl_ops,
1272 .release = video_device_release_empty, /* Check this */
1276 * The OLPC folks put the serial port on the same pin as
1277 * the camera. They also get grumpy if we break the
1278 * serial port and keep them from using it. So we have
1279 * to check the serial enable bit and not step on it.
1281 #define VIACAM_SERIAL_DEVFN 0x88
1282 #define VIACAM_SERIAL_CREG 0x46
1283 #define VIACAM_SERIAL_BIT 0x40
1285 static bool viacam_serial_is_enabled(void)
1287 struct pci_bus *pbus = pci_find_bus(0, 0);
1292 pci_bus_read_config_byte(pbus, VIACAM_SERIAL_DEVFN,
1293 VIACAM_SERIAL_CREG, &cbyte);
1294 if ((cbyte & VIACAM_SERIAL_BIT) == 0)
1295 return false; /* Not enabled */
1296 if (override_serial == 0) {
1297 printk(KERN_NOTICE "Via camera: serial port is enabled, " \
1298 "refusing to load.\n");
1299 printk(KERN_NOTICE "Specify override_serial=1 to force " \
1300 "module loading.\n");
1303 printk(KERN_NOTICE "Via camera: overriding serial port\n");
1304 pci_bus_write_config_byte(pbus, VIACAM_SERIAL_DEVFN,
1305 VIACAM_SERIAL_CREG, cbyte & ~VIACAM_SERIAL_BIT);
1309 static struct ov7670_config sensor_cfg = {
1310 /* The XO-1.5 (only known user) clocks the camera at 90MHz. */
1314 static int viacam_probe(struct platform_device *pdev)
1317 struct i2c_adapter *sensor_adapter;
1318 struct viafb_dev *viadev = pdev->dev.platform_data;
1319 struct i2c_board_info ov7670_info = {
1322 .platform_data = &sensor_cfg,
1326 * Note that there are actually two capture channels on
1327 * the device. We only deal with one for now. That
1328 * is encoded here; nothing else assumes it's dealing with
1329 * a unique capture device.
1331 struct via_camera *cam;
1334 * Ensure that frame buffer memory has been set aside for
1335 * this purpose. As an arbitrary limit, refuse to work
1336 * with less than two frames of VGA 16-bit data.
1338 * If we ever support the second port, we'll need to set
1339 * aside more memory.
1341 if (viadev->camera_fbmem_size < (VGA_HEIGHT*VGA_WIDTH*4)) {
1342 printk(KERN_ERR "viacam: insufficient FB memory reserved\n");
1345 if (viadev->engine_mmio == NULL) {
1346 printk(KERN_ERR "viacam: No I/O memory, so no pictures\n");
1350 if (machine_is_olpc() && viacam_serial_is_enabled())
1354 * Basic structure initialization.
1356 cam = kzalloc (sizeof(struct via_camera), GFP_KERNEL);
1360 cam->platdev = pdev;
1361 cam->viadev = viadev;
1364 cam->opstate = S_IDLE;
1365 cam->user_format = cam->sensor_format = viacam_def_pix_format;
1366 mutex_init(&cam->lock);
1367 INIT_LIST_HEAD(&cam->buffer_queue);
1368 cam->mmio = viadev->engine_mmio;
1369 cam->fbmem = viadev->fbmem;
1370 cam->fb_offset = viadev->camera_fbmem_offset;
1371 cam->flags = 1 << CF_CONFIG_NEEDED;
1372 cam->mbus_code = via_def_mbus_code;
1374 * Tell V4L that we exist.
1376 ret = v4l2_device_register(&pdev->dev, &cam->v4l2_dev);
1378 dev_err(&pdev->dev, "Unable to register v4l2 device\n");
1381 ret = v4l2_ctrl_handler_init(&cam->ctrl_handler, 10);
1383 goto out_unregister;
1384 cam->v4l2_dev.ctrl_handler = &cam->ctrl_handler;
1386 * Convince the system that we can do DMA.
1388 pdev->dev.dma_mask = &viadev->pdev->dma_mask;
1389 dma_set_mask(&pdev->dev, 0xffffffff);
1391 * Fire up the capture port. The write to 0x78 looks purely
1392 * OLPCish; any system will need to tweak 0x1e.
1394 via_write_reg_mask(VIASR, 0x78, 0, 0x80);
1395 via_write_reg_mask(VIASR, 0x1e, 0xc0, 0xc0);
1397 * Get the sensor powered up.
1399 ret = via_sensor_power_setup(cam);
1401 goto out_ctrl_hdl_free;
1402 via_sensor_power_up(cam);
1405 * See if we can't find it on the bus. The VIA_PORT_31 assumption
1406 * is OLPC-specific. 0x42 assumption is ov7670-specific.
1408 sensor_adapter = viafb_find_i2c_adapter(VIA_PORT_31);
1409 cam->sensor = v4l2_i2c_new_subdev_board(&cam->v4l2_dev, sensor_adapter,
1410 &ov7670_info, NULL);
1411 if (cam->sensor == NULL) {
1412 dev_err(&pdev->dev, "Unable to find the sensor!\n");
1414 goto out_power_down;
1419 viacam_int_disable(cam);
1420 ret = request_threaded_irq(viadev->pdev->irq, viacam_quick_irq,
1421 viacam_irq, IRQF_SHARED, "via-camera", cam);
1423 goto out_power_down;
1425 * Tell V4l2 that we exist.
1427 cam->vdev = viacam_v4l_template;
1428 cam->vdev.v4l2_dev = &cam->v4l2_dev;
1429 ret = video_register_device(&cam->vdev, VFL_TYPE_GRABBER, -1);
1432 video_set_drvdata(&cam->vdev, cam);
1436 * Hook into PM events
1438 viacam_pm_hooks.private = cam;
1439 viafb_pm_register(&viacam_pm_hooks);
1442 /* Power the sensor down until somebody opens the device */
1443 via_sensor_power_down(cam);
1447 free_irq(viadev->pdev->irq, cam);
1449 via_sensor_power_release(cam);
1451 v4l2_ctrl_handler_free(&cam->ctrl_handler);
1453 v4l2_device_unregister(&cam->v4l2_dev);
1459 static int viacam_remove(struct platform_device *pdev)
1461 struct via_camera *cam = via_cam_info;
1462 struct viafb_dev *viadev = pdev->dev.platform_data;
1464 video_unregister_device(&cam->vdev);
1465 v4l2_device_unregister(&cam->v4l2_dev);
1466 free_irq(viadev->pdev->irq, cam);
1467 via_sensor_power_release(cam);
1468 v4l2_ctrl_handler_free(&cam->ctrl_handler);
1470 via_cam_info = NULL;
1474 static struct platform_driver viacam_driver = {
1476 .name = "viafb-camera",
1478 .probe = viacam_probe,
1479 .remove = viacam_remove,
1482 module_platform_driver(viacam_driver);