s5p_mfc_dec.c revision 19c2813c2882c7ecc6e1e8813f54ace495cfbe9a
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) && (fmt->fourcc == V4L2_PIX_FMT_VP8)) {
390			mfc_err("Not supported format.\n");
391			return -EINVAL;
392		}
393		if (!IS_MFCV6_PLUS(dev)) {
394			if (fmt->fourcc == V4L2_PIX_FMT_VP8) {
395				mfc_err("Not supported format.\n");
396				return -EINVAL;
397			}
398		}
399	} else if (f->type == V4L2_BUF_TYPE_VIDEO_CAPTURE_MPLANE) {
400		fmt = find_format(f, MFC_FMT_RAW);
401		if (!fmt) {
402			mfc_err("Unsupported format for destination.\n");
403			return -EINVAL;
404		}
405		if (IS_MFCV6_PLUS(dev) &&
406				(fmt->fourcc == V4L2_PIX_FMT_NV12MT)) {
407			mfc_err("Not supported format.\n");
408			return -EINVAL;
409		} else if (!IS_MFCV6_PLUS(dev) &&
410				(fmt->fourcc != V4L2_PIX_FMT_NV12MT)) {
411			mfc_err("Not supported format.\n");
412			return -EINVAL;
413		}
414	}
415
416	return 0;
417}
418
419/* Set format */
420static int vidioc_s_fmt(struct file *file, void *priv, struct v4l2_format *f)
421{
422	struct s5p_mfc_dev *dev = video_drvdata(file);
423	struct s5p_mfc_ctx *ctx = fh_to_ctx(priv);
424	int ret = 0;
425	struct v4l2_pix_format_mplane *pix_mp;
426
427	mfc_debug_enter();
428	ret = vidioc_try_fmt(file, priv, f);
429	pix_mp = &f->fmt.pix_mp;
430	if (ret)
431		return ret;
432	if (ctx->vq_src.streaming || ctx->vq_dst.streaming) {
433		v4l2_err(&dev->v4l2_dev, "%s queue busy\n", __func__);
434		ret = -EBUSY;
435		goto out;
436	}
437	if (f->type == V4L2_BUF_TYPE_VIDEO_CAPTURE_MPLANE) {
438		/* dst_fmt is validated by call to vidioc_try_fmt */
439		ctx->dst_fmt = find_format(f, MFC_FMT_RAW);
440		ret = 0;
441		goto out;
442	} else if (f->type == V4L2_BUF_TYPE_VIDEO_OUTPUT_MPLANE) {
443		/* src_fmt is validated by call to vidioc_try_fmt */
444		ctx->src_fmt = find_format(f, MFC_FMT_DEC);
445		ctx->codec_mode = ctx->src_fmt->codec_mode;
446		mfc_debug(2, "The codec number is: %d\n", ctx->codec_mode);
447		pix_mp->height = 0;
448		pix_mp->width = 0;
449		if (pix_mp->plane_fmt[0].sizeimage)
450			ctx->dec_src_buf_size = pix_mp->plane_fmt[0].sizeimage;
451		else
452			pix_mp->plane_fmt[0].sizeimage = ctx->dec_src_buf_size =
453								DEF_CPB_SIZE;
454		pix_mp->plane_fmt[0].bytesperline = 0;
455		ctx->state = MFCINST_INIT;
456		ret = 0;
457		goto out;
458	} else {
459		mfc_err("Wrong type error for S_FMT : %d", f->type);
460		ret = -EINVAL;
461		goto out;
462	}
463
464out:
465	mfc_debug_leave();
466	return ret;
467}
468
469/* Reqeust buffers */
470static int vidioc_reqbufs(struct file *file, void *priv,
471					  struct v4l2_requestbuffers *reqbufs)
472{
473	struct s5p_mfc_dev *dev = video_drvdata(file);
474	struct s5p_mfc_ctx *ctx = fh_to_ctx(priv);
475	int ret = 0;
476
477	if (reqbufs->memory != V4L2_MEMORY_MMAP) {
478		mfc_err("Only V4L2_MEMORY_MAP is supported\n");
479		return -EINVAL;
480	}
481	if (reqbufs->type == V4L2_BUF_TYPE_VIDEO_OUTPUT_MPLANE) {
482		/* Can only request buffers after an instance has been opened.*/
483		if (ctx->state == MFCINST_INIT) {
484			ctx->src_bufs_cnt = 0;
485			if (reqbufs->count == 0) {
486				mfc_debug(2, "Freeing buffers\n");
487				s5p_mfc_clock_on();
488				ret = vb2_reqbufs(&ctx->vq_src, reqbufs);
489				s5p_mfc_clock_off();
490				return ret;
491			}
492			/* Decoding */
493			if (ctx->output_state != QUEUE_FREE) {
494				mfc_err("Bufs have already been requested\n");
495				return -EINVAL;
496			}
497			s5p_mfc_clock_on();
498			ret = vb2_reqbufs(&ctx->vq_src, reqbufs);
499			s5p_mfc_clock_off();
500			if (ret) {
501				mfc_err("vb2_reqbufs on output failed\n");
502				return ret;
503			}
504			mfc_debug(2, "vb2_reqbufs: %d\n", ret);
505			ctx->output_state = QUEUE_BUFS_REQUESTED;
506		}
507	} else if (reqbufs->type == V4L2_BUF_TYPE_VIDEO_CAPTURE_MPLANE) {
508		ctx->dst_bufs_cnt = 0;
509		if (reqbufs->count == 0) {
510			mfc_debug(2, "Freeing buffers\n");
511			s5p_mfc_clock_on();
512			ret = vb2_reqbufs(&ctx->vq_dst, reqbufs);
513			s5p_mfc_clock_off();
514			return ret;
515		}
516		if (ctx->capture_state != QUEUE_FREE) {
517			mfc_err("Bufs have already been requested\n");
518			return -EINVAL;
519		}
520		ctx->capture_state = QUEUE_BUFS_REQUESTED;
521		s5p_mfc_clock_on();
522		ret = vb2_reqbufs(&ctx->vq_dst, reqbufs);
523		s5p_mfc_clock_off();
524		if (ret) {
525			mfc_err("vb2_reqbufs on capture failed\n");
526			return ret;
527		}
528		if (reqbufs->count < ctx->pb_count) {
529			mfc_err("Not enough buffers allocated\n");
530			reqbufs->count = 0;
531			s5p_mfc_clock_on();
532			ret = vb2_reqbufs(&ctx->vq_dst, reqbufs);
533			s5p_mfc_clock_off();
534			return -ENOMEM;
535		}
536		ctx->total_dpb_count = reqbufs->count;
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			s5p_mfc_clock_on();
542			ret = vb2_reqbufs(&ctx->vq_dst, reqbufs);
543			s5p_mfc_clock_off();
544			return -ENOMEM;
545		}
546		if (ctx->dst_bufs_cnt == ctx->total_dpb_count) {
547			ctx->capture_state = QUEUE_BUFS_MMAPED;
548		} else {
549			mfc_err("Not all buffers passed to buf_init\n");
550			reqbufs->count = 0;
551			s5p_mfc_clock_on();
552			ret = vb2_reqbufs(&ctx->vq_dst, reqbufs);
553			s5p_mfc_hw_call(dev->mfc_ops, release_codec_buffers,
554					ctx);
555			s5p_mfc_clock_off();
556			return -ENOMEM;
557		}
558		if (s5p_mfc_ctx_ready(ctx))
559			set_work_bit_irqsave(ctx);
560		s5p_mfc_hw_call(dev->mfc_ops, try_run, dev);
561		s5p_mfc_wait_for_done_ctx(ctx,
562					S5P_MFC_R2H_CMD_INIT_BUFFERS_RET, 0);
563	}
564	return ret;
565}
566
567/* Query buffer */
568static int vidioc_querybuf(struct file *file, void *priv,
569						   struct v4l2_buffer *buf)
570{
571	struct s5p_mfc_ctx *ctx = fh_to_ctx(priv);
572	int ret;
573	int i;
574
575	if (buf->memory != V4L2_MEMORY_MMAP) {
576		mfc_err("Only mmaped buffers can be used\n");
577		return -EINVAL;
578	}
579	mfc_debug(2, "State: %d, buf->type: %d\n", ctx->state, buf->type);
580	if (ctx->state == MFCINST_INIT &&
581			buf->type == V4L2_BUF_TYPE_VIDEO_OUTPUT_MPLANE) {
582		ret = vb2_querybuf(&ctx->vq_src, buf);
583	} else if (ctx->state == MFCINST_RUNNING &&
584			buf->type == V4L2_BUF_TYPE_VIDEO_CAPTURE_MPLANE) {
585		ret = vb2_querybuf(&ctx->vq_dst, buf);
586		for (i = 0; i < buf->length; i++)
587			buf->m.planes[i].m.mem_offset += DST_QUEUE_OFF_BASE;
588	} else {
589		mfc_err("vidioc_querybuf called in an inappropriate state\n");
590		ret = -EINVAL;
591	}
592	mfc_debug_leave();
593	return ret;
594}
595
596/* Queue a buffer */
597static int vidioc_qbuf(struct file *file, void *priv, struct v4l2_buffer *buf)
598{
599	struct s5p_mfc_ctx *ctx = fh_to_ctx(priv);
600
601	if (ctx->state == MFCINST_ERROR) {
602		mfc_err("Call on QBUF after unrecoverable error\n");
603		return -EIO;
604	}
605	if (buf->type == V4L2_BUF_TYPE_VIDEO_OUTPUT_MPLANE)
606		return vb2_qbuf(&ctx->vq_src, buf);
607	else if (buf->type == V4L2_BUF_TYPE_VIDEO_CAPTURE_MPLANE)
608		return vb2_qbuf(&ctx->vq_dst, buf);
609	return -EINVAL;
610}
611
612/* Dequeue a buffer */
613static int vidioc_dqbuf(struct file *file, void *priv, struct v4l2_buffer *buf)
614{
615	const struct v4l2_event ev = {
616		.type = V4L2_EVENT_EOS
617	};
618	struct s5p_mfc_ctx *ctx = fh_to_ctx(priv);
619	int ret;
620
621	if (ctx->state == MFCINST_ERROR) {
622		mfc_err("Call on DQBUF after unrecoverable error\n");
623		return -EIO;
624	}
625	if (buf->type == V4L2_BUF_TYPE_VIDEO_OUTPUT_MPLANE)
626		ret = vb2_dqbuf(&ctx->vq_src, buf, file->f_flags & O_NONBLOCK);
627	else if (buf->type == V4L2_BUF_TYPE_VIDEO_CAPTURE_MPLANE) {
628		ret = vb2_dqbuf(&ctx->vq_dst, buf, file->f_flags & O_NONBLOCK);
629		if (ret == 0 && ctx->state == MFCINST_FINISHED &&
630				list_empty(&ctx->vq_dst.done_list))
631			v4l2_event_queue_fh(&ctx->fh, &ev);
632	} else {
633		ret = -EINVAL;
634	}
635	return ret;
636}
637
638/* Export DMA buffer */
639static int vidioc_expbuf(struct file *file, void *priv,
640	struct v4l2_exportbuffer *eb)
641{
642	struct s5p_mfc_ctx *ctx = fh_to_ctx(priv);
643
644	if (eb->type == V4L2_BUF_TYPE_VIDEO_OUTPUT_MPLANE)
645		return vb2_expbuf(&ctx->vq_src, eb);
646	if (eb->type == V4L2_BUF_TYPE_VIDEO_CAPTURE_MPLANE)
647		return vb2_expbuf(&ctx->vq_dst, eb);
648	return -EINVAL;
649}
650
651/* Stream on */
652static int vidioc_streamon(struct file *file, void *priv,
653			   enum v4l2_buf_type type)
654{
655	struct s5p_mfc_ctx *ctx = fh_to_ctx(priv);
656	struct s5p_mfc_dev *dev = ctx->dev;
657	int ret = -EINVAL;
658
659	mfc_debug_enter();
660	if (type == V4L2_BUF_TYPE_VIDEO_OUTPUT_MPLANE) {
661
662		if (ctx->state == MFCINST_INIT) {
663			ctx->dst_bufs_cnt = 0;
664			ctx->src_bufs_cnt = 0;
665			ctx->capture_state = QUEUE_FREE;
666			ctx->output_state = QUEUE_FREE;
667			s5p_mfc_hw_call(dev->mfc_ops, alloc_instance_buffer,
668					ctx);
669			s5p_mfc_hw_call(dev->mfc_ops, alloc_dec_temp_buffers,
670					ctx);
671			set_work_bit_irqsave(ctx);
672			s5p_mfc_clean_ctx_int_flags(ctx);
673			s5p_mfc_hw_call(dev->mfc_ops, try_run, dev);
674
675			if (s5p_mfc_wait_for_done_ctx(ctx,
676				S5P_MFC_R2H_CMD_OPEN_INSTANCE_RET, 0)) {
677				/* Error or timeout */
678				mfc_err("Error getting instance from hardware\n");
679				s5p_mfc_hw_call(dev->mfc_ops,
680						release_instance_buffer, ctx);
681				s5p_mfc_hw_call(dev->mfc_ops,
682						release_dec_desc_buffer, ctx);
683				return -EIO;
684			}
685			mfc_debug(2, "Got instance number: %d\n", ctx->inst_no);
686		}
687		ret = vb2_streamon(&ctx->vq_src, type);
688		}
689	else if (type == V4L2_BUF_TYPE_VIDEO_CAPTURE_MPLANE)
690		ret = vb2_streamon(&ctx->vq_dst, type);
691	mfc_debug_leave();
692	return ret;
693}
694
695/* Stream off, which equals to a pause */
696static int vidioc_streamoff(struct file *file, void *priv,
697			    enum v4l2_buf_type type)
698{
699	struct s5p_mfc_ctx *ctx = fh_to_ctx(priv);
700
701	if (type == V4L2_BUF_TYPE_VIDEO_OUTPUT_MPLANE)
702		return vb2_streamoff(&ctx->vq_src, type);
703	else if (type == V4L2_BUF_TYPE_VIDEO_CAPTURE_MPLANE)
704		return vb2_streamoff(&ctx->vq_dst, type);
705	return -EINVAL;
706}
707
708/* Set controls - v4l2 control framework */
709static int s5p_mfc_dec_s_ctrl(struct v4l2_ctrl *ctrl)
710{
711	struct s5p_mfc_ctx *ctx = ctrl_to_ctx(ctrl);
712
713	switch (ctrl->id) {
714	case V4L2_CID_MPEG_MFC51_VIDEO_DECODER_H264_DISPLAY_DELAY:
715		ctx->display_delay = ctrl->val;
716		break;
717	case V4L2_CID_MPEG_MFC51_VIDEO_DECODER_H264_DISPLAY_DELAY_ENABLE:
718		ctx->display_delay_enable = ctrl->val;
719		break;
720	case V4L2_CID_MPEG_VIDEO_DECODER_MPEG4_DEBLOCK_FILTER:
721		ctx->loop_filter_mpeg4 = ctrl->val;
722		break;
723	case V4L2_CID_MPEG_VIDEO_DECODER_SLICE_INTERFACE:
724		ctx->slice_interface = ctrl->val;
725		break;
726	default:
727		mfc_err("Invalid control 0x%08x\n", ctrl->id);
728		return -EINVAL;
729	}
730	return 0;
731}
732
733static int s5p_mfc_dec_g_v_ctrl(struct v4l2_ctrl *ctrl)
734{
735	struct s5p_mfc_ctx *ctx = ctrl_to_ctx(ctrl);
736	struct s5p_mfc_dev *dev = ctx->dev;
737
738	switch (ctrl->id) {
739	case V4L2_CID_MIN_BUFFERS_FOR_CAPTURE:
740		if (ctx->state >= MFCINST_HEAD_PARSED &&
741		    ctx->state < MFCINST_ABORT) {
742			ctrl->val = ctx->pb_count;
743			break;
744		} else if (ctx->state != MFCINST_INIT) {
745			v4l2_err(&dev->v4l2_dev, "Decoding not initialised\n");
746			return -EINVAL;
747		}
748		/* Should wait for the header to be parsed */
749		s5p_mfc_clean_ctx_int_flags(ctx);
750		s5p_mfc_wait_for_done_ctx(ctx,
751				S5P_MFC_R2H_CMD_SEQ_DONE_RET, 0);
752		if (ctx->state >= MFCINST_HEAD_PARSED &&
753		    ctx->state < MFCINST_ABORT) {
754			ctrl->val = ctx->pb_count;
755		} else {
756			v4l2_err(&dev->v4l2_dev, "Decoding not initialised\n");
757			return -EINVAL;
758		}
759		break;
760	}
761	return 0;
762}
763
764
765static const struct v4l2_ctrl_ops s5p_mfc_dec_ctrl_ops = {
766	.s_ctrl = s5p_mfc_dec_s_ctrl,
767	.g_volatile_ctrl = s5p_mfc_dec_g_v_ctrl,
768};
769
770/* Get cropping information */
771static int vidioc_g_crop(struct file *file, void *priv,
772		struct v4l2_crop *cr)
773{
774	struct s5p_mfc_ctx *ctx = fh_to_ctx(priv);
775	struct s5p_mfc_dev *dev = ctx->dev;
776	u32 left, right, top, bottom;
777
778	if (ctx->state != MFCINST_HEAD_PARSED &&
779	ctx->state != MFCINST_RUNNING && ctx->state != MFCINST_FINISHING
780					&& ctx->state != MFCINST_FINISHED) {
781			mfc_err("Cannont set crop\n");
782			return -EINVAL;
783		}
784	if (ctx->src_fmt->fourcc == V4L2_PIX_FMT_H264) {
785		left = s5p_mfc_hw_call(dev->mfc_ops, get_crop_info_h, ctx);
786		right = left >> S5P_FIMV_SHARED_CROP_RIGHT_SHIFT;
787		left = left & S5P_FIMV_SHARED_CROP_LEFT_MASK;
788		top = s5p_mfc_hw_call(dev->mfc_ops, get_crop_info_v, ctx);
789		bottom = top >> S5P_FIMV_SHARED_CROP_BOTTOM_SHIFT;
790		top = top & S5P_FIMV_SHARED_CROP_TOP_MASK;
791		cr->c.left = left;
792		cr->c.top = top;
793		cr->c.width = ctx->img_width - left - right;
794		cr->c.height = ctx->img_height - top - bottom;
795		mfc_debug(2, "Cropping info [h264]: l=%d t=%d "
796			"w=%d h=%d (r=%d b=%d fw=%d fh=%d\n", left, top,
797			cr->c.width, cr->c.height, right, bottom,
798			ctx->buf_width, ctx->buf_height);
799	} else {
800		cr->c.left = 0;
801		cr->c.top = 0;
802		cr->c.width = ctx->img_width;
803		cr->c.height = ctx->img_height;
804		mfc_debug(2, "Cropping info: w=%d h=%d fw=%d "
805			"fh=%d\n", cr->c.width,	cr->c.height, ctx->buf_width,
806							ctx->buf_height);
807	}
808	return 0;
809}
810
811int vidioc_decoder_cmd(struct file *file, void *priv,
812						struct v4l2_decoder_cmd *cmd)
813{
814	struct s5p_mfc_ctx *ctx = fh_to_ctx(priv);
815	struct s5p_mfc_dev *dev = ctx->dev;
816	struct s5p_mfc_buf *buf;
817	unsigned long flags;
818
819	switch (cmd->cmd) {
820	case V4L2_ENC_CMD_STOP:
821		if (cmd->flags != 0)
822			return -EINVAL;
823
824		if (!ctx->vq_src.streaming)
825			return -EINVAL;
826
827		spin_lock_irqsave(&dev->irqlock, flags);
828		if (list_empty(&ctx->src_queue)) {
829			mfc_err("EOS: empty src queue, entering finishing state");
830			ctx->state = MFCINST_FINISHING;
831			if (s5p_mfc_ctx_ready(ctx))
832				set_work_bit_irqsave(ctx);
833			spin_unlock_irqrestore(&dev->irqlock, flags);
834			s5p_mfc_hw_call(dev->mfc_ops, try_run, dev);
835		} else {
836			mfc_err("EOS: marking last buffer of stream");
837			buf = list_entry(ctx->src_queue.prev,
838						struct s5p_mfc_buf, list);
839			if (buf->flags & MFC_BUF_FLAG_USED)
840				ctx->state = MFCINST_FINISHING;
841			else
842				buf->flags |= MFC_BUF_FLAG_EOS;
843			spin_unlock_irqrestore(&dev->irqlock, flags);
844		}
845		break;
846	default:
847		return -EINVAL;
848	}
849	return 0;
850}
851
852static int vidioc_subscribe_event(struct v4l2_fh *fh,
853				const struct  v4l2_event_subscription *sub)
854{
855	switch (sub->type) {
856	case V4L2_EVENT_EOS:
857		return v4l2_event_subscribe(fh, sub, 2, NULL);
858	default:
859		return -EINVAL;
860	}
861}
862
863
864/* v4l2_ioctl_ops */
865static const struct v4l2_ioctl_ops s5p_mfc_dec_ioctl_ops = {
866	.vidioc_querycap = vidioc_querycap,
867	.vidioc_enum_fmt_vid_cap = vidioc_enum_fmt_vid_cap,
868	.vidioc_enum_fmt_vid_cap_mplane = vidioc_enum_fmt_vid_cap_mplane,
869	.vidioc_enum_fmt_vid_out = vidioc_enum_fmt_vid_out,
870	.vidioc_enum_fmt_vid_out_mplane = vidioc_enum_fmt_vid_out_mplane,
871	.vidioc_g_fmt_vid_cap_mplane = vidioc_g_fmt,
872	.vidioc_g_fmt_vid_out_mplane = vidioc_g_fmt,
873	.vidioc_try_fmt_vid_cap_mplane = vidioc_try_fmt,
874	.vidioc_try_fmt_vid_out_mplane = vidioc_try_fmt,
875	.vidioc_s_fmt_vid_cap_mplane = vidioc_s_fmt,
876	.vidioc_s_fmt_vid_out_mplane = vidioc_s_fmt,
877	.vidioc_reqbufs = vidioc_reqbufs,
878	.vidioc_querybuf = vidioc_querybuf,
879	.vidioc_qbuf = vidioc_qbuf,
880	.vidioc_dqbuf = vidioc_dqbuf,
881	.vidioc_expbuf = vidioc_expbuf,
882	.vidioc_streamon = vidioc_streamon,
883	.vidioc_streamoff = vidioc_streamoff,
884	.vidioc_g_crop = vidioc_g_crop,
885	.vidioc_decoder_cmd = vidioc_decoder_cmd,
886	.vidioc_subscribe_event = vidioc_subscribe_event,
887	.vidioc_unsubscribe_event = v4l2_event_unsubscribe,
888};
889
890static int s5p_mfc_queue_setup(struct vb2_queue *vq,
891			const struct v4l2_format *fmt, unsigned int *buf_count,
892			unsigned int *plane_count, unsigned int psize[],
893			void *allocators[])
894{
895	struct s5p_mfc_ctx *ctx = fh_to_ctx(vq->drv_priv);
896	struct s5p_mfc_dev *dev = ctx->dev;
897
898	/* Video output for decoding (source)
899	 * this can be set after getting an instance */
900	if (ctx->state == MFCINST_INIT &&
901	    vq->type == V4L2_BUF_TYPE_VIDEO_OUTPUT_MPLANE) {
902		/* A single plane is required for input */
903		*plane_count = 1;
904		if (*buf_count < 1)
905			*buf_count = 1;
906		if (*buf_count > MFC_MAX_BUFFERS)
907			*buf_count = MFC_MAX_BUFFERS;
908	/* Video capture for decoding (destination)
909	 * this can be set after the header was parsed */
910	} else if (ctx->state == MFCINST_HEAD_PARSED &&
911		   vq->type == V4L2_BUF_TYPE_VIDEO_CAPTURE_MPLANE) {
912		/* Output plane count is 2 - one for Y and one for CbCr */
913		*plane_count = 2;
914		/* Setup buffer count */
915		if (*buf_count < ctx->pb_count)
916			*buf_count = ctx->pb_count;
917		if (*buf_count > ctx->pb_count + MFC_MAX_EXTRA_DPB)
918			*buf_count = ctx->pb_count + MFC_MAX_EXTRA_DPB;
919		if (*buf_count > MFC_MAX_BUFFERS)
920			*buf_count = MFC_MAX_BUFFERS;
921	} else {
922		mfc_err("State seems invalid. State = %d, vq->type = %d\n",
923							ctx->state, vq->type);
924		return -EINVAL;
925	}
926	mfc_debug(2, "Buffer count=%d, plane count=%d\n",
927						*buf_count, *plane_count);
928	if (ctx->state == MFCINST_HEAD_PARSED &&
929	    vq->type == V4L2_BUF_TYPE_VIDEO_CAPTURE_MPLANE) {
930		psize[0] = ctx->luma_size;
931		psize[1] = ctx->chroma_size;
932
933		if (IS_MFCV6_PLUS(dev))
934			allocators[0] =
935				ctx->dev->alloc_ctx[MFC_BANK1_ALLOC_CTX];
936		else
937			allocators[0] =
938				ctx->dev->alloc_ctx[MFC_BANK2_ALLOC_CTX];
939		allocators[1] = ctx->dev->alloc_ctx[MFC_BANK1_ALLOC_CTX];
940	} else if (vq->type == V4L2_BUF_TYPE_VIDEO_OUTPUT_MPLANE &&
941		   ctx->state == MFCINST_INIT) {
942		psize[0] = ctx->dec_src_buf_size;
943		allocators[0] = ctx->dev->alloc_ctx[MFC_BANK1_ALLOC_CTX];
944	} else {
945		mfc_err("This video node is dedicated to decoding. Decoding not initialized\n");
946		return -EINVAL;
947	}
948	return 0;
949}
950
951static void s5p_mfc_unlock(struct vb2_queue *q)
952{
953	struct s5p_mfc_ctx *ctx = fh_to_ctx(q->drv_priv);
954	struct s5p_mfc_dev *dev = ctx->dev;
955
956	mutex_unlock(&dev->mfc_mutex);
957}
958
959static void s5p_mfc_lock(struct vb2_queue *q)
960{
961	struct s5p_mfc_ctx *ctx = fh_to_ctx(q->drv_priv);
962	struct s5p_mfc_dev *dev = ctx->dev;
963
964	mutex_lock(&dev->mfc_mutex);
965}
966
967static int s5p_mfc_buf_init(struct vb2_buffer *vb)
968{
969	struct vb2_queue *vq = vb->vb2_queue;
970	struct s5p_mfc_ctx *ctx = fh_to_ctx(vq->drv_priv);
971	unsigned int i;
972
973	if (vq->type == V4L2_BUF_TYPE_VIDEO_CAPTURE_MPLANE) {
974		if (ctx->capture_state == QUEUE_BUFS_MMAPED)
975			return 0;
976		for (i = 0; i <= ctx->src_fmt->num_planes ; i++) {
977			if (IS_ERR_OR_NULL(ERR_PTR(
978					vb2_dma_contig_plane_dma_addr(vb, i)))) {
979				mfc_err("Plane mem not allocated\n");
980				return -EINVAL;
981			}
982		}
983		if (vb2_plane_size(vb, 0) < ctx->luma_size ||
984			vb2_plane_size(vb, 1) < ctx->chroma_size) {
985			mfc_err("Plane buffer (CAPTURE) is too small\n");
986			return -EINVAL;
987		}
988		i = vb->v4l2_buf.index;
989		ctx->dst_bufs[i].b = vb;
990		ctx->dst_bufs[i].cookie.raw.luma =
991					vb2_dma_contig_plane_dma_addr(vb, 0);
992		ctx->dst_bufs[i].cookie.raw.chroma =
993					vb2_dma_contig_plane_dma_addr(vb, 1);
994		ctx->dst_bufs_cnt++;
995	} else if (vq->type == V4L2_BUF_TYPE_VIDEO_OUTPUT_MPLANE) {
996		if (IS_ERR_OR_NULL(ERR_PTR(
997					vb2_dma_contig_plane_dma_addr(vb, 0)))) {
998			mfc_err("Plane memory not allocated\n");
999			return -EINVAL;
1000		}
1001		if (vb2_plane_size(vb, 0) < ctx->dec_src_buf_size) {
1002			mfc_err("Plane buffer (OUTPUT) is too small\n");
1003			return -EINVAL;
1004		}
1005
1006		i = vb->v4l2_buf.index;
1007		ctx->src_bufs[i].b = vb;
1008		ctx->src_bufs[i].cookie.stream =
1009					vb2_dma_contig_plane_dma_addr(vb, 0);
1010		ctx->src_bufs_cnt++;
1011	} else {
1012		mfc_err("s5p_mfc_buf_init: unknown queue type\n");
1013		return -EINVAL;
1014	}
1015	return 0;
1016}
1017
1018static int s5p_mfc_start_streaming(struct vb2_queue *q, unsigned int count)
1019{
1020	struct s5p_mfc_ctx *ctx = fh_to_ctx(q->drv_priv);
1021	struct s5p_mfc_dev *dev = ctx->dev;
1022
1023	v4l2_ctrl_handler_setup(&ctx->ctrl_handler);
1024	if (ctx->state == MFCINST_FINISHING ||
1025		ctx->state == MFCINST_FINISHED)
1026		ctx->state = MFCINST_RUNNING;
1027	/* If context is ready then dev = work->data;schedule it to run */
1028	if (s5p_mfc_ctx_ready(ctx))
1029		set_work_bit_irqsave(ctx);
1030	s5p_mfc_hw_call(dev->mfc_ops, try_run, dev);
1031	return 0;
1032}
1033
1034static int s5p_mfc_stop_streaming(struct vb2_queue *q)
1035{
1036	unsigned long flags;
1037	struct s5p_mfc_ctx *ctx = fh_to_ctx(q->drv_priv);
1038	struct s5p_mfc_dev *dev = ctx->dev;
1039	int aborted = 0;
1040
1041	if ((ctx->state == MFCINST_FINISHING ||
1042		ctx->state ==  MFCINST_RUNNING) &&
1043		dev->curr_ctx == ctx->num && dev->hw_lock) {
1044		ctx->state = MFCINST_ABORT;
1045		s5p_mfc_wait_for_done_ctx(ctx,
1046					S5P_MFC_R2H_CMD_FRAME_DONE_RET, 0);
1047		aborted = 1;
1048	}
1049	if (q->type == V4L2_BUF_TYPE_VIDEO_CAPTURE_MPLANE) {
1050		spin_lock_irqsave(&dev->irqlock, flags);
1051		s5p_mfc_hw_call(dev->mfc_ops, cleanup_queue, &ctx->dst_queue,
1052				&ctx->vq_dst);
1053		INIT_LIST_HEAD(&ctx->dst_queue);
1054		ctx->dst_queue_cnt = 0;
1055		ctx->dpb_flush_flag = 1;
1056		ctx->dec_dst_flag = 0;
1057		spin_unlock_irqrestore(&dev->irqlock, flags);
1058		if (IS_MFCV6_PLUS(dev) && (ctx->state == MFCINST_RUNNING)) {
1059			ctx->state = MFCINST_FLUSH;
1060			set_work_bit_irqsave(ctx);
1061			s5p_mfc_clean_ctx_int_flags(ctx);
1062			s5p_mfc_hw_call(dev->mfc_ops, try_run, dev);
1063			if (s5p_mfc_wait_for_done_ctx(ctx,
1064				S5P_MFC_R2H_CMD_DPB_FLUSH_RET, 0))
1065				mfc_err("Err flushing buffers\n");
1066		}
1067	}
1068	if (q->type == V4L2_BUF_TYPE_VIDEO_OUTPUT_MPLANE) {
1069		spin_lock_irqsave(&dev->irqlock, flags);
1070		s5p_mfc_hw_call(dev->mfc_ops, cleanup_queue, &ctx->src_queue,
1071				&ctx->vq_src);
1072		INIT_LIST_HEAD(&ctx->src_queue);
1073		ctx->src_queue_cnt = 0;
1074		spin_unlock_irqrestore(&dev->irqlock, flags);
1075	}
1076	if (aborted)
1077		ctx->state = MFCINST_RUNNING;
1078	return 0;
1079}
1080
1081
1082static void s5p_mfc_buf_queue(struct vb2_buffer *vb)
1083{
1084	struct vb2_queue *vq = vb->vb2_queue;
1085	struct s5p_mfc_ctx *ctx = fh_to_ctx(vq->drv_priv);
1086	struct s5p_mfc_dev *dev = ctx->dev;
1087	unsigned long flags;
1088	struct s5p_mfc_buf *mfc_buf;
1089
1090	if (vq->type == V4L2_BUF_TYPE_VIDEO_OUTPUT_MPLANE) {
1091		mfc_buf = &ctx->src_bufs[vb->v4l2_buf.index];
1092		mfc_buf->flags &= ~MFC_BUF_FLAG_USED;
1093		spin_lock_irqsave(&dev->irqlock, flags);
1094		list_add_tail(&mfc_buf->list, &ctx->src_queue);
1095		ctx->src_queue_cnt++;
1096		spin_unlock_irqrestore(&dev->irqlock, flags);
1097	} else if (vq->type == V4L2_BUF_TYPE_VIDEO_CAPTURE_MPLANE) {
1098		mfc_buf = &ctx->dst_bufs[vb->v4l2_buf.index];
1099		mfc_buf->flags &= ~MFC_BUF_FLAG_USED;
1100		/* Mark destination as available for use by MFC */
1101		spin_lock_irqsave(&dev->irqlock, flags);
1102		set_bit(vb->v4l2_buf.index, &ctx->dec_dst_flag);
1103		list_add_tail(&mfc_buf->list, &ctx->dst_queue);
1104		ctx->dst_queue_cnt++;
1105		spin_unlock_irqrestore(&dev->irqlock, flags);
1106	} else {
1107		mfc_err("Unsupported buffer type (%d)\n", vq->type);
1108	}
1109	if (s5p_mfc_ctx_ready(ctx))
1110		set_work_bit_irqsave(ctx);
1111	s5p_mfc_hw_call(dev->mfc_ops, try_run, dev);
1112}
1113
1114static struct vb2_ops s5p_mfc_dec_qops = {
1115	.queue_setup		= s5p_mfc_queue_setup,
1116	.wait_prepare		= s5p_mfc_unlock,
1117	.wait_finish		= s5p_mfc_lock,
1118	.buf_init		= s5p_mfc_buf_init,
1119	.start_streaming	= s5p_mfc_start_streaming,
1120	.stop_streaming		= s5p_mfc_stop_streaming,
1121	.buf_queue		= s5p_mfc_buf_queue,
1122};
1123
1124struct s5p_mfc_codec_ops *get_dec_codec_ops(void)
1125{
1126	return &decoder_codec_ops;
1127}
1128
1129struct vb2_ops *get_dec_queue_ops(void)
1130{
1131	return &s5p_mfc_dec_qops;
1132}
1133
1134const struct v4l2_ioctl_ops *get_dec_v4l2_ioctl_ops(void)
1135{
1136	return &s5p_mfc_dec_ioctl_ops;
1137}
1138
1139#define IS_MFC51_PRIV(x) ((V4L2_CTRL_ID2CLASS(x) == V4L2_CTRL_CLASS_MPEG) \
1140						&& V4L2_CTRL_DRIVER_PRIV(x))
1141
1142int s5p_mfc_dec_ctrls_setup(struct s5p_mfc_ctx *ctx)
1143{
1144	struct v4l2_ctrl_config cfg;
1145	int i;
1146
1147	v4l2_ctrl_handler_init(&ctx->ctrl_handler, NUM_CTRLS);
1148	if (ctx->ctrl_handler.error) {
1149		mfc_err("v4l2_ctrl_handler_init failed\n");
1150		return ctx->ctrl_handler.error;
1151	}
1152
1153	for (i = 0; i < NUM_CTRLS; i++) {
1154		if (IS_MFC51_PRIV(controls[i].id)) {
1155			memset(&cfg, 0, sizeof(struct v4l2_ctrl_config));
1156			cfg.ops = &s5p_mfc_dec_ctrl_ops;
1157			cfg.id = controls[i].id;
1158			cfg.min = controls[i].minimum;
1159			cfg.max = controls[i].maximum;
1160			cfg.def = controls[i].default_value;
1161			cfg.name = controls[i].name;
1162			cfg.type = controls[i].type;
1163
1164			cfg.step = controls[i].step;
1165			cfg.menu_skip_mask = 0;
1166
1167			ctx->ctrls[i] = v4l2_ctrl_new_custom(&ctx->ctrl_handler,
1168					&cfg, NULL);
1169		} else {
1170			ctx->ctrls[i] = v4l2_ctrl_new_std(&ctx->ctrl_handler,
1171					&s5p_mfc_dec_ctrl_ops,
1172					controls[i].id, controls[i].minimum,
1173					controls[i].maximum, controls[i].step,
1174					controls[i].default_value);
1175		}
1176		if (ctx->ctrl_handler.error) {
1177			mfc_err("Adding control (%d) failed\n", i);
1178			return ctx->ctrl_handler.error;
1179		}
1180		if (controls[i].is_volatile && ctx->ctrls[i])
1181			ctx->ctrls[i]->flags |= V4L2_CTRL_FLAG_VOLATILE;
1182	}
1183	return 0;
1184}
1185
1186void s5p_mfc_dec_ctrls_delete(struct s5p_mfc_ctx *ctx)
1187{
1188	int i;
1189
1190	v4l2_ctrl_handler_free(&ctx->ctrl_handler);
1191	for (i = 0; i < NUM_CTRLS; i++)
1192		ctx->ctrls[i] = NULL;
1193}
1194
1195void s5p_mfc_dec_init(struct s5p_mfc_ctx *ctx)
1196{
1197	struct v4l2_format f;
1198	f.fmt.pix_mp.pixelformat = DEF_SRC_FMT_DEC;
1199	ctx->src_fmt = find_format(&f, MFC_FMT_DEC);
1200	f.fmt.pix_mp.pixelformat = DEF_DST_FMT_DEC;
1201	ctx->dst_fmt = find_format(&f, MFC_FMT_RAW);
1202	mfc_debug(2, "Default src_fmt is %x, dest_fmt is %x\n",
1203			(unsigned int)ctx->src_fmt, (unsigned int)ctx->dst_fmt);
1204}
1205
1206