1 // SPDX-License-Identifier: GPL-2.0-only
3 * Driver for the VIA Chrome integrated camera controller.
5 * Copyright 2009,2010 Jonathan Corbet <corbet@lwn.net>
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/consumer.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/v4l2-event.h>
22 #include <media/v4l2-image-sizes.h>
23 #include <media/i2c/ov7670.h>
24 #include <media/videobuf2-dma-sg.h>
25 #include <linux/delay.h>
26 #include <linux/dma-mapping.h>
27 #include <linux/pm_qos.h>
28 #include <linux/via-core.h>
29 #include <linux/via_i2c.h>
34 #define machine_is_olpc(x) 0
37 #include "via-camera.h"
39 MODULE_ALIAS("platform:viafb-camera");
40 MODULE_AUTHOR("Jonathan Corbet <corbet@lwn.net>");
41 MODULE_DESCRIPTION("VIA framebuffer-based camera controller driver");
42 MODULE_LICENSE("GPL");
44 static bool flip_image;
45 module_param(flip_image, bool, 0444);
46 MODULE_PARM_DESC(flip_image,
47 "If set, the sensor will be instructed to flip the image vertically.");
49 static bool override_serial;
50 module_param(override_serial, bool, 0444);
51 MODULE_PARM_DESC(override_serial,
52 "The camera driver will normally refuse to load if the XO 1.5 serial port is enabled. Set this option to force-enable the camera.");
55 * The structure describing our camera.
57 enum viacam_opstate { S_IDLE = 0, S_RUNNING = 1 };
60 struct v4l2_device v4l2_dev;
61 struct v4l2_ctrl_handler ctrl_handler;
62 struct video_device vdev;
63 struct v4l2_subdev *sensor;
64 struct platform_device *platdev;
65 struct viafb_dev *viadev;
67 enum viacam_opstate opstate;
69 struct pm_qos_request qos_request;
71 * GPIO info for power/reset management
73 struct gpio_desc *power_gpio;
74 struct gpio_desc *reset_gpio;
78 void __iomem *mmio; /* Where the registers live */
79 void __iomem *fbmem; /* Frame buffer memory */
80 u32 fb_offset; /* Reserved memory offset (FB) */
82 * Capture buffers and related. The controller supports
83 * up to three, so that's what we have here. These buffers
84 * live in frame buffer memory, so we don't call them "DMA".
86 unsigned int cb_offsets[3]; /* offsets into fb mem */
87 u8 __iomem *cb_addrs[3]; /* Kernel-space addresses */
88 int n_cap_bufs; /* How many are we using? */
90 struct list_head buffer_queue;
93 * Video format information. sensor_format is kept in a form
94 * that we can use to pass to the sensor. We always run the
95 * sensor in VGA resolution, though, and let the controller
96 * downscale things if need be. So we keep the "real*
97 * dimensions separately.
99 struct v4l2_pix_format sensor_format;
100 struct v4l2_pix_format user_format;
104 /* buffer for one video frame */
106 /* common v4l buffer stuff -- must be first */
107 struct vb2_v4l2_buffer vbuf;
108 struct list_head queue;
112 * Yes, this is a hack, but there's only going to be one of these
113 * on any system we know of.
115 static struct via_camera *via_cam_info;
118 * Flag values, manipulated with bitops
120 #define CF_DMA_ACTIVE 0 /* A frame is incoming */
121 #define CF_CONFIG_NEEDED 1 /* Must configure hardware */
125 * Nasty ugly v4l2 boilerplate.
127 #define sensor_call(cam, optype, func, args...) \
128 v4l2_subdev_call(cam->sensor, optype, func, ##args)
131 * Debugging and related.
133 #define cam_err(cam, fmt, arg...) \
134 dev_err(&(cam)->platdev->dev, fmt, ##arg)
135 #define cam_warn(cam, fmt, arg...) \
136 dev_warn(&(cam)->platdev->dev, fmt, ##arg)
137 #define cam_dbg(cam, fmt, arg...) \
138 dev_dbg(&(cam)->platdev->dev, fmt, ##arg)
141 * Format handling. This is ripped almost directly from Hans's changes
142 * to cafe_ccic.c. It's a little unfortunate; until this change, we
143 * didn't need to know anything about the format except its byte depth;
144 * now this information must be managed at this level too.
146 static struct via_format {
148 int bpp; /* Bytes per pixel */
152 .pixelformat = V4L2_PIX_FMT_YUYV,
153 .mbus_code = MEDIA_BUS_FMT_YUYV8_2X8,
156 /* RGB444 and Bayer should be doable, but have never been
157 tested with this driver. RGB565 seems to work at the default
158 resolution, but results in color corruption when being scaled by
159 viacam_set_scaled(), and is disabled as a result. */
161 #define N_VIA_FMTS ARRAY_SIZE(via_formats)
163 static struct via_format *via_find_format(u32 pixelformat)
167 for (i = 0; i < N_VIA_FMTS; i++)
168 if (via_formats[i].pixelformat == pixelformat)
169 return via_formats + i;
170 /* Not found? Then return the first format. */
175 /*--------------------------------------------------------------------------*/
177 * Sensor power/reset management. This piece is OLPC-specific for
178 * sure; other configurations will have things connected differently.
180 static int via_sensor_power_setup(struct via_camera *cam)
182 struct device *dev = &cam->platdev->dev;
184 cam->power_gpio = devm_gpiod_get(dev, "VGPIO3", GPIOD_OUT_LOW);
185 if (IS_ERR(cam->power_gpio))
186 return dev_err_probe(dev, PTR_ERR(cam->power_gpio),
187 "failed to get power GPIO");
189 /* Request the reset line asserted */
190 cam->reset_gpio = devm_gpiod_get(dev, "VGPIO2", GPIOD_OUT_HIGH);
191 if (IS_ERR(cam->reset_gpio))
192 return dev_err_probe(dev, PTR_ERR(cam->reset_gpio),
193 "failed to get reset GPIO");
199 * Power up the sensor and perform the reset dance.
201 static void via_sensor_power_up(struct via_camera *cam)
203 gpiod_set_value(cam->power_gpio, 1);
204 gpiod_set_value(cam->reset_gpio, 1);
205 msleep(20); /* Probably excessive */
206 gpiod_set_value(cam->reset_gpio, 0);
210 static void via_sensor_power_down(struct via_camera *cam)
212 gpiod_set_value(cam->power_gpio, 0);
213 gpiod_set_value(cam->reset_gpio, 1);
217 static void via_sensor_power_release(struct via_camera *cam)
219 via_sensor_power_down(cam);
222 /* --------------------------------------------------------------------------*/
226 * Manage the ov7670 "flip" bit, which needs special help.
228 static int viacam_set_flip(struct via_camera *cam)
230 struct v4l2_control ctrl;
232 memset(&ctrl, 0, sizeof(ctrl));
233 ctrl.id = V4L2_CID_VFLIP;
234 ctrl.value = flip_image;
235 return v4l2_s_ctrl(NULL, cam->sensor->ctrl_handler, &ctrl);
239 * Configure the sensor. It's up to the caller to ensure
240 * that the camera is in the correct operating state.
242 static int viacam_configure_sensor(struct via_camera *cam)
244 struct v4l2_subdev_format format = {
245 .which = V4L2_SUBDEV_FORMAT_ACTIVE,
249 v4l2_fill_mbus_format(&format.format, &cam->sensor_format, cam->mbus_code);
250 ret = sensor_call(cam, core, init, 0);
252 ret = sensor_call(cam, pad, set_fmt, NULL, &format);
254 * OV7670 does weird things if flip is set *before* format...
257 ret = viacam_set_flip(cam);
263 /* --------------------------------------------------------------------------*/
265 * Some simple register accessors; they assume that the lock is held.
267 * Should we want to support the second capture engine, we could
268 * hide the register difference by adding 0x1000 to registers in the
271 static inline void viacam_write_reg(struct via_camera *cam,
274 iowrite32(value, cam->mmio + reg);
277 static inline int viacam_read_reg(struct via_camera *cam, int reg)
279 return ioread32(cam->mmio + reg);
282 static inline void viacam_write_reg_mask(struct via_camera *cam,
283 int reg, int value, int mask)
285 int tmp = viacam_read_reg(cam, reg);
287 tmp = (tmp & ~mask) | (value & mask);
288 viacam_write_reg(cam, reg, tmp);
292 /* --------------------------------------------------------------------------*/
293 /* Interrupt management and handling */
295 static irqreturn_t viacam_quick_irq(int irq, void *data)
297 struct via_camera *cam = data;
298 irqreturn_t ret = IRQ_NONE;
302 * All we do here is to clear the interrupts and tell
303 * the handler thread to wake up.
305 spin_lock(&cam->viadev->reg_lock);
306 icv = viacam_read_reg(cam, VCR_INTCTRL);
307 if (icv & VCR_IC_EAV) {
308 icv |= VCR_IC_EAV|VCR_IC_EVBI|VCR_IC_FFULL;
309 viacam_write_reg(cam, VCR_INTCTRL, icv);
310 ret = IRQ_WAKE_THREAD;
312 spin_unlock(&cam->viadev->reg_lock);
317 * Find the next buffer which has somebody waiting on it.
319 static struct via_buffer *viacam_next_buffer(struct via_camera *cam)
321 if (cam->opstate != S_RUNNING)
323 if (list_empty(&cam->buffer_queue))
325 return list_entry(cam->buffer_queue.next, struct via_buffer, queue);
329 * The threaded IRQ handler.
331 static irqreturn_t viacam_irq(int irq, void *data)
333 struct via_camera *cam = data;
334 struct via_buffer *vb;
336 struct sg_table *sgt;
338 mutex_lock(&cam->lock);
340 * If there is no place to put the data frame, don't bother
341 * with anything else.
343 vb = viacam_next_buffer(cam);
347 * Figure out which buffer we just completed.
349 bufn = (viacam_read_reg(cam, VCR_INTCTRL) & VCR_IC_ACTBUF) >> 3;
352 bufn = cam->n_cap_bufs - 1;
354 * Copy over the data and let any waiters know.
356 sgt = vb2_dma_sg_plane_desc(&vb->vbuf.vb2_buf, 0);
357 vb->vbuf.vb2_buf.timestamp = ktime_get_ns();
358 viafb_dma_copy_out_sg(cam->cb_offsets[bufn], sgt->sgl, sgt->nents);
359 vb->vbuf.sequence = cam->sequence++;
360 vb->vbuf.field = V4L2_FIELD_NONE;
361 list_del(&vb->queue);
362 vb2_buffer_done(&vb->vbuf.vb2_buf, VB2_BUF_STATE_DONE);
364 mutex_unlock(&cam->lock);
370 * These functions must mess around with the general interrupt
371 * control register, which is relevant to much more than just the
372 * camera. Nothing else uses interrupts, though, as of this writing.
373 * Should that situation change, we'll have to improve support at
374 * the via-core level.
376 static void viacam_int_enable(struct via_camera *cam)
378 viacam_write_reg(cam, VCR_INTCTRL,
379 VCR_IC_INTEN|VCR_IC_EAV|VCR_IC_EVBI|VCR_IC_FFULL);
380 viafb_irq_enable(VDE_I_C0AVEN);
383 static void viacam_int_disable(struct via_camera *cam)
385 viafb_irq_disable(VDE_I_C0AVEN);
386 viacam_write_reg(cam, VCR_INTCTRL, 0);
391 /* --------------------------------------------------------------------------*/
392 /* Controller operations */
395 * Set up our capture buffers in framebuffer memory.
397 static int viacam_ctlr_cbufs(struct via_camera *cam)
399 int nbuf = cam->viadev->camera_fbmem_size/cam->sensor_format.sizeimage;
404 * See how many buffers we can work with.
408 viacam_write_reg_mask(cam, VCR_CAPINTC, VCR_CI_3BUFS,
410 } else if (nbuf == 2) {
412 viacam_write_reg_mask(cam, VCR_CAPINTC, 0, VCR_CI_3BUFS);
414 cam_warn(cam, "Insufficient frame buffer memory\n");
420 offset = cam->fb_offset;
421 for (i = 0; i < cam->n_cap_bufs; i++) {
422 cam->cb_offsets[i] = offset;
423 cam->cb_addrs[i] = cam->fbmem + offset;
424 viacam_write_reg(cam, VCR_VBUF1 + i*4, offset & VCR_VBUF_MASK);
425 offset += cam->sensor_format.sizeimage;
431 * Set the scaling register for downscaling the image.
433 * This register works like this... Vertical scaling is enabled
434 * by bit 26; if that bit is set, downscaling is controlled by the
435 * value in bits 16:25. Those bits are divided by 1024 to get
436 * the scaling factor; setting just bit 25 thus cuts the height
439 * Horizontal scaling works about the same, but it's enabled by
440 * bit 11, with bits 0:10 giving the numerator of a fraction
441 * (over 2048) for the scaling value.
443 * This function is naive in that, if the user departs from
444 * the 3x4 VGA scaling factor, the image will distort. We
445 * could work around that if it really seemed important.
447 static void viacam_set_scale(struct via_camera *cam)
449 unsigned int avscale;
452 if (cam->user_format.width == VGA_WIDTH)
455 sf = (cam->user_format.width*2048)/VGA_WIDTH;
456 avscale = VCR_AVS_HEN | sf;
458 if (cam->user_format.height < VGA_HEIGHT) {
459 sf = (1024*cam->user_format.height)/VGA_HEIGHT;
460 avscale |= VCR_AVS_VEN | (sf << 16);
462 viacam_write_reg(cam, VCR_AVSCALE, avscale);
467 * Configure image-related information into the capture engine.
469 static void viacam_ctlr_image(struct via_camera *cam)
474 * Disable clock before messing with stuff - from the via
477 viacam_write_reg(cam, VCR_CAPINTC, ~(VCR_CI_ENABLE|VCR_CI_CLKEN));
479 * Set up the controller for VGA resolution, modulo magic
480 * offsets from the via sample driver.
482 viacam_write_reg(cam, VCR_HORRANGE, 0x06200120);
483 viacam_write_reg(cam, VCR_VERTRANGE, 0x01de0000);
484 viacam_set_scale(cam);
488 viacam_write_reg(cam, VCR_MAXDATA,
489 (cam->sensor_format.height << 16) |
490 (cam->sensor_format.bytesperline >> 3));
491 viacam_write_reg(cam, VCR_MAXVBI, 0);
492 viacam_write_reg(cam, VCR_VSTRIDE,
493 cam->user_format.bytesperline & VCR_VS_STRIDE);
495 * Set up the capture interface control register,
496 * everything but the "go" bit.
498 * The FIFO threshold is a bit of a magic number; 8 is what
499 * VIA's sample code uses.
501 cicreg = VCR_CI_CLKEN |
502 0x08000000 | /* FIFO threshold */
503 VCR_CI_FLDINV | /* OLPC-specific? */
504 VCR_CI_VREFINV | /* OLPC-specific? */
505 VCR_CI_DIBOTH | /* Capture both fields */
507 if (cam->n_cap_bufs == 3)
508 cicreg |= VCR_CI_3BUFS;
510 * YUV formats need different byte swapping than RGB.
512 if (cam->user_format.pixelformat == V4L2_PIX_FMT_YUYV)
513 cicreg |= VCR_CI_YUYV;
515 cicreg |= VCR_CI_UYVY;
516 viacam_write_reg(cam, VCR_CAPINTC, cicreg);
520 static int viacam_config_controller(struct via_camera *cam)
525 spin_lock_irqsave(&cam->viadev->reg_lock, flags);
526 ret = viacam_ctlr_cbufs(cam);
528 viacam_ctlr_image(cam);
529 spin_unlock_irqrestore(&cam->viadev->reg_lock, flags);
530 clear_bit(CF_CONFIG_NEEDED, &cam->flags);
535 * Make it start grabbing data.
537 static void viacam_start_engine(struct via_camera *cam)
539 spin_lock_irq(&cam->viadev->reg_lock);
540 viacam_write_reg_mask(cam, VCR_CAPINTC, VCR_CI_ENABLE, VCR_CI_ENABLE);
541 viacam_int_enable(cam);
542 (void) viacam_read_reg(cam, VCR_CAPINTC); /* Force post */
543 cam->opstate = S_RUNNING;
544 spin_unlock_irq(&cam->viadev->reg_lock);
548 static void viacam_stop_engine(struct via_camera *cam)
550 spin_lock_irq(&cam->viadev->reg_lock);
551 viacam_int_disable(cam);
552 viacam_write_reg_mask(cam, VCR_CAPINTC, 0, VCR_CI_ENABLE);
553 (void) viacam_read_reg(cam, VCR_CAPINTC); /* Force post */
554 cam->opstate = S_IDLE;
555 spin_unlock_irq(&cam->viadev->reg_lock);
559 /* --------------------------------------------------------------------------*/
560 /* vb2 callback ops */
562 static struct via_buffer *vb2_to_via_buffer(struct vb2_buffer *vb)
564 struct vb2_v4l2_buffer *vbuf = to_vb2_v4l2_buffer(vb);
566 return container_of(vbuf, struct via_buffer, vbuf);
569 static void viacam_vb2_queue(struct vb2_buffer *vb)
571 struct via_camera *cam = vb2_get_drv_priv(vb->vb2_queue);
572 struct via_buffer *via = vb2_to_via_buffer(vb);
574 list_add_tail(&via->queue, &cam->buffer_queue);
577 static int viacam_vb2_prepare(struct vb2_buffer *vb)
579 struct via_camera *cam = vb2_get_drv_priv(vb->vb2_queue);
581 if (vb2_plane_size(vb, 0) < cam->user_format.sizeimage) {
583 "Plane size too small (%lu < %u)\n",
584 vb2_plane_size(vb, 0),
585 cam->user_format.sizeimage);
589 vb2_set_plane_payload(vb, 0, cam->user_format.sizeimage);
594 static int viacam_vb2_queue_setup(struct vb2_queue *vq,
596 unsigned int *num_planes, unsigned int sizes[],
597 struct device *alloc_devs[])
599 struct via_camera *cam = vb2_get_drv_priv(vq);
600 int size = cam->user_format.sizeimage;
603 return sizes[0] < size ? -EINVAL : 0;
610 static int viacam_vb2_start_streaming(struct vb2_queue *vq, unsigned int count)
612 struct via_camera *cam = vb2_get_drv_priv(vq);
613 struct via_buffer *buf, *tmp;
616 if (cam->opstate != S_IDLE) {
621 * Configure things if need be.
623 if (test_bit(CF_CONFIG_NEEDED, &cam->flags)) {
624 ret = viacam_configure_sensor(cam);
627 ret = viacam_config_controller(cam);
633 * If the CPU goes into C3, the DMA transfer gets corrupted and
634 * users start filing unsightly bug reports. Put in a "latency"
635 * requirement which will keep the CPU out of the deeper sleep
638 cpu_latency_qos_add_request(&cam->qos_request, 50);
639 viacam_start_engine(cam);
642 list_for_each_entry_safe(buf, tmp, &cam->buffer_queue, queue) {
643 list_del(&buf->queue);
644 vb2_buffer_done(&buf->vbuf.vb2_buf, VB2_BUF_STATE_QUEUED);
649 static void viacam_vb2_stop_streaming(struct vb2_queue *vq)
651 struct via_camera *cam = vb2_get_drv_priv(vq);
652 struct via_buffer *buf, *tmp;
654 cpu_latency_qos_remove_request(&cam->qos_request);
655 viacam_stop_engine(cam);
657 list_for_each_entry_safe(buf, tmp, &cam->buffer_queue, queue) {
658 list_del(&buf->queue);
659 vb2_buffer_done(&buf->vbuf.vb2_buf, VB2_BUF_STATE_ERROR);
663 static const struct vb2_ops viacam_vb2_ops = {
664 .queue_setup = viacam_vb2_queue_setup,
665 .buf_queue = viacam_vb2_queue,
666 .buf_prepare = viacam_vb2_prepare,
667 .start_streaming = viacam_vb2_start_streaming,
668 .stop_streaming = viacam_vb2_stop_streaming,
669 .wait_prepare = vb2_ops_wait_prepare,
670 .wait_finish = vb2_ops_wait_finish,
673 /* --------------------------------------------------------------------------*/
674 /* File operations */
676 static int viacam_open(struct file *filp)
678 struct via_camera *cam = video_drvdata(filp);
682 * Note the new user. If this is the first one, we'll also
683 * need to power up the sensor.
685 mutex_lock(&cam->lock);
686 ret = v4l2_fh_open(filp);
689 if (v4l2_fh_is_singular_file(filp)) {
690 ret = viafb_request_dma();
693 v4l2_fh_release(filp);
696 via_sensor_power_up(cam);
697 set_bit(CF_CONFIG_NEEDED, &cam->flags);
700 mutex_unlock(&cam->lock);
704 static int viacam_release(struct file *filp)
706 struct via_camera *cam = video_drvdata(filp);
709 mutex_lock(&cam->lock);
710 last_open = v4l2_fh_is_singular_file(filp);
711 _vb2_fop_release(filp, NULL);
713 * Last one out needs to turn out the lights.
716 via_sensor_power_down(cam);
719 mutex_unlock(&cam->lock);
723 static const struct v4l2_file_operations viacam_fops = {
724 .owner = THIS_MODULE,
726 .release = viacam_release,
727 .read = vb2_fop_read,
728 .poll = vb2_fop_poll,
729 .mmap = vb2_fop_mmap,
730 .unlocked_ioctl = video_ioctl2,
733 /*----------------------------------------------------------------------------*/
735 * The long list of v4l2 ioctl ops
741 static int viacam_enum_input(struct file *filp, void *priv,
742 struct v4l2_input *input)
744 if (input->index != 0)
747 input->type = V4L2_INPUT_TYPE_CAMERA;
748 strscpy(input->name, "Camera", sizeof(input->name));
752 static int viacam_g_input(struct file *filp, void *priv, unsigned int *i)
758 static int viacam_s_input(struct file *filp, void *priv, unsigned int i)
766 * Video format stuff. Here is our default format until
767 * user space messes with things.
769 static const struct v4l2_pix_format viacam_def_pix_format = {
771 .height = VGA_HEIGHT,
772 .pixelformat = V4L2_PIX_FMT_YUYV,
773 .field = V4L2_FIELD_NONE,
774 .bytesperline = VGA_WIDTH * 2,
775 .sizeimage = VGA_WIDTH * VGA_HEIGHT * 2,
776 .colorspace = V4L2_COLORSPACE_SRGB,
779 static const u32 via_def_mbus_code = MEDIA_BUS_FMT_YUYV8_2X8;
781 static int viacam_enum_fmt_vid_cap(struct file *filp, void *priv,
782 struct v4l2_fmtdesc *fmt)
784 if (fmt->index >= N_VIA_FMTS)
786 fmt->pixelformat = via_formats[fmt->index].pixelformat;
791 * Figure out proper image dimensions, but always force the
794 static void viacam_fmt_pre(struct v4l2_pix_format *userfmt,
795 struct v4l2_pix_format *sensorfmt)
797 *sensorfmt = *userfmt;
798 if (userfmt->width < QCIF_WIDTH || userfmt->height < QCIF_HEIGHT) {
799 userfmt->width = QCIF_WIDTH;
800 userfmt->height = QCIF_HEIGHT;
802 if (userfmt->width > VGA_WIDTH || userfmt->height > VGA_HEIGHT) {
803 userfmt->width = VGA_WIDTH;
804 userfmt->height = VGA_HEIGHT;
806 sensorfmt->width = VGA_WIDTH;
807 sensorfmt->height = VGA_HEIGHT;
810 static void viacam_fmt_post(struct v4l2_pix_format *userfmt,
811 struct v4l2_pix_format *sensorfmt)
813 struct via_format *f = via_find_format(userfmt->pixelformat);
815 sensorfmt->bytesperline = sensorfmt->width * f->bpp;
816 sensorfmt->sizeimage = sensorfmt->height * sensorfmt->bytesperline;
817 userfmt->pixelformat = sensorfmt->pixelformat;
818 userfmt->field = sensorfmt->field;
819 userfmt->bytesperline = 2 * userfmt->width;
820 userfmt->sizeimage = userfmt->bytesperline * userfmt->height;
821 userfmt->colorspace = sensorfmt->colorspace;
822 userfmt->ycbcr_enc = sensorfmt->ycbcr_enc;
823 userfmt->quantization = sensorfmt->quantization;
824 userfmt->xfer_func = sensorfmt->xfer_func;
829 * The real work of figuring out a workable format.
831 static int viacam_do_try_fmt(struct via_camera *cam,
832 struct v4l2_pix_format *upix, struct v4l2_pix_format *spix)
835 struct v4l2_subdev_pad_config pad_cfg;
836 struct v4l2_subdev_state pad_state = {
839 struct v4l2_subdev_format format = {
840 .which = V4L2_SUBDEV_FORMAT_TRY,
842 struct via_format *f = via_find_format(upix->pixelformat);
844 upix->pixelformat = f->pixelformat;
845 viacam_fmt_pre(upix, spix);
846 v4l2_fill_mbus_format(&format.format, spix, f->mbus_code);
847 ret = sensor_call(cam, pad, set_fmt, &pad_state, &format);
848 v4l2_fill_pix_format(spix, &format.format);
849 viacam_fmt_post(upix, spix);
855 static int viacam_try_fmt_vid_cap(struct file *filp, void *priv,
856 struct v4l2_format *fmt)
858 struct via_camera *cam = video_drvdata(filp);
859 struct v4l2_format sfmt;
861 return viacam_do_try_fmt(cam, &fmt->fmt.pix, &sfmt.fmt.pix);
865 static int viacam_g_fmt_vid_cap(struct file *filp, void *priv,
866 struct v4l2_format *fmt)
868 struct via_camera *cam = video_drvdata(filp);
870 fmt->fmt.pix = cam->user_format;
874 static int viacam_s_fmt_vid_cap(struct file *filp, void *priv,
875 struct v4l2_format *fmt)
877 struct via_camera *cam = video_drvdata(filp);
879 struct v4l2_format sfmt;
880 struct via_format *f = via_find_format(fmt->fmt.pix.pixelformat);
883 * Camera must be idle or we can't mess with the
886 if (cam->opstate != S_IDLE)
889 * Let the sensor code look over and tweak the
890 * requested formatting.
892 ret = viacam_do_try_fmt(cam, &fmt->fmt.pix, &sfmt.fmt.pix);
896 * OK, let's commit to the new format.
898 cam->user_format = fmt->fmt.pix;
899 cam->sensor_format = sfmt.fmt.pix;
900 cam->mbus_code = f->mbus_code;
901 ret = viacam_configure_sensor(cam);
903 ret = viacam_config_controller(cam);
907 static int viacam_querycap(struct file *filp, void *priv,
908 struct v4l2_capability *cap)
910 strscpy(cap->driver, "via-camera", sizeof(cap->driver));
911 strscpy(cap->card, "via-camera", sizeof(cap->card));
912 strscpy(cap->bus_info, "platform:via-camera", sizeof(cap->bus_info));
918 static int viacam_g_parm(struct file *filp, void *priv,
919 struct v4l2_streamparm *parm)
921 struct via_camera *cam = video_drvdata(filp);
923 return v4l2_g_parm_cap(video_devdata(filp), cam->sensor, parm);
926 static int viacam_s_parm(struct file *filp, void *priv,
927 struct v4l2_streamparm *parm)
929 struct via_camera *cam = video_drvdata(filp);
931 return v4l2_s_parm_cap(video_devdata(filp), cam->sensor, parm);
934 static int viacam_enum_framesizes(struct file *filp, void *priv,
935 struct v4l2_frmsizeenum *sizes)
939 if (sizes->index != 0)
941 for (i = 0; i < N_VIA_FMTS; i++)
942 if (sizes->pixel_format == via_formats[i].pixelformat)
946 sizes->type = V4L2_FRMSIZE_TYPE_CONTINUOUS;
947 sizes->stepwise.min_width = QCIF_WIDTH;
948 sizes->stepwise.min_height = QCIF_HEIGHT;
949 sizes->stepwise.max_width = VGA_WIDTH;
950 sizes->stepwise.max_height = VGA_HEIGHT;
951 sizes->stepwise.step_width = sizes->stepwise.step_height = 1;
955 static int viacam_enum_frameintervals(struct file *filp, void *priv,
956 struct v4l2_frmivalenum *interval)
958 struct via_camera *cam = video_drvdata(filp);
959 struct v4l2_subdev_frame_interval_enum fie = {
960 .index = interval->index,
961 .code = cam->mbus_code,
962 .width = cam->sensor_format.width,
963 .height = cam->sensor_format.height,
964 .which = V4L2_SUBDEV_FORMAT_ACTIVE,
969 for (i = 0; i < N_VIA_FMTS; i++)
970 if (interval->pixel_format == via_formats[i].pixelformat)
974 if (interval->width < QCIF_WIDTH || interval->width > VGA_WIDTH ||
975 interval->height < QCIF_HEIGHT || interval->height > VGA_HEIGHT)
977 ret = sensor_call(cam, pad, enum_frame_interval, NULL, &fie);
980 interval->type = V4L2_FRMIVAL_TYPE_DISCRETE;
981 interval->discrete = fie.interval;
985 static const struct v4l2_ioctl_ops viacam_ioctl_ops = {
986 .vidioc_enum_input = viacam_enum_input,
987 .vidioc_g_input = viacam_g_input,
988 .vidioc_s_input = viacam_s_input,
989 .vidioc_enum_fmt_vid_cap = viacam_enum_fmt_vid_cap,
990 .vidioc_try_fmt_vid_cap = viacam_try_fmt_vid_cap,
991 .vidioc_g_fmt_vid_cap = viacam_g_fmt_vid_cap,
992 .vidioc_s_fmt_vid_cap = viacam_s_fmt_vid_cap,
993 .vidioc_querycap = viacam_querycap,
994 .vidioc_reqbufs = vb2_ioctl_reqbufs,
995 .vidioc_create_bufs = vb2_ioctl_create_bufs,
996 .vidioc_querybuf = vb2_ioctl_querybuf,
997 .vidioc_prepare_buf = vb2_ioctl_prepare_buf,
998 .vidioc_qbuf = vb2_ioctl_qbuf,
999 .vidioc_dqbuf = vb2_ioctl_dqbuf,
1000 .vidioc_expbuf = vb2_ioctl_expbuf,
1001 .vidioc_streamon = vb2_ioctl_streamon,
1002 .vidioc_streamoff = vb2_ioctl_streamoff,
1003 .vidioc_g_parm = viacam_g_parm,
1004 .vidioc_s_parm = viacam_s_parm,
1005 .vidioc_enum_framesizes = viacam_enum_framesizes,
1006 .vidioc_enum_frameintervals = viacam_enum_frameintervals,
1007 .vidioc_subscribe_event = v4l2_ctrl_subscribe_event,
1008 .vidioc_unsubscribe_event = v4l2_event_unsubscribe,
1011 /*----------------------------------------------------------------------------*/
1018 static int viacam_suspend(void *priv)
1020 struct via_camera *cam = priv;
1021 enum viacam_opstate state = cam->opstate;
1023 if (cam->opstate != S_IDLE) {
1024 viacam_stop_engine(cam);
1025 cam->opstate = state; /* So resume restarts */
1031 static int viacam_resume(void *priv)
1033 struct via_camera *cam = priv;
1037 * Get back to a reasonable operating state.
1039 via_write_reg_mask(VIASR, 0x78, 0, 0x80);
1040 via_write_reg_mask(VIASR, 0x1e, 0xc0, 0xc0);
1041 viacam_int_disable(cam);
1042 set_bit(CF_CONFIG_NEEDED, &cam->flags);
1044 * Make sure the sensor's power state is correct
1046 if (!list_empty(&cam->vdev.fh_list))
1047 via_sensor_power_up(cam);
1049 via_sensor_power_down(cam);
1051 * If it was operating, try to restart it.
1053 if (cam->opstate != S_IDLE) {
1054 mutex_lock(&cam->lock);
1055 ret = viacam_configure_sensor(cam);
1057 ret = viacam_config_controller(cam);
1058 mutex_unlock(&cam->lock);
1060 viacam_start_engine(cam);
1066 static struct viafb_pm_hooks viacam_pm_hooks = {
1067 .suspend = viacam_suspend,
1068 .resume = viacam_resume
1071 #endif /* CONFIG_PM */
1077 static const struct video_device viacam_v4l_template = {
1078 .name = "via-camera",
1080 .fops = &viacam_fops,
1081 .ioctl_ops = &viacam_ioctl_ops,
1082 .release = video_device_release_empty, /* Check this */
1083 .device_caps = V4L2_CAP_VIDEO_CAPTURE | V4L2_CAP_READWRITE |
1088 * The OLPC folks put the serial port on the same pin as
1089 * the camera. They also get grumpy if we break the
1090 * serial port and keep them from using it. So we have
1091 * to check the serial enable bit and not step on it.
1093 #define VIACAM_SERIAL_DEVFN 0x88
1094 #define VIACAM_SERIAL_CREG 0x46
1095 #define VIACAM_SERIAL_BIT 0x40
1097 static bool viacam_serial_is_enabled(void)
1099 struct pci_bus *pbus = pci_find_bus(0, 0);
1104 pci_bus_read_config_byte(pbus, VIACAM_SERIAL_DEVFN,
1105 VIACAM_SERIAL_CREG, &cbyte);
1106 if ((cbyte & VIACAM_SERIAL_BIT) == 0)
1107 return false; /* Not enabled */
1108 if (!override_serial) {
1109 printk(KERN_NOTICE "Via camera: serial port is enabled, " \
1110 "refusing to load.\n");
1111 printk(KERN_NOTICE "Specify override_serial=1 to force " \
1112 "module loading.\n");
1115 printk(KERN_NOTICE "Via camera: overriding serial port\n");
1116 pci_bus_write_config_byte(pbus, VIACAM_SERIAL_DEVFN,
1117 VIACAM_SERIAL_CREG, cbyte & ~VIACAM_SERIAL_BIT);
1121 static struct ov7670_config sensor_cfg = {
1122 /* The XO-1.5 (only known user) clocks the camera at 90MHz. */
1126 static int viacam_probe(struct platform_device *pdev)
1129 struct i2c_adapter *sensor_adapter;
1130 struct viafb_dev *viadev = pdev->dev.platform_data;
1131 struct vb2_queue *vq;
1132 struct i2c_board_info ov7670_info = {
1135 .platform_data = &sensor_cfg,
1139 * Note that there are actually two capture channels on
1140 * the device. We only deal with one for now. That
1141 * is encoded here; nothing else assumes it's dealing with
1142 * a unique capture device.
1144 struct via_camera *cam;
1147 * Ensure that frame buffer memory has been set aside for
1148 * this purpose. As an arbitrary limit, refuse to work
1149 * with less than two frames of VGA 16-bit data.
1151 * If we ever support the second port, we'll need to set
1152 * aside more memory.
1154 if (viadev->camera_fbmem_size < (VGA_HEIGHT*VGA_WIDTH*4)) {
1155 printk(KERN_ERR "viacam: insufficient FB memory reserved\n");
1158 if (viadev->engine_mmio == NULL) {
1159 printk(KERN_ERR "viacam: No I/O memory, so no pictures\n");
1163 if (machine_is_olpc() && viacam_serial_is_enabled())
1167 * Basic structure initialization.
1169 cam = kzalloc (sizeof(struct via_camera), GFP_KERNEL);
1173 cam->platdev = pdev;
1174 cam->viadev = viadev;
1175 cam->opstate = S_IDLE;
1176 cam->user_format = cam->sensor_format = viacam_def_pix_format;
1177 mutex_init(&cam->lock);
1178 INIT_LIST_HEAD(&cam->buffer_queue);
1179 cam->mmio = viadev->engine_mmio;
1180 cam->fbmem = viadev->fbmem;
1181 cam->fb_offset = viadev->camera_fbmem_offset;
1182 cam->flags = 1 << CF_CONFIG_NEEDED;
1183 cam->mbus_code = via_def_mbus_code;
1185 * Tell V4L that we exist.
1187 ret = v4l2_device_register(&pdev->dev, &cam->v4l2_dev);
1189 dev_err(&pdev->dev, "Unable to register v4l2 device\n");
1192 ret = v4l2_ctrl_handler_init(&cam->ctrl_handler, 10);
1194 goto out_unregister;
1195 cam->v4l2_dev.ctrl_handler = &cam->ctrl_handler;
1197 * Convince the system that we can do DMA.
1199 pdev->dev.dma_mask = &viadev->pdev->dma_mask;
1200 ret = dma_set_mask(&pdev->dev, 0xffffffff);
1202 goto out_ctrl_hdl_free;
1204 * Fire up the capture port. The write to 0x78 looks purely
1205 * OLPCish; any system will need to tweak 0x1e.
1207 via_write_reg_mask(VIASR, 0x78, 0, 0x80);
1208 via_write_reg_mask(VIASR, 0x1e, 0xc0, 0xc0);
1210 * Get the sensor powered up.
1212 ret = via_sensor_power_setup(cam);
1214 goto out_ctrl_hdl_free;
1215 via_sensor_power_up(cam);
1218 * See if we can't find it on the bus. The VIA_PORT_31 assumption
1219 * is OLPC-specific. 0x42 assumption is ov7670-specific.
1221 sensor_adapter = viafb_find_i2c_adapter(VIA_PORT_31);
1222 cam->sensor = v4l2_i2c_new_subdev_board(&cam->v4l2_dev, sensor_adapter,
1223 &ov7670_info, NULL);
1224 if (cam->sensor == NULL) {
1225 dev_err(&pdev->dev, "Unable to find the sensor!\n");
1227 goto out_power_down;
1232 viacam_int_disable(cam);
1233 ret = request_threaded_irq(viadev->pdev->irq, viacam_quick_irq,
1234 viacam_irq, IRQF_SHARED, "via-camera", cam);
1236 goto out_power_down;
1239 vq->type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
1240 vq->io_modes = VB2_MMAP | VB2_USERPTR | VB2_DMABUF | VB2_READ;
1242 vq->timestamp_flags = V4L2_BUF_FLAG_TIMESTAMP_MONOTONIC;
1243 vq->buf_struct_size = sizeof(struct via_buffer);
1244 vq->dev = cam->v4l2_dev.dev;
1246 vq->ops = &viacam_vb2_ops;
1247 vq->mem_ops = &vb2_dma_sg_memops;
1248 vq->lock = &cam->lock;
1250 ret = vb2_queue_init(vq);
1252 * Tell V4l2 that we exist.
1254 cam->vdev = viacam_v4l_template;
1255 cam->vdev.v4l2_dev = &cam->v4l2_dev;
1256 cam->vdev.lock = &cam->lock;
1257 cam->vdev.queue = vq;
1258 video_set_drvdata(&cam->vdev, cam);
1259 ret = video_register_device(&cam->vdev, VFL_TYPE_VIDEO, -1);
1265 * Hook into PM events
1267 viacam_pm_hooks.private = cam;
1268 viafb_pm_register(&viacam_pm_hooks);
1271 /* Power the sensor down until somebody opens the device */
1272 via_sensor_power_down(cam);
1276 free_irq(viadev->pdev->irq, cam);
1278 via_sensor_power_release(cam);
1280 v4l2_ctrl_handler_free(&cam->ctrl_handler);
1282 v4l2_device_unregister(&cam->v4l2_dev);
1288 static void viacam_remove(struct platform_device *pdev)
1290 struct via_camera *cam = via_cam_info;
1291 struct viafb_dev *viadev = pdev->dev.platform_data;
1293 video_unregister_device(&cam->vdev);
1294 v4l2_device_unregister(&cam->v4l2_dev);
1296 viafb_pm_unregister(&viacam_pm_hooks);
1298 free_irq(viadev->pdev->irq, cam);
1299 via_sensor_power_release(cam);
1300 v4l2_ctrl_handler_free(&cam->ctrl_handler);
1302 via_cam_info = NULL;
1305 static struct platform_driver viacam_driver = {
1307 .name = "viafb-camera",
1309 .probe = viacam_probe,
1310 .remove_new = viacam_remove,
1313 module_platform_driver(viacam_driver);