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