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