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