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