Lines Matching full:imx283

3  * V4L2 Support for the IMX283
10 * Based on Sony IMX283 driver prepared by Will Whang
179 /* IMX283 native and active pixel array size. */
548 struct imx283 { struct
575 static inline struct imx283 *to_imx283(struct v4l2_subdev *sd) in to_imx283() argument
577 return container_of_const(sd, struct imx283, sd); in to_imx283()
608 static u64 imx283_pixel_rate(struct imx283 *imx283, in imx283_pixel_rate() argument
611 u64 link_frequency = link_frequencies[__ffs(imx283->link_freq_bitmap)]; in imx283_pixel_rate()
661 static u32 imx283_exposure(struct imx283 *imx283, in imx283_exposure() argument
670 numerator = (imx283->vmax * (svr + 1) - shr) * imx283->hmax + offset; in imx283_exposure()
672 do_div(numerator, imx283->hmax); in imx283_exposure()
677 static void imx283_exposure_limits(struct imx283 *imx283, in imx283_exposure_limits() argument
684 u64 max_shr = (svr + 1) * imx283->vmax - 4; in imx283_exposure_limits()
688 *min_exposure = imx283_exposure(imx283, mode, max_shr); in imx283_exposure_limits()
689 *max_exposure = imx283_exposure(imx283, mode, min_shr); in imx283_exposure_limits()
696 static u32 imx283_shr(struct imx283 *imx283, const struct imx283_mode *mode, in imx283_shr() argument
705 temp = ((u64)exposure * imx283->hmax - offset); in imx283_shr()
706 do_div(temp, imx283->hmax); in imx283_shr()
708 return (imx283->vmax * (svr + 1) - temp); in imx283_shr()
731 static int imx283_update_test_pattern(struct imx283 *imx283, u32 pattern_index) in imx283_update_test_pattern() argument
739 return cci_write(imx283->cci, IMX283_REG_TPG_CTRL, 0x00, NULL); in imx283_update_test_pattern()
741 ret = cci_write(imx283->cci, IMX283_REG_TPG_PAT, in imx283_update_test_pattern()
746 return cci_write(imx283->cci, IMX283_REG_TPG_CTRL, in imx283_update_test_pattern()
752 struct imx283 *imx283 = container_of(ctrl->handler, struct imx283, in imx283_set_ctrl() local
762 state = v4l2_subdev_get_locked_active_state(&imx283->sd); in imx283_set_ctrl()
777 imx283->vmax = mode->height + ctrl->val; in imx283_set_ctrl()
779 imx283_exposure_limits(imx283, mode, in imx283_set_ctrl()
782 current_exposure = imx283->exposure->val; in imx283_set_ctrl()
786 __v4l2_ctrl_modify_range(imx283->exposure, min_exposure, in imx283_set_ctrl()
794 if (!pm_runtime_get_if_active(imx283->dev)) in imx283_set_ctrl()
799 shr = imx283_shr(imx283, mode, ctrl->val); in imx283_set_ctrl()
800 dev_dbg(imx283->dev, "V4L2_CID_EXPOSURE : %d - SHR: %lld\n", in imx283_set_ctrl()
802 ret = cci_write(imx283->cci, IMX283_REG_SHR, shr, NULL); in imx283_set_ctrl()
806 pixel_rate = imx283_pixel_rate(imx283, mode); in imx283_set_ctrl()
807 imx283->hmax = imx283_internal_clock(pixel_rate, mode->width + ctrl->val); in imx283_set_ctrl()
808 dev_dbg(imx283->dev, "V4L2_CID_HBLANK : %d HMAX : %u\n", in imx283_set_ctrl()
809 ctrl->val, imx283->hmax); in imx283_set_ctrl()
810 ret = cci_write(imx283->cci, IMX283_REG_HMAX, imx283->hmax, NULL); in imx283_set_ctrl()
814 imx283->vmax = mode->height + ctrl->val; in imx283_set_ctrl()
815 dev_dbg(imx283->dev, "V4L2_CID_VBLANK : %d VMAX : %u\n", in imx283_set_ctrl()
816 ctrl->val, imx283->vmax); in imx283_set_ctrl()
817 ret = cci_write(imx283->cci, IMX283_REG_VMAX, imx283->vmax, NULL); in imx283_set_ctrl()
821 ret = cci_write(imx283->cci, IMX283_REG_ANALOG_GAIN, ctrl->val, NULL); in imx283_set_ctrl()
825 ret = cci_write(imx283->cci, IMX283_REG_DIGITAL_GAIN, ctrl->val, NULL); in imx283_set_ctrl()
834 cci_write(imx283->cci, IMX283_REG_HTRIMMING, in imx283_set_ctrl()
837 cci_write(imx283->cci, IMX283_REG_HTRIMMING, in imx283_set_ctrl()
843 ret = imx283_update_test_pattern(imx283, ctrl->val); in imx283_set_ctrl()
847 dev_err(imx283->dev, "ctrl(id:0x%x, val:0x%x) is not handled\n", in imx283_set_ctrl()
852 pm_runtime_put(imx283->dev); in imx283_set_ctrl()
893 static void imx283_update_image_pad_format(struct imx283 *imx283, in imx283_update_image_pad_format() argument
909 struct imx283 *imx283 = to_imx283(sd); in imx283_init_state() local
919 imx283_update_image_pad_format(imx283, mode, format); in imx283_init_state()
928 static void imx283_set_framing_limits(struct imx283 *imx283, in imx283_set_framing_limits() argument
931 u64 pixel_rate = imx283_pixel_rate(imx283, mode); in imx283_set_framing_limits()
935 imx283->hmax = imx283_internal_clock(pixel_rate, mode->default_hmax); in imx283_set_framing_limits()
936 imx283->vmax = mode->default_vmax; in imx283_set_framing_limits()
945 __v4l2_ctrl_modify_range(imx283->hblank, min_hblank, max_hblank, 1, in imx283_set_framing_limits()
947 __v4l2_ctrl_s_ctrl(imx283->hblank, def_hblank); in imx283_set_framing_limits()
950 __v4l2_ctrl_modify_range(imx283->vblank, mode->min_vmax - mode->height, in imx283_set_framing_limits()
953 __v4l2_ctrl_s_ctrl(imx283->vblank, mode->default_vmax - mode->height); in imx283_set_framing_limits()
962 struct imx283 *imx283 = to_imx283(sd); in imx283_set_pad_format() local
982 imx283_set_framing_limits(imx283, mode); in imx283_set_pad_format()
989 static int imx283_standby_cancel(struct imx283 *imx283) in imx283_standby_cancel() argument
994 cci_write(imx283->cci, IMX283_REG_STANDBY, in imx283_standby_cancel()
998 cci_multi_reg_write(imx283->cci, imx283->freq->regs, in imx283_standby_cancel()
999 imx283->freq->reg_count, &ret); in imx283_standby_cancel()
1001 dev_dbg(imx283->dev, "Using clk freq %ld MHz", in imx283_standby_cancel()
1002 imx283->freq->mhz / HZ_PER_MHZ); in imx283_standby_cancel()
1005 cci_write(imx283->cci, IMX283_REG_PLSTMG08, IMX283_PLSTMG08_VAL, &ret); in imx283_standby_cancel()
1006 cci_write(imx283->cci, IMX283_REG_PLSTMG02, IMX283_PLSTMG02_VAL, &ret); in imx283_standby_cancel()
1009 cci_write(imx283->cci, IMX283_REG_STBPL, IMX283_STBPL_NORMAL, &ret); in imx283_standby_cancel()
1012 link_freq_idx = __ffs(imx283->link_freq_bitmap); in imx283_standby_cancel()
1013 cci_multi_reg_write(imx283->cci, link_freq_reglist[link_freq_idx].regs, in imx283_standby_cancel()
1021 cci_write(imx283->cci, IMX283_REG_STANDBY, IMX283_ACTIVE, &ret); in imx283_standby_cancel()
1026 cci_write(imx283->cci, IMX283_REG_CLAMP, IMX283_CLPSQRST, &ret); in imx283_standby_cancel()
1027 cci_write(imx283->cci, IMX283_REG_XMSTA, 0, &ret); in imx283_standby_cancel()
1028 cci_write(imx283->cci, IMX283_REG_SYNCDRV, IMX283_SYNCDRV_XHS_XVS, &ret); in imx283_standby_cancel()
1034 static int imx283_start_streaming(struct imx283 *imx283, in imx283_start_streaming() argument
1053 ret = imx283_standby_cancel(imx283); in imx283_start_streaming()
1055 dev_err(imx283->dev, "failed to cancel standby\n"); in imx283_start_streaming()
1064 cci_write(imx283->cci, IMX283_REG_MDSEL1, readout->mdsel1, &ret); in imx283_start_streaming()
1065 cci_write(imx283->cci, IMX283_REG_MDSEL2, readout->mdsel2, &ret); in imx283_start_streaming()
1066 cci_write(imx283->cci, IMX283_REG_MDSEL3, in imx283_start_streaming()
1068 cci_write(imx283->cci, IMX283_REG_MDSEL4, in imx283_start_streaming()
1073 cci_write(imx283->cci, IMX283_REG_MDSEL7, 0x01, &ret); in imx283_start_streaming()
1074 cci_write(imx283->cci, IMX283_REG_MDSEL18, 0x1098, &ret); in imx283_start_streaming()
1078 dev_err(imx283->dev, "failed to set readout\n"); in imx283_start_streaming()
1083 cci_write(imx283->cci, IMX283_REG_SVR, 0x00, &ret); in imx283_start_streaming()
1085 dev_dbg(imx283->dev, "Mode: Size %d x %d\n", mode->width, mode->height); in imx283_start_streaming()
1086 dev_dbg(imx283->dev, "Analogue Crop (in the mode) %d,%d %dx%d\n", in imx283_start_streaming()
1098 v_pos = imx283->vflip->val ? in imx283_start_streaming()
1103 cci_write(imx283->cci, IMX283_REG_Y_OUT_SIZE, y_out_size, &ret); in imx283_start_streaming()
1104 cci_write(imx283->cci, IMX283_REG_WRITE_VSIZE, write_v_size, &ret); in imx283_start_streaming()
1105 cci_write(imx283->cci, IMX283_REG_VWIDCUT, v_widcut, &ret); in imx283_start_streaming()
1106 cci_write(imx283->cci, IMX283_REG_VWINPOS, v_pos, &ret); in imx283_start_streaming()
1108 cci_write(imx283->cci, IMX283_REG_OB_SIZE_V, mode->vertical_ob, &ret); in imx283_start_streaming()
1111 cci_write(imx283->cci, IMX283_REG_HTRIMMING_START, mode->crop.left, &ret); in imx283_start_streaming()
1112 cci_write(imx283->cci, IMX283_REG_HTRIMMING_END, in imx283_start_streaming()
1116 cci_write(imx283->cci, IMX283_REG_EBD_X_OUT_SIZE, 0, &ret); in imx283_start_streaming()
1119 ret = __v4l2_ctrl_handler_setup(imx283->sd.ctrl_handler); in imx283_start_streaming()
1128 struct imx283 *imx283 = to_imx283(sd); in imx283_enable_streams() local
1134 ret = pm_runtime_get_sync(imx283->dev); in imx283_enable_streams()
1136 pm_runtime_put_noidle(imx283->dev); in imx283_enable_streams()
1140 ret = imx283_start_streaming(imx283, state); in imx283_enable_streams()
1147 pm_runtime_mark_last_busy(imx283->dev); in imx283_enable_streams()
1148 pm_runtime_put_autosuspend(imx283->dev); in imx283_enable_streams()
1157 struct imx283 *imx283 = to_imx283(sd); in imx283_disable_streams() local
1163 ret = cci_write(imx283->cci, IMX283_REG_STANDBY, IMX283_STBLOGIC, NULL); in imx283_disable_streams()
1165 dev_err(imx283->dev, "Failed to stop stream\n"); in imx283_disable_streams()
1167 pm_runtime_mark_last_busy(imx283->dev); in imx283_disable_streams()
1168 pm_runtime_put_autosuspend(imx283->dev); in imx283_disable_streams()
1174 static int imx283_power_on(struct imx283 *imx283) in imx283_power_on() argument
1179 imx283->supplies); in imx283_power_on()
1181 dev_err(imx283->dev, "failed to enable regulators\n"); in imx283_power_on()
1185 ret = clk_prepare_enable(imx283->xclk); in imx283_power_on()
1187 dev_err(imx283->dev, "failed to enable clock\n"); in imx283_power_on()
1191 gpiod_set_value_cansleep(imx283->reset_gpio, 0); in imx283_power_on()
1199 regulator_bulk_disable(ARRAY_SIZE(imx283_supply_name), imx283->supplies); in imx283_power_on()
1203 static int imx283_power_off(struct imx283 *imx283) in imx283_power_off() argument
1205 gpiod_set_value_cansleep(imx283->reset_gpio, 1); in imx283_power_off()
1206 regulator_bulk_disable(ARRAY_SIZE(imx283_supply_name), imx283->supplies); in imx283_power_off()
1207 clk_disable_unprepare(imx283->xclk); in imx283_power_off()
1215 struct imx283 *imx283 = to_imx283(sd); in imx283_runtime_resume() local
1217 return imx283_power_on(imx283); in imx283_runtime_resume()
1223 struct imx283 *imx283 = to_imx283(sd); in imx283_runtime_suspend() local
1225 imx283_power_off(imx283); in imx283_runtime_suspend()
1230 static int imx283_get_regulators(struct imx283 *imx283) in imx283_get_regulators() argument
1235 imx283->supplies[i].supply = imx283_supply_name[i]; in imx283_get_regulators()
1237 return devm_regulator_bulk_get(imx283->dev, in imx283_get_regulators()
1239 imx283->supplies); in imx283_get_regulators()
1243 static int imx283_identify_module(struct imx283 *imx283) in imx283_identify_module() argument
1248 ret = cci_read(imx283->cci, IMX283_REG_CHIP_ID, &val, NULL); in imx283_identify_module()
1250 dev_err(imx283->dev, "failed to read chip id %x, with error %d\n", in imx283_identify_module()
1256 dev_err(imx283->dev, "chip id mismatch: %x!=%llx\n", in imx283_identify_module()
1317 static int imx283_init_controls(struct imx283 *imx283) in imx283_init_controls() argument
1327 ctrl_hdlr = &imx283->ctrl_handler; in imx283_init_controls()
1338 pixel_rate = imx283_pixel_rate(imx283, mode); in imx283_init_controls()
1345 __fls(imx283->link_freq_bitmap), in imx283_init_controls()
1346 __ffs(imx283->link_freq_bitmap), in imx283_init_controls()
1352 imx283->vblank = v4l2_ctrl_new_std(ctrl_hdlr, &imx283_ctrl_ops, in imx283_init_controls()
1361 imx283->hblank = v4l2_ctrl_new_std(ctrl_hdlr, &imx283_ctrl_ops, in imx283_init_controls()
1365 imx283->exposure = v4l2_ctrl_new_std(ctrl_hdlr, &imx283_ctrl_ops, in imx283_init_controls()
1380 imx283->vflip = v4l2_ctrl_new_std(ctrl_hdlr, &imx283_ctrl_ops, V4L2_CID_VFLIP, in imx283_init_controls()
1382 if (imx283->vflip) in imx283_init_controls()
1383 imx283->vflip->flags |= V4L2_CTRL_FLAG_MODIFY_LAYOUT; in imx283_init_controls()
1392 dev_err(imx283->dev, "control init failed (%d)\n", ret); in imx283_init_controls()
1396 ret = v4l2_fwnode_device_parse(imx283->dev, &props); in imx283_init_controls()
1405 imx283->sd.ctrl_handler = ctrl_hdlr; in imx283_init_controls()
1407 mutex_lock(imx283->ctrl_handler.lock); in imx283_init_controls()
1410 imx283_set_framing_limits(imx283, mode); in imx283_init_controls()
1412 mutex_unlock(imx283->ctrl_handler.lock); in imx283_init_controls()
1422 static int imx283_parse_endpoint(struct imx283 *imx283) in imx283_parse_endpoint() argument
1431 fwnode = dev_fwnode(imx283->dev); in imx283_parse_endpoint()
1434 dev_err(imx283->dev, "Failed to get next endpoint\n"); in imx283_parse_endpoint()
1444 dev_err(imx283->dev, in imx283_parse_endpoint()
1451 ret = v4l2_link_freq_to_bitmap(imx283->dev, bus_cfg.link_frequencies, in imx283_parse_endpoint()
1454 &imx283->link_freq_bitmap); in imx283_parse_endpoint()
1464 struct imx283 *imx283; in imx283_probe() local
1469 imx283 = devm_kzalloc(&client->dev, sizeof(*imx283), GFP_KERNEL); in imx283_probe()
1470 if (!imx283) in imx283_probe()
1473 imx283->dev = &client->dev; in imx283_probe()
1475 v4l2_i2c_subdev_init(&imx283->sd, client, &imx283_subdev_ops); in imx283_probe()
1477 imx283->cci = devm_cci_regmap_init_i2c(client, 16); in imx283_probe()
1478 if (IS_ERR(imx283->cci)) { in imx283_probe()
1479 ret = PTR_ERR(imx283->cci); in imx283_probe()
1480 dev_err(imx283->dev, "failed to initialize CCI: %d\n", ret); in imx283_probe()
1485 imx283->xclk = devm_clk_get(imx283->dev, NULL); in imx283_probe()
1486 if (IS_ERR(imx283->xclk)) { in imx283_probe()
1487 return dev_err_probe(imx283->dev, PTR_ERR(imx283->xclk), in imx283_probe()
1491 xclk_freq = clk_get_rate(imx283->xclk); in imx283_probe()
1494 imx283->freq = &imx283_frequencies[i]; in imx283_probe()
1498 if (!imx283->freq) { in imx283_probe()
1499 dev_err(imx283->dev, "xclk frequency unsupported: %d Hz\n", xclk_freq); in imx283_probe()
1503 ret = imx283_get_regulators(imx283); in imx283_probe()
1505 return dev_err_probe(imx283->dev, ret, in imx283_probe()
1509 ret = imx283_parse_endpoint(imx283); in imx283_probe()
1511 dev_err(imx283->dev, "failed to parse endpoint configuration\n"); in imx283_probe()
1516 imx283->reset_gpio = devm_gpiod_get_optional(imx283->dev, "reset", in imx283_probe()
1518 if (IS_ERR(imx283->reset_gpio)) in imx283_probe()
1519 return dev_err_probe(imx283->dev, PTR_ERR(imx283->reset_gpio), in imx283_probe()
1526 ret = imx283_power_on(imx283); in imx283_probe()
1530 ret = imx283_identify_module(imx283); in imx283_probe()
1539 pm_runtime_set_active(imx283->dev); in imx283_probe()
1540 pm_runtime_get_noresume(imx283->dev); in imx283_probe()
1541 pm_runtime_enable(imx283->dev); in imx283_probe()
1542 pm_runtime_set_autosuspend_delay(imx283->dev, 1000); in imx283_probe()
1543 pm_runtime_use_autosuspend(imx283->dev); in imx283_probe()
1546 ret = imx283_init_controls(imx283); in imx283_probe()
1551 imx283->sd.flags |= V4L2_SUBDEV_FL_HAS_DEVNODE | in imx283_probe()
1553 imx283->sd.entity.function = MEDIA_ENT_F_CAM_SENSOR; in imx283_probe()
1554 imx283->sd.internal_ops = &imx283_internal_ops; in imx283_probe()
1557 imx283->pad.flags = MEDIA_PAD_FL_SOURCE; in imx283_probe()
1559 ret = media_entity_pads_init(&imx283->sd.entity, 1, &imx283->pad); in imx283_probe()
1561 dev_err(imx283->dev, "failed to init entity pads: %d\n", ret); in imx283_probe()
1565 imx283->sd.state_lock = imx283->ctrl_handler.lock; in imx283_probe()
1566 ret = v4l2_subdev_init_finalize(&imx283->sd); in imx283_probe()
1568 dev_err(imx283->dev, "subdev init error: %d\n", ret); in imx283_probe()
1572 ret = v4l2_async_register_subdev_sensor(&imx283->sd); in imx283_probe()
1574 dev_err(imx283->dev, "failed to register sensor sub-device: %d\n", ret); in imx283_probe()
1582 pm_runtime_mark_last_busy(imx283->dev); in imx283_probe()
1583 pm_runtime_put_autosuspend(imx283->dev); in imx283_probe()
1588 v4l2_subdev_cleanup(&imx283->sd); in imx283_probe()
1591 media_entity_cleanup(&imx283->sd.entity); in imx283_probe()
1594 v4l2_ctrl_handler_free(imx283->sd.ctrl_handler); in imx283_probe()
1597 pm_runtime_disable(imx283->dev); in imx283_probe()
1598 pm_runtime_set_suspended(imx283->dev); in imx283_probe()
1600 imx283_power_off(imx283); in imx283_probe()
1608 struct imx283 *imx283 = to_imx283(sd); in imx283_remove() local
1611 v4l2_subdev_cleanup(&imx283->sd); in imx283_remove()
1613 v4l2_ctrl_handler_free(imx283->sd.ctrl_handler); in imx283_remove()
1615 pm_runtime_disable(imx283->dev); in imx283_remove()
1616 if (!pm_runtime_status_suspended(imx283->dev)) in imx283_remove()
1617 imx283_power_off(imx283); in imx283_remove()
1618 pm_runtime_set_suspended(imx283->dev); in imx283_remove()
1625 { .compatible = "sony,imx283" },
1632 .name = "imx283",
1644 MODULE_DESCRIPTION("Sony IMX283 Sensor Driver");