1 // SPDX-License-Identifier: GPL-2.0-only
3 * Driver for the VIA Chrome integrated camera controller.
5 * Copyright 2009,2010 Jonathan Corbet <corbet@lwn.net>
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-event.h>
22 #include <media/v4l2-image-sizes.h>
23 #include <media/i2c/ov7670.h>
24 #include <media/videobuf2-dma-sg.h>
25 #include <linux/delay.h>
26 #include <linux/dma-mapping.h>
27 #include <linux/pm_qos.h>
28 #include <linux/via-core.h>
29 #include <linux/via-gpio.h>
30 #include <linux/via_i2c.h>
35 #define machine_is_olpc(x) 0
38 #include "via-camera.h"
40 MODULE_ALIAS("platform:viafb-camera");
41 MODULE_AUTHOR("Jonathan Corbet <corbet@lwn.net>");
42 MODULE_DESCRIPTION("VIA framebuffer-based camera controller driver");
43 MODULE_LICENSE("GPL");
45 static bool flip_image
;
46 module_param(flip_image
, bool, 0444);
47 MODULE_PARM_DESC(flip_image
,
48 "If set, the sensor will be instructed to flip the image vertically.");
50 static bool override_serial
;
51 module_param(override_serial
, bool, 0444);
52 MODULE_PARM_DESC(override_serial
,
53 "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.");
56 * The structure describing our camera.
58 enum viacam_opstate
{ S_IDLE
= 0, S_RUNNING
= 1 };
61 struct v4l2_device v4l2_dev
;
62 struct v4l2_ctrl_handler ctrl_handler
;
63 struct video_device vdev
;
64 struct v4l2_subdev
*sensor
;
65 struct platform_device
*platdev
;
66 struct viafb_dev
*viadev
;
68 enum viacam_opstate opstate
;
70 struct pm_qos_request qos_request
;
72 * GPIO info for power/reset management
79 void __iomem
*mmio
; /* Where the registers live */
80 void __iomem
*fbmem
; /* Frame buffer memory */
81 u32 fb_offset
; /* Reserved memory offset (FB) */
83 * Capture buffers and related. The controller supports
84 * up to three, so that's what we have here. These buffers
85 * live in frame buffer memory, so we don't call them "DMA".
87 unsigned int cb_offsets
[3]; /* offsets into fb mem */
88 u8 __iomem
*cb_addrs
[3]; /* Kernel-space addresses */
89 int n_cap_bufs
; /* How many are we using? */
91 struct list_head buffer_queue
;
94 * Video format information. sensor_format is kept in a form
95 * that we can use to pass to the sensor. We always run the
96 * sensor in VGA resolution, though, and let the controller
97 * downscale things if need be. So we keep the "real*
98 * dimensions separately.
100 struct v4l2_pix_format sensor_format
;
101 struct v4l2_pix_format user_format
;
105 /* buffer for one video frame */
107 /* common v4l buffer stuff -- must be first */
108 struct vb2_v4l2_buffer vbuf
;
109 struct list_head queue
;
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
{
149 int bpp
; /* Bytes per pixel */
153 .pixelformat
= V4L2_PIX_FMT_YUYV
,
154 .mbus_code
= MEDIA_BUS_FMT_YUYV8_2X8
,
157 /* RGB444 and Bayer should be doable, but have never been
158 tested with this driver. RGB565 seems to work at the default
159 resolution, but results in color corruption when being scaled by
160 viacam_set_scaled(), and is disabled as a result. */
162 #define N_VIA_FMTS ARRAY_SIZE(via_formats)
164 static struct via_format
*via_find_format(u32 pixelformat
)
168 for (i
= 0; i
< N_VIA_FMTS
; i
++)
169 if (via_formats
[i
].pixelformat
== pixelformat
)
170 return via_formats
+ i
;
171 /* Not found? Then return the first format. */
176 /*--------------------------------------------------------------------------*/
178 * Sensor power/reset management. This piece is OLPC-specific for
179 * sure; other configurations will have things connected differently.
181 static int via_sensor_power_setup(struct via_camera
*cam
)
185 cam
->power_gpio
= viafb_gpio_lookup("VGPIO3");
186 cam
->reset_gpio
= viafb_gpio_lookup("VGPIO2");
187 if (!gpio_is_valid(cam
->power_gpio
) || !gpio_is_valid(cam
->reset_gpio
)) {
188 dev_err(&cam
->platdev
->dev
, "Unable to find GPIO lines\n");
191 ret
= gpio_request(cam
->power_gpio
, "viafb-camera");
193 dev_err(&cam
->platdev
->dev
, "Unable to request power GPIO\n");
196 ret
= gpio_request(cam
->reset_gpio
, "viafb-camera");
198 dev_err(&cam
->platdev
->dev
, "Unable to request reset GPIO\n");
199 gpio_free(cam
->power_gpio
);
202 gpio_direction_output(cam
->power_gpio
, 0);
203 gpio_direction_output(cam
->reset_gpio
, 0);
208 * Power up the sensor and perform the reset dance.
210 static void via_sensor_power_up(struct via_camera
*cam
)
212 gpio_set_value(cam
->power_gpio
, 1);
213 gpio_set_value(cam
->reset_gpio
, 0);
214 msleep(20); /* Probably excessive */
215 gpio_set_value(cam
->reset_gpio
, 1);
219 static void via_sensor_power_down(struct via_camera
*cam
)
221 gpio_set_value(cam
->power_gpio
, 0);
222 gpio_set_value(cam
->reset_gpio
, 0);
226 static void via_sensor_power_release(struct via_camera
*cam
)
228 via_sensor_power_down(cam
);
229 gpio_free(cam
->power_gpio
);
230 gpio_free(cam
->reset_gpio
);
233 /* --------------------------------------------------------------------------*/
237 * Manage the ov7670 "flip" bit, which needs special help.
239 static int viacam_set_flip(struct via_camera
*cam
)
241 struct v4l2_control ctrl
;
243 memset(&ctrl
, 0, sizeof(ctrl
));
244 ctrl
.id
= V4L2_CID_VFLIP
;
245 ctrl
.value
= flip_image
;
246 return v4l2_s_ctrl(NULL
, cam
->sensor
->ctrl_handler
, &ctrl
);
250 * Configure the sensor. It's up to the caller to ensure
251 * that the camera is in the correct operating state.
253 static int viacam_configure_sensor(struct via_camera
*cam
)
255 struct v4l2_subdev_format format
= {
256 .which
= V4L2_SUBDEV_FORMAT_ACTIVE
,
260 v4l2_fill_mbus_format(&format
.format
, &cam
->sensor_format
, cam
->mbus_code
);
261 ret
= sensor_call(cam
, core
, init
, 0);
263 ret
= sensor_call(cam
, pad
, set_fmt
, NULL
, &format
);
265 * OV7670 does weird things if flip is set *before* format...
268 ret
= viacam_set_flip(cam
);
274 /* --------------------------------------------------------------------------*/
276 * Some simple register accessors; they assume that the lock is held.
278 * Should we want to support the second capture engine, we could
279 * hide the register difference by adding 0x1000 to registers in the
282 static inline void viacam_write_reg(struct via_camera
*cam
,
285 iowrite32(value
, cam
->mmio
+ reg
);
288 static inline int viacam_read_reg(struct via_camera
*cam
, int reg
)
290 return ioread32(cam
->mmio
+ reg
);
293 static inline void viacam_write_reg_mask(struct via_camera
*cam
,
294 int reg
, int value
, int mask
)
296 int tmp
= viacam_read_reg(cam
, reg
);
298 tmp
= (tmp
& ~mask
) | (value
& mask
);
299 viacam_write_reg(cam
, reg
, tmp
);
303 /* --------------------------------------------------------------------------*/
304 /* Interrupt management and handling */
306 static irqreturn_t
viacam_quick_irq(int irq
, void *data
)
308 struct via_camera
*cam
= data
;
309 irqreturn_t ret
= IRQ_NONE
;
313 * All we do here is to clear the interrupts and tell
314 * the handler thread to wake up.
316 spin_lock(&cam
->viadev
->reg_lock
);
317 icv
= viacam_read_reg(cam
, VCR_INTCTRL
);
318 if (icv
& VCR_IC_EAV
) {
319 icv
|= VCR_IC_EAV
|VCR_IC_EVBI
|VCR_IC_FFULL
;
320 viacam_write_reg(cam
, VCR_INTCTRL
, icv
);
321 ret
= IRQ_WAKE_THREAD
;
323 spin_unlock(&cam
->viadev
->reg_lock
);
328 * Find the next buffer which has somebody waiting on it.
330 static struct via_buffer
*viacam_next_buffer(struct via_camera
*cam
)
332 if (cam
->opstate
!= S_RUNNING
)
334 if (list_empty(&cam
->buffer_queue
))
336 return list_entry(cam
->buffer_queue
.next
, struct via_buffer
, queue
);
340 * The threaded IRQ handler.
342 static irqreturn_t
viacam_irq(int irq
, void *data
)
344 struct via_camera
*cam
= data
;
345 struct via_buffer
*vb
;
347 struct sg_table
*sgt
;
349 mutex_lock(&cam
->lock
);
351 * If there is no place to put the data frame, don't bother
352 * with anything else.
354 vb
= viacam_next_buffer(cam
);
358 * Figure out which buffer we just completed.
360 bufn
= (viacam_read_reg(cam
, VCR_INTCTRL
) & VCR_IC_ACTBUF
) >> 3;
363 bufn
= cam
->n_cap_bufs
- 1;
365 * Copy over the data and let any waiters know.
367 sgt
= vb2_dma_sg_plane_desc(&vb
->vbuf
.vb2_buf
, 0);
368 vb
->vbuf
.vb2_buf
.timestamp
= ktime_get_ns();
369 viafb_dma_copy_out_sg(cam
->cb_offsets
[bufn
], sgt
->sgl
, sgt
->nents
);
370 vb
->vbuf
.sequence
= cam
->sequence
++;
371 vb
->vbuf
.field
= V4L2_FIELD_NONE
;
372 list_del(&vb
->queue
);
373 vb2_buffer_done(&vb
->vbuf
.vb2_buf
, VB2_BUF_STATE_DONE
);
375 mutex_unlock(&cam
->lock
);
381 * These functions must mess around with the general interrupt
382 * control register, which is relevant to much more than just the
383 * camera. Nothing else uses interrupts, though, as of this writing.
384 * Should that situation change, we'll have to improve support at
385 * the via-core level.
387 static void viacam_int_enable(struct via_camera
*cam
)
389 viacam_write_reg(cam
, VCR_INTCTRL
,
390 VCR_IC_INTEN
|VCR_IC_EAV
|VCR_IC_EVBI
|VCR_IC_FFULL
);
391 viafb_irq_enable(VDE_I_C0AVEN
);
394 static void viacam_int_disable(struct via_camera
*cam
)
396 viafb_irq_disable(VDE_I_C0AVEN
);
397 viacam_write_reg(cam
, VCR_INTCTRL
, 0);
402 /* --------------------------------------------------------------------------*/
403 /* Controller operations */
406 * Set up our capture buffers in framebuffer memory.
408 static int viacam_ctlr_cbufs(struct via_camera
*cam
)
410 int nbuf
= cam
->viadev
->camera_fbmem_size
/cam
->sensor_format
.sizeimage
;
415 * See how many buffers we can work with.
419 viacam_write_reg_mask(cam
, VCR_CAPINTC
, VCR_CI_3BUFS
,
421 } else if (nbuf
== 2) {
423 viacam_write_reg_mask(cam
, VCR_CAPINTC
, 0, VCR_CI_3BUFS
);
425 cam_warn(cam
, "Insufficient frame buffer memory\n");
431 offset
= cam
->fb_offset
;
432 for (i
= 0; i
< cam
->n_cap_bufs
; i
++) {
433 cam
->cb_offsets
[i
] = offset
;
434 cam
->cb_addrs
[i
] = cam
->fbmem
+ offset
;
435 viacam_write_reg(cam
, VCR_VBUF1
+ i
*4, offset
& VCR_VBUF_MASK
);
436 offset
+= cam
->sensor_format
.sizeimage
;
442 * Set the scaling register for downscaling the image.
444 * This register works like this... Vertical scaling is enabled
445 * by bit 26; if that bit is set, downscaling is controlled by the
446 * value in bits 16:25. Those bits are divided by 1024 to get
447 * the scaling factor; setting just bit 25 thus cuts the height
450 * Horizontal scaling works about the same, but it's enabled by
451 * bit 11, with bits 0:10 giving the numerator of a fraction
452 * (over 2048) for the scaling value.
454 * This function is naive in that, if the user departs from
455 * the 3x4 VGA scaling factor, the image will distort. We
456 * could work around that if it really seemed important.
458 static void viacam_set_scale(struct via_camera
*cam
)
460 unsigned int avscale
;
463 if (cam
->user_format
.width
== VGA_WIDTH
)
466 sf
= (cam
->user_format
.width
*2048)/VGA_WIDTH
;
467 avscale
= VCR_AVS_HEN
| sf
;
469 if (cam
->user_format
.height
< VGA_HEIGHT
) {
470 sf
= (1024*cam
->user_format
.height
)/VGA_HEIGHT
;
471 avscale
|= VCR_AVS_VEN
| (sf
<< 16);
473 viacam_write_reg(cam
, VCR_AVSCALE
, avscale
);
478 * Configure image-related information into the capture engine.
480 static void viacam_ctlr_image(struct via_camera
*cam
)
485 * Disable clock before messing with stuff - from the via
488 viacam_write_reg(cam
, VCR_CAPINTC
, ~(VCR_CI_ENABLE
|VCR_CI_CLKEN
));
490 * Set up the controller for VGA resolution, modulo magic
491 * offsets from the via sample driver.
493 viacam_write_reg(cam
, VCR_HORRANGE
, 0x06200120);
494 viacam_write_reg(cam
, VCR_VERTRANGE
, 0x01de0000);
495 viacam_set_scale(cam
);
499 viacam_write_reg(cam
, VCR_MAXDATA
,
500 (cam
->sensor_format
.height
<< 16) |
501 (cam
->sensor_format
.bytesperline
>> 3));
502 viacam_write_reg(cam
, VCR_MAXVBI
, 0);
503 viacam_write_reg(cam
, VCR_VSTRIDE
,
504 cam
->user_format
.bytesperline
& VCR_VS_STRIDE
);
506 * Set up the capture interface control register,
507 * everything but the "go" bit.
509 * The FIFO threshold is a bit of a magic number; 8 is what
510 * VIA's sample code uses.
512 cicreg
= VCR_CI_CLKEN
|
513 0x08000000 | /* FIFO threshold */
514 VCR_CI_FLDINV
| /* OLPC-specific? */
515 VCR_CI_VREFINV
| /* OLPC-specific? */
516 VCR_CI_DIBOTH
| /* Capture both fields */
518 if (cam
->n_cap_bufs
== 3)
519 cicreg
|= VCR_CI_3BUFS
;
521 * YUV formats need different byte swapping than RGB.
523 if (cam
->user_format
.pixelformat
== V4L2_PIX_FMT_YUYV
)
524 cicreg
|= VCR_CI_YUYV
;
526 cicreg
|= VCR_CI_UYVY
;
527 viacam_write_reg(cam
, VCR_CAPINTC
, cicreg
);
531 static int viacam_config_controller(struct via_camera
*cam
)
536 spin_lock_irqsave(&cam
->viadev
->reg_lock
, flags
);
537 ret
= viacam_ctlr_cbufs(cam
);
539 viacam_ctlr_image(cam
);
540 spin_unlock_irqrestore(&cam
->viadev
->reg_lock
, flags
);
541 clear_bit(CF_CONFIG_NEEDED
, &cam
->flags
);
546 * Make it start grabbing data.
548 static void viacam_start_engine(struct via_camera
*cam
)
550 spin_lock_irq(&cam
->viadev
->reg_lock
);
551 viacam_write_reg_mask(cam
, VCR_CAPINTC
, VCR_CI_ENABLE
, VCR_CI_ENABLE
);
552 viacam_int_enable(cam
);
553 (void) viacam_read_reg(cam
, VCR_CAPINTC
); /* Force post */
554 cam
->opstate
= S_RUNNING
;
555 spin_unlock_irq(&cam
->viadev
->reg_lock
);
559 static void viacam_stop_engine(struct via_camera
*cam
)
561 spin_lock_irq(&cam
->viadev
->reg_lock
);
562 viacam_int_disable(cam
);
563 viacam_write_reg_mask(cam
, VCR_CAPINTC
, 0, VCR_CI_ENABLE
);
564 (void) viacam_read_reg(cam
, VCR_CAPINTC
); /* Force post */
565 cam
->opstate
= S_IDLE
;
566 spin_unlock_irq(&cam
->viadev
->reg_lock
);
570 /* --------------------------------------------------------------------------*/
571 /* vb2 callback ops */
573 static struct via_buffer
*vb2_to_via_buffer(struct vb2_buffer
*vb
)
575 struct vb2_v4l2_buffer
*vbuf
= to_vb2_v4l2_buffer(vb
);
577 return container_of(vbuf
, struct via_buffer
, vbuf
);
580 static void viacam_vb2_queue(struct vb2_buffer
*vb
)
582 struct via_camera
*cam
= vb2_get_drv_priv(vb
->vb2_queue
);
583 struct via_buffer
*via
= vb2_to_via_buffer(vb
);
585 list_add_tail(&via
->queue
, &cam
->buffer_queue
);
588 static int viacam_vb2_prepare(struct vb2_buffer
*vb
)
590 struct via_camera
*cam
= vb2_get_drv_priv(vb
->vb2_queue
);
592 if (vb2_plane_size(vb
, 0) < cam
->user_format
.sizeimage
) {
594 "Plane size too small (%lu < %u)\n",
595 vb2_plane_size(vb
, 0),
596 cam
->user_format
.sizeimage
);
600 vb2_set_plane_payload(vb
, 0, cam
->user_format
.sizeimage
);
605 static int viacam_vb2_queue_setup(struct vb2_queue
*vq
,
607 unsigned int *num_planes
, unsigned int sizes
[],
608 struct device
*alloc_devs
[])
610 struct via_camera
*cam
= vb2_get_drv_priv(vq
);
611 int size
= cam
->user_format
.sizeimage
;
614 return sizes
[0] < size
? -EINVAL
: 0;
621 static int viacam_vb2_start_streaming(struct vb2_queue
*vq
, unsigned int count
)
623 struct via_camera
*cam
= vb2_get_drv_priv(vq
);
624 struct via_buffer
*buf
, *tmp
;
627 if (cam
->opstate
!= S_IDLE
) {
632 * Configure things if need be.
634 if (test_bit(CF_CONFIG_NEEDED
, &cam
->flags
)) {
635 ret
= viacam_configure_sensor(cam
);
638 ret
= viacam_config_controller(cam
);
644 * If the CPU goes into C3, the DMA transfer gets corrupted and
645 * users start filing unsightly bug reports. Put in a "latency"
646 * requirement which will keep the CPU out of the deeper sleep
649 cpu_latency_qos_add_request(&cam
->qos_request
, 50);
650 viacam_start_engine(cam
);
653 list_for_each_entry_safe(buf
, tmp
, &cam
->buffer_queue
, queue
) {
654 list_del(&buf
->queue
);
655 vb2_buffer_done(&buf
->vbuf
.vb2_buf
, VB2_BUF_STATE_QUEUED
);
660 static void viacam_vb2_stop_streaming(struct vb2_queue
*vq
)
662 struct via_camera
*cam
= vb2_get_drv_priv(vq
);
663 struct via_buffer
*buf
, *tmp
;
665 cpu_latency_qos_remove_request(&cam
->qos_request
);
666 viacam_stop_engine(cam
);
668 list_for_each_entry_safe(buf
, tmp
, &cam
->buffer_queue
, queue
) {
669 list_del(&buf
->queue
);
670 vb2_buffer_done(&buf
->vbuf
.vb2_buf
, VB2_BUF_STATE_ERROR
);
674 static const struct vb2_ops viacam_vb2_ops
= {
675 .queue_setup
= viacam_vb2_queue_setup
,
676 .buf_queue
= viacam_vb2_queue
,
677 .buf_prepare
= viacam_vb2_prepare
,
678 .start_streaming
= viacam_vb2_start_streaming
,
679 .stop_streaming
= viacam_vb2_stop_streaming
,
680 .wait_prepare
= vb2_ops_wait_prepare
,
681 .wait_finish
= vb2_ops_wait_finish
,
684 /* --------------------------------------------------------------------------*/
685 /* File operations */
687 static int viacam_open(struct file
*filp
)
689 struct via_camera
*cam
= video_drvdata(filp
);
693 * Note the new user. If this is the first one, we'll also
694 * need to power up the sensor.
696 mutex_lock(&cam
->lock
);
697 ret
= v4l2_fh_open(filp
);
700 if (v4l2_fh_is_singular_file(filp
)) {
701 ret
= viafb_request_dma();
704 v4l2_fh_release(filp
);
707 via_sensor_power_up(cam
);
708 set_bit(CF_CONFIG_NEEDED
, &cam
->flags
);
711 mutex_unlock(&cam
->lock
);
715 static int viacam_release(struct file
*filp
)
717 struct via_camera
*cam
= video_drvdata(filp
);
720 mutex_lock(&cam
->lock
);
721 last_open
= v4l2_fh_is_singular_file(filp
);
722 _vb2_fop_release(filp
, NULL
);
724 * Last one out needs to turn out the lights.
727 via_sensor_power_down(cam
);
730 mutex_unlock(&cam
->lock
);
734 static const struct v4l2_file_operations viacam_fops
= {
735 .owner
= THIS_MODULE
,
737 .release
= viacam_release
,
738 .read
= vb2_fop_read
,
739 .poll
= vb2_fop_poll
,
740 .mmap
= vb2_fop_mmap
,
741 .unlocked_ioctl
= video_ioctl2
,
744 /*----------------------------------------------------------------------------*/
746 * The long list of v4l2 ioctl ops
752 static int viacam_enum_input(struct file
*filp
, void *priv
,
753 struct v4l2_input
*input
)
755 if (input
->index
!= 0)
758 input
->type
= V4L2_INPUT_TYPE_CAMERA
;
759 strscpy(input
->name
, "Camera", sizeof(input
->name
));
763 static int viacam_g_input(struct file
*filp
, void *priv
, unsigned int *i
)
769 static int viacam_s_input(struct file
*filp
, void *priv
, unsigned int i
)
777 * Video format stuff. Here is our default format until
778 * user space messes with things.
780 static const struct v4l2_pix_format viacam_def_pix_format
= {
782 .height
= VGA_HEIGHT
,
783 .pixelformat
= V4L2_PIX_FMT_YUYV
,
784 .field
= V4L2_FIELD_NONE
,
785 .bytesperline
= VGA_WIDTH
* 2,
786 .sizeimage
= VGA_WIDTH
* VGA_HEIGHT
* 2,
787 .colorspace
= V4L2_COLORSPACE_SRGB
,
790 static const u32 via_def_mbus_code
= MEDIA_BUS_FMT_YUYV8_2X8
;
792 static int viacam_enum_fmt_vid_cap(struct file
*filp
, void *priv
,
793 struct v4l2_fmtdesc
*fmt
)
795 if (fmt
->index
>= N_VIA_FMTS
)
797 fmt
->pixelformat
= via_formats
[fmt
->index
].pixelformat
;
802 * Figure out proper image dimensions, but always force the
805 static void viacam_fmt_pre(struct v4l2_pix_format
*userfmt
,
806 struct v4l2_pix_format
*sensorfmt
)
808 *sensorfmt
= *userfmt
;
809 if (userfmt
->width
< QCIF_WIDTH
|| userfmt
->height
< QCIF_HEIGHT
) {
810 userfmt
->width
= QCIF_WIDTH
;
811 userfmt
->height
= QCIF_HEIGHT
;
813 if (userfmt
->width
> VGA_WIDTH
|| userfmt
->height
> VGA_HEIGHT
) {
814 userfmt
->width
= VGA_WIDTH
;
815 userfmt
->height
= VGA_HEIGHT
;
817 sensorfmt
->width
= VGA_WIDTH
;
818 sensorfmt
->height
= VGA_HEIGHT
;
821 static void viacam_fmt_post(struct v4l2_pix_format
*userfmt
,
822 struct v4l2_pix_format
*sensorfmt
)
824 struct via_format
*f
= via_find_format(userfmt
->pixelformat
);
826 sensorfmt
->bytesperline
= sensorfmt
->width
* f
->bpp
;
827 sensorfmt
->sizeimage
= sensorfmt
->height
* sensorfmt
->bytesperline
;
828 userfmt
->pixelformat
= sensorfmt
->pixelformat
;
829 userfmt
->field
= sensorfmt
->field
;
830 userfmt
->bytesperline
= 2 * userfmt
->width
;
831 userfmt
->sizeimage
= userfmt
->bytesperline
* userfmt
->height
;
832 userfmt
->colorspace
= sensorfmt
->colorspace
;
833 userfmt
->ycbcr_enc
= sensorfmt
->ycbcr_enc
;
834 userfmt
->quantization
= sensorfmt
->quantization
;
835 userfmt
->xfer_func
= sensorfmt
->xfer_func
;
840 * The real work of figuring out a workable format.
842 static int viacam_do_try_fmt(struct via_camera
*cam
,
843 struct v4l2_pix_format
*upix
, struct v4l2_pix_format
*spix
)
846 struct v4l2_subdev_pad_config pad_cfg
;
847 struct v4l2_subdev_format format
= {
848 .which
= V4L2_SUBDEV_FORMAT_TRY
,
850 struct via_format
*f
= via_find_format(upix
->pixelformat
);
852 upix
->pixelformat
= f
->pixelformat
;
853 viacam_fmt_pre(upix
, spix
);
854 v4l2_fill_mbus_format(&format
.format
, spix
, f
->mbus_code
);
855 ret
= sensor_call(cam
, pad
, set_fmt
, &pad_cfg
, &format
);
856 v4l2_fill_pix_format(spix
, &format
.format
);
857 viacam_fmt_post(upix
, spix
);
863 static int viacam_try_fmt_vid_cap(struct file
*filp
, void *priv
,
864 struct v4l2_format
*fmt
)
866 struct via_camera
*cam
= video_drvdata(filp
);
867 struct v4l2_format sfmt
;
869 return viacam_do_try_fmt(cam
, &fmt
->fmt
.pix
, &sfmt
.fmt
.pix
);
873 static int viacam_g_fmt_vid_cap(struct file
*filp
, void *priv
,
874 struct v4l2_format
*fmt
)
876 struct via_camera
*cam
= video_drvdata(filp
);
878 fmt
->fmt
.pix
= cam
->user_format
;
882 static int viacam_s_fmt_vid_cap(struct file
*filp
, void *priv
,
883 struct v4l2_format
*fmt
)
885 struct via_camera
*cam
= video_drvdata(filp
);
887 struct v4l2_format sfmt
;
888 struct via_format
*f
= via_find_format(fmt
->fmt
.pix
.pixelformat
);
891 * Camera must be idle or we can't mess with the
894 if (cam
->opstate
!= S_IDLE
)
897 * Let the sensor code look over and tweak the
898 * requested formatting.
900 ret
= viacam_do_try_fmt(cam
, &fmt
->fmt
.pix
, &sfmt
.fmt
.pix
);
904 * OK, let's commit to the new format.
906 cam
->user_format
= fmt
->fmt
.pix
;
907 cam
->sensor_format
= sfmt
.fmt
.pix
;
908 cam
->mbus_code
= f
->mbus_code
;
909 ret
= viacam_configure_sensor(cam
);
911 ret
= viacam_config_controller(cam
);
915 static int viacam_querycap(struct file
*filp
, void *priv
,
916 struct v4l2_capability
*cap
)
918 strscpy(cap
->driver
, "via-camera", sizeof(cap
->driver
));
919 strscpy(cap
->card
, "via-camera", sizeof(cap
->card
));
920 strscpy(cap
->bus_info
, "platform:via-camera", sizeof(cap
->bus_info
));
926 static int viacam_g_parm(struct file
*filp
, void *priv
,
927 struct v4l2_streamparm
*parm
)
929 struct via_camera
*cam
= video_drvdata(filp
);
931 return v4l2_g_parm_cap(video_devdata(filp
), cam
->sensor
, parm
);
934 static int viacam_s_parm(struct file
*filp
, void *priv
,
935 struct v4l2_streamparm
*parm
)
937 struct via_camera
*cam
= video_drvdata(filp
);
939 return v4l2_s_parm_cap(video_devdata(filp
), cam
->sensor
, parm
);
942 static int viacam_enum_framesizes(struct file
*filp
, void *priv
,
943 struct v4l2_frmsizeenum
*sizes
)
947 if (sizes
->index
!= 0)
949 for (i
= 0; i
< N_VIA_FMTS
; i
++)
950 if (sizes
->pixel_format
== via_formats
[i
].pixelformat
)
954 sizes
->type
= V4L2_FRMSIZE_TYPE_CONTINUOUS
;
955 sizes
->stepwise
.min_width
= QCIF_WIDTH
;
956 sizes
->stepwise
.min_height
= QCIF_HEIGHT
;
957 sizes
->stepwise
.max_width
= VGA_WIDTH
;
958 sizes
->stepwise
.max_height
= VGA_HEIGHT
;
959 sizes
->stepwise
.step_width
= sizes
->stepwise
.step_height
= 1;
963 static int viacam_enum_frameintervals(struct file
*filp
, void *priv
,
964 struct v4l2_frmivalenum
*interval
)
966 struct via_camera
*cam
= video_drvdata(filp
);
967 struct v4l2_subdev_frame_interval_enum fie
= {
968 .index
= interval
->index
,
969 .code
= cam
->mbus_code
,
970 .width
= cam
->sensor_format
.width
,
971 .height
= cam
->sensor_format
.height
,
972 .which
= V4L2_SUBDEV_FORMAT_ACTIVE
,
977 for (i
= 0; i
< N_VIA_FMTS
; i
++)
978 if (interval
->pixel_format
== via_formats
[i
].pixelformat
)
982 if (interval
->width
< QCIF_WIDTH
|| interval
->width
> VGA_WIDTH
||
983 interval
->height
< QCIF_HEIGHT
|| interval
->height
> VGA_HEIGHT
)
985 ret
= sensor_call(cam
, pad
, enum_frame_interval
, NULL
, &fie
);
988 interval
->type
= V4L2_FRMIVAL_TYPE_DISCRETE
;
989 interval
->discrete
= fie
.interval
;
993 static const struct v4l2_ioctl_ops viacam_ioctl_ops
= {
994 .vidioc_enum_input
= viacam_enum_input
,
995 .vidioc_g_input
= viacam_g_input
,
996 .vidioc_s_input
= viacam_s_input
,
997 .vidioc_enum_fmt_vid_cap
= viacam_enum_fmt_vid_cap
,
998 .vidioc_try_fmt_vid_cap
= viacam_try_fmt_vid_cap
,
999 .vidioc_g_fmt_vid_cap
= viacam_g_fmt_vid_cap
,
1000 .vidioc_s_fmt_vid_cap
= viacam_s_fmt_vid_cap
,
1001 .vidioc_querycap
= viacam_querycap
,
1002 .vidioc_reqbufs
= vb2_ioctl_reqbufs
,
1003 .vidioc_create_bufs
= vb2_ioctl_create_bufs
,
1004 .vidioc_querybuf
= vb2_ioctl_querybuf
,
1005 .vidioc_prepare_buf
= vb2_ioctl_prepare_buf
,
1006 .vidioc_qbuf
= vb2_ioctl_qbuf
,
1007 .vidioc_dqbuf
= vb2_ioctl_dqbuf
,
1008 .vidioc_expbuf
= vb2_ioctl_expbuf
,
1009 .vidioc_streamon
= vb2_ioctl_streamon
,
1010 .vidioc_streamoff
= vb2_ioctl_streamoff
,
1011 .vidioc_g_parm
= viacam_g_parm
,
1012 .vidioc_s_parm
= viacam_s_parm
,
1013 .vidioc_enum_framesizes
= viacam_enum_framesizes
,
1014 .vidioc_enum_frameintervals
= viacam_enum_frameintervals
,
1015 .vidioc_subscribe_event
= v4l2_ctrl_subscribe_event
,
1016 .vidioc_unsubscribe_event
= v4l2_event_unsubscribe
,
1019 /*----------------------------------------------------------------------------*/
1026 static int viacam_suspend(void *priv
)
1028 struct via_camera
*cam
= priv
;
1029 enum viacam_opstate state
= cam
->opstate
;
1031 if (cam
->opstate
!= S_IDLE
) {
1032 viacam_stop_engine(cam
);
1033 cam
->opstate
= state
; /* So resume restarts */
1039 static int viacam_resume(void *priv
)
1041 struct via_camera
*cam
= priv
;
1045 * Get back to a reasonable operating state.
1047 via_write_reg_mask(VIASR
, 0x78, 0, 0x80);
1048 via_write_reg_mask(VIASR
, 0x1e, 0xc0, 0xc0);
1049 viacam_int_disable(cam
);
1050 set_bit(CF_CONFIG_NEEDED
, &cam
->flags
);
1052 * Make sure the sensor's power state is correct
1054 if (!list_empty(&cam
->vdev
.fh_list
))
1055 via_sensor_power_up(cam
);
1057 via_sensor_power_down(cam
);
1059 * If it was operating, try to restart it.
1061 if (cam
->opstate
!= S_IDLE
) {
1062 mutex_lock(&cam
->lock
);
1063 ret
= viacam_configure_sensor(cam
);
1065 ret
= viacam_config_controller(cam
);
1066 mutex_unlock(&cam
->lock
);
1068 viacam_start_engine(cam
);
1074 static struct viafb_pm_hooks viacam_pm_hooks
= {
1075 .suspend
= viacam_suspend
,
1076 .resume
= viacam_resume
1079 #endif /* CONFIG_PM */
1085 static const struct video_device viacam_v4l_template
= {
1086 .name
= "via-camera",
1088 .fops
= &viacam_fops
,
1089 .ioctl_ops
= &viacam_ioctl_ops
,
1090 .release
= video_device_release_empty
, /* Check this */
1091 .device_caps
= V4L2_CAP_VIDEO_CAPTURE
| V4L2_CAP_READWRITE
|
1096 * The OLPC folks put the serial port on the same pin as
1097 * the camera. They also get grumpy if we break the
1098 * serial port and keep them from using it. So we have
1099 * to check the serial enable bit and not step on it.
1101 #define VIACAM_SERIAL_DEVFN 0x88
1102 #define VIACAM_SERIAL_CREG 0x46
1103 #define VIACAM_SERIAL_BIT 0x40
1105 static bool viacam_serial_is_enabled(void)
1107 struct pci_bus
*pbus
= pci_find_bus(0, 0);
1112 pci_bus_read_config_byte(pbus
, VIACAM_SERIAL_DEVFN
,
1113 VIACAM_SERIAL_CREG
, &cbyte
);
1114 if ((cbyte
& VIACAM_SERIAL_BIT
) == 0)
1115 return false; /* Not enabled */
1116 if (!override_serial
) {
1117 printk(KERN_NOTICE
"Via camera: serial port is enabled, " \
1118 "refusing to load.\n");
1119 printk(KERN_NOTICE
"Specify override_serial=1 to force " \
1120 "module loading.\n");
1123 printk(KERN_NOTICE
"Via camera: overriding serial port\n");
1124 pci_bus_write_config_byte(pbus
, VIACAM_SERIAL_DEVFN
,
1125 VIACAM_SERIAL_CREG
, cbyte
& ~VIACAM_SERIAL_BIT
);
1129 static struct ov7670_config sensor_cfg
= {
1130 /* The XO-1.5 (only known user) clocks the camera at 90MHz. */
1134 static int viacam_probe(struct platform_device
*pdev
)
1137 struct i2c_adapter
*sensor_adapter
;
1138 struct viafb_dev
*viadev
= pdev
->dev
.platform_data
;
1139 struct vb2_queue
*vq
;
1140 struct i2c_board_info ov7670_info
= {
1143 .platform_data
= &sensor_cfg
,
1147 * Note that there are actually two capture channels on
1148 * the device. We only deal with one for now. That
1149 * is encoded here; nothing else assumes it's dealing with
1150 * a unique capture device.
1152 struct via_camera
*cam
;
1155 * Ensure that frame buffer memory has been set aside for
1156 * this purpose. As an arbitrary limit, refuse to work
1157 * with less than two frames of VGA 16-bit data.
1159 * If we ever support the second port, we'll need to set
1160 * aside more memory.
1162 if (viadev
->camera_fbmem_size
< (VGA_HEIGHT
*VGA_WIDTH
*4)) {
1163 printk(KERN_ERR
"viacam: insufficient FB memory reserved\n");
1166 if (viadev
->engine_mmio
== NULL
) {
1167 printk(KERN_ERR
"viacam: No I/O memory, so no pictures\n");
1171 if (machine_is_olpc() && viacam_serial_is_enabled())
1175 * Basic structure initialization.
1177 cam
= kzalloc (sizeof(struct via_camera
), GFP_KERNEL
);
1181 cam
->platdev
= pdev
;
1182 cam
->viadev
= viadev
;
1183 cam
->opstate
= S_IDLE
;
1184 cam
->user_format
= cam
->sensor_format
= viacam_def_pix_format
;
1185 mutex_init(&cam
->lock
);
1186 INIT_LIST_HEAD(&cam
->buffer_queue
);
1187 cam
->mmio
= viadev
->engine_mmio
;
1188 cam
->fbmem
= viadev
->fbmem
;
1189 cam
->fb_offset
= viadev
->camera_fbmem_offset
;
1190 cam
->flags
= 1 << CF_CONFIG_NEEDED
;
1191 cam
->mbus_code
= via_def_mbus_code
;
1193 * Tell V4L that we exist.
1195 ret
= v4l2_device_register(&pdev
->dev
, &cam
->v4l2_dev
);
1197 dev_err(&pdev
->dev
, "Unable to register v4l2 device\n");
1200 ret
= v4l2_ctrl_handler_init(&cam
->ctrl_handler
, 10);
1202 goto out_unregister
;
1203 cam
->v4l2_dev
.ctrl_handler
= &cam
->ctrl_handler
;
1205 * Convince the system that we can do DMA.
1207 pdev
->dev
.dma_mask
= &viadev
->pdev
->dma_mask
;
1208 dma_set_mask(&pdev
->dev
, 0xffffffff);
1210 * Fire up the capture port. The write to 0x78 looks purely
1211 * OLPCish; any system will need to tweak 0x1e.
1213 via_write_reg_mask(VIASR
, 0x78, 0, 0x80);
1214 via_write_reg_mask(VIASR
, 0x1e, 0xc0, 0xc0);
1216 * Get the sensor powered up.
1218 ret
= via_sensor_power_setup(cam
);
1220 goto out_ctrl_hdl_free
;
1221 via_sensor_power_up(cam
);
1224 * See if we can't find it on the bus. The VIA_PORT_31 assumption
1225 * is OLPC-specific. 0x42 assumption is ov7670-specific.
1227 sensor_adapter
= viafb_find_i2c_adapter(VIA_PORT_31
);
1228 cam
->sensor
= v4l2_i2c_new_subdev_board(&cam
->v4l2_dev
, sensor_adapter
,
1229 &ov7670_info
, NULL
);
1230 if (cam
->sensor
== NULL
) {
1231 dev_err(&pdev
->dev
, "Unable to find the sensor!\n");
1233 goto out_power_down
;
1238 viacam_int_disable(cam
);
1239 ret
= request_threaded_irq(viadev
->pdev
->irq
, viacam_quick_irq
,
1240 viacam_irq
, IRQF_SHARED
, "via-camera", cam
);
1242 goto out_power_down
;
1245 vq
->type
= V4L2_BUF_TYPE_VIDEO_CAPTURE
;
1246 vq
->io_modes
= VB2_MMAP
| VB2_USERPTR
| VB2_DMABUF
| VB2_READ
;
1248 vq
->timestamp_flags
= V4L2_BUF_FLAG_TIMESTAMP_MONOTONIC
;
1249 vq
->buf_struct_size
= sizeof(struct via_buffer
);
1250 vq
->dev
= cam
->v4l2_dev
.dev
;
1252 vq
->ops
= &viacam_vb2_ops
;
1253 vq
->mem_ops
= &vb2_dma_sg_memops
;
1254 vq
->lock
= &cam
->lock
;
1256 ret
= vb2_queue_init(vq
);
1258 * Tell V4l2 that we exist.
1260 cam
->vdev
= viacam_v4l_template
;
1261 cam
->vdev
.v4l2_dev
= &cam
->v4l2_dev
;
1262 cam
->vdev
.lock
= &cam
->lock
;
1263 cam
->vdev
.queue
= vq
;
1264 video_set_drvdata(&cam
->vdev
, cam
);
1265 ret
= video_register_device(&cam
->vdev
, VFL_TYPE_VIDEO
, -1);
1271 * Hook into PM events
1273 viacam_pm_hooks
.private = cam
;
1274 viafb_pm_register(&viacam_pm_hooks
);
1277 /* Power the sensor down until somebody opens the device */
1278 via_sensor_power_down(cam
);
1282 free_irq(viadev
->pdev
->irq
, cam
);
1284 via_sensor_power_release(cam
);
1286 v4l2_ctrl_handler_free(&cam
->ctrl_handler
);
1288 v4l2_device_unregister(&cam
->v4l2_dev
);
1294 static int viacam_remove(struct platform_device
*pdev
)
1296 struct via_camera
*cam
= via_cam_info
;
1297 struct viafb_dev
*viadev
= pdev
->dev
.platform_data
;
1299 video_unregister_device(&cam
->vdev
);
1300 v4l2_device_unregister(&cam
->v4l2_dev
);
1302 viafb_pm_unregister(&viacam_pm_hooks
);
1304 free_irq(viadev
->pdev
->irq
, cam
);
1305 via_sensor_power_release(cam
);
1306 v4l2_ctrl_handler_free(&cam
->ctrl_handler
);
1308 via_cam_info
= NULL
;
1312 static struct platform_driver viacam_driver
= {
1314 .name
= "viafb-camera",
1316 .probe
= viacam_probe
,
1317 .remove
= viacam_remove
,
1320 module_platform_driver(viacam_driver
);