Lines Matching +full:sd +full:- +full:lines

1 // SPDX-License-Identifier: GPL-2.0-only
16 #include <media/v4l2-ctrls.h>
17 #include <media/v4l2-fwnode.h>
18 #include <media/v4l2-subdev.h>
25 /* Lines per frame */
60 * struct imx412_reg - imx412 sensor register
70 * struct imx412_reg_list - imx412 sensor register list
80 * struct imx412_mode - imx412 sensor mode structure
84 * @hblank: Horizontal blanking in lines
85 * @vblank: Vertical blanking in lines
86 * @vblank_min: Minimum vertical blanking in lines
87 * @vblank_max: Maximum vertical blanking in lines
112 * struct imx412 - imx412 sensor device structure
115 * @sd: V4L2 sub-device
127 * @vblank: Vertical blanking in lines
134 struct v4l2_subdev sd; member
410 * to_imx412() - imx412 V4L2 sub-device to imx412 device.
411 * @subdev: pointer to imx412 V4L2 sub-device
417 return container_of(subdev, struct imx412, sd); in to_imx412()
421 * imx412_read_reg() - Read registers.
431 struct i2c_client *client = v4l2_get_subdevdata(&imx412->sd); in imx412_read_reg()
438 return -EINVAL; in imx412_read_reg()
443 msgs[0].addr = client->addr; in imx412_read_reg()
449 msgs[1].addr = client->addr; in imx412_read_reg()
452 msgs[1].buf = &data_buf[4 - len]; in imx412_read_reg()
454 ret = i2c_transfer(client->adapter, msgs, ARRAY_SIZE(msgs)); in imx412_read_reg()
456 return -EIO; in imx412_read_reg()
464 * imx412_write_reg() - Write register
474 struct i2c_client *client = v4l2_get_subdevdata(&imx412->sd); in imx412_write_reg()
478 return -EINVAL; in imx412_write_reg()
481 put_unaligned_be32(val << (8 * (4 - len)), buf + 2); in imx412_write_reg()
483 return -EIO; in imx412_write_reg()
489 * imx412_write_regs() - Write a list of registers
512 * imx412_update_controls() - Update control ranges based on streaming mode
523 ret = __v4l2_ctrl_s_ctrl(imx412->link_freq_ctrl, mode->link_freq_idx); in imx412_update_controls()
527 ret = __v4l2_ctrl_s_ctrl(imx412->hblank_ctrl, mode->hblank); in imx412_update_controls()
531 return __v4l2_ctrl_modify_range(imx412->vblank_ctrl, mode->vblank_min, in imx412_update_controls()
532 mode->vblank_max, 1, mode->vblank); in imx412_update_controls()
536 * imx412_update_exp_gain() - Set updated exposure and gain
548 lpfr = imx412->vblank + imx412->cur_mode->height; in imx412_update_exp_gain()
550 dev_dbg(imx412->dev, "Set exp %u, analog gain %u, lpfr %u", in imx412_update_exp_gain()
574 * imx412_set_ctrl() - Set subdevice control
578 * - V4L2_CID_VBLANK
579 * - cluster controls:
580 * - V4L2_CID_ANALOGUE_GAIN
581 * - V4L2_CID_EXPOSURE
588 container_of(ctrl->handler, struct imx412, ctrl_handler); in imx412_set_ctrl()
593 switch (ctrl->id) { in imx412_set_ctrl()
595 imx412->vblank = imx412->vblank_ctrl->val; in imx412_set_ctrl()
597 dev_dbg(imx412->dev, "Received vblank %u, new lpfr %u", in imx412_set_ctrl()
598 imx412->vblank, in imx412_set_ctrl()
599 imx412->vblank + imx412->cur_mode->height); in imx412_set_ctrl()
601 ret = __v4l2_ctrl_modify_range(imx412->exp_ctrl, in imx412_set_ctrl()
603 imx412->vblank + in imx412_set_ctrl()
604 imx412->cur_mode->height - in imx412_set_ctrl()
610 if (!pm_runtime_get_if_in_use(imx412->dev)) in imx412_set_ctrl()
613 exposure = ctrl->val; in imx412_set_ctrl()
614 analog_gain = imx412->again_ctrl->val; in imx412_set_ctrl()
616 dev_dbg(imx412->dev, "Received exp %u, analog gain %u", in imx412_set_ctrl()
621 pm_runtime_put(imx412->dev); in imx412_set_ctrl()
625 dev_err(imx412->dev, "Invalid control %d", ctrl->id); in imx412_set_ctrl()
626 ret = -EINVAL; in imx412_set_ctrl()
638 * imx412_enum_mbus_code() - Enumerate V4L2 sub-device mbus codes
639 * @sd: pointer to imx412 V4L2 sub-device structure
640 * @sd_state: V4L2 sub-device configuration
641 * @code: V4L2 sub-device code enumeration need to be filled
645 static int imx412_enum_mbus_code(struct v4l2_subdev *sd, in imx412_enum_mbus_code() argument
649 if (code->index > 0) in imx412_enum_mbus_code()
650 return -EINVAL; in imx412_enum_mbus_code()
652 code->code = supported_mode.code; in imx412_enum_mbus_code()
658 * imx412_enum_frame_size() - Enumerate V4L2 sub-device frame sizes
659 * @sd: pointer to imx412 V4L2 sub-device structure
660 * @sd_state: V4L2 sub-device configuration
661 * @fsize: V4L2 sub-device size enumeration need to be filled
665 static int imx412_enum_frame_size(struct v4l2_subdev *sd, in imx412_enum_frame_size() argument
669 if (fsize->index > 0) in imx412_enum_frame_size()
670 return -EINVAL; in imx412_enum_frame_size()
672 if (fsize->code != supported_mode.code) in imx412_enum_frame_size()
673 return -EINVAL; in imx412_enum_frame_size()
675 fsize->min_width = supported_mode.width; in imx412_enum_frame_size()
676 fsize->max_width = fsize->min_width; in imx412_enum_frame_size()
677 fsize->min_height = supported_mode.height; in imx412_enum_frame_size()
678 fsize->max_height = fsize->min_height; in imx412_enum_frame_size()
684 * imx412_fill_pad_format() - Fill subdevice pad format
688 * @fmt: V4L2 sub-device format need to be filled
694 fmt->format.width = mode->width; in imx412_fill_pad_format()
695 fmt->format.height = mode->height; in imx412_fill_pad_format()
696 fmt->format.code = mode->code; in imx412_fill_pad_format()
697 fmt->format.field = V4L2_FIELD_NONE; in imx412_fill_pad_format()
698 fmt->format.colorspace = V4L2_COLORSPACE_RAW; in imx412_fill_pad_format()
699 fmt->format.ycbcr_enc = V4L2_YCBCR_ENC_DEFAULT; in imx412_fill_pad_format()
700 fmt->format.quantization = V4L2_QUANTIZATION_DEFAULT; in imx412_fill_pad_format()
701 fmt->format.xfer_func = V4L2_XFER_FUNC_NONE; in imx412_fill_pad_format()
705 * imx412_get_pad_format() - Get subdevice pad format
706 * @sd: pointer to imx412 V4L2 sub-device structure
707 * @sd_state: V4L2 sub-device configuration
708 * @fmt: V4L2 sub-device format need to be set
712 static int imx412_get_pad_format(struct v4l2_subdev *sd, in imx412_get_pad_format() argument
716 struct imx412 *imx412 = to_imx412(sd); in imx412_get_pad_format()
718 mutex_lock(&imx412->mutex); in imx412_get_pad_format()
720 if (fmt->which == V4L2_SUBDEV_FORMAT_TRY) { in imx412_get_pad_format()
723 framefmt = v4l2_subdev_state_get_format(sd_state, fmt->pad); in imx412_get_pad_format()
724 fmt->format = *framefmt; in imx412_get_pad_format()
726 imx412_fill_pad_format(imx412, imx412->cur_mode, fmt); in imx412_get_pad_format()
729 mutex_unlock(&imx412->mutex); in imx412_get_pad_format()
735 * imx412_set_pad_format() - Set subdevice pad format
736 * @sd: pointer to imx412 V4L2 sub-device structure
737 * @sd_state: V4L2 sub-device configuration
738 * @fmt: V4L2 sub-device format need to be set
742 static int imx412_set_pad_format(struct v4l2_subdev *sd, in imx412_set_pad_format() argument
746 struct imx412 *imx412 = to_imx412(sd); in imx412_set_pad_format()
750 mutex_lock(&imx412->mutex); in imx412_set_pad_format()
755 if (fmt->which == V4L2_SUBDEV_FORMAT_TRY) { in imx412_set_pad_format()
758 framefmt = v4l2_subdev_state_get_format(sd_state, fmt->pad); in imx412_set_pad_format()
759 *framefmt = fmt->format; in imx412_set_pad_format()
763 imx412->cur_mode = mode; in imx412_set_pad_format()
766 mutex_unlock(&imx412->mutex); in imx412_set_pad_format()
772 * imx412_init_state() - Initialize sub-device state
773 * @sd: pointer to imx412 V4L2 sub-device structure
774 * @sd_state: V4L2 sub-device configuration
778 static int imx412_init_state(struct v4l2_subdev *sd, in imx412_init_state() argument
781 struct imx412 *imx412 = to_imx412(sd); in imx412_init_state()
787 return imx412_set_pad_format(sd, sd_state, &fmt); in imx412_init_state()
791 * imx412_start_streaming() - Start sensor stream
802 reg_list = &imx412->cur_mode->reg_list; in imx412_start_streaming()
803 ret = imx412_write_regs(imx412, reg_list->regs, in imx412_start_streaming()
804 reg_list->num_of_regs); in imx412_start_streaming()
806 dev_err(imx412->dev, "fail to write initial registers"); in imx412_start_streaming()
811 ret = __v4l2_ctrl_handler_setup(imx412->sd.ctrl_handler); in imx412_start_streaming()
813 dev_err(imx412->dev, "fail to setup handler"); in imx412_start_streaming()
824 dev_err(imx412->dev, "fail to start streaming"); in imx412_start_streaming()
832 * imx412_stop_streaming() - Stop sensor stream
844 * imx412_set_stream() - Enable sensor streaming
845 * @sd: pointer to imx412 subdevice
850 static int imx412_set_stream(struct v4l2_subdev *sd, int enable) in imx412_set_stream() argument
852 struct imx412 *imx412 = to_imx412(sd); in imx412_set_stream()
855 mutex_lock(&imx412->mutex); in imx412_set_stream()
858 ret = pm_runtime_resume_and_get(imx412->dev); in imx412_set_stream()
867 pm_runtime_put(imx412->dev); in imx412_set_stream()
870 mutex_unlock(&imx412->mutex); in imx412_set_stream()
875 pm_runtime_put(imx412->dev); in imx412_set_stream()
877 mutex_unlock(&imx412->mutex); in imx412_set_stream()
883 * imx412_detect() - Detect imx412 sensor
886 * Return: 0 if successful, -EIO if sensor id does not match
898 dev_err(imx412->dev, "chip id mismatch: %x!=%x", in imx412_detect()
900 return -ENXIO; in imx412_detect()
907 * imx412_parse_hw_config() - Parse HW configuration and check if supported
914 struct fwnode_handle *fwnode = dev_fwnode(imx412->dev); in imx412_parse_hw_config()
924 return -ENXIO; in imx412_parse_hw_config()
927 imx412->reset_gpio = devm_gpiod_get_optional(imx412->dev, "reset", in imx412_parse_hw_config()
929 if (IS_ERR(imx412->reset_gpio)) { in imx412_parse_hw_config()
930 dev_err(imx412->dev, "failed to get reset gpio %ld", in imx412_parse_hw_config()
931 PTR_ERR(imx412->reset_gpio)); in imx412_parse_hw_config()
932 return PTR_ERR(imx412->reset_gpio); in imx412_parse_hw_config()
936 imx412->inclk = devm_clk_get(imx412->dev, NULL); in imx412_parse_hw_config()
937 if (IS_ERR(imx412->inclk)) { in imx412_parse_hw_config()
938 dev_err(imx412->dev, "could not get inclk"); in imx412_parse_hw_config()
939 return PTR_ERR(imx412->inclk); in imx412_parse_hw_config()
942 rate = clk_get_rate(imx412->inclk); in imx412_parse_hw_config()
944 dev_err(imx412->dev, "inclk frequency mismatch"); in imx412_parse_hw_config()
945 return -EINVAL; in imx412_parse_hw_config()
950 imx412->supplies[i].supply = imx412_supply_names[i]; in imx412_parse_hw_config()
952 ret = devm_regulator_bulk_get(imx412->dev, in imx412_parse_hw_config()
954 imx412->supplies); in imx412_parse_hw_config()
960 return -ENXIO; in imx412_parse_hw_config()
968 dev_err(imx412->dev, in imx412_parse_hw_config()
971 ret = -EINVAL; in imx412_parse_hw_config()
976 dev_err(imx412->dev, "no link frequencies defined"); in imx412_parse_hw_config()
977 ret = -EINVAL; in imx412_parse_hw_config()
985 ret = -EINVAL; in imx412_parse_hw_config()
1015 * imx412_power_on() - Sensor power on sequence
1022 struct v4l2_subdev *sd = dev_get_drvdata(dev); in imx412_power_on() local
1023 struct imx412 *imx412 = to_imx412(sd); in imx412_power_on()
1027 imx412->supplies); in imx412_power_on()
1033 gpiod_set_value_cansleep(imx412->reset_gpio, 0); in imx412_power_on()
1035 ret = clk_prepare_enable(imx412->inclk); in imx412_power_on()
1037 dev_err(imx412->dev, "fail to enable inclk"); in imx412_power_on()
1046 gpiod_set_value_cansleep(imx412->reset_gpio, 1); in imx412_power_on()
1048 imx412->supplies); in imx412_power_on()
1054 * imx412_power_off() - Sensor power off sequence
1061 struct v4l2_subdev *sd = dev_get_drvdata(dev); in imx412_power_off() local
1062 struct imx412 *imx412 = to_imx412(sd); in imx412_power_off()
1064 clk_disable_unprepare(imx412->inclk); in imx412_power_off()
1066 gpiod_set_value_cansleep(imx412->reset_gpio, 1); in imx412_power_off()
1069 imx412->supplies); in imx412_power_off()
1075 * imx412_init_controls() - Initialize sensor subdevice controls
1082 struct v4l2_ctrl_handler *ctrl_hdlr = &imx412->ctrl_handler; in imx412_init_controls()
1083 const struct imx412_mode *mode = imx412->cur_mode; in imx412_init_controls()
1092 ctrl_hdlr->lock = &imx412->mutex; in imx412_init_controls()
1095 lpfr = mode->vblank + mode->height; in imx412_init_controls()
1096 imx412->exp_ctrl = v4l2_ctrl_new_std(ctrl_hdlr, in imx412_init_controls()
1100 lpfr - IMX412_EXPOSURE_OFFSET, in imx412_init_controls()
1104 imx412->again_ctrl = v4l2_ctrl_new_std(ctrl_hdlr, in imx412_init_controls()
1112 v4l2_ctrl_cluster(2, &imx412->exp_ctrl); in imx412_init_controls()
1114 imx412->vblank_ctrl = v4l2_ctrl_new_std(ctrl_hdlr, in imx412_init_controls()
1117 mode->vblank_min, in imx412_init_controls()
1118 mode->vblank_max, in imx412_init_controls()
1119 1, mode->vblank); in imx412_init_controls()
1122 imx412->pclk_ctrl = v4l2_ctrl_new_std(ctrl_hdlr, in imx412_init_controls()
1125 mode->pclk, mode->pclk, in imx412_init_controls()
1126 1, mode->pclk); in imx412_init_controls()
1128 imx412->link_freq_ctrl = v4l2_ctrl_new_int_menu(ctrl_hdlr, in imx412_init_controls()
1131 ARRAY_SIZE(link_freq) - in imx412_init_controls()
1133 mode->link_freq_idx, in imx412_init_controls()
1135 if (imx412->link_freq_ctrl) in imx412_init_controls()
1136 imx412->link_freq_ctrl->flags |= V4L2_CTRL_FLAG_READ_ONLY; in imx412_init_controls()
1138 imx412->hblank_ctrl = v4l2_ctrl_new_std(ctrl_hdlr, in imx412_init_controls()
1143 1, mode->hblank); in imx412_init_controls()
1144 if (imx412->hblank_ctrl) in imx412_init_controls()
1145 imx412->hblank_ctrl->flags |= V4L2_CTRL_FLAG_READ_ONLY; in imx412_init_controls()
1147 if (ctrl_hdlr->error) { in imx412_init_controls()
1148 dev_err(imx412->dev, "control init failed: %d", in imx412_init_controls()
1149 ctrl_hdlr->error); in imx412_init_controls()
1151 return ctrl_hdlr->error; in imx412_init_controls()
1154 imx412->sd.ctrl_handler = ctrl_hdlr; in imx412_init_controls()
1160 * imx412_probe() - I2C client device binding
1171 imx412 = devm_kzalloc(&client->dev, sizeof(*imx412), GFP_KERNEL); in imx412_probe()
1173 return -ENOMEM; in imx412_probe()
1175 imx412->dev = &client->dev; in imx412_probe()
1176 name = device_get_match_data(&client->dev); in imx412_probe()
1178 return -ENODEV; in imx412_probe()
1181 v4l2_i2c_subdev_init(&imx412->sd, client, &imx412_subdev_ops); in imx412_probe()
1182 imx412->sd.internal_ops = &imx412_internal_ops; in imx412_probe()
1186 dev_err(imx412->dev, "HW configuration is not supported"); in imx412_probe()
1190 mutex_init(&imx412->mutex); in imx412_probe()
1192 ret = imx412_power_on(imx412->dev); in imx412_probe()
1194 dev_err(imx412->dev, "failed to power-on the sensor"); in imx412_probe()
1201 dev_err(imx412->dev, "failed to find sensor: %d", ret); in imx412_probe()
1206 imx412->cur_mode = &supported_mode; in imx412_probe()
1207 imx412->vblank = imx412->cur_mode->vblank; in imx412_probe()
1211 dev_err(imx412->dev, "failed to init controls: %d", ret); in imx412_probe()
1216 imx412->sd.flags |= V4L2_SUBDEV_FL_HAS_DEVNODE; in imx412_probe()
1217 imx412->sd.entity.function = MEDIA_ENT_F_CAM_SENSOR; in imx412_probe()
1219 v4l2_i2c_subdev_set_name(&imx412->sd, client, name, NULL); 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
1262 struct v4l2_subdev *sd = i2c_get_clientdata(client); in imx412_remove() local
1263 struct imx412 *imx412 = to_imx412(sd); in imx412_remove()
1265 v4l2_async_unregister_subdev(sd); in imx412_remove()
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()