Lines Matching full:sensor
3 * Driver for IMX296 CMOS Image Sensor from Sony
217 static int imx296_read(struct imx296 *sensor, u32 addr) in imx296_read() argument
222 ret = regmap_raw_read(sensor->regmap, addr & IMX296_REG_ADDR_MASK, data, in imx296_read()
230 static int imx296_write(struct imx296 *sensor, u32 addr, u32 value, int *err) in imx296_write() argument
238 ret = regmap_raw_write(sensor->regmap, addr & IMX296_REG_ADDR_MASK, in imx296_write()
241 dev_err(sensor->dev, "%u-bit write to 0x%04x failed: %d\n", in imx296_write()
251 static int imx296_power_on(struct imx296 *sensor) in imx296_power_on() argument
255 ret = regulator_bulk_enable(ARRAY_SIZE(sensor->supplies), in imx296_power_on()
256 sensor->supplies); in imx296_power_on()
262 ret = gpiod_direction_output(sensor->reset, 0); in imx296_power_on()
268 ret = clk_prepare_enable(sensor->clk); in imx296_power_on()
284 gpiod_direction_output(sensor->reset, 1); in imx296_power_on()
286 regulator_bulk_disable(ARRAY_SIZE(sensor->supplies), sensor->supplies); in imx296_power_on()
290 static void imx296_power_off(struct imx296 *sensor) in imx296_power_off() argument
292 clk_disable_unprepare(sensor->clk); in imx296_power_off()
293 gpiod_direction_output(sensor->reset, 1); in imx296_power_off()
294 regulator_bulk_disable(ARRAY_SIZE(sensor->supplies), sensor->supplies); in imx296_power_off()
316 struct imx296 *sensor = container_of(ctrl->handler, struct imx296, ctrls); in imx296_s_ctrl() local
322 if (!pm_runtime_get_if_in_use(sensor->dev)) in imx296_s_ctrl()
325 state = v4l2_subdev_get_locked_active_state(&sensor->subdev); in imx296_s_ctrl()
331 vmax = format->height + sensor->vblank->cur.val; in imx296_s_ctrl()
333 imx296_write(sensor, IMX296_SHS1, vmax - ctrl->val, &ret); in imx296_s_ctrl()
337 imx296_write(sensor, IMX296_GAIN, ctrl->val, &ret); in imx296_s_ctrl()
341 imx296_write(sensor, IMX296_VMAX, format->height + ctrl->val, in imx296_s_ctrl()
347 imx296_write(sensor, IMX296_PGHPOS, 8, &ret); in imx296_s_ctrl()
348 imx296_write(sensor, IMX296_PGVPOS, 8, &ret); in imx296_s_ctrl()
349 imx296_write(sensor, IMX296_PGHPSTEP, 8, &ret); in imx296_s_ctrl()
350 imx296_write(sensor, IMX296_PGVPSTEP, 8, &ret); in imx296_s_ctrl()
351 imx296_write(sensor, IMX296_PGHPNUM, 100, &ret); in imx296_s_ctrl()
352 imx296_write(sensor, IMX296_PGVPNUM, 100, &ret); in imx296_s_ctrl()
353 imx296_write(sensor, IMX296_PGDATA1, 0x300, &ret); in imx296_s_ctrl()
354 imx296_write(sensor, IMX296_PGDATA2, 0x100, &ret); in imx296_s_ctrl()
355 imx296_write(sensor, IMX296_PGHGSTEP, 0, &ret); in imx296_s_ctrl()
356 imx296_write(sensor, IMX296_BLKLEVEL, 0, &ret); in imx296_s_ctrl()
357 imx296_write(sensor, IMX296_BLKLEVELAUTO, in imx296_s_ctrl()
359 imx296_write(sensor, IMX296_PGCTRL, in imx296_s_ctrl()
364 imx296_write(sensor, IMX296_PGCTRL, in imx296_s_ctrl()
366 imx296_write(sensor, IMX296_BLKLEVEL, 0x3c, &ret); in imx296_s_ctrl()
367 imx296_write(sensor, IMX296_BLKLEVELAUTO, in imx296_s_ctrl()
377 pm_runtime_put(sensor->dev); in imx296_s_ctrl()
386 static int imx296_ctrls_init(struct imx296 *sensor) in imx296_ctrls_init() argument
392 ret = v4l2_fwnode_device_parse(sensor->dev, &props); in imx296_ctrls_init()
396 v4l2_ctrl_handler_init(&sensor->ctrls, 9); in imx296_ctrls_init()
398 v4l2_ctrl_new_std(&sensor->ctrls, &imx296_ctrl_ops, in imx296_ctrls_init()
400 v4l2_ctrl_new_std(&sensor->ctrls, &imx296_ctrl_ops, in imx296_ctrls_init()
412 sensor->hblank = v4l2_ctrl_new_std(&sensor->ctrls, &imx296_ctrl_ops, in imx296_ctrls_init()
415 if (sensor->hblank) in imx296_ctrls_init()
416 sensor->hblank->flags |= V4L2_CTRL_FLAG_READ_ONLY; in imx296_ctrls_init()
418 sensor->vblank = v4l2_ctrl_new_std(&sensor->ctrls, &imx296_ctrl_ops, in imx296_ctrls_init()
423 * The sensor calculates the MIPI timings internally to achieve a bit in imx296_ctrls_init()
429 v4l2_ctrl_new_std(&sensor->ctrls, NULL, V4L2_CID_PIXEL_RATE, in imx296_ctrls_init()
431 v4l2_ctrl_new_std_menu_items(&sensor->ctrls, &imx296_ctrl_ops, in imx296_ctrls_init()
436 v4l2_ctrl_new_fwnode_properties(&sensor->ctrls, &imx296_ctrl_ops, in imx296_ctrls_init()
439 if (sensor->ctrls.error) { in imx296_ctrls_init()
440 dev_err(sensor->dev, "failed to add controls (%d)\n", in imx296_ctrls_init()
441 sensor->ctrls.error); in imx296_ctrls_init()
442 v4l2_ctrl_handler_free(&sensor->ctrls); in imx296_ctrls_init()
443 return sensor->ctrls.error; in imx296_ctrls_init()
446 sensor->subdev.ctrl_handler = &sensor->ctrls; in imx296_ctrls_init()
507 static int imx296_setup(struct imx296 *sensor, struct v4l2_subdev_state *state) in imx296_setup() argument
518 imx296_write(sensor, imx296_init_table[i].reg, in imx296_setup()
523 imx296_write(sensor, IMX296_FID0_ROI, in imx296_setup()
525 imx296_write(sensor, IMX296_FID0_ROIPH1, crop->left, &ret); in imx296_setup()
526 imx296_write(sensor, IMX296_FID0_ROIPV1, crop->top, &ret); in imx296_setup()
527 imx296_write(sensor, IMX296_FID0_ROIWH1, crop->width, &ret); in imx296_setup()
528 imx296_write(sensor, IMX296_FID0_ROIWV1, crop->height, &ret); in imx296_setup()
530 imx296_write(sensor, IMX296_FID0_ROI, 0, &ret); in imx296_setup()
533 imx296_write(sensor, IMX296_CTRL0D, in imx296_setup()
548 * In the vertical direction the sensor outputs the following: in imx296_setup()
558 imx296_write(sensor, IMX296_HMAX, 1100, &ret); in imx296_setup()
559 imx296_write(sensor, IMX296_VMAX, in imx296_setup()
560 format->height + sensor->vblank->cur.val, &ret); in imx296_setup()
562 for (i = 0; i < ARRAY_SIZE(sensor->clk_params->incksel); ++i) in imx296_setup()
563 imx296_write(sensor, IMX296_INCKSEL(i), in imx296_setup()
564 sensor->clk_params->incksel[i], &ret); in imx296_setup()
565 imx296_write(sensor, IMX296_GTTABLENUM, 0xc5, &ret); in imx296_setup()
566 imx296_write(sensor, IMX296_CTRL418C, sensor->clk_params->ctrl418c, in imx296_setup()
569 imx296_write(sensor, IMX296_GAINDLY, IMX296_GAINDLY_NONE, &ret); in imx296_setup()
570 imx296_write(sensor, IMX296_BLKLEVEL, 0x03c, &ret); in imx296_setup()
575 static int imx296_stream_on(struct imx296 *sensor) in imx296_stream_on() argument
579 imx296_write(sensor, IMX296_CTRL00, 0, &ret); in imx296_stream_on()
581 imx296_write(sensor, IMX296_CTRL0A, 0, &ret); in imx296_stream_on()
586 static int imx296_stream_off(struct imx296 *sensor) in imx296_stream_off() argument
590 imx296_write(sensor, IMX296_CTRL0A, IMX296_CTRL0A_XMSTA, &ret); in imx296_stream_off()
591 imx296_write(sensor, IMX296_CTRL00, IMX296_CTRL00_STANDBY, &ret); in imx296_stream_off()
598 struct imx296 *sensor = to_imx296(sd); in imx296_s_stream() local
605 ret = imx296_stream_off(sensor); in imx296_s_stream()
607 pm_runtime_mark_last_busy(sensor->dev); in imx296_s_stream()
608 pm_runtime_put_autosuspend(sensor->dev); in imx296_s_stream()
613 ret = pm_runtime_resume_and_get(sensor->dev); in imx296_s_stream()
617 ret = imx296_setup(sensor, state); in imx296_s_stream()
621 ret = __v4l2_ctrl_handler_setup(&sensor->ctrls); in imx296_s_stream()
625 ret = imx296_stream_on(sensor); in imx296_s_stream()
639 pm_runtime_put_sync(sensor->dev); in imx296_s_stream()
648 struct imx296 *sensor = to_imx296(sd); in imx296_enum_mbus_code() local
653 code->code = sensor->mono ? MEDIA_BUS_FMT_Y10_1X10 in imx296_enum_mbus_code()
682 struct imx296 *sensor = to_imx296(sd); in imx296_set_format() local
716 format->code = sensor->mono ? MEDIA_BUS_FMT_Y10_1X10 in imx296_set_format()
844 static int imx296_subdev_init(struct imx296 *sensor) in imx296_subdev_init() argument
846 struct i2c_client *client = to_i2c_client(sensor->dev); in imx296_subdev_init()
849 v4l2_i2c_subdev_init(&sensor->subdev, client, &imx296_subdev_ops); in imx296_subdev_init()
850 sensor->subdev.internal_ops = &imx296_internal_ops; in imx296_subdev_init()
852 ret = imx296_ctrls_init(sensor); in imx296_subdev_init()
856 sensor->subdev.flags |= V4L2_SUBDEV_FL_HAS_DEVNODE; in imx296_subdev_init()
857 sensor->pad.flags = MEDIA_PAD_FL_SOURCE; in imx296_subdev_init()
858 sensor->subdev.entity.function = MEDIA_ENT_F_CAM_SENSOR; in imx296_subdev_init()
859 ret = media_entity_pads_init(&sensor->subdev.entity, 1, &sensor->pad); in imx296_subdev_init()
861 v4l2_ctrl_handler_free(&sensor->ctrls); in imx296_subdev_init()
865 sensor->subdev.state_lock = sensor->subdev.ctrl_handler->lock; in imx296_subdev_init()
867 v4l2_subdev_init_finalize(&sensor->subdev); in imx296_subdev_init()
872 static void imx296_subdev_cleanup(struct imx296 *sensor) in imx296_subdev_cleanup() argument
874 media_entity_cleanup(&sensor->subdev.entity); in imx296_subdev_cleanup()
875 v4l2_ctrl_handler_free(&sensor->ctrls); in imx296_subdev_cleanup()
886 struct imx296 *sensor = to_imx296(subdev); in imx296_runtime_resume() local
888 return imx296_power_on(sensor); in imx296_runtime_resume()
895 struct imx296 *sensor = to_imx296(subdev); in imx296_runtime_suspend() local
897 imx296_power_off(sensor); in imx296_runtime_suspend()
910 static int imx296_read_temperature(struct imx296 *sensor, int *temp) in imx296_read_temperature() argument
915 ret = imx296_write(sensor, IMX296_TMDCTRL, IMX296_TMDCTRL_LATCH, NULL); in imx296_read_temperature()
919 tmdout = imx296_read(sensor, IMX296_TMDOUT); in imx296_read_temperature()
928 return imx296_write(sensor, IMX296_TMDCTRL, 0, NULL); in imx296_read_temperature()
931 static int imx296_identify_model(struct imx296 *sensor) in imx296_identify_model() argument
937 model = (uintptr_t)of_device_get_match_data(sensor->dev); in imx296_identify_model()
939 dev_dbg(sensor->dev, in imx296_identify_model()
940 "sensor model auto-detection disabled, forcing 0x%04x\n", in imx296_identify_model()
942 sensor->mono = model & IMX296_SENSOR_INFO_MONO; in imx296_identify_model()
947 * While most registers can be read when the sensor is in standby, this in imx296_identify_model()
948 * is not the case of the sensor info register :-( in imx296_identify_model()
950 ret = imx296_write(sensor, IMX296_CTRL00, 0, NULL); in imx296_identify_model()
952 dev_err(sensor->dev, in imx296_identify_model()
953 "failed to get sensor out of standby (%d)\n", ret); in imx296_identify_model()
957 ret = imx296_read(sensor, IMX296_SENSOR_INFO); in imx296_identify_model()
959 dev_err(sensor->dev, "failed to read sensor information (%d)\n", in imx296_identify_model()
968 sensor->mono = ret & IMX296_SENSOR_INFO_MONO; in imx296_identify_model()
976 dev_err(sensor->dev, "invalid device model 0x%04x\n", ret); in imx296_identify_model()
981 ret = imx296_read_temperature(sensor, &temp); in imx296_identify_model()
985 dev_info(sensor->dev, "found IMX%u%s (%u.%uC)\n", model, in imx296_identify_model()
986 sensor->mono ? "LL" : "LQ", temp / 1000, (temp / 100) % 10); in imx296_identify_model()
989 imx296_write(sensor, IMX296_CTRL00, IMX296_CTRL00_STANDBY, NULL); in imx296_identify_model()
1012 struct imx296 *sensor; in imx296_probe() local
1022 sensor = devm_kzalloc(&client->dev, sizeof(*sensor), GFP_KERNEL); in imx296_probe()
1023 if (!sensor) in imx296_probe()
1026 sensor->dev = &client->dev; in imx296_probe()
1029 for (i = 0; i < ARRAY_SIZE(sensor->supplies); ++i) in imx296_probe()
1030 sensor->supplies[i].supply = imx296_supply_names[i]; in imx296_probe()
1032 ret = devm_regulator_bulk_get(sensor->dev, ARRAY_SIZE(sensor->supplies), in imx296_probe()
1033 sensor->supplies); in imx296_probe()
1035 dev_err_probe(sensor->dev, ret, "failed to get supplies\n"); in imx296_probe()
1039 sensor->reset = devm_gpiod_get_optional(sensor->dev, "reset", in imx296_probe()
1041 if (IS_ERR(sensor->reset)) in imx296_probe()
1042 return dev_err_probe(sensor->dev, PTR_ERR(sensor->reset), in imx296_probe()
1045 sensor->clk = devm_clk_get(sensor->dev, "inck"); in imx296_probe()
1046 if (IS_ERR(sensor->clk)) in imx296_probe()
1047 return dev_err_probe(sensor->dev, PTR_ERR(sensor->clk), in imx296_probe()
1050 clk_rate = clk_get_rate(sensor->clk); in imx296_probe()
1053 sensor->clk_params = &imx296_clk_params[i]; in imx296_probe()
1058 if (!sensor->clk_params) { in imx296_probe()
1059 dev_err(sensor->dev, "unsupported clock rate %lu\n", clk_rate); in imx296_probe()
1063 sensor->regmap = devm_regmap_init_i2c(client, &imx296_regmap_config); in imx296_probe()
1064 if (IS_ERR(sensor->regmap)) in imx296_probe()
1065 return PTR_ERR(sensor->regmap); in imx296_probe()
1070 * the sensor on manually here, identify it, and fully initialize it. in imx296_probe()
1072 ret = imx296_power_on(sensor); in imx296_probe()
1076 ret = imx296_identify_model(sensor); in imx296_probe()
1081 ret = imx296_subdev_init(sensor); in imx296_probe()
1089 pm_runtime_set_active(sensor->dev); in imx296_probe()
1090 pm_runtime_get_noresume(sensor->dev); in imx296_probe()
1091 pm_runtime_enable(sensor->dev); in imx296_probe()
1094 ret = v4l2_async_register_subdev(&sensor->subdev); in imx296_probe()
1103 pm_runtime_set_autosuspend_delay(sensor->dev, 1000); in imx296_probe()
1104 pm_runtime_use_autosuspend(sensor->dev); in imx296_probe()
1105 pm_runtime_put_autosuspend(sensor->dev); in imx296_probe()
1110 pm_runtime_disable(sensor->dev); in imx296_probe()
1111 pm_runtime_put_noidle(sensor->dev); in imx296_probe()
1112 imx296_subdev_cleanup(sensor); in imx296_probe()
1114 imx296_power_off(sensor); in imx296_probe()
1121 struct imx296 *sensor = to_imx296(subdev); in imx296_remove() local
1125 imx296_subdev_cleanup(sensor); in imx296_remove()
1131 pm_runtime_disable(sensor->dev); in imx296_remove()
1132 if (!pm_runtime_status_suspended(sensor->dev)) in imx296_remove()
1133 imx296_power_off(sensor); in imx296_remove()
1134 pm_runtime_set_suspended(sensor->dev); in imx296_remove()