xref: /linux/drivers/staging/media/atomisp/pci/atomisp_ioctl.c (revision 32a92f8c89326985e05dce8b22d3f0aa07a3e1bd)
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_query_ext_ctrl 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].sensor)
390 		return -EINVAL;
391 
392 	memset(input, 0, sizeof(struct v4l2_input));
393 	strscpy(input->name, isp->inputs[index].sensor->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].sensor)
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->sensor)
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->sensor);
549 	ret = v4l2_subdev_call(input->sensor, 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->sensor)
581 		return -EINVAL;
582 
583 	act_sd_state = v4l2_subdev_lock_and_get_active_state(input->sensor);
584 	ret = v4l2_subdev_call(input->sensor, 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->sensor)
613 		return -EINVAL;
614 
615 	act_sd_state = v4l2_subdev_lock_and_get_active_state(input->sensor);
616 	ret = v4l2_subdev_call(input->sensor, 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_obj(struct atomisp_s3a_buf);
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_obj(struct atomisp_dis_buf);
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_obj(struct atomisp_metadata_buf);
741 				if (!md_buf)
742 					goto error;
743 
744 				if (atomisp_css_allocate_stat_buffers(
745 					asd, stream_id, NULL, NULL, md_buf)) {
746 					kfree(md_buf);
747 					goto error;
748 				}
749 				list_add_tail(&md_buf->list, &asd->metadata[i]);
750 			}
751 		}
752 	}
753 	return 0;
754 
755 error:
756 	dev_err(isp->dev, "failed to allocate statistics buffers\n");
757 
758 	list_for_each_entry_safe(dis_buf, _dis_buf, &asd->dis_stats, list) {
759 		atomisp_css_free_dis_buffer(dis_buf);
760 		list_del(&dis_buf->list);
761 		kfree(dis_buf);
762 	}
763 
764 	list_for_each_entry_safe(s3a_buf, _s3a_buf, &asd->s3a_stats, list) {
765 		atomisp_css_free_3a_buffer(s3a_buf);
766 		list_del(&s3a_buf->list);
767 		kfree(s3a_buf);
768 	}
769 
770 	for (i = 0; i < ATOMISP_METADATA_TYPE_NUM; i++) {
771 		list_for_each_entry_safe(md_buf, _md_buf, &asd->metadata[i],
772 					 list) {
773 			atomisp_css_free_metadata_buffer(md_buf);
774 			list_del(&md_buf->list);
775 			kfree(md_buf);
776 		}
777 	}
778 	return -ENOMEM;
779 }
780 
781 /*
782  * FIXME the abuse of buf->reserved2 in the qbuf and dqbuf wrappers comes from
783  * the original atomisp buffer handling and should be replaced with proper V4L2
784  * per frame parameters use.
785  *
786  * Once this is fixed these wrappers can be removed, replacing them with direct
787  * calls to vb2_ioctl_[d]qbuf().
788  */
atomisp_qbuf_wrapper(struct file * file,void * fh,struct v4l2_buffer * buf)789 static int atomisp_qbuf_wrapper(struct file *file, void *fh, struct v4l2_buffer *buf)
790 {
791 	struct video_device *vdev = video_devdata(file);
792 	struct atomisp_device *isp = video_get_drvdata(vdev);
793 	struct atomisp_video_pipe *pipe = atomisp_to_video_pipe(vdev);
794 
795 	if (buf->index >= vb2_get_num_buffers(vdev->queue))
796 		return -EINVAL;
797 
798 	if (buf->reserved2 & ATOMISP_BUFFER_HAS_PER_FRAME_SETTING) {
799 		/* this buffer will have a per-frame parameter */
800 		pipe->frame_request_config_id[buf->index] = buf->reserved2 &
801 			~ATOMISP_BUFFER_HAS_PER_FRAME_SETTING;
802 		dev_dbg(isp->dev,
803 			"This buffer requires per_frame setting which has isp_config_id %d\n",
804 			pipe->frame_request_config_id[buf->index]);
805 	} else {
806 		pipe->frame_request_config_id[buf->index] = 0;
807 	}
808 
809 	return vb2_ioctl_qbuf(file, fh, buf);
810 }
811 
atomisp_dqbuf_wrapper(struct file * file,void * fh,struct v4l2_buffer * buf)812 static int atomisp_dqbuf_wrapper(struct file *file, void *fh, struct v4l2_buffer *buf)
813 {
814 	struct video_device *vdev = video_devdata(file);
815 	struct atomisp_video_pipe *pipe = atomisp_to_video_pipe(vdev);
816 	struct atomisp_device *isp = video_get_drvdata(vdev);
817 	struct ia_css_frame *frame;
818 	struct vb2_buffer *vb;
819 	int ret;
820 
821 	ret = vb2_ioctl_dqbuf(file, fh, buf);
822 	if (ret)
823 		return ret;
824 
825 	vb = vb2_get_buffer(&pipe->vb_queue, buf->index);
826 	frame = vb_to_frame(vb);
827 
828 	/* reserved bit[31:16] is used for exp_id */
829 	buf->reserved = 0;
830 	if (!(buf->flags & V4L2_BUF_FLAG_ERROR))
831 		buf->reserved |= frame->exp_id;
832 	buf->reserved2 = pipe->frame_config_id[buf->index];
833 
834 	dev_dbg(isp->dev,
835 		"dqbuf buffer %d (%s) with exp_id %d, isp_config_id %d\n",
836 		buf->index, vdev->name, buf->reserved >> 16, buf->reserved2);
837 	return 0;
838 }
839 
840 /* Input system HW workaround */
841 /* Input system address translation corrupts burst during */
842 /* invalidate. SW workaround for this is to set burst length */
843 /* manually to 128 in case of 13MPx snapshot and to 1 otherwise. */
atomisp_dma_burst_len_cfg(struct atomisp_sub_device * asd)844 static void atomisp_dma_burst_len_cfg(struct atomisp_sub_device *asd)
845 {
846 	struct v4l2_mbus_framefmt *sink;
847 
848 	sink = atomisp_subdev_get_ffmt(&asd->subdev, NULL,
849 				       V4L2_SUBDEV_FORMAT_ACTIVE,
850 				       ATOMISP_SUBDEV_PAD_SINK);
851 
852 	if (sink->width * sink->height >= 4096 * 3072)
853 		atomisp_css2_hw_store_32(DMA_BURST_SIZE_REG, 0x7F);
854 	else
855 		atomisp_css2_hw_store_32(DMA_BURST_SIZE_REG, 0x00);
856 }
857 
atomisp_stop_stream(struct atomisp_video_pipe * pipe,bool stop_sensor,enum vb2_buffer_state state)858 static void atomisp_stop_stream(struct atomisp_video_pipe *pipe,
859 				bool stop_sensor, enum vb2_buffer_state state)
860 {
861 	struct atomisp_sub_device *asd = pipe->asd;
862 	struct atomisp_device *isp = asd->isp;
863 	struct pci_dev *pdev = to_pci_dev(isp->dev);
864 	unsigned long flags;
865 	int ret;
866 
867 	spin_lock_irqsave(&isp->lock, flags);
868 	asd->streaming = false;
869 	spin_unlock_irqrestore(&isp->lock, flags);
870 
871 	atomisp_clear_css_buffer_counters(asd);
872 	atomisp_css_irq_enable(isp, IA_CSS_IRQ_INFO_CSS_RECEIVER_SOF, false);
873 
874 	atomisp_css_stop(asd, false);
875 
876 	atomisp_subdev_cleanup_pending_events(asd);
877 
878 	if (stop_sensor) {
879 		ret = v4l2_subdev_call(isp->inputs[asd->input_curr].csi_remote_source,
880 				       video, s_stream, 0);
881 		if (ret)
882 			dev_warn(isp->dev, "Stopping sensor stream failed: %d\n", ret);
883 	}
884 
885 	/* Disable the CSI interface on ANN B0/K0 */
886 	if (isp->media_dev.hw_revision >= ((ATOMISP_HW_REVISION_ISP2401 <<
887 					    ATOMISP_HW_REVISION_SHIFT) | ATOMISP_HW_STEPPING_B0)) {
888 		pci_write_config_word(pdev, MRFLD_PCI_CSI_CONTROL,
889 				      isp->saved_regs.csi_control & ~MRFLD_PCI_CSI_CONTROL_CSI_READY);
890 	}
891 
892 	if (atomisp_freq_scaling(isp, ATOMISP_DFS_MODE_LOW, false))
893 		dev_warn(isp->dev, "DFS failed.\n");
894 
895 	/*
896 	 * ISP work around, need to reset ISP to allow next stream on to work.
897 	 * Streams have already been destroyed by atomisp_css_stop().
898 	 * Disable PUNIT/ISP acknowledge/handshake - SRSE=3 and then reset.
899 	 */
900 	pci_write_config_dword(pdev, PCI_I_CONTROL,
901 			       isp->saved_regs.i_control | MRFLD_PCI_I_CONTROL_SRSE_RESET_MASK);
902 	atomisp_reset(isp);
903 
904 	atomisp_flush_video_pipe(pipe, state, false);
905 
906 	/* Streams were destroyed by atomisp_css_stop(), recreate them. */
907 	ret = atomisp_create_pipes_stream(&isp->asd);
908 	if (ret)
909 		dev_warn(isp->dev, "Recreating streams failed: %d\n", ret);
910 
911 	media_pipeline_stop(&asd->video_out.vdev.entity.pads[0]);
912 }
913 
atomisp_start_streaming(struct vb2_queue * vq,unsigned int count)914 int atomisp_start_streaming(struct vb2_queue *vq, unsigned int count)
915 {
916 	struct atomisp_video_pipe *pipe = vq_to_pipe(vq);
917 	struct atomisp_sub_device *asd = pipe->asd;
918 	struct atomisp_device *isp = asd->isp;
919 	struct pci_dev *pdev = to_pci_dev(isp->dev);
920 	unsigned long irqflags;
921 	int ret;
922 
923 	dev_dbg(isp->dev, "Start stream\n");
924 
925 	mutex_lock(&isp->mutex);
926 
927 	ret = atomisp_pipe_check(pipe, false);
928 	if (ret) {
929 		atomisp_flush_video_pipe(pipe, VB2_BUF_STATE_QUEUED, true);
930 		goto out_unlock;
931 	}
932 
933 	/*
934 	 * When running a classic v4l2 app after a media-controller aware
935 	 * app, the CSI-receiver -> ISP link for the current sensor may be
936 	 * disabled. Fix this up before marking the pipeline as started.
937 	 */
938 	mutex_lock(&isp->media_dev.graph_mutex);
939 	atomisp_setup_input_links(isp);
940 	ret = __media_pipeline_start(&asd->video_out.vdev.entity.pads[0], &asd->video_out.pipe);
941 	mutex_unlock(&isp->media_dev.graph_mutex);
942 	if (ret) {
943 		dev_err(isp->dev, "Error starting mc pipeline: %d\n", ret);
944 		atomisp_flush_video_pipe(pipe, VB2_BUF_STATE_QUEUED, true);
945 		goto out_unlock;
946 	}
947 
948 	/* Input system HW workaround */
949 	atomisp_dma_burst_len_cfg(asd);
950 
951 	/* Invalidate caches. FIXME: should flush only necessary buffers */
952 	wbinvd();
953 
954 	if (asd->params.css_update_params_needed) {
955 		atomisp_apply_css_parameters(asd, &asd->params.css_param);
956 		if (asd->params.css_param.update_flag.dz_config)
957 			asd->params.config.dz_config = &asd->params.css_param.dz_config;
958 		atomisp_css_update_isp_params(asd);
959 		asd->params.css_update_params_needed = false;
960 		memset(&asd->params.css_param.update_flag, 0,
961 		       sizeof(struct atomisp_parameters));
962 	}
963 	asd->params.dvs_6axis = NULL;
964 
965 	ret = atomisp_css_start(asd);
966 	if (ret) {
967 		atomisp_flush_video_pipe(pipe, VB2_BUF_STATE_QUEUED, true);
968 		media_pipeline_stop(&asd->video_out.vdev.entity.pads[0]);
969 		goto out_unlock;
970 	}
971 
972 	spin_lock_irqsave(&isp->lock, irqflags);
973 	asd->streaming = true;
974 	spin_unlock_irqrestore(&isp->lock, irqflags);
975 	atomic_set(&asd->sof_count, 0);
976 	atomic_set(&asd->sequence, 0);
977 	atomic_set(&asd->sequence_temp, 0);
978 
979 	asd->params.dis_proj_data_valid = false;
980 	asd->latest_preview_exp_id = 0;
981 	asd->postview_exp_id = 1;
982 	asd->preview_exp_id = 1;
983 
984 	/* handle per_frame_setting parameter and buffers */
985 	atomisp_handle_parameter_and_buffer(pipe);
986 
987 	atomisp_qbuffers_to_css(asd);
988 
989 	atomisp_css_irq_enable(isp, IA_CSS_IRQ_INFO_CSS_RECEIVER_SOF,
990 			       atomisp_css_valid_sof(isp));
991 	atomisp_csi2_configure(asd);
992 
993 	if (atomisp_freq_scaling(isp, ATOMISP_DFS_MODE_AUTO, false) < 0)
994 		dev_dbg(isp->dev, "DFS auto mode failed!\n");
995 
996 	/* Enable the CSI interface on ANN B0/K0 */
997 	if (isp->media_dev.hw_revision >= ((ATOMISP_HW_REVISION_ISP2401 <<
998 					    ATOMISP_HW_REVISION_SHIFT) | ATOMISP_HW_STEPPING_B0)) {
999 		pci_write_config_word(pdev, MRFLD_PCI_CSI_CONTROL,
1000 				      isp->saved_regs.csi_control | MRFLD_PCI_CSI_CONTROL_CSI_READY);
1001 	}
1002 
1003 	/* stream on the sensor */
1004 	ret = v4l2_subdev_call(isp->inputs[asd->input_curr].csi_remote_source,
1005 			       video, s_stream, 1);
1006 	if (ret) {
1007 		dev_err(isp->dev, "Starting sensor stream failed: %d\n", ret);
1008 		atomisp_stop_stream(pipe, false, VB2_BUF_STATE_QUEUED);
1009 	}
1010 
1011 out_unlock:
1012 	mutex_unlock(&isp->mutex);
1013 	return ret;
1014 }
1015 
atomisp_stop_streaming(struct vb2_queue * vq)1016 void atomisp_stop_streaming(struct vb2_queue *vq)
1017 {
1018 	struct atomisp_video_pipe *pipe = vq_to_pipe(vq);
1019 	struct atomisp_device *isp = pipe->asd->isp;
1020 
1021 	dev_dbg(isp->dev, "Stop stream\n");
1022 
1023 	mutex_lock(&isp->mutex);
1024 	atomisp_stop_stream(pipe, true, VB2_BUF_STATE_ERROR);
1025 	mutex_unlock(&isp->mutex);
1026 }
1027 
1028 /*
1029  * To get the current value of a control.
1030  * applications initialize the id field of a struct v4l2_control and
1031  * call this ioctl with a pointer to this structure
1032  */
atomisp_g_ctrl(struct file * file,void * fh,struct v4l2_control * control)1033 static int atomisp_g_ctrl(struct file *file, void *fh,
1034 			  struct v4l2_control *control)
1035 {
1036 	struct video_device *vdev = video_devdata(file);
1037 	struct atomisp_sub_device *asd = atomisp_to_video_pipe(vdev)->asd;
1038 	int i, ret = -EINVAL;
1039 
1040 	for (i = 0; i < ctrls_num; i++) {
1041 		if (ci_v4l2_controls[i].id == control->id) {
1042 			ret = 0;
1043 			break;
1044 		}
1045 	}
1046 
1047 	if (ret)
1048 		return ret;
1049 
1050 	switch (control->id) {
1051 	case V4L2_CID_COLORFX:
1052 		ret = atomisp_color_effect(asd, 0, &control->value);
1053 		break;
1054 	case V4L2_CID_ATOMISP_BAD_PIXEL_DETECTION:
1055 		ret = atomisp_bad_pixel(asd, 0, &control->value);
1056 		break;
1057 	case V4L2_CID_ATOMISP_POSTPROCESS_GDC_CAC:
1058 		ret = atomisp_gdc_cac(asd, 0, &control->value);
1059 		break;
1060 	case V4L2_CID_ATOMISP_VIDEO_STABLIZATION:
1061 		ret = atomisp_video_stable(asd, 0, &control->value);
1062 		break;
1063 	case V4L2_CID_ATOMISP_FIXED_PATTERN_NR:
1064 		ret = atomisp_fixed_pattern(asd, 0, &control->value);
1065 		break;
1066 	case V4L2_CID_ATOMISP_FALSE_COLOR_CORRECTION:
1067 		ret = atomisp_false_color(asd, 0, &control->value);
1068 		break;
1069 	case V4L2_CID_ATOMISP_LOW_LIGHT:
1070 		ret = atomisp_low_light(asd, 0, &control->value);
1071 		break;
1072 	default:
1073 		ret = -EINVAL;
1074 		break;
1075 	}
1076 
1077 	return ret;
1078 }
1079 
1080 /*
1081  * To change the value of a control.
1082  * applications initialize the id and value fields of a struct v4l2_control
1083  * and call this ioctl.
1084  */
atomisp_s_ctrl(struct file * file,void * fh,struct v4l2_control * control)1085 static int atomisp_s_ctrl(struct file *file, void *fh,
1086 			  struct v4l2_control *control)
1087 {
1088 	struct video_device *vdev = video_devdata(file);
1089 	struct atomisp_sub_device *asd = atomisp_to_video_pipe(vdev)->asd;
1090 	int i, ret = -EINVAL;
1091 
1092 	for (i = 0; i < ctrls_num; i++) {
1093 		if (ci_v4l2_controls[i].id == control->id) {
1094 			ret = 0;
1095 			break;
1096 		}
1097 	}
1098 
1099 	if (ret)
1100 		return ret;
1101 
1102 	switch (control->id) {
1103 	case V4L2_CID_COLORFX:
1104 		ret = atomisp_color_effect(asd, 1, &control->value);
1105 		break;
1106 	case V4L2_CID_ATOMISP_BAD_PIXEL_DETECTION:
1107 		ret = atomisp_bad_pixel(asd, 1, &control->value);
1108 		break;
1109 	case V4L2_CID_ATOMISP_POSTPROCESS_GDC_CAC:
1110 		ret = atomisp_gdc_cac(asd, 1, &control->value);
1111 		break;
1112 	case V4L2_CID_ATOMISP_VIDEO_STABLIZATION:
1113 		ret = atomisp_video_stable(asd, 1, &control->value);
1114 		break;
1115 	case V4L2_CID_ATOMISP_FIXED_PATTERN_NR:
1116 		ret = atomisp_fixed_pattern(asd, 1, &control->value);
1117 		break;
1118 	case V4L2_CID_ATOMISP_FALSE_COLOR_CORRECTION:
1119 		ret = atomisp_false_color(asd, 1, &control->value);
1120 		break;
1121 	case V4L2_CID_ATOMISP_LOW_LIGHT:
1122 		ret = atomisp_low_light(asd, 1, &control->value);
1123 		break;
1124 	default:
1125 		ret = -EINVAL;
1126 		break;
1127 	}
1128 	return ret;
1129 }
1130 
1131 /*
1132  * To query the attributes of a control.
1133  * applications set the id field of a struct v4l2_query_ext_ctrl and call the
1134  * this ioctl with a pointer to this structure. The driver fills
1135  * the rest of the structure.
1136  */
atomisp_query_ext_ctrl(struct file * file,void * fh,struct v4l2_query_ext_ctrl * qc)1137 static int atomisp_query_ext_ctrl(struct file *file, void *fh,
1138 				  struct v4l2_query_ext_ctrl *qc)
1139 {
1140 	int i;
1141 
1142 	/* TODO: implement V4L2_CTRL_FLAG_NEXT_CTRL */
1143 	if (qc->id & V4L2_CTRL_FLAG_NEXT_CTRL)
1144 		return -EINVAL;
1145 
1146 	for (i = 0; i < ctrls_num; i++) {
1147 		if (ci_v4l2_controls[i].id == qc->id) {
1148 			*qc = ci_v4l2_controls[i];
1149 			qc->elems = 1;
1150 			qc->elem_size = 4;
1151 			return 0;
1152 		}
1153 	}
1154 
1155 	/*
1156 	 * This is probably not needed, but this flag has been set for
1157 	 * many kernel versions. Leave it to avoid breaking any apps.
1158 	 */
1159 	qc->flags = V4L2_CTRL_FLAG_DISABLED;
1160 	return -EINVAL;
1161 }
1162 
atomisp_camera_g_ext_ctrls(struct file * file,void * fh,struct v4l2_ext_controls * c)1163 static int atomisp_camera_g_ext_ctrls(struct file *file, void *fh,
1164 				      struct v4l2_ext_controls *c)
1165 {
1166 	struct video_device *vdev = video_devdata(file);
1167 	struct atomisp_sub_device *asd = atomisp_to_video_pipe(vdev)->asd;
1168 	struct v4l2_control ctrl;
1169 	int i;
1170 	int ret = 0;
1171 
1172 	for (i = 0; i < c->count; i++) {
1173 		ctrl.id = c->controls[i].id;
1174 		ctrl.value = c->controls[i].value;
1175 		switch (ctrl.id) {
1176 		case V4L2_CID_ZOOM_ABSOLUTE:
1177 			ret = atomisp_digital_zoom(asd, 0, &ctrl.value);
1178 			break;
1179 		default:
1180 			ret = -EINVAL;
1181 		}
1182 
1183 		if (ret) {
1184 			c->error_idx = i;
1185 			break;
1186 		}
1187 		c->controls[i].value = ctrl.value;
1188 	}
1189 	return ret;
1190 }
1191 
1192 /* 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)1193 static int atomisp_g_ext_ctrls(struct file *file, void *fh,
1194 			       struct v4l2_ext_controls *c)
1195 {
1196 	struct v4l2_control ctrl;
1197 	int i, ret = 0;
1198 
1199 	/*
1200 	 * input_lock is not need for the Camera related IOCTLs
1201 	 * The input_lock downgrade the FPS of 3A
1202 	 */
1203 	ret = atomisp_camera_g_ext_ctrls(file, fh, c);
1204 	if (ret != -EINVAL)
1205 		return ret;
1206 
1207 	for (i = 0; i < c->count; i++) {
1208 		ctrl.id = c->controls[i].id;
1209 		ctrl.value = c->controls[i].value;
1210 		ret = atomisp_g_ctrl(file, fh, &ctrl);
1211 		c->controls[i].value = ctrl.value;
1212 		if (ret) {
1213 			c->error_idx = i;
1214 			break;
1215 		}
1216 	}
1217 	return ret;
1218 }
1219 
atomisp_camera_s_ext_ctrls(struct file * file,void * fh,struct v4l2_ext_controls * c)1220 static int atomisp_camera_s_ext_ctrls(struct file *file, void *fh,
1221 				      struct v4l2_ext_controls *c)
1222 {
1223 	struct video_device *vdev = video_devdata(file);
1224 	struct atomisp_sub_device *asd = atomisp_to_video_pipe(vdev)->asd;
1225 	struct v4l2_control ctrl;
1226 	int i;
1227 	int ret = 0;
1228 
1229 	for (i = 0; i < c->count; i++) {
1230 		struct v4l2_ctrl *ctr;
1231 
1232 		ctrl.id = c->controls[i].id;
1233 		ctrl.value = c->controls[i].value;
1234 		switch (ctrl.id) {
1235 		case V4L2_CID_ZOOM_ABSOLUTE:
1236 			ret = atomisp_digital_zoom(asd, 1, &ctrl.value);
1237 			break;
1238 		default:
1239 			ctr = v4l2_ctrl_find(&asd->ctrl_handler, ctrl.id);
1240 			if (ctr)
1241 				ret = v4l2_ctrl_s_ctrl(ctr, ctrl.value);
1242 			else
1243 				ret = -EINVAL;
1244 		}
1245 
1246 		if (ret) {
1247 			c->error_idx = i;
1248 			break;
1249 		}
1250 		c->controls[i].value = ctrl.value;
1251 	}
1252 	return ret;
1253 }
1254 
1255 /* 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)1256 static int atomisp_s_ext_ctrls(struct file *file, void *fh,
1257 			       struct v4l2_ext_controls *c)
1258 {
1259 	struct v4l2_control ctrl;
1260 	int i, ret = 0;
1261 
1262 	/*
1263 	 * input_lock is not need for the Camera related IOCTLs
1264 	 * The input_lock downgrade the FPS of 3A
1265 	 */
1266 	ret = atomisp_camera_s_ext_ctrls(file, fh, c);
1267 	if (ret != -EINVAL)
1268 		return ret;
1269 
1270 	for (i = 0; i < c->count; i++) {
1271 		ctrl.id = c->controls[i].id;
1272 		ctrl.value = c->controls[i].value;
1273 		ret = atomisp_s_ctrl(file, fh, &ctrl);
1274 		c->controls[i].value = ctrl.value;
1275 		if (ret) {
1276 			c->error_idx = i;
1277 			break;
1278 		}
1279 	}
1280 	return ret;
1281 }
1282 
1283 /*
1284  * vidioc_g/s_param are used to switch isp running mode
1285  */
atomisp_g_parm(struct file * file,void * fh,struct v4l2_streamparm * parm)1286 static int atomisp_g_parm(struct file *file, void *fh,
1287 			  struct v4l2_streamparm *parm)
1288 {
1289 	struct video_device *vdev = video_devdata(file);
1290 	struct atomisp_sub_device *asd = atomisp_to_video_pipe(vdev)->asd;
1291 	struct atomisp_device *isp = video_get_drvdata(vdev);
1292 
1293 	if (parm->type != V4L2_BUF_TYPE_VIDEO_CAPTURE) {
1294 		dev_err(isp->dev, "unsupported v4l2 buf type\n");
1295 		return -EINVAL;
1296 	}
1297 
1298 	parm->parm.capture.capturemode = asd->run_mode->val;
1299 
1300 	return 0;
1301 }
1302 
atomisp_s_parm(struct file * file,void * fh,struct v4l2_streamparm * parm)1303 static int atomisp_s_parm(struct file *file, void *fh,
1304 			  struct v4l2_streamparm *parm)
1305 {
1306 	struct video_device *vdev = video_devdata(file);
1307 	struct atomisp_device *isp = video_get_drvdata(vdev);
1308 	struct atomisp_sub_device *asd = atomisp_to_video_pipe(vdev)->asd;
1309 	int mode;
1310 	int rval;
1311 	int fps;
1312 
1313 	if (parm->type != V4L2_BUF_TYPE_VIDEO_CAPTURE) {
1314 		dev_err(isp->dev, "unsupported v4l2 buf type\n");
1315 		return -EINVAL;
1316 	}
1317 
1318 	asd->high_speed_mode = false;
1319 	switch (parm->parm.capture.capturemode) {
1320 	case CI_MODE_NONE: {
1321 		struct v4l2_subdev_frame_interval fi = {0};
1322 
1323 		fi.interval = parm->parm.capture.timeperframe;
1324 
1325 		rval = v4l2_subdev_call_state_active(isp->inputs[asd->input_curr].csi_remote_source,
1326 						     pad, set_frame_interval, &fi);
1327 		if (!rval)
1328 			parm->parm.capture.timeperframe = fi.interval;
1329 
1330 		if (fi.interval.numerator != 0) {
1331 			fps = fi.interval.denominator / fi.interval.numerator;
1332 			if (fps > 30)
1333 				asd->high_speed_mode = true;
1334 		}
1335 
1336 		return rval == -ENOIOCTLCMD ? 0 : rval;
1337 	}
1338 	case CI_MODE_VIDEO:
1339 		mode = ATOMISP_RUN_MODE_VIDEO;
1340 		break;
1341 	case CI_MODE_STILL_CAPTURE:
1342 		mode = ATOMISP_RUN_MODE_STILL_CAPTURE;
1343 		break;
1344 	case CI_MODE_PREVIEW:
1345 		mode = ATOMISP_RUN_MODE_PREVIEW;
1346 		break;
1347 	default:
1348 		return -EINVAL;
1349 	}
1350 
1351 	rval = v4l2_ctrl_s_ctrl(asd->run_mode, mode);
1352 
1353 	return rval == -ENOIOCTLCMD ? 0 : rval;
1354 }
1355 
atomisp_vidioc_default(struct file * file,void * fh,bool valid_prio,unsigned int cmd,void * arg)1356 static long atomisp_vidioc_default(struct file *file, void *fh,
1357 				   bool valid_prio, unsigned int cmd, void *arg)
1358 {
1359 	struct video_device *vdev = video_devdata(file);
1360 	struct atomisp_sub_device *asd = atomisp_to_video_pipe(vdev)->asd;
1361 	int err;
1362 
1363 	switch (cmd) {
1364 	case ATOMISP_IOC_G_XNR:
1365 		err = atomisp_xnr(asd, 0, arg);
1366 		break;
1367 
1368 	case ATOMISP_IOC_S_XNR:
1369 		err = atomisp_xnr(asd, 1, arg);
1370 		break;
1371 
1372 	case ATOMISP_IOC_G_NR:
1373 		err = atomisp_nr(asd, 0, arg);
1374 		break;
1375 
1376 	case ATOMISP_IOC_S_NR:
1377 		err = atomisp_nr(asd, 1, arg);
1378 		break;
1379 
1380 	case ATOMISP_IOC_G_TNR:
1381 		err = atomisp_tnr(asd, 0, arg);
1382 		break;
1383 
1384 	case ATOMISP_IOC_S_TNR:
1385 		err = atomisp_tnr(asd, 1, arg);
1386 		break;
1387 
1388 	case ATOMISP_IOC_G_BLACK_LEVEL_COMP:
1389 		err = atomisp_black_level(asd, 0, arg);
1390 		break;
1391 
1392 	case ATOMISP_IOC_S_BLACK_LEVEL_COMP:
1393 		err = atomisp_black_level(asd, 1, arg);
1394 		break;
1395 
1396 	case ATOMISP_IOC_G_EE:
1397 		err = atomisp_ee(asd, 0, arg);
1398 		break;
1399 
1400 	case ATOMISP_IOC_S_EE:
1401 		err = atomisp_ee(asd, 1, arg);
1402 		break;
1403 
1404 	case ATOMISP_IOC_G_DIS_STAT:
1405 		err = atomisp_get_dis_stat(asd, arg);
1406 		break;
1407 
1408 	case ATOMISP_IOC_G_DVS2_BQ_RESOLUTIONS:
1409 		err = atomisp_get_dvs2_bq_resolutions(asd, arg);
1410 		break;
1411 
1412 	case ATOMISP_IOC_S_DIS_COEFS:
1413 		err = atomisp_css_cp_dvs2_coefs(asd, arg,
1414 						&asd->params.css_param, true);
1415 		if (!err && arg)
1416 			asd->params.css_update_params_needed = true;
1417 		break;
1418 
1419 	case ATOMISP_IOC_S_DIS_VECTOR:
1420 		err = atomisp_cp_dvs_6axis_config(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_G_ISP_PARM:
1427 		err = atomisp_param(asd, 0, arg);
1428 		break;
1429 
1430 	case ATOMISP_IOC_S_ISP_PARM:
1431 		err = atomisp_param(asd, 1, arg);
1432 		break;
1433 
1434 	case ATOMISP_IOC_G_3A_STAT:
1435 		err = atomisp_3a_stat(asd, 0, arg);
1436 		break;
1437 
1438 	case ATOMISP_IOC_G_ISP_GAMMA:
1439 		err = atomisp_gamma(asd, 0, arg);
1440 		break;
1441 
1442 	case ATOMISP_IOC_S_ISP_GAMMA:
1443 		err = atomisp_gamma(asd, 1, arg);
1444 		break;
1445 
1446 	case ATOMISP_IOC_G_ISP_GDC_TAB:
1447 		err = atomisp_gdc_cac_table(asd, 0, arg);
1448 		break;
1449 
1450 	case ATOMISP_IOC_S_ISP_GDC_TAB:
1451 		err = atomisp_gdc_cac_table(asd, 1, arg);
1452 		break;
1453 
1454 	case ATOMISP_IOC_G_ISP_MACC:
1455 		err = atomisp_macc_table(asd, 0, arg);
1456 		break;
1457 
1458 	case ATOMISP_IOC_S_ISP_MACC:
1459 		err = atomisp_macc_table(asd, 1, arg);
1460 		break;
1461 
1462 	case ATOMISP_IOC_G_ISP_BAD_PIXEL_DETECTION:
1463 		err = atomisp_bad_pixel_param(asd, 0, arg);
1464 		break;
1465 
1466 	case ATOMISP_IOC_S_ISP_BAD_PIXEL_DETECTION:
1467 		err = atomisp_bad_pixel_param(asd, 1, arg);
1468 		break;
1469 
1470 	case ATOMISP_IOC_G_ISP_FALSE_COLOR_CORRECTION:
1471 		err = atomisp_false_color_param(asd, 0, arg);
1472 		break;
1473 
1474 	case ATOMISP_IOC_S_ISP_FALSE_COLOR_CORRECTION:
1475 		err = atomisp_false_color_param(asd, 1, arg);
1476 		break;
1477 
1478 	case ATOMISP_IOC_G_ISP_CTC:
1479 		err = atomisp_ctc(asd, 0, arg);
1480 		break;
1481 
1482 	case ATOMISP_IOC_S_ISP_CTC:
1483 		err = atomisp_ctc(asd, 1, arg);
1484 		break;
1485 
1486 	case ATOMISP_IOC_G_ISP_WHITE_BALANCE:
1487 		err = atomisp_white_balance_param(asd, 0, arg);
1488 		break;
1489 
1490 	case ATOMISP_IOC_S_ISP_WHITE_BALANCE:
1491 		err = atomisp_white_balance_param(asd, 1, arg);
1492 		break;
1493 
1494 	case ATOMISP_IOC_G_3A_CONFIG:
1495 		err = atomisp_3a_config_param(asd, 0, arg);
1496 		break;
1497 
1498 	case ATOMISP_IOC_S_3A_CONFIG:
1499 		err = atomisp_3a_config_param(asd, 1, arg);
1500 		break;
1501 
1502 	case ATOMISP_IOC_S_ISP_FPN_TABLE:
1503 		err = atomisp_fixed_pattern_table(asd, arg);
1504 		break;
1505 
1506 	case ATOMISP_IOC_S_ISP_SHD_TAB:
1507 		err = atomisp_set_shading_table(asd, arg);
1508 		break;
1509 
1510 	case ATOMISP_IOC_G_ISP_GAMMA_CORRECTION:
1511 		err = atomisp_gamma_correction(asd, 0, arg);
1512 		break;
1513 
1514 	case ATOMISP_IOC_S_ISP_GAMMA_CORRECTION:
1515 		err = atomisp_gamma_correction(asd, 1, arg);
1516 		break;
1517 
1518 	case ATOMISP_IOC_S_PARAMETERS:
1519 		err = atomisp_set_parameters(vdev, arg);
1520 		break;
1521 
1522 	case ATOMISP_IOC_EXP_ID_UNLOCK:
1523 		err = atomisp_exp_id_unlock(asd, arg);
1524 		break;
1525 	case ATOMISP_IOC_EXP_ID_CAPTURE:
1526 		err = atomisp_exp_id_capture(asd, arg);
1527 		break;
1528 	case ATOMISP_IOC_S_ENABLE_DZ_CAPT_PIPE:
1529 		err = atomisp_enable_dz_capt_pipe(asd, arg);
1530 		break;
1531 	case ATOMISP_IOC_G_FORMATS_CONFIG:
1532 		err = atomisp_formats(asd, 0, arg);
1533 		break;
1534 
1535 	case ATOMISP_IOC_S_FORMATS_CONFIG:
1536 		err = atomisp_formats(asd, 1, arg);
1537 		break;
1538 	case ATOMISP_IOC_INJECT_A_FAKE_EVENT:
1539 		err = atomisp_inject_a_fake_event(asd, arg);
1540 		break;
1541 	case ATOMISP_IOC_S_ARRAY_RESOLUTION:
1542 		err = atomisp_set_array_res(asd, arg);
1543 		break;
1544 	default:
1545 		err = -EINVAL;
1546 		break;
1547 	}
1548 
1549 	return err;
1550 }
1551 
1552 const struct v4l2_ioctl_ops atomisp_ioctl_ops = {
1553 	.vidioc_querycap = atomisp_querycap,
1554 	.vidioc_enum_input = atomisp_enum_input,
1555 	.vidioc_g_input = atomisp_g_input,
1556 	.vidioc_s_input = atomisp_s_input,
1557 	.vidioc_query_ext_ctrl = atomisp_query_ext_ctrl,
1558 	.vidioc_s_ext_ctrls = atomisp_s_ext_ctrls,
1559 	.vidioc_g_ext_ctrls = atomisp_g_ext_ctrls,
1560 	.vidioc_enum_framesizes   = atomisp_enum_framesizes,
1561 	.vidioc_enum_frameintervals = atomisp_enum_frameintervals,
1562 	.vidioc_enum_fmt_vid_cap = atomisp_enum_fmt_cap,
1563 	.vidioc_try_fmt_vid_cap = atomisp_try_fmt_cap,
1564 	.vidioc_g_fmt_vid_cap = atomisp_g_fmt_cap,
1565 	.vidioc_s_fmt_vid_cap = atomisp_s_fmt_cap,
1566 	.vidioc_reqbufs = vb2_ioctl_reqbufs,
1567 	.vidioc_querybuf = vb2_ioctl_querybuf,
1568 	.vidioc_qbuf = atomisp_qbuf_wrapper,
1569 	.vidioc_dqbuf = atomisp_dqbuf_wrapper,
1570 	.vidioc_expbuf = vb2_ioctl_expbuf,
1571 	.vidioc_streamon = vb2_ioctl_streamon,
1572 	.vidioc_streamoff = vb2_ioctl_streamoff,
1573 	.vidioc_default = atomisp_vidioc_default,
1574 	.vidioc_s_parm = atomisp_s_parm,
1575 	.vidioc_g_parm = atomisp_g_parm,
1576 };
1577