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 
10 #include <linux/delay.h>
11 #include <linux/pci.h>
12 
13 #include <media/v4l2-ioctl.h>
14 #include <media/v4l2-event.h>
15 
16 #include "atomisp_cmd.h"
17 #include "atomisp_common.h"
18 #include "atomisp_fops.h"
19 #include "atomisp_internal.h"
20 #include "atomisp_ioctl.h"
21 #include "atomisp-regs.h"
22 #include "atomisp_compat.h"
23 
24 #include "sh_css_hrt.h"
25 
26 #include "gp_device.h"
27 #include "device_access.h"
28 #include "irq.h"
29 
30 static const char *DRIVER = "atomisp";	/* max size 15 */
31 static const char *CARD = "ATOM ISP";	/* max size 31 */
32 
33 /*
34  * FIXME: ISP should not know beforehand all CIDs supported by sensor.
35  * Instead, it needs to propagate to sensor unknown CIDs.
36  */
37 static struct v4l2_queryctrl ci_v4l2_controls[] = {
38 	{
39 		.id = V4L2_CID_AUTO_WHITE_BALANCE,
40 		.type = V4L2_CTRL_TYPE_BOOLEAN,
41 		.name = "Automatic White Balance",
42 		.minimum = 0,
43 		.maximum = 1,
44 		.step = 1,
45 		.default_value = 0,
46 	},
47 	{
48 		.id = V4L2_CID_RED_BALANCE,
49 		.type = V4L2_CTRL_TYPE_INTEGER,
50 		.name = "Red Balance",
51 		.minimum = 0x00,
52 		.maximum = 0xff,
53 		.step = 1,
54 		.default_value = 0x00,
55 	},
56 	{
57 		.id = V4L2_CID_BLUE_BALANCE,
58 		.type = V4L2_CTRL_TYPE_INTEGER,
59 		.name = "Blue Balance",
60 		.minimum = 0x00,
61 		.maximum = 0xff,
62 		.step = 1,
63 		.default_value = 0x00,
64 	},
65 	{
66 		.id = V4L2_CID_GAMMA,
67 		.type = V4L2_CTRL_TYPE_INTEGER,
68 		.name = "Gamma",
69 		.minimum = 0x00,
70 		.maximum = 0xff,
71 		.step = 1,
72 		.default_value = 0x00,
73 	},
74 	{
75 		.id = V4L2_CID_COLORFX,
76 		.type = V4L2_CTRL_TYPE_INTEGER,
77 		.name = "Image Color Effect",
78 		.minimum = 0,
79 		.maximum = 9,
80 		.step = 1,
81 		.default_value = 0,
82 	},
83 	{
84 		.id = V4L2_CID_ATOMISP_BAD_PIXEL_DETECTION,
85 		.type = V4L2_CTRL_TYPE_INTEGER,
86 		.name = "Bad Pixel Correction",
87 		.minimum = 0,
88 		.maximum = 1,
89 		.step = 1,
90 		.default_value = 0,
91 	},
92 	{
93 		.id = V4L2_CID_ATOMISP_POSTPROCESS_GDC_CAC,
94 		.type = V4L2_CTRL_TYPE_INTEGER,
95 		.name = "GDC/CAC",
96 		.minimum = 0,
97 		.maximum = 1,
98 		.step = 1,
99 		.default_value = 0,
100 	},
101 	{
102 		.id = V4L2_CID_ATOMISP_VIDEO_STABLIZATION,
103 		.type = V4L2_CTRL_TYPE_INTEGER,
104 		.name = "Video Stabilization",
105 		.minimum = 0,
106 		.maximum = 1,
107 		.step = 1,
108 		.default_value = 0,
109 	},
110 	{
111 		.id = V4L2_CID_ATOMISP_FIXED_PATTERN_NR,
112 		.type = V4L2_CTRL_TYPE_INTEGER,
113 		.name = "Fixed Pattern Noise Reduction",
114 		.minimum = 0,
115 		.maximum = 1,
116 		.step = 1,
117 		.default_value = 0,
118 	},
119 	{
120 		.id = V4L2_CID_ATOMISP_FALSE_COLOR_CORRECTION,
121 		.type = V4L2_CTRL_TYPE_INTEGER,
122 		.name = "False Color Correction",
123 		.minimum = 0,
124 		.maximum = 1,
125 		.step = 1,
126 		.default_value = 0,
127 	},
128 	{
129 		.id = V4L2_CID_ATOMISP_LOW_LIGHT,
130 		.type = V4L2_CTRL_TYPE_BOOLEAN,
131 		.name = "Low light mode",
132 		.minimum = 0,
133 		.maximum = 1,
134 		.step = 1,
135 		.default_value = 1,
136 	},
137 };
138 
139 static const u32 ctrls_num = ARRAY_SIZE(ci_v4l2_controls);
140 
141 /*
142  * supported V4L2 fmts and resolutions
143  */
144 const struct atomisp_format_bridge atomisp_output_fmts[] = {
145 	{
146 		.pixelformat = V4L2_PIX_FMT_YUV420,
147 		.depth = 12,
148 		.mbus_code = V4L2_MBUS_FMT_CUSTOM_YUV420,
149 		.sh_fmt = IA_CSS_FRAME_FORMAT_YUV420,
150 		.description = "YUV420, planar",
151 		.planar = true
152 	}, {
153 		.pixelformat = V4L2_PIX_FMT_YVU420,
154 		.depth = 12,
155 		.mbus_code = V4L2_MBUS_FMT_CUSTOM_YVU420,
156 		.sh_fmt = IA_CSS_FRAME_FORMAT_YV12,
157 		.description = "YVU420, planar",
158 		.planar = true
159 	}, {
160 		.pixelformat = V4L2_PIX_FMT_YUV422P,
161 		.depth = 16,
162 		.mbus_code = V4L2_MBUS_FMT_CUSTOM_YUV422P,
163 		.sh_fmt = IA_CSS_FRAME_FORMAT_YUV422,
164 		.description = "YUV422, planar",
165 		.planar = true
166 	}, {
167 		.pixelformat = V4L2_PIX_FMT_YUV444,
168 		.depth = 24,
169 		.mbus_code = V4L2_MBUS_FMT_CUSTOM_YUV444,
170 		.sh_fmt = IA_CSS_FRAME_FORMAT_YUV444,
171 		.description = "YUV444"
172 	}, {
173 		.pixelformat = V4L2_PIX_FMT_NV12,
174 		.depth = 12,
175 		.mbus_code = V4L2_MBUS_FMT_CUSTOM_NV12,
176 		.sh_fmt = IA_CSS_FRAME_FORMAT_NV12,
177 		.description = "NV12, Y-plane, CbCr interleaved",
178 		.planar = true
179 	}, {
180 		.pixelformat = V4L2_PIX_FMT_NV21,
181 		.depth = 12,
182 		.mbus_code = V4L2_MBUS_FMT_CUSTOM_NV21,
183 		.sh_fmt = IA_CSS_FRAME_FORMAT_NV21,
184 		.description = "NV21, Y-plane, CbCr interleaved",
185 		.planar = true
186 	}, {
187 		.pixelformat = V4L2_PIX_FMT_NV16,
188 		.depth = 16,
189 		.mbus_code = V4L2_MBUS_FMT_CUSTOM_NV16,
190 		.sh_fmt = IA_CSS_FRAME_FORMAT_NV16,
191 		.description = "NV16, Y-plane, CbCr interleaved",
192 		.planar = true
193 	}, {
194 		.pixelformat = V4L2_PIX_FMT_YUYV,
195 		.depth = 16,
196 		.mbus_code = V4L2_MBUS_FMT_CUSTOM_YUYV,
197 		.sh_fmt = IA_CSS_FRAME_FORMAT_YUYV,
198 		.description = "YUYV, interleaved"
199 	}, {
200 		.pixelformat = V4L2_PIX_FMT_UYVY,
201 		.depth = 16,
202 		.mbus_code = MEDIA_BUS_FMT_UYVY8_1X16,
203 		.sh_fmt = IA_CSS_FRAME_FORMAT_UYVY,
204 		.description = "UYVY, interleaved"
205 	}, {
206 		.pixelformat = V4L2_PIX_FMT_SBGGR16,
207 		.depth = 16,
208 		.mbus_code = V4L2_MBUS_FMT_CUSTOM_SBGGR16,
209 		.sh_fmt = IA_CSS_FRAME_FORMAT_RAW,
210 		.description = "Bayer 16"
211 	}, {
212 		.pixelformat = V4L2_PIX_FMT_SBGGR8,
213 		.depth = 8,
214 		.mbus_code = MEDIA_BUS_FMT_SBGGR8_1X8,
215 		.sh_fmt = IA_CSS_FRAME_FORMAT_RAW,
216 		.description = "Bayer 8"
217 	}, {
218 		.pixelformat = V4L2_PIX_FMT_SGBRG8,
219 		.depth = 8,
220 		.mbus_code = MEDIA_BUS_FMT_SGBRG8_1X8,
221 		.sh_fmt = IA_CSS_FRAME_FORMAT_RAW,
222 		.description = "Bayer 8"
223 	}, {
224 		.pixelformat = V4L2_PIX_FMT_SGRBG8,
225 		.depth = 8,
226 		.mbus_code = MEDIA_BUS_FMT_SGRBG8_1X8,
227 		.sh_fmt = IA_CSS_FRAME_FORMAT_RAW,
228 		.description = "Bayer 8"
229 	}, {
230 		.pixelformat = V4L2_PIX_FMT_SRGGB8,
231 		.depth = 8,
232 		.mbus_code = MEDIA_BUS_FMT_SRGGB8_1X8,
233 		.sh_fmt = IA_CSS_FRAME_FORMAT_RAW,
234 		.description = "Bayer 8"
235 	}, {
236 		.pixelformat = V4L2_PIX_FMT_SBGGR10,
237 		.depth = 16,
238 		.mbus_code = MEDIA_BUS_FMT_SBGGR10_1X10,
239 		.sh_fmt = IA_CSS_FRAME_FORMAT_RAW,
240 		.description = "Bayer 10"
241 	}, {
242 		.pixelformat = V4L2_PIX_FMT_SGBRG10,
243 		.depth = 16,
244 		.mbus_code = MEDIA_BUS_FMT_SGBRG10_1X10,
245 		.sh_fmt = IA_CSS_FRAME_FORMAT_RAW,
246 		.description = "Bayer 10"
247 	}, {
248 		.pixelformat = V4L2_PIX_FMT_SGRBG10,
249 		.depth = 16,
250 		.mbus_code = MEDIA_BUS_FMT_SGRBG10_1X10,
251 		.sh_fmt = IA_CSS_FRAME_FORMAT_RAW,
252 		.description = "Bayer 10"
253 	}, {
254 		.pixelformat = V4L2_PIX_FMT_SRGGB10,
255 		.depth = 16,
256 		.mbus_code = MEDIA_BUS_FMT_SRGGB10_1X10,
257 		.sh_fmt = IA_CSS_FRAME_FORMAT_RAW,
258 		.description = "Bayer 10"
259 	}, {
260 		.pixelformat = V4L2_PIX_FMT_SBGGR12,
261 		.depth = 16,
262 		.mbus_code = MEDIA_BUS_FMT_SBGGR12_1X12,
263 		.sh_fmt = IA_CSS_FRAME_FORMAT_RAW,
264 		.description = "Bayer 12"
265 	}, {
266 		.pixelformat = V4L2_PIX_FMT_SGBRG12,
267 		.depth = 16,
268 		.mbus_code = MEDIA_BUS_FMT_SGBRG12_1X12,
269 		.sh_fmt = IA_CSS_FRAME_FORMAT_RAW,
270 		.description = "Bayer 12"
271 	}, {
272 		.pixelformat = V4L2_PIX_FMT_SGRBG12,
273 		.depth = 16,
274 		.mbus_code = MEDIA_BUS_FMT_SGRBG12_1X12,
275 		.sh_fmt = IA_CSS_FRAME_FORMAT_RAW,
276 		.description = "Bayer 12"
277 	}, {
278 		.pixelformat = V4L2_PIX_FMT_SRGGB12,
279 		.depth = 16,
280 		.mbus_code = MEDIA_BUS_FMT_SRGGB12_1X12,
281 		.sh_fmt = IA_CSS_FRAME_FORMAT_RAW,
282 		.description = "Bayer 12"
283 	}, {
284 		.pixelformat = V4L2_PIX_FMT_RGB565,
285 		.depth = 16,
286 		.mbus_code = MEDIA_BUS_FMT_BGR565_2X8_LE,
287 		.sh_fmt = IA_CSS_FRAME_FORMAT_RGB565,
288 		.description = "16 RGB 5-6-5"
289 #if 0
290 	}, {
291 		/*
292 		 * Broken, showing vertical columns with random data.
293 		 * For each 128 pixels in a row the last 28 (32?) or so pixels
294 		 * contain random data.
295 		 */
296 		.pixelformat = V4L2_PIX_FMT_RGBX32,
297 		.depth = 32,
298 		.mbus_code = V4L2_MBUS_FMT_CUSTOM_RGB32,
299 		.sh_fmt = IA_CSS_FRAME_FORMAT_RGBA888,
300 		.description = "32 RGB 8-8-8-8"
301 	}, {
302 		.pixelformat = V4L2_PIX_FMT_JPEG,
303 		.depth = 8,
304 		.mbus_code = MEDIA_BUS_FMT_JPEG_1X8,
305 		.sh_fmt = IA_CSS_FRAME_FORMAT_BINARY_8,
306 		.description = "JPEG"
307 	}, {
308 		/* This is a custom format being used by M10MO to send the RAW data */
309 		.pixelformat = V4L2_PIX_FMT_CUSTOM_M10MO_RAW,
310 		.depth = 8,
311 		.mbus_code = V4L2_MBUS_FMT_CUSTOM_M10MO_RAW,
312 		.sh_fmt = IA_CSS_FRAME_FORMAT_BINARY_8,
313 		.description = "Custom RAW for M10MO"
314 #endif
315 	},
316 };
317 
318 const struct atomisp_format_bridge *
atomisp_get_format_bridge(unsigned int pixelformat)319 atomisp_get_format_bridge(unsigned int pixelformat)
320 {
321 	unsigned int i;
322 
323 	for (i = 0; i < ARRAY_SIZE(atomisp_output_fmts); i++) {
324 		if (atomisp_output_fmts[i].pixelformat == pixelformat)
325 			return &atomisp_output_fmts[i];
326 	}
327 
328 	return NULL;
329 }
330 
331 const struct atomisp_format_bridge *
atomisp_get_format_bridge_from_mbus(u32 mbus_code)332 atomisp_get_format_bridge_from_mbus(u32 mbus_code)
333 {
334 	unsigned int i;
335 
336 	for (i = 0; i < ARRAY_SIZE(atomisp_output_fmts); i++) {
337 		if (mbus_code == atomisp_output_fmts[i].mbus_code)
338 			return &atomisp_output_fmts[i];
339 	}
340 
341 	return NULL;
342 }
343 
atomisp_pipe_check(struct atomisp_video_pipe * pipe,bool settings_change)344 int atomisp_pipe_check(struct atomisp_video_pipe *pipe, bool settings_change)
345 {
346 	lockdep_assert_held(&pipe->isp->mutex);
347 
348 	if (pipe->isp->isp_fatal_error)
349 		return -EIO;
350 
351 	if (settings_change && vb2_is_busy(&pipe->vb_queue)) {
352 		dev_err(pipe->isp->dev, "Set fmt/input IOCTL while streaming\n");
353 		return -EBUSY;
354 	}
355 
356 	return 0;
357 }
358 
359 /*
360  * v4l2 ioctls
361  * return ISP capabilities
362  */
atomisp_querycap(struct file * file,void * fh,struct v4l2_capability * cap)363 static int atomisp_querycap(struct file *file, void *fh,
364 			    struct v4l2_capability *cap)
365 {
366 	struct video_device *vdev = video_devdata(file);
367 	struct atomisp_device *isp = video_get_drvdata(vdev);
368 
369 	strscpy(cap->driver, DRIVER, sizeof(cap->driver));
370 	strscpy(cap->card, CARD, sizeof(cap->card));
371 	snprintf(cap->bus_info, sizeof(cap->bus_info), "PCI:%s", dev_name(isp->dev));
372 
373 	return 0;
374 }
375 
376 /*
377  * enum input are used to check primary/secondary camera
378  */
atomisp_enum_input(struct file * file,void * fh,struct v4l2_input * input)379 static int atomisp_enum_input(struct file *file, void *fh,
380 			      struct v4l2_input *input)
381 {
382 	struct video_device *vdev = video_devdata(file);
383 	struct atomisp_device *isp = video_get_drvdata(vdev);
384 	int index = input->index;
385 
386 	if (index >= isp->input_cnt)
387 		return -EINVAL;
388 
389 	if (!isp->inputs[index].camera)
390 		return -EINVAL;
391 
392 	memset(input, 0, sizeof(struct v4l2_input));
393 	strscpy(input->name, isp->inputs[index].camera->name,
394 		sizeof(input->name));
395 
396 	input->type = V4L2_INPUT_TYPE_CAMERA;
397 	input->index = index;
398 	input->reserved[1] = isp->inputs[index].port;
399 
400 	return 0;
401 }
402 
403 /*
404  * get input are used to get current primary/secondary camera
405  */
atomisp_g_input(struct file * file,void * fh,unsigned int * input)406 static int atomisp_g_input(struct file *file, void *fh, unsigned int *input)
407 {
408 	struct video_device *vdev = video_devdata(file);
409 	struct atomisp_sub_device *asd = atomisp_to_video_pipe(vdev)->asd;
410 
411 	*input = asd->input_curr;
412 	return 0;
413 }
414 
atomisp_s_fmt_cap(struct file * file,void * fh,struct v4l2_format * f)415 static int atomisp_s_fmt_cap(struct file *file, void *fh,
416 			     struct v4l2_format *f)
417 {
418 	struct video_device *vdev = video_devdata(file);
419 
420 	return atomisp_set_fmt(vdev, f);
421 }
422 
423 /*
424  * set input are used to set current primary/secondary camera
425  */
atomisp_s_input(struct file * file,void * fh,unsigned int input)426 static int atomisp_s_input(struct file *file, void *fh, unsigned int input)
427 {
428 	struct video_device *vdev = video_devdata(file);
429 	struct atomisp_device *isp = video_get_drvdata(vdev);
430 	struct atomisp_video_pipe *pipe = atomisp_to_video_pipe(vdev);
431 	int ret;
432 
433 	if (input >= isp->input_cnt)
434 		return -EINVAL;
435 
436 	if (!isp->inputs[input].camera)
437 		return -EINVAL;
438 
439 	ret = atomisp_pipe_check(pipe, true);
440 	if (ret)
441 		return ret;
442 
443 	mutex_lock(&isp->media_dev.graph_mutex);
444 	ret = atomisp_select_input(isp, input);
445 	mutex_unlock(&isp->media_dev.graph_mutex);
446 
447 	return ret;
448 }
449 
450 /*
451  * With crop any framesize <= sensor-size can be made, give
452  * userspace a list of sizes to choice from.
453  */
atomisp_enum_framesizes_crop_inner(struct atomisp_device * isp,struct v4l2_frmsizeenum * fsize,const struct v4l2_rect * active,const struct v4l2_rect * native,int * valid_sizes)454 static int atomisp_enum_framesizes_crop_inner(struct atomisp_device *isp,
455 					      struct v4l2_frmsizeenum *fsize,
456 					      const struct v4l2_rect *active,
457 					      const struct v4l2_rect *native,
458 					      int *valid_sizes)
459 {
460 	static const struct v4l2_frmsize_discrete frame_sizes[] = {
461 		{ 1920, 1440 },
462 		{ 1920, 1200 },
463 		{ 1920, 1080 },
464 		{ 1600, 1200 },
465 		{ 1600, 1080 },
466 		{ 1600,  900 },
467 		{ 1440, 1080 },
468 		{ 1280,  960 },
469 		{ 1280,  720 },
470 		{  800,  600 },
471 		{  640,  480 },
472 	};
473 	u32 padding_w, padding_h;
474 	int i;
475 
476 	for (i = 0; i < ARRAY_SIZE(frame_sizes); i++) {
477 		atomisp_get_padding(isp, frame_sizes[i].width, frame_sizes[i].height,
478 				    &padding_w, &padding_h);
479 
480 		if ((frame_sizes[i].width + padding_w) > native->width ||
481 		    (frame_sizes[i].height + padding_h) > native->height)
482 			continue;
483 
484 		/*
485 		 * Skip sizes where width and height are less then 5/8th of the
486 		 * sensor size to avoid sizes with a too small field of view.
487 		 */
488 		if (frame_sizes[i].width < (active->width * 5 / 8) &&
489 		    frame_sizes[i].height < (active->height * 5 / 8))
490 			continue;
491 
492 		if (*valid_sizes == fsize->index) {
493 			fsize->type = V4L2_FRMSIZE_TYPE_DISCRETE;
494 			fsize->discrete = frame_sizes[i];
495 			return 0;
496 		}
497 
498 		(*valid_sizes)++;
499 	}
500 
501 	return -EINVAL;
502 }
503 
atomisp_enum_framesizes_crop(struct atomisp_device * isp,struct v4l2_frmsizeenum * fsize)504 static int atomisp_enum_framesizes_crop(struct atomisp_device *isp,
505 					struct v4l2_frmsizeenum *fsize)
506 {
507 	struct atomisp_input_subdev *input = &isp->inputs[isp->asd.input_curr];
508 	struct v4l2_rect active = input->active_rect;
509 	struct v4l2_rect native = input->native_rect;
510 	int ret, valid_sizes = 0;
511 
512 	ret = atomisp_enum_framesizes_crop_inner(isp, fsize, &active, &native, &valid_sizes);
513 	if (ret == 0)
514 		return 0;
515 
516 	if (!input->binning_support)
517 		return -EINVAL;
518 
519 	active.width /= 2;
520 	active.height /= 2;
521 	native.width /= 2;
522 	native.height /= 2;
523 
524 	return atomisp_enum_framesizes_crop_inner(isp, fsize, &active, &native, &valid_sizes);
525 }
526 
atomisp_enum_framesizes(struct file * file,void * priv,struct v4l2_frmsizeenum * fsize)527 static int atomisp_enum_framesizes(struct file *file, void *priv,
528 				   struct v4l2_frmsizeenum *fsize)
529 {
530 	struct video_device *vdev = video_devdata(file);
531 	struct atomisp_device *isp = video_get_drvdata(vdev);
532 	struct atomisp_sub_device *asd = atomisp_to_video_pipe(vdev)->asd;
533 	struct atomisp_input_subdev *input = &isp->inputs[asd->input_curr];
534 	struct v4l2_subdev_frame_size_enum fse = {
535 		.index = fsize->index,
536 		.which = V4L2_SUBDEV_FORMAT_ACTIVE,
537 		.code = input->code,
538 	};
539 	struct v4l2_subdev_state *act_sd_state;
540 	int ret;
541 
542 	if (!input->camera)
543 		return -EINVAL;
544 
545 	if (input->crop_support)
546 		return atomisp_enum_framesizes_crop(isp, fsize);
547 
548 	act_sd_state = v4l2_subdev_lock_and_get_active_state(input->camera);
549 	ret = v4l2_subdev_call(input->camera, pad, enum_frame_size,
550 			       act_sd_state, &fse);
551 	if (act_sd_state)
552 		v4l2_subdev_unlock_state(act_sd_state);
553 	if (ret)
554 		return ret;
555 
556 	fsize->type = V4L2_FRMSIZE_TYPE_DISCRETE;
557 	fsize->discrete.width = fse.max_width - pad_w;
558 	fsize->discrete.height = fse.max_height - pad_h;
559 
560 	return 0;
561 }
562 
atomisp_enum_frameintervals(struct file * file,void * priv,struct v4l2_frmivalenum * fival)563 static int atomisp_enum_frameintervals(struct file *file, void *priv,
564 				       struct v4l2_frmivalenum *fival)
565 {
566 	struct video_device *vdev = video_devdata(file);
567 	struct atomisp_device *isp = video_get_drvdata(vdev);
568 	struct atomisp_sub_device *asd = atomisp_to_video_pipe(vdev)->asd;
569 	struct atomisp_input_subdev *input = &isp->inputs[asd->input_curr];
570 	struct v4l2_subdev_frame_interval_enum fie = {
571 		.code = atomisp_in_fmt_conv[0].code,
572 		.index = fival->index,
573 		.width = fival->width,
574 		.height = fival->height,
575 		.which = V4L2_SUBDEV_FORMAT_ACTIVE,
576 	};
577 	struct v4l2_subdev_state *act_sd_state;
578 	int ret;
579 
580 	if (!input->camera)
581 		return -EINVAL;
582 
583 	act_sd_state = v4l2_subdev_lock_and_get_active_state(input->camera);
584 	ret = v4l2_subdev_call(input->camera, pad, enum_frame_interval,
585 			       act_sd_state, &fie);
586 	if (act_sd_state)
587 		v4l2_subdev_unlock_state(act_sd_state);
588 	if (ret)
589 		return ret;
590 
591 	fival->type = V4L2_FRMIVAL_TYPE_DISCRETE;
592 	fival->discrete = fie.interval;
593 
594 	return ret;
595 }
596 
atomisp_enum_fmt_cap(struct file * file,void * fh,struct v4l2_fmtdesc * f)597 static int atomisp_enum_fmt_cap(struct file *file, void *fh,
598 				struct v4l2_fmtdesc *f)
599 {
600 	struct video_device *vdev = video_devdata(file);
601 	struct atomisp_device *isp = video_get_drvdata(vdev);
602 	struct atomisp_sub_device *asd = atomisp_to_video_pipe(vdev)->asd;
603 	struct atomisp_input_subdev *input = &isp->inputs[asd->input_curr];
604 	struct v4l2_subdev_mbus_code_enum code = {
605 		.which = V4L2_SUBDEV_FORMAT_ACTIVE,
606 	};
607 	const struct atomisp_format_bridge *format;
608 	struct v4l2_subdev_state *act_sd_state;
609 	unsigned int i, fi = 0;
610 	int ret;
611 
612 	if (!input->camera)
613 		return -EINVAL;
614 
615 	act_sd_state = v4l2_subdev_lock_and_get_active_state(input->camera);
616 	ret = v4l2_subdev_call(input->camera, pad, enum_mbus_code,
617 			       act_sd_state, &code);
618 	if (act_sd_state)
619 		v4l2_subdev_unlock_state(act_sd_state);
620 	if (ret)
621 		return ret;
622 
623 	for (i = 0; i < ARRAY_SIZE(atomisp_output_fmts); i++) {
624 		format = &atomisp_output_fmts[i];
625 
626 		/*
627 		 * Is the atomisp-supported format is valid for the
628 		 * sensor (configuration)? If not, skip it.
629 		 *
630 		 * FIXME: fix the pipeline to allow sensor format too.
631 		 */
632 		if (format->sh_fmt == IA_CSS_FRAME_FORMAT_RAW)
633 			continue;
634 
635 		/* Found a match. Now let's pick f->index'th one. */
636 		if (fi < f->index) {
637 			fi++;
638 			continue;
639 		}
640 
641 		strscpy(f->description, format->description,
642 			sizeof(f->description));
643 		f->pixelformat = format->pixelformat;
644 		return 0;
645 	}
646 
647 	return -EINVAL;
648 }
649 
650 /* This function looks up the closest available resolution. */
atomisp_try_fmt_cap(struct file * file,void * fh,struct v4l2_format * f)651 static int atomisp_try_fmt_cap(struct file *file, void *fh,
652 			       struct v4l2_format *f)
653 {
654 	struct video_device *vdev = video_devdata(file);
655 	struct atomisp_device *isp = video_get_drvdata(vdev);
656 
657 	return atomisp_try_fmt(isp, &f->fmt.pix, NULL, NULL);
658 }
659 
atomisp_g_fmt_cap(struct file * file,void * fh,struct v4l2_format * f)660 static int atomisp_g_fmt_cap(struct file *file, void *fh,
661 			     struct v4l2_format *f)
662 {
663 	struct video_device *vdev = video_devdata(file);
664 	struct atomisp_video_pipe *pipe;
665 
666 	pipe = atomisp_to_video_pipe(vdev);
667 
668 	f->fmt.pix = pipe->pix;
669 
670 	/* If s_fmt was issued, just return whatever is was previously set */
671 	if (f->fmt.pix.sizeimage)
672 		return 0;
673 
674 	f->fmt.pix.pixelformat = V4L2_PIX_FMT_YUV420;
675 	f->fmt.pix.width = 10000;
676 	f->fmt.pix.height = 10000;
677 
678 	return atomisp_try_fmt_cap(file, fh, f);
679 }
680 
atomisp_alloc_css_stat_bufs(struct atomisp_sub_device * asd,uint16_t stream_id)681 int atomisp_alloc_css_stat_bufs(struct atomisp_sub_device *asd,
682 				uint16_t stream_id)
683 {
684 	struct atomisp_device *isp = asd->isp;
685 	struct atomisp_s3a_buf *s3a_buf = NULL, *_s3a_buf;
686 	struct atomisp_dis_buf *dis_buf = NULL, *_dis_buf;
687 	struct atomisp_metadata_buf *md_buf = NULL, *_md_buf;
688 	int count;
689 	struct ia_css_dvs_grid_info *dvs_grid_info =
690 	    atomisp_css_get_dvs_grid_info(&asd->params.curr_grid_info);
691 	unsigned int i;
692 
693 	if (list_empty(&asd->s3a_stats) &&
694 	    asd->params.curr_grid_info.s3a_grid.enable) {
695 		count = ATOMISP_CSS_Q_DEPTH +
696 			ATOMISP_S3A_BUF_QUEUE_DEPTH_FOR_HAL;
697 		dev_dbg(isp->dev, "allocating %d 3a buffers\n", count);
698 		while (count--) {
699 			s3a_buf = kzalloc(sizeof(struct atomisp_s3a_buf), GFP_KERNEL);
700 			if (!s3a_buf)
701 				goto error;
702 
703 			if (atomisp_css_allocate_stat_buffers(
704 				asd, stream_id, s3a_buf, NULL, NULL)) {
705 				kfree(s3a_buf);
706 				goto error;
707 			}
708 
709 			list_add_tail(&s3a_buf->list, &asd->s3a_stats);
710 		}
711 	}
712 
713 	if (list_empty(&asd->dis_stats) && dvs_grid_info &&
714 	    dvs_grid_info->enable) {
715 		count = ATOMISP_CSS_Q_DEPTH + 1;
716 		dev_dbg(isp->dev, "allocating %d dis buffers\n", count);
717 		while (count--) {
718 			dis_buf = kzalloc(sizeof(struct atomisp_dis_buf), GFP_KERNEL);
719 			if (!dis_buf)
720 				goto error;
721 			if (atomisp_css_allocate_stat_buffers(
722 				asd, stream_id, NULL, dis_buf, NULL)) {
723 				kfree(dis_buf);
724 				goto error;
725 			}
726 
727 			list_add_tail(&dis_buf->list, &asd->dis_stats);
728 		}
729 	}
730 
731 	for (i = 0; i < ATOMISP_METADATA_TYPE_NUM; i++) {
732 		if (list_empty(&asd->metadata[i]) &&
733 		    list_empty(&asd->metadata_ready[i]) &&
734 		    list_empty(&asd->metadata_in_css[i])) {
735 			count = ATOMISP_CSS_Q_DEPTH +
736 				ATOMISP_METADATA_QUEUE_DEPTH_FOR_HAL;
737 			dev_dbg(isp->dev, "allocating %d metadata buffers for type %d\n",
738 				count, i);
739 			while (count--) {
740 				md_buf = kzalloc(sizeof(struct atomisp_metadata_buf),
741 						 GFP_KERNEL);
742 				if (!md_buf)
743 					goto error;
744 
745 				if (atomisp_css_allocate_stat_buffers(
746 					asd, stream_id, NULL, NULL, md_buf)) {
747 					kfree(md_buf);
748 					goto error;
749 				}
750 				list_add_tail(&md_buf->list, &asd->metadata[i]);
751 			}
752 		}
753 	}
754 	return 0;
755 
756 error:
757 	dev_err(isp->dev, "failed to allocate statistics buffers\n");
758 
759 	list_for_each_entry_safe(dis_buf, _dis_buf, &asd->dis_stats, list) {
760 		atomisp_css_free_dis_buffer(dis_buf);
761 		list_del(&dis_buf->list);
762 		kfree(dis_buf);
763 	}
764 
765 	list_for_each_entry_safe(s3a_buf, _s3a_buf, &asd->s3a_stats, list) {
766 		atomisp_css_free_3a_buffer(s3a_buf);
767 		list_del(&s3a_buf->list);
768 		kfree(s3a_buf);
769 	}
770 
771 	for (i = 0; i < ATOMISP_METADATA_TYPE_NUM; i++) {
772 		list_for_each_entry_safe(md_buf, _md_buf, &asd->metadata[i],
773 					 list) {
774 			atomisp_css_free_metadata_buffer(md_buf);
775 			list_del(&md_buf->list);
776 			kfree(md_buf);
777 		}
778 	}
779 	return -ENOMEM;
780 }
781 
782 /*
783  * FIXME the abuse of buf->reserved2 in the qbuf and dqbuf wrappers comes from
784  * the original atomisp buffer handling and should be replaced with proper V4L2
785  * per frame parameters use.
786  *
787  * Once this is fixed these wrappers can be removed, replacing them with direct
788  * calls to vb2_ioctl_[d]qbuf().
789  */
atomisp_qbuf_wrapper(struct file * file,void * fh,struct v4l2_buffer * buf)790 static int atomisp_qbuf_wrapper(struct file *file, void *fh, struct v4l2_buffer *buf)
791 {
792 	struct video_device *vdev = video_devdata(file);
793 	struct atomisp_device *isp = video_get_drvdata(vdev);
794 	struct atomisp_video_pipe *pipe = atomisp_to_video_pipe(vdev);
795 
796 	if (buf->index >= vb2_get_num_buffers(vdev->queue))
797 		return -EINVAL;
798 
799 	if (buf->reserved2 & ATOMISP_BUFFER_HAS_PER_FRAME_SETTING) {
800 		/* this buffer will have a per-frame parameter */
801 		pipe->frame_request_config_id[buf->index] = buf->reserved2 &
802 			~ATOMISP_BUFFER_HAS_PER_FRAME_SETTING;
803 		dev_dbg(isp->dev,
804 			"This buffer requires per_frame setting which has isp_config_id %d\n",
805 			pipe->frame_request_config_id[buf->index]);
806 	} else {
807 		pipe->frame_request_config_id[buf->index] = 0;
808 	}
809 
810 	return vb2_ioctl_qbuf(file, fh, buf);
811 }
812 
atomisp_dqbuf_wrapper(struct file * file,void * fh,struct v4l2_buffer * buf)813 static int atomisp_dqbuf_wrapper(struct file *file, void *fh, struct v4l2_buffer *buf)
814 {
815 	struct video_device *vdev = video_devdata(file);
816 	struct atomisp_video_pipe *pipe = atomisp_to_video_pipe(vdev);
817 	struct atomisp_device *isp = video_get_drvdata(vdev);
818 	struct ia_css_frame *frame;
819 	struct vb2_buffer *vb;
820 	int ret;
821 
822 	ret = vb2_ioctl_dqbuf(file, fh, buf);
823 	if (ret)
824 		return ret;
825 
826 	vb = vb2_get_buffer(&pipe->vb_queue, buf->index);
827 	frame = vb_to_frame(vb);
828 
829 	/* reserved bit[31:16] is used for exp_id */
830 	buf->reserved = 0;
831 	if (!(buf->flags & V4L2_BUF_FLAG_ERROR))
832 		buf->reserved |= frame->exp_id;
833 	buf->reserved2 = pipe->frame_config_id[buf->index];
834 
835 	dev_dbg(isp->dev,
836 		"dqbuf buffer %d (%s) with exp_id %d, isp_config_id %d\n",
837 		buf->index, vdev->name, buf->reserved >> 16, buf->reserved2);
838 	return 0;
839 }
840 
841 /* Input system HW workaround */
842 /* Input system address translation corrupts burst during */
843 /* invalidate. SW workaround for this is to set burst length */
844 /* manually to 128 in case of 13MPx snapshot and to 1 otherwise. */
atomisp_dma_burst_len_cfg(struct atomisp_sub_device * asd)845 static void atomisp_dma_burst_len_cfg(struct atomisp_sub_device *asd)
846 {
847 	struct v4l2_mbus_framefmt *sink;
848 
849 	sink = atomisp_subdev_get_ffmt(&asd->subdev, NULL,
850 				       V4L2_SUBDEV_FORMAT_ACTIVE,
851 				       ATOMISP_SUBDEV_PAD_SINK);
852 
853 	if (sink->width * sink->height >= 4096 * 3072)
854 		atomisp_css2_hw_store_32(DMA_BURST_SIZE_REG, 0x7F);
855 	else
856 		atomisp_css2_hw_store_32(DMA_BURST_SIZE_REG, 0x00);
857 }
858 
atomisp_start_streaming(struct vb2_queue * vq,unsigned int count)859 int atomisp_start_streaming(struct vb2_queue *vq, unsigned int count)
860 {
861 	struct atomisp_video_pipe *pipe = vq_to_pipe(vq);
862 	struct atomisp_sub_device *asd = pipe->asd;
863 	struct atomisp_device *isp = asd->isp;
864 	struct pci_dev *pdev = to_pci_dev(isp->dev);
865 	unsigned long irqflags;
866 	int ret;
867 
868 	dev_dbg(isp->dev, "Start stream\n");
869 
870 	mutex_lock(&isp->mutex);
871 
872 	ret = atomisp_pipe_check(pipe, false);
873 	if (ret) {
874 		atomisp_flush_video_pipe(pipe, VB2_BUF_STATE_QUEUED, true);
875 		goto out_unlock;
876 	}
877 
878 	/*
879 	 * When running a classic v4l2 app after a media-controller aware
880 	 * app, the CSI-receiver -> ISP link for the current sensor may be
881 	 * disabled. Fix this up before marking the pipeline as started.
882 	 */
883 	mutex_lock(&isp->media_dev.graph_mutex);
884 	atomisp_setup_input_links(isp);
885 	ret = __media_pipeline_start(&asd->video_out.vdev.entity.pads[0], &asd->video_out.pipe);
886 	mutex_unlock(&isp->media_dev.graph_mutex);
887 	if (ret) {
888 		dev_err(isp->dev, "Error starting mc pipeline: %d\n", ret);
889 		atomisp_flush_video_pipe(pipe, VB2_BUF_STATE_QUEUED, true);
890 		goto out_unlock;
891 	}
892 
893 	/* Input system HW workaround */
894 	atomisp_dma_burst_len_cfg(asd);
895 
896 	/* Invalidate caches. FIXME: should flush only necessary buffers */
897 	wbinvd();
898 
899 	if (asd->params.css_update_params_needed) {
900 		atomisp_apply_css_parameters(asd, &asd->params.css_param);
901 		if (asd->params.css_param.update_flag.dz_config)
902 			asd->params.config.dz_config = &asd->params.css_param.dz_config;
903 		atomisp_css_update_isp_params(asd);
904 		asd->params.css_update_params_needed = false;
905 		memset(&asd->params.css_param.update_flag, 0,
906 		       sizeof(struct atomisp_parameters));
907 	}
908 	asd->params.dvs_6axis = NULL;
909 
910 	ret = atomisp_css_start(asd);
911 	if (ret) {
912 		atomisp_flush_video_pipe(pipe, VB2_BUF_STATE_QUEUED, true);
913 		goto out_unlock;
914 	}
915 
916 	spin_lock_irqsave(&isp->lock, irqflags);
917 	asd->streaming = true;
918 	spin_unlock_irqrestore(&isp->lock, irqflags);
919 	atomic_set(&asd->sof_count, 0);
920 	atomic_set(&asd->sequence, 0);
921 	atomic_set(&asd->sequence_temp, 0);
922 
923 	asd->params.dis_proj_data_valid = false;
924 	asd->latest_preview_exp_id = 0;
925 	asd->postview_exp_id = 1;
926 	asd->preview_exp_id = 1;
927 
928 	/* handle per_frame_setting parameter and buffers */
929 	atomisp_handle_parameter_and_buffer(pipe);
930 
931 	atomisp_qbuffers_to_css(asd);
932 
933 	atomisp_css_irq_enable(isp, IA_CSS_IRQ_INFO_CSS_RECEIVER_SOF,
934 			       atomisp_css_valid_sof(isp));
935 	atomisp_csi2_configure(asd);
936 
937 	if (atomisp_freq_scaling(isp, ATOMISP_DFS_MODE_AUTO, false) < 0)
938 		dev_dbg(isp->dev, "DFS auto mode failed!\n");
939 
940 	/* Enable the CSI interface on ANN B0/K0 */
941 	if (isp->media_dev.hw_revision >= ((ATOMISP_HW_REVISION_ISP2401 <<
942 					    ATOMISP_HW_REVISION_SHIFT) | ATOMISP_HW_STEPPING_B0)) {
943 		pci_write_config_word(pdev, MRFLD_PCI_CSI_CONTROL,
944 				      isp->saved_regs.csi_control | MRFLD_PCI_CSI_CONTROL_CSI_READY);
945 	}
946 
947 	/* stream on the sensor */
948 	ret = v4l2_subdev_call(isp->inputs[asd->input_curr].camera,
949 			       video, s_stream, 1);
950 	if (ret) {
951 		dev_err(isp->dev, "Starting sensor stream failed: %d\n", ret);
952 		spin_lock_irqsave(&isp->lock, irqflags);
953 		asd->streaming = false;
954 		spin_unlock_irqrestore(&isp->lock, irqflags);
955 		ret = -EINVAL;
956 		goto out_unlock;
957 	}
958 
959 out_unlock:
960 	mutex_unlock(&isp->mutex);
961 	return ret;
962 }
963 
atomisp_stop_streaming(struct vb2_queue * vq)964 void atomisp_stop_streaming(struct vb2_queue *vq)
965 {
966 	struct atomisp_video_pipe *pipe = vq_to_pipe(vq);
967 	struct atomisp_sub_device *asd = pipe->asd;
968 	struct atomisp_device *isp = asd->isp;
969 	struct pci_dev *pdev = to_pci_dev(isp->dev);
970 	unsigned long flags;
971 	int ret;
972 
973 	dev_dbg(isp->dev, "Stop stream\n");
974 
975 	mutex_lock(&isp->mutex);
976 	/*
977 	 * There is no guarantee that the buffers queued to / owned by the ISP
978 	 * will properly be returned to the queue when stopping. Set a flag to
979 	 * avoid new buffers getting queued and then wait for all the current
980 	 * buffers to finish.
981 	 */
982 	pipe->stopping = true;
983 	mutex_unlock(&isp->mutex);
984 	/* wait max 1 second */
985 	ret = wait_event_timeout(pipe->vb_queue.done_wq,
986 				 atomisp_buffers_in_css(pipe) == 0, HZ);
987 	mutex_lock(&isp->mutex);
988 	pipe->stopping = false;
989 	if (ret == 0)
990 		dev_warn(isp->dev, "Warning timeout waiting for CSS to return buffers\n");
991 
992 	spin_lock_irqsave(&isp->lock, flags);
993 	asd->streaming = false;
994 	spin_unlock_irqrestore(&isp->lock, flags);
995 
996 	atomisp_clear_css_buffer_counters(asd);
997 	atomisp_css_irq_enable(isp, IA_CSS_IRQ_INFO_CSS_RECEIVER_SOF, false);
998 
999 	atomisp_css_stop(asd, false);
1000 
1001 	atomisp_flush_video_pipe(pipe, VB2_BUF_STATE_ERROR, true);
1002 
1003 	atomisp_subdev_cleanup_pending_events(asd);
1004 
1005 	ret = v4l2_subdev_call(isp->inputs[asd->input_curr].camera,
1006 			       video, s_stream, 0);
1007 	if (ret)
1008 		dev_warn(isp->dev, "Stopping sensor stream failed: %d\n", ret);
1009 
1010 	/* Disable the CSI interface on ANN B0/K0 */
1011 	if (isp->media_dev.hw_revision >= ((ATOMISP_HW_REVISION_ISP2401 <<
1012 					    ATOMISP_HW_REVISION_SHIFT) | ATOMISP_HW_STEPPING_B0)) {
1013 		pci_write_config_word(pdev, MRFLD_PCI_CSI_CONTROL,
1014 				      isp->saved_regs.csi_control & ~MRFLD_PCI_CSI_CONTROL_CSI_READY);
1015 	}
1016 
1017 	if (atomisp_freq_scaling(isp, ATOMISP_DFS_MODE_LOW, false))
1018 		dev_warn(isp->dev, "DFS failed.\n");
1019 
1020 	/*
1021 	 * ISP work around, need to reset ISP to allow next stream on to work.
1022 	 * Streams have already been destroyed by atomisp_css_stop().
1023 	 * Disable PUNIT/ISP acknowledge/handshake - SRSE=3 and then reset.
1024 	 */
1025 	pci_write_config_dword(pdev, PCI_I_CONTROL,
1026 			       isp->saved_regs.i_control | MRFLD_PCI_I_CONTROL_SRSE_RESET_MASK);
1027 	atomisp_reset(isp);
1028 
1029 	/* Streams were destroyed by atomisp_css_stop(), recreate them. */
1030 	ret = atomisp_create_pipes_stream(&isp->asd);
1031 	if (ret)
1032 		dev_warn(isp->dev, "Recreating streams failed: %d\n", ret);
1033 
1034 	media_pipeline_stop(&asd->video_out.vdev.entity.pads[0]);
1035 	mutex_unlock(&isp->mutex);
1036 }
1037 
1038 /*
1039  * To get the current value of a control.
1040  * applications initialize the id field of a struct v4l2_control and
1041  * call this ioctl with a pointer to this structure
1042  */
atomisp_g_ctrl(struct file * file,void * fh,struct v4l2_control * control)1043 static int atomisp_g_ctrl(struct file *file, void *fh,
1044 			  struct v4l2_control *control)
1045 {
1046 	struct video_device *vdev = video_devdata(file);
1047 	struct atomisp_sub_device *asd = atomisp_to_video_pipe(vdev)->asd;
1048 	int i, ret = -EINVAL;
1049 
1050 	for (i = 0; i < ctrls_num; i++) {
1051 		if (ci_v4l2_controls[i].id == control->id) {
1052 			ret = 0;
1053 			break;
1054 		}
1055 	}
1056 
1057 	if (ret)
1058 		return ret;
1059 
1060 	switch (control->id) {
1061 	case V4L2_CID_COLORFX:
1062 		ret = atomisp_color_effect(asd, 0, &control->value);
1063 		break;
1064 	case V4L2_CID_ATOMISP_BAD_PIXEL_DETECTION:
1065 		ret = atomisp_bad_pixel(asd, 0, &control->value);
1066 		break;
1067 	case V4L2_CID_ATOMISP_POSTPROCESS_GDC_CAC:
1068 		ret = atomisp_gdc_cac(asd, 0, &control->value);
1069 		break;
1070 	case V4L2_CID_ATOMISP_VIDEO_STABLIZATION:
1071 		ret = atomisp_video_stable(asd, 0, &control->value);
1072 		break;
1073 	case V4L2_CID_ATOMISP_FIXED_PATTERN_NR:
1074 		ret = atomisp_fixed_pattern(asd, 0, &control->value);
1075 		break;
1076 	case V4L2_CID_ATOMISP_FALSE_COLOR_CORRECTION:
1077 		ret = atomisp_false_color(asd, 0, &control->value);
1078 		break;
1079 	case V4L2_CID_ATOMISP_LOW_LIGHT:
1080 		ret = atomisp_low_light(asd, 0, &control->value);
1081 		break;
1082 	default:
1083 		ret = -EINVAL;
1084 		break;
1085 	}
1086 
1087 	return ret;
1088 }
1089 
1090 /*
1091  * To change the value of a control.
1092  * applications initialize the id and value fields of a struct v4l2_control
1093  * and call this ioctl.
1094  */
atomisp_s_ctrl(struct file * file,void * fh,struct v4l2_control * control)1095 static int atomisp_s_ctrl(struct file *file, void *fh,
1096 			  struct v4l2_control *control)
1097 {
1098 	struct video_device *vdev = video_devdata(file);
1099 	struct atomisp_sub_device *asd = atomisp_to_video_pipe(vdev)->asd;
1100 	int i, ret = -EINVAL;
1101 
1102 	for (i = 0; i < ctrls_num; i++) {
1103 		if (ci_v4l2_controls[i].id == control->id) {
1104 			ret = 0;
1105 			break;
1106 		}
1107 	}
1108 
1109 	if (ret)
1110 		return ret;
1111 
1112 	switch (control->id) {
1113 	case V4L2_CID_COLORFX:
1114 		ret = atomisp_color_effect(asd, 1, &control->value);
1115 		break;
1116 	case V4L2_CID_ATOMISP_BAD_PIXEL_DETECTION:
1117 		ret = atomisp_bad_pixel(asd, 1, &control->value);
1118 		break;
1119 	case V4L2_CID_ATOMISP_POSTPROCESS_GDC_CAC:
1120 		ret = atomisp_gdc_cac(asd, 1, &control->value);
1121 		break;
1122 	case V4L2_CID_ATOMISP_VIDEO_STABLIZATION:
1123 		ret = atomisp_video_stable(asd, 1, &control->value);
1124 		break;
1125 	case V4L2_CID_ATOMISP_FIXED_PATTERN_NR:
1126 		ret = atomisp_fixed_pattern(asd, 1, &control->value);
1127 		break;
1128 	case V4L2_CID_ATOMISP_FALSE_COLOR_CORRECTION:
1129 		ret = atomisp_false_color(asd, 1, &control->value);
1130 		break;
1131 	case V4L2_CID_ATOMISP_LOW_LIGHT:
1132 		ret = atomisp_low_light(asd, 1, &control->value);
1133 		break;
1134 	default:
1135 		ret = -EINVAL;
1136 		break;
1137 	}
1138 	return ret;
1139 }
1140 
1141 /*
1142  * To query the attributes of a control.
1143  * applications set the id field of a struct v4l2_queryctrl and call the
1144  * this ioctl with a pointer to this structure. The driver fills
1145  * the rest of the structure.
1146  */
atomisp_queryctl(struct file * file,void * fh,struct v4l2_queryctrl * qc)1147 static int atomisp_queryctl(struct file *file, void *fh,
1148 			    struct v4l2_queryctrl *qc)
1149 {
1150 	int i, ret = -EINVAL;
1151 
1152 	if (qc->id & V4L2_CTRL_FLAG_NEXT_CTRL)
1153 		return ret;
1154 
1155 	for (i = 0; i < ctrls_num; i++) {
1156 		if (ci_v4l2_controls[i].id == qc->id) {
1157 			memcpy(qc, &ci_v4l2_controls[i],
1158 			       sizeof(struct v4l2_queryctrl));
1159 			qc->reserved[0] = 0;
1160 			ret = 0;
1161 			break;
1162 		}
1163 	}
1164 	if (ret != 0)
1165 		qc->flags = V4L2_CTRL_FLAG_DISABLED;
1166 
1167 	return ret;
1168 }
1169 
atomisp_camera_g_ext_ctrls(struct file * file,void * fh,struct v4l2_ext_controls * c)1170 static int atomisp_camera_g_ext_ctrls(struct file *file, void *fh,
1171 				      struct v4l2_ext_controls *c)
1172 {
1173 	struct video_device *vdev = video_devdata(file);
1174 	struct atomisp_sub_device *asd = atomisp_to_video_pipe(vdev)->asd;
1175 	struct v4l2_control ctrl;
1176 	int i;
1177 	int ret = 0;
1178 
1179 	for (i = 0; i < c->count; i++) {
1180 		ctrl.id = c->controls[i].id;
1181 		ctrl.value = c->controls[i].value;
1182 		switch (ctrl.id) {
1183 		case V4L2_CID_ZOOM_ABSOLUTE:
1184 			ret = atomisp_digital_zoom(asd, 0, &ctrl.value);
1185 			break;
1186 		default:
1187 			ret = -EINVAL;
1188 		}
1189 
1190 		if (ret) {
1191 			c->error_idx = i;
1192 			break;
1193 		}
1194 		c->controls[i].value = ctrl.value;
1195 	}
1196 	return ret;
1197 }
1198 
1199 /* 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)1200 static int atomisp_g_ext_ctrls(struct file *file, void *fh,
1201 			       struct v4l2_ext_controls *c)
1202 {
1203 	struct v4l2_control ctrl;
1204 	int i, ret = 0;
1205 
1206 	/*
1207 	 * input_lock is not need for the Camera related IOCTLs
1208 	 * The input_lock downgrade the FPS of 3A
1209 	 */
1210 	ret = atomisp_camera_g_ext_ctrls(file, fh, c);
1211 	if (ret != -EINVAL)
1212 		return ret;
1213 
1214 	for (i = 0; i < c->count; i++) {
1215 		ctrl.id = c->controls[i].id;
1216 		ctrl.value = c->controls[i].value;
1217 		ret = atomisp_g_ctrl(file, fh, &ctrl);
1218 		c->controls[i].value = ctrl.value;
1219 		if (ret) {
1220 			c->error_idx = i;
1221 			break;
1222 		}
1223 	}
1224 	return ret;
1225 }
1226 
atomisp_camera_s_ext_ctrls(struct file * file,void * fh,struct v4l2_ext_controls * c)1227 static int atomisp_camera_s_ext_ctrls(struct file *file, void *fh,
1228 				      struct v4l2_ext_controls *c)
1229 {
1230 	struct video_device *vdev = video_devdata(file);
1231 	struct atomisp_sub_device *asd = atomisp_to_video_pipe(vdev)->asd;
1232 	struct v4l2_control ctrl;
1233 	int i;
1234 	int ret = 0;
1235 
1236 	for (i = 0; i < c->count; i++) {
1237 		struct v4l2_ctrl *ctr;
1238 
1239 		ctrl.id = c->controls[i].id;
1240 		ctrl.value = c->controls[i].value;
1241 		switch (ctrl.id) {
1242 		case V4L2_CID_ZOOM_ABSOLUTE:
1243 			ret = atomisp_digital_zoom(asd, 1, &ctrl.value);
1244 			break;
1245 		default:
1246 			ctr = v4l2_ctrl_find(&asd->ctrl_handler, ctrl.id);
1247 			if (ctr)
1248 				ret = v4l2_ctrl_s_ctrl(ctr, ctrl.value);
1249 			else
1250 				ret = -EINVAL;
1251 		}
1252 
1253 		if (ret) {
1254 			c->error_idx = i;
1255 			break;
1256 		}
1257 		c->controls[i].value = ctrl.value;
1258 	}
1259 	return ret;
1260 }
1261 
1262 /* 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)1263 static int atomisp_s_ext_ctrls(struct file *file, void *fh,
1264 			       struct v4l2_ext_controls *c)
1265 {
1266 	struct v4l2_control ctrl;
1267 	int i, ret = 0;
1268 
1269 	/*
1270 	 * input_lock is not need for the Camera related IOCTLs
1271 	 * The input_lock downgrade the FPS of 3A
1272 	 */
1273 	ret = atomisp_camera_s_ext_ctrls(file, fh, c);
1274 	if (ret != -EINVAL)
1275 		return ret;
1276 
1277 	for (i = 0; i < c->count; i++) {
1278 		ctrl.id = c->controls[i].id;
1279 		ctrl.value = c->controls[i].value;
1280 		ret = atomisp_s_ctrl(file, fh, &ctrl);
1281 		c->controls[i].value = ctrl.value;
1282 		if (ret) {
1283 			c->error_idx = i;
1284 			break;
1285 		}
1286 	}
1287 	return ret;
1288 }
1289 
1290 /*
1291  * vidioc_g/s_param are used to switch isp running mode
1292  */
atomisp_g_parm(struct file * file,void * fh,struct v4l2_streamparm * parm)1293 static int atomisp_g_parm(struct file *file, void *fh,
1294 			  struct v4l2_streamparm *parm)
1295 {
1296 	struct video_device *vdev = video_devdata(file);
1297 	struct atomisp_sub_device *asd = atomisp_to_video_pipe(vdev)->asd;
1298 	struct atomisp_device *isp = video_get_drvdata(vdev);
1299 
1300 	if (parm->type != V4L2_BUF_TYPE_VIDEO_CAPTURE) {
1301 		dev_err(isp->dev, "unsupported v4l2 buf type\n");
1302 		return -EINVAL;
1303 	}
1304 
1305 	parm->parm.capture.capturemode = asd->run_mode->val;
1306 
1307 	return 0;
1308 }
1309 
atomisp_s_parm(struct file * file,void * fh,struct v4l2_streamparm * parm)1310 static int atomisp_s_parm(struct file *file, void *fh,
1311 			  struct v4l2_streamparm *parm)
1312 {
1313 	struct video_device *vdev = video_devdata(file);
1314 	struct atomisp_device *isp = video_get_drvdata(vdev);
1315 	struct atomisp_sub_device *asd = atomisp_to_video_pipe(vdev)->asd;
1316 	int mode;
1317 	int rval;
1318 	int fps;
1319 
1320 	if (parm->type != V4L2_BUF_TYPE_VIDEO_CAPTURE) {
1321 		dev_err(isp->dev, "unsupported v4l2 buf type\n");
1322 		return -EINVAL;
1323 	}
1324 
1325 	asd->high_speed_mode = false;
1326 	switch (parm->parm.capture.capturemode) {
1327 	case CI_MODE_NONE: {
1328 		struct v4l2_subdev_frame_interval fi = {0};
1329 
1330 		fi.interval = parm->parm.capture.timeperframe;
1331 
1332 		rval = v4l2_subdev_call_state_active(isp->inputs[asd->input_curr].camera,
1333 						     pad, set_frame_interval, &fi);
1334 		if (!rval)
1335 			parm->parm.capture.timeperframe = fi.interval;
1336 
1337 		if (fi.interval.numerator != 0) {
1338 			fps = fi.interval.denominator / fi.interval.numerator;
1339 			if (fps > 30)
1340 				asd->high_speed_mode = true;
1341 		}
1342 
1343 		return rval == -ENOIOCTLCMD ? 0 : rval;
1344 	}
1345 	case CI_MODE_VIDEO:
1346 		mode = ATOMISP_RUN_MODE_VIDEO;
1347 		break;
1348 	case CI_MODE_STILL_CAPTURE:
1349 		mode = ATOMISP_RUN_MODE_STILL_CAPTURE;
1350 		break;
1351 	case CI_MODE_PREVIEW:
1352 		mode = ATOMISP_RUN_MODE_PREVIEW;
1353 		break;
1354 	default:
1355 		return -EINVAL;
1356 	}
1357 
1358 	rval = v4l2_ctrl_s_ctrl(asd->run_mode, mode);
1359 
1360 	return rval == -ENOIOCTLCMD ? 0 : rval;
1361 }
1362 
atomisp_vidioc_default(struct file * file,void * fh,bool valid_prio,unsigned int cmd,void * arg)1363 static long atomisp_vidioc_default(struct file *file, void *fh,
1364 				   bool valid_prio, unsigned int cmd, void *arg)
1365 {
1366 	struct video_device *vdev = video_devdata(file);
1367 	struct atomisp_sub_device *asd = atomisp_to_video_pipe(vdev)->asd;
1368 	int err;
1369 
1370 	switch (cmd) {
1371 	case ATOMISP_IOC_G_XNR:
1372 		err = atomisp_xnr(asd, 0, arg);
1373 		break;
1374 
1375 	case ATOMISP_IOC_S_XNR:
1376 		err = atomisp_xnr(asd, 1, arg);
1377 		break;
1378 
1379 	case ATOMISP_IOC_G_NR:
1380 		err = atomisp_nr(asd, 0, arg);
1381 		break;
1382 
1383 	case ATOMISP_IOC_S_NR:
1384 		err = atomisp_nr(asd, 1, arg);
1385 		break;
1386 
1387 	case ATOMISP_IOC_G_TNR:
1388 		err = atomisp_tnr(asd, 0, arg);
1389 		break;
1390 
1391 	case ATOMISP_IOC_S_TNR:
1392 		err = atomisp_tnr(asd, 1, arg);
1393 		break;
1394 
1395 	case ATOMISP_IOC_G_BLACK_LEVEL_COMP:
1396 		err = atomisp_black_level(asd, 0, arg);
1397 		break;
1398 
1399 	case ATOMISP_IOC_S_BLACK_LEVEL_COMP:
1400 		err = atomisp_black_level(asd, 1, arg);
1401 		break;
1402 
1403 	case ATOMISP_IOC_G_EE:
1404 		err = atomisp_ee(asd, 0, arg);
1405 		break;
1406 
1407 	case ATOMISP_IOC_S_EE:
1408 		err = atomisp_ee(asd, 1, arg);
1409 		break;
1410 
1411 	case ATOMISP_IOC_G_DIS_STAT:
1412 		err = atomisp_get_dis_stat(asd, arg);
1413 		break;
1414 
1415 	case ATOMISP_IOC_G_DVS2_BQ_RESOLUTIONS:
1416 		err = atomisp_get_dvs2_bq_resolutions(asd, arg);
1417 		break;
1418 
1419 	case ATOMISP_IOC_S_DIS_COEFS:
1420 		err = atomisp_css_cp_dvs2_coefs(asd, arg,
1421 						&asd->params.css_param, true);
1422 		if (!err && arg)
1423 			asd->params.css_update_params_needed = true;
1424 		break;
1425 
1426 	case ATOMISP_IOC_S_DIS_VECTOR:
1427 		err = atomisp_cp_dvs_6axis_config(asd, arg,
1428 						  &asd->params.css_param, true);
1429 		if (!err && arg)
1430 			asd->params.css_update_params_needed = true;
1431 		break;
1432 
1433 	case ATOMISP_IOC_G_ISP_PARM:
1434 		err = atomisp_param(asd, 0, arg);
1435 		break;
1436 
1437 	case ATOMISP_IOC_S_ISP_PARM:
1438 		err = atomisp_param(asd, 1, arg);
1439 		break;
1440 
1441 	case ATOMISP_IOC_G_3A_STAT:
1442 		err = atomisp_3a_stat(asd, 0, arg);
1443 		break;
1444 
1445 	case ATOMISP_IOC_G_ISP_GAMMA:
1446 		err = atomisp_gamma(asd, 0, arg);
1447 		break;
1448 
1449 	case ATOMISP_IOC_S_ISP_GAMMA:
1450 		err = atomisp_gamma(asd, 1, arg);
1451 		break;
1452 
1453 	case ATOMISP_IOC_G_ISP_GDC_TAB:
1454 		err = atomisp_gdc_cac_table(asd, 0, arg);
1455 		break;
1456 
1457 	case ATOMISP_IOC_S_ISP_GDC_TAB:
1458 		err = atomisp_gdc_cac_table(asd, 1, arg);
1459 		break;
1460 
1461 	case ATOMISP_IOC_G_ISP_MACC:
1462 		err = atomisp_macc_table(asd, 0, arg);
1463 		break;
1464 
1465 	case ATOMISP_IOC_S_ISP_MACC:
1466 		err = atomisp_macc_table(asd, 1, arg);
1467 		break;
1468 
1469 	case ATOMISP_IOC_G_ISP_BAD_PIXEL_DETECTION:
1470 		err = atomisp_bad_pixel_param(asd, 0, arg);
1471 		break;
1472 
1473 	case ATOMISP_IOC_S_ISP_BAD_PIXEL_DETECTION:
1474 		err = atomisp_bad_pixel_param(asd, 1, arg);
1475 		break;
1476 
1477 	case ATOMISP_IOC_G_ISP_FALSE_COLOR_CORRECTION:
1478 		err = atomisp_false_color_param(asd, 0, arg);
1479 		break;
1480 
1481 	case ATOMISP_IOC_S_ISP_FALSE_COLOR_CORRECTION:
1482 		err = atomisp_false_color_param(asd, 1, arg);
1483 		break;
1484 
1485 	case ATOMISP_IOC_G_ISP_CTC:
1486 		err = atomisp_ctc(asd, 0, arg);
1487 		break;
1488 
1489 	case ATOMISP_IOC_S_ISP_CTC:
1490 		err = atomisp_ctc(asd, 1, arg);
1491 		break;
1492 
1493 	case ATOMISP_IOC_G_ISP_WHITE_BALANCE:
1494 		err = atomisp_white_balance_param(asd, 0, arg);
1495 		break;
1496 
1497 	case ATOMISP_IOC_S_ISP_WHITE_BALANCE:
1498 		err = atomisp_white_balance_param(asd, 1, arg);
1499 		break;
1500 
1501 	case ATOMISP_IOC_G_3A_CONFIG:
1502 		err = atomisp_3a_config_param(asd, 0, arg);
1503 		break;
1504 
1505 	case ATOMISP_IOC_S_3A_CONFIG:
1506 		err = atomisp_3a_config_param(asd, 1, arg);
1507 		break;
1508 
1509 	case ATOMISP_IOC_S_ISP_FPN_TABLE:
1510 		err = atomisp_fixed_pattern_table(asd, arg);
1511 		break;
1512 
1513 	case ATOMISP_IOC_S_ISP_SHD_TAB:
1514 		err = atomisp_set_shading_table(asd, arg);
1515 		break;
1516 
1517 	case ATOMISP_IOC_G_ISP_GAMMA_CORRECTION:
1518 		err = atomisp_gamma_correction(asd, 0, arg);
1519 		break;
1520 
1521 	case ATOMISP_IOC_S_ISP_GAMMA_CORRECTION:
1522 		err = atomisp_gamma_correction(asd, 1, arg);
1523 		break;
1524 
1525 	case ATOMISP_IOC_S_PARAMETERS:
1526 		err = atomisp_set_parameters(vdev, arg);
1527 		break;
1528 
1529 	case ATOMISP_IOC_EXP_ID_UNLOCK:
1530 		err = atomisp_exp_id_unlock(asd, arg);
1531 		break;
1532 	case ATOMISP_IOC_EXP_ID_CAPTURE:
1533 		err = atomisp_exp_id_capture(asd, arg);
1534 		break;
1535 	case ATOMISP_IOC_S_ENABLE_DZ_CAPT_PIPE:
1536 		err = atomisp_enable_dz_capt_pipe(asd, arg);
1537 		break;
1538 	case ATOMISP_IOC_G_FORMATS_CONFIG:
1539 		err = atomisp_formats(asd, 0, arg);
1540 		break;
1541 
1542 	case ATOMISP_IOC_S_FORMATS_CONFIG:
1543 		err = atomisp_formats(asd, 1, arg);
1544 		break;
1545 	case ATOMISP_IOC_INJECT_A_FAKE_EVENT:
1546 		err = atomisp_inject_a_fake_event(asd, arg);
1547 		break;
1548 	case ATOMISP_IOC_S_ARRAY_RESOLUTION:
1549 		err = atomisp_set_array_res(asd, arg);
1550 		break;
1551 	default:
1552 		err = -EINVAL;
1553 		break;
1554 	}
1555 
1556 	return err;
1557 }
1558 
1559 const struct v4l2_ioctl_ops atomisp_ioctl_ops = {
1560 	.vidioc_querycap = atomisp_querycap,
1561 	.vidioc_enum_input = atomisp_enum_input,
1562 	.vidioc_g_input = atomisp_g_input,
1563 	.vidioc_s_input = atomisp_s_input,
1564 	.vidioc_queryctrl = atomisp_queryctl,
1565 	.vidioc_s_ctrl = atomisp_s_ctrl,
1566 	.vidioc_g_ctrl = atomisp_g_ctrl,
1567 	.vidioc_s_ext_ctrls = atomisp_s_ext_ctrls,
1568 	.vidioc_g_ext_ctrls = atomisp_g_ext_ctrls,
1569 	.vidioc_enum_framesizes   = atomisp_enum_framesizes,
1570 	.vidioc_enum_frameintervals = atomisp_enum_frameintervals,
1571 	.vidioc_enum_fmt_vid_cap = atomisp_enum_fmt_cap,
1572 	.vidioc_try_fmt_vid_cap = atomisp_try_fmt_cap,
1573 	.vidioc_g_fmt_vid_cap = atomisp_g_fmt_cap,
1574 	.vidioc_s_fmt_vid_cap = atomisp_s_fmt_cap,
1575 	.vidioc_reqbufs = vb2_ioctl_reqbufs,
1576 	.vidioc_querybuf = vb2_ioctl_querybuf,
1577 	.vidioc_qbuf = atomisp_qbuf_wrapper,
1578 	.vidioc_dqbuf = atomisp_dqbuf_wrapper,
1579 	.vidioc_expbuf = vb2_ioctl_expbuf,
1580 	.vidioc_streamon = vb2_ioctl_streamon,
1581 	.vidioc_streamoff = vb2_ioctl_streamoff,
1582 	.vidioc_default = atomisp_vidioc_default,
1583 	.vidioc_s_parm = atomisp_s_parm,
1584 	.vidioc_g_parm = atomisp_g_parm,
1585 };
1586