Lines Matching +full:sensor +full:- +full:gain

1 // SPDX-License-Identifier: GPL-2.0-only
3 * Sony imx412 Camera Sensor Driver
16 #include <media/v4l2-ctrls.h>
17 #include <media/v4l2-fwnode.h>
18 #include <media/v4l2-subdev.h>
39 /* Analog gain control */
60 * struct imx412_reg - imx412 sensor register
70 * struct imx412_reg_list - imx412 sensor register list
80 * struct imx412_mode - imx412 sensor mode structure
88 * @pclk: Sensor pixel clock
90 * @reg_list: Register list for sensor mode
112 * struct imx412 - imx412 sensor device structure
115 * @sd: V4L2 sub-device
117 * @reset_gpio: Sensor reset gpio
118 * @inclk: Sensor input clock
126 * @again_ctrl: Pointer to analog gain control
128 * @cur_mode: Pointer to current selected sensor mode
129 * @mutex: Mutex for serializing sensor controls
159 /* Sensor mode registers */
394 /* Supported sensor mode configurations */
412 * to_imx412() - imx412 V4L2 sub-device to imx412 device.
413 * @subdev: pointer to imx412 V4L2 sub-device
423 * imx412_read_reg() - Read registers.
433 struct i2c_client *client = v4l2_get_subdevdata(&imx412->sd); in imx412_read_reg()
440 return -EINVAL; in imx412_read_reg()
445 msgs[0].addr = client->addr; in imx412_read_reg()
451 msgs[1].addr = client->addr; in imx412_read_reg()
454 msgs[1].buf = &data_buf[4 - len]; in imx412_read_reg()
456 ret = i2c_transfer(client->adapter, msgs, ARRAY_SIZE(msgs)); in imx412_read_reg()
458 return -EIO; in imx412_read_reg()
466 * imx412_write_reg() - Write register
476 struct i2c_client *client = v4l2_get_subdevdata(&imx412->sd); in imx412_write_reg()
480 return -EINVAL; in imx412_write_reg()
483 put_unaligned_be32(val << (8 * (4 - len)), buf + 2); in imx412_write_reg()
485 return -EIO; in imx412_write_reg()
491 * imx412_write_regs() - Write a list of registers
514 * imx412_update_controls() - Update control ranges based on streaming mode
516 * @mode: pointer to imx412_mode sensor mode
525 ret = __v4l2_ctrl_s_ctrl(imx412->link_freq_ctrl, mode->link_freq_idx); in imx412_update_controls()
529 ret = __v4l2_ctrl_s_ctrl(imx412->hblank_ctrl, mode->hblank); in imx412_update_controls()
533 return __v4l2_ctrl_modify_range(imx412->vblank_ctrl, mode->vblank_min, in imx412_update_controls()
534 mode->vblank_max, 1, mode->vblank); in imx412_update_controls()
538 * imx412_update_exp_gain() - Set updated exposure and gain
541 * @gain: updated analog gain value
545 static int imx412_update_exp_gain(struct imx412 *imx412, u32 exposure, u32 gain) in imx412_update_exp_gain() argument
550 lpfr = imx412->vblank + imx412->cur_mode->height; in imx412_update_exp_gain()
551 shutter = lpfr - exposure; in imx412_update_exp_gain()
553 dev_dbg(imx412->dev, "Set exp %u, analog gain %u, shutter %u, lpfr %u", in imx412_update_exp_gain()
554 exposure, gain, shutter, lpfr); in imx412_update_exp_gain()
568 ret = imx412_write_reg(imx412, IMX412_REG_AGAIN, 2, gain); in imx412_update_exp_gain()
577 * imx412_set_ctrl() - Set subdevice control
581 * - V4L2_CID_VBLANK
582 * - cluster controls:
583 * - V4L2_CID_ANALOGUE_GAIN
584 * - V4L2_CID_EXPOSURE
591 container_of(ctrl->handler, struct imx412, ctrl_handler); in imx412_set_ctrl()
596 switch (ctrl->id) { in imx412_set_ctrl()
598 imx412->vblank = imx412->vblank_ctrl->val; in imx412_set_ctrl()
600 dev_dbg(imx412->dev, "Received vblank %u, new lpfr %u", in imx412_set_ctrl()
601 imx412->vblank, in imx412_set_ctrl()
602 imx412->vblank + imx412->cur_mode->height); in imx412_set_ctrl()
604 ret = __v4l2_ctrl_modify_range(imx412->exp_ctrl, in imx412_set_ctrl()
606 imx412->vblank + in imx412_set_ctrl()
607 imx412->cur_mode->height - in imx412_set_ctrl()
612 /* Set controls only if sensor is in power on state */ in imx412_set_ctrl()
613 if (!pm_runtime_get_if_in_use(imx412->dev)) in imx412_set_ctrl()
616 exposure = ctrl->val; in imx412_set_ctrl()
617 analog_gain = imx412->again_ctrl->val; in imx412_set_ctrl()
619 dev_dbg(imx412->dev, "Received exp %u, analog gain %u", in imx412_set_ctrl()
624 pm_runtime_put(imx412->dev); in imx412_set_ctrl()
628 dev_err(imx412->dev, "Invalid control %d", ctrl->id); in imx412_set_ctrl()
629 ret = -EINVAL; in imx412_set_ctrl()
641 * imx412_enum_mbus_code() - Enumerate V4L2 sub-device mbus codes
642 * @sd: pointer to imx412 V4L2 sub-device structure
643 * @sd_state: V4L2 sub-device configuration
644 * @code: V4L2 sub-device code enumeration need to be filled
652 if (code->index > 0) in imx412_enum_mbus_code()
653 return -EINVAL; in imx412_enum_mbus_code()
655 code->code = supported_mode.code; in imx412_enum_mbus_code()
661 * imx412_enum_frame_size() - Enumerate V4L2 sub-device frame sizes
662 * @sd: pointer to imx412 V4L2 sub-device structure
663 * @sd_state: V4L2 sub-device configuration
664 * @fsize: V4L2 sub-device size enumeration need to be filled
672 if (fsize->index > 0) in imx412_enum_frame_size()
673 return -EINVAL; in imx412_enum_frame_size()
675 if (fsize->code != supported_mode.code) in imx412_enum_frame_size()
676 return -EINVAL; in imx412_enum_frame_size()
678 fsize->min_width = supported_mode.width; in imx412_enum_frame_size()
679 fsize->max_width = fsize->min_width; in imx412_enum_frame_size()
680 fsize->min_height = supported_mode.height; in imx412_enum_frame_size()
681 fsize->max_height = fsize->min_height; in imx412_enum_frame_size()
687 * imx412_fill_pad_format() - Fill subdevice pad format
688 * from selected sensor mode
690 * @mode: pointer to imx412_mode sensor mode
691 * @fmt: V4L2 sub-device format need to be filled
697 fmt->format.width = mode->width; in imx412_fill_pad_format()
698 fmt->format.height = mode->height; in imx412_fill_pad_format()
699 fmt->format.code = mode->code; in imx412_fill_pad_format()
700 fmt->format.field = V4L2_FIELD_NONE; in imx412_fill_pad_format()
701 fmt->format.colorspace = V4L2_COLORSPACE_RAW; in imx412_fill_pad_format()
702 fmt->format.ycbcr_enc = V4L2_YCBCR_ENC_DEFAULT; in imx412_fill_pad_format()
703 fmt->format.quantization = V4L2_QUANTIZATION_DEFAULT; in imx412_fill_pad_format()
704 fmt->format.xfer_func = V4L2_XFER_FUNC_NONE; in imx412_fill_pad_format()
708 * imx412_get_pad_format() - Get subdevice pad format
709 * @sd: pointer to imx412 V4L2 sub-device structure
710 * @sd_state: V4L2 sub-device configuration
711 * @fmt: V4L2 sub-device format need to be set
721 mutex_lock(&imx412->mutex); in imx412_get_pad_format()
723 if (fmt->which == V4L2_SUBDEV_FORMAT_TRY) { in imx412_get_pad_format()
726 framefmt = v4l2_subdev_get_try_format(sd, sd_state, fmt->pad); in imx412_get_pad_format()
727 fmt->format = *framefmt; in imx412_get_pad_format()
729 imx412_fill_pad_format(imx412, imx412->cur_mode, fmt); in imx412_get_pad_format()
732 mutex_unlock(&imx412->mutex); in imx412_get_pad_format()
738 * imx412_set_pad_format() - Set subdevice pad format
739 * @sd: pointer to imx412 V4L2 sub-device structure
740 * @sd_state: V4L2 sub-device configuration
741 * @fmt: V4L2 sub-device format need to be set
753 mutex_lock(&imx412->mutex); in imx412_set_pad_format()
758 if (fmt->which == V4L2_SUBDEV_FORMAT_TRY) { in imx412_set_pad_format()
761 framefmt = v4l2_subdev_get_try_format(sd, sd_state, fmt->pad); in imx412_set_pad_format()
762 *framefmt = fmt->format; in imx412_set_pad_format()
766 imx412->cur_mode = mode; in imx412_set_pad_format()
769 mutex_unlock(&imx412->mutex); in imx412_set_pad_format()
775 * imx412_init_pad_cfg() - Initialize sub-device pad configuration
776 * @sd: pointer to imx412 V4L2 sub-device structure
777 * @sd_state: V4L2 sub-device configuration
794 * imx412_start_streaming() - Start sensor stream
804 /* Write sensor mode registers */ in imx412_start_streaming()
805 reg_list = &imx412->cur_mode->reg_list; in imx412_start_streaming()
806 ret = imx412_write_regs(imx412, reg_list->regs, in imx412_start_streaming()
807 reg_list->num_of_regs); in imx412_start_streaming()
809 dev_err(imx412->dev, "fail to write initial registers"); in imx412_start_streaming()
813 /* Setup handler will write actual exposure and gain */ in imx412_start_streaming()
814 ret = __v4l2_ctrl_handler_setup(imx412->sd.ctrl_handler); in imx412_start_streaming()
816 dev_err(imx412->dev, "fail to setup handler"); in imx412_start_streaming()
827 dev_err(imx412->dev, "fail to start streaming"); in imx412_start_streaming()
835 * imx412_stop_streaming() - Stop sensor stream
847 * imx412_set_stream() - Enable sensor streaming
849 * @enable: set to enable sensor streaming
858 mutex_lock(&imx412->mutex); in imx412_set_stream()
860 if (imx412->streaming == enable) { in imx412_set_stream()
861 mutex_unlock(&imx412->mutex); in imx412_set_stream()
866 ret = pm_runtime_resume_and_get(imx412->dev); in imx412_set_stream()
875 pm_runtime_put(imx412->dev); in imx412_set_stream()
878 imx412->streaming = enable; in imx412_set_stream()
880 mutex_unlock(&imx412->mutex); in imx412_set_stream()
885 pm_runtime_put(imx412->dev); in imx412_set_stream()
887 mutex_unlock(&imx412->mutex); in imx412_set_stream()
893 * imx412_detect() - Detect imx412 sensor
896 * Return: 0 if successful, -EIO if sensor id does not match
908 dev_err(imx412->dev, "chip id mismatch: %x!=%x", in imx412_detect()
910 return -ENXIO; in imx412_detect()
917 * imx412_parse_hw_config() - Parse HW configuration and check if supported
924 struct fwnode_handle *fwnode = dev_fwnode(imx412->dev); in imx412_parse_hw_config()
934 return -ENXIO; in imx412_parse_hw_config()
937 imx412->reset_gpio = devm_gpiod_get_optional(imx412->dev, "reset", in imx412_parse_hw_config()
939 if (IS_ERR(imx412->reset_gpio)) { in imx412_parse_hw_config()
940 dev_err(imx412->dev, "failed to get reset gpio %ld", in imx412_parse_hw_config()
941 PTR_ERR(imx412->reset_gpio)); in imx412_parse_hw_config()
942 return PTR_ERR(imx412->reset_gpio); in imx412_parse_hw_config()
945 /* Get sensor input clock */ in imx412_parse_hw_config()
946 imx412->inclk = devm_clk_get(imx412->dev, NULL); in imx412_parse_hw_config()
947 if (IS_ERR(imx412->inclk)) { in imx412_parse_hw_config()
948 dev_err(imx412->dev, "could not get inclk"); in imx412_parse_hw_config()
949 return PTR_ERR(imx412->inclk); in imx412_parse_hw_config()
952 rate = clk_get_rate(imx412->inclk); in imx412_parse_hw_config()
954 dev_err(imx412->dev, "inclk frequency mismatch"); in imx412_parse_hw_config()
955 return -EINVAL; in imx412_parse_hw_config()
960 imx412->supplies[i].supply = imx412_supply_names[i]; in imx412_parse_hw_config()
962 ret = devm_regulator_bulk_get(imx412->dev, in imx412_parse_hw_config()
964 imx412->supplies); in imx412_parse_hw_config()
970 return -ENXIO; in imx412_parse_hw_config()
978 dev_err(imx412->dev, in imx412_parse_hw_config()
981 ret = -EINVAL; in imx412_parse_hw_config()
986 dev_err(imx412->dev, "no link frequencies defined"); in imx412_parse_hw_config()
987 ret = -EINVAL; in imx412_parse_hw_config()
995 ret = -EINVAL; in imx412_parse_hw_config()
1022 * imx412_power_on() - Sensor power on sequence
1034 imx412->supplies); in imx412_power_on()
1040 gpiod_set_value_cansleep(imx412->reset_gpio, 0); in imx412_power_on()
1042 ret = clk_prepare_enable(imx412->inclk); in imx412_power_on()
1044 dev_err(imx412->dev, "fail to enable inclk"); in imx412_power_on()
1053 gpiod_set_value_cansleep(imx412->reset_gpio, 1); in imx412_power_on()
1055 imx412->supplies); in imx412_power_on()
1061 * imx412_power_off() - Sensor power off sequence
1071 clk_disable_unprepare(imx412->inclk); in imx412_power_off()
1073 gpiod_set_value_cansleep(imx412->reset_gpio, 1); in imx412_power_off()
1076 imx412->supplies); in imx412_power_off()
1082 * imx412_init_controls() - Initialize sensor subdevice controls
1089 struct v4l2_ctrl_handler *ctrl_hdlr = &imx412->ctrl_handler; in imx412_init_controls()
1090 const struct imx412_mode *mode = imx412->cur_mode; in imx412_init_controls()
1098 /* Serialize controls with sensor device */ in imx412_init_controls()
1099 ctrl_hdlr->lock = &imx412->mutex; in imx412_init_controls()
1101 /* Initialize exposure and gain */ in imx412_init_controls()
1102 lpfr = mode->vblank + mode->height; in imx412_init_controls()
1103 imx412->exp_ctrl = v4l2_ctrl_new_std(ctrl_hdlr, in imx412_init_controls()
1107 lpfr - IMX412_EXPOSURE_OFFSET, in imx412_init_controls()
1111 imx412->again_ctrl = v4l2_ctrl_new_std(ctrl_hdlr, in imx412_init_controls()
1119 v4l2_ctrl_cluster(2, &imx412->exp_ctrl); in imx412_init_controls()
1121 imx412->vblank_ctrl = v4l2_ctrl_new_std(ctrl_hdlr, in imx412_init_controls()
1124 mode->vblank_min, in imx412_init_controls()
1125 mode->vblank_max, in imx412_init_controls()
1126 1, mode->vblank); in imx412_init_controls()
1129 imx412->pclk_ctrl = v4l2_ctrl_new_std(ctrl_hdlr, in imx412_init_controls()
1132 mode->pclk, mode->pclk, in imx412_init_controls()
1133 1, mode->pclk); in imx412_init_controls()
1135 imx412->link_freq_ctrl = v4l2_ctrl_new_int_menu(ctrl_hdlr, in imx412_init_controls()
1138 ARRAY_SIZE(link_freq) - in imx412_init_controls()
1140 mode->link_freq_idx, in imx412_init_controls()
1142 if (imx412->link_freq_ctrl) in imx412_init_controls()
1143 imx412->link_freq_ctrl->flags |= V4L2_CTRL_FLAG_READ_ONLY; in imx412_init_controls()
1145 imx412->hblank_ctrl = v4l2_ctrl_new_std(ctrl_hdlr, in imx412_init_controls()
1150 1, mode->hblank); in imx412_init_controls()
1151 if (imx412->hblank_ctrl) in imx412_init_controls()
1152 imx412->hblank_ctrl->flags |= V4L2_CTRL_FLAG_READ_ONLY; in imx412_init_controls()
1154 if (ctrl_hdlr->error) { in imx412_init_controls()
1155 dev_err(imx412->dev, "control init failed: %d", in imx412_init_controls()
1156 ctrl_hdlr->error); in imx412_init_controls()
1158 return ctrl_hdlr->error; in imx412_init_controls()
1161 imx412->sd.ctrl_handler = ctrl_hdlr; in imx412_init_controls()
1167 * imx412_probe() - I2C client device binding
1177 imx412 = devm_kzalloc(&client->dev, sizeof(*imx412), GFP_KERNEL); in imx412_probe()
1179 return -ENOMEM; in imx412_probe()
1181 imx412->dev = &client->dev; in imx412_probe()
1184 v4l2_i2c_subdev_init(&imx412->sd, client, &imx412_subdev_ops); in imx412_probe()
1188 dev_err(imx412->dev, "HW configuration is not supported"); in imx412_probe()
1192 mutex_init(&imx412->mutex); in imx412_probe()
1194 ret = imx412_power_on(imx412->dev); in imx412_probe()
1196 dev_err(imx412->dev, "failed to power-on the sensor"); in imx412_probe()
1203 dev_err(imx412->dev, "failed to find sensor: %d", ret); in imx412_probe()
1208 imx412->cur_mode = &supported_mode; in imx412_probe()
1209 imx412->vblank = imx412->cur_mode->vblank; in imx412_probe()
1213 dev_err(imx412->dev, "failed to init controls: %d", ret); in imx412_probe()
1218 imx412->sd.flags |= V4L2_SUBDEV_FL_HAS_DEVNODE; in imx412_probe()
1219 imx412->sd.entity.function = MEDIA_ENT_F_CAM_SENSOR; in imx412_probe()
1222 imx412->pad.flags = MEDIA_PAD_FL_SOURCE; in imx412_probe()
1223 ret = media_entity_pads_init(&imx412->sd.entity, 1, &imx412->pad); in imx412_probe()
1225 dev_err(imx412->dev, "failed to init entity pads: %d", ret); in imx412_probe()
1229 ret = v4l2_async_register_subdev_sensor(&imx412->sd); in imx412_probe()
1231 dev_err(imx412->dev, in imx412_probe()
1236 pm_runtime_set_active(imx412->dev); in imx412_probe()
1237 pm_runtime_enable(imx412->dev); in imx412_probe()
1238 pm_runtime_idle(imx412->dev); in imx412_probe()
1243 media_entity_cleanup(&imx412->sd.entity); in imx412_probe()
1245 v4l2_ctrl_handler_free(imx412->sd.ctrl_handler); in imx412_probe()
1247 imx412_power_off(imx412->dev); in imx412_probe()
1249 mutex_destroy(&imx412->mutex); in imx412_probe()
1255 * imx412_remove() - I2C client device unbinding
1266 media_entity_cleanup(&sd->entity); in imx412_remove()
1267 v4l2_ctrl_handler_free(sd->ctrl_handler); in imx412_remove()
1269 pm_runtime_disable(&client->dev); in imx412_remove()
1270 if (!pm_runtime_status_suspended(&client->dev)) in imx412_remove()
1271 imx412_power_off(&client->dev); in imx412_remove()
1272 pm_runtime_set_suspended(&client->dev); in imx412_remove()
1274 mutex_destroy(&imx412->mutex); in imx412_remove()
1300 MODULE_DESCRIPTION("Sony imx412 sensor driver");