Lines Matching +full:target +full:- +full:12 +full:v +full:- +full:supply
1 // SPDX-License-Identifier: GPL-2.0-only
16 #include <media/v4l2-cci.h>
17 #include <media/v4l2-ctrls.h>
18 #include <media/v4l2-fwnode.h>
19 #include <media/v4l2-subdev.h>
126 #define IMX335_PIXEL_ARRAY_LEFT 12U
127 #define IMX335_PIXEL_ARRAY_TOP 12U
132 * struct imx335_reg_list - imx335 sensor register list
142 "avdd", /* Analog (2.9V) supply */
143 "ovdd", /* Digital I/O (1.8V) supply */
144 "dvdd", /* Digital Core (1.2V) supply */
148 * struct imx335_mode - imx335 sensor mode structure
172 * struct imx335 - imx335 sensor device structure
175 * @sd: V4L2 sub-device
425 * to_imx335() - imx335 V4L2 sub-device to imx335 device.
426 * @subdev: pointer to imx335 V4L2 sub-device
436 * imx335_update_controls() - Update control ranges based on streaming mode
447 ret = __v4l2_ctrl_s_ctrl(imx335->link_freq_ctrl, in imx335_update_controls()
448 __ffs(imx335->link_freq_bitmap)); in imx335_update_controls()
452 ret = __v4l2_ctrl_s_ctrl(imx335->hblank_ctrl, mode->hblank); in imx335_update_controls()
456 return __v4l2_ctrl_modify_range(imx335->vblank_ctrl, mode->vblank_min, in imx335_update_controls()
457 mode->vblank_max, 1, mode->vblank); in imx335_update_controls()
461 * imx335_update_exp_gain() - Set updated exposure and gain
474 lpfr = imx335->vblank + imx335->cur_mode->height; in imx335_update_exp_gain()
475 shutter = lpfr - exposure; in imx335_update_exp_gain()
477 dev_dbg(imx335->dev, "Set exp %u, analog gain %u, shutter %u, lpfr %u\n", in imx335_update_exp_gain()
480 cci_write(imx335->cci, IMX335_REG_HOLD, 1, &ret); in imx335_update_exp_gain()
481 cci_write(imx335->cci, IMX335_REG_VMAX, lpfr, &ret); in imx335_update_exp_gain()
482 cci_write(imx335->cci, IMX335_REG_SHUTTER, shutter, &ret); in imx335_update_exp_gain()
483 cci_write(imx335->cci, IMX335_REG_GAIN, gain, &ret); in imx335_update_exp_gain()
488 ret_hold = cci_write(imx335->cci, IMX335_REG_HOLD, 0, NULL); in imx335_update_exp_gain()
500 return -EINVAL; in imx335_update_test_pattern()
512 cci_write(imx335->cci, IMX335_REG_TPG, in imx335_update_test_pattern()
515 cci_multi_reg_write(imx335->cci, tpg_enable_regs, in imx335_update_test_pattern()
527 cci_multi_reg_write(imx335->cci, tpg_disable_regs, in imx335_update_test_pattern()
535 * imx335_set_ctrl() - Set subdevice control
539 * - V4L2_CID_VBLANK
540 * - cluster controls:
541 * - V4L2_CID_ANALOGUE_GAIN
542 * - V4L2_CID_EXPOSURE
549 container_of(ctrl->handler, struct imx335, ctrl_handler); in imx335_set_ctrl()
555 if (ctrl->id == V4L2_CID_VBLANK) { in imx335_set_ctrl()
556 imx335->vblank = imx335->vblank_ctrl->val; in imx335_set_ctrl()
558 dev_dbg(imx335->dev, "Received vblank %u, new lpfr %u\n", in imx335_set_ctrl()
559 imx335->vblank, in imx335_set_ctrl()
560 imx335->vblank + imx335->cur_mode->height); in imx335_set_ctrl()
562 return __v4l2_ctrl_modify_range(imx335->exp_ctrl, in imx335_set_ctrl()
564 imx335->vblank + in imx335_set_ctrl()
565 imx335->cur_mode->height - in imx335_set_ctrl()
574 if (pm_runtime_get_if_in_use(imx335->dev) == 0) in imx335_set_ctrl()
577 switch (ctrl->id) { in imx335_set_ctrl()
579 exposure = ctrl->val; in imx335_set_ctrl()
580 analog_gain = imx335->again_ctrl->val; in imx335_set_ctrl()
582 dev_dbg(imx335->dev, "Received exp %u, analog gain %u\n", in imx335_set_ctrl()
589 ret = imx335_update_test_pattern(imx335, ctrl->val); in imx335_set_ctrl()
593 dev_err(imx335->dev, "Invalid control %d\n", ctrl->id); in imx335_set_ctrl()
594 ret = -EINVAL; in imx335_set_ctrl()
597 pm_runtime_put(imx335->dev); in imx335_set_ctrl()
620 * imx335_enum_mbus_code() - Enumerate V4L2 sub-device mbus codes
621 * @sd: pointer to imx335 V4L2 sub-device structure
622 * @sd_state: V4L2 sub-device configuration
623 * @code: V4L2 sub-device code enumeration need to be filled
631 if (code->index >= ARRAY_SIZE(imx335_mbus_codes)) in imx335_enum_mbus_code()
632 return -EINVAL; in imx335_enum_mbus_code()
634 code->code = imx335_mbus_codes[code->index]; in imx335_enum_mbus_code()
640 * imx335_enum_frame_size() - Enumerate V4L2 sub-device frame sizes
641 * @sd: pointer to imx335 V4L2 sub-device structure
642 * @sd_state: V4L2 sub-device configuration
643 * @fsize: V4L2 sub-device size enumeration need to be filled
654 if (fsize->index > ARRAY_SIZE(imx335_mbus_codes)) in imx335_enum_frame_size()
655 return -EINVAL; in imx335_enum_frame_size()
657 code = imx335_get_format_code(imx335, fsize->code); in imx335_enum_frame_size()
658 if (fsize->code != code) in imx335_enum_frame_size()
659 return -EINVAL; in imx335_enum_frame_size()
661 fsize->min_width = supported_mode.width; in imx335_enum_frame_size()
662 fsize->max_width = fsize->min_width; in imx335_enum_frame_size()
663 fsize->min_height = supported_mode.height; in imx335_enum_frame_size()
664 fsize->max_height = fsize->min_height; in imx335_enum_frame_size()
670 * imx335_fill_pad_format() - Fill subdevice pad format
674 * @fmt: V4L2 sub-device format need to be filled
680 fmt->format.width = mode->width; in imx335_fill_pad_format()
681 fmt->format.height = mode->height; in imx335_fill_pad_format()
682 fmt->format.code = imx335->cur_mbus_code; in imx335_fill_pad_format()
683 fmt->format.field = V4L2_FIELD_NONE; in imx335_fill_pad_format()
684 fmt->format.colorspace = V4L2_COLORSPACE_RAW; in imx335_fill_pad_format()
685 fmt->format.ycbcr_enc = V4L2_YCBCR_ENC_DEFAULT; in imx335_fill_pad_format()
686 fmt->format.quantization = V4L2_QUANTIZATION_DEFAULT; in imx335_fill_pad_format()
687 fmt->format.xfer_func = V4L2_XFER_FUNC_NONE; in imx335_fill_pad_format()
691 * imx335_get_pad_format() - Get subdevice pad format
692 * @sd: pointer to imx335 V4L2 sub-device structure
693 * @sd_state: V4L2 sub-device configuration
694 * @fmt: V4L2 sub-device format need to be set
704 mutex_lock(&imx335->mutex); in imx335_get_pad_format()
706 if (fmt->which == V4L2_SUBDEV_FORMAT_TRY) { in imx335_get_pad_format()
709 framefmt = v4l2_subdev_state_get_format(sd_state, fmt->pad); in imx335_get_pad_format()
710 fmt->format = *framefmt; in imx335_get_pad_format()
712 imx335_fill_pad_format(imx335, imx335->cur_mode, fmt); in imx335_get_pad_format()
715 mutex_unlock(&imx335->mutex); in imx335_get_pad_format()
721 * imx335_set_pad_format() - Set subdevice pad format
722 * @sd: pointer to imx335 V4L2 sub-device structure
723 * @sd_state: V4L2 sub-device configuration
724 * @fmt: V4L2 sub-device format need to be set
736 mutex_lock(&imx335->mutex); in imx335_set_pad_format()
740 if (imx335_mbus_codes[i] == fmt->format.code) in imx335_set_pad_format()
741 imx335->cur_mbus_code = imx335_mbus_codes[i]; in imx335_set_pad_format()
746 if (fmt->which == V4L2_SUBDEV_FORMAT_TRY) { in imx335_set_pad_format()
749 framefmt = v4l2_subdev_state_get_format(sd_state, fmt->pad); in imx335_set_pad_format()
750 *framefmt = fmt->format; in imx335_set_pad_format()
754 imx335->cur_mode = mode; in imx335_set_pad_format()
757 mutex_unlock(&imx335->mutex); in imx335_set_pad_format()
763 * imx335_init_state() - Initialize sub-device state
764 * @sd: pointer to imx335 V4L2 sub-device structure
765 * @sd_state: V4L2 sub-device configuration
778 mutex_lock(&imx335->mutex); in imx335_init_state()
779 __v4l2_ctrl_modify_range(imx335->link_freq_ctrl, 0, in imx335_init_state()
780 __fls(imx335->link_freq_bitmap), in imx335_init_state()
781 ~(imx335->link_freq_bitmap), in imx335_init_state()
782 __ffs(imx335->link_freq_bitmap)); in imx335_init_state()
783 mutex_unlock(&imx335->mutex); in imx335_init_state()
789 * imx335_get_selection() - Selection API
790 * @sd: pointer to imx335 V4L2 sub-device structure
791 * @sd_state: V4L2 sub-device configuration
800 switch (sel->target) { in imx335_get_selection()
802 sel->r.top = 0; in imx335_get_selection()
803 sel->r.left = 0; in imx335_get_selection()
804 sel->r.width = IMX335_NATIVE_WIDTH; in imx335_get_selection()
805 sel->r.height = IMX335_NATIVE_HEIGHT; in imx335_get_selection()
812 sel->r.top = IMX335_PIXEL_ARRAY_TOP; in imx335_get_selection()
813 sel->r.left = IMX335_PIXEL_ARRAY_LEFT; in imx335_get_selection()
814 sel->r.width = IMX335_PIXEL_ARRAY_WIDTH; in imx335_get_selection()
815 sel->r.height = IMX335_PIXEL_ARRAY_HEIGHT; in imx335_get_selection()
820 return -EINVAL; in imx335_get_selection()
825 switch (imx335->cur_mbus_code) { in imx335_set_framefmt()
827 return cci_multi_reg_write(imx335->cci, raw10_framefmt_regs, in imx335_set_framefmt()
832 return cci_multi_reg_write(imx335->cci, raw12_framefmt_regs, in imx335_set_framefmt()
837 return -EINVAL; in imx335_set_framefmt()
841 * imx335_start_streaming() - Start sensor stream
852 reg_list = &link_freq_reglist[__ffs(imx335->link_freq_bitmap)]; in imx335_start_streaming()
853 ret = cci_multi_reg_write(imx335->cci, reg_list->regs, in imx335_start_streaming()
854 reg_list->num_of_regs, NULL); in imx335_start_streaming()
856 dev_err(imx335->dev, "%s failed to set plls\n", __func__); in imx335_start_streaming()
861 reg_list = &imx335->cur_mode->reg_list; in imx335_start_streaming()
862 ret = cci_multi_reg_write(imx335->cci, reg_list->regs, in imx335_start_streaming()
863 reg_list->num_of_regs, NULL); in imx335_start_streaming()
865 dev_err(imx335->dev, "fail to write initial registers\n"); in imx335_start_streaming()
871 dev_err(imx335->dev, "%s failed to set frame format: %d\n", in imx335_start_streaming()
877 ret = cci_write(imx335->cci, IMX335_REG_LANEMODE, in imx335_start_streaming()
878 imx335->lane_mode, NULL); in imx335_start_streaming()
883 ret = __v4l2_ctrl_handler_setup(imx335->sd.ctrl_handler); in imx335_start_streaming()
885 dev_err(imx335->dev, "fail to setup handler\n"); in imx335_start_streaming()
890 ret = cci_write(imx335->cci, IMX335_REG_MODE_SELECT, in imx335_start_streaming()
893 dev_err(imx335->dev, "fail to start streaming\n"); in imx335_start_streaming()
904 * imx335_stop_streaming() - Stop sensor stream
911 return cci_write(imx335->cci, IMX335_REG_MODE_SELECT, in imx335_stop_streaming()
916 * imx335_set_stream() - Enable sensor streaming
927 mutex_lock(&imx335->mutex); in imx335_set_stream()
930 ret = pm_runtime_resume_and_get(imx335->dev); in imx335_set_stream()
939 pm_runtime_put(imx335->dev); in imx335_set_stream()
942 mutex_unlock(&imx335->mutex); in imx335_set_stream()
947 pm_runtime_put(imx335->dev); in imx335_set_stream()
949 mutex_unlock(&imx335->mutex); in imx335_set_stream()
955 * imx335_detect() - Detect imx335 sensor
958 * Return: 0 if successful, -EIO if sensor id does not match
965 ret = cci_read(imx335->cci, IMX335_REG_ID, &val, NULL); in imx335_detect()
970 dev_err(imx335->dev, "chip id mismatch: %x!=%llx\n", in imx335_detect()
972 return -ENXIO; in imx335_detect()
979 * imx335_parse_hw_config() - Parse HW configuration and check if supported
986 struct fwnode_handle *fwnode = dev_fwnode(imx335->dev); in imx335_parse_hw_config()
996 return -ENXIO; in imx335_parse_hw_config()
999 imx335->reset_gpio = devm_gpiod_get_optional(imx335->dev, "reset", in imx335_parse_hw_config()
1001 if (IS_ERR(imx335->reset_gpio)) { in imx335_parse_hw_config()
1002 dev_err(imx335->dev, "failed to get reset gpio %ld\n", in imx335_parse_hw_config()
1003 PTR_ERR(imx335->reset_gpio)); in imx335_parse_hw_config()
1004 return PTR_ERR(imx335->reset_gpio); in imx335_parse_hw_config()
1008 imx335->supplies[i].supply = imx335_supply_name[i]; in imx335_parse_hw_config()
1010 ret = devm_regulator_bulk_get(imx335->dev, in imx335_parse_hw_config()
1012 imx335->supplies); in imx335_parse_hw_config()
1014 dev_err(imx335->dev, "Failed to get regulators\n"); in imx335_parse_hw_config()
1019 imx335->inclk = devm_clk_get(imx335->dev, NULL); in imx335_parse_hw_config()
1020 if (IS_ERR(imx335->inclk)) { in imx335_parse_hw_config()
1021 dev_err(imx335->dev, "could not get inclk\n"); in imx335_parse_hw_config()
1022 return PTR_ERR(imx335->inclk); in imx335_parse_hw_config()
1025 rate = clk_get_rate(imx335->inclk); in imx335_parse_hw_config()
1027 dev_err(imx335->dev, "inclk frequency mismatch\n"); in imx335_parse_hw_config()
1028 return -EINVAL; in imx335_parse_hw_config()
1033 dev_err(imx335->dev, "Failed to get next endpoint\n"); in imx335_parse_hw_config()
1034 return -ENXIO; in imx335_parse_hw_config()
1044 imx335->lane_mode = IMX335_2LANE; in imx335_parse_hw_config()
1047 imx335->lane_mode = IMX335_4LANE; in imx335_parse_hw_config()
1050 dev_err(imx335->dev, in imx335_parse_hw_config()
1053 ret = -EINVAL; in imx335_parse_hw_config()
1057 ret = v4l2_link_freq_to_bitmap(imx335->dev, bus_cfg.link_frequencies, in imx335_parse_hw_config()
1060 &imx335->link_freq_bitmap); in imx335_parse_hw_config()
1092 * imx335_power_on() - Sensor power on sequence
1104 imx335->supplies); in imx335_power_on()
1113 gpiod_set_value_cansleep(imx335->reset_gpio, 0); in imx335_power_on()
1115 ret = clk_prepare_enable(imx335->inclk); in imx335_power_on()
1117 dev_err(imx335->dev, "fail to enable inclk\n"); in imx335_power_on()
1126 gpiod_set_value_cansleep(imx335->reset_gpio, 1); in imx335_power_on()
1127 regulator_bulk_disable(ARRAY_SIZE(imx335_supply_name), imx335->supplies); in imx335_power_on()
1133 * imx335_power_off() - Sensor power off sequence
1143 gpiod_set_value_cansleep(imx335->reset_gpio, 1); in imx335_power_off()
1144 clk_disable_unprepare(imx335->inclk); in imx335_power_off()
1145 regulator_bulk_disable(ARRAY_SIZE(imx335_supply_name), imx335->supplies); in imx335_power_off()
1151 * imx335_init_controls() - Initialize sensor subdevice controls
1158 struct v4l2_ctrl_handler *ctrl_hdlr = &imx335->ctrl_handler; in imx335_init_controls()
1159 const struct imx335_mode *mode = imx335->cur_mode; in imx335_init_controls()
1164 ret = v4l2_fwnode_device_parse(imx335->dev, &props); in imx335_init_controls()
1174 ctrl_hdlr->lock = &imx335->mutex; in imx335_init_controls()
1177 lpfr = mode->vblank + mode->height; in imx335_init_controls()
1178 imx335->exp_ctrl = v4l2_ctrl_new_std(ctrl_hdlr, in imx335_init_controls()
1182 lpfr - IMX335_EXPOSURE_OFFSET, in imx335_init_controls()
1194 imx335->again_ctrl = v4l2_ctrl_new_std(ctrl_hdlr, in imx335_init_controls()
1202 v4l2_ctrl_cluster(2, &imx335->exp_ctrl); in imx335_init_controls()
1204 imx335->vblank_ctrl = v4l2_ctrl_new_std(ctrl_hdlr, in imx335_init_controls()
1207 mode->vblank_min, in imx335_init_controls()
1208 mode->vblank_max, in imx335_init_controls()
1209 1, mode->vblank); in imx335_init_controls()
1214 ARRAY_SIZE(imx335_tpg_menu) - 1, in imx335_init_controls()
1218 imx335->pclk_ctrl = v4l2_ctrl_new_std(ctrl_hdlr, in imx335_init_controls()
1221 mode->pclk, mode->pclk, in imx335_init_controls()
1222 1, mode->pclk); in imx335_init_controls()
1224 imx335->link_freq_ctrl = v4l2_ctrl_new_int_menu(ctrl_hdlr, in imx335_init_controls()
1227 __fls(imx335->link_freq_bitmap), in imx335_init_controls()
1228 __ffs(imx335->link_freq_bitmap), in imx335_init_controls()
1230 if (imx335->link_freq_ctrl) in imx335_init_controls()
1231 imx335->link_freq_ctrl->flags |= V4L2_CTRL_FLAG_READ_ONLY; in imx335_init_controls()
1233 imx335->hblank_ctrl = v4l2_ctrl_new_std(ctrl_hdlr, in imx335_init_controls()
1236 mode->hblank, in imx335_init_controls()
1237 mode->hblank, in imx335_init_controls()
1238 1, mode->hblank); in imx335_init_controls()
1239 if (imx335->hblank_ctrl) in imx335_init_controls()
1240 imx335->hblank_ctrl->flags |= V4L2_CTRL_FLAG_READ_ONLY; in imx335_init_controls()
1244 if (ctrl_hdlr->error) { in imx335_init_controls()
1245 dev_err(imx335->dev, "control init failed: %d\n", in imx335_init_controls()
1246 ctrl_hdlr->error); in imx335_init_controls()
1248 return ctrl_hdlr->error; in imx335_init_controls()
1251 imx335->sd.ctrl_handler = ctrl_hdlr; in imx335_init_controls()
1257 * imx335_probe() - I2C client device binding
1267 imx335 = devm_kzalloc(&client->dev, sizeof(*imx335), GFP_KERNEL); in imx335_probe()
1269 return -ENOMEM; in imx335_probe()
1271 imx335->dev = &client->dev; in imx335_probe()
1272 imx335->cci = devm_cci_regmap_init_i2c(client, 16); in imx335_probe()
1273 if (IS_ERR(imx335->cci)) { in imx335_probe()
1274 dev_err(imx335->dev, "Unable to initialize I2C\n"); in imx335_probe()
1275 return -ENODEV; in imx335_probe()
1279 v4l2_i2c_subdev_init(&imx335->sd, client, &imx335_subdev_ops); in imx335_probe()
1280 imx335->sd.internal_ops = &imx335_internal_ops; in imx335_probe()
1284 dev_err(imx335->dev, "HW configuration is not supported\n"); in imx335_probe()
1288 mutex_init(&imx335->mutex); in imx335_probe()
1290 ret = imx335_power_on(imx335->dev); in imx335_probe()
1292 dev_err(imx335->dev, "failed to power-on the sensor\n"); in imx335_probe()
1299 dev_err(imx335->dev, "failed to find sensor: %d\n", ret); in imx335_probe()
1304 imx335->cur_mode = &supported_mode; in imx335_probe()
1305 imx335->cur_mbus_code = imx335_mbus_codes[0]; in imx335_probe()
1306 imx335->vblank = imx335->cur_mode->vblank; in imx335_probe()
1310 dev_err(imx335->dev, "failed to init controls: %d\n", ret); in imx335_probe()
1315 imx335->sd.flags |= V4L2_SUBDEV_FL_HAS_DEVNODE; in imx335_probe()
1316 imx335->sd.entity.function = MEDIA_ENT_F_CAM_SENSOR; in imx335_probe()
1319 imx335->pad.flags = MEDIA_PAD_FL_SOURCE; in imx335_probe()
1320 ret = media_entity_pads_init(&imx335->sd.entity, 1, &imx335->pad); in imx335_probe()
1322 dev_err(imx335->dev, "failed to init entity pads: %d\n", ret); in imx335_probe()
1326 ret = v4l2_async_register_subdev_sensor(&imx335->sd); in imx335_probe()
1328 dev_err(imx335->dev, in imx335_probe()
1333 pm_runtime_set_active(imx335->dev); in imx335_probe()
1334 pm_runtime_enable(imx335->dev); in imx335_probe()
1335 pm_runtime_idle(imx335->dev); in imx335_probe()
1340 media_entity_cleanup(&imx335->sd.entity); in imx335_probe()
1342 v4l2_ctrl_handler_free(imx335->sd.ctrl_handler); in imx335_probe()
1344 imx335_power_off(imx335->dev); in imx335_probe()
1346 mutex_destroy(&imx335->mutex); in imx335_probe()
1352 * imx335_remove() - I2C client device unbinding
1363 media_entity_cleanup(&sd->entity); in imx335_remove()
1364 v4l2_ctrl_handler_free(sd->ctrl_handler); in imx335_remove()
1366 pm_runtime_disable(&client->dev); in imx335_remove()
1367 if (!pm_runtime_status_suspended(&client->dev)) in imx335_remove()
1368 imx335_power_off(&client->dev); in imx335_remove()
1369 pm_runtime_set_suspended(&client->dev); in imx335_remove()
1371 mutex_destroy(&imx335->mutex); in imx335_remove()