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