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>
11 #define OV5670_REG_CHIP_ID 0x300a
12 #define OV5670_CHIP_ID 0x005670
14 #define OV5670_REG_MODE_SELECT 0x0100
15 #define OV5670_MODE_STANDBY 0x00
16 #define OV5670_MODE_STREAMING 0x01
18 #define OV5670_REG_SOFTWARE_RST 0x0103
19 #define OV5670_SOFTWARE_RST 0x01
21 /* vertical-timings from sensor */
22 #define OV5670_REG_VTS 0x380e
23 #define OV5670_VTS_30FPS 0x0808 /* default for 30 fps */
24 #define OV5670_VTS_MAX 0xffff
26 /* horizontal-timings from sensor */
27 #define OV5670_REG_HTS 0x380c
30 * Pixels-per-line(PPL) = Time-per-line * pixel-rate
31 * In OV5670, Time-per-line = HTS/SCLK.
32 * HTS is fixed for all resolutions, not recommended to change.
34 #define OV5670_FIXED_PPL 2724 /* Pixels per line */
36 /* Exposure controls from sensor */
37 #define OV5670_REG_EXPOSURE 0x3500
38 #define OV5670_EXPOSURE_MIN 4
39 #define OV5670_EXPOSURE_STEP 1
41 /* Analog gain controls from sensor */
42 #define OV5670_REG_ANALOG_GAIN 0x3508
43 #define ANALOG_GAIN_MIN 0
44 #define ANALOG_GAIN_MAX 8191
45 #define ANALOG_GAIN_STEP 1
46 #define ANALOG_GAIN_DEFAULT 128
48 /* Digital gain controls from sensor */
49 #define OV5670_REG_R_DGTL_GAIN 0x5032
50 #define OV5670_REG_G_DGTL_GAIN 0x5034
51 #define OV5670_REG_B_DGTL_GAIN 0x5036
52 #define OV5670_DGTL_GAIN_MIN 0
53 #define OV5670_DGTL_GAIN_MAX 4095
54 #define OV5670_DGTL_GAIN_STEP 1
55 #define OV5670_DGTL_GAIN_DEFAULT 1024
57 /* Test Pattern Control */
58 #define OV5670_REG_TEST_PATTERN 0x4303
59 #define OV5670_TEST_PATTERN_ENABLE BIT(3)
60 #define OV5670_REG_TEST_PATTERN_CTRL 0x4320
62 #define OV5670_REG_VALUE_08BIT 1
63 #define OV5670_REG_VALUE_16BIT 2
64 #define OV5670_REG_VALUE_24BIT 3
66 /* Initial number of frames to skip to avoid possible garbage */
67 #define OV5670_NUM_OF_SKIP_FRAMES 2
74 struct ov5670_reg_list
{
76 const struct ov5670_reg
*regs
;
79 struct ov5670_link_freq_config
{
81 const struct ov5670_reg_list reg_list
;
85 /* Frame width in pixels */
88 /* Frame height in pixels */
91 /* Default vertical timining size */
94 /* Min vertical timining size */
97 /* Link frequency needed for this resolution */
100 /* Sensor register settings for this resolution */
101 const struct ov5670_reg_list reg_list
;
104 static const struct ov5670_reg mipi_data_rate_840mbps
[] = {
122 static const struct ov5670_reg mode_2592x1944_regs
[] = {
388 static const struct ov5670_reg mode_1296x972_regs
[] = {
654 static const struct ov5670_reg mode_648x486_regs
[] = {
920 static const struct ov5670_reg mode_2560x1440_regs
[] = {
1185 static const struct ov5670_reg mode_1280x720_regs
[] = {
1451 static const struct ov5670_reg mode_640x360_regs
[] = {
1717 static const char * const ov5670_test_pattern_menu
[] = {
1719 "Vertical Color Bar Type 1",
1722 /* Supported link frequencies */
1723 #define OV5670_LINK_FREQ_422MHZ 422400000
1724 #define OV5670_LINK_FREQ_422MHZ_INDEX 0
1725 static const struct ov5670_link_freq_config link_freq_configs
[] = {
1727 /* pixel_rate = link_freq * 2 * nr_of_lanes / bits_per_sample */
1728 .pixel_rate
= (OV5670_LINK_FREQ_422MHZ
* 2 * 2) / 10,
1730 .num_of_regs
= ARRAY_SIZE(mipi_data_rate_840mbps
),
1731 .regs
= mipi_data_rate_840mbps
,
1736 static const s64 link_freq_menu_items
[] = {
1737 OV5670_LINK_FREQ_422MHZ
1741 * OV5670 sensor supports following resolutions with full FOV:
1742 * 4:3 ==> {2592x1944, 1296x972, 648x486}
1743 * 16:9 ==> {2560x1440, 1280x720, 640x360}
1745 static const struct ov5670_mode supported_modes
[] = {
1749 .vts_def
= OV5670_VTS_30FPS
,
1750 .vts_min
= OV5670_VTS_30FPS
,
1752 .num_of_regs
= ARRAY_SIZE(mode_2592x1944_regs
),
1753 .regs
= mode_2592x1944_regs
,
1755 .link_freq_index
= OV5670_LINK_FREQ_422MHZ_INDEX
,
1760 .vts_def
= OV5670_VTS_30FPS
,
1763 .num_of_regs
= ARRAY_SIZE(mode_1296x972_regs
),
1764 .regs
= mode_1296x972_regs
,
1766 .link_freq_index
= OV5670_LINK_FREQ_422MHZ_INDEX
,
1771 .vts_def
= OV5670_VTS_30FPS
,
1774 .num_of_regs
= ARRAY_SIZE(mode_648x486_regs
),
1775 .regs
= mode_648x486_regs
,
1777 .link_freq_index
= OV5670_LINK_FREQ_422MHZ_INDEX
,
1782 .vts_def
= OV5670_VTS_30FPS
,
1783 .vts_min
= OV5670_VTS_30FPS
,
1785 .num_of_regs
= ARRAY_SIZE(mode_2560x1440_regs
),
1786 .regs
= mode_2560x1440_regs
,
1788 .link_freq_index
= OV5670_LINK_FREQ_422MHZ_INDEX
,
1793 .vts_def
= OV5670_VTS_30FPS
,
1796 .num_of_regs
= ARRAY_SIZE(mode_1280x720_regs
),
1797 .regs
= mode_1280x720_regs
,
1799 .link_freq_index
= OV5670_LINK_FREQ_422MHZ_INDEX
,
1804 .vts_def
= OV5670_VTS_30FPS
,
1807 .num_of_regs
= ARRAY_SIZE(mode_640x360_regs
),
1808 .regs
= mode_640x360_regs
,
1810 .link_freq_index
= OV5670_LINK_FREQ_422MHZ_INDEX
,
1815 struct v4l2_subdev sd
;
1816 struct media_pad pad
;
1818 struct v4l2_ctrl_handler ctrl_handler
;
1820 struct v4l2_ctrl
*link_freq
;
1821 struct v4l2_ctrl
*pixel_rate
;
1822 struct v4l2_ctrl
*vblank
;
1823 struct v4l2_ctrl
*hblank
;
1824 struct v4l2_ctrl
*exposure
;
1827 const struct ov5670_mode
*cur_mode
;
1829 /* To serialize asynchronus callbacks */
1832 /* Streaming on/off */
1836 #define to_ov5670(_sd) container_of(_sd, struct ov5670, sd)
1838 /* Read registers up to 4 at a time */
1839 static int ov5670_read_reg(struct ov5670
*ov5670
, u16 reg
, unsigned int len
,
1842 struct i2c_client
*client
= v4l2_get_subdevdata(&ov5670
->sd
);
1843 struct i2c_msg msgs
[2];
1846 __be16 reg_addr_be
= cpu_to_be16(reg
);
1852 data_be_p
= (u8
*)&data_be
;
1853 /* Write register address */
1854 msgs
[0].addr
= client
->addr
;
1857 msgs
[0].buf
= (u8
*)®_addr_be
;
1859 /* Read data from register */
1860 msgs
[1].addr
= client
->addr
;
1861 msgs
[1].flags
= I2C_M_RD
;
1863 msgs
[1].buf
= &data_be_p
[4 - len
];
1865 ret
= i2c_transfer(client
->adapter
, msgs
, ARRAY_SIZE(msgs
));
1866 if (ret
!= ARRAY_SIZE(msgs
))
1869 *val
= be32_to_cpu(data_be
);
1874 /* Write registers up to 4 at a time */
1875 static int ov5670_write_reg(struct ov5670
*ov5670
, u16 reg
, unsigned int len
,
1878 struct i2c_client
*client
= v4l2_get_subdevdata(&ov5670
->sd
);
1889 buf
[1] = reg
& 0xff;
1891 tmp
= cpu_to_be32(val
);
1897 buf
[buf_i
++] = val_p
[val_i
++];
1899 if (i2c_master_send(client
, buf
, len
+ 2) != len
+ 2)
1905 /* Write a list of registers */
1906 static int ov5670_write_regs(struct ov5670
*ov5670
,
1907 const struct ov5670_reg
*regs
, unsigned int len
)
1909 struct i2c_client
*client
= v4l2_get_subdevdata(&ov5670
->sd
);
1913 for (i
= 0; i
< len
; i
++) {
1914 ret
= ov5670_write_reg(ov5670
, regs
[i
].address
, 1, regs
[i
].val
);
1916 dev_err_ratelimited(
1918 "Failed to write reg 0x%4.4x. error = %d\n",
1919 regs
[i
].address
, ret
);
1928 static int ov5670_write_reg_list(struct ov5670
*ov5670
,
1929 const struct ov5670_reg_list
*r_list
)
1931 return ov5670_write_regs(ov5670
, r_list
->regs
, r_list
->num_of_regs
);
1934 /* Open sub-device */
1935 static int ov5670_open(struct v4l2_subdev
*sd
, struct v4l2_subdev_fh
*fh
)
1937 struct ov5670
*ov5670
= to_ov5670(sd
);
1938 struct v4l2_mbus_framefmt
*try_fmt
=
1939 v4l2_subdev_get_try_format(sd
, fh
->pad
, 0);
1941 mutex_lock(&ov5670
->mutex
);
1943 /* Initialize try_fmt */
1944 try_fmt
->width
= ov5670
->cur_mode
->width
;
1945 try_fmt
->height
= ov5670
->cur_mode
->height
;
1946 try_fmt
->code
= MEDIA_BUS_FMT_SGRBG10_1X10
;
1947 try_fmt
->field
= V4L2_FIELD_NONE
;
1949 /* No crop or compose */
1950 mutex_unlock(&ov5670
->mutex
);
1955 static int ov5670_update_digital_gain(struct ov5670
*ov5670
, u32 d_gain
)
1959 ret
= ov5670_write_reg(ov5670
, OV5670_REG_R_DGTL_GAIN
,
1960 OV5670_REG_VALUE_16BIT
, d_gain
);
1964 ret
= ov5670_write_reg(ov5670
, OV5670_REG_G_DGTL_GAIN
,
1965 OV5670_REG_VALUE_16BIT
, d_gain
);
1969 return ov5670_write_reg(ov5670
, OV5670_REG_B_DGTL_GAIN
,
1970 OV5670_REG_VALUE_16BIT
, d_gain
);
1973 static int ov5670_enable_test_pattern(struct ov5670
*ov5670
, u32 pattern
)
1978 /* Set the bayer order that we support */
1979 ret
= ov5670_write_reg(ov5670
, OV5670_REG_TEST_PATTERN_CTRL
,
1980 OV5670_REG_VALUE_08BIT
, 0);
1984 ret
= ov5670_read_reg(ov5670
, OV5670_REG_TEST_PATTERN
,
1985 OV5670_REG_VALUE_08BIT
, &val
);
1990 val
|= OV5670_TEST_PATTERN_ENABLE
;
1992 val
&= ~OV5670_TEST_PATTERN_ENABLE
;
1994 return ov5670_write_reg(ov5670
, OV5670_REG_TEST_PATTERN
,
1995 OV5670_REG_VALUE_08BIT
, val
);
1998 /* Initialize control handlers */
1999 static int ov5670_set_ctrl(struct v4l2_ctrl
*ctrl
)
2001 struct ov5670
*ov5670
= container_of(ctrl
->handler
,
2002 struct ov5670
, ctrl_handler
);
2003 struct i2c_client
*client
= v4l2_get_subdevdata(&ov5670
->sd
);
2007 /* Propagate change of current control to all related controls */
2009 case V4L2_CID_VBLANK
:
2010 /* Update max exposure while meeting expected vblanking */
2011 max
= ov5670
->cur_mode
->height
+ ctrl
->val
- 8;
2012 __v4l2_ctrl_modify_range(ov5670
->exposure
,
2013 ov5670
->exposure
->minimum
, max
,
2014 ov5670
->exposure
->step
, max
);
2018 /* V4L2 controls values will be applied only when power is already up */
2019 if (!pm_runtime_get_if_in_use(&client
->dev
))
2023 case V4L2_CID_ANALOGUE_GAIN
:
2024 ret
= ov5670_write_reg(ov5670
, OV5670_REG_ANALOG_GAIN
,
2025 OV5670_REG_VALUE_16BIT
, ctrl
->val
);
2027 case V4L2_CID_DIGITAL_GAIN
:
2028 ret
= ov5670_update_digital_gain(ov5670
, ctrl
->val
);
2030 case V4L2_CID_EXPOSURE
:
2031 /* 4 least significant bits of expsoure are fractional part */
2032 ret
= ov5670_write_reg(ov5670
, OV5670_REG_EXPOSURE
,
2033 OV5670_REG_VALUE_24BIT
, ctrl
->val
<< 4);
2035 case V4L2_CID_VBLANK
:
2036 /* Update VTS that meets expected vertical blanking */
2037 ret
= ov5670_write_reg(ov5670
, OV5670_REG_VTS
,
2038 OV5670_REG_VALUE_16BIT
,
2039 ov5670
->cur_mode
->height
+ ctrl
->val
);
2041 case V4L2_CID_TEST_PATTERN
:
2042 ret
= ov5670_enable_test_pattern(ov5670
, ctrl
->val
);
2045 dev_info(&client
->dev
, "%s Unhandled id:0x%x, val:0x%x\n",
2046 __func__
, ctrl
->id
, ctrl
->val
);
2050 pm_runtime_put(&client
->dev
);
2055 static const struct v4l2_ctrl_ops ov5670_ctrl_ops
= {
2056 .s_ctrl
= ov5670_set_ctrl
,
2059 /* Initialize control handlers */
2060 static int ov5670_init_controls(struct ov5670
*ov5670
)
2062 struct v4l2_ctrl_handler
*ctrl_hdlr
;
2069 ctrl_hdlr
= &ov5670
->ctrl_handler
;
2070 ret
= v4l2_ctrl_handler_init(ctrl_hdlr
, 8);
2074 ctrl_hdlr
->lock
= &ov5670
->mutex
;
2075 ov5670
->link_freq
= v4l2_ctrl_new_int_menu(ctrl_hdlr
,
2078 0, 0, link_freq_menu_items
);
2079 if (ov5670
->link_freq
)
2080 ov5670
->link_freq
->flags
|= V4L2_CTRL_FLAG_READ_ONLY
;
2082 /* By default, V4L2_CID_PIXEL_RATE is read only */
2083 ov5670
->pixel_rate
= v4l2_ctrl_new_std(ctrl_hdlr
, &ov5670_ctrl_ops
,
2084 V4L2_CID_PIXEL_RATE
, 0,
2085 link_freq_configs
[0].pixel_rate
,
2087 link_freq_configs
[0].pixel_rate
);
2089 vblank_max
= OV5670_VTS_MAX
- ov5670
->cur_mode
->height
;
2090 vblank_def
= ov5670
->cur_mode
->vts_def
- ov5670
->cur_mode
->height
;
2091 vblank_min
= ov5670
->cur_mode
->vts_min
- ov5670
->cur_mode
->height
;
2092 ov5670
->vblank
= v4l2_ctrl_new_std(ctrl_hdlr
, &ov5670_ctrl_ops
,
2093 V4L2_CID_VBLANK
, vblank_min
,
2094 vblank_max
, 1, vblank_def
);
2096 ov5670
->hblank
= v4l2_ctrl_new_std(
2097 ctrl_hdlr
, &ov5670_ctrl_ops
, V4L2_CID_HBLANK
,
2098 OV5670_FIXED_PPL
- ov5670
->cur_mode
->width
,
2099 OV5670_FIXED_PPL
- ov5670
->cur_mode
->width
, 1,
2100 OV5670_FIXED_PPL
- ov5670
->cur_mode
->width
);
2102 ov5670
->hblank
->flags
|= V4L2_CTRL_FLAG_READ_ONLY
;
2104 /* Get min, max, step, default from sensor */
2105 v4l2_ctrl_new_std(ctrl_hdlr
, &ov5670_ctrl_ops
, V4L2_CID_ANALOGUE_GAIN
,
2106 ANALOG_GAIN_MIN
, ANALOG_GAIN_MAX
, ANALOG_GAIN_STEP
,
2107 ANALOG_GAIN_DEFAULT
);
2110 v4l2_ctrl_new_std(ctrl_hdlr
, &ov5670_ctrl_ops
, V4L2_CID_DIGITAL_GAIN
,
2111 OV5670_DGTL_GAIN_MIN
, OV5670_DGTL_GAIN_MAX
,
2112 OV5670_DGTL_GAIN_STEP
, OV5670_DGTL_GAIN_DEFAULT
);
2114 /* Get min, max, step, default from sensor */
2115 exposure_max
= ov5670
->cur_mode
->vts_def
- 8;
2116 ov5670
->exposure
= v4l2_ctrl_new_std(ctrl_hdlr
, &ov5670_ctrl_ops
,
2118 OV5670_EXPOSURE_MIN
,
2119 exposure_max
, OV5670_EXPOSURE_STEP
,
2122 v4l2_ctrl_new_std_menu_items(ctrl_hdlr
, &ov5670_ctrl_ops
,
2123 V4L2_CID_TEST_PATTERN
,
2124 ARRAY_SIZE(ov5670_test_pattern_menu
) - 1,
2125 0, 0, ov5670_test_pattern_menu
);
2127 if (ctrl_hdlr
->error
) {
2128 ret
= ctrl_hdlr
->error
;
2132 ov5670
->sd
.ctrl_handler
= ctrl_hdlr
;
2137 v4l2_ctrl_handler_free(ctrl_hdlr
);
2142 static int ov5670_enum_mbus_code(struct v4l2_subdev
*sd
,
2143 struct v4l2_subdev_pad_config
*cfg
,
2144 struct v4l2_subdev_mbus_code_enum
*code
)
2146 /* Only one bayer order GRBG is supported */
2147 if (code
->index
> 0)
2150 code
->code
= MEDIA_BUS_FMT_SGRBG10_1X10
;
2155 static int ov5670_enum_frame_size(struct v4l2_subdev
*sd
,
2156 struct v4l2_subdev_pad_config
*cfg
,
2157 struct v4l2_subdev_frame_size_enum
*fse
)
2159 if (fse
->index
>= ARRAY_SIZE(supported_modes
))
2162 if (fse
->code
!= MEDIA_BUS_FMT_SGRBG10_1X10
)
2165 fse
->min_width
= supported_modes
[fse
->index
].width
;
2166 fse
->max_width
= fse
->min_width
;
2167 fse
->min_height
= supported_modes
[fse
->index
].height
;
2168 fse
->max_height
= fse
->min_height
;
2173 static void ov5670_update_pad_format(const struct ov5670_mode
*mode
,
2174 struct v4l2_subdev_format
*fmt
)
2176 fmt
->format
.width
= mode
->width
;
2177 fmt
->format
.height
= mode
->height
;
2178 fmt
->format
.code
= MEDIA_BUS_FMT_SGRBG10_1X10
;
2179 fmt
->format
.field
= V4L2_FIELD_NONE
;
2182 static int ov5670_do_get_pad_format(struct ov5670
*ov5670
,
2183 struct v4l2_subdev_pad_config
*cfg
,
2184 struct v4l2_subdev_format
*fmt
)
2186 if (fmt
->which
== V4L2_SUBDEV_FORMAT_TRY
)
2187 fmt
->format
= *v4l2_subdev_get_try_format(&ov5670
->sd
, cfg
,
2190 ov5670_update_pad_format(ov5670
->cur_mode
, fmt
);
2195 static int ov5670_get_pad_format(struct v4l2_subdev
*sd
,
2196 struct v4l2_subdev_pad_config
*cfg
,
2197 struct v4l2_subdev_format
*fmt
)
2199 struct ov5670
*ov5670
= to_ov5670(sd
);
2202 mutex_lock(&ov5670
->mutex
);
2203 ret
= ov5670_do_get_pad_format(ov5670
, cfg
, fmt
);
2204 mutex_unlock(&ov5670
->mutex
);
2209 static int ov5670_set_pad_format(struct v4l2_subdev
*sd
,
2210 struct v4l2_subdev_pad_config
*cfg
,
2211 struct v4l2_subdev_format
*fmt
)
2213 struct ov5670
*ov5670
= to_ov5670(sd
);
2214 const struct ov5670_mode
*mode
;
2218 mutex_lock(&ov5670
->mutex
);
2220 fmt
->format
.code
= MEDIA_BUS_FMT_SGRBG10_1X10
;
2222 mode
= v4l2_find_nearest_size(supported_modes
,
2223 ARRAY_SIZE(supported_modes
),
2225 fmt
->format
.width
, fmt
->format
.height
);
2226 ov5670_update_pad_format(mode
, fmt
);
2227 if (fmt
->which
== V4L2_SUBDEV_FORMAT_TRY
) {
2228 *v4l2_subdev_get_try_format(sd
, cfg
, fmt
->pad
) = fmt
->format
;
2230 ov5670
->cur_mode
= mode
;
2231 __v4l2_ctrl_s_ctrl(ov5670
->link_freq
, mode
->link_freq_index
);
2232 __v4l2_ctrl_s_ctrl_int64(
2234 link_freq_configs
[mode
->link_freq_index
].pixel_rate
);
2235 /* Update limits and set FPS to default */
2236 vblank_def
= ov5670
->cur_mode
->vts_def
-
2237 ov5670
->cur_mode
->height
;
2238 __v4l2_ctrl_modify_range(
2240 ov5670
->cur_mode
->vts_min
- ov5670
->cur_mode
->height
,
2241 OV5670_VTS_MAX
- ov5670
->cur_mode
->height
, 1,
2243 __v4l2_ctrl_s_ctrl(ov5670
->vblank
, vblank_def
);
2244 h_blank
= OV5670_FIXED_PPL
- ov5670
->cur_mode
->width
;
2245 __v4l2_ctrl_modify_range(ov5670
->hblank
, h_blank
, h_blank
, 1,
2249 mutex_unlock(&ov5670
->mutex
);
2254 static int ov5670_get_skip_frames(struct v4l2_subdev
*sd
, u32
*frames
)
2256 *frames
= OV5670_NUM_OF_SKIP_FRAMES
;
2261 /* Prepare streaming by writing default values and customized values */
2262 static int ov5670_start_streaming(struct ov5670
*ov5670
)
2264 struct i2c_client
*client
= v4l2_get_subdevdata(&ov5670
->sd
);
2265 const struct ov5670_reg_list
*reg_list
;
2266 int link_freq_index
;
2269 /* Get out of from software reset */
2270 ret
= ov5670_write_reg(ov5670
, OV5670_REG_SOFTWARE_RST
,
2271 OV5670_REG_VALUE_08BIT
, OV5670_SOFTWARE_RST
);
2273 dev_err(&client
->dev
, "%s failed to set powerup registers\n",
2279 link_freq_index
= ov5670
->cur_mode
->link_freq_index
;
2280 reg_list
= &link_freq_configs
[link_freq_index
].reg_list
;
2281 ret
= ov5670_write_reg_list(ov5670
, reg_list
);
2283 dev_err(&client
->dev
, "%s failed to set plls\n", __func__
);
2287 /* Apply default values of current mode */
2288 reg_list
= &ov5670
->cur_mode
->reg_list
;
2289 ret
= ov5670_write_reg_list(ov5670
, reg_list
);
2291 dev_err(&client
->dev
, "%s failed to set mode\n", __func__
);
2295 ret
= __v4l2_ctrl_handler_setup(ov5670
->sd
.ctrl_handler
);
2299 /* Write stream on list */
2300 ret
= ov5670_write_reg(ov5670
, OV5670_REG_MODE_SELECT
,
2301 OV5670_REG_VALUE_08BIT
, OV5670_MODE_STREAMING
);
2303 dev_err(&client
->dev
, "%s failed to set stream\n", __func__
);
2310 static int ov5670_stop_streaming(struct ov5670
*ov5670
)
2312 struct i2c_client
*client
= v4l2_get_subdevdata(&ov5670
->sd
);
2315 ret
= ov5670_write_reg(ov5670
, OV5670_REG_MODE_SELECT
,
2316 OV5670_REG_VALUE_08BIT
, OV5670_MODE_STANDBY
);
2318 dev_err(&client
->dev
, "%s failed to set stream\n", __func__
);
2320 /* Return success even if it was an error, as there is nothing the
2321 * caller can do about it.
2326 static int ov5670_set_stream(struct v4l2_subdev
*sd
, int enable
)
2328 struct ov5670
*ov5670
= to_ov5670(sd
);
2329 struct i2c_client
*client
= v4l2_get_subdevdata(sd
);
2332 mutex_lock(&ov5670
->mutex
);
2333 if (ov5670
->streaming
== enable
)
2334 goto unlock_and_return
;
2337 ret
= pm_runtime_get_sync(&client
->dev
);
2339 pm_runtime_put_noidle(&client
->dev
);
2340 goto unlock_and_return
;
2343 ret
= ov5670_start_streaming(ov5670
);
2347 ret
= ov5670_stop_streaming(ov5670
);
2348 pm_runtime_put(&client
->dev
);
2350 ov5670
->streaming
= enable
;
2351 goto unlock_and_return
;
2354 pm_runtime_put(&client
->dev
);
2357 mutex_unlock(&ov5670
->mutex
);
2362 static int __maybe_unused
ov5670_suspend(struct device
*dev
)
2364 struct i2c_client
*client
= to_i2c_client(dev
);
2365 struct v4l2_subdev
*sd
= i2c_get_clientdata(client
);
2366 struct ov5670
*ov5670
= to_ov5670(sd
);
2368 if (ov5670
->streaming
)
2369 ov5670_stop_streaming(ov5670
);
2374 static int __maybe_unused
ov5670_resume(struct device
*dev
)
2376 struct i2c_client
*client
= to_i2c_client(dev
);
2377 struct v4l2_subdev
*sd
= i2c_get_clientdata(client
);
2378 struct ov5670
*ov5670
= to_ov5670(sd
);
2381 if (ov5670
->streaming
) {
2382 ret
= ov5670_start_streaming(ov5670
);
2384 ov5670_stop_streaming(ov5670
);
2392 /* Verify chip ID */
2393 static int ov5670_identify_module(struct ov5670
*ov5670
)
2395 struct i2c_client
*client
= v4l2_get_subdevdata(&ov5670
->sd
);
2399 ret
= ov5670_read_reg(ov5670
, OV5670_REG_CHIP_ID
,
2400 OV5670_REG_VALUE_24BIT
, &val
);
2404 if (val
!= OV5670_CHIP_ID
) {
2405 dev_err(&client
->dev
, "chip id mismatch: %x!=%x\n",
2406 OV5670_CHIP_ID
, val
);
2413 static const struct v4l2_subdev_video_ops ov5670_video_ops
= {
2414 .s_stream
= ov5670_set_stream
,
2417 static const struct v4l2_subdev_pad_ops ov5670_pad_ops
= {
2418 .enum_mbus_code
= ov5670_enum_mbus_code
,
2419 .get_fmt
= ov5670_get_pad_format
,
2420 .set_fmt
= ov5670_set_pad_format
,
2421 .enum_frame_size
= ov5670_enum_frame_size
,
2424 static const struct v4l2_subdev_sensor_ops ov5670_sensor_ops
= {
2425 .g_skip_frames
= ov5670_get_skip_frames
,
2428 static const struct v4l2_subdev_ops ov5670_subdev_ops
= {
2429 .video
= &ov5670_video_ops
,
2430 .pad
= &ov5670_pad_ops
,
2431 .sensor
= &ov5670_sensor_ops
,
2434 static const struct media_entity_operations ov5670_subdev_entity_ops
= {
2435 .link_validate
= v4l2_subdev_link_validate
,
2438 static const struct v4l2_subdev_internal_ops ov5670_internal_ops
= {
2439 .open
= ov5670_open
,
2442 static int ov5670_probe(struct i2c_client
*client
)
2444 struct ov5670
*ov5670
;
2445 const char *err_msg
;
2449 device_property_read_u32(&client
->dev
, "clock-frequency", &input_clk
);
2450 if (input_clk
!= 19200000)
2453 ov5670
= devm_kzalloc(&client
->dev
, sizeof(*ov5670
), GFP_KERNEL
);
2456 err_msg
= "devm_kzalloc() error";
2460 /* Initialize subdev */
2461 v4l2_i2c_subdev_init(&ov5670
->sd
, client
, &ov5670_subdev_ops
);
2463 /* Check module identity */
2464 ret
= ov5670_identify_module(ov5670
);
2466 err_msg
= "ov5670_identify_module() error";
2470 mutex_init(&ov5670
->mutex
);
2472 /* Set default mode to max resolution */
2473 ov5670
->cur_mode
= &supported_modes
[0];
2475 ret
= ov5670_init_controls(ov5670
);
2477 err_msg
= "ov5670_init_controls() error";
2478 goto error_mutex_destroy
;
2481 ov5670
->sd
.internal_ops
= &ov5670_internal_ops
;
2482 ov5670
->sd
.flags
|= V4L2_SUBDEV_FL_HAS_DEVNODE
;
2483 ov5670
->sd
.entity
.ops
= &ov5670_subdev_entity_ops
;
2484 ov5670
->sd
.entity
.function
= MEDIA_ENT_F_CAM_SENSOR
;
2486 /* Source pad initialization */
2487 ov5670
->pad
.flags
= MEDIA_PAD_FL_SOURCE
;
2488 ret
= media_entity_pads_init(&ov5670
->sd
.entity
, 1, &ov5670
->pad
);
2490 err_msg
= "media_entity_pads_init() error";
2491 goto error_handler_free
;
2494 /* Async register for subdev */
2495 ret
= v4l2_async_register_subdev_sensor_common(&ov5670
->sd
);
2497 err_msg
= "v4l2_async_register_subdev() error";
2498 goto error_entity_cleanup
;
2501 ov5670
->streaming
= false;
2504 * Device is already turned on by i2c-core with ACPI domain PM.
2505 * Enable runtime PM and turn off the device.
2507 pm_runtime_set_active(&client
->dev
);
2508 pm_runtime_enable(&client
->dev
);
2509 pm_runtime_idle(&client
->dev
);
2513 error_entity_cleanup
:
2514 media_entity_cleanup(&ov5670
->sd
.entity
);
2517 v4l2_ctrl_handler_free(ov5670
->sd
.ctrl_handler
);
2519 error_mutex_destroy
:
2520 mutex_destroy(&ov5670
->mutex
);
2523 dev_err(&client
->dev
, "%s: %s %d\n", __func__
, err_msg
, ret
);
2528 static int ov5670_remove(struct i2c_client
*client
)
2530 struct v4l2_subdev
*sd
= i2c_get_clientdata(client
);
2531 struct ov5670
*ov5670
= to_ov5670(sd
);
2533 v4l2_async_unregister_subdev(sd
);
2534 media_entity_cleanup(&sd
->entity
);
2535 v4l2_ctrl_handler_free(sd
->ctrl_handler
);
2536 mutex_destroy(&ov5670
->mutex
);
2538 pm_runtime_disable(&client
->dev
);
2543 static const struct dev_pm_ops ov5670_pm_ops
= {
2544 SET_SYSTEM_SLEEP_PM_OPS(ov5670_suspend
, ov5670_resume
)
2548 static const struct acpi_device_id ov5670_acpi_ids
[] = {
2553 MODULE_DEVICE_TABLE(acpi
, ov5670_acpi_ids
);
2556 static struct i2c_driver ov5670_i2c_driver
= {
2559 .pm
= &ov5670_pm_ops
,
2560 .acpi_match_table
= ACPI_PTR(ov5670_acpi_ids
),
2562 .probe_new
= ov5670_probe
,
2563 .remove
= ov5670_remove
,
2566 module_i2c_driver(ov5670_i2c_driver
);
2568 MODULE_AUTHOR("Rapolu, Chiranjeevi <chiranjeevi.rapolu@intel.com>");
2569 MODULE_AUTHOR("Yang, Hyungwoo <hyungwoo.yang@intel.com>");
2570 MODULE_DESCRIPTION("Omnivision ov5670 sensor driver");
2571 MODULE_LICENSE("GPL v2");