coredump: use task comm instead of (unknown)
[linux-2.6/linux-acpi-2.6/ibm-acpi-2.6.git] / drivers / media / video / via-camera.c
blob85d3048c1d679dcc83cdf6a7afb8d0f6af1f15c2
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_ALIAS("platform:viafb-camera");
33 MODULE_AUTHOR("Jonathan Corbet <corbet@lwn.net>");
34 MODULE_DESCRIPTION("VIA framebuffer-based camera controller driver");
35 MODULE_LICENSE("GPL");
37 static int flip_image;
38 module_param(flip_image, bool, 0444);
39 MODULE_PARM_DESC(flip_image,
40 "If set, the sensor will be instructed to flip the image "
41 "vertically.");
43 static int override_serial;
44 module_param(override_serial, bool, 0444);
45 MODULE_PARM_DESC(override_serial,
46 "The camera driver will normally refuse to load if "
47 "the XO 1.5 serial port is enabled. Set this option "
48 "to force-enable the camera.");
51 * Basic window sizes.
53 #define VGA_WIDTH 640
54 #define VGA_HEIGHT 480
55 #define QCIF_WIDTH 176
56 #define QCIF_HEIGHT 144
59 * The structure describing our camera.
61 enum viacam_opstate { S_IDLE = 0, S_RUNNING = 1 };
63 struct via_camera {
64 struct v4l2_device v4l2_dev;
65 struct video_device vdev;
66 struct v4l2_subdev *sensor;
67 struct platform_device *platdev;
68 struct viafb_dev *viadev;
69 struct mutex lock;
70 enum viacam_opstate opstate;
71 unsigned long flags;
72 struct pm_qos_request_list qos_request;
74 * GPIO info for power/reset management
76 int power_gpio;
77 int reset_gpio;
79 * I/O memory stuff.
81 void __iomem *mmio; /* Where the registers live */
82 void __iomem *fbmem; /* Frame buffer memory */
83 u32 fb_offset; /* Reserved memory offset (FB) */
85 * Capture buffers and related. The controller supports
86 * up to three, so that's what we have here. These buffers
87 * live in frame buffer memory, so we don't call them "DMA".
89 unsigned int cb_offsets[3]; /* offsets into fb mem */
90 u8 *cb_addrs[3]; /* Kernel-space addresses */
91 int n_cap_bufs; /* How many are we using? */
92 int next_buf;
93 struct videobuf_queue vb_queue;
94 struct list_head buffer_queue; /* prot. by reg_lock */
96 * User tracking.
98 int users;
99 struct file *owner;
101 * Video format information. sensor_format is kept in a form
102 * that we can use to pass to the sensor. We always run the
103 * sensor in VGA resolution, though, and let the controller
104 * downscale things if need be. So we keep the "real*
105 * dimensions separately.
107 struct v4l2_pix_format sensor_format;
108 struct v4l2_pix_format user_format;
109 enum v4l2_mbus_pixelcode mbus_code;
113 * Yes, this is a hack, but there's only going to be one of these
114 * on any system we know of.
116 static struct via_camera *via_cam_info;
119 * Flag values, manipulated with bitops
121 #define CF_DMA_ACTIVE 0 /* A frame is incoming */
122 #define CF_CONFIG_NEEDED 1 /* Must configure hardware */
126 * Nasty ugly v4l2 boilerplate.
128 #define sensor_call(cam, optype, func, args...) \
129 v4l2_subdev_call(cam->sensor, optype, func, ##args)
132 * Debugging and related.
134 #define cam_err(cam, fmt, arg...) \
135 dev_err(&(cam)->platdev->dev, fmt, ##arg);
136 #define cam_warn(cam, fmt, arg...) \
137 dev_warn(&(cam)->platdev->dev, fmt, ##arg);
138 #define cam_dbg(cam, fmt, arg...) \
139 dev_dbg(&(cam)->platdev->dev, fmt, ##arg);
142 * Format handling. This is ripped almost directly from Hans's changes
143 * to cafe_ccic.c. It's a little unfortunate; until this change, we
144 * didn't need to know anything about the format except its byte depth;
145 * now this information must be managed at this level too.
147 static struct via_format {
148 __u8 *desc;
149 __u32 pixelformat;
150 int bpp; /* Bytes per pixel */
151 enum v4l2_mbus_pixelcode mbus_code;
152 } via_formats[] = {
154 .desc = "YUYV 4:2:2",
155 .pixelformat = V4L2_PIX_FMT_YUYV,
156 .mbus_code = V4L2_MBUS_FMT_YUYV8_2X8,
157 .bpp = 2,
160 .desc = "RGB 565",
161 .pixelformat = V4L2_PIX_FMT_RGB565,
162 .mbus_code = V4L2_MBUS_FMT_RGB565_2X8_LE,
163 .bpp = 2,
165 /* RGB444 and Bayer should be doable, but have never been
166 tested with this driver. */
168 #define N_VIA_FMTS ARRAY_SIZE(via_formats)
170 static struct via_format *via_find_format(u32 pixelformat)
172 unsigned i;
174 for (i = 0; i < N_VIA_FMTS; i++)
175 if (via_formats[i].pixelformat == pixelformat)
176 return via_formats + i;
177 /* Not found? Then return the first format. */
178 return via_formats;
182 /*--------------------------------------------------------------------------*/
184 * Sensor power/reset management. This piece is OLPC-specific for
185 * sure; other configurations will have things connected differently.
187 static int via_sensor_power_setup(struct via_camera *cam)
189 int ret;
191 cam->power_gpio = viafb_gpio_lookup("VGPIO3");
192 cam->reset_gpio = viafb_gpio_lookup("VGPIO2");
193 if (cam->power_gpio < 0 || cam->reset_gpio < 0) {
194 dev_err(&cam->platdev->dev, "Unable to find GPIO lines\n");
195 return -EINVAL;
197 ret = gpio_request(cam->power_gpio, "viafb-camera");
198 if (ret) {
199 dev_err(&cam->platdev->dev, "Unable to request power GPIO\n");
200 return ret;
202 ret = gpio_request(cam->reset_gpio, "viafb-camera");
203 if (ret) {
204 dev_err(&cam->platdev->dev, "Unable to request reset GPIO\n");
205 gpio_free(cam->power_gpio);
206 return ret;
208 gpio_direction_output(cam->power_gpio, 0);
209 gpio_direction_output(cam->reset_gpio, 0);
210 return 0;
214 * Power up the sensor and perform the reset dance.
216 static void via_sensor_power_up(struct via_camera *cam)
218 gpio_set_value(cam->power_gpio, 1);
219 gpio_set_value(cam->reset_gpio, 0);
220 msleep(20); /* Probably excessive */
221 gpio_set_value(cam->reset_gpio, 1);
222 msleep(20);
225 static void via_sensor_power_down(struct via_camera *cam)
227 gpio_set_value(cam->power_gpio, 0);
228 gpio_set_value(cam->reset_gpio, 0);
232 static void via_sensor_power_release(struct via_camera *cam)
234 via_sensor_power_down(cam);
235 gpio_free(cam->power_gpio);
236 gpio_free(cam->reset_gpio);
239 /* --------------------------------------------------------------------------*/
240 /* Sensor ops */
243 * Manage the ov7670 "flip" bit, which needs special help.
245 static int viacam_set_flip(struct via_camera *cam)
247 struct v4l2_control ctrl;
249 memset(&ctrl, 0, sizeof(ctrl));
250 ctrl.id = V4L2_CID_VFLIP;
251 ctrl.value = flip_image;
252 return sensor_call(cam, core, s_ctrl, &ctrl);
256 * Configure the sensor. It's up to the caller to ensure
257 * that the camera is in the correct operating state.
259 static int viacam_configure_sensor(struct via_camera *cam)
261 struct v4l2_mbus_framefmt mbus_fmt;
262 int ret;
264 v4l2_fill_mbus_format(&mbus_fmt, &cam->sensor_format, cam->mbus_code);
265 ret = sensor_call(cam, core, init, 0);
266 if (ret == 0)
267 ret = sensor_call(cam, video, s_mbus_fmt, &mbus_fmt);
269 * OV7670 does weird things if flip is set *before* format...
271 if (ret == 0)
272 ret = viacam_set_flip(cam);
273 return ret;
278 /* --------------------------------------------------------------------------*/
280 * Some simple register accessors; they assume that the lock is held.
282 * Should we want to support the second capture engine, we could
283 * hide the register difference by adding 0x1000 to registers in the
284 * 0x300-350 range.
286 static inline void viacam_write_reg(struct via_camera *cam,
287 int reg, int value)
289 iowrite32(value, cam->mmio + reg);
292 static inline int viacam_read_reg(struct via_camera *cam, int reg)
294 return ioread32(cam->mmio + reg);
297 static inline void viacam_write_reg_mask(struct via_camera *cam,
298 int reg, int value, int mask)
300 int tmp = viacam_read_reg(cam, reg);
302 tmp = (tmp & ~mask) | (value & mask);
303 viacam_write_reg(cam, reg, tmp);
307 /* --------------------------------------------------------------------------*/
308 /* Interrupt management and handling */
310 static irqreturn_t viacam_quick_irq(int irq, void *data)
312 struct via_camera *cam = data;
313 irqreturn_t ret = IRQ_NONE;
314 int icv;
317 * All we do here is to clear the interrupts and tell
318 * the handler thread to wake up.
320 spin_lock(&cam->viadev->reg_lock);
321 icv = viacam_read_reg(cam, VCR_INTCTRL);
322 if (icv & VCR_IC_EAV) {
323 icv |= VCR_IC_EAV|VCR_IC_EVBI|VCR_IC_FFULL;
324 viacam_write_reg(cam, VCR_INTCTRL, icv);
325 ret = IRQ_WAKE_THREAD;
327 spin_unlock(&cam->viadev->reg_lock);
328 return ret;
332 * Find the next videobuf buffer which has somebody waiting on it.
334 static struct videobuf_buffer *viacam_next_buffer(struct via_camera *cam)
336 unsigned long flags;
337 struct videobuf_buffer *buf = NULL;
339 spin_lock_irqsave(&cam->viadev->reg_lock, flags);
340 if (cam->opstate != S_RUNNING)
341 goto out;
342 if (list_empty(&cam->buffer_queue))
343 goto out;
344 buf = list_entry(cam->buffer_queue.next, struct videobuf_buffer, queue);
345 if (!waitqueue_active(&buf->done)) {/* Nobody waiting */
346 buf = NULL;
347 goto out;
349 list_del(&buf->queue);
350 buf->state = VIDEOBUF_ACTIVE;
351 out:
352 spin_unlock_irqrestore(&cam->viadev->reg_lock, flags);
353 return buf;
357 * The threaded IRQ handler.
359 static irqreturn_t viacam_irq(int irq, void *data)
361 int bufn;
362 struct videobuf_buffer *vb;
363 struct via_camera *cam = data;
364 struct videobuf_dmabuf *vdma;
367 * If there is no place to put the data frame, don't bother
368 * with anything else.
370 vb = viacam_next_buffer(cam);
371 if (vb == NULL)
372 goto done;
374 * Figure out which buffer we just completed.
376 bufn = (viacam_read_reg(cam, VCR_INTCTRL) & VCR_IC_ACTBUF) >> 3;
377 bufn -= 1;
378 if (bufn < 0)
379 bufn = cam->n_cap_bufs - 1;
381 * Copy over the data and let any waiters know.
383 vdma = videobuf_to_dma(vb);
384 viafb_dma_copy_out_sg(cam->cb_offsets[bufn], vdma->sglist, vdma->sglen);
385 vb->state = VIDEOBUF_DONE;
386 vb->size = cam->user_format.sizeimage;
387 wake_up(&vb->done);
388 done:
389 return IRQ_HANDLED;
394 * These functions must mess around with the general interrupt
395 * control register, which is relevant to much more than just the
396 * camera. Nothing else uses interrupts, though, as of this writing.
397 * Should that situation change, we'll have to improve support at
398 * the via-core level.
400 static void viacam_int_enable(struct via_camera *cam)
402 viacam_write_reg(cam, VCR_INTCTRL,
403 VCR_IC_INTEN|VCR_IC_EAV|VCR_IC_EVBI|VCR_IC_FFULL);
404 viafb_irq_enable(VDE_I_C0AVEN);
407 static void viacam_int_disable(struct via_camera *cam)
409 viafb_irq_disable(VDE_I_C0AVEN);
410 viacam_write_reg(cam, VCR_INTCTRL, 0);
415 /* --------------------------------------------------------------------------*/
416 /* Controller operations */
419 * Set up our capture buffers in framebuffer memory.
421 static int viacam_ctlr_cbufs(struct via_camera *cam)
423 int nbuf = cam->viadev->camera_fbmem_size/cam->sensor_format.sizeimage;
424 int i;
425 unsigned int offset;
428 * See how many buffers we can work with.
430 if (nbuf >= 3) {
431 cam->n_cap_bufs = 3;
432 viacam_write_reg_mask(cam, VCR_CAPINTC, VCR_CI_3BUFS,
433 VCR_CI_3BUFS);
434 } else if (nbuf == 2) {
435 cam->n_cap_bufs = 2;
436 viacam_write_reg_mask(cam, VCR_CAPINTC, 0, VCR_CI_3BUFS);
437 } else {
438 cam_warn(cam, "Insufficient frame buffer memory\n");
439 return -ENOMEM;
442 * Set them up.
444 offset = cam->fb_offset;
445 for (i = 0; i < cam->n_cap_bufs; i++) {
446 cam->cb_offsets[i] = offset;
447 cam->cb_addrs[i] = cam->fbmem + offset;
448 viacam_write_reg(cam, VCR_VBUF1 + i*4, offset & VCR_VBUF_MASK);
449 offset += cam->sensor_format.sizeimage;
451 return 0;
455 * Set the scaling register for downscaling the image.
457 * This register works like this... Vertical scaling is enabled
458 * by bit 26; if that bit is set, downscaling is controlled by the
459 * value in bits 16:25. Those bits are divided by 1024 to get
460 * the scaling factor; setting just bit 25 thus cuts the height
461 * in half.
463 * Horizontal scaling works about the same, but it's enabled by
464 * bit 11, with bits 0:10 giving the numerator of a fraction
465 * (over 2048) for the scaling value.
467 * This function is naive in that, if the user departs from
468 * the 3x4 VGA scaling factor, the image will distort. We
469 * could work around that if it really seemed important.
471 static void viacam_set_scale(struct via_camera *cam)
473 unsigned int avscale;
474 int sf;
476 if (cam->user_format.width == VGA_WIDTH)
477 avscale = 0;
478 else {
479 sf = (cam->user_format.width*2048)/VGA_WIDTH;
480 avscale = VCR_AVS_HEN | sf;
482 if (cam->user_format.height < VGA_HEIGHT) {
483 sf = (1024*cam->user_format.height)/VGA_HEIGHT;
484 avscale |= VCR_AVS_VEN | (sf << 16);
486 viacam_write_reg(cam, VCR_AVSCALE, avscale);
491 * Configure image-related information into the capture engine.
493 static void viacam_ctlr_image(struct via_camera *cam)
495 int cicreg;
498 * Disable clock before messing with stuff - from the via
499 * sample driver.
501 viacam_write_reg(cam, VCR_CAPINTC, ~(VCR_CI_ENABLE|VCR_CI_CLKEN));
503 * Set up the controller for VGA resolution, modulo magic
504 * offsets from the via sample driver.
506 viacam_write_reg(cam, VCR_HORRANGE, 0x06200120);
507 viacam_write_reg(cam, VCR_VERTRANGE, 0x01de0000);
508 viacam_set_scale(cam);
510 * Image size info.
512 viacam_write_reg(cam, VCR_MAXDATA,
513 (cam->sensor_format.height << 16) |
514 (cam->sensor_format.bytesperline >> 3));
515 viacam_write_reg(cam, VCR_MAXVBI, 0);
516 viacam_write_reg(cam, VCR_VSTRIDE,
517 cam->user_format.bytesperline & VCR_VS_STRIDE);
519 * Set up the capture interface control register,
520 * everything but the "go" bit.
522 * The FIFO threshold is a bit of a magic number; 8 is what
523 * VIA's sample code uses.
525 cicreg = VCR_CI_CLKEN |
526 0x08000000 | /* FIFO threshold */
527 VCR_CI_FLDINV | /* OLPC-specific? */
528 VCR_CI_VREFINV | /* OLPC-specific? */
529 VCR_CI_DIBOTH | /* Capture both fields */
530 VCR_CI_CCIR601_8;
531 if (cam->n_cap_bufs == 3)
532 cicreg |= VCR_CI_3BUFS;
534 * YUV formats need different byte swapping than RGB.
536 if (cam->user_format.pixelformat == V4L2_PIX_FMT_YUYV)
537 cicreg |= VCR_CI_YUYV;
538 else
539 cicreg |= VCR_CI_UYVY;
540 viacam_write_reg(cam, VCR_CAPINTC, cicreg);
544 static int viacam_config_controller(struct via_camera *cam)
546 int ret;
547 unsigned long flags;
549 spin_lock_irqsave(&cam->viadev->reg_lock, flags);
550 ret = viacam_ctlr_cbufs(cam);
551 if (!ret)
552 viacam_ctlr_image(cam);
553 spin_unlock_irqrestore(&cam->viadev->reg_lock, flags);
554 clear_bit(CF_CONFIG_NEEDED, &cam->flags);
555 return ret;
559 * Make it start grabbing data.
561 static void viacam_start_engine(struct via_camera *cam)
563 spin_lock_irq(&cam->viadev->reg_lock);
564 cam->next_buf = 0;
565 viacam_write_reg_mask(cam, VCR_CAPINTC, VCR_CI_ENABLE, VCR_CI_ENABLE);
566 viacam_int_enable(cam);
567 (void) viacam_read_reg(cam, VCR_CAPINTC); /* Force post */
568 cam->opstate = S_RUNNING;
569 spin_unlock_irq(&cam->viadev->reg_lock);
573 static void viacam_stop_engine(struct via_camera *cam)
575 spin_lock_irq(&cam->viadev->reg_lock);
576 viacam_int_disable(cam);
577 viacam_write_reg_mask(cam, VCR_CAPINTC, 0, VCR_CI_ENABLE);
578 (void) viacam_read_reg(cam, VCR_CAPINTC); /* Force post */
579 cam->opstate = S_IDLE;
580 spin_unlock_irq(&cam->viadev->reg_lock);
584 /* --------------------------------------------------------------------------*/
585 /* Videobuf callback ops */
588 * buffer_setup. The purpose of this one would appear to be to tell
589 * videobuf how big a single image is. It's also evidently up to us
590 * to put some sort of limit on the maximum number of buffers allowed.
592 static int viacam_vb_buf_setup(struct videobuf_queue *q,
593 unsigned int *count, unsigned int *size)
595 struct via_camera *cam = q->priv_data;
597 *size = cam->user_format.sizeimage;
598 if (*count == 0 || *count > 6) /* Arbitrary number */
599 *count = 6;
600 return 0;
604 * Prepare a buffer.
606 static int viacam_vb_buf_prepare(struct videobuf_queue *q,
607 struct videobuf_buffer *vb, enum v4l2_field field)
609 struct via_camera *cam = q->priv_data;
611 vb->size = cam->user_format.sizeimage;
612 vb->width = cam->user_format.width; /* bytesperline???? */
613 vb->height = cam->user_format.height;
614 vb->field = field;
615 if (vb->state == VIDEOBUF_NEEDS_INIT) {
616 int ret = videobuf_iolock(q, vb, NULL);
617 if (ret)
618 return ret;
620 vb->state = VIDEOBUF_PREPARED;
621 return 0;
625 * We've got a buffer to put data into.
627 * FIXME: check for a running engine and valid buffers?
629 static void viacam_vb_buf_queue(struct videobuf_queue *q,
630 struct videobuf_buffer *vb)
632 struct via_camera *cam = q->priv_data;
635 * Note that videobuf holds the lock when it calls
636 * us, so we need not (indeed, cannot) take it here.
638 vb->state = VIDEOBUF_QUEUED;
639 list_add_tail(&vb->queue, &cam->buffer_queue);
643 * Free a buffer.
645 static void viacam_vb_buf_release(struct videobuf_queue *q,
646 struct videobuf_buffer *vb)
648 struct via_camera *cam = q->priv_data;
650 videobuf_dma_unmap(&cam->platdev->dev, videobuf_to_dma(vb));
651 videobuf_dma_free(videobuf_to_dma(vb));
652 vb->state = VIDEOBUF_NEEDS_INIT;
655 static const struct videobuf_queue_ops viacam_vb_ops = {
656 .buf_setup = viacam_vb_buf_setup,
657 .buf_prepare = viacam_vb_buf_prepare,
658 .buf_queue = viacam_vb_buf_queue,
659 .buf_release = viacam_vb_buf_release,
662 /* --------------------------------------------------------------------------*/
663 /* File operations */
665 static int viacam_open(struct file *filp)
667 struct via_camera *cam = video_drvdata(filp);
669 filp->private_data = cam;
671 * Note the new user. If this is the first one, we'll also
672 * need to power up the sensor.
674 mutex_lock(&cam->lock);
675 if (cam->users == 0) {
676 int ret = viafb_request_dma();
678 if (ret) {
679 mutex_unlock(&cam->lock);
680 return ret;
682 via_sensor_power_up(cam);
683 set_bit(CF_CONFIG_NEEDED, &cam->flags);
685 * Hook into videobuf. Evidently this cannot fail.
687 videobuf_queue_sg_init(&cam->vb_queue, &viacam_vb_ops,
688 &cam->platdev->dev, &cam->viadev->reg_lock,
689 V4L2_BUF_TYPE_VIDEO_CAPTURE, V4L2_FIELD_NONE,
690 sizeof(struct videobuf_buffer), cam, NULL);
692 (cam->users)++;
693 mutex_unlock(&cam->lock);
694 return 0;
697 static int viacam_release(struct file *filp)
699 struct via_camera *cam = video_drvdata(filp);
701 mutex_lock(&cam->lock);
702 (cam->users)--;
704 * If the "owner" is closing, shut down any ongoing
705 * operations.
707 if (filp == cam->owner) {
708 videobuf_stop(&cam->vb_queue);
710 * We don't hold the spinlock here, but, if release()
711 * is being called by the owner, nobody else will
712 * be changing the state. And an extra stop would
713 * not hurt anyway.
715 if (cam->opstate != S_IDLE)
716 viacam_stop_engine(cam);
717 cam->owner = NULL;
720 * Last one out needs to turn out the lights.
722 if (cam->users == 0) {
723 videobuf_mmap_free(&cam->vb_queue);
724 via_sensor_power_down(cam);
725 viafb_release_dma();
727 mutex_unlock(&cam->lock);
728 return 0;
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);
738 int ret;
740 mutex_lock(&cam->lock);
742 * Enforce the V4l2 "only one owner gets to read data" rule.
744 if (cam->owner && cam->owner != filp) {
745 ret = -EBUSY;
746 goto out_unlock;
748 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);
754 if (!ret)
755 ret = viacam_config_controller(cam);
756 if (ret)
757 goto out_unlock;
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() ?? */
771 out_unlock:
772 mutex_unlock(&cam->lock);
773 return ret;
777 static unsigned int 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,
796 .open = viacam_open,
797 .release = viacam_release,
798 .read = viacam_read,
799 .poll = viacam_poll,
800 .mmap = viacam_mmap,
801 .unlocked_ioctl = video_ioctl2,
804 /*----------------------------------------------------------------------------*/
806 * The long list of v4l2 ioctl ops
809 static int viacam_g_chip_ident(struct file *file, void *priv,
810 struct v4l2_dbg_chip_ident *ident)
812 struct via_camera *cam = priv;
814 ident->ident = V4L2_IDENT_NONE;
815 ident->revision = 0;
816 if (v4l2_chip_match_host(&ident->match)) {
817 ident->ident = V4L2_IDENT_VIA_VX855;
818 return 0;
820 return sensor_call(cam, core, g_chip_ident, ident);
824 * Control ops are passed through to the sensor.
826 static int viacam_queryctrl(struct file *filp, void *priv,
827 struct v4l2_queryctrl *qc)
829 struct via_camera *cam = priv;
830 int ret;
832 mutex_lock(&cam->lock);
833 ret = sensor_call(cam, core, queryctrl, qc);
834 mutex_unlock(&cam->lock);
835 return ret;
839 static int viacam_g_ctrl(struct file *filp, void *priv,
840 struct v4l2_control *ctrl)
842 struct via_camera *cam = priv;
843 int ret;
845 mutex_lock(&cam->lock);
846 ret = sensor_call(cam, core, g_ctrl, ctrl);
847 mutex_unlock(&cam->lock);
848 return ret;
852 static int viacam_s_ctrl(struct file *filp, void *priv,
853 struct v4l2_control *ctrl)
855 struct via_camera *cam = priv;
856 int ret;
858 mutex_lock(&cam->lock);
859 ret = sensor_call(cam, core, s_ctrl, ctrl);
860 mutex_unlock(&cam->lock);
861 return ret;
865 * Only one input.
867 static int viacam_enum_input(struct file *filp, void *priv,
868 struct v4l2_input *input)
870 if (input->index != 0)
871 return -EINVAL;
873 input->type = V4L2_INPUT_TYPE_CAMERA;
874 input->std = V4L2_STD_ALL; /* Not sure what should go here */
875 strcpy(input->name, "Camera");
876 return 0;
879 static int viacam_g_input(struct file *filp, void *priv, unsigned int *i)
881 *i = 0;
882 return 0;
885 static int viacam_s_input(struct file *filp, void *priv, unsigned int i)
887 if (i != 0)
888 return -EINVAL;
889 return 0;
892 static int viacam_s_std(struct file *filp, void *priv, v4l2_std_id *std)
894 return 0;
898 * Video format stuff. Here is our default format until
899 * user space messes with things.
901 static const struct v4l2_pix_format viacam_def_pix_format = {
902 .width = VGA_WIDTH,
903 .height = VGA_HEIGHT,
904 .pixelformat = V4L2_PIX_FMT_YUYV,
905 .field = V4L2_FIELD_NONE,
906 .bytesperline = VGA_WIDTH * 2,
907 .sizeimage = VGA_WIDTH * VGA_HEIGHT * 2,
910 static const enum v4l2_mbus_pixelcode via_def_mbus_code = V4L2_MBUS_FMT_YUYV8_2X8;
912 static int viacam_enum_fmt_vid_cap(struct file *filp, void *priv,
913 struct v4l2_fmtdesc *fmt)
915 if (fmt->index >= N_VIA_FMTS)
916 return -EINVAL;
917 strlcpy(fmt->description, via_formats[fmt->index].desc,
918 sizeof(fmt->description));
919 fmt->pixelformat = via_formats[fmt->index].pixelformat;
920 return 0;
924 * Figure out proper image dimensions, but always force the
925 * sensor to VGA.
927 static void viacam_fmt_pre(struct v4l2_pix_format *userfmt,
928 struct v4l2_pix_format *sensorfmt)
930 *sensorfmt = *userfmt;
931 if (userfmt->width < QCIF_WIDTH || userfmt->height < QCIF_HEIGHT) {
932 userfmt->width = QCIF_WIDTH;
933 userfmt->height = QCIF_HEIGHT;
935 if (userfmt->width > VGA_WIDTH || userfmt->height > VGA_HEIGHT) {
936 userfmt->width = VGA_WIDTH;
937 userfmt->height = VGA_HEIGHT;
939 sensorfmt->width = VGA_WIDTH;
940 sensorfmt->height = VGA_HEIGHT;
943 static void viacam_fmt_post(struct v4l2_pix_format *userfmt,
944 struct v4l2_pix_format *sensorfmt)
946 struct via_format *f = via_find_format(userfmt->pixelformat);
948 sensorfmt->bytesperline = sensorfmt->width * f->bpp;
949 sensorfmt->sizeimage = sensorfmt->height * sensorfmt->bytesperline;
950 userfmt->pixelformat = sensorfmt->pixelformat;
951 userfmt->field = sensorfmt->field;
952 userfmt->bytesperline = 2 * userfmt->width;
953 userfmt->sizeimage = userfmt->bytesperline * userfmt->height;
958 * The real work of figuring out a workable format.
960 static int viacam_do_try_fmt(struct via_camera *cam,
961 struct v4l2_pix_format *upix, struct v4l2_pix_format *spix)
963 int ret;
964 struct v4l2_mbus_framefmt mbus_fmt;
965 struct via_format *f = via_find_format(upix->pixelformat);
967 upix->pixelformat = f->pixelformat;
968 viacam_fmt_pre(upix, spix);
969 v4l2_fill_mbus_format(&mbus_fmt, upix, f->mbus_code);
970 ret = sensor_call(cam, video, try_mbus_fmt, &mbus_fmt);
971 v4l2_fill_pix_format(spix, &mbus_fmt);
972 viacam_fmt_post(upix, spix);
973 return ret;
978 static int viacam_try_fmt_vid_cap(struct file *filp, void *priv,
979 struct v4l2_format *fmt)
981 struct via_camera *cam = priv;
982 struct v4l2_format sfmt;
983 int ret;
985 mutex_lock(&cam->lock);
986 ret = viacam_do_try_fmt(cam, &fmt->fmt.pix, &sfmt.fmt.pix);
987 mutex_unlock(&cam->lock);
988 return ret;
992 static int viacam_g_fmt_vid_cap(struct file *filp, void *priv,
993 struct v4l2_format *fmt)
995 struct via_camera *cam = priv;
997 mutex_lock(&cam->lock);
998 fmt->fmt.pix = cam->user_format;
999 mutex_unlock(&cam->lock);
1000 return 0;
1003 static int viacam_s_fmt_vid_cap(struct file *filp, void *priv,
1004 struct v4l2_format *fmt)
1006 struct via_camera *cam = priv;
1007 int ret;
1008 struct v4l2_format sfmt;
1009 struct via_format *f = via_find_format(fmt->fmt.pix.pixelformat);
1012 * Camera must be idle or we can't mess with the
1013 * video setup.
1015 mutex_lock(&cam->lock);
1016 if (cam->opstate != S_IDLE) {
1017 ret = -EBUSY;
1018 goto out;
1021 * Let the sensor code look over and tweak the
1022 * requested formatting.
1024 ret = viacam_do_try_fmt(cam, &fmt->fmt.pix, &sfmt.fmt.pix);
1025 if (ret)
1026 goto out;
1028 * OK, let's commit to the new format.
1030 cam->user_format = fmt->fmt.pix;
1031 cam->sensor_format = sfmt.fmt.pix;
1032 cam->mbus_code = f->mbus_code;
1033 ret = viacam_configure_sensor(cam);
1034 if (!ret)
1035 ret = viacam_config_controller(cam);
1036 out:
1037 mutex_unlock(&cam->lock);
1038 return ret;
1041 static int viacam_querycap(struct file *filp, void *priv,
1042 struct v4l2_capability *cap)
1044 strcpy(cap->driver, "via-camera");
1045 strcpy(cap->card, "via-camera");
1046 cap->version = 1;
1047 cap->capabilities = V4L2_CAP_VIDEO_CAPTURE |
1048 V4L2_CAP_READWRITE | V4L2_CAP_STREAMING;
1049 return 0;
1053 * Streaming operations - pure videobuf stuff.
1055 static int viacam_reqbufs(struct file *filp, void *priv,
1056 struct v4l2_requestbuffers *rb)
1058 struct via_camera *cam = priv;
1060 return videobuf_reqbufs(&cam->vb_queue, rb);
1063 static int viacam_querybuf(struct file *filp, void *priv,
1064 struct v4l2_buffer *buf)
1066 struct via_camera *cam = priv;
1068 return videobuf_querybuf(&cam->vb_queue, buf);
1071 static int viacam_qbuf(struct file *filp, void *priv, struct v4l2_buffer *buf)
1073 struct via_camera *cam = priv;
1075 return videobuf_qbuf(&cam->vb_queue, buf);
1078 static int viacam_dqbuf(struct file *filp, void *priv, struct v4l2_buffer *buf)
1080 struct via_camera *cam = priv;
1082 return videobuf_dqbuf(&cam->vb_queue, buf, filp->f_flags & O_NONBLOCK);
1085 static int viacam_streamon(struct file *filp, void *priv, enum v4l2_buf_type t)
1087 struct via_camera *cam = priv;
1088 int ret = 0;
1090 if (t != V4L2_BUF_TYPE_VIDEO_CAPTURE)
1091 return -EINVAL;
1093 mutex_lock(&cam->lock);
1094 if (cam->opstate != S_IDLE) {
1095 ret = -EBUSY;
1096 goto out;
1099 * Enforce the V4l2 "only one owner gets to read data" rule.
1101 if (cam->owner && cam->owner != filp) {
1102 ret = -EBUSY;
1103 goto out;
1105 cam->owner = filp;
1107 * Configure things if need be.
1109 if (test_bit(CF_CONFIG_NEEDED, &cam->flags)) {
1110 ret = viacam_configure_sensor(cam);
1111 if (ret)
1112 goto out;
1113 ret = viacam_config_controller(cam);
1114 if (ret)
1115 goto out;
1118 * If the CPU goes into C3, the DMA transfer gets corrupted and
1119 * users start filing unsightly bug reports. Put in a "latency"
1120 * requirement which will keep the CPU out of the deeper sleep
1121 * states.
1123 pm_qos_add_request(&cam->qos_request, PM_QOS_CPU_DMA_LATENCY, 50);
1125 * Fire things up.
1127 INIT_LIST_HEAD(&cam->buffer_queue);
1128 ret = videobuf_streamon(&cam->vb_queue);
1129 if (!ret)
1130 viacam_start_engine(cam);
1131 out:
1132 mutex_unlock(&cam->lock);
1133 return ret;
1136 static int viacam_streamoff(struct file *filp, void *priv, enum v4l2_buf_type t)
1138 struct via_camera *cam = priv;
1139 int ret;
1141 if (t != V4L2_BUF_TYPE_VIDEO_CAPTURE)
1142 return -EINVAL;
1143 mutex_lock(&cam->lock);
1144 if (cam->opstate != S_RUNNING) {
1145 ret = -EINVAL;
1146 goto out;
1148 pm_qos_remove_request(&cam->qos_request);
1149 viacam_stop_engine(cam);
1151 * Videobuf will recycle all of the outstanding buffers, but
1152 * we should be sure we don't retain any references to
1153 * any of them.
1155 ret = videobuf_streamoff(&cam->vb_queue);
1156 INIT_LIST_HEAD(&cam->buffer_queue);
1157 out:
1158 mutex_unlock(&cam->lock);
1159 return ret;
1162 /* G/S_PARM */
1164 static int viacam_g_parm(struct file *filp, void *priv,
1165 struct v4l2_streamparm *parm)
1167 struct via_camera *cam = priv;
1168 int ret;
1170 mutex_lock(&cam->lock);
1171 ret = sensor_call(cam, video, g_parm, parm);
1172 mutex_unlock(&cam->lock);
1173 parm->parm.capture.readbuffers = cam->n_cap_bufs;
1174 return ret;
1177 static int viacam_s_parm(struct file *filp, void *priv,
1178 struct v4l2_streamparm *parm)
1180 struct via_camera *cam = priv;
1181 int ret;
1183 mutex_lock(&cam->lock);
1184 ret = sensor_call(cam, video, s_parm, parm);
1185 mutex_unlock(&cam->lock);
1186 parm->parm.capture.readbuffers = cam->n_cap_bufs;
1187 return ret;
1190 static int viacam_enum_framesizes(struct file *filp, void *priv,
1191 struct v4l2_frmsizeenum *sizes)
1193 if (sizes->index != 0)
1194 return -EINVAL;
1195 sizes->type = V4L2_FRMSIZE_TYPE_CONTINUOUS;
1196 sizes->stepwise.min_width = QCIF_WIDTH;
1197 sizes->stepwise.min_height = QCIF_HEIGHT;
1198 sizes->stepwise.max_width = VGA_WIDTH;
1199 sizes->stepwise.max_height = VGA_HEIGHT;
1200 sizes->stepwise.step_width = sizes->stepwise.step_height = 1;
1201 return 0;
1204 static int viacam_enum_frameintervals(struct file *filp, void *priv,
1205 struct v4l2_frmivalenum *interval)
1207 struct via_camera *cam = priv;
1208 int ret;
1210 mutex_lock(&cam->lock);
1211 ret = sensor_call(cam, video, enum_frameintervals, interval);
1212 mutex_unlock(&cam->lock);
1213 return ret;
1218 static const struct v4l2_ioctl_ops viacam_ioctl_ops = {
1219 .vidioc_g_chip_ident = viacam_g_chip_ident,
1220 .vidioc_queryctrl = viacam_queryctrl,
1221 .vidioc_g_ctrl = viacam_g_ctrl,
1222 .vidioc_s_ctrl = viacam_s_ctrl,
1223 .vidioc_enum_input = viacam_enum_input,
1224 .vidioc_g_input = viacam_g_input,
1225 .vidioc_s_input = viacam_s_input,
1226 .vidioc_s_std = viacam_s_std,
1227 .vidioc_enum_fmt_vid_cap = viacam_enum_fmt_vid_cap,
1228 .vidioc_try_fmt_vid_cap = viacam_try_fmt_vid_cap,
1229 .vidioc_g_fmt_vid_cap = viacam_g_fmt_vid_cap,
1230 .vidioc_s_fmt_vid_cap = viacam_s_fmt_vid_cap,
1231 .vidioc_querycap = viacam_querycap,
1232 .vidioc_reqbufs = viacam_reqbufs,
1233 .vidioc_querybuf = viacam_querybuf,
1234 .vidioc_qbuf = viacam_qbuf,
1235 .vidioc_dqbuf = viacam_dqbuf,
1236 .vidioc_streamon = viacam_streamon,
1237 .vidioc_streamoff = viacam_streamoff,
1238 .vidioc_g_parm = viacam_g_parm,
1239 .vidioc_s_parm = viacam_s_parm,
1240 .vidioc_enum_framesizes = viacam_enum_framesizes,
1241 .vidioc_enum_frameintervals = viacam_enum_frameintervals,
1244 /*----------------------------------------------------------------------------*/
1247 * Power management.
1249 #ifdef CONFIG_PM
1251 static int viacam_suspend(void *priv)
1253 struct via_camera *cam = priv;
1254 enum viacam_opstate state = cam->opstate;
1256 if (cam->opstate != S_IDLE) {
1257 viacam_stop_engine(cam);
1258 cam->opstate = state; /* So resume restarts */
1261 return 0;
1264 static int viacam_resume(void *priv)
1266 struct via_camera *cam = priv;
1267 int ret = 0;
1270 * Get back to a reasonable operating state.
1272 via_write_reg_mask(VIASR, 0x78, 0, 0x80);
1273 via_write_reg_mask(VIASR, 0x1e, 0xc0, 0xc0);
1274 viacam_int_disable(cam);
1275 set_bit(CF_CONFIG_NEEDED, &cam->flags);
1277 * Make sure the sensor's power state is correct
1279 if (cam->users > 0)
1280 via_sensor_power_up(cam);
1281 else
1282 via_sensor_power_down(cam);
1284 * If it was operating, try to restart it.
1286 if (cam->opstate != S_IDLE) {
1287 mutex_lock(&cam->lock);
1288 ret = viacam_configure_sensor(cam);
1289 if (!ret)
1290 ret = viacam_config_controller(cam);
1291 mutex_unlock(&cam->lock);
1292 if (!ret)
1293 viacam_start_engine(cam);
1296 return ret;
1299 static struct viafb_pm_hooks viacam_pm_hooks = {
1300 .suspend = viacam_suspend,
1301 .resume = viacam_resume
1304 #endif /* CONFIG_PM */
1307 * Setup stuff.
1310 static struct video_device viacam_v4l_template = {
1311 .name = "via-camera",
1312 .minor = -1,
1313 .tvnorms = V4L2_STD_NTSC_M,
1314 .current_norm = V4L2_STD_NTSC_M,
1315 .fops = &viacam_fops,
1316 .ioctl_ops = &viacam_ioctl_ops,
1317 .release = video_device_release_empty, /* Check this */
1321 * The OLPC folks put the serial port on the same pin as
1322 * the camera. They also get grumpy if we break the
1323 * serial port and keep them from using it. So we have
1324 * to check the serial enable bit and not step on it.
1326 #define VIACAM_SERIAL_DEVFN 0x88
1327 #define VIACAM_SERIAL_CREG 0x46
1328 #define VIACAM_SERIAL_BIT 0x40
1330 static __devinit bool viacam_serial_is_enabled(void)
1332 struct pci_bus *pbus = pci_find_bus(0, 0);
1333 u8 cbyte;
1335 pci_bus_read_config_byte(pbus, VIACAM_SERIAL_DEVFN,
1336 VIACAM_SERIAL_CREG, &cbyte);
1337 if ((cbyte & VIACAM_SERIAL_BIT) == 0)
1338 return false; /* Not enabled */
1339 if (override_serial == 0) {
1340 printk(KERN_NOTICE "Via camera: serial port is enabled, " \
1341 "refusing to load.\n");
1342 printk(KERN_NOTICE "Specify override_serial=1 to force " \
1343 "module loading.\n");
1344 return true;
1346 printk(KERN_NOTICE "Via camera: overriding serial port\n");
1347 pci_bus_write_config_byte(pbus, VIACAM_SERIAL_DEVFN,
1348 VIACAM_SERIAL_CREG, cbyte & ~VIACAM_SERIAL_BIT);
1349 return false;
1352 static __devinit int viacam_probe(struct platform_device *pdev)
1354 int ret;
1355 struct i2c_adapter *sensor_adapter;
1356 struct viafb_dev *viadev = pdev->dev.platform_data;
1359 * Note that there are actually two capture channels on
1360 * the device. We only deal with one for now. That
1361 * is encoded here; nothing else assumes it's dealing with
1362 * a unique capture device.
1364 struct via_camera *cam;
1367 * Ensure that frame buffer memory has been set aside for
1368 * this purpose. As an arbitrary limit, refuse to work
1369 * with less than two frames of VGA 16-bit data.
1371 * If we ever support the second port, we'll need to set
1372 * aside more memory.
1374 if (viadev->camera_fbmem_size < (VGA_HEIGHT*VGA_WIDTH*4)) {
1375 printk(KERN_ERR "viacam: insufficient FB memory reserved\n");
1376 return -ENOMEM;
1378 if (viadev->engine_mmio == NULL) {
1379 printk(KERN_ERR "viacam: No I/O memory, so no pictures\n");
1380 return -ENOMEM;
1383 if (machine_is_olpc() && viacam_serial_is_enabled())
1384 return -EBUSY;
1387 * Basic structure initialization.
1389 cam = kzalloc (sizeof(struct via_camera), GFP_KERNEL);
1390 if (cam == NULL)
1391 return -ENOMEM;
1392 via_cam_info = cam;
1393 cam->platdev = pdev;
1394 cam->viadev = viadev;
1395 cam->users = 0;
1396 cam->owner = NULL;
1397 cam->opstate = S_IDLE;
1398 cam->user_format = cam->sensor_format = viacam_def_pix_format;
1399 mutex_init(&cam->lock);
1400 INIT_LIST_HEAD(&cam->buffer_queue);
1401 cam->mmio = viadev->engine_mmio;
1402 cam->fbmem = viadev->fbmem;
1403 cam->fb_offset = viadev->camera_fbmem_offset;
1404 cam->flags = 1 << CF_CONFIG_NEEDED;
1405 cam->mbus_code = via_def_mbus_code;
1407 * Tell V4L that we exist.
1409 ret = v4l2_device_register(&pdev->dev, &cam->v4l2_dev);
1410 if (ret) {
1411 dev_err(&pdev->dev, "Unable to register v4l2 device\n");
1412 return ret;
1415 * Convince the system that we can do DMA.
1417 pdev->dev.dma_mask = &viadev->pdev->dma_mask;
1418 dma_set_mask(&pdev->dev, 0xffffffff);
1420 * Fire up the capture port. The write to 0x78 looks purely
1421 * OLPCish; any system will need to tweak 0x1e.
1423 via_write_reg_mask(VIASR, 0x78, 0, 0x80);
1424 via_write_reg_mask(VIASR, 0x1e, 0xc0, 0xc0);
1426 * Get the sensor powered up.
1428 ret = via_sensor_power_setup(cam);
1429 if (ret)
1430 goto out_unregister;
1431 via_sensor_power_up(cam);
1434 * See if we can't find it on the bus. The VIA_PORT_31 assumption
1435 * is OLPC-specific. 0x42 assumption is ov7670-specific.
1437 sensor_adapter = viafb_find_i2c_adapter(VIA_PORT_31);
1438 cam->sensor = v4l2_i2c_new_subdev(&cam->v4l2_dev, sensor_adapter,
1439 "ov7670", 0x42 >> 1, NULL);
1440 if (cam->sensor == NULL) {
1441 dev_err(&pdev->dev, "Unable to find the sensor!\n");
1442 ret = -ENODEV;
1443 goto out_power_down;
1446 * Get the IRQ.
1448 viacam_int_disable(cam);
1449 ret = request_threaded_irq(viadev->pdev->irq, viacam_quick_irq,
1450 viacam_irq, IRQF_SHARED, "via-camera", cam);
1451 if (ret)
1452 goto out_power_down;
1454 * Tell V4l2 that we exist.
1456 cam->vdev = viacam_v4l_template;
1457 cam->vdev.v4l2_dev = &cam->v4l2_dev;
1458 ret = video_register_device(&cam->vdev, VFL_TYPE_GRABBER, -1);
1459 if (ret)
1460 goto out_irq;
1461 video_set_drvdata(&cam->vdev, cam);
1463 #ifdef CONFIG_PM
1465 * Hook into PM events
1467 viacam_pm_hooks.private = cam;
1468 viafb_pm_register(&viacam_pm_hooks);
1469 #endif
1471 /* Power the sensor down until somebody opens the device */
1472 via_sensor_power_down(cam);
1473 return 0;
1475 out_irq:
1476 free_irq(viadev->pdev->irq, cam);
1477 out_power_down:
1478 via_sensor_power_release(cam);
1479 out_unregister:
1480 v4l2_device_unregister(&cam->v4l2_dev);
1481 return ret;
1484 static __devexit int viacam_remove(struct platform_device *pdev)
1486 struct via_camera *cam = via_cam_info;
1487 struct viafb_dev *viadev = pdev->dev.platform_data;
1489 video_unregister_device(&cam->vdev);
1490 v4l2_device_unregister(&cam->v4l2_dev);
1491 free_irq(viadev->pdev->irq, cam);
1492 via_sensor_power_release(cam);
1493 via_cam_info = NULL;
1494 return 0;
1497 static struct platform_driver viacam_driver = {
1498 .driver = {
1499 .name = "viafb-camera",
1501 .probe = viacam_probe,
1502 .remove = viacam_remove,
1505 static int viacam_init(void)
1507 return platform_driver_register(&viacam_driver);
1509 module_init(viacam_init);
1511 static void viacam_exit(void)
1513 platform_driver_unregister(&viacam_driver);
1515 module_exit(viacam_exit);