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-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.h>
25 #include <linux/via-core.h>
26 #include <linux/via-gpio.h>
27 #include <linux/via_i2c.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 "
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.");
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 };
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
;
70 enum viacam_opstate opstate
;
72 struct pm_qos_request qos_request
;
74 * GPIO info for power/reset management
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? */
93 struct videobuf_queue vb_queue
;
94 struct list_head buffer_queue
; /* prot. by reg_lock */
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
{
150 int bpp
; /* Bytes per pixel */
151 enum v4l2_mbus_pixelcode mbus_code
;
154 .desc
= "YUYV 4:2:2",
155 .pixelformat
= V4L2_PIX_FMT_YUYV
,
156 .mbus_code
= V4L2_MBUS_FMT_YUYV8_2X8
,
161 .pixelformat
= V4L2_PIX_FMT_RGB565
,
162 .mbus_code
= V4L2_MBUS_FMT_RGB565_2X8_LE
,
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
)
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. */
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
)
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");
197 ret
= gpio_request(cam
->power_gpio
, "viafb-camera");
199 dev_err(&cam
->platdev
->dev
, "Unable to request power GPIO\n");
202 ret
= gpio_request(cam
->reset_gpio
, "viafb-camera");
204 dev_err(&cam
->platdev
->dev
, "Unable to request reset GPIO\n");
205 gpio_free(cam
->power_gpio
);
208 gpio_direction_output(cam
->power_gpio
, 0);
209 gpio_direction_output(cam
->reset_gpio
, 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);
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 /* --------------------------------------------------------------------------*/
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
;
264 v4l2_fill_mbus_format(&mbus_fmt
, &cam
->sensor_format
, cam
->mbus_code
);
265 ret
= sensor_call(cam
, core
, init
, 0);
267 ret
= sensor_call(cam
, video
, s_mbus_fmt
, &mbus_fmt
);
269 * OV7670 does weird things if flip is set *before* format...
272 ret
= viacam_set_flip(cam
);
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
286 static inline void viacam_write_reg(struct via_camera
*cam
,
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
;
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
);
332 * Find the next videobuf buffer which has somebody waiting on it.
334 static struct videobuf_buffer
*viacam_next_buffer(struct via_camera
*cam
)
337 struct videobuf_buffer
*buf
= NULL
;
339 spin_lock_irqsave(&cam
->viadev
->reg_lock
, flags
);
340 if (cam
->opstate
!= S_RUNNING
)
342 if (list_empty(&cam
->buffer_queue
))
344 buf
= list_entry(cam
->buffer_queue
.next
, struct videobuf_buffer
, queue
);
345 if (!waitqueue_active(&buf
->done
)) {/* Nobody waiting */
349 list_del(&buf
->queue
);
350 buf
->state
= VIDEOBUF_ACTIVE
;
352 spin_unlock_irqrestore(&cam
->viadev
->reg_lock
, flags
);
357 * The threaded IRQ handler.
359 static irqreturn_t
viacam_irq(int irq
, void *data
)
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
);
374 * Figure out which buffer we just completed.
376 bufn
= (viacam_read_reg(cam
, VCR_INTCTRL
) & VCR_IC_ACTBUF
) >> 3;
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
;
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
;
428 * See how many buffers we can work with.
432 viacam_write_reg_mask(cam
, VCR_CAPINTC
, VCR_CI_3BUFS
,
434 } else if (nbuf
== 2) {
436 viacam_write_reg_mask(cam
, VCR_CAPINTC
, 0, VCR_CI_3BUFS
);
438 cam_warn(cam
, "Insufficient frame buffer memory\n");
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
;
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
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
;
476 if (cam
->user_format
.width
== VGA_WIDTH
)
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
)
498 * Disable clock before messing with stuff - from the via
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
);
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 */
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
;
539 cicreg
|= VCR_CI_UYVY
;
540 viacam_write_reg(cam
, VCR_CAPINTC
, cicreg
);
544 static int viacam_config_controller(struct via_camera
*cam
)
549 spin_lock_irqsave(&cam
->viadev
->reg_lock
, flags
);
550 ret
= viacam_ctlr_cbufs(cam
);
552 viacam_ctlr_image(cam
);
553 spin_unlock_irqrestore(&cam
->viadev
->reg_lock
, flags
);
554 clear_bit(CF_CONFIG_NEEDED
, &cam
->flags
);
559 * Make it start grabbing data.
561 static void viacam_start_engine(struct via_camera
*cam
)
563 spin_lock_irq(&cam
->viadev
->reg_lock
);
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 */
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
;
615 if (vb
->state
== VIDEOBUF_NEEDS_INIT
) {
616 int ret
= videobuf_iolock(q
, vb
, NULL
);
620 vb
->state
= VIDEOBUF_PREPARED
;
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
);
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();
679 mutex_unlock(&cam
->lock
);
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
);
693 mutex_unlock(&cam
->lock
);
697 static int viacam_release(struct file
*filp
)
699 struct via_camera
*cam
= video_drvdata(filp
);
701 mutex_lock(&cam
->lock
);
704 * If the "owner" is closing, shut down any ongoing
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
715 if (cam
->opstate
!= S_IDLE
)
716 viacam_stop_engine(cam
);
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
);
727 mutex_unlock(&cam
->lock
);
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
);
740 mutex_lock(&cam
->lock
);
742 * Enforce the V4l2 "only one owner gets to read data" rule.
744 if (cam
->owner
&& 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
);
755 ret
= viacam_config_controller(cam
);
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() ?? */
772 mutex_unlock(&cam
->lock
);
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
,
797 .release
= viacam_release
,
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
;
816 if (v4l2_chip_match_host(&ident
->match
)) {
817 ident
->ident
= V4L2_IDENT_VIA_VX855
;
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
;
832 mutex_lock(&cam
->lock
);
833 ret
= sensor_call(cam
, core
, queryctrl
, qc
);
834 mutex_unlock(&cam
->lock
);
839 static int viacam_g_ctrl(struct file
*filp
, void *priv
,
840 struct v4l2_control
*ctrl
)
842 struct via_camera
*cam
= priv
;
845 mutex_lock(&cam
->lock
);
846 ret
= sensor_call(cam
, core
, g_ctrl
, ctrl
);
847 mutex_unlock(&cam
->lock
);
852 static int viacam_s_ctrl(struct file
*filp
, void *priv
,
853 struct v4l2_control
*ctrl
)
855 struct via_camera
*cam
= priv
;
858 mutex_lock(&cam
->lock
);
859 ret
= sensor_call(cam
, core
, s_ctrl
, ctrl
);
860 mutex_unlock(&cam
->lock
);
867 static int viacam_enum_input(struct file
*filp
, void *priv
,
868 struct v4l2_input
*input
)
870 if (input
->index
!= 0)
873 input
->type
= V4L2_INPUT_TYPE_CAMERA
;
874 input
->std
= V4L2_STD_ALL
; /* Not sure what should go here */
875 strcpy(input
->name
, "Camera");
879 static int viacam_g_input(struct file
*filp
, void *priv
, unsigned int *i
)
885 static int viacam_s_input(struct file
*filp
, void *priv
, unsigned int i
)
892 static int viacam_s_std(struct file
*filp
, void *priv
, v4l2_std_id
*std
)
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
= {
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
)
917 strlcpy(fmt
->description
, via_formats
[fmt
->index
].desc
,
918 sizeof(fmt
->description
));
919 fmt
->pixelformat
= via_formats
[fmt
->index
].pixelformat
;
924 * Figure out proper image dimensions, but always force the
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
)
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
);
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
;
985 mutex_lock(&cam
->lock
);
986 ret
= viacam_do_try_fmt(cam
, &fmt
->fmt
.pix
, &sfmt
.fmt
.pix
);
987 mutex_unlock(&cam
->lock
);
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
);
1003 static int viacam_s_fmt_vid_cap(struct file
*filp
, void *priv
,
1004 struct v4l2_format
*fmt
)
1006 struct via_camera
*cam
= priv
;
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
1015 mutex_lock(&cam
->lock
);
1016 if (cam
->opstate
!= S_IDLE
) {
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
);
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
);
1035 ret
= viacam_config_controller(cam
);
1037 mutex_unlock(&cam
->lock
);
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");
1047 cap
->capabilities
= V4L2_CAP_VIDEO_CAPTURE
|
1048 V4L2_CAP_READWRITE
| V4L2_CAP_STREAMING
;
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
;
1090 if (t
!= V4L2_BUF_TYPE_VIDEO_CAPTURE
)
1093 mutex_lock(&cam
->lock
);
1094 if (cam
->opstate
!= S_IDLE
) {
1099 * Enforce the V4l2 "only one owner gets to read data" rule.
1101 if (cam
->owner
&& cam
->owner
!= filp
) {
1107 * Configure things if need be.
1109 if (test_bit(CF_CONFIG_NEEDED
, &cam
->flags
)) {
1110 ret
= viacam_configure_sensor(cam
);
1113 ret
= viacam_config_controller(cam
);
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
1123 pm_qos_add_request(&cam
->qos_request
, PM_QOS_CPU_DMA_LATENCY
, 50);
1127 INIT_LIST_HEAD(&cam
->buffer_queue
);
1128 ret
= videobuf_streamon(&cam
->vb_queue
);
1130 viacam_start_engine(cam
);
1132 mutex_unlock(&cam
->lock
);
1136 static int viacam_streamoff(struct file
*filp
, void *priv
, enum v4l2_buf_type t
)
1138 struct via_camera
*cam
= priv
;
1141 if (t
!= V4L2_BUF_TYPE_VIDEO_CAPTURE
)
1143 mutex_lock(&cam
->lock
);
1144 if (cam
->opstate
!= S_RUNNING
) {
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
1155 ret
= videobuf_streamoff(&cam
->vb_queue
);
1156 INIT_LIST_HEAD(&cam
->buffer_queue
);
1158 mutex_unlock(&cam
->lock
);
1164 static int viacam_g_parm(struct file
*filp
, void *priv
,
1165 struct v4l2_streamparm
*parm
)
1167 struct via_camera
*cam
= priv
;
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
;
1177 static int viacam_s_parm(struct file
*filp
, void *priv
,
1178 struct v4l2_streamparm
*parm
)
1180 struct via_camera
*cam
= priv
;
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
;
1190 static int viacam_enum_framesizes(struct file
*filp
, void *priv
,
1191 struct v4l2_frmsizeenum
*sizes
)
1193 if (sizes
->index
!= 0)
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;
1204 static int viacam_enum_frameintervals(struct file
*filp
, void *priv
,
1205 struct v4l2_frmivalenum
*interval
)
1207 struct via_camera
*cam
= priv
;
1210 mutex_lock(&cam
->lock
);
1211 ret
= sensor_call(cam
, video
, enum_frameintervals
, interval
);
1212 mutex_unlock(&cam
->lock
);
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 /*----------------------------------------------------------------------------*/
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 */
1264 static int viacam_resume(void *priv
)
1266 struct via_camera
*cam
= priv
;
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
1280 via_sensor_power_up(cam
);
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
);
1290 ret
= viacam_config_controller(cam
);
1291 mutex_unlock(&cam
->lock
);
1293 viacam_start_engine(cam
);
1299 static struct viafb_pm_hooks viacam_pm_hooks
= {
1300 .suspend
= viacam_suspend
,
1301 .resume
= viacam_resume
1304 #endif /* CONFIG_PM */
1310 static struct video_device viacam_v4l_template
= {
1311 .name
= "via-camera",
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);
1337 pci_bus_read_config_byte(pbus
, VIACAM_SERIAL_DEVFN
,
1338 VIACAM_SERIAL_CREG
, &cbyte
);
1339 if ((cbyte
& VIACAM_SERIAL_BIT
) == 0)
1340 return false; /* Not enabled */
1341 if (override_serial
== 0) {
1342 printk(KERN_NOTICE
"Via camera: serial port is enabled, " \
1343 "refusing to load.\n");
1344 printk(KERN_NOTICE
"Specify override_serial=1 to force " \
1345 "module loading.\n");
1348 printk(KERN_NOTICE
"Via camera: overriding serial port\n");
1349 pci_bus_write_config_byte(pbus
, VIACAM_SERIAL_DEVFN
,
1350 VIACAM_SERIAL_CREG
, cbyte
& ~VIACAM_SERIAL_BIT
);
1354 static __devinit
int viacam_probe(struct platform_device
*pdev
)
1357 struct i2c_adapter
*sensor_adapter
;
1358 struct viafb_dev
*viadev
= pdev
->dev
.platform_data
;
1361 * Note that there are actually two capture channels on
1362 * the device. We only deal with one for now. That
1363 * is encoded here; nothing else assumes it's dealing with
1364 * a unique capture device.
1366 struct via_camera
*cam
;
1369 * Ensure that frame buffer memory has been set aside for
1370 * this purpose. As an arbitrary limit, refuse to work
1371 * with less than two frames of VGA 16-bit data.
1373 * If we ever support the second port, we'll need to set
1374 * aside more memory.
1376 if (viadev
->camera_fbmem_size
< (VGA_HEIGHT
*VGA_WIDTH
*4)) {
1377 printk(KERN_ERR
"viacam: insufficient FB memory reserved\n");
1380 if (viadev
->engine_mmio
== NULL
) {
1381 printk(KERN_ERR
"viacam: No I/O memory, so no pictures\n");
1385 if (machine_is_olpc() && viacam_serial_is_enabled())
1389 * Basic structure initialization.
1391 cam
= kzalloc (sizeof(struct via_camera
), GFP_KERNEL
);
1395 cam
->platdev
= pdev
;
1396 cam
->viadev
= viadev
;
1399 cam
->opstate
= S_IDLE
;
1400 cam
->user_format
= cam
->sensor_format
= viacam_def_pix_format
;
1401 mutex_init(&cam
->lock
);
1402 INIT_LIST_HEAD(&cam
->buffer_queue
);
1403 cam
->mmio
= viadev
->engine_mmio
;
1404 cam
->fbmem
= viadev
->fbmem
;
1405 cam
->fb_offset
= viadev
->camera_fbmem_offset
;
1406 cam
->flags
= 1 << CF_CONFIG_NEEDED
;
1407 cam
->mbus_code
= via_def_mbus_code
;
1409 * Tell V4L that we exist.
1411 ret
= v4l2_device_register(&pdev
->dev
, &cam
->v4l2_dev
);
1413 dev_err(&pdev
->dev
, "Unable to register v4l2 device\n");
1417 * Convince the system that we can do DMA.
1419 pdev
->dev
.dma_mask
= &viadev
->pdev
->dma_mask
;
1420 dma_set_mask(&pdev
->dev
, 0xffffffff);
1422 * Fire up the capture port. The write to 0x78 looks purely
1423 * OLPCish; any system will need to tweak 0x1e.
1425 via_write_reg_mask(VIASR
, 0x78, 0, 0x80);
1426 via_write_reg_mask(VIASR
, 0x1e, 0xc0, 0xc0);
1428 * Get the sensor powered up.
1430 ret
= via_sensor_power_setup(cam
);
1432 goto out_unregister
;
1433 via_sensor_power_up(cam
);
1436 * See if we can't find it on the bus. The VIA_PORT_31 assumption
1437 * is OLPC-specific. 0x42 assumption is ov7670-specific.
1439 sensor_adapter
= viafb_find_i2c_adapter(VIA_PORT_31
);
1440 cam
->sensor
= v4l2_i2c_new_subdev(&cam
->v4l2_dev
, sensor_adapter
,
1441 "ov7670", 0x42 >> 1, NULL
);
1442 if (cam
->sensor
== NULL
) {
1443 dev_err(&pdev
->dev
, "Unable to find the sensor!\n");
1445 goto out_power_down
;
1450 viacam_int_disable(cam
);
1451 ret
= request_threaded_irq(viadev
->pdev
->irq
, viacam_quick_irq
,
1452 viacam_irq
, IRQF_SHARED
, "via-camera", cam
);
1454 goto out_power_down
;
1456 * Tell V4l2 that we exist.
1458 cam
->vdev
= viacam_v4l_template
;
1459 cam
->vdev
.v4l2_dev
= &cam
->v4l2_dev
;
1460 ret
= video_register_device(&cam
->vdev
, VFL_TYPE_GRABBER
, -1);
1463 video_set_drvdata(&cam
->vdev
, cam
);
1467 * Hook into PM events
1469 viacam_pm_hooks
.private = cam
;
1470 viafb_pm_register(&viacam_pm_hooks
);
1473 /* Power the sensor down until somebody opens the device */
1474 via_sensor_power_down(cam
);
1478 free_irq(viadev
->pdev
->irq
, cam
);
1480 via_sensor_power_release(cam
);
1482 v4l2_device_unregister(&cam
->v4l2_dev
);
1486 static __devexit
int viacam_remove(struct platform_device
*pdev
)
1488 struct via_camera
*cam
= via_cam_info
;
1489 struct viafb_dev
*viadev
= pdev
->dev
.platform_data
;
1491 video_unregister_device(&cam
->vdev
);
1492 v4l2_device_unregister(&cam
->v4l2_dev
);
1493 free_irq(viadev
->pdev
->irq
, cam
);
1494 via_sensor_power_release(cam
);
1495 via_cam_info
= NULL
;
1499 static struct platform_driver viacam_driver
= {
1501 .name
= "viafb-camera",
1503 .probe
= viacam_probe
,
1504 .remove
= viacam_remove
,
1507 static int viacam_init(void)
1509 return platform_driver_register(&viacam_driver
);
1511 module_init(viacam_init
);
1513 static void viacam_exit(void)
1515 platform_driver_unregister(&viacam_driver
);
1517 module_exit(viacam_exit
);