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