1 // SPDX-License-Identifier: GPL-2.0
3 * Support for Medifield PNW Camera Imaging ISP subsystem.
5 * Copyright (c) 2010 Intel Corporation. All Rights Reserved.
7 * Copyright (c) 2010 Silicon Hive www.siliconhive.com.
9 * This program is free software; you can redistribute it and/or
10 * modify it under the terms of the GNU General Public License version
11 * 2 as published by the Free Software Foundation.
13 * This program is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 * GNU General Public License for more details.
21 #include <linux/delay.h>
22 #include <linux/pci.h>
24 #include <media/v4l2-ioctl.h>
25 #include <media/v4l2-event.h>
27 #include "atomisp_cmd.h"
28 #include "atomisp_common.h"
29 #include "atomisp_fops.h"
30 #include "atomisp_internal.h"
31 #include "atomisp_ioctl.h"
32 #include "atomisp-regs.h"
33 #include "atomisp_compat.h"
35 #include "sh_css_hrt.h"
37 #include "gp_device.h"
38 #include "device_access.h"
41 static const char *DRIVER = "atomisp"; /* max size 15 */
42 static const char *CARD = "ATOM ISP"; /* max size 31 */
45 * FIXME: ISP should not know beforehand all CIDs supported by sensor.
46 * Instead, it needs to propagate to sensor unkonwn CIDs.
48 static struct v4l2_queryctrl ci_v4l2_controls[] = {
50 .id = V4L2_CID_AUTO_WHITE_BALANCE,
51 .type = V4L2_CTRL_TYPE_BOOLEAN,
52 .name = "Automatic White Balance",
59 .id = V4L2_CID_RED_BALANCE,
60 .type = V4L2_CTRL_TYPE_INTEGER,
61 .name = "Red Balance",
65 .default_value = 0x00,
68 .id = V4L2_CID_BLUE_BALANCE,
69 .type = V4L2_CTRL_TYPE_INTEGER,
70 .name = "Blue Balance",
74 .default_value = 0x00,
78 .type = V4L2_CTRL_TYPE_INTEGER,
83 .default_value = 0x00,
86 .id = V4L2_CID_POWER_LINE_FREQUENCY,
87 .type = V4L2_CTRL_TYPE_MENU,
88 .name = "Light frequency filter",
95 .id = V4L2_CID_COLORFX,
96 .type = V4L2_CTRL_TYPE_INTEGER,
97 .name = "Image Color Effect",
104 .id = V4L2_CID_COLORFX_CBCR,
105 .type = V4L2_CTRL_TYPE_INTEGER,
106 .name = "Image Color Effect CbCr",
113 .id = V4L2_CID_ATOMISP_BAD_PIXEL_DETECTION,
114 .type = V4L2_CTRL_TYPE_INTEGER,
115 .name = "Bad Pixel Correction",
122 .id = V4L2_CID_ATOMISP_POSTPROCESS_GDC_CAC,
123 .type = V4L2_CTRL_TYPE_INTEGER,
131 .id = V4L2_CID_ATOMISP_VIDEO_STABLIZATION,
132 .type = V4L2_CTRL_TYPE_INTEGER,
133 .name = "Video Stablization",
140 .id = V4L2_CID_ATOMISP_FIXED_PATTERN_NR,
141 .type = V4L2_CTRL_TYPE_INTEGER,
142 .name = "Fixed Pattern Noise Reduction",
149 .id = V4L2_CID_ATOMISP_FALSE_COLOR_CORRECTION,
150 .type = V4L2_CTRL_TYPE_INTEGER,
151 .name = "False Color Correction",
158 .id = V4L2_CID_REQUEST_FLASH,
159 .type = V4L2_CTRL_TYPE_INTEGER,
160 .name = "Request flash frames",
167 .id = V4L2_CID_ATOMISP_LOW_LIGHT,
168 .type = V4L2_CTRL_TYPE_BOOLEAN,
169 .name = "Low light mode",
176 .id = V4L2_CID_2A_STATUS,
177 .type = V4L2_CTRL_TYPE_BITMASK,
178 .name = "AE and AWB status",
180 .maximum = V4L2_2A_STATUS_AE_READY | V4L2_2A_STATUS_AWB_READY,
185 .id = V4L2_CID_EXPOSURE,
186 .type = V4L2_CTRL_TYPE_INTEGER,
194 .id = V4L2_CID_EXPOSURE_ZONE_NUM,
195 .type = V4L2_CTRL_TYPE_INTEGER,
196 .name = "one-time exposure zone number",
203 .id = V4L2_CID_EXPOSURE_AUTO_PRIORITY,
204 .type = V4L2_CTRL_TYPE_INTEGER,
205 .name = "Exposure auto priority",
206 .minimum = V4L2_EXPOSURE_AUTO,
207 .maximum = V4L2_EXPOSURE_APERTURE_PRIORITY,
209 .default_value = V4L2_EXPOSURE_AUTO,
212 .id = V4L2_CID_SCENE_MODE,
213 .type = V4L2_CTRL_TYPE_INTEGER,
214 .name = "scene mode",
221 .id = V4L2_CID_ISO_SENSITIVITY,
222 .type = V4L2_CTRL_TYPE_INTEGER,
230 .id = V4L2_CID_ISO_SENSITIVITY_AUTO,
231 .type = V4L2_CTRL_TYPE_INTEGER,
233 .minimum = V4L2_ISO_SENSITIVITY_MANUAL,
234 .maximum = V4L2_ISO_SENSITIVITY_AUTO,
236 .default_value = V4L2_ISO_SENSITIVITY_AUTO,
239 .id = V4L2_CID_AUTO_N_PRESET_WHITE_BALANCE,
240 .type = V4L2_CTRL_TYPE_INTEGER,
241 .name = "white balance",
248 .id = V4L2_CID_EXPOSURE_METERING,
249 .type = V4L2_CTRL_TYPE_MENU,
257 .id = V4L2_CID_3A_LOCK,
258 .type = V4L2_CTRL_TYPE_BITMASK,
261 .maximum = V4L2_LOCK_EXPOSURE | V4L2_LOCK_WHITE_BALANCE
267 .id = V4L2_CID_TEST_PATTERN,
268 .type = V4L2_CTRL_TYPE_INTEGER,
269 .name = "Test Pattern",
276 .id = V4L2_CID_TEST_PATTERN_COLOR_R,
277 .type = V4L2_CTRL_TYPE_INTEGER,
278 .name = "Test Pattern Solid Color R",
285 .id = V4L2_CID_TEST_PATTERN_COLOR_GR,
286 .type = V4L2_CTRL_TYPE_INTEGER,
287 .name = "Test Pattern Solid Color GR",
294 .id = V4L2_CID_TEST_PATTERN_COLOR_GB,
295 .type = V4L2_CTRL_TYPE_INTEGER,
296 .name = "Test Pattern Solid Color GB",
303 .id = V4L2_CID_TEST_PATTERN_COLOR_B,
304 .type = V4L2_CTRL_TYPE_INTEGER,
305 .name = "Test Pattern Solid Color B",
313 static const u32 ctrls_num = ARRAY_SIZE(ci_v4l2_controls);
316 * supported V4L2 fmts and resolutions
318 const struct atomisp_format_bridge atomisp_output_fmts[] = {
320 .pixelformat = V4L2_PIX_FMT_YUV420,
322 .mbus_code = V4L2_MBUS_FMT_CUSTOM_YUV420,
323 .sh_fmt = IA_CSS_FRAME_FORMAT_YUV420,
324 .description = "YUV420, planar",
327 .pixelformat = V4L2_PIX_FMT_YVU420,
329 .mbus_code = V4L2_MBUS_FMT_CUSTOM_YVU420,
330 .sh_fmt = IA_CSS_FRAME_FORMAT_YV12,
331 .description = "YVU420, planar",
334 .pixelformat = V4L2_PIX_FMT_YUV422P,
336 .mbus_code = V4L2_MBUS_FMT_CUSTOM_YUV422P,
337 .sh_fmt = IA_CSS_FRAME_FORMAT_YUV422,
338 .description = "YUV422, planar",
341 .pixelformat = V4L2_PIX_FMT_YUV444,
343 .mbus_code = V4L2_MBUS_FMT_CUSTOM_YUV444,
344 .sh_fmt = IA_CSS_FRAME_FORMAT_YUV444,
345 .description = "YUV444"
347 .pixelformat = V4L2_PIX_FMT_NV12,
349 .mbus_code = V4L2_MBUS_FMT_CUSTOM_NV12,
350 .sh_fmt = IA_CSS_FRAME_FORMAT_NV12,
351 .description = "NV12, Y-plane, CbCr interleaved",
354 .pixelformat = V4L2_PIX_FMT_NV21,
356 .mbus_code = V4L2_MBUS_FMT_CUSTOM_NV21,
357 .sh_fmt = IA_CSS_FRAME_FORMAT_NV21,
358 .description = "NV21, Y-plane, CbCr interleaved",
361 .pixelformat = V4L2_PIX_FMT_NV16,
363 .mbus_code = V4L2_MBUS_FMT_CUSTOM_NV16,
364 .sh_fmt = IA_CSS_FRAME_FORMAT_NV16,
365 .description = "NV16, Y-plane, CbCr interleaved",
368 .pixelformat = V4L2_PIX_FMT_YUYV,
370 .mbus_code = V4L2_MBUS_FMT_CUSTOM_YUYV,
371 .sh_fmt = IA_CSS_FRAME_FORMAT_YUYV,
372 .description = "YUYV, interleaved"
374 .pixelformat = V4L2_PIX_FMT_UYVY,
376 .mbus_code = MEDIA_BUS_FMT_UYVY8_1X16,
377 .sh_fmt = IA_CSS_FRAME_FORMAT_UYVY,
378 .description = "UYVY, interleaved"
379 }, { /* This one is for parallel sensors! DO NOT USE! */
380 .pixelformat = V4L2_PIX_FMT_UYVY,
382 .mbus_code = MEDIA_BUS_FMT_UYVY8_2X8,
383 .sh_fmt = IA_CSS_FRAME_FORMAT_UYVY,
384 .description = "UYVY, interleaved"
386 .pixelformat = V4L2_PIX_FMT_SBGGR16,
388 .mbus_code = V4L2_MBUS_FMT_CUSTOM_SBGGR16,
389 .sh_fmt = IA_CSS_FRAME_FORMAT_RAW,
390 .description = "Bayer 16"
392 .pixelformat = V4L2_PIX_FMT_SBGGR8,
394 .mbus_code = MEDIA_BUS_FMT_SBGGR8_1X8,
395 .sh_fmt = IA_CSS_FRAME_FORMAT_RAW,
396 .description = "Bayer 8"
398 .pixelformat = V4L2_PIX_FMT_SGBRG8,
400 .mbus_code = MEDIA_BUS_FMT_SGBRG8_1X8,
401 .sh_fmt = IA_CSS_FRAME_FORMAT_RAW,
402 .description = "Bayer 8"
404 .pixelformat = V4L2_PIX_FMT_SGRBG8,
406 .mbus_code = MEDIA_BUS_FMT_SGRBG8_1X8,
407 .sh_fmt = IA_CSS_FRAME_FORMAT_RAW,
408 .description = "Bayer 8"
410 .pixelformat = V4L2_PIX_FMT_SRGGB8,
412 .mbus_code = MEDIA_BUS_FMT_SRGGB8_1X8,
413 .sh_fmt = IA_CSS_FRAME_FORMAT_RAW,
414 .description = "Bayer 8"
416 .pixelformat = V4L2_PIX_FMT_SBGGR10,
418 .mbus_code = MEDIA_BUS_FMT_SBGGR10_1X10,
419 .sh_fmt = IA_CSS_FRAME_FORMAT_RAW,
420 .description = "Bayer 10"
422 .pixelformat = V4L2_PIX_FMT_SGBRG10,
424 .mbus_code = MEDIA_BUS_FMT_SGBRG10_1X10,
425 .sh_fmt = IA_CSS_FRAME_FORMAT_RAW,
426 .description = "Bayer 10"
428 .pixelformat = V4L2_PIX_FMT_SGRBG10,
430 .mbus_code = MEDIA_BUS_FMT_SGRBG10_1X10,
431 .sh_fmt = IA_CSS_FRAME_FORMAT_RAW,
432 .description = "Bayer 10"
434 .pixelformat = V4L2_PIX_FMT_SRGGB10,
436 .mbus_code = MEDIA_BUS_FMT_SRGGB10_1X10,
437 .sh_fmt = IA_CSS_FRAME_FORMAT_RAW,
438 .description = "Bayer 10"
440 .pixelformat = V4L2_PIX_FMT_SBGGR12,
442 .mbus_code = MEDIA_BUS_FMT_SBGGR12_1X12,
443 .sh_fmt = IA_CSS_FRAME_FORMAT_RAW,
444 .description = "Bayer 12"
446 .pixelformat = V4L2_PIX_FMT_SGBRG12,
448 .mbus_code = MEDIA_BUS_FMT_SGBRG12_1X12,
449 .sh_fmt = IA_CSS_FRAME_FORMAT_RAW,
450 .description = "Bayer 12"
452 .pixelformat = V4L2_PIX_FMT_SGRBG12,
454 .mbus_code = MEDIA_BUS_FMT_SGRBG12_1X12,
455 .sh_fmt = IA_CSS_FRAME_FORMAT_RAW,
456 .description = "Bayer 12"
458 .pixelformat = V4L2_PIX_FMT_SRGGB12,
460 .mbus_code = MEDIA_BUS_FMT_SRGGB12_1X12,
461 .sh_fmt = IA_CSS_FRAME_FORMAT_RAW,
462 .description = "Bayer 12"
464 .pixelformat = V4L2_PIX_FMT_RGB32,
466 .mbus_code = V4L2_MBUS_FMT_CUSTOM_RGB32,
467 .sh_fmt = IA_CSS_FRAME_FORMAT_RGBA888,
468 .description = "32 RGB 8-8-8-8"
470 .pixelformat = V4L2_PIX_FMT_RGB565,
472 .mbus_code = MEDIA_BUS_FMT_BGR565_2X8_LE,
473 .sh_fmt = IA_CSS_FRAME_FORMAT_RGB565,
474 .description = "16 RGB 5-6-5"
477 .pixelformat = V4L2_PIX_FMT_JPEG,
479 .mbus_code = MEDIA_BUS_FMT_JPEG_1X8,
480 .sh_fmt = IA_CSS_FRAME_FORMAT_BINARY_8,
481 .description = "JPEG"
483 /* This is a custom format being used by M10MO to send the RAW data */
484 .pixelformat = V4L2_PIX_FMT_CUSTOM_M10MO_RAW,
486 .mbus_code = V4L2_MBUS_FMT_CUSTOM_M10MO_RAW,
487 .sh_fmt = IA_CSS_FRAME_FORMAT_BINARY_8,
488 .description = "Custom RAW for M10MO"
493 const struct atomisp_format_bridge *
494 atomisp_get_format_bridge(unsigned int pixelformat)
498 for (i = 0; i < ARRAY_SIZE(atomisp_output_fmts); i++) {
499 if (atomisp_output_fmts[i].pixelformat == pixelformat)
500 return &atomisp_output_fmts[i];
506 const struct atomisp_format_bridge *
507 atomisp_get_format_bridge_from_mbus(u32 mbus_code)
511 for (i = 0; i < ARRAY_SIZE(atomisp_output_fmts); i++) {
512 if (mbus_code == atomisp_output_fmts[i].mbus_code)
513 return &atomisp_output_fmts[i];
519 int atomisp_pipe_check(struct atomisp_video_pipe *pipe, bool settings_change)
521 lockdep_assert_held(&pipe->isp->mutex);
523 if (pipe->isp->isp_fatal_error)
526 if (settings_change && vb2_is_busy(&pipe->vb_queue)) {
527 dev_err(pipe->isp->dev, "Set fmt/input IOCTL while streaming\n");
536 * return ISP capabilities
538 static int atomisp_querycap(struct file *file, void *fh,
539 struct v4l2_capability *cap)
541 struct video_device *vdev = video_devdata(file);
542 struct atomisp_device *isp = video_get_drvdata(vdev);
544 strscpy(cap->driver, DRIVER, sizeof(cap->driver));
545 strscpy(cap->card, CARD, sizeof(cap->card));
546 snprintf(cap->bus_info, sizeof(cap->bus_info), "PCI:%s", dev_name(isp->dev));
552 * enum input are used to check primary/secondary camera
554 static int atomisp_enum_input(struct file *file, void *fh,
555 struct v4l2_input *input)
557 struct video_device *vdev = video_devdata(file);
558 struct atomisp_device *isp = video_get_drvdata(vdev);
559 int index = input->index;
560 struct v4l2_subdev *motor;
562 if (index >= isp->input_cnt)
565 if (!isp->inputs[index].camera)
568 memset(input, 0, sizeof(struct v4l2_input));
569 strscpy(input->name, isp->inputs[index].camera->name,
570 sizeof(input->name));
573 * HACK: append actuator's name to sensor's
574 * As currently userspace can't talk directly to subdev nodes, this
575 * ioctl is the only way to enum inputs + possible external actuators
576 * for 3A tuning purpose.
579 motor = isp->inputs[index].motor;
583 if (motor && strlen(motor->name) > 0) {
584 const int cur_len = strlen(input->name);
585 const int max_size = sizeof(input->name) - cur_len - 1;
588 input->name[cur_len] = '+';
589 strscpy(&input->name[cur_len + 1],
590 motor->name, max_size);
594 input->type = V4L2_INPUT_TYPE_CAMERA;
595 input->index = index;
596 input->reserved[0] = isp->inputs[index].type;
597 input->reserved[1] = isp->inputs[index].port;
603 * get input are used to get current primary/secondary camera
605 static int atomisp_g_input(struct file *file, void *fh, unsigned int *input)
607 struct video_device *vdev = video_devdata(file);
608 struct atomisp_sub_device *asd = atomisp_to_video_pipe(vdev)->asd;
610 *input = asd->input_curr;
614 static int atomisp_s_fmt_cap(struct file *file, void *fh,
615 struct v4l2_format *f)
617 struct video_device *vdev = video_devdata(file);
619 return atomisp_set_fmt(vdev, f);
623 * set input are used to set current primary/secondary camera
625 static int atomisp_s_input(struct file *file, void *fh, unsigned int input)
627 struct video_device *vdev = video_devdata(file);
628 struct atomisp_device *isp = video_get_drvdata(vdev);
629 struct atomisp_video_pipe *pipe = atomisp_to_video_pipe(vdev);
630 struct atomisp_sub_device *asd = pipe->asd;
631 struct v4l2_subdev *camera = NULL;
632 struct v4l2_subdev *motor;
635 ret = atomisp_pipe_check(pipe, true);
639 if (input >= ATOM_ISP_MAX_INPUTS || input >= isp->input_cnt) {
640 dev_dbg(isp->dev, "input_cnt: %d\n", isp->input_cnt);
644 camera = isp->inputs[input].camera;
646 dev_err(isp->dev, "%s, no camera\n", __func__);
650 /* power off the current owned sensor, as it is not used this time */
651 if (isp->inputs[asd->input_curr].asd == asd &&
652 asd->input_curr != input) {
653 ret = v4l2_subdev_call(isp->inputs[asd->input_curr].camera,
655 if (ret && ret != -ENOIOCTLCMD)
657 "Failed to power-off sensor\n");
658 /* clear the asd field to show this camera is not used */
659 isp->inputs[asd->input_curr].asd = NULL;
662 /* powe on the new sensor */
663 ret = v4l2_subdev_call(isp->inputs[input].camera, core, s_power, 1);
664 if (ret && ret != -ENOIOCTLCMD) {
665 dev_err(isp->dev, "Failed to power-on sensor\n");
669 /* select operating sensor */
670 ret = v4l2_subdev_call(isp->inputs[input].camera, video, s_routing,
672 if (ret && (ret != -ENOIOCTLCMD)) {
673 dev_err(isp->dev, "Failed to select sensor\n");
678 motor = isp->inputs[input].motor;
682 ret = v4l2_subdev_call(motor, core, s_power, 1);
686 ret = v4l2_subdev_call(motor, core, init, 1);
688 asd->input_curr = input;
689 /* mark this camera is used by the current stream */
690 isp->inputs[input].asd = asd;
696 * With crop any framesize <= sensor-size can be made, give
697 * userspace a list of sizes to choice from.
699 static int atomisp_enum_framesizes_crop_inner(struct atomisp_device *isp,
700 struct v4l2_frmsizeenum *fsize,
701 const struct v4l2_rect *active,
702 const struct v4l2_rect *native,
705 static const struct v4l2_frmsize_discrete frame_sizes[] = {
718 u32 padding_w, padding_h;
721 for (i = 0; i < ARRAY_SIZE(frame_sizes); i++) {
722 atomisp_get_padding(isp, frame_sizes[i].width, frame_sizes[i].height,
723 &padding_w, &padding_h);
725 if ((frame_sizes[i].width + padding_w) > native->width ||
726 (frame_sizes[i].height + padding_h) > native->height)
730 * Skip sizes where width and height are less then 5/8th of the
731 * sensor size to avoid sizes with a too small field of view.
733 if (frame_sizes[i].width < (active->width * 5 / 8) &&
734 frame_sizes[i].height < (active->height * 5 / 8))
737 if (*valid_sizes == fsize->index) {
738 fsize->type = V4L2_FRMSIZE_TYPE_DISCRETE;
739 fsize->discrete = frame_sizes[i];
749 static int atomisp_enum_framesizes_crop(struct atomisp_device *isp,
750 struct v4l2_frmsizeenum *fsize)
752 struct atomisp_input_subdev *input = &isp->inputs[isp->asd.input_curr];
753 struct v4l2_rect active = input->active_rect;
754 struct v4l2_rect native = input->native_rect;
755 int ret, valid_sizes = 0;
757 ret = atomisp_enum_framesizes_crop_inner(isp, fsize, &active, &native, &valid_sizes);
761 if (!input->binning_support)
769 return atomisp_enum_framesizes_crop_inner(isp, fsize, &active, &native, &valid_sizes);
772 static int atomisp_enum_framesizes(struct file *file, void *priv,
773 struct v4l2_frmsizeenum *fsize)
775 struct video_device *vdev = video_devdata(file);
776 struct atomisp_device *isp = video_get_drvdata(vdev);
777 struct atomisp_sub_device *asd = atomisp_to_video_pipe(vdev)->asd;
778 struct atomisp_input_subdev *input = &isp->inputs[asd->input_curr];
779 struct v4l2_subdev_frame_size_enum fse = {
780 .index = fsize->index,
781 .which = V4L2_SUBDEV_FORMAT_ACTIVE,
784 struct v4l2_subdev_state *act_sd_state;
790 if (input->crop_support)
791 return atomisp_enum_framesizes_crop(isp, fsize);
793 act_sd_state = v4l2_subdev_lock_and_get_active_state(input->camera);
794 ret = v4l2_subdev_call(input->camera, pad, enum_frame_size,
797 v4l2_subdev_unlock_state(act_sd_state);
801 fsize->type = V4L2_FRMSIZE_TYPE_DISCRETE;
802 fsize->discrete.width = fse.max_width - pad_w;
803 fsize->discrete.height = fse.max_height - pad_h;
808 static int atomisp_enum_frameintervals(struct file *file, void *priv,
809 struct v4l2_frmivalenum *fival)
811 struct video_device *vdev = video_devdata(file);
812 struct atomisp_device *isp = video_get_drvdata(vdev);
813 struct atomisp_sub_device *asd = atomisp_to_video_pipe(vdev)->asd;
814 struct atomisp_input_subdev *input = &isp->inputs[asd->input_curr];
815 struct v4l2_subdev_frame_interval_enum fie = {
816 .code = atomisp_in_fmt_conv[0].code,
817 .index = fival->index,
818 .width = fival->width,
819 .height = fival->height,
820 .which = V4L2_SUBDEV_FORMAT_ACTIVE,
822 struct v4l2_subdev_state *act_sd_state;
828 act_sd_state = v4l2_subdev_lock_and_get_active_state(input->camera);
829 ret = v4l2_subdev_call(input->camera, pad, enum_frame_interval,
832 v4l2_subdev_unlock_state(act_sd_state);
836 fival->type = V4L2_FRMIVAL_TYPE_DISCRETE;
837 fival->discrete = fie.interval;
842 static int atomisp_enum_fmt_cap(struct file *file, void *fh,
843 struct v4l2_fmtdesc *f)
845 struct video_device *vdev = video_devdata(file);
846 struct atomisp_device *isp = video_get_drvdata(vdev);
847 struct atomisp_sub_device *asd = atomisp_to_video_pipe(vdev)->asd;
848 struct atomisp_input_subdev *input = &isp->inputs[asd->input_curr];
849 struct v4l2_subdev_mbus_code_enum code = {
850 .which = V4L2_SUBDEV_FORMAT_ACTIVE,
852 const struct atomisp_format_bridge *format;
853 struct v4l2_subdev_state *act_sd_state;
854 unsigned int i, fi = 0;
860 act_sd_state = v4l2_subdev_lock_and_get_active_state(input->camera);
861 ret = v4l2_subdev_call(input->camera, pad, enum_mbus_code,
862 act_sd_state, &code);
864 v4l2_subdev_unlock_state(act_sd_state);
868 for (i = 0; i < ARRAY_SIZE(atomisp_output_fmts); i++) {
869 format = &atomisp_output_fmts[i];
872 * Is the atomisp-supported format is valid for the
873 * sensor (configuration)? If not, skip it.
875 * FIXME: fix the pipeline to allow sensor format too.
877 if (format->sh_fmt == IA_CSS_FRAME_FORMAT_RAW)
880 /* Found a match. Now let's pick f->index'th one. */
886 strscpy(f->description, format->description,
887 sizeof(f->description));
888 f->pixelformat = format->pixelformat;
895 /* This function looks up the closest available resolution. */
896 static int atomisp_try_fmt_cap(struct file *file, void *fh,
897 struct v4l2_format *f)
899 struct video_device *vdev = video_devdata(file);
900 struct atomisp_device *isp = video_get_drvdata(vdev);
902 return atomisp_try_fmt(isp, &f->fmt.pix, NULL, NULL);
905 static int atomisp_g_fmt_cap(struct file *file, void *fh,
906 struct v4l2_format *f)
908 struct video_device *vdev = video_devdata(file);
909 struct atomisp_video_pipe *pipe;
911 pipe = atomisp_to_video_pipe(vdev);
913 f->fmt.pix = pipe->pix;
915 /* If s_fmt was issued, just return whatever is was previouly set */
916 if (f->fmt.pix.sizeimage)
919 f->fmt.pix.pixelformat = V4L2_PIX_FMT_YUV420;
920 f->fmt.pix.width = 10000;
921 f->fmt.pix.height = 10000;
923 return atomisp_try_fmt_cap(file, fh, f);
926 int atomisp_alloc_css_stat_bufs(struct atomisp_sub_device *asd,
929 struct atomisp_device *isp = asd->isp;
930 struct atomisp_s3a_buf *s3a_buf = NULL, *_s3a_buf;
931 struct atomisp_dis_buf *dis_buf = NULL, *_dis_buf;
932 struct atomisp_metadata_buf *md_buf = NULL, *_md_buf;
934 struct ia_css_dvs_grid_info *dvs_grid_info =
935 atomisp_css_get_dvs_grid_info(&asd->params.curr_grid_info);
938 if (list_empty(&asd->s3a_stats) &&
939 asd->params.curr_grid_info.s3a_grid.enable) {
940 count = ATOMISP_CSS_Q_DEPTH +
941 ATOMISP_S3A_BUF_QUEUE_DEPTH_FOR_HAL;
942 dev_dbg(isp->dev, "allocating %d 3a buffers\n", count);
944 s3a_buf = kzalloc(sizeof(struct atomisp_s3a_buf), GFP_KERNEL);
948 if (atomisp_css_allocate_stat_buffers(
949 asd, stream_id, s3a_buf, NULL, NULL)) {
954 list_add_tail(&s3a_buf->list, &asd->s3a_stats);
958 if (list_empty(&asd->dis_stats) && dvs_grid_info &&
959 dvs_grid_info->enable) {
960 count = ATOMISP_CSS_Q_DEPTH + 1;
961 dev_dbg(isp->dev, "allocating %d dis buffers\n", count);
963 dis_buf = kzalloc(sizeof(struct atomisp_dis_buf), GFP_KERNEL);
966 if (atomisp_css_allocate_stat_buffers(
967 asd, stream_id, NULL, dis_buf, NULL)) {
972 list_add_tail(&dis_buf->list, &asd->dis_stats);
976 for (i = 0; i < ATOMISP_METADATA_TYPE_NUM; i++) {
977 if (list_empty(&asd->metadata[i]) &&
978 list_empty(&asd->metadata_ready[i]) &&
979 list_empty(&asd->metadata_in_css[i])) {
980 count = ATOMISP_CSS_Q_DEPTH +
981 ATOMISP_METADATA_QUEUE_DEPTH_FOR_HAL;
982 dev_dbg(isp->dev, "allocating %d metadata buffers for type %d\n",
985 md_buf = kzalloc(sizeof(struct atomisp_metadata_buf),
990 if (atomisp_css_allocate_stat_buffers(
991 asd, stream_id, NULL, NULL, md_buf)) {
995 list_add_tail(&md_buf->list, &asd->metadata[i]);
1002 dev_err(isp->dev, "failed to allocate statistics buffers\n");
1004 list_for_each_entry_safe(dis_buf, _dis_buf, &asd->dis_stats, list) {
1005 atomisp_css_free_dis_buffer(dis_buf);
1006 list_del(&dis_buf->list);
1010 list_for_each_entry_safe(s3a_buf, _s3a_buf, &asd->s3a_stats, list) {
1011 atomisp_css_free_3a_buffer(s3a_buf);
1012 list_del(&s3a_buf->list);
1016 for (i = 0; i < ATOMISP_METADATA_TYPE_NUM; i++) {
1017 list_for_each_entry_safe(md_buf, _md_buf, &asd->metadata[i],
1019 atomisp_css_free_metadata_buffer(md_buf);
1020 list_del(&md_buf->list);
1028 * FIXME the abuse of buf->reserved2 in the qbuf and dqbuf wrappers comes from
1029 * the original atomisp buffer handling and should be replaced with proper V4L2
1030 * per frame parameters use.
1032 * Once this is fixed these wrappers can be removed, replacing them with direct
1033 * calls to vb2_ioctl_[d]qbuf().
1035 static int atomisp_qbuf_wrapper(struct file *file, void *fh, struct v4l2_buffer *buf)
1037 struct video_device *vdev = video_devdata(file);
1038 struct atomisp_device *isp = video_get_drvdata(vdev);
1039 struct atomisp_video_pipe *pipe = atomisp_to_video_pipe(vdev);
1041 if (buf->index >= vb2_get_num_buffers(vdev->queue))
1044 if (buf->reserved2 & ATOMISP_BUFFER_HAS_PER_FRAME_SETTING) {
1045 /* this buffer will have a per-frame parameter */
1046 pipe->frame_request_config_id[buf->index] = buf->reserved2 &
1047 ~ATOMISP_BUFFER_HAS_PER_FRAME_SETTING;
1049 "This buffer requires per_frame setting which has isp_config_id %d\n",
1050 pipe->frame_request_config_id[buf->index]);
1052 pipe->frame_request_config_id[buf->index] = 0;
1055 return vb2_ioctl_qbuf(file, fh, buf);
1058 static int atomisp_dqbuf_wrapper(struct file *file, void *fh, struct v4l2_buffer *buf)
1060 struct video_device *vdev = video_devdata(file);
1061 struct atomisp_video_pipe *pipe = atomisp_to_video_pipe(vdev);
1062 struct atomisp_sub_device *asd = pipe->asd;
1063 struct atomisp_device *isp = video_get_drvdata(vdev);
1064 struct ia_css_frame *frame;
1065 struct vb2_buffer *vb;
1068 ret = vb2_ioctl_dqbuf(file, fh, buf);
1072 vb = vb2_get_buffer(&pipe->vb_queue, buf->index);
1073 frame = vb_to_frame(vb);
1075 buf->reserved = asd->frame_status[buf->index];
1079 * Currently frame_status in the enum type which takes no more lower
1081 * use bit[31:16] for exp_id as it is only in the range of 1~255
1083 buf->reserved &= 0x0000ffff;
1084 if (!(buf->flags & V4L2_BUF_FLAG_ERROR))
1085 buf->reserved |= frame->exp_id;
1086 buf->reserved2 = pipe->frame_config_id[buf->index];
1089 "dqbuf buffer %d (%s) with exp_id %d, isp_config_id %d\n",
1090 buf->index, vdev->name, buf->reserved >> 16, buf->reserved2);
1094 /* Input system HW workaround */
1095 /* Input system address translation corrupts burst during */
1096 /* invalidate. SW workaround for this is to set burst length */
1097 /* manually to 128 in case of 13MPx snapshot and to 1 otherwise. */
1098 static void atomisp_dma_burst_len_cfg(struct atomisp_sub_device *asd)
1100 struct v4l2_mbus_framefmt *sink;
1102 sink = atomisp_subdev_get_ffmt(&asd->subdev, NULL,
1103 V4L2_SUBDEV_FORMAT_ACTIVE,
1104 ATOMISP_SUBDEV_PAD_SINK);
1106 if (sink->width * sink->height >= 4096 * 3072)
1107 atomisp_css2_hw_store_32(DMA_BURST_SIZE_REG, 0x7F);
1109 atomisp_css2_hw_store_32(DMA_BURST_SIZE_REG, 0x00);
1112 int atomisp_start_streaming(struct vb2_queue *vq, unsigned int count)
1114 struct atomisp_video_pipe *pipe = vq_to_pipe(vq);
1115 struct atomisp_sub_device *asd = pipe->asd;
1116 struct atomisp_device *isp = asd->isp;
1117 struct pci_dev *pdev = to_pci_dev(isp->dev);
1118 unsigned long irqflags;
1121 dev_dbg(isp->dev, "Start stream\n");
1123 mutex_lock(&isp->mutex);
1125 ret = atomisp_pipe_check(pipe, false);
1129 /* Input system HW workaround */
1130 atomisp_dma_burst_len_cfg(asd);
1132 /* Invalidate caches. FIXME: should flush only necessary buffers */
1135 if (asd->params.css_update_params_needed) {
1136 atomisp_apply_css_parameters(asd, &asd->params.css_param);
1137 if (asd->params.css_param.update_flag.dz_config)
1138 asd->params.config.dz_config = &asd->params.css_param.dz_config;
1139 atomisp_css_update_isp_params(asd);
1140 asd->params.css_update_params_needed = false;
1141 memset(&asd->params.css_param.update_flag, 0,
1142 sizeof(struct atomisp_parameters));
1144 asd->params.dvs_6axis = NULL;
1146 ret = atomisp_css_start(asd);
1148 atomisp_flush_video_pipe(pipe, VB2_BUF_STATE_QUEUED, true);
1152 spin_lock_irqsave(&isp->lock, irqflags);
1153 asd->streaming = true;
1154 spin_unlock_irqrestore(&isp->lock, irqflags);
1155 atomic_set(&asd->sof_count, -1);
1156 atomic_set(&asd->sequence, -1);
1157 atomic_set(&asd->sequence_temp, -1);
1159 asd->params.dis_proj_data_valid = false;
1160 asd->latest_preview_exp_id = 0;
1161 asd->postview_exp_id = 1;
1162 asd->preview_exp_id = 1;
1164 /* handle per_frame_setting parameter and buffers */
1165 atomisp_handle_parameter_and_buffer(pipe);
1167 atomisp_qbuffers_to_css(asd);
1170 asd->params.num_flash_frames = 0;
1171 asd->params.flash_state = ATOMISP_FLASH_IDLE;
1172 atomisp_setup_flash(asd);
1175 atomisp_css_irq_enable(isp, IA_CSS_IRQ_INFO_CSS_RECEIVER_SOF,
1176 atomisp_css_valid_sof(isp));
1177 atomisp_csi2_configure(asd);
1179 if (atomisp_freq_scaling(isp, ATOMISP_DFS_MODE_AUTO, false) < 0)
1180 dev_dbg(isp->dev, "DFS auto mode failed!\n");
1182 /* Enable the CSI interface on ANN B0/K0 */
1183 if (isp->media_dev.hw_revision >= ((ATOMISP_HW_REVISION_ISP2401 <<
1184 ATOMISP_HW_REVISION_SHIFT) | ATOMISP_HW_STEPPING_B0)) {
1185 pci_write_config_word(pdev, MRFLD_PCI_CSI_CONTROL,
1186 isp->saved_regs.csi_control | MRFLD_PCI_CSI_CONTROL_CSI_READY);
1189 /* stream on the sensor */
1190 ret = v4l2_subdev_call(isp->inputs[asd->input_curr].camera,
1191 video, s_stream, 1);
1193 dev_err(isp->dev, "Starting sensor stream failed: %d\n", ret);
1194 spin_lock_irqsave(&isp->lock, irqflags);
1195 asd->streaming = false;
1196 spin_unlock_irqrestore(&isp->lock, irqflags);
1202 mutex_unlock(&isp->mutex);
1206 void atomisp_stop_streaming(struct vb2_queue *vq)
1208 struct atomisp_video_pipe *pipe = vq_to_pipe(vq);
1209 struct atomisp_sub_device *asd = pipe->asd;
1210 struct atomisp_device *isp = asd->isp;
1211 struct pci_dev *pdev = to_pci_dev(isp->dev);
1212 unsigned long flags;
1215 dev_dbg(isp->dev, "Stop stream\n");
1217 mutex_lock(&isp->mutex);
1219 * There is no guarantee that the buffers queued to / owned by the ISP
1220 * will properly be returned to the queue when stopping. Set a flag to
1221 * avoid new buffers getting queued and then wait for all the current
1222 * buffers to finish.
1224 pipe->stopping = true;
1225 mutex_unlock(&isp->mutex);
1226 /* wait max 1 second */
1227 ret = wait_event_timeout(pipe->vb_queue.done_wq,
1228 atomisp_buffers_in_css(pipe) == 0, HZ);
1229 mutex_lock(&isp->mutex);
1230 pipe->stopping = false;
1232 dev_warn(isp->dev, "Warning timeout waiting for CSS to return buffers\n");
1234 spin_lock_irqsave(&isp->lock, flags);
1235 asd->streaming = false;
1236 spin_unlock_irqrestore(&isp->lock, flags);
1238 atomisp_clear_css_buffer_counters(asd);
1239 atomisp_css_irq_enable(isp, IA_CSS_IRQ_INFO_CSS_RECEIVER_SOF, false);
1241 atomisp_css_stop(asd, false);
1243 atomisp_flush_video_pipe(pipe, VB2_BUF_STATE_ERROR, true);
1245 atomisp_subdev_cleanup_pending_events(asd);
1247 ret = v4l2_subdev_call(isp->inputs[asd->input_curr].camera,
1248 video, s_stream, 0);
1250 dev_warn(isp->dev, "Stopping sensor stream failed: %d\n", ret);
1253 asd->params.num_flash_frames = 0;
1254 asd->params.flash_state = ATOMISP_FLASH_IDLE;
1257 /* Disable the CSI interface on ANN B0/K0 */
1258 if (isp->media_dev.hw_revision >= ((ATOMISP_HW_REVISION_ISP2401 <<
1259 ATOMISP_HW_REVISION_SHIFT) | ATOMISP_HW_STEPPING_B0)) {
1260 pci_write_config_word(pdev, MRFLD_PCI_CSI_CONTROL,
1261 isp->saved_regs.csi_control & ~MRFLD_PCI_CSI_CONTROL_CSI_READY);
1264 if (atomisp_freq_scaling(isp, ATOMISP_DFS_MODE_LOW, false))
1265 dev_warn(isp->dev, "DFS failed.\n");
1268 * ISP work around, need to reset ISP to allow next stream on to work.
1269 * Streams have already been destroyed by atomisp_css_stop().
1270 * Disable PUNIT/ISP acknowlede/handshake - SRSE=3 and then reset.
1272 pci_write_config_dword(pdev, PCI_I_CONTROL,
1273 isp->saved_regs.i_control | MRFLD_PCI_I_CONTROL_SRSE_RESET_MASK);
1276 /* Streams were destroyed by atomisp_css_stop(), recreate them. */
1277 ret = atomisp_create_pipes_stream(&isp->asd);
1279 dev_warn(isp->dev, "Recreating streams failed: %d\n", ret);
1281 mutex_unlock(&isp->mutex);
1285 * To get the current value of a control.
1286 * applications initialize the id field of a struct v4l2_control and
1287 * call this ioctl with a pointer to this structure
1289 static int atomisp_g_ctrl(struct file *file, void *fh,
1290 struct v4l2_control *control)
1292 struct video_device *vdev = video_devdata(file);
1293 struct atomisp_sub_device *asd = atomisp_to_video_pipe(vdev)->asd;
1294 struct atomisp_device *isp = video_get_drvdata(vdev);
1295 int i, ret = -EINVAL;
1297 for (i = 0; i < ctrls_num; i++) {
1298 if (ci_v4l2_controls[i].id == control->id) {
1307 switch (control->id) {
1308 case V4L2_CID_IRIS_ABSOLUTE:
1309 case V4L2_CID_EXPOSURE_ABSOLUTE:
1310 case V4L2_CID_2A_STATUS:
1311 case V4L2_CID_AUTO_N_PRESET_WHITE_BALANCE:
1312 case V4L2_CID_EXPOSURE:
1313 case V4L2_CID_EXPOSURE_AUTO:
1314 case V4L2_CID_SCENE_MODE:
1315 case V4L2_CID_ISO_SENSITIVITY:
1316 case V4L2_CID_ISO_SENSITIVITY_AUTO:
1317 case V4L2_CID_CONTRAST:
1318 case V4L2_CID_SATURATION:
1319 case V4L2_CID_SHARPNESS:
1320 case V4L2_CID_3A_LOCK:
1321 case V4L2_CID_EXPOSURE_ZONE_NUM:
1322 case V4L2_CID_TEST_PATTERN:
1323 case V4L2_CID_TEST_PATTERN_COLOR_R:
1324 case V4L2_CID_TEST_PATTERN_COLOR_GR:
1325 case V4L2_CID_TEST_PATTERN_COLOR_GB:
1326 case V4L2_CID_TEST_PATTERN_COLOR_B:
1327 return v4l2_g_ctrl(isp->inputs[asd->input_curr].camera->
1328 ctrl_handler, control);
1329 case V4L2_CID_COLORFX:
1330 ret = atomisp_color_effect(asd, 0, &control->value);
1332 case V4L2_CID_ATOMISP_BAD_PIXEL_DETECTION:
1333 ret = atomisp_bad_pixel(asd, 0, &control->value);
1335 case V4L2_CID_ATOMISP_POSTPROCESS_GDC_CAC:
1336 ret = atomisp_gdc_cac(asd, 0, &control->value);
1338 case V4L2_CID_ATOMISP_VIDEO_STABLIZATION:
1339 ret = atomisp_video_stable(asd, 0, &control->value);
1341 case V4L2_CID_ATOMISP_FIXED_PATTERN_NR:
1342 ret = atomisp_fixed_pattern(asd, 0, &control->value);
1344 case V4L2_CID_ATOMISP_FALSE_COLOR_CORRECTION:
1345 ret = atomisp_false_color(asd, 0, &control->value);
1347 case V4L2_CID_ATOMISP_LOW_LIGHT:
1348 ret = atomisp_low_light(asd, 0, &control->value);
1359 * To change the value of a control.
1360 * applications initialize the id and value fields of a struct v4l2_control
1361 * and call this ioctl.
1363 static int atomisp_s_ctrl(struct file *file, void *fh,
1364 struct v4l2_control *control)
1366 struct video_device *vdev = video_devdata(file);
1367 struct atomisp_sub_device *asd = atomisp_to_video_pipe(vdev)->asd;
1368 struct atomisp_device *isp = video_get_drvdata(vdev);
1369 int i, ret = -EINVAL;
1371 for (i = 0; i < ctrls_num; i++) {
1372 if (ci_v4l2_controls[i].id == control->id) {
1381 switch (control->id) {
1382 case V4L2_CID_AUTO_N_PRESET_WHITE_BALANCE:
1383 case V4L2_CID_EXPOSURE:
1384 case V4L2_CID_EXPOSURE_AUTO:
1385 case V4L2_CID_EXPOSURE_AUTO_PRIORITY:
1386 case V4L2_CID_SCENE_MODE:
1387 case V4L2_CID_ISO_SENSITIVITY:
1388 case V4L2_CID_ISO_SENSITIVITY_AUTO:
1389 case V4L2_CID_POWER_LINE_FREQUENCY:
1390 case V4L2_CID_EXPOSURE_METERING:
1391 case V4L2_CID_CONTRAST:
1392 case V4L2_CID_SATURATION:
1393 case V4L2_CID_SHARPNESS:
1394 case V4L2_CID_3A_LOCK:
1395 case V4L2_CID_COLORFX_CBCR:
1396 case V4L2_CID_TEST_PATTERN:
1397 case V4L2_CID_TEST_PATTERN_COLOR_R:
1398 case V4L2_CID_TEST_PATTERN_COLOR_GR:
1399 case V4L2_CID_TEST_PATTERN_COLOR_GB:
1400 case V4L2_CID_TEST_PATTERN_COLOR_B:
1401 return v4l2_s_ctrl(NULL,
1402 isp->inputs[asd->input_curr].camera->
1403 ctrl_handler, control);
1404 case V4L2_CID_COLORFX:
1405 ret = atomisp_color_effect(asd, 1, &control->value);
1407 case V4L2_CID_ATOMISP_BAD_PIXEL_DETECTION:
1408 ret = atomisp_bad_pixel(asd, 1, &control->value);
1410 case V4L2_CID_ATOMISP_POSTPROCESS_GDC_CAC:
1411 ret = atomisp_gdc_cac(asd, 1, &control->value);
1413 case V4L2_CID_ATOMISP_VIDEO_STABLIZATION:
1414 ret = atomisp_video_stable(asd, 1, &control->value);
1416 case V4L2_CID_ATOMISP_FIXED_PATTERN_NR:
1417 ret = atomisp_fixed_pattern(asd, 1, &control->value);
1419 case V4L2_CID_ATOMISP_FALSE_COLOR_CORRECTION:
1420 ret = atomisp_false_color(asd, 1, &control->value);
1422 case V4L2_CID_REQUEST_FLASH:
1423 ret = atomisp_flash_enable(asd, control->value);
1425 case V4L2_CID_ATOMISP_LOW_LIGHT:
1426 ret = atomisp_low_light(asd, 1, &control->value);
1436 * To query the attributes of a control.
1437 * applications set the id field of a struct v4l2_queryctrl and call the
1438 * this ioctl with a pointer to this structure. The driver fills
1439 * the rest of the structure.
1441 static int atomisp_queryctl(struct file *file, void *fh,
1442 struct v4l2_queryctrl *qc)
1444 int i, ret = -EINVAL;
1445 struct video_device *vdev = video_devdata(file);
1446 struct atomisp_sub_device *asd = atomisp_to_video_pipe(vdev)->asd;
1447 struct atomisp_device *isp = video_get_drvdata(vdev);
1450 case V4L2_CID_FOCUS_ABSOLUTE:
1451 case V4L2_CID_FOCUS_RELATIVE:
1452 case V4L2_CID_FOCUS_STATUS:
1454 return v4l2_queryctrl(isp->inputs[asd->input_curr].camera->
1459 return v4l2_queryctrl(isp->motor->ctrl_handler, qc);
1461 return v4l2_queryctrl(isp->inputs[asd->input_curr].
1462 camera->ctrl_handler, qc);
1465 if (qc->id & V4L2_CTRL_FLAG_NEXT_CTRL)
1468 for (i = 0; i < ctrls_num; i++) {
1469 if (ci_v4l2_controls[i].id == qc->id) {
1470 memcpy(qc, &ci_v4l2_controls[i],
1471 sizeof(struct v4l2_queryctrl));
1472 qc->reserved[0] = 0;
1478 qc->flags = V4L2_CTRL_FLAG_DISABLED;
1483 static int atomisp_camera_g_ext_ctrls(struct file *file, void *fh,
1484 struct v4l2_ext_controls *c)
1486 struct video_device *vdev = video_devdata(file);
1487 struct atomisp_sub_device *asd = atomisp_to_video_pipe(vdev)->asd;
1488 struct atomisp_device *isp = video_get_drvdata(vdev);
1489 struct v4l2_subdev *motor;
1490 struct v4l2_control ctrl;
1495 motor = isp->inputs[asd->input_curr].motor;
1499 for (i = 0; i < c->count; i++) {
1500 ctrl.id = c->controls[i].id;
1501 ctrl.value = c->controls[i].value;
1503 case V4L2_CID_EXPOSURE_ABSOLUTE:
1504 case V4L2_CID_EXPOSURE_AUTO:
1505 case V4L2_CID_IRIS_ABSOLUTE:
1506 case V4L2_CID_3A_LOCK:
1507 case V4L2_CID_TEST_PATTERN:
1508 case V4L2_CID_TEST_PATTERN_COLOR_R:
1509 case V4L2_CID_TEST_PATTERN_COLOR_GR:
1510 case V4L2_CID_TEST_PATTERN_COLOR_GB:
1511 case V4L2_CID_TEST_PATTERN_COLOR_B:
1513 * Exposure related control will be handled by sensor
1517 v4l2_g_ctrl(isp->inputs[asd->input_curr].camera->
1518 ctrl_handler, &ctrl);
1520 case V4L2_CID_FOCUS_ABSOLUTE:
1521 case V4L2_CID_FOCUS_RELATIVE:
1522 case V4L2_CID_FOCUS_STATUS:
1523 case V4L2_CID_FOCUS_AUTO:
1525 ret = v4l2_g_ctrl(motor->ctrl_handler, &ctrl);
1527 case V4L2_CID_FLASH_STATUS:
1528 case V4L2_CID_FLASH_INTENSITY:
1529 case V4L2_CID_FLASH_TORCH_INTENSITY:
1530 case V4L2_CID_FLASH_INDICATOR_INTENSITY:
1531 case V4L2_CID_FLASH_TIMEOUT:
1532 case V4L2_CID_FLASH_STROBE:
1533 case V4L2_CID_FLASH_MODE:
1534 case V4L2_CID_FLASH_STATUS_REGISTER:
1537 v4l2_g_ctrl(isp->flash->ctrl_handler,
1540 case V4L2_CID_ZOOM_ABSOLUTE:
1541 ret = atomisp_digital_zoom(asd, 0, &ctrl.value);
1543 case V4L2_CID_G_SKIP_FRAMES:
1544 ret = v4l2_subdev_call(
1545 isp->inputs[asd->input_curr].camera,
1546 sensor, g_skip_frames, (u32 *)&ctrl.value);
1556 c->controls[i].value = ctrl.value;
1561 /* This ioctl allows the application to get multiple controls by class */
1562 static int atomisp_g_ext_ctrls(struct file *file, void *fh,
1563 struct v4l2_ext_controls *c)
1565 struct v4l2_control ctrl;
1569 * input_lock is not need for the Camera related IOCTLs
1570 * The input_lock downgrade the FPS of 3A
1572 ret = atomisp_camera_g_ext_ctrls(file, fh, c);
1576 for (i = 0; i < c->count; i++) {
1577 ctrl.id = c->controls[i].id;
1578 ctrl.value = c->controls[i].value;
1579 ret = atomisp_g_ctrl(file, fh, &ctrl);
1580 c->controls[i].value = ctrl.value;
1589 static int atomisp_camera_s_ext_ctrls(struct file *file, void *fh,
1590 struct v4l2_ext_controls *c)
1592 struct video_device *vdev = video_devdata(file);
1593 struct atomisp_sub_device *asd = atomisp_to_video_pipe(vdev)->asd;
1594 struct atomisp_device *isp = video_get_drvdata(vdev);
1595 struct v4l2_subdev *motor;
1596 struct v4l2_control ctrl;
1601 motor = isp->inputs[asd->input_curr].motor;
1605 for (i = 0; i < c->count; i++) {
1606 struct v4l2_ctrl *ctr;
1608 ctrl.id = c->controls[i].id;
1609 ctrl.value = c->controls[i].value;
1611 case V4L2_CID_EXPOSURE_ABSOLUTE:
1612 case V4L2_CID_EXPOSURE_AUTO:
1613 case V4L2_CID_EXPOSURE_METERING:
1614 case V4L2_CID_IRIS_ABSOLUTE:
1615 case V4L2_CID_VCM_TIMING:
1616 case V4L2_CID_VCM_SLEW:
1617 case V4L2_CID_3A_LOCK:
1618 case V4L2_CID_TEST_PATTERN:
1619 case V4L2_CID_TEST_PATTERN_COLOR_R:
1620 case V4L2_CID_TEST_PATTERN_COLOR_GR:
1621 case V4L2_CID_TEST_PATTERN_COLOR_GB:
1622 case V4L2_CID_TEST_PATTERN_COLOR_B:
1623 ret = v4l2_s_ctrl(NULL,
1624 isp->inputs[asd->input_curr].camera->
1625 ctrl_handler, &ctrl);
1627 case V4L2_CID_FOCUS_ABSOLUTE:
1628 case V4L2_CID_FOCUS_RELATIVE:
1629 case V4L2_CID_FOCUS_STATUS:
1630 case V4L2_CID_FOCUS_AUTO:
1632 ret = v4l2_s_ctrl(NULL, motor->ctrl_handler,
1635 ret = v4l2_s_ctrl(NULL,
1636 isp->inputs[asd->input_curr].
1637 camera->ctrl_handler, &ctrl);
1639 case V4L2_CID_FLASH_STATUS:
1640 case V4L2_CID_FLASH_INTENSITY:
1641 case V4L2_CID_FLASH_TORCH_INTENSITY:
1642 case V4L2_CID_FLASH_INDICATOR_INTENSITY:
1643 case V4L2_CID_FLASH_TIMEOUT:
1644 case V4L2_CID_FLASH_STROBE:
1645 case V4L2_CID_FLASH_MODE:
1646 case V4L2_CID_FLASH_STATUS_REGISTER:
1649 v4l2_s_ctrl(NULL, isp->flash->ctrl_handler,
1652 * When flash mode is changed we need to reset
1655 if (ctrl.id == V4L2_CID_FLASH_MODE) {
1656 asd->params.flash_state =
1658 asd->params.num_flash_frames = 0;
1662 case V4L2_CID_ZOOM_ABSOLUTE:
1663 ret = atomisp_digital_zoom(asd, 1, &ctrl.value);
1666 ctr = v4l2_ctrl_find(&asd->ctrl_handler, ctrl.id);
1668 ret = v4l2_ctrl_s_ctrl(ctr, ctrl.value);
1677 c->controls[i].value = ctrl.value;
1682 /* This ioctl allows the application to set multiple controls by class */
1683 static int atomisp_s_ext_ctrls(struct file *file, void *fh,
1684 struct v4l2_ext_controls *c)
1686 struct v4l2_control ctrl;
1690 * input_lock is not need for the Camera related IOCTLs
1691 * The input_lock downgrade the FPS of 3A
1693 ret = atomisp_camera_s_ext_ctrls(file, fh, c);
1697 for (i = 0; i < c->count; i++) {
1698 ctrl.id = c->controls[i].id;
1699 ctrl.value = c->controls[i].value;
1700 ret = atomisp_s_ctrl(file, fh, &ctrl);
1701 c->controls[i].value = ctrl.value;
1711 * vidioc_g/s_param are used to switch isp running mode
1713 static int atomisp_g_parm(struct file *file, void *fh,
1714 struct v4l2_streamparm *parm)
1716 struct video_device *vdev = video_devdata(file);
1717 struct atomisp_sub_device *asd = atomisp_to_video_pipe(vdev)->asd;
1718 struct atomisp_device *isp = video_get_drvdata(vdev);
1720 if (parm->type != V4L2_BUF_TYPE_VIDEO_CAPTURE) {
1721 dev_err(isp->dev, "unsupported v4l2 buf type\n");
1725 parm->parm.capture.capturemode = asd->run_mode->val;
1730 static int atomisp_s_parm(struct file *file, void *fh,
1731 struct v4l2_streamparm *parm)
1733 struct video_device *vdev = video_devdata(file);
1734 struct atomisp_device *isp = video_get_drvdata(vdev);
1735 struct atomisp_sub_device *asd = atomisp_to_video_pipe(vdev)->asd;
1740 if (parm->type != V4L2_BUF_TYPE_VIDEO_CAPTURE) {
1741 dev_err(isp->dev, "unsupported v4l2 buf type\n");
1745 asd->high_speed_mode = false;
1746 switch (parm->parm.capture.capturemode) {
1747 case CI_MODE_NONE: {
1748 struct v4l2_subdev_frame_interval fi = {0};
1750 fi.interval = parm->parm.capture.timeperframe;
1752 rval = v4l2_subdev_call_state_active(isp->inputs[asd->input_curr].camera,
1753 pad, set_frame_interval, &fi);
1755 parm->parm.capture.timeperframe = fi.interval;
1757 if (fi.interval.numerator != 0) {
1758 fps = fi.interval.denominator / fi.interval.numerator;
1760 asd->high_speed_mode = true;
1763 return rval == -ENOIOCTLCMD ? 0 : rval;
1766 mode = ATOMISP_RUN_MODE_VIDEO;
1768 case CI_MODE_STILL_CAPTURE:
1769 mode = ATOMISP_RUN_MODE_STILL_CAPTURE;
1771 case CI_MODE_PREVIEW:
1772 mode = ATOMISP_RUN_MODE_PREVIEW;
1778 rval = v4l2_ctrl_s_ctrl(asd->run_mode, mode);
1780 return rval == -ENOIOCTLCMD ? 0 : rval;
1783 static long atomisp_vidioc_default(struct file *file, void *fh,
1784 bool valid_prio, unsigned int cmd, void *arg)
1786 struct video_device *vdev = video_devdata(file);
1787 struct atomisp_device *isp = video_get_drvdata(vdev);
1788 struct atomisp_sub_device *asd = atomisp_to_video_pipe(vdev)->asd;
1792 case ATOMISP_IOC_G_XNR:
1793 err = atomisp_xnr(asd, 0, arg);
1796 case ATOMISP_IOC_S_XNR:
1797 err = atomisp_xnr(asd, 1, arg);
1800 case ATOMISP_IOC_G_NR:
1801 err = atomisp_nr(asd, 0, arg);
1804 case ATOMISP_IOC_S_NR:
1805 err = atomisp_nr(asd, 1, arg);
1808 case ATOMISP_IOC_G_TNR:
1809 err = atomisp_tnr(asd, 0, arg);
1812 case ATOMISP_IOC_S_TNR:
1813 err = atomisp_tnr(asd, 1, arg);
1816 case ATOMISP_IOC_G_BLACK_LEVEL_COMP:
1817 err = atomisp_black_level(asd, 0, arg);
1820 case ATOMISP_IOC_S_BLACK_LEVEL_COMP:
1821 err = atomisp_black_level(asd, 1, arg);
1824 case ATOMISP_IOC_G_EE:
1825 err = atomisp_ee(asd, 0, arg);
1828 case ATOMISP_IOC_S_EE:
1829 err = atomisp_ee(asd, 1, arg);
1832 case ATOMISP_IOC_G_DIS_STAT:
1833 err = atomisp_get_dis_stat(asd, arg);
1836 case ATOMISP_IOC_G_DVS2_BQ_RESOLUTIONS:
1837 err = atomisp_get_dvs2_bq_resolutions(asd, arg);
1840 case ATOMISP_IOC_S_DIS_COEFS:
1841 err = atomisp_css_cp_dvs2_coefs(asd, arg,
1842 &asd->params.css_param, true);
1844 asd->params.css_update_params_needed = true;
1847 case ATOMISP_IOC_S_DIS_VECTOR:
1848 err = atomisp_cp_dvs_6axis_config(asd, arg,
1849 &asd->params.css_param, true);
1851 asd->params.css_update_params_needed = true;
1854 case ATOMISP_IOC_G_ISP_PARM:
1855 err = atomisp_param(asd, 0, arg);
1858 case ATOMISP_IOC_S_ISP_PARM:
1859 err = atomisp_param(asd, 1, arg);
1862 case ATOMISP_IOC_G_3A_STAT:
1863 err = atomisp_3a_stat(asd, 0, arg);
1866 case ATOMISP_IOC_G_ISP_GAMMA:
1867 err = atomisp_gamma(asd, 0, arg);
1870 case ATOMISP_IOC_S_ISP_GAMMA:
1871 err = atomisp_gamma(asd, 1, arg);
1874 case ATOMISP_IOC_G_ISP_GDC_TAB:
1875 err = atomisp_gdc_cac_table(asd, 0, arg);
1878 case ATOMISP_IOC_S_ISP_GDC_TAB:
1879 err = atomisp_gdc_cac_table(asd, 1, arg);
1882 case ATOMISP_IOC_G_ISP_MACC:
1883 err = atomisp_macc_table(asd, 0, arg);
1886 case ATOMISP_IOC_S_ISP_MACC:
1887 err = atomisp_macc_table(asd, 1, arg);
1890 case ATOMISP_IOC_G_ISP_BAD_PIXEL_DETECTION:
1891 err = atomisp_bad_pixel_param(asd, 0, arg);
1894 case ATOMISP_IOC_S_ISP_BAD_PIXEL_DETECTION:
1895 err = atomisp_bad_pixel_param(asd, 1, arg);
1898 case ATOMISP_IOC_G_ISP_FALSE_COLOR_CORRECTION:
1899 err = atomisp_false_color_param(asd, 0, arg);
1902 case ATOMISP_IOC_S_ISP_FALSE_COLOR_CORRECTION:
1903 err = atomisp_false_color_param(asd, 1, arg);
1906 case ATOMISP_IOC_G_ISP_CTC:
1907 err = atomisp_ctc(asd, 0, arg);
1910 case ATOMISP_IOC_S_ISP_CTC:
1911 err = atomisp_ctc(asd, 1, arg);
1914 case ATOMISP_IOC_G_ISP_WHITE_BALANCE:
1915 err = atomisp_white_balance_param(asd, 0, arg);
1918 case ATOMISP_IOC_S_ISP_WHITE_BALANCE:
1919 err = atomisp_white_balance_param(asd, 1, arg);
1922 case ATOMISP_IOC_G_3A_CONFIG:
1923 err = atomisp_3a_config_param(asd, 0, arg);
1926 case ATOMISP_IOC_S_3A_CONFIG:
1927 err = atomisp_3a_config_param(asd, 1, arg);
1930 case ATOMISP_IOC_S_ISP_FPN_TABLE:
1931 err = atomisp_fixed_pattern_table(asd, arg);
1934 case ATOMISP_IOC_S_EXPOSURE:
1935 err = v4l2_subdev_call(isp->inputs[asd->input_curr].camera,
1936 core, ioctl, cmd, arg);
1939 case ATOMISP_IOC_S_ISP_SHD_TAB:
1940 err = atomisp_set_shading_table(asd, arg);
1943 case ATOMISP_IOC_G_ISP_GAMMA_CORRECTION:
1944 err = atomisp_gamma_correction(asd, 0, arg);
1947 case ATOMISP_IOC_S_ISP_GAMMA_CORRECTION:
1948 err = atomisp_gamma_correction(asd, 1, arg);
1951 case ATOMISP_IOC_S_PARAMETERS:
1952 err = atomisp_set_parameters(vdev, arg);
1955 case ATOMISP_IOC_EXT_ISP_CTRL:
1956 err = v4l2_subdev_call(isp->inputs[asd->input_curr].camera,
1957 core, ioctl, cmd, arg);
1959 case ATOMISP_IOC_EXP_ID_UNLOCK:
1960 err = atomisp_exp_id_unlock(asd, arg);
1962 case ATOMISP_IOC_EXP_ID_CAPTURE:
1963 err = atomisp_exp_id_capture(asd, arg);
1965 case ATOMISP_IOC_S_ENABLE_DZ_CAPT_PIPE:
1966 err = atomisp_enable_dz_capt_pipe(asd, arg);
1968 case ATOMISP_IOC_G_FORMATS_CONFIG:
1969 err = atomisp_formats(asd, 0, arg);
1972 case ATOMISP_IOC_S_FORMATS_CONFIG:
1973 err = atomisp_formats(asd, 1, arg);
1975 case ATOMISP_IOC_INJECT_A_FAKE_EVENT:
1976 err = atomisp_inject_a_fake_event(asd, arg);
1978 case ATOMISP_IOC_S_ARRAY_RESOLUTION:
1979 err = atomisp_set_array_res(asd, arg);
1989 const struct v4l2_ioctl_ops atomisp_ioctl_ops = {
1990 .vidioc_querycap = atomisp_querycap,
1991 .vidioc_enum_input = atomisp_enum_input,
1992 .vidioc_g_input = atomisp_g_input,
1993 .vidioc_s_input = atomisp_s_input,
1994 .vidioc_queryctrl = atomisp_queryctl,
1995 .vidioc_s_ctrl = atomisp_s_ctrl,
1996 .vidioc_g_ctrl = atomisp_g_ctrl,
1997 .vidioc_s_ext_ctrls = atomisp_s_ext_ctrls,
1998 .vidioc_g_ext_ctrls = atomisp_g_ext_ctrls,
1999 .vidioc_enum_framesizes = atomisp_enum_framesizes,
2000 .vidioc_enum_frameintervals = atomisp_enum_frameintervals,
2001 .vidioc_enum_fmt_vid_cap = atomisp_enum_fmt_cap,
2002 .vidioc_try_fmt_vid_cap = atomisp_try_fmt_cap,
2003 .vidioc_g_fmt_vid_cap = atomisp_g_fmt_cap,
2004 .vidioc_s_fmt_vid_cap = atomisp_s_fmt_cap,
2005 .vidioc_reqbufs = vb2_ioctl_reqbufs,
2006 .vidioc_querybuf = vb2_ioctl_querybuf,
2007 .vidioc_qbuf = atomisp_qbuf_wrapper,
2008 .vidioc_dqbuf = atomisp_dqbuf_wrapper,
2009 .vidioc_streamon = vb2_ioctl_streamon,
2010 .vidioc_streamoff = vb2_ioctl_streamoff,
2011 .vidioc_default = atomisp_vidioc_default,
2012 .vidioc_s_parm = atomisp_s_parm,
2013 .vidioc_g_parm = atomisp_g_parm,