1 // SPDX-License-Identifier: GPL-2.0
5 * Copyright (C) 2017 Fuzhou Rockchip Electronics Co., Ltd.
9 #include <linux/device.h>
10 #include <linux/delay.h>
11 #include <linux/gpio/consumer.h>
12 #include <linux/i2c.h>
13 #include <linux/module.h>
14 #include <linux/pm_runtime.h>
15 #include <linux/regulator/consumer.h>
16 #include <linux/sysfs.h>
17 #include <media/media-entity.h>
18 #include <media/v4l2-async.h>
19 #include <media/v4l2-ctrls.h>
20 #include <media/v4l2-fwnode.h>
21 #include <media/v4l2-subdev.h>
23 #define CHIP_ID 0x2685
24 #define OV2685_REG_CHIP_ID 0x300a
26 #define OV2685_XVCLK_FREQ 24000000
28 #define REG_SC_CTRL_MODE 0x0100
29 #define SC_CTRL_MODE_STANDBY 0x0
30 #define SC_CTRL_MODE_STREAMING BIT(0)
32 #define OV2685_REG_EXPOSURE 0x3500
33 #define OV2685_EXPOSURE_MIN 4
34 #define OV2685_EXPOSURE_STEP 1
36 #define OV2685_REG_VTS 0x380e
37 #define OV2685_VTS_MAX 0x7fff
39 #define OV2685_REG_GAIN 0x350a
40 #define OV2685_GAIN_MIN 0
41 #define OV2685_GAIN_MAX 0x07ff
42 #define OV2685_GAIN_STEP 0x1
43 #define OV2685_GAIN_DEFAULT 0x0036
45 #define OV2685_REG_TEST_PATTERN 0x5080
46 #define OV2685_TEST_PATTERN_DISABLED 0x00
47 #define OV2685_TEST_PATTERN_COLOR_BAR 0x80
48 #define OV2685_TEST_PATTERN_RANDOM 0x81
49 #define OV2685_TEST_PATTERN_COLOR_BAR_FADE 0x88
50 #define OV2685_TEST_PATTERN_BW_SQUARE 0x92
51 #define OV2685_TEST_PATTERN_COLOR_SQUARE 0x82
53 #define REG_NULL 0xFFFF
55 #define OV2685_REG_VALUE_08BIT 1
56 #define OV2685_REG_VALUE_16BIT 2
57 #define OV2685_REG_VALUE_24BIT 3
59 #define OV2685_NATIVE_WIDTH 1616
60 #define OV2685_NATIVE_HEIGHT 1216
62 #define OV2685_LANES 1
63 #define OV2685_BITS_PER_SAMPLE 10
65 static const char * const ov2685_supply_names[] = {
66 "avdd", /* Analog power */
67 "dovdd", /* Digital I/O power */
68 "dvdd", /* Digital core power */
71 #define OV2685_NUM_SUPPLIES ARRAY_SIZE(ov2685_supply_names)
84 const struct v4l2_rect *analog_crop;
85 const struct regval *reg_list;
89 struct i2c_client *client;
91 struct gpio_desc *reset_gpio;
92 struct regulator_bulk_data supplies[OV2685_NUM_SUPPLIES];
95 struct v4l2_subdev subdev;
97 struct v4l2_ctrl *anal_gain;
98 struct v4l2_ctrl *exposure;
99 struct v4l2_ctrl *hblank;
100 struct v4l2_ctrl *vblank;
101 struct v4l2_ctrl *test_pattern;
102 struct v4l2_ctrl_handler ctrl_handler;
104 const struct ov2685_mode *cur_mode;
107 #define to_ov2685(sd) container_of(sd, struct ov2685, subdev)
109 /* PLL settings bases on 24M xvclk */
110 static struct regval ov2685_1600x1200_regs[] = {
214 #define OV2685_LINK_FREQ_330MHZ 330000000
215 static const s64 link_freq_menu_items[] = {
216 OV2685_LINK_FREQ_330MHZ
219 static const char * const ov2685_test_pattern_menu[] = {
224 "Black White Square",
228 static const int ov2685_test_pattern_val[] = {
229 OV2685_TEST_PATTERN_DISABLED,
230 OV2685_TEST_PATTERN_COLOR_BAR,
231 OV2685_TEST_PATTERN_COLOR_BAR_FADE,
232 OV2685_TEST_PATTERN_RANDOM,
233 OV2685_TEST_PATTERN_BW_SQUARE,
234 OV2685_TEST_PATTERN_COLOR_SQUARE,
237 static const struct v4l2_rect ov2685_analog_crop = {
244 static const struct ov2685_mode supported_modes[] = {
251 .analog_crop = &ov2685_analog_crop,
252 .reg_list = ov2685_1600x1200_regs,
256 /* Write registers up to 4 at a time */
257 static int ov2685_write_reg(struct i2c_client *client, u16 reg,
271 val_be = cpu_to_be32(val);
272 val_p = (u8 *)&val_be;
277 buf[buf_i++] = val_p[val_i++];
279 if (i2c_master_send(client, buf, len + 2) != len + 2)
285 static int ov2685_write_array(struct i2c_client *client,
286 const struct regval *regs)
291 for (i = 0; ret == 0 && regs[i].addr != REG_NULL; i++)
292 ret = ov2685_write_reg(client, regs[i].addr,
293 OV2685_REG_VALUE_08BIT, regs[i].val);
298 /* Read registers up to 4 at a time */
299 static int ov2685_read_reg(struct i2c_client *client, u16 reg,
302 struct i2c_msg msgs[2];
305 __be16 reg_addr_be = cpu_to_be16(reg);
311 data_be_p = (u8 *)&data_be;
312 /* Write register address */
313 msgs[0].addr = client->addr;
316 msgs[0].buf = (u8 *)®_addr_be;
318 /* Read data from register */
319 msgs[1].addr = client->addr;
320 msgs[1].flags = I2C_M_RD;
322 msgs[1].buf = &data_be_p[4 - len];
324 ret = i2c_transfer(client->adapter, msgs, ARRAY_SIZE(msgs));
325 if (ret != ARRAY_SIZE(msgs))
328 *val = be32_to_cpu(data_be);
333 static void ov2685_fill_fmt(const struct ov2685_mode *mode,
334 struct v4l2_mbus_framefmt *fmt)
336 fmt->code = MEDIA_BUS_FMT_SBGGR10_1X10;
337 fmt->width = mode->width;
338 fmt->height = mode->height;
339 fmt->field = V4L2_FIELD_NONE;
342 static int ov2685_set_fmt(struct v4l2_subdev *sd,
343 struct v4l2_subdev_state *sd_state,
344 struct v4l2_subdev_format *fmt)
346 struct ov2685 *ov2685 = to_ov2685(sd);
347 struct v4l2_mbus_framefmt *mbus_fmt = &fmt->format;
349 /* only one mode supported for now */
350 ov2685_fill_fmt(ov2685->cur_mode, mbus_fmt);
355 static int ov2685_get_fmt(struct v4l2_subdev *sd,
356 struct v4l2_subdev_state *sd_state,
357 struct v4l2_subdev_format *fmt)
359 struct ov2685 *ov2685 = to_ov2685(sd);
360 struct v4l2_mbus_framefmt *mbus_fmt = &fmt->format;
362 ov2685_fill_fmt(ov2685->cur_mode, mbus_fmt);
367 static int ov2685_enum_mbus_code(struct v4l2_subdev *sd,
368 struct v4l2_subdev_state *sd_state,
369 struct v4l2_subdev_mbus_code_enum *code)
371 if (code->index >= ARRAY_SIZE(supported_modes))
374 code->code = MEDIA_BUS_FMT_SBGGR10_1X10;
379 static int ov2685_enum_frame_sizes(struct v4l2_subdev *sd,
380 struct v4l2_subdev_state *sd_state,
381 struct v4l2_subdev_frame_size_enum *fse)
383 int index = fse->index;
385 if (index >= ARRAY_SIZE(supported_modes))
388 fse->code = MEDIA_BUS_FMT_SBGGR10_1X10;
390 fse->min_width = supported_modes[index].width;
391 fse->max_width = supported_modes[index].width;
392 fse->max_height = supported_modes[index].height;
393 fse->min_height = supported_modes[index].height;
398 static const struct v4l2_rect *
399 __ov2685_get_pad_crop(struct ov2685 *ov2685,
400 struct v4l2_subdev_state *state, unsigned int pad,
401 enum v4l2_subdev_format_whence which)
403 const struct ov2685_mode *mode = ov2685->cur_mode;
406 case V4L2_SUBDEV_FORMAT_TRY:
407 return v4l2_subdev_state_get_crop(state, pad);
408 case V4L2_SUBDEV_FORMAT_ACTIVE:
409 return mode->analog_crop;
415 static int ov2685_get_selection(struct v4l2_subdev *sd,
416 struct v4l2_subdev_state *sd_state,
417 struct v4l2_subdev_selection *sel)
419 struct ov2685 *ov2685 = to_ov2685(sd);
421 switch (sel->target) {
422 case V4L2_SEL_TGT_CROP:
423 mutex_lock(&ov2685->mutex);
424 sel->r = *__ov2685_get_pad_crop(ov2685, sd_state, sel->pad,
426 mutex_unlock(&ov2685->mutex);
428 case V4L2_SEL_TGT_NATIVE_SIZE:
429 case V4L2_SEL_TGT_CROP_BOUNDS:
432 sel->r.width = OV2685_NATIVE_WIDTH;
433 sel->r.height = OV2685_NATIVE_HEIGHT;
435 case V4L2_SEL_TGT_CROP_DEFAULT:
436 sel->r = ov2685_analog_crop;
445 /* Calculate the delay in us by clock rate and clock cycles */
446 static inline u32 ov2685_cal_delay(u32 cycles)
448 return DIV_ROUND_UP(cycles, OV2685_XVCLK_FREQ / 1000 / 1000);
451 static int __ov2685_power_on(struct ov2685 *ov2685)
455 struct device *dev = &ov2685->client->dev;
457 ret = clk_prepare_enable(ov2685->xvclk);
459 dev_err(dev, "Failed to enable xvclk\n");
463 gpiod_set_value_cansleep(ov2685->reset_gpio, 1);
465 ret = regulator_bulk_enable(OV2685_NUM_SUPPLIES, ov2685->supplies);
467 dev_err(dev, "Failed to enable regulators\n");
471 /* The minimum delay between power supplies and reset rising can be 0 */
472 gpiod_set_value_cansleep(ov2685->reset_gpio, 0);
473 /* 8192 xvclk cycles prior to the first SCCB transaction */
474 delay_us = ov2685_cal_delay(8192);
475 usleep_range(delay_us, delay_us * 2);
477 /* HACK: ov2685 would output messy data after reset(R0103),
478 * writing register before .s_stream() as a workaround
480 ret = ov2685_write_array(ov2685->client, ov2685->cur_mode->reg_list);
482 dev_err(dev, "Failed to set regs for power on\n");
483 goto disable_supplies;
489 regulator_bulk_disable(OV2685_NUM_SUPPLIES, ov2685->supplies);
491 clk_disable_unprepare(ov2685->xvclk);
496 static void __ov2685_power_off(struct ov2685 *ov2685)
498 /* 512 xvclk cycles after the last SCCB transaction or MIPI frame end */
499 u32 delay_us = ov2685_cal_delay(512);
501 usleep_range(delay_us, delay_us * 2);
502 clk_disable_unprepare(ov2685->xvclk);
503 gpiod_set_value_cansleep(ov2685->reset_gpio, 1);
504 regulator_bulk_disable(OV2685_NUM_SUPPLIES, ov2685->supplies);
507 static int ov2685_s_stream(struct v4l2_subdev *sd, int on)
509 struct ov2685 *ov2685 = to_ov2685(sd);
510 struct i2c_client *client = ov2685->client;
513 mutex_lock(&ov2685->mutex);
516 ret = pm_runtime_resume_and_get(&ov2685->client->dev);
518 goto unlock_and_return;
520 ret = __v4l2_ctrl_handler_setup(&ov2685->ctrl_handler);
522 pm_runtime_put(&client->dev);
523 goto unlock_and_return;
525 ret = ov2685_write_reg(client, REG_SC_CTRL_MODE,
526 OV2685_REG_VALUE_08BIT, SC_CTRL_MODE_STREAMING);
528 pm_runtime_put(&client->dev);
529 goto unlock_and_return;
532 ov2685_write_reg(client, REG_SC_CTRL_MODE,
533 OV2685_REG_VALUE_08BIT, SC_CTRL_MODE_STANDBY);
534 pm_runtime_put(&ov2685->client->dev);
538 mutex_unlock(&ov2685->mutex);
543 static int ov2685_open(struct v4l2_subdev *sd, struct v4l2_subdev_fh *fh)
545 struct ov2685 *ov2685 = to_ov2685(sd);
546 struct v4l2_mbus_framefmt *try_fmt;
548 mutex_lock(&ov2685->mutex);
550 try_fmt = v4l2_subdev_state_get_format(fh->state, 0);
551 /* Initialize try_fmt */
552 ov2685_fill_fmt(&supported_modes[0], try_fmt);
554 mutex_unlock(&ov2685->mutex);
559 static int __maybe_unused ov2685_runtime_resume(struct device *dev)
561 struct v4l2_subdev *sd = dev_get_drvdata(dev);
562 struct ov2685 *ov2685 = to_ov2685(sd);
564 return __ov2685_power_on(ov2685);
567 static int __maybe_unused ov2685_runtime_suspend(struct device *dev)
569 struct v4l2_subdev *sd = dev_get_drvdata(dev);
570 struct ov2685 *ov2685 = to_ov2685(sd);
572 __ov2685_power_off(ov2685);
577 static const struct dev_pm_ops ov2685_pm_ops = {
578 SET_RUNTIME_PM_OPS(ov2685_runtime_suspend,
579 ov2685_runtime_resume, NULL)
582 static int ov2685_set_ctrl(struct v4l2_ctrl *ctrl)
584 struct ov2685 *ov2685 = container_of(ctrl->handler,
585 struct ov2685, ctrl_handler);
586 struct i2c_client *client = ov2685->client;
590 /* Propagate change of current control to all related controls */
592 case V4L2_CID_VBLANK:
593 /* Update max exposure while meeting expected vblanking */
594 max_expo = ov2685->cur_mode->height + ctrl->val - 4;
595 __v4l2_ctrl_modify_range(ov2685->exposure,
596 ov2685->exposure->minimum, max_expo,
597 ov2685->exposure->step,
598 ov2685->exposure->default_value);
602 if (!pm_runtime_get_if_in_use(&client->dev))
606 case V4L2_CID_EXPOSURE:
607 ret = ov2685_write_reg(ov2685->client, OV2685_REG_EXPOSURE,
608 OV2685_REG_VALUE_24BIT, ctrl->val << 4);
610 case V4L2_CID_ANALOGUE_GAIN:
611 ret = ov2685_write_reg(ov2685->client, OV2685_REG_GAIN,
612 OV2685_REG_VALUE_16BIT, ctrl->val);
614 case V4L2_CID_VBLANK:
615 ret = ov2685_write_reg(ov2685->client, OV2685_REG_VTS,
616 OV2685_REG_VALUE_16BIT,
617 ctrl->val + ov2685->cur_mode->height);
619 case V4L2_CID_TEST_PATTERN:
620 ret = ov2685_write_reg(ov2685->client, OV2685_REG_TEST_PATTERN,
621 OV2685_REG_VALUE_08BIT,
622 ov2685_test_pattern_val[ctrl->val]);
625 dev_warn(&client->dev, "%s Unhandled id:0x%x, val:0x%x\n",
626 __func__, ctrl->id, ctrl->val);
631 pm_runtime_put(&client->dev);
636 static const struct v4l2_subdev_video_ops ov2685_video_ops = {
637 .s_stream = ov2685_s_stream,
640 static const struct v4l2_subdev_pad_ops ov2685_pad_ops = {
641 .enum_mbus_code = ov2685_enum_mbus_code,
642 .enum_frame_size = ov2685_enum_frame_sizes,
643 .get_fmt = ov2685_get_fmt,
644 .set_fmt = ov2685_set_fmt,
645 .get_selection = ov2685_get_selection,
646 .set_selection = ov2685_get_selection,
649 static const struct v4l2_subdev_ops ov2685_subdev_ops = {
650 .video = &ov2685_video_ops,
651 .pad = &ov2685_pad_ops,
654 static const struct v4l2_subdev_internal_ops ov2685_internal_ops = {
658 static const struct v4l2_ctrl_ops ov2685_ctrl_ops = {
659 .s_ctrl = ov2685_set_ctrl,
662 static int ov2685_initialize_controls(struct ov2685 *ov2685)
664 const struct ov2685_mode *mode;
665 struct v4l2_ctrl_handler *handler;
666 struct v4l2_ctrl *ctrl;
667 struct v4l2_fwnode_device_properties props;
669 u32 pixel_rate, h_blank;
672 handler = &ov2685->ctrl_handler;
673 mode = ov2685->cur_mode;
674 ret = v4l2_ctrl_handler_init(handler, 10);
677 handler->lock = &ov2685->mutex;
679 ctrl = v4l2_ctrl_new_int_menu(handler, NULL, V4L2_CID_LINK_FREQ,
680 0, 0, link_freq_menu_items);
682 ctrl->flags |= V4L2_CTRL_FLAG_READ_ONLY;
684 pixel_rate = (link_freq_menu_items[0] * 2 * OV2685_LANES) /
685 OV2685_BITS_PER_SAMPLE;
686 v4l2_ctrl_new_std(handler, NULL, V4L2_CID_PIXEL_RATE,
687 0, pixel_rate, 1, pixel_rate);
689 h_blank = mode->hts_def - mode->width;
690 ov2685->hblank = v4l2_ctrl_new_std(handler, NULL, V4L2_CID_HBLANK,
691 h_blank, h_blank, 1, h_blank);
693 ov2685->hblank->flags |= V4L2_CTRL_FLAG_READ_ONLY;
695 ov2685->vblank = v4l2_ctrl_new_std(handler, &ov2685_ctrl_ops,
696 V4L2_CID_VBLANK, mode->vts_def - mode->height,
697 OV2685_VTS_MAX - mode->height, 1,
698 mode->vts_def - mode->height);
700 exposure_max = mode->vts_def - 4;
701 ov2685->exposure = v4l2_ctrl_new_std(handler, &ov2685_ctrl_ops,
702 V4L2_CID_EXPOSURE, OV2685_EXPOSURE_MIN,
703 exposure_max, OV2685_EXPOSURE_STEP,
706 ov2685->anal_gain = v4l2_ctrl_new_std(handler, &ov2685_ctrl_ops,
707 V4L2_CID_ANALOGUE_GAIN, OV2685_GAIN_MIN,
708 OV2685_GAIN_MAX, OV2685_GAIN_STEP,
709 OV2685_GAIN_DEFAULT);
711 ov2685->test_pattern = v4l2_ctrl_new_std_menu_items(handler,
712 &ov2685_ctrl_ops, V4L2_CID_TEST_PATTERN,
713 ARRAY_SIZE(ov2685_test_pattern_menu) - 1,
714 0, 0, ov2685_test_pattern_menu);
716 /* set properties from fwnode (e.g. rotation, orientation) */
717 ret = v4l2_fwnode_device_parse(&ov2685->client->dev, &props);
719 goto err_free_handler;
721 ret = v4l2_ctrl_new_fwnode_properties(handler, &ov2685_ctrl_ops, &props);
723 goto err_free_handler;
725 if (handler->error) {
726 ret = handler->error;
727 dev_err(&ov2685->client->dev,
728 "Failed to init controls(%d)\n", ret);
729 goto err_free_handler;
732 ov2685->subdev.ctrl_handler = handler;
737 v4l2_ctrl_handler_free(handler);
742 static int ov2685_check_sensor_id(struct ov2685 *ov2685,
743 struct i2c_client *client)
745 struct device *dev = &ov2685->client->dev;
749 ret = ov2685_read_reg(client, OV2685_REG_CHIP_ID,
750 OV2685_REG_VALUE_16BIT, &id);
752 dev_err(dev, "Unexpected sensor id(%04x), ret(%d)\n", id, ret);
756 dev_info(dev, "Detected OV%04x sensor\n", CHIP_ID);
761 static int ov2685_configure_regulators(struct ov2685 *ov2685)
765 for (i = 0; i < OV2685_NUM_SUPPLIES; i++)
766 ov2685->supplies[i].supply = ov2685_supply_names[i];
768 return devm_regulator_bulk_get(&ov2685->client->dev,
773 static int ov2685_probe(struct i2c_client *client)
775 struct device *dev = &client->dev;
776 struct ov2685 *ov2685;
779 ov2685 = devm_kzalloc(dev, sizeof(*ov2685), GFP_KERNEL);
783 ov2685->client = client;
784 ov2685->cur_mode = &supported_modes[0];
786 ov2685->xvclk = devm_clk_get(dev, "xvclk");
787 if (IS_ERR(ov2685->xvclk)) {
788 dev_err(dev, "Failed to get xvclk\n");
791 ret = clk_set_rate(ov2685->xvclk, OV2685_XVCLK_FREQ);
793 dev_err(dev, "Failed to set xvclk rate (24MHz)\n");
796 if (clk_get_rate(ov2685->xvclk) != OV2685_XVCLK_FREQ)
797 dev_warn(dev, "xvclk mismatched, modes are based on 24MHz\n");
799 ov2685->reset_gpio = devm_gpiod_get_optional(dev, "reset", GPIOD_OUT_LOW);
800 if (IS_ERR(ov2685->reset_gpio)) {
801 dev_err(dev, "Failed to get reset-gpios\n");
805 ret = ov2685_configure_regulators(ov2685);
807 dev_err(dev, "Failed to get power regulators\n");
811 mutex_init(&ov2685->mutex);
812 v4l2_i2c_subdev_init(&ov2685->subdev, client, &ov2685_subdev_ops);
813 ret = ov2685_initialize_controls(ov2685);
815 goto err_destroy_mutex;
817 ret = __ov2685_power_on(ov2685);
819 goto err_free_handler;
821 ret = ov2685_check_sensor_id(ov2685, client);
825 ov2685->subdev.internal_ops = &ov2685_internal_ops;
826 ov2685->subdev.flags |= V4L2_SUBDEV_FL_HAS_DEVNODE;
827 ov2685->pad.flags = MEDIA_PAD_FL_SOURCE;
828 ov2685->subdev.entity.function = MEDIA_ENT_F_CAM_SENSOR;
829 ret = media_entity_pads_init(&ov2685->subdev.entity, 1, &ov2685->pad);
833 ret = v4l2_async_register_subdev(&ov2685->subdev);
835 dev_err(dev, "v4l2 async register subdev failed\n");
836 goto err_clean_entity;
839 pm_runtime_set_active(dev);
840 pm_runtime_enable(dev);
841 pm_runtime_idle(dev);
846 media_entity_cleanup(&ov2685->subdev.entity);
848 __ov2685_power_off(ov2685);
850 v4l2_ctrl_handler_free(&ov2685->ctrl_handler);
852 mutex_destroy(&ov2685->mutex);
857 static void ov2685_remove(struct i2c_client *client)
859 struct v4l2_subdev *sd = i2c_get_clientdata(client);
860 struct ov2685 *ov2685 = to_ov2685(sd);
862 v4l2_async_unregister_subdev(sd);
863 media_entity_cleanup(&sd->entity);
864 v4l2_ctrl_handler_free(&ov2685->ctrl_handler);
865 mutex_destroy(&ov2685->mutex);
867 pm_runtime_disable(&client->dev);
868 if (!pm_runtime_status_suspended(&client->dev))
869 __ov2685_power_off(ov2685);
870 pm_runtime_set_suspended(&client->dev);
873 #if IS_ENABLED(CONFIG_OF)
874 static const struct of_device_id ov2685_of_match[] = {
875 { .compatible = "ovti,ov2685" },
878 MODULE_DEVICE_TABLE(of, ov2685_of_match);
881 static struct i2c_driver ov2685_i2c_driver = {
884 .pm = &ov2685_pm_ops,
885 .of_match_table = of_match_ptr(ov2685_of_match),
887 .probe = ov2685_probe,
888 .remove = ov2685_remove,
891 module_i2c_driver(ov2685_i2c_driver);
893 MODULE_DESCRIPTION("OmniVision ov2685 sensor driver");
894 MODULE_LICENSE("GPL v2");