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