Lines Matching full:sensor

3  * Driver for the Sony IMX415 CMOS Image Sensor.
465 static int imx415_read(struct imx415 *sensor, u32 addr) in imx415_read() argument
470 ret = regmap_raw_read(sensor->regmap, addr & IMX415_REG_ADDR_MASK, data, in imx415_read()
478 static int imx415_write(struct imx415 *sensor, u32 addr, u32 value) in imx415_write() argument
483 ret = regmap_raw_write(sensor->regmap, addr & IMX415_REG_ADDR_MASK, in imx415_write()
486 dev_err_ratelimited(sensor->dev, in imx415_write()
494 static int imx415_set_testpattern(struct imx415 *sensor, int val) in imx415_set_testpattern() argument
499 ret = imx415_write(sensor, IMX415_BLKLEVEL, 0x00); in imx415_set_testpattern()
502 ret = imx415_write(sensor, IMX415_TPG_EN_DUOUT, 0x01); in imx415_set_testpattern()
505 ret = imx415_write(sensor, IMX415_TPG_PATSEL_DUOUT, val - 1); in imx415_set_testpattern()
508 ret = imx415_write(sensor, IMX415_TPG_COLORWIDTH, 0x01); in imx415_set_testpattern()
511 ret = imx415_write(sensor, IMX415_TESTCLKEN_MIPI, 0x20); in imx415_set_testpattern()
514 ret = imx415_write(sensor, IMX415_DIG_CLP_MODE, 0x00); in imx415_set_testpattern()
517 ret = imx415_write(sensor, IMX415_WRJ_OPEN, 0x00); in imx415_set_testpattern()
519 ret = imx415_write(sensor, IMX415_BLKLEVEL, in imx415_set_testpattern()
523 ret = imx415_write(sensor, IMX415_TPG_EN_DUOUT, 0x00); in imx415_set_testpattern()
526 ret = imx415_write(sensor, IMX415_TESTCLKEN_MIPI, 0x00); in imx415_set_testpattern()
529 ret = imx415_write(sensor, IMX415_DIG_CLP_MODE, 0x01); in imx415_set_testpattern()
532 ret = imx415_write(sensor, IMX415_WRJ_OPEN, 0x01); in imx415_set_testpattern()
539 struct imx415 *sensor = container_of(ctrl->handler, struct imx415, in imx415_s_ctrl() local
546 if (!sensor->streaming) in imx415_s_ctrl()
549 state = v4l2_subdev_get_locked_active_state(&sensor->subdev); in imx415_s_ctrl()
550 format = v4l2_subdev_get_pad_format(&sensor->subdev, state, 0); in imx415_s_ctrl()
555 vmax = format->height + sensor->vblank->cur.val; in imx415_s_ctrl()
557 return imx415_write(sensor, IMX415_SHR0, vmax - ctrl->val); in imx415_s_ctrl()
561 return imx415_write(sensor, IMX415_GAIN_PCG_0, ctrl->val); in imx415_s_ctrl()
565 flip = (sensor->hflip->val << IMX415_HREVERSE_SHIFT) | in imx415_s_ctrl()
566 (sensor->vflip->val << IMX415_VREVERSE_SHIFT); in imx415_s_ctrl()
567 return imx415_write(sensor, IMX415_REVERSE, flip); in imx415_s_ctrl()
570 return imx415_set_testpattern(sensor, ctrl->val); in imx415_s_ctrl()
581 static int imx415_ctrls_init(struct imx415 *sensor) in imx415_ctrls_init() argument
585 u64 pixel_rate = supported_modes[sensor->cur_mode].pixel_rate; in imx415_ctrls_init()
586 u64 lane_rate = supported_modes[sensor->cur_mode].lane_rate; in imx415_ctrls_init()
593 ret = v4l2_fwnode_device_parse(sensor->dev, &props); in imx415_ctrls_init()
597 v4l2_ctrl_handler_init(&sensor->ctrls, 10); in imx415_ctrls_init()
604 return dev_err_probe(sensor->dev, -EINVAL, in imx415_ctrls_init()
609 ctrl = v4l2_ctrl_new_int_menu(&sensor->ctrls, &imx415_ctrl_ops, in imx415_ctrls_init()
617 v4l2_ctrl_new_std(&sensor->ctrls, &imx415_ctrl_ops, V4L2_CID_EXPOSURE, in imx415_ctrls_init()
620 v4l2_ctrl_new_std(&sensor->ctrls, &imx415_ctrl_ops, in imx415_ctrls_init()
625 hblank = supported_modes[sensor->cur_mode].hmax_pix - in imx415_ctrls_init()
627 ctrl = v4l2_ctrl_new_std(&sensor->ctrls, &imx415_ctrl_ops, in imx415_ctrls_init()
632 sensor->vblank = v4l2_ctrl_new_std(&sensor->ctrls, &imx415_ctrl_ops, in imx415_ctrls_init()
637 if (sensor->vblank) in imx415_ctrls_init()
638 sensor->vblank->flags |= V4L2_CTRL_FLAG_READ_ONLY; in imx415_ctrls_init()
645 v4l2_ctrl_new_std(&sensor->ctrls, NULL, V4L2_CID_PIXEL_RATE, pixel_rate, in imx415_ctrls_init()
648 sensor->hflip = v4l2_ctrl_new_std(&sensor->ctrls, &imx415_ctrl_ops, in imx415_ctrls_init()
650 sensor->vflip = v4l2_ctrl_new_std(&sensor->ctrls, &imx415_ctrl_ops, in imx415_ctrls_init()
653 v4l2_ctrl_new_std_menu_items(&sensor->ctrls, &imx415_ctrl_ops, in imx415_ctrls_init()
658 v4l2_ctrl_new_fwnode_properties(&sensor->ctrls, &imx415_ctrl_ops, in imx415_ctrls_init()
661 if (sensor->ctrls.error) { in imx415_ctrls_init()
662 dev_err_probe(sensor->dev, sensor->ctrls.error, in imx415_ctrls_init()
664 v4l2_ctrl_handler_free(&sensor->ctrls); in imx415_ctrls_init()
665 return sensor->ctrls.error; in imx415_ctrls_init()
667 sensor->subdev.ctrl_handler = &sensor->ctrls; in imx415_ctrls_init()
672 static int imx415_set_mode(struct imx415 *sensor, int mode) in imx415_set_mode() argument
679 dev_err(sensor->dev, "Mode %d not supported\n", mode); in imx415_set_mode()
685 ret = imx415_write(sensor, reg->address, reg->val); in imx415_set_mode()
691 reg = &sensor->clk_params->regs[i]; in imx415_set_mode()
692 ret = imx415_write(sensor, reg->address, reg->val); in imx415_set_mode()
700 static int imx415_setup(struct imx415 *sensor, struct v4l2_subdev_state *state) in imx415_setup() argument
706 ret = imx415_write(sensor, imx415_init_table[i].address, in imx415_setup()
712 return imx415_set_mode(sensor, sensor->cur_mode); in imx415_setup()
715 static int imx415_wakeup(struct imx415 *sensor) in imx415_wakeup() argument
719 ret = imx415_write(sensor, IMX415_MODE, IMX415_MODE_OPERATING); in imx415_wakeup()
733 static int imx415_stream_on(struct imx415 *sensor) in imx415_stream_on() argument
737 ret = imx415_wakeup(sensor); in imx415_stream_on()
741 return imx415_write(sensor, IMX415_XMSTA, IMX415_XMSTA_START); in imx415_stream_on()
744 static int imx415_stream_off(struct imx415 *sensor) in imx415_stream_off() argument
748 ret = imx415_write(sensor, IMX415_XMSTA, IMX415_XMSTA_STOP); in imx415_stream_off()
752 return imx415_write(sensor, IMX415_MODE, IMX415_MODE_STANDBY); in imx415_stream_off()
757 struct imx415 *sensor = to_imx415(sd); in imx415_s_stream() local
764 ret = imx415_stream_off(sensor); in imx415_s_stream()
766 pm_runtime_mark_last_busy(sensor->dev); in imx415_s_stream()
767 pm_runtime_put_autosuspend(sensor->dev); in imx415_s_stream()
769 sensor->streaming = false; in imx415_s_stream()
774 ret = pm_runtime_resume_and_get(sensor->dev); in imx415_s_stream()
778 ret = imx415_setup(sensor, state); in imx415_s_stream()
787 sensor->streaming = true; in imx415_s_stream()
789 ret = __v4l2_ctrl_handler_setup(&sensor->ctrls); in imx415_s_stream()
793 ret = imx415_stream_on(sensor); in imx415_s_stream()
809 pm_runtime_put_sync(sensor->dev); in imx415_s_stream()
810 sensor->streaming = false; in imx415_s_stream()
927 static int imx415_subdev_init(struct imx415 *sensor) in imx415_subdev_init() argument
929 struct i2c_client *client = to_i2c_client(sensor->dev); in imx415_subdev_init()
932 v4l2_i2c_subdev_init(&sensor->subdev, client, &imx415_subdev_ops); in imx415_subdev_init()
934 ret = imx415_ctrls_init(sensor); in imx415_subdev_init()
938 sensor->subdev.flags |= V4L2_SUBDEV_FL_HAS_DEVNODE | in imx415_subdev_init()
940 sensor->pad.flags = MEDIA_PAD_FL_SOURCE; in imx415_subdev_init()
941 sensor->subdev.entity.function = MEDIA_ENT_F_CAM_SENSOR; in imx415_subdev_init()
942 ret = media_entity_pads_init(&sensor->subdev.entity, 1, &sensor->pad); in imx415_subdev_init()
944 v4l2_ctrl_handler_free(&sensor->ctrls); in imx415_subdev_init()
948 sensor->subdev.state_lock = sensor->subdev.ctrl_handler->lock; in imx415_subdev_init()
949 v4l2_subdev_init_finalize(&sensor->subdev); in imx415_subdev_init()
954 static void imx415_subdev_cleanup(struct imx415 *sensor) in imx415_subdev_cleanup() argument
956 media_entity_cleanup(&sensor->subdev.entity); in imx415_subdev_cleanup()
957 v4l2_ctrl_handler_free(&sensor->ctrls); in imx415_subdev_cleanup()
960 static int imx415_power_on(struct imx415 *sensor) in imx415_power_on() argument
964 ret = regulator_bulk_enable(ARRAY_SIZE(sensor->supplies), in imx415_power_on()
965 sensor->supplies); in imx415_power_on()
969 gpiod_set_value_cansleep(sensor->reset, 0); in imx415_power_on()
973 ret = clk_prepare_enable(sensor->clk); in imx415_power_on()
987 gpiod_set_value_cansleep(sensor->reset, 1); in imx415_power_on()
988 regulator_bulk_disable(ARRAY_SIZE(sensor->supplies), sensor->supplies); in imx415_power_on()
992 static void imx415_power_off(struct imx415 *sensor) in imx415_power_off() argument
994 clk_disable_unprepare(sensor->clk); in imx415_power_off()
995 gpiod_set_value_cansleep(sensor->reset, 1); in imx415_power_off()
996 regulator_bulk_disable(ARRAY_SIZE(sensor->supplies), sensor->supplies); in imx415_power_off()
999 static int imx415_identify_model(struct imx415 *sensor) in imx415_identify_model() argument
1004 * While most registers can be read when the sensor is in standby, this in imx415_identify_model()
1005 * is not the case of the sensor info register :-( in imx415_identify_model()
1007 ret = imx415_wakeup(sensor); in imx415_identify_model()
1009 return dev_err_probe(sensor->dev, ret, in imx415_identify_model()
1010 "failed to get sensor out of standby\n"); in imx415_identify_model()
1012 ret = imx415_read(sensor, IMX415_SENSOR_INFO); in imx415_identify_model()
1014 dev_err_probe(sensor->dev, ret, in imx415_identify_model()
1015 "failed to read sensor information\n"); in imx415_identify_model()
1023 dev_info(sensor->dev, "Detected IMX415 image sensor\n"); in imx415_identify_model()
1026 ret = dev_err_probe(sensor->dev, -ENODEV, in imx415_identify_model()
1034 imx415_write(sensor, IMX415_MODE, IMX415_MODE_STANDBY); in imx415_identify_model()
1054 static int imx415_parse_hw_config(struct imx415 *sensor) in imx415_parse_hw_config() argument
1065 for (i = 0; i < ARRAY_SIZE(sensor->supplies); ++i) in imx415_parse_hw_config()
1066 sensor->supplies[i].supply = imx415_supply_names[i]; in imx415_parse_hw_config()
1068 ret = devm_regulator_bulk_get(sensor->dev, ARRAY_SIZE(sensor->supplies), in imx415_parse_hw_config()
1069 sensor->supplies); in imx415_parse_hw_config()
1071 return dev_err_probe(sensor->dev, ret, in imx415_parse_hw_config()
1074 sensor->reset = devm_gpiod_get_optional(sensor->dev, "reset", in imx415_parse_hw_config()
1076 if (IS_ERR(sensor->reset)) in imx415_parse_hw_config()
1077 return dev_err_probe(sensor->dev, PTR_ERR(sensor->reset), in imx415_parse_hw_config()
1080 sensor->clk = devm_clk_get(sensor->dev, "inck"); in imx415_parse_hw_config()
1081 if (IS_ERR(sensor->clk)) in imx415_parse_hw_config()
1082 return dev_err_probe(sensor->dev, PTR_ERR(sensor->clk), in imx415_parse_hw_config()
1085 ep = fwnode_graph_get_next_endpoint(dev_fwnode(sensor->dev), NULL); in imx415_parse_hw_config()
1097 sensor->num_data_lanes = bus_cfg.bus.mipi_csi2.num_data_lanes; in imx415_parse_hw_config()
1100 ret = dev_err_probe(sensor->dev, -EINVAL, in imx415_parse_hw_config()
1107 ret = dev_err_probe(sensor->dev, -EINVAL, in imx415_parse_hw_config()
1113 * Check if there exists a sensor mode defined for current INCK, in imx415_parse_hw_config()
1116 inck = clk_get_rate(sensor->clk); in imx415_parse_hw_config()
1119 dev_dbg(sensor->dev, in imx415_parse_hw_config()
1126 if (sensor->num_data_lanes != supported_modes[j].lanes) in imx415_parse_hw_config()
1131 sensor->cur_mode = j; in imx415_parse_hw_config()
1138 ret = dev_err_probe(sensor->dev, -EINVAL, in imx415_parse_hw_config()
1139 "no valid sensor mode defined\n"); in imx415_parse_hw_config()
1143 lane_rate = supported_modes[sensor->cur_mode].lane_rate; in imx415_parse_hw_config()
1147 sensor->clk_params = &imx415_clk_params[i]; in imx415_parse_hw_config()
1152 ret = dev_err_probe(sensor->dev, -EINVAL, in imx415_parse_hw_config()
1154 sensor->cur_mode); in imx415_parse_hw_config()
1159 dev_dbg(sensor->dev, "clock: %lu Hz, lane_rate: %llu bps, lanes: %d\n", in imx415_parse_hw_config()
1160 inck, lane_rate, sensor->num_data_lanes); in imx415_parse_hw_config()
1170 struct imx415 *sensor; in imx415_probe() local
1173 sensor = devm_kzalloc(&client->dev, sizeof(*sensor), GFP_KERNEL); in imx415_probe()
1174 if (!sensor) in imx415_probe()
1177 sensor->dev = &client->dev; in imx415_probe()
1179 ret = imx415_parse_hw_config(sensor); in imx415_probe()
1183 sensor->regmap = devm_regmap_init_i2c(client, &imx415_regmap_config); in imx415_probe()
1184 if (IS_ERR(sensor->regmap)) in imx415_probe()
1185 return PTR_ERR(sensor->regmap); in imx415_probe()
1190 * the sensor on manually here, identify it, and fully initialize it. in imx415_probe()
1192 ret = imx415_power_on(sensor); in imx415_probe()
1196 ret = imx415_identify_model(sensor); in imx415_probe()
1200 ret = imx415_subdev_init(sensor); in imx415_probe()
1208 pm_runtime_set_active(sensor->dev); in imx415_probe()
1209 pm_runtime_get_noresume(sensor->dev); in imx415_probe()
1210 pm_runtime_enable(sensor->dev); in imx415_probe()
1212 ret = v4l2_async_register_subdev_sensor(&sensor->subdev); in imx415_probe()
1221 pm_runtime_set_autosuspend_delay(sensor->dev, 1000); in imx415_probe()
1222 pm_runtime_use_autosuspend(sensor->dev); in imx415_probe()
1223 pm_runtime_put_autosuspend(sensor->dev); in imx415_probe()
1228 pm_runtime_disable(sensor->dev); in imx415_probe()
1229 pm_runtime_put_noidle(sensor->dev); in imx415_probe()
1230 imx415_subdev_cleanup(sensor); in imx415_probe()
1232 imx415_power_off(sensor); in imx415_probe()
1239 struct imx415 *sensor = to_imx415(subdev); in imx415_remove() local
1243 imx415_subdev_cleanup(sensor); in imx415_remove()
1249 pm_runtime_disable(sensor->dev); in imx415_remove()
1250 if (!pm_runtime_status_suspended(sensor->dev)) in imx415_remove()
1251 imx415_power_off(sensor); in imx415_remove()
1252 pm_runtime_set_suspended(sensor->dev); in imx415_remove()
1259 struct imx415 *sensor = to_imx415(subdev); in imx415_runtime_resume() local
1261 return imx415_power_on(sensor); in imx415_runtime_resume()
1268 struct imx415 *sensor = to_imx415(subdev); in imx415_runtime_suspend() local
1270 imx415_power_off(sensor); in imx415_runtime_suspend()
1297 MODULE_DESCRIPTION("Sony IMX415 image sensor driver");