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