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