Linux 4.16.11
[linux/fpc-iii.git] / drivers / media / platform / via-camera.c
blobf77be930212087e9a87d4a690a0edae2cc4f685e
1 /*
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
8 */
9 #include <linux/kernel.h>
10 #include <linux/module.h>
11 #include <linux/device.h>
12 #include <linux/list.h>
13 #include <linux/pci.h>
14 #include <linux/gpio.h>
15 #include <linux/interrupt.h>
16 #include <linux/platform_device.h>
17 #include <linux/videodev2.h>
18 #include <media/v4l2-device.h>
19 #include <media/v4l2-ioctl.h>
20 #include <media/v4l2-ctrls.h>
21 #include <media/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>
30 #include <asm/olpc.h>
32 #include "via-camera.h"
34 MODULE_ALIAS("platform:viafb-camera");
35 MODULE_AUTHOR("Jonathan Corbet <corbet@lwn.net>");
36 MODULE_DESCRIPTION("VIA framebuffer-based camera controller driver");
37 MODULE_LICENSE("GPL");
39 static bool flip_image;
40 module_param(flip_image, bool, 0444);
41 MODULE_PARM_DESC(flip_image,
42 "If set, the sensor will be instructed to flip the image vertically.");
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 the XO 1.5 serial port is enabled. Set this option to force-enable the camera.");
50 * The structure describing our camera.
52 enum viacam_opstate { S_IDLE = 0, S_RUNNING = 1 };
54 struct via_camera {
55 struct v4l2_device v4l2_dev;
56 struct v4l2_ctrl_handler ctrl_handler;
57 struct video_device vdev;
58 struct v4l2_subdev *sensor;
59 struct platform_device *platdev;
60 struct viafb_dev *viadev;
61 struct mutex lock;
62 enum viacam_opstate opstate;
63 unsigned long flags;
64 struct pm_qos_request qos_request;
66 * GPIO info for power/reset management
68 int power_gpio;
69 int reset_gpio;
71 * I/O memory stuff.
73 void __iomem *mmio; /* Where the registers live */
74 void __iomem *fbmem; /* Frame buffer memory */
75 u32 fb_offset; /* Reserved memory offset (FB) */
77 * Capture buffers and related. The controller supports
78 * up to three, so that's what we have here. These buffers
79 * live in frame buffer memory, so we don't call them "DMA".
81 unsigned int cb_offsets[3]; /* offsets into fb mem */
82 u8 __iomem *cb_addrs[3]; /* Kernel-space addresses */
83 int n_cap_bufs; /* How many are we using? */
84 int next_buf;
85 struct videobuf_queue vb_queue;
86 struct list_head buffer_queue; /* prot. by reg_lock */
88 * User tracking.
90 int users;
91 struct file *owner;
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;
101 u32 mbus_code;
105 * Yes, this is a hack, but there's only going to be one of these
106 * on any system we know of.
108 static struct via_camera *via_cam_info;
111 * Flag values, manipulated with bitops
113 #define CF_DMA_ACTIVE 0 /* A frame is incoming */
114 #define CF_CONFIG_NEEDED 1 /* Must configure hardware */
118 * Nasty ugly v4l2 boilerplate.
120 #define sensor_call(cam, optype, func, args...) \
121 v4l2_subdev_call(cam->sensor, optype, func, ##args)
124 * Debugging and related.
126 #define cam_err(cam, fmt, arg...) \
127 dev_err(&(cam)->platdev->dev, fmt, ##arg);
128 #define cam_warn(cam, fmt, arg...) \
129 dev_warn(&(cam)->platdev->dev, fmt, ##arg);
130 #define cam_dbg(cam, fmt, arg...) \
131 dev_dbg(&(cam)->platdev->dev, fmt, ##arg);
134 * Format handling. This is ripped almost directly from Hans's changes
135 * to cafe_ccic.c. It's a little unfortunate; until this change, we
136 * didn't need to know anything about the format except its byte depth;
137 * now this information must be managed at this level too.
139 static struct via_format {
140 __u8 *desc;
141 __u32 pixelformat;
142 int bpp; /* Bytes per pixel */
143 u32 mbus_code;
144 } via_formats[] = {
146 .desc = "YUYV 4:2:2",
147 .pixelformat = V4L2_PIX_FMT_YUYV,
148 .mbus_code = MEDIA_BUS_FMT_YUYV8_2X8,
149 .bpp = 2,
151 /* RGB444 and Bayer should be doable, but have never been
152 tested with this driver. RGB565 seems to work at the default
153 resolution, but results in color corruption when being scaled by
154 viacam_set_scaled(), and is disabled as a result. */
156 #define N_VIA_FMTS ARRAY_SIZE(via_formats)
158 static struct via_format *via_find_format(u32 pixelformat)
160 unsigned i;
162 for (i = 0; i < N_VIA_FMTS; i++)
163 if (via_formats[i].pixelformat == pixelformat)
164 return via_formats + i;
165 /* Not found? Then return the first format. */
166 return via_formats;
170 /*--------------------------------------------------------------------------*/
172 * Sensor power/reset management. This piece is OLPC-specific for
173 * sure; other configurations will have things connected differently.
175 static int via_sensor_power_setup(struct via_camera *cam)
177 int ret;
179 cam->power_gpio = viafb_gpio_lookup("VGPIO3");
180 cam->reset_gpio = viafb_gpio_lookup("VGPIO2");
181 if (cam->power_gpio < 0 || cam->reset_gpio < 0) {
182 dev_err(&cam->platdev->dev, "Unable to find GPIO lines\n");
183 return -EINVAL;
185 ret = gpio_request(cam->power_gpio, "viafb-camera");
186 if (ret) {
187 dev_err(&cam->platdev->dev, "Unable to request power GPIO\n");
188 return ret;
190 ret = gpio_request(cam->reset_gpio, "viafb-camera");
191 if (ret) {
192 dev_err(&cam->platdev->dev, "Unable to request reset GPIO\n");
193 gpio_free(cam->power_gpio);
194 return ret;
196 gpio_direction_output(cam->power_gpio, 0);
197 gpio_direction_output(cam->reset_gpio, 0);
198 return 0;
202 * Power up the sensor and perform the reset dance.
204 static void via_sensor_power_up(struct via_camera *cam)
206 gpio_set_value(cam->power_gpio, 1);
207 gpio_set_value(cam->reset_gpio, 0);
208 msleep(20); /* Probably excessive */
209 gpio_set_value(cam->reset_gpio, 1);
210 msleep(20);
213 static void via_sensor_power_down(struct via_camera *cam)
215 gpio_set_value(cam->power_gpio, 0);
216 gpio_set_value(cam->reset_gpio, 0);
220 static void via_sensor_power_release(struct via_camera *cam)
222 via_sensor_power_down(cam);
223 gpio_free(cam->power_gpio);
224 gpio_free(cam->reset_gpio);
227 /* --------------------------------------------------------------------------*/
228 /* Sensor ops */
231 * Manage the ov7670 "flip" bit, which needs special help.
233 static int viacam_set_flip(struct via_camera *cam)
235 struct v4l2_control ctrl;
237 memset(&ctrl, 0, sizeof(ctrl));
238 ctrl.id = V4L2_CID_VFLIP;
239 ctrl.value = flip_image;
240 return v4l2_s_ctrl(NULL, cam->sensor->ctrl_handler, &ctrl);
244 * Configure the sensor. It's up to the caller to ensure
245 * that the camera is in the correct operating state.
247 static int viacam_configure_sensor(struct via_camera *cam)
249 struct v4l2_subdev_format format = {
250 .which = V4L2_SUBDEV_FORMAT_ACTIVE,
252 int ret;
254 v4l2_fill_mbus_format(&format.format, &cam->sensor_format, cam->mbus_code);
255 ret = sensor_call(cam, core, init, 0);
256 if (ret == 0)
257 ret = sensor_call(cam, pad, set_fmt, NULL, &format);
259 * OV7670 does weird things if flip is set *before* format...
261 if (ret == 0)
262 ret = viacam_set_flip(cam);
263 return ret;
268 /* --------------------------------------------------------------------------*/
270 * Some simple register accessors; they assume that the lock is held.
272 * Should we want to support the second capture engine, we could
273 * hide the register difference by adding 0x1000 to registers in the
274 * 0x300-350 range.
276 static inline void viacam_write_reg(struct via_camera *cam,
277 int reg, int value)
279 iowrite32(value, cam->mmio + reg);
282 static inline int viacam_read_reg(struct via_camera *cam, int reg)
284 return ioread32(cam->mmio + reg);
287 static inline void viacam_write_reg_mask(struct via_camera *cam,
288 int reg, int value, int mask)
290 int tmp = viacam_read_reg(cam, reg);
292 tmp = (tmp & ~mask) | (value & mask);
293 viacam_write_reg(cam, reg, tmp);
297 /* --------------------------------------------------------------------------*/
298 /* Interrupt management and handling */
300 static irqreturn_t viacam_quick_irq(int irq, void *data)
302 struct via_camera *cam = data;
303 irqreturn_t ret = IRQ_NONE;
304 int icv;
307 * All we do here is to clear the interrupts and tell
308 * the handler thread to wake up.
310 spin_lock(&cam->viadev->reg_lock);
311 icv = viacam_read_reg(cam, VCR_INTCTRL);
312 if (icv & VCR_IC_EAV) {
313 icv |= VCR_IC_EAV|VCR_IC_EVBI|VCR_IC_FFULL;
314 viacam_write_reg(cam, VCR_INTCTRL, icv);
315 ret = IRQ_WAKE_THREAD;
317 spin_unlock(&cam->viadev->reg_lock);
318 return ret;
322 * Find the next videobuf buffer which has somebody waiting on it.
324 static struct videobuf_buffer *viacam_next_buffer(struct via_camera *cam)
326 unsigned long flags;
327 struct videobuf_buffer *buf = NULL;
329 spin_lock_irqsave(&cam->viadev->reg_lock, flags);
330 if (cam->opstate != S_RUNNING)
331 goto out;
332 if (list_empty(&cam->buffer_queue))
333 goto out;
334 buf = list_entry(cam->buffer_queue.next, struct videobuf_buffer, queue);
335 if (!waitqueue_active(&buf->done)) {/* Nobody waiting */
336 buf = NULL;
337 goto out;
339 list_del(&buf->queue);
340 buf->state = VIDEOBUF_ACTIVE;
341 out:
342 spin_unlock_irqrestore(&cam->viadev->reg_lock, flags);
343 return buf;
347 * The threaded IRQ handler.
349 static irqreturn_t viacam_irq(int irq, void *data)
351 int bufn;
352 struct videobuf_buffer *vb;
353 struct via_camera *cam = data;
354 struct videobuf_dmabuf *vdma;
357 * If there is no place to put the data frame, don't bother
358 * with anything else.
360 vb = viacam_next_buffer(cam);
361 if (vb == NULL)
362 goto done;
364 * Figure out which buffer we just completed.
366 bufn = (viacam_read_reg(cam, VCR_INTCTRL) & VCR_IC_ACTBUF) >> 3;
367 bufn -= 1;
368 if (bufn < 0)
369 bufn = cam->n_cap_bufs - 1;
371 * Copy over the data and let any waiters know.
373 vdma = videobuf_to_dma(vb);
374 viafb_dma_copy_out_sg(cam->cb_offsets[bufn], vdma->sglist, vdma->sglen);
375 vb->state = VIDEOBUF_DONE;
376 vb->size = cam->user_format.sizeimage;
377 wake_up(&vb->done);
378 done:
379 return IRQ_HANDLED;
384 * These functions must mess around with the general interrupt
385 * control register, which is relevant to much more than just the
386 * camera. Nothing else uses interrupts, though, as of this writing.
387 * Should that situation change, we'll have to improve support at
388 * the via-core level.
390 static void viacam_int_enable(struct via_camera *cam)
392 viacam_write_reg(cam, VCR_INTCTRL,
393 VCR_IC_INTEN|VCR_IC_EAV|VCR_IC_EVBI|VCR_IC_FFULL);
394 viafb_irq_enable(VDE_I_C0AVEN);
397 static void viacam_int_disable(struct via_camera *cam)
399 viafb_irq_disable(VDE_I_C0AVEN);
400 viacam_write_reg(cam, VCR_INTCTRL, 0);
405 /* --------------------------------------------------------------------------*/
406 /* Controller operations */
409 * Set up our capture buffers in framebuffer memory.
411 static int viacam_ctlr_cbufs(struct via_camera *cam)
413 int nbuf = cam->viadev->camera_fbmem_size/cam->sensor_format.sizeimage;
414 int i;
415 unsigned int offset;
418 * See how many buffers we can work with.
420 if (nbuf >= 3) {
421 cam->n_cap_bufs = 3;
422 viacam_write_reg_mask(cam, VCR_CAPINTC, VCR_CI_3BUFS,
423 VCR_CI_3BUFS);
424 } else if (nbuf == 2) {
425 cam->n_cap_bufs = 2;
426 viacam_write_reg_mask(cam, VCR_CAPINTC, 0, VCR_CI_3BUFS);
427 } else {
428 cam_warn(cam, "Insufficient frame buffer memory\n");
429 return -ENOMEM;
432 * Set them up.
434 offset = cam->fb_offset;
435 for (i = 0; i < cam->n_cap_bufs; i++) {
436 cam->cb_offsets[i] = offset;
437 cam->cb_addrs[i] = cam->fbmem + offset;
438 viacam_write_reg(cam, VCR_VBUF1 + i*4, offset & VCR_VBUF_MASK);
439 offset += cam->sensor_format.sizeimage;
441 return 0;
445 * Set the scaling register for downscaling the image.
447 * This register works like this... Vertical scaling is enabled
448 * by bit 26; if that bit is set, downscaling is controlled by the
449 * value in bits 16:25. Those bits are divided by 1024 to get
450 * the scaling factor; setting just bit 25 thus cuts the height
451 * in half.
453 * Horizontal scaling works about the same, but it's enabled by
454 * bit 11, with bits 0:10 giving the numerator of a fraction
455 * (over 2048) for the scaling value.
457 * This function is naive in that, if the user departs from
458 * the 3x4 VGA scaling factor, the image will distort. We
459 * could work around that if it really seemed important.
461 static void viacam_set_scale(struct via_camera *cam)
463 unsigned int avscale;
464 int sf;
466 if (cam->user_format.width == VGA_WIDTH)
467 avscale = 0;
468 else {
469 sf = (cam->user_format.width*2048)/VGA_WIDTH;
470 avscale = VCR_AVS_HEN | sf;
472 if (cam->user_format.height < VGA_HEIGHT) {
473 sf = (1024*cam->user_format.height)/VGA_HEIGHT;
474 avscale |= VCR_AVS_VEN | (sf << 16);
476 viacam_write_reg(cam, VCR_AVSCALE, avscale);
481 * Configure image-related information into the capture engine.
483 static void viacam_ctlr_image(struct via_camera *cam)
485 int cicreg;
488 * Disable clock before messing with stuff - from the via
489 * sample driver.
491 viacam_write_reg(cam, VCR_CAPINTC, ~(VCR_CI_ENABLE|VCR_CI_CLKEN));
493 * Set up the controller for VGA resolution, modulo magic
494 * offsets from the via sample driver.
496 viacam_write_reg(cam, VCR_HORRANGE, 0x06200120);
497 viacam_write_reg(cam, VCR_VERTRANGE, 0x01de0000);
498 viacam_set_scale(cam);
500 * Image size info.
502 viacam_write_reg(cam, VCR_MAXDATA,
503 (cam->sensor_format.height << 16) |
504 (cam->sensor_format.bytesperline >> 3));
505 viacam_write_reg(cam, VCR_MAXVBI, 0);
506 viacam_write_reg(cam, VCR_VSTRIDE,
507 cam->user_format.bytesperline & VCR_VS_STRIDE);
509 * Set up the capture interface control register,
510 * everything but the "go" bit.
512 * The FIFO threshold is a bit of a magic number; 8 is what
513 * VIA's sample code uses.
515 cicreg = VCR_CI_CLKEN |
516 0x08000000 | /* FIFO threshold */
517 VCR_CI_FLDINV | /* OLPC-specific? */
518 VCR_CI_VREFINV | /* OLPC-specific? */
519 VCR_CI_DIBOTH | /* Capture both fields */
520 VCR_CI_CCIR601_8;
521 if (cam->n_cap_bufs == 3)
522 cicreg |= VCR_CI_3BUFS;
524 * YUV formats need different byte swapping than RGB.
526 if (cam->user_format.pixelformat == V4L2_PIX_FMT_YUYV)
527 cicreg |= VCR_CI_YUYV;
528 else
529 cicreg |= VCR_CI_UYVY;
530 viacam_write_reg(cam, VCR_CAPINTC, cicreg);
534 static int viacam_config_controller(struct via_camera *cam)
536 int ret;
537 unsigned long flags;
539 spin_lock_irqsave(&cam->viadev->reg_lock, flags);
540 ret = viacam_ctlr_cbufs(cam);
541 if (!ret)
542 viacam_ctlr_image(cam);
543 spin_unlock_irqrestore(&cam->viadev->reg_lock, flags);
544 clear_bit(CF_CONFIG_NEEDED, &cam->flags);
545 return ret;
549 * Make it start grabbing data.
551 static void viacam_start_engine(struct via_camera *cam)
553 spin_lock_irq(&cam->viadev->reg_lock);
554 cam->next_buf = 0;
555 viacam_write_reg_mask(cam, VCR_CAPINTC, VCR_CI_ENABLE, VCR_CI_ENABLE);
556 viacam_int_enable(cam);
557 (void) viacam_read_reg(cam, VCR_CAPINTC); /* Force post */
558 cam->opstate = S_RUNNING;
559 spin_unlock_irq(&cam->viadev->reg_lock);
563 static void viacam_stop_engine(struct via_camera *cam)
565 spin_lock_irq(&cam->viadev->reg_lock);
566 viacam_int_disable(cam);
567 viacam_write_reg_mask(cam, VCR_CAPINTC, 0, VCR_CI_ENABLE);
568 (void) viacam_read_reg(cam, VCR_CAPINTC); /* Force post */
569 cam->opstate = S_IDLE;
570 spin_unlock_irq(&cam->viadev->reg_lock);
574 /* --------------------------------------------------------------------------*/
575 /* Videobuf callback ops */
578 * buffer_setup. The purpose of this one would appear to be to tell
579 * videobuf how big a single image is. It's also evidently up to us
580 * to put some sort of limit on the maximum number of buffers allowed.
582 static int viacam_vb_buf_setup(struct videobuf_queue *q,
583 unsigned int *count, unsigned int *size)
585 struct via_camera *cam = q->priv_data;
587 *size = cam->user_format.sizeimage;
588 if (*count == 0 || *count > 6) /* Arbitrary number */
589 *count = 6;
590 return 0;
594 * Prepare a buffer.
596 static int viacam_vb_buf_prepare(struct videobuf_queue *q,
597 struct videobuf_buffer *vb, enum v4l2_field field)
599 struct via_camera *cam = q->priv_data;
601 vb->size = cam->user_format.sizeimage;
602 vb->width = cam->user_format.width; /* bytesperline???? */
603 vb->height = cam->user_format.height;
604 vb->field = field;
605 if (vb->state == VIDEOBUF_NEEDS_INIT) {
606 int ret = videobuf_iolock(q, vb, NULL);
607 if (ret)
608 return ret;
610 vb->state = VIDEOBUF_PREPARED;
611 return 0;
615 * We've got a buffer to put data into.
617 * FIXME: check for a running engine and valid buffers?
619 static void viacam_vb_buf_queue(struct videobuf_queue *q,
620 struct videobuf_buffer *vb)
622 struct via_camera *cam = q->priv_data;
625 * Note that videobuf holds the lock when it calls
626 * us, so we need not (indeed, cannot) take it here.
628 vb->state = VIDEOBUF_QUEUED;
629 list_add_tail(&vb->queue, &cam->buffer_queue);
633 * Free a buffer.
635 static void viacam_vb_buf_release(struct videobuf_queue *q,
636 struct videobuf_buffer *vb)
638 struct via_camera *cam = q->priv_data;
640 videobuf_dma_unmap(&cam->platdev->dev, videobuf_to_dma(vb));
641 videobuf_dma_free(videobuf_to_dma(vb));
642 vb->state = VIDEOBUF_NEEDS_INIT;
645 static const struct videobuf_queue_ops viacam_vb_ops = {
646 .buf_setup = viacam_vb_buf_setup,
647 .buf_prepare = viacam_vb_buf_prepare,
648 .buf_queue = viacam_vb_buf_queue,
649 .buf_release = viacam_vb_buf_release,
652 /* --------------------------------------------------------------------------*/
653 /* File operations */
655 static int viacam_open(struct file *filp)
657 struct via_camera *cam = video_drvdata(filp);
659 filp->private_data = cam;
661 * Note the new user. If this is the first one, we'll also
662 * need to power up the sensor.
664 mutex_lock(&cam->lock);
665 if (cam->users == 0) {
666 int ret = viafb_request_dma();
668 if (ret) {
669 mutex_unlock(&cam->lock);
670 return ret;
672 via_sensor_power_up(cam);
673 set_bit(CF_CONFIG_NEEDED, &cam->flags);
675 * Hook into videobuf. Evidently this cannot fail.
677 videobuf_queue_sg_init(&cam->vb_queue, &viacam_vb_ops,
678 &cam->platdev->dev, &cam->viadev->reg_lock,
679 V4L2_BUF_TYPE_VIDEO_CAPTURE, V4L2_FIELD_NONE,
680 sizeof(struct videobuf_buffer), cam, NULL);
682 (cam->users)++;
683 mutex_unlock(&cam->lock);
684 return 0;
687 static int viacam_release(struct file *filp)
689 struct via_camera *cam = video_drvdata(filp);
691 mutex_lock(&cam->lock);
692 (cam->users)--;
694 * If the "owner" is closing, shut down any ongoing
695 * operations.
697 if (filp == cam->owner) {
698 videobuf_stop(&cam->vb_queue);
700 * We don't hold the spinlock here, but, if release()
701 * is being called by the owner, nobody else will
702 * be changing the state. And an extra stop would
703 * not hurt anyway.
705 if (cam->opstate != S_IDLE)
706 viacam_stop_engine(cam);
707 cam->owner = NULL;
710 * Last one out needs to turn out the lights.
712 if (cam->users == 0) {
713 videobuf_mmap_free(&cam->vb_queue);
714 via_sensor_power_down(cam);
715 viafb_release_dma();
717 mutex_unlock(&cam->lock);
718 return 0;
722 * Read a frame from the device.
724 static ssize_t viacam_read(struct file *filp, char __user *buffer,
725 size_t len, loff_t *pos)
727 struct via_camera *cam = video_drvdata(filp);
728 int ret;
730 mutex_lock(&cam->lock);
732 * Enforce the V4l2 "only one owner gets to read data" rule.
734 if (cam->owner && cam->owner != filp) {
735 ret = -EBUSY;
736 goto out_unlock;
738 cam->owner = filp;
740 * Do we need to configure the hardware?
742 if (test_bit(CF_CONFIG_NEEDED, &cam->flags)) {
743 ret = viacam_configure_sensor(cam);
744 if (!ret)
745 ret = viacam_config_controller(cam);
746 if (ret)
747 goto out_unlock;
750 * Fire up the capture engine, then have videobuf do
751 * the heavy lifting. Someday it would be good to avoid
752 * stopping and restarting the engine each time.
754 INIT_LIST_HEAD(&cam->buffer_queue);
755 viacam_start_engine(cam);
756 ret = videobuf_read_stream(&cam->vb_queue, buffer, len, pos, 0,
757 filp->f_flags & O_NONBLOCK);
758 viacam_stop_engine(cam);
759 /* videobuf_stop() ?? */
761 out_unlock:
762 mutex_unlock(&cam->lock);
763 return ret;
767 static __poll_t viacam_poll(struct file *filp, struct poll_table_struct *pt)
769 struct via_camera *cam = video_drvdata(filp);
771 return videobuf_poll_stream(filp, &cam->vb_queue, pt);
775 static int viacam_mmap(struct file *filp, struct vm_area_struct *vma)
777 struct via_camera *cam = video_drvdata(filp);
779 return videobuf_mmap_mapper(&cam->vb_queue, vma);
784 static const struct v4l2_file_operations viacam_fops = {
785 .owner = THIS_MODULE,
786 .open = viacam_open,
787 .release = viacam_release,
788 .read = viacam_read,
789 .poll = viacam_poll,
790 .mmap = viacam_mmap,
791 .unlocked_ioctl = video_ioctl2,
794 /*----------------------------------------------------------------------------*/
796 * The long list of v4l2 ioctl ops
800 * Only one input.
802 static int viacam_enum_input(struct file *filp, void *priv,
803 struct v4l2_input *input)
805 if (input->index != 0)
806 return -EINVAL;
808 input->type = V4L2_INPUT_TYPE_CAMERA;
809 input->std = V4L2_STD_ALL; /* Not sure what should go here */
810 strcpy(input->name, "Camera");
811 return 0;
814 static int viacam_g_input(struct file *filp, void *priv, unsigned int *i)
816 *i = 0;
817 return 0;
820 static int viacam_s_input(struct file *filp, void *priv, unsigned int i)
822 if (i != 0)
823 return -EINVAL;
824 return 0;
827 static int viacam_s_std(struct file *filp, void *priv, v4l2_std_id std)
829 return 0;
832 static int viacam_g_std(struct file *filp, void *priv, v4l2_std_id *std)
834 *std = V4L2_STD_NTSC_M;
835 return 0;
839 * Video format stuff. Here is our default format until
840 * user space messes with things.
842 static const struct v4l2_pix_format viacam_def_pix_format = {
843 .width = VGA_WIDTH,
844 .height = VGA_HEIGHT,
845 .pixelformat = V4L2_PIX_FMT_YUYV,
846 .field = V4L2_FIELD_NONE,
847 .bytesperline = VGA_WIDTH * 2,
848 .sizeimage = VGA_WIDTH * VGA_HEIGHT * 2,
851 static const u32 via_def_mbus_code = MEDIA_BUS_FMT_YUYV8_2X8;
853 static int viacam_enum_fmt_vid_cap(struct file *filp, void *priv,
854 struct v4l2_fmtdesc *fmt)
856 if (fmt->index >= N_VIA_FMTS)
857 return -EINVAL;
858 strlcpy(fmt->description, via_formats[fmt->index].desc,
859 sizeof(fmt->description));
860 fmt->pixelformat = via_formats[fmt->index].pixelformat;
861 return 0;
865 * Figure out proper image dimensions, but always force the
866 * sensor to VGA.
868 static void viacam_fmt_pre(struct v4l2_pix_format *userfmt,
869 struct v4l2_pix_format *sensorfmt)
871 *sensorfmt = *userfmt;
872 if (userfmt->width < QCIF_WIDTH || userfmt->height < QCIF_HEIGHT) {
873 userfmt->width = QCIF_WIDTH;
874 userfmt->height = QCIF_HEIGHT;
876 if (userfmt->width > VGA_WIDTH || userfmt->height > VGA_HEIGHT) {
877 userfmt->width = VGA_WIDTH;
878 userfmt->height = VGA_HEIGHT;
880 sensorfmt->width = VGA_WIDTH;
881 sensorfmt->height = VGA_HEIGHT;
884 static void viacam_fmt_post(struct v4l2_pix_format *userfmt,
885 struct v4l2_pix_format *sensorfmt)
887 struct via_format *f = via_find_format(userfmt->pixelformat);
889 sensorfmt->bytesperline = sensorfmt->width * f->bpp;
890 sensorfmt->sizeimage = sensorfmt->height * sensorfmt->bytesperline;
891 userfmt->pixelformat = sensorfmt->pixelformat;
892 userfmt->field = sensorfmt->field;
893 userfmt->bytesperline = 2 * userfmt->width;
894 userfmt->sizeimage = userfmt->bytesperline * userfmt->height;
899 * The real work of figuring out a workable format.
901 static int viacam_do_try_fmt(struct via_camera *cam,
902 struct v4l2_pix_format *upix, struct v4l2_pix_format *spix)
904 int ret;
905 struct v4l2_subdev_pad_config pad_cfg;
906 struct v4l2_subdev_format format = {
907 .which = V4L2_SUBDEV_FORMAT_TRY,
909 struct via_format *f = via_find_format(upix->pixelformat);
911 upix->pixelformat = f->pixelformat;
912 viacam_fmt_pre(upix, spix);
913 v4l2_fill_mbus_format(&format.format, spix, f->mbus_code);
914 ret = sensor_call(cam, pad, set_fmt, &pad_cfg, &format);
915 v4l2_fill_pix_format(spix, &format.format);
916 viacam_fmt_post(upix, spix);
917 return ret;
922 static int viacam_try_fmt_vid_cap(struct file *filp, void *priv,
923 struct v4l2_format *fmt)
925 struct via_camera *cam = priv;
926 struct v4l2_format sfmt;
927 int ret;
929 mutex_lock(&cam->lock);
930 ret = viacam_do_try_fmt(cam, &fmt->fmt.pix, &sfmt.fmt.pix);
931 mutex_unlock(&cam->lock);
932 return ret;
936 static int viacam_g_fmt_vid_cap(struct file *filp, void *priv,
937 struct v4l2_format *fmt)
939 struct via_camera *cam = priv;
941 mutex_lock(&cam->lock);
942 fmt->fmt.pix = cam->user_format;
943 mutex_unlock(&cam->lock);
944 return 0;
947 static int viacam_s_fmt_vid_cap(struct file *filp, void *priv,
948 struct v4l2_format *fmt)
950 struct via_camera *cam = priv;
951 int ret;
952 struct v4l2_format sfmt;
953 struct via_format *f = via_find_format(fmt->fmt.pix.pixelformat);
956 * Camera must be idle or we can't mess with the
957 * video setup.
959 mutex_lock(&cam->lock);
960 if (cam->opstate != S_IDLE) {
961 ret = -EBUSY;
962 goto out;
965 * Let the sensor code look over and tweak the
966 * requested formatting.
968 ret = viacam_do_try_fmt(cam, &fmt->fmt.pix, &sfmt.fmt.pix);
969 if (ret)
970 goto out;
972 * OK, let's commit to the new format.
974 cam->user_format = fmt->fmt.pix;
975 cam->sensor_format = sfmt.fmt.pix;
976 cam->mbus_code = f->mbus_code;
977 ret = viacam_configure_sensor(cam);
978 if (!ret)
979 ret = viacam_config_controller(cam);
980 out:
981 mutex_unlock(&cam->lock);
982 return ret;
985 static int viacam_querycap(struct file *filp, void *priv,
986 struct v4l2_capability *cap)
988 strcpy(cap->driver, "via-camera");
989 strcpy(cap->card, "via-camera");
990 cap->device_caps = V4L2_CAP_VIDEO_CAPTURE |
991 V4L2_CAP_READWRITE | V4L2_CAP_STREAMING;
992 cap->capabilities = cap->device_caps | V4L2_CAP_DEVICE_CAPS;
993 return 0;
997 * Streaming operations - pure videobuf stuff.
999 static int viacam_reqbufs(struct file *filp, void *priv,
1000 struct v4l2_requestbuffers *rb)
1002 struct via_camera *cam = priv;
1004 return videobuf_reqbufs(&cam->vb_queue, rb);
1007 static int viacam_querybuf(struct file *filp, void *priv,
1008 struct v4l2_buffer *buf)
1010 struct via_camera *cam = priv;
1012 return videobuf_querybuf(&cam->vb_queue, buf);
1015 static int viacam_qbuf(struct file *filp, void *priv, struct v4l2_buffer *buf)
1017 struct via_camera *cam = priv;
1019 return videobuf_qbuf(&cam->vb_queue, buf);
1022 static int viacam_dqbuf(struct file *filp, void *priv, struct v4l2_buffer *buf)
1024 struct via_camera *cam = priv;
1026 return videobuf_dqbuf(&cam->vb_queue, buf, filp->f_flags & O_NONBLOCK);
1029 static int viacam_streamon(struct file *filp, void *priv, enum v4l2_buf_type t)
1031 struct via_camera *cam = priv;
1032 int ret = 0;
1034 if (t != V4L2_BUF_TYPE_VIDEO_CAPTURE)
1035 return -EINVAL;
1037 mutex_lock(&cam->lock);
1038 if (cam->opstate != S_IDLE) {
1039 ret = -EBUSY;
1040 goto out;
1043 * Enforce the V4l2 "only one owner gets to read data" rule.
1045 if (cam->owner && cam->owner != filp) {
1046 ret = -EBUSY;
1047 goto out;
1049 cam->owner = filp;
1051 * Configure things if need be.
1053 if (test_bit(CF_CONFIG_NEEDED, &cam->flags)) {
1054 ret = viacam_configure_sensor(cam);
1055 if (ret)
1056 goto out;
1057 ret = viacam_config_controller(cam);
1058 if (ret)
1059 goto out;
1062 * If the CPU goes into C3, the DMA transfer gets corrupted and
1063 * users start filing unsightly bug reports. Put in a "latency"
1064 * requirement which will keep the CPU out of the deeper sleep
1065 * states.
1067 pm_qos_add_request(&cam->qos_request, PM_QOS_CPU_DMA_LATENCY, 50);
1069 * Fire things up.
1071 INIT_LIST_HEAD(&cam->buffer_queue);
1072 ret = videobuf_streamon(&cam->vb_queue);
1073 if (!ret)
1074 viacam_start_engine(cam);
1075 out:
1076 mutex_unlock(&cam->lock);
1077 return ret;
1080 static int viacam_streamoff(struct file *filp, void *priv, enum v4l2_buf_type t)
1082 struct via_camera *cam = priv;
1083 int ret;
1085 if (t != V4L2_BUF_TYPE_VIDEO_CAPTURE)
1086 return -EINVAL;
1087 mutex_lock(&cam->lock);
1088 if (cam->opstate != S_RUNNING) {
1089 ret = -EINVAL;
1090 goto out;
1092 pm_qos_remove_request(&cam->qos_request);
1093 viacam_stop_engine(cam);
1095 * Videobuf will recycle all of the outstanding buffers, but
1096 * we should be sure we don't retain any references to
1097 * any of them.
1099 ret = videobuf_streamoff(&cam->vb_queue);
1100 INIT_LIST_HEAD(&cam->buffer_queue);
1101 out:
1102 mutex_unlock(&cam->lock);
1103 return ret;
1106 /* G/S_PARM */
1108 static int viacam_g_parm(struct file *filp, void *priv,
1109 struct v4l2_streamparm *parm)
1111 struct via_camera *cam = priv;
1112 int ret;
1114 mutex_lock(&cam->lock);
1115 ret = sensor_call(cam, video, g_parm, parm);
1116 mutex_unlock(&cam->lock);
1117 parm->parm.capture.readbuffers = cam->n_cap_bufs;
1118 return ret;
1121 static int viacam_s_parm(struct file *filp, void *priv,
1122 struct v4l2_streamparm *parm)
1124 struct via_camera *cam = priv;
1125 int ret;
1127 mutex_lock(&cam->lock);
1128 ret = sensor_call(cam, video, s_parm, parm);
1129 mutex_unlock(&cam->lock);
1130 parm->parm.capture.readbuffers = cam->n_cap_bufs;
1131 return ret;
1134 static int viacam_enum_framesizes(struct file *filp, void *priv,
1135 struct v4l2_frmsizeenum *sizes)
1137 if (sizes->index != 0)
1138 return -EINVAL;
1139 sizes->type = V4L2_FRMSIZE_TYPE_CONTINUOUS;
1140 sizes->stepwise.min_width = QCIF_WIDTH;
1141 sizes->stepwise.min_height = QCIF_HEIGHT;
1142 sizes->stepwise.max_width = VGA_WIDTH;
1143 sizes->stepwise.max_height = VGA_HEIGHT;
1144 sizes->stepwise.step_width = sizes->stepwise.step_height = 1;
1145 return 0;
1148 static int viacam_enum_frameintervals(struct file *filp, void *priv,
1149 struct v4l2_frmivalenum *interval)
1151 struct via_camera *cam = priv;
1152 struct v4l2_subdev_frame_interval_enum fie = {
1153 .index = interval->index,
1154 .code = cam->mbus_code,
1155 .width = cam->sensor_format.width,
1156 .height = cam->sensor_format.height,
1157 .which = V4L2_SUBDEV_FORMAT_ACTIVE,
1159 int ret;
1161 mutex_lock(&cam->lock);
1162 ret = sensor_call(cam, pad, enum_frame_interval, NULL, &fie);
1163 mutex_unlock(&cam->lock);
1164 if (ret)
1165 return ret;
1166 interval->type = V4L2_FRMIVAL_TYPE_DISCRETE;
1167 interval->discrete = fie.interval;
1168 return 0;
1173 static const struct v4l2_ioctl_ops viacam_ioctl_ops = {
1174 .vidioc_enum_input = viacam_enum_input,
1175 .vidioc_g_input = viacam_g_input,
1176 .vidioc_s_input = viacam_s_input,
1177 .vidioc_s_std = viacam_s_std,
1178 .vidioc_g_std = viacam_g_std,
1179 .vidioc_enum_fmt_vid_cap = viacam_enum_fmt_vid_cap,
1180 .vidioc_try_fmt_vid_cap = viacam_try_fmt_vid_cap,
1181 .vidioc_g_fmt_vid_cap = viacam_g_fmt_vid_cap,
1182 .vidioc_s_fmt_vid_cap = viacam_s_fmt_vid_cap,
1183 .vidioc_querycap = viacam_querycap,
1184 .vidioc_reqbufs = viacam_reqbufs,
1185 .vidioc_querybuf = viacam_querybuf,
1186 .vidioc_qbuf = viacam_qbuf,
1187 .vidioc_dqbuf = viacam_dqbuf,
1188 .vidioc_streamon = viacam_streamon,
1189 .vidioc_streamoff = viacam_streamoff,
1190 .vidioc_g_parm = viacam_g_parm,
1191 .vidioc_s_parm = viacam_s_parm,
1192 .vidioc_enum_framesizes = viacam_enum_framesizes,
1193 .vidioc_enum_frameintervals = viacam_enum_frameintervals,
1196 /*----------------------------------------------------------------------------*/
1199 * Power management.
1201 #ifdef CONFIG_PM
1203 static int viacam_suspend(void *priv)
1205 struct via_camera *cam = priv;
1206 enum viacam_opstate state = cam->opstate;
1208 if (cam->opstate != S_IDLE) {
1209 viacam_stop_engine(cam);
1210 cam->opstate = state; /* So resume restarts */
1213 return 0;
1216 static int viacam_resume(void *priv)
1218 struct via_camera *cam = priv;
1219 int ret = 0;
1222 * Get back to a reasonable operating state.
1224 via_write_reg_mask(VIASR, 0x78, 0, 0x80);
1225 via_write_reg_mask(VIASR, 0x1e, 0xc0, 0xc0);
1226 viacam_int_disable(cam);
1227 set_bit(CF_CONFIG_NEEDED, &cam->flags);
1229 * Make sure the sensor's power state is correct
1231 if (cam->users > 0)
1232 via_sensor_power_up(cam);
1233 else
1234 via_sensor_power_down(cam);
1236 * If it was operating, try to restart it.
1238 if (cam->opstate != S_IDLE) {
1239 mutex_lock(&cam->lock);
1240 ret = viacam_configure_sensor(cam);
1241 if (!ret)
1242 ret = viacam_config_controller(cam);
1243 mutex_unlock(&cam->lock);
1244 if (!ret)
1245 viacam_start_engine(cam);
1248 return ret;
1251 static struct viafb_pm_hooks viacam_pm_hooks = {
1252 .suspend = viacam_suspend,
1253 .resume = viacam_resume
1256 #endif /* CONFIG_PM */
1259 * Setup stuff.
1262 static const struct video_device viacam_v4l_template = {
1263 .name = "via-camera",
1264 .minor = -1,
1265 .tvnorms = V4L2_STD_NTSC_M,
1266 .fops = &viacam_fops,
1267 .ioctl_ops = &viacam_ioctl_ops,
1268 .release = video_device_release_empty, /* Check this */
1272 * The OLPC folks put the serial port on the same pin as
1273 * the camera. They also get grumpy if we break the
1274 * serial port and keep them from using it. So we have
1275 * to check the serial enable bit and not step on it.
1277 #define VIACAM_SERIAL_DEVFN 0x88
1278 #define VIACAM_SERIAL_CREG 0x46
1279 #define VIACAM_SERIAL_BIT 0x40
1281 static bool viacam_serial_is_enabled(void)
1283 struct pci_bus *pbus = pci_find_bus(0, 0);
1284 u8 cbyte;
1286 if (!pbus)
1287 return false;
1288 pci_bus_read_config_byte(pbus, VIACAM_SERIAL_DEVFN,
1289 VIACAM_SERIAL_CREG, &cbyte);
1290 if ((cbyte & VIACAM_SERIAL_BIT) == 0)
1291 return false; /* Not enabled */
1292 if (!override_serial) {
1293 printk(KERN_NOTICE "Via camera: serial port is enabled, " \
1294 "refusing to load.\n");
1295 printk(KERN_NOTICE "Specify override_serial=1 to force " \
1296 "module loading.\n");
1297 return true;
1299 printk(KERN_NOTICE "Via camera: overriding serial port\n");
1300 pci_bus_write_config_byte(pbus, VIACAM_SERIAL_DEVFN,
1301 VIACAM_SERIAL_CREG, cbyte & ~VIACAM_SERIAL_BIT);
1302 return false;
1305 static struct ov7670_config sensor_cfg = {
1306 /* The XO-1.5 (only known user) clocks the camera at 90MHz. */
1307 .clock_speed = 90,
1310 static int viacam_probe(struct platform_device *pdev)
1312 int ret;
1313 struct i2c_adapter *sensor_adapter;
1314 struct viafb_dev *viadev = pdev->dev.platform_data;
1315 struct i2c_board_info ov7670_info = {
1316 .type = "ov7670",
1317 .addr = 0x42 >> 1,
1318 .platform_data = &sensor_cfg,
1322 * Note that there are actually two capture channels on
1323 * the device. We only deal with one for now. That
1324 * is encoded here; nothing else assumes it's dealing with
1325 * a unique capture device.
1327 struct via_camera *cam;
1330 * Ensure that frame buffer memory has been set aside for
1331 * this purpose. As an arbitrary limit, refuse to work
1332 * with less than two frames of VGA 16-bit data.
1334 * If we ever support the second port, we'll need to set
1335 * aside more memory.
1337 if (viadev->camera_fbmem_size < (VGA_HEIGHT*VGA_WIDTH*4)) {
1338 printk(KERN_ERR "viacam: insufficient FB memory reserved\n");
1339 return -ENOMEM;
1341 if (viadev->engine_mmio == NULL) {
1342 printk(KERN_ERR "viacam: No I/O memory, so no pictures\n");
1343 return -ENOMEM;
1346 if (machine_is_olpc() && viacam_serial_is_enabled())
1347 return -EBUSY;
1350 * Basic structure initialization.
1352 cam = kzalloc (sizeof(struct via_camera), GFP_KERNEL);
1353 if (cam == NULL)
1354 return -ENOMEM;
1355 via_cam_info = cam;
1356 cam->platdev = pdev;
1357 cam->viadev = viadev;
1358 cam->users = 0;
1359 cam->owner = NULL;
1360 cam->opstate = S_IDLE;
1361 cam->user_format = cam->sensor_format = viacam_def_pix_format;
1362 mutex_init(&cam->lock);
1363 INIT_LIST_HEAD(&cam->buffer_queue);
1364 cam->mmio = viadev->engine_mmio;
1365 cam->fbmem = viadev->fbmem;
1366 cam->fb_offset = viadev->camera_fbmem_offset;
1367 cam->flags = 1 << CF_CONFIG_NEEDED;
1368 cam->mbus_code = via_def_mbus_code;
1370 * Tell V4L that we exist.
1372 ret = v4l2_device_register(&pdev->dev, &cam->v4l2_dev);
1373 if (ret) {
1374 dev_err(&pdev->dev, "Unable to register v4l2 device\n");
1375 goto out_free;
1377 ret = v4l2_ctrl_handler_init(&cam->ctrl_handler, 10);
1378 if (ret)
1379 goto out_unregister;
1380 cam->v4l2_dev.ctrl_handler = &cam->ctrl_handler;
1382 * Convince the system that we can do DMA.
1384 pdev->dev.dma_mask = &viadev->pdev->dma_mask;
1385 dma_set_mask(&pdev->dev, 0xffffffff);
1387 * Fire up the capture port. The write to 0x78 looks purely
1388 * OLPCish; any system will need to tweak 0x1e.
1390 via_write_reg_mask(VIASR, 0x78, 0, 0x80);
1391 via_write_reg_mask(VIASR, 0x1e, 0xc0, 0xc0);
1393 * Get the sensor powered up.
1395 ret = via_sensor_power_setup(cam);
1396 if (ret)
1397 goto out_ctrl_hdl_free;
1398 via_sensor_power_up(cam);
1401 * See if we can't find it on the bus. The VIA_PORT_31 assumption
1402 * is OLPC-specific. 0x42 assumption is ov7670-specific.
1404 sensor_adapter = viafb_find_i2c_adapter(VIA_PORT_31);
1405 cam->sensor = v4l2_i2c_new_subdev_board(&cam->v4l2_dev, sensor_adapter,
1406 &ov7670_info, NULL);
1407 if (cam->sensor == NULL) {
1408 dev_err(&pdev->dev, "Unable to find the sensor!\n");
1409 ret = -ENODEV;
1410 goto out_power_down;
1413 * Get the IRQ.
1415 viacam_int_disable(cam);
1416 ret = request_threaded_irq(viadev->pdev->irq, viacam_quick_irq,
1417 viacam_irq, IRQF_SHARED, "via-camera", cam);
1418 if (ret)
1419 goto out_power_down;
1421 * Tell V4l2 that we exist.
1423 cam->vdev = viacam_v4l_template;
1424 cam->vdev.v4l2_dev = &cam->v4l2_dev;
1425 ret = video_register_device(&cam->vdev, VFL_TYPE_GRABBER, -1);
1426 if (ret)
1427 goto out_irq;
1428 video_set_drvdata(&cam->vdev, cam);
1430 #ifdef CONFIG_PM
1432 * Hook into PM events
1434 viacam_pm_hooks.private = cam;
1435 viafb_pm_register(&viacam_pm_hooks);
1436 #endif
1438 /* Power the sensor down until somebody opens the device */
1439 via_sensor_power_down(cam);
1440 return 0;
1442 out_irq:
1443 free_irq(viadev->pdev->irq, cam);
1444 out_power_down:
1445 via_sensor_power_release(cam);
1446 out_ctrl_hdl_free:
1447 v4l2_ctrl_handler_free(&cam->ctrl_handler);
1448 out_unregister:
1449 v4l2_device_unregister(&cam->v4l2_dev);
1450 out_free:
1451 kfree(cam);
1452 return ret;
1455 static int viacam_remove(struct platform_device *pdev)
1457 struct via_camera *cam = via_cam_info;
1458 struct viafb_dev *viadev = pdev->dev.platform_data;
1460 video_unregister_device(&cam->vdev);
1461 v4l2_device_unregister(&cam->v4l2_dev);
1462 free_irq(viadev->pdev->irq, cam);
1463 via_sensor_power_release(cam);
1464 v4l2_ctrl_handler_free(&cam->ctrl_handler);
1465 kfree(cam);
1466 via_cam_info = NULL;
1467 return 0;
1470 static struct platform_driver viacam_driver = {
1471 .driver = {
1472 .name = "viafb-camera",
1474 .probe = viacam_probe,
1475 .remove = viacam_remove,
1478 module_platform_driver(viacam_driver);