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