sfc: Don't use enums as a bitmask.
[zen-stable.git] / drivers / media / video / via-camera.c
blob8c780c2d937be5b02d174c04d35d5f97815ac2e1
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-chip-ident.h>
21 #include <media/videobuf-dma-sg.h>
22 #include <linux/delay.h>
23 #include <linux/dma-mapping.h>
24 #include <linux/pm_qos_params.h>
25 #include <linux/via-core.h>
26 #include <linux/via-gpio.h>
27 #include <linux/via_i2c.h>
28 #include <asm/olpc.h>
30 #include "via-camera.h"
32 MODULE_AUTHOR("Jonathan Corbet <corbet@lwn.net>");
33 MODULE_DESCRIPTION("VIA framebuffer-based camera controller driver");
34 MODULE_LICENSE("GPL");
36 static int flip_image;
37 module_param(flip_image, bool, 0444);
38 MODULE_PARM_DESC(flip_image,
39 "If set, the sensor will be instructed to flip the image "
40 "vertically.");
42 static int override_serial;
43 module_param(override_serial, bool, 0444);
44 MODULE_PARM_DESC(override_serial,
45 "The camera driver will normally refuse to load if "
46 "the XO 1.5 serial port is enabled. Set this option "
47 "to force-enable the camera.");
50 * Basic window sizes.
52 #define VGA_WIDTH 640
53 #define VGA_HEIGHT 480
54 #define QCIF_WIDTH 176
55 #define QCIF_HEIGHT 144
58 * The structure describing our camera.
60 enum viacam_opstate { S_IDLE = 0, S_RUNNING = 1 };
62 struct via_camera {
63 struct v4l2_device v4l2_dev;
64 struct video_device vdev;
65 struct v4l2_subdev *sensor;
66 struct platform_device *platdev;
67 struct viafb_dev *viadev;
68 struct mutex lock;
69 enum viacam_opstate opstate;
70 unsigned long flags;
71 struct pm_qos_request_list qos_request;
73 * GPIO info for power/reset management
75 int power_gpio;
76 int reset_gpio;
78 * I/O memory stuff.
80 void __iomem *mmio; /* Where the registers live */
81 void __iomem *fbmem; /* Frame buffer memory */
82 u32 fb_offset; /* Reserved memory offset (FB) */
84 * Capture buffers and related. The controller supports
85 * up to three, so that's what we have here. These buffers
86 * live in frame buffer memory, so we don't call them "DMA".
88 unsigned int cb_offsets[3]; /* offsets into fb mem */
89 u8 *cb_addrs[3]; /* Kernel-space addresses */
90 int n_cap_bufs; /* How many are we using? */
91 int next_buf;
92 struct videobuf_queue vb_queue;
93 struct list_head buffer_queue; /* prot. by reg_lock */
95 * User tracking.
97 int users;
98 struct file *owner;
100 * Video format information. sensor_format is kept in a form
101 * that we can use to pass to the sensor. We always run the
102 * sensor in VGA resolution, though, and let the controller
103 * downscale things if need be. So we keep the "real*
104 * dimensions separately.
106 struct v4l2_pix_format sensor_format;
107 struct v4l2_pix_format user_format;
108 enum v4l2_mbus_pixelcode mbus_code;
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 {
147 __u8 *desc;
148 __u32 pixelformat;
149 int bpp; /* Bytes per pixel */
150 enum v4l2_mbus_pixelcode mbus_code;
151 } via_formats[] = {
153 .desc = "YUYV 4:2:2",
154 .pixelformat = V4L2_PIX_FMT_YUYV,
155 .mbus_code = V4L2_MBUS_FMT_YUYV8_2X8,
156 .bpp = 2,
159 .desc = "RGB 565",
160 .pixelformat = V4L2_PIX_FMT_RGB565,
161 .mbus_code = V4L2_MBUS_FMT_RGB565_2X8_LE,
162 .bpp = 2,
164 /* RGB444 and Bayer should be doable, but have never been
165 tested with this driver. */
167 #define N_VIA_FMTS ARRAY_SIZE(via_formats)
169 static struct via_format *via_find_format(u32 pixelformat)
171 unsigned i;
173 for (i = 0; i < N_VIA_FMTS; i++)
174 if (via_formats[i].pixelformat == pixelformat)
175 return via_formats + i;
176 /* Not found? Then return the first format. */
177 return via_formats;
181 /*--------------------------------------------------------------------------*/
183 * Sensor power/reset management. This piece is OLPC-specific for
184 * sure; other configurations will have things connected differently.
186 static int via_sensor_power_setup(struct via_camera *cam)
188 int ret;
190 cam->power_gpio = viafb_gpio_lookup("VGPIO3");
191 cam->reset_gpio = viafb_gpio_lookup("VGPIO2");
192 if (cam->power_gpio < 0 || cam->reset_gpio < 0) {
193 dev_err(&cam->platdev->dev, "Unable to find GPIO lines\n");
194 return -EINVAL;
196 ret = gpio_request(cam->power_gpio, "viafb-camera");
197 if (ret) {
198 dev_err(&cam->platdev->dev, "Unable to request power GPIO\n");
199 return ret;
201 ret = gpio_request(cam->reset_gpio, "viafb-camera");
202 if (ret) {
203 dev_err(&cam->platdev->dev, "Unable to request reset GPIO\n");
204 gpio_free(cam->power_gpio);
205 return ret;
207 gpio_direction_output(cam->power_gpio, 0);
208 gpio_direction_output(cam->reset_gpio, 0);
209 return 0;
213 * Power up the sensor and perform the reset dance.
215 static void via_sensor_power_up(struct via_camera *cam)
217 gpio_set_value(cam->power_gpio, 1);
218 gpio_set_value(cam->reset_gpio, 0);
219 msleep(20); /* Probably excessive */
220 gpio_set_value(cam->reset_gpio, 1);
221 msleep(20);
224 static void via_sensor_power_down(struct via_camera *cam)
226 gpio_set_value(cam->power_gpio, 0);
227 gpio_set_value(cam->reset_gpio, 0);
231 static void via_sensor_power_release(struct via_camera *cam)
233 via_sensor_power_down(cam);
234 gpio_free(cam->power_gpio);
235 gpio_free(cam->reset_gpio);
238 /* --------------------------------------------------------------------------*/
239 /* Sensor ops */
242 * Manage the ov7670 "flip" bit, which needs special help.
244 static int viacam_set_flip(struct via_camera *cam)
246 struct v4l2_control ctrl;
248 memset(&ctrl, 0, sizeof(ctrl));
249 ctrl.id = V4L2_CID_VFLIP;
250 ctrl.value = flip_image;
251 return sensor_call(cam, core, s_ctrl, &ctrl);
255 * Configure the sensor. It's up to the caller to ensure
256 * that the camera is in the correct operating state.
258 static int viacam_configure_sensor(struct via_camera *cam)
260 struct v4l2_mbus_framefmt mbus_fmt;
261 int ret;
263 v4l2_fill_mbus_format(&mbus_fmt, &cam->sensor_format, cam->mbus_code);
264 ret = sensor_call(cam, core, init, 0);
265 if (ret == 0)
266 ret = sensor_call(cam, video, s_mbus_fmt, &mbus_fmt);
268 * OV7670 does weird things if flip is set *before* format...
270 if (ret == 0)
271 ret = viacam_set_flip(cam);
272 return ret;
277 /* --------------------------------------------------------------------------*/
279 * Some simple register accessors; they assume that the lock is held.
281 * Should we want to support the second capture engine, we could
282 * hide the register difference by adding 0x1000 to registers in the
283 * 0x300-350 range.
285 static inline void viacam_write_reg(struct via_camera *cam,
286 int reg, int value)
288 iowrite32(value, cam->mmio + reg);
291 static inline int viacam_read_reg(struct via_camera *cam, int reg)
293 return ioread32(cam->mmio + reg);
296 static inline void viacam_write_reg_mask(struct via_camera *cam,
297 int reg, int value, int mask)
299 int tmp = viacam_read_reg(cam, reg);
301 tmp = (tmp & ~mask) | (value & mask);
302 viacam_write_reg(cam, reg, tmp);
306 /* --------------------------------------------------------------------------*/
307 /* Interrupt management and handling */
309 static irqreturn_t viacam_quick_irq(int irq, void *data)
311 struct via_camera *cam = data;
312 irqreturn_t ret = IRQ_NONE;
313 int icv;
316 * All we do here is to clear the interrupts and tell
317 * the handler thread to wake up.
319 spin_lock(&cam->viadev->reg_lock);
320 icv = viacam_read_reg(cam, VCR_INTCTRL);
321 if (icv & VCR_IC_EAV) {
322 icv |= VCR_IC_EAV|VCR_IC_EVBI|VCR_IC_FFULL;
323 viacam_write_reg(cam, VCR_INTCTRL, icv);
324 ret = IRQ_WAKE_THREAD;
326 spin_unlock(&cam->viadev->reg_lock);
327 return ret;
331 * Find the next videobuf buffer which has somebody waiting on it.
333 static struct videobuf_buffer *viacam_next_buffer(struct via_camera *cam)
335 unsigned long flags;
336 struct videobuf_buffer *buf = NULL;
338 spin_lock_irqsave(&cam->viadev->reg_lock, flags);
339 if (cam->opstate != S_RUNNING)
340 goto out;
341 if (list_empty(&cam->buffer_queue))
342 goto out;
343 buf = list_entry(cam->buffer_queue.next, struct videobuf_buffer, queue);
344 if (!waitqueue_active(&buf->done)) {/* Nobody waiting */
345 buf = NULL;
346 goto out;
348 list_del(&buf->queue);
349 buf->state = VIDEOBUF_ACTIVE;
350 out:
351 spin_unlock_irqrestore(&cam->viadev->reg_lock, flags);
352 return buf;
356 * The threaded IRQ handler.
358 static irqreturn_t viacam_irq(int irq, void *data)
360 int bufn;
361 struct videobuf_buffer *vb;
362 struct via_camera *cam = data;
363 struct videobuf_dmabuf *vdma;
366 * If there is no place to put the data frame, don't bother
367 * with anything else.
369 vb = viacam_next_buffer(cam);
370 if (vb == NULL)
371 goto done;
373 * Figure out which buffer we just completed.
375 bufn = (viacam_read_reg(cam, VCR_INTCTRL) & VCR_IC_ACTBUF) >> 3;
376 bufn -= 1;
377 if (bufn < 0)
378 bufn = cam->n_cap_bufs - 1;
380 * Copy over the data and let any waiters know.
382 vdma = videobuf_to_dma(vb);
383 viafb_dma_copy_out_sg(cam->cb_offsets[bufn], vdma->sglist, vdma->sglen);
384 vb->state = VIDEOBUF_DONE;
385 vb->size = cam->user_format.sizeimage;
386 wake_up(&vb->done);
387 done:
388 return IRQ_HANDLED;
393 * These functions must mess around with the general interrupt
394 * control register, which is relevant to much more than just the
395 * camera. Nothing else uses interrupts, though, as of this writing.
396 * Should that situation change, we'll have to improve support at
397 * the via-core level.
399 static void viacam_int_enable(struct via_camera *cam)
401 viacam_write_reg(cam, VCR_INTCTRL,
402 VCR_IC_INTEN|VCR_IC_EAV|VCR_IC_EVBI|VCR_IC_FFULL);
403 viafb_irq_enable(VDE_I_C0AVEN);
406 static void viacam_int_disable(struct via_camera *cam)
408 viafb_irq_disable(VDE_I_C0AVEN);
409 viacam_write_reg(cam, VCR_INTCTRL, 0);
414 /* --------------------------------------------------------------------------*/
415 /* Controller operations */
418 * Set up our capture buffers in framebuffer memory.
420 static int viacam_ctlr_cbufs(struct via_camera *cam)
422 int nbuf = cam->viadev->camera_fbmem_size/cam->sensor_format.sizeimage;
423 int i;
424 unsigned int offset;
427 * See how many buffers we can work with.
429 if (nbuf >= 3) {
430 cam->n_cap_bufs = 3;
431 viacam_write_reg_mask(cam, VCR_CAPINTC, VCR_CI_3BUFS,
432 VCR_CI_3BUFS);
433 } else if (nbuf == 2) {
434 cam->n_cap_bufs = 2;
435 viacam_write_reg_mask(cam, VCR_CAPINTC, 0, VCR_CI_3BUFS);
436 } else {
437 cam_warn(cam, "Insufficient frame buffer memory\n");
438 return -ENOMEM;
441 * Set them up.
443 offset = cam->fb_offset;
444 for (i = 0; i < cam->n_cap_bufs; i++) {
445 cam->cb_offsets[i] = offset;
446 cam->cb_addrs[i] = cam->fbmem + offset;
447 viacam_write_reg(cam, VCR_VBUF1 + i*4, offset & VCR_VBUF_MASK);
448 offset += cam->sensor_format.sizeimage;
450 return 0;
454 * Set the scaling register for downscaling the image.
456 * This register works like this... Vertical scaling is enabled
457 * by bit 26; if that bit is set, downscaling is controlled by the
458 * value in bits 16:25. Those bits are divided by 1024 to get
459 * the scaling factor; setting just bit 25 thus cuts the height
460 * in half.
462 * Horizontal scaling works about the same, but it's enabled by
463 * bit 11, with bits 0:10 giving the numerator of a fraction
464 * (over 2048) for the scaling value.
466 * This function is naive in that, if the user departs from
467 * the 3x4 VGA scaling factor, the image will distort. We
468 * could work around that if it really seemed important.
470 static void viacam_set_scale(struct via_camera *cam)
472 unsigned int avscale;
473 int sf;
475 if (cam->user_format.width == VGA_WIDTH)
476 avscale = 0;
477 else {
478 sf = (cam->user_format.width*2048)/VGA_WIDTH;
479 avscale = VCR_AVS_HEN | sf;
481 if (cam->user_format.height < VGA_HEIGHT) {
482 sf = (1024*cam->user_format.height)/VGA_HEIGHT;
483 avscale |= VCR_AVS_VEN | (sf << 16);
485 viacam_write_reg(cam, VCR_AVSCALE, avscale);
490 * Configure image-related information into the capture engine.
492 static void viacam_ctlr_image(struct via_camera *cam)
494 int cicreg;
497 * Disable clock before messing with stuff - from the via
498 * sample driver.
500 viacam_write_reg(cam, VCR_CAPINTC, ~(VCR_CI_ENABLE|VCR_CI_CLKEN));
502 * Set up the controller for VGA resolution, modulo magic
503 * offsets from the via sample driver.
505 viacam_write_reg(cam, VCR_HORRANGE, 0x06200120);
506 viacam_write_reg(cam, VCR_VERTRANGE, 0x01de0000);
507 viacam_set_scale(cam);
509 * Image size info.
511 viacam_write_reg(cam, VCR_MAXDATA,
512 (cam->sensor_format.height << 16) |
513 (cam->sensor_format.bytesperline >> 3));
514 viacam_write_reg(cam, VCR_MAXVBI, 0);
515 viacam_write_reg(cam, VCR_VSTRIDE,
516 cam->user_format.bytesperline & VCR_VS_STRIDE);
518 * Set up the capture interface control register,
519 * everything but the "go" bit.
521 * The FIFO threshold is a bit of a magic number; 8 is what
522 * VIA's sample code uses.
524 cicreg = VCR_CI_CLKEN |
525 0x08000000 | /* FIFO threshold */
526 VCR_CI_FLDINV | /* OLPC-specific? */
527 VCR_CI_VREFINV | /* OLPC-specific? */
528 VCR_CI_DIBOTH | /* Capture both fields */
529 VCR_CI_CCIR601_8;
530 if (cam->n_cap_bufs == 3)
531 cicreg |= VCR_CI_3BUFS;
533 * YUV formats need different byte swapping than RGB.
535 if (cam->user_format.pixelformat == V4L2_PIX_FMT_YUYV)
536 cicreg |= VCR_CI_YUYV;
537 else
538 cicreg |= VCR_CI_UYVY;
539 viacam_write_reg(cam, VCR_CAPINTC, cicreg);
543 static int viacam_config_controller(struct via_camera *cam)
545 int ret;
546 unsigned long flags;
548 spin_lock_irqsave(&cam->viadev->reg_lock, flags);
549 ret = viacam_ctlr_cbufs(cam);
550 if (!ret)
551 viacam_ctlr_image(cam);
552 spin_unlock_irqrestore(&cam->viadev->reg_lock, flags);
553 clear_bit(CF_CONFIG_NEEDED, &cam->flags);
554 return ret;
558 * Make it start grabbing data.
560 static void viacam_start_engine(struct via_camera *cam)
562 spin_lock_irq(&cam->viadev->reg_lock);
563 cam->next_buf = 0;
564 viacam_write_reg_mask(cam, VCR_CAPINTC, VCR_CI_ENABLE, VCR_CI_ENABLE);
565 viacam_int_enable(cam);
566 (void) viacam_read_reg(cam, VCR_CAPINTC); /* Force post */
567 cam->opstate = S_RUNNING;
568 spin_unlock_irq(&cam->viadev->reg_lock);
572 static void viacam_stop_engine(struct via_camera *cam)
574 spin_lock_irq(&cam->viadev->reg_lock);
575 viacam_int_disable(cam);
576 viacam_write_reg_mask(cam, VCR_CAPINTC, 0, VCR_CI_ENABLE);
577 (void) viacam_read_reg(cam, VCR_CAPINTC); /* Force post */
578 cam->opstate = S_IDLE;
579 spin_unlock_irq(&cam->viadev->reg_lock);
583 /* --------------------------------------------------------------------------*/
584 /* Videobuf callback ops */
587 * buffer_setup. The purpose of this one would appear to be to tell
588 * videobuf how big a single image is. It's also evidently up to us
589 * to put some sort of limit on the maximum number of buffers allowed.
591 static int viacam_vb_buf_setup(struct videobuf_queue *q,
592 unsigned int *count, unsigned int *size)
594 struct via_camera *cam = q->priv_data;
596 *size = cam->user_format.sizeimage;
597 if (*count == 0 || *count > 6) /* Arbitrary number */
598 *count = 6;
599 return 0;
603 * Prepare a buffer.
605 static int viacam_vb_buf_prepare(struct videobuf_queue *q,
606 struct videobuf_buffer *vb, enum v4l2_field field)
608 struct via_camera *cam = q->priv_data;
610 vb->size = cam->user_format.sizeimage;
611 vb->width = cam->user_format.width; /* bytesperline???? */
612 vb->height = cam->user_format.height;
613 vb->field = field;
614 if (vb->state == VIDEOBUF_NEEDS_INIT) {
615 int ret = videobuf_iolock(q, vb, NULL);
616 if (ret)
617 return ret;
619 vb->state = VIDEOBUF_PREPARED;
620 return 0;
624 * We've got a buffer to put data into.
626 * FIXME: check for a running engine and valid buffers?
628 static void viacam_vb_buf_queue(struct videobuf_queue *q,
629 struct videobuf_buffer *vb)
631 struct via_camera *cam = q->priv_data;
634 * Note that videobuf holds the lock when it calls
635 * us, so we need not (indeed, cannot) take it here.
637 vb->state = VIDEOBUF_QUEUED;
638 list_add_tail(&vb->queue, &cam->buffer_queue);
642 * Free a buffer.
644 static void viacam_vb_buf_release(struct videobuf_queue *q,
645 struct videobuf_buffer *vb)
647 struct via_camera *cam = q->priv_data;
649 videobuf_dma_unmap(&cam->platdev->dev, videobuf_to_dma(vb));
650 videobuf_dma_free(videobuf_to_dma(vb));
651 vb->state = VIDEOBUF_NEEDS_INIT;
654 static const struct videobuf_queue_ops viacam_vb_ops = {
655 .buf_setup = viacam_vb_buf_setup,
656 .buf_prepare = viacam_vb_buf_prepare,
657 .buf_queue = viacam_vb_buf_queue,
658 .buf_release = viacam_vb_buf_release,
661 /* --------------------------------------------------------------------------*/
662 /* File operations */
664 static int viacam_open(struct file *filp)
666 struct via_camera *cam = video_drvdata(filp);
668 filp->private_data = cam;
670 * Note the new user. If this is the first one, we'll also
671 * need to power up the sensor.
673 mutex_lock(&cam->lock);
674 if (cam->users == 0) {
675 int ret = viafb_request_dma();
677 if (ret) {
678 mutex_unlock(&cam->lock);
679 return ret;
681 via_sensor_power_up(cam);
682 set_bit(CF_CONFIG_NEEDED, &cam->flags);
684 * Hook into videobuf. Evidently this cannot fail.
686 videobuf_queue_sg_init(&cam->vb_queue, &viacam_vb_ops,
687 &cam->platdev->dev, &cam->viadev->reg_lock,
688 V4L2_BUF_TYPE_VIDEO_CAPTURE, V4L2_FIELD_NONE,
689 sizeof(struct videobuf_buffer), cam, NULL);
691 (cam->users)++;
692 mutex_unlock(&cam->lock);
693 return 0;
696 static int viacam_release(struct file *filp)
698 struct via_camera *cam = video_drvdata(filp);
700 mutex_lock(&cam->lock);
701 (cam->users)--;
703 * If the "owner" is closing, shut down any ongoing
704 * operations.
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
712 * not hurt anyway.
714 if (cam->opstate != S_IDLE)
715 viacam_stop_engine(cam);
716 cam->owner = NULL;
719 * Last one out needs to turn out the lights.
721 if (cam->users == 0) {
722 videobuf_mmap_free(&cam->vb_queue);
723 via_sensor_power_down(cam);
724 viafb_release_dma();
726 mutex_unlock(&cam->lock);
727 return 0;
731 * Read a frame from the device.
733 static ssize_t viacam_read(struct file *filp, char __user *buffer,
734 size_t len, loff_t *pos)
736 struct via_camera *cam = video_drvdata(filp);
737 int ret;
739 mutex_lock(&cam->lock);
741 * Enforce the V4l2 "only one owner gets to read data" rule.
743 if (cam->owner && cam->owner != filp) {
744 ret = -EBUSY;
745 goto out_unlock;
747 cam->owner = filp;
749 * Do we need to configure the hardware?
751 if (test_bit(CF_CONFIG_NEEDED, &cam->flags)) {
752 ret = viacam_configure_sensor(cam);
753 if (!ret)
754 ret = viacam_config_controller(cam);
755 if (ret)
756 goto out_unlock;
759 * Fire up the capture engine, then have videobuf do
760 * the heavy lifting. Someday it would be good to avoid
761 * stopping and restarting the engine each time.
763 INIT_LIST_HEAD(&cam->buffer_queue);
764 viacam_start_engine(cam);
765 ret = videobuf_read_stream(&cam->vb_queue, buffer, len, pos, 0,
766 filp->f_flags & O_NONBLOCK);
767 viacam_stop_engine(cam);
768 /* videobuf_stop() ?? */
770 out_unlock:
771 mutex_unlock(&cam->lock);
772 return ret;
776 static unsigned int viacam_poll(struct file *filp, struct poll_table_struct *pt)
778 struct via_camera *cam = video_drvdata(filp);
780 return videobuf_poll_stream(filp, &cam->vb_queue, pt);
784 static int viacam_mmap(struct file *filp, struct vm_area_struct *vma)
786 struct via_camera *cam = video_drvdata(filp);
788 return videobuf_mmap_mapper(&cam->vb_queue, vma);
793 static const struct v4l2_file_operations viacam_fops = {
794 .owner = THIS_MODULE,
795 .open = viacam_open,
796 .release = viacam_release,
797 .read = viacam_read,
798 .poll = viacam_poll,
799 .mmap = viacam_mmap,
800 .unlocked_ioctl = video_ioctl2,
803 /*----------------------------------------------------------------------------*/
805 * The long list of v4l2 ioctl ops
808 static int viacam_g_chip_ident(struct file *file, void *priv,
809 struct v4l2_dbg_chip_ident *ident)
811 struct via_camera *cam = priv;
813 ident->ident = V4L2_IDENT_NONE;
814 ident->revision = 0;
815 if (v4l2_chip_match_host(&ident->match)) {
816 ident->ident = V4L2_IDENT_VIA_VX855;
817 return 0;
819 return sensor_call(cam, core, g_chip_ident, ident);
823 * Control ops are passed through to the sensor.
825 static int viacam_queryctrl(struct file *filp, void *priv,
826 struct v4l2_queryctrl *qc)
828 struct via_camera *cam = priv;
829 int ret;
831 mutex_lock(&cam->lock);
832 ret = sensor_call(cam, core, queryctrl, qc);
833 mutex_unlock(&cam->lock);
834 return ret;
838 static int viacam_g_ctrl(struct file *filp, void *priv,
839 struct v4l2_control *ctrl)
841 struct via_camera *cam = priv;
842 int ret;
844 mutex_lock(&cam->lock);
845 ret = sensor_call(cam, core, g_ctrl, ctrl);
846 mutex_unlock(&cam->lock);
847 return ret;
851 static int viacam_s_ctrl(struct file *filp, void *priv,
852 struct v4l2_control *ctrl)
854 struct via_camera *cam = priv;
855 int ret;
857 mutex_lock(&cam->lock);
858 ret = sensor_call(cam, core, s_ctrl, ctrl);
859 mutex_unlock(&cam->lock);
860 return ret;
864 * Only one input.
866 static int viacam_enum_input(struct file *filp, void *priv,
867 struct v4l2_input *input)
869 if (input->index != 0)
870 return -EINVAL;
872 input->type = V4L2_INPUT_TYPE_CAMERA;
873 input->std = V4L2_STD_ALL; /* Not sure what should go here */
874 strcpy(input->name, "Camera");
875 return 0;
878 static int viacam_g_input(struct file *filp, void *priv, unsigned int *i)
880 *i = 0;
881 return 0;
884 static int viacam_s_input(struct file *filp, void *priv, unsigned int i)
886 if (i != 0)
887 return -EINVAL;
888 return 0;
891 static int viacam_s_std(struct file *filp, void *priv, v4l2_std_id *std)
893 return 0;
897 * Video format stuff. Here is our default format until
898 * user space messes with things.
900 static const struct v4l2_pix_format viacam_def_pix_format = {
901 .width = VGA_WIDTH,
902 .height = VGA_HEIGHT,
903 .pixelformat = V4L2_PIX_FMT_YUYV,
904 .field = V4L2_FIELD_NONE,
905 .bytesperline = VGA_WIDTH * 2,
906 .sizeimage = VGA_WIDTH * VGA_HEIGHT * 2,
909 static const enum v4l2_mbus_pixelcode via_def_mbus_code = V4L2_MBUS_FMT_YUYV8_2X8;
911 static int viacam_enum_fmt_vid_cap(struct file *filp, void *priv,
912 struct v4l2_fmtdesc *fmt)
914 if (fmt->index >= N_VIA_FMTS)
915 return -EINVAL;
916 strlcpy(fmt->description, via_formats[fmt->index].desc,
917 sizeof(fmt->description));
918 fmt->pixelformat = via_formats[fmt->index].pixelformat;
919 return 0;
923 * Figure out proper image dimensions, but always force the
924 * sensor to VGA.
926 static void viacam_fmt_pre(struct v4l2_pix_format *userfmt,
927 struct v4l2_pix_format *sensorfmt)
929 *sensorfmt = *userfmt;
930 if (userfmt->width < QCIF_WIDTH || userfmt->height < QCIF_HEIGHT) {
931 userfmt->width = QCIF_WIDTH;
932 userfmt->height = QCIF_HEIGHT;
934 if (userfmt->width > VGA_WIDTH || userfmt->height > VGA_HEIGHT) {
935 userfmt->width = VGA_WIDTH;
936 userfmt->height = VGA_HEIGHT;
938 sensorfmt->width = VGA_WIDTH;
939 sensorfmt->height = VGA_HEIGHT;
942 static void viacam_fmt_post(struct v4l2_pix_format *userfmt,
943 struct v4l2_pix_format *sensorfmt)
945 struct via_format *f = via_find_format(userfmt->pixelformat);
947 sensorfmt->bytesperline = sensorfmt->width * f->bpp;
948 sensorfmt->sizeimage = sensorfmt->height * sensorfmt->bytesperline;
949 userfmt->pixelformat = sensorfmt->pixelformat;
950 userfmt->field = sensorfmt->field;
951 userfmt->bytesperline = 2 * userfmt->width;
952 userfmt->sizeimage = userfmt->bytesperline * userfmt->height;
957 * The real work of figuring out a workable format.
959 static int viacam_do_try_fmt(struct via_camera *cam,
960 struct v4l2_pix_format *upix, struct v4l2_pix_format *spix)
962 int ret;
963 struct v4l2_mbus_framefmt mbus_fmt;
964 struct via_format *f = via_find_format(upix->pixelformat);
966 upix->pixelformat = f->pixelformat;
967 viacam_fmt_pre(upix, spix);
968 v4l2_fill_mbus_format(&mbus_fmt, upix, f->mbus_code);
969 ret = sensor_call(cam, video, try_mbus_fmt, &mbus_fmt);
970 v4l2_fill_pix_format(spix, &mbus_fmt);
971 viacam_fmt_post(upix, spix);
972 return ret;
977 static int viacam_try_fmt_vid_cap(struct file *filp, void *priv,
978 struct v4l2_format *fmt)
980 struct via_camera *cam = priv;
981 struct v4l2_format sfmt;
982 int ret;
984 mutex_lock(&cam->lock);
985 ret = viacam_do_try_fmt(cam, &fmt->fmt.pix, &sfmt.fmt.pix);
986 mutex_unlock(&cam->lock);
987 return ret;
991 static int viacam_g_fmt_vid_cap(struct file *filp, void *priv,
992 struct v4l2_format *fmt)
994 struct via_camera *cam = priv;
996 mutex_lock(&cam->lock);
997 fmt->fmt.pix = cam->user_format;
998 mutex_unlock(&cam->lock);
999 return 0;
1002 static int viacam_s_fmt_vid_cap(struct file *filp, void *priv,
1003 struct v4l2_format *fmt)
1005 struct via_camera *cam = priv;
1006 int ret;
1007 struct v4l2_format sfmt;
1008 struct via_format *f = via_find_format(fmt->fmt.pix.pixelformat);
1011 * Camera must be idle or we can't mess with the
1012 * video setup.
1014 mutex_lock(&cam->lock);
1015 if (cam->opstate != S_IDLE) {
1016 ret = -EBUSY;
1017 goto out;
1020 * Let the sensor code look over and tweak the
1021 * requested formatting.
1023 ret = viacam_do_try_fmt(cam, &fmt->fmt.pix, &sfmt.fmt.pix);
1024 if (ret)
1025 goto out;
1027 * OK, let's commit to the new format.
1029 cam->user_format = fmt->fmt.pix;
1030 cam->sensor_format = sfmt.fmt.pix;
1031 cam->mbus_code = f->mbus_code;
1032 ret = viacam_configure_sensor(cam);
1033 if (!ret)
1034 ret = viacam_config_controller(cam);
1035 out:
1036 mutex_unlock(&cam->lock);
1037 return ret;
1040 static int viacam_querycap(struct file *filp, void *priv,
1041 struct v4l2_capability *cap)
1043 strcpy(cap->driver, "via-camera");
1044 strcpy(cap->card, "via-camera");
1045 cap->version = 1;
1046 cap->capabilities = V4L2_CAP_VIDEO_CAPTURE |
1047 V4L2_CAP_READWRITE | V4L2_CAP_STREAMING;
1048 return 0;
1052 * Streaming operations - pure videobuf stuff.
1054 static int viacam_reqbufs(struct file *filp, void *priv,
1055 struct v4l2_requestbuffers *rb)
1057 struct via_camera *cam = priv;
1059 return videobuf_reqbufs(&cam->vb_queue, rb);
1062 static int viacam_querybuf(struct file *filp, void *priv,
1063 struct v4l2_buffer *buf)
1065 struct via_camera *cam = priv;
1067 return videobuf_querybuf(&cam->vb_queue, buf);
1070 static int viacam_qbuf(struct file *filp, void *priv, struct v4l2_buffer *buf)
1072 struct via_camera *cam = priv;
1074 return videobuf_qbuf(&cam->vb_queue, buf);
1077 static int viacam_dqbuf(struct file *filp, void *priv, struct v4l2_buffer *buf)
1079 struct via_camera *cam = priv;
1081 return videobuf_dqbuf(&cam->vb_queue, buf, filp->f_flags & O_NONBLOCK);
1084 static int viacam_streamon(struct file *filp, void *priv, enum v4l2_buf_type t)
1086 struct via_camera *cam = priv;
1087 int ret = 0;
1089 if (t != V4L2_BUF_TYPE_VIDEO_CAPTURE)
1090 return -EINVAL;
1092 mutex_lock(&cam->lock);
1093 if (cam->opstate != S_IDLE) {
1094 ret = -EBUSY;
1095 goto out;
1098 * Enforce the V4l2 "only one owner gets to read data" rule.
1100 if (cam->owner && cam->owner != filp) {
1101 ret = -EBUSY;
1102 goto out;
1104 cam->owner = filp;
1106 * Configure things if need be.
1108 if (test_bit(CF_CONFIG_NEEDED, &cam->flags)) {
1109 ret = viacam_configure_sensor(cam);
1110 if (ret)
1111 goto out;
1112 ret = viacam_config_controller(cam);
1113 if (ret)
1114 goto out;
1117 * If the CPU goes into C3, the DMA transfer gets corrupted and
1118 * users start filing unsightly bug reports. Put in a "latency"
1119 * requirement which will keep the CPU out of the deeper sleep
1120 * states.
1122 pm_qos_add_request(&cam->qos_request, PM_QOS_CPU_DMA_LATENCY, 50);
1124 * Fire things up.
1126 INIT_LIST_HEAD(&cam->buffer_queue);
1127 ret = videobuf_streamon(&cam->vb_queue);
1128 if (!ret)
1129 viacam_start_engine(cam);
1130 out:
1131 mutex_unlock(&cam->lock);
1132 return ret;
1135 static int viacam_streamoff(struct file *filp, void *priv, enum v4l2_buf_type t)
1137 struct via_camera *cam = priv;
1138 int ret;
1140 if (t != V4L2_BUF_TYPE_VIDEO_CAPTURE)
1141 return -EINVAL;
1142 mutex_lock(&cam->lock);
1143 if (cam->opstate != S_RUNNING) {
1144 ret = -EINVAL;
1145 goto out;
1147 pm_qos_remove_request(&cam->qos_request);
1148 viacam_stop_engine(cam);
1150 * Videobuf will recycle all of the outstanding buffers, but
1151 * we should be sure we don't retain any references to
1152 * any of them.
1154 ret = videobuf_streamoff(&cam->vb_queue);
1155 INIT_LIST_HEAD(&cam->buffer_queue);
1156 out:
1157 mutex_unlock(&cam->lock);
1158 return ret;
1161 /* G/S_PARM */
1163 static int viacam_g_parm(struct file *filp, void *priv,
1164 struct v4l2_streamparm *parm)
1166 struct via_camera *cam = priv;
1167 int ret;
1169 mutex_lock(&cam->lock);
1170 ret = sensor_call(cam, video, g_parm, parm);
1171 mutex_unlock(&cam->lock);
1172 parm->parm.capture.readbuffers = cam->n_cap_bufs;
1173 return ret;
1176 static int viacam_s_parm(struct file *filp, void *priv,
1177 struct v4l2_streamparm *parm)
1179 struct via_camera *cam = priv;
1180 int ret;
1182 mutex_lock(&cam->lock);
1183 ret = sensor_call(cam, video, s_parm, parm);
1184 mutex_unlock(&cam->lock);
1185 parm->parm.capture.readbuffers = cam->n_cap_bufs;
1186 return ret;
1189 static int viacam_enum_framesizes(struct file *filp, void *priv,
1190 struct v4l2_frmsizeenum *sizes)
1192 if (sizes->index != 0)
1193 return -EINVAL;
1194 sizes->type = V4L2_FRMSIZE_TYPE_CONTINUOUS;
1195 sizes->stepwise.min_width = QCIF_WIDTH;
1196 sizes->stepwise.min_height = QCIF_HEIGHT;
1197 sizes->stepwise.max_width = VGA_WIDTH;
1198 sizes->stepwise.max_height = VGA_HEIGHT;
1199 sizes->stepwise.step_width = sizes->stepwise.step_height = 1;
1200 return 0;
1203 static int viacam_enum_frameintervals(struct file *filp, void *priv,
1204 struct v4l2_frmivalenum *interval)
1206 struct via_camera *cam = priv;
1207 int ret;
1209 mutex_lock(&cam->lock);
1210 ret = sensor_call(cam, video, enum_frameintervals, interval);
1211 mutex_unlock(&cam->lock);
1212 return ret;
1217 static const struct v4l2_ioctl_ops viacam_ioctl_ops = {
1218 .vidioc_g_chip_ident = viacam_g_chip_ident,
1219 .vidioc_queryctrl = viacam_queryctrl,
1220 .vidioc_g_ctrl = viacam_g_ctrl,
1221 .vidioc_s_ctrl = viacam_s_ctrl,
1222 .vidioc_enum_input = viacam_enum_input,
1223 .vidioc_g_input = viacam_g_input,
1224 .vidioc_s_input = viacam_s_input,
1225 .vidioc_s_std = viacam_s_std,
1226 .vidioc_enum_fmt_vid_cap = viacam_enum_fmt_vid_cap,
1227 .vidioc_try_fmt_vid_cap = viacam_try_fmt_vid_cap,
1228 .vidioc_g_fmt_vid_cap = viacam_g_fmt_vid_cap,
1229 .vidioc_s_fmt_vid_cap = viacam_s_fmt_vid_cap,
1230 .vidioc_querycap = viacam_querycap,
1231 .vidioc_reqbufs = viacam_reqbufs,
1232 .vidioc_querybuf = viacam_querybuf,
1233 .vidioc_qbuf = viacam_qbuf,
1234 .vidioc_dqbuf = viacam_dqbuf,
1235 .vidioc_streamon = viacam_streamon,
1236 .vidioc_streamoff = viacam_streamoff,
1237 .vidioc_g_parm = viacam_g_parm,
1238 .vidioc_s_parm = viacam_s_parm,
1239 .vidioc_enum_framesizes = viacam_enum_framesizes,
1240 .vidioc_enum_frameintervals = viacam_enum_frameintervals,
1243 /*----------------------------------------------------------------------------*/
1246 * Power management.
1248 #ifdef CONFIG_PM
1250 static int viacam_suspend(void *priv)
1252 struct via_camera *cam = priv;
1253 enum viacam_opstate state = cam->opstate;
1255 if (cam->opstate != S_IDLE) {
1256 viacam_stop_engine(cam);
1257 cam->opstate = state; /* So resume restarts */
1260 return 0;
1263 static int viacam_resume(void *priv)
1265 struct via_camera *cam = priv;
1266 int ret = 0;
1269 * Get back to a reasonable operating state.
1271 via_write_reg_mask(VIASR, 0x78, 0, 0x80);
1272 via_write_reg_mask(VIASR, 0x1e, 0xc0, 0xc0);
1273 viacam_int_disable(cam);
1274 set_bit(CF_CONFIG_NEEDED, &cam->flags);
1276 * Make sure the sensor's power state is correct
1278 if (cam->users > 0)
1279 via_sensor_power_up(cam);
1280 else
1281 via_sensor_power_down(cam);
1283 * If it was operating, try to restart it.
1285 if (cam->opstate != S_IDLE) {
1286 mutex_lock(&cam->lock);
1287 ret = viacam_configure_sensor(cam);
1288 if (!ret)
1289 ret = viacam_config_controller(cam);
1290 mutex_unlock(&cam->lock);
1291 if (!ret)
1292 viacam_start_engine(cam);
1295 return ret;
1298 static struct viafb_pm_hooks viacam_pm_hooks = {
1299 .suspend = viacam_suspend,
1300 .resume = viacam_resume
1303 #endif /* CONFIG_PM */
1306 * Setup stuff.
1309 static struct video_device viacam_v4l_template = {
1310 .name = "via-camera",
1311 .minor = -1,
1312 .tvnorms = V4L2_STD_NTSC_M,
1313 .current_norm = V4L2_STD_NTSC_M,
1314 .fops = &viacam_fops,
1315 .ioctl_ops = &viacam_ioctl_ops,
1316 .release = video_device_release_empty, /* Check this */
1320 * The OLPC folks put the serial port on the same pin as
1321 * the camera. They also get grumpy if we break the
1322 * serial port and keep them from using it. So we have
1323 * to check the serial enable bit and not step on it.
1325 #define VIACAM_SERIAL_DEVFN 0x88
1326 #define VIACAM_SERIAL_CREG 0x46
1327 #define VIACAM_SERIAL_BIT 0x40
1329 static __devinit bool viacam_serial_is_enabled(void)
1331 struct pci_bus *pbus = pci_find_bus(0, 0);
1332 u8 cbyte;
1334 pci_bus_read_config_byte(pbus, VIACAM_SERIAL_DEVFN,
1335 VIACAM_SERIAL_CREG, &cbyte);
1336 if ((cbyte & VIACAM_SERIAL_BIT) == 0)
1337 return false; /* Not enabled */
1338 if (override_serial == 0) {
1339 printk(KERN_NOTICE "Via camera: serial port is enabled, " \
1340 "refusing to load.\n");
1341 printk(KERN_NOTICE "Specify override_serial=1 to force " \
1342 "module loading.\n");
1343 return true;
1345 printk(KERN_NOTICE "Via camera: overriding serial port\n");
1346 pci_bus_write_config_byte(pbus, VIACAM_SERIAL_DEVFN,
1347 VIACAM_SERIAL_CREG, cbyte & ~VIACAM_SERIAL_BIT);
1348 return false;
1351 static __devinit int viacam_probe(struct platform_device *pdev)
1353 int ret;
1354 struct i2c_adapter *sensor_adapter;
1355 struct viafb_dev *viadev = pdev->dev.platform_data;
1358 * Note that there are actually two capture channels on
1359 * the device. We only deal with one for now. That
1360 * is encoded here; nothing else assumes it's dealing with
1361 * a unique capture device.
1363 struct via_camera *cam;
1366 * Ensure that frame buffer memory has been set aside for
1367 * this purpose. As an arbitrary limit, refuse to work
1368 * with less than two frames of VGA 16-bit data.
1370 * If we ever support the second port, we'll need to set
1371 * aside more memory.
1373 if (viadev->camera_fbmem_size < (VGA_HEIGHT*VGA_WIDTH*4)) {
1374 printk(KERN_ERR "viacam: insufficient FB memory reserved\n");
1375 return -ENOMEM;
1377 if (viadev->engine_mmio == NULL) {
1378 printk(KERN_ERR "viacam: No I/O memory, so no pictures\n");
1379 return -ENOMEM;
1382 if (machine_is_olpc() && viacam_serial_is_enabled())
1383 return -EBUSY;
1386 * Basic structure initialization.
1388 cam = kzalloc (sizeof(struct via_camera), GFP_KERNEL);
1389 if (cam == NULL)
1390 return -ENOMEM;
1391 via_cam_info = cam;
1392 cam->platdev = pdev;
1393 cam->viadev = viadev;
1394 cam->users = 0;
1395 cam->owner = NULL;
1396 cam->opstate = S_IDLE;
1397 cam->user_format = cam->sensor_format = viacam_def_pix_format;
1398 mutex_init(&cam->lock);
1399 INIT_LIST_HEAD(&cam->buffer_queue);
1400 cam->mmio = viadev->engine_mmio;
1401 cam->fbmem = viadev->fbmem;
1402 cam->fb_offset = viadev->camera_fbmem_offset;
1403 cam->flags = 1 << CF_CONFIG_NEEDED;
1404 cam->mbus_code = via_def_mbus_code;
1406 * Tell V4L that we exist.
1408 ret = v4l2_device_register(&pdev->dev, &cam->v4l2_dev);
1409 if (ret) {
1410 dev_err(&pdev->dev, "Unable to register v4l2 device\n");
1411 return ret;
1414 * Convince the system that we can do DMA.
1416 pdev->dev.dma_mask = &viadev->pdev->dma_mask;
1417 dma_set_mask(&pdev->dev, 0xffffffff);
1419 * Fire up the capture port. The write to 0x78 looks purely
1420 * OLPCish; any system will need to tweak 0x1e.
1422 via_write_reg_mask(VIASR, 0x78, 0, 0x80);
1423 via_write_reg_mask(VIASR, 0x1e, 0xc0, 0xc0);
1425 * Get the sensor powered up.
1427 ret = via_sensor_power_setup(cam);
1428 if (ret)
1429 goto out_unregister;
1430 via_sensor_power_up(cam);
1433 * See if we can't find it on the bus. The VIA_PORT_31 assumption
1434 * is OLPC-specific. 0x42 assumption is ov7670-specific.
1436 sensor_adapter = viafb_find_i2c_adapter(VIA_PORT_31);
1437 cam->sensor = v4l2_i2c_new_subdev(&cam->v4l2_dev, sensor_adapter,
1438 "ov7670", 0x42 >> 1, NULL);
1439 if (cam->sensor == NULL) {
1440 dev_err(&pdev->dev, "Unable to find the sensor!\n");
1441 ret = -ENODEV;
1442 goto out_power_down;
1445 * Get the IRQ.
1447 viacam_int_disable(cam);
1448 ret = request_threaded_irq(viadev->pdev->irq, viacam_quick_irq,
1449 viacam_irq, IRQF_SHARED, "via-camera", cam);
1450 if (ret)
1451 goto out_power_down;
1453 * Tell V4l2 that we exist.
1455 cam->vdev = viacam_v4l_template;
1456 cam->vdev.v4l2_dev = &cam->v4l2_dev;
1457 ret = video_register_device(&cam->vdev, VFL_TYPE_GRABBER, -1);
1458 if (ret)
1459 goto out_irq;
1460 video_set_drvdata(&cam->vdev, cam);
1462 #ifdef CONFIG_PM
1464 * Hook into PM events
1466 viacam_pm_hooks.private = cam;
1467 viafb_pm_register(&viacam_pm_hooks);
1468 #endif
1470 /* Power the sensor down until somebody opens the device */
1471 via_sensor_power_down(cam);
1472 return 0;
1474 out_irq:
1475 free_irq(viadev->pdev->irq, cam);
1476 out_power_down:
1477 via_sensor_power_release(cam);
1478 out_unregister:
1479 v4l2_device_unregister(&cam->v4l2_dev);
1480 return ret;
1483 static __devexit int viacam_remove(struct platform_device *pdev)
1485 struct via_camera *cam = via_cam_info;
1486 struct viafb_dev *viadev = pdev->dev.platform_data;
1488 video_unregister_device(&cam->vdev);
1489 v4l2_device_unregister(&cam->v4l2_dev);
1490 free_irq(viadev->pdev->irq, cam);
1491 via_sensor_power_release(cam);
1492 via_cam_info = NULL;
1493 return 0;
1496 static struct platform_driver viacam_driver = {
1497 .driver = {
1498 .name = "viafb-camera",
1500 .probe = viacam_probe,
1501 .remove = viacam_remove,
1504 static int viacam_init(void)
1506 return platform_driver_register(&viacam_driver);
1508 module_init(viacam_init);
1510 static void viacam_exit(void)
1512 platform_driver_unregister(&viacam_driver);
1514 module_exit(viacam_exit);