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
[] = {
399 static const struct ov5670_reg mode_1296x972_regs
[] = {
665 static const struct ov5670_reg mode_648x486_regs
[] = {
931 static const struct ov5670_reg mode_2560x1440_regs
[] = {
1196 static const struct ov5670_reg mode_1280x720_regs
[] = {
1462 static const struct ov5670_reg mode_640x360_regs
[] = {
1728 static const char * const ov5670_test_pattern_menu
[] = {
1730 "Vertical Color Bar Type 1",
1733 /* Supported link frequencies */
1734 #define OV5670_LINK_FREQ_422MHZ 422400000
1735 #define OV5670_LINK_FREQ_422MHZ_INDEX 0
1736 static const struct ov5670_link_freq_config link_freq_configs
[] = {
1738 /* pixel_rate = link_freq * 2 * nr_of_lanes / bits_per_sample */
1739 .pixel_rate
= (OV5670_LINK_FREQ_422MHZ
* 2 * 2) / 10,
1741 .num_of_regs
= ARRAY_SIZE(mipi_data_rate_840mbps
),
1742 .regs
= mipi_data_rate_840mbps
,
1747 static const s64 link_freq_menu_items
[] = {
1748 OV5670_LINK_FREQ_422MHZ
1752 * OV5670 sensor supports following resolutions with full FOV:
1753 * 4:3 ==> {2592x1944, 1296x972, 648x486}
1754 * 16:9 ==> {2560x1440, 1280x720, 640x360}
1756 static const struct ov5670_mode supported_modes
[] = {
1760 .vts_def
= OV5670_VTS_30FPS
,
1761 .vts_min
= OV5670_VTS_30FPS
,
1763 .num_of_regs
= ARRAY_SIZE(mode_2592x1944_regs
),
1764 .regs
= mode_2592x1944_regs
,
1766 .link_freq_index
= OV5670_LINK_FREQ_422MHZ_INDEX
,
1771 .vts_def
= OV5670_VTS_30FPS
,
1774 .num_of_regs
= ARRAY_SIZE(mode_1296x972_regs
),
1775 .regs
= mode_1296x972_regs
,
1777 .link_freq_index
= OV5670_LINK_FREQ_422MHZ_INDEX
,
1782 .vts_def
= OV5670_VTS_30FPS
,
1785 .num_of_regs
= ARRAY_SIZE(mode_648x486_regs
),
1786 .regs
= mode_648x486_regs
,
1788 .link_freq_index
= OV5670_LINK_FREQ_422MHZ_INDEX
,
1793 .vts_def
= OV5670_VTS_30FPS
,
1794 .vts_min
= OV5670_VTS_30FPS
,
1796 .num_of_regs
= ARRAY_SIZE(mode_2560x1440_regs
),
1797 .regs
= mode_2560x1440_regs
,
1799 .link_freq_index
= OV5670_LINK_FREQ_422MHZ_INDEX
,
1804 .vts_def
= OV5670_VTS_30FPS
,
1807 .num_of_regs
= ARRAY_SIZE(mode_1280x720_regs
),
1808 .regs
= mode_1280x720_regs
,
1810 .link_freq_index
= OV5670_LINK_FREQ_422MHZ_INDEX
,
1815 .vts_def
= OV5670_VTS_30FPS
,
1818 .num_of_regs
= ARRAY_SIZE(mode_640x360_regs
),
1819 .regs
= mode_640x360_regs
,
1821 .link_freq_index
= OV5670_LINK_FREQ_422MHZ_INDEX
,
1826 struct v4l2_subdev sd
;
1827 struct media_pad pad
;
1829 struct v4l2_ctrl_handler ctrl_handler
;
1831 struct v4l2_ctrl
*link_freq
;
1832 struct v4l2_ctrl
*pixel_rate
;
1833 struct v4l2_ctrl
*vblank
;
1834 struct v4l2_ctrl
*hblank
;
1835 struct v4l2_ctrl
*exposure
;
1838 const struct ov5670_mode
*cur_mode
;
1840 /* To serialize asynchronus callbacks */
1843 /* Streaming on/off */
1847 #define to_ov5670(_sd) container_of(_sd, struct ov5670, sd)
1849 /* Read registers up to 4 at a time */
1850 static int ov5670_read_reg(struct ov5670
*ov5670
, u16 reg
, unsigned int len
,
1853 struct i2c_client
*client
= v4l2_get_subdevdata(&ov5670
->sd
);
1854 struct i2c_msg msgs
[2];
1857 u16 reg_addr_be
= cpu_to_be16(reg
);
1863 data_be_p
= (u8
*)&data_be
;
1864 /* Write register address */
1865 msgs
[0].addr
= client
->addr
;
1868 msgs
[0].buf
= (u8
*)®_addr_be
;
1870 /* Read data from register */
1871 msgs
[1].addr
= client
->addr
;
1872 msgs
[1].flags
= I2C_M_RD
;
1874 msgs
[1].buf
= &data_be_p
[4 - len
];
1876 ret
= i2c_transfer(client
->adapter
, msgs
, ARRAY_SIZE(msgs
));
1877 if (ret
!= ARRAY_SIZE(msgs
))
1880 *val
= be32_to_cpu(data_be
);
1885 /* Write registers up to 4 at a time */
1886 static int ov5670_write_reg(struct ov5670
*ov5670
, u16 reg
, unsigned int len
,
1889 struct i2c_client
*client
= v4l2_get_subdevdata(&ov5670
->sd
);
1899 buf
[1] = reg
& 0xff;
1901 val
= cpu_to_be32(val
);
1907 buf
[buf_i
++] = val_p
[val_i
++];
1909 if (i2c_master_send(client
, buf
, len
+ 2) != len
+ 2)
1915 /* Write a list of registers */
1916 static int ov5670_write_regs(struct ov5670
*ov5670
,
1917 const struct ov5670_reg
*regs
, unsigned int len
)
1919 struct i2c_client
*client
= v4l2_get_subdevdata(&ov5670
->sd
);
1923 for (i
= 0; i
< len
; i
++) {
1924 ret
= ov5670_write_reg(ov5670
, regs
[i
].address
, 1, regs
[i
].val
);
1926 dev_err_ratelimited(
1928 "Failed to write reg 0x%4.4x. error = %d\n",
1929 regs
[i
].address
, ret
);
1938 static int ov5670_write_reg_list(struct ov5670
*ov5670
,
1939 const struct ov5670_reg_list
*r_list
)
1941 return ov5670_write_regs(ov5670
, r_list
->regs
, r_list
->num_of_regs
);
1944 /* Open sub-device */
1945 static int ov5670_open(struct v4l2_subdev
*sd
, struct v4l2_subdev_fh
*fh
)
1947 struct ov5670
*ov5670
= to_ov5670(sd
);
1948 struct v4l2_mbus_framefmt
*try_fmt
=
1949 v4l2_subdev_get_try_format(sd
, fh
->pad
, 0);
1951 mutex_lock(&ov5670
->mutex
);
1953 /* Initialize try_fmt */
1954 try_fmt
->width
= ov5670
->cur_mode
->width
;
1955 try_fmt
->height
= ov5670
->cur_mode
->height
;
1956 try_fmt
->code
= MEDIA_BUS_FMT_SGRBG10_1X10
;
1957 try_fmt
->field
= V4L2_FIELD_NONE
;
1959 /* No crop or compose */
1960 mutex_unlock(&ov5670
->mutex
);
1965 static int ov5670_update_digital_gain(struct ov5670
*ov5670
, u32 d_gain
)
1969 ret
= ov5670_write_reg(ov5670
, OV5670_REG_R_DGTL_GAIN
,
1970 OV5670_REG_VALUE_16BIT
, d_gain
);
1974 ret
= ov5670_write_reg(ov5670
, OV5670_REG_G_DGTL_GAIN
,
1975 OV5670_REG_VALUE_16BIT
, d_gain
);
1979 return ov5670_write_reg(ov5670
, OV5670_REG_B_DGTL_GAIN
,
1980 OV5670_REG_VALUE_16BIT
, d_gain
);
1983 static int ov5670_enable_test_pattern(struct ov5670
*ov5670
, u32 pattern
)
1988 /* Set the bayer order that we support */
1989 ret
= ov5670_write_reg(ov5670
, OV5670_REG_TEST_PATTERN_CTRL
,
1990 OV5670_REG_VALUE_08BIT
, 0);
1994 ret
= ov5670_read_reg(ov5670
, OV5670_REG_TEST_PATTERN
,
1995 OV5670_REG_VALUE_08BIT
, &val
);
2000 val
|= OV5670_TEST_PATTERN_ENABLE
;
2002 val
&= ~OV5670_TEST_PATTERN_ENABLE
;
2004 return ov5670_write_reg(ov5670
, OV5670_REG_TEST_PATTERN
,
2005 OV5670_REG_VALUE_08BIT
, val
);
2008 /* Initialize control handlers */
2009 static int ov5670_set_ctrl(struct v4l2_ctrl
*ctrl
)
2011 struct ov5670
*ov5670
= container_of(ctrl
->handler
,
2012 struct ov5670
, ctrl_handler
);
2013 struct i2c_client
*client
= v4l2_get_subdevdata(&ov5670
->sd
);
2017 /* Propagate change of current control to all related controls */
2019 case V4L2_CID_VBLANK
:
2020 /* Update max exposure while meeting expected vblanking */
2021 max
= ov5670
->cur_mode
->height
+ ctrl
->val
- 8;
2022 __v4l2_ctrl_modify_range(ov5670
->exposure
,
2023 ov5670
->exposure
->minimum
, max
,
2024 ov5670
->exposure
->step
, max
);
2028 /* V4L2 controls values will be applied only when power is already up */
2029 if (pm_runtime_get_if_in_use(&client
->dev
) <= 0)
2033 case V4L2_CID_ANALOGUE_GAIN
:
2034 ret
= ov5670_write_reg(ov5670
, OV5670_REG_ANALOG_GAIN
,
2035 OV5670_REG_VALUE_16BIT
, ctrl
->val
);
2037 case V4L2_CID_DIGITAL_GAIN
:
2038 ret
= ov5670_update_digital_gain(ov5670
, ctrl
->val
);
2040 case V4L2_CID_EXPOSURE
:
2041 /* 4 least significant bits of expsoure are fractional part */
2042 ret
= ov5670_write_reg(ov5670
, OV5670_REG_EXPOSURE
,
2043 OV5670_REG_VALUE_24BIT
, ctrl
->val
<< 4);
2045 case V4L2_CID_VBLANK
:
2046 /* Update VTS that meets expected vertical blanking */
2047 ret
= ov5670_write_reg(ov5670
, OV5670_REG_VTS
,
2048 OV5670_REG_VALUE_16BIT
,
2049 ov5670
->cur_mode
->height
+ ctrl
->val
);
2051 case V4L2_CID_TEST_PATTERN
:
2052 ret
= ov5670_enable_test_pattern(ov5670
, ctrl
->val
);
2055 dev_info(&client
->dev
, "%s Unhandled id:0x%x, val:0x%x\n",
2056 __func__
, ctrl
->id
, ctrl
->val
);
2060 pm_runtime_put(&client
->dev
);
2065 static const struct v4l2_ctrl_ops ov5670_ctrl_ops
= {
2066 .s_ctrl
= ov5670_set_ctrl
,
2069 /* Initialize control handlers */
2070 static int ov5670_init_controls(struct ov5670
*ov5670
)
2072 struct v4l2_ctrl_handler
*ctrl_hdlr
;
2079 ctrl_hdlr
= &ov5670
->ctrl_handler
;
2080 ret
= v4l2_ctrl_handler_init(ctrl_hdlr
, 8);
2084 ctrl_hdlr
->lock
= &ov5670
->mutex
;
2085 ov5670
->link_freq
= v4l2_ctrl_new_int_menu(ctrl_hdlr
,
2088 0, 0, link_freq_menu_items
);
2089 if (ov5670
->link_freq
)
2090 ov5670
->link_freq
->flags
|= V4L2_CTRL_FLAG_READ_ONLY
;
2092 /* By default, V4L2_CID_PIXEL_RATE is read only */
2093 ov5670
->pixel_rate
= v4l2_ctrl_new_std(ctrl_hdlr
, &ov5670_ctrl_ops
,
2094 V4L2_CID_PIXEL_RATE
, 0,
2095 link_freq_configs
[0].pixel_rate
,
2097 link_freq_configs
[0].pixel_rate
);
2099 vblank_max
= OV5670_VTS_MAX
- ov5670
->cur_mode
->height
;
2100 vblank_def
= ov5670
->cur_mode
->vts_def
- ov5670
->cur_mode
->height
;
2101 vblank_min
= ov5670
->cur_mode
->vts_min
- ov5670
->cur_mode
->height
;
2102 ov5670
->vblank
= v4l2_ctrl_new_std(ctrl_hdlr
, &ov5670_ctrl_ops
,
2103 V4L2_CID_VBLANK
, vblank_min
,
2104 vblank_max
, 1, vblank_def
);
2106 ov5670
->hblank
= v4l2_ctrl_new_std(
2107 ctrl_hdlr
, &ov5670_ctrl_ops
, V4L2_CID_HBLANK
,
2108 OV5670_FIXED_PPL
- ov5670
->cur_mode
->width
,
2109 OV5670_FIXED_PPL
- ov5670
->cur_mode
->width
, 1,
2110 OV5670_FIXED_PPL
- ov5670
->cur_mode
->width
);
2112 ov5670
->hblank
->flags
|= V4L2_CTRL_FLAG_READ_ONLY
;
2114 /* Get min, max, step, default from sensor */
2115 v4l2_ctrl_new_std(ctrl_hdlr
, &ov5670_ctrl_ops
, V4L2_CID_ANALOGUE_GAIN
,
2116 ANALOG_GAIN_MIN
, ANALOG_GAIN_MAX
, ANALOG_GAIN_STEP
,
2117 ANALOG_GAIN_DEFAULT
);
2120 v4l2_ctrl_new_std(ctrl_hdlr
, &ov5670_ctrl_ops
, V4L2_CID_DIGITAL_GAIN
,
2121 OV5670_DGTL_GAIN_MIN
, OV5670_DGTL_GAIN_MAX
,
2122 OV5670_DGTL_GAIN_STEP
, OV5670_DGTL_GAIN_DEFAULT
);
2124 /* Get min, max, step, default from sensor */
2125 exposure_max
= ov5670
->cur_mode
->vts_def
- 8;
2126 ov5670
->exposure
= v4l2_ctrl_new_std(ctrl_hdlr
, &ov5670_ctrl_ops
,
2128 OV5670_EXPOSURE_MIN
,
2129 exposure_max
, OV5670_EXPOSURE_STEP
,
2132 v4l2_ctrl_new_std_menu_items(ctrl_hdlr
, &ov5670_ctrl_ops
,
2133 V4L2_CID_TEST_PATTERN
,
2134 ARRAY_SIZE(ov5670_test_pattern_menu
) - 1,
2135 0, 0, ov5670_test_pattern_menu
);
2137 if (ctrl_hdlr
->error
) {
2138 ret
= ctrl_hdlr
->error
;
2142 ov5670
->sd
.ctrl_handler
= ctrl_hdlr
;
2147 v4l2_ctrl_handler_free(ctrl_hdlr
);
2152 static int ov5670_enum_mbus_code(struct v4l2_subdev
*sd
,
2153 struct v4l2_subdev_pad_config
*cfg
,
2154 struct v4l2_subdev_mbus_code_enum
*code
)
2156 /* Only one bayer order GRBG is supported */
2157 if (code
->index
> 0)
2160 code
->code
= MEDIA_BUS_FMT_SGRBG10_1X10
;
2165 static int ov5670_enum_frame_size(struct v4l2_subdev
*sd
,
2166 struct v4l2_subdev_pad_config
*cfg
,
2167 struct v4l2_subdev_frame_size_enum
*fse
)
2169 if (fse
->index
>= ARRAY_SIZE(supported_modes
))
2172 if (fse
->code
!= MEDIA_BUS_FMT_SGRBG10_1X10
)
2175 fse
->min_width
= supported_modes
[fse
->index
].width
;
2176 fse
->max_width
= fse
->min_width
;
2177 fse
->min_height
= supported_modes
[fse
->index
].height
;
2178 fse
->max_height
= fse
->min_height
;
2183 /* Calculate resolution distance */
2184 static int ov5670_get_reso_dist(const struct ov5670_mode
*mode
,
2185 struct v4l2_mbus_framefmt
*framefmt
)
2187 return abs(mode
->width
- framefmt
->width
) +
2188 abs(mode
->height
- framefmt
->height
);
2191 /* Find the closest supported resolution to the requested resolution */
2192 static const struct ov5670_mode
*ov5670_find_best_fit(
2193 struct ov5670
*ov5670
,
2194 struct v4l2_subdev_format
*fmt
)
2196 struct v4l2_mbus_framefmt
*framefmt
= &fmt
->format
;
2198 int cur_best_fit
= 0;
2199 int cur_best_fit_dist
= -1;
2202 for (i
= 0; i
< ARRAY_SIZE(supported_modes
); i
++) {
2203 dist
= ov5670_get_reso_dist(&supported_modes
[i
], framefmt
);
2204 if (cur_best_fit_dist
== -1 || dist
< cur_best_fit_dist
) {
2205 cur_best_fit_dist
= dist
;
2210 return &supported_modes
[cur_best_fit
];
2213 static void ov5670_update_pad_format(const struct ov5670_mode
*mode
,
2214 struct v4l2_subdev_format
*fmt
)
2216 fmt
->format
.width
= mode
->width
;
2217 fmt
->format
.height
= mode
->height
;
2218 fmt
->format
.code
= MEDIA_BUS_FMT_SGRBG10_1X10
;
2219 fmt
->format
.field
= V4L2_FIELD_NONE
;
2222 static int ov5670_do_get_pad_format(struct ov5670
*ov5670
,
2223 struct v4l2_subdev_pad_config
*cfg
,
2224 struct v4l2_subdev_format
*fmt
)
2226 if (fmt
->which
== V4L2_SUBDEV_FORMAT_TRY
)
2227 fmt
->format
= *v4l2_subdev_get_try_format(&ov5670
->sd
, cfg
,
2230 ov5670_update_pad_format(ov5670
->cur_mode
, fmt
);
2235 static int ov5670_get_pad_format(struct v4l2_subdev
*sd
,
2236 struct v4l2_subdev_pad_config
*cfg
,
2237 struct v4l2_subdev_format
*fmt
)
2239 struct ov5670
*ov5670
= to_ov5670(sd
);
2242 mutex_lock(&ov5670
->mutex
);
2243 ret
= ov5670_do_get_pad_format(ov5670
, cfg
, fmt
);
2244 mutex_unlock(&ov5670
->mutex
);
2249 static int ov5670_set_pad_format(struct v4l2_subdev
*sd
,
2250 struct v4l2_subdev_pad_config
*cfg
,
2251 struct v4l2_subdev_format
*fmt
)
2253 struct ov5670
*ov5670
= to_ov5670(sd
);
2254 const struct ov5670_mode
*mode
;
2258 mutex_lock(&ov5670
->mutex
);
2260 fmt
->format
.code
= MEDIA_BUS_FMT_SGRBG10_1X10
;
2262 mode
= ov5670_find_best_fit(ov5670
, fmt
);
2263 ov5670_update_pad_format(mode
, fmt
);
2264 if (fmt
->which
== V4L2_SUBDEV_FORMAT_TRY
) {
2265 *v4l2_subdev_get_try_format(sd
, cfg
, fmt
->pad
) = fmt
->format
;
2267 ov5670
->cur_mode
= mode
;
2268 __v4l2_ctrl_s_ctrl(ov5670
->link_freq
, mode
->link_freq_index
);
2269 __v4l2_ctrl_s_ctrl_int64(
2271 link_freq_configs
[mode
->link_freq_index
].pixel_rate
);
2272 /* Update limits and set FPS to default */
2273 vblank_def
= ov5670
->cur_mode
->vts_def
-
2274 ov5670
->cur_mode
->height
;
2275 __v4l2_ctrl_modify_range(
2277 ov5670
->cur_mode
->vts_min
- ov5670
->cur_mode
->height
,
2278 OV5670_VTS_MAX
- ov5670
->cur_mode
->height
, 1,
2280 __v4l2_ctrl_s_ctrl(ov5670
->vblank
, vblank_def
);
2281 h_blank
= OV5670_FIXED_PPL
- ov5670
->cur_mode
->width
;
2282 __v4l2_ctrl_modify_range(ov5670
->hblank
, h_blank
, h_blank
, 1,
2286 mutex_unlock(&ov5670
->mutex
);
2291 static int ov5670_get_skip_frames(struct v4l2_subdev
*sd
, u32
*frames
)
2293 *frames
= OV5670_NUM_OF_SKIP_FRAMES
;
2298 /* Prepare streaming by writing default values and customized values */
2299 static int ov5670_start_streaming(struct ov5670
*ov5670
)
2301 struct i2c_client
*client
= v4l2_get_subdevdata(&ov5670
->sd
);
2302 const struct ov5670_reg_list
*reg_list
;
2303 int link_freq_index
;
2306 /* Get out of from software reset */
2307 ret
= ov5670_write_reg(ov5670
, OV5670_REG_SOFTWARE_RST
,
2308 OV5670_REG_VALUE_08BIT
, OV5670_SOFTWARE_RST
);
2310 dev_err(&client
->dev
, "%s failed to set powerup registers\n",
2316 link_freq_index
= ov5670
->cur_mode
->link_freq_index
;
2317 reg_list
= &link_freq_configs
[link_freq_index
].reg_list
;
2318 ret
= ov5670_write_reg_list(ov5670
, reg_list
);
2320 dev_err(&client
->dev
, "%s failed to set plls\n", __func__
);
2324 /* Apply default values of current mode */
2325 reg_list
= &ov5670
->cur_mode
->reg_list
;
2326 ret
= ov5670_write_reg_list(ov5670
, reg_list
);
2328 dev_err(&client
->dev
, "%s failed to set mode\n", __func__
);
2332 ret
= __v4l2_ctrl_handler_setup(ov5670
->sd
.ctrl_handler
);
2336 /* Write stream on list */
2337 ret
= ov5670_write_reg(ov5670
, OV5670_REG_MODE_SELECT
,
2338 OV5670_REG_VALUE_08BIT
, OV5670_MODE_STREAMING
);
2340 dev_err(&client
->dev
, "%s failed to set stream\n", __func__
);
2347 static int ov5670_stop_streaming(struct ov5670
*ov5670
)
2349 struct i2c_client
*client
= v4l2_get_subdevdata(&ov5670
->sd
);
2352 ret
= ov5670_write_reg(ov5670
, OV5670_REG_MODE_SELECT
,
2353 OV5670_REG_VALUE_08BIT
, OV5670_MODE_STANDBY
);
2355 dev_err(&client
->dev
, "%s failed to set stream\n", __func__
);
2357 /* Return success even if it was an error, as there is nothing the
2358 * caller can do about it.
2363 static int ov5670_set_stream(struct v4l2_subdev
*sd
, int enable
)
2365 struct ov5670
*ov5670
= to_ov5670(sd
);
2366 struct i2c_client
*client
= v4l2_get_subdevdata(sd
);
2369 mutex_lock(&ov5670
->mutex
);
2370 if (ov5670
->streaming
== enable
)
2371 goto unlock_and_return
;
2374 ret
= pm_runtime_get_sync(&client
->dev
);
2376 pm_runtime_put_noidle(&client
->dev
);
2377 goto unlock_and_return
;
2380 ret
= ov5670_start_streaming(ov5670
);
2384 ret
= ov5670_stop_streaming(ov5670
);
2385 pm_runtime_put(&client
->dev
);
2387 ov5670
->streaming
= enable
;
2388 goto unlock_and_return
;
2391 pm_runtime_put(&client
->dev
);
2394 mutex_unlock(&ov5670
->mutex
);
2399 static int __maybe_unused
ov5670_suspend(struct device
*dev
)
2401 struct i2c_client
*client
= to_i2c_client(dev
);
2402 struct v4l2_subdev
*sd
= i2c_get_clientdata(client
);
2403 struct ov5670
*ov5670
= to_ov5670(sd
);
2405 if (ov5670
->streaming
)
2406 ov5670_stop_streaming(ov5670
);
2411 static int __maybe_unused
ov5670_resume(struct device
*dev
)
2413 struct i2c_client
*client
= to_i2c_client(dev
);
2414 struct v4l2_subdev
*sd
= i2c_get_clientdata(client
);
2415 struct ov5670
*ov5670
= to_ov5670(sd
);
2418 if (ov5670
->streaming
) {
2419 ret
= ov5670_start_streaming(ov5670
);
2421 ov5670_stop_streaming(ov5670
);
2429 /* Verify chip ID */
2430 static int ov5670_identify_module(struct ov5670
*ov5670
)
2432 struct i2c_client
*client
= v4l2_get_subdevdata(&ov5670
->sd
);
2436 ret
= ov5670_read_reg(ov5670
, OV5670_REG_CHIP_ID
,
2437 OV5670_REG_VALUE_24BIT
, &val
);
2441 if (val
!= OV5670_CHIP_ID
) {
2442 dev_err(&client
->dev
, "chip id mismatch: %x!=%x\n",
2443 OV5670_CHIP_ID
, val
);
2450 static const struct v4l2_subdev_video_ops ov5670_video_ops
= {
2451 .s_stream
= ov5670_set_stream
,
2454 static const struct v4l2_subdev_pad_ops ov5670_pad_ops
= {
2455 .enum_mbus_code
= ov5670_enum_mbus_code
,
2456 .get_fmt
= ov5670_get_pad_format
,
2457 .set_fmt
= ov5670_set_pad_format
,
2458 .enum_frame_size
= ov5670_enum_frame_size
,
2461 static const struct v4l2_subdev_sensor_ops ov5670_sensor_ops
= {
2462 .g_skip_frames
= ov5670_get_skip_frames
,
2465 static const struct v4l2_subdev_ops ov5670_subdev_ops
= {
2466 .video
= &ov5670_video_ops
,
2467 .pad
= &ov5670_pad_ops
,
2468 .sensor
= &ov5670_sensor_ops
,
2471 static const struct media_entity_operations ov5670_subdev_entity_ops
= {
2472 .link_validate
= v4l2_subdev_link_validate
,
2475 static const struct v4l2_subdev_internal_ops ov5670_internal_ops
= {
2476 .open
= ov5670_open
,
2479 static int ov5670_probe(struct i2c_client
*client
)
2481 struct ov5670
*ov5670
;
2482 const char *err_msg
;
2486 device_property_read_u32(&client
->dev
, "clock-frequency", &input_clk
);
2487 if (input_clk
!= 19200000)
2490 ov5670
= devm_kzalloc(&client
->dev
, sizeof(*ov5670
), GFP_KERNEL
);
2493 err_msg
= "devm_kzalloc() error";
2497 /* Initialize subdev */
2498 v4l2_i2c_subdev_init(&ov5670
->sd
, client
, &ov5670_subdev_ops
);
2500 /* Check module identity */
2501 ret
= ov5670_identify_module(ov5670
);
2503 err_msg
= "ov5670_identify_module() error";
2507 mutex_init(&ov5670
->mutex
);
2509 /* Set default mode to max resolution */
2510 ov5670
->cur_mode
= &supported_modes
[0];
2512 ret
= ov5670_init_controls(ov5670
);
2514 err_msg
= "ov5670_init_controls() error";
2515 goto error_mutex_destroy
;
2518 ov5670
->sd
.internal_ops
= &ov5670_internal_ops
;
2519 ov5670
->sd
.flags
|= V4L2_SUBDEV_FL_HAS_DEVNODE
;
2520 ov5670
->sd
.entity
.ops
= &ov5670_subdev_entity_ops
;
2521 ov5670
->sd
.entity
.function
= MEDIA_ENT_F_CAM_SENSOR
;
2523 /* Source pad initialization */
2524 ov5670
->pad
.flags
= MEDIA_PAD_FL_SOURCE
;
2525 ret
= media_entity_pads_init(&ov5670
->sd
.entity
, 1, &ov5670
->pad
);
2527 err_msg
= "media_entity_pads_init() error";
2528 goto error_handler_free
;
2531 /* Async register for subdev */
2532 ret
= v4l2_async_register_subdev_sensor_common(&ov5670
->sd
);
2534 err_msg
= "v4l2_async_register_subdev() error";
2535 goto error_entity_cleanup
;
2538 ov5670
->streaming
= false;
2541 * Device is already turned on by i2c-core with ACPI domain PM.
2542 * Enable runtime PM and turn off the device.
2544 pm_runtime_get_noresume(&client
->dev
);
2545 pm_runtime_set_active(&client
->dev
);
2546 pm_runtime_enable(&client
->dev
);
2547 pm_runtime_put(&client
->dev
);
2551 error_entity_cleanup
:
2552 media_entity_cleanup(&ov5670
->sd
.entity
);
2555 v4l2_ctrl_handler_free(ov5670
->sd
.ctrl_handler
);
2557 error_mutex_destroy
:
2558 mutex_destroy(&ov5670
->mutex
);
2561 dev_err(&client
->dev
, "%s: %s %d\n", __func__
, err_msg
, ret
);
2566 static int ov5670_remove(struct i2c_client
*client
)
2568 struct v4l2_subdev
*sd
= i2c_get_clientdata(client
);
2569 struct ov5670
*ov5670
= to_ov5670(sd
);
2571 v4l2_async_unregister_subdev(sd
);
2572 media_entity_cleanup(&sd
->entity
);
2573 v4l2_ctrl_handler_free(sd
->ctrl_handler
);
2574 mutex_destroy(&ov5670
->mutex
);
2577 * Disable runtime PM but keep the device turned on.
2578 * i2c-core with ACPI domain PM will turn off the device.
2580 pm_runtime_get_sync(&client
->dev
);
2581 pm_runtime_disable(&client
->dev
);
2582 pm_runtime_set_suspended(&client
->dev
);
2583 pm_runtime_put_noidle(&client
->dev
);
2588 static const struct dev_pm_ops ov5670_pm_ops
= {
2589 SET_SYSTEM_SLEEP_PM_OPS(ov5670_suspend
, ov5670_resume
)
2593 static const struct acpi_device_id ov5670_acpi_ids
[] = {
2598 MODULE_DEVICE_TABLE(acpi
, ov5670_acpi_ids
);
2601 static struct i2c_driver ov5670_i2c_driver
= {
2604 .pm
= &ov5670_pm_ops
,
2605 .acpi_match_table
= ACPI_PTR(ov5670_acpi_ids
),
2607 .probe_new
= ov5670_probe
,
2608 .remove
= ov5670_remove
,
2611 module_i2c_driver(ov5670_i2c_driver
);
2613 MODULE_AUTHOR("Rapolu, Chiranjeevi <chiranjeevi.rapolu@intel.com>");
2614 MODULE_AUTHOR("Yang, Hyungwoo <hyungwoo.yang@intel.com>");
2615 MODULE_DESCRIPTION("Omnivision ov5670 sensor driver");
2616 MODULE_LICENSE("GPL v2");