s5p_mfc_enc.c revision 8f532a7fec5ee872a65d2096f846f76afd9ede6f
1/*
2 * linux/drivers/media/platform/s5p-mfc/s5p_mfc_enc.c
3 *
4 * Copyright (c) 2010-2011 Samsung Electronics Co., Ltd.
5 *		http://www.samsung.com/
6 *
7 * Jeongtae Park	<jtp.park@samsung.com>
8 * Kamil Debski		<k.debski@samsung.com>
9 *
10 * This program is free software; you can redistribute it and/or modify
11 * it under the terms of the GNU General Public License as published by
12 * the Free Software Foundation; either version 2 of the License, or
13 * (at your option) any later version.
14 */
15
16#include <linux/clk.h>
17#include <linux/interrupt.h>
18#include <linux/io.h>
19#include <linux/module.h>
20#include <linux/platform_device.h>
21#include <linux/sched.h>
22#include <linux/version.h>
23#include <linux/videodev2.h>
24#include <media/v4l2-event.h>
25#include <linux/workqueue.h>
26#include <media/v4l2-ctrls.h>
27#include <media/videobuf2-core.h>
28#include "s5p_mfc_common.h"
29#include "s5p_mfc_debug.h"
30#include "s5p_mfc_enc.h"
31#include "s5p_mfc_intr.h"
32#include "s5p_mfc_opr.h"
33
34#define DEF_SRC_FMT_ENC	V4L2_PIX_FMT_NV12MT
35#define DEF_DST_FMT_ENC	V4L2_PIX_FMT_H264
36
37static struct s5p_mfc_fmt formats[] = {
38	{
39		.name = "4:2:0 2 Planes 64x32 Tiles",
40		.fourcc = V4L2_PIX_FMT_NV12MT,
41		.codec_mode = S5P_MFC_CODEC_NONE,
42		.type = MFC_FMT_RAW,
43		.num_planes = 2,
44	},
45	{
46		.name = "4:2:0 2 Planes",
47		.fourcc = V4L2_PIX_FMT_NV12M,
48		.codec_mode = S5P_MFC_CODEC_NONE,
49		.type = MFC_FMT_RAW,
50		.num_planes = 2,
51	},
52	{
53		.name = "H264 Encoded Stream",
54		.fourcc = V4L2_PIX_FMT_H264,
55		.codec_mode = S5P_MFC_CODEC_H264_ENC,
56		.type = MFC_FMT_ENC,
57		.num_planes = 1,
58	},
59	{
60		.name = "MPEG4 Encoded Stream",
61		.fourcc = V4L2_PIX_FMT_MPEG4,
62		.codec_mode = S5P_MFC_CODEC_MPEG4_ENC,
63		.type = MFC_FMT_ENC,
64		.num_planes = 1,
65	},
66	{
67		.name = "H263 Encoded Stream",
68		.fourcc = V4L2_PIX_FMT_H263,
69		.codec_mode = S5P_MFC_CODEC_H263_ENC,
70		.type = MFC_FMT_ENC,
71		.num_planes = 1,
72	},
73};
74
75#define NUM_FORMATS ARRAY_SIZE(formats)
76static struct s5p_mfc_fmt *find_format(struct v4l2_format *f, unsigned int t)
77{
78	unsigned int i;
79
80	for (i = 0; i < NUM_FORMATS; i++) {
81		if (formats[i].fourcc == f->fmt.pix_mp.pixelformat &&
82		    formats[i].type == t)
83			return &formats[i];
84	}
85	return NULL;
86}
87
88static struct mfc_control controls[] = {
89	{
90		.id = V4L2_CID_MPEG_VIDEO_GOP_SIZE,
91		.type = V4L2_CTRL_TYPE_INTEGER,
92		.minimum = 0,
93		.maximum = (1 << 16) - 1,
94		.step = 1,
95		.default_value = 0,
96	},
97	{
98		.id = V4L2_CID_MPEG_VIDEO_MULTI_SLICE_MODE,
99		.type = V4L2_CTRL_TYPE_MENU,
100		.minimum = V4L2_MPEG_VIDEO_MULTI_SLICE_MODE_SINGLE,
101		.maximum = V4L2_MPEG_VIDEO_MULTI_SICE_MODE_MAX_BYTES,
102		.default_value = V4L2_MPEG_VIDEO_MULTI_SLICE_MODE_SINGLE,
103		.menu_skip_mask = 0,
104	},
105	{
106		.id = V4L2_CID_MPEG_VIDEO_MULTI_SLICE_MAX_MB,
107		.type = V4L2_CTRL_TYPE_INTEGER,
108		.minimum = 1,
109		.maximum = (1 << 16) - 1,
110		.step = 1,
111		.default_value = 1,
112	},
113	{
114		.id = V4L2_CID_MPEG_VIDEO_MULTI_SLICE_MAX_BYTES,
115		.type = V4L2_CTRL_TYPE_INTEGER,
116		.minimum = 1900,
117		.maximum = (1 << 30) - 1,
118		.step = 1,
119		.default_value = 1900,
120	},
121	{
122		.id = V4L2_CID_MPEG_VIDEO_CYCLIC_INTRA_REFRESH_MB,
123		.type = V4L2_CTRL_TYPE_INTEGER,
124		.minimum = 0,
125		.maximum = (1 << 16) - 1,
126		.step = 1,
127		.default_value = 0,
128	},
129	{
130		.id = V4L2_CID_MPEG_MFC51_VIDEO_PADDING,
131		.type = V4L2_CTRL_TYPE_BOOLEAN,
132		.name = "Padding Control Enable",
133		.minimum = 0,
134		.maximum = 1,
135		.step = 1,
136		.default_value = 0,
137	},
138	{
139		.id = V4L2_CID_MPEG_MFC51_VIDEO_PADDING_YUV,
140		.type = V4L2_CTRL_TYPE_INTEGER,
141		.name = "Padding Color YUV Value",
142		.minimum = 0,
143		.maximum = (1 << 25) - 1,
144		.step = 1,
145		.default_value = 0,
146	},
147	{
148		.id = V4L2_CID_MPEG_VIDEO_FRAME_RC_ENABLE,
149		.type = V4L2_CTRL_TYPE_BOOLEAN,
150		.minimum = 0,
151		.maximum = 1,
152		.step = 1,
153		.default_value = 0,
154	},
155	{
156		.id = V4L2_CID_MPEG_VIDEO_BITRATE,
157		.type = V4L2_CTRL_TYPE_INTEGER,
158		.minimum = 1,
159		.maximum = (1 << 30) - 1,
160		.step = 1,
161		.default_value = 1,
162	},
163	{
164		.id = V4L2_CID_MPEG_MFC51_VIDEO_RC_REACTION_COEFF,
165		.type = V4L2_CTRL_TYPE_INTEGER,
166		.name = "Rate Control Reaction Coeff.",
167		.minimum = 1,
168		.maximum = (1 << 16) - 1,
169		.step = 1,
170		.default_value = 1,
171	},
172	{
173		.id = V4L2_CID_MPEG_MFC51_VIDEO_FORCE_FRAME_TYPE,
174		.type = V4L2_CTRL_TYPE_MENU,
175		.name = "Force frame type",
176		.minimum = V4L2_MPEG_MFC51_VIDEO_FORCE_FRAME_TYPE_DISABLED,
177		.maximum = V4L2_MPEG_MFC51_VIDEO_FORCE_FRAME_TYPE_NOT_CODED,
178		.default_value = V4L2_MPEG_MFC51_VIDEO_FORCE_FRAME_TYPE_DISABLED,
179		.menu_skip_mask = 0,
180	},
181	{
182		.id = V4L2_CID_MPEG_VIDEO_VBV_SIZE,
183		.type = V4L2_CTRL_TYPE_INTEGER,
184		.minimum = 0,
185		.maximum = (1 << 16) - 1,
186		.step = 1,
187		.default_value = 0,
188	},
189	{
190		.id = V4L2_CID_MPEG_VIDEO_H264_CPB_SIZE,
191		.type = V4L2_CTRL_TYPE_INTEGER,
192		.minimum = 0,
193		.maximum = (1 << 16) - 1,
194		.step = 1,
195		.default_value = 0,
196	},
197	{
198		.id = V4L2_CID_MPEG_VIDEO_HEADER_MODE,
199		.type = V4L2_CTRL_TYPE_MENU,
200		.minimum = V4L2_MPEG_VIDEO_HEADER_MODE_SEPARATE,
201		.maximum = V4L2_MPEG_VIDEO_HEADER_MODE_JOINED_WITH_1ST_FRAME,
202		.default_value = V4L2_MPEG_VIDEO_HEADER_MODE_SEPARATE,
203		.menu_skip_mask = 0,
204	},
205	{
206		.id = V4L2_CID_MPEG_MFC51_VIDEO_FRAME_SKIP_MODE,
207		.type = V4L2_CTRL_TYPE_MENU,
208		.name = "Frame Skip Enable",
209		.minimum = V4L2_MPEG_MFC51_VIDEO_FRAME_SKIP_MODE_DISABLED,
210		.maximum = V4L2_MPEG_MFC51_VIDEO_FRAME_SKIP_MODE_BUF_LIMIT,
211		.menu_skip_mask = 0,
212		.default_value = V4L2_MPEG_MFC51_VIDEO_FRAME_SKIP_MODE_DISABLED,
213	},
214	{
215		.id = V4L2_CID_MPEG_MFC51_VIDEO_RC_FIXED_TARGET_BIT,
216		.type = V4L2_CTRL_TYPE_BOOLEAN,
217		.name = "Fixed Target Bit Enable",
218		.minimum = 0,
219		.maximum = 1,
220		.default_value = 0,
221		.menu_skip_mask = 0,
222	},
223	{
224		.id = V4L2_CID_MPEG_VIDEO_B_FRAMES,
225		.type = V4L2_CTRL_TYPE_INTEGER,
226		.minimum = 0,
227		.maximum = 2,
228		.step = 1,
229		.default_value = 0,
230	},
231	{
232		.id = V4L2_CID_MPEG_VIDEO_H264_PROFILE,
233		.type = V4L2_CTRL_TYPE_MENU,
234		.minimum = V4L2_MPEG_VIDEO_H264_PROFILE_BASELINE,
235		.maximum = V4L2_MPEG_VIDEO_H264_PROFILE_MULTIVIEW_HIGH,
236		.default_value = V4L2_MPEG_VIDEO_H264_PROFILE_BASELINE,
237		.menu_skip_mask = ~(
238				(1 << V4L2_MPEG_VIDEO_H264_PROFILE_BASELINE) |
239				(1 << V4L2_MPEG_VIDEO_H264_PROFILE_MAIN) |
240				(1 << V4L2_MPEG_VIDEO_H264_PROFILE_HIGH)
241				),
242	},
243	{
244		.id = V4L2_CID_MPEG_VIDEO_H264_LEVEL,
245		.type = V4L2_CTRL_TYPE_MENU,
246		.minimum = V4L2_MPEG_VIDEO_H264_LEVEL_1_0,
247		.maximum = V4L2_MPEG_VIDEO_H264_LEVEL_4_0,
248		.default_value = V4L2_MPEG_VIDEO_H264_LEVEL_1_0,
249	},
250	{
251		.id = V4L2_CID_MPEG_VIDEO_MPEG4_LEVEL,
252		.type = V4L2_CTRL_TYPE_MENU,
253		.minimum = V4L2_MPEG_VIDEO_MPEG4_LEVEL_0,
254		.maximum = V4L2_MPEG_VIDEO_MPEG4_LEVEL_5,
255		.default_value = V4L2_MPEG_VIDEO_MPEG4_LEVEL_0,
256		.menu_skip_mask = 0,
257	},
258	{
259		.id = V4L2_CID_MPEG_VIDEO_H264_LOOP_FILTER_MODE,
260		.type = V4L2_CTRL_TYPE_MENU,
261		.minimum = V4L2_MPEG_VIDEO_H264_LOOP_FILTER_MODE_ENABLED,
262		.maximum = V4L2_MPEG_VIDEO_H264_LOOP_FILTER_MODE_DISABLED_AT_SLICE_BOUNDARY,
263		.default_value = V4L2_MPEG_VIDEO_H264_LOOP_FILTER_MODE_ENABLED,
264		.menu_skip_mask = 0,
265	},
266	{
267		.id = V4L2_CID_MPEG_VIDEO_H264_LOOP_FILTER_ALPHA,
268		.type = V4L2_CTRL_TYPE_INTEGER,
269		.minimum = -6,
270		.maximum = 6,
271		.step = 1,
272		.default_value = 0,
273	},
274	{
275		.id = V4L2_CID_MPEG_VIDEO_H264_LOOP_FILTER_BETA,
276		.type = V4L2_CTRL_TYPE_INTEGER,
277		.minimum = -6,
278		.maximum = 6,
279		.step = 1,
280		.default_value = 0,
281	},
282	{
283		.id = V4L2_CID_MPEG_VIDEO_H264_ENTROPY_MODE,
284		.type = V4L2_CTRL_TYPE_MENU,
285		.minimum = V4L2_MPEG_VIDEO_H264_ENTROPY_MODE_CAVLC,
286		.maximum = V4L2_MPEG_VIDEO_H264_ENTROPY_MODE_CABAC,
287		.default_value = V4L2_MPEG_VIDEO_H264_ENTROPY_MODE_CAVLC,
288		.menu_skip_mask = 0,
289	},
290	{
291		.id = V4L2_CID_MPEG_MFC51_VIDEO_H264_NUM_REF_PIC_FOR_P,
292		.type = V4L2_CTRL_TYPE_INTEGER,
293		.name = "The Number of Ref. Pic for P",
294		.minimum = 1,
295		.maximum = 2,
296		.step = 1,
297		.default_value = 1,
298	},
299	{
300		.id = V4L2_CID_MPEG_VIDEO_H264_8X8_TRANSFORM,
301		.type = V4L2_CTRL_TYPE_BOOLEAN,
302		.minimum = 0,
303		.maximum = 1,
304		.step = 1,
305		.default_value = 0,
306	},
307	{
308		.id = V4L2_CID_MPEG_VIDEO_MB_RC_ENABLE,
309		.type = V4L2_CTRL_TYPE_BOOLEAN,
310		.minimum = 0,
311		.maximum = 1,
312		.step = 1,
313		.default_value = 0,
314	},
315	{
316		.id = V4L2_CID_MPEG_VIDEO_H264_I_FRAME_QP,
317		.type = V4L2_CTRL_TYPE_INTEGER,
318		.minimum = 0,
319		.maximum = 51,
320		.step = 1,
321		.default_value = 1,
322	},
323	{
324		.id = V4L2_CID_MPEG_VIDEO_H264_MIN_QP,
325		.type = V4L2_CTRL_TYPE_INTEGER,
326		.minimum = 0,
327		.maximum = 51,
328		.step = 1,
329		.default_value = 1,
330	},
331	{
332		.id = V4L2_CID_MPEG_VIDEO_H264_MAX_QP,
333		.type = V4L2_CTRL_TYPE_INTEGER,
334		.minimum = 0,
335		.maximum = 51,
336		.step = 1,
337		.default_value = 1,
338	},
339	{
340		.id = V4L2_CID_MPEG_VIDEO_H264_P_FRAME_QP,
341		.type = V4L2_CTRL_TYPE_INTEGER,
342		.minimum = 0,
343		.maximum = 51,
344		.step = 1,
345		.default_value = 1,
346	},
347	{
348		.id = V4L2_CID_MPEG_VIDEO_H264_B_FRAME_QP,
349		.type = V4L2_CTRL_TYPE_INTEGER,
350		.minimum = 0,
351		.maximum = 51,
352		.step = 1,
353		.default_value = 1,
354	},
355	{
356		.id = V4L2_CID_MPEG_VIDEO_H263_I_FRAME_QP,
357		.type = V4L2_CTRL_TYPE_INTEGER,
358		.name = "H263 I-Frame QP value",
359		.minimum = 1,
360		.maximum = 31,
361		.step = 1,
362		.default_value = 1,
363	},
364	{
365		.id = V4L2_CID_MPEG_VIDEO_H263_MIN_QP,
366		.type = V4L2_CTRL_TYPE_INTEGER,
367		.name = "H263 Minimum QP value",
368		.minimum = 1,
369		.maximum = 31,
370		.step = 1,
371		.default_value = 1,
372	},
373	{
374		.id = V4L2_CID_MPEG_VIDEO_H263_MAX_QP,
375		.type = V4L2_CTRL_TYPE_INTEGER,
376		.name = "H263 Maximum QP value",
377		.minimum = 1,
378		.maximum = 31,
379		.step = 1,
380		.default_value = 1,
381	},
382	{
383		.id = V4L2_CID_MPEG_VIDEO_H263_P_FRAME_QP,
384		.type = V4L2_CTRL_TYPE_INTEGER,
385		.name = "H263 P frame QP value",
386		.minimum = 1,
387		.maximum = 31,
388		.step = 1,
389		.default_value = 1,
390	},
391	{
392		.id = V4L2_CID_MPEG_VIDEO_H263_B_FRAME_QP,
393		.type = V4L2_CTRL_TYPE_INTEGER,
394		.name = "H263 B frame QP value",
395		.minimum = 1,
396		.maximum = 31,
397		.step = 1,
398		.default_value = 1,
399	},
400	{
401		.id = V4L2_CID_MPEG_VIDEO_MPEG4_I_FRAME_QP,
402		.type = V4L2_CTRL_TYPE_INTEGER,
403		.name = "MPEG4 I-Frame QP value",
404		.minimum = 1,
405		.maximum = 31,
406		.step = 1,
407		.default_value = 1,
408	},
409	{
410		.id = V4L2_CID_MPEG_VIDEO_MPEG4_MIN_QP,
411		.type = V4L2_CTRL_TYPE_INTEGER,
412		.name = "MPEG4 Minimum QP value",
413		.minimum = 1,
414		.maximum = 31,
415		.step = 1,
416		.default_value = 1,
417	},
418	{
419		.id = V4L2_CID_MPEG_VIDEO_MPEG4_MAX_QP,
420		.type = V4L2_CTRL_TYPE_INTEGER,
421		.name = "MPEG4 Maximum QP value",
422		.minimum = 0,
423		.maximum = 51,
424		.step = 1,
425		.default_value = 1,
426	},
427	{
428		.id = V4L2_CID_MPEG_VIDEO_MPEG4_P_FRAME_QP,
429		.type = V4L2_CTRL_TYPE_INTEGER,
430		.name = "MPEG4 P frame QP value",
431		.minimum = 1,
432		.maximum = 31,
433		.step = 1,
434		.default_value = 1,
435	},
436	{
437		.id = V4L2_CID_MPEG_VIDEO_MPEG4_B_FRAME_QP,
438		.type = V4L2_CTRL_TYPE_INTEGER,
439		.name = "MPEG4 B frame QP value",
440		.minimum = 1,
441		.maximum = 31,
442		.step = 1,
443		.default_value = 1,
444	},
445	{
446		.id = V4L2_CID_MPEG_MFC51_VIDEO_H264_ADAPTIVE_RC_DARK,
447		.type = V4L2_CTRL_TYPE_BOOLEAN,
448		.name = "H264 Dark Reg Adaptive RC",
449		.minimum = 0,
450		.maximum = 1,
451		.step = 1,
452		.default_value = 0,
453	},
454	{
455		.id = V4L2_CID_MPEG_MFC51_VIDEO_H264_ADAPTIVE_RC_SMOOTH,
456		.type = V4L2_CTRL_TYPE_BOOLEAN,
457		.name = "H264 Smooth Reg Adaptive RC",
458		.minimum = 0,
459		.maximum = 1,
460		.step = 1,
461		.default_value = 0,
462	},
463	{
464		.id = V4L2_CID_MPEG_MFC51_VIDEO_H264_ADAPTIVE_RC_STATIC,
465		.type = V4L2_CTRL_TYPE_BOOLEAN,
466		.name = "H264 Static Reg Adaptive RC",
467		.minimum = 0,
468		.maximum = 1,
469		.step = 1,
470		.default_value = 0,
471	},
472	{
473		.id = V4L2_CID_MPEG_MFC51_VIDEO_H264_ADAPTIVE_RC_ACTIVITY,
474		.type = V4L2_CTRL_TYPE_BOOLEAN,
475		.name = "H264 Activity Reg Adaptive RC",
476		.minimum = 0,
477		.maximum = 1,
478		.step = 1,
479		.default_value = 0,
480	},
481	{
482		.id = V4L2_CID_MPEG_VIDEO_H264_VUI_SAR_ENABLE,
483		.type = V4L2_CTRL_TYPE_BOOLEAN,
484		.minimum = 0,
485		.maximum = 1,
486		.step = 1,
487		.default_value = 0,
488	},
489	{
490		.id = V4L2_CID_MPEG_VIDEO_H264_VUI_SAR_IDC,
491		.type = V4L2_CTRL_TYPE_MENU,
492		.minimum = V4L2_MPEG_VIDEO_H264_VUI_SAR_IDC_UNSPECIFIED,
493		.maximum = V4L2_MPEG_VIDEO_H264_VUI_SAR_IDC_EXTENDED,
494		.default_value = V4L2_MPEG_VIDEO_H264_VUI_SAR_IDC_UNSPECIFIED,
495		.menu_skip_mask = 0,
496	},
497	{
498		.id = V4L2_CID_MPEG_VIDEO_H264_VUI_EXT_SAR_WIDTH,
499		.type = V4L2_CTRL_TYPE_INTEGER,
500		.minimum = 0,
501		.maximum = (1 << 16) - 1,
502		.step = 1,
503		.default_value = 0,
504	},
505	{
506		.id = V4L2_CID_MPEG_VIDEO_H264_VUI_EXT_SAR_HEIGHT,
507		.type = V4L2_CTRL_TYPE_INTEGER,
508		.minimum = 0,
509		.maximum = (1 << 16) - 1,
510		.step = 1,
511		.default_value = 0,
512	},
513	{
514		.id = V4L2_CID_MPEG_VIDEO_GOP_CLOSURE,
515		.type = V4L2_CTRL_TYPE_BOOLEAN,
516		.minimum = 0,
517		.maximum = 1,
518		.step = 1,
519		.default_value = 1,
520	},
521	{
522		.id = V4L2_CID_MPEG_VIDEO_H264_I_PERIOD,
523		.type = V4L2_CTRL_TYPE_INTEGER,
524		.minimum = 0,
525		.maximum = (1 << 16) - 1,
526		.step = 1,
527		.default_value = 0,
528	},
529	{
530		.id = V4L2_CID_MPEG_VIDEO_MPEG4_PROFILE,
531		.type = V4L2_CTRL_TYPE_MENU,
532		.minimum = V4L2_MPEG_VIDEO_MPEG4_PROFILE_SIMPLE,
533		.maximum = V4L2_MPEG_VIDEO_MPEG4_PROFILE_ADVANCED_SIMPLE,
534		.default_value = V4L2_MPEG_VIDEO_MPEG4_PROFILE_SIMPLE,
535		.menu_skip_mask = 0,
536	},
537	{
538		.id = V4L2_CID_MPEG_VIDEO_MPEG4_QPEL,
539		.type = V4L2_CTRL_TYPE_BOOLEAN,
540		.minimum = 0,
541		.maximum = 1,
542		.step = 1,
543		.default_value = 0,
544	},
545};
546
547#define NUM_CTRLS ARRAY_SIZE(controls)
548static const char * const *mfc51_get_menu(u32 id)
549{
550	static const char * const mfc51_video_frame_skip[] = {
551		"Disabled",
552		"Level Limit",
553		"VBV/CPB Limit",
554		NULL,
555	};
556	static const char * const mfc51_video_force_frame[] = {
557		"Disabled",
558		"I Frame",
559		"Not Coded",
560		NULL,
561	};
562	switch (id) {
563	case V4L2_CID_MPEG_MFC51_VIDEO_FRAME_SKIP_MODE:
564		return mfc51_video_frame_skip;
565	case V4L2_CID_MPEG_MFC51_VIDEO_FORCE_FRAME_TYPE:
566		return mfc51_video_force_frame;
567	}
568	return NULL;
569}
570
571static int s5p_mfc_ctx_ready(struct s5p_mfc_ctx *ctx)
572{
573	mfc_debug(2, "src=%d, dst=%d, state=%d\n",
574		  ctx->src_queue_cnt, ctx->dst_queue_cnt, ctx->state);
575	/* context is ready to make header */
576	if (ctx->state == MFCINST_GOT_INST && ctx->dst_queue_cnt >= 1)
577		return 1;
578	/* context is ready to encode a frame */
579	if (ctx->state == MFCINST_RUNNING &&
580		ctx->src_queue_cnt >= 1 && ctx->dst_queue_cnt >= 1)
581		return 1;
582	/* context is ready to encode remaining frames */
583	if (ctx->state == MFCINST_FINISHING &&
584		ctx->dst_queue_cnt >= 1)
585		return 1;
586	mfc_debug(2, "ctx is not ready\n");
587	return 0;
588}
589
590static void cleanup_ref_queue(struct s5p_mfc_ctx *ctx)
591{
592	struct s5p_mfc_buf *mb_entry;
593	unsigned long mb_y_addr, mb_c_addr;
594
595	/* move buffers in ref queue to src queue */
596	while (!list_empty(&ctx->ref_queue)) {
597		mb_entry = list_entry((&ctx->ref_queue)->next,
598						struct s5p_mfc_buf, list);
599		mb_y_addr = vb2_dma_contig_plane_dma_addr(mb_entry->b, 0);
600		mb_c_addr = vb2_dma_contig_plane_dma_addr(mb_entry->b, 1);
601		list_del(&mb_entry->list);
602		ctx->ref_queue_cnt--;
603		list_add_tail(&mb_entry->list, &ctx->src_queue);
604		ctx->src_queue_cnt++;
605	}
606	mfc_debug(2, "enc src count: %d, enc ref count: %d\n",
607		  ctx->src_queue_cnt, ctx->ref_queue_cnt);
608	INIT_LIST_HEAD(&ctx->ref_queue);
609	ctx->ref_queue_cnt = 0;
610}
611
612static int enc_pre_seq_start(struct s5p_mfc_ctx *ctx)
613{
614	struct s5p_mfc_dev *dev = ctx->dev;
615	struct s5p_mfc_buf *dst_mb;
616	unsigned long dst_addr;
617	unsigned int dst_size;
618	unsigned long flags;
619
620	spin_lock_irqsave(&dev->irqlock, flags);
621	dst_mb = list_entry(ctx->dst_queue.next, struct s5p_mfc_buf, list);
622	dst_addr = vb2_dma_contig_plane_dma_addr(dst_mb->b, 0);
623	dst_size = vb2_plane_size(dst_mb->b, 0);
624	s5p_mfc_hw_call(dev->mfc_ops, set_enc_stream_buffer, ctx, dst_addr,
625			dst_size);
626	spin_unlock_irqrestore(&dev->irqlock, flags);
627	return 0;
628}
629
630static int enc_post_seq_start(struct s5p_mfc_ctx *ctx)
631{
632	struct s5p_mfc_dev *dev = ctx->dev;
633	struct s5p_mfc_enc_params *p = &ctx->enc_params;
634	struct s5p_mfc_buf *dst_mb;
635	unsigned long flags;
636
637	if (p->seq_hdr_mode == V4L2_MPEG_VIDEO_HEADER_MODE_SEPARATE) {
638		spin_lock_irqsave(&dev->irqlock, flags);
639		dst_mb = list_entry(ctx->dst_queue.next,
640				struct s5p_mfc_buf, list);
641		list_del(&dst_mb->list);
642		ctx->dst_queue_cnt--;
643		vb2_set_plane_payload(dst_mb->b, 0,
644			s5p_mfc_hw_call(dev->mfc_ops, get_enc_strm_size, dev));
645		vb2_buffer_done(dst_mb->b, VB2_BUF_STATE_DONE);
646		spin_unlock_irqrestore(&dev->irqlock, flags);
647	}
648	ctx->state = MFCINST_RUNNING;
649	if (s5p_mfc_ctx_ready(ctx))
650		set_work_bit_irqsave(ctx);
651	s5p_mfc_hw_call(dev->mfc_ops, try_run, dev);
652	return 0;
653}
654
655static int enc_pre_frame_start(struct s5p_mfc_ctx *ctx)
656{
657	struct s5p_mfc_dev *dev = ctx->dev;
658	struct s5p_mfc_buf *dst_mb;
659	struct s5p_mfc_buf *src_mb;
660	unsigned long flags;
661	unsigned long src_y_addr, src_c_addr, dst_addr;
662	unsigned int dst_size;
663
664	spin_lock_irqsave(&dev->irqlock, flags);
665	src_mb = list_entry(ctx->src_queue.next, struct s5p_mfc_buf, list);
666	src_y_addr = vb2_dma_contig_plane_dma_addr(src_mb->b, 0);
667	src_c_addr = vb2_dma_contig_plane_dma_addr(src_mb->b, 1);
668	s5p_mfc_hw_call(dev->mfc_ops, set_enc_frame_buffer, ctx, src_y_addr,
669			src_c_addr);
670	spin_unlock_irqrestore(&dev->irqlock, flags);
671
672	spin_lock_irqsave(&dev->irqlock, flags);
673	dst_mb = list_entry(ctx->dst_queue.next, struct s5p_mfc_buf, list);
674	dst_addr = vb2_dma_contig_plane_dma_addr(dst_mb->b, 0);
675	dst_size = vb2_plane_size(dst_mb->b, 0);
676	s5p_mfc_hw_call(dev->mfc_ops, set_enc_stream_buffer, ctx, dst_addr,
677			dst_size);
678	spin_unlock_irqrestore(&dev->irqlock, flags);
679
680	return 0;
681}
682
683static int enc_post_frame_start(struct s5p_mfc_ctx *ctx)
684{
685	struct s5p_mfc_dev *dev = ctx->dev;
686	struct s5p_mfc_buf *mb_entry;
687	unsigned long enc_y_addr, enc_c_addr;
688	unsigned long mb_y_addr, mb_c_addr;
689	int slice_type;
690	unsigned int strm_size;
691	unsigned long flags;
692
693	slice_type = s5p_mfc_hw_call(dev->mfc_ops, get_enc_slice_type, dev);
694	strm_size = s5p_mfc_hw_call(dev->mfc_ops, get_enc_strm_size, dev);
695	mfc_debug(2, "Encoded slice type: %d", slice_type);
696	mfc_debug(2, "Encoded stream size: %d", strm_size);
697	mfc_debug(2, "Display order: %d",
698		  mfc_read(dev, S5P_FIMV_ENC_SI_PIC_CNT));
699	spin_lock_irqsave(&dev->irqlock, flags);
700	if (slice_type >= 0) {
701		s5p_mfc_hw_call(dev->mfc_ops, get_enc_frame_buffer, ctx,
702				&enc_y_addr, &enc_c_addr);
703		list_for_each_entry(mb_entry, &ctx->src_queue, list) {
704			mb_y_addr = vb2_dma_contig_plane_dma_addr(mb_entry->b, 0);
705			mb_c_addr = vb2_dma_contig_plane_dma_addr(mb_entry->b, 1);
706			if ((enc_y_addr == mb_y_addr) &&
707						(enc_c_addr == mb_c_addr)) {
708				list_del(&mb_entry->list);
709				ctx->src_queue_cnt--;
710				vb2_buffer_done(mb_entry->b,
711							VB2_BUF_STATE_DONE);
712				break;
713			}
714		}
715		list_for_each_entry(mb_entry, &ctx->ref_queue, list) {
716			mb_y_addr = vb2_dma_contig_plane_dma_addr(mb_entry->b, 0);
717			mb_c_addr = vb2_dma_contig_plane_dma_addr(mb_entry->b, 1);
718			if ((enc_y_addr == mb_y_addr) &&
719						(enc_c_addr == mb_c_addr)) {
720				list_del(&mb_entry->list);
721				ctx->ref_queue_cnt--;
722				vb2_buffer_done(mb_entry->b,
723							VB2_BUF_STATE_DONE);
724				break;
725			}
726		}
727	}
728	if ((ctx->src_queue_cnt > 0) && (ctx->state == MFCINST_RUNNING)) {
729		mb_entry = list_entry(ctx->src_queue.next, struct s5p_mfc_buf,
730									list);
731		if (mb_entry->flags & MFC_BUF_FLAG_USED) {
732			list_del(&mb_entry->list);
733			ctx->src_queue_cnt--;
734			list_add_tail(&mb_entry->list, &ctx->ref_queue);
735			ctx->ref_queue_cnt++;
736		}
737		mfc_debug(2, "enc src count: %d, enc ref count: %d\n",
738			  ctx->src_queue_cnt, ctx->ref_queue_cnt);
739	}
740	if (strm_size > 0) {
741		/* at least one more dest. buffers exist always  */
742		mb_entry = list_entry(ctx->dst_queue.next, struct s5p_mfc_buf,
743									list);
744		list_del(&mb_entry->list);
745		ctx->dst_queue_cnt--;
746		switch (slice_type) {
747		case S5P_FIMV_ENC_SI_SLICE_TYPE_I:
748			mb_entry->b->v4l2_buf.flags |= V4L2_BUF_FLAG_KEYFRAME;
749			break;
750		case S5P_FIMV_ENC_SI_SLICE_TYPE_P:
751			mb_entry->b->v4l2_buf.flags |= V4L2_BUF_FLAG_PFRAME;
752			break;
753		case S5P_FIMV_ENC_SI_SLICE_TYPE_B:
754			mb_entry->b->v4l2_buf.flags |= V4L2_BUF_FLAG_BFRAME;
755			break;
756		}
757		vb2_set_plane_payload(mb_entry->b, 0, strm_size);
758		vb2_buffer_done(mb_entry->b, VB2_BUF_STATE_DONE);
759	}
760	spin_unlock_irqrestore(&dev->irqlock, flags);
761	if ((ctx->src_queue_cnt == 0) || (ctx->dst_queue_cnt == 0))
762		clear_work_bit(ctx);
763	return 0;
764}
765
766static struct s5p_mfc_codec_ops encoder_codec_ops = {
767	.pre_seq_start		= enc_pre_seq_start,
768	.post_seq_start		= enc_post_seq_start,
769	.pre_frame_start	= enc_pre_frame_start,
770	.post_frame_start	= enc_post_frame_start,
771};
772
773/* Query capabilities of the device */
774static int vidioc_querycap(struct file *file, void *priv,
775			   struct v4l2_capability *cap)
776{
777	struct s5p_mfc_dev *dev = video_drvdata(file);
778
779	strncpy(cap->driver, dev->plat_dev->name, sizeof(cap->driver) - 1);
780	strncpy(cap->card, dev->plat_dev->name, sizeof(cap->card) - 1);
781	cap->bus_info[0] = 0;
782	cap->version = KERNEL_VERSION(1, 0, 0);
783	/*
784	 * This is only a mem-to-mem video device. The capture and output
785	 * device capability flags are left only for backward compatibility
786	 * and are scheduled for removal.
787	 */
788	cap->capabilities = V4L2_CAP_VIDEO_M2M_MPLANE | V4L2_CAP_STREAMING |
789			    V4L2_CAP_VIDEO_CAPTURE_MPLANE |
790			    V4L2_CAP_VIDEO_OUTPUT_MPLANE;
791	return 0;
792}
793
794static int vidioc_enum_fmt(struct v4l2_fmtdesc *f, bool mplane, bool out)
795{
796	struct s5p_mfc_fmt *fmt;
797	int i, j = 0;
798
799	for (i = 0; i < ARRAY_SIZE(formats); ++i) {
800		if (mplane && formats[i].num_planes == 1)
801			continue;
802		else if (!mplane && formats[i].num_planes > 1)
803			continue;
804		if (out && formats[i].type != MFC_FMT_RAW)
805			continue;
806		else if (!out && formats[i].type != MFC_FMT_ENC)
807			continue;
808		if (j == f->index) {
809			fmt = &formats[i];
810			strlcpy(f->description, fmt->name,
811				sizeof(f->description));
812			f->pixelformat = fmt->fourcc;
813			return 0;
814		}
815		++j;
816	}
817	return -EINVAL;
818}
819
820static int vidioc_enum_fmt_vid_cap(struct file *file, void *pirv,
821				   struct v4l2_fmtdesc *f)
822{
823	return vidioc_enum_fmt(f, false, false);
824}
825
826static int vidioc_enum_fmt_vid_cap_mplane(struct file *file, void *pirv,
827					  struct v4l2_fmtdesc *f)
828{
829	return vidioc_enum_fmt(f, true, false);
830}
831
832static int vidioc_enum_fmt_vid_out(struct file *file, void *prov,
833				   struct v4l2_fmtdesc *f)
834{
835	return vidioc_enum_fmt(f, false, true);
836}
837
838static int vidioc_enum_fmt_vid_out_mplane(struct file *file, void *prov,
839					  struct v4l2_fmtdesc *f)
840{
841	return vidioc_enum_fmt(f, true, true);
842}
843
844static int vidioc_g_fmt(struct file *file, void *priv, struct v4l2_format *f)
845{
846	struct s5p_mfc_ctx *ctx = fh_to_ctx(priv);
847	struct v4l2_pix_format_mplane *pix_fmt_mp = &f->fmt.pix_mp;
848
849	mfc_debug(2, "f->type = %d ctx->state = %d\n", f->type, ctx->state);
850	if (f->type == V4L2_BUF_TYPE_VIDEO_CAPTURE_MPLANE) {
851		/* This is run on output (encoder dest) */
852		pix_fmt_mp->width = 0;
853		pix_fmt_mp->height = 0;
854		pix_fmt_mp->field = V4L2_FIELD_NONE;
855		pix_fmt_mp->pixelformat = ctx->dst_fmt->fourcc;
856		pix_fmt_mp->num_planes = ctx->dst_fmt->num_planes;
857
858		pix_fmt_mp->plane_fmt[0].bytesperline = ctx->enc_dst_buf_size;
859		pix_fmt_mp->plane_fmt[0].sizeimage = ctx->enc_dst_buf_size;
860	} else if (f->type == V4L2_BUF_TYPE_VIDEO_OUTPUT_MPLANE) {
861		/* This is run on capture (encoder src) */
862		pix_fmt_mp->width = ctx->img_width;
863		pix_fmt_mp->height = ctx->img_height;
864
865		pix_fmt_mp->field = V4L2_FIELD_NONE;
866		pix_fmt_mp->pixelformat = ctx->src_fmt->fourcc;
867		pix_fmt_mp->num_planes = ctx->src_fmt->num_planes;
868
869		pix_fmt_mp->plane_fmt[0].bytesperline = ctx->buf_width;
870		pix_fmt_mp->plane_fmt[0].sizeimage = ctx->luma_size;
871		pix_fmt_mp->plane_fmt[1].bytesperline = ctx->buf_width;
872		pix_fmt_mp->plane_fmt[1].sizeimage = ctx->chroma_size;
873	} else {
874		mfc_err("invalid buf type\n");
875		return -EINVAL;
876	}
877	return 0;
878}
879
880static int vidioc_try_fmt(struct file *file, void *priv, struct v4l2_format *f)
881{
882	struct s5p_mfc_fmt *fmt;
883	struct v4l2_pix_format_mplane *pix_fmt_mp = &f->fmt.pix_mp;
884
885	if (f->type == V4L2_BUF_TYPE_VIDEO_CAPTURE_MPLANE) {
886		fmt = find_format(f, MFC_FMT_ENC);
887		if (!fmt) {
888			mfc_err("failed to try output format\n");
889			return -EINVAL;
890		}
891
892		if (pix_fmt_mp->plane_fmt[0].sizeimage == 0) {
893			mfc_err("must be set encoding output size\n");
894			return -EINVAL;
895		}
896
897		pix_fmt_mp->plane_fmt[0].bytesperline =
898			pix_fmt_mp->plane_fmt[0].sizeimage;
899	} else if (f->type == V4L2_BUF_TYPE_VIDEO_OUTPUT_MPLANE) {
900		fmt = find_format(f, MFC_FMT_RAW);
901		if (!fmt) {
902			mfc_err("failed to try output format\n");
903			return -EINVAL;
904		}
905
906		if (fmt->num_planes != pix_fmt_mp->num_planes) {
907			mfc_err("failed to try output format\n");
908			return -EINVAL;
909		}
910		v4l_bound_align_image(&pix_fmt_mp->width, 8, 1920, 1,
911			&pix_fmt_mp->height, 4, 1080, 1, 0);
912	} else {
913		mfc_err("invalid buf type\n");
914		return -EINVAL;
915	}
916	return 0;
917}
918
919static int vidioc_s_fmt(struct file *file, void *priv, struct v4l2_format *f)
920{
921	struct s5p_mfc_dev *dev = video_drvdata(file);
922	struct s5p_mfc_ctx *ctx = fh_to_ctx(priv);
923	struct s5p_mfc_fmt *fmt;
924	struct v4l2_pix_format_mplane *pix_fmt_mp = &f->fmt.pix_mp;
925	int ret = 0;
926
927	ret = vidioc_try_fmt(file, priv, f);
928	if (ret)
929		return ret;
930	if (ctx->vq_src.streaming || ctx->vq_dst.streaming) {
931		v4l2_err(&dev->v4l2_dev, "%s queue busy\n", __func__);
932		ret = -EBUSY;
933		goto out;
934	}
935	if (f->type == V4L2_BUF_TYPE_VIDEO_CAPTURE_MPLANE) {
936		fmt = find_format(f, MFC_FMT_ENC);
937		if (!fmt) {
938			mfc_err("failed to set capture format\n");
939			return -EINVAL;
940		}
941		ctx->state = MFCINST_INIT;
942		ctx->dst_fmt = fmt;
943		ctx->codec_mode = ctx->dst_fmt->codec_mode;
944		ctx->enc_dst_buf_size =	pix_fmt_mp->plane_fmt[0].sizeimage;
945		pix_fmt_mp->plane_fmt[0].bytesperline = 0;
946		ctx->dst_bufs_cnt = 0;
947		ctx->capture_state = QUEUE_FREE;
948		s5p_mfc_hw_call(dev->mfc_ops, alloc_instance_buffer, ctx);
949		set_work_bit_irqsave(ctx);
950		s5p_mfc_clean_ctx_int_flags(ctx);
951		s5p_mfc_hw_call(dev->mfc_ops, try_run, dev);
952		if (s5p_mfc_wait_for_done_ctx(ctx, \
953				S5P_MFC_R2H_CMD_OPEN_INSTANCE_RET, 1)) {
954				/* Error or timeout */
955			mfc_err("Error getting instance from hardware\n");
956			s5p_mfc_hw_call(dev->mfc_ops, release_instance_buffer,
957					ctx);
958			ret = -EIO;
959			goto out;
960		}
961		mfc_debug(2, "Got instance number: %d\n", ctx->inst_no);
962	} else if (f->type == V4L2_BUF_TYPE_VIDEO_OUTPUT_MPLANE) {
963		fmt = find_format(f, MFC_FMT_RAW);
964		if (!fmt) {
965			mfc_err("failed to set output format\n");
966			return -EINVAL;
967		}
968		if (fmt->num_planes != pix_fmt_mp->num_planes) {
969			mfc_err("failed to set output format\n");
970			ret = -EINVAL;
971			goto out;
972		}
973		ctx->src_fmt = fmt;
974		ctx->img_width = pix_fmt_mp->width;
975		ctx->img_height = pix_fmt_mp->height;
976		mfc_debug(2, "codec number: %d\n", ctx->src_fmt->codec_mode);
977		mfc_debug(2, "fmt - w: %d, h: %d, ctx - w: %d, h: %d\n",
978			pix_fmt_mp->width, pix_fmt_mp->height,
979			ctx->img_width, ctx->img_height);
980
981		s5p_mfc_hw_call(dev->mfc_ops, enc_calc_src_size, ctx);
982		pix_fmt_mp->plane_fmt[0].sizeimage = ctx->luma_size;
983		pix_fmt_mp->plane_fmt[0].bytesperline = ctx->buf_width;
984		pix_fmt_mp->plane_fmt[1].sizeimage = ctx->chroma_size;
985		pix_fmt_mp->plane_fmt[1].bytesperline = ctx->buf_width;
986
987		ctx->src_bufs_cnt = 0;
988		ctx->output_state = QUEUE_FREE;
989	} else {
990		mfc_err("invalid buf type\n");
991		return -EINVAL;
992	}
993out:
994	mfc_debug_leave();
995	return ret;
996}
997
998static int vidioc_reqbufs(struct file *file, void *priv,
999					  struct v4l2_requestbuffers *reqbufs)
1000{
1001	struct s5p_mfc_ctx *ctx = fh_to_ctx(priv);
1002	int ret = 0;
1003
1004	/* if memory is not mmp or userptr return error */
1005	if ((reqbufs->memory != V4L2_MEMORY_MMAP) &&
1006		(reqbufs->memory != V4L2_MEMORY_USERPTR))
1007		return -EINVAL;
1008	if (reqbufs->type == V4L2_BUF_TYPE_VIDEO_CAPTURE_MPLANE) {
1009		if (ctx->capture_state != QUEUE_FREE) {
1010			mfc_err("invalid capture state: %d\n",
1011							ctx->capture_state);
1012			return -EINVAL;
1013		}
1014		ret = vb2_reqbufs(&ctx->vq_dst, reqbufs);
1015		if (ret != 0) {
1016			mfc_err("error in vb2_reqbufs() for E(D)\n");
1017			return ret;
1018		}
1019		ctx->capture_state = QUEUE_BUFS_REQUESTED;
1020		ret = s5p_mfc_hw_call(ctx->dev->mfc_ops, alloc_codec_buffers,
1021				ctx);
1022		if (ret) {
1023			mfc_err("Failed to allocate encoding buffers\n");
1024			reqbufs->count = 0;
1025			ret = vb2_reqbufs(&ctx->vq_dst, reqbufs);
1026			return -ENOMEM;
1027		}
1028	} else if (reqbufs->type == V4L2_BUF_TYPE_VIDEO_OUTPUT_MPLANE) {
1029		if (ctx->output_state != QUEUE_FREE) {
1030			mfc_err("invalid output state: %d\n",
1031							ctx->output_state);
1032			return -EINVAL;
1033		}
1034		ret = vb2_reqbufs(&ctx->vq_src, reqbufs);
1035		if (ret != 0) {
1036			mfc_err("error in vb2_reqbufs() for E(S)\n");
1037			return ret;
1038		}
1039		ctx->output_state = QUEUE_BUFS_REQUESTED;
1040	} else {
1041		mfc_err("invalid buf type\n");
1042		return -EINVAL;
1043	}
1044	return ret;
1045}
1046
1047static int vidioc_querybuf(struct file *file, void *priv,
1048						   struct v4l2_buffer *buf)
1049{
1050	struct s5p_mfc_ctx *ctx = fh_to_ctx(priv);
1051	int ret = 0;
1052
1053	/* if memory is not mmp or userptr return error */
1054	if ((buf->memory != V4L2_MEMORY_MMAP) &&
1055		(buf->memory != V4L2_MEMORY_USERPTR))
1056		return -EINVAL;
1057	if (buf->type == V4L2_BUF_TYPE_VIDEO_CAPTURE_MPLANE) {
1058		if (ctx->state != MFCINST_GOT_INST) {
1059			mfc_err("invalid context state: %d\n", ctx->state);
1060			return -EINVAL;
1061		}
1062		ret = vb2_querybuf(&ctx->vq_dst, buf);
1063		if (ret != 0) {
1064			mfc_err("error in vb2_querybuf() for E(D)\n");
1065			return ret;
1066		}
1067		buf->m.planes[0].m.mem_offset += DST_QUEUE_OFF_BASE;
1068	} else if (buf->type == V4L2_BUF_TYPE_VIDEO_OUTPUT_MPLANE) {
1069		ret = vb2_querybuf(&ctx->vq_src, buf);
1070		if (ret != 0) {
1071			mfc_err("error in vb2_querybuf() for E(S)\n");
1072			return ret;
1073		}
1074	} else {
1075		mfc_err("invalid buf type\n");
1076		return -EINVAL;
1077	}
1078	return ret;
1079}
1080
1081/* Queue a buffer */
1082static int vidioc_qbuf(struct file *file, void *priv, struct v4l2_buffer *buf)
1083{
1084	struct s5p_mfc_ctx *ctx = fh_to_ctx(priv);
1085
1086	if (ctx->state == MFCINST_ERROR) {
1087		mfc_err("Call on QBUF after unrecoverable error\n");
1088		return -EIO;
1089	}
1090	if (buf->type == V4L2_BUF_TYPE_VIDEO_OUTPUT_MPLANE) {
1091		if (ctx->state == MFCINST_FINISHING) {
1092			mfc_err("Call on QBUF after EOS command\n");
1093			return -EIO;
1094		}
1095		return vb2_qbuf(&ctx->vq_src, buf);
1096	} else if (buf->type == V4L2_BUF_TYPE_VIDEO_CAPTURE_MPLANE) {
1097		return vb2_qbuf(&ctx->vq_dst, buf);
1098	}
1099	return -EINVAL;
1100}
1101
1102/* Dequeue a buffer */
1103static int vidioc_dqbuf(struct file *file, void *priv, struct v4l2_buffer *buf)
1104{
1105	const struct v4l2_event ev = {
1106		.type = V4L2_EVENT_EOS
1107	};
1108	struct s5p_mfc_ctx *ctx = fh_to_ctx(priv);
1109	int ret;
1110
1111	if (ctx->state == MFCINST_ERROR) {
1112		mfc_err("Call on DQBUF after unrecoverable error\n");
1113		return -EIO;
1114	}
1115	if (buf->type == V4L2_BUF_TYPE_VIDEO_OUTPUT_MPLANE) {
1116		ret = vb2_dqbuf(&ctx->vq_src, buf, file->f_flags & O_NONBLOCK);
1117	} else if (buf->type == V4L2_BUF_TYPE_VIDEO_CAPTURE_MPLANE) {
1118		ret = vb2_dqbuf(&ctx->vq_dst, buf, file->f_flags & O_NONBLOCK);
1119		if (ret == 0 && ctx->state == MFCINST_FINISHED
1120					&& list_empty(&ctx->vq_dst.done_list))
1121			v4l2_event_queue_fh(&ctx->fh, &ev);
1122	} else {
1123		ret = -EINVAL;
1124	}
1125
1126	return ret;
1127}
1128
1129/* Stream on */
1130static int vidioc_streamon(struct file *file, void *priv,
1131			   enum v4l2_buf_type type)
1132{
1133	struct s5p_mfc_ctx *ctx = fh_to_ctx(priv);
1134
1135	if (type == V4L2_BUF_TYPE_VIDEO_OUTPUT_MPLANE)
1136		return vb2_streamon(&ctx->vq_src, type);
1137	else if (type == V4L2_BUF_TYPE_VIDEO_CAPTURE_MPLANE)
1138		return vb2_streamon(&ctx->vq_dst, type);
1139	return -EINVAL;
1140}
1141
1142/* Stream off, which equals to a pause */
1143static int vidioc_streamoff(struct file *file, void *priv,
1144			    enum v4l2_buf_type type)
1145{
1146	struct s5p_mfc_ctx *ctx = fh_to_ctx(priv);
1147
1148	if (type == V4L2_BUF_TYPE_VIDEO_OUTPUT_MPLANE)
1149		return vb2_streamoff(&ctx->vq_src, type);
1150	else if (type == V4L2_BUF_TYPE_VIDEO_CAPTURE_MPLANE)
1151		return vb2_streamoff(&ctx->vq_dst, type);
1152	return -EINVAL;
1153}
1154
1155static inline int h264_level(enum v4l2_mpeg_video_h264_level lvl)
1156{
1157	static unsigned int t[V4L2_MPEG_VIDEO_H264_LEVEL_4_0 + 1] = {
1158		/* V4L2_MPEG_VIDEO_H264_LEVEL_1_0   */ 10,
1159		/* V4L2_MPEG_VIDEO_H264_LEVEL_1B    */ 9,
1160		/* V4L2_MPEG_VIDEO_H264_LEVEL_1_1   */ 11,
1161		/* V4L2_MPEG_VIDEO_H264_LEVEL_1_2   */ 12,
1162		/* V4L2_MPEG_VIDEO_H264_LEVEL_1_3   */ 13,
1163		/* V4L2_MPEG_VIDEO_H264_LEVEL_2_0   */ 20,
1164		/* V4L2_MPEG_VIDEO_H264_LEVEL_2_1   */ 21,
1165		/* V4L2_MPEG_VIDEO_H264_LEVEL_2_2   */ 22,
1166		/* V4L2_MPEG_VIDEO_H264_LEVEL_3_0   */ 30,
1167		/* V4L2_MPEG_VIDEO_H264_LEVEL_3_1   */ 31,
1168		/* V4L2_MPEG_VIDEO_H264_LEVEL_3_2   */ 32,
1169		/* V4L2_MPEG_VIDEO_H264_LEVEL_4_0   */ 40,
1170	};
1171	return t[lvl];
1172}
1173
1174static inline int mpeg4_level(enum v4l2_mpeg_video_mpeg4_level lvl)
1175{
1176	static unsigned int t[V4L2_MPEG_VIDEO_MPEG4_LEVEL_5 + 1] = {
1177		/* V4L2_MPEG_VIDEO_MPEG4_LEVEL_0    */ 0,
1178		/* V4L2_MPEG_VIDEO_MPEG4_LEVEL_0B   */ 9,
1179		/* V4L2_MPEG_VIDEO_MPEG4_LEVEL_1    */ 1,
1180		/* V4L2_MPEG_VIDEO_MPEG4_LEVEL_2    */ 2,
1181		/* V4L2_MPEG_VIDEO_MPEG4_LEVEL_3    */ 3,
1182		/* V4L2_MPEG_VIDEO_MPEG4_LEVEL_3B   */ 7,
1183		/* V4L2_MPEG_VIDEO_MPEG4_LEVEL_4    */ 4,
1184		/* V4L2_MPEG_VIDEO_MPEG4_LEVEL_5    */ 5,
1185	};
1186	return t[lvl];
1187}
1188
1189static inline int vui_sar_idc(enum v4l2_mpeg_video_h264_vui_sar_idc sar)
1190{
1191	static unsigned int t[V4L2_MPEG_VIDEO_H264_VUI_SAR_IDC_EXTENDED + 1] = {
1192		/* V4L2_MPEG_VIDEO_H264_VUI_SAR_IDC_UNSPECIFIED     */ 0,
1193		/* V4L2_MPEG_VIDEO_H264_VUI_SAR_IDC_1x1             */ 1,
1194		/* V4L2_MPEG_VIDEO_H264_VUI_SAR_IDC_12x11           */ 2,
1195		/* V4L2_MPEG_VIDEO_H264_VUI_SAR_IDC_10x11           */ 3,
1196		/* V4L2_MPEG_VIDEO_H264_VUI_SAR_IDC_16x11           */ 4,
1197		/* V4L2_MPEG_VIDEO_H264_VUI_SAR_IDC_40x33           */ 5,
1198		/* V4L2_MPEG_VIDEO_H264_VUI_SAR_IDC_24x11           */ 6,
1199		/* V4L2_MPEG_VIDEO_H264_VUI_SAR_IDC_20x11           */ 7,
1200		/* V4L2_MPEG_VIDEO_H264_VUI_SAR_IDC_32x11           */ 8,
1201		/* V4L2_MPEG_VIDEO_H264_VUI_SAR_IDC_80x33           */ 9,
1202		/* V4L2_MPEG_VIDEO_H264_VUI_SAR_IDC_18x11           */ 10,
1203		/* V4L2_MPEG_VIDEO_H264_VUI_SAR_IDC_15x11           */ 11,
1204		/* V4L2_MPEG_VIDEO_H264_VUI_SAR_IDC_64x33           */ 12,
1205		/* V4L2_MPEG_VIDEO_H264_VUI_SAR_IDC_160x99          */ 13,
1206		/* V4L2_MPEG_VIDEO_H264_VUI_SAR_IDC_4x3             */ 14,
1207		/* V4L2_MPEG_VIDEO_H264_VUI_SAR_IDC_3x2             */ 15,
1208		/* V4L2_MPEG_VIDEO_H264_VUI_SAR_IDC_2x1             */ 16,
1209		/* V4L2_MPEG_VIDEO_H264_VUI_SAR_IDC_EXTENDED        */ 255,
1210	};
1211	return t[sar];
1212}
1213
1214static int s5p_mfc_enc_s_ctrl(struct v4l2_ctrl *ctrl)
1215{
1216	struct s5p_mfc_ctx *ctx = ctrl_to_ctx(ctrl);
1217	struct s5p_mfc_dev *dev = ctx->dev;
1218	struct s5p_mfc_enc_params *p = &ctx->enc_params;
1219	int ret = 0;
1220
1221	switch (ctrl->id) {
1222	case V4L2_CID_MPEG_VIDEO_GOP_SIZE:
1223		p->gop_size = ctrl->val;
1224		break;
1225	case V4L2_CID_MPEG_VIDEO_MULTI_SLICE_MODE:
1226		p->slice_mode = ctrl->val;
1227		break;
1228	case V4L2_CID_MPEG_VIDEO_MULTI_SLICE_MAX_MB:
1229		p->slice_mb = ctrl->val;
1230		break;
1231	case V4L2_CID_MPEG_VIDEO_MULTI_SLICE_MAX_BYTES:
1232		p->slice_bit = ctrl->val * 8;
1233		break;
1234	case V4L2_CID_MPEG_VIDEO_CYCLIC_INTRA_REFRESH_MB:
1235		p->intra_refresh_mb = ctrl->val;
1236		break;
1237	case V4L2_CID_MPEG_MFC51_VIDEO_PADDING:
1238		p->pad = ctrl->val;
1239		break;
1240	case V4L2_CID_MPEG_MFC51_VIDEO_PADDING_YUV:
1241		p->pad_luma = (ctrl->val >> 16) & 0xff;
1242		p->pad_cb = (ctrl->val >> 8) & 0xff;
1243		p->pad_cr = (ctrl->val >> 0) & 0xff;
1244		break;
1245	case V4L2_CID_MPEG_VIDEO_FRAME_RC_ENABLE:
1246		p->rc_frame = ctrl->val;
1247		break;
1248	case V4L2_CID_MPEG_VIDEO_BITRATE:
1249		p->rc_bitrate = ctrl->val;
1250		break;
1251	case V4L2_CID_MPEG_MFC51_VIDEO_RC_REACTION_COEFF:
1252		p->rc_reaction_coeff = ctrl->val;
1253		break;
1254	case V4L2_CID_MPEG_MFC51_VIDEO_FORCE_FRAME_TYPE:
1255		ctx->force_frame_type = ctrl->val;
1256		break;
1257	case V4L2_CID_MPEG_VIDEO_VBV_SIZE:
1258		p->vbv_size = ctrl->val;
1259		break;
1260	case V4L2_CID_MPEG_VIDEO_H264_CPB_SIZE:
1261		p->codec.h264.cpb_size = ctrl->val;
1262		break;
1263	case V4L2_CID_MPEG_VIDEO_HEADER_MODE:
1264		p->seq_hdr_mode = ctrl->val;
1265		break;
1266	case V4L2_CID_MPEG_MFC51_VIDEO_FRAME_SKIP_MODE:
1267		p->frame_skip_mode = ctrl->val;
1268		break;
1269	case V4L2_CID_MPEG_MFC51_VIDEO_RC_FIXED_TARGET_BIT:
1270		p->fixed_target_bit = ctrl->val;
1271		break;
1272	case V4L2_CID_MPEG_VIDEO_B_FRAMES:
1273		p->num_b_frame = ctrl->val;
1274		break;
1275	case V4L2_CID_MPEG_VIDEO_H264_PROFILE:
1276		switch (ctrl->val) {
1277		case V4L2_MPEG_VIDEO_H264_PROFILE_MAIN:
1278			p->codec.h264.profile =
1279					S5P_FIMV_ENC_PROFILE_H264_MAIN;
1280			break;
1281		case V4L2_MPEG_VIDEO_H264_PROFILE_HIGH:
1282			p->codec.h264.profile =
1283					S5P_FIMV_ENC_PROFILE_H264_HIGH;
1284			break;
1285		case V4L2_MPEG_VIDEO_H264_PROFILE_BASELINE:
1286			p->codec.h264.profile =
1287				S5P_FIMV_ENC_PROFILE_H264_BASELINE;
1288			break;
1289		default:
1290			ret = -EINVAL;
1291		}
1292		break;
1293	case V4L2_CID_MPEG_VIDEO_H264_LEVEL:
1294		p->codec.h264.level_v4l2 = ctrl->val;
1295		p->codec.h264.level = h264_level(ctrl->val);
1296		if (p->codec.h264.level < 0) {
1297			mfc_err("Level number is wrong\n");
1298			ret = p->codec.h264.level;
1299		}
1300		break;
1301	case V4L2_CID_MPEG_VIDEO_MPEG4_LEVEL:
1302		p->codec.mpeg4.level_v4l2 = ctrl->val;
1303		p->codec.mpeg4.level = mpeg4_level(ctrl->val);
1304		if (p->codec.mpeg4.level < 0) {
1305			mfc_err("Level number is wrong\n");
1306			ret = p->codec.mpeg4.level;
1307		}
1308		break;
1309	case V4L2_CID_MPEG_VIDEO_H264_LOOP_FILTER_MODE:
1310		p->codec.h264.loop_filter_mode = ctrl->val;
1311		break;
1312	case V4L2_CID_MPEG_VIDEO_H264_LOOP_FILTER_ALPHA:
1313		p->codec.h264.loop_filter_alpha = ctrl->val;
1314		break;
1315	case V4L2_CID_MPEG_VIDEO_H264_LOOP_FILTER_BETA:
1316		p->codec.h264.loop_filter_beta = ctrl->val;
1317		break;
1318	case V4L2_CID_MPEG_VIDEO_H264_ENTROPY_MODE:
1319		p->codec.h264.entropy_mode = ctrl->val;
1320		break;
1321	case V4L2_CID_MPEG_MFC51_VIDEO_H264_NUM_REF_PIC_FOR_P:
1322		p->codec.h264.num_ref_pic_4p = ctrl->val;
1323		break;
1324	case V4L2_CID_MPEG_VIDEO_H264_8X8_TRANSFORM:
1325		p->codec.h264._8x8_transform = ctrl->val;
1326		break;
1327	case V4L2_CID_MPEG_VIDEO_MB_RC_ENABLE:
1328		p->rc_mb = ctrl->val;
1329		break;
1330	case V4L2_CID_MPEG_VIDEO_H264_I_FRAME_QP:
1331		p->codec.h264.rc_frame_qp = ctrl->val;
1332		break;
1333	case V4L2_CID_MPEG_VIDEO_H264_MIN_QP:
1334		p->codec.h264.rc_min_qp = ctrl->val;
1335		break;
1336	case V4L2_CID_MPEG_VIDEO_H264_MAX_QP:
1337		p->codec.h264.rc_max_qp = ctrl->val;
1338		break;
1339	case V4L2_CID_MPEG_VIDEO_H264_P_FRAME_QP:
1340		p->codec.h264.rc_p_frame_qp = ctrl->val;
1341		break;
1342	case V4L2_CID_MPEG_VIDEO_H264_B_FRAME_QP:
1343		p->codec.h264.rc_b_frame_qp = ctrl->val;
1344		break;
1345	case V4L2_CID_MPEG_VIDEO_MPEG4_I_FRAME_QP:
1346	case V4L2_CID_MPEG_VIDEO_H263_I_FRAME_QP:
1347		p->codec.mpeg4.rc_frame_qp = ctrl->val;
1348		break;
1349	case V4L2_CID_MPEG_VIDEO_MPEG4_MIN_QP:
1350	case V4L2_CID_MPEG_VIDEO_H263_MIN_QP:
1351		p->codec.mpeg4.rc_min_qp = ctrl->val;
1352		break;
1353	case V4L2_CID_MPEG_VIDEO_MPEG4_MAX_QP:
1354	case V4L2_CID_MPEG_VIDEO_H263_MAX_QP:
1355		p->codec.mpeg4.rc_max_qp = ctrl->val;
1356		break;
1357	case V4L2_CID_MPEG_VIDEO_MPEG4_P_FRAME_QP:
1358	case V4L2_CID_MPEG_VIDEO_H263_P_FRAME_QP:
1359		p->codec.mpeg4.rc_p_frame_qp = ctrl->val;
1360		break;
1361	case V4L2_CID_MPEG_VIDEO_MPEG4_B_FRAME_QP:
1362	case V4L2_CID_MPEG_VIDEO_H263_B_FRAME_QP:
1363		p->codec.mpeg4.rc_b_frame_qp = ctrl->val;
1364		break;
1365	case V4L2_CID_MPEG_MFC51_VIDEO_H264_ADAPTIVE_RC_DARK:
1366		p->codec.h264.rc_mb_dark = ctrl->val;
1367		break;
1368	case V4L2_CID_MPEG_MFC51_VIDEO_H264_ADAPTIVE_RC_SMOOTH:
1369		p->codec.h264.rc_mb_smooth = ctrl->val;
1370		break;
1371	case V4L2_CID_MPEG_MFC51_VIDEO_H264_ADAPTIVE_RC_STATIC:
1372		p->codec.h264.rc_mb_static = ctrl->val;
1373		break;
1374	case V4L2_CID_MPEG_MFC51_VIDEO_H264_ADAPTIVE_RC_ACTIVITY:
1375		p->codec.h264.rc_mb_activity = ctrl->val;
1376		break;
1377	case V4L2_CID_MPEG_VIDEO_H264_VUI_SAR_ENABLE:
1378		p->codec.h264.vui_sar = ctrl->val;
1379		break;
1380	case V4L2_CID_MPEG_VIDEO_H264_VUI_SAR_IDC:
1381		p->codec.h264.vui_sar_idc = vui_sar_idc(ctrl->val);
1382		break;
1383	case V4L2_CID_MPEG_VIDEO_H264_VUI_EXT_SAR_WIDTH:
1384		p->codec.h264.vui_ext_sar_width = ctrl->val;
1385		break;
1386	case V4L2_CID_MPEG_VIDEO_H264_VUI_EXT_SAR_HEIGHT:
1387		p->codec.h264.vui_ext_sar_height = ctrl->val;
1388		break;
1389	case V4L2_CID_MPEG_VIDEO_GOP_CLOSURE:
1390		p->codec.h264.open_gop = !ctrl->val;
1391		break;
1392	case V4L2_CID_MPEG_VIDEO_H264_I_PERIOD:
1393		p->codec.h264.open_gop_size = ctrl->val;
1394		break;
1395	case V4L2_CID_MPEG_VIDEO_MPEG4_PROFILE:
1396		switch (ctrl->val) {
1397		case V4L2_MPEG_VIDEO_MPEG4_PROFILE_SIMPLE:
1398			p->codec.mpeg4.profile =
1399				S5P_FIMV_ENC_PROFILE_MPEG4_SIMPLE;
1400			break;
1401		case V4L2_MPEG_VIDEO_MPEG4_PROFILE_ADVANCED_SIMPLE:
1402			p->codec.mpeg4.profile =
1403			S5P_FIMV_ENC_PROFILE_MPEG4_ADVANCED_SIMPLE;
1404			break;
1405		default:
1406			ret = -EINVAL;
1407		}
1408		break;
1409	case V4L2_CID_MPEG_VIDEO_MPEG4_QPEL:
1410		p->codec.mpeg4.quarter_pixel = ctrl->val;
1411		break;
1412	default:
1413		v4l2_err(&dev->v4l2_dev, "Invalid control, id=%d, val=%d\n",
1414							ctrl->id, ctrl->val);
1415		ret = -EINVAL;
1416	}
1417	return ret;
1418}
1419
1420static const struct v4l2_ctrl_ops s5p_mfc_enc_ctrl_ops = {
1421	.s_ctrl = s5p_mfc_enc_s_ctrl,
1422};
1423
1424static int vidioc_s_parm(struct file *file, void *priv,
1425			 struct v4l2_streamparm *a)
1426{
1427	struct s5p_mfc_ctx *ctx = fh_to_ctx(priv);
1428
1429	if (a->type == V4L2_BUF_TYPE_VIDEO_OUTPUT_MPLANE) {
1430		ctx->enc_params.rc_framerate_num =
1431					a->parm.output.timeperframe.denominator;
1432		ctx->enc_params.rc_framerate_denom =
1433					a->parm.output.timeperframe.numerator;
1434	} else {
1435		mfc_err("Setting FPS is only possible for the output queue\n");
1436		return -EINVAL;
1437	}
1438	return 0;
1439}
1440
1441static int vidioc_g_parm(struct file *file, void *priv,
1442			 struct v4l2_streamparm *a)
1443{
1444	struct s5p_mfc_ctx *ctx = fh_to_ctx(priv);
1445
1446	if (a->type == V4L2_BUF_TYPE_VIDEO_OUTPUT) {
1447		a->parm.output.timeperframe.denominator =
1448					ctx->enc_params.rc_framerate_num;
1449		a->parm.output.timeperframe.numerator =
1450					ctx->enc_params.rc_framerate_denom;
1451	} else {
1452		mfc_err("Setting FPS is only possible for the output queue\n");
1453		return -EINVAL;
1454	}
1455	return 0;
1456}
1457
1458int vidioc_encoder_cmd(struct file *file, void *priv,
1459						struct v4l2_encoder_cmd *cmd)
1460{
1461	struct s5p_mfc_ctx *ctx = fh_to_ctx(priv);
1462	struct s5p_mfc_dev *dev = ctx->dev;
1463	struct s5p_mfc_buf *buf;
1464	unsigned long flags;
1465
1466	switch (cmd->cmd) {
1467	case V4L2_ENC_CMD_STOP:
1468		if (cmd->flags != 0)
1469			return -EINVAL;
1470
1471		if (!ctx->vq_src.streaming)
1472			return -EINVAL;
1473
1474		spin_lock_irqsave(&dev->irqlock, flags);
1475		if (list_empty(&ctx->src_queue)) {
1476			mfc_debug(2, "EOS: empty src queue, entering finishing state");
1477			ctx->state = MFCINST_FINISHING;
1478			spin_unlock_irqrestore(&dev->irqlock, flags);
1479			s5p_mfc_hw_call(dev->mfc_ops, try_run, dev);
1480		} else {
1481			mfc_debug(2, "EOS: marking last buffer of stream");
1482			buf = list_entry(ctx->src_queue.prev,
1483						struct s5p_mfc_buf, list);
1484			if (buf->flags & MFC_BUF_FLAG_USED)
1485				ctx->state = MFCINST_FINISHING;
1486			else
1487				buf->flags |= MFC_BUF_FLAG_EOS;
1488			spin_unlock_irqrestore(&dev->irqlock, flags);
1489		}
1490		break;
1491	default:
1492		return -EINVAL;
1493
1494	}
1495	return 0;
1496}
1497
1498static int vidioc_subscribe_event(struct v4l2_fh *fh,
1499					struct v4l2_event_subscription *sub)
1500{
1501	switch (sub->type) {
1502	case V4L2_EVENT_EOS:
1503		return v4l2_event_subscribe(fh, sub, 2, NULL);
1504	default:
1505		return -EINVAL;
1506	}
1507}
1508
1509static const struct v4l2_ioctl_ops s5p_mfc_enc_ioctl_ops = {
1510	.vidioc_querycap = vidioc_querycap,
1511	.vidioc_enum_fmt_vid_cap = vidioc_enum_fmt_vid_cap,
1512	.vidioc_enum_fmt_vid_cap_mplane = vidioc_enum_fmt_vid_cap_mplane,
1513	.vidioc_enum_fmt_vid_out = vidioc_enum_fmt_vid_out,
1514	.vidioc_enum_fmt_vid_out_mplane = vidioc_enum_fmt_vid_out_mplane,
1515	.vidioc_g_fmt_vid_cap_mplane = vidioc_g_fmt,
1516	.vidioc_g_fmt_vid_out_mplane = vidioc_g_fmt,
1517	.vidioc_try_fmt_vid_cap_mplane = vidioc_try_fmt,
1518	.vidioc_try_fmt_vid_out_mplane = vidioc_try_fmt,
1519	.vidioc_s_fmt_vid_cap_mplane = vidioc_s_fmt,
1520	.vidioc_s_fmt_vid_out_mplane = vidioc_s_fmt,
1521	.vidioc_reqbufs = vidioc_reqbufs,
1522	.vidioc_querybuf = vidioc_querybuf,
1523	.vidioc_qbuf = vidioc_qbuf,
1524	.vidioc_dqbuf = vidioc_dqbuf,
1525	.vidioc_streamon = vidioc_streamon,
1526	.vidioc_streamoff = vidioc_streamoff,
1527	.vidioc_s_parm = vidioc_s_parm,
1528	.vidioc_g_parm = vidioc_g_parm,
1529	.vidioc_encoder_cmd = vidioc_encoder_cmd,
1530	.vidioc_subscribe_event = vidioc_subscribe_event,
1531	.vidioc_unsubscribe_event = v4l2_event_unsubscribe,
1532};
1533
1534static int check_vb_with_fmt(struct s5p_mfc_fmt *fmt, struct vb2_buffer *vb)
1535{
1536	int i;
1537
1538	if (!fmt)
1539		return -EINVAL;
1540	if (fmt->num_planes != vb->num_planes) {
1541		mfc_err("invalid plane number for the format\n");
1542		return -EINVAL;
1543	}
1544	for (i = 0; i < fmt->num_planes; i++) {
1545		if (!vb2_dma_contig_plane_dma_addr(vb, i)) {
1546			mfc_err("failed to get plane cookie\n");
1547			return -EINVAL;
1548		}
1549		mfc_debug(2, "index: %d, plane[%d] cookie: 0x%08zx",
1550				vb->v4l2_buf.index, i,
1551				vb2_dma_contig_plane_dma_addr(vb, i));
1552	}
1553	return 0;
1554}
1555
1556static int s5p_mfc_queue_setup(struct vb2_queue *vq,
1557			const struct v4l2_format *fmt,
1558			unsigned int *buf_count, unsigned int *plane_count,
1559			unsigned int psize[], void *allocators[])
1560{
1561	struct s5p_mfc_ctx *ctx = fh_to_ctx(vq->drv_priv);
1562
1563	if (ctx->state != MFCINST_GOT_INST) {
1564		mfc_err("inavlid state: %d\n", ctx->state);
1565		return -EINVAL;
1566	}
1567	if (vq->type == V4L2_BUF_TYPE_VIDEO_CAPTURE_MPLANE) {
1568		if (ctx->dst_fmt)
1569			*plane_count = ctx->dst_fmt->num_planes;
1570		else
1571			*plane_count = MFC_ENC_CAP_PLANE_COUNT;
1572		if (*buf_count < 1)
1573			*buf_count = 1;
1574		if (*buf_count > MFC_MAX_BUFFERS)
1575			*buf_count = MFC_MAX_BUFFERS;
1576		psize[0] = ctx->enc_dst_buf_size;
1577		allocators[0] = ctx->dev->alloc_ctx[MFC_BANK1_ALLOC_CTX];
1578	} else if (vq->type == V4L2_BUF_TYPE_VIDEO_OUTPUT_MPLANE) {
1579		if (ctx->src_fmt)
1580			*plane_count = ctx->src_fmt->num_planes;
1581		else
1582			*plane_count = MFC_ENC_OUT_PLANE_COUNT;
1583
1584		if (*buf_count < 1)
1585			*buf_count = 1;
1586		if (*buf_count > MFC_MAX_BUFFERS)
1587			*buf_count = MFC_MAX_BUFFERS;
1588		psize[0] = ctx->luma_size;
1589		psize[1] = ctx->chroma_size;
1590		allocators[0] = ctx->dev->alloc_ctx[MFC_BANK2_ALLOC_CTX];
1591		allocators[1] = ctx->dev->alloc_ctx[MFC_BANK2_ALLOC_CTX];
1592	} else {
1593		mfc_err("inavlid queue type: %d\n", vq->type);
1594		return -EINVAL;
1595	}
1596	return 0;
1597}
1598
1599static void s5p_mfc_unlock(struct vb2_queue *q)
1600{
1601	struct s5p_mfc_ctx *ctx = fh_to_ctx(q->drv_priv);
1602	struct s5p_mfc_dev *dev = ctx->dev;
1603
1604	mutex_unlock(&dev->mfc_mutex);
1605}
1606
1607static void s5p_mfc_lock(struct vb2_queue *q)
1608{
1609	struct s5p_mfc_ctx *ctx = fh_to_ctx(q->drv_priv);
1610	struct s5p_mfc_dev *dev = ctx->dev;
1611
1612	mutex_lock(&dev->mfc_mutex);
1613}
1614
1615static int s5p_mfc_buf_init(struct vb2_buffer *vb)
1616{
1617	struct vb2_queue *vq = vb->vb2_queue;
1618	struct s5p_mfc_ctx *ctx = fh_to_ctx(vq->drv_priv);
1619	unsigned int i;
1620	int ret;
1621
1622	if (vq->type == V4L2_BUF_TYPE_VIDEO_CAPTURE_MPLANE) {
1623		ret = check_vb_with_fmt(ctx->dst_fmt, vb);
1624		if (ret < 0)
1625			return ret;
1626		i = vb->v4l2_buf.index;
1627		ctx->dst_bufs[i].b = vb;
1628		ctx->dst_bufs[i].cookie.stream =
1629					vb2_dma_contig_plane_dma_addr(vb, 0);
1630		ctx->dst_bufs_cnt++;
1631	} else if (vq->type == V4L2_BUF_TYPE_VIDEO_OUTPUT_MPLANE) {
1632		ret = check_vb_with_fmt(ctx->src_fmt, vb);
1633		if (ret < 0)
1634			return ret;
1635		i = vb->v4l2_buf.index;
1636		ctx->src_bufs[i].b = vb;
1637		ctx->src_bufs[i].cookie.raw.luma =
1638					vb2_dma_contig_plane_dma_addr(vb, 0);
1639		ctx->src_bufs[i].cookie.raw.chroma =
1640					vb2_dma_contig_plane_dma_addr(vb, 1);
1641		ctx->src_bufs_cnt++;
1642	} else {
1643		mfc_err("inavlid queue type: %d\n", vq->type);
1644		return -EINVAL;
1645	}
1646	return 0;
1647}
1648
1649static int s5p_mfc_buf_prepare(struct vb2_buffer *vb)
1650{
1651	struct vb2_queue *vq = vb->vb2_queue;
1652	struct s5p_mfc_ctx *ctx = fh_to_ctx(vq->drv_priv);
1653	int ret;
1654
1655	if (vq->type == V4L2_BUF_TYPE_VIDEO_CAPTURE_MPLANE) {
1656		ret = check_vb_with_fmt(ctx->dst_fmt, vb);
1657		if (ret < 0)
1658			return ret;
1659		mfc_debug(2, "plane size: %ld, dst size: %d\n",
1660			vb2_plane_size(vb, 0), ctx->enc_dst_buf_size);
1661		if (vb2_plane_size(vb, 0) < ctx->enc_dst_buf_size) {
1662			mfc_err("plane size is too small for capture\n");
1663			return -EINVAL;
1664		}
1665	} else if (vq->type == V4L2_BUF_TYPE_VIDEO_OUTPUT_MPLANE) {
1666		ret = check_vb_with_fmt(ctx->src_fmt, vb);
1667		if (ret < 0)
1668			return ret;
1669		mfc_debug(2, "plane size: %ld, luma size: %d\n",
1670			vb2_plane_size(vb, 0), ctx->luma_size);
1671		mfc_debug(2, "plane size: %ld, chroma size: %d\n",
1672			vb2_plane_size(vb, 1), ctx->chroma_size);
1673		if (vb2_plane_size(vb, 0) < ctx->luma_size ||
1674		    vb2_plane_size(vb, 1) < ctx->chroma_size) {
1675			mfc_err("plane size is too small for output\n");
1676			return -EINVAL;
1677		}
1678	} else {
1679		mfc_err("inavlid queue type: %d\n", vq->type);
1680		return -EINVAL;
1681	}
1682	return 0;
1683}
1684
1685static int s5p_mfc_start_streaming(struct vb2_queue *q, unsigned int count)
1686{
1687	struct s5p_mfc_ctx *ctx = fh_to_ctx(q->drv_priv);
1688	struct s5p_mfc_dev *dev = ctx->dev;
1689
1690	v4l2_ctrl_handler_setup(&ctx->ctrl_handler);
1691	/* If context is ready then dev = work->data;schedule it to run */
1692	if (s5p_mfc_ctx_ready(ctx))
1693		set_work_bit_irqsave(ctx);
1694	s5p_mfc_hw_call(dev->mfc_ops, try_run, dev);
1695	return 0;
1696}
1697
1698static int s5p_mfc_stop_streaming(struct vb2_queue *q)
1699{
1700	unsigned long flags;
1701	struct s5p_mfc_ctx *ctx = fh_to_ctx(q->drv_priv);
1702	struct s5p_mfc_dev *dev = ctx->dev;
1703
1704	if ((ctx->state == MFCINST_FINISHING ||
1705		ctx->state == MFCINST_RUNNING) &&
1706		dev->curr_ctx == ctx->num && dev->hw_lock) {
1707		ctx->state = MFCINST_ABORT;
1708		s5p_mfc_wait_for_done_ctx(ctx, S5P_MFC_R2H_CMD_FRAME_DONE_RET,
1709					  0);
1710	}
1711	ctx->state = MFCINST_FINISHED;
1712	spin_lock_irqsave(&dev->irqlock, flags);
1713	if (q->type == V4L2_BUF_TYPE_VIDEO_CAPTURE_MPLANE) {
1714		s5p_mfc_hw_call(dev->mfc_ops, cleanup_queue, &ctx->dst_queue,
1715				&ctx->vq_dst);
1716		INIT_LIST_HEAD(&ctx->dst_queue);
1717		ctx->dst_queue_cnt = 0;
1718	}
1719	if (q->type == V4L2_BUF_TYPE_VIDEO_OUTPUT_MPLANE) {
1720		cleanup_ref_queue(ctx);
1721		s5p_mfc_hw_call(dev->mfc_ops, cleanup_queue, &ctx->src_queue,
1722				&ctx->vq_src);
1723		INIT_LIST_HEAD(&ctx->src_queue);
1724		ctx->src_queue_cnt = 0;
1725	}
1726	spin_unlock_irqrestore(&dev->irqlock, flags);
1727	return 0;
1728}
1729
1730static void s5p_mfc_buf_queue(struct vb2_buffer *vb)
1731{
1732	struct vb2_queue *vq = vb->vb2_queue;
1733	struct s5p_mfc_ctx *ctx = fh_to_ctx(vq->drv_priv);
1734	struct s5p_mfc_dev *dev = ctx->dev;
1735	unsigned long flags;
1736	struct s5p_mfc_buf *mfc_buf;
1737
1738	if (ctx->state == MFCINST_ERROR) {
1739		vb2_buffer_done(vb, VB2_BUF_STATE_ERROR);
1740		cleanup_ref_queue(ctx);
1741		return;
1742	}
1743	if (vq->type == V4L2_BUF_TYPE_VIDEO_CAPTURE_MPLANE) {
1744		mfc_buf = &ctx->dst_bufs[vb->v4l2_buf.index];
1745		mfc_buf->flags &= ~MFC_BUF_FLAG_USED;
1746		/* Mark destination as available for use by MFC */
1747		spin_lock_irqsave(&dev->irqlock, flags);
1748		list_add_tail(&mfc_buf->list, &ctx->dst_queue);
1749		ctx->dst_queue_cnt++;
1750		spin_unlock_irqrestore(&dev->irqlock, flags);
1751	} else if (vq->type == V4L2_BUF_TYPE_VIDEO_OUTPUT_MPLANE) {
1752		mfc_buf = &ctx->src_bufs[vb->v4l2_buf.index];
1753		mfc_buf->flags &= ~MFC_BUF_FLAG_USED;
1754		spin_lock_irqsave(&dev->irqlock, flags);
1755		list_add_tail(&mfc_buf->list, &ctx->src_queue);
1756		ctx->src_queue_cnt++;
1757		spin_unlock_irqrestore(&dev->irqlock, flags);
1758	} else {
1759		mfc_err("unsupported buffer type (%d)\n", vq->type);
1760	}
1761	if (s5p_mfc_ctx_ready(ctx))
1762		set_work_bit_irqsave(ctx);
1763	s5p_mfc_hw_call(dev->mfc_ops, try_run, dev);
1764}
1765
1766static struct vb2_ops s5p_mfc_enc_qops = {
1767	.queue_setup		= s5p_mfc_queue_setup,
1768	.wait_prepare		= s5p_mfc_unlock,
1769	.wait_finish		= s5p_mfc_lock,
1770	.buf_init		= s5p_mfc_buf_init,
1771	.buf_prepare		= s5p_mfc_buf_prepare,
1772	.start_streaming	= s5p_mfc_start_streaming,
1773	.stop_streaming		= s5p_mfc_stop_streaming,
1774	.buf_queue		= s5p_mfc_buf_queue,
1775};
1776
1777struct s5p_mfc_codec_ops *get_enc_codec_ops(void)
1778{
1779	return &encoder_codec_ops;
1780}
1781
1782struct vb2_ops *get_enc_queue_ops(void)
1783{
1784	return &s5p_mfc_enc_qops;
1785}
1786
1787const struct v4l2_ioctl_ops *get_enc_v4l2_ioctl_ops(void)
1788{
1789	return &s5p_mfc_enc_ioctl_ops;
1790}
1791
1792#define IS_MFC51_PRIV(x) ((V4L2_CTRL_ID2CLASS(x) == V4L2_CTRL_CLASS_MPEG) \
1793						&& V4L2_CTRL_DRIVER_PRIV(x))
1794
1795int s5p_mfc_enc_ctrls_setup(struct s5p_mfc_ctx *ctx)
1796{
1797	struct v4l2_ctrl_config cfg;
1798	int i;
1799
1800	v4l2_ctrl_handler_init(&ctx->ctrl_handler, NUM_CTRLS);
1801	if (ctx->ctrl_handler.error) {
1802		mfc_err("v4l2_ctrl_handler_init failed\n");
1803		return ctx->ctrl_handler.error;
1804	}
1805	for (i = 0; i < NUM_CTRLS; i++) {
1806		if (IS_MFC51_PRIV(controls[i].id)) {
1807			memset(&cfg, 0, sizeof(struct v4l2_ctrl_config));
1808			cfg.ops = &s5p_mfc_enc_ctrl_ops;
1809			cfg.id = controls[i].id;
1810			cfg.min = controls[i].minimum;
1811			cfg.max = controls[i].maximum;
1812			cfg.def = controls[i].default_value;
1813			cfg.name = controls[i].name;
1814			cfg.type = controls[i].type;
1815			cfg.flags = 0;
1816
1817			if (cfg.type == V4L2_CTRL_TYPE_MENU) {
1818				cfg.step = 0;
1819				cfg.menu_skip_mask = cfg.menu_skip_mask;
1820				cfg.qmenu = mfc51_get_menu(cfg.id);
1821			} else {
1822				cfg.step = controls[i].step;
1823				cfg.menu_skip_mask = 0;
1824			}
1825			ctx->ctrls[i] = v4l2_ctrl_new_custom(&ctx->ctrl_handler,
1826					&cfg, NULL);
1827		} else {
1828			if (controls[i].type == V4L2_CTRL_TYPE_MENU) {
1829				ctx->ctrls[i] = v4l2_ctrl_new_std_menu(
1830					&ctx->ctrl_handler,
1831					&s5p_mfc_enc_ctrl_ops, controls[i].id,
1832					controls[i].maximum, 0,
1833					controls[i].default_value);
1834			} else {
1835				ctx->ctrls[i] = v4l2_ctrl_new_std(
1836					&ctx->ctrl_handler,
1837					&s5p_mfc_enc_ctrl_ops, controls[i].id,
1838					controls[i].minimum,
1839					controls[i].maximum, controls[i].step,
1840					controls[i].default_value);
1841			}
1842		}
1843		if (ctx->ctrl_handler.error) {
1844			mfc_err("Adding control (%d) failed\n", i);
1845			return ctx->ctrl_handler.error;
1846		}
1847		if (controls[i].is_volatile && ctx->ctrls[i])
1848			ctx->ctrls[i]->flags |= V4L2_CTRL_FLAG_VOLATILE;
1849	}
1850	return 0;
1851}
1852
1853void s5p_mfc_enc_ctrls_delete(struct s5p_mfc_ctx *ctx)
1854{
1855	int i;
1856
1857	v4l2_ctrl_handler_free(&ctx->ctrl_handler);
1858	for (i = 0; i < NUM_CTRLS; i++)
1859		ctx->ctrls[i] = NULL;
1860}
1861
1862void s5p_mfc_enc_init(struct s5p_mfc_ctx *ctx)
1863{
1864	struct v4l2_format f;
1865	f.fmt.pix_mp.pixelformat = DEF_SRC_FMT_ENC;
1866	ctx->src_fmt = find_format(&f, MFC_FMT_RAW);
1867	f.fmt.pix_mp.pixelformat = DEF_DST_FMT_ENC;
1868	ctx->dst_fmt = find_format(&f, MFC_FMT_ENC);
1869}
1870
1871