Merge tag 'csky-for-linus-4.20-fixup-dtb' of https://github.com/c-sky/csky-linux
[linux-block.git] / drivers / media / platform / mx2_emmaprp.c
CommitLineData
8091cb7d
JM
1/*
2 * Support eMMa-PrP through mem2mem framework.
3 *
4 * eMMa-PrP is a piece of HW that allows fetching buffers
5 * from one memory location and do several operations on
6 * them such as scaling or format conversion giving, as a result
7 * a new processed buffer in another memory location.
8 *
9 * Based on mem2mem_testdev.c by Pawel Osciak.
10 *
11 * Copyright (c) 2011 Vista Silicon S.L.
12 * Javier Martin <javier.martin@vista-silicon.com>
13 *
14 * This program is free software; you can redistribute it and/or modify
15 * it under the terms of the GNU General Public License as published by the
16 * Free Software Foundation; either version 2 of the
17 * License, or (at your option) any later version
18 */
19#include <linux/module.h>
20#include <linux/clk.h>
21#include <linux/slab.h>
22#include <linux/interrupt.h>
23#include <linux/io.h>
24
25#include <linux/platform_device.h>
26#include <media/v4l2-mem2mem.h>
27#include <media/v4l2-device.h>
28#include <media/v4l2-ioctl.h>
29#include <media/videobuf2-dma-contig.h>
1c17d8c7 30#include <linux/sizes.h>
8091cb7d
JM
31
32#define EMMAPRP_MODULE_NAME "mem2mem-emmaprp"
33
34MODULE_DESCRIPTION("Mem-to-mem device which supports eMMa-PrP present in mx2 SoCs");
35MODULE_AUTHOR("Javier Martin <javier.martin@vista-silicon.com");
36MODULE_LICENSE("GPL");
37MODULE_VERSION("0.0.1");
38
39static bool debug;
40module_param(debug, bool, 0644);
41
42#define MIN_W 32
43#define MIN_H 32
44#define MAX_W 2040
45#define MAX_H 2046
46
47#define S_ALIGN 1 /* multiple of 2 */
48#define W_ALIGN_YUV420 3 /* multiple of 8 */
49#define W_ALIGN_OTHERS 2 /* multiple of 4 */
50#define H_ALIGN 1 /* multiple of 2 */
51
52/* Flags that indicate a format can be used for capture/output */
53#define MEM2MEM_CAPTURE (1 << 0)
54#define MEM2MEM_OUTPUT (1 << 1)
55
56#define MEM2MEM_NAME "m2m-emmaprp"
57
58/* In bytes, per queue */
59#define MEM2MEM_VID_MEM_LIMIT SZ_16M
60
61#define dprintk(dev, fmt, arg...) \
62 v4l2_dbg(1, debug, &dev->v4l2_dev, "%s: " fmt, __func__, ## arg)
63
64/* EMMA PrP */
65#define PRP_CNTL 0x00
66#define PRP_INTR_CNTL 0x04
67#define PRP_INTRSTATUS 0x08
68#define PRP_SOURCE_Y_PTR 0x0c
69#define PRP_SOURCE_CB_PTR 0x10
70#define PRP_SOURCE_CR_PTR 0x14
71#define PRP_DEST_RGB1_PTR 0x18
72#define PRP_DEST_RGB2_PTR 0x1c
73#define PRP_DEST_Y_PTR 0x20
74#define PRP_DEST_CB_PTR 0x24
75#define PRP_DEST_CR_PTR 0x28
76#define PRP_SRC_FRAME_SIZE 0x2c
77#define PRP_DEST_CH1_LINE_STRIDE 0x30
78#define PRP_SRC_PIXEL_FORMAT_CNTL 0x34
79#define PRP_CH1_PIXEL_FORMAT_CNTL 0x38
80#define PRP_CH1_OUT_IMAGE_SIZE 0x3c
81#define PRP_CH2_OUT_IMAGE_SIZE 0x40
82#define PRP_SRC_LINE_STRIDE 0x44
83#define PRP_CSC_COEF_012 0x48
84#define PRP_CSC_COEF_345 0x4c
85#define PRP_CSC_COEF_678 0x50
86#define PRP_CH1_RZ_HORI_COEF1 0x54
87#define PRP_CH1_RZ_HORI_COEF2 0x58
88#define PRP_CH1_RZ_HORI_VALID 0x5c
89#define PRP_CH1_RZ_VERT_COEF1 0x60
90#define PRP_CH1_RZ_VERT_COEF2 0x64
91#define PRP_CH1_RZ_VERT_VALID 0x68
92#define PRP_CH2_RZ_HORI_COEF1 0x6c
93#define PRP_CH2_RZ_HORI_COEF2 0x70
94#define PRP_CH2_RZ_HORI_VALID 0x74
95#define PRP_CH2_RZ_VERT_COEF1 0x78
96#define PRP_CH2_RZ_VERT_COEF2 0x7c
97#define PRP_CH2_RZ_VERT_VALID 0x80
98
99#define PRP_CNTL_CH1EN (1 << 0)
100#define PRP_CNTL_CH2EN (1 << 1)
101#define PRP_CNTL_CSIEN (1 << 2)
102#define PRP_CNTL_DATA_IN_YUV420 (0 << 3)
103#define PRP_CNTL_DATA_IN_YUV422 (1 << 3)
104#define PRP_CNTL_DATA_IN_RGB16 (2 << 3)
105#define PRP_CNTL_DATA_IN_RGB32 (3 << 3)
106#define PRP_CNTL_CH1_OUT_RGB8 (0 << 5)
107#define PRP_CNTL_CH1_OUT_RGB16 (1 << 5)
108#define PRP_CNTL_CH1_OUT_RGB32 (2 << 5)
109#define PRP_CNTL_CH1_OUT_YUV422 (3 << 5)
110#define PRP_CNTL_CH2_OUT_YUV420 (0 << 7)
111#define PRP_CNTL_CH2_OUT_YUV422 (1 << 7)
112#define PRP_CNTL_CH2_OUT_YUV444 (2 << 7)
113#define PRP_CNTL_CH1_LEN (1 << 9)
114#define PRP_CNTL_CH2_LEN (1 << 10)
115#define PRP_CNTL_SKIP_FRAME (1 << 11)
116#define PRP_CNTL_SWRST (1 << 12)
117#define PRP_CNTL_CLKEN (1 << 13)
118#define PRP_CNTL_WEN (1 << 14)
119#define PRP_CNTL_CH1BYP (1 << 15)
120#define PRP_CNTL_IN_TSKIP(x) ((x) << 16)
121#define PRP_CNTL_CH1_TSKIP(x) ((x) << 19)
122#define PRP_CNTL_CH2_TSKIP(x) ((x) << 22)
123#define PRP_CNTL_INPUT_FIFO_LEVEL(x) ((x) << 25)
124#define PRP_CNTL_RZ_FIFO_LEVEL(x) ((x) << 27)
125#define PRP_CNTL_CH2B1EN (1 << 29)
126#define PRP_CNTL_CH2B2EN (1 << 30)
127#define PRP_CNTL_CH2FEN (1 << 31)
128
129#define PRP_SIZE_HEIGHT(x) (x)
130#define PRP_SIZE_WIDTH(x) ((x) << 16)
131
132/* IRQ Enable and status register */
133#define PRP_INTR_RDERR (1 << 0)
134#define PRP_INTR_CH1WERR (1 << 1)
135#define PRP_INTR_CH2WERR (1 << 2)
136#define PRP_INTR_CH1FC (1 << 3)
137#define PRP_INTR_CH2FC (1 << 5)
138#define PRP_INTR_LBOVF (1 << 7)
139#define PRP_INTR_CH2OVF (1 << 8)
140
141#define PRP_INTR_ST_RDERR (1 << 0)
142#define PRP_INTR_ST_CH1WERR (1 << 1)
143#define PRP_INTR_ST_CH2WERR (1 << 2)
144#define PRP_INTR_ST_CH2B2CI (1 << 3)
145#define PRP_INTR_ST_CH2B1CI (1 << 4)
146#define PRP_INTR_ST_CH1B2CI (1 << 5)
147#define PRP_INTR_ST_CH1B1CI (1 << 6)
148#define PRP_INTR_ST_LBOVF (1 << 7)
149#define PRP_INTR_ST_CH2OVF (1 << 8)
150
151struct emmaprp_fmt {
152 char *name;
153 u32 fourcc;
154 /* Types the format can be used for */
155 u32 types;
156};
157
158static struct emmaprp_fmt formats[] = {
159 {
160 .name = "YUV 4:2:0 Planar",
161 .fourcc = V4L2_PIX_FMT_YUV420,
162 .types = MEM2MEM_CAPTURE,
163 },
164 {
165 .name = "4:2:2, packed, YUYV",
166 .fourcc = V4L2_PIX_FMT_YUYV,
167 .types = MEM2MEM_OUTPUT,
168 },
169};
170
171/* Per-queue, driver-specific private data */
172struct emmaprp_q_data {
173 unsigned int width;
174 unsigned int height;
175 unsigned int sizeimage;
176 struct emmaprp_fmt *fmt;
177};
178
179enum {
180 V4L2_M2M_SRC = 0,
181 V4L2_M2M_DST = 1,
182};
183
184#define NUM_FORMATS ARRAY_SIZE(formats)
185
186static struct emmaprp_fmt *find_format(struct v4l2_format *f)
187{
188 struct emmaprp_fmt *fmt;
189 unsigned int k;
190
191 for (k = 0; k < NUM_FORMATS; k++) {
192 fmt = &formats[k];
193 if (fmt->fourcc == f->fmt.pix.pixelformat)
194 break;
195 }
196
197 if (k == NUM_FORMATS)
198 return NULL;
199
200 return &formats[k];
201}
202
203struct emmaprp_dev {
204 struct v4l2_device v4l2_dev;
205 struct video_device *vfd;
206
207 struct mutex dev_mutex;
208 spinlock_t irqlock;
209
8091cb7d 210 void __iomem *base_emma;
33eb46a7 211 struct clk *clk_emma_ahb, *clk_emma_ipg;
8091cb7d
JM
212
213 struct v4l2_m2m_dev *m2m_dev;
8091cb7d
JM
214};
215
216struct emmaprp_ctx {
217 struct emmaprp_dev *dev;
218 /* Abort requested by m2m */
219 int aborting;
220 struct emmaprp_q_data q_data[2];
221 struct v4l2_m2m_ctx *m2m_ctx;
222};
223
224static struct emmaprp_q_data *get_q_data(struct emmaprp_ctx *ctx,
225 enum v4l2_buf_type type)
226{
227 switch (type) {
228 case V4L2_BUF_TYPE_VIDEO_OUTPUT:
229 return &(ctx->q_data[V4L2_M2M_SRC]);
230 case V4L2_BUF_TYPE_VIDEO_CAPTURE:
231 return &(ctx->q_data[V4L2_M2M_DST]);
232 default:
233 BUG();
234 }
235 return NULL;
236}
237
238/*
239 * mem2mem callbacks
240 */
241static void emmaprp_job_abort(void *priv)
242{
243 struct emmaprp_ctx *ctx = priv;
244 struct emmaprp_dev *pcdev = ctx->dev;
245
246 ctx->aborting = 1;
247
248 dprintk(pcdev, "Aborting task\n");
249
250 v4l2_m2m_job_finish(pcdev->m2m_dev, ctx->m2m_ctx);
251}
252
8091cb7d
JM
253static inline void emmaprp_dump_regs(struct emmaprp_dev *pcdev)
254{
255 dprintk(pcdev,
256 "eMMa-PrP Registers:\n"
257 " SOURCE_Y_PTR = 0x%08X\n"
258 " SRC_FRAME_SIZE = 0x%08X\n"
259 " DEST_Y_PTR = 0x%08X\n"
260 " DEST_CR_PTR = 0x%08X\n"
261 " DEST_CB_PTR = 0x%08X\n"
262 " CH2_OUT_IMAGE_SIZE = 0x%08X\n"
263 " CNTL = 0x%08X\n",
264 readl(pcdev->base_emma + PRP_SOURCE_Y_PTR),
265 readl(pcdev->base_emma + PRP_SRC_FRAME_SIZE),
266 readl(pcdev->base_emma + PRP_DEST_Y_PTR),
267 readl(pcdev->base_emma + PRP_DEST_CR_PTR),
268 readl(pcdev->base_emma + PRP_DEST_CB_PTR),
269 readl(pcdev->base_emma + PRP_CH2_OUT_IMAGE_SIZE),
270 readl(pcdev->base_emma + PRP_CNTL));
271}
272
273static void emmaprp_device_run(void *priv)
274{
275 struct emmaprp_ctx *ctx = priv;
276 struct emmaprp_q_data *s_q_data, *d_q_data;
277 struct vb2_buffer *src_buf, *dst_buf;
278 struct emmaprp_dev *pcdev = ctx->dev;
279 unsigned int s_width, s_height;
280 unsigned int d_width, d_height;
281 unsigned int d_size;
282 dma_addr_t p_in, p_out;
283 u32 tmp;
284
285 src_buf = v4l2_m2m_next_src_buf(ctx->m2m_ctx);
286 dst_buf = v4l2_m2m_next_dst_buf(ctx->m2m_ctx);
287
288 s_q_data = get_q_data(ctx, V4L2_BUF_TYPE_VIDEO_OUTPUT);
289 s_width = s_q_data->width;
290 s_height = s_q_data->height;
291
292 d_q_data = get_q_data(ctx, V4L2_BUF_TYPE_VIDEO_CAPTURE);
293 d_width = d_q_data->width;
294 d_height = d_q_data->height;
295 d_size = d_width * d_height;
296
297 p_in = vb2_dma_contig_plane_dma_addr(src_buf, 0);
298 p_out = vb2_dma_contig_plane_dma_addr(dst_buf, 0);
299 if (!p_in || !p_out) {
300 v4l2_err(&pcdev->v4l2_dev,
301 "Acquiring kernel pointers to buffers failed\n");
302 return;
303 }
304
305 /* Input frame parameters */
306 writel(p_in, pcdev->base_emma + PRP_SOURCE_Y_PTR);
307 writel(PRP_SIZE_WIDTH(s_width) | PRP_SIZE_HEIGHT(s_height),
308 pcdev->base_emma + PRP_SRC_FRAME_SIZE);
309
310 /* Output frame parameters */
311 writel(p_out, pcdev->base_emma + PRP_DEST_Y_PTR);
312 writel(p_out + d_size, pcdev->base_emma + PRP_DEST_CB_PTR);
313 writel(p_out + d_size + (d_size >> 2),
314 pcdev->base_emma + PRP_DEST_CR_PTR);
315 writel(PRP_SIZE_WIDTH(d_width) | PRP_SIZE_HEIGHT(d_height),
316 pcdev->base_emma + PRP_CH2_OUT_IMAGE_SIZE);
317
318 /* IRQ configuration */
319 tmp = readl(pcdev->base_emma + PRP_INTR_CNTL);
320 writel(tmp | PRP_INTR_RDERR |
321 PRP_INTR_CH2WERR |
322 PRP_INTR_CH2FC,
323 pcdev->base_emma + PRP_INTR_CNTL);
324
325 emmaprp_dump_regs(pcdev);
326
327 /* Enable transfer */
328 tmp = readl(pcdev->base_emma + PRP_CNTL);
329 writel(tmp | PRP_CNTL_CH2_OUT_YUV420 |
330 PRP_CNTL_DATA_IN_YUV422 |
331 PRP_CNTL_CH2EN,
332 pcdev->base_emma + PRP_CNTL);
333}
334
335static irqreturn_t emmaprp_irq(int irq_emma, void *data)
336{
337 struct emmaprp_dev *pcdev = data;
338 struct emmaprp_ctx *curr_ctx;
2d700715 339 struct vb2_v4l2_buffer *src_vb, *dst_vb;
8091cb7d
JM
340 unsigned long flags;
341 u32 irqst;
342
343 /* Check irq flags and clear irq */
344 irqst = readl(pcdev->base_emma + PRP_INTRSTATUS);
345 writel(irqst, pcdev->base_emma + PRP_INTRSTATUS);
346 dprintk(pcdev, "irqst = 0x%08x\n", irqst);
347
348 curr_ctx = v4l2_m2m_get_curr_priv(pcdev->m2m_dev);
349 if (curr_ctx == NULL) {
350 pr_err("Instance released before the end of transaction\n");
351 return IRQ_HANDLED;
352 }
353
354 if (!curr_ctx->aborting) {
355 if ((irqst & PRP_INTR_ST_RDERR) ||
356 (irqst & PRP_INTR_ST_CH2WERR)) {
9c768207 357 pr_err("PrP bus error occurred, this transfer is probably corrupted\n");
8091cb7d
JM
358 writel(PRP_CNTL_SWRST, pcdev->base_emma + PRP_CNTL);
359 } else if (irqst & PRP_INTR_ST_CH2B1CI) { /* buffer ready */
360 src_vb = v4l2_m2m_src_buf_remove(curr_ctx->m2m_ctx);
361 dst_vb = v4l2_m2m_dst_buf_remove(curr_ctx->m2m_ctx);
362
d6dd645e 363 dst_vb->vb2_buf.timestamp = src_vb->vb2_buf.timestamp;
2d700715 364 dst_vb->flags &=
309f4d62 365 ~V4L2_BUF_FLAG_TSTAMP_SRC_MASK;
2d700715
JS
366 dst_vb->flags |=
367 src_vb->flags
309f4d62 368 & V4L2_BUF_FLAG_TSTAMP_SRC_MASK;
2d700715 369 dst_vb->timecode = src_vb->timecode;
a8c9c345 370
8091cb7d
JM
371 spin_lock_irqsave(&pcdev->irqlock, flags);
372 v4l2_m2m_buf_done(src_vb, VB2_BUF_STATE_DONE);
373 v4l2_m2m_buf_done(dst_vb, VB2_BUF_STATE_DONE);
374 spin_unlock_irqrestore(&pcdev->irqlock, flags);
375 }
376 }
377
378 v4l2_m2m_job_finish(pcdev->m2m_dev, curr_ctx->m2m_ctx);
379 return IRQ_HANDLED;
380}
381
382/*
383 * video ioctls
384 */
385static int vidioc_querycap(struct file *file, void *priv,
386 struct v4l2_capability *cap)
387{
388 strncpy(cap->driver, MEM2MEM_NAME, sizeof(cap->driver) - 1);
389 strncpy(cap->card, MEM2MEM_NAME, sizeof(cap->card) - 1);
a020c747
HV
390 cap->device_caps = V4L2_CAP_VIDEO_M2M | V4L2_CAP_STREAMING;
391 cap->capabilities = cap->device_caps | V4L2_CAP_DEVICE_CAPS;
8091cb7d
JM
392 return 0;
393}
394
395static int enum_fmt(struct v4l2_fmtdesc *f, u32 type)
396{
397 int i, num;
398 struct emmaprp_fmt *fmt;
399
400 num = 0;
401
402 for (i = 0; i < NUM_FORMATS; ++i) {
403 if (formats[i].types & type) {
404 /* index-th format of type type found ? */
405 if (num == f->index)
406 break;
407 /* Correct type but haven't reached our index yet,
408 * just increment per-type index */
409 ++num;
410 }
411 }
412
413 if (i < NUM_FORMATS) {
414 /* Format found */
415 fmt = &formats[i];
c0decac1 416 strscpy(f->description, fmt->name, sizeof(f->description) - 1);
8091cb7d
JM
417 f->pixelformat = fmt->fourcc;
418 return 0;
419 }
420
421 /* Format not found */
422 return -EINVAL;
423}
424
425static int vidioc_enum_fmt_vid_cap(struct file *file, void *priv,
426 struct v4l2_fmtdesc *f)
427{
428 return enum_fmt(f, MEM2MEM_CAPTURE);
429}
430
431static int vidioc_enum_fmt_vid_out(struct file *file, void *priv,
432 struct v4l2_fmtdesc *f)
433{
434 return enum_fmt(f, MEM2MEM_OUTPUT);
435}
436
437static int vidioc_g_fmt(struct emmaprp_ctx *ctx, struct v4l2_format *f)
438{
439 struct vb2_queue *vq;
440 struct emmaprp_q_data *q_data;
441
442 vq = v4l2_m2m_get_vq(ctx->m2m_ctx, f->type);
443 if (!vq)
444 return -EINVAL;
445
446 q_data = get_q_data(ctx, f->type);
447
448 f->fmt.pix.width = q_data->width;
449 f->fmt.pix.height = q_data->height;
450 f->fmt.pix.field = V4L2_FIELD_NONE;
451 f->fmt.pix.pixelformat = q_data->fmt->fourcc;
452 if (f->fmt.pix.pixelformat == V4L2_PIX_FMT_YUV420)
453 f->fmt.pix.bytesperline = q_data->width * 3 / 2;
454 else /* YUYV */
455 f->fmt.pix.bytesperline = q_data->width * 2;
456 f->fmt.pix.sizeimage = q_data->sizeimage;
457
458 return 0;
459}
460
461static int vidioc_g_fmt_vid_out(struct file *file, void *priv,
462 struct v4l2_format *f)
463{
464 return vidioc_g_fmt(priv, f);
465}
466
467static int vidioc_g_fmt_vid_cap(struct file *file, void *priv,
468 struct v4l2_format *f)
469{
470 return vidioc_g_fmt(priv, f);
471}
472
473static int vidioc_try_fmt(struct v4l2_format *f)
474{
475 enum v4l2_field field;
476
477
478 if (!find_format(f))
479 return -EINVAL;
480
481 field = f->fmt.pix.field;
482 if (field == V4L2_FIELD_ANY)
483 field = V4L2_FIELD_NONE;
484 else if (V4L2_FIELD_NONE != field)
485 return -EINVAL;
486
487 /* V4L2 specification suggests the driver corrects the format struct
488 * if any of the dimensions is unsupported */
489 f->fmt.pix.field = field;
490
491 if (f->fmt.pix.pixelformat == V4L2_PIX_FMT_YUV420) {
492 v4l_bound_align_image(&f->fmt.pix.width, MIN_W, MAX_W,
493 W_ALIGN_YUV420, &f->fmt.pix.height,
494 MIN_H, MAX_H, H_ALIGN, S_ALIGN);
495 f->fmt.pix.bytesperline = f->fmt.pix.width * 3 / 2;
496 } else {
497 v4l_bound_align_image(&f->fmt.pix.width, MIN_W, MAX_W,
498 W_ALIGN_OTHERS, &f->fmt.pix.height,
499 MIN_H, MAX_H, H_ALIGN, S_ALIGN);
500 f->fmt.pix.bytesperline = f->fmt.pix.width * 2;
501 }
502 f->fmt.pix.sizeimage = f->fmt.pix.height * f->fmt.pix.bytesperline;
503
504 return 0;
505}
506
507static int vidioc_try_fmt_vid_cap(struct file *file, void *priv,
508 struct v4l2_format *f)
509{
510 struct emmaprp_fmt *fmt;
511 struct emmaprp_ctx *ctx = priv;
512
513 fmt = find_format(f);
514 if (!fmt || !(fmt->types & MEM2MEM_CAPTURE)) {
515 v4l2_err(&ctx->dev->v4l2_dev,
516 "Fourcc format (0x%08x) invalid.\n",
517 f->fmt.pix.pixelformat);
518 return -EINVAL;
519 }
520
521 return vidioc_try_fmt(f);
522}
523
524static int vidioc_try_fmt_vid_out(struct file *file, void *priv,
525 struct v4l2_format *f)
526{
527 struct emmaprp_fmt *fmt;
528 struct emmaprp_ctx *ctx = priv;
529
530 fmt = find_format(f);
531 if (!fmt || !(fmt->types & MEM2MEM_OUTPUT)) {
532 v4l2_err(&ctx->dev->v4l2_dev,
533 "Fourcc format (0x%08x) invalid.\n",
534 f->fmt.pix.pixelformat);
535 return -EINVAL;
536 }
537
538 return vidioc_try_fmt(f);
539}
540
541static int vidioc_s_fmt(struct emmaprp_ctx *ctx, struct v4l2_format *f)
542{
543 struct emmaprp_q_data *q_data;
544 struct vb2_queue *vq;
545 int ret;
546
547 vq = v4l2_m2m_get_vq(ctx->m2m_ctx, f->type);
548 if (!vq)
549 return -EINVAL;
550
551 q_data = get_q_data(ctx, f->type);
552 if (!q_data)
553 return -EINVAL;
554
555 if (vb2_is_busy(vq)) {
556 v4l2_err(&ctx->dev->v4l2_dev, "%s queue busy\n", __func__);
557 return -EBUSY;
558 }
559
560 ret = vidioc_try_fmt(f);
561 if (ret)
562 return ret;
563
564 q_data->fmt = find_format(f);
565 q_data->width = f->fmt.pix.width;
566 q_data->height = f->fmt.pix.height;
567 if (q_data->fmt->fourcc == V4L2_PIX_FMT_YUV420)
568 q_data->sizeimage = q_data->width * q_data->height * 3 / 2;
569 else /* YUYV */
570 q_data->sizeimage = q_data->width * q_data->height * 2;
571
572 dprintk(ctx->dev,
573 "Setting format for type %d, wxh: %dx%d, fmt: %d\n",
574 f->type, q_data->width, q_data->height, q_data->fmt->fourcc);
575
576 return 0;
577}
578
579static int vidioc_s_fmt_vid_cap(struct file *file, void *priv,
580 struct v4l2_format *f)
581{
582 int ret;
583
584 ret = vidioc_try_fmt_vid_cap(file, priv, f);
585 if (ret)
586 return ret;
587
588 return vidioc_s_fmt(priv, f);
589}
590
591static int vidioc_s_fmt_vid_out(struct file *file, void *priv,
592 struct v4l2_format *f)
593{
594 int ret;
595
596 ret = vidioc_try_fmt_vid_out(file, priv, f);
597 if (ret)
598 return ret;
599
600 return vidioc_s_fmt(priv, f);
601}
602
603static int vidioc_reqbufs(struct file *file, void *priv,
604 struct v4l2_requestbuffers *reqbufs)
605{
606 struct emmaprp_ctx *ctx = priv;
607
608 return v4l2_m2m_reqbufs(file, ctx->m2m_ctx, reqbufs);
609}
610
611static int vidioc_querybuf(struct file *file, void *priv,
612 struct v4l2_buffer *buf)
613{
614 struct emmaprp_ctx *ctx = priv;
615
616 return v4l2_m2m_querybuf(file, ctx->m2m_ctx, buf);
617}
618
619static int vidioc_qbuf(struct file *file, void *priv, struct v4l2_buffer *buf)
620{
621 struct emmaprp_ctx *ctx = priv;
622
623 return v4l2_m2m_qbuf(file, ctx->m2m_ctx, buf);
624}
625
626static int vidioc_dqbuf(struct file *file, void *priv, struct v4l2_buffer *buf)
627{
628 struct emmaprp_ctx *ctx = priv;
629
630 return v4l2_m2m_dqbuf(file, ctx->m2m_ctx, buf);
631}
632
633static int vidioc_streamon(struct file *file, void *priv,
634 enum v4l2_buf_type type)
635{
636 struct emmaprp_ctx *ctx = priv;
637
638 return v4l2_m2m_streamon(file, ctx->m2m_ctx, type);
639}
640
641static int vidioc_streamoff(struct file *file, void *priv,
642 enum v4l2_buf_type type)
643{
644 struct emmaprp_ctx *ctx = priv;
645
646 return v4l2_m2m_streamoff(file, ctx->m2m_ctx, type);
647}
648
649static const struct v4l2_ioctl_ops emmaprp_ioctl_ops = {
650 .vidioc_querycap = vidioc_querycap,
651
652 .vidioc_enum_fmt_vid_cap = vidioc_enum_fmt_vid_cap,
653 .vidioc_g_fmt_vid_cap = vidioc_g_fmt_vid_cap,
654 .vidioc_try_fmt_vid_cap = vidioc_try_fmt_vid_cap,
655 .vidioc_s_fmt_vid_cap = vidioc_s_fmt_vid_cap,
656
657 .vidioc_enum_fmt_vid_out = vidioc_enum_fmt_vid_out,
658 .vidioc_g_fmt_vid_out = vidioc_g_fmt_vid_out,
659 .vidioc_try_fmt_vid_out = vidioc_try_fmt_vid_out,
660 .vidioc_s_fmt_vid_out = vidioc_s_fmt_vid_out,
661
662 .vidioc_reqbufs = vidioc_reqbufs,
663 .vidioc_querybuf = vidioc_querybuf,
664
665 .vidioc_qbuf = vidioc_qbuf,
666 .vidioc_dqbuf = vidioc_dqbuf,
667
668 .vidioc_streamon = vidioc_streamon,
669 .vidioc_streamoff = vidioc_streamoff,
670};
671
672
673/*
674 * Queue operations
675 */
676static int emmaprp_queue_setup(struct vb2_queue *vq,
8091cb7d 677 unsigned int *nbuffers, unsigned int *nplanes,
36c0f8b3 678 unsigned int sizes[], struct device *alloc_devs[])
8091cb7d
JM
679{
680 struct emmaprp_ctx *ctx = vb2_get_drv_priv(vq);
681 struct emmaprp_q_data *q_data;
682 unsigned int size, count = *nbuffers;
683
684 q_data = get_q_data(ctx, vq->type);
685
686 if (q_data->fmt->fourcc == V4L2_PIX_FMT_YUV420)
687 size = q_data->width * q_data->height * 3 / 2;
688 else
689 size = q_data->width * q_data->height * 2;
690
691 while (size * count > MEM2MEM_VID_MEM_LIMIT)
692 (count)--;
693
694 *nplanes = 1;
695 *nbuffers = count;
696 sizes[0] = size;
697
8091cb7d
JM
698 dprintk(ctx->dev, "get %d buffer(s) of size %d each.\n", count, size);
699
700 return 0;
701}
702
703static int emmaprp_buf_prepare(struct vb2_buffer *vb)
704{
705 struct emmaprp_ctx *ctx = vb2_get_drv_priv(vb->vb2_queue);
706 struct emmaprp_q_data *q_data;
707
708 dprintk(ctx->dev, "type: %d\n", vb->vb2_queue->type);
709
710 q_data = get_q_data(ctx, vb->vb2_queue->type);
711
712 if (vb2_plane_size(vb, 0) < q_data->sizeimage) {
759a4ed4
MCC
713 dprintk(ctx->dev,
714 "%s data will not fit into plane(%lu < %lu)\n",
715 __func__, vb2_plane_size(vb, 0),
716 (long)q_data->sizeimage);
8091cb7d
JM
717 return -EINVAL;
718 }
719
720 vb2_set_plane_payload(vb, 0, q_data->sizeimage);
721
722 return 0;
723}
724
725static void emmaprp_buf_queue(struct vb2_buffer *vb)
726{
2d700715 727 struct vb2_v4l2_buffer *vbuf = to_vb2_v4l2_buffer(vb);
8091cb7d 728 struct emmaprp_ctx *ctx = vb2_get_drv_priv(vb->vb2_queue);
2d700715 729 v4l2_m2m_buf_queue(ctx->m2m_ctx, vbuf);
8091cb7d
JM
730}
731
b7b361f0 732static const struct vb2_ops emmaprp_qops = {
8091cb7d
JM
733 .queue_setup = emmaprp_queue_setup,
734 .buf_prepare = emmaprp_buf_prepare,
735 .buf_queue = emmaprp_buf_queue,
a4273abc
EG
736 .wait_prepare = vb2_ops_wait_prepare,
737 .wait_finish = vb2_ops_wait_finish,
8091cb7d
JM
738};
739
740static int queue_init(void *priv, struct vb2_queue *src_vq,
741 struct vb2_queue *dst_vq)
742{
743 struct emmaprp_ctx *ctx = priv;
744 int ret;
745
8091cb7d 746 src_vq->type = V4L2_BUF_TYPE_VIDEO_OUTPUT;
28638601 747 src_vq->io_modes = VB2_MMAP | VB2_USERPTR;
8091cb7d
JM
748 src_vq->drv_priv = ctx;
749 src_vq->buf_struct_size = sizeof(struct v4l2_m2m_buffer);
750 src_vq->ops = &emmaprp_qops;
751 src_vq->mem_ops = &vb2_dma_contig_memops;
ade48681 752 src_vq->timestamp_flags = V4L2_BUF_FLAG_TIMESTAMP_COPY;
1ad70ced 753 src_vq->dev = ctx->dev->v4l2_dev.dev;
a4273abc 754 src_vq->lock = &ctx->dev->dev_mutex;
8091cb7d
JM
755
756 ret = vb2_queue_init(src_vq);
757 if (ret)
758 return ret;
759
8091cb7d 760 dst_vq->type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
28638601 761 dst_vq->io_modes = VB2_MMAP | VB2_USERPTR;
8091cb7d
JM
762 dst_vq->drv_priv = ctx;
763 dst_vq->buf_struct_size = sizeof(struct v4l2_m2m_buffer);
764 dst_vq->ops = &emmaprp_qops;
765 dst_vq->mem_ops = &vb2_dma_contig_memops;
ade48681 766 dst_vq->timestamp_flags = V4L2_BUF_FLAG_TIMESTAMP_COPY;
1ad70ced 767 dst_vq->dev = ctx->dev->v4l2_dev.dev;
a4273abc 768 dst_vq->lock = &ctx->dev->dev_mutex;
8091cb7d
JM
769
770 return vb2_queue_init(dst_vq);
771}
772
773/*
774 * File operations
775 */
776static int emmaprp_open(struct file *file)
777{
778 struct emmaprp_dev *pcdev = video_drvdata(file);
779 struct emmaprp_ctx *ctx;
780
781 ctx = kzalloc(sizeof *ctx, GFP_KERNEL);
782 if (!ctx)
783 return -ENOMEM;
784
785 file->private_data = ctx;
786 ctx->dev = pcdev;
787
954f5bcb
HV
788 if (mutex_lock_interruptible(&pcdev->dev_mutex)) {
789 kfree(ctx);
790 return -ERESTARTSYS;
791 }
792
8091cb7d
JM
793 ctx->m2m_ctx = v4l2_m2m_ctx_init(pcdev->m2m_dev, ctx, &queue_init);
794
795 if (IS_ERR(ctx->m2m_ctx)) {
796 int ret = PTR_ERR(ctx->m2m_ctx);
797
954f5bcb 798 mutex_unlock(&pcdev->dev_mutex);
8091cb7d
JM
799 kfree(ctx);
800 return ret;
801 }
802
33eb46a7
JM
803 clk_prepare_enable(pcdev->clk_emma_ipg);
804 clk_prepare_enable(pcdev->clk_emma_ahb);
8091cb7d
JM
805 ctx->q_data[V4L2_M2M_SRC].fmt = &formats[1];
806 ctx->q_data[V4L2_M2M_DST].fmt = &formats[0];
954f5bcb 807 mutex_unlock(&pcdev->dev_mutex);
8091cb7d
JM
808
809 dprintk(pcdev, "Created instance %p, m2m_ctx: %p\n", ctx, ctx->m2m_ctx);
810
811 return 0;
812}
813
814static int emmaprp_release(struct file *file)
815{
816 struct emmaprp_dev *pcdev = video_drvdata(file);
817 struct emmaprp_ctx *ctx = file->private_data;
818
819 dprintk(pcdev, "Releasing instance %p\n", ctx);
820
954f5bcb 821 mutex_lock(&pcdev->dev_mutex);
33eb46a7
JM
822 clk_disable_unprepare(pcdev->clk_emma_ahb);
823 clk_disable_unprepare(pcdev->clk_emma_ipg);
8091cb7d 824 v4l2_m2m_ctx_release(ctx->m2m_ctx);
954f5bcb 825 mutex_unlock(&pcdev->dev_mutex);
8091cb7d
JM
826 kfree(ctx);
827
828 return 0;
829}
830
c23e0cb8 831static __poll_t emmaprp_poll(struct file *file,
8091cb7d
JM
832 struct poll_table_struct *wait)
833{
954f5bcb 834 struct emmaprp_dev *pcdev = video_drvdata(file);
8091cb7d 835 struct emmaprp_ctx *ctx = file->private_data;
c23e0cb8 836 __poll_t res;
8091cb7d 837
954f5bcb
HV
838 mutex_lock(&pcdev->dev_mutex);
839 res = v4l2_m2m_poll(file, ctx->m2m_ctx, wait);
840 mutex_unlock(&pcdev->dev_mutex);
841 return res;
8091cb7d
JM
842}
843
844static int emmaprp_mmap(struct file *file, struct vm_area_struct *vma)
845{
954f5bcb 846 struct emmaprp_dev *pcdev = video_drvdata(file);
8091cb7d 847 struct emmaprp_ctx *ctx = file->private_data;
954f5bcb 848 int ret;
8091cb7d 849
954f5bcb
HV
850 if (mutex_lock_interruptible(&pcdev->dev_mutex))
851 return -ERESTARTSYS;
852 ret = v4l2_m2m_mmap(file, ctx->m2m_ctx, vma);
853 mutex_unlock(&pcdev->dev_mutex);
854 return ret;
8091cb7d
JM
855}
856
857static const struct v4l2_file_operations emmaprp_fops = {
858 .owner = THIS_MODULE,
859 .open = emmaprp_open,
860 .release = emmaprp_release,
861 .poll = emmaprp_poll,
862 .unlocked_ioctl = video_ioctl2,
863 .mmap = emmaprp_mmap,
864};
865
5303135c 866static const struct video_device emmaprp_videodev = {
8091cb7d
JM
867 .name = MEM2MEM_NAME,
868 .fops = &emmaprp_fops,
869 .ioctl_ops = &emmaprp_ioctl_ops,
870 .minor = -1,
871 .release = video_device_release,
954f340f 872 .vfl_dir = VFL_DIR_M2M,
8091cb7d
JM
873};
874
c3f6ddfe 875static const struct v4l2_m2m_ops m2m_ops = {
8091cb7d
JM
876 .device_run = emmaprp_device_run,
877 .job_abort = emmaprp_job_abort,
8091cb7d
JM
878};
879
880static int emmaprp_probe(struct platform_device *pdev)
881{
882 struct emmaprp_dev *pcdev;
883 struct video_device *vfd;
b97a6216
AS
884 struct resource *res;
885 int irq, ret;
8091cb7d 886
d1bb4b29 887 pcdev = devm_kzalloc(&pdev->dev, sizeof(*pcdev), GFP_KERNEL);
8091cb7d
JM
888 if (!pcdev)
889 return -ENOMEM;
890
891 spin_lock_init(&pcdev->irqlock);
892
33eb46a7
JM
893 pcdev->clk_emma_ipg = devm_clk_get(&pdev->dev, "ipg");
894 if (IS_ERR(pcdev->clk_emma_ipg)) {
d1bb4b29 895 return PTR_ERR(pcdev->clk_emma_ipg);
33eb46a7
JM
896 }
897
898 pcdev->clk_emma_ahb = devm_clk_get(&pdev->dev, "ahb");
9f9831a8 899 if (IS_ERR(pcdev->clk_emma_ahb))
d1bb4b29 900 return PTR_ERR(pcdev->clk_emma_ahb);
8091cb7d 901
b97a6216
AS
902 res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
903 pcdev->base_emma = devm_ioremap_resource(&pdev->dev, res);
904 if (IS_ERR(pcdev->base_emma))
905 return PTR_ERR(pcdev->base_emma);
8091cb7d
JM
906
907 ret = v4l2_device_register(&pdev->dev, &pcdev->v4l2_dev);
908 if (ret)
d1bb4b29 909 return ret;
8091cb7d
JM
910
911 mutex_init(&pcdev->dev_mutex);
912
913 vfd = video_device_alloc();
914 if (!vfd) {
915 v4l2_err(&pcdev->v4l2_dev, "Failed to allocate video device\n");
916 ret = -ENOMEM;
917 goto unreg_dev;
918 }
919
920 *vfd = emmaprp_videodev;
921 vfd->lock = &pcdev->dev_mutex;
8f484d87 922 vfd->v4l2_dev = &pcdev->v4l2_dev;
8091cb7d
JM
923
924 video_set_drvdata(vfd, pcdev);
8091cb7d
JM
925 pcdev->vfd = vfd;
926 v4l2_info(&pcdev->v4l2_dev, EMMAPRP_MODULE_NAME
759a4ed4 927 " Device registered as /dev/video%d\n", vfd->num);
8091cb7d
JM
928
929 platform_set_drvdata(pdev, pcdev);
930
b97a6216 931 irq = platform_get_irq(pdev, 0);
d4e192cc
FE
932 if (irq < 0)
933 return irq;
b97a6216
AS
934 ret = devm_request_irq(&pdev->dev, irq, emmaprp_irq, 0,
935 dev_name(&pdev->dev), pcdev);
936 if (ret)
8091cb7d
JM
937 goto rel_vdev;
938
8091cb7d
JM
939 pcdev->m2m_dev = v4l2_m2m_init(&m2m_ops);
940 if (IS_ERR(pcdev->m2m_dev)) {
941 v4l2_err(&pcdev->v4l2_dev, "Failed to init mem2mem device\n");
942 ret = PTR_ERR(pcdev->m2m_dev);
1ad70ced 943 goto rel_vdev;
8091cb7d
JM
944 }
945
946 ret = video_register_device(vfd, VFL_TYPE_GRABBER, 0);
947 if (ret) {
948 v4l2_err(&pcdev->v4l2_dev, "Failed to register video device\n");
949 goto rel_m2m;
950 }
951
952 return 0;
953
954
955rel_m2m:
956 v4l2_m2m_release(pcdev->m2m_dev);
8091cb7d
JM
957rel_vdev:
958 video_device_release(vfd);
959unreg_dev:
960 v4l2_device_unregister(&pcdev->v4l2_dev);
8091cb7d 961
d98f1b78
AS
962 mutex_destroy(&pcdev->dev_mutex);
963
8091cb7d
JM
964 return ret;
965}
966
967static int emmaprp_remove(struct platform_device *pdev)
968{
969 struct emmaprp_dev *pcdev = platform_get_drvdata(pdev);
970
971 v4l2_info(&pcdev->v4l2_dev, "Removing " EMMAPRP_MODULE_NAME);
972
973 video_unregister_device(pcdev->vfd);
974 v4l2_m2m_release(pcdev->m2m_dev);
8091cb7d 975 v4l2_device_unregister(&pcdev->v4l2_dev);
d98f1b78 976 mutex_destroy(&pcdev->dev_mutex);
8091cb7d
JM
977
978 return 0;
979}
980
981static struct platform_driver emmaprp_pdrv = {
982 .probe = emmaprp_probe,
983 .remove = emmaprp_remove,
984 .driver = {
985 .name = MEM2MEM_NAME,
8091cb7d
JM
986 },
987};
0e368f30 988module_platform_driver(emmaprp_pdrv);