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