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 *
atomisp_get_format_bridge(unsigned int pixelformat)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 *
atomisp_get_format_bridge_from_mbus(u32 mbus_code)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 
atomisp_pipe_check(struct atomisp_video_pipe * pipe,bool settings_change)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 	switch (pipe->asd->streaming) {
532 	case ATOMISP_DEVICE_STREAMING_DISABLED:
533 		break;
534 	case ATOMISP_DEVICE_STREAMING_ENABLED:
535 		if (settings_change) {
536 			dev_err(pipe->isp->dev, "Set fmt/input IOCTL while streaming\n");
537 			return -EBUSY;
538 		}
539 		break;
540 	case ATOMISP_DEVICE_STREAMING_STOPPING:
541 		dev_err(pipe->isp->dev, "IOCTL issued while stopping\n");
542 		return -EBUSY;
543 	default:
544 		return -EINVAL;
545 	}
546 
547 	return 0;
548 }
549 
550 /*
551  * v4l2 ioctls
552  * return ISP capabilities
553  */
atomisp_querycap(struct file * file,void * fh,struct v4l2_capability * cap)554 static int atomisp_querycap(struct file *file, void *fh,
555 			    struct v4l2_capability *cap)
556 {
557 	struct video_device *vdev = video_devdata(file);
558 	struct atomisp_device *isp = video_get_drvdata(vdev);
559 
560 	strscpy(cap->driver, DRIVER, sizeof(cap->driver));
561 	strscpy(cap->card, CARD, sizeof(cap->card));
562 	snprintf(cap->bus_info, sizeof(cap->bus_info), "PCI:%s", dev_name(isp->dev));
563 
564 	return 0;
565 }
566 
567 /*
568  * enum input are used to check primary/secondary camera
569  */
atomisp_enum_input(struct file * file,void * fh,struct v4l2_input * input)570 static int atomisp_enum_input(struct file *file, void *fh,
571 			      struct v4l2_input *input)
572 {
573 	struct video_device *vdev = video_devdata(file);
574 	struct atomisp_device *isp = video_get_drvdata(vdev);
575 	int index = input->index;
576 	struct v4l2_subdev *motor;
577 
578 	if (index >= isp->input_cnt)
579 		return -EINVAL;
580 
581 	if (!isp->inputs[index].camera)
582 		return -EINVAL;
583 
584 	memset(input, 0, sizeof(struct v4l2_input));
585 	strscpy(input->name, isp->inputs[index].camera->name,
586 		sizeof(input->name));
587 
588 	/*
589 	 * HACK: append actuator's name to sensor's
590 	 * As currently userspace can't talk directly to subdev nodes, this
591 	 * ioctl is the only way to enum inputs + possible external actuators
592 	 * for 3A tuning purpose.
593 	 */
594 	if (!IS_ISP2401)
595 		motor = isp->inputs[index].motor;
596 	else
597 		motor = isp->motor;
598 
599 	if (motor && strlen(motor->name) > 0) {
600 		const int cur_len = strlen(input->name);
601 		const int max_size = sizeof(input->name) - cur_len - 1;
602 
603 		if (max_size > 1) {
604 			input->name[cur_len] = '+';
605 			strscpy(&input->name[cur_len + 1],
606 				motor->name, max_size);
607 		}
608 	}
609 
610 	input->type = V4L2_INPUT_TYPE_CAMERA;
611 	input->index = index;
612 	input->reserved[0] = isp->inputs[index].type;
613 	input->reserved[1] = isp->inputs[index].port;
614 
615 	return 0;
616 }
617 
618 static unsigned int
atomisp_subdev_streaming_count(struct atomisp_sub_device * asd)619 atomisp_subdev_streaming_count(struct atomisp_sub_device *asd)
620 {
621 	return vb2_start_streaming_called(&asd->video_out_preview.vb_queue) +
622 	       vb2_start_streaming_called(&asd->video_out_capture.vb_queue) +
623 	       vb2_start_streaming_called(&asd->video_out_video_capture.vb_queue) +
624 	       vb2_start_streaming_called(&asd->video_out_vf.vb_queue);
625 }
626 
atomisp_streaming_count(struct atomisp_device * isp)627 unsigned int atomisp_streaming_count(struct atomisp_device *isp)
628 {
629 	unsigned int i, sum;
630 
631 	for (i = 0, sum = 0; i < isp->num_of_streams; i++)
632 		sum += isp->asd[i].streaming ==
633 		       ATOMISP_DEVICE_STREAMING_ENABLED;
634 
635 	return sum;
636 }
637 
638 /*
639  * get input are used to get current primary/secondary camera
640  */
atomisp_g_input(struct file * file,void * fh,unsigned int * input)641 static int atomisp_g_input(struct file *file, void *fh, unsigned int *input)
642 {
643 	struct video_device *vdev = video_devdata(file);
644 	struct atomisp_sub_device *asd = atomisp_to_video_pipe(vdev)->asd;
645 
646 	*input = asd->input_curr;
647 	return 0;
648 }
649 
atomisp_s_fmt_cap(struct file * file,void * fh,struct v4l2_format * f)650 static int atomisp_s_fmt_cap(struct file *file, void *fh,
651 			     struct v4l2_format *f)
652 {
653 	struct video_device *vdev = video_devdata(file);
654 
655 	return atomisp_set_fmt(vdev, f);
656 }
657 
658 /*
659  * set input are used to set current primary/secondary camera
660  */
atomisp_s_input(struct file * file,void * fh,unsigned int input)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_video_pipe *pipe = atomisp_to_video_pipe(vdev);
666 	struct atomisp_sub_device *asd = pipe->asd;
667 	struct v4l2_subdev *camera = NULL;
668 	struct v4l2_subdev *motor;
669 	int ret;
670 
671 	ret = atomisp_pipe_check(pipe, true);
672 	if (ret)
673 		return ret;
674 
675 	if (input >= ATOM_ISP_MAX_INPUTS || input >= isp->input_cnt) {
676 		dev_dbg(isp->dev, "input_cnt: %d\n", isp->input_cnt);
677 		return -EINVAL;
678 	}
679 
680 	/*
681 	 * check whether the request camera:
682 	 * 1: already in use
683 	 * 2: if in use, whether it is used by other streams
684 	 */
685 	if (isp->inputs[input].asd && isp->inputs[input].asd != asd) {
686 		dev_err(isp->dev,
687 			"%s, camera is already used by stream: %d\n", __func__,
688 			isp->inputs[input].asd->index);
689 		return -EBUSY;
690 	}
691 
692 	camera = isp->inputs[input].camera;
693 	if (!camera) {
694 		dev_err(isp->dev, "%s, no camera\n", __func__);
695 		return -EINVAL;
696 	}
697 
698 	/* power off the current owned sensor, as it is not used this time */
699 	if (isp->inputs[asd->input_curr].asd == asd &&
700 	    asd->input_curr != input) {
701 		ret = v4l2_subdev_call(isp->inputs[asd->input_curr].camera,
702 				       core, s_power, 0);
703 		if (ret && ret != -ENOIOCTLCMD)
704 			dev_warn(isp->dev,
705 				 "Failed to power-off sensor\n");
706 		/* clear the asd field to show this camera is not used */
707 		isp->inputs[asd->input_curr].asd = NULL;
708 	}
709 
710 	/* powe on the new sensor */
711 	ret = v4l2_subdev_call(isp->inputs[input].camera, core, s_power, 1);
712 	if (ret && ret != -ENOIOCTLCMD) {
713 		dev_err(isp->dev, "Failed to power-on sensor\n");
714 		return ret;
715 	}
716 	/*
717 	 * Some sensor driver resets the run mode during power-on, thus force
718 	 * update the run mode to sensor after power-on.
719 	 */
720 	atomisp_update_run_mode(asd);
721 
722 	/* select operating sensor */
723 	ret = v4l2_subdev_call(isp->inputs[input].camera, video, s_routing,
724 			       0, isp->inputs[input].sensor_index, 0);
725 	if (ret && (ret != -ENOIOCTLCMD)) {
726 		dev_err(isp->dev, "Failed to select sensor\n");
727 		return ret;
728 	}
729 
730 	if (!IS_ISP2401) {
731 		motor = isp->inputs[input].motor;
732 	} else {
733 		motor = isp->motor;
734 		if (motor)
735 			ret = v4l2_subdev_call(motor, core, s_power, 1);
736 	}
737 
738 	if (motor)
739 		ret = v4l2_subdev_call(motor, core, init, 1);
740 
741 	asd->input_curr = input;
742 	/* mark this camera is used by the current stream */
743 	isp->inputs[input].asd = asd;
744 
745 	return 0;
746 }
747 
atomisp_enum_framesizes(struct file * file,void * priv,struct v4l2_frmsizeenum * fsize)748 static int atomisp_enum_framesizes(struct file *file, void *priv,
749 				   struct v4l2_frmsizeenum *fsize)
750 {
751 	struct video_device *vdev = video_devdata(file);
752 	struct atomisp_device *isp = video_get_drvdata(vdev);
753 	struct atomisp_sub_device *asd = atomisp_to_video_pipe(vdev)->asd;
754 	struct v4l2_subdev_frame_size_enum fse = {
755 		.index = fsize->index,
756 		.which = V4L2_SUBDEV_FORMAT_ACTIVE,
757 	};
758 	int ret;
759 
760 	ret = v4l2_subdev_call(isp->inputs[asd->input_curr].camera,
761 			       pad, enum_frame_size, NULL, &fse);
762 	if (ret)
763 		return ret;
764 
765 	fsize->type = V4L2_FRMSIZE_TYPE_DISCRETE;
766 	fsize->discrete.width = fse.max_width - pad_w;
767 	fsize->discrete.height = fse.max_height - pad_h;
768 
769 	return 0;
770 }
771 
atomisp_enum_frameintervals(struct file * file,void * priv,struct v4l2_frmivalenum * fival)772 static int atomisp_enum_frameintervals(struct file *file, void *priv,
773 				       struct v4l2_frmivalenum *fival)
774 {
775 	struct video_device *vdev = video_devdata(file);
776 	struct atomisp_device *isp = video_get_drvdata(vdev);
777 	struct atomisp_sub_device *asd = atomisp_to_video_pipe(vdev)->asd;
778 	struct v4l2_subdev_frame_interval_enum fie = {
779 		.code	= atomisp_in_fmt_conv[0].code,
780 		.index = fival->index,
781 		.width = fival->width,
782 		.height = fival->height,
783 		.which = V4L2_SUBDEV_FORMAT_ACTIVE,
784 	};
785 	int ret;
786 
787 	ret = v4l2_subdev_call(isp->inputs[asd->input_curr].camera,
788 			       pad, enum_frame_interval, NULL,
789 			       &fie);
790 	if (ret)
791 		return ret;
792 
793 	fival->type = V4L2_FRMIVAL_TYPE_DISCRETE;
794 	fival->discrete = fie.interval;
795 
796 	return ret;
797 }
798 
atomisp_enum_fmt_cap(struct file * file,void * fh,struct v4l2_fmtdesc * f)799 static int atomisp_enum_fmt_cap(struct file *file, void *fh,
800 				struct v4l2_fmtdesc *f)
801 {
802 	struct video_device *vdev = video_devdata(file);
803 	struct atomisp_device *isp = video_get_drvdata(vdev);
804 	struct atomisp_sub_device *asd = atomisp_to_video_pipe(vdev)->asd;
805 	struct v4l2_subdev_mbus_code_enum code = {
806 		.which = V4L2_SUBDEV_FORMAT_ACTIVE,
807 	};
808 	const struct atomisp_format_bridge *format;
809 	struct v4l2_subdev *camera;
810 	unsigned int i, fi = 0;
811 	int rval;
812 
813 	camera = isp->inputs[asd->input_curr].camera;
814 	if(!camera) {
815 		dev_err(isp->dev, "%s(): camera is NULL, device is %s\n",
816 			__func__, vdev->name);
817 		return -EINVAL;
818 	}
819 
820 	rval = v4l2_subdev_call(camera, pad, enum_mbus_code, NULL, &code);
821 	if (rval == -ENOIOCTLCMD) {
822 		dev_warn(isp->dev,
823 			 "enum_mbus_code pad op not supported by %s. Please fix your sensor driver!\n",
824 			 camera->name);
825 	}
826 
827 	if (rval)
828 		return rval;
829 
830 	for (i = 0; i < ARRAY_SIZE(atomisp_output_fmts); i++) {
831 		format = &atomisp_output_fmts[i];
832 
833 		/*
834 		 * Is the atomisp-supported format is valid for the
835 		 * sensor (configuration)? If not, skip it.
836 		 *
837 		 * FIXME: fix the pipeline to allow sensor format too.
838 		 */
839 		if (format->sh_fmt == IA_CSS_FRAME_FORMAT_RAW)
840 			continue;
841 
842 		/* Found a match. Now let's pick f->index'th one. */
843 		if (fi < f->index) {
844 			fi++;
845 			continue;
846 		}
847 
848 		strscpy(f->description, format->description,
849 			sizeof(f->description));
850 		f->pixelformat = format->pixelformat;
851 		return 0;
852 	}
853 
854 	return -EINVAL;
855 }
856 
atomisp_adjust_fmt(struct v4l2_format * f)857 static int atomisp_adjust_fmt(struct v4l2_format *f)
858 {
859 	const struct atomisp_format_bridge *format_bridge;
860 	u32 padded_width;
861 
862 	format_bridge = atomisp_get_format_bridge(f->fmt.pix.pixelformat);
863 	/* Currently, raw formats are broken!!! */
864 	if (!format_bridge || format_bridge->sh_fmt == IA_CSS_FRAME_FORMAT_RAW) {
865 		f->fmt.pix.pixelformat = V4L2_PIX_FMT_YUV420;
866 
867 		format_bridge = atomisp_get_format_bridge(f->fmt.pix.pixelformat);
868 		if (!format_bridge)
869 			return -EINVAL;
870 	}
871 
872 	padded_width = f->fmt.pix.width + pad_w;
873 
874 	if (format_bridge->planar) {
875 		f->fmt.pix.bytesperline = padded_width;
876 		f->fmt.pix.sizeimage = PAGE_ALIGN(f->fmt.pix.height *
877 						  DIV_ROUND_UP(format_bridge->depth *
878 						  padded_width, 8));
879 	} else {
880 		f->fmt.pix.bytesperline = DIV_ROUND_UP(format_bridge->depth *
881 						      padded_width, 8);
882 		f->fmt.pix.sizeimage = PAGE_ALIGN(f->fmt.pix.height * f->fmt.pix.bytesperline);
883 	}
884 
885 	if (f->fmt.pix.field == V4L2_FIELD_ANY)
886 		f->fmt.pix.field = V4L2_FIELD_NONE;
887 
888 	/*
889 	 * FIXME: do we need to setup this differently, depending on the
890 	 * sensor or the pipeline?
891 	 */
892 	f->fmt.pix.colorspace = V4L2_COLORSPACE_REC709;
893 	f->fmt.pix.ycbcr_enc = V4L2_YCBCR_ENC_709;
894 	f->fmt.pix.xfer_func = V4L2_XFER_FUNC_709;
895 
896 	f->fmt.pix.width -= pad_w;
897 	f->fmt.pix.height -= pad_h;
898 
899 	return 0;
900 }
901 
902 /* This function looks up the closest available resolution. */
atomisp_try_fmt_cap(struct file * file,void * fh,struct v4l2_format * f)903 static int atomisp_try_fmt_cap(struct file *file, void *fh,
904 			       struct v4l2_format *f)
905 {
906 	struct video_device *vdev = video_devdata(file);
907 	u32 pixfmt = f->fmt.pix.pixelformat;
908 	int ret;
909 
910 	/*
911 	 * atomisp_try_fmt() gived results with padding included, note
912 	 * (this gets removed again by the atomisp_adjust_fmt() call below.
913 	 */
914 	f->fmt.pix.width += pad_w;
915 	f->fmt.pix.height += pad_h;
916 
917 	ret = atomisp_try_fmt(vdev, &f->fmt.pix, NULL);
918 	if (ret)
919 		return ret;
920 
921 	/*
922 	 * atomisp_try_fmt() replaces pixelformat with the sensors native
923 	 * format, restore the actual format requested by userspace.
924 	 */
925 	f->fmt.pix.pixelformat = pixfmt;
926 
927 	return atomisp_adjust_fmt(f);
928 }
929 
atomisp_g_fmt_cap(struct file * file,void * fh,struct v4l2_format * f)930 static int atomisp_g_fmt_cap(struct file *file, void *fh,
931 			     struct v4l2_format *f)
932 {
933 	struct video_device *vdev = video_devdata(file);
934 	struct atomisp_video_pipe *pipe;
935 
936 	pipe = atomisp_to_video_pipe(vdev);
937 
938 	f->fmt.pix = pipe->pix;
939 
940 	/* If s_fmt was issued, just return whatever is was previouly set */
941 	if (f->fmt.pix.sizeimage)
942 		return 0;
943 
944 	f->fmt.pix.pixelformat = V4L2_PIX_FMT_YUV420;
945 	f->fmt.pix.width = 10000;
946 	f->fmt.pix.height = 10000;
947 
948 	return atomisp_try_fmt_cap(file, fh, f);
949 }
950 
atomisp_alloc_css_stat_bufs(struct atomisp_sub_device * asd,uint16_t stream_id)951 int atomisp_alloc_css_stat_bufs(struct atomisp_sub_device *asd,
952 				uint16_t stream_id)
953 {
954 	struct atomisp_device *isp = asd->isp;
955 	struct atomisp_s3a_buf *s3a_buf = NULL, *_s3a_buf;
956 	struct atomisp_dis_buf *dis_buf = NULL, *_dis_buf;
957 	struct atomisp_metadata_buf *md_buf = NULL, *_md_buf;
958 	int count;
959 	struct ia_css_dvs_grid_info *dvs_grid_info =
960 	    atomisp_css_get_dvs_grid_info(&asd->params.curr_grid_info);
961 	unsigned int i;
962 
963 	if (list_empty(&asd->s3a_stats) &&
964 	    asd->params.curr_grid_info.s3a_grid.enable) {
965 		count = ATOMISP_CSS_Q_DEPTH +
966 			ATOMISP_S3A_BUF_QUEUE_DEPTH_FOR_HAL;
967 		dev_dbg(isp->dev, "allocating %d 3a buffers\n", count);
968 		while (count--) {
969 			s3a_buf = kzalloc(sizeof(struct atomisp_s3a_buf), GFP_KERNEL);
970 			if (!s3a_buf)
971 				goto error;
972 
973 			if (atomisp_css_allocate_stat_buffers(
974 				asd, stream_id, s3a_buf, NULL, NULL)) {
975 				kfree(s3a_buf);
976 				goto error;
977 			}
978 
979 			list_add_tail(&s3a_buf->list, &asd->s3a_stats);
980 		}
981 	}
982 
983 	if (list_empty(&asd->dis_stats) && dvs_grid_info &&
984 	    dvs_grid_info->enable) {
985 		count = ATOMISP_CSS_Q_DEPTH + 1;
986 		dev_dbg(isp->dev, "allocating %d dis buffers\n", count);
987 		while (count--) {
988 			dis_buf = kzalloc(sizeof(struct atomisp_dis_buf), GFP_KERNEL);
989 			if (!dis_buf)
990 				goto error;
991 			if (atomisp_css_allocate_stat_buffers(
992 				asd, stream_id, NULL, dis_buf, NULL)) {
993 				kfree(dis_buf);
994 				goto error;
995 			}
996 
997 			list_add_tail(&dis_buf->list, &asd->dis_stats);
998 		}
999 	}
1000 
1001 	for (i = 0; i < ATOMISP_METADATA_TYPE_NUM; i++) {
1002 		if (list_empty(&asd->metadata[i]) &&
1003 		    list_empty(&asd->metadata_ready[i]) &&
1004 		    list_empty(&asd->metadata_in_css[i])) {
1005 			count = ATOMISP_CSS_Q_DEPTH +
1006 				ATOMISP_METADATA_QUEUE_DEPTH_FOR_HAL;
1007 			dev_dbg(isp->dev, "allocating %d metadata buffers for type %d\n",
1008 				count, i);
1009 			while (count--) {
1010 				md_buf = kzalloc(sizeof(struct atomisp_metadata_buf),
1011 						 GFP_KERNEL);
1012 				if (!md_buf)
1013 					goto error;
1014 
1015 				if (atomisp_css_allocate_stat_buffers(
1016 					asd, stream_id, NULL, NULL, md_buf)) {
1017 					kfree(md_buf);
1018 					goto error;
1019 				}
1020 				list_add_tail(&md_buf->list, &asd->metadata[i]);
1021 			}
1022 		}
1023 	}
1024 	return 0;
1025 
1026 error:
1027 	dev_err(isp->dev, "failed to allocate statistics buffers\n");
1028 
1029 	list_for_each_entry_safe(dis_buf, _dis_buf, &asd->dis_stats, list) {
1030 		atomisp_css_free_dis_buffer(dis_buf);
1031 		list_del(&dis_buf->list);
1032 		kfree(dis_buf);
1033 	}
1034 
1035 	list_for_each_entry_safe(s3a_buf, _s3a_buf, &asd->s3a_stats, list) {
1036 		atomisp_css_free_3a_buffer(s3a_buf);
1037 		list_del(&s3a_buf->list);
1038 		kfree(s3a_buf);
1039 	}
1040 
1041 	for (i = 0; i < ATOMISP_METADATA_TYPE_NUM; i++) {
1042 		list_for_each_entry_safe(md_buf, _md_buf, &asd->metadata[i],
1043 					 list) {
1044 			atomisp_css_free_metadata_buffer(md_buf);
1045 			list_del(&md_buf->list);
1046 			kfree(md_buf);
1047 		}
1048 	}
1049 	return -ENOMEM;
1050 }
1051 
1052 /*
1053  * FIXME the abuse of buf->reserved2 in the qbuf and dqbuf wrappers comes from
1054  * the original atomisp buffer handling and should be replaced with proper V4L2
1055  * per frame parameters use.
1056  *
1057  * Once this is fixed these wrappers can be removed, replacing them with direct
1058  * calls to vb2_ioctl_[d]qbuf().
1059  */
atomisp_qbuf_wrapper(struct file * file,void * fh,struct v4l2_buffer * buf)1060 static int atomisp_qbuf_wrapper(struct file *file, void *fh, struct v4l2_buffer *buf)
1061 {
1062 	struct video_device *vdev = video_devdata(file);
1063 	struct atomisp_device *isp = video_get_drvdata(vdev);
1064 	struct atomisp_video_pipe *pipe = atomisp_to_video_pipe(vdev);
1065 
1066 	if (buf->index >= vdev->queue->num_buffers)
1067 		return -EINVAL;
1068 
1069 	if (!atomisp_is_vf_pipe(pipe) &&
1070 	    (buf->reserved2 & ATOMISP_BUFFER_HAS_PER_FRAME_SETTING)) {
1071 		/* this buffer will have a per-frame parameter */
1072 		pipe->frame_request_config_id[buf->index] = buf->reserved2 &
1073 			~ATOMISP_BUFFER_HAS_PER_FRAME_SETTING;
1074 		dev_dbg(isp->dev,
1075 			"This buffer requires per_frame setting which has isp_config_id %d\n",
1076 			pipe->frame_request_config_id[buf->index]);
1077 	} else {
1078 		pipe->frame_request_config_id[buf->index] = 0;
1079 	}
1080 
1081 	return vb2_ioctl_qbuf(file, fh, buf);
1082 }
1083 
atomisp_dqbuf_wrapper(struct file * file,void * fh,struct v4l2_buffer * buf)1084 static int atomisp_dqbuf_wrapper(struct file *file, void *fh, struct v4l2_buffer *buf)
1085 {
1086 	struct video_device *vdev = video_devdata(file);
1087 	struct atomisp_video_pipe *pipe = atomisp_to_video_pipe(vdev);
1088 	struct atomisp_sub_device *asd = pipe->asd;
1089 	struct atomisp_device *isp = video_get_drvdata(vdev);
1090 	struct ia_css_frame *frame;
1091 	struct vb2_buffer *vb;
1092 	int ret;
1093 
1094 	ret = vb2_ioctl_dqbuf(file, fh, buf);
1095 	if (ret)
1096 		return ret;
1097 
1098 	vb = pipe->vb_queue.bufs[buf->index];
1099 	frame = vb_to_frame(vb);
1100 
1101 	buf->reserved = asd->frame_status[buf->index];
1102 
1103 	/*
1104 	 * Hack:
1105 	 * Currently frame_status in the enum type which takes no more lower
1106 	 * 8 bit.
1107 	 * use bit[31:16] for exp_id as it is only in the range of 1~255
1108 	 */
1109 	buf->reserved &= 0x0000ffff;
1110 	if (!(buf->flags & V4L2_BUF_FLAG_ERROR))
1111 		buf->reserved |= frame->exp_id;
1112 	buf->reserved2 = pipe->frame_config_id[buf->index];
1113 
1114 	dev_dbg(isp->dev,
1115 		"dqbuf buffer %d (%s) for asd%d with exp_id %d, isp_config_id %d\n",
1116 		buf->index, vdev->name, asd->index, buf->reserved >> 16,
1117 		buf->reserved2);
1118 	return 0;
1119 }
1120 
atomisp_get_css_pipe_id(struct atomisp_sub_device * asd)1121 enum ia_css_pipe_id atomisp_get_css_pipe_id(struct atomisp_sub_device *asd)
1122 {
1123 	if (ATOMISP_USE_YUVPP(asd))
1124 		return IA_CSS_PIPE_ID_YUVPP;
1125 
1126 	if (asd->continuous_mode->val) {
1127 		if (asd->run_mode->val == ATOMISP_RUN_MODE_VIDEO)
1128 			return IA_CSS_PIPE_ID_VIDEO;
1129 		else
1130 			return IA_CSS_PIPE_ID_PREVIEW;
1131 	}
1132 
1133 	/*
1134 	 * Disable vf_pp and run CSS in video mode. This allows using ISP
1135 	 * scaling but it has one frame delay due to CSS internal buffering.
1136 	 */
1137 	if (asd->vfpp->val == ATOMISP_VFPP_DISABLE_SCALER)
1138 		return IA_CSS_PIPE_ID_VIDEO;
1139 
1140 	/*
1141 	 * Disable vf_pp and run CSS in still capture mode. In this mode
1142 	 * CSS does not cause extra latency with buffering, but scaling
1143 	 * is not available.
1144 	 */
1145 	if (asd->vfpp->val == ATOMISP_VFPP_DISABLE_LOWLAT)
1146 		return IA_CSS_PIPE_ID_CAPTURE;
1147 
1148 	switch (asd->run_mode->val) {
1149 	case ATOMISP_RUN_MODE_PREVIEW:
1150 		return IA_CSS_PIPE_ID_PREVIEW;
1151 	case ATOMISP_RUN_MODE_VIDEO:
1152 		return IA_CSS_PIPE_ID_VIDEO;
1153 	case ATOMISP_RUN_MODE_STILL_CAPTURE:
1154 	default:
1155 		return IA_CSS_PIPE_ID_CAPTURE;
1156 	}
1157 }
1158 
atomisp_sensor_start_stream(struct atomisp_sub_device * asd)1159 static unsigned int atomisp_sensor_start_stream(struct atomisp_sub_device *asd)
1160 {
1161 	if (asd->vfpp->val != ATOMISP_VFPP_ENABLE ||
1162 	    asd->copy_mode)
1163 		return 1;
1164 
1165 	if (asd->run_mode->val == ATOMISP_RUN_MODE_VIDEO ||
1166 	    (asd->run_mode->val == ATOMISP_RUN_MODE_STILL_CAPTURE &&
1167 	     !atomisp_is_mbuscode_raw(
1168 		 asd->fmt[
1169 		  asd->capture_pad].fmt.code) &&
1170 	     !asd->continuous_mode->val))
1171 		return 2;
1172 	else
1173 		return 1;
1174 }
1175 
atomisp_stream_on_master_slave_sensor(struct atomisp_device * isp,bool isp_timeout)1176 int atomisp_stream_on_master_slave_sensor(struct atomisp_device *isp,
1177 	bool isp_timeout)
1178 {
1179 	unsigned int master, slave, delay_slave = 0;
1180 	int ret;
1181 
1182 	master = ATOMISP_DEPTH_DEFAULT_MASTER_SENSOR;
1183 	slave = ATOMISP_DEPTH_DEFAULT_SLAVE_SENSOR;
1184 	dev_warn(isp->dev,
1185 		 "depth mode use default master=%s.slave=%s.\n",
1186 		 isp->inputs[master].camera->name,
1187 		 isp->inputs[slave].camera->name);
1188 
1189 	ret = v4l2_subdev_call(isp->inputs[master].camera, core,
1190 			       ioctl, ATOMISP_IOC_G_DEPTH_SYNC_COMP,
1191 			       &delay_slave);
1192 	if (ret)
1193 		dev_warn(isp->dev,
1194 			 "get depth sensor %s compensation delay failed.\n",
1195 			 isp->inputs[master].camera->name);
1196 
1197 	ret = v4l2_subdev_call(isp->inputs[master].camera,
1198 			       video, s_stream, 1);
1199 	if (ret) {
1200 		dev_err(isp->dev, "depth mode master sensor %s stream-on failed.\n",
1201 			isp->inputs[master].camera->name);
1202 		return -EINVAL;
1203 	}
1204 
1205 	if (delay_slave != 0)
1206 		udelay(delay_slave);
1207 
1208 	ret = v4l2_subdev_call(isp->inputs[slave].camera,
1209 			       video, s_stream, 1);
1210 	if (ret) {
1211 		dev_err(isp->dev, "depth mode slave sensor %s stream-on failed.\n",
1212 			isp->inputs[slave].camera->name);
1213 		v4l2_subdev_call(isp->inputs[master].camera, video, s_stream, 0);
1214 
1215 		return -EINVAL;
1216 	}
1217 
1218 	return 0;
1219 }
1220 
1221 /* Input system HW workaround */
1222 /* Input system address translation corrupts burst during */
1223 /* invalidate. SW workaround for this is to set burst length */
1224 /* manually to 128 in case of 13MPx snapshot and to 1 otherwise. */
atomisp_dma_burst_len_cfg(struct atomisp_sub_device * asd)1225 static void atomisp_dma_burst_len_cfg(struct atomisp_sub_device *asd)
1226 {
1227 	struct v4l2_mbus_framefmt *sink;
1228 
1229 	sink = atomisp_subdev_get_ffmt(&asd->subdev, NULL,
1230 				       V4L2_SUBDEV_FORMAT_ACTIVE,
1231 				       ATOMISP_SUBDEV_PAD_SINK);
1232 
1233 	if (sink->width * sink->height >= 4096 * 3072)
1234 		atomisp_css2_hw_store_32(DMA_BURST_SIZE_REG, 0x7F);
1235 	else
1236 		atomisp_css2_hw_store_32(DMA_BURST_SIZE_REG, 0x00);
1237 }
1238 
atomisp_start_streaming(struct vb2_queue * vq,unsigned int count)1239 int atomisp_start_streaming(struct vb2_queue *vq, unsigned int count)
1240 {
1241 	struct atomisp_video_pipe *pipe = vq_to_pipe(vq);
1242 	struct atomisp_sub_device *asd = pipe->asd;
1243 	struct video_device *vdev = &pipe->vdev;
1244 	struct atomisp_device *isp = asd->isp;
1245 	struct pci_dev *pdev = to_pci_dev(isp->dev);
1246 	enum ia_css_pipe_id css_pipe_id;
1247 	unsigned int sensor_start_stream;
1248 	unsigned long irqflags;
1249 	int ret;
1250 
1251 	mutex_lock(&isp->mutex);
1252 
1253 	dev_dbg(isp->dev, "Start stream on pad %d for asd%d\n",
1254 		atomisp_subdev_source_pad(vdev), asd->index);
1255 
1256 	ret = atomisp_pipe_check(pipe, false);
1257 	if (ret)
1258 		goto out_unlock;
1259 
1260 	/* Input system HW workaround */
1261 	atomisp_dma_burst_len_cfg(asd);
1262 
1263 	/*
1264 	 * The number of streaming video nodes is based on which
1265 	 * binary is going to be run.
1266 	 */
1267 	sensor_start_stream = atomisp_sensor_start_stream(asd);
1268 
1269 	/* Reset pending capture request count. */
1270 	asd->pending_capture_request = 0;
1271 
1272 	if (atomisp_subdev_streaming_count(asd) > sensor_start_stream) {
1273 		/* trigger still capture */
1274 		if (asd->continuous_mode->val &&
1275 		    atomisp_subdev_source_pad(vdev)
1276 		    == ATOMISP_SUBDEV_PAD_SOURCE_CAPTURE) {
1277 			if (asd->run_mode->val == ATOMISP_RUN_MODE_VIDEO)
1278 				dev_dbg(isp->dev, "SDV last video raw buffer id: %u\n",
1279 					asd->latest_preview_exp_id);
1280 			else
1281 				dev_dbg(isp->dev, "ZSL last preview raw buffer id: %u\n",
1282 					asd->latest_preview_exp_id);
1283 
1284 			if (asd->delayed_init == ATOMISP_DELAYED_INIT_QUEUED) {
1285 				flush_work(&asd->delayed_init_work);
1286 				mutex_unlock(&isp->mutex);
1287 				ret = wait_for_completion_interruptible(&asd->init_done);
1288 				mutex_lock(&isp->mutex);
1289 				if (ret) {
1290 					ret = -ERESTARTSYS;
1291 					goto out_unlock;
1292 				}
1293 			}
1294 
1295 			/* handle per_frame_setting parameter and buffers */
1296 			atomisp_handle_parameter_and_buffer(pipe);
1297 
1298 			/*
1299 			 * only ZSL/SDV capture request will be here, raise
1300 			 * the ISP freq to the highest possible to minimize
1301 			 * the S2S latency.
1302 			 */
1303 			atomisp_freq_scaling(isp, ATOMISP_DFS_MODE_MAX, false);
1304 			/*
1305 			 * When asd->enable_raw_buffer_lock->val is true,
1306 			 * An extra IOCTL is needed to call
1307 			 * atomisp_css_exp_id_capture and trigger real capture
1308 			 */
1309 			if (!asd->enable_raw_buffer_lock->val) {
1310 				ret = atomisp_css_offline_capture_configure(asd,
1311 					asd->params.offline_parm.num_captures,
1312 					asd->params.offline_parm.skip_frames,
1313 					asd->params.offline_parm.offset);
1314 				if (ret) {
1315 					ret = -EINVAL;
1316 					goto out_unlock;
1317 				}
1318 			}
1319 		}
1320 		atomisp_qbuffers_to_css(asd);
1321 		ret = 0;
1322 		goto out_unlock;
1323 	}
1324 
1325 	if (asd->streaming == ATOMISP_DEVICE_STREAMING_ENABLED) {
1326 		atomisp_qbuffers_to_css(asd);
1327 		goto start_sensor;
1328 	}
1329 
1330 	css_pipe_id = atomisp_get_css_pipe_id(asd);
1331 
1332 	/* Invalidate caches. FIXME: should flush only necessary buffers */
1333 	wbinvd();
1334 
1335 	if (asd->params.css_update_params_needed) {
1336 		atomisp_apply_css_parameters(asd, &asd->params.css_param);
1337 		if (asd->params.css_param.update_flag.dz_config)
1338 			asd->params.config.dz_config = &asd->params.css_param.dz_config;
1339 		atomisp_css_update_isp_params(asd);
1340 		asd->params.css_update_params_needed = false;
1341 		memset(&asd->params.css_param.update_flag, 0,
1342 		       sizeof(struct atomisp_parameters));
1343 	}
1344 	asd->params.dvs_6axis = NULL;
1345 
1346 	ret = atomisp_css_start(asd, css_pipe_id, false);
1347 	if (ret) {
1348 		atomisp_flush_video_pipe(pipe, VB2_BUF_STATE_QUEUED, true);
1349 		goto out_unlock;
1350 	}
1351 
1352 	spin_lock_irqsave(&isp->lock, irqflags);
1353 	asd->streaming = ATOMISP_DEVICE_STREAMING_ENABLED;
1354 	spin_unlock_irqrestore(&isp->lock, irqflags);
1355 	atomic_set(&asd->sof_count, -1);
1356 	atomic_set(&asd->sequence, -1);
1357 	atomic_set(&asd->sequence_temp, -1);
1358 
1359 	asd->params.dis_proj_data_valid = false;
1360 	asd->latest_preview_exp_id = 0;
1361 	asd->postview_exp_id = 1;
1362 	asd->preview_exp_id = 1;
1363 
1364 	/* handle per_frame_setting parameter and buffers */
1365 	atomisp_handle_parameter_and_buffer(pipe);
1366 
1367 	atomisp_qbuffers_to_css(asd);
1368 
1369 	/* Only start sensor when the last streaming instance started */
1370 	if (atomisp_subdev_streaming_count(asd) < sensor_start_stream) {
1371 		ret = 0;
1372 		goto out_unlock;
1373 	}
1374 
1375 start_sensor:
1376 	if (isp->flash) {
1377 		asd->params.num_flash_frames = 0;
1378 		asd->params.flash_state = ATOMISP_FLASH_IDLE;
1379 		atomisp_setup_flash(asd);
1380 	}
1381 
1382 	atomisp_css_irq_enable(isp, IA_CSS_IRQ_INFO_CSS_RECEIVER_SOF,
1383 			       atomisp_css_valid_sof(isp));
1384 	atomisp_csi2_configure(asd);
1385 	/*
1386 	 * set freq to max when streaming count > 1 which indicate
1387 	 * dual camera would run
1388 	 */
1389 	if (atomisp_streaming_count(isp) > 1) {
1390 		if (atomisp_freq_scaling(isp,
1391 					 ATOMISP_DFS_MODE_MAX, false) < 0)
1392 			dev_dbg(isp->dev, "DFS max mode failed!\n");
1393 	} else {
1394 		if (atomisp_freq_scaling(isp,
1395 					 ATOMISP_DFS_MODE_AUTO, false) < 0)
1396 			dev_dbg(isp->dev, "DFS auto mode failed!\n");
1397 	}
1398 
1399 	if (asd->depth_mode->val && atomisp_streaming_count(isp) ==
1400 	    ATOMISP_DEPTH_SENSOR_STREAMON_COUNT) {
1401 		ret = atomisp_stream_on_master_slave_sensor(isp, false);
1402 		if (ret) {
1403 			dev_err(isp->dev, "master slave sensor stream on failed!\n");
1404 			goto out_unlock;
1405 		}
1406 		goto start_delay_wq;
1407 	} else if (asd->depth_mode->val && (atomisp_streaming_count(isp) <
1408 					    ATOMISP_DEPTH_SENSOR_STREAMON_COUNT)) {
1409 		goto start_delay_wq;
1410 	}
1411 
1412 	/* Enable the CSI interface on ANN B0/K0 */
1413 	if (isp->media_dev.hw_revision >= ((ATOMISP_HW_REVISION_ISP2401 <<
1414 					    ATOMISP_HW_REVISION_SHIFT) | ATOMISP_HW_STEPPING_B0)) {
1415 		pci_write_config_word(pdev, MRFLD_PCI_CSI_CONTROL,
1416 				      isp->saved_regs.csi_control | MRFLD_PCI_CSI_CONTROL_CSI_READY);
1417 	}
1418 
1419 	/* stream on the sensor */
1420 	ret = v4l2_subdev_call(isp->inputs[asd->input_curr].camera,
1421 			       video, s_stream, 1);
1422 	if (ret) {
1423 		spin_lock_irqsave(&isp->lock, irqflags);
1424 		asd->streaming = ATOMISP_DEVICE_STREAMING_DISABLED;
1425 		spin_unlock_irqrestore(&isp->lock, irqflags);
1426 		ret = -EINVAL;
1427 		goto out_unlock;
1428 	}
1429 
1430 start_delay_wq:
1431 	if (asd->continuous_mode->val) {
1432 		atomisp_subdev_get_ffmt(&asd->subdev, NULL,
1433 				        V4L2_SUBDEV_FORMAT_ACTIVE,
1434 				        ATOMISP_SUBDEV_PAD_SINK);
1435 
1436 		reinit_completion(&asd->init_done);
1437 		asd->delayed_init = ATOMISP_DELAYED_INIT_QUEUED;
1438 		queue_work(asd->delayed_init_workq, &asd->delayed_init_work);
1439 	} else {
1440 		asd->delayed_init = ATOMISP_DELAYED_INIT_NOT_QUEUED;
1441 	}
1442 
1443 out_unlock:
1444 	mutex_unlock(&isp->mutex);
1445 	return ret;
1446 }
1447 
atomisp_stop_streaming(struct vb2_queue * vq)1448 void atomisp_stop_streaming(struct vb2_queue *vq)
1449 {
1450 	struct atomisp_video_pipe *pipe = vq_to_pipe(vq);
1451 	struct atomisp_sub_device *asd = pipe->asd;
1452 	struct video_device *vdev = &pipe->vdev;
1453 	struct atomisp_device *isp = asd->isp;
1454 	struct pci_dev *pdev = to_pci_dev(isp->dev);
1455 	bool recreate_streams[MAX_STREAM_NUM] = {0};
1456 	enum ia_css_pipe_id css_pipe_id;
1457 	bool first_streamoff = false;
1458 	unsigned long flags;
1459 	int i, ret;
1460 
1461 	mutex_lock(&isp->mutex);
1462 
1463 	dev_dbg(isp->dev, "Stop stream on pad %d for asd%d\n",
1464 		atomisp_subdev_source_pad(vdev), asd->index);
1465 
1466 	/*
1467 	 * There is no guarantee that the buffers queued to / owned by the ISP
1468 	 * will properly be returned to the queue when stopping. Set a flag to
1469 	 * avoid new buffers getting queued and then wait for all the current
1470 	 * buffers to finish.
1471 	 */
1472 	pipe->stopping = true;
1473 	mutex_unlock(&isp->mutex);
1474 	/* wait max 1 second */
1475 	ret = wait_event_timeout(pipe->vb_queue.done_wq,
1476 				 atomisp_buffers_in_css(pipe) == 0, HZ);
1477 	mutex_lock(&isp->mutex);
1478 	pipe->stopping = false;
1479 	if (ret == 0)
1480 		dev_warn(isp->dev, "Warning timeout waiting for CSS to return buffers\n");
1481 
1482 	/*
1483 	 * do only videobuf_streamoff for capture & vf pipes in
1484 	 * case of continuous capture
1485 	 */
1486 	if (asd->continuous_mode->val &&
1487 	    atomisp_subdev_source_pad(vdev) != ATOMISP_SUBDEV_PAD_SOURCE_PREVIEW &&
1488 	    atomisp_subdev_source_pad(vdev) != ATOMISP_SUBDEV_PAD_SOURCE_VIDEO) {
1489 		if (atomisp_subdev_source_pad(vdev) == ATOMISP_SUBDEV_PAD_SOURCE_CAPTURE) {
1490 			/* stop continuous still capture if needed */
1491 			if (asd->params.offline_parm.num_captures == -1)
1492 				atomisp_css_offline_capture_configure(asd,
1493 								      0, 0, 0);
1494 			atomisp_freq_scaling(isp, ATOMISP_DFS_MODE_AUTO, false);
1495 		}
1496 
1497 		goto out_unlock;
1498 	}
1499 
1500 	if (asd->streaming == ATOMISP_DEVICE_STREAMING_ENABLED)
1501 		first_streamoff = true;
1502 
1503 	spin_lock_irqsave(&isp->lock, flags);
1504 	if (atomisp_subdev_streaming_count(asd) == 1)
1505 		asd->streaming = ATOMISP_DEVICE_STREAMING_DISABLED;
1506 	else
1507 		asd->streaming = ATOMISP_DEVICE_STREAMING_STOPPING;
1508 	spin_unlock_irqrestore(&isp->lock, flags);
1509 
1510 	if (!first_streamoff)
1511 		goto stopsensor;
1512 
1513 	atomisp_clear_css_buffer_counters(asd);
1514 	atomisp_css_irq_enable(isp, IA_CSS_IRQ_INFO_CSS_RECEIVER_SOF, false);
1515 
1516 	if (asd->delayed_init == ATOMISP_DELAYED_INIT_QUEUED) {
1517 		cancel_work_sync(&asd->delayed_init_work);
1518 		asd->delayed_init = ATOMISP_DELAYED_INIT_NOT_QUEUED;
1519 	}
1520 
1521 	css_pipe_id = atomisp_get_css_pipe_id(asd);
1522 	atomisp_css_stop(asd, css_pipe_id, false);
1523 
1524 	atomisp_flush_video_pipe(pipe, VB2_BUF_STATE_ERROR, true);
1525 
1526 	atomisp_subdev_cleanup_pending_events(asd);
1527 stopsensor:
1528 	if (atomisp_subdev_streaming_count(asd) != atomisp_sensor_start_stream(asd))
1529 		goto out_unlock;
1530 
1531 	ret = v4l2_subdev_call(isp->inputs[asd->input_curr].camera,
1532 			       video, s_stream, 0);
1533 
1534 	if (isp->flash) {
1535 		asd->params.num_flash_frames = 0;
1536 		asd->params.flash_state = ATOMISP_FLASH_IDLE;
1537 	}
1538 
1539 	/* if other streams are running, isp should not be powered off */
1540 	if (atomisp_streaming_count(isp)) {
1541 		atomisp_css_flush(isp);
1542 		goto out_unlock;
1543 	}
1544 
1545 	/* Disable the CSI interface on ANN B0/K0 */
1546 	if (isp->media_dev.hw_revision >= ((ATOMISP_HW_REVISION_ISP2401 <<
1547 					    ATOMISP_HW_REVISION_SHIFT) | ATOMISP_HW_STEPPING_B0)) {
1548 		pci_write_config_word(pdev, MRFLD_PCI_CSI_CONTROL,
1549 				      isp->saved_regs.csi_control & ~MRFLD_PCI_CSI_CONTROL_CSI_READY);
1550 	}
1551 
1552 	if (atomisp_freq_scaling(isp, ATOMISP_DFS_MODE_LOW, false))
1553 		dev_warn(isp->dev, "DFS failed.\n");
1554 	/*
1555 	 * ISP work around, need to reset isp
1556 	 * Is it correct time to reset ISP when first node does streamoff?
1557 	 */
1558 	if (isp->isp_timeout)
1559 		dev_err(isp->dev, "%s: Resetting with WA activated",
1560 			__func__);
1561 	/*
1562 	 * It is possible that the other asd stream is in the stage
1563 	 * that v4l2_setfmt is just get called on it, which will
1564 	 * create css stream on that stream. But at this point, there
1565 	 * is no way to destroy the css stream created on that stream.
1566 	 *
1567 	 * So force stream destroy here.
1568 	 */
1569 	for (i = 0; i < isp->num_of_streams; i++) {
1570 		if (isp->asd[i].stream_prepared) {
1571 			atomisp_destroy_pipes_stream_force(&isp->asd[i]);
1572 			recreate_streams[i] = true;
1573 		}
1574 	}
1575 
1576 	/* disable  PUNIT/ISP acknowlede/handshake - SRSE=3 */
1577 	pci_write_config_dword(pdev, PCI_I_CONTROL,
1578 			       isp->saved_regs.i_control | MRFLD_PCI_I_CONTROL_SRSE_RESET_MASK);
1579 	dev_err(isp->dev, "atomisp_reset");
1580 	atomisp_reset(isp);
1581 	for (i = 0; i < isp->num_of_streams; i++) {
1582 		if (recreate_streams[i]) {
1583 			int ret2;
1584 
1585 			ret2 = atomisp_create_pipes_stream(&isp->asd[i]);
1586 			if (ret2) {
1587 				dev_err(isp->dev, "%s error re-creating streams: %d\n",
1588 					__func__, ret2);
1589 				if (!ret)
1590 					ret = ret2;
1591 			}
1592 		}
1593 	}
1594 	isp->isp_timeout = false;
1595 out_unlock:
1596 	mutex_unlock(&isp->mutex);
1597 }
1598 
1599 /*
1600  * To get the current value of a control.
1601  * applications initialize the id field of a struct v4l2_control and
1602  * call this ioctl with a pointer to this structure
1603  */
atomisp_g_ctrl(struct file * file,void * fh,struct v4l2_control * control)1604 static int atomisp_g_ctrl(struct file *file, void *fh,
1605 			  struct v4l2_control *control)
1606 {
1607 	struct video_device *vdev = video_devdata(file);
1608 	struct atomisp_sub_device *asd = atomisp_to_video_pipe(vdev)->asd;
1609 	struct atomisp_device *isp = video_get_drvdata(vdev);
1610 	int i, ret = -EINVAL;
1611 
1612 	for (i = 0; i < ctrls_num; i++) {
1613 		if (ci_v4l2_controls[i].id == control->id) {
1614 			ret = 0;
1615 			break;
1616 		}
1617 	}
1618 
1619 	if (ret)
1620 		return ret;
1621 
1622 	switch (control->id) {
1623 	case V4L2_CID_IRIS_ABSOLUTE:
1624 	case V4L2_CID_EXPOSURE_ABSOLUTE:
1625 	case V4L2_CID_2A_STATUS:
1626 	case V4L2_CID_AUTO_N_PRESET_WHITE_BALANCE:
1627 	case V4L2_CID_EXPOSURE:
1628 	case V4L2_CID_EXPOSURE_AUTO:
1629 	case V4L2_CID_SCENE_MODE:
1630 	case V4L2_CID_ISO_SENSITIVITY:
1631 	case V4L2_CID_ISO_SENSITIVITY_AUTO:
1632 	case V4L2_CID_CONTRAST:
1633 	case V4L2_CID_SATURATION:
1634 	case V4L2_CID_SHARPNESS:
1635 	case V4L2_CID_3A_LOCK:
1636 	case V4L2_CID_EXPOSURE_ZONE_NUM:
1637 	case V4L2_CID_TEST_PATTERN:
1638 	case V4L2_CID_TEST_PATTERN_COLOR_R:
1639 	case V4L2_CID_TEST_PATTERN_COLOR_GR:
1640 	case V4L2_CID_TEST_PATTERN_COLOR_GB:
1641 	case V4L2_CID_TEST_PATTERN_COLOR_B:
1642 		return v4l2_g_ctrl(isp->inputs[asd->input_curr].camera->
1643 				   ctrl_handler, control);
1644 	case V4L2_CID_COLORFX:
1645 		ret = atomisp_color_effect(asd, 0, &control->value);
1646 		break;
1647 	case V4L2_CID_ATOMISP_BAD_PIXEL_DETECTION:
1648 		ret = atomisp_bad_pixel(asd, 0, &control->value);
1649 		break;
1650 	case V4L2_CID_ATOMISP_POSTPROCESS_GDC_CAC:
1651 		ret = atomisp_gdc_cac(asd, 0, &control->value);
1652 		break;
1653 	case V4L2_CID_ATOMISP_VIDEO_STABLIZATION:
1654 		ret = atomisp_video_stable(asd, 0, &control->value);
1655 		break;
1656 	case V4L2_CID_ATOMISP_FIXED_PATTERN_NR:
1657 		ret = atomisp_fixed_pattern(asd, 0, &control->value);
1658 		break;
1659 	case V4L2_CID_ATOMISP_FALSE_COLOR_CORRECTION:
1660 		ret = atomisp_false_color(asd, 0, &control->value);
1661 		break;
1662 	case V4L2_CID_ATOMISP_LOW_LIGHT:
1663 		ret = atomisp_low_light(asd, 0, &control->value);
1664 		break;
1665 	default:
1666 		ret = -EINVAL;
1667 		break;
1668 	}
1669 
1670 	return ret;
1671 }
1672 
1673 /*
1674  * To change the value of a control.
1675  * applications initialize the id and value fields of a struct v4l2_control
1676  * and call this ioctl.
1677  */
atomisp_s_ctrl(struct file * file,void * fh,struct v4l2_control * control)1678 static int atomisp_s_ctrl(struct file *file, void *fh,
1679 			  struct v4l2_control *control)
1680 {
1681 	struct video_device *vdev = video_devdata(file);
1682 	struct atomisp_sub_device *asd = atomisp_to_video_pipe(vdev)->asd;
1683 	struct atomisp_device *isp = video_get_drvdata(vdev);
1684 	int i, ret = -EINVAL;
1685 
1686 	for (i = 0; i < ctrls_num; i++) {
1687 		if (ci_v4l2_controls[i].id == control->id) {
1688 			ret = 0;
1689 			break;
1690 		}
1691 	}
1692 
1693 	if (ret)
1694 		return ret;
1695 
1696 	switch (control->id) {
1697 	case V4L2_CID_AUTO_N_PRESET_WHITE_BALANCE:
1698 	case V4L2_CID_EXPOSURE:
1699 	case V4L2_CID_EXPOSURE_AUTO:
1700 	case V4L2_CID_EXPOSURE_AUTO_PRIORITY:
1701 	case V4L2_CID_SCENE_MODE:
1702 	case V4L2_CID_ISO_SENSITIVITY:
1703 	case V4L2_CID_ISO_SENSITIVITY_AUTO:
1704 	case V4L2_CID_POWER_LINE_FREQUENCY:
1705 	case V4L2_CID_EXPOSURE_METERING:
1706 	case V4L2_CID_CONTRAST:
1707 	case V4L2_CID_SATURATION:
1708 	case V4L2_CID_SHARPNESS:
1709 	case V4L2_CID_3A_LOCK:
1710 	case V4L2_CID_COLORFX_CBCR:
1711 	case V4L2_CID_TEST_PATTERN:
1712 	case V4L2_CID_TEST_PATTERN_COLOR_R:
1713 	case V4L2_CID_TEST_PATTERN_COLOR_GR:
1714 	case V4L2_CID_TEST_PATTERN_COLOR_GB:
1715 	case V4L2_CID_TEST_PATTERN_COLOR_B:
1716 		return v4l2_s_ctrl(NULL,
1717 				   isp->inputs[asd->input_curr].camera->
1718 				   ctrl_handler, control);
1719 	case V4L2_CID_COLORFX:
1720 		ret = atomisp_color_effect(asd, 1, &control->value);
1721 		break;
1722 	case V4L2_CID_ATOMISP_BAD_PIXEL_DETECTION:
1723 		ret = atomisp_bad_pixel(asd, 1, &control->value);
1724 		break;
1725 	case V4L2_CID_ATOMISP_POSTPROCESS_GDC_CAC:
1726 		ret = atomisp_gdc_cac(asd, 1, &control->value);
1727 		break;
1728 	case V4L2_CID_ATOMISP_VIDEO_STABLIZATION:
1729 		ret = atomisp_video_stable(asd, 1, &control->value);
1730 		break;
1731 	case V4L2_CID_ATOMISP_FIXED_PATTERN_NR:
1732 		ret = atomisp_fixed_pattern(asd, 1, &control->value);
1733 		break;
1734 	case V4L2_CID_ATOMISP_FALSE_COLOR_CORRECTION:
1735 		ret = atomisp_false_color(asd, 1, &control->value);
1736 		break;
1737 	case V4L2_CID_REQUEST_FLASH:
1738 		ret = atomisp_flash_enable(asd, control->value);
1739 		break;
1740 	case V4L2_CID_ATOMISP_LOW_LIGHT:
1741 		ret = atomisp_low_light(asd, 1, &control->value);
1742 		break;
1743 	default:
1744 		ret = -EINVAL;
1745 		break;
1746 	}
1747 	return ret;
1748 }
1749 
1750 /*
1751  * To query the attributes of a control.
1752  * applications set the id field of a struct v4l2_queryctrl and call the
1753  * this ioctl with a pointer to this structure. The driver fills
1754  * the rest of the structure.
1755  */
atomisp_queryctl(struct file * file,void * fh,struct v4l2_queryctrl * qc)1756 static int atomisp_queryctl(struct file *file, void *fh,
1757 			    struct v4l2_queryctrl *qc)
1758 {
1759 	int i, ret = -EINVAL;
1760 	struct video_device *vdev = video_devdata(file);
1761 	struct atomisp_sub_device *asd = atomisp_to_video_pipe(vdev)->asd;
1762 	struct atomisp_device *isp = video_get_drvdata(vdev);
1763 
1764 	switch (qc->id) {
1765 	case V4L2_CID_FOCUS_ABSOLUTE:
1766 	case V4L2_CID_FOCUS_RELATIVE:
1767 	case V4L2_CID_FOCUS_STATUS:
1768 		if (!IS_ISP2401) {
1769 			return v4l2_queryctrl(isp->inputs[asd->input_curr].camera->
1770 					    ctrl_handler, qc);
1771 		}
1772 		/* ISP2401 */
1773 		if (isp->motor)
1774 			return v4l2_queryctrl(isp->motor->ctrl_handler, qc);
1775 		else
1776 			return v4l2_queryctrl(isp->inputs[asd->input_curr].
1777 					      camera->ctrl_handler, qc);
1778 	}
1779 
1780 	if (qc->id & V4L2_CTRL_FLAG_NEXT_CTRL)
1781 		return ret;
1782 
1783 	for (i = 0; i < ctrls_num; i++) {
1784 		if (ci_v4l2_controls[i].id == qc->id) {
1785 			memcpy(qc, &ci_v4l2_controls[i],
1786 			       sizeof(struct v4l2_queryctrl));
1787 			qc->reserved[0] = 0;
1788 			ret = 0;
1789 			break;
1790 		}
1791 	}
1792 	if (ret != 0)
1793 		qc->flags = V4L2_CTRL_FLAG_DISABLED;
1794 
1795 	return ret;
1796 }
1797 
atomisp_camera_g_ext_ctrls(struct file * file,void * fh,struct v4l2_ext_controls * c)1798 static int atomisp_camera_g_ext_ctrls(struct file *file, void *fh,
1799 				      struct v4l2_ext_controls *c)
1800 {
1801 	struct video_device *vdev = video_devdata(file);
1802 	struct atomisp_sub_device *asd = atomisp_to_video_pipe(vdev)->asd;
1803 	struct atomisp_device *isp = video_get_drvdata(vdev);
1804 	struct v4l2_subdev *motor;
1805 	struct v4l2_control ctrl;
1806 	int i;
1807 	int ret = 0;
1808 
1809 	if (!IS_ISP2401)
1810 		motor = isp->inputs[asd->input_curr].motor;
1811 	else
1812 		motor = isp->motor;
1813 
1814 	for (i = 0; i < c->count; i++) {
1815 		ctrl.id = c->controls[i].id;
1816 		ctrl.value = c->controls[i].value;
1817 		switch (ctrl.id) {
1818 		case V4L2_CID_EXPOSURE_ABSOLUTE:
1819 		case V4L2_CID_EXPOSURE_AUTO:
1820 		case V4L2_CID_IRIS_ABSOLUTE:
1821 		case V4L2_CID_3A_LOCK:
1822 		case V4L2_CID_TEST_PATTERN:
1823 		case V4L2_CID_TEST_PATTERN_COLOR_R:
1824 		case V4L2_CID_TEST_PATTERN_COLOR_GR:
1825 		case V4L2_CID_TEST_PATTERN_COLOR_GB:
1826 		case V4L2_CID_TEST_PATTERN_COLOR_B:
1827 			/*
1828 			 * Exposure related control will be handled by sensor
1829 			 * driver
1830 			 */
1831 			ret =
1832 			    v4l2_g_ctrl(isp->inputs[asd->input_curr].camera->
1833 					ctrl_handler, &ctrl);
1834 			break;
1835 		case V4L2_CID_FOCUS_ABSOLUTE:
1836 		case V4L2_CID_FOCUS_RELATIVE:
1837 		case V4L2_CID_FOCUS_STATUS:
1838 		case V4L2_CID_FOCUS_AUTO:
1839 			if (motor)
1840 				ret = v4l2_g_ctrl(motor->ctrl_handler, &ctrl);
1841 			break;
1842 		case V4L2_CID_FLASH_STATUS:
1843 		case V4L2_CID_FLASH_INTENSITY:
1844 		case V4L2_CID_FLASH_TORCH_INTENSITY:
1845 		case V4L2_CID_FLASH_INDICATOR_INTENSITY:
1846 		case V4L2_CID_FLASH_TIMEOUT:
1847 		case V4L2_CID_FLASH_STROBE:
1848 		case V4L2_CID_FLASH_MODE:
1849 		case V4L2_CID_FLASH_STATUS_REGISTER:
1850 			if (isp->flash)
1851 				ret =
1852 				    v4l2_g_ctrl(isp->flash->ctrl_handler,
1853 						&ctrl);
1854 			break;
1855 		case V4L2_CID_ZOOM_ABSOLUTE:
1856 			ret = atomisp_digital_zoom(asd, 0, &ctrl.value);
1857 			break;
1858 		case V4L2_CID_G_SKIP_FRAMES:
1859 			ret = v4l2_subdev_call(
1860 				  isp->inputs[asd->input_curr].camera,
1861 				  sensor, g_skip_frames, (u32 *)&ctrl.value);
1862 			break;
1863 		default:
1864 			ret = -EINVAL;
1865 		}
1866 
1867 		if (ret) {
1868 			c->error_idx = i;
1869 			break;
1870 		}
1871 		c->controls[i].value = ctrl.value;
1872 	}
1873 	return ret;
1874 }
1875 
1876 /* This ioctl allows the application to get multiple controls by class */
atomisp_g_ext_ctrls(struct file * file,void * fh,struct v4l2_ext_controls * c)1877 static int atomisp_g_ext_ctrls(struct file *file, void *fh,
1878 			       struct v4l2_ext_controls *c)
1879 {
1880 	struct v4l2_control ctrl;
1881 	int i, ret = 0;
1882 
1883 	/*
1884 	 * input_lock is not need for the Camera related IOCTLs
1885 	 * The input_lock downgrade the FPS of 3A
1886 	 */
1887 	ret = atomisp_camera_g_ext_ctrls(file, fh, c);
1888 	if (ret != -EINVAL)
1889 		return ret;
1890 
1891 	for (i = 0; i < c->count; i++) {
1892 		ctrl.id = c->controls[i].id;
1893 		ctrl.value = c->controls[i].value;
1894 		ret = atomisp_g_ctrl(file, fh, &ctrl);
1895 		c->controls[i].value = ctrl.value;
1896 		if (ret) {
1897 			c->error_idx = i;
1898 			break;
1899 		}
1900 	}
1901 	return ret;
1902 }
1903 
atomisp_camera_s_ext_ctrls(struct file * file,void * fh,struct v4l2_ext_controls * c)1904 static int atomisp_camera_s_ext_ctrls(struct file *file, void *fh,
1905 				      struct v4l2_ext_controls *c)
1906 {
1907 	struct video_device *vdev = video_devdata(file);
1908 	struct atomisp_sub_device *asd = atomisp_to_video_pipe(vdev)->asd;
1909 	struct atomisp_device *isp = video_get_drvdata(vdev);
1910 	struct v4l2_subdev *motor;
1911 	struct v4l2_control ctrl;
1912 	int i;
1913 	int ret = 0;
1914 
1915 	if (!IS_ISP2401)
1916 		motor = isp->inputs[asd->input_curr].motor;
1917 	else
1918 		motor = isp->motor;
1919 
1920 	for (i = 0; i < c->count; i++) {
1921 		struct v4l2_ctrl *ctr;
1922 
1923 		ctrl.id = c->controls[i].id;
1924 		ctrl.value = c->controls[i].value;
1925 		switch (ctrl.id) {
1926 		case V4L2_CID_EXPOSURE_ABSOLUTE:
1927 		case V4L2_CID_EXPOSURE_AUTO:
1928 		case V4L2_CID_EXPOSURE_METERING:
1929 		case V4L2_CID_IRIS_ABSOLUTE:
1930 		case V4L2_CID_VCM_TIMING:
1931 		case V4L2_CID_VCM_SLEW:
1932 		case V4L2_CID_3A_LOCK:
1933 		case V4L2_CID_TEST_PATTERN:
1934 		case V4L2_CID_TEST_PATTERN_COLOR_R:
1935 		case V4L2_CID_TEST_PATTERN_COLOR_GR:
1936 		case V4L2_CID_TEST_PATTERN_COLOR_GB:
1937 		case V4L2_CID_TEST_PATTERN_COLOR_B:
1938 			ret = v4l2_s_ctrl(NULL,
1939 					  isp->inputs[asd->input_curr].camera->
1940 					  ctrl_handler, &ctrl);
1941 			break;
1942 		case V4L2_CID_FOCUS_ABSOLUTE:
1943 		case V4L2_CID_FOCUS_RELATIVE:
1944 		case V4L2_CID_FOCUS_STATUS:
1945 		case V4L2_CID_FOCUS_AUTO:
1946 			if (motor)
1947 				ret = v4l2_s_ctrl(NULL, motor->ctrl_handler,
1948 						  &ctrl);
1949 			else
1950 				ret = v4l2_s_ctrl(NULL,
1951 						  isp->inputs[asd->input_curr].
1952 						  camera->ctrl_handler, &ctrl);
1953 			break;
1954 		case V4L2_CID_FLASH_STATUS:
1955 		case V4L2_CID_FLASH_INTENSITY:
1956 		case V4L2_CID_FLASH_TORCH_INTENSITY:
1957 		case V4L2_CID_FLASH_INDICATOR_INTENSITY:
1958 		case V4L2_CID_FLASH_TIMEOUT:
1959 		case V4L2_CID_FLASH_STROBE:
1960 		case V4L2_CID_FLASH_MODE:
1961 		case V4L2_CID_FLASH_STATUS_REGISTER:
1962 			if (isp->flash) {
1963 				ret =
1964 				    v4l2_s_ctrl(NULL, isp->flash->ctrl_handler,
1965 						&ctrl);
1966 				/*
1967 				 * When flash mode is changed we need to reset
1968 				 * flash state
1969 				 */
1970 				if (ctrl.id == V4L2_CID_FLASH_MODE) {
1971 					asd->params.flash_state =
1972 					    ATOMISP_FLASH_IDLE;
1973 					asd->params.num_flash_frames = 0;
1974 				}
1975 			}
1976 			break;
1977 		case V4L2_CID_ZOOM_ABSOLUTE:
1978 			ret = atomisp_digital_zoom(asd, 1, &ctrl.value);
1979 			break;
1980 		default:
1981 			ctr = v4l2_ctrl_find(&asd->ctrl_handler, ctrl.id);
1982 			if (ctr)
1983 				ret = v4l2_ctrl_s_ctrl(ctr, ctrl.value);
1984 			else
1985 				ret = -EINVAL;
1986 		}
1987 
1988 		if (ret) {
1989 			c->error_idx = i;
1990 			break;
1991 		}
1992 		c->controls[i].value = ctrl.value;
1993 	}
1994 	return ret;
1995 }
1996 
1997 /* This ioctl allows the application to set multiple controls by class */
atomisp_s_ext_ctrls(struct file * file,void * fh,struct v4l2_ext_controls * c)1998 static int atomisp_s_ext_ctrls(struct file *file, void *fh,
1999 			       struct v4l2_ext_controls *c)
2000 {
2001 	struct v4l2_control ctrl;
2002 	int i, ret = 0;
2003 
2004 	/*
2005 	 * input_lock is not need for the Camera related IOCTLs
2006 	 * The input_lock downgrade the FPS of 3A
2007 	 */
2008 	ret = atomisp_camera_s_ext_ctrls(file, fh, c);
2009 	if (ret != -EINVAL)
2010 		return ret;
2011 
2012 	for (i = 0; i < c->count; i++) {
2013 		ctrl.id = c->controls[i].id;
2014 		ctrl.value = c->controls[i].value;
2015 		ret = atomisp_s_ctrl(file, fh, &ctrl);
2016 		c->controls[i].value = ctrl.value;
2017 		if (ret) {
2018 			c->error_idx = i;
2019 			break;
2020 		}
2021 	}
2022 	return ret;
2023 }
2024 
2025 /*
2026  * vidioc_g/s_param are used to switch isp running mode
2027  */
atomisp_g_parm(struct file * file,void * fh,struct v4l2_streamparm * parm)2028 static int atomisp_g_parm(struct file *file, void *fh,
2029 			  struct v4l2_streamparm *parm)
2030 {
2031 	struct video_device *vdev = video_devdata(file);
2032 	struct atomisp_sub_device *asd = atomisp_to_video_pipe(vdev)->asd;
2033 	struct atomisp_device *isp = video_get_drvdata(vdev);
2034 
2035 	if (parm->type != V4L2_BUF_TYPE_VIDEO_CAPTURE) {
2036 		dev_err(isp->dev, "unsupported v4l2 buf type\n");
2037 		return -EINVAL;
2038 	}
2039 
2040 	parm->parm.capture.capturemode = asd->run_mode->val;
2041 
2042 	return 0;
2043 }
2044 
atomisp_s_parm(struct file * file,void * fh,struct v4l2_streamparm * parm)2045 static int atomisp_s_parm(struct file *file, void *fh,
2046 			  struct v4l2_streamparm *parm)
2047 {
2048 	struct video_device *vdev = video_devdata(file);
2049 	struct atomisp_device *isp = video_get_drvdata(vdev);
2050 	struct atomisp_sub_device *asd = atomisp_to_video_pipe(vdev)->asd;
2051 	int mode;
2052 	int rval;
2053 	int fps;
2054 
2055 	if (parm->type != V4L2_BUF_TYPE_VIDEO_CAPTURE) {
2056 		dev_err(isp->dev, "unsupported v4l2 buf type\n");
2057 		return -EINVAL;
2058 	}
2059 
2060 	asd->high_speed_mode = false;
2061 	switch (parm->parm.capture.capturemode) {
2062 	case CI_MODE_NONE: {
2063 		struct v4l2_subdev_frame_interval fi = {0};
2064 
2065 		fi.interval = parm->parm.capture.timeperframe;
2066 
2067 		rval = v4l2_subdev_call(isp->inputs[asd->input_curr].camera,
2068 					video, s_frame_interval, &fi);
2069 		if (!rval)
2070 			parm->parm.capture.timeperframe = fi.interval;
2071 
2072 		if (fi.interval.numerator != 0) {
2073 			fps = fi.interval.denominator / fi.interval.numerator;
2074 			if (fps > 30)
2075 				asd->high_speed_mode = true;
2076 		}
2077 
2078 		return rval == -ENOIOCTLCMD ? 0 : rval;
2079 	}
2080 	case CI_MODE_VIDEO:
2081 		mode = ATOMISP_RUN_MODE_VIDEO;
2082 		break;
2083 	case CI_MODE_STILL_CAPTURE:
2084 		mode = ATOMISP_RUN_MODE_STILL_CAPTURE;
2085 		break;
2086 	case CI_MODE_CONTINUOUS:
2087 		mode = ATOMISP_RUN_MODE_CONTINUOUS_CAPTURE;
2088 		break;
2089 	case CI_MODE_PREVIEW:
2090 		mode = ATOMISP_RUN_MODE_PREVIEW;
2091 		break;
2092 	default:
2093 		return -EINVAL;
2094 	}
2095 
2096 	rval = v4l2_ctrl_s_ctrl(asd->run_mode, mode);
2097 
2098 	return rval == -ENOIOCTLCMD ? 0 : rval;
2099 }
2100 
atomisp_vidioc_default(struct file * file,void * fh,bool valid_prio,unsigned int cmd,void * arg)2101 static long atomisp_vidioc_default(struct file *file, void *fh,
2102 				   bool valid_prio, unsigned int cmd, void *arg)
2103 {
2104 	struct video_device *vdev = video_devdata(file);
2105 	struct atomisp_device *isp = video_get_drvdata(vdev);
2106 	struct atomisp_sub_device *asd = atomisp_to_video_pipe(vdev)->asd;
2107 	struct v4l2_subdev *motor;
2108 	int err;
2109 
2110 	if (!IS_ISP2401)
2111 		motor = isp->inputs[asd->input_curr].motor;
2112 	else
2113 		motor = isp->motor;
2114 
2115 	switch (cmd) {
2116 	case ATOMISP_IOC_S_SENSOR_RUNMODE:
2117 		if (IS_ISP2401)
2118 			err = atomisp_set_sensor_runmode(asd, arg);
2119 		else
2120 			err = -EINVAL;
2121 		break;
2122 
2123 	case ATOMISP_IOC_G_XNR:
2124 		err = atomisp_xnr(asd, 0, arg);
2125 		break;
2126 
2127 	case ATOMISP_IOC_S_XNR:
2128 		err = atomisp_xnr(asd, 1, arg);
2129 		break;
2130 
2131 	case ATOMISP_IOC_G_NR:
2132 		err = atomisp_nr(asd, 0, arg);
2133 		break;
2134 
2135 	case ATOMISP_IOC_S_NR:
2136 		err = atomisp_nr(asd, 1, arg);
2137 		break;
2138 
2139 	case ATOMISP_IOC_G_TNR:
2140 		err = atomisp_tnr(asd, 0, arg);
2141 		break;
2142 
2143 	case ATOMISP_IOC_S_TNR:
2144 		err = atomisp_tnr(asd, 1, arg);
2145 		break;
2146 
2147 	case ATOMISP_IOC_G_BLACK_LEVEL_COMP:
2148 		err = atomisp_black_level(asd, 0, arg);
2149 		break;
2150 
2151 	case ATOMISP_IOC_S_BLACK_LEVEL_COMP:
2152 		err = atomisp_black_level(asd, 1, arg);
2153 		break;
2154 
2155 	case ATOMISP_IOC_G_EE:
2156 		err = atomisp_ee(asd, 0, arg);
2157 		break;
2158 
2159 	case ATOMISP_IOC_S_EE:
2160 		err = atomisp_ee(asd, 1, arg);
2161 		break;
2162 
2163 	case ATOMISP_IOC_G_DIS_STAT:
2164 		err = atomisp_get_dis_stat(asd, arg);
2165 		break;
2166 
2167 	case ATOMISP_IOC_G_DVS2_BQ_RESOLUTIONS:
2168 		err = atomisp_get_dvs2_bq_resolutions(asd, arg);
2169 		break;
2170 
2171 	case ATOMISP_IOC_S_DIS_COEFS:
2172 		err = atomisp_css_cp_dvs2_coefs(asd, arg,
2173 						&asd->params.css_param, true);
2174 		if (!err && arg)
2175 			asd->params.css_update_params_needed = true;
2176 		break;
2177 
2178 	case ATOMISP_IOC_S_DIS_VECTOR:
2179 		err = atomisp_cp_dvs_6axis_config(asd, arg,
2180 						  &asd->params.css_param, true);
2181 		if (!err && arg)
2182 			asd->params.css_update_params_needed = true;
2183 		break;
2184 
2185 	case ATOMISP_IOC_G_ISP_PARM:
2186 		err = atomisp_param(asd, 0, arg);
2187 		break;
2188 
2189 	case ATOMISP_IOC_S_ISP_PARM:
2190 		err = atomisp_param(asd, 1, arg);
2191 		break;
2192 
2193 	case ATOMISP_IOC_G_3A_STAT:
2194 		err = atomisp_3a_stat(asd, 0, arg);
2195 		break;
2196 
2197 	case ATOMISP_IOC_G_ISP_GAMMA:
2198 		err = atomisp_gamma(asd, 0, arg);
2199 		break;
2200 
2201 	case ATOMISP_IOC_S_ISP_GAMMA:
2202 		err = atomisp_gamma(asd, 1, arg);
2203 		break;
2204 
2205 	case ATOMISP_IOC_G_ISP_GDC_TAB:
2206 		err = atomisp_gdc_cac_table(asd, 0, arg);
2207 		break;
2208 
2209 	case ATOMISP_IOC_S_ISP_GDC_TAB:
2210 		err = atomisp_gdc_cac_table(asd, 1, arg);
2211 		break;
2212 
2213 	case ATOMISP_IOC_G_ISP_MACC:
2214 		err = atomisp_macc_table(asd, 0, arg);
2215 		break;
2216 
2217 	case ATOMISP_IOC_S_ISP_MACC:
2218 		err = atomisp_macc_table(asd, 1, arg);
2219 		break;
2220 
2221 	case ATOMISP_IOC_G_ISP_BAD_PIXEL_DETECTION:
2222 		err = atomisp_bad_pixel_param(asd, 0, arg);
2223 		break;
2224 
2225 	case ATOMISP_IOC_S_ISP_BAD_PIXEL_DETECTION:
2226 		err = atomisp_bad_pixel_param(asd, 1, arg);
2227 		break;
2228 
2229 	case ATOMISP_IOC_G_ISP_FALSE_COLOR_CORRECTION:
2230 		err = atomisp_false_color_param(asd, 0, arg);
2231 		break;
2232 
2233 	case ATOMISP_IOC_S_ISP_FALSE_COLOR_CORRECTION:
2234 		err = atomisp_false_color_param(asd, 1, arg);
2235 		break;
2236 
2237 	case ATOMISP_IOC_G_ISP_CTC:
2238 		err = atomisp_ctc(asd, 0, arg);
2239 		break;
2240 
2241 	case ATOMISP_IOC_S_ISP_CTC:
2242 		err = atomisp_ctc(asd, 1, arg);
2243 		break;
2244 
2245 	case ATOMISP_IOC_G_ISP_WHITE_BALANCE:
2246 		err = atomisp_white_balance_param(asd, 0, arg);
2247 		break;
2248 
2249 	case ATOMISP_IOC_S_ISP_WHITE_BALANCE:
2250 		err = atomisp_white_balance_param(asd, 1, arg);
2251 		break;
2252 
2253 	case ATOMISP_IOC_G_3A_CONFIG:
2254 		err = atomisp_3a_config_param(asd, 0, arg);
2255 		break;
2256 
2257 	case ATOMISP_IOC_S_3A_CONFIG:
2258 		err = atomisp_3a_config_param(asd, 1, arg);
2259 		break;
2260 
2261 	case ATOMISP_IOC_S_ISP_FPN_TABLE:
2262 		err = atomisp_fixed_pattern_table(asd, arg);
2263 		break;
2264 
2265 	case ATOMISP_IOC_G_MOTOR_PRIV_INT_DATA:
2266 		if (motor)
2267 			err = v4l2_subdev_call(motor, core, ioctl, cmd, arg);
2268 		else
2269 			err = v4l2_subdev_call(isp->inputs[asd->input_curr].camera,
2270 					       core, ioctl, cmd, arg);
2271 		break;
2272 
2273 	case ATOMISP_IOC_S_EXPOSURE:
2274 	case ATOMISP_IOC_G_SENSOR_CALIBRATION_GROUP:
2275 	case ATOMISP_IOC_G_SENSOR_PRIV_INT_DATA:
2276 	case ATOMISP_IOC_G_SENSOR_AE_BRACKETING_INFO:
2277 	case ATOMISP_IOC_S_SENSOR_AE_BRACKETING_MODE:
2278 	case ATOMISP_IOC_G_SENSOR_AE_BRACKETING_MODE:
2279 	case ATOMISP_IOC_S_SENSOR_AE_BRACKETING_LUT:
2280 		err = v4l2_subdev_call(isp->inputs[asd->input_curr].camera,
2281 				       core, ioctl, cmd, arg);
2282 		break;
2283 	case ATOMISP_IOC_G_UPDATE_EXPOSURE:
2284 		if (IS_ISP2401)
2285 			err = v4l2_subdev_call(isp->inputs[asd->input_curr].camera,
2286 					       core, ioctl, cmd, arg);
2287 		else
2288 			err = -EINVAL;
2289 		break;
2290 
2291 	case ATOMISP_IOC_S_ISP_SHD_TAB:
2292 		err = atomisp_set_shading_table(asd, arg);
2293 		break;
2294 
2295 	case ATOMISP_IOC_G_ISP_GAMMA_CORRECTION:
2296 		err = atomisp_gamma_correction(asd, 0, arg);
2297 		break;
2298 
2299 	case ATOMISP_IOC_S_ISP_GAMMA_CORRECTION:
2300 		err = atomisp_gamma_correction(asd, 1, arg);
2301 		break;
2302 
2303 	case ATOMISP_IOC_S_PARAMETERS:
2304 		err = atomisp_set_parameters(vdev, arg);
2305 		break;
2306 
2307 	case ATOMISP_IOC_S_CONT_CAPTURE_CONFIG:
2308 		err = atomisp_offline_capture_configure(asd, arg);
2309 		break;
2310 	case ATOMISP_IOC_G_METADATA:
2311 		err = atomisp_get_metadata(asd, 0, arg);
2312 		break;
2313 	case ATOMISP_IOC_G_METADATA_BY_TYPE:
2314 		err = atomisp_get_metadata_by_type(asd, 0, arg);
2315 		break;
2316 	case ATOMISP_IOC_EXT_ISP_CTRL:
2317 		err = v4l2_subdev_call(isp->inputs[asd->input_curr].camera,
2318 				       core, ioctl, cmd, arg);
2319 		break;
2320 	case ATOMISP_IOC_EXP_ID_UNLOCK:
2321 		err = atomisp_exp_id_unlock(asd, arg);
2322 		break;
2323 	case ATOMISP_IOC_EXP_ID_CAPTURE:
2324 		err = atomisp_exp_id_capture(asd, arg);
2325 		break;
2326 	case ATOMISP_IOC_S_ENABLE_DZ_CAPT_PIPE:
2327 		err = atomisp_enable_dz_capt_pipe(asd, arg);
2328 		break;
2329 	case ATOMISP_IOC_G_FORMATS_CONFIG:
2330 		err = atomisp_formats(asd, 0, arg);
2331 		break;
2332 
2333 	case ATOMISP_IOC_S_FORMATS_CONFIG:
2334 		err = atomisp_formats(asd, 1, arg);
2335 		break;
2336 	case ATOMISP_IOC_S_EXPOSURE_WINDOW:
2337 		err = atomisp_s_ae_window(asd, arg);
2338 		break;
2339 	case ATOMISP_IOC_INJECT_A_FAKE_EVENT:
2340 		err = atomisp_inject_a_fake_event(asd, arg);
2341 		break;
2342 	case ATOMISP_IOC_G_INVALID_FRAME_NUM:
2343 		err = atomisp_get_invalid_frame_num(vdev, arg);
2344 		break;
2345 	case ATOMISP_IOC_S_ARRAY_RESOLUTION:
2346 		err = atomisp_set_array_res(asd, arg);
2347 		break;
2348 	default:
2349 		err = -EINVAL;
2350 		break;
2351 	}
2352 
2353 	return err;
2354 }
2355 
2356 const struct v4l2_ioctl_ops atomisp_ioctl_ops = {
2357 	.vidioc_querycap = atomisp_querycap,
2358 	.vidioc_enum_input = atomisp_enum_input,
2359 	.vidioc_g_input = atomisp_g_input,
2360 	.vidioc_s_input = atomisp_s_input,
2361 	.vidioc_queryctrl = atomisp_queryctl,
2362 	.vidioc_s_ctrl = atomisp_s_ctrl,
2363 	.vidioc_g_ctrl = atomisp_g_ctrl,
2364 	.vidioc_s_ext_ctrls = atomisp_s_ext_ctrls,
2365 	.vidioc_g_ext_ctrls = atomisp_g_ext_ctrls,
2366 	.vidioc_enum_framesizes   = atomisp_enum_framesizes,
2367 	.vidioc_enum_frameintervals = atomisp_enum_frameintervals,
2368 	.vidioc_enum_fmt_vid_cap = atomisp_enum_fmt_cap,
2369 	.vidioc_try_fmt_vid_cap = atomisp_try_fmt_cap,
2370 	.vidioc_g_fmt_vid_cap = atomisp_g_fmt_cap,
2371 	.vidioc_s_fmt_vid_cap = atomisp_s_fmt_cap,
2372 	.vidioc_reqbufs = vb2_ioctl_reqbufs,
2373 	.vidioc_querybuf = vb2_ioctl_querybuf,
2374 	.vidioc_qbuf = atomisp_qbuf_wrapper,
2375 	.vidioc_dqbuf = atomisp_dqbuf_wrapper,
2376 	.vidioc_streamon = vb2_ioctl_streamon,
2377 	.vidioc_streamoff = vb2_ioctl_streamoff,
2378 	.vidioc_default = atomisp_vidioc_default,
2379 	.vidioc_s_parm = atomisp_s_parm,
2380 	.vidioc_g_parm = atomisp_g_parm,
2381 };
2382