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