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