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 #ifndef V4L2_CID_DIGITAL_GAIN
23 #define V4L2_CID_DIGITAL_GAIN V4L2_CID_GAIN
26 /* 45Mhz * 4 Binning */
27 #define OV5695_PIXEL_RATE (45 * 1000 * 1000 * 4)
28 #define OV5695_XVCLK_FREQ 24000000
30 #define CHIP_ID 0x005695
31 #define OV5695_REG_CHIP_ID 0x300a
33 #define OV5695_REG_CTRL_MODE 0x0100
34 #define OV5695_MODE_SW_STANDBY 0x0
35 #define OV5695_MODE_STREAMING BIT(0)
37 #define OV5695_REG_EXPOSURE 0x3500
38 #define OV5695_EXPOSURE_MIN 4
39 #define OV5695_EXPOSURE_STEP 1
40 #define OV5695_VTS_MAX 0x7fff
42 #define OV5695_REG_ANALOG_GAIN 0x3509
43 #define ANALOG_GAIN_MIN 0x10
44 #define ANALOG_GAIN_MAX 0xf8
45 #define ANALOG_GAIN_STEP 1
46 #define ANALOG_GAIN_DEFAULT 0xf8
48 #define OV5695_REG_DIGI_GAIN_H 0x350a
49 #define OV5695_REG_DIGI_GAIN_L 0x350b
50 #define OV5695_DIGI_GAIN_L_MASK 0x3f
51 #define OV5695_DIGI_GAIN_H_SHIFT 6
52 #define OV5695_DIGI_GAIN_MIN 0
53 #define OV5695_DIGI_GAIN_MAX (0x4000 - 1)
54 #define OV5695_DIGI_GAIN_STEP 1
55 #define OV5695_DIGI_GAIN_DEFAULT 1024
57 #define OV5695_REG_TEST_PATTERN 0x4503
58 #define OV5695_TEST_PATTERN_ENABLE 0x80
59 #define OV5695_TEST_PATTERN_DISABLE 0x0
61 #define OV5695_REG_VTS 0x380e
63 #define REG_NULL 0xFFFF
65 #define OV5695_REG_VALUE_08BIT 1
66 #define OV5695_REG_VALUE_16BIT 2
67 #define OV5695_REG_VALUE_24BIT 3
69 #define OV5695_LANES 2
70 #define OV5695_BITS_PER_SAMPLE 10
72 static const char * const ov5695_supply_names
[] = {
73 "avdd", /* Analog power */
74 "dovdd", /* Digital I/O power */
75 "dvdd", /* Digital core power */
78 #define OV5695_NUM_SUPPLIES ARRAY_SIZE(ov5695_supply_names)
92 const struct regval
*reg_list
;
96 struct i2c_client
*client
;
98 struct gpio_desc
*reset_gpio
;
99 struct regulator_bulk_data supplies
[OV5695_NUM_SUPPLIES
];
101 struct v4l2_subdev subdev
;
102 struct media_pad pad
;
103 struct v4l2_ctrl_handler ctrl_handler
;
104 struct v4l2_ctrl
*exposure
;
105 struct v4l2_ctrl
*anal_gain
;
106 struct v4l2_ctrl
*digi_gain
;
107 struct v4l2_ctrl
*hblank
;
108 struct v4l2_ctrl
*vblank
;
109 struct v4l2_ctrl
*test_pattern
;
112 const struct ov5695_mode
*cur_mode
;
115 #define to_ov5695(sd) container_of(sd, struct ov5695, subdev)
120 * linelength 672(0x2a0)
121 * framelength 2232(0x8b8)
122 * grabwindow_width 1296
123 * grabwindow_height 972
124 * max_framerate 30fps
125 * mipi_datarate per lane 840Mbps
127 static const struct regval ov5695_global_regs
[] = {
295 * linelength 740(0x2e4)
296 * framelength 2024(0x7e8)
297 * grabwindow_width 2592
298 * grabwindow_height 1944
299 * max_framerate 30fps
300 * mipi_datarate per lane 840Mbps
302 static const struct regval ov5695_2592x1944_regs
[] = {
337 * linelength 672(0x2a0)
338 * framelength 2232(0x8b8)
339 * grabwindow_width 1920
340 * grabwindow_height 1080
341 * max_framerate 30fps
342 * mipi_datarate per lane 840Mbps
344 static const struct regval ov5695_1920x1080_regs
[] = {
379 * linelength 740(0x02e4)
380 * framelength 1012(0x03f4)
381 * grabwindow_width 1296
382 * grabwindow_height 972
383 * max_framerate 60fps
384 * mipi_datarate per lane 840Mbps
386 static const struct regval ov5695_1296x972_regs
[] = {
562 * linelength 672(0x2a0)
563 * framelength 2232(0x8b8)
564 * grabwindow_width 1280
565 * grabwindow_height 720
566 * max_framerate 30fps
567 * mipi_datarate per lane 840Mbps
569 static const struct regval ov5695_1280x720_regs
[] = {
604 * linelength 672(0x2a0)
605 * framelength 558(0x22e)
606 * grabwindow_width 640
607 * grabwindow_height 480
608 * max_framerate 120fps
609 * mipi_datarate per lane 840Mbps
611 static const struct regval ov5695_640x480_regs
[] = {
643 static const struct ov5695_mode supported_modes
[] = {
649 .hts_def
= 0x02e4 * 4,
651 .reg_list
= ov5695_2592x1944_regs
,
658 .hts_def
= 0x02a0 * 4,
660 .reg_list
= ov5695_1920x1080_regs
,
667 .hts_def
= 0x02e4 * 4,
669 .reg_list
= ov5695_1296x972_regs
,
676 .hts_def
= 0x02a0 * 4,
678 .reg_list
= ov5695_1280x720_regs
,
685 .hts_def
= 0x02a0 * 4,
687 .reg_list
= ov5695_640x480_regs
,
691 #define OV5695_LINK_FREQ_420MHZ 420000000
692 static const s64 link_freq_menu_items
[] = {
693 OV5695_LINK_FREQ_420MHZ
696 static const char * const ov5695_test_pattern_menu
[] = {
698 "Vertical Color Bar Type 1",
699 "Vertical Color Bar Type 2",
700 "Vertical Color Bar Type 3",
701 "Vertical Color Bar Type 4"
704 /* Write registers up to 4 at a time */
705 static int ov5695_write_reg(struct i2c_client
*client
, u16 reg
,
719 val_be
= cpu_to_be32(val
);
720 val_p
= (u8
*)&val_be
;
725 buf
[buf_i
++] = val_p
[val_i
++];
727 if (i2c_master_send(client
, buf
, len
+ 2) != len
+ 2)
733 static int ov5695_write_array(struct i2c_client
*client
,
734 const struct regval
*regs
)
739 for (i
= 0; ret
== 0 && regs
[i
].addr
!= REG_NULL
; i
++)
740 ret
= ov5695_write_reg(client
, regs
[i
].addr
,
741 OV5695_REG_VALUE_08BIT
, regs
[i
].val
);
746 /* Read registers up to 4 at a time */
747 static int ov5695_read_reg(struct i2c_client
*client
, u16 reg
, unsigned int len
,
750 struct i2c_msg msgs
[2];
753 __be16 reg_addr_be
= cpu_to_be16(reg
);
759 data_be_p
= (u8
*)&data_be
;
760 /* Write register address */
761 msgs
[0].addr
= client
->addr
;
764 msgs
[0].buf
= (u8
*)®_addr_be
;
766 /* Read data from register */
767 msgs
[1].addr
= client
->addr
;
768 msgs
[1].flags
= I2C_M_RD
;
770 msgs
[1].buf
= &data_be_p
[4 - len
];
772 ret
= i2c_transfer(client
->adapter
, msgs
, ARRAY_SIZE(msgs
));
773 if (ret
!= ARRAY_SIZE(msgs
))
776 *val
= be32_to_cpu(data_be
);
781 static int ov5695_get_reso_dist(const struct ov5695_mode
*mode
,
782 struct v4l2_mbus_framefmt
*framefmt
)
784 return abs(mode
->width
- framefmt
->width
) +
785 abs(mode
->height
- framefmt
->height
);
788 static const struct ov5695_mode
*
789 ov5695_find_best_fit(struct v4l2_subdev_format
*fmt
)
791 struct v4l2_mbus_framefmt
*framefmt
= &fmt
->format
;
793 int cur_best_fit
= 0;
794 int cur_best_fit_dist
= -1;
797 for (i
= 0; i
< ARRAY_SIZE(supported_modes
); i
++) {
798 dist
= ov5695_get_reso_dist(&supported_modes
[i
], framefmt
);
799 if (cur_best_fit_dist
== -1 || dist
< cur_best_fit_dist
) {
800 cur_best_fit_dist
= dist
;
805 return &supported_modes
[cur_best_fit
];
808 static int ov5695_set_fmt(struct v4l2_subdev
*sd
,
809 struct v4l2_subdev_pad_config
*cfg
,
810 struct v4l2_subdev_format
*fmt
)
812 struct ov5695
*ov5695
= to_ov5695(sd
);
813 const struct ov5695_mode
*mode
;
814 s64 h_blank
, vblank_def
;
816 mutex_lock(&ov5695
->mutex
);
818 mode
= ov5695_find_best_fit(fmt
);
819 fmt
->format
.code
= MEDIA_BUS_FMT_SBGGR10_1X10
;
820 fmt
->format
.width
= mode
->width
;
821 fmt
->format
.height
= mode
->height
;
822 fmt
->format
.field
= V4L2_FIELD_NONE
;
823 if (fmt
->which
== V4L2_SUBDEV_FORMAT_TRY
) {
824 #ifdef CONFIG_VIDEO_V4L2_SUBDEV_API
825 *v4l2_subdev_get_try_format(sd
, cfg
, fmt
->pad
) = fmt
->format
;
828 ov5695
->cur_mode
= mode
;
829 h_blank
= mode
->hts_def
- mode
->width
;
830 __v4l2_ctrl_modify_range(ov5695
->hblank
, h_blank
,
831 h_blank
, 1, h_blank
);
832 vblank_def
= mode
->vts_def
- mode
->height
;
833 __v4l2_ctrl_modify_range(ov5695
->vblank
, vblank_def
,
834 OV5695_VTS_MAX
- mode
->height
,
838 mutex_unlock(&ov5695
->mutex
);
843 static int ov5695_get_fmt(struct v4l2_subdev
*sd
,
844 struct v4l2_subdev_pad_config
*cfg
,
845 struct v4l2_subdev_format
*fmt
)
847 struct ov5695
*ov5695
= to_ov5695(sd
);
848 const struct ov5695_mode
*mode
= ov5695
->cur_mode
;
850 mutex_lock(&ov5695
->mutex
);
851 if (fmt
->which
== V4L2_SUBDEV_FORMAT_TRY
) {
852 #ifdef CONFIG_VIDEO_V4L2_SUBDEV_API
853 fmt
->format
= *v4l2_subdev_get_try_format(sd
, cfg
, fmt
->pad
);
855 mutex_unlock(&ov5695
->mutex
);
859 fmt
->format
.width
= mode
->width
;
860 fmt
->format
.height
= mode
->height
;
861 fmt
->format
.code
= MEDIA_BUS_FMT_SBGGR10_1X10
;
862 fmt
->format
.field
= V4L2_FIELD_NONE
;
864 mutex_unlock(&ov5695
->mutex
);
869 static int ov5695_enum_mbus_code(struct v4l2_subdev
*sd
,
870 struct v4l2_subdev_pad_config
*cfg
,
871 struct v4l2_subdev_mbus_code_enum
*code
)
873 if (code
->index
!= 0)
875 code
->code
= MEDIA_BUS_FMT_SBGGR10_1X10
;
880 static int ov5695_enum_frame_sizes(struct v4l2_subdev
*sd
,
881 struct v4l2_subdev_pad_config
*cfg
,
882 struct v4l2_subdev_frame_size_enum
*fse
)
884 if (fse
->index
>= ARRAY_SIZE(supported_modes
))
887 if (fse
->code
!= MEDIA_BUS_FMT_SBGGR10_1X10
)
890 fse
->min_width
= supported_modes
[fse
->index
].width
;
891 fse
->max_width
= supported_modes
[fse
->index
].width
;
892 fse
->max_height
= supported_modes
[fse
->index
].height
;
893 fse
->min_height
= supported_modes
[fse
->index
].height
;
898 static int ov5695_enable_test_pattern(struct ov5695
*ov5695
, u32 pattern
)
903 val
= (pattern
- 1) | OV5695_TEST_PATTERN_ENABLE
;
905 val
= OV5695_TEST_PATTERN_DISABLE
;
907 return ov5695_write_reg(ov5695
->client
, OV5695_REG_TEST_PATTERN
,
908 OV5695_REG_VALUE_08BIT
, val
);
911 static int __ov5695_start_stream(struct ov5695
*ov5695
)
915 ret
= ov5695_write_array(ov5695
->client
, ov5695_global_regs
);
918 ret
= ov5695_write_array(ov5695
->client
, ov5695
->cur_mode
->reg_list
);
922 /* In case these controls are set before streaming */
923 ret
= __v4l2_ctrl_handler_setup(&ov5695
->ctrl_handler
);
927 return ov5695_write_reg(ov5695
->client
, OV5695_REG_CTRL_MODE
,
928 OV5695_REG_VALUE_08BIT
, OV5695_MODE_STREAMING
);
931 static int __ov5695_stop_stream(struct ov5695
*ov5695
)
933 return ov5695_write_reg(ov5695
->client
, OV5695_REG_CTRL_MODE
,
934 OV5695_REG_VALUE_08BIT
, OV5695_MODE_SW_STANDBY
);
937 static int ov5695_s_stream(struct v4l2_subdev
*sd
, int on
)
939 struct ov5695
*ov5695
= to_ov5695(sd
);
940 struct i2c_client
*client
= ov5695
->client
;
943 mutex_lock(&ov5695
->mutex
);
945 if (on
== ov5695
->streaming
)
946 goto unlock_and_return
;
949 ret
= pm_runtime_get_sync(&client
->dev
);
951 pm_runtime_put_noidle(&client
->dev
);
952 goto unlock_and_return
;
955 ret
= __ov5695_start_stream(ov5695
);
957 v4l2_err(sd
, "start stream failed while write regs\n");
958 pm_runtime_put(&client
->dev
);
959 goto unlock_and_return
;
962 __ov5695_stop_stream(ov5695
);
963 pm_runtime_put(&client
->dev
);
966 ov5695
->streaming
= on
;
969 mutex_unlock(&ov5695
->mutex
);
974 static int __ov5695_power_on(struct ov5695
*ov5695
)
977 struct device
*dev
= &ov5695
->client
->dev
;
979 ret
= clk_prepare_enable(ov5695
->xvclk
);
981 dev_err(dev
, "Failed to enable xvclk\n");
985 gpiod_set_value_cansleep(ov5695
->reset_gpio
, 1);
988 * The hardware requires the regulators to be powered on in order,
989 * so enable them one by one.
991 for (i
= 0; i
< OV5695_NUM_SUPPLIES
; i
++) {
992 ret
= regulator_enable(ov5695
->supplies
[i
].consumer
);
994 dev_err(dev
, "Failed to enable %s: %d\n",
995 ov5695
->supplies
[i
].supply
, ret
);
996 goto disable_reg_clk
;
1000 gpiod_set_value_cansleep(ov5695
->reset_gpio
, 0);
1002 usleep_range(1000, 1200);
1007 for (--i
; i
>= 0; i
--)
1008 regulator_disable(ov5695
->supplies
[i
].consumer
);
1009 clk_disable_unprepare(ov5695
->xvclk
);
1014 static void __ov5695_power_off(struct ov5695
*ov5695
)
1016 struct device
*dev
= &ov5695
->client
->dev
;
1019 clk_disable_unprepare(ov5695
->xvclk
);
1020 gpiod_set_value_cansleep(ov5695
->reset_gpio
, 1);
1023 * The hardware requires the regulators to be powered off in order,
1024 * so disable them one by one.
1026 for (i
= OV5695_NUM_SUPPLIES
- 1; i
>= 0; i
--) {
1027 ret
= regulator_disable(ov5695
->supplies
[i
].consumer
);
1029 dev_err(dev
, "Failed to disable %s: %d\n",
1030 ov5695
->supplies
[i
].supply
, ret
);
1034 static int __maybe_unused
ov5695_runtime_resume(struct device
*dev
)
1036 struct v4l2_subdev
*sd
= dev_get_drvdata(dev
);
1037 struct ov5695
*ov5695
= to_ov5695(sd
);
1039 return __ov5695_power_on(ov5695
);
1042 static int __maybe_unused
ov5695_runtime_suspend(struct device
*dev
)
1044 struct v4l2_subdev
*sd
= dev_get_drvdata(dev
);
1045 struct ov5695
*ov5695
= to_ov5695(sd
);
1047 __ov5695_power_off(ov5695
);
1052 #ifdef CONFIG_VIDEO_V4L2_SUBDEV_API
1053 static int ov5695_open(struct v4l2_subdev
*sd
, struct v4l2_subdev_fh
*fh
)
1055 struct ov5695
*ov5695
= to_ov5695(sd
);
1056 struct v4l2_mbus_framefmt
*try_fmt
=
1057 v4l2_subdev_get_try_format(sd
, fh
->pad
, 0);
1058 const struct ov5695_mode
*def_mode
= &supported_modes
[0];
1060 mutex_lock(&ov5695
->mutex
);
1061 /* Initialize try_fmt */
1062 try_fmt
->width
= def_mode
->width
;
1063 try_fmt
->height
= def_mode
->height
;
1064 try_fmt
->code
= MEDIA_BUS_FMT_SBGGR10_1X10
;
1065 try_fmt
->field
= V4L2_FIELD_NONE
;
1067 mutex_unlock(&ov5695
->mutex
);
1068 /* No crop or compose */
1074 static const struct dev_pm_ops ov5695_pm_ops
= {
1075 SET_RUNTIME_PM_OPS(ov5695_runtime_suspend
,
1076 ov5695_runtime_resume
, NULL
)
1079 #ifdef CONFIG_VIDEO_V4L2_SUBDEV_API
1080 static const struct v4l2_subdev_internal_ops ov5695_internal_ops
= {
1081 .open
= ov5695_open
,
1085 static const struct v4l2_subdev_video_ops ov5695_video_ops
= {
1086 .s_stream
= ov5695_s_stream
,
1089 static const struct v4l2_subdev_pad_ops ov5695_pad_ops
= {
1090 .enum_mbus_code
= ov5695_enum_mbus_code
,
1091 .enum_frame_size
= ov5695_enum_frame_sizes
,
1092 .get_fmt
= ov5695_get_fmt
,
1093 .set_fmt
= ov5695_set_fmt
,
1096 static const struct v4l2_subdev_ops ov5695_subdev_ops
= {
1097 .video
= &ov5695_video_ops
,
1098 .pad
= &ov5695_pad_ops
,
1101 static int ov5695_set_ctrl(struct v4l2_ctrl
*ctrl
)
1103 struct ov5695
*ov5695
= container_of(ctrl
->handler
,
1104 struct ov5695
, ctrl_handler
);
1105 struct i2c_client
*client
= ov5695
->client
;
1109 /* Propagate change of current control to all related controls */
1111 case V4L2_CID_VBLANK
:
1112 /* Update max exposure while meeting expected vblanking */
1113 max
= ov5695
->cur_mode
->height
+ ctrl
->val
- 4;
1114 __v4l2_ctrl_modify_range(ov5695
->exposure
,
1115 ov5695
->exposure
->minimum
, max
,
1116 ov5695
->exposure
->step
,
1117 ov5695
->exposure
->default_value
);
1121 if (!pm_runtime_get_if_in_use(&client
->dev
))
1125 case V4L2_CID_EXPOSURE
:
1126 /* 4 least significant bits of expsoure are fractional part */
1127 ret
= ov5695_write_reg(ov5695
->client
, OV5695_REG_EXPOSURE
,
1128 OV5695_REG_VALUE_24BIT
, ctrl
->val
<< 4);
1130 case V4L2_CID_ANALOGUE_GAIN
:
1131 ret
= ov5695_write_reg(ov5695
->client
, OV5695_REG_ANALOG_GAIN
,
1132 OV5695_REG_VALUE_08BIT
, ctrl
->val
);
1134 case V4L2_CID_DIGITAL_GAIN
:
1135 ret
= ov5695_write_reg(ov5695
->client
, OV5695_REG_DIGI_GAIN_L
,
1136 OV5695_REG_VALUE_08BIT
,
1137 ctrl
->val
& OV5695_DIGI_GAIN_L_MASK
);
1138 ret
= ov5695_write_reg(ov5695
->client
, OV5695_REG_DIGI_GAIN_H
,
1139 OV5695_REG_VALUE_08BIT
,
1140 ctrl
->val
>> OV5695_DIGI_GAIN_H_SHIFT
);
1142 case V4L2_CID_VBLANK
:
1143 ret
= ov5695_write_reg(ov5695
->client
, OV5695_REG_VTS
,
1144 OV5695_REG_VALUE_16BIT
,
1145 ctrl
->val
+ ov5695
->cur_mode
->height
);
1147 case V4L2_CID_TEST_PATTERN
:
1148 ret
= ov5695_enable_test_pattern(ov5695
, ctrl
->val
);
1151 dev_warn(&client
->dev
, "%s Unhandled id:0x%x, val:0x%x\n",
1152 __func__
, ctrl
->id
, ctrl
->val
);
1156 pm_runtime_put(&client
->dev
);
1161 static const struct v4l2_ctrl_ops ov5695_ctrl_ops
= {
1162 .s_ctrl
= ov5695_set_ctrl
,
1165 static int ov5695_initialize_controls(struct ov5695
*ov5695
)
1167 const struct ov5695_mode
*mode
;
1168 struct v4l2_ctrl_handler
*handler
;
1169 struct v4l2_ctrl
*ctrl
;
1170 s64 exposure_max
, vblank_def
;
1174 handler
= &ov5695
->ctrl_handler
;
1175 mode
= ov5695
->cur_mode
;
1176 ret
= v4l2_ctrl_handler_init(handler
, 8);
1179 handler
->lock
= &ov5695
->mutex
;
1181 ctrl
= v4l2_ctrl_new_int_menu(handler
, NULL
, V4L2_CID_LINK_FREQ
,
1182 0, 0, link_freq_menu_items
);
1184 ctrl
->flags
|= V4L2_CTRL_FLAG_READ_ONLY
;
1186 v4l2_ctrl_new_std(handler
, NULL
, V4L2_CID_PIXEL_RATE
,
1187 0, OV5695_PIXEL_RATE
, 1, OV5695_PIXEL_RATE
);
1189 h_blank
= mode
->hts_def
- mode
->width
;
1190 ov5695
->hblank
= v4l2_ctrl_new_std(handler
, NULL
, V4L2_CID_HBLANK
,
1191 h_blank
, h_blank
, 1, h_blank
);
1193 ov5695
->hblank
->flags
|= V4L2_CTRL_FLAG_READ_ONLY
;
1195 vblank_def
= mode
->vts_def
- mode
->height
;
1196 ov5695
->vblank
= v4l2_ctrl_new_std(handler
, &ov5695_ctrl_ops
,
1197 V4L2_CID_VBLANK
, vblank_def
,
1198 OV5695_VTS_MAX
- mode
->height
,
1201 exposure_max
= mode
->vts_def
- 4;
1202 ov5695
->exposure
= v4l2_ctrl_new_std(handler
, &ov5695_ctrl_ops
,
1203 V4L2_CID_EXPOSURE
, OV5695_EXPOSURE_MIN
,
1204 exposure_max
, OV5695_EXPOSURE_STEP
,
1207 ov5695
->anal_gain
= v4l2_ctrl_new_std(handler
, &ov5695_ctrl_ops
,
1208 V4L2_CID_ANALOGUE_GAIN
, ANALOG_GAIN_MIN
,
1209 ANALOG_GAIN_MAX
, ANALOG_GAIN_STEP
,
1210 ANALOG_GAIN_DEFAULT
);
1213 ov5695
->digi_gain
= v4l2_ctrl_new_std(handler
, &ov5695_ctrl_ops
,
1214 V4L2_CID_DIGITAL_GAIN
, OV5695_DIGI_GAIN_MIN
,
1215 OV5695_DIGI_GAIN_MAX
, OV5695_DIGI_GAIN_STEP
,
1216 OV5695_DIGI_GAIN_DEFAULT
);
1218 ov5695
->test_pattern
= v4l2_ctrl_new_std_menu_items(handler
,
1219 &ov5695_ctrl_ops
, V4L2_CID_TEST_PATTERN
,
1220 ARRAY_SIZE(ov5695_test_pattern_menu
) - 1,
1221 0, 0, ov5695_test_pattern_menu
);
1223 if (handler
->error
) {
1224 ret
= handler
->error
;
1225 dev_err(&ov5695
->client
->dev
,
1226 "Failed to init controls(%d)\n", ret
);
1227 goto err_free_handler
;
1230 ov5695
->subdev
.ctrl_handler
= handler
;
1235 v4l2_ctrl_handler_free(handler
);
1240 static int ov5695_check_sensor_id(struct ov5695
*ov5695
,
1241 struct i2c_client
*client
)
1243 struct device
*dev
= &ov5695
->client
->dev
;
1247 ret
= ov5695_read_reg(client
, OV5695_REG_CHIP_ID
,
1248 OV5695_REG_VALUE_24BIT
, &id
);
1249 if (id
!= CHIP_ID
) {
1250 dev_err(dev
, "Unexpected sensor id(%06x), ret(%d)\n", id
, ret
);
1254 dev_info(dev
, "Detected OV%06x sensor\n", CHIP_ID
);
1259 static int ov5695_configure_regulators(struct ov5695
*ov5695
)
1263 for (i
= 0; i
< OV5695_NUM_SUPPLIES
; i
++)
1264 ov5695
->supplies
[i
].supply
= ov5695_supply_names
[i
];
1266 return devm_regulator_bulk_get(&ov5695
->client
->dev
,
1267 OV5695_NUM_SUPPLIES
,
1271 static int ov5695_probe(struct i2c_client
*client
,
1272 const struct i2c_device_id
*id
)
1274 struct device
*dev
= &client
->dev
;
1275 struct ov5695
*ov5695
;
1276 struct v4l2_subdev
*sd
;
1279 ov5695
= devm_kzalloc(dev
, sizeof(*ov5695
), GFP_KERNEL
);
1283 ov5695
->client
= client
;
1284 ov5695
->cur_mode
= &supported_modes
[0];
1286 ov5695
->xvclk
= devm_clk_get(dev
, "xvclk");
1287 if (IS_ERR(ov5695
->xvclk
)) {
1288 dev_err(dev
, "Failed to get xvclk\n");
1291 ret
= clk_set_rate(ov5695
->xvclk
, OV5695_XVCLK_FREQ
);
1293 dev_err(dev
, "Failed to set xvclk rate (24MHz)\n");
1296 if (clk_get_rate(ov5695
->xvclk
) != OV5695_XVCLK_FREQ
)
1297 dev_warn(dev
, "xvclk mismatched, modes are based on 24MHz\n");
1299 ov5695
->reset_gpio
= devm_gpiod_get(dev
, "reset", GPIOD_OUT_HIGH
);
1300 if (IS_ERR(ov5695
->reset_gpio
)) {
1301 dev_err(dev
, "Failed to get reset-gpios\n");
1305 ret
= ov5695_configure_regulators(ov5695
);
1307 dev_err(dev
, "Failed to get power regulators\n");
1311 mutex_init(&ov5695
->mutex
);
1313 sd
= &ov5695
->subdev
;
1314 v4l2_i2c_subdev_init(sd
, client
, &ov5695_subdev_ops
);
1315 ret
= ov5695_initialize_controls(ov5695
);
1317 goto err_destroy_mutex
;
1319 ret
= __ov5695_power_on(ov5695
);
1321 goto err_free_handler
;
1323 ret
= ov5695_check_sensor_id(ov5695
, client
);
1327 #ifdef CONFIG_VIDEO_V4L2_SUBDEV_API
1328 sd
->internal_ops
= &ov5695_internal_ops
;
1329 sd
->flags
|= V4L2_SUBDEV_FL_HAS_DEVNODE
;
1331 #if defined(CONFIG_MEDIA_CONTROLLER)
1332 ov5695
->pad
.flags
= MEDIA_PAD_FL_SOURCE
;
1333 sd
->entity
.function
= MEDIA_ENT_F_CAM_SENSOR
;
1334 ret
= media_entity_pads_init(&sd
->entity
, 1, &ov5695
->pad
);
1339 ret
= v4l2_async_register_subdev_sensor_common(sd
);
1341 dev_err(dev
, "v4l2 async register subdev failed\n");
1342 goto err_clean_entity
;
1345 pm_runtime_set_active(dev
);
1346 pm_runtime_enable(dev
);
1347 pm_runtime_idle(dev
);
1352 #if defined(CONFIG_MEDIA_CONTROLLER)
1353 media_entity_cleanup(&sd
->entity
);
1356 __ov5695_power_off(ov5695
);
1358 v4l2_ctrl_handler_free(&ov5695
->ctrl_handler
);
1360 mutex_destroy(&ov5695
->mutex
);
1365 static int ov5695_remove(struct i2c_client
*client
)
1367 struct v4l2_subdev
*sd
= i2c_get_clientdata(client
);
1368 struct ov5695
*ov5695
= to_ov5695(sd
);
1370 v4l2_async_unregister_subdev(sd
);
1371 #if defined(CONFIG_MEDIA_CONTROLLER)
1372 media_entity_cleanup(&sd
->entity
);
1374 v4l2_ctrl_handler_free(&ov5695
->ctrl_handler
);
1375 mutex_destroy(&ov5695
->mutex
);
1377 pm_runtime_disable(&client
->dev
);
1378 if (!pm_runtime_status_suspended(&client
->dev
))
1379 __ov5695_power_off(ov5695
);
1380 pm_runtime_set_suspended(&client
->dev
);
1385 #if IS_ENABLED(CONFIG_OF)
1386 static const struct of_device_id ov5695_of_match
[] = {
1387 { .compatible
= "ovti,ov5695" },
1390 MODULE_DEVICE_TABLE(of
, ov5695_of_match
);
1393 static struct i2c_driver ov5695_i2c_driver
= {
1396 .pm
= &ov5695_pm_ops
,
1397 .of_match_table
= of_match_ptr(ov5695_of_match
),
1399 .probe
= &ov5695_probe
,
1400 .remove
= &ov5695_remove
,
1403 module_i2c_driver(ov5695_i2c_driver
);
1405 MODULE_DESCRIPTION("OmniVision ov5695 sensor driver");
1406 MODULE_LICENSE("GPL v2");