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