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