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