media: soc_camera: add OV5650 SoC camera I2C device driver
[linux-2.6.git] / drivers / media / video / ov5650.c
1 /*
2  * OmniVision OV5650 sensor driver
3  *
4  * Copyright (c) 2013, NVIDIA CORPORATION.  All rights reserved.
5  *
6  * This program is free software; you can redistribute it and/or
7  * modify it under the terms of the GNU General Public License as
8  * published by the Free Software Foundation version 2.
9  *
10  * This program is distributed "as is" WITHOUT ANY WARRANTY of any
11  * kind, whether express or implied; without even the implied warranty
12  * of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13  * GNU General Public License for more details.
14  */
15
16 #include <linux/videodev2.h>
17 #include <linux/slab.h>
18 #include <linux/i2c.h>
19 #include <linux/log2.h>
20 #include <linux/delay.h>
21 #include <linux/module.h>
22
23 #include <media/v4l2-device.h>
24 #include <media/v4l2-subdev.h>
25 #include <media/v4l2-ctrls.h>
26 #include <media/v4l2-chip-ident.h>
27 #include <media/soc_camera.h>
28
29 #include <media/ov5650.h>
30
31 #define SIZEOF_I2C_TRANSBUF 32
32
33 struct ov5650_priv {
34         struct v4l2_subdev subdev;
35         struct v4l2_mbus_framefmt mf;
36         const struct ov5650_platform_data *pdata;
37
38         int ident;
39         u16 chip_id;
40         u8 revision;
41
42         int mode;
43
44         struct i2c_client *client;
45         u8 i2c_trans_buf[SIZEOF_I2C_TRANSBUF];
46 };
47
48 static struct ov5650_priv *to_ov5650(const struct v4l2_subdev *sd)
49 {
50         return container_of(sd, struct ov5650_priv, subdev);
51 }
52
53 /**
54  * struct ov5650_reg - ov5650 register format
55  * @addr: 16-bit offset to register
56  * @val: 8/16/32-bit register value
57  *
58  * Define a structure for OV5650 register initialization values
59  */
60 struct ov5650_reg {
61         u16     addr;
62         u16     val;
63 };
64
65 #define OV5650_TABLE_WAIT_MS 0
66 #define OV5650_TABLE_END 1
67 #define OV5650_MAX_RETRIES 3
68
69 static struct ov5650_reg tp_none_seq[] = {
70         {0x5046, 0x00},
71         {OV5650_TABLE_END, 0x0000}
72 };
73
74 static struct ov5650_reg tp_cbars_seq[] = {
75         {0x503D, 0xC0},
76         {0x503E, 0x00},
77         {0x5046, 0x01},
78         {OV5650_TABLE_END, 0x0000}
79 };
80
81 static struct ov5650_reg tp_checker_seq[] = {
82         {0x503D, 0xC0},
83         {0x503E, 0x0A},
84         {0x5046, 0x01},
85         {OV5650_TABLE_END, 0x0000}
86 };
87
88 static struct ov5650_reg *test_pattern_modes[] = {
89         tp_none_seq,
90         tp_cbars_seq,
91         tp_checker_seq,
92 };
93
94 static struct ov5650_reg reset_seq[] = {
95         {0x3008, 0x82},
96         {OV5650_TABLE_WAIT_MS, 5},
97         {0x3008, 0x42},
98         {OV5650_TABLE_WAIT_MS, 5},
99         {OV5650_TABLE_END, 0x0000},
100 };
101
102 static struct ov5650_reg mode_start[] = {
103         {0x3103, 0x93},
104         {0x3017, 0xff},
105         {0x3018, 0xfc},
106
107         {0x3600, 0x50},
108         {0x3601, 0x0d},
109         {0x3604, 0x50},
110         {0x3605, 0x04},
111         {0x3606, 0x3f},
112         {0x3612, 0x1a},
113         {0x3630, 0x22},
114         {0x3631, 0x22},
115         {0x3702, 0x3a},
116         {0x3704, 0x18},
117         {0x3705, 0xda},
118         {0x3706, 0x41},
119         {0x370a, 0x80},
120         {0x370b, 0x40},
121         {0x370e, 0x00},
122         {0x3710, 0x28},
123         {0x3712, 0x13},
124         {0x3830, 0x50},
125         {0x3a18, 0x00},
126         {0x3a19, 0xf8},
127         {0x3a00, 0x38},
128
129
130         {0x3603, 0xa7},
131         {0x3615, 0x50},
132         {0x3620, 0x56},
133         {0x3810, 0x00},
134         {0x3836, 0x00},
135         {0x3a1a, 0x06},
136         {0x4000, 0x01},
137         {0x401c, 0x48},
138         {0x401d, 0x08},
139         {0x5000, 0x06},
140         {0x5001, 0x00},
141         {0x5002, 0x00},
142         {0x503d, 0x00},
143         {0x5046, 0x00},
144
145         {0x300f, 0x8f},
146
147         {0x3010, 0x10},
148         {0x3011, 0x14},
149         {0x3012, 0x02},
150         {0x3815, 0x82},
151         {0x3503, 0x00},
152         {0x3613, 0x44},
153         {OV5650_TABLE_END, 0x0},
154 };
155
156 static struct ov5650_reg mode_2592x1944[] = {
157         {0x3621, 0x2f},
158
159         {0x3632, 0x55},
160         {0x3703, 0xe6},
161         {0x370c, 0xa0},
162         {0x370d, 0x04},
163         {0x3713, 0x2f},
164         {0x3800, 0x02},
165         {0x3801, 0x58},
166         {0x3802, 0x00},
167         {0x3803, 0x0c},
168         {0x3804, 0x0a},
169         {0x3805, 0x20},
170         {0x3806, 0x07},
171         {0x3807, 0xa0},
172         {0x3808, 0x0a},
173
174         {0x3809, 0x20},
175
176         {0x380a, 0x07},
177
178         {0x380b, 0xa0},
179
180         {0x380c, 0x0c},
181
182         {0x380d, 0xb4},
183
184         {0x380e, 0x07},
185
186         {0x380f, 0xb0},
187
188         {0x3818, 0xc0},
189         {0x381a, 0x3c},
190         {0x3a0d, 0x06},
191         {0x3c01, 0x00},
192         {0x3007, 0x3f},
193         {0x5059, 0x80},
194         {0x3003, 0x03},
195         {0x3500, 0x00},
196         {0x3501, 0x7a},
197
198         {0x3502, 0xd0},
199
200         {0x350a, 0x00},
201         {0x350b, 0x00},
202         {0x401d, 0x08},
203         {0x4801, 0x0f},
204         {0x300e, 0x0c},
205         {0x4803, 0x50},
206         {0x4800, 0x34},
207         {OV5650_TABLE_END, 0x0000}
208 };
209
210 static struct ov5650_reg mode_1296x972[] = {
211         {0x3621, 0xaf},
212
213         {0x3632, 0x5a},
214         {0x3703, 0xb0},
215         {0x370c, 0xc5},
216         {0x370d, 0x42},
217         {0x3713, 0x2f},
218         {0x3800, 0x03},
219         {0x3801, 0x3c},
220         {0x3802, 0x00},
221         {0x3803, 0x06},
222         {0x3804, 0x05},
223         {0x3805, 0x10},
224         {0x3806, 0x03},
225         {0x3807, 0xd0},
226         {0x3808, 0x05},
227
228         {0x3809, 0x10},
229
230         {0x380a, 0x03},
231
232         {0x380b, 0xd0},
233
234         {0x380c, 0x08},
235
236         {0x380d, 0xa8},
237
238         {0x380e, 0x05},
239
240         {0x380f, 0xa4},
241
242         {0x3818, 0xc1},
243         {0x381a, 0x00},
244         {0x3a0d, 0x08},
245         {0x3c01, 0x00},
246         {0x3007, 0x3b},
247         {0x5059, 0x80},
248         {0x3003, 0x03},
249         {0x3500, 0x00},
250
251         {0x3501, 0x5a},
252         {0x3502, 0x10},
253         {0x350a, 0x00},
254         {0x350b, 0x10},
255         {0x401d, 0x08},
256         {0x4801, 0x0f},
257         {0x300e, 0x0c},
258         {0x4803, 0x50},
259         {0x4800, 0x34},
260         {OV5650_TABLE_END, 0x0000}
261 };
262
263 static struct ov5650_reg mode_2080x1164[] = {
264         {0x3103, 0x93},
265         {0x3007, 0x3b},
266         {0x3017, 0xff},
267         {0x3018, 0xfc},
268
269         {0x3600, 0x54},
270         {0x3601, 0x05},
271         {0x3603, 0xa7},
272         {0x3604, 0x40},
273         {0x3605, 0x04},
274         {0x3606, 0x3f},
275         {0x3612, 0x1a},
276         {0x3613, 0x44},
277         {0x3615, 0x52},
278         {0x3620, 0x56},
279         {0x3623, 0x01},
280         {0x3630, 0x22},
281         {0x3631, 0x36},
282         {0x3632, 0x5f},
283         {0x3633, 0x24},
284
285         {0x3702, 0x3a},
286         {0x3704, 0x18},
287         {0x3706, 0x41},
288         {0x370b, 0x40},
289         {0x370e, 0x00},
290         {0x3710, 0x28},
291         {0x3711, 0x24},
292         {0x3712, 0x13},
293
294         {0x3810, 0x00},
295         {0x3815, 0x82},
296         {0x3830, 0x50},
297         {0x3836, 0x00},
298
299         {0x3a1a, 0x06},
300         {0x3a18, 0x00},
301         {0x3a19, 0xf8},
302         {0x3a00, 0x38},
303
304         {0x3a0d, 0x06},
305         {0x3c01, 0x34},
306
307         {0x401f, 0x03},
308         {0x4000, 0x05},
309         {0x401d, 0x08},
310         {0x4001, 0x02},
311
312         {0x5001, 0x00},
313         {0x5002, 0x00},
314         {0x503d, 0x00},
315         {0x5046, 0x00},
316
317         {0x300f, 0x8f},
318
319         {0x3010, 0x10},
320         {0x3011, 0x14},
321         {0x3012, 0x02},
322         {0x3503, 0x00},
323
324
325         {0x3621, 0x2f},
326
327         {0x3703, 0xe6},
328         {0x370c, 0x00},
329         {0x370d, 0x04},
330         {0x3713, 0x22},
331         {0x3714, 0x27},
332         {0x3705, 0xda},
333         {0x370a, 0x80},
334
335         {0x3800, 0x02},
336         {0x3801, 0x12},
337         {0x3802, 0x00},
338         {0x3803, 0x0a},
339         {0x3804, 0x08},
340         {0x3805, 0x20},
341         {0x3806, 0x04},
342         {0x3807, 0x92},
343         {0x3808, 0x08},
344
345         {0x3809, 0x20},
346
347         {0x380a, 0x04},
348
349         {0x380b, 0x92},
350
351         {0x380c, 0x0a},
352
353         {0x380d, 0x96},
354
355         {0x380e, 0x04},
356
357         {0x380f, 0x9e},
358
359         {0x3818, 0xc0},
360         {0x381a, 0x3c},
361         {0x381c, 0x31},
362         {0x381d, 0x8e},
363         {0x381e, 0x04},
364         {0x381f, 0x92},
365         {0x3820, 0x04},
366         {0x3821, 0x19},
367         {0x3824, 0x01},
368         {0x3827, 0x0a},
369         {0x401c, 0x46},
370
371         {0x3003, 0x03},
372         {0x3500, 0x00},
373         {0x3501, 0x49},
374         {0x3502, 0xa0},
375         {0x350a, 0x00},
376         {0x350b, 0x00},
377         {0x4801, 0x0f},
378         {0x300e, 0x0c},
379         {0x4803, 0x50},
380         {0x4800, 0x34},
381
382         {OV5650_TABLE_END, 0x0000}
383 };
384
385 static struct ov5650_reg mode_1920x1080[] = {
386         {0x3103, 0x93},
387         {0x3007, 0x3b},
388         {0x3017, 0xff},
389         {0x3018, 0xfc},
390
391         {0x3600, 0x54},
392         {0x3601, 0x05},
393         {0x3603, 0xa7},
394         {0x3604, 0x40},
395         {0x3605, 0x04},
396         {0x3606, 0x3f},
397         {0x3612, 0x1a},
398         {0x3613, 0x44},
399         {0x3615, 0x52},
400         {0x3620, 0x56},
401         {0x3623, 0x01},
402         {0x3630, 0x22},
403         {0x3631, 0x36},
404         {0x3632, 0x5f},
405         {0x3633, 0x24},
406
407         {0x3702, 0x3a},
408         {0x3704, 0x18},
409         {0x3706, 0x41},
410         {0x370b, 0x40},
411         {0x370e, 0x00},
412         {0x3710, 0x28},
413         {0x3711, 0x24},
414         {0x3712, 0x13},
415
416         {0x3810, 0x00},
417         {0x3815, 0x82},
418
419         {0x3830, 0x50},
420         {0x3836, 0x00},
421
422         {0x3a1a, 0x06},
423         {0x3a18, 0x00},
424         {0x3a19, 0xf8},
425         {0x3a00, 0x38},
426         {0x3a0d, 0x06},
427         {0x3c01, 0x34},
428
429         {0x401f, 0x03},
430         {0x4000, 0x05},
431         {0x401d, 0x08},
432         {0x4001, 0x02},
433
434         {0x5001, 0x00},
435         {0x5002, 0x00},
436         {0x503d, 0x00},
437         {0x5046, 0x00},
438
439         {0x300f, 0x8f},
440         {0x3010, 0x10},
441         {0x3011, 0x14},
442         {0x3012, 0x02},
443         {0x3503, 0x00},
444
445         {0x3621, 0x2f},
446         {0x3703, 0xe6},
447         {0x370c, 0x00},
448         {0x370d, 0x04},
449         {0x3713, 0x22},
450         {0x3714, 0x27},
451         {0x3705, 0xda},
452         {0x370a, 0x80},
453
454         {0x3800, 0x02},
455         {0x3801, 0x94},
456         {0x3802, 0x00},
457         {0x3803, 0x0c},
458         {0x3804, 0x07},
459         {0x3805, 0x80},
460         {0x3806, 0x04},
461         {0x3807, 0x40},
462         {0x3808, 0x07},
463         {0x3809, 0x80},
464         {0x380a, 0x04},
465         {0x380b, 0x40},
466         {0x380c, 0x0a},
467         {0x380d, 0x84},
468         {0x380e, 0x04},
469         {0x380f, 0xa4},
470         {0x3818, 0xc0},
471         {0x381a, 0x3c},
472         {0x381c, 0x31},
473         {0x381d, 0xa4},
474         {0x381e, 0x04},
475         {0x381f, 0x60},
476         {0x3820, 0x03},
477         {0x3821, 0x1a},
478         {0x3824, 0x01},
479         {0x3827, 0x0a},
480         {0x401c, 0x46},
481
482         {0x3003, 0x03},
483         {0x3500, 0x00},
484         {0x3501, 0x49},
485         {0x3502, 0xa0},
486         {0x350a, 0x00},
487         {0x350b, 0x00},
488         {0x4801, 0x0f},
489         {0x300e, 0x0c},
490         {0x4803, 0x50},
491         {0x4800, 0x34},
492
493         {OV5650_TABLE_END, 0x0000}
494 };
495
496 static struct ov5650_reg mode_1280x720[] = {
497         {0x3103, 0x93},
498         {0x3b07, 0x0c},
499         {0x3017, 0xff},
500         {0x3018, 0xfc},
501         {0x3706, 0x41},
502         {0x3613, 0xc4},
503         {0x370d, 0x42},
504         {0x3703, 0x9a},
505         {0x3630, 0x22},
506         {0x3605, 0x04},
507         {0x3606, 0x3f},
508         {0x3712, 0x13},
509         {0x370e, 0x00},
510         {0x370b, 0x40},
511         {0x3600, 0x54},
512         {0x3601, 0x05},
513         {0x3713, 0x22},
514         {0x3714, 0x27},
515         {0x3631, 0x22},
516         {0x3612, 0x1a},
517         {0x3604, 0x40},
518         {0x3705, 0xdb},
519         {0x370a, 0x81},
520         {0x370c, 0x00},
521         {0x3710, 0x28},
522         {0x3702, 0x3a},
523         {0x3704, 0x18},
524         {0x3a18, 0x00},
525         {0x3a19, 0xf8},
526         {0x3a00, 0x38},
527         {0x3800, 0x02},
528         {0x3801, 0x54},
529         {0x3803, 0x0c},
530         {0x380c, 0x0c},
531         {0x380d, 0xb4},
532         {0x380e, 0x07},
533         {0x380f, 0xb0},
534         {0x3830, 0x50},
535         {0x3a08, 0x12},
536         {0x3a09, 0x70},
537         {0x3a0a, 0x0f},
538         {0x3a0b, 0x60},
539         {0x3a0d, 0x06},
540         {0x3a0e, 0x06},
541         {0x3a13, 0x54},
542         {0x3815, 0x82},
543         {0x5059, 0x80},
544         {0x3615, 0x52},
545         {0x505a, 0x0a},
546         {0x505b, 0x2e},
547         {0x3713, 0x92},
548         {0x3714, 0x17},
549         {0x3804, 0x05},
550         {0x3805, 0x00},
551         {0x3806, 0x02},
552         {0x3807, 0xd0},
553         {0x3808, 0x05},
554         {0x3809, 0x00},
555         {0x380a, 0x02},
556         {0x380b, 0xd0},
557         {0x380c, 0x08},
558         {0x380d, 0x72},
559         {0x380e, 0x02},
560         {0x380f, 0xe4},
561         {0x3815, 0x81},
562         {0x381c, 0x10},
563         {0x381d, 0x82},
564         {0x381e, 0x05},
565         {0x381f, 0xc0},
566         {0x3821, 0x20},
567         {0x3824, 0x23},
568         {0x3825, 0x2c},
569         {0x3826, 0x00},
570         {0x3827, 0x0c},
571         {0x3a08, 0x1b},
572         {0x3a09, 0xc0},
573         {0x3a0a, 0x17},
574         {0x3a0b, 0x20},
575         {0x3a0d, 0x01},
576         {0x3a0e, 0x01},
577         {0x3a1a, 0x06},
578         {0x3503, 0x00},
579         {0x3623, 0x01},
580         {0x3633, 0x24},
581         {0x3c01, 0x34},
582         {0x3c04, 0x28},
583         {0x3c05, 0x98},
584         {0x3c07, 0x07},
585         {0x3c09, 0xc2},
586         {0x4000, 0x05},
587         {0x401d, 0x28},
588         {0x4001, 0x02},
589         {0x401c, 0x42},
590         {0x5046, 0x09},
591         {0x3810, 0x40},
592         {0x3836, 0x41},
593         {0x505f, 0x04},
594         {0x5000, 0xfe},
595         {0x5001, 0x01},
596         {0x5002, 0x00},
597         {0x503d, 0x00},  /* bit[7]=1 enable test_pattern */
598         {0x5901, 0x00},
599         {0x585a, 0x01},
600         {0x585b, 0x2c},
601         {0x585c, 0x01},
602         {0x585d, 0x93},
603         {0x585e, 0x01},
604         {0x585f, 0x90},
605         {0x5860, 0x01},
606         {0x5861, 0x0d},
607         {0x5180, 0xc0},
608         {0x5184, 0x00},
609         {0x470a, 0x00},
610         {0x470b, 0x00},
611         {0x470c, 0x00},
612         {0x300f, 0x8e},
613         {0x3603, 0xa7},
614         {0x3632, 0x55},
615         {0x3620, 0x56},
616         {0x3621, 0xaf},
617         {0x3818, 0xc1},
618         {0x3631, 0x36},
619         {0x3632, 0x5f},
620         {0x3711, 0x24},
621         {0x401f, 0x03},
622         {0x3008, 0x02},
623
624         {0x3011, 0x14},
625         {0x3007, 0x3B},
626         {0x4801, 0x0f},
627         {0x3003, 0x03},
628         {0x300e, 0x0c},
629         {0x4803, 0x50},
630         {0x4800, 0x04},  /* bit[5]=0 as CSI continuous clock */
631         {0x300f, 0x8f},
632         {0x3010, 0x10},
633         {0x3815, 0x82},
634         {0x3003, 0x01},
635
636         {OV5650_TABLE_END, 0x0000}
637 };
638
639 static struct ov5650_reg mode_1264x704[] = {
640         {0x3600, 0x54},
641         {0x3601, 0x05},
642         {0x3604, 0x40},
643         {0x3705, 0xdb},
644         {0x370a, 0x81},
645         {0x3615, 0x52},
646         {0x3810, 0x40},
647         {0x3836, 0x41},
648         {0x4000, 0x05},
649         {0x401c, 0x42},
650         {0x401d, 0x08},
651         {0x5046, 0x09},
652         {0x3010, 0x00},
653         {0x3503, 0x00},
654         {0x3613, 0xc4},
655
656         {0x3621, 0xaf},
657
658         {0x3632, 0x55},
659         {0x3703, 0x9a},
660         {0x370c, 0x00},
661         {0x370d, 0x42},
662         {0x3713, 0x22},
663         {0x3800, 0x02},
664         {0x3801, 0x54},
665         {0x3802, 0x00},
666         {0x3803, 0x0c},
667         {0x3804, 0x05},
668         {0x3805, 0x00},
669         {0x3806, 0x02},
670         {0x3807, 0xd0},
671         {0x3808, 0x05},
672
673         {0x3809, 0x00},
674
675         {0x380a, 0x02},
676
677         {0x380b, 0xd0},
678
679         {0x380c, 0x08},
680
681         {0x380d, 0x72},
682
683         {0x380e, 0x02},
684
685         {0x380f, 0xe4},
686
687         {0x3818, 0xc1},
688         {0x381a, 0x3c},
689         {0x3a0d, 0x06},
690         {0x3c01, 0x34},
691         {0x3007, 0x3b},
692         {0x5059, 0x80},
693         {0x3003, 0x03},
694         {0x3500, 0x04},
695         {0x3501, 0xa5},
696
697         {0x3502, 0x10},
698
699         {0x350a, 0x00},
700         {0x350b, 0x00},
701         {0x4801, 0x0f},
702         {0x300e, 0x0c},
703         {0x4803, 0x50},
704         {0x4800, 0x24},
705         {0x300f, 0x8b},
706
707         {0x3711, 0x24},
708         {0x3713, 0x92},
709         {0x3714, 0x17},
710         {0x381c, 0x10},
711         {0x381d, 0x82},
712         {0x381e, 0x05},
713         {0x381f, 0xc0},
714         {0x3821, 0x20},
715         {0x3824, 0x23},
716         {0x3825, 0x2c},
717         {0x3826, 0x00},
718         {0x3827, 0x0c},
719         {0x3623, 0x01},
720         {0x3633, 0x24},
721         {0x3632, 0x5f},
722         {0x401f, 0x03},
723
724         {OV5650_TABLE_END, 0x0000}
725 };
726
727 static struct ov5650_reg mode_320x240[] = {
728         {0x3103, 0x93},
729         {0x3b07, 0x0c},
730         {0x3017, 0xff},
731         {0x3018, 0xfc},
732         {0x3706, 0x41},
733         {0x3613, 0xc4},
734         {0x370d, 0x42},
735         {0x3703, 0x9a},
736         {0x3630, 0x22},
737         {0x3605, 0x04},
738         {0x3606, 0x3f},
739         {0x3712, 0x13},
740         {0x370e, 0x00},
741         {0x370b, 0x40},
742         {0x3600, 0x54},
743         {0x3601, 0x05},
744         {0x3713, 0x22},
745         {0x3714, 0x27},
746         {0x3631, 0x22},
747         {0x3612, 0x1a},
748         {0x3604, 0x40},
749         {0x3705, 0xdc},
750         {0x370a, 0x83},
751         {0x370c, 0xc8},
752         {0x3710, 0x28},
753         {0x3702, 0x3a},
754         {0x3704, 0x18},
755         {0x3a18, 0x00},
756         {0x3a19, 0xf8},
757         {0x3a00, 0x38},
758         {0x3800, 0x02},
759         {0x3801, 0x54},
760         {0x3803, 0x0c},
761         {0x380c, 0x0c},
762         {0x380d, 0xb4},
763         {0x380e, 0x07},
764         {0x380f, 0xb0},
765         {0x3830, 0x50},
766         {0x3a08, 0x12},
767         {0x3a09, 0x70},
768         {0x3a0a, 0x0f},
769         {0x3a0b, 0x60},
770         {0x3a0d, 0x06},
771         {0x3a0e, 0x06},
772         {0x3a13, 0x54},
773         {0x3815, 0x82},
774         {0x5059, 0x80},
775         {0x3615, 0x52},
776         {0x505a, 0x0a},
777         {0x505b, 0x2e},
778         {0x3713, 0x92},
779         {0x3714, 0x17},
780         {0x3803, 0x0a},
781         {0x3804, 0x05},
782         {0x3805, 0x00},
783         {0x3806, 0x01},
784         {0x3807, 0x00},
785         {0x3808, 0x01},
786         {0x3809, 0x40},
787         {0x380a, 0x01},
788         {0x380b, 0x00},
789         {0x380c, 0x0a},
790
791         {0x380d, 0x04},
792
793         {0x380e, 0x01},
794
795         {0x380f, 0x38},
796
797         {0x3500, 0x00},
798         {0x3501, 0x13},
799         {0x3502, 0x80},
800         {0x350b, 0x7f},
801
802         {0x3815, 0x81},
803         {0x3824, 0x23},
804         {0x3825, 0x20},
805         {0x3826, 0x00},
806         {0x3827, 0x08},
807         {0x370d, 0xc2},
808         {0x3a08, 0x17},
809         {0x3a09, 0x64},
810         {0x3a0a, 0x13},
811         {0x3a0b, 0x80},
812         {0x3a00, 0x58},
813         {0x3a1a, 0x06},
814         {0x3503, 0x00},
815         {0x3623, 0x01},
816         {0x3633, 0x24},
817         {0x3c01, 0x34},
818         {0x3c04, 0x28},
819         {0x3c05, 0x98},
820         {0x3c07, 0x07},
821         {0x3c09, 0xc2},
822         {0x4000, 0x05},
823         {0x401d, 0x08},
824         {0x4001, 0x02},
825         {0x401c, 0x42},
826         {0x5046, 0x09},
827         {0x3810, 0x40},
828         {0x3836, 0x41},
829         {0x505f, 0x04},
830         {0x5001, 0x00},
831         {0x5002, 0x02},
832         {0x503d, 0x00},
833         {0x5901, 0x08},
834         {0x585a, 0x01},
835         {0x585b, 0x2c},
836         {0x585c, 0x01},
837         {0x585d, 0x93},
838         {0x585e, 0x01},
839         {0x585f, 0x90},
840         {0x5860, 0x01},
841         {0x5861, 0x0d},
842         {0x5180, 0xc0},
843         {0x5184, 0x00},
844         {0x470a, 0x00},
845         {0x470b, 0x00},
846         {0x470c, 0x00},
847         {0x300f, 0x8e},
848         {0x3603, 0xa7},
849         {0x3632, 0x55},
850         {0x3620, 0x56},
851         {0x3621, 0xaf},
852         {0x3818, 0xc3},
853         {0x3631, 0x36},
854         {0x3632, 0x5f},
855         {0x3711, 0x24},
856         {0x401f, 0x03},
857
858         {0x3011, 0x14},
859         {0x3007, 0x3B},
860         {0x300f, 0x8f},
861         {0x4801, 0x0f},
862         {0x3003, 0x03},
863         {0x300e, 0x0c},
864         {0x3010, 0x15},
865         {0x4803, 0x50},
866         {0x4800, 0x24},
867         {0x4837, 0x40},
868         {0x3815, 0x82},
869
870         {OV5650_TABLE_END, 0x0000}
871 };
872
873 static struct ov5650_reg mode_end[] = {
874         {0x3212, 0x00},
875         {0x3003, 0x01},
876         {0x3212, 0x10},
877         {0x3212, 0xa0},
878         {0x3008, 0x02},
879
880         {OV5650_TABLE_END, 0x0000}
881 };
882
883 enum {
884         OV5650_MODE_2592x1944,
885         OV5650_MODE_2080x1164,
886         OV5650_MODE_1920x1080,
887         OV5650_MODE_1296x972,
888         OV5650_MODE_1280x720,
889         OV5650_MODE_1264x704,
890         OV5650_MODE_320x240,
891         OV5650_MODE_INVALID
892 };
893
894 static struct ov5650_reg *mode_table[] = {
895         [OV5650_MODE_2592x1944] = mode_2592x1944,
896         [OV5650_MODE_2080x1164] = mode_2080x1164,
897         [OV5650_MODE_1920x1080] = mode_1920x1080,
898         [OV5650_MODE_1296x972]  = mode_1296x972,
899         [OV5650_MODE_1280x720]  = mode_1280x720,
900         [OV5650_MODE_1264x704]  = mode_1264x704,
901         [OV5650_MODE_320x240]   = mode_320x240
902 };
903
904 static const struct v4l2_frmsize_discrete ov5650_frmsizes[] = {
905         {2592, 1944},
906         {2080, 1164},
907         {1920, 1080},
908         {1296, 972},
909         {1280, 720},
910         {1264, 704},
911         {320, 240},
912 };
913
914 static int ov5650_find_mode(u32 width, u32 height)
915 {
916         if (width == 2592 && height == 1944)
917                 return OV5650_MODE_2592x1944;
918         else if (width == 2080 && height == 1164)
919                 return OV5650_MODE_2080x1164;
920         else if (width == 1920 && height == 1080)
921                 return OV5650_MODE_1920x1080;
922         else if (width == 1296 && height == 972)
923                 return OV5650_MODE_1296x972;
924         else if (width == 1280 && height == 720)
925                 return OV5650_MODE_1280x720;
926         else if (width == 1264 && height == 704)
927                 return OV5650_MODE_1264x704;
928         else if (width == 320 && height == 240)
929                 return OV5650_MODE_320x240;
930         else {
931                 pr_err("ov5650: %dx%d is not supported\n", width, height);
932                 return OV5650_MODE_2592x1944;
933         }
934 }
935
936 /**
937  * ov5650_reg_read - Read a value from a register in an ov5650 sensor device
938  * @client: i2c driver client structure
939  * @reg: register address / offset
940  * @val: stores the value that gets read
941  *
942  * Read a value from a register in an ov5650 sensor device.
943  * The value is returned in 'val'.
944  * Returns zero if successful, or non-zero otherwise.
945  */
946 static int ov5650_reg_read(struct i2c_client *client, u16 reg, u8 *val)
947 {
948         int ret;
949         u8 data[2] = {0};
950         struct i2c_msg msg = {
951                 .addr   = client->addr,
952                 .flags  = 0,
953                 .len    = 2,
954                 .buf    = data,
955         };
956
957         data[0] = (u8)(reg >> 8);
958         data[1] = (u8)(reg & 0xff);
959
960         ret = i2c_transfer(client->adapter, &msg, 1);
961         if (ret < 0)
962                 goto err;
963
964         msg.flags = I2C_M_RD;
965         msg.len = 1;
966         ret = i2c_transfer(client->adapter, &msg, 1);
967         if (ret < 0)
968                 goto err;
969
970         *val = data[0];
971         return 0;
972
973 err:
974         dev_err(&client->dev, "Failed reading register 0x%02x!\n", reg);
975         return ret;
976 }
977
978 /**
979  * Write a value to a register in ov5650 sensor device.
980  * @client: i2c driver client structure.
981  * @reg: Address of the register to read value from.
982  * @val: Value to be written to a specific register.
983  * Returns zero if successful, or non-zero otherwise.
984  */
985 static int ov5650_reg_write(struct i2c_client *client, u16 reg, u8 val)
986 {
987         int ret;
988         unsigned char data[3] = { (u8)(reg >> 8), (u8)(reg & 0xff), val };
989         struct i2c_msg msg = {
990                 .addr   = client->addr,
991                 .flags  = 0,
992                 .len    = 3,
993                 .buf    = data,
994         };
995
996         ret = i2c_transfer(client->adapter, &msg, 1);
997         if (ret < 0) {
998                 dev_err(&client->dev, "Failed writing register 0x%02x!\n", reg);
999                 return ret;
1000         }
1001
1002         return 0;
1003 }
1004
1005 static int ov5650_write_bulk_reg(struct ov5650_priv *priv, int len)
1006 {
1007         struct i2c_client *client = priv->client;
1008         u8 *data = priv->i2c_trans_buf;
1009         int err;
1010         struct i2c_msg msg;
1011
1012         if (!client->adapter)
1013                 return -ENODEV;
1014
1015         msg.addr = client->addr;
1016         msg.flags = 0;
1017         msg.len = len;
1018         msg.buf = data;
1019
1020         err = i2c_transfer(client->adapter, &msg, 1);
1021         if (err != 1) {
1022                 dev_err(&client->dev, "I2C bulk transfer failed at %x\n",
1023                         (int)data[0] << 8 | data[1]);
1024                 return err;
1025         }
1026
1027         return 0;
1028 }
1029 static int ov5650_write_table(struct ov5650_priv *priv,
1030                                 const struct ov5650_reg table[],
1031                                 const struct ov5650_reg override_list[],
1032                                 int num_override_regs)
1033 {
1034         int err;
1035         const struct ov5650_reg *next, *n_next;
1036         u8 *b_ptr = priv->i2c_trans_buf;
1037         unsigned int buf_filled = 0;
1038         unsigned int i;
1039         u16 val;
1040
1041         for (next = table; next->addr != OV5650_TABLE_END; next++) {
1042                 if (next->addr == OV5650_TABLE_WAIT_MS) {
1043                         msleep(next->val);
1044                         continue;
1045                 }
1046
1047                 val = next->val;
1048                 /* When an override list is passed in, replace the reg */
1049                 /* value to write if the reg is in the list            */
1050                 if (override_list) {
1051                         for (i = 0; i < num_override_regs; i++) {
1052                                 if (next->addr == override_list[i].addr) {
1053                                         val = override_list[i].val;
1054                                         break;
1055                                 }
1056                         }
1057                 }
1058
1059                 if (!buf_filled) {
1060                         b_ptr = priv->i2c_trans_buf;
1061                         *b_ptr++ = next->addr >> 8;
1062                         *b_ptr++ = next->addr & 0xff;
1063                         buf_filled = 2;
1064                 }
1065                 *b_ptr++ = val;
1066                 buf_filled++;
1067
1068                 n_next = next + 1;
1069                 if (n_next->addr != OV5650_TABLE_END &&
1070                         n_next->addr != OV5650_TABLE_WAIT_MS &&
1071                         buf_filled < SIZEOF_I2C_TRANSBUF &&
1072                         n_next->addr == next->addr + 1) {
1073                         continue;
1074                 }
1075
1076                 err = ov5650_write_bulk_reg(priv, buf_filled);
1077                 if (err)
1078                         return err;
1079
1080                 buf_filled = 0;
1081         }
1082         return 0;
1083 }
1084
1085 /* -----------------------------------------------------------------------------
1086  * V4L2 subdev internal operations
1087  */
1088
1089 static void ov5650_set_default_fmt(struct ov5650_priv *priv)
1090 {
1091         struct v4l2_mbus_framefmt *mf = &priv->mf;
1092
1093         mf->width = ov5650_frmsizes[OV5650_MODE_2592x1944].width;
1094         mf->height = ov5650_frmsizes[OV5650_MODE_2592x1944].height;
1095         mf->code = V4L2_MBUS_FMT_SGRBG10_1X10;
1096         mf->field = V4L2_FIELD_NONE;
1097         mf->colorspace = V4L2_COLORSPACE_SRGB;
1098 }
1099
1100 static int ov5650_s_stream(struct v4l2_subdev *sd, int enable)
1101 {
1102         struct ov5650_priv *priv = to_ov5650(sd);
1103         struct ov5650_reg reg_list[6];
1104         int ret = 0;
1105
1106         if (enable) {
1107                 ret = ov5650_write_table(priv, reset_seq, NULL, 0);
1108                 if (ret)
1109                         return ret;
1110
1111                 ret = ov5650_write_table(priv, mode_start, NULL, 0);
1112                 if (ret)
1113                         return ret;
1114
1115                 ret = ov5650_write_table(priv, mode_table[priv->mode],
1116                                 reg_list, 6);
1117                 if (ret)
1118                         return ret;
1119
1120                 ret = ov5650_write_table(priv, mode_end, NULL, 0);
1121                 if (ret)
1122                         return ret;
1123         } else
1124                 ov5650_set_default_fmt(priv);
1125
1126         return ret;
1127 }
1128
1129 /* Alter bus settings on camera side */
1130 static int ov5650_set_bus_param(struct soc_camera_device *icd,
1131                 unsigned long flags)
1132 {
1133         return 0;
1134 }
1135
1136 /* Request bus settings on camera side */
1137 static unsigned long ov5650_query_bus_param(struct soc_camera_device *icd)
1138 {
1139         struct soc_camera_link *icl = to_soc_camera_link(icd);
1140
1141         unsigned long flags = SOCAM_PCLK_SAMPLE_RISING | SOCAM_MASTER |
1142                 SOCAM_VSYNC_ACTIVE_HIGH | SOCAM_HSYNC_ACTIVE_HIGH |
1143                 SOCAM_DATA_ACTIVE_HIGH | SOCAM_DATAWIDTH_10;
1144
1145         return soc_camera_apply_sensor_flags(icl, flags);
1146 }
1147
1148 static int ov5650_s_fmt(struct v4l2_subdev *sd,
1149                         struct v4l2_mbus_framefmt *mf)
1150 {
1151         struct ov5650_priv *priv = to_ov5650(sd);
1152
1153         priv->mode = ov5650_find_mode(mf->width, mf->height);
1154         memcpy(&priv->mf, mf, sizeof(struct v4l2_mbus_framefmt));
1155
1156         return 0;
1157 }
1158
1159 static int ov5650_s_power(struct v4l2_subdev *sd, int on)
1160 {
1161         struct ov5650_priv *priv = to_ov5650(sd);
1162
1163         if (on) {
1164                 ov5650_s_fmt(sd, &priv->mf);
1165                 ov5650_s_stream(sd, 1);
1166         } else
1167                 ov5650_s_stream(sd, 0);
1168
1169         return 0;
1170 }
1171
1172 static int ov5650_try_fmt(struct v4l2_subdev *sd,
1173                           struct v4l2_mbus_framefmt *mf)
1174 {
1175         int mode;
1176
1177         mode = ov5650_find_mode(mf->width, mf->height);
1178         mf->width = ov5650_frmsizes[mode].width;
1179         mf->height = ov5650_frmsizes[mode].height;
1180
1181         if (mf->code != V4L2_MBUS_FMT_SGRBG8_1X8 &&
1182                 mf->code != V4L2_MBUS_FMT_SGRBG10_1X10)
1183                 mf->code = V4L2_MBUS_FMT_SGRBG10_1X10;
1184
1185         mf->field = V4L2_FIELD_NONE;
1186         mf->colorspace = V4L2_COLORSPACE_SRGB;
1187
1188         ov5650_s_fmt(sd, mf);
1189
1190         return 0;
1191 }
1192
1193 static int ov5650_enum_fmt(struct v4l2_subdev *sd, unsigned int index,
1194                            enum v4l2_mbus_pixelcode *code)
1195 {
1196         if (index >= 2)
1197                 return -EINVAL;
1198
1199         switch (index) {
1200         case 0:
1201                 *code = V4L2_MBUS_FMT_SGRBG10_1X10;
1202                 break;
1203         case 1:
1204                 *code = V4L2_MBUS_FMT_SGRBG8_1X8;
1205                 break;
1206         }
1207
1208         return 0;
1209 }
1210
1211 static int ov5650_cropcap(struct v4l2_subdev *sd, struct v4l2_cropcap *a)
1212 {
1213         a->bounds.left          = 0;
1214         a->bounds.top           = 0;
1215         a->bounds.width         = ov5650_frmsizes[OV5650_MODE_2592x1944].width;
1216         a->bounds.height        = ov5650_frmsizes[OV5650_MODE_2592x1944].height;
1217         a->defrect              = a->bounds;
1218         a->type                 = V4L2_BUF_TYPE_VIDEO_CAPTURE;
1219         a->pixelaspect.numerator        = 1;
1220         a->pixelaspect.denominator      = 1;
1221
1222         return 0;
1223 }
1224
1225 static int ov5650_g_crop(struct v4l2_subdev *sd, struct v4l2_crop *a)
1226 {
1227         a->c.left               = 0;
1228         a->c.top                = 0;
1229         a->c.width              = ov5650_frmsizes[OV5650_MODE_2592x1944].width;
1230         a->c.height             = ov5650_frmsizes[OV5650_MODE_2592x1944].height;
1231         a->type                 = V4L2_BUF_TYPE_VIDEO_CAPTURE;
1232
1233         return 0;
1234 }
1235
1236 /* Get chip identification */
1237 static int ov5650_g_chip_ident(struct v4l2_subdev *sd,
1238                                struct v4l2_dbg_chip_ident *id)
1239 {
1240         struct ov5650_priv *priv = to_ov5650(sd);
1241
1242         id->ident = priv->ident;
1243         id->revision = priv->revision;
1244
1245         return 0;
1246 }
1247
1248 static struct soc_camera_ops ov5650_ops = {
1249         .set_bus_param          = ov5650_set_bus_param,
1250         .query_bus_param        = ov5650_query_bus_param,
1251 };
1252
1253 static struct v4l2_subdev_video_ops ov5650_video_ops = {
1254         .s_stream               = ov5650_s_stream,
1255         .s_mbus_fmt             = ov5650_s_fmt,
1256         .try_mbus_fmt           = ov5650_try_fmt,
1257         .enum_mbus_fmt          = ov5650_enum_fmt,
1258         .cropcap                = ov5650_cropcap,
1259         .g_crop                 = ov5650_g_crop,
1260 };
1261
1262 static struct v4l2_subdev_core_ops ov5650_core_ops = {
1263         .g_chip_ident           = ov5650_g_chip_ident,
1264         .s_power                = ov5650_s_power,
1265 };
1266
1267 static struct v4l2_subdev_ops ov5650_subdev_ops = {
1268         .core                   = &ov5650_core_ops,
1269         .video                  = &ov5650_video_ops,
1270 };
1271
1272 static int ov5650_probe(struct i2c_client *client,
1273                          const struct i2c_device_id *did)
1274 {
1275         struct ov5650_priv *priv;
1276         struct soc_camera_device *icd   = client->dev.platform_data;
1277         struct soc_camera_link *icl;
1278         u8 chipid[2];
1279         int ret;
1280
1281         /* Checking soc-camera interface */
1282         if (!icd) {
1283                 dev_err(&client->dev, "Missing soc-camera data!\n");
1284                 return -EINVAL;
1285         }
1286
1287         icl = to_soc_camera_link(icd);
1288         if (!icl) {
1289                 dev_err(&client->dev, "Missing platform_data for driver\n");
1290                 return -EINVAL;
1291         }
1292
1293         /* Register OV5650 soc_camera device interface */
1294         priv = kzalloc(sizeof(struct ov5650_priv), GFP_KERNEL);
1295         if (!priv) {
1296                 dev_err(&client->dev, "Failed to allocate private data!\n");
1297                 return -ENOMEM;
1298         }
1299
1300         v4l2_i2c_subdev_init(&priv->subdev, client, &ov5650_subdev_ops);
1301         icd->ops = &ov5650_ops;
1302
1303         priv->subdev.flags |= V4L2_SUBDEV_FL_HAS_DEVNODE;
1304
1305         priv->ident = V4L2_IDENT_OV5650;
1306
1307         ret = ov5650_reg_read(client, 0x300A, &chipid[0]);
1308         if (ret) {
1309                 dev_err(&client->dev, "Failure to read Chip ID (high byte)\n");
1310                 goto err;
1311         }
1312
1313         ret = ov5650_reg_read(client, 0x300B, &chipid[1]);
1314         if (ret) {
1315                 dev_err(&client->dev, "Failure to read Chip ID (low byte)\n");
1316                 goto err;
1317         }
1318
1319         if ((chipid[0] != 0x56) || ((chipid[1] & 0x51) != chipid[1])) {
1320                 dev_err(&client->dev, "Chip ID: %x%x not supported!\n",
1321                         chipid[0], chipid[1]);
1322                 ret = -ENODEV;
1323                 goto err;
1324         }
1325
1326         priv->chip_id = (chipid[0] << 8) | chipid[1];
1327         priv->revision = (chipid[1] == 0x50) ? 0x1A : 0x1B;
1328
1329         priv->client = client;
1330
1331         ov5650_set_default_fmt(priv);
1332
1333         dev_info(&client->dev, "Detected a OV%x chip, revision %x\n",
1334                         priv->chip_id, priv->revision);
1335
1336         return 0;
1337
1338 err:
1339         kfree(priv);
1340
1341         return ret;
1342 }
1343
1344 static int ov5650_remove(struct i2c_client *client)
1345 {
1346         struct ov5650_priv *priv = i2c_get_clientdata(client);
1347
1348         kfree(priv);
1349         return 0;
1350 }
1351
1352 static const struct i2c_device_id ov5650_id[] = {
1353         { "ov5650", 0 },
1354         { }
1355 };
1356 MODULE_DEVICE_TABLE(i2c, ov5650_id);
1357
1358 static struct i2c_driver ov5650_i2c_driver = {
1359         .driver = {
1360                 .name = "ov5650",
1361         },
1362         .probe          = ov5650_probe,
1363         .remove         = ov5650_remove,
1364         .id_table       = ov5650_id,
1365 };
1366
1367 static int __init ov5650_module_init(void)
1368 {
1369         return i2c_add_driver(&ov5650_i2c_driver);
1370 }
1371
1372 static void __exit ov5650_module_exit(void)
1373 {
1374         i2c_del_driver(&ov5650_i2c_driver);
1375 }
1376
1377 module_init(ov5650_module_init);
1378 module_exit(ov5650_module_exit);
1379
1380 MODULE_DESCRIPTION("OmniVision OV5650 Camera driver");
1381 MODULE_AUTHOR("Bryan Wu <pengw@nvidia.com>");
1382 MODULE_LICENSE("GPL v2");