xref: /openbmc/linux/drivers/media/i2c/ov5670.c (revision d3402925)
1 // SPDX-License-Identifier: GPL-2.0
2 // Copyright (c) 2017 Intel Corporation.
3 
4 #include <asm/unaligned.h>
5 #include <linux/acpi.h>
6 #include <linux/clk.h>
7 #include <linux/delay.h>
8 #include <linux/gpio/consumer.h>
9 #include <linux/i2c.h>
10 #include <linux/mod_devicetable.h>
11 #include <linux/module.h>
12 #include <linux/of.h>
13 #include <linux/pm_runtime.h>
14 #include <linux/regulator/consumer.h>
15 #include <media/v4l2-ctrls.h>
16 #include <media/v4l2-device.h>
17 #include <media/v4l2-event.h>
18 #include <media/v4l2-fwnode.h>
19 
20 #define OV5670_XVCLK_FREQ		19200000
21 
22 #define OV5670_REG_CHIP_ID		0x300a
23 #define OV5670_CHIP_ID			0x005670
24 
25 #define OV5670_REG_MODE_SELECT		0x0100
26 #define OV5670_MODE_STANDBY		0x00
27 #define OV5670_MODE_STREAMING		0x01
28 
29 #define OV5670_REG_SOFTWARE_RST		0x0103
30 #define OV5670_SOFTWARE_RST		0x01
31 
32 /* vertical-timings from sensor */
33 #define OV5670_REG_VTS			0x380e
34 #define OV5670_VTS_30FPS		0x0808 /* default for 30 fps */
35 #define OV5670_VTS_MAX			0xffff
36 
37 /* horizontal-timings from sensor */
38 #define OV5670_REG_HTS			0x380c
39 
40 /*
41  * Pixels-per-line(PPL) = Time-per-line * pixel-rate
42  * In OV5670, Time-per-line = HTS/SCLK.
43  * HTS is fixed for all resolutions, not recommended to change.
44  */
45 #define OV5670_FIXED_PPL		2724	/* Pixels per line */
46 
47 /* Exposure controls from sensor */
48 #define OV5670_REG_EXPOSURE		0x3500
49 #define	OV5670_EXPOSURE_MIN		4
50 #define	OV5670_EXPOSURE_STEP		1
51 
52 /* Analog gain controls from sensor */
53 #define OV5670_REG_ANALOG_GAIN		0x3508
54 #define	ANALOG_GAIN_MIN			0
55 #define	ANALOG_GAIN_MAX			8191
56 #define	ANALOG_GAIN_STEP		1
57 #define	ANALOG_GAIN_DEFAULT		128
58 
59 /* Digital gain controls from sensor */
60 #define OV5670_REG_R_DGTL_GAIN		0x5032
61 #define OV5670_REG_G_DGTL_GAIN		0x5034
62 #define OV5670_REG_B_DGTL_GAIN		0x5036
63 #define OV5670_DGTL_GAIN_MIN		0
64 #define OV5670_DGTL_GAIN_MAX		4095
65 #define OV5670_DGTL_GAIN_STEP		1
66 #define OV5670_DGTL_GAIN_DEFAULT	1024
67 
68 /* Test Pattern Control */
69 #define OV5670_REG_TEST_PATTERN		0x4303
70 #define OV5670_TEST_PATTERN_ENABLE	BIT(3)
71 #define OV5670_REG_TEST_PATTERN_CTRL	0x4320
72 
73 #define OV5670_REG_VALUE_08BIT		1
74 #define OV5670_REG_VALUE_16BIT		2
75 #define OV5670_REG_VALUE_24BIT		3
76 
77 /* Pixel Array */
78 #define OV5670_NATIVE_WIDTH		2624
79 #define OV5670_NATIVE_HEIGHT		1980
80 
81 /* Initial number of frames to skip to avoid possible garbage */
82 #define OV5670_NUM_OF_SKIP_FRAMES	2
83 
84 struct ov5670_reg {
85 	u16 address;
86 	u8 val;
87 };
88 
89 struct ov5670_reg_list {
90 	u32 num_of_regs;
91 	const struct ov5670_reg *regs;
92 };
93 
94 struct ov5670_link_freq_config {
95 	u32 pixel_rate;
96 	const struct ov5670_reg_list reg_list;
97 };
98 
99 static const char * const ov5670_supply_names[] = {
100 	"avdd",		/* Analog power */
101 	"dvdd",		/* Digital power */
102 	"dovdd",	/* Digital output power */
103 };
104 
105 #define OV5670_NUM_SUPPLIES ARRAY_SIZE(ov5670_supply_names)
106 
107 struct ov5670_mode {
108 	/* Frame width in pixels */
109 	u32 width;
110 
111 	/* Frame height in pixels */
112 	u32 height;
113 
114 	/* Default vertical timining size */
115 	u32 vts_def;
116 
117 	/* Min vertical timining size */
118 	u32 vts_min;
119 
120 	/* Link frequency needed for this resolution */
121 	u32 link_freq_index;
122 
123 	/* Analog crop rectangle */
124 	const struct v4l2_rect *analog_crop;
125 
126 	/* Sensor register settings for this resolution */
127 	const struct ov5670_reg_list reg_list;
128 };
129 
130 /*
131  * All the modes supported by the driver are obtained by subsampling the
132  * full pixel array. The below values are reflected in registers from
133  * 0x3800-0x3807 in the modes register-value tables.
134  */
135 static const struct v4l2_rect ov5670_analog_crop = {
136 	.left	= 12,
137 	.top	= 4,
138 	.width	= 2600,
139 	.height	= 1952,
140 };
141 
142 static const struct ov5670_reg mipi_data_rate_840mbps[] = {
143 	{0x0300, 0x04},
144 	{0x0301, 0x00},
145 	{0x0302, 0x84},
146 	{0x0303, 0x00},
147 	{0x0304, 0x03},
148 	{0x0305, 0x01},
149 	{0x0306, 0x01},
150 	{0x030a, 0x00},
151 	{0x030b, 0x00},
152 	{0x030c, 0x00},
153 	{0x030d, 0x26},
154 	{0x030e, 0x00},
155 	{0x030f, 0x06},
156 	{0x0312, 0x01},
157 	{0x3031, 0x0a},
158 };
159 
160 static const struct ov5670_reg mode_2592x1944_regs[] = {
161 	{0x3000, 0x00},
162 	{0x3002, 0x21},
163 	{0x3005, 0xf0},
164 	{0x3007, 0x00},
165 	{0x3015, 0x0f},
166 	{0x3018, 0x32},
167 	{0x301a, 0xf0},
168 	{0x301b, 0xf0},
169 	{0x301c, 0xf0},
170 	{0x301d, 0xf0},
171 	{0x301e, 0xf0},
172 	{0x3030, 0x00},
173 	{0x3031, 0x0a},
174 	{0x303c, 0xff},
175 	{0x303e, 0xff},
176 	{0x3040, 0xf0},
177 	{0x3041, 0x00},
178 	{0x3042, 0xf0},
179 	{0x3106, 0x11},
180 	{0x3500, 0x00},
181 	{0x3501, 0x80},
182 	{0x3502, 0x00},
183 	{0x3503, 0x04},
184 	{0x3504, 0x03},
185 	{0x3505, 0x83},
186 	{0x3508, 0x04},
187 	{0x3509, 0x00},
188 	{0x350e, 0x04},
189 	{0x350f, 0x00},
190 	{0x3510, 0x00},
191 	{0x3511, 0x02},
192 	{0x3512, 0x00},
193 	{0x3601, 0xc8},
194 	{0x3610, 0x88},
195 	{0x3612, 0x48},
196 	{0x3614, 0x5b},
197 	{0x3615, 0x96},
198 	{0x3621, 0xd0},
199 	{0x3622, 0x00},
200 	{0x3623, 0x00},
201 	{0x3633, 0x13},
202 	{0x3634, 0x13},
203 	{0x3635, 0x13},
204 	{0x3636, 0x13},
205 	{0x3645, 0x13},
206 	{0x3646, 0x82},
207 	{0x3650, 0x00},
208 	{0x3652, 0xff},
209 	{0x3655, 0x20},
210 	{0x3656, 0xff},
211 	{0x365a, 0xff},
212 	{0x365e, 0xff},
213 	{0x3668, 0x00},
214 	{0x366a, 0x07},
215 	{0x366e, 0x10},
216 	{0x366d, 0x00},
217 	{0x366f, 0x80},
218 	{0x3700, 0x28},
219 	{0x3701, 0x10},
220 	{0x3702, 0x3a},
221 	{0x3703, 0x19},
222 	{0x3704, 0x10},
223 	{0x3705, 0x00},
224 	{0x3706, 0x66},
225 	{0x3707, 0x08},
226 	{0x3708, 0x34},
227 	{0x3709, 0x40},
228 	{0x370a, 0x01},
229 	{0x370b, 0x1b},
230 	{0x3714, 0x24},
231 	{0x371a, 0x3e},
232 	{0x3733, 0x00},
233 	{0x3734, 0x00},
234 	{0x373a, 0x05},
235 	{0x373b, 0x06},
236 	{0x373c, 0x0a},
237 	{0x373f, 0xa0},
238 	{0x3755, 0x00},
239 	{0x3758, 0x00},
240 	{0x375b, 0x0e},
241 	{0x3766, 0x5f},
242 	{0x3768, 0x00},
243 	{0x3769, 0x22},
244 	{0x3773, 0x08},
245 	{0x3774, 0x1f},
246 	{0x3776, 0x06},
247 	{0x37a0, 0x88},
248 	{0x37a1, 0x5c},
249 	{0x37a7, 0x88},
250 	{0x37a8, 0x70},
251 	{0x37aa, 0x88},
252 	{0x37ab, 0x48},
253 	{0x37b3, 0x66},
254 	{0x37c2, 0x04},
255 	{0x37c5, 0x00},
256 	{0x37c8, 0x00},
257 	{0x3800, 0x00},
258 	{0x3801, 0x0c},
259 	{0x3802, 0x00},
260 	{0x3803, 0x04},
261 	{0x3804, 0x0a},
262 	{0x3805, 0x33},
263 	{0x3806, 0x07},
264 	{0x3807, 0xa3},
265 	{0x3808, 0x0a},
266 	{0x3809, 0x20},
267 	{0x380a, 0x07},
268 	{0x380b, 0x98},
269 	{0x380c, 0x06},
270 	{0x380d, 0x90},
271 	{0x380e, 0x08},
272 	{0x380f, 0x08},
273 	{0x3811, 0x04},
274 	{0x3813, 0x02},
275 	{0x3814, 0x01},
276 	{0x3815, 0x01},
277 	{0x3816, 0x00},
278 	{0x3817, 0x00},
279 	{0x3818, 0x00},
280 	{0x3819, 0x00},
281 	{0x3820, 0x84},
282 	{0x3821, 0x46},
283 	{0x3822, 0x48},
284 	{0x3826, 0x00},
285 	{0x3827, 0x08},
286 	{0x382a, 0x01},
287 	{0x382b, 0x01},
288 	{0x3830, 0x08},
289 	{0x3836, 0x02},
290 	{0x3837, 0x00},
291 	{0x3838, 0x10},
292 	{0x3841, 0xff},
293 	{0x3846, 0x48},
294 	{0x3861, 0x00},
295 	{0x3862, 0x04},
296 	{0x3863, 0x06},
297 	{0x3a11, 0x01},
298 	{0x3a12, 0x78},
299 	{0x3b00, 0x00},
300 	{0x3b02, 0x00},
301 	{0x3b03, 0x00},
302 	{0x3b04, 0x00},
303 	{0x3b05, 0x00},
304 	{0x3c00, 0x89},
305 	{0x3c01, 0xab},
306 	{0x3c02, 0x01},
307 	{0x3c03, 0x00},
308 	{0x3c04, 0x00},
309 	{0x3c05, 0x03},
310 	{0x3c06, 0x00},
311 	{0x3c07, 0x05},
312 	{0x3c0c, 0x00},
313 	{0x3c0d, 0x00},
314 	{0x3c0e, 0x00},
315 	{0x3c0f, 0x00},
316 	{0x3c40, 0x00},
317 	{0x3c41, 0xa3},
318 	{0x3c43, 0x7d},
319 	{0x3c45, 0xd7},
320 	{0x3c47, 0xfc},
321 	{0x3c50, 0x05},
322 	{0x3c52, 0xaa},
323 	{0x3c54, 0x71},
324 	{0x3c56, 0x80},
325 	{0x3d85, 0x17},
326 	{0x3f03, 0x00},
327 	{0x3f0a, 0x00},
328 	{0x3f0b, 0x00},
329 	{0x4001, 0x60},
330 	{0x4009, 0x0d},
331 	{0x4020, 0x00},
332 	{0x4021, 0x00},
333 	{0x4022, 0x00},
334 	{0x4023, 0x00},
335 	{0x4024, 0x00},
336 	{0x4025, 0x00},
337 	{0x4026, 0x00},
338 	{0x4027, 0x00},
339 	{0x4028, 0x00},
340 	{0x4029, 0x00},
341 	{0x402a, 0x00},
342 	{0x402b, 0x00},
343 	{0x402c, 0x00},
344 	{0x402d, 0x00},
345 	{0x402e, 0x00},
346 	{0x402f, 0x00},
347 	{0x4040, 0x00},
348 	{0x4041, 0x03},
349 	{0x4042, 0x00},
350 	{0x4043, 0x7A},
351 	{0x4044, 0x00},
352 	{0x4045, 0x7A},
353 	{0x4046, 0x00},
354 	{0x4047, 0x7A},
355 	{0x4048, 0x00},
356 	{0x4049, 0x7A},
357 	{0x4307, 0x30},
358 	{0x4500, 0x58},
359 	{0x4501, 0x04},
360 	{0x4502, 0x40},
361 	{0x4503, 0x10},
362 	{0x4508, 0xaa},
363 	{0x4509, 0xaa},
364 	{0x450a, 0x00},
365 	{0x450b, 0x00},
366 	{0x4600, 0x01},
367 	{0x4601, 0x03},
368 	{0x4700, 0xa4},
369 	{0x4800, 0x4c},
370 	{0x4816, 0x53},
371 	{0x481f, 0x40},
372 	{0x4837, 0x13},
373 	{0x5000, 0x56},
374 	{0x5001, 0x01},
375 	{0x5002, 0x28},
376 	{0x5004, 0x0c},
377 	{0x5006, 0x0c},
378 	{0x5007, 0xe0},
379 	{0x5008, 0x01},
380 	{0x5009, 0xb0},
381 	{0x5901, 0x00},
382 	{0x5a01, 0x00},
383 	{0x5a03, 0x00},
384 	{0x5a04, 0x0c},
385 	{0x5a05, 0xe0},
386 	{0x5a06, 0x09},
387 	{0x5a07, 0xb0},
388 	{0x5a08, 0x06},
389 	{0x5e00, 0x00},
390 	{0x3734, 0x40},
391 	{0x5b00, 0x01},
392 	{0x5b01, 0x10},
393 	{0x5b02, 0x01},
394 	{0x5b03, 0xdb},
395 	{0x3d8c, 0x71},
396 	{0x3d8d, 0xea},
397 	{0x4017, 0x08},
398 	{0x3618, 0x2a},
399 	{0x5780, 0x3e},
400 	{0x5781, 0x0f},
401 	{0x5782, 0x44},
402 	{0x5783, 0x02},
403 	{0x5784, 0x01},
404 	{0x5785, 0x01},
405 	{0x5786, 0x00},
406 	{0x5787, 0x04},
407 	{0x5788, 0x02},
408 	{0x5789, 0x0f},
409 	{0x578a, 0xfd},
410 	{0x578b, 0xf5},
411 	{0x578c, 0xf5},
412 	{0x578d, 0x03},
413 	{0x578e, 0x08},
414 	{0x578f, 0x0c},
415 	{0x5790, 0x08},
416 	{0x5791, 0x06},
417 	{0x5792, 0x00},
418 	{0x5793, 0x52},
419 	{0x5794, 0xa3},
420 	{0x3503, 0x00},
421 	{0x5045, 0x05},
422 	{0x4003, 0x40},
423 	{0x5048, 0x40}
424 };
425 
426 static const struct ov5670_reg mode_1296x972_regs[] = {
427 	{0x3000, 0x00},
428 	{0x3002, 0x21},
429 	{0x3005, 0xf0},
430 	{0x3007, 0x00},
431 	{0x3015, 0x0f},
432 	{0x3018, 0x32},
433 	{0x301a, 0xf0},
434 	{0x301b, 0xf0},
435 	{0x301c, 0xf0},
436 	{0x301d, 0xf0},
437 	{0x301e, 0xf0},
438 	{0x3030, 0x00},
439 	{0x3031, 0x0a},
440 	{0x303c, 0xff},
441 	{0x303e, 0xff},
442 	{0x3040, 0xf0},
443 	{0x3041, 0x00},
444 	{0x3042, 0xf0},
445 	{0x3106, 0x11},
446 	{0x3500, 0x00},
447 	{0x3501, 0x80},
448 	{0x3502, 0x00},
449 	{0x3503, 0x04},
450 	{0x3504, 0x03},
451 	{0x3505, 0x83},
452 	{0x3508, 0x07},
453 	{0x3509, 0x80},
454 	{0x350e, 0x04},
455 	{0x350f, 0x00},
456 	{0x3510, 0x00},
457 	{0x3511, 0x02},
458 	{0x3512, 0x00},
459 	{0x3601, 0xc8},
460 	{0x3610, 0x88},
461 	{0x3612, 0x48},
462 	{0x3614, 0x5b},
463 	{0x3615, 0x96},
464 	{0x3621, 0xd0},
465 	{0x3622, 0x00},
466 	{0x3623, 0x00},
467 	{0x3633, 0x13},
468 	{0x3634, 0x13},
469 	{0x3635, 0x13},
470 	{0x3636, 0x13},
471 	{0x3645, 0x13},
472 	{0x3646, 0x82},
473 	{0x3650, 0x00},
474 	{0x3652, 0xff},
475 	{0x3655, 0x20},
476 	{0x3656, 0xff},
477 	{0x365a, 0xff},
478 	{0x365e, 0xff},
479 	{0x3668, 0x00},
480 	{0x366a, 0x07},
481 	{0x366e, 0x08},
482 	{0x366d, 0x00},
483 	{0x366f, 0x80},
484 	{0x3700, 0x28},
485 	{0x3701, 0x10},
486 	{0x3702, 0x3a},
487 	{0x3703, 0x19},
488 	{0x3704, 0x10},
489 	{0x3705, 0x00},
490 	{0x3706, 0x66},
491 	{0x3707, 0x08},
492 	{0x3708, 0x34},
493 	{0x3709, 0x40},
494 	{0x370a, 0x01},
495 	{0x370b, 0x1b},
496 	{0x3714, 0x24},
497 	{0x371a, 0x3e},
498 	{0x3733, 0x00},
499 	{0x3734, 0x00},
500 	{0x373a, 0x05},
501 	{0x373b, 0x06},
502 	{0x373c, 0x0a},
503 	{0x373f, 0xa0},
504 	{0x3755, 0x00},
505 	{0x3758, 0x00},
506 	{0x375b, 0x0e},
507 	{0x3766, 0x5f},
508 	{0x3768, 0x00},
509 	{0x3769, 0x22},
510 	{0x3773, 0x08},
511 	{0x3774, 0x1f},
512 	{0x3776, 0x06},
513 	{0x37a0, 0x88},
514 	{0x37a1, 0x5c},
515 	{0x37a7, 0x88},
516 	{0x37a8, 0x70},
517 	{0x37aa, 0x88},
518 	{0x37ab, 0x48},
519 	{0x37b3, 0x66},
520 	{0x37c2, 0x04},
521 	{0x37c5, 0x00},
522 	{0x37c8, 0x00},
523 	{0x3800, 0x00},
524 	{0x3801, 0x0c},
525 	{0x3802, 0x00},
526 	{0x3803, 0x04},
527 	{0x3804, 0x0a},
528 	{0x3805, 0x33},
529 	{0x3806, 0x07},
530 	{0x3807, 0xa3},
531 	{0x3808, 0x05},
532 	{0x3809, 0x10},
533 	{0x380a, 0x03},
534 	{0x380b, 0xcc},
535 	{0x380c, 0x06},
536 	{0x380d, 0x90},
537 	{0x380e, 0x08},
538 	{0x380f, 0x08},
539 	{0x3811, 0x04},
540 	{0x3813, 0x04},
541 	{0x3814, 0x03},
542 	{0x3815, 0x01},
543 	{0x3816, 0x00},
544 	{0x3817, 0x00},
545 	{0x3818, 0x00},
546 	{0x3819, 0x00},
547 	{0x3820, 0x94},
548 	{0x3821, 0x47},
549 	{0x3822, 0x48},
550 	{0x3826, 0x00},
551 	{0x3827, 0x08},
552 	{0x382a, 0x03},
553 	{0x382b, 0x01},
554 	{0x3830, 0x08},
555 	{0x3836, 0x02},
556 	{0x3837, 0x00},
557 	{0x3838, 0x10},
558 	{0x3841, 0xff},
559 	{0x3846, 0x48},
560 	{0x3861, 0x00},
561 	{0x3862, 0x04},
562 	{0x3863, 0x06},
563 	{0x3a11, 0x01},
564 	{0x3a12, 0x78},
565 	{0x3b00, 0x00},
566 	{0x3b02, 0x00},
567 	{0x3b03, 0x00},
568 	{0x3b04, 0x00},
569 	{0x3b05, 0x00},
570 	{0x3c00, 0x89},
571 	{0x3c01, 0xab},
572 	{0x3c02, 0x01},
573 	{0x3c03, 0x00},
574 	{0x3c04, 0x00},
575 	{0x3c05, 0x03},
576 	{0x3c06, 0x00},
577 	{0x3c07, 0x05},
578 	{0x3c0c, 0x00},
579 	{0x3c0d, 0x00},
580 	{0x3c0e, 0x00},
581 	{0x3c0f, 0x00},
582 	{0x3c40, 0x00},
583 	{0x3c41, 0xa3},
584 	{0x3c43, 0x7d},
585 	{0x3c45, 0xd7},
586 	{0x3c47, 0xfc},
587 	{0x3c50, 0x05},
588 	{0x3c52, 0xaa},
589 	{0x3c54, 0x71},
590 	{0x3c56, 0x80},
591 	{0x3d85, 0x17},
592 	{0x3f03, 0x00},
593 	{0x3f0a, 0x00},
594 	{0x3f0b, 0x00},
595 	{0x4001, 0x60},
596 	{0x4009, 0x05},
597 	{0x4020, 0x00},
598 	{0x4021, 0x00},
599 	{0x4022, 0x00},
600 	{0x4023, 0x00},
601 	{0x4024, 0x00},
602 	{0x4025, 0x00},
603 	{0x4026, 0x00},
604 	{0x4027, 0x00},
605 	{0x4028, 0x00},
606 	{0x4029, 0x00},
607 	{0x402a, 0x00},
608 	{0x402b, 0x00},
609 	{0x402c, 0x00},
610 	{0x402d, 0x00},
611 	{0x402e, 0x00},
612 	{0x402f, 0x00},
613 	{0x4040, 0x00},
614 	{0x4041, 0x03},
615 	{0x4042, 0x00},
616 	{0x4043, 0x7A},
617 	{0x4044, 0x00},
618 	{0x4045, 0x7A},
619 	{0x4046, 0x00},
620 	{0x4047, 0x7A},
621 	{0x4048, 0x00},
622 	{0x4049, 0x7A},
623 	{0x4307, 0x30},
624 	{0x4500, 0x58},
625 	{0x4501, 0x04},
626 	{0x4502, 0x48},
627 	{0x4503, 0x10},
628 	{0x4508, 0x55},
629 	{0x4509, 0x55},
630 	{0x450a, 0x00},
631 	{0x450b, 0x00},
632 	{0x4600, 0x00},
633 	{0x4601, 0x81},
634 	{0x4700, 0xa4},
635 	{0x4800, 0x4c},
636 	{0x4816, 0x53},
637 	{0x481f, 0x40},
638 	{0x4837, 0x13},
639 	{0x5000, 0x56},
640 	{0x5001, 0x01},
641 	{0x5002, 0x28},
642 	{0x5004, 0x0c},
643 	{0x5006, 0x0c},
644 	{0x5007, 0xe0},
645 	{0x5008, 0x01},
646 	{0x5009, 0xb0},
647 	{0x5901, 0x00},
648 	{0x5a01, 0x00},
649 	{0x5a03, 0x00},
650 	{0x5a04, 0x0c},
651 	{0x5a05, 0xe0},
652 	{0x5a06, 0x09},
653 	{0x5a07, 0xb0},
654 	{0x5a08, 0x06},
655 	{0x5e00, 0x00},
656 	{0x3734, 0x40},
657 	{0x5b00, 0x01},
658 	{0x5b01, 0x10},
659 	{0x5b02, 0x01},
660 	{0x5b03, 0xdb},
661 	{0x3d8c, 0x71},
662 	{0x3d8d, 0xea},
663 	{0x4017, 0x10},
664 	{0x3618, 0x2a},
665 	{0x5780, 0x3e},
666 	{0x5781, 0x0f},
667 	{0x5782, 0x44},
668 	{0x5783, 0x02},
669 	{0x5784, 0x01},
670 	{0x5785, 0x01},
671 	{0x5786, 0x00},
672 	{0x5787, 0x04},
673 	{0x5788, 0x02},
674 	{0x5789, 0x0f},
675 	{0x578a, 0xfd},
676 	{0x578b, 0xf5},
677 	{0x578c, 0xf5},
678 	{0x578d, 0x03},
679 	{0x578e, 0x08},
680 	{0x578f, 0x0c},
681 	{0x5790, 0x08},
682 	{0x5791, 0x04},
683 	{0x5792, 0x00},
684 	{0x5793, 0x52},
685 	{0x5794, 0xa3},
686 	{0x3503, 0x00},
687 	{0x5045, 0x05},
688 	{0x4003, 0x40},
689 	{0x5048, 0x40}
690 };
691 
692 static const struct ov5670_reg mode_648x486_regs[] = {
693 	{0x3000, 0x00},
694 	{0x3002, 0x21},
695 	{0x3005, 0xf0},
696 	{0x3007, 0x00},
697 	{0x3015, 0x0f},
698 	{0x3018, 0x32},
699 	{0x301a, 0xf0},
700 	{0x301b, 0xf0},
701 	{0x301c, 0xf0},
702 	{0x301d, 0xf0},
703 	{0x301e, 0xf0},
704 	{0x3030, 0x00},
705 	{0x3031, 0x0a},
706 	{0x303c, 0xff},
707 	{0x303e, 0xff},
708 	{0x3040, 0xf0},
709 	{0x3041, 0x00},
710 	{0x3042, 0xf0},
711 	{0x3106, 0x11},
712 	{0x3500, 0x00},
713 	{0x3501, 0x80},
714 	{0x3502, 0x00},
715 	{0x3503, 0x04},
716 	{0x3504, 0x03},
717 	{0x3505, 0x83},
718 	{0x3508, 0x04},
719 	{0x3509, 0x00},
720 	{0x350e, 0x04},
721 	{0x350f, 0x00},
722 	{0x3510, 0x00},
723 	{0x3511, 0x02},
724 	{0x3512, 0x00},
725 	{0x3601, 0xc8},
726 	{0x3610, 0x88},
727 	{0x3612, 0x48},
728 	{0x3614, 0x5b},
729 	{0x3615, 0x96},
730 	{0x3621, 0xd0},
731 	{0x3622, 0x00},
732 	{0x3623, 0x04},
733 	{0x3633, 0x13},
734 	{0x3634, 0x13},
735 	{0x3635, 0x13},
736 	{0x3636, 0x13},
737 	{0x3645, 0x13},
738 	{0x3646, 0x82},
739 	{0x3650, 0x00},
740 	{0x3652, 0xff},
741 	{0x3655, 0x20},
742 	{0x3656, 0xff},
743 	{0x365a, 0xff},
744 	{0x365e, 0xff},
745 	{0x3668, 0x00},
746 	{0x366a, 0x07},
747 	{0x366e, 0x08},
748 	{0x366d, 0x00},
749 	{0x366f, 0x80},
750 	{0x3700, 0x28},
751 	{0x3701, 0x10},
752 	{0x3702, 0x3a},
753 	{0x3703, 0x19},
754 	{0x3704, 0x10},
755 	{0x3705, 0x00},
756 	{0x3706, 0x66},
757 	{0x3707, 0x08},
758 	{0x3708, 0x34},
759 	{0x3709, 0x40},
760 	{0x370a, 0x01},
761 	{0x370b, 0x1b},
762 	{0x3714, 0x24},
763 	{0x371a, 0x3e},
764 	{0x3733, 0x00},
765 	{0x3734, 0x00},
766 	{0x373a, 0x05},
767 	{0x373b, 0x06},
768 	{0x373c, 0x0a},
769 	{0x373f, 0xa0},
770 	{0x3755, 0x00},
771 	{0x3758, 0x00},
772 	{0x375b, 0x0e},
773 	{0x3766, 0x5f},
774 	{0x3768, 0x00},
775 	{0x3769, 0x22},
776 	{0x3773, 0x08},
777 	{0x3774, 0x1f},
778 	{0x3776, 0x06},
779 	{0x37a0, 0x88},
780 	{0x37a1, 0x5c},
781 	{0x37a7, 0x88},
782 	{0x37a8, 0x70},
783 	{0x37aa, 0x88},
784 	{0x37ab, 0x48},
785 	{0x37b3, 0x66},
786 	{0x37c2, 0x04},
787 	{0x37c5, 0x00},
788 	{0x37c8, 0x00},
789 	{0x3800, 0x00},
790 	{0x3801, 0x0c},
791 	{0x3802, 0x00},
792 	{0x3803, 0x04},
793 	{0x3804, 0x0a},
794 	{0x3805, 0x33},
795 	{0x3806, 0x07},
796 	{0x3807, 0xa3},
797 	{0x3808, 0x02},
798 	{0x3809, 0x88},
799 	{0x380a, 0x01},
800 	{0x380b, 0xe6},
801 	{0x380c, 0x06},
802 	{0x380d, 0x90},
803 	{0x380e, 0x08},
804 	{0x380f, 0x08},
805 	{0x3811, 0x04},
806 	{0x3813, 0x02},
807 	{0x3814, 0x07},
808 	{0x3815, 0x01},
809 	{0x3816, 0x00},
810 	{0x3817, 0x00},
811 	{0x3818, 0x00},
812 	{0x3819, 0x00},
813 	{0x3820, 0x94},
814 	{0x3821, 0xc6},
815 	{0x3822, 0x48},
816 	{0x3826, 0x00},
817 	{0x3827, 0x08},
818 	{0x382a, 0x07},
819 	{0x382b, 0x01},
820 	{0x3830, 0x08},
821 	{0x3836, 0x02},
822 	{0x3837, 0x00},
823 	{0x3838, 0x10},
824 	{0x3841, 0xff},
825 	{0x3846, 0x48},
826 	{0x3861, 0x00},
827 	{0x3862, 0x04},
828 	{0x3863, 0x06},
829 	{0x3a11, 0x01},
830 	{0x3a12, 0x78},
831 	{0x3b00, 0x00},
832 	{0x3b02, 0x00},
833 	{0x3b03, 0x00},
834 	{0x3b04, 0x00},
835 	{0x3b05, 0x00},
836 	{0x3c00, 0x89},
837 	{0x3c01, 0xab},
838 	{0x3c02, 0x01},
839 	{0x3c03, 0x00},
840 	{0x3c04, 0x00},
841 	{0x3c05, 0x03},
842 	{0x3c06, 0x00},
843 	{0x3c07, 0x05},
844 	{0x3c0c, 0x00},
845 	{0x3c0d, 0x00},
846 	{0x3c0e, 0x00},
847 	{0x3c0f, 0x00},
848 	{0x3c40, 0x00},
849 	{0x3c41, 0xa3},
850 	{0x3c43, 0x7d},
851 	{0x3c45, 0xd7},
852 	{0x3c47, 0xfc},
853 	{0x3c50, 0x05},
854 	{0x3c52, 0xaa},
855 	{0x3c54, 0x71},
856 	{0x3c56, 0x80},
857 	{0x3d85, 0x17},
858 	{0x3f03, 0x00},
859 	{0x3f0a, 0x00},
860 	{0x3f0b, 0x00},
861 	{0x4001, 0x60},
862 	{0x4009, 0x05},
863 	{0x4020, 0x00},
864 	{0x4021, 0x00},
865 	{0x4022, 0x00},
866 	{0x4023, 0x00},
867 	{0x4024, 0x00},
868 	{0x4025, 0x00},
869 	{0x4026, 0x00},
870 	{0x4027, 0x00},
871 	{0x4028, 0x00},
872 	{0x4029, 0x00},
873 	{0x402a, 0x00},
874 	{0x402b, 0x00},
875 	{0x402c, 0x00},
876 	{0x402d, 0x00},
877 	{0x402e, 0x00},
878 	{0x402f, 0x00},
879 	{0x4040, 0x00},
880 	{0x4041, 0x03},
881 	{0x4042, 0x00},
882 	{0x4043, 0x7A},
883 	{0x4044, 0x00},
884 	{0x4045, 0x7A},
885 	{0x4046, 0x00},
886 	{0x4047, 0x7A},
887 	{0x4048, 0x00},
888 	{0x4049, 0x7A},
889 	{0x4307, 0x30},
890 	{0x4500, 0x58},
891 	{0x4501, 0x04},
892 	{0x4502, 0x40},
893 	{0x4503, 0x10},
894 	{0x4508, 0x55},
895 	{0x4509, 0x55},
896 	{0x450a, 0x02},
897 	{0x450b, 0x00},
898 	{0x4600, 0x00},
899 	{0x4601, 0x40},
900 	{0x4700, 0xa4},
901 	{0x4800, 0x4c},
902 	{0x4816, 0x53},
903 	{0x481f, 0x40},
904 	{0x4837, 0x13},
905 	{0x5000, 0x56},
906 	{0x5001, 0x01},
907 	{0x5002, 0x28},
908 	{0x5004, 0x0c},
909 	{0x5006, 0x0c},
910 	{0x5007, 0xe0},
911 	{0x5008, 0x01},
912 	{0x5009, 0xb0},
913 	{0x5901, 0x00},
914 	{0x5a01, 0x00},
915 	{0x5a03, 0x00},
916 	{0x5a04, 0x0c},
917 	{0x5a05, 0xe0},
918 	{0x5a06, 0x09},
919 	{0x5a07, 0xb0},
920 	{0x5a08, 0x06},
921 	{0x5e00, 0x00},
922 	{0x3734, 0x40},
923 	{0x5b00, 0x01},
924 	{0x5b01, 0x10},
925 	{0x5b02, 0x01},
926 	{0x5b03, 0xdb},
927 	{0x3d8c, 0x71},
928 	{0x3d8d, 0xea},
929 	{0x4017, 0x10},
930 	{0x3618, 0x2a},
931 	{0x5780, 0x3e},
932 	{0x5781, 0x0f},
933 	{0x5782, 0x44},
934 	{0x5783, 0x02},
935 	{0x5784, 0x01},
936 	{0x5785, 0x01},
937 	{0x5786, 0x00},
938 	{0x5787, 0x04},
939 	{0x5788, 0x02},
940 	{0x5789, 0x0f},
941 	{0x578a, 0xfd},
942 	{0x578b, 0xf5},
943 	{0x578c, 0xf5},
944 	{0x578d, 0x03},
945 	{0x578e, 0x08},
946 	{0x578f, 0x0c},
947 	{0x5790, 0x08},
948 	{0x5791, 0x06},
949 	{0x5792, 0x00},
950 	{0x5793, 0x52},
951 	{0x5794, 0xa3},
952 	{0x3503, 0x00},
953 	{0x5045, 0x05},
954 	{0x4003, 0x40},
955 	{0x5048, 0x40}
956 };
957 
958 static const struct ov5670_reg mode_2560x1440_regs[] = {
959 	{0x3000, 0x00},
960 	{0x3002, 0x21},
961 	{0x3005, 0xf0},
962 	{0x3007, 0x00},
963 	{0x3015, 0x0f},
964 	{0x3018, 0x32},
965 	{0x301a, 0xf0},
966 	{0x301b, 0xf0},
967 	{0x301c, 0xf0},
968 	{0x301d, 0xf0},
969 	{0x301e, 0xf0},
970 	{0x3030, 0x00},
971 	{0x3031, 0x0a},
972 	{0x303c, 0xff},
973 	{0x303e, 0xff},
974 	{0x3040, 0xf0},
975 	{0x3041, 0x00},
976 	{0x3042, 0xf0},
977 	{0x3106, 0x11},
978 	{0x3500, 0x00},
979 	{0x3501, 0x80},
980 	{0x3502, 0x00},
981 	{0x3503, 0x04},
982 	{0x3504, 0x03},
983 	{0x3505, 0x83},
984 	{0x3508, 0x04},
985 	{0x3509, 0x00},
986 	{0x350e, 0x04},
987 	{0x350f, 0x00},
988 	{0x3510, 0x00},
989 	{0x3511, 0x02},
990 	{0x3512, 0x00},
991 	{0x3601, 0xc8},
992 	{0x3610, 0x88},
993 	{0x3612, 0x48},
994 	{0x3614, 0x5b},
995 	{0x3615, 0x96},
996 	{0x3621, 0xd0},
997 	{0x3622, 0x00},
998 	{0x3623, 0x00},
999 	{0x3633, 0x13},
1000 	{0x3634, 0x13},
1001 	{0x3635, 0x13},
1002 	{0x3636, 0x13},
1003 	{0x3645, 0x13},
1004 	{0x3646, 0x82},
1005 	{0x3650, 0x00},
1006 	{0x3652, 0xff},
1007 	{0x3655, 0x20},
1008 	{0x3656, 0xff},
1009 	{0x365a, 0xff},
1010 	{0x365e, 0xff},
1011 	{0x3668, 0x00},
1012 	{0x366a, 0x07},
1013 	{0x366e, 0x10},
1014 	{0x366d, 0x00},
1015 	{0x366f, 0x80},
1016 	{0x3700, 0x28},
1017 	{0x3701, 0x10},
1018 	{0x3702, 0x3a},
1019 	{0x3703, 0x19},
1020 	{0x3704, 0x10},
1021 	{0x3705, 0x00},
1022 	{0x3706, 0x66},
1023 	{0x3707, 0x08},
1024 	{0x3708, 0x34},
1025 	{0x3709, 0x40},
1026 	{0x370a, 0x01},
1027 	{0x370b, 0x1b},
1028 	{0x3714, 0x24},
1029 	{0x371a, 0x3e},
1030 	{0x3733, 0x00},
1031 	{0x3734, 0x00},
1032 	{0x373a, 0x05},
1033 	{0x373b, 0x06},
1034 	{0x373c, 0x0a},
1035 	{0x373f, 0xa0},
1036 	{0x3755, 0x00},
1037 	{0x3758, 0x00},
1038 	{0x375b, 0x0e},
1039 	{0x3766, 0x5f},
1040 	{0x3768, 0x00},
1041 	{0x3769, 0x22},
1042 	{0x3773, 0x08},
1043 	{0x3774, 0x1f},
1044 	{0x3776, 0x06},
1045 	{0x37a0, 0x88},
1046 	{0x37a1, 0x5c},
1047 	{0x37a7, 0x88},
1048 	{0x37a8, 0x70},
1049 	{0x37aa, 0x88},
1050 	{0x37ab, 0x48},
1051 	{0x37b3, 0x66},
1052 	{0x37c2, 0x04},
1053 	{0x37c5, 0x00},
1054 	{0x37c8, 0x00},
1055 	{0x3800, 0x00},
1056 	{0x3801, 0x0c},
1057 	{0x3802, 0x00},
1058 	{0x3803, 0x04},
1059 	{0x3804, 0x0a},
1060 	{0x3805, 0x33},
1061 	{0x3806, 0x07},
1062 	{0x3807, 0xa3},
1063 	{0x3808, 0x0a},
1064 	{0x3809, 0x00},
1065 	{0x380a, 0x05},
1066 	{0x380b, 0xa0},
1067 	{0x380c, 0x06},
1068 	{0x380d, 0x90},
1069 	{0x380e, 0x08},
1070 	{0x380f, 0x08},
1071 	{0x3811, 0x04},
1072 	{0x3813, 0x02},
1073 	{0x3814, 0x01},
1074 	{0x3815, 0x01},
1075 	{0x3816, 0x00},
1076 	{0x3817, 0x00},
1077 	{0x3818, 0x00},
1078 	{0x3819, 0x00},
1079 	{0x3820, 0x84},
1080 	{0x3821, 0x46},
1081 	{0x3822, 0x48},
1082 	{0x3826, 0x00},
1083 	{0x3827, 0x08},
1084 	{0x382a, 0x01},
1085 	{0x382b, 0x01},
1086 	{0x3830, 0x08},
1087 	{0x3836, 0x02},
1088 	{0x3837, 0x00},
1089 	{0x3838, 0x10},
1090 	{0x3841, 0xff},
1091 	{0x3846, 0x48},
1092 	{0x3861, 0x00},
1093 	{0x3862, 0x04},
1094 	{0x3863, 0x06},
1095 	{0x3a11, 0x01},
1096 	{0x3a12, 0x78},
1097 	{0x3b00, 0x00},
1098 	{0x3b02, 0x00},
1099 	{0x3b03, 0x00},
1100 	{0x3b04, 0x00},
1101 	{0x3b05, 0x00},
1102 	{0x3c00, 0x89},
1103 	{0x3c01, 0xab},
1104 	{0x3c02, 0x01},
1105 	{0x3c03, 0x00},
1106 	{0x3c04, 0x00},
1107 	{0x3c05, 0x03},
1108 	{0x3c06, 0x00},
1109 	{0x3c07, 0x05},
1110 	{0x3c0c, 0x00},
1111 	{0x3c0d, 0x00},
1112 	{0x3c0e, 0x00},
1113 	{0x3c0f, 0x00},
1114 	{0x3c40, 0x00},
1115 	{0x3c41, 0xa3},
1116 	{0x3c43, 0x7d},
1117 	{0x3c45, 0xd7},
1118 	{0x3c47, 0xfc},
1119 	{0x3c50, 0x05},
1120 	{0x3c52, 0xaa},
1121 	{0x3c54, 0x71},
1122 	{0x3c56, 0x80},
1123 	{0x3d85, 0x17},
1124 	{0x3f03, 0x00},
1125 	{0x3f0a, 0x00},
1126 	{0x3f0b, 0x00},
1127 	{0x4001, 0x60},
1128 	{0x4009, 0x0d},
1129 	{0x4020, 0x00},
1130 	{0x4021, 0x00},
1131 	{0x4022, 0x00},
1132 	{0x4023, 0x00},
1133 	{0x4024, 0x00},
1134 	{0x4025, 0x00},
1135 	{0x4026, 0x00},
1136 	{0x4027, 0x00},
1137 	{0x4028, 0x00},
1138 	{0x4029, 0x00},
1139 	{0x402a, 0x00},
1140 	{0x402b, 0x00},
1141 	{0x402c, 0x00},
1142 	{0x402d, 0x00},
1143 	{0x402e, 0x00},
1144 	{0x402f, 0x00},
1145 	{0x4040, 0x00},
1146 	{0x4041, 0x03},
1147 	{0x4042, 0x00},
1148 	{0x4043, 0x7A},
1149 	{0x4044, 0x00},
1150 	{0x4045, 0x7A},
1151 	{0x4046, 0x00},
1152 	{0x4047, 0x7A},
1153 	{0x4048, 0x00},
1154 	{0x4049, 0x7A},
1155 	{0x4307, 0x30},
1156 	{0x4500, 0x58},
1157 	{0x4501, 0x04},
1158 	{0x4502, 0x40},
1159 	{0x4503, 0x10},
1160 	{0x4508, 0xaa},
1161 	{0x4509, 0xaa},
1162 	{0x450a, 0x00},
1163 	{0x450b, 0x00},
1164 	{0x4600, 0x01},
1165 	{0x4601, 0x00},
1166 	{0x4700, 0xa4},
1167 	{0x4800, 0x4c},
1168 	{0x4816, 0x53},
1169 	{0x481f, 0x40},
1170 	{0x4837, 0x13},
1171 	{0x5000, 0x56},
1172 	{0x5001, 0x01},
1173 	{0x5002, 0x28},
1174 	{0x5004, 0x0c},
1175 	{0x5006, 0x0c},
1176 	{0x5007, 0xe0},
1177 	{0x5008, 0x01},
1178 	{0x5009, 0xb0},
1179 	{0x5901, 0x00},
1180 	{0x5a01, 0x00},
1181 	{0x5a03, 0x00},
1182 	{0x5a04, 0x0c},
1183 	{0x5a05, 0xe0},
1184 	{0x5a06, 0x09},
1185 	{0x5a07, 0xb0},
1186 	{0x5a08, 0x06},
1187 	{0x5e00, 0x00},
1188 	{0x3734, 0x40},
1189 	{0x5b00, 0x01},
1190 	{0x5b01, 0x10},
1191 	{0x5b02, 0x01},
1192 	{0x5b03, 0xdb},
1193 	{0x3d8c, 0x71},
1194 	{0x3d8d, 0xea},
1195 	{0x4017, 0x08},
1196 	{0x3618, 0x2a},
1197 	{0x5780, 0x3e},
1198 	{0x5781, 0x0f},
1199 	{0x5782, 0x44},
1200 	{0x5783, 0x02},
1201 	{0x5784, 0x01},
1202 	{0x5785, 0x01},
1203 	{0x5786, 0x00},
1204 	{0x5787, 0x04},
1205 	{0x5788, 0x02},
1206 	{0x5789, 0x0f},
1207 	{0x578a, 0xfd},
1208 	{0x578b, 0xf5},
1209 	{0x578c, 0xf5},
1210 	{0x578d, 0x03},
1211 	{0x578e, 0x08},
1212 	{0x578f, 0x0c},
1213 	{0x5790, 0x08},
1214 	{0x5791, 0x06},
1215 	{0x5792, 0x00},
1216 	{0x5793, 0x52},
1217 	{0x5794, 0xa3},
1218 	{0x5045, 0x05},
1219 	{0x4003, 0x40},
1220 	{0x5048, 0x40}
1221 };
1222 
1223 static const struct ov5670_reg mode_1280x720_regs[] = {
1224 	{0x3000, 0x00},
1225 	{0x3002, 0x21},
1226 	{0x3005, 0xf0},
1227 	{0x3007, 0x00},
1228 	{0x3015, 0x0f},
1229 	{0x3018, 0x32},
1230 	{0x301a, 0xf0},
1231 	{0x301b, 0xf0},
1232 	{0x301c, 0xf0},
1233 	{0x301d, 0xf0},
1234 	{0x301e, 0xf0},
1235 	{0x3030, 0x00},
1236 	{0x3031, 0x0a},
1237 	{0x303c, 0xff},
1238 	{0x303e, 0xff},
1239 	{0x3040, 0xf0},
1240 	{0x3041, 0x00},
1241 	{0x3042, 0xf0},
1242 	{0x3106, 0x11},
1243 	{0x3500, 0x00},
1244 	{0x3501, 0x80},
1245 	{0x3502, 0x00},
1246 	{0x3503, 0x04},
1247 	{0x3504, 0x03},
1248 	{0x3505, 0x83},
1249 	{0x3508, 0x04},
1250 	{0x3509, 0x00},
1251 	{0x350e, 0x04},
1252 	{0x350f, 0x00},
1253 	{0x3510, 0x00},
1254 	{0x3511, 0x02},
1255 	{0x3512, 0x00},
1256 	{0x3601, 0xc8},
1257 	{0x3610, 0x88},
1258 	{0x3612, 0x48},
1259 	{0x3614, 0x5b},
1260 	{0x3615, 0x96},
1261 	{0x3621, 0xd0},
1262 	{0x3622, 0x00},
1263 	{0x3623, 0x00},
1264 	{0x3633, 0x13},
1265 	{0x3634, 0x13},
1266 	{0x3635, 0x13},
1267 	{0x3636, 0x13},
1268 	{0x3645, 0x13},
1269 	{0x3646, 0x82},
1270 	{0x3650, 0x00},
1271 	{0x3652, 0xff},
1272 	{0x3655, 0x20},
1273 	{0x3656, 0xff},
1274 	{0x365a, 0xff},
1275 	{0x365e, 0xff},
1276 	{0x3668, 0x00},
1277 	{0x366a, 0x07},
1278 	{0x366e, 0x08},
1279 	{0x366d, 0x00},
1280 	{0x366f, 0x80},
1281 	{0x3700, 0x28},
1282 	{0x3701, 0x10},
1283 	{0x3702, 0x3a},
1284 	{0x3703, 0x19},
1285 	{0x3704, 0x10},
1286 	{0x3705, 0x00},
1287 	{0x3706, 0x66},
1288 	{0x3707, 0x08},
1289 	{0x3708, 0x34},
1290 	{0x3709, 0x40},
1291 	{0x370a, 0x01},
1292 	{0x370b, 0x1b},
1293 	{0x3714, 0x24},
1294 	{0x371a, 0x3e},
1295 	{0x3733, 0x00},
1296 	{0x3734, 0x00},
1297 	{0x373a, 0x05},
1298 	{0x373b, 0x06},
1299 	{0x373c, 0x0a},
1300 	{0x373f, 0xa0},
1301 	{0x3755, 0x00},
1302 	{0x3758, 0x00},
1303 	{0x375b, 0x0e},
1304 	{0x3766, 0x5f},
1305 	{0x3768, 0x00},
1306 	{0x3769, 0x22},
1307 	{0x3773, 0x08},
1308 	{0x3774, 0x1f},
1309 	{0x3776, 0x06},
1310 	{0x37a0, 0x88},
1311 	{0x37a1, 0x5c},
1312 	{0x37a7, 0x88},
1313 	{0x37a8, 0x70},
1314 	{0x37aa, 0x88},
1315 	{0x37ab, 0x48},
1316 	{0x37b3, 0x66},
1317 	{0x37c2, 0x04},
1318 	{0x37c5, 0x00},
1319 	{0x37c8, 0x00},
1320 	{0x3800, 0x00},
1321 	{0x3801, 0x0c},
1322 	{0x3802, 0x00},
1323 	{0x3803, 0x04},
1324 	{0x3804, 0x0a},
1325 	{0x3805, 0x33},
1326 	{0x3806, 0x07},
1327 	{0x3807, 0xa3},
1328 	{0x3808, 0x05},
1329 	{0x3809, 0x00},
1330 	{0x380a, 0x02},
1331 	{0x380b, 0xd0},
1332 	{0x380c, 0x06},
1333 	{0x380d, 0x90},
1334 	{0x380e, 0x08},
1335 	{0x380f, 0x08},
1336 	{0x3811, 0x04},
1337 	{0x3813, 0x02},
1338 	{0x3814, 0x03},
1339 	{0x3815, 0x01},
1340 	{0x3816, 0x00},
1341 	{0x3817, 0x00},
1342 	{0x3818, 0x00},
1343 	{0x3819, 0x00},
1344 	{0x3820, 0x94},
1345 	{0x3821, 0x47},
1346 	{0x3822, 0x48},
1347 	{0x3826, 0x00},
1348 	{0x3827, 0x08},
1349 	{0x382a, 0x03},
1350 	{0x382b, 0x01},
1351 	{0x3830, 0x08},
1352 	{0x3836, 0x02},
1353 	{0x3837, 0x00},
1354 	{0x3838, 0x10},
1355 	{0x3841, 0xff},
1356 	{0x3846, 0x48},
1357 	{0x3861, 0x00},
1358 	{0x3862, 0x04},
1359 	{0x3863, 0x06},
1360 	{0x3a11, 0x01},
1361 	{0x3a12, 0x78},
1362 	{0x3b00, 0x00},
1363 	{0x3b02, 0x00},
1364 	{0x3b03, 0x00},
1365 	{0x3b04, 0x00},
1366 	{0x3b05, 0x00},
1367 	{0x3c00, 0x89},
1368 	{0x3c01, 0xab},
1369 	{0x3c02, 0x01},
1370 	{0x3c03, 0x00},
1371 	{0x3c04, 0x00},
1372 	{0x3c05, 0x03},
1373 	{0x3c06, 0x00},
1374 	{0x3c07, 0x05},
1375 	{0x3c0c, 0x00},
1376 	{0x3c0d, 0x00},
1377 	{0x3c0e, 0x00},
1378 	{0x3c0f, 0x00},
1379 	{0x3c40, 0x00},
1380 	{0x3c41, 0xa3},
1381 	{0x3c43, 0x7d},
1382 	{0x3c45, 0xd7},
1383 	{0x3c47, 0xfc},
1384 	{0x3c50, 0x05},
1385 	{0x3c52, 0xaa},
1386 	{0x3c54, 0x71},
1387 	{0x3c56, 0x80},
1388 	{0x3d85, 0x17},
1389 	{0x3f03, 0x00},
1390 	{0x3f0a, 0x00},
1391 	{0x3f0b, 0x00},
1392 	{0x4001, 0x60},
1393 	{0x4009, 0x05},
1394 	{0x4020, 0x00},
1395 	{0x4021, 0x00},
1396 	{0x4022, 0x00},
1397 	{0x4023, 0x00},
1398 	{0x4024, 0x00},
1399 	{0x4025, 0x00},
1400 	{0x4026, 0x00},
1401 	{0x4027, 0x00},
1402 	{0x4028, 0x00},
1403 	{0x4029, 0x00},
1404 	{0x402a, 0x00},
1405 	{0x402b, 0x00},
1406 	{0x402c, 0x00},
1407 	{0x402d, 0x00},
1408 	{0x402e, 0x00},
1409 	{0x402f, 0x00},
1410 	{0x4040, 0x00},
1411 	{0x4041, 0x03},
1412 	{0x4042, 0x00},
1413 	{0x4043, 0x7A},
1414 	{0x4044, 0x00},
1415 	{0x4045, 0x7A},
1416 	{0x4046, 0x00},
1417 	{0x4047, 0x7A},
1418 	{0x4048, 0x00},
1419 	{0x4049, 0x7A},
1420 	{0x4307, 0x30},
1421 	{0x4500, 0x58},
1422 	{0x4501, 0x04},
1423 	{0x4502, 0x48},
1424 	{0x4503, 0x10},
1425 	{0x4508, 0x55},
1426 	{0x4509, 0x55},
1427 	{0x450a, 0x00},
1428 	{0x450b, 0x00},
1429 	{0x4600, 0x00},
1430 	{0x4601, 0x80},
1431 	{0x4700, 0xa4},
1432 	{0x4800, 0x4c},
1433 	{0x4816, 0x53},
1434 	{0x481f, 0x40},
1435 	{0x4837, 0x13},
1436 	{0x5000, 0x56},
1437 	{0x5001, 0x01},
1438 	{0x5002, 0x28},
1439 	{0x5004, 0x0c},
1440 	{0x5006, 0x0c},
1441 	{0x5007, 0xe0},
1442 	{0x5008, 0x01},
1443 	{0x5009, 0xb0},
1444 	{0x5901, 0x00},
1445 	{0x5a01, 0x00},
1446 	{0x5a03, 0x00},
1447 	{0x5a04, 0x0c},
1448 	{0x5a05, 0xe0},
1449 	{0x5a06, 0x09},
1450 	{0x5a07, 0xb0},
1451 	{0x5a08, 0x06},
1452 	{0x5e00, 0x00},
1453 	{0x3734, 0x40},
1454 	{0x5b00, 0x01},
1455 	{0x5b01, 0x10},
1456 	{0x5b02, 0x01},
1457 	{0x5b03, 0xdb},
1458 	{0x3d8c, 0x71},
1459 	{0x3d8d, 0xea},
1460 	{0x4017, 0x10},
1461 	{0x3618, 0x2a},
1462 	{0x5780, 0x3e},
1463 	{0x5781, 0x0f},
1464 	{0x5782, 0x44},
1465 	{0x5783, 0x02},
1466 	{0x5784, 0x01},
1467 	{0x5785, 0x01},
1468 	{0x5786, 0x00},
1469 	{0x5787, 0x04},
1470 	{0x5788, 0x02},
1471 	{0x5789, 0x0f},
1472 	{0x578a, 0xfd},
1473 	{0x578b, 0xf5},
1474 	{0x578c, 0xf5},
1475 	{0x578d, 0x03},
1476 	{0x578e, 0x08},
1477 	{0x578f, 0x0c},
1478 	{0x5790, 0x08},
1479 	{0x5791, 0x06},
1480 	{0x5792, 0x00},
1481 	{0x5793, 0x52},
1482 	{0x5794, 0xa3},
1483 	{0x3503, 0x00},
1484 	{0x5045, 0x05},
1485 	{0x4003, 0x40},
1486 	{0x5048, 0x40}
1487 };
1488 
1489 static const struct ov5670_reg mode_640x360_regs[] = {
1490 	{0x3000, 0x00},
1491 	{0x3002, 0x21},
1492 	{0x3005, 0xf0},
1493 	{0x3007, 0x00},
1494 	{0x3015, 0x0f},
1495 	{0x3018, 0x32},
1496 	{0x301a, 0xf0},
1497 	{0x301b, 0xf0},
1498 	{0x301c, 0xf0},
1499 	{0x301d, 0xf0},
1500 	{0x301e, 0xf0},
1501 	{0x3030, 0x00},
1502 	{0x3031, 0x0a},
1503 	{0x303c, 0xff},
1504 	{0x303e, 0xff},
1505 	{0x3040, 0xf0},
1506 	{0x3041, 0x00},
1507 	{0x3042, 0xf0},
1508 	{0x3106, 0x11},
1509 	{0x3500, 0x00},
1510 	{0x3501, 0x80},
1511 	{0x3502, 0x00},
1512 	{0x3503, 0x04},
1513 	{0x3504, 0x03},
1514 	{0x3505, 0x83},
1515 	{0x3508, 0x04},
1516 	{0x3509, 0x00},
1517 	{0x350e, 0x04},
1518 	{0x350f, 0x00},
1519 	{0x3510, 0x00},
1520 	{0x3511, 0x02},
1521 	{0x3512, 0x00},
1522 	{0x3601, 0xc8},
1523 	{0x3610, 0x88},
1524 	{0x3612, 0x48},
1525 	{0x3614, 0x5b},
1526 	{0x3615, 0x96},
1527 	{0x3621, 0xd0},
1528 	{0x3622, 0x00},
1529 	{0x3623, 0x04},
1530 	{0x3633, 0x13},
1531 	{0x3634, 0x13},
1532 	{0x3635, 0x13},
1533 	{0x3636, 0x13},
1534 	{0x3645, 0x13},
1535 	{0x3646, 0x82},
1536 	{0x3650, 0x00},
1537 	{0x3652, 0xff},
1538 	{0x3655, 0x20},
1539 	{0x3656, 0xff},
1540 	{0x365a, 0xff},
1541 	{0x365e, 0xff},
1542 	{0x3668, 0x00},
1543 	{0x366a, 0x07},
1544 	{0x366e, 0x08},
1545 	{0x366d, 0x00},
1546 	{0x366f, 0x80},
1547 	{0x3700, 0x28},
1548 	{0x3701, 0x10},
1549 	{0x3702, 0x3a},
1550 	{0x3703, 0x19},
1551 	{0x3704, 0x10},
1552 	{0x3705, 0x00},
1553 	{0x3706, 0x66},
1554 	{0x3707, 0x08},
1555 	{0x3708, 0x34},
1556 	{0x3709, 0x40},
1557 	{0x370a, 0x01},
1558 	{0x370b, 0x1b},
1559 	{0x3714, 0x24},
1560 	{0x371a, 0x3e},
1561 	{0x3733, 0x00},
1562 	{0x3734, 0x00},
1563 	{0x373a, 0x05},
1564 	{0x373b, 0x06},
1565 	{0x373c, 0x0a},
1566 	{0x373f, 0xa0},
1567 	{0x3755, 0x00},
1568 	{0x3758, 0x00},
1569 	{0x375b, 0x0e},
1570 	{0x3766, 0x5f},
1571 	{0x3768, 0x00},
1572 	{0x3769, 0x22},
1573 	{0x3773, 0x08},
1574 	{0x3774, 0x1f},
1575 	{0x3776, 0x06},
1576 	{0x37a0, 0x88},
1577 	{0x37a1, 0x5c},
1578 	{0x37a7, 0x88},
1579 	{0x37a8, 0x70},
1580 	{0x37aa, 0x88},
1581 	{0x37ab, 0x48},
1582 	{0x37b3, 0x66},
1583 	{0x37c2, 0x04},
1584 	{0x37c5, 0x00},
1585 	{0x37c8, 0x00},
1586 	{0x3800, 0x00},
1587 	{0x3801, 0x0c},
1588 	{0x3802, 0x00},
1589 	{0x3803, 0x04},
1590 	{0x3804, 0x0a},
1591 	{0x3805, 0x33},
1592 	{0x3806, 0x07},
1593 	{0x3807, 0xa3},
1594 	{0x3808, 0x02},
1595 	{0x3809, 0x80},
1596 	{0x380a, 0x01},
1597 	{0x380b, 0x68},
1598 	{0x380c, 0x06},
1599 	{0x380d, 0x90},
1600 	{0x380e, 0x08},
1601 	{0x380f, 0x08},
1602 	{0x3811, 0x04},
1603 	{0x3813, 0x02},
1604 	{0x3814, 0x07},
1605 	{0x3815, 0x01},
1606 	{0x3816, 0x00},
1607 	{0x3817, 0x00},
1608 	{0x3818, 0x00},
1609 	{0x3819, 0x00},
1610 	{0x3820, 0x94},
1611 	{0x3821, 0xc6},
1612 	{0x3822, 0x48},
1613 	{0x3826, 0x00},
1614 	{0x3827, 0x08},
1615 	{0x382a, 0x07},
1616 	{0x382b, 0x01},
1617 	{0x3830, 0x08},
1618 	{0x3836, 0x02},
1619 	{0x3837, 0x00},
1620 	{0x3838, 0x10},
1621 	{0x3841, 0xff},
1622 	{0x3846, 0x48},
1623 	{0x3861, 0x00},
1624 	{0x3862, 0x04},
1625 	{0x3863, 0x06},
1626 	{0x3a11, 0x01},
1627 	{0x3a12, 0x78},
1628 	{0x3b00, 0x00},
1629 	{0x3b02, 0x00},
1630 	{0x3b03, 0x00},
1631 	{0x3b04, 0x00},
1632 	{0x3b05, 0x00},
1633 	{0x3c00, 0x89},
1634 	{0x3c01, 0xab},
1635 	{0x3c02, 0x01},
1636 	{0x3c03, 0x00},
1637 	{0x3c04, 0x00},
1638 	{0x3c05, 0x03},
1639 	{0x3c06, 0x00},
1640 	{0x3c07, 0x05},
1641 	{0x3c0c, 0x00},
1642 	{0x3c0d, 0x00},
1643 	{0x3c0e, 0x00},
1644 	{0x3c0f, 0x00},
1645 	{0x3c40, 0x00},
1646 	{0x3c41, 0xa3},
1647 	{0x3c43, 0x7d},
1648 	{0x3c45, 0xd7},
1649 	{0x3c47, 0xfc},
1650 	{0x3c50, 0x05},
1651 	{0x3c52, 0xaa},
1652 	{0x3c54, 0x71},
1653 	{0x3c56, 0x80},
1654 	{0x3d85, 0x17},
1655 	{0x3f03, 0x00},
1656 	{0x3f0a, 0x00},
1657 	{0x3f0b, 0x00},
1658 	{0x4001, 0x60},
1659 	{0x4009, 0x05},
1660 	{0x4020, 0x00},
1661 	{0x4021, 0x00},
1662 	{0x4022, 0x00},
1663 	{0x4023, 0x00},
1664 	{0x4024, 0x00},
1665 	{0x4025, 0x00},
1666 	{0x4026, 0x00},
1667 	{0x4027, 0x00},
1668 	{0x4028, 0x00},
1669 	{0x4029, 0x00},
1670 	{0x402a, 0x00},
1671 	{0x402b, 0x00},
1672 	{0x402c, 0x00},
1673 	{0x402d, 0x00},
1674 	{0x402e, 0x00},
1675 	{0x402f, 0x00},
1676 	{0x4040, 0x00},
1677 	{0x4041, 0x03},
1678 	{0x4042, 0x00},
1679 	{0x4043, 0x7A},
1680 	{0x4044, 0x00},
1681 	{0x4045, 0x7A},
1682 	{0x4046, 0x00},
1683 	{0x4047, 0x7A},
1684 	{0x4048, 0x00},
1685 	{0x4049, 0x7A},
1686 	{0x4307, 0x30},
1687 	{0x4500, 0x58},
1688 	{0x4501, 0x04},
1689 	{0x4502, 0x40},
1690 	{0x4503, 0x10},
1691 	{0x4508, 0x55},
1692 	{0x4509, 0x55},
1693 	{0x450a, 0x02},
1694 	{0x450b, 0x00},
1695 	{0x4600, 0x00},
1696 	{0x4601, 0x40},
1697 	{0x4700, 0xa4},
1698 	{0x4800, 0x4c},
1699 	{0x4816, 0x53},
1700 	{0x481f, 0x40},
1701 	{0x4837, 0x13},
1702 	{0x5000, 0x56},
1703 	{0x5001, 0x01},
1704 	{0x5002, 0x28},
1705 	{0x5004, 0x0c},
1706 	{0x5006, 0x0c},
1707 	{0x5007, 0xe0},
1708 	{0x5008, 0x01},
1709 	{0x5009, 0xb0},
1710 	{0x5901, 0x00},
1711 	{0x5a01, 0x00},
1712 	{0x5a03, 0x00},
1713 	{0x5a04, 0x0c},
1714 	{0x5a05, 0xe0},
1715 	{0x5a06, 0x09},
1716 	{0x5a07, 0xb0},
1717 	{0x5a08, 0x06},
1718 	{0x5e00, 0x00},
1719 	{0x3734, 0x40},
1720 	{0x5b00, 0x01},
1721 	{0x5b01, 0x10},
1722 	{0x5b02, 0x01},
1723 	{0x5b03, 0xdb},
1724 	{0x3d8c, 0x71},
1725 	{0x3d8d, 0xea},
1726 	{0x4017, 0x10},
1727 	{0x3618, 0x2a},
1728 	{0x5780, 0x3e},
1729 	{0x5781, 0x0f},
1730 	{0x5782, 0x44},
1731 	{0x5783, 0x02},
1732 	{0x5784, 0x01},
1733 	{0x5785, 0x01},
1734 	{0x5786, 0x00},
1735 	{0x5787, 0x04},
1736 	{0x5788, 0x02},
1737 	{0x5789, 0x0f},
1738 	{0x578a, 0xfd},
1739 	{0x578b, 0xf5},
1740 	{0x578c, 0xf5},
1741 	{0x578d, 0x03},
1742 	{0x578e, 0x08},
1743 	{0x578f, 0x0c},
1744 	{0x5790, 0x08},
1745 	{0x5791, 0x06},
1746 	{0x5792, 0x00},
1747 	{0x5793, 0x52},
1748 	{0x5794, 0xa3},
1749 	{0x3503, 0x00},
1750 	{0x5045, 0x05},
1751 	{0x4003, 0x40},
1752 	{0x5048, 0x40}
1753 };
1754 
1755 static const char * const ov5670_test_pattern_menu[] = {
1756 	"Disabled",
1757 	"Vertical Color Bar Type 1",
1758 };
1759 
1760 /* Supported link frequencies */
1761 #define OV5670_LINK_FREQ_422MHZ		422400000
1762 #define OV5670_LINK_FREQ_422MHZ_INDEX	0
1763 static const struct ov5670_link_freq_config link_freq_configs[] = {
1764 	{
1765 		/* pixel_rate = link_freq * 2 * nr_of_lanes / bits_per_sample */
1766 		.pixel_rate = (OV5670_LINK_FREQ_422MHZ * 2 * 2) / 10,
1767 		.reg_list = {
1768 			.num_of_regs = ARRAY_SIZE(mipi_data_rate_840mbps),
1769 			.regs = mipi_data_rate_840mbps,
1770 		}
1771 	}
1772 };
1773 
1774 static const s64 link_freq_menu_items[] = {
1775 	OV5670_LINK_FREQ_422MHZ
1776 };
1777 
1778 /*
1779  * OV5670 sensor supports following resolutions with full FOV:
1780  * 4:3  ==> {2592x1944, 1296x972, 648x486}
1781  * 16:9 ==> {2560x1440, 1280x720, 640x360}
1782  */
1783 static const struct ov5670_mode supported_modes[] = {
1784 	{
1785 		.width = 2592,
1786 		.height = 1944,
1787 		.vts_def = OV5670_VTS_30FPS,
1788 		.vts_min = OV5670_VTS_30FPS,
1789 		.link_freq_index = OV5670_LINK_FREQ_422MHZ_INDEX,
1790 		.analog_crop = &ov5670_analog_crop,
1791 		.reg_list = {
1792 			.num_of_regs = ARRAY_SIZE(mode_2592x1944_regs),
1793 			.regs = mode_2592x1944_regs,
1794 		},
1795 	},
1796 	{
1797 		.width = 1296,
1798 		.height = 972,
1799 		.vts_def = OV5670_VTS_30FPS,
1800 		.vts_min = 996,
1801 		.link_freq_index = OV5670_LINK_FREQ_422MHZ_INDEX,
1802 		.analog_crop = &ov5670_analog_crop,
1803 		.reg_list = {
1804 			.num_of_regs = ARRAY_SIZE(mode_1296x972_regs),
1805 			.regs = mode_1296x972_regs,
1806 		},
1807 	},
1808 	{
1809 		.width = 648,
1810 		.height = 486,
1811 		.vts_def = OV5670_VTS_30FPS,
1812 		.vts_min = 516,
1813 		.link_freq_index = OV5670_LINK_FREQ_422MHZ_INDEX,
1814 		.analog_crop = &ov5670_analog_crop,
1815 		.reg_list = {
1816 			.num_of_regs = ARRAY_SIZE(mode_648x486_regs),
1817 			.regs = mode_648x486_regs,
1818 		},
1819 	},
1820 	{
1821 		.width = 2560,
1822 		.height = 1440,
1823 		.vts_def = OV5670_VTS_30FPS,
1824 		.vts_min = OV5670_VTS_30FPS,
1825 		.link_freq_index = OV5670_LINK_FREQ_422MHZ_INDEX,
1826 		.analog_crop = &ov5670_analog_crop,
1827 		.reg_list = {
1828 			.num_of_regs = ARRAY_SIZE(mode_2560x1440_regs),
1829 			.regs = mode_2560x1440_regs,
1830 		},
1831 	},
1832 	{
1833 		.width = 1280,
1834 		.height = 720,
1835 		.vts_def = OV5670_VTS_30FPS,
1836 		.vts_min = 1020,
1837 
1838 		.link_freq_index = OV5670_LINK_FREQ_422MHZ_INDEX,
1839 		.analog_crop = &ov5670_analog_crop,
1840 		.reg_list = {
1841 			.num_of_regs = ARRAY_SIZE(mode_1280x720_regs),
1842 			.regs = mode_1280x720_regs,
1843 		},
1844 	},
1845 	{
1846 		.width = 640,
1847 		.height = 360,
1848 		.vts_def = OV5670_VTS_30FPS,
1849 		.vts_min = 510,
1850 		.link_freq_index = OV5670_LINK_FREQ_422MHZ_INDEX,
1851 		.analog_crop = &ov5670_analog_crop,
1852 		.reg_list = {
1853 			.num_of_regs = ARRAY_SIZE(mode_640x360_regs),
1854 			.regs = mode_640x360_regs,
1855 		},
1856 	}
1857 };
1858 
1859 struct ov5670 {
1860 	struct v4l2_subdev sd;
1861 	struct media_pad pad;
1862 
1863 	struct v4l2_ctrl_handler ctrl_handler;
1864 	/* V4L2 Controls */
1865 	struct v4l2_ctrl *link_freq;
1866 	struct v4l2_ctrl *pixel_rate;
1867 	struct v4l2_ctrl *vblank;
1868 	struct v4l2_ctrl *hblank;
1869 	struct v4l2_ctrl *exposure;
1870 
1871 	/* Current mode */
1872 	const struct ov5670_mode *cur_mode;
1873 
1874 	/* xvclk input clock */
1875 	struct clk *xvclk;
1876 
1877 	/* Regulators */
1878 	struct regulator_bulk_data supplies[OV5670_NUM_SUPPLIES];
1879 
1880 	/* Power-down and reset gpios. */
1881 	struct gpio_desc *pwdn_gpio; /* PWDNB pin. */
1882 	struct gpio_desc *reset_gpio; /* XSHUTDOWN pin. */
1883 
1884 	/* To serialize asynchronus callbacks */
1885 	struct mutex mutex;
1886 
1887 	/* Streaming on/off */
1888 	bool streaming;
1889 	/* True if the device has been identified */
1890 	bool identified;
1891 };
1892 
1893 #define to_ov5670(_sd)	container_of(_sd, struct ov5670, sd)
1894 
1895 /* Read registers up to 4 at a time */
1896 static int ov5670_read_reg(struct ov5670 *ov5670, u16 reg, unsigned int len,
1897 			   u32 *val)
1898 {
1899 	struct i2c_client *client = v4l2_get_subdevdata(&ov5670->sd);
1900 	struct i2c_msg msgs[2];
1901 	u8 *data_be_p;
1902 	__be32 data_be = 0;
1903 	__be16 reg_addr_be = cpu_to_be16(reg);
1904 	int ret;
1905 
1906 	if (len > 4)
1907 		return -EINVAL;
1908 
1909 	data_be_p = (u8 *)&data_be;
1910 	/* Write register address */
1911 	msgs[0].addr = client->addr;
1912 	msgs[0].flags = 0;
1913 	msgs[0].len = 2;
1914 	msgs[0].buf = (u8 *)&reg_addr_be;
1915 
1916 	/* Read data from register */
1917 	msgs[1].addr = client->addr;
1918 	msgs[1].flags = I2C_M_RD;
1919 	msgs[1].len = len;
1920 	msgs[1].buf = &data_be_p[4 - len];
1921 
1922 	ret = i2c_transfer(client->adapter, msgs, ARRAY_SIZE(msgs));
1923 	if (ret != ARRAY_SIZE(msgs))
1924 		return -EIO;
1925 
1926 	*val = be32_to_cpu(data_be);
1927 
1928 	return 0;
1929 }
1930 
1931 /* Write registers up to 4 at a time */
1932 static int ov5670_write_reg(struct ov5670 *ov5670, u16 reg, unsigned int len,
1933 			    u32 val)
1934 {
1935 	struct i2c_client *client = v4l2_get_subdevdata(&ov5670->sd);
1936 	int buf_i;
1937 	int val_i;
1938 	u8 buf[6];
1939 	u8 *val_p;
1940 	__be32 tmp;
1941 
1942 	if (len > 4)
1943 		return -EINVAL;
1944 
1945 	buf[0] = reg >> 8;
1946 	buf[1] = reg & 0xff;
1947 
1948 	tmp = cpu_to_be32(val);
1949 	val_p = (u8 *)&tmp;
1950 	buf_i = 2;
1951 	val_i = 4 - len;
1952 
1953 	while (val_i < 4)
1954 		buf[buf_i++] = val_p[val_i++];
1955 
1956 	if (i2c_master_send(client, buf, len + 2) != len + 2)
1957 		return -EIO;
1958 
1959 	return 0;
1960 }
1961 
1962 /* Write a list of registers */
1963 static int ov5670_write_regs(struct ov5670 *ov5670,
1964 			     const struct ov5670_reg *regs, unsigned int len)
1965 {
1966 	struct i2c_client *client = v4l2_get_subdevdata(&ov5670->sd);
1967 	unsigned int i;
1968 	int ret;
1969 
1970 	for (i = 0; i < len; i++) {
1971 		ret = ov5670_write_reg(ov5670, regs[i].address, 1, regs[i].val);
1972 		if (ret) {
1973 			dev_err_ratelimited(
1974 				&client->dev,
1975 				"Failed to write reg 0x%4.4x. error = %d\n",
1976 				regs[i].address, ret);
1977 
1978 			return ret;
1979 		}
1980 	}
1981 
1982 	return 0;
1983 }
1984 
1985 static int ov5670_write_reg_list(struct ov5670 *ov5670,
1986 				 const struct ov5670_reg_list *r_list)
1987 {
1988 	return ov5670_write_regs(ov5670, r_list->regs, r_list->num_of_regs);
1989 }
1990 
1991 static int ov5670_update_digital_gain(struct ov5670 *ov5670, u32 d_gain)
1992 {
1993 	int ret;
1994 
1995 	ret = ov5670_write_reg(ov5670, OV5670_REG_R_DGTL_GAIN,
1996 			       OV5670_REG_VALUE_16BIT, d_gain);
1997 	if (ret)
1998 		return ret;
1999 
2000 	ret = ov5670_write_reg(ov5670, OV5670_REG_G_DGTL_GAIN,
2001 			       OV5670_REG_VALUE_16BIT, d_gain);
2002 	if (ret)
2003 		return ret;
2004 
2005 	return ov5670_write_reg(ov5670, OV5670_REG_B_DGTL_GAIN,
2006 				OV5670_REG_VALUE_16BIT, d_gain);
2007 }
2008 
2009 static int ov5670_enable_test_pattern(struct ov5670 *ov5670, u32 pattern)
2010 {
2011 	u32 val;
2012 	int ret;
2013 
2014 	/* Set the bayer order that we support */
2015 	ret = ov5670_write_reg(ov5670, OV5670_REG_TEST_PATTERN_CTRL,
2016 			       OV5670_REG_VALUE_08BIT, 0);
2017 	if (ret)
2018 		return ret;
2019 
2020 	ret = ov5670_read_reg(ov5670, OV5670_REG_TEST_PATTERN,
2021 			      OV5670_REG_VALUE_08BIT, &val);
2022 	if (ret)
2023 		return ret;
2024 
2025 	if (pattern)
2026 		val |= OV5670_TEST_PATTERN_ENABLE;
2027 	else
2028 		val &= ~OV5670_TEST_PATTERN_ENABLE;
2029 
2030 	return ov5670_write_reg(ov5670, OV5670_REG_TEST_PATTERN,
2031 				OV5670_REG_VALUE_08BIT, val);
2032 }
2033 
2034 /* Initialize control handlers */
2035 static int ov5670_set_ctrl(struct v4l2_ctrl *ctrl)
2036 {
2037 	struct ov5670 *ov5670 = container_of(ctrl->handler,
2038 					     struct ov5670, ctrl_handler);
2039 	struct i2c_client *client = v4l2_get_subdevdata(&ov5670->sd);
2040 	s64 max;
2041 	int ret;
2042 
2043 	/* Propagate change of current control to all related controls */
2044 	switch (ctrl->id) {
2045 	case V4L2_CID_VBLANK:
2046 		/* Update max exposure while meeting expected vblanking */
2047 		max = ov5670->cur_mode->height + ctrl->val - 8;
2048 		__v4l2_ctrl_modify_range(ov5670->exposure,
2049 					 ov5670->exposure->minimum, max,
2050 					 ov5670->exposure->step, max);
2051 		break;
2052 	}
2053 
2054 	/* V4L2 controls values will be applied only when power is already up */
2055 	if (!pm_runtime_get_if_in_use(&client->dev))
2056 		return 0;
2057 
2058 	switch (ctrl->id) {
2059 	case V4L2_CID_ANALOGUE_GAIN:
2060 		ret = ov5670_write_reg(ov5670, OV5670_REG_ANALOG_GAIN,
2061 				       OV5670_REG_VALUE_16BIT, ctrl->val);
2062 		break;
2063 	case V4L2_CID_DIGITAL_GAIN:
2064 		ret = ov5670_update_digital_gain(ov5670, ctrl->val);
2065 		break;
2066 	case V4L2_CID_EXPOSURE:
2067 		/* 4 least significant bits of expsoure are fractional part */
2068 		ret = ov5670_write_reg(ov5670, OV5670_REG_EXPOSURE,
2069 				       OV5670_REG_VALUE_24BIT, ctrl->val << 4);
2070 		break;
2071 	case V4L2_CID_VBLANK:
2072 		/* Update VTS that meets expected vertical blanking */
2073 		ret = ov5670_write_reg(ov5670, OV5670_REG_VTS,
2074 				       OV5670_REG_VALUE_16BIT,
2075 				       ov5670->cur_mode->height + ctrl->val);
2076 		break;
2077 	case V4L2_CID_TEST_PATTERN:
2078 		ret = ov5670_enable_test_pattern(ov5670, ctrl->val);
2079 		break;
2080 	case V4L2_CID_HBLANK:
2081 	case V4L2_CID_LINK_FREQ:
2082 	case V4L2_CID_PIXEL_RATE:
2083 		ret = 0;
2084 		break;
2085 	default:
2086 		ret = -EINVAL;
2087 		dev_info(&client->dev, "%s Unhandled id:0x%x, val:0x%x\n",
2088 			 __func__, ctrl->id, ctrl->val);
2089 		break;
2090 	}
2091 
2092 	pm_runtime_put(&client->dev);
2093 
2094 	return ret;
2095 }
2096 
2097 static const struct v4l2_ctrl_ops ov5670_ctrl_ops = {
2098 	.s_ctrl = ov5670_set_ctrl,
2099 };
2100 
2101 /* Initialize control handlers */
2102 static int ov5670_init_controls(struct ov5670 *ov5670)
2103 {
2104 	struct i2c_client *client = v4l2_get_subdevdata(&ov5670->sd);
2105 	struct v4l2_fwnode_device_properties props;
2106 	struct v4l2_ctrl_handler *ctrl_hdlr;
2107 	s64 vblank_max;
2108 	s64 vblank_def;
2109 	s64 vblank_min;
2110 	s64 exposure_max;
2111 	int ret;
2112 
2113 	ctrl_hdlr = &ov5670->ctrl_handler;
2114 	ret = v4l2_ctrl_handler_init(ctrl_hdlr, 10);
2115 	if (ret)
2116 		return ret;
2117 
2118 	ctrl_hdlr->lock = &ov5670->mutex;
2119 	ov5670->link_freq = v4l2_ctrl_new_int_menu(ctrl_hdlr,
2120 						   &ov5670_ctrl_ops,
2121 						   V4L2_CID_LINK_FREQ,
2122 						   0, 0, link_freq_menu_items);
2123 	if (ov5670->link_freq)
2124 		ov5670->link_freq->flags |= V4L2_CTRL_FLAG_READ_ONLY;
2125 
2126 	/* By default, V4L2_CID_PIXEL_RATE is read only */
2127 	ov5670->pixel_rate = v4l2_ctrl_new_std(ctrl_hdlr, &ov5670_ctrl_ops,
2128 					       V4L2_CID_PIXEL_RATE,
2129 					       link_freq_configs[0].pixel_rate,
2130 					       link_freq_configs[0].pixel_rate,
2131 					       1,
2132 					       link_freq_configs[0].pixel_rate);
2133 
2134 	vblank_max = OV5670_VTS_MAX - ov5670->cur_mode->height;
2135 	vblank_def = ov5670->cur_mode->vts_def - ov5670->cur_mode->height;
2136 	vblank_min = ov5670->cur_mode->vts_min - ov5670->cur_mode->height;
2137 	ov5670->vblank = v4l2_ctrl_new_std(ctrl_hdlr, &ov5670_ctrl_ops,
2138 					   V4L2_CID_VBLANK, vblank_min,
2139 					   vblank_max, 1, vblank_def);
2140 
2141 	ov5670->hblank = v4l2_ctrl_new_std(
2142 				ctrl_hdlr, &ov5670_ctrl_ops, V4L2_CID_HBLANK,
2143 				OV5670_FIXED_PPL - ov5670->cur_mode->width,
2144 				OV5670_FIXED_PPL - ov5670->cur_mode->width, 1,
2145 				OV5670_FIXED_PPL - ov5670->cur_mode->width);
2146 	if (ov5670->hblank)
2147 		ov5670->hblank->flags |= V4L2_CTRL_FLAG_READ_ONLY;
2148 
2149 	/* Get min, max, step, default from sensor */
2150 	v4l2_ctrl_new_std(ctrl_hdlr, &ov5670_ctrl_ops, V4L2_CID_ANALOGUE_GAIN,
2151 			  ANALOG_GAIN_MIN, ANALOG_GAIN_MAX, ANALOG_GAIN_STEP,
2152 			  ANALOG_GAIN_DEFAULT);
2153 
2154 	/* Digital gain */
2155 	v4l2_ctrl_new_std(ctrl_hdlr, &ov5670_ctrl_ops, V4L2_CID_DIGITAL_GAIN,
2156 			  OV5670_DGTL_GAIN_MIN, OV5670_DGTL_GAIN_MAX,
2157 			  OV5670_DGTL_GAIN_STEP, OV5670_DGTL_GAIN_DEFAULT);
2158 
2159 	/* Get min, max, step, default from sensor */
2160 	exposure_max = ov5670->cur_mode->vts_def - 8;
2161 	ov5670->exposure = v4l2_ctrl_new_std(ctrl_hdlr, &ov5670_ctrl_ops,
2162 					     V4L2_CID_EXPOSURE,
2163 					     OV5670_EXPOSURE_MIN,
2164 					     exposure_max, OV5670_EXPOSURE_STEP,
2165 					     exposure_max);
2166 
2167 	v4l2_ctrl_new_std_menu_items(ctrl_hdlr, &ov5670_ctrl_ops,
2168 				     V4L2_CID_TEST_PATTERN,
2169 				     ARRAY_SIZE(ov5670_test_pattern_menu) - 1,
2170 				     0, 0, ov5670_test_pattern_menu);
2171 
2172 	if (ctrl_hdlr->error) {
2173 		ret = ctrl_hdlr->error;
2174 		goto error;
2175 	}
2176 
2177 	ret = v4l2_fwnode_device_parse(&client->dev, &props);
2178 	if (ret)
2179 		goto error;
2180 
2181 	ret = v4l2_ctrl_new_fwnode_properties(ctrl_hdlr, &ov5670_ctrl_ops,
2182 					      &props);
2183 	if (ret)
2184 		goto error;
2185 
2186 	ov5670->sd.ctrl_handler = ctrl_hdlr;
2187 
2188 	return 0;
2189 
2190 error:
2191 	v4l2_ctrl_handler_free(ctrl_hdlr);
2192 
2193 	return ret;
2194 }
2195 
2196 static int ov5670_init_cfg(struct v4l2_subdev *sd,
2197 			   struct v4l2_subdev_state *state)
2198 {
2199 	struct v4l2_mbus_framefmt *fmt =
2200 				v4l2_subdev_get_try_format(sd, state, 0);
2201 	const struct ov5670_mode *default_mode = &supported_modes[0];
2202 	struct v4l2_rect *crop = v4l2_subdev_get_try_crop(sd, state, 0);
2203 
2204 	fmt->width = default_mode->width;
2205 	fmt->height = default_mode->height;
2206 	fmt->code = MEDIA_BUS_FMT_SGRBG10_1X10;
2207 	fmt->field = V4L2_FIELD_NONE;
2208 	fmt->colorspace = V4L2_COLORSPACE_SRGB;
2209 	fmt->ycbcr_enc = V4L2_MAP_YCBCR_ENC_DEFAULT(V4L2_COLORSPACE_SRGB);
2210 	fmt->quantization = V4L2_QUANTIZATION_FULL_RANGE;
2211 	fmt->xfer_func = V4L2_MAP_XFER_FUNC_DEFAULT(V4L2_COLORSPACE_SRGB);
2212 
2213 	*crop = *default_mode->analog_crop;
2214 
2215 	return 0;
2216 }
2217 
2218 static int ov5670_enum_mbus_code(struct v4l2_subdev *sd,
2219 				 struct v4l2_subdev_state *sd_state,
2220 				 struct v4l2_subdev_mbus_code_enum *code)
2221 {
2222 	/* Only one bayer order GRBG is supported */
2223 	if (code->index > 0)
2224 		return -EINVAL;
2225 
2226 	code->code = MEDIA_BUS_FMT_SGRBG10_1X10;
2227 
2228 	return 0;
2229 }
2230 
2231 static int ov5670_enum_frame_size(struct v4l2_subdev *sd,
2232 				  struct v4l2_subdev_state *sd_state,
2233 				  struct v4l2_subdev_frame_size_enum *fse)
2234 {
2235 	if (fse->index >= ARRAY_SIZE(supported_modes))
2236 		return -EINVAL;
2237 
2238 	if (fse->code != MEDIA_BUS_FMT_SGRBG10_1X10)
2239 		return -EINVAL;
2240 
2241 	fse->min_width = supported_modes[fse->index].width;
2242 	fse->max_width = fse->min_width;
2243 	fse->min_height = supported_modes[fse->index].height;
2244 	fse->max_height = fse->min_height;
2245 
2246 	return 0;
2247 }
2248 
2249 static void ov5670_update_pad_format(const struct ov5670_mode *mode,
2250 				     struct v4l2_subdev_format *fmt)
2251 {
2252 	fmt->format.width = mode->width;
2253 	fmt->format.height = mode->height;
2254 	fmt->format.code = MEDIA_BUS_FMT_SGRBG10_1X10;
2255 	fmt->format.field = V4L2_FIELD_NONE;
2256 }
2257 
2258 static int ov5670_do_get_pad_format(struct ov5670 *ov5670,
2259 				    struct v4l2_subdev_state *sd_state,
2260 				    struct v4l2_subdev_format *fmt)
2261 {
2262 	if (fmt->which == V4L2_SUBDEV_FORMAT_TRY)
2263 		fmt->format = *v4l2_subdev_get_try_format(&ov5670->sd,
2264 							  sd_state,
2265 							  fmt->pad);
2266 	else
2267 		ov5670_update_pad_format(ov5670->cur_mode, fmt);
2268 
2269 	return 0;
2270 }
2271 
2272 static int ov5670_get_pad_format(struct v4l2_subdev *sd,
2273 				 struct v4l2_subdev_state *sd_state,
2274 				 struct v4l2_subdev_format *fmt)
2275 {
2276 	struct ov5670 *ov5670 = to_ov5670(sd);
2277 	int ret;
2278 
2279 	mutex_lock(&ov5670->mutex);
2280 	ret = ov5670_do_get_pad_format(ov5670, sd_state, fmt);
2281 	mutex_unlock(&ov5670->mutex);
2282 
2283 	return ret;
2284 }
2285 
2286 static int ov5670_set_pad_format(struct v4l2_subdev *sd,
2287 				 struct v4l2_subdev_state *sd_state,
2288 				 struct v4l2_subdev_format *fmt)
2289 {
2290 	struct ov5670 *ov5670 = to_ov5670(sd);
2291 	const struct ov5670_mode *mode;
2292 	s32 vblank_def;
2293 	s32 h_blank;
2294 
2295 	mutex_lock(&ov5670->mutex);
2296 
2297 	fmt->format.code = MEDIA_BUS_FMT_SGRBG10_1X10;
2298 
2299 	mode = v4l2_find_nearest_size(supported_modes,
2300 				      ARRAY_SIZE(supported_modes),
2301 				      width, height,
2302 				      fmt->format.width, fmt->format.height);
2303 	ov5670_update_pad_format(mode, fmt);
2304 	if (fmt->which == V4L2_SUBDEV_FORMAT_TRY) {
2305 		*v4l2_subdev_get_try_format(sd, sd_state, fmt->pad) = fmt->format;
2306 	} else {
2307 		ov5670->cur_mode = mode;
2308 		__v4l2_ctrl_s_ctrl(ov5670->link_freq, mode->link_freq_index);
2309 		__v4l2_ctrl_s_ctrl_int64(
2310 			ov5670->pixel_rate,
2311 			link_freq_configs[mode->link_freq_index].pixel_rate);
2312 		/* Update limits and set FPS to default */
2313 		vblank_def = ov5670->cur_mode->vts_def -
2314 			     ov5670->cur_mode->height;
2315 		__v4l2_ctrl_modify_range(
2316 			ov5670->vblank,
2317 			ov5670->cur_mode->vts_min - ov5670->cur_mode->height,
2318 			OV5670_VTS_MAX - ov5670->cur_mode->height, 1,
2319 			vblank_def);
2320 		__v4l2_ctrl_s_ctrl(ov5670->vblank, vblank_def);
2321 		h_blank = OV5670_FIXED_PPL - ov5670->cur_mode->width;
2322 		__v4l2_ctrl_modify_range(ov5670->hblank, h_blank, h_blank, 1,
2323 					 h_blank);
2324 	}
2325 
2326 	mutex_unlock(&ov5670->mutex);
2327 
2328 	return 0;
2329 }
2330 
2331 static int ov5670_get_skip_frames(struct v4l2_subdev *sd, u32 *frames)
2332 {
2333 	*frames = OV5670_NUM_OF_SKIP_FRAMES;
2334 
2335 	return 0;
2336 }
2337 
2338 /* Verify chip ID */
2339 static int ov5670_identify_module(struct ov5670 *ov5670)
2340 {
2341 	struct i2c_client *client = v4l2_get_subdevdata(&ov5670->sd);
2342 	int ret;
2343 	u32 val;
2344 
2345 	if (ov5670->identified)
2346 		return 0;
2347 
2348 	ret = ov5670_read_reg(ov5670, OV5670_REG_CHIP_ID,
2349 			      OV5670_REG_VALUE_24BIT, &val);
2350 	if (ret)
2351 		return ret;
2352 
2353 	if (val != OV5670_CHIP_ID) {
2354 		dev_err(&client->dev, "chip id mismatch: %x!=%x\n",
2355 			OV5670_CHIP_ID, val);
2356 		return -ENXIO;
2357 	}
2358 
2359 	ov5670->identified = true;
2360 
2361 	return 0;
2362 }
2363 
2364 /* Prepare streaming by writing default values and customized values */
2365 static int ov5670_start_streaming(struct ov5670 *ov5670)
2366 {
2367 	struct i2c_client *client = v4l2_get_subdevdata(&ov5670->sd);
2368 	const struct ov5670_reg_list *reg_list;
2369 	int link_freq_index;
2370 	int ret;
2371 
2372 	ret = ov5670_identify_module(ov5670);
2373 	if (ret)
2374 		return ret;
2375 
2376 	/* Get out of from software reset */
2377 	ret = ov5670_write_reg(ov5670, OV5670_REG_SOFTWARE_RST,
2378 			       OV5670_REG_VALUE_08BIT, OV5670_SOFTWARE_RST);
2379 	if (ret) {
2380 		dev_err(&client->dev, "%s failed to set powerup registers\n",
2381 			__func__);
2382 		return ret;
2383 	}
2384 
2385 	/* Setup PLL */
2386 	link_freq_index = ov5670->cur_mode->link_freq_index;
2387 	reg_list = &link_freq_configs[link_freq_index].reg_list;
2388 	ret = ov5670_write_reg_list(ov5670, reg_list);
2389 	if (ret) {
2390 		dev_err(&client->dev, "%s failed to set plls\n", __func__);
2391 		return ret;
2392 	}
2393 
2394 	/* Apply default values of current mode */
2395 	reg_list = &ov5670->cur_mode->reg_list;
2396 	ret = ov5670_write_reg_list(ov5670, reg_list);
2397 	if (ret) {
2398 		dev_err(&client->dev, "%s failed to set mode\n", __func__);
2399 		return ret;
2400 	}
2401 
2402 	ret = __v4l2_ctrl_handler_setup(ov5670->sd.ctrl_handler);
2403 	if (ret)
2404 		return ret;
2405 
2406 	/* Write stream on list */
2407 	ret = ov5670_write_reg(ov5670, OV5670_REG_MODE_SELECT,
2408 			       OV5670_REG_VALUE_08BIT, OV5670_MODE_STREAMING);
2409 	if (ret) {
2410 		dev_err(&client->dev, "%s failed to set stream\n", __func__);
2411 		return ret;
2412 	}
2413 
2414 	return 0;
2415 }
2416 
2417 static int ov5670_stop_streaming(struct ov5670 *ov5670)
2418 {
2419 	struct i2c_client *client = v4l2_get_subdevdata(&ov5670->sd);
2420 	int ret;
2421 
2422 	ret = ov5670_write_reg(ov5670, OV5670_REG_MODE_SELECT,
2423 			       OV5670_REG_VALUE_08BIT, OV5670_MODE_STANDBY);
2424 	if (ret)
2425 		dev_err(&client->dev, "%s failed to set stream\n", __func__);
2426 
2427 	/* Return success even if it was an error, as there is nothing the
2428 	 * caller can do about it.
2429 	 */
2430 	return 0;
2431 }
2432 
2433 static int ov5670_set_stream(struct v4l2_subdev *sd, int enable)
2434 {
2435 	struct ov5670 *ov5670 = to_ov5670(sd);
2436 	struct i2c_client *client = v4l2_get_subdevdata(sd);
2437 	int ret = 0;
2438 
2439 	mutex_lock(&ov5670->mutex);
2440 	if (ov5670->streaming == enable)
2441 		goto unlock_and_return;
2442 
2443 	if (enable) {
2444 		ret = pm_runtime_resume_and_get(&client->dev);
2445 		if (ret < 0)
2446 			goto unlock_and_return;
2447 
2448 		ret = ov5670_start_streaming(ov5670);
2449 		if (ret)
2450 			goto error;
2451 	} else {
2452 		ret = ov5670_stop_streaming(ov5670);
2453 		pm_runtime_put(&client->dev);
2454 	}
2455 	ov5670->streaming = enable;
2456 	goto unlock_and_return;
2457 
2458 error:
2459 	pm_runtime_put(&client->dev);
2460 
2461 unlock_and_return:
2462 	mutex_unlock(&ov5670->mutex);
2463 
2464 	return ret;
2465 }
2466 
2467 static int __maybe_unused ov5670_runtime_resume(struct device *dev)
2468 {
2469 	struct i2c_client *client = to_i2c_client(dev);
2470 	struct v4l2_subdev *sd = i2c_get_clientdata(client);
2471 	struct ov5670 *ov5670 = to_ov5670(sd);
2472 	unsigned long delay_us;
2473 	int ret;
2474 
2475 	ret = clk_prepare_enable(ov5670->xvclk);
2476 	if (ret)
2477 		return ret;
2478 
2479 	ret = regulator_bulk_enable(OV5670_NUM_SUPPLIES, ov5670->supplies);
2480 	if (ret) {
2481 		clk_disable_unprepare(ov5670->xvclk);
2482 		return ret;
2483 	}
2484 
2485 	gpiod_set_value_cansleep(ov5670->pwdn_gpio, 0);
2486 	gpiod_set_value_cansleep(ov5670->reset_gpio, 0);
2487 
2488 	/* 8192 * 2 clock pulses before the first SCCB transaction. */
2489 	delay_us = DIV_ROUND_UP(8192 * 2 * 1000,
2490 				DIV_ROUND_UP(OV5670_XVCLK_FREQ, 1000));
2491 	fsleep(delay_us);
2492 
2493 	return 0;
2494 }
2495 
2496 static int __maybe_unused ov5670_runtime_suspend(struct device *dev)
2497 {
2498 	struct i2c_client *client = to_i2c_client(dev);
2499 	struct v4l2_subdev *sd = i2c_get_clientdata(client);
2500 	struct ov5670 *ov5670 = to_ov5670(sd);
2501 
2502 	gpiod_set_value_cansleep(ov5670->reset_gpio, 1);
2503 	gpiod_set_value_cansleep(ov5670->pwdn_gpio, 1);
2504 	regulator_bulk_disable(OV5670_NUM_SUPPLIES, ov5670->supplies);
2505 	clk_disable_unprepare(ov5670->xvclk);
2506 
2507 	return 0;
2508 }
2509 
2510 static int __maybe_unused ov5670_suspend(struct device *dev)
2511 {
2512 	struct v4l2_subdev *sd = dev_get_drvdata(dev);
2513 	struct ov5670 *ov5670 = to_ov5670(sd);
2514 
2515 	if (ov5670->streaming)
2516 		ov5670_stop_streaming(ov5670);
2517 
2518 	return 0;
2519 }
2520 
2521 static int __maybe_unused ov5670_resume(struct device *dev)
2522 {
2523 	struct v4l2_subdev *sd = dev_get_drvdata(dev);
2524 	struct ov5670 *ov5670 = to_ov5670(sd);
2525 	int ret;
2526 
2527 	if (ov5670->streaming) {
2528 		ret = ov5670_start_streaming(ov5670);
2529 		if (ret) {
2530 			ov5670_stop_streaming(ov5670);
2531 			return ret;
2532 		}
2533 	}
2534 
2535 	return 0;
2536 }
2537 
2538 static const struct v4l2_subdev_core_ops ov5670_core_ops = {
2539 	.log_status = v4l2_ctrl_subdev_log_status,
2540 	.subscribe_event = v4l2_ctrl_subdev_subscribe_event,
2541 	.unsubscribe_event = v4l2_event_subdev_unsubscribe,
2542 };
2543 
2544 static const struct v4l2_rect *
2545 __ov5670_get_pad_crop(struct ov5670 *sensor, struct v4l2_subdev_state *state,
2546 		      unsigned int pad, enum v4l2_subdev_format_whence which)
2547 {
2548 	const struct ov5670_mode *mode = sensor->cur_mode;
2549 
2550 	switch (which) {
2551 	case V4L2_SUBDEV_FORMAT_TRY:
2552 		return v4l2_subdev_get_try_crop(&sensor->sd, state, pad);
2553 	case V4L2_SUBDEV_FORMAT_ACTIVE:
2554 		return mode->analog_crop;
2555 	}
2556 
2557 	return NULL;
2558 }
2559 
2560 static int ov5670_get_selection(struct v4l2_subdev *subdev,
2561 				struct v4l2_subdev_state *state,
2562 				struct v4l2_subdev_selection *sel)
2563 {
2564 	struct ov5670 *sensor = to_ov5670(subdev);
2565 
2566 	switch (sel->target) {
2567 	case V4L2_SEL_TGT_CROP:
2568 		mutex_lock(&sensor->mutex);
2569 		sel->r = *__ov5670_get_pad_crop(sensor, state, sel->pad,
2570 						sel->which);
2571 		mutex_unlock(&sensor->mutex);
2572 		break;
2573 	case V4L2_SEL_TGT_NATIVE_SIZE:
2574 	case V4L2_SEL_TGT_CROP_BOUNDS:
2575 		sel->r.top = 0;
2576 		sel->r.left = 0;
2577 		sel->r.width = OV5670_NATIVE_WIDTH;
2578 		sel->r.height = OV5670_NATIVE_HEIGHT;
2579 		break;
2580 	case V4L2_SEL_TGT_CROP_DEFAULT:
2581 		sel->r = ov5670_analog_crop;
2582 		break;
2583 	default:
2584 		return -EINVAL;
2585 	}
2586 
2587 	return 0;
2588 }
2589 
2590 static const struct v4l2_subdev_video_ops ov5670_video_ops = {
2591 	.s_stream = ov5670_set_stream,
2592 };
2593 
2594 static const struct v4l2_subdev_pad_ops ov5670_pad_ops = {
2595 	.init_cfg = ov5670_init_cfg,
2596 	.enum_mbus_code = ov5670_enum_mbus_code,
2597 	.get_fmt = ov5670_get_pad_format,
2598 	.set_fmt = ov5670_set_pad_format,
2599 	.enum_frame_size = ov5670_enum_frame_size,
2600 	.get_selection = ov5670_get_selection,
2601 	.set_selection = ov5670_get_selection,
2602 };
2603 
2604 static const struct v4l2_subdev_sensor_ops ov5670_sensor_ops = {
2605 	.g_skip_frames = ov5670_get_skip_frames,
2606 };
2607 
2608 static const struct v4l2_subdev_ops ov5670_subdev_ops = {
2609 	.core = &ov5670_core_ops,
2610 	.video = &ov5670_video_ops,
2611 	.pad = &ov5670_pad_ops,
2612 	.sensor = &ov5670_sensor_ops,
2613 };
2614 
2615 static const struct media_entity_operations ov5670_subdev_entity_ops = {
2616 	.link_validate = v4l2_subdev_link_validate,
2617 };
2618 
2619 static int ov5670_regulators_probe(struct ov5670 *ov5670)
2620 {
2621 	struct i2c_client *client = v4l2_get_subdevdata(&ov5670->sd);
2622 	unsigned int i;
2623 
2624 	for (i = 0; i < OV5670_NUM_SUPPLIES; i++)
2625 		ov5670->supplies[i].supply = ov5670_supply_names[i];
2626 
2627 	return devm_regulator_bulk_get(&client->dev, OV5670_NUM_SUPPLIES,
2628 				       ov5670->supplies);
2629 }
2630 
2631 static int ov5670_gpio_probe(struct ov5670 *ov5670)
2632 {
2633 	struct i2c_client *client = v4l2_get_subdevdata(&ov5670->sd);
2634 
2635 	ov5670->pwdn_gpio = devm_gpiod_get_optional(&client->dev, "powerdown",
2636 						    GPIOD_OUT_LOW);
2637 	if (IS_ERR(ov5670->pwdn_gpio))
2638 		return PTR_ERR(ov5670->pwdn_gpio);
2639 
2640 	ov5670->reset_gpio = devm_gpiod_get_optional(&client->dev, "reset",
2641 						     GPIOD_OUT_LOW);
2642 	if (IS_ERR(ov5670->reset_gpio))
2643 		return PTR_ERR(ov5670->reset_gpio);
2644 
2645 	return 0;
2646 }
2647 
2648 static int ov5670_probe(struct i2c_client *client)
2649 {
2650 	struct ov5670 *ov5670;
2651 	const char *err_msg;
2652 	u32 input_clk = 0;
2653 	bool full_power;
2654 	int ret;
2655 
2656 	ov5670 = devm_kzalloc(&client->dev, sizeof(*ov5670), GFP_KERNEL);
2657 	if (!ov5670) {
2658 		ret = -ENOMEM;
2659 		err_msg = "devm_kzalloc() error";
2660 		goto error_print;
2661 	}
2662 
2663 	ov5670->xvclk = devm_clk_get(&client->dev, NULL);
2664 	if (!IS_ERR_OR_NULL(ov5670->xvclk))
2665 		input_clk = clk_get_rate(ov5670->xvclk);
2666 	else if (PTR_ERR(ov5670->xvclk) == -ENOENT)
2667 		device_property_read_u32(&client->dev, "clock-frequency",
2668 					 &input_clk);
2669 	else
2670 		return dev_err_probe(&client->dev, PTR_ERR(ov5670->xvclk),
2671 				     "error getting clock\n");
2672 
2673 	if (input_clk != OV5670_XVCLK_FREQ) {
2674 		dev_err(&client->dev,
2675 			"Unsupported clock frequency %u\n", input_clk);
2676 		return -EINVAL;
2677 	}
2678 
2679 	/* Initialize subdev */
2680 	v4l2_i2c_subdev_init(&ov5670->sd, client, &ov5670_subdev_ops);
2681 
2682 	ret = ov5670_regulators_probe(ov5670);
2683 	if (ret) {
2684 		err_msg = "Regulators probe failed";
2685 		goto error_print;
2686 	}
2687 
2688 	ret = ov5670_gpio_probe(ov5670);
2689 	if (ret) {
2690 		err_msg = "GPIO probe failed";
2691 		goto error_print;
2692 	}
2693 
2694 	full_power = acpi_dev_state_d0(&client->dev);
2695 	if (full_power) {
2696 		ret = ov5670_runtime_resume(&client->dev);
2697 		if (ret) {
2698 			err_msg = "Power up failed";
2699 			goto error_print;
2700 		}
2701 
2702 		/* Check module identity */
2703 		ret = ov5670_identify_module(ov5670);
2704 		if (ret) {
2705 			err_msg = "ov5670_identify_module() error";
2706 			goto error_power_off;
2707 		}
2708 	}
2709 
2710 	mutex_init(&ov5670->mutex);
2711 
2712 	/* Set default mode to max resolution */
2713 	ov5670->cur_mode = &supported_modes[0];
2714 
2715 	ret = ov5670_init_controls(ov5670);
2716 	if (ret) {
2717 		err_msg = "ov5670_init_controls() error";
2718 		goto error_mutex_destroy;
2719 	}
2720 
2721 	ov5670->sd.flags |= V4L2_SUBDEV_FL_HAS_DEVNODE |
2722 			    V4L2_SUBDEV_FL_HAS_EVENTS;
2723 	ov5670->sd.entity.ops = &ov5670_subdev_entity_ops;
2724 	ov5670->sd.entity.function = MEDIA_ENT_F_CAM_SENSOR;
2725 
2726 	/* Source pad initialization */
2727 	ov5670->pad.flags = MEDIA_PAD_FL_SOURCE;
2728 	ret = media_entity_pads_init(&ov5670->sd.entity, 1, &ov5670->pad);
2729 	if (ret) {
2730 		err_msg = "media_entity_pads_init() error";
2731 		goto error_handler_free;
2732 	}
2733 
2734 	ov5670->streaming = false;
2735 
2736 	/* Set the device's state to active if it's in D0 state. */
2737 	if (full_power)
2738 		pm_runtime_set_active(&client->dev);
2739 	pm_runtime_enable(&client->dev);
2740 
2741 	/* Async register for subdev */
2742 	ret = v4l2_async_register_subdev_sensor(&ov5670->sd);
2743 	if (ret < 0) {
2744 		err_msg = "v4l2_async_register_subdev() error";
2745 		goto error_pm_disable;
2746 	}
2747 
2748 	pm_runtime_idle(&client->dev);
2749 
2750 	return 0;
2751 
2752 error_pm_disable:
2753 	pm_runtime_disable(&client->dev);
2754 
2755 	media_entity_cleanup(&ov5670->sd.entity);
2756 
2757 error_handler_free:
2758 	v4l2_ctrl_handler_free(ov5670->sd.ctrl_handler);
2759 
2760 error_mutex_destroy:
2761 	mutex_destroy(&ov5670->mutex);
2762 
2763 error_power_off:
2764 	if (full_power)
2765 		ov5670_runtime_suspend(&client->dev);
2766 
2767 error_print:
2768 	dev_err(&client->dev, "%s: %s %d\n", __func__, err_msg, ret);
2769 
2770 	return ret;
2771 }
2772 
2773 static void ov5670_remove(struct i2c_client *client)
2774 {
2775 	struct v4l2_subdev *sd = i2c_get_clientdata(client);
2776 	struct ov5670 *ov5670 = to_ov5670(sd);
2777 
2778 	v4l2_async_unregister_subdev(sd);
2779 	media_entity_cleanup(&sd->entity);
2780 	v4l2_ctrl_handler_free(sd->ctrl_handler);
2781 	mutex_destroy(&ov5670->mutex);
2782 
2783 	pm_runtime_disable(&client->dev);
2784 	ov5670_runtime_suspend(&client->dev);
2785 }
2786 
2787 static const struct dev_pm_ops ov5670_pm_ops = {
2788 	SET_SYSTEM_SLEEP_PM_OPS(ov5670_suspend, ov5670_resume)
2789 	SET_RUNTIME_PM_OPS(ov5670_runtime_suspend, ov5670_runtime_resume, NULL)
2790 };
2791 
2792 #ifdef CONFIG_ACPI
2793 static const struct acpi_device_id ov5670_acpi_ids[] = {
2794 	{ "INT3479" },
2795 	{ /* sentinel */ }
2796 };
2797 
2798 MODULE_DEVICE_TABLE(acpi, ov5670_acpi_ids);
2799 #endif
2800 
2801 static const struct of_device_id ov5670_of_ids[] = {
2802 	{ .compatible = "ovti,ov5670" },
2803 	{ /* sentinel */ }
2804 };
2805 MODULE_DEVICE_TABLE(of, ov5670_of_ids);
2806 
2807 static struct i2c_driver ov5670_i2c_driver = {
2808 	.driver = {
2809 		.name = "ov5670",
2810 		.pm = &ov5670_pm_ops,
2811 		.acpi_match_table = ACPI_PTR(ov5670_acpi_ids),
2812 		.of_match_table = ov5670_of_ids,
2813 	},
2814 	.probe_new = ov5670_probe,
2815 	.remove = ov5670_remove,
2816 	.flags = I2C_DRV_ACPI_WAIVE_D0_PROBE,
2817 };
2818 
2819 module_i2c_driver(ov5670_i2c_driver);
2820 
2821 MODULE_AUTHOR("Rapolu, Chiranjeevi <chiranjeevi.rapolu@intel.com>");
2822 MODULE_AUTHOR("Yang, Hyungwoo");
2823 MODULE_DESCRIPTION("Omnivision ov5670 sensor driver");
2824 MODULE_LICENSE("GPL v2");
2825