xref: /linux/drivers/media/i2c/ov8865.c (revision fab183d632628381b466a41479489541ac0e29a0)
1 // SPDX-License-Identifier: GPL-2.0-or-later
2 /*
3  * Copyright 2020 Kévin L'hôpital <kevin.lhopital@bootlin.com>
4  * Copyright 2020 Bootlin
5  * Author: Paul Kocialkowski <paul.kocialkowski@bootlin.com>
6  */
7 
8 #include <linux/clk.h>
9 #include <linux/delay.h>
10 #include <linux/device.h>
11 #include <linux/i2c.h>
12 #include <linux/module.h>
13 #include <linux/of_graph.h>
14 #include <linux/pm_runtime.h>
15 #include <linux/regulator/consumer.h>
16 #include <linux/videodev2.h>
17 #include <media/v4l2-ctrls.h>
18 #include <media/v4l2-device.h>
19 #include <media/v4l2-fwnode.h>
20 #include <media/v4l2-image-sizes.h>
21 #include <media/v4l2-mediabus.h>
22 
23 /* Register definitions */
24 
25 /* System */
26 
27 #define OV8865_SW_STANDBY_REG			0x100
28 #define OV8865_SW_STANDBY_STREAM_ON		BIT(0)
29 
30 #define OV8865_SW_RESET_REG			0x103
31 #define OV8865_SW_RESET_RESET			BIT(0)
32 
33 #define OV8865_PLL_CTRL0_REG			0x300
34 #define OV8865_PLL_CTRL0_PRE_DIV(v)		((v) & GENMASK(2, 0))
35 #define OV8865_PLL_CTRL1_REG			0x301
36 #define OV8865_PLL_CTRL1_MUL_H(v)		(((v) & GENMASK(9, 8)) >> 8)
37 #define OV8865_PLL_CTRL2_REG			0x302
38 #define OV8865_PLL_CTRL2_MUL_L(v)		((v) & GENMASK(7, 0))
39 #define OV8865_PLL_CTRL3_REG			0x303
40 #define OV8865_PLL_CTRL3_M_DIV(v)		(((v) - 1) & GENMASK(3, 0))
41 #define OV8865_PLL_CTRL4_REG			0x304
42 #define OV8865_PLL_CTRL4_MIPI_DIV(v)		((v) & GENMASK(1, 0))
43 #define OV8865_PLL_CTRL5_REG			0x305
44 #define OV8865_PLL_CTRL5_SYS_PRE_DIV(v)		((v) & GENMASK(1, 0))
45 #define OV8865_PLL_CTRL6_REG			0x306
46 #define OV8865_PLL_CTRL6_SYS_DIV(v)		(((v) - 1) & BIT(0))
47 
48 #define OV8865_PLL_CTRL8_REG			0x308
49 #define OV8865_PLL_CTRL9_REG			0x309
50 #define OV8865_PLL_CTRLA_REG			0x30a
51 #define OV8865_PLL_CTRLA_PRE_DIV_HALF(v)	(((v) - 1) & BIT(0))
52 #define OV8865_PLL_CTRLB_REG			0x30b
53 #define OV8865_PLL_CTRLB_PRE_DIV(v)		((v) & GENMASK(2, 0))
54 #define OV8865_PLL_CTRLC_REG			0x30c
55 #define OV8865_PLL_CTRLC_MUL_H(v)		(((v) & GENMASK(9, 8)) >> 8)
56 #define OV8865_PLL_CTRLD_REG			0x30d
57 #define OV8865_PLL_CTRLD_MUL_L(v)		((v) & GENMASK(7, 0))
58 #define OV8865_PLL_CTRLE_REG			0x30e
59 #define OV8865_PLL_CTRLE_SYS_DIV(v)		((v) & GENMASK(2, 0))
60 #define OV8865_PLL_CTRLF_REG			0x30f
61 #define OV8865_PLL_CTRLF_SYS_PRE_DIV(v)		(((v) - 1) & GENMASK(3, 0))
62 #define OV8865_PLL_CTRL10_REG			0x310
63 #define OV8865_PLL_CTRL11_REG			0x311
64 #define OV8865_PLL_CTRL12_REG			0x312
65 #define OV8865_PLL_CTRL12_PRE_DIV_HALF(v)	((((v) - 1) << 4) & BIT(4))
66 #define OV8865_PLL_CTRL12_DAC_DIV(v)		(((v) - 1) & GENMASK(3, 0))
67 
68 #define OV8865_PLL_CTRL1B_REG			0x31b
69 #define OV8865_PLL_CTRL1C_REG			0x31c
70 
71 #define OV8865_PLL_CTRL1E_REG			0x31e
72 #define OV8865_PLL_CTRL1E_PLL1_NO_LAT		BIT(3)
73 
74 #define OV8865_PAD_OEN0_REG			0x3000
75 
76 #define OV8865_PAD_OEN2_REG			0x3002
77 
78 #define OV8865_CLK_RST5_REG			0x3005
79 
80 #define OV8865_CHIP_ID_HH_REG			0x300a
81 #define OV8865_CHIP_ID_HH_VALUE			0x00
82 #define OV8865_CHIP_ID_H_REG			0x300b
83 #define OV8865_CHIP_ID_H_VALUE			0x88
84 #define OV8865_CHIP_ID_L_REG			0x300c
85 #define OV8865_CHIP_ID_L_VALUE			0x65
86 #define OV8865_PAD_OUT2_REG			0x300d
87 
88 #define OV8865_PAD_SEL2_REG			0x3010
89 #define OV8865_PAD_PK_REG			0x3011
90 #define OV8865_PAD_PK_DRIVE_STRENGTH_1X		(0 << 5)
91 #define OV8865_PAD_PK_DRIVE_STRENGTH_2X		(1 << 5)
92 #define OV8865_PAD_PK_DRIVE_STRENGTH_3X		(2 << 5)
93 #define OV8865_PAD_PK_DRIVE_STRENGTH_4X		(3 << 5)
94 
95 #define OV8865_PUMP_CLK_DIV_REG			0x3015
96 #define OV8865_PUMP_CLK_DIV_PUMP_N(v)		(((v) << 4) & GENMASK(6, 4))
97 #define OV8865_PUMP_CLK_DIV_PUMP_P(v)		((v) & GENMASK(2, 0))
98 
99 #define OV8865_MIPI_SC_CTRL0_REG		0x3018
100 #define OV8865_MIPI_SC_CTRL0_LANES(v)		((((v) - 1) << 5) & \
101 						 GENMASK(7, 5))
102 #define OV8865_MIPI_SC_CTRL0_MIPI_EN		BIT(4)
103 #define OV8865_MIPI_SC_CTRL0_UNKNOWN		BIT(1)
104 #define OV8865_MIPI_SC_CTRL0_LANES_PD_MIPI	BIT(0)
105 #define OV8865_MIPI_SC_CTRL1_REG		0x3019
106 #define OV8865_CLK_RST0_REG			0x301a
107 #define OV8865_CLK_RST1_REG			0x301b
108 #define OV8865_CLK_RST2_REG			0x301c
109 #define OV8865_CLK_RST3_REG			0x301d
110 #define OV8865_CLK_RST4_REG			0x301e
111 
112 #define OV8865_PCLK_SEL_REG			0x3020
113 #define OV8865_PCLK_SEL_PCLK_DIV_MASK		BIT(3)
114 #define OV8865_PCLK_SEL_PCLK_DIV(v)		((((v) - 1) << 3) & BIT(3))
115 
116 #define OV8865_MISC_CTRL_REG			0x3021
117 #define OV8865_MIPI_SC_CTRL2_REG		0x3022
118 #define OV8865_MIPI_SC_CTRL2_CLK_LANES_PD_MIPI	BIT(1)
119 #define OV8865_MIPI_SC_CTRL2_PD_MIPI_RST_SYNC	BIT(0)
120 
121 #define OV8865_MIPI_BIT_SEL_REG			0x3031
122 #define OV8865_MIPI_BIT_SEL(v)			(((v) << 0) & GENMASK(4, 0))
123 #define OV8865_CLK_SEL0_REG			0x3032
124 #define OV8865_CLK_SEL0_PLL1_SYS_SEL(v)		(((v) << 7) & BIT(7))
125 #define OV8865_CLK_SEL1_REG			0x3033
126 #define OV8865_CLK_SEL1_MIPI_EOF		BIT(5)
127 #define OV8865_CLK_SEL1_UNKNOWN			BIT(2)
128 #define OV8865_CLK_SEL1_PLL_SCLK_SEL_MASK	BIT(1)
129 #define OV8865_CLK_SEL1_PLL_SCLK_SEL(v)		(((v) << 1) & BIT(1))
130 
131 #define OV8865_SCLK_CTRL_REG			0x3106
132 #define OV8865_SCLK_CTRL_SCLK_DIV(v)		(((v) << 4) & GENMASK(7, 4))
133 #define OV8865_SCLK_CTRL_SCLK_PRE_DIV(v)	(((v) << 2) & GENMASK(3, 2))
134 #define OV8865_SCLK_CTRL_UNKNOWN		BIT(0)
135 
136 /* Exposure/gain */
137 
138 #define OV8865_EXPOSURE_CTRL_HH_REG		0x3500
139 #define OV8865_EXPOSURE_CTRL_HH(v)		(((v) & GENMASK(19, 16)) >> 16)
140 #define OV8865_EXPOSURE_CTRL_H_REG		0x3501
141 #define OV8865_EXPOSURE_CTRL_H(v)		(((v) & GENMASK(15, 8)) >> 8)
142 #define OV8865_EXPOSURE_CTRL_L_REG		0x3502
143 #define OV8865_EXPOSURE_CTRL_L(v)		((v) & GENMASK(7, 0))
144 #define OV8865_EXPOSURE_GAIN_MANUAL_REG		0x3503
145 #define OV8865_INTEGRATION_TIME_MARGIN		8
146 
147 #define OV8865_GAIN_CTRL_H_REG			0x3508
148 #define OV8865_GAIN_CTRL_H(v)			(((v) & GENMASK(12, 8)) >> 8)
149 #define OV8865_GAIN_CTRL_L_REG			0x3509
150 #define OV8865_GAIN_CTRL_L(v)			((v) & GENMASK(7, 0))
151 
152 /* Timing */
153 
154 #define OV8865_CROP_START_X_H_REG		0x3800
155 #define OV8865_CROP_START_X_H(v)		(((v) & GENMASK(11, 8)) >> 8)
156 #define OV8865_CROP_START_X_L_REG		0x3801
157 #define OV8865_CROP_START_X_L(v)		((v) & GENMASK(7, 0))
158 #define OV8865_CROP_START_Y_H_REG		0x3802
159 #define OV8865_CROP_START_Y_H(v)		(((v) & GENMASK(11, 8)) >> 8)
160 #define OV8865_CROP_START_Y_L_REG		0x3803
161 #define OV8865_CROP_START_Y_L(v)		((v) & GENMASK(7, 0))
162 #define OV8865_CROP_END_X_H_REG			0x3804
163 #define OV8865_CROP_END_X_H(v)			(((v) & GENMASK(11, 8)) >> 8)
164 #define OV8865_CROP_END_X_L_REG			0x3805
165 #define OV8865_CROP_END_X_L(v)			((v) & GENMASK(7, 0))
166 #define OV8865_CROP_END_Y_H_REG			0x3806
167 #define OV8865_CROP_END_Y_H(v)			(((v) & GENMASK(11, 8)) >> 8)
168 #define OV8865_CROP_END_Y_L_REG			0x3807
169 #define OV8865_CROP_END_Y_L(v)			((v) & GENMASK(7, 0))
170 #define OV8865_OUTPUT_SIZE_X_H_REG		0x3808
171 #define OV8865_OUTPUT_SIZE_X_H(v)		(((v) & GENMASK(11, 8)) >> 8)
172 #define OV8865_OUTPUT_SIZE_X_L_REG		0x3809
173 #define OV8865_OUTPUT_SIZE_X_L(v)		((v) & GENMASK(7, 0))
174 #define OV8865_OUTPUT_SIZE_Y_H_REG		0x380a
175 #define OV8865_OUTPUT_SIZE_Y_H(v)		(((v) & GENMASK(11, 8)) >> 8)
176 #define OV8865_OUTPUT_SIZE_Y_L_REG		0x380b
177 #define OV8865_OUTPUT_SIZE_Y_L(v)		((v) & GENMASK(7, 0))
178 #define OV8865_HTS_H_REG			0x380c
179 #define OV8865_HTS_H(v)				(((v) & GENMASK(11, 8)) >> 8)
180 #define OV8865_HTS_L_REG			0x380d
181 #define OV8865_HTS_L(v)				((v) & GENMASK(7, 0))
182 #define OV8865_VTS_H_REG			0x380e
183 #define OV8865_VTS_H(v)				(((v) & GENMASK(11, 8)) >> 8)
184 #define OV8865_VTS_L_REG			0x380f
185 #define OV8865_VTS_L(v)				((v) & GENMASK(7, 0))
186 #define OV8865_TIMING_MAX_VTS			0xffff
187 #define OV8865_TIMING_MIN_VTS			0x04
188 #define OV8865_OFFSET_X_H_REG			0x3810
189 #define OV8865_OFFSET_X_H(v)			(((v) & GENMASK(15, 8)) >> 8)
190 #define OV8865_OFFSET_X_L_REG			0x3811
191 #define OV8865_OFFSET_X_L(v)			((v) & GENMASK(7, 0))
192 #define OV8865_OFFSET_Y_H_REG			0x3812
193 #define OV8865_OFFSET_Y_H(v)			(((v) & GENMASK(14, 8)) >> 8)
194 #define OV8865_OFFSET_Y_L_REG			0x3813
195 #define OV8865_OFFSET_Y_L(v)			((v) & GENMASK(7, 0))
196 #define OV8865_INC_X_ODD_REG			0x3814
197 #define OV8865_INC_X_ODD(v)			((v) & GENMASK(4, 0))
198 #define OV8865_INC_X_EVEN_REG			0x3815
199 #define OV8865_INC_X_EVEN(v)			((v) & GENMASK(4, 0))
200 #define OV8865_VSYNC_START_H_REG		0x3816
201 #define OV8865_VSYNC_START_H(v)			(((v) & GENMASK(15, 8)) >> 8)
202 #define OV8865_VSYNC_START_L_REG		0x3817
203 #define OV8865_VSYNC_START_L(v)			((v) & GENMASK(7, 0))
204 #define OV8865_VSYNC_END_H_REG			0x3818
205 #define OV8865_VSYNC_END_H(v)			(((v) & GENMASK(15, 8)) >> 8)
206 #define OV8865_VSYNC_END_L_REG			0x3819
207 #define OV8865_VSYNC_END_L(v)			((v) & GENMASK(7, 0))
208 #define OV8865_HSYNC_FIRST_H_REG		0x381a
209 #define OV8865_HSYNC_FIRST_H(v)			(((v) & GENMASK(15, 8)) >> 8)
210 #define OV8865_HSYNC_FIRST_L_REG		0x381b
211 #define OV8865_HSYNC_FIRST_L(v)			((v) & GENMASK(7, 0))
212 
213 #define OV8865_FORMAT1_REG			0x3820
214 #define OV8865_FORMAT1_FLIP_VERT_ISP_EN		BIT(2)
215 #define OV8865_FORMAT1_FLIP_VERT_SENSOR_EN	BIT(1)
216 #define OV8865_FORMAT2_REG			0x3821
217 #define OV8865_FORMAT2_HSYNC_EN			BIT(6)
218 #define OV8865_FORMAT2_FST_VBIN_EN		BIT(5)
219 #define OV8865_FORMAT2_FST_HBIN_EN		BIT(4)
220 #define OV8865_FORMAT2_ISP_HORZ_VAR2_EN		BIT(3)
221 #define OV8865_FORMAT2_FLIP_HORZ_ISP_EN		BIT(2)
222 #define OV8865_FORMAT2_FLIP_HORZ_SENSOR_EN	BIT(1)
223 #define OV8865_FORMAT2_SYNC_HBIN_EN		BIT(0)
224 
225 #define OV8865_INC_Y_ODD_REG			0x382a
226 #define OV8865_INC_Y_ODD(v)			((v) & GENMASK(4, 0))
227 #define OV8865_INC_Y_EVEN_REG			0x382b
228 #define OV8865_INC_Y_EVEN(v)			((v) & GENMASK(4, 0))
229 
230 #define OV8865_ABLC_NUM_REG			0x3830
231 #define OV8865_ABLC_NUM(v)			((v) & GENMASK(4, 0))
232 
233 #define OV8865_ZLINE_NUM_REG			0x3836
234 #define OV8865_ZLINE_NUM(v)			((v) & GENMASK(4, 0))
235 
236 #define OV8865_AUTO_SIZE_CTRL_REG		0x3841
237 #define OV8865_AUTO_SIZE_CTRL_OFFSET_Y_REG	BIT(5)
238 #define OV8865_AUTO_SIZE_CTRL_OFFSET_X_REG	BIT(4)
239 #define OV8865_AUTO_SIZE_CTRL_CROP_END_Y_REG	BIT(3)
240 #define OV8865_AUTO_SIZE_CTRL_CROP_END_X_REG	BIT(2)
241 #define OV8865_AUTO_SIZE_CTRL_CROP_START_Y_REG	BIT(1)
242 #define OV8865_AUTO_SIZE_CTRL_CROP_START_X_REG	BIT(0)
243 #define OV8865_AUTO_SIZE_X_OFFSET_H_REG		0x3842
244 #define OV8865_AUTO_SIZE_X_OFFSET_L_REG		0x3843
245 #define OV8865_AUTO_SIZE_Y_OFFSET_H_REG		0x3844
246 #define OV8865_AUTO_SIZE_Y_OFFSET_L_REG		0x3845
247 #define OV8865_AUTO_SIZE_BOUNDARIES_REG		0x3846
248 #define OV8865_AUTO_SIZE_BOUNDARIES_Y(v)	(((v) << 4) & GENMASK(7, 4))
249 #define OV8865_AUTO_SIZE_BOUNDARIES_X(v)	((v) & GENMASK(3, 0))
250 
251 /* PSRAM */
252 
253 #define OV8865_PSRAM_CTRL8_REG			0x3f08
254 
255 /* Black Level */
256 
257 #define OV8865_BLC_CTRL0_REG			0x4000
258 #define OV8865_BLC_CTRL0_TRIG_RANGE_EN		BIT(7)
259 #define OV8865_BLC_CTRL0_TRIG_FORMAT_EN		BIT(6)
260 #define OV8865_BLC_CTRL0_TRIG_GAIN_EN		BIT(5)
261 #define OV8865_BLC_CTRL0_TRIG_EXPOSURE_EN	BIT(4)
262 #define OV8865_BLC_CTRL0_TRIG_MANUAL_EN		BIT(3)
263 #define OV8865_BLC_CTRL0_FREEZE_EN		BIT(2)
264 #define OV8865_BLC_CTRL0_ALWAYS_EN		BIT(1)
265 #define OV8865_BLC_CTRL0_FILTER_EN		BIT(0)
266 #define OV8865_BLC_CTRL1_REG			0x4001
267 #define OV8865_BLC_CTRL1_DITHER_EN		BIT(7)
268 #define OV8865_BLC_CTRL1_ZERO_LINE_DIFF_EN	BIT(6)
269 #define OV8865_BLC_CTRL1_COL_SHIFT_256		(0 << 4)
270 #define OV8865_BLC_CTRL1_COL_SHIFT_128		(1 << 4)
271 #define OV8865_BLC_CTRL1_COL_SHIFT_64		(2 << 4)
272 #define OV8865_BLC_CTRL1_COL_SHIFT_32		(3 << 4)
273 #define OV8865_BLC_CTRL1_OFFSET_LIMIT_EN	BIT(2)
274 #define OV8865_BLC_CTRL1_COLUMN_CANCEL_EN	BIT(1)
275 #define OV8865_BLC_CTRL2_REG			0x4002
276 #define OV8865_BLC_CTRL3_REG			0x4003
277 #define OV8865_BLC_CTRL4_REG			0x4004
278 #define OV8865_BLC_CTRL5_REG			0x4005
279 #define OV8865_BLC_CTRL6_REG			0x4006
280 #define OV8865_BLC_CTRL7_REG			0x4007
281 #define OV8865_BLC_CTRL8_REG			0x4008
282 #define OV8865_BLC_CTRL9_REG			0x4009
283 #define OV8865_BLC_CTRLA_REG			0x400a
284 #define OV8865_BLC_CTRLB_REG			0x400b
285 #define OV8865_BLC_CTRLC_REG			0x400c
286 #define OV8865_BLC_CTRLD_REG			0x400d
287 #define OV8865_BLC_CTRLD_OFFSET_TRIGGER(v)	((v) & GENMASK(7, 0))
288 
289 #define OV8865_BLC_CTRL1F_REG			0x401f
290 #define OV8865_BLC_CTRL1F_RB_REVERSE		BIT(3)
291 #define OV8865_BLC_CTRL1F_INTERPOL_X_EN		BIT(2)
292 #define OV8865_BLC_CTRL1F_INTERPOL_Y_EN		BIT(1)
293 
294 #define OV8865_BLC_ANCHOR_LEFT_START_H_REG	0x4020
295 #define OV8865_BLC_ANCHOR_LEFT_START_H(v)	(((v) & GENMASK(11, 8)) >> 8)
296 #define OV8865_BLC_ANCHOR_LEFT_START_L_REG	0x4021
297 #define OV8865_BLC_ANCHOR_LEFT_START_L(v)	((v) & GENMASK(7, 0))
298 #define OV8865_BLC_ANCHOR_LEFT_END_H_REG	0x4022
299 #define OV8865_BLC_ANCHOR_LEFT_END_H(v)		(((v) & GENMASK(11, 8)) >> 8)
300 #define OV8865_BLC_ANCHOR_LEFT_END_L_REG	0x4023
301 #define OV8865_BLC_ANCHOR_LEFT_END_L(v)		((v) & GENMASK(7, 0))
302 #define OV8865_BLC_ANCHOR_RIGHT_START_H_REG	0x4024
303 #define OV8865_BLC_ANCHOR_RIGHT_START_H(v)	(((v) & GENMASK(11, 8)) >> 8)
304 #define OV8865_BLC_ANCHOR_RIGHT_START_L_REG	0x4025
305 #define OV8865_BLC_ANCHOR_RIGHT_START_L(v)	((v) & GENMASK(7, 0))
306 #define OV8865_BLC_ANCHOR_RIGHT_END_H_REG	0x4026
307 #define OV8865_BLC_ANCHOR_RIGHT_END_H(v)	(((v) & GENMASK(11, 8)) >> 8)
308 #define OV8865_BLC_ANCHOR_RIGHT_END_L_REG	0x4027
309 #define OV8865_BLC_ANCHOR_RIGHT_END_L(v)	((v) & GENMASK(7, 0))
310 
311 #define OV8865_BLC_TOP_ZLINE_START_REG		0x4028
312 #define OV8865_BLC_TOP_ZLINE_START(v)		((v) & GENMASK(5, 0))
313 #define OV8865_BLC_TOP_ZLINE_NUM_REG		0x4029
314 #define OV8865_BLC_TOP_ZLINE_NUM(v)		((v) & GENMASK(4, 0))
315 #define OV8865_BLC_TOP_BLKLINE_START_REG	0x402a
316 #define OV8865_BLC_TOP_BLKLINE_START(v)		((v) & GENMASK(5, 0))
317 #define OV8865_BLC_TOP_BLKLINE_NUM_REG		0x402b
318 #define OV8865_BLC_TOP_BLKLINE_NUM(v)		((v) & GENMASK(4, 0))
319 #define OV8865_BLC_BOT_ZLINE_START_REG		0x402c
320 #define OV8865_BLC_BOT_ZLINE_START(v)		((v) & GENMASK(5, 0))
321 #define OV8865_BLC_BOT_ZLINE_NUM_REG		0x402d
322 #define OV8865_BLC_BOT_ZLINE_NUM(v)		((v) & GENMASK(4, 0))
323 #define OV8865_BLC_BOT_BLKLINE_START_REG	0x402e
324 #define OV8865_BLC_BOT_BLKLINE_START(v)		((v) & GENMASK(5, 0))
325 #define OV8865_BLC_BOT_BLKLINE_NUM_REG		0x402f
326 #define OV8865_BLC_BOT_BLKLINE_NUM(v)		((v) & GENMASK(4, 0))
327 
328 #define OV8865_BLC_OFFSET_LIMIT_REG		0x4034
329 #define OV8865_BLC_OFFSET_LIMIT(v)		((v) & GENMASK(7, 0))
330 
331 /* VFIFO */
332 
333 #define OV8865_VFIFO_READ_START_H_REG		0x4600
334 #define OV8865_VFIFO_READ_START_H(v)		(((v) & GENMASK(15, 8)) >> 8)
335 #define OV8865_VFIFO_READ_START_L_REG		0x4601
336 #define OV8865_VFIFO_READ_START_L(v)		((v) & GENMASK(7, 0))
337 
338 /* MIPI */
339 
340 #define OV8865_MIPI_CTRL0_REG			0x4800
341 #define OV8865_MIPI_CTRL1_REG			0x4801
342 #define OV8865_MIPI_CTRL2_REG			0x4802
343 #define OV8865_MIPI_CTRL3_REG			0x4803
344 #define OV8865_MIPI_CTRL4_REG			0x4804
345 #define OV8865_MIPI_CTRL5_REG			0x4805
346 #define OV8865_MIPI_CTRL6_REG			0x4806
347 #define OV8865_MIPI_CTRL7_REG			0x4807
348 #define OV8865_MIPI_CTRL8_REG			0x4808
349 
350 #define OV8865_MIPI_FCNT_MAX_H_REG		0x4810
351 #define OV8865_MIPI_FCNT_MAX_L_REG		0x4811
352 
353 #define OV8865_MIPI_CTRL13_REG			0x4813
354 #define OV8865_MIPI_CTRL14_REG			0x4814
355 #define OV8865_MIPI_CTRL15_REG			0x4815
356 #define OV8865_MIPI_EMBEDDED_DT_REG		0x4816
357 
358 #define OV8865_MIPI_HS_ZERO_MIN_H_REG		0x4818
359 #define OV8865_MIPI_HS_ZERO_MIN_L_REG		0x4819
360 #define OV8865_MIPI_HS_TRAIL_MIN_H_REG		0x481a
361 #define OV8865_MIPI_HS_TRAIL_MIN_L_REG		0x481b
362 #define OV8865_MIPI_CLK_ZERO_MIN_H_REG		0x481c
363 #define OV8865_MIPI_CLK_ZERO_MIN_L_REG		0x481d
364 #define OV8865_MIPI_CLK_PREPARE_MAX_REG		0x481e
365 #define OV8865_MIPI_CLK_PREPARE_MIN_REG		0x481f
366 #define OV8865_MIPI_CLK_POST_MIN_H_REG		0x4820
367 #define OV8865_MIPI_CLK_POST_MIN_L_REG		0x4821
368 #define OV8865_MIPI_CLK_TRAIL_MIN_H_REG		0x4822
369 #define OV8865_MIPI_CLK_TRAIL_MIN_L_REG		0x4823
370 #define OV8865_MIPI_LPX_P_MIN_H_REG		0x4824
371 #define OV8865_MIPI_LPX_P_MIN_L_REG		0x4825
372 #define OV8865_MIPI_HS_PREPARE_MIN_REG		0x4826
373 #define OV8865_MIPI_HS_PREPARE_MAX_REG		0x4827
374 #define OV8865_MIPI_HS_EXIT_MIN_H_REG		0x4828
375 #define OV8865_MIPI_HS_EXIT_MIN_L_REG		0x4829
376 #define OV8865_MIPI_UI_HS_ZERO_MIN_REG		0x482a
377 #define OV8865_MIPI_UI_HS_TRAIL_MIN_REG		0x482b
378 #define OV8865_MIPI_UI_CLK_ZERO_MIN_REG		0x482c
379 #define OV8865_MIPI_UI_CLK_PREPARE_REG		0x482d
380 #define OV8865_MIPI_UI_CLK_POST_MIN_REG		0x482e
381 #define OV8865_MIPI_UI_CLK_TRAIL_MIN_REG	0x482f
382 #define OV8865_MIPI_UI_LPX_P_MIN_REG		0x4830
383 #define OV8865_MIPI_UI_HS_PREPARE_REG		0x4831
384 #define OV8865_MIPI_UI_HS_EXIT_MIN_REG		0x4832
385 #define OV8865_MIPI_PKT_START_SIZE_REG		0x4833
386 
387 #define OV8865_MIPI_PCLK_PERIOD_REG		0x4837
388 #define OV8865_MIPI_LP_GPIO0_REG		0x4838
389 #define OV8865_MIPI_LP_GPIO1_REG		0x4839
390 
391 #define OV8865_MIPI_CTRL3C_REG			0x483c
392 #define OV8865_MIPI_LP_GPIO4_REG		0x483d
393 
394 #define OV8865_MIPI_CTRL4A_REG			0x484a
395 #define OV8865_MIPI_CTRL4B_REG			0x484b
396 #define OV8865_MIPI_CTRL4C_REG			0x484c
397 #define OV8865_MIPI_LANE_TEST_PATTERN_REG	0x484d
398 #define OV8865_MIPI_FRAME_END_DELAY_REG		0x484e
399 #define OV8865_MIPI_CLOCK_TEST_PATTERN_REG	0x484f
400 #define OV8865_MIPI_LANE_SEL01_REG		0x4850
401 #define OV8865_MIPI_LANE_SEL01_LANE0(v)		(((v) << 0) & GENMASK(2, 0))
402 #define OV8865_MIPI_LANE_SEL01_LANE1(v)		(((v) << 4) & GENMASK(6, 4))
403 #define OV8865_MIPI_LANE_SEL23_REG		0x4851
404 #define OV8865_MIPI_LANE_SEL23_LANE2(v)		(((v) << 0) & GENMASK(2, 0))
405 #define OV8865_MIPI_LANE_SEL23_LANE3(v)		(((v) << 4) & GENMASK(6, 4))
406 
407 /* ISP */
408 
409 #define OV8865_ISP_CTRL0_REG			0x5000
410 #define OV8865_ISP_CTRL0_LENC_EN		BIT(7)
411 #define OV8865_ISP_CTRL0_WHITE_BALANCE_EN	BIT(4)
412 #define OV8865_ISP_CTRL0_DPC_BLACK_EN		BIT(2)
413 #define OV8865_ISP_CTRL0_DPC_WHITE_EN		BIT(1)
414 #define OV8865_ISP_CTRL1_REG			0x5001
415 #define OV8865_ISP_CTRL1_BLC_EN			BIT(0)
416 #define OV8865_ISP_CTRL2_REG			0x5002
417 #define OV8865_ISP_CTRL2_DEBUG			BIT(3)
418 #define OV8865_ISP_CTRL2_VARIOPIXEL_EN		BIT(2)
419 #define OV8865_ISP_CTRL2_VSYNC_LATCH_EN		BIT(0)
420 #define OV8865_ISP_CTRL3_REG			0x5003
421 
422 #define OV8865_ISP_GAIN_RED_H_REG		0x5018
423 #define OV8865_ISP_GAIN_RED_H(v)		(((v) & GENMASK(13, 6)) >> 6)
424 #define OV8865_ISP_GAIN_RED_L_REG		0x5019
425 #define OV8865_ISP_GAIN_RED_L(v)		((v) & GENMASK(5, 0))
426 #define OV8865_ISP_GAIN_GREEN_H_REG		0x501a
427 #define OV8865_ISP_GAIN_GREEN_H(v)		(((v) & GENMASK(13, 6)) >> 6)
428 #define OV8865_ISP_GAIN_GREEN_L_REG		0x501b
429 #define OV8865_ISP_GAIN_GREEN_L(v)		((v) & GENMASK(5, 0))
430 #define OV8865_ISP_GAIN_BLUE_H_REG		0x501c
431 #define OV8865_ISP_GAIN_BLUE_H(v)		(((v) & GENMASK(13, 6)) >> 6)
432 #define OV8865_ISP_GAIN_BLUE_L_REG		0x501d
433 #define OV8865_ISP_GAIN_BLUE_L(v)		((v) & GENMASK(5, 0))
434 
435 /* VarioPixel */
436 
437 #define OV8865_VAP_CTRL0_REG			0x5900
438 #define OV8865_VAP_CTRL1_REG			0x5901
439 #define OV8865_VAP_CTRL1_HSUB_COEF(v)		((((v) - 1) << 2) & \
440 						 GENMASK(3, 2))
441 #define OV8865_VAP_CTRL1_VSUB_COEF(v)		(((v) - 1) & GENMASK(1, 0))
442 
443 /* Pre-DSP */
444 
445 #define OV8865_PRE_CTRL0_REG			0x5e00
446 #define OV8865_PRE_CTRL0_PATTERN_EN		BIT(7)
447 #define OV8865_PRE_CTRL0_ROLLING_BAR_EN		BIT(6)
448 #define OV8865_PRE_CTRL0_TRANSPARENT_MODE	BIT(5)
449 #define OV8865_PRE_CTRL0_SQUARES_BW_MODE	BIT(4)
450 #define OV8865_PRE_CTRL0_PATTERN_COLOR_BARS	0
451 #define OV8865_PRE_CTRL0_PATTERN_RANDOM_DATA	1
452 #define OV8865_PRE_CTRL0_PATTERN_COLOR_SQUARES	2
453 #define OV8865_PRE_CTRL0_PATTERN_BLACK		3
454 
455 /* Pixel Array */
456 
457 #define OV8865_NATIVE_WIDTH			3296
458 #define OV8865_NATIVE_HEIGHT			2528
459 #define OV8865_ACTIVE_START_LEFT		16
460 #define OV8865_ACTIVE_START_TOP			40
461 #define OV8865_ACTIVE_WIDTH			3264
462 #define OV8865_ACTIVE_HEIGHT			2448
463 
464 /* Macros */
465 
466 #define ov8865_subdev_sensor(s) \
467 	container_of(s, struct ov8865_sensor, subdev)
468 
469 #define ov8865_ctrl_subdev(c) \
470 	(&container_of((c)->handler, struct ov8865_sensor, \
471 		       ctrls.handler)->subdev)
472 
473 /* Data structures */
474 
475 struct ov8865_register_value {
476 	u16 address;
477 	u8 value;
478 	unsigned int delay_ms;
479 };
480 
481 /*
482  * PLL1 Clock Tree:
483  *
484  * +-< EXTCLK
485  * |
486  * +-+ pll_pre_div_half (0x30a [0])
487  *   |
488  *   +-+ pll_pre_div (0x300 [2:0], special values:
489  *     |              0: 1, 1: 1.5, 3: 2.5, 4: 3, 5: 4, 7: 8)
490  *     +-+ pll_mul (0x301 [1:0], 0x302 [7:0])
491  *       |
492  *       +-+ m_div (0x303 [3:0])
493  *       | |
494  *       | +-> PHY_SCLK
495  *       | |
496  *       | +-+ mipi_div (0x304 [1:0], special values: 0: 4, 1: 5, 2: 6, 3: 8)
497  *       |   |
498  *       |   +-+ pclk_div (0x3020 [3])
499  *       |     |
500  *       |     +-> PCLK
501  *       |
502  *       +-+ sys_pre_div (0x305 [1:0], special values: 0: 3, 1: 4, 2: 5, 3: 6)
503  *         |
504  *         +-+ sys_div (0x306 [0])
505  *           |
506  *           +-+ sys_sel (0x3032 [7], 0: PLL1, 1: PLL2)
507  *             |
508  *             +-+ sclk_sel (0x3033 [1], 0: sys_sel, 1: PLL2 DAC_CLK)
509  *               |
510  *               +-+ sclk_pre_div (0x3106 [3:2], special values:
511  *                 |               0: 1, 1: 2, 2: 4, 3: 1)
512  *                 |
513  *                 +-+ sclk_div (0x3106 [7:4], special values: 0: 1)
514  *                   |
515  *                   +-> SCLK
516  */
517 
518 struct ov8865_pll1_config {
519 	unsigned int pll_pre_div_half;
520 	unsigned int pll_pre_div;
521 	unsigned int pll_mul;
522 	unsigned int m_div;
523 	unsigned int mipi_div;
524 	unsigned int pclk_div;
525 	unsigned int sys_pre_div;
526 	unsigned int sys_div;
527 };
528 
529 /*
530  * PLL2 Clock Tree:
531  *
532  * +-< EXTCLK
533  * |
534  * +-+ pll_pre_div_half (0x312 [4])
535  *   |
536  *   +-+ pll_pre_div (0x30b [2:0], special values:
537  *     |              0: 1, 1: 1.5, 3: 2.5, 4: 3, 5: 4, 7: 8)
538  *     +-+ pll_mul (0x30c [1:0], 0x30d [7:0])
539  *       |
540  *       +-+ dac_div (0x312 [3:0])
541  *       | |
542  *       | +-> DAC_CLK
543  *       |
544  *       +-+ sys_pre_div (0x30f [3:0])
545  *         |
546  *         +-+ sys_div (0x30e [2:0], special values:
547  *           |          0: 1, 1: 1.5, 3: 2.5, 4: 3, 5: 3.5, 6: 4, 7:5)
548  *           |
549  *           +-+ sys_sel (0x3032 [7], 0: PLL1, 1: PLL2)
550  *             |
551  *             +-+ sclk_sel (0x3033 [1], 0: sys_sel, 1: PLL2 DAC_CLK)
552  *               |
553  *               +-+ sclk_pre_div (0x3106 [3:2], special values:
554  *                 |               0: 1, 1: 2, 2: 4, 3: 1)
555  *                 |
556  *                 +-+ sclk_div (0x3106 [7:4], special values: 0: 1)
557  *                   |
558  *                   +-> SCLK
559  */
560 
561 struct ov8865_pll2_config {
562 	unsigned int pll_pre_div_half;
563 	unsigned int pll_pre_div;
564 	unsigned int pll_mul;
565 	unsigned int dac_div;
566 	unsigned int sys_pre_div;
567 	unsigned int sys_div;
568 };
569 
570 struct ov8865_sclk_config {
571 	unsigned int sys_sel;
572 	unsigned int sclk_sel;
573 	unsigned int sclk_pre_div;
574 	unsigned int sclk_div;
575 };
576 
577 struct ov8865_pll_configs {
578 	const struct ov8865_pll1_config *pll1_config;
579 	const struct ov8865_pll2_config *pll2_config_native;
580 	const struct ov8865_pll2_config *pll2_config_binning;
581 };
582 
583 /* Clock rate */
584 
585 enum extclk_rate {
586 	OV8865_19_2_MHZ,
587 	OV8865_24_MHZ,
588 	OV8865_NUM_SUPPORTED_RATES
589 };
590 
591 static const unsigned long supported_extclk_rates[] = {
592 	[OV8865_19_2_MHZ] = 19200000,
593 	[OV8865_24_MHZ] = 24000000,
594 };
595 
596 /*
597  * General formulas for (array-centered) mode calculation:
598  * - photo_array_width = 3296
599  * - crop_start_x = (photo_array_width - output_size_x) / 2
600  * - crop_end_x = crop_start_x + offset_x + output_size_x - 1
601  *
602  * - photo_array_height = 2480
603  * - crop_start_y = (photo_array_height - output_size_y) / 2
604  * - crop_end_y = crop_start_y + offset_y + output_size_y - 1
605  */
606 
607 struct ov8865_mode {
608 	unsigned int crop_start_x;
609 	unsigned int offset_x;
610 	unsigned int output_size_x;
611 	unsigned int crop_end_x;
612 	unsigned int hts;
613 
614 	unsigned int crop_start_y;
615 	unsigned int offset_y;
616 	unsigned int output_size_y;
617 	unsigned int crop_end_y;
618 	unsigned int vts;
619 
620 	/* With auto size, only output and total sizes need to be set. */
621 	bool size_auto;
622 	unsigned int size_auto_boundary_x;
623 	unsigned int size_auto_boundary_y;
624 
625 	bool binning_x;
626 	bool binning_y;
627 	bool variopixel;
628 	unsigned int variopixel_hsub_coef;
629 	unsigned int variopixel_vsub_coef;
630 
631 	/* Bits for the format register, used for binning. */
632 	bool sync_hbin;
633 	bool horz_var2;
634 
635 	unsigned int inc_x_odd;
636 	unsigned int inc_x_even;
637 	unsigned int inc_y_odd;
638 	unsigned int inc_y_even;
639 
640 	unsigned int vfifo_read_start;
641 
642 	unsigned int ablc_num;
643 	unsigned int zline_num;
644 
645 	unsigned int blc_top_zero_line_start;
646 	unsigned int blc_top_zero_line_num;
647 	unsigned int blc_top_black_line_start;
648 	unsigned int blc_top_black_line_num;
649 
650 	unsigned int blc_bottom_zero_line_start;
651 	unsigned int blc_bottom_zero_line_num;
652 	unsigned int blc_bottom_black_line_start;
653 	unsigned int blc_bottom_black_line_num;
654 
655 	u8 blc_col_shift_mask;
656 
657 	unsigned int blc_anchor_left_start;
658 	unsigned int blc_anchor_left_end;
659 	unsigned int blc_anchor_right_start;
660 	unsigned int blc_anchor_right_end;
661 
662 	bool pll2_binning;
663 
664 	const struct ov8865_register_value *register_values;
665 	unsigned int register_values_count;
666 };
667 
668 struct ov8865_state {
669 	const struct ov8865_mode *mode;
670 	u32 mbus_code;
671 
672 	bool streaming;
673 };
674 
675 struct ov8865_ctrls {
676 	struct v4l2_ctrl *link_freq;
677 	struct v4l2_ctrl *pixel_rate;
678 	struct v4l2_ctrl *hblank;
679 	struct v4l2_ctrl *vblank;
680 	struct v4l2_ctrl *exposure;
681 
682 	struct v4l2_ctrl_handler handler;
683 };
684 
685 struct ov8865_sensor {
686 	struct device *dev;
687 	struct i2c_client *i2c_client;
688 	struct gpio_desc *reset;
689 	struct gpio_desc *powerdown;
690 	struct regulator *avdd;
691 	struct regulator *dvdd;
692 	struct regulator *dovdd;
693 
694 	unsigned long extclk_rate;
695 	const struct ov8865_pll_configs *pll_configs;
696 	struct clk *extclk;
697 
698 	struct v4l2_fwnode_endpoint endpoint;
699 	struct v4l2_subdev subdev;
700 	struct media_pad pad;
701 
702 	struct mutex mutex;
703 
704 	struct ov8865_state state;
705 	struct ov8865_ctrls ctrls;
706 };
707 
708 /* Static definitions */
709 
710 /*
711  * PHY_SCLK = 720 MHz
712  * MIPI_PCLK = 90 MHz
713  */
714 
715 static const struct ov8865_pll1_config ov8865_pll1_config_native_19_2mhz = {
716 		.pll_pre_div_half	= 1,
717 		.pll_pre_div		= 2,
718 		.pll_mul		= 75,
719 		.m_div			= 1,
720 		.mipi_div		= 3,
721 		.pclk_div		= 1,
722 		.sys_pre_div		= 1,
723 		.sys_div		= 2,
724 };
725 
726 static const struct ov8865_pll1_config ov8865_pll1_config_native_24mhz = {
727 		.pll_pre_div_half	= 1,
728 		.pll_pre_div		= 0,
729 		.pll_mul		= 30,
730 		.m_div			= 1,
731 		.mipi_div		= 3,
732 		.pclk_div		= 1,
733 		.sys_pre_div		= 1,
734 		.sys_div		= 2,
735 };
736 
737 /*
738  * DAC_CLK = 360 MHz
739  * SCLK = 144 MHz
740  */
741 
742 static const struct ov8865_pll2_config ov8865_pll2_config_native_19_2mhz = {
743 		.pll_pre_div_half	= 1,
744 		.pll_pre_div		= 5,
745 		.pll_mul		= 75,
746 		.dac_div		= 1,
747 		.sys_pre_div		= 1,
748 		.sys_div		= 3,
749 };
750 
751 static const struct ov8865_pll2_config ov8865_pll2_config_native_24mhz = {
752 		.pll_pre_div_half	= 1,
753 		.pll_pre_div		= 0,
754 		.pll_mul		= 30,
755 		.dac_div		= 2,
756 		.sys_pre_div		= 5,
757 		.sys_div		= 0,
758 };
759 
760 /*
761  * DAC_CLK = 360 MHz
762  * SCLK = 72 MHz
763  */
764 
765 static const struct ov8865_pll2_config ov8865_pll2_config_binning_19_2mhz = {
766 	.pll_pre_div_half	= 1,
767 	.pll_pre_div		= 2,
768 	.pll_mul		= 75,
769 	.dac_div		= 2,
770 	.sys_pre_div		= 10,
771 	.sys_div		= 0,
772 };
773 
774 static const struct ov8865_pll2_config ov8865_pll2_config_binning_24mhz = {
775 	.pll_pre_div_half	= 1,
776 	.pll_pre_div		= 0,
777 	.pll_mul		= 30,
778 	.dac_div		= 2,
779 	.sys_pre_div		= 10,
780 	.sys_div		= 0,
781 };
782 
783 static const struct ov8865_pll_configs ov8865_pll_configs_19_2mhz = {
784 	.pll1_config = &ov8865_pll1_config_native_19_2mhz,
785 	.pll2_config_native = &ov8865_pll2_config_native_19_2mhz,
786 	.pll2_config_binning = &ov8865_pll2_config_binning_19_2mhz,
787 };
788 
789 static const struct ov8865_pll_configs ov8865_pll_configs_24mhz = {
790 	.pll1_config = &ov8865_pll1_config_native_24mhz,
791 	.pll2_config_native = &ov8865_pll2_config_native_24mhz,
792 	.pll2_config_binning = &ov8865_pll2_config_binning_24mhz,
793 };
794 
795 static const struct ov8865_pll_configs *ov8865_pll_configs[] = {
796 	&ov8865_pll_configs_19_2mhz,
797 	&ov8865_pll_configs_24mhz,
798 };
799 
800 static const struct ov8865_sclk_config ov8865_sclk_config_native = {
801 	.sys_sel		= 1,
802 	.sclk_sel		= 0,
803 	.sclk_pre_div		= 0,
804 	.sclk_div		= 0,
805 };
806 
807 static const struct ov8865_register_value ov8865_register_values_native[] = {
808 	/* Sensor */
809 
810 	{ 0x3700, 0x48 },
811 	{ 0x3701, 0x18 },
812 	{ 0x3702, 0x50 },
813 	{ 0x3703, 0x32 },
814 	{ 0x3704, 0x28 },
815 	{ 0x3706, 0x70 },
816 	{ 0x3707, 0x08 },
817 	{ 0x3708, 0x48 },
818 	{ 0x3709, 0x80 },
819 	{ 0x370a, 0x01 },
820 	{ 0x370b, 0x70 },
821 	{ 0x370c, 0x07 },
822 	{ 0x3718, 0x14 },
823 	{ 0x3712, 0x44 },
824 	{ 0x371e, 0x31 },
825 	{ 0x371f, 0x7f },
826 	{ 0x3720, 0x0a },
827 	{ 0x3721, 0x0a },
828 	{ 0x3724, 0x04 },
829 	{ 0x3725, 0x04 },
830 	{ 0x3726, 0x0c },
831 	{ 0x3728, 0x0a },
832 	{ 0x3729, 0x03 },
833 	{ 0x372a, 0x06 },
834 	{ 0x372b, 0xa6 },
835 	{ 0x372c, 0xa6 },
836 	{ 0x372d, 0xa6 },
837 	{ 0x372e, 0x0c },
838 	{ 0x372f, 0x20 },
839 	{ 0x3730, 0x02 },
840 	{ 0x3731, 0x0c },
841 	{ 0x3732, 0x28 },
842 	{ 0x3736, 0x30 },
843 	{ 0x373a, 0x04 },
844 	{ 0x373b, 0x18 },
845 	{ 0x373c, 0x14 },
846 	{ 0x373e, 0x06 },
847 	{ 0x375a, 0x0c },
848 	{ 0x375b, 0x26 },
849 	{ 0x375d, 0x04 },
850 	{ 0x375f, 0x28 },
851 	{ 0x3767, 0x1e },
852 	{ 0x3772, 0x46 },
853 	{ 0x3773, 0x04 },
854 	{ 0x3774, 0x2c },
855 	{ 0x3775, 0x13 },
856 	{ 0x3776, 0x10 },
857 	{ 0x37a0, 0x88 },
858 	{ 0x37a1, 0x7a },
859 	{ 0x37a2, 0x7a },
860 	{ 0x37a3, 0x02 },
861 	{ 0x37a5, 0x09 },
862 	{ 0x37a7, 0x88 },
863 	{ 0x37a8, 0xb0 },
864 	{ 0x37a9, 0xb0 },
865 	{ 0x37aa, 0x88 },
866 	{ 0x37ab, 0x5c },
867 	{ 0x37ac, 0x5c },
868 	{ 0x37ad, 0x55 },
869 	{ 0x37ae, 0x19 },
870 	{ 0x37af, 0x19 },
871 	{ 0x37b3, 0x84 },
872 	{ 0x37b4, 0x84 },
873 	{ 0x37b5, 0x66 },
874 
875 	/* PSRAM */
876 
877 	{ OV8865_PSRAM_CTRL8_REG, 0x16 },
878 
879 	/* ADC Sync */
880 
881 	{ 0x4500, 0x68 },
882 };
883 
884 static const struct ov8865_register_value ov8865_register_values_binning[] = {
885 	/* Sensor */
886 
887 	{ 0x3700, 0x24 },
888 	{ 0x3701, 0x0c },
889 	{ 0x3702, 0x28 },
890 	{ 0x3703, 0x19 },
891 	{ 0x3704, 0x14 },
892 	{ 0x3706, 0x38 },
893 	{ 0x3707, 0x04 },
894 	{ 0x3708, 0x24 },
895 	{ 0x3709, 0x40 },
896 	{ 0x370a, 0x00 },
897 	{ 0x370b, 0xb8 },
898 	{ 0x370c, 0x04 },
899 	{ 0x3718, 0x12 },
900 	{ 0x3712, 0x42 },
901 	{ 0x371e, 0x19 },
902 	{ 0x371f, 0x40 },
903 	{ 0x3720, 0x05 },
904 	{ 0x3721, 0x05 },
905 	{ 0x3724, 0x02 },
906 	{ 0x3725, 0x02 },
907 	{ 0x3726, 0x06 },
908 	{ 0x3728, 0x05 },
909 	{ 0x3729, 0x02 },
910 	{ 0x372a, 0x03 },
911 	{ 0x372b, 0x53 },
912 	{ 0x372c, 0xa3 },
913 	{ 0x372d, 0x53 },
914 	{ 0x372e, 0x06 },
915 	{ 0x372f, 0x10 },
916 	{ 0x3730, 0x01 },
917 	{ 0x3731, 0x06 },
918 	{ 0x3732, 0x14 },
919 	{ 0x3736, 0x20 },
920 	{ 0x373a, 0x02 },
921 	{ 0x373b, 0x0c },
922 	{ 0x373c, 0x0a },
923 	{ 0x373e, 0x03 },
924 	{ 0x375a, 0x06 },
925 	{ 0x375b, 0x13 },
926 	{ 0x375d, 0x02 },
927 	{ 0x375f, 0x14 },
928 	{ 0x3767, 0x1c },
929 	{ 0x3772, 0x23 },
930 	{ 0x3773, 0x02 },
931 	{ 0x3774, 0x16 },
932 	{ 0x3775, 0x12 },
933 	{ 0x3776, 0x08 },
934 	{ 0x37a0, 0x44 },
935 	{ 0x37a1, 0x3d },
936 	{ 0x37a2, 0x3d },
937 	{ 0x37a3, 0x01 },
938 	{ 0x37a5, 0x08 },
939 	{ 0x37a7, 0x44 },
940 	{ 0x37a8, 0x58 },
941 	{ 0x37a9, 0x58 },
942 	{ 0x37aa, 0x44 },
943 	{ 0x37ab, 0x2e },
944 	{ 0x37ac, 0x2e },
945 	{ 0x37ad, 0x33 },
946 	{ 0x37ae, 0x0d },
947 	{ 0x37af, 0x0d },
948 	{ 0x37b3, 0x42 },
949 	{ 0x37b4, 0x42 },
950 	{ 0x37b5, 0x33 },
951 
952 	/* PSRAM */
953 
954 	{ OV8865_PSRAM_CTRL8_REG, 0x0b },
955 
956 	/* ADC Sync */
957 
958 	{ 0x4500, 0x40 },
959 };
960 
961 static const struct ov8865_mode ov8865_modes[] = {
962 	/* 3264x2448 */
963 	{
964 		/* Horizontal */
965 		.output_size_x			= 3264,
966 		.hts				= 3888,
967 
968 		/* Vertical */
969 		.output_size_y			= 2448,
970 		.vts				= 2470,
971 
972 		.size_auto			= true,
973 		.size_auto_boundary_x		= 8,
974 		.size_auto_boundary_y		= 4,
975 
976 		/* Subsample increase */
977 		.inc_x_odd			= 1,
978 		.inc_x_even			= 1,
979 		.inc_y_odd			= 1,
980 		.inc_y_even			= 1,
981 
982 		/* VFIFO */
983 		.vfifo_read_start		= 16,
984 
985 		.ablc_num			= 4,
986 		.zline_num			= 1,
987 
988 		/* Black Level */
989 
990 		.blc_top_zero_line_start	= 0,
991 		.blc_top_zero_line_num		= 2,
992 		.blc_top_black_line_start	= 4,
993 		.blc_top_black_line_num		= 4,
994 
995 		.blc_bottom_zero_line_start	= 2,
996 		.blc_bottom_zero_line_num	= 2,
997 		.blc_bottom_black_line_start	= 8,
998 		.blc_bottom_black_line_num	= 2,
999 
1000 		.blc_anchor_left_start		= 576,
1001 		.blc_anchor_left_end		= 831,
1002 		.blc_anchor_right_start		= 1984,
1003 		.blc_anchor_right_end		= 2239,
1004 
1005 		/* PLL */
1006 		.pll2_binning			= false,
1007 
1008 		/* Registers */
1009 		.register_values	= ov8865_register_values_native,
1010 		.register_values_count	=
1011 			ARRAY_SIZE(ov8865_register_values_native),
1012 	},
1013 	/* 3264x1836 */
1014 	{
1015 		/* Horizontal */
1016 		.output_size_x			= 3264,
1017 		.hts				= 3888,
1018 
1019 		/* Vertical */
1020 		.output_size_y			= 1836,
1021 		.vts				= 2470,
1022 
1023 		.size_auto			= true,
1024 		.size_auto_boundary_x		= 8,
1025 		.size_auto_boundary_y		= 4,
1026 
1027 		/* Subsample increase */
1028 		.inc_x_odd			= 1,
1029 		.inc_x_even			= 1,
1030 		.inc_y_odd			= 1,
1031 		.inc_y_even			= 1,
1032 
1033 		/* VFIFO */
1034 		.vfifo_read_start		= 16,
1035 
1036 		.ablc_num			= 4,
1037 		.zline_num			= 1,
1038 
1039 		/* Black Level */
1040 
1041 		.blc_top_zero_line_start	= 0,
1042 		.blc_top_zero_line_num		= 2,
1043 		.blc_top_black_line_start	= 4,
1044 		.blc_top_black_line_num		= 4,
1045 
1046 		.blc_bottom_zero_line_start	= 2,
1047 		.blc_bottom_zero_line_num	= 2,
1048 		.blc_bottom_black_line_start	= 8,
1049 		.blc_bottom_black_line_num	= 2,
1050 
1051 		.blc_anchor_left_start		= 576,
1052 		.blc_anchor_left_end		= 831,
1053 		.blc_anchor_right_start		= 1984,
1054 		.blc_anchor_right_end		= 2239,
1055 
1056 		/* PLL */
1057 		.pll2_binning			= false,
1058 
1059 		/* Registers */
1060 		.register_values	= ov8865_register_values_native,
1061 		.register_values_count	=
1062 			ARRAY_SIZE(ov8865_register_values_native),
1063 	},
1064 	/* 1632x1224 */
1065 	{
1066 		/* Horizontal */
1067 		.output_size_x			= 1632,
1068 		.hts				= 1923,
1069 
1070 		/* Vertical */
1071 		.output_size_y			= 1224,
1072 		.vts				= 1248,
1073 
1074 		.size_auto			= true,
1075 		.size_auto_boundary_x		= 8,
1076 		.size_auto_boundary_y		= 8,
1077 
1078 		/* Subsample increase */
1079 		.inc_x_odd			= 3,
1080 		.inc_x_even			= 1,
1081 		.inc_y_odd			= 3,
1082 		.inc_y_even			= 1,
1083 
1084 		/* Binning */
1085 		.binning_y			= true,
1086 		.sync_hbin			= true,
1087 
1088 		/* VFIFO */
1089 		.vfifo_read_start		= 116,
1090 
1091 		.ablc_num			= 8,
1092 		.zline_num			= 2,
1093 
1094 		/* Black Level */
1095 
1096 		.blc_top_zero_line_start	= 0,
1097 		.blc_top_zero_line_num		= 2,
1098 		.blc_top_black_line_start	= 4,
1099 		.blc_top_black_line_num		= 4,
1100 
1101 		.blc_bottom_zero_line_start	= 2,
1102 		.blc_bottom_zero_line_num	= 2,
1103 		.blc_bottom_black_line_start	= 8,
1104 		.blc_bottom_black_line_num	= 2,
1105 
1106 		.blc_anchor_left_start		= 288,
1107 		.blc_anchor_left_end		= 415,
1108 		.blc_anchor_right_start		= 992,
1109 		.blc_anchor_right_end		= 1119,
1110 
1111 		/* PLL */
1112 		.pll2_binning			= true,
1113 
1114 		/* Registers */
1115 		.register_values	= ov8865_register_values_binning,
1116 		.register_values_count	=
1117 			ARRAY_SIZE(ov8865_register_values_binning),
1118 	},
1119 	/* 800x600 (SVGA) */
1120 	{
1121 		/* Horizontal */
1122 		.output_size_x			= 800,
1123 		.hts				= 1250,
1124 
1125 		/* Vertical */
1126 		.output_size_y			= 600,
1127 		.vts				= 640,
1128 
1129 		.size_auto			= true,
1130 		.size_auto_boundary_x		= 8,
1131 		.size_auto_boundary_y		= 8,
1132 
1133 		/* Subsample increase */
1134 		.inc_x_odd			= 3,
1135 		.inc_x_even			= 1,
1136 		.inc_y_odd			= 5,
1137 		.inc_y_even			= 3,
1138 
1139 		/* Binning */
1140 		.binning_y			= true,
1141 		.variopixel			= true,
1142 		.variopixel_hsub_coef		= 2,
1143 		.variopixel_vsub_coef		= 1,
1144 		.sync_hbin			= true,
1145 		.horz_var2			= true,
1146 
1147 		/* VFIFO */
1148 		.vfifo_read_start		= 80,
1149 
1150 		.ablc_num			= 8,
1151 		.zline_num			= 2,
1152 
1153 		/* Black Level */
1154 
1155 		.blc_top_zero_line_start	= 0,
1156 		.blc_top_zero_line_num		= 2,
1157 		.blc_top_black_line_start	= 2,
1158 		.blc_top_black_line_num		= 2,
1159 
1160 		.blc_bottom_zero_line_start	= 0,
1161 		.blc_bottom_zero_line_num	= 0,
1162 		.blc_bottom_black_line_start	= 4,
1163 		.blc_bottom_black_line_num	= 2,
1164 
1165 		.blc_col_shift_mask	= OV8865_BLC_CTRL1_COL_SHIFT_128,
1166 
1167 		.blc_anchor_left_start		= 288,
1168 		.blc_anchor_left_end		= 415,
1169 		.blc_anchor_right_start		= 992,
1170 		.blc_anchor_right_end		= 1119,
1171 
1172 		/* PLL */
1173 		.pll2_binning			= true,
1174 
1175 		/* Registers */
1176 		.register_values	= ov8865_register_values_binning,
1177 		.register_values_count	=
1178 			ARRAY_SIZE(ov8865_register_values_binning),
1179 	},
1180 };
1181 
1182 static const u32 ov8865_mbus_codes[] = {
1183 	MEDIA_BUS_FMT_SBGGR10_1X10,
1184 };
1185 
1186 static const struct ov8865_register_value ov8865_init_sequence[] = {
1187 	/* Analog */
1188 
1189 	{ 0x3604, 0x04 },
1190 	{ 0x3602, 0x30 },
1191 	{ 0x3605, 0x00 },
1192 	{ 0x3607, 0x20 },
1193 	{ 0x3608, 0x11 },
1194 	{ 0x3609, 0x68 },
1195 	{ 0x360a, 0x40 },
1196 	{ 0x360c, 0xdd },
1197 	{ 0x360e, 0x0c },
1198 	{ 0x3610, 0x07 },
1199 	{ 0x3612, 0x86 },
1200 	{ 0x3613, 0x58 },
1201 	{ 0x3614, 0x28 },
1202 	{ 0x3617, 0x40 },
1203 	{ 0x3618, 0x5a },
1204 	{ 0x3619, 0x9b },
1205 	{ 0x361c, 0x00 },
1206 	{ 0x361d, 0x60 },
1207 	{ 0x3631, 0x60 },
1208 	{ 0x3633, 0x10 },
1209 	{ 0x3634, 0x10 },
1210 	{ 0x3635, 0x10 },
1211 	{ 0x3636, 0x10 },
1212 	{ 0x3638, 0xff },
1213 	{ 0x3641, 0x55 },
1214 	{ 0x3646, 0x86 },
1215 	{ 0x3647, 0x27 },
1216 	{ 0x364a, 0x1b },
1217 
1218 	/* Sensor */
1219 
1220 	{ 0x3700, 0x24 },
1221 	{ 0x3701, 0x0c },
1222 	{ 0x3702, 0x28 },
1223 	{ 0x3703, 0x19 },
1224 	{ 0x3704, 0x14 },
1225 	{ 0x3705, 0x00 },
1226 	{ 0x3706, 0x38 },
1227 	{ 0x3707, 0x04 },
1228 	{ 0x3708, 0x24 },
1229 	{ 0x3709, 0x40 },
1230 	{ 0x370a, 0x00 },
1231 	{ 0x370b, 0xb8 },
1232 	{ 0x370c, 0x04 },
1233 	{ 0x3718, 0x12 },
1234 	{ 0x3719, 0x31 },
1235 	{ 0x3712, 0x42 },
1236 	{ 0x3714, 0x12 },
1237 	{ 0x371e, 0x19 },
1238 	{ 0x371f, 0x40 },
1239 	{ 0x3720, 0x05 },
1240 	{ 0x3721, 0x05 },
1241 	{ 0x3724, 0x02 },
1242 	{ 0x3725, 0x02 },
1243 	{ 0x3726, 0x06 },
1244 	{ 0x3728, 0x05 },
1245 	{ 0x3729, 0x02 },
1246 	{ 0x372a, 0x03 },
1247 	{ 0x372b, 0x53 },
1248 	{ 0x372c, 0xa3 },
1249 	{ 0x372d, 0x53 },
1250 	{ 0x372e, 0x06 },
1251 	{ 0x372f, 0x10 },
1252 	{ 0x3730, 0x01 },
1253 	{ 0x3731, 0x06 },
1254 	{ 0x3732, 0x14 },
1255 	{ 0x3733, 0x10 },
1256 	{ 0x3734, 0x40 },
1257 	{ 0x3736, 0x20 },
1258 	{ 0x373a, 0x02 },
1259 	{ 0x373b, 0x0c },
1260 	{ 0x373c, 0x0a },
1261 	{ 0x373e, 0x03 },
1262 	{ 0x3755, 0x40 },
1263 	{ 0x3758, 0x00 },
1264 	{ 0x3759, 0x4c },
1265 	{ 0x375a, 0x06 },
1266 	{ 0x375b, 0x13 },
1267 	{ 0x375c, 0x40 },
1268 	{ 0x375d, 0x02 },
1269 	{ 0x375e, 0x00 },
1270 	{ 0x375f, 0x14 },
1271 	{ 0x3767, 0x1c },
1272 	{ 0x3768, 0x04 },
1273 	{ 0x3769, 0x20 },
1274 	{ 0x376c, 0xc0 },
1275 	{ 0x376d, 0xc0 },
1276 	{ 0x376a, 0x08 },
1277 	{ 0x3761, 0x00 },
1278 	{ 0x3762, 0x00 },
1279 	{ 0x3763, 0x00 },
1280 	{ 0x3766, 0xff },
1281 	{ 0x376b, 0x42 },
1282 	{ 0x3772, 0x23 },
1283 	{ 0x3773, 0x02 },
1284 	{ 0x3774, 0x16 },
1285 	{ 0x3775, 0x12 },
1286 	{ 0x3776, 0x08 },
1287 	{ 0x37a0, 0x44 },
1288 	{ 0x37a1, 0x3d },
1289 	{ 0x37a2, 0x3d },
1290 	{ 0x37a3, 0x01 },
1291 	{ 0x37a4, 0x00 },
1292 	{ 0x37a5, 0x08 },
1293 	{ 0x37a6, 0x00 },
1294 	{ 0x37a7, 0x44 },
1295 	{ 0x37a8, 0x58 },
1296 	{ 0x37a9, 0x58 },
1297 	{ 0x3760, 0x00 },
1298 	{ 0x376f, 0x01 },
1299 	{ 0x37aa, 0x44 },
1300 	{ 0x37ab, 0x2e },
1301 	{ 0x37ac, 0x2e },
1302 	{ 0x37ad, 0x33 },
1303 	{ 0x37ae, 0x0d },
1304 	{ 0x37af, 0x0d },
1305 	{ 0x37b0, 0x00 },
1306 	{ 0x37b1, 0x00 },
1307 	{ 0x37b2, 0x00 },
1308 	{ 0x37b3, 0x42 },
1309 	{ 0x37b4, 0x42 },
1310 	{ 0x37b5, 0x33 },
1311 	{ 0x37b6, 0x00 },
1312 	{ 0x37b7, 0x00 },
1313 	{ 0x37b8, 0x00 },
1314 	{ 0x37b9, 0xff },
1315 
1316 	/* ADC Sync */
1317 
1318 	{ 0x4503, 0x10 },
1319 };
1320 
1321 static const s64 ov8865_link_freq_menu[] = {
1322 	360000000,
1323 };
1324 
1325 static const char *const ov8865_test_pattern_menu[] = {
1326 	"Disabled",
1327 	"Random data",
1328 	"Color bars",
1329 	"Color bars with rolling bar",
1330 	"Color squares",
1331 	"Color squares with rolling bar"
1332 };
1333 
1334 static const u8 ov8865_test_pattern_bits[] = {
1335 	0,
1336 	OV8865_PRE_CTRL0_PATTERN_EN | OV8865_PRE_CTRL0_PATTERN_RANDOM_DATA,
1337 	OV8865_PRE_CTRL0_PATTERN_EN | OV8865_PRE_CTRL0_PATTERN_COLOR_BARS,
1338 	OV8865_PRE_CTRL0_PATTERN_EN | OV8865_PRE_CTRL0_ROLLING_BAR_EN |
1339 	OV8865_PRE_CTRL0_PATTERN_COLOR_BARS,
1340 	OV8865_PRE_CTRL0_PATTERN_EN | OV8865_PRE_CTRL0_PATTERN_COLOR_SQUARES,
1341 	OV8865_PRE_CTRL0_PATTERN_EN | OV8865_PRE_CTRL0_ROLLING_BAR_EN |
1342 	OV8865_PRE_CTRL0_PATTERN_COLOR_SQUARES,
1343 };
1344 
1345 /* Input/Output */
1346 
ov8865_read(struct ov8865_sensor * sensor,u16 address,u8 * value)1347 static int ov8865_read(struct ov8865_sensor *sensor, u16 address, u8 *value)
1348 {
1349 	unsigned char data[2] = { address >> 8, address & 0xff };
1350 	struct i2c_client *client = sensor->i2c_client;
1351 	int ret;
1352 
1353 	ret = i2c_master_send(client, data, sizeof(data));
1354 	if (ret < 0) {
1355 		dev_dbg(&client->dev, "i2c send error at address %#04x\n",
1356 			address);
1357 		return ret;
1358 	}
1359 
1360 	ret = i2c_master_recv(client, value, 1);
1361 	if (ret < 0) {
1362 		dev_dbg(&client->dev, "i2c recv error at address %#04x\n",
1363 			address);
1364 		return ret;
1365 	}
1366 
1367 	return 0;
1368 }
1369 
ov8865_write(struct ov8865_sensor * sensor,u16 address,u8 value)1370 static int ov8865_write(struct ov8865_sensor *sensor, u16 address, u8 value)
1371 {
1372 	unsigned char data[3] = { address >> 8, address & 0xff, value };
1373 	struct i2c_client *client = sensor->i2c_client;
1374 	int ret;
1375 
1376 	ret = i2c_master_send(client, data, sizeof(data));
1377 	if (ret < 0) {
1378 		dev_dbg(&client->dev, "i2c send error at address %#04x\n",
1379 			address);
1380 		return ret;
1381 	}
1382 
1383 	return 0;
1384 }
1385 
ov8865_write_sequence(struct ov8865_sensor * sensor,const struct ov8865_register_value * sequence,unsigned int sequence_count)1386 static int ov8865_write_sequence(struct ov8865_sensor *sensor,
1387 				 const struct ov8865_register_value *sequence,
1388 				 unsigned int sequence_count)
1389 {
1390 	unsigned int i;
1391 	int ret = 0;
1392 
1393 	for (i = 0; i < sequence_count; i++) {
1394 		ret = ov8865_write(sensor, sequence[i].address,
1395 				   sequence[i].value);
1396 		if (ret)
1397 			break;
1398 
1399 		if (sequence[i].delay_ms)
1400 			msleep(sequence[i].delay_ms);
1401 	}
1402 
1403 	return ret;
1404 }
1405 
ov8865_update_bits(struct ov8865_sensor * sensor,u16 address,u8 mask,u8 bits)1406 static int ov8865_update_bits(struct ov8865_sensor *sensor, u16 address,
1407 			      u8 mask, u8 bits)
1408 {
1409 	u8 value = 0;
1410 	int ret;
1411 
1412 	ret = ov8865_read(sensor, address, &value);
1413 	if (ret)
1414 		return ret;
1415 
1416 	value &= ~mask;
1417 	value |= bits;
1418 
1419 	return ov8865_write(sensor, address, value);
1420 }
1421 
1422 /* Sensor */
1423 
ov8865_sw_reset(struct ov8865_sensor * sensor)1424 static int ov8865_sw_reset(struct ov8865_sensor *sensor)
1425 {
1426 	return ov8865_write(sensor, OV8865_SW_RESET_REG, OV8865_SW_RESET_RESET);
1427 }
1428 
ov8865_sw_standby(struct ov8865_sensor * sensor,int standby)1429 static int ov8865_sw_standby(struct ov8865_sensor *sensor, int standby)
1430 {
1431 	u8 value = 0;
1432 
1433 	if (!standby)
1434 		value = OV8865_SW_STANDBY_STREAM_ON;
1435 
1436 	return ov8865_write(sensor, OV8865_SW_STANDBY_REG, value);
1437 }
1438 
ov8865_chip_id_check(struct ov8865_sensor * sensor)1439 static int ov8865_chip_id_check(struct ov8865_sensor *sensor)
1440 {
1441 	u16 regs[] = { OV8865_CHIP_ID_HH_REG, OV8865_CHIP_ID_H_REG,
1442 		       OV8865_CHIP_ID_L_REG };
1443 	u8 values[] = { OV8865_CHIP_ID_HH_VALUE, OV8865_CHIP_ID_H_VALUE,
1444 			OV8865_CHIP_ID_L_VALUE };
1445 	unsigned int i;
1446 	u8 value;
1447 	int ret;
1448 
1449 	for (i = 0; i < ARRAY_SIZE(regs); i++) {
1450 		ret = ov8865_read(sensor, regs[i], &value);
1451 		if (ret < 0)
1452 			return ret;
1453 
1454 		if (value != values[i]) {
1455 			dev_err(sensor->dev,
1456 				"chip id value mismatch: %#x instead of %#x\n",
1457 				value, values[i]);
1458 			return -EINVAL;
1459 		}
1460 	}
1461 
1462 	return 0;
1463 }
1464 
ov8865_charge_pump_configure(struct ov8865_sensor * sensor)1465 static int ov8865_charge_pump_configure(struct ov8865_sensor *sensor)
1466 {
1467 	return ov8865_write(sensor, OV8865_PUMP_CLK_DIV_REG,
1468 			    OV8865_PUMP_CLK_DIV_PUMP_P(1));
1469 }
1470 
ov8865_mipi_configure(struct ov8865_sensor * sensor)1471 static int ov8865_mipi_configure(struct ov8865_sensor *sensor)
1472 {
1473 	struct v4l2_mbus_config_mipi_csi2 *bus_mipi_csi2 =
1474 		&sensor->endpoint.bus.mipi_csi2;
1475 	unsigned int lanes_count = bus_mipi_csi2->num_data_lanes;
1476 	int ret;
1477 
1478 	ret = ov8865_write(sensor, OV8865_MIPI_SC_CTRL0_REG,
1479 			   OV8865_MIPI_SC_CTRL0_LANES(lanes_count) |
1480 			   OV8865_MIPI_SC_CTRL0_MIPI_EN |
1481 			   OV8865_MIPI_SC_CTRL0_UNKNOWN);
1482 	if (ret)
1483 		return ret;
1484 
1485 	ret = ov8865_write(sensor, OV8865_MIPI_SC_CTRL2_REG,
1486 			   OV8865_MIPI_SC_CTRL2_PD_MIPI_RST_SYNC);
1487 	if (ret)
1488 		return ret;
1489 
1490 	if (lanes_count >= 2) {
1491 		ret = ov8865_write(sensor, OV8865_MIPI_LANE_SEL01_REG,
1492 				   OV8865_MIPI_LANE_SEL01_LANE0(0) |
1493 				   OV8865_MIPI_LANE_SEL01_LANE1(1));
1494 		if (ret)
1495 			return ret;
1496 	}
1497 
1498 	if (lanes_count >= 4) {
1499 		ret = ov8865_write(sensor, OV8865_MIPI_LANE_SEL23_REG,
1500 				   OV8865_MIPI_LANE_SEL23_LANE2(2) |
1501 				   OV8865_MIPI_LANE_SEL23_LANE3(3));
1502 		if (ret)
1503 			return ret;
1504 	}
1505 
1506 	ret = ov8865_update_bits(sensor, OV8865_CLK_SEL1_REG,
1507 				 OV8865_CLK_SEL1_MIPI_EOF,
1508 				 OV8865_CLK_SEL1_MIPI_EOF);
1509 	if (ret)
1510 		return ret;
1511 
1512 	/*
1513 	 * This value might need to change depending on PCLK rate,
1514 	 * but it's unclear how. This value seems to generally work
1515 	 * while the default value was found to cause transmission errors.
1516 	 */
1517 	return ov8865_write(sensor, OV8865_MIPI_PCLK_PERIOD_REG, 0x16);
1518 }
1519 
ov8865_black_level_configure(struct ov8865_sensor * sensor)1520 static int ov8865_black_level_configure(struct ov8865_sensor *sensor)
1521 {
1522 	int ret;
1523 
1524 	/* Trigger BLC on relevant events and enable filter. */
1525 	ret = ov8865_write(sensor, OV8865_BLC_CTRL0_REG,
1526 			   OV8865_BLC_CTRL0_TRIG_RANGE_EN |
1527 			   OV8865_BLC_CTRL0_TRIG_FORMAT_EN |
1528 			   OV8865_BLC_CTRL0_TRIG_GAIN_EN |
1529 			   OV8865_BLC_CTRL0_TRIG_EXPOSURE_EN |
1530 			   OV8865_BLC_CTRL0_FILTER_EN);
1531 	if (ret)
1532 		return ret;
1533 
1534 	/* Lower BLC offset trigger threshold. */
1535 	ret = ov8865_write(sensor, OV8865_BLC_CTRLD_REG,
1536 			   OV8865_BLC_CTRLD_OFFSET_TRIGGER(16));
1537 	if (ret)
1538 		return ret;
1539 
1540 	ret = ov8865_write(sensor, OV8865_BLC_CTRL1F_REG, 0);
1541 	if (ret)
1542 		return ret;
1543 
1544 	/* Increase BLC offset maximum limit. */
1545 	return ov8865_write(sensor, OV8865_BLC_OFFSET_LIMIT_REG,
1546 			    OV8865_BLC_OFFSET_LIMIT(63));
1547 }
1548 
ov8865_isp_configure(struct ov8865_sensor * sensor)1549 static int ov8865_isp_configure(struct ov8865_sensor *sensor)
1550 {
1551 	int ret;
1552 
1553 	/* Disable lens correction. */
1554 	ret = ov8865_write(sensor, OV8865_ISP_CTRL0_REG,
1555 			   OV8865_ISP_CTRL0_WHITE_BALANCE_EN |
1556 			   OV8865_ISP_CTRL0_DPC_BLACK_EN |
1557 			   OV8865_ISP_CTRL0_DPC_WHITE_EN);
1558 	if (ret)
1559 		return ret;
1560 
1561 	return ov8865_write(sensor, OV8865_ISP_CTRL1_REG,
1562 			    OV8865_ISP_CTRL1_BLC_EN);
1563 }
1564 
ov8865_mode_pll1_rate(struct ov8865_sensor * sensor,const struct ov8865_mode * mode)1565 static unsigned long ov8865_mode_pll1_rate(struct ov8865_sensor *sensor,
1566 					   const struct ov8865_mode *mode)
1567 {
1568 	const struct ov8865_pll1_config *config;
1569 	unsigned long pll1_rate;
1570 
1571 	config = sensor->pll_configs->pll1_config;
1572 	pll1_rate = sensor->extclk_rate * config->pll_mul / config->pll_pre_div_half;
1573 
1574 	switch (config->pll_pre_div) {
1575 	case 0:
1576 		break;
1577 	case 1:
1578 		pll1_rate *= 3;
1579 		pll1_rate /= 2;
1580 		break;
1581 	case 3:
1582 		pll1_rate *= 5;
1583 		pll1_rate /= 2;
1584 		break;
1585 	case 4:
1586 		pll1_rate /= 3;
1587 		break;
1588 	case 5:
1589 		pll1_rate /= 4;
1590 		break;
1591 	case 7:
1592 		pll1_rate /= 8;
1593 		break;
1594 	default:
1595 		pll1_rate /= config->pll_pre_div;
1596 		break;
1597 	}
1598 
1599 	return pll1_rate;
1600 }
1601 
ov8865_mode_pll1_configure(struct ov8865_sensor * sensor,const struct ov8865_mode * mode,u32 mbus_code)1602 static int ov8865_mode_pll1_configure(struct ov8865_sensor *sensor,
1603 				      const struct ov8865_mode *mode,
1604 				      u32 mbus_code)
1605 {
1606 	const struct ov8865_pll1_config *config;
1607 	u8 value;
1608 	int ret;
1609 
1610 	config = sensor->pll_configs->pll1_config;
1611 
1612 	switch (mbus_code) {
1613 	case MEDIA_BUS_FMT_SBGGR10_1X10:
1614 		value = OV8865_MIPI_BIT_SEL(10);
1615 		break;
1616 	default:
1617 		return -EINVAL;
1618 	}
1619 
1620 	ret = ov8865_write(sensor, OV8865_MIPI_BIT_SEL_REG, value);
1621 	if (ret)
1622 		return ret;
1623 
1624 	ret = ov8865_write(sensor, OV8865_PLL_CTRLA_REG,
1625 			   OV8865_PLL_CTRLA_PRE_DIV_HALF(config->pll_pre_div_half));
1626 	if (ret)
1627 		return ret;
1628 
1629 	ret = ov8865_write(sensor, OV8865_PLL_CTRL0_REG,
1630 			   OV8865_PLL_CTRL0_PRE_DIV(config->pll_pre_div));
1631 	if (ret)
1632 		return ret;
1633 
1634 	ret = ov8865_write(sensor, OV8865_PLL_CTRL1_REG,
1635 			   OV8865_PLL_CTRL1_MUL_H(config->pll_mul));
1636 	if (ret)
1637 		return ret;
1638 
1639 	ret = ov8865_write(sensor, OV8865_PLL_CTRL2_REG,
1640 			   OV8865_PLL_CTRL2_MUL_L(config->pll_mul));
1641 	if (ret)
1642 		return ret;
1643 
1644 	ret = ov8865_write(sensor, OV8865_PLL_CTRL3_REG,
1645 			   OV8865_PLL_CTRL3_M_DIV(config->m_div));
1646 	if (ret)
1647 		return ret;
1648 
1649 	ret = ov8865_write(sensor, OV8865_PLL_CTRL4_REG,
1650 			   OV8865_PLL_CTRL4_MIPI_DIV(config->mipi_div));
1651 	if (ret)
1652 		return ret;
1653 
1654 	ret = ov8865_update_bits(sensor, OV8865_PCLK_SEL_REG,
1655 				 OV8865_PCLK_SEL_PCLK_DIV_MASK,
1656 				 OV8865_PCLK_SEL_PCLK_DIV(config->pclk_div));
1657 	if (ret)
1658 		return ret;
1659 
1660 	ret = ov8865_write(sensor, OV8865_PLL_CTRL5_REG,
1661 			   OV8865_PLL_CTRL5_SYS_PRE_DIV(config->sys_pre_div));
1662 	if (ret)
1663 		return ret;
1664 
1665 	ret = ov8865_write(sensor, OV8865_PLL_CTRL6_REG,
1666 			   OV8865_PLL_CTRL6_SYS_DIV(config->sys_div));
1667 	if (ret)
1668 		return ret;
1669 
1670 	return ov8865_update_bits(sensor, OV8865_PLL_CTRL1E_REG,
1671 				  OV8865_PLL_CTRL1E_PLL1_NO_LAT,
1672 				  OV8865_PLL_CTRL1E_PLL1_NO_LAT);
1673 }
1674 
ov8865_mode_pll2_configure(struct ov8865_sensor * sensor,const struct ov8865_mode * mode)1675 static int ov8865_mode_pll2_configure(struct ov8865_sensor *sensor,
1676 				      const struct ov8865_mode *mode)
1677 {
1678 	const struct ov8865_pll2_config *config;
1679 	int ret;
1680 
1681 	config = mode->pll2_binning ? sensor->pll_configs->pll2_config_binning :
1682 				      sensor->pll_configs->pll2_config_native;
1683 
1684 	ret = ov8865_write(sensor, OV8865_PLL_CTRL12_REG,
1685 			   OV8865_PLL_CTRL12_PRE_DIV_HALF(config->pll_pre_div_half) |
1686 			   OV8865_PLL_CTRL12_DAC_DIV(config->dac_div));
1687 	if (ret)
1688 		return ret;
1689 
1690 	ret = ov8865_write(sensor, OV8865_PLL_CTRLB_REG,
1691 			   OV8865_PLL_CTRLB_PRE_DIV(config->pll_pre_div));
1692 	if (ret)
1693 		return ret;
1694 
1695 	ret = ov8865_write(sensor, OV8865_PLL_CTRLC_REG,
1696 			   OV8865_PLL_CTRLC_MUL_H(config->pll_mul));
1697 	if (ret)
1698 		return ret;
1699 
1700 	ret = ov8865_write(sensor, OV8865_PLL_CTRLD_REG,
1701 			   OV8865_PLL_CTRLD_MUL_L(config->pll_mul));
1702 	if (ret)
1703 		return ret;
1704 
1705 	ret = ov8865_write(sensor, OV8865_PLL_CTRLF_REG,
1706 			   OV8865_PLL_CTRLF_SYS_PRE_DIV(config->sys_pre_div));
1707 	if (ret)
1708 		return ret;
1709 
1710 	return ov8865_write(sensor, OV8865_PLL_CTRLE_REG,
1711 			    OV8865_PLL_CTRLE_SYS_DIV(config->sys_div));
1712 }
1713 
ov8865_mode_sclk_configure(struct ov8865_sensor * sensor,const struct ov8865_mode * mode)1714 static int ov8865_mode_sclk_configure(struct ov8865_sensor *sensor,
1715 				      const struct ov8865_mode *mode)
1716 {
1717 	const struct ov8865_sclk_config *config = &ov8865_sclk_config_native;
1718 	int ret;
1719 
1720 	ret = ov8865_write(sensor, OV8865_CLK_SEL0_REG,
1721 			   OV8865_CLK_SEL0_PLL1_SYS_SEL(config->sys_sel));
1722 	if (ret)
1723 		return ret;
1724 
1725 	ret = ov8865_update_bits(sensor, OV8865_CLK_SEL1_REG,
1726 				 OV8865_CLK_SEL1_PLL_SCLK_SEL_MASK,
1727 				 OV8865_CLK_SEL1_PLL_SCLK_SEL(config->sclk_sel));
1728 	if (ret)
1729 		return ret;
1730 
1731 	return ov8865_write(sensor, OV8865_SCLK_CTRL_REG,
1732 			    OV8865_SCLK_CTRL_UNKNOWN |
1733 			    OV8865_SCLK_CTRL_SCLK_DIV(config->sclk_div) |
1734 			    OV8865_SCLK_CTRL_SCLK_PRE_DIV(config->sclk_pre_div));
1735 }
1736 
ov8865_mode_binning_configure(struct ov8865_sensor * sensor,const struct ov8865_mode * mode)1737 static int ov8865_mode_binning_configure(struct ov8865_sensor *sensor,
1738 					 const struct ov8865_mode *mode)
1739 {
1740 	unsigned int variopixel_hsub_coef, variopixel_vsub_coef;
1741 	u8 value;
1742 	int ret;
1743 
1744 	ret = ov8865_write(sensor, OV8865_FORMAT1_REG, 0);
1745 	if (ret)
1746 		return ret;
1747 
1748 	value = OV8865_FORMAT2_HSYNC_EN;
1749 
1750 	if (mode->binning_x)
1751 		value |= OV8865_FORMAT2_FST_HBIN_EN;
1752 
1753 	if (mode->binning_y)
1754 		value |= OV8865_FORMAT2_FST_VBIN_EN;
1755 
1756 	if (mode->sync_hbin)
1757 		value |= OV8865_FORMAT2_SYNC_HBIN_EN;
1758 
1759 	if (mode->horz_var2)
1760 		value |= OV8865_FORMAT2_ISP_HORZ_VAR2_EN;
1761 
1762 	ret = ov8865_write(sensor, OV8865_FORMAT2_REG, value);
1763 	if (ret)
1764 		return ret;
1765 
1766 	ret = ov8865_update_bits(sensor, OV8865_ISP_CTRL2_REG,
1767 				 OV8865_ISP_CTRL2_VARIOPIXEL_EN,
1768 				 mode->variopixel ?
1769 				 OV8865_ISP_CTRL2_VARIOPIXEL_EN : 0);
1770 	if (ret)
1771 		return ret;
1772 
1773 	if (mode->variopixel) {
1774 		/* VarioPixel coefs needs to be > 1. */
1775 		variopixel_hsub_coef = mode->variopixel_hsub_coef;
1776 		variopixel_vsub_coef = mode->variopixel_vsub_coef;
1777 	} else {
1778 		variopixel_hsub_coef = 1;
1779 		variopixel_vsub_coef = 1;
1780 	}
1781 
1782 	ret = ov8865_write(sensor, OV8865_VAP_CTRL1_REG,
1783 			   OV8865_VAP_CTRL1_HSUB_COEF(variopixel_hsub_coef) |
1784 			   OV8865_VAP_CTRL1_VSUB_COEF(variopixel_vsub_coef));
1785 	if (ret)
1786 		return ret;
1787 
1788 	ret = ov8865_write(sensor, OV8865_INC_X_ODD_REG,
1789 			   OV8865_INC_X_ODD(mode->inc_x_odd));
1790 	if (ret)
1791 		return ret;
1792 
1793 	ret = ov8865_write(sensor, OV8865_INC_X_EVEN_REG,
1794 			   OV8865_INC_X_EVEN(mode->inc_x_even));
1795 	if (ret)
1796 		return ret;
1797 
1798 	ret = ov8865_write(sensor, OV8865_INC_Y_ODD_REG,
1799 			   OV8865_INC_Y_ODD(mode->inc_y_odd));
1800 	if (ret)
1801 		return ret;
1802 
1803 	return ov8865_write(sensor, OV8865_INC_Y_EVEN_REG,
1804 			    OV8865_INC_Y_EVEN(mode->inc_y_even));
1805 }
1806 
ov8865_mode_black_level_configure(struct ov8865_sensor * sensor,const struct ov8865_mode * mode)1807 static int ov8865_mode_black_level_configure(struct ov8865_sensor *sensor,
1808 					     const struct ov8865_mode *mode)
1809 {
1810 	int ret;
1811 
1812 	/* Note that a zero value for blc_col_shift_mask is the default 256. */
1813 	ret = ov8865_write(sensor, OV8865_BLC_CTRL1_REG,
1814 			   mode->blc_col_shift_mask |
1815 			   OV8865_BLC_CTRL1_OFFSET_LIMIT_EN);
1816 	if (ret)
1817 		return ret;
1818 
1819 	/* BLC top zero line */
1820 
1821 	ret = ov8865_write(sensor, OV8865_BLC_TOP_ZLINE_START_REG,
1822 			   OV8865_BLC_TOP_ZLINE_START(mode->blc_top_zero_line_start));
1823 	if (ret)
1824 		return ret;
1825 
1826 	ret = ov8865_write(sensor, OV8865_BLC_TOP_ZLINE_NUM_REG,
1827 			   OV8865_BLC_TOP_ZLINE_NUM(mode->blc_top_zero_line_num));
1828 	if (ret)
1829 		return ret;
1830 
1831 	/* BLC top black line */
1832 
1833 	ret = ov8865_write(sensor, OV8865_BLC_TOP_BLKLINE_START_REG,
1834 			   OV8865_BLC_TOP_BLKLINE_START(mode->blc_top_black_line_start));
1835 	if (ret)
1836 		return ret;
1837 
1838 	ret = ov8865_write(sensor, OV8865_BLC_TOP_BLKLINE_NUM_REG,
1839 			   OV8865_BLC_TOP_BLKLINE_NUM(mode->blc_top_black_line_num));
1840 	if (ret)
1841 		return ret;
1842 
1843 	/* BLC bottom zero line */
1844 
1845 	ret = ov8865_write(sensor, OV8865_BLC_BOT_ZLINE_START_REG,
1846 			   OV8865_BLC_BOT_ZLINE_START(mode->blc_bottom_zero_line_start));
1847 	if (ret)
1848 		return ret;
1849 
1850 	ret = ov8865_write(sensor, OV8865_BLC_BOT_ZLINE_NUM_REG,
1851 			   OV8865_BLC_BOT_ZLINE_NUM(mode->blc_bottom_zero_line_num));
1852 	if (ret)
1853 		return ret;
1854 
1855 	/* BLC bottom black line */
1856 
1857 	ret = ov8865_write(sensor, OV8865_BLC_BOT_BLKLINE_START_REG,
1858 			   OV8865_BLC_BOT_BLKLINE_START(mode->blc_bottom_black_line_start));
1859 	if (ret)
1860 		return ret;
1861 
1862 	ret = ov8865_write(sensor, OV8865_BLC_BOT_BLKLINE_NUM_REG,
1863 			   OV8865_BLC_BOT_BLKLINE_NUM(mode->blc_bottom_black_line_num));
1864 	if (ret)
1865 		return ret;
1866 
1867 	/* BLC anchor */
1868 
1869 	ret = ov8865_write(sensor, OV8865_BLC_ANCHOR_LEFT_START_H_REG,
1870 			   OV8865_BLC_ANCHOR_LEFT_START_H(mode->blc_anchor_left_start));
1871 	if (ret)
1872 		return ret;
1873 
1874 	ret = ov8865_write(sensor, OV8865_BLC_ANCHOR_LEFT_START_L_REG,
1875 			   OV8865_BLC_ANCHOR_LEFT_START_L(mode->blc_anchor_left_start));
1876 	if (ret)
1877 		return ret;
1878 
1879 	ret = ov8865_write(sensor, OV8865_BLC_ANCHOR_LEFT_END_H_REG,
1880 			   OV8865_BLC_ANCHOR_LEFT_END_H(mode->blc_anchor_left_end));
1881 	if (ret)
1882 		return ret;
1883 
1884 	ret = ov8865_write(sensor, OV8865_BLC_ANCHOR_LEFT_END_L_REG,
1885 			   OV8865_BLC_ANCHOR_LEFT_END_L(mode->blc_anchor_left_end));
1886 	if (ret)
1887 		return ret;
1888 
1889 	ret = ov8865_write(sensor, OV8865_BLC_ANCHOR_RIGHT_START_H_REG,
1890 			   OV8865_BLC_ANCHOR_RIGHT_START_H(mode->blc_anchor_right_start));
1891 	if (ret)
1892 		return ret;
1893 
1894 	ret = ov8865_write(sensor, OV8865_BLC_ANCHOR_RIGHT_START_L_REG,
1895 			   OV8865_BLC_ANCHOR_RIGHT_START_L(mode->blc_anchor_right_start));
1896 	if (ret)
1897 		return ret;
1898 
1899 	ret = ov8865_write(sensor, OV8865_BLC_ANCHOR_RIGHT_END_H_REG,
1900 			   OV8865_BLC_ANCHOR_RIGHT_END_H(mode->blc_anchor_right_end));
1901 	if (ret)
1902 		return ret;
1903 
1904 	return ov8865_write(sensor, OV8865_BLC_ANCHOR_RIGHT_END_L_REG,
1905 			    OV8865_BLC_ANCHOR_RIGHT_END_L(mode->blc_anchor_right_end));
1906 }
1907 
ov8865_mode_configure(struct ov8865_sensor * sensor,const struct ov8865_mode * mode,u32 mbus_code)1908 static int ov8865_mode_configure(struct ov8865_sensor *sensor,
1909 				 const struct ov8865_mode *mode, u32 mbus_code)
1910 {
1911 	int ret;
1912 
1913 	/* Output Size X */
1914 
1915 	ret = ov8865_write(sensor, OV8865_OUTPUT_SIZE_X_H_REG,
1916 			   OV8865_OUTPUT_SIZE_X_H(mode->output_size_x));
1917 	if (ret)
1918 		return ret;
1919 
1920 	ret = ov8865_write(sensor, OV8865_OUTPUT_SIZE_X_L_REG,
1921 			   OV8865_OUTPUT_SIZE_X_L(mode->output_size_x));
1922 	if (ret)
1923 		return ret;
1924 
1925 	/* Horizontal Total Size */
1926 
1927 	ret = ov8865_write(sensor, OV8865_HTS_H_REG, OV8865_HTS_H(mode->hts));
1928 	if (ret)
1929 		return ret;
1930 
1931 	ret = ov8865_write(sensor, OV8865_HTS_L_REG, OV8865_HTS_L(mode->hts));
1932 	if (ret)
1933 		return ret;
1934 
1935 	/* Output Size Y */
1936 
1937 	ret = ov8865_write(sensor, OV8865_OUTPUT_SIZE_Y_H_REG,
1938 			   OV8865_OUTPUT_SIZE_Y_H(mode->output_size_y));
1939 	if (ret)
1940 		return ret;
1941 
1942 	ret = ov8865_write(sensor, OV8865_OUTPUT_SIZE_Y_L_REG,
1943 			   OV8865_OUTPUT_SIZE_Y_L(mode->output_size_y));
1944 	if (ret)
1945 		return ret;
1946 
1947 	/* Vertical Total Size */
1948 
1949 	ret = ov8865_write(sensor, OV8865_VTS_H_REG, OV8865_VTS_H(mode->vts));
1950 	if (ret)
1951 		return ret;
1952 
1953 	ret = ov8865_write(sensor, OV8865_VTS_L_REG, OV8865_VTS_L(mode->vts));
1954 	if (ret)
1955 		return ret;
1956 
1957 	if (mode->size_auto) {
1958 		/* Auto Size */
1959 
1960 		ret = ov8865_write(sensor, OV8865_AUTO_SIZE_CTRL_REG,
1961 				   OV8865_AUTO_SIZE_CTRL_OFFSET_Y_REG |
1962 				   OV8865_AUTO_SIZE_CTRL_OFFSET_X_REG |
1963 				   OV8865_AUTO_SIZE_CTRL_CROP_END_Y_REG |
1964 				   OV8865_AUTO_SIZE_CTRL_CROP_END_X_REG |
1965 				   OV8865_AUTO_SIZE_CTRL_CROP_START_Y_REG |
1966 				   OV8865_AUTO_SIZE_CTRL_CROP_START_X_REG);
1967 		if (ret)
1968 			return ret;
1969 
1970 		ret = ov8865_write(sensor, OV8865_AUTO_SIZE_BOUNDARIES_REG,
1971 				   OV8865_AUTO_SIZE_BOUNDARIES_Y(mode->size_auto_boundary_y) |
1972 				   OV8865_AUTO_SIZE_BOUNDARIES_X(mode->size_auto_boundary_x));
1973 		if (ret)
1974 			return ret;
1975 	} else {
1976 		/* Crop Start X */
1977 
1978 		ret = ov8865_write(sensor, OV8865_CROP_START_X_H_REG,
1979 				   OV8865_CROP_START_X_H(mode->crop_start_x));
1980 		if (ret)
1981 			return ret;
1982 
1983 		ret = ov8865_write(sensor, OV8865_CROP_START_X_L_REG,
1984 				   OV8865_CROP_START_X_L(mode->crop_start_x));
1985 		if (ret)
1986 			return ret;
1987 
1988 		/* Offset X */
1989 
1990 		ret = ov8865_write(sensor, OV8865_OFFSET_X_H_REG,
1991 				   OV8865_OFFSET_X_H(mode->offset_x));
1992 		if (ret)
1993 			return ret;
1994 
1995 		ret = ov8865_write(sensor, OV8865_OFFSET_X_L_REG,
1996 				   OV8865_OFFSET_X_L(mode->offset_x));
1997 		if (ret)
1998 			return ret;
1999 
2000 		/* Crop End X */
2001 
2002 		ret = ov8865_write(sensor, OV8865_CROP_END_X_H_REG,
2003 				   OV8865_CROP_END_X_H(mode->crop_end_x));
2004 		if (ret)
2005 			return ret;
2006 
2007 		ret = ov8865_write(sensor, OV8865_CROP_END_X_L_REG,
2008 				   OV8865_CROP_END_X_L(mode->crop_end_x));
2009 		if (ret)
2010 			return ret;
2011 
2012 		/* Crop Start Y */
2013 
2014 		ret = ov8865_write(sensor, OV8865_CROP_START_Y_H_REG,
2015 				   OV8865_CROP_START_Y_H(mode->crop_start_y));
2016 		if (ret)
2017 			return ret;
2018 
2019 		ret = ov8865_write(sensor, OV8865_CROP_START_Y_L_REG,
2020 				   OV8865_CROP_START_Y_L(mode->crop_start_y));
2021 		if (ret)
2022 			return ret;
2023 
2024 		/* Offset Y */
2025 
2026 		ret = ov8865_write(sensor, OV8865_OFFSET_Y_H_REG,
2027 				   OV8865_OFFSET_Y_H(mode->offset_y));
2028 		if (ret)
2029 			return ret;
2030 
2031 		ret = ov8865_write(sensor, OV8865_OFFSET_Y_L_REG,
2032 				   OV8865_OFFSET_Y_L(mode->offset_y));
2033 		if (ret)
2034 			return ret;
2035 
2036 		/* Crop End Y */
2037 
2038 		ret = ov8865_write(sensor, OV8865_CROP_END_Y_H_REG,
2039 				   OV8865_CROP_END_Y_H(mode->crop_end_y));
2040 		if (ret)
2041 			return ret;
2042 
2043 		ret = ov8865_write(sensor, OV8865_CROP_END_Y_L_REG,
2044 				   OV8865_CROP_END_Y_L(mode->crop_end_y));
2045 		if (ret)
2046 			return ret;
2047 	}
2048 
2049 	/* VFIFO */
2050 
2051 	ret = ov8865_write(sensor, OV8865_VFIFO_READ_START_H_REG,
2052 			   OV8865_VFIFO_READ_START_H(mode->vfifo_read_start));
2053 	if (ret)
2054 		return ret;
2055 
2056 	ret = ov8865_write(sensor, OV8865_VFIFO_READ_START_L_REG,
2057 			   OV8865_VFIFO_READ_START_L(mode->vfifo_read_start));
2058 	if (ret)
2059 		return ret;
2060 
2061 	ret = ov8865_write(sensor, OV8865_ABLC_NUM_REG,
2062 			   OV8865_ABLC_NUM(mode->ablc_num));
2063 	if (ret)
2064 		return ret;
2065 
2066 	ret = ov8865_write(sensor, OV8865_ZLINE_NUM_REG,
2067 			   OV8865_ZLINE_NUM(mode->zline_num));
2068 	if (ret)
2069 		return ret;
2070 
2071 	/* Binning */
2072 
2073 	ret = ov8865_mode_binning_configure(sensor, mode);
2074 	if (ret)
2075 		return ret;
2076 
2077 	/* Black Level */
2078 
2079 	ret = ov8865_mode_black_level_configure(sensor, mode);
2080 	if (ret)
2081 		return ret;
2082 
2083 	/* PLLs */
2084 
2085 	ret = ov8865_mode_pll1_configure(sensor, mode, mbus_code);
2086 	if (ret)
2087 		return ret;
2088 
2089 	ret = ov8865_mode_pll2_configure(sensor, mode);
2090 	if (ret)
2091 		return ret;
2092 
2093 	ret = ov8865_mode_sclk_configure(sensor, mode);
2094 	if (ret)
2095 		return ret;
2096 
2097 	/* Extra registers */
2098 
2099 	if (mode->register_values) {
2100 		ret = ov8865_write_sequence(sensor, mode->register_values,
2101 					    mode->register_values_count);
2102 		if (ret)
2103 			return ret;
2104 	}
2105 
2106 	return 0;
2107 }
2108 
ov8865_mode_mipi_clk_rate(struct ov8865_sensor * sensor,const struct ov8865_mode * mode)2109 static unsigned long ov8865_mode_mipi_clk_rate(struct ov8865_sensor *sensor,
2110 					       const struct ov8865_mode *mode)
2111 {
2112 	const struct ov8865_pll1_config *config;
2113 	unsigned long pll1_rate;
2114 
2115 	config = sensor->pll_configs->pll1_config;
2116 
2117 	pll1_rate = ov8865_mode_pll1_rate(sensor, mode);
2118 
2119 	return pll1_rate / config->m_div / 2;
2120 }
2121 
2122 /* Exposure */
2123 
ov8865_exposure_configure(struct ov8865_sensor * sensor,u32 exposure)2124 static int ov8865_exposure_configure(struct ov8865_sensor *sensor, u32 exposure)
2125 {
2126 	int ret;
2127 
2128 	/* The sensor stores exposure in units of 1/16th of a line */
2129 	exposure *= 16;
2130 
2131 	ret = ov8865_write(sensor, OV8865_EXPOSURE_CTRL_HH_REG,
2132 			   OV8865_EXPOSURE_CTRL_HH(exposure));
2133 	if (ret)
2134 		return ret;
2135 
2136 	ret = ov8865_write(sensor, OV8865_EXPOSURE_CTRL_H_REG,
2137 			   OV8865_EXPOSURE_CTRL_H(exposure));
2138 	if (ret)
2139 		return ret;
2140 
2141 	return ov8865_write(sensor, OV8865_EXPOSURE_CTRL_L_REG,
2142 			    OV8865_EXPOSURE_CTRL_L(exposure));
2143 }
2144 
2145 /* Gain */
2146 
ov8865_analog_gain_configure(struct ov8865_sensor * sensor,u32 gain)2147 static int ov8865_analog_gain_configure(struct ov8865_sensor *sensor, u32 gain)
2148 {
2149 	int ret;
2150 
2151 	ret = ov8865_write(sensor, OV8865_GAIN_CTRL_H_REG,
2152 			   OV8865_GAIN_CTRL_H(gain));
2153 	if (ret)
2154 		return ret;
2155 
2156 	return ov8865_write(sensor, OV8865_GAIN_CTRL_L_REG,
2157 			    OV8865_GAIN_CTRL_L(gain));
2158 }
2159 
2160 /* White Balance */
2161 
ov8865_red_balance_configure(struct ov8865_sensor * sensor,u32 red_balance)2162 static int ov8865_red_balance_configure(struct ov8865_sensor *sensor,
2163 					u32 red_balance)
2164 {
2165 	int ret;
2166 
2167 	ret = ov8865_write(sensor, OV8865_ISP_GAIN_RED_H_REG,
2168 			   OV8865_ISP_GAIN_RED_H(red_balance));
2169 	if (ret)
2170 		return ret;
2171 
2172 	return ov8865_write(sensor, OV8865_ISP_GAIN_RED_L_REG,
2173 			    OV8865_ISP_GAIN_RED_L(red_balance));
2174 }
2175 
ov8865_blue_balance_configure(struct ov8865_sensor * sensor,u32 blue_balance)2176 static int ov8865_blue_balance_configure(struct ov8865_sensor *sensor,
2177 					 u32 blue_balance)
2178 {
2179 	int ret;
2180 
2181 	ret = ov8865_write(sensor, OV8865_ISP_GAIN_BLUE_H_REG,
2182 			   OV8865_ISP_GAIN_BLUE_H(blue_balance));
2183 	if (ret)
2184 		return ret;
2185 
2186 	return ov8865_write(sensor, OV8865_ISP_GAIN_BLUE_L_REG,
2187 			    OV8865_ISP_GAIN_BLUE_L(blue_balance));
2188 }
2189 
2190 /* Flip */
2191 
ov8865_flip_vert_configure(struct ov8865_sensor * sensor,bool enable)2192 static int ov8865_flip_vert_configure(struct ov8865_sensor *sensor, bool enable)
2193 {
2194 	u8 bits = OV8865_FORMAT1_FLIP_VERT_ISP_EN |
2195 		  OV8865_FORMAT1_FLIP_VERT_SENSOR_EN;
2196 
2197 	return ov8865_update_bits(sensor, OV8865_FORMAT1_REG, bits,
2198 				  enable ? bits : 0);
2199 }
2200 
ov8865_flip_horz_configure(struct ov8865_sensor * sensor,bool enable)2201 static int ov8865_flip_horz_configure(struct ov8865_sensor *sensor, bool enable)
2202 {
2203 	u8 bits = OV8865_FORMAT2_FLIP_HORZ_ISP_EN |
2204 		  OV8865_FORMAT2_FLIP_HORZ_SENSOR_EN;
2205 
2206 	return ov8865_update_bits(sensor, OV8865_FORMAT2_REG, bits,
2207 				  enable ? bits : 0);
2208 }
2209 
2210 /* Test Pattern */
2211 
ov8865_test_pattern_configure(struct ov8865_sensor * sensor,unsigned int index)2212 static int ov8865_test_pattern_configure(struct ov8865_sensor *sensor,
2213 					 unsigned int index)
2214 {
2215 	if (index >= ARRAY_SIZE(ov8865_test_pattern_bits))
2216 		return -EINVAL;
2217 
2218 	return ov8865_write(sensor, OV8865_PRE_CTRL0_REG,
2219 			    ov8865_test_pattern_bits[index]);
2220 }
2221 
2222 /* Blanking */
2223 
ov8865_vts_configure(struct ov8865_sensor * sensor,u32 vblank)2224 static int ov8865_vts_configure(struct ov8865_sensor *sensor, u32 vblank)
2225 {
2226 	u16 vts = sensor->state.mode->output_size_y + vblank;
2227 	int ret;
2228 
2229 	ret = ov8865_write(sensor, OV8865_VTS_H_REG, OV8865_VTS_H(vts));
2230 	if (ret)
2231 		return ret;
2232 
2233 	return ov8865_write(sensor, OV8865_VTS_L_REG, OV8865_VTS_L(vts));
2234 }
2235 
2236 /* State */
2237 
ov8865_state_mipi_configure(struct ov8865_sensor * sensor,const struct ov8865_mode * mode,u32 mbus_code)2238 static int ov8865_state_mipi_configure(struct ov8865_sensor *sensor,
2239 				       const struct ov8865_mode *mode,
2240 				       u32 mbus_code)
2241 {
2242 	struct ov8865_ctrls *ctrls = &sensor->ctrls;
2243 	struct v4l2_mbus_config_mipi_csi2 *bus_mipi_csi2 =
2244 		&sensor->endpoint.bus.mipi_csi2;
2245 	unsigned long mipi_clk_rate;
2246 	unsigned int bits_per_sample;
2247 	unsigned int lanes_count;
2248 	unsigned int i, j;
2249 	s64 mipi_pixel_rate;
2250 
2251 	mipi_clk_rate = ov8865_mode_mipi_clk_rate(sensor, mode);
2252 	if (!mipi_clk_rate)
2253 		return -EINVAL;
2254 
2255 	for (i = 0; i < ARRAY_SIZE(ov8865_link_freq_menu); i++) {
2256 		s64 freq = ov8865_link_freq_menu[i];
2257 
2258 		if (freq == mipi_clk_rate)
2259 			break;
2260 	}
2261 
2262 	for (j = 0; j < sensor->endpoint.nr_of_link_frequencies; j++) {
2263 		u64 freq = sensor->endpoint.link_frequencies[j];
2264 
2265 		if (freq == mipi_clk_rate)
2266 			break;
2267 	}
2268 
2269 	if (i == ARRAY_SIZE(ov8865_link_freq_menu)) {
2270 		dev_err(sensor->dev,
2271 			"failed to find %lu clk rate in link freq\n",
2272 			mipi_clk_rate);
2273 	} else if (j == sensor->endpoint.nr_of_link_frequencies) {
2274 		dev_err(sensor->dev,
2275 			"failed to find %lu clk rate in endpoint link-frequencies\n",
2276 			mipi_clk_rate);
2277 	} else {
2278 		__v4l2_ctrl_s_ctrl(ctrls->link_freq, i);
2279 	}
2280 
2281 	switch (mbus_code) {
2282 	case MEDIA_BUS_FMT_SBGGR10_1X10:
2283 		bits_per_sample = 10;
2284 		break;
2285 	default:
2286 		return -EINVAL;
2287 	}
2288 
2289 	lanes_count = bus_mipi_csi2->num_data_lanes;
2290 	mipi_pixel_rate = mipi_clk_rate * 2 * lanes_count / bits_per_sample;
2291 
2292 	__v4l2_ctrl_s_ctrl_int64(ctrls->pixel_rate, mipi_pixel_rate);
2293 
2294 	return 0;
2295 }
2296 
ov8865_state_configure(struct ov8865_sensor * sensor,const struct ov8865_mode * mode,u32 mbus_code)2297 static int ov8865_state_configure(struct ov8865_sensor *sensor,
2298 				  const struct ov8865_mode *mode,
2299 				  u32 mbus_code)
2300 {
2301 	int ret;
2302 
2303 	if (sensor->state.streaming)
2304 		return -EBUSY;
2305 
2306 	ret = ov8865_state_mipi_configure(sensor, mode, mbus_code);
2307 	if (ret)
2308 		return ret;
2309 
2310 	sensor->state.mode = mode;
2311 	sensor->state.mbus_code = mbus_code;
2312 
2313 	return 0;
2314 }
2315 
ov8865_state_init(struct ov8865_sensor * sensor)2316 static int ov8865_state_init(struct ov8865_sensor *sensor)
2317 {
2318 	return ov8865_state_configure(sensor, &ov8865_modes[0],
2319 				      ov8865_mbus_codes[0]);
2320 }
2321 
2322 /* Sensor Base */
2323 
ov8865_sensor_init(struct ov8865_sensor * sensor)2324 static int ov8865_sensor_init(struct ov8865_sensor *sensor)
2325 {
2326 	int ret;
2327 
2328 	ret = ov8865_sw_reset(sensor);
2329 	if (ret) {
2330 		dev_err(sensor->dev, "failed to perform sw reset\n");
2331 		return ret;
2332 	}
2333 
2334 	ret = ov8865_sw_standby(sensor, 1);
2335 	if (ret) {
2336 		dev_err(sensor->dev, "failed to set sensor standby\n");
2337 		return ret;
2338 	}
2339 
2340 	ret = ov8865_chip_id_check(sensor);
2341 	if (ret) {
2342 		dev_err(sensor->dev, "failed to check sensor chip id\n");
2343 		return ret;
2344 	}
2345 
2346 	ret = ov8865_write_sequence(sensor, ov8865_init_sequence,
2347 				    ARRAY_SIZE(ov8865_init_sequence));
2348 	if (ret) {
2349 		dev_err(sensor->dev, "failed to write init sequence\n");
2350 		return ret;
2351 	}
2352 
2353 	ret = ov8865_charge_pump_configure(sensor);
2354 	if (ret) {
2355 		dev_err(sensor->dev, "failed to configure pad\n");
2356 		return ret;
2357 	}
2358 
2359 	ret = ov8865_mipi_configure(sensor);
2360 	if (ret) {
2361 		dev_err(sensor->dev, "failed to configure MIPI\n");
2362 		return ret;
2363 	}
2364 
2365 	ret = ov8865_isp_configure(sensor);
2366 	if (ret) {
2367 		dev_err(sensor->dev, "failed to configure ISP\n");
2368 		return ret;
2369 	}
2370 
2371 	ret = ov8865_black_level_configure(sensor);
2372 	if (ret) {
2373 		dev_err(sensor->dev, "failed to configure black level\n");
2374 		return ret;
2375 	}
2376 
2377 	/* Configure current mode. */
2378 	ret = ov8865_mode_configure(sensor, sensor->state.mode,
2379 				    sensor->state.mbus_code);
2380 	if (ret) {
2381 		dev_err(sensor->dev, "failed to configure mode\n");
2382 		return ret;
2383 	}
2384 
2385 	return 0;
2386 }
2387 
ov8865_sensor_power(struct ov8865_sensor * sensor,bool on)2388 static int ov8865_sensor_power(struct ov8865_sensor *sensor, bool on)
2389 {
2390 	/* Keep initialized to zero for disable label. */
2391 	int ret = 0;
2392 
2393 	if (on) {
2394 		gpiod_set_value_cansleep(sensor->reset, 1);
2395 		gpiod_set_value_cansleep(sensor->powerdown, 1);
2396 
2397 		ret = regulator_enable(sensor->dovdd);
2398 		if (ret) {
2399 			dev_err(sensor->dev,
2400 				"failed to enable DOVDD regulator\n");
2401 			return ret;
2402 		}
2403 
2404 		ret = regulator_enable(sensor->avdd);
2405 		if (ret) {
2406 			dev_err(sensor->dev,
2407 				"failed to enable AVDD regulator\n");
2408 			goto disable_dovdd;
2409 		}
2410 
2411 		ret = regulator_enable(sensor->dvdd);
2412 		if (ret) {
2413 			dev_err(sensor->dev,
2414 				"failed to enable DVDD regulator\n");
2415 			goto disable_avdd;
2416 		}
2417 
2418 		ret = clk_prepare_enable(sensor->extclk);
2419 		if (ret) {
2420 			dev_err(sensor->dev, "failed to enable EXTCLK clock\n");
2421 			goto disable_dvdd;
2422 		}
2423 
2424 		gpiod_set_value_cansleep(sensor->reset, 0);
2425 		gpiod_set_value_cansleep(sensor->powerdown, 0);
2426 
2427 		/* Time to enter streaming mode according to power timings. */
2428 		usleep_range(10000, 12000);
2429 	} else {
2430 		gpiod_set_value_cansleep(sensor->powerdown, 1);
2431 		gpiod_set_value_cansleep(sensor->reset, 1);
2432 
2433 		clk_disable_unprepare(sensor->extclk);
2434 
2435 disable_dvdd:
2436 		regulator_disable(sensor->dvdd);
2437 disable_avdd:
2438 		regulator_disable(sensor->avdd);
2439 disable_dovdd:
2440 		regulator_disable(sensor->dovdd);
2441 	}
2442 
2443 	return ret;
2444 }
2445 
2446 /* Controls */
2447 
ov8865_s_ctrl(struct v4l2_ctrl * ctrl)2448 static int ov8865_s_ctrl(struct v4l2_ctrl *ctrl)
2449 {
2450 	struct v4l2_subdev *subdev = ov8865_ctrl_subdev(ctrl);
2451 	struct ov8865_sensor *sensor = ov8865_subdev_sensor(subdev);
2452 	unsigned int index;
2453 	int ret;
2454 
2455 	/* If VBLANK is altered we need to update exposure to compensate */
2456 	if (ctrl->id == V4L2_CID_VBLANK) {
2457 		int exposure_max;
2458 
2459 		exposure_max = sensor->state.mode->output_size_y + ctrl->val -
2460 			       OV8865_INTEGRATION_TIME_MARGIN;
2461 		__v4l2_ctrl_modify_range(sensor->ctrls.exposure,
2462 					 sensor->ctrls.exposure->minimum,
2463 					 exposure_max,
2464 					 sensor->ctrls.exposure->step,
2465 					 min(sensor->ctrls.exposure->val,
2466 					     exposure_max));
2467 	}
2468 
2469 	/* Wait for the sensor to be on before setting controls. */
2470 	if (pm_runtime_suspended(sensor->dev))
2471 		return 0;
2472 
2473 	switch (ctrl->id) {
2474 	case V4L2_CID_EXPOSURE:
2475 		ret = ov8865_exposure_configure(sensor, ctrl->val);
2476 		if (ret)
2477 			return ret;
2478 		break;
2479 	case V4L2_CID_ANALOGUE_GAIN:
2480 		ret = ov8865_analog_gain_configure(sensor, ctrl->val);
2481 		if (ret)
2482 			return ret;
2483 		break;
2484 	case V4L2_CID_RED_BALANCE:
2485 		return ov8865_red_balance_configure(sensor, ctrl->val);
2486 	case V4L2_CID_BLUE_BALANCE:
2487 		return ov8865_blue_balance_configure(sensor, ctrl->val);
2488 	case V4L2_CID_HFLIP:
2489 		return ov8865_flip_horz_configure(sensor, !!ctrl->val);
2490 	case V4L2_CID_VFLIP:
2491 		return ov8865_flip_vert_configure(sensor, !!ctrl->val);
2492 	case V4L2_CID_TEST_PATTERN:
2493 		index = (unsigned int)ctrl->val;
2494 		return ov8865_test_pattern_configure(sensor, index);
2495 	case V4L2_CID_VBLANK:
2496 		return ov8865_vts_configure(sensor, ctrl->val);
2497 	default:
2498 		return -EINVAL;
2499 	}
2500 
2501 	return 0;
2502 }
2503 
2504 static const struct v4l2_ctrl_ops ov8865_ctrl_ops = {
2505 	.s_ctrl			= ov8865_s_ctrl,
2506 };
2507 
ov8865_ctrls_init(struct ov8865_sensor * sensor)2508 static int ov8865_ctrls_init(struct ov8865_sensor *sensor)
2509 {
2510 	struct ov8865_ctrls *ctrls = &sensor->ctrls;
2511 	struct v4l2_ctrl_handler *handler = &ctrls->handler;
2512 	const struct v4l2_ctrl_ops *ops = &ov8865_ctrl_ops;
2513 	const struct ov8865_mode *mode = &ov8865_modes[0];
2514 	struct v4l2_fwnode_device_properties props;
2515 	unsigned int vblank_max, vblank_def;
2516 	unsigned int hblank;
2517 	int ret;
2518 
2519 	v4l2_ctrl_handler_init(handler, 32);
2520 
2521 	/* Use our mutex for ctrl locking. */
2522 	handler->lock = &sensor->mutex;
2523 
2524 	/* Exposure */
2525 
2526 	ctrls->exposure = v4l2_ctrl_new_std(handler, ops, V4L2_CID_EXPOSURE, 2,
2527 					    65535, 1, 32);
2528 
2529 	/* Gain */
2530 
2531 	v4l2_ctrl_new_std(handler, ops, V4L2_CID_ANALOGUE_GAIN, 128, 2048, 128,
2532 			  128);
2533 
2534 	/* White Balance */
2535 
2536 	v4l2_ctrl_new_std(handler, ops, V4L2_CID_RED_BALANCE, 1, 32767, 1,
2537 			  1024);
2538 
2539 	v4l2_ctrl_new_std(handler, ops, V4L2_CID_BLUE_BALANCE, 1, 32767, 1,
2540 			  1024);
2541 
2542 	/* Flip */
2543 
2544 	v4l2_ctrl_new_std(handler, ops, V4L2_CID_HFLIP, 0, 1, 1, 0);
2545 	v4l2_ctrl_new_std(handler, ops, V4L2_CID_VFLIP, 0, 1, 1, 0);
2546 
2547 	/* Test Pattern */
2548 
2549 	v4l2_ctrl_new_std_menu_items(handler, ops, V4L2_CID_TEST_PATTERN,
2550 				     ARRAY_SIZE(ov8865_test_pattern_menu) - 1,
2551 				     0, 0, ov8865_test_pattern_menu);
2552 
2553 	/* Blanking */
2554 	hblank = mode->hts - mode->output_size_x;
2555 	ctrls->hblank = v4l2_ctrl_new_std(handler, ops, V4L2_CID_HBLANK, hblank,
2556 					  hblank, 1, hblank);
2557 
2558 	if (ctrls->hblank)
2559 		ctrls->hblank->flags |= V4L2_CTRL_FLAG_READ_ONLY;
2560 
2561 	vblank_max = OV8865_TIMING_MAX_VTS - mode->output_size_y;
2562 	vblank_def = mode->vts - mode->output_size_y;
2563 	ctrls->vblank = v4l2_ctrl_new_std(handler, ops, V4L2_CID_VBLANK,
2564 					  OV8865_TIMING_MIN_VTS, vblank_max, 1,
2565 					  vblank_def);
2566 
2567 	/* MIPI CSI-2 */
2568 
2569 	ctrls->link_freq =
2570 		v4l2_ctrl_new_int_menu(handler, NULL, V4L2_CID_LINK_FREQ,
2571 				       ARRAY_SIZE(ov8865_link_freq_menu) - 1,
2572 				       0, ov8865_link_freq_menu);
2573 
2574 	ctrls->pixel_rate =
2575 		v4l2_ctrl_new_std(handler, NULL, V4L2_CID_PIXEL_RATE, 1,
2576 				  INT_MAX, 1, 1);
2577 
2578 	/* set properties from fwnode (e.g. rotation, orientation) */
2579 	ret = v4l2_fwnode_device_parse(sensor->dev, &props);
2580 	if (ret)
2581 		goto error_ctrls;
2582 
2583 	ret = v4l2_ctrl_new_fwnode_properties(handler, ops, &props);
2584 	if (ret)
2585 		goto error_ctrls;
2586 
2587 	if (handler->error) {
2588 		ret = handler->error;
2589 		goto error_ctrls;
2590 	}
2591 
2592 	ctrls->link_freq->flags |= V4L2_CTRL_FLAG_READ_ONLY;
2593 	ctrls->pixel_rate->flags |= V4L2_CTRL_FLAG_READ_ONLY;
2594 
2595 	sensor->subdev.ctrl_handler = handler;
2596 
2597 	return 0;
2598 
2599 error_ctrls:
2600 	v4l2_ctrl_handler_free(handler);
2601 
2602 	return ret;
2603 }
2604 
2605 /* Subdev Video Operations */
2606 
ov8865_s_stream(struct v4l2_subdev * subdev,int enable)2607 static int ov8865_s_stream(struct v4l2_subdev *subdev, int enable)
2608 {
2609 	struct ov8865_sensor *sensor = ov8865_subdev_sensor(subdev);
2610 	struct ov8865_state *state = &sensor->state;
2611 	int ret = 0;
2612 
2613 	if (enable) {
2614 		ret = pm_runtime_resume_and_get(sensor->dev);
2615 		if (ret < 0)
2616 			return ret;
2617 	}
2618 
2619 	mutex_lock(&sensor->mutex);
2620 
2621 	/*
2622 	 * The sensor may have been kept powered by something else (e.g. the
2623 	 * VCM's runtime PM device link on IPU3 platforms), in which case
2624 	 * runtime resume did not run and the hardware may still be
2625 	 * configured for a previous mode. Always program the negotiated
2626 	 * configuration on stream start.
2627 	 */
2628 	if (enable) {
2629 		ret = ov8865_sensor_init(sensor);
2630 		if (!ret)
2631 			ret = __v4l2_ctrl_handler_setup(&sensor->ctrls.handler);
2632 	}
2633 
2634 	if (!ret)
2635 		ret = ov8865_sw_standby(sensor, !enable);
2636 
2637 	mutex_unlock(&sensor->mutex);
2638 
2639 	if (ret || !enable)
2640 		pm_runtime_put(sensor->dev);
2641 
2642 	if (!ret)
2643 		state->streaming = enable;
2644 
2645 	return ret;
2646 }
2647 
2648 static const struct v4l2_subdev_video_ops ov8865_subdev_video_ops = {
2649 	.s_stream		= ov8865_s_stream,
2650 };
2651 
2652 /* Subdev Pad Operations */
2653 
ov8865_enum_mbus_code(struct v4l2_subdev * subdev,struct v4l2_subdev_state * sd_state,struct v4l2_subdev_mbus_code_enum * code_enum)2654 static int ov8865_enum_mbus_code(struct v4l2_subdev *subdev,
2655 				 struct v4l2_subdev_state *sd_state,
2656 				 struct v4l2_subdev_mbus_code_enum *code_enum)
2657 {
2658 	if (code_enum->index >= ARRAY_SIZE(ov8865_mbus_codes))
2659 		return -EINVAL;
2660 
2661 	code_enum->code = ov8865_mbus_codes[code_enum->index];
2662 
2663 	return 0;
2664 }
2665 
ov8865_mbus_format_fill(struct v4l2_mbus_framefmt * mbus_format,u32 mbus_code,const struct ov8865_mode * mode)2666 static void ov8865_mbus_format_fill(struct v4l2_mbus_framefmt *mbus_format,
2667 				    u32 mbus_code,
2668 				    const struct ov8865_mode *mode)
2669 {
2670 	mbus_format->width = mode->output_size_x;
2671 	mbus_format->height = mode->output_size_y;
2672 	mbus_format->code = mbus_code;
2673 
2674 	mbus_format->field = V4L2_FIELD_NONE;
2675 	mbus_format->colorspace = V4L2_COLORSPACE_RAW;
2676 	mbus_format->ycbcr_enc =
2677 		V4L2_MAP_YCBCR_ENC_DEFAULT(mbus_format->colorspace);
2678 	mbus_format->quantization = V4L2_QUANTIZATION_FULL_RANGE;
2679 	mbus_format->xfer_func =
2680 		V4L2_MAP_XFER_FUNC_DEFAULT(mbus_format->colorspace);
2681 }
2682 
ov8865_get_fmt(struct v4l2_subdev * subdev,struct v4l2_subdev_state * sd_state,struct v4l2_subdev_format * format)2683 static int ov8865_get_fmt(struct v4l2_subdev *subdev,
2684 			  struct v4l2_subdev_state *sd_state,
2685 			  struct v4l2_subdev_format *format)
2686 {
2687 	struct ov8865_sensor *sensor = ov8865_subdev_sensor(subdev);
2688 	struct v4l2_mbus_framefmt *mbus_format = &format->format;
2689 
2690 	mutex_lock(&sensor->mutex);
2691 
2692 	if (format->which == V4L2_SUBDEV_FORMAT_TRY)
2693 		*mbus_format = *v4l2_subdev_state_get_format(sd_state,
2694 							     format->pad);
2695 	else
2696 		ov8865_mbus_format_fill(mbus_format, sensor->state.mbus_code,
2697 					sensor->state.mode);
2698 
2699 	mutex_unlock(&sensor->mutex);
2700 
2701 	return 0;
2702 }
2703 
ov8865_set_fmt(struct v4l2_subdev * subdev,struct v4l2_subdev_state * sd_state,struct v4l2_subdev_format * format)2704 static int ov8865_set_fmt(struct v4l2_subdev *subdev,
2705 			  struct v4l2_subdev_state *sd_state,
2706 			  struct v4l2_subdev_format *format)
2707 {
2708 	struct ov8865_sensor *sensor = ov8865_subdev_sensor(subdev);
2709 	struct v4l2_mbus_framefmt *mbus_format = &format->format;
2710 	const struct ov8865_mode *mode;
2711 	u32 mbus_code = 0;
2712 	unsigned int hblank;
2713 	unsigned int index;
2714 	int exposure_max;
2715 	int ret = 0;
2716 
2717 	mutex_lock(&sensor->mutex);
2718 
2719 	if (sensor->state.streaming) {
2720 		ret = -EBUSY;
2721 		goto complete;
2722 	}
2723 
2724 	/* Try to find requested mbus code. */
2725 	for (index = 0; index < ARRAY_SIZE(ov8865_mbus_codes); index++) {
2726 		if (ov8865_mbus_codes[index] == mbus_format->code) {
2727 			mbus_code = mbus_format->code;
2728 			break;
2729 		}
2730 	}
2731 
2732 	/* Fallback to default. */
2733 	if (!mbus_code)
2734 		mbus_code = ov8865_mbus_codes[0];
2735 
2736 	/* Find the mode with nearest dimensions. */
2737 	mode = v4l2_find_nearest_size(ov8865_modes, ARRAY_SIZE(ov8865_modes),
2738 				      output_size_x, output_size_y,
2739 				      mbus_format->width, mbus_format->height);
2740 	if (!mode) {
2741 		ret = -EINVAL;
2742 		goto complete;
2743 	}
2744 
2745 	ov8865_mbus_format_fill(mbus_format, mbus_code, mode);
2746 
2747 	if (format->which == V4L2_SUBDEV_FORMAT_TRY)
2748 		*v4l2_subdev_state_get_format(sd_state, format->pad) =
2749 			*mbus_format;
2750 	else if (sensor->state.mode != mode ||
2751 		 sensor->state.mbus_code != mbus_code)
2752 		ret = ov8865_state_configure(sensor, mode, mbus_code);
2753 
2754 	__v4l2_ctrl_modify_range(sensor->ctrls.vblank, OV8865_TIMING_MIN_VTS,
2755 				 OV8865_TIMING_MAX_VTS - mode->output_size_y,
2756 				 1, mode->vts - mode->output_size_y);
2757 
2758 	hblank = mode->hts - mode->output_size_x;
2759 	__v4l2_ctrl_modify_range(sensor->ctrls.hblank, hblank, hblank, 1,
2760 				 hblank);
2761 
2762 	exposure_max = mode->vts - OV8865_INTEGRATION_TIME_MARGIN;
2763 	__v4l2_ctrl_modify_range(sensor->ctrls.exposure,
2764 				 sensor->ctrls.exposure->minimum, exposure_max,
2765 				 sensor->ctrls.exposure->step,
2766 				 min(sensor->ctrls.exposure->val,
2767 				     exposure_max));
2768 
2769 complete:
2770 	mutex_unlock(&sensor->mutex);
2771 
2772 	return ret;
2773 }
2774 
ov8865_enum_frame_size(struct v4l2_subdev * subdev,struct v4l2_subdev_state * sd_state,struct v4l2_subdev_frame_size_enum * size_enum)2775 static int ov8865_enum_frame_size(struct v4l2_subdev *subdev,
2776 				  struct v4l2_subdev_state *sd_state,
2777 				  struct v4l2_subdev_frame_size_enum *size_enum)
2778 {
2779 	const struct ov8865_mode *mode;
2780 
2781 	if (size_enum->index >= ARRAY_SIZE(ov8865_modes))
2782 		return -EINVAL;
2783 
2784 	mode = &ov8865_modes[size_enum->index];
2785 
2786 	size_enum->min_width = size_enum->max_width = mode->output_size_x;
2787 	size_enum->min_height = size_enum->max_height = mode->output_size_y;
2788 
2789 	return 0;
2790 }
2791 
2792 static void
__ov8865_get_pad_crop(struct ov8865_sensor * sensor,struct v4l2_subdev_state * state,unsigned int pad,enum v4l2_subdev_format_whence which,struct v4l2_rect * r)2793 __ov8865_get_pad_crop(struct ov8865_sensor *sensor,
2794 		      struct v4l2_subdev_state *state, unsigned int pad,
2795 		      enum v4l2_subdev_format_whence which, struct v4l2_rect *r)
2796 {
2797 	const struct ov8865_mode *mode = sensor->state.mode;
2798 
2799 	switch (which) {
2800 	case V4L2_SUBDEV_FORMAT_TRY:
2801 		*r = *v4l2_subdev_state_get_crop(state, pad);
2802 		break;
2803 	case V4L2_SUBDEV_FORMAT_ACTIVE:
2804 		r->height = mode->output_size_y;
2805 		r->width = mode->output_size_x;
2806 		r->top = (OV8865_NATIVE_HEIGHT - mode->output_size_y) / 2;
2807 		r->left = (OV8865_NATIVE_WIDTH - mode->output_size_x) / 2;
2808 		break;
2809 	}
2810 }
2811 
ov8865_get_selection(struct v4l2_subdev * subdev,struct v4l2_subdev_state * state,struct v4l2_subdev_selection * sel)2812 static int ov8865_get_selection(struct v4l2_subdev *subdev,
2813 				struct v4l2_subdev_state *state,
2814 				struct v4l2_subdev_selection *sel)
2815 {
2816 	struct ov8865_sensor *sensor = ov8865_subdev_sensor(subdev);
2817 
2818 	switch (sel->target) {
2819 	case V4L2_SEL_TGT_CROP:
2820 		mutex_lock(&sensor->mutex);
2821 		__ov8865_get_pad_crop(sensor, state, sel->pad,
2822 				      sel->which, &sel->r);
2823 		mutex_unlock(&sensor->mutex);
2824 		break;
2825 	case V4L2_SEL_TGT_NATIVE_SIZE:
2826 		sel->r.top = 0;
2827 		sel->r.left = 0;
2828 		sel->r.width = OV8865_NATIVE_WIDTH;
2829 		sel->r.height = OV8865_NATIVE_HEIGHT;
2830 		break;
2831 	case V4L2_SEL_TGT_CROP_BOUNDS:
2832 	case V4L2_SEL_TGT_CROP_DEFAULT:
2833 		sel->r.top = OV8865_ACTIVE_START_TOP;
2834 		sel->r.left = OV8865_ACTIVE_START_LEFT;
2835 		sel->r.width = OV8865_ACTIVE_WIDTH;
2836 		sel->r.height = OV8865_ACTIVE_HEIGHT;
2837 		break;
2838 	default:
2839 		return -EINVAL;
2840 	}
2841 
2842 	return 0;
2843 }
2844 
ov8865_get_frame_interval(struct v4l2_subdev * subdev,struct v4l2_subdev_state * sd_state,struct v4l2_subdev_frame_interval * interval)2845 static int ov8865_get_frame_interval(struct v4l2_subdev *subdev,
2846 				     struct v4l2_subdev_state *sd_state,
2847 				     struct v4l2_subdev_frame_interval *interval)
2848 {
2849 	struct ov8865_sensor *sensor = ov8865_subdev_sensor(subdev);
2850 	const struct ov8865_mode *mode;
2851 	unsigned int framesize;
2852 	unsigned int fps;
2853 
2854 	/*
2855 	 * FIXME: Implement support for V4L2_SUBDEV_FORMAT_TRY, using the V4L2
2856 	 * subdev active state API.
2857 	 */
2858 	if (interval->which != V4L2_SUBDEV_FORMAT_ACTIVE)
2859 		return -EINVAL;
2860 
2861 	mutex_lock(&sensor->mutex);
2862 
2863 	mode = sensor->state.mode;
2864 	framesize = mode->hts * (mode->output_size_y +
2865 				 sensor->ctrls.vblank->val);
2866 	fps = DIV_ROUND_CLOSEST(sensor->ctrls.pixel_rate->val, framesize);
2867 
2868 	interval->interval.numerator = 1;
2869 	interval->interval.denominator = fps;
2870 
2871 	mutex_unlock(&sensor->mutex);
2872 
2873 	return 0;
2874 }
2875 
2876 static const struct v4l2_subdev_pad_ops ov8865_subdev_pad_ops = {
2877 	.enum_mbus_code		= ov8865_enum_mbus_code,
2878 	.get_fmt		= ov8865_get_fmt,
2879 	.set_fmt		= ov8865_set_fmt,
2880 	.enum_frame_size	= ov8865_enum_frame_size,
2881 	.get_selection		= ov8865_get_selection,
2882 	.set_selection		= ov8865_get_selection,
2883 	.get_frame_interval	= ov8865_get_frame_interval,
2884 	.set_frame_interval	= ov8865_get_frame_interval,
2885 };
2886 
2887 static const struct v4l2_subdev_ops ov8865_subdev_ops = {
2888 	.video		= &ov8865_subdev_video_ops,
2889 	.pad		= &ov8865_subdev_pad_ops,
2890 };
2891 
ov8865_suspend(struct device * dev)2892 static int ov8865_suspend(struct device *dev)
2893 {
2894 	struct i2c_client *client = to_i2c_client(dev);
2895 	struct v4l2_subdev *subdev = i2c_get_clientdata(client);
2896 	struct ov8865_sensor *sensor = ov8865_subdev_sensor(subdev);
2897 	struct ov8865_state *state = &sensor->state;
2898 	int ret = 0;
2899 
2900 	mutex_lock(&sensor->mutex);
2901 
2902 	if (state->streaming) {
2903 		ret = ov8865_sw_standby(sensor, true);
2904 		if (ret)
2905 			goto complete;
2906 	}
2907 
2908 	ret = ov8865_sensor_power(sensor, false);
2909 	if (ret)
2910 		ov8865_sw_standby(sensor, false);
2911 
2912 complete:
2913 	mutex_unlock(&sensor->mutex);
2914 
2915 	return ret;
2916 }
2917 
ov8865_resume(struct device * dev)2918 static int ov8865_resume(struct device *dev)
2919 {
2920 	struct i2c_client *client = to_i2c_client(dev);
2921 	struct v4l2_subdev *subdev = i2c_get_clientdata(client);
2922 	struct ov8865_sensor *sensor = ov8865_subdev_sensor(subdev);
2923 	struct ov8865_state *state = &sensor->state;
2924 	int ret = 0;
2925 
2926 	mutex_lock(&sensor->mutex);
2927 
2928 	ret = ov8865_sensor_power(sensor, true);
2929 	if (ret)
2930 		goto complete;
2931 
2932 	if (state->streaming) {
2933 		ret = ov8865_sensor_init(sensor);
2934 		if (ret)
2935 			goto error_power;
2936 
2937 		ret = __v4l2_ctrl_handler_setup(&sensor->ctrls.handler);
2938 		if (ret)
2939 			goto error_power;
2940 
2941 		ret = ov8865_sw_standby(sensor, false);
2942 		if (ret)
2943 			goto error_power;
2944 	}
2945 
2946 	goto complete;
2947 
2948 error_power:
2949 	ov8865_sensor_power(sensor, false);
2950 
2951 complete:
2952 	mutex_unlock(&sensor->mutex);
2953 
2954 	return ret;
2955 }
2956 
ov8865_probe(struct i2c_client * client)2957 static int ov8865_probe(struct i2c_client *client)
2958 {
2959 	struct device *dev = &client->dev;
2960 	struct fwnode_handle *handle;
2961 	struct ov8865_sensor *sensor;
2962 	struct v4l2_subdev *subdev;
2963 	struct media_pad *pad;
2964 	unsigned int i;
2965 	int ret;
2966 
2967 	sensor = devm_kzalloc(dev, sizeof(*sensor), GFP_KERNEL);
2968 	if (!sensor)
2969 		return -ENOMEM;
2970 
2971 	sensor->dev = dev;
2972 	sensor->i2c_client = client;
2973 
2974 	/* Regulators */
2975 
2976 	/* DVDD: digital core */
2977 	sensor->dvdd = devm_regulator_get(dev, "dvdd");
2978 	if (IS_ERR(sensor->dvdd))
2979 		return dev_err_probe(dev, PTR_ERR(sensor->dvdd),
2980 				     "cannot get DVDD regulator\n");
2981 
2982 	/* DOVDD: digital I/O */
2983 	sensor->dovdd = devm_regulator_get(dev, "dovdd");
2984 	if (IS_ERR(sensor->dovdd))
2985 		return dev_err_probe(dev, PTR_ERR(sensor->dovdd),
2986 				     "cannot get DOVDD regulator\n");
2987 
2988 	/* AVDD: analog */
2989 	sensor->avdd = devm_regulator_get(dev, "avdd");
2990 	if (IS_ERR(sensor->avdd))
2991 		return dev_err_probe(dev, PTR_ERR(sensor->avdd),
2992 				     "cannot get AVDD (analog) regulator\n");
2993 
2994 	/* Graph Endpoint */
2995 
2996 	handle = fwnode_graph_get_next_endpoint(dev_fwnode(dev), NULL);
2997 	if (!handle)
2998 		return dev_err_probe(dev, -EPROBE_DEFER,
2999 				     "waiting for fwnode graph endpoint\n");
3000 
3001 	sensor->endpoint.bus_type = V4L2_MBUS_CSI2_DPHY;
3002 
3003 	ret = v4l2_fwnode_endpoint_alloc_parse(handle, &sensor->endpoint);
3004 	fwnode_handle_put(handle);
3005 	if (ret) {
3006 		dev_err(dev, "failed to parse endpoint node\n");
3007 		return ret;
3008 	}
3009 
3010 	/* GPIOs */
3011 
3012 	sensor->powerdown = devm_gpiod_get_optional(dev, "powerdown",
3013 						    GPIOD_OUT_HIGH);
3014 	if (IS_ERR(sensor->powerdown)) {
3015 		ret = PTR_ERR(sensor->powerdown);
3016 		goto error_endpoint;
3017 	}
3018 
3019 	sensor->reset = devm_gpiod_get_optional(dev, "reset", GPIOD_OUT_HIGH);
3020 	if (IS_ERR(sensor->reset)) {
3021 		ret = PTR_ERR(sensor->reset);
3022 		goto error_endpoint;
3023 	}
3024 
3025 	/* External Clock */
3026 
3027 	sensor->extclk = devm_v4l2_sensor_clk_get(dev, NULL);
3028 	if (IS_ERR(sensor->extclk)) {
3029 		ret = dev_err_probe(dev, PTR_ERR(sensor->extclk),
3030 				    "failed to get external clock\n");
3031 		goto error_endpoint;
3032 	}
3033 
3034 	sensor->extclk_rate = clk_get_rate(sensor->extclk);
3035 
3036 	for (i = 0; i < ARRAY_SIZE(supported_extclk_rates); i++) {
3037 		if (sensor->extclk_rate == supported_extclk_rates[i])
3038 			break;
3039 	}
3040 
3041 	if (i == ARRAY_SIZE(supported_extclk_rates)) {
3042 		dev_err(dev, "clock rate %lu Hz is unsupported\n",
3043 			sensor->extclk_rate);
3044 		ret = -EINVAL;
3045 		goto error_endpoint;
3046 	}
3047 
3048 	sensor->pll_configs = ov8865_pll_configs[i];
3049 
3050 	/* Subdev, entity and pad */
3051 
3052 	subdev = &sensor->subdev;
3053 	v4l2_i2c_subdev_init(subdev, client, &ov8865_subdev_ops);
3054 
3055 	subdev->flags |= V4L2_SUBDEV_FL_HAS_DEVNODE;
3056 	subdev->entity.function = MEDIA_ENT_F_CAM_SENSOR;
3057 
3058 	pad = &sensor->pad;
3059 	pad->flags = MEDIA_PAD_FL_SOURCE;
3060 
3061 	ret = media_entity_pads_init(&subdev->entity, 1, pad);
3062 	if (ret)
3063 		goto error_entity;
3064 
3065 	/* Mutex */
3066 
3067 	mutex_init(&sensor->mutex);
3068 
3069 	/* Sensor */
3070 
3071 	ret = ov8865_ctrls_init(sensor);
3072 	if (ret)
3073 		goto error_mutex;
3074 
3075 	mutex_lock(&sensor->mutex);
3076 	ret = ov8865_state_init(sensor);
3077 	mutex_unlock(&sensor->mutex);
3078 	if (ret)
3079 		goto error_ctrls;
3080 
3081 	/* Runtime PM */
3082 
3083 	pm_runtime_set_suspended(sensor->dev);
3084 	pm_runtime_enable(sensor->dev);
3085 
3086 	/* V4L2 subdev register */
3087 
3088 	ret = v4l2_async_register_subdev_sensor(subdev);
3089 	if (ret)
3090 		goto error_pm;
3091 
3092 	return 0;
3093 
3094 error_pm:
3095 	pm_runtime_disable(sensor->dev);
3096 
3097 error_ctrls:
3098 	v4l2_ctrl_handler_free(&sensor->ctrls.handler);
3099 
3100 error_mutex:
3101 	mutex_destroy(&sensor->mutex);
3102 
3103 error_entity:
3104 	media_entity_cleanup(&sensor->subdev.entity);
3105 
3106 error_endpoint:
3107 	v4l2_fwnode_endpoint_free(&sensor->endpoint);
3108 
3109 	return ret;
3110 }
3111 
ov8865_remove(struct i2c_client * client)3112 static void ov8865_remove(struct i2c_client *client)
3113 {
3114 	struct v4l2_subdev *subdev = i2c_get_clientdata(client);
3115 	struct ov8865_sensor *sensor = ov8865_subdev_sensor(subdev);
3116 
3117 	v4l2_async_unregister_subdev(subdev);
3118 	pm_runtime_disable(sensor->dev);
3119 	v4l2_ctrl_handler_free(&sensor->ctrls.handler);
3120 	mutex_destroy(&sensor->mutex);
3121 	media_entity_cleanup(&subdev->entity);
3122 
3123 	v4l2_fwnode_endpoint_free(&sensor->endpoint);
3124 }
3125 
3126 static const struct dev_pm_ops ov8865_pm_ops = {
3127 	SET_RUNTIME_PM_OPS(ov8865_suspend, ov8865_resume, NULL)
3128 };
3129 
3130 static const struct acpi_device_id ov8865_acpi_match[] = {
3131 	{"INT347A"},
3132 	{ }
3133 };
3134 MODULE_DEVICE_TABLE(acpi, ov8865_acpi_match);
3135 
3136 static const struct of_device_id ov8865_of_match[] = {
3137 	{ .compatible = "ovti,ov8865" },
3138 	{ }
3139 };
3140 MODULE_DEVICE_TABLE(of, ov8865_of_match);
3141 
3142 static struct i2c_driver ov8865_driver = {
3143 	.driver = {
3144 		.name = "ov8865",
3145 		.of_match_table = ov8865_of_match,
3146 		.acpi_match_table = ov8865_acpi_match,
3147 		.pm = &ov8865_pm_ops,
3148 	},
3149 	.probe = ov8865_probe,
3150 	.remove = ov8865_remove,
3151 };
3152 
3153 module_i2c_driver(ov8865_driver);
3154 
3155 MODULE_AUTHOR("Paul Kocialkowski <paul.kocialkowski@bootlin.com>");
3156 MODULE_DESCRIPTION("V4L2 driver for the OmniVision OV8865 image sensor");
3157 MODULE_LICENSE("GPL v2");
3158