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 i2c_client
*client
= to_i2c_client(dev
);
510 struct v4l2_subdev
*sd
= i2c_get_clientdata(client
);
511 struct ov2685
*ov2685
= to_ov2685(sd
);
513 return __ov2685_power_on(ov2685
);
516 static int __maybe_unused
ov2685_runtime_suspend(struct device
*dev
)
518 struct i2c_client
*client
= to_i2c_client(dev
);
519 struct v4l2_subdev
*sd
= i2c_get_clientdata(client
);
520 struct ov2685
*ov2685
= to_ov2685(sd
);
522 __ov2685_power_off(ov2685
);
527 static const struct dev_pm_ops ov2685_pm_ops
= {
528 SET_RUNTIME_PM_OPS(ov2685_runtime_suspend
,
529 ov2685_runtime_resume
, NULL
)
532 static int ov2685_set_ctrl(struct v4l2_ctrl
*ctrl
)
534 struct ov2685
*ov2685
= container_of(ctrl
->handler
,
535 struct ov2685
, ctrl_handler
);
536 struct i2c_client
*client
= ov2685
->client
;
540 /* Propagate change of current control to all related controls */
542 case V4L2_CID_VBLANK
:
543 /* Update max exposure while meeting expected vblanking */
544 max_expo
= ov2685
->cur_mode
->height
+ ctrl
->val
- 4;
545 __v4l2_ctrl_modify_range(ov2685
->exposure
,
546 ov2685
->exposure
->minimum
, max_expo
,
547 ov2685
->exposure
->step
,
548 ov2685
->exposure
->default_value
);
552 if (!pm_runtime_get_if_in_use(&client
->dev
))
556 case V4L2_CID_EXPOSURE
:
557 ret
= ov2685_write_reg(ov2685
->client
, OV2685_REG_EXPOSURE
,
558 OV2685_REG_VALUE_24BIT
, ctrl
->val
<< 4);
560 case V4L2_CID_ANALOGUE_GAIN
:
561 ret
= ov2685_write_reg(ov2685
->client
, OV2685_REG_GAIN
,
562 OV2685_REG_VALUE_16BIT
, ctrl
->val
);
564 case V4L2_CID_VBLANK
:
565 ret
= ov2685_write_reg(ov2685
->client
, OV2685_REG_VTS
,
566 OV2685_REG_VALUE_16BIT
,
567 ctrl
->val
+ ov2685
->cur_mode
->height
);
569 case V4L2_CID_TEST_PATTERN
:
570 ret
= ov2685_write_reg(ov2685
->client
, OV2685_REG_TEST_PATTERN
,
571 OV2685_REG_VALUE_08BIT
,
572 ov2685_test_pattern_val
[ctrl
->val
]);
575 dev_warn(&client
->dev
, "%s Unhandled id:0x%x, val:0x%x\n",
576 __func__
, ctrl
->id
, ctrl
->val
);
581 pm_runtime_put(&client
->dev
);
586 static const struct v4l2_subdev_video_ops ov2685_video_ops
= {
587 .s_stream
= ov2685_s_stream
,
590 static const struct v4l2_subdev_pad_ops ov2685_pad_ops
= {
591 .enum_mbus_code
= ov2685_enum_mbus_code
,
592 .enum_frame_size
= ov2685_enum_frame_sizes
,
593 .get_fmt
= ov2685_get_fmt
,
594 .set_fmt
= ov2685_set_fmt
,
597 static const struct v4l2_subdev_ops ov2685_subdev_ops
= {
598 .video
= &ov2685_video_ops
,
599 .pad
= &ov2685_pad_ops
,
602 #ifdef CONFIG_VIDEO_V4L2_SUBDEV_API
603 static const struct v4l2_subdev_internal_ops ov2685_internal_ops
= {
608 static const struct v4l2_ctrl_ops ov2685_ctrl_ops
= {
609 .s_ctrl
= ov2685_set_ctrl
,
612 static int ov2685_initialize_controls(struct ov2685
*ov2685
)
614 const struct ov2685_mode
*mode
;
615 struct v4l2_ctrl_handler
*handler
;
616 struct v4l2_ctrl
*ctrl
;
618 u32 pixel_rate
, h_blank
;
621 handler
= &ov2685
->ctrl_handler
;
622 mode
= ov2685
->cur_mode
;
623 ret
= v4l2_ctrl_handler_init(handler
, 8);
626 handler
->lock
= &ov2685
->mutex
;
628 ctrl
= v4l2_ctrl_new_int_menu(handler
, NULL
, V4L2_CID_LINK_FREQ
,
629 0, 0, link_freq_menu_items
);
631 ctrl
->flags
|= V4L2_CTRL_FLAG_READ_ONLY
;
633 pixel_rate
= (link_freq_menu_items
[0] * 2 * OV2685_LANES
) /
634 OV2685_BITS_PER_SAMPLE
;
635 v4l2_ctrl_new_std(handler
, NULL
, V4L2_CID_PIXEL_RATE
,
636 0, pixel_rate
, 1, pixel_rate
);
638 h_blank
= mode
->hts_def
- mode
->width
;
639 ov2685
->hblank
= v4l2_ctrl_new_std(handler
, NULL
, V4L2_CID_HBLANK
,
640 h_blank
, h_blank
, 1, h_blank
);
642 ov2685
->hblank
->flags
|= V4L2_CTRL_FLAG_READ_ONLY
;
644 ov2685
->vblank
= v4l2_ctrl_new_std(handler
, &ov2685_ctrl_ops
,
645 V4L2_CID_VBLANK
, mode
->vts_def
- mode
->height
,
646 OV2685_VTS_MAX
- mode
->height
, 1,
647 mode
->vts_def
- mode
->height
);
649 exposure_max
= mode
->vts_def
- 4;
650 ov2685
->exposure
= v4l2_ctrl_new_std(handler
, &ov2685_ctrl_ops
,
651 V4L2_CID_EXPOSURE
, OV2685_EXPOSURE_MIN
,
652 exposure_max
, OV2685_EXPOSURE_STEP
,
655 ov2685
->anal_gain
= v4l2_ctrl_new_std(handler
, &ov2685_ctrl_ops
,
656 V4L2_CID_ANALOGUE_GAIN
, OV2685_GAIN_MIN
,
657 OV2685_GAIN_MAX
, OV2685_GAIN_STEP
,
658 OV2685_GAIN_DEFAULT
);
660 ov2685
->test_pattern
= v4l2_ctrl_new_std_menu_items(handler
,
661 &ov2685_ctrl_ops
, V4L2_CID_TEST_PATTERN
,
662 ARRAY_SIZE(ov2685_test_pattern_menu
) - 1,
663 0, 0, ov2685_test_pattern_menu
);
665 if (handler
->error
) {
666 ret
= handler
->error
;
667 dev_err(&ov2685
->client
->dev
,
668 "Failed to init controls(%d)\n", ret
);
669 goto err_free_handler
;
672 ov2685
->subdev
.ctrl_handler
= handler
;
677 v4l2_ctrl_handler_free(handler
);
682 static int ov2685_check_sensor_id(struct ov2685
*ov2685
,
683 struct i2c_client
*client
)
685 struct device
*dev
= &ov2685
->client
->dev
;
689 ret
= ov2685_read_reg(client
, OV2685_REG_CHIP_ID
,
690 OV2685_REG_VALUE_16BIT
, &id
);
692 dev_err(dev
, "Unexpected sensor id(%04x), ret(%d)\n", id
, ret
);
696 dev_info(dev
, "Detected OV%04x sensor\n", CHIP_ID
);
701 static int ov2685_configure_regulators(struct ov2685
*ov2685
)
705 for (i
= 0; i
< OV2685_NUM_SUPPLIES
; i
++)
706 ov2685
->supplies
[i
].supply
= ov2685_supply_names
[i
];
708 return devm_regulator_bulk_get(&ov2685
->client
->dev
,
713 static int ov2685_probe(struct i2c_client
*client
,
714 const struct i2c_device_id
*id
)
716 struct device
*dev
= &client
->dev
;
717 struct ov2685
*ov2685
;
720 ov2685
= devm_kzalloc(dev
, sizeof(*ov2685
), GFP_KERNEL
);
724 ov2685
->client
= client
;
725 ov2685
->cur_mode
= &supported_modes
[0];
727 ov2685
->xvclk
= devm_clk_get(dev
, "xvclk");
728 if (IS_ERR(ov2685
->xvclk
)) {
729 dev_err(dev
, "Failed to get xvclk\n");
732 ret
= clk_set_rate(ov2685
->xvclk
, OV2685_XVCLK_FREQ
);
734 dev_err(dev
, "Failed to set xvclk rate (24MHz)\n");
737 if (clk_get_rate(ov2685
->xvclk
) != OV2685_XVCLK_FREQ
)
738 dev_warn(dev
, "xvclk mismatched, modes are based on 24MHz\n");
740 ov2685
->reset_gpio
= devm_gpiod_get(dev
, "reset", GPIOD_OUT_LOW
);
741 if (IS_ERR(ov2685
->reset_gpio
)) {
742 dev_err(dev
, "Failed to get reset-gpios\n");
746 ret
= ov2685_configure_regulators(ov2685
);
748 dev_err(dev
, "Failed to get power regulators\n");
752 mutex_init(&ov2685
->mutex
);
753 v4l2_i2c_subdev_init(&ov2685
->subdev
, client
, &ov2685_subdev_ops
);
754 ret
= ov2685_initialize_controls(ov2685
);
756 goto err_destroy_mutex
;
758 ret
= __ov2685_power_on(ov2685
);
760 goto err_free_handler
;
762 ret
= ov2685_check_sensor_id(ov2685
, client
);
766 #ifdef CONFIG_VIDEO_V4L2_SUBDEV_API
767 ov2685
->subdev
.internal_ops
= &ov2685_internal_ops
;
768 ov2685
->subdev
.flags
|= V4L2_SUBDEV_FL_HAS_DEVNODE
;
770 #if defined(CONFIG_MEDIA_CONTROLLER)
771 ov2685
->pad
.flags
= MEDIA_PAD_FL_SOURCE
;
772 ov2685
->subdev
.entity
.function
= MEDIA_ENT_F_CAM_SENSOR
;
773 ret
= media_entity_pads_init(&ov2685
->subdev
.entity
, 1, &ov2685
->pad
);
778 ret
= v4l2_async_register_subdev(&ov2685
->subdev
);
780 dev_err(dev
, "v4l2 async register subdev failed\n");
781 goto err_clean_entity
;
784 pm_runtime_set_active(dev
);
785 pm_runtime_enable(dev
);
786 pm_runtime_idle(dev
);
791 #if defined(CONFIG_MEDIA_CONTROLLER)
792 media_entity_cleanup(&ov2685
->subdev
.entity
);
795 __ov2685_power_off(ov2685
);
797 v4l2_ctrl_handler_free(&ov2685
->ctrl_handler
);
799 mutex_destroy(&ov2685
->mutex
);
804 static int ov2685_remove(struct i2c_client
*client
)
806 struct v4l2_subdev
*sd
= i2c_get_clientdata(client
);
807 struct ov2685
*ov2685
= to_ov2685(sd
);
809 v4l2_async_unregister_subdev(sd
);
810 #if defined(CONFIG_MEDIA_CONTROLLER)
811 media_entity_cleanup(&sd
->entity
);
813 v4l2_ctrl_handler_free(&ov2685
->ctrl_handler
);
814 mutex_destroy(&ov2685
->mutex
);
816 pm_runtime_disable(&client
->dev
);
817 if (!pm_runtime_status_suspended(&client
->dev
))
818 __ov2685_power_off(ov2685
);
819 pm_runtime_set_suspended(&client
->dev
);
824 #if IS_ENABLED(CONFIG_OF)
825 static const struct of_device_id ov2685_of_match
[] = {
826 { .compatible
= "ovti,ov2685" },
829 MODULE_DEVICE_TABLE(of
, ov2685_of_match
);
832 static struct i2c_driver ov2685_i2c_driver
= {
835 .pm
= &ov2685_pm_ops
,
836 .of_match_table
= of_match_ptr(ov2685_of_match
),
838 .probe
= &ov2685_probe
,
839 .remove
= &ov2685_remove
,
842 module_i2c_driver(ov2685_i2c_driver
);
844 MODULE_DESCRIPTION("OmniVision ov2685 sensor driver");
845 MODULE_LICENSE("GPL v2");