1 // SPDX-License-Identifier: GPL-2.0
2 // Copyright (c) 2017 Intel Corporation.
4 #include <asm/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 asynchronus callbacks */
1885 /* Streaming on/off */
1887 /* True if the device has been identified */
1891 #define to_ov5670(_sd) container_of(_sd, struct ov5670, sd)
1893 /* Read registers up to 4 at a time */
1894 static int ov5670_read_reg(struct ov5670 *ov5670, u16 reg, unsigned int len,
1897 struct i2c_client *client = v4l2_get_subdevdata(&ov5670->sd);
1898 struct i2c_msg msgs[2];
1901 __be16 reg_addr_be = cpu_to_be16(reg);
1907 data_be_p = (u8 *)&data_be;
1908 /* Write register address */
1909 msgs[0].addr = client->addr;
1912 msgs[0].buf = (u8 *)®_addr_be;
1914 /* Read data from register */
1915 msgs[1].addr = client->addr;
1916 msgs[1].flags = I2C_M_RD;
1918 msgs[1].buf = &data_be_p[4 - len];
1920 ret = i2c_transfer(client->adapter, msgs, ARRAY_SIZE(msgs));
1921 if (ret != ARRAY_SIZE(msgs))
1924 *val = be32_to_cpu(data_be);
1929 /* Write registers up to 4 at a time */
1930 static int ov5670_write_reg(struct ov5670 *ov5670, u16 reg, unsigned int len,
1933 struct i2c_client *client = v4l2_get_subdevdata(&ov5670->sd);
1944 buf[1] = reg & 0xff;
1946 tmp = cpu_to_be32(val);
1952 buf[buf_i++] = val_p[val_i++];
1954 if (i2c_master_send(client, buf, len + 2) != len + 2)
1960 /* Write a list of registers */
1961 static int ov5670_write_regs(struct ov5670 *ov5670,
1962 const struct ov5670_reg *regs, unsigned int len)
1964 struct i2c_client *client = v4l2_get_subdevdata(&ov5670->sd);
1968 for (i = 0; i < len; i++) {
1969 ret = ov5670_write_reg(ov5670, regs[i].address, 1, regs[i].val);
1971 dev_err_ratelimited(
1973 "Failed to write reg 0x%4.4x. error = %d\n",
1974 regs[i].address, ret);
1983 static int ov5670_write_reg_list(struct ov5670 *ov5670,
1984 const struct ov5670_reg_list *r_list)
1986 return ov5670_write_regs(ov5670, r_list->regs, r_list->num_of_regs);
1989 static int ov5670_update_digital_gain(struct ov5670 *ov5670, u32 d_gain)
1993 ret = ov5670_write_reg(ov5670, OV5670_REG_R_DGTL_GAIN,
1994 OV5670_REG_VALUE_16BIT, d_gain);
1998 ret = ov5670_write_reg(ov5670, OV5670_REG_G_DGTL_GAIN,
1999 OV5670_REG_VALUE_16BIT, d_gain);
2003 return ov5670_write_reg(ov5670, OV5670_REG_B_DGTL_GAIN,
2004 OV5670_REG_VALUE_16BIT, d_gain);
2007 static int ov5670_enable_test_pattern(struct ov5670 *ov5670, u32 pattern)
2012 /* Set the bayer order that we support */
2013 ret = ov5670_write_reg(ov5670, OV5670_REG_TEST_PATTERN_CTRL,
2014 OV5670_REG_VALUE_08BIT, 0);
2018 ret = ov5670_read_reg(ov5670, OV5670_REG_TEST_PATTERN,
2019 OV5670_REG_VALUE_08BIT, &val);
2024 val |= OV5670_TEST_PATTERN_ENABLE;
2026 val &= ~OV5670_TEST_PATTERN_ENABLE;
2028 return ov5670_write_reg(ov5670, OV5670_REG_TEST_PATTERN,
2029 OV5670_REG_VALUE_08BIT, val);
2032 /* Initialize control handlers */
2033 static int ov5670_set_ctrl(struct v4l2_ctrl *ctrl)
2035 struct ov5670 *ov5670 = container_of(ctrl->handler,
2036 struct ov5670, ctrl_handler);
2037 struct i2c_client *client = v4l2_get_subdevdata(&ov5670->sd);
2041 /* Propagate change of current control to all related controls */
2043 case V4L2_CID_VBLANK:
2044 /* Update max exposure while meeting expected vblanking */
2045 max = ov5670->cur_mode->height + ctrl->val - 8;
2046 __v4l2_ctrl_modify_range(ov5670->exposure,
2047 ov5670->exposure->minimum, max,
2048 ov5670->exposure->step, max);
2052 /* V4L2 controls values will be applied only when power is already up */
2053 if (!pm_runtime_get_if_in_use(&client->dev))
2057 case V4L2_CID_ANALOGUE_GAIN:
2058 ret = ov5670_write_reg(ov5670, OV5670_REG_ANALOG_GAIN,
2059 OV5670_REG_VALUE_16BIT, ctrl->val);
2061 case V4L2_CID_DIGITAL_GAIN:
2062 ret = ov5670_update_digital_gain(ov5670, ctrl->val);
2064 case V4L2_CID_EXPOSURE:
2065 /* 4 least significant bits of expsoure are fractional part */
2066 ret = ov5670_write_reg(ov5670, OV5670_REG_EXPOSURE,
2067 OV5670_REG_VALUE_24BIT, ctrl->val << 4);
2069 case V4L2_CID_VBLANK:
2070 /* Update VTS that meets expected vertical blanking */
2071 ret = ov5670_write_reg(ov5670, OV5670_REG_VTS,
2072 OV5670_REG_VALUE_16BIT,
2073 ov5670->cur_mode->height + ctrl->val);
2075 case V4L2_CID_TEST_PATTERN:
2076 ret = ov5670_enable_test_pattern(ov5670, ctrl->val);
2078 case V4L2_CID_HBLANK:
2079 case V4L2_CID_LINK_FREQ:
2080 case V4L2_CID_PIXEL_RATE:
2085 dev_info(&client->dev, "%s Unhandled id:0x%x, val:0x%x\n",
2086 __func__, ctrl->id, ctrl->val);
2090 pm_runtime_put(&client->dev);
2095 static const struct v4l2_ctrl_ops ov5670_ctrl_ops = {
2096 .s_ctrl = ov5670_set_ctrl,
2099 /* Initialize control handlers */
2100 static int ov5670_init_controls(struct ov5670 *ov5670)
2102 struct v4l2_mbus_config_mipi_csi2 *bus_mipi_csi2 =
2103 &ov5670->endpoint.bus.mipi_csi2;
2104 struct i2c_client *client = v4l2_get_subdevdata(&ov5670->sd);
2105 struct v4l2_fwnode_device_properties props;
2106 struct v4l2_ctrl_handler *ctrl_hdlr;
2107 unsigned int lanes_count;
2108 s64 mipi_pixel_rate;
2115 ctrl_hdlr = &ov5670->ctrl_handler;
2116 ret = v4l2_ctrl_handler_init(ctrl_hdlr, 10);
2120 ctrl_hdlr->lock = &ov5670->mutex;
2121 ov5670->link_freq = v4l2_ctrl_new_int_menu(ctrl_hdlr,
2124 0, 0, link_freq_menu_items);
2125 if (ov5670->link_freq)
2126 ov5670->link_freq->flags |= V4L2_CTRL_FLAG_READ_ONLY;
2128 /* By default, V4L2_CID_PIXEL_RATE is read only */
2129 lanes_count = bus_mipi_csi2->num_data_lanes;
2130 mipi_pixel_rate = OV5670_LINK_FREQ_422MHZ * 2 * lanes_count / 10;
2132 ov5670->pixel_rate = v4l2_ctrl_new_std(ctrl_hdlr, &ov5670_ctrl_ops,
2133 V4L2_CID_PIXEL_RATE,
2139 vblank_max = OV5670_VTS_MAX - ov5670->cur_mode->height;
2140 vblank_def = ov5670->cur_mode->vts_def - ov5670->cur_mode->height;
2141 vblank_min = ov5670->cur_mode->vts_min - ov5670->cur_mode->height;
2142 ov5670->vblank = v4l2_ctrl_new_std(ctrl_hdlr, &ov5670_ctrl_ops,
2143 V4L2_CID_VBLANK, vblank_min,
2144 vblank_max, 1, vblank_def);
2146 ov5670->hblank = v4l2_ctrl_new_std(
2147 ctrl_hdlr, &ov5670_ctrl_ops, V4L2_CID_HBLANK,
2148 OV5670_FIXED_PPL - ov5670->cur_mode->width,
2149 OV5670_FIXED_PPL - ov5670->cur_mode->width, 1,
2150 OV5670_FIXED_PPL - ov5670->cur_mode->width);
2152 ov5670->hblank->flags |= V4L2_CTRL_FLAG_READ_ONLY;
2154 /* Get min, max, step, default from sensor */
2155 v4l2_ctrl_new_std(ctrl_hdlr, &ov5670_ctrl_ops, V4L2_CID_ANALOGUE_GAIN,
2156 ANALOG_GAIN_MIN, ANALOG_GAIN_MAX, ANALOG_GAIN_STEP,
2157 ANALOG_GAIN_DEFAULT);
2160 v4l2_ctrl_new_std(ctrl_hdlr, &ov5670_ctrl_ops, V4L2_CID_DIGITAL_GAIN,
2161 OV5670_DGTL_GAIN_MIN, OV5670_DGTL_GAIN_MAX,
2162 OV5670_DGTL_GAIN_STEP, OV5670_DGTL_GAIN_DEFAULT);
2164 /* Get min, max, step, default from sensor */
2165 exposure_max = ov5670->cur_mode->vts_def - 8;
2166 ov5670->exposure = v4l2_ctrl_new_std(ctrl_hdlr, &ov5670_ctrl_ops,
2168 OV5670_EXPOSURE_MIN,
2169 exposure_max, OV5670_EXPOSURE_STEP,
2172 v4l2_ctrl_new_std_menu_items(ctrl_hdlr, &ov5670_ctrl_ops,
2173 V4L2_CID_TEST_PATTERN,
2174 ARRAY_SIZE(ov5670_test_pattern_menu) - 1,
2175 0, 0, ov5670_test_pattern_menu);
2177 if (ctrl_hdlr->error) {
2178 ret = ctrl_hdlr->error;
2182 ret = v4l2_fwnode_device_parse(&client->dev, &props);
2186 ret = v4l2_ctrl_new_fwnode_properties(ctrl_hdlr, &ov5670_ctrl_ops,
2191 ov5670->sd.ctrl_handler = ctrl_hdlr;
2196 v4l2_ctrl_handler_free(ctrl_hdlr);
2201 static int ov5670_init_cfg(struct v4l2_subdev *sd,
2202 struct v4l2_subdev_state *state)
2204 struct v4l2_mbus_framefmt *fmt =
2205 v4l2_subdev_get_try_format(sd, state, 0);
2206 const struct ov5670_mode *default_mode = &supported_modes[0];
2207 struct v4l2_rect *crop = v4l2_subdev_get_try_crop(sd, state, 0);
2209 fmt->width = default_mode->width;
2210 fmt->height = default_mode->height;
2211 fmt->code = MEDIA_BUS_FMT_SGRBG10_1X10;
2212 fmt->field = V4L2_FIELD_NONE;
2213 fmt->colorspace = V4L2_COLORSPACE_SRGB;
2214 fmt->ycbcr_enc = V4L2_MAP_YCBCR_ENC_DEFAULT(V4L2_COLORSPACE_SRGB);
2215 fmt->quantization = V4L2_QUANTIZATION_FULL_RANGE;
2216 fmt->xfer_func = V4L2_MAP_XFER_FUNC_DEFAULT(V4L2_COLORSPACE_SRGB);
2218 *crop = *default_mode->analog_crop;
2223 static int ov5670_enum_mbus_code(struct v4l2_subdev *sd,
2224 struct v4l2_subdev_state *sd_state,
2225 struct v4l2_subdev_mbus_code_enum *code)
2227 /* Only one bayer order GRBG is supported */
2228 if (code->index > 0)
2231 code->code = MEDIA_BUS_FMT_SGRBG10_1X10;
2236 static int ov5670_enum_frame_size(struct v4l2_subdev *sd,
2237 struct v4l2_subdev_state *sd_state,
2238 struct v4l2_subdev_frame_size_enum *fse)
2240 if (fse->index >= ARRAY_SIZE(supported_modes))
2243 if (fse->code != MEDIA_BUS_FMT_SGRBG10_1X10)
2246 fse->min_width = supported_modes[fse->index].width;
2247 fse->max_width = fse->min_width;
2248 fse->min_height = supported_modes[fse->index].height;
2249 fse->max_height = fse->min_height;
2254 static void ov5670_update_pad_format(const struct ov5670_mode *mode,
2255 struct v4l2_subdev_format *fmt)
2257 fmt->format.width = mode->width;
2258 fmt->format.height = mode->height;
2259 fmt->format.code = MEDIA_BUS_FMT_SGRBG10_1X10;
2260 fmt->format.field = V4L2_FIELD_NONE;
2263 static int ov5670_do_get_pad_format(struct ov5670 *ov5670,
2264 struct v4l2_subdev_state *sd_state,
2265 struct v4l2_subdev_format *fmt)
2267 if (fmt->which == V4L2_SUBDEV_FORMAT_TRY)
2268 fmt->format = *v4l2_subdev_get_try_format(&ov5670->sd,
2272 ov5670_update_pad_format(ov5670->cur_mode, fmt);
2277 static int ov5670_get_pad_format(struct v4l2_subdev *sd,
2278 struct v4l2_subdev_state *sd_state,
2279 struct v4l2_subdev_format *fmt)
2281 struct ov5670 *ov5670 = to_ov5670(sd);
2284 mutex_lock(&ov5670->mutex);
2285 ret = ov5670_do_get_pad_format(ov5670, sd_state, fmt);
2286 mutex_unlock(&ov5670->mutex);
2291 static int ov5670_set_pad_format(struct v4l2_subdev *sd,
2292 struct v4l2_subdev_state *sd_state,
2293 struct v4l2_subdev_format *fmt)
2295 struct ov5670 *ov5670 = to_ov5670(sd);
2296 struct v4l2_mbus_config_mipi_csi2 *bus_mipi_csi2 =
2297 &ov5670->endpoint.bus.mipi_csi2;
2298 const struct ov5670_mode *mode;
2299 unsigned int lanes_count;
2300 s64 mipi_pixel_rate;
2305 mutex_lock(&ov5670->mutex);
2307 fmt->format.code = MEDIA_BUS_FMT_SGRBG10_1X10;
2309 mode = v4l2_find_nearest_size(supported_modes,
2310 ARRAY_SIZE(supported_modes),
2312 fmt->format.width, fmt->format.height);
2313 ov5670_update_pad_format(mode, fmt);
2314 if (fmt->which == V4L2_SUBDEV_FORMAT_TRY) {
2315 *v4l2_subdev_get_try_format(sd, sd_state, fmt->pad) = fmt->format;
2317 ov5670->cur_mode = mode;
2318 __v4l2_ctrl_s_ctrl(ov5670->link_freq, mode->link_freq_index);
2320 lanes_count = bus_mipi_csi2->num_data_lanes;
2321 link_freq = link_freq_menu_items[mode->link_freq_index];
2322 /* pixel_rate = link_freq * 2 * nr_of_lanes / bits_per_sample */
2323 mipi_pixel_rate = div_s64(link_freq * 2 * lanes_count, 10);
2324 __v4l2_ctrl_s_ctrl_int64(
2327 /* Update limits and set FPS to default */
2328 vblank_def = ov5670->cur_mode->vts_def -
2329 ov5670->cur_mode->height;
2330 __v4l2_ctrl_modify_range(
2332 ov5670->cur_mode->vts_min - ov5670->cur_mode->height,
2333 OV5670_VTS_MAX - ov5670->cur_mode->height, 1,
2335 __v4l2_ctrl_s_ctrl(ov5670->vblank, vblank_def);
2336 h_blank = OV5670_FIXED_PPL - ov5670->cur_mode->width;
2337 __v4l2_ctrl_modify_range(ov5670->hblank, h_blank, h_blank, 1,
2341 mutex_unlock(&ov5670->mutex);
2346 static int ov5670_get_skip_frames(struct v4l2_subdev *sd, u32 *frames)
2348 *frames = OV5670_NUM_OF_SKIP_FRAMES;
2353 /* Verify chip ID */
2354 static int ov5670_identify_module(struct ov5670 *ov5670)
2356 struct i2c_client *client = v4l2_get_subdevdata(&ov5670->sd);
2360 if (ov5670->identified)
2363 ret = ov5670_read_reg(ov5670, OV5670_REG_CHIP_ID,
2364 OV5670_REG_VALUE_24BIT, &val);
2368 if (val != OV5670_CHIP_ID) {
2369 dev_err(&client->dev, "chip id mismatch: %x!=%x\n",
2370 OV5670_CHIP_ID, val);
2374 ov5670->identified = true;
2379 static int ov5670_mipi_configure(struct ov5670 *ov5670)
2381 struct v4l2_mbus_config_mipi_csi2 *bus_mipi_csi2 =
2382 &ov5670->endpoint.bus.mipi_csi2;
2383 unsigned int lanes_count = bus_mipi_csi2->num_data_lanes;
2385 return ov5670_write_reg(ov5670, OV5670_MIPI_SC_CTRL0_REG,
2386 OV5670_REG_VALUE_08BIT,
2387 OV5670_MIPI_SC_CTRL0_LANES(lanes_count) |
2388 OV5670_MIPI_SC_CTRL0_MIPI_EN |
2389 OV5670_MIPI_SC_CTRL0_RESERVED);
2392 /* Prepare streaming by writing default values and customized values */
2393 static int ov5670_start_streaming(struct ov5670 *ov5670)
2395 struct i2c_client *client = v4l2_get_subdevdata(&ov5670->sd);
2396 const struct ov5670_reg_list *reg_list;
2397 int link_freq_index;
2400 ret = ov5670_identify_module(ov5670);
2404 /* Get out of from software reset */
2405 ret = ov5670_write_reg(ov5670, OV5670_REG_SOFTWARE_RST,
2406 OV5670_REG_VALUE_08BIT, OV5670_SOFTWARE_RST);
2408 dev_err(&client->dev, "%s failed to set powerup registers\n",
2414 link_freq_index = ov5670->cur_mode->link_freq_index;
2415 reg_list = &link_freq_configs[link_freq_index].reg_list;
2416 ret = ov5670_write_reg_list(ov5670, reg_list);
2418 dev_err(&client->dev, "%s failed to set plls\n", __func__);
2422 /* Apply default values of current mode */
2423 reg_list = &ov5670->cur_mode->reg_list;
2424 ret = ov5670_write_reg_list(ov5670, reg_list);
2426 dev_err(&client->dev, "%s failed to set mode\n", __func__);
2430 ret = ov5670_mipi_configure(ov5670);
2432 dev_err(&client->dev, "%s failed to configure MIPI\n", __func__);
2436 ret = __v4l2_ctrl_handler_setup(ov5670->sd.ctrl_handler);
2440 /* Write stream on list */
2441 ret = ov5670_write_reg(ov5670, OV5670_REG_MODE_SELECT,
2442 OV5670_REG_VALUE_08BIT, OV5670_MODE_STREAMING);
2444 dev_err(&client->dev, "%s failed to set stream\n", __func__);
2451 static int ov5670_stop_streaming(struct ov5670 *ov5670)
2453 struct i2c_client *client = v4l2_get_subdevdata(&ov5670->sd);
2456 ret = ov5670_write_reg(ov5670, OV5670_REG_MODE_SELECT,
2457 OV5670_REG_VALUE_08BIT, OV5670_MODE_STANDBY);
2459 dev_err(&client->dev, "%s failed to set stream\n", __func__);
2461 /* Return success even if it was an error, as there is nothing the
2462 * caller can do about it.
2467 static int ov5670_set_stream(struct v4l2_subdev *sd, int enable)
2469 struct ov5670 *ov5670 = to_ov5670(sd);
2470 struct i2c_client *client = v4l2_get_subdevdata(sd);
2473 mutex_lock(&ov5670->mutex);
2474 if (ov5670->streaming == enable)
2475 goto unlock_and_return;
2478 ret = pm_runtime_resume_and_get(&client->dev);
2480 goto unlock_and_return;
2482 ret = ov5670_start_streaming(ov5670);
2486 ret = ov5670_stop_streaming(ov5670);
2487 pm_runtime_put(&client->dev);
2489 ov5670->streaming = enable;
2490 goto unlock_and_return;
2493 pm_runtime_put(&client->dev);
2496 mutex_unlock(&ov5670->mutex);
2501 static int __maybe_unused ov5670_runtime_resume(struct device *dev)
2503 struct i2c_client *client = to_i2c_client(dev);
2504 struct v4l2_subdev *sd = i2c_get_clientdata(client);
2505 struct ov5670 *ov5670 = to_ov5670(sd);
2506 unsigned long delay_us;
2509 ret = clk_prepare_enable(ov5670->xvclk);
2513 ret = regulator_bulk_enable(OV5670_NUM_SUPPLIES, ov5670->supplies);
2515 clk_disable_unprepare(ov5670->xvclk);
2519 gpiod_set_value_cansleep(ov5670->pwdn_gpio, 0);
2520 gpiod_set_value_cansleep(ov5670->reset_gpio, 0);
2522 /* 8192 * 2 clock pulses before the first SCCB transaction. */
2523 delay_us = DIV_ROUND_UP(8192 * 2 * 1000,
2524 DIV_ROUND_UP(OV5670_XVCLK_FREQ, 1000));
2530 static int __maybe_unused ov5670_runtime_suspend(struct device *dev)
2532 struct i2c_client *client = to_i2c_client(dev);
2533 struct v4l2_subdev *sd = i2c_get_clientdata(client);
2534 struct ov5670 *ov5670 = to_ov5670(sd);
2536 gpiod_set_value_cansleep(ov5670->reset_gpio, 1);
2537 gpiod_set_value_cansleep(ov5670->pwdn_gpio, 1);
2538 regulator_bulk_disable(OV5670_NUM_SUPPLIES, ov5670->supplies);
2539 clk_disable_unprepare(ov5670->xvclk);
2544 static int __maybe_unused ov5670_suspend(struct device *dev)
2546 struct v4l2_subdev *sd = dev_get_drvdata(dev);
2547 struct ov5670 *ov5670 = to_ov5670(sd);
2549 if (ov5670->streaming)
2550 ov5670_stop_streaming(ov5670);
2555 static int __maybe_unused ov5670_resume(struct device *dev)
2557 struct v4l2_subdev *sd = dev_get_drvdata(dev);
2558 struct ov5670 *ov5670 = to_ov5670(sd);
2561 if (ov5670->streaming) {
2562 ret = ov5670_start_streaming(ov5670);
2564 ov5670_stop_streaming(ov5670);
2572 static const struct v4l2_subdev_core_ops ov5670_core_ops = {
2573 .log_status = v4l2_ctrl_subdev_log_status,
2574 .subscribe_event = v4l2_ctrl_subdev_subscribe_event,
2575 .unsubscribe_event = v4l2_event_subdev_unsubscribe,
2578 static const struct v4l2_rect *
2579 __ov5670_get_pad_crop(struct ov5670 *sensor, struct v4l2_subdev_state *state,
2580 unsigned int pad, enum v4l2_subdev_format_whence which)
2582 const struct ov5670_mode *mode = sensor->cur_mode;
2585 case V4L2_SUBDEV_FORMAT_TRY:
2586 return v4l2_subdev_get_try_crop(&sensor->sd, state, pad);
2587 case V4L2_SUBDEV_FORMAT_ACTIVE:
2588 return mode->analog_crop;
2594 static int ov5670_get_selection(struct v4l2_subdev *subdev,
2595 struct v4l2_subdev_state *state,
2596 struct v4l2_subdev_selection *sel)
2598 struct ov5670 *sensor = to_ov5670(subdev);
2600 switch (sel->target) {
2601 case V4L2_SEL_TGT_CROP:
2602 mutex_lock(&sensor->mutex);
2603 sel->r = *__ov5670_get_pad_crop(sensor, state, sel->pad,
2605 mutex_unlock(&sensor->mutex);
2607 case V4L2_SEL_TGT_NATIVE_SIZE:
2608 case V4L2_SEL_TGT_CROP_BOUNDS:
2611 sel->r.width = OV5670_NATIVE_WIDTH;
2612 sel->r.height = OV5670_NATIVE_HEIGHT;
2614 case V4L2_SEL_TGT_CROP_DEFAULT:
2615 sel->r = ov5670_analog_crop;
2624 static const struct v4l2_subdev_video_ops ov5670_video_ops = {
2625 .s_stream = ov5670_set_stream,
2628 static const struct v4l2_subdev_pad_ops ov5670_pad_ops = {
2629 .init_cfg = ov5670_init_cfg,
2630 .enum_mbus_code = ov5670_enum_mbus_code,
2631 .get_fmt = ov5670_get_pad_format,
2632 .set_fmt = ov5670_set_pad_format,
2633 .enum_frame_size = ov5670_enum_frame_size,
2634 .get_selection = ov5670_get_selection,
2635 .set_selection = ov5670_get_selection,
2638 static const struct v4l2_subdev_sensor_ops ov5670_sensor_ops = {
2639 .g_skip_frames = ov5670_get_skip_frames,
2642 static const struct v4l2_subdev_ops ov5670_subdev_ops = {
2643 .core = &ov5670_core_ops,
2644 .video = &ov5670_video_ops,
2645 .pad = &ov5670_pad_ops,
2646 .sensor = &ov5670_sensor_ops,
2649 static const struct media_entity_operations ov5670_subdev_entity_ops = {
2650 .link_validate = v4l2_subdev_link_validate,
2653 static int ov5670_regulators_probe(struct ov5670 *ov5670)
2655 struct i2c_client *client = v4l2_get_subdevdata(&ov5670->sd);
2658 for (i = 0; i < OV5670_NUM_SUPPLIES; i++)
2659 ov5670->supplies[i].supply = ov5670_supply_names[i];
2661 return devm_regulator_bulk_get(&client->dev, OV5670_NUM_SUPPLIES,
2665 static int ov5670_gpio_probe(struct ov5670 *ov5670)
2667 struct i2c_client *client = v4l2_get_subdevdata(&ov5670->sd);
2669 ov5670->pwdn_gpio = devm_gpiod_get_optional(&client->dev, "powerdown",
2671 if (IS_ERR(ov5670->pwdn_gpio))
2672 return PTR_ERR(ov5670->pwdn_gpio);
2674 ov5670->reset_gpio = devm_gpiod_get_optional(&client->dev, "reset",
2676 if (IS_ERR(ov5670->reset_gpio))
2677 return PTR_ERR(ov5670->reset_gpio);
2682 static int ov5670_probe(struct i2c_client *client)
2684 struct fwnode_handle *handle;
2685 struct ov5670 *ov5670;
2690 ov5670 = devm_kzalloc(&client->dev, sizeof(*ov5670), GFP_KERNEL);
2694 ov5670->xvclk = devm_clk_get_optional(&client->dev, NULL);
2695 if (!IS_ERR_OR_NULL(ov5670->xvclk))
2696 input_clk = clk_get_rate(ov5670->xvclk);
2697 else if (!ov5670->xvclk || PTR_ERR(ov5670->xvclk) == -ENOENT)
2698 device_property_read_u32(&client->dev, "clock-frequency",
2701 return dev_err_probe(&client->dev, PTR_ERR(ov5670->xvclk),
2702 "error getting clock\n");
2704 if (input_clk != OV5670_XVCLK_FREQ) {
2705 dev_err(&client->dev,
2706 "Unsupported clock frequency %u\n", input_clk);
2710 /* Initialize subdev */
2711 v4l2_i2c_subdev_init(&ov5670->sd, client, &ov5670_subdev_ops);
2713 ret = ov5670_regulators_probe(ov5670);
2715 return dev_err_probe(&client->dev, ret, "Regulators probe failed\n");
2717 ret = ov5670_gpio_probe(ov5670);
2719 return dev_err_probe(&client->dev, ret, "GPIO probe failed\n");
2721 /* Graph Endpoint */
2722 handle = fwnode_graph_get_next_endpoint(dev_fwnode(&client->dev), NULL);
2724 return dev_err_probe(&client->dev, -ENXIO, "Endpoint for node get failed\n");
2726 ov5670->endpoint.bus_type = V4L2_MBUS_CSI2_DPHY;
2727 ov5670->endpoint.bus.mipi_csi2.num_data_lanes = 2;
2729 ret = v4l2_fwnode_endpoint_alloc_parse(handle, &ov5670->endpoint);
2730 fwnode_handle_put(handle);
2732 return dev_err_probe(&client->dev, ret, "Endpoint parse failed\n");
2734 full_power = acpi_dev_state_d0(&client->dev);
2736 ret = ov5670_runtime_resume(&client->dev);
2738 dev_err_probe(&client->dev, ret, "Power up failed\n");
2739 goto error_endpoint;
2742 /* Check module identity */
2743 ret = ov5670_identify_module(ov5670);
2745 dev_err_probe(&client->dev, ret, "ov5670_identify_module() error\n");
2746 goto error_power_off;
2750 mutex_init(&ov5670->mutex);
2752 /* Set default mode to max resolution */
2753 ov5670->cur_mode = &supported_modes[0];
2755 ret = ov5670_init_controls(ov5670);
2757 dev_err_probe(&client->dev, ret, "ov5670_init_controls() error\n");
2758 goto error_mutex_destroy;
2761 ov5670->sd.flags |= V4L2_SUBDEV_FL_HAS_DEVNODE |
2762 V4L2_SUBDEV_FL_HAS_EVENTS;
2763 ov5670->sd.entity.ops = &ov5670_subdev_entity_ops;
2764 ov5670->sd.entity.function = MEDIA_ENT_F_CAM_SENSOR;
2766 /* Source pad initialization */
2767 ov5670->pad.flags = MEDIA_PAD_FL_SOURCE;
2768 ret = media_entity_pads_init(&ov5670->sd.entity, 1, &ov5670->pad);
2770 dev_err_probe(&client->dev, ret, "media_entity_pads_init() error\n");
2771 goto error_handler_free;
2774 ov5670->streaming = false;
2776 /* Set the device's state to active if it's in D0 state. */
2778 pm_runtime_set_active(&client->dev);
2779 pm_runtime_enable(&client->dev);
2781 /* Async register for subdev */
2782 ret = v4l2_async_register_subdev_sensor(&ov5670->sd);
2784 dev_err_probe(&client->dev, ret, "v4l2_async_register_subdev() error\n");
2785 goto error_pm_disable;
2788 pm_runtime_idle(&client->dev);
2793 pm_runtime_disable(&client->dev);
2795 media_entity_cleanup(&ov5670->sd.entity);
2798 v4l2_ctrl_handler_free(ov5670->sd.ctrl_handler);
2800 error_mutex_destroy:
2801 mutex_destroy(&ov5670->mutex);
2805 ov5670_runtime_suspend(&client->dev);
2808 v4l2_fwnode_endpoint_free(&ov5670->endpoint);
2813 static void ov5670_remove(struct i2c_client *client)
2815 struct v4l2_subdev *sd = i2c_get_clientdata(client);
2816 struct ov5670 *ov5670 = to_ov5670(sd);
2818 v4l2_async_unregister_subdev(sd);
2819 media_entity_cleanup(&sd->entity);
2820 v4l2_ctrl_handler_free(sd->ctrl_handler);
2821 mutex_destroy(&ov5670->mutex);
2823 pm_runtime_disable(&client->dev);
2824 ov5670_runtime_suspend(&client->dev);
2826 v4l2_fwnode_endpoint_free(&ov5670->endpoint);
2829 static const struct dev_pm_ops ov5670_pm_ops = {
2830 SET_SYSTEM_SLEEP_PM_OPS(ov5670_suspend, ov5670_resume)
2831 SET_RUNTIME_PM_OPS(ov5670_runtime_suspend, ov5670_runtime_resume, NULL)
2835 static const struct acpi_device_id ov5670_acpi_ids[] = {
2840 MODULE_DEVICE_TABLE(acpi, ov5670_acpi_ids);
2843 static const struct of_device_id ov5670_of_ids[] = {
2844 { .compatible = "ovti,ov5670" },
2847 MODULE_DEVICE_TABLE(of, ov5670_of_ids);
2849 static struct i2c_driver ov5670_i2c_driver = {
2852 .pm = &ov5670_pm_ops,
2853 .acpi_match_table = ACPI_PTR(ov5670_acpi_ids),
2854 .of_match_table = ov5670_of_ids,
2856 .probe = ov5670_probe,
2857 .remove = ov5670_remove,
2858 .flags = I2C_DRV_ACPI_WAIVE_D0_PROBE,
2861 module_i2c_driver(ov5670_i2c_driver);
2864 MODULE_AUTHOR("Yang, Hyungwoo");
2865 MODULE_DESCRIPTION("Omnivision ov5670 sensor driver");
2866 MODULE_LICENSE("GPL v2");