1 // SPDX-License-Identifier: GPL-2.0-or-later
2 /*
3  * linux/drivers/media/platform/samsung/s5p-mfc/s5p_mfc_enc.c
4  *
5  * Copyright (c) 2010-2011 Samsung Electronics Co., Ltd.
6  *		http://www.samsung.com/
7  *
8  * Jeongtae Park	<jtp.park@samsung.com>
9  * Kamil Debski		<k.debski@samsung.com>
10  */
11 
12 #include <linux/clk.h>
13 #include <linux/interrupt.h>
14 #include <linux/io.h>
15 #include <linux/module.h>
16 #include <linux/platform_device.h>
17 #include <linux/sched.h>
18 #include <linux/videodev2.h>
19 #include <media/v4l2-event.h>
20 #include <linux/workqueue.h>
21 #include <media/v4l2-ctrls.h>
22 #include <media/videobuf2-v4l2.h>
23 #include "s5p_mfc_common.h"
24 #include "s5p_mfc_ctrl.h"
25 #include "s5p_mfc_debug.h"
26 #include "s5p_mfc_enc.h"
27 #include "s5p_mfc_intr.h"
28 #include "s5p_mfc_opr.h"
29 
30 #define DEF_SRC_FMT_ENC	V4L2_PIX_FMT_NV12M
31 #define DEF_DST_FMT_ENC	V4L2_PIX_FMT_H264
32 
33 static struct s5p_mfc_fmt formats[] = {
34 	{
35 		.fourcc		= V4L2_PIX_FMT_NV12MT_16X16,
36 		.codec_mode	= S5P_MFC_CODEC_NONE,
37 		.type		= MFC_FMT_RAW,
38 		.num_planes	= 2,
39 		.versions	= MFC_V6_BIT | MFC_V7_BIT,
40 	},
41 	{
42 		.fourcc		= V4L2_PIX_FMT_NV12MT,
43 		.codec_mode	= S5P_MFC_CODEC_NONE,
44 		.type		= MFC_FMT_RAW,
45 		.num_planes	= 2,
46 		.versions	= MFC_V5_BIT,
47 	},
48 	{
49 		.fourcc		= V4L2_PIX_FMT_NV12M,
50 		.codec_mode	= S5P_MFC_CODEC_NONE,
51 		.type		= MFC_FMT_RAW,
52 		.num_planes	= 2,
53 		.versions	= MFC_V5PLUS_BITS,
54 	},
55 	{
56 		.fourcc		= V4L2_PIX_FMT_NV21M,
57 		.codec_mode	= S5P_MFC_CODEC_NONE,
58 		.type		= MFC_FMT_RAW,
59 		.num_planes	= 2,
60 		.versions	= MFC_V6PLUS_BITS,
61 	},
62 	{
63 		.fourcc		= V4L2_PIX_FMT_H264,
64 		.codec_mode	= S5P_MFC_CODEC_H264_ENC,
65 		.type		= MFC_FMT_ENC,
66 		.num_planes	= 1,
67 		.versions	= MFC_V5PLUS_BITS,
68 	},
69 	{
70 		.fourcc		= V4L2_PIX_FMT_MPEG4,
71 		.codec_mode	= S5P_MFC_CODEC_MPEG4_ENC,
72 		.type		= MFC_FMT_ENC,
73 		.num_planes	= 1,
74 		.versions	= MFC_V5PLUS_BITS,
75 	},
76 	{
77 		.fourcc		= V4L2_PIX_FMT_H263,
78 		.codec_mode	= S5P_MFC_CODEC_H263_ENC,
79 		.type		= MFC_FMT_ENC,
80 		.num_planes	= 1,
81 		.versions	= MFC_V5PLUS_BITS,
82 	},
83 	{
84 		.fourcc		= V4L2_PIX_FMT_VP8,
85 		.codec_mode	= S5P_MFC_CODEC_VP8_ENC,
86 		.type		= MFC_FMT_ENC,
87 		.num_planes	= 1,
88 		.versions	= MFC_V7PLUS_BITS,
89 	},
90 	{
91 		.fourcc		= V4L2_PIX_FMT_HEVC,
92 		.codec_mode	= S5P_FIMV_CODEC_HEVC_ENC,
93 		.type		= MFC_FMT_ENC,
94 		.num_planes	= 1,
95 		.versions	= MFC_V10_BIT,
96 	},
97 };
98 
99 #define NUM_FORMATS ARRAY_SIZE(formats)
100 static struct s5p_mfc_fmt *find_format(struct v4l2_format *f, unsigned int t)
101 {
102 	unsigned int i;
103 
104 	for (i = 0; i < NUM_FORMATS; i++) {
105 		if (formats[i].fourcc == f->fmt.pix_mp.pixelformat &&
106 		    formats[i].type == t)
107 			return &formats[i];
108 	}
109 	return NULL;
110 }
111 
112 static struct mfc_control controls[] = {
113 	{
114 		.id = V4L2_CID_MPEG_VIDEO_GOP_SIZE,
115 		.type = V4L2_CTRL_TYPE_INTEGER,
116 		.minimum = 0,
117 		.maximum = (1 << 16) - 1,
118 		.step = 1,
119 		.default_value = 12,
120 	},
121 	{
122 		.id = V4L2_CID_MPEG_VIDEO_MULTI_SLICE_MODE,
123 		.type = V4L2_CTRL_TYPE_MENU,
124 		.minimum = V4L2_MPEG_VIDEO_MULTI_SLICE_MODE_SINGLE,
125 		.maximum = V4L2_MPEG_VIDEO_MULTI_SLICE_MODE_MAX_BYTES,
126 		.default_value = V4L2_MPEG_VIDEO_MULTI_SLICE_MODE_SINGLE,
127 		.menu_skip_mask = 0,
128 	},
129 	{
130 		.id = V4L2_CID_MPEG_VIDEO_MULTI_SLICE_MAX_MB,
131 		.type = V4L2_CTRL_TYPE_INTEGER,
132 		.minimum = 1,
133 		.maximum = (1 << 16) - 1,
134 		.step = 1,
135 		.default_value = 1,
136 	},
137 	{
138 		.id = V4L2_CID_MPEG_VIDEO_MULTI_SLICE_MAX_BYTES,
139 		.type = V4L2_CTRL_TYPE_INTEGER,
140 		.minimum = 1900,
141 		.maximum = (1 << 30) - 1,
142 		.step = 1,
143 		.default_value = 1900,
144 	},
145 	{
146 		.id = V4L2_CID_MPEG_VIDEO_CYCLIC_INTRA_REFRESH_MB,
147 		.type = V4L2_CTRL_TYPE_INTEGER,
148 		.minimum = 0,
149 		.maximum = (1 << 16) - 1,
150 		.step = 1,
151 		.default_value = 0,
152 	},
153 	{
154 		.id = V4L2_CID_MPEG_MFC51_VIDEO_PADDING,
155 		.type = V4L2_CTRL_TYPE_BOOLEAN,
156 		.name = "Padding Control Enable",
157 		.minimum = 0,
158 		.maximum = 1,
159 		.step = 1,
160 		.default_value = 0,
161 	},
162 	{
163 		.id = V4L2_CID_MPEG_MFC51_VIDEO_PADDING_YUV,
164 		.type = V4L2_CTRL_TYPE_INTEGER,
165 		.name = "Padding Color YUV Value",
166 		.minimum = 0,
167 		.maximum = (1 << 25) - 1,
168 		.step = 1,
169 		.default_value = 0,
170 	},
171 	{
172 		.id = V4L2_CID_MPEG_VIDEO_FRAME_RC_ENABLE,
173 		.type = V4L2_CTRL_TYPE_BOOLEAN,
174 		.minimum = 0,
175 		.maximum = 1,
176 		.step = 1,
177 		.default_value = 0,
178 	},
179 	{
180 		.id = V4L2_CID_MPEG_VIDEO_BITRATE,
181 		.type = V4L2_CTRL_TYPE_INTEGER,
182 		.minimum = 1,
183 		.maximum = (1 << 30) - 1,
184 		.step = 1,
185 		.default_value = 1,
186 	},
187 	{
188 		.id = V4L2_CID_MPEG_MFC51_VIDEO_RC_REACTION_COEFF,
189 		.type = V4L2_CTRL_TYPE_INTEGER,
190 		.name = "Rate Control Reaction Coeff.",
191 		.minimum = 1,
192 		.maximum = (1 << 16) - 1,
193 		.step = 1,
194 		.default_value = 1,
195 	},
196 	{
197 		.id = V4L2_CID_MPEG_MFC51_VIDEO_FORCE_FRAME_TYPE,
198 		.type = V4L2_CTRL_TYPE_MENU,
199 		.name = "Force frame type",
200 		.minimum = V4L2_MPEG_MFC51_VIDEO_FORCE_FRAME_TYPE_DISABLED,
201 		.maximum = V4L2_MPEG_MFC51_VIDEO_FORCE_FRAME_TYPE_NOT_CODED,
202 		.default_value = V4L2_MPEG_MFC51_VIDEO_FORCE_FRAME_TYPE_DISABLED,
203 		.menu_skip_mask = 0,
204 	},
205 	{
206 		.id = V4L2_CID_MPEG_VIDEO_FORCE_KEY_FRAME,
207 		.type = V4L2_CTRL_TYPE_BUTTON,
208 		.minimum = 0,
209 		.maximum = 0,
210 		.step = 0,
211 		.default_value = 0,
212 	},
213 	{
214 		.id = V4L2_CID_MPEG_VIDEO_VBV_SIZE,
215 		.type = V4L2_CTRL_TYPE_INTEGER,
216 		.minimum = 0,
217 		.maximum = (1 << 16) - 1,
218 		.step = 1,
219 		.default_value = 0,
220 	},
221 	{
222 		.id = V4L2_CID_MPEG_VIDEO_MV_H_SEARCH_RANGE,
223 		.type = V4L2_CTRL_TYPE_INTEGER,
224 		.name = "Horizontal MV Search Range",
225 		.minimum = 16,
226 		.maximum = 128,
227 		.step = 16,
228 		.default_value = 32,
229 	},
230 	{
231 		.id = V4L2_CID_MPEG_VIDEO_MV_V_SEARCH_RANGE,
232 		.type = V4L2_CTRL_TYPE_INTEGER,
233 		.name = "Vertical MV Search Range",
234 		.minimum = 16,
235 		.maximum = 128,
236 		.step = 16,
237 		.default_value = 32,
238 	},
239 	{
240 		.id = V4L2_CID_MPEG_VIDEO_H264_CPB_SIZE,
241 		.type = V4L2_CTRL_TYPE_INTEGER,
242 		.minimum = 0,
243 		.maximum = (1 << 16) - 1,
244 		.step = 1,
245 		.default_value = 0,
246 	},
247 	{
248 		.id = V4L2_CID_MPEG_VIDEO_HEADER_MODE,
249 		.type = V4L2_CTRL_TYPE_MENU,
250 		.minimum = V4L2_MPEG_VIDEO_HEADER_MODE_SEPARATE,
251 		.maximum = V4L2_MPEG_VIDEO_HEADER_MODE_JOINED_WITH_1ST_FRAME,
252 		.default_value = V4L2_MPEG_VIDEO_HEADER_MODE_SEPARATE,
253 		.menu_skip_mask = 0,
254 	},
255 	{
256 		.id = V4L2_CID_MPEG_MFC51_VIDEO_FRAME_SKIP_MODE,
257 		.type = V4L2_CTRL_TYPE_MENU,
258 		.name = "Frame Skip Enable",
259 		.minimum = V4L2_MPEG_MFC51_VIDEO_FRAME_SKIP_MODE_DISABLED,
260 		.maximum = V4L2_MPEG_MFC51_VIDEO_FRAME_SKIP_MODE_BUF_LIMIT,
261 		.menu_skip_mask = 0,
262 		.default_value = V4L2_MPEG_MFC51_VIDEO_FRAME_SKIP_MODE_DISABLED,
263 	},
264 	{
265 		.id = V4L2_CID_MPEG_VIDEO_FRAME_SKIP_MODE,
266 		.type = V4L2_CTRL_TYPE_MENU,
267 		.maximum = V4L2_MPEG_VIDEO_FRAME_SKIP_MODE_BUF_LIMIT,
268 		.default_value = V4L2_MPEG_VIDEO_FRAME_SKIP_MODE_DISABLED,
269 	},
270 	{
271 		.id = V4L2_CID_MPEG_MFC51_VIDEO_RC_FIXED_TARGET_BIT,
272 		.type = V4L2_CTRL_TYPE_BOOLEAN,
273 		.name = "Fixed Target Bit Enable",
274 		.minimum = 0,
275 		.maximum = 1,
276 		.default_value = 0,
277 		.step = 1,
278 		.menu_skip_mask = 0,
279 	},
280 	{
281 		.id = V4L2_CID_MPEG_VIDEO_B_FRAMES,
282 		.type = V4L2_CTRL_TYPE_INTEGER,
283 		.minimum = 0,
284 		.maximum = 2,
285 		.step = 1,
286 		.default_value = 0,
287 	},
288 	{
289 		.id = V4L2_CID_MPEG_VIDEO_H264_PROFILE,
290 		.type = V4L2_CTRL_TYPE_MENU,
291 		.minimum = V4L2_MPEG_VIDEO_H264_PROFILE_BASELINE,
292 		.maximum = V4L2_MPEG_VIDEO_H264_PROFILE_MULTIVIEW_HIGH,
293 		.default_value = V4L2_MPEG_VIDEO_H264_PROFILE_BASELINE,
294 		.menu_skip_mask = ~(
295 				(1 << V4L2_MPEG_VIDEO_H264_PROFILE_BASELINE) |
296 				(1 << V4L2_MPEG_VIDEO_H264_PROFILE_MAIN) |
297 				(1 << V4L2_MPEG_VIDEO_H264_PROFILE_HIGH)
298 				),
299 	},
300 	{
301 		.id = V4L2_CID_MPEG_VIDEO_H264_LEVEL,
302 		.type = V4L2_CTRL_TYPE_MENU,
303 		.minimum = V4L2_MPEG_VIDEO_H264_LEVEL_1_0,
304 		.maximum = V4L2_MPEG_VIDEO_H264_LEVEL_4_0,
305 		.default_value = V4L2_MPEG_VIDEO_H264_LEVEL_1_0,
306 	},
307 	{
308 		.id = V4L2_CID_MPEG_VIDEO_MPEG4_LEVEL,
309 		.type = V4L2_CTRL_TYPE_MENU,
310 		.minimum = V4L2_MPEG_VIDEO_MPEG4_LEVEL_0,
311 		.maximum = V4L2_MPEG_VIDEO_MPEG4_LEVEL_5,
312 		.default_value = V4L2_MPEG_VIDEO_MPEG4_LEVEL_0,
313 		.menu_skip_mask = 0,
314 	},
315 	{
316 		.id = V4L2_CID_MPEG_VIDEO_H264_LOOP_FILTER_MODE,
317 		.type = V4L2_CTRL_TYPE_MENU,
318 		.minimum = V4L2_MPEG_VIDEO_H264_LOOP_FILTER_MODE_ENABLED,
319 		.maximum = V4L2_MPEG_VIDEO_H264_LOOP_FILTER_MODE_DISABLED_AT_SLICE_BOUNDARY,
320 		.default_value = V4L2_MPEG_VIDEO_H264_LOOP_FILTER_MODE_ENABLED,
321 		.menu_skip_mask = 0,
322 	},
323 	{
324 		.id = V4L2_CID_MPEG_VIDEO_H264_LOOP_FILTER_ALPHA,
325 		.type = V4L2_CTRL_TYPE_INTEGER,
326 		.minimum = -6,
327 		.maximum = 6,
328 		.step = 1,
329 		.default_value = 0,
330 	},
331 	{
332 		.id = V4L2_CID_MPEG_VIDEO_H264_LOOP_FILTER_BETA,
333 		.type = V4L2_CTRL_TYPE_INTEGER,
334 		.minimum = -6,
335 		.maximum = 6,
336 		.step = 1,
337 		.default_value = 0,
338 	},
339 	{
340 		.id = V4L2_CID_MPEG_VIDEO_H264_ENTROPY_MODE,
341 		.type = V4L2_CTRL_TYPE_MENU,
342 		.minimum = V4L2_MPEG_VIDEO_H264_ENTROPY_MODE_CAVLC,
343 		.maximum = V4L2_MPEG_VIDEO_H264_ENTROPY_MODE_CABAC,
344 		.default_value = V4L2_MPEG_VIDEO_H264_ENTROPY_MODE_CAVLC,
345 		.menu_skip_mask = 0,
346 	},
347 	{
348 		.id = V4L2_CID_MPEG_MFC51_VIDEO_H264_NUM_REF_PIC_FOR_P,
349 		.type = V4L2_CTRL_TYPE_INTEGER,
350 		.name = "The Number of Ref. Pic for P",
351 		.minimum = 1,
352 		.maximum = 2,
353 		.step = 1,
354 		.default_value = 1,
355 	},
356 	{
357 		.id = V4L2_CID_MPEG_VIDEO_H264_8X8_TRANSFORM,
358 		.type = V4L2_CTRL_TYPE_BOOLEAN,
359 		.minimum = 0,
360 		.maximum = 1,
361 		.step = 1,
362 		.default_value = 0,
363 	},
364 	{
365 		.id = V4L2_CID_MPEG_VIDEO_MB_RC_ENABLE,
366 		.type = V4L2_CTRL_TYPE_BOOLEAN,
367 		.minimum = 0,
368 		.maximum = 1,
369 		.step = 1,
370 		.default_value = 0,
371 	},
372 	{
373 		.id = V4L2_CID_MPEG_VIDEO_H264_I_FRAME_QP,
374 		.type = V4L2_CTRL_TYPE_INTEGER,
375 		.minimum = 0,
376 		.maximum = 51,
377 		.step = 1,
378 		.default_value = 1,
379 	},
380 	{
381 		.id = V4L2_CID_MPEG_VIDEO_H264_MIN_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_MAX_QP,
390 		.type = V4L2_CTRL_TYPE_INTEGER,
391 		.minimum = 0,
392 		.maximum = 51,
393 		.step = 1,
394 		.default_value = 51,
395 	},
396 	{
397 		.id = V4L2_CID_MPEG_VIDEO_H264_P_FRAME_QP,
398 		.type = V4L2_CTRL_TYPE_INTEGER,
399 		.minimum = 0,
400 		.maximum = 51,
401 		.step = 1,
402 		.default_value = 1,
403 	},
404 	{
405 		.id = V4L2_CID_MPEG_VIDEO_H264_B_FRAME_QP,
406 		.type = V4L2_CTRL_TYPE_INTEGER,
407 		.minimum = 0,
408 		.maximum = 51,
409 		.step = 1,
410 		.default_value = 1,
411 	},
412 	{
413 		.id = V4L2_CID_MPEG_VIDEO_H263_I_FRAME_QP,
414 		.type = V4L2_CTRL_TYPE_INTEGER,
415 		.name = "H263 I-Frame QP value",
416 		.minimum = 1,
417 		.maximum = 31,
418 		.step = 1,
419 		.default_value = 1,
420 	},
421 	{
422 		.id = V4L2_CID_MPEG_VIDEO_H263_MIN_QP,
423 		.type = V4L2_CTRL_TYPE_INTEGER,
424 		.name = "H263 Minimum QP value",
425 		.minimum = 1,
426 		.maximum = 31,
427 		.step = 1,
428 		.default_value = 1,
429 	},
430 	{
431 		.id = V4L2_CID_MPEG_VIDEO_H263_MAX_QP,
432 		.type = V4L2_CTRL_TYPE_INTEGER,
433 		.name = "H263 Maximum QP value",
434 		.minimum = 1,
435 		.maximum = 31,
436 		.step = 1,
437 		.default_value = 31,
438 	},
439 	{
440 		.id = V4L2_CID_MPEG_VIDEO_H263_P_FRAME_QP,
441 		.type = V4L2_CTRL_TYPE_INTEGER,
442 		.name = "H263 P frame QP value",
443 		.minimum = 1,
444 		.maximum = 31,
445 		.step = 1,
446 		.default_value = 1,
447 	},
448 	{
449 		.id = V4L2_CID_MPEG_VIDEO_H263_B_FRAME_QP,
450 		.type = V4L2_CTRL_TYPE_INTEGER,
451 		.name = "H263 B frame QP value",
452 		.minimum = 1,
453 		.maximum = 31,
454 		.step = 1,
455 		.default_value = 1,
456 	},
457 	{
458 		.id = V4L2_CID_MPEG_VIDEO_MPEG4_I_FRAME_QP,
459 		.type = V4L2_CTRL_TYPE_INTEGER,
460 		.name = "MPEG4 I-Frame QP value",
461 		.minimum = 1,
462 		.maximum = 31,
463 		.step = 1,
464 		.default_value = 1,
465 	},
466 	{
467 		.id = V4L2_CID_MPEG_VIDEO_MPEG4_MIN_QP,
468 		.type = V4L2_CTRL_TYPE_INTEGER,
469 		.name = "MPEG4 Minimum QP value",
470 		.minimum = 1,
471 		.maximum = 31,
472 		.step = 1,
473 		.default_value = 1,
474 	},
475 	{
476 		.id = V4L2_CID_MPEG_VIDEO_MPEG4_MAX_QP,
477 		.type = V4L2_CTRL_TYPE_INTEGER,
478 		.name = "MPEG4 Maximum QP value",
479 		.minimum = 0,
480 		.maximum = 51,
481 		.step = 1,
482 		.default_value = 51,
483 	},
484 	{
485 		.id = V4L2_CID_MPEG_VIDEO_MPEG4_P_FRAME_QP,
486 		.type = V4L2_CTRL_TYPE_INTEGER,
487 		.name = "MPEG4 P frame QP value",
488 		.minimum = 1,
489 		.maximum = 31,
490 		.step = 1,
491 		.default_value = 1,
492 	},
493 	{
494 		.id = V4L2_CID_MPEG_VIDEO_MPEG4_B_FRAME_QP,
495 		.type = V4L2_CTRL_TYPE_INTEGER,
496 		.name = "MPEG4 B frame QP value",
497 		.minimum = 1,
498 		.maximum = 31,
499 		.step = 1,
500 		.default_value = 1,
501 	},
502 	{
503 		.id = V4L2_CID_MPEG_MFC51_VIDEO_H264_ADAPTIVE_RC_DARK,
504 		.type = V4L2_CTRL_TYPE_BOOLEAN,
505 		.name = "H264 Dark Reg Adaptive RC",
506 		.minimum = 0,
507 		.maximum = 1,
508 		.step = 1,
509 		.default_value = 0,
510 	},
511 	{
512 		.id = V4L2_CID_MPEG_MFC51_VIDEO_H264_ADAPTIVE_RC_SMOOTH,
513 		.type = V4L2_CTRL_TYPE_BOOLEAN,
514 		.name = "H264 Smooth Reg Adaptive RC",
515 		.minimum = 0,
516 		.maximum = 1,
517 		.step = 1,
518 		.default_value = 0,
519 	},
520 	{
521 		.id = V4L2_CID_MPEG_MFC51_VIDEO_H264_ADAPTIVE_RC_STATIC,
522 		.type = V4L2_CTRL_TYPE_BOOLEAN,
523 		.name = "H264 Static Reg Adaptive RC",
524 		.minimum = 0,
525 		.maximum = 1,
526 		.step = 1,
527 		.default_value = 0,
528 	},
529 	{
530 		.id = V4L2_CID_MPEG_MFC51_VIDEO_H264_ADAPTIVE_RC_ACTIVITY,
531 		.type = V4L2_CTRL_TYPE_BOOLEAN,
532 		.name = "H264 Activity Reg Adaptive RC",
533 		.minimum = 0,
534 		.maximum = 1,
535 		.step = 1,
536 		.default_value = 0,
537 	},
538 	{
539 		.id = V4L2_CID_MPEG_VIDEO_H264_VUI_SAR_ENABLE,
540 		.type = V4L2_CTRL_TYPE_BOOLEAN,
541 		.minimum = 0,
542 		.maximum = 1,
543 		.step = 1,
544 		.default_value = 0,
545 	},
546 	{
547 		.id = V4L2_CID_MPEG_VIDEO_H264_VUI_SAR_IDC,
548 		.type = V4L2_CTRL_TYPE_MENU,
549 		.minimum = V4L2_MPEG_VIDEO_H264_VUI_SAR_IDC_UNSPECIFIED,
550 		.maximum = V4L2_MPEG_VIDEO_H264_VUI_SAR_IDC_EXTENDED,
551 		.default_value = V4L2_MPEG_VIDEO_H264_VUI_SAR_IDC_UNSPECIFIED,
552 		.menu_skip_mask = 0,
553 	},
554 	{
555 		.id = V4L2_CID_MPEG_VIDEO_H264_VUI_EXT_SAR_WIDTH,
556 		.type = V4L2_CTRL_TYPE_INTEGER,
557 		.minimum = 0,
558 		.maximum = (1 << 16) - 1,
559 		.step = 1,
560 		.default_value = 0,
561 	},
562 	{
563 		.id = V4L2_CID_MPEG_VIDEO_H264_VUI_EXT_SAR_HEIGHT,
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_GOP_CLOSURE,
572 		.type = V4L2_CTRL_TYPE_BOOLEAN,
573 		.minimum = 0,
574 		.maximum = 1,
575 		.step = 1,
576 		.default_value = 1,
577 	},
578 	{
579 		.id = V4L2_CID_MPEG_VIDEO_H264_I_PERIOD,
580 		.type = V4L2_CTRL_TYPE_INTEGER,
581 		.minimum = 0,
582 		.maximum = (1 << 16) - 1,
583 		.step = 1,
584 		.default_value = 0,
585 	},
586 	{
587 		.id = V4L2_CID_MPEG_VIDEO_MPEG4_PROFILE,
588 		.type = V4L2_CTRL_TYPE_MENU,
589 		.minimum = V4L2_MPEG_VIDEO_MPEG4_PROFILE_SIMPLE,
590 		.maximum = V4L2_MPEG_VIDEO_MPEG4_PROFILE_ADVANCED_SIMPLE,
591 		.default_value = V4L2_MPEG_VIDEO_MPEG4_PROFILE_SIMPLE,
592 		.menu_skip_mask = 0,
593 	},
594 	{
595 		.id = V4L2_CID_MPEG_VIDEO_MPEG4_QPEL,
596 		.type = V4L2_CTRL_TYPE_BOOLEAN,
597 		.minimum = 0,
598 		.maximum = 1,
599 		.step = 1,
600 		.default_value = 0,
601 	},
602 	{
603 		.id = V4L2_CID_MPEG_VIDEO_VPX_NUM_PARTITIONS,
604 		.type = V4L2_CTRL_TYPE_INTEGER_MENU,
605 		.maximum = V4L2_CID_MPEG_VIDEO_VPX_8_PARTITIONS,
606 		.default_value = V4L2_CID_MPEG_VIDEO_VPX_1_PARTITION,
607 		.menu_skip_mask = 0,
608 	},
609 	{
610 		.id = V4L2_CID_MPEG_VIDEO_VPX_IMD_DISABLE_4X4,
611 		.type = V4L2_CTRL_TYPE_BOOLEAN,
612 		.minimum = 0,
613 		.maximum = 1,
614 		.step = 1,
615 		.default_value = 0,
616 	},
617 	{
618 		.id = V4L2_CID_MPEG_VIDEO_VPX_NUM_REF_FRAMES,
619 		.type = V4L2_CTRL_TYPE_INTEGER_MENU,
620 		.maximum = V4L2_CID_MPEG_VIDEO_VPX_2_REF_FRAME,
621 		.default_value = V4L2_CID_MPEG_VIDEO_VPX_1_REF_FRAME,
622 		.menu_skip_mask = 0,
623 	},
624 	{
625 		.id = V4L2_CID_MPEG_VIDEO_VPX_FILTER_LEVEL,
626 		.type = V4L2_CTRL_TYPE_INTEGER,
627 		.minimum = 0,
628 		.maximum = 63,
629 		.step = 1,
630 		.default_value = 0,
631 	},
632 	{
633 		.id = V4L2_CID_MPEG_VIDEO_VPX_FILTER_SHARPNESS,
634 		.type = V4L2_CTRL_TYPE_INTEGER,
635 		.minimum = 0,
636 		.maximum = 7,
637 		.step = 1,
638 		.default_value = 0,
639 	},
640 	{
641 		.id = V4L2_CID_MPEG_VIDEO_VPX_GOLDEN_FRAME_REF_PERIOD,
642 		.type = V4L2_CTRL_TYPE_INTEGER,
643 		.minimum = 0,
644 		.maximum = (1 << 16) - 1,
645 		.step = 1,
646 		.default_value = 0,
647 	},
648 	{
649 		.id = V4L2_CID_MPEG_VIDEO_VPX_GOLDEN_FRAME_SEL,
650 		.type = V4L2_CTRL_TYPE_MENU,
651 		.minimum = V4L2_CID_MPEG_VIDEO_VPX_GOLDEN_FRAME_USE_PREV,
652 		.maximum = V4L2_CID_MPEG_VIDEO_VPX_GOLDEN_FRAME_USE_REF_PERIOD,
653 		.default_value = V4L2_CID_MPEG_VIDEO_VPX_GOLDEN_FRAME_USE_PREV,
654 		.menu_skip_mask = 0,
655 	},
656 	{
657 		.id = V4L2_CID_MPEG_VIDEO_VPX_MAX_QP,
658 		.type = V4L2_CTRL_TYPE_INTEGER,
659 		.minimum = 0,
660 		.maximum = 127,
661 		.step = 1,
662 		.default_value = 127,
663 	},
664 	{
665 		.id = V4L2_CID_MPEG_VIDEO_VPX_MIN_QP,
666 		.type = V4L2_CTRL_TYPE_INTEGER,
667 		.minimum = 0,
668 		.maximum = 11,
669 		.step = 1,
670 		.default_value = 0,
671 	},
672 	{
673 		.id = V4L2_CID_MPEG_VIDEO_VPX_I_FRAME_QP,
674 		.type = V4L2_CTRL_TYPE_INTEGER,
675 		.minimum = 0,
676 		.maximum = 127,
677 		.step = 1,
678 		.default_value = 10,
679 	},
680 	{
681 		.id = V4L2_CID_MPEG_VIDEO_VPX_P_FRAME_QP,
682 		.type = V4L2_CTRL_TYPE_INTEGER,
683 		.minimum = 0,
684 		.maximum = 127,
685 		.step = 1,
686 		.default_value = 10,
687 	},
688 	{
689 		.id = V4L2_CID_MPEG_VIDEO_VP8_PROFILE,
690 		.type = V4L2_CTRL_TYPE_MENU,
691 		.minimum = V4L2_MPEG_VIDEO_VP8_PROFILE_0,
692 		.maximum = V4L2_MPEG_VIDEO_VP8_PROFILE_3,
693 		.default_value = V4L2_MPEG_VIDEO_VP8_PROFILE_0,
694 		.menu_skip_mask = 0,
695 	},
696 	{
697 		.id = V4L2_CID_MPEG_VIDEO_HEVC_I_FRAME_QP,
698 		.type = V4L2_CTRL_TYPE_INTEGER,
699 		.name = "HEVC I Frame QP Value",
700 		.minimum = 0,
701 		.maximum = 51,
702 		.step = 1,
703 		.default_value = 0,
704 	},
705 	{
706 		.id = V4L2_CID_MPEG_VIDEO_HEVC_P_FRAME_QP,
707 		.type = V4L2_CTRL_TYPE_INTEGER,
708 		.name = "HEVC P Frame QP Value",
709 		.minimum = 0,
710 		.maximum = 51,
711 		.step = 1,
712 		.default_value = 0,
713 	},
714 	{
715 		.id = V4L2_CID_MPEG_VIDEO_HEVC_B_FRAME_QP,
716 		.type = V4L2_CTRL_TYPE_INTEGER,
717 		.minimum = 0,
718 		.maximum = 51,
719 		.step = 1,
720 		.default_value = 0,
721 	},
722 	{
723 		.id = V4L2_CID_MPEG_VIDEO_HEVC_MIN_QP,
724 		.type = V4L2_CTRL_TYPE_INTEGER,
725 		.minimum = 0,
726 		.maximum = 51,
727 		.step = 1,
728 		.default_value = 0,
729 	},
730 	{
731 		.id = V4L2_CID_MPEG_VIDEO_HEVC_MAX_QP,
732 		.type = V4L2_CTRL_TYPE_INTEGER,
733 		.minimum = 0,
734 		.maximum = 51,
735 		.step = 1,
736 		.default_value = 0,
737 	},
738 	{
739 		.id = V4L2_CID_MPEG_VIDEO_HEVC_PROFILE,
740 		.type = V4L2_CTRL_TYPE_MENU,
741 		.minimum = V4L2_MPEG_VIDEO_HEVC_PROFILE_MAIN,
742 		.maximum = V4L2_MPEG_VIDEO_HEVC_PROFILE_MAIN_STILL_PICTURE,
743 		.step = 1,
744 		.default_value = V4L2_MPEG_VIDEO_HEVC_PROFILE_MAIN,
745 	},
746 	{
747 		.id = V4L2_CID_MPEG_VIDEO_HEVC_LEVEL,
748 		.type = V4L2_CTRL_TYPE_MENU,
749 		.minimum = V4L2_MPEG_VIDEO_HEVC_LEVEL_1,
750 		.maximum = V4L2_MPEG_VIDEO_HEVC_LEVEL_6_2,
751 		.step = 1,
752 		.default_value = V4L2_MPEG_VIDEO_HEVC_LEVEL_1,
753 	},
754 	{
755 		.id = V4L2_CID_MPEG_VIDEO_HEVC_TIER,
756 		.type = V4L2_CTRL_TYPE_MENU,
757 		.minimum = V4L2_MPEG_VIDEO_HEVC_TIER_MAIN,
758 		.maximum = V4L2_MPEG_VIDEO_HEVC_TIER_HIGH,
759 		.step = 1,
760 		.default_value = V4L2_MPEG_VIDEO_HEVC_TIER_MAIN,
761 	},
762 	{
763 		.id = V4L2_CID_MPEG_VIDEO_HEVC_FRAME_RATE_RESOLUTION,
764 		.type = V4L2_CTRL_TYPE_INTEGER,
765 		.minimum = 1,
766 		.maximum = (1 << 16) - 1,
767 		.step = 1,
768 		.default_value = 1,
769 	},
770 	{
771 		.id = V4L2_CID_MPEG_VIDEO_HEVC_MAX_PARTITION_DEPTH,
772 		.type = V4L2_CTRL_TYPE_INTEGER,
773 		.minimum = 0,
774 		.maximum = 1,
775 		.step = 1,
776 		.default_value = 0,
777 	},
778 	{
779 		.id = V4L2_CID_MPEG_VIDEO_REF_NUMBER_FOR_PFRAMES,
780 		.type = V4L2_CTRL_TYPE_INTEGER,
781 		.minimum = 1,
782 		.maximum = 2,
783 		.step = 1,
784 		.default_value = 1,
785 	},
786 	{
787 		.id = V4L2_CID_MPEG_VIDEO_HEVC_REFRESH_TYPE,
788 		.type = V4L2_CTRL_TYPE_MENU,
789 		.minimum = V4L2_MPEG_VIDEO_HEVC_REFRESH_NONE,
790 		.maximum = V4L2_MPEG_VIDEO_HEVC_REFRESH_IDR,
791 		.step = 1,
792 		.default_value = V4L2_MPEG_VIDEO_HEVC_REFRESH_NONE,
793 	},
794 	{
795 		.id = V4L2_CID_MPEG_VIDEO_HEVC_CONST_INTRA_PRED,
796 		.type = V4L2_CTRL_TYPE_BOOLEAN,
797 		.minimum = 0,
798 		.maximum = 1,
799 		.step = 1,
800 		.default_value = 0,
801 	},
802 	{
803 		.id = V4L2_CID_MPEG_VIDEO_HEVC_LOSSLESS_CU,
804 		.type = V4L2_CTRL_TYPE_BOOLEAN,
805 		.minimum = 0,
806 		.maximum = 1,
807 		.step = 1,
808 		.default_value = 0,
809 	},
810 	{
811 		.id = V4L2_CID_MPEG_VIDEO_HEVC_WAVEFRONT,
812 		.type = V4L2_CTRL_TYPE_BOOLEAN,
813 		.minimum = 0,
814 		.maximum = 1,
815 		.step = 1,
816 		.default_value = 0,
817 	},
818 	{
819 		.id = V4L2_CID_MPEG_VIDEO_HEVC_LOOP_FILTER_MODE,
820 		.type = V4L2_CTRL_TYPE_MENU,
821 		.minimum = V4L2_MPEG_VIDEO_HEVC_LOOP_FILTER_MODE_DISABLED,
822 		.maximum = V4L2_MPEG_VIDEO_HEVC_LOOP_FILTER_MODE_DISABLED_AT_SLICE_BOUNDARY,
823 		.step = 1,
824 		.default_value = V4L2_MPEG_VIDEO_HEVC_LOOP_FILTER_MODE_DISABLED,
825 	},
826 	{
827 		.id = V4L2_CID_MPEG_VIDEO_HEVC_HIER_QP,
828 		.type = V4L2_CTRL_TYPE_BOOLEAN,
829 		.minimum = 0,
830 		.maximum = 1,
831 		.step = 1,
832 		.default_value = 0,
833 	},
834 	{
835 		.id = V4L2_CID_MPEG_VIDEO_HEVC_HIER_CODING_TYPE,
836 		.type = V4L2_CTRL_TYPE_MENU,
837 		.minimum = V4L2_MPEG_VIDEO_HEVC_HIERARCHICAL_CODING_B,
838 		.maximum = V4L2_MPEG_VIDEO_HEVC_HIERARCHICAL_CODING_P,
839 		.step = 1,
840 		.default_value = V4L2_MPEG_VIDEO_HEVC_HIERARCHICAL_CODING_B,
841 	},
842 	{
843 		.id = V4L2_CID_MPEG_VIDEO_HEVC_HIER_CODING_LAYER,
844 		.type = V4L2_CTRL_TYPE_INTEGER,
845 		.minimum = 0,
846 		.maximum = 6,
847 		.step = 1,
848 		.default_value = 0,
849 	},
850 	{
851 		.id = V4L2_CID_MPEG_VIDEO_HEVC_HIER_CODING_L0_QP,
852 		.type = V4L2_CTRL_TYPE_INTEGER,
853 		.minimum = 0,
854 		.maximum = 51,
855 		.step = 1,
856 		.default_value = 0,
857 	},
858 	{
859 		.id = V4L2_CID_MPEG_VIDEO_HEVC_HIER_CODING_L1_QP,
860 		.type = V4L2_CTRL_TYPE_INTEGER,
861 		.minimum = 0,
862 		.maximum = 51,
863 		.step = 1,
864 		.default_value = 0,
865 	},
866 	{
867 		.id = V4L2_CID_MPEG_VIDEO_HEVC_HIER_CODING_L2_QP,
868 		.type = V4L2_CTRL_TYPE_INTEGER,
869 		.minimum = 0,
870 		.maximum = 51,
871 		.step = 1,
872 		.default_value = 0,
873 	},
874 	{
875 		.id = V4L2_CID_MPEG_VIDEO_HEVC_HIER_CODING_L3_QP,
876 		.type = V4L2_CTRL_TYPE_INTEGER,
877 		.minimum = 0,
878 		.maximum = 51,
879 		.step = 1,
880 		.default_value = 0,
881 	},
882 	{
883 		.id = V4L2_CID_MPEG_VIDEO_HEVC_HIER_CODING_L4_QP,
884 		.type = V4L2_CTRL_TYPE_INTEGER,
885 		.minimum = 0,
886 		.maximum = 51,
887 		.step = 1,
888 		.default_value = 0,
889 	},
890 	{
891 		.id = V4L2_CID_MPEG_VIDEO_HEVC_HIER_CODING_L5_QP,
892 		.type = V4L2_CTRL_TYPE_INTEGER,
893 		.minimum = 0,
894 		.maximum = 51,
895 		.step = 1,
896 		.default_value = 0,
897 	},
898 	{
899 		.id = V4L2_CID_MPEG_VIDEO_HEVC_HIER_CODING_L6_QP,
900 		.type = V4L2_CTRL_TYPE_INTEGER,
901 		.minimum = 0,
902 		.maximum = 51,
903 		.step = 1,
904 		.default_value = 0,
905 	},
906 	{
907 		.id = V4L2_CID_MPEG_VIDEO_HEVC_HIER_CODING_L0_BR,
908 		.type = V4L2_CTRL_TYPE_INTEGER,
909 		.minimum = INT_MIN,
910 		.maximum = INT_MAX,
911 		.step = 1,
912 		.default_value = 0,
913 	},
914 	{
915 		.id = V4L2_CID_MPEG_VIDEO_HEVC_HIER_CODING_L1_BR,
916 		.type = V4L2_CTRL_TYPE_INTEGER,
917 		.minimum = INT_MIN,
918 		.maximum = INT_MAX,
919 		.step = 1,
920 		.default_value = 0,
921 	},
922 	{
923 		.id = V4L2_CID_MPEG_VIDEO_HEVC_HIER_CODING_L2_BR,
924 		.type = V4L2_CTRL_TYPE_INTEGER,
925 		.minimum = INT_MIN,
926 		.maximum = INT_MAX,
927 		.step = 1,
928 		.default_value = 0,
929 	},
930 	{
931 		.id = V4L2_CID_MPEG_VIDEO_HEVC_HIER_CODING_L3_BR,
932 		.type = V4L2_CTRL_TYPE_INTEGER,
933 		.minimum = INT_MIN,
934 		.maximum = INT_MAX,
935 		.step = 1,
936 		.default_value = 0,
937 	},
938 	{
939 		.id = V4L2_CID_MPEG_VIDEO_HEVC_HIER_CODING_L4_BR,
940 		.type = V4L2_CTRL_TYPE_INTEGER,
941 		.minimum = INT_MIN,
942 		.maximum = INT_MAX,
943 		.step = 1,
944 		.default_value = 0,
945 	},
946 	{
947 		.id = V4L2_CID_MPEG_VIDEO_HEVC_HIER_CODING_L5_BR,
948 		.type = V4L2_CTRL_TYPE_INTEGER,
949 		.minimum = INT_MIN,
950 		.maximum = INT_MAX,
951 		.step = 1,
952 		.default_value = 0,
953 	},
954 	{
955 		.id = V4L2_CID_MPEG_VIDEO_HEVC_HIER_CODING_L6_BR,
956 		.type = V4L2_CTRL_TYPE_INTEGER,
957 		.minimum = INT_MIN,
958 		.maximum = INT_MAX,
959 		.step = 1,
960 		.default_value = 0,
961 	},
962 	{
963 		.id = V4L2_CID_MPEG_VIDEO_HEVC_GENERAL_PB,
964 		.type = V4L2_CTRL_TYPE_BOOLEAN,
965 		.minimum = 0,
966 		.maximum = 1,
967 		.step = 1,
968 		.default_value = 0,
969 	},
970 	{
971 		.id = V4L2_CID_MPEG_VIDEO_HEVC_TEMPORAL_ID,
972 		.type = V4L2_CTRL_TYPE_BOOLEAN,
973 		.minimum = 0,
974 		.maximum = 1,
975 		.step = 1,
976 		.default_value = 0,
977 	},
978 	{
979 		.id = V4L2_CID_MPEG_VIDEO_HEVC_STRONG_SMOOTHING,
980 		.type = V4L2_CTRL_TYPE_BOOLEAN,
981 		.minimum = 0,
982 		.maximum = 1,
983 		.step = 1,
984 		.default_value = 0,
985 	},
986 	{
987 		.id = V4L2_CID_MPEG_VIDEO_HEVC_INTRA_PU_SPLIT,
988 		.type = V4L2_CTRL_TYPE_BOOLEAN,
989 		.minimum = 0,
990 		.maximum = 1,
991 		.step = 1,
992 		.default_value = 0,
993 	},
994 	{
995 		.id = V4L2_CID_MPEG_VIDEO_HEVC_TMV_PREDICTION,
996 		.type = V4L2_CTRL_TYPE_BOOLEAN,
997 		.minimum = 0,
998 		.maximum = 1,
999 		.step = 1,
1000 		.default_value = 0,
1001 	},
1002 	{
1003 		.id = V4L2_CID_MPEG_VIDEO_HEVC_MAX_NUM_MERGE_MV_MINUS1,
1004 		.type = V4L2_CTRL_TYPE_INTEGER,
1005 		.minimum = 0,
1006 		.maximum = 4,
1007 		.step = 1,
1008 		.default_value = 0,
1009 	},
1010 	{
1011 		.id = V4L2_CID_MPEG_VIDEO_HEVC_WITHOUT_STARTCODE,
1012 		.type = V4L2_CTRL_TYPE_BOOLEAN,
1013 		.minimum = 0,
1014 		.maximum = 1,
1015 		.step = 1,
1016 		.default_value = 0,
1017 	},
1018 	{
1019 		.id = V4L2_CID_MPEG_VIDEO_HEVC_REFRESH_PERIOD,
1020 		.type = V4L2_CTRL_TYPE_INTEGER,
1021 		.minimum = 0,
1022 		.maximum = (1 << 16) - 1,
1023 		.step = 1,
1024 		.default_value = 0,
1025 	},
1026 	{
1027 		.id = V4L2_CID_MPEG_VIDEO_HEVC_LF_BETA_OFFSET_DIV2,
1028 		.type = V4L2_CTRL_TYPE_INTEGER,
1029 		.minimum = -6,
1030 		.maximum = 6,
1031 		.step = 1,
1032 		.default_value = 0,
1033 	},
1034 	{
1035 		.id = V4L2_CID_MPEG_VIDEO_HEVC_LF_TC_OFFSET_DIV2,
1036 		.type = V4L2_CTRL_TYPE_INTEGER,
1037 		.minimum = -6,
1038 		.maximum = 6,
1039 		.step = 1,
1040 		.default_value = 0,
1041 	},
1042 	{
1043 		.id = V4L2_CID_MPEG_VIDEO_HEVC_SIZE_OF_LENGTH_FIELD,
1044 		.type = V4L2_CTRL_TYPE_MENU,
1045 		.minimum = V4L2_MPEG_VIDEO_HEVC_SIZE_0,
1046 		.maximum = V4L2_MPEG_VIDEO_HEVC_SIZE_4,
1047 		.step = 1,
1048 		.default_value = V4L2_MPEG_VIDEO_HEVC_SIZE_0,
1049 	},
1050 	{
1051 		.id = V4L2_CID_MPEG_VIDEO_PREPEND_SPSPPS_TO_IDR,
1052 		.type = V4L2_CTRL_TYPE_INTEGER,
1053 		.minimum = 0,
1054 		.maximum = 1,
1055 		.step = 1,
1056 		.default_value = 0,
1057 	},
1058 	{
1059 		.id = V4L2_CID_MIN_BUFFERS_FOR_OUTPUT,
1060 		.type = V4L2_CTRL_TYPE_INTEGER,
1061 		.name = "Minimum number of output bufs",
1062 		.minimum = 1,
1063 		.maximum = 32,
1064 		.step = 1,
1065 		.default_value = 1,
1066 		.is_volatile = 1,
1067 	},
1068 };
1069 
1070 #define NUM_CTRLS ARRAY_SIZE(controls)
1071 static const char * const *mfc51_get_menu(u32 id)
1072 {
1073 	static const char * const mfc51_video_frame_skip[] = {
1074 		"Disabled",
1075 		"Level Limit",
1076 		"VBV/CPB Limit",
1077 		NULL,
1078 	};
1079 	static const char * const mfc51_video_force_frame[] = {
1080 		"Disabled",
1081 		"I Frame",
1082 		"Not Coded",
1083 		NULL,
1084 	};
1085 	switch (id) {
1086 	case V4L2_CID_MPEG_MFC51_VIDEO_FRAME_SKIP_MODE:
1087 		return mfc51_video_frame_skip;
1088 	case V4L2_CID_MPEG_MFC51_VIDEO_FORCE_FRAME_TYPE:
1089 		return mfc51_video_force_frame;
1090 	}
1091 	return NULL;
1092 }
1093 
1094 static int s5p_mfc_ctx_ready(struct s5p_mfc_ctx *ctx)
1095 {
1096 	mfc_debug(2, "src=%d, dst=%d, state=%d\n",
1097 		  ctx->src_queue_cnt, ctx->dst_queue_cnt, ctx->state);
1098 	/* context is ready to make header */
1099 	if (ctx->state == MFCINST_GOT_INST && ctx->dst_queue_cnt >= 1)
1100 		return 1;
1101 	/* context is ready to encode a frame */
1102 	if ((ctx->state == MFCINST_RUNNING ||
1103 		ctx->state == MFCINST_HEAD_PRODUCED) &&
1104 		ctx->src_queue_cnt >= 1 && ctx->dst_queue_cnt >= 1)
1105 		return 1;
1106 	/* context is ready to encode remaining frames */
1107 	if (ctx->state == MFCINST_FINISHING &&
1108 		ctx->dst_queue_cnt >= 1)
1109 		return 1;
1110 	mfc_debug(2, "ctx is not ready\n");
1111 	return 0;
1112 }
1113 
1114 static void cleanup_ref_queue(struct s5p_mfc_ctx *ctx)
1115 {
1116 	struct s5p_mfc_buf *mb_entry;
1117 
1118 	/* move buffers in ref queue to src queue */
1119 	while (!list_empty(&ctx->ref_queue)) {
1120 		mb_entry = list_entry((&ctx->ref_queue)->next,
1121 						struct s5p_mfc_buf, list);
1122 		list_del(&mb_entry->list);
1123 		ctx->ref_queue_cnt--;
1124 		list_add_tail(&mb_entry->list, &ctx->src_queue);
1125 		ctx->src_queue_cnt++;
1126 	}
1127 	mfc_debug(2, "enc src count: %d, enc ref count: %d\n",
1128 		  ctx->src_queue_cnt, ctx->ref_queue_cnt);
1129 	INIT_LIST_HEAD(&ctx->ref_queue);
1130 	ctx->ref_queue_cnt = 0;
1131 }
1132 
1133 static int enc_pre_seq_start(struct s5p_mfc_ctx *ctx)
1134 {
1135 	struct s5p_mfc_dev *dev = ctx->dev;
1136 	struct s5p_mfc_buf *dst_mb;
1137 	unsigned long dst_addr;
1138 	unsigned int dst_size;
1139 
1140 	dst_mb = list_entry(ctx->dst_queue.next, struct s5p_mfc_buf, list);
1141 	dst_addr = vb2_dma_contig_plane_dma_addr(&dst_mb->b->vb2_buf, 0);
1142 	dst_size = vb2_plane_size(&dst_mb->b->vb2_buf, 0);
1143 	s5p_mfc_hw_call(dev->mfc_ops, set_enc_stream_buffer, ctx, dst_addr,
1144 			dst_size);
1145 	return 0;
1146 }
1147 
1148 static int enc_post_seq_start(struct s5p_mfc_ctx *ctx)
1149 {
1150 	struct s5p_mfc_dev *dev = ctx->dev;
1151 	struct s5p_mfc_enc_params *p = &ctx->enc_params;
1152 	struct s5p_mfc_buf *dst_mb;
1153 	unsigned int enc_pb_count;
1154 
1155 	if (p->seq_hdr_mode == V4L2_MPEG_VIDEO_HEADER_MODE_SEPARATE) {
1156 		if (!list_empty(&ctx->dst_queue)) {
1157 			dst_mb = list_entry(ctx->dst_queue.next,
1158 					struct s5p_mfc_buf, list);
1159 			list_del(&dst_mb->list);
1160 			ctx->dst_queue_cnt--;
1161 			vb2_set_plane_payload(&dst_mb->b->vb2_buf, 0,
1162 				s5p_mfc_hw_call(dev->mfc_ops, get_enc_strm_size,
1163 						dev));
1164 			vb2_buffer_done(&dst_mb->b->vb2_buf,
1165 					VB2_BUF_STATE_DONE);
1166 		}
1167 	}
1168 
1169 	if (!IS_MFCV6_PLUS(dev)) {
1170 		ctx->state = MFCINST_RUNNING;
1171 		if (s5p_mfc_ctx_ready(ctx))
1172 			set_work_bit_irqsave(ctx);
1173 		s5p_mfc_hw_call(dev->mfc_ops, try_run, dev);
1174 	} else {
1175 		enc_pb_count = s5p_mfc_hw_call(dev->mfc_ops,
1176 				get_enc_dpb_count, dev);
1177 		if (ctx->pb_count < enc_pb_count)
1178 			ctx->pb_count = enc_pb_count;
1179 		if (FW_HAS_E_MIN_SCRATCH_BUF(dev)) {
1180 			ctx->scratch_buf_size = s5p_mfc_hw_call(dev->mfc_ops,
1181 					get_e_min_scratch_buf_size, dev);
1182 			ctx->bank1.size += ctx->scratch_buf_size;
1183 		}
1184 		ctx->state = MFCINST_HEAD_PRODUCED;
1185 	}
1186 
1187 	return 0;
1188 }
1189 
1190 static int enc_pre_frame_start(struct s5p_mfc_ctx *ctx)
1191 {
1192 	struct s5p_mfc_dev *dev = ctx->dev;
1193 	struct s5p_mfc_buf *dst_mb;
1194 	struct s5p_mfc_buf *src_mb;
1195 	unsigned long src_y_addr, src_c_addr, dst_addr;
1196 	unsigned int dst_size;
1197 
1198 	src_mb = list_entry(ctx->src_queue.next, struct s5p_mfc_buf, list);
1199 	src_y_addr = vb2_dma_contig_plane_dma_addr(&src_mb->b->vb2_buf, 0);
1200 	src_c_addr = vb2_dma_contig_plane_dma_addr(&src_mb->b->vb2_buf, 1);
1201 	s5p_mfc_hw_call(dev->mfc_ops, set_enc_frame_buffer, ctx,
1202 							src_y_addr, src_c_addr);
1203 
1204 	dst_mb = list_entry(ctx->dst_queue.next, struct s5p_mfc_buf, list);
1205 	dst_addr = vb2_dma_contig_plane_dma_addr(&dst_mb->b->vb2_buf, 0);
1206 	dst_size = vb2_plane_size(&dst_mb->b->vb2_buf, 0);
1207 	s5p_mfc_hw_call(dev->mfc_ops, set_enc_stream_buffer, ctx, dst_addr,
1208 			dst_size);
1209 
1210 	return 0;
1211 }
1212 
1213 static int enc_post_frame_start(struct s5p_mfc_ctx *ctx)
1214 {
1215 	struct s5p_mfc_dev *dev = ctx->dev;
1216 	struct s5p_mfc_buf *mb_entry;
1217 	unsigned long enc_y_addr = 0, enc_c_addr = 0;
1218 	unsigned long mb_y_addr, mb_c_addr;
1219 	int slice_type;
1220 	unsigned int strm_size;
1221 
1222 	slice_type = s5p_mfc_hw_call(dev->mfc_ops, get_enc_slice_type, dev);
1223 	strm_size = s5p_mfc_hw_call(dev->mfc_ops, get_enc_strm_size, dev);
1224 	mfc_debug(2, "Encoded slice type: %d\n", slice_type);
1225 	mfc_debug(2, "Encoded stream size: %d\n", strm_size);
1226 	mfc_debug(2, "Display order: %d\n",
1227 		  mfc_read(dev, S5P_FIMV_ENC_SI_PIC_CNT));
1228 	if (slice_type >= 0) {
1229 		s5p_mfc_hw_call(dev->mfc_ops, get_enc_frame_buffer, ctx,
1230 				&enc_y_addr, &enc_c_addr);
1231 		list_for_each_entry(mb_entry, &ctx->src_queue, list) {
1232 			mb_y_addr = vb2_dma_contig_plane_dma_addr(
1233 					&mb_entry->b->vb2_buf, 0);
1234 			mb_c_addr = vb2_dma_contig_plane_dma_addr(
1235 					&mb_entry->b->vb2_buf, 1);
1236 			if ((enc_y_addr == mb_y_addr) &&
1237 						(enc_c_addr == mb_c_addr)) {
1238 				list_del(&mb_entry->list);
1239 				ctx->src_queue_cnt--;
1240 				vb2_buffer_done(&mb_entry->b->vb2_buf,
1241 							VB2_BUF_STATE_DONE);
1242 				break;
1243 			}
1244 		}
1245 		list_for_each_entry(mb_entry, &ctx->ref_queue, list) {
1246 			mb_y_addr = vb2_dma_contig_plane_dma_addr(
1247 					&mb_entry->b->vb2_buf, 0);
1248 			mb_c_addr = vb2_dma_contig_plane_dma_addr(
1249 					&mb_entry->b->vb2_buf, 1);
1250 			if ((enc_y_addr == mb_y_addr) &&
1251 						(enc_c_addr == mb_c_addr)) {
1252 				list_del(&mb_entry->list);
1253 				ctx->ref_queue_cnt--;
1254 				vb2_buffer_done(&mb_entry->b->vb2_buf,
1255 							VB2_BUF_STATE_DONE);
1256 				break;
1257 			}
1258 		}
1259 	}
1260 	if ((ctx->src_queue_cnt > 0) && (ctx->state == MFCINST_RUNNING)) {
1261 		mb_entry = list_entry(ctx->src_queue.next, struct s5p_mfc_buf,
1262 									list);
1263 		if (mb_entry->flags & MFC_BUF_FLAG_USED) {
1264 			list_del(&mb_entry->list);
1265 			ctx->src_queue_cnt--;
1266 			list_add_tail(&mb_entry->list, &ctx->ref_queue);
1267 			ctx->ref_queue_cnt++;
1268 		}
1269 	}
1270 	mfc_debug(2, "enc src count: %d, enc ref count: %d\n",
1271 		  ctx->src_queue_cnt, ctx->ref_queue_cnt);
1272 	if ((ctx->dst_queue_cnt > 0) && (strm_size > 0)) {
1273 		mb_entry = list_entry(ctx->dst_queue.next, struct s5p_mfc_buf,
1274 									list);
1275 		list_del(&mb_entry->list);
1276 		ctx->dst_queue_cnt--;
1277 		switch (slice_type) {
1278 		case S5P_FIMV_ENC_SI_SLICE_TYPE_I:
1279 			mb_entry->b->flags |= V4L2_BUF_FLAG_KEYFRAME;
1280 			break;
1281 		case S5P_FIMV_ENC_SI_SLICE_TYPE_P:
1282 			mb_entry->b->flags |= V4L2_BUF_FLAG_PFRAME;
1283 			break;
1284 		case S5P_FIMV_ENC_SI_SLICE_TYPE_B:
1285 			mb_entry->b->flags |= V4L2_BUF_FLAG_BFRAME;
1286 			break;
1287 		}
1288 		vb2_set_plane_payload(&mb_entry->b->vb2_buf, 0, strm_size);
1289 		vb2_buffer_done(&mb_entry->b->vb2_buf, VB2_BUF_STATE_DONE);
1290 	}
1291 	if ((ctx->src_queue_cnt == 0) || (ctx->dst_queue_cnt == 0))
1292 		clear_work_bit(ctx);
1293 
1294 	return 0;
1295 }
1296 
1297 static const struct s5p_mfc_codec_ops encoder_codec_ops = {
1298 	.pre_seq_start		= enc_pre_seq_start,
1299 	.post_seq_start		= enc_post_seq_start,
1300 	.pre_frame_start	= enc_pre_frame_start,
1301 	.post_frame_start	= enc_post_frame_start,
1302 };
1303 
1304 /* Query capabilities of the device */
1305 static int vidioc_querycap(struct file *file, void *priv,
1306 			   struct v4l2_capability *cap)
1307 {
1308 	struct s5p_mfc_dev *dev = video_drvdata(file);
1309 
1310 	strscpy(cap->driver, S5P_MFC_NAME, sizeof(cap->driver));
1311 	strscpy(cap->card, dev->vfd_enc->name, sizeof(cap->card));
1312 	return 0;
1313 }
1314 
1315 static int vidioc_enum_fmt(struct file *file, struct v4l2_fmtdesc *f,
1316 							bool out)
1317 {
1318 	struct s5p_mfc_dev *dev = video_drvdata(file);
1319 	int i, j = 0;
1320 
1321 	for (i = 0; i < ARRAY_SIZE(formats); ++i) {
1322 		if (out && formats[i].type != MFC_FMT_RAW)
1323 			continue;
1324 		else if (!out && formats[i].type != MFC_FMT_ENC)
1325 			continue;
1326 		else if ((dev->variant->version_bit & formats[i].versions) == 0)
1327 			continue;
1328 
1329 		if (j == f->index) {
1330 			f->pixelformat = formats[i].fourcc;
1331 			return 0;
1332 		}
1333 		++j;
1334 	}
1335 	return -EINVAL;
1336 }
1337 
1338 static int vidioc_enum_fmt_vid_cap(struct file *file, void *pirv,
1339 				   struct v4l2_fmtdesc *f)
1340 {
1341 	return vidioc_enum_fmt(file, f, false);
1342 }
1343 
1344 static int vidioc_enum_fmt_vid_out(struct file *file, void *priv,
1345 				   struct v4l2_fmtdesc *f)
1346 {
1347 	return vidioc_enum_fmt(file, f, true);
1348 }
1349 
1350 static int vidioc_g_fmt(struct file *file, void *priv, struct v4l2_format *f)
1351 {
1352 	struct s5p_mfc_ctx *ctx = fh_to_ctx(priv);
1353 	struct v4l2_pix_format_mplane *pix_fmt_mp = &f->fmt.pix_mp;
1354 
1355 	mfc_debug(2, "f->type = %d ctx->state = %d\n", f->type, ctx->state);
1356 	if (f->type == V4L2_BUF_TYPE_VIDEO_CAPTURE_MPLANE) {
1357 		/* This is run on output (encoder dest) */
1358 		pix_fmt_mp->width = 0;
1359 		pix_fmt_mp->height = 0;
1360 		pix_fmt_mp->field = V4L2_FIELD_NONE;
1361 		pix_fmt_mp->pixelformat = ctx->dst_fmt->fourcc;
1362 		pix_fmt_mp->num_planes = ctx->dst_fmt->num_planes;
1363 
1364 		pix_fmt_mp->plane_fmt[0].bytesperline = ctx->enc_dst_buf_size;
1365 		pix_fmt_mp->plane_fmt[0].sizeimage = ctx->enc_dst_buf_size;
1366 	} else if (f->type == V4L2_BUF_TYPE_VIDEO_OUTPUT_MPLANE) {
1367 		/* This is run on capture (encoder src) */
1368 		pix_fmt_mp->width = ctx->img_width;
1369 		pix_fmt_mp->height = ctx->img_height;
1370 
1371 		pix_fmt_mp->field = V4L2_FIELD_NONE;
1372 		pix_fmt_mp->pixelformat = ctx->src_fmt->fourcc;
1373 		pix_fmt_mp->num_planes = ctx->src_fmt->num_planes;
1374 
1375 		pix_fmt_mp->plane_fmt[0].bytesperline = ctx->buf_width;
1376 		pix_fmt_mp->plane_fmt[0].sizeimage = ctx->luma_size;
1377 		pix_fmt_mp->plane_fmt[1].bytesperline = ctx->buf_width;
1378 		pix_fmt_mp->plane_fmt[1].sizeimage = ctx->chroma_size;
1379 	} else {
1380 		mfc_err("invalid buf type\n");
1381 		return -EINVAL;
1382 	}
1383 	return 0;
1384 }
1385 
1386 static int vidioc_try_fmt(struct file *file, void *priv, struct v4l2_format *f)
1387 {
1388 	struct s5p_mfc_dev *dev = video_drvdata(file);
1389 	struct s5p_mfc_fmt *fmt;
1390 	struct v4l2_pix_format_mplane *pix_fmt_mp = &f->fmt.pix_mp;
1391 
1392 	if (f->type == V4L2_BUF_TYPE_VIDEO_CAPTURE_MPLANE) {
1393 		fmt = find_format(f, MFC_FMT_ENC);
1394 		if (!fmt) {
1395 			mfc_err("failed to try output format\n");
1396 			return -EINVAL;
1397 		}
1398 		if ((dev->variant->version_bit & fmt->versions) == 0) {
1399 			mfc_err("Unsupported format by this MFC version.\n");
1400 			return -EINVAL;
1401 		}
1402 
1403 		pix_fmt_mp->plane_fmt[0].bytesperline =
1404 			pix_fmt_mp->plane_fmt[0].sizeimage;
1405 	} else if (f->type == V4L2_BUF_TYPE_VIDEO_OUTPUT_MPLANE) {
1406 		fmt = find_format(f, MFC_FMT_RAW);
1407 		if (!fmt) {
1408 			mfc_err("failed to try output format\n");
1409 			return -EINVAL;
1410 		}
1411 		if ((dev->variant->version_bit & fmt->versions) == 0) {
1412 			mfc_err("Unsupported format by this MFC version.\n");
1413 			return -EINVAL;
1414 		}
1415 
1416 		v4l_bound_align_image(&pix_fmt_mp->width, 8, 1920, 1,
1417 			&pix_fmt_mp->height, 4, 1080, 1, 0);
1418 	} else {
1419 		mfc_err("invalid buf type\n");
1420 		return -EINVAL;
1421 	}
1422 	return 0;
1423 }
1424 
1425 static int vidioc_s_fmt(struct file *file, void *priv, struct v4l2_format *f)
1426 {
1427 	struct s5p_mfc_dev *dev = video_drvdata(file);
1428 	struct s5p_mfc_ctx *ctx = fh_to_ctx(priv);
1429 	struct v4l2_pix_format_mplane *pix_fmt_mp = &f->fmt.pix_mp;
1430 	int ret = 0;
1431 
1432 	ret = vidioc_try_fmt(file, priv, f);
1433 	if (ret)
1434 		return ret;
1435 	if (ctx->vq_src.streaming || ctx->vq_dst.streaming) {
1436 		v4l2_err(&dev->v4l2_dev, "%s queue busy\n", __func__);
1437 		ret = -EBUSY;
1438 		goto out;
1439 	}
1440 	if (f->type == V4L2_BUF_TYPE_VIDEO_CAPTURE_MPLANE) {
1441 		/* dst_fmt is validated by call to vidioc_try_fmt */
1442 		ctx->dst_fmt = find_format(f, MFC_FMT_ENC);
1443 		ctx->state = MFCINST_INIT;
1444 		ctx->codec_mode = ctx->dst_fmt->codec_mode;
1445 		ctx->enc_dst_buf_size =	pix_fmt_mp->plane_fmt[0].sizeimage;
1446 		pix_fmt_mp->plane_fmt[0].bytesperline = 0;
1447 		ctx->dst_bufs_cnt = 0;
1448 		ctx->capture_state = QUEUE_FREE;
1449 		ret = s5p_mfc_open_mfc_inst(dev, ctx);
1450 	} else if (f->type == V4L2_BUF_TYPE_VIDEO_OUTPUT_MPLANE) {
1451 		/* src_fmt is validated by call to vidioc_try_fmt */
1452 		ctx->src_fmt = find_format(f, MFC_FMT_RAW);
1453 		ctx->img_width = pix_fmt_mp->width;
1454 		ctx->img_height = pix_fmt_mp->height;
1455 		mfc_debug(2, "codec number: %d\n", ctx->src_fmt->codec_mode);
1456 		mfc_debug(2, "fmt - w: %d, h: %d, ctx - w: %d, h: %d\n",
1457 			pix_fmt_mp->width, pix_fmt_mp->height,
1458 			ctx->img_width, ctx->img_height);
1459 
1460 		s5p_mfc_hw_call(dev->mfc_ops, enc_calc_src_size, ctx);
1461 		pix_fmt_mp->plane_fmt[0].sizeimage = ctx->luma_size;
1462 		pix_fmt_mp->plane_fmt[0].bytesperline = ctx->buf_width;
1463 		pix_fmt_mp->plane_fmt[1].sizeimage = ctx->chroma_size;
1464 		pix_fmt_mp->plane_fmt[1].bytesperline = ctx->buf_width;
1465 
1466 		ctx->src_bufs_cnt = 0;
1467 		ctx->output_state = QUEUE_FREE;
1468 	} else {
1469 		mfc_err("invalid buf type\n");
1470 		ret = -EINVAL;
1471 	}
1472 out:
1473 	mfc_debug_leave();
1474 	return ret;
1475 }
1476 
1477 static int vidioc_reqbufs(struct file *file, void *priv,
1478 					  struct v4l2_requestbuffers *reqbufs)
1479 {
1480 	struct s5p_mfc_dev *dev = video_drvdata(file);
1481 	struct s5p_mfc_ctx *ctx = fh_to_ctx(priv);
1482 	int ret = 0;
1483 
1484 	/* if memory is not mmp or userptr return error */
1485 	if ((reqbufs->memory != V4L2_MEMORY_MMAP) &&
1486 		(reqbufs->memory != V4L2_MEMORY_USERPTR))
1487 		return -EINVAL;
1488 	if (reqbufs->type == V4L2_BUF_TYPE_VIDEO_CAPTURE_MPLANE) {
1489 		if (reqbufs->count == 0) {
1490 			mfc_debug(2, "Freeing buffers\n");
1491 			ret = vb2_reqbufs(&ctx->vq_dst, reqbufs);
1492 			s5p_mfc_hw_call(dev->mfc_ops, release_codec_buffers,
1493 					ctx);
1494 			ctx->capture_state = QUEUE_FREE;
1495 			return ret;
1496 		}
1497 		if (ctx->capture_state != QUEUE_FREE) {
1498 			mfc_err("invalid capture state: %d\n",
1499 							ctx->capture_state);
1500 			return -EINVAL;
1501 		}
1502 		ret = vb2_reqbufs(&ctx->vq_dst, reqbufs);
1503 		if (ret != 0) {
1504 			mfc_err("error in vb2_reqbufs() for E(D)\n");
1505 			return ret;
1506 		}
1507 		ctx->capture_state = QUEUE_BUFS_REQUESTED;
1508 
1509 		ret = s5p_mfc_hw_call(ctx->dev->mfc_ops,
1510 				alloc_codec_buffers, ctx);
1511 		if (ret) {
1512 			mfc_err("Failed to allocate encoding buffers\n");
1513 			reqbufs->count = 0;
1514 			ret = vb2_reqbufs(&ctx->vq_dst, reqbufs);
1515 			return -ENOMEM;
1516 		}
1517 	} else if (reqbufs->type == V4L2_BUF_TYPE_VIDEO_OUTPUT_MPLANE) {
1518 		if (reqbufs->count == 0) {
1519 			mfc_debug(2, "Freeing buffers\n");
1520 			ret = vb2_reqbufs(&ctx->vq_src, reqbufs);
1521 			s5p_mfc_hw_call(dev->mfc_ops, release_codec_buffers,
1522 					ctx);
1523 			ctx->output_state = QUEUE_FREE;
1524 			return ret;
1525 		}
1526 		if (ctx->output_state != QUEUE_FREE) {
1527 			mfc_err("invalid output state: %d\n",
1528 							ctx->output_state);
1529 			return -EINVAL;
1530 		}
1531 
1532 		if (IS_MFCV6_PLUS(dev)) {
1533 			/* Check for min encoder buffers */
1534 			if (ctx->pb_count &&
1535 				(reqbufs->count < ctx->pb_count)) {
1536 				reqbufs->count = ctx->pb_count;
1537 				mfc_debug(2, "Minimum %d output buffers needed\n",
1538 						ctx->pb_count);
1539 			} else {
1540 				ctx->pb_count = reqbufs->count;
1541 			}
1542 		}
1543 
1544 		ret = vb2_reqbufs(&ctx->vq_src, reqbufs);
1545 		if (ret != 0) {
1546 			mfc_err("error in vb2_reqbufs() for E(S)\n");
1547 			return ret;
1548 		}
1549 		ctx->output_state = QUEUE_BUFS_REQUESTED;
1550 	} else {
1551 		mfc_err("invalid buf type\n");
1552 		return -EINVAL;
1553 	}
1554 	return ret;
1555 }
1556 
1557 static int vidioc_querybuf(struct file *file, void *priv,
1558 						   struct v4l2_buffer *buf)
1559 {
1560 	struct s5p_mfc_ctx *ctx = fh_to_ctx(priv);
1561 	int ret = 0;
1562 
1563 	/* if memory is not mmp or userptr return error */
1564 	if ((buf->memory != V4L2_MEMORY_MMAP) &&
1565 		(buf->memory != V4L2_MEMORY_USERPTR))
1566 		return -EINVAL;
1567 	if (buf->type == V4L2_BUF_TYPE_VIDEO_CAPTURE_MPLANE) {
1568 		if (ctx->state != MFCINST_GOT_INST) {
1569 			mfc_err("invalid context state: %d\n", ctx->state);
1570 			return -EINVAL;
1571 		}
1572 		ret = vb2_querybuf(&ctx->vq_dst, buf);
1573 		if (ret != 0) {
1574 			mfc_err("error in vb2_querybuf() for E(D)\n");
1575 			return ret;
1576 		}
1577 		buf->m.planes[0].m.mem_offset += DST_QUEUE_OFF_BASE;
1578 	} else if (buf->type == V4L2_BUF_TYPE_VIDEO_OUTPUT_MPLANE) {
1579 		ret = vb2_querybuf(&ctx->vq_src, buf);
1580 		if (ret != 0) {
1581 			mfc_err("error in vb2_querybuf() for E(S)\n");
1582 			return ret;
1583 		}
1584 	} else {
1585 		mfc_err("invalid buf type\n");
1586 		return -EINVAL;
1587 	}
1588 	return ret;
1589 }
1590 
1591 /* Queue a buffer */
1592 static int vidioc_qbuf(struct file *file, void *priv, struct v4l2_buffer *buf)
1593 {
1594 	struct s5p_mfc_ctx *ctx = fh_to_ctx(priv);
1595 
1596 	if (ctx->state == MFCINST_ERROR) {
1597 		mfc_err("Call on QBUF after unrecoverable error\n");
1598 		return -EIO;
1599 	}
1600 	if (buf->type == V4L2_BUF_TYPE_VIDEO_OUTPUT_MPLANE) {
1601 		if (ctx->state == MFCINST_FINISHING) {
1602 			mfc_err("Call on QBUF after EOS command\n");
1603 			return -EIO;
1604 		}
1605 		return vb2_qbuf(&ctx->vq_src, NULL, buf);
1606 	} else if (buf->type == V4L2_BUF_TYPE_VIDEO_CAPTURE_MPLANE) {
1607 		return vb2_qbuf(&ctx->vq_dst, NULL, buf);
1608 	}
1609 	return -EINVAL;
1610 }
1611 
1612 /* Dequeue a buffer */
1613 static int vidioc_dqbuf(struct file *file, void *priv, struct v4l2_buffer *buf)
1614 {
1615 	const struct v4l2_event ev = {
1616 		.type = V4L2_EVENT_EOS
1617 	};
1618 	struct s5p_mfc_ctx *ctx = fh_to_ctx(priv);
1619 	int ret;
1620 
1621 	if (ctx->state == MFCINST_ERROR) {
1622 		mfc_err_limited("Call on DQBUF after unrecoverable error\n");
1623 		return -EIO;
1624 	}
1625 	if (buf->type == V4L2_BUF_TYPE_VIDEO_OUTPUT_MPLANE) {
1626 		ret = vb2_dqbuf(&ctx->vq_src, buf, file->f_flags & O_NONBLOCK);
1627 	} else if (buf->type == V4L2_BUF_TYPE_VIDEO_CAPTURE_MPLANE) {
1628 		ret = vb2_dqbuf(&ctx->vq_dst, buf, file->f_flags & O_NONBLOCK);
1629 		if (ret == 0 && ctx->state == MFCINST_FINISHED
1630 					&& list_empty(&ctx->vq_dst.done_list))
1631 			v4l2_event_queue_fh(&ctx->fh, &ev);
1632 	} else {
1633 		ret = -EINVAL;
1634 	}
1635 
1636 	return ret;
1637 }
1638 
1639 /* Export DMA buffer */
1640 static int vidioc_expbuf(struct file *file, void *priv,
1641 	struct v4l2_exportbuffer *eb)
1642 {
1643 	struct s5p_mfc_ctx *ctx = fh_to_ctx(priv);
1644 
1645 	if (eb->type == V4L2_BUF_TYPE_VIDEO_OUTPUT_MPLANE)
1646 		return vb2_expbuf(&ctx->vq_src, eb);
1647 	if (eb->type == V4L2_BUF_TYPE_VIDEO_CAPTURE_MPLANE)
1648 		return vb2_expbuf(&ctx->vq_dst, eb);
1649 	return -EINVAL;
1650 }
1651 
1652 /* Stream on */
1653 static int vidioc_streamon(struct file *file, void *priv,
1654 			   enum v4l2_buf_type type)
1655 {
1656 	struct s5p_mfc_ctx *ctx = fh_to_ctx(priv);
1657 
1658 	if (type == V4L2_BUF_TYPE_VIDEO_OUTPUT_MPLANE)
1659 		return vb2_streamon(&ctx->vq_src, type);
1660 	else if (type == V4L2_BUF_TYPE_VIDEO_CAPTURE_MPLANE)
1661 		return vb2_streamon(&ctx->vq_dst, type);
1662 	return -EINVAL;
1663 }
1664 
1665 /* Stream off, which equals to a pause */
1666 static int vidioc_streamoff(struct file *file, void *priv,
1667 			    enum v4l2_buf_type type)
1668 {
1669 	struct s5p_mfc_ctx *ctx = fh_to_ctx(priv);
1670 
1671 	if (type == V4L2_BUF_TYPE_VIDEO_OUTPUT_MPLANE)
1672 		return vb2_streamoff(&ctx->vq_src, type);
1673 	else if (type == V4L2_BUF_TYPE_VIDEO_CAPTURE_MPLANE)
1674 		return vb2_streamoff(&ctx->vq_dst, type);
1675 	return -EINVAL;
1676 }
1677 
1678 static inline int h264_level(enum v4l2_mpeg_video_h264_level lvl)
1679 {
1680 	static unsigned int t[V4L2_MPEG_VIDEO_H264_LEVEL_4_0 + 1] = {
1681 		/* V4L2_MPEG_VIDEO_H264_LEVEL_1_0   */ 10,
1682 		/* V4L2_MPEG_VIDEO_H264_LEVEL_1B    */ 9,
1683 		/* V4L2_MPEG_VIDEO_H264_LEVEL_1_1   */ 11,
1684 		/* V4L2_MPEG_VIDEO_H264_LEVEL_1_2   */ 12,
1685 		/* V4L2_MPEG_VIDEO_H264_LEVEL_1_3   */ 13,
1686 		/* V4L2_MPEG_VIDEO_H264_LEVEL_2_0   */ 20,
1687 		/* V4L2_MPEG_VIDEO_H264_LEVEL_2_1   */ 21,
1688 		/* V4L2_MPEG_VIDEO_H264_LEVEL_2_2   */ 22,
1689 		/* V4L2_MPEG_VIDEO_H264_LEVEL_3_0   */ 30,
1690 		/* V4L2_MPEG_VIDEO_H264_LEVEL_3_1   */ 31,
1691 		/* V4L2_MPEG_VIDEO_H264_LEVEL_3_2   */ 32,
1692 		/* V4L2_MPEG_VIDEO_H264_LEVEL_4_0   */ 40,
1693 	};
1694 	return t[lvl];
1695 }
1696 
1697 static inline int mpeg4_level(enum v4l2_mpeg_video_mpeg4_level lvl)
1698 {
1699 	static unsigned int t[V4L2_MPEG_VIDEO_MPEG4_LEVEL_5 + 1] = {
1700 		/* V4L2_MPEG_VIDEO_MPEG4_LEVEL_0    */ 0,
1701 		/* V4L2_MPEG_VIDEO_MPEG4_LEVEL_0B   */ 9,
1702 		/* V4L2_MPEG_VIDEO_MPEG4_LEVEL_1    */ 1,
1703 		/* V4L2_MPEG_VIDEO_MPEG4_LEVEL_2    */ 2,
1704 		/* V4L2_MPEG_VIDEO_MPEG4_LEVEL_3    */ 3,
1705 		/* V4L2_MPEG_VIDEO_MPEG4_LEVEL_3B   */ 7,
1706 		/* V4L2_MPEG_VIDEO_MPEG4_LEVEL_4    */ 4,
1707 		/* V4L2_MPEG_VIDEO_MPEG4_LEVEL_5    */ 5,
1708 	};
1709 	return t[lvl];
1710 }
1711 
1712 static inline int hevc_level(enum v4l2_mpeg_video_hevc_level lvl)
1713 {
1714 	static unsigned int t[] = {
1715 		/* V4L2_MPEG_VIDEO_HEVC_LEVEL_1    */ 10,
1716 		/* V4L2_MPEG_VIDEO_HEVC_LEVEL_2    */ 20,
1717 		/* V4L2_MPEG_VIDEO_HEVC_LEVEL_2_1  */ 21,
1718 		/* V4L2_MPEG_VIDEO_HEVC_LEVEL_3    */ 30,
1719 		/* V4L2_MPEG_VIDEO_HEVC_LEVEL_3_1  */ 31,
1720 		/* V4L2_MPEG_VIDEO_HEVC_LEVEL_4    */ 40,
1721 		/* V4L2_MPEG_VIDEO_HEVC_LEVEL_4_1  */ 41,
1722 		/* V4L2_MPEG_VIDEO_HEVC_LEVEL_5    */ 50,
1723 		/* V4L2_MPEG_VIDEO_HEVC_LEVEL_5_1  */ 51,
1724 		/* V4L2_MPEG_VIDEO_HEVC_LEVEL_5_2  */ 52,
1725 		/* V4L2_MPEG_VIDEO_HEVC_LEVEL_6    */ 60,
1726 		/* V4L2_MPEG_VIDEO_HEVC_LEVEL_6_1  */ 61,
1727 		/* V4L2_MPEG_VIDEO_HEVC_LEVEL_6_2  */ 62,
1728 	};
1729 	return t[lvl];
1730 }
1731 
1732 static inline int vui_sar_idc(enum v4l2_mpeg_video_h264_vui_sar_idc sar)
1733 {
1734 	static unsigned int t[V4L2_MPEG_VIDEO_H264_VUI_SAR_IDC_EXTENDED + 1] = {
1735 		/* V4L2_MPEG_VIDEO_H264_VUI_SAR_IDC_UNSPECIFIED     */ 0,
1736 		/* V4L2_MPEG_VIDEO_H264_VUI_SAR_IDC_1x1             */ 1,
1737 		/* V4L2_MPEG_VIDEO_H264_VUI_SAR_IDC_12x11           */ 2,
1738 		/* V4L2_MPEG_VIDEO_H264_VUI_SAR_IDC_10x11           */ 3,
1739 		/* V4L2_MPEG_VIDEO_H264_VUI_SAR_IDC_16x11           */ 4,
1740 		/* V4L2_MPEG_VIDEO_H264_VUI_SAR_IDC_40x33           */ 5,
1741 		/* V4L2_MPEG_VIDEO_H264_VUI_SAR_IDC_24x11           */ 6,
1742 		/* V4L2_MPEG_VIDEO_H264_VUI_SAR_IDC_20x11           */ 7,
1743 		/* V4L2_MPEG_VIDEO_H264_VUI_SAR_IDC_32x11           */ 8,
1744 		/* V4L2_MPEG_VIDEO_H264_VUI_SAR_IDC_80x33           */ 9,
1745 		/* V4L2_MPEG_VIDEO_H264_VUI_SAR_IDC_18x11           */ 10,
1746 		/* V4L2_MPEG_VIDEO_H264_VUI_SAR_IDC_15x11           */ 11,
1747 		/* V4L2_MPEG_VIDEO_H264_VUI_SAR_IDC_64x33           */ 12,
1748 		/* V4L2_MPEG_VIDEO_H264_VUI_SAR_IDC_160x99          */ 13,
1749 		/* V4L2_MPEG_VIDEO_H264_VUI_SAR_IDC_4x3             */ 14,
1750 		/* V4L2_MPEG_VIDEO_H264_VUI_SAR_IDC_3x2             */ 15,
1751 		/* V4L2_MPEG_VIDEO_H264_VUI_SAR_IDC_2x1             */ 16,
1752 		/* V4L2_MPEG_VIDEO_H264_VUI_SAR_IDC_EXTENDED        */ 255,
1753 	};
1754 	return t[sar];
1755 }
1756 
1757 /*
1758  * Update range of all HEVC quantization parameter controls that depend on the
1759  * V4L2_CID_MPEG_VIDEO_HEVC_MIN_QP, V4L2_CID_MPEG_VIDEO_HEVC_MAX_QP controls.
1760  */
1761 static void __enc_update_hevc_qp_ctrls_range(struct s5p_mfc_ctx *ctx,
1762 					     int min, int max)
1763 {
1764 	static const int __hevc_qp_ctrls[] = {
1765 		V4L2_CID_MPEG_VIDEO_HEVC_I_FRAME_QP,
1766 		V4L2_CID_MPEG_VIDEO_HEVC_P_FRAME_QP,
1767 		V4L2_CID_MPEG_VIDEO_HEVC_B_FRAME_QP,
1768 		V4L2_CID_MPEG_VIDEO_HEVC_HIER_CODING_L0_QP,
1769 		V4L2_CID_MPEG_VIDEO_HEVC_HIER_CODING_L1_QP,
1770 		V4L2_CID_MPEG_VIDEO_HEVC_HIER_CODING_L2_QP,
1771 		V4L2_CID_MPEG_VIDEO_HEVC_HIER_CODING_L3_QP,
1772 		V4L2_CID_MPEG_VIDEO_HEVC_HIER_CODING_L4_QP,
1773 		V4L2_CID_MPEG_VIDEO_HEVC_HIER_CODING_L5_QP,
1774 		V4L2_CID_MPEG_VIDEO_HEVC_HIER_CODING_L6_QP,
1775 	};
1776 	struct v4l2_ctrl *ctrl = NULL;
1777 	int i, j;
1778 
1779 	for (i = 0; i < ARRAY_SIZE(__hevc_qp_ctrls); i++) {
1780 		for (j = 0; j < ARRAY_SIZE(ctx->ctrls); j++) {
1781 			if (ctx->ctrls[j]->id == __hevc_qp_ctrls[i]) {
1782 				ctrl = ctx->ctrls[j];
1783 				break;
1784 			}
1785 		}
1786 		if (WARN_ON(!ctrl))
1787 			break;
1788 
1789 		__v4l2_ctrl_modify_range(ctrl, min, max, ctrl->step, min);
1790 	}
1791 }
1792 
1793 static int s5p_mfc_enc_s_ctrl(struct v4l2_ctrl *ctrl)
1794 {
1795 	struct s5p_mfc_ctx *ctx = ctrl_to_ctx(ctrl);
1796 	struct s5p_mfc_dev *dev = ctx->dev;
1797 	struct s5p_mfc_enc_params *p = &ctx->enc_params;
1798 	int ret = 0;
1799 
1800 	switch (ctrl->id) {
1801 	case V4L2_CID_MPEG_VIDEO_GOP_SIZE:
1802 		p->gop_size = ctrl->val;
1803 		break;
1804 	case V4L2_CID_MPEG_VIDEO_MULTI_SLICE_MODE:
1805 		p->slice_mode = ctrl->val;
1806 		break;
1807 	case V4L2_CID_MPEG_VIDEO_MULTI_SLICE_MAX_MB:
1808 		p->slice_mb = ctrl->val;
1809 		break;
1810 	case V4L2_CID_MPEG_VIDEO_MULTI_SLICE_MAX_BYTES:
1811 		p->slice_bit = ctrl->val * 8;
1812 		break;
1813 	case V4L2_CID_MPEG_VIDEO_CYCLIC_INTRA_REFRESH_MB:
1814 		p->intra_refresh_mb = ctrl->val;
1815 		break;
1816 	case V4L2_CID_MPEG_MFC51_VIDEO_PADDING:
1817 		p->pad = ctrl->val;
1818 		break;
1819 	case V4L2_CID_MPEG_MFC51_VIDEO_PADDING_YUV:
1820 		p->pad_luma = (ctrl->val >> 16) & 0xff;
1821 		p->pad_cb = (ctrl->val >> 8) & 0xff;
1822 		p->pad_cr = (ctrl->val >> 0) & 0xff;
1823 		break;
1824 	case V4L2_CID_MPEG_VIDEO_FRAME_RC_ENABLE:
1825 		p->rc_frame = ctrl->val;
1826 		break;
1827 	case V4L2_CID_MPEG_VIDEO_BITRATE:
1828 		p->rc_bitrate = ctrl->val;
1829 		break;
1830 	case V4L2_CID_MPEG_MFC51_VIDEO_RC_REACTION_COEFF:
1831 		p->rc_reaction_coeff = ctrl->val;
1832 		break;
1833 	case V4L2_CID_MPEG_MFC51_VIDEO_FORCE_FRAME_TYPE:
1834 		ctx->force_frame_type = ctrl->val;
1835 		break;
1836 	case V4L2_CID_MPEG_VIDEO_FORCE_KEY_FRAME:
1837 		ctx->force_frame_type =
1838 			V4L2_MPEG_MFC51_VIDEO_FORCE_FRAME_TYPE_I_FRAME;
1839 		break;
1840 	case V4L2_CID_MPEG_VIDEO_VBV_SIZE:
1841 		p->vbv_size = ctrl->val;
1842 		break;
1843 	case V4L2_CID_MPEG_VIDEO_MV_H_SEARCH_RANGE:
1844 		p->mv_h_range = ctrl->val;
1845 		break;
1846 	case V4L2_CID_MPEG_VIDEO_MV_V_SEARCH_RANGE:
1847 		p->mv_v_range = ctrl->val;
1848 		break;
1849 	case V4L2_CID_MPEG_VIDEO_H264_CPB_SIZE:
1850 		p->codec.h264.cpb_size = ctrl->val;
1851 		break;
1852 	case V4L2_CID_MPEG_VIDEO_HEADER_MODE:
1853 		p->seq_hdr_mode = ctrl->val;
1854 		break;
1855 	case V4L2_CID_MPEG_MFC51_VIDEO_FRAME_SKIP_MODE:
1856 	case V4L2_CID_MPEG_VIDEO_FRAME_SKIP_MODE:
1857 		p->frame_skip_mode = ctrl->val;
1858 		break;
1859 	case V4L2_CID_MPEG_MFC51_VIDEO_RC_FIXED_TARGET_BIT:
1860 		p->fixed_target_bit = ctrl->val;
1861 		break;
1862 	case V4L2_CID_MPEG_VIDEO_B_FRAMES:
1863 		p->num_b_frame = ctrl->val;
1864 		break;
1865 	case V4L2_CID_MPEG_VIDEO_H264_PROFILE:
1866 		switch (ctrl->val) {
1867 		case V4L2_MPEG_VIDEO_H264_PROFILE_MAIN:
1868 			p->codec.h264.profile =
1869 					S5P_FIMV_ENC_PROFILE_H264_MAIN;
1870 			break;
1871 		case V4L2_MPEG_VIDEO_H264_PROFILE_HIGH:
1872 			p->codec.h264.profile =
1873 					S5P_FIMV_ENC_PROFILE_H264_HIGH;
1874 			break;
1875 		case V4L2_MPEG_VIDEO_H264_PROFILE_BASELINE:
1876 			p->codec.h264.profile =
1877 				S5P_FIMV_ENC_PROFILE_H264_BASELINE;
1878 			break;
1879 		case V4L2_MPEG_VIDEO_H264_PROFILE_CONSTRAINED_BASELINE:
1880 			if (IS_MFCV6_PLUS(dev))
1881 				p->codec.h264.profile =
1882 				S5P_FIMV_ENC_PROFILE_H264_CONSTRAINED_BASELINE;
1883 			else
1884 				ret = -EINVAL;
1885 			break;
1886 		default:
1887 			ret = -EINVAL;
1888 		}
1889 		break;
1890 	case V4L2_CID_MPEG_VIDEO_H264_LEVEL:
1891 		p->codec.h264.level_v4l2 = ctrl->val;
1892 		p->codec.h264.level = h264_level(ctrl->val);
1893 		if (p->codec.h264.level < 0) {
1894 			mfc_err("Level number is wrong\n");
1895 			ret = p->codec.h264.level;
1896 		}
1897 		break;
1898 	case V4L2_CID_MPEG_VIDEO_MPEG4_LEVEL:
1899 		p->codec.mpeg4.level_v4l2 = ctrl->val;
1900 		p->codec.mpeg4.level = mpeg4_level(ctrl->val);
1901 		if (p->codec.mpeg4.level < 0) {
1902 			mfc_err("Level number is wrong\n");
1903 			ret = p->codec.mpeg4.level;
1904 		}
1905 		break;
1906 	case V4L2_CID_MPEG_VIDEO_H264_LOOP_FILTER_MODE:
1907 		p->codec.h264.loop_filter_mode = ctrl->val;
1908 		break;
1909 	case V4L2_CID_MPEG_VIDEO_H264_LOOP_FILTER_ALPHA:
1910 		p->codec.h264.loop_filter_alpha = ctrl->val;
1911 		break;
1912 	case V4L2_CID_MPEG_VIDEO_H264_LOOP_FILTER_BETA:
1913 		p->codec.h264.loop_filter_beta = ctrl->val;
1914 		break;
1915 	case V4L2_CID_MPEG_VIDEO_H264_ENTROPY_MODE:
1916 		p->codec.h264.entropy_mode = ctrl->val;
1917 		break;
1918 	case V4L2_CID_MPEG_MFC51_VIDEO_H264_NUM_REF_PIC_FOR_P:
1919 		p->codec.h264.num_ref_pic_4p = ctrl->val;
1920 		break;
1921 	case V4L2_CID_MPEG_VIDEO_H264_8X8_TRANSFORM:
1922 		p->codec.h264._8x8_transform = ctrl->val;
1923 		break;
1924 	case V4L2_CID_MPEG_VIDEO_MB_RC_ENABLE:
1925 		p->rc_mb = ctrl->val;
1926 		break;
1927 	case V4L2_CID_MPEG_VIDEO_H264_I_FRAME_QP:
1928 		p->codec.h264.rc_frame_qp = ctrl->val;
1929 		break;
1930 	case V4L2_CID_MPEG_VIDEO_H264_MIN_QP:
1931 		p->codec.h264.rc_min_qp = ctrl->val;
1932 		break;
1933 	case V4L2_CID_MPEG_VIDEO_H264_MAX_QP:
1934 		p->codec.h264.rc_max_qp = ctrl->val;
1935 		break;
1936 	case V4L2_CID_MPEG_VIDEO_H264_P_FRAME_QP:
1937 		p->codec.h264.rc_p_frame_qp = ctrl->val;
1938 		break;
1939 	case V4L2_CID_MPEG_VIDEO_H264_B_FRAME_QP:
1940 		p->codec.h264.rc_b_frame_qp = ctrl->val;
1941 		break;
1942 	case V4L2_CID_MPEG_VIDEO_MPEG4_I_FRAME_QP:
1943 	case V4L2_CID_MPEG_VIDEO_H263_I_FRAME_QP:
1944 		p->codec.mpeg4.rc_frame_qp = ctrl->val;
1945 		break;
1946 	case V4L2_CID_MPEG_VIDEO_MPEG4_MIN_QP:
1947 	case V4L2_CID_MPEG_VIDEO_H263_MIN_QP:
1948 		p->codec.mpeg4.rc_min_qp = ctrl->val;
1949 		break;
1950 	case V4L2_CID_MPEG_VIDEO_MPEG4_MAX_QP:
1951 	case V4L2_CID_MPEG_VIDEO_H263_MAX_QP:
1952 		p->codec.mpeg4.rc_max_qp = ctrl->val;
1953 		break;
1954 	case V4L2_CID_MPEG_VIDEO_MPEG4_P_FRAME_QP:
1955 	case V4L2_CID_MPEG_VIDEO_H263_P_FRAME_QP:
1956 		p->codec.mpeg4.rc_p_frame_qp = ctrl->val;
1957 		break;
1958 	case V4L2_CID_MPEG_VIDEO_MPEG4_B_FRAME_QP:
1959 	case V4L2_CID_MPEG_VIDEO_H263_B_FRAME_QP:
1960 		p->codec.mpeg4.rc_b_frame_qp = ctrl->val;
1961 		break;
1962 	case V4L2_CID_MPEG_MFC51_VIDEO_H264_ADAPTIVE_RC_DARK:
1963 		p->codec.h264.rc_mb_dark = ctrl->val;
1964 		break;
1965 	case V4L2_CID_MPEG_MFC51_VIDEO_H264_ADAPTIVE_RC_SMOOTH:
1966 		p->codec.h264.rc_mb_smooth = ctrl->val;
1967 		break;
1968 	case V4L2_CID_MPEG_MFC51_VIDEO_H264_ADAPTIVE_RC_STATIC:
1969 		p->codec.h264.rc_mb_static = ctrl->val;
1970 		break;
1971 	case V4L2_CID_MPEG_MFC51_VIDEO_H264_ADAPTIVE_RC_ACTIVITY:
1972 		p->codec.h264.rc_mb_activity = ctrl->val;
1973 		break;
1974 	case V4L2_CID_MPEG_VIDEO_H264_VUI_SAR_ENABLE:
1975 		p->codec.h264.vui_sar = ctrl->val;
1976 		break;
1977 	case V4L2_CID_MPEG_VIDEO_H264_VUI_SAR_IDC:
1978 		p->codec.h264.vui_sar_idc = vui_sar_idc(ctrl->val);
1979 		break;
1980 	case V4L2_CID_MPEG_VIDEO_H264_VUI_EXT_SAR_WIDTH:
1981 		p->codec.h264.vui_ext_sar_width = ctrl->val;
1982 		break;
1983 	case V4L2_CID_MPEG_VIDEO_H264_VUI_EXT_SAR_HEIGHT:
1984 		p->codec.h264.vui_ext_sar_height = ctrl->val;
1985 		break;
1986 	case V4L2_CID_MPEG_VIDEO_GOP_CLOSURE:
1987 		p->codec.h264.open_gop = !ctrl->val;
1988 		break;
1989 	case V4L2_CID_MPEG_VIDEO_H264_I_PERIOD:
1990 		p->codec.h264.open_gop_size = ctrl->val;
1991 		break;
1992 	case V4L2_CID_MPEG_VIDEO_MPEG4_PROFILE:
1993 		switch (ctrl->val) {
1994 		case V4L2_MPEG_VIDEO_MPEG4_PROFILE_SIMPLE:
1995 			p->codec.mpeg4.profile =
1996 				S5P_FIMV_ENC_PROFILE_MPEG4_SIMPLE;
1997 			break;
1998 		case V4L2_MPEG_VIDEO_MPEG4_PROFILE_ADVANCED_SIMPLE:
1999 			p->codec.mpeg4.profile =
2000 			S5P_FIMV_ENC_PROFILE_MPEG4_ADVANCED_SIMPLE;
2001 			break;
2002 		default:
2003 			ret = -EINVAL;
2004 		}
2005 		break;
2006 	case V4L2_CID_MPEG_VIDEO_MPEG4_QPEL:
2007 		p->codec.mpeg4.quarter_pixel = ctrl->val;
2008 		break;
2009 	case V4L2_CID_MPEG_VIDEO_VPX_NUM_PARTITIONS:
2010 		p->codec.vp8.num_partitions = ctrl->val;
2011 		break;
2012 	case V4L2_CID_MPEG_VIDEO_VPX_IMD_DISABLE_4X4:
2013 		p->codec.vp8.imd_4x4 = ctrl->val;
2014 		break;
2015 	case V4L2_CID_MPEG_VIDEO_VPX_NUM_REF_FRAMES:
2016 		p->codec.vp8.num_ref = ctrl->val;
2017 		break;
2018 	case V4L2_CID_MPEG_VIDEO_VPX_FILTER_LEVEL:
2019 		p->codec.vp8.filter_level = ctrl->val;
2020 		break;
2021 	case V4L2_CID_MPEG_VIDEO_VPX_FILTER_SHARPNESS:
2022 		p->codec.vp8.filter_sharpness = ctrl->val;
2023 		break;
2024 	case V4L2_CID_MPEG_VIDEO_VPX_GOLDEN_FRAME_REF_PERIOD:
2025 		p->codec.vp8.golden_frame_ref_period = ctrl->val;
2026 		break;
2027 	case V4L2_CID_MPEG_VIDEO_VPX_GOLDEN_FRAME_SEL:
2028 		p->codec.vp8.golden_frame_sel = ctrl->val;
2029 		break;
2030 	case V4L2_CID_MPEG_VIDEO_VPX_MIN_QP:
2031 		p->codec.vp8.rc_min_qp = ctrl->val;
2032 		break;
2033 	case V4L2_CID_MPEG_VIDEO_VPX_MAX_QP:
2034 		p->codec.vp8.rc_max_qp = ctrl->val;
2035 		break;
2036 	case V4L2_CID_MPEG_VIDEO_VPX_I_FRAME_QP:
2037 		p->codec.vp8.rc_frame_qp = ctrl->val;
2038 		break;
2039 	case V4L2_CID_MPEG_VIDEO_VPX_P_FRAME_QP:
2040 		p->codec.vp8.rc_p_frame_qp = ctrl->val;
2041 		break;
2042 	case V4L2_CID_MPEG_VIDEO_VP8_PROFILE:
2043 		p->codec.vp8.profile = ctrl->val;
2044 		break;
2045 	case V4L2_CID_MPEG_VIDEO_HEVC_I_FRAME_QP:
2046 		p->codec.hevc.rc_frame_qp = ctrl->val;
2047 		break;
2048 	case V4L2_CID_MPEG_VIDEO_HEVC_P_FRAME_QP:
2049 		p->codec.hevc.rc_p_frame_qp = ctrl->val;
2050 		break;
2051 	case V4L2_CID_MPEG_VIDEO_HEVC_B_FRAME_QP:
2052 		p->codec.hevc.rc_b_frame_qp = ctrl->val;
2053 		break;
2054 	case V4L2_CID_MPEG_VIDEO_HEVC_FRAME_RATE_RESOLUTION:
2055 		p->codec.hevc.rc_framerate = ctrl->val;
2056 		break;
2057 	case V4L2_CID_MPEG_VIDEO_HEVC_MIN_QP:
2058 		p->codec.hevc.rc_min_qp = ctrl->val;
2059 		__enc_update_hevc_qp_ctrls_range(ctx, ctrl->val,
2060 						 p->codec.hevc.rc_max_qp);
2061 		break;
2062 	case V4L2_CID_MPEG_VIDEO_HEVC_MAX_QP:
2063 		p->codec.hevc.rc_max_qp = ctrl->val;
2064 		__enc_update_hevc_qp_ctrls_range(ctx, p->codec.hevc.rc_min_qp,
2065 						 ctrl->val);
2066 		break;
2067 	case V4L2_CID_MPEG_VIDEO_HEVC_LEVEL:
2068 		p->codec.hevc.level_v4l2 = ctrl->val;
2069 		p->codec.hevc.level = hevc_level(ctrl->val);
2070 		break;
2071 	case V4L2_CID_MPEG_VIDEO_HEVC_PROFILE:
2072 		switch (ctrl->val) {
2073 		case V4L2_MPEG_VIDEO_HEVC_PROFILE_MAIN:
2074 			p->codec.hevc.profile =
2075 				V4L2_MPEG_VIDEO_HEVC_PROFILE_MAIN;
2076 			break;
2077 		case V4L2_MPEG_VIDEO_HEVC_PROFILE_MAIN_STILL_PICTURE:
2078 			p->codec.hevc.profile =
2079 			V4L2_MPEG_VIDEO_HEVC_PROFILE_MAIN_STILL_PICTURE;
2080 			break;
2081 		default:
2082 			ret = -EINVAL;
2083 		}
2084 		break;
2085 	case V4L2_CID_MPEG_VIDEO_HEVC_TIER:
2086 		p->codec.hevc.tier = ctrl->val;
2087 		break;
2088 	case V4L2_CID_MPEG_VIDEO_HEVC_MAX_PARTITION_DEPTH:
2089 		p->codec.hevc.max_partition_depth = ctrl->val;
2090 		break;
2091 	case V4L2_CID_MPEG_VIDEO_REF_NUMBER_FOR_PFRAMES:
2092 		p->codec.hevc.num_refs_for_p = ctrl->val;
2093 		break;
2094 	case V4L2_CID_MPEG_VIDEO_HEVC_REFRESH_TYPE:
2095 		p->codec.hevc.refreshtype = ctrl->val;
2096 		break;
2097 	case V4L2_CID_MPEG_VIDEO_HEVC_CONST_INTRA_PRED:
2098 		p->codec.hevc.const_intra_period_enable = ctrl->val;
2099 		break;
2100 	case V4L2_CID_MPEG_VIDEO_HEVC_LOSSLESS_CU:
2101 		p->codec.hevc.lossless_cu_enable = ctrl->val;
2102 		break;
2103 	case V4L2_CID_MPEG_VIDEO_HEVC_WAVEFRONT:
2104 		p->codec.hevc.wavefront_enable = ctrl->val;
2105 		break;
2106 	case V4L2_CID_MPEG_VIDEO_HEVC_LOOP_FILTER_MODE:
2107 		p->codec.hevc.loopfilter = ctrl->val;
2108 		break;
2109 	case V4L2_CID_MPEG_VIDEO_HEVC_HIER_QP:
2110 		p->codec.hevc.hier_qp_enable = ctrl->val;
2111 		break;
2112 	case V4L2_CID_MPEG_VIDEO_HEVC_HIER_CODING_TYPE:
2113 		p->codec.hevc.hier_qp_type = ctrl->val;
2114 		break;
2115 	case V4L2_CID_MPEG_VIDEO_HEVC_HIER_CODING_LAYER:
2116 		p->codec.hevc.num_hier_layer = ctrl->val;
2117 		break;
2118 	case V4L2_CID_MPEG_VIDEO_HEVC_HIER_CODING_L0_QP:
2119 		p->codec.hevc.hier_qp_layer[0] = ctrl->val;
2120 		break;
2121 	case V4L2_CID_MPEG_VIDEO_HEVC_HIER_CODING_L1_QP:
2122 		p->codec.hevc.hier_qp_layer[1] = ctrl->val;
2123 		break;
2124 	case V4L2_CID_MPEG_VIDEO_HEVC_HIER_CODING_L2_QP:
2125 		p->codec.hevc.hier_qp_layer[2] = ctrl->val;
2126 		break;
2127 	case V4L2_CID_MPEG_VIDEO_HEVC_HIER_CODING_L3_QP:
2128 		p->codec.hevc.hier_qp_layer[3] = ctrl->val;
2129 		break;
2130 	case V4L2_CID_MPEG_VIDEO_HEVC_HIER_CODING_L4_QP:
2131 		p->codec.hevc.hier_qp_layer[4] = ctrl->val;
2132 		break;
2133 	case V4L2_CID_MPEG_VIDEO_HEVC_HIER_CODING_L5_QP:
2134 		p->codec.hevc.hier_qp_layer[5] = ctrl->val;
2135 		break;
2136 	case V4L2_CID_MPEG_VIDEO_HEVC_HIER_CODING_L6_QP:
2137 		p->codec.hevc.hier_qp_layer[6] = ctrl->val;
2138 		break;
2139 	case V4L2_CID_MPEG_VIDEO_HEVC_HIER_CODING_L0_BR:
2140 		p->codec.hevc.hier_bit_layer[0] = ctrl->val;
2141 		break;
2142 	case V4L2_CID_MPEG_VIDEO_HEVC_HIER_CODING_L1_BR:
2143 		p->codec.hevc.hier_bit_layer[1] = ctrl->val;
2144 		break;
2145 	case V4L2_CID_MPEG_VIDEO_HEVC_HIER_CODING_L2_BR:
2146 		p->codec.hevc.hier_bit_layer[2] = ctrl->val;
2147 		break;
2148 	case V4L2_CID_MPEG_VIDEO_HEVC_HIER_CODING_L3_BR:
2149 		p->codec.hevc.hier_bit_layer[3] = ctrl->val;
2150 		break;
2151 	case V4L2_CID_MPEG_VIDEO_HEVC_HIER_CODING_L4_BR:
2152 		p->codec.hevc.hier_bit_layer[4] = ctrl->val;
2153 		break;
2154 	case V4L2_CID_MPEG_VIDEO_HEVC_HIER_CODING_L5_BR:
2155 		p->codec.hevc.hier_bit_layer[5] = ctrl->val;
2156 		break;
2157 	case V4L2_CID_MPEG_VIDEO_HEVC_HIER_CODING_L6_BR:
2158 		p->codec.hevc.hier_bit_layer[6] = ctrl->val;
2159 		break;
2160 	case V4L2_CID_MPEG_VIDEO_HEVC_GENERAL_PB:
2161 		p->codec.hevc.general_pb_enable = ctrl->val;
2162 		break;
2163 	case V4L2_CID_MPEG_VIDEO_HEVC_TEMPORAL_ID:
2164 		p->codec.hevc.temporal_id_enable = ctrl->val;
2165 		break;
2166 	case V4L2_CID_MPEG_VIDEO_HEVC_STRONG_SMOOTHING:
2167 		p->codec.hevc.strong_intra_smooth = ctrl->val;
2168 		break;
2169 	case V4L2_CID_MPEG_VIDEO_HEVC_INTRA_PU_SPLIT:
2170 		p->codec.hevc.intra_pu_split_disable = ctrl->val;
2171 		break;
2172 	case V4L2_CID_MPEG_VIDEO_HEVC_TMV_PREDICTION:
2173 		p->codec.hevc.tmv_prediction_disable = !ctrl->val;
2174 		break;
2175 	case V4L2_CID_MPEG_VIDEO_HEVC_MAX_NUM_MERGE_MV_MINUS1:
2176 		p->codec.hevc.max_num_merge_mv = ctrl->val;
2177 		break;
2178 	case V4L2_CID_MPEG_VIDEO_HEVC_WITHOUT_STARTCODE:
2179 		p->codec.hevc.encoding_nostartcode_enable = ctrl->val;
2180 		break;
2181 	case V4L2_CID_MPEG_VIDEO_HEVC_REFRESH_PERIOD:
2182 		p->codec.hevc.refreshperiod = ctrl->val;
2183 		break;
2184 	case V4L2_CID_MPEG_VIDEO_HEVC_LF_BETA_OFFSET_DIV2:
2185 		p->codec.hevc.lf_beta_offset_div2 = ctrl->val;
2186 		break;
2187 	case V4L2_CID_MPEG_VIDEO_HEVC_LF_TC_OFFSET_DIV2:
2188 		p->codec.hevc.lf_tc_offset_div2 = ctrl->val;
2189 		break;
2190 	case V4L2_CID_MPEG_VIDEO_HEVC_SIZE_OF_LENGTH_FIELD:
2191 		p->codec.hevc.size_of_length_field = ctrl->val;
2192 		break;
2193 	case V4L2_CID_MPEG_VIDEO_PREPEND_SPSPPS_TO_IDR:
2194 		p->codec.hevc.prepend_sps_pps_to_idr = ctrl->val;
2195 		break;
2196 	default:
2197 		v4l2_err(&dev->v4l2_dev, "Invalid control, id=%d, val=%d\n",
2198 							ctrl->id, ctrl->val);
2199 		ret = -EINVAL;
2200 	}
2201 	return ret;
2202 }
2203 
2204 static int s5p_mfc_enc_g_v_ctrl(struct v4l2_ctrl *ctrl)
2205 {
2206 	struct s5p_mfc_ctx *ctx = ctrl_to_ctx(ctrl);
2207 	struct s5p_mfc_dev *dev = ctx->dev;
2208 
2209 	switch (ctrl->id) {
2210 	case V4L2_CID_MIN_BUFFERS_FOR_OUTPUT:
2211 		if (ctx->state >= MFCINST_HEAD_PARSED &&
2212 		    ctx->state < MFCINST_ABORT) {
2213 			ctrl->val = ctx->pb_count;
2214 			break;
2215 		} else if (ctx->state != MFCINST_INIT) {
2216 			v4l2_err(&dev->v4l2_dev, "Encoding not initialised\n");
2217 			return -EINVAL;
2218 		}
2219 		/* Should wait for the header to be produced */
2220 		s5p_mfc_wait_for_done_ctx(ctx,
2221 				S5P_MFC_R2H_CMD_SEQ_DONE_RET, 0);
2222 		if (ctx->state >= MFCINST_HEAD_PARSED &&
2223 		    ctx->state < MFCINST_ABORT) {
2224 			ctrl->val = ctx->pb_count;
2225 		} else {
2226 			v4l2_err(&dev->v4l2_dev, "Encoding not initialised\n");
2227 			return -EINVAL;
2228 		}
2229 		break;
2230 	}
2231 	return 0;
2232 }
2233 
2234 static const struct v4l2_ctrl_ops s5p_mfc_enc_ctrl_ops = {
2235 	.s_ctrl = s5p_mfc_enc_s_ctrl,
2236 	.g_volatile_ctrl = s5p_mfc_enc_g_v_ctrl,
2237 };
2238 
2239 static int vidioc_s_parm(struct file *file, void *priv,
2240 			 struct v4l2_streamparm *a)
2241 {
2242 	struct s5p_mfc_ctx *ctx = fh_to_ctx(priv);
2243 
2244 	if (a->type == V4L2_BUF_TYPE_VIDEO_OUTPUT_MPLANE) {
2245 		ctx->enc_params.rc_framerate_num =
2246 					a->parm.output.timeperframe.denominator;
2247 		ctx->enc_params.rc_framerate_denom =
2248 					a->parm.output.timeperframe.numerator;
2249 	} else {
2250 		mfc_err("Setting FPS is only possible for the output queue\n");
2251 		return -EINVAL;
2252 	}
2253 	return 0;
2254 }
2255 
2256 static int vidioc_g_parm(struct file *file, void *priv,
2257 			 struct v4l2_streamparm *a)
2258 {
2259 	struct s5p_mfc_ctx *ctx = fh_to_ctx(priv);
2260 
2261 	if (a->type == V4L2_BUF_TYPE_VIDEO_OUTPUT) {
2262 		a->parm.output.timeperframe.denominator =
2263 					ctx->enc_params.rc_framerate_num;
2264 		a->parm.output.timeperframe.numerator =
2265 					ctx->enc_params.rc_framerate_denom;
2266 	} else {
2267 		mfc_err("Setting FPS is only possible for the output queue\n");
2268 		return -EINVAL;
2269 	}
2270 	return 0;
2271 }
2272 
2273 static int vidioc_encoder_cmd(struct file *file, void *priv,
2274 			      struct v4l2_encoder_cmd *cmd)
2275 {
2276 	struct s5p_mfc_ctx *ctx = fh_to_ctx(priv);
2277 	struct s5p_mfc_dev *dev = ctx->dev;
2278 	struct s5p_mfc_buf *buf;
2279 	unsigned long flags;
2280 
2281 	switch (cmd->cmd) {
2282 	case V4L2_ENC_CMD_STOP:
2283 		if (cmd->flags != 0)
2284 			return -EINVAL;
2285 
2286 		if (!ctx->vq_src.streaming)
2287 			return -EINVAL;
2288 
2289 		spin_lock_irqsave(&dev->irqlock, flags);
2290 		if (list_empty(&ctx->src_queue)) {
2291 			mfc_debug(2, "EOS: empty src queue, entering finishing state\n");
2292 			ctx->state = MFCINST_FINISHING;
2293 			if (s5p_mfc_ctx_ready(ctx))
2294 				set_work_bit_irqsave(ctx);
2295 			spin_unlock_irqrestore(&dev->irqlock, flags);
2296 			s5p_mfc_hw_call(dev->mfc_ops, try_run, dev);
2297 		} else {
2298 			mfc_debug(2, "EOS: marking last buffer of stream\n");
2299 			buf = list_entry(ctx->src_queue.prev,
2300 						struct s5p_mfc_buf, list);
2301 			if (buf->flags & MFC_BUF_FLAG_USED)
2302 				ctx->state = MFCINST_FINISHING;
2303 			else
2304 				buf->flags |= MFC_BUF_FLAG_EOS;
2305 			spin_unlock_irqrestore(&dev->irqlock, flags);
2306 		}
2307 		break;
2308 	default:
2309 		return -EINVAL;
2310 
2311 	}
2312 	return 0;
2313 }
2314 
2315 static int vidioc_subscribe_event(struct v4l2_fh *fh,
2316 				  const struct v4l2_event_subscription *sub)
2317 {
2318 	switch (sub->type) {
2319 	case V4L2_EVENT_EOS:
2320 		return v4l2_event_subscribe(fh, sub, 2, NULL);
2321 	default:
2322 		return -EINVAL;
2323 	}
2324 }
2325 
2326 static const struct v4l2_ioctl_ops s5p_mfc_enc_ioctl_ops = {
2327 	.vidioc_querycap = vidioc_querycap,
2328 	.vidioc_enum_fmt_vid_cap = vidioc_enum_fmt_vid_cap,
2329 	.vidioc_enum_fmt_vid_out = vidioc_enum_fmt_vid_out,
2330 	.vidioc_g_fmt_vid_cap_mplane = vidioc_g_fmt,
2331 	.vidioc_g_fmt_vid_out_mplane = vidioc_g_fmt,
2332 	.vidioc_try_fmt_vid_cap_mplane = vidioc_try_fmt,
2333 	.vidioc_try_fmt_vid_out_mplane = vidioc_try_fmt,
2334 	.vidioc_s_fmt_vid_cap_mplane = vidioc_s_fmt,
2335 	.vidioc_s_fmt_vid_out_mplane = vidioc_s_fmt,
2336 	.vidioc_reqbufs = vidioc_reqbufs,
2337 	.vidioc_querybuf = vidioc_querybuf,
2338 	.vidioc_qbuf = vidioc_qbuf,
2339 	.vidioc_dqbuf = vidioc_dqbuf,
2340 	.vidioc_expbuf = vidioc_expbuf,
2341 	.vidioc_streamon = vidioc_streamon,
2342 	.vidioc_streamoff = vidioc_streamoff,
2343 	.vidioc_s_parm = vidioc_s_parm,
2344 	.vidioc_g_parm = vidioc_g_parm,
2345 	.vidioc_encoder_cmd = vidioc_encoder_cmd,
2346 	.vidioc_subscribe_event = vidioc_subscribe_event,
2347 	.vidioc_unsubscribe_event = v4l2_event_unsubscribe,
2348 };
2349 
2350 static int check_vb_with_fmt(struct s5p_mfc_fmt *fmt, struct vb2_buffer *vb)
2351 {
2352 	int i;
2353 
2354 	if (!fmt)
2355 		return -EINVAL;
2356 	if (fmt->num_planes != vb->num_planes) {
2357 		mfc_err("invalid plane number for the format\n");
2358 		return -EINVAL;
2359 	}
2360 	for (i = 0; i < fmt->num_planes; i++) {
2361 		dma_addr_t dma = vb2_dma_contig_plane_dma_addr(vb, i);
2362 		if (!dma) {
2363 			mfc_err("failed to get plane cookie\n");
2364 			return -EINVAL;
2365 		}
2366 		mfc_debug(2, "index: %d, plane[%d] cookie: %pad\n",
2367 			  vb->index, i, &dma);
2368 	}
2369 	return 0;
2370 }
2371 
2372 static int s5p_mfc_queue_setup(struct vb2_queue *vq,
2373 			unsigned int *buf_count, unsigned int *plane_count,
2374 			unsigned int psize[], struct device *alloc_devs[])
2375 {
2376 	struct s5p_mfc_ctx *ctx = fh_to_ctx(vq->drv_priv);
2377 	struct s5p_mfc_dev *dev = ctx->dev;
2378 
2379 	if (vq->type == V4L2_BUF_TYPE_VIDEO_CAPTURE_MPLANE) {
2380 		if (ctx->state != MFCINST_GOT_INST) {
2381 			mfc_err("invalid state: %d\n", ctx->state);
2382 			return -EINVAL;
2383 		}
2384 
2385 		if (ctx->dst_fmt)
2386 			*plane_count = ctx->dst_fmt->num_planes;
2387 		else
2388 			*plane_count = MFC_ENC_CAP_PLANE_COUNT;
2389 		if (*buf_count < 1)
2390 			*buf_count = 1;
2391 		if (*buf_count > MFC_MAX_BUFFERS)
2392 			*buf_count = MFC_MAX_BUFFERS;
2393 		psize[0] = ctx->enc_dst_buf_size;
2394 		alloc_devs[0] = ctx->dev->mem_dev[BANK_L_CTX];
2395 	} else if (vq->type == V4L2_BUF_TYPE_VIDEO_OUTPUT_MPLANE) {
2396 		if (ctx->src_fmt)
2397 			*plane_count = ctx->src_fmt->num_planes;
2398 		else
2399 			*plane_count = MFC_ENC_OUT_PLANE_COUNT;
2400 
2401 		if (*buf_count < 1)
2402 			*buf_count = 1;
2403 		if (*buf_count > MFC_MAX_BUFFERS)
2404 			*buf_count = MFC_MAX_BUFFERS;
2405 
2406 		psize[0] = ctx->luma_size;
2407 		psize[1] = ctx->chroma_size;
2408 
2409 		if (IS_MFCV6_PLUS(dev)) {
2410 			alloc_devs[0] = ctx->dev->mem_dev[BANK_L_CTX];
2411 			alloc_devs[1] = ctx->dev->mem_dev[BANK_L_CTX];
2412 		} else {
2413 			alloc_devs[0] = ctx->dev->mem_dev[BANK_R_CTX];
2414 			alloc_devs[1] = ctx->dev->mem_dev[BANK_R_CTX];
2415 		}
2416 	} else {
2417 		mfc_err("invalid queue type: %d\n", vq->type);
2418 		return -EINVAL;
2419 	}
2420 	return 0;
2421 }
2422 
2423 static int s5p_mfc_buf_init(struct vb2_buffer *vb)
2424 {
2425 	struct vb2_v4l2_buffer *vbuf = to_vb2_v4l2_buffer(vb);
2426 	struct vb2_queue *vq = vb->vb2_queue;
2427 	struct s5p_mfc_ctx *ctx = fh_to_ctx(vq->drv_priv);
2428 	unsigned int i;
2429 	int ret;
2430 
2431 	if (vq->type == V4L2_BUF_TYPE_VIDEO_CAPTURE_MPLANE) {
2432 		ret = check_vb_with_fmt(ctx->dst_fmt, vb);
2433 		if (ret < 0)
2434 			return ret;
2435 		i = vb->index;
2436 		ctx->dst_bufs[i].b = vbuf;
2437 		ctx->dst_bufs[i].cookie.stream =
2438 					vb2_dma_contig_plane_dma_addr(vb, 0);
2439 		ctx->dst_bufs_cnt++;
2440 	} else if (vq->type == V4L2_BUF_TYPE_VIDEO_OUTPUT_MPLANE) {
2441 		ret = check_vb_with_fmt(ctx->src_fmt, vb);
2442 		if (ret < 0)
2443 			return ret;
2444 		i = vb->index;
2445 		ctx->src_bufs[i].b = vbuf;
2446 		ctx->src_bufs[i].cookie.raw.luma =
2447 					vb2_dma_contig_plane_dma_addr(vb, 0);
2448 		ctx->src_bufs[i].cookie.raw.chroma =
2449 					vb2_dma_contig_plane_dma_addr(vb, 1);
2450 		ctx->src_bufs_cnt++;
2451 	} else {
2452 		mfc_err("invalid queue type: %d\n", vq->type);
2453 		return -EINVAL;
2454 	}
2455 	return 0;
2456 }
2457 
2458 static int s5p_mfc_buf_prepare(struct vb2_buffer *vb)
2459 {
2460 	struct vb2_queue *vq = vb->vb2_queue;
2461 	struct s5p_mfc_ctx *ctx = fh_to_ctx(vq->drv_priv);
2462 	int ret;
2463 
2464 	if (vq->type == V4L2_BUF_TYPE_VIDEO_CAPTURE_MPLANE) {
2465 		ret = check_vb_with_fmt(ctx->dst_fmt, vb);
2466 		if (ret < 0)
2467 			return ret;
2468 		mfc_debug(2, "plane size: %ld, dst size: %zu\n",
2469 			vb2_plane_size(vb, 0), ctx->enc_dst_buf_size);
2470 		if (vb2_plane_size(vb, 0) < ctx->enc_dst_buf_size) {
2471 			mfc_err("plane size is too small for capture\n");
2472 			return -EINVAL;
2473 		}
2474 	} else if (vq->type == V4L2_BUF_TYPE_VIDEO_OUTPUT_MPLANE) {
2475 		ret = check_vb_with_fmt(ctx->src_fmt, vb);
2476 		if (ret < 0)
2477 			return ret;
2478 		mfc_debug(2, "plane size: %ld, luma size: %d\n",
2479 			vb2_plane_size(vb, 0), ctx->luma_size);
2480 		mfc_debug(2, "plane size: %ld, chroma size: %d\n",
2481 			vb2_plane_size(vb, 1), ctx->chroma_size);
2482 		if (vb2_plane_size(vb, 0) < ctx->luma_size ||
2483 		    vb2_plane_size(vb, 1) < ctx->chroma_size) {
2484 			mfc_err("plane size is too small for output\n");
2485 			return -EINVAL;
2486 		}
2487 	} else {
2488 		mfc_err("invalid queue type: %d\n", vq->type);
2489 		return -EINVAL;
2490 	}
2491 	return 0;
2492 }
2493 
2494 static int s5p_mfc_start_streaming(struct vb2_queue *q, unsigned int count)
2495 {
2496 	struct s5p_mfc_ctx *ctx = fh_to_ctx(q->drv_priv);
2497 	struct s5p_mfc_dev *dev = ctx->dev;
2498 
2499 	if (IS_MFCV6_PLUS(dev) &&
2500 			(q->type == V4L2_BUF_TYPE_VIDEO_CAPTURE_MPLANE)) {
2501 
2502 		if ((ctx->state == MFCINST_GOT_INST) &&
2503 			(dev->curr_ctx == ctx->num) && dev->hw_lock) {
2504 			s5p_mfc_wait_for_done_ctx(ctx,
2505 						S5P_MFC_R2H_CMD_SEQ_DONE_RET,
2506 						0);
2507 		}
2508 
2509 		if (ctx->src_bufs_cnt < ctx->pb_count) {
2510 			mfc_err("Need minimum %d OUTPUT buffers\n",
2511 					ctx->pb_count);
2512 			return -ENOBUFS;
2513 		}
2514 	}
2515 
2516 	/* If context is ready then dev = work->data;schedule it to run */
2517 	if (s5p_mfc_ctx_ready(ctx))
2518 		set_work_bit_irqsave(ctx);
2519 	s5p_mfc_hw_call(dev->mfc_ops, try_run, dev);
2520 
2521 	return 0;
2522 }
2523 
2524 static void s5p_mfc_stop_streaming(struct vb2_queue *q)
2525 {
2526 	unsigned long flags;
2527 	struct s5p_mfc_ctx *ctx = fh_to_ctx(q->drv_priv);
2528 	struct s5p_mfc_dev *dev = ctx->dev;
2529 
2530 	if ((ctx->state == MFCINST_FINISHING ||
2531 		ctx->state == MFCINST_RUNNING) &&
2532 		dev->curr_ctx == ctx->num && dev->hw_lock) {
2533 		ctx->state = MFCINST_ABORT;
2534 		s5p_mfc_wait_for_done_ctx(ctx, S5P_MFC_R2H_CMD_FRAME_DONE_RET,
2535 					  0);
2536 	}
2537 	ctx->state = MFCINST_FINISHED;
2538 	spin_lock_irqsave(&dev->irqlock, flags);
2539 	if (q->type == V4L2_BUF_TYPE_VIDEO_CAPTURE_MPLANE) {
2540 		s5p_mfc_cleanup_queue(&ctx->dst_queue, &ctx->vq_dst);
2541 		INIT_LIST_HEAD(&ctx->dst_queue);
2542 		ctx->dst_queue_cnt = 0;
2543 	}
2544 	if (q->type == V4L2_BUF_TYPE_VIDEO_OUTPUT_MPLANE) {
2545 		cleanup_ref_queue(ctx);
2546 		s5p_mfc_cleanup_queue(&ctx->src_queue, &ctx->vq_src);
2547 		INIT_LIST_HEAD(&ctx->src_queue);
2548 		ctx->src_queue_cnt = 0;
2549 	}
2550 	spin_unlock_irqrestore(&dev->irqlock, flags);
2551 }
2552 
2553 static void s5p_mfc_buf_queue(struct vb2_buffer *vb)
2554 {
2555 	struct vb2_queue *vq = vb->vb2_queue;
2556 	struct s5p_mfc_ctx *ctx = fh_to_ctx(vq->drv_priv);
2557 	struct s5p_mfc_dev *dev = ctx->dev;
2558 	unsigned long flags;
2559 	struct s5p_mfc_buf *mfc_buf;
2560 
2561 	if (ctx->state == MFCINST_ERROR) {
2562 		vb2_buffer_done(vb, VB2_BUF_STATE_ERROR);
2563 		cleanup_ref_queue(ctx);
2564 		return;
2565 	}
2566 	if (vq->type == V4L2_BUF_TYPE_VIDEO_CAPTURE_MPLANE) {
2567 		mfc_buf = &ctx->dst_bufs[vb->index];
2568 		mfc_buf->flags &= ~MFC_BUF_FLAG_USED;
2569 		/* Mark destination as available for use by MFC */
2570 		spin_lock_irqsave(&dev->irqlock, flags);
2571 		list_add_tail(&mfc_buf->list, &ctx->dst_queue);
2572 		ctx->dst_queue_cnt++;
2573 		spin_unlock_irqrestore(&dev->irqlock, flags);
2574 	} else if (vq->type == V4L2_BUF_TYPE_VIDEO_OUTPUT_MPLANE) {
2575 		mfc_buf = &ctx->src_bufs[vb->index];
2576 		mfc_buf->flags &= ~MFC_BUF_FLAG_USED;
2577 		spin_lock_irqsave(&dev->irqlock, flags);
2578 		list_add_tail(&mfc_buf->list, &ctx->src_queue);
2579 		ctx->src_queue_cnt++;
2580 		spin_unlock_irqrestore(&dev->irqlock, flags);
2581 	} else {
2582 		mfc_err("unsupported buffer type (%d)\n", vq->type);
2583 	}
2584 	if (s5p_mfc_ctx_ready(ctx))
2585 		set_work_bit_irqsave(ctx);
2586 	s5p_mfc_hw_call(dev->mfc_ops, try_run, dev);
2587 }
2588 
2589 static struct vb2_ops s5p_mfc_enc_qops = {
2590 	.queue_setup		= s5p_mfc_queue_setup,
2591 	.wait_prepare		= vb2_ops_wait_prepare,
2592 	.wait_finish		= vb2_ops_wait_finish,
2593 	.buf_init		= s5p_mfc_buf_init,
2594 	.buf_prepare		= s5p_mfc_buf_prepare,
2595 	.start_streaming	= s5p_mfc_start_streaming,
2596 	.stop_streaming		= s5p_mfc_stop_streaming,
2597 	.buf_queue		= s5p_mfc_buf_queue,
2598 };
2599 
2600 const struct s5p_mfc_codec_ops *get_enc_codec_ops(void)
2601 {
2602 	return &encoder_codec_ops;
2603 }
2604 
2605 struct vb2_ops *get_enc_queue_ops(void)
2606 {
2607 	return &s5p_mfc_enc_qops;
2608 }
2609 
2610 const struct v4l2_ioctl_ops *get_enc_v4l2_ioctl_ops(void)
2611 {
2612 	return &s5p_mfc_enc_ioctl_ops;
2613 }
2614 
2615 #define IS_MFC51_PRIV(x) ((V4L2_CTRL_ID2WHICH(x) == V4L2_CTRL_CLASS_CODEC) \
2616 						&& V4L2_CTRL_DRIVER_PRIV(x))
2617 
2618 int s5p_mfc_enc_ctrls_setup(struct s5p_mfc_ctx *ctx)
2619 {
2620 	struct v4l2_ctrl_config cfg;
2621 	int i;
2622 
2623 	v4l2_ctrl_handler_init(&ctx->ctrl_handler, NUM_CTRLS);
2624 	if (ctx->ctrl_handler.error) {
2625 		mfc_err("v4l2_ctrl_handler_init failed\n");
2626 		return ctx->ctrl_handler.error;
2627 	}
2628 	for (i = 0; i < NUM_CTRLS; i++) {
2629 		if (IS_MFC51_PRIV(controls[i].id)) {
2630 			memset(&cfg, 0, sizeof(struct v4l2_ctrl_config));
2631 			cfg.ops = &s5p_mfc_enc_ctrl_ops;
2632 			cfg.id = controls[i].id;
2633 			cfg.min = controls[i].minimum;
2634 			cfg.max = controls[i].maximum;
2635 			cfg.def = controls[i].default_value;
2636 			cfg.name = controls[i].name;
2637 			cfg.type = controls[i].type;
2638 			cfg.flags = 0;
2639 
2640 			if (cfg.type == V4L2_CTRL_TYPE_MENU) {
2641 				cfg.step = 0;
2642 				cfg.menu_skip_mask = controls[i].menu_skip_mask;
2643 				cfg.qmenu = mfc51_get_menu(cfg.id);
2644 			} else {
2645 				cfg.step = controls[i].step;
2646 				cfg.menu_skip_mask = 0;
2647 			}
2648 			ctx->ctrls[i] = v4l2_ctrl_new_custom(&ctx->ctrl_handler,
2649 					&cfg, NULL);
2650 		} else {
2651 			if ((controls[i].type == V4L2_CTRL_TYPE_MENU) ||
2652 				(controls[i].type ==
2653 					V4L2_CTRL_TYPE_INTEGER_MENU)) {
2654 				ctx->ctrls[i] = v4l2_ctrl_new_std_menu(
2655 					&ctx->ctrl_handler,
2656 					&s5p_mfc_enc_ctrl_ops, controls[i].id,
2657 					controls[i].maximum, 0,
2658 					controls[i].default_value);
2659 			} else {
2660 				ctx->ctrls[i] = v4l2_ctrl_new_std(
2661 					&ctx->ctrl_handler,
2662 					&s5p_mfc_enc_ctrl_ops, controls[i].id,
2663 					controls[i].minimum,
2664 					controls[i].maximum, controls[i].step,
2665 					controls[i].default_value);
2666 			}
2667 		}
2668 		if (ctx->ctrl_handler.error) {
2669 			mfc_err("Adding control (%d) failed\n", i);
2670 			return ctx->ctrl_handler.error;
2671 		}
2672 		if (controls[i].is_volatile && ctx->ctrls[i])
2673 			ctx->ctrls[i]->flags |= V4L2_CTRL_FLAG_VOLATILE;
2674 	}
2675 	v4l2_ctrl_handler_setup(&ctx->ctrl_handler);
2676 	return 0;
2677 }
2678 
2679 void s5p_mfc_enc_ctrls_delete(struct s5p_mfc_ctx *ctx)
2680 {
2681 	int i;
2682 
2683 	v4l2_ctrl_handler_free(&ctx->ctrl_handler);
2684 	for (i = 0; i < NUM_CTRLS; i++)
2685 		ctx->ctrls[i] = NULL;
2686 }
2687 
2688 void s5p_mfc_enc_init(struct s5p_mfc_ctx *ctx)
2689 {
2690 	struct v4l2_format f;
2691 	f.fmt.pix_mp.pixelformat = DEF_SRC_FMT_ENC;
2692 	ctx->src_fmt = find_format(&f, MFC_FMT_RAW);
2693 	f.fmt.pix_mp.pixelformat = DEF_DST_FMT_ENC;
2694 	ctx->dst_fmt = find_format(&f, MFC_FMT_ENC);
2695 }
2696