2 * The Marvell camera core. This device appears in a number of settings,
3 * so it needs platform-specific support outside of the core.
5 * Copyright 2011 Jonathan Corbet corbet@lwn.net
7 #include <linux/kernel.h>
8 #include <linux/module.h>
11 #include <linux/i2c.h>
12 #include <linux/interrupt.h>
13 #include <linux/spinlock.h>
14 #include <linux/videodev2.h>
15 #include <linux/slab.h>
16 #include <media/v4l2-device.h>
17 #include <media/v4l2-ioctl.h>
18 #include <media/v4l2-chip-ident.h>
19 #include <media/ov7670.h>
20 #include <media/videobuf2-vmalloc.h>
21 #include <linux/device.h>
22 #include <linux/wait.h>
23 #include <linux/list.h>
24 #include <linux/dma-mapping.h>
25 #include <linux/delay.h>
26 #include <linux/jiffies.h>
27 #include <linux/vmalloc.h>
28 #include <linux/uaccess.h>
31 #include "mcam-core.h"
35 * Internal DMA buffer management. Since the controller cannot do S/G I/O,
36 * we must have physically contiguous buffers to bring frames into.
37 * These parameters control how many buffers we use, whether we
38 * allocate them at load time (better chance of success, but nails down
39 * memory) or when somebody tries to use the camera (riskier), and,
40 * for load-time allocation, how big they should be.
42 * The controller can cycle through three buffers. We could use
43 * more by flipping pointers around, but it probably makes little
47 static int alloc_bufs_at_read;
48 module_param(alloc_bufs_at_read, bool, 0444);
49 MODULE_PARM_DESC(alloc_bufs_at_read,
50 "Non-zero value causes DMA buffers to be allocated when the "
51 "video capture device is read, rather than at module load "
52 "time. This saves memory, but decreases the chances of "
53 "successfully getting those buffers.");
55 static int n_dma_bufs = 3;
56 module_param(n_dma_bufs, uint, 0644);
57 MODULE_PARM_DESC(n_dma_bufs,
58 "The number of DMA buffers to allocate. Can be either two "
59 "(saves memory, makes timing tighter) or three.");
61 static int dma_buf_size = VGA_WIDTH * VGA_HEIGHT * 2; /* Worst case */
62 module_param(dma_buf_size, uint, 0444);
63 MODULE_PARM_DESC(dma_buf_size,
64 "The size of the allocated DMA buffers. If actual operating "
65 "parameters require larger buffers, an attempt to reallocate "
68 static int min_buffers = 1;
69 module_param(min_buffers, uint, 0644);
70 MODULE_PARM_DESC(min_buffers,
71 "The minimum number of streaming I/O buffers we are willing "
74 static int max_buffers = 10;
75 module_param(max_buffers, uint, 0644);
76 MODULE_PARM_DESC(max_buffers,
77 "The maximum number of streaming I/O buffers an application "
78 "will be allowed to allocate. These buffers are big and live "
82 module_param(flip, bool, 0444);
83 MODULE_PARM_DESC(flip,
84 "If set, the sensor will be instructed to flip the image "
88 * Status flags. Always manipulated with bit operations.
90 #define CF_BUF0_VALID 0 /* Buffers valid - first three */
91 #define CF_BUF1_VALID 1
92 #define CF_BUF2_VALID 2
93 #define CF_DMA_ACTIVE 3 /* A frame is incoming */
94 #define CF_CONFIG_NEEDED 4 /* Must configure hardware */
96 #define sensor_call(cam, o, f, args...) \
97 v4l2_subdev_call(cam->sensor, o, f, ##args)
99 static struct mcam_format_struct {
102 int bpp; /* Bytes per pixel */
103 enum v4l2_mbus_pixelcode mbus_code;
106 .desc = "YUYV 4:2:2",
107 .pixelformat = V4L2_PIX_FMT_YUYV,
108 .mbus_code = V4L2_MBUS_FMT_YUYV8_2X8,
113 .pixelformat = V4L2_PIX_FMT_RGB444,
114 .mbus_code = V4L2_MBUS_FMT_RGB444_2X8_PADHI_LE,
119 .pixelformat = V4L2_PIX_FMT_RGB565,
120 .mbus_code = V4L2_MBUS_FMT_RGB565_2X8_LE,
124 .desc = "Raw RGB Bayer",
125 .pixelformat = V4L2_PIX_FMT_SBGGR8,
126 .mbus_code = V4L2_MBUS_FMT_SBGGR8_1X8,
130 #define N_MCAM_FMTS ARRAY_SIZE(mcam_formats)
132 static struct mcam_format_struct *mcam_find_format(u32 pixelformat)
136 for (i = 0; i < N_MCAM_FMTS; i++)
137 if (mcam_formats[i].pixelformat == pixelformat)
138 return mcam_formats + i;
139 /* Not found? Then return the first format. */
144 * Start over with DMA buffers - dev_lock needed.
146 static void mcam_reset_buffers(struct mcam_camera *cam)
151 for (i = 0; i < cam->nbufs; i++)
152 clear_bit(i, &cam->flags);
155 static inline int mcam_needs_config(struct mcam_camera *cam)
157 return test_bit(CF_CONFIG_NEEDED, &cam->flags);
160 static void mcam_set_config_needed(struct mcam_camera *cam, int needed)
163 set_bit(CF_CONFIG_NEEDED, &cam->flags);
165 clear_bit(CF_CONFIG_NEEDED, &cam->flags);
169 * Our buffer type for working with videobuf2. Note that the vb2
170 * developers have decreed that struct vb2_buffer must be at the
171 * beginning of this structure.
173 struct mcam_vb_buffer {
174 struct vb2_buffer vb_buf;
175 struct list_head queue;
178 static inline struct mcam_vb_buffer *vb_to_mvb(struct vb2_buffer *vb)
180 return container_of(vb, struct mcam_vb_buffer, vb_buf);
185 * Debugging and related.
187 #define cam_err(cam, fmt, arg...) \
188 dev_err((cam)->dev, fmt, ##arg);
189 #define cam_warn(cam, fmt, arg...) \
190 dev_warn((cam)->dev, fmt, ##arg);
191 #define cam_dbg(cam, fmt, arg...) \
192 dev_dbg((cam)->dev, fmt, ##arg);
196 /* ------------------------------------------------------------------- */
198 * Deal with the controller.
202 * Do everything we think we need to have the interface operating
203 * according to the desired format.
205 static void mcam_ctlr_dma(struct mcam_camera *cam)
208 * Store the first two Y buffers (we aren't supporting
209 * planar formats for now, so no UV bufs). Then either
210 * set the third if it exists, or tell the controller
213 mcam_reg_write(cam, REG_Y0BAR, cam->dma_handles[0]);
214 mcam_reg_write(cam, REG_Y1BAR, cam->dma_handles[1]);
215 if (cam->nbufs > 2) {
216 mcam_reg_write(cam, REG_Y2BAR, cam->dma_handles[2]);
217 mcam_reg_clear_bit(cam, REG_CTRL1, C1_TWOBUFS);
219 mcam_reg_set_bit(cam, REG_CTRL1, C1_TWOBUFS);
220 if (cam->chip_id == V4L2_IDENT_CAFE)
221 mcam_reg_write(cam, REG_UBAR, 0); /* 32 bits only */
224 static void mcam_ctlr_image(struct mcam_camera *cam)
227 struct v4l2_pix_format *fmt = &cam->pix_format;
229 imgsz = ((fmt->height << IMGSZ_V_SHIFT) & IMGSZ_V_MASK) |
230 (fmt->bytesperline & IMGSZ_H_MASK);
231 mcam_reg_write(cam, REG_IMGSIZE, imgsz);
232 mcam_reg_write(cam, REG_IMGOFFSET, 0);
233 /* YPITCH just drops the last two bits */
234 mcam_reg_write_mask(cam, REG_IMGPITCH, fmt->bytesperline,
237 * Tell the controller about the image format we are using.
239 switch (cam->pix_format.pixelformat) {
240 case V4L2_PIX_FMT_YUYV:
241 mcam_reg_write_mask(cam, REG_CTRL0,
242 C0_DF_YUV|C0_YUV_PACKED|C0_YUVE_YUYV,
246 case V4L2_PIX_FMT_RGB444:
247 mcam_reg_write_mask(cam, REG_CTRL0,
248 C0_DF_RGB|C0_RGBF_444|C0_RGB4_XRGB,
253 case V4L2_PIX_FMT_RGB565:
254 mcam_reg_write_mask(cam, REG_CTRL0,
255 C0_DF_RGB|C0_RGBF_565|C0_RGB5_BGGR,
260 cam_err(cam, "Unknown format %x\n", cam->pix_format.pixelformat);
264 * Make sure it knows we want to use hsync/vsync.
266 mcam_reg_write_mask(cam, REG_CTRL0, C0_SIF_HVSYNC,
272 * Configure the controller for operation; caller holds the
275 static int mcam_ctlr_configure(struct mcam_camera *cam)
279 spin_lock_irqsave(&cam->dev_lock, flags);
281 mcam_ctlr_image(cam);
282 mcam_set_config_needed(cam, 0);
283 spin_unlock_irqrestore(&cam->dev_lock, flags);
287 static void mcam_ctlr_irq_enable(struct mcam_camera *cam)
290 * Clear any pending interrupts, since we do not
291 * expect to have I/O active prior to enabling.
293 mcam_reg_write(cam, REG_IRQSTAT, FRAMEIRQS);
294 mcam_reg_set_bit(cam, REG_IRQMASK, FRAMEIRQS);
297 static void mcam_ctlr_irq_disable(struct mcam_camera *cam)
299 mcam_reg_clear_bit(cam, REG_IRQMASK, FRAMEIRQS);
303 * Make the controller start grabbing images. Everything must
304 * be set up before doing this.
306 static void mcam_ctlr_start(struct mcam_camera *cam)
308 /* set_bit performs a read, so no other barrier should be
310 mcam_reg_set_bit(cam, REG_CTRL0, C0_ENABLE);
313 static void mcam_ctlr_stop(struct mcam_camera *cam)
315 mcam_reg_clear_bit(cam, REG_CTRL0, C0_ENABLE);
318 static void mcam_ctlr_init(struct mcam_camera *cam)
322 spin_lock_irqsave(&cam->dev_lock, flags);
324 * Make sure it's not powered down.
326 mcam_reg_clear_bit(cam, REG_CTRL1, C1_PWRDWN);
328 * Turn off the enable bit. It sure should be off anyway,
329 * but it's good to be sure.
331 mcam_reg_clear_bit(cam, REG_CTRL0, C0_ENABLE);
333 * Clock the sensor appropriately. Controller clock should
334 * be 48MHz, sensor "typical" value is half that.
336 mcam_reg_write_mask(cam, REG_CLKCTRL, 2, CLK_DIV_MASK);
337 spin_unlock_irqrestore(&cam->dev_lock, flags);
342 * Stop the controller, and don't return until we're really sure that no
343 * further DMA is going on.
345 static void mcam_ctlr_stop_dma(struct mcam_camera *cam)
350 * Theory: stop the camera controller (whether it is operating
351 * or not). Delay briefly just in case we race with the SOF
352 * interrupt, then wait until no DMA is active.
354 spin_lock_irqsave(&cam->dev_lock, flags);
356 spin_unlock_irqrestore(&cam->dev_lock, flags);
358 if (test_bit(CF_DMA_ACTIVE, &cam->flags))
359 cam_err(cam, "Timeout waiting for DMA to end\n");
360 /* This would be bad news - what now? */
361 spin_lock_irqsave(&cam->dev_lock, flags);
363 mcam_ctlr_irq_disable(cam);
364 spin_unlock_irqrestore(&cam->dev_lock, flags);
370 static void mcam_ctlr_power_up(struct mcam_camera *cam)
374 spin_lock_irqsave(&cam->dev_lock, flags);
375 cam->plat_power_up(cam);
376 mcam_reg_clear_bit(cam, REG_CTRL1, C1_PWRDWN);
377 spin_unlock_irqrestore(&cam->dev_lock, flags);
378 msleep(5); /* Just to be sure */
381 static void mcam_ctlr_power_down(struct mcam_camera *cam)
385 spin_lock_irqsave(&cam->dev_lock, flags);
387 * School of hard knocks department: be sure we do any register
388 * twiddling on the controller *before* calling the platform
389 * power down routine.
391 mcam_reg_set_bit(cam, REG_CTRL1, C1_PWRDWN);
392 cam->plat_power_down(cam);
393 spin_unlock_irqrestore(&cam->dev_lock, flags);
396 /* -------------------------------------------------------------------- */
398 * Communications with the sensor.
401 static int __mcam_cam_reset(struct mcam_camera *cam)
403 return sensor_call(cam, core, reset, 0);
407 * We have found the sensor on the i2c. Let's try to have a
410 static int mcam_cam_init(struct mcam_camera *cam)
412 struct v4l2_dbg_chip_ident chip;
415 mutex_lock(&cam->s_mutex);
416 if (cam->state != S_NOTREADY)
417 cam_warn(cam, "Cam init with device in funky state %d",
419 ret = __mcam_cam_reset(cam);
422 chip.ident = V4L2_IDENT_NONE;
423 chip.match.type = V4L2_CHIP_MATCH_I2C_ADDR;
424 chip.match.addr = cam->sensor_addr;
425 ret = sensor_call(cam, core, g_chip_ident, &chip);
428 cam->sensor_type = chip.ident;
429 if (cam->sensor_type != V4L2_IDENT_OV7670) {
430 cam_err(cam, "Unsupported sensor type 0x%x", cam->sensor_type);
434 /* Get/set parameters? */
438 mcam_ctlr_power_down(cam);
439 mutex_unlock(&cam->s_mutex);
444 * Configure the sensor to match the parameters we have. Caller should
447 static int mcam_cam_set_flip(struct mcam_camera *cam)
449 struct v4l2_control ctrl;
451 memset(&ctrl, 0, sizeof(ctrl));
452 ctrl.id = V4L2_CID_VFLIP;
454 return sensor_call(cam, core, s_ctrl, &ctrl);
458 static int mcam_cam_configure(struct mcam_camera *cam)
460 struct v4l2_mbus_framefmt mbus_fmt;
463 v4l2_fill_mbus_format(&mbus_fmt, &cam->pix_format, cam->mbus_code);
464 ret = sensor_call(cam, core, init, 0);
466 ret = sensor_call(cam, video, s_mbus_fmt, &mbus_fmt);
468 * OV7670 does weird things if flip is set *before* format...
470 ret += mcam_cam_set_flip(cam);
474 /* -------------------------------------------------------------------- */
476 * DMA buffer management. These functions need s_mutex held.
479 /* FIXME: this is inefficient as hell, since dma_alloc_coherent just
480 * does a get_free_pages() call, and we waste a good chunk of an orderN
481 * allocation. Should try to allocate the whole set in one chunk.
483 static int mcam_alloc_dma_bufs(struct mcam_camera *cam, int loadtime)
487 mcam_set_config_needed(cam, 1);
489 cam->dma_buf_size = dma_buf_size;
491 cam->dma_buf_size = cam->pix_format.sizeimage;
496 for (i = 0; i < n_dma_bufs; i++) {
497 cam->dma_bufs[i] = dma_alloc_coherent(cam->dev,
498 cam->dma_buf_size, cam->dma_handles + i,
500 if (cam->dma_bufs[i] == NULL) {
501 cam_warn(cam, "Failed to allocate DMA buffer\n");
504 /* For debug, remove eventually */
505 memset(cam->dma_bufs[i], 0xcc, cam->dma_buf_size);
509 switch (cam->nbufs) {
511 dma_free_coherent(cam->dev, cam->dma_buf_size,
512 cam->dma_bufs[0], cam->dma_handles[0]);
515 cam_err(cam, "Insufficient DMA buffers, cannot operate\n");
520 cam_warn(cam, "Will limp along with only 2 buffers\n");
526 static void mcam_free_dma_bufs(struct mcam_camera *cam)
530 for (i = 0; i < cam->nbufs; i++) {
531 dma_free_coherent(cam->dev, cam->dma_buf_size,
532 cam->dma_bufs[i], cam->dma_handles[i]);
533 cam->dma_bufs[i] = NULL;
540 /* ----------------------------------------------------------------------- */
542 * Here starts the V4L2 interface code.
547 * Get everything ready, and start grabbing frames.
549 static int mcam_read_setup(struct mcam_camera *cam, enum mcam_state state)
555 * Configuration. If we still don't have DMA buffers,
556 * make one last, desperate attempt.
559 if (mcam_alloc_dma_bufs(cam, 0))
562 if (mcam_needs_config(cam)) {
563 mcam_cam_configure(cam);
564 ret = mcam_ctlr_configure(cam);
572 spin_lock_irqsave(&cam->dev_lock, flags);
573 mcam_reset_buffers(cam);
574 mcam_ctlr_irq_enable(cam);
576 mcam_ctlr_start(cam);
577 spin_unlock_irqrestore(&cam->dev_lock, flags);
581 /* ----------------------------------------------------------------------- */
583 * Videobuf2 interface code.
586 static int mcam_vb_queue_setup(struct vb2_queue *vq, unsigned int *nbufs,
587 unsigned int *num_planes, unsigned long sizes[],
590 struct mcam_camera *cam = vb2_get_drv_priv(vq);
592 sizes[0] = cam->pix_format.sizeimage;
593 *num_planes = 1; /* Someday we have to support planar formats... */
594 if (*nbufs < 2 || *nbufs > 32)
595 *nbufs = 6; /* semi-arbitrary numbers */
599 static int mcam_vb_buf_init(struct vb2_buffer *vb)
601 struct mcam_vb_buffer *mvb = vb_to_mvb(vb);
603 INIT_LIST_HEAD(&mvb->queue);
607 static void mcam_vb_buf_queue(struct vb2_buffer *vb)
609 struct mcam_vb_buffer *mvb = vb_to_mvb(vb);
610 struct mcam_camera *cam = vb2_get_drv_priv(vb->vb2_queue);
613 spin_lock_irqsave(&cam->dev_lock, flags);
614 list_add(&cam->buffers, &mvb->queue);
615 spin_unlock_irqrestore(&cam->dev_lock, flags);
619 * vb2 uses these to release the mutex when waiting in dqbuf. I'm
620 * not actually sure we need to do this (I'm not sure that vb2_dqbuf() needs
621 * to be called with the mutex held), but better safe than sorry.
623 static void mcam_vb_wait_prepare(struct vb2_queue *vq)
625 struct mcam_camera *cam = vb2_get_drv_priv(vq);
627 mutex_unlock(&cam->s_mutex);
630 static void mcam_vb_wait_finish(struct vb2_queue *vq)
632 struct mcam_camera *cam = vb2_get_drv_priv(vq);
634 mutex_lock(&cam->s_mutex);
638 * These need to be called with the mutex held from vb2
640 static int mcam_vb_start_streaming(struct vb2_queue *vq)
642 struct mcam_camera *cam = vb2_get_drv_priv(vq);
645 if (cam->state == S_IDLE) {
647 ret = mcam_read_setup(cam, S_STREAMING);
652 static int mcam_vb_stop_streaming(struct vb2_queue *vq)
654 struct mcam_camera *cam = vb2_get_drv_priv(vq);
657 if (cam->state != S_STREAMING)
659 mcam_ctlr_stop_dma(cam);
661 * VB2 reclaims the buffers, so we need to forget
664 spin_lock_irqsave(&cam->dev_lock, flags);
665 INIT_LIST_HEAD(&cam->buffers);
666 spin_unlock_irqrestore(&cam->dev_lock, flags);
671 static const struct vb2_ops mcam_vb2_ops = {
672 .queue_setup = mcam_vb_queue_setup,
673 .buf_init = mcam_vb_buf_init,
674 .buf_queue = mcam_vb_buf_queue,
675 .start_streaming = mcam_vb_start_streaming,
676 .stop_streaming = mcam_vb_stop_streaming,
677 .wait_prepare = mcam_vb_wait_prepare,
678 .wait_finish = mcam_vb_wait_finish,
681 static int mcam_setup_vb2(struct mcam_camera *cam)
683 struct vb2_queue *vq = &cam->vb_queue;
685 memset(vq, 0, sizeof(*vq));
686 vq->type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
687 vq->io_modes = VB2_MMAP; /* Add userptr */
689 vq->ops = &mcam_vb2_ops;
690 vq->mem_ops = &vb2_vmalloc_memops;
691 vq->buf_struct_size = sizeof(struct mcam_vb_buffer);
693 return vb2_queue_init(vq);
696 static void mcam_cleanup_vb2(struct mcam_camera *cam)
698 vb2_queue_release(&cam->vb_queue);
701 static ssize_t mcam_v4l_read(struct file *filp,
702 char __user *buffer, size_t len, loff_t *pos)
704 struct mcam_camera *cam = filp->private_data;
707 mutex_lock(&cam->s_mutex);
708 ret = vb2_read(&cam->vb_queue, buffer, len, pos,
709 filp->f_flags & O_NONBLOCK);
710 mutex_unlock(&cam->s_mutex);
717 * Streaming I/O support.
720 static int mcam_vidioc_streamon(struct file *filp, void *priv,
721 enum v4l2_buf_type type)
723 struct mcam_camera *cam = filp->private_data;
726 mutex_lock(&cam->s_mutex);
727 ret = vb2_streamon(&cam->vb_queue, type);
728 mutex_unlock(&cam->s_mutex);
733 static int mcam_vidioc_streamoff(struct file *filp, void *priv,
734 enum v4l2_buf_type type)
736 struct mcam_camera *cam = filp->private_data;
739 mutex_lock(&cam->s_mutex);
740 ret = vb2_streamoff(&cam->vb_queue, type);
741 mutex_unlock(&cam->s_mutex);
746 static int mcam_vidioc_reqbufs(struct file *filp, void *priv,
747 struct v4l2_requestbuffers *req)
749 struct mcam_camera *cam = filp->private_data;
752 mutex_lock(&cam->s_mutex);
753 ret = vb2_reqbufs(&cam->vb_queue, req);
754 mutex_unlock(&cam->s_mutex);
759 static int mcam_vidioc_querybuf(struct file *filp, void *priv,
760 struct v4l2_buffer *buf)
762 struct mcam_camera *cam = filp->private_data;
765 mutex_lock(&cam->s_mutex);
766 ret = vb2_querybuf(&cam->vb_queue, buf);
767 mutex_unlock(&cam->s_mutex);
771 static int mcam_vidioc_qbuf(struct file *filp, void *priv,
772 struct v4l2_buffer *buf)
774 struct mcam_camera *cam = filp->private_data;
777 mutex_lock(&cam->s_mutex);
778 ret = vb2_qbuf(&cam->vb_queue, buf);
779 mutex_unlock(&cam->s_mutex);
783 static int mcam_vidioc_dqbuf(struct file *filp, void *priv,
784 struct v4l2_buffer *buf)
786 struct mcam_camera *cam = filp->private_data;
789 mutex_lock(&cam->s_mutex);
790 ret = vb2_dqbuf(&cam->vb_queue, buf, filp->f_flags & O_NONBLOCK);
791 mutex_unlock(&cam->s_mutex);
796 static int mcam_v4l_mmap(struct file *filp, struct vm_area_struct *vma)
798 struct mcam_camera *cam = filp->private_data;
801 mutex_lock(&cam->s_mutex);
802 ret = vb2_mmap(&cam->vb_queue, vma);
803 mutex_unlock(&cam->s_mutex);
809 static int mcam_v4l_open(struct file *filp)
811 struct mcam_camera *cam = video_drvdata(filp);
814 filp->private_data = cam;
816 mutex_lock(&cam->s_mutex);
817 if (cam->users == 0) {
818 ret = mcam_setup_vb2(cam);
821 mcam_ctlr_power_up(cam);
822 __mcam_cam_reset(cam);
823 mcam_set_config_needed(cam, 1);
827 mutex_unlock(&cam->s_mutex);
832 static int mcam_v4l_release(struct file *filp)
834 struct mcam_camera *cam = filp->private_data;
836 mutex_lock(&cam->s_mutex);
838 if (filp == cam->owner) {
839 mcam_ctlr_stop_dma(cam);
842 if (cam->users == 0) {
843 mcam_cleanup_vb2(cam);
844 mcam_ctlr_power_down(cam);
845 if (alloc_bufs_at_read)
846 mcam_free_dma_bufs(cam);
848 mutex_unlock(&cam->s_mutex);
854 static unsigned int mcam_v4l_poll(struct file *filp,
855 struct poll_table_struct *pt)
857 struct mcam_camera *cam = filp->private_data;
860 mutex_lock(&cam->s_mutex);
861 ret = vb2_poll(&cam->vb_queue, filp, pt);
862 mutex_unlock(&cam->s_mutex);
868 static int mcam_vidioc_queryctrl(struct file *filp, void *priv,
869 struct v4l2_queryctrl *qc)
871 struct mcam_camera *cam = priv;
874 mutex_lock(&cam->s_mutex);
875 ret = sensor_call(cam, core, queryctrl, qc);
876 mutex_unlock(&cam->s_mutex);
881 static int mcam_vidioc_g_ctrl(struct file *filp, void *priv,
882 struct v4l2_control *ctrl)
884 struct mcam_camera *cam = priv;
887 mutex_lock(&cam->s_mutex);
888 ret = sensor_call(cam, core, g_ctrl, ctrl);
889 mutex_unlock(&cam->s_mutex);
894 static int mcam_vidioc_s_ctrl(struct file *filp, void *priv,
895 struct v4l2_control *ctrl)
897 struct mcam_camera *cam = priv;
900 mutex_lock(&cam->s_mutex);
901 ret = sensor_call(cam, core, s_ctrl, ctrl);
902 mutex_unlock(&cam->s_mutex);
907 static int mcam_vidioc_querycap(struct file *file, void *priv,
908 struct v4l2_capability *cap)
910 strcpy(cap->driver, "marvell_ccic");
911 strcpy(cap->card, "marvell_ccic");
913 cap->capabilities = V4L2_CAP_VIDEO_CAPTURE |
914 V4L2_CAP_READWRITE | V4L2_CAP_STREAMING;
920 * The default format we use until somebody says otherwise.
922 static const struct v4l2_pix_format mcam_def_pix_format = {
924 .height = VGA_HEIGHT,
925 .pixelformat = V4L2_PIX_FMT_YUYV,
926 .field = V4L2_FIELD_NONE,
927 .bytesperline = VGA_WIDTH*2,
928 .sizeimage = VGA_WIDTH*VGA_HEIGHT*2,
931 static const enum v4l2_mbus_pixelcode mcam_def_mbus_code =
932 V4L2_MBUS_FMT_YUYV8_2X8;
934 static int mcam_vidioc_enum_fmt_vid_cap(struct file *filp,
935 void *priv, struct v4l2_fmtdesc *fmt)
937 if (fmt->index >= N_MCAM_FMTS)
939 strlcpy(fmt->description, mcam_formats[fmt->index].desc,
940 sizeof(fmt->description));
941 fmt->pixelformat = mcam_formats[fmt->index].pixelformat;
945 static int mcam_vidioc_try_fmt_vid_cap(struct file *filp, void *priv,
946 struct v4l2_format *fmt)
948 struct mcam_camera *cam = priv;
949 struct mcam_format_struct *f;
950 struct v4l2_pix_format *pix = &fmt->fmt.pix;
951 struct v4l2_mbus_framefmt mbus_fmt;
954 f = mcam_find_format(pix->pixelformat);
955 pix->pixelformat = f->pixelformat;
956 v4l2_fill_mbus_format(&mbus_fmt, pix, f->mbus_code);
957 mutex_lock(&cam->s_mutex);
958 ret = sensor_call(cam, video, try_mbus_fmt, &mbus_fmt);
959 mutex_unlock(&cam->s_mutex);
960 v4l2_fill_pix_format(pix, &mbus_fmt);
961 pix->bytesperline = pix->width * f->bpp;
962 pix->sizeimage = pix->height * pix->bytesperline;
966 static int mcam_vidioc_s_fmt_vid_cap(struct file *filp, void *priv,
967 struct v4l2_format *fmt)
969 struct mcam_camera *cam = priv;
970 struct mcam_format_struct *f;
974 * Can't do anything if the device is not idle
975 * Also can't if there are streaming buffers in place.
977 if (cam->state != S_IDLE || cam->vb_queue.num_buffers > 0)
980 f = mcam_find_format(fmt->fmt.pix.pixelformat);
983 * See if the formatting works in principle.
985 ret = mcam_vidioc_try_fmt_vid_cap(filp, priv, fmt);
989 * Now we start to change things for real, so let's do it
992 mutex_lock(&cam->s_mutex);
993 cam->pix_format = fmt->fmt.pix;
994 cam->mbus_code = f->mbus_code;
997 * Make sure we have appropriate DMA buffers.
1000 if (cam->nbufs > 0 && cam->dma_buf_size < cam->pix_format.sizeimage)
1001 mcam_free_dma_bufs(cam);
1002 if (cam->nbufs == 0) {
1003 if (mcam_alloc_dma_bufs(cam, 0))
1007 * It looks like this might work, so let's program the sensor.
1009 ret = mcam_cam_configure(cam);
1011 ret = mcam_ctlr_configure(cam);
1013 mutex_unlock(&cam->s_mutex);
1018 * Return our stored notion of how the camera is/should be configured.
1019 * The V4l2 spec wants us to be smarter, and actually get this from
1020 * the camera (and not mess with it at open time). Someday.
1022 static int mcam_vidioc_g_fmt_vid_cap(struct file *filp, void *priv,
1023 struct v4l2_format *f)
1025 struct mcam_camera *cam = priv;
1027 f->fmt.pix = cam->pix_format;
1032 * We only have one input - the sensor - so minimize the nonsense here.
1034 static int mcam_vidioc_enum_input(struct file *filp, void *priv,
1035 struct v4l2_input *input)
1037 if (input->index != 0)
1040 input->type = V4L2_INPUT_TYPE_CAMERA;
1041 input->std = V4L2_STD_ALL; /* Not sure what should go here */
1042 strcpy(input->name, "Camera");
1046 static int mcam_vidioc_g_input(struct file *filp, void *priv, unsigned int *i)
1052 static int mcam_vidioc_s_input(struct file *filp, void *priv, unsigned int i)
1060 static int mcam_vidioc_s_std(struct file *filp, void *priv, v4l2_std_id *a)
1066 * G/S_PARM. Most of this is done by the sensor, but we are
1067 * the level which controls the number of read buffers.
1069 static int mcam_vidioc_g_parm(struct file *filp, void *priv,
1070 struct v4l2_streamparm *parms)
1072 struct mcam_camera *cam = priv;
1075 mutex_lock(&cam->s_mutex);
1076 ret = sensor_call(cam, video, g_parm, parms);
1077 mutex_unlock(&cam->s_mutex);
1078 parms->parm.capture.readbuffers = n_dma_bufs;
1082 static int mcam_vidioc_s_parm(struct file *filp, void *priv,
1083 struct v4l2_streamparm *parms)
1085 struct mcam_camera *cam = priv;
1088 mutex_lock(&cam->s_mutex);
1089 ret = sensor_call(cam, video, s_parm, parms);
1090 mutex_unlock(&cam->s_mutex);
1091 parms->parm.capture.readbuffers = n_dma_bufs;
1095 static int mcam_vidioc_g_chip_ident(struct file *file, void *priv,
1096 struct v4l2_dbg_chip_ident *chip)
1098 struct mcam_camera *cam = priv;
1100 chip->ident = V4L2_IDENT_NONE;
1102 if (v4l2_chip_match_host(&chip->match)) {
1103 chip->ident = cam->chip_id;
1106 return sensor_call(cam, core, g_chip_ident, chip);
1109 static int mcam_vidioc_enum_framesizes(struct file *filp, void *priv,
1110 struct v4l2_frmsizeenum *sizes)
1112 struct mcam_camera *cam = priv;
1115 mutex_lock(&cam->s_mutex);
1116 ret = sensor_call(cam, video, enum_framesizes, sizes);
1117 mutex_unlock(&cam->s_mutex);
1121 static int mcam_vidioc_enum_frameintervals(struct file *filp, void *priv,
1122 struct v4l2_frmivalenum *interval)
1124 struct mcam_camera *cam = priv;
1127 mutex_lock(&cam->s_mutex);
1128 ret = sensor_call(cam, video, enum_frameintervals, interval);
1129 mutex_unlock(&cam->s_mutex);
1133 #ifdef CONFIG_VIDEO_ADV_DEBUG
1134 static int mcam_vidioc_g_register(struct file *file, void *priv,
1135 struct v4l2_dbg_register *reg)
1137 struct mcam_camera *cam = priv;
1139 if (v4l2_chip_match_host(®->match)) {
1140 reg->val = mcam_reg_read(cam, reg->reg);
1144 return sensor_call(cam, core, g_register, reg);
1147 static int mcam_vidioc_s_register(struct file *file, void *priv,
1148 struct v4l2_dbg_register *reg)
1150 struct mcam_camera *cam = priv;
1152 if (v4l2_chip_match_host(®->match)) {
1153 mcam_reg_write(cam, reg->reg, reg->val);
1156 return sensor_call(cam, core, s_register, reg);
1161 * This template device holds all of those v4l2 methods; we
1162 * clone it for specific real devices.
1165 static const struct v4l2_file_operations mcam_v4l_fops = {
1166 .owner = THIS_MODULE,
1167 .open = mcam_v4l_open,
1168 .release = mcam_v4l_release,
1169 .read = mcam_v4l_read,
1170 .poll = mcam_v4l_poll,
1171 .mmap = mcam_v4l_mmap,
1172 .unlocked_ioctl = video_ioctl2,
1175 static const struct v4l2_ioctl_ops mcam_v4l_ioctl_ops = {
1176 .vidioc_querycap = mcam_vidioc_querycap,
1177 .vidioc_enum_fmt_vid_cap = mcam_vidioc_enum_fmt_vid_cap,
1178 .vidioc_try_fmt_vid_cap = mcam_vidioc_try_fmt_vid_cap,
1179 .vidioc_s_fmt_vid_cap = mcam_vidioc_s_fmt_vid_cap,
1180 .vidioc_g_fmt_vid_cap = mcam_vidioc_g_fmt_vid_cap,
1181 .vidioc_enum_input = mcam_vidioc_enum_input,
1182 .vidioc_g_input = mcam_vidioc_g_input,
1183 .vidioc_s_input = mcam_vidioc_s_input,
1184 .vidioc_s_std = mcam_vidioc_s_std,
1185 .vidioc_reqbufs = mcam_vidioc_reqbufs,
1186 .vidioc_querybuf = mcam_vidioc_querybuf,
1187 .vidioc_qbuf = mcam_vidioc_qbuf,
1188 .vidioc_dqbuf = mcam_vidioc_dqbuf,
1189 .vidioc_streamon = mcam_vidioc_streamon,
1190 .vidioc_streamoff = mcam_vidioc_streamoff,
1191 .vidioc_queryctrl = mcam_vidioc_queryctrl,
1192 .vidioc_g_ctrl = mcam_vidioc_g_ctrl,
1193 .vidioc_s_ctrl = mcam_vidioc_s_ctrl,
1194 .vidioc_g_parm = mcam_vidioc_g_parm,
1195 .vidioc_s_parm = mcam_vidioc_s_parm,
1196 .vidioc_enum_framesizes = mcam_vidioc_enum_framesizes,
1197 .vidioc_enum_frameintervals = mcam_vidioc_enum_frameintervals,
1198 .vidioc_g_chip_ident = mcam_vidioc_g_chip_ident,
1199 #ifdef CONFIG_VIDEO_ADV_DEBUG
1200 .vidioc_g_register = mcam_vidioc_g_register,
1201 .vidioc_s_register = mcam_vidioc_s_register,
1205 static struct video_device mcam_v4l_template = {
1207 .tvnorms = V4L2_STD_NTSC_M,
1208 .current_norm = V4L2_STD_NTSC_M, /* make mplayer happy */
1210 .fops = &mcam_v4l_fops,
1211 .ioctl_ops = &mcam_v4l_ioctl_ops,
1212 .release = video_device_release_empty,
1215 /* ---------------------------------------------------------------------- */
1217 * Interrupt handler stuff
1222 static void mcam_frame_tasklet(unsigned long data)
1224 struct mcam_camera *cam = (struct mcam_camera *) data;
1226 unsigned long flags;
1227 struct mcam_vb_buffer *buf;
1229 spin_lock_irqsave(&cam->dev_lock, flags);
1230 for (i = 0; i < cam->nbufs; i++) {
1231 int bufno = cam->next_buf;
1233 if (cam->state != S_STREAMING || bufno < 0)
1234 break; /* I/O got stopped */
1235 if (++(cam->next_buf) >= cam->nbufs)
1237 if (!test_bit(bufno, &cam->flags))
1239 if (list_empty(&cam->buffers))
1240 break; /* Leave it valid, hope for better later */
1241 clear_bit(bufno, &cam->flags);
1242 buf = list_first_entry(&cam->buffers, struct mcam_vb_buffer,
1244 list_del_init(&buf->queue);
1246 * Drop the lock during the big copy. This *should* be safe...
1248 spin_unlock_irqrestore(&cam->dev_lock, flags);
1249 memcpy(vb2_plane_vaddr(&buf->vb_buf, 0), cam->dma_bufs[bufno],
1250 cam->pix_format.sizeimage);
1251 buf->vb_buf.v4l2_buf.bytesused = cam->pix_format.sizeimage;
1252 buf->vb_buf.v4l2_buf.sequence = cam->buf_seq[bufno];
1253 buf->vb_buf.v4l2_buf.flags &= ~V4L2_BUF_FLAG_QUEUED;
1254 buf->vb_buf.v4l2_buf.flags |= V4L2_BUF_FLAG_DONE;
1255 vb2_set_plane_payload(&buf->vb_buf, 0,
1256 cam->pix_format.sizeimage);
1257 vb2_buffer_done(&buf->vb_buf, VB2_BUF_STATE_DONE);
1258 spin_lock_irqsave(&cam->dev_lock, flags);
1260 spin_unlock_irqrestore(&cam->dev_lock, flags);
1265 static void mcam_frame_complete(struct mcam_camera *cam, int frame)
1268 * Basic frame housekeeping.
1270 if (test_bit(frame, &cam->flags) && printk_ratelimit())
1271 cam_err(cam, "Frame overrun on %d, frames lost\n", frame);
1272 set_bit(frame, &cam->flags);
1273 clear_bit(CF_DMA_ACTIVE, &cam->flags);
1274 if (cam->next_buf < 0)
1275 cam->next_buf = frame;
1276 cam->buf_seq[frame] = ++(cam->sequence);
1278 switch (cam->state) {
1280 * For the streaming case, we defer the real work to the
1283 * FIXME: if the application is not consuming the buffers,
1284 * we should eventually put things on hold and restart in
1288 tasklet_schedule(&cam->s_tasklet);
1292 cam_err(cam, "Frame interrupt in non-operational state\n");
1300 int mccic_irq(struct mcam_camera *cam, unsigned int irqs)
1302 unsigned int frame, handled = 0;
1304 mcam_reg_write(cam, REG_IRQSTAT, FRAMEIRQS); /* Clear'em all */
1306 * Handle any frame completions. There really should
1307 * not be more than one of these, or we have fallen
1310 for (frame = 0; frame < cam->nbufs; frame++)
1311 if (irqs & (IRQ_EOF0 << frame)) {
1312 mcam_frame_complete(cam, frame);
1316 * If a frame starts, note that we have DMA active. This
1317 * code assumes that we won't get multiple frame interrupts
1318 * at once; may want to rethink that.
1320 if (irqs & (IRQ_SOF0 | IRQ_SOF1 | IRQ_SOF2)) {
1321 set_bit(CF_DMA_ACTIVE, &cam->flags);
1328 * Registration and such.
1331 static struct ov7670_config sensor_cfg = {
1333 * Exclude QCIF mode, because it only captures a tiny portion
1341 int mccic_register(struct mcam_camera *cam)
1343 struct i2c_board_info ov7670_info = {
1346 .platform_data = &sensor_cfg,
1353 ret = v4l2_device_register(cam->dev, &cam->v4l2_dev);
1357 mutex_init(&cam->s_mutex);
1358 cam->state = S_NOTREADY;
1359 mcam_set_config_needed(cam, 1);
1360 cam->pix_format = mcam_def_pix_format;
1361 cam->mbus_code = mcam_def_mbus_code;
1362 INIT_LIST_HEAD(&cam->dev_list);
1363 INIT_LIST_HEAD(&cam->buffers);
1364 tasklet_init(&cam->s_tasklet, mcam_frame_tasklet, (unsigned long) cam);
1366 mcam_ctlr_init(cam);
1369 * Try to find the sensor.
1371 sensor_cfg.clock_speed = cam->clock_speed;
1372 sensor_cfg.use_smbus = cam->use_smbus;
1373 cam->sensor_addr = ov7670_info.addr;
1374 cam->sensor = v4l2_i2c_new_subdev_board(&cam->v4l2_dev,
1375 cam->i2c_adapter, &ov7670_info, NULL);
1376 if (cam->sensor == NULL) {
1378 goto out_unregister;
1381 ret = mcam_cam_init(cam);
1383 goto out_unregister;
1385 * Get the v4l2 setup done.
1387 mutex_lock(&cam->s_mutex);
1388 cam->vdev = mcam_v4l_template;
1389 cam->vdev.debug = 0;
1390 cam->vdev.v4l2_dev = &cam->v4l2_dev;
1391 ret = video_register_device(&cam->vdev, VFL_TYPE_GRABBER, -1);
1394 video_set_drvdata(&cam->vdev, cam);
1397 * If so requested, try to get our DMA buffers now.
1399 if (!alloc_bufs_at_read) {
1400 if (mcam_alloc_dma_bufs(cam, 1))
1401 cam_warn(cam, "Unable to alloc DMA buffers at load"
1402 " will try again later.");
1406 mutex_unlock(&cam->s_mutex);
1409 v4l2_device_unregister(&cam->v4l2_dev);
1414 void mccic_shutdown(struct mcam_camera *cam)
1417 * If we have no users (and we really, really should have no
1418 * users) the device will already be powered down. Trying to
1419 * take it down again will wedge the machine, which is frowned
1422 if (cam->users > 0) {
1423 cam_warn(cam, "Removing a device with users!\n");
1424 mcam_ctlr_power_down(cam);
1426 vb2_queue_release(&cam->vb_queue);
1427 mcam_free_dma_bufs(cam);
1428 video_unregister_device(&cam->vdev);
1429 v4l2_device_unregister(&cam->v4l2_dev);
1437 void mccic_suspend(struct mcam_camera *cam)
1439 enum mcam_state cstate = cam->state;
1441 mcam_ctlr_stop_dma(cam);
1442 mcam_ctlr_power_down(cam);
1443 cam->state = cstate;
1446 int mccic_resume(struct mcam_camera *cam)
1450 mutex_lock(&cam->s_mutex);
1451 if (cam->users > 0) {
1452 mcam_ctlr_power_up(cam);
1453 __mcam_cam_reset(cam);
1455 mcam_ctlr_power_down(cam);
1457 mutex_unlock(&cam->s_mutex);
1459 set_bit(CF_CONFIG_NEEDED, &cam->flags);
1460 if (cam->state == S_STREAMING)
1461 ret = mcam_read_setup(cam, cam->state);
1464 #endif /* CONFIG_PM */