1 // SPDX-License-Identifier: GPL-2.0-or-later
3 * Copyright 2020 Kévin L'hôpital <kevin.lhopital@bootlin.com>
4 * Copyright 2020 Bootlin
5 * Author: Paul Kocialkowski <paul.kocialkowski@bootlin.com>
9 #include <linux/delay.h>
10 #include <linux/device.h>
11 #include <linux/i2c.h>
12 #include <linux/mod_devicetable.h>
13 #include <linux/module.h>
14 #include <linux/of_graph.h>
15 #include <linux/pm_runtime.h>
16 #include <linux/regulator/consumer.h>
17 #include <linux/videodev2.h>
18 #include <media/v4l2-ctrls.h>
19 #include <media/v4l2-device.h>
20 #include <media/v4l2-fwnode.h>
21 #include <media/v4l2-image-sizes.h>
22 #include <media/v4l2-mediabus.h>
24 /* Register definitions */
28 #define OV8865_SW_STANDBY_REG 0x100
29 #define OV8865_SW_STANDBY_STREAM_ON BIT(0)
31 #define OV8865_SW_RESET_REG 0x103
32 #define OV8865_SW_RESET_RESET BIT(0)
34 #define OV8865_PLL_CTRL0_REG 0x300
35 #define OV8865_PLL_CTRL0_PRE_DIV(v) ((v) & GENMASK(2, 0))
36 #define OV8865_PLL_CTRL1_REG 0x301
37 #define OV8865_PLL_CTRL1_MUL_H(v) (((v) & GENMASK(9, 8)) >> 8)
38 #define OV8865_PLL_CTRL2_REG 0x302
39 #define OV8865_PLL_CTRL2_MUL_L(v) ((v) & GENMASK(7, 0))
40 #define OV8865_PLL_CTRL3_REG 0x303
41 #define OV8865_PLL_CTRL3_M_DIV(v) (((v) - 1) & GENMASK(3, 0))
42 #define OV8865_PLL_CTRL4_REG 0x304
43 #define OV8865_PLL_CTRL4_MIPI_DIV(v) ((v) & GENMASK(1, 0))
44 #define OV8865_PLL_CTRL5_REG 0x305
45 #define OV8865_PLL_CTRL5_SYS_PRE_DIV(v) ((v) & GENMASK(1, 0))
46 #define OV8865_PLL_CTRL6_REG 0x306
47 #define OV8865_PLL_CTRL6_SYS_DIV(v) (((v) - 1) & BIT(0))
49 #define OV8865_PLL_CTRL8_REG 0x308
50 #define OV8865_PLL_CTRL9_REG 0x309
51 #define OV8865_PLL_CTRLA_REG 0x30a
52 #define OV8865_PLL_CTRLA_PRE_DIV_HALF(v) (((v) - 1) & BIT(0))
53 #define OV8865_PLL_CTRLB_REG 0x30b
54 #define OV8865_PLL_CTRLB_PRE_DIV(v) ((v) & GENMASK(2, 0))
55 #define OV8865_PLL_CTRLC_REG 0x30c
56 #define OV8865_PLL_CTRLC_MUL_H(v) (((v) & GENMASK(9, 8)) >> 8)
57 #define OV8865_PLL_CTRLD_REG 0x30d
58 #define OV8865_PLL_CTRLD_MUL_L(v) ((v) & GENMASK(7, 0))
59 #define OV8865_PLL_CTRLE_REG 0x30e
60 #define OV8865_PLL_CTRLE_SYS_DIV(v) ((v) & GENMASK(2, 0))
61 #define OV8865_PLL_CTRLF_REG 0x30f
62 #define OV8865_PLL_CTRLF_SYS_PRE_DIV(v) (((v) - 1) & GENMASK(3, 0))
63 #define OV8865_PLL_CTRL10_REG 0x310
64 #define OV8865_PLL_CTRL11_REG 0x311
65 #define OV8865_PLL_CTRL12_REG 0x312
66 #define OV8865_PLL_CTRL12_PRE_DIV_HALF(v) ((((v) - 1) << 4) & BIT(4))
67 #define OV8865_PLL_CTRL12_DAC_DIV(v) (((v) - 1) & GENMASK(3, 0))
69 #define OV8865_PLL_CTRL1B_REG 0x31b
70 #define OV8865_PLL_CTRL1C_REG 0x31c
72 #define OV8865_PLL_CTRL1E_REG 0x31e
73 #define OV8865_PLL_CTRL1E_PLL1_NO_LAT BIT(3)
75 #define OV8865_PAD_OEN0_REG 0x3000
77 #define OV8865_PAD_OEN2_REG 0x3002
79 #define OV8865_CLK_RST5_REG 0x3005
81 #define OV8865_CHIP_ID_HH_REG 0x300a
82 #define OV8865_CHIP_ID_HH_VALUE 0x00
83 #define OV8865_CHIP_ID_H_REG 0x300b
84 #define OV8865_CHIP_ID_H_VALUE 0x88
85 #define OV8865_CHIP_ID_L_REG 0x300c
86 #define OV8865_CHIP_ID_L_VALUE 0x65
87 #define OV8865_PAD_OUT2_REG 0x300d
89 #define OV8865_PAD_SEL2_REG 0x3010
90 #define OV8865_PAD_PK_REG 0x3011
91 #define OV8865_PAD_PK_DRIVE_STRENGTH_1X (0 << 5)
92 #define OV8865_PAD_PK_DRIVE_STRENGTH_2X (1 << 5)
93 #define OV8865_PAD_PK_DRIVE_STRENGTH_3X (2 << 5)
94 #define OV8865_PAD_PK_DRIVE_STRENGTH_4X (3 << 5)
96 #define OV8865_PUMP_CLK_DIV_REG 0x3015
97 #define OV8865_PUMP_CLK_DIV_PUMP_N(v) (((v) << 4) & GENMASK(6, 4))
98 #define OV8865_PUMP_CLK_DIV_PUMP_P(v) ((v) & GENMASK(2, 0))
100 #define OV8865_MIPI_SC_CTRL0_REG 0x3018
101 #define OV8865_MIPI_SC_CTRL0_LANES(v) ((((v) - 1) << 5) & \
103 #define OV8865_MIPI_SC_CTRL0_MIPI_EN BIT(4)
104 #define OV8865_MIPI_SC_CTRL0_UNKNOWN BIT(1)
105 #define OV8865_MIPI_SC_CTRL0_LANES_PD_MIPI BIT(0)
106 #define OV8865_MIPI_SC_CTRL1_REG 0x3019
107 #define OV8865_CLK_RST0_REG 0x301a
108 #define OV8865_CLK_RST1_REG 0x301b
109 #define OV8865_CLK_RST2_REG 0x301c
110 #define OV8865_CLK_RST3_REG 0x301d
111 #define OV8865_CLK_RST4_REG 0x301e
113 #define OV8865_PCLK_SEL_REG 0x3020
114 #define OV8865_PCLK_SEL_PCLK_DIV_MASK BIT(3)
115 #define OV8865_PCLK_SEL_PCLK_DIV(v) ((((v) - 1) << 3) & BIT(3))
117 #define OV8865_MISC_CTRL_REG 0x3021
118 #define OV8865_MIPI_SC_CTRL2_REG 0x3022
119 #define OV8865_MIPI_SC_CTRL2_CLK_LANES_PD_MIPI BIT(1)
120 #define OV8865_MIPI_SC_CTRL2_PD_MIPI_RST_SYNC BIT(0)
122 #define OV8865_MIPI_BIT_SEL_REG 0x3031
123 #define OV8865_MIPI_BIT_SEL(v) (((v) << 0) & GENMASK(4, 0))
124 #define OV8865_CLK_SEL0_REG 0x3032
125 #define OV8865_CLK_SEL0_PLL1_SYS_SEL(v) (((v) << 7) & BIT(7))
126 #define OV8865_CLK_SEL1_REG 0x3033
127 #define OV8865_CLK_SEL1_MIPI_EOF BIT(5)
128 #define OV8865_CLK_SEL1_UNKNOWN BIT(2)
129 #define OV8865_CLK_SEL1_PLL_SCLK_SEL_MASK BIT(1)
130 #define OV8865_CLK_SEL1_PLL_SCLK_SEL(v) (((v) << 1) & BIT(1))
132 #define OV8865_SCLK_CTRL_REG 0x3106
133 #define OV8865_SCLK_CTRL_SCLK_DIV(v) (((v) << 4) & GENMASK(7, 4))
134 #define OV8865_SCLK_CTRL_SCLK_PRE_DIV(v) (((v) << 2) & GENMASK(3, 2))
135 #define OV8865_SCLK_CTRL_UNKNOWN BIT(0)
139 #define OV8865_EXPOSURE_CTRL_HH_REG 0x3500
140 #define OV8865_EXPOSURE_CTRL_HH(v) (((v) & GENMASK(19, 16)) >> 16)
141 #define OV8865_EXPOSURE_CTRL_H_REG 0x3501
142 #define OV8865_EXPOSURE_CTRL_H(v) (((v) & GENMASK(15, 8)) >> 8)
143 #define OV8865_EXPOSURE_CTRL_L_REG 0x3502
144 #define OV8865_EXPOSURE_CTRL_L(v) ((v) & GENMASK(7, 0))
145 #define OV8865_EXPOSURE_GAIN_MANUAL_REG 0x3503
146 #define OV8865_INTEGRATION_TIME_MARGIN 8
148 #define OV8865_GAIN_CTRL_H_REG 0x3508
149 #define OV8865_GAIN_CTRL_H(v) (((v) & GENMASK(12, 8)) >> 8)
150 #define OV8865_GAIN_CTRL_L_REG 0x3509
151 #define OV8865_GAIN_CTRL_L(v) ((v) & GENMASK(7, 0))
155 #define OV8865_CROP_START_X_H_REG 0x3800
156 #define OV8865_CROP_START_X_H(v) (((v) & GENMASK(11, 8)) >> 8)
157 #define OV8865_CROP_START_X_L_REG 0x3801
158 #define OV8865_CROP_START_X_L(v) ((v) & GENMASK(7, 0))
159 #define OV8865_CROP_START_Y_H_REG 0x3802
160 #define OV8865_CROP_START_Y_H(v) (((v) & GENMASK(11, 8)) >> 8)
161 #define OV8865_CROP_START_Y_L_REG 0x3803
162 #define OV8865_CROP_START_Y_L(v) ((v) & GENMASK(7, 0))
163 #define OV8865_CROP_END_X_H_REG 0x3804
164 #define OV8865_CROP_END_X_H(v) (((v) & GENMASK(11, 8)) >> 8)
165 #define OV8865_CROP_END_X_L_REG 0x3805
166 #define OV8865_CROP_END_X_L(v) ((v) & GENMASK(7, 0))
167 #define OV8865_CROP_END_Y_H_REG 0x3806
168 #define OV8865_CROP_END_Y_H(v) (((v) & GENMASK(11, 8)) >> 8)
169 #define OV8865_CROP_END_Y_L_REG 0x3807
170 #define OV8865_CROP_END_Y_L(v) ((v) & GENMASK(7, 0))
171 #define OV8865_OUTPUT_SIZE_X_H_REG 0x3808
172 #define OV8865_OUTPUT_SIZE_X_H(v) (((v) & GENMASK(11, 8)) >> 8)
173 #define OV8865_OUTPUT_SIZE_X_L_REG 0x3809
174 #define OV8865_OUTPUT_SIZE_X_L(v) ((v) & GENMASK(7, 0))
175 #define OV8865_OUTPUT_SIZE_Y_H_REG 0x380a
176 #define OV8865_OUTPUT_SIZE_Y_H(v) (((v) & GENMASK(11, 8)) >> 8)
177 #define OV8865_OUTPUT_SIZE_Y_L_REG 0x380b
178 #define OV8865_OUTPUT_SIZE_Y_L(v) ((v) & GENMASK(7, 0))
179 #define OV8865_HTS_H_REG 0x380c
180 #define OV8865_HTS_H(v) (((v) & GENMASK(11, 8)) >> 8)
181 #define OV8865_HTS_L_REG 0x380d
182 #define OV8865_HTS_L(v) ((v) & GENMASK(7, 0))
183 #define OV8865_VTS_H_REG 0x380e
184 #define OV8865_VTS_H(v) (((v) & GENMASK(11, 8)) >> 8)
185 #define OV8865_VTS_L_REG 0x380f
186 #define OV8865_VTS_L(v) ((v) & GENMASK(7, 0))
187 #define OV8865_TIMING_MAX_VTS 0xffff
188 #define OV8865_TIMING_MIN_VTS 0x04
189 #define OV8865_OFFSET_X_H_REG 0x3810
190 #define OV8865_OFFSET_X_H(v) (((v) & GENMASK(15, 8)) >> 8)
191 #define OV8865_OFFSET_X_L_REG 0x3811
192 #define OV8865_OFFSET_X_L(v) ((v) & GENMASK(7, 0))
193 #define OV8865_OFFSET_Y_H_REG 0x3812
194 #define OV8865_OFFSET_Y_H(v) (((v) & GENMASK(14, 8)) >> 8)
195 #define OV8865_OFFSET_Y_L_REG 0x3813
196 #define OV8865_OFFSET_Y_L(v) ((v) & GENMASK(7, 0))
197 #define OV8865_INC_X_ODD_REG 0x3814
198 #define OV8865_INC_X_ODD(v) ((v) & GENMASK(4, 0))
199 #define OV8865_INC_X_EVEN_REG 0x3815
200 #define OV8865_INC_X_EVEN(v) ((v) & GENMASK(4, 0))
201 #define OV8865_VSYNC_START_H_REG 0x3816
202 #define OV8865_VSYNC_START_H(v) (((v) & GENMASK(15, 8)) >> 8)
203 #define OV8865_VSYNC_START_L_REG 0x3817
204 #define OV8865_VSYNC_START_L(v) ((v) & GENMASK(7, 0))
205 #define OV8865_VSYNC_END_H_REG 0x3818
206 #define OV8865_VSYNC_END_H(v) (((v) & GENMASK(15, 8)) >> 8)
207 #define OV8865_VSYNC_END_L_REG 0x3819
208 #define OV8865_VSYNC_END_L(v) ((v) & GENMASK(7, 0))
209 #define OV8865_HSYNC_FIRST_H_REG 0x381a
210 #define OV8865_HSYNC_FIRST_H(v) (((v) & GENMASK(15, 8)) >> 8)
211 #define OV8865_HSYNC_FIRST_L_REG 0x381b
212 #define OV8865_HSYNC_FIRST_L(v) ((v) & GENMASK(7, 0))
214 #define OV8865_FORMAT1_REG 0x3820
215 #define OV8865_FORMAT1_FLIP_VERT_ISP_EN BIT(2)
216 #define OV8865_FORMAT1_FLIP_VERT_SENSOR_EN BIT(1)
217 #define OV8865_FORMAT2_REG 0x3821
218 #define OV8865_FORMAT2_HSYNC_EN BIT(6)
219 #define OV8865_FORMAT2_FST_VBIN_EN BIT(5)
220 #define OV8865_FORMAT2_FST_HBIN_EN BIT(4)
221 #define OV8865_FORMAT2_ISP_HORZ_VAR2_EN BIT(3)
222 #define OV8865_FORMAT2_FLIP_HORZ_ISP_EN BIT(2)
223 #define OV8865_FORMAT2_FLIP_HORZ_SENSOR_EN BIT(1)
224 #define OV8865_FORMAT2_SYNC_HBIN_EN BIT(0)
226 #define OV8865_INC_Y_ODD_REG 0x382a
227 #define OV8865_INC_Y_ODD(v) ((v) & GENMASK(4, 0))
228 #define OV8865_INC_Y_EVEN_REG 0x382b
229 #define OV8865_INC_Y_EVEN(v) ((v) & GENMASK(4, 0))
231 #define OV8865_ABLC_NUM_REG 0x3830
232 #define OV8865_ABLC_NUM(v) ((v) & GENMASK(4, 0))
234 #define OV8865_ZLINE_NUM_REG 0x3836
235 #define OV8865_ZLINE_NUM(v) ((v) & GENMASK(4, 0))
237 #define OV8865_AUTO_SIZE_CTRL_REG 0x3841
238 #define OV8865_AUTO_SIZE_CTRL_OFFSET_Y_REG BIT(5)
239 #define OV8865_AUTO_SIZE_CTRL_OFFSET_X_REG BIT(4)
240 #define OV8865_AUTO_SIZE_CTRL_CROP_END_Y_REG BIT(3)
241 #define OV8865_AUTO_SIZE_CTRL_CROP_END_X_REG BIT(2)
242 #define OV8865_AUTO_SIZE_CTRL_CROP_START_Y_REG BIT(1)
243 #define OV8865_AUTO_SIZE_CTRL_CROP_START_X_REG BIT(0)
244 #define OV8865_AUTO_SIZE_X_OFFSET_H_REG 0x3842
245 #define OV8865_AUTO_SIZE_X_OFFSET_L_REG 0x3843
246 #define OV8865_AUTO_SIZE_Y_OFFSET_H_REG 0x3844
247 #define OV8865_AUTO_SIZE_Y_OFFSET_L_REG 0x3845
248 #define OV8865_AUTO_SIZE_BOUNDARIES_REG 0x3846
249 #define OV8865_AUTO_SIZE_BOUNDARIES_Y(v) (((v) << 4) & GENMASK(7, 4))
250 #define OV8865_AUTO_SIZE_BOUNDARIES_X(v) ((v) & GENMASK(3, 0))
254 #define OV8865_PSRAM_CTRL8_REG 0x3f08
258 #define OV8865_BLC_CTRL0_REG 0x4000
259 #define OV8865_BLC_CTRL0_TRIG_RANGE_EN BIT(7)
260 #define OV8865_BLC_CTRL0_TRIG_FORMAT_EN BIT(6)
261 #define OV8865_BLC_CTRL0_TRIG_GAIN_EN BIT(5)
262 #define OV8865_BLC_CTRL0_TRIG_EXPOSURE_EN BIT(4)
263 #define OV8865_BLC_CTRL0_TRIG_MANUAL_EN BIT(3)
264 #define OV8865_BLC_CTRL0_FREEZE_EN BIT(2)
265 #define OV8865_BLC_CTRL0_ALWAYS_EN BIT(1)
266 #define OV8865_BLC_CTRL0_FILTER_EN BIT(0)
267 #define OV8865_BLC_CTRL1_REG 0x4001
268 #define OV8865_BLC_CTRL1_DITHER_EN BIT(7)
269 #define OV8865_BLC_CTRL1_ZERO_LINE_DIFF_EN BIT(6)
270 #define OV8865_BLC_CTRL1_COL_SHIFT_256 (0 << 4)
271 #define OV8865_BLC_CTRL1_COL_SHIFT_128 (1 << 4)
272 #define OV8865_BLC_CTRL1_COL_SHIFT_64 (2 << 4)
273 #define OV8865_BLC_CTRL1_COL_SHIFT_32 (3 << 4)
274 #define OV8865_BLC_CTRL1_OFFSET_LIMIT_EN BIT(2)
275 #define OV8865_BLC_CTRL1_COLUMN_CANCEL_EN BIT(1)
276 #define OV8865_BLC_CTRL2_REG 0x4002
277 #define OV8865_BLC_CTRL3_REG 0x4003
278 #define OV8865_BLC_CTRL4_REG 0x4004
279 #define OV8865_BLC_CTRL5_REG 0x4005
280 #define OV8865_BLC_CTRL6_REG 0x4006
281 #define OV8865_BLC_CTRL7_REG 0x4007
282 #define OV8865_BLC_CTRL8_REG 0x4008
283 #define OV8865_BLC_CTRL9_REG 0x4009
284 #define OV8865_BLC_CTRLA_REG 0x400a
285 #define OV8865_BLC_CTRLB_REG 0x400b
286 #define OV8865_BLC_CTRLC_REG 0x400c
287 #define OV8865_BLC_CTRLD_REG 0x400d
288 #define OV8865_BLC_CTRLD_OFFSET_TRIGGER(v) ((v) & GENMASK(7, 0))
290 #define OV8865_BLC_CTRL1F_REG 0x401f
291 #define OV8865_BLC_CTRL1F_RB_REVERSE BIT(3)
292 #define OV8865_BLC_CTRL1F_INTERPOL_X_EN BIT(2)
293 #define OV8865_BLC_CTRL1F_INTERPOL_Y_EN BIT(1)
295 #define OV8865_BLC_ANCHOR_LEFT_START_H_REG 0x4020
296 #define OV8865_BLC_ANCHOR_LEFT_START_H(v) (((v) & GENMASK(11, 8)) >> 8)
297 #define OV8865_BLC_ANCHOR_LEFT_START_L_REG 0x4021
298 #define OV8865_BLC_ANCHOR_LEFT_START_L(v) ((v) & GENMASK(7, 0))
299 #define OV8865_BLC_ANCHOR_LEFT_END_H_REG 0x4022
300 #define OV8865_BLC_ANCHOR_LEFT_END_H(v) (((v) & GENMASK(11, 8)) >> 8)
301 #define OV8865_BLC_ANCHOR_LEFT_END_L_REG 0x4023
302 #define OV8865_BLC_ANCHOR_LEFT_END_L(v) ((v) & GENMASK(7, 0))
303 #define OV8865_BLC_ANCHOR_RIGHT_START_H_REG 0x4024
304 #define OV8865_BLC_ANCHOR_RIGHT_START_H(v) (((v) & GENMASK(11, 8)) >> 8)
305 #define OV8865_BLC_ANCHOR_RIGHT_START_L_REG 0x4025
306 #define OV8865_BLC_ANCHOR_RIGHT_START_L(v) ((v) & GENMASK(7, 0))
307 #define OV8865_BLC_ANCHOR_RIGHT_END_H_REG 0x4026
308 #define OV8865_BLC_ANCHOR_RIGHT_END_H(v) (((v) & GENMASK(11, 8)) >> 8)
309 #define OV8865_BLC_ANCHOR_RIGHT_END_L_REG 0x4027
310 #define OV8865_BLC_ANCHOR_RIGHT_END_L(v) ((v) & GENMASK(7, 0))
312 #define OV8865_BLC_TOP_ZLINE_START_REG 0x4028
313 #define OV8865_BLC_TOP_ZLINE_START(v) ((v) & GENMASK(5, 0))
314 #define OV8865_BLC_TOP_ZLINE_NUM_REG 0x4029
315 #define OV8865_BLC_TOP_ZLINE_NUM(v) ((v) & GENMASK(4, 0))
316 #define OV8865_BLC_TOP_BLKLINE_START_REG 0x402a
317 #define OV8865_BLC_TOP_BLKLINE_START(v) ((v) & GENMASK(5, 0))
318 #define OV8865_BLC_TOP_BLKLINE_NUM_REG 0x402b
319 #define OV8865_BLC_TOP_BLKLINE_NUM(v) ((v) & GENMASK(4, 0))
320 #define OV8865_BLC_BOT_ZLINE_START_REG 0x402c
321 #define OV8865_BLC_BOT_ZLINE_START(v) ((v) & GENMASK(5, 0))
322 #define OV8865_BLC_BOT_ZLINE_NUM_REG 0x402d
323 #define OV8865_BLC_BOT_ZLINE_NUM(v) ((v) & GENMASK(4, 0))
324 #define OV8865_BLC_BOT_BLKLINE_START_REG 0x402e
325 #define OV8865_BLC_BOT_BLKLINE_START(v) ((v) & GENMASK(5, 0))
326 #define OV8865_BLC_BOT_BLKLINE_NUM_REG 0x402f
327 #define OV8865_BLC_BOT_BLKLINE_NUM(v) ((v) & GENMASK(4, 0))
329 #define OV8865_BLC_OFFSET_LIMIT_REG 0x4034
330 #define OV8865_BLC_OFFSET_LIMIT(v) ((v) & GENMASK(7, 0))
334 #define OV8865_VFIFO_READ_START_H_REG 0x4600
335 #define OV8865_VFIFO_READ_START_H(v) (((v) & GENMASK(15, 8)) >> 8)
336 #define OV8865_VFIFO_READ_START_L_REG 0x4601
337 #define OV8865_VFIFO_READ_START_L(v) ((v) & GENMASK(7, 0))
341 #define OV8865_MIPI_CTRL0_REG 0x4800
342 #define OV8865_MIPI_CTRL1_REG 0x4801
343 #define OV8865_MIPI_CTRL2_REG 0x4802
344 #define OV8865_MIPI_CTRL3_REG 0x4803
345 #define OV8865_MIPI_CTRL4_REG 0x4804
346 #define OV8865_MIPI_CTRL5_REG 0x4805
347 #define OV8865_MIPI_CTRL6_REG 0x4806
348 #define OV8865_MIPI_CTRL7_REG 0x4807
349 #define OV8865_MIPI_CTRL8_REG 0x4808
351 #define OV8865_MIPI_FCNT_MAX_H_REG 0x4810
352 #define OV8865_MIPI_FCNT_MAX_L_REG 0x4811
354 #define OV8865_MIPI_CTRL13_REG 0x4813
355 #define OV8865_MIPI_CTRL14_REG 0x4814
356 #define OV8865_MIPI_CTRL15_REG 0x4815
357 #define OV8865_MIPI_EMBEDDED_DT_REG 0x4816
359 #define OV8865_MIPI_HS_ZERO_MIN_H_REG 0x4818
360 #define OV8865_MIPI_HS_ZERO_MIN_L_REG 0x4819
361 #define OV8865_MIPI_HS_TRAIL_MIN_H_REG 0x481a
362 #define OV8865_MIPI_HS_TRAIL_MIN_L_REG 0x481b
363 #define OV8865_MIPI_CLK_ZERO_MIN_H_REG 0x481c
364 #define OV8865_MIPI_CLK_ZERO_MIN_L_REG 0x481d
365 #define OV8865_MIPI_CLK_PREPARE_MAX_REG 0x481e
366 #define OV8865_MIPI_CLK_PREPARE_MIN_REG 0x481f
367 #define OV8865_MIPI_CLK_POST_MIN_H_REG 0x4820
368 #define OV8865_MIPI_CLK_POST_MIN_L_REG 0x4821
369 #define OV8865_MIPI_CLK_TRAIL_MIN_H_REG 0x4822
370 #define OV8865_MIPI_CLK_TRAIL_MIN_L_REG 0x4823
371 #define OV8865_MIPI_LPX_P_MIN_H_REG 0x4824
372 #define OV8865_MIPI_LPX_P_MIN_L_REG 0x4825
373 #define OV8865_MIPI_HS_PREPARE_MIN_REG 0x4826
374 #define OV8865_MIPI_HS_PREPARE_MAX_REG 0x4827
375 #define OV8865_MIPI_HS_EXIT_MIN_H_REG 0x4828
376 #define OV8865_MIPI_HS_EXIT_MIN_L_REG 0x4829
377 #define OV8865_MIPI_UI_HS_ZERO_MIN_REG 0x482a
378 #define OV8865_MIPI_UI_HS_TRAIL_MIN_REG 0x482b
379 #define OV8865_MIPI_UI_CLK_ZERO_MIN_REG 0x482c
380 #define OV8865_MIPI_UI_CLK_PREPARE_REG 0x482d
381 #define OV8865_MIPI_UI_CLK_POST_MIN_REG 0x482e
382 #define OV8865_MIPI_UI_CLK_TRAIL_MIN_REG 0x482f
383 #define OV8865_MIPI_UI_LPX_P_MIN_REG 0x4830
384 #define OV8865_MIPI_UI_HS_PREPARE_REG 0x4831
385 #define OV8865_MIPI_UI_HS_EXIT_MIN_REG 0x4832
386 #define OV8865_MIPI_PKT_START_SIZE_REG 0x4833
388 #define OV8865_MIPI_PCLK_PERIOD_REG 0x4837
389 #define OV8865_MIPI_LP_GPIO0_REG 0x4838
390 #define OV8865_MIPI_LP_GPIO1_REG 0x4839
392 #define OV8865_MIPI_CTRL3C_REG 0x483c
393 #define OV8865_MIPI_LP_GPIO4_REG 0x483d
395 #define OV8865_MIPI_CTRL4A_REG 0x484a
396 #define OV8865_MIPI_CTRL4B_REG 0x484b
397 #define OV8865_MIPI_CTRL4C_REG 0x484c
398 #define OV8865_MIPI_LANE_TEST_PATTERN_REG 0x484d
399 #define OV8865_MIPI_FRAME_END_DELAY_REG 0x484e
400 #define OV8865_MIPI_CLOCK_TEST_PATTERN_REG 0x484f
401 #define OV8865_MIPI_LANE_SEL01_REG 0x4850
402 #define OV8865_MIPI_LANE_SEL01_LANE0(v) (((v) << 0) & GENMASK(2, 0))
403 #define OV8865_MIPI_LANE_SEL01_LANE1(v) (((v) << 4) & GENMASK(6, 4))
404 #define OV8865_MIPI_LANE_SEL23_REG 0x4851
405 #define OV8865_MIPI_LANE_SEL23_LANE2(v) (((v) << 0) & GENMASK(2, 0))
406 #define OV8865_MIPI_LANE_SEL23_LANE3(v) (((v) << 4) & GENMASK(6, 4))
410 #define OV8865_ISP_CTRL0_REG 0x5000
411 #define OV8865_ISP_CTRL0_LENC_EN BIT(7)
412 #define OV8865_ISP_CTRL0_WHITE_BALANCE_EN BIT(4)
413 #define OV8865_ISP_CTRL0_DPC_BLACK_EN BIT(2)
414 #define OV8865_ISP_CTRL0_DPC_WHITE_EN BIT(1)
415 #define OV8865_ISP_CTRL1_REG 0x5001
416 #define OV8865_ISP_CTRL1_BLC_EN BIT(0)
417 #define OV8865_ISP_CTRL2_REG 0x5002
418 #define OV8865_ISP_CTRL2_DEBUG BIT(3)
419 #define OV8865_ISP_CTRL2_VARIOPIXEL_EN BIT(2)
420 #define OV8865_ISP_CTRL2_VSYNC_LATCH_EN BIT(0)
421 #define OV8865_ISP_CTRL3_REG 0x5003
423 #define OV8865_ISP_GAIN_RED_H_REG 0x5018
424 #define OV8865_ISP_GAIN_RED_H(v) (((v) & GENMASK(13, 6)) >> 6)
425 #define OV8865_ISP_GAIN_RED_L_REG 0x5019
426 #define OV8865_ISP_GAIN_RED_L(v) ((v) & GENMASK(5, 0))
427 #define OV8865_ISP_GAIN_GREEN_H_REG 0x501a
428 #define OV8865_ISP_GAIN_GREEN_H(v) (((v) & GENMASK(13, 6)) >> 6)
429 #define OV8865_ISP_GAIN_GREEN_L_REG 0x501b
430 #define OV8865_ISP_GAIN_GREEN_L(v) ((v) & GENMASK(5, 0))
431 #define OV8865_ISP_GAIN_BLUE_H_REG 0x501c
432 #define OV8865_ISP_GAIN_BLUE_H(v) (((v) & GENMASK(13, 6)) >> 6)
433 #define OV8865_ISP_GAIN_BLUE_L_REG 0x501d
434 #define OV8865_ISP_GAIN_BLUE_L(v) ((v) & GENMASK(5, 0))
438 #define OV8865_VAP_CTRL0_REG 0x5900
439 #define OV8865_VAP_CTRL1_REG 0x5901
440 #define OV8865_VAP_CTRL1_HSUB_COEF(v) ((((v) - 1) << 2) & \
442 #define OV8865_VAP_CTRL1_VSUB_COEF(v) (((v) - 1) & GENMASK(1, 0))
446 #define OV8865_PRE_CTRL0_REG 0x5e00
447 #define OV8865_PRE_CTRL0_PATTERN_EN BIT(7)
448 #define OV8865_PRE_CTRL0_ROLLING_BAR_EN BIT(6)
449 #define OV8865_PRE_CTRL0_TRANSPARENT_MODE BIT(5)
450 #define OV8865_PRE_CTRL0_SQUARES_BW_MODE BIT(4)
451 #define OV8865_PRE_CTRL0_PATTERN_COLOR_BARS 0
452 #define OV8865_PRE_CTRL0_PATTERN_RANDOM_DATA 1
453 #define OV8865_PRE_CTRL0_PATTERN_COLOR_SQUARES 2
454 #define OV8865_PRE_CTRL0_PATTERN_BLACK 3
458 #define OV8865_NATIVE_WIDTH 3296
459 #define OV8865_NATIVE_HEIGHT 2528
460 #define OV8865_ACTIVE_START_LEFT 16
461 #define OV8865_ACTIVE_START_TOP 40
462 #define OV8865_ACTIVE_WIDTH 3264
463 #define OV8865_ACTIVE_HEIGHT 2448
467 #define ov8865_subdev_sensor(s) \
468 container_of(s, struct ov8865_sensor, subdev)
470 #define ov8865_ctrl_subdev(c) \
471 (&container_of((c)->handler, struct ov8865_sensor, \
472 ctrls.handler)->subdev)
474 /* Data structures */
476 struct ov8865_register_value
{
479 unsigned int delay_ms
;
487 * +-+ pll_pre_div_half (0x30a [0])
489 * +-+ pll_pre_div (0x300 [2:0], special values:
490 * | 0: 1, 1: 1.5, 3: 2.5, 4: 3, 5: 4, 7: 8)
491 * +-+ pll_mul (0x301 [1:0], 0x302 [7:0])
493 * +-+ m_div (0x303 [3:0])
497 * | +-+ mipi_div (0x304 [1:0], special values: 0: 4, 1: 5, 2: 6, 3: 8)
499 * | +-+ pclk_div (0x3020 [3])
503 * +-+ sys_pre_div (0x305 [1:0], special values: 0: 3, 1: 4, 2: 5, 3: 6)
505 * +-+ sys_div (0x306 [0])
507 * +-+ sys_sel (0x3032 [7], 0: PLL1, 1: PLL2)
509 * +-+ sclk_sel (0x3033 [1], 0: sys_sel, 1: PLL2 DAC_CLK)
511 * +-+ sclk_pre_div (0x3106 [3:2], special values:
512 * | 0: 1, 1: 2, 2: 4, 3: 1)
514 * +-+ sclk_div (0x3106 [7:4], special values: 0: 1)
519 struct ov8865_pll1_config
{
520 unsigned int pll_pre_div_half
;
521 unsigned int pll_pre_div
;
522 unsigned int pll_mul
;
524 unsigned int mipi_div
;
525 unsigned int pclk_div
;
526 unsigned int sys_pre_div
;
527 unsigned int sys_div
;
535 * +-+ pll_pre_div_half (0x312 [4])
537 * +-+ pll_pre_div (0x30b [2:0], special values:
538 * | 0: 1, 1: 1.5, 3: 2.5, 4: 3, 5: 4, 7: 8)
539 * +-+ pll_mul (0x30c [1:0], 0x30d [7:0])
541 * +-+ dac_div (0x312 [3:0])
545 * +-+ sys_pre_div (0x30f [3:0])
547 * +-+ sys_div (0x30e [2:0], special values:
548 * | 0: 1, 1: 1.5, 3: 2.5, 4: 3, 5: 3.5, 6: 4, 7:5)
550 * +-+ sys_sel (0x3032 [7], 0: PLL1, 1: PLL2)
552 * +-+ sclk_sel (0x3033 [1], 0: sys_sel, 1: PLL2 DAC_CLK)
554 * +-+ sclk_pre_div (0x3106 [3:2], special values:
555 * | 0: 1, 1: 2, 2: 4, 3: 1)
557 * +-+ sclk_div (0x3106 [7:4], special values: 0: 1)
562 struct ov8865_pll2_config
{
563 unsigned int pll_pre_div_half
;
564 unsigned int pll_pre_div
;
565 unsigned int pll_mul
;
566 unsigned int dac_div
;
567 unsigned int sys_pre_div
;
568 unsigned int sys_div
;
571 struct ov8865_sclk_config
{
572 unsigned int sys_sel
;
573 unsigned int sclk_sel
;
574 unsigned int sclk_pre_div
;
575 unsigned int sclk_div
;
578 struct ov8865_pll_configs
{
579 const struct ov8865_pll1_config
*pll1_config
;
580 const struct ov8865_pll2_config
*pll2_config_native
;
581 const struct ov8865_pll2_config
*pll2_config_binning
;
589 OV8865_NUM_SUPPORTED_RATES
592 static const unsigned long supported_extclk_rates
[] = {
593 [OV8865_19_2_MHZ
] = 19200000,
594 [OV8865_24_MHZ
] = 24000000,
598 * General formulas for (array-centered) mode calculation:
599 * - photo_array_width = 3296
600 * - crop_start_x = (photo_array_width - output_size_x) / 2
601 * - crop_end_x = crop_start_x + offset_x + output_size_x - 1
603 * - photo_array_height = 2480
604 * - crop_start_y = (photo_array_height - output_size_y) / 2
605 * - crop_end_y = crop_start_y + offset_y + output_size_y - 1
609 unsigned int crop_start_x
;
610 unsigned int offset_x
;
611 unsigned int output_size_x
;
612 unsigned int crop_end_x
;
615 unsigned int crop_start_y
;
616 unsigned int offset_y
;
617 unsigned int output_size_y
;
618 unsigned int crop_end_y
;
621 /* With auto size, only output and total sizes need to be set. */
623 unsigned int size_auto_boundary_x
;
624 unsigned int size_auto_boundary_y
;
629 unsigned int variopixel_hsub_coef
;
630 unsigned int variopixel_vsub_coef
;
632 /* Bits for the format register, used for binning. */
636 unsigned int inc_x_odd
;
637 unsigned int inc_x_even
;
638 unsigned int inc_y_odd
;
639 unsigned int inc_y_even
;
641 unsigned int vfifo_read_start
;
643 unsigned int ablc_num
;
644 unsigned int zline_num
;
646 unsigned int blc_top_zero_line_start
;
647 unsigned int blc_top_zero_line_num
;
648 unsigned int blc_top_black_line_start
;
649 unsigned int blc_top_black_line_num
;
651 unsigned int blc_bottom_zero_line_start
;
652 unsigned int blc_bottom_zero_line_num
;
653 unsigned int blc_bottom_black_line_start
;
654 unsigned int blc_bottom_black_line_num
;
656 u8 blc_col_shift_mask
;
658 unsigned int blc_anchor_left_start
;
659 unsigned int blc_anchor_left_end
;
660 unsigned int blc_anchor_right_start
;
661 unsigned int blc_anchor_right_end
;
665 const struct ov8865_register_value
*register_values
;
666 unsigned int register_values_count
;
669 struct ov8865_state
{
670 const struct ov8865_mode
*mode
;
676 struct ov8865_ctrls
{
677 struct v4l2_ctrl
*link_freq
;
678 struct v4l2_ctrl
*pixel_rate
;
679 struct v4l2_ctrl
*hblank
;
680 struct v4l2_ctrl
*vblank
;
681 struct v4l2_ctrl
*exposure
;
683 struct v4l2_ctrl_handler handler
;
686 struct ov8865_sensor
{
688 struct i2c_client
*i2c_client
;
689 struct gpio_desc
*reset
;
690 struct gpio_desc
*powerdown
;
691 struct regulator
*avdd
;
692 struct regulator
*dvdd
;
693 struct regulator
*dovdd
;
695 unsigned long extclk_rate
;
696 const struct ov8865_pll_configs
*pll_configs
;
699 struct v4l2_fwnode_endpoint endpoint
;
700 struct v4l2_subdev subdev
;
701 struct media_pad pad
;
705 struct ov8865_state state
;
706 struct ov8865_ctrls ctrls
;
709 /* Static definitions */
716 static const struct ov8865_pll1_config ov8865_pll1_config_native_19_2mhz
= {
717 .pll_pre_div_half
= 1,
727 static const struct ov8865_pll1_config ov8865_pll1_config_native_24mhz
= {
728 .pll_pre_div_half
= 1,
743 static const struct ov8865_pll2_config ov8865_pll2_config_native_19_2mhz
= {
744 .pll_pre_div_half
= 1,
752 static const struct ov8865_pll2_config ov8865_pll2_config_native_24mhz
= {
753 .pll_pre_div_half
= 1,
766 static const struct ov8865_pll2_config ov8865_pll2_config_binning_19_2mhz
= {
767 .pll_pre_div_half
= 1,
775 static const struct ov8865_pll2_config ov8865_pll2_config_binning_24mhz
= {
776 .pll_pre_div_half
= 1,
784 static const struct ov8865_pll_configs ov8865_pll_configs_19_2mhz
= {
785 .pll1_config
= &ov8865_pll1_config_native_19_2mhz
,
786 .pll2_config_native
= &ov8865_pll2_config_native_19_2mhz
,
787 .pll2_config_binning
= &ov8865_pll2_config_binning_19_2mhz
,
790 static const struct ov8865_pll_configs ov8865_pll_configs_24mhz
= {
791 .pll1_config
= &ov8865_pll1_config_native_24mhz
,
792 .pll2_config_native
= &ov8865_pll2_config_native_24mhz
,
793 .pll2_config_binning
= &ov8865_pll2_config_binning_24mhz
,
796 static const struct ov8865_pll_configs
*ov8865_pll_configs
[] = {
797 &ov8865_pll_configs_19_2mhz
,
798 &ov8865_pll_configs_24mhz
,
801 static const struct ov8865_sclk_config ov8865_sclk_config_native
= {
808 static const struct ov8865_register_value ov8865_register_values_native
[] = {
878 { OV8865_PSRAM_CTRL8_REG
, 0x16 },
885 static const struct ov8865_register_value ov8865_register_values_binning
[] = {
955 { OV8865_PSRAM_CTRL8_REG
, 0x0b },
962 static const struct ov8865_mode ov8865_modes
[] = {
966 .output_size_x
= 3264,
970 .output_size_y
= 2448,
974 .size_auto_boundary_x
= 8,
975 .size_auto_boundary_y
= 4,
977 /* Subsample increase */
984 .vfifo_read_start
= 16,
991 .blc_top_zero_line_start
= 0,
992 .blc_top_zero_line_num
= 2,
993 .blc_top_black_line_start
= 4,
994 .blc_top_black_line_num
= 4,
996 .blc_bottom_zero_line_start
= 2,
997 .blc_bottom_zero_line_num
= 2,
998 .blc_bottom_black_line_start
= 8,
999 .blc_bottom_black_line_num
= 2,
1001 .blc_anchor_left_start
= 576,
1002 .blc_anchor_left_end
= 831,
1003 .blc_anchor_right_start
= 1984,
1004 .blc_anchor_right_end
= 2239,
1007 .pll2_binning
= false,
1010 .register_values
= ov8865_register_values_native
,
1011 .register_values_count
=
1012 ARRAY_SIZE(ov8865_register_values_native
),
1017 .output_size_x
= 3264,
1021 .output_size_y
= 1836,
1025 .size_auto_boundary_x
= 8,
1026 .size_auto_boundary_y
= 4,
1028 /* Subsample increase */
1035 .vfifo_read_start
= 16,
1042 .blc_top_zero_line_start
= 0,
1043 .blc_top_zero_line_num
= 2,
1044 .blc_top_black_line_start
= 4,
1045 .blc_top_black_line_num
= 4,
1047 .blc_bottom_zero_line_start
= 2,
1048 .blc_bottom_zero_line_num
= 2,
1049 .blc_bottom_black_line_start
= 8,
1050 .blc_bottom_black_line_num
= 2,
1052 .blc_anchor_left_start
= 576,
1053 .blc_anchor_left_end
= 831,
1054 .blc_anchor_right_start
= 1984,
1055 .blc_anchor_right_end
= 2239,
1058 .pll2_binning
= false,
1061 .register_values
= ov8865_register_values_native
,
1062 .register_values_count
=
1063 ARRAY_SIZE(ov8865_register_values_native
),
1068 .output_size_x
= 1632,
1072 .output_size_y
= 1224,
1076 .size_auto_boundary_x
= 8,
1077 .size_auto_boundary_y
= 8,
1079 /* Subsample increase */
1090 .vfifo_read_start
= 116,
1097 .blc_top_zero_line_start
= 0,
1098 .blc_top_zero_line_num
= 2,
1099 .blc_top_black_line_start
= 4,
1100 .blc_top_black_line_num
= 4,
1102 .blc_bottom_zero_line_start
= 2,
1103 .blc_bottom_zero_line_num
= 2,
1104 .blc_bottom_black_line_start
= 8,
1105 .blc_bottom_black_line_num
= 2,
1107 .blc_anchor_left_start
= 288,
1108 .blc_anchor_left_end
= 415,
1109 .blc_anchor_right_start
= 992,
1110 .blc_anchor_right_end
= 1119,
1113 .pll2_binning
= true,
1116 .register_values
= ov8865_register_values_binning
,
1117 .register_values_count
=
1118 ARRAY_SIZE(ov8865_register_values_binning
),
1120 /* 800x600 (SVGA) */
1123 .output_size_x
= 800,
1127 .output_size_y
= 600,
1131 .size_auto_boundary_x
= 8,
1132 .size_auto_boundary_y
= 8,
1134 /* Subsample increase */
1143 .variopixel_hsub_coef
= 2,
1144 .variopixel_vsub_coef
= 1,
1149 .vfifo_read_start
= 80,
1156 .blc_top_zero_line_start
= 0,
1157 .blc_top_zero_line_num
= 2,
1158 .blc_top_black_line_start
= 2,
1159 .blc_top_black_line_num
= 2,
1161 .blc_bottom_zero_line_start
= 0,
1162 .blc_bottom_zero_line_num
= 0,
1163 .blc_bottom_black_line_start
= 4,
1164 .blc_bottom_black_line_num
= 2,
1166 .blc_col_shift_mask
= OV8865_BLC_CTRL1_COL_SHIFT_128
,
1168 .blc_anchor_left_start
= 288,
1169 .blc_anchor_left_end
= 415,
1170 .blc_anchor_right_start
= 992,
1171 .blc_anchor_right_end
= 1119,
1174 .pll2_binning
= true,
1177 .register_values
= ov8865_register_values_binning
,
1178 .register_values_count
=
1179 ARRAY_SIZE(ov8865_register_values_binning
),
1183 static const u32 ov8865_mbus_codes
[] = {
1184 MEDIA_BUS_FMT_SBGGR10_1X10
,
1187 static const struct ov8865_register_value ov8865_init_sequence
[] = {
1322 static const s64 ov8865_link_freq_menu
[] = {
1326 static const char *const ov8865_test_pattern_menu
[] = {
1330 "Color bars with rolling bar",
1332 "Color squares with rolling bar"
1335 static const u8 ov8865_test_pattern_bits
[] = {
1337 OV8865_PRE_CTRL0_PATTERN_EN
| OV8865_PRE_CTRL0_PATTERN_RANDOM_DATA
,
1338 OV8865_PRE_CTRL0_PATTERN_EN
| OV8865_PRE_CTRL0_PATTERN_COLOR_BARS
,
1339 OV8865_PRE_CTRL0_PATTERN_EN
| OV8865_PRE_CTRL0_ROLLING_BAR_EN
|
1340 OV8865_PRE_CTRL0_PATTERN_COLOR_BARS
,
1341 OV8865_PRE_CTRL0_PATTERN_EN
| OV8865_PRE_CTRL0_PATTERN_COLOR_SQUARES
,
1342 OV8865_PRE_CTRL0_PATTERN_EN
| OV8865_PRE_CTRL0_ROLLING_BAR_EN
|
1343 OV8865_PRE_CTRL0_PATTERN_COLOR_SQUARES
,
1348 static int ov8865_read(struct ov8865_sensor
*sensor
, u16 address
, u8
*value
)
1350 unsigned char data
[2] = { address
>> 8, address
& 0xff };
1351 struct i2c_client
*client
= sensor
->i2c_client
;
1354 ret
= i2c_master_send(client
, data
, sizeof(data
));
1356 dev_dbg(&client
->dev
, "i2c send error at address %#04x\n",
1361 ret
= i2c_master_recv(client
, value
, 1);
1363 dev_dbg(&client
->dev
, "i2c recv error at address %#04x\n",
1371 static int ov8865_write(struct ov8865_sensor
*sensor
, u16 address
, u8 value
)
1373 unsigned char data
[3] = { address
>> 8, address
& 0xff, value
};
1374 struct i2c_client
*client
= sensor
->i2c_client
;
1377 ret
= i2c_master_send(client
, data
, sizeof(data
));
1379 dev_dbg(&client
->dev
, "i2c send error at address %#04x\n",
1387 static int ov8865_write_sequence(struct ov8865_sensor
*sensor
,
1388 const struct ov8865_register_value
*sequence
,
1389 unsigned int sequence_count
)
1394 for (i
= 0; i
< sequence_count
; i
++) {
1395 ret
= ov8865_write(sensor
, sequence
[i
].address
,
1400 if (sequence
[i
].delay_ms
)
1401 msleep(sequence
[i
].delay_ms
);
1407 static int ov8865_update_bits(struct ov8865_sensor
*sensor
, u16 address
,
1413 ret
= ov8865_read(sensor
, address
, &value
);
1420 return ov8865_write(sensor
, address
, value
);
1425 static int ov8865_sw_reset(struct ov8865_sensor
*sensor
)
1427 return ov8865_write(sensor
, OV8865_SW_RESET_REG
, OV8865_SW_RESET_RESET
);
1430 static int ov8865_sw_standby(struct ov8865_sensor
*sensor
, int standby
)
1435 value
= OV8865_SW_STANDBY_STREAM_ON
;
1437 return ov8865_write(sensor
, OV8865_SW_STANDBY_REG
, value
);
1440 static int ov8865_chip_id_check(struct ov8865_sensor
*sensor
)
1442 u16 regs
[] = { OV8865_CHIP_ID_HH_REG
, OV8865_CHIP_ID_H_REG
,
1443 OV8865_CHIP_ID_L_REG
};
1444 u8 values
[] = { OV8865_CHIP_ID_HH_VALUE
, OV8865_CHIP_ID_H_VALUE
,
1445 OV8865_CHIP_ID_L_VALUE
};
1450 for (i
= 0; i
< ARRAY_SIZE(regs
); i
++) {
1451 ret
= ov8865_read(sensor
, regs
[i
], &value
);
1455 if (value
!= values
[i
]) {
1456 dev_err(sensor
->dev
,
1457 "chip id value mismatch: %#x instead of %#x\n",
1466 static int ov8865_charge_pump_configure(struct ov8865_sensor
*sensor
)
1468 return ov8865_write(sensor
, OV8865_PUMP_CLK_DIV_REG
,
1469 OV8865_PUMP_CLK_DIV_PUMP_P(1));
1472 static int ov8865_mipi_configure(struct ov8865_sensor
*sensor
)
1474 struct v4l2_mbus_config_mipi_csi2
*bus_mipi_csi2
=
1475 &sensor
->endpoint
.bus
.mipi_csi2
;
1476 unsigned int lanes_count
= bus_mipi_csi2
->num_data_lanes
;
1479 ret
= ov8865_write(sensor
, OV8865_MIPI_SC_CTRL0_REG
,
1480 OV8865_MIPI_SC_CTRL0_LANES(lanes_count
) |
1481 OV8865_MIPI_SC_CTRL0_MIPI_EN
|
1482 OV8865_MIPI_SC_CTRL0_UNKNOWN
);
1486 ret
= ov8865_write(sensor
, OV8865_MIPI_SC_CTRL2_REG
,
1487 OV8865_MIPI_SC_CTRL2_PD_MIPI_RST_SYNC
);
1491 if (lanes_count
>= 2) {
1492 ret
= ov8865_write(sensor
, OV8865_MIPI_LANE_SEL01_REG
,
1493 OV8865_MIPI_LANE_SEL01_LANE0(0) |
1494 OV8865_MIPI_LANE_SEL01_LANE1(1));
1499 if (lanes_count
>= 4) {
1500 ret
= ov8865_write(sensor
, OV8865_MIPI_LANE_SEL23_REG
,
1501 OV8865_MIPI_LANE_SEL23_LANE2(2) |
1502 OV8865_MIPI_LANE_SEL23_LANE3(3));
1507 ret
= ov8865_update_bits(sensor
, OV8865_CLK_SEL1_REG
,
1508 OV8865_CLK_SEL1_MIPI_EOF
,
1509 OV8865_CLK_SEL1_MIPI_EOF
);
1514 * This value might need to change depending on PCLK rate,
1515 * but it's unclear how. This value seems to generally work
1516 * while the default value was found to cause transmission errors.
1518 return ov8865_write(sensor
, OV8865_MIPI_PCLK_PERIOD_REG
, 0x16);
1521 static int ov8865_black_level_configure(struct ov8865_sensor
*sensor
)
1525 /* Trigger BLC on relevant events and enable filter. */
1526 ret
= ov8865_write(sensor
, OV8865_BLC_CTRL0_REG
,
1527 OV8865_BLC_CTRL0_TRIG_RANGE_EN
|
1528 OV8865_BLC_CTRL0_TRIG_FORMAT_EN
|
1529 OV8865_BLC_CTRL0_TRIG_GAIN_EN
|
1530 OV8865_BLC_CTRL0_TRIG_EXPOSURE_EN
|
1531 OV8865_BLC_CTRL0_FILTER_EN
);
1535 /* Lower BLC offset trigger threshold. */
1536 ret
= ov8865_write(sensor
, OV8865_BLC_CTRLD_REG
,
1537 OV8865_BLC_CTRLD_OFFSET_TRIGGER(16));
1541 ret
= ov8865_write(sensor
, OV8865_BLC_CTRL1F_REG
, 0);
1545 /* Increase BLC offset maximum limit. */
1546 return ov8865_write(sensor
, OV8865_BLC_OFFSET_LIMIT_REG
,
1547 OV8865_BLC_OFFSET_LIMIT(63));
1550 static int ov8865_isp_configure(struct ov8865_sensor
*sensor
)
1554 /* Disable lens correction. */
1555 ret
= ov8865_write(sensor
, OV8865_ISP_CTRL0_REG
,
1556 OV8865_ISP_CTRL0_WHITE_BALANCE_EN
|
1557 OV8865_ISP_CTRL0_DPC_BLACK_EN
|
1558 OV8865_ISP_CTRL0_DPC_WHITE_EN
);
1562 return ov8865_write(sensor
, OV8865_ISP_CTRL1_REG
,
1563 OV8865_ISP_CTRL1_BLC_EN
);
1566 static unsigned long ov8865_mode_pll1_rate(struct ov8865_sensor
*sensor
,
1567 const struct ov8865_mode
*mode
)
1569 const struct ov8865_pll1_config
*config
;
1570 unsigned long pll1_rate
;
1572 config
= sensor
->pll_configs
->pll1_config
;
1573 pll1_rate
= sensor
->extclk_rate
* config
->pll_mul
/ config
->pll_pre_div_half
;
1575 switch (config
->pll_pre_div
) {
1596 pll1_rate
/= config
->pll_pre_div
;
1603 static int ov8865_mode_pll1_configure(struct ov8865_sensor
*sensor
,
1604 const struct ov8865_mode
*mode
,
1607 const struct ov8865_pll1_config
*config
;
1611 config
= sensor
->pll_configs
->pll1_config
;
1613 switch (mbus_code
) {
1614 case MEDIA_BUS_FMT_SBGGR10_1X10
:
1615 value
= OV8865_MIPI_BIT_SEL(10);
1621 ret
= ov8865_write(sensor
, OV8865_MIPI_BIT_SEL_REG
, value
);
1625 ret
= ov8865_write(sensor
, OV8865_PLL_CTRLA_REG
,
1626 OV8865_PLL_CTRLA_PRE_DIV_HALF(config
->pll_pre_div_half
));
1630 ret
= ov8865_write(sensor
, OV8865_PLL_CTRL0_REG
,
1631 OV8865_PLL_CTRL0_PRE_DIV(config
->pll_pre_div
));
1635 ret
= ov8865_write(sensor
, OV8865_PLL_CTRL1_REG
,
1636 OV8865_PLL_CTRL1_MUL_H(config
->pll_mul
));
1640 ret
= ov8865_write(sensor
, OV8865_PLL_CTRL2_REG
,
1641 OV8865_PLL_CTRL2_MUL_L(config
->pll_mul
));
1645 ret
= ov8865_write(sensor
, OV8865_PLL_CTRL3_REG
,
1646 OV8865_PLL_CTRL3_M_DIV(config
->m_div
));
1650 ret
= ov8865_write(sensor
, OV8865_PLL_CTRL4_REG
,
1651 OV8865_PLL_CTRL4_MIPI_DIV(config
->mipi_div
));
1655 ret
= ov8865_update_bits(sensor
, OV8865_PCLK_SEL_REG
,
1656 OV8865_PCLK_SEL_PCLK_DIV_MASK
,
1657 OV8865_PCLK_SEL_PCLK_DIV(config
->pclk_div
));
1661 ret
= ov8865_write(sensor
, OV8865_PLL_CTRL5_REG
,
1662 OV8865_PLL_CTRL5_SYS_PRE_DIV(config
->sys_pre_div
));
1666 ret
= ov8865_write(sensor
, OV8865_PLL_CTRL6_REG
,
1667 OV8865_PLL_CTRL6_SYS_DIV(config
->sys_div
));
1671 return ov8865_update_bits(sensor
, OV8865_PLL_CTRL1E_REG
,
1672 OV8865_PLL_CTRL1E_PLL1_NO_LAT
,
1673 OV8865_PLL_CTRL1E_PLL1_NO_LAT
);
1676 static int ov8865_mode_pll2_configure(struct ov8865_sensor
*sensor
,
1677 const struct ov8865_mode
*mode
)
1679 const struct ov8865_pll2_config
*config
;
1682 config
= mode
->pll2_binning
? sensor
->pll_configs
->pll2_config_binning
:
1683 sensor
->pll_configs
->pll2_config_native
;
1685 ret
= ov8865_write(sensor
, OV8865_PLL_CTRL12_REG
,
1686 OV8865_PLL_CTRL12_PRE_DIV_HALF(config
->pll_pre_div_half
) |
1687 OV8865_PLL_CTRL12_DAC_DIV(config
->dac_div
));
1691 ret
= ov8865_write(sensor
, OV8865_PLL_CTRLB_REG
,
1692 OV8865_PLL_CTRLB_PRE_DIV(config
->pll_pre_div
));
1696 ret
= ov8865_write(sensor
, OV8865_PLL_CTRLC_REG
,
1697 OV8865_PLL_CTRLC_MUL_H(config
->pll_mul
));
1701 ret
= ov8865_write(sensor
, OV8865_PLL_CTRLD_REG
,
1702 OV8865_PLL_CTRLD_MUL_L(config
->pll_mul
));
1706 ret
= ov8865_write(sensor
, OV8865_PLL_CTRLF_REG
,
1707 OV8865_PLL_CTRLF_SYS_PRE_DIV(config
->sys_pre_div
));
1711 return ov8865_write(sensor
, OV8865_PLL_CTRLE_REG
,
1712 OV8865_PLL_CTRLE_SYS_DIV(config
->sys_div
));
1715 static int ov8865_mode_sclk_configure(struct ov8865_sensor
*sensor
,
1716 const struct ov8865_mode
*mode
)
1718 const struct ov8865_sclk_config
*config
= &ov8865_sclk_config_native
;
1721 ret
= ov8865_write(sensor
, OV8865_CLK_SEL0_REG
,
1722 OV8865_CLK_SEL0_PLL1_SYS_SEL(config
->sys_sel
));
1726 ret
= ov8865_update_bits(sensor
, OV8865_CLK_SEL1_REG
,
1727 OV8865_CLK_SEL1_PLL_SCLK_SEL_MASK
,
1728 OV8865_CLK_SEL1_PLL_SCLK_SEL(config
->sclk_sel
));
1732 return ov8865_write(sensor
, OV8865_SCLK_CTRL_REG
,
1733 OV8865_SCLK_CTRL_UNKNOWN
|
1734 OV8865_SCLK_CTRL_SCLK_DIV(config
->sclk_div
) |
1735 OV8865_SCLK_CTRL_SCLK_PRE_DIV(config
->sclk_pre_div
));
1738 static int ov8865_mode_binning_configure(struct ov8865_sensor
*sensor
,
1739 const struct ov8865_mode
*mode
)
1741 unsigned int variopixel_hsub_coef
, variopixel_vsub_coef
;
1745 ret
= ov8865_write(sensor
, OV8865_FORMAT1_REG
, 0);
1749 value
= OV8865_FORMAT2_HSYNC_EN
;
1751 if (mode
->binning_x
)
1752 value
|= OV8865_FORMAT2_FST_HBIN_EN
;
1754 if (mode
->binning_y
)
1755 value
|= OV8865_FORMAT2_FST_VBIN_EN
;
1757 if (mode
->sync_hbin
)
1758 value
|= OV8865_FORMAT2_SYNC_HBIN_EN
;
1760 if (mode
->horz_var2
)
1761 value
|= OV8865_FORMAT2_ISP_HORZ_VAR2_EN
;
1763 ret
= ov8865_write(sensor
, OV8865_FORMAT2_REG
, value
);
1767 ret
= ov8865_update_bits(sensor
, OV8865_ISP_CTRL2_REG
,
1768 OV8865_ISP_CTRL2_VARIOPIXEL_EN
,
1770 OV8865_ISP_CTRL2_VARIOPIXEL_EN
: 0);
1774 if (mode
->variopixel
) {
1775 /* VarioPixel coefs needs to be > 1. */
1776 variopixel_hsub_coef
= mode
->variopixel_hsub_coef
;
1777 variopixel_vsub_coef
= mode
->variopixel_vsub_coef
;
1779 variopixel_hsub_coef
= 1;
1780 variopixel_vsub_coef
= 1;
1783 ret
= ov8865_write(sensor
, OV8865_VAP_CTRL1_REG
,
1784 OV8865_VAP_CTRL1_HSUB_COEF(variopixel_hsub_coef
) |
1785 OV8865_VAP_CTRL1_VSUB_COEF(variopixel_vsub_coef
));
1789 ret
= ov8865_write(sensor
, OV8865_INC_X_ODD_REG
,
1790 OV8865_INC_X_ODD(mode
->inc_x_odd
));
1794 ret
= ov8865_write(sensor
, OV8865_INC_X_EVEN_REG
,
1795 OV8865_INC_X_EVEN(mode
->inc_x_even
));
1799 ret
= ov8865_write(sensor
, OV8865_INC_Y_ODD_REG
,
1800 OV8865_INC_Y_ODD(mode
->inc_y_odd
));
1804 return ov8865_write(sensor
, OV8865_INC_Y_EVEN_REG
,
1805 OV8865_INC_Y_EVEN(mode
->inc_y_even
));
1808 static int ov8865_mode_black_level_configure(struct ov8865_sensor
*sensor
,
1809 const struct ov8865_mode
*mode
)
1813 /* Note that a zero value for blc_col_shift_mask is the default 256. */
1814 ret
= ov8865_write(sensor
, OV8865_BLC_CTRL1_REG
,
1815 mode
->blc_col_shift_mask
|
1816 OV8865_BLC_CTRL1_OFFSET_LIMIT_EN
);
1820 /* BLC top zero line */
1822 ret
= ov8865_write(sensor
, OV8865_BLC_TOP_ZLINE_START_REG
,
1823 OV8865_BLC_TOP_ZLINE_START(mode
->blc_top_zero_line_start
));
1827 ret
= ov8865_write(sensor
, OV8865_BLC_TOP_ZLINE_NUM_REG
,
1828 OV8865_BLC_TOP_ZLINE_NUM(mode
->blc_top_zero_line_num
));
1832 /* BLC top black line */
1834 ret
= ov8865_write(sensor
, OV8865_BLC_TOP_BLKLINE_START_REG
,
1835 OV8865_BLC_TOP_BLKLINE_START(mode
->blc_top_black_line_start
));
1839 ret
= ov8865_write(sensor
, OV8865_BLC_TOP_BLKLINE_NUM_REG
,
1840 OV8865_BLC_TOP_BLKLINE_NUM(mode
->blc_top_black_line_num
));
1844 /* BLC bottom zero line */
1846 ret
= ov8865_write(sensor
, OV8865_BLC_BOT_ZLINE_START_REG
,
1847 OV8865_BLC_BOT_ZLINE_START(mode
->blc_bottom_zero_line_start
));
1851 ret
= ov8865_write(sensor
, OV8865_BLC_BOT_ZLINE_NUM_REG
,
1852 OV8865_BLC_BOT_ZLINE_NUM(mode
->blc_bottom_zero_line_num
));
1856 /* BLC bottom black line */
1858 ret
= ov8865_write(sensor
, OV8865_BLC_BOT_BLKLINE_START_REG
,
1859 OV8865_BLC_BOT_BLKLINE_START(mode
->blc_bottom_black_line_start
));
1863 ret
= ov8865_write(sensor
, OV8865_BLC_BOT_BLKLINE_NUM_REG
,
1864 OV8865_BLC_BOT_BLKLINE_NUM(mode
->blc_bottom_black_line_num
));
1870 ret
= ov8865_write(sensor
, OV8865_BLC_ANCHOR_LEFT_START_H_REG
,
1871 OV8865_BLC_ANCHOR_LEFT_START_H(mode
->blc_anchor_left_start
));
1875 ret
= ov8865_write(sensor
, OV8865_BLC_ANCHOR_LEFT_START_L_REG
,
1876 OV8865_BLC_ANCHOR_LEFT_START_L(mode
->blc_anchor_left_start
));
1880 ret
= ov8865_write(sensor
, OV8865_BLC_ANCHOR_LEFT_END_H_REG
,
1881 OV8865_BLC_ANCHOR_LEFT_END_H(mode
->blc_anchor_left_end
));
1885 ret
= ov8865_write(sensor
, OV8865_BLC_ANCHOR_LEFT_END_L_REG
,
1886 OV8865_BLC_ANCHOR_LEFT_END_L(mode
->blc_anchor_left_end
));
1890 ret
= ov8865_write(sensor
, OV8865_BLC_ANCHOR_RIGHT_START_H_REG
,
1891 OV8865_BLC_ANCHOR_RIGHT_START_H(mode
->blc_anchor_right_start
));
1895 ret
= ov8865_write(sensor
, OV8865_BLC_ANCHOR_RIGHT_START_L_REG
,
1896 OV8865_BLC_ANCHOR_RIGHT_START_L(mode
->blc_anchor_right_start
));
1900 ret
= ov8865_write(sensor
, OV8865_BLC_ANCHOR_RIGHT_END_H_REG
,
1901 OV8865_BLC_ANCHOR_RIGHT_END_H(mode
->blc_anchor_right_end
));
1905 return ov8865_write(sensor
, OV8865_BLC_ANCHOR_RIGHT_END_L_REG
,
1906 OV8865_BLC_ANCHOR_RIGHT_END_L(mode
->blc_anchor_right_end
));
1909 static int ov8865_mode_configure(struct ov8865_sensor
*sensor
,
1910 const struct ov8865_mode
*mode
, u32 mbus_code
)
1916 ret
= ov8865_write(sensor
, OV8865_OUTPUT_SIZE_X_H_REG
,
1917 OV8865_OUTPUT_SIZE_X_H(mode
->output_size_x
));
1921 ret
= ov8865_write(sensor
, OV8865_OUTPUT_SIZE_X_L_REG
,
1922 OV8865_OUTPUT_SIZE_X_L(mode
->output_size_x
));
1926 /* Horizontal Total Size */
1928 ret
= ov8865_write(sensor
, OV8865_HTS_H_REG
, OV8865_HTS_H(mode
->hts
));
1932 ret
= ov8865_write(sensor
, OV8865_HTS_L_REG
, OV8865_HTS_L(mode
->hts
));
1938 ret
= ov8865_write(sensor
, OV8865_OUTPUT_SIZE_Y_H_REG
,
1939 OV8865_OUTPUT_SIZE_Y_H(mode
->output_size_y
));
1943 ret
= ov8865_write(sensor
, OV8865_OUTPUT_SIZE_Y_L_REG
,
1944 OV8865_OUTPUT_SIZE_Y_L(mode
->output_size_y
));
1948 /* Vertical Total Size */
1950 ret
= ov8865_write(sensor
, OV8865_VTS_H_REG
, OV8865_VTS_H(mode
->vts
));
1954 ret
= ov8865_write(sensor
, OV8865_VTS_L_REG
, OV8865_VTS_L(mode
->vts
));
1958 if (mode
->size_auto
) {
1961 ret
= ov8865_write(sensor
, OV8865_AUTO_SIZE_CTRL_REG
,
1962 OV8865_AUTO_SIZE_CTRL_OFFSET_Y_REG
|
1963 OV8865_AUTO_SIZE_CTRL_OFFSET_X_REG
|
1964 OV8865_AUTO_SIZE_CTRL_CROP_END_Y_REG
|
1965 OV8865_AUTO_SIZE_CTRL_CROP_END_X_REG
|
1966 OV8865_AUTO_SIZE_CTRL_CROP_START_Y_REG
|
1967 OV8865_AUTO_SIZE_CTRL_CROP_START_X_REG
);
1971 ret
= ov8865_write(sensor
, OV8865_AUTO_SIZE_BOUNDARIES_REG
,
1972 OV8865_AUTO_SIZE_BOUNDARIES_Y(mode
->size_auto_boundary_y
) |
1973 OV8865_AUTO_SIZE_BOUNDARIES_X(mode
->size_auto_boundary_x
));
1979 ret
= ov8865_write(sensor
, OV8865_CROP_START_X_H_REG
,
1980 OV8865_CROP_START_X_H(mode
->crop_start_x
));
1984 ret
= ov8865_write(sensor
, OV8865_CROP_START_X_L_REG
,
1985 OV8865_CROP_START_X_L(mode
->crop_start_x
));
1991 ret
= ov8865_write(sensor
, OV8865_OFFSET_X_H_REG
,
1992 OV8865_OFFSET_X_H(mode
->offset_x
));
1996 ret
= ov8865_write(sensor
, OV8865_OFFSET_X_L_REG
,
1997 OV8865_OFFSET_X_L(mode
->offset_x
));
2003 ret
= ov8865_write(sensor
, OV8865_CROP_END_X_H_REG
,
2004 OV8865_CROP_END_X_H(mode
->crop_end_x
));
2008 ret
= ov8865_write(sensor
, OV8865_CROP_END_X_L_REG
,
2009 OV8865_CROP_END_X_L(mode
->crop_end_x
));
2015 ret
= ov8865_write(sensor
, OV8865_CROP_START_Y_H_REG
,
2016 OV8865_CROP_START_Y_H(mode
->crop_start_y
));
2020 ret
= ov8865_write(sensor
, OV8865_CROP_START_Y_L_REG
,
2021 OV8865_CROP_START_Y_L(mode
->crop_start_y
));
2027 ret
= ov8865_write(sensor
, OV8865_OFFSET_Y_H_REG
,
2028 OV8865_OFFSET_Y_H(mode
->offset_y
));
2032 ret
= ov8865_write(sensor
, OV8865_OFFSET_Y_L_REG
,
2033 OV8865_OFFSET_Y_L(mode
->offset_y
));
2039 ret
= ov8865_write(sensor
, OV8865_CROP_END_Y_H_REG
,
2040 OV8865_CROP_END_Y_H(mode
->crop_end_y
));
2044 ret
= ov8865_write(sensor
, OV8865_CROP_END_Y_L_REG
,
2045 OV8865_CROP_END_Y_L(mode
->crop_end_y
));
2052 ret
= ov8865_write(sensor
, OV8865_VFIFO_READ_START_H_REG
,
2053 OV8865_VFIFO_READ_START_H(mode
->vfifo_read_start
));
2057 ret
= ov8865_write(sensor
, OV8865_VFIFO_READ_START_L_REG
,
2058 OV8865_VFIFO_READ_START_L(mode
->vfifo_read_start
));
2062 ret
= ov8865_write(sensor
, OV8865_ABLC_NUM_REG
,
2063 OV8865_ABLC_NUM(mode
->ablc_num
));
2067 ret
= ov8865_write(sensor
, OV8865_ZLINE_NUM_REG
,
2068 OV8865_ZLINE_NUM(mode
->zline_num
));
2074 ret
= ov8865_mode_binning_configure(sensor
, mode
);
2080 ret
= ov8865_mode_black_level_configure(sensor
, mode
);
2086 ret
= ov8865_mode_pll1_configure(sensor
, mode
, mbus_code
);
2090 ret
= ov8865_mode_pll2_configure(sensor
, mode
);
2094 ret
= ov8865_mode_sclk_configure(sensor
, mode
);
2098 /* Extra registers */
2100 if (mode
->register_values
) {
2101 ret
= ov8865_write_sequence(sensor
, mode
->register_values
,
2102 mode
->register_values_count
);
2110 static unsigned long ov8865_mode_mipi_clk_rate(struct ov8865_sensor
*sensor
,
2111 const struct ov8865_mode
*mode
)
2113 const struct ov8865_pll1_config
*config
;
2114 unsigned long pll1_rate
;
2116 config
= sensor
->pll_configs
->pll1_config
;
2118 pll1_rate
= ov8865_mode_pll1_rate(sensor
, mode
);
2120 return pll1_rate
/ config
->m_div
/ 2;
2125 static int ov8865_exposure_configure(struct ov8865_sensor
*sensor
, u32 exposure
)
2129 /* The sensor stores exposure in units of 1/16th of a line */
2132 ret
= ov8865_write(sensor
, OV8865_EXPOSURE_CTRL_HH_REG
,
2133 OV8865_EXPOSURE_CTRL_HH(exposure
));
2137 ret
= ov8865_write(sensor
, OV8865_EXPOSURE_CTRL_H_REG
,
2138 OV8865_EXPOSURE_CTRL_H(exposure
));
2142 return ov8865_write(sensor
, OV8865_EXPOSURE_CTRL_L_REG
,
2143 OV8865_EXPOSURE_CTRL_L(exposure
));
2148 static int ov8865_analog_gain_configure(struct ov8865_sensor
*sensor
, u32 gain
)
2152 ret
= ov8865_write(sensor
, OV8865_GAIN_CTRL_H_REG
,
2153 OV8865_GAIN_CTRL_H(gain
));
2157 return ov8865_write(sensor
, OV8865_GAIN_CTRL_L_REG
,
2158 OV8865_GAIN_CTRL_L(gain
));
2163 static int ov8865_red_balance_configure(struct ov8865_sensor
*sensor
,
2168 ret
= ov8865_write(sensor
, OV8865_ISP_GAIN_RED_H_REG
,
2169 OV8865_ISP_GAIN_RED_H(red_balance
));
2173 return ov8865_write(sensor
, OV8865_ISP_GAIN_RED_L_REG
,
2174 OV8865_ISP_GAIN_RED_L(red_balance
));
2177 static int ov8865_blue_balance_configure(struct ov8865_sensor
*sensor
,
2182 ret
= ov8865_write(sensor
, OV8865_ISP_GAIN_BLUE_H_REG
,
2183 OV8865_ISP_GAIN_BLUE_H(blue_balance
));
2187 return ov8865_write(sensor
, OV8865_ISP_GAIN_BLUE_L_REG
,
2188 OV8865_ISP_GAIN_BLUE_L(blue_balance
));
2193 static int ov8865_flip_vert_configure(struct ov8865_sensor
*sensor
, bool enable
)
2195 u8 bits
= OV8865_FORMAT1_FLIP_VERT_ISP_EN
|
2196 OV8865_FORMAT1_FLIP_VERT_SENSOR_EN
;
2198 return ov8865_update_bits(sensor
, OV8865_FORMAT1_REG
, bits
,
2202 static int ov8865_flip_horz_configure(struct ov8865_sensor
*sensor
, bool enable
)
2204 u8 bits
= OV8865_FORMAT2_FLIP_HORZ_ISP_EN
|
2205 OV8865_FORMAT2_FLIP_HORZ_SENSOR_EN
;
2207 return ov8865_update_bits(sensor
, OV8865_FORMAT2_REG
, bits
,
2213 static int ov8865_test_pattern_configure(struct ov8865_sensor
*sensor
,
2216 if (index
>= ARRAY_SIZE(ov8865_test_pattern_bits
))
2219 return ov8865_write(sensor
, OV8865_PRE_CTRL0_REG
,
2220 ov8865_test_pattern_bits
[index
]);
2225 static int ov8865_vts_configure(struct ov8865_sensor
*sensor
, u32 vblank
)
2227 u16 vts
= sensor
->state
.mode
->output_size_y
+ vblank
;
2230 ret
= ov8865_write(sensor
, OV8865_VTS_H_REG
, OV8865_VTS_H(vts
));
2234 return ov8865_write(sensor
, OV8865_VTS_L_REG
, OV8865_VTS_L(vts
));
2239 static int ov8865_state_mipi_configure(struct ov8865_sensor
*sensor
,
2240 const struct ov8865_mode
*mode
,
2243 struct ov8865_ctrls
*ctrls
= &sensor
->ctrls
;
2244 struct v4l2_mbus_config_mipi_csi2
*bus_mipi_csi2
=
2245 &sensor
->endpoint
.bus
.mipi_csi2
;
2246 unsigned long mipi_clk_rate
;
2247 unsigned int bits_per_sample
;
2248 unsigned int lanes_count
;
2250 s64 mipi_pixel_rate
;
2252 mipi_clk_rate
= ov8865_mode_mipi_clk_rate(sensor
, mode
);
2256 for (i
= 0; i
< ARRAY_SIZE(ov8865_link_freq_menu
); i
++) {
2257 s64 freq
= ov8865_link_freq_menu
[i
];
2259 if (freq
== mipi_clk_rate
)
2263 for (j
= 0; j
< sensor
->endpoint
.nr_of_link_frequencies
; j
++) {
2264 u64 freq
= sensor
->endpoint
.link_frequencies
[j
];
2266 if (freq
== mipi_clk_rate
)
2270 if (i
== ARRAY_SIZE(ov8865_link_freq_menu
)) {
2271 dev_err(sensor
->dev
,
2272 "failed to find %lu clk rate in link freq\n",
2274 } else if (j
== sensor
->endpoint
.nr_of_link_frequencies
) {
2275 dev_err(sensor
->dev
,
2276 "failed to find %lu clk rate in endpoint link-frequencies\n",
2279 __v4l2_ctrl_s_ctrl(ctrls
->link_freq
, i
);
2282 switch (mbus_code
) {
2283 case MEDIA_BUS_FMT_SBGGR10_1X10
:
2284 bits_per_sample
= 10;
2290 lanes_count
= bus_mipi_csi2
->num_data_lanes
;
2291 mipi_pixel_rate
= mipi_clk_rate
* 2 * lanes_count
/ bits_per_sample
;
2293 __v4l2_ctrl_s_ctrl_int64(ctrls
->pixel_rate
, mipi_pixel_rate
);
2298 static int ov8865_state_configure(struct ov8865_sensor
*sensor
,
2299 const struct ov8865_mode
*mode
,
2304 if (sensor
->state
.streaming
)
2307 /* State will be configured at first power on otherwise. */
2308 if (pm_runtime_enabled(sensor
->dev
) &&
2309 !pm_runtime_suspended(sensor
->dev
)) {
2310 ret
= ov8865_mode_configure(sensor
, mode
, mbus_code
);
2315 ret
= ov8865_state_mipi_configure(sensor
, mode
, mbus_code
);
2319 sensor
->state
.mode
= mode
;
2320 sensor
->state
.mbus_code
= mbus_code
;
2325 static int ov8865_state_init(struct ov8865_sensor
*sensor
)
2327 return ov8865_state_configure(sensor
, &ov8865_modes
[0],
2328 ov8865_mbus_codes
[0]);
2333 static int ov8865_sensor_init(struct ov8865_sensor
*sensor
)
2337 ret
= ov8865_sw_reset(sensor
);
2339 dev_err(sensor
->dev
, "failed to perform sw reset\n");
2343 ret
= ov8865_sw_standby(sensor
, 1);
2345 dev_err(sensor
->dev
, "failed to set sensor standby\n");
2349 ret
= ov8865_chip_id_check(sensor
);
2351 dev_err(sensor
->dev
, "failed to check sensor chip id\n");
2355 ret
= ov8865_write_sequence(sensor
, ov8865_init_sequence
,
2356 ARRAY_SIZE(ov8865_init_sequence
));
2358 dev_err(sensor
->dev
, "failed to write init sequence\n");
2362 ret
= ov8865_charge_pump_configure(sensor
);
2364 dev_err(sensor
->dev
, "failed to configure pad\n");
2368 ret
= ov8865_mipi_configure(sensor
);
2370 dev_err(sensor
->dev
, "failed to configure MIPI\n");
2374 ret
= ov8865_isp_configure(sensor
);
2376 dev_err(sensor
->dev
, "failed to configure ISP\n");
2380 ret
= ov8865_black_level_configure(sensor
);
2382 dev_err(sensor
->dev
, "failed to configure black level\n");
2386 /* Configure current mode. */
2387 ret
= ov8865_state_configure(sensor
, sensor
->state
.mode
,
2388 sensor
->state
.mbus_code
);
2390 dev_err(sensor
->dev
, "failed to configure state\n");
2397 static int ov8865_sensor_power(struct ov8865_sensor
*sensor
, bool on
)
2399 /* Keep initialized to zero for disable label. */
2403 gpiod_set_value_cansleep(sensor
->reset
, 1);
2404 gpiod_set_value_cansleep(sensor
->powerdown
, 1);
2406 ret
= regulator_enable(sensor
->dovdd
);
2408 dev_err(sensor
->dev
,
2409 "failed to enable DOVDD regulator\n");
2413 ret
= regulator_enable(sensor
->avdd
);
2415 dev_err(sensor
->dev
,
2416 "failed to enable AVDD regulator\n");
2420 ret
= regulator_enable(sensor
->dvdd
);
2422 dev_err(sensor
->dev
,
2423 "failed to enable DVDD regulator\n");
2427 ret
= clk_prepare_enable(sensor
->extclk
);
2429 dev_err(sensor
->dev
, "failed to enable EXTCLK clock\n");
2433 gpiod_set_value_cansleep(sensor
->reset
, 0);
2434 gpiod_set_value_cansleep(sensor
->powerdown
, 0);
2436 /* Time to enter streaming mode according to power timings. */
2437 usleep_range(10000, 12000);
2439 gpiod_set_value_cansleep(sensor
->powerdown
, 1);
2440 gpiod_set_value_cansleep(sensor
->reset
, 1);
2442 clk_disable_unprepare(sensor
->extclk
);
2445 regulator_disable(sensor
->dvdd
);
2447 regulator_disable(sensor
->avdd
);
2449 regulator_disable(sensor
->dovdd
);
2457 static int ov8865_s_ctrl(struct v4l2_ctrl
*ctrl
)
2459 struct v4l2_subdev
*subdev
= ov8865_ctrl_subdev(ctrl
);
2460 struct ov8865_sensor
*sensor
= ov8865_subdev_sensor(subdev
);
2464 /* If VBLANK is altered we need to update exposure to compensate */
2465 if (ctrl
->id
== V4L2_CID_VBLANK
) {
2468 exposure_max
= sensor
->state
.mode
->output_size_y
+ ctrl
->val
-
2469 OV8865_INTEGRATION_TIME_MARGIN
;
2470 __v4l2_ctrl_modify_range(sensor
->ctrls
.exposure
,
2471 sensor
->ctrls
.exposure
->minimum
,
2473 sensor
->ctrls
.exposure
->step
,
2474 min(sensor
->ctrls
.exposure
->val
,
2478 /* Wait for the sensor to be on before setting controls. */
2479 if (pm_runtime_suspended(sensor
->dev
))
2483 case V4L2_CID_EXPOSURE
:
2484 ret
= ov8865_exposure_configure(sensor
, ctrl
->val
);
2488 case V4L2_CID_ANALOGUE_GAIN
:
2489 ret
= ov8865_analog_gain_configure(sensor
, ctrl
->val
);
2493 case V4L2_CID_RED_BALANCE
:
2494 return ov8865_red_balance_configure(sensor
, ctrl
->val
);
2495 case V4L2_CID_BLUE_BALANCE
:
2496 return ov8865_blue_balance_configure(sensor
, ctrl
->val
);
2497 case V4L2_CID_HFLIP
:
2498 return ov8865_flip_horz_configure(sensor
, !!ctrl
->val
);
2499 case V4L2_CID_VFLIP
:
2500 return ov8865_flip_vert_configure(sensor
, !!ctrl
->val
);
2501 case V4L2_CID_TEST_PATTERN
:
2502 index
= (unsigned int)ctrl
->val
;
2503 return ov8865_test_pattern_configure(sensor
, index
);
2504 case V4L2_CID_VBLANK
:
2505 return ov8865_vts_configure(sensor
, ctrl
->val
);
2513 static const struct v4l2_ctrl_ops ov8865_ctrl_ops
= {
2514 .s_ctrl
= ov8865_s_ctrl
,
2517 static int ov8865_ctrls_init(struct ov8865_sensor
*sensor
)
2519 struct ov8865_ctrls
*ctrls
= &sensor
->ctrls
;
2520 struct v4l2_ctrl_handler
*handler
= &ctrls
->handler
;
2521 const struct v4l2_ctrl_ops
*ops
= &ov8865_ctrl_ops
;
2522 const struct ov8865_mode
*mode
= &ov8865_modes
[0];
2523 struct v4l2_fwnode_device_properties props
;
2524 unsigned int vblank_max
, vblank_def
;
2525 unsigned int hblank
;
2528 v4l2_ctrl_handler_init(handler
, 32);
2530 /* Use our mutex for ctrl locking. */
2531 handler
->lock
= &sensor
->mutex
;
2535 ctrls
->exposure
= v4l2_ctrl_new_std(handler
, ops
, V4L2_CID_EXPOSURE
, 2,
2540 v4l2_ctrl_new_std(handler
, ops
, V4L2_CID_ANALOGUE_GAIN
, 128, 2048, 128,
2545 v4l2_ctrl_new_std(handler
, ops
, V4L2_CID_RED_BALANCE
, 1, 32767, 1,
2548 v4l2_ctrl_new_std(handler
, ops
, V4L2_CID_BLUE_BALANCE
, 1, 32767, 1,
2553 v4l2_ctrl_new_std(handler
, ops
, V4L2_CID_HFLIP
, 0, 1, 1, 0);
2554 v4l2_ctrl_new_std(handler
, ops
, V4L2_CID_VFLIP
, 0, 1, 1, 0);
2558 v4l2_ctrl_new_std_menu_items(handler
, ops
, V4L2_CID_TEST_PATTERN
,
2559 ARRAY_SIZE(ov8865_test_pattern_menu
) - 1,
2560 0, 0, ov8865_test_pattern_menu
);
2563 hblank
= mode
->hts
- mode
->output_size_x
;
2564 ctrls
->hblank
= v4l2_ctrl_new_std(handler
, ops
, V4L2_CID_HBLANK
, hblank
,
2568 ctrls
->hblank
->flags
|= V4L2_CTRL_FLAG_READ_ONLY
;
2570 vblank_max
= OV8865_TIMING_MAX_VTS
- mode
->output_size_y
;
2571 vblank_def
= mode
->vts
- mode
->output_size_y
;
2572 ctrls
->vblank
= v4l2_ctrl_new_std(handler
, ops
, V4L2_CID_VBLANK
,
2573 OV8865_TIMING_MIN_VTS
, vblank_max
, 1,
2579 v4l2_ctrl_new_int_menu(handler
, NULL
, V4L2_CID_LINK_FREQ
,
2580 ARRAY_SIZE(ov8865_link_freq_menu
) - 1,
2581 0, ov8865_link_freq_menu
);
2584 v4l2_ctrl_new_std(handler
, NULL
, V4L2_CID_PIXEL_RATE
, 1,
2587 /* set properties from fwnode (e.g. rotation, orientation) */
2588 ret
= v4l2_fwnode_device_parse(sensor
->dev
, &props
);
2592 ret
= v4l2_ctrl_new_fwnode_properties(handler
, ops
, &props
);
2596 if (handler
->error
) {
2597 ret
= handler
->error
;
2601 ctrls
->link_freq
->flags
|= V4L2_CTRL_FLAG_READ_ONLY
;
2602 ctrls
->pixel_rate
->flags
|= V4L2_CTRL_FLAG_READ_ONLY
;
2604 sensor
->subdev
.ctrl_handler
= handler
;
2609 v4l2_ctrl_handler_free(handler
);
2614 /* Subdev Video Operations */
2616 static int ov8865_s_stream(struct v4l2_subdev
*subdev
, int enable
)
2618 struct ov8865_sensor
*sensor
= ov8865_subdev_sensor(subdev
);
2619 struct ov8865_state
*state
= &sensor
->state
;
2623 ret
= pm_runtime_resume_and_get(sensor
->dev
);
2628 mutex_lock(&sensor
->mutex
);
2629 ret
= ov8865_sw_standby(sensor
, !enable
);
2630 mutex_unlock(&sensor
->mutex
);
2635 state
->streaming
= !!enable
;
2638 pm_runtime_put(sensor
->dev
);
2643 static const struct v4l2_subdev_video_ops ov8865_subdev_video_ops
= {
2644 .s_stream
= ov8865_s_stream
,
2647 /* Subdev Pad Operations */
2649 static int ov8865_enum_mbus_code(struct v4l2_subdev
*subdev
,
2650 struct v4l2_subdev_state
*sd_state
,
2651 struct v4l2_subdev_mbus_code_enum
*code_enum
)
2653 if (code_enum
->index
>= ARRAY_SIZE(ov8865_mbus_codes
))
2656 code_enum
->code
= ov8865_mbus_codes
[code_enum
->index
];
2661 static void ov8865_mbus_format_fill(struct v4l2_mbus_framefmt
*mbus_format
,
2663 const struct ov8865_mode
*mode
)
2665 mbus_format
->width
= mode
->output_size_x
;
2666 mbus_format
->height
= mode
->output_size_y
;
2667 mbus_format
->code
= mbus_code
;
2669 mbus_format
->field
= V4L2_FIELD_NONE
;
2670 mbus_format
->colorspace
= V4L2_COLORSPACE_RAW
;
2671 mbus_format
->ycbcr_enc
=
2672 V4L2_MAP_YCBCR_ENC_DEFAULT(mbus_format
->colorspace
);
2673 mbus_format
->quantization
= V4L2_QUANTIZATION_FULL_RANGE
;
2674 mbus_format
->xfer_func
=
2675 V4L2_MAP_XFER_FUNC_DEFAULT(mbus_format
->colorspace
);
2678 static int ov8865_get_fmt(struct v4l2_subdev
*subdev
,
2679 struct v4l2_subdev_state
*sd_state
,
2680 struct v4l2_subdev_format
*format
)
2682 struct ov8865_sensor
*sensor
= ov8865_subdev_sensor(subdev
);
2683 struct v4l2_mbus_framefmt
*mbus_format
= &format
->format
;
2685 mutex_lock(&sensor
->mutex
);
2687 if (format
->which
== V4L2_SUBDEV_FORMAT_TRY
)
2688 *mbus_format
= *v4l2_subdev_state_get_format(sd_state
,
2691 ov8865_mbus_format_fill(mbus_format
, sensor
->state
.mbus_code
,
2692 sensor
->state
.mode
);
2694 mutex_unlock(&sensor
->mutex
);
2699 static int ov8865_set_fmt(struct v4l2_subdev
*subdev
,
2700 struct v4l2_subdev_state
*sd_state
,
2701 struct v4l2_subdev_format
*format
)
2703 struct ov8865_sensor
*sensor
= ov8865_subdev_sensor(subdev
);
2704 struct v4l2_mbus_framefmt
*mbus_format
= &format
->format
;
2705 const struct ov8865_mode
*mode
;
2707 unsigned int hblank
;
2712 mutex_lock(&sensor
->mutex
);
2714 if (sensor
->state
.streaming
) {
2719 /* Try to find requested mbus code. */
2720 for (index
= 0; index
< ARRAY_SIZE(ov8865_mbus_codes
); index
++) {
2721 if (ov8865_mbus_codes
[index
] == mbus_format
->code
) {
2722 mbus_code
= mbus_format
->code
;
2727 /* Fallback to default. */
2729 mbus_code
= ov8865_mbus_codes
[0];
2731 /* Find the mode with nearest dimensions. */
2732 mode
= v4l2_find_nearest_size(ov8865_modes
, ARRAY_SIZE(ov8865_modes
),
2733 output_size_x
, output_size_y
,
2734 mbus_format
->width
, mbus_format
->height
);
2740 ov8865_mbus_format_fill(mbus_format
, mbus_code
, mode
);
2742 if (format
->which
== V4L2_SUBDEV_FORMAT_TRY
)
2743 *v4l2_subdev_state_get_format(sd_state
, format
->pad
) =
2745 else if (sensor
->state
.mode
!= mode
||
2746 sensor
->state
.mbus_code
!= mbus_code
)
2747 ret
= ov8865_state_configure(sensor
, mode
, mbus_code
);
2749 __v4l2_ctrl_modify_range(sensor
->ctrls
.vblank
, OV8865_TIMING_MIN_VTS
,
2750 OV8865_TIMING_MAX_VTS
- mode
->output_size_y
,
2751 1, mode
->vts
- mode
->output_size_y
);
2753 hblank
= mode
->hts
- mode
->output_size_x
;
2754 __v4l2_ctrl_modify_range(sensor
->ctrls
.hblank
, hblank
, hblank
, 1,
2757 exposure_max
= mode
->vts
- OV8865_INTEGRATION_TIME_MARGIN
;
2758 __v4l2_ctrl_modify_range(sensor
->ctrls
.exposure
,
2759 sensor
->ctrls
.exposure
->minimum
, exposure_max
,
2760 sensor
->ctrls
.exposure
->step
,
2761 min(sensor
->ctrls
.exposure
->val
,
2765 mutex_unlock(&sensor
->mutex
);
2770 static int ov8865_enum_frame_size(struct v4l2_subdev
*subdev
,
2771 struct v4l2_subdev_state
*sd_state
,
2772 struct v4l2_subdev_frame_size_enum
*size_enum
)
2774 const struct ov8865_mode
*mode
;
2776 if (size_enum
->index
>= ARRAY_SIZE(ov8865_modes
))
2779 mode
= &ov8865_modes
[size_enum
->index
];
2781 size_enum
->min_width
= size_enum
->max_width
= mode
->output_size_x
;
2782 size_enum
->min_height
= size_enum
->max_height
= mode
->output_size_y
;
2788 __ov8865_get_pad_crop(struct ov8865_sensor
*sensor
,
2789 struct v4l2_subdev_state
*state
, unsigned int pad
,
2790 enum v4l2_subdev_format_whence which
, struct v4l2_rect
*r
)
2792 const struct ov8865_mode
*mode
= sensor
->state
.mode
;
2795 case V4L2_SUBDEV_FORMAT_TRY
:
2796 *r
= *v4l2_subdev_state_get_crop(state
, pad
);
2798 case V4L2_SUBDEV_FORMAT_ACTIVE
:
2799 r
->height
= mode
->output_size_y
;
2800 r
->width
= mode
->output_size_x
;
2801 r
->top
= (OV8865_NATIVE_HEIGHT
- mode
->output_size_y
) / 2;
2802 r
->left
= (OV8865_NATIVE_WIDTH
- mode
->output_size_x
) / 2;
2807 static int ov8865_get_selection(struct v4l2_subdev
*subdev
,
2808 struct v4l2_subdev_state
*state
,
2809 struct v4l2_subdev_selection
*sel
)
2811 struct ov8865_sensor
*sensor
= ov8865_subdev_sensor(subdev
);
2813 switch (sel
->target
) {
2814 case V4L2_SEL_TGT_CROP
:
2815 mutex_lock(&sensor
->mutex
);
2816 __ov8865_get_pad_crop(sensor
, state
, sel
->pad
,
2817 sel
->which
, &sel
->r
);
2818 mutex_unlock(&sensor
->mutex
);
2820 case V4L2_SEL_TGT_NATIVE_SIZE
:
2823 sel
->r
.width
= OV8865_NATIVE_WIDTH
;
2824 sel
->r
.height
= OV8865_NATIVE_HEIGHT
;
2826 case V4L2_SEL_TGT_CROP_BOUNDS
:
2827 case V4L2_SEL_TGT_CROP_DEFAULT
:
2828 sel
->r
.top
= OV8865_ACTIVE_START_TOP
;
2829 sel
->r
.left
= OV8865_ACTIVE_START_LEFT
;
2830 sel
->r
.width
= OV8865_ACTIVE_WIDTH
;
2831 sel
->r
.height
= OV8865_ACTIVE_HEIGHT
;
2840 static int ov8865_get_frame_interval(struct v4l2_subdev
*subdev
,
2841 struct v4l2_subdev_state
*sd_state
,
2842 struct v4l2_subdev_frame_interval
*interval
)
2844 struct ov8865_sensor
*sensor
= ov8865_subdev_sensor(subdev
);
2845 const struct ov8865_mode
*mode
;
2846 unsigned int framesize
;
2850 * FIXME: Implement support for V4L2_SUBDEV_FORMAT_TRY, using the V4L2
2851 * subdev active state API.
2853 if (interval
->which
!= V4L2_SUBDEV_FORMAT_ACTIVE
)
2856 mutex_lock(&sensor
->mutex
);
2858 mode
= sensor
->state
.mode
;
2859 framesize
= mode
->hts
* (mode
->output_size_y
+
2860 sensor
->ctrls
.vblank
->val
);
2861 fps
= DIV_ROUND_CLOSEST(sensor
->ctrls
.pixel_rate
->val
, framesize
);
2863 interval
->interval
.numerator
= 1;
2864 interval
->interval
.denominator
= fps
;
2866 mutex_unlock(&sensor
->mutex
);
2871 static const struct v4l2_subdev_pad_ops ov8865_subdev_pad_ops
= {
2872 .enum_mbus_code
= ov8865_enum_mbus_code
,
2873 .get_fmt
= ov8865_get_fmt
,
2874 .set_fmt
= ov8865_set_fmt
,
2875 .enum_frame_size
= ov8865_enum_frame_size
,
2876 .get_selection
= ov8865_get_selection
,
2877 .set_selection
= ov8865_get_selection
,
2878 .get_frame_interval
= ov8865_get_frame_interval
,
2879 .set_frame_interval
= ov8865_get_frame_interval
,
2882 static const struct v4l2_subdev_ops ov8865_subdev_ops
= {
2883 .video
= &ov8865_subdev_video_ops
,
2884 .pad
= &ov8865_subdev_pad_ops
,
2887 static int ov8865_suspend(struct device
*dev
)
2889 struct i2c_client
*client
= to_i2c_client(dev
);
2890 struct v4l2_subdev
*subdev
= i2c_get_clientdata(client
);
2891 struct ov8865_sensor
*sensor
= ov8865_subdev_sensor(subdev
);
2892 struct ov8865_state
*state
= &sensor
->state
;
2895 mutex_lock(&sensor
->mutex
);
2897 if (state
->streaming
) {
2898 ret
= ov8865_sw_standby(sensor
, true);
2903 ret
= ov8865_sensor_power(sensor
, false);
2905 ov8865_sw_standby(sensor
, false);
2908 mutex_unlock(&sensor
->mutex
);
2913 static int ov8865_resume(struct device
*dev
)
2915 struct i2c_client
*client
= to_i2c_client(dev
);
2916 struct v4l2_subdev
*subdev
= i2c_get_clientdata(client
);
2917 struct ov8865_sensor
*sensor
= ov8865_subdev_sensor(subdev
);
2918 struct ov8865_state
*state
= &sensor
->state
;
2921 mutex_lock(&sensor
->mutex
);
2923 ret
= ov8865_sensor_power(sensor
, true);
2927 ret
= ov8865_sensor_init(sensor
);
2931 ret
= __v4l2_ctrl_handler_setup(&sensor
->ctrls
.handler
);
2935 if (state
->streaming
) {
2936 ret
= ov8865_sw_standby(sensor
, false);
2944 ov8865_sensor_power(sensor
, false);
2947 mutex_unlock(&sensor
->mutex
);
2952 static int ov8865_probe(struct i2c_client
*client
)
2954 struct device
*dev
= &client
->dev
;
2955 struct fwnode_handle
*handle
;
2956 struct ov8865_sensor
*sensor
;
2957 struct v4l2_subdev
*subdev
;
2958 struct media_pad
*pad
;
2959 unsigned int rate
= 0;
2963 sensor
= devm_kzalloc(dev
, sizeof(*sensor
), GFP_KERNEL
);
2968 sensor
->i2c_client
= client
;
2972 /* DVDD: digital core */
2973 sensor
->dvdd
= devm_regulator_get(dev
, "dvdd");
2974 if (IS_ERR(sensor
->dvdd
))
2975 return dev_err_probe(dev
, PTR_ERR(sensor
->dvdd
),
2976 "cannot get DVDD regulator\n");
2978 /* DOVDD: digital I/O */
2979 sensor
->dovdd
= devm_regulator_get(dev
, "dovdd");
2980 if (IS_ERR(sensor
->dovdd
))
2981 return dev_err_probe(dev
, PTR_ERR(sensor
->dovdd
),
2982 "cannot get DOVDD regulator\n");
2985 sensor
->avdd
= devm_regulator_get(dev
, "avdd");
2986 if (IS_ERR(sensor
->avdd
))
2987 return dev_err_probe(dev
, PTR_ERR(sensor
->avdd
),
2988 "cannot get AVDD (analog) regulator\n");
2990 /* Graph Endpoint */
2992 handle
= fwnode_graph_get_next_endpoint(dev_fwnode(dev
), NULL
);
2994 return -EPROBE_DEFER
;
2996 sensor
->endpoint
.bus_type
= V4L2_MBUS_CSI2_DPHY
;
2998 ret
= v4l2_fwnode_endpoint_alloc_parse(handle
, &sensor
->endpoint
);
2999 fwnode_handle_put(handle
);
3001 dev_err(dev
, "failed to parse endpoint node\n");
3007 sensor
->powerdown
= devm_gpiod_get_optional(dev
, "powerdown",
3009 if (IS_ERR(sensor
->powerdown
)) {
3010 ret
= PTR_ERR(sensor
->powerdown
);
3011 goto error_endpoint
;
3014 sensor
->reset
= devm_gpiod_get_optional(dev
, "reset", GPIOD_OUT_HIGH
);
3015 if (IS_ERR(sensor
->reset
)) {
3016 ret
= PTR_ERR(sensor
->reset
);
3017 goto error_endpoint
;
3020 /* External Clock */
3022 sensor
->extclk
= devm_clk_get(dev
, NULL
);
3023 if (PTR_ERR(sensor
->extclk
) == -ENOENT
) {
3024 dev_info(dev
, "no external clock found, continuing...\n");
3025 sensor
->extclk
= NULL
;
3026 } else if (IS_ERR(sensor
->extclk
)) {
3027 dev_err(dev
, "failed to get external clock\n");
3028 ret
= PTR_ERR(sensor
->extclk
);
3029 goto error_endpoint
;
3033 * We could have either a 24MHz or 19.2MHz clock rate from either dt or
3034 * ACPI...but we also need to support the weird IPU3 case which will
3035 * have an external clock AND a clock-frequency property. Check for the
3036 * clock-frequency property and if found, set that rate if we managed
3037 * to acquire a clock. This should cover the ACPI case. If the system
3038 * uses devicetree then the configured rate should already be set, so
3039 * we can just read it.
3041 ret
= fwnode_property_read_u32(dev_fwnode(dev
), "clock-frequency",
3043 if (!ret
&& sensor
->extclk
) {
3044 ret
= clk_set_rate(sensor
->extclk
, rate
);
3046 dev_err_probe(dev
, ret
, "failed to set clock rate\n");
3047 goto error_endpoint
;
3049 } else if (ret
&& !sensor
->extclk
) {
3050 dev_err_probe(dev
, ret
, "invalid clock config\n");
3051 goto error_endpoint
;
3054 sensor
->extclk_rate
= rate
? rate
: clk_get_rate(sensor
->extclk
);
3056 for (i
= 0; i
< ARRAY_SIZE(supported_extclk_rates
); i
++) {
3057 if (sensor
->extclk_rate
== supported_extclk_rates
[i
])
3061 if (i
== ARRAY_SIZE(supported_extclk_rates
)) {
3062 dev_err(dev
, "clock rate %lu Hz is unsupported\n",
3063 sensor
->extclk_rate
);
3065 goto error_endpoint
;
3068 sensor
->pll_configs
= ov8865_pll_configs
[i
];
3070 /* Subdev, entity and pad */
3072 subdev
= &sensor
->subdev
;
3073 v4l2_i2c_subdev_init(subdev
, client
, &ov8865_subdev_ops
);
3075 subdev
->flags
|= V4L2_SUBDEV_FL_HAS_DEVNODE
;
3076 subdev
->entity
.function
= MEDIA_ENT_F_CAM_SENSOR
;
3079 pad
->flags
= MEDIA_PAD_FL_SOURCE
;
3081 ret
= media_entity_pads_init(&subdev
->entity
, 1, pad
);
3087 mutex_init(&sensor
->mutex
);
3091 ret
= ov8865_ctrls_init(sensor
);
3095 mutex_lock(&sensor
->mutex
);
3096 ret
= ov8865_state_init(sensor
);
3097 mutex_unlock(&sensor
->mutex
);
3103 pm_runtime_set_suspended(sensor
->dev
);
3104 pm_runtime_enable(sensor
->dev
);
3106 /* V4L2 subdev register */
3108 ret
= v4l2_async_register_subdev_sensor(subdev
);
3115 pm_runtime_disable(sensor
->dev
);
3118 v4l2_ctrl_handler_free(&sensor
->ctrls
.handler
);
3121 mutex_destroy(&sensor
->mutex
);
3124 media_entity_cleanup(&sensor
->subdev
.entity
);
3127 v4l2_fwnode_endpoint_free(&sensor
->endpoint
);
3132 static void ov8865_remove(struct i2c_client
*client
)
3134 struct v4l2_subdev
*subdev
= i2c_get_clientdata(client
);
3135 struct ov8865_sensor
*sensor
= ov8865_subdev_sensor(subdev
);
3137 v4l2_async_unregister_subdev(subdev
);
3138 pm_runtime_disable(sensor
->dev
);
3139 v4l2_ctrl_handler_free(&sensor
->ctrls
.handler
);
3140 mutex_destroy(&sensor
->mutex
);
3141 media_entity_cleanup(&subdev
->entity
);
3143 v4l2_fwnode_endpoint_free(&sensor
->endpoint
);
3146 static const struct dev_pm_ops ov8865_pm_ops
= {
3147 SET_RUNTIME_PM_OPS(ov8865_suspend
, ov8865_resume
, NULL
)
3150 static const struct acpi_device_id ov8865_acpi_match
[] = {
3154 MODULE_DEVICE_TABLE(acpi
, ov8865_acpi_match
);
3156 static const struct of_device_id ov8865_of_match
[] = {
3157 { .compatible
= "ovti,ov8865" },
3160 MODULE_DEVICE_TABLE(of
, ov8865_of_match
);
3162 static struct i2c_driver ov8865_driver
= {
3165 .of_match_table
= ov8865_of_match
,
3166 .acpi_match_table
= ov8865_acpi_match
,
3167 .pm
= &ov8865_pm_ops
,
3169 .probe
= ov8865_probe
,
3170 .remove
= ov8865_remove
,
3173 module_i2c_driver(ov8865_driver
);
3175 MODULE_AUTHOR("Paul Kocialkowski <paul.kocialkowski@bootlin.com>");
3176 MODULE_DESCRIPTION("V4L2 driver for the OmniVision OV8865 image sensor");
3177 MODULE_LICENSE("GPL v2");