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/v4l2-event.h>
28 #include <media/i2c/ov7670.h>
29 #include <media/videobuf2-vmalloc.h>
30 #include <media/videobuf2-dma-contig.h>
31 #include <media/videobuf2-dma-sg.h>
33 #include "mcam-core.h"
35 #ifdef MCAM_MODE_VMALLOC
37 * Internal DMA buffer management. Since the controller cannot do S/G I/O,
38 * we must have physically contiguous buffers to bring frames into.
39 * These parameters control how many buffers we use, whether we
40 * allocate them at load time (better chance of success, but nails down
41 * memory) or when somebody tries to use the camera (riskier), and,
42 * for load-time allocation, how big they should be.
44 * The controller can cycle through three buffers. We could use
45 * more by flipping pointers around, but it probably makes little
49 static bool alloc_bufs_at_read
;
50 module_param(alloc_bufs_at_read
, bool, 0444);
51 MODULE_PARM_DESC(alloc_bufs_at_read
,
52 "Non-zero value causes DMA buffers to be allocated when the video capture device is read, rather than at module load time. This saves memory, but decreases the chances of successfully getting those buffers. This parameter is only used in the vmalloc buffer mode");
54 static int n_dma_bufs
= 3;
55 module_param(n_dma_bufs
, uint
, 0644);
56 MODULE_PARM_DESC(n_dma_bufs
,
57 "The number of DMA buffers to allocate. Can be either two (saves memory, makes timing tighter) or three.");
59 static int dma_buf_size
= VGA_WIDTH
* VGA_HEIGHT
* 2; /* Worst case */
60 module_param(dma_buf_size
, uint
, 0444);
61 MODULE_PARM_DESC(dma_buf_size
,
62 "The size of the allocated DMA buffers. If actual operating parameters require larger buffers, an attempt to reallocate will be made.");
63 #else /* MCAM_MODE_VMALLOC */
64 static const bool alloc_bufs_at_read
;
65 static const int n_dma_bufs
= 3; /* Used by S/G_PARM */
66 #endif /* MCAM_MODE_VMALLOC */
69 module_param(flip
, bool, 0444);
70 MODULE_PARM_DESC(flip
,
71 "If set, the sensor will be instructed to flip the image vertically.");
73 static int buffer_mode
= -1;
74 module_param(buffer_mode
, int, 0444);
75 MODULE_PARM_DESC(buffer_mode
,
76 "Set the buffer mode to be used; default is to go with what the platform driver asks for. Set to 0 for vmalloc, 1 for DMA contiguous.");
79 * Status flags. Always manipulated with bit operations.
81 #define CF_BUF0_VALID 0 /* Buffers valid - first three */
82 #define CF_BUF1_VALID 1
83 #define CF_BUF2_VALID 2
84 #define CF_DMA_ACTIVE 3 /* A frame is incoming */
85 #define CF_CONFIG_NEEDED 4 /* Must configure hardware */
86 #define CF_SINGLE_BUFFER 5 /* Running with a single buffer */
87 #define CF_SG_RESTART 6 /* SG restart needed */
88 #define CF_FRAME_SOF0 7 /* Frame 0 started */
89 #define CF_FRAME_SOF1 8
90 #define CF_FRAME_SOF2 9
92 #define sensor_call(cam, o, f, args...) \
93 v4l2_subdev_call(cam->sensor, o, f, ##args)
95 static struct mcam_format_struct
{
98 int bpp
; /* Bytes per pixel */
103 .desc
= "YUYV 4:2:2",
104 .pixelformat
= V4L2_PIX_FMT_YUYV
,
105 .mbus_code
= MEDIA_BUS_FMT_YUYV8_2X8
,
110 .desc
= "YVYU 4:2:2",
111 .pixelformat
= V4L2_PIX_FMT_YVYU
,
112 .mbus_code
= MEDIA_BUS_FMT_YUYV8_2X8
,
117 .desc
= "YUV 4:2:0 PLANAR",
118 .pixelformat
= V4L2_PIX_FMT_YUV420
,
119 .mbus_code
= MEDIA_BUS_FMT_YUYV8_2X8
,
124 .desc
= "YVU 4:2:0 PLANAR",
125 .pixelformat
= V4L2_PIX_FMT_YVU420
,
126 .mbus_code
= MEDIA_BUS_FMT_YUYV8_2X8
,
132 .pixelformat
= V4L2_PIX_FMT_XRGB444
,
133 .mbus_code
= MEDIA_BUS_FMT_RGB444_2X8_PADHI_LE
,
139 .pixelformat
= V4L2_PIX_FMT_RGB565
,
140 .mbus_code
= MEDIA_BUS_FMT_RGB565_2X8_LE
,
145 .desc
= "Raw RGB Bayer",
146 .pixelformat
= V4L2_PIX_FMT_SBGGR8
,
147 .mbus_code
= MEDIA_BUS_FMT_SBGGR8_1X8
,
152 #define N_MCAM_FMTS ARRAY_SIZE(mcam_formats)
154 static struct mcam_format_struct
*mcam_find_format(u32 pixelformat
)
158 for (i
= 0; i
< N_MCAM_FMTS
; i
++)
159 if (mcam_formats
[i
].pixelformat
== pixelformat
)
160 return mcam_formats
+ i
;
161 /* Not found? Then return the first format. */
166 * The default format we use until somebody says otherwise.
168 static const struct v4l2_pix_format mcam_def_pix_format
= {
170 .height
= VGA_HEIGHT
,
171 .pixelformat
= V4L2_PIX_FMT_YUYV
,
172 .field
= V4L2_FIELD_NONE
,
173 .bytesperline
= VGA_WIDTH
*2,
174 .sizeimage
= VGA_WIDTH
*VGA_HEIGHT
*2,
175 .colorspace
= V4L2_COLORSPACE_SRGB
,
178 static const u32 mcam_def_mbus_code
= MEDIA_BUS_FMT_YUYV8_2X8
;
182 * The two-word DMA descriptor format used by the Armada 610 and like. There
183 * Is a three-word format as well (set C1_DESC_3WORD) where the third
184 * word is a pointer to the next descriptor, but we don't use it. Two-word
185 * descriptors have to be contiguous in memory.
187 struct mcam_dma_desc
{
193 * Our buffer type for working with videobuf2. Note that the vb2
194 * developers have decreed that struct vb2_v4l2_buffer must be at the
195 * beginning of this structure.
197 struct mcam_vb_buffer
{
198 struct vb2_v4l2_buffer vb_buf
;
199 struct list_head queue
;
200 struct mcam_dma_desc
*dma_desc
; /* Descriptor virtual address */
201 dma_addr_t dma_desc_pa
; /* Descriptor physical address */
202 int dma_desc_nent
; /* Number of mapped descriptors */
205 static inline struct mcam_vb_buffer
*vb_to_mvb(struct vb2_v4l2_buffer
*vb
)
207 return container_of(vb
, struct mcam_vb_buffer
, vb_buf
);
211 * Hand a completed buffer back to user space.
213 static void mcam_buffer_done(struct mcam_camera
*cam
, int frame
,
214 struct vb2_v4l2_buffer
*vbuf
)
216 vbuf
->vb2_buf
.planes
[0].bytesused
= cam
->pix_format
.sizeimage
;
217 vbuf
->sequence
= cam
->buf_seq
[frame
];
218 vbuf
->field
= V4L2_FIELD_NONE
;
219 vbuf
->vb2_buf
.timestamp
= ktime_get_ns();
220 vb2_set_plane_payload(&vbuf
->vb2_buf
, 0, cam
->pix_format
.sizeimage
);
221 vb2_buffer_done(&vbuf
->vb2_buf
, VB2_BUF_STATE_DONE
);
227 * Debugging and related.
229 #define cam_err(cam, fmt, arg...) \
230 dev_err((cam)->dev, fmt, ##arg);
231 #define cam_warn(cam, fmt, arg...) \
232 dev_warn((cam)->dev, fmt, ##arg);
233 #define cam_dbg(cam, fmt, arg...) \
234 dev_dbg((cam)->dev, fmt, ##arg);
238 * Flag manipulation helpers
240 static void mcam_reset_buffers(struct mcam_camera
*cam
)
245 for (i
= 0; i
< cam
->nbufs
; i
++) {
246 clear_bit(i
, &cam
->flags
);
247 clear_bit(CF_FRAME_SOF0
+ i
, &cam
->flags
);
251 static inline int mcam_needs_config(struct mcam_camera
*cam
)
253 return test_bit(CF_CONFIG_NEEDED
, &cam
->flags
);
256 static void mcam_set_config_needed(struct mcam_camera
*cam
, int needed
)
259 set_bit(CF_CONFIG_NEEDED
, &cam
->flags
);
261 clear_bit(CF_CONFIG_NEEDED
, &cam
->flags
);
264 /* ------------------------------------------------------------------- */
266 * Make the controller start grabbing images. Everything must
267 * be set up before doing this.
269 static void mcam_ctlr_start(struct mcam_camera
*cam
)
271 /* set_bit performs a read, so no other barrier should be
273 mcam_reg_set_bit(cam
, REG_CTRL0
, C0_ENABLE
);
276 static void mcam_ctlr_stop(struct mcam_camera
*cam
)
278 mcam_reg_clear_bit(cam
, REG_CTRL0
, C0_ENABLE
);
281 static void mcam_enable_mipi(struct mcam_camera
*mcam
)
283 /* Using MIPI mode and enable MIPI */
284 cam_dbg(mcam
, "camera: DPHY3=0x%x, DPHY5=0x%x, DPHY6=0x%x\n",
285 mcam
->dphy
[0], mcam
->dphy
[1], mcam
->dphy
[2]);
286 mcam_reg_write(mcam
, REG_CSI2_DPHY3
, mcam
->dphy
[0]);
287 mcam_reg_write(mcam
, REG_CSI2_DPHY5
, mcam
->dphy
[1]);
288 mcam_reg_write(mcam
, REG_CSI2_DPHY6
, mcam
->dphy
[2]);
290 if (!mcam
->mipi_enabled
) {
291 if (mcam
->lane
> 4 || mcam
->lane
<= 0) {
292 cam_warn(mcam
, "lane number error\n");
293 mcam
->lane
= 1; /* set the default value */
296 * 0x41 actives 1 lane
297 * 0x43 actives 2 lanes
298 * 0x45 actives 3 lanes (never happen)
299 * 0x47 actives 4 lanes
301 mcam_reg_write(mcam
, REG_CSI2_CTRL0
,
302 CSI2_C0_MIPI_EN
| CSI2_C0_ACT_LANE(mcam
->lane
));
303 mcam_reg_write(mcam
, REG_CLKCTRL
,
304 (mcam
->mclk_src
<< 29) | mcam
->mclk_div
);
306 mcam
->mipi_enabled
= true;
310 static void mcam_disable_mipi(struct mcam_camera
*mcam
)
312 /* Using Parallel mode or disable MIPI */
313 mcam_reg_write(mcam
, REG_CSI2_CTRL0
, 0x0);
314 mcam_reg_write(mcam
, REG_CSI2_DPHY3
, 0x0);
315 mcam_reg_write(mcam
, REG_CSI2_DPHY5
, 0x0);
316 mcam_reg_write(mcam
, REG_CSI2_DPHY6
, 0x0);
317 mcam
->mipi_enabled
= false;
320 static bool mcam_fmt_is_planar(__u32 pfmt
)
322 struct mcam_format_struct
*f
;
324 f
= mcam_find_format(pfmt
);
328 static void mcam_write_yuv_bases(struct mcam_camera
*cam
,
329 unsigned frame
, dma_addr_t base
)
331 struct v4l2_pix_format
*fmt
= &cam
->pix_format
;
332 u32 pixel_count
= fmt
->width
* fmt
->height
;
333 dma_addr_t y
, u
= 0, v
= 0;
337 switch (fmt
->pixelformat
) {
338 case V4L2_PIX_FMT_YUV420
:
340 v
= u
+ pixel_count
/ 4;
342 case V4L2_PIX_FMT_YVU420
:
344 u
= v
+ pixel_count
/ 4;
350 mcam_reg_write(cam
, REG_Y0BAR
+ frame
* 4, y
);
351 if (mcam_fmt_is_planar(fmt
->pixelformat
)) {
352 mcam_reg_write(cam
, REG_U0BAR
+ frame
* 4, u
);
353 mcam_reg_write(cam
, REG_V0BAR
+ frame
* 4, v
);
357 /* ------------------------------------------------------------------- */
359 #ifdef MCAM_MODE_VMALLOC
361 * Code specific to the vmalloc buffer mode.
365 * Allocate in-kernel DMA buffers for vmalloc mode.
367 static int mcam_alloc_dma_bufs(struct mcam_camera
*cam
, int loadtime
)
371 mcam_set_config_needed(cam
, 1);
373 cam
->dma_buf_size
= dma_buf_size
;
375 cam
->dma_buf_size
= cam
->pix_format
.sizeimage
;
380 for (i
= 0; i
< n_dma_bufs
; i
++) {
381 cam
->dma_bufs
[i
] = dma_alloc_coherent(cam
->dev
,
382 cam
->dma_buf_size
, cam
->dma_handles
+ i
,
384 if (cam
->dma_bufs
[i
] == NULL
) {
385 cam_warn(cam
, "Failed to allocate DMA buffer\n");
391 switch (cam
->nbufs
) {
393 dma_free_coherent(cam
->dev
, cam
->dma_buf_size
,
394 cam
->dma_bufs
[0], cam
->dma_handles
[0]);
397 cam_err(cam
, "Insufficient DMA buffers, cannot operate\n");
402 cam_warn(cam
, "Will limp along with only 2 buffers\n");
408 static void mcam_free_dma_bufs(struct mcam_camera
*cam
)
412 for (i
= 0; i
< cam
->nbufs
; i
++) {
413 dma_free_coherent(cam
->dev
, cam
->dma_buf_size
,
414 cam
->dma_bufs
[i
], cam
->dma_handles
[i
]);
415 cam
->dma_bufs
[i
] = NULL
;
422 * Set up DMA buffers when operating in vmalloc mode
424 static void mcam_ctlr_dma_vmalloc(struct mcam_camera
*cam
)
427 * Store the first two YUV buffers. Then either
428 * set the third if it exists, or tell the controller
431 mcam_write_yuv_bases(cam
, 0, cam
->dma_handles
[0]);
432 mcam_write_yuv_bases(cam
, 1, cam
->dma_handles
[1]);
433 if (cam
->nbufs
> 2) {
434 mcam_write_yuv_bases(cam
, 2, cam
->dma_handles
[2]);
435 mcam_reg_clear_bit(cam
, REG_CTRL1
, C1_TWOBUFS
);
437 mcam_reg_set_bit(cam
, REG_CTRL1
, C1_TWOBUFS
);
438 if (cam
->chip_id
== MCAM_CAFE
)
439 mcam_reg_write(cam
, REG_UBAR
, 0); /* 32 bits only */
443 * Copy data out to user space in the vmalloc case
445 static void mcam_frame_tasklet(unsigned long data
)
447 struct mcam_camera
*cam
= (struct mcam_camera
*) data
;
450 struct mcam_vb_buffer
*buf
;
452 spin_lock_irqsave(&cam
->dev_lock
, flags
);
453 for (i
= 0; i
< cam
->nbufs
; i
++) {
454 int bufno
= cam
->next_buf
;
456 if (cam
->state
!= S_STREAMING
|| bufno
< 0)
457 break; /* I/O got stopped */
458 if (++(cam
->next_buf
) >= cam
->nbufs
)
460 if (!test_bit(bufno
, &cam
->flags
))
462 if (list_empty(&cam
->buffers
)) {
463 cam
->frame_state
.singles
++;
464 break; /* Leave it valid, hope for better later */
466 cam
->frame_state
.delivered
++;
467 clear_bit(bufno
, &cam
->flags
);
468 buf
= list_first_entry(&cam
->buffers
, struct mcam_vb_buffer
,
470 list_del_init(&buf
->queue
);
472 * Drop the lock during the big copy. This *should* be safe...
474 spin_unlock_irqrestore(&cam
->dev_lock
, flags
);
475 memcpy(vb2_plane_vaddr(&buf
->vb_buf
.vb2_buf
, 0),
476 cam
->dma_bufs
[bufno
],
477 cam
->pix_format
.sizeimage
);
478 mcam_buffer_done(cam
, bufno
, &buf
->vb_buf
);
479 spin_lock_irqsave(&cam
->dev_lock
, flags
);
481 spin_unlock_irqrestore(&cam
->dev_lock
, flags
);
486 * Make sure our allocated buffers are up to the task.
488 static int mcam_check_dma_buffers(struct mcam_camera
*cam
)
490 if (cam
->nbufs
> 0 && cam
->dma_buf_size
< cam
->pix_format
.sizeimage
)
491 mcam_free_dma_bufs(cam
);
493 return mcam_alloc_dma_bufs(cam
, 0);
497 static void mcam_vmalloc_done(struct mcam_camera
*cam
, int frame
)
499 tasklet_schedule(&cam
->s_tasklet
);
502 #else /* MCAM_MODE_VMALLOC */
504 static inline int mcam_alloc_dma_bufs(struct mcam_camera
*cam
, int loadtime
)
509 static inline void mcam_free_dma_bufs(struct mcam_camera
*cam
)
514 static inline int mcam_check_dma_buffers(struct mcam_camera
*cam
)
521 #endif /* MCAM_MODE_VMALLOC */
524 #ifdef MCAM_MODE_DMA_CONTIG
525 /* ---------------------------------------------------------------------- */
527 * DMA-contiguous code.
531 * Set up a contiguous buffer for the given frame. Here also is where
532 * the underrun strategy is set: if there is no buffer available, reuse
533 * the buffer from the other BAR and set the CF_SINGLE_BUFFER flag to
534 * keep the interrupt handler from giving that buffer back to user
535 * space. In this way, we always have a buffer to DMA to and don't
536 * have to try to play games stopping and restarting the controller.
538 static void mcam_set_contig_buffer(struct mcam_camera
*cam
, int frame
)
540 struct mcam_vb_buffer
*buf
;
541 dma_addr_t dma_handle
;
542 struct vb2_v4l2_buffer
*vb
;
545 * If there are no available buffers, go into single mode
547 if (list_empty(&cam
->buffers
)) {
548 buf
= cam
->vb_bufs
[frame
^ 0x1];
549 set_bit(CF_SINGLE_BUFFER
, &cam
->flags
);
550 cam
->frame_state
.singles
++;
553 * OK, we have a buffer we can use.
555 buf
= list_first_entry(&cam
->buffers
, struct mcam_vb_buffer
,
557 list_del_init(&buf
->queue
);
558 clear_bit(CF_SINGLE_BUFFER
, &cam
->flags
);
561 cam
->vb_bufs
[frame
] = buf
;
564 dma_handle
= vb2_dma_contig_plane_dma_addr(&vb
->vb2_buf
, 0);
565 mcam_write_yuv_bases(cam
, frame
, dma_handle
);
569 * Initial B_DMA_contig setup.
571 static void mcam_ctlr_dma_contig(struct mcam_camera
*cam
)
573 mcam_reg_set_bit(cam
, REG_CTRL1
, C1_TWOBUFS
);
575 mcam_set_contig_buffer(cam
, 0);
576 mcam_set_contig_buffer(cam
, 1);
580 * Frame completion handling.
582 static void mcam_dma_contig_done(struct mcam_camera
*cam
, int frame
)
584 struct mcam_vb_buffer
*buf
= cam
->vb_bufs
[frame
];
586 if (!test_bit(CF_SINGLE_BUFFER
, &cam
->flags
)) {
587 cam
->frame_state
.delivered
++;
588 cam
->vb_bufs
[frame
] = NULL
;
589 mcam_buffer_done(cam
, frame
, &buf
->vb_buf
);
591 mcam_set_contig_buffer(cam
, frame
);
594 #endif /* MCAM_MODE_DMA_CONTIG */
596 #ifdef MCAM_MODE_DMA_SG
597 /* ---------------------------------------------------------------------- */
599 * Scatter/gather-specific code.
603 * Set up the next buffer for S/G I/O; caller should be sure that
604 * the controller is stopped and a buffer is available.
606 static void mcam_sg_next_buffer(struct mcam_camera
*cam
)
608 struct mcam_vb_buffer
*buf
;
610 buf
= list_first_entry(&cam
->buffers
, struct mcam_vb_buffer
, queue
);
611 list_del_init(&buf
->queue
);
613 * Very Bad Not Good Things happen if you don't clear
614 * C1_DESC_ENA before making any descriptor changes.
616 mcam_reg_clear_bit(cam
, REG_CTRL1
, C1_DESC_ENA
);
617 mcam_reg_write(cam
, REG_DMA_DESC_Y
, buf
->dma_desc_pa
);
618 mcam_reg_write(cam
, REG_DESC_LEN_Y
,
619 buf
->dma_desc_nent
*sizeof(struct mcam_dma_desc
));
620 mcam_reg_write(cam
, REG_DESC_LEN_U
, 0);
621 mcam_reg_write(cam
, REG_DESC_LEN_V
, 0);
622 mcam_reg_set_bit(cam
, REG_CTRL1
, C1_DESC_ENA
);
623 cam
->vb_bufs
[0] = buf
;
627 * Initial B_DMA_sg setup
629 static void mcam_ctlr_dma_sg(struct mcam_camera
*cam
)
632 * The list-empty condition can hit us at resume time
633 * if the buffer list was empty when the system was suspended.
635 if (list_empty(&cam
->buffers
)) {
636 set_bit(CF_SG_RESTART
, &cam
->flags
);
640 mcam_reg_clear_bit(cam
, REG_CTRL1
, C1_DESC_3WORD
);
641 mcam_sg_next_buffer(cam
);
647 * Frame completion with S/G is trickier. We can't muck with
648 * a descriptor chain on the fly, since the controller buffers it
649 * internally. So we have to actually stop and restart; Marvell
650 * says this is the way to do it.
652 * Of course, stopping is easier said than done; experience shows
653 * that the controller can start a frame *after* C0_ENABLE has been
654 * cleared. So when running in S/G mode, the controller is "stopped"
655 * on receipt of the start-of-frame interrupt. That means we can
656 * safely change the DMA descriptor array here and restart things
657 * (assuming there's another buffer waiting to go).
659 static void mcam_dma_sg_done(struct mcam_camera
*cam
, int frame
)
661 struct mcam_vb_buffer
*buf
= cam
->vb_bufs
[0];
664 * If we're no longer supposed to be streaming, don't do anything.
666 if (cam
->state
!= S_STREAMING
)
669 * If we have another buffer available, put it in and
670 * restart the engine.
672 if (!list_empty(&cam
->buffers
)) {
673 mcam_sg_next_buffer(cam
);
674 mcam_ctlr_start(cam
);
676 * Otherwise set CF_SG_RESTART and the controller will
677 * be restarted once another buffer shows up.
680 set_bit(CF_SG_RESTART
, &cam
->flags
);
681 cam
->frame_state
.singles
++;
682 cam
->vb_bufs
[0] = NULL
;
685 * Now we can give the completed frame back to user space.
687 cam
->frame_state
.delivered
++;
688 mcam_buffer_done(cam
, frame
, &buf
->vb_buf
);
693 * Scatter/gather mode requires stopping the controller between
694 * frames so we can put in a new DMA descriptor array. If no new
695 * buffer exists at frame completion, the controller is left stopped;
696 * this function is charged with gettig things going again.
698 static void mcam_sg_restart(struct mcam_camera
*cam
)
700 mcam_ctlr_dma_sg(cam
);
701 mcam_ctlr_start(cam
);
702 clear_bit(CF_SG_RESTART
, &cam
->flags
);
705 #else /* MCAM_MODE_DMA_SG */
707 static inline void mcam_sg_restart(struct mcam_camera
*cam
)
712 #endif /* MCAM_MODE_DMA_SG */
714 /* ---------------------------------------------------------------------- */
716 * Buffer-mode-independent controller code.
722 static void mcam_ctlr_image(struct mcam_camera
*cam
)
724 struct v4l2_pix_format
*fmt
= &cam
->pix_format
;
725 u32 widthy
= 0, widthuv
= 0, imgsz_h
, imgsz_w
;
727 cam_dbg(cam
, "camera: bytesperline = %d; height = %d\n",
728 fmt
->bytesperline
, fmt
->sizeimage
/ fmt
->bytesperline
);
729 imgsz_h
= (fmt
->height
<< IMGSZ_V_SHIFT
) & IMGSZ_V_MASK
;
730 imgsz_w
= (fmt
->width
* 2) & IMGSZ_H_MASK
;
732 switch (fmt
->pixelformat
) {
733 case V4L2_PIX_FMT_YUYV
:
734 case V4L2_PIX_FMT_YVYU
:
735 widthy
= fmt
->width
* 2;
738 case V4L2_PIX_FMT_YUV420
:
739 case V4L2_PIX_FMT_YVU420
:
741 widthuv
= fmt
->width
/ 2;
744 widthy
= fmt
->bytesperline
;
749 mcam_reg_write_mask(cam
, REG_IMGPITCH
, widthuv
<< 16 | widthy
,
750 IMGP_YP_MASK
| IMGP_UVP_MASK
);
751 mcam_reg_write(cam
, REG_IMGSIZE
, imgsz_h
| imgsz_w
);
752 mcam_reg_write(cam
, REG_IMGOFFSET
, 0x0);
755 * Tell the controller about the image format we are using.
757 switch (fmt
->pixelformat
) {
758 case V4L2_PIX_FMT_YUV420
:
759 case V4L2_PIX_FMT_YVU420
:
760 mcam_reg_write_mask(cam
, REG_CTRL0
,
761 C0_DF_YUV
| C0_YUV_420PL
| C0_YUVE_VYUY
, C0_DF_MASK
);
763 case V4L2_PIX_FMT_YUYV
:
764 mcam_reg_write_mask(cam
, REG_CTRL0
,
765 C0_DF_YUV
| C0_YUV_PACKED
| C0_YUVE_NOSWAP
, C0_DF_MASK
);
767 case V4L2_PIX_FMT_YVYU
:
768 mcam_reg_write_mask(cam
, REG_CTRL0
,
769 C0_DF_YUV
| C0_YUV_PACKED
| C0_YUVE_SWAP24
, C0_DF_MASK
);
771 case V4L2_PIX_FMT_XRGB444
:
772 mcam_reg_write_mask(cam
, REG_CTRL0
,
773 C0_DF_RGB
| C0_RGBF_444
| C0_RGB4_XBGR
, C0_DF_MASK
);
775 case V4L2_PIX_FMT_RGB565
:
776 mcam_reg_write_mask(cam
, REG_CTRL0
,
777 C0_DF_RGB
| C0_RGBF_565
| C0_RGB5_BGGR
, C0_DF_MASK
);
779 case V4L2_PIX_FMT_SBGGR8
:
780 mcam_reg_write_mask(cam
, REG_CTRL0
,
781 C0_DF_RGB
| C0_RGB5_GRBG
, C0_DF_MASK
);
784 cam_err(cam
, "camera: unknown format: %#x\n", fmt
->pixelformat
);
789 * Make sure it knows we want to use hsync/vsync.
791 mcam_reg_write_mask(cam
, REG_CTRL0
, C0_SIF_HVSYNC
, C0_SIFM_MASK
);
793 * This field controls the generation of EOF(DVP only)
795 if (cam
->bus_type
!= V4L2_MBUS_CSI2
)
796 mcam_reg_set_bit(cam
, REG_CTRL0
,
797 C0_EOF_VSYNC
| C0_VEDGE_CTRL
);
802 * Configure the controller for operation; caller holds the
805 static int mcam_ctlr_configure(struct mcam_camera
*cam
)
809 spin_lock_irqsave(&cam
->dev_lock
, flags
);
810 clear_bit(CF_SG_RESTART
, &cam
->flags
);
812 mcam_ctlr_image(cam
);
813 mcam_set_config_needed(cam
, 0);
814 spin_unlock_irqrestore(&cam
->dev_lock
, flags
);
818 static void mcam_ctlr_irq_enable(struct mcam_camera
*cam
)
821 * Clear any pending interrupts, since we do not
822 * expect to have I/O active prior to enabling.
824 mcam_reg_write(cam
, REG_IRQSTAT
, FRAMEIRQS
);
825 mcam_reg_set_bit(cam
, REG_IRQMASK
, FRAMEIRQS
);
828 static void mcam_ctlr_irq_disable(struct mcam_camera
*cam
)
830 mcam_reg_clear_bit(cam
, REG_IRQMASK
, FRAMEIRQS
);
835 static void mcam_ctlr_init(struct mcam_camera
*cam
)
839 spin_lock_irqsave(&cam
->dev_lock
, flags
);
841 * Make sure it's not powered down.
843 mcam_reg_clear_bit(cam
, REG_CTRL1
, C1_PWRDWN
);
845 * Turn off the enable bit. It sure should be off anyway,
846 * but it's good to be sure.
848 mcam_reg_clear_bit(cam
, REG_CTRL0
, C0_ENABLE
);
850 * Clock the sensor appropriately. Controller clock should
851 * be 48MHz, sensor "typical" value is half that.
853 mcam_reg_write_mask(cam
, REG_CLKCTRL
, 2, CLK_DIV_MASK
);
854 spin_unlock_irqrestore(&cam
->dev_lock
, flags
);
859 * Stop the controller, and don't return until we're really sure that no
860 * further DMA is going on.
862 static void mcam_ctlr_stop_dma(struct mcam_camera
*cam
)
867 * Theory: stop the camera controller (whether it is operating
868 * or not). Delay briefly just in case we race with the SOF
869 * interrupt, then wait until no DMA is active.
871 spin_lock_irqsave(&cam
->dev_lock
, flags
);
872 clear_bit(CF_SG_RESTART
, &cam
->flags
);
875 spin_unlock_irqrestore(&cam
->dev_lock
, flags
);
877 * This is a brutally long sleep, but experience shows that
878 * it can take the controller a while to get the message that
879 * it needs to stop grabbing frames. In particular, we can
880 * sometimes (on mmp) get a frame at the end WITHOUT the
881 * start-of-frame indication.
884 if (test_bit(CF_DMA_ACTIVE
, &cam
->flags
))
885 cam_err(cam
, "Timeout waiting for DMA to end\n");
886 /* This would be bad news - what now? */
887 spin_lock_irqsave(&cam
->dev_lock
, flags
);
888 mcam_ctlr_irq_disable(cam
);
889 spin_unlock_irqrestore(&cam
->dev_lock
, flags
);
895 static int mcam_ctlr_power_up(struct mcam_camera
*cam
)
900 spin_lock_irqsave(&cam
->dev_lock
, flags
);
901 ret
= cam
->plat_power_up(cam
);
903 spin_unlock_irqrestore(&cam
->dev_lock
, flags
);
906 mcam_reg_clear_bit(cam
, REG_CTRL1
, C1_PWRDWN
);
907 spin_unlock_irqrestore(&cam
->dev_lock
, flags
);
908 msleep(5); /* Just to be sure */
912 static void mcam_ctlr_power_down(struct mcam_camera
*cam
)
916 spin_lock_irqsave(&cam
->dev_lock
, flags
);
918 * School of hard knocks department: be sure we do any register
919 * twiddling on the controller *before* calling the platform
920 * power down routine.
922 mcam_reg_set_bit(cam
, REG_CTRL1
, C1_PWRDWN
);
923 cam
->plat_power_down(cam
);
924 spin_unlock_irqrestore(&cam
->dev_lock
, flags
);
927 /* -------------------------------------------------------------------- */
929 * Communications with the sensor.
932 static int __mcam_cam_reset(struct mcam_camera
*cam
)
934 return sensor_call(cam
, core
, reset
, 0);
938 * We have found the sensor on the i2c. Let's try to have a
941 static int mcam_cam_init(struct mcam_camera
*cam
)
945 if (cam
->state
!= S_NOTREADY
)
946 cam_warn(cam
, "Cam init with device in funky state %d",
948 ret
= __mcam_cam_reset(cam
);
949 /* Get/set parameters? */
951 mcam_ctlr_power_down(cam
);
956 * Configure the sensor to match the parameters we have. Caller should
959 static int mcam_cam_set_flip(struct mcam_camera
*cam
)
961 struct v4l2_control ctrl
;
963 memset(&ctrl
, 0, sizeof(ctrl
));
964 ctrl
.id
= V4L2_CID_VFLIP
;
966 return v4l2_s_ctrl(NULL
, cam
->sensor
->ctrl_handler
, &ctrl
);
970 static int mcam_cam_configure(struct mcam_camera
*cam
)
972 struct v4l2_subdev_format format
= {
973 .which
= V4L2_SUBDEV_FORMAT_ACTIVE
,
977 v4l2_fill_mbus_format(&format
.format
, &cam
->pix_format
, cam
->mbus_code
);
978 ret
= sensor_call(cam
, core
, init
, 0);
980 ret
= sensor_call(cam
, pad
, set_fmt
, NULL
, &format
);
982 * OV7670 does weird things if flip is set *before* format...
984 ret
+= mcam_cam_set_flip(cam
);
989 * Get everything ready, and start grabbing frames.
991 static int mcam_read_setup(struct mcam_camera
*cam
)
997 * Configuration. If we still don't have DMA buffers,
998 * make one last, desperate attempt.
1000 if (cam
->buffer_mode
== B_vmalloc
&& cam
->nbufs
== 0 &&
1001 mcam_alloc_dma_bufs(cam
, 0))
1004 if (mcam_needs_config(cam
)) {
1005 mcam_cam_configure(cam
);
1006 ret
= mcam_ctlr_configure(cam
);
1014 spin_lock_irqsave(&cam
->dev_lock
, flags
);
1015 clear_bit(CF_DMA_ACTIVE
, &cam
->flags
);
1016 mcam_reset_buffers(cam
);
1018 * Update CSI2_DPHY value
1021 cam
->calc_dphy(cam
);
1022 cam_dbg(cam
, "camera: DPHY sets: dphy3=0x%x, dphy5=0x%x, dphy6=0x%x\n",
1023 cam
->dphy
[0], cam
->dphy
[1], cam
->dphy
[2]);
1024 if (cam
->bus_type
== V4L2_MBUS_CSI2
)
1025 mcam_enable_mipi(cam
);
1027 mcam_disable_mipi(cam
);
1028 mcam_ctlr_irq_enable(cam
);
1029 cam
->state
= S_STREAMING
;
1030 if (!test_bit(CF_SG_RESTART
, &cam
->flags
))
1031 mcam_ctlr_start(cam
);
1032 spin_unlock_irqrestore(&cam
->dev_lock
, flags
);
1036 /* ----------------------------------------------------------------------- */
1038 * Videobuf2 interface code.
1041 static int mcam_vb_queue_setup(struct vb2_queue
*vq
,
1042 unsigned int *nbufs
,
1043 unsigned int *num_planes
, unsigned int sizes
[],
1044 struct device
*alloc_devs
[])
1046 struct mcam_camera
*cam
= vb2_get_drv_priv(vq
);
1047 int minbufs
= (cam
->buffer_mode
== B_DMA_contig
) ? 3 : 2;
1048 unsigned size
= cam
->pix_format
.sizeimage
;
1050 if (*nbufs
< minbufs
)
1054 return sizes
[0] < size
? -EINVAL
: 0;
1056 *num_planes
= 1; /* Someday we have to support planar formats... */
1061 static void mcam_vb_buf_queue(struct vb2_buffer
*vb
)
1063 struct vb2_v4l2_buffer
*vbuf
= to_vb2_v4l2_buffer(vb
);
1064 struct mcam_vb_buffer
*mvb
= vb_to_mvb(vbuf
);
1065 struct mcam_camera
*cam
= vb2_get_drv_priv(vb
->vb2_queue
);
1066 unsigned long flags
;
1069 spin_lock_irqsave(&cam
->dev_lock
, flags
);
1070 start
= (cam
->state
== S_BUFWAIT
) && !list_empty(&cam
->buffers
);
1071 list_add(&mvb
->queue
, &cam
->buffers
);
1072 if (cam
->state
== S_STREAMING
&& test_bit(CF_SG_RESTART
, &cam
->flags
))
1073 mcam_sg_restart(cam
);
1074 spin_unlock_irqrestore(&cam
->dev_lock
, flags
);
1076 mcam_read_setup(cam
);
1079 static void mcam_vb_requeue_bufs(struct vb2_queue
*vq
,
1080 enum vb2_buffer_state state
)
1082 struct mcam_camera
*cam
= vb2_get_drv_priv(vq
);
1083 struct mcam_vb_buffer
*buf
, *node
;
1084 unsigned long flags
;
1087 spin_lock_irqsave(&cam
->dev_lock
, flags
);
1088 list_for_each_entry_safe(buf
, node
, &cam
->buffers
, queue
) {
1089 vb2_buffer_done(&buf
->vb_buf
.vb2_buf
, state
);
1090 list_del(&buf
->queue
);
1092 for (i
= 0; i
< MAX_DMA_BUFS
; i
++) {
1093 buf
= cam
->vb_bufs
[i
];
1096 vb2_buffer_done(&buf
->vb_buf
.vb2_buf
, state
);
1097 cam
->vb_bufs
[i
] = NULL
;
1100 spin_unlock_irqrestore(&cam
->dev_lock
, flags
);
1104 * These need to be called with the mutex held from vb2
1106 static int mcam_vb_start_streaming(struct vb2_queue
*vq
, unsigned int count
)
1108 struct mcam_camera
*cam
= vb2_get_drv_priv(vq
);
1112 if (cam
->state
!= S_IDLE
) {
1113 mcam_vb_requeue_bufs(vq
, VB2_BUF_STATE_QUEUED
);
1116 cam
->frame_state
.frames
= 0;
1117 cam
->frame_state
.singles
= 0;
1118 cam
->frame_state
.delivered
= 0;
1121 * Videobuf2 sneakily hoards all the buffers and won't
1122 * give them to us until *after* streaming starts. But
1123 * we can't actually start streaming until we have a
1124 * destination. So go into a wait state and hope they
1125 * give us buffers soon.
1127 if (cam
->buffer_mode
!= B_vmalloc
&& list_empty(&cam
->buffers
)) {
1128 cam
->state
= S_BUFWAIT
;
1133 * Ensure clear the left over frame flags
1134 * before every really start streaming
1136 for (frame
= 0; frame
< cam
->nbufs
; frame
++)
1137 clear_bit(CF_FRAME_SOF0
+ frame
, &cam
->flags
);
1139 ret
= mcam_read_setup(cam
);
1141 mcam_vb_requeue_bufs(vq
, VB2_BUF_STATE_QUEUED
);
1145 static void mcam_vb_stop_streaming(struct vb2_queue
*vq
)
1147 struct mcam_camera
*cam
= vb2_get_drv_priv(vq
);
1149 cam_dbg(cam
, "stop_streaming: %d frames, %d singles, %d delivered\n",
1150 cam
->frame_state
.frames
, cam
->frame_state
.singles
,
1151 cam
->frame_state
.delivered
);
1152 if (cam
->state
== S_BUFWAIT
) {
1153 /* They never gave us buffers */
1154 cam
->state
= S_IDLE
;
1157 if (cam
->state
!= S_STREAMING
)
1159 mcam_ctlr_stop_dma(cam
);
1161 * Reset the CCIC PHY after stopping streaming,
1162 * otherwise, the CCIC may be unstable.
1164 if (cam
->ctlr_reset
)
1165 cam
->ctlr_reset(cam
);
1167 * VB2 reclaims the buffers, so we need to forget
1170 mcam_vb_requeue_bufs(vq
, VB2_BUF_STATE_ERROR
);
1174 static const struct vb2_ops mcam_vb2_ops
= {
1175 .queue_setup
= mcam_vb_queue_setup
,
1176 .buf_queue
= mcam_vb_buf_queue
,
1177 .start_streaming
= mcam_vb_start_streaming
,
1178 .stop_streaming
= mcam_vb_stop_streaming
,
1179 .wait_prepare
= vb2_ops_wait_prepare
,
1180 .wait_finish
= vb2_ops_wait_finish
,
1184 #ifdef MCAM_MODE_DMA_SG
1186 * Scatter/gather mode uses all of the above functions plus a
1187 * few extras to deal with DMA mapping.
1189 static int mcam_vb_sg_buf_init(struct vb2_buffer
*vb
)
1191 struct vb2_v4l2_buffer
*vbuf
= to_vb2_v4l2_buffer(vb
);
1192 struct mcam_vb_buffer
*mvb
= vb_to_mvb(vbuf
);
1193 struct mcam_camera
*cam
= vb2_get_drv_priv(vb
->vb2_queue
);
1194 int ndesc
= cam
->pix_format
.sizeimage
/PAGE_SIZE
+ 1;
1196 mvb
->dma_desc
= dma_alloc_coherent(cam
->dev
,
1197 ndesc
* sizeof(struct mcam_dma_desc
),
1198 &mvb
->dma_desc_pa
, GFP_KERNEL
);
1199 if (mvb
->dma_desc
== NULL
) {
1200 cam_err(cam
, "Unable to get DMA descriptor array\n");
1206 static int mcam_vb_sg_buf_prepare(struct vb2_buffer
*vb
)
1208 struct vb2_v4l2_buffer
*vbuf
= to_vb2_v4l2_buffer(vb
);
1209 struct mcam_vb_buffer
*mvb
= vb_to_mvb(vbuf
);
1210 struct sg_table
*sg_table
= vb2_dma_sg_plane_desc(vb
, 0);
1211 struct mcam_dma_desc
*desc
= mvb
->dma_desc
;
1212 struct scatterlist
*sg
;
1215 for_each_sg(sg_table
->sgl
, sg
, sg_table
->nents
, i
) {
1216 desc
->dma_addr
= sg_dma_address(sg
);
1217 desc
->segment_len
= sg_dma_len(sg
);
1223 static void mcam_vb_sg_buf_cleanup(struct vb2_buffer
*vb
)
1225 struct vb2_v4l2_buffer
*vbuf
= to_vb2_v4l2_buffer(vb
);
1226 struct mcam_camera
*cam
= vb2_get_drv_priv(vb
->vb2_queue
);
1227 struct mcam_vb_buffer
*mvb
= vb_to_mvb(vbuf
);
1228 int ndesc
= cam
->pix_format
.sizeimage
/PAGE_SIZE
+ 1;
1230 dma_free_coherent(cam
->dev
, ndesc
* sizeof(struct mcam_dma_desc
),
1231 mvb
->dma_desc
, mvb
->dma_desc_pa
);
1235 static const struct vb2_ops mcam_vb2_sg_ops
= {
1236 .queue_setup
= mcam_vb_queue_setup
,
1237 .buf_init
= mcam_vb_sg_buf_init
,
1238 .buf_prepare
= mcam_vb_sg_buf_prepare
,
1239 .buf_queue
= mcam_vb_buf_queue
,
1240 .buf_cleanup
= mcam_vb_sg_buf_cleanup
,
1241 .start_streaming
= mcam_vb_start_streaming
,
1242 .stop_streaming
= mcam_vb_stop_streaming
,
1243 .wait_prepare
= vb2_ops_wait_prepare
,
1244 .wait_finish
= vb2_ops_wait_finish
,
1247 #endif /* MCAM_MODE_DMA_SG */
1249 static int mcam_setup_vb2(struct mcam_camera
*cam
)
1251 struct vb2_queue
*vq
= &cam
->vb_queue
;
1253 memset(vq
, 0, sizeof(*vq
));
1254 vq
->type
= V4L2_BUF_TYPE_VIDEO_CAPTURE
;
1256 vq
->lock
= &cam
->s_mutex
;
1257 vq
->timestamp_flags
= V4L2_BUF_FLAG_TIMESTAMP_MONOTONIC
;
1258 vq
->io_modes
= VB2_MMAP
| VB2_USERPTR
| VB2_DMABUF
| VB2_READ
;
1259 vq
->buf_struct_size
= sizeof(struct mcam_vb_buffer
);
1261 INIT_LIST_HEAD(&cam
->buffers
);
1262 switch (cam
->buffer_mode
) {
1264 #ifdef MCAM_MODE_DMA_CONTIG
1265 vq
->ops
= &mcam_vb2_ops
;
1266 vq
->mem_ops
= &vb2_dma_contig_memops
;
1267 cam
->dma_setup
= mcam_ctlr_dma_contig
;
1268 cam
->frame_complete
= mcam_dma_contig_done
;
1272 #ifdef MCAM_MODE_DMA_SG
1273 vq
->ops
= &mcam_vb2_sg_ops
;
1274 vq
->mem_ops
= &vb2_dma_sg_memops
;
1275 cam
->dma_setup
= mcam_ctlr_dma_sg
;
1276 cam
->frame_complete
= mcam_dma_sg_done
;
1280 #ifdef MCAM_MODE_VMALLOC
1281 tasklet_init(&cam
->s_tasklet
, mcam_frame_tasklet
,
1282 (unsigned long) cam
);
1283 vq
->ops
= &mcam_vb2_ops
;
1284 vq
->mem_ops
= &vb2_vmalloc_memops
;
1285 cam
->dma_setup
= mcam_ctlr_dma_vmalloc
;
1286 cam
->frame_complete
= mcam_vmalloc_done
;
1290 return vb2_queue_init(vq
);
1294 /* ---------------------------------------------------------------------- */
1296 * The long list of V4L2 ioctl() operations.
1299 static int mcam_vidioc_querycap(struct file
*file
, void *priv
,
1300 struct v4l2_capability
*cap
)
1302 struct mcam_camera
*cam
= video_drvdata(file
);
1304 strcpy(cap
->driver
, "marvell_ccic");
1305 strcpy(cap
->card
, "marvell_ccic");
1306 strlcpy(cap
->bus_info
, cam
->bus_info
, sizeof(cap
->bus_info
));
1307 cap
->device_caps
= V4L2_CAP_VIDEO_CAPTURE
|
1308 V4L2_CAP_READWRITE
| V4L2_CAP_STREAMING
;
1309 cap
->capabilities
= cap
->device_caps
| V4L2_CAP_DEVICE_CAPS
;
1314 static int mcam_vidioc_enum_fmt_vid_cap(struct file
*filp
,
1315 void *priv
, struct v4l2_fmtdesc
*fmt
)
1317 if (fmt
->index
>= N_MCAM_FMTS
)
1319 strlcpy(fmt
->description
, mcam_formats
[fmt
->index
].desc
,
1320 sizeof(fmt
->description
));
1321 fmt
->pixelformat
= mcam_formats
[fmt
->index
].pixelformat
;
1325 static int mcam_vidioc_try_fmt_vid_cap(struct file
*filp
, void *priv
,
1326 struct v4l2_format
*fmt
)
1328 struct mcam_camera
*cam
= video_drvdata(filp
);
1329 struct mcam_format_struct
*f
;
1330 struct v4l2_pix_format
*pix
= &fmt
->fmt
.pix
;
1331 struct v4l2_subdev_pad_config pad_cfg
;
1332 struct v4l2_subdev_format format
= {
1333 .which
= V4L2_SUBDEV_FORMAT_TRY
,
1337 f
= mcam_find_format(pix
->pixelformat
);
1338 pix
->pixelformat
= f
->pixelformat
;
1339 v4l2_fill_mbus_format(&format
.format
, pix
, f
->mbus_code
);
1340 ret
= sensor_call(cam
, pad
, set_fmt
, &pad_cfg
, &format
);
1341 v4l2_fill_pix_format(pix
, &format
.format
);
1342 pix
->bytesperline
= pix
->width
* f
->bpp
;
1343 switch (f
->pixelformat
) {
1344 case V4L2_PIX_FMT_YUV420
:
1345 case V4L2_PIX_FMT_YVU420
:
1346 pix
->sizeimage
= pix
->height
* pix
->bytesperline
* 3 / 2;
1349 pix
->sizeimage
= pix
->height
* pix
->bytesperline
;
1352 pix
->colorspace
= V4L2_COLORSPACE_SRGB
;
1356 static int mcam_vidioc_s_fmt_vid_cap(struct file
*filp
, void *priv
,
1357 struct v4l2_format
*fmt
)
1359 struct mcam_camera
*cam
= video_drvdata(filp
);
1360 struct mcam_format_struct
*f
;
1364 * Can't do anything if the device is not idle
1365 * Also can't if there are streaming buffers in place.
1367 if (cam
->state
!= S_IDLE
|| vb2_is_busy(&cam
->vb_queue
))
1370 f
= mcam_find_format(fmt
->fmt
.pix
.pixelformat
);
1373 * See if the formatting works in principle.
1375 ret
= mcam_vidioc_try_fmt_vid_cap(filp
, priv
, fmt
);
1379 * Now we start to change things for real, so let's do it
1382 cam
->pix_format
= fmt
->fmt
.pix
;
1383 cam
->mbus_code
= f
->mbus_code
;
1386 * Make sure we have appropriate DMA buffers.
1388 if (cam
->buffer_mode
== B_vmalloc
) {
1389 ret
= mcam_check_dma_buffers(cam
);
1393 mcam_set_config_needed(cam
, 1);
1399 * Return our stored notion of how the camera is/should be configured.
1400 * The V4l2 spec wants us to be smarter, and actually get this from
1401 * the camera (and not mess with it at open time). Someday.
1403 static int mcam_vidioc_g_fmt_vid_cap(struct file
*filp
, void *priv
,
1404 struct v4l2_format
*f
)
1406 struct mcam_camera
*cam
= video_drvdata(filp
);
1408 f
->fmt
.pix
= cam
->pix_format
;
1413 * We only have one input - the sensor - so minimize the nonsense here.
1415 static int mcam_vidioc_enum_input(struct file
*filp
, void *priv
,
1416 struct v4l2_input
*input
)
1418 if (input
->index
!= 0)
1421 input
->type
= V4L2_INPUT_TYPE_CAMERA
;
1422 strcpy(input
->name
, "Camera");
1426 static int mcam_vidioc_g_input(struct file
*filp
, void *priv
, unsigned int *i
)
1432 static int mcam_vidioc_s_input(struct file
*filp
, void *priv
, unsigned int i
)
1440 * G/S_PARM. Most of this is done by the sensor, but we are
1441 * the level which controls the number of read buffers.
1443 static int mcam_vidioc_g_parm(struct file
*filp
, void *priv
,
1444 struct v4l2_streamparm
*parms
)
1446 struct mcam_camera
*cam
= video_drvdata(filp
);
1449 ret
= sensor_call(cam
, video
, g_parm
, parms
);
1450 parms
->parm
.capture
.readbuffers
= n_dma_bufs
;
1454 static int mcam_vidioc_s_parm(struct file
*filp
, void *priv
,
1455 struct v4l2_streamparm
*parms
)
1457 struct mcam_camera
*cam
= video_drvdata(filp
);
1460 ret
= sensor_call(cam
, video
, s_parm
, parms
);
1461 parms
->parm
.capture
.readbuffers
= n_dma_bufs
;
1465 static int mcam_vidioc_enum_framesizes(struct file
*filp
, void *priv
,
1466 struct v4l2_frmsizeenum
*sizes
)
1468 struct mcam_camera
*cam
= video_drvdata(filp
);
1469 struct mcam_format_struct
*f
;
1470 struct v4l2_subdev_frame_size_enum fse
= {
1471 .index
= sizes
->index
,
1472 .which
= V4L2_SUBDEV_FORMAT_ACTIVE
,
1476 f
= mcam_find_format(sizes
->pixel_format
);
1477 if (f
->pixelformat
!= sizes
->pixel_format
)
1479 fse
.code
= f
->mbus_code
;
1480 ret
= sensor_call(cam
, pad
, enum_frame_size
, NULL
, &fse
);
1483 if (fse
.min_width
== fse
.max_width
&&
1484 fse
.min_height
== fse
.max_height
) {
1485 sizes
->type
= V4L2_FRMSIZE_TYPE_DISCRETE
;
1486 sizes
->discrete
.width
= fse
.min_width
;
1487 sizes
->discrete
.height
= fse
.min_height
;
1490 sizes
->type
= V4L2_FRMSIZE_TYPE_CONTINUOUS
;
1491 sizes
->stepwise
.min_width
= fse
.min_width
;
1492 sizes
->stepwise
.max_width
= fse
.max_width
;
1493 sizes
->stepwise
.min_height
= fse
.min_height
;
1494 sizes
->stepwise
.max_height
= fse
.max_height
;
1495 sizes
->stepwise
.step_width
= 1;
1496 sizes
->stepwise
.step_height
= 1;
1500 static int mcam_vidioc_enum_frameintervals(struct file
*filp
, void *priv
,
1501 struct v4l2_frmivalenum
*interval
)
1503 struct mcam_camera
*cam
= video_drvdata(filp
);
1504 struct mcam_format_struct
*f
;
1505 struct v4l2_subdev_frame_interval_enum fie
= {
1506 .index
= interval
->index
,
1507 .width
= interval
->width
,
1508 .height
= interval
->height
,
1509 .which
= V4L2_SUBDEV_FORMAT_ACTIVE
,
1513 f
= mcam_find_format(interval
->pixel_format
);
1514 if (f
->pixelformat
!= interval
->pixel_format
)
1516 fie
.code
= f
->mbus_code
;
1517 ret
= sensor_call(cam
, pad
, enum_frame_interval
, NULL
, &fie
);
1520 interval
->type
= V4L2_FRMIVAL_TYPE_DISCRETE
;
1521 interval
->discrete
= fie
.interval
;
1525 #ifdef CONFIG_VIDEO_ADV_DEBUG
1526 static int mcam_vidioc_g_register(struct file
*file
, void *priv
,
1527 struct v4l2_dbg_register
*reg
)
1529 struct mcam_camera
*cam
= video_drvdata(file
);
1531 if (reg
->reg
> cam
->regs_size
- 4)
1533 reg
->val
= mcam_reg_read(cam
, reg
->reg
);
1538 static int mcam_vidioc_s_register(struct file
*file
, void *priv
,
1539 const struct v4l2_dbg_register
*reg
)
1541 struct mcam_camera
*cam
= video_drvdata(file
);
1543 if (reg
->reg
> cam
->regs_size
- 4)
1545 mcam_reg_write(cam
, reg
->reg
, reg
->val
);
1550 static const struct v4l2_ioctl_ops mcam_v4l_ioctl_ops
= {
1551 .vidioc_querycap
= mcam_vidioc_querycap
,
1552 .vidioc_enum_fmt_vid_cap
= mcam_vidioc_enum_fmt_vid_cap
,
1553 .vidioc_try_fmt_vid_cap
= mcam_vidioc_try_fmt_vid_cap
,
1554 .vidioc_s_fmt_vid_cap
= mcam_vidioc_s_fmt_vid_cap
,
1555 .vidioc_g_fmt_vid_cap
= mcam_vidioc_g_fmt_vid_cap
,
1556 .vidioc_enum_input
= mcam_vidioc_enum_input
,
1557 .vidioc_g_input
= mcam_vidioc_g_input
,
1558 .vidioc_s_input
= mcam_vidioc_s_input
,
1559 .vidioc_reqbufs
= vb2_ioctl_reqbufs
,
1560 .vidioc_create_bufs
= vb2_ioctl_create_bufs
,
1561 .vidioc_querybuf
= vb2_ioctl_querybuf
,
1562 .vidioc_qbuf
= vb2_ioctl_qbuf
,
1563 .vidioc_dqbuf
= vb2_ioctl_dqbuf
,
1564 .vidioc_expbuf
= vb2_ioctl_expbuf
,
1565 .vidioc_streamon
= vb2_ioctl_streamon
,
1566 .vidioc_streamoff
= vb2_ioctl_streamoff
,
1567 .vidioc_g_parm
= mcam_vidioc_g_parm
,
1568 .vidioc_s_parm
= mcam_vidioc_s_parm
,
1569 .vidioc_enum_framesizes
= mcam_vidioc_enum_framesizes
,
1570 .vidioc_enum_frameintervals
= mcam_vidioc_enum_frameintervals
,
1571 .vidioc_subscribe_event
= v4l2_ctrl_subscribe_event
,
1572 .vidioc_unsubscribe_event
= v4l2_event_unsubscribe
,
1573 #ifdef CONFIG_VIDEO_ADV_DEBUG
1574 .vidioc_g_register
= mcam_vidioc_g_register
,
1575 .vidioc_s_register
= mcam_vidioc_s_register
,
1579 /* ---------------------------------------------------------------------- */
1581 * Our various file operations.
1583 static int mcam_v4l_open(struct file
*filp
)
1585 struct mcam_camera
*cam
= video_drvdata(filp
);
1588 mutex_lock(&cam
->s_mutex
);
1589 ret
= v4l2_fh_open(filp
);
1592 if (v4l2_fh_is_singular_file(filp
)) {
1593 ret
= mcam_ctlr_power_up(cam
);
1596 __mcam_cam_reset(cam
);
1597 mcam_set_config_needed(cam
, 1);
1600 mutex_unlock(&cam
->s_mutex
);
1602 v4l2_fh_release(filp
);
1607 static int mcam_v4l_release(struct file
*filp
)
1609 struct mcam_camera
*cam
= video_drvdata(filp
);
1612 mutex_lock(&cam
->s_mutex
);
1613 last_open
= v4l2_fh_is_singular_file(filp
);
1614 _vb2_fop_release(filp
, NULL
);
1616 mcam_disable_mipi(cam
);
1617 mcam_ctlr_power_down(cam
);
1618 if (cam
->buffer_mode
== B_vmalloc
&& alloc_bufs_at_read
)
1619 mcam_free_dma_bufs(cam
);
1622 mutex_unlock(&cam
->s_mutex
);
1626 static const struct v4l2_file_operations mcam_v4l_fops
= {
1627 .owner
= THIS_MODULE
,
1628 .open
= mcam_v4l_open
,
1629 .release
= mcam_v4l_release
,
1630 .read
= vb2_fop_read
,
1631 .poll
= vb2_fop_poll
,
1632 .mmap
= vb2_fop_mmap
,
1633 .unlocked_ioctl
= video_ioctl2
,
1638 * This template device holds all of those v4l2 methods; we
1639 * clone it for specific real devices.
1641 static struct video_device mcam_v4l_template
= {
1643 .fops
= &mcam_v4l_fops
,
1644 .ioctl_ops
= &mcam_v4l_ioctl_ops
,
1645 .release
= video_device_release_empty
,
1648 /* ---------------------------------------------------------------------- */
1650 * Interrupt handler stuff
1652 static void mcam_frame_complete(struct mcam_camera
*cam
, int frame
)
1655 * Basic frame housekeeping.
1657 set_bit(frame
, &cam
->flags
);
1658 clear_bit(CF_DMA_ACTIVE
, &cam
->flags
);
1659 cam
->next_buf
= frame
;
1660 cam
->buf_seq
[frame
] = cam
->sequence
++;
1661 cam
->frame_state
.frames
++;
1663 * "This should never happen"
1665 if (cam
->state
!= S_STREAMING
)
1668 * Process the frame and set up the next one.
1670 cam
->frame_complete(cam
, frame
);
1675 * The interrupt handler; this needs to be called from the
1676 * platform irq handler with the lock held.
1678 int mccic_irq(struct mcam_camera
*cam
, unsigned int irqs
)
1680 unsigned int frame
, handled
= 0;
1682 mcam_reg_write(cam
, REG_IRQSTAT
, FRAMEIRQS
); /* Clear'em all */
1684 * Handle any frame completions. There really should
1685 * not be more than one of these, or we have fallen
1688 * When running in S/G mode, the frame number lacks any
1689 * real meaning - there's only one descriptor array - but
1690 * the controller still picks a different one to signal
1693 for (frame
= 0; frame
< cam
->nbufs
; frame
++)
1694 if (irqs
& (IRQ_EOF0
<< frame
) &&
1695 test_bit(CF_FRAME_SOF0
+ frame
, &cam
->flags
)) {
1696 mcam_frame_complete(cam
, frame
);
1698 clear_bit(CF_FRAME_SOF0
+ frame
, &cam
->flags
);
1699 if (cam
->buffer_mode
== B_DMA_sg
)
1703 * If a frame starts, note that we have DMA active. This
1704 * code assumes that we won't get multiple frame interrupts
1705 * at once; may want to rethink that.
1707 for (frame
= 0; frame
< cam
->nbufs
; frame
++) {
1708 if (irqs
& (IRQ_SOF0
<< frame
)) {
1709 set_bit(CF_FRAME_SOF0
+ frame
, &cam
->flags
);
1710 handled
= IRQ_HANDLED
;
1714 if (handled
== IRQ_HANDLED
) {
1715 set_bit(CF_DMA_ACTIVE
, &cam
->flags
);
1716 if (cam
->buffer_mode
== B_DMA_sg
)
1717 mcam_ctlr_stop(cam
);
1722 /* ---------------------------------------------------------------------- */
1724 * Registration and such.
1726 static struct ov7670_config sensor_cfg
= {
1728 * Exclude QCIF mode, because it only captures a tiny portion
1736 int mccic_register(struct mcam_camera
*cam
)
1738 struct i2c_board_info ov7670_info
= {
1741 .platform_data
= &sensor_cfg
,
1746 * Validate the requested buffer mode.
1748 if (buffer_mode
>= 0)
1749 cam
->buffer_mode
= buffer_mode
;
1750 if (cam
->buffer_mode
== B_DMA_sg
&&
1751 cam
->chip_id
== MCAM_CAFE
) {
1752 printk(KERN_ERR
"marvell-cam: Cafe can't do S/G I/O, attempting vmalloc mode instead\n");
1753 cam
->buffer_mode
= B_vmalloc
;
1755 if (!mcam_buffer_mode_supported(cam
->buffer_mode
)) {
1756 printk(KERN_ERR
"marvell-cam: buffer mode %d unsupported\n",
1763 ret
= v4l2_device_register(cam
->dev
, &cam
->v4l2_dev
);
1767 mutex_init(&cam
->s_mutex
);
1768 cam
->state
= S_NOTREADY
;
1769 mcam_set_config_needed(cam
, 1);
1770 cam
->pix_format
= mcam_def_pix_format
;
1771 cam
->mbus_code
= mcam_def_mbus_code
;
1772 mcam_ctlr_init(cam
);
1775 * Get the v4l2 setup done.
1777 ret
= v4l2_ctrl_handler_init(&cam
->ctrl_handler
, 10);
1779 goto out_unregister
;
1780 cam
->v4l2_dev
.ctrl_handler
= &cam
->ctrl_handler
;
1783 * Try to find the sensor.
1785 sensor_cfg
.clock_speed
= cam
->clock_speed
;
1786 sensor_cfg
.use_smbus
= cam
->use_smbus
;
1787 cam
->sensor_addr
= ov7670_info
.addr
;
1788 cam
->sensor
= v4l2_i2c_new_subdev_board(&cam
->v4l2_dev
,
1789 cam
->i2c_adapter
, &ov7670_info
, NULL
);
1790 if (cam
->sensor
== NULL
) {
1792 goto out_unregister
;
1795 ret
= mcam_cam_init(cam
);
1797 goto out_unregister
;
1799 ret
= mcam_setup_vb2(cam
);
1801 goto out_unregister
;
1803 mutex_lock(&cam
->s_mutex
);
1804 cam
->vdev
= mcam_v4l_template
;
1805 cam
->vdev
.v4l2_dev
= &cam
->v4l2_dev
;
1806 cam
->vdev
.lock
= &cam
->s_mutex
;
1807 cam
->vdev
.queue
= &cam
->vb_queue
;
1808 video_set_drvdata(&cam
->vdev
, cam
);
1809 ret
= video_register_device(&cam
->vdev
, VFL_TYPE_GRABBER
, -1);
1811 mutex_unlock(&cam
->s_mutex
);
1812 goto out_unregister
;
1816 * If so requested, try to get our DMA buffers now.
1818 if (cam
->buffer_mode
== B_vmalloc
&& !alloc_bufs_at_read
) {
1819 if (mcam_alloc_dma_bufs(cam
, 1))
1820 cam_warn(cam
, "Unable to alloc DMA buffers at load will try again later.");
1823 mutex_unlock(&cam
->s_mutex
);
1827 v4l2_ctrl_handler_free(&cam
->ctrl_handler
);
1828 v4l2_device_unregister(&cam
->v4l2_dev
);
1833 void mccic_shutdown(struct mcam_camera
*cam
)
1836 * If we have no users (and we really, really should have no
1837 * users) the device will already be powered down. Trying to
1838 * take it down again will wedge the machine, which is frowned
1841 if (!list_empty(&cam
->vdev
.fh_list
)) {
1842 cam_warn(cam
, "Removing a device with users!\n");
1843 mcam_ctlr_power_down(cam
);
1845 if (cam
->buffer_mode
== B_vmalloc
)
1846 mcam_free_dma_bufs(cam
);
1847 video_unregister_device(&cam
->vdev
);
1848 v4l2_ctrl_handler_free(&cam
->ctrl_handler
);
1849 v4l2_device_unregister(&cam
->v4l2_dev
);
1857 void mccic_suspend(struct mcam_camera
*cam
)
1859 mutex_lock(&cam
->s_mutex
);
1860 if (!list_empty(&cam
->vdev
.fh_list
)) {
1861 enum mcam_state cstate
= cam
->state
;
1863 mcam_ctlr_stop_dma(cam
);
1864 mcam_ctlr_power_down(cam
);
1865 cam
->state
= cstate
;
1867 mutex_unlock(&cam
->s_mutex
);
1870 int mccic_resume(struct mcam_camera
*cam
)
1874 mutex_lock(&cam
->s_mutex
);
1875 if (!list_empty(&cam
->vdev
.fh_list
)) {
1876 ret
= mcam_ctlr_power_up(cam
);
1878 mutex_unlock(&cam
->s_mutex
);
1881 __mcam_cam_reset(cam
);
1883 mcam_ctlr_power_down(cam
);
1885 mutex_unlock(&cam
->s_mutex
);
1887 set_bit(CF_CONFIG_NEEDED
, &cam
->flags
);
1888 if (cam
->state
== S_STREAMING
) {
1890 * If there was a buffer in the DMA engine at suspend
1891 * time, put it back on the queue or we'll forget about it.
1893 if (cam
->buffer_mode
== B_DMA_sg
&& cam
->vb_bufs
[0])
1894 list_add(&cam
->vb_bufs
[0]->queue
, &cam
->buffers
);
1895 ret
= mcam_read_setup(cam
);
1899 #endif /* CONFIG_PM */