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-subdev.h>
22 #define CHIP_ID 0x2685
23 #define OV2685_REG_CHIP_ID 0x300a
25 #define OV2685_XVCLK_FREQ 24000000
27 #define REG_SC_CTRL_MODE 0x0100
28 #define SC_CTRL_MODE_STANDBY 0x0
29 #define SC_CTRL_MODE_STREAMING BIT(0)
31 #define OV2685_REG_EXPOSURE 0x3500
32 #define OV2685_EXPOSURE_MIN 4
33 #define OV2685_EXPOSURE_STEP 1
35 #define OV2685_REG_VTS 0x380e
36 #define OV2685_VTS_MAX 0x7fff
38 #define OV2685_REG_GAIN 0x350a
39 #define OV2685_GAIN_MIN 0
40 #define OV2685_GAIN_MAX 0x07ff
41 #define OV2685_GAIN_STEP 0x1
42 #define OV2685_GAIN_DEFAULT 0x0036
44 #define OV2685_REG_TEST_PATTERN 0x5080
45 #define OV2685_TEST_PATTERN_DISABLED 0x00
46 #define OV2685_TEST_PATTERN_COLOR_BAR 0x80
47 #define OV2685_TEST_PATTERN_RANDOM 0x81
48 #define OV2685_TEST_PATTERN_COLOR_BAR_FADE 0x88
49 #define OV2685_TEST_PATTERN_BW_SQUARE 0x92
50 #define OV2685_TEST_PATTERN_COLOR_SQUARE 0x82
52 #define REG_NULL 0xFFFF
54 #define OV2685_REG_VALUE_08BIT 1
55 #define OV2685_REG_VALUE_16BIT 2
56 #define OV2685_REG_VALUE_24BIT 3
58 #define OV2685_LANES 1
59 #define OV2685_BITS_PER_SAMPLE 10
61 static const char * const ov2685_supply_names[] = {
62 "avdd", /* Analog power */
63 "dovdd", /* Digital I/O power */
64 "dvdd", /* Digital core power */
67 #define OV2685_NUM_SUPPLIES ARRAY_SIZE(ov2685_supply_names)
80 const struct regval *reg_list;
84 struct i2c_client *client;
86 struct gpio_desc *reset_gpio;
87 struct regulator_bulk_data supplies[OV2685_NUM_SUPPLIES];
91 struct v4l2_subdev subdev;
93 struct v4l2_ctrl *anal_gain;
94 struct v4l2_ctrl *exposure;
95 struct v4l2_ctrl *hblank;
96 struct v4l2_ctrl *vblank;
97 struct v4l2_ctrl *test_pattern;
98 struct v4l2_ctrl_handler ctrl_handler;
100 const struct ov2685_mode *cur_mode;
103 #define to_ov2685(sd) container_of(sd, struct ov2685, subdev)
105 /* PLL settings bases on 24M xvclk */
106 static struct regval ov2685_1600x1200_regs[] = {
210 #define OV2685_LINK_FREQ_330MHZ 330000000
211 static const s64 link_freq_menu_items[] = {
212 OV2685_LINK_FREQ_330MHZ
215 static const char * const ov2685_test_pattern_menu[] = {
220 "Black White Square",
224 static const int ov2685_test_pattern_val[] = {
225 OV2685_TEST_PATTERN_DISABLED,
226 OV2685_TEST_PATTERN_COLOR_BAR,
227 OV2685_TEST_PATTERN_COLOR_BAR_FADE,
228 OV2685_TEST_PATTERN_RANDOM,
229 OV2685_TEST_PATTERN_BW_SQUARE,
230 OV2685_TEST_PATTERN_COLOR_SQUARE,
233 static const struct ov2685_mode supported_modes[] = {
240 .reg_list = ov2685_1600x1200_regs,
244 /* Write registers up to 4 at a time */
245 static int ov2685_write_reg(struct i2c_client *client, u16 reg,
259 val_be = cpu_to_be32(val);
260 val_p = (u8 *)&val_be;
265 buf[buf_i++] = val_p[val_i++];
267 if (i2c_master_send(client, buf, len + 2) != len + 2)
273 static int ov2685_write_array(struct i2c_client *client,
274 const struct regval *regs)
279 for (i = 0; ret == 0 && regs[i].addr != REG_NULL; i++)
280 ret = ov2685_write_reg(client, regs[i].addr,
281 OV2685_REG_VALUE_08BIT, regs[i].val);
286 /* Read registers up to 4 at a time */
287 static int ov2685_read_reg(struct i2c_client *client, u16 reg,
290 struct i2c_msg msgs[2];
293 __be16 reg_addr_be = cpu_to_be16(reg);
299 data_be_p = (u8 *)&data_be;
300 /* Write register address */
301 msgs[0].addr = client->addr;
304 msgs[0].buf = (u8 *)®_addr_be;
306 /* Read data from register */
307 msgs[1].addr = client->addr;
308 msgs[1].flags = I2C_M_RD;
310 msgs[1].buf = &data_be_p[4 - len];
312 ret = i2c_transfer(client->adapter, msgs, ARRAY_SIZE(msgs));
313 if (ret != ARRAY_SIZE(msgs))
316 *val = be32_to_cpu(data_be);
321 static void ov2685_fill_fmt(const struct ov2685_mode *mode,
322 struct v4l2_mbus_framefmt *fmt)
324 fmt->code = MEDIA_BUS_FMT_SBGGR10_1X10;
325 fmt->width = mode->width;
326 fmt->height = mode->height;
327 fmt->field = V4L2_FIELD_NONE;
330 static int ov2685_set_fmt(struct v4l2_subdev *sd,
331 struct v4l2_subdev_pad_config *cfg,
332 struct v4l2_subdev_format *fmt)
334 struct ov2685 *ov2685 = to_ov2685(sd);
335 struct v4l2_mbus_framefmt *mbus_fmt = &fmt->format;
337 /* only one mode supported for now */
338 ov2685_fill_fmt(ov2685->cur_mode, mbus_fmt);
343 static int ov2685_get_fmt(struct v4l2_subdev *sd,
344 struct v4l2_subdev_pad_config *cfg,
345 struct v4l2_subdev_format *fmt)
347 struct ov2685 *ov2685 = to_ov2685(sd);
348 struct v4l2_mbus_framefmt *mbus_fmt = &fmt->format;
350 ov2685_fill_fmt(ov2685->cur_mode, mbus_fmt);
355 static int ov2685_enum_mbus_code(struct v4l2_subdev *sd,
356 struct v4l2_subdev_pad_config *cfg,
357 struct v4l2_subdev_mbus_code_enum *code)
359 if (code->index >= ARRAY_SIZE(supported_modes))
362 code->code = MEDIA_BUS_FMT_SBGGR10_1X10;
367 static int ov2685_enum_frame_sizes(struct v4l2_subdev *sd,
368 struct v4l2_subdev_pad_config *cfg,
369 struct v4l2_subdev_frame_size_enum *fse)
371 int index = fse->index;
373 if (index >= ARRAY_SIZE(supported_modes))
376 fse->code = MEDIA_BUS_FMT_SBGGR10_1X10;
378 fse->min_width = supported_modes[index].width;
379 fse->max_width = supported_modes[index].width;
380 fse->max_height = supported_modes[index].height;
381 fse->min_height = supported_modes[index].height;
386 /* Calculate the delay in us by clock rate and clock cycles */
387 static inline u32 ov2685_cal_delay(u32 cycles)
389 return DIV_ROUND_UP(cycles, OV2685_XVCLK_FREQ / 1000 / 1000);
392 static int __ov2685_power_on(struct ov2685 *ov2685)
396 struct device *dev = &ov2685->client->dev;
398 ret = clk_prepare_enable(ov2685->xvclk);
400 dev_err(dev, "Failed to enable xvclk\n");
404 gpiod_set_value_cansleep(ov2685->reset_gpio, 1);
406 ret = regulator_bulk_enable(OV2685_NUM_SUPPLIES, ov2685->supplies);
408 dev_err(dev, "Failed to enable regulators\n");
412 /* The minimum delay between power supplies and reset rising can be 0 */
413 gpiod_set_value_cansleep(ov2685->reset_gpio, 0);
414 /* 8192 xvclk cycles prior to the first SCCB transaction */
415 delay_us = ov2685_cal_delay(8192);
416 usleep_range(delay_us, delay_us * 2);
418 /* HACK: ov2685 would output messy data after reset(R0103),
419 * writing register before .s_stream() as a workaround
421 ret = ov2685_write_array(ov2685->client, ov2685->cur_mode->reg_list);
423 goto disable_supplies;
428 regulator_bulk_disable(OV2685_NUM_SUPPLIES, ov2685->supplies);
430 clk_disable_unprepare(ov2685->xvclk);
435 static void __ov2685_power_off(struct ov2685 *ov2685)
437 /* 512 xvclk cycles after the last SCCB transaction or MIPI frame end */
438 u32 delay_us = ov2685_cal_delay(512);
440 usleep_range(delay_us, delay_us * 2);
441 clk_disable_unprepare(ov2685->xvclk);
442 gpiod_set_value_cansleep(ov2685->reset_gpio, 1);
443 regulator_bulk_disable(OV2685_NUM_SUPPLIES, ov2685->supplies);
446 static int ov2685_s_stream(struct v4l2_subdev *sd, int on)
448 struct ov2685 *ov2685 = to_ov2685(sd);
449 struct i2c_client *client = ov2685->client;
452 mutex_lock(&ov2685->mutex);
455 if (on == ov2685->streaming)
456 goto unlock_and_return;
459 ret = pm_runtime_get_sync(&ov2685->client->dev);
461 pm_runtime_put_noidle(&client->dev);
462 goto unlock_and_return;
464 ret = __v4l2_ctrl_handler_setup(&ov2685->ctrl_handler);
466 pm_runtime_put(&client->dev);
467 goto unlock_and_return;
469 ret = ov2685_write_reg(client, REG_SC_CTRL_MODE,
470 OV2685_REG_VALUE_08BIT, SC_CTRL_MODE_STREAMING);
472 pm_runtime_put(&client->dev);
473 goto unlock_and_return;
476 ov2685_write_reg(client, REG_SC_CTRL_MODE,
477 OV2685_REG_VALUE_08BIT, SC_CTRL_MODE_STANDBY);
478 pm_runtime_put(&ov2685->client->dev);
481 ov2685->streaming = on;
484 mutex_unlock(&ov2685->mutex);
489 #ifdef CONFIG_VIDEO_V4L2_SUBDEV_API
490 static int ov2685_open(struct v4l2_subdev *sd, struct v4l2_subdev_fh *fh)
492 struct ov2685 *ov2685 = to_ov2685(sd);
493 struct v4l2_mbus_framefmt *try_fmt;
495 mutex_lock(&ov2685->mutex);
497 try_fmt = v4l2_subdev_get_try_format(sd, fh->pad, 0);
498 /* Initialize try_fmt */
499 ov2685_fill_fmt(&supported_modes[0], try_fmt);
501 mutex_unlock(&ov2685->mutex);
507 static int __maybe_unused ov2685_runtime_resume(struct device *dev)
509 struct v4l2_subdev *sd = dev_get_drvdata(dev);
510 struct ov2685 *ov2685 = to_ov2685(sd);
512 return __ov2685_power_on(ov2685);
515 static int __maybe_unused ov2685_runtime_suspend(struct device *dev)
517 struct v4l2_subdev *sd = dev_get_drvdata(dev);
518 struct ov2685 *ov2685 = to_ov2685(sd);
520 __ov2685_power_off(ov2685);
525 static const struct dev_pm_ops ov2685_pm_ops = {
526 SET_RUNTIME_PM_OPS(ov2685_runtime_suspend,
527 ov2685_runtime_resume, NULL)
530 static int ov2685_set_ctrl(struct v4l2_ctrl *ctrl)
532 struct ov2685 *ov2685 = container_of(ctrl->handler,
533 struct ov2685, ctrl_handler);
534 struct i2c_client *client = ov2685->client;
538 /* Propagate change of current control to all related controls */
540 case V4L2_CID_VBLANK:
541 /* Update max exposure while meeting expected vblanking */
542 max_expo = ov2685->cur_mode->height + ctrl->val - 4;
543 __v4l2_ctrl_modify_range(ov2685->exposure,
544 ov2685->exposure->minimum, max_expo,
545 ov2685->exposure->step,
546 ov2685->exposure->default_value);
550 if (!pm_runtime_get_if_in_use(&client->dev))
554 case V4L2_CID_EXPOSURE:
555 ret = ov2685_write_reg(ov2685->client, OV2685_REG_EXPOSURE,
556 OV2685_REG_VALUE_24BIT, ctrl->val << 4);
558 case V4L2_CID_ANALOGUE_GAIN:
559 ret = ov2685_write_reg(ov2685->client, OV2685_REG_GAIN,
560 OV2685_REG_VALUE_16BIT, ctrl->val);
562 case V4L2_CID_VBLANK:
563 ret = ov2685_write_reg(ov2685->client, OV2685_REG_VTS,
564 OV2685_REG_VALUE_16BIT,
565 ctrl->val + ov2685->cur_mode->height);
567 case V4L2_CID_TEST_PATTERN:
568 ret = ov2685_write_reg(ov2685->client, OV2685_REG_TEST_PATTERN,
569 OV2685_REG_VALUE_08BIT,
570 ov2685_test_pattern_val[ctrl->val]);
573 dev_warn(&client->dev, "%s Unhandled id:0x%x, val:0x%x\n",
574 __func__, ctrl->id, ctrl->val);
579 pm_runtime_put(&client->dev);
584 static const struct v4l2_subdev_video_ops ov2685_video_ops = {
585 .s_stream = ov2685_s_stream,
588 static const struct v4l2_subdev_pad_ops ov2685_pad_ops = {
589 .enum_mbus_code = ov2685_enum_mbus_code,
590 .enum_frame_size = ov2685_enum_frame_sizes,
591 .get_fmt = ov2685_get_fmt,
592 .set_fmt = ov2685_set_fmt,
595 static const struct v4l2_subdev_ops ov2685_subdev_ops = {
596 .video = &ov2685_video_ops,
597 .pad = &ov2685_pad_ops,
600 #ifdef CONFIG_VIDEO_V4L2_SUBDEV_API
601 static const struct v4l2_subdev_internal_ops ov2685_internal_ops = {
606 static const struct v4l2_ctrl_ops ov2685_ctrl_ops = {
607 .s_ctrl = ov2685_set_ctrl,
610 static int ov2685_initialize_controls(struct ov2685 *ov2685)
612 const struct ov2685_mode *mode;
613 struct v4l2_ctrl_handler *handler;
614 struct v4l2_ctrl *ctrl;
616 u32 pixel_rate, h_blank;
619 handler = &ov2685->ctrl_handler;
620 mode = ov2685->cur_mode;
621 ret = v4l2_ctrl_handler_init(handler, 8);
624 handler->lock = &ov2685->mutex;
626 ctrl = v4l2_ctrl_new_int_menu(handler, NULL, V4L2_CID_LINK_FREQ,
627 0, 0, link_freq_menu_items);
629 ctrl->flags |= V4L2_CTRL_FLAG_READ_ONLY;
631 pixel_rate = (link_freq_menu_items[0] * 2 * OV2685_LANES) /
632 OV2685_BITS_PER_SAMPLE;
633 v4l2_ctrl_new_std(handler, NULL, V4L2_CID_PIXEL_RATE,
634 0, pixel_rate, 1, pixel_rate);
636 h_blank = mode->hts_def - mode->width;
637 ov2685->hblank = v4l2_ctrl_new_std(handler, NULL, V4L2_CID_HBLANK,
638 h_blank, h_blank, 1, h_blank);
640 ov2685->hblank->flags |= V4L2_CTRL_FLAG_READ_ONLY;
642 ov2685->vblank = v4l2_ctrl_new_std(handler, &ov2685_ctrl_ops,
643 V4L2_CID_VBLANK, mode->vts_def - mode->height,
644 OV2685_VTS_MAX - mode->height, 1,
645 mode->vts_def - mode->height);
647 exposure_max = mode->vts_def - 4;
648 ov2685->exposure = v4l2_ctrl_new_std(handler, &ov2685_ctrl_ops,
649 V4L2_CID_EXPOSURE, OV2685_EXPOSURE_MIN,
650 exposure_max, OV2685_EXPOSURE_STEP,
653 ov2685->anal_gain = v4l2_ctrl_new_std(handler, &ov2685_ctrl_ops,
654 V4L2_CID_ANALOGUE_GAIN, OV2685_GAIN_MIN,
655 OV2685_GAIN_MAX, OV2685_GAIN_STEP,
656 OV2685_GAIN_DEFAULT);
658 ov2685->test_pattern = v4l2_ctrl_new_std_menu_items(handler,
659 &ov2685_ctrl_ops, V4L2_CID_TEST_PATTERN,
660 ARRAY_SIZE(ov2685_test_pattern_menu) - 1,
661 0, 0, ov2685_test_pattern_menu);
663 if (handler->error) {
664 ret = handler->error;
665 dev_err(&ov2685->client->dev,
666 "Failed to init controls(%d)\n", ret);
667 goto err_free_handler;
670 ov2685->subdev.ctrl_handler = handler;
675 v4l2_ctrl_handler_free(handler);
680 static int ov2685_check_sensor_id(struct ov2685 *ov2685,
681 struct i2c_client *client)
683 struct device *dev = &ov2685->client->dev;
687 ret = ov2685_read_reg(client, OV2685_REG_CHIP_ID,
688 OV2685_REG_VALUE_16BIT, &id);
690 dev_err(dev, "Unexpected sensor id(%04x), ret(%d)\n", id, ret);
694 dev_info(dev, "Detected OV%04x sensor\n", CHIP_ID);
699 static int ov2685_configure_regulators(struct ov2685 *ov2685)
703 for (i = 0; i < OV2685_NUM_SUPPLIES; i++)
704 ov2685->supplies[i].supply = ov2685_supply_names[i];
706 return devm_regulator_bulk_get(&ov2685->client->dev,
711 static int ov2685_probe(struct i2c_client *client,
712 const struct i2c_device_id *id)
714 struct device *dev = &client->dev;
715 struct ov2685 *ov2685;
718 ov2685 = devm_kzalloc(dev, sizeof(*ov2685), GFP_KERNEL);
722 ov2685->client = client;
723 ov2685->cur_mode = &supported_modes[0];
725 ov2685->xvclk = devm_clk_get(dev, "xvclk");
726 if (IS_ERR(ov2685->xvclk)) {
727 dev_err(dev, "Failed to get xvclk\n");
730 ret = clk_set_rate(ov2685->xvclk, OV2685_XVCLK_FREQ);
732 dev_err(dev, "Failed to set xvclk rate (24MHz)\n");
735 if (clk_get_rate(ov2685->xvclk) != OV2685_XVCLK_FREQ)
736 dev_warn(dev, "xvclk mismatched, modes are based on 24MHz\n");
738 ov2685->reset_gpio = devm_gpiod_get(dev, "reset", GPIOD_OUT_LOW);
739 if (IS_ERR(ov2685->reset_gpio)) {
740 dev_err(dev, "Failed to get reset-gpios\n");
744 ret = ov2685_configure_regulators(ov2685);
746 dev_err(dev, "Failed to get power regulators\n");
750 mutex_init(&ov2685->mutex);
751 v4l2_i2c_subdev_init(&ov2685->subdev, client, &ov2685_subdev_ops);
752 ret = ov2685_initialize_controls(ov2685);
754 goto err_destroy_mutex;
756 ret = __ov2685_power_on(ov2685);
758 goto err_free_handler;
760 ret = ov2685_check_sensor_id(ov2685, client);
764 #ifdef CONFIG_VIDEO_V4L2_SUBDEV_API
765 ov2685->subdev.internal_ops = &ov2685_internal_ops;
766 ov2685->subdev.flags |= V4L2_SUBDEV_FL_HAS_DEVNODE;
768 #if defined(CONFIG_MEDIA_CONTROLLER)
769 ov2685->pad.flags = MEDIA_PAD_FL_SOURCE;
770 ov2685->subdev.entity.function = MEDIA_ENT_F_CAM_SENSOR;
771 ret = media_entity_pads_init(&ov2685->subdev.entity, 1, &ov2685->pad);
776 ret = v4l2_async_register_subdev(&ov2685->subdev);
778 dev_err(dev, "v4l2 async register subdev failed\n");
779 goto err_clean_entity;
782 pm_runtime_set_active(dev);
783 pm_runtime_enable(dev);
784 pm_runtime_idle(dev);
789 #if defined(CONFIG_MEDIA_CONTROLLER)
790 media_entity_cleanup(&ov2685->subdev.entity);
793 __ov2685_power_off(ov2685);
795 v4l2_ctrl_handler_free(&ov2685->ctrl_handler);
797 mutex_destroy(&ov2685->mutex);
802 static int ov2685_remove(struct i2c_client *client)
804 struct v4l2_subdev *sd = i2c_get_clientdata(client);
805 struct ov2685 *ov2685 = to_ov2685(sd);
807 v4l2_async_unregister_subdev(sd);
808 #if defined(CONFIG_MEDIA_CONTROLLER)
809 media_entity_cleanup(&sd->entity);
811 v4l2_ctrl_handler_free(&ov2685->ctrl_handler);
812 mutex_destroy(&ov2685->mutex);
814 pm_runtime_disable(&client->dev);
815 if (!pm_runtime_status_suspended(&client->dev))
816 __ov2685_power_off(ov2685);
817 pm_runtime_set_suspended(&client->dev);
822 #if IS_ENABLED(CONFIG_OF)
823 static const struct of_device_id ov2685_of_match[] = {
824 { .compatible = "ovti,ov2685" },
827 MODULE_DEVICE_TABLE(of, ov2685_of_match);
830 static struct i2c_driver ov2685_i2c_driver = {
833 .pm = &ov2685_pm_ops,
834 .of_match_table = of_match_ptr(ov2685_of_match),
836 .probe = &ov2685_probe,
837 .remove = &ov2685_remove,
840 module_i2c_driver(ov2685_i2c_driver);
842 MODULE_DESCRIPTION("OmniVision ov2685 sensor driver");
843 MODULE_LICENSE("GPL v2");