1 // SPDX-License-Identifier: GPL-2.0-or-later
3 * Support eMMa-PrP through mem2mem framework.
5 * eMMa-PrP is a piece of HW that allows fetching buffers
6 * from one memory location and do several operations on
7 * them such as scaling or format conversion giving, as a result
8 * a new processed buffer in another memory location.
10 * Based on mem2mem_testdev.c by Pawel Osciak.
12 * Copyright (c) 2011 Vista Silicon S.L.
13 * Javier Martin <javier.martin@vista-silicon.com>
15 #include <linux/module.h>
16 #include <linux/clk.h>
17 #include <linux/slab.h>
18 #include <linux/interrupt.h>
21 #include <linux/platform_device.h>
22 #include <media/v4l2-mem2mem.h>
23 #include <media/v4l2-device.h>
24 #include <media/v4l2-ioctl.h>
25 #include <media/videobuf2-dma-contig.h>
26 #include <linux/sizes.h>
28 #define EMMAPRP_MODULE_NAME "mem2mem-emmaprp"
30 MODULE_DESCRIPTION("Mem-to-mem device which supports eMMa-PrP present in mx2 SoCs");
31 MODULE_AUTHOR("Javier Martin <javier.martin@vista-silicon.com");
32 MODULE_LICENSE("GPL");
33 MODULE_VERSION("0.0.1");
36 module_param(debug
, bool, 0644);
43 #define S_ALIGN 1 /* multiple of 2 */
44 #define W_ALIGN_YUV420 3 /* multiple of 8 */
45 #define W_ALIGN_OTHERS 2 /* multiple of 4 */
46 #define H_ALIGN 1 /* multiple of 2 */
48 /* Flags that indicate a format can be used for capture/output */
49 #define MEM2MEM_CAPTURE (1 << 0)
50 #define MEM2MEM_OUTPUT (1 << 1)
52 #define MEM2MEM_NAME "m2m-emmaprp"
54 /* In bytes, per queue */
55 #define MEM2MEM_VID_MEM_LIMIT SZ_16M
57 #define dprintk(dev, fmt, arg...) \
58 v4l2_dbg(1, debug, &dev->v4l2_dev, "%s: " fmt, __func__, ## arg)
62 #define PRP_INTR_CNTL 0x04
63 #define PRP_INTRSTATUS 0x08
64 #define PRP_SOURCE_Y_PTR 0x0c
65 #define PRP_SOURCE_CB_PTR 0x10
66 #define PRP_SOURCE_CR_PTR 0x14
67 #define PRP_DEST_RGB1_PTR 0x18
68 #define PRP_DEST_RGB2_PTR 0x1c
69 #define PRP_DEST_Y_PTR 0x20
70 #define PRP_DEST_CB_PTR 0x24
71 #define PRP_DEST_CR_PTR 0x28
72 #define PRP_SRC_FRAME_SIZE 0x2c
73 #define PRP_DEST_CH1_LINE_STRIDE 0x30
74 #define PRP_SRC_PIXEL_FORMAT_CNTL 0x34
75 #define PRP_CH1_PIXEL_FORMAT_CNTL 0x38
76 #define PRP_CH1_OUT_IMAGE_SIZE 0x3c
77 #define PRP_CH2_OUT_IMAGE_SIZE 0x40
78 #define PRP_SRC_LINE_STRIDE 0x44
79 #define PRP_CSC_COEF_012 0x48
80 #define PRP_CSC_COEF_345 0x4c
81 #define PRP_CSC_COEF_678 0x50
82 #define PRP_CH1_RZ_HORI_COEF1 0x54
83 #define PRP_CH1_RZ_HORI_COEF2 0x58
84 #define PRP_CH1_RZ_HORI_VALID 0x5c
85 #define PRP_CH1_RZ_VERT_COEF1 0x60
86 #define PRP_CH1_RZ_VERT_COEF2 0x64
87 #define PRP_CH1_RZ_VERT_VALID 0x68
88 #define PRP_CH2_RZ_HORI_COEF1 0x6c
89 #define PRP_CH2_RZ_HORI_COEF2 0x70
90 #define PRP_CH2_RZ_HORI_VALID 0x74
91 #define PRP_CH2_RZ_VERT_COEF1 0x78
92 #define PRP_CH2_RZ_VERT_COEF2 0x7c
93 #define PRP_CH2_RZ_VERT_VALID 0x80
95 #define PRP_CNTL_CH1EN (1 << 0)
96 #define PRP_CNTL_CH2EN (1 << 1)
97 #define PRP_CNTL_CSIEN (1 << 2)
98 #define PRP_CNTL_DATA_IN_YUV420 (0 << 3)
99 #define PRP_CNTL_DATA_IN_YUV422 (1 << 3)
100 #define PRP_CNTL_DATA_IN_RGB16 (2 << 3)
101 #define PRP_CNTL_DATA_IN_RGB32 (3 << 3)
102 #define PRP_CNTL_CH1_OUT_RGB8 (0 << 5)
103 #define PRP_CNTL_CH1_OUT_RGB16 (1 << 5)
104 #define PRP_CNTL_CH1_OUT_RGB32 (2 << 5)
105 #define PRP_CNTL_CH1_OUT_YUV422 (3 << 5)
106 #define PRP_CNTL_CH2_OUT_YUV420 (0 << 7)
107 #define PRP_CNTL_CH2_OUT_YUV422 (1 << 7)
108 #define PRP_CNTL_CH2_OUT_YUV444 (2 << 7)
109 #define PRP_CNTL_CH1_LEN (1 << 9)
110 #define PRP_CNTL_CH2_LEN (1 << 10)
111 #define PRP_CNTL_SKIP_FRAME (1 << 11)
112 #define PRP_CNTL_SWRST (1 << 12)
113 #define PRP_CNTL_CLKEN (1 << 13)
114 #define PRP_CNTL_WEN (1 << 14)
115 #define PRP_CNTL_CH1BYP (1 << 15)
116 #define PRP_CNTL_IN_TSKIP(x) ((x) << 16)
117 #define PRP_CNTL_CH1_TSKIP(x) ((x) << 19)
118 #define PRP_CNTL_CH2_TSKIP(x) ((x) << 22)
119 #define PRP_CNTL_INPUT_FIFO_LEVEL(x) ((x) << 25)
120 #define PRP_CNTL_RZ_FIFO_LEVEL(x) ((x) << 27)
121 #define PRP_CNTL_CH2B1EN (1 << 29)
122 #define PRP_CNTL_CH2B2EN (1 << 30)
123 #define PRP_CNTL_CH2FEN (1UL << 31)
125 #define PRP_SIZE_HEIGHT(x) (x)
126 #define PRP_SIZE_WIDTH(x) ((x) << 16)
128 /* IRQ Enable and status register */
129 #define PRP_INTR_RDERR (1 << 0)
130 #define PRP_INTR_CH1WERR (1 << 1)
131 #define PRP_INTR_CH2WERR (1 << 2)
132 #define PRP_INTR_CH1FC (1 << 3)
133 #define PRP_INTR_CH2FC (1 << 5)
134 #define PRP_INTR_LBOVF (1 << 7)
135 #define PRP_INTR_CH2OVF (1 << 8)
137 #define PRP_INTR_ST_RDERR (1 << 0)
138 #define PRP_INTR_ST_CH1WERR (1 << 1)
139 #define PRP_INTR_ST_CH2WERR (1 << 2)
140 #define PRP_INTR_ST_CH2B2CI (1 << 3)
141 #define PRP_INTR_ST_CH2B1CI (1 << 4)
142 #define PRP_INTR_ST_CH1B2CI (1 << 5)
143 #define PRP_INTR_ST_CH1B1CI (1 << 6)
144 #define PRP_INTR_ST_LBOVF (1 << 7)
145 #define PRP_INTR_ST_CH2OVF (1 << 8)
149 /* Types the format can be used for */
153 static struct emmaprp_fmt formats
[] = {
155 .fourcc
= V4L2_PIX_FMT_YUV420
,
156 .types
= MEM2MEM_CAPTURE
,
159 .fourcc
= V4L2_PIX_FMT_YUYV
,
160 .types
= MEM2MEM_OUTPUT
,
164 /* Per-queue, driver-specific private data */
165 struct emmaprp_q_data
{
168 unsigned int sizeimage
;
169 struct emmaprp_fmt
*fmt
;
177 #define NUM_FORMATS ARRAY_SIZE(formats)
179 static struct emmaprp_fmt
*find_format(struct v4l2_format
*f
)
181 struct emmaprp_fmt
*fmt
;
184 for (k
= 0; k
< NUM_FORMATS
; k
++) {
186 if (fmt
->fourcc
== f
->fmt
.pix
.pixelformat
)
190 if (k
== NUM_FORMATS
)
197 struct v4l2_device v4l2_dev
;
198 struct video_device
*vfd
;
200 struct mutex dev_mutex
;
203 void __iomem
*base_emma
;
204 struct clk
*clk_emma_ahb
, *clk_emma_ipg
;
206 struct v4l2_m2m_dev
*m2m_dev
;
211 struct emmaprp_dev
*dev
;
212 /* Abort requested by m2m */
214 struct emmaprp_q_data q_data
[2];
217 static struct emmaprp_q_data
*get_q_data(struct emmaprp_ctx
*ctx
,
218 enum v4l2_buf_type type
)
221 case V4L2_BUF_TYPE_VIDEO_OUTPUT
:
222 return &(ctx
->q_data
[V4L2_M2M_SRC
]);
223 case V4L2_BUF_TYPE_VIDEO_CAPTURE
:
224 return &(ctx
->q_data
[V4L2_M2M_DST
]);
234 static void emmaprp_job_abort(void *priv
)
236 struct emmaprp_ctx
*ctx
= priv
;
237 struct emmaprp_dev
*pcdev
= ctx
->dev
;
241 dprintk(pcdev
, "Aborting task\n");
243 v4l2_m2m_job_finish(pcdev
->m2m_dev
, ctx
->fh
.m2m_ctx
);
246 static inline void emmaprp_dump_regs(struct emmaprp_dev
*pcdev
)
249 "eMMa-PrP Registers:\n"
250 " SOURCE_Y_PTR = 0x%08X\n"
251 " SRC_FRAME_SIZE = 0x%08X\n"
252 " DEST_Y_PTR = 0x%08X\n"
253 " DEST_CR_PTR = 0x%08X\n"
254 " DEST_CB_PTR = 0x%08X\n"
255 " CH2_OUT_IMAGE_SIZE = 0x%08X\n"
257 readl(pcdev
->base_emma
+ PRP_SOURCE_Y_PTR
),
258 readl(pcdev
->base_emma
+ PRP_SRC_FRAME_SIZE
),
259 readl(pcdev
->base_emma
+ PRP_DEST_Y_PTR
),
260 readl(pcdev
->base_emma
+ PRP_DEST_CR_PTR
),
261 readl(pcdev
->base_emma
+ PRP_DEST_CB_PTR
),
262 readl(pcdev
->base_emma
+ PRP_CH2_OUT_IMAGE_SIZE
),
263 readl(pcdev
->base_emma
+ PRP_CNTL
));
266 static void emmaprp_device_run(void *priv
)
268 struct emmaprp_ctx
*ctx
= priv
;
269 struct emmaprp_q_data
*s_q_data
, *d_q_data
;
270 struct vb2_v4l2_buffer
*src_buf
, *dst_buf
;
271 struct emmaprp_dev
*pcdev
= ctx
->dev
;
272 unsigned int s_width
, s_height
;
273 unsigned int d_width
, d_height
;
275 dma_addr_t p_in
, p_out
;
278 src_buf
= v4l2_m2m_next_src_buf(ctx
->fh
.m2m_ctx
);
279 dst_buf
= v4l2_m2m_next_dst_buf(ctx
->fh
.m2m_ctx
);
281 s_q_data
= get_q_data(ctx
, V4L2_BUF_TYPE_VIDEO_OUTPUT
);
282 s_width
= s_q_data
->width
;
283 s_height
= s_q_data
->height
;
285 d_q_data
= get_q_data(ctx
, V4L2_BUF_TYPE_VIDEO_CAPTURE
);
286 d_width
= d_q_data
->width
;
287 d_height
= d_q_data
->height
;
288 d_size
= d_width
* d_height
;
290 p_in
= vb2_dma_contig_plane_dma_addr(&src_buf
->vb2_buf
, 0);
291 p_out
= vb2_dma_contig_plane_dma_addr(&dst_buf
->vb2_buf
, 0);
292 if (!p_in
|| !p_out
) {
293 v4l2_err(&pcdev
->v4l2_dev
,
294 "Acquiring kernel pointers to buffers failed\n");
298 /* Input frame parameters */
299 writel(p_in
, pcdev
->base_emma
+ PRP_SOURCE_Y_PTR
);
300 writel(PRP_SIZE_WIDTH(s_width
) | PRP_SIZE_HEIGHT(s_height
),
301 pcdev
->base_emma
+ PRP_SRC_FRAME_SIZE
);
303 /* Output frame parameters */
304 writel(p_out
, pcdev
->base_emma
+ PRP_DEST_Y_PTR
);
305 writel(p_out
+ d_size
, pcdev
->base_emma
+ PRP_DEST_CB_PTR
);
306 writel(p_out
+ d_size
+ (d_size
>> 2),
307 pcdev
->base_emma
+ PRP_DEST_CR_PTR
);
308 writel(PRP_SIZE_WIDTH(d_width
) | PRP_SIZE_HEIGHT(d_height
),
309 pcdev
->base_emma
+ PRP_CH2_OUT_IMAGE_SIZE
);
311 /* IRQ configuration */
312 tmp
= readl(pcdev
->base_emma
+ PRP_INTR_CNTL
);
313 writel(tmp
| PRP_INTR_RDERR
|
316 pcdev
->base_emma
+ PRP_INTR_CNTL
);
318 emmaprp_dump_regs(pcdev
);
320 /* Enable transfer */
321 tmp
= readl(pcdev
->base_emma
+ PRP_CNTL
);
322 writel(tmp
| PRP_CNTL_CH2_OUT_YUV420
|
323 PRP_CNTL_DATA_IN_YUV422
|
325 pcdev
->base_emma
+ PRP_CNTL
);
328 static irqreturn_t
emmaprp_irq(int irq_emma
, void *data
)
330 struct emmaprp_dev
*pcdev
= data
;
331 struct emmaprp_ctx
*curr_ctx
;
332 struct vb2_v4l2_buffer
*src_vb
, *dst_vb
;
336 /* Check irq flags and clear irq */
337 irqst
= readl(pcdev
->base_emma
+ PRP_INTRSTATUS
);
338 writel(irqst
, pcdev
->base_emma
+ PRP_INTRSTATUS
);
339 dprintk(pcdev
, "irqst = 0x%08x\n", irqst
);
341 curr_ctx
= v4l2_m2m_get_curr_priv(pcdev
->m2m_dev
);
342 if (curr_ctx
== NULL
) {
343 pr_err("Instance released before the end of transaction\n");
347 if (!curr_ctx
->aborting
) {
348 if ((irqst
& PRP_INTR_ST_RDERR
) ||
349 (irqst
& PRP_INTR_ST_CH2WERR
)) {
350 pr_err("PrP bus error occurred, this transfer is probably corrupted\n");
351 writel(PRP_CNTL_SWRST
, pcdev
->base_emma
+ PRP_CNTL
);
352 } else if (irqst
& PRP_INTR_ST_CH2B1CI
) { /* buffer ready */
353 src_vb
= v4l2_m2m_src_buf_remove(curr_ctx
->fh
.m2m_ctx
);
354 dst_vb
= v4l2_m2m_dst_buf_remove(curr_ctx
->fh
.m2m_ctx
);
356 dst_vb
->vb2_buf
.timestamp
= src_vb
->vb2_buf
.timestamp
;
358 ~V4L2_BUF_FLAG_TSTAMP_SRC_MASK
;
361 & V4L2_BUF_FLAG_TSTAMP_SRC_MASK
;
362 dst_vb
->timecode
= src_vb
->timecode
;
364 spin_lock_irqsave(&pcdev
->irqlock
, flags
);
365 v4l2_m2m_buf_done(src_vb
, VB2_BUF_STATE_DONE
);
366 v4l2_m2m_buf_done(dst_vb
, VB2_BUF_STATE_DONE
);
367 spin_unlock_irqrestore(&pcdev
->irqlock
, flags
);
371 v4l2_m2m_job_finish(pcdev
->m2m_dev
, curr_ctx
->fh
.m2m_ctx
);
378 static int vidioc_querycap(struct file
*file
, void *priv
,
379 struct v4l2_capability
*cap
)
381 strscpy(cap
->driver
, MEM2MEM_NAME
, sizeof(cap
->driver
));
382 strscpy(cap
->card
, MEM2MEM_NAME
, sizeof(cap
->card
));
386 static int enum_fmt(struct v4l2_fmtdesc
*f
, u32 type
)
389 struct emmaprp_fmt
*fmt
;
393 for (i
= 0; i
< NUM_FORMATS
; ++i
) {
394 if (formats
[i
].types
& type
) {
395 /* index-th format of type type found ? */
398 /* Correct type but haven't reached our index yet,
399 * just increment per-type index */
404 if (i
< NUM_FORMATS
) {
407 f
->pixelformat
= fmt
->fourcc
;
411 /* Format not found */
415 static int vidioc_enum_fmt_vid_cap(struct file
*file
, void *priv
,
416 struct v4l2_fmtdesc
*f
)
418 return enum_fmt(f
, MEM2MEM_CAPTURE
);
421 static int vidioc_enum_fmt_vid_out(struct file
*file
, void *priv
,
422 struct v4l2_fmtdesc
*f
)
424 return enum_fmt(f
, MEM2MEM_OUTPUT
);
427 static int vidioc_g_fmt(struct emmaprp_ctx
*ctx
, struct v4l2_format
*f
)
429 struct vb2_queue
*vq
;
430 struct emmaprp_q_data
*q_data
;
432 vq
= v4l2_m2m_get_vq(ctx
->fh
.m2m_ctx
, f
->type
);
436 q_data
= get_q_data(ctx
, f
->type
);
438 f
->fmt
.pix
.width
= q_data
->width
;
439 f
->fmt
.pix
.height
= q_data
->height
;
440 f
->fmt
.pix
.field
= V4L2_FIELD_NONE
;
441 f
->fmt
.pix
.pixelformat
= q_data
->fmt
->fourcc
;
442 if (f
->fmt
.pix
.pixelformat
== V4L2_PIX_FMT_YUV420
)
443 f
->fmt
.pix
.bytesperline
= q_data
->width
* 3 / 2;
445 f
->fmt
.pix
.bytesperline
= q_data
->width
* 2;
446 f
->fmt
.pix
.sizeimage
= q_data
->sizeimage
;
451 static int vidioc_g_fmt_vid_out(struct file
*file
, void *priv
,
452 struct v4l2_format
*f
)
454 return vidioc_g_fmt(priv
, f
);
457 static int vidioc_g_fmt_vid_cap(struct file
*file
, void *priv
,
458 struct v4l2_format
*f
)
460 return vidioc_g_fmt(priv
, f
);
463 static int vidioc_try_fmt(struct v4l2_format
*f
)
465 enum v4l2_field field
;
471 field
= f
->fmt
.pix
.field
;
472 if (field
== V4L2_FIELD_ANY
)
473 field
= V4L2_FIELD_NONE
;
474 else if (V4L2_FIELD_NONE
!= field
)
477 /* V4L2 specification suggests the driver corrects the format struct
478 * if any of the dimensions is unsupported */
479 f
->fmt
.pix
.field
= field
;
481 if (f
->fmt
.pix
.pixelformat
== V4L2_PIX_FMT_YUV420
) {
482 v4l_bound_align_image(&f
->fmt
.pix
.width
, MIN_W
, MAX_W
,
483 W_ALIGN_YUV420
, &f
->fmt
.pix
.height
,
484 MIN_H
, MAX_H
, H_ALIGN
, S_ALIGN
);
485 f
->fmt
.pix
.bytesperline
= f
->fmt
.pix
.width
* 3 / 2;
487 v4l_bound_align_image(&f
->fmt
.pix
.width
, MIN_W
, MAX_W
,
488 W_ALIGN_OTHERS
, &f
->fmt
.pix
.height
,
489 MIN_H
, MAX_H
, H_ALIGN
, S_ALIGN
);
490 f
->fmt
.pix
.bytesperline
= f
->fmt
.pix
.width
* 2;
492 f
->fmt
.pix
.sizeimage
= f
->fmt
.pix
.height
* f
->fmt
.pix
.bytesperline
;
497 static int vidioc_try_fmt_vid_cap(struct file
*file
, void *priv
,
498 struct v4l2_format
*f
)
500 struct emmaprp_fmt
*fmt
;
501 struct emmaprp_ctx
*ctx
= priv
;
503 fmt
= find_format(f
);
504 if (!fmt
|| !(fmt
->types
& MEM2MEM_CAPTURE
)) {
505 v4l2_err(&ctx
->dev
->v4l2_dev
,
506 "Fourcc format (0x%08x) invalid.\n",
507 f
->fmt
.pix
.pixelformat
);
511 return vidioc_try_fmt(f
);
514 static int vidioc_try_fmt_vid_out(struct file
*file
, void *priv
,
515 struct v4l2_format
*f
)
517 struct emmaprp_fmt
*fmt
;
518 struct emmaprp_ctx
*ctx
= priv
;
520 fmt
= find_format(f
);
521 if (!fmt
|| !(fmt
->types
& MEM2MEM_OUTPUT
)) {
522 v4l2_err(&ctx
->dev
->v4l2_dev
,
523 "Fourcc format (0x%08x) invalid.\n",
524 f
->fmt
.pix
.pixelformat
);
528 return vidioc_try_fmt(f
);
531 static int vidioc_s_fmt(struct emmaprp_ctx
*ctx
, struct v4l2_format
*f
)
533 struct emmaprp_q_data
*q_data
;
534 struct vb2_queue
*vq
;
537 vq
= v4l2_m2m_get_vq(ctx
->fh
.m2m_ctx
, f
->type
);
541 q_data
= get_q_data(ctx
, f
->type
);
545 if (vb2_is_busy(vq
)) {
546 v4l2_err(&ctx
->dev
->v4l2_dev
, "%s queue busy\n", __func__
);
550 ret
= vidioc_try_fmt(f
);
554 q_data
->fmt
= find_format(f
);
555 q_data
->width
= f
->fmt
.pix
.width
;
556 q_data
->height
= f
->fmt
.pix
.height
;
557 if (q_data
->fmt
->fourcc
== V4L2_PIX_FMT_YUV420
)
558 q_data
->sizeimage
= q_data
->width
* q_data
->height
* 3 / 2;
560 q_data
->sizeimage
= q_data
->width
* q_data
->height
* 2;
563 "Setting format for type %d, wxh: %dx%d, fmt: %d\n",
564 f
->type
, q_data
->width
, q_data
->height
, q_data
->fmt
->fourcc
);
569 static int vidioc_s_fmt_vid_cap(struct file
*file
, void *priv
,
570 struct v4l2_format
*f
)
574 ret
= vidioc_try_fmt_vid_cap(file
, priv
, f
);
578 return vidioc_s_fmt(priv
, f
);
581 static int vidioc_s_fmt_vid_out(struct file
*file
, void *priv
,
582 struct v4l2_format
*f
)
586 ret
= vidioc_try_fmt_vid_out(file
, priv
, f
);
590 return vidioc_s_fmt(priv
, f
);
593 static const struct v4l2_ioctl_ops emmaprp_ioctl_ops
= {
594 .vidioc_querycap
= vidioc_querycap
,
596 .vidioc_enum_fmt_vid_cap
= vidioc_enum_fmt_vid_cap
,
597 .vidioc_g_fmt_vid_cap
= vidioc_g_fmt_vid_cap
,
598 .vidioc_try_fmt_vid_cap
= vidioc_try_fmt_vid_cap
,
599 .vidioc_s_fmt_vid_cap
= vidioc_s_fmt_vid_cap
,
601 .vidioc_enum_fmt_vid_out
= vidioc_enum_fmt_vid_out
,
602 .vidioc_g_fmt_vid_out
= vidioc_g_fmt_vid_out
,
603 .vidioc_try_fmt_vid_out
= vidioc_try_fmt_vid_out
,
604 .vidioc_s_fmt_vid_out
= vidioc_s_fmt_vid_out
,
606 .vidioc_reqbufs
= v4l2_m2m_ioctl_reqbufs
,
607 .vidioc_querybuf
= v4l2_m2m_ioctl_querybuf
,
608 .vidioc_qbuf
= v4l2_m2m_ioctl_qbuf
,
609 .vidioc_dqbuf
= v4l2_m2m_ioctl_dqbuf
,
610 .vidioc_prepare_buf
= v4l2_m2m_ioctl_prepare_buf
,
611 .vidioc_expbuf
= v4l2_m2m_ioctl_expbuf
,
612 .vidioc_streamon
= v4l2_m2m_ioctl_streamon
,
613 .vidioc_streamoff
= v4l2_m2m_ioctl_streamoff
,
620 static int emmaprp_queue_setup(struct vb2_queue
*vq
,
621 unsigned int *nbuffers
, unsigned int *nplanes
,
622 unsigned int sizes
[], struct device
*alloc_devs
[])
624 struct emmaprp_ctx
*ctx
= vb2_get_drv_priv(vq
);
625 struct emmaprp_q_data
*q_data
;
626 unsigned int size
, count
= *nbuffers
;
628 q_data
= get_q_data(ctx
, vq
->type
);
630 if (q_data
->fmt
->fourcc
== V4L2_PIX_FMT_YUV420
)
631 size
= q_data
->width
* q_data
->height
* 3 / 2;
633 size
= q_data
->width
* q_data
->height
* 2;
635 while (size
* count
> MEM2MEM_VID_MEM_LIMIT
)
642 dprintk(ctx
->dev
, "get %d buffer(s) of size %d each.\n", count
, size
);
647 static int emmaprp_buf_prepare(struct vb2_buffer
*vb
)
649 struct emmaprp_ctx
*ctx
= vb2_get_drv_priv(vb
->vb2_queue
);
650 struct emmaprp_q_data
*q_data
;
652 dprintk(ctx
->dev
, "type: %d\n", vb
->vb2_queue
->type
);
654 q_data
= get_q_data(ctx
, vb
->vb2_queue
->type
);
656 if (vb2_plane_size(vb
, 0) < q_data
->sizeimage
) {
658 "%s data will not fit into plane(%lu < %lu)\n",
659 __func__
, vb2_plane_size(vb
, 0),
660 (long)q_data
->sizeimage
);
664 vb2_set_plane_payload(vb
, 0, q_data
->sizeimage
);
669 static void emmaprp_buf_queue(struct vb2_buffer
*vb
)
671 struct vb2_v4l2_buffer
*vbuf
= to_vb2_v4l2_buffer(vb
);
672 struct emmaprp_ctx
*ctx
= vb2_get_drv_priv(vb
->vb2_queue
);
673 v4l2_m2m_buf_queue(ctx
->fh
.m2m_ctx
, vbuf
);
676 static const struct vb2_ops emmaprp_qops
= {
677 .queue_setup
= emmaprp_queue_setup
,
678 .buf_prepare
= emmaprp_buf_prepare
,
679 .buf_queue
= emmaprp_buf_queue
,
680 .wait_prepare
= vb2_ops_wait_prepare
,
681 .wait_finish
= vb2_ops_wait_finish
,
684 static int queue_init(void *priv
, struct vb2_queue
*src_vq
,
685 struct vb2_queue
*dst_vq
)
687 struct emmaprp_ctx
*ctx
= priv
;
690 src_vq
->type
= V4L2_BUF_TYPE_VIDEO_OUTPUT
;
691 src_vq
->io_modes
= VB2_MMAP
| VB2_USERPTR
| VB2_DMABUF
;
692 src_vq
->drv_priv
= ctx
;
693 src_vq
->buf_struct_size
= sizeof(struct v4l2_m2m_buffer
);
694 src_vq
->ops
= &emmaprp_qops
;
695 src_vq
->mem_ops
= &vb2_dma_contig_memops
;
696 src_vq
->timestamp_flags
= V4L2_BUF_FLAG_TIMESTAMP_COPY
;
697 src_vq
->dev
= ctx
->dev
->v4l2_dev
.dev
;
698 src_vq
->lock
= &ctx
->dev
->dev_mutex
;
700 ret
= vb2_queue_init(src_vq
);
704 dst_vq
->type
= V4L2_BUF_TYPE_VIDEO_CAPTURE
;
705 dst_vq
->io_modes
= VB2_MMAP
| VB2_USERPTR
| VB2_DMABUF
;
706 dst_vq
->drv_priv
= ctx
;
707 dst_vq
->buf_struct_size
= sizeof(struct v4l2_m2m_buffer
);
708 dst_vq
->ops
= &emmaprp_qops
;
709 dst_vq
->mem_ops
= &vb2_dma_contig_memops
;
710 dst_vq
->timestamp_flags
= V4L2_BUF_FLAG_TIMESTAMP_COPY
;
711 dst_vq
->dev
= ctx
->dev
->v4l2_dev
.dev
;
712 dst_vq
->lock
= &ctx
->dev
->dev_mutex
;
714 return vb2_queue_init(dst_vq
);
720 static int emmaprp_open(struct file
*file
)
722 struct emmaprp_dev
*pcdev
= video_drvdata(file
);
723 struct emmaprp_ctx
*ctx
;
725 ctx
= kzalloc(sizeof *ctx
, GFP_KERNEL
);
729 v4l2_fh_init(&ctx
->fh
, video_devdata(file
));
730 file
->private_data
= &ctx
->fh
;
733 if (mutex_lock_interruptible(&pcdev
->dev_mutex
)) {
738 ctx
->fh
.m2m_ctx
= v4l2_m2m_ctx_init(pcdev
->m2m_dev
, ctx
, &queue_init
);
740 if (IS_ERR(ctx
->fh
.m2m_ctx
)) {
741 int ret
= PTR_ERR(ctx
->fh
.m2m_ctx
);
743 mutex_unlock(&pcdev
->dev_mutex
);
748 clk_prepare_enable(pcdev
->clk_emma_ipg
);
749 clk_prepare_enable(pcdev
->clk_emma_ahb
);
750 ctx
->q_data
[V4L2_M2M_SRC
].fmt
= &formats
[1];
751 ctx
->q_data
[V4L2_M2M_DST
].fmt
= &formats
[0];
752 v4l2_fh_add(&ctx
->fh
);
753 mutex_unlock(&pcdev
->dev_mutex
);
755 dprintk(pcdev
, "Created instance %p, m2m_ctx: %p\n", ctx
, ctx
->fh
.m2m_ctx
);
760 static int emmaprp_release(struct file
*file
)
762 struct emmaprp_dev
*pcdev
= video_drvdata(file
);
763 struct emmaprp_ctx
*ctx
= file
->private_data
;
765 dprintk(pcdev
, "Releasing instance %p\n", ctx
);
767 mutex_lock(&pcdev
->dev_mutex
);
768 clk_disable_unprepare(pcdev
->clk_emma_ahb
);
769 clk_disable_unprepare(pcdev
->clk_emma_ipg
);
770 v4l2_fh_del(&ctx
->fh
);
771 v4l2_fh_exit(&ctx
->fh
);
772 v4l2_m2m_ctx_release(ctx
->fh
.m2m_ctx
);
773 mutex_unlock(&pcdev
->dev_mutex
);
779 static const struct v4l2_file_operations emmaprp_fops
= {
780 .owner
= THIS_MODULE
,
781 .open
= emmaprp_open
,
782 .release
= emmaprp_release
,
783 .poll
= v4l2_m2m_fop_poll
,
784 .unlocked_ioctl
= video_ioctl2
,
785 .mmap
= v4l2_m2m_fop_mmap
,
788 static const struct video_device emmaprp_videodev
= {
789 .name
= MEM2MEM_NAME
,
790 .fops
= &emmaprp_fops
,
791 .ioctl_ops
= &emmaprp_ioctl_ops
,
793 .release
= video_device_release
,
794 .vfl_dir
= VFL_DIR_M2M
,
795 .device_caps
= V4L2_CAP_VIDEO_M2M
| V4L2_CAP_STREAMING
,
798 static const struct v4l2_m2m_ops m2m_ops
= {
799 .device_run
= emmaprp_device_run
,
800 .job_abort
= emmaprp_job_abort
,
803 static int emmaprp_probe(struct platform_device
*pdev
)
805 struct emmaprp_dev
*pcdev
;
806 struct video_device
*vfd
;
807 struct resource
*res
;
810 pcdev
= devm_kzalloc(&pdev
->dev
, sizeof(*pcdev
), GFP_KERNEL
);
814 spin_lock_init(&pcdev
->irqlock
);
816 pcdev
->clk_emma_ipg
= devm_clk_get(&pdev
->dev
, "ipg");
817 if (IS_ERR(pcdev
->clk_emma_ipg
)) {
818 return PTR_ERR(pcdev
->clk_emma_ipg
);
821 pcdev
->clk_emma_ahb
= devm_clk_get(&pdev
->dev
, "ahb");
822 if (IS_ERR(pcdev
->clk_emma_ahb
))
823 return PTR_ERR(pcdev
->clk_emma_ahb
);
825 res
= platform_get_resource(pdev
, IORESOURCE_MEM
, 0);
826 pcdev
->base_emma
= devm_ioremap_resource(&pdev
->dev
, res
);
827 if (IS_ERR(pcdev
->base_emma
))
828 return PTR_ERR(pcdev
->base_emma
);
830 ret
= v4l2_device_register(&pdev
->dev
, &pcdev
->v4l2_dev
);
834 mutex_init(&pcdev
->dev_mutex
);
836 vfd
= video_device_alloc();
838 v4l2_err(&pcdev
->v4l2_dev
, "Failed to allocate video device\n");
843 *vfd
= emmaprp_videodev
;
844 vfd
->lock
= &pcdev
->dev_mutex
;
845 vfd
->v4l2_dev
= &pcdev
->v4l2_dev
;
847 video_set_drvdata(vfd
, pcdev
);
849 v4l2_info(&pcdev
->v4l2_dev
, EMMAPRP_MODULE_NAME
850 " Device registered as /dev/video%d\n", vfd
->num
);
852 platform_set_drvdata(pdev
, pcdev
);
854 irq
= platform_get_irq(pdev
, 0);
860 ret
= devm_request_irq(&pdev
->dev
, irq
, emmaprp_irq
, 0,
861 dev_name(&pdev
->dev
), pcdev
);
865 pcdev
->m2m_dev
= v4l2_m2m_init(&m2m_ops
);
866 if (IS_ERR(pcdev
->m2m_dev
)) {
867 v4l2_err(&pcdev
->v4l2_dev
, "Failed to init mem2mem device\n");
868 ret
= PTR_ERR(pcdev
->m2m_dev
);
872 ret
= video_register_device(vfd
, VFL_TYPE_VIDEO
, 0);
874 v4l2_err(&pcdev
->v4l2_dev
, "Failed to register video device\n");
882 v4l2_m2m_release(pcdev
->m2m_dev
);
884 video_device_release(vfd
);
886 v4l2_device_unregister(&pcdev
->v4l2_dev
);
888 mutex_destroy(&pcdev
->dev_mutex
);
893 static int emmaprp_remove(struct platform_device
*pdev
)
895 struct emmaprp_dev
*pcdev
= platform_get_drvdata(pdev
);
897 v4l2_info(&pcdev
->v4l2_dev
, "Removing " EMMAPRP_MODULE_NAME
);
899 video_unregister_device(pcdev
->vfd
);
900 v4l2_m2m_release(pcdev
->m2m_dev
);
901 v4l2_device_unregister(&pcdev
->v4l2_dev
);
902 mutex_destroy(&pcdev
->dev_mutex
);
907 static struct platform_driver emmaprp_pdrv
= {
908 .probe
= emmaprp_probe
,
909 .remove
= emmaprp_remove
,
911 .name
= MEM2MEM_NAME
,
914 module_platform_driver(emmaprp_pdrv
);