ima: fix bug in argument order
[sfrench/cifs-2.6.git] / drivers / media / video / s5p-mfc / s5p_mfc_enc.c
1 /*
2  * linux/drivers/media/video/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 <linux/workqueue.h>
25 #include <media/v4l2-ctrls.h>
26 #include <media/videobuf2-core.h>
27 #include "regs-mfc.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 static struct s5p_mfc_fmt formats[] = {
35         {
36                 .name = "4:2:0 2 Planes 64x32 Tiles",
37                 .fourcc = V4L2_PIX_FMT_NV12MT,
38                 .codec_mode = S5P_FIMV_CODEC_NONE,
39                 .type = MFC_FMT_RAW,
40                 .num_planes = 2,
41         },
42         {
43                 .name = "4:2:0 2 Planes",
44                 .fourcc = V4L2_PIX_FMT_NV12M,
45                 .codec_mode = S5P_FIMV_CODEC_NONE,
46                 .type = MFC_FMT_RAW,
47                 .num_planes = 2,
48         },
49         {
50                 .name = "H264 Encoded Stream",
51                 .fourcc = V4L2_PIX_FMT_H264,
52                 .codec_mode = S5P_FIMV_CODEC_H264_ENC,
53                 .type = MFC_FMT_ENC,
54                 .num_planes = 1,
55         },
56         {
57                 .name = "MPEG4 Encoded Stream",
58                 .fourcc = V4L2_PIX_FMT_MPEG4,
59                 .codec_mode = S5P_FIMV_CODEC_MPEG4_ENC,
60                 .type = MFC_FMT_ENC,
61                 .num_planes = 1,
62         },
63         {
64                 .name = "H263 Encoded Stream",
65                 .fourcc = V4L2_PIX_FMT_H263,
66                 .codec_mode = S5P_FIMV_CODEC_H263_ENC,
67                 .type = MFC_FMT_ENC,
68                 .num_planes = 1,
69         },
70 };
71
72 #define NUM_FORMATS ARRAY_SIZE(formats)
73 static struct s5p_mfc_fmt *find_format(struct v4l2_format *f, unsigned int t)
74 {
75         unsigned int i;
76
77         for (i = 0; i < NUM_FORMATS; i++) {
78                 if (formats[i].fourcc == f->fmt.pix_mp.pixelformat &&
79                     formats[i].type == t)
80                         return &formats[i];
81         }
82         return NULL;
83 }
84
85 static struct mfc_control controls[] = {
86         {
87                 .id = V4L2_CID_MPEG_VIDEO_GOP_SIZE,
88                 .type = V4L2_CTRL_TYPE_INTEGER,
89                 .minimum = 0,
90                 .maximum = (1 << 16) - 1,
91                 .step = 1,
92                 .default_value = 0,
93         },
94         {
95                 .id = V4L2_CID_MPEG_VIDEO_MULTI_SLICE_MODE,
96                 .type = V4L2_CTRL_TYPE_MENU,
97                 .minimum = V4L2_MPEG_VIDEO_MULTI_SLICE_MODE_SINGLE,
98                 .maximum = V4L2_MPEG_VIDEO_MULTI_SICE_MODE_MAX_BYTES,
99                 .default_value = V4L2_MPEG_VIDEO_MULTI_SLICE_MODE_SINGLE,
100                 .menu_skip_mask = 0,
101         },
102         {
103                 .id = V4L2_CID_MPEG_VIDEO_MULTI_SLICE_MAX_MB,
104                 .type = V4L2_CTRL_TYPE_INTEGER,
105                 .minimum = 1,
106                 .maximum = (1 << 16) - 1,
107                 .step = 1,
108                 .default_value = 1,
109         },
110         {
111                 .id = V4L2_CID_MPEG_VIDEO_MULTI_SLICE_MAX_BYTES,
112                 .type = V4L2_CTRL_TYPE_INTEGER,
113                 .minimum = 1900,
114                 .maximum = (1 << 30) - 1,
115                 .step = 1,
116                 .default_value = 1900,
117         },
118         {
119                 .id = V4L2_CID_MPEG_VIDEO_CYCLIC_INTRA_REFRESH_MB,
120                 .type = V4L2_CTRL_TYPE_INTEGER,
121                 .minimum = 0,
122                 .maximum = (1 << 16) - 1,
123                 .step = 1,
124                 .default_value = 0,
125         },
126         {
127                 .id = V4L2_CID_MPEG_MFC51_VIDEO_PADDING,
128                 .type = V4L2_CTRL_TYPE_BOOLEAN,
129                 .name = "Padding Control Enable",
130                 .minimum = 0,
131                 .maximum = 1,
132                 .step = 1,
133                 .default_value = 0,
134         },
135         {
136                 .id = V4L2_CID_MPEG_MFC51_VIDEO_PADDING_YUV,
137                 .type = V4L2_CTRL_TYPE_INTEGER,
138                 .name = "Padding Color YUV Value",
139                 .minimum = 0,
140                 .maximum = (1 << 25) - 1,
141                 .step = 1,
142                 .default_value = 0,
143         },
144         {
145                 .id = V4L2_CID_MPEG_VIDEO_FRAME_RC_ENABLE,
146                 .type = V4L2_CTRL_TYPE_BOOLEAN,
147                 .minimum = 0,
148                 .maximum = 1,
149                 .step = 1,
150                 .default_value = 0,
151         },
152         {
153                 .id = V4L2_CID_MPEG_VIDEO_BITRATE,
154                 .type = V4L2_CTRL_TYPE_INTEGER,
155                 .minimum = 1,
156                 .maximum = (1 << 30) - 1,
157                 .step = 1,
158                 .default_value = 1,
159         },
160         {
161                 .id = V4L2_CID_MPEG_MFC51_VIDEO_RC_REACTION_COEFF,
162                 .type = V4L2_CTRL_TYPE_INTEGER,
163                 .name = "Rate Control Reaction Coeff.",
164                 .minimum = 1,
165                 .maximum = (1 << 16) - 1,
166                 .step = 1,
167                 .default_value = 1,
168         },
169         {
170                 .id = V4L2_CID_MPEG_MFC51_VIDEO_FORCE_FRAME_TYPE,
171                 .type = V4L2_CTRL_TYPE_MENU,
172                 .name = "Force frame type",
173                 .minimum = V4L2_MPEG_MFC51_VIDEO_FORCE_FRAME_TYPE_DISABLED,
174                 .maximum = V4L2_MPEG_MFC51_VIDEO_FORCE_FRAME_TYPE_NOT_CODED,
175                 .default_value = V4L2_MPEG_MFC51_VIDEO_FORCE_FRAME_TYPE_DISABLED,
176                 .menu_skip_mask = 0,
177         },
178         {
179                 .id = V4L2_CID_MPEG_VIDEO_VBV_SIZE,
180                 .type = V4L2_CTRL_TYPE_INTEGER,
181                 .minimum = 0,
182                 .maximum = (1 << 16) - 1,
183                 .step = 1,
184                 .default_value = 0,
185         },
186         {
187                 .id = V4L2_CID_MPEG_VIDEO_H264_CPB_SIZE,
188                 .type = V4L2_CTRL_TYPE_INTEGER,
189                 .minimum = 0,
190                 .maximum = (1 << 16) - 1,
191                 .step = 1,
192                 .default_value = 0,
193         },
194         {
195                 .id = V4L2_CID_MPEG_VIDEO_HEADER_MODE,
196                 .type = V4L2_CTRL_TYPE_MENU,
197                 .minimum = V4L2_MPEG_VIDEO_HEADER_MODE_SEPARATE,
198                 .maximum = V4L2_MPEG_VIDEO_HEADER_MODE_JOINED_WITH_1ST_FRAME,
199                 .default_value = V4L2_MPEG_VIDEO_HEADER_MODE_SEPARATE,
200                 .menu_skip_mask = 0,
201         },
202         {
203                 .id = V4L2_CID_MPEG_MFC51_VIDEO_FRAME_SKIP_MODE,
204                 .type = V4L2_CTRL_TYPE_MENU,
205                 .name = "Frame Skip Enable",
206                 .minimum = V4L2_MPEG_MFC51_VIDEO_FRAME_SKIP_MODE_DISABLED,
207                 .maximum = V4L2_MPEG_MFC51_VIDEO_FRAME_SKIP_MODE_BUF_LIMIT,
208                 .menu_skip_mask = 0,
209                 .default_value = V4L2_MPEG_MFC51_VIDEO_FRAME_SKIP_MODE_DISABLED,
210         },
211         {
212                 .id = V4L2_CID_MPEG_MFC51_VIDEO_RC_FIXED_TARGET_BIT,
213                 .type = V4L2_CTRL_TYPE_BOOLEAN,
214                 .name = "Fixed Target Bit Enable",
215                 .minimum = 0,
216                 .maximum = 1,
217                 .default_value = 0,
218                 .menu_skip_mask = 0,
219         },
220         {
221                 .id = V4L2_CID_MPEG_VIDEO_B_FRAMES,
222                 .type = V4L2_CTRL_TYPE_INTEGER,
223                 .minimum = 0,
224                 .maximum = 2,
225                 .step = 1,
226                 .default_value = 0,
227         },
228         {
229                 .id = V4L2_CID_MPEG_VIDEO_H264_PROFILE,
230                 .type = V4L2_CTRL_TYPE_MENU,
231                 .minimum = V4L2_MPEG_VIDEO_H264_PROFILE_BASELINE,
232                 .maximum = V4L2_MPEG_VIDEO_H264_PROFILE_MULTIVIEW_HIGH,
233                 .default_value = V4L2_MPEG_VIDEO_H264_PROFILE_BASELINE,
234                 .menu_skip_mask = ~(
235                                 (1 << V4L2_MPEG_VIDEO_H264_PROFILE_BASELINE) |
236                                 (1 << V4L2_MPEG_VIDEO_H264_PROFILE_MAIN) |
237                                 (1 << V4L2_MPEG_VIDEO_H264_PROFILE_HIGH)
238                                 ),
239         },
240         {
241                 .id = V4L2_CID_MPEG_VIDEO_H264_LEVEL,
242                 .type = V4L2_CTRL_TYPE_MENU,
243                 .minimum = V4L2_MPEG_VIDEO_H264_LEVEL_1_0,
244                 .maximum = V4L2_MPEG_VIDEO_H264_LEVEL_4_0,
245                 .default_value = V4L2_MPEG_VIDEO_H264_LEVEL_1_0,
246         },
247         {
248                 .id = V4L2_CID_MPEG_VIDEO_MPEG4_LEVEL,
249                 .type = V4L2_CTRL_TYPE_MENU,
250                 .minimum = V4L2_MPEG_VIDEO_MPEG4_LEVEL_0,
251                 .maximum = V4L2_MPEG_VIDEO_MPEG4_LEVEL_5,
252                 .default_value = V4L2_MPEG_VIDEO_MPEG4_LEVEL_0,
253                 .menu_skip_mask = 0,
254         },
255         {
256                 .id = V4L2_CID_MPEG_VIDEO_H264_LOOP_FILTER_MODE,
257                 .type = V4L2_CTRL_TYPE_MENU,
258                 .minimum = V4L2_MPEG_VIDEO_H264_LOOP_FILTER_MODE_ENABLED,
259                 .maximum = V4L2_MPEG_VIDEO_H264_LOOP_FILTER_MODE_DISABLED_AT_SLICE_BOUNDARY,
260                 .default_value = V4L2_MPEG_VIDEO_H264_LOOP_FILTER_MODE_ENABLED,
261                 .menu_skip_mask = 0,
262         },
263         {
264                 .id = V4L2_CID_MPEG_VIDEO_H264_LOOP_FILTER_ALPHA,
265                 .type = V4L2_CTRL_TYPE_INTEGER,
266                 .minimum = -6,
267                 .maximum = 6,
268                 .step = 1,
269                 .default_value = 0,
270         },
271         {
272                 .id = V4L2_CID_MPEG_VIDEO_H264_LOOP_FILTER_BETA,
273                 .type = V4L2_CTRL_TYPE_INTEGER,
274                 .minimum = -6,
275                 .maximum = 6,
276                 .step = 1,
277                 .default_value = 0,
278         },
279         {
280                 .id = V4L2_CID_MPEG_VIDEO_H264_ENTROPY_MODE,
281                 .type = V4L2_CTRL_TYPE_MENU,
282                 .minimum = V4L2_MPEG_VIDEO_H264_ENTROPY_MODE_CAVLC,
283                 .maximum = V4L2_MPEG_VIDEO_H264_ENTROPY_MODE_CABAC,
284                 .default_value = V4L2_MPEG_VIDEO_H264_ENTROPY_MODE_CAVLC,
285                 .menu_skip_mask = 0,
286         },
287         {
288                 .id = V4L2_CID_MPEG_MFC51_VIDEO_H264_NUM_REF_PIC_FOR_P,
289                 .type = V4L2_CTRL_TYPE_INTEGER,
290                 .name = "The Number of Ref. Pic for P",
291                 .minimum = 1,
292                 .maximum = 2,
293                 .step = 1,
294                 .default_value = 1,
295         },
296         {
297                 .id = V4L2_CID_MPEG_VIDEO_H264_8X8_TRANSFORM,
298                 .type = V4L2_CTRL_TYPE_BOOLEAN,
299                 .minimum = 0,
300                 .maximum = 1,
301                 .step = 1,
302                 .default_value = 0,
303         },
304         {
305                 .id = V4L2_CID_MPEG_VIDEO_MB_RC_ENABLE,
306                 .type = V4L2_CTRL_TYPE_BOOLEAN,
307                 .minimum = 0,
308                 .maximum = 1,
309                 .step = 1,
310                 .default_value = 0,
311         },
312         {
313                 .id = V4L2_CID_MPEG_VIDEO_H264_I_FRAME_QP,
314                 .type = V4L2_CTRL_TYPE_INTEGER,
315                 .minimum = 0,
316                 .maximum = 51,
317                 .step = 1,
318                 .default_value = 1,
319         },
320         {
321                 .id = V4L2_CID_MPEG_VIDEO_H264_MIN_QP,
322                 .type = V4L2_CTRL_TYPE_INTEGER,
323                 .minimum = 0,
324                 .maximum = 51,
325                 .step = 1,
326                 .default_value = 1,
327         },
328         {
329                 .id = V4L2_CID_MPEG_VIDEO_H264_MAX_QP,
330                 .type = V4L2_CTRL_TYPE_INTEGER,
331                 .minimum = 0,
332                 .maximum = 51,
333                 .step = 1,
334                 .default_value = 1,
335         },
336         {
337                 .id = V4L2_CID_MPEG_VIDEO_H264_P_FRAME_QP,
338                 .type = V4L2_CTRL_TYPE_INTEGER,
339                 .minimum = 0,
340                 .maximum = 51,
341                 .step = 1,
342                 .default_value = 1,
343         },
344         {
345                 .id = V4L2_CID_MPEG_VIDEO_H264_B_FRAME_QP,
346                 .type = V4L2_CTRL_TYPE_INTEGER,
347                 .minimum = 0,
348                 .maximum = 51,
349                 .step = 1,
350                 .default_value = 1,
351         },
352         {
353                 .id = V4L2_CID_MPEG_VIDEO_H263_I_FRAME_QP,
354                 .type = V4L2_CTRL_TYPE_INTEGER,
355                 .name = "H263 I-Frame QP value",
356                 .minimum = 1,
357                 .maximum = 31,
358                 .step = 1,
359                 .default_value = 1,
360         },
361         {
362                 .id = V4L2_CID_MPEG_VIDEO_H263_MIN_QP,
363                 .type = V4L2_CTRL_TYPE_INTEGER,
364                 .name = "H263 Minimum QP value",
365                 .minimum = 1,
366                 .maximum = 31,
367                 .step = 1,
368                 .default_value = 1,
369         },
370         {
371                 .id = V4L2_CID_MPEG_VIDEO_H263_MAX_QP,
372                 .type = V4L2_CTRL_TYPE_INTEGER,
373                 .name = "H263 Maximum QP value",
374                 .minimum = 1,
375                 .maximum = 31,
376                 .step = 1,
377                 .default_value = 1,
378         },
379         {
380                 .id = V4L2_CID_MPEG_VIDEO_H263_P_FRAME_QP,
381                 .type = V4L2_CTRL_TYPE_INTEGER,
382                 .name = "H263 P frame QP value",
383                 .minimum = 1,
384                 .maximum = 31,
385                 .step = 1,
386                 .default_value = 1,
387         },
388         {
389                 .id = V4L2_CID_MPEG_VIDEO_H263_B_FRAME_QP,
390                 .type = V4L2_CTRL_TYPE_INTEGER,
391                 .name = "H263 B frame QP value",
392                 .minimum = 1,
393                 .maximum = 31,
394                 .step = 1,
395                 .default_value = 1,
396         },
397         {
398                 .id = V4L2_CID_MPEG_VIDEO_MPEG4_I_FRAME_QP,
399                 .type = V4L2_CTRL_TYPE_INTEGER,
400                 .name = "MPEG4 I-Frame QP value",
401                 .minimum = 1,
402                 .maximum = 31,
403                 .step = 1,
404                 .default_value = 1,
405         },
406         {
407                 .id = V4L2_CID_MPEG_VIDEO_MPEG4_MIN_QP,
408                 .type = V4L2_CTRL_TYPE_INTEGER,
409                 .name = "MPEG4 Minimum QP value",
410                 .minimum = 1,
411                 .maximum = 31,
412                 .step = 1,
413                 .default_value = 1,
414         },
415         {
416                 .id = V4L2_CID_MPEG_VIDEO_MPEG4_MAX_QP,
417                 .type = V4L2_CTRL_TYPE_INTEGER,
418                 .name = "MPEG4 Maximum QP value",
419                 .minimum = 0,
420                 .maximum = 51,
421                 .step = 1,
422                 .default_value = 1,
423         },
424         {
425                 .id = V4L2_CID_MPEG_VIDEO_MPEG4_P_FRAME_QP,
426                 .type = V4L2_CTRL_TYPE_INTEGER,
427                 .name = "MPEG4 P frame QP value",
428                 .minimum = 1,
429                 .maximum = 31,
430                 .step = 1,
431                 .default_value = 1,
432         },
433         {
434                 .id = V4L2_CID_MPEG_VIDEO_MPEG4_B_FRAME_QP,
435                 .type = V4L2_CTRL_TYPE_INTEGER,
436                 .name = "MPEG4 B frame QP value",
437                 .minimum = 1,
438                 .maximum = 31,
439                 .step = 1,
440                 .default_value = 1,
441         },
442         {
443                 .id = V4L2_CID_MPEG_MFC51_VIDEO_H264_ADAPTIVE_RC_DARK,
444                 .type = V4L2_CTRL_TYPE_BOOLEAN,
445                 .name = "H264 Dark Reg Adaptive RC",
446                 .minimum = 0,
447                 .maximum = 1,
448                 .step = 1,
449                 .default_value = 0,
450         },
451         {
452                 .id = V4L2_CID_MPEG_MFC51_VIDEO_H264_ADAPTIVE_RC_SMOOTH,
453                 .type = V4L2_CTRL_TYPE_BOOLEAN,
454                 .name = "H264 Smooth Reg Adaptive RC",
455                 .minimum = 0,
456                 .maximum = 1,
457                 .step = 1,
458                 .default_value = 0,
459         },
460         {
461                 .id = V4L2_CID_MPEG_MFC51_VIDEO_H264_ADAPTIVE_RC_STATIC,
462                 .type = V4L2_CTRL_TYPE_BOOLEAN,
463                 .name = "H264 Static Reg Adaptive RC",
464                 .minimum = 0,
465                 .maximum = 1,
466                 .step = 1,
467                 .default_value = 0,
468         },
469         {
470                 .id = V4L2_CID_MPEG_MFC51_VIDEO_H264_ADAPTIVE_RC_ACTIVITY,
471                 .type = V4L2_CTRL_TYPE_BOOLEAN,
472                 .name = "H264 Activity Reg Adaptive RC",
473                 .minimum = 0,
474                 .maximum = 1,
475                 .step = 1,
476                 .default_value = 0,
477         },
478         {
479                 .id = V4L2_CID_MPEG_VIDEO_H264_VUI_SAR_ENABLE,
480                 .type = V4L2_CTRL_TYPE_BOOLEAN,
481                 .minimum = 0,
482                 .maximum = 1,
483                 .step = 1,
484                 .default_value = 0,
485         },
486         {
487                 .id = V4L2_CID_MPEG_VIDEO_H264_VUI_SAR_IDC,
488                 .type = V4L2_CTRL_TYPE_MENU,
489                 .minimum = V4L2_MPEG_VIDEO_H264_VUI_SAR_IDC_UNSPECIFIED,
490                 .maximum = V4L2_MPEG_VIDEO_H264_VUI_SAR_IDC_EXTENDED,
491                 .default_value = V4L2_MPEG_VIDEO_H264_VUI_SAR_IDC_UNSPECIFIED,
492                 .menu_skip_mask = 0,
493         },
494         {
495                 .id = V4L2_CID_MPEG_VIDEO_H264_VUI_EXT_SAR_WIDTH,
496                 .type = V4L2_CTRL_TYPE_INTEGER,
497                 .minimum = 0,
498                 .maximum = (1 << 16) - 1,
499                 .step = 1,
500                 .default_value = 0,
501         },
502         {
503                 .id = V4L2_CID_MPEG_VIDEO_H264_VUI_EXT_SAR_HEIGHT,
504                 .type = V4L2_CTRL_TYPE_INTEGER,
505                 .minimum = 0,
506                 .maximum = (1 << 16) - 1,
507                 .step = 1,
508                 .default_value = 0,
509         },
510         {
511                 .id = V4L2_CID_MPEG_VIDEO_GOP_CLOSURE,
512                 .type = V4L2_CTRL_TYPE_BOOLEAN,
513                 .minimum = 0,
514                 .maximum = 1,
515                 .step = 1,
516                 .default_value = 1,
517         },
518         {
519                 .id = V4L2_CID_MPEG_VIDEO_H264_I_PERIOD,
520                 .type = V4L2_CTRL_TYPE_INTEGER,
521                 .minimum = 0,
522                 .maximum = (1 << 16) - 1,
523                 .step = 1,
524                 .default_value = 0,
525         },
526         {
527                 .id = V4L2_CID_MPEG_VIDEO_MPEG4_PROFILE,
528                 .type = V4L2_CTRL_TYPE_MENU,
529                 .minimum = V4L2_MPEG_VIDEO_MPEG4_PROFILE_SIMPLE,
530                 .maximum = V4L2_MPEG_VIDEO_MPEG4_PROFILE_ADVANCED_SIMPLE,
531                 .default_value = V4L2_MPEG_VIDEO_MPEG4_PROFILE_SIMPLE,
532                 .menu_skip_mask = 0,
533         },
534         {
535                 .id = V4L2_CID_MPEG_VIDEO_MPEG4_QPEL,
536                 .type = V4L2_CTRL_TYPE_BOOLEAN,
537                 .minimum = 0,
538                 .maximum = 1,
539                 .step = 1,
540                 .default_value = 0,
541         },
542 };
543
544 #define NUM_CTRLS ARRAY_SIZE(controls)
545 static const char * const *mfc51_get_menu(u32 id)
546 {
547         static const char * const mfc51_video_frame_skip[] = {
548                 "Disabled",
549                 "Level Limit",
550                 "VBV/CPB Limit",
551                 NULL,
552         };
553         static const char * const mfc51_video_force_frame[] = {
554                 "Disabled",
555                 "I Frame",
556                 "Not Coded",
557                 NULL,
558         };
559         switch (id) {
560         case V4L2_CID_MPEG_MFC51_VIDEO_FRAME_SKIP_MODE:
561                 return mfc51_video_frame_skip;
562         case V4L2_CID_MPEG_MFC51_VIDEO_FORCE_FRAME_TYPE:
563                 return mfc51_video_force_frame;
564         }
565         return NULL;
566 }
567
568 static int s5p_mfc_ctx_ready(struct s5p_mfc_ctx *ctx)
569 {
570         mfc_debug(2, "src=%d, dst=%d, state=%d\n",
571                   ctx->src_queue_cnt, ctx->dst_queue_cnt, ctx->state);
572         /* context is ready to make header */
573         if (ctx->state == MFCINST_GOT_INST && ctx->dst_queue_cnt >= 1)
574                 return 1;
575         /* context is ready to encode a frame */
576         if (ctx->state == MFCINST_RUNNING &&
577                 ctx->src_queue_cnt >= 1 && ctx->dst_queue_cnt >= 1)
578                 return 1;
579         /* context is ready to encode remain frames */
580         if (ctx->state == MFCINST_FINISHING &&
581                 ctx->src_queue_cnt >= 1 && ctx->dst_queue_cnt >= 1)
582                 return 1;
583         mfc_debug(2, "ctx is not ready\n");
584         return 0;
585 }
586
587 static void cleanup_ref_queue(struct s5p_mfc_ctx *ctx)
588 {
589         struct s5p_mfc_buf *mb_entry;
590         unsigned long mb_y_addr, mb_c_addr;
591
592         /* move buffers in ref queue to src queue */
593         while (!list_empty(&ctx->ref_queue)) {
594                 mb_entry = list_entry((&ctx->ref_queue)->next,
595                                                 struct s5p_mfc_buf, list);
596                 mb_y_addr = vb2_dma_contig_plane_dma_addr(mb_entry->b, 0);
597                 mb_c_addr = vb2_dma_contig_plane_dma_addr(mb_entry->b, 1);
598                 list_del(&mb_entry->list);
599                 ctx->ref_queue_cnt--;
600                 list_add_tail(&mb_entry->list, &ctx->src_queue);
601                 ctx->src_queue_cnt++;
602         }
603         mfc_debug(2, "enc src count: %d, enc ref count: %d\n",
604                   ctx->src_queue_cnt, ctx->ref_queue_cnt);
605         INIT_LIST_HEAD(&ctx->ref_queue);
606         ctx->ref_queue_cnt = 0;
607 }
608
609 static int enc_pre_seq_start(struct s5p_mfc_ctx *ctx)
610 {
611         struct s5p_mfc_dev *dev = ctx->dev;
612         struct s5p_mfc_buf *dst_mb;
613         unsigned long dst_addr;
614         unsigned int dst_size;
615         unsigned long flags;
616
617         spin_lock_irqsave(&dev->irqlock, flags);
618         dst_mb = list_entry(ctx->dst_queue.next, struct s5p_mfc_buf, list);
619         dst_addr = vb2_dma_contig_plane_dma_addr(dst_mb->b, 0);
620         dst_size = vb2_plane_size(dst_mb->b, 0);
621         s5p_mfc_set_enc_stream_buffer(ctx, dst_addr, dst_size);
622         spin_unlock_irqrestore(&dev->irqlock, flags);
623         return 0;
624 }
625
626 static int enc_post_seq_start(struct s5p_mfc_ctx *ctx)
627 {
628         struct s5p_mfc_dev *dev = ctx->dev;
629         struct s5p_mfc_enc_params *p = &ctx->enc_params;
630         struct s5p_mfc_buf *dst_mb;
631         unsigned long flags;
632
633         if (p->seq_hdr_mode == V4L2_MPEG_VIDEO_HEADER_MODE_SEPARATE) {
634                 spin_lock_irqsave(&dev->irqlock, flags);
635                 dst_mb = list_entry(ctx->dst_queue.next,
636                                 struct s5p_mfc_buf, list);
637                 list_del(&dst_mb->list);
638                 ctx->dst_queue_cnt--;
639                 vb2_set_plane_payload(dst_mb->b, 0,
640                                                 s5p_mfc_get_enc_strm_size());
641                 vb2_buffer_done(dst_mb->b, VB2_BUF_STATE_DONE);
642                 spin_unlock_irqrestore(&dev->irqlock, flags);
643         }
644         ctx->state = MFCINST_RUNNING;
645         if (s5p_mfc_ctx_ready(ctx)) {
646                 spin_lock_irqsave(&dev->condlock, flags);
647                 set_bit(ctx->num, &dev->ctx_work_bits);
648                 spin_unlock_irqrestore(&dev->condlock, flags);
649         }
650         s5p_mfc_try_run(dev);
651         return 0;
652 }
653
654 static int enc_pre_frame_start(struct s5p_mfc_ctx *ctx)
655 {
656         struct s5p_mfc_dev *dev = ctx->dev;
657         struct s5p_mfc_buf *dst_mb;
658         struct s5p_mfc_buf *src_mb;
659         unsigned long flags;
660         unsigned long src_y_addr, src_c_addr, dst_addr;
661         unsigned int dst_size;
662
663         spin_lock_irqsave(&dev->irqlock, flags);
664         src_mb = list_entry(ctx->src_queue.next, struct s5p_mfc_buf, list);
665         src_y_addr = vb2_dma_contig_plane_dma_addr(src_mb->b, 0);
666         src_c_addr = vb2_dma_contig_plane_dma_addr(src_mb->b, 1);
667         s5p_mfc_set_enc_frame_buffer(ctx, src_y_addr, src_c_addr);
668         spin_unlock_irqrestore(&dev->irqlock, flags);
669
670         spin_lock_irqsave(&dev->irqlock, flags);
671         dst_mb = list_entry(ctx->dst_queue.next, struct s5p_mfc_buf, list);
672         dst_addr = vb2_dma_contig_plane_dma_addr(dst_mb->b, 0);
673         dst_size = vb2_plane_size(dst_mb->b, 0);
674         s5p_mfc_set_enc_stream_buffer(ctx, dst_addr, dst_size);
675         spin_unlock_irqrestore(&dev->irqlock, flags);
676
677         return 0;
678 }
679
680 static int enc_post_frame_start(struct s5p_mfc_ctx *ctx)
681 {
682         struct s5p_mfc_dev *dev = ctx->dev;
683         struct s5p_mfc_buf *mb_entry;
684         unsigned long enc_y_addr, enc_c_addr;
685         unsigned long mb_y_addr, mb_c_addr;
686         int slice_type;
687         unsigned int strm_size;
688         unsigned long flags;
689
690         slice_type = s5p_mfc_get_enc_slice_type();
691         strm_size = s5p_mfc_get_enc_strm_size();
692         mfc_debug(2, "Encoded slice type: %d", slice_type);
693         mfc_debug(2, "Encoded stream size: %d", strm_size);
694         mfc_debug(2, "Display order: %d",
695                   mfc_read(dev, S5P_FIMV_ENC_SI_PIC_CNT));
696         spin_lock_irqsave(&dev->irqlock, flags);
697         if (slice_type >= 0) {
698                 s5p_mfc_get_enc_frame_buffer(ctx, &enc_y_addr, &enc_c_addr);
699                 list_for_each_entry(mb_entry, &ctx->src_queue, list) {
700                         mb_y_addr = vb2_dma_contig_plane_dma_addr(mb_entry->b, 0);
701                         mb_c_addr = vb2_dma_contig_plane_dma_addr(mb_entry->b, 1);
702                         if ((enc_y_addr == mb_y_addr) &&
703                                                 (enc_c_addr == mb_c_addr)) {
704                                 list_del(&mb_entry->list);
705                                 ctx->src_queue_cnt--;
706                                 vb2_buffer_done(mb_entry->b,
707                                                         VB2_BUF_STATE_DONE);
708                                 break;
709                         }
710                 }
711                 list_for_each_entry(mb_entry, &ctx->ref_queue, list) {
712                         mb_y_addr = vb2_dma_contig_plane_dma_addr(mb_entry->b, 0);
713                         mb_c_addr = vb2_dma_contig_plane_dma_addr(mb_entry->b, 1);
714                         if ((enc_y_addr == mb_y_addr) &&
715                                                 (enc_c_addr == mb_c_addr)) {
716                                 list_del(&mb_entry->list);
717                                 ctx->ref_queue_cnt--;
718                                 vb2_buffer_done(mb_entry->b,
719                                                         VB2_BUF_STATE_DONE);
720                                 break;
721                         }
722                 }
723         }
724         if ((ctx->src_queue_cnt > 0) && (ctx->state == MFCINST_RUNNING)) {
725                 mb_entry = list_entry(ctx->src_queue.next, struct s5p_mfc_buf,
726                                                                         list);
727                 if (mb_entry->used) {
728                         list_del(&mb_entry->list);
729                         ctx->src_queue_cnt--;
730                         list_add_tail(&mb_entry->list, &ctx->ref_queue);
731                         ctx->ref_queue_cnt++;
732                 }
733                 mfc_debug(2, "enc src count: %d, enc ref count: %d\n",
734                           ctx->src_queue_cnt, ctx->ref_queue_cnt);
735         }
736         if (strm_size > 0) {
737                 /* at least one more dest. buffers exist always  */
738                 mb_entry = list_entry(ctx->dst_queue.next, struct s5p_mfc_buf,
739                                                                         list);
740                 list_del(&mb_entry->list);
741                 ctx->dst_queue_cnt--;
742                 switch (slice_type) {
743                 case S5P_FIMV_ENC_SI_SLICE_TYPE_I:
744                         mb_entry->b->v4l2_buf.flags |= V4L2_BUF_FLAG_KEYFRAME;
745                         break;
746                 case S5P_FIMV_ENC_SI_SLICE_TYPE_P:
747                         mb_entry->b->v4l2_buf.flags |= V4L2_BUF_FLAG_PFRAME;
748                         break;
749                 case S5P_FIMV_ENC_SI_SLICE_TYPE_B:
750                         mb_entry->b->v4l2_buf.flags |= V4L2_BUF_FLAG_BFRAME;
751                         break;
752                 }
753                 vb2_set_plane_payload(mb_entry->b, 0, strm_size);
754                 vb2_buffer_done(mb_entry->b, VB2_BUF_STATE_DONE);
755         }
756         spin_unlock_irqrestore(&dev->irqlock, flags);
757         if ((ctx->src_queue_cnt == 0) || (ctx->dst_queue_cnt == 0)) {
758                 spin_lock(&dev->condlock);
759                 clear_bit(ctx->num, &dev->ctx_work_bits);
760                 spin_unlock(&dev->condlock);
761         }
762         return 0;
763 }
764
765 static struct s5p_mfc_codec_ops encoder_codec_ops = {
766         .pre_seq_start          = enc_pre_seq_start,
767         .post_seq_start         = enc_post_seq_start,
768         .pre_frame_start        = enc_pre_frame_start,
769         .post_frame_start       = enc_post_frame_start,
770 };
771
772 /* Query capabilities of the device */
773 static int vidioc_querycap(struct file *file, void *priv,
774                            struct v4l2_capability *cap)
775 {
776         struct s5p_mfc_dev *dev = video_drvdata(file);
777
778         strncpy(cap->driver, dev->plat_dev->name, sizeof(cap->driver) - 1);
779         strncpy(cap->card, dev->plat_dev->name, sizeof(cap->card) - 1);
780         cap->bus_info[0] = 0;
781         cap->version = KERNEL_VERSION(1, 0, 0);
782         /*
783          * This is only a mem-to-mem video device. The capture and output
784          * device capability flags are left only for backward compatibility
785          * and are scheduled for removal.
786          */
787         cap->capabilities = V4L2_CAP_VIDEO_M2M_MPLANE | V4L2_CAP_STREAMING |
788                             V4L2_CAP_VIDEO_CAPTURE_MPLANE |
789                             V4L2_CAP_VIDEO_OUTPUT_MPLANE;
790         return 0;
791 }
792
793 static int vidioc_enum_fmt(struct v4l2_fmtdesc *f, bool mplane, bool out)
794 {
795         struct s5p_mfc_fmt *fmt;
796         int i, j = 0;
797
798         for (i = 0; i < ARRAY_SIZE(formats); ++i) {
799                 if (mplane && formats[i].num_planes == 1)
800                         continue;
801                 else if (!mplane && formats[i].num_planes > 1)
802                         continue;
803                 if (out && formats[i].type != MFC_FMT_RAW)
804                         continue;
805                 else if (!out && formats[i].type != MFC_FMT_ENC)
806                         continue;
807                 if (j == f->index) {
808                         fmt = &formats[i];
809                         strlcpy(f->description, fmt->name,
810                                 sizeof(f->description));
811                         f->pixelformat = fmt->fourcc;
812                         return 0;
813                 }
814                 ++j;
815         }
816         return -EINVAL;
817 }
818
819 static int vidioc_enum_fmt_vid_cap(struct file *file, void *pirv,
820                                    struct v4l2_fmtdesc *f)
821 {
822         return vidioc_enum_fmt(f, false, false);
823 }
824
825 static int vidioc_enum_fmt_vid_cap_mplane(struct file *file, void *pirv,
826                                           struct v4l2_fmtdesc *f)
827 {
828         return vidioc_enum_fmt(f, true, false);
829 }
830
831 static int vidioc_enum_fmt_vid_out(struct file *file, void *prov,
832                                    struct v4l2_fmtdesc *f)
833 {
834         return vidioc_enum_fmt(f, false, true);
835 }
836
837 static int vidioc_enum_fmt_vid_out_mplane(struct file *file, void *prov,
838                                           struct v4l2_fmtdesc *f)
839 {
840         return vidioc_enum_fmt(f, true, true);
841 }
842
843 static int vidioc_g_fmt(struct file *file, void *priv, struct v4l2_format *f)
844 {
845         struct s5p_mfc_ctx *ctx = fh_to_ctx(priv);
846         struct v4l2_pix_format_mplane *pix_fmt_mp = &f->fmt.pix_mp;
847
848         mfc_debug(2, "f->type = %d ctx->state = %d\n", f->type, ctx->state);
849         if (f->type == V4L2_BUF_TYPE_VIDEO_CAPTURE_MPLANE) {
850                 /* This is run on output (encoder dest) */
851                 pix_fmt_mp->width = 0;
852                 pix_fmt_mp->height = 0;
853                 pix_fmt_mp->field = V4L2_FIELD_NONE;
854                 pix_fmt_mp->pixelformat = ctx->dst_fmt->fourcc;
855                 pix_fmt_mp->num_planes = ctx->dst_fmt->num_planes;
856
857                 pix_fmt_mp->plane_fmt[0].bytesperline = ctx->enc_dst_buf_size;
858                 pix_fmt_mp->plane_fmt[0].sizeimage = ctx->enc_dst_buf_size;
859         } else if (f->type == V4L2_BUF_TYPE_VIDEO_OUTPUT_MPLANE) {
860                 /* This is run on capture (encoder src) */
861                 pix_fmt_mp->width = ctx->img_width;
862                 pix_fmt_mp->height = ctx->img_height;
863
864                 pix_fmt_mp->field = V4L2_FIELD_NONE;
865                 pix_fmt_mp->pixelformat = ctx->src_fmt->fourcc;
866                 pix_fmt_mp->num_planes = ctx->src_fmt->num_planes;
867
868                 pix_fmt_mp->plane_fmt[0].bytesperline = ctx->buf_width;
869                 pix_fmt_mp->plane_fmt[0].sizeimage = ctx->luma_size;
870                 pix_fmt_mp->plane_fmt[1].bytesperline = ctx->buf_width;
871                 pix_fmt_mp->plane_fmt[1].sizeimage = ctx->chroma_size;
872         } else {
873                 mfc_err("invalid buf type\n");
874                 return -EINVAL;
875         }
876         return 0;
877 }
878
879 static int vidioc_try_fmt(struct file *file, void *priv, struct v4l2_format *f)
880 {
881         struct s5p_mfc_fmt *fmt;
882         struct v4l2_pix_format_mplane *pix_fmt_mp = &f->fmt.pix_mp;
883
884         if (f->type == V4L2_BUF_TYPE_VIDEO_CAPTURE_MPLANE) {
885                 fmt = find_format(f, MFC_FMT_ENC);
886                 if (!fmt) {
887                         mfc_err("failed to try output format\n");
888                         return -EINVAL;
889                 }
890
891                 if (pix_fmt_mp->plane_fmt[0].sizeimage == 0) {
892                         mfc_err("must be set encoding output size\n");
893                         return -EINVAL;
894                 }
895
896                 pix_fmt_mp->plane_fmt[0].bytesperline =
897                         pix_fmt_mp->plane_fmt[0].sizeimage;
898         } else if (f->type == V4L2_BUF_TYPE_VIDEO_OUTPUT_MPLANE) {
899                 fmt = find_format(f, MFC_FMT_RAW);
900                 if (!fmt) {
901                         mfc_err("failed to try output format\n");
902                         return -EINVAL;
903                 }
904
905                 if (fmt->num_planes != pix_fmt_mp->num_planes) {
906                         mfc_err("failed to try output format\n");
907                         return -EINVAL;
908                 }
909                 v4l_bound_align_image(&pix_fmt_mp->width, 8, 1920, 1,
910                         &pix_fmt_mp->height, 4, 1080, 1, 0);
911         } else {
912                 mfc_err("invalid buf type\n");
913                 return -EINVAL;
914         }
915         return 0;
916 }
917
918 static int vidioc_s_fmt(struct file *file, void *priv, struct v4l2_format *f)
919 {
920         struct s5p_mfc_dev *dev = video_drvdata(file);
921         struct s5p_mfc_ctx *ctx = fh_to_ctx(priv);
922         struct s5p_mfc_fmt *fmt;
923         struct v4l2_pix_format_mplane *pix_fmt_mp = &f->fmt.pix_mp;
924         unsigned long flags;
925         int ret = 0;
926
927         ret = vidioc_try_fmt(file, priv, f);
928         if (ret)
929                 return ret;
930         if (ctx->vq_src.streaming || ctx->vq_dst.streaming) {
931                 v4l2_err(&dev->v4l2_dev, "%s queue busy\n", __func__);
932                 ret = -EBUSY;
933                 goto out;
934         }
935         if (f->type == V4L2_BUF_TYPE_VIDEO_CAPTURE_MPLANE) {
936                 fmt = find_format(f, MFC_FMT_ENC);
937                 if (!fmt) {
938                         mfc_err("failed to set capture format\n");
939                         return -EINVAL;
940                 }
941                 ctx->state = MFCINST_INIT;
942                 ctx->dst_fmt = fmt;
943                 ctx->codec_mode = ctx->dst_fmt->codec_mode;
944                 ctx->enc_dst_buf_size = pix_fmt_mp->plane_fmt[0].sizeimage;
945                 pix_fmt_mp->plane_fmt[0].bytesperline = 0;
946                 ctx->dst_bufs_cnt = 0;
947                 ctx->capture_state = QUEUE_FREE;
948                 s5p_mfc_alloc_instance_buffer(ctx);
949                 spin_lock_irqsave(&dev->condlock, flags);
950                 set_bit(ctx->num, &dev->ctx_work_bits);
951                 spin_unlock_irqrestore(&dev->condlock, flags);
952                 s5p_mfc_clean_ctx_int_flags(ctx);
953                 s5p_mfc_try_run(dev);
954                 if (s5p_mfc_wait_for_done_ctx(ctx, \
955                                 S5P_FIMV_R2H_CMD_OPEN_INSTANCE_RET, 1)) {
956                                 /* Error or timeout */
957                         mfc_err("Error getting instance from hardware\n");
958                         s5p_mfc_release_instance_buffer(ctx);
959                         ret = -EIO;
960                         goto out;
961                 }
962                 mfc_debug(2, "Got instance number: %d\n", ctx->inst_no);
963         } else if (f->type == V4L2_BUF_TYPE_VIDEO_OUTPUT_MPLANE) {
964                 fmt = find_format(f, MFC_FMT_RAW);
965                 if (!fmt) {
966                         mfc_err("failed to set output format\n");
967                         return -EINVAL;
968                 }
969                 if (fmt->num_planes != pix_fmt_mp->num_planes) {
970                         mfc_err("failed to set output format\n");
971                         ret = -EINVAL;
972                         goto out;
973                 }
974                 ctx->src_fmt = fmt;
975                 ctx->img_width = pix_fmt_mp->width;
976                 ctx->img_height = pix_fmt_mp->height;
977                 mfc_debug(2, "codec number: %d\n", ctx->src_fmt->codec_mode);
978                 mfc_debug(2, "fmt - w: %d, h: %d, ctx - w: %d, h: %d\n",
979                         pix_fmt_mp->width, pix_fmt_mp->height,
980                         ctx->img_width, ctx->img_height);
981                 if (ctx->src_fmt->fourcc == V4L2_PIX_FMT_NV12M) {
982                         ctx->buf_width = ALIGN(ctx->img_width,
983                                                         S5P_FIMV_NV12M_HALIGN);
984                         ctx->luma_size = ALIGN(ctx->img_width,
985                                 S5P_FIMV_NV12M_HALIGN) * ALIGN(ctx->img_height,
986                                 S5P_FIMV_NV12M_LVALIGN);
987                         ctx->chroma_size = ALIGN(ctx->img_width,
988                                 S5P_FIMV_NV12M_HALIGN) * ALIGN((ctx->img_height
989                                 >> 1), S5P_FIMV_NV12M_CVALIGN);
990
991                         ctx->luma_size = ALIGN(ctx->luma_size,
992                                                         S5P_FIMV_NV12M_SALIGN);
993                         ctx->chroma_size = ALIGN(ctx->chroma_size,
994                                                         S5P_FIMV_NV12M_SALIGN);
995
996                         pix_fmt_mp->plane_fmt[0].sizeimage = ctx->luma_size;
997                         pix_fmt_mp->plane_fmt[0].bytesperline = ctx->buf_width;
998                         pix_fmt_mp->plane_fmt[1].sizeimage = ctx->chroma_size;
999                         pix_fmt_mp->plane_fmt[1].bytesperline = ctx->buf_width;
1000
1001                 } else if (ctx->src_fmt->fourcc == V4L2_PIX_FMT_NV12MT) {
1002                         ctx->buf_width = ALIGN(ctx->img_width,
1003                                                         S5P_FIMV_NV12MT_HALIGN);
1004                         ctx->luma_size = ALIGN(ctx->img_width,
1005                                 S5P_FIMV_NV12MT_HALIGN) * ALIGN(ctx->img_height,
1006                                 S5P_FIMV_NV12MT_VALIGN);
1007                         ctx->chroma_size = ALIGN(ctx->img_width,
1008                                 S5P_FIMV_NV12MT_HALIGN) * ALIGN((ctx->img_height
1009                                 >> 1), S5P_FIMV_NV12MT_VALIGN);
1010                         ctx->luma_size = ALIGN(ctx->luma_size,
1011                                                         S5P_FIMV_NV12MT_SALIGN);
1012                         ctx->chroma_size = ALIGN(ctx->chroma_size,
1013                                                         S5P_FIMV_NV12MT_SALIGN);
1014
1015                         pix_fmt_mp->plane_fmt[0].sizeimage = ctx->luma_size;
1016                         pix_fmt_mp->plane_fmt[0].bytesperline = ctx->buf_width;
1017                         pix_fmt_mp->plane_fmt[1].sizeimage = ctx->chroma_size;
1018                         pix_fmt_mp->plane_fmt[1].bytesperline = ctx->buf_width;
1019                 }
1020                 ctx->src_bufs_cnt = 0;
1021                 ctx->output_state = QUEUE_FREE;
1022         } else {
1023                 mfc_err("invalid buf type\n");
1024                 return -EINVAL;
1025         }
1026 out:
1027         mfc_debug_leave();
1028         return ret;
1029 }
1030
1031 static int vidioc_reqbufs(struct file *file, void *priv,
1032                                           struct v4l2_requestbuffers *reqbufs)
1033 {
1034         struct s5p_mfc_ctx *ctx = fh_to_ctx(priv);
1035         int ret = 0;
1036
1037         /* if memory is not mmp or userptr return error */
1038         if ((reqbufs->memory != V4L2_MEMORY_MMAP) &&
1039                 (reqbufs->memory != V4L2_MEMORY_USERPTR))
1040                 return -EINVAL;
1041         if (reqbufs->type == V4L2_BUF_TYPE_VIDEO_CAPTURE_MPLANE) {
1042                 if (ctx->capture_state != QUEUE_FREE) {
1043                         mfc_err("invalid capture state: %d\n",
1044                                                         ctx->capture_state);
1045                         return -EINVAL;
1046                 }
1047                 ret = vb2_reqbufs(&ctx->vq_dst, reqbufs);
1048                 if (ret != 0) {
1049                         mfc_err("error in vb2_reqbufs() for E(D)\n");
1050                         return ret;
1051                 }
1052                 ctx->capture_state = QUEUE_BUFS_REQUESTED;
1053                 ret = s5p_mfc_alloc_codec_buffers(ctx);
1054                 if (ret) {
1055                         mfc_err("Failed to allocate encoding buffers\n");
1056                         reqbufs->count = 0;
1057                         ret = vb2_reqbufs(&ctx->vq_dst, reqbufs);
1058                         return -ENOMEM;
1059                 }
1060         } else if (reqbufs->type == V4L2_BUF_TYPE_VIDEO_OUTPUT_MPLANE) {
1061                 if (ctx->output_state != QUEUE_FREE) {
1062                         mfc_err("invalid output state: %d\n",
1063                                                         ctx->output_state);
1064                         return -EINVAL;
1065                 }
1066                 ret = vb2_reqbufs(&ctx->vq_src, reqbufs);
1067                 if (ret != 0) {
1068                         mfc_err("error in vb2_reqbufs() for E(S)\n");
1069                         return ret;
1070                 }
1071                 ctx->output_state = QUEUE_BUFS_REQUESTED;
1072         } else {
1073                 mfc_err("invalid buf type\n");
1074                 return -EINVAL;
1075         }
1076         return ret;
1077 }
1078
1079 static int vidioc_querybuf(struct file *file, void *priv,
1080                                                    struct v4l2_buffer *buf)
1081 {
1082         struct s5p_mfc_ctx *ctx = fh_to_ctx(priv);
1083         int ret = 0;
1084
1085         /* if memory is not mmp or userptr return error */
1086         if ((buf->memory != V4L2_MEMORY_MMAP) &&
1087                 (buf->memory != V4L2_MEMORY_USERPTR))
1088                 return -EINVAL;
1089         if (buf->type == V4L2_BUF_TYPE_VIDEO_CAPTURE_MPLANE) {
1090                 if (ctx->state != MFCINST_GOT_INST) {
1091                         mfc_err("invalid context state: %d\n", ctx->state);
1092                         return -EINVAL;
1093                 }
1094                 ret = vb2_querybuf(&ctx->vq_dst, buf);
1095                 if (ret != 0) {
1096                         mfc_err("error in vb2_querybuf() for E(D)\n");
1097                         return ret;
1098                 }
1099                 buf->m.planes[0].m.mem_offset += DST_QUEUE_OFF_BASE;
1100         } else if (buf->type == V4L2_BUF_TYPE_VIDEO_OUTPUT_MPLANE) {
1101                 ret = vb2_querybuf(&ctx->vq_src, buf);
1102                 if (ret != 0) {
1103                         mfc_err("error in vb2_querybuf() for E(S)\n");
1104                         return ret;
1105                 }
1106         } else {
1107                 mfc_err("invalid buf type\n");
1108                 return -EINVAL;
1109         }
1110         return ret;
1111 }
1112
1113 /* Queue a buffer */
1114 static int vidioc_qbuf(struct file *file, void *priv, struct v4l2_buffer *buf)
1115 {
1116         struct s5p_mfc_ctx *ctx = fh_to_ctx(priv);
1117
1118         if (ctx->state == MFCINST_ERROR) {
1119                 mfc_err("Call on QBUF after unrecoverable error\n");
1120                 return -EIO;
1121         }
1122         if (buf->type == V4L2_BUF_TYPE_VIDEO_OUTPUT_MPLANE)
1123                 return vb2_qbuf(&ctx->vq_src, buf);
1124         else if (buf->type == V4L2_BUF_TYPE_VIDEO_CAPTURE_MPLANE)
1125                 return vb2_qbuf(&ctx->vq_dst, buf);
1126         return -EINVAL;
1127 }
1128
1129 /* Dequeue a buffer */
1130 static int vidioc_dqbuf(struct file *file, void *priv, struct v4l2_buffer *buf)
1131 {
1132         struct s5p_mfc_ctx *ctx = fh_to_ctx(priv);
1133
1134         if (ctx->state == MFCINST_ERROR) {
1135                 mfc_err("Call on DQBUF after unrecoverable error\n");
1136                 return -EIO;
1137         }
1138         if (buf->type == V4L2_BUF_TYPE_VIDEO_OUTPUT_MPLANE)
1139                 return vb2_dqbuf(&ctx->vq_src, buf, file->f_flags & O_NONBLOCK);
1140         else if (buf->type == V4L2_BUF_TYPE_VIDEO_CAPTURE_MPLANE)
1141                 return vb2_dqbuf(&ctx->vq_dst, buf, file->f_flags & O_NONBLOCK);
1142         return -EINVAL;
1143 }
1144
1145 /* Stream on */
1146 static int vidioc_streamon(struct file *file, void *priv,
1147                            enum v4l2_buf_type type)
1148 {
1149         struct s5p_mfc_ctx *ctx = fh_to_ctx(priv);
1150
1151         if (type == V4L2_BUF_TYPE_VIDEO_OUTPUT_MPLANE)
1152                 return vb2_streamon(&ctx->vq_src, type);
1153         else if (type == V4L2_BUF_TYPE_VIDEO_CAPTURE_MPLANE)
1154                 return vb2_streamon(&ctx->vq_dst, type);
1155         return -EINVAL;
1156 }
1157
1158 /* Stream off, which equals to a pause */
1159 static int vidioc_streamoff(struct file *file, void *priv,
1160                             enum v4l2_buf_type type)
1161 {
1162         struct s5p_mfc_ctx *ctx = fh_to_ctx(priv);
1163
1164         if (type == V4L2_BUF_TYPE_VIDEO_OUTPUT_MPLANE)
1165                 return vb2_streamoff(&ctx->vq_src, type);
1166         else if (type == V4L2_BUF_TYPE_VIDEO_CAPTURE_MPLANE)
1167                 return vb2_streamoff(&ctx->vq_dst, type);
1168         return -EINVAL;
1169 }
1170
1171 static inline int h264_level(enum v4l2_mpeg_video_h264_level lvl)
1172 {
1173         static unsigned int t[V4L2_MPEG_VIDEO_H264_LEVEL_4_0 + 1] = {
1174                 /* V4L2_MPEG_VIDEO_H264_LEVEL_1_0   */ 10,
1175                 /* V4L2_MPEG_VIDEO_H264_LEVEL_1B    */ 9,
1176                 /* V4L2_MPEG_VIDEO_H264_LEVEL_1_1   */ 11,
1177                 /* V4L2_MPEG_VIDEO_H264_LEVEL_1_2   */ 12,
1178                 /* V4L2_MPEG_VIDEO_H264_LEVEL_1_3   */ 13,
1179                 /* V4L2_MPEG_VIDEO_H264_LEVEL_2_0   */ 20,
1180                 /* V4L2_MPEG_VIDEO_H264_LEVEL_2_1   */ 21,
1181                 /* V4L2_MPEG_VIDEO_H264_LEVEL_2_2   */ 22,
1182                 /* V4L2_MPEG_VIDEO_H264_LEVEL_3_0   */ 30,
1183                 /* V4L2_MPEG_VIDEO_H264_LEVEL_3_1   */ 31,
1184                 /* V4L2_MPEG_VIDEO_H264_LEVEL_3_2   */ 32,
1185                 /* V4L2_MPEG_VIDEO_H264_LEVEL_4_0   */ 40,
1186         };
1187         return t[lvl];
1188 }
1189
1190 static inline int mpeg4_level(enum v4l2_mpeg_video_mpeg4_level lvl)
1191 {
1192         static unsigned int t[V4L2_MPEG_VIDEO_MPEG4_LEVEL_5 + 1] = {
1193                 /* V4L2_MPEG_VIDEO_MPEG4_LEVEL_0    */ 0,
1194                 /* V4L2_MPEG_VIDEO_MPEG4_LEVEL_0B   */ 9,
1195                 /* V4L2_MPEG_VIDEO_MPEG4_LEVEL_1    */ 1,
1196                 /* V4L2_MPEG_VIDEO_MPEG4_LEVEL_2    */ 2,
1197                 /* V4L2_MPEG_VIDEO_MPEG4_LEVEL_3    */ 3,
1198                 /* V4L2_MPEG_VIDEO_MPEG4_LEVEL_3B   */ 7,
1199                 /* V4L2_MPEG_VIDEO_MPEG4_LEVEL_4    */ 4,
1200                 /* V4L2_MPEG_VIDEO_MPEG4_LEVEL_5    */ 5,
1201         };
1202         return t[lvl];
1203 }
1204
1205 static inline int vui_sar_idc(enum v4l2_mpeg_video_h264_vui_sar_idc sar)
1206 {
1207         static unsigned int t[V4L2_MPEG_VIDEO_H264_VUI_SAR_IDC_EXTENDED + 1] = {
1208                 /* V4L2_MPEG_VIDEO_H264_VUI_SAR_IDC_UNSPECIFIED     */ 0,
1209                 /* V4L2_MPEG_VIDEO_H264_VUI_SAR_IDC_1x1             */ 1,
1210                 /* V4L2_MPEG_VIDEO_H264_VUI_SAR_IDC_12x11           */ 2,
1211                 /* V4L2_MPEG_VIDEO_H264_VUI_SAR_IDC_10x11           */ 3,
1212                 /* V4L2_MPEG_VIDEO_H264_VUI_SAR_IDC_16x11           */ 4,
1213                 /* V4L2_MPEG_VIDEO_H264_VUI_SAR_IDC_40x33           */ 5,
1214                 /* V4L2_MPEG_VIDEO_H264_VUI_SAR_IDC_24x11           */ 6,
1215                 /* V4L2_MPEG_VIDEO_H264_VUI_SAR_IDC_20x11           */ 7,
1216                 /* V4L2_MPEG_VIDEO_H264_VUI_SAR_IDC_32x11           */ 8,
1217                 /* V4L2_MPEG_VIDEO_H264_VUI_SAR_IDC_80x33           */ 9,
1218                 /* V4L2_MPEG_VIDEO_H264_VUI_SAR_IDC_18x11           */ 10,
1219                 /* V4L2_MPEG_VIDEO_H264_VUI_SAR_IDC_15x11           */ 11,
1220                 /* V4L2_MPEG_VIDEO_H264_VUI_SAR_IDC_64x33           */ 12,
1221                 /* V4L2_MPEG_VIDEO_H264_VUI_SAR_IDC_160x99          */ 13,
1222                 /* V4L2_MPEG_VIDEO_H264_VUI_SAR_IDC_4x3             */ 14,
1223                 /* V4L2_MPEG_VIDEO_H264_VUI_SAR_IDC_3x2             */ 15,
1224                 /* V4L2_MPEG_VIDEO_H264_VUI_SAR_IDC_2x1             */ 16,
1225                 /* V4L2_MPEG_VIDEO_H264_VUI_SAR_IDC_EXTENDED        */ 255,
1226         };
1227         return t[sar];
1228 }
1229
1230 static int s5p_mfc_enc_s_ctrl(struct v4l2_ctrl *ctrl)
1231 {
1232         struct s5p_mfc_ctx *ctx = ctrl_to_ctx(ctrl);
1233         struct s5p_mfc_dev *dev = ctx->dev;
1234         struct s5p_mfc_enc_params *p = &ctx->enc_params;
1235         int ret = 0;
1236
1237         switch (ctrl->id) {
1238         case V4L2_CID_MPEG_VIDEO_GOP_SIZE:
1239                 p->gop_size = ctrl->val;
1240                 break;
1241         case V4L2_CID_MPEG_VIDEO_MULTI_SLICE_MODE:
1242                 p->slice_mode = ctrl->val;
1243                 break;
1244         case V4L2_CID_MPEG_VIDEO_MULTI_SLICE_MAX_MB:
1245                 p->slice_mb = ctrl->val;
1246                 break;
1247         case V4L2_CID_MPEG_VIDEO_MULTI_SLICE_MAX_BYTES:
1248                 p->slice_bit = ctrl->val * 8;
1249                 break;
1250         case V4L2_CID_MPEG_VIDEO_CYCLIC_INTRA_REFRESH_MB:
1251                 p->intra_refresh_mb = ctrl->val;
1252                 break;
1253         case V4L2_CID_MPEG_MFC51_VIDEO_PADDING:
1254                 p->pad = ctrl->val;
1255                 break;
1256         case V4L2_CID_MPEG_MFC51_VIDEO_PADDING_YUV:
1257                 p->pad_luma = (ctrl->val >> 16) & 0xff;
1258                 p->pad_cb = (ctrl->val >> 8) & 0xff;
1259                 p->pad_cr = (ctrl->val >> 0) & 0xff;
1260                 break;
1261         case V4L2_CID_MPEG_VIDEO_FRAME_RC_ENABLE:
1262                 p->rc_frame = ctrl->val;
1263                 break;
1264         case V4L2_CID_MPEG_VIDEO_BITRATE:
1265                 p->rc_bitrate = ctrl->val;
1266                 break;
1267         case V4L2_CID_MPEG_MFC51_VIDEO_RC_REACTION_COEFF:
1268                 p->rc_reaction_coeff = ctrl->val;
1269                 break;
1270         case V4L2_CID_MPEG_MFC51_VIDEO_FORCE_FRAME_TYPE:
1271                 ctx->force_frame_type = ctrl->val;
1272                 break;
1273         case V4L2_CID_MPEG_VIDEO_VBV_SIZE:
1274                 p->vbv_size = ctrl->val;
1275                 break;
1276         case V4L2_CID_MPEG_VIDEO_H264_CPB_SIZE:
1277                 p->codec.h264.cpb_size = ctrl->val;
1278                 break;
1279         case V4L2_CID_MPEG_VIDEO_HEADER_MODE:
1280                 p->seq_hdr_mode = ctrl->val;
1281                 break;
1282         case V4L2_CID_MPEG_MFC51_VIDEO_FRAME_SKIP_MODE:
1283                 p->frame_skip_mode = ctrl->val;
1284                 break;
1285         case V4L2_CID_MPEG_MFC51_VIDEO_RC_FIXED_TARGET_BIT:
1286                 p->fixed_target_bit = ctrl->val;
1287                 break;
1288         case V4L2_CID_MPEG_VIDEO_B_FRAMES:
1289                 p->num_b_frame = ctrl->val;
1290                 break;
1291         case V4L2_CID_MPEG_VIDEO_H264_PROFILE:
1292                 switch (ctrl->val) {
1293                 case V4L2_MPEG_VIDEO_H264_PROFILE_MAIN:
1294                         p->codec.h264.profile =
1295                                         S5P_FIMV_ENC_PROFILE_H264_MAIN;
1296                         break;
1297                 case V4L2_MPEG_VIDEO_H264_PROFILE_HIGH:
1298                         p->codec.h264.profile =
1299                                         S5P_FIMV_ENC_PROFILE_H264_HIGH;
1300                         break;
1301                 case V4L2_MPEG_VIDEO_H264_PROFILE_BASELINE:
1302                         p->codec.h264.profile =
1303                                 S5P_FIMV_ENC_PROFILE_H264_BASELINE;
1304                         break;
1305                 default:
1306                         ret = -EINVAL;
1307                 }
1308                 break;
1309         case V4L2_CID_MPEG_VIDEO_H264_LEVEL:
1310                 p->codec.h264.level_v4l2 = ctrl->val;
1311                 p->codec.h264.level = h264_level(ctrl->val);
1312                 if (p->codec.h264.level < 0) {
1313                         mfc_err("Level number is wrong\n");
1314                         ret = p->codec.h264.level;
1315                 }
1316                 break;
1317         case V4L2_CID_MPEG_VIDEO_MPEG4_LEVEL:
1318                 p->codec.mpeg4.level_v4l2 = ctrl->val;
1319                 p->codec.mpeg4.level = mpeg4_level(ctrl->val);
1320                 if (p->codec.mpeg4.level < 0) {
1321                         mfc_err("Level number is wrong\n");
1322                         ret = p->codec.mpeg4.level;
1323                 }
1324                 break;
1325         case V4L2_CID_MPEG_VIDEO_H264_LOOP_FILTER_MODE:
1326                 p->codec.h264.loop_filter_mode = ctrl->val;
1327                 break;
1328         case V4L2_CID_MPEG_VIDEO_H264_LOOP_FILTER_ALPHA:
1329                 p->codec.h264.loop_filter_alpha = ctrl->val;
1330                 break;
1331         case V4L2_CID_MPEG_VIDEO_H264_LOOP_FILTER_BETA:
1332                 p->codec.h264.loop_filter_beta = ctrl->val;
1333                 break;
1334         case V4L2_CID_MPEG_VIDEO_H264_ENTROPY_MODE:
1335                 p->codec.h264.entropy_mode = ctrl->val;
1336                 break;
1337         case V4L2_CID_MPEG_MFC51_VIDEO_H264_NUM_REF_PIC_FOR_P:
1338                 p->codec.h264.num_ref_pic_4p = ctrl->val;
1339                 break;
1340         case V4L2_CID_MPEG_VIDEO_H264_8X8_TRANSFORM:
1341                 p->codec.h264._8x8_transform = ctrl->val;
1342                 break;
1343         case V4L2_CID_MPEG_VIDEO_MB_RC_ENABLE:
1344                 p->codec.h264.rc_mb = ctrl->val;
1345                 break;
1346         case V4L2_CID_MPEG_VIDEO_H264_I_FRAME_QP:
1347                 p->codec.h264.rc_frame_qp = ctrl->val;
1348                 break;
1349         case V4L2_CID_MPEG_VIDEO_H264_MIN_QP:
1350                 p->codec.h264.rc_min_qp = ctrl->val;
1351                 break;
1352         case V4L2_CID_MPEG_VIDEO_H264_MAX_QP:
1353                 p->codec.h264.rc_max_qp = ctrl->val;
1354                 break;
1355         case V4L2_CID_MPEG_VIDEO_H264_P_FRAME_QP:
1356                 p->codec.h264.rc_p_frame_qp = ctrl->val;
1357                 break;
1358         case V4L2_CID_MPEG_VIDEO_H264_B_FRAME_QP:
1359                 p->codec.h264.rc_b_frame_qp = ctrl->val;
1360                 break;
1361         case V4L2_CID_MPEG_VIDEO_MPEG4_I_FRAME_QP:
1362         case V4L2_CID_MPEG_VIDEO_H263_I_FRAME_QP:
1363                 p->codec.mpeg4.rc_frame_qp = ctrl->val;
1364                 break;
1365         case V4L2_CID_MPEG_VIDEO_MPEG4_MIN_QP:
1366         case V4L2_CID_MPEG_VIDEO_H263_MIN_QP:
1367                 p->codec.mpeg4.rc_min_qp = ctrl->val;
1368                 break;
1369         case V4L2_CID_MPEG_VIDEO_MPEG4_MAX_QP:
1370         case V4L2_CID_MPEG_VIDEO_H263_MAX_QP:
1371                 p->codec.mpeg4.rc_max_qp = ctrl->val;
1372                 break;
1373         case V4L2_CID_MPEG_VIDEO_MPEG4_P_FRAME_QP:
1374         case V4L2_CID_MPEG_VIDEO_H263_P_FRAME_QP:
1375                 p->codec.mpeg4.rc_p_frame_qp = ctrl->val;
1376                 break;
1377         case V4L2_CID_MPEG_VIDEO_MPEG4_B_FRAME_QP:
1378         case V4L2_CID_MPEG_VIDEO_H263_B_FRAME_QP:
1379                 p->codec.mpeg4.rc_b_frame_qp = ctrl->val;
1380                 break;
1381         case V4L2_CID_MPEG_MFC51_VIDEO_H264_ADAPTIVE_RC_DARK:
1382                 p->codec.h264.rc_mb_dark = ctrl->val;
1383                 break;
1384         case V4L2_CID_MPEG_MFC51_VIDEO_H264_ADAPTIVE_RC_SMOOTH:
1385                 p->codec.h264.rc_mb_smooth = ctrl->val;
1386                 break;
1387         case V4L2_CID_MPEG_MFC51_VIDEO_H264_ADAPTIVE_RC_STATIC:
1388                 p->codec.h264.rc_mb_static = ctrl->val;
1389                 break;
1390         case V4L2_CID_MPEG_MFC51_VIDEO_H264_ADAPTIVE_RC_ACTIVITY:
1391                 p->codec.h264.rc_mb_activity = ctrl->val;
1392                 break;
1393         case V4L2_CID_MPEG_VIDEO_H264_VUI_SAR_ENABLE:
1394                 p->codec.h264.vui_sar = ctrl->val;
1395                 break;
1396         case V4L2_CID_MPEG_VIDEO_H264_VUI_SAR_IDC:
1397                 p->codec.h264.vui_sar_idc = vui_sar_idc(ctrl->val);
1398                 break;
1399         case V4L2_CID_MPEG_VIDEO_H264_VUI_EXT_SAR_WIDTH:
1400                 p->codec.h264.vui_ext_sar_width = ctrl->val;
1401                 break;
1402         case V4L2_CID_MPEG_VIDEO_H264_VUI_EXT_SAR_HEIGHT:
1403                 p->codec.h264.vui_ext_sar_height = ctrl->val;
1404                 break;
1405         case V4L2_CID_MPEG_VIDEO_GOP_CLOSURE:
1406                 p->codec.h264.open_gop = !ctrl->val;
1407                 break;
1408         case V4L2_CID_MPEG_VIDEO_H264_I_PERIOD:
1409                 p->codec.h264.open_gop_size = ctrl->val;
1410                 break;
1411         case V4L2_CID_MPEG_VIDEO_MPEG4_PROFILE:
1412                 switch (ctrl->val) {
1413                 case V4L2_MPEG_VIDEO_MPEG4_PROFILE_SIMPLE:
1414                         p->codec.mpeg4.profile =
1415                                 S5P_FIMV_ENC_PROFILE_MPEG4_SIMPLE;
1416                         break;
1417                 case V4L2_MPEG_VIDEO_MPEG4_PROFILE_ADVANCED_SIMPLE:
1418                         p->codec.mpeg4.profile =
1419                         S5P_FIMV_ENC_PROFILE_MPEG4_ADVANCED_SIMPLE;
1420                         break;
1421                 default:
1422                         ret = -EINVAL;
1423                 }
1424                 break;
1425         case V4L2_CID_MPEG_VIDEO_MPEG4_QPEL:
1426                 p->codec.mpeg4.quarter_pixel = ctrl->val;
1427                 break;
1428         default:
1429                 v4l2_err(&dev->v4l2_dev, "Invalid control, id=%d, val=%d\n",
1430                                                         ctrl->id, ctrl->val);
1431                 ret = -EINVAL;
1432         }
1433         return ret;
1434 }
1435
1436 static const struct v4l2_ctrl_ops s5p_mfc_enc_ctrl_ops = {
1437         .s_ctrl = s5p_mfc_enc_s_ctrl,
1438 };
1439
1440 static int vidioc_s_parm(struct file *file, void *priv,
1441                          struct v4l2_streamparm *a)
1442 {
1443         struct s5p_mfc_ctx *ctx = fh_to_ctx(priv);
1444
1445         if (a->type == V4L2_BUF_TYPE_VIDEO_OUTPUT_MPLANE) {
1446                 ctx->enc_params.rc_framerate_num =
1447                                         a->parm.output.timeperframe.denominator;
1448                 ctx->enc_params.rc_framerate_denom =
1449                                         a->parm.output.timeperframe.numerator;
1450         } else {
1451                 mfc_err("Setting FPS is only possible for the output queue\n");
1452                 return -EINVAL;
1453         }
1454         return 0;
1455 }
1456
1457 static int vidioc_g_parm(struct file *file, void *priv,
1458                          struct v4l2_streamparm *a)
1459 {
1460         struct s5p_mfc_ctx *ctx = fh_to_ctx(priv);
1461
1462         if (a->type == V4L2_BUF_TYPE_VIDEO_OUTPUT) {
1463                 a->parm.output.timeperframe.denominator =
1464                                         ctx->enc_params.rc_framerate_num;
1465                 a->parm.output.timeperframe.numerator =
1466                                         ctx->enc_params.rc_framerate_denom;
1467         } else {
1468                 mfc_err("Setting FPS is only possible for the output queue\n");
1469                 return -EINVAL;
1470         }
1471         return 0;
1472 }
1473
1474 static const struct v4l2_ioctl_ops s5p_mfc_enc_ioctl_ops = {
1475         .vidioc_querycap = vidioc_querycap,
1476         .vidioc_enum_fmt_vid_cap = vidioc_enum_fmt_vid_cap,
1477         .vidioc_enum_fmt_vid_cap_mplane = vidioc_enum_fmt_vid_cap_mplane,
1478         .vidioc_enum_fmt_vid_out = vidioc_enum_fmt_vid_out,
1479         .vidioc_enum_fmt_vid_out_mplane = vidioc_enum_fmt_vid_out_mplane,
1480         .vidioc_g_fmt_vid_cap_mplane = vidioc_g_fmt,
1481         .vidioc_g_fmt_vid_out_mplane = vidioc_g_fmt,
1482         .vidioc_try_fmt_vid_cap_mplane = vidioc_try_fmt,
1483         .vidioc_try_fmt_vid_out_mplane = vidioc_try_fmt,
1484         .vidioc_s_fmt_vid_cap_mplane = vidioc_s_fmt,
1485         .vidioc_s_fmt_vid_out_mplane = vidioc_s_fmt,
1486         .vidioc_reqbufs = vidioc_reqbufs,
1487         .vidioc_querybuf = vidioc_querybuf,
1488         .vidioc_qbuf = vidioc_qbuf,
1489         .vidioc_dqbuf = vidioc_dqbuf,
1490         .vidioc_streamon = vidioc_streamon,
1491         .vidioc_streamoff = vidioc_streamoff,
1492         .vidioc_s_parm = vidioc_s_parm,
1493         .vidioc_g_parm = vidioc_g_parm,
1494 };
1495
1496 static int check_vb_with_fmt(struct s5p_mfc_fmt *fmt, struct vb2_buffer *vb)
1497 {
1498         int i;
1499
1500         if (!fmt)
1501                 return -EINVAL;
1502         if (fmt->num_planes != vb->num_planes) {
1503                 mfc_err("invalid plane number for the format\n");
1504                 return -EINVAL;
1505         }
1506         for (i = 0; i < fmt->num_planes; i++) {
1507                 if (!vb2_dma_contig_plane_dma_addr(vb, i)) {
1508                         mfc_err("failed to get plane cookie\n");
1509                         return -EINVAL;
1510                 }
1511                 mfc_debug(2, "index: %d, plane[%d] cookie: 0x%08zx",
1512                                 vb->v4l2_buf.index, i,
1513                                 vb2_dma_contig_plane_dma_addr(vb, i));
1514         }
1515         return 0;
1516 }
1517
1518 static int s5p_mfc_queue_setup(struct vb2_queue *vq,
1519                         const struct v4l2_format *fmt,
1520                         unsigned int *buf_count, unsigned int *plane_count,
1521                         unsigned int psize[], void *allocators[])
1522 {
1523         struct s5p_mfc_ctx *ctx = fh_to_ctx(vq->drv_priv);
1524
1525         if (ctx->state != MFCINST_GOT_INST) {
1526                 mfc_err("inavlid state: %d\n", ctx->state);
1527                 return -EINVAL;
1528         }
1529         if (vq->type == V4L2_BUF_TYPE_VIDEO_CAPTURE_MPLANE) {
1530                 if (ctx->dst_fmt)
1531                         *plane_count = ctx->dst_fmt->num_planes;
1532                 else
1533                         *plane_count = MFC_ENC_CAP_PLANE_COUNT;
1534                 if (*buf_count < 1)
1535                         *buf_count = 1;
1536                 if (*buf_count > MFC_MAX_BUFFERS)
1537                         *buf_count = MFC_MAX_BUFFERS;
1538                 psize[0] = ctx->enc_dst_buf_size;
1539                 allocators[0] = ctx->dev->alloc_ctx[MFC_BANK1_ALLOC_CTX];
1540         } else if (vq->type == V4L2_BUF_TYPE_VIDEO_OUTPUT_MPLANE) {
1541                 if (ctx->src_fmt)
1542                         *plane_count = ctx->src_fmt->num_planes;
1543                 else
1544                         *plane_count = MFC_ENC_OUT_PLANE_COUNT;
1545
1546                 if (*buf_count < 1)
1547                         *buf_count = 1;
1548                 if (*buf_count > MFC_MAX_BUFFERS)
1549                         *buf_count = MFC_MAX_BUFFERS;
1550                 psize[0] = ctx->luma_size;
1551                 psize[1] = ctx->chroma_size;
1552                 allocators[0] = ctx->dev->alloc_ctx[MFC_BANK2_ALLOC_CTX];
1553                 allocators[1] = ctx->dev->alloc_ctx[MFC_BANK2_ALLOC_CTX];
1554         } else {
1555                 mfc_err("inavlid queue type: %d\n", vq->type);
1556                 return -EINVAL;
1557         }
1558         return 0;
1559 }
1560
1561 static void s5p_mfc_unlock(struct vb2_queue *q)
1562 {
1563         struct s5p_mfc_ctx *ctx = fh_to_ctx(q->drv_priv);
1564         struct s5p_mfc_dev *dev = ctx->dev;
1565
1566         mutex_unlock(&dev->mfc_mutex);
1567 }
1568
1569 static void s5p_mfc_lock(struct vb2_queue *q)
1570 {
1571         struct s5p_mfc_ctx *ctx = fh_to_ctx(q->drv_priv);
1572         struct s5p_mfc_dev *dev = ctx->dev;
1573
1574         mutex_lock(&dev->mfc_mutex);
1575 }
1576
1577 static int s5p_mfc_buf_init(struct vb2_buffer *vb)
1578 {
1579         struct vb2_queue *vq = vb->vb2_queue;
1580         struct s5p_mfc_ctx *ctx = fh_to_ctx(vq->drv_priv);
1581         unsigned int i;
1582         int ret;
1583
1584         if (vq->type == V4L2_BUF_TYPE_VIDEO_CAPTURE_MPLANE) {
1585                 ret = check_vb_with_fmt(ctx->dst_fmt, vb);
1586                 if (ret < 0)
1587                         return ret;
1588                 i = vb->v4l2_buf.index;
1589                 ctx->dst_bufs[i].b = vb;
1590                 ctx->dst_bufs[i].cookie.stream =
1591                                         vb2_dma_contig_plane_dma_addr(vb, 0);
1592                 ctx->dst_bufs_cnt++;
1593         } else if (vq->type == V4L2_BUF_TYPE_VIDEO_OUTPUT_MPLANE) {
1594                 ret = check_vb_with_fmt(ctx->src_fmt, vb);
1595                 if (ret < 0)
1596                         return ret;
1597                 i = vb->v4l2_buf.index;
1598                 ctx->src_bufs[i].b = vb;
1599                 ctx->src_bufs[i].cookie.raw.luma =
1600                                         vb2_dma_contig_plane_dma_addr(vb, 0);
1601                 ctx->src_bufs[i].cookie.raw.chroma =
1602                                         vb2_dma_contig_plane_dma_addr(vb, 1);
1603                 ctx->src_bufs_cnt++;
1604         } else {
1605                 mfc_err("inavlid queue type: %d\n", vq->type);
1606                 return -EINVAL;
1607         }
1608         return 0;
1609 }
1610
1611 static int s5p_mfc_buf_prepare(struct vb2_buffer *vb)
1612 {
1613         struct vb2_queue *vq = vb->vb2_queue;
1614         struct s5p_mfc_ctx *ctx = fh_to_ctx(vq->drv_priv);
1615         int ret;
1616
1617         if (vq->type == V4L2_BUF_TYPE_VIDEO_CAPTURE_MPLANE) {
1618                 ret = check_vb_with_fmt(ctx->dst_fmt, vb);
1619                 if (ret < 0)
1620                         return ret;
1621                 mfc_debug(2, "plane size: %ld, dst size: %d\n",
1622                         vb2_plane_size(vb, 0), ctx->enc_dst_buf_size);
1623                 if (vb2_plane_size(vb, 0) < ctx->enc_dst_buf_size) {
1624                         mfc_err("plane size is too small for capture\n");
1625                         return -EINVAL;
1626                 }
1627         } else if (vq->type == V4L2_BUF_TYPE_VIDEO_OUTPUT_MPLANE) {
1628                 ret = check_vb_with_fmt(ctx->src_fmt, vb);
1629                 if (ret < 0)
1630                         return ret;
1631                 mfc_debug(2, "plane size: %ld, luma size: %d\n",
1632                         vb2_plane_size(vb, 0), ctx->luma_size);
1633                 mfc_debug(2, "plane size: %ld, chroma size: %d\n",
1634                         vb2_plane_size(vb, 1), ctx->chroma_size);
1635                 if (vb2_plane_size(vb, 0) < ctx->luma_size ||
1636                     vb2_plane_size(vb, 1) < ctx->chroma_size) {
1637                         mfc_err("plane size is too small for output\n");
1638                         return -EINVAL;
1639                 }
1640         } else {
1641                 mfc_err("inavlid queue type: %d\n", vq->type);
1642                 return -EINVAL;
1643         }
1644         return 0;
1645 }
1646
1647 static int s5p_mfc_start_streaming(struct vb2_queue *q, unsigned int count)
1648 {
1649         struct s5p_mfc_ctx *ctx = fh_to_ctx(q->drv_priv);
1650         struct s5p_mfc_dev *dev = ctx->dev;
1651         unsigned long flags;
1652
1653         v4l2_ctrl_handler_setup(&ctx->ctrl_handler);
1654         /* If context is ready then dev = work->data;schedule it to run */
1655         if (s5p_mfc_ctx_ready(ctx)) {
1656                 spin_lock_irqsave(&dev->condlock, flags);
1657                 set_bit(ctx->num, &dev->ctx_work_bits);
1658                 spin_unlock_irqrestore(&dev->condlock, flags);
1659         }
1660         s5p_mfc_try_run(dev);
1661         return 0;
1662 }
1663
1664 static int s5p_mfc_stop_streaming(struct vb2_queue *q)
1665 {
1666         unsigned long flags;
1667         struct s5p_mfc_ctx *ctx = fh_to_ctx(q->drv_priv);
1668         struct s5p_mfc_dev *dev = ctx->dev;
1669
1670         if ((ctx->state == MFCINST_FINISHING ||
1671                 ctx->state == MFCINST_RUNNING) &&
1672                 dev->curr_ctx == ctx->num && dev->hw_lock) {
1673                 ctx->state = MFCINST_ABORT;
1674                 s5p_mfc_wait_for_done_ctx(ctx, S5P_FIMV_R2H_CMD_FRAME_DONE_RET,
1675                                           0);
1676         }
1677         ctx->state = MFCINST_FINISHED;
1678         spin_lock_irqsave(&dev->irqlock, flags);
1679         if (q->type == V4L2_BUF_TYPE_VIDEO_CAPTURE_MPLANE) {
1680                 s5p_mfc_cleanup_queue(&ctx->dst_queue, &ctx->vq_dst);
1681                 INIT_LIST_HEAD(&ctx->dst_queue);
1682                 ctx->dst_queue_cnt = 0;
1683         }
1684         if (q->type == V4L2_BUF_TYPE_VIDEO_OUTPUT_MPLANE) {
1685                 cleanup_ref_queue(ctx);
1686                 s5p_mfc_cleanup_queue(&ctx->src_queue, &ctx->vq_src);
1687                 INIT_LIST_HEAD(&ctx->src_queue);
1688                 ctx->src_queue_cnt = 0;
1689         }
1690         spin_unlock_irqrestore(&dev->irqlock, flags);
1691         return 0;
1692 }
1693
1694 static void s5p_mfc_buf_queue(struct vb2_buffer *vb)
1695 {
1696         struct vb2_queue *vq = vb->vb2_queue;
1697         struct s5p_mfc_ctx *ctx = fh_to_ctx(vq->drv_priv);
1698         struct s5p_mfc_dev *dev = ctx->dev;
1699         unsigned long flags;
1700         struct s5p_mfc_buf *mfc_buf;
1701
1702         if (ctx->state == MFCINST_ERROR) {
1703                 vb2_buffer_done(vb, VB2_BUF_STATE_ERROR);
1704                 cleanup_ref_queue(ctx);
1705                 return;
1706         }
1707         if (vq->type == V4L2_BUF_TYPE_VIDEO_CAPTURE_MPLANE) {
1708                 mfc_buf = &ctx->dst_bufs[vb->v4l2_buf.index];
1709                 mfc_buf->used = 0;
1710                 /* Mark destination as available for use by MFC */
1711                 spin_lock_irqsave(&dev->irqlock, flags);
1712                 list_add_tail(&mfc_buf->list, &ctx->dst_queue);
1713                 ctx->dst_queue_cnt++;
1714                 spin_unlock_irqrestore(&dev->irqlock, flags);
1715         } else if (vq->type == V4L2_BUF_TYPE_VIDEO_OUTPUT_MPLANE) {
1716                 mfc_buf = &ctx->src_bufs[vb->v4l2_buf.index];
1717                 mfc_buf->used = 0;
1718                 spin_lock_irqsave(&dev->irqlock, flags);
1719                 if (vb->v4l2_planes[0].bytesused == 0) {
1720                         mfc_debug(1, "change state to FINISHING\n");
1721                         ctx->state = MFCINST_FINISHING;
1722                         vb2_buffer_done(vb, VB2_BUF_STATE_DONE);
1723                         cleanup_ref_queue(ctx);
1724                 } else {
1725                         list_add_tail(&mfc_buf->list, &ctx->src_queue);
1726                         ctx->src_queue_cnt++;
1727                 }
1728                 spin_unlock_irqrestore(&dev->irqlock, flags);
1729         } else {
1730                 mfc_err("unsupported buffer type (%d)\n", vq->type);
1731         }
1732         if (s5p_mfc_ctx_ready(ctx)) {
1733                 spin_lock_irqsave(&dev->condlock, flags);
1734                 set_bit(ctx->num, &dev->ctx_work_bits);
1735                 spin_unlock_irqrestore(&dev->condlock, flags);
1736         }
1737         s5p_mfc_try_run(dev);
1738 }
1739
1740 static struct vb2_ops s5p_mfc_enc_qops = {
1741         .queue_setup            = s5p_mfc_queue_setup,
1742         .wait_prepare           = s5p_mfc_unlock,
1743         .wait_finish            = s5p_mfc_lock,
1744         .buf_init               = s5p_mfc_buf_init,
1745         .buf_prepare            = s5p_mfc_buf_prepare,
1746         .start_streaming        = s5p_mfc_start_streaming,
1747         .stop_streaming         = s5p_mfc_stop_streaming,
1748         .buf_queue              = s5p_mfc_buf_queue,
1749 };
1750
1751 struct s5p_mfc_codec_ops *get_enc_codec_ops(void)
1752 {
1753         return &encoder_codec_ops;
1754 }
1755
1756 struct vb2_ops *get_enc_queue_ops(void)
1757 {
1758         return &s5p_mfc_enc_qops;
1759 }
1760
1761 const struct v4l2_ioctl_ops *get_enc_v4l2_ioctl_ops(void)
1762 {
1763         return &s5p_mfc_enc_ioctl_ops;
1764 }
1765
1766 #define IS_MFC51_PRIV(x) ((V4L2_CTRL_ID2CLASS(x) == V4L2_CTRL_CLASS_MPEG) \
1767                                                 && V4L2_CTRL_DRIVER_PRIV(x))
1768
1769 int s5p_mfc_enc_ctrls_setup(struct s5p_mfc_ctx *ctx)
1770 {
1771         struct v4l2_ctrl_config cfg;
1772         int i;
1773
1774         v4l2_ctrl_handler_init(&ctx->ctrl_handler, NUM_CTRLS);
1775         if (ctx->ctrl_handler.error) {
1776                 mfc_err("v4l2_ctrl_handler_init failed\n");
1777                 return ctx->ctrl_handler.error;
1778         }
1779         for (i = 0; i < NUM_CTRLS; i++) {
1780                 if (IS_MFC51_PRIV(controls[i].id)) {
1781                         memset(&cfg, 0, sizeof(struct v4l2_ctrl_config));
1782                         cfg.ops = &s5p_mfc_enc_ctrl_ops;
1783                         cfg.id = controls[i].id;
1784                         cfg.min = controls[i].minimum;
1785                         cfg.max = controls[i].maximum;
1786                         cfg.def = controls[i].default_value;
1787                         cfg.name = controls[i].name;
1788                         cfg.type = controls[i].type;
1789                         cfg.flags = 0;
1790
1791                         if (cfg.type == V4L2_CTRL_TYPE_MENU) {
1792                                 cfg.step = 0;
1793                                 cfg.menu_skip_mask = cfg.menu_skip_mask;
1794                                 cfg.qmenu = mfc51_get_menu(cfg.id);
1795                         } else {
1796                                 cfg.step = controls[i].step;
1797                                 cfg.menu_skip_mask = 0;
1798                         }
1799                         ctx->ctrls[i] = v4l2_ctrl_new_custom(&ctx->ctrl_handler,
1800                                         &cfg, NULL);
1801                 } else {
1802                         if (controls[i].type == V4L2_CTRL_TYPE_MENU) {
1803                                 ctx->ctrls[i] = v4l2_ctrl_new_std_menu(
1804                                         &ctx->ctrl_handler,
1805                                         &s5p_mfc_enc_ctrl_ops, controls[i].id,
1806                                         controls[i].maximum, 0,
1807                                         controls[i].default_value);
1808                         } else {
1809                                 ctx->ctrls[i] = v4l2_ctrl_new_std(
1810                                         &ctx->ctrl_handler,
1811                                         &s5p_mfc_enc_ctrl_ops, controls[i].id,
1812                                         controls[i].minimum,
1813                                         controls[i].maximum, controls[i].step,
1814                                         controls[i].default_value);
1815                         }
1816                 }
1817                 if (ctx->ctrl_handler.error) {
1818                         mfc_err("Adding control (%d) failed\n", i);
1819                         return ctx->ctrl_handler.error;
1820                 }
1821                 if (controls[i].is_volatile && ctx->ctrls[i])
1822                         ctx->ctrls[i]->flags |= V4L2_CTRL_FLAG_VOLATILE;
1823         }
1824         return 0;
1825 }
1826
1827 void s5p_mfc_enc_ctrls_delete(struct s5p_mfc_ctx *ctx)
1828 {
1829         int i;
1830
1831         v4l2_ctrl_handler_free(&ctx->ctrl_handler);
1832         for (i = 0; i < NUM_CTRLS; i++)
1833                 ctx->ctrls[i] = NULL;
1834 }