1 // SPDX-License-Identifier: GPL-2.0
2 /*
3  * Support for Medifield PNW Camera Imaging ISP subsystem.
4  *
5  * Copyright (c) 2010 Intel Corporation. All Rights Reserved.
6  *
7  * Copyright (c) 2010 Silicon Hive www.siliconhive.com.
8  *
9  * This program is free software; you can redistribute it and/or
10  * modify it under the terms of the GNU General Public License version
11  * 2 as published by the Free Software Foundation.
12  *
13  * This program is distributed in the hope that it will be useful,
14  * but WITHOUT ANY WARRANTY; without even the implied warranty of
15  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
16  * GNU General Public License for more details.
17  *
18  *
19  */
20 
21 #include <linux/delay.h>
22 #include <linux/pci.h>
23 
24 #include <media/v4l2-ioctl.h>
25 #include <media/v4l2-event.h>
26 
27 #include "atomisp_cmd.h"
28 #include "atomisp_common.h"
29 #include "atomisp_fops.h"
30 #include "atomisp_internal.h"
31 #include "atomisp_ioctl.h"
32 #include "atomisp-regs.h"
33 #include "atomisp_compat.h"
34 
35 #include "sh_css_hrt.h"
36 
37 #include "gp_device.h"
38 #include "device_access.h"
39 #include "irq.h"
40 
41 static const char *DRIVER = "atomisp";	/* max size 15 */
42 static const char *CARD = "ATOM ISP";	/* max size 31 */
43 
44 /*
45  * FIXME: ISP should not know beforehand all CIDs supported by sensor.
46  * Instead, it needs to propagate to sensor unkonwn CIDs.
47  */
48 static struct v4l2_queryctrl ci_v4l2_controls[] = {
49 	{
50 		.id = V4L2_CID_AUTO_WHITE_BALANCE,
51 		.type = V4L2_CTRL_TYPE_BOOLEAN,
52 		.name = "Automatic White Balance",
53 		.minimum = 0,
54 		.maximum = 1,
55 		.step = 1,
56 		.default_value = 0,
57 	},
58 	{
59 		.id = V4L2_CID_RED_BALANCE,
60 		.type = V4L2_CTRL_TYPE_INTEGER,
61 		.name = "Red Balance",
62 		.minimum = 0x00,
63 		.maximum = 0xff,
64 		.step = 1,
65 		.default_value = 0x00,
66 	},
67 	{
68 		.id = V4L2_CID_BLUE_BALANCE,
69 		.type = V4L2_CTRL_TYPE_INTEGER,
70 		.name = "Blue Balance",
71 		.minimum = 0x00,
72 		.maximum = 0xff,
73 		.step = 1,
74 		.default_value = 0x00,
75 	},
76 	{
77 		.id = V4L2_CID_GAMMA,
78 		.type = V4L2_CTRL_TYPE_INTEGER,
79 		.name = "Gamma",
80 		.minimum = 0x00,
81 		.maximum = 0xff,
82 		.step = 1,
83 		.default_value = 0x00,
84 	},
85 	{
86 		.id = V4L2_CID_POWER_LINE_FREQUENCY,
87 		.type = V4L2_CTRL_TYPE_MENU,
88 		.name = "Light frequency filter",
89 		.minimum = 1,
90 		.maximum = 2,
91 		.step = 1,
92 		.default_value = 1,
93 	},
94 	{
95 		.id = V4L2_CID_COLORFX,
96 		.type = V4L2_CTRL_TYPE_INTEGER,
97 		.name = "Image Color Effect",
98 		.minimum = 0,
99 		.maximum = 9,
100 		.step = 1,
101 		.default_value = 0,
102 	},
103 	{
104 		.id = V4L2_CID_COLORFX_CBCR,
105 		.type = V4L2_CTRL_TYPE_INTEGER,
106 		.name = "Image Color Effect CbCr",
107 		.minimum = 0,
108 		.maximum = 0xffff,
109 		.step = 1,
110 		.default_value = 0,
111 	},
112 	{
113 		.id = V4L2_CID_ATOMISP_BAD_PIXEL_DETECTION,
114 		.type = V4L2_CTRL_TYPE_INTEGER,
115 		.name = "Bad Pixel Correction",
116 		.minimum = 0,
117 		.maximum = 1,
118 		.step = 1,
119 		.default_value = 0,
120 	},
121 	{
122 		.id = V4L2_CID_ATOMISP_POSTPROCESS_GDC_CAC,
123 		.type = V4L2_CTRL_TYPE_INTEGER,
124 		.name = "GDC/CAC",
125 		.minimum = 0,
126 		.maximum = 1,
127 		.step = 1,
128 		.default_value = 0,
129 	},
130 	{
131 		.id = V4L2_CID_ATOMISP_VIDEO_STABLIZATION,
132 		.type = V4L2_CTRL_TYPE_INTEGER,
133 		.name = "Video Stablization",
134 		.minimum = 0,
135 		.maximum = 1,
136 		.step = 1,
137 		.default_value = 0,
138 	},
139 	{
140 		.id = V4L2_CID_ATOMISP_FIXED_PATTERN_NR,
141 		.type = V4L2_CTRL_TYPE_INTEGER,
142 		.name = "Fixed Pattern Noise Reduction",
143 		.minimum = 0,
144 		.maximum = 1,
145 		.step = 1,
146 		.default_value = 0,
147 	},
148 	{
149 		.id = V4L2_CID_ATOMISP_FALSE_COLOR_CORRECTION,
150 		.type = V4L2_CTRL_TYPE_INTEGER,
151 		.name = "False Color Correction",
152 		.minimum = 0,
153 		.maximum = 1,
154 		.step = 1,
155 		.default_value = 0,
156 	},
157 	{
158 		.id = V4L2_CID_REQUEST_FLASH,
159 		.type = V4L2_CTRL_TYPE_INTEGER,
160 		.name = "Request flash frames",
161 		.minimum = 0,
162 		.maximum = 10,
163 		.step = 1,
164 		.default_value = 1,
165 	},
166 	{
167 		.id = V4L2_CID_ATOMISP_LOW_LIGHT,
168 		.type = V4L2_CTRL_TYPE_BOOLEAN,
169 		.name = "Low light mode",
170 		.minimum = 0,
171 		.maximum = 1,
172 		.step = 1,
173 		.default_value = 1,
174 	},
175 	{
176 		.id = V4L2_CID_2A_STATUS,
177 		.type = V4L2_CTRL_TYPE_BITMASK,
178 		.name = "AE and AWB status",
179 		.minimum = 0,
180 		.maximum = V4L2_2A_STATUS_AE_READY | V4L2_2A_STATUS_AWB_READY,
181 		.step = 1,
182 		.default_value = 0,
183 	},
184 	{
185 		.id = V4L2_CID_EXPOSURE,
186 		.type = V4L2_CTRL_TYPE_INTEGER,
187 		.name = "exposure",
188 		.minimum = -4,
189 		.maximum = 4,
190 		.step = 1,
191 		.default_value = 0,
192 	},
193 	{
194 		.id = V4L2_CID_EXPOSURE_ZONE_NUM,
195 		.type = V4L2_CTRL_TYPE_INTEGER,
196 		.name = "one-time exposure zone number",
197 		.minimum = 0x0,
198 		.maximum = 0xffff,
199 		.step = 1,
200 		.default_value = 0,
201 	},
202 	{
203 		.id = V4L2_CID_EXPOSURE_AUTO_PRIORITY,
204 		.type = V4L2_CTRL_TYPE_INTEGER,
205 		.name = "Exposure auto priority",
206 		.minimum = V4L2_EXPOSURE_AUTO,
207 		.maximum = V4L2_EXPOSURE_APERTURE_PRIORITY,
208 		.step = 1,
209 		.default_value = V4L2_EXPOSURE_AUTO,
210 	},
211 	{
212 		.id = V4L2_CID_SCENE_MODE,
213 		.type = V4L2_CTRL_TYPE_INTEGER,
214 		.name = "scene mode",
215 		.minimum = 0,
216 		.maximum = 13,
217 		.step = 1,
218 		.default_value = 0,
219 	},
220 	{
221 		.id = V4L2_CID_ISO_SENSITIVITY,
222 		.type = V4L2_CTRL_TYPE_INTEGER,
223 		.name = "iso",
224 		.minimum = -4,
225 		.maximum = 4,
226 		.step = 1,
227 		.default_value = 0,
228 	},
229 	{
230 		.id = V4L2_CID_ISO_SENSITIVITY_AUTO,
231 		.type = V4L2_CTRL_TYPE_INTEGER,
232 		.name = "iso mode",
233 		.minimum = V4L2_ISO_SENSITIVITY_MANUAL,
234 		.maximum = V4L2_ISO_SENSITIVITY_AUTO,
235 		.step = 1,
236 		.default_value = V4L2_ISO_SENSITIVITY_AUTO,
237 	},
238 	{
239 		.id = V4L2_CID_AUTO_N_PRESET_WHITE_BALANCE,
240 		.type = V4L2_CTRL_TYPE_INTEGER,
241 		.name = "white balance",
242 		.minimum = 0,
243 		.maximum = 9,
244 		.step = 1,
245 		.default_value = 0,
246 	},
247 	{
248 		.id = V4L2_CID_EXPOSURE_METERING,
249 		.type = V4L2_CTRL_TYPE_MENU,
250 		.name = "metering",
251 		.minimum = 0,
252 		.maximum = 3,
253 		.step = 1,
254 		.default_value = 1,
255 	},
256 	{
257 		.id = V4L2_CID_3A_LOCK,
258 		.type = V4L2_CTRL_TYPE_BITMASK,
259 		.name = "3a lock",
260 		.minimum = 0,
261 		.maximum = V4L2_LOCK_EXPOSURE | V4L2_LOCK_WHITE_BALANCE
262 		| V4L2_LOCK_FOCUS,
263 		.step = 1,
264 		.default_value = 0,
265 	},
266 	{
267 		.id = V4L2_CID_TEST_PATTERN,
268 		.type = V4L2_CTRL_TYPE_INTEGER,
269 		.name = "Test Pattern",
270 		.minimum = 0,
271 		.maximum = 0xffff,
272 		.step = 1,
273 		.default_value = 0,
274 	},
275 	{
276 		.id = V4L2_CID_TEST_PATTERN_COLOR_R,
277 		.type = V4L2_CTRL_TYPE_INTEGER,
278 		.name = "Test Pattern Solid Color R",
279 		.minimum = INT_MIN,
280 		.maximum = INT_MAX,
281 		.step = 1,
282 		.default_value = 0,
283 	},
284 	{
285 		.id = V4L2_CID_TEST_PATTERN_COLOR_GR,
286 		.type = V4L2_CTRL_TYPE_INTEGER,
287 		.name = "Test Pattern Solid Color GR",
288 		.minimum = INT_MIN,
289 		.maximum = INT_MAX,
290 		.step = 1,
291 		.default_value = 0,
292 	},
293 	{
294 		.id = V4L2_CID_TEST_PATTERN_COLOR_GB,
295 		.type = V4L2_CTRL_TYPE_INTEGER,
296 		.name = "Test Pattern Solid Color GB",
297 		.minimum = INT_MIN,
298 		.maximum = INT_MAX,
299 		.step = 1,
300 		.default_value = 0,
301 	},
302 	{
303 		.id = V4L2_CID_TEST_PATTERN_COLOR_B,
304 		.type = V4L2_CTRL_TYPE_INTEGER,
305 		.name = "Test Pattern Solid Color B",
306 		.minimum = INT_MIN,
307 		.maximum = INT_MAX,
308 		.step = 1,
309 		.default_value = 0,
310 	},
311 };
312 
313 static const u32 ctrls_num = ARRAY_SIZE(ci_v4l2_controls);
314 
315 /*
316  * supported V4L2 fmts and resolutions
317  */
318 const struct atomisp_format_bridge atomisp_output_fmts[] = {
319 	{
320 		.pixelformat = V4L2_PIX_FMT_YUV420,
321 		.depth = 12,
322 		.mbus_code = V4L2_MBUS_FMT_CUSTOM_YUV420,
323 		.sh_fmt = IA_CSS_FRAME_FORMAT_YUV420,
324 		.description = "YUV420, planar",
325 		.planar = true
326 	}, {
327 		.pixelformat = V4L2_PIX_FMT_YVU420,
328 		.depth = 12,
329 		.mbus_code = V4L2_MBUS_FMT_CUSTOM_YVU420,
330 		.sh_fmt = IA_CSS_FRAME_FORMAT_YV12,
331 		.description = "YVU420, planar",
332 		.planar = true
333 	}, {
334 		.pixelformat = V4L2_PIX_FMT_YUV422P,
335 		.depth = 16,
336 		.mbus_code = V4L2_MBUS_FMT_CUSTOM_YUV422P,
337 		.sh_fmt = IA_CSS_FRAME_FORMAT_YUV422,
338 		.description = "YUV422, planar",
339 		.planar = true
340 	}, {
341 		.pixelformat = V4L2_PIX_FMT_YUV444,
342 		.depth = 24,
343 		.mbus_code = V4L2_MBUS_FMT_CUSTOM_YUV444,
344 		.sh_fmt = IA_CSS_FRAME_FORMAT_YUV444,
345 		.description = "YUV444"
346 	}, {
347 		.pixelformat = V4L2_PIX_FMT_NV12,
348 		.depth = 12,
349 		.mbus_code = V4L2_MBUS_FMT_CUSTOM_NV12,
350 		.sh_fmt = IA_CSS_FRAME_FORMAT_NV12,
351 		.description = "NV12, Y-plane, CbCr interleaved",
352 		.planar = true
353 	}, {
354 		.pixelformat = V4L2_PIX_FMT_NV21,
355 		.depth = 12,
356 		.mbus_code = V4L2_MBUS_FMT_CUSTOM_NV21,
357 		.sh_fmt = IA_CSS_FRAME_FORMAT_NV21,
358 		.description = "NV21, Y-plane, CbCr interleaved",
359 		.planar = true
360 	}, {
361 		.pixelformat = V4L2_PIX_FMT_NV16,
362 		.depth = 16,
363 		.mbus_code = V4L2_MBUS_FMT_CUSTOM_NV16,
364 		.sh_fmt = IA_CSS_FRAME_FORMAT_NV16,
365 		.description = "NV16, Y-plane, CbCr interleaved",
366 		.planar = true
367 	}, {
368 		.pixelformat = V4L2_PIX_FMT_YUYV,
369 		.depth = 16,
370 		.mbus_code = V4L2_MBUS_FMT_CUSTOM_YUYV,
371 		.sh_fmt = IA_CSS_FRAME_FORMAT_YUYV,
372 		.description = "YUYV, interleaved"
373 	}, {
374 		.pixelformat = V4L2_PIX_FMT_UYVY,
375 		.depth = 16,
376 		.mbus_code = MEDIA_BUS_FMT_UYVY8_1X16,
377 		.sh_fmt = IA_CSS_FRAME_FORMAT_UYVY,
378 		.description = "UYVY, interleaved"
379 	}, { /* This one is for parallel sensors! DO NOT USE! */
380 		.pixelformat = V4L2_PIX_FMT_UYVY,
381 		.depth = 16,
382 		.mbus_code = MEDIA_BUS_FMT_UYVY8_2X8,
383 		.sh_fmt = IA_CSS_FRAME_FORMAT_UYVY,
384 		.description = "UYVY, interleaved"
385 	}, {
386 		.pixelformat = V4L2_PIX_FMT_SBGGR16,
387 		.depth = 16,
388 		.mbus_code = V4L2_MBUS_FMT_CUSTOM_SBGGR16,
389 		.sh_fmt = IA_CSS_FRAME_FORMAT_RAW,
390 		.description = "Bayer 16"
391 	}, {
392 		.pixelformat = V4L2_PIX_FMT_SBGGR8,
393 		.depth = 8,
394 		.mbus_code = MEDIA_BUS_FMT_SBGGR8_1X8,
395 		.sh_fmt = IA_CSS_FRAME_FORMAT_RAW,
396 		.description = "Bayer 8"
397 	}, {
398 		.pixelformat = V4L2_PIX_FMT_SGBRG8,
399 		.depth = 8,
400 		.mbus_code = MEDIA_BUS_FMT_SGBRG8_1X8,
401 		.sh_fmt = IA_CSS_FRAME_FORMAT_RAW,
402 		.description = "Bayer 8"
403 	}, {
404 		.pixelformat = V4L2_PIX_FMT_SGRBG8,
405 		.depth = 8,
406 		.mbus_code = MEDIA_BUS_FMT_SGRBG8_1X8,
407 		.sh_fmt = IA_CSS_FRAME_FORMAT_RAW,
408 		.description = "Bayer 8"
409 	}, {
410 		.pixelformat = V4L2_PIX_FMT_SRGGB8,
411 		.depth = 8,
412 		.mbus_code = MEDIA_BUS_FMT_SRGGB8_1X8,
413 		.sh_fmt = IA_CSS_FRAME_FORMAT_RAW,
414 		.description = "Bayer 8"
415 	}, {
416 		.pixelformat = V4L2_PIX_FMT_SBGGR10,
417 		.depth = 16,
418 		.mbus_code = MEDIA_BUS_FMT_SBGGR10_1X10,
419 		.sh_fmt = IA_CSS_FRAME_FORMAT_RAW,
420 		.description = "Bayer 10"
421 	}, {
422 		.pixelformat = V4L2_PIX_FMT_SGBRG10,
423 		.depth = 16,
424 		.mbus_code = MEDIA_BUS_FMT_SGBRG10_1X10,
425 		.sh_fmt = IA_CSS_FRAME_FORMAT_RAW,
426 		.description = "Bayer 10"
427 	}, {
428 		.pixelformat = V4L2_PIX_FMT_SGRBG10,
429 		.depth = 16,
430 		.mbus_code = MEDIA_BUS_FMT_SGRBG10_1X10,
431 		.sh_fmt = IA_CSS_FRAME_FORMAT_RAW,
432 		.description = "Bayer 10"
433 	}, {
434 		.pixelformat = V4L2_PIX_FMT_SRGGB10,
435 		.depth = 16,
436 		.mbus_code = MEDIA_BUS_FMT_SRGGB10_1X10,
437 		.sh_fmt = IA_CSS_FRAME_FORMAT_RAW,
438 		.description = "Bayer 10"
439 	}, {
440 		.pixelformat = V4L2_PIX_FMT_SBGGR12,
441 		.depth = 16,
442 		.mbus_code = MEDIA_BUS_FMT_SBGGR12_1X12,
443 		.sh_fmt = IA_CSS_FRAME_FORMAT_RAW,
444 		.description = "Bayer 12"
445 	}, {
446 		.pixelformat = V4L2_PIX_FMT_SGBRG12,
447 		.depth = 16,
448 		.mbus_code = MEDIA_BUS_FMT_SGBRG12_1X12,
449 		.sh_fmt = IA_CSS_FRAME_FORMAT_RAW,
450 		.description = "Bayer 12"
451 	}, {
452 		.pixelformat = V4L2_PIX_FMT_SGRBG12,
453 		.depth = 16,
454 		.mbus_code = MEDIA_BUS_FMT_SGRBG12_1X12,
455 		.sh_fmt = IA_CSS_FRAME_FORMAT_RAW,
456 		.description = "Bayer 12"
457 	}, {
458 		.pixelformat = V4L2_PIX_FMT_SRGGB12,
459 		.depth = 16,
460 		.mbus_code = MEDIA_BUS_FMT_SRGGB12_1X12,
461 		.sh_fmt = IA_CSS_FRAME_FORMAT_RAW,
462 		.description = "Bayer 12"
463 	}, {
464 		.pixelformat = V4L2_PIX_FMT_RGB32,
465 		.depth = 32,
466 		.mbus_code = V4L2_MBUS_FMT_CUSTOM_RGB32,
467 		.sh_fmt = IA_CSS_FRAME_FORMAT_RGBA888,
468 		.description = "32 RGB 8-8-8-8"
469 	}, {
470 		.pixelformat = V4L2_PIX_FMT_RGB565,
471 		.depth = 16,
472 		.mbus_code = MEDIA_BUS_FMT_BGR565_2X8_LE,
473 		.sh_fmt = IA_CSS_FRAME_FORMAT_RGB565,
474 		.description = "16 RGB 5-6-5"
475 #if 0
476 	}, {
477 		.pixelformat = V4L2_PIX_FMT_JPEG,
478 		.depth = 8,
479 		.mbus_code = MEDIA_BUS_FMT_JPEG_1X8,
480 		.sh_fmt = IA_CSS_FRAME_FORMAT_BINARY_8,
481 		.description = "JPEG"
482 	}, {
483 		/* This is a custom format being used by M10MO to send the RAW data */
484 		.pixelformat = V4L2_PIX_FMT_CUSTOM_M10MO_RAW,
485 		.depth = 8,
486 		.mbus_code = V4L2_MBUS_FMT_CUSTOM_M10MO_RAW,
487 		.sh_fmt = IA_CSS_FRAME_FORMAT_BINARY_8,
488 		.description = "Custom RAW for M10MO"
489 #endif
490 	},
491 };
492 
493 const struct atomisp_format_bridge *
494 atomisp_get_format_bridge(unsigned int pixelformat)
495 {
496 	unsigned int i;
497 
498 	for (i = 0; i < ARRAY_SIZE(atomisp_output_fmts); i++) {
499 		if (atomisp_output_fmts[i].pixelformat == pixelformat)
500 			return &atomisp_output_fmts[i];
501 	}
502 
503 	return NULL;
504 }
505 
506 const struct atomisp_format_bridge *
507 atomisp_get_format_bridge_from_mbus(u32 mbus_code)
508 {
509 	unsigned int i;
510 
511 	for (i = 0; i < ARRAY_SIZE(atomisp_output_fmts); i++) {
512 		if (mbus_code == atomisp_output_fmts[i].mbus_code)
513 			return &atomisp_output_fmts[i];
514 	}
515 
516 	return NULL;
517 }
518 
519 int atomisp_pipe_check(struct atomisp_video_pipe *pipe, bool settings_change)
520 {
521 	lockdep_assert_held(&pipe->isp->mutex);
522 
523 	if (pipe->isp->isp_fatal_error)
524 		return -EIO;
525 
526 	if (settings_change && vb2_is_busy(&pipe->vb_queue)) {
527 		dev_err(pipe->isp->dev, "Set fmt/input IOCTL while streaming\n");
528 		return -EBUSY;
529 	}
530 
531 	return 0;
532 }
533 
534 /*
535  * v4l2 ioctls
536  * return ISP capabilities
537  */
538 static int atomisp_querycap(struct file *file, void *fh,
539 			    struct v4l2_capability *cap)
540 {
541 	struct video_device *vdev = video_devdata(file);
542 	struct atomisp_device *isp = video_get_drvdata(vdev);
543 
544 	strscpy(cap->driver, DRIVER, sizeof(cap->driver));
545 	strscpy(cap->card, CARD, sizeof(cap->card));
546 	snprintf(cap->bus_info, sizeof(cap->bus_info), "PCI:%s", dev_name(isp->dev));
547 
548 	return 0;
549 }
550 
551 /*
552  * enum input are used to check primary/secondary camera
553  */
554 static int atomisp_enum_input(struct file *file, void *fh,
555 			      struct v4l2_input *input)
556 {
557 	struct video_device *vdev = video_devdata(file);
558 	struct atomisp_device *isp = video_get_drvdata(vdev);
559 	int index = input->index;
560 	struct v4l2_subdev *motor;
561 
562 	if (index >= isp->input_cnt)
563 		return -EINVAL;
564 
565 	if (!isp->inputs[index].camera)
566 		return -EINVAL;
567 
568 	memset(input, 0, sizeof(struct v4l2_input));
569 	strscpy(input->name, isp->inputs[index].camera->name,
570 		sizeof(input->name));
571 
572 	/*
573 	 * HACK: append actuator's name to sensor's
574 	 * As currently userspace can't talk directly to subdev nodes, this
575 	 * ioctl is the only way to enum inputs + possible external actuators
576 	 * for 3A tuning purpose.
577 	 */
578 	if (!IS_ISP2401)
579 		motor = isp->inputs[index].motor;
580 	else
581 		motor = isp->motor;
582 
583 	if (motor && strlen(motor->name) > 0) {
584 		const int cur_len = strlen(input->name);
585 		const int max_size = sizeof(input->name) - cur_len - 1;
586 
587 		if (max_size > 1) {
588 			input->name[cur_len] = '+';
589 			strscpy(&input->name[cur_len + 1],
590 				motor->name, max_size);
591 		}
592 	}
593 
594 	input->type = V4L2_INPUT_TYPE_CAMERA;
595 	input->index = index;
596 	input->reserved[0] = isp->inputs[index].type;
597 	input->reserved[1] = isp->inputs[index].port;
598 
599 	return 0;
600 }
601 
602 /*
603  * get input are used to get current primary/secondary camera
604  */
605 static int atomisp_g_input(struct file *file, void *fh, unsigned int *input)
606 {
607 	struct video_device *vdev = video_devdata(file);
608 	struct atomisp_sub_device *asd = atomisp_to_video_pipe(vdev)->asd;
609 
610 	*input = asd->input_curr;
611 	return 0;
612 }
613 
614 static int atomisp_s_fmt_cap(struct file *file, void *fh,
615 			     struct v4l2_format *f)
616 {
617 	struct video_device *vdev = video_devdata(file);
618 
619 	return atomisp_set_fmt(vdev, f);
620 }
621 
622 /*
623  * set input are used to set current primary/secondary camera
624  */
625 static int atomisp_s_input(struct file *file, void *fh, unsigned int input)
626 {
627 	struct video_device *vdev = video_devdata(file);
628 	struct atomisp_device *isp = video_get_drvdata(vdev);
629 	struct atomisp_video_pipe *pipe = atomisp_to_video_pipe(vdev);
630 	struct atomisp_sub_device *asd = pipe->asd;
631 	struct v4l2_subdev *camera = NULL;
632 	struct v4l2_subdev *motor;
633 	int ret;
634 
635 	ret = atomisp_pipe_check(pipe, true);
636 	if (ret)
637 		return ret;
638 
639 	if (input >= ATOM_ISP_MAX_INPUTS || input >= isp->input_cnt) {
640 		dev_dbg(isp->dev, "input_cnt: %d\n", isp->input_cnt);
641 		return -EINVAL;
642 	}
643 
644 	camera = isp->inputs[input].camera;
645 	if (!camera) {
646 		dev_err(isp->dev, "%s, no camera\n", __func__);
647 		return -EINVAL;
648 	}
649 
650 	/* power off the current owned sensor, as it is not used this time */
651 	if (isp->inputs[asd->input_curr].asd == asd &&
652 	    asd->input_curr != input) {
653 		ret = v4l2_subdev_call(isp->inputs[asd->input_curr].camera,
654 				       core, s_power, 0);
655 		if (ret && ret != -ENOIOCTLCMD)
656 			dev_warn(isp->dev,
657 				 "Failed to power-off sensor\n");
658 		/* clear the asd field to show this camera is not used */
659 		isp->inputs[asd->input_curr].asd = NULL;
660 	}
661 
662 	/* powe on the new sensor */
663 	ret = v4l2_subdev_call(isp->inputs[input].camera, core, s_power, 1);
664 	if (ret && ret != -ENOIOCTLCMD) {
665 		dev_err(isp->dev, "Failed to power-on sensor\n");
666 		return ret;
667 	}
668 	/*
669 	 * Some sensor driver resets the run mode during power-on, thus force
670 	 * update the run mode to sensor after power-on.
671 	 */
672 	atomisp_update_run_mode(asd);
673 
674 	/* select operating sensor */
675 	ret = v4l2_subdev_call(isp->inputs[input].camera, video, s_routing,
676 			       0, 0, 0);
677 	if (ret && (ret != -ENOIOCTLCMD)) {
678 		dev_err(isp->dev, "Failed to select sensor\n");
679 		return ret;
680 	}
681 
682 	if (!IS_ISP2401) {
683 		motor = isp->inputs[input].motor;
684 	} else {
685 		motor = isp->motor;
686 		if (motor)
687 			ret = v4l2_subdev_call(motor, core, s_power, 1);
688 	}
689 
690 	if (motor)
691 		ret = v4l2_subdev_call(motor, core, init, 1);
692 
693 	asd->input_curr = input;
694 	/* mark this camera is used by the current stream */
695 	isp->inputs[input].asd = asd;
696 
697 	return 0;
698 }
699 
700 static int atomisp_enum_framesizes(struct file *file, void *priv,
701 				   struct v4l2_frmsizeenum *fsize)
702 {
703 	struct video_device *vdev = video_devdata(file);
704 	struct atomisp_device *isp = video_get_drvdata(vdev);
705 	struct atomisp_sub_device *asd = atomisp_to_video_pipe(vdev)->asd;
706 	struct atomisp_input_subdev *input = &isp->inputs[asd->input_curr];
707 	struct v4l2_subdev_frame_size_enum fse = {
708 		.index = fsize->index,
709 		.which = V4L2_SUBDEV_FORMAT_ACTIVE,
710 	};
711 	int ret;
712 
713 	ret = v4l2_subdev_call(input->camera, pad, enum_frame_size, NULL, &fse);
714 	if (ret)
715 		return ret;
716 
717 	fsize->type = V4L2_FRMSIZE_TYPE_DISCRETE;
718 	fsize->discrete.width = fse.max_width - pad_w;
719 	fsize->discrete.height = fse.max_height - pad_h;
720 
721 	return 0;
722 }
723 
724 static int atomisp_enum_frameintervals(struct file *file, void *priv,
725 				       struct v4l2_frmivalenum *fival)
726 {
727 	struct video_device *vdev = video_devdata(file);
728 	struct atomisp_device *isp = video_get_drvdata(vdev);
729 	struct atomisp_sub_device *asd = atomisp_to_video_pipe(vdev)->asd;
730 	struct v4l2_subdev_frame_interval_enum fie = {
731 		.code	= atomisp_in_fmt_conv[0].code,
732 		.index = fival->index,
733 		.width = fival->width,
734 		.height = fival->height,
735 		.which = V4L2_SUBDEV_FORMAT_ACTIVE,
736 	};
737 	int ret;
738 
739 	ret = v4l2_subdev_call(isp->inputs[asd->input_curr].camera,
740 			       pad, enum_frame_interval, NULL,
741 			       &fie);
742 	if (ret)
743 		return ret;
744 
745 	fival->type = V4L2_FRMIVAL_TYPE_DISCRETE;
746 	fival->discrete = fie.interval;
747 
748 	return ret;
749 }
750 
751 static int atomisp_enum_fmt_cap(struct file *file, void *fh,
752 				struct v4l2_fmtdesc *f)
753 {
754 	struct video_device *vdev = video_devdata(file);
755 	struct atomisp_device *isp = video_get_drvdata(vdev);
756 	struct atomisp_sub_device *asd = atomisp_to_video_pipe(vdev)->asd;
757 	struct v4l2_subdev_mbus_code_enum code = {
758 		.which = V4L2_SUBDEV_FORMAT_ACTIVE,
759 	};
760 	const struct atomisp_format_bridge *format;
761 	struct v4l2_subdev *camera;
762 	unsigned int i, fi = 0;
763 	int rval;
764 
765 	camera = isp->inputs[asd->input_curr].camera;
766 	if(!camera) {
767 		dev_err(isp->dev, "%s(): camera is NULL, device is %s\n",
768 			__func__, vdev->name);
769 		return -EINVAL;
770 	}
771 
772 	rval = v4l2_subdev_call(camera, pad, enum_mbus_code, NULL, &code);
773 	if (rval == -ENOIOCTLCMD) {
774 		dev_warn(isp->dev,
775 			 "enum_mbus_code pad op not supported by %s. Please fix your sensor driver!\n",
776 			 camera->name);
777 	}
778 
779 	if (rval)
780 		return rval;
781 
782 	for (i = 0; i < ARRAY_SIZE(atomisp_output_fmts); i++) {
783 		format = &atomisp_output_fmts[i];
784 
785 		/*
786 		 * Is the atomisp-supported format is valid for the
787 		 * sensor (configuration)? If not, skip it.
788 		 *
789 		 * FIXME: fix the pipeline to allow sensor format too.
790 		 */
791 		if (format->sh_fmt == IA_CSS_FRAME_FORMAT_RAW)
792 			continue;
793 
794 		/* Found a match. Now let's pick f->index'th one. */
795 		if (fi < f->index) {
796 			fi++;
797 			continue;
798 		}
799 
800 		strscpy(f->description, format->description,
801 			sizeof(f->description));
802 		f->pixelformat = format->pixelformat;
803 		return 0;
804 	}
805 
806 	return -EINVAL;
807 }
808 
809 static int atomisp_adjust_fmt(struct v4l2_format *f)
810 {
811 	const struct atomisp_format_bridge *format_bridge;
812 	u32 padded_width;
813 
814 	format_bridge = atomisp_get_format_bridge(f->fmt.pix.pixelformat);
815 	/* Currently, raw formats are broken!!! */
816 	if (!format_bridge || format_bridge->sh_fmt == IA_CSS_FRAME_FORMAT_RAW) {
817 		f->fmt.pix.pixelformat = V4L2_PIX_FMT_YUV420;
818 
819 		format_bridge = atomisp_get_format_bridge(f->fmt.pix.pixelformat);
820 		if (!format_bridge)
821 			return -EINVAL;
822 	}
823 
824 	padded_width = f->fmt.pix.width + pad_w;
825 
826 	if (format_bridge->planar) {
827 		f->fmt.pix.bytesperline = padded_width;
828 		f->fmt.pix.sizeimage = PAGE_ALIGN(f->fmt.pix.height *
829 						  DIV_ROUND_UP(format_bridge->depth *
830 						  padded_width, 8));
831 	} else {
832 		f->fmt.pix.bytesperline = DIV_ROUND_UP(format_bridge->depth *
833 						      padded_width, 8);
834 		f->fmt.pix.sizeimage = PAGE_ALIGN(f->fmt.pix.height * f->fmt.pix.bytesperline);
835 	}
836 
837 	if (f->fmt.pix.field == V4L2_FIELD_ANY)
838 		f->fmt.pix.field = V4L2_FIELD_NONE;
839 
840 	/*
841 	 * FIXME: do we need to setup this differently, depending on the
842 	 * sensor or the pipeline?
843 	 */
844 	f->fmt.pix.colorspace = V4L2_COLORSPACE_REC709;
845 	f->fmt.pix.ycbcr_enc = V4L2_YCBCR_ENC_709;
846 	f->fmt.pix.xfer_func = V4L2_XFER_FUNC_709;
847 
848 	f->fmt.pix.width -= pad_w;
849 	f->fmt.pix.height -= pad_h;
850 
851 	return 0;
852 }
853 
854 /* This function looks up the closest available resolution. */
855 static int atomisp_try_fmt_cap(struct file *file, void *fh,
856 			       struct v4l2_format *f)
857 {
858 	struct video_device *vdev = video_devdata(file);
859 	u32 pixfmt = f->fmt.pix.pixelformat;
860 	int ret;
861 
862 	/*
863 	 * atomisp_try_fmt() gived results with padding included, note
864 	 * (this gets removed again by the atomisp_adjust_fmt() call below.
865 	 */
866 	f->fmt.pix.width += pad_w;
867 	f->fmt.pix.height += pad_h;
868 
869 	ret = atomisp_try_fmt(vdev, &f->fmt.pix);
870 	if (ret)
871 		return ret;
872 
873 	/*
874 	 * atomisp_try_fmt() replaces pixelformat with the sensors native
875 	 * format, restore the actual format requested by userspace.
876 	 */
877 	f->fmt.pix.pixelformat = pixfmt;
878 
879 	return atomisp_adjust_fmt(f);
880 }
881 
882 static int atomisp_g_fmt_cap(struct file *file, void *fh,
883 			     struct v4l2_format *f)
884 {
885 	struct video_device *vdev = video_devdata(file);
886 	struct atomisp_video_pipe *pipe;
887 
888 	pipe = atomisp_to_video_pipe(vdev);
889 
890 	f->fmt.pix = pipe->pix;
891 
892 	/* If s_fmt was issued, just return whatever is was previouly set */
893 	if (f->fmt.pix.sizeimage)
894 		return 0;
895 
896 	f->fmt.pix.pixelformat = V4L2_PIX_FMT_YUV420;
897 	f->fmt.pix.width = 10000;
898 	f->fmt.pix.height = 10000;
899 
900 	return atomisp_try_fmt_cap(file, fh, f);
901 }
902 
903 int atomisp_alloc_css_stat_bufs(struct atomisp_sub_device *asd,
904 				uint16_t stream_id)
905 {
906 	struct atomisp_device *isp = asd->isp;
907 	struct atomisp_s3a_buf *s3a_buf = NULL, *_s3a_buf;
908 	struct atomisp_dis_buf *dis_buf = NULL, *_dis_buf;
909 	struct atomisp_metadata_buf *md_buf = NULL, *_md_buf;
910 	int count;
911 	struct ia_css_dvs_grid_info *dvs_grid_info =
912 	    atomisp_css_get_dvs_grid_info(&asd->params.curr_grid_info);
913 	unsigned int i;
914 
915 	if (list_empty(&asd->s3a_stats) &&
916 	    asd->params.curr_grid_info.s3a_grid.enable) {
917 		count = ATOMISP_CSS_Q_DEPTH +
918 			ATOMISP_S3A_BUF_QUEUE_DEPTH_FOR_HAL;
919 		dev_dbg(isp->dev, "allocating %d 3a buffers\n", count);
920 		while (count--) {
921 			s3a_buf = kzalloc(sizeof(struct atomisp_s3a_buf), GFP_KERNEL);
922 			if (!s3a_buf)
923 				goto error;
924 
925 			if (atomisp_css_allocate_stat_buffers(
926 				asd, stream_id, s3a_buf, NULL, NULL)) {
927 				kfree(s3a_buf);
928 				goto error;
929 			}
930 
931 			list_add_tail(&s3a_buf->list, &asd->s3a_stats);
932 		}
933 	}
934 
935 	if (list_empty(&asd->dis_stats) && dvs_grid_info &&
936 	    dvs_grid_info->enable) {
937 		count = ATOMISP_CSS_Q_DEPTH + 1;
938 		dev_dbg(isp->dev, "allocating %d dis buffers\n", count);
939 		while (count--) {
940 			dis_buf = kzalloc(sizeof(struct atomisp_dis_buf), GFP_KERNEL);
941 			if (!dis_buf)
942 				goto error;
943 			if (atomisp_css_allocate_stat_buffers(
944 				asd, stream_id, NULL, dis_buf, NULL)) {
945 				kfree(dis_buf);
946 				goto error;
947 			}
948 
949 			list_add_tail(&dis_buf->list, &asd->dis_stats);
950 		}
951 	}
952 
953 	for (i = 0; i < ATOMISP_METADATA_TYPE_NUM; i++) {
954 		if (list_empty(&asd->metadata[i]) &&
955 		    list_empty(&asd->metadata_ready[i]) &&
956 		    list_empty(&asd->metadata_in_css[i])) {
957 			count = ATOMISP_CSS_Q_DEPTH +
958 				ATOMISP_METADATA_QUEUE_DEPTH_FOR_HAL;
959 			dev_dbg(isp->dev, "allocating %d metadata buffers for type %d\n",
960 				count, i);
961 			while (count--) {
962 				md_buf = kzalloc(sizeof(struct atomisp_metadata_buf),
963 						 GFP_KERNEL);
964 				if (!md_buf)
965 					goto error;
966 
967 				if (atomisp_css_allocate_stat_buffers(
968 					asd, stream_id, NULL, NULL, md_buf)) {
969 					kfree(md_buf);
970 					goto error;
971 				}
972 				list_add_tail(&md_buf->list, &asd->metadata[i]);
973 			}
974 		}
975 	}
976 	return 0;
977 
978 error:
979 	dev_err(isp->dev, "failed to allocate statistics buffers\n");
980 
981 	list_for_each_entry_safe(dis_buf, _dis_buf, &asd->dis_stats, list) {
982 		atomisp_css_free_dis_buffer(dis_buf);
983 		list_del(&dis_buf->list);
984 		kfree(dis_buf);
985 	}
986 
987 	list_for_each_entry_safe(s3a_buf, _s3a_buf, &asd->s3a_stats, list) {
988 		atomisp_css_free_3a_buffer(s3a_buf);
989 		list_del(&s3a_buf->list);
990 		kfree(s3a_buf);
991 	}
992 
993 	for (i = 0; i < ATOMISP_METADATA_TYPE_NUM; i++) {
994 		list_for_each_entry_safe(md_buf, _md_buf, &asd->metadata[i],
995 					 list) {
996 			atomisp_css_free_metadata_buffer(md_buf);
997 			list_del(&md_buf->list);
998 			kfree(md_buf);
999 		}
1000 	}
1001 	return -ENOMEM;
1002 }
1003 
1004 /*
1005  * FIXME the abuse of buf->reserved2 in the qbuf and dqbuf wrappers comes from
1006  * the original atomisp buffer handling and should be replaced with proper V4L2
1007  * per frame parameters use.
1008  *
1009  * Once this is fixed these wrappers can be removed, replacing them with direct
1010  * calls to vb2_ioctl_[d]qbuf().
1011  */
1012 static int atomisp_qbuf_wrapper(struct file *file, void *fh, struct v4l2_buffer *buf)
1013 {
1014 	struct video_device *vdev = video_devdata(file);
1015 	struct atomisp_device *isp = video_get_drvdata(vdev);
1016 	struct atomisp_video_pipe *pipe = atomisp_to_video_pipe(vdev);
1017 
1018 	if (buf->index >= vdev->queue->num_buffers)
1019 		return -EINVAL;
1020 
1021 	if (buf->reserved2 & ATOMISP_BUFFER_HAS_PER_FRAME_SETTING) {
1022 		/* this buffer will have a per-frame parameter */
1023 		pipe->frame_request_config_id[buf->index] = buf->reserved2 &
1024 			~ATOMISP_BUFFER_HAS_PER_FRAME_SETTING;
1025 		dev_dbg(isp->dev,
1026 			"This buffer requires per_frame setting which has isp_config_id %d\n",
1027 			pipe->frame_request_config_id[buf->index]);
1028 	} else {
1029 		pipe->frame_request_config_id[buf->index] = 0;
1030 	}
1031 
1032 	return vb2_ioctl_qbuf(file, fh, buf);
1033 }
1034 
1035 static int atomisp_dqbuf_wrapper(struct file *file, void *fh, struct v4l2_buffer *buf)
1036 {
1037 	struct video_device *vdev = video_devdata(file);
1038 	struct atomisp_video_pipe *pipe = atomisp_to_video_pipe(vdev);
1039 	struct atomisp_sub_device *asd = pipe->asd;
1040 	struct atomisp_device *isp = video_get_drvdata(vdev);
1041 	struct ia_css_frame *frame;
1042 	struct vb2_buffer *vb;
1043 	int ret;
1044 
1045 	ret = vb2_ioctl_dqbuf(file, fh, buf);
1046 	if (ret)
1047 		return ret;
1048 
1049 	vb = pipe->vb_queue.bufs[buf->index];
1050 	frame = vb_to_frame(vb);
1051 
1052 	buf->reserved = asd->frame_status[buf->index];
1053 
1054 	/*
1055 	 * Hack:
1056 	 * Currently frame_status in the enum type which takes no more lower
1057 	 * 8 bit.
1058 	 * use bit[31:16] for exp_id as it is only in the range of 1~255
1059 	 */
1060 	buf->reserved &= 0x0000ffff;
1061 	if (!(buf->flags & V4L2_BUF_FLAG_ERROR))
1062 		buf->reserved |= frame->exp_id;
1063 	buf->reserved2 = pipe->frame_config_id[buf->index];
1064 
1065 	dev_dbg(isp->dev,
1066 		"dqbuf buffer %d (%s) with exp_id %d, isp_config_id %d\n",
1067 		buf->index, vdev->name, buf->reserved >> 16, buf->reserved2);
1068 	return 0;
1069 }
1070 
1071 /* Input system HW workaround */
1072 /* Input system address translation corrupts burst during */
1073 /* invalidate. SW workaround for this is to set burst length */
1074 /* manually to 128 in case of 13MPx snapshot and to 1 otherwise. */
1075 static void atomisp_dma_burst_len_cfg(struct atomisp_sub_device *asd)
1076 {
1077 	struct v4l2_mbus_framefmt *sink;
1078 
1079 	sink = atomisp_subdev_get_ffmt(&asd->subdev, NULL,
1080 				       V4L2_SUBDEV_FORMAT_ACTIVE,
1081 				       ATOMISP_SUBDEV_PAD_SINK);
1082 
1083 	if (sink->width * sink->height >= 4096 * 3072)
1084 		atomisp_css2_hw_store_32(DMA_BURST_SIZE_REG, 0x7F);
1085 	else
1086 		atomisp_css2_hw_store_32(DMA_BURST_SIZE_REG, 0x00);
1087 }
1088 
1089 int atomisp_start_streaming(struct vb2_queue *vq, unsigned int count)
1090 {
1091 	struct atomisp_video_pipe *pipe = vq_to_pipe(vq);
1092 	struct atomisp_sub_device *asd = pipe->asd;
1093 	struct atomisp_device *isp = asd->isp;
1094 	struct pci_dev *pdev = to_pci_dev(isp->dev);
1095 	unsigned long irqflags;
1096 	int ret;
1097 
1098 	dev_dbg(isp->dev, "Start stream\n");
1099 
1100 	mutex_lock(&isp->mutex);
1101 
1102 	ret = atomisp_pipe_check(pipe, false);
1103 	if (ret)
1104 		goto out_unlock;
1105 
1106 	/* Input system HW workaround */
1107 	atomisp_dma_burst_len_cfg(asd);
1108 
1109 	/* Invalidate caches. FIXME: should flush only necessary buffers */
1110 	wbinvd();
1111 
1112 	if (asd->params.css_update_params_needed) {
1113 		atomisp_apply_css_parameters(asd, &asd->params.css_param);
1114 		if (asd->params.css_param.update_flag.dz_config)
1115 			asd->params.config.dz_config = &asd->params.css_param.dz_config;
1116 		atomisp_css_update_isp_params(asd);
1117 		asd->params.css_update_params_needed = false;
1118 		memset(&asd->params.css_param.update_flag, 0,
1119 		       sizeof(struct atomisp_parameters));
1120 	}
1121 	asd->params.dvs_6axis = NULL;
1122 
1123 	ret = atomisp_css_start(asd);
1124 	if (ret) {
1125 		atomisp_flush_video_pipe(pipe, VB2_BUF_STATE_QUEUED, true);
1126 		goto out_unlock;
1127 	}
1128 
1129 	spin_lock_irqsave(&isp->lock, irqflags);
1130 	asd->streaming = true;
1131 	spin_unlock_irqrestore(&isp->lock, irqflags);
1132 	atomic_set(&asd->sof_count, -1);
1133 	atomic_set(&asd->sequence, -1);
1134 	atomic_set(&asd->sequence_temp, -1);
1135 
1136 	asd->params.dis_proj_data_valid = false;
1137 	asd->latest_preview_exp_id = 0;
1138 	asd->postview_exp_id = 1;
1139 	asd->preview_exp_id = 1;
1140 
1141 	/* handle per_frame_setting parameter and buffers */
1142 	atomisp_handle_parameter_and_buffer(pipe);
1143 
1144 	atomisp_qbuffers_to_css(asd);
1145 
1146 	if (isp->flash) {
1147 		asd->params.num_flash_frames = 0;
1148 		asd->params.flash_state = ATOMISP_FLASH_IDLE;
1149 		atomisp_setup_flash(asd);
1150 	}
1151 
1152 	atomisp_css_irq_enable(isp, IA_CSS_IRQ_INFO_CSS_RECEIVER_SOF,
1153 			       atomisp_css_valid_sof(isp));
1154 	atomisp_csi2_configure(asd);
1155 
1156 	if (atomisp_freq_scaling(isp, ATOMISP_DFS_MODE_AUTO, false) < 0)
1157 		dev_dbg(isp->dev, "DFS auto mode failed!\n");
1158 
1159 	/* Enable the CSI interface on ANN B0/K0 */
1160 	if (isp->media_dev.hw_revision >= ((ATOMISP_HW_REVISION_ISP2401 <<
1161 					    ATOMISP_HW_REVISION_SHIFT) | ATOMISP_HW_STEPPING_B0)) {
1162 		pci_write_config_word(pdev, MRFLD_PCI_CSI_CONTROL,
1163 				      isp->saved_regs.csi_control | MRFLD_PCI_CSI_CONTROL_CSI_READY);
1164 	}
1165 
1166 	/* stream on the sensor */
1167 	ret = v4l2_subdev_call(isp->inputs[asd->input_curr].camera,
1168 			       video, s_stream, 1);
1169 	if (ret) {
1170 		dev_err(isp->dev, "Starting sensor stream failed: %d\n", ret);
1171 		spin_lock_irqsave(&isp->lock, irqflags);
1172 		asd->streaming = false;
1173 		spin_unlock_irqrestore(&isp->lock, irqflags);
1174 		ret = -EINVAL;
1175 		goto out_unlock;
1176 	}
1177 
1178 out_unlock:
1179 	mutex_unlock(&isp->mutex);
1180 	return ret;
1181 }
1182 
1183 void atomisp_stop_streaming(struct vb2_queue *vq)
1184 {
1185 	struct atomisp_video_pipe *pipe = vq_to_pipe(vq);
1186 	struct atomisp_sub_device *asd = pipe->asd;
1187 	struct atomisp_device *isp = asd->isp;
1188 	struct pci_dev *pdev = to_pci_dev(isp->dev);
1189 	unsigned long flags;
1190 	int ret;
1191 
1192 	dev_dbg(isp->dev, "Stop stream\n");
1193 
1194 	mutex_lock(&isp->mutex);
1195 	/*
1196 	 * There is no guarantee that the buffers queued to / owned by the ISP
1197 	 * will properly be returned to the queue when stopping. Set a flag to
1198 	 * avoid new buffers getting queued and then wait for all the current
1199 	 * buffers to finish.
1200 	 */
1201 	pipe->stopping = true;
1202 	mutex_unlock(&isp->mutex);
1203 	/* wait max 1 second */
1204 	ret = wait_event_timeout(pipe->vb_queue.done_wq,
1205 				 atomisp_buffers_in_css(pipe) == 0, HZ);
1206 	mutex_lock(&isp->mutex);
1207 	pipe->stopping = false;
1208 	if (ret == 0)
1209 		dev_warn(isp->dev, "Warning timeout waiting for CSS to return buffers\n");
1210 
1211 	spin_lock_irqsave(&isp->lock, flags);
1212 	asd->streaming = false;
1213 	spin_unlock_irqrestore(&isp->lock, flags);
1214 
1215 	atomisp_clear_css_buffer_counters(asd);
1216 	atomisp_css_irq_enable(isp, IA_CSS_IRQ_INFO_CSS_RECEIVER_SOF, false);
1217 
1218 	atomisp_css_stop(asd, false);
1219 
1220 	atomisp_flush_video_pipe(pipe, VB2_BUF_STATE_ERROR, true);
1221 
1222 	atomisp_subdev_cleanup_pending_events(asd);
1223 
1224 	ret = v4l2_subdev_call(isp->inputs[asd->input_curr].camera,
1225 			       video, s_stream, 0);
1226 	if (ret)
1227 		dev_warn(isp->dev, "Stopping sensor stream failed: %d\n", ret);
1228 
1229 	if (isp->flash) {
1230 		asd->params.num_flash_frames = 0;
1231 		asd->params.flash_state = ATOMISP_FLASH_IDLE;
1232 	}
1233 
1234 	/* Disable the CSI interface on ANN B0/K0 */
1235 	if (isp->media_dev.hw_revision >= ((ATOMISP_HW_REVISION_ISP2401 <<
1236 					    ATOMISP_HW_REVISION_SHIFT) | ATOMISP_HW_STEPPING_B0)) {
1237 		pci_write_config_word(pdev, MRFLD_PCI_CSI_CONTROL,
1238 				      isp->saved_regs.csi_control & ~MRFLD_PCI_CSI_CONTROL_CSI_READY);
1239 	}
1240 
1241 	if (atomisp_freq_scaling(isp, ATOMISP_DFS_MODE_LOW, false))
1242 		dev_warn(isp->dev, "DFS failed.\n");
1243 
1244 	/*
1245 	 * ISP work around, need to reset ISP to allow next stream on to work.
1246 	 * Streams have already been destroyed by atomisp_css_stop().
1247 	 * Disable PUNIT/ISP acknowlede/handshake - SRSE=3 and then reset.
1248 	 */
1249 	pci_write_config_dword(pdev, PCI_I_CONTROL,
1250 			       isp->saved_regs.i_control | MRFLD_PCI_I_CONTROL_SRSE_RESET_MASK);
1251 	atomisp_reset(isp);
1252 
1253 	/* Streams were destroyed by atomisp_css_stop(), recreate them. */
1254 	ret = atomisp_create_pipes_stream(&isp->asd);
1255 	if (ret)
1256 		dev_warn(isp->dev, "Recreating streams failed: %d\n", ret);
1257 
1258 	mutex_unlock(&isp->mutex);
1259 }
1260 
1261 /*
1262  * To get the current value of a control.
1263  * applications initialize the id field of a struct v4l2_control and
1264  * call this ioctl with a pointer to this structure
1265  */
1266 static int atomisp_g_ctrl(struct file *file, void *fh,
1267 			  struct v4l2_control *control)
1268 {
1269 	struct video_device *vdev = video_devdata(file);
1270 	struct atomisp_sub_device *asd = atomisp_to_video_pipe(vdev)->asd;
1271 	struct atomisp_device *isp = video_get_drvdata(vdev);
1272 	int i, ret = -EINVAL;
1273 
1274 	for (i = 0; i < ctrls_num; i++) {
1275 		if (ci_v4l2_controls[i].id == control->id) {
1276 			ret = 0;
1277 			break;
1278 		}
1279 	}
1280 
1281 	if (ret)
1282 		return ret;
1283 
1284 	switch (control->id) {
1285 	case V4L2_CID_IRIS_ABSOLUTE:
1286 	case V4L2_CID_EXPOSURE_ABSOLUTE:
1287 	case V4L2_CID_2A_STATUS:
1288 	case V4L2_CID_AUTO_N_PRESET_WHITE_BALANCE:
1289 	case V4L2_CID_EXPOSURE:
1290 	case V4L2_CID_EXPOSURE_AUTO:
1291 	case V4L2_CID_SCENE_MODE:
1292 	case V4L2_CID_ISO_SENSITIVITY:
1293 	case V4L2_CID_ISO_SENSITIVITY_AUTO:
1294 	case V4L2_CID_CONTRAST:
1295 	case V4L2_CID_SATURATION:
1296 	case V4L2_CID_SHARPNESS:
1297 	case V4L2_CID_3A_LOCK:
1298 	case V4L2_CID_EXPOSURE_ZONE_NUM:
1299 	case V4L2_CID_TEST_PATTERN:
1300 	case V4L2_CID_TEST_PATTERN_COLOR_R:
1301 	case V4L2_CID_TEST_PATTERN_COLOR_GR:
1302 	case V4L2_CID_TEST_PATTERN_COLOR_GB:
1303 	case V4L2_CID_TEST_PATTERN_COLOR_B:
1304 		return v4l2_g_ctrl(isp->inputs[asd->input_curr].camera->
1305 				   ctrl_handler, control);
1306 	case V4L2_CID_COLORFX:
1307 		ret = atomisp_color_effect(asd, 0, &control->value);
1308 		break;
1309 	case V4L2_CID_ATOMISP_BAD_PIXEL_DETECTION:
1310 		ret = atomisp_bad_pixel(asd, 0, &control->value);
1311 		break;
1312 	case V4L2_CID_ATOMISP_POSTPROCESS_GDC_CAC:
1313 		ret = atomisp_gdc_cac(asd, 0, &control->value);
1314 		break;
1315 	case V4L2_CID_ATOMISP_VIDEO_STABLIZATION:
1316 		ret = atomisp_video_stable(asd, 0, &control->value);
1317 		break;
1318 	case V4L2_CID_ATOMISP_FIXED_PATTERN_NR:
1319 		ret = atomisp_fixed_pattern(asd, 0, &control->value);
1320 		break;
1321 	case V4L2_CID_ATOMISP_FALSE_COLOR_CORRECTION:
1322 		ret = atomisp_false_color(asd, 0, &control->value);
1323 		break;
1324 	case V4L2_CID_ATOMISP_LOW_LIGHT:
1325 		ret = atomisp_low_light(asd, 0, &control->value);
1326 		break;
1327 	default:
1328 		ret = -EINVAL;
1329 		break;
1330 	}
1331 
1332 	return ret;
1333 }
1334 
1335 /*
1336  * To change the value of a control.
1337  * applications initialize the id and value fields of a struct v4l2_control
1338  * and call this ioctl.
1339  */
1340 static int atomisp_s_ctrl(struct file *file, void *fh,
1341 			  struct v4l2_control *control)
1342 {
1343 	struct video_device *vdev = video_devdata(file);
1344 	struct atomisp_sub_device *asd = atomisp_to_video_pipe(vdev)->asd;
1345 	struct atomisp_device *isp = video_get_drvdata(vdev);
1346 	int i, ret = -EINVAL;
1347 
1348 	for (i = 0; i < ctrls_num; i++) {
1349 		if (ci_v4l2_controls[i].id == control->id) {
1350 			ret = 0;
1351 			break;
1352 		}
1353 	}
1354 
1355 	if (ret)
1356 		return ret;
1357 
1358 	switch (control->id) {
1359 	case V4L2_CID_AUTO_N_PRESET_WHITE_BALANCE:
1360 	case V4L2_CID_EXPOSURE:
1361 	case V4L2_CID_EXPOSURE_AUTO:
1362 	case V4L2_CID_EXPOSURE_AUTO_PRIORITY:
1363 	case V4L2_CID_SCENE_MODE:
1364 	case V4L2_CID_ISO_SENSITIVITY:
1365 	case V4L2_CID_ISO_SENSITIVITY_AUTO:
1366 	case V4L2_CID_POWER_LINE_FREQUENCY:
1367 	case V4L2_CID_EXPOSURE_METERING:
1368 	case V4L2_CID_CONTRAST:
1369 	case V4L2_CID_SATURATION:
1370 	case V4L2_CID_SHARPNESS:
1371 	case V4L2_CID_3A_LOCK:
1372 	case V4L2_CID_COLORFX_CBCR:
1373 	case V4L2_CID_TEST_PATTERN:
1374 	case V4L2_CID_TEST_PATTERN_COLOR_R:
1375 	case V4L2_CID_TEST_PATTERN_COLOR_GR:
1376 	case V4L2_CID_TEST_PATTERN_COLOR_GB:
1377 	case V4L2_CID_TEST_PATTERN_COLOR_B:
1378 		return v4l2_s_ctrl(NULL,
1379 				   isp->inputs[asd->input_curr].camera->
1380 				   ctrl_handler, control);
1381 	case V4L2_CID_COLORFX:
1382 		ret = atomisp_color_effect(asd, 1, &control->value);
1383 		break;
1384 	case V4L2_CID_ATOMISP_BAD_PIXEL_DETECTION:
1385 		ret = atomisp_bad_pixel(asd, 1, &control->value);
1386 		break;
1387 	case V4L2_CID_ATOMISP_POSTPROCESS_GDC_CAC:
1388 		ret = atomisp_gdc_cac(asd, 1, &control->value);
1389 		break;
1390 	case V4L2_CID_ATOMISP_VIDEO_STABLIZATION:
1391 		ret = atomisp_video_stable(asd, 1, &control->value);
1392 		break;
1393 	case V4L2_CID_ATOMISP_FIXED_PATTERN_NR:
1394 		ret = atomisp_fixed_pattern(asd, 1, &control->value);
1395 		break;
1396 	case V4L2_CID_ATOMISP_FALSE_COLOR_CORRECTION:
1397 		ret = atomisp_false_color(asd, 1, &control->value);
1398 		break;
1399 	case V4L2_CID_REQUEST_FLASH:
1400 		ret = atomisp_flash_enable(asd, control->value);
1401 		break;
1402 	case V4L2_CID_ATOMISP_LOW_LIGHT:
1403 		ret = atomisp_low_light(asd, 1, &control->value);
1404 		break;
1405 	default:
1406 		ret = -EINVAL;
1407 		break;
1408 	}
1409 	return ret;
1410 }
1411 
1412 /*
1413  * To query the attributes of a control.
1414  * applications set the id field of a struct v4l2_queryctrl and call the
1415  * this ioctl with a pointer to this structure. The driver fills
1416  * the rest of the structure.
1417  */
1418 static int atomisp_queryctl(struct file *file, void *fh,
1419 			    struct v4l2_queryctrl *qc)
1420 {
1421 	int i, ret = -EINVAL;
1422 	struct video_device *vdev = video_devdata(file);
1423 	struct atomisp_sub_device *asd = atomisp_to_video_pipe(vdev)->asd;
1424 	struct atomisp_device *isp = video_get_drvdata(vdev);
1425 
1426 	switch (qc->id) {
1427 	case V4L2_CID_FOCUS_ABSOLUTE:
1428 	case V4L2_CID_FOCUS_RELATIVE:
1429 	case V4L2_CID_FOCUS_STATUS:
1430 		if (!IS_ISP2401) {
1431 			return v4l2_queryctrl(isp->inputs[asd->input_curr].camera->
1432 					    ctrl_handler, qc);
1433 		}
1434 		/* ISP2401 */
1435 		if (isp->motor)
1436 			return v4l2_queryctrl(isp->motor->ctrl_handler, qc);
1437 		else
1438 			return v4l2_queryctrl(isp->inputs[asd->input_curr].
1439 					      camera->ctrl_handler, qc);
1440 	}
1441 
1442 	if (qc->id & V4L2_CTRL_FLAG_NEXT_CTRL)
1443 		return ret;
1444 
1445 	for (i = 0; i < ctrls_num; i++) {
1446 		if (ci_v4l2_controls[i].id == qc->id) {
1447 			memcpy(qc, &ci_v4l2_controls[i],
1448 			       sizeof(struct v4l2_queryctrl));
1449 			qc->reserved[0] = 0;
1450 			ret = 0;
1451 			break;
1452 		}
1453 	}
1454 	if (ret != 0)
1455 		qc->flags = V4L2_CTRL_FLAG_DISABLED;
1456 
1457 	return ret;
1458 }
1459 
1460 static int atomisp_camera_g_ext_ctrls(struct file *file, void *fh,
1461 				      struct v4l2_ext_controls *c)
1462 {
1463 	struct video_device *vdev = video_devdata(file);
1464 	struct atomisp_sub_device *asd = atomisp_to_video_pipe(vdev)->asd;
1465 	struct atomisp_device *isp = video_get_drvdata(vdev);
1466 	struct v4l2_subdev *motor;
1467 	struct v4l2_control ctrl;
1468 	int i;
1469 	int ret = 0;
1470 
1471 	if (!IS_ISP2401)
1472 		motor = isp->inputs[asd->input_curr].motor;
1473 	else
1474 		motor = isp->motor;
1475 
1476 	for (i = 0; i < c->count; i++) {
1477 		ctrl.id = c->controls[i].id;
1478 		ctrl.value = c->controls[i].value;
1479 		switch (ctrl.id) {
1480 		case V4L2_CID_EXPOSURE_ABSOLUTE:
1481 		case V4L2_CID_EXPOSURE_AUTO:
1482 		case V4L2_CID_IRIS_ABSOLUTE:
1483 		case V4L2_CID_3A_LOCK:
1484 		case V4L2_CID_TEST_PATTERN:
1485 		case V4L2_CID_TEST_PATTERN_COLOR_R:
1486 		case V4L2_CID_TEST_PATTERN_COLOR_GR:
1487 		case V4L2_CID_TEST_PATTERN_COLOR_GB:
1488 		case V4L2_CID_TEST_PATTERN_COLOR_B:
1489 			/*
1490 			 * Exposure related control will be handled by sensor
1491 			 * driver
1492 			 */
1493 			ret =
1494 			    v4l2_g_ctrl(isp->inputs[asd->input_curr].camera->
1495 					ctrl_handler, &ctrl);
1496 			break;
1497 		case V4L2_CID_FOCUS_ABSOLUTE:
1498 		case V4L2_CID_FOCUS_RELATIVE:
1499 		case V4L2_CID_FOCUS_STATUS:
1500 		case V4L2_CID_FOCUS_AUTO:
1501 			if (motor)
1502 				ret = v4l2_g_ctrl(motor->ctrl_handler, &ctrl);
1503 			break;
1504 		case V4L2_CID_FLASH_STATUS:
1505 		case V4L2_CID_FLASH_INTENSITY:
1506 		case V4L2_CID_FLASH_TORCH_INTENSITY:
1507 		case V4L2_CID_FLASH_INDICATOR_INTENSITY:
1508 		case V4L2_CID_FLASH_TIMEOUT:
1509 		case V4L2_CID_FLASH_STROBE:
1510 		case V4L2_CID_FLASH_MODE:
1511 		case V4L2_CID_FLASH_STATUS_REGISTER:
1512 			if (isp->flash)
1513 				ret =
1514 				    v4l2_g_ctrl(isp->flash->ctrl_handler,
1515 						&ctrl);
1516 			break;
1517 		case V4L2_CID_ZOOM_ABSOLUTE:
1518 			ret = atomisp_digital_zoom(asd, 0, &ctrl.value);
1519 			break;
1520 		case V4L2_CID_G_SKIP_FRAMES:
1521 			ret = v4l2_subdev_call(
1522 				  isp->inputs[asd->input_curr].camera,
1523 				  sensor, g_skip_frames, (u32 *)&ctrl.value);
1524 			break;
1525 		default:
1526 			ret = -EINVAL;
1527 		}
1528 
1529 		if (ret) {
1530 			c->error_idx = i;
1531 			break;
1532 		}
1533 		c->controls[i].value = ctrl.value;
1534 	}
1535 	return ret;
1536 }
1537 
1538 /* This ioctl allows the application to get multiple controls by class */
1539 static int atomisp_g_ext_ctrls(struct file *file, void *fh,
1540 			       struct v4l2_ext_controls *c)
1541 {
1542 	struct v4l2_control ctrl;
1543 	int i, ret = 0;
1544 
1545 	/*
1546 	 * input_lock is not need for the Camera related IOCTLs
1547 	 * The input_lock downgrade the FPS of 3A
1548 	 */
1549 	ret = atomisp_camera_g_ext_ctrls(file, fh, c);
1550 	if (ret != -EINVAL)
1551 		return ret;
1552 
1553 	for (i = 0; i < c->count; i++) {
1554 		ctrl.id = c->controls[i].id;
1555 		ctrl.value = c->controls[i].value;
1556 		ret = atomisp_g_ctrl(file, fh, &ctrl);
1557 		c->controls[i].value = ctrl.value;
1558 		if (ret) {
1559 			c->error_idx = i;
1560 			break;
1561 		}
1562 	}
1563 	return ret;
1564 }
1565 
1566 static int atomisp_camera_s_ext_ctrls(struct file *file, void *fh,
1567 				      struct v4l2_ext_controls *c)
1568 {
1569 	struct video_device *vdev = video_devdata(file);
1570 	struct atomisp_sub_device *asd = atomisp_to_video_pipe(vdev)->asd;
1571 	struct atomisp_device *isp = video_get_drvdata(vdev);
1572 	struct v4l2_subdev *motor;
1573 	struct v4l2_control ctrl;
1574 	int i;
1575 	int ret = 0;
1576 
1577 	if (!IS_ISP2401)
1578 		motor = isp->inputs[asd->input_curr].motor;
1579 	else
1580 		motor = isp->motor;
1581 
1582 	for (i = 0; i < c->count; i++) {
1583 		struct v4l2_ctrl *ctr;
1584 
1585 		ctrl.id = c->controls[i].id;
1586 		ctrl.value = c->controls[i].value;
1587 		switch (ctrl.id) {
1588 		case V4L2_CID_EXPOSURE_ABSOLUTE:
1589 		case V4L2_CID_EXPOSURE_AUTO:
1590 		case V4L2_CID_EXPOSURE_METERING:
1591 		case V4L2_CID_IRIS_ABSOLUTE:
1592 		case V4L2_CID_VCM_TIMING:
1593 		case V4L2_CID_VCM_SLEW:
1594 		case V4L2_CID_3A_LOCK:
1595 		case V4L2_CID_TEST_PATTERN:
1596 		case V4L2_CID_TEST_PATTERN_COLOR_R:
1597 		case V4L2_CID_TEST_PATTERN_COLOR_GR:
1598 		case V4L2_CID_TEST_PATTERN_COLOR_GB:
1599 		case V4L2_CID_TEST_PATTERN_COLOR_B:
1600 			ret = v4l2_s_ctrl(NULL,
1601 					  isp->inputs[asd->input_curr].camera->
1602 					  ctrl_handler, &ctrl);
1603 			break;
1604 		case V4L2_CID_FOCUS_ABSOLUTE:
1605 		case V4L2_CID_FOCUS_RELATIVE:
1606 		case V4L2_CID_FOCUS_STATUS:
1607 		case V4L2_CID_FOCUS_AUTO:
1608 			if (motor)
1609 				ret = v4l2_s_ctrl(NULL, motor->ctrl_handler,
1610 						  &ctrl);
1611 			else
1612 				ret = v4l2_s_ctrl(NULL,
1613 						  isp->inputs[asd->input_curr].
1614 						  camera->ctrl_handler, &ctrl);
1615 			break;
1616 		case V4L2_CID_FLASH_STATUS:
1617 		case V4L2_CID_FLASH_INTENSITY:
1618 		case V4L2_CID_FLASH_TORCH_INTENSITY:
1619 		case V4L2_CID_FLASH_INDICATOR_INTENSITY:
1620 		case V4L2_CID_FLASH_TIMEOUT:
1621 		case V4L2_CID_FLASH_STROBE:
1622 		case V4L2_CID_FLASH_MODE:
1623 		case V4L2_CID_FLASH_STATUS_REGISTER:
1624 			if (isp->flash) {
1625 				ret =
1626 				    v4l2_s_ctrl(NULL, isp->flash->ctrl_handler,
1627 						&ctrl);
1628 				/*
1629 				 * When flash mode is changed we need to reset
1630 				 * flash state
1631 				 */
1632 				if (ctrl.id == V4L2_CID_FLASH_MODE) {
1633 					asd->params.flash_state =
1634 					    ATOMISP_FLASH_IDLE;
1635 					asd->params.num_flash_frames = 0;
1636 				}
1637 			}
1638 			break;
1639 		case V4L2_CID_ZOOM_ABSOLUTE:
1640 			ret = atomisp_digital_zoom(asd, 1, &ctrl.value);
1641 			break;
1642 		default:
1643 			ctr = v4l2_ctrl_find(&asd->ctrl_handler, ctrl.id);
1644 			if (ctr)
1645 				ret = v4l2_ctrl_s_ctrl(ctr, ctrl.value);
1646 			else
1647 				ret = -EINVAL;
1648 		}
1649 
1650 		if (ret) {
1651 			c->error_idx = i;
1652 			break;
1653 		}
1654 		c->controls[i].value = ctrl.value;
1655 	}
1656 	return ret;
1657 }
1658 
1659 /* This ioctl allows the application to set multiple controls by class */
1660 static int atomisp_s_ext_ctrls(struct file *file, void *fh,
1661 			       struct v4l2_ext_controls *c)
1662 {
1663 	struct v4l2_control ctrl;
1664 	int i, ret = 0;
1665 
1666 	/*
1667 	 * input_lock is not need for the Camera related IOCTLs
1668 	 * The input_lock downgrade the FPS of 3A
1669 	 */
1670 	ret = atomisp_camera_s_ext_ctrls(file, fh, c);
1671 	if (ret != -EINVAL)
1672 		return ret;
1673 
1674 	for (i = 0; i < c->count; i++) {
1675 		ctrl.id = c->controls[i].id;
1676 		ctrl.value = c->controls[i].value;
1677 		ret = atomisp_s_ctrl(file, fh, &ctrl);
1678 		c->controls[i].value = ctrl.value;
1679 		if (ret) {
1680 			c->error_idx = i;
1681 			break;
1682 		}
1683 	}
1684 	return ret;
1685 }
1686 
1687 /*
1688  * vidioc_g/s_param are used to switch isp running mode
1689  */
1690 static int atomisp_g_parm(struct file *file, void *fh,
1691 			  struct v4l2_streamparm *parm)
1692 {
1693 	struct video_device *vdev = video_devdata(file);
1694 	struct atomisp_sub_device *asd = atomisp_to_video_pipe(vdev)->asd;
1695 	struct atomisp_device *isp = video_get_drvdata(vdev);
1696 
1697 	if (parm->type != V4L2_BUF_TYPE_VIDEO_CAPTURE) {
1698 		dev_err(isp->dev, "unsupported v4l2 buf type\n");
1699 		return -EINVAL;
1700 	}
1701 
1702 	parm->parm.capture.capturemode = asd->run_mode->val;
1703 
1704 	return 0;
1705 }
1706 
1707 static int atomisp_s_parm(struct file *file, void *fh,
1708 			  struct v4l2_streamparm *parm)
1709 {
1710 	struct video_device *vdev = video_devdata(file);
1711 	struct atomisp_device *isp = video_get_drvdata(vdev);
1712 	struct atomisp_sub_device *asd = atomisp_to_video_pipe(vdev)->asd;
1713 	int mode;
1714 	int rval;
1715 	int fps;
1716 
1717 	if (parm->type != V4L2_BUF_TYPE_VIDEO_CAPTURE) {
1718 		dev_err(isp->dev, "unsupported v4l2 buf type\n");
1719 		return -EINVAL;
1720 	}
1721 
1722 	asd->high_speed_mode = false;
1723 	switch (parm->parm.capture.capturemode) {
1724 	case CI_MODE_NONE: {
1725 		struct v4l2_subdev_frame_interval fi = {0};
1726 
1727 		fi.interval = parm->parm.capture.timeperframe;
1728 
1729 		rval = v4l2_subdev_call(isp->inputs[asd->input_curr].camera,
1730 					video, s_frame_interval, &fi);
1731 		if (!rval)
1732 			parm->parm.capture.timeperframe = fi.interval;
1733 
1734 		if (fi.interval.numerator != 0) {
1735 			fps = fi.interval.denominator / fi.interval.numerator;
1736 			if (fps > 30)
1737 				asd->high_speed_mode = true;
1738 		}
1739 
1740 		return rval == -ENOIOCTLCMD ? 0 : rval;
1741 	}
1742 	case CI_MODE_VIDEO:
1743 		mode = ATOMISP_RUN_MODE_VIDEO;
1744 		break;
1745 	case CI_MODE_STILL_CAPTURE:
1746 		mode = ATOMISP_RUN_MODE_STILL_CAPTURE;
1747 		break;
1748 	case CI_MODE_PREVIEW:
1749 		mode = ATOMISP_RUN_MODE_PREVIEW;
1750 		break;
1751 	default:
1752 		return -EINVAL;
1753 	}
1754 
1755 	rval = v4l2_ctrl_s_ctrl(asd->run_mode, mode);
1756 
1757 	return rval == -ENOIOCTLCMD ? 0 : rval;
1758 }
1759 
1760 static long atomisp_vidioc_default(struct file *file, void *fh,
1761 				   bool valid_prio, unsigned int cmd, void *arg)
1762 {
1763 	struct video_device *vdev = video_devdata(file);
1764 	struct atomisp_device *isp = video_get_drvdata(vdev);
1765 	struct atomisp_sub_device *asd = atomisp_to_video_pipe(vdev)->asd;
1766 	int err;
1767 
1768 	switch (cmd) {
1769 	case ATOMISP_IOC_S_SENSOR_RUNMODE:
1770 		if (IS_ISP2401)
1771 			err = atomisp_set_sensor_runmode(asd, arg);
1772 		else
1773 			err = -EINVAL;
1774 		break;
1775 
1776 	case ATOMISP_IOC_G_XNR:
1777 		err = atomisp_xnr(asd, 0, arg);
1778 		break;
1779 
1780 	case ATOMISP_IOC_S_XNR:
1781 		err = atomisp_xnr(asd, 1, arg);
1782 		break;
1783 
1784 	case ATOMISP_IOC_G_NR:
1785 		err = atomisp_nr(asd, 0, arg);
1786 		break;
1787 
1788 	case ATOMISP_IOC_S_NR:
1789 		err = atomisp_nr(asd, 1, arg);
1790 		break;
1791 
1792 	case ATOMISP_IOC_G_TNR:
1793 		err = atomisp_tnr(asd, 0, arg);
1794 		break;
1795 
1796 	case ATOMISP_IOC_S_TNR:
1797 		err = atomisp_tnr(asd, 1, arg);
1798 		break;
1799 
1800 	case ATOMISP_IOC_G_BLACK_LEVEL_COMP:
1801 		err = atomisp_black_level(asd, 0, arg);
1802 		break;
1803 
1804 	case ATOMISP_IOC_S_BLACK_LEVEL_COMP:
1805 		err = atomisp_black_level(asd, 1, arg);
1806 		break;
1807 
1808 	case ATOMISP_IOC_G_EE:
1809 		err = atomisp_ee(asd, 0, arg);
1810 		break;
1811 
1812 	case ATOMISP_IOC_S_EE:
1813 		err = atomisp_ee(asd, 1, arg);
1814 		break;
1815 
1816 	case ATOMISP_IOC_G_DIS_STAT:
1817 		err = atomisp_get_dis_stat(asd, arg);
1818 		break;
1819 
1820 	case ATOMISP_IOC_G_DVS2_BQ_RESOLUTIONS:
1821 		err = atomisp_get_dvs2_bq_resolutions(asd, arg);
1822 		break;
1823 
1824 	case ATOMISP_IOC_S_DIS_COEFS:
1825 		err = atomisp_css_cp_dvs2_coefs(asd, arg,
1826 						&asd->params.css_param, true);
1827 		if (!err && arg)
1828 			asd->params.css_update_params_needed = true;
1829 		break;
1830 
1831 	case ATOMISP_IOC_S_DIS_VECTOR:
1832 		err = atomisp_cp_dvs_6axis_config(asd, arg,
1833 						  &asd->params.css_param, true);
1834 		if (!err && arg)
1835 			asd->params.css_update_params_needed = true;
1836 		break;
1837 
1838 	case ATOMISP_IOC_G_ISP_PARM:
1839 		err = atomisp_param(asd, 0, arg);
1840 		break;
1841 
1842 	case ATOMISP_IOC_S_ISP_PARM:
1843 		err = atomisp_param(asd, 1, arg);
1844 		break;
1845 
1846 	case ATOMISP_IOC_G_3A_STAT:
1847 		err = atomisp_3a_stat(asd, 0, arg);
1848 		break;
1849 
1850 	case ATOMISP_IOC_G_ISP_GAMMA:
1851 		err = atomisp_gamma(asd, 0, arg);
1852 		break;
1853 
1854 	case ATOMISP_IOC_S_ISP_GAMMA:
1855 		err = atomisp_gamma(asd, 1, arg);
1856 		break;
1857 
1858 	case ATOMISP_IOC_G_ISP_GDC_TAB:
1859 		err = atomisp_gdc_cac_table(asd, 0, arg);
1860 		break;
1861 
1862 	case ATOMISP_IOC_S_ISP_GDC_TAB:
1863 		err = atomisp_gdc_cac_table(asd, 1, arg);
1864 		break;
1865 
1866 	case ATOMISP_IOC_G_ISP_MACC:
1867 		err = atomisp_macc_table(asd, 0, arg);
1868 		break;
1869 
1870 	case ATOMISP_IOC_S_ISP_MACC:
1871 		err = atomisp_macc_table(asd, 1, arg);
1872 		break;
1873 
1874 	case ATOMISP_IOC_G_ISP_BAD_PIXEL_DETECTION:
1875 		err = atomisp_bad_pixel_param(asd, 0, arg);
1876 		break;
1877 
1878 	case ATOMISP_IOC_S_ISP_BAD_PIXEL_DETECTION:
1879 		err = atomisp_bad_pixel_param(asd, 1, arg);
1880 		break;
1881 
1882 	case ATOMISP_IOC_G_ISP_FALSE_COLOR_CORRECTION:
1883 		err = atomisp_false_color_param(asd, 0, arg);
1884 		break;
1885 
1886 	case ATOMISP_IOC_S_ISP_FALSE_COLOR_CORRECTION:
1887 		err = atomisp_false_color_param(asd, 1, arg);
1888 		break;
1889 
1890 	case ATOMISP_IOC_G_ISP_CTC:
1891 		err = atomisp_ctc(asd, 0, arg);
1892 		break;
1893 
1894 	case ATOMISP_IOC_S_ISP_CTC:
1895 		err = atomisp_ctc(asd, 1, arg);
1896 		break;
1897 
1898 	case ATOMISP_IOC_G_ISP_WHITE_BALANCE:
1899 		err = atomisp_white_balance_param(asd, 0, arg);
1900 		break;
1901 
1902 	case ATOMISP_IOC_S_ISP_WHITE_BALANCE:
1903 		err = atomisp_white_balance_param(asd, 1, arg);
1904 		break;
1905 
1906 	case ATOMISP_IOC_G_3A_CONFIG:
1907 		err = atomisp_3a_config_param(asd, 0, arg);
1908 		break;
1909 
1910 	case ATOMISP_IOC_S_3A_CONFIG:
1911 		err = atomisp_3a_config_param(asd, 1, arg);
1912 		break;
1913 
1914 	case ATOMISP_IOC_S_ISP_FPN_TABLE:
1915 		err = atomisp_fixed_pattern_table(asd, arg);
1916 		break;
1917 
1918 	case ATOMISP_IOC_S_EXPOSURE:
1919 		err = v4l2_subdev_call(isp->inputs[asd->input_curr].camera,
1920 				       core, ioctl, cmd, arg);
1921 		break;
1922 
1923 	case ATOMISP_IOC_S_ISP_SHD_TAB:
1924 		err = atomisp_set_shading_table(asd, arg);
1925 		break;
1926 
1927 	case ATOMISP_IOC_G_ISP_GAMMA_CORRECTION:
1928 		err = atomisp_gamma_correction(asd, 0, arg);
1929 		break;
1930 
1931 	case ATOMISP_IOC_S_ISP_GAMMA_CORRECTION:
1932 		err = atomisp_gamma_correction(asd, 1, arg);
1933 		break;
1934 
1935 	case ATOMISP_IOC_S_PARAMETERS:
1936 		err = atomisp_set_parameters(vdev, arg);
1937 		break;
1938 
1939 	case ATOMISP_IOC_EXT_ISP_CTRL:
1940 		err = v4l2_subdev_call(isp->inputs[asd->input_curr].camera,
1941 				       core, ioctl, cmd, arg);
1942 		break;
1943 	case ATOMISP_IOC_EXP_ID_UNLOCK:
1944 		err = atomisp_exp_id_unlock(asd, arg);
1945 		break;
1946 	case ATOMISP_IOC_EXP_ID_CAPTURE:
1947 		err = atomisp_exp_id_capture(asd, arg);
1948 		break;
1949 	case ATOMISP_IOC_S_ENABLE_DZ_CAPT_PIPE:
1950 		err = atomisp_enable_dz_capt_pipe(asd, arg);
1951 		break;
1952 	case ATOMISP_IOC_G_FORMATS_CONFIG:
1953 		err = atomisp_formats(asd, 0, arg);
1954 		break;
1955 
1956 	case ATOMISP_IOC_S_FORMATS_CONFIG:
1957 		err = atomisp_formats(asd, 1, arg);
1958 		break;
1959 	case ATOMISP_IOC_INJECT_A_FAKE_EVENT:
1960 		err = atomisp_inject_a_fake_event(asd, arg);
1961 		break;
1962 	case ATOMISP_IOC_S_ARRAY_RESOLUTION:
1963 		err = atomisp_set_array_res(asd, arg);
1964 		break;
1965 	default:
1966 		err = -EINVAL;
1967 		break;
1968 	}
1969 
1970 	return err;
1971 }
1972 
1973 const struct v4l2_ioctl_ops atomisp_ioctl_ops = {
1974 	.vidioc_querycap = atomisp_querycap,
1975 	.vidioc_enum_input = atomisp_enum_input,
1976 	.vidioc_g_input = atomisp_g_input,
1977 	.vidioc_s_input = atomisp_s_input,
1978 	.vidioc_queryctrl = atomisp_queryctl,
1979 	.vidioc_s_ctrl = atomisp_s_ctrl,
1980 	.vidioc_g_ctrl = atomisp_g_ctrl,
1981 	.vidioc_s_ext_ctrls = atomisp_s_ext_ctrls,
1982 	.vidioc_g_ext_ctrls = atomisp_g_ext_ctrls,
1983 	.vidioc_enum_framesizes   = atomisp_enum_framesizes,
1984 	.vidioc_enum_frameintervals = atomisp_enum_frameintervals,
1985 	.vidioc_enum_fmt_vid_cap = atomisp_enum_fmt_cap,
1986 	.vidioc_try_fmt_vid_cap = atomisp_try_fmt_cap,
1987 	.vidioc_g_fmt_vid_cap = atomisp_g_fmt_cap,
1988 	.vidioc_s_fmt_vid_cap = atomisp_s_fmt_cap,
1989 	.vidioc_reqbufs = vb2_ioctl_reqbufs,
1990 	.vidioc_querybuf = vb2_ioctl_querybuf,
1991 	.vidioc_qbuf = atomisp_qbuf_wrapper,
1992 	.vidioc_dqbuf = atomisp_dqbuf_wrapper,
1993 	.vidioc_streamon = vb2_ioctl_streamon,
1994 	.vidioc_streamoff = vb2_ioctl_streamoff,
1995 	.vidioc_default = atomisp_vidioc_default,
1996 	.vidioc_s_parm = atomisp_s_parm,
1997 	.vidioc_g_parm = atomisp_g_parm,
1998 };
1999