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