OSDN Git Service

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