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
135 struct v4l2_subdev sd; member
412 * to_imx412() - imx412 V4L2 sub-device to imx412 device.
413 * @subdev: pointer to imx412 V4L2 sub-device
419 return container_of(subdev, struct imx412, sd); in to_imx412()
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
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
550 lpfr = imx412->vblank + imx412->cur_mode->height; in imx412_update_exp_gain()
552 dev_dbg(imx412->dev, "Set exp %u, analog gain %u, lpfr %u", in imx412_update_exp_gain()
576 * imx412_set_ctrl() - Set subdevice control
580 * - V4L2_CID_VBLANK
581 * - cluster controls:
582 * - V4L2_CID_ANALOGUE_GAIN
583 * - V4L2_CID_EXPOSURE
590 container_of(ctrl->handler, struct imx412, ctrl_handler); in imx412_set_ctrl()
595 switch (ctrl->id) { in imx412_set_ctrl()
597 imx412->vblank = imx412->vblank_ctrl->val; in imx412_set_ctrl()
599 dev_dbg(imx412->dev, "Received vblank %u, new lpfr %u", in imx412_set_ctrl()
600 imx412->vblank, in imx412_set_ctrl()
601 imx412->vblank + imx412->cur_mode->height); in imx412_set_ctrl()
603 ret = __v4l2_ctrl_modify_range(imx412->exp_ctrl, in imx412_set_ctrl()
605 imx412->vblank + in imx412_set_ctrl()
606 imx412->cur_mode->height - in imx412_set_ctrl()
612 if (!pm_runtime_get_if_in_use(imx412->dev)) in imx412_set_ctrl()
615 exposure = ctrl->val; in imx412_set_ctrl()
616 analog_gain = imx412->again_ctrl->val; in imx412_set_ctrl()
618 dev_dbg(imx412->dev, "Received exp %u, analog gain %u", in imx412_set_ctrl()
623 pm_runtime_put(imx412->dev); in imx412_set_ctrl()
627 dev_err(imx412->dev, "Invalid control %d", ctrl->id); in imx412_set_ctrl()
628 ret = -EINVAL; in imx412_set_ctrl()
640 * imx412_enum_mbus_code() - Enumerate V4L2 sub-device mbus codes
641 * @sd: pointer to imx412 V4L2 sub-device structure
642 * @sd_state: V4L2 sub-device configuration
643 * @code: V4L2 sub-device code enumeration need to be filled
647 static int imx412_enum_mbus_code(struct v4l2_subdev *sd, in imx412_enum_mbus_code() argument
651 if (code->index > 0) in imx412_enum_mbus_code()
652 return -EINVAL; in imx412_enum_mbus_code()
654 code->code = supported_mode.code; in imx412_enum_mbus_code()
660 * imx412_enum_frame_size() - Enumerate V4L2 sub-device frame sizes
661 * @sd: pointer to imx412 V4L2 sub-device structure
662 * @sd_state: V4L2 sub-device configuration
663 * @fsize: V4L2 sub-device size enumeration need to be filled
667 static int imx412_enum_frame_size(struct v4l2_subdev *sd, in imx412_enum_frame_size() argument
671 if (fsize->index > 0) in imx412_enum_frame_size()
672 return -EINVAL; in imx412_enum_frame_size()
674 if (fsize->code != supported_mode.code) in imx412_enum_frame_size()
675 return -EINVAL; in imx412_enum_frame_size()
677 fsize->min_width = supported_mode.width; in imx412_enum_frame_size()
678 fsize->max_width = fsize->min_width; in imx412_enum_frame_size()
679 fsize->min_height = supported_mode.height; in imx412_enum_frame_size()
680 fsize->max_height = fsize->min_height; in imx412_enum_frame_size()
686 * imx412_fill_pad_format() - Fill subdevice pad format
690 * @fmt: V4L2 sub-device format need to be filled
696 fmt->format.width = mode->width; in imx412_fill_pad_format()
697 fmt->format.height = mode->height; in imx412_fill_pad_format()
698 fmt->format.code = mode->code; in imx412_fill_pad_format()
699 fmt->format.field = V4L2_FIELD_NONE; in imx412_fill_pad_format()
700 fmt->format.colorspace = V4L2_COLORSPACE_RAW; in imx412_fill_pad_format()
701 fmt->format.ycbcr_enc = V4L2_YCBCR_ENC_DEFAULT; in imx412_fill_pad_format()
702 fmt->format.quantization = V4L2_QUANTIZATION_DEFAULT; in imx412_fill_pad_format()
703 fmt->format.xfer_func = V4L2_XFER_FUNC_NONE; in imx412_fill_pad_format()
707 * imx412_get_pad_format() - Get subdevice pad format
708 * @sd: pointer to imx412 V4L2 sub-device structure
709 * @sd_state: V4L2 sub-device configuration
710 * @fmt: V4L2 sub-device format need to be set
714 static int imx412_get_pad_format(struct v4l2_subdev *sd, in imx412_get_pad_format() argument
718 struct imx412 *imx412 = to_imx412(sd); in imx412_get_pad_format()
720 mutex_lock(&imx412->mutex); in imx412_get_pad_format()
722 if (fmt->which == V4L2_SUBDEV_FORMAT_TRY) { in imx412_get_pad_format()
725 framefmt = v4l2_subdev_get_try_format(sd, sd_state, fmt->pad); in imx412_get_pad_format()
726 fmt->format = *framefmt; in imx412_get_pad_format()
728 imx412_fill_pad_format(imx412, imx412->cur_mode, fmt); in imx412_get_pad_format()
731 mutex_unlock(&imx412->mutex); in imx412_get_pad_format()
737 * imx412_set_pad_format() - Set subdevice pad format
738 * @sd: pointer to imx412 V4L2 sub-device structure
739 * @sd_state: V4L2 sub-device configuration
740 * @fmt: V4L2 sub-device format need to be set
744 static int imx412_set_pad_format(struct v4l2_subdev *sd, in imx412_set_pad_format() argument
748 struct imx412 *imx412 = to_imx412(sd); in imx412_set_pad_format()
752 mutex_lock(&imx412->mutex); in imx412_set_pad_format()
757 if (fmt->which == V4L2_SUBDEV_FORMAT_TRY) { in imx412_set_pad_format()
760 framefmt = v4l2_subdev_get_try_format(sd, sd_state, fmt->pad); in imx412_set_pad_format()
761 *framefmt = fmt->format; in imx412_set_pad_format()
765 imx412->cur_mode = mode; in imx412_set_pad_format()
768 mutex_unlock(&imx412->mutex); in imx412_set_pad_format()
774 * imx412_init_pad_cfg() - Initialize sub-device pad configuration
775 * @sd: pointer to imx412 V4L2 sub-device structure
776 * @sd_state: V4L2 sub-device configuration
780 static int imx412_init_pad_cfg(struct v4l2_subdev *sd, in imx412_init_pad_cfg() argument
783 struct imx412 *imx412 = to_imx412(sd); in imx412_init_pad_cfg()
789 return imx412_set_pad_format(sd, sd_state, &fmt); in imx412_init_pad_cfg()
793 * imx412_start_streaming() - Start sensor stream
804 reg_list = &imx412->cur_mode->reg_list; in imx412_start_streaming()
805 ret = imx412_write_regs(imx412, reg_list->regs, in imx412_start_streaming()
806 reg_list->num_of_regs); in imx412_start_streaming()
808 dev_err(imx412->dev, "fail to write initial registers"); in imx412_start_streaming()
813 ret = __v4l2_ctrl_handler_setup(imx412->sd.ctrl_handler); in imx412_start_streaming()
815 dev_err(imx412->dev, "fail to setup handler"); in imx412_start_streaming()
826 dev_err(imx412->dev, "fail to start streaming"); in imx412_start_streaming()
834 * imx412_stop_streaming() - Stop sensor stream
846 * imx412_set_stream() - Enable sensor streaming
847 * @sd: pointer to imx412 subdevice
852 static int imx412_set_stream(struct v4l2_subdev *sd, int enable) in imx412_set_stream() argument
854 struct imx412 *imx412 = to_imx412(sd); in imx412_set_stream()
857 mutex_lock(&imx412->mutex); in imx412_set_stream()
859 if (imx412->streaming == enable) { in imx412_set_stream()
860 mutex_unlock(&imx412->mutex); in imx412_set_stream()
865 ret = pm_runtime_resume_and_get(imx412->dev); in imx412_set_stream()
874 pm_runtime_put(imx412->dev); in imx412_set_stream()
877 imx412->streaming = enable; in imx412_set_stream()
879 mutex_unlock(&imx412->mutex); in imx412_set_stream()
884 pm_runtime_put(imx412->dev); in imx412_set_stream()
886 mutex_unlock(&imx412->mutex); in imx412_set_stream()
892 * imx412_detect() - Detect imx412 sensor
895 * Return: 0 if successful, -EIO if sensor id does not match
907 dev_err(imx412->dev, "chip id mismatch: %x!=%x", in imx412_detect()
909 return -ENXIO; in imx412_detect()
916 * imx412_parse_hw_config() - Parse HW configuration and check if supported
923 struct fwnode_handle *fwnode = dev_fwnode(imx412->dev); in imx412_parse_hw_config()
933 return -ENXIO; in imx412_parse_hw_config()
936 imx412->reset_gpio = devm_gpiod_get_optional(imx412->dev, "reset", in imx412_parse_hw_config()
938 if (IS_ERR(imx412->reset_gpio)) { in imx412_parse_hw_config()
939 dev_err(imx412->dev, "failed to get reset gpio %ld", in imx412_parse_hw_config()
940 PTR_ERR(imx412->reset_gpio)); in imx412_parse_hw_config()
941 return PTR_ERR(imx412->reset_gpio); in imx412_parse_hw_config()
945 imx412->inclk = devm_clk_get(imx412->dev, NULL); in imx412_parse_hw_config()
946 if (IS_ERR(imx412->inclk)) { in imx412_parse_hw_config()
947 dev_err(imx412->dev, "could not get inclk"); in imx412_parse_hw_config()
948 return PTR_ERR(imx412->inclk); in imx412_parse_hw_config()
951 rate = clk_get_rate(imx412->inclk); in imx412_parse_hw_config()
953 dev_err(imx412->dev, "inclk frequency mismatch"); in imx412_parse_hw_config()
954 return -EINVAL; in imx412_parse_hw_config()
959 imx412->supplies[i].supply = imx412_supply_names[i]; in imx412_parse_hw_config()
961 ret = devm_regulator_bulk_get(imx412->dev, in imx412_parse_hw_config()
963 imx412->supplies); in imx412_parse_hw_config()
969 return -ENXIO; in imx412_parse_hw_config()
977 dev_err(imx412->dev, in imx412_parse_hw_config()
980 ret = -EINVAL; in imx412_parse_hw_config()
985 dev_err(imx412->dev, "no link frequencies defined"); in imx412_parse_hw_config()
986 ret = -EINVAL; in imx412_parse_hw_config()
994 ret = -EINVAL; in imx412_parse_hw_config()
1021 * imx412_power_on() - Sensor power on sequence
1028 struct v4l2_subdev *sd = dev_get_drvdata(dev); in imx412_power_on() local
1029 struct imx412 *imx412 = to_imx412(sd); in imx412_power_on()
1033 imx412->supplies); in imx412_power_on()
1039 gpiod_set_value_cansleep(imx412->reset_gpio, 0); in imx412_power_on()
1041 ret = clk_prepare_enable(imx412->inclk); in imx412_power_on()
1043 dev_err(imx412->dev, "fail to enable inclk"); in imx412_power_on()
1052 gpiod_set_value_cansleep(imx412->reset_gpio, 1); in imx412_power_on()
1054 imx412->supplies); in imx412_power_on()
1060 * imx412_power_off() - Sensor power off sequence
1067 struct v4l2_subdev *sd = dev_get_drvdata(dev); in imx412_power_off() local
1068 struct imx412 *imx412 = to_imx412(sd); in imx412_power_off()
1070 clk_disable_unprepare(imx412->inclk); in imx412_power_off()
1072 gpiod_set_value_cansleep(imx412->reset_gpio, 1); in imx412_power_off()
1075 imx412->supplies); in imx412_power_off()
1081 * imx412_init_controls() - Initialize sensor subdevice controls
1088 struct v4l2_ctrl_handler *ctrl_hdlr = &imx412->ctrl_handler; in imx412_init_controls()
1089 const struct imx412_mode *mode = imx412->cur_mode; in imx412_init_controls()
1098 ctrl_hdlr->lock = &imx412->mutex; in imx412_init_controls()
1101 lpfr = mode->vblank + mode->height; in imx412_init_controls()
1102 imx412->exp_ctrl = v4l2_ctrl_new_std(ctrl_hdlr, in imx412_init_controls()
1106 lpfr - IMX412_EXPOSURE_OFFSET, in imx412_init_controls()
1110 imx412->again_ctrl = v4l2_ctrl_new_std(ctrl_hdlr, in imx412_init_controls()
1118 v4l2_ctrl_cluster(2, &imx412->exp_ctrl); in imx412_init_controls()
1120 imx412->vblank_ctrl = v4l2_ctrl_new_std(ctrl_hdlr, in imx412_init_controls()
1123 mode->vblank_min, in imx412_init_controls()
1124 mode->vblank_max, in imx412_init_controls()
1125 1, mode->vblank); in imx412_init_controls()
1128 imx412->pclk_ctrl = v4l2_ctrl_new_std(ctrl_hdlr, in imx412_init_controls()
1131 mode->pclk, mode->pclk, in imx412_init_controls()
1132 1, mode->pclk); in imx412_init_controls()
1134 imx412->link_freq_ctrl = v4l2_ctrl_new_int_menu(ctrl_hdlr, in imx412_init_controls()
1137 ARRAY_SIZE(link_freq) - in imx412_init_controls()
1139 mode->link_freq_idx, in imx412_init_controls()
1141 if (imx412->link_freq_ctrl) in imx412_init_controls()
1142 imx412->link_freq_ctrl->flags |= V4L2_CTRL_FLAG_READ_ONLY; in imx412_init_controls()
1144 imx412->hblank_ctrl = v4l2_ctrl_new_std(ctrl_hdlr, in imx412_init_controls()
1149 1, mode->hblank); in imx412_init_controls()
1150 if (imx412->hblank_ctrl) in imx412_init_controls()
1151 imx412->hblank_ctrl->flags |= V4L2_CTRL_FLAG_READ_ONLY; in imx412_init_controls()
1153 if (ctrl_hdlr->error) { in imx412_init_controls()
1154 dev_err(imx412->dev, "control init failed: %d", in imx412_init_controls()
1155 ctrl_hdlr->error); in imx412_init_controls()
1157 return ctrl_hdlr->error; in imx412_init_controls()
1160 imx412->sd.ctrl_handler = ctrl_hdlr; in imx412_init_controls()
1166 * 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()
1182 name = device_get_match_data(&client->dev); in imx412_probe()
1184 return -ENODEV; in imx412_probe()
1187 v4l2_i2c_subdev_init(&imx412->sd, client, &imx412_subdev_ops); in imx412_probe()
1191 dev_err(imx412->dev, "HW configuration is not supported"); in imx412_probe()
1195 mutex_init(&imx412->mutex); in imx412_probe()
1197 ret = imx412_power_on(imx412->dev); in imx412_probe()
1199 dev_err(imx412->dev, "failed to power-on the sensor"); in imx412_probe()
1206 dev_err(imx412->dev, "failed to find sensor: %d", ret); in imx412_probe()
1211 imx412->cur_mode = &supported_mode; in imx412_probe()
1212 imx412->vblank = imx412->cur_mode->vblank; in imx412_probe()
1216 dev_err(imx412->dev, "failed to init controls: %d", ret); in imx412_probe()
1221 imx412->sd.flags |= V4L2_SUBDEV_FL_HAS_DEVNODE; in imx412_probe()
1222 imx412->sd.entity.function = MEDIA_ENT_F_CAM_SENSOR; in imx412_probe()
1224 v4l2_i2c_subdev_set_name(&imx412->sd, client, name, NULL); in imx412_probe()
1227 imx412->pad.flags = MEDIA_PAD_FL_SOURCE; in imx412_probe()
1228 ret = media_entity_pads_init(&imx412->sd.entity, 1, &imx412->pad); in imx412_probe()
1230 dev_err(imx412->dev, "failed to init entity pads: %d", ret); in imx412_probe()
1234 ret = v4l2_async_register_subdev_sensor(&imx412->sd); in imx412_probe()
1236 dev_err(imx412->dev, in imx412_probe()
1241 pm_runtime_set_active(imx412->dev); in imx412_probe()
1242 pm_runtime_enable(imx412->dev); in imx412_probe()
1243 pm_runtime_idle(imx412->dev); in imx412_probe()
1248 media_entity_cleanup(&imx412->sd.entity); in imx412_probe()
1250 v4l2_ctrl_handler_free(imx412->sd.ctrl_handler); in imx412_probe()
1252 imx412_power_off(imx412->dev); in imx412_probe()
1254 mutex_destroy(&imx412->mutex); in imx412_probe()
1260 * imx412_remove() - I2C client device unbinding
1267 struct v4l2_subdev *sd = i2c_get_clientdata(client); in imx412_remove() local
1268 struct imx412 *imx412 = to_imx412(sd); in imx412_remove()
1270 v4l2_async_unregister_subdev(sd); in imx412_remove()
1271 media_entity_cleanup(&sd->entity); in imx412_remove()
1272 v4l2_ctrl_handler_free(sd->ctrl_handler); in imx412_remove()
1274 pm_runtime_disable(&client->dev); in imx412_remove()
1275 if (!pm_runtime_status_suspended(&client->dev)) in imx412_remove()
1276 imx412_power_off(&client->dev); in imx412_remove()
1277 pm_runtime_set_suspended(&client->dev); in imx412_remove()
1279 mutex_destroy(&imx412->mutex); in imx412_remove()