linux/drivers/staging/media/atomisp/pci/atomisp2/atomisp_ioctl.c
<<
>>
Prefs
   1/*
   2 * Support for Medifield PNW Camera Imaging ISP subsystem.
   3 *
   4 * Copyright (c) 2010 Intel Corporation. All Rights Reserved.
   5 *
   6 * Copyright (c) 2010 Silicon Hive www.siliconhive.com.
   7 *
   8 * This program is free software; you can redistribute it and/or
   9 * modify it under the terms of the GNU General Public License version
  10 * 2 as published by the Free Software Foundation.
  11 *
  12 * This program is distributed in the hope that it will be useful,
  13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
  14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  15 * GNU General Public License for more details.
  16 *
  17 *
  18 */
  19
  20#include <linux/delay.h>
  21#include <linux/pci.h>
  22
  23
  24#include <media/v4l2-ioctl.h>
  25#include <media/v4l2-event.h>
  26#include <media/videobuf-vmalloc.h>
  27
  28#include "atomisp_acc.h"
  29#include "atomisp_cmd.h"
  30#include "atomisp_common.h"
  31#include "atomisp_fops.h"
  32#include "atomisp_internal.h"
  33#include "atomisp_ioctl.h"
  34#include "atomisp-regs.h"
  35#include "atomisp_compat.h"
  36
  37#include "sh_css_hrt.h"
  38
  39#include "gp_device.h"
  40#include "device_access.h"
  41#include "irq.h"
  42
  43#include "hrt/hive_isp_css_mm_hrt.h"
  44
  45/* for v4l2_capability */
  46static const char *DRIVER = "atomisp";  /* max size 15 */
  47static const char *CARD = "ATOM ISP";   /* max size 31 */
  48static const char *BUS_INFO = "PCI-3";  /* max size 31 */
  49
  50/*
  51 * FIXME: ISP should not know beforehand all CIDs supported by sensor.
  52 * Instead, it needs to propagate to sensor unkonwn CIDs.
  53 */
  54static struct v4l2_queryctrl ci_v4l2_controls[] = {
  55        {
  56                .id = V4L2_CID_AUTO_WHITE_BALANCE,
  57                .type = V4L2_CTRL_TYPE_BOOLEAN,
  58                .name = "Automatic White Balance",
  59                .minimum = 0,
  60                .maximum = 1,
  61                .step = 1,
  62                .default_value = 0,
  63        },
  64        {
  65                .id = V4L2_CID_RED_BALANCE,
  66                .type = V4L2_CTRL_TYPE_INTEGER,
  67                .name = "Red Balance",
  68                .minimum = 0x00,
  69                .maximum = 0xff,
  70                .step = 1,
  71                .default_value = 0x00,
  72        },
  73        {
  74                .id = V4L2_CID_BLUE_BALANCE,
  75                .type = V4L2_CTRL_TYPE_INTEGER,
  76                .name = "Blue Balance",
  77                .minimum = 0x00,
  78                .maximum = 0xff,
  79                .step = 1,
  80                .default_value = 0x00,
  81        },
  82        {
  83                .id = V4L2_CID_GAMMA,
  84                .type = V4L2_CTRL_TYPE_INTEGER,
  85                .name = "Gamma",
  86                .minimum = 0x00,
  87                .maximum = 0xff,
  88                .step = 1,
  89                .default_value = 0x00,
  90        },
  91        {
  92                .id = V4L2_CID_POWER_LINE_FREQUENCY,
  93                .type = V4L2_CTRL_TYPE_MENU,
  94                .name = "Light frequency filter",
  95                .minimum = 1,
  96                .maximum = 2,
  97                .step = 1,
  98                .default_value = 1,
  99        },
 100        {
 101                .id = V4L2_CID_COLORFX,
 102                .type = V4L2_CTRL_TYPE_INTEGER,
 103                .name = "Image Color Effect",
 104                .minimum = 0,
 105                .maximum = 9,
 106                .step = 1,
 107                .default_value = 0,
 108        },
 109        {
 110                .id = V4L2_CID_COLORFX_CBCR,
 111                .type = V4L2_CTRL_TYPE_INTEGER,
 112                .name = "Image Color Effect CbCr",
 113                .minimum = 0,
 114                .maximum = 0xffff,
 115                .step = 1,
 116                .default_value = 0,
 117        },
 118        {
 119                .id = V4L2_CID_ATOMISP_BAD_PIXEL_DETECTION,
 120                .type = V4L2_CTRL_TYPE_INTEGER,
 121                .name = "Bad Pixel Correction",
 122                .minimum = 0,
 123                .maximum = 1,
 124                .step = 1,
 125                .default_value = 0,
 126        },
 127        {
 128                .id = V4L2_CID_ATOMISP_POSTPROCESS_GDC_CAC,
 129                .type = V4L2_CTRL_TYPE_INTEGER,
 130                .name = "GDC/CAC",
 131                .minimum = 0,
 132                .maximum = 1,
 133                .step = 1,
 134                .default_value = 0,
 135        },
 136        {
 137                .id = V4L2_CID_ATOMISP_VIDEO_STABLIZATION,
 138                .type = V4L2_CTRL_TYPE_INTEGER,
 139                .name = "Video Stablization",
 140                .minimum = 0,
 141                .maximum = 1,
 142                .step = 1,
 143                .default_value = 0,
 144        },
 145        {
 146                .id = V4L2_CID_ATOMISP_FIXED_PATTERN_NR,
 147                .type = V4L2_CTRL_TYPE_INTEGER,
 148                .name = "Fixed Pattern Noise Reduction",
 149                .minimum = 0,
 150                .maximum = 1,
 151                .step = 1,
 152                .default_value = 0,
 153        },
 154        {
 155                .id = V4L2_CID_ATOMISP_FALSE_COLOR_CORRECTION,
 156                .type = V4L2_CTRL_TYPE_INTEGER,
 157                .name = "False Color Correction",
 158                .minimum = 0,
 159                .maximum = 1,
 160                .step = 1,
 161                .default_value = 0,
 162        },
 163        {
 164                .id = V4L2_CID_REQUEST_FLASH,
 165                .type = V4L2_CTRL_TYPE_INTEGER,
 166                .name = "Request flash frames",
 167                .minimum = 0,
 168                .maximum = 10,
 169                .step = 1,
 170                .default_value = 1,
 171        },
 172        {
 173                .id = V4L2_CID_ATOMISP_LOW_LIGHT,
 174                .type = V4L2_CTRL_TYPE_BOOLEAN,
 175                .name = "Low light mode",
 176                .minimum = 0,
 177                .maximum = 1,
 178                .step = 1,
 179                .default_value = 1,
 180        },
 181        {
 182                .id = V4L2_CID_BIN_FACTOR_HORZ,
 183                .type = V4L2_CTRL_TYPE_INTEGER,
 184                .name = "Horizontal binning factor",
 185                .minimum = 0,
 186                .maximum = 10,
 187                .step = 1,
 188                .default_value = 0,
 189        },
 190        {
 191                .id = V4L2_CID_BIN_FACTOR_VERT,
 192                .type = V4L2_CTRL_TYPE_INTEGER,
 193                .name = "Vertical binning factor",
 194                .minimum = 0,
 195                .maximum = 10,
 196                .step = 1,
 197                .default_value = 0,
 198        },
 199        {
 200                .id = V4L2_CID_2A_STATUS,
 201                .type = V4L2_CTRL_TYPE_BITMASK,
 202                .name = "AE and AWB status",
 203                .minimum = 0,
 204                .maximum = V4L2_2A_STATUS_AE_READY | V4L2_2A_STATUS_AWB_READY,
 205                .step = 1,
 206                .default_value = 0,
 207        },
 208        {
 209                .id = V4L2_CID_EXPOSURE,
 210                .type = V4L2_CTRL_TYPE_INTEGER,
 211                .name = "exposure",
 212                .minimum = -4,
 213                .maximum = 4,
 214                .step = 1,
 215                .default_value = 0,
 216        },
 217        {
 218                .id = V4L2_CID_EXPOSURE_ZONE_NUM,
 219                .type = V4L2_CTRL_TYPE_INTEGER,
 220                .name = "one-time exposure zone number",
 221                .minimum = 0x0,
 222                .maximum = 0xffff,
 223                .step = 1,
 224                .default_value = 0,
 225        },
 226        {
 227                .id = V4L2_CID_EXPOSURE_AUTO_PRIORITY,
 228                .type = V4L2_CTRL_TYPE_INTEGER,
 229                .name = "Exposure auto priority",
 230                .minimum = V4L2_EXPOSURE_AUTO,
 231                .maximum = V4L2_EXPOSURE_APERTURE_PRIORITY,
 232                .step = 1,
 233                .default_value = V4L2_EXPOSURE_AUTO,
 234        },
 235        {
 236                .id = V4L2_CID_SCENE_MODE,
 237                .type = V4L2_CTRL_TYPE_INTEGER,
 238                .name = "scene mode",
 239                .minimum = 0,
 240                .maximum = 13,
 241                .step = 1,
 242                .default_value = 0,
 243        },
 244        {
 245                .id = V4L2_CID_ISO_SENSITIVITY,
 246                .type = V4L2_CTRL_TYPE_INTEGER,
 247                .name = "iso",
 248                .minimum = -4,
 249                .maximum = 4,
 250                .step = 1,
 251                .default_value = 0,
 252        },
 253        {
 254                .id = V4L2_CID_ISO_SENSITIVITY_AUTO,
 255                .type = V4L2_CTRL_TYPE_INTEGER,
 256                .name = "iso mode",
 257                .minimum = V4L2_ISO_SENSITIVITY_MANUAL,
 258                .maximum = V4L2_ISO_SENSITIVITY_AUTO,
 259                .step = 1,
 260                .default_value = V4L2_ISO_SENSITIVITY_AUTO,
 261        },
 262        {
 263                .id = V4L2_CID_AUTO_N_PRESET_WHITE_BALANCE,
 264                .type = V4L2_CTRL_TYPE_INTEGER,
 265                .name = "white balance",
 266                .minimum = 0,
 267                .maximum = 9,
 268                .step = 1,
 269                .default_value = 0,
 270        },
 271        {
 272                .id = V4L2_CID_EXPOSURE_METERING,
 273                .type = V4L2_CTRL_TYPE_MENU,
 274                .name = "metering",
 275                .minimum = 0,
 276                .maximum = 3,
 277                .step = 1,
 278                .default_value = 1,
 279        },
 280        {
 281                .id = V4L2_CID_3A_LOCK,
 282                .type = V4L2_CTRL_TYPE_BITMASK,
 283                .name = "3a lock",
 284                .minimum = 0,
 285                .maximum = V4L2_LOCK_EXPOSURE | V4L2_LOCK_WHITE_BALANCE
 286                         | V4L2_LOCK_FOCUS,
 287                .step = 1,
 288                .default_value = 0,
 289        },
 290        {
 291                .id = V4L2_CID_TEST_PATTERN,
 292                .type = V4L2_CTRL_TYPE_INTEGER,
 293                .name = "Test Pattern",
 294                .minimum = 0,
 295                .maximum = 0xffff,
 296                .step = 1,
 297                .default_value = 0,
 298        },
 299        {
 300                .id = V4L2_CID_TEST_PATTERN_COLOR_R,
 301                .type = V4L2_CTRL_TYPE_INTEGER,
 302                .name = "Test Pattern Solid Color R",
 303                .minimum = INT_MIN,
 304                .maximum = INT_MAX,
 305                .step = 1,
 306                .default_value = 0,
 307        },
 308        {
 309                .id = V4L2_CID_TEST_PATTERN_COLOR_GR,
 310                .type = V4L2_CTRL_TYPE_INTEGER,
 311                .name = "Test Pattern Solid Color GR",
 312                .minimum = INT_MIN,
 313                .maximum = INT_MAX,
 314                .step = 1,
 315                .default_value = 0,
 316        },
 317        {
 318                .id = V4L2_CID_TEST_PATTERN_COLOR_GB,
 319                .type = V4L2_CTRL_TYPE_INTEGER,
 320                .name = "Test Pattern Solid Color GB",
 321                .minimum = INT_MIN,
 322                .maximum = INT_MAX,
 323                .step = 1,
 324                .default_value = 0,
 325        },
 326        {
 327                .id = V4L2_CID_TEST_PATTERN_COLOR_B,
 328                .type = V4L2_CTRL_TYPE_INTEGER,
 329                .name = "Test Pattern Solid Color B",
 330                .minimum = INT_MIN,
 331                .maximum = INT_MAX,
 332                .step = 1,
 333                .default_value = 0,
 334        },
 335};
 336static const u32 ctrls_num = ARRAY_SIZE(ci_v4l2_controls);
 337
 338/*
 339 * supported V4L2 fmts and resolutions
 340 */
 341const struct atomisp_format_bridge atomisp_output_fmts[] = {
 342        {
 343                .pixelformat = V4L2_PIX_FMT_YUV420,
 344                .depth = 12,
 345                .mbus_code = V4L2_MBUS_FMT_CUSTOM_YUV420,
 346                .sh_fmt = CSS_FRAME_FORMAT_YUV420,
 347                .description = "YUV420, planar",
 348                .planar = true
 349        }, {
 350                .pixelformat = V4L2_PIX_FMT_YVU420,
 351                .depth = 12,
 352                .mbus_code = V4L2_MBUS_FMT_CUSTOM_YVU420,
 353                .sh_fmt = CSS_FRAME_FORMAT_YV12,
 354                .description = "YVU420, planar",
 355                .planar = true
 356        }, {
 357                .pixelformat = V4L2_PIX_FMT_YUV422P,
 358                .depth = 16,
 359                .mbus_code = V4L2_MBUS_FMT_CUSTOM_YUV422P,
 360                .sh_fmt = CSS_FRAME_FORMAT_YUV422,
 361                .description = "YUV422, planar",
 362                .planar = true
 363        }, {
 364                .pixelformat = V4L2_PIX_FMT_YUV444,
 365                .depth = 24,
 366                .mbus_code = V4L2_MBUS_FMT_CUSTOM_YUV444,
 367                .sh_fmt = CSS_FRAME_FORMAT_YUV444,
 368                .description = "YUV444"
 369        }, {
 370                .pixelformat = V4L2_PIX_FMT_NV12,
 371                .depth = 12,
 372                .mbus_code = V4L2_MBUS_FMT_CUSTOM_NV12,
 373                .sh_fmt = CSS_FRAME_FORMAT_NV12,
 374                .description = "NV12, Y-plane, CbCr interleaved",
 375                .planar = true
 376        }, {
 377                .pixelformat = V4L2_PIX_FMT_NV21,
 378                .depth = 12,
 379                .mbus_code = V4L2_MBUS_FMT_CUSTOM_NV21,
 380                .sh_fmt = CSS_FRAME_FORMAT_NV21,
 381                .description = "NV21, Y-plane, CbCr interleaved",
 382                .planar = true
 383        }, {
 384                .pixelformat = V4L2_PIX_FMT_NV16,
 385                .depth = 16,
 386                .mbus_code = V4L2_MBUS_FMT_CUSTOM_NV16,
 387                .sh_fmt = CSS_FRAME_FORMAT_NV16,
 388                .description = "NV16, Y-plane, CbCr interleaved",
 389                .planar = true
 390        }, {
 391                .pixelformat = V4L2_PIX_FMT_YUYV,
 392                .depth = 16,
 393                .mbus_code = V4L2_MBUS_FMT_CUSTOM_YUYV,
 394                .sh_fmt = CSS_FRAME_FORMAT_YUYV,
 395                .description = "YUYV, interleaved"
 396        }, {
 397                .pixelformat = V4L2_PIX_FMT_UYVY,
 398                .depth = 16,
 399                .mbus_code = MEDIA_BUS_FMT_UYVY8_1X16,
 400                .sh_fmt = CSS_FRAME_FORMAT_UYVY,
 401                .description = "UYVY, interleaved"
 402        }, { /* This one is for parallel sensors! DO NOT USE! */
 403                .pixelformat = V4L2_PIX_FMT_UYVY,
 404                .depth = 16,
 405                .mbus_code = MEDIA_BUS_FMT_UYVY8_2X8,
 406                .sh_fmt = CSS_FRAME_FORMAT_UYVY,
 407                .description = "UYVY, interleaved"
 408        }, {
 409                .pixelformat = V4L2_PIX_FMT_SBGGR16,
 410                .depth = 16,
 411                .mbus_code = V4L2_MBUS_FMT_CUSTOM_SBGGR16,
 412                .sh_fmt = CSS_FRAME_FORMAT_RAW,
 413                .description = "Bayer 16"
 414        }, {
 415                .pixelformat = V4L2_PIX_FMT_SBGGR8,
 416                .depth = 8,
 417                .mbus_code = MEDIA_BUS_FMT_SBGGR8_1X8,
 418                .sh_fmt = CSS_FRAME_FORMAT_RAW,
 419                .description = "Bayer 8"
 420        }, {
 421                .pixelformat = V4L2_PIX_FMT_SGBRG8,
 422                .depth = 8,
 423                .mbus_code = MEDIA_BUS_FMT_SGBRG8_1X8,
 424                .sh_fmt = CSS_FRAME_FORMAT_RAW,
 425                .description = "Bayer 8"
 426        }, {
 427                .pixelformat = V4L2_PIX_FMT_SGRBG8,
 428                .depth = 8,
 429                .mbus_code = MEDIA_BUS_FMT_SGRBG8_1X8,
 430                .sh_fmt = CSS_FRAME_FORMAT_RAW,
 431                .description = "Bayer 8"
 432        }, {
 433                .pixelformat = V4L2_PIX_FMT_SRGGB8,
 434                .depth = 8,
 435                .mbus_code = MEDIA_BUS_FMT_SRGGB8_1X8,
 436                .sh_fmt = CSS_FRAME_FORMAT_RAW,
 437                .description = "Bayer 8"
 438        }, {
 439                .pixelformat = V4L2_PIX_FMT_SBGGR10,
 440                .depth = 16,
 441                .mbus_code = MEDIA_BUS_FMT_SBGGR10_1X10,
 442                .sh_fmt = CSS_FRAME_FORMAT_RAW,
 443                .description = "Bayer 10"
 444        }, {
 445                .pixelformat = V4L2_PIX_FMT_SGBRG10,
 446                .depth = 16,
 447                .mbus_code = MEDIA_BUS_FMT_SGBRG10_1X10,
 448                .sh_fmt = CSS_FRAME_FORMAT_RAW,
 449                .description = "Bayer 10"
 450        }, {
 451                .pixelformat = V4L2_PIX_FMT_SGRBG10,
 452                .depth = 16,
 453                .mbus_code = MEDIA_BUS_FMT_SGRBG10_1X10,
 454                .sh_fmt = CSS_FRAME_FORMAT_RAW,
 455                .description = "Bayer 10"
 456        }, {
 457                .pixelformat = V4L2_PIX_FMT_SRGGB10,
 458                .depth = 16,
 459                .mbus_code = MEDIA_BUS_FMT_SRGGB10_1X10,
 460                .sh_fmt = CSS_FRAME_FORMAT_RAW,
 461                .description = "Bayer 10"
 462        }, {
 463                .pixelformat = V4L2_PIX_FMT_SBGGR12,
 464                .depth = 16,
 465                .mbus_code = MEDIA_BUS_FMT_SBGGR12_1X12,
 466                .sh_fmt = CSS_FRAME_FORMAT_RAW,
 467                .description = "Bayer 12"
 468        }, {
 469                .pixelformat = V4L2_PIX_FMT_SGBRG12,
 470                .depth = 16,
 471                .mbus_code = MEDIA_BUS_FMT_SGBRG12_1X12,
 472                .sh_fmt = CSS_FRAME_FORMAT_RAW,
 473                .description = "Bayer 12"
 474        }, {
 475                .pixelformat = V4L2_PIX_FMT_SGRBG12,
 476                .depth = 16,
 477                .mbus_code = MEDIA_BUS_FMT_SGRBG12_1X12,
 478                .sh_fmt = CSS_FRAME_FORMAT_RAW,
 479                .description = "Bayer 12"
 480        }, {
 481                .pixelformat = V4L2_PIX_FMT_SRGGB12,
 482                .depth = 16,
 483                .mbus_code = MEDIA_BUS_FMT_SRGGB12_1X12,
 484                .sh_fmt = CSS_FRAME_FORMAT_RAW,
 485                .description = "Bayer 12"
 486        }, {
 487                .pixelformat = V4L2_PIX_FMT_RGB32,
 488                .depth = 32,
 489                .mbus_code = V4L2_MBUS_FMT_CUSTOM_RGB32,
 490                .sh_fmt = CSS_FRAME_FORMAT_RGBA888,
 491                .description = "32 RGB 8-8-8-8"
 492        }, {
 493                .pixelformat = V4L2_PIX_FMT_RGB565,
 494                .depth = 16,
 495                .mbus_code = MEDIA_BUS_FMT_BGR565_2X8_LE,
 496                .sh_fmt = CSS_FRAME_FORMAT_RGB565,
 497                .description = "16 RGB 5-6-5"
 498        }, {
 499                .pixelformat = V4L2_PIX_FMT_JPEG,
 500                .depth = 8,
 501                .mbus_code = MEDIA_BUS_FMT_JPEG_1X8,
 502                .sh_fmt = CSS_FRAME_FORMAT_BINARY_8,
 503                .description = "JPEG"
 504        },
 505#if 0
 506        {
 507        /* This is a custom format being used by M10MO to send the RAW data */
 508                .pixelformat = V4L2_PIX_FMT_CUSTOM_M10MO_RAW,
 509                .depth = 8,
 510                .mbus_code = V4L2_MBUS_FMT_CUSTOM_M10MO_RAW,
 511                .sh_fmt = CSS_FRAME_FORMAT_BINARY_8,
 512                .description = "Custom RAW for M10MO"
 513        },
 514#endif
 515};
 516
 517const struct atomisp_format_bridge *atomisp_get_format_bridge(
 518        unsigned int pixelformat)
 519{
 520        unsigned int i;
 521
 522        for (i = 0; i < ARRAY_SIZE(atomisp_output_fmts); i++) {
 523                if (atomisp_output_fmts[i].pixelformat == pixelformat)
 524                        return &atomisp_output_fmts[i];
 525        }
 526
 527        return NULL;
 528}
 529
 530const struct atomisp_format_bridge *atomisp_get_format_bridge_from_mbus(
 531        u32 mbus_code)
 532{
 533        unsigned int i;
 534
 535        for (i = 0; i < ARRAY_SIZE(atomisp_output_fmts); i++) {
 536                if (mbus_code == atomisp_output_fmts[i].mbus_code)
 537                        return &atomisp_output_fmts[i];
 538        }
 539
 540        return NULL;
 541}
 542
 543/*
 544 * v4l2 ioctls
 545 * return ISP capabilities
 546 *
 547 * FIXME: capabilities should be different for video0/video2/video3
 548 */
 549static int atomisp_querycap(struct file *file, void *fh,
 550                            struct v4l2_capability *cap)
 551{
 552        memset(cap, 0, sizeof(struct v4l2_capability));
 553
 554        WARN_ON(sizeof(DRIVER) > sizeof(cap->driver) ||
 555                sizeof(CARD) > sizeof(cap->card) ||
 556                sizeof(BUS_INFO) > sizeof(cap->bus_info));
 557
 558        strncpy(cap->driver, DRIVER, sizeof(cap->driver) - 1);
 559        strncpy(cap->card, CARD, sizeof(cap->card) - 1);
 560        strncpy(cap->bus_info, BUS_INFO, sizeof(cap->card) - 1);
 561
 562        cap->device_caps = V4L2_CAP_VIDEO_CAPTURE |
 563            V4L2_CAP_STREAMING | V4L2_CAP_VIDEO_OUTPUT;
 564        cap->capabilities = cap->device_caps | V4L2_CAP_DEVICE_CAPS;
 565        return 0;
 566}
 567
 568/*
 569 * enum input are used to check primary/secondary camera
 570 */
 571static int atomisp_enum_input(struct file *file, void *fh,
 572        struct v4l2_input *input)
 573{
 574        struct video_device *vdev = video_devdata(file);
 575        struct atomisp_device *isp = video_get_drvdata(vdev);
 576        int index = input->index;
 577
 578        if (index >= isp->input_cnt)
 579                return -EINVAL;
 580
 581        if (!isp->inputs[index].camera)
 582                return -EINVAL;
 583
 584        memset(input, 0, sizeof(struct v4l2_input));
 585        strncpy(input->name, isp->inputs[index].camera->name,
 586                sizeof(input->name) - 1);
 587
 588        /*
 589         * HACK: append actuator's name to sensor's
 590         * As currently userspace can't talk directly to subdev nodes, this
 591         * ioctl is the only way to enum inputs + possible external actuators
 592         * for 3A tuning purpose.
 593         */
 594#ifndef ISP2401
 595        if (isp->inputs[index].motor &&
 596            strlen(isp->inputs[index].motor->name) > 0) {
 597#else
 598        if (isp->motor &&
 599            strlen(isp->motor->name) > 0) {
 600#endif
 601                const int cur_len = strlen(input->name);
 602                const int max_size = sizeof(input->name) - cur_len - 1;
 603
 604                if (max_size > 1) {
 605                        input->name[cur_len] = '+';
 606                        strncpy(&input->name[cur_len + 1],
 607#ifndef ISP2401
 608                                isp->inputs[index].motor->name, max_size - 1);
 609#else
 610                                isp->motor->name, max_size - 1);
 611#endif
 612                }
 613        }
 614
 615        input->type = V4L2_INPUT_TYPE_CAMERA;
 616        input->index = index;
 617        input->reserved[0] = isp->inputs[index].type;
 618        input->reserved[1] = isp->inputs[index].port;
 619
 620        return 0;
 621}
 622
 623static unsigned int atomisp_subdev_streaming_count(
 624                                        struct atomisp_sub_device *asd)
 625{
 626        return asd->video_out_preview.capq.streaming
 627                + asd->video_out_capture.capq.streaming
 628                + asd->video_out_video_capture.capq.streaming
 629                + asd->video_out_vf.capq.streaming
 630                + asd->video_in.capq.streaming;
 631}
 632
 633unsigned int atomisp_streaming_count(struct atomisp_device *isp)
 634{
 635        unsigned int i, sum;
 636
 637        for (i = 0, sum = 0; i < isp->num_of_streams; i++)
 638                sum += isp->asd[i].streaming ==
 639                    ATOMISP_DEVICE_STREAMING_ENABLED;
 640
 641        return sum;
 642}
 643
 644unsigned int atomisp_is_acc_enabled(struct atomisp_device *isp)
 645{
 646        unsigned int i;
 647
 648        for (i = 0; i < isp->num_of_streams; i++)
 649                if (isp->asd[i].acc.pipeline)
 650                        return 1;
 651
 652        return 0;
 653}
 654/*
 655 * get input are used to get current primary/secondary camera
 656 */
 657static int atomisp_g_input(struct file *file, void *fh, unsigned int *input)
 658{
 659        struct video_device *vdev = video_devdata(file);
 660        struct atomisp_device *isp = video_get_drvdata(vdev);
 661        struct atomisp_sub_device *asd = atomisp_to_video_pipe(vdev)->asd;
 662
 663        rt_mutex_lock(&isp->mutex);
 664        *input = asd->input_curr;
 665        rt_mutex_unlock(&isp->mutex);
 666
 667        return 0;
 668}
 669/*
 670 * set input are used to set current primary/secondary camera
 671 */
 672static int atomisp_s_input(struct file *file, void *fh, unsigned int input)
 673{
 674        struct video_device *vdev = video_devdata(file);
 675        struct atomisp_device *isp = video_get_drvdata(vdev);
 676        struct atomisp_sub_device *asd = atomisp_to_video_pipe(vdev)->asd;
 677        struct v4l2_subdev *camera = NULL;
 678        int ret;
 679
 680        rt_mutex_lock(&isp->mutex);
 681        if (input >= ATOM_ISP_MAX_INPUTS || input >= isp->input_cnt) {
 682                dev_dbg(isp->dev, "input_cnt: %d\n", isp->input_cnt);
 683                ret = -EINVAL;
 684                goto error;
 685        }
 686
 687        /*
 688         * check whether the request camera:
 689         * 1: already in use
 690         * 2: if in use, whether it is used by other streams
 691         */
 692        if (isp->inputs[input].asd != NULL && isp->inputs[input].asd != asd) {
 693                dev_err(isp->dev,
 694                         "%s, camera is already used by stream: %d\n", __func__,
 695                         isp->inputs[input].asd->index);
 696                ret = -EBUSY;
 697                goto error;
 698        }
 699
 700        camera = isp->inputs[input].camera;
 701        if (!camera) {
 702                dev_err(isp->dev, "%s, no camera\n", __func__);
 703                ret = -EINVAL;
 704                goto error;
 705        }
 706
 707        if (atomisp_subdev_streaming_count(asd)) {
 708                dev_err(isp->dev,
 709                         "ISP is still streaming, stop first\n");
 710                ret = -EINVAL;
 711                goto error;
 712        }
 713
 714        /* power off the current owned sensor, as it is not used this time */
 715        if (isp->inputs[asd->input_curr].asd == asd &&
 716            asd->input_curr != input) {
 717                ret = v4l2_subdev_call(isp->inputs[asd->input_curr].camera,
 718                                       core, s_power, 0);
 719                if (ret)
 720                        dev_warn(isp->dev,
 721                                    "Failed to power-off sensor\n");
 722                /* clear the asd field to show this camera is not used */
 723                isp->inputs[asd->input_curr].asd = NULL;
 724        }
 725
 726        /* powe on the new sensor */
 727        ret = v4l2_subdev_call(isp->inputs[input].camera, core, s_power, 1);
 728        if (ret) {
 729                dev_err(isp->dev, "Failed to power-on sensor\n");
 730                goto error;
 731        }
 732        /*
 733         * Some sensor driver resets the run mode during power-on, thus force
 734         * update the run mode to sensor after power-on.
 735         */
 736        atomisp_update_run_mode(asd);
 737
 738        /* select operating sensor */
 739        ret = v4l2_subdev_call(isp->inputs[input].camera, video, s_routing,
 740                0, isp->inputs[input].sensor_index, 0);
 741        if (ret && (ret != -ENOIOCTLCMD)) {
 742                dev_err(isp->dev, "Failed to select sensor\n");
 743                goto error;
 744        }
 745
 746#ifndef ISP2401
 747        if (!isp->sw_contex.file_input && isp->inputs[input].motor)
 748                ret = v4l2_subdev_call(isp->inputs[input].motor, core,
 749                                       init, 1);
 750#else
 751        if (isp->motor)
 752                ret = v4l2_subdev_call(isp->motor, core, s_power, 1);
 753
 754        if (!isp->sw_contex.file_input && isp->motor)
 755                ret = v4l2_subdev_call(isp->motor, core, init, 1);
 756#endif
 757
 758        asd->input_curr = input;
 759        /* mark this camera is used by the current stream */
 760        isp->inputs[input].asd = asd;
 761        rt_mutex_unlock(&isp->mutex);
 762
 763        return 0;
 764
 765error:
 766        rt_mutex_unlock(&isp->mutex);
 767
 768        return ret;
 769}
 770
 771static int atomisp_enum_fmt_cap(struct file *file, void *fh,
 772        struct v4l2_fmtdesc *f)
 773{
 774        struct video_device *vdev = video_devdata(file);
 775        struct atomisp_device *isp = video_get_drvdata(vdev);
 776        struct atomisp_sub_device *asd = atomisp_to_video_pipe(vdev)->asd;
 777        struct v4l2_subdev_mbus_code_enum code = { 0 };
 778        unsigned int i, fi = 0;
 779        int rval;
 780
 781        rt_mutex_lock(&isp->mutex);
 782        rval = v4l2_subdev_call(isp->inputs[asd->input_curr].camera, pad,
 783                                enum_mbus_code, NULL, &code);
 784        if (rval == -ENOIOCTLCMD) {
 785                dev_warn(isp->dev, "enum_mbus_code pad op not supported. Please fix your sensor driver!\n");
 786        //      rval = v4l2_subdev_call(isp->inputs[asd->input_curr].camera,
 787        //                              video, enum_mbus_fmt, 0, &code.code);
 788        }
 789        rt_mutex_unlock(&isp->mutex);
 790
 791        if (rval)
 792                return rval;
 793
 794        for (i = 0; i < ARRAY_SIZE(atomisp_output_fmts); i++) {
 795                const struct atomisp_format_bridge *format =
 796                        &atomisp_output_fmts[i];
 797
 798                /*
 799                 * Is the atomisp-supported format is valid for the
 800                 * sensor (configuration)? If not, skip it.
 801                 */
 802                if (format->sh_fmt == CSS_FRAME_FORMAT_RAW
 803                    && format->mbus_code != code.code)
 804                        continue;
 805
 806                /* Found a match. Now let's pick f->index'th one. */
 807                if (fi < f->index) {
 808                        fi++;
 809                        continue;
 810                }
 811
 812                strlcpy(f->description, format->description,
 813                        sizeof(f->description));
 814                f->pixelformat = format->pixelformat;
 815                return 0;
 816        }
 817
 818        return -EINVAL;
 819}
 820
 821static int atomisp_g_fmt_cap(struct file *file, void *fh,
 822        struct v4l2_format *f)
 823{
 824        struct video_device *vdev = video_devdata(file);
 825        struct atomisp_device *isp = video_get_drvdata(vdev);
 826
 827        int ret;
 828
 829        rt_mutex_lock(&isp->mutex);
 830        ret = atomisp_get_fmt(vdev, f);
 831        rt_mutex_unlock(&isp->mutex);
 832        return ret;
 833}
 834
 835static int atomisp_g_fmt_file(struct file *file, void *fh,
 836                struct v4l2_format *f)
 837{
 838        struct video_device *vdev = video_devdata(file);
 839        struct atomisp_device *isp = video_get_drvdata(vdev);
 840        struct atomisp_video_pipe *pipe = atomisp_to_video_pipe(vdev);
 841
 842        rt_mutex_lock(&isp->mutex);
 843        f->fmt.pix = pipe->pix;
 844        rt_mutex_unlock(&isp->mutex);
 845
 846        return 0;
 847}
 848
 849/* This function looks up the closest available resolution. */
 850static int atomisp_try_fmt_cap(struct file *file, void *fh,
 851        struct v4l2_format *f)
 852{
 853        struct video_device *vdev = video_devdata(file);
 854        struct atomisp_device *isp = video_get_drvdata(vdev);
 855        int ret;
 856
 857        rt_mutex_lock(&isp->mutex);
 858        ret = atomisp_try_fmt(vdev, f, NULL);
 859        rt_mutex_unlock(&isp->mutex);
 860        return ret;
 861}
 862
 863static int atomisp_s_fmt_cap(struct file *file, void *fh,
 864        struct v4l2_format *f)
 865{
 866        struct video_device *vdev = video_devdata(file);
 867        struct atomisp_device *isp = video_get_drvdata(vdev);
 868        int ret;
 869
 870        rt_mutex_lock(&isp->mutex);
 871        if (isp->isp_fatal_error) {
 872                ret = -EIO;
 873                rt_mutex_unlock(&isp->mutex);
 874                return ret;
 875        }
 876        ret = atomisp_set_fmt(vdev, f);
 877        rt_mutex_unlock(&isp->mutex);
 878        return ret;
 879}
 880
 881static int atomisp_s_fmt_file(struct file *file, void *fh,
 882                                struct v4l2_format *f)
 883{
 884        struct video_device *vdev = video_devdata(file);
 885        struct atomisp_device *isp = video_get_drvdata(vdev);
 886        int ret;
 887
 888        rt_mutex_lock(&isp->mutex);
 889        ret = atomisp_set_fmt_file(vdev, f);
 890        rt_mutex_unlock(&isp->mutex);
 891        return ret;
 892}
 893
 894/*
 895 * Free videobuffer buffer priv data
 896 */
 897void atomisp_videobuf_free_buf(struct videobuf_buffer *vb)
 898{
 899        struct videobuf_vmalloc_memory *vm_mem;
 900
 901        if (vb == NULL)
 902                return;
 903
 904        vm_mem = vb->priv;
 905        if (vm_mem && vm_mem->vaddr) {
 906                atomisp_css_frame_free(vm_mem->vaddr);
 907                vm_mem->vaddr = NULL;
 908        }
 909}
 910
 911/*
 912 * this function is used to free video buffer queue
 913 */
 914static void atomisp_videobuf_free_queue(struct videobuf_queue *q)
 915{
 916        int i;
 917
 918        for (i = 0; i < VIDEO_MAX_FRAME; i++) {
 919                atomisp_videobuf_free_buf(q->bufs[i]);
 920                kfree(q->bufs[i]);
 921                q->bufs[i] = NULL;
 922        }
 923}
 924
 925int atomisp_alloc_css_stat_bufs(struct atomisp_sub_device *asd,
 926        uint16_t stream_id)
 927{
 928        struct atomisp_device *isp = asd->isp;
 929        struct atomisp_s3a_buf *s3a_buf = NULL, *_s3a_buf;
 930        struct atomisp_dis_buf *dis_buf = NULL, *_dis_buf;
 931        struct atomisp_metadata_buf *md_buf = NULL, *_md_buf;
 932        int count;
 933        struct atomisp_css_dvs_grid_info *dvs_grid_info =
 934                atomisp_css_get_dvs_grid_info(&asd->params.curr_grid_info);
 935        unsigned int i;
 936
 937        if (list_empty(&asd->s3a_stats) &&
 938                asd->params.curr_grid_info.s3a_grid.enable) {
 939                count = ATOMISP_CSS_Q_DEPTH +
 940                        ATOMISP_S3A_BUF_QUEUE_DEPTH_FOR_HAL;
 941                dev_dbg(isp->dev, "allocating %d 3a buffers\n", count);
 942                while (count--) {
 943                        s3a_buf = kzalloc(sizeof(struct atomisp_s3a_buf), GFP_KERNEL);
 944                        if (!s3a_buf)
 945                                goto error;
 946
 947                        if (atomisp_css_allocate_stat_buffers(
 948                                        asd, stream_id, s3a_buf, NULL, NULL)) {
 949                                kfree(s3a_buf);
 950                                goto error;
 951                        }
 952
 953                        list_add_tail(&s3a_buf->list, &asd->s3a_stats);
 954                }
 955        }
 956
 957        if (list_empty(&asd->dis_stats) && dvs_grid_info &&
 958                dvs_grid_info->enable) {
 959                count = ATOMISP_CSS_Q_DEPTH + 1;
 960                dev_dbg(isp->dev, "allocating %d dis buffers\n", count);
 961                while (count--) {
 962                        dis_buf = kzalloc(sizeof(struct atomisp_dis_buf), GFP_KERNEL);
 963                        if (!dis_buf) {
 964                                kfree(s3a_buf);
 965                                goto error;
 966                        }
 967                        if (atomisp_css_allocate_stat_buffers(
 968                                        asd, stream_id, NULL, dis_buf, NULL)) {
 969                                kfree(dis_buf);
 970                                goto error;
 971                        }
 972
 973                        list_add_tail(&dis_buf->list, &asd->dis_stats);
 974                }
 975        }
 976
 977        for (i = 0; i < ATOMISP_METADATA_TYPE_NUM; i++) {
 978                if (list_empty(&asd->metadata[i]) &&
 979                    list_empty(&asd->metadata_ready[i]) &&
 980                    list_empty(&asd->metadata_in_css[i])) {
 981                        count = ATOMISP_CSS_Q_DEPTH +
 982                                ATOMISP_METADATA_QUEUE_DEPTH_FOR_HAL;
 983                        dev_dbg(isp->dev, "allocating %d metadata buffers for type %d\n",
 984                                count, i);
 985                        while (count--) {
 986                                md_buf = kzalloc(sizeof(struct atomisp_metadata_buf),
 987                                                 GFP_KERNEL);
 988                                if (!md_buf)
 989                                        goto error;
 990
 991                                if (atomisp_css_allocate_stat_buffers(
 992                                                asd, stream_id, NULL, NULL, md_buf)) {
 993                                        kfree(md_buf);
 994                                        goto error;
 995                                }
 996                                list_add_tail(&md_buf->list, &asd->metadata[i]);
 997                        }
 998                }
 999        }
1000        return 0;
1001
1002error:
1003        dev_err(isp->dev, "failed to allocate statistics buffers\n");
1004
1005        list_for_each_entry_safe(dis_buf, _dis_buf, &asd->dis_stats, list) {
1006                atomisp_css_free_dis_buffer(dis_buf);
1007                list_del(&dis_buf->list);
1008                kfree(dis_buf);
1009        }
1010
1011        list_for_each_entry_safe(s3a_buf, _s3a_buf, &asd->s3a_stats, list) {
1012                atomisp_css_free_3a_buffer(s3a_buf);
1013                list_del(&s3a_buf->list);
1014                kfree(s3a_buf);
1015        }
1016
1017        for (i = 0; i < ATOMISP_METADATA_TYPE_NUM; i++) {
1018                list_for_each_entry_safe(md_buf, _md_buf, &asd->metadata[i],
1019                                        list) {
1020                        atomisp_css_free_metadata_buffer(md_buf);
1021                        list_del(&md_buf->list);
1022                        kfree(md_buf);
1023                }
1024        }
1025        return -ENOMEM;
1026}
1027
1028/*
1029 * Initiate Memory Mapping or User Pointer I/O
1030 */
1031int __atomisp_reqbufs(struct file *file, void *fh,
1032        struct v4l2_requestbuffers *req)
1033{
1034        struct video_device *vdev = video_devdata(file);
1035        struct atomisp_video_pipe *pipe = atomisp_to_video_pipe(vdev);
1036        struct atomisp_sub_device *asd = pipe->asd;
1037        struct atomisp_css_frame_info frame_info;
1038        struct atomisp_css_frame *frame;
1039        struct videobuf_vmalloc_memory *vm_mem;
1040        uint16_t source_pad = atomisp_subdev_source_pad(vdev);
1041        uint16_t stream_id = atomisp_source_pad_to_stream_id(asd, source_pad);
1042        int ret = 0, i = 0;
1043
1044        if (req->count == 0) {
1045                mutex_lock(&pipe->capq.vb_lock);
1046                if (!list_empty(&pipe->capq.stream))
1047                        videobuf_queue_cancel(&pipe->capq);
1048
1049                atomisp_videobuf_free_queue(&pipe->capq);
1050                mutex_unlock(&pipe->capq.vb_lock);
1051                /* clear request config id */
1052                memset(pipe->frame_request_config_id, 0,
1053                        VIDEO_MAX_FRAME * sizeof(unsigned int));
1054                memset(pipe->frame_params, 0,
1055                        VIDEO_MAX_FRAME *
1056                        sizeof(struct atomisp_css_params_with_list *));
1057                return 0;
1058        }
1059
1060        ret = videobuf_reqbufs(&pipe->capq, req);
1061        if (ret)
1062                return ret;
1063
1064        atomisp_alloc_css_stat_bufs(asd, stream_id);
1065
1066        /*
1067         * for user pointer type, buffers are not really allcated here,
1068         * buffers are setup in QBUF operation through v4l2_buffer structure
1069         */
1070        if (req->memory == V4L2_MEMORY_USERPTR)
1071                return 0;
1072
1073        ret = atomisp_get_css_frame_info(asd, source_pad, &frame_info);
1074        if (ret)
1075                return ret;
1076
1077        /*
1078         * Allocate the real frame here for selected node using our
1079         * memory management function
1080         */
1081        for (i = 0; i < req->count; i++) {
1082                if (atomisp_css_frame_allocate_from_info(&frame, &frame_info))
1083                        goto error;
1084                vm_mem = pipe->capq.bufs[i]->priv;
1085                vm_mem->vaddr = frame;
1086        }
1087
1088        return ret;
1089
1090error:
1091        while (i--) {
1092                vm_mem = pipe->capq.bufs[i]->priv;
1093                atomisp_css_frame_free(vm_mem->vaddr);
1094        }
1095
1096        if (asd->vf_frame)
1097                atomisp_css_frame_free(asd->vf_frame);
1098
1099        return -ENOMEM;
1100}
1101
1102int atomisp_reqbufs(struct file *file, void *fh,
1103        struct v4l2_requestbuffers *req)
1104{
1105        struct video_device *vdev = video_devdata(file);
1106        struct atomisp_device *isp = video_get_drvdata(vdev);
1107        int ret;
1108
1109        rt_mutex_lock(&isp->mutex);
1110        ret = __atomisp_reqbufs(file, fh, req);
1111        rt_mutex_unlock(&isp->mutex);
1112
1113        return ret;
1114}
1115
1116static int atomisp_reqbufs_file(struct file *file, void *fh,
1117                struct v4l2_requestbuffers *req)
1118{
1119        struct video_device *vdev = video_devdata(file);
1120        struct atomisp_video_pipe *pipe = atomisp_to_video_pipe(vdev);
1121
1122        if (req->count == 0) {
1123                mutex_lock(&pipe->outq.vb_lock);
1124                atomisp_videobuf_free_queue(&pipe->outq);
1125                mutex_unlock(&pipe->outq.vb_lock);
1126                return 0;
1127        }
1128
1129        return videobuf_reqbufs(&pipe->outq, req);
1130}
1131
1132/* application query the status of a buffer */
1133static int atomisp_querybuf(struct file *file, void *fh,
1134        struct v4l2_buffer *buf)
1135{
1136        struct video_device *vdev = video_devdata(file);
1137        struct atomisp_video_pipe *pipe = atomisp_to_video_pipe(vdev);
1138
1139        return videobuf_querybuf(&pipe->capq, buf);
1140}
1141
1142static int atomisp_querybuf_file(struct file *file, void *fh,
1143                                struct v4l2_buffer *buf)
1144{
1145        struct video_device *vdev = video_devdata(file);
1146        struct atomisp_video_pipe *pipe = atomisp_to_video_pipe(vdev);
1147
1148        return videobuf_querybuf(&pipe->outq, buf);
1149}
1150
1151/*
1152 * Applications call the VIDIOC_QBUF ioctl to enqueue an empty (capturing) or
1153 * filled (output) buffer in the drivers incoming queue.
1154 */
1155static int atomisp_qbuf(struct file *file, void *fh, struct v4l2_buffer *buf)
1156{
1157        static const int NOFLUSH_FLAGS = V4L2_BUF_FLAG_NO_CACHE_INVALIDATE |
1158                                         V4L2_BUF_FLAG_NO_CACHE_CLEAN;
1159        struct video_device *vdev = video_devdata(file);
1160        struct atomisp_device *isp = video_get_drvdata(vdev);
1161        struct atomisp_video_pipe *pipe = atomisp_to_video_pipe(vdev);
1162        struct atomisp_sub_device *asd = pipe->asd;
1163        struct videobuf_buffer *vb;
1164        struct videobuf_vmalloc_memory *vm_mem;
1165        struct atomisp_css_frame_info frame_info;
1166        struct atomisp_css_frame *handle = NULL;
1167        u32 length;
1168        u32 pgnr;
1169        int ret = 0;
1170
1171        rt_mutex_lock(&isp->mutex);
1172        if (isp->isp_fatal_error) {
1173                ret = -EIO;
1174                goto error;
1175        }
1176
1177        if (asd->streaming == ATOMISP_DEVICE_STREAMING_STOPPING) {
1178                dev_err(isp->dev, "%s: reject, as ISP at stopping.\n",
1179                                __func__);
1180                ret = -EIO;
1181                goto error;
1182        }
1183
1184        if (!buf || buf->index >= VIDEO_MAX_FRAME ||
1185                !pipe->capq.bufs[buf->index]) {
1186                dev_err(isp->dev, "Invalid index for qbuf.\n");
1187                ret = -EINVAL;
1188                goto error;
1189        }
1190
1191        /*
1192         * For userptr type frame, we convert user space address to physic
1193         * address and reprograme out page table properly
1194         */
1195        if (buf->memory == V4L2_MEMORY_USERPTR) {
1196                struct hrt_userbuffer_attr attributes;
1197                vb = pipe->capq.bufs[buf->index];
1198                vm_mem = vb->priv;
1199                if (!vm_mem) {
1200                        ret = -EINVAL;
1201                        goto error;
1202                }
1203
1204                length = vb->bsize;
1205                pgnr = (length + (PAGE_SIZE - 1)) >> PAGE_SHIFT;
1206
1207                if (vb->baddr == buf->m.userptr && vm_mem->vaddr)
1208                        goto done;
1209
1210                if (atomisp_get_css_frame_info(asd,
1211                                atomisp_subdev_source_pad(vdev), &frame_info)) {
1212                        ret = -EIO;
1213                        goto error;
1214                }
1215
1216                attributes.pgnr = pgnr;
1217#ifdef CONFIG_ION
1218#ifndef ISP2401
1219                attributes.type = buf->type == V4L2_BUF_TYPE_VIDEO_CAPTURE_ION
1220                                        ? HRT_USR_ION : HRT_USR_PTR;
1221#else
1222                if (buf->reserved & ATOMISP_BUFFER_TYPE_IS_ION) {
1223                        attributes.type = HRT_USR_ION;
1224                        if (asd->ion_dev_fd->val !=  ION_FD_UNSET) {
1225                                dev_dbg(isp->dev, "ION buffer queued, share_fd=%lddev_fd=%d.\n",
1226                                buf->m.userptr, asd->ion_dev_fd->val);
1227                                /*
1228                                 * Make sure the shared fd we just got
1229                                 * from user space isn't larger than
1230                                 * the space we have for it.
1231                                 */
1232                                if ((buf->m.userptr &
1233                                (ATOMISP_ION_DEVICE_FD_MASK)) != 0) {
1234                                        dev_err(isp->dev,
1235                                                        "Error: v4l2 buffer fd:0X%0lX > 0XFFFF.\n",
1236                                                        buf->m.userptr);
1237                                        ret = -EINVAL;
1238                                        goto error;
1239                                }
1240                                buf->m.userptr |= asd->ion_dev_fd->val <<
1241                                        ATOMISP_ION_DEVICE_FD_OFFSET;
1242                        } else {
1243                                dev_err(isp->dev, "v4l2 buffer type is ION, \
1244                                                but no dev fd set from userspace.\n");
1245                                ret = -EINVAL;
1246                                goto error;
1247                        }
1248                } else {
1249                        attributes.type = HRT_USR_PTR;
1250                }
1251#endif
1252#else
1253                attributes.type = HRT_USR_PTR;
1254#endif
1255                ret = atomisp_css_frame_map(&handle, &frame_info,
1256                                       (void *)buf->m.userptr,
1257                                       0, &attributes);
1258                if (ret) {
1259                        dev_err(isp->dev, "Failed to map user buffer\n");
1260                        goto error;
1261                }
1262
1263                if (vm_mem->vaddr) {
1264                        mutex_lock(&pipe->capq.vb_lock);
1265                        atomisp_css_frame_free(vm_mem->vaddr);
1266                        vm_mem->vaddr = NULL;
1267                        vb->state = VIDEOBUF_NEEDS_INIT;
1268                        mutex_unlock(&pipe->capq.vb_lock);
1269                }
1270
1271                vm_mem->vaddr = handle;
1272
1273                buf->flags &= ~V4L2_BUF_FLAG_MAPPED;
1274                buf->flags |= V4L2_BUF_FLAG_QUEUED;
1275                buf->flags &= ~V4L2_BUF_FLAG_DONE;
1276        } else if (buf->memory == V4L2_MEMORY_MMAP) {
1277                buf->flags |= V4L2_BUF_FLAG_MAPPED;
1278                buf->flags |= V4L2_BUF_FLAG_QUEUED;
1279                buf->flags &= ~V4L2_BUF_FLAG_DONE;
1280        }
1281
1282done:
1283        if (!((buf->flags & NOFLUSH_FLAGS) == NOFLUSH_FLAGS))
1284                wbinvd();
1285
1286        if (!atomisp_is_vf_pipe(pipe) &&
1287            (buf->reserved2 & ATOMISP_BUFFER_HAS_PER_FRAME_SETTING)) {
1288                /* this buffer will have a per-frame parameter */
1289                pipe->frame_request_config_id[buf->index] = buf->reserved2 &
1290                                        ~ATOMISP_BUFFER_HAS_PER_FRAME_SETTING;
1291                dev_dbg(isp->dev, "This buffer requires per_frame setting which has isp_config_id %d\n",
1292                        pipe->frame_request_config_id[buf->index]);
1293        } else {
1294                pipe->frame_request_config_id[buf->index] = 0;
1295        }
1296
1297        pipe->frame_params[buf->index] = NULL;
1298
1299        rt_mutex_unlock(&isp->mutex);
1300
1301        ret = videobuf_qbuf(&pipe->capq, buf);
1302        rt_mutex_lock(&isp->mutex);
1303        if (ret)
1304                goto error;
1305
1306        /* TODO: do this better, not best way to queue to css */
1307        if (asd->streaming == ATOMISP_DEVICE_STREAMING_ENABLED) {
1308                if (!list_empty(&pipe->buffers_waiting_for_param)) {
1309                        atomisp_handle_parameter_and_buffer(pipe);
1310                } else {
1311                        atomisp_qbuffers_to_css(asd);
1312
1313#ifndef ISP2401
1314                        if (!atomisp_is_wdt_running(asd) && atomisp_buffers_queued(asd))
1315                                atomisp_wdt_start(asd);
1316#else
1317                        if (!atomisp_is_wdt_running(pipe) &&
1318                                atomisp_buffers_queued_pipe(pipe))
1319                                atomisp_wdt_start(pipe);
1320#endif
1321                }
1322        }
1323
1324        /* Workaround: Due to the design of HALv3,
1325         * sometimes in ZSL or SDV mode HAL needs to
1326         * capture multiple images within one streaming cycle.
1327         * But the capture number cannot be determined by HAL.
1328         * So HAL only sets the capture number to be 1 and queue multiple
1329         * buffers. Atomisp driver needs to check this case and re-trigger
1330         * CSS to do capture when new buffer is queued. */
1331        if (asd->continuous_mode->val &&
1332            atomisp_subdev_source_pad(vdev)
1333            == ATOMISP_SUBDEV_PAD_SOURCE_CAPTURE &&
1334            pipe->capq.streaming &&
1335            !asd->enable_raw_buffer_lock->val &&
1336            asd->params.offline_parm.num_captures == 1) {
1337#ifndef ISP2401
1338                asd->pending_capture_request++;
1339                dev_dbg(isp->dev, "Add one pending capture request.\n");
1340#else
1341            if (asd->re_trigger_capture) {
1342                        ret = atomisp_css_offline_capture_configure(asd,
1343                                asd->params.offline_parm.num_captures,
1344                                asd->params.offline_parm.skip_frames,
1345                                asd->params.offline_parm.offset);
1346                        asd->re_trigger_capture = false;
1347                        dev_dbg(isp->dev, "%s Trigger capture again ret=%d\n",
1348                                __func__, ret);
1349
1350            } else {
1351                        asd->pending_capture_request++;
1352                        asd->re_trigger_capture = false;
1353                        dev_dbg(isp->dev, "Add one pending capture request.\n");
1354            }
1355#endif
1356        }
1357        rt_mutex_unlock(&isp->mutex);
1358
1359        dev_dbg(isp->dev, "qbuf buffer %d (%s) for asd%d\n", buf->index,
1360                vdev->name, asd->index);
1361
1362        return ret;
1363
1364error:
1365        rt_mutex_unlock(&isp->mutex);
1366        return ret;
1367}
1368
1369static int atomisp_qbuf_file(struct file *file, void *fh,
1370                                        struct v4l2_buffer *buf)
1371{
1372        struct video_device *vdev = video_devdata(file);
1373        struct atomisp_device *isp = video_get_drvdata(vdev);
1374        struct atomisp_video_pipe *pipe = atomisp_to_video_pipe(vdev);
1375        int ret;
1376
1377        rt_mutex_lock(&isp->mutex);
1378        if (isp->isp_fatal_error) {
1379                ret = -EIO;
1380                goto error;
1381        }
1382
1383        if (!buf || buf->index >= VIDEO_MAX_FRAME ||
1384                !pipe->outq.bufs[buf->index]) {
1385                dev_err(isp->dev, "Invalid index for qbuf.\n");
1386                ret = -EINVAL;
1387                goto error;
1388        }
1389
1390        if (buf->memory != V4L2_MEMORY_MMAP) {
1391                dev_err(isp->dev, "Unsupported memory method\n");
1392                ret = -EINVAL;
1393                goto error;
1394        }
1395
1396        if (buf->type != V4L2_BUF_TYPE_VIDEO_OUTPUT) {
1397                dev_err(isp->dev, "Unsupported buffer type\n");
1398                ret = -EINVAL;
1399                goto error;
1400        }
1401        rt_mutex_unlock(&isp->mutex);
1402
1403        return videobuf_qbuf(&pipe->outq, buf);
1404
1405error:
1406        rt_mutex_unlock(&isp->mutex);
1407
1408        return ret;
1409}
1410
1411static int __get_frame_exp_id(struct atomisp_video_pipe *pipe,
1412                struct v4l2_buffer *buf)
1413{
1414        struct videobuf_vmalloc_memory *vm_mem;
1415        struct atomisp_css_frame *handle;
1416        int i;
1417
1418        for (i = 0; pipe->capq.bufs[i]; i++) {
1419                vm_mem = pipe->capq.bufs[i]->priv;
1420                handle = vm_mem->vaddr;
1421                if (buf->index == pipe->capq.bufs[i]->i && handle)
1422                        return handle->exp_id;
1423        }
1424        return -EINVAL;
1425}
1426
1427/*
1428 * Applications call the VIDIOC_DQBUF ioctl to dequeue a filled (capturing) or
1429 * displayed (output buffer)from the driver's outgoing queue
1430 */
1431static int atomisp_dqbuf(struct file *file, void *fh, struct v4l2_buffer *buf)
1432{
1433        struct video_device *vdev = video_devdata(file);
1434        struct atomisp_video_pipe *pipe = atomisp_to_video_pipe(vdev);
1435        struct atomisp_sub_device *asd = pipe->asd;
1436        struct atomisp_device *isp = video_get_drvdata(vdev);
1437        int ret = 0;
1438
1439        rt_mutex_lock(&isp->mutex);
1440
1441        if (isp->isp_fatal_error) {
1442                rt_mutex_unlock(&isp->mutex);
1443                return -EIO;
1444        }
1445
1446        if (asd->streaming == ATOMISP_DEVICE_STREAMING_STOPPING) {
1447                rt_mutex_unlock(&isp->mutex);
1448                dev_err(isp->dev, "%s: reject, as ISP at stopping.\n",
1449                                __func__);
1450                return -EIO;
1451        }
1452
1453        rt_mutex_unlock(&isp->mutex);
1454
1455        ret = videobuf_dqbuf(&pipe->capq, buf, file->f_flags & O_NONBLOCK);
1456        if (ret) {
1457                dev_dbg(isp->dev, "<%s: %d\n", __func__, ret);
1458                return ret;
1459        }
1460        rt_mutex_lock(&isp->mutex);
1461        buf->bytesused = pipe->pix.sizeimage;
1462        buf->reserved = asd->frame_status[buf->index];
1463
1464        /*
1465         * Hack:
1466         * Currently frame_status in the enum type which takes no more lower
1467         * 8 bit.
1468         * use bit[31:16] for exp_id as it is only in the range of 1~255
1469         */
1470        buf->reserved &= 0x0000ffff;
1471        if (!(buf->flags & V4L2_BUF_FLAG_ERROR))
1472                buf->reserved |= __get_frame_exp_id(pipe, buf) << 16;
1473        buf->reserved2 = pipe->frame_config_id[buf->index];
1474        rt_mutex_unlock(&isp->mutex);
1475
1476        dev_dbg(isp->dev, "dqbuf buffer %d (%s) for asd%d with exp_id %d, isp_config_id %d\n",
1477                buf->index, vdev->name, asd->index, buf->reserved >> 16,
1478                buf->reserved2);
1479        return 0;
1480}
1481
1482enum atomisp_css_pipe_id atomisp_get_css_pipe_id(struct atomisp_sub_device *asd)
1483{
1484        if (ATOMISP_USE_YUVPP(asd))
1485                return CSS_PIPE_ID_YUVPP;
1486
1487        if (asd->continuous_mode->val) {
1488                if (asd->run_mode->val == ATOMISP_RUN_MODE_VIDEO)
1489                        return CSS_PIPE_ID_VIDEO;
1490                else
1491                        return CSS_PIPE_ID_PREVIEW;
1492        }
1493
1494        /*
1495         * Disable vf_pp and run CSS in video mode. This allows using ISP
1496         * scaling but it has one frame delay due to CSS internal buffering.
1497         */
1498        if (asd->vfpp->val == ATOMISP_VFPP_DISABLE_SCALER)
1499                return CSS_PIPE_ID_VIDEO;
1500
1501        /*
1502         * Disable vf_pp and run CSS in still capture mode. In this mode
1503         * CSS does not cause extra latency with buffering, but scaling
1504         * is not available.
1505         */
1506        if (asd->vfpp->val == ATOMISP_VFPP_DISABLE_LOWLAT)
1507                return CSS_PIPE_ID_CAPTURE;
1508
1509        switch (asd->run_mode->val) {
1510        case ATOMISP_RUN_MODE_PREVIEW:
1511                return CSS_PIPE_ID_PREVIEW;
1512        case ATOMISP_RUN_MODE_VIDEO:
1513                return CSS_PIPE_ID_VIDEO;
1514        case ATOMISP_RUN_MODE_STILL_CAPTURE:
1515                /* fall through */
1516        default:
1517                return CSS_PIPE_ID_CAPTURE;
1518        }
1519}
1520
1521static unsigned int atomisp_sensor_start_stream(struct atomisp_sub_device *asd)
1522{
1523        struct atomisp_device *isp = asd->isp;
1524
1525        if (isp->inputs[asd->input_curr].camera_caps->
1526            sensor[asd->sensor_curr].stream_num > 1) {
1527                if (asd->high_speed_mode)
1528                        return 1;
1529                else
1530                        return 2;
1531        }
1532
1533        if (asd->vfpp->val != ATOMISP_VFPP_ENABLE ||
1534            asd->copy_mode)
1535                return 1;
1536
1537        if (asd->run_mode->val == ATOMISP_RUN_MODE_VIDEO ||
1538            (asd->run_mode->val == ATOMISP_RUN_MODE_STILL_CAPTURE &&
1539             !atomisp_is_mbuscode_raw(
1540                     asd->fmt[
1541                             asd->capture_pad].fmt.code) &&
1542             !asd->continuous_mode->val))
1543                return 2;
1544        else
1545                return 1;
1546}
1547
1548int atomisp_stream_on_master_slave_sensor(struct atomisp_device *isp,
1549        bool isp_timeout)
1550{
1551        unsigned int master = -1, slave = -1, delay_slave = 0;
1552        int i, ret;
1553
1554        /*
1555         * ISP only support 2 streams now so ignore multiple master/slave
1556         * case to reduce the delay between 2 stream_on calls.
1557         */
1558        for (i = 0; i < isp->num_of_streams; i++) {
1559                int sensor_index = isp->asd[i].input_curr;
1560                if (isp->inputs[sensor_index].camera_caps->
1561                                sensor[isp->asd[i].sensor_curr].is_slave)
1562                        slave = sensor_index;
1563                else
1564                        master = sensor_index;
1565        }
1566
1567        if (master == -1 || slave == -1) {
1568                master = ATOMISP_DEPTH_DEFAULT_MASTER_SENSOR;
1569                slave = ATOMISP_DEPTH_DEFAULT_SLAVE_SENSOR;
1570                dev_warn(isp->dev,
1571                         "depth mode use default master=%s.slave=%s.\n",
1572                         isp->inputs[master].camera->name,
1573                         isp->inputs[slave].camera->name);
1574        }
1575
1576        ret = v4l2_subdev_call(isp->inputs[master].camera, core,
1577                               ioctl, ATOMISP_IOC_G_DEPTH_SYNC_COMP,
1578                               &delay_slave);
1579        if (ret)
1580                dev_warn(isp->dev,
1581                         "get depth sensor %s compensation delay failed.\n",
1582                         isp->inputs[master].camera->name);
1583
1584        ret = v4l2_subdev_call(isp->inputs[master].camera,
1585                               video, s_stream, 1);
1586        if (ret) {
1587                dev_err(isp->dev, "depth mode master sensor %s stream-on failed.\n",
1588                        isp->inputs[master].camera->name);
1589                return -EINVAL;
1590        }
1591
1592        if (delay_slave != 0)
1593                udelay(delay_slave);
1594
1595        ret = v4l2_subdev_call(isp->inputs[slave].camera,
1596                               video, s_stream, 1);
1597        if (ret) {
1598                dev_err(isp->dev, "depth mode slave sensor %s stream-on failed.\n",
1599                        isp->inputs[slave].camera->name);
1600                v4l2_subdev_call(isp->inputs[master].camera, video, s_stream, 0);
1601
1602                return -EINVAL;
1603        }
1604
1605        return 0;
1606}
1607
1608/* FIXME! */
1609#ifndef ISP2401
1610static void __wdt_on_master_slave_sensor(struct atomisp_device *isp,
1611                                         unsigned int wdt_duration)
1612#else
1613static void __wdt_on_master_slave_sensor(struct atomisp_video_pipe *pipe,
1614                                         unsigned int wdt_duration,
1615                                         bool enable)
1616#endif
1617{
1618#ifndef ISP2401
1619        if (atomisp_buffers_queued(&isp->asd[0]))
1620                atomisp_wdt_refresh(&isp->asd[0], wdt_duration);
1621        if (atomisp_buffers_queued(&isp->asd[1]))
1622                atomisp_wdt_refresh(&isp->asd[1], wdt_duration);
1623#else
1624        static struct atomisp_video_pipe *pipe0;
1625
1626        if (enable) {
1627                if (atomisp_buffers_queued_pipe(pipe0))
1628                        atomisp_wdt_refresh_pipe(pipe0, wdt_duration);
1629                if (atomisp_buffers_queued_pipe(pipe))
1630                        atomisp_wdt_refresh_pipe(pipe, wdt_duration);
1631        } else {
1632                pipe0 = pipe;
1633        }
1634#endif
1635}
1636
1637static void atomisp_pause_buffer_event(struct atomisp_device *isp)
1638{
1639        struct v4l2_event event = {0};
1640        int i;
1641
1642        event.type = V4L2_EVENT_ATOMISP_PAUSE_BUFFER;
1643
1644        for (i = 0; i < isp->num_of_streams; i++) {
1645                int sensor_index = isp->asd[i].input_curr;
1646                if (isp->inputs[sensor_index].camera_caps->
1647                                sensor[isp->asd[i].sensor_curr].is_slave) {
1648                        v4l2_event_queue(isp->asd[i].subdev.devnode, &event);
1649                        break;
1650                }
1651        }
1652}
1653
1654/* Input system HW workaround */
1655/* Input system address translation corrupts burst during */
1656/* invalidate. SW workaround for this is to set burst length */
1657/* manually to 128 in case of 13MPx snapshot and to 1 otherwise. */
1658static void atomisp_dma_burst_len_cfg(struct atomisp_sub_device *asd)
1659{
1660
1661        struct v4l2_mbus_framefmt *sink;
1662        sink = atomisp_subdev_get_ffmt(&asd->subdev, NULL,
1663                                       V4L2_SUBDEV_FORMAT_ACTIVE,
1664                                       ATOMISP_SUBDEV_PAD_SINK);
1665
1666        if (sink->width * sink->height >= 4096*3072)
1667                atomisp_store_uint32(DMA_BURST_SIZE_REG, 0x7F);
1668        else
1669                atomisp_store_uint32(DMA_BURST_SIZE_REG, 0x00);
1670}
1671
1672/*
1673 * This ioctl start the capture during streaming I/O.
1674 */
1675static int atomisp_streamon(struct file *file, void *fh,
1676        enum v4l2_buf_type type)
1677{
1678        struct video_device *vdev = video_devdata(file);
1679        struct atomisp_video_pipe *pipe = atomisp_to_video_pipe(vdev);
1680        struct atomisp_sub_device *asd = pipe->asd;
1681        struct atomisp_device *isp = video_get_drvdata(vdev);
1682        enum atomisp_css_pipe_id css_pipe_id;
1683        unsigned int sensor_start_stream;
1684        unsigned int wdt_duration = ATOMISP_ISP_TIMEOUT_DURATION;
1685        int ret = 0;
1686        unsigned long irqflags;
1687
1688        dev_dbg(isp->dev, "Start stream on pad %d for asd%d\n",
1689                atomisp_subdev_source_pad(vdev), asd->index);
1690
1691        if (type != V4L2_BUF_TYPE_VIDEO_CAPTURE) {
1692                dev_dbg(isp->dev, "unsupported v4l2 buf type\n");
1693                return -EINVAL;
1694        }
1695
1696        rt_mutex_lock(&isp->mutex);
1697        if (isp->isp_fatal_error) {
1698                ret = -EIO;
1699                goto out;
1700        }
1701
1702        if (asd->streaming == ATOMISP_DEVICE_STREAMING_STOPPING) {
1703                ret = -EBUSY;
1704                goto out;
1705        }
1706
1707        if (pipe->capq.streaming)
1708                goto out;
1709
1710        /* Input system HW workaround */
1711        atomisp_dma_burst_len_cfg(asd);
1712
1713        /*
1714         * The number of streaming video nodes is based on which
1715         * binary is going to be run.
1716         */
1717        sensor_start_stream = atomisp_sensor_start_stream(asd);
1718
1719        spin_lock_irqsave(&pipe->irq_lock, irqflags);
1720        if (list_empty(&(pipe->capq.stream))) {
1721                spin_unlock_irqrestore(&pipe->irq_lock, irqflags);
1722                dev_dbg(isp->dev, "no buffer in the queue\n");
1723                ret = -EINVAL;
1724                goto out;
1725        }
1726        spin_unlock_irqrestore(&pipe->irq_lock, irqflags);
1727
1728        ret = videobuf_streamon(&pipe->capq);
1729        if (ret)
1730                goto out;
1731
1732        /* Reset pending capture request count. */
1733        asd->pending_capture_request = 0;
1734#ifdef ISP2401
1735        asd->re_trigger_capture = false;
1736#endif
1737
1738        if ((atomisp_subdev_streaming_count(asd) > sensor_start_stream) &&
1739            (!isp->inputs[asd->input_curr].camera_caps->multi_stream_ctrl)) {
1740                /* trigger still capture */
1741                if (asd->continuous_mode->val &&
1742                    atomisp_subdev_source_pad(vdev)
1743                    == ATOMISP_SUBDEV_PAD_SOURCE_CAPTURE) {
1744                        if (asd->run_mode->val == ATOMISP_RUN_MODE_VIDEO)
1745                                dev_dbg(isp->dev, "SDV last video raw buffer id: %u\n",
1746                                        asd->latest_preview_exp_id);
1747                        else
1748                                dev_dbg(isp->dev, "ZSL last preview raw buffer id: %u\n",
1749                                        asd->latest_preview_exp_id);
1750
1751                        if (asd->delayed_init == ATOMISP_DELAYED_INIT_QUEUED) {
1752                                flush_work(&asd->delayed_init_work);
1753                                rt_mutex_unlock(&isp->mutex);
1754                                if (wait_for_completion_interruptible(
1755                                                &asd->init_done) != 0)
1756                                        return -ERESTARTSYS;
1757                                rt_mutex_lock(&isp->mutex);
1758                        }
1759
1760                        /* handle per_frame_setting parameter and buffers */
1761                        atomisp_handle_parameter_and_buffer(pipe);
1762
1763                        /*
1764                         * only ZSL/SDV capture request will be here, raise
1765                         * the ISP freq to the highest possible to minimize
1766                         * the S2S latency.
1767                         */
1768                        atomisp_freq_scaling(isp, ATOMISP_DFS_MODE_MAX, false);
1769                        /*
1770                         * When asd->enable_raw_buffer_lock->val is true,
1771                         * An extra IOCTL is needed to call
1772                         * atomisp_css_exp_id_capture and trigger real capture
1773                         */
1774                        if (!asd->enable_raw_buffer_lock->val) {
1775                                ret = atomisp_css_offline_capture_configure(asd,
1776                                        asd->params.offline_parm.num_captures,
1777                                        asd->params.offline_parm.skip_frames,
1778                                        asd->params.offline_parm.offset);
1779                                if (ret) {
1780                                        ret = -EINVAL;
1781                                        goto out;
1782                                }
1783                                if (asd->depth_mode->val)
1784                                        atomisp_pause_buffer_event(isp);
1785                        }
1786                }
1787                atomisp_qbuffers_to_css(asd);
1788                goto out;
1789        }
1790
1791        if (asd->streaming == ATOMISP_DEVICE_STREAMING_ENABLED) {
1792                atomisp_qbuffers_to_css(asd);
1793                goto start_sensor;
1794        }
1795
1796        css_pipe_id = atomisp_get_css_pipe_id(asd);
1797
1798        ret = atomisp_acc_load_extensions(asd);
1799        if (ret < 0) {
1800                dev_err(isp->dev, "acc extension failed to load\n");
1801                goto out;
1802        }
1803
1804        if (asd->params.css_update_params_needed) {
1805                atomisp_apply_css_parameters(asd, &asd->params.css_param);
1806                if (asd->params.css_param.update_flag.dz_config)
1807                        atomisp_css_set_dz_config(asd,
1808                                &asd->params.css_param.dz_config);
1809                atomisp_css_update_isp_params(asd);
1810                asd->params.css_update_params_needed = false;
1811                memset(&asd->params.css_param.update_flag, 0,
1812                       sizeof(struct atomisp_parameters));
1813        }
1814        asd->params.dvs_6axis = NULL;
1815
1816        ret = atomisp_css_start(asd, css_pipe_id, false);
1817        if (ret)
1818                goto out;
1819
1820        asd->streaming = ATOMISP_DEVICE_STREAMING_ENABLED;
1821        atomic_set(&asd->sof_count, -1);
1822        atomic_set(&asd->sequence, -1);
1823        atomic_set(&asd->sequence_temp, -1);
1824        if (isp->sw_contex.file_input)
1825                wdt_duration = ATOMISP_ISP_FILE_TIMEOUT_DURATION;
1826
1827        asd->params.dis_proj_data_valid = false;
1828        asd->latest_preview_exp_id = 0;
1829        asd->postview_exp_id = 1;
1830        asd->preview_exp_id = 1;
1831
1832        /* handle per_frame_setting parameter and buffers */
1833        atomisp_handle_parameter_and_buffer(pipe);
1834
1835        atomisp_qbuffers_to_css(asd);
1836
1837        /* Only start sensor when the last streaming instance started */
1838        if (atomisp_subdev_streaming_count(asd) < sensor_start_stream)
1839                goto out;
1840
1841start_sensor:
1842        if (isp->flash) {
1843                asd->params.num_flash_frames = 0;
1844                asd->params.flash_state = ATOMISP_FLASH_IDLE;
1845                atomisp_setup_flash(asd);
1846        }
1847
1848        if (!isp->sw_contex.file_input) {
1849                atomisp_css_irq_enable(isp, CSS_IRQ_INFO_CSS_RECEIVER_SOF,
1850                                atomisp_css_valid_sof(isp));
1851                atomisp_csi2_configure(asd);
1852                /*
1853                 * set freq to max when streaming count > 1 which indicate
1854                 * dual camera would run
1855                */
1856                if (atomisp_streaming_count(isp) > 1) {
1857                        if (atomisp_freq_scaling(isp,
1858                                ATOMISP_DFS_MODE_MAX, false) < 0)
1859                                dev_dbg(isp->dev, "dfs failed!\n");
1860                } else {
1861                        if (atomisp_freq_scaling(isp,
1862                                ATOMISP_DFS_MODE_AUTO, false) < 0)
1863                                dev_dbg(isp->dev, "dfs failed!\n");
1864                }
1865        } else {
1866                if (atomisp_freq_scaling(isp, ATOMISP_DFS_MODE_MAX, false) < 0)
1867                        dev_dbg(isp->dev, "dfs failed!\n");
1868        }
1869
1870        if (asd->depth_mode->val && atomisp_streaming_count(isp) ==
1871                        ATOMISP_DEPTH_SENSOR_STREAMON_COUNT) {
1872                ret = atomisp_stream_on_master_slave_sensor(isp, false);
1873                if (ret) {
1874                        dev_err(isp->dev, "master slave sensor stream on failed!\n");
1875                        goto out;
1876                }
1877#ifndef ISP2401
1878                __wdt_on_master_slave_sensor(isp, wdt_duration);
1879#else
1880                __wdt_on_master_slave_sensor(pipe, wdt_duration, true);
1881#endif
1882                goto start_delay_wq;
1883        } else if (asd->depth_mode->val && (atomisp_streaming_count(isp) <
1884                   ATOMISP_DEPTH_SENSOR_STREAMON_COUNT)) {
1885#ifdef ISP2401
1886                __wdt_on_master_slave_sensor(pipe, wdt_duration, false);
1887#endif
1888                goto start_delay_wq;
1889        }
1890
1891        /* Enable the CSI interface on ANN B0/K0 */
1892        if (isp->media_dev.hw_revision >= ((ATOMISP_HW_REVISION_ISP2401 <<
1893            ATOMISP_HW_REVISION_SHIFT) | ATOMISP_HW_STEPPING_B0)) {
1894                pci_write_config_word(isp->pdev, MRFLD_PCI_CSI_CONTROL,
1895                                      isp->saved_regs.csi_control |
1896                                      MRFLD_PCI_CSI_CONTROL_CSI_READY);
1897        }
1898
1899        /* stream on the sensor */
1900        ret = v4l2_subdev_call(isp->inputs[asd->input_curr].camera,
1901                               video, s_stream, 1);
1902        if (ret) {
1903                asd->streaming = ATOMISP_DEVICE_STREAMING_DISABLED;
1904                ret = -EINVAL;
1905                goto out;
1906        }
1907
1908#ifndef ISP2401
1909        if (atomisp_buffers_queued(asd))
1910                atomisp_wdt_refresh(asd, wdt_duration);
1911#else
1912        if (atomisp_buffers_queued_pipe(pipe))
1913                atomisp_wdt_refresh_pipe(pipe, wdt_duration);
1914#endif
1915
1916start_delay_wq:
1917        if (asd->continuous_mode->val) {
1918                struct v4l2_mbus_framefmt *sink;
1919
1920                sink = atomisp_subdev_get_ffmt(&asd->subdev, NULL,
1921                                       V4L2_SUBDEV_FORMAT_ACTIVE,
1922                                       ATOMISP_SUBDEV_PAD_SINK);
1923
1924                reinit_completion(&asd->init_done);
1925                asd->delayed_init = ATOMISP_DELAYED_INIT_QUEUED;
1926                queue_work(asd->delayed_init_workq, &asd->delayed_init_work);
1927                atomisp_css_set_cont_prev_start_time(isp,
1928                                ATOMISP_CALC_CSS_PREV_OVERLAP(sink->height));
1929        } else {
1930                asd->delayed_init = ATOMISP_DELAYED_INIT_NOT_QUEUED;
1931        }
1932out:
1933        rt_mutex_unlock(&isp->mutex);
1934        return ret;
1935}
1936
1937int __atomisp_streamoff(struct file *file, void *fh, enum v4l2_buf_type type)
1938{
1939        struct video_device *vdev = video_devdata(file);
1940        struct atomisp_device *isp = video_get_drvdata(vdev);
1941        struct atomisp_video_pipe *pipe = atomisp_to_video_pipe(vdev);
1942        struct atomisp_sub_device *asd = pipe->asd;
1943        struct atomisp_video_pipe *capture_pipe = NULL;
1944        struct atomisp_video_pipe *vf_pipe = NULL;
1945        struct atomisp_video_pipe *preview_pipe = NULL;
1946        struct atomisp_video_pipe *video_pipe = NULL;
1947        struct videobuf_buffer *vb, *_vb;
1948        enum atomisp_css_pipe_id css_pipe_id;
1949        int ret;
1950        unsigned long flags;
1951        bool first_streamoff = false;
1952
1953        dev_dbg(isp->dev, "Stop stream on pad %d for asd%d\n",
1954                atomisp_subdev_source_pad(vdev), asd->index);
1955
1956        BUG_ON(!rt_mutex_is_locked(&isp->mutex));
1957        BUG_ON(!mutex_is_locked(&isp->streamoff_mutex));
1958
1959        if (type != V4L2_BUF_TYPE_VIDEO_CAPTURE) {
1960                dev_dbg(isp->dev, "unsupported v4l2 buf type\n");
1961                return -EINVAL;
1962        }
1963
1964        /*
1965         * do only videobuf_streamoff for capture & vf pipes in
1966         * case of continuous capture
1967         */
1968        if ((asd->continuous_mode->val ||
1969            isp->inputs[asd->input_curr].camera_caps->multi_stream_ctrl) &&
1970            atomisp_subdev_source_pad(vdev) !=
1971                ATOMISP_SUBDEV_PAD_SOURCE_PREVIEW &&
1972            atomisp_subdev_source_pad(vdev) !=
1973                ATOMISP_SUBDEV_PAD_SOURCE_VIDEO) {
1974
1975                if (isp->inputs[asd->input_curr].camera_caps->multi_stream_ctrl) {
1976                        v4l2_subdev_call(isp->inputs[asd->input_curr].camera,
1977                                video, s_stream, 0);
1978                } else if (atomisp_subdev_source_pad(vdev)
1979                    == ATOMISP_SUBDEV_PAD_SOURCE_CAPTURE) {
1980                        /* stop continuous still capture if needed */
1981                        if (asd->params.offline_parm.num_captures == -1)
1982                                atomisp_css_offline_capture_configure(asd,
1983                                                0, 0, 0);
1984                        atomisp_freq_scaling(isp, ATOMISP_DFS_MODE_AUTO, false);
1985                }
1986                /*
1987                 * Currently there is no way to flush buffers queued to css.
1988                 * When doing videobuf_streamoff, active buffers will be
1989                 * marked as VIDEOBUF_NEEDS_INIT. HAL will be able to use
1990                 * these buffers again, and these buffers might be queued to
1991                 * css more than once! Warn here, if HAL has not dequeued all
1992                 * buffers back before calling streamoff.
1993                 */
1994                if (pipe->buffers_in_css != 0) {
1995                        WARN(1, "%s: buffers of vdev %s still in CSS!\n",
1996                             __func__, pipe->vdev.name);
1997
1998                        /*
1999                         * Buffers remained in css maybe dequeued out in the
2000                         * next stream on, while this will causes serious
2001                         * issues as buffers already get invalid after
2002                         * previous stream off.
2003                         *
2004                         * No way to flush buffers but to reset the whole css
2005                         */
2006                        dev_warn(isp->dev, "Reset CSS to clean up css buffers.\n");
2007                        atomisp_css_flush(isp);
2008                }
2009
2010                return videobuf_streamoff(&pipe->capq);
2011        }
2012
2013        if (!pipe->capq.streaming)
2014                return 0;
2015
2016        spin_lock_irqsave(&isp->lock, flags);
2017        if (asd->streaming == ATOMISP_DEVICE_STREAMING_ENABLED) {
2018                asd->streaming = ATOMISP_DEVICE_STREAMING_STOPPING;
2019                first_streamoff = true;
2020        }
2021        spin_unlock_irqrestore(&isp->lock, flags);
2022
2023        if (first_streamoff) {
2024                /* if other streams are running, should not disable watch dog */
2025                rt_mutex_unlock(&isp->mutex);
2026                atomisp_wdt_stop(asd, true);
2027
2028                /*
2029                 * must stop sending pixels into GP_FIFO before stop
2030                 * the pipeline.
2031                 */
2032                if (isp->sw_contex.file_input)
2033                        v4l2_subdev_call(isp->inputs[asd->input_curr].camera,
2034                                        video, s_stream, 0);
2035
2036                rt_mutex_lock(&isp->mutex);
2037                atomisp_acc_unload_extensions(asd);
2038        }
2039
2040        spin_lock_irqsave(&isp->lock, flags);
2041        if (atomisp_subdev_streaming_count(asd) == 1)
2042                asd->streaming = ATOMISP_DEVICE_STREAMING_DISABLED;
2043        spin_unlock_irqrestore(&isp->lock, flags);
2044
2045        if (!first_streamoff) {
2046                ret = videobuf_streamoff(&pipe->capq);
2047                if (ret)
2048                        return ret;
2049                goto stopsensor;
2050        }
2051
2052        atomisp_clear_css_buffer_counters(asd);
2053
2054        if (!isp->sw_contex.file_input)
2055                atomisp_css_irq_enable(isp, CSS_IRQ_INFO_CSS_RECEIVER_SOF,
2056                                        false);
2057
2058        if (asd->delayed_init == ATOMISP_DELAYED_INIT_QUEUED) {
2059                cancel_work_sync(&asd->delayed_init_work);
2060                asd->delayed_init = ATOMISP_DELAYED_INIT_NOT_QUEUED;
2061        }
2062        if (first_streamoff) {
2063                css_pipe_id = atomisp_get_css_pipe_id(asd);
2064                ret = atomisp_css_stop(asd, css_pipe_id, false);
2065        }
2066        /* cancel work queue*/
2067        if (asd->video_out_capture.users) {
2068                capture_pipe = &asd->video_out_capture;
2069                wake_up_interruptible(&capture_pipe->capq.wait);
2070        }
2071        if (asd->video_out_vf.users) {
2072                vf_pipe = &asd->video_out_vf;
2073                wake_up_interruptible(&vf_pipe->capq.wait);
2074        }
2075        if (asd->video_out_preview.users) {
2076                preview_pipe = &asd->video_out_preview;
2077                wake_up_interruptible(&preview_pipe->capq.wait);
2078        }
2079        if (asd->video_out_video_capture.users) {
2080                video_pipe = &asd->video_out_video_capture;
2081                wake_up_interruptible(&video_pipe->capq.wait);
2082        }
2083        ret = videobuf_streamoff(&pipe->capq);
2084        if (ret)
2085                return ret;
2086
2087        /* cleanup css here */
2088        /* no need for this, as ISP will be reset anyway */
2089        /*atomisp_flush_bufs_in_css(isp);*/
2090
2091        spin_lock_irqsave(&pipe->irq_lock, flags);
2092        list_for_each_entry_safe(vb, _vb, &pipe->activeq, queue) {
2093                vb->state = VIDEOBUF_PREPARED;
2094                list_del(&vb->queue);
2095        }
2096        list_for_each_entry_safe(vb, _vb, &pipe->buffers_waiting_for_param, queue) {
2097                vb->state = VIDEOBUF_PREPARED;
2098                list_del(&vb->queue);
2099                pipe->frame_request_config_id[vb->i] = 0;
2100        }
2101        spin_unlock_irqrestore(&pipe->irq_lock, flags);
2102
2103        atomisp_subdev_cleanup_pending_events(asd);
2104stopsensor:
2105        if (atomisp_subdev_streaming_count(asd) + 1
2106            != atomisp_sensor_start_stream(asd))
2107                return 0;
2108
2109        if (!isp->sw_contex.file_input)
2110                ret = v4l2_subdev_call(isp->inputs[asd->input_curr].camera,
2111                                       video, s_stream, 0);
2112
2113        if (isp->flash) {
2114                asd->params.num_flash_frames = 0;
2115                asd->params.flash_state = ATOMISP_FLASH_IDLE;
2116        }
2117
2118        /* if other streams are running, isp should not be powered off */
2119        if (atomisp_streaming_count(isp)) {
2120                atomisp_css_flush(isp);
2121                return 0;
2122        }
2123
2124        /* Disable the CSI interface on ANN B0/K0 */
2125        if (isp->media_dev.hw_revision >= ((ATOMISP_HW_REVISION_ISP2401 <<
2126            ATOMISP_HW_REVISION_SHIFT) | ATOMISP_HW_STEPPING_B0)) {
2127                pci_write_config_word(isp->pdev, MRFLD_PCI_CSI_CONTROL,
2128                                      isp->saved_regs.csi_control &
2129                                      ~MRFLD_PCI_CSI_CONTROL_CSI_READY);
2130        }
2131
2132        if (atomisp_freq_scaling(isp, ATOMISP_DFS_MODE_LOW, false))
2133                dev_warn(isp->dev, "DFS failed.\n");
2134        /*
2135         * ISP work around, need to reset isp
2136         * Is it correct time to reset ISP when first node does streamoff?
2137         */
2138        if (isp->sw_contex.power_state == ATOM_ISP_POWER_UP) {
2139                unsigned int i;
2140                bool recreate_streams[MAX_STREAM_NUM] = {0};
2141                if (isp->isp_timeout)
2142                        dev_err(isp->dev, "%s: Resetting with WA activated",
2143                                __func__);
2144                /*
2145                 * It is possible that the other asd stream is in the stage
2146                 * that v4l2_setfmt is just get called on it, which will
2147                 * create css stream on that stream. But at this point, there
2148                 * is no way to destroy the css stream created on that stream.
2149                 *
2150                 * So force stream destroy here.
2151                 */
2152                for (i = 0; i < isp->num_of_streams; i++) {
2153                        if (isp->asd[i].stream_prepared) {
2154                                atomisp_destroy_pipes_stream_force(&isp->
2155                                                asd[i]);
2156                                recreate_streams[i] = true;
2157                        }
2158                }
2159
2160                /* disable  PUNIT/ISP acknowlede/handshake - SRSE=3 */
2161                pci_write_config_dword(isp->pdev, PCI_I_CONTROL, isp->saved_regs.i_control |
2162                                MRFLD_PCI_I_CONTROL_SRSE_RESET_MASK);
2163                dev_err(isp->dev, "atomisp_reset");
2164                atomisp_reset(isp);
2165                for (i = 0; i < isp->num_of_streams; i++) {
2166                        if (recreate_streams[i])
2167                                atomisp_create_pipes_stream(&isp->asd[i]);
2168                }
2169                isp->isp_timeout = false;
2170        }
2171        return ret;
2172}
2173
2174static int atomisp_streamoff(struct file *file, void *fh,
2175                             enum v4l2_buf_type type)
2176{
2177        struct video_device *vdev = video_devdata(file);
2178        struct atomisp_device *isp = video_get_drvdata(vdev);
2179        int rval;
2180
2181        mutex_lock(&isp->streamoff_mutex);
2182        rt_mutex_lock(&isp->mutex);
2183        rval = __atomisp_streamoff(file, fh, type);
2184        rt_mutex_unlock(&isp->mutex);
2185        mutex_unlock(&isp->streamoff_mutex);
2186
2187        return rval;
2188}
2189
2190/*
2191 * To get the current value of a control.
2192 * applications initialize the id field of a struct v4l2_control and
2193 * call this ioctl with a pointer to this structure
2194 */
2195static int atomisp_g_ctrl(struct file *file, void *fh,
2196        struct v4l2_control *control)
2197{
2198        struct video_device *vdev = video_devdata(file);
2199        struct atomisp_sub_device *asd = atomisp_to_video_pipe(vdev)->asd;
2200        struct atomisp_device *isp = video_get_drvdata(vdev);
2201        int i, ret = -EINVAL;
2202
2203        for (i = 0; i < ctrls_num; i++) {
2204                if (ci_v4l2_controls[i].id == control->id) {
2205                        ret = 0;
2206                        break;
2207                }
2208        }
2209
2210        if (ret)
2211                return ret;
2212
2213        rt_mutex_lock(&isp->mutex);
2214
2215        switch (control->id) {
2216        case V4L2_CID_IRIS_ABSOLUTE:
2217        case V4L2_CID_EXPOSURE_ABSOLUTE:
2218        case V4L2_CID_FNUMBER_ABSOLUTE:
2219        case V4L2_CID_2A_STATUS:
2220        case V4L2_CID_AUTO_N_PRESET_WHITE_BALANCE:
2221        case V4L2_CID_EXPOSURE:
2222        case V4L2_CID_EXPOSURE_AUTO:
2223        case V4L2_CID_SCENE_MODE:
2224        case V4L2_CID_ISO_SENSITIVITY:
2225        case V4L2_CID_ISO_SENSITIVITY_AUTO:
2226        case V4L2_CID_CONTRAST:
2227        case V4L2_CID_SATURATION:
2228        case V4L2_CID_SHARPNESS:
2229        case V4L2_CID_3A_LOCK:
2230        case V4L2_CID_EXPOSURE_ZONE_NUM:
2231        case V4L2_CID_TEST_PATTERN:
2232        case V4L2_CID_TEST_PATTERN_COLOR_R:
2233        case V4L2_CID_TEST_PATTERN_COLOR_GR:
2234        case V4L2_CID_TEST_PATTERN_COLOR_GB:
2235        case V4L2_CID_TEST_PATTERN_COLOR_B:
2236                rt_mutex_unlock(&isp->mutex);
2237                return v4l2_g_ctrl(isp->inputs[asd->input_curr].camera->
2238                                   ctrl_handler, control);
2239        case V4L2_CID_COLORFX:
2240                ret = atomisp_color_effect(asd, 0, &control->value);
2241                break;
2242        case V4L2_CID_ATOMISP_BAD_PIXEL_DETECTION:
2243                ret = atomisp_bad_pixel(asd, 0, &control->value);
2244                break;
2245        case V4L2_CID_ATOMISP_POSTPROCESS_GDC_CAC:
2246                ret = atomisp_gdc_cac(asd, 0, &control->value);
2247                break;
2248        case V4L2_CID_ATOMISP_VIDEO_STABLIZATION:
2249                ret = atomisp_video_stable(asd, 0, &control->value);
2250                break;
2251        case V4L2_CID_ATOMISP_FIXED_PATTERN_NR:
2252                ret = atomisp_fixed_pattern(asd, 0, &control->value);
2253                break;
2254        case V4L2_CID_ATOMISP_FALSE_COLOR_CORRECTION:
2255                ret = atomisp_false_color(asd, 0, &control->value);
2256                break;
2257        case V4L2_CID_ATOMISP_LOW_LIGHT:
2258                ret = atomisp_low_light(asd, 0, &control->value);
2259                break;
2260        default:
2261                ret = -EINVAL;
2262                break;
2263        }
2264
2265        rt_mutex_unlock(&isp->mutex);
2266        return ret;
2267}
2268
2269/*
2270 * To change the value of a control.
2271 * applications initialize the id and value fields of a struct v4l2_control
2272 * and call this ioctl.
2273 */
2274static int atomisp_s_ctrl(struct file *file, void *fh,
2275                          struct v4l2_control *control)
2276{
2277        struct video_device *vdev = video_devdata(file);
2278        struct atomisp_sub_device *asd = atomisp_to_video_pipe(vdev)->asd;
2279        struct atomisp_device *isp = video_get_drvdata(vdev);
2280        int i, ret = -EINVAL;
2281
2282        for (i = 0; i < ctrls_num; i++) {
2283                if (ci_v4l2_controls[i].id == control->id) {
2284                        ret = 0;
2285                        break;
2286                }
2287        }
2288
2289        if (ret)
2290                return ret;
2291
2292        rt_mutex_lock(&isp->mutex);
2293        switch (control->id) {
2294        case V4L2_CID_AUTO_N_PRESET_WHITE_BALANCE:
2295        case V4L2_CID_EXPOSURE:
2296        case V4L2_CID_EXPOSURE_AUTO:
2297        case V4L2_CID_EXPOSURE_AUTO_PRIORITY:
2298        case V4L2_CID_SCENE_MODE:
2299        case V4L2_CID_ISO_SENSITIVITY:
2300        case V4L2_CID_ISO_SENSITIVITY_AUTO:
2301        case V4L2_CID_POWER_LINE_FREQUENCY:
2302        case V4L2_CID_EXPOSURE_METERING:
2303        case V4L2_CID_CONTRAST:
2304        case V4L2_CID_SATURATION:
2305        case V4L2_CID_SHARPNESS:
2306        case V4L2_CID_3A_LOCK:
2307        case V4L2_CID_COLORFX_CBCR:
2308        case V4L2_CID_TEST_PATTERN:
2309        case V4L2_CID_TEST_PATTERN_COLOR_R:
2310        case V4L2_CID_TEST_PATTERN_COLOR_GR:
2311        case V4L2_CID_TEST_PATTERN_COLOR_GB:
2312        case V4L2_CID_TEST_PATTERN_COLOR_B:
2313                rt_mutex_unlock(&isp->mutex);
2314                return v4l2_s_ctrl(NULL,
2315                                   isp->inputs[asd->input_curr].camera->
2316                                   ctrl_handler, control);
2317        case V4L2_CID_COLORFX:
2318                ret = atomisp_color_effect(asd, 1, &control->value);
2319                break;
2320        case V4L2_CID_ATOMISP_BAD_PIXEL_DETECTION:
2321                ret = atomisp_bad_pixel(asd, 1, &control->value);
2322                break;
2323        case V4L2_CID_ATOMISP_POSTPROCESS_GDC_CAC:
2324                ret = atomisp_gdc_cac(asd, 1, &control->value);
2325                break;
2326        case V4L2_CID_ATOMISP_VIDEO_STABLIZATION:
2327                ret = atomisp_video_stable(asd, 1, &control->value);
2328                break;
2329        case V4L2_CID_ATOMISP_FIXED_PATTERN_NR:
2330                ret = atomisp_fixed_pattern(asd, 1, &control->value);
2331                break;
2332        case V4L2_CID_ATOMISP_FALSE_COLOR_CORRECTION:
2333                ret = atomisp_false_color(asd, 1, &control->value);
2334                break;
2335        case V4L2_CID_REQUEST_FLASH:
2336                ret = atomisp_flash_enable(asd, control->value);
2337                break;
2338        case V4L2_CID_ATOMISP_LOW_LIGHT:
2339                ret = atomisp_low_light(asd, 1, &control->value);
2340                break;
2341        default:
2342                ret = -EINVAL;
2343                break;
2344        }
2345        rt_mutex_unlock(&isp->mutex);
2346        return ret;
2347}
2348/*
2349 * To query the attributes of a control.
2350 * applications set the id field of a struct v4l2_queryctrl and call the
2351 * this ioctl with a pointer to this structure. The driver fills
2352 * the rest of the structure.
2353 */
2354static int atomisp_queryctl(struct file *file, void *fh,
2355                            struct v4l2_queryctrl *qc)
2356{
2357        int i, ret = -EINVAL;
2358        struct video_device *vdev = video_devdata(file);
2359        struct atomisp_sub_device *asd = atomisp_to_video_pipe(vdev)->asd;
2360        struct atomisp_device *isp = video_get_drvdata(vdev);
2361
2362        switch (qc->id) {
2363        case V4L2_CID_FOCUS_ABSOLUTE:
2364        case V4L2_CID_FOCUS_RELATIVE:
2365        case V4L2_CID_FOCUS_STATUS:
2366#ifndef ISP2401
2367                return v4l2_queryctrl(isp->inputs[asd->input_curr].camera->
2368                                      ctrl_handler, qc);
2369#else
2370                if (isp->motor)
2371                        return v4l2_queryctrl(isp->motor->ctrl_handler, qc);
2372                else
2373                        return v4l2_queryctrl(isp->inputs[asd->input_curr].
2374                                              camera->ctrl_handler, qc);
2375#endif
2376        }
2377
2378        if (qc->id & V4L2_CTRL_FLAG_NEXT_CTRL)
2379                return ret;
2380
2381        for (i = 0; i < ctrls_num; i++) {
2382                if (ci_v4l2_controls[i].id == qc->id) {
2383                        memcpy(qc, &ci_v4l2_controls[i],
2384                               sizeof(struct v4l2_queryctrl));
2385                        qc->reserved[0] = 0;
2386                        ret = 0;
2387                        break;
2388                }
2389        }
2390        if (ret != 0)
2391                qc->flags = V4L2_CTRL_FLAG_DISABLED;
2392
2393        return ret;
2394}
2395
2396static int atomisp_camera_g_ext_ctrls(struct file *file, void *fh,
2397        struct v4l2_ext_controls *c)
2398{
2399        struct video_device *vdev = video_devdata(file);
2400        struct atomisp_sub_device *asd = atomisp_to_video_pipe(vdev)->asd;
2401        struct atomisp_device *isp = video_get_drvdata(vdev);
2402        struct v4l2_control ctrl;
2403        int i;
2404        int ret = 0;
2405
2406        for (i = 0; i < c->count; i++) {
2407                ctrl.id = c->controls[i].id;
2408                ctrl.value = c->controls[i].value;
2409                switch (ctrl.id) {
2410                case V4L2_CID_EXPOSURE_ABSOLUTE:
2411                case V4L2_CID_EXPOSURE_AUTO:
2412                case V4L2_CID_IRIS_ABSOLUTE:
2413                case V4L2_CID_FNUMBER_ABSOLUTE:
2414                case V4L2_CID_BIN_FACTOR_HORZ:
2415                case V4L2_CID_BIN_FACTOR_VERT:
2416                case V4L2_CID_3A_LOCK:
2417                case V4L2_CID_TEST_PATTERN:
2418                case V4L2_CID_TEST_PATTERN_COLOR_R:
2419                case V4L2_CID_TEST_PATTERN_COLOR_GR:
2420                case V4L2_CID_TEST_PATTERN_COLOR_GB:
2421                case V4L2_CID_TEST_PATTERN_COLOR_B:
2422                        /*
2423                         * Exposure related control will be handled by sensor
2424                         * driver
2425                         */
2426                        ret =
2427                            v4l2_g_ctrl(isp->inputs[asd->input_curr].camera->
2428                                        ctrl_handler, &ctrl);
2429                        break;
2430                case V4L2_CID_FOCUS_ABSOLUTE:
2431                case V4L2_CID_FOCUS_RELATIVE:
2432                case V4L2_CID_FOCUS_STATUS:
2433                case V4L2_CID_FOCUS_AUTO:
2434#ifndef ISP2401
2435                        if (isp->inputs[asd->input_curr].motor)
2436#else
2437                        if (isp->motor)
2438#endif
2439                                ret =
2440#ifndef ISP2401
2441                                    v4l2_g_ctrl(isp->inputs[asd->input_curr].
2442                                                motor->ctrl_handler, &ctrl);
2443#else
2444                                    v4l2_g_ctrl(isp->motor->ctrl_handler,
2445                                                &ctrl);
2446#endif
2447                        else
2448                                ret =
2449                                    v4l2_g_ctrl(isp->inputs[asd->input_curr].
2450                                                camera->ctrl_handler, &ctrl);
2451                        break;
2452                case V4L2_CID_FLASH_STATUS:
2453                case V4L2_CID_FLASH_INTENSITY:
2454                case V4L2_CID_FLASH_TORCH_INTENSITY:
2455                case V4L2_CID_FLASH_INDICATOR_INTENSITY:
2456                case V4L2_CID_FLASH_TIMEOUT:
2457                case V4L2_CID_FLASH_STROBE:
2458                case V4L2_CID_FLASH_MODE:
2459                case V4L2_CID_FLASH_STATUS_REGISTER:
2460                        if (isp->flash)
2461                                ret =
2462                                    v4l2_g_ctrl(isp->flash->ctrl_handler,
2463                                                &ctrl);
2464                        break;
2465                case V4L2_CID_ZOOM_ABSOLUTE:
2466                        rt_mutex_lock(&isp->mutex);
2467                        ret = atomisp_digital_zoom(asd, 0, &ctrl.value);
2468                        rt_mutex_unlock(&isp->mutex);
2469                        break;
2470                case V4L2_CID_G_SKIP_FRAMES:
2471                        ret = v4l2_subdev_call(
2472                                isp->inputs[asd->input_curr].camera,
2473                                sensor, g_skip_frames, (u32 *)&ctrl.value);
2474                        break;
2475                default:
2476                        ret = -EINVAL;
2477                }
2478
2479                if (ret) {
2480                        c->error_idx = i;
2481                        break;
2482                }
2483                c->controls[i].value = ctrl.value;
2484        }
2485        return ret;
2486}
2487
2488/* This ioctl allows the application to get multiple controls by class */
2489static int atomisp_g_ext_ctrls(struct file *file, void *fh,
2490        struct v4l2_ext_controls *c)
2491{
2492        struct v4l2_control ctrl;
2493        int i, ret = 0;
2494
2495        /* input_lock is not need for the Camera releated IOCTLs
2496         * The input_lock downgrade the FPS of 3A*/
2497        ret = atomisp_camera_g_ext_ctrls(file, fh, c);
2498        if (ret != -EINVAL)
2499                return ret;
2500
2501        for (i = 0; i < c->count; i++) {
2502                ctrl.id = c->controls[i].id;
2503                ctrl.value = c->controls[i].value;
2504                ret = atomisp_g_ctrl(file, fh, &ctrl);
2505                c->controls[i].value = ctrl.value;
2506                if (ret) {
2507                        c->error_idx = i;
2508                        break;
2509                }
2510        }
2511        return ret;
2512}
2513
2514static int atomisp_camera_s_ext_ctrls(struct file *file, void *fh,
2515        struct v4l2_ext_controls *c)
2516{
2517        struct video_device *vdev = video_devdata(file);
2518        struct atomisp_sub_device *asd = atomisp_to_video_pipe(vdev)->asd;
2519        struct atomisp_device *isp = video_get_drvdata(vdev);
2520        struct v4l2_control ctrl;
2521        int i;
2522        int ret = 0;
2523
2524        for (i = 0; i < c->count; i++) {
2525                struct v4l2_ctrl *ctr;
2526
2527                ctrl.id = c->controls[i].id;
2528                ctrl.value = c->controls[i].value;
2529                switch (ctrl.id) {
2530                case V4L2_CID_EXPOSURE_ABSOLUTE:
2531                case V4L2_CID_EXPOSURE_AUTO:
2532                case V4L2_CID_EXPOSURE_METERING:
2533                case V4L2_CID_IRIS_ABSOLUTE:
2534                case V4L2_CID_FNUMBER_ABSOLUTE:
2535                case V4L2_CID_VCM_TIMEING:
2536                case V4L2_CID_VCM_SLEW:
2537                case V4L2_CID_3A_LOCK:
2538                case V4L2_CID_TEST_PATTERN:
2539                case V4L2_CID_TEST_PATTERN_COLOR_R:
2540                case V4L2_CID_TEST_PATTERN_COLOR_GR:
2541                case V4L2_CID_TEST_PATTERN_COLOR_GB:
2542                case V4L2_CID_TEST_PATTERN_COLOR_B:
2543                        ret = v4l2_s_ctrl(NULL,
2544                                          isp->inputs[asd->input_curr].camera->
2545                                          ctrl_handler, &ctrl);
2546                        break;
2547                case V4L2_CID_FOCUS_ABSOLUTE:
2548                case V4L2_CID_FOCUS_RELATIVE:
2549                case V4L2_CID_FOCUS_STATUS:
2550                case V4L2_CID_FOCUS_AUTO:
2551#ifndef ISP2401
2552                        if (isp->inputs[asd->input_curr].motor)
2553#else
2554                        if (isp->motor)
2555#endif
2556                                ret = v4l2_s_ctrl(NULL,
2557#ifndef ISP2401
2558                                                  isp->inputs[asd->input_curr].
2559                                                  motor->ctrl_handler, &ctrl);
2560#else
2561                                                  isp->motor->ctrl_handler,
2562                                                  &ctrl);
2563#endif
2564                        else
2565                                ret = v4l2_s_ctrl(NULL,
2566                                                  isp->inputs[asd->input_curr].
2567                                                  camera->ctrl_handler, &ctrl);
2568                        break;
2569                case V4L2_CID_FLASH_STATUS:
2570                case V4L2_CID_FLASH_INTENSITY:
2571                case V4L2_CID_FLASH_TORCH_INTENSITY:
2572                case V4L2_CID_FLASH_INDICATOR_INTENSITY:
2573                case V4L2_CID_FLASH_TIMEOUT:
2574                case V4L2_CID_FLASH_STROBE:
2575                case V4L2_CID_FLASH_MODE:
2576                case V4L2_CID_FLASH_STATUS_REGISTER:
2577                        rt_mutex_lock(&isp->mutex);
2578                        if (isp->flash) {
2579                                ret =
2580                                    v4l2_s_ctrl(NULL, isp->flash->ctrl_handler,
2581                                                &ctrl);
2582                                /* When flash mode is changed we need to reset
2583                                 * flash state */
2584                                if (ctrl.id == V4L2_CID_FLASH_MODE) {
2585                                        asd->params.flash_state =
2586                                                ATOMISP_FLASH_IDLE;
2587                                        asd->params.num_flash_frames = 0;
2588                                }
2589                        }
2590                        rt_mutex_unlock(&isp->mutex);
2591                        break;
2592                case V4L2_CID_ZOOM_ABSOLUTE:
2593                        rt_mutex_lock(&isp->mutex);
2594                        ret = atomisp_digital_zoom(asd, 1, &ctrl.value);
2595                        rt_mutex_unlock(&isp->mutex);
2596                        break;
2597                default:
2598                        ctr = v4l2_ctrl_find(&asd->ctrl_handler, ctrl.id);
2599                        if (ctr)
2600                                ret = v4l2_ctrl_s_ctrl(ctr, ctrl.value);
2601                        else
2602                                ret = -EINVAL;
2603                }
2604
2605                if (ret) {
2606                        c->error_idx = i;
2607                        break;
2608                }
2609                c->controls[i].value = ctrl.value;
2610        }
2611        return ret;
2612}
2613
2614/* This ioctl allows the application to set multiple controls by class */
2615static int atomisp_s_ext_ctrls(struct file *file, void *fh,
2616        struct v4l2_ext_controls *c)
2617{
2618        struct v4l2_control ctrl;
2619        int i, ret = 0;
2620
2621        /* input_lock is not need for the Camera releated IOCTLs
2622         * The input_lock downgrade the FPS of 3A*/
2623        ret = atomisp_camera_s_ext_ctrls(file, fh, c);
2624        if (ret != -EINVAL)
2625                return ret;
2626
2627        for (i = 0; i < c->count; i++) {
2628                ctrl.id = c->controls[i].id;
2629                ctrl.value = c->controls[i].value;
2630                ret = atomisp_s_ctrl(file, fh, &ctrl);
2631                c->controls[i].value = ctrl.value;
2632                if (ret) {
2633                        c->error_idx = i;
2634                        break;
2635                }
2636        }
2637        return ret;
2638}
2639
2640/*
2641 * vidioc_g/s_param are used to switch isp running mode
2642 */
2643static int atomisp_g_parm(struct file *file, void *fh,
2644        struct v4l2_streamparm *parm)
2645{
2646        struct video_device *vdev = video_devdata(file);
2647        struct atomisp_sub_device *asd = atomisp_to_video_pipe(vdev)->asd;
2648        struct atomisp_device *isp = video_get_drvdata(vdev);
2649
2650        if (parm->type != V4L2_BUF_TYPE_VIDEO_CAPTURE) {
2651                dev_err(isp->dev, "unsupport v4l2 buf type\n");
2652                return -EINVAL;
2653        }
2654
2655        rt_mutex_lock(&isp->mutex);
2656        parm->parm.capture.capturemode = asd->run_mode->val;
2657        rt_mutex_unlock(&isp->mutex);
2658
2659        return 0;
2660}
2661
2662static int atomisp_s_parm(struct file *file, void *fh,
2663        struct v4l2_streamparm *parm)
2664{
2665        struct video_device *vdev = video_devdata(file);
2666        struct atomisp_device *isp = video_get_drvdata(vdev);
2667        struct atomisp_sub_device *asd = atomisp_to_video_pipe(vdev)->asd;
2668        int mode;
2669        int rval;
2670        int fps;
2671
2672        if (parm->type != V4L2_BUF_TYPE_VIDEO_CAPTURE) {
2673                dev_err(isp->dev, "unsupport v4l2 buf type\n");
2674                return -EINVAL;
2675        }
2676
2677        rt_mutex_lock(&isp->mutex);
2678
2679        asd->high_speed_mode = false;
2680        switch (parm->parm.capture.capturemode) {
2681        case CI_MODE_NONE: {
2682                struct v4l2_subdev_frame_interval fi = {0};
2683
2684                fi.interval = parm->parm.capture.timeperframe;
2685
2686                rval = v4l2_subdev_call(isp->inputs[asd->input_curr].camera,
2687                                        video, s_frame_interval, &fi);
2688                if (!rval)
2689                        parm->parm.capture.timeperframe = fi.interval;
2690
2691                if (fi.interval.numerator != 0) {
2692                        fps = fi.interval.denominator / fi.interval.numerator;
2693                        if (fps > 30)
2694                                asd->high_speed_mode = true;
2695                }
2696
2697                goto out;
2698        }
2699        case CI_MODE_VIDEO:
2700                mode = ATOMISP_RUN_MODE_VIDEO;
2701                break;
2702        case CI_MODE_STILL_CAPTURE:
2703                mode = ATOMISP_RUN_MODE_STILL_CAPTURE;
2704                break;
2705        case CI_MODE_CONTINUOUS:
2706                mode = ATOMISP_RUN_MODE_CONTINUOUS_CAPTURE;
2707                break;
2708        case CI_MODE_PREVIEW:
2709                mode = ATOMISP_RUN_MODE_PREVIEW;
2710                break;
2711        default:
2712                rval = -EINVAL;
2713                goto out;
2714        }
2715
2716        rval = v4l2_ctrl_s_ctrl(asd->run_mode, mode);
2717
2718out:
2719        rt_mutex_unlock(&isp->mutex);
2720
2721        return rval == -ENOIOCTLCMD ? 0 : rval;
2722}
2723
2724static int atomisp_s_parm_file(struct file *file, void *fh,
2725                                struct v4l2_streamparm *parm)
2726{
2727        struct video_device *vdev = video_devdata(file);
2728        struct atomisp_device *isp = video_get_drvdata(vdev);
2729
2730        if (parm->type != V4L2_BUF_TYPE_VIDEO_OUTPUT) {
2731                dev_err(isp->dev, "unsupport v4l2 buf type for output\n");
2732                return -EINVAL;
2733        }
2734
2735        rt_mutex_lock(&isp->mutex);
2736        isp->sw_contex.file_input = true;
2737        rt_mutex_unlock(&isp->mutex);
2738
2739        return 0;
2740}
2741
2742static long atomisp_vidioc_default(struct file *file, void *fh,
2743        bool valid_prio, unsigned int cmd, void *arg)
2744{
2745        struct video_device *vdev = video_devdata(file);
2746        struct atomisp_device *isp = video_get_drvdata(vdev);
2747        struct atomisp_sub_device *asd;
2748        bool acc_node;
2749        int err;
2750
2751        acc_node = !strncmp(vdev->name, "ATOMISP ISP ACC",
2752                        sizeof(vdev->name));
2753        if (acc_node)
2754                asd = atomisp_to_acc_pipe(vdev)->asd;
2755        else
2756                asd = atomisp_to_video_pipe(vdev)->asd;
2757
2758        switch (cmd) {
2759        case ATOMISP_IOC_G_MOTOR_PRIV_INT_DATA:
2760        case ATOMISP_IOC_S_EXPOSURE:
2761        case ATOMISP_IOC_G_SENSOR_CALIBRATION_GROUP:
2762        case ATOMISP_IOC_G_SENSOR_PRIV_INT_DATA:
2763        case ATOMISP_IOC_EXT_ISP_CTRL:
2764        case ATOMISP_IOC_G_SENSOR_AE_BRACKETING_INFO:
2765        case ATOMISP_IOC_S_SENSOR_AE_BRACKETING_MODE:
2766        case ATOMISP_IOC_G_SENSOR_AE_BRACKETING_MODE:
2767        case ATOMISP_IOC_S_SENSOR_AE_BRACKETING_LUT:
2768        case ATOMISP_IOC_S_SENSOR_EE_CONFIG:
2769#ifdef ISP2401
2770        case ATOMISP_IOC_G_UPDATE_EXPOSURE:
2771#endif
2772                /* we do not need take isp->mutex for these IOCTLs */
2773                break;
2774        default:
2775                rt_mutex_lock(&isp->mutex);
2776                break;
2777        }
2778        switch (cmd) {
2779#ifdef ISP2401
2780        case ATOMISP_IOC_S_SENSOR_RUNMODE:
2781                err = atomisp_set_sensor_runmode(asd, arg);
2782                break;
2783
2784#endif
2785        case ATOMISP_IOC_G_XNR:
2786                err = atomisp_xnr(asd, 0, arg);
2787                break;
2788
2789        case ATOMISP_IOC_S_XNR:
2790                err = atomisp_xnr(asd, 1, arg);
2791                break;
2792
2793        case ATOMISP_IOC_G_NR:
2794                err = atomisp_nr(asd, 0, arg);
2795                break;
2796
2797        case ATOMISP_IOC_S_NR:
2798                err = atomisp_nr(asd, 1, arg);
2799                break;
2800
2801        case ATOMISP_IOC_G_TNR:
2802                err = atomisp_tnr(asd, 0, arg);
2803                break;
2804
2805        case ATOMISP_IOC_S_TNR:
2806                err = atomisp_tnr(asd, 1, arg);
2807                break;
2808
2809        case ATOMISP_IOC_G_BLACK_LEVEL_COMP:
2810                err = atomisp_black_level(asd, 0, arg);
2811                break;
2812
2813        case ATOMISP_IOC_S_BLACK_LEVEL_COMP:
2814                err = atomisp_black_level(asd, 1, arg);
2815                break;
2816
2817        case ATOMISP_IOC_G_EE:
2818                err = atomisp_ee(asd, 0, arg);
2819                break;
2820
2821        case ATOMISP_IOC_S_EE:
2822                err = atomisp_ee(asd, 1, arg);
2823                break;
2824
2825        case ATOMISP_IOC_G_DIS_STAT:
2826                err = atomisp_get_dis_stat(asd, arg);
2827                break;
2828
2829        case ATOMISP_IOC_G_DVS2_BQ_RESOLUTIONS:
2830                err = atomisp_get_dvs2_bq_resolutions(asd, arg);
2831                break;
2832
2833        case ATOMISP_IOC_S_DIS_COEFS:
2834                err = atomisp_css_cp_dvs2_coefs(asd, arg,
2835                                &asd->params.css_param, true);
2836                if (!err && arg)
2837                        asd->params.css_update_params_needed = true;
2838                break;
2839
2840        case ATOMISP_IOC_S_DIS_VECTOR:
2841                err = atomisp_cp_dvs_6axis_config(asd, arg,
2842                                &asd->params.css_param, true);
2843                if (!err && arg)
2844                        asd->params.css_update_params_needed = true;
2845                break;
2846
2847        case ATOMISP_IOC_G_ISP_PARM:
2848                err = atomisp_param(asd, 0, arg);
2849                break;
2850
2851        case ATOMISP_IOC_S_ISP_PARM:
2852                err = atomisp_param(asd, 1, arg);
2853                break;
2854
2855        case ATOMISP_IOC_G_3A_STAT:
2856                err = atomisp_3a_stat(asd, 0, arg);
2857                break;
2858
2859        case ATOMISP_IOC_G_ISP_GAMMA:
2860                err = atomisp_gamma(asd, 0, arg);
2861                break;
2862
2863        case ATOMISP_IOC_S_ISP_GAMMA:
2864                err = atomisp_gamma(asd, 1, arg);
2865                break;
2866
2867        case ATOMISP_IOC_G_ISP_GDC_TAB:
2868                err = atomisp_gdc_cac_table(asd, 0, arg);
2869                break;
2870
2871        case ATOMISP_IOC_S_ISP_GDC_TAB:
2872                err = atomisp_gdc_cac_table(asd, 1, arg);
2873                break;
2874
2875        case ATOMISP_IOC_G_ISP_MACC:
2876                err = atomisp_macc_table(asd, 0, arg);
2877                break;
2878
2879        case ATOMISP_IOC_S_ISP_MACC:
2880                err = atomisp_macc_table(asd, 1, arg);
2881                break;
2882
2883        case ATOMISP_IOC_G_ISP_BAD_PIXEL_DETECTION:
2884                err = atomisp_bad_pixel_param(asd, 0, arg);
2885                break;
2886
2887        case ATOMISP_IOC_S_ISP_BAD_PIXEL_DETECTION:
2888                err = atomisp_bad_pixel_param(asd, 1, arg);
2889                break;
2890
2891        case ATOMISP_IOC_G_ISP_FALSE_COLOR_CORRECTION:
2892                err = atomisp_false_color_param(asd, 0, arg);
2893                break;
2894
2895        case ATOMISP_IOC_S_ISP_FALSE_COLOR_CORRECTION:
2896                err = atomisp_false_color_param(asd, 1, arg);
2897                break;
2898
2899        case ATOMISP_IOC_G_ISP_CTC:
2900                err = atomisp_ctc(asd, 0, arg);
2901                break;
2902
2903        case ATOMISP_IOC_S_ISP_CTC:
2904                err = atomisp_ctc(asd, 1, arg);
2905                break;
2906
2907        case ATOMISP_IOC_G_ISP_WHITE_BALANCE:
2908                err = atomisp_white_balance_param(asd, 0, arg);
2909                break;
2910
2911        case ATOMISP_IOC_S_ISP_WHITE_BALANCE:
2912                err = atomisp_white_balance_param(asd, 1, arg);
2913                break;
2914
2915        case ATOMISP_IOC_G_3A_CONFIG:
2916                err = atomisp_3a_config_param(asd, 0, arg);
2917                break;
2918
2919        case ATOMISP_IOC_S_3A_CONFIG:
2920                err = atomisp_3a_config_param(asd, 1, arg);
2921                break;
2922
2923        case ATOMISP_IOC_S_ISP_FPN_TABLE:
2924                err = atomisp_fixed_pattern_table(asd, arg);
2925                break;
2926
2927        case ATOMISP_IOC_ISP_MAKERNOTE:
2928                err = atomisp_exif_makernote(asd, arg);
2929                break;
2930
2931        case ATOMISP_IOC_G_SENSOR_MODE_DATA:
2932                err = atomisp_get_sensor_mode_data(asd, arg);
2933                break;
2934
2935        case ATOMISP_IOC_G_MOTOR_PRIV_INT_DATA:
2936#ifndef ISP2401
2937                if (isp->inputs[asd->input_curr].motor)
2938#else
2939                if (isp->motor)
2940#endif
2941#ifndef ISP2401
2942                        err = v4l2_subdev_call(
2943                                        isp->inputs[asd->input_curr].motor,
2944                                        core, ioctl, cmd, arg);
2945#else
2946                        err = v4l2_subdev_call(
2947                                        isp->motor,
2948                                        core, ioctl, cmd, arg);
2949#endif
2950                else
2951                        err = v4l2_subdev_call(
2952                                        isp->inputs[asd->input_curr].camera,
2953                                        core, ioctl, cmd, arg);
2954                break;
2955
2956        case ATOMISP_IOC_S_EXPOSURE:
2957        case ATOMISP_IOC_G_SENSOR_CALIBRATION_GROUP:
2958        case ATOMISP_IOC_G_SENSOR_PRIV_INT_DATA:
2959        case ATOMISP_IOC_G_SENSOR_AE_BRACKETING_INFO:
2960        case ATOMISP_IOC_S_SENSOR_AE_BRACKETING_MODE:
2961        case ATOMISP_IOC_G_SENSOR_AE_BRACKETING_MODE:
2962        case ATOMISP_IOC_S_SENSOR_AE_BRACKETING_LUT:
2963#ifdef ISP2401
2964        case ATOMISP_IOC_G_UPDATE_EXPOSURE:
2965#endif
2966                err = v4l2_subdev_call(isp->inputs[asd->input_curr].camera,
2967                                        core, ioctl, cmd, arg);
2968                break;
2969
2970        case ATOMISP_IOC_ACC_LOAD:
2971                err = atomisp_acc_load(asd, arg);
2972                break;
2973
2974        case ATOMISP_IOC_ACC_LOAD_TO_PIPE:
2975                err = atomisp_acc_load_to_pipe(asd, arg);
2976                break;
2977
2978        case ATOMISP_IOC_ACC_UNLOAD:
2979                err = atomisp_acc_unload(asd, arg);
2980                break;
2981
2982        case ATOMISP_IOC_ACC_START:
2983                err = atomisp_acc_start(asd, arg);
2984                break;
2985
2986        case ATOMISP_IOC_ACC_WAIT:
2987                err = atomisp_acc_wait(asd, arg);
2988                break;
2989
2990        case ATOMISP_IOC_ACC_MAP:
2991                err = atomisp_acc_map(asd, arg);
2992                break;
2993
2994        case ATOMISP_IOC_ACC_UNMAP:
2995                err = atomisp_acc_unmap(asd, arg);
2996                break;
2997
2998        case ATOMISP_IOC_ACC_S_MAPPED_ARG:
2999                err = atomisp_acc_s_mapped_arg(asd, arg);
3000                break;
3001
3002        case ATOMISP_IOC_S_ISP_SHD_TAB:
3003                err = atomisp_set_shading_table(asd, arg);
3004                break;
3005
3006        case ATOMISP_IOC_G_ISP_GAMMA_CORRECTION:
3007                err = atomisp_gamma_correction(asd, 0, arg);
3008                break;
3009
3010        case ATOMISP_IOC_S_ISP_GAMMA_CORRECTION:
3011                err = atomisp_gamma_correction(asd, 1, arg);
3012                break;
3013
3014        case ATOMISP_IOC_S_PARAMETERS:
3015                err = atomisp_set_parameters(vdev, arg);
3016                break;
3017
3018        case ATOMISP_IOC_S_CONT_CAPTURE_CONFIG:
3019                err = atomisp_offline_capture_configure(asd, arg);
3020                break;
3021        case ATOMISP_IOC_G_METADATA:
3022                err = atomisp_get_metadata(asd, 0, arg);
3023                break;
3024        case ATOMISP_IOC_G_METADATA_BY_TYPE:
3025                err = atomisp_get_metadata_by_type(asd, 0, arg);
3026                break;
3027        case ATOMISP_IOC_EXT_ISP_CTRL:
3028                err = v4l2_subdev_call(isp->inputs[asd->input_curr].camera,
3029                                        core, ioctl, cmd, arg);
3030                break;
3031        case ATOMISP_IOC_EXP_ID_UNLOCK:
3032                err = atomisp_exp_id_unlock(asd, arg);
3033                break;
3034        case ATOMISP_IOC_EXP_ID_CAPTURE:
3035                err = atomisp_exp_id_capture(asd, arg);
3036                break;
3037        case ATOMISP_IOC_S_ENABLE_DZ_CAPT_PIPE:
3038                err = atomisp_enable_dz_capt_pipe(asd, arg);
3039                break;
3040        case ATOMISP_IOC_G_FORMATS_CONFIG:
3041                err = atomisp_formats(asd, 0, arg);
3042                break;
3043
3044        case ATOMISP_IOC_S_FORMATS_CONFIG:
3045                err = atomisp_formats(asd, 1, arg);
3046                break;
3047        case ATOMISP_IOC_S_EXPOSURE_WINDOW:
3048                err = atomisp_s_ae_window(asd, arg);
3049                break;
3050        case ATOMISP_IOC_S_ACC_STATE:
3051                err = atomisp_acc_set_state(asd, arg);
3052                break;
3053        case ATOMISP_IOC_G_ACC_STATE:
3054                err = atomisp_acc_get_state(asd, arg);
3055                break;
3056        case ATOMISP_IOC_INJECT_A_FAKE_EVENT:
3057                err = atomisp_inject_a_fake_event(asd, arg);
3058                break;
3059        case ATOMISP_IOC_G_INVALID_FRAME_NUM:
3060                err = atomisp_get_invalid_frame_num(vdev, arg);
3061                break;
3062        case ATOMISP_IOC_S_ARRAY_RESOLUTION:
3063                err = atomisp_set_array_res(asd, arg);
3064                break;
3065        default:
3066                err = -EINVAL;
3067                break;
3068        }
3069
3070        switch (cmd) {
3071        case ATOMISP_IOC_G_MOTOR_PRIV_INT_DATA:
3072        case ATOMISP_IOC_S_EXPOSURE:
3073        case ATOMISP_IOC_G_SENSOR_CALIBRATION_GROUP:
3074        case ATOMISP_IOC_G_SENSOR_PRIV_INT_DATA:
3075        case ATOMISP_IOC_EXT_ISP_CTRL:
3076        case ATOMISP_IOC_G_SENSOR_AE_BRACKETING_INFO:
3077        case ATOMISP_IOC_S_SENSOR_AE_BRACKETING_MODE:
3078        case ATOMISP_IOC_G_SENSOR_AE_BRACKETING_MODE:
3079        case ATOMISP_IOC_S_SENSOR_AE_BRACKETING_LUT:
3080#ifdef ISP2401
3081        case ATOMISP_IOC_G_UPDATE_EXPOSURE:
3082#endif
3083                break;
3084        default:
3085                rt_mutex_unlock(&isp->mutex);
3086                break;
3087        }
3088        return err;
3089}
3090
3091const struct v4l2_ioctl_ops atomisp_ioctl_ops = {
3092        .vidioc_querycap = atomisp_querycap,
3093        .vidioc_enum_input = atomisp_enum_input,
3094        .vidioc_g_input = atomisp_g_input,
3095        .vidioc_s_input = atomisp_s_input,
3096        .vidioc_queryctrl = atomisp_queryctl,
3097        .vidioc_s_ctrl = atomisp_s_ctrl,
3098        .vidioc_g_ctrl = atomisp_g_ctrl,
3099        .vidioc_s_ext_ctrls = atomisp_s_ext_ctrls,
3100        .vidioc_g_ext_ctrls = atomisp_g_ext_ctrls,
3101        .vidioc_enum_fmt_vid_cap = atomisp_enum_fmt_cap,
3102        .vidioc_try_fmt_vid_cap = atomisp_try_fmt_cap,
3103        .vidioc_g_fmt_vid_cap = atomisp_g_fmt_cap,
3104        .vidioc_s_fmt_vid_cap = atomisp_s_fmt_cap,
3105        .vidioc_reqbufs = atomisp_reqbufs,
3106        .vidioc_querybuf = atomisp_querybuf,
3107        .vidioc_qbuf = atomisp_qbuf,
3108        .vidioc_dqbuf = atomisp_dqbuf,
3109        .vidioc_streamon = atomisp_streamon,
3110        .vidioc_streamoff = atomisp_streamoff,
3111        .vidioc_default = atomisp_vidioc_default,
3112        .vidioc_s_parm = atomisp_s_parm,
3113        .vidioc_g_parm = atomisp_g_parm,
3114};
3115
3116const struct v4l2_ioctl_ops atomisp_file_ioctl_ops = {
3117        .vidioc_querycap = atomisp_querycap,
3118        .vidioc_g_fmt_vid_out = atomisp_g_fmt_file,
3119        .vidioc_s_fmt_vid_out = atomisp_s_fmt_file,
3120        .vidioc_s_parm = atomisp_s_parm_file,
3121        .vidioc_reqbufs = atomisp_reqbufs_file,
3122        .vidioc_querybuf = atomisp_querybuf_file,
3123        .vidioc_qbuf = atomisp_qbuf_file,
3124};
3125