1 // SPDX-License-Identifier: GPL-2.0
2 // Copyright (c) 2017 Intel Corporation.
4 #include <linux/unaligned.h>
5 #include <linux/acpi.h>
7 #include <linux/delay.h>
8 #include <linux/gpio/consumer.h>
10 #include <linux/mod_devicetable.h>
11 #include <linux/module.h>
13 #include <linux/pm_runtime.h>
14 #include <linux/regulator/consumer.h>
15 #include <media/v4l2-ctrls.h>
16 #include <media/v4l2-device.h>
17 #include <media/v4l2-event.h>
18 #include <media/v4l2-fwnode.h>
20 #define OV5670_XVCLK_FREQ 19200000
22 #define OV5670_REG_CHIP_ID 0x300a
23 #define OV5670_CHIP_ID 0x005670
25 #define OV5670_REG_MODE_SELECT 0x0100
26 #define OV5670_MODE_STANDBY 0x00
27 #define OV5670_MODE_STREAMING 0x01
29 #define OV5670_REG_SOFTWARE_RST 0x0103
30 #define OV5670_SOFTWARE_RST 0x01
32 #define OV5670_MIPI_SC_CTRL0_REG 0x3018
33 #define OV5670_MIPI_SC_CTRL0_LANES(v) ((((v) - 1) << 5) & \
35 #define OV5670_MIPI_SC_CTRL0_MIPI_EN BIT(4)
36 #define OV5670_MIPI_SC_CTRL0_RESERVED BIT(1)
38 /* vertical-timings from sensor */
39 #define OV5670_REG_VTS 0x380e
40 #define OV5670_VTS_30FPS 0x0808 /* default for 30 fps */
41 #define OV5670_VTS_MAX 0xffff
43 /* horizontal-timings from sensor */
44 #define OV5670_REG_HTS 0x380c
47 * Pixels-per-line(PPL) = Time-per-line * pixel-rate
48 * In OV5670, Time-per-line = HTS/SCLK.
49 * HTS is fixed for all resolutions, not recommended to change.
51 #define OV5670_FIXED_PPL 2724 /* Pixels per line */
53 /* Exposure controls from sensor */
54 #define OV5670_REG_EXPOSURE 0x3500
55 #define OV5670_EXPOSURE_MIN 4
56 #define OV5670_EXPOSURE_STEP 1
58 /* Analog gain controls from sensor */
59 #define OV5670_REG_ANALOG_GAIN 0x3508
60 #define ANALOG_GAIN_MIN 0
61 #define ANALOG_GAIN_MAX 8191
62 #define ANALOG_GAIN_STEP 1
63 #define ANALOG_GAIN_DEFAULT 128
65 /* Digital gain controls from sensor */
66 #define OV5670_REG_R_DGTL_GAIN 0x5032
67 #define OV5670_REG_G_DGTL_GAIN 0x5034
68 #define OV5670_REG_B_DGTL_GAIN 0x5036
69 #define OV5670_DGTL_GAIN_MIN 0
70 #define OV5670_DGTL_GAIN_MAX 4095
71 #define OV5670_DGTL_GAIN_STEP 1
72 #define OV5670_DGTL_GAIN_DEFAULT 1024
74 /* Test Pattern Control */
75 #define OV5670_REG_TEST_PATTERN 0x4303
76 #define OV5670_TEST_PATTERN_ENABLE BIT(3)
77 #define OV5670_REG_TEST_PATTERN_CTRL 0x4320
79 #define OV5670_REG_VALUE_08BIT 1
80 #define OV5670_REG_VALUE_16BIT 2
81 #define OV5670_REG_VALUE_24BIT 3
84 #define OV5670_NATIVE_WIDTH 2624
85 #define OV5670_NATIVE_HEIGHT 1980
87 /* Initial number of frames to skip to avoid possible garbage */
88 #define OV5670_NUM_OF_SKIP_FRAMES 2
95 struct ov5670_reg_list
{
97 const struct ov5670_reg
*regs
;
100 struct ov5670_link_freq_config
{
101 const struct ov5670_reg_list reg_list
;
104 static const char * const ov5670_supply_names
[] = {
105 "avdd", /* Analog power */
106 "dvdd", /* Digital power */
107 "dovdd", /* Digital output power */
110 #define OV5670_NUM_SUPPLIES ARRAY_SIZE(ov5670_supply_names)
113 /* Frame width in pixels */
116 /* Frame height in pixels */
119 /* Default vertical timining size */
122 /* Min vertical timining size */
125 /* Link frequency needed for this resolution */
128 /* Analog crop rectangle */
129 const struct v4l2_rect
*analog_crop
;
131 /* Sensor register settings for this resolution */
132 const struct ov5670_reg_list reg_list
;
136 * All the modes supported by the driver are obtained by subsampling the
137 * full pixel array. The below values are reflected in registers from
138 * 0x3800-0x3807 in the modes register-value tables.
140 static const struct v4l2_rect ov5670_analog_crop
= {
147 static const struct ov5670_reg mipi_data_rate_840mbps
[] = {
165 static const struct ov5670_reg mode_2592x1944_regs
[] = {
430 static const struct ov5670_reg mode_1296x972_regs
[] = {
695 static const struct ov5670_reg mode_648x486_regs
[] = {
960 static const struct ov5670_reg mode_2560x1440_regs
[] = {
1224 static const struct ov5670_reg mode_1280x720_regs
[] = {
1489 static const struct ov5670_reg mode_640x360_regs
[] = {
1754 static const char * const ov5670_test_pattern_menu
[] = {
1756 "Vertical Color Bar Type 1",
1759 /* Supported link frequencies */
1760 #define OV5670_LINK_FREQ_422MHZ 422400000
1761 #define OV5670_LINK_FREQ_422MHZ_INDEX 0
1762 static const struct ov5670_link_freq_config link_freq_configs
[] = {
1765 .num_of_regs
= ARRAY_SIZE(mipi_data_rate_840mbps
),
1766 .regs
= mipi_data_rate_840mbps
,
1771 static const s64 link_freq_menu_items
[] = {
1772 OV5670_LINK_FREQ_422MHZ
1776 * OV5670 sensor supports following resolutions with full FOV:
1777 * 4:3 ==> {2592x1944, 1296x972, 648x486}
1778 * 16:9 ==> {2560x1440, 1280x720, 640x360}
1780 static const struct ov5670_mode supported_modes
[] = {
1784 .vts_def
= OV5670_VTS_30FPS
,
1785 .vts_min
= OV5670_VTS_30FPS
,
1786 .link_freq_index
= OV5670_LINK_FREQ_422MHZ_INDEX
,
1787 .analog_crop
= &ov5670_analog_crop
,
1789 .num_of_regs
= ARRAY_SIZE(mode_2592x1944_regs
),
1790 .regs
= mode_2592x1944_regs
,
1796 .vts_def
= OV5670_VTS_30FPS
,
1798 .link_freq_index
= OV5670_LINK_FREQ_422MHZ_INDEX
,
1799 .analog_crop
= &ov5670_analog_crop
,
1801 .num_of_regs
= ARRAY_SIZE(mode_1296x972_regs
),
1802 .regs
= mode_1296x972_regs
,
1808 .vts_def
= OV5670_VTS_30FPS
,
1810 .link_freq_index
= OV5670_LINK_FREQ_422MHZ_INDEX
,
1811 .analog_crop
= &ov5670_analog_crop
,
1813 .num_of_regs
= ARRAY_SIZE(mode_648x486_regs
),
1814 .regs
= mode_648x486_regs
,
1820 .vts_def
= OV5670_VTS_30FPS
,
1821 .vts_min
= OV5670_VTS_30FPS
,
1822 .link_freq_index
= OV5670_LINK_FREQ_422MHZ_INDEX
,
1823 .analog_crop
= &ov5670_analog_crop
,
1825 .num_of_regs
= ARRAY_SIZE(mode_2560x1440_regs
),
1826 .regs
= mode_2560x1440_regs
,
1832 .vts_def
= OV5670_VTS_30FPS
,
1835 .link_freq_index
= OV5670_LINK_FREQ_422MHZ_INDEX
,
1836 .analog_crop
= &ov5670_analog_crop
,
1838 .num_of_regs
= ARRAY_SIZE(mode_1280x720_regs
),
1839 .regs
= mode_1280x720_regs
,
1845 .vts_def
= OV5670_VTS_30FPS
,
1847 .link_freq_index
= OV5670_LINK_FREQ_422MHZ_INDEX
,
1848 .analog_crop
= &ov5670_analog_crop
,
1850 .num_of_regs
= ARRAY_SIZE(mode_640x360_regs
),
1851 .regs
= mode_640x360_regs
,
1857 struct v4l2_subdev sd
;
1858 struct media_pad pad
;
1859 struct v4l2_fwnode_endpoint endpoint
;
1861 struct v4l2_ctrl_handler ctrl_handler
;
1863 struct v4l2_ctrl
*link_freq
;
1864 struct v4l2_ctrl
*pixel_rate
;
1865 struct v4l2_ctrl
*vblank
;
1866 struct v4l2_ctrl
*hblank
;
1867 struct v4l2_ctrl
*exposure
;
1870 const struct ov5670_mode
*cur_mode
;
1872 /* xvclk input clock */
1876 struct regulator_bulk_data supplies
[OV5670_NUM_SUPPLIES
];
1878 /* Power-down and reset gpios. */
1879 struct gpio_desc
*pwdn_gpio
; /* PWDNB pin. */
1880 struct gpio_desc
*reset_gpio
; /* XSHUTDOWN pin. */
1882 /* To serialize asynchronous callbacks */
1885 /* True if the device has been identified */
1889 #define to_ov5670(_sd) container_of(_sd, struct ov5670, sd)
1891 /* Read registers up to 4 at a time */
1892 static int ov5670_read_reg(struct ov5670
*ov5670
, u16 reg
, unsigned int len
,
1895 struct i2c_client
*client
= v4l2_get_subdevdata(&ov5670
->sd
);
1896 struct i2c_msg msgs
[2];
1899 __be16 reg_addr_be
= cpu_to_be16(reg
);
1905 data_be_p
= (u8
*)&data_be
;
1906 /* Write register address */
1907 msgs
[0].addr
= client
->addr
;
1910 msgs
[0].buf
= (u8
*)®_addr_be
;
1912 /* Read data from register */
1913 msgs
[1].addr
= client
->addr
;
1914 msgs
[1].flags
= I2C_M_RD
;
1916 msgs
[1].buf
= &data_be_p
[4 - len
];
1918 ret
= i2c_transfer(client
->adapter
, msgs
, ARRAY_SIZE(msgs
));
1919 if (ret
!= ARRAY_SIZE(msgs
))
1922 *val
= be32_to_cpu(data_be
);
1927 /* Write registers up to 4 at a time */
1928 static int ov5670_write_reg(struct ov5670
*ov5670
, u16 reg
, unsigned int len
,
1931 struct i2c_client
*client
= v4l2_get_subdevdata(&ov5670
->sd
);
1942 buf
[1] = reg
& 0xff;
1944 tmp
= cpu_to_be32(val
);
1950 buf
[buf_i
++] = val_p
[val_i
++];
1952 if (i2c_master_send(client
, buf
, len
+ 2) != len
+ 2)
1958 /* Write a list of registers */
1959 static int ov5670_write_regs(struct ov5670
*ov5670
,
1960 const struct ov5670_reg
*regs
, unsigned int len
)
1962 struct i2c_client
*client
= v4l2_get_subdevdata(&ov5670
->sd
);
1966 for (i
= 0; i
< len
; i
++) {
1967 ret
= ov5670_write_reg(ov5670
, regs
[i
].address
, 1, regs
[i
].val
);
1969 dev_err_ratelimited(
1971 "Failed to write reg 0x%4.4x. error = %d\n",
1972 regs
[i
].address
, ret
);
1981 static int ov5670_write_reg_list(struct ov5670
*ov5670
,
1982 const struct ov5670_reg_list
*r_list
)
1984 return ov5670_write_regs(ov5670
, r_list
->regs
, r_list
->num_of_regs
);
1987 static int ov5670_update_digital_gain(struct ov5670
*ov5670
, u32 d_gain
)
1991 ret
= ov5670_write_reg(ov5670
, OV5670_REG_R_DGTL_GAIN
,
1992 OV5670_REG_VALUE_16BIT
, d_gain
);
1996 ret
= ov5670_write_reg(ov5670
, OV5670_REG_G_DGTL_GAIN
,
1997 OV5670_REG_VALUE_16BIT
, d_gain
);
2001 return ov5670_write_reg(ov5670
, OV5670_REG_B_DGTL_GAIN
,
2002 OV5670_REG_VALUE_16BIT
, d_gain
);
2005 static int ov5670_enable_test_pattern(struct ov5670
*ov5670
, u32 pattern
)
2010 /* Set the bayer order that we support */
2011 ret
= ov5670_write_reg(ov5670
, OV5670_REG_TEST_PATTERN_CTRL
,
2012 OV5670_REG_VALUE_08BIT
, 0);
2016 ret
= ov5670_read_reg(ov5670
, OV5670_REG_TEST_PATTERN
,
2017 OV5670_REG_VALUE_08BIT
, &val
);
2022 val
|= OV5670_TEST_PATTERN_ENABLE
;
2024 val
&= ~OV5670_TEST_PATTERN_ENABLE
;
2026 return ov5670_write_reg(ov5670
, OV5670_REG_TEST_PATTERN
,
2027 OV5670_REG_VALUE_08BIT
, val
);
2030 /* Initialize control handlers */
2031 static int ov5670_set_ctrl(struct v4l2_ctrl
*ctrl
)
2033 struct ov5670
*ov5670
= container_of(ctrl
->handler
,
2034 struct ov5670
, ctrl_handler
);
2035 struct i2c_client
*client
= v4l2_get_subdevdata(&ov5670
->sd
);
2039 /* Propagate change of current control to all related controls */
2041 case V4L2_CID_VBLANK
:
2042 /* Update max exposure while meeting expected vblanking */
2043 max
= ov5670
->cur_mode
->height
+ ctrl
->val
- 8;
2044 __v4l2_ctrl_modify_range(ov5670
->exposure
,
2045 ov5670
->exposure
->minimum
, max
,
2046 ov5670
->exposure
->step
, max
);
2050 /* V4L2 controls values will be applied only when power is already up */
2051 if (!pm_runtime_get_if_in_use(&client
->dev
))
2055 case V4L2_CID_ANALOGUE_GAIN
:
2056 ret
= ov5670_write_reg(ov5670
, OV5670_REG_ANALOG_GAIN
,
2057 OV5670_REG_VALUE_16BIT
, ctrl
->val
);
2059 case V4L2_CID_DIGITAL_GAIN
:
2060 ret
= ov5670_update_digital_gain(ov5670
, ctrl
->val
);
2062 case V4L2_CID_EXPOSURE
:
2063 /* 4 least significant bits of expsoure are fractional part */
2064 ret
= ov5670_write_reg(ov5670
, OV5670_REG_EXPOSURE
,
2065 OV5670_REG_VALUE_24BIT
, ctrl
->val
<< 4);
2067 case V4L2_CID_VBLANK
:
2068 /* Update VTS that meets expected vertical blanking */
2069 ret
= ov5670_write_reg(ov5670
, OV5670_REG_VTS
,
2070 OV5670_REG_VALUE_16BIT
,
2071 ov5670
->cur_mode
->height
+ ctrl
->val
);
2073 case V4L2_CID_TEST_PATTERN
:
2074 ret
= ov5670_enable_test_pattern(ov5670
, ctrl
->val
);
2076 case V4L2_CID_HBLANK
:
2077 case V4L2_CID_LINK_FREQ
:
2078 case V4L2_CID_PIXEL_RATE
:
2083 dev_info(&client
->dev
, "%s Unhandled id:0x%x, val:0x%x\n",
2084 __func__
, ctrl
->id
, ctrl
->val
);
2088 pm_runtime_put(&client
->dev
);
2093 static const struct v4l2_ctrl_ops ov5670_ctrl_ops
= {
2094 .s_ctrl
= ov5670_set_ctrl
,
2097 /* Initialize control handlers */
2098 static int ov5670_init_controls(struct ov5670
*ov5670
)
2100 struct v4l2_mbus_config_mipi_csi2
*bus_mipi_csi2
=
2101 &ov5670
->endpoint
.bus
.mipi_csi2
;
2102 struct i2c_client
*client
= v4l2_get_subdevdata(&ov5670
->sd
);
2103 struct v4l2_fwnode_device_properties props
;
2104 struct v4l2_ctrl_handler
*ctrl_hdlr
;
2105 unsigned int lanes_count
;
2106 s64 mipi_pixel_rate
;
2113 ctrl_hdlr
= &ov5670
->ctrl_handler
;
2114 ret
= v4l2_ctrl_handler_init(ctrl_hdlr
, 10);
2118 ctrl_hdlr
->lock
= &ov5670
->mutex
;
2119 ov5670
->link_freq
= v4l2_ctrl_new_int_menu(ctrl_hdlr
,
2122 0, 0, link_freq_menu_items
);
2123 if (ov5670
->link_freq
)
2124 ov5670
->link_freq
->flags
|= V4L2_CTRL_FLAG_READ_ONLY
;
2126 /* By default, V4L2_CID_PIXEL_RATE is read only */
2127 lanes_count
= bus_mipi_csi2
->num_data_lanes
;
2128 mipi_pixel_rate
= OV5670_LINK_FREQ_422MHZ
* 2 * lanes_count
/ 10;
2130 ov5670
->pixel_rate
= v4l2_ctrl_new_std(ctrl_hdlr
, &ov5670_ctrl_ops
,
2131 V4L2_CID_PIXEL_RATE
,
2137 vblank_max
= OV5670_VTS_MAX
- ov5670
->cur_mode
->height
;
2138 vblank_def
= ov5670
->cur_mode
->vts_def
- ov5670
->cur_mode
->height
;
2139 vblank_min
= ov5670
->cur_mode
->vts_min
- ov5670
->cur_mode
->height
;
2140 ov5670
->vblank
= v4l2_ctrl_new_std(ctrl_hdlr
, &ov5670_ctrl_ops
,
2141 V4L2_CID_VBLANK
, vblank_min
,
2142 vblank_max
, 1, vblank_def
);
2144 ov5670
->hblank
= v4l2_ctrl_new_std(
2145 ctrl_hdlr
, &ov5670_ctrl_ops
, V4L2_CID_HBLANK
,
2146 OV5670_FIXED_PPL
- ov5670
->cur_mode
->width
,
2147 OV5670_FIXED_PPL
- ov5670
->cur_mode
->width
, 1,
2148 OV5670_FIXED_PPL
- ov5670
->cur_mode
->width
);
2150 ov5670
->hblank
->flags
|= V4L2_CTRL_FLAG_READ_ONLY
;
2152 /* Get min, max, step, default from sensor */
2153 v4l2_ctrl_new_std(ctrl_hdlr
, &ov5670_ctrl_ops
, V4L2_CID_ANALOGUE_GAIN
,
2154 ANALOG_GAIN_MIN
, ANALOG_GAIN_MAX
, ANALOG_GAIN_STEP
,
2155 ANALOG_GAIN_DEFAULT
);
2158 v4l2_ctrl_new_std(ctrl_hdlr
, &ov5670_ctrl_ops
, V4L2_CID_DIGITAL_GAIN
,
2159 OV5670_DGTL_GAIN_MIN
, OV5670_DGTL_GAIN_MAX
,
2160 OV5670_DGTL_GAIN_STEP
, OV5670_DGTL_GAIN_DEFAULT
);
2162 /* Get min, max, step, default from sensor */
2163 exposure_max
= ov5670
->cur_mode
->vts_def
- 8;
2164 ov5670
->exposure
= v4l2_ctrl_new_std(ctrl_hdlr
, &ov5670_ctrl_ops
,
2166 OV5670_EXPOSURE_MIN
,
2167 exposure_max
, OV5670_EXPOSURE_STEP
,
2170 v4l2_ctrl_new_std_menu_items(ctrl_hdlr
, &ov5670_ctrl_ops
,
2171 V4L2_CID_TEST_PATTERN
,
2172 ARRAY_SIZE(ov5670_test_pattern_menu
) - 1,
2173 0, 0, ov5670_test_pattern_menu
);
2175 if (ctrl_hdlr
->error
) {
2176 ret
= ctrl_hdlr
->error
;
2180 ret
= v4l2_fwnode_device_parse(&client
->dev
, &props
);
2184 ret
= v4l2_ctrl_new_fwnode_properties(ctrl_hdlr
, &ov5670_ctrl_ops
,
2189 ov5670
->sd
.ctrl_handler
= ctrl_hdlr
;
2194 v4l2_ctrl_handler_free(ctrl_hdlr
);
2199 static int ov5670_init_state(struct v4l2_subdev
*sd
,
2200 struct v4l2_subdev_state
*state
)
2202 struct v4l2_mbus_framefmt
*fmt
=
2203 v4l2_subdev_state_get_format(state
, 0);
2204 const struct ov5670_mode
*default_mode
= &supported_modes
[0];
2205 struct v4l2_rect
*crop
= v4l2_subdev_state_get_crop(state
, 0);
2207 fmt
->width
= default_mode
->width
;
2208 fmt
->height
= default_mode
->height
;
2209 fmt
->code
= MEDIA_BUS_FMT_SGRBG10_1X10
;
2210 fmt
->field
= V4L2_FIELD_NONE
;
2211 fmt
->colorspace
= V4L2_COLORSPACE_SRGB
;
2212 fmt
->ycbcr_enc
= V4L2_MAP_YCBCR_ENC_DEFAULT(V4L2_COLORSPACE_SRGB
);
2213 fmt
->quantization
= V4L2_QUANTIZATION_FULL_RANGE
;
2214 fmt
->xfer_func
= V4L2_MAP_XFER_FUNC_DEFAULT(V4L2_COLORSPACE_SRGB
);
2216 *crop
= *default_mode
->analog_crop
;
2221 static int ov5670_enum_mbus_code(struct v4l2_subdev
*sd
,
2222 struct v4l2_subdev_state
*sd_state
,
2223 struct v4l2_subdev_mbus_code_enum
*code
)
2225 /* Only one bayer order GRBG is supported */
2226 if (code
->index
> 0)
2229 code
->code
= MEDIA_BUS_FMT_SGRBG10_1X10
;
2234 static int ov5670_enum_frame_size(struct v4l2_subdev
*sd
,
2235 struct v4l2_subdev_state
*sd_state
,
2236 struct v4l2_subdev_frame_size_enum
*fse
)
2238 if (fse
->index
>= ARRAY_SIZE(supported_modes
))
2241 if (fse
->code
!= MEDIA_BUS_FMT_SGRBG10_1X10
)
2244 fse
->min_width
= supported_modes
[fse
->index
].width
;
2245 fse
->max_width
= fse
->min_width
;
2246 fse
->min_height
= supported_modes
[fse
->index
].height
;
2247 fse
->max_height
= fse
->min_height
;
2252 static void ov5670_update_pad_format(const struct ov5670_mode
*mode
,
2253 struct v4l2_subdev_format
*fmt
)
2255 fmt
->format
.width
= mode
->width
;
2256 fmt
->format
.height
= mode
->height
;
2257 fmt
->format
.code
= MEDIA_BUS_FMT_SGRBG10_1X10
;
2258 fmt
->format
.field
= V4L2_FIELD_NONE
;
2261 static int ov5670_do_get_pad_format(struct ov5670
*ov5670
,
2262 struct v4l2_subdev_state
*sd_state
,
2263 struct v4l2_subdev_format
*fmt
)
2265 if (fmt
->which
== V4L2_SUBDEV_FORMAT_TRY
)
2266 fmt
->format
= *v4l2_subdev_state_get_format(sd_state
,
2269 ov5670_update_pad_format(ov5670
->cur_mode
, fmt
);
2274 static int ov5670_get_pad_format(struct v4l2_subdev
*sd
,
2275 struct v4l2_subdev_state
*sd_state
,
2276 struct v4l2_subdev_format
*fmt
)
2278 struct ov5670
*ov5670
= to_ov5670(sd
);
2281 mutex_lock(&ov5670
->mutex
);
2282 ret
= ov5670_do_get_pad_format(ov5670
, sd_state
, fmt
);
2283 mutex_unlock(&ov5670
->mutex
);
2288 static int ov5670_set_pad_format(struct v4l2_subdev
*sd
,
2289 struct v4l2_subdev_state
*sd_state
,
2290 struct v4l2_subdev_format
*fmt
)
2292 struct ov5670
*ov5670
= to_ov5670(sd
);
2293 struct v4l2_mbus_config_mipi_csi2
*bus_mipi_csi2
=
2294 &ov5670
->endpoint
.bus
.mipi_csi2
;
2295 const struct ov5670_mode
*mode
;
2296 unsigned int lanes_count
;
2297 s64 mipi_pixel_rate
;
2302 mutex_lock(&ov5670
->mutex
);
2304 fmt
->format
.code
= MEDIA_BUS_FMT_SGRBG10_1X10
;
2306 mode
= v4l2_find_nearest_size(supported_modes
,
2307 ARRAY_SIZE(supported_modes
),
2309 fmt
->format
.width
, fmt
->format
.height
);
2310 ov5670_update_pad_format(mode
, fmt
);
2311 if (fmt
->which
== V4L2_SUBDEV_FORMAT_TRY
) {
2312 *v4l2_subdev_state_get_format(sd_state
, fmt
->pad
) = fmt
->format
;
2314 ov5670
->cur_mode
= mode
;
2315 __v4l2_ctrl_s_ctrl(ov5670
->link_freq
, mode
->link_freq_index
);
2317 lanes_count
= bus_mipi_csi2
->num_data_lanes
;
2318 link_freq
= link_freq_menu_items
[mode
->link_freq_index
];
2319 /* pixel_rate = link_freq * 2 * nr_of_lanes / bits_per_sample */
2320 mipi_pixel_rate
= div_s64(link_freq
* 2 * lanes_count
, 10);
2321 __v4l2_ctrl_s_ctrl_int64(
2324 /* Update limits and set FPS to default */
2325 vblank_def
= ov5670
->cur_mode
->vts_def
-
2326 ov5670
->cur_mode
->height
;
2327 __v4l2_ctrl_modify_range(
2329 ov5670
->cur_mode
->vts_min
- ov5670
->cur_mode
->height
,
2330 OV5670_VTS_MAX
- ov5670
->cur_mode
->height
, 1,
2332 __v4l2_ctrl_s_ctrl(ov5670
->vblank
, vblank_def
);
2333 h_blank
= OV5670_FIXED_PPL
- ov5670
->cur_mode
->width
;
2334 __v4l2_ctrl_modify_range(ov5670
->hblank
, h_blank
, h_blank
, 1,
2338 mutex_unlock(&ov5670
->mutex
);
2343 static int ov5670_get_skip_frames(struct v4l2_subdev
*sd
, u32
*frames
)
2345 *frames
= OV5670_NUM_OF_SKIP_FRAMES
;
2350 /* Verify chip ID */
2351 static int ov5670_identify_module(struct ov5670
*ov5670
)
2353 struct i2c_client
*client
= v4l2_get_subdevdata(&ov5670
->sd
);
2357 if (ov5670
->identified
)
2360 ret
= ov5670_read_reg(ov5670
, OV5670_REG_CHIP_ID
,
2361 OV5670_REG_VALUE_24BIT
, &val
);
2365 if (val
!= OV5670_CHIP_ID
) {
2366 dev_err(&client
->dev
, "chip id mismatch: %x!=%x\n",
2367 OV5670_CHIP_ID
, val
);
2371 ov5670
->identified
= true;
2376 static int ov5670_mipi_configure(struct ov5670
*ov5670
)
2378 struct v4l2_mbus_config_mipi_csi2
*bus_mipi_csi2
=
2379 &ov5670
->endpoint
.bus
.mipi_csi2
;
2380 unsigned int lanes_count
= bus_mipi_csi2
->num_data_lanes
;
2382 return ov5670_write_reg(ov5670
, OV5670_MIPI_SC_CTRL0_REG
,
2383 OV5670_REG_VALUE_08BIT
,
2384 OV5670_MIPI_SC_CTRL0_LANES(lanes_count
) |
2385 OV5670_MIPI_SC_CTRL0_MIPI_EN
|
2386 OV5670_MIPI_SC_CTRL0_RESERVED
);
2389 /* Prepare streaming by writing default values and customized values */
2390 static int ov5670_start_streaming(struct ov5670
*ov5670
)
2392 struct i2c_client
*client
= v4l2_get_subdevdata(&ov5670
->sd
);
2393 const struct ov5670_reg_list
*reg_list
;
2394 int link_freq_index
;
2397 ret
= ov5670_identify_module(ov5670
);
2401 /* Get out of from software reset */
2402 ret
= ov5670_write_reg(ov5670
, OV5670_REG_SOFTWARE_RST
,
2403 OV5670_REG_VALUE_08BIT
, OV5670_SOFTWARE_RST
);
2405 dev_err(&client
->dev
, "%s failed to set powerup registers\n",
2411 link_freq_index
= ov5670
->cur_mode
->link_freq_index
;
2412 reg_list
= &link_freq_configs
[link_freq_index
].reg_list
;
2413 ret
= ov5670_write_reg_list(ov5670
, reg_list
);
2415 dev_err(&client
->dev
, "%s failed to set plls\n", __func__
);
2419 /* Apply default values of current mode */
2420 reg_list
= &ov5670
->cur_mode
->reg_list
;
2421 ret
= ov5670_write_reg_list(ov5670
, reg_list
);
2423 dev_err(&client
->dev
, "%s failed to set mode\n", __func__
);
2427 ret
= ov5670_mipi_configure(ov5670
);
2429 dev_err(&client
->dev
, "%s failed to configure MIPI\n", __func__
);
2433 ret
= __v4l2_ctrl_handler_setup(ov5670
->sd
.ctrl_handler
);
2437 /* Write stream on list */
2438 ret
= ov5670_write_reg(ov5670
, OV5670_REG_MODE_SELECT
,
2439 OV5670_REG_VALUE_08BIT
, OV5670_MODE_STREAMING
);
2441 dev_err(&client
->dev
, "%s failed to set stream\n", __func__
);
2448 static int ov5670_stop_streaming(struct ov5670
*ov5670
)
2450 struct i2c_client
*client
= v4l2_get_subdevdata(&ov5670
->sd
);
2453 ret
= ov5670_write_reg(ov5670
, OV5670_REG_MODE_SELECT
,
2454 OV5670_REG_VALUE_08BIT
, OV5670_MODE_STANDBY
);
2456 dev_err(&client
->dev
, "%s failed to set stream\n", __func__
);
2458 /* Return success even if it was an error, as there is nothing the
2459 * caller can do about it.
2464 static int ov5670_set_stream(struct v4l2_subdev
*sd
, int enable
)
2466 struct ov5670
*ov5670
= to_ov5670(sd
);
2467 struct i2c_client
*client
= v4l2_get_subdevdata(sd
);
2470 mutex_lock(&ov5670
->mutex
);
2473 ret
= pm_runtime_resume_and_get(&client
->dev
);
2475 goto unlock_and_return
;
2477 ret
= ov5670_start_streaming(ov5670
);
2481 ret
= ov5670_stop_streaming(ov5670
);
2482 pm_runtime_put(&client
->dev
);
2484 goto unlock_and_return
;
2487 pm_runtime_put(&client
->dev
);
2490 mutex_unlock(&ov5670
->mutex
);
2495 static int __maybe_unused
ov5670_runtime_resume(struct device
*dev
)
2497 struct i2c_client
*client
= to_i2c_client(dev
);
2498 struct v4l2_subdev
*sd
= i2c_get_clientdata(client
);
2499 struct ov5670
*ov5670
= to_ov5670(sd
);
2500 unsigned long delay_us
;
2503 ret
= clk_prepare_enable(ov5670
->xvclk
);
2507 ret
= regulator_bulk_enable(OV5670_NUM_SUPPLIES
, ov5670
->supplies
);
2509 clk_disable_unprepare(ov5670
->xvclk
);
2513 gpiod_set_value_cansleep(ov5670
->pwdn_gpio
, 0);
2514 gpiod_set_value_cansleep(ov5670
->reset_gpio
, 0);
2516 /* 8192 * 2 clock pulses before the first SCCB transaction. */
2517 delay_us
= DIV_ROUND_UP(8192 * 2 * 1000,
2518 DIV_ROUND_UP(OV5670_XVCLK_FREQ
, 1000));
2524 static int __maybe_unused
ov5670_runtime_suspend(struct device
*dev
)
2526 struct i2c_client
*client
= to_i2c_client(dev
);
2527 struct v4l2_subdev
*sd
= i2c_get_clientdata(client
);
2528 struct ov5670
*ov5670
= to_ov5670(sd
);
2530 gpiod_set_value_cansleep(ov5670
->reset_gpio
, 1);
2531 gpiod_set_value_cansleep(ov5670
->pwdn_gpio
, 1);
2532 regulator_bulk_disable(OV5670_NUM_SUPPLIES
, ov5670
->supplies
);
2533 clk_disable_unprepare(ov5670
->xvclk
);
2538 static const struct v4l2_subdev_core_ops ov5670_core_ops
= {
2539 .log_status
= v4l2_ctrl_subdev_log_status
,
2540 .subscribe_event
= v4l2_ctrl_subdev_subscribe_event
,
2541 .unsubscribe_event
= v4l2_event_subdev_unsubscribe
,
2544 static const struct v4l2_rect
*
2545 __ov5670_get_pad_crop(struct ov5670
*sensor
, struct v4l2_subdev_state
*state
,
2546 unsigned int pad
, enum v4l2_subdev_format_whence which
)
2548 const struct ov5670_mode
*mode
= sensor
->cur_mode
;
2551 case V4L2_SUBDEV_FORMAT_TRY
:
2552 return v4l2_subdev_state_get_crop(state
, pad
);
2553 case V4L2_SUBDEV_FORMAT_ACTIVE
:
2554 return mode
->analog_crop
;
2560 static int ov5670_get_selection(struct v4l2_subdev
*subdev
,
2561 struct v4l2_subdev_state
*state
,
2562 struct v4l2_subdev_selection
*sel
)
2564 struct ov5670
*sensor
= to_ov5670(subdev
);
2566 switch (sel
->target
) {
2567 case V4L2_SEL_TGT_CROP
:
2568 mutex_lock(&sensor
->mutex
);
2569 sel
->r
= *__ov5670_get_pad_crop(sensor
, state
, sel
->pad
,
2571 mutex_unlock(&sensor
->mutex
);
2573 case V4L2_SEL_TGT_NATIVE_SIZE
:
2574 case V4L2_SEL_TGT_CROP_BOUNDS
:
2577 sel
->r
.width
= OV5670_NATIVE_WIDTH
;
2578 sel
->r
.height
= OV5670_NATIVE_HEIGHT
;
2580 case V4L2_SEL_TGT_CROP_DEFAULT
:
2581 sel
->r
= ov5670_analog_crop
;
2590 static const struct v4l2_subdev_video_ops ov5670_video_ops
= {
2591 .s_stream
= ov5670_set_stream
,
2594 static const struct v4l2_subdev_pad_ops ov5670_pad_ops
= {
2595 .enum_mbus_code
= ov5670_enum_mbus_code
,
2596 .get_fmt
= ov5670_get_pad_format
,
2597 .set_fmt
= ov5670_set_pad_format
,
2598 .enum_frame_size
= ov5670_enum_frame_size
,
2599 .get_selection
= ov5670_get_selection
,
2600 .set_selection
= ov5670_get_selection
,
2603 static const struct v4l2_subdev_sensor_ops ov5670_sensor_ops
= {
2604 .g_skip_frames
= ov5670_get_skip_frames
,
2607 static const struct v4l2_subdev_ops ov5670_subdev_ops
= {
2608 .core
= &ov5670_core_ops
,
2609 .video
= &ov5670_video_ops
,
2610 .pad
= &ov5670_pad_ops
,
2611 .sensor
= &ov5670_sensor_ops
,
2614 static const struct v4l2_subdev_internal_ops ov5670_internal_ops
= {
2615 .init_state
= ov5670_init_state
,
2618 static const struct media_entity_operations ov5670_subdev_entity_ops
= {
2619 .link_validate
= v4l2_subdev_link_validate
,
2622 static int ov5670_regulators_probe(struct ov5670
*ov5670
)
2624 struct i2c_client
*client
= v4l2_get_subdevdata(&ov5670
->sd
);
2627 for (i
= 0; i
< OV5670_NUM_SUPPLIES
; i
++)
2628 ov5670
->supplies
[i
].supply
= ov5670_supply_names
[i
];
2630 return devm_regulator_bulk_get(&client
->dev
, OV5670_NUM_SUPPLIES
,
2634 static int ov5670_gpio_probe(struct ov5670
*ov5670
)
2636 struct i2c_client
*client
= v4l2_get_subdevdata(&ov5670
->sd
);
2638 ov5670
->pwdn_gpio
= devm_gpiod_get_optional(&client
->dev
, "powerdown",
2640 if (IS_ERR(ov5670
->pwdn_gpio
))
2641 return PTR_ERR(ov5670
->pwdn_gpio
);
2643 ov5670
->reset_gpio
= devm_gpiod_get_optional(&client
->dev
, "reset",
2645 if (IS_ERR(ov5670
->reset_gpio
))
2646 return PTR_ERR(ov5670
->reset_gpio
);
2651 static int ov5670_probe(struct i2c_client
*client
)
2653 struct fwnode_handle
*handle
;
2654 struct ov5670
*ov5670
;
2659 ov5670
= devm_kzalloc(&client
->dev
, sizeof(*ov5670
), GFP_KERNEL
);
2663 ov5670
->xvclk
= devm_clk_get_optional(&client
->dev
, NULL
);
2664 if (!IS_ERR_OR_NULL(ov5670
->xvclk
))
2665 input_clk
= clk_get_rate(ov5670
->xvclk
);
2666 else if (!ov5670
->xvclk
|| PTR_ERR(ov5670
->xvclk
) == -ENOENT
)
2667 device_property_read_u32(&client
->dev
, "clock-frequency",
2670 return dev_err_probe(&client
->dev
, PTR_ERR(ov5670
->xvclk
),
2671 "error getting clock\n");
2673 if (input_clk
!= OV5670_XVCLK_FREQ
) {
2674 dev_err(&client
->dev
,
2675 "Unsupported clock frequency %u\n", input_clk
);
2679 /* Initialize subdev */
2680 v4l2_i2c_subdev_init(&ov5670
->sd
, client
, &ov5670_subdev_ops
);
2681 ov5670
->sd
.internal_ops
= &ov5670_internal_ops
;
2683 ret
= ov5670_regulators_probe(ov5670
);
2685 return dev_err_probe(&client
->dev
, ret
, "Regulators probe failed\n");
2687 ret
= ov5670_gpio_probe(ov5670
);
2689 return dev_err_probe(&client
->dev
, ret
, "GPIO probe failed\n");
2691 /* Graph Endpoint */
2692 handle
= fwnode_graph_get_next_endpoint(dev_fwnode(&client
->dev
), NULL
);
2694 return dev_err_probe(&client
->dev
, -ENXIO
, "Endpoint for node get failed\n");
2696 ov5670
->endpoint
.bus_type
= V4L2_MBUS_CSI2_DPHY
;
2697 ov5670
->endpoint
.bus
.mipi_csi2
.num_data_lanes
= 2;
2699 ret
= v4l2_fwnode_endpoint_alloc_parse(handle
, &ov5670
->endpoint
);
2700 fwnode_handle_put(handle
);
2702 return dev_err_probe(&client
->dev
, ret
, "Endpoint parse failed\n");
2704 full_power
= acpi_dev_state_d0(&client
->dev
);
2706 ret
= ov5670_runtime_resume(&client
->dev
);
2708 dev_err_probe(&client
->dev
, ret
, "Power up failed\n");
2709 goto error_endpoint
;
2712 /* Check module identity */
2713 ret
= ov5670_identify_module(ov5670
);
2715 dev_err_probe(&client
->dev
, ret
, "ov5670_identify_module() error\n");
2716 goto error_power_off
;
2720 mutex_init(&ov5670
->mutex
);
2722 /* Set default mode to max resolution */
2723 ov5670
->cur_mode
= &supported_modes
[0];
2725 ret
= ov5670_init_controls(ov5670
);
2727 dev_err_probe(&client
->dev
, ret
, "ov5670_init_controls() error\n");
2728 goto error_mutex_destroy
;
2731 ov5670
->sd
.flags
|= V4L2_SUBDEV_FL_HAS_DEVNODE
|
2732 V4L2_SUBDEV_FL_HAS_EVENTS
;
2733 ov5670
->sd
.entity
.ops
= &ov5670_subdev_entity_ops
;
2734 ov5670
->sd
.entity
.function
= MEDIA_ENT_F_CAM_SENSOR
;
2736 /* Source pad initialization */
2737 ov5670
->pad
.flags
= MEDIA_PAD_FL_SOURCE
;
2738 ret
= media_entity_pads_init(&ov5670
->sd
.entity
, 1, &ov5670
->pad
);
2740 dev_err_probe(&client
->dev
, ret
, "media_entity_pads_init() error\n");
2741 goto error_handler_free
;
2744 /* Set the device's state to active if it's in D0 state. */
2746 pm_runtime_set_active(&client
->dev
);
2747 pm_runtime_enable(&client
->dev
);
2749 /* Async register for subdev */
2750 ret
= v4l2_async_register_subdev_sensor(&ov5670
->sd
);
2752 dev_err_probe(&client
->dev
, ret
, "v4l2_async_register_subdev() error\n");
2753 goto error_pm_disable
;
2756 pm_runtime_idle(&client
->dev
);
2761 pm_runtime_disable(&client
->dev
);
2763 media_entity_cleanup(&ov5670
->sd
.entity
);
2766 v4l2_ctrl_handler_free(ov5670
->sd
.ctrl_handler
);
2768 error_mutex_destroy
:
2769 mutex_destroy(&ov5670
->mutex
);
2773 ov5670_runtime_suspend(&client
->dev
);
2776 v4l2_fwnode_endpoint_free(&ov5670
->endpoint
);
2781 static void ov5670_remove(struct i2c_client
*client
)
2783 struct v4l2_subdev
*sd
= i2c_get_clientdata(client
);
2784 struct ov5670
*ov5670
= to_ov5670(sd
);
2786 v4l2_async_unregister_subdev(sd
);
2787 media_entity_cleanup(&sd
->entity
);
2788 v4l2_ctrl_handler_free(sd
->ctrl_handler
);
2789 mutex_destroy(&ov5670
->mutex
);
2791 pm_runtime_disable(&client
->dev
);
2792 ov5670_runtime_suspend(&client
->dev
);
2794 v4l2_fwnode_endpoint_free(&ov5670
->endpoint
);
2797 static const struct dev_pm_ops ov5670_pm_ops
= {
2798 SET_RUNTIME_PM_OPS(ov5670_runtime_suspend
, ov5670_runtime_resume
, NULL
)
2802 static const struct acpi_device_id ov5670_acpi_ids
[] = {
2807 MODULE_DEVICE_TABLE(acpi
, ov5670_acpi_ids
);
2810 static const struct of_device_id ov5670_of_ids
[] = {
2811 { .compatible
= "ovti,ov5670" },
2814 MODULE_DEVICE_TABLE(of
, ov5670_of_ids
);
2816 static struct i2c_driver ov5670_i2c_driver
= {
2819 .pm
= &ov5670_pm_ops
,
2820 .acpi_match_table
= ACPI_PTR(ov5670_acpi_ids
),
2821 .of_match_table
= ov5670_of_ids
,
2823 .probe
= ov5670_probe
,
2824 .remove
= ov5670_remove
,
2825 .flags
= I2C_DRV_ACPI_WAIVE_D0_PROBE
,
2828 module_i2c_driver(ov5670_i2c_driver
);
2830 MODULE_AUTHOR("Rapolu, Chiranjeevi");
2831 MODULE_AUTHOR("Yang, Hyungwoo");
2832 MODULE_DESCRIPTION("Omnivision ov5670 sensor driver");
2833 MODULE_LICENSE("GPL v2");