s5p_mfc_dec.c revision 818cd91ab8c6e42c2658c8e61f8462637c6f586b
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
35#define DEF_SRC_FMT_DEC	V4L2_PIX_FMT_H264
36#define DEF_DST_FMT_DEC	V4L2_PIX_FMT_NV12MT_16X16
37
38static struct s5p_mfc_fmt formats[] = {
39	{
40		.name		= "4:2:0 2 Planes 16x16 Tiles",
41		.fourcc		= V4L2_PIX_FMT_NV12MT_16X16,
42		.codec_mode	= S5P_MFC_CODEC_NONE,
43		.type		= MFC_FMT_RAW,
44		.num_planes	= 2,
45	},
46	{
47		.name		= "4:2:0 2 Planes 64x32 Tiles",
48		.fourcc		= V4L2_PIX_FMT_NV12MT,
49		.codec_mode	= S5P_MFC_CODEC_NONE,
50		.type		= MFC_FMT_RAW,
51		.num_planes	= 2,
52	},
53	{
54		.name		= "4:2:0 2 Planes Y/CbCr",
55		.fourcc		= V4L2_PIX_FMT_NV12M,
56		.codec_mode	= S5P_MFC_CODEC_NONE,
57		.type		= MFC_FMT_RAW,
58		.num_planes	= 2,
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	},
67	{
68		.name		= "H264 Encoded Stream",
69		.fourcc		= V4L2_PIX_FMT_H264,
70		.codec_mode	= S5P_MFC_CODEC_H264_DEC,
71		.type		= MFC_FMT_DEC,
72		.num_planes	= 1,
73	},
74	{
75		.name		= "H264/MVC Encoded Stream",
76		.fourcc		= V4L2_PIX_FMT_H264_MVC,
77		.codec_mode	= S5P_MFC_CODEC_H264_MVC_DEC,
78		.type		= MFC_FMT_DEC,
79		.num_planes	= 1,
80	},
81	{
82		.name		= "H263 Encoded Stream",
83		.fourcc		= V4L2_PIX_FMT_H263,
84		.codec_mode	= S5P_MFC_CODEC_H263_DEC,
85		.type		= MFC_FMT_DEC,
86		.num_planes	= 1,
87	},
88	{
89		.name		= "MPEG1 Encoded Stream",
90		.fourcc		= V4L2_PIX_FMT_MPEG1,
91		.codec_mode	= S5P_MFC_CODEC_MPEG2_DEC,
92		.type		= MFC_FMT_DEC,
93		.num_planes	= 1,
94	},
95	{
96		.name		= "MPEG2 Encoded Stream",
97		.fourcc		= V4L2_PIX_FMT_MPEG2,
98		.codec_mode	= S5P_MFC_CODEC_MPEG2_DEC,
99		.type		= MFC_FMT_DEC,
100		.num_planes	= 1,
101	},
102	{
103		.name		= "MPEG4 Encoded Stream",
104		.fourcc		= V4L2_PIX_FMT_MPEG4,
105		.codec_mode	= S5P_MFC_CODEC_MPEG4_DEC,
106		.type		= MFC_FMT_DEC,
107		.num_planes	= 1,
108	},
109	{
110		.name		= "XviD Encoded Stream",
111		.fourcc		= V4L2_PIX_FMT_XVID,
112		.codec_mode	= S5P_MFC_CODEC_MPEG4_DEC,
113		.type		= MFC_FMT_DEC,
114		.num_planes	= 1,
115	},
116	{
117		.name		= "VC1 Encoded Stream",
118		.fourcc		= V4L2_PIX_FMT_VC1_ANNEX_G,
119		.codec_mode	= S5P_MFC_CODEC_VC1_DEC,
120		.type		= MFC_FMT_DEC,
121		.num_planes	= 1,
122	},
123	{
124		.name		= "VC1 RCV Encoded Stream",
125		.fourcc		= V4L2_PIX_FMT_VC1_ANNEX_L,
126		.codec_mode	= S5P_MFC_CODEC_VC1RCV_DEC,
127		.type		= MFC_FMT_DEC,
128		.num_planes	= 1,
129	},
130	{
131		.name		= "VP8 Encoded Stream",
132		.fourcc		= V4L2_PIX_FMT_VP8,
133		.codec_mode	= S5P_MFC_CODEC_VP8_DEC,
134		.type		= MFC_FMT_DEC,
135		.num_planes	= 1,
136	},
137};
138
139#define NUM_FORMATS ARRAY_SIZE(formats)
140
141/* Find selected format description */
142static struct s5p_mfc_fmt *find_format(struct v4l2_format *f, unsigned int t)
143{
144	unsigned int i;
145
146	for (i = 0; i < NUM_FORMATS; i++) {
147		if (formats[i].fourcc == f->fmt.pix_mp.pixelformat &&
148		    formats[i].type == t)
149			return &formats[i];
150	}
151	return NULL;
152}
153
154static struct mfc_control controls[] = {
155	{
156		.id = V4L2_CID_MPEG_MFC51_VIDEO_DECODER_H264_DISPLAY_DELAY,
157		.type = V4L2_CTRL_TYPE_INTEGER,
158		.name = "H264 Display Delay",
159		.minimum = 0,
160		.maximum = 16383,
161		.step = 1,
162		.default_value = 0,
163	},
164	{
165		.id = V4L2_CID_MPEG_MFC51_VIDEO_DECODER_H264_DISPLAY_DELAY_ENABLE,
166		.type = V4L2_CTRL_TYPE_BOOLEAN,
167		.name = "H264 Display Delay Enable",
168		.minimum = 0,
169		.maximum = 1,
170		.step = 1,
171		.default_value = 0,
172	},
173	{
174		.id = V4L2_CID_MPEG_VIDEO_DECODER_MPEG4_DEBLOCK_FILTER,
175		.type = V4L2_CTRL_TYPE_BOOLEAN,
176		.name = "Mpeg4 Loop Filter Enable",
177		.minimum = 0,
178		.maximum = 1,
179		.step = 1,
180		.default_value = 0,
181	},
182	{
183		.id = V4L2_CID_MPEG_VIDEO_DECODER_SLICE_INTERFACE,
184		.type = V4L2_CTRL_TYPE_BOOLEAN,
185		.name = "Slice Interface Enable",
186		.minimum = 0,
187		.maximum = 1,
188		.step = 1,
189		.default_value = 0,
190	},
191	{
192		.id = V4L2_CID_MIN_BUFFERS_FOR_CAPTURE,
193		.type = V4L2_CTRL_TYPE_INTEGER,
194		.name = "Minimum number of cap bufs",
195		.minimum = 1,
196		.maximum = 32,
197		.step = 1,
198		.default_value = 1,
199		.is_volatile = 1,
200	},
201};
202
203#define NUM_CTRLS ARRAY_SIZE(controls)
204
205/* Check whether a context should be run on hardware */
206static int s5p_mfc_ctx_ready(struct s5p_mfc_ctx *ctx)
207{
208	/* Context is to parse header */
209	if (ctx->src_queue_cnt >= 1 && ctx->state == MFCINST_GOT_INST)
210		return 1;
211	/* Context is to decode a frame */
212	if (ctx->src_queue_cnt >= 1 &&
213	    ctx->state == MFCINST_RUNNING &&
214	    ctx->dst_queue_cnt >= ctx->pb_count)
215		return 1;
216	/* Context is to return last frame */
217	if (ctx->state == MFCINST_FINISHING &&
218	    ctx->dst_queue_cnt >= ctx->pb_count)
219		return 1;
220	/* Context is to set buffers */
221	if (ctx->src_queue_cnt >= 1 &&
222	    ctx->state == MFCINST_HEAD_PARSED &&
223	    ctx->capture_state == QUEUE_BUFS_MMAPED)
224		return 1;
225	/* Resolution change */
226	if ((ctx->state == MFCINST_RES_CHANGE_INIT ||
227		ctx->state == MFCINST_RES_CHANGE_FLUSH) &&
228		ctx->dst_queue_cnt >= ctx->pb_count)
229		return 1;
230	if (ctx->state == MFCINST_RES_CHANGE_END &&
231		ctx->src_queue_cnt >= 1)
232		return 1;
233	mfc_debug(2, "ctx is not ready\n");
234	return 0;
235}
236
237static struct s5p_mfc_codec_ops decoder_codec_ops = {
238	.pre_seq_start		= NULL,
239	.post_seq_start		= NULL,
240	.pre_frame_start	= NULL,
241	.post_frame_start	= NULL,
242};
243
244/* Query capabilities of the device */
245static int vidioc_querycap(struct file *file, void *priv,
246			   struct v4l2_capability *cap)
247{
248	struct s5p_mfc_dev *dev = video_drvdata(file);
249
250	strncpy(cap->driver, dev->plat_dev->name, sizeof(cap->driver) - 1);
251	strncpy(cap->card, dev->plat_dev->name, sizeof(cap->card) - 1);
252	cap->bus_info[0] = 0;
253	cap->version = KERNEL_VERSION(1, 0, 0);
254	/*
255	 * This is only a mem-to-mem video device. The capture and output
256	 * device capability flags are left only for backward compatibility
257	 * and are scheduled for removal.
258	 */
259	cap->capabilities = V4L2_CAP_VIDEO_M2M_MPLANE | V4L2_CAP_STREAMING |
260			    V4L2_CAP_VIDEO_CAPTURE_MPLANE |
261			    V4L2_CAP_VIDEO_OUTPUT_MPLANE;
262	return 0;
263}
264
265/* Enumerate format */
266static int vidioc_enum_fmt(struct v4l2_fmtdesc *f, bool mplane, bool out)
267{
268	struct s5p_mfc_fmt *fmt;
269	int i, j = 0;
270
271	for (i = 0; i < ARRAY_SIZE(formats); ++i) {
272		if (mplane && formats[i].num_planes == 1)
273			continue;
274		else if (!mplane && formats[i].num_planes > 1)
275			continue;
276		if (out && formats[i].type != MFC_FMT_DEC)
277			continue;
278		else if (!out && formats[i].type != MFC_FMT_RAW)
279			continue;
280
281		if (j == f->index)
282			break;
283		++j;
284	}
285	if (i == ARRAY_SIZE(formats))
286		return -EINVAL;
287	fmt = &formats[i];
288	strlcpy(f->description, fmt->name, sizeof(f->description));
289	f->pixelformat = fmt->fourcc;
290	return 0;
291}
292
293static int vidioc_enum_fmt_vid_cap(struct file *file, void *pirv,
294							struct v4l2_fmtdesc *f)
295{
296	return vidioc_enum_fmt(f, false, false);
297}
298
299static int vidioc_enum_fmt_vid_cap_mplane(struct file *file, void *pirv,
300							struct v4l2_fmtdesc *f)
301{
302	return vidioc_enum_fmt(f, true, false);
303}
304
305static int vidioc_enum_fmt_vid_out(struct file *file, void *prov,
306							struct v4l2_fmtdesc *f)
307{
308	return vidioc_enum_fmt(f, false, true);
309}
310
311static int vidioc_enum_fmt_vid_out_mplane(struct file *file, void *prov,
312							struct v4l2_fmtdesc *f)
313{
314	return vidioc_enum_fmt(f, true, true);
315}
316
317/* Get format */
318static int vidioc_g_fmt(struct file *file, void *priv, struct v4l2_format *f)
319{
320	struct s5p_mfc_ctx *ctx = fh_to_ctx(priv);
321	struct v4l2_pix_format_mplane *pix_mp;
322
323	mfc_debug_enter();
324	pix_mp = &f->fmt.pix_mp;
325	if (f->type == V4L2_BUF_TYPE_VIDEO_CAPTURE_MPLANE &&
326	    (ctx->state == MFCINST_GOT_INST || ctx->state ==
327						MFCINST_RES_CHANGE_END)) {
328		/* If the MFC is parsing the header,
329		 * so wait until it is finished */
330		s5p_mfc_clean_ctx_int_flags(ctx);
331		s5p_mfc_wait_for_done_ctx(ctx, S5P_MFC_R2H_CMD_SEQ_DONE_RET,
332									0);
333	}
334	if (f->type == V4L2_BUF_TYPE_VIDEO_CAPTURE_MPLANE &&
335	    ctx->state >= MFCINST_HEAD_PARSED &&
336	    ctx->state < MFCINST_ABORT) {
337		/* This is run on CAPTURE (decode output) */
338		/* Width and height are set to the dimensions
339		   of the movie, the buffer is bigger and
340		   further processing stages should crop to this
341		   rectangle. */
342		pix_mp->width = ctx->buf_width;
343		pix_mp->height = ctx->buf_height;
344		pix_mp->field = V4L2_FIELD_NONE;
345		pix_mp->num_planes = 2;
346		/* Set pixelformat to the format in which MFC
347		   outputs the decoded frame */
348		pix_mp->pixelformat = ctx->dst_fmt->fourcc;
349		pix_mp->plane_fmt[0].bytesperline = ctx->buf_width;
350		pix_mp->plane_fmt[0].sizeimage = ctx->luma_size;
351		pix_mp->plane_fmt[1].bytesperline = ctx->buf_width;
352		pix_mp->plane_fmt[1].sizeimage = ctx->chroma_size;
353	} else if (f->type == V4L2_BUF_TYPE_VIDEO_OUTPUT_MPLANE) {
354		/* This is run on OUTPUT
355		   The buffer contains compressed image
356		   so width and height have no meaning */
357		pix_mp->width = 0;
358		pix_mp->height = 0;
359		pix_mp->field = V4L2_FIELD_NONE;
360		pix_mp->plane_fmt[0].bytesperline = ctx->dec_src_buf_size;
361		pix_mp->plane_fmt[0].sizeimage = ctx->dec_src_buf_size;
362		pix_mp->pixelformat = ctx->src_fmt->fourcc;
363		pix_mp->num_planes = ctx->src_fmt->num_planes;
364	} else {
365		mfc_err("Format could not be read\n");
366		mfc_debug(2, "%s-- with error\n", __func__);
367		return -EINVAL;
368	}
369	mfc_debug_leave();
370	return 0;
371}
372
373/* Try format */
374static int vidioc_try_fmt(struct file *file, void *priv, struct v4l2_format *f)
375{
376	struct s5p_mfc_dev *dev = video_drvdata(file);
377	struct s5p_mfc_fmt *fmt;
378
379	mfc_debug(2, "Type is %d\n", f->type);
380	if (f->type == V4L2_BUF_TYPE_VIDEO_OUTPUT_MPLANE) {
381		fmt = find_format(f, MFC_FMT_DEC);
382		if (!fmt) {
383			mfc_err("Unsupported format for source.\n");
384			return -EINVAL;
385		}
386		if (fmt->codec_mode == S5P_FIMV_CODEC_NONE) {
387			mfc_err("Unknown codec\n");
388			return -EINVAL;
389		}
390		if (!IS_MFCV6_PLUS(dev)) {
391			if (fmt->fourcc == V4L2_PIX_FMT_VP8) {
392				mfc_err("Not supported format.\n");
393				return -EINVAL;
394			}
395		}
396	} else if (f->type == V4L2_BUF_TYPE_VIDEO_CAPTURE_MPLANE) {
397		fmt = find_format(f, MFC_FMT_RAW);
398		if (!fmt) {
399			mfc_err("Unsupported format for destination.\n");
400			return -EINVAL;
401		}
402		if (IS_MFCV6_PLUS(dev) &&
403				(fmt->fourcc == V4L2_PIX_FMT_NV12MT)) {
404			mfc_err("Not supported format.\n");
405			return -EINVAL;
406		} else if (!IS_MFCV6_PLUS(dev) &&
407				(fmt->fourcc != V4L2_PIX_FMT_NV12MT)) {
408			mfc_err("Not supported format.\n");
409			return -EINVAL;
410		}
411	}
412
413	return 0;
414}
415
416/* Set format */
417static int vidioc_s_fmt(struct file *file, void *priv, struct v4l2_format *f)
418{
419	struct s5p_mfc_dev *dev = video_drvdata(file);
420	struct s5p_mfc_ctx *ctx = fh_to_ctx(priv);
421	int ret = 0;
422	struct v4l2_pix_format_mplane *pix_mp;
423
424	mfc_debug_enter();
425	ret = vidioc_try_fmt(file, priv, f);
426	pix_mp = &f->fmt.pix_mp;
427	if (ret)
428		return ret;
429	if (ctx->vq_src.streaming || ctx->vq_dst.streaming) {
430		v4l2_err(&dev->v4l2_dev, "%s queue busy\n", __func__);
431		ret = -EBUSY;
432		goto out;
433	}
434	if (f->type == V4L2_BUF_TYPE_VIDEO_CAPTURE_MPLANE) {
435		/* dst_fmt is validated by call to vidioc_try_fmt */
436		ctx->dst_fmt = find_format(f, MFC_FMT_RAW);
437		ret = 0;
438		goto out;
439	} else if (f->type == V4L2_BUF_TYPE_VIDEO_OUTPUT_MPLANE) {
440		/* src_fmt is validated by call to vidioc_try_fmt */
441		ctx->src_fmt = find_format(f, MFC_FMT_DEC);
442		ctx->codec_mode = ctx->src_fmt->codec_mode;
443		mfc_debug(2, "The codec number is: %d\n", ctx->codec_mode);
444		pix_mp->height = 0;
445		pix_mp->width = 0;
446		if (pix_mp->plane_fmt[0].sizeimage)
447			ctx->dec_src_buf_size = pix_mp->plane_fmt[0].sizeimage;
448		else
449			pix_mp->plane_fmt[0].sizeimage = ctx->dec_src_buf_size =
450								DEF_CPB_SIZE;
451		pix_mp->plane_fmt[0].bytesperline = 0;
452		ctx->state = MFCINST_INIT;
453		ret = 0;
454		goto out;
455	} else {
456		mfc_err("Wrong type error for S_FMT : %d", f->type);
457		ret = -EINVAL;
458		goto out;
459	}
460
461out:
462	mfc_debug_leave();
463	return ret;
464}
465
466static int reqbufs_output(struct s5p_mfc_dev *dev, struct s5p_mfc_ctx *ctx,
467				struct v4l2_requestbuffers *reqbufs)
468{
469	int ret = 0;
470
471	s5p_mfc_clock_on();
472
473	if (reqbufs->count == 0) {
474		mfc_debug(2, "Freeing buffers\n");
475		ret = vb2_reqbufs(&ctx->vq_src, reqbufs);
476		if (ret)
477			goto out;
478		ctx->src_bufs_cnt = 0;
479	} else if (ctx->output_state == QUEUE_FREE) {
480		/* Can only request buffers after the instance
481		 * has been opened.
482		 */
483		WARN_ON(ctx->src_bufs_cnt != 0);
484		if (ctx->state != MFCINST_INIT) {
485			mfc_err("Reqbufs called in an invalid state\n");
486			ret = -EINVAL;
487			goto out;
488		}
489
490		mfc_debug(2, "Allocating %d buffers for OUTPUT queue\n",
491				reqbufs->count);
492		ret = vb2_reqbufs(&ctx->vq_src, reqbufs);
493		if (ret)
494			goto out;
495
496		ctx->output_state = QUEUE_BUFS_REQUESTED;
497	} else {
498		mfc_err("Buffers have already been requested\n");
499		ret = -EINVAL;
500	}
501out:
502	s5p_mfc_clock_off();
503	if (ret)
504		mfc_err("Failed allocating buffers for OUTPUT queue\n");
505	return ret;
506}
507
508static int reqbufs_capture(struct s5p_mfc_dev *dev, struct s5p_mfc_ctx *ctx,
509				struct v4l2_requestbuffers *reqbufs)
510{
511	int ret = 0;
512
513	s5p_mfc_clock_on();
514
515	if (reqbufs->count == 0) {
516		mfc_debug(2, "Freeing buffers\n");
517		ret = vb2_reqbufs(&ctx->vq_dst, reqbufs);
518		if (ret)
519			goto out;
520		s5p_mfc_hw_call(dev->mfc_ops, release_codec_buffers, ctx);
521		ctx->dst_bufs_cnt = 0;
522	} else if (ctx->capture_state == QUEUE_FREE) {
523		WARN_ON(ctx->dst_bufs_cnt != 0);
524		mfc_debug(2, "Allocating %d buffers for CAPTURE queue\n",
525				reqbufs->count);
526		ret = vb2_reqbufs(&ctx->vq_dst, reqbufs);
527		if (ret)
528			goto out;
529
530		ctx->capture_state = QUEUE_BUFS_REQUESTED;
531		ctx->total_dpb_count = reqbufs->count;
532
533		ret = s5p_mfc_hw_call(dev->mfc_ops, alloc_codec_buffers, ctx);
534		if (ret) {
535			mfc_err("Failed to allocate decoding buffers\n");
536			reqbufs->count = 0;
537			vb2_reqbufs(&ctx->vq_dst, reqbufs);
538			ret = -ENOMEM;
539			ctx->capture_state = QUEUE_FREE;
540			goto out;
541		}
542
543		WARN_ON(ctx->dst_bufs_cnt != ctx->total_dpb_count);
544		ctx->capture_state = QUEUE_BUFS_MMAPED;
545
546		if (s5p_mfc_ctx_ready(ctx))
547			set_work_bit_irqsave(ctx);
548		s5p_mfc_hw_call(dev->mfc_ops, try_run, dev);
549		s5p_mfc_wait_for_done_ctx(ctx, S5P_MFC_R2H_CMD_INIT_BUFFERS_RET,
550					  0);
551	} else {
552		mfc_err("Buffers have already been requested\n");
553		ret = -EINVAL;
554	}
555out:
556	s5p_mfc_clock_off();
557	if (ret)
558		mfc_err("Failed allocating buffers for CAPTURE queue\n");
559	return ret;
560}
561
562/* Reqeust buffers */
563static int vidioc_reqbufs(struct file *file, void *priv,
564					  struct v4l2_requestbuffers *reqbufs)
565{
566	struct s5p_mfc_dev *dev = video_drvdata(file);
567	struct s5p_mfc_ctx *ctx = fh_to_ctx(priv);
568
569	if (reqbufs->memory != V4L2_MEMORY_MMAP) {
570		mfc_err("Only V4L2_MEMORY_MAP is supported\n");
571		return -EINVAL;
572	}
573
574	if (reqbufs->type == V4L2_BUF_TYPE_VIDEO_OUTPUT_MPLANE) {
575		return reqbufs_output(dev, ctx, reqbufs);
576	} else if (reqbufs->type == V4L2_BUF_TYPE_VIDEO_CAPTURE_MPLANE) {
577		return reqbufs_capture(dev, ctx, reqbufs);
578	} else {
579		mfc_err("Invalid type requested\n");
580		return -EINVAL;
581	}
582}
583
584/* Query buffer */
585static int vidioc_querybuf(struct file *file, void *priv,
586						   struct v4l2_buffer *buf)
587{
588	struct s5p_mfc_ctx *ctx = fh_to_ctx(priv);
589	int ret;
590	int i;
591
592	if (buf->memory != V4L2_MEMORY_MMAP) {
593		mfc_err("Only mmaped buffers can be used\n");
594		return -EINVAL;
595	}
596	mfc_debug(2, "State: %d, buf->type: %d\n", ctx->state, buf->type);
597	if (ctx->state == MFCINST_INIT &&
598			buf->type == V4L2_BUF_TYPE_VIDEO_OUTPUT_MPLANE) {
599		ret = vb2_querybuf(&ctx->vq_src, buf);
600	} else if (ctx->state == MFCINST_RUNNING &&
601			buf->type == V4L2_BUF_TYPE_VIDEO_CAPTURE_MPLANE) {
602		ret = vb2_querybuf(&ctx->vq_dst, buf);
603		for (i = 0; i < buf->length; i++)
604			buf->m.planes[i].m.mem_offset += DST_QUEUE_OFF_BASE;
605	} else {
606		mfc_err("vidioc_querybuf called in an inappropriate state\n");
607		ret = -EINVAL;
608	}
609	mfc_debug_leave();
610	return ret;
611}
612
613/* Queue a buffer */
614static int vidioc_qbuf(struct file *file, void *priv, struct v4l2_buffer *buf)
615{
616	struct s5p_mfc_ctx *ctx = fh_to_ctx(priv);
617
618	if (ctx->state == MFCINST_ERROR) {
619		mfc_err("Call on QBUF after unrecoverable error\n");
620		return -EIO;
621	}
622	if (buf->type == V4L2_BUF_TYPE_VIDEO_OUTPUT_MPLANE)
623		return vb2_qbuf(&ctx->vq_src, buf);
624	else if (buf->type == V4L2_BUF_TYPE_VIDEO_CAPTURE_MPLANE)
625		return vb2_qbuf(&ctx->vq_dst, buf);
626	return -EINVAL;
627}
628
629/* Dequeue a buffer */
630static int vidioc_dqbuf(struct file *file, void *priv, struct v4l2_buffer *buf)
631{
632	const struct v4l2_event ev = {
633		.type = V4L2_EVENT_EOS
634	};
635	struct s5p_mfc_ctx *ctx = fh_to_ctx(priv);
636	int ret;
637
638	if (ctx->state == MFCINST_ERROR) {
639		mfc_err("Call on DQBUF after unrecoverable error\n");
640		return -EIO;
641	}
642	if (buf->type == V4L2_BUF_TYPE_VIDEO_OUTPUT_MPLANE)
643		ret = vb2_dqbuf(&ctx->vq_src, buf, file->f_flags & O_NONBLOCK);
644	else if (buf->type == V4L2_BUF_TYPE_VIDEO_CAPTURE_MPLANE) {
645		ret = vb2_dqbuf(&ctx->vq_dst, buf, file->f_flags & O_NONBLOCK);
646		if (ret == 0 && ctx->state == MFCINST_FINISHED &&
647				list_empty(&ctx->vq_dst.done_list))
648			v4l2_event_queue_fh(&ctx->fh, &ev);
649	} else {
650		ret = -EINVAL;
651	}
652	return ret;
653}
654
655/* Export DMA buffer */
656static int vidioc_expbuf(struct file *file, void *priv,
657	struct v4l2_exportbuffer *eb)
658{
659	struct s5p_mfc_ctx *ctx = fh_to_ctx(priv);
660
661	if (eb->type == V4L2_BUF_TYPE_VIDEO_OUTPUT_MPLANE)
662		return vb2_expbuf(&ctx->vq_src, eb);
663	if (eb->type == V4L2_BUF_TYPE_VIDEO_CAPTURE_MPLANE)
664		return vb2_expbuf(&ctx->vq_dst, eb);
665	return -EINVAL;
666}
667
668/* Stream on */
669static int vidioc_streamon(struct file *file, void *priv,
670			   enum v4l2_buf_type type)
671{
672	struct s5p_mfc_ctx *ctx = fh_to_ctx(priv);
673	struct s5p_mfc_dev *dev = ctx->dev;
674	int ret = -EINVAL;
675
676	mfc_debug_enter();
677	if (type == V4L2_BUF_TYPE_VIDEO_OUTPUT_MPLANE) {
678		if (ctx->state == MFCINST_INIT) {
679			ctx->dst_bufs_cnt = 0;
680			ctx->src_bufs_cnt = 0;
681			ctx->capture_state = QUEUE_FREE;
682			ctx->output_state = QUEUE_FREE;
683			ret = s5p_mfc_open_mfc_inst(dev, ctx);
684			if (ret)
685				return ret;
686		}
687		ret = vb2_streamon(&ctx->vq_src, type);
688	} else if (type == V4L2_BUF_TYPE_VIDEO_CAPTURE_MPLANE) {
689		ret = vb2_streamon(&ctx->vq_dst, type);
690	}
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 void 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}
1079
1080
1081static void s5p_mfc_buf_queue(struct vb2_buffer *vb)
1082{
1083	struct vb2_queue *vq = vb->vb2_queue;
1084	struct s5p_mfc_ctx *ctx = fh_to_ctx(vq->drv_priv);
1085	struct s5p_mfc_dev *dev = ctx->dev;
1086	unsigned long flags;
1087	struct s5p_mfc_buf *mfc_buf;
1088
1089	if (vq->type == V4L2_BUF_TYPE_VIDEO_OUTPUT_MPLANE) {
1090		mfc_buf = &ctx->src_bufs[vb->v4l2_buf.index];
1091		mfc_buf->flags &= ~MFC_BUF_FLAG_USED;
1092		spin_lock_irqsave(&dev->irqlock, flags);
1093		list_add_tail(&mfc_buf->list, &ctx->src_queue);
1094		ctx->src_queue_cnt++;
1095		spin_unlock_irqrestore(&dev->irqlock, flags);
1096	} else if (vq->type == V4L2_BUF_TYPE_VIDEO_CAPTURE_MPLANE) {
1097		mfc_buf = &ctx->dst_bufs[vb->v4l2_buf.index];
1098		mfc_buf->flags &= ~MFC_BUF_FLAG_USED;
1099		/* Mark destination as available for use by MFC */
1100		spin_lock_irqsave(&dev->irqlock, flags);
1101		set_bit(vb->v4l2_buf.index, &ctx->dec_dst_flag);
1102		list_add_tail(&mfc_buf->list, &ctx->dst_queue);
1103		ctx->dst_queue_cnt++;
1104		spin_unlock_irqrestore(&dev->irqlock, flags);
1105	} else {
1106		mfc_err("Unsupported buffer type (%d)\n", vq->type);
1107	}
1108	if (s5p_mfc_ctx_ready(ctx))
1109		set_work_bit_irqsave(ctx);
1110	s5p_mfc_hw_call(dev->mfc_ops, try_run, dev);
1111}
1112
1113static struct vb2_ops s5p_mfc_dec_qops = {
1114	.queue_setup		= s5p_mfc_queue_setup,
1115	.wait_prepare		= s5p_mfc_unlock,
1116	.wait_finish		= s5p_mfc_lock,
1117	.buf_init		= s5p_mfc_buf_init,
1118	.start_streaming	= s5p_mfc_start_streaming,
1119	.stop_streaming		= s5p_mfc_stop_streaming,
1120	.buf_queue		= s5p_mfc_buf_queue,
1121};
1122
1123struct s5p_mfc_codec_ops *get_dec_codec_ops(void)
1124{
1125	return &decoder_codec_ops;
1126}
1127
1128struct vb2_ops *get_dec_queue_ops(void)
1129{
1130	return &s5p_mfc_dec_qops;
1131}
1132
1133const struct v4l2_ioctl_ops *get_dec_v4l2_ioctl_ops(void)
1134{
1135	return &s5p_mfc_dec_ioctl_ops;
1136}
1137
1138#define IS_MFC51_PRIV(x) ((V4L2_CTRL_ID2CLASS(x) == V4L2_CTRL_CLASS_MPEG) \
1139						&& V4L2_CTRL_DRIVER_PRIV(x))
1140
1141int s5p_mfc_dec_ctrls_setup(struct s5p_mfc_ctx *ctx)
1142{
1143	struct v4l2_ctrl_config cfg;
1144	int i;
1145
1146	v4l2_ctrl_handler_init(&ctx->ctrl_handler, NUM_CTRLS);
1147	if (ctx->ctrl_handler.error) {
1148		mfc_err("v4l2_ctrl_handler_init failed\n");
1149		return ctx->ctrl_handler.error;
1150	}
1151
1152	for (i = 0; i < NUM_CTRLS; i++) {
1153		if (IS_MFC51_PRIV(controls[i].id)) {
1154			memset(&cfg, 0, sizeof(struct v4l2_ctrl_config));
1155			cfg.ops = &s5p_mfc_dec_ctrl_ops;
1156			cfg.id = controls[i].id;
1157			cfg.min = controls[i].minimum;
1158			cfg.max = controls[i].maximum;
1159			cfg.def = controls[i].default_value;
1160			cfg.name = controls[i].name;
1161			cfg.type = controls[i].type;
1162
1163			cfg.step = controls[i].step;
1164			cfg.menu_skip_mask = 0;
1165
1166			ctx->ctrls[i] = v4l2_ctrl_new_custom(&ctx->ctrl_handler,
1167					&cfg, NULL);
1168		} else {
1169			ctx->ctrls[i] = v4l2_ctrl_new_std(&ctx->ctrl_handler,
1170					&s5p_mfc_dec_ctrl_ops,
1171					controls[i].id, controls[i].minimum,
1172					controls[i].maximum, controls[i].step,
1173					controls[i].default_value);
1174		}
1175		if (ctx->ctrl_handler.error) {
1176			mfc_err("Adding control (%d) failed\n", i);
1177			return ctx->ctrl_handler.error;
1178		}
1179		if (controls[i].is_volatile && ctx->ctrls[i])
1180			ctx->ctrls[i]->flags |= V4L2_CTRL_FLAG_VOLATILE;
1181	}
1182	return 0;
1183}
1184
1185void s5p_mfc_dec_ctrls_delete(struct s5p_mfc_ctx *ctx)
1186{
1187	int i;
1188
1189	v4l2_ctrl_handler_free(&ctx->ctrl_handler);
1190	for (i = 0; i < NUM_CTRLS; i++)
1191		ctx->ctrls[i] = NULL;
1192}
1193
1194void s5p_mfc_dec_init(struct s5p_mfc_ctx *ctx)
1195{
1196	struct v4l2_format f;
1197	f.fmt.pix_mp.pixelformat = DEF_SRC_FMT_DEC;
1198	ctx->src_fmt = find_format(&f, MFC_FMT_DEC);
1199	f.fmt.pix_mp.pixelformat = DEF_DST_FMT_DEC;
1200	ctx->dst_fmt = find_format(&f, MFC_FMT_RAW);
1201	mfc_debug(2, "Default src_fmt is %x, dest_fmt is %x\n",
1202			(unsigned int)ctx->src_fmt, (unsigned int)ctx->dst_fmt);
1203}
1204
1205