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