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