Linux Audio

Check our new training course

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