2 * Copyright (c) 2017 Intel Corporation.
4 * This program is free software; you can redistribute it and/or
5 * modify it under the terms of the GNU General Public License version
6 * 2 as published by the Free Software Foundation.
8 * This program is distributed in the hope that it will be useful,
9 * but WITHOUT ANY WARRANTY; without even the implied warranty of
10 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11 * GNU General Public License for more details.
15 #include <linux/acpi.h>
16 #include <linux/i2c.h>
17 #include <linux/module.h>
18 #include <linux/pm_runtime.h>
19 #include <media/v4l2-ctrls.h>
20 #include <media/v4l2-device.h>
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 /* vertical-timings from sensor */
33 #define OV5670_REG_VTS 0x380e
34 #define OV5670_VTS_30FPS 0x0808 /* default for 30 fps */
35 #define OV5670_VTS_MAX 0xffff
37 /* horizontal-timings from sensor */
38 #define OV5670_REG_HTS 0x380c
41 * Pixels-per-line(PPL) = Time-per-line * pixel-rate
42 * In OV5670, Time-per-line = HTS/SCLK.
43 * HTS is fixed for all resolutions, not recommended to change.
45 #define OV5670_FIXED_PPL 2724 /* Pixels per line */
47 /* Exposure controls from sensor */
48 #define OV5670_REG_EXPOSURE 0x3500
49 #define OV5670_EXPOSURE_MIN 4
50 #define OV5670_EXPOSURE_STEP 1
52 /* Analog gain controls from sensor */
53 #define OV5670_REG_ANALOG_GAIN 0x3508
54 #define ANALOG_GAIN_MIN 0
55 #define ANALOG_GAIN_MAX 8191
56 #define ANALOG_GAIN_STEP 1
57 #define ANALOG_GAIN_DEFAULT 128
59 /* Digital gain controls from sensor */
60 #define OV5670_REG_R_DGTL_GAIN 0x5032
61 #define OV5670_REG_G_DGTL_GAIN 0x5034
62 #define OV5670_REG_B_DGTL_GAIN 0x5036
63 #define OV5670_DGTL_GAIN_MIN 0
64 #define OV5670_DGTL_GAIN_MAX 4095
65 #define OV5670_DGTL_GAIN_STEP 1
66 #define OV5670_DGTL_GAIN_DEFAULT 1024
68 /* Test Pattern Control */
69 #define OV5670_REG_TEST_PATTERN 0x4303
70 #define OV5670_TEST_PATTERN_ENABLE BIT(3)
71 #define OV5670_REG_TEST_PATTERN_CTRL 0x4320
73 #define OV5670_REG_VALUE_08BIT 1
74 #define OV5670_REG_VALUE_16BIT 2
75 #define OV5670_REG_VALUE_24BIT 3
77 /* Initial number of frames to skip to avoid possible garbage */
78 #define OV5670_NUM_OF_SKIP_FRAMES 2
85 struct ov5670_reg_list {
87 const struct ov5670_reg *regs;
90 struct ov5670_link_freq_config {
92 const struct ov5670_reg_list reg_list;
96 /* Frame width in pixels */
99 /* Frame height in pixels */
102 /* Default vertical timining size */
105 /* Min vertical timining size */
108 /* Link frequency needed for this resolution */
111 /* Sensor register settings for this resolution */
112 const struct ov5670_reg_list reg_list;
115 static const struct ov5670_reg mipi_data_rate_840mbps[] = {
133 static const struct ov5670_reg mode_2592x1944_regs[] = {
396 static const struct ov5670_reg mode_1296x972_regs[] = {
659 static const struct ov5670_reg mode_648x486_regs[] = {
922 static const struct ov5670_reg mode_2560x1440_regs[] = {
1184 static const struct ov5670_reg mode_1280x720_regs[] = {
1447 static const struct ov5670_reg mode_640x360_regs[] = {
1710 static const char * const ov5670_test_pattern_menu[] = {
1712 "Vertical Color Bar Type 1",
1715 /* Supported link frequencies */
1716 #define OV5670_LINK_FREQ_422MHZ 422400000
1717 #define OV5670_LINK_FREQ_422MHZ_INDEX 0
1718 static const struct ov5670_link_freq_config link_freq_configs[] = {
1720 /* pixel_rate = link_freq * 2 * nr_of_lanes / bits_per_sample */
1721 .pixel_rate = (OV5670_LINK_FREQ_422MHZ * 2 * 2) / 10,
1723 .num_of_regs = ARRAY_SIZE(mipi_data_rate_840mbps),
1724 .regs = mipi_data_rate_840mbps,
1729 static const s64 link_freq_menu_items[] = {
1730 OV5670_LINK_FREQ_422MHZ
1734 * OV5670 sensor supports following resolutions with full FOV:
1735 * 4:3 ==> {2592x1944, 1296x972, 648x486}
1736 * 16:9 ==> {2560x1440, 1280x720, 640x360}
1738 static const struct ov5670_mode supported_modes[] = {
1742 .vts_def = OV5670_VTS_30FPS,
1743 .vts_min = OV5670_VTS_30FPS,
1745 .num_of_regs = ARRAY_SIZE(mode_2592x1944_regs),
1746 .regs = mode_2592x1944_regs,
1748 .link_freq_index = OV5670_LINK_FREQ_422MHZ_INDEX,
1753 .vts_def = OV5670_VTS_30FPS,
1756 .num_of_regs = ARRAY_SIZE(mode_1296x972_regs),
1757 .regs = mode_1296x972_regs,
1759 .link_freq_index = OV5670_LINK_FREQ_422MHZ_INDEX,
1764 .vts_def = OV5670_VTS_30FPS,
1767 .num_of_regs = ARRAY_SIZE(mode_648x486_regs),
1768 .regs = mode_648x486_regs,
1770 .link_freq_index = OV5670_LINK_FREQ_422MHZ_INDEX,
1775 .vts_def = OV5670_VTS_30FPS,
1776 .vts_min = OV5670_VTS_30FPS,
1778 .num_of_regs = ARRAY_SIZE(mode_2560x1440_regs),
1779 .regs = mode_2560x1440_regs,
1781 .link_freq_index = OV5670_LINK_FREQ_422MHZ_INDEX,
1786 .vts_def = OV5670_VTS_30FPS,
1789 .num_of_regs = ARRAY_SIZE(mode_1280x720_regs),
1790 .regs = mode_1280x720_regs,
1792 .link_freq_index = OV5670_LINK_FREQ_422MHZ_INDEX,
1797 .vts_def = OV5670_VTS_30FPS,
1800 .num_of_regs = ARRAY_SIZE(mode_640x360_regs),
1801 .regs = mode_640x360_regs,
1803 .link_freq_index = OV5670_LINK_FREQ_422MHZ_INDEX,
1808 struct v4l2_subdev sd;
1809 struct media_pad pad;
1811 struct v4l2_ctrl_handler ctrl_handler;
1813 struct v4l2_ctrl *link_freq;
1814 struct v4l2_ctrl *pixel_rate;
1815 struct v4l2_ctrl *vblank;
1816 struct v4l2_ctrl *hblank;
1817 struct v4l2_ctrl *exposure;
1820 const struct ov5670_mode *cur_mode;
1822 /* To serialize asynchronus callbacks */
1825 /* Streaming on/off */
1829 #define to_ov5670(_sd) container_of(_sd, struct ov5670, sd)
1831 /* Read registers up to 4 at a time */
1832 static int ov5670_read_reg(struct ov5670 *ov5670, u16 reg, unsigned int len,
1835 struct i2c_client *client = v4l2_get_subdevdata(&ov5670->sd);
1836 struct i2c_msg msgs[2];
1839 u16 reg_addr_be = cpu_to_be16(reg);
1845 data_be_p = (u8 *)&data_be;
1846 /* Write register address */
1847 msgs[0].addr = client->addr;
1850 msgs[0].buf = (u8 *)®_addr_be;
1852 /* Read data from register */
1853 msgs[1].addr = client->addr;
1854 msgs[1].flags = I2C_M_RD;
1856 msgs[1].buf = &data_be_p[4 - len];
1858 ret = i2c_transfer(client->adapter, msgs, ARRAY_SIZE(msgs));
1859 if (ret != ARRAY_SIZE(msgs))
1862 *val = be32_to_cpu(data_be);
1867 /* Write registers up to 4 at a time */
1868 static int ov5670_write_reg(struct ov5670 *ov5670, u16 reg, unsigned int len,
1871 struct i2c_client *client = v4l2_get_subdevdata(&ov5670->sd);
1881 buf[1] = reg & 0xff;
1883 val = cpu_to_be32(val);
1889 buf[buf_i++] = val_p[val_i++];
1891 if (i2c_master_send(client, buf, len + 2) != len + 2)
1897 /* Write a list of registers */
1898 static int ov5670_write_regs(struct ov5670 *ov5670,
1899 const struct ov5670_reg *regs, unsigned int len)
1901 struct i2c_client *client = v4l2_get_subdevdata(&ov5670->sd);
1905 for (i = 0; i < len; i++) {
1906 ret = ov5670_write_reg(ov5670, regs[i].address, 1, regs[i].val);
1908 dev_err_ratelimited(
1910 "Failed to write reg 0x%4.4x. error = %d\n",
1911 regs[i].address, ret);
1920 static int ov5670_write_reg_list(struct ov5670 *ov5670,
1921 const struct ov5670_reg_list *r_list)
1923 return ov5670_write_regs(ov5670, r_list->regs, r_list->num_of_regs);
1926 /* Open sub-device */
1927 static int ov5670_open(struct v4l2_subdev *sd, struct v4l2_subdev_fh *fh)
1929 struct ov5670 *ov5670 = to_ov5670(sd);
1930 struct v4l2_mbus_framefmt *try_fmt =
1931 v4l2_subdev_get_try_format(sd, fh->pad, 0);
1933 mutex_lock(&ov5670->mutex);
1935 /* Initialize try_fmt */
1936 try_fmt->width = ov5670->cur_mode->width;
1937 try_fmt->height = ov5670->cur_mode->height;
1938 try_fmt->code = MEDIA_BUS_FMT_SGRBG10_1X10;
1939 try_fmt->field = V4L2_FIELD_NONE;
1941 /* No crop or compose */
1942 mutex_unlock(&ov5670->mutex);
1947 static int ov5670_update_digital_gain(struct ov5670 *ov5670, u32 d_gain)
1951 ret = ov5670_write_reg(ov5670, OV5670_REG_R_DGTL_GAIN,
1952 OV5670_REG_VALUE_16BIT, d_gain);
1956 ret = ov5670_write_reg(ov5670, OV5670_REG_G_DGTL_GAIN,
1957 OV5670_REG_VALUE_16BIT, d_gain);
1961 return ov5670_write_reg(ov5670, OV5670_REG_B_DGTL_GAIN,
1962 OV5670_REG_VALUE_16BIT, d_gain);
1965 static int ov5670_enable_test_pattern(struct ov5670 *ov5670, u32 pattern)
1970 /* Set the bayer order that we support */
1971 ret = ov5670_write_reg(ov5670, OV5670_REG_TEST_PATTERN_CTRL,
1972 OV5670_REG_VALUE_08BIT, 0);
1976 ret = ov5670_read_reg(ov5670, OV5670_REG_TEST_PATTERN,
1977 OV5670_REG_VALUE_08BIT, &val);
1982 val |= OV5670_TEST_PATTERN_ENABLE;
1984 val &= ~OV5670_TEST_PATTERN_ENABLE;
1986 return ov5670_write_reg(ov5670, OV5670_REG_TEST_PATTERN,
1987 OV5670_REG_VALUE_08BIT, val);
1990 /* Initialize control handlers */
1991 static int ov5670_set_ctrl(struct v4l2_ctrl *ctrl)
1993 struct ov5670 *ov5670 = container_of(ctrl->handler,
1994 struct ov5670, ctrl_handler);
1995 struct i2c_client *client = v4l2_get_subdevdata(&ov5670->sd);
1999 /* Propagate change of current control to all related controls */
2001 case V4L2_CID_VBLANK:
2002 /* Update max exposure while meeting expected vblanking */
2003 max = ov5670->cur_mode->height + ctrl->val - 8;
2004 __v4l2_ctrl_modify_range(ov5670->exposure,
2005 ov5670->exposure->minimum, max,
2006 ov5670->exposure->step, max);
2010 /* V4L2 controls values will be applied only when power is already up */
2011 if (pm_runtime_get_if_in_use(&client->dev) <= 0)
2015 case V4L2_CID_ANALOGUE_GAIN:
2016 ret = ov5670_write_reg(ov5670, OV5670_REG_ANALOG_GAIN,
2017 OV5670_REG_VALUE_16BIT, ctrl->val);
2019 case V4L2_CID_DIGITAL_GAIN:
2020 ret = ov5670_update_digital_gain(ov5670, ctrl->val);
2022 case V4L2_CID_EXPOSURE:
2023 /* 4 least significant bits of expsoure are fractional part */
2024 ret = ov5670_write_reg(ov5670, OV5670_REG_EXPOSURE,
2025 OV5670_REG_VALUE_24BIT, ctrl->val << 4);
2027 case V4L2_CID_VBLANK:
2028 /* Update VTS that meets expected vertical blanking */
2029 ret = ov5670_write_reg(ov5670, OV5670_REG_VTS,
2030 OV5670_REG_VALUE_16BIT,
2031 ov5670->cur_mode->height + ctrl->val);
2033 case V4L2_CID_TEST_PATTERN:
2034 ret = ov5670_enable_test_pattern(ov5670, ctrl->val);
2037 dev_info(&client->dev, "%s Unhandled id:0x%x, val:0x%x\n",
2038 __func__, ctrl->id, ctrl->val);
2042 pm_runtime_put(&client->dev);
2047 static const struct v4l2_ctrl_ops ov5670_ctrl_ops = {
2048 .s_ctrl = ov5670_set_ctrl,
2051 /* Initialize control handlers */
2052 static int ov5670_init_controls(struct ov5670 *ov5670)
2054 struct v4l2_ctrl_handler *ctrl_hdlr;
2061 ctrl_hdlr = &ov5670->ctrl_handler;
2062 ret = v4l2_ctrl_handler_init(ctrl_hdlr, 8);
2066 ctrl_hdlr->lock = &ov5670->mutex;
2067 ov5670->link_freq = v4l2_ctrl_new_int_menu(ctrl_hdlr,
2070 0, 0, link_freq_menu_items);
2071 if (ov5670->link_freq)
2072 ov5670->link_freq->flags |= V4L2_CTRL_FLAG_READ_ONLY;
2074 /* By default, V4L2_CID_PIXEL_RATE is read only */
2075 ov5670->pixel_rate = v4l2_ctrl_new_std(ctrl_hdlr, &ov5670_ctrl_ops,
2076 V4L2_CID_PIXEL_RATE,
2077 link_freq_configs[0].pixel_rate,
2078 link_freq_configs[0].pixel_rate,
2080 link_freq_configs[0].pixel_rate);
2082 vblank_max = OV5670_VTS_MAX - ov5670->cur_mode->height;
2083 vblank_def = ov5670->cur_mode->vts_def - ov5670->cur_mode->height;
2084 vblank_min = ov5670->cur_mode->vts_min - ov5670->cur_mode->height;
2085 ov5670->vblank = v4l2_ctrl_new_std(ctrl_hdlr, &ov5670_ctrl_ops,
2086 V4L2_CID_VBLANK, vblank_min,
2087 vblank_max, 1, vblank_def);
2089 ov5670->hblank = v4l2_ctrl_new_std(
2090 ctrl_hdlr, &ov5670_ctrl_ops, V4L2_CID_HBLANK,
2091 OV5670_FIXED_PPL - ov5670->cur_mode->width,
2092 OV5670_FIXED_PPL - ov5670->cur_mode->width, 1,
2093 OV5670_FIXED_PPL - ov5670->cur_mode->width);
2095 ov5670->hblank->flags |= V4L2_CTRL_FLAG_READ_ONLY;
2097 /* Get min, max, step, default from sensor */
2098 v4l2_ctrl_new_std(ctrl_hdlr, &ov5670_ctrl_ops, V4L2_CID_ANALOGUE_GAIN,
2099 ANALOG_GAIN_MIN, ANALOG_GAIN_MAX, ANALOG_GAIN_STEP,
2100 ANALOG_GAIN_DEFAULT);
2103 v4l2_ctrl_new_std(ctrl_hdlr, &ov5670_ctrl_ops, V4L2_CID_DIGITAL_GAIN,
2104 OV5670_DGTL_GAIN_MIN, OV5670_DGTL_GAIN_MAX,
2105 OV5670_DGTL_GAIN_STEP, OV5670_DGTL_GAIN_DEFAULT);
2107 /* Get min, max, step, default from sensor */
2108 exposure_max = ov5670->cur_mode->vts_def - 8;
2109 ov5670->exposure = v4l2_ctrl_new_std(ctrl_hdlr, &ov5670_ctrl_ops,
2111 OV5670_EXPOSURE_MIN,
2112 exposure_max, OV5670_EXPOSURE_STEP,
2115 v4l2_ctrl_new_std_menu_items(ctrl_hdlr, &ov5670_ctrl_ops,
2116 V4L2_CID_TEST_PATTERN,
2117 ARRAY_SIZE(ov5670_test_pattern_menu) - 1,
2118 0, 0, ov5670_test_pattern_menu);
2120 if (ctrl_hdlr->error) {
2121 ret = ctrl_hdlr->error;
2125 ov5670->sd.ctrl_handler = ctrl_hdlr;
2130 v4l2_ctrl_handler_free(ctrl_hdlr);
2135 static int ov5670_enum_mbus_code(struct v4l2_subdev *sd,
2136 struct v4l2_subdev_pad_config *cfg,
2137 struct v4l2_subdev_mbus_code_enum *code)
2139 /* Only one bayer order GRBG is supported */
2140 if (code->index > 0)
2143 code->code = MEDIA_BUS_FMT_SGRBG10_1X10;
2148 static int ov5670_enum_frame_size(struct v4l2_subdev *sd,
2149 struct v4l2_subdev_pad_config *cfg,
2150 struct v4l2_subdev_frame_size_enum *fse)
2152 if (fse->index >= ARRAY_SIZE(supported_modes))
2155 if (fse->code != MEDIA_BUS_FMT_SGRBG10_1X10)
2158 fse->min_width = supported_modes[fse->index].width;
2159 fse->max_width = fse->min_width;
2160 fse->min_height = supported_modes[fse->index].height;
2161 fse->max_height = fse->min_height;
2166 /* Calculate resolution distance */
2167 static int ov5670_get_reso_dist(const struct ov5670_mode *mode,
2168 struct v4l2_mbus_framefmt *framefmt)
2170 return abs(mode->width - framefmt->width) +
2171 abs(mode->height - framefmt->height);
2174 /* Find the closest supported resolution to the requested resolution */
2175 static const struct ov5670_mode *ov5670_find_best_fit(
2176 struct ov5670 *ov5670,
2177 struct v4l2_subdev_format *fmt)
2179 struct v4l2_mbus_framefmt *framefmt = &fmt->format;
2181 int cur_best_fit = 0;
2182 int cur_best_fit_dist = -1;
2185 for (i = 0; i < ARRAY_SIZE(supported_modes); i++) {
2186 dist = ov5670_get_reso_dist(&supported_modes[i], framefmt);
2187 if (cur_best_fit_dist == -1 || dist < cur_best_fit_dist) {
2188 cur_best_fit_dist = dist;
2193 return &supported_modes[cur_best_fit];
2196 static void ov5670_update_pad_format(const struct ov5670_mode *mode,
2197 struct v4l2_subdev_format *fmt)
2199 fmt->format.width = mode->width;
2200 fmt->format.height = mode->height;
2201 fmt->format.code = MEDIA_BUS_FMT_SGRBG10_1X10;
2202 fmt->format.field = V4L2_FIELD_NONE;
2205 static int ov5670_do_get_pad_format(struct ov5670 *ov5670,
2206 struct v4l2_subdev_pad_config *cfg,
2207 struct v4l2_subdev_format *fmt)
2209 if (fmt->which == V4L2_SUBDEV_FORMAT_TRY)
2210 fmt->format = *v4l2_subdev_get_try_format(&ov5670->sd, cfg,
2213 ov5670_update_pad_format(ov5670->cur_mode, fmt);
2218 static int ov5670_get_pad_format(struct v4l2_subdev *sd,
2219 struct v4l2_subdev_pad_config *cfg,
2220 struct v4l2_subdev_format *fmt)
2222 struct ov5670 *ov5670 = to_ov5670(sd);
2225 mutex_lock(&ov5670->mutex);
2226 ret = ov5670_do_get_pad_format(ov5670, cfg, fmt);
2227 mutex_unlock(&ov5670->mutex);
2232 static int ov5670_set_pad_format(struct v4l2_subdev *sd,
2233 struct v4l2_subdev_pad_config *cfg,
2234 struct v4l2_subdev_format *fmt)
2236 struct ov5670 *ov5670 = to_ov5670(sd);
2237 const struct ov5670_mode *mode;
2241 mutex_lock(&ov5670->mutex);
2243 fmt->format.code = MEDIA_BUS_FMT_SGRBG10_1X10;
2245 mode = ov5670_find_best_fit(ov5670, fmt);
2246 ov5670_update_pad_format(mode, fmt);
2247 if (fmt->which == V4L2_SUBDEV_FORMAT_TRY) {
2248 *v4l2_subdev_get_try_format(sd, cfg, fmt->pad) = fmt->format;
2250 ov5670->cur_mode = mode;
2251 __v4l2_ctrl_s_ctrl(ov5670->link_freq, mode->link_freq_index);
2252 __v4l2_ctrl_s_ctrl_int64(
2254 link_freq_configs[mode->link_freq_index].pixel_rate);
2255 /* Update limits and set FPS to default */
2256 vblank_def = ov5670->cur_mode->vts_def -
2257 ov5670->cur_mode->height;
2258 __v4l2_ctrl_modify_range(
2260 ov5670->cur_mode->vts_min - ov5670->cur_mode->height,
2261 OV5670_VTS_MAX - ov5670->cur_mode->height, 1,
2263 __v4l2_ctrl_s_ctrl(ov5670->vblank, vblank_def);
2264 h_blank = OV5670_FIXED_PPL - ov5670->cur_mode->width;
2265 __v4l2_ctrl_modify_range(ov5670->hblank, h_blank, h_blank, 1,
2269 mutex_unlock(&ov5670->mutex);
2274 static int ov5670_get_skip_frames(struct v4l2_subdev *sd, u32 *frames)
2276 *frames = OV5670_NUM_OF_SKIP_FRAMES;
2281 /* Prepare streaming by writing default values and customized values */
2282 static int ov5670_start_streaming(struct ov5670 *ov5670)
2284 struct i2c_client *client = v4l2_get_subdevdata(&ov5670->sd);
2285 const struct ov5670_reg_list *reg_list;
2286 int link_freq_index;
2289 /* Get out of from software reset */
2290 ret = ov5670_write_reg(ov5670, OV5670_REG_SOFTWARE_RST,
2291 OV5670_REG_VALUE_08BIT, OV5670_SOFTWARE_RST);
2293 dev_err(&client->dev, "%s failed to set powerup registers\n",
2299 link_freq_index = ov5670->cur_mode->link_freq_index;
2300 reg_list = &link_freq_configs[link_freq_index].reg_list;
2301 ret = ov5670_write_reg_list(ov5670, reg_list);
2303 dev_err(&client->dev, "%s failed to set plls\n", __func__);
2307 /* Apply default values of current mode */
2308 reg_list = &ov5670->cur_mode->reg_list;
2309 ret = ov5670_write_reg_list(ov5670, reg_list);
2311 dev_err(&client->dev, "%s failed to set mode\n", __func__);
2315 ret = __v4l2_ctrl_handler_setup(ov5670->sd.ctrl_handler);
2319 /* Write stream on list */
2320 ret = ov5670_write_reg(ov5670, OV5670_REG_MODE_SELECT,
2321 OV5670_REG_VALUE_08BIT, OV5670_MODE_STREAMING);
2323 dev_err(&client->dev, "%s failed to set stream\n", __func__);
2327 ov5670->streaming = true;
2332 static int ov5670_stop_streaming(struct ov5670 *ov5670)
2334 struct i2c_client *client = v4l2_get_subdevdata(&ov5670->sd);
2337 ret = ov5670_write_reg(ov5670, OV5670_REG_MODE_SELECT,
2338 OV5670_REG_VALUE_08BIT, OV5670_MODE_STANDBY);
2340 dev_err(&client->dev, "%s failed to set stream\n", __func__);
2342 ov5670->streaming = false;
2344 /* Return success even if it was an error, as there is nothing the
2345 * caller can do about it.
2350 static int ov5670_set_stream(struct v4l2_subdev *sd, int enable)
2352 struct ov5670 *ov5670 = to_ov5670(sd);
2353 struct i2c_client *client = v4l2_get_subdevdata(sd);
2356 mutex_lock(&ov5670->mutex);
2357 if (ov5670->streaming == enable)
2358 goto unlock_and_return;
2361 ret = pm_runtime_get_sync(&client->dev);
2363 pm_runtime_put_noidle(&client->dev);
2364 goto unlock_and_return;
2367 ret = ov5670_start_streaming(ov5670);
2371 ret = ov5670_stop_streaming(ov5670);
2372 pm_runtime_put(&client->dev);
2374 goto unlock_and_return;
2377 pm_runtime_put(&client->dev);
2380 mutex_unlock(&ov5670->mutex);
2385 static int __maybe_unused ov5670_suspend(struct device *dev)
2387 struct i2c_client *client = to_i2c_client(dev);
2388 struct v4l2_subdev *sd = i2c_get_clientdata(client);
2389 struct ov5670 *ov5670 = to_ov5670(sd);
2391 if (ov5670->streaming)
2392 ov5670_stop_streaming(ov5670);
2397 static int __maybe_unused ov5670_resume(struct device *dev)
2399 struct i2c_client *client = to_i2c_client(dev);
2400 struct v4l2_subdev *sd = i2c_get_clientdata(client);
2401 struct ov5670 *ov5670 = to_ov5670(sd);
2404 if (ov5670->streaming) {
2405 ret = ov5670_start_streaming(ov5670);
2407 ov5670_stop_streaming(ov5670);
2415 /* Verify chip ID */
2416 static int ov5670_identify_module(struct ov5670 *ov5670)
2418 struct i2c_client *client = v4l2_get_subdevdata(&ov5670->sd);
2422 ret = ov5670_read_reg(ov5670, OV5670_REG_CHIP_ID,
2423 OV5670_REG_VALUE_24BIT, &val);
2427 if (val != OV5670_CHIP_ID) {
2428 dev_err(&client->dev, "chip id mismatch: %x!=%x\n",
2429 OV5670_CHIP_ID, val);
2436 static const struct v4l2_subdev_video_ops ov5670_video_ops = {
2437 .s_stream = ov5670_set_stream,
2440 static const struct v4l2_subdev_pad_ops ov5670_pad_ops = {
2441 .enum_mbus_code = ov5670_enum_mbus_code,
2442 .get_fmt = ov5670_get_pad_format,
2443 .set_fmt = ov5670_set_pad_format,
2444 .enum_frame_size = ov5670_enum_frame_size,
2447 static const struct v4l2_subdev_sensor_ops ov5670_sensor_ops = {
2448 .g_skip_frames = ov5670_get_skip_frames,
2451 static const struct v4l2_subdev_ops ov5670_subdev_ops = {
2452 .video = &ov5670_video_ops,
2453 .pad = &ov5670_pad_ops,
2454 .sensor = &ov5670_sensor_ops,
2457 static const struct media_entity_operations ov5670_subdev_entity_ops = {
2458 .link_validate = v4l2_subdev_link_validate,
2461 static const struct v4l2_subdev_internal_ops ov5670_internal_ops = {
2462 .open = ov5670_open,
2465 static int ov5670_probe(struct i2c_client *client)
2467 struct ov5670 *ov5670;
2468 const char *err_msg;
2472 device_property_read_u32(&client->dev, "clock-frequency", &input_clk);
2473 if (input_clk != 19200000)
2476 ov5670 = devm_kzalloc(&client->dev, sizeof(*ov5670), GFP_KERNEL);
2479 err_msg = "devm_kzalloc() error";
2483 /* Initialize subdev */
2484 v4l2_i2c_subdev_init(&ov5670->sd, client, &ov5670_subdev_ops);
2486 /* Check module identity */
2487 ret = ov5670_identify_module(ov5670);
2489 err_msg = "ov5670_identify_module() error";
2493 mutex_init(&ov5670->mutex);
2495 /* Set default mode to max resolution */
2496 ov5670->cur_mode = &supported_modes[0];
2498 ret = ov5670_init_controls(ov5670);
2500 err_msg = "ov5670_init_controls() error";
2501 goto error_mutex_destroy;
2504 ov5670->sd.internal_ops = &ov5670_internal_ops;
2505 ov5670->sd.flags |= V4L2_SUBDEV_FL_HAS_DEVNODE;
2506 ov5670->sd.entity.ops = &ov5670_subdev_entity_ops;
2507 ov5670->sd.entity.function = MEDIA_ENT_F_CAM_SENSOR;
2509 /* Source pad initialization */
2510 ov5670->pad.flags = MEDIA_PAD_FL_SOURCE;
2511 ret = media_entity_pads_init(&ov5670->sd.entity, 1, &ov5670->pad);
2513 err_msg = "media_entity_pads_init() error";
2514 goto error_handler_free;
2517 /* Async register for subdev */
2518 ret = v4l2_async_register_subdev(&ov5670->sd);
2520 err_msg = "v4l2_async_register_subdev() error";
2521 goto error_entity_cleanup;
2524 ov5670->streaming = false;
2527 * Device is already turned on by i2c-core with ACPI domain PM.
2528 * Enable runtime PM and turn off the device.
2530 pm_runtime_get_noresume(&client->dev);
2531 pm_runtime_set_active(&client->dev);
2532 pm_runtime_enable(&client->dev);
2533 pm_runtime_put(&client->dev);
2537 error_entity_cleanup:
2538 media_entity_cleanup(&ov5670->sd.entity);
2541 v4l2_ctrl_handler_free(ov5670->sd.ctrl_handler);
2543 error_mutex_destroy:
2544 mutex_destroy(&ov5670->mutex);
2547 dev_err(&client->dev, "%s: %s %d\n", __func__, err_msg, ret);
2552 static int ov5670_remove(struct i2c_client *client)
2554 struct v4l2_subdev *sd = i2c_get_clientdata(client);
2555 struct ov5670 *ov5670 = to_ov5670(sd);
2557 v4l2_async_unregister_subdev(sd);
2558 media_entity_cleanup(&sd->entity);
2559 v4l2_ctrl_handler_free(sd->ctrl_handler);
2560 mutex_destroy(&ov5670->mutex);
2563 * Disable runtime PM but keep the device turned on.
2564 * i2c-core with ACPI domain PM will turn off the device.
2566 pm_runtime_get_sync(&client->dev);
2567 pm_runtime_disable(&client->dev);
2568 pm_runtime_set_suspended(&client->dev);
2569 pm_runtime_put_noidle(&client->dev);
2574 static const struct dev_pm_ops ov5670_pm_ops = {
2575 SET_SYSTEM_SLEEP_PM_OPS(ov5670_suspend, ov5670_resume)
2579 static const struct acpi_device_id ov5670_acpi_ids[] = {
2584 MODULE_DEVICE_TABLE(acpi, ov5670_acpi_ids);
2587 static struct i2c_driver ov5670_i2c_driver = {
2590 .pm = &ov5670_pm_ops,
2591 .acpi_match_table = ACPI_PTR(ov5670_acpi_ids),
2593 .probe_new = ov5670_probe,
2594 .remove = ov5670_remove,
2597 module_i2c_driver(ov5670_i2c_driver);
2599 MODULE_AUTHOR("Rapolu, Chiranjeevi <chiranjeevi.rapolu@intel.com>");
2600 MODULE_AUTHOR("Yang, Hyungwoo <hyungwoo.yang@intel.com>");
2601 MODULE_DESCRIPTION("Omnivision ov5670 sensor driver");
2602 MODULE_LICENSE("GPL v2");