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