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