linux/drivers/media/test-drivers/vivid/vivid-vid-common.c
<<
>>
Prefs
   1// SPDX-License-Identifier: GPL-2.0-only
   2/*
   3 * vivid-vid-common.c - common video support functions.
   4 *
   5 * Copyright 2014 Cisco Systems, Inc. and/or its affiliates. All rights reserved.
   6 */
   7
   8#include <linux/errno.h>
   9#include <linux/kernel.h>
  10#include <linux/sched.h>
  11#include <linux/videodev2.h>
  12#include <linux/v4l2-dv-timings.h>
  13#include <media/v4l2-common.h>
  14#include <media/v4l2-event.h>
  15#include <media/v4l2-dv-timings.h>
  16
  17#include "vivid-core.h"
  18#include "vivid-vid-common.h"
  19
  20const struct v4l2_dv_timings_cap vivid_dv_timings_cap = {
  21        .type = V4L2_DV_BT_656_1120,
  22        /* keep this initialization for compatibility with GCC < 4.4.6 */
  23        .reserved = { 0 },
  24        V4L2_INIT_BT_TIMINGS(16, MAX_WIDTH, 16, MAX_HEIGHT, 14000000, 775000000,
  25                V4L2_DV_BT_STD_CEA861 | V4L2_DV_BT_STD_DMT |
  26                V4L2_DV_BT_STD_CVT | V4L2_DV_BT_STD_GTF,
  27                V4L2_DV_BT_CAP_PROGRESSIVE | V4L2_DV_BT_CAP_INTERLACED)
  28};
  29
  30/* ------------------------------------------------------------------
  31        Basic structures
  32   ------------------------------------------------------------------*/
  33
  34struct vivid_fmt vivid_formats[] = {
  35        {
  36                .fourcc   = V4L2_PIX_FMT_YUYV,
  37                .vdownsampling = { 1 },
  38                .bit_depth = { 16 },
  39                .color_enc = TGP_COLOR_ENC_YCBCR,
  40                .planes   = 1,
  41                .buffers = 1,
  42                .data_offset = { PLANE0_DATA_OFFSET },
  43        },
  44        {
  45                .fourcc   = V4L2_PIX_FMT_UYVY,
  46                .vdownsampling = { 1 },
  47                .bit_depth = { 16 },
  48                .color_enc = TGP_COLOR_ENC_YCBCR,
  49                .planes   = 1,
  50                .buffers = 1,
  51        },
  52        {
  53                .fourcc   = V4L2_PIX_FMT_YVYU,
  54                .vdownsampling = { 1 },
  55                .bit_depth = { 16 },
  56                .color_enc = TGP_COLOR_ENC_YCBCR,
  57                .planes   = 1,
  58                .buffers = 1,
  59        },
  60        {
  61                .fourcc   = V4L2_PIX_FMT_VYUY,
  62                .vdownsampling = { 1 },
  63                .bit_depth = { 16 },
  64                .color_enc = TGP_COLOR_ENC_YCBCR,
  65                .planes   = 1,
  66                .buffers = 1,
  67        },
  68        {
  69                .fourcc   = V4L2_PIX_FMT_YUV422P,
  70                .vdownsampling = { 1, 1, 1 },
  71                .bit_depth = { 8, 4, 4 },
  72                .color_enc = TGP_COLOR_ENC_YCBCR,
  73                .planes   = 3,
  74                .buffers = 1,
  75        },
  76        {
  77                .fourcc   = V4L2_PIX_FMT_YUV444P,
  78                .vdownsampling = { 1, 1, 1 },
  79                .bit_depth = { 8, 8, 8 },
  80                .color_enc = TGP_COLOR_ENC_YCBCR,
  81                .planes   = 3,
  82                .buffers = 1,
  83        },
  84        {
  85                .fourcc   = V4L2_PIX_FMT_YUV420,
  86                .vdownsampling = { 1, 2, 2 },
  87                .bit_depth = { 8, 4, 4 },
  88                .color_enc = TGP_COLOR_ENC_YCBCR,
  89                .planes   = 3,
  90                .buffers = 1,
  91        },
  92        {
  93                .fourcc   = V4L2_PIX_FMT_YVU420,
  94                .vdownsampling = { 1, 2, 2 },
  95                .bit_depth = { 8, 4, 4 },
  96                .color_enc = TGP_COLOR_ENC_YCBCR,
  97                .planes   = 3,
  98                .buffers = 1,
  99        },
 100        {
 101                .fourcc   = V4L2_PIX_FMT_NV12,
 102                .vdownsampling = { 1, 2 },
 103                .bit_depth = { 8, 8 },
 104                .color_enc = TGP_COLOR_ENC_YCBCR,
 105                .planes   = 2,
 106                .buffers = 1,
 107        },
 108        {
 109                .fourcc   = V4L2_PIX_FMT_NV21,
 110                .vdownsampling = { 1, 2 },
 111                .bit_depth = { 8, 8 },
 112                .color_enc = TGP_COLOR_ENC_YCBCR,
 113                .planes   = 2,
 114                .buffers = 1,
 115        },
 116        {
 117                .fourcc   = V4L2_PIX_FMT_NV16,
 118                .vdownsampling = { 1, 1 },
 119                .bit_depth = { 8, 8 },
 120                .color_enc = TGP_COLOR_ENC_YCBCR,
 121                .planes   = 2,
 122                .buffers = 1,
 123        },
 124        {
 125                .fourcc   = V4L2_PIX_FMT_NV61,
 126                .vdownsampling = { 1, 1 },
 127                .bit_depth = { 8, 8 },
 128                .color_enc = TGP_COLOR_ENC_YCBCR,
 129                .planes   = 2,
 130                .buffers = 1,
 131        },
 132        {
 133                .fourcc   = V4L2_PIX_FMT_NV24,
 134                .vdownsampling = { 1, 1 },
 135                .bit_depth = { 8, 16 },
 136                .color_enc = TGP_COLOR_ENC_YCBCR,
 137                .planes   = 2,
 138                .buffers = 1,
 139        },
 140        {
 141                .fourcc   = V4L2_PIX_FMT_NV42,
 142                .vdownsampling = { 1, 1 },
 143                .bit_depth = { 8, 16 },
 144                .color_enc = TGP_COLOR_ENC_YCBCR,
 145                .planes   = 2,
 146                .buffers = 1,
 147        },
 148        {
 149                .fourcc   = V4L2_PIX_FMT_YUV555, /* uuuvvvvv ayyyyyuu */
 150                .vdownsampling = { 1 },
 151                .bit_depth = { 16 },
 152                .planes   = 1,
 153                .buffers = 1,
 154                .alpha_mask = 0x8000,
 155        },
 156        {
 157                .fourcc   = V4L2_PIX_FMT_YUV565, /* uuuvvvvv yyyyyuuu */
 158                .vdownsampling = { 1 },
 159                .bit_depth = { 16 },
 160                .planes   = 1,
 161                .buffers = 1,
 162        },
 163        {
 164                .fourcc   = V4L2_PIX_FMT_YUV444, /* uuuuvvvv aaaayyyy */
 165                .vdownsampling = { 1 },
 166                .bit_depth = { 16 },
 167                .planes   = 1,
 168                .buffers = 1,
 169                .alpha_mask = 0xf000,
 170        },
 171        {
 172                .fourcc   = V4L2_PIX_FMT_YUV32, /* ayuv */
 173                .vdownsampling = { 1 },
 174                .bit_depth = { 32 },
 175                .planes   = 1,
 176                .buffers = 1,
 177                .alpha_mask = 0x000000ff,
 178        },
 179        {
 180                .fourcc   = V4L2_PIX_FMT_AYUV32,
 181                .vdownsampling = { 1 },
 182                .bit_depth = { 32 },
 183                .planes   = 1,
 184                .buffers = 1,
 185                .alpha_mask = 0x000000ff,
 186        },
 187        {
 188                .fourcc   = V4L2_PIX_FMT_XYUV32,
 189                .vdownsampling = { 1 },
 190                .bit_depth = { 32 },
 191                .planes   = 1,
 192                .buffers = 1,
 193        },
 194        {
 195                .fourcc   = V4L2_PIX_FMT_VUYA32,
 196                .vdownsampling = { 1 },
 197                .bit_depth = { 32 },
 198                .planes   = 1,
 199                .buffers = 1,
 200                .alpha_mask = 0xff000000,
 201        },
 202        {
 203                .fourcc   = V4L2_PIX_FMT_VUYX32,
 204                .vdownsampling = { 1 },
 205                .bit_depth = { 32 },
 206                .planes   = 1,
 207                .buffers = 1,
 208        },
 209        {
 210                .fourcc   = V4L2_PIX_FMT_GREY,
 211                .vdownsampling = { 1 },
 212                .bit_depth = { 8 },
 213                .color_enc = TGP_COLOR_ENC_LUMA,
 214                .planes   = 1,
 215                .buffers = 1,
 216        },
 217        {
 218                .fourcc   = V4L2_PIX_FMT_Y10,
 219                .vdownsampling = { 1 },
 220                .bit_depth = { 16 },
 221                .color_enc = TGP_COLOR_ENC_LUMA,
 222                .planes   = 1,
 223                .buffers = 1,
 224        },
 225        {
 226                .fourcc   = V4L2_PIX_FMT_Y12,
 227                .vdownsampling = { 1 },
 228                .bit_depth = { 16 },
 229                .color_enc = TGP_COLOR_ENC_LUMA,
 230                .planes   = 1,
 231                .buffers = 1,
 232        },
 233        {
 234                .fourcc   = V4L2_PIX_FMT_Y16,
 235                .vdownsampling = { 1 },
 236                .bit_depth = { 16 },
 237                .color_enc = TGP_COLOR_ENC_LUMA,
 238                .planes   = 1,
 239                .buffers = 1,
 240        },
 241        {
 242                .fourcc   = V4L2_PIX_FMT_Y16_BE,
 243                .vdownsampling = { 1 },
 244                .bit_depth = { 16 },
 245                .color_enc = TGP_COLOR_ENC_LUMA,
 246                .planes   = 1,
 247                .buffers = 1,
 248        },
 249        {
 250                .fourcc   = V4L2_PIX_FMT_RGB332, /* rrrgggbb */
 251                .vdownsampling = { 1 },
 252                .bit_depth = { 8 },
 253                .planes   = 1,
 254                .buffers = 1,
 255        },
 256        {
 257                .fourcc   = V4L2_PIX_FMT_RGB565, /* gggbbbbb rrrrrggg */
 258                .vdownsampling = { 1 },
 259                .bit_depth = { 16 },
 260                .planes   = 1,
 261                .buffers = 1,
 262                .can_do_overlay = true,
 263        },
 264        {
 265                .fourcc   = V4L2_PIX_FMT_RGB565X, /* rrrrrggg gggbbbbb */
 266                .vdownsampling = { 1 },
 267                .bit_depth = { 16 },
 268                .planes   = 1,
 269                .buffers = 1,
 270                .can_do_overlay = true,
 271        },
 272        {
 273                .fourcc   = V4L2_PIX_FMT_RGB444, /* ggggbbbb xxxxrrrr */
 274                .vdownsampling = { 1 },
 275                .bit_depth = { 16 },
 276                .planes   = 1,
 277                .buffers = 1,
 278        },
 279        {
 280                .fourcc   = V4L2_PIX_FMT_XRGB444, /* ggggbbbb xxxxrrrr */
 281                .vdownsampling = { 1 },
 282                .bit_depth = { 16 },
 283                .planes   = 1,
 284                .buffers = 1,
 285        },
 286        {
 287                .fourcc   = V4L2_PIX_FMT_ARGB444, /* ggggbbbb aaaarrrr */
 288                .vdownsampling = { 1 },
 289                .bit_depth = { 16 },
 290                .planes   = 1,
 291                .buffers = 1,
 292                .alpha_mask = 0x00f0,
 293        },
 294        {
 295                .fourcc   = V4L2_PIX_FMT_RGBX444, /* bbbbxxxx rrrrgggg */
 296                .vdownsampling = { 1 },
 297                .bit_depth = { 16 },
 298                .planes   = 1,
 299                .buffers = 1,
 300        },
 301        {
 302                .fourcc   = V4L2_PIX_FMT_RGBA444, /* bbbbaaaa rrrrgggg */
 303                .vdownsampling = { 1 },
 304                .bit_depth = { 16 },
 305                .planes   = 1,
 306                .buffers = 1,
 307                .alpha_mask = 0x00f0,
 308        },
 309        {
 310                .fourcc   = V4L2_PIX_FMT_XBGR444, /* ggggrrrr xxxxbbbb */
 311                .vdownsampling = { 1 },
 312                .bit_depth = { 16 },
 313                .planes   = 1,
 314                .buffers = 1,
 315        },
 316        {
 317                .fourcc   = V4L2_PIX_FMT_ABGR444, /* ggggrrrr aaaabbbb */
 318                .vdownsampling = { 1 },
 319                .bit_depth = { 16 },
 320                .planes   = 1,
 321                .buffers = 1,
 322                .alpha_mask = 0x00f0,
 323        },
 324        {
 325                .fourcc   = V4L2_PIX_FMT_BGRX444, /* rrrrxxxx bbbbgggg */
 326                .vdownsampling = { 1 },
 327                .bit_depth = { 16 },
 328                .planes   = 1,
 329                .buffers = 1,
 330        },
 331        {
 332                .fourcc   = V4L2_PIX_FMT_BGRA444, /* rrrraaaa bbbbgggg  */
 333                .vdownsampling = { 1 },
 334                .bit_depth = { 16 },
 335                .planes   = 1,
 336                .buffers = 1,
 337                .alpha_mask = 0x00f0,
 338        },
 339        {
 340                .fourcc   = V4L2_PIX_FMT_RGB555, /* gggbbbbb xrrrrrgg */
 341                .vdownsampling = { 1 },
 342                .bit_depth = { 16 },
 343                .planes   = 1,
 344                .buffers = 1,
 345                .can_do_overlay = true,
 346        },
 347        {
 348                .fourcc   = V4L2_PIX_FMT_XRGB555, /* gggbbbbb xrrrrrgg */
 349                .vdownsampling = { 1 },
 350                .bit_depth = { 16 },
 351                .planes   = 1,
 352                .buffers = 1,
 353                .can_do_overlay = true,
 354        },
 355        {
 356                .fourcc   = V4L2_PIX_FMT_ARGB555, /* gggbbbbb arrrrrgg */
 357                .vdownsampling = { 1 },
 358                .bit_depth = { 16 },
 359                .planes   = 1,
 360                .buffers = 1,
 361                .can_do_overlay = true,
 362                .alpha_mask = 0x8000,
 363        },
 364        {
 365                .fourcc   = V4L2_PIX_FMT_RGBX555, /* ggbbbbbx rrrrrggg */
 366                .vdownsampling = { 1 },
 367                .bit_depth = { 16 },
 368                .planes   = 1,
 369                .buffers = 1,
 370                .can_do_overlay = true,
 371        },
 372        {
 373                .fourcc   = V4L2_PIX_FMT_RGBA555, /* ggbbbbba rrrrrggg */
 374                .vdownsampling = { 1 },
 375                .bit_depth = { 16 },
 376                .planes   = 1,
 377                .buffers = 1,
 378                .can_do_overlay = true,
 379                .alpha_mask = 0x8000,
 380        },
 381        {
 382                .fourcc   = V4L2_PIX_FMT_XBGR555, /* gggrrrrr xbbbbbgg */
 383                .vdownsampling = { 1 },
 384                .bit_depth = { 16 },
 385                .planes   = 1,
 386                .buffers = 1,
 387                .can_do_overlay = true,
 388        },
 389        {
 390                .fourcc   = V4L2_PIX_FMT_ABGR555, /* gggrrrrr abbbbbgg */
 391                .vdownsampling = { 1 },
 392                .bit_depth = { 16 },
 393                .planes   = 1,
 394                .buffers = 1,
 395                .can_do_overlay = true,
 396                .alpha_mask = 0x8000,
 397        },
 398        {
 399                .fourcc   = V4L2_PIX_FMT_BGRX555, /* ggrrrrrx bbbbbggg */
 400                .vdownsampling = { 1 },
 401                .bit_depth = { 16 },
 402                .planes   = 1,
 403                .buffers = 1,
 404                .can_do_overlay = true,
 405        },
 406        {
 407                .fourcc   = V4L2_PIX_FMT_BGRA555, /* ggrrrrra bbbbbggg */
 408                .vdownsampling = { 1 },
 409                .bit_depth = { 16 },
 410                .planes   = 1,
 411                .buffers = 1,
 412                .can_do_overlay = true,
 413                .alpha_mask = 0x8000,
 414        },
 415        {
 416                .fourcc   = V4L2_PIX_FMT_RGB555X, /* xrrrrrgg gggbbbbb */
 417                .vdownsampling = { 1 },
 418                .bit_depth = { 16 },
 419                .planes   = 1,
 420                .buffers = 1,
 421        },
 422        {
 423                .fourcc   = V4L2_PIX_FMT_XRGB555X, /* xrrrrrgg gggbbbbb */
 424                .vdownsampling = { 1 },
 425                .bit_depth = { 16 },
 426                .planes   = 1,
 427                .buffers = 1,
 428        },
 429        {
 430                .fourcc   = V4L2_PIX_FMT_ARGB555X, /* arrrrrgg gggbbbbb */
 431                .vdownsampling = { 1 },
 432                .bit_depth = { 16 },
 433                .planes   = 1,
 434                .buffers = 1,
 435                .alpha_mask = 0x0080,
 436        },
 437        {
 438                .fourcc   = V4L2_PIX_FMT_RGB24, /* rgb */
 439                .vdownsampling = { 1 },
 440                .bit_depth = { 24 },
 441                .planes   = 1,
 442                .buffers = 1,
 443        },
 444        {
 445                .fourcc   = V4L2_PIX_FMT_BGR24, /* bgr */
 446                .vdownsampling = { 1 },
 447                .bit_depth = { 24 },
 448                .planes   = 1,
 449                .buffers = 1,
 450        },
 451        {
 452                .fourcc   = V4L2_PIX_FMT_BGR666, /* bbbbbbgg ggggrrrr rrxxxxxx */
 453                .vdownsampling = { 1 },
 454                .bit_depth = { 32 },
 455                .planes   = 1,
 456                .buffers = 1,
 457        },
 458        {
 459                .fourcc   = V4L2_PIX_FMT_RGB32, /* xrgb */
 460                .vdownsampling = { 1 },
 461                .bit_depth = { 32 },
 462                .planes   = 1,
 463                .buffers = 1,
 464        },
 465        {
 466                .fourcc   = V4L2_PIX_FMT_BGR32, /* bgrx */
 467                .vdownsampling = { 1 },
 468                .bit_depth = { 32 },
 469                .planes   = 1,
 470                .buffers = 1,
 471        },
 472        {
 473                .fourcc   = V4L2_PIX_FMT_XRGB32, /* xrgb */
 474                .vdownsampling = { 1 },
 475                .bit_depth = { 32 },
 476                .planes   = 1,
 477                .buffers = 1,
 478        },
 479        {
 480                .fourcc   = V4L2_PIX_FMT_XBGR32, /* bgrx */
 481                .vdownsampling = { 1 },
 482                .bit_depth = { 32 },
 483                .planes   = 1,
 484                .buffers = 1,
 485        },
 486        {
 487                .fourcc   = V4L2_PIX_FMT_ARGB32, /* argb */
 488                .vdownsampling = { 1 },
 489                .bit_depth = { 32 },
 490                .planes   = 1,
 491                .buffers = 1,
 492                .alpha_mask = 0x000000ff,
 493        },
 494        {
 495                .fourcc   = V4L2_PIX_FMT_ABGR32, /* bgra */
 496                .vdownsampling = { 1 },
 497                .bit_depth = { 32 },
 498                .planes   = 1,
 499                .buffers = 1,
 500                .alpha_mask = 0xff000000,
 501        },
 502        {
 503                .fourcc   = V4L2_PIX_FMT_RGBX32, /* rgbx */
 504                .vdownsampling = { 1 },
 505                .bit_depth = { 32 },
 506                .planes   = 1,
 507                .buffers = 1,
 508        },
 509        {
 510                .fourcc   = V4L2_PIX_FMT_BGRX32, /* xbgr */
 511                .vdownsampling = { 1 },
 512                .bit_depth = { 32 },
 513                .planes   = 1,
 514                .buffers = 1,
 515        },
 516        {
 517                .fourcc   = V4L2_PIX_FMT_RGBA32, /* rgba */
 518                .vdownsampling = { 1 },
 519                .bit_depth = { 32 },
 520                .planes   = 1,
 521                .buffers = 1,
 522                .alpha_mask = 0x000000ff,
 523        },
 524        {
 525                .fourcc   = V4L2_PIX_FMT_BGRA32, /* abgr */
 526                .vdownsampling = { 1 },
 527                .bit_depth = { 32 },
 528                .planes   = 1,
 529                .buffers = 1,
 530                .alpha_mask = 0xff000000,
 531        },
 532        {
 533                .fourcc   = V4L2_PIX_FMT_SBGGR8, /* Bayer BG/GR */
 534                .vdownsampling = { 1 },
 535                .bit_depth = { 8 },
 536                .planes   = 1,
 537                .buffers = 1,
 538        },
 539        {
 540                .fourcc   = V4L2_PIX_FMT_SGBRG8, /* Bayer GB/RG */
 541                .vdownsampling = { 1 },
 542                .bit_depth = { 8 },
 543                .planes   = 1,
 544                .buffers = 1,
 545        },
 546        {
 547                .fourcc   = V4L2_PIX_FMT_SGRBG8, /* Bayer GR/BG */
 548                .vdownsampling = { 1 },
 549                .bit_depth = { 8 },
 550                .planes   = 1,
 551                .buffers = 1,
 552        },
 553        {
 554                .fourcc   = V4L2_PIX_FMT_SRGGB8, /* Bayer RG/GB */
 555                .vdownsampling = { 1 },
 556                .bit_depth = { 8 },
 557                .planes   = 1,
 558                .buffers = 1,
 559        },
 560        {
 561                .fourcc   = V4L2_PIX_FMT_SBGGR10, /* Bayer BG/GR */
 562                .vdownsampling = { 1 },
 563                .bit_depth = { 16 },
 564                .planes   = 1,
 565                .buffers = 1,
 566        },
 567        {
 568                .fourcc   = V4L2_PIX_FMT_SGBRG10, /* Bayer GB/RG */
 569                .vdownsampling = { 1 },
 570                .bit_depth = { 16 },
 571                .planes   = 1,
 572                .buffers = 1,
 573        },
 574        {
 575                .fourcc   = V4L2_PIX_FMT_SGRBG10, /* Bayer GR/BG */
 576                .vdownsampling = { 1 },
 577                .bit_depth = { 16 },
 578                .planes   = 1,
 579                .buffers = 1,
 580        },
 581        {
 582                .fourcc   = V4L2_PIX_FMT_SRGGB10, /* Bayer RG/GB */
 583                .vdownsampling = { 1 },
 584                .bit_depth = { 16 },
 585                .planes   = 1,
 586                .buffers = 1,
 587        },
 588        {
 589                .fourcc   = V4L2_PIX_FMT_SBGGR12, /* Bayer BG/GR */
 590                .vdownsampling = { 1 },
 591                .bit_depth = { 16 },
 592                .planes   = 1,
 593                .buffers = 1,
 594        },
 595        {
 596                .fourcc   = V4L2_PIX_FMT_SGBRG12, /* Bayer GB/RG */
 597                .vdownsampling = { 1 },
 598                .bit_depth = { 16 },
 599                .planes   = 1,
 600                .buffers = 1,
 601        },
 602        {
 603                .fourcc   = V4L2_PIX_FMT_SGRBG12, /* Bayer GR/BG */
 604                .vdownsampling = { 1 },
 605                .bit_depth = { 16 },
 606                .planes   = 1,
 607                .buffers = 1,
 608        },
 609        {
 610                .fourcc   = V4L2_PIX_FMT_SRGGB12, /* Bayer RG/GB */
 611                .vdownsampling = { 1 },
 612                .bit_depth = { 16 },
 613                .planes   = 1,
 614                .buffers = 1,
 615        },
 616        {
 617                .fourcc   = V4L2_PIX_FMT_SBGGR16, /* Bayer BG/GR */
 618                .vdownsampling = { 1 },
 619                .bit_depth = { 16 },
 620                .planes   = 1,
 621                .buffers = 1,
 622        },
 623        {
 624                .fourcc   = V4L2_PIX_FMT_SGBRG16, /* Bayer GB/RG */
 625                .vdownsampling = { 1 },
 626                .bit_depth = { 16 },
 627                .planes   = 1,
 628                .buffers = 1,
 629        },
 630        {
 631                .fourcc   = V4L2_PIX_FMT_SGRBG16, /* Bayer GR/BG */
 632                .vdownsampling = { 1 },
 633                .bit_depth = { 16 },
 634                .planes   = 1,
 635                .buffers = 1,
 636        },
 637        {
 638                .fourcc   = V4L2_PIX_FMT_SRGGB16, /* Bayer RG/GB */
 639                .vdownsampling = { 1 },
 640                .bit_depth = { 16 },
 641                .planes   = 1,
 642                .buffers = 1,
 643        },
 644        {
 645                .fourcc   = V4L2_PIX_FMT_HSV24, /* HSV 24bits */
 646                .color_enc = TGP_COLOR_ENC_HSV,
 647                .vdownsampling = { 1 },
 648                .bit_depth = { 24 },
 649                .planes   = 1,
 650                .buffers = 1,
 651        },
 652        {
 653                .fourcc   = V4L2_PIX_FMT_HSV32, /* HSV 32bits */
 654                .color_enc = TGP_COLOR_ENC_HSV,
 655                .vdownsampling = { 1 },
 656                .bit_depth = { 32 },
 657                .planes   = 1,
 658                .buffers = 1,
 659        },
 660
 661        /* Multiplanar formats */
 662
 663        {
 664                .fourcc   = V4L2_PIX_FMT_NV16M,
 665                .vdownsampling = { 1, 1 },
 666                .bit_depth = { 8, 8 },
 667                .color_enc = TGP_COLOR_ENC_YCBCR,
 668                .planes   = 2,
 669                .buffers = 2,
 670                .data_offset = { PLANE0_DATA_OFFSET, 0 },
 671        },
 672        {
 673                .fourcc   = V4L2_PIX_FMT_NV61M,
 674                .vdownsampling = { 1, 1 },
 675                .bit_depth = { 8, 8 },
 676                .color_enc = TGP_COLOR_ENC_YCBCR,
 677                .planes   = 2,
 678                .buffers = 2,
 679                .data_offset = { 0, PLANE0_DATA_OFFSET },
 680        },
 681        {
 682                .fourcc   = V4L2_PIX_FMT_YUV420M,
 683                .vdownsampling = { 1, 2, 2 },
 684                .bit_depth = { 8, 4, 4 },
 685                .color_enc = TGP_COLOR_ENC_YCBCR,
 686                .planes   = 3,
 687                .buffers = 3,
 688        },
 689        {
 690                .fourcc   = V4L2_PIX_FMT_YVU420M,
 691                .vdownsampling = { 1, 2, 2 },
 692                .bit_depth = { 8, 4, 4 },
 693                .color_enc = TGP_COLOR_ENC_YCBCR,
 694                .planes   = 3,
 695                .buffers = 3,
 696        },
 697        {
 698                .fourcc   = V4L2_PIX_FMT_NV12M,
 699                .vdownsampling = { 1, 2 },
 700                .bit_depth = { 8, 8 },
 701                .color_enc = TGP_COLOR_ENC_YCBCR,
 702                .planes   = 2,
 703                .buffers = 2,
 704        },
 705        {
 706                .fourcc   = V4L2_PIX_FMT_NV21M,
 707                .vdownsampling = { 1, 2 },
 708                .bit_depth = { 8, 8 },
 709                .color_enc = TGP_COLOR_ENC_YCBCR,
 710                .planes   = 2,
 711                .buffers = 2,
 712        },
 713        {
 714                .fourcc   = V4L2_PIX_FMT_YUV422M,
 715                .vdownsampling = { 1, 1, 1 },
 716                .bit_depth = { 8, 4, 4 },
 717                .color_enc = TGP_COLOR_ENC_YCBCR,
 718                .planes   = 3,
 719                .buffers = 3,
 720        },
 721        {
 722                .fourcc   = V4L2_PIX_FMT_YVU422M,
 723                .vdownsampling = { 1, 1, 1 },
 724                .bit_depth = { 8, 4, 4 },
 725                .color_enc = TGP_COLOR_ENC_YCBCR,
 726                .planes   = 3,
 727                .buffers = 3,
 728        },
 729        {
 730                .fourcc   = V4L2_PIX_FMT_YUV444M,
 731                .vdownsampling = { 1, 1, 1 },
 732                .bit_depth = { 8, 8, 8 },
 733                .color_enc = TGP_COLOR_ENC_YCBCR,
 734                .planes   = 3,
 735                .buffers = 3,
 736        },
 737        {
 738                .fourcc   = V4L2_PIX_FMT_YVU444M,
 739                .vdownsampling = { 1, 1, 1 },
 740                .bit_depth = { 8, 8, 8 },
 741                .color_enc = TGP_COLOR_ENC_YCBCR,
 742                .planes   = 3,
 743                .buffers = 3,
 744        },
 745};
 746
 747/* There are this many multiplanar formats in the list */
 748#define VIVID_MPLANAR_FORMATS 10
 749
 750const struct vivid_fmt *vivid_get_format(struct vivid_dev *dev, u32 pixelformat)
 751{
 752        const struct vivid_fmt *fmt;
 753        unsigned k;
 754
 755        for (k = 0; k < ARRAY_SIZE(vivid_formats); k++) {
 756                fmt = &vivid_formats[k];
 757                if (fmt->fourcc == pixelformat)
 758                        if (fmt->buffers == 1 || dev->multiplanar)
 759                                return fmt;
 760        }
 761
 762        return NULL;
 763}
 764
 765bool vivid_vid_can_loop(struct vivid_dev *dev)
 766{
 767        if (dev->src_rect.width != dev->sink_rect.width ||
 768            dev->src_rect.height != dev->sink_rect.height)
 769                return false;
 770        if (dev->fmt_cap->fourcc != dev->fmt_out->fourcc)
 771                return false;
 772        if (dev->field_cap != dev->field_out)
 773                return false;
 774        /*
 775         * While this can be supported, it is just too much work
 776         * to actually implement.
 777         */
 778        if (dev->field_cap == V4L2_FIELD_SEQ_TB ||
 779            dev->field_cap == V4L2_FIELD_SEQ_BT)
 780                return false;
 781        if (vivid_is_svid_cap(dev) && vivid_is_svid_out(dev)) {
 782                if (!(dev->std_cap[dev->input] & V4L2_STD_525_60) !=
 783                    !(dev->std_out & V4L2_STD_525_60))
 784                        return false;
 785                return true;
 786        }
 787        if (vivid_is_hdmi_cap(dev) && vivid_is_hdmi_out(dev))
 788                return true;
 789        return false;
 790}
 791
 792void vivid_send_source_change(struct vivid_dev *dev, unsigned type)
 793{
 794        struct v4l2_event ev = {
 795                .type = V4L2_EVENT_SOURCE_CHANGE,
 796                .u.src_change.changes = V4L2_EVENT_SRC_CH_RESOLUTION,
 797        };
 798        unsigned i;
 799
 800        for (i = 0; i < dev->num_inputs; i++) {
 801                ev.id = i;
 802                if (dev->input_type[i] == type) {
 803                        if (video_is_registered(&dev->vid_cap_dev) && dev->has_vid_cap)
 804                                v4l2_event_queue(&dev->vid_cap_dev, &ev);
 805                        if (video_is_registered(&dev->vbi_cap_dev) && dev->has_vbi_cap)
 806                                v4l2_event_queue(&dev->vbi_cap_dev, &ev);
 807                }
 808        }
 809}
 810
 811/*
 812 * Conversion function that converts a single-planar format to a
 813 * single-plane multiplanar format.
 814 */
 815void fmt_sp2mp(const struct v4l2_format *sp_fmt, struct v4l2_format *mp_fmt)
 816{
 817        struct v4l2_pix_format_mplane *mp = &mp_fmt->fmt.pix_mp;
 818        struct v4l2_plane_pix_format *ppix = &mp->plane_fmt[0];
 819        const struct v4l2_pix_format *pix = &sp_fmt->fmt.pix;
 820        bool is_out = sp_fmt->type == V4L2_BUF_TYPE_VIDEO_OUTPUT;
 821
 822        memset(mp->reserved, 0, sizeof(mp->reserved));
 823        mp_fmt->type = is_out ? V4L2_BUF_TYPE_VIDEO_OUTPUT_MPLANE :
 824                           V4L2_BUF_TYPE_VIDEO_CAPTURE_MPLANE;
 825        mp->width = pix->width;
 826        mp->height = pix->height;
 827        mp->pixelformat = pix->pixelformat;
 828        mp->field = pix->field;
 829        mp->colorspace = pix->colorspace;
 830        mp->xfer_func = pix->xfer_func;
 831        /* Also copies hsv_enc */
 832        mp->ycbcr_enc = pix->ycbcr_enc;
 833        mp->quantization = pix->quantization;
 834        mp->num_planes = 1;
 835        mp->flags = pix->flags;
 836        ppix->sizeimage = pix->sizeimage;
 837        ppix->bytesperline = pix->bytesperline;
 838        memset(ppix->reserved, 0, sizeof(ppix->reserved));
 839}
 840
 841int fmt_sp2mp_func(struct file *file, void *priv,
 842                struct v4l2_format *f, fmtfunc func)
 843{
 844        struct v4l2_format fmt;
 845        struct v4l2_pix_format_mplane *mp = &fmt.fmt.pix_mp;
 846        struct v4l2_plane_pix_format *ppix = &mp->plane_fmt[0];
 847        struct v4l2_pix_format *pix = &f->fmt.pix;
 848        int ret;
 849
 850        /* Converts to a mplane format */
 851        fmt_sp2mp(f, &fmt);
 852        /* Passes it to the generic mplane format function */
 853        ret = func(file, priv, &fmt);
 854        /* Copies back the mplane data to the single plane format */
 855        pix->width = mp->width;
 856        pix->height = mp->height;
 857        pix->pixelformat = mp->pixelformat;
 858        pix->field = mp->field;
 859        pix->colorspace = mp->colorspace;
 860        pix->xfer_func = mp->xfer_func;
 861        /* Also copies hsv_enc */
 862        pix->ycbcr_enc = mp->ycbcr_enc;
 863        pix->quantization = mp->quantization;
 864        pix->sizeimage = ppix->sizeimage;
 865        pix->bytesperline = ppix->bytesperline;
 866        pix->flags = mp->flags;
 867        return ret;
 868}
 869
 870int vivid_vid_adjust_sel(unsigned flags, struct v4l2_rect *r)
 871{
 872        unsigned w = r->width;
 873        unsigned h = r->height;
 874
 875        /* sanitize w and h in case someone passes ~0 as the value */
 876        w &= 0xffff;
 877        h &= 0xffff;
 878        if (!(flags & V4L2_SEL_FLAG_LE)) {
 879                w++;
 880                h++;
 881                if (w < 2)
 882                        w = 2;
 883                if (h < 2)
 884                        h = 2;
 885        }
 886        if (!(flags & V4L2_SEL_FLAG_GE)) {
 887                if (w > MAX_WIDTH)
 888                        w = MAX_WIDTH;
 889                if (h > MAX_HEIGHT)
 890                        h = MAX_HEIGHT;
 891        }
 892        w = w & ~1;
 893        h = h & ~1;
 894        if (w < 2 || h < 2)
 895                return -ERANGE;
 896        if (w > MAX_WIDTH || h > MAX_HEIGHT)
 897                return -ERANGE;
 898        if (r->top < 0)
 899                r->top = 0;
 900        if (r->left < 0)
 901                r->left = 0;
 902        /* sanitize left and top in case someone passes ~0 as the value */
 903        r->left &= 0xfffe;
 904        r->top &= 0xfffe;
 905        if (r->left + w > MAX_WIDTH)
 906                r->left = MAX_WIDTH - w;
 907        if (r->top + h > MAX_HEIGHT)
 908                r->top = MAX_HEIGHT - h;
 909        if ((flags & (V4L2_SEL_FLAG_GE | V4L2_SEL_FLAG_LE)) ==
 910                        (V4L2_SEL_FLAG_GE | V4L2_SEL_FLAG_LE) &&
 911            (r->width != w || r->height != h))
 912                return -ERANGE;
 913        r->width = w;
 914        r->height = h;
 915        return 0;
 916}
 917
 918int vivid_enum_fmt_vid(struct file *file, void  *priv,
 919                                        struct v4l2_fmtdesc *f)
 920{
 921        struct vivid_dev *dev = video_drvdata(file);
 922        const struct vivid_fmt *fmt;
 923
 924        if (f->index >= ARRAY_SIZE(vivid_formats) -
 925            (dev->multiplanar ? 0 : VIVID_MPLANAR_FORMATS))
 926                return -EINVAL;
 927
 928        fmt = &vivid_formats[f->index];
 929
 930        f->pixelformat = fmt->fourcc;
 931
 932        if (f->type != V4L2_BUF_TYPE_VIDEO_CAPTURE &&
 933            f->type != V4L2_BUF_TYPE_VIDEO_CAPTURE_MPLANE)
 934                return 0;
 935        /*
 936         * For capture devices, we support the CSC API.
 937         * We allow userspace to:
 938         * 1. set the colorspace
 939         * 2. set the xfer_func
 940         * 3. set the ycbcr_enc on YUV formats
 941         * 4. set the hsv_enc on HSV formats
 942         * 5. set the quantization on YUV and RGB formats
 943         */
 944        f->flags |= V4L2_FMT_FLAG_CSC_COLORSPACE;
 945        f->flags |= V4L2_FMT_FLAG_CSC_XFER_FUNC;
 946
 947        if (fmt->color_enc == TGP_COLOR_ENC_YCBCR) {
 948                f->flags |= V4L2_FMT_FLAG_CSC_YCBCR_ENC;
 949                f->flags |= V4L2_FMT_FLAG_CSC_QUANTIZATION;
 950        } else if (fmt->color_enc == TGP_COLOR_ENC_HSV) {
 951                f->flags |= V4L2_FMT_FLAG_CSC_HSV_ENC;
 952        } else if (fmt->color_enc == TGP_COLOR_ENC_RGB) {
 953                f->flags |= V4L2_FMT_FLAG_CSC_QUANTIZATION;
 954        }
 955
 956        return 0;
 957}
 958
 959int vidioc_g_std(struct file *file, void *priv, v4l2_std_id *id)
 960{
 961        struct vivid_dev *dev = video_drvdata(file);
 962        struct video_device *vdev = video_devdata(file);
 963
 964        if (vdev->vfl_dir == VFL_DIR_RX) {
 965                if (!vivid_is_sdtv_cap(dev))
 966                        return -ENODATA;
 967                *id = dev->std_cap[dev->input];
 968        } else {
 969                if (!vivid_is_svid_out(dev))
 970                        return -ENODATA;
 971                *id = dev->std_out;
 972        }
 973        return 0;
 974}
 975
 976int vidioc_g_dv_timings(struct file *file, void *_fh,
 977                                    struct v4l2_dv_timings *timings)
 978{
 979        struct vivid_dev *dev = video_drvdata(file);
 980        struct video_device *vdev = video_devdata(file);
 981
 982        if (vdev->vfl_dir == VFL_DIR_RX) {
 983                if (!vivid_is_hdmi_cap(dev))
 984                        return -ENODATA;
 985                *timings = dev->dv_timings_cap[dev->input];
 986        } else {
 987                if (!vivid_is_hdmi_out(dev))
 988                        return -ENODATA;
 989                *timings = dev->dv_timings_out;
 990        }
 991        return 0;
 992}
 993
 994int vidioc_enum_dv_timings(struct file *file, void *_fh,
 995                                    struct v4l2_enum_dv_timings *timings)
 996{
 997        struct vivid_dev *dev = video_drvdata(file);
 998        struct video_device *vdev = video_devdata(file);
 999
1000        if (vdev->vfl_dir == VFL_DIR_RX) {
1001                if (!vivid_is_hdmi_cap(dev))
1002                        return -ENODATA;
1003        } else {
1004                if (!vivid_is_hdmi_out(dev))
1005                        return -ENODATA;
1006        }
1007        return v4l2_enum_dv_timings_cap(timings, &vivid_dv_timings_cap,
1008                        NULL, NULL);
1009}
1010
1011int vidioc_dv_timings_cap(struct file *file, void *_fh,
1012                                    struct v4l2_dv_timings_cap *cap)
1013{
1014        struct vivid_dev *dev = video_drvdata(file);
1015        struct video_device *vdev = video_devdata(file);
1016
1017        if (vdev->vfl_dir == VFL_DIR_RX) {
1018                if (!vivid_is_hdmi_cap(dev))
1019                        return -ENODATA;
1020        } else {
1021                if (!vivid_is_hdmi_out(dev))
1022                        return -ENODATA;
1023        }
1024        *cap = vivid_dv_timings_cap;
1025        return 0;
1026}
1027
1028int vidioc_g_edid(struct file *file, void *_fh,
1029                         struct v4l2_edid *edid)
1030{
1031        struct vivid_dev *dev = video_drvdata(file);
1032        struct video_device *vdev = video_devdata(file);
1033        struct cec_adapter *adap;
1034
1035        memset(edid->reserved, 0, sizeof(edid->reserved));
1036        if (vdev->vfl_dir == VFL_DIR_RX) {
1037                if (edid->pad >= dev->num_inputs)
1038                        return -EINVAL;
1039                if (dev->input_type[edid->pad] != HDMI)
1040                        return -EINVAL;
1041                adap = dev->cec_rx_adap;
1042        } else {
1043                unsigned int bus_idx;
1044
1045                if (edid->pad >= dev->num_outputs)
1046                        return -EINVAL;
1047                if (dev->output_type[edid->pad] != HDMI)
1048                        return -EINVAL;
1049                if (!dev->display_present[edid->pad])
1050                        return -ENODATA;
1051                bus_idx = dev->cec_output2bus_map[edid->pad];
1052                adap = dev->cec_tx_adap[bus_idx];
1053        }
1054        if (edid->start_block == 0 && edid->blocks == 0) {
1055                edid->blocks = dev->edid_blocks;
1056                return 0;
1057        }
1058        if (dev->edid_blocks == 0)
1059                return -ENODATA;
1060        if (edid->start_block >= dev->edid_blocks)
1061                return -EINVAL;
1062        if (edid->blocks > dev->edid_blocks - edid->start_block)
1063                edid->blocks = dev->edid_blocks - edid->start_block;
1064        if (adap)
1065                v4l2_set_edid_phys_addr(dev->edid, dev->edid_blocks * 128, adap->phys_addr);
1066        memcpy(edid->edid, dev->edid + edid->start_block * 128, edid->blocks * 128);
1067        return 0;
1068}
1069