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.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-image-sizes.h>
22 #include <media/i2c/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>
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
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 videobuf_queue vb_queue;
91 struct list_head buffer_queue; /* prot. by reg_lock */
98 * Video format information. sensor_format is kept in a form
99 * that we can use to pass to the sensor. We always run the
100 * sensor in VGA resolution, though, and let the controller
101 * downscale things if need be. So we keep the "real*
102 * dimensions separately.
104 struct v4l2_pix_format sensor_format;
105 struct v4l2_pix_format user_format;
110 * Yes, this is a hack, but there's only going to be one of these
111 * on any system we know of.
113 static struct via_camera *via_cam_info;
116 * Flag values, manipulated with bitops
118 #define CF_DMA_ACTIVE 0 /* A frame is incoming */
119 #define CF_CONFIG_NEEDED 1 /* Must configure hardware */
123 * Nasty ugly v4l2 boilerplate.
125 #define sensor_call(cam, optype, func, args...) \
126 v4l2_subdev_call(cam->sensor, optype, func, ##args)
129 * Debugging and related.
131 #define cam_err(cam, fmt, arg...) \
132 dev_err(&(cam)->platdev->dev, fmt, ##arg);
133 #define cam_warn(cam, fmt, arg...) \
134 dev_warn(&(cam)->platdev->dev, fmt, ##arg);
135 #define cam_dbg(cam, fmt, arg...) \
136 dev_dbg(&(cam)->platdev->dev, fmt, ##arg);
139 * Format handling. This is ripped almost directly from Hans's changes
140 * to cafe_ccic.c. It's a little unfortunate; until this change, we
141 * didn't need to know anything about the format except its byte depth;
142 * now this information must be managed at this level too.
144 static struct via_format {
146 int bpp; /* Bytes per pixel */
150 .pixelformat = V4L2_PIX_FMT_YUYV,
151 .mbus_code = MEDIA_BUS_FMT_YUYV8_2X8,
154 /* RGB444 and Bayer should be doable, but have never been
155 tested with this driver. RGB565 seems to work at the default
156 resolution, but results in color corruption when being scaled by
157 viacam_set_scaled(), and is disabled as a result. */
159 #define N_VIA_FMTS ARRAY_SIZE(via_formats)
161 static struct via_format *via_find_format(u32 pixelformat)
165 for (i = 0; i < N_VIA_FMTS; i++)
166 if (via_formats[i].pixelformat == pixelformat)
167 return via_formats + i;
168 /* Not found? Then return the first format. */
173 /*--------------------------------------------------------------------------*/
175 * Sensor power/reset management. This piece is OLPC-specific for
176 * sure; other configurations will have things connected differently.
178 static int via_sensor_power_setup(struct via_camera *cam)
182 cam->power_gpio = viafb_gpio_lookup("VGPIO3");
183 cam->reset_gpio = viafb_gpio_lookup("VGPIO2");
184 if (!gpio_is_valid(cam->power_gpio) || !gpio_is_valid(cam->reset_gpio)) {
185 dev_err(&cam->platdev->dev, "Unable to find GPIO lines\n");
188 ret = gpio_request(cam->power_gpio, "viafb-camera");
190 dev_err(&cam->platdev->dev, "Unable to request power GPIO\n");
193 ret = gpio_request(cam->reset_gpio, "viafb-camera");
195 dev_err(&cam->platdev->dev, "Unable to request reset GPIO\n");
196 gpio_free(cam->power_gpio);
199 gpio_direction_output(cam->power_gpio, 0);
200 gpio_direction_output(cam->reset_gpio, 0);
205 * Power up the sensor and perform the reset dance.
207 static void via_sensor_power_up(struct via_camera *cam)
209 gpio_set_value(cam->power_gpio, 1);
210 gpio_set_value(cam->reset_gpio, 0);
211 msleep(20); /* Probably excessive */
212 gpio_set_value(cam->reset_gpio, 1);
216 static void via_sensor_power_down(struct via_camera *cam)
218 gpio_set_value(cam->power_gpio, 0);
219 gpio_set_value(cam->reset_gpio, 0);
223 static void via_sensor_power_release(struct via_camera *cam)
225 via_sensor_power_down(cam);
226 gpio_free(cam->power_gpio);
227 gpio_free(cam->reset_gpio);
230 /* --------------------------------------------------------------------------*/
234 * Manage the ov7670 "flip" bit, which needs special help.
236 static int viacam_set_flip(struct via_camera *cam)
238 struct v4l2_control ctrl;
240 memset(&ctrl, 0, sizeof(ctrl));
241 ctrl.id = V4L2_CID_VFLIP;
242 ctrl.value = flip_image;
243 return v4l2_s_ctrl(NULL, cam->sensor->ctrl_handler, &ctrl);
247 * Configure the sensor. It's up to the caller to ensure
248 * that the camera is in the correct operating state.
250 static int viacam_configure_sensor(struct via_camera *cam)
252 struct v4l2_subdev_format format = {
253 .which = V4L2_SUBDEV_FORMAT_ACTIVE,
257 v4l2_fill_mbus_format(&format.format, &cam->sensor_format, cam->mbus_code);
258 ret = sensor_call(cam, core, init, 0);
260 ret = sensor_call(cam, pad, set_fmt, NULL, &format);
262 * OV7670 does weird things if flip is set *before* format...
265 ret = viacam_set_flip(cam);
271 /* --------------------------------------------------------------------------*/
273 * Some simple register accessors; they assume that the lock is held.
275 * Should we want to support the second capture engine, we could
276 * hide the register difference by adding 0x1000 to registers in the
279 static inline void viacam_write_reg(struct via_camera *cam,
282 iowrite32(value, cam->mmio + reg);
285 static inline int viacam_read_reg(struct via_camera *cam, int reg)
287 return ioread32(cam->mmio + reg);
290 static inline void viacam_write_reg_mask(struct via_camera *cam,
291 int reg, int value, int mask)
293 int tmp = viacam_read_reg(cam, reg);
295 tmp = (tmp & ~mask) | (value & mask);
296 viacam_write_reg(cam, reg, tmp);
300 /* --------------------------------------------------------------------------*/
301 /* Interrupt management and handling */
303 static irqreturn_t viacam_quick_irq(int irq, void *data)
305 struct via_camera *cam = data;
306 irqreturn_t ret = IRQ_NONE;
310 * All we do here is to clear the interrupts and tell
311 * the handler thread to wake up.
313 spin_lock(&cam->viadev->reg_lock);
314 icv = viacam_read_reg(cam, VCR_INTCTRL);
315 if (icv & VCR_IC_EAV) {
316 icv |= VCR_IC_EAV|VCR_IC_EVBI|VCR_IC_FFULL;
317 viacam_write_reg(cam, VCR_INTCTRL, icv);
318 ret = IRQ_WAKE_THREAD;
320 spin_unlock(&cam->viadev->reg_lock);
325 * Find the next videobuf buffer which has somebody waiting on it.
327 static struct videobuf_buffer *viacam_next_buffer(struct via_camera *cam)
330 struct videobuf_buffer *buf = NULL;
332 spin_lock_irqsave(&cam->viadev->reg_lock, flags);
333 if (cam->opstate != S_RUNNING)
335 if (list_empty(&cam->buffer_queue))
337 buf = list_entry(cam->buffer_queue.next, struct videobuf_buffer, queue);
338 if (!waitqueue_active(&buf->done)) {/* Nobody waiting */
342 list_del(&buf->queue);
343 buf->state = VIDEOBUF_ACTIVE;
345 spin_unlock_irqrestore(&cam->viadev->reg_lock, flags);
350 * The threaded IRQ handler.
352 static irqreturn_t viacam_irq(int irq, void *data)
355 struct videobuf_buffer *vb;
356 struct via_camera *cam = data;
357 struct videobuf_dmabuf *vdma;
360 * If there is no place to put the data frame, don't bother
361 * with anything else.
363 vb = viacam_next_buffer(cam);
367 * Figure out which buffer we just completed.
369 bufn = (viacam_read_reg(cam, VCR_INTCTRL) & VCR_IC_ACTBUF) >> 3;
372 bufn = cam->n_cap_bufs - 1;
374 * Copy over the data and let any waiters know.
376 vdma = videobuf_to_dma(vb);
377 viafb_dma_copy_out_sg(cam->cb_offsets[bufn], vdma->sglist, vdma->sglen);
378 vb->state = VIDEOBUF_DONE;
379 vb->size = cam->user_format.sizeimage;
387 * These functions must mess around with the general interrupt
388 * control register, which is relevant to much more than just the
389 * camera. Nothing else uses interrupts, though, as of this writing.
390 * Should that situation change, we'll have to improve support at
391 * the via-core level.
393 static void viacam_int_enable(struct via_camera *cam)
395 viacam_write_reg(cam, VCR_INTCTRL,
396 VCR_IC_INTEN|VCR_IC_EAV|VCR_IC_EVBI|VCR_IC_FFULL);
397 viafb_irq_enable(VDE_I_C0AVEN);
400 static void viacam_int_disable(struct via_camera *cam)
402 viafb_irq_disable(VDE_I_C0AVEN);
403 viacam_write_reg(cam, VCR_INTCTRL, 0);
408 /* --------------------------------------------------------------------------*/
409 /* Controller operations */
412 * Set up our capture buffers in framebuffer memory.
414 static int viacam_ctlr_cbufs(struct via_camera *cam)
416 int nbuf = cam->viadev->camera_fbmem_size/cam->sensor_format.sizeimage;
421 * See how many buffers we can work with.
425 viacam_write_reg_mask(cam, VCR_CAPINTC, VCR_CI_3BUFS,
427 } else if (nbuf == 2) {
429 viacam_write_reg_mask(cam, VCR_CAPINTC, 0, VCR_CI_3BUFS);
431 cam_warn(cam, "Insufficient frame buffer memory\n");
437 offset = cam->fb_offset;
438 for (i = 0; i < cam->n_cap_bufs; i++) {
439 cam->cb_offsets[i] = offset;
440 cam->cb_addrs[i] = cam->fbmem + offset;
441 viacam_write_reg(cam, VCR_VBUF1 + i*4, offset & VCR_VBUF_MASK);
442 offset += cam->sensor_format.sizeimage;
448 * Set the scaling register for downscaling the image.
450 * This register works like this... Vertical scaling is enabled
451 * by bit 26; if that bit is set, downscaling is controlled by the
452 * value in bits 16:25. Those bits are divided by 1024 to get
453 * the scaling factor; setting just bit 25 thus cuts the height
456 * Horizontal scaling works about the same, but it's enabled by
457 * bit 11, with bits 0:10 giving the numerator of a fraction
458 * (over 2048) for the scaling value.
460 * This function is naive in that, if the user departs from
461 * the 3x4 VGA scaling factor, the image will distort. We
462 * could work around that if it really seemed important.
464 static void viacam_set_scale(struct via_camera *cam)
466 unsigned int avscale;
469 if (cam->user_format.width == VGA_WIDTH)
472 sf = (cam->user_format.width*2048)/VGA_WIDTH;
473 avscale = VCR_AVS_HEN | sf;
475 if (cam->user_format.height < VGA_HEIGHT) {
476 sf = (1024*cam->user_format.height)/VGA_HEIGHT;
477 avscale |= VCR_AVS_VEN | (sf << 16);
479 viacam_write_reg(cam, VCR_AVSCALE, avscale);
484 * Configure image-related information into the capture engine.
486 static void viacam_ctlr_image(struct via_camera *cam)
491 * Disable clock before messing with stuff - from the via
494 viacam_write_reg(cam, VCR_CAPINTC, ~(VCR_CI_ENABLE|VCR_CI_CLKEN));
496 * Set up the controller for VGA resolution, modulo magic
497 * offsets from the via sample driver.
499 viacam_write_reg(cam, VCR_HORRANGE, 0x06200120);
500 viacam_write_reg(cam, VCR_VERTRANGE, 0x01de0000);
501 viacam_set_scale(cam);
505 viacam_write_reg(cam, VCR_MAXDATA,
506 (cam->sensor_format.height << 16) |
507 (cam->sensor_format.bytesperline >> 3));
508 viacam_write_reg(cam, VCR_MAXVBI, 0);
509 viacam_write_reg(cam, VCR_VSTRIDE,
510 cam->user_format.bytesperline & VCR_VS_STRIDE);
512 * Set up the capture interface control register,
513 * everything but the "go" bit.
515 * The FIFO threshold is a bit of a magic number; 8 is what
516 * VIA's sample code uses.
518 cicreg = VCR_CI_CLKEN |
519 0x08000000 | /* FIFO threshold */
520 VCR_CI_FLDINV | /* OLPC-specific? */
521 VCR_CI_VREFINV | /* OLPC-specific? */
522 VCR_CI_DIBOTH | /* Capture both fields */
524 if (cam->n_cap_bufs == 3)
525 cicreg |= VCR_CI_3BUFS;
527 * YUV formats need different byte swapping than RGB.
529 if (cam->user_format.pixelformat == V4L2_PIX_FMT_YUYV)
530 cicreg |= VCR_CI_YUYV;
532 cicreg |= VCR_CI_UYVY;
533 viacam_write_reg(cam, VCR_CAPINTC, cicreg);
537 static int viacam_config_controller(struct via_camera *cam)
542 spin_lock_irqsave(&cam->viadev->reg_lock, flags);
543 ret = viacam_ctlr_cbufs(cam);
545 viacam_ctlr_image(cam);
546 spin_unlock_irqrestore(&cam->viadev->reg_lock, flags);
547 clear_bit(CF_CONFIG_NEEDED, &cam->flags);
552 * Make it start grabbing data.
554 static void viacam_start_engine(struct via_camera *cam)
556 spin_lock_irq(&cam->viadev->reg_lock);
558 viacam_write_reg_mask(cam, VCR_CAPINTC, VCR_CI_ENABLE, VCR_CI_ENABLE);
559 viacam_int_enable(cam);
560 (void) viacam_read_reg(cam, VCR_CAPINTC); /* Force post */
561 cam->opstate = S_RUNNING;
562 spin_unlock_irq(&cam->viadev->reg_lock);
566 static void viacam_stop_engine(struct via_camera *cam)
568 spin_lock_irq(&cam->viadev->reg_lock);
569 viacam_int_disable(cam);
570 viacam_write_reg_mask(cam, VCR_CAPINTC, 0, VCR_CI_ENABLE);
571 (void) viacam_read_reg(cam, VCR_CAPINTC); /* Force post */
572 cam->opstate = S_IDLE;
573 spin_unlock_irq(&cam->viadev->reg_lock);
577 /* --------------------------------------------------------------------------*/
578 /* Videobuf callback ops */
581 * buffer_setup. The purpose of this one would appear to be to tell
582 * videobuf how big a single image is. It's also evidently up to us
583 * to put some sort of limit on the maximum number of buffers allowed.
585 static int viacam_vb_buf_setup(struct videobuf_queue *q,
586 unsigned int *count, unsigned int *size)
588 struct via_camera *cam = q->priv_data;
590 *size = cam->user_format.sizeimage;
591 if (*count == 0 || *count > 6) /* Arbitrary number */
599 static int viacam_vb_buf_prepare(struct videobuf_queue *q,
600 struct videobuf_buffer *vb, enum v4l2_field field)
602 struct via_camera *cam = q->priv_data;
604 vb->size = cam->user_format.sizeimage;
605 vb->width = cam->user_format.width; /* bytesperline???? */
606 vb->height = cam->user_format.height;
608 if (vb->state == VIDEOBUF_NEEDS_INIT) {
609 int ret = videobuf_iolock(q, vb, NULL);
613 vb->state = VIDEOBUF_PREPARED;
618 * We've got a buffer to put data into.
620 * FIXME: check for a running engine and valid buffers?
622 static void viacam_vb_buf_queue(struct videobuf_queue *q,
623 struct videobuf_buffer *vb)
625 struct via_camera *cam = q->priv_data;
628 * Note that videobuf holds the lock when it calls
629 * us, so we need not (indeed, cannot) take it here.
631 vb->state = VIDEOBUF_QUEUED;
632 list_add_tail(&vb->queue, &cam->buffer_queue);
638 static void viacam_vb_buf_release(struct videobuf_queue *q,
639 struct videobuf_buffer *vb)
641 struct via_camera *cam = q->priv_data;
643 videobuf_dma_unmap(&cam->platdev->dev, videobuf_to_dma(vb));
644 videobuf_dma_free(videobuf_to_dma(vb));
645 vb->state = VIDEOBUF_NEEDS_INIT;
648 static const struct videobuf_queue_ops viacam_vb_ops = {
649 .buf_setup = viacam_vb_buf_setup,
650 .buf_prepare = viacam_vb_buf_prepare,
651 .buf_queue = viacam_vb_buf_queue,
652 .buf_release = viacam_vb_buf_release,
655 /* --------------------------------------------------------------------------*/
656 /* File operations */
658 static int viacam_open(struct file *filp)
660 struct via_camera *cam = video_drvdata(filp);
664 * Note the new user. If this is the first one, we'll also
665 * need to power up the sensor.
667 mutex_lock(&cam->lock);
668 ret = v4l2_fh_open(filp);
671 if (v4l2_fh_is_singular_file(filp)) {
672 ret = viafb_request_dma();
675 v4l2_fh_release(filp);
678 via_sensor_power_up(cam);
679 set_bit(CF_CONFIG_NEEDED, &cam->flags);
681 * Hook into videobuf. Evidently this cannot fail.
683 videobuf_queue_sg_init(&cam->vb_queue, &viacam_vb_ops,
684 &cam->platdev->dev, &cam->viadev->reg_lock,
685 V4L2_BUF_TYPE_VIDEO_CAPTURE, V4L2_FIELD_NONE,
686 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);
699 mutex_lock(&cam->lock);
701 last_open = v4l2_fh_is_singular_file(filp);
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.
722 videobuf_mmap_free(&cam->vb_queue);
723 via_sensor_power_down(cam);
726 v4l2_fh_release(filp);
727 mutex_unlock(&cam->lock);
732 * Read a frame from the device.
734 static ssize_t viacam_read(struct file *filp, char __user *buffer,
735 size_t len, loff_t *pos)
737 struct via_camera *cam = video_drvdata(filp);
740 mutex_lock(&cam->lock);
742 * Enforce the V4l2 "only one owner gets to read data" rule.
744 if (cam->owner && cam->owner != filp) {
750 * Do we need to configure the hardware?
752 if (test_bit(CF_CONFIG_NEEDED, &cam->flags)) {
753 ret = viacam_configure_sensor(cam);
755 ret = viacam_config_controller(cam);
760 * Fire up the capture engine, then have videobuf do
761 * the heavy lifting. Someday it would be good to avoid
762 * stopping and restarting the engine each time.
764 INIT_LIST_HEAD(&cam->buffer_queue);
765 viacam_start_engine(cam);
766 ret = videobuf_read_stream(&cam->vb_queue, buffer, len, pos, 0,
767 filp->f_flags & O_NONBLOCK);
768 viacam_stop_engine(cam);
769 /* videobuf_stop() ?? */
772 mutex_unlock(&cam->lock);
777 static __poll_t viacam_poll(struct file *filp, struct poll_table_struct *pt)
779 struct via_camera *cam = video_drvdata(filp);
781 return videobuf_poll_stream(filp, &cam->vb_queue, pt);
785 static int viacam_mmap(struct file *filp, struct vm_area_struct *vma)
787 struct via_camera *cam = video_drvdata(filp);
789 return videobuf_mmap_mapper(&cam->vb_queue, vma);
794 static const struct v4l2_file_operations viacam_fops = {
795 .owner = THIS_MODULE,
797 .release = viacam_release,
801 .unlocked_ioctl = video_ioctl2,
804 /*----------------------------------------------------------------------------*/
806 * The long list of v4l2 ioctl ops
812 static int viacam_enum_input(struct file *filp, void *priv,
813 struct v4l2_input *input)
815 if (input->index != 0)
818 input->type = V4L2_INPUT_TYPE_CAMERA;
819 input->std = V4L2_STD_ALL; /* Not sure what should go here */
820 strscpy(input->name, "Camera", sizeof(input->name));
824 static int viacam_g_input(struct file *filp, void *priv, unsigned int *i)
830 static int viacam_s_input(struct file *filp, void *priv, unsigned int i)
837 static int viacam_s_std(struct file *filp, void *priv, v4l2_std_id std)
842 static int viacam_g_std(struct file *filp, void *priv, v4l2_std_id *std)
844 *std = V4L2_STD_NTSC_M;
849 * Video format stuff. Here is our default format until
850 * user space messes with things.
852 static const struct v4l2_pix_format viacam_def_pix_format = {
854 .height = VGA_HEIGHT,
855 .pixelformat = V4L2_PIX_FMT_YUYV,
856 .field = V4L2_FIELD_NONE,
857 .bytesperline = VGA_WIDTH * 2,
858 .sizeimage = VGA_WIDTH * VGA_HEIGHT * 2,
861 static const u32 via_def_mbus_code = MEDIA_BUS_FMT_YUYV8_2X8;
863 static int viacam_enum_fmt_vid_cap(struct file *filp, void *priv,
864 struct v4l2_fmtdesc *fmt)
866 if (fmt->index >= N_VIA_FMTS)
868 fmt->pixelformat = via_formats[fmt->index].pixelformat;
873 * Figure out proper image dimensions, but always force the
876 static void viacam_fmt_pre(struct v4l2_pix_format *userfmt,
877 struct v4l2_pix_format *sensorfmt)
879 *sensorfmt = *userfmt;
880 if (userfmt->width < QCIF_WIDTH || userfmt->height < QCIF_HEIGHT) {
881 userfmt->width = QCIF_WIDTH;
882 userfmt->height = QCIF_HEIGHT;
884 if (userfmt->width > VGA_WIDTH || userfmt->height > VGA_HEIGHT) {
885 userfmt->width = VGA_WIDTH;
886 userfmt->height = VGA_HEIGHT;
888 sensorfmt->width = VGA_WIDTH;
889 sensorfmt->height = VGA_HEIGHT;
892 static void viacam_fmt_post(struct v4l2_pix_format *userfmt,
893 struct v4l2_pix_format *sensorfmt)
895 struct via_format *f = via_find_format(userfmt->pixelformat);
897 sensorfmt->bytesperline = sensorfmt->width * f->bpp;
898 sensorfmt->sizeimage = sensorfmt->height * sensorfmt->bytesperline;
899 userfmt->pixelformat = sensorfmt->pixelformat;
900 userfmt->field = sensorfmt->field;
901 userfmt->bytesperline = 2 * userfmt->width;
902 userfmt->sizeimage = userfmt->bytesperline * userfmt->height;
907 * The real work of figuring out a workable format.
909 static int viacam_do_try_fmt(struct via_camera *cam,
910 struct v4l2_pix_format *upix, struct v4l2_pix_format *spix)
913 struct v4l2_subdev_pad_config pad_cfg;
914 struct v4l2_subdev_format format = {
915 .which = V4L2_SUBDEV_FORMAT_TRY,
917 struct via_format *f = via_find_format(upix->pixelformat);
919 upix->pixelformat = f->pixelformat;
920 viacam_fmt_pre(upix, spix);
921 v4l2_fill_mbus_format(&format.format, spix, f->mbus_code);
922 ret = sensor_call(cam, pad, set_fmt, &pad_cfg, &format);
923 v4l2_fill_pix_format(spix, &format.format);
924 viacam_fmt_post(upix, spix);
930 static int viacam_try_fmt_vid_cap(struct file *filp, void *priv,
931 struct v4l2_format *fmt)
933 struct via_camera *cam = video_drvdata(filp);
934 struct v4l2_format sfmt;
937 mutex_lock(&cam->lock);
938 ret = viacam_do_try_fmt(cam, &fmt->fmt.pix, &sfmt.fmt.pix);
939 mutex_unlock(&cam->lock);
944 static int viacam_g_fmt_vid_cap(struct file *filp, void *priv,
945 struct v4l2_format *fmt)
947 struct via_camera *cam = video_drvdata(filp);
949 mutex_lock(&cam->lock);
950 fmt->fmt.pix = cam->user_format;
951 mutex_unlock(&cam->lock);
955 static int viacam_s_fmt_vid_cap(struct file *filp, void *priv,
956 struct v4l2_format *fmt)
958 struct via_camera *cam = video_drvdata(filp);
960 struct v4l2_format sfmt;
961 struct via_format *f = via_find_format(fmt->fmt.pix.pixelformat);
964 * Camera must be idle or we can't mess with the
967 mutex_lock(&cam->lock);
968 if (cam->opstate != S_IDLE) {
973 * Let the sensor code look over and tweak the
974 * requested formatting.
976 ret = viacam_do_try_fmt(cam, &fmt->fmt.pix, &sfmt.fmt.pix);
980 * OK, let's commit to the new format.
982 cam->user_format = fmt->fmt.pix;
983 cam->sensor_format = sfmt.fmt.pix;
984 cam->mbus_code = f->mbus_code;
985 ret = viacam_configure_sensor(cam);
987 ret = viacam_config_controller(cam);
989 mutex_unlock(&cam->lock);
993 static int viacam_querycap(struct file *filp, void *priv,
994 struct v4l2_capability *cap)
996 strscpy(cap->driver, "via-camera", sizeof(cap->driver));
997 strscpy(cap->card, "via-camera", sizeof(cap->card));
1002 * Streaming operations - pure videobuf stuff.
1004 static int viacam_reqbufs(struct file *filp, void *priv,
1005 struct v4l2_requestbuffers *rb)
1007 struct via_camera *cam = video_drvdata(filp);
1009 return videobuf_reqbufs(&cam->vb_queue, rb);
1012 static int viacam_querybuf(struct file *filp, void *priv,
1013 struct v4l2_buffer *buf)
1015 struct via_camera *cam = video_drvdata(filp);
1017 return videobuf_querybuf(&cam->vb_queue, buf);
1020 static int viacam_qbuf(struct file *filp, void *priv, struct v4l2_buffer *buf)
1022 struct via_camera *cam = video_drvdata(filp);
1024 return videobuf_qbuf(&cam->vb_queue, buf);
1027 static int viacam_dqbuf(struct file *filp, void *priv, struct v4l2_buffer *buf)
1029 struct via_camera *cam = video_drvdata(filp);
1031 return videobuf_dqbuf(&cam->vb_queue, buf, filp->f_flags & O_NONBLOCK);
1034 static int viacam_streamon(struct file *filp, void *priv, enum v4l2_buf_type t)
1036 struct via_camera *cam = video_drvdata(filp);
1039 if (t != V4L2_BUF_TYPE_VIDEO_CAPTURE)
1042 mutex_lock(&cam->lock);
1043 if (cam->opstate != S_IDLE) {
1048 * Enforce the V4l2 "only one owner gets to read data" rule.
1050 if (cam->owner && cam->owner != filp) {
1056 * Configure things if need be.
1058 if (test_bit(CF_CONFIG_NEEDED, &cam->flags)) {
1059 ret = viacam_configure_sensor(cam);
1062 ret = viacam_config_controller(cam);
1067 * If the CPU goes into C3, the DMA transfer gets corrupted and
1068 * users start filing unsightly bug reports. Put in a "latency"
1069 * requirement which will keep the CPU out of the deeper sleep
1072 pm_qos_add_request(&cam->qos_request, PM_QOS_CPU_DMA_LATENCY, 50);
1076 INIT_LIST_HEAD(&cam->buffer_queue);
1077 ret = videobuf_streamon(&cam->vb_queue);
1079 viacam_start_engine(cam);
1081 mutex_unlock(&cam->lock);
1085 static int viacam_streamoff(struct file *filp, void *priv, enum v4l2_buf_type t)
1087 struct via_camera *cam = video_drvdata(filp);
1090 if (t != V4L2_BUF_TYPE_VIDEO_CAPTURE)
1092 mutex_lock(&cam->lock);
1093 if (cam->opstate != S_RUNNING) {
1097 pm_qos_remove_request(&cam->qos_request);
1098 viacam_stop_engine(cam);
1100 * Videobuf will recycle all of the outstanding buffers, but
1101 * we should be sure we don't retain any references to
1104 ret = videobuf_streamoff(&cam->vb_queue);
1105 INIT_LIST_HEAD(&cam->buffer_queue);
1107 mutex_unlock(&cam->lock);
1113 static int viacam_g_parm(struct file *filp, void *priv,
1114 struct v4l2_streamparm *parm)
1116 struct via_camera *cam = video_drvdata(filp);
1119 mutex_lock(&cam->lock);
1120 ret = v4l2_g_parm_cap(video_devdata(filp), cam->sensor, parm);
1121 mutex_unlock(&cam->lock);
1122 parm->parm.capture.readbuffers = cam->n_cap_bufs;
1126 static int viacam_s_parm(struct file *filp, void *priv,
1127 struct v4l2_streamparm *parm)
1129 struct via_camera *cam = video_drvdata(filp);
1132 mutex_lock(&cam->lock);
1133 ret = v4l2_s_parm_cap(video_devdata(filp), cam->sensor, parm);
1134 mutex_unlock(&cam->lock);
1135 parm->parm.capture.readbuffers = cam->n_cap_bufs;
1139 static int viacam_enum_framesizes(struct file *filp, void *priv,
1140 struct v4l2_frmsizeenum *sizes)
1142 if (sizes->index != 0)
1144 sizes->type = V4L2_FRMSIZE_TYPE_CONTINUOUS;
1145 sizes->stepwise.min_width = QCIF_WIDTH;
1146 sizes->stepwise.min_height = QCIF_HEIGHT;
1147 sizes->stepwise.max_width = VGA_WIDTH;
1148 sizes->stepwise.max_height = VGA_HEIGHT;
1149 sizes->stepwise.step_width = sizes->stepwise.step_height = 1;
1153 static int viacam_enum_frameintervals(struct file *filp, void *priv,
1154 struct v4l2_frmivalenum *interval)
1156 struct via_camera *cam = video_drvdata(filp);
1157 struct v4l2_subdev_frame_interval_enum fie = {
1158 .index = interval->index,
1159 .code = cam->mbus_code,
1160 .width = cam->sensor_format.width,
1161 .height = cam->sensor_format.height,
1162 .which = V4L2_SUBDEV_FORMAT_ACTIVE,
1166 mutex_lock(&cam->lock);
1167 ret = sensor_call(cam, pad, enum_frame_interval, NULL, &fie);
1168 mutex_unlock(&cam->lock);
1171 interval->type = V4L2_FRMIVAL_TYPE_DISCRETE;
1172 interval->discrete = fie.interval;
1178 static const struct v4l2_ioctl_ops viacam_ioctl_ops = {
1179 .vidioc_enum_input = viacam_enum_input,
1180 .vidioc_g_input = viacam_g_input,
1181 .vidioc_s_input = viacam_s_input,
1182 .vidioc_s_std = viacam_s_std,
1183 .vidioc_g_std = viacam_g_std,
1184 .vidioc_enum_fmt_vid_cap = viacam_enum_fmt_vid_cap,
1185 .vidioc_try_fmt_vid_cap = viacam_try_fmt_vid_cap,
1186 .vidioc_g_fmt_vid_cap = viacam_g_fmt_vid_cap,
1187 .vidioc_s_fmt_vid_cap = viacam_s_fmt_vid_cap,
1188 .vidioc_querycap = viacam_querycap,
1189 .vidioc_reqbufs = viacam_reqbufs,
1190 .vidioc_querybuf = viacam_querybuf,
1191 .vidioc_qbuf = viacam_qbuf,
1192 .vidioc_dqbuf = viacam_dqbuf,
1193 .vidioc_streamon = viacam_streamon,
1194 .vidioc_streamoff = viacam_streamoff,
1195 .vidioc_g_parm = viacam_g_parm,
1196 .vidioc_s_parm = viacam_s_parm,
1197 .vidioc_enum_framesizes = viacam_enum_framesizes,
1198 .vidioc_enum_frameintervals = viacam_enum_frameintervals,
1201 /*----------------------------------------------------------------------------*/
1208 static int viacam_suspend(void *priv)
1210 struct via_camera *cam = priv;
1211 enum viacam_opstate state = cam->opstate;
1213 if (cam->opstate != S_IDLE) {
1214 viacam_stop_engine(cam);
1215 cam->opstate = state; /* So resume restarts */
1221 static int viacam_resume(void *priv)
1223 struct via_camera *cam = priv;
1227 * Get back to a reasonable operating state.
1229 via_write_reg_mask(VIASR, 0x78, 0, 0x80);
1230 via_write_reg_mask(VIASR, 0x1e, 0xc0, 0xc0);
1231 viacam_int_disable(cam);
1232 set_bit(CF_CONFIG_NEEDED, &cam->flags);
1234 * Make sure the sensor's power state is correct
1237 via_sensor_power_up(cam);
1239 via_sensor_power_down(cam);
1241 * If it was operating, try to restart it.
1243 if (cam->opstate != S_IDLE) {
1244 mutex_lock(&cam->lock);
1245 ret = viacam_configure_sensor(cam);
1247 ret = viacam_config_controller(cam);
1248 mutex_unlock(&cam->lock);
1250 viacam_start_engine(cam);
1256 static struct viafb_pm_hooks viacam_pm_hooks = {
1257 .suspend = viacam_suspend,
1258 .resume = viacam_resume
1261 #endif /* CONFIG_PM */
1267 static const struct video_device viacam_v4l_template = {
1268 .name = "via-camera",
1270 .tvnorms = V4L2_STD_NTSC_M,
1271 .fops = &viacam_fops,
1272 .ioctl_ops = &viacam_ioctl_ops,
1273 .release = video_device_release_empty, /* Check this */
1274 .device_caps = V4L2_CAP_VIDEO_CAPTURE | V4L2_CAP_READWRITE |
1279 * The OLPC folks put the serial port on the same pin as
1280 * the camera. They also get grumpy if we break the
1281 * serial port and keep them from using it. So we have
1282 * to check the serial enable bit and not step on it.
1284 #define VIACAM_SERIAL_DEVFN 0x88
1285 #define VIACAM_SERIAL_CREG 0x46
1286 #define VIACAM_SERIAL_BIT 0x40
1288 static bool viacam_serial_is_enabled(void)
1290 struct pci_bus *pbus = pci_find_bus(0, 0);
1295 pci_bus_read_config_byte(pbus, VIACAM_SERIAL_DEVFN,
1296 VIACAM_SERIAL_CREG, &cbyte);
1297 if ((cbyte & VIACAM_SERIAL_BIT) == 0)
1298 return false; /* Not enabled */
1299 if (!override_serial) {
1300 printk(KERN_NOTICE "Via camera: serial port is enabled, " \
1301 "refusing to load.\n");
1302 printk(KERN_NOTICE "Specify override_serial=1 to force " \
1303 "module loading.\n");
1306 printk(KERN_NOTICE "Via camera: overriding serial port\n");
1307 pci_bus_write_config_byte(pbus, VIACAM_SERIAL_DEVFN,
1308 VIACAM_SERIAL_CREG, cbyte & ~VIACAM_SERIAL_BIT);
1312 static struct ov7670_config sensor_cfg = {
1313 /* The XO-1.5 (only known user) clocks the camera at 90MHz. */
1317 static int viacam_probe(struct platform_device *pdev)
1320 struct i2c_adapter *sensor_adapter;
1321 struct viafb_dev *viadev = pdev->dev.platform_data;
1322 struct i2c_board_info ov7670_info = {
1325 .platform_data = &sensor_cfg,
1329 * Note that there are actually two capture channels on
1330 * the device. We only deal with one for now. That
1331 * is encoded here; nothing else assumes it's dealing with
1332 * a unique capture device.
1334 struct via_camera *cam;
1337 * Ensure that frame buffer memory has been set aside for
1338 * this purpose. As an arbitrary limit, refuse to work
1339 * with less than two frames of VGA 16-bit data.
1341 * If we ever support the second port, we'll need to set
1342 * aside more memory.
1344 if (viadev->camera_fbmem_size < (VGA_HEIGHT*VGA_WIDTH*4)) {
1345 printk(KERN_ERR "viacam: insufficient FB memory reserved\n");
1348 if (viadev->engine_mmio == NULL) {
1349 printk(KERN_ERR "viacam: No I/O memory, so no pictures\n");
1353 if (machine_is_olpc() && viacam_serial_is_enabled())
1357 * Basic structure initialization.
1359 cam = kzalloc (sizeof(struct via_camera), GFP_KERNEL);
1363 cam->platdev = pdev;
1364 cam->viadev = viadev;
1367 cam->opstate = S_IDLE;
1368 cam->user_format = cam->sensor_format = viacam_def_pix_format;
1369 mutex_init(&cam->lock);
1370 INIT_LIST_HEAD(&cam->buffer_queue);
1371 cam->mmio = viadev->engine_mmio;
1372 cam->fbmem = viadev->fbmem;
1373 cam->fb_offset = viadev->camera_fbmem_offset;
1374 cam->flags = 1 << CF_CONFIG_NEEDED;
1375 cam->mbus_code = via_def_mbus_code;
1377 * Tell V4L that we exist.
1379 ret = v4l2_device_register(&pdev->dev, &cam->v4l2_dev);
1381 dev_err(&pdev->dev, "Unable to register v4l2 device\n");
1384 ret = v4l2_ctrl_handler_init(&cam->ctrl_handler, 10);
1386 goto out_unregister;
1387 cam->v4l2_dev.ctrl_handler = &cam->ctrl_handler;
1389 * Convince the system that we can do DMA.
1391 pdev->dev.dma_mask = &viadev->pdev->dma_mask;
1392 dma_set_mask(&pdev->dev, 0xffffffff);
1394 * Fire up the capture port. The write to 0x78 looks purely
1395 * OLPCish; any system will need to tweak 0x1e.
1397 via_write_reg_mask(VIASR, 0x78, 0, 0x80);
1398 via_write_reg_mask(VIASR, 0x1e, 0xc0, 0xc0);
1400 * Get the sensor powered up.
1402 ret = via_sensor_power_setup(cam);
1404 goto out_ctrl_hdl_free;
1405 via_sensor_power_up(cam);
1408 * See if we can't find it on the bus. The VIA_PORT_31 assumption
1409 * is OLPC-specific. 0x42 assumption is ov7670-specific.
1411 sensor_adapter = viafb_find_i2c_adapter(VIA_PORT_31);
1412 cam->sensor = v4l2_i2c_new_subdev_board(&cam->v4l2_dev, sensor_adapter,
1413 &ov7670_info, NULL);
1414 if (cam->sensor == NULL) {
1415 dev_err(&pdev->dev, "Unable to find the sensor!\n");
1417 goto out_power_down;
1422 viacam_int_disable(cam);
1423 ret = request_threaded_irq(viadev->pdev->irq, viacam_quick_irq,
1424 viacam_irq, IRQF_SHARED, "via-camera", cam);
1426 goto out_power_down;
1428 * Tell V4l2 that we exist.
1430 cam->vdev = viacam_v4l_template;
1431 cam->vdev.v4l2_dev = &cam->v4l2_dev;
1432 video_set_drvdata(&cam->vdev, cam);
1433 ret = video_register_device(&cam->vdev, VFL_TYPE_GRABBER, -1);
1439 * Hook into PM events
1441 viacam_pm_hooks.private = cam;
1442 viafb_pm_register(&viacam_pm_hooks);
1445 /* Power the sensor down until somebody opens the device */
1446 via_sensor_power_down(cam);
1450 free_irq(viadev->pdev->irq, cam);
1452 via_sensor_power_release(cam);
1454 v4l2_ctrl_handler_free(&cam->ctrl_handler);
1456 v4l2_device_unregister(&cam->v4l2_dev);
1462 static int viacam_remove(struct platform_device *pdev)
1464 struct via_camera *cam = via_cam_info;
1465 struct viafb_dev *viadev = pdev->dev.platform_data;
1467 video_unregister_device(&cam->vdev);
1468 v4l2_device_unregister(&cam->v4l2_dev);
1470 viafb_pm_unregister(&viacam_pm_hooks);
1472 free_irq(viadev->pdev->irq, cam);
1473 via_sensor_power_release(cam);
1474 v4l2_ctrl_handler_free(&cam->ctrl_handler);
1476 via_cam_info = NULL;
1480 static struct platform_driver viacam_driver = {
1482 .name = "viafb-camera",
1484 .probe = viacam_probe,
1485 .remove = viacam_remove,
1488 module_platform_driver(viacam_driver);