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