1 // SPDX-License-Identifier: GPL-2.0
2 // Copyright (c) 2017 Intel Corporation.
4 #include <linux/acpi.h>
6 #include <linux/module.h>
7 #include <linux/pm_runtime.h>
8 #include <media/v4l2-ctrls.h>
9 #include <media/v4l2-device.h>
10 #include <media/v4l2-event.h>
11 #include <media/v4l2-fwnode.h>
13 #define OV5670_REG_CHIP_ID 0x300a
14 #define OV5670_CHIP_ID 0x005670
16 #define OV5670_REG_MODE_SELECT 0x0100
17 #define OV5670_MODE_STANDBY 0x00
18 #define OV5670_MODE_STREAMING 0x01
20 #define OV5670_REG_SOFTWARE_RST 0x0103
21 #define OV5670_SOFTWARE_RST 0x01
23 /* vertical-timings from sensor */
24 #define OV5670_REG_VTS 0x380e
25 #define OV5670_VTS_30FPS 0x0808 /* default for 30 fps */
26 #define OV5670_VTS_MAX 0xffff
28 /* horizontal-timings from sensor */
29 #define OV5670_REG_HTS 0x380c
32 * Pixels-per-line(PPL) = Time-per-line * pixel-rate
33 * In OV5670, Time-per-line = HTS/SCLK.
34 * HTS is fixed for all resolutions, not recommended to change.
36 #define OV5670_FIXED_PPL 2724 /* Pixels per line */
38 /* Exposure controls from sensor */
39 #define OV5670_REG_EXPOSURE 0x3500
40 #define OV5670_EXPOSURE_MIN 4
41 #define OV5670_EXPOSURE_STEP 1
43 /* Analog gain controls from sensor */
44 #define OV5670_REG_ANALOG_GAIN 0x3508
45 #define ANALOG_GAIN_MIN 0
46 #define ANALOG_GAIN_MAX 8191
47 #define ANALOG_GAIN_STEP 1
48 #define ANALOG_GAIN_DEFAULT 128
50 /* Digital gain controls from sensor */
51 #define OV5670_REG_R_DGTL_GAIN 0x5032
52 #define OV5670_REG_G_DGTL_GAIN 0x5034
53 #define OV5670_REG_B_DGTL_GAIN 0x5036
54 #define OV5670_DGTL_GAIN_MIN 0
55 #define OV5670_DGTL_GAIN_MAX 4095
56 #define OV5670_DGTL_GAIN_STEP 1
57 #define OV5670_DGTL_GAIN_DEFAULT 1024
59 /* Test Pattern Control */
60 #define OV5670_REG_TEST_PATTERN 0x4303
61 #define OV5670_TEST_PATTERN_ENABLE BIT(3)
62 #define OV5670_REG_TEST_PATTERN_CTRL 0x4320
64 #define OV5670_REG_VALUE_08BIT 1
65 #define OV5670_REG_VALUE_16BIT 2
66 #define OV5670_REG_VALUE_24BIT 3
68 /* Initial number of frames to skip to avoid possible garbage */
69 #define OV5670_NUM_OF_SKIP_FRAMES 2
76 struct ov5670_reg_list {
78 const struct ov5670_reg *regs;
81 struct ov5670_link_freq_config {
83 const struct ov5670_reg_list reg_list;
87 /* Frame width in pixels */
90 /* Frame height in pixels */
93 /* Default vertical timining size */
96 /* Min vertical timining size */
99 /* Link frequency needed for this resolution */
102 /* Sensor register settings for this resolution */
103 const struct ov5670_reg_list reg_list;
106 static const struct ov5670_reg mipi_data_rate_840mbps[] = {
124 static const struct ov5670_reg mode_2592x1944_regs[] = {
390 static const struct ov5670_reg mode_1296x972_regs[] = {
656 static const struct ov5670_reg mode_648x486_regs[] = {
922 static const struct ov5670_reg mode_2560x1440_regs[] = {
1187 static const struct ov5670_reg mode_1280x720_regs[] = {
1453 static const struct ov5670_reg mode_640x360_regs[] = {
1719 static const char * const ov5670_test_pattern_menu[] = {
1721 "Vertical Color Bar Type 1",
1724 /* Supported link frequencies */
1725 #define OV5670_LINK_FREQ_422MHZ 422400000
1726 #define OV5670_LINK_FREQ_422MHZ_INDEX 0
1727 static const struct ov5670_link_freq_config link_freq_configs[] = {
1729 /* pixel_rate = link_freq * 2 * nr_of_lanes / bits_per_sample */
1730 .pixel_rate = (OV5670_LINK_FREQ_422MHZ * 2 * 2) / 10,
1732 .num_of_regs = ARRAY_SIZE(mipi_data_rate_840mbps),
1733 .regs = mipi_data_rate_840mbps,
1738 static const s64 link_freq_menu_items[] = {
1739 OV5670_LINK_FREQ_422MHZ
1743 * OV5670 sensor supports following resolutions with full FOV:
1744 * 4:3 ==> {2592x1944, 1296x972, 648x486}
1745 * 16:9 ==> {2560x1440, 1280x720, 640x360}
1747 static const struct ov5670_mode supported_modes[] = {
1751 .vts_def = OV5670_VTS_30FPS,
1752 .vts_min = OV5670_VTS_30FPS,
1754 .num_of_regs = ARRAY_SIZE(mode_2592x1944_regs),
1755 .regs = mode_2592x1944_regs,
1757 .link_freq_index = OV5670_LINK_FREQ_422MHZ_INDEX,
1762 .vts_def = OV5670_VTS_30FPS,
1765 .num_of_regs = ARRAY_SIZE(mode_1296x972_regs),
1766 .regs = mode_1296x972_regs,
1768 .link_freq_index = OV5670_LINK_FREQ_422MHZ_INDEX,
1773 .vts_def = OV5670_VTS_30FPS,
1776 .num_of_regs = ARRAY_SIZE(mode_648x486_regs),
1777 .regs = mode_648x486_regs,
1779 .link_freq_index = OV5670_LINK_FREQ_422MHZ_INDEX,
1784 .vts_def = OV5670_VTS_30FPS,
1785 .vts_min = OV5670_VTS_30FPS,
1787 .num_of_regs = ARRAY_SIZE(mode_2560x1440_regs),
1788 .regs = mode_2560x1440_regs,
1790 .link_freq_index = OV5670_LINK_FREQ_422MHZ_INDEX,
1795 .vts_def = OV5670_VTS_30FPS,
1798 .num_of_regs = ARRAY_SIZE(mode_1280x720_regs),
1799 .regs = mode_1280x720_regs,
1801 .link_freq_index = OV5670_LINK_FREQ_422MHZ_INDEX,
1806 .vts_def = OV5670_VTS_30FPS,
1809 .num_of_regs = ARRAY_SIZE(mode_640x360_regs),
1810 .regs = mode_640x360_regs,
1812 .link_freq_index = OV5670_LINK_FREQ_422MHZ_INDEX,
1817 struct v4l2_subdev sd;
1818 struct media_pad pad;
1820 struct v4l2_ctrl_handler ctrl_handler;
1822 struct v4l2_ctrl *link_freq;
1823 struct v4l2_ctrl *pixel_rate;
1824 struct v4l2_ctrl *vblank;
1825 struct v4l2_ctrl *hblank;
1826 struct v4l2_ctrl *exposure;
1829 const struct ov5670_mode *cur_mode;
1831 /* To serialize asynchronus callbacks */
1834 /* Streaming on/off */
1836 /* True if the device has been identified */
1840 #define to_ov5670(_sd) container_of(_sd, struct ov5670, sd)
1842 /* Read registers up to 4 at a time */
1843 static int ov5670_read_reg(struct ov5670 *ov5670, u16 reg, unsigned int len,
1846 struct i2c_client *client = v4l2_get_subdevdata(&ov5670->sd);
1847 struct i2c_msg msgs[2];
1850 __be16 reg_addr_be = cpu_to_be16(reg);
1856 data_be_p = (u8 *)&data_be;
1857 /* Write register address */
1858 msgs[0].addr = client->addr;
1861 msgs[0].buf = (u8 *)®_addr_be;
1863 /* Read data from register */
1864 msgs[1].addr = client->addr;
1865 msgs[1].flags = I2C_M_RD;
1867 msgs[1].buf = &data_be_p[4 - len];
1869 ret = i2c_transfer(client->adapter, msgs, ARRAY_SIZE(msgs));
1870 if (ret != ARRAY_SIZE(msgs))
1873 *val = be32_to_cpu(data_be);
1878 /* Write registers up to 4 at a time */
1879 static int ov5670_write_reg(struct ov5670 *ov5670, u16 reg, unsigned int len,
1882 struct i2c_client *client = v4l2_get_subdevdata(&ov5670->sd);
1893 buf[1] = reg & 0xff;
1895 tmp = cpu_to_be32(val);
1901 buf[buf_i++] = val_p[val_i++];
1903 if (i2c_master_send(client, buf, len + 2) != len + 2)
1909 /* Write a list of registers */
1910 static int ov5670_write_regs(struct ov5670 *ov5670,
1911 const struct ov5670_reg *regs, unsigned int len)
1913 struct i2c_client *client = v4l2_get_subdevdata(&ov5670->sd);
1917 for (i = 0; i < len; i++) {
1918 ret = ov5670_write_reg(ov5670, regs[i].address, 1, regs[i].val);
1920 dev_err_ratelimited(
1922 "Failed to write reg 0x%4.4x. error = %d\n",
1923 regs[i].address, ret);
1932 static int ov5670_write_reg_list(struct ov5670 *ov5670,
1933 const struct ov5670_reg_list *r_list)
1935 return ov5670_write_regs(ov5670, r_list->regs, r_list->num_of_regs);
1938 /* Open sub-device */
1939 static int ov5670_open(struct v4l2_subdev *sd, struct v4l2_subdev_fh *fh)
1941 struct ov5670 *ov5670 = to_ov5670(sd);
1942 struct v4l2_mbus_framefmt *try_fmt =
1943 v4l2_subdev_get_try_format(sd, fh->state, 0);
1945 mutex_lock(&ov5670->mutex);
1947 /* Initialize try_fmt */
1948 try_fmt->width = ov5670->cur_mode->width;
1949 try_fmt->height = ov5670->cur_mode->height;
1950 try_fmt->code = MEDIA_BUS_FMT_SGRBG10_1X10;
1951 try_fmt->field = V4L2_FIELD_NONE;
1953 /* No crop or compose */
1954 mutex_unlock(&ov5670->mutex);
1959 static int ov5670_update_digital_gain(struct ov5670 *ov5670, u32 d_gain)
1963 ret = ov5670_write_reg(ov5670, OV5670_REG_R_DGTL_GAIN,
1964 OV5670_REG_VALUE_16BIT, d_gain);
1968 ret = ov5670_write_reg(ov5670, OV5670_REG_G_DGTL_GAIN,
1969 OV5670_REG_VALUE_16BIT, d_gain);
1973 return ov5670_write_reg(ov5670, OV5670_REG_B_DGTL_GAIN,
1974 OV5670_REG_VALUE_16BIT, d_gain);
1977 static int ov5670_enable_test_pattern(struct ov5670 *ov5670, u32 pattern)
1982 /* Set the bayer order that we support */
1983 ret = ov5670_write_reg(ov5670, OV5670_REG_TEST_PATTERN_CTRL,
1984 OV5670_REG_VALUE_08BIT, 0);
1988 ret = ov5670_read_reg(ov5670, OV5670_REG_TEST_PATTERN,
1989 OV5670_REG_VALUE_08BIT, &val);
1994 val |= OV5670_TEST_PATTERN_ENABLE;
1996 val &= ~OV5670_TEST_PATTERN_ENABLE;
1998 return ov5670_write_reg(ov5670, OV5670_REG_TEST_PATTERN,
1999 OV5670_REG_VALUE_08BIT, val);
2002 /* Initialize control handlers */
2003 static int ov5670_set_ctrl(struct v4l2_ctrl *ctrl)
2005 struct ov5670 *ov5670 = container_of(ctrl->handler,
2006 struct ov5670, ctrl_handler);
2007 struct i2c_client *client = v4l2_get_subdevdata(&ov5670->sd);
2011 /* Propagate change of current control to all related controls */
2013 case V4L2_CID_VBLANK:
2014 /* Update max exposure while meeting expected vblanking */
2015 max = ov5670->cur_mode->height + ctrl->val - 8;
2016 __v4l2_ctrl_modify_range(ov5670->exposure,
2017 ov5670->exposure->minimum, max,
2018 ov5670->exposure->step, max);
2022 /* V4L2 controls values will be applied only when power is already up */
2023 if (!pm_runtime_get_if_in_use(&client->dev))
2027 case V4L2_CID_ANALOGUE_GAIN:
2028 ret = ov5670_write_reg(ov5670, OV5670_REG_ANALOG_GAIN,
2029 OV5670_REG_VALUE_16BIT, ctrl->val);
2031 case V4L2_CID_DIGITAL_GAIN:
2032 ret = ov5670_update_digital_gain(ov5670, ctrl->val);
2034 case V4L2_CID_EXPOSURE:
2035 /* 4 least significant bits of expsoure are fractional part */
2036 ret = ov5670_write_reg(ov5670, OV5670_REG_EXPOSURE,
2037 OV5670_REG_VALUE_24BIT, ctrl->val << 4);
2039 case V4L2_CID_VBLANK:
2040 /* Update VTS that meets expected vertical blanking */
2041 ret = ov5670_write_reg(ov5670, OV5670_REG_VTS,
2042 OV5670_REG_VALUE_16BIT,
2043 ov5670->cur_mode->height + ctrl->val);
2045 case V4L2_CID_TEST_PATTERN:
2046 ret = ov5670_enable_test_pattern(ov5670, ctrl->val);
2049 dev_info(&client->dev, "%s Unhandled id:0x%x, val:0x%x\n",
2050 __func__, ctrl->id, ctrl->val);
2054 pm_runtime_put(&client->dev);
2059 static const struct v4l2_ctrl_ops ov5670_ctrl_ops = {
2060 .s_ctrl = ov5670_set_ctrl,
2063 /* Initialize control handlers */
2064 static int ov5670_init_controls(struct ov5670 *ov5670)
2066 struct i2c_client *client = v4l2_get_subdevdata(&ov5670->sd);
2067 struct v4l2_fwnode_device_properties props;
2068 struct v4l2_ctrl_handler *ctrl_hdlr;
2075 ctrl_hdlr = &ov5670->ctrl_handler;
2076 ret = v4l2_ctrl_handler_init(ctrl_hdlr, 10);
2080 ctrl_hdlr->lock = &ov5670->mutex;
2081 ov5670->link_freq = v4l2_ctrl_new_int_menu(ctrl_hdlr,
2084 0, 0, link_freq_menu_items);
2085 if (ov5670->link_freq)
2086 ov5670->link_freq->flags |= V4L2_CTRL_FLAG_READ_ONLY;
2088 /* By default, V4L2_CID_PIXEL_RATE is read only */
2089 ov5670->pixel_rate = v4l2_ctrl_new_std(ctrl_hdlr, &ov5670_ctrl_ops,
2090 V4L2_CID_PIXEL_RATE,
2091 link_freq_configs[0].pixel_rate,
2092 link_freq_configs[0].pixel_rate,
2094 link_freq_configs[0].pixel_rate);
2096 vblank_max = OV5670_VTS_MAX - ov5670->cur_mode->height;
2097 vblank_def = ov5670->cur_mode->vts_def - ov5670->cur_mode->height;
2098 vblank_min = ov5670->cur_mode->vts_min - ov5670->cur_mode->height;
2099 ov5670->vblank = v4l2_ctrl_new_std(ctrl_hdlr, &ov5670_ctrl_ops,
2100 V4L2_CID_VBLANK, vblank_min,
2101 vblank_max, 1, vblank_def);
2103 ov5670->hblank = v4l2_ctrl_new_std(
2104 ctrl_hdlr, &ov5670_ctrl_ops, V4L2_CID_HBLANK,
2105 OV5670_FIXED_PPL - ov5670->cur_mode->width,
2106 OV5670_FIXED_PPL - ov5670->cur_mode->width, 1,
2107 OV5670_FIXED_PPL - ov5670->cur_mode->width);
2109 ov5670->hblank->flags |= V4L2_CTRL_FLAG_READ_ONLY;
2111 /* Get min, max, step, default from sensor */
2112 v4l2_ctrl_new_std(ctrl_hdlr, &ov5670_ctrl_ops, V4L2_CID_ANALOGUE_GAIN,
2113 ANALOG_GAIN_MIN, ANALOG_GAIN_MAX, ANALOG_GAIN_STEP,
2114 ANALOG_GAIN_DEFAULT);
2117 v4l2_ctrl_new_std(ctrl_hdlr, &ov5670_ctrl_ops, V4L2_CID_DIGITAL_GAIN,
2118 OV5670_DGTL_GAIN_MIN, OV5670_DGTL_GAIN_MAX,
2119 OV5670_DGTL_GAIN_STEP, OV5670_DGTL_GAIN_DEFAULT);
2121 /* Get min, max, step, default from sensor */
2122 exposure_max = ov5670->cur_mode->vts_def - 8;
2123 ov5670->exposure = v4l2_ctrl_new_std(ctrl_hdlr, &ov5670_ctrl_ops,
2125 OV5670_EXPOSURE_MIN,
2126 exposure_max, OV5670_EXPOSURE_STEP,
2129 v4l2_ctrl_new_std_menu_items(ctrl_hdlr, &ov5670_ctrl_ops,
2130 V4L2_CID_TEST_PATTERN,
2131 ARRAY_SIZE(ov5670_test_pattern_menu) - 1,
2132 0, 0, ov5670_test_pattern_menu);
2134 if (ctrl_hdlr->error) {
2135 ret = ctrl_hdlr->error;
2139 ret = v4l2_fwnode_device_parse(&client->dev, &props);
2143 ret = v4l2_ctrl_new_fwnode_properties(ctrl_hdlr, &ov5670_ctrl_ops,
2148 ov5670->sd.ctrl_handler = ctrl_hdlr;
2153 v4l2_ctrl_handler_free(ctrl_hdlr);
2158 static int ov5670_enum_mbus_code(struct v4l2_subdev *sd,
2159 struct v4l2_subdev_state *sd_state,
2160 struct v4l2_subdev_mbus_code_enum *code)
2162 /* Only one bayer order GRBG is supported */
2163 if (code->index > 0)
2166 code->code = MEDIA_BUS_FMT_SGRBG10_1X10;
2171 static int ov5670_enum_frame_size(struct v4l2_subdev *sd,
2172 struct v4l2_subdev_state *sd_state,
2173 struct v4l2_subdev_frame_size_enum *fse)
2175 if (fse->index >= ARRAY_SIZE(supported_modes))
2178 if (fse->code != MEDIA_BUS_FMT_SGRBG10_1X10)
2181 fse->min_width = supported_modes[fse->index].width;
2182 fse->max_width = fse->min_width;
2183 fse->min_height = supported_modes[fse->index].height;
2184 fse->max_height = fse->min_height;
2189 static void ov5670_update_pad_format(const struct ov5670_mode *mode,
2190 struct v4l2_subdev_format *fmt)
2192 fmt->format.width = mode->width;
2193 fmt->format.height = mode->height;
2194 fmt->format.code = MEDIA_BUS_FMT_SGRBG10_1X10;
2195 fmt->format.field = V4L2_FIELD_NONE;
2198 static int ov5670_do_get_pad_format(struct ov5670 *ov5670,
2199 struct v4l2_subdev_state *sd_state,
2200 struct v4l2_subdev_format *fmt)
2202 if (fmt->which == V4L2_SUBDEV_FORMAT_TRY)
2203 fmt->format = *v4l2_subdev_get_try_format(&ov5670->sd,
2207 ov5670_update_pad_format(ov5670->cur_mode, fmt);
2212 static int ov5670_get_pad_format(struct v4l2_subdev *sd,
2213 struct v4l2_subdev_state *sd_state,
2214 struct v4l2_subdev_format *fmt)
2216 struct ov5670 *ov5670 = to_ov5670(sd);
2219 mutex_lock(&ov5670->mutex);
2220 ret = ov5670_do_get_pad_format(ov5670, sd_state, fmt);
2221 mutex_unlock(&ov5670->mutex);
2226 static int ov5670_set_pad_format(struct v4l2_subdev *sd,
2227 struct v4l2_subdev_state *sd_state,
2228 struct v4l2_subdev_format *fmt)
2230 struct ov5670 *ov5670 = to_ov5670(sd);
2231 const struct ov5670_mode *mode;
2235 mutex_lock(&ov5670->mutex);
2237 fmt->format.code = MEDIA_BUS_FMT_SGRBG10_1X10;
2239 mode = v4l2_find_nearest_size(supported_modes,
2240 ARRAY_SIZE(supported_modes),
2242 fmt->format.width, fmt->format.height);
2243 ov5670_update_pad_format(mode, fmt);
2244 if (fmt->which == V4L2_SUBDEV_FORMAT_TRY) {
2245 *v4l2_subdev_get_try_format(sd, sd_state, fmt->pad) = fmt->format;
2247 ov5670->cur_mode = mode;
2248 __v4l2_ctrl_s_ctrl(ov5670->link_freq, mode->link_freq_index);
2249 __v4l2_ctrl_s_ctrl_int64(
2251 link_freq_configs[mode->link_freq_index].pixel_rate);
2252 /* Update limits and set FPS to default */
2253 vblank_def = ov5670->cur_mode->vts_def -
2254 ov5670->cur_mode->height;
2255 __v4l2_ctrl_modify_range(
2257 ov5670->cur_mode->vts_min - ov5670->cur_mode->height,
2258 OV5670_VTS_MAX - ov5670->cur_mode->height, 1,
2260 __v4l2_ctrl_s_ctrl(ov5670->vblank, vblank_def);
2261 h_blank = OV5670_FIXED_PPL - ov5670->cur_mode->width;
2262 __v4l2_ctrl_modify_range(ov5670->hblank, h_blank, h_blank, 1,
2266 mutex_unlock(&ov5670->mutex);
2271 static int ov5670_get_skip_frames(struct v4l2_subdev *sd, u32 *frames)
2273 *frames = OV5670_NUM_OF_SKIP_FRAMES;
2278 /* Verify chip ID */
2279 static int ov5670_identify_module(struct ov5670 *ov5670)
2281 struct i2c_client *client = v4l2_get_subdevdata(&ov5670->sd);
2285 if (ov5670->identified)
2288 ret = ov5670_read_reg(ov5670, OV5670_REG_CHIP_ID,
2289 OV5670_REG_VALUE_24BIT, &val);
2293 if (val != OV5670_CHIP_ID) {
2294 dev_err(&client->dev, "chip id mismatch: %x!=%x\n",
2295 OV5670_CHIP_ID, val);
2299 ov5670->identified = true;
2304 /* Prepare streaming by writing default values and customized values */
2305 static int ov5670_start_streaming(struct ov5670 *ov5670)
2307 struct i2c_client *client = v4l2_get_subdevdata(&ov5670->sd);
2308 const struct ov5670_reg_list *reg_list;
2309 int link_freq_index;
2312 ret = ov5670_identify_module(ov5670);
2316 /* Get out of from software reset */
2317 ret = ov5670_write_reg(ov5670, OV5670_REG_SOFTWARE_RST,
2318 OV5670_REG_VALUE_08BIT, OV5670_SOFTWARE_RST);
2320 dev_err(&client->dev, "%s failed to set powerup registers\n",
2326 link_freq_index = ov5670->cur_mode->link_freq_index;
2327 reg_list = &link_freq_configs[link_freq_index].reg_list;
2328 ret = ov5670_write_reg_list(ov5670, reg_list);
2330 dev_err(&client->dev, "%s failed to set plls\n", __func__);
2334 /* Apply default values of current mode */
2335 reg_list = &ov5670->cur_mode->reg_list;
2336 ret = ov5670_write_reg_list(ov5670, reg_list);
2338 dev_err(&client->dev, "%s failed to set mode\n", __func__);
2342 ret = __v4l2_ctrl_handler_setup(ov5670->sd.ctrl_handler);
2346 /* Write stream on list */
2347 ret = ov5670_write_reg(ov5670, OV5670_REG_MODE_SELECT,
2348 OV5670_REG_VALUE_08BIT, OV5670_MODE_STREAMING);
2350 dev_err(&client->dev, "%s failed to set stream\n", __func__);
2357 static int ov5670_stop_streaming(struct ov5670 *ov5670)
2359 struct i2c_client *client = v4l2_get_subdevdata(&ov5670->sd);
2362 ret = ov5670_write_reg(ov5670, OV5670_REG_MODE_SELECT,
2363 OV5670_REG_VALUE_08BIT, OV5670_MODE_STANDBY);
2365 dev_err(&client->dev, "%s failed to set stream\n", __func__);
2367 /* Return success even if it was an error, as there is nothing the
2368 * caller can do about it.
2373 static int ov5670_set_stream(struct v4l2_subdev *sd, int enable)
2375 struct ov5670 *ov5670 = to_ov5670(sd);
2376 struct i2c_client *client = v4l2_get_subdevdata(sd);
2379 mutex_lock(&ov5670->mutex);
2380 if (ov5670->streaming == enable)
2381 goto unlock_and_return;
2384 ret = pm_runtime_resume_and_get(&client->dev);
2386 goto unlock_and_return;
2388 ret = ov5670_start_streaming(ov5670);
2392 ret = ov5670_stop_streaming(ov5670);
2393 pm_runtime_put(&client->dev);
2395 ov5670->streaming = enable;
2396 goto unlock_and_return;
2399 pm_runtime_put(&client->dev);
2402 mutex_unlock(&ov5670->mutex);
2407 static int __maybe_unused ov5670_suspend(struct device *dev)
2409 struct v4l2_subdev *sd = dev_get_drvdata(dev);
2410 struct ov5670 *ov5670 = to_ov5670(sd);
2412 if (ov5670->streaming)
2413 ov5670_stop_streaming(ov5670);
2418 static int __maybe_unused ov5670_resume(struct device *dev)
2420 struct v4l2_subdev *sd = dev_get_drvdata(dev);
2421 struct ov5670 *ov5670 = to_ov5670(sd);
2424 if (ov5670->streaming) {
2425 ret = ov5670_start_streaming(ov5670);
2427 ov5670_stop_streaming(ov5670);
2435 static const struct v4l2_subdev_core_ops ov5670_core_ops = {
2436 .log_status = v4l2_ctrl_subdev_log_status,
2437 .subscribe_event = v4l2_ctrl_subdev_subscribe_event,
2438 .unsubscribe_event = v4l2_event_subdev_unsubscribe,
2441 static const struct v4l2_subdev_video_ops ov5670_video_ops = {
2442 .s_stream = ov5670_set_stream,
2445 static const struct v4l2_subdev_pad_ops ov5670_pad_ops = {
2446 .enum_mbus_code = ov5670_enum_mbus_code,
2447 .get_fmt = ov5670_get_pad_format,
2448 .set_fmt = ov5670_set_pad_format,
2449 .enum_frame_size = ov5670_enum_frame_size,
2452 static const struct v4l2_subdev_sensor_ops ov5670_sensor_ops = {
2453 .g_skip_frames = ov5670_get_skip_frames,
2456 static const struct v4l2_subdev_ops ov5670_subdev_ops = {
2457 .core = &ov5670_core_ops,
2458 .video = &ov5670_video_ops,
2459 .pad = &ov5670_pad_ops,
2460 .sensor = &ov5670_sensor_ops,
2463 static const struct media_entity_operations ov5670_subdev_entity_ops = {
2464 .link_validate = v4l2_subdev_link_validate,
2467 static const struct v4l2_subdev_internal_ops ov5670_internal_ops = {
2468 .open = ov5670_open,
2471 static int ov5670_probe(struct i2c_client *client)
2473 struct ov5670 *ov5670;
2474 const char *err_msg;
2479 device_property_read_u32(&client->dev, "clock-frequency", &input_clk);
2480 if (input_clk != 19200000)
2483 ov5670 = devm_kzalloc(&client->dev, sizeof(*ov5670), GFP_KERNEL);
2486 err_msg = "devm_kzalloc() error";
2490 /* Initialize subdev */
2491 v4l2_i2c_subdev_init(&ov5670->sd, client, &ov5670_subdev_ops);
2493 full_power = acpi_dev_state_d0(&client->dev);
2495 /* Check module identity */
2496 ret = ov5670_identify_module(ov5670);
2498 err_msg = "ov5670_identify_module() error";
2503 mutex_init(&ov5670->mutex);
2505 /* Set default mode to max resolution */
2506 ov5670->cur_mode = &supported_modes[0];
2508 ret = ov5670_init_controls(ov5670);
2510 err_msg = "ov5670_init_controls() error";
2511 goto error_mutex_destroy;
2514 ov5670->sd.internal_ops = &ov5670_internal_ops;
2515 ov5670->sd.flags |= V4L2_SUBDEV_FL_HAS_DEVNODE |
2516 V4L2_SUBDEV_FL_HAS_EVENTS;
2517 ov5670->sd.entity.ops = &ov5670_subdev_entity_ops;
2518 ov5670->sd.entity.function = MEDIA_ENT_F_CAM_SENSOR;
2520 /* Source pad initialization */
2521 ov5670->pad.flags = MEDIA_PAD_FL_SOURCE;
2522 ret = media_entity_pads_init(&ov5670->sd.entity, 1, &ov5670->pad);
2524 err_msg = "media_entity_pads_init() error";
2525 goto error_handler_free;
2528 /* Async register for subdev */
2529 ret = v4l2_async_register_subdev_sensor(&ov5670->sd);
2531 err_msg = "v4l2_async_register_subdev() error";
2532 goto error_entity_cleanup;
2535 ov5670->streaming = false;
2537 /* Set the device's state to active if it's in D0 state. */
2539 pm_runtime_set_active(&client->dev);
2540 pm_runtime_enable(&client->dev);
2541 pm_runtime_idle(&client->dev);
2545 error_entity_cleanup:
2546 media_entity_cleanup(&ov5670->sd.entity);
2549 v4l2_ctrl_handler_free(ov5670->sd.ctrl_handler);
2551 error_mutex_destroy:
2552 mutex_destroy(&ov5670->mutex);
2555 dev_err(&client->dev, "%s: %s %d\n", __func__, err_msg, ret);
2560 static void ov5670_remove(struct i2c_client *client)
2562 struct v4l2_subdev *sd = i2c_get_clientdata(client);
2563 struct ov5670 *ov5670 = to_ov5670(sd);
2565 v4l2_async_unregister_subdev(sd);
2566 media_entity_cleanup(&sd->entity);
2567 v4l2_ctrl_handler_free(sd->ctrl_handler);
2568 mutex_destroy(&ov5670->mutex);
2570 pm_runtime_disable(&client->dev);
2573 static const struct dev_pm_ops ov5670_pm_ops = {
2574 SET_SYSTEM_SLEEP_PM_OPS(ov5670_suspend, ov5670_resume)
2578 static const struct acpi_device_id ov5670_acpi_ids[] = {
2583 MODULE_DEVICE_TABLE(acpi, ov5670_acpi_ids);
2586 static struct i2c_driver ov5670_i2c_driver = {
2589 .pm = &ov5670_pm_ops,
2590 .acpi_match_table = ACPI_PTR(ov5670_acpi_ids),
2592 .probe_new = ov5670_probe,
2593 .remove = ov5670_remove,
2594 .flags = I2C_DRV_ACPI_WAIVE_D0_PROBE,
2597 module_i2c_driver(ov5670_i2c_driver);
2599 MODULE_AUTHOR("Rapolu, Chiranjeevi <chiranjeevi.rapolu@intel.com>");
2600 MODULE_AUTHOR("Yang, Hyungwoo");
2601 MODULE_DESCRIPTION("Omnivision ov5670 sensor driver");
2602 MODULE_LICENSE("GPL v2");