xref: /linux/drivers/media/platform/samsung/s5p-mfc/s5p_mfc_dec.c (revision 34f7c6e7d4396090692a09789db231e12cb4762b)
1 // SPDX-License-Identifier: GPL-2.0-or-later
2 /*
3  * linux/drivers/media/platform/samsung/s5p-mfc/s5p_mfc_dec.c
4  *
5  * Copyright (C) 2011 Samsung Electronics Co., Ltd.
6  *		http://www.samsung.com/
7  * Kamil Debski, <k.debski@samsung.com>
8  */
9 
10 #include <linux/clk.h>
11 #include <linux/interrupt.h>
12 #include <linux/io.h>
13 #include <linux/module.h>
14 #include <linux/platform_device.h>
15 #include <linux/sched.h>
16 #include <linux/slab.h>
17 #include <linux/videodev2.h>
18 #include <linux/workqueue.h>
19 #include <media/v4l2-ctrls.h>
20 #include <media/v4l2-event.h>
21 #include <media/videobuf2-v4l2.h>
22 #include "s5p_mfc_common.h"
23 #include "s5p_mfc_ctrl.h"
24 #include "s5p_mfc_debug.h"
25 #include "s5p_mfc_dec.h"
26 #include "s5p_mfc_intr.h"
27 #include "s5p_mfc_opr.h"
28 #include "s5p_mfc_pm.h"
29 
30 static struct s5p_mfc_fmt formats[] = {
31 	{
32 		.fourcc		= V4L2_PIX_FMT_NV12MT_16X16,
33 		.codec_mode	= S5P_MFC_CODEC_NONE,
34 		.type		= MFC_FMT_RAW,
35 		.num_planes	= 2,
36 		.versions	= MFC_V6_BIT | MFC_V7_BIT,
37 	},
38 	{
39 		.fourcc		= V4L2_PIX_FMT_NV12MT,
40 		.codec_mode	= S5P_MFC_CODEC_NONE,
41 		.type		= MFC_FMT_RAW,
42 		.num_planes	= 2,
43 		.versions	= MFC_V5_BIT,
44 	},
45 	{
46 		.fourcc		= V4L2_PIX_FMT_NV12M,
47 		.codec_mode	= S5P_MFC_CODEC_NONE,
48 		.type		= MFC_FMT_RAW,
49 		.num_planes	= 2,
50 		.versions	= MFC_V6PLUS_BITS,
51 	},
52 	{
53 		.fourcc		= V4L2_PIX_FMT_NV21M,
54 		.codec_mode	= S5P_MFC_CODEC_NONE,
55 		.type		= MFC_FMT_RAW,
56 		.num_planes	= 2,
57 		.versions	= MFC_V6PLUS_BITS,
58 	},
59 	{
60 		.fourcc		= V4L2_PIX_FMT_H264,
61 		.codec_mode	= S5P_MFC_CODEC_H264_DEC,
62 		.type		= MFC_FMT_DEC,
63 		.num_planes	= 1,
64 		.versions	= MFC_V5PLUS_BITS,
65 		.flags		= V4L2_FMT_FLAG_DYN_RESOLUTION |
66 				  V4L2_FMT_FLAG_CONTINUOUS_BYTESTREAM,
67 	},
68 	{
69 		.fourcc		= V4L2_PIX_FMT_H264_MVC,
70 		.codec_mode	= S5P_MFC_CODEC_H264_MVC_DEC,
71 		.type		= MFC_FMT_DEC,
72 		.num_planes	= 1,
73 		.versions	= MFC_V6PLUS_BITS,
74 		.flags		= V4L2_FMT_FLAG_DYN_RESOLUTION |
75 				  V4L2_FMT_FLAG_CONTINUOUS_BYTESTREAM,
76 	},
77 	{
78 		.fourcc		= V4L2_PIX_FMT_H263,
79 		.codec_mode	= S5P_MFC_CODEC_H263_DEC,
80 		.type		= MFC_FMT_DEC,
81 		.num_planes	= 1,
82 		.versions	= MFC_V5PLUS_BITS,
83 		.flags		= V4L2_FMT_FLAG_DYN_RESOLUTION,
84 	},
85 	{
86 		.fourcc		= V4L2_PIX_FMT_MPEG1,
87 		.codec_mode	= S5P_MFC_CODEC_MPEG2_DEC,
88 		.type		= MFC_FMT_DEC,
89 		.num_planes	= 1,
90 		.versions	= MFC_V5PLUS_BITS,
91 		.flags		= V4L2_FMT_FLAG_DYN_RESOLUTION |
92 				  V4L2_FMT_FLAG_CONTINUOUS_BYTESTREAM,
93 	},
94 	{
95 		.fourcc		= V4L2_PIX_FMT_MPEG2,
96 		.codec_mode	= S5P_MFC_CODEC_MPEG2_DEC,
97 		.type		= MFC_FMT_DEC,
98 		.num_planes	= 1,
99 		.versions	= MFC_V5PLUS_BITS,
100 		.flags		= V4L2_FMT_FLAG_DYN_RESOLUTION |
101 				  V4L2_FMT_FLAG_CONTINUOUS_BYTESTREAM,
102 	},
103 	{
104 		.fourcc		= V4L2_PIX_FMT_MPEG4,
105 		.codec_mode	= S5P_MFC_CODEC_MPEG4_DEC,
106 		.type		= MFC_FMT_DEC,
107 		.num_planes	= 1,
108 		.versions	= MFC_V5PLUS_BITS,
109 		.flags		= V4L2_FMT_FLAG_DYN_RESOLUTION |
110 				  V4L2_FMT_FLAG_CONTINUOUS_BYTESTREAM,
111 	},
112 	{
113 		.fourcc		= V4L2_PIX_FMT_XVID,
114 		.codec_mode	= S5P_MFC_CODEC_MPEG4_DEC,
115 		.type		= MFC_FMT_DEC,
116 		.num_planes	= 1,
117 		.versions	= MFC_V5PLUS_BITS,
118 		.flags		= V4L2_FMT_FLAG_DYN_RESOLUTION,
119 	},
120 	{
121 		.fourcc		= V4L2_PIX_FMT_VC1_ANNEX_G,
122 		.codec_mode	= S5P_MFC_CODEC_VC1_DEC,
123 		.type		= MFC_FMT_DEC,
124 		.num_planes	= 1,
125 		.versions	= MFC_V5PLUS_BITS,
126 		.flags		= V4L2_FMT_FLAG_DYN_RESOLUTION,
127 	},
128 	{
129 		.fourcc		= V4L2_PIX_FMT_VC1_ANNEX_L,
130 		.codec_mode	= S5P_MFC_CODEC_VC1RCV_DEC,
131 		.type		= MFC_FMT_DEC,
132 		.num_planes	= 1,
133 		.versions	= MFC_V5PLUS_BITS,
134 		.flags		= V4L2_FMT_FLAG_DYN_RESOLUTION,
135 	},
136 	{
137 		.fourcc		= V4L2_PIX_FMT_VP8,
138 		.codec_mode	= S5P_MFC_CODEC_VP8_DEC,
139 		.type		= MFC_FMT_DEC,
140 		.num_planes	= 1,
141 		.versions	= MFC_V6PLUS_BITS,
142 		.flags		= V4L2_FMT_FLAG_DYN_RESOLUTION,
143 	},
144 	{
145 		.fourcc		= V4L2_PIX_FMT_HEVC,
146 		.codec_mode	= S5P_FIMV_CODEC_HEVC_DEC,
147 		.type		= MFC_FMT_DEC,
148 		.num_planes	= 1,
149 		.versions	= MFC_V10_BIT,
150 		.flags		= V4L2_FMT_FLAG_DYN_RESOLUTION |
151 				  V4L2_FMT_FLAG_CONTINUOUS_BYTESTREAM,
152 	},
153 	{
154 		.fourcc		= V4L2_PIX_FMT_VP9,
155 		.codec_mode	= S5P_FIMV_CODEC_VP9_DEC,
156 		.type		= MFC_FMT_DEC,
157 		.num_planes	= 1,
158 		.versions	= MFC_V10_BIT,
159 		.flags		= V4L2_FMT_FLAG_DYN_RESOLUTION,
160 	},
161 };
162 
163 #define NUM_FORMATS ARRAY_SIZE(formats)
164 
165 /* Find selected format description */
166 static struct s5p_mfc_fmt *find_format(struct v4l2_format *f, unsigned int t)
167 {
168 	unsigned int i;
169 
170 	for (i = 0; i < NUM_FORMATS; i++) {
171 		if (formats[i].fourcc == f->fmt.pix_mp.pixelformat &&
172 		    formats[i].type == t)
173 			return &formats[i];
174 	}
175 	return NULL;
176 }
177 
178 static struct mfc_control controls[] = {
179 	{
180 		.id = V4L2_CID_MPEG_MFC51_VIDEO_DECODER_H264_DISPLAY_DELAY,
181 		.type = V4L2_CTRL_TYPE_INTEGER,
182 		.name = "H264 Display Delay",
183 		.minimum = 0,
184 		.maximum = 16383,
185 		.step = 1,
186 		.default_value = 0,
187 	},
188 	{
189 		.id = V4L2_CID_MPEG_VIDEO_DEC_DISPLAY_DELAY,
190 		.type = V4L2_CTRL_TYPE_INTEGER,
191 		.minimum = 0,
192 		.maximum = 16383,
193 		.step = 1,
194 		.default_value = 0,
195 	},
196 	{
197 		.id = V4L2_CID_MPEG_MFC51_VIDEO_DECODER_H264_DISPLAY_DELAY_ENABLE,
198 		.type = V4L2_CTRL_TYPE_BOOLEAN,
199 		.name = "H264 Display Delay Enable",
200 		.minimum = 0,
201 		.maximum = 1,
202 		.step = 1,
203 		.default_value = 0,
204 	},
205 	{
206 		.id = V4L2_CID_MPEG_VIDEO_DEC_DISPLAY_DELAY_ENABLE,
207 		.type = V4L2_CTRL_TYPE_BOOLEAN,
208 		.minimum = 0,
209 		.maximum = 1,
210 		.default_value = 0,
211 	},
212 	{
213 		.id = V4L2_CID_MPEG_VIDEO_DECODER_MPEG4_DEBLOCK_FILTER,
214 		.type = V4L2_CTRL_TYPE_BOOLEAN,
215 		.name = "Mpeg4 Loop Filter Enable",
216 		.minimum = 0,
217 		.maximum = 1,
218 		.step = 1,
219 		.default_value = 0,
220 	},
221 	{
222 		.id = V4L2_CID_MPEG_VIDEO_DECODER_SLICE_INTERFACE,
223 		.type = V4L2_CTRL_TYPE_BOOLEAN,
224 		.name = "Slice Interface Enable",
225 		.minimum = 0,
226 		.maximum = 1,
227 		.step = 1,
228 		.default_value = 0,
229 	},
230 	{
231 		.id = V4L2_CID_MIN_BUFFERS_FOR_CAPTURE,
232 		.type = V4L2_CTRL_TYPE_INTEGER,
233 		.name = "Minimum number of cap bufs",
234 		.minimum = 1,
235 		.maximum = 32,
236 		.step = 1,
237 		.default_value = 1,
238 		.is_volatile = 1,
239 	},
240 };
241 
242 #define NUM_CTRLS ARRAY_SIZE(controls)
243 
244 /* Check whether a context should be run on hardware */
245 static int s5p_mfc_ctx_ready(struct s5p_mfc_ctx *ctx)
246 {
247 	/* Context is to parse header */
248 	if (ctx->src_queue_cnt >= 1 && ctx->state == MFCINST_GOT_INST)
249 		return 1;
250 	/* Context is to decode a frame */
251 	if (ctx->src_queue_cnt >= 1 &&
252 	    ctx->state == MFCINST_RUNNING &&
253 	    ctx->dst_queue_cnt >= ctx->pb_count)
254 		return 1;
255 	/* Context is to return last frame */
256 	if (ctx->state == MFCINST_FINISHING &&
257 	    ctx->dst_queue_cnt >= ctx->pb_count)
258 		return 1;
259 	/* Context is to set buffers */
260 	if (ctx->src_queue_cnt >= 1 &&
261 	    ctx->state == MFCINST_HEAD_PARSED &&
262 	    ctx->capture_state == QUEUE_BUFS_MMAPED)
263 		return 1;
264 	/* Resolution change */
265 	if ((ctx->state == MFCINST_RES_CHANGE_INIT ||
266 		ctx->state == MFCINST_RES_CHANGE_FLUSH) &&
267 		ctx->dst_queue_cnt >= ctx->pb_count)
268 		return 1;
269 	if (ctx->state == MFCINST_RES_CHANGE_END &&
270 		ctx->src_queue_cnt >= 1)
271 		return 1;
272 	mfc_debug(2, "ctx is not ready\n");
273 	return 0;
274 }
275 
276 static const struct s5p_mfc_codec_ops decoder_codec_ops = {
277 	.pre_seq_start		= NULL,
278 	.post_seq_start		= NULL,
279 	.pre_frame_start	= NULL,
280 	.post_frame_start	= NULL,
281 };
282 
283 /* Query capabilities of the device */
284 static int vidioc_querycap(struct file *file, void *priv,
285 			   struct v4l2_capability *cap)
286 {
287 	struct s5p_mfc_dev *dev = video_drvdata(file);
288 
289 	strscpy(cap->driver, S5P_MFC_NAME, sizeof(cap->driver));
290 	strscpy(cap->card, dev->vfd_dec->name, sizeof(cap->card));
291 	snprintf(cap->bus_info, sizeof(cap->bus_info), "platform:%s",
292 		 dev_name(&dev->plat_dev->dev));
293 	return 0;
294 }
295 
296 /* Enumerate format */
297 static int vidioc_enum_fmt(struct file *file, struct v4l2_fmtdesc *f,
298 							bool out)
299 {
300 	struct s5p_mfc_dev *dev = video_drvdata(file);
301 	int i, j = 0;
302 
303 	for (i = 0; i < ARRAY_SIZE(formats); ++i) {
304 		if (out && formats[i].type != MFC_FMT_DEC)
305 			continue;
306 		else if (!out && formats[i].type != MFC_FMT_RAW)
307 			continue;
308 		else if ((dev->variant->version_bit & formats[i].versions) == 0)
309 			continue;
310 
311 		if (j == f->index)
312 			break;
313 		++j;
314 	}
315 	if (i == ARRAY_SIZE(formats))
316 		return -EINVAL;
317 	f->pixelformat = formats[i].fourcc;
318 	return 0;
319 }
320 
321 static int vidioc_enum_fmt_vid_cap(struct file *file, void *pirv,
322 				   struct v4l2_fmtdesc *f)
323 {
324 	return vidioc_enum_fmt(file, f, false);
325 }
326 
327 static int vidioc_enum_fmt_vid_out(struct file *file, void *priv,
328 				   struct v4l2_fmtdesc *f)
329 {
330 	return vidioc_enum_fmt(file, f, true);
331 }
332 
333 /* Get format */
334 static int vidioc_g_fmt(struct file *file, void *priv, struct v4l2_format *f)
335 {
336 	struct s5p_mfc_ctx *ctx = fh_to_ctx(priv);
337 	struct v4l2_pix_format_mplane *pix_mp;
338 
339 	mfc_debug_enter();
340 	pix_mp = &f->fmt.pix_mp;
341 	if (f->type == V4L2_BUF_TYPE_VIDEO_CAPTURE_MPLANE &&
342 	    (ctx->state == MFCINST_GOT_INST || ctx->state ==
343 						MFCINST_RES_CHANGE_END)) {
344 		/* If the MFC is parsing the header,
345 		 * so wait until it is finished */
346 		s5p_mfc_wait_for_done_ctx(ctx, S5P_MFC_R2H_CMD_SEQ_DONE_RET,
347 									0);
348 	}
349 	if (f->type == V4L2_BUF_TYPE_VIDEO_CAPTURE_MPLANE &&
350 	    ctx->state >= MFCINST_HEAD_PARSED &&
351 	    ctx->state < MFCINST_ABORT) {
352 		/* This is run on CAPTURE (decode output) */
353 		/* Width and height are set to the dimensions
354 		   of the movie, the buffer is bigger and
355 		   further processing stages should crop to this
356 		   rectangle. */
357 		pix_mp->width = ctx->buf_width;
358 		pix_mp->height = ctx->buf_height;
359 		pix_mp->field = V4L2_FIELD_NONE;
360 		pix_mp->num_planes = 2;
361 		/* Set pixelformat to the format in which MFC
362 		   outputs the decoded frame */
363 		pix_mp->pixelformat = ctx->dst_fmt->fourcc;
364 		pix_mp->plane_fmt[0].bytesperline = ctx->buf_width;
365 		pix_mp->plane_fmt[0].sizeimage = ctx->luma_size;
366 		pix_mp->plane_fmt[1].bytesperline = ctx->buf_width;
367 		pix_mp->plane_fmt[1].sizeimage = ctx->chroma_size;
368 	} else if (f->type == V4L2_BUF_TYPE_VIDEO_OUTPUT_MPLANE) {
369 		/* This is run on OUTPUT
370 		   The buffer contains compressed image
371 		   so width and height have no meaning */
372 		pix_mp->width = 0;
373 		pix_mp->height = 0;
374 		pix_mp->field = V4L2_FIELD_NONE;
375 		pix_mp->plane_fmt[0].bytesperline = ctx->dec_src_buf_size;
376 		pix_mp->plane_fmt[0].sizeimage = ctx->dec_src_buf_size;
377 		pix_mp->pixelformat = ctx->src_fmt->fourcc;
378 		pix_mp->num_planes = ctx->src_fmt->num_planes;
379 	} else {
380 		mfc_err("Format could not be read\n");
381 		mfc_debug(2, "%s-- with error\n", __func__);
382 		return -EINVAL;
383 	}
384 	mfc_debug_leave();
385 	return 0;
386 }
387 
388 /* Try format */
389 static int vidioc_try_fmt(struct file *file, void *priv, struct v4l2_format *f)
390 {
391 	struct s5p_mfc_dev *dev = video_drvdata(file);
392 	struct s5p_mfc_fmt *fmt;
393 
394 	mfc_debug(2, "Type is %d\n", f->type);
395 	if (f->type == V4L2_BUF_TYPE_VIDEO_OUTPUT_MPLANE) {
396 		fmt = find_format(f, MFC_FMT_DEC);
397 		if (!fmt) {
398 			mfc_err("Unsupported format for source.\n");
399 			return -EINVAL;
400 		}
401 		if (fmt->codec_mode == S5P_FIMV_CODEC_NONE) {
402 			mfc_err("Unknown codec\n");
403 			return -EINVAL;
404 		}
405 		if ((dev->variant->version_bit & fmt->versions) == 0) {
406 			mfc_err("Unsupported format by this MFC version.\n");
407 			return -EINVAL;
408 		}
409 	} else if (f->type == V4L2_BUF_TYPE_VIDEO_CAPTURE_MPLANE) {
410 		fmt = find_format(f, MFC_FMT_RAW);
411 		if (!fmt) {
412 			mfc_err("Unsupported format for destination.\n");
413 			return -EINVAL;
414 		}
415 		if ((dev->variant->version_bit & fmt->versions) == 0) {
416 			mfc_err("Unsupported format by this MFC version.\n");
417 			return -EINVAL;
418 		}
419 	}
420 
421 	return 0;
422 }
423 
424 /* Set format */
425 static int vidioc_s_fmt(struct file *file, void *priv, struct v4l2_format *f)
426 {
427 	struct s5p_mfc_dev *dev = video_drvdata(file);
428 	struct s5p_mfc_ctx *ctx = fh_to_ctx(priv);
429 	int ret = 0;
430 	struct v4l2_pix_format_mplane *pix_mp;
431 	struct s5p_mfc_buf_size *buf_size = dev->variant->buf_size;
432 
433 	mfc_debug_enter();
434 	ret = vidioc_try_fmt(file, priv, f);
435 	pix_mp = &f->fmt.pix_mp;
436 	if (ret)
437 		return ret;
438 	if (vb2_is_streaming(&ctx->vq_src) || vb2_is_streaming(&ctx->vq_dst)) {
439 		v4l2_err(&dev->v4l2_dev, "%s queue busy\n", __func__);
440 		ret = -EBUSY;
441 		goto out;
442 	}
443 	if (f->type == V4L2_BUF_TYPE_VIDEO_CAPTURE_MPLANE) {
444 		/* dst_fmt is validated by call to vidioc_try_fmt */
445 		ctx->dst_fmt = find_format(f, MFC_FMT_RAW);
446 		ret = 0;
447 		goto out;
448 	} else if (f->type == V4L2_BUF_TYPE_VIDEO_OUTPUT_MPLANE) {
449 		/* src_fmt is validated by call to vidioc_try_fmt */
450 		ctx->src_fmt = find_format(f, MFC_FMT_DEC);
451 		ctx->codec_mode = ctx->src_fmt->codec_mode;
452 		mfc_debug(2, "The codec number is: %d\n", ctx->codec_mode);
453 		pix_mp->height = 0;
454 		pix_mp->width = 0;
455 		if (pix_mp->plane_fmt[0].sizeimage == 0)
456 			pix_mp->plane_fmt[0].sizeimage = ctx->dec_src_buf_size =
457 								DEF_CPB_SIZE;
458 		else if (pix_mp->plane_fmt[0].sizeimage > buf_size->cpb)
459 			ctx->dec_src_buf_size = buf_size->cpb;
460 		else
461 			ctx->dec_src_buf_size = pix_mp->plane_fmt[0].sizeimage;
462 		pix_mp->plane_fmt[0].bytesperline = 0;
463 		ctx->state = MFCINST_INIT;
464 		ret = 0;
465 		goto out;
466 	} else {
467 		mfc_err("Wrong type error for S_FMT : %d", f->type);
468 		ret = -EINVAL;
469 		goto out;
470 	}
471 
472 out:
473 	mfc_debug_leave();
474 	return ret;
475 }
476 
477 static int reqbufs_output(struct s5p_mfc_dev *dev, struct s5p_mfc_ctx *ctx,
478 				struct v4l2_requestbuffers *reqbufs)
479 {
480 	int ret = 0;
481 
482 	s5p_mfc_clock_on();
483 
484 	if (reqbufs->count == 0) {
485 		mfc_debug(2, "Freeing buffers\n");
486 		ret = vb2_reqbufs(&ctx->vq_src, reqbufs);
487 		if (ret)
488 			goto out;
489 		ctx->src_bufs_cnt = 0;
490 		ctx->output_state = QUEUE_FREE;
491 	} else if (ctx->output_state == QUEUE_FREE) {
492 		/* Can only request buffers when we have a valid format set. */
493 		WARN_ON(ctx->src_bufs_cnt != 0);
494 		if (ctx->state != MFCINST_INIT) {
495 			mfc_err("Reqbufs called in an invalid state\n");
496 			ret = -EINVAL;
497 			goto out;
498 		}
499 
500 		mfc_debug(2, "Allocating %d buffers for OUTPUT queue\n",
501 				reqbufs->count);
502 		ret = vb2_reqbufs(&ctx->vq_src, reqbufs);
503 		if (ret)
504 			goto out;
505 
506 		ret = s5p_mfc_open_mfc_inst(dev, ctx);
507 		if (ret) {
508 			reqbufs->count = 0;
509 			vb2_reqbufs(&ctx->vq_src, reqbufs);
510 			goto out;
511 		}
512 
513 		ctx->output_state = QUEUE_BUFS_REQUESTED;
514 	} else {
515 		mfc_err("Buffers have already been requested\n");
516 		ret = -EINVAL;
517 	}
518 out:
519 	s5p_mfc_clock_off();
520 	if (ret)
521 		mfc_err("Failed allocating buffers for OUTPUT queue\n");
522 	return ret;
523 }
524 
525 static int reqbufs_capture(struct s5p_mfc_dev *dev, struct s5p_mfc_ctx *ctx,
526 				struct v4l2_requestbuffers *reqbufs)
527 {
528 	int ret = 0;
529 
530 	s5p_mfc_clock_on();
531 
532 	if (reqbufs->count == 0) {
533 		mfc_debug(2, "Freeing buffers\n");
534 		ret = vb2_reqbufs(&ctx->vq_dst, reqbufs);
535 		if (ret)
536 			goto out;
537 		s5p_mfc_hw_call(dev->mfc_ops, release_codec_buffers, ctx);
538 		ctx->dst_bufs_cnt = 0;
539 	} else if (ctx->capture_state == QUEUE_FREE) {
540 		WARN_ON(ctx->dst_bufs_cnt != 0);
541 		mfc_debug(2, "Allocating %d buffers for CAPTURE queue\n",
542 				reqbufs->count);
543 		ret = vb2_reqbufs(&ctx->vq_dst, reqbufs);
544 		if (ret)
545 			goto out;
546 
547 		ctx->capture_state = QUEUE_BUFS_REQUESTED;
548 		ctx->total_dpb_count = reqbufs->count;
549 
550 		ret = s5p_mfc_hw_call(dev->mfc_ops, alloc_codec_buffers, ctx);
551 		if (ret) {
552 			mfc_err("Failed to allocate decoding buffers\n");
553 			reqbufs->count = 0;
554 			vb2_reqbufs(&ctx->vq_dst, reqbufs);
555 			ret = -ENOMEM;
556 			ctx->capture_state = QUEUE_FREE;
557 			goto out;
558 		}
559 
560 		WARN_ON(ctx->dst_bufs_cnt != ctx->total_dpb_count);
561 		ctx->capture_state = QUEUE_BUFS_MMAPED;
562 
563 		if (s5p_mfc_ctx_ready(ctx))
564 			set_work_bit_irqsave(ctx);
565 		s5p_mfc_hw_call(dev->mfc_ops, try_run, dev);
566 		s5p_mfc_wait_for_done_ctx(ctx, S5P_MFC_R2H_CMD_INIT_BUFFERS_RET,
567 					  0);
568 	} else {
569 		mfc_err("Buffers have already been requested\n");
570 		ret = -EINVAL;
571 	}
572 out:
573 	s5p_mfc_clock_off();
574 	if (ret)
575 		mfc_err("Failed allocating buffers for CAPTURE queue\n");
576 	return ret;
577 }
578 
579 /* Request buffers */
580 static int vidioc_reqbufs(struct file *file, void *priv,
581 					  struct v4l2_requestbuffers *reqbufs)
582 {
583 	struct s5p_mfc_dev *dev = video_drvdata(file);
584 	struct s5p_mfc_ctx *ctx = fh_to_ctx(priv);
585 
586 	if (reqbufs->memory != V4L2_MEMORY_MMAP) {
587 		mfc_debug(2, "Only V4L2_MEMORY_MMAP is supported\n");
588 		return -EINVAL;
589 	}
590 
591 	if (reqbufs->type == V4L2_BUF_TYPE_VIDEO_OUTPUT_MPLANE) {
592 		return reqbufs_output(dev, ctx, reqbufs);
593 	} else if (reqbufs->type == V4L2_BUF_TYPE_VIDEO_CAPTURE_MPLANE) {
594 		return reqbufs_capture(dev, ctx, reqbufs);
595 	} else {
596 		mfc_err("Invalid type requested\n");
597 		return -EINVAL;
598 	}
599 }
600 
601 /* Query buffer */
602 static int vidioc_querybuf(struct file *file, void *priv,
603 						   struct v4l2_buffer *buf)
604 {
605 	struct s5p_mfc_ctx *ctx = fh_to_ctx(priv);
606 	int ret;
607 	int i;
608 
609 	if (buf->memory != V4L2_MEMORY_MMAP) {
610 		mfc_err("Only mmapped buffers can be used\n");
611 		return -EINVAL;
612 	}
613 	mfc_debug(2, "State: %d, buf->type: %d\n", ctx->state, buf->type);
614 	if (ctx->state == MFCINST_GOT_INST &&
615 			buf->type == V4L2_BUF_TYPE_VIDEO_OUTPUT_MPLANE) {
616 		ret = vb2_querybuf(&ctx->vq_src, buf);
617 	} else if (ctx->state == MFCINST_RUNNING &&
618 			buf->type == V4L2_BUF_TYPE_VIDEO_CAPTURE_MPLANE) {
619 		ret = vb2_querybuf(&ctx->vq_dst, buf);
620 		for (i = 0; i < buf->length; i++)
621 			buf->m.planes[i].m.mem_offset += DST_QUEUE_OFF_BASE;
622 	} else {
623 		mfc_err("vidioc_querybuf called in an inappropriate state\n");
624 		ret = -EINVAL;
625 	}
626 	mfc_debug_leave();
627 	return ret;
628 }
629 
630 /* Queue a buffer */
631 static int vidioc_qbuf(struct file *file, void *priv, struct v4l2_buffer *buf)
632 {
633 	struct s5p_mfc_ctx *ctx = fh_to_ctx(priv);
634 
635 	if (ctx->state == MFCINST_ERROR) {
636 		mfc_err("Call on QBUF after unrecoverable error\n");
637 		return -EIO;
638 	}
639 	if (buf->type == V4L2_BUF_TYPE_VIDEO_OUTPUT_MPLANE)
640 		return vb2_qbuf(&ctx->vq_src, NULL, buf);
641 	else if (buf->type == V4L2_BUF_TYPE_VIDEO_CAPTURE_MPLANE)
642 		return vb2_qbuf(&ctx->vq_dst, NULL, buf);
643 	return -EINVAL;
644 }
645 
646 /* Dequeue a buffer */
647 static int vidioc_dqbuf(struct file *file, void *priv, struct v4l2_buffer *buf)
648 {
649 	const struct v4l2_event ev = {
650 		.type = V4L2_EVENT_EOS
651 	};
652 	struct s5p_mfc_ctx *ctx = fh_to_ctx(priv);
653 	int ret;
654 
655 	if (ctx->state == MFCINST_ERROR) {
656 		mfc_err_limited("Call on DQBUF after unrecoverable error\n");
657 		return -EIO;
658 	}
659 
660 	switch (buf->type) {
661 	case V4L2_BUF_TYPE_VIDEO_OUTPUT_MPLANE:
662 		return vb2_dqbuf(&ctx->vq_src, buf, file->f_flags & O_NONBLOCK);
663 	case V4L2_BUF_TYPE_VIDEO_CAPTURE_MPLANE:
664 		ret = vb2_dqbuf(&ctx->vq_dst, buf, file->f_flags & O_NONBLOCK);
665 		if (ret)
666 			return ret;
667 
668 		if (ctx->state == MFCINST_FINISHED &&
669 		    (ctx->dst_bufs[buf->index].flags & MFC_BUF_FLAG_EOS))
670 			v4l2_event_queue_fh(&ctx->fh, &ev);
671 		return 0;
672 	default:
673 		return -EINVAL;
674 	}
675 }
676 
677 /* Export DMA buffer */
678 static int vidioc_expbuf(struct file *file, void *priv,
679 	struct v4l2_exportbuffer *eb)
680 {
681 	struct s5p_mfc_ctx *ctx = fh_to_ctx(priv);
682 
683 	if (eb->type == V4L2_BUF_TYPE_VIDEO_OUTPUT_MPLANE)
684 		return vb2_expbuf(&ctx->vq_src, eb);
685 	if (eb->type == V4L2_BUF_TYPE_VIDEO_CAPTURE_MPLANE)
686 		return vb2_expbuf(&ctx->vq_dst, eb);
687 	return -EINVAL;
688 }
689 
690 /* Stream on */
691 static int vidioc_streamon(struct file *file, void *priv,
692 			   enum v4l2_buf_type type)
693 {
694 	struct s5p_mfc_ctx *ctx = fh_to_ctx(priv);
695 	int ret = -EINVAL;
696 
697 	mfc_debug_enter();
698 	if (type == V4L2_BUF_TYPE_VIDEO_OUTPUT_MPLANE)
699 		ret = vb2_streamon(&ctx->vq_src, type);
700 	else if (type == V4L2_BUF_TYPE_VIDEO_CAPTURE_MPLANE)
701 		ret = vb2_streamon(&ctx->vq_dst, type);
702 	mfc_debug_leave();
703 	return ret;
704 }
705 
706 /* Stream off, which equals to a pause */
707 static int vidioc_streamoff(struct file *file, void *priv,
708 			    enum v4l2_buf_type type)
709 {
710 	struct s5p_mfc_ctx *ctx = fh_to_ctx(priv);
711 
712 	if (type == V4L2_BUF_TYPE_VIDEO_OUTPUT_MPLANE)
713 		return vb2_streamoff(&ctx->vq_src, type);
714 	else if (type == V4L2_BUF_TYPE_VIDEO_CAPTURE_MPLANE)
715 		return vb2_streamoff(&ctx->vq_dst, type);
716 	return -EINVAL;
717 }
718 
719 /* Set controls - v4l2 control framework */
720 static int s5p_mfc_dec_s_ctrl(struct v4l2_ctrl *ctrl)
721 {
722 	struct s5p_mfc_ctx *ctx = ctrl_to_ctx(ctrl);
723 
724 	switch (ctrl->id) {
725 	case V4L2_CID_MPEG_MFC51_VIDEO_DECODER_H264_DISPLAY_DELAY:
726 	case V4L2_CID_MPEG_VIDEO_DEC_DISPLAY_DELAY:
727 		ctx->display_delay = ctrl->val;
728 		break;
729 	case V4L2_CID_MPEG_MFC51_VIDEO_DECODER_H264_DISPLAY_DELAY_ENABLE:
730 	case V4L2_CID_MPEG_VIDEO_DEC_DISPLAY_DELAY_ENABLE:
731 		ctx->display_delay_enable = ctrl->val;
732 		break;
733 	case V4L2_CID_MPEG_VIDEO_DECODER_MPEG4_DEBLOCK_FILTER:
734 		ctx->loop_filter_mpeg4 = ctrl->val;
735 		break;
736 	case V4L2_CID_MPEG_VIDEO_DECODER_SLICE_INTERFACE:
737 		ctx->slice_interface = ctrl->val;
738 		break;
739 	default:
740 		mfc_err("Invalid control 0x%08x\n", ctrl->id);
741 		return -EINVAL;
742 	}
743 	return 0;
744 }
745 
746 static int s5p_mfc_dec_g_v_ctrl(struct v4l2_ctrl *ctrl)
747 {
748 	struct s5p_mfc_ctx *ctx = ctrl_to_ctx(ctrl);
749 	struct s5p_mfc_dev *dev = ctx->dev;
750 
751 	switch (ctrl->id) {
752 	case V4L2_CID_MIN_BUFFERS_FOR_CAPTURE:
753 		if (ctx->state >= MFCINST_HEAD_PARSED &&
754 		    ctx->state < MFCINST_ABORT) {
755 			ctrl->val = ctx->pb_count;
756 			break;
757 		} else if (ctx->state != MFCINST_INIT &&
758 				ctx->state != MFCINST_RES_CHANGE_END) {
759 			v4l2_err(&dev->v4l2_dev, "Decoding not initialised\n");
760 			return -EINVAL;
761 		}
762 		/* Should wait for the header to be parsed */
763 		s5p_mfc_wait_for_done_ctx(ctx,
764 				S5P_MFC_R2H_CMD_SEQ_DONE_RET, 0);
765 		if (ctx->state >= MFCINST_HEAD_PARSED &&
766 		    ctx->state < MFCINST_ABORT) {
767 			ctrl->val = ctx->pb_count;
768 		} else {
769 			v4l2_err(&dev->v4l2_dev, "Decoding not initialised\n");
770 			return -EINVAL;
771 		}
772 		break;
773 	}
774 	return 0;
775 }
776 
777 
778 static const struct v4l2_ctrl_ops s5p_mfc_dec_ctrl_ops = {
779 	.s_ctrl = s5p_mfc_dec_s_ctrl,
780 	.g_volatile_ctrl = s5p_mfc_dec_g_v_ctrl,
781 };
782 
783 /* Get compose information */
784 static int vidioc_g_selection(struct file *file, void *priv,
785 			      struct v4l2_selection *s)
786 {
787 	struct s5p_mfc_ctx *ctx = fh_to_ctx(priv);
788 	struct s5p_mfc_dev *dev = ctx->dev;
789 	u32 left, right, top, bottom;
790 	u32 width, height;
791 
792 	if (s->type != V4L2_BUF_TYPE_VIDEO_CAPTURE)
793 		return -EINVAL;
794 
795 	if (ctx->state != MFCINST_HEAD_PARSED &&
796 	    ctx->state != MFCINST_RUNNING &&
797 	    ctx->state != MFCINST_FINISHING &&
798 	    ctx->state != MFCINST_FINISHED) {
799 		mfc_err("Can not get compose information\n");
800 		return -EINVAL;
801 	}
802 	if (ctx->src_fmt->fourcc == V4L2_PIX_FMT_H264) {
803 		left = s5p_mfc_hw_call(dev->mfc_ops, get_crop_info_h, ctx);
804 		right = left >> S5P_FIMV_SHARED_CROP_RIGHT_SHIFT;
805 		left = left & S5P_FIMV_SHARED_CROP_LEFT_MASK;
806 		top = s5p_mfc_hw_call(dev->mfc_ops, get_crop_info_v, ctx);
807 		bottom = top >> S5P_FIMV_SHARED_CROP_BOTTOM_SHIFT;
808 		top = top & S5P_FIMV_SHARED_CROP_TOP_MASK;
809 		width = ctx->img_width - left - right;
810 		height = ctx->img_height - top - bottom;
811 		mfc_debug(2, "Composing info [h264]: l=%d t=%d w=%d h=%d (r=%d b=%d fw=%d fh=%d\n",
812 			  left, top, s->r.width, s->r.height, right, bottom,
813 			  ctx->buf_width, ctx->buf_height);
814 	} else {
815 		left = 0;
816 		top = 0;
817 		width = ctx->img_width;
818 		height = ctx->img_height;
819 		mfc_debug(2, "Composing info: w=%d h=%d fw=%d fh=%d\n",
820 			  s->r.width, s->r.height, ctx->buf_width,
821 			  ctx->buf_height);
822 	}
823 
824 	switch (s->target) {
825 	case V4L2_SEL_TGT_COMPOSE:
826 	case V4L2_SEL_TGT_COMPOSE_DEFAULT:
827 	case V4L2_SEL_TGT_COMPOSE_BOUNDS:
828 		s->r.left = left;
829 		s->r.top = top;
830 		s->r.width = width;
831 		s->r.height = height;
832 		break;
833 	default:
834 		return -EINVAL;
835 	}
836 	return 0;
837 }
838 
839 static int vidioc_decoder_cmd(struct file *file, void *priv,
840 			      struct v4l2_decoder_cmd *cmd)
841 {
842 	struct s5p_mfc_ctx *ctx = fh_to_ctx(priv);
843 	struct s5p_mfc_dev *dev = ctx->dev;
844 	struct s5p_mfc_buf *buf;
845 	unsigned long flags;
846 
847 	switch (cmd->cmd) {
848 	case V4L2_DEC_CMD_STOP:
849 		if (cmd->flags != 0)
850 			return -EINVAL;
851 
852 		if (!vb2_is_streaming(&ctx->vq_src))
853 			return -EINVAL;
854 
855 		spin_lock_irqsave(&dev->irqlock, flags);
856 		if (list_empty(&ctx->src_queue)) {
857 			mfc_err("EOS: empty src queue, entering finishing state");
858 			ctx->state = MFCINST_FINISHING;
859 			if (s5p_mfc_ctx_ready(ctx))
860 				set_work_bit_irqsave(ctx);
861 			spin_unlock_irqrestore(&dev->irqlock, flags);
862 			s5p_mfc_hw_call(dev->mfc_ops, try_run, dev);
863 		} else {
864 			mfc_err("EOS: marking last buffer of stream");
865 			buf = list_entry(ctx->src_queue.prev,
866 						struct s5p_mfc_buf, list);
867 			if (buf->flags & MFC_BUF_FLAG_USED)
868 				ctx->state = MFCINST_FINISHING;
869 			else
870 				buf->flags |= MFC_BUF_FLAG_EOS;
871 			spin_unlock_irqrestore(&dev->irqlock, flags);
872 		}
873 		break;
874 	default:
875 		return -EINVAL;
876 	}
877 	return 0;
878 }
879 
880 static int vidioc_subscribe_event(struct v4l2_fh *fh,
881 				const struct  v4l2_event_subscription *sub)
882 {
883 	switch (sub->type) {
884 	case V4L2_EVENT_EOS:
885 		return v4l2_event_subscribe(fh, sub, 2, NULL);
886 	case V4L2_EVENT_SOURCE_CHANGE:
887 		return v4l2_src_change_event_subscribe(fh, sub);
888 	default:
889 		return -EINVAL;
890 	}
891 }
892 
893 
894 /* v4l2_ioctl_ops */
895 static const struct v4l2_ioctl_ops s5p_mfc_dec_ioctl_ops = {
896 	.vidioc_querycap = vidioc_querycap,
897 	.vidioc_enum_fmt_vid_cap = vidioc_enum_fmt_vid_cap,
898 	.vidioc_enum_fmt_vid_out = vidioc_enum_fmt_vid_out,
899 	.vidioc_g_fmt_vid_cap_mplane = vidioc_g_fmt,
900 	.vidioc_g_fmt_vid_out_mplane = vidioc_g_fmt,
901 	.vidioc_try_fmt_vid_cap_mplane = vidioc_try_fmt,
902 	.vidioc_try_fmt_vid_out_mplane = vidioc_try_fmt,
903 	.vidioc_s_fmt_vid_cap_mplane = vidioc_s_fmt,
904 	.vidioc_s_fmt_vid_out_mplane = vidioc_s_fmt,
905 	.vidioc_reqbufs = vidioc_reqbufs,
906 	.vidioc_querybuf = vidioc_querybuf,
907 	.vidioc_qbuf = vidioc_qbuf,
908 	.vidioc_dqbuf = vidioc_dqbuf,
909 	.vidioc_expbuf = vidioc_expbuf,
910 	.vidioc_streamon = vidioc_streamon,
911 	.vidioc_streamoff = vidioc_streamoff,
912 	.vidioc_g_selection = vidioc_g_selection,
913 	.vidioc_decoder_cmd = vidioc_decoder_cmd,
914 	.vidioc_subscribe_event = vidioc_subscribe_event,
915 	.vidioc_unsubscribe_event = v4l2_event_unsubscribe,
916 };
917 
918 static int s5p_mfc_queue_setup(struct vb2_queue *vq,
919 			unsigned int *buf_count,
920 			unsigned int *plane_count, unsigned int psize[],
921 			struct device *alloc_devs[])
922 {
923 	struct s5p_mfc_ctx *ctx = fh_to_ctx(vq->drv_priv);
924 	struct s5p_mfc_dev *dev = ctx->dev;
925 
926 	/* Video output for decoding (source)
927 	 * this can be set after getting an instance */
928 	if (ctx->state == MFCINST_INIT &&
929 	    vq->type == V4L2_BUF_TYPE_VIDEO_OUTPUT_MPLANE) {
930 		/* A single plane is required for input */
931 		*plane_count = 1;
932 		if (*buf_count < 1)
933 			*buf_count = 1;
934 		if (*buf_count > MFC_MAX_BUFFERS)
935 			*buf_count = MFC_MAX_BUFFERS;
936 	/* Video capture for decoding (destination)
937 	 * this can be set after the header was parsed */
938 	} else if (ctx->state == MFCINST_HEAD_PARSED &&
939 		   vq->type == V4L2_BUF_TYPE_VIDEO_CAPTURE_MPLANE) {
940 		/* Output plane count is 2 - one for Y and one for CbCr */
941 		*plane_count = 2;
942 		/* Setup buffer count */
943 		if (*buf_count < ctx->pb_count)
944 			*buf_count = ctx->pb_count;
945 		if (*buf_count > ctx->pb_count + MFC_MAX_EXTRA_DPB)
946 			*buf_count = ctx->pb_count + MFC_MAX_EXTRA_DPB;
947 		if (*buf_count > MFC_MAX_BUFFERS)
948 			*buf_count = MFC_MAX_BUFFERS;
949 	} else {
950 		mfc_err("State seems invalid. State = %d, vq->type = %d\n",
951 							ctx->state, vq->type);
952 		return -EINVAL;
953 	}
954 	mfc_debug(2, "Buffer count=%d, plane count=%d\n",
955 						*buf_count, *plane_count);
956 	if (ctx->state == MFCINST_HEAD_PARSED &&
957 	    vq->type == V4L2_BUF_TYPE_VIDEO_CAPTURE_MPLANE) {
958 		psize[0] = ctx->luma_size;
959 		psize[1] = ctx->chroma_size;
960 
961 		if (IS_MFCV6_PLUS(dev))
962 			alloc_devs[0] = ctx->dev->mem_dev[BANK_L_CTX];
963 		else
964 			alloc_devs[0] = ctx->dev->mem_dev[BANK_R_CTX];
965 		alloc_devs[1] = ctx->dev->mem_dev[BANK_L_CTX];
966 	} else if (vq->type == V4L2_BUF_TYPE_VIDEO_OUTPUT_MPLANE &&
967 		   ctx->state == MFCINST_INIT) {
968 		psize[0] = ctx->dec_src_buf_size;
969 		alloc_devs[0] = ctx->dev->mem_dev[BANK_L_CTX];
970 	} else {
971 		mfc_err("This video node is dedicated to decoding. Decoding not initialized\n");
972 		return -EINVAL;
973 	}
974 	return 0;
975 }
976 
977 static int s5p_mfc_buf_init(struct vb2_buffer *vb)
978 {
979 	struct vb2_v4l2_buffer *vbuf = to_vb2_v4l2_buffer(vb);
980 	struct vb2_queue *vq = vb->vb2_queue;
981 	struct s5p_mfc_ctx *ctx = fh_to_ctx(vq->drv_priv);
982 	unsigned int i;
983 
984 	if (vq->type == V4L2_BUF_TYPE_VIDEO_CAPTURE_MPLANE) {
985 		if (ctx->capture_state == QUEUE_BUFS_MMAPED)
986 			return 0;
987 		for (i = 0; i < ctx->dst_fmt->num_planes; i++) {
988 			if (IS_ERR_OR_NULL(ERR_PTR(
989 					vb2_dma_contig_plane_dma_addr(vb, i)))) {
990 				mfc_err("Plane mem not allocated\n");
991 				return -EINVAL;
992 			}
993 		}
994 		if (vb2_plane_size(vb, 0) < ctx->luma_size ||
995 			vb2_plane_size(vb, 1) < ctx->chroma_size) {
996 			mfc_err("Plane buffer (CAPTURE) is too small\n");
997 			return -EINVAL;
998 		}
999 		i = vb->index;
1000 		ctx->dst_bufs[i].b = vbuf;
1001 		ctx->dst_bufs[i].cookie.raw.luma =
1002 					vb2_dma_contig_plane_dma_addr(vb, 0);
1003 		ctx->dst_bufs[i].cookie.raw.chroma =
1004 					vb2_dma_contig_plane_dma_addr(vb, 1);
1005 		ctx->dst_bufs_cnt++;
1006 	} else if (vq->type == V4L2_BUF_TYPE_VIDEO_OUTPUT_MPLANE) {
1007 		if (IS_ERR_OR_NULL(ERR_PTR(
1008 					vb2_dma_contig_plane_dma_addr(vb, 0)))) {
1009 			mfc_err("Plane memory not allocated\n");
1010 			return -EINVAL;
1011 		}
1012 		if (vb2_plane_size(vb, 0) < ctx->dec_src_buf_size) {
1013 			mfc_err("Plane buffer (OUTPUT) is too small\n");
1014 			return -EINVAL;
1015 		}
1016 
1017 		i = vb->index;
1018 		ctx->src_bufs[i].b = vbuf;
1019 		ctx->src_bufs[i].cookie.stream =
1020 					vb2_dma_contig_plane_dma_addr(vb, 0);
1021 		ctx->src_bufs_cnt++;
1022 	} else {
1023 		mfc_err("s5p_mfc_buf_init: unknown queue type\n");
1024 		return -EINVAL;
1025 	}
1026 	return 0;
1027 }
1028 
1029 static int s5p_mfc_start_streaming(struct vb2_queue *q, unsigned int count)
1030 {
1031 	struct s5p_mfc_ctx *ctx = fh_to_ctx(q->drv_priv);
1032 	struct s5p_mfc_dev *dev = ctx->dev;
1033 
1034 	v4l2_ctrl_handler_setup(&ctx->ctrl_handler);
1035 	if (ctx->state == MFCINST_FINISHING ||
1036 		ctx->state == MFCINST_FINISHED)
1037 		ctx->state = MFCINST_RUNNING;
1038 	/* If context is ready then dev = work->data;schedule it to run */
1039 	if (s5p_mfc_ctx_ready(ctx))
1040 		set_work_bit_irqsave(ctx);
1041 	s5p_mfc_hw_call(dev->mfc_ops, try_run, dev);
1042 	return 0;
1043 }
1044 
1045 static void s5p_mfc_stop_streaming(struct vb2_queue *q)
1046 {
1047 	unsigned long flags;
1048 	struct s5p_mfc_ctx *ctx = fh_to_ctx(q->drv_priv);
1049 	struct s5p_mfc_dev *dev = ctx->dev;
1050 	int aborted = 0;
1051 
1052 	spin_lock_irqsave(&dev->irqlock, flags);
1053 	if ((ctx->state == MFCINST_FINISHING ||
1054 		ctx->state ==  MFCINST_RUNNING) &&
1055 		dev->curr_ctx == ctx->num && dev->hw_lock) {
1056 		ctx->state = MFCINST_ABORT;
1057 		spin_unlock_irqrestore(&dev->irqlock, flags);
1058 		s5p_mfc_wait_for_done_ctx(ctx,
1059 					S5P_MFC_R2H_CMD_FRAME_DONE_RET, 0);
1060 		aborted = 1;
1061 		spin_lock_irqsave(&dev->irqlock, flags);
1062 	}
1063 	if (q->type == V4L2_BUF_TYPE_VIDEO_CAPTURE_MPLANE) {
1064 		s5p_mfc_cleanup_queue(&ctx->dst_queue, &ctx->vq_dst);
1065 		INIT_LIST_HEAD(&ctx->dst_queue);
1066 		ctx->dst_queue_cnt = 0;
1067 		ctx->dpb_flush_flag = 1;
1068 		ctx->dec_dst_flag = 0;
1069 		if (IS_MFCV6_PLUS(dev) && (ctx->state == MFCINST_RUNNING)) {
1070 			ctx->state = MFCINST_FLUSH;
1071 			set_work_bit_irqsave(ctx);
1072 			s5p_mfc_hw_call(dev->mfc_ops, try_run, dev);
1073 			spin_unlock_irqrestore(&dev->irqlock, flags);
1074 			if (s5p_mfc_wait_for_done_ctx(ctx,
1075 				S5P_MFC_R2H_CMD_DPB_FLUSH_RET, 0))
1076 				mfc_err("Err flushing buffers\n");
1077 			spin_lock_irqsave(&dev->irqlock, flags);
1078 		}
1079 	} else if (q->type == V4L2_BUF_TYPE_VIDEO_OUTPUT_MPLANE) {
1080 		s5p_mfc_cleanup_queue(&ctx->src_queue, &ctx->vq_src);
1081 		INIT_LIST_HEAD(&ctx->src_queue);
1082 		ctx->src_queue_cnt = 0;
1083 	}
1084 	if (aborted)
1085 		ctx->state = MFCINST_RUNNING;
1086 	spin_unlock_irqrestore(&dev->irqlock, flags);
1087 }
1088 
1089 
1090 static void s5p_mfc_buf_queue(struct vb2_buffer *vb)
1091 {
1092 	struct vb2_queue *vq = vb->vb2_queue;
1093 	struct s5p_mfc_ctx *ctx = fh_to_ctx(vq->drv_priv);
1094 	struct s5p_mfc_dev *dev = ctx->dev;
1095 	unsigned long flags;
1096 	struct s5p_mfc_buf *mfc_buf;
1097 
1098 	if (vq->type == V4L2_BUF_TYPE_VIDEO_OUTPUT_MPLANE) {
1099 		mfc_buf = &ctx->src_bufs[vb->index];
1100 		mfc_buf->flags &= ~MFC_BUF_FLAG_USED;
1101 		spin_lock_irqsave(&dev->irqlock, flags);
1102 		list_add_tail(&mfc_buf->list, &ctx->src_queue);
1103 		ctx->src_queue_cnt++;
1104 		spin_unlock_irqrestore(&dev->irqlock, flags);
1105 	} else if (vq->type == V4L2_BUF_TYPE_VIDEO_CAPTURE_MPLANE) {
1106 		mfc_buf = &ctx->dst_bufs[vb->index];
1107 		mfc_buf->flags &= ~MFC_BUF_FLAG_USED;
1108 		/* Mark destination as available for use by MFC */
1109 		spin_lock_irqsave(&dev->irqlock, flags);
1110 		set_bit(vb->index, &ctx->dec_dst_flag);
1111 		list_add_tail(&mfc_buf->list, &ctx->dst_queue);
1112 		ctx->dst_queue_cnt++;
1113 		spin_unlock_irqrestore(&dev->irqlock, flags);
1114 	} else {
1115 		mfc_err("Unsupported buffer type (%d)\n", vq->type);
1116 	}
1117 	if (s5p_mfc_ctx_ready(ctx))
1118 		set_work_bit_irqsave(ctx);
1119 	s5p_mfc_hw_call(dev->mfc_ops, try_run, dev);
1120 }
1121 
1122 static struct vb2_ops s5p_mfc_dec_qops = {
1123 	.queue_setup		= s5p_mfc_queue_setup,
1124 	.wait_prepare		= vb2_ops_wait_prepare,
1125 	.wait_finish		= vb2_ops_wait_finish,
1126 	.buf_init		= s5p_mfc_buf_init,
1127 	.start_streaming	= s5p_mfc_start_streaming,
1128 	.stop_streaming		= s5p_mfc_stop_streaming,
1129 	.buf_queue		= s5p_mfc_buf_queue,
1130 };
1131 
1132 const struct s5p_mfc_codec_ops *get_dec_codec_ops(void)
1133 {
1134 	return &decoder_codec_ops;
1135 }
1136 
1137 struct vb2_ops *get_dec_queue_ops(void)
1138 {
1139 	return &s5p_mfc_dec_qops;
1140 }
1141 
1142 const struct v4l2_ioctl_ops *get_dec_v4l2_ioctl_ops(void)
1143 {
1144 	return &s5p_mfc_dec_ioctl_ops;
1145 }
1146 
1147 #define IS_MFC51_PRIV(x) ((V4L2_CTRL_ID2WHICH(x) == V4L2_CTRL_CLASS_CODEC) \
1148 						&& V4L2_CTRL_DRIVER_PRIV(x))
1149 
1150 int s5p_mfc_dec_ctrls_setup(struct s5p_mfc_ctx *ctx)
1151 {
1152 	struct v4l2_ctrl_config cfg;
1153 	int i;
1154 
1155 	v4l2_ctrl_handler_init(&ctx->ctrl_handler, NUM_CTRLS);
1156 	if (ctx->ctrl_handler.error) {
1157 		mfc_err("v4l2_ctrl_handler_init failed\n");
1158 		return ctx->ctrl_handler.error;
1159 	}
1160 
1161 	for (i = 0; i < NUM_CTRLS; i++) {
1162 		if (IS_MFC51_PRIV(controls[i].id)) {
1163 			memset(&cfg, 0, sizeof(struct v4l2_ctrl_config));
1164 			cfg.ops = &s5p_mfc_dec_ctrl_ops;
1165 			cfg.id = controls[i].id;
1166 			cfg.min = controls[i].minimum;
1167 			cfg.max = controls[i].maximum;
1168 			cfg.def = controls[i].default_value;
1169 			cfg.name = controls[i].name;
1170 			cfg.type = controls[i].type;
1171 
1172 			cfg.step = controls[i].step;
1173 			cfg.menu_skip_mask = 0;
1174 
1175 			ctx->ctrls[i] = v4l2_ctrl_new_custom(&ctx->ctrl_handler,
1176 					&cfg, NULL);
1177 		} else {
1178 			ctx->ctrls[i] = v4l2_ctrl_new_std(&ctx->ctrl_handler,
1179 					&s5p_mfc_dec_ctrl_ops,
1180 					controls[i].id, controls[i].minimum,
1181 					controls[i].maximum, controls[i].step,
1182 					controls[i].default_value);
1183 		}
1184 		if (ctx->ctrl_handler.error) {
1185 			mfc_err("Adding control (%d) failed\n", i);
1186 			return ctx->ctrl_handler.error;
1187 		}
1188 		if (controls[i].is_volatile && ctx->ctrls[i])
1189 			ctx->ctrls[i]->flags |= V4L2_CTRL_FLAG_VOLATILE;
1190 	}
1191 	return 0;
1192 }
1193 
1194 void s5p_mfc_dec_ctrls_delete(struct s5p_mfc_ctx *ctx)
1195 {
1196 	int i;
1197 
1198 	v4l2_ctrl_handler_free(&ctx->ctrl_handler);
1199 	for (i = 0; i < NUM_CTRLS; i++)
1200 		ctx->ctrls[i] = NULL;
1201 }
1202 
1203 void s5p_mfc_dec_init(struct s5p_mfc_ctx *ctx)
1204 {
1205 	struct v4l2_format f;
1206 	f.fmt.pix_mp.pixelformat = V4L2_PIX_FMT_H264;
1207 	ctx->src_fmt = find_format(&f, MFC_FMT_DEC);
1208 	if (IS_MFCV8_PLUS(ctx->dev))
1209 		f.fmt.pix_mp.pixelformat = V4L2_PIX_FMT_NV12M;
1210 	else if (IS_MFCV6_PLUS(ctx->dev))
1211 		f.fmt.pix_mp.pixelformat = V4L2_PIX_FMT_NV12MT_16X16;
1212 	else
1213 		f.fmt.pix_mp.pixelformat = V4L2_PIX_FMT_NV12MT;
1214 	ctx->dst_fmt = find_format(&f, MFC_FMT_RAW);
1215 	mfc_debug(2, "Default src_fmt is %p, dest_fmt is %p\n",
1216 			ctx->src_fmt, ctx->dst_fmt);
1217 }
1218 
1219