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