Lines Matching full:sensor
3 * Omnivision OV2680 CMOS Image Sensor driver
7 * Based on OV5640 Sensor Driver
205 /* Sensor control register tweaks */
272 static void ov2680_power_up(struct ov2680_dev *sensor) in ov2680_power_up() argument
274 if (!sensor->pwdn_gpio) in ov2680_power_up()
277 gpiod_set_value(sensor->pwdn_gpio, 0); in ov2680_power_up()
281 static void ov2680_power_down(struct ov2680_dev *sensor) in ov2680_power_down() argument
283 if (!sensor->pwdn_gpio) in ov2680_power_down()
286 gpiod_set_value(sensor->pwdn_gpio, 1); in ov2680_power_down()
290 static void ov2680_set_bayer_order(struct ov2680_dev *sensor, in ov2680_set_bayer_order() argument
295 if (sensor->ctrls.vflip && sensor->ctrls.vflip->val) in ov2680_set_bayer_order()
298 if (sensor->ctrls.hflip && sensor->ctrls.hflip->val) in ov2680_set_bayer_order()
305 __ov2680_get_pad_format(struct ov2680_dev *sensor, in __ov2680_get_pad_format() argument
313 return &sensor->mode.fmt; in __ov2680_get_pad_format()
317 __ov2680_get_pad_crop(struct ov2680_dev *sensor, in __ov2680_get_pad_crop() argument
325 return &sensor->mode.crop; in __ov2680_get_pad_crop()
328 static void ov2680_fill_format(struct ov2680_dev *sensor, in ov2680_fill_format() argument
337 ov2680_set_bayer_order(sensor, fmt); in ov2680_fill_format()
340 static void ov2680_calc_mode(struct ov2680_dev *sensor) in ov2680_calc_mode() argument
342 int width = sensor->mode.fmt.width; in ov2680_calc_mode()
343 int height = sensor->mode.fmt.height; in ov2680_calc_mode()
347 if (width <= (sensor->mode.crop.width / 2) && in ov2680_calc_mode()
348 height <= (sensor->mode.crop.height / 2)) { in ov2680_calc_mode()
349 sensor->mode.binning = true; in ov2680_calc_mode()
353 sensor->mode.binning = false; in ov2680_calc_mode()
356 sensor->mode.h_start = (sensor->mode.crop.left + in ov2680_calc_mode()
357 (sensor->mode.crop.width - width) / 2) & ~1; in ov2680_calc_mode()
358 sensor->mode.v_start = (sensor->mode.crop.top + in ov2680_calc_mode()
359 (sensor->mode.crop.height - height) / 2) & ~1; in ov2680_calc_mode()
360 sensor->mode.h_end = in ov2680_calc_mode()
361 min(sensor->mode.h_start + width - 1, OV2680_NATIVE_WIDTH - 1); in ov2680_calc_mode()
362 sensor->mode.v_end = in ov2680_calc_mode()
363 min(sensor->mode.v_start + height - 1, OV2680_NATIVE_HEIGHT - 1); in ov2680_calc_mode()
364 sensor->mode.h_output_size = orig_width; in ov2680_calc_mode()
365 sensor->mode.v_output_size = orig_height; in ov2680_calc_mode()
368 static int ov2680_set_mode(struct ov2680_dev *sensor) in ov2680_set_mode() argument
373 if (sensor->mode.binning) { in ov2680_set_mode()
385 cci_write(sensor->regmap, OV2680_REG_SENSOR_CTRL_0A, in ov2680_set_mode()
387 cci_write(sensor->regmap, OV2680_REG_HORIZONTAL_START, in ov2680_set_mode()
388 sensor->mode.h_start, &ret); in ov2680_set_mode()
389 cci_write(sensor->regmap, OV2680_REG_VERTICAL_START, in ov2680_set_mode()
390 sensor->mode.v_start, &ret); in ov2680_set_mode()
391 cci_write(sensor->regmap, OV2680_REG_HORIZONTAL_END, in ov2680_set_mode()
392 sensor->mode.h_end, &ret); in ov2680_set_mode()
393 cci_write(sensor->regmap, OV2680_REG_VERTICAL_END, in ov2680_set_mode()
394 sensor->mode.v_end, &ret); in ov2680_set_mode()
395 cci_write(sensor->regmap, OV2680_REG_HORIZONTAL_OUTPUT_SIZE, in ov2680_set_mode()
396 sensor->mode.h_output_size, &ret); in ov2680_set_mode()
397 cci_write(sensor->regmap, OV2680_REG_VERTICAL_OUTPUT_SIZE, in ov2680_set_mode()
398 sensor->mode.v_output_size, &ret); in ov2680_set_mode()
399 cci_write(sensor->regmap, OV2680_REG_TIMING_HTS, in ov2680_set_mode()
402 cci_write(sensor->regmap, OV2680_REG_ISP_X_WIN, 0, &ret); in ov2680_set_mode()
403 cci_write(sensor->regmap, OV2680_REG_ISP_Y_WIN, 0, &ret); in ov2680_set_mode()
404 cci_write(sensor->regmap, OV2680_REG_X_INC, inc, &ret); in ov2680_set_mode()
405 cci_write(sensor->regmap, OV2680_REG_Y_INC, inc, &ret); in ov2680_set_mode()
406 cci_write(sensor->regmap, OV2680_REG_X_WIN, in ov2680_set_mode()
407 sensor->mode.h_output_size, &ret); in ov2680_set_mode()
408 cci_write(sensor->regmap, OV2680_REG_Y_WIN, in ov2680_set_mode()
409 sensor->mode.v_output_size, &ret); in ov2680_set_mode()
410 cci_write(sensor->regmap, OV2680_REG_FORMAT1, fmt1, &ret); in ov2680_set_mode()
411 cci_write(sensor->regmap, OV2680_REG_FORMAT2, fmt2, &ret); in ov2680_set_mode()
416 static int ov2680_set_vflip(struct ov2680_dev *sensor, s32 val) in ov2680_set_vflip() argument
420 if (sensor->is_streaming) in ov2680_set_vflip()
423 ret = cci_update_bits(sensor->regmap, OV2680_REG_FORMAT1, in ov2680_set_vflip()
428 ov2680_set_bayer_order(sensor, &sensor->mode.fmt); in ov2680_set_vflip()
432 static int ov2680_set_hflip(struct ov2680_dev *sensor, s32 val) in ov2680_set_hflip() argument
436 if (sensor->is_streaming) in ov2680_set_hflip()
439 ret = cci_update_bits(sensor->regmap, OV2680_REG_FORMAT2, in ov2680_set_hflip()
444 ov2680_set_bayer_order(sensor, &sensor->mode.fmt); in ov2680_set_hflip()
448 static int ov2680_test_pattern_set(struct ov2680_dev *sensor, int value) in ov2680_test_pattern_set() argument
453 return cci_update_bits(sensor->regmap, OV2680_REG_ISP_CTRL00, in ov2680_test_pattern_set()
456 cci_update_bits(sensor->regmap, OV2680_REG_ISP_CTRL00, in ov2680_test_pattern_set()
458 cci_update_bits(sensor->regmap, OV2680_REG_ISP_CTRL00, in ov2680_test_pattern_set()
464 static int ov2680_gain_set(struct ov2680_dev *sensor, u32 gain) in ov2680_gain_set() argument
466 return cci_write(sensor->regmap, OV2680_REG_GAIN_PK, gain, NULL); in ov2680_gain_set()
469 static int ov2680_exposure_set(struct ov2680_dev *sensor, u32 exp) in ov2680_exposure_set() argument
471 return cci_write(sensor->regmap, OV2680_REG_EXPOSURE_PK, exp << 4, in ov2680_exposure_set()
475 static int ov2680_exposure_update_range(struct ov2680_dev *sensor) in ov2680_exposure_update_range() argument
477 int exp_max = sensor->mode.fmt.height + sensor->ctrls.vblank->val - in ov2680_exposure_update_range()
480 return __v4l2_ctrl_modify_range(sensor->ctrls.exposure, 0, exp_max, in ov2680_exposure_update_range()
484 static int ov2680_stream_enable(struct ov2680_dev *sensor) in ov2680_stream_enable() argument
488 ret = cci_write(sensor->regmap, OV2680_REG_PLL_MULTIPLIER, in ov2680_stream_enable()
489 sensor->pll_mult, NULL); in ov2680_stream_enable()
493 ret = regmap_multi_reg_write(sensor->regmap, in ov2680_stream_enable()
499 ret = ov2680_set_mode(sensor); in ov2680_stream_enable()
504 ret = __v4l2_ctrl_handler_setup(&sensor->ctrls.handler); in ov2680_stream_enable()
508 return cci_write(sensor->regmap, OV2680_REG_STREAM_CTRL, 1, NULL); in ov2680_stream_enable()
511 static int ov2680_stream_disable(struct ov2680_dev *sensor) in ov2680_stream_disable() argument
513 return cci_write(sensor->regmap, OV2680_REG_STREAM_CTRL, 0, NULL); in ov2680_stream_disable()
516 static int ov2680_power_off(struct ov2680_dev *sensor) in ov2680_power_off() argument
518 clk_disable_unprepare(sensor->xvclk); in ov2680_power_off()
519 ov2680_power_down(sensor); in ov2680_power_off()
520 regulator_bulk_disable(OV2680_NUM_SUPPLIES, sensor->supplies); in ov2680_power_off()
524 static int ov2680_power_on(struct ov2680_dev *sensor) in ov2680_power_on() argument
528 ret = regulator_bulk_enable(OV2680_NUM_SUPPLIES, sensor->supplies); in ov2680_power_on()
530 dev_err(sensor->dev, "failed to enable regulators: %d\n", ret); in ov2680_power_on()
534 if (!sensor->pwdn_gpio) { in ov2680_power_on()
535 ret = cci_write(sensor->regmap, OV2680_REG_SOFT_RESET, 0x01, in ov2680_power_on()
538 dev_err(sensor->dev, "sensor soft reset failed\n"); in ov2680_power_on()
543 ov2680_power_down(sensor); in ov2680_power_on()
544 ov2680_power_up(sensor); in ov2680_power_on()
547 ret = clk_prepare_enable(sensor->xvclk); in ov2680_power_on()
554 regulator_bulk_disable(OV2680_NUM_SUPPLIES, sensor->supplies); in ov2680_power_on()
562 struct ov2680_dev *sensor = to_ov2680_dev(sd); in ov2680_get_frame_interval() local
571 mutex_lock(&sensor->lock); in ov2680_get_frame_interval()
572 fi->interval = sensor->mode.frame_interval; in ov2680_get_frame_interval()
573 mutex_unlock(&sensor->lock); in ov2680_get_frame_interval()
580 struct ov2680_dev *sensor = to_ov2680_dev(sd); in ov2680_s_stream() local
583 mutex_lock(&sensor->lock); in ov2680_s_stream()
585 if (sensor->is_streaming == !!enable) in ov2680_s_stream()
589 ret = pm_runtime_resume_and_get(sensor->sd.dev); in ov2680_s_stream()
593 ret = ov2680_stream_enable(sensor); in ov2680_s_stream()
595 pm_runtime_put(sensor->sd.dev); in ov2680_s_stream()
599 ret = ov2680_stream_disable(sensor); in ov2680_s_stream()
600 pm_runtime_put(sensor->sd.dev); in ov2680_s_stream()
603 sensor->is_streaming = !!enable; in ov2680_s_stream()
606 mutex_unlock(&sensor->lock); in ov2680_s_stream()
615 struct ov2680_dev *sensor = to_ov2680_dev(sd); in ov2680_enum_mbus_code() local
620 code->code = sensor->mode.fmt.code; in ov2680_enum_mbus_code()
629 struct ov2680_dev *sensor = to_ov2680_dev(sd); in ov2680_get_fmt() local
632 fmt = __ov2680_get_pad_format(sensor, sd_state, format->pad, in ov2680_get_fmt()
635 mutex_lock(&sensor->lock); in ov2680_get_fmt()
637 mutex_unlock(&sensor->lock); in ov2680_get_fmt()
646 struct ov2680_dev *sensor = to_ov2680_dev(sd); in ov2680_set_fmt() local
652 crop = __ov2680_get_pad_crop(sensor, sd_state, format->pad, in ov2680_set_fmt()
661 ov2680_fill_format(sensor, &format->format, width, height); in ov2680_set_fmt()
669 mutex_lock(&sensor->lock); in ov2680_set_fmt()
671 if (sensor->is_streaming) { in ov2680_set_fmt()
676 sensor->mode.fmt = format->format; in ov2680_set_fmt()
677 ov2680_calc_mode(sensor); in ov2680_set_fmt()
682 ret = __v4l2_ctrl_modify_range(sensor->ctrls.vblank, OV2680_MIN_VBLANK, in ov2680_set_fmt()
687 ret = __v4l2_ctrl_s_ctrl(sensor->ctrls.vblank, def); in ov2680_set_fmt()
692 ret = ov2680_exposure_update_range(sensor); in ov2680_set_fmt()
698 ret = __v4l2_ctrl_modify_range(sensor->ctrls.hblank, def, def, 1, def); in ov2680_set_fmt()
701 mutex_unlock(&sensor->lock); in ov2680_set_fmt()
710 struct ov2680_dev *sensor = to_ov2680_dev(sd); in ov2680_get_selection() local
714 mutex_lock(&sensor->lock); in ov2680_get_selection()
715 sel->r = *__ov2680_get_pad_crop(sensor, state, sel->pad, in ov2680_get_selection()
717 mutex_unlock(&sensor->lock); in ov2680_get_selection()
740 struct ov2680_dev *sensor = to_ov2680_dev(sd); in ov2680_set_selection() local
749 * Clamp the boundaries of the crop rectangle to the size of the sensor in ov2680_set_selection()
768 crop = __ov2680_get_pad_crop(sensor, state, sel->pad, sel->which); in ov2680_set_selection()
770 mutex_lock(&sensor->lock); in ov2680_set_selection()
776 format = __ov2680_get_pad_format(sensor, state, sel->pad, in ov2680_set_selection()
783 mutex_unlock(&sensor->lock); in ov2680_set_selection()
793 struct ov2680_dev *sensor = to_ov2680_dev(sd); in ov2680_init_state() local
797 ov2680_fill_format(sensor, v4l2_subdev_state_get_format(sd_state, 0), in ov2680_init_state()
806 struct ov2680_dev *sensor = to_ov2680_dev(sd); in ov2680_enum_frame_size() local
812 crop = __ov2680_get_pad_crop(sensor, sd_state, fse->pad, fse->which); in ov2680_enum_frame_size()
852 struct ov2680_dev *sensor = to_ov2680_dev(sd); in ov2680_enum_frame_interval() local
858 fie->interval = sensor->mode.frame_interval; in ov2680_enum_frame_interval()
866 struct ov2680_dev *sensor = to_ov2680_dev(sd); in ov2680_s_ctrl() local
871 ret = ov2680_exposure_update_range(sensor); in ov2680_s_ctrl()
877 if (!pm_runtime_get_if_in_use(sensor->sd.dev)) { in ov2680_s_ctrl()
878 ov2680_set_bayer_order(sensor, &sensor->mode.fmt); in ov2680_s_ctrl()
884 ret = ov2680_gain_set(sensor, ctrl->val); in ov2680_s_ctrl()
887 ret = ov2680_exposure_set(sensor, ctrl->val); in ov2680_s_ctrl()
890 ret = ov2680_set_vflip(sensor, ctrl->val); in ov2680_s_ctrl()
893 ret = ov2680_set_hflip(sensor, ctrl->val); in ov2680_s_ctrl()
896 ret = ov2680_test_pattern_set(sensor, ctrl->val); in ov2680_s_ctrl()
899 ret = cci_write(sensor->regmap, OV2680_REG_TIMING_VTS, in ov2680_s_ctrl()
900 sensor->mode.fmt.height + ctrl->val, NULL); in ov2680_s_ctrl()
907 pm_runtime_put(sensor->sd.dev); in ov2680_s_ctrl()
940 static int ov2680_mode_init(struct ov2680_dev *sensor) in ov2680_mode_init() argument
943 sensor->mode.crop = ov2680_default_crop; in ov2680_mode_init()
944 ov2680_fill_format(sensor, &sensor->mode.fmt, in ov2680_mode_init()
946 ov2680_calc_mode(sensor); in ov2680_mode_init()
948 sensor->mode.frame_interval.denominator = OV2680_FRAME_RATE; in ov2680_mode_init()
949 sensor->mode.frame_interval.numerator = 1; in ov2680_mode_init()
954 static int ov2680_v4l2_register(struct ov2680_dev *sensor) in ov2680_v4l2_register() argument
956 struct i2c_client *client = to_i2c_client(sensor->dev); in ov2680_v4l2_register()
958 struct ov2680_ctrls *ctrls = &sensor->ctrls; in ov2680_v4l2_register()
963 v4l2_i2c_subdev_init(&sensor->sd, client, &ov2680_subdev_ops); in ov2680_v4l2_register()
964 sensor->sd.internal_ops = &ov2680_internal_ops; in ov2680_v4l2_register()
966 sensor->sd.flags = V4L2_SUBDEV_FL_HAS_DEVNODE; in ov2680_v4l2_register()
967 sensor->pad.flags = MEDIA_PAD_FL_SOURCE; in ov2680_v4l2_register()
968 sensor->sd.entity.function = MEDIA_ENT_F_CAM_SENSOR; in ov2680_v4l2_register()
970 ret = media_entity_pads_init(&sensor->sd.entity, 1, &sensor->pad); in ov2680_v4l2_register()
976 hdl->lock = &sensor->lock; in ov2680_v4l2_register()
994 0, 0, sensor->link_freq); in ov2680_v4l2_register()
996 0, sensor->pixel_rate, in ov2680_v4l2_register()
997 1, sensor->pixel_rate); in ov2680_v4l2_register()
1008 ret = v4l2_fwnode_device_parse(sensor->dev, &props); in ov2680_v4l2_register()
1024 sensor->sd.ctrl_handler = hdl; in ov2680_v4l2_register()
1026 ret = v4l2_async_register_subdev(&sensor->sd); in ov2680_v4l2_register()
1033 media_entity_cleanup(&sensor->sd.entity); in ov2680_v4l2_register()
1039 static int ov2680_get_regulators(struct ov2680_dev *sensor) in ov2680_get_regulators() argument
1044 sensor->supplies[i].supply = ov2680_supply_name[i]; in ov2680_get_regulators()
1046 return devm_regulator_bulk_get(sensor->dev, in ov2680_get_regulators()
1047 OV2680_NUM_SUPPLIES, sensor->supplies); in ov2680_get_regulators()
1050 static int ov2680_check_id(struct ov2680_dev *sensor) in ov2680_check_id() argument
1055 cci_read(sensor->regmap, OV2680_REG_CHIP_ID, &chip_id, &ret); in ov2680_check_id()
1056 cci_read(sensor->regmap, OV2680_REG_SC_CMMN_SUB_ID, &rev, &ret); in ov2680_check_id()
1058 dev_err(sensor->dev, "failed to read chip id\n"); in ov2680_check_id()
1063 dev_err(sensor->dev, "chip id: 0x%04llx does not match expected 0x%04x\n", in ov2680_check_id()
1068 dev_info(sensor->dev, "sensor_revision id = 0x%llx, rev= %lld\n", in ov2680_check_id()
1074 static int ov2680_parse_dt(struct ov2680_dev *sensor) in ov2680_parse_dt() argument
1079 struct device *dev = sensor->dev; in ov2680_parse_dt()
1100 * The pin we want is named XSHUTDN in the datasheet. Linux sensor in ov2680_parse_dt()
1115 sensor->pwdn_gpio = gpio; in ov2680_parse_dt()
1117 sensor->xvclk = devm_clk_get_optional(dev, "xvclk"); in ov2680_parse_dt()
1118 if (IS_ERR(sensor->xvclk)) { in ov2680_parse_dt()
1119 ret = dev_err_probe(dev, PTR_ERR(sensor->xvclk), in ov2680_parse_dt()
1135 if (ret && !sensor->xvclk) { in ov2680_parse_dt()
1140 if (!ret && sensor->xvclk) { in ov2680_parse_dt()
1141 ret = clk_set_rate(sensor->xvclk, rate); in ov2680_parse_dt()
1148 sensor->xvclk_freq = rate ?: clk_get_rate(sensor->xvclk); in ov2680_parse_dt()
1151 if (sensor->xvclk_freq == ov2680_xvclk_freqs[i]) in ov2680_parse_dt()
1158 sensor->xvclk_freq); in ov2680_parse_dt()
1162 sensor->pll_mult = ov2680_pll_multipliers[i]; in ov2680_parse_dt()
1164 sensor->link_freq[0] = sensor->xvclk_freq / OV2680_PLL_PREDIV0 / in ov2680_parse_dt()
1165 OV2680_PLL_PREDIV * sensor->pll_mult; in ov2680_parse_dt()
1168 sensor->pixel_rate = sensor->link_freq[0] * 2; in ov2680_parse_dt()
1169 do_div(sensor->pixel_rate, 10); in ov2680_parse_dt()
1177 if (bus_cfg.link_frequencies[i] == sensor->link_freq[0]) in ov2680_parse_dt()
1183 sensor->link_freq[0]); in ov2680_parse_dt()
1197 struct ov2680_dev *sensor; in ov2680_probe() local
1200 sensor = devm_kzalloc(dev, sizeof(*sensor), GFP_KERNEL); in ov2680_probe()
1201 if (!sensor) in ov2680_probe()
1204 sensor->dev = &client->dev; in ov2680_probe()
1206 sensor->regmap = devm_cci_regmap_init_i2c(client, 16); in ov2680_probe()
1207 if (IS_ERR(sensor->regmap)) in ov2680_probe()
1208 return PTR_ERR(sensor->regmap); in ov2680_probe()
1210 ret = ov2680_parse_dt(sensor); in ov2680_probe()
1214 ret = ov2680_mode_init(sensor); in ov2680_probe()
1218 ret = ov2680_get_regulators(sensor); in ov2680_probe()
1224 mutex_init(&sensor->lock); in ov2680_probe()
1230 ret = ov2680_power_on(sensor); in ov2680_probe()
1234 ret = ov2680_check_id(sensor); in ov2680_probe()
1242 ret = ov2680_v4l2_register(sensor); in ov2680_probe()
1256 ov2680_power_off(sensor); in ov2680_probe()
1259 mutex_destroy(&sensor->lock); in ov2680_probe()
1267 struct ov2680_dev *sensor = to_ov2680_dev(sd); in ov2680_remove() local
1269 v4l2_async_unregister_subdev(&sensor->sd); in ov2680_remove()
1270 mutex_destroy(&sensor->lock); in ov2680_remove()
1271 media_entity_cleanup(&sensor->sd.entity); in ov2680_remove()
1272 v4l2_ctrl_handler_free(&sensor->ctrls.handler); in ov2680_remove()
1280 ov2680_power_off(sensor); in ov2680_remove()
1287 struct ov2680_dev *sensor = to_ov2680_dev(sd); in ov2680_suspend() local
1289 if (sensor->is_streaming) in ov2680_suspend()
1290 ov2680_stream_disable(sensor); in ov2680_suspend()
1292 return ov2680_power_off(sensor); in ov2680_suspend()
1298 struct ov2680_dev *sensor = to_ov2680_dev(sd); in ov2680_resume() local
1301 ret = ov2680_power_on(sensor); in ov2680_resume()
1305 if (sensor->is_streaming) { in ov2680_resume()
1306 ret = ov2680_stream_enable(sensor); in ov2680_resume()
1314 ov2680_stream_disable(sensor); in ov2680_resume()
1315 sensor->is_streaming = false; in ov2680_resume()
1348 MODULE_DESCRIPTION("OV2680 CMOS Image Sensor driver");