linux/drivers/media/platform/s5p-mfc/s5p_mfc_enc.c
<<
>>
Prefs
   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/videodev2.h>
  23#include <media/v4l2-event.h>
  24#include <linux/workqueue.h>
  25#include <media/v4l2-ctrls.h>
  26#include <media/videobuf2-v4l2.h>
  27#include "s5p_mfc_common.h"
  28#include "s5p_mfc_ctrl.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_NV12M
  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                .versions       = MFC_V6_BIT | MFC_V7_BIT,
  45        },
  46        {
  47                .name           = "4:2:0 2 Planes 64x32 Tiles",
  48                .fourcc         = V4L2_PIX_FMT_NV12MT,
  49                .codec_mode     = S5P_MFC_CODEC_NONE,
  50                .type           = MFC_FMT_RAW,
  51                .num_planes     = 2,
  52                .versions       = MFC_V5_BIT,
  53        },
  54        {
  55                .name           = "4:2:0 2 Planes Y/CbCr",
  56                .fourcc         = V4L2_PIX_FMT_NV12M,
  57                .codec_mode     = S5P_MFC_CODEC_NONE,
  58                .type           = MFC_FMT_RAW,
  59                .num_planes     = 2,
  60                .versions       = MFC_V5PLUS_BITS,
  61        },
  62        {
  63                .name           = "4:2:0 2 Planes Y/CrCb",
  64                .fourcc         = V4L2_PIX_FMT_NV21M,
  65                .codec_mode     = S5P_MFC_CODEC_NONE,
  66                .type           = MFC_FMT_RAW,
  67                .num_planes     = 2,
  68                .versions       = MFC_V6PLUS_BITS,
  69        },
  70        {
  71                .name           = "H264 Encoded Stream",
  72                .fourcc         = V4L2_PIX_FMT_H264,
  73                .codec_mode     = S5P_MFC_CODEC_H264_ENC,
  74                .type           = MFC_FMT_ENC,
  75                .num_planes     = 1,
  76                .versions       = MFC_V5PLUS_BITS,
  77        },
  78        {
  79                .name           = "MPEG4 Encoded Stream",
  80                .fourcc         = V4L2_PIX_FMT_MPEG4,
  81                .codec_mode     = S5P_MFC_CODEC_MPEG4_ENC,
  82                .type           = MFC_FMT_ENC,
  83                .num_planes     = 1,
  84                .versions       = MFC_V5PLUS_BITS,
  85        },
  86        {
  87                .name           = "H263 Encoded Stream",
  88                .fourcc         = V4L2_PIX_FMT_H263,
  89                .codec_mode     = S5P_MFC_CODEC_H263_ENC,
  90                .type           = MFC_FMT_ENC,
  91                .num_planes     = 1,
  92                .versions       = MFC_V5PLUS_BITS,
  93        },
  94        {
  95                .name           = "VP8 Encoded Stream",
  96                .fourcc         = V4L2_PIX_FMT_VP8,
  97                .codec_mode     = S5P_MFC_CODEC_VP8_ENC,
  98                .type           = MFC_FMT_ENC,
  99                .num_planes     = 1,
 100                .versions       = MFC_V7PLUS_BITS,
 101        },
 102        {
 103                .fourcc         = V4L2_PIX_FMT_HEVC,
 104                .codec_mode     = S5P_FIMV_CODEC_HEVC_ENC,
 105                .type           = MFC_FMT_ENC,
 106                .num_planes     = 1,
 107                .versions       = MFC_V10_BIT,
 108        },
 109};
 110
 111#define NUM_FORMATS ARRAY_SIZE(formats)
 112static struct s5p_mfc_fmt *find_format(struct v4l2_format *f, unsigned int t)
 113{
 114        unsigned int i;
 115
 116        for (i = 0; i < NUM_FORMATS; i++) {
 117                if (formats[i].fourcc == f->fmt.pix_mp.pixelformat &&
 118                    formats[i].type == t)
 119                        return &formats[i];
 120        }
 121        return NULL;
 122}
 123
 124static struct mfc_control controls[] = {
 125        {
 126                .id = V4L2_CID_MPEG_VIDEO_GOP_SIZE,
 127                .type = V4L2_CTRL_TYPE_INTEGER,
 128                .minimum = 0,
 129                .maximum = (1 << 16) - 1,
 130                .step = 1,
 131                .default_value = 12,
 132        },
 133        {
 134                .id = V4L2_CID_MPEG_VIDEO_MULTI_SLICE_MODE,
 135                .type = V4L2_CTRL_TYPE_MENU,
 136                .minimum = V4L2_MPEG_VIDEO_MULTI_SLICE_MODE_SINGLE,
 137                .maximum = V4L2_MPEG_VIDEO_MULTI_SICE_MODE_MAX_BYTES,
 138                .default_value = V4L2_MPEG_VIDEO_MULTI_SLICE_MODE_SINGLE,
 139                .menu_skip_mask = 0,
 140        },
 141        {
 142                .id = V4L2_CID_MPEG_VIDEO_MULTI_SLICE_MAX_MB,
 143                .type = V4L2_CTRL_TYPE_INTEGER,
 144                .minimum = 1,
 145                .maximum = (1 << 16) - 1,
 146                .step = 1,
 147                .default_value = 1,
 148        },
 149        {
 150                .id = V4L2_CID_MPEG_VIDEO_MULTI_SLICE_MAX_BYTES,
 151                .type = V4L2_CTRL_TYPE_INTEGER,
 152                .minimum = 1900,
 153                .maximum = (1 << 30) - 1,
 154                .step = 1,
 155                .default_value = 1900,
 156        },
 157        {
 158                .id = V4L2_CID_MPEG_VIDEO_CYCLIC_INTRA_REFRESH_MB,
 159                .type = V4L2_CTRL_TYPE_INTEGER,
 160                .minimum = 0,
 161                .maximum = (1 << 16) - 1,
 162                .step = 1,
 163                .default_value = 0,
 164        },
 165        {
 166                .id = V4L2_CID_MPEG_MFC51_VIDEO_PADDING,
 167                .type = V4L2_CTRL_TYPE_BOOLEAN,
 168                .name = "Padding Control Enable",
 169                .minimum = 0,
 170                .maximum = 1,
 171                .step = 1,
 172                .default_value = 0,
 173        },
 174        {
 175                .id = V4L2_CID_MPEG_MFC51_VIDEO_PADDING_YUV,
 176                .type = V4L2_CTRL_TYPE_INTEGER,
 177                .name = "Padding Color YUV Value",
 178                .minimum = 0,
 179                .maximum = (1 << 25) - 1,
 180                .step = 1,
 181                .default_value = 0,
 182        },
 183        {
 184                .id = V4L2_CID_MPEG_VIDEO_FRAME_RC_ENABLE,
 185                .type = V4L2_CTRL_TYPE_BOOLEAN,
 186                .minimum = 0,
 187                .maximum = 1,
 188                .step = 1,
 189                .default_value = 0,
 190        },
 191        {
 192                .id = V4L2_CID_MPEG_VIDEO_BITRATE,
 193                .type = V4L2_CTRL_TYPE_INTEGER,
 194                .minimum = 1,
 195                .maximum = (1 << 30) - 1,
 196                .step = 1,
 197                .default_value = 1,
 198        },
 199        {
 200                .id = V4L2_CID_MPEG_MFC51_VIDEO_RC_REACTION_COEFF,
 201                .type = V4L2_CTRL_TYPE_INTEGER,
 202                .name = "Rate Control Reaction Coeff.",
 203                .minimum = 1,
 204                .maximum = (1 << 16) - 1,
 205                .step = 1,
 206                .default_value = 1,
 207        },
 208        {
 209                .id = V4L2_CID_MPEG_MFC51_VIDEO_FORCE_FRAME_TYPE,
 210                .type = V4L2_CTRL_TYPE_MENU,
 211                .name = "Force frame type",
 212                .minimum = V4L2_MPEG_MFC51_VIDEO_FORCE_FRAME_TYPE_DISABLED,
 213                .maximum = V4L2_MPEG_MFC51_VIDEO_FORCE_FRAME_TYPE_NOT_CODED,
 214                .default_value = V4L2_MPEG_MFC51_VIDEO_FORCE_FRAME_TYPE_DISABLED,
 215                .menu_skip_mask = 0,
 216        },
 217        {
 218                .id = V4L2_CID_MPEG_VIDEO_FORCE_KEY_FRAME,
 219                .type = V4L2_CTRL_TYPE_BUTTON,
 220                .minimum = 0,
 221                .maximum = 0,
 222                .step = 0,
 223                .default_value = 0,
 224        },
 225        {
 226                .id = V4L2_CID_MPEG_VIDEO_VBV_SIZE,
 227                .type = V4L2_CTRL_TYPE_INTEGER,
 228                .minimum = 0,
 229                .maximum = (1 << 16) - 1,
 230                .step = 1,
 231                .default_value = 0,
 232        },
 233        {
 234                .id = V4L2_CID_MPEG_VIDEO_MV_H_SEARCH_RANGE,
 235                .type = V4L2_CTRL_TYPE_INTEGER,
 236                .name = "Horizontal MV Search Range",
 237                .minimum = 16,
 238                .maximum = 128,
 239                .step = 16,
 240                .default_value = 32,
 241        },
 242        {
 243                .id = V4L2_CID_MPEG_VIDEO_MV_V_SEARCH_RANGE,
 244                .type = V4L2_CTRL_TYPE_INTEGER,
 245                .name = "Vertical MV Search Range",
 246                .minimum = 16,
 247                .maximum = 128,
 248                .step = 16,
 249                .default_value = 32,
 250        },
 251        {
 252                .id = V4L2_CID_MPEG_VIDEO_H264_CPB_SIZE,
 253                .type = V4L2_CTRL_TYPE_INTEGER,
 254                .minimum = 0,
 255                .maximum = (1 << 16) - 1,
 256                .step = 1,
 257                .default_value = 0,
 258        },
 259        {
 260                .id = V4L2_CID_MPEG_VIDEO_HEADER_MODE,
 261                .type = V4L2_CTRL_TYPE_MENU,
 262                .minimum = V4L2_MPEG_VIDEO_HEADER_MODE_SEPARATE,
 263                .maximum = V4L2_MPEG_VIDEO_HEADER_MODE_JOINED_WITH_1ST_FRAME,
 264                .default_value = V4L2_MPEG_VIDEO_HEADER_MODE_SEPARATE,
 265                .menu_skip_mask = 0,
 266        },
 267        {
 268                .id = V4L2_CID_MPEG_MFC51_VIDEO_FRAME_SKIP_MODE,
 269                .type = V4L2_CTRL_TYPE_MENU,
 270                .name = "Frame Skip Enable",
 271                .minimum = V4L2_MPEG_MFC51_VIDEO_FRAME_SKIP_MODE_DISABLED,
 272                .maximum = V4L2_MPEG_MFC51_VIDEO_FRAME_SKIP_MODE_BUF_LIMIT,
 273                .menu_skip_mask = 0,
 274                .default_value = V4L2_MPEG_MFC51_VIDEO_FRAME_SKIP_MODE_DISABLED,
 275        },
 276        {
 277                .id = V4L2_CID_MPEG_MFC51_VIDEO_RC_FIXED_TARGET_BIT,
 278                .type = V4L2_CTRL_TYPE_BOOLEAN,
 279                .name = "Fixed Target Bit Enable",
 280                .minimum = 0,
 281                .maximum = 1,
 282                .default_value = 0,
 283                .step = 1,
 284                .menu_skip_mask = 0,
 285        },
 286        {
 287                .id = V4L2_CID_MPEG_VIDEO_B_FRAMES,
 288                .type = V4L2_CTRL_TYPE_INTEGER,
 289                .minimum = 0,
 290                .maximum = 2,
 291                .step = 1,
 292                .default_value = 0,
 293        },
 294        {
 295                .id = V4L2_CID_MPEG_VIDEO_H264_PROFILE,
 296                .type = V4L2_CTRL_TYPE_MENU,
 297                .minimum = V4L2_MPEG_VIDEO_H264_PROFILE_BASELINE,
 298                .maximum = V4L2_MPEG_VIDEO_H264_PROFILE_MULTIVIEW_HIGH,
 299                .default_value = V4L2_MPEG_VIDEO_H264_PROFILE_BASELINE,
 300                .menu_skip_mask = ~(
 301                                (1 << V4L2_MPEG_VIDEO_H264_PROFILE_BASELINE) |
 302                                (1 << V4L2_MPEG_VIDEO_H264_PROFILE_MAIN) |
 303                                (1 << V4L2_MPEG_VIDEO_H264_PROFILE_HIGH)
 304                                ),
 305        },
 306        {
 307                .id = V4L2_CID_MPEG_VIDEO_H264_LEVEL,
 308                .type = V4L2_CTRL_TYPE_MENU,
 309                .minimum = V4L2_MPEG_VIDEO_H264_LEVEL_1_0,
 310                .maximum = V4L2_MPEG_VIDEO_H264_LEVEL_4_0,
 311                .default_value = V4L2_MPEG_VIDEO_H264_LEVEL_1_0,
 312        },
 313        {
 314                .id = V4L2_CID_MPEG_VIDEO_MPEG4_LEVEL,
 315                .type = V4L2_CTRL_TYPE_MENU,
 316                .minimum = V4L2_MPEG_VIDEO_MPEG4_LEVEL_0,
 317                .maximum = V4L2_MPEG_VIDEO_MPEG4_LEVEL_5,
 318                .default_value = V4L2_MPEG_VIDEO_MPEG4_LEVEL_0,
 319                .menu_skip_mask = 0,
 320        },
 321        {
 322                .id = V4L2_CID_MPEG_VIDEO_H264_LOOP_FILTER_MODE,
 323                .type = V4L2_CTRL_TYPE_MENU,
 324                .minimum = V4L2_MPEG_VIDEO_H264_LOOP_FILTER_MODE_ENABLED,
 325                .maximum = V4L2_MPEG_VIDEO_H264_LOOP_FILTER_MODE_DISABLED_AT_SLICE_BOUNDARY,
 326                .default_value = V4L2_MPEG_VIDEO_H264_LOOP_FILTER_MODE_ENABLED,
 327                .menu_skip_mask = 0,
 328        },
 329        {
 330                .id = V4L2_CID_MPEG_VIDEO_H264_LOOP_FILTER_ALPHA,
 331                .type = V4L2_CTRL_TYPE_INTEGER,
 332                .minimum = -6,
 333                .maximum = 6,
 334                .step = 1,
 335                .default_value = 0,
 336        },
 337        {
 338                .id = V4L2_CID_MPEG_VIDEO_H264_LOOP_FILTER_BETA,
 339                .type = V4L2_CTRL_TYPE_INTEGER,
 340                .minimum = -6,
 341                .maximum = 6,
 342                .step = 1,
 343                .default_value = 0,
 344        },
 345        {
 346                .id = V4L2_CID_MPEG_VIDEO_H264_ENTROPY_MODE,
 347                .type = V4L2_CTRL_TYPE_MENU,
 348                .minimum = V4L2_MPEG_VIDEO_H264_ENTROPY_MODE_CAVLC,
 349                .maximum = V4L2_MPEG_VIDEO_H264_ENTROPY_MODE_CABAC,
 350                .default_value = V4L2_MPEG_VIDEO_H264_ENTROPY_MODE_CAVLC,
 351                .menu_skip_mask = 0,
 352        },
 353        {
 354                .id = V4L2_CID_MPEG_MFC51_VIDEO_H264_NUM_REF_PIC_FOR_P,
 355                .type = V4L2_CTRL_TYPE_INTEGER,
 356                .name = "The Number of Ref. Pic for P",
 357                .minimum = 1,
 358                .maximum = 2,
 359                .step = 1,
 360                .default_value = 1,
 361        },
 362        {
 363                .id = V4L2_CID_MPEG_VIDEO_H264_8X8_TRANSFORM,
 364                .type = V4L2_CTRL_TYPE_BOOLEAN,
 365                .minimum = 0,
 366                .maximum = 1,
 367                .step = 1,
 368                .default_value = 0,
 369        },
 370        {
 371                .id = V4L2_CID_MPEG_VIDEO_MB_RC_ENABLE,
 372                .type = V4L2_CTRL_TYPE_BOOLEAN,
 373                .minimum = 0,
 374                .maximum = 1,
 375                .step = 1,
 376                .default_value = 0,
 377        },
 378        {
 379                .id = V4L2_CID_MPEG_VIDEO_H264_I_FRAME_QP,
 380                .type = V4L2_CTRL_TYPE_INTEGER,
 381                .minimum = 0,
 382                .maximum = 51,
 383                .step = 1,
 384                .default_value = 1,
 385        },
 386        {
 387                .id = V4L2_CID_MPEG_VIDEO_H264_MIN_QP,
 388                .type = V4L2_CTRL_TYPE_INTEGER,
 389                .minimum = 0,
 390                .maximum = 51,
 391                .step = 1,
 392                .default_value = 1,
 393        },
 394        {
 395                .id = V4L2_CID_MPEG_VIDEO_H264_MAX_QP,
 396                .type = V4L2_CTRL_TYPE_INTEGER,
 397                .minimum = 0,
 398                .maximum = 51,
 399                .step = 1,
 400                .default_value = 51,
 401        },
 402        {
 403                .id = V4L2_CID_MPEG_VIDEO_H264_P_FRAME_QP,
 404                .type = V4L2_CTRL_TYPE_INTEGER,
 405                .minimum = 0,
 406                .maximum = 51,
 407                .step = 1,
 408                .default_value = 1,
 409        },
 410        {
 411                .id = V4L2_CID_MPEG_VIDEO_H264_B_FRAME_QP,
 412                .type = V4L2_CTRL_TYPE_INTEGER,
 413                .minimum = 0,
 414                .maximum = 51,
 415                .step = 1,
 416                .default_value = 1,
 417        },
 418        {
 419                .id = V4L2_CID_MPEG_VIDEO_H263_I_FRAME_QP,
 420                .type = V4L2_CTRL_TYPE_INTEGER,
 421                .name = "H263 I-Frame QP value",
 422                .minimum = 1,
 423                .maximum = 31,
 424                .step = 1,
 425                .default_value = 1,
 426        },
 427        {
 428                .id = V4L2_CID_MPEG_VIDEO_H263_MIN_QP,
 429                .type = V4L2_CTRL_TYPE_INTEGER,
 430                .name = "H263 Minimum QP value",
 431                .minimum = 1,
 432                .maximum = 31,
 433                .step = 1,
 434                .default_value = 1,
 435        },
 436        {
 437                .id = V4L2_CID_MPEG_VIDEO_H263_MAX_QP,
 438                .type = V4L2_CTRL_TYPE_INTEGER,
 439                .name = "H263 Maximum QP value",
 440                .minimum = 1,
 441                .maximum = 31,
 442                .step = 1,
 443                .default_value = 31,
 444        },
 445        {
 446                .id = V4L2_CID_MPEG_VIDEO_H263_P_FRAME_QP,
 447                .type = V4L2_CTRL_TYPE_INTEGER,
 448                .name = "H263 P frame QP value",
 449                .minimum = 1,
 450                .maximum = 31,
 451                .step = 1,
 452                .default_value = 1,
 453        },
 454        {
 455                .id = V4L2_CID_MPEG_VIDEO_H263_B_FRAME_QP,
 456                .type = V4L2_CTRL_TYPE_INTEGER,
 457                .name = "H263 B frame QP value",
 458                .minimum = 1,
 459                .maximum = 31,
 460                .step = 1,
 461                .default_value = 1,
 462        },
 463        {
 464                .id = V4L2_CID_MPEG_VIDEO_MPEG4_I_FRAME_QP,
 465                .type = V4L2_CTRL_TYPE_INTEGER,
 466                .name = "MPEG4 I-Frame QP value",
 467                .minimum = 1,
 468                .maximum = 31,
 469                .step = 1,
 470                .default_value = 1,
 471        },
 472        {
 473                .id = V4L2_CID_MPEG_VIDEO_MPEG4_MIN_QP,
 474                .type = V4L2_CTRL_TYPE_INTEGER,
 475                .name = "MPEG4 Minimum QP value",
 476                .minimum = 1,
 477                .maximum = 31,
 478                .step = 1,
 479                .default_value = 1,
 480        },
 481        {
 482                .id = V4L2_CID_MPEG_VIDEO_MPEG4_MAX_QP,
 483                .type = V4L2_CTRL_TYPE_INTEGER,
 484                .name = "MPEG4 Maximum QP value",
 485                .minimum = 0,
 486                .maximum = 51,
 487                .step = 1,
 488                .default_value = 51,
 489        },
 490        {
 491                .id = V4L2_CID_MPEG_VIDEO_MPEG4_P_FRAME_QP,
 492                .type = V4L2_CTRL_TYPE_INTEGER,
 493                .name = "MPEG4 P frame QP value",
 494                .minimum = 1,
 495                .maximum = 31,
 496                .step = 1,
 497                .default_value = 1,
 498        },
 499        {
 500                .id = V4L2_CID_MPEG_VIDEO_MPEG4_B_FRAME_QP,
 501                .type = V4L2_CTRL_TYPE_INTEGER,
 502                .name = "MPEG4 B frame QP value",
 503                .minimum = 1,
 504                .maximum = 31,
 505                .step = 1,
 506                .default_value = 1,
 507        },
 508        {
 509                .id = V4L2_CID_MPEG_MFC51_VIDEO_H264_ADAPTIVE_RC_DARK,
 510                .type = V4L2_CTRL_TYPE_BOOLEAN,
 511                .name = "H264 Dark Reg Adaptive RC",
 512                .minimum = 0,
 513                .maximum = 1,
 514                .step = 1,
 515                .default_value = 0,
 516        },
 517        {
 518                .id = V4L2_CID_MPEG_MFC51_VIDEO_H264_ADAPTIVE_RC_SMOOTH,
 519                .type = V4L2_CTRL_TYPE_BOOLEAN,
 520                .name = "H264 Smooth Reg Adaptive RC",
 521                .minimum = 0,
 522                .maximum = 1,
 523                .step = 1,
 524                .default_value = 0,
 525        },
 526        {
 527                .id = V4L2_CID_MPEG_MFC51_VIDEO_H264_ADAPTIVE_RC_STATIC,
 528                .type = V4L2_CTRL_TYPE_BOOLEAN,
 529                .name = "H264 Static Reg Adaptive RC",
 530                .minimum = 0,
 531                .maximum = 1,
 532                .step = 1,
 533                .default_value = 0,
 534        },
 535        {
 536                .id = V4L2_CID_MPEG_MFC51_VIDEO_H264_ADAPTIVE_RC_ACTIVITY,
 537                .type = V4L2_CTRL_TYPE_BOOLEAN,
 538                .name = "H264 Activity Reg Adaptive RC",
 539                .minimum = 0,
 540                .maximum = 1,
 541                .step = 1,
 542                .default_value = 0,
 543        },
 544        {
 545                .id = V4L2_CID_MPEG_VIDEO_H264_VUI_SAR_ENABLE,
 546                .type = V4L2_CTRL_TYPE_BOOLEAN,
 547                .minimum = 0,
 548                .maximum = 1,
 549                .step = 1,
 550                .default_value = 0,
 551        },
 552        {
 553                .id = V4L2_CID_MPEG_VIDEO_H264_VUI_SAR_IDC,
 554                .type = V4L2_CTRL_TYPE_MENU,
 555                .minimum = V4L2_MPEG_VIDEO_H264_VUI_SAR_IDC_UNSPECIFIED,
 556                .maximum = V4L2_MPEG_VIDEO_H264_VUI_SAR_IDC_EXTENDED,
 557                .default_value = V4L2_MPEG_VIDEO_H264_VUI_SAR_IDC_UNSPECIFIED,
 558                .menu_skip_mask = 0,
 559        },
 560        {
 561                .id = V4L2_CID_MPEG_VIDEO_H264_VUI_EXT_SAR_WIDTH,
 562                .type = V4L2_CTRL_TYPE_INTEGER,
 563                .minimum = 0,
 564                .maximum = (1 << 16) - 1,
 565                .step = 1,
 566                .default_value = 0,
 567        },
 568        {
 569                .id = V4L2_CID_MPEG_VIDEO_H264_VUI_EXT_SAR_HEIGHT,
 570                .type = V4L2_CTRL_TYPE_INTEGER,
 571                .minimum = 0,
 572                .maximum = (1 << 16) - 1,
 573                .step = 1,
 574                .default_value = 0,
 575        },
 576        {
 577                .id = V4L2_CID_MPEG_VIDEO_GOP_CLOSURE,
 578                .type = V4L2_CTRL_TYPE_BOOLEAN,
 579                .minimum = 0,
 580                .maximum = 1,
 581                .step = 1,
 582                .default_value = 1,
 583        },
 584        {
 585                .id = V4L2_CID_MPEG_VIDEO_H264_I_PERIOD,
 586                .type = V4L2_CTRL_TYPE_INTEGER,
 587                .minimum = 0,
 588                .maximum = (1 << 16) - 1,
 589                .step = 1,
 590                .default_value = 0,
 591        },
 592        {
 593                .id = V4L2_CID_MPEG_VIDEO_MPEG4_PROFILE,
 594                .type = V4L2_CTRL_TYPE_MENU,
 595                .minimum = V4L2_MPEG_VIDEO_MPEG4_PROFILE_SIMPLE,
 596                .maximum = V4L2_MPEG_VIDEO_MPEG4_PROFILE_ADVANCED_SIMPLE,
 597                .default_value = V4L2_MPEG_VIDEO_MPEG4_PROFILE_SIMPLE,
 598                .menu_skip_mask = 0,
 599        },
 600        {
 601                .id = V4L2_CID_MPEG_VIDEO_MPEG4_QPEL,
 602                .type = V4L2_CTRL_TYPE_BOOLEAN,
 603                .minimum = 0,
 604                .maximum = 1,
 605                .step = 1,
 606                .default_value = 0,
 607        },
 608        {
 609                .id = V4L2_CID_MPEG_VIDEO_VPX_NUM_PARTITIONS,
 610                .type = V4L2_CTRL_TYPE_INTEGER_MENU,
 611                .maximum = V4L2_CID_MPEG_VIDEO_VPX_8_PARTITIONS,
 612                .default_value = V4L2_CID_MPEG_VIDEO_VPX_1_PARTITION,
 613                .menu_skip_mask = 0,
 614        },
 615        {
 616                .id = V4L2_CID_MPEG_VIDEO_VPX_IMD_DISABLE_4X4,
 617                .type = V4L2_CTRL_TYPE_BOOLEAN,
 618                .minimum = 0,
 619                .maximum = 1,
 620                .step = 1,
 621                .default_value = 0,
 622        },
 623        {
 624                .id = V4L2_CID_MPEG_VIDEO_VPX_NUM_REF_FRAMES,
 625                .type = V4L2_CTRL_TYPE_INTEGER_MENU,
 626                .maximum = V4L2_CID_MPEG_VIDEO_VPX_2_REF_FRAME,
 627                .default_value = V4L2_CID_MPEG_VIDEO_VPX_1_REF_FRAME,
 628                .menu_skip_mask = 0,
 629        },
 630        {
 631                .id = V4L2_CID_MPEG_VIDEO_VPX_FILTER_LEVEL,
 632                .type = V4L2_CTRL_TYPE_INTEGER,
 633                .minimum = 0,
 634                .maximum = 63,
 635                .step = 1,
 636                .default_value = 0,
 637        },
 638        {
 639                .id = V4L2_CID_MPEG_VIDEO_VPX_FILTER_SHARPNESS,
 640                .type = V4L2_CTRL_TYPE_INTEGER,
 641                .minimum = 0,
 642                .maximum = 7,
 643                .step = 1,
 644                .default_value = 0,
 645        },
 646        {
 647                .id = V4L2_CID_MPEG_VIDEO_VPX_GOLDEN_FRAME_REF_PERIOD,
 648                .type = V4L2_CTRL_TYPE_INTEGER,
 649                .minimum = 0,
 650                .maximum = (1 << 16) - 1,
 651                .step = 1,
 652                .default_value = 0,
 653        },
 654        {
 655                .id = V4L2_CID_MPEG_VIDEO_VPX_GOLDEN_FRAME_SEL,
 656                .type = V4L2_CTRL_TYPE_MENU,
 657                .minimum = V4L2_CID_MPEG_VIDEO_VPX_GOLDEN_FRAME_USE_PREV,
 658                .maximum = V4L2_CID_MPEG_VIDEO_VPX_GOLDEN_FRAME_USE_REF_PERIOD,
 659                .default_value = V4L2_CID_MPEG_VIDEO_VPX_GOLDEN_FRAME_USE_PREV,
 660                .menu_skip_mask = 0,
 661        },
 662        {
 663                .id = V4L2_CID_MPEG_VIDEO_VPX_MAX_QP,
 664                .type = V4L2_CTRL_TYPE_INTEGER,
 665                .minimum = 0,
 666                .maximum = 127,
 667                .step = 1,
 668                .default_value = 127,
 669        },
 670        {
 671                .id = V4L2_CID_MPEG_VIDEO_VPX_MIN_QP,
 672                .type = V4L2_CTRL_TYPE_INTEGER,
 673                .minimum = 0,
 674                .maximum = 11,
 675                .step = 1,
 676                .default_value = 0,
 677        },
 678        {
 679                .id = V4L2_CID_MPEG_VIDEO_VPX_I_FRAME_QP,
 680                .type = V4L2_CTRL_TYPE_INTEGER,
 681                .minimum = 0,
 682                .maximum = 127,
 683                .step = 1,
 684                .default_value = 10,
 685        },
 686        {
 687                .id = V4L2_CID_MPEG_VIDEO_VPX_P_FRAME_QP,
 688                .type = V4L2_CTRL_TYPE_INTEGER,
 689                .minimum = 0,
 690                .maximum = 127,
 691                .step = 1,
 692                .default_value = 10,
 693        },
 694        {
 695                .id = V4L2_CID_MPEG_VIDEO_VP8_PROFILE,
 696                .type = V4L2_CTRL_TYPE_MENU,
 697                .minimum = V4L2_MPEG_VIDEO_VP8_PROFILE_0,
 698                .maximum = V4L2_MPEG_VIDEO_VP8_PROFILE_3,
 699                .default_value = V4L2_MPEG_VIDEO_VP8_PROFILE_0,
 700                .menu_skip_mask = 0,
 701        },
 702        {
 703                .id = V4L2_CID_MPEG_VIDEO_HEVC_I_FRAME_QP,
 704                .type = V4L2_CTRL_TYPE_INTEGER,
 705                .name = "HEVC I Frame QP Value",
 706                .minimum = 0,
 707                .maximum = 51,
 708                .step = 1,
 709                .default_value = 0,
 710        },
 711        {
 712                .id = V4L2_CID_MPEG_VIDEO_HEVC_P_FRAME_QP,
 713                .type = V4L2_CTRL_TYPE_INTEGER,
 714                .name = "HEVC P Frame QP Value",
 715                .minimum = 0,
 716                .maximum = 51,
 717                .step = 1,
 718                .default_value = 0,
 719        },
 720        {
 721                .id = V4L2_CID_MPEG_VIDEO_HEVC_B_FRAME_QP,
 722                .type = V4L2_CTRL_TYPE_INTEGER,
 723                .minimum = 0,
 724                .maximum = 51,
 725                .step = 1,
 726                .default_value = 0,
 727        },
 728        {
 729                .id = V4L2_CID_MPEG_VIDEO_HEVC_MIN_QP,
 730                .type = V4L2_CTRL_TYPE_INTEGER,
 731                .minimum = 0,
 732                .maximum = 51,
 733                .step = 1,
 734                .default_value = 0,
 735        },
 736        {
 737                .id = V4L2_CID_MPEG_VIDEO_HEVC_MAX_QP,
 738                .type = V4L2_CTRL_TYPE_INTEGER,
 739                .minimum = 0,
 740                .maximum = 51,
 741                .step = 1,
 742                .default_value = 0,
 743        },
 744        {
 745                .id = V4L2_CID_MPEG_VIDEO_HEVC_PROFILE,
 746                .type = V4L2_CTRL_TYPE_MENU,
 747                .minimum = V4L2_MPEG_VIDEO_HEVC_PROFILE_MAIN,
 748                .maximum = V4L2_MPEG_VIDEO_HEVC_PROFILE_MAIN_STILL_PICTURE,
 749                .step = 1,
 750                .default_value = V4L2_MPEG_VIDEO_HEVC_PROFILE_MAIN,
 751        },
 752        {
 753                .id = V4L2_CID_MPEG_VIDEO_HEVC_LEVEL,
 754                .type = V4L2_CTRL_TYPE_MENU,
 755                .minimum = V4L2_MPEG_VIDEO_HEVC_LEVEL_1,
 756                .maximum = V4L2_MPEG_VIDEO_HEVC_LEVEL_6_2,
 757                .step = 1,
 758                .default_value = V4L2_MPEG_VIDEO_HEVC_LEVEL_1,
 759        },
 760        {
 761                .id = V4L2_CID_MPEG_VIDEO_HEVC_TIER,
 762                .type = V4L2_CTRL_TYPE_MENU,
 763                .minimum = V4L2_MPEG_VIDEO_HEVC_TIER_MAIN,
 764                .maximum = V4L2_MPEG_VIDEO_HEVC_TIER_HIGH,
 765                .step = 1,
 766                .default_value = V4L2_MPEG_VIDEO_HEVC_TIER_MAIN,
 767        },
 768        {
 769                .id = V4L2_CID_MPEG_VIDEO_HEVC_FRAME_RATE_RESOLUTION,
 770                .type = V4L2_CTRL_TYPE_INTEGER,
 771                .minimum = 1,
 772                .maximum = (1 << 16) - 1,
 773                .step = 1,
 774                .default_value = 1,
 775        },
 776        {
 777                .id = V4L2_CID_MPEG_VIDEO_HEVC_MAX_PARTITION_DEPTH,
 778                .type = V4L2_CTRL_TYPE_INTEGER,
 779                .minimum = 0,
 780                .maximum = 1,
 781                .step = 1,
 782                .default_value = 0,
 783        },
 784        {
 785                .id = V4L2_CID_MPEG_VIDEO_REF_NUMBER_FOR_PFRAMES,
 786                .type = V4L2_CTRL_TYPE_INTEGER,
 787                .minimum = 1,
 788                .maximum = 2,
 789                .step = 1,
 790                .default_value = 1,
 791        },
 792        {
 793                .id = V4L2_CID_MPEG_VIDEO_HEVC_REFRESH_TYPE,
 794                .type = V4L2_CTRL_TYPE_MENU,
 795                .minimum = V4L2_MPEG_VIDEO_HEVC_REFRESH_NONE,
 796                .maximum = V4L2_MPEG_VIDEO_HEVC_REFRESH_IDR,
 797                .step = 1,
 798                .default_value = V4L2_MPEG_VIDEO_HEVC_REFRESH_NONE,
 799        },
 800        {
 801                .id = V4L2_CID_MPEG_VIDEO_HEVC_CONST_INTRA_PRED,
 802                .type = V4L2_CTRL_TYPE_BOOLEAN,
 803                .minimum = 0,
 804                .maximum = 1,
 805                .step = 1,
 806                .default_value = 0,
 807        },
 808        {
 809                .id = V4L2_CID_MPEG_VIDEO_HEVC_LOSSLESS_CU,
 810                .type = V4L2_CTRL_TYPE_BOOLEAN,
 811                .minimum = 0,
 812                .maximum = 1,
 813                .step = 1,
 814                .default_value = 0,
 815        },
 816        {
 817                .id = V4L2_CID_MPEG_VIDEO_HEVC_WAVEFRONT,
 818                .type = V4L2_CTRL_TYPE_BOOLEAN,
 819                .minimum = 0,
 820                .maximum = 1,
 821                .step = 1,
 822                .default_value = 0,
 823        },
 824        {
 825                .id = V4L2_CID_MPEG_VIDEO_HEVC_LOOP_FILTER_MODE,
 826                .type = V4L2_CTRL_TYPE_MENU,
 827                .minimum = V4L2_MPEG_VIDEO_HEVC_LOOP_FILTER_MODE_DISABLED,
 828                .maximum = V4L2_MPEG_VIDEO_HEVC_LOOP_FILTER_MODE_DISABLED_AT_SLICE_BOUNDARY,
 829                .step = 1,
 830                .default_value = V4L2_MPEG_VIDEO_HEVC_LOOP_FILTER_MODE_DISABLED,
 831        },
 832        {
 833                .id = V4L2_CID_MPEG_VIDEO_HEVC_HIER_QP,
 834                .type = V4L2_CTRL_TYPE_BOOLEAN,
 835                .minimum = 0,
 836                .maximum = 1,
 837                .step = 1,
 838                .default_value = 0,
 839        },
 840        {
 841                .id = V4L2_CID_MPEG_VIDEO_HEVC_HIER_CODING_TYPE,
 842                .type = V4L2_CTRL_TYPE_MENU,
 843                .minimum = V4L2_MPEG_VIDEO_HEVC_HIERARCHICAL_CODING_B,
 844                .maximum = V4L2_MPEG_VIDEO_HEVC_HIERARCHICAL_CODING_P,
 845                .step = 1,
 846                .default_value = V4L2_MPEG_VIDEO_HEVC_HIERARCHICAL_CODING_B,
 847        },
 848        {
 849                .id = V4L2_CID_MPEG_VIDEO_HEVC_HIER_CODING_LAYER,
 850                .type = V4L2_CTRL_TYPE_INTEGER,
 851                .minimum = 0,
 852                .maximum = 6,
 853                .step = 1,
 854                .default_value = 0,
 855        },
 856        {
 857                .id = V4L2_CID_MPEG_VIDEO_HEVC_HIER_CODING_L0_QP,
 858                .type = V4L2_CTRL_TYPE_INTEGER,
 859                .minimum = 0,
 860                .maximum = 51,
 861                .step = 1,
 862                .default_value = 0,
 863        },
 864        {
 865                .id = V4L2_CID_MPEG_VIDEO_HEVC_HIER_CODING_L1_QP,
 866                .type = V4L2_CTRL_TYPE_INTEGER,
 867                .minimum = 0,
 868                .maximum = 51,
 869                .step = 1,
 870                .default_value = 0,
 871        },
 872        {
 873                .id = V4L2_CID_MPEG_VIDEO_HEVC_HIER_CODING_L2_QP,
 874                .type = V4L2_CTRL_TYPE_INTEGER,
 875                .minimum = 0,
 876                .maximum = 51,
 877                .step = 1,
 878                .default_value = 0,
 879        },
 880        {
 881                .id = V4L2_CID_MPEG_VIDEO_HEVC_HIER_CODING_L3_QP,
 882                .type = V4L2_CTRL_TYPE_INTEGER,
 883                .minimum = 0,
 884                .maximum = 51,
 885                .step = 1,
 886                .default_value = 0,
 887        },
 888        {
 889                .id = V4L2_CID_MPEG_VIDEO_HEVC_HIER_CODING_L4_QP,
 890                .type = V4L2_CTRL_TYPE_INTEGER,
 891                .minimum = 0,
 892                .maximum = 51,
 893                .step = 1,
 894                .default_value = 0,
 895        },
 896        {
 897                .id = V4L2_CID_MPEG_VIDEO_HEVC_HIER_CODING_L5_QP,
 898                .type = V4L2_CTRL_TYPE_INTEGER,
 899                .minimum = 0,
 900                .maximum = 51,
 901                .step = 1,
 902                .default_value = 0,
 903        },
 904        {
 905                .id = V4L2_CID_MPEG_VIDEO_HEVC_HIER_CODING_L6_QP,
 906                .type = V4L2_CTRL_TYPE_INTEGER,
 907                .minimum = 0,
 908                .maximum = 51,
 909                .step = 1,
 910                .default_value = 0,
 911        },
 912        {
 913                .id = V4L2_CID_MPEG_VIDEO_HEVC_HIER_CODING_L0_BR,
 914                .type = V4L2_CTRL_TYPE_INTEGER,
 915                .minimum = INT_MIN,
 916                .maximum = INT_MAX,
 917                .step = 1,
 918                .default_value = 0,
 919        },
 920        {
 921                .id = V4L2_CID_MPEG_VIDEO_HEVC_HIER_CODING_L1_BR,
 922                .type = V4L2_CTRL_TYPE_INTEGER,
 923                .minimum = INT_MIN,
 924                .maximum = INT_MAX,
 925                .step = 1,
 926                .default_value = 0,
 927        },
 928        {
 929                .id = V4L2_CID_MPEG_VIDEO_HEVC_HIER_CODING_L2_BR,
 930                .type = V4L2_CTRL_TYPE_INTEGER,
 931                .minimum = INT_MIN,
 932                .maximum = INT_MAX,
 933                .step = 1,
 934                .default_value = 0,
 935        },
 936        {
 937                .id = V4L2_CID_MPEG_VIDEO_HEVC_HIER_CODING_L3_BR,
 938                .type = V4L2_CTRL_TYPE_INTEGER,
 939                .minimum = INT_MIN,
 940                .maximum = INT_MAX,
 941                .step = 1,
 942                .default_value = 0,
 943        },
 944        {
 945                .id = V4L2_CID_MPEG_VIDEO_HEVC_HIER_CODING_L4_BR,
 946                .type = V4L2_CTRL_TYPE_INTEGER,
 947                .minimum = INT_MIN,
 948                .maximum = INT_MAX,
 949                .step = 1,
 950                .default_value = 0,
 951        },
 952        {
 953                .id = V4L2_CID_MPEG_VIDEO_HEVC_HIER_CODING_L5_BR,
 954                .type = V4L2_CTRL_TYPE_INTEGER,
 955                .minimum = INT_MIN,
 956                .maximum = INT_MAX,
 957                .step = 1,
 958                .default_value = 0,
 959        },
 960        {
 961                .id = V4L2_CID_MPEG_VIDEO_HEVC_HIER_CODING_L6_BR,
 962                .type = V4L2_CTRL_TYPE_INTEGER,
 963                .minimum = INT_MIN,
 964                .maximum = INT_MAX,
 965                .step = 1,
 966                .default_value = 0,
 967        },
 968        {
 969                .id = V4L2_CID_MPEG_VIDEO_HEVC_GENERAL_PB,
 970                .type = V4L2_CTRL_TYPE_BOOLEAN,
 971                .minimum = 0,
 972                .maximum = 1,
 973                .step = 1,
 974                .default_value = 0,
 975        },
 976        {
 977                .id = V4L2_CID_MPEG_VIDEO_HEVC_TEMPORAL_ID,
 978                .type = V4L2_CTRL_TYPE_BOOLEAN,
 979                .minimum = 0,
 980                .maximum = 1,
 981                .step = 1,
 982                .default_value = 0,
 983        },
 984        {
 985                .id = V4L2_CID_MPEG_VIDEO_HEVC_STRONG_SMOOTHING,
 986                .type = V4L2_CTRL_TYPE_BOOLEAN,
 987                .minimum = 0,
 988                .maximum = 1,
 989                .step = 1,
 990                .default_value = 0,
 991        },
 992        {
 993                .id = V4L2_CID_MPEG_VIDEO_HEVC_INTRA_PU_SPLIT,
 994                .type = V4L2_CTRL_TYPE_BOOLEAN,
 995                .minimum = 0,
 996                .maximum = 1,
 997                .step = 1,
 998                .default_value = 0,
 999        },
1000        {
1001                .id = V4L2_CID_MPEG_VIDEO_HEVC_TMV_PREDICTION,
1002                .type = V4L2_CTRL_TYPE_BOOLEAN,
1003                .minimum = 0,
1004                .maximum = 1,
1005                .step = 1,
1006                .default_value = 0,
1007        },
1008        {
1009                .id = V4L2_CID_MPEG_VIDEO_HEVC_MAX_NUM_MERGE_MV_MINUS1,
1010                .type = V4L2_CTRL_TYPE_INTEGER,
1011                .minimum = 0,
1012                .maximum = 4,
1013                .step = 1,
1014                .default_value = 0,
1015        },
1016        {
1017                .id = V4L2_CID_MPEG_VIDEO_HEVC_WITHOUT_STARTCODE,
1018                .type = V4L2_CTRL_TYPE_BOOLEAN,
1019                .minimum = 0,
1020                .maximum = 1,
1021                .step = 1,
1022                .default_value = 0,
1023        },
1024        {
1025                .id = V4L2_CID_MPEG_VIDEO_HEVC_REFRESH_PERIOD,
1026                .type = V4L2_CTRL_TYPE_INTEGER,
1027                .minimum = 0,
1028                .maximum = (1 << 16) - 1,
1029                .step = 1,
1030                .default_value = 0,
1031        },
1032        {
1033                .id = V4L2_CID_MPEG_VIDEO_HEVC_LF_BETA_OFFSET_DIV2,
1034                .type = V4L2_CTRL_TYPE_INTEGER,
1035                .minimum = -6,
1036                .maximum = 6,
1037                .step = 1,
1038                .default_value = 0,
1039        },
1040        {
1041                .id = V4L2_CID_MPEG_VIDEO_HEVC_LF_TC_OFFSET_DIV2,
1042                .type = V4L2_CTRL_TYPE_INTEGER,
1043                .minimum = -6,
1044                .maximum = 6,
1045                .step = 1,
1046                .default_value = 0,
1047        },
1048        {
1049                .id = V4L2_CID_MPEG_VIDEO_HEVC_SIZE_OF_LENGTH_FIELD,
1050                .type = V4L2_CTRL_TYPE_MENU,
1051                .minimum = V4L2_MPEG_VIDEO_HEVC_SIZE_0,
1052                .maximum = V4L2_MPEG_VIDEO_HEVC_SIZE_4,
1053                .step = 1,
1054                .default_value = V4L2_MPEG_VIDEO_HEVC_SIZE_0,
1055        },
1056        {
1057                .id = V4L2_CID_MPEG_VIDEO_PREPEND_SPSPPS_TO_IDR,
1058                .type = V4L2_CTRL_TYPE_INTEGER,
1059                .minimum = 0,
1060                .maximum = 1,
1061                .step = 1,
1062                .default_value = 0,
1063        },
1064        {
1065                .id = V4L2_CID_MIN_BUFFERS_FOR_OUTPUT,
1066                .type = V4L2_CTRL_TYPE_INTEGER,
1067                .name = "Minimum number of output bufs",
1068                .minimum = 1,
1069                .maximum = 32,
1070                .step = 1,
1071                .default_value = 1,
1072                .is_volatile = 1,
1073        },
1074};
1075
1076#define NUM_CTRLS ARRAY_SIZE(controls)
1077static const char * const *mfc51_get_menu(u32 id)
1078{
1079        static const char * const mfc51_video_frame_skip[] = {
1080                "Disabled",
1081                "Level Limit",
1082                "VBV/CPB Limit",
1083                NULL,
1084        };
1085        static const char * const mfc51_video_force_frame[] = {
1086                "Disabled",
1087                "I Frame",
1088                "Not Coded",
1089                NULL,
1090        };
1091        switch (id) {
1092        case V4L2_CID_MPEG_MFC51_VIDEO_FRAME_SKIP_MODE:
1093                return mfc51_video_frame_skip;
1094        case V4L2_CID_MPEG_MFC51_VIDEO_FORCE_FRAME_TYPE:
1095                return mfc51_video_force_frame;
1096        }
1097        return NULL;
1098}
1099
1100static int s5p_mfc_ctx_ready(struct s5p_mfc_ctx *ctx)
1101{
1102        mfc_debug(2, "src=%d, dst=%d, state=%d\n",
1103                  ctx->src_queue_cnt, ctx->dst_queue_cnt, ctx->state);
1104        /* context is ready to make header */
1105        if (ctx->state == MFCINST_GOT_INST && ctx->dst_queue_cnt >= 1)
1106                return 1;
1107        /* context is ready to encode a frame */
1108        if ((ctx->state == MFCINST_RUNNING ||
1109                ctx->state == MFCINST_HEAD_PRODUCED) &&
1110                ctx->src_queue_cnt >= 1 && ctx->dst_queue_cnt >= 1)
1111                return 1;
1112        /* context is ready to encode remaining frames */
1113        if (ctx->state == MFCINST_FINISHING &&
1114                ctx->dst_queue_cnt >= 1)
1115                return 1;
1116        mfc_debug(2, "ctx is not ready\n");
1117        return 0;
1118}
1119
1120static void cleanup_ref_queue(struct s5p_mfc_ctx *ctx)
1121{
1122        struct s5p_mfc_buf *mb_entry;
1123
1124        /* move buffers in ref queue to src queue */
1125        while (!list_empty(&ctx->ref_queue)) {
1126                mb_entry = list_entry((&ctx->ref_queue)->next,
1127                                                struct s5p_mfc_buf, list);
1128                list_del(&mb_entry->list);
1129                ctx->ref_queue_cnt--;
1130                list_add_tail(&mb_entry->list, &ctx->src_queue);
1131                ctx->src_queue_cnt++;
1132        }
1133        mfc_debug(2, "enc src count: %d, enc ref count: %d\n",
1134                  ctx->src_queue_cnt, ctx->ref_queue_cnt);
1135        INIT_LIST_HEAD(&ctx->ref_queue);
1136        ctx->ref_queue_cnt = 0;
1137}
1138
1139static int enc_pre_seq_start(struct s5p_mfc_ctx *ctx)
1140{
1141        struct s5p_mfc_dev *dev = ctx->dev;
1142        struct s5p_mfc_buf *dst_mb;
1143        unsigned long dst_addr;
1144        unsigned int dst_size;
1145
1146        dst_mb = list_entry(ctx->dst_queue.next, struct s5p_mfc_buf, list);
1147        dst_addr = vb2_dma_contig_plane_dma_addr(&dst_mb->b->vb2_buf, 0);
1148        dst_size = vb2_plane_size(&dst_mb->b->vb2_buf, 0);
1149        s5p_mfc_hw_call(dev->mfc_ops, set_enc_stream_buffer, ctx, dst_addr,
1150                        dst_size);
1151        return 0;
1152}
1153
1154static int enc_post_seq_start(struct s5p_mfc_ctx *ctx)
1155{
1156        struct s5p_mfc_dev *dev = ctx->dev;
1157        struct s5p_mfc_enc_params *p = &ctx->enc_params;
1158        struct s5p_mfc_buf *dst_mb;
1159        unsigned int enc_pb_count;
1160
1161        if (p->seq_hdr_mode == V4L2_MPEG_VIDEO_HEADER_MODE_SEPARATE) {
1162                if (!list_empty(&ctx->dst_queue)) {
1163                        dst_mb = list_entry(ctx->dst_queue.next,
1164                                        struct s5p_mfc_buf, list);
1165                        list_del(&dst_mb->list);
1166                        ctx->dst_queue_cnt--;
1167                        vb2_set_plane_payload(&dst_mb->b->vb2_buf, 0,
1168                                s5p_mfc_hw_call(dev->mfc_ops, get_enc_strm_size,
1169                                                dev));
1170                        vb2_buffer_done(&dst_mb->b->vb2_buf,
1171                                        VB2_BUF_STATE_DONE);
1172                }
1173        }
1174
1175        if (!IS_MFCV6_PLUS(dev)) {
1176                ctx->state = MFCINST_RUNNING;
1177                if (s5p_mfc_ctx_ready(ctx))
1178                        set_work_bit_irqsave(ctx);
1179                s5p_mfc_hw_call(dev->mfc_ops, try_run, dev);
1180        } else {
1181                enc_pb_count = s5p_mfc_hw_call(dev->mfc_ops,
1182                                get_enc_dpb_count, dev);
1183                if (ctx->pb_count < enc_pb_count)
1184                        ctx->pb_count = enc_pb_count;
1185                if (FW_HAS_E_MIN_SCRATCH_BUF(dev)) {
1186                        ctx->scratch_buf_size = s5p_mfc_hw_call(dev->mfc_ops,
1187                                        get_e_min_scratch_buf_size, dev);
1188                        ctx->bank1.size += ctx->scratch_buf_size;
1189                }
1190                ctx->state = MFCINST_HEAD_PRODUCED;
1191        }
1192
1193        return 0;
1194}
1195
1196static int enc_pre_frame_start(struct s5p_mfc_ctx *ctx)
1197{
1198        struct s5p_mfc_dev *dev = ctx->dev;
1199        struct s5p_mfc_buf *dst_mb;
1200        struct s5p_mfc_buf *src_mb;
1201        unsigned long src_y_addr, src_c_addr, dst_addr;
1202        unsigned int dst_size;
1203
1204        src_mb = list_entry(ctx->src_queue.next, struct s5p_mfc_buf, list);
1205        src_y_addr = vb2_dma_contig_plane_dma_addr(&src_mb->b->vb2_buf, 0);
1206        src_c_addr = vb2_dma_contig_plane_dma_addr(&src_mb->b->vb2_buf, 1);
1207        s5p_mfc_hw_call(dev->mfc_ops, set_enc_frame_buffer, ctx,
1208                                                        src_y_addr, src_c_addr);
1209
1210        dst_mb = list_entry(ctx->dst_queue.next, struct s5p_mfc_buf, list);
1211        dst_addr = vb2_dma_contig_plane_dma_addr(&dst_mb->b->vb2_buf, 0);
1212        dst_size = vb2_plane_size(&dst_mb->b->vb2_buf, 0);
1213        s5p_mfc_hw_call(dev->mfc_ops, set_enc_stream_buffer, ctx, dst_addr,
1214                        dst_size);
1215
1216        return 0;
1217}
1218
1219static int enc_post_frame_start(struct s5p_mfc_ctx *ctx)
1220{
1221        struct s5p_mfc_dev *dev = ctx->dev;
1222        struct s5p_mfc_buf *mb_entry;
1223        unsigned long enc_y_addr = 0, enc_c_addr = 0;
1224        unsigned long mb_y_addr, mb_c_addr;
1225        int slice_type;
1226        unsigned int strm_size;
1227
1228        slice_type = s5p_mfc_hw_call(dev->mfc_ops, get_enc_slice_type, dev);
1229        strm_size = s5p_mfc_hw_call(dev->mfc_ops, get_enc_strm_size, dev);
1230        mfc_debug(2, "Encoded slice type: %d\n", slice_type);
1231        mfc_debug(2, "Encoded stream size: %d\n", strm_size);
1232        mfc_debug(2, "Display order: %d\n",
1233                  mfc_read(dev, S5P_FIMV_ENC_SI_PIC_CNT));
1234        if (slice_type >= 0) {
1235                s5p_mfc_hw_call(dev->mfc_ops, get_enc_frame_buffer, ctx,
1236                                &enc_y_addr, &enc_c_addr);
1237                list_for_each_entry(mb_entry, &ctx->src_queue, list) {
1238                        mb_y_addr = vb2_dma_contig_plane_dma_addr(
1239                                        &mb_entry->b->vb2_buf, 0);
1240                        mb_c_addr = vb2_dma_contig_plane_dma_addr(
1241                                        &mb_entry->b->vb2_buf, 1);
1242                        if ((enc_y_addr == mb_y_addr) &&
1243                                                (enc_c_addr == mb_c_addr)) {
1244                                list_del(&mb_entry->list);
1245                                ctx->src_queue_cnt--;
1246                                vb2_buffer_done(&mb_entry->b->vb2_buf,
1247                                                        VB2_BUF_STATE_DONE);
1248                                break;
1249                        }
1250                }
1251                list_for_each_entry(mb_entry, &ctx->ref_queue, list) {
1252                        mb_y_addr = vb2_dma_contig_plane_dma_addr(
1253                                        &mb_entry->b->vb2_buf, 0);
1254                        mb_c_addr = vb2_dma_contig_plane_dma_addr(
1255                                        &mb_entry->b->vb2_buf, 1);
1256                        if ((enc_y_addr == mb_y_addr) &&
1257                                                (enc_c_addr == mb_c_addr)) {
1258                                list_del(&mb_entry->list);
1259                                ctx->ref_queue_cnt--;
1260                                vb2_buffer_done(&mb_entry->b->vb2_buf,
1261                                                        VB2_BUF_STATE_DONE);
1262                                break;
1263                        }
1264                }
1265        }
1266        if ((ctx->src_queue_cnt > 0) && (ctx->state == MFCINST_RUNNING)) {
1267                mb_entry = list_entry(ctx->src_queue.next, struct s5p_mfc_buf,
1268                                                                        list);
1269                if (mb_entry->flags & MFC_BUF_FLAG_USED) {
1270                        list_del(&mb_entry->list);
1271                        ctx->src_queue_cnt--;
1272                        list_add_tail(&mb_entry->list, &ctx->ref_queue);
1273                        ctx->ref_queue_cnt++;
1274                }
1275        }
1276        mfc_debug(2, "enc src count: %d, enc ref count: %d\n",
1277                  ctx->src_queue_cnt, ctx->ref_queue_cnt);
1278        if ((ctx->dst_queue_cnt > 0) && (strm_size > 0)) {
1279                mb_entry = list_entry(ctx->dst_queue.next, struct s5p_mfc_buf,
1280                                                                        list);
1281                list_del(&mb_entry->list);
1282                ctx->dst_queue_cnt--;
1283                switch (slice_type) {
1284                case S5P_FIMV_ENC_SI_SLICE_TYPE_I:
1285                        mb_entry->b->flags |= V4L2_BUF_FLAG_KEYFRAME;
1286                        break;
1287                case S5P_FIMV_ENC_SI_SLICE_TYPE_P:
1288                        mb_entry->b->flags |= V4L2_BUF_FLAG_PFRAME;
1289                        break;
1290                case S5P_FIMV_ENC_SI_SLICE_TYPE_B:
1291                        mb_entry->b->flags |= V4L2_BUF_FLAG_BFRAME;
1292                        break;
1293                }
1294                vb2_set_plane_payload(&mb_entry->b->vb2_buf, 0, strm_size);
1295                vb2_buffer_done(&mb_entry->b->vb2_buf, VB2_BUF_STATE_DONE);
1296        }
1297        if ((ctx->src_queue_cnt == 0) || (ctx->dst_queue_cnt == 0))
1298                clear_work_bit(ctx);
1299
1300        return 0;
1301}
1302
1303static const struct s5p_mfc_codec_ops encoder_codec_ops = {
1304        .pre_seq_start          = enc_pre_seq_start,
1305        .post_seq_start         = enc_post_seq_start,
1306        .pre_frame_start        = enc_pre_frame_start,
1307        .post_frame_start       = enc_post_frame_start,
1308};
1309
1310/* Query capabilities of the device */
1311static int vidioc_querycap(struct file *file, void *priv,
1312                           struct v4l2_capability *cap)
1313{
1314        struct s5p_mfc_dev *dev = video_drvdata(file);
1315
1316        strlcpy(cap->driver, S5P_MFC_NAME, sizeof(cap->driver));
1317        strlcpy(cap->card, dev->vfd_enc->name, sizeof(cap->card));
1318        snprintf(cap->bus_info, sizeof(cap->bus_info), "platform:%s",
1319                 dev_name(&dev->plat_dev->dev));
1320        /*
1321         * This is only a mem-to-mem video device. The capture and output
1322         * device capability flags are left only for backward compatibility
1323         * and are scheduled for removal.
1324         */
1325        cap->device_caps = V4L2_CAP_VIDEO_M2M_MPLANE | V4L2_CAP_STREAMING;
1326        cap->capabilities = cap->device_caps | V4L2_CAP_DEVICE_CAPS;
1327        return 0;
1328}
1329
1330static int vidioc_enum_fmt(struct file *file, struct v4l2_fmtdesc *f,
1331                                                        bool out)
1332{
1333        struct s5p_mfc_dev *dev = video_drvdata(file);
1334        struct s5p_mfc_fmt *fmt;
1335        int i, j = 0;
1336
1337        for (i = 0; i < ARRAY_SIZE(formats); ++i) {
1338                if (out && formats[i].type != MFC_FMT_RAW)
1339                        continue;
1340                else if (!out && formats[i].type != MFC_FMT_ENC)
1341                        continue;
1342                else if ((dev->variant->version_bit & formats[i].versions) == 0)
1343                        continue;
1344
1345                if (j == f->index) {
1346                        fmt = &formats[i];
1347                        strlcpy(f->description, fmt->name,
1348                                sizeof(f->description));
1349                        f->pixelformat = fmt->fourcc;
1350                        return 0;
1351                }
1352                ++j;
1353        }
1354        return -EINVAL;
1355}
1356
1357static int vidioc_enum_fmt_vid_cap_mplane(struct file *file, void *pirv,
1358                                          struct v4l2_fmtdesc *f)
1359{
1360        return vidioc_enum_fmt(file, f, false);
1361}
1362
1363static int vidioc_enum_fmt_vid_out_mplane(struct file *file, void *prov,
1364                                          struct v4l2_fmtdesc *f)
1365{
1366        return vidioc_enum_fmt(file, f, true);
1367}
1368
1369static int vidioc_g_fmt(struct file *file, void *priv, struct v4l2_format *f)
1370{
1371        struct s5p_mfc_ctx *ctx = fh_to_ctx(priv);
1372        struct v4l2_pix_format_mplane *pix_fmt_mp = &f->fmt.pix_mp;
1373
1374        mfc_debug(2, "f->type = %d ctx->state = %d\n", f->type, ctx->state);
1375        if (f->type == V4L2_BUF_TYPE_VIDEO_CAPTURE_MPLANE) {
1376                /* This is run on output (encoder dest) */
1377                pix_fmt_mp->width = 0;
1378                pix_fmt_mp->height = 0;
1379                pix_fmt_mp->field = V4L2_FIELD_NONE;
1380                pix_fmt_mp->pixelformat = ctx->dst_fmt->fourcc;
1381                pix_fmt_mp->num_planes = ctx->dst_fmt->num_planes;
1382
1383                pix_fmt_mp->plane_fmt[0].bytesperline = ctx->enc_dst_buf_size;
1384                pix_fmt_mp->plane_fmt[0].sizeimage = ctx->enc_dst_buf_size;
1385        } else if (f->type == V4L2_BUF_TYPE_VIDEO_OUTPUT_MPLANE) {
1386                /* This is run on capture (encoder src) */
1387                pix_fmt_mp->width = ctx->img_width;
1388                pix_fmt_mp->height = ctx->img_height;
1389
1390                pix_fmt_mp->field = V4L2_FIELD_NONE;
1391                pix_fmt_mp->pixelformat = ctx->src_fmt->fourcc;
1392                pix_fmt_mp->num_planes = ctx->src_fmt->num_planes;
1393
1394                pix_fmt_mp->plane_fmt[0].bytesperline = ctx->buf_width;
1395                pix_fmt_mp->plane_fmt[0].sizeimage = ctx->luma_size;
1396                pix_fmt_mp->plane_fmt[1].bytesperline = ctx->buf_width;
1397                pix_fmt_mp->plane_fmt[1].sizeimage = ctx->chroma_size;
1398        } else {
1399                mfc_err("invalid buf type\n");
1400                return -EINVAL;
1401        }
1402        return 0;
1403}
1404
1405static int vidioc_try_fmt(struct file *file, void *priv, struct v4l2_format *f)
1406{
1407        struct s5p_mfc_dev *dev = video_drvdata(file);
1408        struct s5p_mfc_fmt *fmt;
1409        struct v4l2_pix_format_mplane *pix_fmt_mp = &f->fmt.pix_mp;
1410
1411        if (f->type == V4L2_BUF_TYPE_VIDEO_CAPTURE_MPLANE) {
1412                fmt = find_format(f, MFC_FMT_ENC);
1413                if (!fmt) {
1414                        mfc_err("failed to try output format\n");
1415                        return -EINVAL;
1416                }
1417                if ((dev->variant->version_bit & fmt->versions) == 0) {
1418                        mfc_err("Unsupported format by this MFC version.\n");
1419                        return -EINVAL;
1420                }
1421
1422                pix_fmt_mp->plane_fmt[0].bytesperline =
1423                        pix_fmt_mp->plane_fmt[0].sizeimage;
1424        } else if (f->type == V4L2_BUF_TYPE_VIDEO_OUTPUT_MPLANE) {
1425                fmt = find_format(f, MFC_FMT_RAW);
1426                if (!fmt) {
1427                        mfc_err("failed to try output format\n");
1428                        return -EINVAL;
1429                }
1430                if ((dev->variant->version_bit & fmt->versions) == 0) {
1431                        mfc_err("Unsupported format by this MFC version.\n");
1432                        return -EINVAL;
1433                }
1434
1435                v4l_bound_align_image(&pix_fmt_mp->width, 8, 1920, 1,
1436                        &pix_fmt_mp->height, 4, 1080, 1, 0);
1437        } else {
1438                mfc_err("invalid buf type\n");
1439                return -EINVAL;
1440        }
1441        return 0;
1442}
1443
1444static int vidioc_s_fmt(struct file *file, void *priv, struct v4l2_format *f)
1445{
1446        struct s5p_mfc_dev *dev = video_drvdata(file);
1447        struct s5p_mfc_ctx *ctx = fh_to_ctx(priv);
1448        struct v4l2_pix_format_mplane *pix_fmt_mp = &f->fmt.pix_mp;
1449        int ret = 0;
1450
1451        ret = vidioc_try_fmt(file, priv, f);
1452        if (ret)
1453                return ret;
1454        if (ctx->vq_src.streaming || ctx->vq_dst.streaming) {
1455                v4l2_err(&dev->v4l2_dev, "%s queue busy\n", __func__);
1456                ret = -EBUSY;
1457                goto out;
1458        }
1459        if (f->type == V4L2_BUF_TYPE_VIDEO_CAPTURE_MPLANE) {
1460                /* dst_fmt is validated by call to vidioc_try_fmt */
1461                ctx->dst_fmt = find_format(f, MFC_FMT_ENC);
1462                ctx->state = MFCINST_INIT;
1463                ctx->codec_mode = ctx->dst_fmt->codec_mode;
1464                ctx->enc_dst_buf_size = pix_fmt_mp->plane_fmt[0].sizeimage;
1465                pix_fmt_mp->plane_fmt[0].bytesperline = 0;
1466                ctx->dst_bufs_cnt = 0;
1467                ctx->capture_state = QUEUE_FREE;
1468                ret = s5p_mfc_open_mfc_inst(dev, ctx);
1469        } else if (f->type == V4L2_BUF_TYPE_VIDEO_OUTPUT_MPLANE) {
1470                /* src_fmt is validated by call to vidioc_try_fmt */
1471                ctx->src_fmt = find_format(f, MFC_FMT_RAW);
1472                ctx->img_width = pix_fmt_mp->width;
1473                ctx->img_height = pix_fmt_mp->height;
1474                mfc_debug(2, "codec number: %d\n", ctx->src_fmt->codec_mode);
1475                mfc_debug(2, "fmt - w: %d, h: %d, ctx - w: %d, h: %d\n",
1476                        pix_fmt_mp->width, pix_fmt_mp->height,
1477                        ctx->img_width, ctx->img_height);
1478
1479                s5p_mfc_hw_call(dev->mfc_ops, enc_calc_src_size, ctx);
1480                pix_fmt_mp->plane_fmt[0].sizeimage = ctx->luma_size;
1481                pix_fmt_mp->plane_fmt[0].bytesperline = ctx->buf_width;
1482                pix_fmt_mp->plane_fmt[1].sizeimage = ctx->chroma_size;
1483                pix_fmt_mp->plane_fmt[1].bytesperline = ctx->buf_width;
1484
1485                ctx->src_bufs_cnt = 0;
1486                ctx->output_state = QUEUE_FREE;
1487        } else {
1488                mfc_err("invalid buf type\n");
1489                ret = -EINVAL;
1490        }
1491out:
1492        mfc_debug_leave();
1493        return ret;
1494}
1495
1496static int vidioc_reqbufs(struct file *file, void *priv,
1497                                          struct v4l2_requestbuffers *reqbufs)
1498{
1499        struct s5p_mfc_dev *dev = video_drvdata(file);
1500        struct s5p_mfc_ctx *ctx = fh_to_ctx(priv);
1501        int ret = 0;
1502
1503        /* if memory is not mmp or userptr return error */
1504        if ((reqbufs->memory != V4L2_MEMORY_MMAP) &&
1505                (reqbufs->memory != V4L2_MEMORY_USERPTR))
1506                return -EINVAL;
1507        if (reqbufs->type == V4L2_BUF_TYPE_VIDEO_CAPTURE_MPLANE) {
1508                if (reqbufs->count == 0) {
1509                        mfc_debug(2, "Freeing buffers\n");
1510                        ret = vb2_reqbufs(&ctx->vq_dst, reqbufs);
1511                        s5p_mfc_hw_call(dev->mfc_ops, release_codec_buffers,
1512                                        ctx);
1513                        ctx->capture_state = QUEUE_FREE;
1514                        return ret;
1515                }
1516                if (ctx->capture_state != QUEUE_FREE) {
1517                        mfc_err("invalid capture state: %d\n",
1518                                                        ctx->capture_state);
1519                        return -EINVAL;
1520                }
1521                ret = vb2_reqbufs(&ctx->vq_dst, reqbufs);
1522                if (ret != 0) {
1523                        mfc_err("error in vb2_reqbufs() for E(D)\n");
1524                        return ret;
1525                }
1526                ctx->capture_state = QUEUE_BUFS_REQUESTED;
1527
1528                ret = s5p_mfc_hw_call(ctx->dev->mfc_ops,
1529                                alloc_codec_buffers, ctx);
1530                if (ret) {
1531                        mfc_err("Failed to allocate encoding buffers\n");
1532                        reqbufs->count = 0;
1533                        ret = vb2_reqbufs(&ctx->vq_dst, reqbufs);
1534                        return -ENOMEM;
1535                }
1536        } else if (reqbufs->type == V4L2_BUF_TYPE_VIDEO_OUTPUT_MPLANE) {
1537                if (reqbufs->count == 0) {
1538                        mfc_debug(2, "Freeing buffers\n");
1539                        ret = vb2_reqbufs(&ctx->vq_src, reqbufs);
1540                        s5p_mfc_hw_call(dev->mfc_ops, release_codec_buffers,
1541                                        ctx);
1542                        ctx->output_state = QUEUE_FREE;
1543                        return ret;
1544                }
1545                if (ctx->output_state != QUEUE_FREE) {
1546                        mfc_err("invalid output state: %d\n",
1547                                                        ctx->output_state);
1548                        return -EINVAL;
1549                }
1550
1551                if (IS_MFCV6_PLUS(dev)) {
1552                        /* Check for min encoder buffers */
1553                        if (ctx->pb_count &&
1554                                (reqbufs->count < ctx->pb_count)) {
1555                                reqbufs->count = ctx->pb_count;
1556                                mfc_debug(2, "Minimum %d output buffers needed\n",
1557                                                ctx->pb_count);
1558                        } else {
1559                                ctx->pb_count = reqbufs->count;
1560                        }
1561                }
1562
1563                ret = vb2_reqbufs(&ctx->vq_src, reqbufs);
1564                if (ret != 0) {
1565                        mfc_err("error in vb2_reqbufs() for E(S)\n");
1566                        return ret;
1567                }
1568                ctx->output_state = QUEUE_BUFS_REQUESTED;
1569        } else {
1570                mfc_err("invalid buf type\n");
1571                return -EINVAL;
1572        }
1573        return ret;
1574}
1575
1576static int vidioc_querybuf(struct file *file, void *priv,
1577                                                   struct v4l2_buffer *buf)
1578{
1579        struct s5p_mfc_ctx *ctx = fh_to_ctx(priv);
1580        int ret = 0;
1581
1582        /* if memory is not mmp or userptr return error */
1583        if ((buf->memory != V4L2_MEMORY_MMAP) &&
1584                (buf->memory != V4L2_MEMORY_USERPTR))
1585                return -EINVAL;
1586        if (buf->type == V4L2_BUF_TYPE_VIDEO_CAPTURE_MPLANE) {
1587                if (ctx->state != MFCINST_GOT_INST) {
1588                        mfc_err("invalid context state: %d\n", ctx->state);
1589                        return -EINVAL;
1590                }
1591                ret = vb2_querybuf(&ctx->vq_dst, buf);
1592                if (ret != 0) {
1593                        mfc_err("error in vb2_querybuf() for E(D)\n");
1594                        return ret;
1595                }
1596                buf->m.planes[0].m.mem_offset += DST_QUEUE_OFF_BASE;
1597        } else if (buf->type == V4L2_BUF_TYPE_VIDEO_OUTPUT_MPLANE) {
1598                ret = vb2_querybuf(&ctx->vq_src, buf);
1599                if (ret != 0) {
1600                        mfc_err("error in vb2_querybuf() for E(S)\n");
1601                        return ret;
1602                }
1603        } else {
1604                mfc_err("invalid buf type\n");
1605                return -EINVAL;
1606        }
1607        return ret;
1608}
1609
1610/* Queue a buffer */
1611static int vidioc_qbuf(struct file *file, void *priv, struct v4l2_buffer *buf)
1612{
1613        struct s5p_mfc_ctx *ctx = fh_to_ctx(priv);
1614
1615        if (ctx->state == MFCINST_ERROR) {
1616                mfc_err("Call on QBUF after unrecoverable error\n");
1617                return -EIO;
1618        }
1619        if (buf->type == V4L2_BUF_TYPE_VIDEO_OUTPUT_MPLANE) {
1620                if (ctx->state == MFCINST_FINISHING) {
1621                        mfc_err("Call on QBUF after EOS command\n");
1622                        return -EIO;
1623                }
1624                return vb2_qbuf(&ctx->vq_src, buf);
1625        } else if (buf->type == V4L2_BUF_TYPE_VIDEO_CAPTURE_MPLANE) {
1626                return vb2_qbuf(&ctx->vq_dst, buf);
1627        }
1628        return -EINVAL;
1629}
1630
1631/* Dequeue a buffer */
1632static int vidioc_dqbuf(struct file *file, void *priv, struct v4l2_buffer *buf)
1633{
1634        const struct v4l2_event ev = {
1635                .type = V4L2_EVENT_EOS
1636        };
1637        struct s5p_mfc_ctx *ctx = fh_to_ctx(priv);
1638        int ret;
1639
1640        if (ctx->state == MFCINST_ERROR) {
1641                mfc_err_limited("Call on DQBUF after unrecoverable error\n");
1642                return -EIO;
1643        }
1644        if (buf->type == V4L2_BUF_TYPE_VIDEO_OUTPUT_MPLANE) {
1645                ret = vb2_dqbuf(&ctx->vq_src, buf, file->f_flags & O_NONBLOCK);
1646        } else if (buf->type == V4L2_BUF_TYPE_VIDEO_CAPTURE_MPLANE) {
1647                ret = vb2_dqbuf(&ctx->vq_dst, buf, file->f_flags & O_NONBLOCK);
1648                if (ret == 0 && ctx->state == MFCINST_FINISHED
1649                                        && list_empty(&ctx->vq_dst.done_list))
1650                        v4l2_event_queue_fh(&ctx->fh, &ev);
1651        } else {
1652                ret = -EINVAL;
1653        }
1654
1655        return ret;
1656}
1657
1658/* Export DMA buffer */
1659static int vidioc_expbuf(struct file *file, void *priv,
1660        struct v4l2_exportbuffer *eb)
1661{
1662        struct s5p_mfc_ctx *ctx = fh_to_ctx(priv);
1663
1664        if (eb->type == V4L2_BUF_TYPE_VIDEO_OUTPUT_MPLANE)
1665                return vb2_expbuf(&ctx->vq_src, eb);
1666        if (eb->type == V4L2_BUF_TYPE_VIDEO_CAPTURE_MPLANE)
1667                return vb2_expbuf(&ctx->vq_dst, eb);
1668        return -EINVAL;
1669}
1670
1671/* Stream on */
1672static int vidioc_streamon(struct file *file, void *priv,
1673                           enum v4l2_buf_type type)
1674{
1675        struct s5p_mfc_ctx *ctx = fh_to_ctx(priv);
1676
1677        if (type == V4L2_BUF_TYPE_VIDEO_OUTPUT_MPLANE)
1678                return vb2_streamon(&ctx->vq_src, type);
1679        else if (type == V4L2_BUF_TYPE_VIDEO_CAPTURE_MPLANE)
1680                return vb2_streamon(&ctx->vq_dst, type);
1681        return -EINVAL;
1682}
1683
1684/* Stream off, which equals to a pause */
1685static int vidioc_streamoff(struct file *file, void *priv,
1686                            enum v4l2_buf_type type)
1687{
1688        struct s5p_mfc_ctx *ctx = fh_to_ctx(priv);
1689
1690        if (type == V4L2_BUF_TYPE_VIDEO_OUTPUT_MPLANE)
1691                return vb2_streamoff(&ctx->vq_src, type);
1692        else if (type == V4L2_BUF_TYPE_VIDEO_CAPTURE_MPLANE)
1693                return vb2_streamoff(&ctx->vq_dst, type);
1694        return -EINVAL;
1695}
1696
1697static inline int h264_level(enum v4l2_mpeg_video_h264_level lvl)
1698{
1699        static unsigned int t[V4L2_MPEG_VIDEO_H264_LEVEL_4_0 + 1] = {
1700                /* V4L2_MPEG_VIDEO_H264_LEVEL_1_0   */ 10,
1701                /* V4L2_MPEG_VIDEO_H264_LEVEL_1B    */ 9,
1702                /* V4L2_MPEG_VIDEO_H264_LEVEL_1_1   */ 11,
1703                /* V4L2_MPEG_VIDEO_H264_LEVEL_1_2   */ 12,
1704                /* V4L2_MPEG_VIDEO_H264_LEVEL_1_3   */ 13,
1705                /* V4L2_MPEG_VIDEO_H264_LEVEL_2_0   */ 20,
1706                /* V4L2_MPEG_VIDEO_H264_LEVEL_2_1   */ 21,
1707                /* V4L2_MPEG_VIDEO_H264_LEVEL_2_2   */ 22,
1708                /* V4L2_MPEG_VIDEO_H264_LEVEL_3_0   */ 30,
1709                /* V4L2_MPEG_VIDEO_H264_LEVEL_3_1   */ 31,
1710                /* V4L2_MPEG_VIDEO_H264_LEVEL_3_2   */ 32,
1711                /* V4L2_MPEG_VIDEO_H264_LEVEL_4_0   */ 40,
1712        };
1713        return t[lvl];
1714}
1715
1716static inline int mpeg4_level(enum v4l2_mpeg_video_mpeg4_level lvl)
1717{
1718        static unsigned int t[V4L2_MPEG_VIDEO_MPEG4_LEVEL_5 + 1] = {
1719                /* V4L2_MPEG_VIDEO_MPEG4_LEVEL_0    */ 0,
1720                /* V4L2_MPEG_VIDEO_MPEG4_LEVEL_0B   */ 9,
1721                /* V4L2_MPEG_VIDEO_MPEG4_LEVEL_1    */ 1,
1722                /* V4L2_MPEG_VIDEO_MPEG4_LEVEL_2    */ 2,
1723                /* V4L2_MPEG_VIDEO_MPEG4_LEVEL_3    */ 3,
1724                /* V4L2_MPEG_VIDEO_MPEG4_LEVEL_3B   */ 7,
1725                /* V4L2_MPEG_VIDEO_MPEG4_LEVEL_4    */ 4,
1726                /* V4L2_MPEG_VIDEO_MPEG4_LEVEL_5    */ 5,
1727        };
1728        return t[lvl];
1729}
1730
1731static inline int hevc_level(enum v4l2_mpeg_video_hevc_level lvl)
1732{
1733        static unsigned int t[] = {
1734                /* V4L2_MPEG_VIDEO_HEVC_LEVEL_1    */ 10,
1735                /* V4L2_MPEG_VIDEO_HEVC_LEVEL_2    */ 20,
1736                /* V4L2_MPEG_VIDEO_HEVC_LEVEL_2_1  */ 21,
1737                /* V4L2_MPEG_VIDEO_HEVC_LEVEL_3    */ 30,
1738                /* V4L2_MPEG_VIDEO_HEVC_LEVEL_3_1  */ 31,
1739                /* V4L2_MPEG_VIDEO_HEVC_LEVEL_4    */ 40,
1740                /* V4L2_MPEG_VIDEO_HEVC_LEVEL_4_1  */ 41,
1741                /* V4L2_MPEG_VIDEO_HEVC_LEVEL_5    */ 50,
1742                /* V4L2_MPEG_VIDEO_HEVC_LEVEL_5_1  */ 51,
1743                /* V4L2_MPEG_VIDEO_HEVC_LEVEL_5_2  */ 52,
1744                /* V4L2_MPEG_VIDEO_HEVC_LEVEL_6    */ 60,
1745                /* V4L2_MPEG_VIDEO_HEVC_LEVEL_6_1  */ 61,
1746                /* V4L2_MPEG_VIDEO_HEVC_LEVEL_6_2  */ 62,
1747        };
1748        return t[lvl];
1749}
1750
1751static inline int vui_sar_idc(enum v4l2_mpeg_video_h264_vui_sar_idc sar)
1752{
1753        static unsigned int t[V4L2_MPEG_VIDEO_H264_VUI_SAR_IDC_EXTENDED + 1] = {
1754                /* V4L2_MPEG_VIDEO_H264_VUI_SAR_IDC_UNSPECIFIED     */ 0,
1755                /* V4L2_MPEG_VIDEO_H264_VUI_SAR_IDC_1x1             */ 1,
1756                /* V4L2_MPEG_VIDEO_H264_VUI_SAR_IDC_12x11           */ 2,
1757                /* V4L2_MPEG_VIDEO_H264_VUI_SAR_IDC_10x11           */ 3,
1758                /* V4L2_MPEG_VIDEO_H264_VUI_SAR_IDC_16x11           */ 4,
1759                /* V4L2_MPEG_VIDEO_H264_VUI_SAR_IDC_40x33           */ 5,
1760                /* V4L2_MPEG_VIDEO_H264_VUI_SAR_IDC_24x11           */ 6,
1761                /* V4L2_MPEG_VIDEO_H264_VUI_SAR_IDC_20x11           */ 7,
1762                /* V4L2_MPEG_VIDEO_H264_VUI_SAR_IDC_32x11           */ 8,
1763                /* V4L2_MPEG_VIDEO_H264_VUI_SAR_IDC_80x33           */ 9,
1764                /* V4L2_MPEG_VIDEO_H264_VUI_SAR_IDC_18x11           */ 10,
1765                /* V4L2_MPEG_VIDEO_H264_VUI_SAR_IDC_15x11           */ 11,
1766                /* V4L2_MPEG_VIDEO_H264_VUI_SAR_IDC_64x33           */ 12,
1767                /* V4L2_MPEG_VIDEO_H264_VUI_SAR_IDC_160x99          */ 13,
1768                /* V4L2_MPEG_VIDEO_H264_VUI_SAR_IDC_4x3             */ 14,
1769                /* V4L2_MPEG_VIDEO_H264_VUI_SAR_IDC_3x2             */ 15,
1770                /* V4L2_MPEG_VIDEO_H264_VUI_SAR_IDC_2x1             */ 16,
1771                /* V4L2_MPEG_VIDEO_H264_VUI_SAR_IDC_EXTENDED        */ 255,
1772        };
1773        return t[sar];
1774}
1775
1776/*
1777 * Update range of all HEVC quantization parameter controls that depend on the
1778 * V4L2_CID_MPEG_VIDEO_HEVC_MIN_QP, V4L2_CID_MPEG_VIDEO_HEVC_MAX_QP controls.
1779 */
1780static void __enc_update_hevc_qp_ctrls_range(struct s5p_mfc_ctx *ctx,
1781                                             int min, int max)
1782{
1783        static const int __hevc_qp_ctrls[] = {
1784                V4L2_CID_MPEG_VIDEO_HEVC_I_FRAME_QP,
1785                V4L2_CID_MPEG_VIDEO_HEVC_P_FRAME_QP,
1786                V4L2_CID_MPEG_VIDEO_HEVC_B_FRAME_QP,
1787                V4L2_CID_MPEG_VIDEO_HEVC_HIER_CODING_L0_QP,
1788                V4L2_CID_MPEG_VIDEO_HEVC_HIER_CODING_L1_QP,
1789                V4L2_CID_MPEG_VIDEO_HEVC_HIER_CODING_L2_QP,
1790                V4L2_CID_MPEG_VIDEO_HEVC_HIER_CODING_L3_QP,
1791                V4L2_CID_MPEG_VIDEO_HEVC_HIER_CODING_L4_QP,
1792                V4L2_CID_MPEG_VIDEO_HEVC_HIER_CODING_L5_QP,
1793                V4L2_CID_MPEG_VIDEO_HEVC_HIER_CODING_L6_QP,
1794        };
1795        struct v4l2_ctrl *ctrl = NULL;
1796        int i, j;
1797
1798        for (i = 0; i < ARRAY_SIZE(__hevc_qp_ctrls); i++) {
1799                for (j = 0; j < ARRAY_SIZE(ctx->ctrls); j++) {
1800                        if (ctx->ctrls[j]->id == __hevc_qp_ctrls[i]) {
1801                                ctrl = ctx->ctrls[j];
1802                                break;
1803                        }
1804                }
1805                if (WARN_ON(!ctrl))
1806                        break;
1807
1808                __v4l2_ctrl_modify_range(ctrl, min, max, ctrl->step, min);
1809        }
1810}
1811
1812static int s5p_mfc_enc_s_ctrl(struct v4l2_ctrl *ctrl)
1813{
1814        struct s5p_mfc_ctx *ctx = ctrl_to_ctx(ctrl);
1815        struct s5p_mfc_dev *dev = ctx->dev;
1816        struct s5p_mfc_enc_params *p = &ctx->enc_params;
1817        int ret = 0;
1818
1819        switch (ctrl->id) {
1820        case V4L2_CID_MPEG_VIDEO_GOP_SIZE:
1821                p->gop_size = ctrl->val;
1822                break;
1823        case V4L2_CID_MPEG_VIDEO_MULTI_SLICE_MODE:
1824                p->slice_mode = ctrl->val;
1825                break;
1826        case V4L2_CID_MPEG_VIDEO_MULTI_SLICE_MAX_MB:
1827                p->slice_mb = ctrl->val;
1828                break;
1829        case V4L2_CID_MPEG_VIDEO_MULTI_SLICE_MAX_BYTES:
1830                p->slice_bit = ctrl->val * 8;
1831                break;
1832        case V4L2_CID_MPEG_VIDEO_CYCLIC_INTRA_REFRESH_MB:
1833                p->intra_refresh_mb = ctrl->val;
1834                break;
1835        case V4L2_CID_MPEG_MFC51_VIDEO_PADDING:
1836                p->pad = ctrl->val;
1837                break;
1838        case V4L2_CID_MPEG_MFC51_VIDEO_PADDING_YUV:
1839                p->pad_luma = (ctrl->val >> 16) & 0xff;
1840                p->pad_cb = (ctrl->val >> 8) & 0xff;
1841                p->pad_cr = (ctrl->val >> 0) & 0xff;
1842                break;
1843        case V4L2_CID_MPEG_VIDEO_FRAME_RC_ENABLE:
1844                p->rc_frame = ctrl->val;
1845                break;
1846        case V4L2_CID_MPEG_VIDEO_BITRATE:
1847                p->rc_bitrate = ctrl->val;
1848                break;
1849        case V4L2_CID_MPEG_MFC51_VIDEO_RC_REACTION_COEFF:
1850                p->rc_reaction_coeff = ctrl->val;
1851                break;
1852        case V4L2_CID_MPEG_MFC51_VIDEO_FORCE_FRAME_TYPE:
1853                ctx->force_frame_type = ctrl->val;
1854                break;
1855        case V4L2_CID_MPEG_VIDEO_FORCE_KEY_FRAME:
1856                ctx->force_frame_type =
1857                        V4L2_MPEG_MFC51_VIDEO_FORCE_FRAME_TYPE_I_FRAME;
1858                break;
1859        case V4L2_CID_MPEG_VIDEO_VBV_SIZE:
1860                p->vbv_size = ctrl->val;
1861                break;
1862        case V4L2_CID_MPEG_VIDEO_MV_H_SEARCH_RANGE:
1863                p->mv_h_range = ctrl->val;
1864                break;
1865        case V4L2_CID_MPEG_VIDEO_MV_V_SEARCH_RANGE:
1866                p->mv_v_range = ctrl->val;
1867                break;
1868        case V4L2_CID_MPEG_VIDEO_H264_CPB_SIZE:
1869                p->codec.h264.cpb_size = ctrl->val;
1870                break;
1871        case V4L2_CID_MPEG_VIDEO_HEADER_MODE:
1872                p->seq_hdr_mode = ctrl->val;
1873                break;
1874        case V4L2_CID_MPEG_MFC51_VIDEO_FRAME_SKIP_MODE:
1875                p->frame_skip_mode = ctrl->val;
1876                break;
1877        case V4L2_CID_MPEG_MFC51_VIDEO_RC_FIXED_TARGET_BIT:
1878                p->fixed_target_bit = ctrl->val;
1879                break;
1880        case V4L2_CID_MPEG_VIDEO_B_FRAMES:
1881                p->num_b_frame = ctrl->val;
1882                break;
1883        case V4L2_CID_MPEG_VIDEO_H264_PROFILE:
1884                switch (ctrl->val) {
1885                case V4L2_MPEG_VIDEO_H264_PROFILE_MAIN:
1886                        p->codec.h264.profile =
1887                                        S5P_FIMV_ENC_PROFILE_H264_MAIN;
1888                        break;
1889                case V4L2_MPEG_VIDEO_H264_PROFILE_HIGH:
1890                        p->codec.h264.profile =
1891                                        S5P_FIMV_ENC_PROFILE_H264_HIGH;
1892                        break;
1893                case V4L2_MPEG_VIDEO_H264_PROFILE_BASELINE:
1894                        p->codec.h264.profile =
1895                                S5P_FIMV_ENC_PROFILE_H264_BASELINE;
1896                        break;
1897                case V4L2_MPEG_VIDEO_H264_PROFILE_CONSTRAINED_BASELINE:
1898                        if (IS_MFCV6_PLUS(dev))
1899                                p->codec.h264.profile =
1900                                S5P_FIMV_ENC_PROFILE_H264_CONSTRAINED_BASELINE;
1901                        else
1902                                ret = -EINVAL;
1903                        break;
1904                default:
1905                        ret = -EINVAL;
1906                }
1907                break;
1908        case V4L2_CID_MPEG_VIDEO_H264_LEVEL:
1909                p->codec.h264.level_v4l2 = ctrl->val;
1910                p->codec.h264.level = h264_level(ctrl->val);
1911                if (p->codec.h264.level < 0) {
1912                        mfc_err("Level number is wrong\n");
1913                        ret = p->codec.h264.level;
1914                }
1915                break;
1916        case V4L2_CID_MPEG_VIDEO_MPEG4_LEVEL:
1917                p->codec.mpeg4.level_v4l2 = ctrl->val;
1918                p->codec.mpeg4.level = mpeg4_level(ctrl->val);
1919                if (p->codec.mpeg4.level < 0) {
1920                        mfc_err("Level number is wrong\n");
1921                        ret = p->codec.mpeg4.level;
1922                }
1923                break;
1924        case V4L2_CID_MPEG_VIDEO_H264_LOOP_FILTER_MODE:
1925                p->codec.h264.loop_filter_mode = ctrl->val;
1926                break;
1927        case V4L2_CID_MPEG_VIDEO_H264_LOOP_FILTER_ALPHA:
1928                p->codec.h264.loop_filter_alpha = ctrl->val;
1929                break;
1930        case V4L2_CID_MPEG_VIDEO_H264_LOOP_FILTER_BETA:
1931                p->codec.h264.loop_filter_beta = ctrl->val;
1932                break;
1933        case V4L2_CID_MPEG_VIDEO_H264_ENTROPY_MODE:
1934                p->codec.h264.entropy_mode = ctrl->val;
1935                break;
1936        case V4L2_CID_MPEG_MFC51_VIDEO_H264_NUM_REF_PIC_FOR_P:
1937                p->codec.h264.num_ref_pic_4p = ctrl->val;
1938                break;
1939        case V4L2_CID_MPEG_VIDEO_H264_8X8_TRANSFORM:
1940                p->codec.h264._8x8_transform = ctrl->val;
1941                break;
1942        case V4L2_CID_MPEG_VIDEO_MB_RC_ENABLE:
1943                p->rc_mb = ctrl->val;
1944                break;
1945        case V4L2_CID_MPEG_VIDEO_H264_I_FRAME_QP:
1946                p->codec.h264.rc_frame_qp = ctrl->val;
1947                break;
1948        case V4L2_CID_MPEG_VIDEO_H264_MIN_QP:
1949                p->codec.h264.rc_min_qp = ctrl->val;
1950                break;
1951        case V4L2_CID_MPEG_VIDEO_H264_MAX_QP:
1952                p->codec.h264.rc_max_qp = ctrl->val;
1953                break;
1954        case V4L2_CID_MPEG_VIDEO_H264_P_FRAME_QP:
1955                p->codec.h264.rc_p_frame_qp = ctrl->val;
1956                break;
1957        case V4L2_CID_MPEG_VIDEO_H264_B_FRAME_QP:
1958                p->codec.h264.rc_b_frame_qp = ctrl->val;
1959                break;
1960        case V4L2_CID_MPEG_VIDEO_MPEG4_I_FRAME_QP:
1961        case V4L2_CID_MPEG_VIDEO_H263_I_FRAME_QP:
1962                p->codec.mpeg4.rc_frame_qp = ctrl->val;
1963                break;
1964        case V4L2_CID_MPEG_VIDEO_MPEG4_MIN_QP:
1965        case V4L2_CID_MPEG_VIDEO_H263_MIN_QP:
1966                p->codec.mpeg4.rc_min_qp = ctrl->val;
1967                break;
1968        case V4L2_CID_MPEG_VIDEO_MPEG4_MAX_QP:
1969        case V4L2_CID_MPEG_VIDEO_H263_MAX_QP:
1970                p->codec.mpeg4.rc_max_qp = ctrl->val;
1971                break;
1972        case V4L2_CID_MPEG_VIDEO_MPEG4_P_FRAME_QP:
1973        case V4L2_CID_MPEG_VIDEO_H263_P_FRAME_QP:
1974                p->codec.mpeg4.rc_p_frame_qp = ctrl->val;
1975                break;
1976        case V4L2_CID_MPEG_VIDEO_MPEG4_B_FRAME_QP:
1977        case V4L2_CID_MPEG_VIDEO_H263_B_FRAME_QP:
1978                p->codec.mpeg4.rc_b_frame_qp = ctrl->val;
1979                break;
1980        case V4L2_CID_MPEG_MFC51_VIDEO_H264_ADAPTIVE_RC_DARK:
1981                p->codec.h264.rc_mb_dark = ctrl->val;
1982                break;
1983        case V4L2_CID_MPEG_MFC51_VIDEO_H264_ADAPTIVE_RC_SMOOTH:
1984                p->codec.h264.rc_mb_smooth = ctrl->val;
1985                break;
1986        case V4L2_CID_MPEG_MFC51_VIDEO_H264_ADAPTIVE_RC_STATIC:
1987                p->codec.h264.rc_mb_static = ctrl->val;
1988                break;
1989        case V4L2_CID_MPEG_MFC51_VIDEO_H264_ADAPTIVE_RC_ACTIVITY:
1990                p->codec.h264.rc_mb_activity = ctrl->val;
1991                break;
1992        case V4L2_CID_MPEG_VIDEO_H264_VUI_SAR_ENABLE:
1993                p->codec.h264.vui_sar = ctrl->val;
1994                break;
1995        case V4L2_CID_MPEG_VIDEO_H264_VUI_SAR_IDC:
1996                p->codec.h264.vui_sar_idc = vui_sar_idc(ctrl->val);
1997                break;
1998        case V4L2_CID_MPEG_VIDEO_H264_VUI_EXT_SAR_WIDTH:
1999                p->codec.h264.vui_ext_sar_width = ctrl->val;
2000                break;
2001        case V4L2_CID_MPEG_VIDEO_H264_VUI_EXT_SAR_HEIGHT:
2002                p->codec.h264.vui_ext_sar_height = ctrl->val;
2003                break;
2004        case V4L2_CID_MPEG_VIDEO_GOP_CLOSURE:
2005                p->codec.h264.open_gop = !ctrl->val;
2006                break;
2007        case V4L2_CID_MPEG_VIDEO_H264_I_PERIOD:
2008                p->codec.h264.open_gop_size = ctrl->val;
2009                break;
2010        case V4L2_CID_MPEG_VIDEO_MPEG4_PROFILE:
2011                switch (ctrl->val) {
2012                case V4L2_MPEG_VIDEO_MPEG4_PROFILE_SIMPLE:
2013                        p->codec.mpeg4.profile =
2014                                S5P_FIMV_ENC_PROFILE_MPEG4_SIMPLE;
2015                        break;
2016                case V4L2_MPEG_VIDEO_MPEG4_PROFILE_ADVANCED_SIMPLE:
2017                        p->codec.mpeg4.profile =
2018                        S5P_FIMV_ENC_PROFILE_MPEG4_ADVANCED_SIMPLE;
2019                        break;
2020                default:
2021                        ret = -EINVAL;
2022                }
2023                break;
2024        case V4L2_CID_MPEG_VIDEO_MPEG4_QPEL:
2025                p->codec.mpeg4.quarter_pixel = ctrl->val;
2026                break;
2027        case V4L2_CID_MPEG_VIDEO_VPX_NUM_PARTITIONS:
2028                p->codec.vp8.num_partitions = ctrl->val;
2029                break;
2030        case V4L2_CID_MPEG_VIDEO_VPX_IMD_DISABLE_4X4:
2031                p->codec.vp8.imd_4x4 = ctrl->val;
2032                break;
2033        case V4L2_CID_MPEG_VIDEO_VPX_NUM_REF_FRAMES:
2034                p->codec.vp8.num_ref = ctrl->val;
2035                break;
2036        case V4L2_CID_MPEG_VIDEO_VPX_FILTER_LEVEL:
2037                p->codec.vp8.filter_level = ctrl->val;
2038                break;
2039        case V4L2_CID_MPEG_VIDEO_VPX_FILTER_SHARPNESS:
2040                p->codec.vp8.filter_sharpness = ctrl->val;
2041                break;
2042        case V4L2_CID_MPEG_VIDEO_VPX_GOLDEN_FRAME_REF_PERIOD:
2043                p->codec.vp8.golden_frame_ref_period = ctrl->val;
2044                break;
2045        case V4L2_CID_MPEG_VIDEO_VPX_GOLDEN_FRAME_SEL:
2046                p->codec.vp8.golden_frame_sel = ctrl->val;
2047                break;
2048        case V4L2_CID_MPEG_VIDEO_VPX_MIN_QP:
2049                p->codec.vp8.rc_min_qp = ctrl->val;
2050                break;
2051        case V4L2_CID_MPEG_VIDEO_VPX_MAX_QP:
2052                p->codec.vp8.rc_max_qp = ctrl->val;
2053                break;
2054        case V4L2_CID_MPEG_VIDEO_VPX_I_FRAME_QP:
2055                p->codec.vp8.rc_frame_qp = ctrl->val;
2056                break;
2057        case V4L2_CID_MPEG_VIDEO_VPX_P_FRAME_QP:
2058                p->codec.vp8.rc_p_frame_qp = ctrl->val;
2059                break;
2060        case V4L2_CID_MPEG_VIDEO_VP8_PROFILE:
2061                p->codec.vp8.profile = ctrl->val;
2062                break;
2063        case V4L2_CID_MPEG_VIDEO_HEVC_I_FRAME_QP:
2064                p->codec.hevc.rc_frame_qp = ctrl->val;
2065                break;
2066        case V4L2_CID_MPEG_VIDEO_HEVC_P_FRAME_QP:
2067                p->codec.hevc.rc_p_frame_qp = ctrl->val;
2068                break;
2069        case V4L2_CID_MPEG_VIDEO_HEVC_B_FRAME_QP:
2070                p->codec.hevc.rc_b_frame_qp = ctrl->val;
2071                break;
2072        case V4L2_CID_MPEG_VIDEO_HEVC_FRAME_RATE_RESOLUTION:
2073                p->codec.hevc.rc_framerate = ctrl->val;
2074                break;
2075        case V4L2_CID_MPEG_VIDEO_HEVC_MIN_QP:
2076                p->codec.hevc.rc_min_qp = ctrl->val;
2077                __enc_update_hevc_qp_ctrls_range(ctx, ctrl->val,
2078                                                 p->codec.hevc.rc_max_qp);
2079                break;
2080        case V4L2_CID_MPEG_VIDEO_HEVC_MAX_QP:
2081                p->codec.hevc.rc_max_qp = ctrl->val;
2082                __enc_update_hevc_qp_ctrls_range(ctx, p->codec.hevc.rc_min_qp,
2083                                                 ctrl->val);
2084                break;
2085        case V4L2_CID_MPEG_VIDEO_HEVC_LEVEL:
2086                p->codec.hevc.level_v4l2 = ctrl->val;
2087                p->codec.hevc.level = hevc_level(ctrl->val);
2088                break;
2089        case V4L2_CID_MPEG_VIDEO_HEVC_PROFILE:
2090                switch (ctrl->val) {
2091                case V4L2_MPEG_VIDEO_HEVC_PROFILE_MAIN:
2092                        p->codec.hevc.profile =
2093                                V4L2_MPEG_VIDEO_HEVC_PROFILE_MAIN;
2094                        break;
2095                case V4L2_MPEG_VIDEO_HEVC_PROFILE_MAIN_STILL_PICTURE:
2096                        p->codec.hevc.profile =
2097                        V4L2_MPEG_VIDEO_HEVC_PROFILE_MAIN_STILL_PICTURE;
2098                        break;
2099                default:
2100                        ret = -EINVAL;
2101                }
2102                break;
2103        case V4L2_CID_MPEG_VIDEO_HEVC_TIER:
2104                p->codec.hevc.tier = ctrl->val;
2105                break;
2106        case V4L2_CID_MPEG_VIDEO_HEVC_MAX_PARTITION_DEPTH:
2107                p->codec.hevc.max_partition_depth = ctrl->val;
2108                break;
2109        case V4L2_CID_MPEG_VIDEO_REF_NUMBER_FOR_PFRAMES:
2110                p->codec.hevc.num_refs_for_p = ctrl->val;
2111                break;
2112        case V4L2_CID_MPEG_VIDEO_HEVC_REFRESH_TYPE:
2113                p->codec.hevc.refreshtype = ctrl->val;
2114                break;
2115        case V4L2_CID_MPEG_VIDEO_HEVC_CONST_INTRA_PRED:
2116                p->codec.hevc.const_intra_period_enable = ctrl->val;
2117                break;
2118        case V4L2_CID_MPEG_VIDEO_HEVC_LOSSLESS_CU:
2119                p->codec.hevc.lossless_cu_enable = ctrl->val;
2120                break;
2121        case V4L2_CID_MPEG_VIDEO_HEVC_WAVEFRONT:
2122                p->codec.hevc.wavefront_enable = ctrl->val;
2123                break;
2124        case V4L2_CID_MPEG_VIDEO_HEVC_LOOP_FILTER_MODE:
2125                p->codec.hevc.loopfilter = ctrl->val;
2126                break;
2127        case V4L2_CID_MPEG_VIDEO_HEVC_HIER_QP:
2128                p->codec.hevc.hier_qp_enable = ctrl->val;
2129                break;
2130        case V4L2_CID_MPEG_VIDEO_HEVC_HIER_CODING_TYPE:
2131                p->codec.hevc.hier_qp_type = ctrl->val;
2132                break;
2133        case V4L2_CID_MPEG_VIDEO_HEVC_HIER_CODING_LAYER:
2134                p->codec.hevc.num_hier_layer = ctrl->val;
2135                break;
2136        case V4L2_CID_MPEG_VIDEO_HEVC_HIER_CODING_L0_QP:
2137                p->codec.hevc.hier_qp_layer[0] = ctrl->val;
2138                break;
2139        case V4L2_CID_MPEG_VIDEO_HEVC_HIER_CODING_L1_QP:
2140                p->codec.hevc.hier_qp_layer[1] = ctrl->val;
2141                break;
2142        case V4L2_CID_MPEG_VIDEO_HEVC_HIER_CODING_L2_QP:
2143                p->codec.hevc.hier_qp_layer[2] = ctrl->val;
2144                break;
2145        case V4L2_CID_MPEG_VIDEO_HEVC_HIER_CODING_L3_QP:
2146                p->codec.hevc.hier_qp_layer[3] = ctrl->val;
2147                break;
2148        case V4L2_CID_MPEG_VIDEO_HEVC_HIER_CODING_L4_QP:
2149                p->codec.hevc.hier_qp_layer[4] = ctrl->val;
2150                break;
2151        case V4L2_CID_MPEG_VIDEO_HEVC_HIER_CODING_L5_QP:
2152                p->codec.hevc.hier_qp_layer[5] = ctrl->val;
2153                break;
2154        case V4L2_CID_MPEG_VIDEO_HEVC_HIER_CODING_L6_QP:
2155                p->codec.hevc.hier_qp_layer[6] = ctrl->val;
2156                break;
2157        case V4L2_CID_MPEG_VIDEO_HEVC_HIER_CODING_L0_BR:
2158                p->codec.hevc.hier_bit_layer[0] = ctrl->val;
2159                break;
2160        case V4L2_CID_MPEG_VIDEO_HEVC_HIER_CODING_L1_BR:
2161                p->codec.hevc.hier_bit_layer[1] = ctrl->val;
2162                break;
2163        case V4L2_CID_MPEG_VIDEO_HEVC_HIER_CODING_L2_BR:
2164                p->codec.hevc.hier_bit_layer[2] = ctrl->val;
2165                break;
2166        case V4L2_CID_MPEG_VIDEO_HEVC_HIER_CODING_L3_BR:
2167                p->codec.hevc.hier_bit_layer[3] = ctrl->val;
2168                break;
2169        case V4L2_CID_MPEG_VIDEO_HEVC_HIER_CODING_L4_BR:
2170                p->codec.hevc.hier_bit_layer[4] = ctrl->val;
2171                break;
2172        case V4L2_CID_MPEG_VIDEO_HEVC_HIER_CODING_L5_BR:
2173                p->codec.hevc.hier_bit_layer[5] = ctrl->val;
2174                break;
2175        case V4L2_CID_MPEG_VIDEO_HEVC_HIER_CODING_L6_BR:
2176                p->codec.hevc.hier_bit_layer[6] = ctrl->val;
2177                break;
2178        case V4L2_CID_MPEG_VIDEO_HEVC_GENERAL_PB:
2179                p->codec.hevc.general_pb_enable = ctrl->val;
2180                break;
2181        case V4L2_CID_MPEG_VIDEO_HEVC_TEMPORAL_ID:
2182                p->codec.hevc.temporal_id_enable = ctrl->val;
2183                break;
2184        case V4L2_CID_MPEG_VIDEO_HEVC_STRONG_SMOOTHING:
2185                p->codec.hevc.strong_intra_smooth = ctrl->val;
2186                break;
2187        case V4L2_CID_MPEG_VIDEO_HEVC_INTRA_PU_SPLIT:
2188                p->codec.hevc.intra_pu_split_disable = ctrl->val;
2189                break;
2190        case V4L2_CID_MPEG_VIDEO_HEVC_TMV_PREDICTION:
2191                p->codec.hevc.tmv_prediction_disable = !ctrl->val;
2192                break;
2193        case V4L2_CID_MPEG_VIDEO_HEVC_MAX_NUM_MERGE_MV_MINUS1:
2194                p->codec.hevc.max_num_merge_mv = ctrl->val;
2195                break;
2196        case V4L2_CID_MPEG_VIDEO_HEVC_WITHOUT_STARTCODE:
2197                p->codec.hevc.encoding_nostartcode_enable = ctrl->val;
2198                break;
2199        case V4L2_CID_MPEG_VIDEO_HEVC_REFRESH_PERIOD:
2200                p->codec.hevc.refreshperiod = ctrl->val;
2201                break;
2202        case V4L2_CID_MPEG_VIDEO_HEVC_LF_BETA_OFFSET_DIV2:
2203                p->codec.hevc.lf_beta_offset_div2 = ctrl->val;
2204                break;
2205        case V4L2_CID_MPEG_VIDEO_HEVC_LF_TC_OFFSET_DIV2:
2206                p->codec.hevc.lf_tc_offset_div2 = ctrl->val;
2207                break;
2208        case V4L2_CID_MPEG_VIDEO_HEVC_SIZE_OF_LENGTH_FIELD:
2209                p->codec.hevc.size_of_length_field = ctrl->val;
2210                break;
2211        case V4L2_CID_MPEG_VIDEO_PREPEND_SPSPPS_TO_IDR:
2212                p->codec.hevc.prepend_sps_pps_to_idr = ctrl->val;
2213                break;
2214        default:
2215                v4l2_err(&dev->v4l2_dev, "Invalid control, id=%d, val=%d\n",
2216                                                        ctrl->id, ctrl->val);
2217                ret = -EINVAL;
2218        }
2219        return ret;
2220}
2221
2222static int s5p_mfc_enc_g_v_ctrl(struct v4l2_ctrl *ctrl)
2223{
2224        struct s5p_mfc_ctx *ctx = ctrl_to_ctx(ctrl);
2225        struct s5p_mfc_dev *dev = ctx->dev;
2226
2227        switch (ctrl->id) {
2228        case V4L2_CID_MIN_BUFFERS_FOR_OUTPUT:
2229                if (ctx->state >= MFCINST_HEAD_PARSED &&
2230                    ctx->state < MFCINST_ABORT) {
2231                        ctrl->val = ctx->pb_count;
2232                        break;
2233                } else if (ctx->state != MFCINST_INIT) {
2234                        v4l2_err(&dev->v4l2_dev, "Encoding not initialised\n");
2235                        return -EINVAL;
2236                }
2237                /* Should wait for the header to be produced */
2238                s5p_mfc_wait_for_done_ctx(ctx,
2239                                S5P_MFC_R2H_CMD_SEQ_DONE_RET, 0);
2240                if (ctx->state >= MFCINST_HEAD_PARSED &&
2241                    ctx->state < MFCINST_ABORT) {
2242                        ctrl->val = ctx->pb_count;
2243                } else {
2244                        v4l2_err(&dev->v4l2_dev, "Encoding not initialised\n");
2245                        return -EINVAL;
2246                }
2247                break;
2248        }
2249        return 0;
2250}
2251
2252static const struct v4l2_ctrl_ops s5p_mfc_enc_ctrl_ops = {
2253        .s_ctrl = s5p_mfc_enc_s_ctrl,
2254        .g_volatile_ctrl = s5p_mfc_enc_g_v_ctrl,
2255};
2256
2257static int vidioc_s_parm(struct file *file, void *priv,
2258                         struct v4l2_streamparm *a)
2259{
2260        struct s5p_mfc_ctx *ctx = fh_to_ctx(priv);
2261
2262        if (a->type == V4L2_BUF_TYPE_VIDEO_OUTPUT_MPLANE) {
2263                ctx->enc_params.rc_framerate_num =
2264                                        a->parm.output.timeperframe.denominator;
2265                ctx->enc_params.rc_framerate_denom =
2266                                        a->parm.output.timeperframe.numerator;
2267        } else {
2268                mfc_err("Setting FPS is only possible for the output queue\n");
2269                return -EINVAL;
2270        }
2271        return 0;
2272}
2273
2274static int vidioc_g_parm(struct file *file, void *priv,
2275                         struct v4l2_streamparm *a)
2276{
2277        struct s5p_mfc_ctx *ctx = fh_to_ctx(priv);
2278
2279        if (a->type == V4L2_BUF_TYPE_VIDEO_OUTPUT) {
2280                a->parm.output.timeperframe.denominator =
2281                                        ctx->enc_params.rc_framerate_num;
2282                a->parm.output.timeperframe.numerator =
2283                                        ctx->enc_params.rc_framerate_denom;
2284        } else {
2285                mfc_err("Setting FPS is only possible for the output queue\n");
2286                return -EINVAL;
2287        }
2288        return 0;
2289}
2290
2291static int vidioc_encoder_cmd(struct file *file, void *priv,
2292                              struct v4l2_encoder_cmd *cmd)
2293{
2294        struct s5p_mfc_ctx *ctx = fh_to_ctx(priv);
2295        struct s5p_mfc_dev *dev = ctx->dev;
2296        struct s5p_mfc_buf *buf;
2297        unsigned long flags;
2298
2299        switch (cmd->cmd) {
2300        case V4L2_ENC_CMD_STOP:
2301                if (cmd->flags != 0)
2302                        return -EINVAL;
2303
2304                if (!ctx->vq_src.streaming)
2305                        return -EINVAL;
2306
2307                spin_lock_irqsave(&dev->irqlock, flags);
2308                if (list_empty(&ctx->src_queue)) {
2309                        mfc_debug(2, "EOS: empty src queue, entering finishing state\n");
2310                        ctx->state = MFCINST_FINISHING;
2311                        if (s5p_mfc_ctx_ready(ctx))
2312                                set_work_bit_irqsave(ctx);
2313                        spin_unlock_irqrestore(&dev->irqlock, flags);
2314                        s5p_mfc_hw_call(dev->mfc_ops, try_run, dev);
2315                } else {
2316                        mfc_debug(2, "EOS: marking last buffer of stream\n");
2317                        buf = list_entry(ctx->src_queue.prev,
2318                                                struct s5p_mfc_buf, list);
2319                        if (buf->flags & MFC_BUF_FLAG_USED)
2320                                ctx->state = MFCINST_FINISHING;
2321                        else
2322                                buf->flags |= MFC_BUF_FLAG_EOS;
2323                        spin_unlock_irqrestore(&dev->irqlock, flags);
2324                }
2325                break;
2326        default:
2327                return -EINVAL;
2328
2329        }
2330        return 0;
2331}
2332
2333static int vidioc_subscribe_event(struct v4l2_fh *fh,
2334                                  const struct v4l2_event_subscription *sub)
2335{
2336        switch (sub->type) {
2337        case V4L2_EVENT_EOS:
2338                return v4l2_event_subscribe(fh, sub, 2, NULL);
2339        default:
2340                return -EINVAL;
2341        }
2342}
2343
2344static const struct v4l2_ioctl_ops s5p_mfc_enc_ioctl_ops = {
2345        .vidioc_querycap = vidioc_querycap,
2346        .vidioc_enum_fmt_vid_cap_mplane = vidioc_enum_fmt_vid_cap_mplane,
2347        .vidioc_enum_fmt_vid_out_mplane = vidioc_enum_fmt_vid_out_mplane,
2348        .vidioc_g_fmt_vid_cap_mplane = vidioc_g_fmt,
2349        .vidioc_g_fmt_vid_out_mplane = vidioc_g_fmt,
2350        .vidioc_try_fmt_vid_cap_mplane = vidioc_try_fmt,
2351        .vidioc_try_fmt_vid_out_mplane = vidioc_try_fmt,
2352        .vidioc_s_fmt_vid_cap_mplane = vidioc_s_fmt,
2353        .vidioc_s_fmt_vid_out_mplane = vidioc_s_fmt,
2354        .vidioc_reqbufs = vidioc_reqbufs,
2355        .vidioc_querybuf = vidioc_querybuf,
2356        .vidioc_qbuf = vidioc_qbuf,
2357        .vidioc_dqbuf = vidioc_dqbuf,
2358        .vidioc_expbuf = vidioc_expbuf,
2359        .vidioc_streamon = vidioc_streamon,
2360        .vidioc_streamoff = vidioc_streamoff,
2361        .vidioc_s_parm = vidioc_s_parm,
2362        .vidioc_g_parm = vidioc_g_parm,
2363        .vidioc_encoder_cmd = vidioc_encoder_cmd,
2364        .vidioc_subscribe_event = vidioc_subscribe_event,
2365        .vidioc_unsubscribe_event = v4l2_event_unsubscribe,
2366};
2367
2368static int check_vb_with_fmt(struct s5p_mfc_fmt *fmt, struct vb2_buffer *vb)
2369{
2370        int i;
2371
2372        if (!fmt)
2373                return -EINVAL;
2374        if (fmt->num_planes != vb->num_planes) {
2375                mfc_err("invalid plane number for the format\n");
2376                return -EINVAL;
2377        }
2378        for (i = 0; i < fmt->num_planes; i++) {
2379                dma_addr_t dma = vb2_dma_contig_plane_dma_addr(vb, i);
2380                if (!dma) {
2381                        mfc_err("failed to get plane cookie\n");
2382                        return -EINVAL;
2383                }
2384                mfc_debug(2, "index: %d, plane[%d] cookie: %pad\n",
2385                          vb->index, i, &dma);
2386        }
2387        return 0;
2388}
2389
2390static int s5p_mfc_queue_setup(struct vb2_queue *vq,
2391                        unsigned int *buf_count, unsigned int *plane_count,
2392                        unsigned int psize[], struct device *alloc_devs[])
2393{
2394        struct s5p_mfc_ctx *ctx = fh_to_ctx(vq->drv_priv);
2395        struct s5p_mfc_dev *dev = ctx->dev;
2396
2397        if (vq->type == V4L2_BUF_TYPE_VIDEO_CAPTURE_MPLANE) {
2398                if (ctx->state != MFCINST_GOT_INST) {
2399                        mfc_err("invalid state: %d\n", ctx->state);
2400                        return -EINVAL;
2401                }
2402
2403                if (ctx->dst_fmt)
2404                        *plane_count = ctx->dst_fmt->num_planes;
2405                else
2406                        *plane_count = MFC_ENC_CAP_PLANE_COUNT;
2407                if (*buf_count < 1)
2408                        *buf_count = 1;
2409                if (*buf_count > MFC_MAX_BUFFERS)
2410                        *buf_count = MFC_MAX_BUFFERS;
2411                psize[0] = ctx->enc_dst_buf_size;
2412                alloc_devs[0] = ctx->dev->mem_dev[BANK_L_CTX];
2413        } else if (vq->type == V4L2_BUF_TYPE_VIDEO_OUTPUT_MPLANE) {
2414                if (ctx->src_fmt)
2415                        *plane_count = ctx->src_fmt->num_planes;
2416                else
2417                        *plane_count = MFC_ENC_OUT_PLANE_COUNT;
2418
2419                if (*buf_count < 1)
2420                        *buf_count = 1;
2421                if (*buf_count > MFC_MAX_BUFFERS)
2422                        *buf_count = MFC_MAX_BUFFERS;
2423
2424                psize[0] = ctx->luma_size;
2425                psize[1] = ctx->chroma_size;
2426
2427                if (IS_MFCV6_PLUS(dev)) {
2428                        alloc_devs[0] = ctx->dev->mem_dev[BANK_L_CTX];
2429                        alloc_devs[1] = ctx->dev->mem_dev[BANK_L_CTX];
2430                } else {
2431                        alloc_devs[0] = ctx->dev->mem_dev[BANK_R_CTX];
2432                        alloc_devs[1] = ctx->dev->mem_dev[BANK_R_CTX];
2433                }
2434        } else {
2435                mfc_err("invalid queue type: %d\n", vq->type);
2436                return -EINVAL;
2437        }
2438        return 0;
2439}
2440
2441static int s5p_mfc_buf_init(struct vb2_buffer *vb)
2442{
2443        struct vb2_v4l2_buffer *vbuf = to_vb2_v4l2_buffer(vb);
2444        struct vb2_queue *vq = vb->vb2_queue;
2445        struct s5p_mfc_ctx *ctx = fh_to_ctx(vq->drv_priv);
2446        unsigned int i;
2447        int ret;
2448
2449        if (vq->type == V4L2_BUF_TYPE_VIDEO_CAPTURE_MPLANE) {
2450                ret = check_vb_with_fmt(ctx->dst_fmt, vb);
2451                if (ret < 0)
2452                        return ret;
2453                i = vb->index;
2454                ctx->dst_bufs[i].b = vbuf;
2455                ctx->dst_bufs[i].cookie.stream =
2456                                        vb2_dma_contig_plane_dma_addr(vb, 0);
2457                ctx->dst_bufs_cnt++;
2458        } else if (vq->type == V4L2_BUF_TYPE_VIDEO_OUTPUT_MPLANE) {
2459                ret = check_vb_with_fmt(ctx->src_fmt, vb);
2460                if (ret < 0)
2461                        return ret;
2462                i = vb->index;
2463                ctx->src_bufs[i].b = vbuf;
2464                ctx->src_bufs[i].cookie.raw.luma =
2465                                        vb2_dma_contig_plane_dma_addr(vb, 0);
2466                ctx->src_bufs[i].cookie.raw.chroma =
2467                                        vb2_dma_contig_plane_dma_addr(vb, 1);
2468                ctx->src_bufs_cnt++;
2469        } else {
2470                mfc_err("invalid queue type: %d\n", vq->type);
2471                return -EINVAL;
2472        }
2473        return 0;
2474}
2475
2476static int s5p_mfc_buf_prepare(struct vb2_buffer *vb)
2477{
2478        struct vb2_queue *vq = vb->vb2_queue;
2479        struct s5p_mfc_ctx *ctx = fh_to_ctx(vq->drv_priv);
2480        int ret;
2481
2482        if (vq->type == V4L2_BUF_TYPE_VIDEO_CAPTURE_MPLANE) {
2483                ret = check_vb_with_fmt(ctx->dst_fmt, vb);
2484                if (ret < 0)
2485                        return ret;
2486                mfc_debug(2, "plane size: %ld, dst size: %zu\n",
2487                        vb2_plane_size(vb, 0), ctx->enc_dst_buf_size);
2488                if (vb2_plane_size(vb, 0) < ctx->enc_dst_buf_size) {
2489                        mfc_err("plane size is too small for capture\n");
2490                        return -EINVAL;
2491                }
2492        } else if (vq->type == V4L2_BUF_TYPE_VIDEO_OUTPUT_MPLANE) {
2493                ret = check_vb_with_fmt(ctx->src_fmt, vb);
2494                if (ret < 0)
2495                        return ret;
2496                mfc_debug(2, "plane size: %ld, luma size: %d\n",
2497                        vb2_plane_size(vb, 0), ctx->luma_size);
2498                mfc_debug(2, "plane size: %ld, chroma size: %d\n",
2499                        vb2_plane_size(vb, 1), ctx->chroma_size);
2500                if (vb2_plane_size(vb, 0) < ctx->luma_size ||
2501                    vb2_plane_size(vb, 1) < ctx->chroma_size) {
2502                        mfc_err("plane size is too small for output\n");
2503                        return -EINVAL;
2504                }
2505        } else {
2506                mfc_err("invalid queue type: %d\n", vq->type);
2507                return -EINVAL;
2508        }
2509        return 0;
2510}
2511
2512static int s5p_mfc_start_streaming(struct vb2_queue *q, unsigned int count)
2513{
2514        struct s5p_mfc_ctx *ctx = fh_to_ctx(q->drv_priv);
2515        struct s5p_mfc_dev *dev = ctx->dev;
2516
2517        if (IS_MFCV6_PLUS(dev) &&
2518                        (q->type == V4L2_BUF_TYPE_VIDEO_CAPTURE_MPLANE)) {
2519
2520                if ((ctx->state == MFCINST_GOT_INST) &&
2521                        (dev->curr_ctx == ctx->num) && dev->hw_lock) {
2522                        s5p_mfc_wait_for_done_ctx(ctx,
2523                                                S5P_MFC_R2H_CMD_SEQ_DONE_RET,
2524                                                0);
2525                }
2526
2527                if (ctx->src_bufs_cnt < ctx->pb_count) {
2528                        mfc_err("Need minimum %d OUTPUT buffers\n",
2529                                        ctx->pb_count);
2530                        return -ENOBUFS;
2531                }
2532        }
2533
2534        /* If context is ready then dev = work->data;schedule it to run */
2535        if (s5p_mfc_ctx_ready(ctx))
2536                set_work_bit_irqsave(ctx);
2537        s5p_mfc_hw_call(dev->mfc_ops, try_run, dev);
2538
2539        return 0;
2540}
2541
2542static void s5p_mfc_stop_streaming(struct vb2_queue *q)
2543{
2544        unsigned long flags;
2545        struct s5p_mfc_ctx *ctx = fh_to_ctx(q->drv_priv);
2546        struct s5p_mfc_dev *dev = ctx->dev;
2547
2548        if ((ctx->state == MFCINST_FINISHING ||
2549                ctx->state == MFCINST_RUNNING) &&
2550                dev->curr_ctx == ctx->num && dev->hw_lock) {
2551                ctx->state = MFCINST_ABORT;
2552                s5p_mfc_wait_for_done_ctx(ctx, S5P_MFC_R2H_CMD_FRAME_DONE_RET,
2553                                          0);
2554        }
2555        ctx->state = MFCINST_FINISHED;
2556        spin_lock_irqsave(&dev->irqlock, flags);
2557        if (q->type == V4L2_BUF_TYPE_VIDEO_CAPTURE_MPLANE) {
2558                s5p_mfc_cleanup_queue(&ctx->dst_queue, &ctx->vq_dst);
2559                INIT_LIST_HEAD(&ctx->dst_queue);
2560                ctx->dst_queue_cnt = 0;
2561        }
2562        if (q->type == V4L2_BUF_TYPE_VIDEO_OUTPUT_MPLANE) {
2563                cleanup_ref_queue(ctx);
2564                s5p_mfc_cleanup_queue(&ctx->src_queue, &ctx->vq_src);
2565                INIT_LIST_HEAD(&ctx->src_queue);
2566                ctx->src_queue_cnt = 0;
2567        }
2568        spin_unlock_irqrestore(&dev->irqlock, flags);
2569}
2570
2571static void s5p_mfc_buf_queue(struct vb2_buffer *vb)
2572{
2573        struct vb2_queue *vq = vb->vb2_queue;
2574        struct s5p_mfc_ctx *ctx = fh_to_ctx(vq->drv_priv);
2575        struct s5p_mfc_dev *dev = ctx->dev;
2576        unsigned long flags;
2577        struct s5p_mfc_buf *mfc_buf;
2578
2579        if (ctx->state == MFCINST_ERROR) {
2580                vb2_buffer_done(vb, VB2_BUF_STATE_ERROR);
2581                cleanup_ref_queue(ctx);
2582                return;
2583        }
2584        if (vq->type == V4L2_BUF_TYPE_VIDEO_CAPTURE_MPLANE) {
2585                mfc_buf = &ctx->dst_bufs[vb->index];
2586                mfc_buf->flags &= ~MFC_BUF_FLAG_USED;
2587                /* Mark destination as available for use by MFC */
2588                spin_lock_irqsave(&dev->irqlock, flags);
2589                list_add_tail(&mfc_buf->list, &ctx->dst_queue);
2590                ctx->dst_queue_cnt++;
2591                spin_unlock_irqrestore(&dev->irqlock, flags);
2592        } else if (vq->type == V4L2_BUF_TYPE_VIDEO_OUTPUT_MPLANE) {
2593                mfc_buf = &ctx->src_bufs[vb->index];
2594                mfc_buf->flags &= ~MFC_BUF_FLAG_USED;
2595                spin_lock_irqsave(&dev->irqlock, flags);
2596                list_add_tail(&mfc_buf->list, &ctx->src_queue);
2597                ctx->src_queue_cnt++;
2598                spin_unlock_irqrestore(&dev->irqlock, flags);
2599        } else {
2600                mfc_err("unsupported buffer type (%d)\n", vq->type);
2601        }
2602        if (s5p_mfc_ctx_ready(ctx))
2603                set_work_bit_irqsave(ctx);
2604        s5p_mfc_hw_call(dev->mfc_ops, try_run, dev);
2605}
2606
2607static struct vb2_ops s5p_mfc_enc_qops = {
2608        .queue_setup            = s5p_mfc_queue_setup,
2609        .wait_prepare           = vb2_ops_wait_prepare,
2610        .wait_finish            = vb2_ops_wait_finish,
2611        .buf_init               = s5p_mfc_buf_init,
2612        .buf_prepare            = s5p_mfc_buf_prepare,
2613        .start_streaming        = s5p_mfc_start_streaming,
2614        .stop_streaming         = s5p_mfc_stop_streaming,
2615        .buf_queue              = s5p_mfc_buf_queue,
2616};
2617
2618const struct s5p_mfc_codec_ops *get_enc_codec_ops(void)
2619{
2620        return &encoder_codec_ops;
2621}
2622
2623struct vb2_ops *get_enc_queue_ops(void)
2624{
2625        return &s5p_mfc_enc_qops;
2626}
2627
2628const struct v4l2_ioctl_ops *get_enc_v4l2_ioctl_ops(void)
2629{
2630        return &s5p_mfc_enc_ioctl_ops;
2631}
2632
2633#define IS_MFC51_PRIV(x) ((V4L2_CTRL_ID2WHICH(x) == V4L2_CTRL_CLASS_MPEG) \
2634                                                && V4L2_CTRL_DRIVER_PRIV(x))
2635
2636int s5p_mfc_enc_ctrls_setup(struct s5p_mfc_ctx *ctx)
2637{
2638        struct v4l2_ctrl_config cfg;
2639        int i;
2640
2641        v4l2_ctrl_handler_init(&ctx->ctrl_handler, NUM_CTRLS);
2642        if (ctx->ctrl_handler.error) {
2643                mfc_err("v4l2_ctrl_handler_init failed\n");
2644                return ctx->ctrl_handler.error;
2645        }
2646        for (i = 0; i < NUM_CTRLS; i++) {
2647                if (IS_MFC51_PRIV(controls[i].id)) {
2648                        memset(&cfg, 0, sizeof(struct v4l2_ctrl_config));
2649                        cfg.ops = &s5p_mfc_enc_ctrl_ops;
2650                        cfg.id = controls[i].id;
2651                        cfg.min = controls[i].minimum;
2652                        cfg.max = controls[i].maximum;
2653                        cfg.def = controls[i].default_value;
2654                        cfg.name = controls[i].name;
2655                        cfg.type = controls[i].type;
2656                        cfg.flags = 0;
2657
2658                        if (cfg.type == V4L2_CTRL_TYPE_MENU) {
2659                                cfg.step = 0;
2660                                cfg.menu_skip_mask = controls[i].menu_skip_mask;
2661                                cfg.qmenu = mfc51_get_menu(cfg.id);
2662                        } else {
2663                                cfg.step = controls[i].step;
2664                                cfg.menu_skip_mask = 0;
2665                        }
2666                        ctx->ctrls[i] = v4l2_ctrl_new_custom(&ctx->ctrl_handler,
2667                                        &cfg, NULL);
2668                } else {
2669                        if ((controls[i].type == V4L2_CTRL_TYPE_MENU) ||
2670                                (controls[i].type ==
2671                                        V4L2_CTRL_TYPE_INTEGER_MENU)) {
2672                                ctx->ctrls[i] = v4l2_ctrl_new_std_menu(
2673                                        &ctx->ctrl_handler,
2674                                        &s5p_mfc_enc_ctrl_ops, controls[i].id,
2675                                        controls[i].maximum, 0,
2676                                        controls[i].default_value);
2677                        } else {
2678                                ctx->ctrls[i] = v4l2_ctrl_new_std(
2679                                        &ctx->ctrl_handler,
2680                                        &s5p_mfc_enc_ctrl_ops, controls[i].id,
2681                                        controls[i].minimum,
2682                                        controls[i].maximum, controls[i].step,
2683                                        controls[i].default_value);
2684                        }
2685                }
2686                if (ctx->ctrl_handler.error) {
2687                        mfc_err("Adding control (%d) failed\n", i);
2688                        return ctx->ctrl_handler.error;
2689                }
2690                if (controls[i].is_volatile && ctx->ctrls[i])
2691                        ctx->ctrls[i]->flags |= V4L2_CTRL_FLAG_VOLATILE;
2692        }
2693        v4l2_ctrl_handler_setup(&ctx->ctrl_handler);
2694        return 0;
2695}
2696
2697void s5p_mfc_enc_ctrls_delete(struct s5p_mfc_ctx *ctx)
2698{
2699        int i;
2700
2701        v4l2_ctrl_handler_free(&ctx->ctrl_handler);
2702        for (i = 0; i < NUM_CTRLS; i++)
2703                ctx->ctrls[i] = NULL;
2704}
2705
2706void s5p_mfc_enc_init(struct s5p_mfc_ctx *ctx)
2707{
2708        struct v4l2_format f;
2709        f.fmt.pix_mp.pixelformat = DEF_SRC_FMT_ENC;
2710        ctx->src_fmt = find_format(&f, MFC_FMT_RAW);
2711        f.fmt.pix_mp.pixelformat = DEF_DST_FMT_ENC;
2712        ctx->dst_fmt = find_format(&f, MFC_FMT_ENC);
2713}
2714