s5p_mfc_enc.c revision 8492ec69801ea039546fd27e84fa84f7a0612474
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(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(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(dev->mfc_ops, set_enc_frame_buffer, ctx, src_y_addr,
829			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(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(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 mplane, 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 (mplane && formats[i].num_planes == 1)
962			continue;
963		else if (!mplane && formats[i].num_planes > 1)
964			continue;
965		if (out && formats[i].type != MFC_FMT_RAW)
966			continue;
967		else if (!out && formats[i].type != MFC_FMT_ENC)
968			continue;
969		else if ((dev->variant->version_bit & formats[i].versions) == 0)
970			continue;
971
972		if (j == f->index) {
973			fmt = &formats[i];
974			strlcpy(f->description, fmt->name,
975				sizeof(f->description));
976			f->pixelformat = fmt->fourcc;
977			return 0;
978		}
979		++j;
980	}
981	return -EINVAL;
982}
983
984static int vidioc_enum_fmt_vid_cap(struct file *file, void *pirv,
985				   struct v4l2_fmtdesc *f)
986{
987	return vidioc_enum_fmt(file, f, false, false);
988}
989
990static int vidioc_enum_fmt_vid_cap_mplane(struct file *file, void *pirv,
991					  struct v4l2_fmtdesc *f)
992{
993	return vidioc_enum_fmt(file, f, true, false);
994}
995
996static int vidioc_enum_fmt_vid_out(struct file *file, void *prov,
997				   struct v4l2_fmtdesc *f)
998{
999	return vidioc_enum_fmt(file, f, false, true);
1000}
1001
1002static int vidioc_enum_fmt_vid_out_mplane(struct file *file, void *prov,
1003					  struct v4l2_fmtdesc *f)
1004{
1005	return vidioc_enum_fmt(file, f, true, true);
1006}
1007
1008static int vidioc_g_fmt(struct file *file, void *priv, struct v4l2_format *f)
1009{
1010	struct s5p_mfc_ctx *ctx = fh_to_ctx(priv);
1011	struct v4l2_pix_format_mplane *pix_fmt_mp = &f->fmt.pix_mp;
1012
1013	mfc_debug(2, "f->type = %d ctx->state = %d\n", f->type, ctx->state);
1014	if (f->type == V4L2_BUF_TYPE_VIDEO_CAPTURE_MPLANE) {
1015		/* This is run on output (encoder dest) */
1016		pix_fmt_mp->width = 0;
1017		pix_fmt_mp->height = 0;
1018		pix_fmt_mp->field = V4L2_FIELD_NONE;
1019		pix_fmt_mp->pixelformat = ctx->dst_fmt->fourcc;
1020		pix_fmt_mp->num_planes = ctx->dst_fmt->num_planes;
1021
1022		pix_fmt_mp->plane_fmt[0].bytesperline = ctx->enc_dst_buf_size;
1023		pix_fmt_mp->plane_fmt[0].sizeimage = ctx->enc_dst_buf_size;
1024	} else if (f->type == V4L2_BUF_TYPE_VIDEO_OUTPUT_MPLANE) {
1025		/* This is run on capture (encoder src) */
1026		pix_fmt_mp->width = ctx->img_width;
1027		pix_fmt_mp->height = ctx->img_height;
1028
1029		pix_fmt_mp->field = V4L2_FIELD_NONE;
1030		pix_fmt_mp->pixelformat = ctx->src_fmt->fourcc;
1031		pix_fmt_mp->num_planes = ctx->src_fmt->num_planes;
1032
1033		pix_fmt_mp->plane_fmt[0].bytesperline = ctx->buf_width;
1034		pix_fmt_mp->plane_fmt[0].sizeimage = ctx->luma_size;
1035		pix_fmt_mp->plane_fmt[1].bytesperline = ctx->buf_width;
1036		pix_fmt_mp->plane_fmt[1].sizeimage = ctx->chroma_size;
1037	} else {
1038		mfc_err("invalid buf type\n");
1039		return -EINVAL;
1040	}
1041	return 0;
1042}
1043
1044static int vidioc_try_fmt(struct file *file, void *priv, struct v4l2_format *f)
1045{
1046	struct s5p_mfc_dev *dev = video_drvdata(file);
1047	struct s5p_mfc_fmt *fmt;
1048	struct v4l2_pix_format_mplane *pix_fmt_mp = &f->fmt.pix_mp;
1049
1050	if (f->type == V4L2_BUF_TYPE_VIDEO_CAPTURE_MPLANE) {
1051		fmt = find_format(f, MFC_FMT_ENC);
1052		if (!fmt) {
1053			mfc_err("failed to try output format\n");
1054			return -EINVAL;
1055		}
1056		if (pix_fmt_mp->plane_fmt[0].sizeimage == 0) {
1057			mfc_err("must be set encoding output size\n");
1058			return -EINVAL;
1059		}
1060		if ((dev->variant->version_bit & fmt->versions) == 0) {
1061			mfc_err("Unsupported format by this MFC version.\n");
1062			return -EINVAL;
1063		}
1064
1065		pix_fmt_mp->plane_fmt[0].bytesperline =
1066			pix_fmt_mp->plane_fmt[0].sizeimage;
1067	} else if (f->type == V4L2_BUF_TYPE_VIDEO_OUTPUT_MPLANE) {
1068		fmt = find_format(f, MFC_FMT_RAW);
1069		if (!fmt) {
1070			mfc_err("failed to try output format\n");
1071			return -EINVAL;
1072		}
1073
1074		if (fmt->num_planes != pix_fmt_mp->num_planes) {
1075			mfc_err("failed to try output format\n");
1076			return -EINVAL;
1077		}
1078		if ((dev->variant->version_bit & fmt->versions) == 0) {
1079			mfc_err("Unsupported format by this MFC version.\n");
1080			return -EINVAL;
1081		}
1082
1083		v4l_bound_align_image(&pix_fmt_mp->width, 8, 1920, 1,
1084			&pix_fmt_mp->height, 4, 1080, 1, 0);
1085	} else {
1086		mfc_err("invalid buf type\n");
1087		return -EINVAL;
1088	}
1089	return 0;
1090}
1091
1092static int vidioc_s_fmt(struct file *file, void *priv, struct v4l2_format *f)
1093{
1094	struct s5p_mfc_dev *dev = video_drvdata(file);
1095	struct s5p_mfc_ctx *ctx = fh_to_ctx(priv);
1096	struct v4l2_pix_format_mplane *pix_fmt_mp = &f->fmt.pix_mp;
1097	int ret = 0;
1098
1099	ret = vidioc_try_fmt(file, priv, f);
1100	if (ret)
1101		return ret;
1102	if (ctx->vq_src.streaming || ctx->vq_dst.streaming) {
1103		v4l2_err(&dev->v4l2_dev, "%s queue busy\n", __func__);
1104		ret = -EBUSY;
1105		goto out;
1106	}
1107	if (f->type == V4L2_BUF_TYPE_VIDEO_CAPTURE_MPLANE) {
1108		/* dst_fmt is validated by call to vidioc_try_fmt */
1109		ctx->dst_fmt = find_format(f, MFC_FMT_ENC);
1110		ctx->state = MFCINST_INIT;
1111		ctx->codec_mode = ctx->dst_fmt->codec_mode;
1112		ctx->enc_dst_buf_size =	pix_fmt_mp->plane_fmt[0].sizeimage;
1113		pix_fmt_mp->plane_fmt[0].bytesperline = 0;
1114		ctx->dst_bufs_cnt = 0;
1115		ctx->capture_state = QUEUE_FREE;
1116		ret = s5p_mfc_open_mfc_inst(dev, ctx);
1117	} else if (f->type == V4L2_BUF_TYPE_VIDEO_OUTPUT_MPLANE) {
1118		/* src_fmt is validated by call to vidioc_try_fmt */
1119		ctx->src_fmt = find_format(f, MFC_FMT_RAW);
1120		ctx->img_width = pix_fmt_mp->width;
1121		ctx->img_height = pix_fmt_mp->height;
1122		mfc_debug(2, "codec number: %d\n", ctx->src_fmt->codec_mode);
1123		mfc_debug(2, "fmt - w: %d, h: %d, ctx - w: %d, h: %d\n",
1124			pix_fmt_mp->width, pix_fmt_mp->height,
1125			ctx->img_width, ctx->img_height);
1126
1127		s5p_mfc_hw_call(dev->mfc_ops, enc_calc_src_size, ctx);
1128		pix_fmt_mp->plane_fmt[0].sizeimage = ctx->luma_size;
1129		pix_fmt_mp->plane_fmt[0].bytesperline = ctx->buf_width;
1130		pix_fmt_mp->plane_fmt[1].sizeimage = ctx->chroma_size;
1131		pix_fmt_mp->plane_fmt[1].bytesperline = ctx->buf_width;
1132
1133		ctx->src_bufs_cnt = 0;
1134		ctx->output_state = QUEUE_FREE;
1135	} else {
1136		mfc_err("invalid buf type\n");
1137		ret = -EINVAL;
1138	}
1139out:
1140	mfc_debug_leave();
1141	return ret;
1142}
1143
1144static int vidioc_reqbufs(struct file *file, void *priv,
1145					  struct v4l2_requestbuffers *reqbufs)
1146{
1147	struct s5p_mfc_dev *dev = video_drvdata(file);
1148	struct s5p_mfc_ctx *ctx = fh_to_ctx(priv);
1149	int ret = 0;
1150
1151	/* if memory is not mmp or userptr return error */
1152	if ((reqbufs->memory != V4L2_MEMORY_MMAP) &&
1153		(reqbufs->memory != V4L2_MEMORY_USERPTR))
1154		return -EINVAL;
1155	if (reqbufs->type == V4L2_BUF_TYPE_VIDEO_CAPTURE_MPLANE) {
1156		if (ctx->capture_state != QUEUE_FREE) {
1157			mfc_err("invalid capture state: %d\n",
1158							ctx->capture_state);
1159			return -EINVAL;
1160		}
1161		ret = vb2_reqbufs(&ctx->vq_dst, reqbufs);
1162		if (ret != 0) {
1163			mfc_err("error in vb2_reqbufs() for E(D)\n");
1164			return ret;
1165		}
1166		ctx->capture_state = QUEUE_BUFS_REQUESTED;
1167
1168		ret = s5p_mfc_hw_call(ctx->dev->mfc_ops,
1169				alloc_codec_buffers, ctx);
1170		if (ret) {
1171			mfc_err("Failed to allocate encoding buffers\n");
1172			reqbufs->count = 0;
1173			ret = vb2_reqbufs(&ctx->vq_dst, reqbufs);
1174			return -ENOMEM;
1175		}
1176	} else if (reqbufs->type == V4L2_BUF_TYPE_VIDEO_OUTPUT_MPLANE) {
1177		if (ctx->output_state != QUEUE_FREE) {
1178			mfc_err("invalid output state: %d\n",
1179							ctx->output_state);
1180			return -EINVAL;
1181		}
1182
1183		if (IS_MFCV6_PLUS(dev)) {
1184			/* Check for min encoder buffers */
1185			if (ctx->pb_count &&
1186				(reqbufs->count < ctx->pb_count)) {
1187				reqbufs->count = ctx->pb_count;
1188				mfc_debug(2, "Minimum %d output buffers needed\n",
1189						ctx->pb_count);
1190			} else {
1191				ctx->pb_count = reqbufs->count;
1192			}
1193		}
1194
1195		ret = vb2_reqbufs(&ctx->vq_src, reqbufs);
1196		if (ret != 0) {
1197			mfc_err("error in vb2_reqbufs() for E(S)\n");
1198			return ret;
1199		}
1200		ctx->output_state = QUEUE_BUFS_REQUESTED;
1201	} else {
1202		mfc_err("invalid buf type\n");
1203		return -EINVAL;
1204	}
1205	return ret;
1206}
1207
1208static int vidioc_querybuf(struct file *file, void *priv,
1209						   struct v4l2_buffer *buf)
1210{
1211	struct s5p_mfc_ctx *ctx = fh_to_ctx(priv);
1212	int ret = 0;
1213
1214	/* if memory is not mmp or userptr return error */
1215	if ((buf->memory != V4L2_MEMORY_MMAP) &&
1216		(buf->memory != V4L2_MEMORY_USERPTR))
1217		return -EINVAL;
1218	if (buf->type == V4L2_BUF_TYPE_VIDEO_CAPTURE_MPLANE) {
1219		if (ctx->state != MFCINST_GOT_INST) {
1220			mfc_err("invalid context state: %d\n", ctx->state);
1221			return -EINVAL;
1222		}
1223		ret = vb2_querybuf(&ctx->vq_dst, buf);
1224		if (ret != 0) {
1225			mfc_err("error in vb2_querybuf() for E(D)\n");
1226			return ret;
1227		}
1228		buf->m.planes[0].m.mem_offset += DST_QUEUE_OFF_BASE;
1229	} else if (buf->type == V4L2_BUF_TYPE_VIDEO_OUTPUT_MPLANE) {
1230		ret = vb2_querybuf(&ctx->vq_src, buf);
1231		if (ret != 0) {
1232			mfc_err("error in vb2_querybuf() for E(S)\n");
1233			return ret;
1234		}
1235	} else {
1236		mfc_err("invalid buf type\n");
1237		return -EINVAL;
1238	}
1239	return ret;
1240}
1241
1242/* Queue a buffer */
1243static int vidioc_qbuf(struct file *file, void *priv, struct v4l2_buffer *buf)
1244{
1245	struct s5p_mfc_ctx *ctx = fh_to_ctx(priv);
1246
1247	if (ctx->state == MFCINST_ERROR) {
1248		mfc_err("Call on QBUF after unrecoverable error\n");
1249		return -EIO;
1250	}
1251	if (buf->type == V4L2_BUF_TYPE_VIDEO_OUTPUT_MPLANE) {
1252		if (ctx->state == MFCINST_FINISHING) {
1253			mfc_err("Call on QBUF after EOS command\n");
1254			return -EIO;
1255		}
1256		return vb2_qbuf(&ctx->vq_src, buf);
1257	} else if (buf->type == V4L2_BUF_TYPE_VIDEO_CAPTURE_MPLANE) {
1258		return vb2_qbuf(&ctx->vq_dst, buf);
1259	}
1260	return -EINVAL;
1261}
1262
1263/* Dequeue a buffer */
1264static int vidioc_dqbuf(struct file *file, void *priv, struct v4l2_buffer *buf)
1265{
1266	const struct v4l2_event ev = {
1267		.type = V4L2_EVENT_EOS
1268	};
1269	struct s5p_mfc_ctx *ctx = fh_to_ctx(priv);
1270	int ret;
1271
1272	if (ctx->state == MFCINST_ERROR) {
1273		mfc_err("Call on DQBUF after unrecoverable error\n");
1274		return -EIO;
1275	}
1276	if (buf->type == V4L2_BUF_TYPE_VIDEO_OUTPUT_MPLANE) {
1277		ret = vb2_dqbuf(&ctx->vq_src, buf, file->f_flags & O_NONBLOCK);
1278	} else if (buf->type == V4L2_BUF_TYPE_VIDEO_CAPTURE_MPLANE) {
1279		ret = vb2_dqbuf(&ctx->vq_dst, buf, file->f_flags & O_NONBLOCK);
1280		if (ret == 0 && ctx->state == MFCINST_FINISHED
1281					&& list_empty(&ctx->vq_dst.done_list))
1282			v4l2_event_queue_fh(&ctx->fh, &ev);
1283	} else {
1284		ret = -EINVAL;
1285	}
1286
1287	return ret;
1288}
1289
1290/* Export DMA buffer */
1291static int vidioc_expbuf(struct file *file, void *priv,
1292	struct v4l2_exportbuffer *eb)
1293{
1294	struct s5p_mfc_ctx *ctx = fh_to_ctx(priv);
1295
1296	if (eb->type == V4L2_BUF_TYPE_VIDEO_OUTPUT_MPLANE)
1297		return vb2_expbuf(&ctx->vq_src, eb);
1298	if (eb->type == V4L2_BUF_TYPE_VIDEO_CAPTURE_MPLANE)
1299		return vb2_expbuf(&ctx->vq_dst, eb);
1300	return -EINVAL;
1301}
1302
1303/* Stream on */
1304static int vidioc_streamon(struct file *file, void *priv,
1305			   enum v4l2_buf_type type)
1306{
1307	struct s5p_mfc_ctx *ctx = fh_to_ctx(priv);
1308
1309	if (type == V4L2_BUF_TYPE_VIDEO_OUTPUT_MPLANE)
1310		return vb2_streamon(&ctx->vq_src, type);
1311	else if (type == V4L2_BUF_TYPE_VIDEO_CAPTURE_MPLANE)
1312		return vb2_streamon(&ctx->vq_dst, type);
1313	return -EINVAL;
1314}
1315
1316/* Stream off, which equals to a pause */
1317static int vidioc_streamoff(struct file *file, void *priv,
1318			    enum v4l2_buf_type type)
1319{
1320	struct s5p_mfc_ctx *ctx = fh_to_ctx(priv);
1321
1322	if (type == V4L2_BUF_TYPE_VIDEO_OUTPUT_MPLANE)
1323		return vb2_streamoff(&ctx->vq_src, type);
1324	else if (type == V4L2_BUF_TYPE_VIDEO_CAPTURE_MPLANE)
1325		return vb2_streamoff(&ctx->vq_dst, type);
1326	return -EINVAL;
1327}
1328
1329static inline int h264_level(enum v4l2_mpeg_video_h264_level lvl)
1330{
1331	static unsigned int t[V4L2_MPEG_VIDEO_H264_LEVEL_4_0 + 1] = {
1332		/* V4L2_MPEG_VIDEO_H264_LEVEL_1_0   */ 10,
1333		/* V4L2_MPEG_VIDEO_H264_LEVEL_1B    */ 9,
1334		/* V4L2_MPEG_VIDEO_H264_LEVEL_1_1   */ 11,
1335		/* V4L2_MPEG_VIDEO_H264_LEVEL_1_2   */ 12,
1336		/* V4L2_MPEG_VIDEO_H264_LEVEL_1_3   */ 13,
1337		/* V4L2_MPEG_VIDEO_H264_LEVEL_2_0   */ 20,
1338		/* V4L2_MPEG_VIDEO_H264_LEVEL_2_1   */ 21,
1339		/* V4L2_MPEG_VIDEO_H264_LEVEL_2_2   */ 22,
1340		/* V4L2_MPEG_VIDEO_H264_LEVEL_3_0   */ 30,
1341		/* V4L2_MPEG_VIDEO_H264_LEVEL_3_1   */ 31,
1342		/* V4L2_MPEG_VIDEO_H264_LEVEL_3_2   */ 32,
1343		/* V4L2_MPEG_VIDEO_H264_LEVEL_4_0   */ 40,
1344	};
1345	return t[lvl];
1346}
1347
1348static inline int mpeg4_level(enum v4l2_mpeg_video_mpeg4_level lvl)
1349{
1350	static unsigned int t[V4L2_MPEG_VIDEO_MPEG4_LEVEL_5 + 1] = {
1351		/* V4L2_MPEG_VIDEO_MPEG4_LEVEL_0    */ 0,
1352		/* V4L2_MPEG_VIDEO_MPEG4_LEVEL_0B   */ 9,
1353		/* V4L2_MPEG_VIDEO_MPEG4_LEVEL_1    */ 1,
1354		/* V4L2_MPEG_VIDEO_MPEG4_LEVEL_2    */ 2,
1355		/* V4L2_MPEG_VIDEO_MPEG4_LEVEL_3    */ 3,
1356		/* V4L2_MPEG_VIDEO_MPEG4_LEVEL_3B   */ 7,
1357		/* V4L2_MPEG_VIDEO_MPEG4_LEVEL_4    */ 4,
1358		/* V4L2_MPEG_VIDEO_MPEG4_LEVEL_5    */ 5,
1359	};
1360	return t[lvl];
1361}
1362
1363static inline int vui_sar_idc(enum v4l2_mpeg_video_h264_vui_sar_idc sar)
1364{
1365	static unsigned int t[V4L2_MPEG_VIDEO_H264_VUI_SAR_IDC_EXTENDED + 1] = {
1366		/* V4L2_MPEG_VIDEO_H264_VUI_SAR_IDC_UNSPECIFIED     */ 0,
1367		/* V4L2_MPEG_VIDEO_H264_VUI_SAR_IDC_1x1             */ 1,
1368		/* V4L2_MPEG_VIDEO_H264_VUI_SAR_IDC_12x11           */ 2,
1369		/* V4L2_MPEG_VIDEO_H264_VUI_SAR_IDC_10x11           */ 3,
1370		/* V4L2_MPEG_VIDEO_H264_VUI_SAR_IDC_16x11           */ 4,
1371		/* V4L2_MPEG_VIDEO_H264_VUI_SAR_IDC_40x33           */ 5,
1372		/* V4L2_MPEG_VIDEO_H264_VUI_SAR_IDC_24x11           */ 6,
1373		/* V4L2_MPEG_VIDEO_H264_VUI_SAR_IDC_20x11           */ 7,
1374		/* V4L2_MPEG_VIDEO_H264_VUI_SAR_IDC_32x11           */ 8,
1375		/* V4L2_MPEG_VIDEO_H264_VUI_SAR_IDC_80x33           */ 9,
1376		/* V4L2_MPEG_VIDEO_H264_VUI_SAR_IDC_18x11           */ 10,
1377		/* V4L2_MPEG_VIDEO_H264_VUI_SAR_IDC_15x11           */ 11,
1378		/* V4L2_MPEG_VIDEO_H264_VUI_SAR_IDC_64x33           */ 12,
1379		/* V4L2_MPEG_VIDEO_H264_VUI_SAR_IDC_160x99          */ 13,
1380		/* V4L2_MPEG_VIDEO_H264_VUI_SAR_IDC_4x3             */ 14,
1381		/* V4L2_MPEG_VIDEO_H264_VUI_SAR_IDC_3x2             */ 15,
1382		/* V4L2_MPEG_VIDEO_H264_VUI_SAR_IDC_2x1             */ 16,
1383		/* V4L2_MPEG_VIDEO_H264_VUI_SAR_IDC_EXTENDED        */ 255,
1384	};
1385	return t[sar];
1386}
1387
1388static int s5p_mfc_enc_s_ctrl(struct v4l2_ctrl *ctrl)
1389{
1390	struct s5p_mfc_ctx *ctx = ctrl_to_ctx(ctrl);
1391	struct s5p_mfc_dev *dev = ctx->dev;
1392	struct s5p_mfc_enc_params *p = &ctx->enc_params;
1393	int ret = 0;
1394
1395	switch (ctrl->id) {
1396	case V4L2_CID_MPEG_VIDEO_GOP_SIZE:
1397		p->gop_size = ctrl->val;
1398		break;
1399	case V4L2_CID_MPEG_VIDEO_MULTI_SLICE_MODE:
1400		p->slice_mode = ctrl->val;
1401		break;
1402	case V4L2_CID_MPEG_VIDEO_MULTI_SLICE_MAX_MB:
1403		p->slice_mb = ctrl->val;
1404		break;
1405	case V4L2_CID_MPEG_VIDEO_MULTI_SLICE_MAX_BYTES:
1406		p->slice_bit = ctrl->val * 8;
1407		break;
1408	case V4L2_CID_MPEG_VIDEO_CYCLIC_INTRA_REFRESH_MB:
1409		p->intra_refresh_mb = ctrl->val;
1410		break;
1411	case V4L2_CID_MPEG_MFC51_VIDEO_PADDING:
1412		p->pad = ctrl->val;
1413		break;
1414	case V4L2_CID_MPEG_MFC51_VIDEO_PADDING_YUV:
1415		p->pad_luma = (ctrl->val >> 16) & 0xff;
1416		p->pad_cb = (ctrl->val >> 8) & 0xff;
1417		p->pad_cr = (ctrl->val >> 0) & 0xff;
1418		break;
1419	case V4L2_CID_MPEG_VIDEO_FRAME_RC_ENABLE:
1420		p->rc_frame = ctrl->val;
1421		break;
1422	case V4L2_CID_MPEG_VIDEO_BITRATE:
1423		p->rc_bitrate = ctrl->val;
1424		break;
1425	case V4L2_CID_MPEG_MFC51_VIDEO_RC_REACTION_COEFF:
1426		p->rc_reaction_coeff = ctrl->val;
1427		break;
1428	case V4L2_CID_MPEG_MFC51_VIDEO_FORCE_FRAME_TYPE:
1429		ctx->force_frame_type = ctrl->val;
1430		break;
1431	case V4L2_CID_MPEG_VIDEO_VBV_SIZE:
1432		p->vbv_size = ctrl->val;
1433		break;
1434	case V4L2_CID_MPEG_VIDEO_MV_H_SEARCH_RANGE:
1435		p->mv_h_range = ctrl->val;
1436		break;
1437	case V4L2_CID_MPEG_VIDEO_MV_V_SEARCH_RANGE:
1438		p->mv_v_range = ctrl->val;
1439		break;
1440	case V4L2_CID_MPEG_VIDEO_H264_CPB_SIZE:
1441		p->codec.h264.cpb_size = ctrl->val;
1442		break;
1443	case V4L2_CID_MPEG_VIDEO_HEADER_MODE:
1444		p->seq_hdr_mode = ctrl->val;
1445		break;
1446	case V4L2_CID_MPEG_MFC51_VIDEO_FRAME_SKIP_MODE:
1447		p->frame_skip_mode = ctrl->val;
1448		break;
1449	case V4L2_CID_MPEG_MFC51_VIDEO_RC_FIXED_TARGET_BIT:
1450		p->fixed_target_bit = ctrl->val;
1451		break;
1452	case V4L2_CID_MPEG_VIDEO_B_FRAMES:
1453		p->num_b_frame = ctrl->val;
1454		break;
1455	case V4L2_CID_MPEG_VIDEO_H264_PROFILE:
1456		switch (ctrl->val) {
1457		case V4L2_MPEG_VIDEO_H264_PROFILE_MAIN:
1458			p->codec.h264.profile =
1459					S5P_FIMV_ENC_PROFILE_H264_MAIN;
1460			break;
1461		case V4L2_MPEG_VIDEO_H264_PROFILE_HIGH:
1462			p->codec.h264.profile =
1463					S5P_FIMV_ENC_PROFILE_H264_HIGH;
1464			break;
1465		case V4L2_MPEG_VIDEO_H264_PROFILE_BASELINE:
1466			p->codec.h264.profile =
1467				S5P_FIMV_ENC_PROFILE_H264_BASELINE;
1468			break;
1469		case V4L2_MPEG_VIDEO_H264_PROFILE_CONSTRAINED_BASELINE:
1470			if (IS_MFCV6_PLUS(dev))
1471				p->codec.h264.profile =
1472				S5P_FIMV_ENC_PROFILE_H264_CONSTRAINED_BASELINE;
1473			else
1474				ret = -EINVAL;
1475			break;
1476		default:
1477			ret = -EINVAL;
1478		}
1479		break;
1480	case V4L2_CID_MPEG_VIDEO_H264_LEVEL:
1481		p->codec.h264.level_v4l2 = ctrl->val;
1482		p->codec.h264.level = h264_level(ctrl->val);
1483		if (p->codec.h264.level < 0) {
1484			mfc_err("Level number is wrong\n");
1485			ret = p->codec.h264.level;
1486		}
1487		break;
1488	case V4L2_CID_MPEG_VIDEO_MPEG4_LEVEL:
1489		p->codec.mpeg4.level_v4l2 = ctrl->val;
1490		p->codec.mpeg4.level = mpeg4_level(ctrl->val);
1491		if (p->codec.mpeg4.level < 0) {
1492			mfc_err("Level number is wrong\n");
1493			ret = p->codec.mpeg4.level;
1494		}
1495		break;
1496	case V4L2_CID_MPEG_VIDEO_H264_LOOP_FILTER_MODE:
1497		p->codec.h264.loop_filter_mode = ctrl->val;
1498		break;
1499	case V4L2_CID_MPEG_VIDEO_H264_LOOP_FILTER_ALPHA:
1500		p->codec.h264.loop_filter_alpha = ctrl->val;
1501		break;
1502	case V4L2_CID_MPEG_VIDEO_H264_LOOP_FILTER_BETA:
1503		p->codec.h264.loop_filter_beta = ctrl->val;
1504		break;
1505	case V4L2_CID_MPEG_VIDEO_H264_ENTROPY_MODE:
1506		p->codec.h264.entropy_mode = ctrl->val;
1507		break;
1508	case V4L2_CID_MPEG_MFC51_VIDEO_H264_NUM_REF_PIC_FOR_P:
1509		p->codec.h264.num_ref_pic_4p = ctrl->val;
1510		break;
1511	case V4L2_CID_MPEG_VIDEO_H264_8X8_TRANSFORM:
1512		p->codec.h264._8x8_transform = ctrl->val;
1513		break;
1514	case V4L2_CID_MPEG_VIDEO_MB_RC_ENABLE:
1515		p->rc_mb = ctrl->val;
1516		break;
1517	case V4L2_CID_MPEG_VIDEO_H264_I_FRAME_QP:
1518		p->codec.h264.rc_frame_qp = ctrl->val;
1519		break;
1520	case V4L2_CID_MPEG_VIDEO_H264_MIN_QP:
1521		p->codec.h264.rc_min_qp = ctrl->val;
1522		break;
1523	case V4L2_CID_MPEG_VIDEO_H264_MAX_QP:
1524		p->codec.h264.rc_max_qp = ctrl->val;
1525		break;
1526	case V4L2_CID_MPEG_VIDEO_H264_P_FRAME_QP:
1527		p->codec.h264.rc_p_frame_qp = ctrl->val;
1528		break;
1529	case V4L2_CID_MPEG_VIDEO_H264_B_FRAME_QP:
1530		p->codec.h264.rc_b_frame_qp = ctrl->val;
1531		break;
1532	case V4L2_CID_MPEG_VIDEO_MPEG4_I_FRAME_QP:
1533	case V4L2_CID_MPEG_VIDEO_H263_I_FRAME_QP:
1534		p->codec.mpeg4.rc_frame_qp = ctrl->val;
1535		break;
1536	case V4L2_CID_MPEG_VIDEO_MPEG4_MIN_QP:
1537	case V4L2_CID_MPEG_VIDEO_H263_MIN_QP:
1538		p->codec.mpeg4.rc_min_qp = ctrl->val;
1539		break;
1540	case V4L2_CID_MPEG_VIDEO_MPEG4_MAX_QP:
1541	case V4L2_CID_MPEG_VIDEO_H263_MAX_QP:
1542		p->codec.mpeg4.rc_max_qp = ctrl->val;
1543		break;
1544	case V4L2_CID_MPEG_VIDEO_MPEG4_P_FRAME_QP:
1545	case V4L2_CID_MPEG_VIDEO_H263_P_FRAME_QP:
1546		p->codec.mpeg4.rc_p_frame_qp = ctrl->val;
1547		break;
1548	case V4L2_CID_MPEG_VIDEO_MPEG4_B_FRAME_QP:
1549	case V4L2_CID_MPEG_VIDEO_H263_B_FRAME_QP:
1550		p->codec.mpeg4.rc_b_frame_qp = ctrl->val;
1551		break;
1552	case V4L2_CID_MPEG_MFC51_VIDEO_H264_ADAPTIVE_RC_DARK:
1553		p->codec.h264.rc_mb_dark = ctrl->val;
1554		break;
1555	case V4L2_CID_MPEG_MFC51_VIDEO_H264_ADAPTIVE_RC_SMOOTH:
1556		p->codec.h264.rc_mb_smooth = ctrl->val;
1557		break;
1558	case V4L2_CID_MPEG_MFC51_VIDEO_H264_ADAPTIVE_RC_STATIC:
1559		p->codec.h264.rc_mb_static = ctrl->val;
1560		break;
1561	case V4L2_CID_MPEG_MFC51_VIDEO_H264_ADAPTIVE_RC_ACTIVITY:
1562		p->codec.h264.rc_mb_activity = ctrl->val;
1563		break;
1564	case V4L2_CID_MPEG_VIDEO_H264_VUI_SAR_ENABLE:
1565		p->codec.h264.vui_sar = ctrl->val;
1566		break;
1567	case V4L2_CID_MPEG_VIDEO_H264_VUI_SAR_IDC:
1568		p->codec.h264.vui_sar_idc = vui_sar_idc(ctrl->val);
1569		break;
1570	case V4L2_CID_MPEG_VIDEO_H264_VUI_EXT_SAR_WIDTH:
1571		p->codec.h264.vui_ext_sar_width = ctrl->val;
1572		break;
1573	case V4L2_CID_MPEG_VIDEO_H264_VUI_EXT_SAR_HEIGHT:
1574		p->codec.h264.vui_ext_sar_height = ctrl->val;
1575		break;
1576	case V4L2_CID_MPEG_VIDEO_GOP_CLOSURE:
1577		p->codec.h264.open_gop = !ctrl->val;
1578		break;
1579	case V4L2_CID_MPEG_VIDEO_H264_I_PERIOD:
1580		p->codec.h264.open_gop_size = ctrl->val;
1581		break;
1582	case V4L2_CID_MPEG_VIDEO_MPEG4_PROFILE:
1583		switch (ctrl->val) {
1584		case V4L2_MPEG_VIDEO_MPEG4_PROFILE_SIMPLE:
1585			p->codec.mpeg4.profile =
1586				S5P_FIMV_ENC_PROFILE_MPEG4_SIMPLE;
1587			break;
1588		case V4L2_MPEG_VIDEO_MPEG4_PROFILE_ADVANCED_SIMPLE:
1589			p->codec.mpeg4.profile =
1590			S5P_FIMV_ENC_PROFILE_MPEG4_ADVANCED_SIMPLE;
1591			break;
1592		default:
1593			ret = -EINVAL;
1594		}
1595		break;
1596	case V4L2_CID_MPEG_VIDEO_MPEG4_QPEL:
1597		p->codec.mpeg4.quarter_pixel = ctrl->val;
1598		break;
1599	case V4L2_CID_MPEG_VIDEO_VPX_NUM_PARTITIONS:
1600		p->codec.vp8.num_partitions = ctrl->val;
1601		break;
1602	case V4L2_CID_MPEG_VIDEO_VPX_IMD_DISABLE_4X4:
1603		p->codec.vp8.imd_4x4 = ctrl->val;
1604		break;
1605	case V4L2_CID_MPEG_VIDEO_VPX_NUM_REF_FRAMES:
1606		p->codec.vp8.num_ref = ctrl->val;
1607		break;
1608	case V4L2_CID_MPEG_VIDEO_VPX_FILTER_LEVEL:
1609		p->codec.vp8.filter_level = ctrl->val;
1610		break;
1611	case V4L2_CID_MPEG_VIDEO_VPX_FILTER_SHARPNESS:
1612		p->codec.vp8.filter_sharpness = ctrl->val;
1613		break;
1614	case V4L2_CID_MPEG_VIDEO_VPX_GOLDEN_FRAME_REF_PERIOD:
1615		p->codec.vp8.golden_frame_ref_period = ctrl->val;
1616		break;
1617	case V4L2_CID_MPEG_VIDEO_VPX_GOLDEN_FRAME_SEL:
1618		p->codec.vp8.golden_frame_sel = ctrl->val;
1619		break;
1620	case V4L2_CID_MPEG_VIDEO_VPX_MIN_QP:
1621		p->codec.vp8.rc_min_qp = ctrl->val;
1622		break;
1623	case V4L2_CID_MPEG_VIDEO_VPX_MAX_QP:
1624		p->codec.vp8.rc_max_qp = ctrl->val;
1625		break;
1626	case V4L2_CID_MPEG_VIDEO_VPX_I_FRAME_QP:
1627		p->codec.vp8.rc_frame_qp = ctrl->val;
1628		break;
1629	case V4L2_CID_MPEG_VIDEO_VPX_P_FRAME_QP:
1630		p->codec.vp8.rc_p_frame_qp = ctrl->val;
1631		break;
1632	case V4L2_CID_MPEG_VIDEO_VPX_PROFILE:
1633		p->codec.vp8.profile = ctrl->val;
1634		break;
1635	default:
1636		v4l2_err(&dev->v4l2_dev, "Invalid control, id=%d, val=%d\n",
1637							ctrl->id, ctrl->val);
1638		ret = -EINVAL;
1639	}
1640	return ret;
1641}
1642
1643static const struct v4l2_ctrl_ops s5p_mfc_enc_ctrl_ops = {
1644	.s_ctrl = s5p_mfc_enc_s_ctrl,
1645};
1646
1647static int vidioc_s_parm(struct file *file, void *priv,
1648			 struct v4l2_streamparm *a)
1649{
1650	struct s5p_mfc_ctx *ctx = fh_to_ctx(priv);
1651
1652	if (a->type == V4L2_BUF_TYPE_VIDEO_OUTPUT_MPLANE) {
1653		ctx->enc_params.rc_framerate_num =
1654					a->parm.output.timeperframe.denominator;
1655		ctx->enc_params.rc_framerate_denom =
1656					a->parm.output.timeperframe.numerator;
1657	} else {
1658		mfc_err("Setting FPS is only possible for the output queue\n");
1659		return -EINVAL;
1660	}
1661	return 0;
1662}
1663
1664static int vidioc_g_parm(struct file *file, void *priv,
1665			 struct v4l2_streamparm *a)
1666{
1667	struct s5p_mfc_ctx *ctx = fh_to_ctx(priv);
1668
1669	if (a->type == V4L2_BUF_TYPE_VIDEO_OUTPUT) {
1670		a->parm.output.timeperframe.denominator =
1671					ctx->enc_params.rc_framerate_num;
1672		a->parm.output.timeperframe.numerator =
1673					ctx->enc_params.rc_framerate_denom;
1674	} else {
1675		mfc_err("Setting FPS is only possible for the output queue\n");
1676		return -EINVAL;
1677	}
1678	return 0;
1679}
1680
1681static int vidioc_encoder_cmd(struct file *file, void *priv,
1682			      struct v4l2_encoder_cmd *cmd)
1683{
1684	struct s5p_mfc_ctx *ctx = fh_to_ctx(priv);
1685	struct s5p_mfc_dev *dev = ctx->dev;
1686	struct s5p_mfc_buf *buf;
1687	unsigned long flags;
1688
1689	switch (cmd->cmd) {
1690	case V4L2_ENC_CMD_STOP:
1691		if (cmd->flags != 0)
1692			return -EINVAL;
1693
1694		if (!ctx->vq_src.streaming)
1695			return -EINVAL;
1696
1697		spin_lock_irqsave(&dev->irqlock, flags);
1698		if (list_empty(&ctx->src_queue)) {
1699			mfc_debug(2, "EOS: empty src queue, entering finishing state\n");
1700			ctx->state = MFCINST_FINISHING;
1701			if (s5p_mfc_ctx_ready(ctx))
1702				set_work_bit_irqsave(ctx);
1703			spin_unlock_irqrestore(&dev->irqlock, flags);
1704			s5p_mfc_hw_call(dev->mfc_ops, try_run, dev);
1705		} else {
1706			mfc_debug(2, "EOS: marking last buffer of stream\n");
1707			buf = list_entry(ctx->src_queue.prev,
1708						struct s5p_mfc_buf, list);
1709			if (buf->flags & MFC_BUF_FLAG_USED)
1710				ctx->state = MFCINST_FINISHING;
1711			else
1712				buf->flags |= MFC_BUF_FLAG_EOS;
1713			spin_unlock_irqrestore(&dev->irqlock, flags);
1714		}
1715		break;
1716	default:
1717		return -EINVAL;
1718
1719	}
1720	return 0;
1721}
1722
1723static int vidioc_subscribe_event(struct v4l2_fh *fh,
1724				  const struct v4l2_event_subscription *sub)
1725{
1726	switch (sub->type) {
1727	case V4L2_EVENT_EOS:
1728		return v4l2_event_subscribe(fh, sub, 2, NULL);
1729	default:
1730		return -EINVAL;
1731	}
1732}
1733
1734static const struct v4l2_ioctl_ops s5p_mfc_enc_ioctl_ops = {
1735	.vidioc_querycap = vidioc_querycap,
1736	.vidioc_enum_fmt_vid_cap = vidioc_enum_fmt_vid_cap,
1737	.vidioc_enum_fmt_vid_cap_mplane = vidioc_enum_fmt_vid_cap_mplane,
1738	.vidioc_enum_fmt_vid_out = vidioc_enum_fmt_vid_out,
1739	.vidioc_enum_fmt_vid_out_mplane = vidioc_enum_fmt_vid_out_mplane,
1740	.vidioc_g_fmt_vid_cap_mplane = vidioc_g_fmt,
1741	.vidioc_g_fmt_vid_out_mplane = vidioc_g_fmt,
1742	.vidioc_try_fmt_vid_cap_mplane = vidioc_try_fmt,
1743	.vidioc_try_fmt_vid_out_mplane = vidioc_try_fmt,
1744	.vidioc_s_fmt_vid_cap_mplane = vidioc_s_fmt,
1745	.vidioc_s_fmt_vid_out_mplane = vidioc_s_fmt,
1746	.vidioc_reqbufs = vidioc_reqbufs,
1747	.vidioc_querybuf = vidioc_querybuf,
1748	.vidioc_qbuf = vidioc_qbuf,
1749	.vidioc_dqbuf = vidioc_dqbuf,
1750	.vidioc_expbuf = vidioc_expbuf,
1751	.vidioc_streamon = vidioc_streamon,
1752	.vidioc_streamoff = vidioc_streamoff,
1753	.vidioc_s_parm = vidioc_s_parm,
1754	.vidioc_g_parm = vidioc_g_parm,
1755	.vidioc_encoder_cmd = vidioc_encoder_cmd,
1756	.vidioc_subscribe_event = vidioc_subscribe_event,
1757	.vidioc_unsubscribe_event = v4l2_event_unsubscribe,
1758};
1759
1760static int check_vb_with_fmt(struct s5p_mfc_fmt *fmt, struct vb2_buffer *vb)
1761{
1762	int i;
1763
1764	if (!fmt)
1765		return -EINVAL;
1766	if (fmt->num_planes != vb->num_planes) {
1767		mfc_err("invalid plane number for the format\n");
1768		return -EINVAL;
1769	}
1770	for (i = 0; i < fmt->num_planes; i++) {
1771		if (!vb2_dma_contig_plane_dma_addr(vb, i)) {
1772			mfc_err("failed to get plane cookie\n");
1773			return -EINVAL;
1774		}
1775		mfc_debug(2, "index: %d, plane[%d] cookie: 0x%08zx\n",
1776			  vb->v4l2_buf.index, i,
1777			  vb2_dma_contig_plane_dma_addr(vb, i));
1778	}
1779	return 0;
1780}
1781
1782static int s5p_mfc_queue_setup(struct vb2_queue *vq,
1783			const struct v4l2_format *fmt,
1784			unsigned int *buf_count, unsigned int *plane_count,
1785			unsigned int psize[], void *allocators[])
1786{
1787	struct s5p_mfc_ctx *ctx = fh_to_ctx(vq->drv_priv);
1788	struct s5p_mfc_dev *dev = ctx->dev;
1789
1790	if (ctx->state != MFCINST_GOT_INST) {
1791		mfc_err("inavlid state: %d\n", ctx->state);
1792		return -EINVAL;
1793	}
1794	if (vq->type == V4L2_BUF_TYPE_VIDEO_CAPTURE_MPLANE) {
1795		if (ctx->dst_fmt)
1796			*plane_count = ctx->dst_fmt->num_planes;
1797		else
1798			*plane_count = MFC_ENC_CAP_PLANE_COUNT;
1799		if (*buf_count < 1)
1800			*buf_count = 1;
1801		if (*buf_count > MFC_MAX_BUFFERS)
1802			*buf_count = MFC_MAX_BUFFERS;
1803		psize[0] = ctx->enc_dst_buf_size;
1804		allocators[0] = ctx->dev->alloc_ctx[MFC_BANK1_ALLOC_CTX];
1805	} else if (vq->type == V4L2_BUF_TYPE_VIDEO_OUTPUT_MPLANE) {
1806		if (ctx->src_fmt)
1807			*plane_count = ctx->src_fmt->num_planes;
1808		else
1809			*plane_count = MFC_ENC_OUT_PLANE_COUNT;
1810
1811		if (*buf_count < 1)
1812			*buf_count = 1;
1813		if (*buf_count > MFC_MAX_BUFFERS)
1814			*buf_count = MFC_MAX_BUFFERS;
1815
1816		psize[0] = ctx->luma_size;
1817		psize[1] = ctx->chroma_size;
1818
1819		if (IS_MFCV6_PLUS(dev)) {
1820			allocators[0] =
1821				ctx->dev->alloc_ctx[MFC_BANK1_ALLOC_CTX];
1822			allocators[1] =
1823				ctx->dev->alloc_ctx[MFC_BANK1_ALLOC_CTX];
1824		} else {
1825			allocators[0] =
1826				ctx->dev->alloc_ctx[MFC_BANK2_ALLOC_CTX];
1827			allocators[1] =
1828				ctx->dev->alloc_ctx[MFC_BANK2_ALLOC_CTX];
1829		}
1830	} else {
1831		mfc_err("inavlid queue type: %d\n", vq->type);
1832		return -EINVAL;
1833	}
1834	return 0;
1835}
1836
1837static void s5p_mfc_unlock(struct vb2_queue *q)
1838{
1839	struct s5p_mfc_ctx *ctx = fh_to_ctx(q->drv_priv);
1840	struct s5p_mfc_dev *dev = ctx->dev;
1841
1842	mutex_unlock(&dev->mfc_mutex);
1843}
1844
1845static void s5p_mfc_lock(struct vb2_queue *q)
1846{
1847	struct s5p_mfc_ctx *ctx = fh_to_ctx(q->drv_priv);
1848	struct s5p_mfc_dev *dev = ctx->dev;
1849
1850	mutex_lock(&dev->mfc_mutex);
1851}
1852
1853static int s5p_mfc_buf_init(struct vb2_buffer *vb)
1854{
1855	struct vb2_queue *vq = vb->vb2_queue;
1856	struct s5p_mfc_ctx *ctx = fh_to_ctx(vq->drv_priv);
1857	unsigned int i;
1858	int ret;
1859
1860	if (vq->type == V4L2_BUF_TYPE_VIDEO_CAPTURE_MPLANE) {
1861		ret = check_vb_with_fmt(ctx->dst_fmt, vb);
1862		if (ret < 0)
1863			return ret;
1864		i = vb->v4l2_buf.index;
1865		ctx->dst_bufs[i].b = vb;
1866		ctx->dst_bufs[i].cookie.stream =
1867					vb2_dma_contig_plane_dma_addr(vb, 0);
1868		ctx->dst_bufs_cnt++;
1869	} else if (vq->type == V4L2_BUF_TYPE_VIDEO_OUTPUT_MPLANE) {
1870		ret = check_vb_with_fmt(ctx->src_fmt, vb);
1871		if (ret < 0)
1872			return ret;
1873		i = vb->v4l2_buf.index;
1874		ctx->src_bufs[i].b = vb;
1875		ctx->src_bufs[i].cookie.raw.luma =
1876					vb2_dma_contig_plane_dma_addr(vb, 0);
1877		ctx->src_bufs[i].cookie.raw.chroma =
1878					vb2_dma_contig_plane_dma_addr(vb, 1);
1879		ctx->src_bufs_cnt++;
1880	} else {
1881		mfc_err("inavlid queue type: %d\n", vq->type);
1882		return -EINVAL;
1883	}
1884	return 0;
1885}
1886
1887static int s5p_mfc_buf_prepare(struct vb2_buffer *vb)
1888{
1889	struct vb2_queue *vq = vb->vb2_queue;
1890	struct s5p_mfc_ctx *ctx = fh_to_ctx(vq->drv_priv);
1891	int ret;
1892
1893	if (vq->type == V4L2_BUF_TYPE_VIDEO_CAPTURE_MPLANE) {
1894		ret = check_vb_with_fmt(ctx->dst_fmt, vb);
1895		if (ret < 0)
1896			return ret;
1897		mfc_debug(2, "plane size: %ld, dst size: %d\n",
1898			vb2_plane_size(vb, 0), ctx->enc_dst_buf_size);
1899		if (vb2_plane_size(vb, 0) < ctx->enc_dst_buf_size) {
1900			mfc_err("plane size is too small for capture\n");
1901			return -EINVAL;
1902		}
1903	} else if (vq->type == V4L2_BUF_TYPE_VIDEO_OUTPUT_MPLANE) {
1904		ret = check_vb_with_fmt(ctx->src_fmt, vb);
1905		if (ret < 0)
1906			return ret;
1907		mfc_debug(2, "plane size: %ld, luma size: %d\n",
1908			vb2_plane_size(vb, 0), ctx->luma_size);
1909		mfc_debug(2, "plane size: %ld, chroma size: %d\n",
1910			vb2_plane_size(vb, 1), ctx->chroma_size);
1911		if (vb2_plane_size(vb, 0) < ctx->luma_size ||
1912		    vb2_plane_size(vb, 1) < ctx->chroma_size) {
1913			mfc_err("plane size is too small for output\n");
1914			return -EINVAL;
1915		}
1916	} else {
1917		mfc_err("inavlid queue type: %d\n", vq->type);
1918		return -EINVAL;
1919	}
1920	return 0;
1921}
1922
1923static int s5p_mfc_start_streaming(struct vb2_queue *q, unsigned int count)
1924{
1925	struct s5p_mfc_ctx *ctx = fh_to_ctx(q->drv_priv);
1926	struct s5p_mfc_dev *dev = ctx->dev;
1927
1928	if (IS_MFCV6_PLUS(dev) &&
1929			(q->type == V4L2_BUF_TYPE_VIDEO_CAPTURE_MPLANE)) {
1930
1931		if ((ctx->state == MFCINST_GOT_INST) &&
1932			(dev->curr_ctx == ctx->num) && dev->hw_lock) {
1933			s5p_mfc_wait_for_done_ctx(ctx,
1934						S5P_MFC_R2H_CMD_SEQ_DONE_RET,
1935						0);
1936		}
1937
1938		if (ctx->src_bufs_cnt < ctx->pb_count) {
1939			mfc_err("Need minimum %d OUTPUT buffers\n",
1940					ctx->pb_count);
1941			return -ENOBUFS;
1942		}
1943	}
1944
1945	/* If context is ready then dev = work->data;schedule it to run */
1946	if (s5p_mfc_ctx_ready(ctx))
1947		set_work_bit_irqsave(ctx);
1948	s5p_mfc_hw_call(dev->mfc_ops, try_run, dev);
1949
1950	return 0;
1951}
1952
1953static void s5p_mfc_stop_streaming(struct vb2_queue *q)
1954{
1955	unsigned long flags;
1956	struct s5p_mfc_ctx *ctx = fh_to_ctx(q->drv_priv);
1957	struct s5p_mfc_dev *dev = ctx->dev;
1958
1959	if ((ctx->state == MFCINST_FINISHING ||
1960		ctx->state == MFCINST_RUNNING) &&
1961		dev->curr_ctx == ctx->num && dev->hw_lock) {
1962		ctx->state = MFCINST_ABORT;
1963		s5p_mfc_wait_for_done_ctx(ctx, S5P_MFC_R2H_CMD_FRAME_DONE_RET,
1964					  0);
1965	}
1966	ctx->state = MFCINST_FINISHED;
1967	spin_lock_irqsave(&dev->irqlock, flags);
1968	if (q->type == V4L2_BUF_TYPE_VIDEO_CAPTURE_MPLANE) {
1969		s5p_mfc_hw_call(dev->mfc_ops, cleanup_queue, &ctx->dst_queue,
1970				&ctx->vq_dst);
1971		INIT_LIST_HEAD(&ctx->dst_queue);
1972		ctx->dst_queue_cnt = 0;
1973	}
1974	if (q->type == V4L2_BUF_TYPE_VIDEO_OUTPUT_MPLANE) {
1975		cleanup_ref_queue(ctx);
1976		s5p_mfc_hw_call(dev->mfc_ops, cleanup_queue, &ctx->src_queue,
1977				&ctx->vq_src);
1978		INIT_LIST_HEAD(&ctx->src_queue);
1979		ctx->src_queue_cnt = 0;
1980	}
1981	spin_unlock_irqrestore(&dev->irqlock, flags);
1982}
1983
1984static void s5p_mfc_buf_queue(struct vb2_buffer *vb)
1985{
1986	struct vb2_queue *vq = vb->vb2_queue;
1987	struct s5p_mfc_ctx *ctx = fh_to_ctx(vq->drv_priv);
1988	struct s5p_mfc_dev *dev = ctx->dev;
1989	unsigned long flags;
1990	struct s5p_mfc_buf *mfc_buf;
1991
1992	if (ctx->state == MFCINST_ERROR) {
1993		vb2_buffer_done(vb, VB2_BUF_STATE_ERROR);
1994		cleanup_ref_queue(ctx);
1995		return;
1996	}
1997	if (vq->type == V4L2_BUF_TYPE_VIDEO_CAPTURE_MPLANE) {
1998		mfc_buf = &ctx->dst_bufs[vb->v4l2_buf.index];
1999		mfc_buf->flags &= ~MFC_BUF_FLAG_USED;
2000		/* Mark destination as available for use by MFC */
2001		spin_lock_irqsave(&dev->irqlock, flags);
2002		list_add_tail(&mfc_buf->list, &ctx->dst_queue);
2003		ctx->dst_queue_cnt++;
2004		spin_unlock_irqrestore(&dev->irqlock, flags);
2005	} else if (vq->type == V4L2_BUF_TYPE_VIDEO_OUTPUT_MPLANE) {
2006		mfc_buf = &ctx->src_bufs[vb->v4l2_buf.index];
2007		mfc_buf->flags &= ~MFC_BUF_FLAG_USED;
2008		spin_lock_irqsave(&dev->irqlock, flags);
2009		list_add_tail(&mfc_buf->list, &ctx->src_queue);
2010		ctx->src_queue_cnt++;
2011		spin_unlock_irqrestore(&dev->irqlock, flags);
2012	} else {
2013		mfc_err("unsupported buffer type (%d)\n", vq->type);
2014	}
2015	if (s5p_mfc_ctx_ready(ctx))
2016		set_work_bit_irqsave(ctx);
2017	s5p_mfc_hw_call(dev->mfc_ops, try_run, dev);
2018}
2019
2020static struct vb2_ops s5p_mfc_enc_qops = {
2021	.queue_setup		= s5p_mfc_queue_setup,
2022	.wait_prepare		= s5p_mfc_unlock,
2023	.wait_finish		= s5p_mfc_lock,
2024	.buf_init		= s5p_mfc_buf_init,
2025	.buf_prepare		= s5p_mfc_buf_prepare,
2026	.start_streaming	= s5p_mfc_start_streaming,
2027	.stop_streaming		= s5p_mfc_stop_streaming,
2028	.buf_queue		= s5p_mfc_buf_queue,
2029};
2030
2031struct s5p_mfc_codec_ops *get_enc_codec_ops(void)
2032{
2033	return &encoder_codec_ops;
2034}
2035
2036struct vb2_ops *get_enc_queue_ops(void)
2037{
2038	return &s5p_mfc_enc_qops;
2039}
2040
2041const struct v4l2_ioctl_ops *get_enc_v4l2_ioctl_ops(void)
2042{
2043	return &s5p_mfc_enc_ioctl_ops;
2044}
2045
2046#define IS_MFC51_PRIV(x) ((V4L2_CTRL_ID2CLASS(x) == V4L2_CTRL_CLASS_MPEG) \
2047						&& V4L2_CTRL_DRIVER_PRIV(x))
2048
2049int s5p_mfc_enc_ctrls_setup(struct s5p_mfc_ctx *ctx)
2050{
2051	struct v4l2_ctrl_config cfg;
2052	int i;
2053
2054	v4l2_ctrl_handler_init(&ctx->ctrl_handler, NUM_CTRLS);
2055	if (ctx->ctrl_handler.error) {
2056		mfc_err("v4l2_ctrl_handler_init failed\n");
2057		return ctx->ctrl_handler.error;
2058	}
2059	for (i = 0; i < NUM_CTRLS; i++) {
2060		if (IS_MFC51_PRIV(controls[i].id)) {
2061			memset(&cfg, 0, sizeof(struct v4l2_ctrl_config));
2062			cfg.ops = &s5p_mfc_enc_ctrl_ops;
2063			cfg.id = controls[i].id;
2064			cfg.min = controls[i].minimum;
2065			cfg.max = controls[i].maximum;
2066			cfg.def = controls[i].default_value;
2067			cfg.name = controls[i].name;
2068			cfg.type = controls[i].type;
2069			cfg.flags = 0;
2070
2071			if (cfg.type == V4L2_CTRL_TYPE_MENU) {
2072				cfg.step = 0;
2073				cfg.menu_skip_mask = cfg.menu_skip_mask;
2074				cfg.qmenu = mfc51_get_menu(cfg.id);
2075			} else {
2076				cfg.step = controls[i].step;
2077				cfg.menu_skip_mask = 0;
2078			}
2079			ctx->ctrls[i] = v4l2_ctrl_new_custom(&ctx->ctrl_handler,
2080					&cfg, NULL);
2081		} else {
2082			if ((controls[i].type == V4L2_CTRL_TYPE_MENU) ||
2083				(controls[i].type ==
2084					V4L2_CTRL_TYPE_INTEGER_MENU)) {
2085				ctx->ctrls[i] = v4l2_ctrl_new_std_menu(
2086					&ctx->ctrl_handler,
2087					&s5p_mfc_enc_ctrl_ops, controls[i].id,
2088					controls[i].maximum, 0,
2089					controls[i].default_value);
2090			} else {
2091				ctx->ctrls[i] = v4l2_ctrl_new_std(
2092					&ctx->ctrl_handler,
2093					&s5p_mfc_enc_ctrl_ops, controls[i].id,
2094					controls[i].minimum,
2095					controls[i].maximum, controls[i].step,
2096					controls[i].default_value);
2097			}
2098		}
2099		if (ctx->ctrl_handler.error) {
2100			mfc_err("Adding control (%d) failed\n", i);
2101			return ctx->ctrl_handler.error;
2102		}
2103		if (controls[i].is_volatile && ctx->ctrls[i])
2104			ctx->ctrls[i]->flags |= V4L2_CTRL_FLAG_VOLATILE;
2105	}
2106	v4l2_ctrl_handler_setup(&ctx->ctrl_handler);
2107	return 0;
2108}
2109
2110void s5p_mfc_enc_ctrls_delete(struct s5p_mfc_ctx *ctx)
2111{
2112	int i;
2113
2114	v4l2_ctrl_handler_free(&ctx->ctrl_handler);
2115	for (i = 0; i < NUM_CTRLS; i++)
2116		ctx->ctrls[i] = NULL;
2117}
2118
2119void s5p_mfc_enc_init(struct s5p_mfc_ctx *ctx)
2120{
2121	struct v4l2_format f;
2122	f.fmt.pix_mp.pixelformat = DEF_SRC_FMT_ENC;
2123	ctx->src_fmt = find_format(&f, MFC_FMT_RAW);
2124	f.fmt.pix_mp.pixelformat = DEF_DST_FMT_ENC;
2125	ctx->dst_fmt = find_format(&f, MFC_FMT_ENC);
2126}
2127
2128