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/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>
31 #include "via-camera.h"
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");
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 "
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.");
55 #define VGA_HEIGHT 480
56 #define QCIF_WIDTH 176
57 #define QCIF_HEIGHT 144
60 * The structure describing our camera.
62 enum viacam_opstate { S_IDLE = 0, S_RUNNING = 1 };
65 struct v4l2_device v4l2_dev;
66 struct video_device vdev;
67 struct v4l2_subdev *sensor;
68 struct platform_device *platdev;
69 struct viafb_dev *viadev;
71 enum viacam_opstate opstate;
73 struct pm_qos_request qos_request;
75 * GPIO info for power/reset management
82 void __iomem *mmio; /* Where the registers live */
83 void __iomem *fbmem; /* Frame buffer memory */
84 u32 fb_offset; /* Reserved memory offset (FB) */
86 * Capture buffers and related. The controller supports
87 * up to three, so that's what we have here. These buffers
88 * live in frame buffer memory, so we don't call them "DMA".
90 unsigned int cb_offsets[3]; /* offsets into fb mem */
91 u8 *cb_addrs[3]; /* Kernel-space addresses */
92 int n_cap_bufs; /* How many are we using? */
94 struct videobuf_queue vb_queue;
95 struct list_head buffer_queue; /* prot. by reg_lock */
102 * Video format information. sensor_format is kept in a form
103 * that we can use to pass to the sensor. We always run the
104 * sensor in VGA resolution, though, and let the controller
105 * downscale things if need be. So we keep the "real*
106 * dimensions separately.
108 struct v4l2_pix_format sensor_format;
109 struct v4l2_pix_format user_format;
110 enum v4l2_mbus_pixelcode mbus_code;
114 * Yes, this is a hack, but there's only going to be one of these
115 * on any system we know of.
117 static struct via_camera *via_cam_info;
120 * Flag values, manipulated with bitops
122 #define CF_DMA_ACTIVE 0 /* A frame is incoming */
123 #define CF_CONFIG_NEEDED 1 /* Must configure hardware */
127 * Nasty ugly v4l2 boilerplate.
129 #define sensor_call(cam, optype, func, args...) \
130 v4l2_subdev_call(cam->sensor, optype, func, ##args)
133 * Debugging and related.
135 #define cam_err(cam, fmt, arg...) \
136 dev_err(&(cam)->platdev->dev, fmt, ##arg);
137 #define cam_warn(cam, fmt, arg...) \
138 dev_warn(&(cam)->platdev->dev, fmt, ##arg);
139 #define cam_dbg(cam, fmt, arg...) \
140 dev_dbg(&(cam)->platdev->dev, fmt, ##arg);
143 * Format handling. This is ripped almost directly from Hans's changes
144 * to cafe_ccic.c. It's a little unfortunate; until this change, we
145 * didn't need to know anything about the format except its byte depth;
146 * now this information must be managed at this level too.
148 static struct via_format {
151 int bpp; /* Bytes per pixel */
152 enum v4l2_mbus_pixelcode mbus_code;
155 .desc = "YUYV 4:2:2",
156 .pixelformat = V4L2_PIX_FMT_YUYV,
157 .mbus_code = V4L2_MBUS_FMT_YUYV8_2X8,
160 /* RGB444 and Bayer should be doable, but have never been
161 tested with this driver. RGB565 seems to work at the default
162 resolution, but results in color corruption when being scaled by
163 viacam_set_scaled(), and is disabled as a result. */
165 #define N_VIA_FMTS ARRAY_SIZE(via_formats)
167 static struct via_format *via_find_format(u32 pixelformat)
171 for (i = 0; i < N_VIA_FMTS; i++)
172 if (via_formats[i].pixelformat == pixelformat)
173 return via_formats + i;
174 /* Not found? Then return the first format. */
179 /*--------------------------------------------------------------------------*/
181 * Sensor power/reset management. This piece is OLPC-specific for
182 * sure; other configurations will have things connected differently.
184 static int via_sensor_power_setup(struct via_camera *cam)
188 cam->power_gpio = viafb_gpio_lookup("VGPIO3");
189 cam->reset_gpio = viafb_gpio_lookup("VGPIO2");
190 if (cam->power_gpio < 0 || cam->reset_gpio < 0) {
191 dev_err(&cam->platdev->dev, "Unable to find GPIO lines\n");
194 ret = gpio_request(cam->power_gpio, "viafb-camera");
196 dev_err(&cam->platdev->dev, "Unable to request power GPIO\n");
199 ret = gpio_request(cam->reset_gpio, "viafb-camera");
201 dev_err(&cam->platdev->dev, "Unable to request reset GPIO\n");
202 gpio_free(cam->power_gpio);
205 gpio_direction_output(cam->power_gpio, 0);
206 gpio_direction_output(cam->reset_gpio, 0);
211 * Power up the sensor and perform the reset dance.
213 static void via_sensor_power_up(struct via_camera *cam)
215 gpio_set_value(cam->power_gpio, 1);
216 gpio_set_value(cam->reset_gpio, 0);
217 msleep(20); /* Probably excessive */
218 gpio_set_value(cam->reset_gpio, 1);
222 static void via_sensor_power_down(struct via_camera *cam)
224 gpio_set_value(cam->power_gpio, 0);
225 gpio_set_value(cam->reset_gpio, 0);
229 static void via_sensor_power_release(struct via_camera *cam)
231 via_sensor_power_down(cam);
232 gpio_free(cam->power_gpio);
233 gpio_free(cam->reset_gpio);
236 /* --------------------------------------------------------------------------*/
240 * Manage the ov7670 "flip" bit, which needs special help.
242 static int viacam_set_flip(struct via_camera *cam)
244 struct v4l2_control ctrl;
246 memset(&ctrl, 0, sizeof(ctrl));
247 ctrl.id = V4L2_CID_VFLIP;
248 ctrl.value = flip_image;
249 return sensor_call(cam, core, s_ctrl, &ctrl);
253 * Configure the sensor. It's up to the caller to ensure
254 * that the camera is in the correct operating state.
256 static int viacam_configure_sensor(struct via_camera *cam)
258 struct v4l2_mbus_framefmt mbus_fmt;
261 v4l2_fill_mbus_format(&mbus_fmt, &cam->sensor_format, cam->mbus_code);
262 ret = sensor_call(cam, core, init, 0);
264 ret = sensor_call(cam, video, s_mbus_fmt, &mbus_fmt);
266 * OV7670 does weird things if flip is set *before* format...
269 ret = viacam_set_flip(cam);
275 /* --------------------------------------------------------------------------*/
277 * Some simple register accessors; they assume that the lock is held.
279 * Should we want to support the second capture engine, we could
280 * hide the register difference by adding 0x1000 to registers in the
283 static inline void viacam_write_reg(struct via_camera *cam,
286 iowrite32(value, cam->mmio + reg);
289 static inline int viacam_read_reg(struct via_camera *cam, int reg)
291 return ioread32(cam->mmio + reg);
294 static inline void viacam_write_reg_mask(struct via_camera *cam,
295 int reg, int value, int mask)
297 int tmp = viacam_read_reg(cam, reg);
299 tmp = (tmp & ~mask) | (value & mask);
300 viacam_write_reg(cam, reg, tmp);
304 /* --------------------------------------------------------------------------*/
305 /* Interrupt management and handling */
307 static irqreturn_t viacam_quick_irq(int irq, void *data)
309 struct via_camera *cam = data;
310 irqreturn_t ret = IRQ_NONE;
314 * All we do here is to clear the interrupts and tell
315 * the handler thread to wake up.
317 spin_lock(&cam->viadev->reg_lock);
318 icv = viacam_read_reg(cam, VCR_INTCTRL);
319 if (icv & VCR_IC_EAV) {
320 icv |= VCR_IC_EAV|VCR_IC_EVBI|VCR_IC_FFULL;
321 viacam_write_reg(cam, VCR_INTCTRL, icv);
322 ret = IRQ_WAKE_THREAD;
324 spin_unlock(&cam->viadev->reg_lock);
329 * Find the next videobuf buffer which has somebody waiting on it.
331 static struct videobuf_buffer *viacam_next_buffer(struct via_camera *cam)
334 struct videobuf_buffer *buf = NULL;
336 spin_lock_irqsave(&cam->viadev->reg_lock, flags);
337 if (cam->opstate != S_RUNNING)
339 if (list_empty(&cam->buffer_queue))
341 buf = list_entry(cam->buffer_queue.next, struct videobuf_buffer, queue);
342 if (!waitqueue_active(&buf->done)) {/* Nobody waiting */
346 list_del(&buf->queue);
347 buf->state = VIDEOBUF_ACTIVE;
349 spin_unlock_irqrestore(&cam->viadev->reg_lock, flags);
354 * The threaded IRQ handler.
356 static irqreturn_t viacam_irq(int irq, void *data)
359 struct videobuf_buffer *vb;
360 struct via_camera *cam = data;
361 struct videobuf_dmabuf *vdma;
364 * If there is no place to put the data frame, don't bother
365 * with anything else.
367 vb = viacam_next_buffer(cam);
371 * Figure out which buffer we just completed.
373 bufn = (viacam_read_reg(cam, VCR_INTCTRL) & VCR_IC_ACTBUF) >> 3;
376 bufn = cam->n_cap_bufs - 1;
378 * Copy over the data and let any waiters know.
380 vdma = videobuf_to_dma(vb);
381 viafb_dma_copy_out_sg(cam->cb_offsets[bufn], vdma->sglist, vdma->sglen);
382 vb->state = VIDEOBUF_DONE;
383 vb->size = cam->user_format.sizeimage;
391 * These functions must mess around with the general interrupt
392 * control register, which is relevant to much more than just the
393 * camera. Nothing else uses interrupts, though, as of this writing.
394 * Should that situation change, we'll have to improve support at
395 * the via-core level.
397 static void viacam_int_enable(struct via_camera *cam)
399 viacam_write_reg(cam, VCR_INTCTRL,
400 VCR_IC_INTEN|VCR_IC_EAV|VCR_IC_EVBI|VCR_IC_FFULL);
401 viafb_irq_enable(VDE_I_C0AVEN);
404 static void viacam_int_disable(struct via_camera *cam)
406 viafb_irq_disable(VDE_I_C0AVEN);
407 viacam_write_reg(cam, VCR_INTCTRL, 0);
412 /* --------------------------------------------------------------------------*/
413 /* Controller operations */
416 * Set up our capture buffers in framebuffer memory.
418 static int viacam_ctlr_cbufs(struct via_camera *cam)
420 int nbuf = cam->viadev->camera_fbmem_size/cam->sensor_format.sizeimage;
425 * See how many buffers we can work with.
429 viacam_write_reg_mask(cam, VCR_CAPINTC, VCR_CI_3BUFS,
431 } else if (nbuf == 2) {
433 viacam_write_reg_mask(cam, VCR_CAPINTC, 0, VCR_CI_3BUFS);
435 cam_warn(cam, "Insufficient frame buffer memory\n");
441 offset = cam->fb_offset;
442 for (i = 0; i < cam->n_cap_bufs; i++) {
443 cam->cb_offsets[i] = offset;
444 cam->cb_addrs[i] = cam->fbmem + offset;
445 viacam_write_reg(cam, VCR_VBUF1 + i*4, offset & VCR_VBUF_MASK);
446 offset += cam->sensor_format.sizeimage;
452 * Set the scaling register for downscaling the image.
454 * This register works like this... Vertical scaling is enabled
455 * by bit 26; if that bit is set, downscaling is controlled by the
456 * value in bits 16:25. Those bits are divided by 1024 to get
457 * the scaling factor; setting just bit 25 thus cuts the height
460 * Horizontal scaling works about the same, but it's enabled by
461 * bit 11, with bits 0:10 giving the numerator of a fraction
462 * (over 2048) for the scaling value.
464 * This function is naive in that, if the user departs from
465 * the 3x4 VGA scaling factor, the image will distort. We
466 * could work around that if it really seemed important.
468 static void viacam_set_scale(struct via_camera *cam)
470 unsigned int avscale;
473 if (cam->user_format.width == VGA_WIDTH)
476 sf = (cam->user_format.width*2048)/VGA_WIDTH;
477 avscale = VCR_AVS_HEN | sf;
479 if (cam->user_format.height < VGA_HEIGHT) {
480 sf = (1024*cam->user_format.height)/VGA_HEIGHT;
481 avscale |= VCR_AVS_VEN | (sf << 16);
483 viacam_write_reg(cam, VCR_AVSCALE, avscale);
488 * Configure image-related information into the capture engine.
490 static void viacam_ctlr_image(struct via_camera *cam)
495 * Disable clock before messing with stuff - from the via
498 viacam_write_reg(cam, VCR_CAPINTC, ~(VCR_CI_ENABLE|VCR_CI_CLKEN));
500 * Set up the controller for VGA resolution, modulo magic
501 * offsets from the via sample driver.
503 viacam_write_reg(cam, VCR_HORRANGE, 0x06200120);
504 viacam_write_reg(cam, VCR_VERTRANGE, 0x01de0000);
505 viacam_set_scale(cam);
509 viacam_write_reg(cam, VCR_MAXDATA,
510 (cam->sensor_format.height << 16) |
511 (cam->sensor_format.bytesperline >> 3));
512 viacam_write_reg(cam, VCR_MAXVBI, 0);
513 viacam_write_reg(cam, VCR_VSTRIDE,
514 cam->user_format.bytesperline & VCR_VS_STRIDE);
516 * Set up the capture interface control register,
517 * everything but the "go" bit.
519 * The FIFO threshold is a bit of a magic number; 8 is what
520 * VIA's sample code uses.
522 cicreg = VCR_CI_CLKEN |
523 0x08000000 | /* FIFO threshold */
524 VCR_CI_FLDINV | /* OLPC-specific? */
525 VCR_CI_VREFINV | /* OLPC-specific? */
526 VCR_CI_DIBOTH | /* Capture both fields */
528 if (cam->n_cap_bufs == 3)
529 cicreg |= VCR_CI_3BUFS;
531 * YUV formats need different byte swapping than RGB.
533 if (cam->user_format.pixelformat == V4L2_PIX_FMT_YUYV)
534 cicreg |= VCR_CI_YUYV;
536 cicreg |= VCR_CI_UYVY;
537 viacam_write_reg(cam, VCR_CAPINTC, cicreg);
541 static int viacam_config_controller(struct via_camera *cam)
546 spin_lock_irqsave(&cam->viadev->reg_lock, flags);
547 ret = viacam_ctlr_cbufs(cam);
549 viacam_ctlr_image(cam);
550 spin_unlock_irqrestore(&cam->viadev->reg_lock, flags);
551 clear_bit(CF_CONFIG_NEEDED, &cam->flags);
556 * Make it start grabbing data.
558 static void viacam_start_engine(struct via_camera *cam)
560 spin_lock_irq(&cam->viadev->reg_lock);
562 viacam_write_reg_mask(cam, VCR_CAPINTC, VCR_CI_ENABLE, VCR_CI_ENABLE);
563 viacam_int_enable(cam);
564 (void) viacam_read_reg(cam, VCR_CAPINTC); /* Force post */
565 cam->opstate = S_RUNNING;
566 spin_unlock_irq(&cam->viadev->reg_lock);
570 static void viacam_stop_engine(struct via_camera *cam)
572 spin_lock_irq(&cam->viadev->reg_lock);
573 viacam_int_disable(cam);
574 viacam_write_reg_mask(cam, VCR_CAPINTC, 0, VCR_CI_ENABLE);
575 (void) viacam_read_reg(cam, VCR_CAPINTC); /* Force post */
576 cam->opstate = S_IDLE;
577 spin_unlock_irq(&cam->viadev->reg_lock);
581 /* --------------------------------------------------------------------------*/
582 /* Videobuf callback ops */
585 * buffer_setup. The purpose of this one would appear to be to tell
586 * videobuf how big a single image is. It's also evidently up to us
587 * to put some sort of limit on the maximum number of buffers allowed.
589 static int viacam_vb_buf_setup(struct videobuf_queue *q,
590 unsigned int *count, unsigned int *size)
592 struct via_camera *cam = q->priv_data;
594 *size = cam->user_format.sizeimage;
595 if (*count == 0 || *count > 6) /* Arbitrary number */
603 static int viacam_vb_buf_prepare(struct videobuf_queue *q,
604 struct videobuf_buffer *vb, enum v4l2_field field)
606 struct via_camera *cam = q->priv_data;
608 vb->size = cam->user_format.sizeimage;
609 vb->width = cam->user_format.width; /* bytesperline???? */
610 vb->height = cam->user_format.height;
612 if (vb->state == VIDEOBUF_NEEDS_INIT) {
613 int ret = videobuf_iolock(q, vb, NULL);
617 vb->state = VIDEOBUF_PREPARED;
622 * We've got a buffer to put data into.
624 * FIXME: check for a running engine and valid buffers?
626 static void viacam_vb_buf_queue(struct videobuf_queue *q,
627 struct videobuf_buffer *vb)
629 struct via_camera *cam = q->priv_data;
632 * Note that videobuf holds the lock when it calls
633 * us, so we need not (indeed, cannot) take it here.
635 vb->state = VIDEOBUF_QUEUED;
636 list_add_tail(&vb->queue, &cam->buffer_queue);
642 static void viacam_vb_buf_release(struct videobuf_queue *q,
643 struct videobuf_buffer *vb)
645 struct via_camera *cam = q->priv_data;
647 videobuf_dma_unmap(&cam->platdev->dev, videobuf_to_dma(vb));
648 videobuf_dma_free(videobuf_to_dma(vb));
649 vb->state = VIDEOBUF_NEEDS_INIT;
652 static const struct videobuf_queue_ops viacam_vb_ops = {
653 .buf_setup = viacam_vb_buf_setup,
654 .buf_prepare = viacam_vb_buf_prepare,
655 .buf_queue = viacam_vb_buf_queue,
656 .buf_release = viacam_vb_buf_release,
659 /* --------------------------------------------------------------------------*/
660 /* File operations */
662 static int viacam_open(struct file *filp)
664 struct via_camera *cam = video_drvdata(filp);
666 filp->private_data = cam;
668 * Note the new user. If this is the first one, we'll also
669 * need to power up the sensor.
671 mutex_lock(&cam->lock);
672 if (cam->users == 0) {
673 int ret = viafb_request_dma();
676 mutex_unlock(&cam->lock);
679 via_sensor_power_up(cam);
680 set_bit(CF_CONFIG_NEEDED, &cam->flags);
682 * Hook into videobuf. Evidently this cannot fail.
684 videobuf_queue_sg_init(&cam->vb_queue, &viacam_vb_ops,
685 &cam->platdev->dev, &cam->viadev->reg_lock,
686 V4L2_BUF_TYPE_VIDEO_CAPTURE, V4L2_FIELD_NONE,
687 sizeof(struct videobuf_buffer), cam, NULL);
690 mutex_unlock(&cam->lock);
694 static int viacam_release(struct file *filp)
696 struct via_camera *cam = video_drvdata(filp);
698 mutex_lock(&cam->lock);
701 * If the "owner" is closing, shut down any ongoing
704 if (filp == cam->owner) {
705 videobuf_stop(&cam->vb_queue);
707 * We don't hold the spinlock here, but, if release()
708 * is being called by the owner, nobody else will
709 * be changing the state. And an extra stop would
712 if (cam->opstate != S_IDLE)
713 viacam_stop_engine(cam);
717 * Last one out needs to turn out the lights.
719 if (cam->users == 0) {
720 videobuf_mmap_free(&cam->vb_queue);
721 via_sensor_power_down(cam);
724 mutex_unlock(&cam->lock);
729 * Read a frame from the device.
731 static ssize_t viacam_read(struct file *filp, char __user *buffer,
732 size_t len, loff_t *pos)
734 struct via_camera *cam = video_drvdata(filp);
737 mutex_lock(&cam->lock);
739 * Enforce the V4l2 "only one owner gets to read data" rule.
741 if (cam->owner && cam->owner != filp) {
747 * Do we need to configure the hardware?
749 if (test_bit(CF_CONFIG_NEEDED, &cam->flags)) {
750 ret = viacam_configure_sensor(cam);
752 ret = viacam_config_controller(cam);
757 * Fire up the capture engine, then have videobuf do
758 * the heavy lifting. Someday it would be good to avoid
759 * stopping and restarting the engine each time.
761 INIT_LIST_HEAD(&cam->buffer_queue);
762 viacam_start_engine(cam);
763 ret = videobuf_read_stream(&cam->vb_queue, buffer, len, pos, 0,
764 filp->f_flags & O_NONBLOCK);
765 viacam_stop_engine(cam);
766 /* videobuf_stop() ?? */
769 mutex_unlock(&cam->lock);
774 static unsigned int viacam_poll(struct file *filp, struct poll_table_struct *pt)
776 struct via_camera *cam = video_drvdata(filp);
778 return videobuf_poll_stream(filp, &cam->vb_queue, pt);
782 static int viacam_mmap(struct file *filp, struct vm_area_struct *vma)
784 struct via_camera *cam = video_drvdata(filp);
786 return videobuf_mmap_mapper(&cam->vb_queue, vma);
791 static const struct v4l2_file_operations viacam_fops = {
792 .owner = THIS_MODULE,
794 .release = viacam_release,
798 .unlocked_ioctl = video_ioctl2,
801 /*----------------------------------------------------------------------------*/
803 * The long list of v4l2 ioctl ops
806 static int viacam_g_chip_ident(struct file *file, void *priv,
807 struct v4l2_dbg_chip_ident *ident)
809 struct via_camera *cam = priv;
811 ident->ident = V4L2_IDENT_NONE;
813 if (v4l2_chip_match_host(&ident->match)) {
814 ident->ident = V4L2_IDENT_VIA_VX855;
817 return sensor_call(cam, core, g_chip_ident, ident);
821 * Control ops are passed through to the sensor.
823 static int viacam_queryctrl(struct file *filp, void *priv,
824 struct v4l2_queryctrl *qc)
826 struct via_camera *cam = priv;
829 mutex_lock(&cam->lock);
830 ret = sensor_call(cam, core, queryctrl, qc);
831 mutex_unlock(&cam->lock);
836 static int viacam_g_ctrl(struct file *filp, void *priv,
837 struct v4l2_control *ctrl)
839 struct via_camera *cam = priv;
842 mutex_lock(&cam->lock);
843 ret = sensor_call(cam, core, g_ctrl, ctrl);
844 mutex_unlock(&cam->lock);
849 static int viacam_s_ctrl(struct file *filp, void *priv,
850 struct v4l2_control *ctrl)
852 struct via_camera *cam = priv;
855 mutex_lock(&cam->lock);
856 ret = sensor_call(cam, core, s_ctrl, ctrl);
857 mutex_unlock(&cam->lock);
864 static int viacam_enum_input(struct file *filp, void *priv,
865 struct v4l2_input *input)
867 if (input->index != 0)
870 input->type = V4L2_INPUT_TYPE_CAMERA;
871 input->std = V4L2_STD_ALL; /* Not sure what should go here */
872 strcpy(input->name, "Camera");
876 static int viacam_g_input(struct file *filp, void *priv, unsigned int *i)
882 static int viacam_s_input(struct file *filp, void *priv, unsigned int i)
889 static int viacam_s_std(struct file *filp, void *priv, v4l2_std_id *std)
895 * Video format stuff. Here is our default format until
896 * user space messes with things.
898 static const struct v4l2_pix_format viacam_def_pix_format = {
900 .height = VGA_HEIGHT,
901 .pixelformat = V4L2_PIX_FMT_YUYV,
902 .field = V4L2_FIELD_NONE,
903 .bytesperline = VGA_WIDTH * 2,
904 .sizeimage = VGA_WIDTH * VGA_HEIGHT * 2,
907 static const enum v4l2_mbus_pixelcode via_def_mbus_code = V4L2_MBUS_FMT_YUYV8_2X8;
909 static int viacam_enum_fmt_vid_cap(struct file *filp, void *priv,
910 struct v4l2_fmtdesc *fmt)
912 if (fmt->index >= N_VIA_FMTS)
914 strlcpy(fmt->description, via_formats[fmt->index].desc,
915 sizeof(fmt->description));
916 fmt->pixelformat = via_formats[fmt->index].pixelformat;
921 * Figure out proper image dimensions, but always force the
924 static void viacam_fmt_pre(struct v4l2_pix_format *userfmt,
925 struct v4l2_pix_format *sensorfmt)
927 *sensorfmt = *userfmt;
928 if (userfmt->width < QCIF_WIDTH || userfmt->height < QCIF_HEIGHT) {
929 userfmt->width = QCIF_WIDTH;
930 userfmt->height = QCIF_HEIGHT;
932 if (userfmt->width > VGA_WIDTH || userfmt->height > VGA_HEIGHT) {
933 userfmt->width = VGA_WIDTH;
934 userfmt->height = VGA_HEIGHT;
936 sensorfmt->width = VGA_WIDTH;
937 sensorfmt->height = VGA_HEIGHT;
940 static void viacam_fmt_post(struct v4l2_pix_format *userfmt,
941 struct v4l2_pix_format *sensorfmt)
943 struct via_format *f = via_find_format(userfmt->pixelformat);
945 sensorfmt->bytesperline = sensorfmt->width * f->bpp;
946 sensorfmt->sizeimage = sensorfmt->height * sensorfmt->bytesperline;
947 userfmt->pixelformat = sensorfmt->pixelformat;
948 userfmt->field = sensorfmt->field;
949 userfmt->bytesperline = 2 * userfmt->width;
950 userfmt->sizeimage = userfmt->bytesperline * userfmt->height;
955 * The real work of figuring out a workable format.
957 static int viacam_do_try_fmt(struct via_camera *cam,
958 struct v4l2_pix_format *upix, struct v4l2_pix_format *spix)
961 struct v4l2_mbus_framefmt mbus_fmt;
962 struct via_format *f = via_find_format(upix->pixelformat);
964 upix->pixelformat = f->pixelformat;
965 viacam_fmt_pre(upix, spix);
966 v4l2_fill_mbus_format(&mbus_fmt, spix, f->mbus_code);
967 ret = sensor_call(cam, video, try_mbus_fmt, &mbus_fmt);
968 v4l2_fill_pix_format(spix, &mbus_fmt);
969 viacam_fmt_post(upix, spix);
975 static int viacam_try_fmt_vid_cap(struct file *filp, void *priv,
976 struct v4l2_format *fmt)
978 struct via_camera *cam = priv;
979 struct v4l2_format sfmt;
982 mutex_lock(&cam->lock);
983 ret = viacam_do_try_fmt(cam, &fmt->fmt.pix, &sfmt.fmt.pix);
984 mutex_unlock(&cam->lock);
989 static int viacam_g_fmt_vid_cap(struct file *filp, void *priv,
990 struct v4l2_format *fmt)
992 struct via_camera *cam = priv;
994 mutex_lock(&cam->lock);
995 fmt->fmt.pix = cam->user_format;
996 mutex_unlock(&cam->lock);
1000 static int viacam_s_fmt_vid_cap(struct file *filp, void *priv,
1001 struct v4l2_format *fmt)
1003 struct via_camera *cam = priv;
1005 struct v4l2_format sfmt;
1006 struct via_format *f = via_find_format(fmt->fmt.pix.pixelformat);
1009 * Camera must be idle or we can't mess with the
1012 mutex_lock(&cam->lock);
1013 if (cam->opstate != S_IDLE) {
1018 * Let the sensor code look over and tweak the
1019 * requested formatting.
1021 ret = viacam_do_try_fmt(cam, &fmt->fmt.pix, &sfmt.fmt.pix);
1025 * OK, let's commit to the new format.
1027 cam->user_format = fmt->fmt.pix;
1028 cam->sensor_format = sfmt.fmt.pix;
1029 cam->mbus_code = f->mbus_code;
1030 ret = viacam_configure_sensor(cam);
1032 ret = viacam_config_controller(cam);
1034 mutex_unlock(&cam->lock);
1038 static int viacam_querycap(struct file *filp, void *priv,
1039 struct v4l2_capability *cap)
1041 strcpy(cap->driver, "via-camera");
1042 strcpy(cap->card, "via-camera");
1044 cap->capabilities = V4L2_CAP_VIDEO_CAPTURE |
1045 V4L2_CAP_READWRITE | V4L2_CAP_STREAMING;
1050 * Streaming operations - pure videobuf stuff.
1052 static int viacam_reqbufs(struct file *filp, void *priv,
1053 struct v4l2_requestbuffers *rb)
1055 struct via_camera *cam = priv;
1057 return videobuf_reqbufs(&cam->vb_queue, rb);
1060 static int viacam_querybuf(struct file *filp, void *priv,
1061 struct v4l2_buffer *buf)
1063 struct via_camera *cam = priv;
1065 return videobuf_querybuf(&cam->vb_queue, buf);
1068 static int viacam_qbuf(struct file *filp, void *priv, struct v4l2_buffer *buf)
1070 struct via_camera *cam = priv;
1072 return videobuf_qbuf(&cam->vb_queue, buf);
1075 static int viacam_dqbuf(struct file *filp, void *priv, struct v4l2_buffer *buf)
1077 struct via_camera *cam = priv;
1079 return videobuf_dqbuf(&cam->vb_queue, buf, filp->f_flags & O_NONBLOCK);
1082 static int viacam_streamon(struct file *filp, void *priv, enum v4l2_buf_type t)
1084 struct via_camera *cam = priv;
1087 if (t != V4L2_BUF_TYPE_VIDEO_CAPTURE)
1090 mutex_lock(&cam->lock);
1091 if (cam->opstate != S_IDLE) {
1096 * Enforce the V4l2 "only one owner gets to read data" rule.
1098 if (cam->owner && cam->owner != filp) {
1104 * Configure things if need be.
1106 if (test_bit(CF_CONFIG_NEEDED, &cam->flags)) {
1107 ret = viacam_configure_sensor(cam);
1110 ret = viacam_config_controller(cam);
1115 * If the CPU goes into C3, the DMA transfer gets corrupted and
1116 * users start filing unsightly bug reports. Put in a "latency"
1117 * requirement which will keep the CPU out of the deeper sleep
1120 pm_qos_add_request(&cam->qos_request, PM_QOS_CPU_DMA_LATENCY, 50);
1124 INIT_LIST_HEAD(&cam->buffer_queue);
1125 ret = videobuf_streamon(&cam->vb_queue);
1127 viacam_start_engine(cam);
1129 mutex_unlock(&cam->lock);
1133 static int viacam_streamoff(struct file *filp, void *priv, enum v4l2_buf_type t)
1135 struct via_camera *cam = priv;
1138 if (t != V4L2_BUF_TYPE_VIDEO_CAPTURE)
1140 mutex_lock(&cam->lock);
1141 if (cam->opstate != S_RUNNING) {
1145 pm_qos_remove_request(&cam->qos_request);
1146 viacam_stop_engine(cam);
1148 * Videobuf will recycle all of the outstanding buffers, but
1149 * we should be sure we don't retain any references to
1152 ret = videobuf_streamoff(&cam->vb_queue);
1153 INIT_LIST_HEAD(&cam->buffer_queue);
1155 mutex_unlock(&cam->lock);
1161 static int viacam_g_parm(struct file *filp, void *priv,
1162 struct v4l2_streamparm *parm)
1164 struct via_camera *cam = priv;
1167 mutex_lock(&cam->lock);
1168 ret = sensor_call(cam, video, g_parm, parm);
1169 mutex_unlock(&cam->lock);
1170 parm->parm.capture.readbuffers = cam->n_cap_bufs;
1174 static int viacam_s_parm(struct file *filp, void *priv,
1175 struct v4l2_streamparm *parm)
1177 struct via_camera *cam = priv;
1180 mutex_lock(&cam->lock);
1181 ret = sensor_call(cam, video, s_parm, parm);
1182 mutex_unlock(&cam->lock);
1183 parm->parm.capture.readbuffers = cam->n_cap_bufs;
1187 static int viacam_enum_framesizes(struct file *filp, void *priv,
1188 struct v4l2_frmsizeenum *sizes)
1190 if (sizes->index != 0)
1192 sizes->type = V4L2_FRMSIZE_TYPE_CONTINUOUS;
1193 sizes->stepwise.min_width = QCIF_WIDTH;
1194 sizes->stepwise.min_height = QCIF_HEIGHT;
1195 sizes->stepwise.max_width = VGA_WIDTH;
1196 sizes->stepwise.max_height = VGA_HEIGHT;
1197 sizes->stepwise.step_width = sizes->stepwise.step_height = 1;
1201 static int viacam_enum_frameintervals(struct file *filp, void *priv,
1202 struct v4l2_frmivalenum *interval)
1204 struct via_camera *cam = priv;
1207 mutex_lock(&cam->lock);
1208 ret = sensor_call(cam, video, enum_frameintervals, interval);
1209 mutex_unlock(&cam->lock);
1215 static const struct v4l2_ioctl_ops viacam_ioctl_ops = {
1216 .vidioc_g_chip_ident = viacam_g_chip_ident,
1217 .vidioc_queryctrl = viacam_queryctrl,
1218 .vidioc_g_ctrl = viacam_g_ctrl,
1219 .vidioc_s_ctrl = viacam_s_ctrl,
1220 .vidioc_enum_input = viacam_enum_input,
1221 .vidioc_g_input = viacam_g_input,
1222 .vidioc_s_input = viacam_s_input,
1223 .vidioc_s_std = viacam_s_std,
1224 .vidioc_enum_fmt_vid_cap = viacam_enum_fmt_vid_cap,
1225 .vidioc_try_fmt_vid_cap = viacam_try_fmt_vid_cap,
1226 .vidioc_g_fmt_vid_cap = viacam_g_fmt_vid_cap,
1227 .vidioc_s_fmt_vid_cap = viacam_s_fmt_vid_cap,
1228 .vidioc_querycap = viacam_querycap,
1229 .vidioc_reqbufs = viacam_reqbufs,
1230 .vidioc_querybuf = viacam_querybuf,
1231 .vidioc_qbuf = viacam_qbuf,
1232 .vidioc_dqbuf = viacam_dqbuf,
1233 .vidioc_streamon = viacam_streamon,
1234 .vidioc_streamoff = viacam_streamoff,
1235 .vidioc_g_parm = viacam_g_parm,
1236 .vidioc_s_parm = viacam_s_parm,
1237 .vidioc_enum_framesizes = viacam_enum_framesizes,
1238 .vidioc_enum_frameintervals = viacam_enum_frameintervals,
1241 /*----------------------------------------------------------------------------*/
1248 static int viacam_suspend(void *priv)
1250 struct via_camera *cam = priv;
1251 enum viacam_opstate state = cam->opstate;
1253 if (cam->opstate != S_IDLE) {
1254 viacam_stop_engine(cam);
1255 cam->opstate = state; /* So resume restarts */
1261 static int viacam_resume(void *priv)
1263 struct via_camera *cam = priv;
1267 * Get back to a reasonable operating state.
1269 via_write_reg_mask(VIASR, 0x78, 0, 0x80);
1270 via_write_reg_mask(VIASR, 0x1e, 0xc0, 0xc0);
1271 viacam_int_disable(cam);
1272 set_bit(CF_CONFIG_NEEDED, &cam->flags);
1274 * Make sure the sensor's power state is correct
1277 via_sensor_power_up(cam);
1279 via_sensor_power_down(cam);
1281 * If it was operating, try to restart it.
1283 if (cam->opstate != S_IDLE) {
1284 mutex_lock(&cam->lock);
1285 ret = viacam_configure_sensor(cam);
1287 ret = viacam_config_controller(cam);
1288 mutex_unlock(&cam->lock);
1290 viacam_start_engine(cam);
1296 static struct viafb_pm_hooks viacam_pm_hooks = {
1297 .suspend = viacam_suspend,
1298 .resume = viacam_resume
1301 #endif /* CONFIG_PM */
1307 static struct video_device viacam_v4l_template = {
1308 .name = "via-camera",
1310 .tvnorms = V4L2_STD_NTSC_M,
1311 .current_norm = V4L2_STD_NTSC_M,
1312 .fops = &viacam_fops,
1313 .ioctl_ops = &viacam_ioctl_ops,
1314 .release = video_device_release_empty, /* Check this */
1318 * The OLPC folks put the serial port on the same pin as
1319 * the camera. They also get grumpy if we break the
1320 * serial port and keep them from using it. So we have
1321 * to check the serial enable bit and not step on it.
1323 #define VIACAM_SERIAL_DEVFN 0x88
1324 #define VIACAM_SERIAL_CREG 0x46
1325 #define VIACAM_SERIAL_BIT 0x40
1327 static __devinit bool viacam_serial_is_enabled(void)
1329 struct pci_bus *pbus = pci_find_bus(0, 0);
1334 pci_bus_read_config_byte(pbus, VIACAM_SERIAL_DEVFN,
1335 VIACAM_SERIAL_CREG, &cbyte);
1336 if ((cbyte & VIACAM_SERIAL_BIT) == 0)
1337 return false; /* Not enabled */
1338 if (override_serial == 0) {
1339 printk(KERN_NOTICE "Via camera: serial port is enabled, " \
1340 "refusing to load.\n");
1341 printk(KERN_NOTICE "Specify override_serial=1 to force " \
1342 "module loading.\n");
1345 printk(KERN_NOTICE "Via camera: overriding serial port\n");
1346 pci_bus_write_config_byte(pbus, VIACAM_SERIAL_DEVFN,
1347 VIACAM_SERIAL_CREG, cbyte & ~VIACAM_SERIAL_BIT);
1351 static struct ov7670_config sensor_cfg = {
1352 /* The XO-1.5 (only known user) clocks the camera at 90MHz. */
1356 static __devinit int viacam_probe(struct platform_device *pdev)
1359 struct i2c_adapter *sensor_adapter;
1360 struct viafb_dev *viadev = pdev->dev.platform_data;
1361 struct i2c_board_info ov7670_info = {
1364 .platform_data = &sensor_cfg,
1368 * Note that there are actually two capture channels on
1369 * the device. We only deal with one for now. That
1370 * is encoded here; nothing else assumes it's dealing with
1371 * a unique capture device.
1373 struct via_camera *cam;
1376 * Ensure that frame buffer memory has been set aside for
1377 * this purpose. As an arbitrary limit, refuse to work
1378 * with less than two frames of VGA 16-bit data.
1380 * If we ever support the second port, we'll need to set
1381 * aside more memory.
1383 if (viadev->camera_fbmem_size < (VGA_HEIGHT*VGA_WIDTH*4)) {
1384 printk(KERN_ERR "viacam: insufficient FB memory reserved\n");
1387 if (viadev->engine_mmio == NULL) {
1388 printk(KERN_ERR "viacam: No I/O memory, so no pictures\n");
1392 if (machine_is_olpc() && viacam_serial_is_enabled())
1396 * Basic structure initialization.
1398 cam = kzalloc (sizeof(struct via_camera), GFP_KERNEL);
1402 cam->platdev = pdev;
1403 cam->viadev = viadev;
1406 cam->opstate = S_IDLE;
1407 cam->user_format = cam->sensor_format = viacam_def_pix_format;
1408 mutex_init(&cam->lock);
1409 INIT_LIST_HEAD(&cam->buffer_queue);
1410 cam->mmio = viadev->engine_mmio;
1411 cam->fbmem = viadev->fbmem;
1412 cam->fb_offset = viadev->camera_fbmem_offset;
1413 cam->flags = 1 << CF_CONFIG_NEEDED;
1414 cam->mbus_code = via_def_mbus_code;
1416 * Tell V4L that we exist.
1418 ret = v4l2_device_register(&pdev->dev, &cam->v4l2_dev);
1420 dev_err(&pdev->dev, "Unable to register v4l2 device\n");
1424 * Convince the system that we can do DMA.
1426 pdev->dev.dma_mask = &viadev->pdev->dma_mask;
1427 dma_set_mask(&pdev->dev, 0xffffffff);
1429 * Fire up the capture port. The write to 0x78 looks purely
1430 * OLPCish; any system will need to tweak 0x1e.
1432 via_write_reg_mask(VIASR, 0x78, 0, 0x80);
1433 via_write_reg_mask(VIASR, 0x1e, 0xc0, 0xc0);
1435 * Get the sensor powered up.
1437 ret = via_sensor_power_setup(cam);
1439 goto out_unregister;
1440 via_sensor_power_up(cam);
1443 * See if we can't find it on the bus. The VIA_PORT_31 assumption
1444 * is OLPC-specific. 0x42 assumption is ov7670-specific.
1446 sensor_adapter = viafb_find_i2c_adapter(VIA_PORT_31);
1447 cam->sensor = v4l2_i2c_new_subdev_board(&cam->v4l2_dev, sensor_adapter,
1448 &ov7670_info, NULL);
1449 if (cam->sensor == NULL) {
1450 dev_err(&pdev->dev, "Unable to find the sensor!\n");
1452 goto out_power_down;
1457 viacam_int_disable(cam);
1458 ret = request_threaded_irq(viadev->pdev->irq, viacam_quick_irq,
1459 viacam_irq, IRQF_SHARED, "via-camera", cam);
1461 goto out_power_down;
1463 * Tell V4l2 that we exist.
1465 cam->vdev = viacam_v4l_template;
1466 cam->vdev.v4l2_dev = &cam->v4l2_dev;
1467 ret = video_register_device(&cam->vdev, VFL_TYPE_GRABBER, -1);
1470 video_set_drvdata(&cam->vdev, cam);
1474 * Hook into PM events
1476 viacam_pm_hooks.private = cam;
1477 viafb_pm_register(&viacam_pm_hooks);
1480 /* Power the sensor down until somebody opens the device */
1481 via_sensor_power_down(cam);
1485 free_irq(viadev->pdev->irq, cam);
1487 via_sensor_power_release(cam);
1489 v4l2_device_unregister(&cam->v4l2_dev);
1493 static __devexit int viacam_remove(struct platform_device *pdev)
1495 struct via_camera *cam = via_cam_info;
1496 struct viafb_dev *viadev = pdev->dev.platform_data;
1498 video_unregister_device(&cam->vdev);
1499 v4l2_device_unregister(&cam->v4l2_dev);
1500 free_irq(viadev->pdev->irq, cam);
1501 via_sensor_power_release(cam);
1502 via_cam_info = NULL;
1506 static struct platform_driver viacam_driver = {
1508 .name = "viafb-camera",
1510 .probe = viacam_probe,
1511 .remove = viacam_remove,
1514 module_platform_driver(viacam_driver);