xref: /linux/drivers/media/platform/m2m-deinterlace.c (revision 6ed7ffddcf61f668114edb676417e5fb33773b59)
1 /*
2  * V4L2 deinterlacing support.
3  *
4  * Copyright (c) 2012 Vista Silicon S.L.
5  * Javier Martin <javier.martin@vista-silicon.com>
6  *
7  * This program is free software; you can redistribute it and/or modify
8  * it under the terms of the GNU General Public License as published by the
9  * Free Software Foundation; either version 2 of the
10  * License, or (at your option) any later version
11  */
12 
13 #include <linux/module.h>
14 #include <linux/slab.h>
15 #include <linux/interrupt.h>
16 #include <linux/dmaengine.h>
17 #include <linux/platform_device.h>
18 
19 #include <media/v4l2-mem2mem.h>
20 #include <media/v4l2-device.h>
21 #include <media/v4l2-ioctl.h>
22 #include <media/videobuf2-dma-contig.h>
23 
24 #define MEM2MEM_TEST_MODULE_NAME "mem2mem-deinterlace"
25 
26 MODULE_DESCRIPTION("mem2mem device which supports deinterlacing using dmaengine");
27 MODULE_AUTHOR("Javier Martin <javier.martin@vista-silicon.com");
28 MODULE_LICENSE("GPL");
29 MODULE_VERSION("0.0.1");
30 
31 static bool debug;
32 module_param(debug, bool, 0644);
33 
34 /* Flags that indicate a format can be used for capture/output */
35 #define MEM2MEM_CAPTURE	(1 << 0)
36 #define MEM2MEM_OUTPUT	(1 << 1)
37 
38 #define MEM2MEM_NAME		"m2m-deinterlace"
39 
40 #define dprintk(dev, fmt, arg...) \
41 	v4l2_dbg(1, debug, &dev->v4l2_dev, "%s: " fmt, __func__, ## arg)
42 
43 struct deinterlace_fmt {
44 	char	*name;
45 	u32	fourcc;
46 	/* Types the format can be used for */
47 	u32	types;
48 };
49 
50 static struct deinterlace_fmt formats[] = {
51 	{
52 		.name	= "YUV 4:2:0 Planar",
53 		.fourcc	= V4L2_PIX_FMT_YUV420,
54 		.types	= MEM2MEM_CAPTURE | MEM2MEM_OUTPUT,
55 	},
56 	{
57 		.name	= "YUYV 4:2:2",
58 		.fourcc	= V4L2_PIX_FMT_YUYV,
59 		.types	= MEM2MEM_CAPTURE | MEM2MEM_OUTPUT,
60 	},
61 };
62 
63 #define NUM_FORMATS ARRAY_SIZE(formats)
64 
65 /* Per-queue, driver-specific private data */
66 struct deinterlace_q_data {
67 	unsigned int		width;
68 	unsigned int		height;
69 	unsigned int		sizeimage;
70 	struct deinterlace_fmt	*fmt;
71 	enum v4l2_field		field;
72 };
73 
74 enum {
75 	V4L2_M2M_SRC = 0,
76 	V4L2_M2M_DST = 1,
77 };
78 
79 enum {
80 	YUV420_DMA_Y_ODD,
81 	YUV420_DMA_Y_EVEN,
82 	YUV420_DMA_U_ODD,
83 	YUV420_DMA_U_EVEN,
84 	YUV420_DMA_V_ODD,
85 	YUV420_DMA_V_EVEN,
86 	YUV420_DMA_Y_ODD_DOUBLING,
87 	YUV420_DMA_U_ODD_DOUBLING,
88 	YUV420_DMA_V_ODD_DOUBLING,
89 	YUYV_DMA_ODD,
90 	YUYV_DMA_EVEN,
91 	YUYV_DMA_EVEN_DOUBLING,
92 };
93 
94 /* Source and destination queue data */
95 static struct deinterlace_q_data q_data[2];
96 
97 static struct deinterlace_q_data *get_q_data(enum v4l2_buf_type type)
98 {
99 	switch (type) {
100 	case V4L2_BUF_TYPE_VIDEO_OUTPUT:
101 		return &q_data[V4L2_M2M_SRC];
102 	case V4L2_BUF_TYPE_VIDEO_CAPTURE:
103 		return &q_data[V4L2_M2M_DST];
104 	default:
105 		BUG();
106 	}
107 	return NULL;
108 }
109 
110 static struct deinterlace_fmt *find_format(struct v4l2_format *f)
111 {
112 	struct deinterlace_fmt *fmt;
113 	unsigned int k;
114 
115 	for (k = 0; k < NUM_FORMATS; k++) {
116 		fmt = &formats[k];
117 		if ((fmt->types & f->type) &&
118 			(fmt->fourcc == f->fmt.pix.pixelformat))
119 			break;
120 	}
121 
122 	if (k == NUM_FORMATS)
123 		return NULL;
124 
125 	return &formats[k];
126 }
127 
128 struct deinterlace_dev {
129 	struct v4l2_device	v4l2_dev;
130 	struct video_device	*vfd;
131 
132 	atomic_t		busy;
133 	struct mutex		dev_mutex;
134 	spinlock_t		irqlock;
135 
136 	struct dma_chan		*dma_chan;
137 
138 	struct v4l2_m2m_dev	*m2m_dev;
139 	struct vb2_alloc_ctx	*alloc_ctx;
140 };
141 
142 struct deinterlace_ctx {
143 	struct deinterlace_dev	*dev;
144 
145 	/* Abort requested by m2m */
146 	int			aborting;
147 	enum v4l2_colorspace	colorspace;
148 	dma_cookie_t		cookie;
149 	struct v4l2_m2m_ctx	*m2m_ctx;
150 	struct dma_interleaved_template *xt;
151 };
152 
153 /*
154  * mem2mem callbacks
155  */
156 static int deinterlace_job_ready(void *priv)
157 {
158 	struct deinterlace_ctx *ctx = priv;
159 	struct deinterlace_dev *pcdev = ctx->dev;
160 
161 	if ((v4l2_m2m_num_src_bufs_ready(ctx->m2m_ctx) > 0)
162 	    && (v4l2_m2m_num_dst_bufs_ready(ctx->m2m_ctx) > 0)
163 	    && (atomic_read(&ctx->dev->busy) == 0)) {
164 		dprintk(pcdev, "Task ready\n");
165 		return 1;
166 	}
167 
168 	dprintk(pcdev, "Task not ready to run\n");
169 
170 	return 0;
171 }
172 
173 static void deinterlace_job_abort(void *priv)
174 {
175 	struct deinterlace_ctx *ctx = priv;
176 	struct deinterlace_dev *pcdev = ctx->dev;
177 
178 	ctx->aborting = 1;
179 
180 	dprintk(pcdev, "Aborting task\n");
181 
182 	v4l2_m2m_job_finish(pcdev->m2m_dev, ctx->m2m_ctx);
183 }
184 
185 static void deinterlace_lock(void *priv)
186 {
187 	struct deinterlace_ctx *ctx = priv;
188 	struct deinterlace_dev *pcdev = ctx->dev;
189 	mutex_lock(&pcdev->dev_mutex);
190 }
191 
192 static void deinterlace_unlock(void *priv)
193 {
194 	struct deinterlace_ctx *ctx = priv;
195 	struct deinterlace_dev *pcdev = ctx->dev;
196 	mutex_unlock(&pcdev->dev_mutex);
197 }
198 
199 static void dma_callback(void *data)
200 {
201 	struct deinterlace_ctx *curr_ctx = data;
202 	struct deinterlace_dev *pcdev = curr_ctx->dev;
203 	struct vb2_buffer *src_vb, *dst_vb;
204 
205 	atomic_set(&pcdev->busy, 0);
206 
207 	src_vb = v4l2_m2m_src_buf_remove(curr_ctx->m2m_ctx);
208 	dst_vb = v4l2_m2m_dst_buf_remove(curr_ctx->m2m_ctx);
209 
210 	v4l2_m2m_buf_done(src_vb, VB2_BUF_STATE_DONE);
211 	v4l2_m2m_buf_done(dst_vb, VB2_BUF_STATE_DONE);
212 
213 	v4l2_m2m_job_finish(pcdev->m2m_dev, curr_ctx->m2m_ctx);
214 
215 	dprintk(pcdev, "dma transfers completed.\n");
216 }
217 
218 static void deinterlace_issue_dma(struct deinterlace_ctx *ctx, int op,
219 				  int do_callback)
220 {
221 	struct deinterlace_q_data *s_q_data;
222 	struct vb2_buffer *src_buf, *dst_buf;
223 	struct deinterlace_dev *pcdev = ctx->dev;
224 	struct dma_chan *chan = pcdev->dma_chan;
225 	struct dma_device *dmadev = chan->device;
226 	struct dma_async_tx_descriptor *tx;
227 	unsigned int s_width, s_height;
228 	unsigned int s_size;
229 	dma_addr_t p_in, p_out;
230 	enum dma_ctrl_flags flags;
231 
232 	src_buf = v4l2_m2m_next_src_buf(ctx->m2m_ctx);
233 	dst_buf = v4l2_m2m_next_dst_buf(ctx->m2m_ctx);
234 
235 	s_q_data = get_q_data(V4L2_BUF_TYPE_VIDEO_OUTPUT);
236 	s_width	= s_q_data->width;
237 	s_height = s_q_data->height;
238 	s_size = s_width * s_height;
239 
240 	p_in = (dma_addr_t)vb2_dma_contig_plane_dma_addr(src_buf, 0);
241 	p_out = (dma_addr_t)vb2_dma_contig_plane_dma_addr(dst_buf, 0);
242 	if (!p_in || !p_out) {
243 		v4l2_err(&pcdev->v4l2_dev,
244 			 "Acquiring kernel pointers to buffers failed\n");
245 		return;
246 	}
247 
248 	switch (op) {
249 	case YUV420_DMA_Y_ODD:
250 		ctx->xt->numf = s_height / 2;
251 		ctx->xt->sgl[0].size = s_width;
252 		ctx->xt->sgl[0].icg = s_width;
253 		ctx->xt->src_start = p_in;
254 		ctx->xt->dst_start = p_out;
255 		break;
256 	case YUV420_DMA_Y_EVEN:
257 		ctx->xt->numf = s_height / 2;
258 		ctx->xt->sgl[0].size = s_width;
259 		ctx->xt->sgl[0].icg = s_width;
260 		ctx->xt->src_start = p_in + s_size / 2;
261 		ctx->xt->dst_start = p_out + s_width;
262 		break;
263 	case YUV420_DMA_U_ODD:
264 		ctx->xt->numf = s_height / 4;
265 		ctx->xt->sgl[0].size = s_width / 2;
266 		ctx->xt->sgl[0].icg = s_width / 2;
267 		ctx->xt->src_start = p_in + s_size;
268 		ctx->xt->dst_start = p_out + s_size;
269 		break;
270 	case YUV420_DMA_U_EVEN:
271 		ctx->xt->numf = s_height / 4;
272 		ctx->xt->sgl[0].size = s_width / 2;
273 		ctx->xt->sgl[0].icg = s_width / 2;
274 		ctx->xt->src_start = p_in + (9 * s_size) / 8;
275 		ctx->xt->dst_start = p_out + s_size + s_width / 2;
276 		break;
277 	case YUV420_DMA_V_ODD:
278 		ctx->xt->numf = s_height / 4;
279 		ctx->xt->sgl[0].size = s_width / 2;
280 		ctx->xt->sgl[0].icg = s_width / 2;
281 		ctx->xt->src_start = p_in + (5 * s_size) / 4;
282 		ctx->xt->dst_start = p_out + (5 * s_size) / 4;
283 		break;
284 	case YUV420_DMA_V_EVEN:
285 		ctx->xt->numf = s_height / 4;
286 		ctx->xt->sgl[0].size = s_width / 2;
287 		ctx->xt->sgl[0].icg = s_width / 2;
288 		ctx->xt->src_start = p_in + (11 * s_size) / 8;
289 		ctx->xt->dst_start = p_out + (5 * s_size) / 4 + s_width / 2;
290 		break;
291 	case YUV420_DMA_Y_ODD_DOUBLING:
292 		ctx->xt->numf = s_height / 2;
293 		ctx->xt->sgl[0].size = s_width;
294 		ctx->xt->sgl[0].icg = s_width;
295 		ctx->xt->src_start = p_in;
296 		ctx->xt->dst_start = p_out + s_width;
297 		break;
298 	case YUV420_DMA_U_ODD_DOUBLING:
299 		ctx->xt->numf = s_height / 4;
300 		ctx->xt->sgl[0].size = s_width / 2;
301 		ctx->xt->sgl[0].icg = s_width / 2;
302 		ctx->xt->src_start = p_in + s_size;
303 		ctx->xt->dst_start = p_out + s_size + s_width / 2;
304 		break;
305 	case YUV420_DMA_V_ODD_DOUBLING:
306 		ctx->xt->numf = s_height / 4;
307 		ctx->xt->sgl[0].size = s_width / 2;
308 		ctx->xt->sgl[0].icg = s_width / 2;
309 		ctx->xt->src_start = p_in + (5 * s_size) / 4;
310 		ctx->xt->dst_start = p_out + (5 * s_size) / 4 + s_width / 2;
311 		break;
312 	case YUYV_DMA_ODD:
313 		ctx->xt->numf = s_height / 2;
314 		ctx->xt->sgl[0].size = s_width * 2;
315 		ctx->xt->sgl[0].icg = s_width * 2;
316 		ctx->xt->src_start = p_in;
317 		ctx->xt->dst_start = p_out;
318 		break;
319 	case YUYV_DMA_EVEN:
320 		ctx->xt->numf = s_height / 2;
321 		ctx->xt->sgl[0].size = s_width * 2;
322 		ctx->xt->sgl[0].icg = s_width * 2;
323 		ctx->xt->src_start = p_in + s_size;
324 		ctx->xt->dst_start = p_out + s_width * 2;
325 		break;
326 	case YUYV_DMA_EVEN_DOUBLING:
327 	default:
328 		ctx->xt->numf = s_height / 2;
329 		ctx->xt->sgl[0].size = s_width * 2;
330 		ctx->xt->sgl[0].icg = s_width * 2;
331 		ctx->xt->src_start = p_in;
332 		ctx->xt->dst_start = p_out + s_width * 2;
333 		break;
334 	}
335 
336 	/* Common parameters for al transfers */
337 	ctx->xt->frame_size = 1;
338 	ctx->xt->dir = DMA_MEM_TO_MEM;
339 	ctx->xt->src_sgl = false;
340 	ctx->xt->dst_sgl = true;
341 	flags = DMA_CTRL_ACK | DMA_PREP_INTERRUPT |
342 		DMA_COMPL_SKIP_DEST_UNMAP | DMA_COMPL_SKIP_SRC_UNMAP;
343 
344 	tx = dmadev->device_prep_interleaved_dma(chan, ctx->xt, flags);
345 	if (tx == NULL) {
346 		v4l2_warn(&pcdev->v4l2_dev, "DMA interleaved prep error\n");
347 		return;
348 	}
349 
350 	if (do_callback) {
351 		tx->callback = dma_callback;
352 		tx->callback_param = ctx;
353 	}
354 
355 	ctx->cookie = dmaengine_submit(tx);
356 	if (dma_submit_error(ctx->cookie)) {
357 		v4l2_warn(&pcdev->v4l2_dev,
358 			  "DMA submit error %d with src=0x%x dst=0x%x len=0x%x\n",
359 			  ctx->cookie, (unsigned)p_in, (unsigned)p_out,
360 			  s_size * 3/2);
361 		return;
362 	}
363 
364 	dma_async_issue_pending(chan);
365 }
366 
367 static void deinterlace_device_run(void *priv)
368 {
369 	struct deinterlace_ctx *ctx = priv;
370 	struct deinterlace_q_data *dst_q_data;
371 
372 	atomic_set(&ctx->dev->busy, 1);
373 
374 	dprintk(ctx->dev, "%s: DMA try issue.\n", __func__);
375 
376 	dst_q_data = get_q_data(V4L2_BUF_TYPE_VIDEO_CAPTURE);
377 
378 	/*
379 	 * 4 possible field conversions are possible at the moment:
380 	 *  V4L2_FIELD_SEQ_TB --> V4L2_FIELD_INTERLACED_TB:
381 	 *	two separate fields in the same input buffer are interlaced
382 	 * 	in the output buffer using weaving. Top field comes first.
383 	 *  V4L2_FIELD_SEQ_TB --> V4L2_FIELD_NONE:
384 	 * 	top field from the input buffer is copied to the output buffer
385 	 * 	using line doubling. Bottom field from the input buffer is discarded.
386 	 * V4L2_FIELD_SEQ_BT --> V4L2_FIELD_INTERLACED_BT:
387 	 *	two separate fields in the same input buffer are interlaced
388 	 * 	in the output buffer using weaving. Bottom field comes first.
389 	 * V4L2_FIELD_SEQ_BT --> V4L2_FIELD_NONE:
390 	 * 	bottom field from the input buffer is copied to the output buffer
391 	 * 	using line doubling. Top field from the input buffer is discarded.
392 	 */
393 	switch (dst_q_data->fmt->fourcc) {
394 	case V4L2_PIX_FMT_YUV420:
395 		switch (dst_q_data->field) {
396 		case V4L2_FIELD_INTERLACED_TB:
397 		case V4L2_FIELD_INTERLACED_BT:
398 			dprintk(ctx->dev, "%s: yuv420 interlaced tb.\n",
399 				__func__);
400 			deinterlace_issue_dma(ctx, YUV420_DMA_Y_ODD, 0);
401 			deinterlace_issue_dma(ctx, YUV420_DMA_Y_EVEN, 0);
402 			deinterlace_issue_dma(ctx, YUV420_DMA_U_ODD, 0);
403 			deinterlace_issue_dma(ctx, YUV420_DMA_U_EVEN, 0);
404 			deinterlace_issue_dma(ctx, YUV420_DMA_V_ODD, 0);
405 			deinterlace_issue_dma(ctx, YUV420_DMA_V_EVEN, 1);
406 			break;
407 		case V4L2_FIELD_NONE:
408 		default:
409 			dprintk(ctx->dev, "%s: yuv420 interlaced line doubling.\n",
410 				__func__);
411 			deinterlace_issue_dma(ctx, YUV420_DMA_Y_ODD, 0);
412 			deinterlace_issue_dma(ctx, YUV420_DMA_Y_ODD_DOUBLING, 0);
413 			deinterlace_issue_dma(ctx, YUV420_DMA_U_ODD, 0);
414 			deinterlace_issue_dma(ctx, YUV420_DMA_U_ODD_DOUBLING, 0);
415 			deinterlace_issue_dma(ctx, YUV420_DMA_V_ODD, 0);
416 			deinterlace_issue_dma(ctx, YUV420_DMA_V_ODD_DOUBLING, 1);
417 			break;
418 		}
419 		break;
420 	case V4L2_PIX_FMT_YUYV:
421 	default:
422 		switch (dst_q_data->field) {
423 		case V4L2_FIELD_INTERLACED_TB:
424 		case V4L2_FIELD_INTERLACED_BT:
425 			dprintk(ctx->dev, "%s: yuyv interlaced_tb.\n",
426 				__func__);
427 			deinterlace_issue_dma(ctx, YUYV_DMA_ODD, 0);
428 			deinterlace_issue_dma(ctx, YUYV_DMA_EVEN, 1);
429 			break;
430 		case V4L2_FIELD_NONE:
431 		default:
432 			dprintk(ctx->dev, "%s: yuyv interlaced line doubling.\n",
433 				__func__);
434 			deinterlace_issue_dma(ctx, YUYV_DMA_ODD, 0);
435 			deinterlace_issue_dma(ctx, YUYV_DMA_EVEN_DOUBLING, 1);
436 			break;
437 		}
438 		break;
439 	}
440 
441 	dprintk(ctx->dev, "%s: DMA issue done.\n", __func__);
442 }
443 
444 /*
445  * video ioctls
446  */
447 static int vidioc_querycap(struct file *file, void *priv,
448 			   struct v4l2_capability *cap)
449 {
450 	strlcpy(cap->driver, MEM2MEM_NAME, sizeof(cap->driver));
451 	strlcpy(cap->card, MEM2MEM_NAME, sizeof(cap->card));
452 	strlcpy(cap->bus_info, MEM2MEM_NAME, sizeof(cap->card));
453 	/*
454 	 * This is only a mem-to-mem video device. The capture and output
455 	 * device capability flags are left only for backward compatibility
456 	 * and are scheduled for removal.
457 	 */
458 	cap->device_caps = V4L2_CAP_VIDEO_CAPTURE | V4L2_CAP_VIDEO_OUTPUT |
459 			   V4L2_CAP_VIDEO_M2M | V4L2_CAP_STREAMING;
460 	cap->capabilities = cap->device_caps | V4L2_CAP_DEVICE_CAPS;
461 
462 	return 0;
463 }
464 
465 static int enum_fmt(struct v4l2_fmtdesc *f, u32 type)
466 {
467 	int i, num;
468 	struct deinterlace_fmt *fmt;
469 
470 	num = 0;
471 
472 	for (i = 0; i < NUM_FORMATS; ++i) {
473 		if (formats[i].types & type) {
474 			/* index-th format of type type found ? */
475 			if (num == f->index)
476 				break;
477 			/* Correct type but haven't reached our index yet,
478 			 * just increment per-type index */
479 			++num;
480 		}
481 	}
482 
483 	if (i < NUM_FORMATS) {
484 		/* Format found */
485 		fmt = &formats[i];
486 		strlcpy(f->description, fmt->name, sizeof(f->description));
487 		f->pixelformat = fmt->fourcc;
488 		return 0;
489 	}
490 
491 	/* Format not found */
492 	return -EINVAL;
493 }
494 
495 static int vidioc_enum_fmt_vid_cap(struct file *file, void *priv,
496 				   struct v4l2_fmtdesc *f)
497 {
498 	return enum_fmt(f, MEM2MEM_CAPTURE);
499 }
500 
501 static int vidioc_enum_fmt_vid_out(struct file *file, void *priv,
502 				   struct v4l2_fmtdesc *f)
503 {
504 	return enum_fmt(f, MEM2MEM_OUTPUT);
505 }
506 
507 static int vidioc_g_fmt(struct deinterlace_ctx *ctx, struct v4l2_format *f)
508 {
509 	struct vb2_queue *vq;
510 	struct deinterlace_q_data *q_data;
511 
512 	vq = v4l2_m2m_get_vq(ctx->m2m_ctx, f->type);
513 	if (!vq)
514 		return -EINVAL;
515 
516 	q_data = get_q_data(f->type);
517 
518 	f->fmt.pix.width	= q_data->width;
519 	f->fmt.pix.height	= q_data->height;
520 	f->fmt.pix.field	= q_data->field;
521 	f->fmt.pix.pixelformat	= q_data->fmt->fourcc;
522 
523 	switch (q_data->fmt->fourcc) {
524 	case V4L2_PIX_FMT_YUV420:
525 		f->fmt.pix.bytesperline = q_data->width * 3 / 2;
526 		break;
527 	case V4L2_PIX_FMT_YUYV:
528 	default:
529 		f->fmt.pix.bytesperline = q_data->width * 2;
530 	}
531 
532 	f->fmt.pix.sizeimage	= q_data->sizeimage;
533 	f->fmt.pix.colorspace	= ctx->colorspace;
534 
535 	return 0;
536 }
537 
538 static int vidioc_g_fmt_vid_out(struct file *file, void *priv,
539 				struct v4l2_format *f)
540 {
541 	return vidioc_g_fmt(priv, f);
542 }
543 
544 static int vidioc_g_fmt_vid_cap(struct file *file, void *priv,
545 				struct v4l2_format *f)
546 {
547 	return vidioc_g_fmt(priv, f);
548 }
549 
550 static int vidioc_try_fmt(struct v4l2_format *f, struct deinterlace_fmt *fmt)
551 {
552 	switch (f->fmt.pix.pixelformat) {
553 	case V4L2_PIX_FMT_YUV420:
554 		f->fmt.pix.bytesperline = f->fmt.pix.width * 3 / 2;
555 		break;
556 	case V4L2_PIX_FMT_YUYV:
557 	default:
558 		f->fmt.pix.bytesperline = f->fmt.pix.width * 2;
559 	}
560 	f->fmt.pix.sizeimage = f->fmt.pix.height * f->fmt.pix.bytesperline;
561 
562 	return 0;
563 }
564 
565 static int vidioc_try_fmt_vid_cap(struct file *file, void *priv,
566 				  struct v4l2_format *f)
567 {
568 	struct deinterlace_fmt *fmt;
569 	struct deinterlace_ctx *ctx = priv;
570 
571 	fmt = find_format(f);
572 	if (!fmt || !(fmt->types & MEM2MEM_CAPTURE))
573 		f->fmt.pix.pixelformat = V4L2_PIX_FMT_YUV420;
574 
575 	f->fmt.pix.colorspace = ctx->colorspace;
576 
577 	if (f->fmt.pix.field != V4L2_FIELD_INTERLACED_TB &&
578 	    f->fmt.pix.field != V4L2_FIELD_INTERLACED_BT &&
579 	    f->fmt.pix.field != V4L2_FIELD_NONE)
580 		f->fmt.pix.field = V4L2_FIELD_INTERLACED_TB;
581 
582 	return vidioc_try_fmt(f, fmt);
583 }
584 
585 static int vidioc_try_fmt_vid_out(struct file *file, void *priv,
586 				  struct v4l2_format *f)
587 {
588 	struct deinterlace_fmt *fmt;
589 
590 	fmt = find_format(f);
591 	if (!fmt || !(fmt->types & MEM2MEM_OUTPUT))
592 		f->fmt.pix.pixelformat = V4L2_PIX_FMT_YUV420;
593 
594 	if (!f->fmt.pix.colorspace)
595 		f->fmt.pix.colorspace = V4L2_COLORSPACE_REC709;
596 
597 	if (f->fmt.pix.field != V4L2_FIELD_SEQ_TB &&
598 	    f->fmt.pix.field != V4L2_FIELD_SEQ_BT)
599 		f->fmt.pix.field = V4L2_FIELD_SEQ_TB;
600 
601 	return vidioc_try_fmt(f, fmt);
602 }
603 
604 static int vidioc_s_fmt(struct deinterlace_ctx *ctx, struct v4l2_format *f)
605 {
606 	struct deinterlace_q_data *q_data;
607 	struct vb2_queue *vq;
608 
609 	vq = v4l2_m2m_get_vq(ctx->m2m_ctx, f->type);
610 	if (!vq)
611 		return -EINVAL;
612 
613 	q_data = get_q_data(f->type);
614 	if (!q_data)
615 		return -EINVAL;
616 
617 	if (vb2_is_busy(vq)) {
618 		v4l2_err(&ctx->dev->v4l2_dev, "%s queue busy\n", __func__);
619 		return -EBUSY;
620 	}
621 
622 	q_data->fmt = find_format(f);
623 	if (!q_data->fmt) {
624 		v4l2_err(&ctx->dev->v4l2_dev,
625 			 "Couldn't set format type %d, wxh: %dx%d. fmt: %d, field: %d\n",
626 			f->type, f->fmt.pix.width, f->fmt.pix.height,
627 			f->fmt.pix.pixelformat, f->fmt.pix.field);
628 		return -EINVAL;
629 	}
630 
631 	q_data->width		= f->fmt.pix.width;
632 	q_data->height		= f->fmt.pix.height;
633 	q_data->field		= f->fmt.pix.field;
634 
635 	switch (f->fmt.pix.pixelformat) {
636 	case V4L2_PIX_FMT_YUV420:
637 		f->fmt.pix.bytesperline = f->fmt.pix.width * 3 / 2;
638 		q_data->sizeimage = (q_data->width * q_data->height * 3) / 2;
639 		break;
640 	case V4L2_PIX_FMT_YUYV:
641 	default:
642 		f->fmt.pix.bytesperline = f->fmt.pix.width * 2;
643 		q_data->sizeimage = q_data->width * q_data->height * 2;
644 	}
645 
646 	dprintk(ctx->dev,
647 		"Setting format for type %d, wxh: %dx%d, fmt: %d, field: %d\n",
648 		f->type, q_data->width, q_data->height, q_data->fmt->fourcc,
649 		q_data->field);
650 
651 	return 0;
652 }
653 
654 static int vidioc_s_fmt_vid_cap(struct file *file, void *priv,
655 				struct v4l2_format *f)
656 {
657 	int ret;
658 
659 	ret = vidioc_try_fmt_vid_cap(file, priv, f);
660 	if (ret)
661 		return ret;
662 	return vidioc_s_fmt(priv, f);
663 }
664 
665 static int vidioc_s_fmt_vid_out(struct file *file, void *priv,
666 				struct v4l2_format *f)
667 {
668 	struct deinterlace_ctx *ctx = priv;
669 	int ret;
670 
671 	ret = vidioc_try_fmt_vid_out(file, priv, f);
672 	if (ret)
673 		return ret;
674 
675 	ret = vidioc_s_fmt(priv, f);
676 	if (!ret)
677 		ctx->colorspace = f->fmt.pix.colorspace;
678 
679 	return ret;
680 }
681 
682 static int vidioc_reqbufs(struct file *file, void *priv,
683 			  struct v4l2_requestbuffers *reqbufs)
684 {
685 	struct deinterlace_ctx *ctx = priv;
686 
687 	return v4l2_m2m_reqbufs(file, ctx->m2m_ctx, reqbufs);
688 }
689 
690 static int vidioc_querybuf(struct file *file, void *priv,
691 			   struct v4l2_buffer *buf)
692 {
693 	struct deinterlace_ctx *ctx = priv;
694 
695 	return v4l2_m2m_querybuf(file, ctx->m2m_ctx, buf);
696 }
697 
698 static int vidioc_qbuf(struct file *file, void *priv, struct v4l2_buffer *buf)
699 {
700 	struct deinterlace_ctx *ctx = priv;
701 
702 	return v4l2_m2m_qbuf(file, ctx->m2m_ctx, buf);
703 }
704 
705 static int vidioc_dqbuf(struct file *file, void *priv, struct v4l2_buffer *buf)
706 {
707 	struct deinterlace_ctx *ctx = priv;
708 
709 	return v4l2_m2m_dqbuf(file, ctx->m2m_ctx, buf);
710 }
711 
712 static int vidioc_streamon(struct file *file, void *priv,
713 			   enum v4l2_buf_type type)
714 {
715 	struct deinterlace_q_data *s_q_data, *d_q_data;
716 	struct deinterlace_ctx *ctx = priv;
717 
718 	s_q_data = get_q_data(V4L2_BUF_TYPE_VIDEO_OUTPUT);
719 	d_q_data = get_q_data(V4L2_BUF_TYPE_VIDEO_CAPTURE);
720 
721 	/* Check that src and dst queues have the same pix format */
722 	if (s_q_data->fmt->fourcc != d_q_data->fmt->fourcc) {
723 		v4l2_err(&ctx->dev->v4l2_dev,
724 			 "src and dst formats don't match.\n");
725 		return -EINVAL;
726 	}
727 
728 	/* Check that input and output deinterlacing types are compatible */
729 	switch (s_q_data->field) {
730 	case V4L2_FIELD_SEQ_BT:
731 		if (d_q_data->field != V4L2_FIELD_NONE &&
732 			d_q_data->field != V4L2_FIELD_INTERLACED_BT) {
733 			v4l2_err(&ctx->dev->v4l2_dev,
734 			 "src and dst field conversion [(%d)->(%d)] not supported.\n",
735 				s_q_data->field, d_q_data->field);
736 			return -EINVAL;
737 		}
738 		break;
739 	case V4L2_FIELD_SEQ_TB:
740 		if (d_q_data->field != V4L2_FIELD_NONE &&
741 			d_q_data->field != V4L2_FIELD_INTERLACED_TB) {
742 			v4l2_err(&ctx->dev->v4l2_dev,
743 			 "src and dst field conversion [(%d)->(%d)] not supported.\n",
744 				s_q_data->field, d_q_data->field);
745 			return -EINVAL;
746 		}
747 		break;
748 	default:
749 		return -EINVAL;
750 	}
751 
752 	return v4l2_m2m_streamon(file, ctx->m2m_ctx, type);
753 }
754 
755 static int vidioc_streamoff(struct file *file, void *priv,
756 			    enum v4l2_buf_type type)
757 {
758 	struct deinterlace_ctx *ctx = priv;
759 
760 	return v4l2_m2m_streamoff(file, ctx->m2m_ctx, type);
761 }
762 
763 static const struct v4l2_ioctl_ops deinterlace_ioctl_ops = {
764 	.vidioc_querycap	= vidioc_querycap,
765 
766 	.vidioc_enum_fmt_vid_cap = vidioc_enum_fmt_vid_cap,
767 	.vidioc_g_fmt_vid_cap	= vidioc_g_fmt_vid_cap,
768 	.vidioc_try_fmt_vid_cap	= vidioc_try_fmt_vid_cap,
769 	.vidioc_s_fmt_vid_cap	= vidioc_s_fmt_vid_cap,
770 
771 	.vidioc_enum_fmt_vid_out = vidioc_enum_fmt_vid_out,
772 	.vidioc_g_fmt_vid_out	= vidioc_g_fmt_vid_out,
773 	.vidioc_try_fmt_vid_out	= vidioc_try_fmt_vid_out,
774 	.vidioc_s_fmt_vid_out	= vidioc_s_fmt_vid_out,
775 
776 	.vidioc_reqbufs		= vidioc_reqbufs,
777 	.vidioc_querybuf	= vidioc_querybuf,
778 
779 	.vidioc_qbuf		= vidioc_qbuf,
780 	.vidioc_dqbuf		= vidioc_dqbuf,
781 
782 	.vidioc_streamon	= vidioc_streamon,
783 	.vidioc_streamoff	= vidioc_streamoff,
784 };
785 
786 
787 /*
788  * Queue operations
789  */
790 struct vb2_dc_conf {
791 	struct device           *dev;
792 };
793 
794 static int deinterlace_queue_setup(struct vb2_queue *vq,
795 				const struct v4l2_format *fmt,
796 				unsigned int *nbuffers, unsigned int *nplanes,
797 				unsigned int sizes[], void *alloc_ctxs[])
798 {
799 	struct deinterlace_ctx *ctx = vb2_get_drv_priv(vq);
800 	struct deinterlace_q_data *q_data;
801 	unsigned int size, count = *nbuffers;
802 
803 	q_data = get_q_data(vq->type);
804 
805 	switch (q_data->fmt->fourcc) {
806 	case V4L2_PIX_FMT_YUV420:
807 		size = q_data->width * q_data->height * 3 / 2;
808 		break;
809 	case V4L2_PIX_FMT_YUYV:
810 	default:
811 		size = q_data->width * q_data->height * 2;
812 	}
813 
814 	*nplanes = 1;
815 	*nbuffers = count;
816 	sizes[0] = size;
817 
818 	alloc_ctxs[0] = ctx->dev->alloc_ctx;
819 
820 	dprintk(ctx->dev, "get %d buffer(s) of size %d each.\n", count, size);
821 
822 	return 0;
823 }
824 
825 static int deinterlace_buf_prepare(struct vb2_buffer *vb)
826 {
827 	struct deinterlace_ctx *ctx = vb2_get_drv_priv(vb->vb2_queue);
828 	struct deinterlace_q_data *q_data;
829 
830 	dprintk(ctx->dev, "type: %d\n", vb->vb2_queue->type);
831 
832 	q_data = get_q_data(vb->vb2_queue->type);
833 
834 	if (vb2_plane_size(vb, 0) < q_data->sizeimage) {
835 		dprintk(ctx->dev, "%s data will not fit into plane (%lu < %lu)\n",
836 			__func__, vb2_plane_size(vb, 0), (long)q_data->sizeimage);
837 		return -EINVAL;
838 	}
839 
840 	vb2_set_plane_payload(vb, 0, q_data->sizeimage);
841 
842 	return 0;
843 }
844 
845 static void deinterlace_buf_queue(struct vb2_buffer *vb)
846 {
847 	struct deinterlace_ctx *ctx = vb2_get_drv_priv(vb->vb2_queue);
848 	v4l2_m2m_buf_queue(ctx->m2m_ctx, vb);
849 }
850 
851 static struct vb2_ops deinterlace_qops = {
852 	.queue_setup	 = deinterlace_queue_setup,
853 	.buf_prepare	 = deinterlace_buf_prepare,
854 	.buf_queue	 = deinterlace_buf_queue,
855 };
856 
857 static int queue_init(void *priv, struct vb2_queue *src_vq,
858 		      struct vb2_queue *dst_vq)
859 {
860 	struct deinterlace_ctx *ctx = priv;
861 	int ret;
862 
863 	src_vq->type = V4L2_BUF_TYPE_VIDEO_OUTPUT;
864 	src_vq->io_modes = VB2_MMAP | VB2_USERPTR;
865 	src_vq->drv_priv = ctx;
866 	src_vq->buf_struct_size = sizeof(struct v4l2_m2m_buffer);
867 	src_vq->ops = &deinterlace_qops;
868 	src_vq->mem_ops = &vb2_dma_contig_memops;
869 	q_data[V4L2_M2M_SRC].fmt = &formats[0];
870 	q_data[V4L2_M2M_SRC].width = 640;
871 	q_data[V4L2_M2M_SRC].height = 480;
872 	q_data[V4L2_M2M_SRC].sizeimage = (640 * 480 * 3) / 2;
873 	q_data[V4L2_M2M_SRC].field = V4L2_FIELD_SEQ_TB;
874 
875 	ret = vb2_queue_init(src_vq);
876 	if (ret)
877 		return ret;
878 
879 	dst_vq->type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
880 	dst_vq->io_modes = VB2_MMAP | VB2_USERPTR;
881 	dst_vq->drv_priv = ctx;
882 	dst_vq->buf_struct_size = sizeof(struct v4l2_m2m_buffer);
883 	dst_vq->ops = &deinterlace_qops;
884 	dst_vq->mem_ops = &vb2_dma_contig_memops;
885 	q_data[V4L2_M2M_DST].fmt = &formats[0];
886 	q_data[V4L2_M2M_DST].width = 640;
887 	q_data[V4L2_M2M_DST].height = 480;
888 	q_data[V4L2_M2M_DST].sizeimage = (640 * 480 * 3) / 2;
889 	q_data[V4L2_M2M_SRC].field = V4L2_FIELD_INTERLACED_TB;
890 
891 	return vb2_queue_init(dst_vq);
892 }
893 
894 /*
895  * File operations
896  */
897 static int deinterlace_open(struct file *file)
898 {
899 	struct deinterlace_dev *pcdev = video_drvdata(file);
900 	struct deinterlace_ctx *ctx = NULL;
901 
902 	ctx = kzalloc(sizeof *ctx, GFP_KERNEL);
903 	if (!ctx)
904 		return -ENOMEM;
905 
906 	file->private_data = ctx;
907 	ctx->dev = pcdev;
908 
909 	ctx->m2m_ctx = v4l2_m2m_ctx_init(pcdev->m2m_dev, ctx, &queue_init);
910 	if (IS_ERR(ctx->m2m_ctx)) {
911 		int ret = PTR_ERR(ctx->m2m_ctx);
912 
913 		kfree(ctx);
914 		return ret;
915 	}
916 
917 	ctx->xt = kzalloc(sizeof(struct dma_async_tx_descriptor) +
918 				sizeof(struct data_chunk), GFP_KERNEL);
919 	if (!ctx->xt) {
920 		kfree(ctx);
921 		return -ENOMEM;
922 	}
923 
924 	ctx->colorspace = V4L2_COLORSPACE_REC709;
925 
926 	dprintk(pcdev, "Created instance %p, m2m_ctx: %p\n", ctx, ctx->m2m_ctx);
927 
928 	return 0;
929 }
930 
931 static int deinterlace_release(struct file *file)
932 {
933 	struct deinterlace_dev *pcdev = video_drvdata(file);
934 	struct deinterlace_ctx *ctx = file->private_data;
935 
936 	dprintk(pcdev, "Releasing instance %p\n", ctx);
937 
938 	v4l2_m2m_ctx_release(ctx->m2m_ctx);
939 	kfree(ctx->xt);
940 	kfree(ctx);
941 
942 	return 0;
943 }
944 
945 static unsigned int deinterlace_poll(struct file *file,
946 				 struct poll_table_struct *wait)
947 {
948 	struct deinterlace_ctx *ctx = file->private_data;
949 	int ret;
950 
951 	deinterlace_lock(ctx);
952 	ret = v4l2_m2m_poll(file, ctx->m2m_ctx, wait);
953 	deinterlace_unlock(ctx);
954 
955 	return ret;
956 }
957 
958 static int deinterlace_mmap(struct file *file, struct vm_area_struct *vma)
959 {
960 	struct deinterlace_ctx *ctx = file->private_data;
961 
962 	return v4l2_m2m_mmap(file, ctx->m2m_ctx, vma);
963 }
964 
965 static const struct v4l2_file_operations deinterlace_fops = {
966 	.owner		= THIS_MODULE,
967 	.open		= deinterlace_open,
968 	.release	= deinterlace_release,
969 	.poll		= deinterlace_poll,
970 	.unlocked_ioctl	= video_ioctl2,
971 	.mmap		= deinterlace_mmap,
972 };
973 
974 static struct video_device deinterlace_videodev = {
975 	.name		= MEM2MEM_NAME,
976 	.fops		= &deinterlace_fops,
977 	.ioctl_ops	= &deinterlace_ioctl_ops,
978 	.minor		= -1,
979 	.release	= video_device_release,
980 	.vfl_dir	= VFL_DIR_M2M,
981 };
982 
983 static struct v4l2_m2m_ops m2m_ops = {
984 	.device_run	= deinterlace_device_run,
985 	.job_ready	= deinterlace_job_ready,
986 	.job_abort	= deinterlace_job_abort,
987 	.lock		= deinterlace_lock,
988 	.unlock		= deinterlace_unlock,
989 };
990 
991 static int deinterlace_probe(struct platform_device *pdev)
992 {
993 	struct deinterlace_dev *pcdev;
994 	struct video_device *vfd;
995 	dma_cap_mask_t mask;
996 	int ret = 0;
997 
998 	pcdev = kzalloc(sizeof *pcdev, GFP_KERNEL);
999 	if (!pcdev)
1000 		return -ENOMEM;
1001 
1002 	spin_lock_init(&pcdev->irqlock);
1003 
1004 	dma_cap_zero(mask);
1005 	dma_cap_set(DMA_INTERLEAVE, mask);
1006 	pcdev->dma_chan = dma_request_channel(mask, NULL, pcdev);
1007 	if (!pcdev->dma_chan)
1008 		goto free_dev;
1009 
1010 	if (!dma_has_cap(DMA_INTERLEAVE, pcdev->dma_chan->device->cap_mask)) {
1011 		v4l2_err(&pcdev->v4l2_dev, "DMA does not support INTERLEAVE\n");
1012 		goto rel_dma;
1013 	}
1014 
1015 	ret = v4l2_device_register(&pdev->dev, &pcdev->v4l2_dev);
1016 	if (ret)
1017 		goto rel_dma;
1018 
1019 	atomic_set(&pcdev->busy, 0);
1020 	mutex_init(&pcdev->dev_mutex);
1021 
1022 	vfd = video_device_alloc();
1023 	if (!vfd) {
1024 		v4l2_err(&pcdev->v4l2_dev, "Failed to allocate video device\n");
1025 		ret = -ENOMEM;
1026 		goto unreg_dev;
1027 	}
1028 
1029 	*vfd = deinterlace_videodev;
1030 	vfd->lock = &pcdev->dev_mutex;
1031 
1032 	ret = video_register_device(vfd, VFL_TYPE_GRABBER, 0);
1033 	if (ret) {
1034 		v4l2_err(&pcdev->v4l2_dev, "Failed to register video device\n");
1035 		goto rel_vdev;
1036 	}
1037 
1038 	video_set_drvdata(vfd, pcdev);
1039 	snprintf(vfd->name, sizeof(vfd->name), "%s", deinterlace_videodev.name);
1040 	pcdev->vfd = vfd;
1041 	v4l2_info(&pcdev->v4l2_dev, MEM2MEM_TEST_MODULE_NAME
1042 			" Device registered as /dev/video%d\n", vfd->num);
1043 
1044 	platform_set_drvdata(pdev, pcdev);
1045 
1046 	pcdev->alloc_ctx = vb2_dma_contig_init_ctx(&pdev->dev);
1047 	if (IS_ERR(pcdev->alloc_ctx)) {
1048 		v4l2_err(&pcdev->v4l2_dev, "Failed to alloc vb2 context\n");
1049 		ret = PTR_ERR(pcdev->alloc_ctx);
1050 		goto err_ctx;
1051 	}
1052 
1053 	pcdev->m2m_dev = v4l2_m2m_init(&m2m_ops);
1054 	if (IS_ERR(pcdev->m2m_dev)) {
1055 		v4l2_err(&pcdev->v4l2_dev, "Failed to init mem2mem device\n");
1056 		ret = PTR_ERR(pcdev->m2m_dev);
1057 		goto err_m2m;
1058 	}
1059 
1060 	return 0;
1061 
1062 	v4l2_m2m_release(pcdev->m2m_dev);
1063 err_m2m:
1064 	video_unregister_device(pcdev->vfd);
1065 err_ctx:
1066 	vb2_dma_contig_cleanup_ctx(pcdev->alloc_ctx);
1067 rel_vdev:
1068 	video_device_release(vfd);
1069 unreg_dev:
1070 	v4l2_device_unregister(&pcdev->v4l2_dev);
1071 rel_dma:
1072 	dma_release_channel(pcdev->dma_chan);
1073 free_dev:
1074 	kfree(pcdev);
1075 
1076 	return ret;
1077 }
1078 
1079 static int deinterlace_remove(struct platform_device *pdev)
1080 {
1081 	struct deinterlace_dev *pcdev =
1082 		(struct deinterlace_dev *)platform_get_drvdata(pdev);
1083 
1084 	v4l2_info(&pcdev->v4l2_dev, "Removing " MEM2MEM_TEST_MODULE_NAME);
1085 	v4l2_m2m_release(pcdev->m2m_dev);
1086 	video_unregister_device(pcdev->vfd);
1087 	v4l2_device_unregister(&pcdev->v4l2_dev);
1088 	vb2_dma_contig_cleanup_ctx(pcdev->alloc_ctx);
1089 	dma_release_channel(pcdev->dma_chan);
1090 	kfree(pcdev);
1091 
1092 	return 0;
1093 }
1094 
1095 static struct platform_driver deinterlace_pdrv = {
1096 	.probe		= deinterlace_probe,
1097 	.remove		= deinterlace_remove,
1098 	.driver		= {
1099 		.name	= MEM2MEM_NAME,
1100 		.owner	= THIS_MODULE,
1101 	},
1102 };
1103 module_platform_driver(deinterlace_pdrv);
1104 
1105