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
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/pci.h>
17 #include <linux/platform_device.h>
18 #include <linux/videodev2.h>
19 #include <media/v4l2-device.h>
20 #include <media/v4l2-ioctl.h>
21 #include <media/v4l2-chip-ident.h>
22 #include <media/videobuf-dma-sg.h>
23 #include <linux/device.h>
24 #include <linux/delay.h>
25 #include <linux/dma-mapping.h>
26 #include <linux/pm_qos_params.h>
27 #include <linux/via-core.h>
28 #include <linux/via-gpio.h>
29 #include <linux/via_i2c.h>
31 #include "via-camera.h"
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 "
43 #ifdef CONFIG_OLPC_XO_1_5
44 static int 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 "
48 "the XO 1.5 serial port is enabled. Set this option "
49 "to force the issue.");
56 #define VGA_HEIGHT 480
57 #define QCIF_WIDTH 176
58 #define QCIF_HEIGHT 144
61 * The structure describing our camera.
63 enum viacam_opstate
{ S_IDLE
= 0, S_RUNNING
= 1 };
66 struct v4l2_device v4l2_dev
;
67 struct video_device vdev
;
68 struct v4l2_subdev
*sensor
;
69 struct platform_device
*platdev
;
70 struct viafb_dev
*viadev
;
72 enum viacam_opstate opstate
;
74 struct pm_qos_request_list qos_request
;
76 * GPIO info for power/reset management
83 void __iomem
*mmio
; /* Where the registers live */
84 void __iomem
*fbmem
; /* Frame buffer memory */
85 u32 fb_offset
; /* Reserved memory offset (FB) */
87 * Capture buffers and related. The controller supports
88 * up to three, so that's what we have here. These buffers
89 * live in frame buffer memory, so we don't call them "DMA".
91 unsigned int cb_offsets
[3]; /* offsets into fb mem */
92 u8
*cb_addrs
[3]; /* Kernel-space addresses */
93 int n_cap_bufs
; /* How many are we using? */
95 struct videobuf_queue vb_queue
;
96 struct list_head buffer_queue
; /* prot. by reg_lock */
103 * Video format information. sensor_format is kept in a form
104 * that we can use to pass to the sensor. We always run the
105 * sensor in VGA resolution, though, and let the controller
106 * downscale things if need be. So we keep the "real*
107 * dimensions separately.
109 struct v4l2_pix_format sensor_format
;
110 struct v4l2_pix_format user_format
;
111 enum v4l2_mbus_pixelcode mbus_code
;
115 * Yes, this is a hack, but there's only going to be one of these
116 * on any system we know of.
118 static struct via_camera
*via_cam_info
;
121 * Flag values, manipulated with bitops
123 #define CF_DMA_ACTIVE 0 /* A frame is incoming */
124 #define CF_CONFIG_NEEDED 1 /* Must configure hardware */
128 * Nasty ugly v4l2 boilerplate.
130 #define sensor_call(cam, optype, func, args...) \
131 v4l2_subdev_call(cam->sensor, optype, func, ##args)
134 * Debugging and related.
136 #define cam_err(cam, fmt, arg...) \
137 dev_err(&(cam)->platdev->dev, fmt, ##arg);
138 #define cam_warn(cam, fmt, arg...) \
139 dev_warn(&(cam)->platdev->dev, fmt, ##arg);
140 #define cam_dbg(cam, fmt, arg...) \
141 dev_dbg(&(cam)->platdev->dev, fmt, ##arg);
144 * Format handling. This is ripped almost directly from Hans's changes
145 * to cafe_ccic.c. It's a little unfortunate; until this change, we
146 * didn't need to know anything about the format except its byte depth;
147 * now this information must be managed at this level too.
149 static struct via_format
{
152 int bpp
; /* Bytes per pixel */
153 enum v4l2_mbus_pixelcode mbus_code
;
156 .desc
= "YUYV 4:2:2",
157 .pixelformat
= V4L2_PIX_FMT_YUYV
,
158 .mbus_code
= V4L2_MBUS_FMT_YUYV8_2X8
,
163 .pixelformat
= V4L2_PIX_FMT_RGB565
,
164 .mbus_code
= V4L2_MBUS_FMT_RGB565_2X8_LE
,
167 /* RGB444 and Bayer should be doable, but have never been
168 tested with this driver. */
170 #define N_VIA_FMTS ARRAY_SIZE(via_formats)
172 static struct via_format
*via_find_format(u32 pixelformat
)
176 for (i
= 0; i
< N_VIA_FMTS
; i
++)
177 if (via_formats
[i
].pixelformat
== pixelformat
)
178 return via_formats
+ i
;
179 /* Not found? Then return the first format. */
184 /*--------------------------------------------------------------------------*/
186 * Sensor power/reset management. This piece is OLPC-specific for
187 * sure; other configurations will have things connected differently.
189 static int via_sensor_power_setup(struct via_camera
*cam
)
193 cam
->power_gpio
= viafb_gpio_lookup("VGPIO3");
194 cam
->reset_gpio
= viafb_gpio_lookup("VGPIO2");
195 if (cam
->power_gpio
< 0 || cam
->reset_gpio
< 0) {
196 dev_err(&cam
->platdev
->dev
, "Unable to find GPIO lines\n");
199 ret
= gpio_request(cam
->power_gpio
, "viafb-camera");
201 dev_err(&cam
->platdev
->dev
, "Unable to request power GPIO\n");
204 ret
= gpio_request(cam
->reset_gpio
, "viafb-camera");
206 dev_err(&cam
->platdev
->dev
, "Unable to request reset GPIO\n");
207 gpio_free(cam
->power_gpio
);
210 gpio_direction_output(cam
->power_gpio
, 0);
211 gpio_direction_output(cam
->reset_gpio
, 0);
216 * Power up the sensor and perform the reset dance.
218 static void via_sensor_power_up(struct via_camera
*cam
)
220 gpio_set_value(cam
->power_gpio
, 1);
221 gpio_set_value(cam
->reset_gpio
, 0);
222 msleep(20); /* Probably excessive */
223 gpio_set_value(cam
->reset_gpio
, 1);
227 static void via_sensor_power_down(struct via_camera
*cam
)
229 gpio_set_value(cam
->power_gpio
, 0);
230 gpio_set_value(cam
->reset_gpio
, 0);
234 static void via_sensor_power_release(struct via_camera
*cam
)
236 via_sensor_power_down(cam
);
237 gpio_free(cam
->power_gpio
);
238 gpio_free(cam
->reset_gpio
);
241 /* --------------------------------------------------------------------------*/
245 * Manage the ov7670 "flip" bit, which needs special help.
247 static int viacam_set_flip(struct via_camera
*cam
)
249 struct v4l2_control ctrl
;
251 memset(&ctrl
, 0, sizeof(ctrl
));
252 ctrl
.id
= V4L2_CID_VFLIP
;
253 ctrl
.value
= flip_image
;
254 return sensor_call(cam
, core
, s_ctrl
, &ctrl
);
258 * Configure the sensor. It's up to the caller to ensure
259 * that the camera is in the correct operating state.
261 static int viacam_configure_sensor(struct via_camera
*cam
)
263 struct v4l2_mbus_framefmt mbus_fmt
;
266 v4l2_fill_mbus_format(&mbus_fmt
, &cam
->sensor_format
, cam
->mbus_code
);
267 ret
= sensor_call(cam
, core
, init
, 0);
269 ret
= sensor_call(cam
, video
, s_mbus_fmt
, &mbus_fmt
);
271 * OV7670 does weird things if flip is set *before* format...
274 ret
= viacam_set_flip(cam
);
280 /* --------------------------------------------------------------------------*/
282 * Some simple register accessors; they assume that the lock is held.
284 * Should we want to support the second capture engine, we could
285 * hide the register difference by adding 0x1000 to registers in the
288 static inline void viacam_write_reg(struct via_camera
*cam
,
291 iowrite32(value
, cam
->mmio
+ reg
);
294 static inline int viacam_read_reg(struct via_camera
*cam
, int reg
)
296 return ioread32(cam
->mmio
+ reg
);
299 static inline void viacam_write_reg_mask(struct via_camera
*cam
,
300 int reg
, int value
, int mask
)
302 int tmp
= viacam_read_reg(cam
, reg
);
304 tmp
= (tmp
& ~mask
) | (value
& mask
);
305 viacam_write_reg(cam
, reg
, tmp
);
309 /* --------------------------------------------------------------------------*/
310 /* Interrupt management and handling */
312 static irqreturn_t
viacam_quick_irq(int irq
, void *data
)
314 struct via_camera
*cam
= data
;
315 irqreturn_t ret
= IRQ_NONE
;
319 * All we do here is to clear the interrupts and tell
320 * the handler thread to wake up.
322 spin_lock(&cam
->viadev
->reg_lock
);
323 icv
= viacam_read_reg(cam
, VCR_INTCTRL
);
324 if (icv
& VCR_IC_EAV
) {
325 icv
|= VCR_IC_EAV
|VCR_IC_EVBI
|VCR_IC_FFULL
;
326 viacam_write_reg(cam
, VCR_INTCTRL
, icv
);
327 ret
= IRQ_WAKE_THREAD
;
329 spin_unlock(&cam
->viadev
->reg_lock
);
334 * Find the next videobuf buffer which has somebody waiting on it.
336 static struct videobuf_buffer
*viacam_next_buffer(struct via_camera
*cam
)
339 struct videobuf_buffer
*buf
= NULL
;
341 spin_lock_irqsave(&cam
->viadev
->reg_lock
, flags
);
342 if (cam
->opstate
!= S_RUNNING
)
344 if (list_empty(&cam
->buffer_queue
))
346 buf
= list_entry(cam
->buffer_queue
.next
, struct videobuf_buffer
, queue
);
347 if (!waitqueue_active(&buf
->done
)) {/* Nobody waiting */
351 list_del(&buf
->queue
);
352 buf
->state
= VIDEOBUF_ACTIVE
;
354 spin_unlock_irqrestore(&cam
->viadev
->reg_lock
, flags
);
359 * The threaded IRQ handler.
361 static irqreturn_t
viacam_irq(int irq
, void *data
)
364 struct videobuf_buffer
*vb
;
365 struct via_camera
*cam
= data
;
366 struct videobuf_dmabuf
*vdma
;
369 * If there is no place to put the data frame, don't bother
370 * with anything else.
372 vb
= viacam_next_buffer(cam
);
376 * Figure out which buffer we just completed.
378 bufn
= (viacam_read_reg(cam
, VCR_INTCTRL
) & VCR_IC_ACTBUF
) >> 3;
381 bufn
= cam
->n_cap_bufs
- 1;
383 * Copy over the data and let any waiters know.
385 vdma
= videobuf_to_dma(vb
);
386 viafb_dma_copy_out_sg(cam
->cb_offsets
[bufn
], vdma
->sglist
, vdma
->sglen
);
387 vb
->state
= VIDEOBUF_DONE
;
388 vb
->size
= cam
->user_format
.sizeimage
;
396 * These functions must mess around with the general interrupt
397 * control register, which is relevant to much more than just the
398 * camera. Nothing else uses interrupts, though, as of this writing.
399 * Should that situation change, we'll have to improve support at
400 * the via-core level.
402 static void viacam_int_enable(struct via_camera
*cam
)
404 viacam_write_reg(cam
, VCR_INTCTRL
,
405 VCR_IC_INTEN
|VCR_IC_EAV
|VCR_IC_EVBI
|VCR_IC_FFULL
);
406 viafb_irq_enable(VDE_I_C0AVEN
);
409 static void viacam_int_disable(struct via_camera
*cam
)
411 viafb_irq_disable(VDE_I_C0AVEN
);
412 viacam_write_reg(cam
, VCR_INTCTRL
, 0);
417 /* --------------------------------------------------------------------------*/
418 /* Controller operations */
421 * Set up our capture buffers in framebuffer memory.
423 static int viacam_ctlr_cbufs(struct via_camera
*cam
)
425 int nbuf
= cam
->viadev
->camera_fbmem_size
/cam
->sensor_format
.sizeimage
;
430 * See how many buffers we can work with.
434 viacam_write_reg_mask(cam
, VCR_CAPINTC
, VCR_CI_3BUFS
,
436 } else if (nbuf
== 2) {
438 viacam_write_reg_mask(cam
, VCR_CAPINTC
, 0, VCR_CI_3BUFS
);
440 cam_warn(cam
, "Insufficient frame buffer memory\n");
446 offset
= cam
->fb_offset
;
447 for (i
= 0; i
< cam
->n_cap_bufs
; i
++) {
448 cam
->cb_offsets
[i
] = offset
;
449 cam
->cb_addrs
[i
] = cam
->fbmem
+ offset
;
450 viacam_write_reg(cam
, VCR_VBUF1
+ i
*4, offset
& VCR_VBUF_MASK
);
451 offset
+= cam
->sensor_format
.sizeimage
;
457 * Set the scaling register for downscaling the image.
459 * This register works like this... Vertical scaling is enabled
460 * by bit 26; if that bit is set, downscaling is controlled by the
461 * value in bits 16:25. Those bits are divided by 1024 to get
462 * the scaling factor; setting just bit 25 thus cuts the height
465 * Horizontal scaling works about the same, but it's enabled by
466 * bit 11, with bits 0:10 giving the numerator of a fraction
467 * (over 2048) for the scaling value.
469 * This function is naive in that, if the user departs from
470 * the 3x4 VGA scaling factor, the image will distort. We
471 * could work around that if it really seemed important.
473 static void viacam_set_scale(struct via_camera
*cam
)
475 unsigned int avscale
;
478 if (cam
->user_format
.width
== VGA_WIDTH
)
481 sf
= (cam
->user_format
.width
*2048)/VGA_WIDTH
;
482 avscale
= VCR_AVS_HEN
| sf
;
484 if (cam
->user_format
.height
< VGA_HEIGHT
) {
485 sf
= (1024*cam
->user_format
.height
)/VGA_HEIGHT
;
486 avscale
|= VCR_AVS_VEN
| (sf
<< 16);
488 viacam_write_reg(cam
, VCR_AVSCALE
, avscale
);
493 * Configure image-related information into the capture engine.
495 static void viacam_ctlr_image(struct via_camera
*cam
)
500 * Disable clock before messing with stuff - from the via
503 viacam_write_reg(cam
, VCR_CAPINTC
, ~(VCR_CI_ENABLE
|VCR_CI_CLKEN
));
505 * Set up the controller for VGA resolution, modulo magic
506 * offsets from the via sample driver.
508 viacam_write_reg(cam
, VCR_HORRANGE
, 0x06200120);
509 viacam_write_reg(cam
, VCR_VERTRANGE
, 0x01de0000);
510 viacam_set_scale(cam
);
514 viacam_write_reg(cam
, VCR_MAXDATA
,
515 (cam
->sensor_format
.height
<< 16) |
516 (cam
->sensor_format
.bytesperline
>> 3));
517 viacam_write_reg(cam
, VCR_MAXVBI
, 0);
518 viacam_write_reg(cam
, VCR_VSTRIDE
,
519 cam
->user_format
.bytesperline
& VCR_VS_STRIDE
);
521 * Set up the capture interface control register,
522 * everything but the "go" bit.
524 * The FIFO threshold is a bit of a magic number; 8 is what
525 * VIA's sample code uses.
527 cicreg
= VCR_CI_CLKEN
|
528 0x08000000 | /* FIFO threshold */
529 VCR_CI_FLDINV
| /* OLPC-specific? */
530 VCR_CI_VREFINV
| /* OLPC-specific? */
531 VCR_CI_DIBOTH
| /* Capture both fields */
533 if (cam
->n_cap_bufs
== 3)
534 cicreg
|= VCR_CI_3BUFS
;
536 * YUV formats need different byte swapping than RGB.
538 if (cam
->user_format
.pixelformat
== V4L2_PIX_FMT_YUYV
)
539 cicreg
|= VCR_CI_YUYV
;
541 cicreg
|= VCR_CI_UYVY
;
542 viacam_write_reg(cam
, VCR_CAPINTC
, cicreg
);
546 static int viacam_config_controller(struct via_camera
*cam
)
551 spin_lock_irqsave(&cam
->viadev
->reg_lock
, flags
);
552 ret
= viacam_ctlr_cbufs(cam
);
554 viacam_ctlr_image(cam
);
555 spin_unlock_irqrestore(&cam
->viadev
->reg_lock
, flags
);
556 clear_bit(CF_CONFIG_NEEDED
, &cam
->flags
);
561 * Make it start grabbing data.
563 static void viacam_start_engine(struct via_camera
*cam
)
565 spin_lock_irq(&cam
->viadev
->reg_lock
);
567 viacam_write_reg_mask(cam
, VCR_CAPINTC
, VCR_CI_ENABLE
, VCR_CI_ENABLE
);
568 viacam_int_enable(cam
);
569 (void) viacam_read_reg(cam
, VCR_CAPINTC
); /* Force post */
570 cam
->opstate
= S_RUNNING
;
571 spin_unlock_irq(&cam
->viadev
->reg_lock
);
575 static void viacam_stop_engine(struct via_camera
*cam
)
577 spin_lock_irq(&cam
->viadev
->reg_lock
);
578 viacam_int_disable(cam
);
579 viacam_write_reg_mask(cam
, VCR_CAPINTC
, 0, VCR_CI_ENABLE
);
580 (void) viacam_read_reg(cam
, VCR_CAPINTC
); /* Force post */
581 cam
->opstate
= S_IDLE
;
582 spin_unlock_irq(&cam
->viadev
->reg_lock
);
586 /* --------------------------------------------------------------------------*/
587 /* Videobuf callback ops */
590 * buffer_setup. The purpose of this one would appear to be to tell
591 * videobuf how big a single image is. It's also evidently up to us
592 * to put some sort of limit on the maximum number of buffers allowed.
594 static int viacam_vb_buf_setup(struct videobuf_queue
*q
,
595 unsigned int *count
, unsigned int *size
)
597 struct via_camera
*cam
= q
->priv_data
;
599 *size
= cam
->user_format
.sizeimage
;
600 if (*count
== 0 || *count
> 6) /* Arbitrary number */
608 static int viacam_vb_buf_prepare(struct videobuf_queue
*q
,
609 struct videobuf_buffer
*vb
, enum v4l2_field field
)
611 struct via_camera
*cam
= q
->priv_data
;
613 vb
->size
= cam
->user_format
.sizeimage
;
614 vb
->width
= cam
->user_format
.width
; /* bytesperline???? */
615 vb
->height
= cam
->user_format
.height
;
617 if (vb
->state
== VIDEOBUF_NEEDS_INIT
) {
618 int ret
= videobuf_iolock(q
, vb
, NULL
);
622 vb
->state
= VIDEOBUF_PREPARED
;
627 * We've got a buffer to put data into.
629 * FIXME: check for a running engine and valid buffers?
631 static void viacam_vb_buf_queue(struct videobuf_queue
*q
,
632 struct videobuf_buffer
*vb
)
634 struct via_camera
*cam
= q
->priv_data
;
637 * Note that videobuf holds the lock when it calls
638 * us, so we need not (indeed, cannot) take it here.
640 vb
->state
= VIDEOBUF_QUEUED
;
641 list_add_tail(&vb
->queue
, &cam
->buffer_queue
);
647 static void viacam_vb_buf_release(struct videobuf_queue
*q
,
648 struct videobuf_buffer
*vb
)
650 struct via_camera
*cam
= q
->priv_data
;
652 videobuf_dma_unmap(&cam
->platdev
->dev
, videobuf_to_dma(vb
));
653 videobuf_dma_free(videobuf_to_dma(vb
));
654 vb
->state
= VIDEOBUF_NEEDS_INIT
;
657 static const struct videobuf_queue_ops viacam_vb_ops
= {
658 .buf_setup
= viacam_vb_buf_setup
,
659 .buf_prepare
= viacam_vb_buf_prepare
,
660 .buf_queue
= viacam_vb_buf_queue
,
661 .buf_release
= viacam_vb_buf_release
,
664 /* --------------------------------------------------------------------------*/
665 /* File operations */
667 static int viacam_open(struct file
*filp
)
669 struct via_camera
*cam
= video_drvdata(filp
);
671 filp
->private_data
= cam
;
673 * Note the new user. If this is the first one, we'll also
674 * need to power up the sensor.
676 mutex_lock(&cam
->lock
);
677 if (cam
->users
== 0) {
678 int ret
= viafb_request_dma();
681 mutex_unlock(&cam
->lock
);
684 via_sensor_power_up(cam
);
685 set_bit(CF_CONFIG_NEEDED
, &cam
->flags
);
687 * Hook into videobuf. Evidently this cannot fail.
689 videobuf_queue_sg_init(&cam
->vb_queue
, &viacam_vb_ops
,
690 &cam
->platdev
->dev
, &cam
->viadev
->reg_lock
,
691 V4L2_BUF_TYPE_VIDEO_CAPTURE
, V4L2_FIELD_NONE
,
692 sizeof(struct videobuf_buffer
), cam
, NULL
);
695 mutex_unlock(&cam
->lock
);
699 static int viacam_release(struct file
*filp
)
701 struct via_camera
*cam
= video_drvdata(filp
);
703 mutex_lock(&cam
->lock
);
706 * If the "owner" is closing, shut down any ongoing
709 if (filp
== cam
->owner
) {
710 videobuf_stop(&cam
->vb_queue
);
712 * We don't hold the spinlock here, but, if release()
713 * is being called by the owner, nobody else will
714 * be changing the state. And an extra stop would
717 if (cam
->opstate
!= S_IDLE
)
718 viacam_stop_engine(cam
);
722 * Last one out needs to turn out the lights.
724 if (cam
->users
== 0) {
725 videobuf_mmap_free(&cam
->vb_queue
);
726 via_sensor_power_down(cam
);
729 mutex_unlock(&cam
->lock
);
734 * Read a frame from the device.
736 static ssize_t
viacam_read(struct file
*filp
, char __user
*buffer
,
737 size_t len
, loff_t
*pos
)
739 struct via_camera
*cam
= video_drvdata(filp
);
742 mutex_lock(&cam
->lock
);
744 * Enforce the V4l2 "only one owner gets to read data" rule.
746 if (cam
->owner
&& cam
->owner
!= filp
) {
752 * Do we need to configure the hardware?
754 if (test_bit(CF_CONFIG_NEEDED
, &cam
->flags
)) {
755 ret
= viacam_configure_sensor(cam
);
757 ret
= viacam_config_controller(cam
);
762 * Fire up the capture engine, then have videobuf do
763 * the heavy lifting. Someday it would be good to avoid
764 * stopping and restarting the engine each time.
766 INIT_LIST_HEAD(&cam
->buffer_queue
);
767 viacam_start_engine(cam
);
768 ret
= videobuf_read_stream(&cam
->vb_queue
, buffer
, len
, pos
, 0,
769 filp
->f_flags
& O_NONBLOCK
);
770 viacam_stop_engine(cam
);
771 /* videobuf_stop() ?? */
774 mutex_unlock(&cam
->lock
);
779 static unsigned int viacam_poll(struct file
*filp
, struct poll_table_struct
*pt
)
781 struct via_camera
*cam
= video_drvdata(filp
);
783 return videobuf_poll_stream(filp
, &cam
->vb_queue
, pt
);
787 static int viacam_mmap(struct file
*filp
, struct vm_area_struct
*vma
)
789 struct via_camera
*cam
= video_drvdata(filp
);
791 return videobuf_mmap_mapper(&cam
->vb_queue
, vma
);
796 static const struct v4l2_file_operations viacam_fops
= {
797 .owner
= THIS_MODULE
,
799 .release
= viacam_release
,
803 .unlocked_ioctl
= video_ioctl2
,
806 /*----------------------------------------------------------------------------*/
808 * The long list of v4l2 ioctl ops
811 static int viacam_g_chip_ident(struct file
*file
, void *priv
,
812 struct v4l2_dbg_chip_ident
*ident
)
814 struct via_camera
*cam
= priv
;
816 ident
->ident
= V4L2_IDENT_NONE
;
818 if (v4l2_chip_match_host(&ident
->match
)) {
819 ident
->ident
= V4L2_IDENT_VIA_VX855
;
822 return sensor_call(cam
, core
, g_chip_ident
, ident
);
826 * Control ops are passed through to the sensor.
828 static int viacam_queryctrl(struct file
*filp
, void *priv
,
829 struct v4l2_queryctrl
*qc
)
831 struct via_camera
*cam
= priv
;
834 mutex_lock(&cam
->lock
);
835 ret
= sensor_call(cam
, core
, queryctrl
, qc
);
836 mutex_unlock(&cam
->lock
);
841 static int viacam_g_ctrl(struct file
*filp
, void *priv
,
842 struct v4l2_control
*ctrl
)
844 struct via_camera
*cam
= priv
;
847 mutex_lock(&cam
->lock
);
848 ret
= sensor_call(cam
, core
, g_ctrl
, ctrl
);
849 mutex_unlock(&cam
->lock
);
854 static int viacam_s_ctrl(struct file
*filp
, void *priv
,
855 struct v4l2_control
*ctrl
)
857 struct via_camera
*cam
= priv
;
860 mutex_lock(&cam
->lock
);
861 ret
= sensor_call(cam
, core
, s_ctrl
, ctrl
);
862 mutex_unlock(&cam
->lock
);
869 static int viacam_enum_input(struct file
*filp
, void *priv
,
870 struct v4l2_input
*input
)
872 if (input
->index
!= 0)
875 input
->type
= V4L2_INPUT_TYPE_CAMERA
;
876 input
->std
= V4L2_STD_ALL
; /* Not sure what should go here */
877 strcpy(input
->name
, "Camera");
881 static int viacam_g_input(struct file
*filp
, void *priv
, unsigned int *i
)
887 static int viacam_s_input(struct file
*filp
, void *priv
, unsigned int i
)
894 static int viacam_s_std(struct file
*filp
, void *priv
, v4l2_std_id
*std
)
900 * Video format stuff. Here is our default format until
901 * user space messes with things.
903 static const struct v4l2_pix_format viacam_def_pix_format
= {
905 .height
= VGA_HEIGHT
,
906 .pixelformat
= V4L2_PIX_FMT_YUYV
,
907 .field
= V4L2_FIELD_NONE
,
908 .bytesperline
= VGA_WIDTH
* 2,
909 .sizeimage
= VGA_WIDTH
* VGA_HEIGHT
* 2,
912 static const enum v4l2_mbus_pixelcode via_def_mbus_code
= V4L2_MBUS_FMT_YUYV8_2X8
;
914 static int viacam_enum_fmt_vid_cap(struct file
*filp
, void *priv
,
915 struct v4l2_fmtdesc
*fmt
)
917 if (fmt
->index
>= N_VIA_FMTS
)
919 strlcpy(fmt
->description
, via_formats
[fmt
->index
].desc
,
920 sizeof(fmt
->description
));
921 fmt
->pixelformat
= via_formats
[fmt
->index
].pixelformat
;
926 * Figure out proper image dimensions, but always force the
929 static void viacam_fmt_pre(struct v4l2_pix_format
*userfmt
,
930 struct v4l2_pix_format
*sensorfmt
)
932 *sensorfmt
= *userfmt
;
933 if (userfmt
->width
< QCIF_WIDTH
|| userfmt
->height
< QCIF_HEIGHT
) {
934 userfmt
->width
= QCIF_WIDTH
;
935 userfmt
->height
= QCIF_HEIGHT
;
937 if (userfmt
->width
> VGA_WIDTH
|| userfmt
->height
> VGA_HEIGHT
) {
938 userfmt
->width
= VGA_WIDTH
;
939 userfmt
->height
= VGA_HEIGHT
;
941 sensorfmt
->width
= VGA_WIDTH
;
942 sensorfmt
->height
= VGA_HEIGHT
;
945 static void viacam_fmt_post(struct v4l2_pix_format
*userfmt
,
946 struct v4l2_pix_format
*sensorfmt
)
948 struct via_format
*f
= via_find_format(userfmt
->pixelformat
);
950 sensorfmt
->bytesperline
= sensorfmt
->width
* f
->bpp
;
951 sensorfmt
->sizeimage
= sensorfmt
->height
* sensorfmt
->bytesperline
;
952 userfmt
->pixelformat
= sensorfmt
->pixelformat
;
953 userfmt
->field
= sensorfmt
->field
;
954 userfmt
->bytesperline
= 2 * userfmt
->width
;
955 userfmt
->sizeimage
= userfmt
->bytesperline
* userfmt
->height
;
960 * The real work of figuring out a workable format.
962 static int viacam_do_try_fmt(struct via_camera
*cam
,
963 struct v4l2_pix_format
*upix
, struct v4l2_pix_format
*spix
)
966 struct v4l2_mbus_framefmt mbus_fmt
;
967 struct via_format
*f
= via_find_format(upix
->pixelformat
);
969 upix
->pixelformat
= f
->pixelformat
;
970 viacam_fmt_pre(upix
, spix
);
971 v4l2_fill_mbus_format(&mbus_fmt
, upix
, f
->mbus_code
);
972 ret
= sensor_call(cam
, video
, try_mbus_fmt
, &mbus_fmt
);
973 v4l2_fill_pix_format(spix
, &mbus_fmt
);
974 viacam_fmt_post(upix
, spix
);
980 static int viacam_try_fmt_vid_cap(struct file
*filp
, void *priv
,
981 struct v4l2_format
*fmt
)
983 struct via_camera
*cam
= priv
;
984 struct v4l2_format sfmt
;
987 mutex_lock(&cam
->lock
);
988 ret
= viacam_do_try_fmt(cam
, &fmt
->fmt
.pix
, &sfmt
.fmt
.pix
);
989 mutex_unlock(&cam
->lock
);
994 static int viacam_g_fmt_vid_cap(struct file
*filp
, void *priv
,
995 struct v4l2_format
*fmt
)
997 struct via_camera
*cam
= priv
;
999 mutex_lock(&cam
->lock
);
1000 fmt
->fmt
.pix
= cam
->user_format
;
1001 mutex_unlock(&cam
->lock
);
1005 static int viacam_s_fmt_vid_cap(struct file
*filp
, void *priv
,
1006 struct v4l2_format
*fmt
)
1008 struct via_camera
*cam
= priv
;
1010 struct v4l2_format sfmt
;
1011 struct via_format
*f
= via_find_format(fmt
->fmt
.pix
.pixelformat
);
1014 * Camera must be idle or we can't mess with the
1017 mutex_lock(&cam
->lock
);
1018 if (cam
->opstate
!= S_IDLE
) {
1023 * Let the sensor code look over and tweak the
1024 * requested formatting.
1026 ret
= viacam_do_try_fmt(cam
, &fmt
->fmt
.pix
, &sfmt
.fmt
.pix
);
1030 * OK, let's commit to the new format.
1032 cam
->user_format
= fmt
->fmt
.pix
;
1033 cam
->sensor_format
= sfmt
.fmt
.pix
;
1034 cam
->mbus_code
= f
->mbus_code
;
1035 ret
= viacam_configure_sensor(cam
);
1037 ret
= viacam_config_controller(cam
);
1039 mutex_unlock(&cam
->lock
);
1043 static int viacam_querycap(struct file
*filp
, void *priv
,
1044 struct v4l2_capability
*cap
)
1046 strcpy(cap
->driver
, "via-camera");
1047 strcpy(cap
->card
, "via-camera");
1049 cap
->capabilities
= V4L2_CAP_VIDEO_CAPTURE
|
1050 V4L2_CAP_READWRITE
| V4L2_CAP_STREAMING
;
1055 * Streaming operations - pure videobuf stuff.
1057 static int viacam_reqbufs(struct file
*filp
, void *priv
,
1058 struct v4l2_requestbuffers
*rb
)
1060 struct via_camera
*cam
= priv
;
1062 return videobuf_reqbufs(&cam
->vb_queue
, rb
);
1065 static int viacam_querybuf(struct file
*filp
, void *priv
,
1066 struct v4l2_buffer
*buf
)
1068 struct via_camera
*cam
= priv
;
1070 return videobuf_querybuf(&cam
->vb_queue
, buf
);
1073 static int viacam_qbuf(struct file
*filp
, void *priv
, struct v4l2_buffer
*buf
)
1075 struct via_camera
*cam
= priv
;
1077 return videobuf_qbuf(&cam
->vb_queue
, buf
);
1080 static int viacam_dqbuf(struct file
*filp
, void *priv
, struct v4l2_buffer
*buf
)
1082 struct via_camera
*cam
= priv
;
1084 return videobuf_dqbuf(&cam
->vb_queue
, buf
, filp
->f_flags
& O_NONBLOCK
);
1087 static int viacam_streamon(struct file
*filp
, void *priv
, enum v4l2_buf_type t
)
1089 struct via_camera
*cam
= priv
;
1092 if (t
!= V4L2_BUF_TYPE_VIDEO_CAPTURE
)
1095 mutex_lock(&cam
->lock
);
1096 if (cam
->opstate
!= S_IDLE
) {
1101 * Enforce the V4l2 "only one owner gets to read data" rule.
1103 if (cam
->owner
&& cam
->owner
!= filp
) {
1109 * Configure things if need be.
1111 if (test_bit(CF_CONFIG_NEEDED
, &cam
->flags
)) {
1112 ret
= viacam_configure_sensor(cam
);
1115 ret
= viacam_config_controller(cam
);
1120 * If the CPU goes into C3, the DMA transfer gets corrupted and
1121 * users start filing unsightly bug reports. Put in a "latency"
1122 * requirement which will keep the CPU out of the deeper sleep
1125 pm_qos_add_request(&cam
->qos_request
, PM_QOS_CPU_DMA_LATENCY
, 50);
1129 INIT_LIST_HEAD(&cam
->buffer_queue
);
1130 ret
= videobuf_streamon(&cam
->vb_queue
);
1132 viacam_start_engine(cam
);
1134 mutex_unlock(&cam
->lock
);
1138 static int viacam_streamoff(struct file
*filp
, void *priv
, enum v4l2_buf_type t
)
1140 struct via_camera
*cam
= priv
;
1143 if (t
!= V4L2_BUF_TYPE_VIDEO_CAPTURE
)
1145 mutex_lock(&cam
->lock
);
1146 if (cam
->opstate
!= S_RUNNING
) {
1150 pm_qos_remove_request(&cam
->qos_request
);
1151 viacam_stop_engine(cam
);
1153 * Videobuf will recycle all of the outstanding buffers, but
1154 * we should be sure we don't retain any references to
1157 ret
= videobuf_streamoff(&cam
->vb_queue
);
1158 INIT_LIST_HEAD(&cam
->buffer_queue
);
1160 mutex_unlock(&cam
->lock
);
1164 #ifdef CONFIG_VIDEO_V4L1_COMPAT
1165 static int viacam_vidiocgmbuf(struct file
*filp
, void *priv
,
1166 struct video_mbuf
*mbuf
)
1168 struct via_camera
*cam
= priv
;
1170 return videobuf_cgmbuf(&cam
->vb_queue
, mbuf
, 6);
1176 static int viacam_g_parm(struct file
*filp
, void *priv
,
1177 struct v4l2_streamparm
*parm
)
1179 struct via_camera
*cam
= priv
;
1182 mutex_lock(&cam
->lock
);
1183 ret
= sensor_call(cam
, video
, g_parm
, parm
);
1184 mutex_unlock(&cam
->lock
);
1185 parm
->parm
.capture
.readbuffers
= cam
->n_cap_bufs
;
1189 static int viacam_s_parm(struct file
*filp
, void *priv
,
1190 struct v4l2_streamparm
*parm
)
1192 struct via_camera
*cam
= priv
;
1195 mutex_lock(&cam
->lock
);
1196 ret
= sensor_call(cam
, video
, s_parm
, parm
);
1197 mutex_unlock(&cam
->lock
);
1198 parm
->parm
.capture
.readbuffers
= cam
->n_cap_bufs
;
1202 static int viacam_enum_framesizes(struct file
*filp
, void *priv
,
1203 struct v4l2_frmsizeenum
*sizes
)
1205 if (sizes
->index
!= 0)
1207 sizes
->type
= V4L2_FRMSIZE_TYPE_CONTINUOUS
;
1208 sizes
->stepwise
.min_width
= QCIF_WIDTH
;
1209 sizes
->stepwise
.min_height
= QCIF_HEIGHT
;
1210 sizes
->stepwise
.max_width
= VGA_WIDTH
;
1211 sizes
->stepwise
.max_height
= VGA_HEIGHT
;
1212 sizes
->stepwise
.step_width
= sizes
->stepwise
.step_height
= 1;
1216 static int viacam_enum_frameintervals(struct file
*filp
, void *priv
,
1217 struct v4l2_frmivalenum
*interval
)
1219 struct via_camera
*cam
= priv
;
1222 mutex_lock(&cam
->lock
);
1223 ret
= sensor_call(cam
, video
, enum_frameintervals
, interval
);
1224 mutex_unlock(&cam
->lock
);
1230 static const struct v4l2_ioctl_ops viacam_ioctl_ops
= {
1231 .vidioc_g_chip_ident
= viacam_g_chip_ident
,
1232 .vidioc_queryctrl
= viacam_queryctrl
,
1233 .vidioc_g_ctrl
= viacam_g_ctrl
,
1234 .vidioc_s_ctrl
= viacam_s_ctrl
,
1235 .vidioc_enum_input
= viacam_enum_input
,
1236 .vidioc_g_input
= viacam_g_input
,
1237 .vidioc_s_input
= viacam_s_input
,
1238 .vidioc_s_std
= viacam_s_std
,
1239 .vidioc_enum_fmt_vid_cap
= viacam_enum_fmt_vid_cap
,
1240 .vidioc_try_fmt_vid_cap
= viacam_try_fmt_vid_cap
,
1241 .vidioc_g_fmt_vid_cap
= viacam_g_fmt_vid_cap
,
1242 .vidioc_s_fmt_vid_cap
= viacam_s_fmt_vid_cap
,
1243 .vidioc_querycap
= viacam_querycap
,
1244 .vidioc_reqbufs
= viacam_reqbufs
,
1245 .vidioc_querybuf
= viacam_querybuf
,
1246 .vidioc_qbuf
= viacam_qbuf
,
1247 .vidioc_dqbuf
= viacam_dqbuf
,
1248 .vidioc_streamon
= viacam_streamon
,
1249 .vidioc_streamoff
= viacam_streamoff
,
1250 .vidioc_g_parm
= viacam_g_parm
,
1251 .vidioc_s_parm
= viacam_s_parm
,
1252 .vidioc_enum_framesizes
= viacam_enum_framesizes
,
1253 .vidioc_enum_frameintervals
= viacam_enum_frameintervals
,
1254 #ifdef CONFIG_VIDEO_V4L1_COMPAT
1255 .vidiocgmbuf
= viacam_vidiocgmbuf
,
1259 /*----------------------------------------------------------------------------*/
1269 static struct video_device viacam_v4l_template
= {
1270 .name
= "via-camera",
1272 .tvnorms
= V4L2_STD_NTSC_M
,
1273 .current_norm
= V4L2_STD_NTSC_M
,
1274 .fops
= &viacam_fops
,
1275 .ioctl_ops
= &viacam_ioctl_ops
,
1276 .release
= video_device_release_empty
, /* Check this */
1280 static __devinit
int viacam_probe(struct platform_device
*pdev
)
1283 struct i2c_adapter
*sensor_adapter
;
1284 struct viafb_dev
*viadev
= pdev
->dev
.platform_data
;
1287 * Note that there are actually two capture channels on
1288 * the device. We only deal with one for now. That
1289 * is encoded here; nothing else assumes it's dealing with
1290 * a unique capture device.
1292 struct via_camera
*cam
;
1295 * Ensure that frame buffer memory has been set aside for
1296 * this purpose. As an arbitrary limit, refuse to work
1297 * with less than two frames of VGA 16-bit data.
1299 * If we ever support the second port, we'll need to set
1300 * aside more memory.
1302 if (viadev
->camera_fbmem_size
< (VGA_HEIGHT
*VGA_WIDTH
*4)) {
1303 printk(KERN_ERR
"viacam: insufficient FB memory reserved\n");
1306 if (viadev
->engine_mmio
== NULL
) {
1307 printk(KERN_ERR
"viacam: No I/O memory, so no pictures\n");
1311 * Basic structure initialization.
1313 cam
= kzalloc (sizeof(struct via_camera
), GFP_KERNEL
);
1317 cam
->platdev
= pdev
;
1318 cam
->viadev
= viadev
;
1321 cam
->opstate
= S_IDLE
;
1322 cam
->user_format
= cam
->sensor_format
= viacam_def_pix_format
;
1323 mutex_init(&cam
->lock
);
1324 INIT_LIST_HEAD(&cam
->buffer_queue
);
1325 cam
->mmio
= viadev
->engine_mmio
;
1326 cam
->fbmem
= viadev
->fbmem
;
1327 cam
->fb_offset
= viadev
->camera_fbmem_offset
;
1328 cam
->flags
= 1 << CF_CONFIG_NEEDED
;
1329 cam
->mbus_code
= via_def_mbus_code
;
1331 * Tell V4L that we exist.
1333 ret
= v4l2_device_register(&pdev
->dev
, &cam
->v4l2_dev
);
1335 dev_err(&pdev
->dev
, "Unable to register v4l2 device\n");
1339 * Convince the system that we can do DMA.
1341 pdev
->dev
.dma_mask
= &viadev
->pdev
->dma_mask
;
1342 dma_set_mask(&pdev
->dev
, 0xffffffff);
1344 * Fire up the capture port. The write to 0x78 looks purely
1345 * OLPCish; any system will need to tweak 0x1e.
1347 via_write_reg_mask(VIASR
, 0x78, 0, 0x80);
1348 via_write_reg_mask(VIASR
, 0x1e, 0xc0, 0xc0);
1350 * Get the sensor powered up.
1352 ret
= via_sensor_power_setup(cam
);
1354 goto out_unregister
;
1355 via_sensor_power_up(cam
);
1358 * See if we can't find it on the bus. The VIA_PORT_31 assumption
1359 * is OLPC-specific. 0x42 assumption is ov7670-specific.
1361 sensor_adapter
= viafb_find_i2c_adapter(VIA_PORT_31
);
1362 cam
->sensor
= v4l2_i2c_new_subdev(&cam
->v4l2_dev
, sensor_adapter
,
1363 "ov7670", 0x42 >> 1, NULL
);
1364 if (cam
->sensor
== NULL
) {
1365 dev_err(&pdev
->dev
, "Unable to find the sensor!\n");
1367 goto out_power_down
;
1372 viacam_int_disable(cam
);
1373 ret
= request_threaded_irq(viadev
->pdev
->irq
, viacam_quick_irq
,
1374 viacam_irq
, IRQF_SHARED
, "via-camera", cam
);
1376 goto out_power_down
;
1378 * Tell V4l2 that we exist.
1380 cam
->vdev
= viacam_v4l_template
;
1381 cam
->vdev
.v4l2_dev
= &cam
->v4l2_dev
;
1382 ret
= video_register_device(&cam
->vdev
, VFL_TYPE_GRABBER
, -1);
1385 video_set_drvdata(&cam
->vdev
, cam
);
1387 /* Power the sensor down until somebody opens the device */
1388 via_sensor_power_down(cam
);
1392 free_irq(viadev
->pdev
->irq
, cam
);
1394 via_sensor_power_release(cam
);
1396 v4l2_device_unregister(&cam
->v4l2_dev
);
1400 static __devexit
int viacam_remove(struct platform_device
*pdev
)
1402 struct via_camera
*cam
= via_cam_info
;
1403 struct viafb_dev
*viadev
= pdev
->dev
.platform_data
;
1405 video_unregister_device(&cam
->vdev
);
1406 v4l2_device_unregister(&cam
->v4l2_dev
);
1407 free_irq(viadev
->pdev
->irq
, cam
);
1408 via_sensor_power_release(cam
);
1409 via_cam_info
= NULL
;
1414 static struct platform_driver viacam_driver
= {
1416 .name
= "viafb-camera",
1418 .probe
= viacam_probe
,
1419 .remove
= viacam_remove
,
1423 #ifdef CONFIG_OLPC_XO_1_5
1425 * The OLPC folks put the serial port on the same pin as
1426 * the camera. They also get grumpy if we break the
1427 * serial port and keep them from using it. So we have
1428 * to check the serial enable bit and not step on it.
1430 #define VIACAM_SERIAL_DEVFN 0x88
1431 #define VIACAM_SERIAL_CREG 0x46
1432 #define VIACAM_SERIAL_BIT 0x40
1434 static __devinit
int viacam_check_serial_port(void)
1436 struct pci_bus
*pbus
= pci_find_bus(0, 0);
1439 pci_bus_read_config_byte(pbus
, VIACAM_SERIAL_DEVFN
,
1440 VIACAM_SERIAL_CREG
, &cbyte
);
1441 if ((cbyte
& VIACAM_SERIAL_BIT
) == 0)
1442 return 0; /* Not enabled */
1443 if (override_serial
== 0) {
1444 printk(KERN_NOTICE
"Via camera: serial port is enabled, " \
1445 "refusing to load.\n");
1446 printk(KERN_NOTICE
"Specify override_serial=1 to force " \
1447 "module loading.\n");
1450 printk(KERN_NOTICE
"Via camera: overriding serial port\n");
1451 pci_bus_write_config_byte(pbus
, VIACAM_SERIAL_DEVFN
,
1452 VIACAM_SERIAL_CREG
, cbyte
& ~VIACAM_SERIAL_BIT
);
1460 static int viacam_init(void)
1462 #ifdef CONFIG_OLPC_XO_1_5
1463 if (viacam_check_serial_port())
1466 return platform_driver_register(&viacam_driver
);
1468 module_init(viacam_init
);
1470 static void viacam_exit(void)
1472 platform_driver_unregister(&viacam_driver
);
1474 module_exit(viacam_exit
);