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