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 /* 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_cfg(struct v4l2_subdev *sd,
2200 struct v4l2_subdev_state *state)
2202 struct v4l2_mbus_framefmt *fmt =
2203 v4l2_subdev_get_try_format(sd, state, 0);
2204 const struct ov5670_mode *default_mode = &supported_modes[0];
2205 struct v4l2_rect *crop = v4l2_subdev_get_try_crop(sd, 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_get_try_format(&ov5670->sd,
2270 ov5670_update_pad_format(ov5670->cur_mode, fmt);
2275 static int ov5670_get_pad_format(struct v4l2_subdev *sd,
2276 struct v4l2_subdev_state *sd_state,
2277 struct v4l2_subdev_format *fmt)
2279 struct ov5670 *ov5670 = to_ov5670(sd);
2282 mutex_lock(&ov5670->mutex);
2283 ret = ov5670_do_get_pad_format(ov5670, sd_state, fmt);
2284 mutex_unlock(&ov5670->mutex);
2289 static int ov5670_set_pad_format(struct v4l2_subdev *sd,
2290 struct v4l2_subdev_state *sd_state,
2291 struct v4l2_subdev_format *fmt)
2293 struct ov5670 *ov5670 = to_ov5670(sd);
2294 struct v4l2_mbus_config_mipi_csi2 *bus_mipi_csi2 =
2295 &ov5670->endpoint.bus.mipi_csi2;
2296 const struct ov5670_mode *mode;
2297 unsigned int lanes_count;
2298 s64 mipi_pixel_rate;
2303 mutex_lock(&ov5670->mutex);
2305 fmt->format.code = MEDIA_BUS_FMT_SGRBG10_1X10;
2307 mode = v4l2_find_nearest_size(supported_modes,
2308 ARRAY_SIZE(supported_modes),
2310 fmt->format.width, fmt->format.height);
2311 ov5670_update_pad_format(mode, fmt);
2312 if (fmt->which == V4L2_SUBDEV_FORMAT_TRY) {
2313 *v4l2_subdev_get_try_format(sd, sd_state, fmt->pad) = fmt->format;
2315 ov5670->cur_mode = mode;
2316 __v4l2_ctrl_s_ctrl(ov5670->link_freq, mode->link_freq_index);
2318 lanes_count = bus_mipi_csi2->num_data_lanes;
2319 link_freq = link_freq_menu_items[mode->link_freq_index];
2320 /* pixel_rate = link_freq * 2 * nr_of_lanes / bits_per_sample */
2321 mipi_pixel_rate = div_s64(link_freq * 2 * lanes_count, 10);
2322 __v4l2_ctrl_s_ctrl_int64(
2325 /* Update limits and set FPS to default */
2326 vblank_def = ov5670->cur_mode->vts_def -
2327 ov5670->cur_mode->height;
2328 __v4l2_ctrl_modify_range(
2330 ov5670->cur_mode->vts_min - ov5670->cur_mode->height,
2331 OV5670_VTS_MAX - ov5670->cur_mode->height, 1,
2333 __v4l2_ctrl_s_ctrl(ov5670->vblank, vblank_def);
2334 h_blank = OV5670_FIXED_PPL - ov5670->cur_mode->width;
2335 __v4l2_ctrl_modify_range(ov5670->hblank, h_blank, h_blank, 1,
2339 mutex_unlock(&ov5670->mutex);
2344 static int ov5670_get_skip_frames(struct v4l2_subdev *sd, u32 *frames)
2346 *frames = OV5670_NUM_OF_SKIP_FRAMES;
2351 /* Verify chip ID */
2352 static int ov5670_identify_module(struct ov5670 *ov5670)
2354 struct i2c_client *client = v4l2_get_subdevdata(&ov5670->sd);
2358 if (ov5670->identified)
2361 ret = ov5670_read_reg(ov5670, OV5670_REG_CHIP_ID,
2362 OV5670_REG_VALUE_24BIT, &val);
2366 if (val != OV5670_CHIP_ID) {
2367 dev_err(&client->dev, "chip id mismatch: %x!=%x\n",
2368 OV5670_CHIP_ID, val);
2372 ov5670->identified = true;
2377 static int ov5670_mipi_configure(struct ov5670 *ov5670)
2379 struct v4l2_mbus_config_mipi_csi2 *bus_mipi_csi2 =
2380 &ov5670->endpoint.bus.mipi_csi2;
2381 unsigned int lanes_count = bus_mipi_csi2->num_data_lanes;
2383 return ov5670_write_reg(ov5670, OV5670_MIPI_SC_CTRL0_REG,
2384 OV5670_REG_VALUE_08BIT,
2385 OV5670_MIPI_SC_CTRL0_LANES(lanes_count) |
2386 OV5670_MIPI_SC_CTRL0_MIPI_EN |
2387 OV5670_MIPI_SC_CTRL0_RESERVED);
2390 /* Prepare streaming by writing default values and customized values */
2391 static int ov5670_start_streaming(struct ov5670 *ov5670)
2393 struct i2c_client *client = v4l2_get_subdevdata(&ov5670->sd);
2394 const struct ov5670_reg_list *reg_list;
2395 int link_freq_index;
2398 ret = ov5670_identify_module(ov5670);
2402 /* Get out of from software reset */
2403 ret = ov5670_write_reg(ov5670, OV5670_REG_SOFTWARE_RST,
2404 OV5670_REG_VALUE_08BIT, OV5670_SOFTWARE_RST);
2406 dev_err(&client->dev, "%s failed to set powerup registers\n",
2412 link_freq_index = ov5670->cur_mode->link_freq_index;
2413 reg_list = &link_freq_configs[link_freq_index].reg_list;
2414 ret = ov5670_write_reg_list(ov5670, reg_list);
2416 dev_err(&client->dev, "%s failed to set plls\n", __func__);
2420 /* Apply default values of current mode */
2421 reg_list = &ov5670->cur_mode->reg_list;
2422 ret = ov5670_write_reg_list(ov5670, reg_list);
2424 dev_err(&client->dev, "%s failed to set mode\n", __func__);
2428 ret = ov5670_mipi_configure(ov5670);
2430 dev_err(&client->dev, "%s failed to configure MIPI\n", __func__);
2434 ret = __v4l2_ctrl_handler_setup(ov5670->sd.ctrl_handler);
2438 /* Write stream on list */
2439 ret = ov5670_write_reg(ov5670, OV5670_REG_MODE_SELECT,
2440 OV5670_REG_VALUE_08BIT, OV5670_MODE_STREAMING);
2442 dev_err(&client->dev, "%s failed to set stream\n", __func__);
2449 static int ov5670_stop_streaming(struct ov5670 *ov5670)
2451 struct i2c_client *client = v4l2_get_subdevdata(&ov5670->sd);
2454 ret = ov5670_write_reg(ov5670, OV5670_REG_MODE_SELECT,
2455 OV5670_REG_VALUE_08BIT, OV5670_MODE_STANDBY);
2457 dev_err(&client->dev, "%s failed to set stream\n", __func__);
2459 /* Return success even if it was an error, as there is nothing the
2460 * caller can do about it.
2465 static int ov5670_set_stream(struct v4l2_subdev *sd, int enable)
2467 struct ov5670 *ov5670 = to_ov5670(sd);
2468 struct i2c_client *client = v4l2_get_subdevdata(sd);
2471 mutex_lock(&ov5670->mutex);
2474 ret = pm_runtime_resume_and_get(&client->dev);
2476 goto unlock_and_return;
2478 ret = ov5670_start_streaming(ov5670);
2482 ret = ov5670_stop_streaming(ov5670);
2483 pm_runtime_put(&client->dev);
2485 goto unlock_and_return;
2488 pm_runtime_put(&client->dev);
2491 mutex_unlock(&ov5670->mutex);
2496 static int __maybe_unused ov5670_runtime_resume(struct device *dev)
2498 struct i2c_client *client = to_i2c_client(dev);
2499 struct v4l2_subdev *sd = i2c_get_clientdata(client);
2500 struct ov5670 *ov5670 = to_ov5670(sd);
2501 unsigned long delay_us;
2504 ret = clk_prepare_enable(ov5670->xvclk);
2508 ret = regulator_bulk_enable(OV5670_NUM_SUPPLIES, ov5670->supplies);
2510 clk_disable_unprepare(ov5670->xvclk);
2514 gpiod_set_value_cansleep(ov5670->pwdn_gpio, 0);
2515 gpiod_set_value_cansleep(ov5670->reset_gpio, 0);
2517 /* 8192 * 2 clock pulses before the first SCCB transaction. */
2518 delay_us = DIV_ROUND_UP(8192 * 2 * 1000,
2519 DIV_ROUND_UP(OV5670_XVCLK_FREQ, 1000));
2525 static int __maybe_unused ov5670_runtime_suspend(struct device *dev)
2527 struct i2c_client *client = to_i2c_client(dev);
2528 struct v4l2_subdev *sd = i2c_get_clientdata(client);
2529 struct ov5670 *ov5670 = to_ov5670(sd);
2531 gpiod_set_value_cansleep(ov5670->reset_gpio, 1);
2532 gpiod_set_value_cansleep(ov5670->pwdn_gpio, 1);
2533 regulator_bulk_disable(OV5670_NUM_SUPPLIES, ov5670->supplies);
2534 clk_disable_unprepare(ov5670->xvclk);
2539 static const struct v4l2_subdev_core_ops ov5670_core_ops = {
2540 .log_status = v4l2_ctrl_subdev_log_status,
2541 .subscribe_event = v4l2_ctrl_subdev_subscribe_event,
2542 .unsubscribe_event = v4l2_event_subdev_unsubscribe,
2545 static const struct v4l2_rect *
2546 __ov5670_get_pad_crop(struct ov5670 *sensor, struct v4l2_subdev_state *state,
2547 unsigned int pad, enum v4l2_subdev_format_whence which)
2549 const struct ov5670_mode *mode = sensor->cur_mode;
2552 case V4L2_SUBDEV_FORMAT_TRY:
2553 return v4l2_subdev_get_try_crop(&sensor->sd, state, pad);
2554 case V4L2_SUBDEV_FORMAT_ACTIVE:
2555 return mode->analog_crop;
2561 static int ov5670_get_selection(struct v4l2_subdev *subdev,
2562 struct v4l2_subdev_state *state,
2563 struct v4l2_subdev_selection *sel)
2565 struct ov5670 *sensor = to_ov5670(subdev);
2567 switch (sel->target) {
2568 case V4L2_SEL_TGT_CROP:
2569 mutex_lock(&sensor->mutex);
2570 sel->r = *__ov5670_get_pad_crop(sensor, state, sel->pad,
2572 mutex_unlock(&sensor->mutex);
2574 case V4L2_SEL_TGT_NATIVE_SIZE:
2575 case V4L2_SEL_TGT_CROP_BOUNDS:
2578 sel->r.width = OV5670_NATIVE_WIDTH;
2579 sel->r.height = OV5670_NATIVE_HEIGHT;
2581 case V4L2_SEL_TGT_CROP_DEFAULT:
2582 sel->r = ov5670_analog_crop;
2591 static const struct v4l2_subdev_video_ops ov5670_video_ops = {
2592 .s_stream = ov5670_set_stream,
2595 static const struct v4l2_subdev_pad_ops ov5670_pad_ops = {
2596 .init_cfg = ov5670_init_cfg,
2597 .enum_mbus_code = ov5670_enum_mbus_code,
2598 .get_fmt = ov5670_get_pad_format,
2599 .set_fmt = ov5670_set_pad_format,
2600 .enum_frame_size = ov5670_enum_frame_size,
2601 .get_selection = ov5670_get_selection,
2602 .set_selection = ov5670_get_selection,
2605 static const struct v4l2_subdev_sensor_ops ov5670_sensor_ops = {
2606 .g_skip_frames = ov5670_get_skip_frames,
2609 static const struct v4l2_subdev_ops ov5670_subdev_ops = {
2610 .core = &ov5670_core_ops,
2611 .video = &ov5670_video_ops,
2612 .pad = &ov5670_pad_ops,
2613 .sensor = &ov5670_sensor_ops,
2616 static const struct media_entity_operations ov5670_subdev_entity_ops = {
2617 .link_validate = v4l2_subdev_link_validate,
2620 static int ov5670_regulators_probe(struct ov5670 *ov5670)
2622 struct i2c_client *client = v4l2_get_subdevdata(&ov5670->sd);
2625 for (i = 0; i < OV5670_NUM_SUPPLIES; i++)
2626 ov5670->supplies[i].supply = ov5670_supply_names[i];
2628 return devm_regulator_bulk_get(&client->dev, OV5670_NUM_SUPPLIES,
2632 static int ov5670_gpio_probe(struct ov5670 *ov5670)
2634 struct i2c_client *client = v4l2_get_subdevdata(&ov5670->sd);
2636 ov5670->pwdn_gpio = devm_gpiod_get_optional(&client->dev, "powerdown",
2638 if (IS_ERR(ov5670->pwdn_gpio))
2639 return PTR_ERR(ov5670->pwdn_gpio);
2641 ov5670->reset_gpio = devm_gpiod_get_optional(&client->dev, "reset",
2643 if (IS_ERR(ov5670->reset_gpio))
2644 return PTR_ERR(ov5670->reset_gpio);
2649 static int ov5670_probe(struct i2c_client *client)
2651 struct fwnode_handle *handle;
2652 struct ov5670 *ov5670;
2657 ov5670 = devm_kzalloc(&client->dev, sizeof(*ov5670), GFP_KERNEL);
2661 ov5670->xvclk = devm_clk_get_optional(&client->dev, NULL);
2662 if (!IS_ERR_OR_NULL(ov5670->xvclk))
2663 input_clk = clk_get_rate(ov5670->xvclk);
2664 else if (!ov5670->xvclk || PTR_ERR(ov5670->xvclk) == -ENOENT)
2665 device_property_read_u32(&client->dev, "clock-frequency",
2668 return dev_err_probe(&client->dev, PTR_ERR(ov5670->xvclk),
2669 "error getting clock\n");
2671 if (input_clk != OV5670_XVCLK_FREQ) {
2672 dev_err(&client->dev,
2673 "Unsupported clock frequency %u\n", input_clk);
2677 /* Initialize subdev */
2678 v4l2_i2c_subdev_init(&ov5670->sd, client, &ov5670_subdev_ops);
2680 ret = ov5670_regulators_probe(ov5670);
2682 return dev_err_probe(&client->dev, ret, "Regulators probe failed\n");
2684 ret = ov5670_gpio_probe(ov5670);
2686 return dev_err_probe(&client->dev, ret, "GPIO probe failed\n");
2688 /* Graph Endpoint */
2689 handle = fwnode_graph_get_next_endpoint(dev_fwnode(&client->dev), NULL);
2691 return dev_err_probe(&client->dev, -ENXIO, "Endpoint for node get failed\n");
2693 ov5670->endpoint.bus_type = V4L2_MBUS_CSI2_DPHY;
2694 ov5670->endpoint.bus.mipi_csi2.num_data_lanes = 2;
2696 ret = v4l2_fwnode_endpoint_alloc_parse(handle, &ov5670->endpoint);
2697 fwnode_handle_put(handle);
2699 return dev_err_probe(&client->dev, ret, "Endpoint parse failed\n");
2701 full_power = acpi_dev_state_d0(&client->dev);
2703 ret = ov5670_runtime_resume(&client->dev);
2705 dev_err_probe(&client->dev, ret, "Power up failed\n");
2706 goto error_endpoint;
2709 /* Check module identity */
2710 ret = ov5670_identify_module(ov5670);
2712 dev_err_probe(&client->dev, ret, "ov5670_identify_module() error\n");
2713 goto error_power_off;
2717 mutex_init(&ov5670->mutex);
2719 /* Set default mode to max resolution */
2720 ov5670->cur_mode = &supported_modes[0];
2722 ret = ov5670_init_controls(ov5670);
2724 dev_err_probe(&client->dev, ret, "ov5670_init_controls() error\n");
2725 goto error_mutex_destroy;
2728 ov5670->sd.flags |= V4L2_SUBDEV_FL_HAS_DEVNODE |
2729 V4L2_SUBDEV_FL_HAS_EVENTS;
2730 ov5670->sd.entity.ops = &ov5670_subdev_entity_ops;
2731 ov5670->sd.entity.function = MEDIA_ENT_F_CAM_SENSOR;
2733 /* Source pad initialization */
2734 ov5670->pad.flags = MEDIA_PAD_FL_SOURCE;
2735 ret = media_entity_pads_init(&ov5670->sd.entity, 1, &ov5670->pad);
2737 dev_err_probe(&client->dev, ret, "media_entity_pads_init() error\n");
2738 goto error_handler_free;
2741 /* Set the device's state to active if it's in D0 state. */
2743 pm_runtime_set_active(&client->dev);
2744 pm_runtime_enable(&client->dev);
2746 /* Async register for subdev */
2747 ret = v4l2_async_register_subdev_sensor(&ov5670->sd);
2749 dev_err_probe(&client->dev, ret, "v4l2_async_register_subdev() error\n");
2750 goto error_pm_disable;
2753 pm_runtime_idle(&client->dev);
2758 pm_runtime_disable(&client->dev);
2760 media_entity_cleanup(&ov5670->sd.entity);
2763 v4l2_ctrl_handler_free(ov5670->sd.ctrl_handler);
2765 error_mutex_destroy:
2766 mutex_destroy(&ov5670->mutex);
2770 ov5670_runtime_suspend(&client->dev);
2773 v4l2_fwnode_endpoint_free(&ov5670->endpoint);
2778 static void ov5670_remove(struct i2c_client *client)
2780 struct v4l2_subdev *sd = i2c_get_clientdata(client);
2781 struct ov5670 *ov5670 = to_ov5670(sd);
2783 v4l2_async_unregister_subdev(sd);
2784 media_entity_cleanup(&sd->entity);
2785 v4l2_ctrl_handler_free(sd->ctrl_handler);
2786 mutex_destroy(&ov5670->mutex);
2788 pm_runtime_disable(&client->dev);
2789 ov5670_runtime_suspend(&client->dev);
2791 v4l2_fwnode_endpoint_free(&ov5670->endpoint);
2794 static const struct dev_pm_ops ov5670_pm_ops = {
2795 SET_RUNTIME_PM_OPS(ov5670_runtime_suspend, ov5670_runtime_resume, NULL)
2799 static const struct acpi_device_id ov5670_acpi_ids[] = {
2804 MODULE_DEVICE_TABLE(acpi, ov5670_acpi_ids);
2807 static const struct of_device_id ov5670_of_ids[] = {
2808 { .compatible = "ovti,ov5670" },
2811 MODULE_DEVICE_TABLE(of, ov5670_of_ids);
2813 static struct i2c_driver ov5670_i2c_driver = {
2816 .pm = &ov5670_pm_ops,
2817 .acpi_match_table = ACPI_PTR(ov5670_acpi_ids),
2818 .of_match_table = ov5670_of_ids,
2820 .probe = ov5670_probe,
2821 .remove = ov5670_remove,
2822 .flags = I2C_DRV_ACPI_WAIVE_D0_PROBE,
2825 module_i2c_driver(ov5670_i2c_driver);
2827 MODULE_AUTHOR("Rapolu, Chiranjeevi");
2828 MODULE_AUTHOR("Yang, Hyungwoo");
2829 MODULE_DESCRIPTION("Omnivision ov5670 sensor driver");
2830 MODULE_LICENSE("GPL v2");