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