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-fwnode.h>
12 #define OV5670_REG_CHIP_ID 0x300a
13 #define OV5670_CHIP_ID 0x005670
15 #define OV5670_REG_MODE_SELECT 0x0100
16 #define OV5670_MODE_STANDBY 0x00
17 #define OV5670_MODE_STREAMING 0x01
19 #define OV5670_REG_SOFTWARE_RST 0x0103
20 #define OV5670_SOFTWARE_RST 0x01
22 /* vertical-timings from sensor */
23 #define OV5670_REG_VTS 0x380e
24 #define OV5670_VTS_30FPS 0x0808 /* default for 30 fps */
25 #define OV5670_VTS_MAX 0xffff
27 /* horizontal-timings from sensor */
28 #define OV5670_REG_HTS 0x380c
31 * Pixels-per-line(PPL) = Time-per-line * pixel-rate
32 * In OV5670, Time-per-line = HTS/SCLK.
33 * HTS is fixed for all resolutions, not recommended to change.
35 #define OV5670_FIXED_PPL 2724 /* Pixels per line */
37 /* Exposure controls from sensor */
38 #define OV5670_REG_EXPOSURE 0x3500
39 #define OV5670_EXPOSURE_MIN 4
40 #define OV5670_EXPOSURE_STEP 1
42 /* Analog gain controls from sensor */
43 #define OV5670_REG_ANALOG_GAIN 0x3508
44 #define ANALOG_GAIN_MIN 0
45 #define ANALOG_GAIN_MAX 8191
46 #define ANALOG_GAIN_STEP 1
47 #define ANALOG_GAIN_DEFAULT 128
49 /* Digital gain controls from sensor */
50 #define OV5670_REG_R_DGTL_GAIN 0x5032
51 #define OV5670_REG_G_DGTL_GAIN 0x5034
52 #define OV5670_REG_B_DGTL_GAIN 0x5036
53 #define OV5670_DGTL_GAIN_MIN 0
54 #define OV5670_DGTL_GAIN_MAX 4095
55 #define OV5670_DGTL_GAIN_STEP 1
56 #define OV5670_DGTL_GAIN_DEFAULT 1024
58 /* Test Pattern Control */
59 #define OV5670_REG_TEST_PATTERN 0x4303
60 #define OV5670_TEST_PATTERN_ENABLE BIT(3)
61 #define OV5670_REG_TEST_PATTERN_CTRL 0x4320
63 #define OV5670_REG_VALUE_08BIT 1
64 #define OV5670_REG_VALUE_16BIT 2
65 #define OV5670_REG_VALUE_24BIT 3
67 /* Initial number of frames to skip to avoid possible garbage */
68 #define OV5670_NUM_OF_SKIP_FRAMES 2
75 struct ov5670_reg_list
{
77 const struct ov5670_reg
*regs
;
80 struct ov5670_link_freq_config
{
82 const struct ov5670_reg_list reg_list
;
86 /* Frame width in pixels */
89 /* Frame height in pixels */
92 /* Default vertical timining size */
95 /* Min vertical timining size */
98 /* Link frequency needed for this resolution */
101 /* Sensor register settings for this resolution */
102 const struct ov5670_reg_list reg_list
;
105 static const struct ov5670_reg mipi_data_rate_840mbps
[] = {
123 static const struct ov5670_reg mode_2592x1944_regs
[] = {
389 static const struct ov5670_reg mode_1296x972_regs
[] = {
655 static const struct ov5670_reg mode_648x486_regs
[] = {
921 static const struct ov5670_reg mode_2560x1440_regs
[] = {
1186 static const struct ov5670_reg mode_1280x720_regs
[] = {
1452 static const struct ov5670_reg mode_640x360_regs
[] = {
1718 static const char * const ov5670_test_pattern_menu
[] = {
1720 "Vertical Color Bar Type 1",
1723 /* Supported link frequencies */
1724 #define OV5670_LINK_FREQ_422MHZ 422400000
1725 #define OV5670_LINK_FREQ_422MHZ_INDEX 0
1726 static const struct ov5670_link_freq_config link_freq_configs
[] = {
1728 /* pixel_rate = link_freq * 2 * nr_of_lanes / bits_per_sample */
1729 .pixel_rate
= (OV5670_LINK_FREQ_422MHZ
* 2 * 2) / 10,
1731 .num_of_regs
= ARRAY_SIZE(mipi_data_rate_840mbps
),
1732 .regs
= mipi_data_rate_840mbps
,
1737 static const s64 link_freq_menu_items
[] = {
1738 OV5670_LINK_FREQ_422MHZ
1742 * OV5670 sensor supports following resolutions with full FOV:
1743 * 4:3 ==> {2592x1944, 1296x972, 648x486}
1744 * 16:9 ==> {2560x1440, 1280x720, 640x360}
1746 static const struct ov5670_mode supported_modes
[] = {
1750 .vts_def
= OV5670_VTS_30FPS
,
1751 .vts_min
= OV5670_VTS_30FPS
,
1753 .num_of_regs
= ARRAY_SIZE(mode_2592x1944_regs
),
1754 .regs
= mode_2592x1944_regs
,
1756 .link_freq_index
= OV5670_LINK_FREQ_422MHZ_INDEX
,
1761 .vts_def
= OV5670_VTS_30FPS
,
1764 .num_of_regs
= ARRAY_SIZE(mode_1296x972_regs
),
1765 .regs
= mode_1296x972_regs
,
1767 .link_freq_index
= OV5670_LINK_FREQ_422MHZ_INDEX
,
1772 .vts_def
= OV5670_VTS_30FPS
,
1775 .num_of_regs
= ARRAY_SIZE(mode_648x486_regs
),
1776 .regs
= mode_648x486_regs
,
1778 .link_freq_index
= OV5670_LINK_FREQ_422MHZ_INDEX
,
1783 .vts_def
= OV5670_VTS_30FPS
,
1784 .vts_min
= OV5670_VTS_30FPS
,
1786 .num_of_regs
= ARRAY_SIZE(mode_2560x1440_regs
),
1787 .regs
= mode_2560x1440_regs
,
1789 .link_freq_index
= OV5670_LINK_FREQ_422MHZ_INDEX
,
1794 .vts_def
= OV5670_VTS_30FPS
,
1797 .num_of_regs
= ARRAY_SIZE(mode_1280x720_regs
),
1798 .regs
= mode_1280x720_regs
,
1800 .link_freq_index
= OV5670_LINK_FREQ_422MHZ_INDEX
,
1805 .vts_def
= OV5670_VTS_30FPS
,
1808 .num_of_regs
= ARRAY_SIZE(mode_640x360_regs
),
1809 .regs
= mode_640x360_regs
,
1811 .link_freq_index
= OV5670_LINK_FREQ_422MHZ_INDEX
,
1816 struct v4l2_subdev sd
;
1817 struct media_pad pad
;
1819 struct v4l2_ctrl_handler ctrl_handler
;
1821 struct v4l2_ctrl
*link_freq
;
1822 struct v4l2_ctrl
*pixel_rate
;
1823 struct v4l2_ctrl
*vblank
;
1824 struct v4l2_ctrl
*hblank
;
1825 struct v4l2_ctrl
*exposure
;
1828 const struct ov5670_mode
*cur_mode
;
1830 /* To serialize asynchronus callbacks */
1833 /* Streaming on/off */
1837 #define to_ov5670(_sd) container_of(_sd, struct ov5670, sd)
1839 /* Read registers up to 4 at a time */
1840 static int ov5670_read_reg(struct ov5670
*ov5670
, u16 reg
, unsigned int len
,
1843 struct i2c_client
*client
= v4l2_get_subdevdata(&ov5670
->sd
);
1844 struct i2c_msg msgs
[2];
1847 __be16 reg_addr_be
= cpu_to_be16(reg
);
1853 data_be_p
= (u8
*)&data_be
;
1854 /* Write register address */
1855 msgs
[0].addr
= client
->addr
;
1858 msgs
[0].buf
= (u8
*)®_addr_be
;
1860 /* Read data from register */
1861 msgs
[1].addr
= client
->addr
;
1862 msgs
[1].flags
= I2C_M_RD
;
1864 msgs
[1].buf
= &data_be_p
[4 - len
];
1866 ret
= i2c_transfer(client
->adapter
, msgs
, ARRAY_SIZE(msgs
));
1867 if (ret
!= ARRAY_SIZE(msgs
))
1870 *val
= be32_to_cpu(data_be
);
1875 /* Write registers up to 4 at a time */
1876 static int ov5670_write_reg(struct ov5670
*ov5670
, u16 reg
, unsigned int len
,
1879 struct i2c_client
*client
= v4l2_get_subdevdata(&ov5670
->sd
);
1890 buf
[1] = reg
& 0xff;
1892 tmp
= cpu_to_be32(val
);
1898 buf
[buf_i
++] = val_p
[val_i
++];
1900 if (i2c_master_send(client
, buf
, len
+ 2) != len
+ 2)
1906 /* Write a list of registers */
1907 static int ov5670_write_regs(struct ov5670
*ov5670
,
1908 const struct ov5670_reg
*regs
, unsigned int len
)
1910 struct i2c_client
*client
= v4l2_get_subdevdata(&ov5670
->sd
);
1914 for (i
= 0; i
< len
; i
++) {
1915 ret
= ov5670_write_reg(ov5670
, regs
[i
].address
, 1, regs
[i
].val
);
1917 dev_err_ratelimited(
1919 "Failed to write reg 0x%4.4x. error = %d\n",
1920 regs
[i
].address
, ret
);
1929 static int ov5670_write_reg_list(struct ov5670
*ov5670
,
1930 const struct ov5670_reg_list
*r_list
)
1932 return ov5670_write_regs(ov5670
, r_list
->regs
, r_list
->num_of_regs
);
1935 /* Open sub-device */
1936 static int ov5670_open(struct v4l2_subdev
*sd
, struct v4l2_subdev_fh
*fh
)
1938 struct ov5670
*ov5670
= to_ov5670(sd
);
1939 struct v4l2_mbus_framefmt
*try_fmt
=
1940 v4l2_subdev_get_try_format(sd
, fh
->pad
, 0);
1942 mutex_lock(&ov5670
->mutex
);
1944 /* Initialize try_fmt */
1945 try_fmt
->width
= ov5670
->cur_mode
->width
;
1946 try_fmt
->height
= ov5670
->cur_mode
->height
;
1947 try_fmt
->code
= MEDIA_BUS_FMT_SGRBG10_1X10
;
1948 try_fmt
->field
= V4L2_FIELD_NONE
;
1950 /* No crop or compose */
1951 mutex_unlock(&ov5670
->mutex
);
1956 static int ov5670_update_digital_gain(struct ov5670
*ov5670
, u32 d_gain
)
1960 ret
= ov5670_write_reg(ov5670
, OV5670_REG_R_DGTL_GAIN
,
1961 OV5670_REG_VALUE_16BIT
, d_gain
);
1965 ret
= ov5670_write_reg(ov5670
, OV5670_REG_G_DGTL_GAIN
,
1966 OV5670_REG_VALUE_16BIT
, d_gain
);
1970 return ov5670_write_reg(ov5670
, OV5670_REG_B_DGTL_GAIN
,
1971 OV5670_REG_VALUE_16BIT
, d_gain
);
1974 static int ov5670_enable_test_pattern(struct ov5670
*ov5670
, u32 pattern
)
1979 /* Set the bayer order that we support */
1980 ret
= ov5670_write_reg(ov5670
, OV5670_REG_TEST_PATTERN_CTRL
,
1981 OV5670_REG_VALUE_08BIT
, 0);
1985 ret
= ov5670_read_reg(ov5670
, OV5670_REG_TEST_PATTERN
,
1986 OV5670_REG_VALUE_08BIT
, &val
);
1991 val
|= OV5670_TEST_PATTERN_ENABLE
;
1993 val
&= ~OV5670_TEST_PATTERN_ENABLE
;
1995 return ov5670_write_reg(ov5670
, OV5670_REG_TEST_PATTERN
,
1996 OV5670_REG_VALUE_08BIT
, val
);
1999 /* Initialize control handlers */
2000 static int ov5670_set_ctrl(struct v4l2_ctrl
*ctrl
)
2002 struct ov5670
*ov5670
= container_of(ctrl
->handler
,
2003 struct ov5670
, ctrl_handler
);
2004 struct i2c_client
*client
= v4l2_get_subdevdata(&ov5670
->sd
);
2008 /* Propagate change of current control to all related controls */
2010 case V4L2_CID_VBLANK
:
2011 /* Update max exposure while meeting expected vblanking */
2012 max
= ov5670
->cur_mode
->height
+ ctrl
->val
- 8;
2013 __v4l2_ctrl_modify_range(ov5670
->exposure
,
2014 ov5670
->exposure
->minimum
, max
,
2015 ov5670
->exposure
->step
, max
);
2019 /* V4L2 controls values will be applied only when power is already up */
2020 if (!pm_runtime_get_if_in_use(&client
->dev
))
2024 case V4L2_CID_ANALOGUE_GAIN
:
2025 ret
= ov5670_write_reg(ov5670
, OV5670_REG_ANALOG_GAIN
,
2026 OV5670_REG_VALUE_16BIT
, ctrl
->val
);
2028 case V4L2_CID_DIGITAL_GAIN
:
2029 ret
= ov5670_update_digital_gain(ov5670
, ctrl
->val
);
2031 case V4L2_CID_EXPOSURE
:
2032 /* 4 least significant bits of expsoure are fractional part */
2033 ret
= ov5670_write_reg(ov5670
, OV5670_REG_EXPOSURE
,
2034 OV5670_REG_VALUE_24BIT
, ctrl
->val
<< 4);
2036 case V4L2_CID_VBLANK
:
2037 /* Update VTS that meets expected vertical blanking */
2038 ret
= ov5670_write_reg(ov5670
, OV5670_REG_VTS
,
2039 OV5670_REG_VALUE_16BIT
,
2040 ov5670
->cur_mode
->height
+ ctrl
->val
);
2042 case V4L2_CID_TEST_PATTERN
:
2043 ret
= ov5670_enable_test_pattern(ov5670
, ctrl
->val
);
2046 dev_info(&client
->dev
, "%s Unhandled id:0x%x, val:0x%x\n",
2047 __func__
, ctrl
->id
, ctrl
->val
);
2051 pm_runtime_put(&client
->dev
);
2056 static const struct v4l2_ctrl_ops ov5670_ctrl_ops
= {
2057 .s_ctrl
= ov5670_set_ctrl
,
2060 /* Initialize control handlers */
2061 static int ov5670_init_controls(struct ov5670
*ov5670
)
2063 struct i2c_client
*client
= v4l2_get_subdevdata(&ov5670
->sd
);
2064 struct v4l2_fwnode_device_properties props
;
2065 struct v4l2_ctrl_handler
*ctrl_hdlr
;
2072 ctrl_hdlr
= &ov5670
->ctrl_handler
;
2073 ret
= v4l2_ctrl_handler_init(ctrl_hdlr
, 10);
2077 ctrl_hdlr
->lock
= &ov5670
->mutex
;
2078 ov5670
->link_freq
= v4l2_ctrl_new_int_menu(ctrl_hdlr
,
2081 0, 0, link_freq_menu_items
);
2082 if (ov5670
->link_freq
)
2083 ov5670
->link_freq
->flags
|= V4L2_CTRL_FLAG_READ_ONLY
;
2085 /* By default, V4L2_CID_PIXEL_RATE is read only */
2086 ov5670
->pixel_rate
= v4l2_ctrl_new_std(ctrl_hdlr
, &ov5670_ctrl_ops
,
2087 V4L2_CID_PIXEL_RATE
, 0,
2088 link_freq_configs
[0].pixel_rate
,
2090 link_freq_configs
[0].pixel_rate
);
2092 vblank_max
= OV5670_VTS_MAX
- ov5670
->cur_mode
->height
;
2093 vblank_def
= ov5670
->cur_mode
->vts_def
- ov5670
->cur_mode
->height
;
2094 vblank_min
= ov5670
->cur_mode
->vts_min
- ov5670
->cur_mode
->height
;
2095 ov5670
->vblank
= v4l2_ctrl_new_std(ctrl_hdlr
, &ov5670_ctrl_ops
,
2096 V4L2_CID_VBLANK
, vblank_min
,
2097 vblank_max
, 1, vblank_def
);
2099 ov5670
->hblank
= v4l2_ctrl_new_std(
2100 ctrl_hdlr
, &ov5670_ctrl_ops
, V4L2_CID_HBLANK
,
2101 OV5670_FIXED_PPL
- ov5670
->cur_mode
->width
,
2102 OV5670_FIXED_PPL
- ov5670
->cur_mode
->width
, 1,
2103 OV5670_FIXED_PPL
- ov5670
->cur_mode
->width
);
2105 ov5670
->hblank
->flags
|= V4L2_CTRL_FLAG_READ_ONLY
;
2107 /* Get min, max, step, default from sensor */
2108 v4l2_ctrl_new_std(ctrl_hdlr
, &ov5670_ctrl_ops
, V4L2_CID_ANALOGUE_GAIN
,
2109 ANALOG_GAIN_MIN
, ANALOG_GAIN_MAX
, ANALOG_GAIN_STEP
,
2110 ANALOG_GAIN_DEFAULT
);
2113 v4l2_ctrl_new_std(ctrl_hdlr
, &ov5670_ctrl_ops
, V4L2_CID_DIGITAL_GAIN
,
2114 OV5670_DGTL_GAIN_MIN
, OV5670_DGTL_GAIN_MAX
,
2115 OV5670_DGTL_GAIN_STEP
, OV5670_DGTL_GAIN_DEFAULT
);
2117 /* Get min, max, step, default from sensor */
2118 exposure_max
= ov5670
->cur_mode
->vts_def
- 8;
2119 ov5670
->exposure
= v4l2_ctrl_new_std(ctrl_hdlr
, &ov5670_ctrl_ops
,
2121 OV5670_EXPOSURE_MIN
,
2122 exposure_max
, OV5670_EXPOSURE_STEP
,
2125 v4l2_ctrl_new_std_menu_items(ctrl_hdlr
, &ov5670_ctrl_ops
,
2126 V4L2_CID_TEST_PATTERN
,
2127 ARRAY_SIZE(ov5670_test_pattern_menu
) - 1,
2128 0, 0, ov5670_test_pattern_menu
);
2130 if (ctrl_hdlr
->error
) {
2131 ret
= ctrl_hdlr
->error
;
2135 ret
= v4l2_fwnode_device_parse(&client
->dev
, &props
);
2139 ret
= v4l2_ctrl_new_fwnode_properties(ctrl_hdlr
, &ov5670_ctrl_ops
,
2144 ov5670
->sd
.ctrl_handler
= ctrl_hdlr
;
2149 v4l2_ctrl_handler_free(ctrl_hdlr
);
2154 static int ov5670_enum_mbus_code(struct v4l2_subdev
*sd
,
2155 struct v4l2_subdev_pad_config
*cfg
,
2156 struct v4l2_subdev_mbus_code_enum
*code
)
2158 /* Only one bayer order GRBG is supported */
2159 if (code
->index
> 0)
2162 code
->code
= MEDIA_BUS_FMT_SGRBG10_1X10
;
2167 static int ov5670_enum_frame_size(struct v4l2_subdev
*sd
,
2168 struct v4l2_subdev_pad_config
*cfg
,
2169 struct v4l2_subdev_frame_size_enum
*fse
)
2171 if (fse
->index
>= ARRAY_SIZE(supported_modes
))
2174 if (fse
->code
!= MEDIA_BUS_FMT_SGRBG10_1X10
)
2177 fse
->min_width
= supported_modes
[fse
->index
].width
;
2178 fse
->max_width
= fse
->min_width
;
2179 fse
->min_height
= supported_modes
[fse
->index
].height
;
2180 fse
->max_height
= fse
->min_height
;
2185 static void ov5670_update_pad_format(const struct ov5670_mode
*mode
,
2186 struct v4l2_subdev_format
*fmt
)
2188 fmt
->format
.width
= mode
->width
;
2189 fmt
->format
.height
= mode
->height
;
2190 fmt
->format
.code
= MEDIA_BUS_FMT_SGRBG10_1X10
;
2191 fmt
->format
.field
= V4L2_FIELD_NONE
;
2194 static int ov5670_do_get_pad_format(struct ov5670
*ov5670
,
2195 struct v4l2_subdev_pad_config
*cfg
,
2196 struct v4l2_subdev_format
*fmt
)
2198 if (fmt
->which
== V4L2_SUBDEV_FORMAT_TRY
)
2199 fmt
->format
= *v4l2_subdev_get_try_format(&ov5670
->sd
, cfg
,
2202 ov5670_update_pad_format(ov5670
->cur_mode
, fmt
);
2207 static int ov5670_get_pad_format(struct v4l2_subdev
*sd
,
2208 struct v4l2_subdev_pad_config
*cfg
,
2209 struct v4l2_subdev_format
*fmt
)
2211 struct ov5670
*ov5670
= to_ov5670(sd
);
2214 mutex_lock(&ov5670
->mutex
);
2215 ret
= ov5670_do_get_pad_format(ov5670
, cfg
, fmt
);
2216 mutex_unlock(&ov5670
->mutex
);
2221 static int ov5670_set_pad_format(struct v4l2_subdev
*sd
,
2222 struct v4l2_subdev_pad_config
*cfg
,
2223 struct v4l2_subdev_format
*fmt
)
2225 struct ov5670
*ov5670
= to_ov5670(sd
);
2226 const struct ov5670_mode
*mode
;
2230 mutex_lock(&ov5670
->mutex
);
2232 fmt
->format
.code
= MEDIA_BUS_FMT_SGRBG10_1X10
;
2234 mode
= v4l2_find_nearest_size(supported_modes
,
2235 ARRAY_SIZE(supported_modes
),
2237 fmt
->format
.width
, fmt
->format
.height
);
2238 ov5670_update_pad_format(mode
, fmt
);
2239 if (fmt
->which
== V4L2_SUBDEV_FORMAT_TRY
) {
2240 *v4l2_subdev_get_try_format(sd
, cfg
, fmt
->pad
) = fmt
->format
;
2242 ov5670
->cur_mode
= mode
;
2243 __v4l2_ctrl_s_ctrl(ov5670
->link_freq
, mode
->link_freq_index
);
2244 __v4l2_ctrl_s_ctrl_int64(
2246 link_freq_configs
[mode
->link_freq_index
].pixel_rate
);
2247 /* Update limits and set FPS to default */
2248 vblank_def
= ov5670
->cur_mode
->vts_def
-
2249 ov5670
->cur_mode
->height
;
2250 __v4l2_ctrl_modify_range(
2252 ov5670
->cur_mode
->vts_min
- ov5670
->cur_mode
->height
,
2253 OV5670_VTS_MAX
- ov5670
->cur_mode
->height
, 1,
2255 __v4l2_ctrl_s_ctrl(ov5670
->vblank
, vblank_def
);
2256 h_blank
= OV5670_FIXED_PPL
- ov5670
->cur_mode
->width
;
2257 __v4l2_ctrl_modify_range(ov5670
->hblank
, h_blank
, h_blank
, 1,
2261 mutex_unlock(&ov5670
->mutex
);
2266 static int ov5670_get_skip_frames(struct v4l2_subdev
*sd
, u32
*frames
)
2268 *frames
= OV5670_NUM_OF_SKIP_FRAMES
;
2273 /* Prepare streaming by writing default values and customized values */
2274 static int ov5670_start_streaming(struct ov5670
*ov5670
)
2276 struct i2c_client
*client
= v4l2_get_subdevdata(&ov5670
->sd
);
2277 const struct ov5670_reg_list
*reg_list
;
2278 int link_freq_index
;
2281 /* Get out of from software reset */
2282 ret
= ov5670_write_reg(ov5670
, OV5670_REG_SOFTWARE_RST
,
2283 OV5670_REG_VALUE_08BIT
, OV5670_SOFTWARE_RST
);
2285 dev_err(&client
->dev
, "%s failed to set powerup registers\n",
2291 link_freq_index
= ov5670
->cur_mode
->link_freq_index
;
2292 reg_list
= &link_freq_configs
[link_freq_index
].reg_list
;
2293 ret
= ov5670_write_reg_list(ov5670
, reg_list
);
2295 dev_err(&client
->dev
, "%s failed to set plls\n", __func__
);
2299 /* Apply default values of current mode */
2300 reg_list
= &ov5670
->cur_mode
->reg_list
;
2301 ret
= ov5670_write_reg_list(ov5670
, reg_list
);
2303 dev_err(&client
->dev
, "%s failed to set mode\n", __func__
);
2307 ret
= __v4l2_ctrl_handler_setup(ov5670
->sd
.ctrl_handler
);
2311 /* Write stream on list */
2312 ret
= ov5670_write_reg(ov5670
, OV5670_REG_MODE_SELECT
,
2313 OV5670_REG_VALUE_08BIT
, OV5670_MODE_STREAMING
);
2315 dev_err(&client
->dev
, "%s failed to set stream\n", __func__
);
2322 static int ov5670_stop_streaming(struct ov5670
*ov5670
)
2324 struct i2c_client
*client
= v4l2_get_subdevdata(&ov5670
->sd
);
2327 ret
= ov5670_write_reg(ov5670
, OV5670_REG_MODE_SELECT
,
2328 OV5670_REG_VALUE_08BIT
, OV5670_MODE_STANDBY
);
2330 dev_err(&client
->dev
, "%s failed to set stream\n", __func__
);
2332 /* Return success even if it was an error, as there is nothing the
2333 * caller can do about it.
2338 static int ov5670_set_stream(struct v4l2_subdev
*sd
, int enable
)
2340 struct ov5670
*ov5670
= to_ov5670(sd
);
2341 struct i2c_client
*client
= v4l2_get_subdevdata(sd
);
2344 mutex_lock(&ov5670
->mutex
);
2345 if (ov5670
->streaming
== enable
)
2346 goto unlock_and_return
;
2349 ret
= pm_runtime_get_sync(&client
->dev
);
2351 pm_runtime_put_noidle(&client
->dev
);
2352 goto unlock_and_return
;
2355 ret
= ov5670_start_streaming(ov5670
);
2359 ret
= ov5670_stop_streaming(ov5670
);
2360 pm_runtime_put(&client
->dev
);
2362 ov5670
->streaming
= enable
;
2363 goto unlock_and_return
;
2366 pm_runtime_put(&client
->dev
);
2369 mutex_unlock(&ov5670
->mutex
);
2374 static int __maybe_unused
ov5670_suspend(struct device
*dev
)
2376 struct v4l2_subdev
*sd
= dev_get_drvdata(dev
);
2377 struct ov5670
*ov5670
= to_ov5670(sd
);
2379 if (ov5670
->streaming
)
2380 ov5670_stop_streaming(ov5670
);
2385 static int __maybe_unused
ov5670_resume(struct device
*dev
)
2387 struct v4l2_subdev
*sd
= dev_get_drvdata(dev
);
2388 struct ov5670
*ov5670
= to_ov5670(sd
);
2391 if (ov5670
->streaming
) {
2392 ret
= ov5670_start_streaming(ov5670
);
2394 ov5670_stop_streaming(ov5670
);
2402 /* Verify chip ID */
2403 static int ov5670_identify_module(struct ov5670
*ov5670
)
2405 struct i2c_client
*client
= v4l2_get_subdevdata(&ov5670
->sd
);
2409 ret
= ov5670_read_reg(ov5670
, OV5670_REG_CHIP_ID
,
2410 OV5670_REG_VALUE_24BIT
, &val
);
2414 if (val
!= OV5670_CHIP_ID
) {
2415 dev_err(&client
->dev
, "chip id mismatch: %x!=%x\n",
2416 OV5670_CHIP_ID
, val
);
2423 static const struct v4l2_subdev_video_ops ov5670_video_ops
= {
2424 .s_stream
= ov5670_set_stream
,
2427 static const struct v4l2_subdev_pad_ops ov5670_pad_ops
= {
2428 .enum_mbus_code
= ov5670_enum_mbus_code
,
2429 .get_fmt
= ov5670_get_pad_format
,
2430 .set_fmt
= ov5670_set_pad_format
,
2431 .enum_frame_size
= ov5670_enum_frame_size
,
2434 static const struct v4l2_subdev_sensor_ops ov5670_sensor_ops
= {
2435 .g_skip_frames
= ov5670_get_skip_frames
,
2438 static const struct v4l2_subdev_ops ov5670_subdev_ops
= {
2439 .video
= &ov5670_video_ops
,
2440 .pad
= &ov5670_pad_ops
,
2441 .sensor
= &ov5670_sensor_ops
,
2444 static const struct media_entity_operations ov5670_subdev_entity_ops
= {
2445 .link_validate
= v4l2_subdev_link_validate
,
2448 static const struct v4l2_subdev_internal_ops ov5670_internal_ops
= {
2449 .open
= ov5670_open
,
2452 static int ov5670_probe(struct i2c_client
*client
)
2454 struct ov5670
*ov5670
;
2455 const char *err_msg
;
2459 device_property_read_u32(&client
->dev
, "clock-frequency", &input_clk
);
2460 if (input_clk
!= 19200000)
2463 ov5670
= devm_kzalloc(&client
->dev
, sizeof(*ov5670
), GFP_KERNEL
);
2466 err_msg
= "devm_kzalloc() error";
2470 /* Initialize subdev */
2471 v4l2_i2c_subdev_init(&ov5670
->sd
, client
, &ov5670_subdev_ops
);
2473 /* Check module identity */
2474 ret
= ov5670_identify_module(ov5670
);
2476 err_msg
= "ov5670_identify_module() error";
2480 mutex_init(&ov5670
->mutex
);
2482 /* Set default mode to max resolution */
2483 ov5670
->cur_mode
= &supported_modes
[0];
2485 ret
= ov5670_init_controls(ov5670
);
2487 err_msg
= "ov5670_init_controls() error";
2488 goto error_mutex_destroy
;
2491 ov5670
->sd
.internal_ops
= &ov5670_internal_ops
;
2492 ov5670
->sd
.flags
|= V4L2_SUBDEV_FL_HAS_DEVNODE
;
2493 ov5670
->sd
.entity
.ops
= &ov5670_subdev_entity_ops
;
2494 ov5670
->sd
.entity
.function
= MEDIA_ENT_F_CAM_SENSOR
;
2496 /* Source pad initialization */
2497 ov5670
->pad
.flags
= MEDIA_PAD_FL_SOURCE
;
2498 ret
= media_entity_pads_init(&ov5670
->sd
.entity
, 1, &ov5670
->pad
);
2500 err_msg
= "media_entity_pads_init() error";
2501 goto error_handler_free
;
2504 /* Async register for subdev */
2505 ret
= v4l2_async_register_subdev_sensor_common(&ov5670
->sd
);
2507 err_msg
= "v4l2_async_register_subdev() error";
2508 goto error_entity_cleanup
;
2511 ov5670
->streaming
= false;
2514 * Device is already turned on by i2c-core with ACPI domain PM.
2515 * Enable runtime PM and turn off the device.
2517 pm_runtime_set_active(&client
->dev
);
2518 pm_runtime_enable(&client
->dev
);
2519 pm_runtime_idle(&client
->dev
);
2523 error_entity_cleanup
:
2524 media_entity_cleanup(&ov5670
->sd
.entity
);
2527 v4l2_ctrl_handler_free(ov5670
->sd
.ctrl_handler
);
2529 error_mutex_destroy
:
2530 mutex_destroy(&ov5670
->mutex
);
2533 dev_err(&client
->dev
, "%s: %s %d\n", __func__
, err_msg
, ret
);
2538 static int ov5670_remove(struct i2c_client
*client
)
2540 struct v4l2_subdev
*sd
= i2c_get_clientdata(client
);
2541 struct ov5670
*ov5670
= to_ov5670(sd
);
2543 v4l2_async_unregister_subdev(sd
);
2544 media_entity_cleanup(&sd
->entity
);
2545 v4l2_ctrl_handler_free(sd
->ctrl_handler
);
2546 mutex_destroy(&ov5670
->mutex
);
2548 pm_runtime_disable(&client
->dev
);
2553 static const struct dev_pm_ops ov5670_pm_ops
= {
2554 SET_SYSTEM_SLEEP_PM_OPS(ov5670_suspend
, ov5670_resume
)
2558 static const struct acpi_device_id ov5670_acpi_ids
[] = {
2563 MODULE_DEVICE_TABLE(acpi
, ov5670_acpi_ids
);
2566 static struct i2c_driver ov5670_i2c_driver
= {
2569 .pm
= &ov5670_pm_ops
,
2570 .acpi_match_table
= ACPI_PTR(ov5670_acpi_ids
),
2572 .probe_new
= ov5670_probe
,
2573 .remove
= ov5670_remove
,
2576 module_i2c_driver(ov5670_i2c_driver
);
2578 MODULE_AUTHOR("Rapolu, Chiranjeevi <chiranjeevi.rapolu@intel.com>");
2579 MODULE_AUTHOR("Yang, Hyungwoo <hyungwoo.yang@intel.com>");
2580 MODULE_DESCRIPTION("Omnivision ov5670 sensor driver");
2581 MODULE_LICENSE("GPL v2");