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/slab.h>
15 #include <linux/device.h>
16 #include <linux/wait.h>
17 #include <linux/list.h>
18 #include <linux/dma-mapping.h>
19 #include <linux/delay.h>
20 #include <linux/vmalloc.h>
22 #include <linux/clk.h>
23 #include <linux/videodev2.h>
24 #include <media/v4l2-device.h>
25 #include <media/v4l2-ioctl.h>
26 #include <media/v4l2-ctrls.h>
27 #include <media/ov7670.h>
28 #include <media/videobuf2-vmalloc.h>
29 #include <media/videobuf2-dma-contig.h>
30 #include <media/videobuf2-dma-sg.h>
32 #include "mcam-core.h"
34 #ifdef MCAM_MODE_VMALLOC
36 * Internal DMA buffer management. Since the controller cannot do S/G I/O,
37 * we must have physically contiguous buffers to bring frames into.
38 * These parameters control how many buffers we use, whether we
39 * allocate them at load time (better chance of success, but nails down
40 * memory) or when somebody tries to use the camera (riskier), and,
41 * for load-time allocation, how big they should be.
43 * The controller can cycle through three buffers. We could use
44 * more by flipping pointers around, but it probably makes little
48 static bool alloc_bufs_at_read;
49 module_param(alloc_bufs_at_read, bool, 0444);
50 MODULE_PARM_DESC(alloc_bufs_at_read,
51 "Non-zero value causes DMA buffers to be allocated when the "
52 "video capture device is read, rather than at module load "
53 "time. This saves memory, but decreases the chances of "
54 "successfully getting those buffers. This parameter is "
55 "only used in the vmalloc buffer mode");
57 static int n_dma_bufs = 3;
58 module_param(n_dma_bufs, uint, 0644);
59 MODULE_PARM_DESC(n_dma_bufs,
60 "The number of DMA buffers to allocate. Can be either two "
61 "(saves memory, makes timing tighter) or three.");
63 static int dma_buf_size = VGA_WIDTH * VGA_HEIGHT * 2; /* Worst case */
64 module_param(dma_buf_size, uint, 0444);
65 MODULE_PARM_DESC(dma_buf_size,
66 "The size of the allocated DMA buffers. If actual operating "
67 "parameters require larger buffers, an attempt to reallocate "
69 #else /* MCAM_MODE_VMALLOC */
70 static const bool alloc_bufs_at_read = 0;
71 static const int n_dma_bufs = 3; /* Used by S/G_PARM */
72 #endif /* MCAM_MODE_VMALLOC */
75 module_param(flip, bool, 0444);
76 MODULE_PARM_DESC(flip,
77 "If set, the sensor will be instructed to flip the image "
80 static int buffer_mode = -1;
81 module_param(buffer_mode, int, 0444);
82 MODULE_PARM_DESC(buffer_mode,
83 "Set the buffer mode to be used; default is to go with what "
84 "the platform driver asks for. Set to 0 for vmalloc, 1 for "
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 */
95 #define CF_SINGLE_BUFFER 5 /* Running with a single buffer */
96 #define CF_SG_RESTART 6 /* SG restart needed */
98 #define sensor_call(cam, o, f, args...) \
99 v4l2_subdev_call(cam->sensor, o, f, ##args)
101 static struct mcam_format_struct {
104 int bpp; /* Bytes per pixel */
105 enum v4l2_mbus_pixelcode mbus_code;
108 .desc = "YUYV 4:2:2",
109 .pixelformat = V4L2_PIX_FMT_YUYV,
110 .mbus_code = V4L2_MBUS_FMT_YUYV8_2X8,
115 .pixelformat = V4L2_PIX_FMT_RGB444,
116 .mbus_code = V4L2_MBUS_FMT_RGB444_2X8_PADHI_LE,
121 .pixelformat = V4L2_PIX_FMT_RGB565,
122 .mbus_code = V4L2_MBUS_FMT_RGB565_2X8_LE,
126 .desc = "Raw RGB Bayer",
127 .pixelformat = V4L2_PIX_FMT_SBGGR8,
128 .mbus_code = V4L2_MBUS_FMT_SBGGR8_1X8,
132 #define N_MCAM_FMTS ARRAY_SIZE(mcam_formats)
134 static struct mcam_format_struct *mcam_find_format(u32 pixelformat)
138 for (i = 0; i < N_MCAM_FMTS; i++)
139 if (mcam_formats[i].pixelformat == pixelformat)
140 return mcam_formats + i;
141 /* Not found? Then return the first format. */
146 * The default format we use until somebody says otherwise.
148 static const struct v4l2_pix_format mcam_def_pix_format = {
150 .height = VGA_HEIGHT,
151 .pixelformat = V4L2_PIX_FMT_YUYV,
152 .field = V4L2_FIELD_NONE,
153 .bytesperline = VGA_WIDTH*2,
154 .sizeimage = VGA_WIDTH*VGA_HEIGHT*2,
157 static const enum v4l2_mbus_pixelcode mcam_def_mbus_code =
158 V4L2_MBUS_FMT_YUYV8_2X8;
162 * The two-word DMA descriptor format used by the Armada 610 and like. There
163 * Is a three-word format as well (set C1_DESC_3WORD) where the third
164 * word is a pointer to the next descriptor, but we don't use it. Two-word
165 * descriptors have to be contiguous in memory.
167 struct mcam_dma_desc {
173 * Our buffer type for working with videobuf2. Note that the vb2
174 * developers have decreed that struct vb2_buffer must be at the
175 * beginning of this structure.
177 struct mcam_vb_buffer {
178 struct vb2_buffer vb_buf;
179 struct list_head queue;
180 struct mcam_dma_desc *dma_desc; /* Descriptor virtual address */
181 dma_addr_t dma_desc_pa; /* Descriptor physical address */
182 int dma_desc_nent; /* Number of mapped descriptors */
185 static inline struct mcam_vb_buffer *vb_to_mvb(struct vb2_buffer *vb)
187 return container_of(vb, struct mcam_vb_buffer, vb_buf);
191 * Hand a completed buffer back to user space.
193 static void mcam_buffer_done(struct mcam_camera *cam, int frame,
194 struct vb2_buffer *vbuf)
196 vbuf->v4l2_buf.bytesused = cam->pix_format.sizeimage;
197 vbuf->v4l2_buf.sequence = cam->buf_seq[frame];
198 vb2_set_plane_payload(vbuf, 0, cam->pix_format.sizeimage);
199 vb2_buffer_done(vbuf, VB2_BUF_STATE_DONE);
205 * Debugging and related.
207 #define cam_err(cam, fmt, arg...) \
208 dev_err((cam)->dev, fmt, ##arg);
209 #define cam_warn(cam, fmt, arg...) \
210 dev_warn((cam)->dev, fmt, ##arg);
211 #define cam_dbg(cam, fmt, arg...) \
212 dev_dbg((cam)->dev, fmt, ##arg);
216 * Flag manipulation helpers
218 static void mcam_reset_buffers(struct mcam_camera *cam)
223 for (i = 0; i < cam->nbufs; i++)
224 clear_bit(i, &cam->flags);
227 static inline int mcam_needs_config(struct mcam_camera *cam)
229 return test_bit(CF_CONFIG_NEEDED, &cam->flags);
232 static void mcam_set_config_needed(struct mcam_camera *cam, int needed)
235 set_bit(CF_CONFIG_NEEDED, &cam->flags);
237 clear_bit(CF_CONFIG_NEEDED, &cam->flags);
240 /* ------------------------------------------------------------------- */
242 * Make the controller start grabbing images. Everything must
243 * be set up before doing this.
245 static void mcam_ctlr_start(struct mcam_camera *cam)
247 /* set_bit performs a read, so no other barrier should be
249 mcam_reg_set_bit(cam, REG_CTRL0, C0_ENABLE);
252 static void mcam_ctlr_stop(struct mcam_camera *cam)
254 mcam_reg_clear_bit(cam, REG_CTRL0, C0_ENABLE);
257 static void mcam_enable_mipi(struct mcam_camera *mcam)
259 /* Using MIPI mode and enable MIPI */
260 cam_dbg(mcam, "camera: DPHY3=0x%x, DPHY5=0x%x, DPHY6=0x%x\n",
261 mcam->dphy[0], mcam->dphy[1], mcam->dphy[2]);
262 mcam_reg_write(mcam, REG_CSI2_DPHY3, mcam->dphy[0]);
263 mcam_reg_write(mcam, REG_CSI2_DPHY5, mcam->dphy[1]);
264 mcam_reg_write(mcam, REG_CSI2_DPHY6, mcam->dphy[2]);
266 if (!mcam->mipi_enabled) {
267 if (mcam->lane > 4 || mcam->lane <= 0) {
268 cam_warn(mcam, "lane number error\n");
269 mcam->lane = 1; /* set the default value */
272 * 0x41 actives 1 lane
273 * 0x43 actives 2 lanes
274 * 0x45 actives 3 lanes (never happen)
275 * 0x47 actives 4 lanes
277 mcam_reg_write(mcam, REG_CSI2_CTRL0,
278 CSI2_C0_MIPI_EN | CSI2_C0_ACT_LANE(mcam->lane));
279 mcam_reg_write(mcam, REG_CLKCTRL,
280 (mcam->mclk_src << 29) | mcam->mclk_div);
282 mcam->mipi_enabled = true;
286 static void mcam_disable_mipi(struct mcam_camera *mcam)
288 /* Using Parallel mode or disable MIPI */
289 mcam_reg_write(mcam, REG_CSI2_CTRL0, 0x0);
290 mcam_reg_write(mcam, REG_CSI2_DPHY3, 0x0);
291 mcam_reg_write(mcam, REG_CSI2_DPHY5, 0x0);
292 mcam_reg_write(mcam, REG_CSI2_DPHY6, 0x0);
293 mcam->mipi_enabled = false;
296 /* ------------------------------------------------------------------- */
298 #ifdef MCAM_MODE_VMALLOC
300 * Code specific to the vmalloc buffer mode.
304 * Allocate in-kernel DMA buffers for vmalloc mode.
306 static int mcam_alloc_dma_bufs(struct mcam_camera *cam, int loadtime)
310 mcam_set_config_needed(cam, 1);
312 cam->dma_buf_size = dma_buf_size;
314 cam->dma_buf_size = cam->pix_format.sizeimage;
319 for (i = 0; i < n_dma_bufs; i++) {
320 cam->dma_bufs[i] = dma_alloc_coherent(cam->dev,
321 cam->dma_buf_size, cam->dma_handles + i,
323 if (cam->dma_bufs[i] == NULL) {
324 cam_warn(cam, "Failed to allocate DMA buffer\n");
330 switch (cam->nbufs) {
332 dma_free_coherent(cam->dev, cam->dma_buf_size,
333 cam->dma_bufs[0], cam->dma_handles[0]);
336 cam_err(cam, "Insufficient DMA buffers, cannot operate\n");
341 cam_warn(cam, "Will limp along with only 2 buffers\n");
347 static void mcam_free_dma_bufs(struct mcam_camera *cam)
351 for (i = 0; i < cam->nbufs; i++) {
352 dma_free_coherent(cam->dev, cam->dma_buf_size,
353 cam->dma_bufs[i], cam->dma_handles[i]);
354 cam->dma_bufs[i] = NULL;
361 * Set up DMA buffers when operating in vmalloc mode
363 static void mcam_ctlr_dma_vmalloc(struct mcam_camera *cam)
366 * Store the first two Y buffers (we aren't supporting
367 * planar formats for now, so no UV bufs). Then either
368 * set the third if it exists, or tell the controller
371 mcam_reg_write(cam, REG_Y0BAR, cam->dma_handles[0]);
372 mcam_reg_write(cam, REG_Y1BAR, cam->dma_handles[1]);
373 if (cam->nbufs > 2) {
374 mcam_reg_write(cam, REG_Y2BAR, cam->dma_handles[2]);
375 mcam_reg_clear_bit(cam, REG_CTRL1, C1_TWOBUFS);
377 mcam_reg_set_bit(cam, REG_CTRL1, C1_TWOBUFS);
378 if (cam->chip_id == MCAM_CAFE)
379 mcam_reg_write(cam, REG_UBAR, 0); /* 32 bits only */
383 * Copy data out to user space in the vmalloc case
385 static void mcam_frame_tasklet(unsigned long data)
387 struct mcam_camera *cam = (struct mcam_camera *) data;
390 struct mcam_vb_buffer *buf;
392 spin_lock_irqsave(&cam->dev_lock, flags);
393 for (i = 0; i < cam->nbufs; i++) {
394 int bufno = cam->next_buf;
396 if (cam->state != S_STREAMING || bufno < 0)
397 break; /* I/O got stopped */
398 if (++(cam->next_buf) >= cam->nbufs)
400 if (!test_bit(bufno, &cam->flags))
402 if (list_empty(&cam->buffers)) {
403 cam->frame_state.singles++;
404 break; /* Leave it valid, hope for better later */
406 cam->frame_state.delivered++;
407 clear_bit(bufno, &cam->flags);
408 buf = list_first_entry(&cam->buffers, struct mcam_vb_buffer,
410 list_del_init(&buf->queue);
412 * Drop the lock during the big copy. This *should* be safe...
414 spin_unlock_irqrestore(&cam->dev_lock, flags);
415 memcpy(vb2_plane_vaddr(&buf->vb_buf, 0), cam->dma_bufs[bufno],
416 cam->pix_format.sizeimage);
417 mcam_buffer_done(cam, bufno, &buf->vb_buf);
418 spin_lock_irqsave(&cam->dev_lock, flags);
420 spin_unlock_irqrestore(&cam->dev_lock, flags);
425 * Make sure our allocated buffers are up to the task.
427 static int mcam_check_dma_buffers(struct mcam_camera *cam)
429 if (cam->nbufs > 0 && cam->dma_buf_size < cam->pix_format.sizeimage)
430 mcam_free_dma_bufs(cam);
432 return mcam_alloc_dma_bufs(cam, 0);
436 static void mcam_vmalloc_done(struct mcam_camera *cam, int frame)
438 tasklet_schedule(&cam->s_tasklet);
441 #else /* MCAM_MODE_VMALLOC */
443 static inline int mcam_alloc_dma_bufs(struct mcam_camera *cam, int loadtime)
448 static inline void mcam_free_dma_bufs(struct mcam_camera *cam)
453 static inline int mcam_check_dma_buffers(struct mcam_camera *cam)
460 #endif /* MCAM_MODE_VMALLOC */
463 #ifdef MCAM_MODE_DMA_CONTIG
464 /* ---------------------------------------------------------------------- */
466 * DMA-contiguous code.
469 * Set up a contiguous buffer for the given frame. Here also is where
470 * the underrun strategy is set: if there is no buffer available, reuse
471 * the buffer from the other BAR and set the CF_SINGLE_BUFFER flag to
472 * keep the interrupt handler from giving that buffer back to user
473 * space. In this way, we always have a buffer to DMA to and don't
474 * have to try to play games stopping and restarting the controller.
476 static void mcam_set_contig_buffer(struct mcam_camera *cam, int frame)
478 struct mcam_vb_buffer *buf;
480 * If there are no available buffers, go into single mode
482 if (list_empty(&cam->buffers)) {
483 buf = cam->vb_bufs[frame ^ 0x1];
484 set_bit(CF_SINGLE_BUFFER, &cam->flags);
485 cam->frame_state.singles++;
488 * OK, we have a buffer we can use.
490 buf = list_first_entry(&cam->buffers, struct mcam_vb_buffer,
492 list_del_init(&buf->queue);
493 clear_bit(CF_SINGLE_BUFFER, &cam->flags);
496 cam->vb_bufs[frame] = buf;
497 mcam_reg_write(cam, frame == 0 ? REG_Y0BAR : REG_Y1BAR,
498 vb2_dma_contig_plane_dma_addr(&buf->vb_buf, 0));
502 * Initial B_DMA_contig setup.
504 static void mcam_ctlr_dma_contig(struct mcam_camera *cam)
506 mcam_reg_set_bit(cam, REG_CTRL1, C1_TWOBUFS);
508 mcam_set_contig_buffer(cam, 0);
509 mcam_set_contig_buffer(cam, 1);
513 * Frame completion handling.
515 static void mcam_dma_contig_done(struct mcam_camera *cam, int frame)
517 struct mcam_vb_buffer *buf = cam->vb_bufs[frame];
519 if (!test_bit(CF_SINGLE_BUFFER, &cam->flags)) {
520 cam->frame_state.delivered++;
521 mcam_buffer_done(cam, frame, &buf->vb_buf);
523 mcam_set_contig_buffer(cam, frame);
526 #endif /* MCAM_MODE_DMA_CONTIG */
528 #ifdef MCAM_MODE_DMA_SG
529 /* ---------------------------------------------------------------------- */
531 * Scatter/gather-specific code.
535 * Set up the next buffer for S/G I/O; caller should be sure that
536 * the controller is stopped and a buffer is available.
538 static void mcam_sg_next_buffer(struct mcam_camera *cam)
540 struct mcam_vb_buffer *buf;
542 buf = list_first_entry(&cam->buffers, struct mcam_vb_buffer, queue);
543 list_del_init(&buf->queue);
545 * Very Bad Not Good Things happen if you don't clear
546 * C1_DESC_ENA before making any descriptor changes.
548 mcam_reg_clear_bit(cam, REG_CTRL1, C1_DESC_ENA);
549 mcam_reg_write(cam, REG_DMA_DESC_Y, buf->dma_desc_pa);
550 mcam_reg_write(cam, REG_DESC_LEN_Y,
551 buf->dma_desc_nent*sizeof(struct mcam_dma_desc));
552 mcam_reg_write(cam, REG_DESC_LEN_U, 0);
553 mcam_reg_write(cam, REG_DESC_LEN_V, 0);
554 mcam_reg_set_bit(cam, REG_CTRL1, C1_DESC_ENA);
555 cam->vb_bufs[0] = buf;
559 * Initial B_DMA_sg setup
561 static void mcam_ctlr_dma_sg(struct mcam_camera *cam)
564 * The list-empty condition can hit us at resume time
565 * if the buffer list was empty when the system was suspended.
567 if (list_empty(&cam->buffers)) {
568 set_bit(CF_SG_RESTART, &cam->flags);
572 mcam_reg_clear_bit(cam, REG_CTRL1, C1_DESC_3WORD);
573 mcam_sg_next_buffer(cam);
579 * Frame completion with S/G is trickier. We can't muck with
580 * a descriptor chain on the fly, since the controller buffers it
581 * internally. So we have to actually stop and restart; Marvell
582 * says this is the way to do it.
584 * Of course, stopping is easier said than done; experience shows
585 * that the controller can start a frame *after* C0_ENABLE has been
586 * cleared. So when running in S/G mode, the controller is "stopped"
587 * on receipt of the start-of-frame interrupt. That means we can
588 * safely change the DMA descriptor array here and restart things
589 * (assuming there's another buffer waiting to go).
591 static void mcam_dma_sg_done(struct mcam_camera *cam, int frame)
593 struct mcam_vb_buffer *buf = cam->vb_bufs[0];
596 * If we're no longer supposed to be streaming, don't do anything.
598 if (cam->state != S_STREAMING)
601 * If we have another buffer available, put it in and
602 * restart the engine.
604 if (!list_empty(&cam->buffers)) {
605 mcam_sg_next_buffer(cam);
606 mcam_ctlr_start(cam);
608 * Otherwise set CF_SG_RESTART and the controller will
609 * be restarted once another buffer shows up.
612 set_bit(CF_SG_RESTART, &cam->flags);
613 cam->frame_state.singles++;
614 cam->vb_bufs[0] = NULL;
617 * Now we can give the completed frame back to user space.
619 cam->frame_state.delivered++;
620 mcam_buffer_done(cam, frame, &buf->vb_buf);
625 * Scatter/gather mode requires stopping the controller between
626 * frames so we can put in a new DMA descriptor array. If no new
627 * buffer exists at frame completion, the controller is left stopped;
628 * this function is charged with gettig things going again.
630 static void mcam_sg_restart(struct mcam_camera *cam)
632 mcam_ctlr_dma_sg(cam);
633 mcam_ctlr_start(cam);
634 clear_bit(CF_SG_RESTART, &cam->flags);
637 #else /* MCAM_MODE_DMA_SG */
639 static inline void mcam_sg_restart(struct mcam_camera *cam)
644 #endif /* MCAM_MODE_DMA_SG */
646 /* ---------------------------------------------------------------------- */
648 * Buffer-mode-independent controller code.
654 static void mcam_ctlr_image(struct mcam_camera *cam)
657 struct v4l2_pix_format *fmt = &cam->pix_format;
659 imgsz = ((fmt->height << IMGSZ_V_SHIFT) & IMGSZ_V_MASK) |
660 (fmt->bytesperline & IMGSZ_H_MASK);
661 mcam_reg_write(cam, REG_IMGSIZE, imgsz);
662 mcam_reg_write(cam, REG_IMGOFFSET, 0);
663 /* YPITCH just drops the last two bits */
664 mcam_reg_write_mask(cam, REG_IMGPITCH, fmt->bytesperline,
667 * Tell the controller about the image format we are using.
669 switch (cam->pix_format.pixelformat) {
670 case V4L2_PIX_FMT_YUYV:
671 mcam_reg_write_mask(cam, REG_CTRL0,
672 C0_DF_YUV|C0_YUV_PACKED|C0_YUVE_YUYV,
676 case V4L2_PIX_FMT_RGB444:
677 mcam_reg_write_mask(cam, REG_CTRL0,
678 C0_DF_RGB|C0_RGBF_444|C0_RGB4_XRGB,
683 case V4L2_PIX_FMT_RGB565:
684 mcam_reg_write_mask(cam, REG_CTRL0,
685 C0_DF_RGB|C0_RGBF_565|C0_RGB5_BGGR,
690 cam_err(cam, "Unknown format %x\n", cam->pix_format.pixelformat);
694 * Make sure it knows we want to use hsync/vsync.
696 mcam_reg_write_mask(cam, REG_CTRL0, C0_SIF_HVSYNC,
700 * This field controls the generation of EOF(DVP only)
702 if (cam->bus_type != V4L2_MBUS_CSI2)
703 mcam_reg_set_bit(cam, REG_CTRL0,
704 C0_EOF_VSYNC | C0_VEDGE_CTRL);
709 * Configure the controller for operation; caller holds the
712 static int mcam_ctlr_configure(struct mcam_camera *cam)
716 spin_lock_irqsave(&cam->dev_lock, flags);
717 clear_bit(CF_SG_RESTART, &cam->flags);
719 mcam_ctlr_image(cam);
720 mcam_set_config_needed(cam, 0);
721 spin_unlock_irqrestore(&cam->dev_lock, flags);
725 static void mcam_ctlr_irq_enable(struct mcam_camera *cam)
728 * Clear any pending interrupts, since we do not
729 * expect to have I/O active prior to enabling.
731 mcam_reg_write(cam, REG_IRQSTAT, FRAMEIRQS);
732 mcam_reg_set_bit(cam, REG_IRQMASK, FRAMEIRQS);
735 static void mcam_ctlr_irq_disable(struct mcam_camera *cam)
737 mcam_reg_clear_bit(cam, REG_IRQMASK, FRAMEIRQS);
742 static void mcam_ctlr_init(struct mcam_camera *cam)
746 spin_lock_irqsave(&cam->dev_lock, flags);
748 * Make sure it's not powered down.
750 mcam_reg_clear_bit(cam, REG_CTRL1, C1_PWRDWN);
752 * Turn off the enable bit. It sure should be off anyway,
753 * but it's good to be sure.
755 mcam_reg_clear_bit(cam, REG_CTRL0, C0_ENABLE);
757 * Clock the sensor appropriately. Controller clock should
758 * be 48MHz, sensor "typical" value is half that.
760 mcam_reg_write_mask(cam, REG_CLKCTRL, 2, CLK_DIV_MASK);
761 spin_unlock_irqrestore(&cam->dev_lock, flags);
766 * Stop the controller, and don't return until we're really sure that no
767 * further DMA is going on.
769 static void mcam_ctlr_stop_dma(struct mcam_camera *cam)
774 * Theory: stop the camera controller (whether it is operating
775 * or not). Delay briefly just in case we race with the SOF
776 * interrupt, then wait until no DMA is active.
778 spin_lock_irqsave(&cam->dev_lock, flags);
779 clear_bit(CF_SG_RESTART, &cam->flags);
782 spin_unlock_irqrestore(&cam->dev_lock, flags);
784 * This is a brutally long sleep, but experience shows that
785 * it can take the controller a while to get the message that
786 * it needs to stop grabbing frames. In particular, we can
787 * sometimes (on mmp) get a frame at the end WITHOUT the
788 * start-of-frame indication.
791 if (test_bit(CF_DMA_ACTIVE, &cam->flags))
792 cam_err(cam, "Timeout waiting for DMA to end\n");
793 /* This would be bad news - what now? */
794 spin_lock_irqsave(&cam->dev_lock, flags);
795 mcam_ctlr_irq_disable(cam);
796 spin_unlock_irqrestore(&cam->dev_lock, flags);
802 static int mcam_ctlr_power_up(struct mcam_camera *cam)
807 spin_lock_irqsave(&cam->dev_lock, flags);
808 ret = cam->plat_power_up(cam);
810 spin_unlock_irqrestore(&cam->dev_lock, flags);
813 mcam_reg_clear_bit(cam, REG_CTRL1, C1_PWRDWN);
814 spin_unlock_irqrestore(&cam->dev_lock, flags);
815 msleep(5); /* Just to be sure */
819 static void mcam_ctlr_power_down(struct mcam_camera *cam)
823 spin_lock_irqsave(&cam->dev_lock, flags);
825 * School of hard knocks department: be sure we do any register
826 * twiddling on the controller *before* calling the platform
827 * power down routine.
829 mcam_reg_set_bit(cam, REG_CTRL1, C1_PWRDWN);
830 cam->plat_power_down(cam);
831 spin_unlock_irqrestore(&cam->dev_lock, flags);
834 /* -------------------------------------------------------------------- */
836 * Communications with the sensor.
839 static int __mcam_cam_reset(struct mcam_camera *cam)
841 return sensor_call(cam, core, reset, 0);
845 * We have found the sensor on the i2c. Let's try to have a
848 static int mcam_cam_init(struct mcam_camera *cam)
852 mutex_lock(&cam->s_mutex);
853 if (cam->state != S_NOTREADY)
854 cam_warn(cam, "Cam init with device in funky state %d",
856 ret = __mcam_cam_reset(cam);
857 /* Get/set parameters? */
859 mcam_ctlr_power_down(cam);
860 mutex_unlock(&cam->s_mutex);
865 * Configure the sensor to match the parameters we have. Caller should
868 static int mcam_cam_set_flip(struct mcam_camera *cam)
870 struct v4l2_control ctrl;
872 memset(&ctrl, 0, sizeof(ctrl));
873 ctrl.id = V4L2_CID_VFLIP;
875 return sensor_call(cam, core, s_ctrl, &ctrl);
879 static int mcam_cam_configure(struct mcam_camera *cam)
881 struct v4l2_mbus_framefmt mbus_fmt;
884 v4l2_fill_mbus_format(&mbus_fmt, &cam->pix_format, cam->mbus_code);
885 ret = sensor_call(cam, core, init, 0);
887 ret = sensor_call(cam, video, s_mbus_fmt, &mbus_fmt);
889 * OV7670 does weird things if flip is set *before* format...
891 ret += mcam_cam_set_flip(cam);
896 * Get everything ready, and start grabbing frames.
898 static int mcam_read_setup(struct mcam_camera *cam)
904 * Configuration. If we still don't have DMA buffers,
905 * make one last, desperate attempt.
907 if (cam->buffer_mode == B_vmalloc && cam->nbufs == 0 &&
908 mcam_alloc_dma_bufs(cam, 0))
911 if (mcam_needs_config(cam)) {
912 mcam_cam_configure(cam);
913 ret = mcam_ctlr_configure(cam);
921 spin_lock_irqsave(&cam->dev_lock, flags);
922 clear_bit(CF_DMA_ACTIVE, &cam->flags);
923 mcam_reset_buffers(cam);
925 * Update CSI2_DPHY value
929 cam_dbg(cam, "camera: DPHY sets: dphy3=0x%x, dphy5=0x%x, dphy6=0x%x\n",
930 cam->dphy[0], cam->dphy[1], cam->dphy[2]);
931 if (cam->bus_type == V4L2_MBUS_CSI2)
932 mcam_enable_mipi(cam);
934 mcam_disable_mipi(cam);
935 mcam_ctlr_irq_enable(cam);
936 cam->state = S_STREAMING;
937 if (!test_bit(CF_SG_RESTART, &cam->flags))
938 mcam_ctlr_start(cam);
939 spin_unlock_irqrestore(&cam->dev_lock, flags);
943 /* ----------------------------------------------------------------------- */
945 * Videobuf2 interface code.
948 static int mcam_vb_queue_setup(struct vb2_queue *vq,
949 const struct v4l2_format *fmt, unsigned int *nbufs,
950 unsigned int *num_planes, unsigned int sizes[],
953 struct mcam_camera *cam = vb2_get_drv_priv(vq);
954 int minbufs = (cam->buffer_mode == B_DMA_contig) ? 3 : 2;
956 sizes[0] = cam->pix_format.sizeimage;
957 *num_planes = 1; /* Someday we have to support planar formats... */
958 if (*nbufs < minbufs)
960 if (cam->buffer_mode == B_DMA_contig)
961 alloc_ctxs[0] = cam->vb_alloc_ctx;
966 static void mcam_vb_buf_queue(struct vb2_buffer *vb)
968 struct mcam_vb_buffer *mvb = vb_to_mvb(vb);
969 struct mcam_camera *cam = vb2_get_drv_priv(vb->vb2_queue);
973 spin_lock_irqsave(&cam->dev_lock, flags);
974 start = (cam->state == S_BUFWAIT) && !list_empty(&cam->buffers);
975 list_add(&mvb->queue, &cam->buffers);
976 if (cam->state == S_STREAMING && test_bit(CF_SG_RESTART, &cam->flags))
977 mcam_sg_restart(cam);
978 spin_unlock_irqrestore(&cam->dev_lock, flags);
980 mcam_read_setup(cam);
985 * vb2 uses these to release the mutex when waiting in dqbuf. I'm
986 * not actually sure we need to do this (I'm not sure that vb2_dqbuf() needs
987 * to be called with the mutex held), but better safe than sorry.
989 static void mcam_vb_wait_prepare(struct vb2_queue *vq)
991 struct mcam_camera *cam = vb2_get_drv_priv(vq);
993 mutex_unlock(&cam->s_mutex);
996 static void mcam_vb_wait_finish(struct vb2_queue *vq)
998 struct mcam_camera *cam = vb2_get_drv_priv(vq);
1000 mutex_lock(&cam->s_mutex);
1004 * These need to be called with the mutex held from vb2
1006 static int mcam_vb_start_streaming(struct vb2_queue *vq, unsigned int count)
1008 struct mcam_camera *cam = vb2_get_drv_priv(vq);
1010 if (cam->state != S_IDLE) {
1011 INIT_LIST_HEAD(&cam->buffers);
1016 * Videobuf2 sneakily hoards all the buffers and won't
1017 * give them to us until *after* streaming starts. But
1018 * we can't actually start streaming until we have a
1019 * destination. So go into a wait state and hope they
1020 * give us buffers soon.
1022 if (cam->buffer_mode != B_vmalloc && list_empty(&cam->buffers)) {
1023 cam->state = S_BUFWAIT;
1026 return mcam_read_setup(cam);
1029 static int mcam_vb_stop_streaming(struct vb2_queue *vq)
1031 struct mcam_camera *cam = vb2_get_drv_priv(vq);
1032 unsigned long flags;
1034 if (cam->state == S_BUFWAIT) {
1035 /* They never gave us buffers */
1036 cam->state = S_IDLE;
1039 if (cam->state != S_STREAMING)
1041 mcam_ctlr_stop_dma(cam);
1043 * Reset the CCIC PHY after stopping streaming,
1044 * otherwise, the CCIC may be unstable.
1046 if (cam->ctlr_reset)
1047 cam->ctlr_reset(cam);
1049 * VB2 reclaims the buffers, so we need to forget
1052 spin_lock_irqsave(&cam->dev_lock, flags);
1053 INIT_LIST_HEAD(&cam->buffers);
1054 spin_unlock_irqrestore(&cam->dev_lock, flags);
1059 static const struct vb2_ops mcam_vb2_ops = {
1060 .queue_setup = mcam_vb_queue_setup,
1061 .buf_queue = mcam_vb_buf_queue,
1062 .start_streaming = mcam_vb_start_streaming,
1063 .stop_streaming = mcam_vb_stop_streaming,
1064 .wait_prepare = mcam_vb_wait_prepare,
1065 .wait_finish = mcam_vb_wait_finish,
1069 #ifdef MCAM_MODE_DMA_SG
1071 * Scatter/gather mode uses all of the above functions plus a
1072 * few extras to deal with DMA mapping.
1074 static int mcam_vb_sg_buf_init(struct vb2_buffer *vb)
1076 struct mcam_vb_buffer *mvb = vb_to_mvb(vb);
1077 struct mcam_camera *cam = vb2_get_drv_priv(vb->vb2_queue);
1078 int ndesc = cam->pix_format.sizeimage/PAGE_SIZE + 1;
1080 mvb->dma_desc = dma_alloc_coherent(cam->dev,
1081 ndesc * sizeof(struct mcam_dma_desc),
1082 &mvb->dma_desc_pa, GFP_KERNEL);
1083 if (mvb->dma_desc == NULL) {
1084 cam_err(cam, "Unable to get DMA descriptor array\n");
1090 static int mcam_vb_sg_buf_prepare(struct vb2_buffer *vb)
1092 struct mcam_vb_buffer *mvb = vb_to_mvb(vb);
1093 struct mcam_camera *cam = vb2_get_drv_priv(vb->vb2_queue);
1094 struct vb2_dma_sg_desc *sgd = vb2_dma_sg_plane_desc(vb, 0);
1095 struct mcam_dma_desc *desc = mvb->dma_desc;
1096 struct scatterlist *sg;
1099 mvb->dma_desc_nent = dma_map_sg(cam->dev, sgd->sglist, sgd->num_pages,
1101 if (mvb->dma_desc_nent <= 0)
1102 return -EIO; /* Not sure what's right here */
1103 for_each_sg(sgd->sglist, sg, mvb->dma_desc_nent, i) {
1104 desc->dma_addr = sg_dma_address(sg);
1105 desc->segment_len = sg_dma_len(sg);
1111 static int mcam_vb_sg_buf_finish(struct vb2_buffer *vb)
1113 struct mcam_camera *cam = vb2_get_drv_priv(vb->vb2_queue);
1114 struct vb2_dma_sg_desc *sgd = vb2_dma_sg_plane_desc(vb, 0);
1116 dma_unmap_sg(cam->dev, sgd->sglist, sgd->num_pages, DMA_FROM_DEVICE);
1120 static void mcam_vb_sg_buf_cleanup(struct vb2_buffer *vb)
1122 struct mcam_camera *cam = vb2_get_drv_priv(vb->vb2_queue);
1123 struct mcam_vb_buffer *mvb = vb_to_mvb(vb);
1124 int ndesc = cam->pix_format.sizeimage/PAGE_SIZE + 1;
1126 dma_free_coherent(cam->dev, ndesc * sizeof(struct mcam_dma_desc),
1127 mvb->dma_desc, mvb->dma_desc_pa);
1131 static const struct vb2_ops mcam_vb2_sg_ops = {
1132 .queue_setup = mcam_vb_queue_setup,
1133 .buf_init = mcam_vb_sg_buf_init,
1134 .buf_prepare = mcam_vb_sg_buf_prepare,
1135 .buf_queue = mcam_vb_buf_queue,
1136 .buf_finish = mcam_vb_sg_buf_finish,
1137 .buf_cleanup = mcam_vb_sg_buf_cleanup,
1138 .start_streaming = mcam_vb_start_streaming,
1139 .stop_streaming = mcam_vb_stop_streaming,
1140 .wait_prepare = mcam_vb_wait_prepare,
1141 .wait_finish = mcam_vb_wait_finish,
1144 #endif /* MCAM_MODE_DMA_SG */
1146 static int mcam_setup_vb2(struct mcam_camera *cam)
1148 struct vb2_queue *vq = &cam->vb_queue;
1150 memset(vq, 0, sizeof(*vq));
1151 vq->type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
1153 INIT_LIST_HEAD(&cam->buffers);
1154 switch (cam->buffer_mode) {
1156 #ifdef MCAM_MODE_DMA_CONTIG
1157 vq->ops = &mcam_vb2_ops;
1158 vq->mem_ops = &vb2_dma_contig_memops;
1159 cam->vb_alloc_ctx = vb2_dma_contig_init_ctx(cam->dev);
1160 vq->io_modes = VB2_MMAP | VB2_USERPTR;
1161 cam->dma_setup = mcam_ctlr_dma_contig;
1162 cam->frame_complete = mcam_dma_contig_done;
1166 #ifdef MCAM_MODE_DMA_SG
1167 vq->ops = &mcam_vb2_sg_ops;
1168 vq->mem_ops = &vb2_dma_sg_memops;
1169 vq->io_modes = VB2_MMAP | VB2_USERPTR;
1170 cam->dma_setup = mcam_ctlr_dma_sg;
1171 cam->frame_complete = mcam_dma_sg_done;
1175 #ifdef MCAM_MODE_VMALLOC
1176 tasklet_init(&cam->s_tasklet, mcam_frame_tasklet,
1177 (unsigned long) cam);
1178 vq->ops = &mcam_vb2_ops;
1179 vq->mem_ops = &vb2_vmalloc_memops;
1180 vq->buf_struct_size = sizeof(struct mcam_vb_buffer);
1181 vq->io_modes = VB2_MMAP;
1182 cam->dma_setup = mcam_ctlr_dma_vmalloc;
1183 cam->frame_complete = mcam_vmalloc_done;
1187 return vb2_queue_init(vq);
1190 static void mcam_cleanup_vb2(struct mcam_camera *cam)
1192 vb2_queue_release(&cam->vb_queue);
1193 #ifdef MCAM_MODE_DMA_CONTIG
1194 if (cam->buffer_mode == B_DMA_contig)
1195 vb2_dma_contig_cleanup_ctx(cam->vb_alloc_ctx);
1200 /* ---------------------------------------------------------------------- */
1202 * The long list of V4L2 ioctl() operations.
1205 static int mcam_vidioc_streamon(struct file *filp, void *priv,
1206 enum v4l2_buf_type type)
1208 struct mcam_camera *cam = filp->private_data;
1211 mutex_lock(&cam->s_mutex);
1212 ret = vb2_streamon(&cam->vb_queue, type);
1213 mutex_unlock(&cam->s_mutex);
1218 static int mcam_vidioc_streamoff(struct file *filp, void *priv,
1219 enum v4l2_buf_type type)
1221 struct mcam_camera *cam = filp->private_data;
1224 mutex_lock(&cam->s_mutex);
1225 ret = vb2_streamoff(&cam->vb_queue, type);
1226 mutex_unlock(&cam->s_mutex);
1231 static int mcam_vidioc_reqbufs(struct file *filp, void *priv,
1232 struct v4l2_requestbuffers *req)
1234 struct mcam_camera *cam = filp->private_data;
1237 mutex_lock(&cam->s_mutex);
1238 ret = vb2_reqbufs(&cam->vb_queue, req);
1239 mutex_unlock(&cam->s_mutex);
1244 static int mcam_vidioc_querybuf(struct file *filp, void *priv,
1245 struct v4l2_buffer *buf)
1247 struct mcam_camera *cam = filp->private_data;
1250 mutex_lock(&cam->s_mutex);
1251 ret = vb2_querybuf(&cam->vb_queue, buf);
1252 mutex_unlock(&cam->s_mutex);
1256 static int mcam_vidioc_qbuf(struct file *filp, void *priv,
1257 struct v4l2_buffer *buf)
1259 struct mcam_camera *cam = filp->private_data;
1262 mutex_lock(&cam->s_mutex);
1263 ret = vb2_qbuf(&cam->vb_queue, buf);
1264 mutex_unlock(&cam->s_mutex);
1268 static int mcam_vidioc_dqbuf(struct file *filp, void *priv,
1269 struct v4l2_buffer *buf)
1271 struct mcam_camera *cam = filp->private_data;
1274 mutex_lock(&cam->s_mutex);
1275 ret = vb2_dqbuf(&cam->vb_queue, buf, filp->f_flags & O_NONBLOCK);
1276 mutex_unlock(&cam->s_mutex);
1280 static int mcam_vidioc_querycap(struct file *file, void *priv,
1281 struct v4l2_capability *cap)
1283 strcpy(cap->driver, "marvell_ccic");
1284 strcpy(cap->card, "marvell_ccic");
1286 cap->capabilities = V4L2_CAP_VIDEO_CAPTURE |
1287 V4L2_CAP_READWRITE | V4L2_CAP_STREAMING;
1292 static int mcam_vidioc_enum_fmt_vid_cap(struct file *filp,
1293 void *priv, struct v4l2_fmtdesc *fmt)
1295 if (fmt->index >= N_MCAM_FMTS)
1297 strlcpy(fmt->description, mcam_formats[fmt->index].desc,
1298 sizeof(fmt->description));
1299 fmt->pixelformat = mcam_formats[fmt->index].pixelformat;
1303 static int mcam_vidioc_try_fmt_vid_cap(struct file *filp, void *priv,
1304 struct v4l2_format *fmt)
1306 struct mcam_camera *cam = priv;
1307 struct mcam_format_struct *f;
1308 struct v4l2_pix_format *pix = &fmt->fmt.pix;
1309 struct v4l2_mbus_framefmt mbus_fmt;
1312 f = mcam_find_format(pix->pixelformat);
1313 pix->pixelformat = f->pixelformat;
1314 v4l2_fill_mbus_format(&mbus_fmt, pix, f->mbus_code);
1315 mutex_lock(&cam->s_mutex);
1316 ret = sensor_call(cam, video, try_mbus_fmt, &mbus_fmt);
1317 mutex_unlock(&cam->s_mutex);
1318 v4l2_fill_pix_format(pix, &mbus_fmt);
1319 pix->bytesperline = pix->width * f->bpp;
1320 pix->sizeimage = pix->height * pix->bytesperline;
1324 static int mcam_vidioc_s_fmt_vid_cap(struct file *filp, void *priv,
1325 struct v4l2_format *fmt)
1327 struct mcam_camera *cam = priv;
1328 struct mcam_format_struct *f;
1332 * Can't do anything if the device is not idle
1333 * Also can't if there are streaming buffers in place.
1335 if (cam->state != S_IDLE || cam->vb_queue.num_buffers > 0)
1338 f = mcam_find_format(fmt->fmt.pix.pixelformat);
1341 * See if the formatting works in principle.
1343 ret = mcam_vidioc_try_fmt_vid_cap(filp, priv, fmt);
1347 * Now we start to change things for real, so let's do it
1350 mutex_lock(&cam->s_mutex);
1351 cam->pix_format = fmt->fmt.pix;
1352 cam->mbus_code = f->mbus_code;
1355 * Make sure we have appropriate DMA buffers.
1357 if (cam->buffer_mode == B_vmalloc) {
1358 ret = mcam_check_dma_buffers(cam);
1362 mcam_set_config_needed(cam, 1);
1364 mutex_unlock(&cam->s_mutex);
1369 * Return our stored notion of how the camera is/should be configured.
1370 * The V4l2 spec wants us to be smarter, and actually get this from
1371 * the camera (and not mess with it at open time). Someday.
1373 static int mcam_vidioc_g_fmt_vid_cap(struct file *filp, void *priv,
1374 struct v4l2_format *f)
1376 struct mcam_camera *cam = priv;
1378 f->fmt.pix = cam->pix_format;
1383 * We only have one input - the sensor - so minimize the nonsense here.
1385 static int mcam_vidioc_enum_input(struct file *filp, void *priv,
1386 struct v4l2_input *input)
1388 if (input->index != 0)
1391 input->type = V4L2_INPUT_TYPE_CAMERA;
1392 input->std = V4L2_STD_ALL; /* Not sure what should go here */
1393 strcpy(input->name, "Camera");
1397 static int mcam_vidioc_g_input(struct file *filp, void *priv, unsigned int *i)
1403 static int mcam_vidioc_s_input(struct file *filp, void *priv, unsigned int i)
1411 static int mcam_vidioc_s_std(struct file *filp, void *priv, v4l2_std_id a)
1416 static int mcam_vidioc_g_std(struct file *filp, void *priv, v4l2_std_id *a)
1418 *a = V4L2_STD_NTSC_M;
1423 * G/S_PARM. Most of this is done by the sensor, but we are
1424 * the level which controls the number of read buffers.
1426 static int mcam_vidioc_g_parm(struct file *filp, void *priv,
1427 struct v4l2_streamparm *parms)
1429 struct mcam_camera *cam = priv;
1432 mutex_lock(&cam->s_mutex);
1433 ret = sensor_call(cam, video, g_parm, parms);
1434 mutex_unlock(&cam->s_mutex);
1435 parms->parm.capture.readbuffers = n_dma_bufs;
1439 static int mcam_vidioc_s_parm(struct file *filp, void *priv,
1440 struct v4l2_streamparm *parms)
1442 struct mcam_camera *cam = priv;
1445 mutex_lock(&cam->s_mutex);
1446 ret = sensor_call(cam, video, s_parm, parms);
1447 mutex_unlock(&cam->s_mutex);
1448 parms->parm.capture.readbuffers = n_dma_bufs;
1452 static int mcam_vidioc_enum_framesizes(struct file *filp, void *priv,
1453 struct v4l2_frmsizeenum *sizes)
1455 struct mcam_camera *cam = priv;
1458 mutex_lock(&cam->s_mutex);
1459 ret = sensor_call(cam, video, enum_framesizes, sizes);
1460 mutex_unlock(&cam->s_mutex);
1464 static int mcam_vidioc_enum_frameintervals(struct file *filp, void *priv,
1465 struct v4l2_frmivalenum *interval)
1467 struct mcam_camera *cam = priv;
1470 mutex_lock(&cam->s_mutex);
1471 ret = sensor_call(cam, video, enum_frameintervals, interval);
1472 mutex_unlock(&cam->s_mutex);
1476 #ifdef CONFIG_VIDEO_ADV_DEBUG
1477 static int mcam_vidioc_g_register(struct file *file, void *priv,
1478 struct v4l2_dbg_register *reg)
1480 struct mcam_camera *cam = priv;
1482 if (reg->reg > cam->regs_size - 4)
1484 reg->val = mcam_reg_read(cam, reg->reg);
1489 static int mcam_vidioc_s_register(struct file *file, void *priv,
1490 const struct v4l2_dbg_register *reg)
1492 struct mcam_camera *cam = priv;
1494 if (reg->reg > cam->regs_size - 4)
1496 mcam_reg_write(cam, reg->reg, reg->val);
1501 static const struct v4l2_ioctl_ops mcam_v4l_ioctl_ops = {
1502 .vidioc_querycap = mcam_vidioc_querycap,
1503 .vidioc_enum_fmt_vid_cap = mcam_vidioc_enum_fmt_vid_cap,
1504 .vidioc_try_fmt_vid_cap = mcam_vidioc_try_fmt_vid_cap,
1505 .vidioc_s_fmt_vid_cap = mcam_vidioc_s_fmt_vid_cap,
1506 .vidioc_g_fmt_vid_cap = mcam_vidioc_g_fmt_vid_cap,
1507 .vidioc_enum_input = mcam_vidioc_enum_input,
1508 .vidioc_g_input = mcam_vidioc_g_input,
1509 .vidioc_s_input = mcam_vidioc_s_input,
1510 .vidioc_s_std = mcam_vidioc_s_std,
1511 .vidioc_g_std = mcam_vidioc_g_std,
1512 .vidioc_reqbufs = mcam_vidioc_reqbufs,
1513 .vidioc_querybuf = mcam_vidioc_querybuf,
1514 .vidioc_qbuf = mcam_vidioc_qbuf,
1515 .vidioc_dqbuf = mcam_vidioc_dqbuf,
1516 .vidioc_streamon = mcam_vidioc_streamon,
1517 .vidioc_streamoff = mcam_vidioc_streamoff,
1518 .vidioc_g_parm = mcam_vidioc_g_parm,
1519 .vidioc_s_parm = mcam_vidioc_s_parm,
1520 .vidioc_enum_framesizes = mcam_vidioc_enum_framesizes,
1521 .vidioc_enum_frameintervals = mcam_vidioc_enum_frameintervals,
1522 #ifdef CONFIG_VIDEO_ADV_DEBUG
1523 .vidioc_g_register = mcam_vidioc_g_register,
1524 .vidioc_s_register = mcam_vidioc_s_register,
1528 /* ---------------------------------------------------------------------- */
1530 * Our various file operations.
1532 static int mcam_v4l_open(struct file *filp)
1534 struct mcam_camera *cam = video_drvdata(filp);
1537 filp->private_data = cam;
1539 cam->frame_state.frames = 0;
1540 cam->frame_state.singles = 0;
1541 cam->frame_state.delivered = 0;
1542 mutex_lock(&cam->s_mutex);
1543 if (cam->users == 0) {
1544 ret = mcam_setup_vb2(cam);
1547 ret = mcam_ctlr_power_up(cam);
1550 __mcam_cam_reset(cam);
1551 mcam_set_config_needed(cam, 1);
1555 mutex_unlock(&cam->s_mutex);
1560 static int mcam_v4l_release(struct file *filp)
1562 struct mcam_camera *cam = filp->private_data;
1564 cam_dbg(cam, "Release, %d frames, %d singles, %d delivered\n",
1565 cam->frame_state.frames, cam->frame_state.singles,
1566 cam->frame_state.delivered);
1567 mutex_lock(&cam->s_mutex);
1569 if (cam->users == 0) {
1570 mcam_ctlr_stop_dma(cam);
1571 mcam_cleanup_vb2(cam);
1572 mcam_disable_mipi(cam);
1573 mcam_ctlr_power_down(cam);
1574 if (cam->buffer_mode == B_vmalloc && alloc_bufs_at_read)
1575 mcam_free_dma_bufs(cam);
1578 mutex_unlock(&cam->s_mutex);
1582 static ssize_t mcam_v4l_read(struct file *filp,
1583 char __user *buffer, size_t len, loff_t *pos)
1585 struct mcam_camera *cam = filp->private_data;
1588 mutex_lock(&cam->s_mutex);
1589 ret = vb2_read(&cam->vb_queue, buffer, len, pos,
1590 filp->f_flags & O_NONBLOCK);
1591 mutex_unlock(&cam->s_mutex);
1597 static unsigned int mcam_v4l_poll(struct file *filp,
1598 struct poll_table_struct *pt)
1600 struct mcam_camera *cam = filp->private_data;
1603 mutex_lock(&cam->s_mutex);
1604 ret = vb2_poll(&cam->vb_queue, filp, pt);
1605 mutex_unlock(&cam->s_mutex);
1610 static int mcam_v4l_mmap(struct file *filp, struct vm_area_struct *vma)
1612 struct mcam_camera *cam = filp->private_data;
1615 mutex_lock(&cam->s_mutex);
1616 ret = vb2_mmap(&cam->vb_queue, vma);
1617 mutex_unlock(&cam->s_mutex);
1623 static const struct v4l2_file_operations mcam_v4l_fops = {
1624 .owner = THIS_MODULE,
1625 .open = mcam_v4l_open,
1626 .release = mcam_v4l_release,
1627 .read = mcam_v4l_read,
1628 .poll = mcam_v4l_poll,
1629 .mmap = mcam_v4l_mmap,
1630 .unlocked_ioctl = video_ioctl2,
1635 * This template device holds all of those v4l2 methods; we
1636 * clone it for specific real devices.
1638 static struct video_device mcam_v4l_template = {
1640 .tvnorms = V4L2_STD_NTSC_M,
1642 .fops = &mcam_v4l_fops,
1643 .ioctl_ops = &mcam_v4l_ioctl_ops,
1644 .release = video_device_release_empty,
1647 /* ---------------------------------------------------------------------- */
1649 * Interrupt handler stuff
1651 static void mcam_frame_complete(struct mcam_camera *cam, int frame)
1654 * Basic frame housekeeping.
1656 set_bit(frame, &cam->flags);
1657 clear_bit(CF_DMA_ACTIVE, &cam->flags);
1658 cam->next_buf = frame;
1659 cam->buf_seq[frame] = ++(cam->sequence);
1660 cam->frame_state.frames++;
1662 * "This should never happen"
1664 if (cam->state != S_STREAMING)
1667 * Process the frame and set up the next one.
1669 cam->frame_complete(cam, frame);
1674 * The interrupt handler; this needs to be called from the
1675 * platform irq handler with the lock held.
1677 int mccic_irq(struct mcam_camera *cam, unsigned int irqs)
1679 unsigned int frame, handled = 0;
1681 mcam_reg_write(cam, REG_IRQSTAT, FRAMEIRQS); /* Clear'em all */
1683 * Handle any frame completions. There really should
1684 * not be more than one of these, or we have fallen
1687 * When running in S/G mode, the frame number lacks any
1688 * real meaning - there's only one descriptor array - but
1689 * the controller still picks a different one to signal
1692 for (frame = 0; frame < cam->nbufs; frame++)
1693 if (irqs & (IRQ_EOF0 << frame)) {
1694 mcam_frame_complete(cam, frame);
1696 if (cam->buffer_mode == B_DMA_sg)
1700 * If a frame starts, note that we have DMA active. This
1701 * code assumes that we won't get multiple frame interrupts
1702 * at once; may want to rethink that.
1704 if (irqs & (IRQ_SOF0 | IRQ_SOF1 | IRQ_SOF2)) {
1705 set_bit(CF_DMA_ACTIVE, &cam->flags);
1707 if (cam->buffer_mode == B_DMA_sg)
1708 mcam_ctlr_stop(cam);
1713 /* ---------------------------------------------------------------------- */
1715 * Registration and such.
1717 static struct ov7670_config sensor_cfg = {
1719 * Exclude QCIF mode, because it only captures a tiny portion
1727 int mccic_register(struct mcam_camera *cam)
1729 struct i2c_board_info ov7670_info = {
1732 .platform_data = &sensor_cfg,
1737 * Validate the requested buffer mode.
1739 if (buffer_mode >= 0)
1740 cam->buffer_mode = buffer_mode;
1741 if (cam->buffer_mode == B_DMA_sg &&
1742 cam->chip_id == MCAM_CAFE) {
1743 printk(KERN_ERR "marvell-cam: Cafe can't do S/G I/O, "
1744 "attempting vmalloc mode instead\n");
1745 cam->buffer_mode = B_vmalloc;
1747 if (!mcam_buffer_mode_supported(cam->buffer_mode)) {
1748 printk(KERN_ERR "marvell-cam: buffer mode %d unsupported\n",
1755 ret = v4l2_device_register(cam->dev, &cam->v4l2_dev);
1759 mutex_init(&cam->s_mutex);
1760 cam->state = S_NOTREADY;
1761 mcam_set_config_needed(cam, 1);
1762 cam->pix_format = mcam_def_pix_format;
1763 cam->mbus_code = mcam_def_mbus_code;
1764 INIT_LIST_HEAD(&cam->buffers);
1765 mcam_ctlr_init(cam);
1768 * Try to find the sensor.
1770 sensor_cfg.clock_speed = cam->clock_speed;
1771 sensor_cfg.use_smbus = cam->use_smbus;
1772 cam->sensor_addr = ov7670_info.addr;
1773 cam->sensor = v4l2_i2c_new_subdev_board(&cam->v4l2_dev,
1774 cam->i2c_adapter, &ov7670_info, NULL);
1775 if (cam->sensor == NULL) {
1777 goto out_unregister;
1780 ret = mcam_cam_init(cam);
1782 goto out_unregister;
1784 * Get the v4l2 setup done.
1786 ret = v4l2_ctrl_handler_init(&cam->ctrl_handler, 10);
1788 goto out_unregister;
1789 cam->v4l2_dev.ctrl_handler = &cam->ctrl_handler;
1791 mutex_lock(&cam->s_mutex);
1792 cam->vdev = mcam_v4l_template;
1793 cam->vdev.debug = 0;
1794 cam->vdev.v4l2_dev = &cam->v4l2_dev;
1795 video_set_drvdata(&cam->vdev, cam);
1796 ret = video_register_device(&cam->vdev, VFL_TYPE_GRABBER, -1);
1801 * If so requested, try to get our DMA buffers now.
1803 if (cam->buffer_mode == B_vmalloc && !alloc_bufs_at_read) {
1804 if (mcam_alloc_dma_bufs(cam, 1))
1805 cam_warn(cam, "Unable to alloc DMA buffers at load"
1806 " will try again later.");
1810 v4l2_ctrl_handler_free(&cam->ctrl_handler);
1811 mutex_unlock(&cam->s_mutex);
1814 v4l2_device_unregister(&cam->v4l2_dev);
1819 void mccic_shutdown(struct mcam_camera *cam)
1822 * If we have no users (and we really, really should have no
1823 * users) the device will already be powered down. Trying to
1824 * take it down again will wedge the machine, which is frowned
1827 if (cam->users > 0) {
1828 cam_warn(cam, "Removing a device with users!\n");
1829 mcam_ctlr_power_down(cam);
1831 vb2_queue_release(&cam->vb_queue);
1832 if (cam->buffer_mode == B_vmalloc)
1833 mcam_free_dma_bufs(cam);
1834 video_unregister_device(&cam->vdev);
1835 v4l2_ctrl_handler_free(&cam->ctrl_handler);
1836 v4l2_device_unregister(&cam->v4l2_dev);
1844 void mccic_suspend(struct mcam_camera *cam)
1846 mutex_lock(&cam->s_mutex);
1847 if (cam->users > 0) {
1848 enum mcam_state cstate = cam->state;
1850 mcam_ctlr_stop_dma(cam);
1851 mcam_ctlr_power_down(cam);
1852 cam->state = cstate;
1854 mutex_unlock(&cam->s_mutex);
1857 int mccic_resume(struct mcam_camera *cam)
1861 mutex_lock(&cam->s_mutex);
1862 if (cam->users > 0) {
1863 ret = mcam_ctlr_power_up(cam);
1865 mutex_unlock(&cam->s_mutex);
1868 __mcam_cam_reset(cam);
1870 mcam_ctlr_power_down(cam);
1872 mutex_unlock(&cam->s_mutex);
1874 set_bit(CF_CONFIG_NEEDED, &cam->flags);
1875 if (cam->state == S_STREAMING) {
1877 * If there was a buffer in the DMA engine at suspend
1878 * time, put it back on the queue or we'll forget about it.
1880 if (cam->buffer_mode == B_DMA_sg && cam->vb_bufs[0])
1881 list_add(&cam->vb_bufs[0]->queue, &cam->buffers);
1882 ret = mcam_read_setup(cam);
1886 #endif /* CONFIG_PM */