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 	snprintf(cap->bus_info, sizeof(cap->bus_info), "platform:%s",
1313 		 dev_name(&dev->plat_dev->dev));
1314 	return 0;
1315 }
1316 
1317 static int vidioc_enum_fmt(struct file *file, struct v4l2_fmtdesc *f,
1318 							bool out)
1319 {
1320 	struct s5p_mfc_dev *dev = video_drvdata(file);
1321 	int i, j = 0;
1322 
1323 	for (i = 0; i < ARRAY_SIZE(formats); ++i) {
1324 		if (out && formats[i].type != MFC_FMT_RAW)
1325 			continue;
1326 		else if (!out && formats[i].type != MFC_FMT_ENC)
1327 			continue;
1328 		else if ((dev->variant->version_bit & formats[i].versions) == 0)
1329 			continue;
1330 
1331 		if (j == f->index) {
1332 			f->pixelformat = formats[i].fourcc;
1333 			return 0;
1334 		}
1335 		++j;
1336 	}
1337 	return -EINVAL;
1338 }
1339 
1340 static int vidioc_enum_fmt_vid_cap(struct file *file, void *pirv,
1341 				   struct v4l2_fmtdesc *f)
1342 {
1343 	return vidioc_enum_fmt(file, f, false);
1344 }
1345 
1346 static int vidioc_enum_fmt_vid_out(struct file *file, void *priv,
1347 				   struct v4l2_fmtdesc *f)
1348 {
1349 	return vidioc_enum_fmt(file, f, true);
1350 }
1351 
1352 static int vidioc_g_fmt(struct file *file, void *priv, struct v4l2_format *f)
1353 {
1354 	struct s5p_mfc_ctx *ctx = fh_to_ctx(priv);
1355 	struct v4l2_pix_format_mplane *pix_fmt_mp = &f->fmt.pix_mp;
1356 
1357 	mfc_debug(2, "f->type = %d ctx->state = %d\n", f->type, ctx->state);
1358 	if (f->type == V4L2_BUF_TYPE_VIDEO_CAPTURE_MPLANE) {
1359 		/* This is run on output (encoder dest) */
1360 		pix_fmt_mp->width = 0;
1361 		pix_fmt_mp->height = 0;
1362 		pix_fmt_mp->field = V4L2_FIELD_NONE;
1363 		pix_fmt_mp->pixelformat = ctx->dst_fmt->fourcc;
1364 		pix_fmt_mp->num_planes = ctx->dst_fmt->num_planes;
1365 
1366 		pix_fmt_mp->plane_fmt[0].bytesperline = ctx->enc_dst_buf_size;
1367 		pix_fmt_mp->plane_fmt[0].sizeimage = ctx->enc_dst_buf_size;
1368 	} else if (f->type == V4L2_BUF_TYPE_VIDEO_OUTPUT_MPLANE) {
1369 		/* This is run on capture (encoder src) */
1370 		pix_fmt_mp->width = ctx->img_width;
1371 		pix_fmt_mp->height = ctx->img_height;
1372 
1373 		pix_fmt_mp->field = V4L2_FIELD_NONE;
1374 		pix_fmt_mp->pixelformat = ctx->src_fmt->fourcc;
1375 		pix_fmt_mp->num_planes = ctx->src_fmt->num_planes;
1376 
1377 		pix_fmt_mp->plane_fmt[0].bytesperline = ctx->buf_width;
1378 		pix_fmt_mp->plane_fmt[0].sizeimage = ctx->luma_size;
1379 		pix_fmt_mp->plane_fmt[1].bytesperline = ctx->buf_width;
1380 		pix_fmt_mp->plane_fmt[1].sizeimage = ctx->chroma_size;
1381 	} else {
1382 		mfc_err("invalid buf type\n");
1383 		return -EINVAL;
1384 	}
1385 	return 0;
1386 }
1387 
1388 static int vidioc_try_fmt(struct file *file, void *priv, struct v4l2_format *f)
1389 {
1390 	struct s5p_mfc_dev *dev = video_drvdata(file);
1391 	struct s5p_mfc_fmt *fmt;
1392 	struct v4l2_pix_format_mplane *pix_fmt_mp = &f->fmt.pix_mp;
1393 
1394 	if (f->type == V4L2_BUF_TYPE_VIDEO_CAPTURE_MPLANE) {
1395 		fmt = find_format(f, MFC_FMT_ENC);
1396 		if (!fmt) {
1397 			mfc_err("failed to try output format\n");
1398 			return -EINVAL;
1399 		}
1400 		if ((dev->variant->version_bit & fmt->versions) == 0) {
1401 			mfc_err("Unsupported format by this MFC version.\n");
1402 			return -EINVAL;
1403 		}
1404 
1405 		pix_fmt_mp->plane_fmt[0].bytesperline =
1406 			pix_fmt_mp->plane_fmt[0].sizeimage;
1407 	} else if (f->type == V4L2_BUF_TYPE_VIDEO_OUTPUT_MPLANE) {
1408 		fmt = find_format(f, MFC_FMT_RAW);
1409 		if (!fmt) {
1410 			mfc_err("failed to try output format\n");
1411 			return -EINVAL;
1412 		}
1413 		if ((dev->variant->version_bit & fmt->versions) == 0) {
1414 			mfc_err("Unsupported format by this MFC version.\n");
1415 			return -EINVAL;
1416 		}
1417 
1418 		v4l_bound_align_image(&pix_fmt_mp->width, 8, 1920, 1,
1419 			&pix_fmt_mp->height, 4, 1080, 1, 0);
1420 	} else {
1421 		mfc_err("invalid buf type\n");
1422 		return -EINVAL;
1423 	}
1424 	return 0;
1425 }
1426 
1427 static int vidioc_s_fmt(struct file *file, void *priv, struct v4l2_format *f)
1428 {
1429 	struct s5p_mfc_dev *dev = video_drvdata(file);
1430 	struct s5p_mfc_ctx *ctx = fh_to_ctx(priv);
1431 	struct v4l2_pix_format_mplane *pix_fmt_mp = &f->fmt.pix_mp;
1432 	int ret = 0;
1433 
1434 	ret = vidioc_try_fmt(file, priv, f);
1435 	if (ret)
1436 		return ret;
1437 	if (ctx->vq_src.streaming || ctx->vq_dst.streaming) {
1438 		v4l2_err(&dev->v4l2_dev, "%s queue busy\n", __func__);
1439 		ret = -EBUSY;
1440 		goto out;
1441 	}
1442 	if (f->type == V4L2_BUF_TYPE_VIDEO_CAPTURE_MPLANE) {
1443 		/* dst_fmt is validated by call to vidioc_try_fmt */
1444 		ctx->dst_fmt = find_format(f, MFC_FMT_ENC);
1445 		ctx->state = MFCINST_INIT;
1446 		ctx->codec_mode = ctx->dst_fmt->codec_mode;
1447 		ctx->enc_dst_buf_size =	pix_fmt_mp->plane_fmt[0].sizeimage;
1448 		pix_fmt_mp->plane_fmt[0].bytesperline = 0;
1449 		ctx->dst_bufs_cnt = 0;
1450 		ctx->capture_state = QUEUE_FREE;
1451 		ret = s5p_mfc_open_mfc_inst(dev, ctx);
1452 	} else if (f->type == V4L2_BUF_TYPE_VIDEO_OUTPUT_MPLANE) {
1453 		/* src_fmt is validated by call to vidioc_try_fmt */
1454 		ctx->src_fmt = find_format(f, MFC_FMT_RAW);
1455 		ctx->img_width = pix_fmt_mp->width;
1456 		ctx->img_height = pix_fmt_mp->height;
1457 		mfc_debug(2, "codec number: %d\n", ctx->src_fmt->codec_mode);
1458 		mfc_debug(2, "fmt - w: %d, h: %d, ctx - w: %d, h: %d\n",
1459 			pix_fmt_mp->width, pix_fmt_mp->height,
1460 			ctx->img_width, ctx->img_height);
1461 
1462 		s5p_mfc_hw_call(dev->mfc_ops, enc_calc_src_size, ctx);
1463 		pix_fmt_mp->plane_fmt[0].sizeimage = ctx->luma_size;
1464 		pix_fmt_mp->plane_fmt[0].bytesperline = ctx->buf_width;
1465 		pix_fmt_mp->plane_fmt[1].sizeimage = ctx->chroma_size;
1466 		pix_fmt_mp->plane_fmt[1].bytesperline = ctx->buf_width;
1467 
1468 		ctx->src_bufs_cnt = 0;
1469 		ctx->output_state = QUEUE_FREE;
1470 	} else {
1471 		mfc_err("invalid buf type\n");
1472 		ret = -EINVAL;
1473 	}
1474 out:
1475 	mfc_debug_leave();
1476 	return ret;
1477 }
1478 
1479 static int vidioc_reqbufs(struct file *file, void *priv,
1480 					  struct v4l2_requestbuffers *reqbufs)
1481 {
1482 	struct s5p_mfc_dev *dev = video_drvdata(file);
1483 	struct s5p_mfc_ctx *ctx = fh_to_ctx(priv);
1484 	int ret = 0;
1485 
1486 	/* if memory is not mmp or userptr return error */
1487 	if ((reqbufs->memory != V4L2_MEMORY_MMAP) &&
1488 		(reqbufs->memory != V4L2_MEMORY_USERPTR))
1489 		return -EINVAL;
1490 	if (reqbufs->type == V4L2_BUF_TYPE_VIDEO_CAPTURE_MPLANE) {
1491 		if (reqbufs->count == 0) {
1492 			mfc_debug(2, "Freeing buffers\n");
1493 			ret = vb2_reqbufs(&ctx->vq_dst, reqbufs);
1494 			s5p_mfc_hw_call(dev->mfc_ops, release_codec_buffers,
1495 					ctx);
1496 			ctx->capture_state = QUEUE_FREE;
1497 			return ret;
1498 		}
1499 		if (ctx->capture_state != QUEUE_FREE) {
1500 			mfc_err("invalid capture state: %d\n",
1501 							ctx->capture_state);
1502 			return -EINVAL;
1503 		}
1504 		ret = vb2_reqbufs(&ctx->vq_dst, reqbufs);
1505 		if (ret != 0) {
1506 			mfc_err("error in vb2_reqbufs() for E(D)\n");
1507 			return ret;
1508 		}
1509 		ctx->capture_state = QUEUE_BUFS_REQUESTED;
1510 
1511 		ret = s5p_mfc_hw_call(ctx->dev->mfc_ops,
1512 				alloc_codec_buffers, ctx);
1513 		if (ret) {
1514 			mfc_err("Failed to allocate encoding buffers\n");
1515 			reqbufs->count = 0;
1516 			ret = vb2_reqbufs(&ctx->vq_dst, reqbufs);
1517 			return -ENOMEM;
1518 		}
1519 	} else if (reqbufs->type == V4L2_BUF_TYPE_VIDEO_OUTPUT_MPLANE) {
1520 		if (reqbufs->count == 0) {
1521 			mfc_debug(2, "Freeing buffers\n");
1522 			ret = vb2_reqbufs(&ctx->vq_src, reqbufs);
1523 			s5p_mfc_hw_call(dev->mfc_ops, release_codec_buffers,
1524 					ctx);
1525 			ctx->output_state = QUEUE_FREE;
1526 			return ret;
1527 		}
1528 		if (ctx->output_state != QUEUE_FREE) {
1529 			mfc_err("invalid output state: %d\n",
1530 							ctx->output_state);
1531 			return -EINVAL;
1532 		}
1533 
1534 		if (IS_MFCV6_PLUS(dev)) {
1535 			/* Check for min encoder buffers */
1536 			if (ctx->pb_count &&
1537 				(reqbufs->count < ctx->pb_count)) {
1538 				reqbufs->count = ctx->pb_count;
1539 				mfc_debug(2, "Minimum %d output buffers needed\n",
1540 						ctx->pb_count);
1541 			} else {
1542 				ctx->pb_count = reqbufs->count;
1543 			}
1544 		}
1545 
1546 		ret = vb2_reqbufs(&ctx->vq_src, reqbufs);
1547 		if (ret != 0) {
1548 			mfc_err("error in vb2_reqbufs() for E(S)\n");
1549 			return ret;
1550 		}
1551 		ctx->output_state = QUEUE_BUFS_REQUESTED;
1552 	} else {
1553 		mfc_err("invalid buf type\n");
1554 		return -EINVAL;
1555 	}
1556 	return ret;
1557 }
1558 
1559 static int vidioc_querybuf(struct file *file, void *priv,
1560 						   struct v4l2_buffer *buf)
1561 {
1562 	struct s5p_mfc_ctx *ctx = fh_to_ctx(priv);
1563 	int ret = 0;
1564 
1565 	/* if memory is not mmp or userptr return error */
1566 	if ((buf->memory != V4L2_MEMORY_MMAP) &&
1567 		(buf->memory != V4L2_MEMORY_USERPTR))
1568 		return -EINVAL;
1569 	if (buf->type == V4L2_BUF_TYPE_VIDEO_CAPTURE_MPLANE) {
1570 		if (ctx->state != MFCINST_GOT_INST) {
1571 			mfc_err("invalid context state: %d\n", ctx->state);
1572 			return -EINVAL;
1573 		}
1574 		ret = vb2_querybuf(&ctx->vq_dst, buf);
1575 		if (ret != 0) {
1576 			mfc_err("error in vb2_querybuf() for E(D)\n");
1577 			return ret;
1578 		}
1579 		buf->m.planes[0].m.mem_offset += DST_QUEUE_OFF_BASE;
1580 	} else if (buf->type == V4L2_BUF_TYPE_VIDEO_OUTPUT_MPLANE) {
1581 		ret = vb2_querybuf(&ctx->vq_src, buf);
1582 		if (ret != 0) {
1583 			mfc_err("error in vb2_querybuf() for E(S)\n");
1584 			return ret;
1585 		}
1586 	} else {
1587 		mfc_err("invalid buf type\n");
1588 		return -EINVAL;
1589 	}
1590 	return ret;
1591 }
1592 
1593 /* Queue a buffer */
1594 static int vidioc_qbuf(struct file *file, void *priv, struct v4l2_buffer *buf)
1595 {
1596 	struct s5p_mfc_ctx *ctx = fh_to_ctx(priv);
1597 
1598 	if (ctx->state == MFCINST_ERROR) {
1599 		mfc_err("Call on QBUF after unrecoverable error\n");
1600 		return -EIO;
1601 	}
1602 	if (buf->type == V4L2_BUF_TYPE_VIDEO_OUTPUT_MPLANE) {
1603 		if (ctx->state == MFCINST_FINISHING) {
1604 			mfc_err("Call on QBUF after EOS command\n");
1605 			return -EIO;
1606 		}
1607 		return vb2_qbuf(&ctx->vq_src, NULL, buf);
1608 	} else if (buf->type == V4L2_BUF_TYPE_VIDEO_CAPTURE_MPLANE) {
1609 		return vb2_qbuf(&ctx->vq_dst, NULL, buf);
1610 	}
1611 	return -EINVAL;
1612 }
1613 
1614 /* Dequeue a buffer */
1615 static int vidioc_dqbuf(struct file *file, void *priv, struct v4l2_buffer *buf)
1616 {
1617 	const struct v4l2_event ev = {
1618 		.type = V4L2_EVENT_EOS
1619 	};
1620 	struct s5p_mfc_ctx *ctx = fh_to_ctx(priv);
1621 	int ret;
1622 
1623 	if (ctx->state == MFCINST_ERROR) {
1624 		mfc_err_limited("Call on DQBUF after unrecoverable error\n");
1625 		return -EIO;
1626 	}
1627 	if (buf->type == V4L2_BUF_TYPE_VIDEO_OUTPUT_MPLANE) {
1628 		ret = vb2_dqbuf(&ctx->vq_src, buf, file->f_flags & O_NONBLOCK);
1629 	} else if (buf->type == V4L2_BUF_TYPE_VIDEO_CAPTURE_MPLANE) {
1630 		ret = vb2_dqbuf(&ctx->vq_dst, buf, file->f_flags & O_NONBLOCK);
1631 		if (ret == 0 && ctx->state == MFCINST_FINISHED
1632 					&& list_empty(&ctx->vq_dst.done_list))
1633 			v4l2_event_queue_fh(&ctx->fh, &ev);
1634 	} else {
1635 		ret = -EINVAL;
1636 	}
1637 
1638 	return ret;
1639 }
1640 
1641 /* Export DMA buffer */
1642 static int vidioc_expbuf(struct file *file, void *priv,
1643 	struct v4l2_exportbuffer *eb)
1644 {
1645 	struct s5p_mfc_ctx *ctx = fh_to_ctx(priv);
1646 
1647 	if (eb->type == V4L2_BUF_TYPE_VIDEO_OUTPUT_MPLANE)
1648 		return vb2_expbuf(&ctx->vq_src, eb);
1649 	if (eb->type == V4L2_BUF_TYPE_VIDEO_CAPTURE_MPLANE)
1650 		return vb2_expbuf(&ctx->vq_dst, eb);
1651 	return -EINVAL;
1652 }
1653 
1654 /* Stream on */
1655 static int vidioc_streamon(struct file *file, void *priv,
1656 			   enum v4l2_buf_type type)
1657 {
1658 	struct s5p_mfc_ctx *ctx = fh_to_ctx(priv);
1659 
1660 	if (type == V4L2_BUF_TYPE_VIDEO_OUTPUT_MPLANE)
1661 		return vb2_streamon(&ctx->vq_src, type);
1662 	else if (type == V4L2_BUF_TYPE_VIDEO_CAPTURE_MPLANE)
1663 		return vb2_streamon(&ctx->vq_dst, type);
1664 	return -EINVAL;
1665 }
1666 
1667 /* Stream off, which equals to a pause */
1668 static int vidioc_streamoff(struct file *file, void *priv,
1669 			    enum v4l2_buf_type type)
1670 {
1671 	struct s5p_mfc_ctx *ctx = fh_to_ctx(priv);
1672 
1673 	if (type == V4L2_BUF_TYPE_VIDEO_OUTPUT_MPLANE)
1674 		return vb2_streamoff(&ctx->vq_src, type);
1675 	else if (type == V4L2_BUF_TYPE_VIDEO_CAPTURE_MPLANE)
1676 		return vb2_streamoff(&ctx->vq_dst, type);
1677 	return -EINVAL;
1678 }
1679 
1680 static inline int h264_level(enum v4l2_mpeg_video_h264_level lvl)
1681 {
1682 	static unsigned int t[V4L2_MPEG_VIDEO_H264_LEVEL_4_0 + 1] = {
1683 		/* V4L2_MPEG_VIDEO_H264_LEVEL_1_0   */ 10,
1684 		/* V4L2_MPEG_VIDEO_H264_LEVEL_1B    */ 9,
1685 		/* V4L2_MPEG_VIDEO_H264_LEVEL_1_1   */ 11,
1686 		/* V4L2_MPEG_VIDEO_H264_LEVEL_1_2   */ 12,
1687 		/* V4L2_MPEG_VIDEO_H264_LEVEL_1_3   */ 13,
1688 		/* V4L2_MPEG_VIDEO_H264_LEVEL_2_0   */ 20,
1689 		/* V4L2_MPEG_VIDEO_H264_LEVEL_2_1   */ 21,
1690 		/* V4L2_MPEG_VIDEO_H264_LEVEL_2_2   */ 22,
1691 		/* V4L2_MPEG_VIDEO_H264_LEVEL_3_0   */ 30,
1692 		/* V4L2_MPEG_VIDEO_H264_LEVEL_3_1   */ 31,
1693 		/* V4L2_MPEG_VIDEO_H264_LEVEL_3_2   */ 32,
1694 		/* V4L2_MPEG_VIDEO_H264_LEVEL_4_0   */ 40,
1695 	};
1696 	return t[lvl];
1697 }
1698 
1699 static inline int mpeg4_level(enum v4l2_mpeg_video_mpeg4_level lvl)
1700 {
1701 	static unsigned int t[V4L2_MPEG_VIDEO_MPEG4_LEVEL_5 + 1] = {
1702 		/* V4L2_MPEG_VIDEO_MPEG4_LEVEL_0    */ 0,
1703 		/* V4L2_MPEG_VIDEO_MPEG4_LEVEL_0B   */ 9,
1704 		/* V4L2_MPEG_VIDEO_MPEG4_LEVEL_1    */ 1,
1705 		/* V4L2_MPEG_VIDEO_MPEG4_LEVEL_2    */ 2,
1706 		/* V4L2_MPEG_VIDEO_MPEG4_LEVEL_3    */ 3,
1707 		/* V4L2_MPEG_VIDEO_MPEG4_LEVEL_3B   */ 7,
1708 		/* V4L2_MPEG_VIDEO_MPEG4_LEVEL_4    */ 4,
1709 		/* V4L2_MPEG_VIDEO_MPEG4_LEVEL_5    */ 5,
1710 	};
1711 	return t[lvl];
1712 }
1713 
1714 static inline int hevc_level(enum v4l2_mpeg_video_hevc_level lvl)
1715 {
1716 	static unsigned int t[] = {
1717 		/* V4L2_MPEG_VIDEO_HEVC_LEVEL_1    */ 10,
1718 		/* V4L2_MPEG_VIDEO_HEVC_LEVEL_2    */ 20,
1719 		/* V4L2_MPEG_VIDEO_HEVC_LEVEL_2_1  */ 21,
1720 		/* V4L2_MPEG_VIDEO_HEVC_LEVEL_3    */ 30,
1721 		/* V4L2_MPEG_VIDEO_HEVC_LEVEL_3_1  */ 31,
1722 		/* V4L2_MPEG_VIDEO_HEVC_LEVEL_4    */ 40,
1723 		/* V4L2_MPEG_VIDEO_HEVC_LEVEL_4_1  */ 41,
1724 		/* V4L2_MPEG_VIDEO_HEVC_LEVEL_5    */ 50,
1725 		/* V4L2_MPEG_VIDEO_HEVC_LEVEL_5_1  */ 51,
1726 		/* V4L2_MPEG_VIDEO_HEVC_LEVEL_5_2  */ 52,
1727 		/* V4L2_MPEG_VIDEO_HEVC_LEVEL_6    */ 60,
1728 		/* V4L2_MPEG_VIDEO_HEVC_LEVEL_6_1  */ 61,
1729 		/* V4L2_MPEG_VIDEO_HEVC_LEVEL_6_2  */ 62,
1730 	};
1731 	return t[lvl];
1732 }
1733 
1734 static inline int vui_sar_idc(enum v4l2_mpeg_video_h264_vui_sar_idc sar)
1735 {
1736 	static unsigned int t[V4L2_MPEG_VIDEO_H264_VUI_SAR_IDC_EXTENDED + 1] = {
1737 		/* V4L2_MPEG_VIDEO_H264_VUI_SAR_IDC_UNSPECIFIED     */ 0,
1738 		/* V4L2_MPEG_VIDEO_H264_VUI_SAR_IDC_1x1             */ 1,
1739 		/* V4L2_MPEG_VIDEO_H264_VUI_SAR_IDC_12x11           */ 2,
1740 		/* V4L2_MPEG_VIDEO_H264_VUI_SAR_IDC_10x11           */ 3,
1741 		/* V4L2_MPEG_VIDEO_H264_VUI_SAR_IDC_16x11           */ 4,
1742 		/* V4L2_MPEG_VIDEO_H264_VUI_SAR_IDC_40x33           */ 5,
1743 		/* V4L2_MPEG_VIDEO_H264_VUI_SAR_IDC_24x11           */ 6,
1744 		/* V4L2_MPEG_VIDEO_H264_VUI_SAR_IDC_20x11           */ 7,
1745 		/* V4L2_MPEG_VIDEO_H264_VUI_SAR_IDC_32x11           */ 8,
1746 		/* V4L2_MPEG_VIDEO_H264_VUI_SAR_IDC_80x33           */ 9,
1747 		/* V4L2_MPEG_VIDEO_H264_VUI_SAR_IDC_18x11           */ 10,
1748 		/* V4L2_MPEG_VIDEO_H264_VUI_SAR_IDC_15x11           */ 11,
1749 		/* V4L2_MPEG_VIDEO_H264_VUI_SAR_IDC_64x33           */ 12,
1750 		/* V4L2_MPEG_VIDEO_H264_VUI_SAR_IDC_160x99          */ 13,
1751 		/* V4L2_MPEG_VIDEO_H264_VUI_SAR_IDC_4x3             */ 14,
1752 		/* V4L2_MPEG_VIDEO_H264_VUI_SAR_IDC_3x2             */ 15,
1753 		/* V4L2_MPEG_VIDEO_H264_VUI_SAR_IDC_2x1             */ 16,
1754 		/* V4L2_MPEG_VIDEO_H264_VUI_SAR_IDC_EXTENDED        */ 255,
1755 	};
1756 	return t[sar];
1757 }
1758 
1759 /*
1760  * Update range of all HEVC quantization parameter controls that depend on the
1761  * V4L2_CID_MPEG_VIDEO_HEVC_MIN_QP, V4L2_CID_MPEG_VIDEO_HEVC_MAX_QP controls.
1762  */
1763 static void __enc_update_hevc_qp_ctrls_range(struct s5p_mfc_ctx *ctx,
1764 					     int min, int max)
1765 {
1766 	static const int __hevc_qp_ctrls[] = {
1767 		V4L2_CID_MPEG_VIDEO_HEVC_I_FRAME_QP,
1768 		V4L2_CID_MPEG_VIDEO_HEVC_P_FRAME_QP,
1769 		V4L2_CID_MPEG_VIDEO_HEVC_B_FRAME_QP,
1770 		V4L2_CID_MPEG_VIDEO_HEVC_HIER_CODING_L0_QP,
1771 		V4L2_CID_MPEG_VIDEO_HEVC_HIER_CODING_L1_QP,
1772 		V4L2_CID_MPEG_VIDEO_HEVC_HIER_CODING_L2_QP,
1773 		V4L2_CID_MPEG_VIDEO_HEVC_HIER_CODING_L3_QP,
1774 		V4L2_CID_MPEG_VIDEO_HEVC_HIER_CODING_L4_QP,
1775 		V4L2_CID_MPEG_VIDEO_HEVC_HIER_CODING_L5_QP,
1776 		V4L2_CID_MPEG_VIDEO_HEVC_HIER_CODING_L6_QP,
1777 	};
1778 	struct v4l2_ctrl *ctrl = NULL;
1779 	int i, j;
1780 
1781 	for (i = 0; i < ARRAY_SIZE(__hevc_qp_ctrls); i++) {
1782 		for (j = 0; j < ARRAY_SIZE(ctx->ctrls); j++) {
1783 			if (ctx->ctrls[j]->id == __hevc_qp_ctrls[i]) {
1784 				ctrl = ctx->ctrls[j];
1785 				break;
1786 			}
1787 		}
1788 		if (WARN_ON(!ctrl))
1789 			break;
1790 
1791 		__v4l2_ctrl_modify_range(ctrl, min, max, ctrl->step, min);
1792 	}
1793 }
1794 
1795 static int s5p_mfc_enc_s_ctrl(struct v4l2_ctrl *ctrl)
1796 {
1797 	struct s5p_mfc_ctx *ctx = ctrl_to_ctx(ctrl);
1798 	struct s5p_mfc_dev *dev = ctx->dev;
1799 	struct s5p_mfc_enc_params *p = &ctx->enc_params;
1800 	int ret = 0;
1801 
1802 	switch (ctrl->id) {
1803 	case V4L2_CID_MPEG_VIDEO_GOP_SIZE:
1804 		p->gop_size = ctrl->val;
1805 		break;
1806 	case V4L2_CID_MPEG_VIDEO_MULTI_SLICE_MODE:
1807 		p->slice_mode = ctrl->val;
1808 		break;
1809 	case V4L2_CID_MPEG_VIDEO_MULTI_SLICE_MAX_MB:
1810 		p->slice_mb = ctrl->val;
1811 		break;
1812 	case V4L2_CID_MPEG_VIDEO_MULTI_SLICE_MAX_BYTES:
1813 		p->slice_bit = ctrl->val * 8;
1814 		break;
1815 	case V4L2_CID_MPEG_VIDEO_CYCLIC_INTRA_REFRESH_MB:
1816 		p->intra_refresh_mb = ctrl->val;
1817 		break;
1818 	case V4L2_CID_MPEG_MFC51_VIDEO_PADDING:
1819 		p->pad = ctrl->val;
1820 		break;
1821 	case V4L2_CID_MPEG_MFC51_VIDEO_PADDING_YUV:
1822 		p->pad_luma = (ctrl->val >> 16) & 0xff;
1823 		p->pad_cb = (ctrl->val >> 8) & 0xff;
1824 		p->pad_cr = (ctrl->val >> 0) & 0xff;
1825 		break;
1826 	case V4L2_CID_MPEG_VIDEO_FRAME_RC_ENABLE:
1827 		p->rc_frame = ctrl->val;
1828 		break;
1829 	case V4L2_CID_MPEG_VIDEO_BITRATE:
1830 		p->rc_bitrate = ctrl->val;
1831 		break;
1832 	case V4L2_CID_MPEG_MFC51_VIDEO_RC_REACTION_COEFF:
1833 		p->rc_reaction_coeff = ctrl->val;
1834 		break;
1835 	case V4L2_CID_MPEG_MFC51_VIDEO_FORCE_FRAME_TYPE:
1836 		ctx->force_frame_type = ctrl->val;
1837 		break;
1838 	case V4L2_CID_MPEG_VIDEO_FORCE_KEY_FRAME:
1839 		ctx->force_frame_type =
1840 			V4L2_MPEG_MFC51_VIDEO_FORCE_FRAME_TYPE_I_FRAME;
1841 		break;
1842 	case V4L2_CID_MPEG_VIDEO_VBV_SIZE:
1843 		p->vbv_size = ctrl->val;
1844 		break;
1845 	case V4L2_CID_MPEG_VIDEO_MV_H_SEARCH_RANGE:
1846 		p->mv_h_range = ctrl->val;
1847 		break;
1848 	case V4L2_CID_MPEG_VIDEO_MV_V_SEARCH_RANGE:
1849 		p->mv_v_range = ctrl->val;
1850 		break;
1851 	case V4L2_CID_MPEG_VIDEO_H264_CPB_SIZE:
1852 		p->codec.h264.cpb_size = ctrl->val;
1853 		break;
1854 	case V4L2_CID_MPEG_VIDEO_HEADER_MODE:
1855 		p->seq_hdr_mode = ctrl->val;
1856 		break;
1857 	case V4L2_CID_MPEG_MFC51_VIDEO_FRAME_SKIP_MODE:
1858 	case V4L2_CID_MPEG_VIDEO_FRAME_SKIP_MODE:
1859 		p->frame_skip_mode = ctrl->val;
1860 		break;
1861 	case V4L2_CID_MPEG_MFC51_VIDEO_RC_FIXED_TARGET_BIT:
1862 		p->fixed_target_bit = ctrl->val;
1863 		break;
1864 	case V4L2_CID_MPEG_VIDEO_B_FRAMES:
1865 		p->num_b_frame = ctrl->val;
1866 		break;
1867 	case V4L2_CID_MPEG_VIDEO_H264_PROFILE:
1868 		switch (ctrl->val) {
1869 		case V4L2_MPEG_VIDEO_H264_PROFILE_MAIN:
1870 			p->codec.h264.profile =
1871 					S5P_FIMV_ENC_PROFILE_H264_MAIN;
1872 			break;
1873 		case V4L2_MPEG_VIDEO_H264_PROFILE_HIGH:
1874 			p->codec.h264.profile =
1875 					S5P_FIMV_ENC_PROFILE_H264_HIGH;
1876 			break;
1877 		case V4L2_MPEG_VIDEO_H264_PROFILE_BASELINE:
1878 			p->codec.h264.profile =
1879 				S5P_FIMV_ENC_PROFILE_H264_BASELINE;
1880 			break;
1881 		case V4L2_MPEG_VIDEO_H264_PROFILE_CONSTRAINED_BASELINE:
1882 			if (IS_MFCV6_PLUS(dev))
1883 				p->codec.h264.profile =
1884 				S5P_FIMV_ENC_PROFILE_H264_CONSTRAINED_BASELINE;
1885 			else
1886 				ret = -EINVAL;
1887 			break;
1888 		default:
1889 			ret = -EINVAL;
1890 		}
1891 		break;
1892 	case V4L2_CID_MPEG_VIDEO_H264_LEVEL:
1893 		p->codec.h264.level_v4l2 = ctrl->val;
1894 		p->codec.h264.level = h264_level(ctrl->val);
1895 		if (p->codec.h264.level < 0) {
1896 			mfc_err("Level number is wrong\n");
1897 			ret = p->codec.h264.level;
1898 		}
1899 		break;
1900 	case V4L2_CID_MPEG_VIDEO_MPEG4_LEVEL:
1901 		p->codec.mpeg4.level_v4l2 = ctrl->val;
1902 		p->codec.mpeg4.level = mpeg4_level(ctrl->val);
1903 		if (p->codec.mpeg4.level < 0) {
1904 			mfc_err("Level number is wrong\n");
1905 			ret = p->codec.mpeg4.level;
1906 		}
1907 		break;
1908 	case V4L2_CID_MPEG_VIDEO_H264_LOOP_FILTER_MODE:
1909 		p->codec.h264.loop_filter_mode = ctrl->val;
1910 		break;
1911 	case V4L2_CID_MPEG_VIDEO_H264_LOOP_FILTER_ALPHA:
1912 		p->codec.h264.loop_filter_alpha = ctrl->val;
1913 		break;
1914 	case V4L2_CID_MPEG_VIDEO_H264_LOOP_FILTER_BETA:
1915 		p->codec.h264.loop_filter_beta = ctrl->val;
1916 		break;
1917 	case V4L2_CID_MPEG_VIDEO_H264_ENTROPY_MODE:
1918 		p->codec.h264.entropy_mode = ctrl->val;
1919 		break;
1920 	case V4L2_CID_MPEG_MFC51_VIDEO_H264_NUM_REF_PIC_FOR_P:
1921 		p->codec.h264.num_ref_pic_4p = ctrl->val;
1922 		break;
1923 	case V4L2_CID_MPEG_VIDEO_H264_8X8_TRANSFORM:
1924 		p->codec.h264._8x8_transform = ctrl->val;
1925 		break;
1926 	case V4L2_CID_MPEG_VIDEO_MB_RC_ENABLE:
1927 		p->rc_mb = ctrl->val;
1928 		break;
1929 	case V4L2_CID_MPEG_VIDEO_H264_I_FRAME_QP:
1930 		p->codec.h264.rc_frame_qp = ctrl->val;
1931 		break;
1932 	case V4L2_CID_MPEG_VIDEO_H264_MIN_QP:
1933 		p->codec.h264.rc_min_qp = ctrl->val;
1934 		break;
1935 	case V4L2_CID_MPEG_VIDEO_H264_MAX_QP:
1936 		p->codec.h264.rc_max_qp = ctrl->val;
1937 		break;
1938 	case V4L2_CID_MPEG_VIDEO_H264_P_FRAME_QP:
1939 		p->codec.h264.rc_p_frame_qp = ctrl->val;
1940 		break;
1941 	case V4L2_CID_MPEG_VIDEO_H264_B_FRAME_QP:
1942 		p->codec.h264.rc_b_frame_qp = ctrl->val;
1943 		break;
1944 	case V4L2_CID_MPEG_VIDEO_MPEG4_I_FRAME_QP:
1945 	case V4L2_CID_MPEG_VIDEO_H263_I_FRAME_QP:
1946 		p->codec.mpeg4.rc_frame_qp = ctrl->val;
1947 		break;
1948 	case V4L2_CID_MPEG_VIDEO_MPEG4_MIN_QP:
1949 	case V4L2_CID_MPEG_VIDEO_H263_MIN_QP:
1950 		p->codec.mpeg4.rc_min_qp = ctrl->val;
1951 		break;
1952 	case V4L2_CID_MPEG_VIDEO_MPEG4_MAX_QP:
1953 	case V4L2_CID_MPEG_VIDEO_H263_MAX_QP:
1954 		p->codec.mpeg4.rc_max_qp = ctrl->val;
1955 		break;
1956 	case V4L2_CID_MPEG_VIDEO_MPEG4_P_FRAME_QP:
1957 	case V4L2_CID_MPEG_VIDEO_H263_P_FRAME_QP:
1958 		p->codec.mpeg4.rc_p_frame_qp = ctrl->val;
1959 		break;
1960 	case V4L2_CID_MPEG_VIDEO_MPEG4_B_FRAME_QP:
1961 	case V4L2_CID_MPEG_VIDEO_H263_B_FRAME_QP:
1962 		p->codec.mpeg4.rc_b_frame_qp = ctrl->val;
1963 		break;
1964 	case V4L2_CID_MPEG_MFC51_VIDEO_H264_ADAPTIVE_RC_DARK:
1965 		p->codec.h264.rc_mb_dark = ctrl->val;
1966 		break;
1967 	case V4L2_CID_MPEG_MFC51_VIDEO_H264_ADAPTIVE_RC_SMOOTH:
1968 		p->codec.h264.rc_mb_smooth = ctrl->val;
1969 		break;
1970 	case V4L2_CID_MPEG_MFC51_VIDEO_H264_ADAPTIVE_RC_STATIC:
1971 		p->codec.h264.rc_mb_static = ctrl->val;
1972 		break;
1973 	case V4L2_CID_MPEG_MFC51_VIDEO_H264_ADAPTIVE_RC_ACTIVITY:
1974 		p->codec.h264.rc_mb_activity = ctrl->val;
1975 		break;
1976 	case V4L2_CID_MPEG_VIDEO_H264_VUI_SAR_ENABLE:
1977 		p->codec.h264.vui_sar = ctrl->val;
1978 		break;
1979 	case V4L2_CID_MPEG_VIDEO_H264_VUI_SAR_IDC:
1980 		p->codec.h264.vui_sar_idc = vui_sar_idc(ctrl->val);
1981 		break;
1982 	case V4L2_CID_MPEG_VIDEO_H264_VUI_EXT_SAR_WIDTH:
1983 		p->codec.h264.vui_ext_sar_width = ctrl->val;
1984 		break;
1985 	case V4L2_CID_MPEG_VIDEO_H264_VUI_EXT_SAR_HEIGHT:
1986 		p->codec.h264.vui_ext_sar_height = ctrl->val;
1987 		break;
1988 	case V4L2_CID_MPEG_VIDEO_GOP_CLOSURE:
1989 		p->codec.h264.open_gop = !ctrl->val;
1990 		break;
1991 	case V4L2_CID_MPEG_VIDEO_H264_I_PERIOD:
1992 		p->codec.h264.open_gop_size = ctrl->val;
1993 		break;
1994 	case V4L2_CID_MPEG_VIDEO_MPEG4_PROFILE:
1995 		switch (ctrl->val) {
1996 		case V4L2_MPEG_VIDEO_MPEG4_PROFILE_SIMPLE:
1997 			p->codec.mpeg4.profile =
1998 				S5P_FIMV_ENC_PROFILE_MPEG4_SIMPLE;
1999 			break;
2000 		case V4L2_MPEG_VIDEO_MPEG4_PROFILE_ADVANCED_SIMPLE:
2001 			p->codec.mpeg4.profile =
2002 			S5P_FIMV_ENC_PROFILE_MPEG4_ADVANCED_SIMPLE;
2003 			break;
2004 		default:
2005 			ret = -EINVAL;
2006 		}
2007 		break;
2008 	case V4L2_CID_MPEG_VIDEO_MPEG4_QPEL:
2009 		p->codec.mpeg4.quarter_pixel = ctrl->val;
2010 		break;
2011 	case V4L2_CID_MPEG_VIDEO_VPX_NUM_PARTITIONS:
2012 		p->codec.vp8.num_partitions = ctrl->val;
2013 		break;
2014 	case V4L2_CID_MPEG_VIDEO_VPX_IMD_DISABLE_4X4:
2015 		p->codec.vp8.imd_4x4 = ctrl->val;
2016 		break;
2017 	case V4L2_CID_MPEG_VIDEO_VPX_NUM_REF_FRAMES:
2018 		p->codec.vp8.num_ref = ctrl->val;
2019 		break;
2020 	case V4L2_CID_MPEG_VIDEO_VPX_FILTER_LEVEL:
2021 		p->codec.vp8.filter_level = ctrl->val;
2022 		break;
2023 	case V4L2_CID_MPEG_VIDEO_VPX_FILTER_SHARPNESS:
2024 		p->codec.vp8.filter_sharpness = ctrl->val;
2025 		break;
2026 	case V4L2_CID_MPEG_VIDEO_VPX_GOLDEN_FRAME_REF_PERIOD:
2027 		p->codec.vp8.golden_frame_ref_period = ctrl->val;
2028 		break;
2029 	case V4L2_CID_MPEG_VIDEO_VPX_GOLDEN_FRAME_SEL:
2030 		p->codec.vp8.golden_frame_sel = ctrl->val;
2031 		break;
2032 	case V4L2_CID_MPEG_VIDEO_VPX_MIN_QP:
2033 		p->codec.vp8.rc_min_qp = ctrl->val;
2034 		break;
2035 	case V4L2_CID_MPEG_VIDEO_VPX_MAX_QP:
2036 		p->codec.vp8.rc_max_qp = ctrl->val;
2037 		break;
2038 	case V4L2_CID_MPEG_VIDEO_VPX_I_FRAME_QP:
2039 		p->codec.vp8.rc_frame_qp = ctrl->val;
2040 		break;
2041 	case V4L2_CID_MPEG_VIDEO_VPX_P_FRAME_QP:
2042 		p->codec.vp8.rc_p_frame_qp = ctrl->val;
2043 		break;
2044 	case V4L2_CID_MPEG_VIDEO_VP8_PROFILE:
2045 		p->codec.vp8.profile = ctrl->val;
2046 		break;
2047 	case V4L2_CID_MPEG_VIDEO_HEVC_I_FRAME_QP:
2048 		p->codec.hevc.rc_frame_qp = ctrl->val;
2049 		break;
2050 	case V4L2_CID_MPEG_VIDEO_HEVC_P_FRAME_QP:
2051 		p->codec.hevc.rc_p_frame_qp = ctrl->val;
2052 		break;
2053 	case V4L2_CID_MPEG_VIDEO_HEVC_B_FRAME_QP:
2054 		p->codec.hevc.rc_b_frame_qp = ctrl->val;
2055 		break;
2056 	case V4L2_CID_MPEG_VIDEO_HEVC_FRAME_RATE_RESOLUTION:
2057 		p->codec.hevc.rc_framerate = ctrl->val;
2058 		break;
2059 	case V4L2_CID_MPEG_VIDEO_HEVC_MIN_QP:
2060 		p->codec.hevc.rc_min_qp = ctrl->val;
2061 		__enc_update_hevc_qp_ctrls_range(ctx, ctrl->val,
2062 						 p->codec.hevc.rc_max_qp);
2063 		break;
2064 	case V4L2_CID_MPEG_VIDEO_HEVC_MAX_QP:
2065 		p->codec.hevc.rc_max_qp = ctrl->val;
2066 		__enc_update_hevc_qp_ctrls_range(ctx, p->codec.hevc.rc_min_qp,
2067 						 ctrl->val);
2068 		break;
2069 	case V4L2_CID_MPEG_VIDEO_HEVC_LEVEL:
2070 		p->codec.hevc.level_v4l2 = ctrl->val;
2071 		p->codec.hevc.level = hevc_level(ctrl->val);
2072 		break;
2073 	case V4L2_CID_MPEG_VIDEO_HEVC_PROFILE:
2074 		switch (ctrl->val) {
2075 		case V4L2_MPEG_VIDEO_HEVC_PROFILE_MAIN:
2076 			p->codec.hevc.profile =
2077 				V4L2_MPEG_VIDEO_HEVC_PROFILE_MAIN;
2078 			break;
2079 		case V4L2_MPEG_VIDEO_HEVC_PROFILE_MAIN_STILL_PICTURE:
2080 			p->codec.hevc.profile =
2081 			V4L2_MPEG_VIDEO_HEVC_PROFILE_MAIN_STILL_PICTURE;
2082 			break;
2083 		default:
2084 			ret = -EINVAL;
2085 		}
2086 		break;
2087 	case V4L2_CID_MPEG_VIDEO_HEVC_TIER:
2088 		p->codec.hevc.tier = ctrl->val;
2089 		break;
2090 	case V4L2_CID_MPEG_VIDEO_HEVC_MAX_PARTITION_DEPTH:
2091 		p->codec.hevc.max_partition_depth = ctrl->val;
2092 		break;
2093 	case V4L2_CID_MPEG_VIDEO_REF_NUMBER_FOR_PFRAMES:
2094 		p->codec.hevc.num_refs_for_p = ctrl->val;
2095 		break;
2096 	case V4L2_CID_MPEG_VIDEO_HEVC_REFRESH_TYPE:
2097 		p->codec.hevc.refreshtype = ctrl->val;
2098 		break;
2099 	case V4L2_CID_MPEG_VIDEO_HEVC_CONST_INTRA_PRED:
2100 		p->codec.hevc.const_intra_period_enable = ctrl->val;
2101 		break;
2102 	case V4L2_CID_MPEG_VIDEO_HEVC_LOSSLESS_CU:
2103 		p->codec.hevc.lossless_cu_enable = ctrl->val;
2104 		break;
2105 	case V4L2_CID_MPEG_VIDEO_HEVC_WAVEFRONT:
2106 		p->codec.hevc.wavefront_enable = ctrl->val;
2107 		break;
2108 	case V4L2_CID_MPEG_VIDEO_HEVC_LOOP_FILTER_MODE:
2109 		p->codec.hevc.loopfilter = ctrl->val;
2110 		break;
2111 	case V4L2_CID_MPEG_VIDEO_HEVC_HIER_QP:
2112 		p->codec.hevc.hier_qp_enable = ctrl->val;
2113 		break;
2114 	case V4L2_CID_MPEG_VIDEO_HEVC_HIER_CODING_TYPE:
2115 		p->codec.hevc.hier_qp_type = ctrl->val;
2116 		break;
2117 	case V4L2_CID_MPEG_VIDEO_HEVC_HIER_CODING_LAYER:
2118 		p->codec.hevc.num_hier_layer = ctrl->val;
2119 		break;
2120 	case V4L2_CID_MPEG_VIDEO_HEVC_HIER_CODING_L0_QP:
2121 		p->codec.hevc.hier_qp_layer[0] = ctrl->val;
2122 		break;
2123 	case V4L2_CID_MPEG_VIDEO_HEVC_HIER_CODING_L1_QP:
2124 		p->codec.hevc.hier_qp_layer[1] = ctrl->val;
2125 		break;
2126 	case V4L2_CID_MPEG_VIDEO_HEVC_HIER_CODING_L2_QP:
2127 		p->codec.hevc.hier_qp_layer[2] = ctrl->val;
2128 		break;
2129 	case V4L2_CID_MPEG_VIDEO_HEVC_HIER_CODING_L3_QP:
2130 		p->codec.hevc.hier_qp_layer[3] = ctrl->val;
2131 		break;
2132 	case V4L2_CID_MPEG_VIDEO_HEVC_HIER_CODING_L4_QP:
2133 		p->codec.hevc.hier_qp_layer[4] = ctrl->val;
2134 		break;
2135 	case V4L2_CID_MPEG_VIDEO_HEVC_HIER_CODING_L5_QP:
2136 		p->codec.hevc.hier_qp_layer[5] = ctrl->val;
2137 		break;
2138 	case V4L2_CID_MPEG_VIDEO_HEVC_HIER_CODING_L6_QP:
2139 		p->codec.hevc.hier_qp_layer[6] = ctrl->val;
2140 		break;
2141 	case V4L2_CID_MPEG_VIDEO_HEVC_HIER_CODING_L0_BR:
2142 		p->codec.hevc.hier_bit_layer[0] = ctrl->val;
2143 		break;
2144 	case V4L2_CID_MPEG_VIDEO_HEVC_HIER_CODING_L1_BR:
2145 		p->codec.hevc.hier_bit_layer[1] = ctrl->val;
2146 		break;
2147 	case V4L2_CID_MPEG_VIDEO_HEVC_HIER_CODING_L2_BR:
2148 		p->codec.hevc.hier_bit_layer[2] = ctrl->val;
2149 		break;
2150 	case V4L2_CID_MPEG_VIDEO_HEVC_HIER_CODING_L3_BR:
2151 		p->codec.hevc.hier_bit_layer[3] = ctrl->val;
2152 		break;
2153 	case V4L2_CID_MPEG_VIDEO_HEVC_HIER_CODING_L4_BR:
2154 		p->codec.hevc.hier_bit_layer[4] = ctrl->val;
2155 		break;
2156 	case V4L2_CID_MPEG_VIDEO_HEVC_HIER_CODING_L5_BR:
2157 		p->codec.hevc.hier_bit_layer[5] = ctrl->val;
2158 		break;
2159 	case V4L2_CID_MPEG_VIDEO_HEVC_HIER_CODING_L6_BR:
2160 		p->codec.hevc.hier_bit_layer[6] = ctrl->val;
2161 		break;
2162 	case V4L2_CID_MPEG_VIDEO_HEVC_GENERAL_PB:
2163 		p->codec.hevc.general_pb_enable = ctrl->val;
2164 		break;
2165 	case V4L2_CID_MPEG_VIDEO_HEVC_TEMPORAL_ID:
2166 		p->codec.hevc.temporal_id_enable = ctrl->val;
2167 		break;
2168 	case V4L2_CID_MPEG_VIDEO_HEVC_STRONG_SMOOTHING:
2169 		p->codec.hevc.strong_intra_smooth = ctrl->val;
2170 		break;
2171 	case V4L2_CID_MPEG_VIDEO_HEVC_INTRA_PU_SPLIT:
2172 		p->codec.hevc.intra_pu_split_disable = ctrl->val;
2173 		break;
2174 	case V4L2_CID_MPEG_VIDEO_HEVC_TMV_PREDICTION:
2175 		p->codec.hevc.tmv_prediction_disable = !ctrl->val;
2176 		break;
2177 	case V4L2_CID_MPEG_VIDEO_HEVC_MAX_NUM_MERGE_MV_MINUS1:
2178 		p->codec.hevc.max_num_merge_mv = ctrl->val;
2179 		break;
2180 	case V4L2_CID_MPEG_VIDEO_HEVC_WITHOUT_STARTCODE:
2181 		p->codec.hevc.encoding_nostartcode_enable = ctrl->val;
2182 		break;
2183 	case V4L2_CID_MPEG_VIDEO_HEVC_REFRESH_PERIOD:
2184 		p->codec.hevc.refreshperiod = ctrl->val;
2185 		break;
2186 	case V4L2_CID_MPEG_VIDEO_HEVC_LF_BETA_OFFSET_DIV2:
2187 		p->codec.hevc.lf_beta_offset_div2 = ctrl->val;
2188 		break;
2189 	case V4L2_CID_MPEG_VIDEO_HEVC_LF_TC_OFFSET_DIV2:
2190 		p->codec.hevc.lf_tc_offset_div2 = ctrl->val;
2191 		break;
2192 	case V4L2_CID_MPEG_VIDEO_HEVC_SIZE_OF_LENGTH_FIELD:
2193 		p->codec.hevc.size_of_length_field = ctrl->val;
2194 		break;
2195 	case V4L2_CID_MPEG_VIDEO_PREPEND_SPSPPS_TO_IDR:
2196 		p->codec.hevc.prepend_sps_pps_to_idr = ctrl->val;
2197 		break;
2198 	default:
2199 		v4l2_err(&dev->v4l2_dev, "Invalid control, id=%d, val=%d\n",
2200 							ctrl->id, ctrl->val);
2201 		ret = -EINVAL;
2202 	}
2203 	return ret;
2204 }
2205 
2206 static int s5p_mfc_enc_g_v_ctrl(struct v4l2_ctrl *ctrl)
2207 {
2208 	struct s5p_mfc_ctx *ctx = ctrl_to_ctx(ctrl);
2209 	struct s5p_mfc_dev *dev = ctx->dev;
2210 
2211 	switch (ctrl->id) {
2212 	case V4L2_CID_MIN_BUFFERS_FOR_OUTPUT:
2213 		if (ctx->state >= MFCINST_HEAD_PARSED &&
2214 		    ctx->state < MFCINST_ABORT) {
2215 			ctrl->val = ctx->pb_count;
2216 			break;
2217 		} else if (ctx->state != MFCINST_INIT) {
2218 			v4l2_err(&dev->v4l2_dev, "Encoding not initialised\n");
2219 			return -EINVAL;
2220 		}
2221 		/* Should wait for the header to be produced */
2222 		s5p_mfc_wait_for_done_ctx(ctx,
2223 				S5P_MFC_R2H_CMD_SEQ_DONE_RET, 0);
2224 		if (ctx->state >= MFCINST_HEAD_PARSED &&
2225 		    ctx->state < MFCINST_ABORT) {
2226 			ctrl->val = ctx->pb_count;
2227 		} else {
2228 			v4l2_err(&dev->v4l2_dev, "Encoding not initialised\n");
2229 			return -EINVAL;
2230 		}
2231 		break;
2232 	}
2233 	return 0;
2234 }
2235 
2236 static const struct v4l2_ctrl_ops s5p_mfc_enc_ctrl_ops = {
2237 	.s_ctrl = s5p_mfc_enc_s_ctrl,
2238 	.g_volatile_ctrl = s5p_mfc_enc_g_v_ctrl,
2239 };
2240 
2241 static int vidioc_s_parm(struct file *file, void *priv,
2242 			 struct v4l2_streamparm *a)
2243 {
2244 	struct s5p_mfc_ctx *ctx = fh_to_ctx(priv);
2245 
2246 	if (a->type == V4L2_BUF_TYPE_VIDEO_OUTPUT_MPLANE) {
2247 		ctx->enc_params.rc_framerate_num =
2248 					a->parm.output.timeperframe.denominator;
2249 		ctx->enc_params.rc_framerate_denom =
2250 					a->parm.output.timeperframe.numerator;
2251 	} else {
2252 		mfc_err("Setting FPS is only possible for the output queue\n");
2253 		return -EINVAL;
2254 	}
2255 	return 0;
2256 }
2257 
2258 static int vidioc_g_parm(struct file *file, void *priv,
2259 			 struct v4l2_streamparm *a)
2260 {
2261 	struct s5p_mfc_ctx *ctx = fh_to_ctx(priv);
2262 
2263 	if (a->type == V4L2_BUF_TYPE_VIDEO_OUTPUT) {
2264 		a->parm.output.timeperframe.denominator =
2265 					ctx->enc_params.rc_framerate_num;
2266 		a->parm.output.timeperframe.numerator =
2267 					ctx->enc_params.rc_framerate_denom;
2268 	} else {
2269 		mfc_err("Setting FPS is only possible for the output queue\n");
2270 		return -EINVAL;
2271 	}
2272 	return 0;
2273 }
2274 
2275 static int vidioc_encoder_cmd(struct file *file, void *priv,
2276 			      struct v4l2_encoder_cmd *cmd)
2277 {
2278 	struct s5p_mfc_ctx *ctx = fh_to_ctx(priv);
2279 	struct s5p_mfc_dev *dev = ctx->dev;
2280 	struct s5p_mfc_buf *buf;
2281 	unsigned long flags;
2282 
2283 	switch (cmd->cmd) {
2284 	case V4L2_ENC_CMD_STOP:
2285 		if (cmd->flags != 0)
2286 			return -EINVAL;
2287 
2288 		if (!ctx->vq_src.streaming)
2289 			return -EINVAL;
2290 
2291 		spin_lock_irqsave(&dev->irqlock, flags);
2292 		if (list_empty(&ctx->src_queue)) {
2293 			mfc_debug(2, "EOS: empty src queue, entering finishing state\n");
2294 			ctx->state = MFCINST_FINISHING;
2295 			if (s5p_mfc_ctx_ready(ctx))
2296 				set_work_bit_irqsave(ctx);
2297 			spin_unlock_irqrestore(&dev->irqlock, flags);
2298 			s5p_mfc_hw_call(dev->mfc_ops, try_run, dev);
2299 		} else {
2300 			mfc_debug(2, "EOS: marking last buffer of stream\n");
2301 			buf = list_entry(ctx->src_queue.prev,
2302 						struct s5p_mfc_buf, list);
2303 			if (buf->flags & MFC_BUF_FLAG_USED)
2304 				ctx->state = MFCINST_FINISHING;
2305 			else
2306 				buf->flags |= MFC_BUF_FLAG_EOS;
2307 			spin_unlock_irqrestore(&dev->irqlock, flags);
2308 		}
2309 		break;
2310 	default:
2311 		return -EINVAL;
2312 
2313 	}
2314 	return 0;
2315 }
2316 
2317 static int vidioc_subscribe_event(struct v4l2_fh *fh,
2318 				  const struct v4l2_event_subscription *sub)
2319 {
2320 	switch (sub->type) {
2321 	case V4L2_EVENT_EOS:
2322 		return v4l2_event_subscribe(fh, sub, 2, NULL);
2323 	default:
2324 		return -EINVAL;
2325 	}
2326 }
2327 
2328 static const struct v4l2_ioctl_ops s5p_mfc_enc_ioctl_ops = {
2329 	.vidioc_querycap = vidioc_querycap,
2330 	.vidioc_enum_fmt_vid_cap = vidioc_enum_fmt_vid_cap,
2331 	.vidioc_enum_fmt_vid_out = vidioc_enum_fmt_vid_out,
2332 	.vidioc_g_fmt_vid_cap_mplane = vidioc_g_fmt,
2333 	.vidioc_g_fmt_vid_out_mplane = vidioc_g_fmt,
2334 	.vidioc_try_fmt_vid_cap_mplane = vidioc_try_fmt,
2335 	.vidioc_try_fmt_vid_out_mplane = vidioc_try_fmt,
2336 	.vidioc_s_fmt_vid_cap_mplane = vidioc_s_fmt,
2337 	.vidioc_s_fmt_vid_out_mplane = vidioc_s_fmt,
2338 	.vidioc_reqbufs = vidioc_reqbufs,
2339 	.vidioc_querybuf = vidioc_querybuf,
2340 	.vidioc_qbuf = vidioc_qbuf,
2341 	.vidioc_dqbuf = vidioc_dqbuf,
2342 	.vidioc_expbuf = vidioc_expbuf,
2343 	.vidioc_streamon = vidioc_streamon,
2344 	.vidioc_streamoff = vidioc_streamoff,
2345 	.vidioc_s_parm = vidioc_s_parm,
2346 	.vidioc_g_parm = vidioc_g_parm,
2347 	.vidioc_encoder_cmd = vidioc_encoder_cmd,
2348 	.vidioc_subscribe_event = vidioc_subscribe_event,
2349 	.vidioc_unsubscribe_event = v4l2_event_unsubscribe,
2350 };
2351 
2352 static int check_vb_with_fmt(struct s5p_mfc_fmt *fmt, struct vb2_buffer *vb)
2353 {
2354 	int i;
2355 
2356 	if (!fmt)
2357 		return -EINVAL;
2358 	if (fmt->num_planes != vb->num_planes) {
2359 		mfc_err("invalid plane number for the format\n");
2360 		return -EINVAL;
2361 	}
2362 	for (i = 0; i < fmt->num_planes; i++) {
2363 		dma_addr_t dma = vb2_dma_contig_plane_dma_addr(vb, i);
2364 		if (!dma) {
2365 			mfc_err("failed to get plane cookie\n");
2366 			return -EINVAL;
2367 		}
2368 		mfc_debug(2, "index: %d, plane[%d] cookie: %pad\n",
2369 			  vb->index, i, &dma);
2370 	}
2371 	return 0;
2372 }
2373 
2374 static int s5p_mfc_queue_setup(struct vb2_queue *vq,
2375 			unsigned int *buf_count, unsigned int *plane_count,
2376 			unsigned int psize[], struct device *alloc_devs[])
2377 {
2378 	struct s5p_mfc_ctx *ctx = fh_to_ctx(vq->drv_priv);
2379 	struct s5p_mfc_dev *dev = ctx->dev;
2380 
2381 	if (vq->type == V4L2_BUF_TYPE_VIDEO_CAPTURE_MPLANE) {
2382 		if (ctx->state != MFCINST_GOT_INST) {
2383 			mfc_err("invalid state: %d\n", ctx->state);
2384 			return -EINVAL;
2385 		}
2386 
2387 		if (ctx->dst_fmt)
2388 			*plane_count = ctx->dst_fmt->num_planes;
2389 		else
2390 			*plane_count = MFC_ENC_CAP_PLANE_COUNT;
2391 		if (*buf_count < 1)
2392 			*buf_count = 1;
2393 		if (*buf_count > MFC_MAX_BUFFERS)
2394 			*buf_count = MFC_MAX_BUFFERS;
2395 		psize[0] = ctx->enc_dst_buf_size;
2396 		alloc_devs[0] = ctx->dev->mem_dev[BANK_L_CTX];
2397 	} else if (vq->type == V4L2_BUF_TYPE_VIDEO_OUTPUT_MPLANE) {
2398 		if (ctx->src_fmt)
2399 			*plane_count = ctx->src_fmt->num_planes;
2400 		else
2401 			*plane_count = MFC_ENC_OUT_PLANE_COUNT;
2402 
2403 		if (*buf_count < 1)
2404 			*buf_count = 1;
2405 		if (*buf_count > MFC_MAX_BUFFERS)
2406 			*buf_count = MFC_MAX_BUFFERS;
2407 
2408 		psize[0] = ctx->luma_size;
2409 		psize[1] = ctx->chroma_size;
2410 
2411 		if (IS_MFCV6_PLUS(dev)) {
2412 			alloc_devs[0] = ctx->dev->mem_dev[BANK_L_CTX];
2413 			alloc_devs[1] = ctx->dev->mem_dev[BANK_L_CTX];
2414 		} else {
2415 			alloc_devs[0] = ctx->dev->mem_dev[BANK_R_CTX];
2416 			alloc_devs[1] = ctx->dev->mem_dev[BANK_R_CTX];
2417 		}
2418 	} else {
2419 		mfc_err("invalid queue type: %d\n", vq->type);
2420 		return -EINVAL;
2421 	}
2422 	return 0;
2423 }
2424 
2425 static int s5p_mfc_buf_init(struct vb2_buffer *vb)
2426 {
2427 	struct vb2_v4l2_buffer *vbuf = to_vb2_v4l2_buffer(vb);
2428 	struct vb2_queue *vq = vb->vb2_queue;
2429 	struct s5p_mfc_ctx *ctx = fh_to_ctx(vq->drv_priv);
2430 	unsigned int i;
2431 	int ret;
2432 
2433 	if (vq->type == V4L2_BUF_TYPE_VIDEO_CAPTURE_MPLANE) {
2434 		ret = check_vb_with_fmt(ctx->dst_fmt, vb);
2435 		if (ret < 0)
2436 			return ret;
2437 		i = vb->index;
2438 		ctx->dst_bufs[i].b = vbuf;
2439 		ctx->dst_bufs[i].cookie.stream =
2440 					vb2_dma_contig_plane_dma_addr(vb, 0);
2441 		ctx->dst_bufs_cnt++;
2442 	} else if (vq->type == V4L2_BUF_TYPE_VIDEO_OUTPUT_MPLANE) {
2443 		ret = check_vb_with_fmt(ctx->src_fmt, vb);
2444 		if (ret < 0)
2445 			return ret;
2446 		i = vb->index;
2447 		ctx->src_bufs[i].b = vbuf;
2448 		ctx->src_bufs[i].cookie.raw.luma =
2449 					vb2_dma_contig_plane_dma_addr(vb, 0);
2450 		ctx->src_bufs[i].cookie.raw.chroma =
2451 					vb2_dma_contig_plane_dma_addr(vb, 1);
2452 		ctx->src_bufs_cnt++;
2453 	} else {
2454 		mfc_err("invalid queue type: %d\n", vq->type);
2455 		return -EINVAL;
2456 	}
2457 	return 0;
2458 }
2459 
2460 static int s5p_mfc_buf_prepare(struct vb2_buffer *vb)
2461 {
2462 	struct vb2_queue *vq = vb->vb2_queue;
2463 	struct s5p_mfc_ctx *ctx = fh_to_ctx(vq->drv_priv);
2464 	int ret;
2465 
2466 	if (vq->type == V4L2_BUF_TYPE_VIDEO_CAPTURE_MPLANE) {
2467 		ret = check_vb_with_fmt(ctx->dst_fmt, vb);
2468 		if (ret < 0)
2469 			return ret;
2470 		mfc_debug(2, "plane size: %ld, dst size: %zu\n",
2471 			vb2_plane_size(vb, 0), ctx->enc_dst_buf_size);
2472 		if (vb2_plane_size(vb, 0) < ctx->enc_dst_buf_size) {
2473 			mfc_err("plane size is too small for capture\n");
2474 			return -EINVAL;
2475 		}
2476 	} else if (vq->type == V4L2_BUF_TYPE_VIDEO_OUTPUT_MPLANE) {
2477 		ret = check_vb_with_fmt(ctx->src_fmt, vb);
2478 		if (ret < 0)
2479 			return ret;
2480 		mfc_debug(2, "plane size: %ld, luma size: %d\n",
2481 			vb2_plane_size(vb, 0), ctx->luma_size);
2482 		mfc_debug(2, "plane size: %ld, chroma size: %d\n",
2483 			vb2_plane_size(vb, 1), ctx->chroma_size);
2484 		if (vb2_plane_size(vb, 0) < ctx->luma_size ||
2485 		    vb2_plane_size(vb, 1) < ctx->chroma_size) {
2486 			mfc_err("plane size is too small for output\n");
2487 			return -EINVAL;
2488 		}
2489 	} else {
2490 		mfc_err("invalid queue type: %d\n", vq->type);
2491 		return -EINVAL;
2492 	}
2493 	return 0;
2494 }
2495 
2496 static int s5p_mfc_start_streaming(struct vb2_queue *q, unsigned int count)
2497 {
2498 	struct s5p_mfc_ctx *ctx = fh_to_ctx(q->drv_priv);
2499 	struct s5p_mfc_dev *dev = ctx->dev;
2500 
2501 	if (IS_MFCV6_PLUS(dev) &&
2502 			(q->type == V4L2_BUF_TYPE_VIDEO_CAPTURE_MPLANE)) {
2503 
2504 		if ((ctx->state == MFCINST_GOT_INST) &&
2505 			(dev->curr_ctx == ctx->num) && dev->hw_lock) {
2506 			s5p_mfc_wait_for_done_ctx(ctx,
2507 						S5P_MFC_R2H_CMD_SEQ_DONE_RET,
2508 						0);
2509 		}
2510 
2511 		if (ctx->src_bufs_cnt < ctx->pb_count) {
2512 			mfc_err("Need minimum %d OUTPUT buffers\n",
2513 					ctx->pb_count);
2514 			return -ENOBUFS;
2515 		}
2516 	}
2517 
2518 	/* If context is ready then dev = work->data;schedule it to run */
2519 	if (s5p_mfc_ctx_ready(ctx))
2520 		set_work_bit_irqsave(ctx);
2521 	s5p_mfc_hw_call(dev->mfc_ops, try_run, dev);
2522 
2523 	return 0;
2524 }
2525 
2526 static void s5p_mfc_stop_streaming(struct vb2_queue *q)
2527 {
2528 	unsigned long flags;
2529 	struct s5p_mfc_ctx *ctx = fh_to_ctx(q->drv_priv);
2530 	struct s5p_mfc_dev *dev = ctx->dev;
2531 
2532 	if ((ctx->state == MFCINST_FINISHING ||
2533 		ctx->state == MFCINST_RUNNING) &&
2534 		dev->curr_ctx == ctx->num && dev->hw_lock) {
2535 		ctx->state = MFCINST_ABORT;
2536 		s5p_mfc_wait_for_done_ctx(ctx, S5P_MFC_R2H_CMD_FRAME_DONE_RET,
2537 					  0);
2538 	}
2539 	ctx->state = MFCINST_FINISHED;
2540 	spin_lock_irqsave(&dev->irqlock, flags);
2541 	if (q->type == V4L2_BUF_TYPE_VIDEO_CAPTURE_MPLANE) {
2542 		s5p_mfc_cleanup_queue(&ctx->dst_queue, &ctx->vq_dst);
2543 		INIT_LIST_HEAD(&ctx->dst_queue);
2544 		ctx->dst_queue_cnt = 0;
2545 	}
2546 	if (q->type == V4L2_BUF_TYPE_VIDEO_OUTPUT_MPLANE) {
2547 		cleanup_ref_queue(ctx);
2548 		s5p_mfc_cleanup_queue(&ctx->src_queue, &ctx->vq_src);
2549 		INIT_LIST_HEAD(&ctx->src_queue);
2550 		ctx->src_queue_cnt = 0;
2551 	}
2552 	spin_unlock_irqrestore(&dev->irqlock, flags);
2553 }
2554 
2555 static void s5p_mfc_buf_queue(struct vb2_buffer *vb)
2556 {
2557 	struct vb2_queue *vq = vb->vb2_queue;
2558 	struct s5p_mfc_ctx *ctx = fh_to_ctx(vq->drv_priv);
2559 	struct s5p_mfc_dev *dev = ctx->dev;
2560 	unsigned long flags;
2561 	struct s5p_mfc_buf *mfc_buf;
2562 
2563 	if (ctx->state == MFCINST_ERROR) {
2564 		vb2_buffer_done(vb, VB2_BUF_STATE_ERROR);
2565 		cleanup_ref_queue(ctx);
2566 		return;
2567 	}
2568 	if (vq->type == V4L2_BUF_TYPE_VIDEO_CAPTURE_MPLANE) {
2569 		mfc_buf = &ctx->dst_bufs[vb->index];
2570 		mfc_buf->flags &= ~MFC_BUF_FLAG_USED;
2571 		/* Mark destination as available for use by MFC */
2572 		spin_lock_irqsave(&dev->irqlock, flags);
2573 		list_add_tail(&mfc_buf->list, &ctx->dst_queue);
2574 		ctx->dst_queue_cnt++;
2575 		spin_unlock_irqrestore(&dev->irqlock, flags);
2576 	} else if (vq->type == V4L2_BUF_TYPE_VIDEO_OUTPUT_MPLANE) {
2577 		mfc_buf = &ctx->src_bufs[vb->index];
2578 		mfc_buf->flags &= ~MFC_BUF_FLAG_USED;
2579 		spin_lock_irqsave(&dev->irqlock, flags);
2580 		list_add_tail(&mfc_buf->list, &ctx->src_queue);
2581 		ctx->src_queue_cnt++;
2582 		spin_unlock_irqrestore(&dev->irqlock, flags);
2583 	} else {
2584 		mfc_err("unsupported buffer type (%d)\n", vq->type);
2585 	}
2586 	if (s5p_mfc_ctx_ready(ctx))
2587 		set_work_bit_irqsave(ctx);
2588 	s5p_mfc_hw_call(dev->mfc_ops, try_run, dev);
2589 }
2590 
2591 static struct vb2_ops s5p_mfc_enc_qops = {
2592 	.queue_setup		= s5p_mfc_queue_setup,
2593 	.wait_prepare		= vb2_ops_wait_prepare,
2594 	.wait_finish		= vb2_ops_wait_finish,
2595 	.buf_init		= s5p_mfc_buf_init,
2596 	.buf_prepare		= s5p_mfc_buf_prepare,
2597 	.start_streaming	= s5p_mfc_start_streaming,
2598 	.stop_streaming		= s5p_mfc_stop_streaming,
2599 	.buf_queue		= s5p_mfc_buf_queue,
2600 };
2601 
2602 const struct s5p_mfc_codec_ops *get_enc_codec_ops(void)
2603 {
2604 	return &encoder_codec_ops;
2605 }
2606 
2607 struct vb2_ops *get_enc_queue_ops(void)
2608 {
2609 	return &s5p_mfc_enc_qops;
2610 }
2611 
2612 const struct v4l2_ioctl_ops *get_enc_v4l2_ioctl_ops(void)
2613 {
2614 	return &s5p_mfc_enc_ioctl_ops;
2615 }
2616 
2617 #define IS_MFC51_PRIV(x) ((V4L2_CTRL_ID2WHICH(x) == V4L2_CTRL_CLASS_CODEC) \
2618 						&& V4L2_CTRL_DRIVER_PRIV(x))
2619 
2620 int s5p_mfc_enc_ctrls_setup(struct s5p_mfc_ctx *ctx)
2621 {
2622 	struct v4l2_ctrl_config cfg;
2623 	int i;
2624 
2625 	v4l2_ctrl_handler_init(&ctx->ctrl_handler, NUM_CTRLS);
2626 	if (ctx->ctrl_handler.error) {
2627 		mfc_err("v4l2_ctrl_handler_init failed\n");
2628 		return ctx->ctrl_handler.error;
2629 	}
2630 	for (i = 0; i < NUM_CTRLS; i++) {
2631 		if (IS_MFC51_PRIV(controls[i].id)) {
2632 			memset(&cfg, 0, sizeof(struct v4l2_ctrl_config));
2633 			cfg.ops = &s5p_mfc_enc_ctrl_ops;
2634 			cfg.id = controls[i].id;
2635 			cfg.min = controls[i].minimum;
2636 			cfg.max = controls[i].maximum;
2637 			cfg.def = controls[i].default_value;
2638 			cfg.name = controls[i].name;
2639 			cfg.type = controls[i].type;
2640 			cfg.flags = 0;
2641 
2642 			if (cfg.type == V4L2_CTRL_TYPE_MENU) {
2643 				cfg.step = 0;
2644 				cfg.menu_skip_mask = controls[i].menu_skip_mask;
2645 				cfg.qmenu = mfc51_get_menu(cfg.id);
2646 			} else {
2647 				cfg.step = controls[i].step;
2648 				cfg.menu_skip_mask = 0;
2649 			}
2650 			ctx->ctrls[i] = v4l2_ctrl_new_custom(&ctx->ctrl_handler,
2651 					&cfg, NULL);
2652 		} else {
2653 			if ((controls[i].type == V4L2_CTRL_TYPE_MENU) ||
2654 				(controls[i].type ==
2655 					V4L2_CTRL_TYPE_INTEGER_MENU)) {
2656 				ctx->ctrls[i] = v4l2_ctrl_new_std_menu(
2657 					&ctx->ctrl_handler,
2658 					&s5p_mfc_enc_ctrl_ops, controls[i].id,
2659 					controls[i].maximum, 0,
2660 					controls[i].default_value);
2661 			} else {
2662 				ctx->ctrls[i] = v4l2_ctrl_new_std(
2663 					&ctx->ctrl_handler,
2664 					&s5p_mfc_enc_ctrl_ops, controls[i].id,
2665 					controls[i].minimum,
2666 					controls[i].maximum, controls[i].step,
2667 					controls[i].default_value);
2668 			}
2669 		}
2670 		if (ctx->ctrl_handler.error) {
2671 			mfc_err("Adding control (%d) failed\n", i);
2672 			return ctx->ctrl_handler.error;
2673 		}
2674 		if (controls[i].is_volatile && ctx->ctrls[i])
2675 			ctx->ctrls[i]->flags |= V4L2_CTRL_FLAG_VOLATILE;
2676 	}
2677 	v4l2_ctrl_handler_setup(&ctx->ctrl_handler);
2678 	return 0;
2679 }
2680 
2681 void s5p_mfc_enc_ctrls_delete(struct s5p_mfc_ctx *ctx)
2682 {
2683 	int i;
2684 
2685 	v4l2_ctrl_handler_free(&ctx->ctrl_handler);
2686 	for (i = 0; i < NUM_CTRLS; i++)
2687 		ctx->ctrls[i] = NULL;
2688 }
2689 
2690 void s5p_mfc_enc_init(struct s5p_mfc_ctx *ctx)
2691 {
2692 	struct v4l2_format f;
2693 	f.fmt.pix_mp.pixelformat = DEF_SRC_FMT_ENC;
2694 	ctx->src_fmt = find_format(&f, MFC_FMT_RAW);
2695 	f.fmt.pix_mp.pixelformat = DEF_DST_FMT_ENC;
2696 	ctx->dst_fmt = find_format(&f, MFC_FMT_ENC);
2697 }
2698