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