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