Lines Matching full:imx335

3  * Sony imx335 Camera Sensor Driver
123 /* IMX335 native and active pixel array size. */
132 * struct imx335_reg_list - imx335 sensor register list
148 * struct imx335_mode - imx335 sensor mode structure
172 * struct imx335 - imx335 sensor device structure
195 struct imx335 { struct
425 * to_imx335() - imx335 V4L2 sub-device to imx335 device.
426 * @subdev: pointer to imx335 V4L2 sub-device
428 * Return: pointer to imx335 device
430 static inline struct imx335 *to_imx335(struct v4l2_subdev *subdev) in to_imx335()
432 return container_of(subdev, struct imx335, sd); in to_imx335()
437 * @imx335: pointer to imx335 device
442 static int imx335_update_controls(struct imx335 *imx335, in imx335_update_controls() argument
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()
462 * @imx335: pointer to imx335 device
468 static int imx335_update_exp_gain(struct imx335 *imx335, u32 exposure, u32 gain) in imx335_update_exp_gain() argument
474 lpfr = imx335->vblank + imx335->cur_mode->height; 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()
495 static int imx335_update_test_pattern(struct imx335 *imx335, u32 pattern_index) in imx335_update_test_pattern() argument
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()
548 struct imx335 *imx335 = in imx335_set_ctrl() local
549 container_of(ctrl->handler, struct imx335, ctrl_handler); 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()
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()
585 ret = imx335_update_exp_gain(imx335, exposure, analog_gain); 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()
597 pm_runtime_put(imx335->dev); in imx335_set_ctrl()
607 static int imx335_get_format_code(struct imx335 *imx335, u32 code) in imx335_get_format_code() argument
621 * @sd: pointer to imx335 V4L2 sub-device structure
641 * @sd: pointer to imx335 V4L2 sub-device structure
651 struct imx335 *imx335 = to_imx335(sd); in imx335_enum_frame_size() local
657 code = imx335_get_format_code(imx335, fsize->code); in imx335_enum_frame_size()
672 * @imx335: pointer to imx335 device
676 static void imx335_fill_pad_format(struct imx335 *imx335, in imx335_fill_pad_format() argument
682 fmt->format.code = imx335->cur_mbus_code; in imx335_fill_pad_format()
692 * @sd: pointer to imx335 V4L2 sub-device structure
702 struct imx335 *imx335 = to_imx335(sd); in imx335_get_pad_format() local
704 mutex_lock(&imx335->mutex); 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()
722 * @sd: pointer to imx335 V4L2 sub-device structure
732 struct imx335 *imx335 = to_imx335(sd); in imx335_set_pad_format() local
736 mutex_lock(&imx335->mutex); in imx335_set_pad_format()
741 imx335->cur_mbus_code = imx335_mbus_codes[i]; in imx335_set_pad_format()
744 imx335_fill_pad_format(imx335, mode, fmt); in imx335_set_pad_format()
752 ret = imx335_update_controls(imx335, mode); 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()
764 * @sd: pointer to imx335 V4L2 sub-device structure
772 struct imx335 *imx335 = to_imx335(sd); in imx335_init_state() local
776 imx335_fill_pad_format(imx335, &supported_mode, &fmt); in imx335_init_state()
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()
790 * @sd: pointer to imx335 V4L2 sub-device structure
823 static int imx335_set_framefmt(struct imx335 *imx335) in imx335_set_framefmt() argument
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()
842 * @imx335: pointer to imx335 device
846 static int imx335_start_streaming(struct imx335 *imx335) in imx335_start_streaming() argument
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()
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()
865 dev_err(imx335->dev, "fail to write initial registers\n"); in imx335_start_streaming()
869 ret = imx335_set_framefmt(imx335); 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()
905 * @imx335: pointer to imx335 device
909 static int imx335_stop_streaming(struct imx335 *imx335) in imx335_stop_streaming() argument
911 return cci_write(imx335->cci, IMX335_REG_MODE_SELECT, in imx335_stop_streaming()
917 * @sd: pointer to imx335 subdevice
924 struct imx335 *imx335 = to_imx335(sd); in imx335_set_stream() local
927 mutex_lock(&imx335->mutex); in imx335_set_stream()
930 ret = pm_runtime_resume_and_get(imx335->dev); in imx335_set_stream()
934 ret = imx335_start_streaming(imx335); in imx335_set_stream()
938 imx335_stop_streaming(imx335); 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
956 * @imx335: pointer to imx335 device
960 static int imx335_detect(struct imx335 *imx335) in imx335_detect() argument
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()
980 * @imx335: pointer to imx335 device
984 static int imx335_parse_hw_config(struct imx335 *imx335) in imx335_parse_hw_config() argument
986 struct fwnode_handle *fwnode = dev_fwnode(imx335->dev); 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()
1033 dev_err(imx335->dev, "Failed to get next endpoint\n"); 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()
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()
1100 struct imx335 *imx335 = to_imx335(sd); in imx335_power_on() local
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()
1141 struct imx335 *imx335 = to_imx335(sd); in imx335_power_off() local
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()
1152 * @imx335: pointer to imx335 device
1156 static int imx335_init_controls(struct imx335 *imx335) in imx335_init_controls() argument
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()
1178 imx335->exp_ctrl = v4l2_ctrl_new_std(ctrl_hdlr, 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()
1218 imx335->pclk_ctrl = v4l2_ctrl_new_std(ctrl_hdlr, 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()
1239 if (imx335->hblank_ctrl) in imx335_init_controls()
1240 imx335->hblank_ctrl->flags |= V4L2_CTRL_FLAG_READ_ONLY; in imx335_init_controls()
1245 dev_err(imx335->dev, "control init failed: %d\n", in imx335_init_controls()
1251 imx335->sd.ctrl_handler = ctrl_hdlr; in imx335_init_controls()
1264 struct imx335 *imx335; in imx335_probe() local
1267 imx335 = devm_kzalloc(&client->dev, sizeof(*imx335), GFP_KERNEL); in imx335_probe()
1268 if (!imx335) 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()
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()
1282 ret = imx335_parse_hw_config(imx335); 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()
1297 ret = imx335_detect(imx335); 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()
1308 ret = imx335_init_controls(imx335); 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()
1360 struct imx335 *imx335 = to_imx335(sd); in imx335_remove() local
1371 mutex_destroy(&imx335->mutex); in imx335_remove()
1379 { .compatible = "sony,imx335" },
1389 .name = "imx335",
1397 MODULE_DESCRIPTION("Sony imx335 sensor driver");