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