Revert "Merge commit 'main-jb-2012.08.03-B4' into t114-0806"
[linux-2.6.git] / drivers / media / video / tegra / ov5650.c
1 /*
2  * ov5650.c - ov5650 sensor driver
3  *
4  * Copyright (C) 2011 Google Inc.
5  *
6  * Contributors:
7  *      Rebecca Schultz Zavin <rebecca@android.com>
8  *
9  * Leverage OV9640.c
10  *
11  * This file is licensed under the terms of the GNU General Public License
12  * version 2. This program is licensed "as is" without any warranty of any
13  * kind, whether express or implied.
14  */
15
16 #include <linux/delay.h>
17 #include <linux/fs.h>
18 #include <linux/i2c.h>
19 #include <linux/miscdevice.h>
20 #include <linux/slab.h>
21 #include <linux/uaccess.h>
22 #include <linux/module.h>
23
24 #include <media/ov5650.h>
25 #include <media/tegra_camera.h>
26
27 #define SIZEOF_I2C_TRANSBUF 32
28
29 struct ov5650_reg {
30         u16 addr;
31         u16 val;
32 };
33
34 struct ov5650_sensor {
35         struct i2c_client *i2c_client;
36         struct ov5650_platform_data *pdata;
37 };
38
39 struct ov5650_info {
40         int mode;
41         enum StereoCameraMode camera_mode;
42         struct ov5650_sensor left;
43         struct ov5650_sensor right;
44         struct ov5650_sensordata sensor_data;
45         struct mutex mutex_le;
46         struct mutex mutex_ri;
47         int power_refcnt_le;
48         int power_refcnt_ri;
49         u8 i2c_trans_buf[SIZEOF_I2C_TRANSBUF];
50 };
51
52 static struct ov5650_info *stereo_ov5650_info;
53
54 #define OV5650_TABLE_WAIT_MS 0
55 #define OV5650_TABLE_END 1
56 #define OV5650_MAX_RETRIES 3
57
58 static struct ov5650_reg tp_none_seq[] = {
59         {0x5046, 0x00},
60         {OV5650_TABLE_END, 0x0000}
61 };
62
63 static struct ov5650_reg tp_cbars_seq[] = {
64         {0x503D, 0xC0},
65         {0x503E, 0x00},
66         {0x5046, 0x01},
67         {OV5650_TABLE_END, 0x0000}
68 };
69
70 static struct ov5650_reg tp_checker_seq[] = {
71         {0x503D, 0xC0},
72         {0x503E, 0x0A},
73         {0x5046, 0x01},
74         {OV5650_TABLE_END, 0x0000}
75 };
76
77 static struct ov5650_reg *test_pattern_modes[] = {
78         tp_none_seq,
79         tp_cbars_seq,
80         tp_checker_seq,
81 };
82
83 static struct ov5650_reg reset_seq[] = {
84         {0x3008, 0x82},
85         {OV5650_TABLE_WAIT_MS, 5},
86         {0x3008, 0x42},
87         {OV5650_TABLE_WAIT_MS, 5},
88         {OV5650_TABLE_END, 0x0000},
89 };
90
91 static struct ov5650_reg mode_start[] = {
92         {0x3103, 0x93},
93         {0x3017, 0xff},
94         {0x3018, 0xfc},
95
96         {0x3600, 0x50},
97         {0x3601, 0x0d},
98         {0x3604, 0x50},
99         {0x3605, 0x04},
100         {0x3606, 0x3f},
101         {0x3612, 0x1a},
102         {0x3630, 0x22},
103         {0x3631, 0x22},
104         {0x3702, 0x3a},
105         {0x3704, 0x18},
106         {0x3705, 0xda},
107         {0x3706, 0x41},
108         {0x370a, 0x80},
109         {0x370b, 0x40},
110         {0x370e, 0x00},
111         {0x3710, 0x28},
112         {0x3712, 0x13},
113         {0x3830, 0x50},
114         {0x3a18, 0x00},
115         {0x3a19, 0xf8},
116         {0x3a00, 0x38},
117
118
119         {0x3603, 0xa7},
120         {0x3615, 0x50},
121         {0x3620, 0x56},
122         {0x3810, 0x00},
123         {0x3836, 0x00},
124         {0x3a1a, 0x06},
125         {0x4000, 0x01},
126         {0x401c, 0x48},
127         {0x401d, 0x08},
128         {0x5000, 0x06},
129         {0x5001, 0x00},
130         {0x5002, 0x00},
131         {0x503d, 0x00},
132         {0x5046, 0x00},
133
134         {0x300f, 0x8f},
135
136         {0x3010, 0x10},
137         {0x3011, 0x14},
138         {0x3012, 0x02},
139         {0x3815, 0x82},
140         {0x3503, 0x33},
141         {0x3613, 0x44},
142         {OV5650_TABLE_END, 0x0},
143 };
144
145 static struct ov5650_reg mode_2592x1944[] = {
146         {0x3621, 0x2f},
147
148         {0x3632, 0x55},
149         {0x3703, 0xe6},
150         {0x370c, 0xa0},
151         {0x370d, 0x04},
152         {0x3713, 0x2f},
153         {0x3800, 0x02},
154         {0x3801, 0x58},
155         {0x3802, 0x00},
156         {0x3803, 0x0c},
157         {0x3804, 0x0a},
158         {0x3805, 0x20},
159         {0x3806, 0x07},
160         {0x3807, 0xa0},
161         {0x3808, 0x0a},
162
163         {0x3809, 0x20},
164
165         {0x380a, 0x07},
166
167         {0x380b, 0xa0},
168
169         {0x380c, 0x0c},
170
171         {0x380d, 0xb4},
172
173         {0x380e, 0x07},
174
175         {0x380f, 0xb0},
176
177         {0x3818, 0xc0},
178         {0x381a, 0x3c},
179         {0x3a0d, 0x06},
180         {0x3c01, 0x00},
181         {0x3007, 0x3f},
182         {0x5059, 0x80},
183         {0x3003, 0x03},
184         {0x3500, 0x00},
185         {0x3501, 0x7a},
186
187         {0x3502, 0xd0},
188
189         {0x350a, 0x00},
190         {0x350b, 0x00},
191         {0x401d, 0x08},
192         {0x4801, 0x0f},
193         {0x300e, 0x0c},
194         {0x4803, 0x50},
195         {0x4800, 0x34},
196         {OV5650_TABLE_END, 0x0000}
197 };
198
199 static struct ov5650_reg mode_1296x972[] = {
200         {0x3621, 0xaf},
201
202         {0x3632, 0x5a},
203         {0x3703, 0xb0},
204         {0x370c, 0xc5},
205         {0x370d, 0x42},
206         {0x3713, 0x2f},
207         {0x3800, 0x03},
208         {0x3801, 0x3c},
209         {0x3802, 0x00},
210         {0x3803, 0x06},
211         {0x3804, 0x05},
212         {0x3805, 0x10},
213         {0x3806, 0x03},
214         {0x3807, 0xd0},
215         {0x3808, 0x05},
216
217         {0x3809, 0x10},
218
219         {0x380a, 0x03},
220
221         {0x380b, 0xd0},
222
223         {0x380c, 0x08},
224
225         {0x380d, 0xa8},
226
227         {0x380e, 0x05},
228
229         {0x380f, 0xa4},
230
231         {0x3818, 0xc1},
232         {0x381a, 0x00},
233         {0x3a0d, 0x08},
234         {0x3c01, 0x00},
235         {0x3007, 0x3b},
236         {0x5059, 0x80},
237         {0x3003, 0x03},
238         {0x3500, 0x00},
239
240         {0x3501, 0x5a},
241         {0x3502, 0x10},
242         {0x350a, 0x00},
243         {0x350b, 0x10},
244         {0x401d, 0x08},
245         {0x4801, 0x0f},
246         {0x300e, 0x0c},
247         {0x4803, 0x50},
248         {0x4800, 0x34},
249         {OV5650_TABLE_END, 0x0000}
250 };
251
252 static struct ov5650_reg mode_2080x1164[] = {
253         {0x3103, 0x93},
254         {0x3007, 0x3b},
255         {0x3017, 0xff},
256         {0x3018, 0xfc},
257
258         {0x3600, 0x54},
259         {0x3601, 0x05},
260         {0x3603, 0xa7},
261         {0x3604, 0x40},
262         {0x3605, 0x04},
263         {0x3606, 0x3f},
264         {0x3612, 0x1a},
265         {0x3613, 0x44},
266         {0x3615, 0x52},
267         {0x3620, 0x56},
268         {0x3623, 0x01},
269         {0x3630, 0x22},
270         {0x3631, 0x36},
271         {0x3632, 0x5f},
272         {0x3633, 0x24},
273
274         {0x3702, 0x3a},
275         {0x3704, 0x18},
276         {0x3706, 0x41},
277         {0x370b, 0x40},
278         {0x370e, 0x00},
279         {0x3710, 0x28},
280         {0x3711, 0x24},
281         {0x3712, 0x13},
282
283         {0x3810, 0x00},
284         {0x3815, 0x82},
285         {0x3830, 0x50},
286         {0x3836, 0x00},
287
288         {0x3a1a, 0x06},
289         {0x3a18, 0x00},
290         {0x3a19, 0xf8},
291         {0x3a00, 0x38},
292
293         {0x3a0d, 0x06},
294         {0x3c01, 0x34},
295
296         {0x401f, 0x03},
297         {0x4000, 0x05},
298         {0x401d, 0x08},
299         {0x4001, 0x02},
300
301         {0x5001, 0x00},
302         {0x5002, 0x00},
303         {0x503d, 0x00},
304         {0x5046, 0x00},
305
306         {0x300f, 0x8f},
307
308         {0x3010, 0x10},
309         {0x3011, 0x14},
310         {0x3012, 0x02},
311         {0x3503, 0x33},
312
313
314         {0x3621, 0x2f},
315
316         {0x3703, 0xe6},
317         {0x370c, 0x00},
318         {0x370d, 0x04},
319         {0x3713, 0x22},
320         {0x3714, 0x27},
321         {0x3705, 0xda},
322         {0x370a, 0x80},
323
324         {0x3800, 0x02},
325         {0x3801, 0x12},
326         {0x3802, 0x00},
327         {0x3803, 0x0a},
328         {0x3804, 0x08},
329         {0x3805, 0x20},
330         {0x3806, 0x04},
331         {0x3807, 0x92},
332         {0x3808, 0x08},
333
334         {0x3809, 0x20},
335
336         {0x380a, 0x04},
337
338         {0x380b, 0x92},
339
340         {0x380c, 0x0a},
341
342         {0x380d, 0x96},
343
344         {0x380e, 0x04},
345
346         {0x380f, 0x9e},
347
348         {0x3818, 0xc0},
349         {0x381a, 0x3c},
350         {0x381c, 0x31},
351         {0x381d, 0x8e},
352         {0x381e, 0x04},
353         {0x381f, 0x92},
354         {0x3820, 0x04},
355         {0x3821, 0x19},
356         {0x3824, 0x01},
357         {0x3827, 0x0a},
358         {0x401c, 0x46},
359
360         {0x3003, 0x03},
361         {0x3500, 0x00},
362         {0x3501, 0x49},
363         {0x3502, 0xa0},
364         {0x350a, 0x00},
365         {0x350b, 0x00},
366         {0x4801, 0x0f},
367         {0x300e, 0x0c},
368         {0x4803, 0x50},
369         {0x4800, 0x34},
370
371         {OV5650_TABLE_END, 0x0000}
372 };
373
374 static struct ov5650_reg mode_1920x1080[] = {
375         {0x3103, 0x93},
376         {0x3007, 0x3b},
377         {0x3017, 0xff},
378         {0x3018, 0xfc},
379
380         {0x3600, 0x54},
381         {0x3601, 0x05},
382         {0x3603, 0xa7},
383         {0x3604, 0x40},
384         {0x3605, 0x04},
385         {0x3606, 0x3f},
386         {0x3612, 0x1a},
387         {0x3613, 0x44},
388         {0x3615, 0x52},
389         {0x3620, 0x56},
390         {0x3623, 0x01},
391         {0x3630, 0x22},
392         {0x3631, 0x36},
393         {0x3632, 0x5f},
394         {0x3633, 0x24},
395
396         {0x3702, 0x3a},
397         {0x3704, 0x18},
398         {0x3706, 0x41},
399         {0x370b, 0x40},
400         {0x370e, 0x00},
401         {0x3710, 0x28},
402         {0x3711, 0x24},
403         {0x3712, 0x13},
404
405         {0x3810, 0x00},
406         {0x3815, 0x82},
407
408         {0x3830, 0x50},
409         {0x3836, 0x00},
410
411         {0x3a1a, 0x06},
412         {0x3a18, 0x00},
413         {0x3a19, 0xf8},
414         {0x3a00, 0x38},
415         {0x3a0d, 0x06},
416         {0x3c01, 0x34},
417
418         {0x401f, 0x03},
419         {0x4000, 0x05},
420         {0x401d, 0x08},
421         {0x4001, 0x02},
422
423         {0x5001, 0x00},
424         {0x5002, 0x00},
425         {0x503d, 0x00},
426         {0x5046, 0x00},
427
428         {0x300f, 0x8f},
429         {0x3010, 0x10},
430         {0x3011, 0x14},
431         {0x3012, 0x02},
432         {0x3503, 0x33},
433
434         {0x3621, 0x2f},
435         {0x3703, 0xe6},
436         {0x370c, 0x00},
437         {0x370d, 0x04},
438         {0x3713, 0x22},
439         {0x3714, 0x27},
440         {0x3705, 0xda},
441         {0x370a, 0x80},
442
443         {0x3800, 0x02},
444         {0x3801, 0x94},
445         {0x3802, 0x00},
446         {0x3803, 0x0c},
447         {0x3804, 0x07},
448         {0x3805, 0x80},
449         {0x3806, 0x04},
450         {0x3807, 0x40},
451         {0x3808, 0x07},
452         {0x3809, 0x80},
453         {0x380a, 0x04},
454         {0x380b, 0x40},
455         {0x380c, 0x0a},
456         {0x380d, 0x84},
457         {0x380e, 0x04},
458         {0x380f, 0xa4},
459         {0x3818, 0xc0},
460         {0x381a, 0x3c},
461         {0x381c, 0x31},
462         {0x381d, 0xa4},
463         {0x381e, 0x04},
464         {0x381f, 0x60},
465         {0x3820, 0x03},
466         {0x3821, 0x1a},
467         {0x3824, 0x01},
468         {0x3827, 0x0a},
469         {0x401c, 0x46},
470
471         {0x3003, 0x03},
472         {0x3500, 0x00},
473         {0x3501, 0x49},
474         {0x3502, 0xa0},
475         {0x350a, 0x00},
476         {0x350b, 0x00},
477         {0x4801, 0x0f},
478         {0x300e, 0x0c},
479         {0x4803, 0x50},
480         {0x4800, 0x34},
481
482         {OV5650_TABLE_END, 0x0000}
483 };
484
485
486 static struct ov5650_reg mode_1264x704[] = {
487         {0x3600, 0x54},
488         {0x3601, 0x05},
489         {0x3604, 0x40},
490         {0x3705, 0xdb},
491         {0x370a, 0x81},
492         {0x3615, 0x52},
493         {0x3810, 0x40},
494         {0x3836, 0x41},
495         {0x4000, 0x05},
496         {0x401c, 0x42},
497         {0x401d, 0x08},
498         {0x5046, 0x09},
499         {0x3010, 0x00},
500         {0x3503, 0x00},
501         {0x3613, 0xc4},
502
503         {0x3621, 0xaf},
504
505         {0x3632, 0x55},
506         {0x3703, 0x9a},
507         {0x370c, 0x00},
508         {0x370d, 0x42},
509         {0x3713, 0x22},
510         {0x3800, 0x02},
511         {0x3801, 0x54},
512         {0x3802, 0x00},
513         {0x3803, 0x0c},
514         {0x3804, 0x05},
515         {0x3805, 0x00},
516         {0x3806, 0x02},
517         {0x3807, 0xd0},
518         {0x3808, 0x05},
519
520         {0x3809, 0x00},
521
522         {0x380a, 0x02},
523
524         {0x380b, 0xd0},
525
526         {0x380c, 0x08},
527
528         {0x380d, 0x72},
529
530         {0x380e, 0x02},
531
532         {0x380f, 0xe4},
533
534         {0x3818, 0xc1},
535         {0x381a, 0x3c},
536         {0x3a0d, 0x06},
537         {0x3c01, 0x34},
538         {0x3007, 0x3b},
539         {0x5059, 0x80},
540         {0x3003, 0x03},
541         {0x3500, 0x04},
542         {0x3501, 0xa5},
543
544         {0x3502, 0x10},
545
546         {0x350a, 0x00},
547         {0x350b, 0x00},
548         {0x4801, 0x0f},
549         {0x300e, 0x0c},
550         {0x4803, 0x50},
551         {0x4800, 0x24},
552         {0x300f, 0x8b},
553
554         {0x3711, 0x24},
555         {0x3713, 0x92},
556         {0x3714, 0x17},
557         {0x381c, 0x10},
558         {0x381d, 0x82},
559         {0x381e, 0x05},
560         {0x381f, 0xc0},
561         {0x3821, 0x20},
562         {0x3824, 0x23},
563         {0x3825, 0x2c},
564         {0x3826, 0x00},
565         {0x3827, 0x0c},
566         {0x3623, 0x01},
567         {0x3633, 0x24},
568         {0x3632, 0x5f},
569         {0x401f, 0x03},
570
571         {OV5650_TABLE_END, 0x0000}
572 };
573
574 static struct ov5650_reg mode_320x240[] = {
575         {0x3103, 0x93},
576         {0x3b07, 0x0c},
577         {0x3017, 0xff},
578         {0x3018, 0xfc},
579         {0x3706, 0x41},
580         {0x3613, 0xc4},
581         {0x370d, 0x42},
582         {0x3703, 0x9a},
583         {0x3630, 0x22},
584         {0x3605, 0x04},
585         {0x3606, 0x3f},
586         {0x3712, 0x13},
587         {0x370e, 0x00},
588         {0x370b, 0x40},
589         {0x3600, 0x54},
590         {0x3601, 0x05},
591         {0x3713, 0x22},
592         {0x3714, 0x27},
593         {0x3631, 0x22},
594         {0x3612, 0x1a},
595         {0x3604, 0x40},
596         {0x3705, 0xdc},
597         {0x370a, 0x83},
598         {0x370c, 0xc8},
599         {0x3710, 0x28},
600         {0x3702, 0x3a},
601         {0x3704, 0x18},
602         {0x3a18, 0x00},
603         {0x3a19, 0xf8},
604         {0x3a00, 0x38},
605         {0x3800, 0x02},
606         {0x3801, 0x54},
607         {0x3803, 0x0c},
608         {0x380c, 0x0c},
609         {0x380d, 0xb4},
610         {0x380e, 0x07},
611         {0x380f, 0xb0},
612         {0x3830, 0x50},
613         {0x3a08, 0x12},
614         {0x3a09, 0x70},
615         {0x3a0a, 0x0f},
616         {0x3a0b, 0x60},
617         {0x3a0d, 0x06},
618         {0x3a0e, 0x06},
619         {0x3a13, 0x54},
620         {0x3815, 0x82},
621         {0x5059, 0x80},
622         {0x3615, 0x52},
623         {0x505a, 0x0a},
624         {0x505b, 0x2e},
625         {0x3713, 0x92},
626         {0x3714, 0x17},
627         {0x3803, 0x0a},
628         {0x3804, 0x05},
629         {0x3805, 0x00},
630         {0x3806, 0x01},
631         {0x3807, 0x00},
632         {0x3808, 0x01},
633         {0x3809, 0x40},
634         {0x380a, 0x01},
635         {0x380b, 0x00},
636         {0x380c, 0x0a},
637
638         {0x380d, 0x04},
639
640         {0x380e, 0x01},
641
642         {0x380f, 0x38},
643
644         {0x3815, 0x81},
645         {0x3824, 0x23},
646         {0x3825, 0x20},
647         {0x3826, 0x00},
648         {0x3827, 0x08},
649         {0x370d, 0xc2},
650         {0x3a08, 0x17},
651         {0x3a09, 0x64},
652         {0x3a0a, 0x13},
653         {0x3a0b, 0x80},
654         {0x3a00, 0x58},
655         {0x3a1a, 0x06},
656         {0x3503, 0x33},
657         {0x3623, 0x01},
658         {0x3633, 0x24},
659         {0x3c01, 0x34},
660         {0x3c04, 0x28},
661         {0x3c05, 0x98},
662         {0x3c07, 0x07},
663         {0x3c09, 0xc2},
664         {0x4000, 0x05},
665         {0x401d, 0x08},
666         {0x4001, 0x02},
667         {0x401c, 0x42},
668         {0x5046, 0x09},
669         {0x3810, 0x40},
670         {0x3836, 0x41},
671         {0x505f, 0x04},
672         {0x5001, 0x00},
673         {0x5002, 0x02},
674         {0x503d, 0x00},
675         {0x5901, 0x08},
676         {0x585a, 0x01},
677         {0x585b, 0x2c},
678         {0x585c, 0x01},
679         {0x585d, 0x93},
680         {0x585e, 0x01},
681         {0x585f, 0x90},
682         {0x5860, 0x01},
683         {0x5861, 0x0d},
684         {0x5180, 0xc0},
685         {0x5184, 0x00},
686         {0x470a, 0x00},
687         {0x470b, 0x00},
688         {0x470c, 0x00},
689         {0x300f, 0x8e},
690         {0x3603, 0xa7},
691         {0x3632, 0x55},
692         {0x3620, 0x56},
693         {0x3621, 0xaf},
694         {0x3818, 0xc3},
695         {0x3631, 0x36},
696         {0x3632, 0x5f},
697         {0x3711, 0x24},
698         {0x401f, 0x03},
699
700         {0x3011, 0x14},
701         {0x3007, 0x3B},
702         {0x300f, 0x8f},
703         {0x4801, 0x0f},
704         {0x3003, 0x03},
705         {0x300e, 0x0c},
706         {0x3010, 0x15},
707         {0x4803, 0x50},
708         {0x4800, 0x24},
709         {0x4837, 0x40},
710         {0x3815, 0x82},
711
712         {OV5650_TABLE_END, 0x0000}
713 };
714
715 static struct ov5650_reg mode_end[] = {
716         {0x3212, 0x00},
717         {0x3003, 0x01},
718         {0x3212, 0x10},
719         {0x3212, 0xa0},
720         {0x3008, 0x02},
721
722         {OV5650_TABLE_END, 0x0000}
723 };
724
725 enum {
726         OV5650_MODE_2592x1944,
727         OV5650_MODE_1296x972,
728         OV5650_MODE_2080x1164,
729         OV5650_MODE_1920x1080,
730         OV5650_MODE_1264x704,
731         OV5650_MODE_320x240,
732         OV5650_MODE_INVALID
733 };
734
735 static struct ov5650_reg *mode_table[] = {
736         [OV5650_MODE_2592x1944] = mode_2592x1944,
737         [OV5650_MODE_1296x972]  = mode_1296x972,
738         [OV5650_MODE_2080x1164] = mode_2080x1164,
739         [OV5650_MODE_1920x1080] = mode_1920x1080,
740         [OV5650_MODE_1264x704]  = mode_1264x704,
741         [OV5650_MODE_320x240]   = mode_320x240
742 };
743
744 static inline void ov5650_get_frame_length_regs(struct ov5650_reg *regs,
745                                                 u32 frame_length)
746 {
747         regs->addr = 0x380e;
748         regs->val = (frame_length >> 8) & 0xff;
749         (regs + 1)->addr = 0x380f;
750         (regs + 1)->val = (frame_length) & 0xff;
751 }
752
753 static inline void ov5650_get_coarse_time_regs(struct ov5650_reg *regs,
754                                                 u32 coarse_time)
755 {
756         regs->addr = 0x3500;
757         regs->val = (coarse_time >> 12) & 0xff;
758         (regs + 1)->addr = 0x3501;
759         (regs + 1)->val = (coarse_time >> 4) & 0xff;
760         (regs + 2)->addr = 0x3502;
761         (regs + 2)->val = (coarse_time & 0xf) << 4;
762 }
763
764 static inline void ov5650_get_gain_reg(struct ov5650_reg *regs, u16 gain)
765 {
766         regs->addr = 0x350b;
767         regs->val = gain;
768 }
769
770 static int ov5650_read_reg(struct i2c_client *client, u16 addr, u8 *val)
771 {
772         int err;
773         struct i2c_msg msg[2];
774         unsigned char data[3];
775
776         if (!client->adapter)
777                 return -ENODEV;
778
779         msg[0].addr = client->addr;
780         msg[0].flags = 0;
781         msg[0].len = 2;
782         msg[0].buf = data;
783
784         /* high byte goes out first */
785         data[0] = (u8) (addr >> 8);
786         data[1] = (u8) (addr & 0xff);
787
788         msg[1].addr = client->addr;
789         msg[1].flags = I2C_M_RD;
790         msg[1].len = 1;
791         msg[1].buf = data + 2;
792
793         err = i2c_transfer(client->adapter, msg, 2);
794
795         if (err != 2)
796                 return -EINVAL;
797
798         *val = data[2];
799
800         return 0;
801 }
802
803 static int ov5650_read_reg_helper(struct ov5650_info *info,
804                                         u16 addr, u8 *val)
805 {
806         int ret;
807         switch (info->camera_mode) {
808         case Main:
809         case StereoCameraMode_Left:
810                 ret = ov5650_read_reg(info->left.i2c_client, addr, val);
811                 break;
812         case StereoCameraMode_Stereo:
813                 ret = ov5650_read_reg(info->left.i2c_client, addr, val);
814                 if (ret)
815                         break;
816                 ret = ov5650_read_reg(info->right.i2c_client, addr, val);
817                 break;
818         case StereoCameraMode_Right:
819                 ret = ov5650_read_reg(info->right.i2c_client, addr, val);
820                 break;
821         default:
822                 return -1;
823         }
824         return ret;
825 }
826
827 static int ov5650_write_reg(struct i2c_client *client, u16 addr, u8 val)
828 {
829         int err;
830         struct i2c_msg msg;
831         unsigned char data[3];
832
833         if (!client->adapter)
834                 return -ENODEV;
835
836         data[0] = (u8) (addr >> 8);
837         data[1] = (u8) (addr & 0xff);
838         data[2] = (u8) (val & 0xff);
839
840         msg.addr = client->addr;
841         msg.flags = 0;
842         msg.len = 3;
843         msg.buf = data;
844
845         err = i2c_transfer(client->adapter, &msg, 1);
846         if (err == 1)
847                 return 0;
848
849         pr_err("ov5650: i2c transfer failed, retrying %x %x\n", addr, val);
850
851         return err;
852 }
853
854 static int ov5650_write_reg_helper(struct ov5650_info *info,
855                                         u16 addr, u8 val)
856 {
857         int ret;
858         switch (info->camera_mode) {
859         case Main:
860         case StereoCameraMode_Left:
861                 ret = ov5650_write_reg(info->left.i2c_client, addr, val);
862                 break;
863         case StereoCameraMode_Stereo:
864                 ret = ov5650_write_reg(info->left.i2c_client, addr, val);
865                 if (ret)
866                         break;
867                 ret = ov5650_write_reg(info->right.i2c_client, addr, val);
868                 break;
869         case StereoCameraMode_Right:
870                 ret = ov5650_write_reg(info->right.i2c_client, addr, val);
871                 break;
872         default:
873                 return -1;
874         }
875         return ret;
876 }
877
878 static int ov5650_write_bulk_reg(struct i2c_client *client, u8 *data, int len)
879 {
880         int err;
881         struct i2c_msg msg;
882
883         if (!client->adapter)
884                 return -ENODEV;
885
886         msg.addr = client->addr;
887         msg.flags = 0;
888         msg.len = len;
889         msg.buf = data;
890
891         err = i2c_transfer(client->adapter, &msg, 1);
892         if (err == 1)
893                 return 0;
894
895         pr_err("ov5650: i2c bulk transfer failed at %x\n",
896                 (int)data[0] << 8 | data[1]);
897
898         return err;
899 }
900
901 static int ov5650_write_bulk_reg_helper(struct ov5650_info *info, int len)
902 {
903         int ret;
904         switch (info->camera_mode) {
905         case Main:
906         case StereoCameraMode_Left:
907                 ret = ov5650_write_bulk_reg(info->left.i2c_client,
908                                         info->i2c_trans_buf, len);
909                 break;
910         case StereoCameraMode_Stereo:
911                 ret = ov5650_write_bulk_reg(info->left.i2c_client,
912                                         info->i2c_trans_buf, len);
913                 if (ret)
914                         break;
915                 ret = ov5650_write_bulk_reg(info->right.i2c_client,
916                                         info->i2c_trans_buf, len);
917                 break;
918         case StereoCameraMode_Right:
919                 ret = ov5650_write_bulk_reg(info->right.i2c_client,
920                                         info->i2c_trans_buf, len);
921                 break;
922         default:
923                 return -1;
924         }
925         return ret;
926 }
927
928 static int ov5650_write_table(struct ov5650_info *info,
929                                 const struct ov5650_reg table[],
930                                 const struct ov5650_reg override_list[],
931                                 int num_override_regs)
932 {
933         int err;
934         const struct ov5650_reg *next, *n_next;
935         u8 *b_ptr = info->i2c_trans_buf;
936         unsigned int buf_filled = 0;
937         unsigned int i;
938         u16 val;
939
940         for (next = table; next->addr != OV5650_TABLE_END; next++) {
941                 if (next->addr == OV5650_TABLE_WAIT_MS) {
942                         msleep(next->val);
943                         continue;
944                 }
945
946                 val = next->val;
947                 /* When an override list is passed in, replace the reg */
948                 /* value to write if the reg is in the list            */
949                 if (override_list) {
950                         for (i = 0; i < num_override_regs; i++) {
951                                 if (next->addr == override_list[i].addr) {
952                                         val = override_list[i].val;
953                                         break;
954                                 }
955                         }
956                 }
957
958                 if (!buf_filled) {
959                         b_ptr = info->i2c_trans_buf;
960                         *b_ptr++ = next->addr >> 8;
961                         *b_ptr++ = next->addr & 0xff;
962                         buf_filled = 2;
963                 }
964                 *b_ptr++ = val;
965                 buf_filled++;
966
967                 n_next = next + 1;
968                 if (n_next->addr != OV5650_TABLE_END &&
969                         n_next->addr != OV5650_TABLE_WAIT_MS &&
970                         buf_filled < SIZEOF_I2C_TRANSBUF &&
971                         n_next->addr == next->addr + 1) {
972                         continue;
973                 }
974
975                 err = ov5650_write_bulk_reg_helper(info, buf_filled);
976                 if (err)
977                         return err;
978
979                 buf_filled = 0;
980         }
981         return 0;
982 }
983
984 static int ov5650_set_mode(struct ov5650_info *info, struct ov5650_mode *mode)
985 {
986         int sensor_mode;
987         int err;
988         struct ov5650_reg reg_list[6];
989
990         pr_info("%s: xres %u yres %u framelength %u coarsetime %u gain %u\n",
991                 __func__, mode->xres, mode->yres, mode->frame_length,
992                 mode->coarse_time, mode->gain);
993         if (mode->xres == 2592 && mode->yres == 1944)
994                 sensor_mode = OV5650_MODE_2592x1944;
995         else if (mode->xres == 1296 && mode->yres == 972)
996                 sensor_mode = OV5650_MODE_1296x972;
997         else if (mode->xres == 2080 && mode->yres == 1164)
998                 sensor_mode = OV5650_MODE_2080x1164;
999         else if (mode->xres == 1920 && mode->yres == 1080)
1000                 sensor_mode = OV5650_MODE_1920x1080;
1001         else if (mode->xres == 1264 && mode->yres == 704)
1002                 sensor_mode = OV5650_MODE_1264x704;
1003         else if (mode->xres == 320 && mode->yres == 240)
1004                 sensor_mode = OV5650_MODE_320x240;
1005         else {
1006                 pr_err("%s: invalid resolution supplied to set mode %d %d\n",
1007                        __func__, mode->xres, mode->yres);
1008                 return -EINVAL;
1009         }
1010
1011         /* get a list of override regs for the asking frame length, */
1012         /* coarse integration time, and gain.                       */
1013         ov5650_get_frame_length_regs(reg_list, mode->frame_length);
1014         ov5650_get_coarse_time_regs(reg_list + 2, mode->coarse_time);
1015         ov5650_get_gain_reg(reg_list + 5, mode->gain);
1016
1017         err = ov5650_write_table(info, reset_seq, NULL, 0);
1018         if (err)
1019                 return err;
1020
1021         err = ov5650_write_table(info, mode_start, NULL, 0);
1022         if (err)
1023                 return err;
1024
1025         err = ov5650_write_table(info, mode_table[sensor_mode],
1026                 reg_list, 6);
1027         if (err)
1028                 return err;
1029
1030         err = ov5650_write_table(info, mode_end, NULL, 0);
1031         if (err)
1032                 return err;
1033
1034         info->mode = sensor_mode;
1035         return 0;
1036 }
1037
1038 static int ov5650_set_frame_length(struct ov5650_info *info, u32 frame_length)
1039 {
1040         int ret;
1041         struct ov5650_reg reg_list[2];
1042         u8 *b_ptr = info->i2c_trans_buf;
1043
1044         ov5650_get_frame_length_regs(reg_list, frame_length);
1045
1046         *b_ptr++ = reg_list[0].addr >> 8;
1047         *b_ptr++ = reg_list[0].addr & 0xff;
1048         *b_ptr++ = reg_list[0].val & 0xff;
1049         *b_ptr++ = reg_list[1].val & 0xff;
1050         ret = ov5650_write_bulk_reg_helper(info, 4);
1051
1052         return ret;
1053 }
1054
1055 static int ov5650_set_coarse_time(struct ov5650_info *info, u32 coarse_time)
1056 {
1057         int ret;
1058         struct ov5650_reg reg_list[3];
1059         u8 *b_ptr = info->i2c_trans_buf;
1060
1061         ov5650_get_coarse_time_regs(reg_list, coarse_time);
1062
1063         *b_ptr++ = reg_list[0].addr >> 8;
1064         *b_ptr++ = reg_list[0].addr & 0xff;
1065         *b_ptr++ = reg_list[0].val & 0xff;
1066         *b_ptr++ = reg_list[1].val & 0xff;
1067         *b_ptr++ = reg_list[2].val & 0xff;
1068         ret = ov5650_write_bulk_reg_helper(info, 5);
1069
1070         return ret;
1071 }
1072
1073 static int ov5650_set_gain(struct ov5650_info *info, u16 gain)
1074 {
1075         int ret;
1076         struct ov5650_reg reg_list;
1077
1078         ov5650_get_gain_reg(&reg_list, gain);
1079         ret = ov5650_write_reg_helper(info, reg_list.addr, reg_list.val);
1080
1081         return ret;
1082 }
1083
1084 static int ov5650_set_group_hold(struct ov5650_info *info, struct ov5650_ae *ae)
1085 {
1086         int ret;
1087         int count = 0;
1088         bool groupHoldEnabled = false;
1089
1090         if (ae->gain_enable)
1091                 count++;
1092         if (ae->coarse_time_enable)
1093                 count++;
1094         if (ae->frame_length_enable)
1095                 count++;
1096         if (count >= 2)
1097                 groupHoldEnabled = true;
1098
1099         if (groupHoldEnabled) {
1100                 ret = ov5650_write_reg_helper(info, 0x3212, 0x01);
1101                 if (ret)
1102                         return ret;
1103         }
1104
1105         if (ae->gain_enable)
1106                 ov5650_set_gain(info, ae->gain);
1107         if (ae->coarse_time_enable)
1108                 ov5650_set_coarse_time(info, ae->coarse_time);
1109         if (ae->frame_length_enable)
1110                 ov5650_set_frame_length(info, ae->frame_length);
1111
1112         if (groupHoldEnabled) {
1113                 ret = ov5650_write_reg_helper(info, 0x3212, 0x11);
1114                 if (ret)
1115                         return ret;
1116
1117                 ret = ov5650_write_reg_helper(info, 0x3212, 0xa1);
1118                 if (ret)
1119                         return ret;
1120         }
1121
1122         return 0;
1123 }
1124
1125
1126 static int ov5650_set_binning(struct ov5650_info *info, u8 enable)
1127 {
1128         s32 ret;
1129         u8  array_ctrl_reg, analog_ctrl_reg, timing_reg;
1130         u32 val;
1131
1132         if (info->mode == OV5650_MODE_2592x1944
1133          || info->mode == OV5650_MODE_2080x1164
1134          || info->mode >= OV5650_MODE_INVALID) {
1135                 return -EINVAL;
1136         }
1137
1138         ov5650_read_reg_helper(info, OV5650_ARRAY_CONTROL_01, &array_ctrl_reg);
1139         ov5650_read_reg_helper(info, OV5650_ANALOG_CONTROL_D, &analog_ctrl_reg);
1140         ov5650_read_reg_helper(info, OV5650_TIMING_TC_REG_18, &timing_reg);
1141
1142         ret = ov5650_write_reg_helper(info,
1143                         OV5650_SRM_GRUP_ACCESS,
1144                         OV5650_GROUP_ID(3));
1145         if (ret < 0)
1146                 return -EIO;
1147
1148         if (!enable) {
1149                 ret = ov5650_write_reg_helper(info,
1150                         OV5650_ARRAY_CONTROL_01,
1151                         array_ctrl_reg |
1152                         (OV5650_H_BINNING_BIT | OV5650_H_SUBSAMPLING_BIT));
1153
1154                 if (ret < 0)
1155                         goto exit;
1156
1157                 ret = ov5650_write_reg_helper(info,
1158                         OV5650_ANALOG_CONTROL_D,
1159                         analog_ctrl_reg & ~OV5650_V_BINNING_BIT);
1160
1161                 if (ret < 0)
1162                         goto exit;
1163
1164                 ret = ov5650_write_reg_helper(info,
1165                         OV5650_TIMING_TC_REG_18,
1166                         timing_reg | OV5650_V_SUBSAMPLING_BIT);
1167
1168                 if (ret < 0)
1169                         goto exit;
1170
1171                 if (info->mode == OV5650_MODE_1296x972)
1172                         val = 0x1A2;
1173                 else
1174                         /* FIXME: this value is not verified yet. */
1175                         val = 0x1A8;
1176
1177                 ret = ov5650_write_reg_helper(info,
1178                         OV5650_TIMING_CONTROL_HS_HIGH,
1179                         (val >> 8));
1180
1181                 if (ret < 0)
1182                         goto exit;
1183
1184                 ret = ov5650_write_reg_helper(info,
1185                         OV5650_TIMING_CONTROL_HS_LOW,
1186                         (val & 0xFF));
1187         } else {
1188                 ret = ov5650_write_reg_helper(info,
1189                         OV5650_ARRAY_CONTROL_01,
1190                         (array_ctrl_reg | OV5650_H_BINNING_BIT)
1191                         & ~OV5650_H_SUBSAMPLING_BIT);
1192
1193                 if (ret < 0)
1194                         goto exit;
1195
1196                 ret = ov5650_write_reg_helper(info,
1197                         OV5650_ANALOG_CONTROL_D,
1198                         analog_ctrl_reg | OV5650_V_BINNING_BIT);
1199
1200                 if (ret < 0)
1201                         goto exit;
1202
1203                 ret = ov5650_write_reg_helper(info,
1204                         OV5650_TIMING_TC_REG_18,
1205                         timing_reg | OV5650_V_SUBSAMPLING_BIT);
1206
1207                 if (ret < 0)
1208                         goto exit;
1209
1210                 if (info->mode == OV5650_MODE_1296x972)
1211                         val = 0x33C;
1212                 else
1213                         val = 0x254;
1214
1215                 ret = ov5650_write_reg_helper(info,
1216                         OV5650_TIMING_CONTROL_HS_HIGH,
1217                         (val >> 8));
1218
1219                 if (ret < 0)
1220                         goto exit;
1221
1222                 ret = ov5650_write_reg_helper(info,
1223                         OV5650_TIMING_CONTROL_HS_LOW,
1224                         (val & 0xFF));
1225         }
1226
1227 exit:
1228         ret = ov5650_write_reg_helper(info,
1229                 OV5650_SRM_GRUP_ACCESS,
1230                 (OV5650_GROUP_HOLD_END_BIT | OV5650_GROUP_ID(3)));
1231
1232         ret |= ov5650_write_reg_helper(info,
1233                 OV5650_SRM_GRUP_ACCESS,
1234                 (OV5650_GROUP_HOLD_BIT | OV5650_GROUP_LAUNCH_BIT |
1235                 OV5650_GROUP_ID(3)));
1236
1237         return ret;
1238 }
1239
1240 static int ov5650_test_pattern(struct ov5650_info *info,
1241                                enum ov5650_test_pattern pattern)
1242 {
1243         if (pattern >= ARRAY_SIZE(test_pattern_modes))
1244                 return -EINVAL;
1245
1246         return ov5650_write_table(info,
1247                                   test_pattern_modes[pattern],
1248                                   NULL, 0);
1249 }
1250
1251 static int set_power_helper(struct ov5650_platform_data *pdata,
1252                                 int powerLevel, int *ref_cnt)
1253 {
1254         if (pdata) {
1255                 if (powerLevel && pdata->power_on) {
1256                         if (*ref_cnt == 0)
1257                                 pdata->power_on();
1258                         *ref_cnt = *ref_cnt + 1;
1259                 }
1260                 else if (pdata->power_off) {
1261                         *ref_cnt = *ref_cnt - 1;
1262                         if (*ref_cnt <= 0)
1263                                 pdata->power_off();
1264                 }
1265         }
1266         return 0;
1267 }
1268
1269 static int ov5650_set_power(struct ov5650_info *info, int powerLevel)
1270 {
1271         pr_info("%s: powerLevel=%d camera mode=%d\n", __func__, powerLevel,
1272                         info->camera_mode);
1273
1274         if (StereoCameraMode_Left & info->camera_mode) {
1275                 mutex_lock(&info->mutex_le);
1276                 set_power_helper(info->left.pdata, powerLevel,
1277                         &info->power_refcnt_le);
1278                 mutex_unlock(&info->mutex_le);
1279         }
1280
1281         if (StereoCameraMode_Right & info->camera_mode) {
1282                 mutex_lock(&info->mutex_ri);
1283                 set_power_helper(info->right.pdata, powerLevel,
1284                         &info->power_refcnt_ri);
1285                 mutex_unlock(&info->mutex_ri);
1286         }
1287
1288         return 0;
1289 }
1290
1291 static int ov5650_get_sensor_id(struct ov5650_info *info)
1292 {
1293         int ret = 0;
1294         int i;
1295         u8  bak;
1296
1297         pr_info("%s\n", __func__);
1298         if (info->sensor_data.fuse_id_size)
1299                 return 0;
1300
1301         ov5650_set_power(info, 1);
1302
1303         for (i = 0; i < 5; i++) {
1304                 ret |= ov5650_write_reg_helper(info, 0x3d00, i);
1305                 ret |= ov5650_read_reg_helper(info, 0x3d04,
1306                                 &bak);
1307                 info->sensor_data.fuse_id[i] = bak;
1308         }
1309
1310         if (!ret)
1311                 info->sensor_data.fuse_id_size = i;
1312
1313         ov5650_set_power(info, 0);
1314         return ret;
1315 }
1316
1317 static long ov5650_ioctl(struct file *file,
1318                          unsigned int cmd, unsigned long arg)
1319 {
1320         int err;
1321         struct ov5650_info *info = file->private_data;
1322
1323         switch (cmd) {
1324         case OV5650_IOCTL_SET_CAMERA_MODE:
1325         {
1326                 if (info->camera_mode != arg) {
1327                         err = ov5650_set_power(info, 0);
1328                         if (err) {
1329                                 pr_info("%s %d\n", __func__, __LINE__);
1330                                 return err;
1331                         }
1332                         info->camera_mode = arg;
1333                         err = ov5650_set_power(info, 1);
1334                         if (err)
1335                                 return err;
1336                 }
1337                 return 0;
1338         }
1339         case OV5650_IOCTL_SYNC_SENSORS:
1340                 if (info->right.pdata->synchronize_sensors)
1341                         info->right.pdata->synchronize_sensors();
1342                 return 0;
1343         case OV5650_IOCTL_SET_MODE:
1344         {
1345                 struct ov5650_mode mode;
1346                 if (copy_from_user(&mode,
1347                                    (const void __user *)arg,
1348                                    sizeof(struct ov5650_mode))) {
1349                         pr_info("%s %d\n", __func__, __LINE__);
1350                         return -EFAULT;
1351                 }
1352
1353                 return ov5650_set_mode(info, &mode);
1354         }
1355         case OV5650_IOCTL_SET_FRAME_LENGTH:
1356                 return ov5650_set_frame_length(info, (u32)arg);
1357         case OV5650_IOCTL_SET_COARSE_TIME:
1358                 return ov5650_set_coarse_time(info, (u32)arg);
1359         case OV5650_IOCTL_SET_GAIN:
1360                 return ov5650_set_gain(info, (u16)arg);
1361         case OV5650_IOCTL_SET_BINNING:
1362                 return ov5650_set_binning(info, (u8)arg);
1363         case OV5650_IOCTL_GET_STATUS:
1364         {
1365                 u16 status = 0;
1366                 if (copy_to_user((void __user *)arg, &status,
1367                                  2)) {
1368                         pr_info("%s %d\n", __func__, __LINE__);
1369                         return -EFAULT;
1370                 }
1371                 return 0;
1372         }
1373         case OV5650_IOCTL_TEST_PATTERN:
1374         {
1375                 err = ov5650_test_pattern(info, (enum ov5650_test_pattern) arg);
1376                 if (err)
1377                         pr_err("%s %d %d\n", __func__, __LINE__, err);
1378                 return err;
1379         }
1380         case OV5650_IOCTL_SET_GROUP_HOLD:
1381         {
1382                 struct ov5650_ae ae;
1383                 if (copy_from_user(&ae,
1384                                 (const void __user *)arg,
1385                                 sizeof(struct ov5650_ae))) {
1386                         pr_info("%s %d\n", __func__, __LINE__);
1387                         return -EFAULT;
1388                 }
1389                 return ov5650_set_group_hold(info, &ae);
1390         }
1391         case OV5650_IOCTL_GET_SENSORDATA:
1392         {
1393                 err = ov5650_get_sensor_id(info);
1394                 if (err) {
1395                         pr_err("%s %d %d\n", __func__, __LINE__, err);
1396                         return err;
1397                 }
1398                 if (copy_to_user((void __user *)arg,
1399                                 &info->sensor_data,
1400                                 sizeof(struct ov5650_sensordata))) {
1401                         pr_info("%s %d\n", __func__, __LINE__);
1402                         return -EFAULT;
1403                 }
1404                 return 0;
1405         }
1406         default:
1407                 return -EINVAL;
1408         }
1409         return 0;
1410 }
1411
1412 static int ov5650_open(struct inode *inode, struct file *file)
1413 {
1414         pr_info("%s\n", __func__);
1415         file->private_data = stereo_ov5650_info;
1416         ov5650_set_power(stereo_ov5650_info, 1);
1417         return 0;
1418 }
1419
1420 int ov5650_release(struct inode *inode, struct file *file)
1421 {
1422         struct ov5650_info *info = file->private_data;
1423
1424         ov5650_set_power(info, 0);
1425         file->private_data = NULL;
1426         return 0;
1427 }
1428
1429
1430 static const struct file_operations ov5650_fileops = {
1431         .owner = THIS_MODULE,
1432         .open = ov5650_open,
1433         .unlocked_ioctl = ov5650_ioctl,
1434         .release = ov5650_release,
1435 };
1436
1437 static struct miscdevice ov5650_device = {
1438         .minor = MISC_DYNAMIC_MINOR,
1439         .name = "ov5650",
1440         .fops = &ov5650_fileops,
1441 };
1442
1443 static int ov5650_probe_common(void)
1444 {
1445         int err;
1446
1447         if (!stereo_ov5650_info) {
1448                 stereo_ov5650_info = kzalloc(sizeof(struct ov5650_info),
1449                                         GFP_KERNEL);
1450                 if (!stereo_ov5650_info) {
1451                         pr_err("ov5650: Unable to allocate memory!\n");
1452                         return -ENOMEM;
1453                 }
1454
1455                 err = misc_register(&ov5650_device);
1456                 if (err) {
1457                         pr_err("ov5650: Unable to register misc device!\n");
1458                         kfree(stereo_ov5650_info);
1459                         return err;
1460                 }
1461         }
1462         return 0;
1463 }
1464
1465 static int ov5650_remove_common(struct i2c_client *client)
1466 {
1467         if (stereo_ov5650_info->left.i2c_client ||
1468                 stereo_ov5650_info->right.i2c_client)
1469                 return 0;
1470
1471         misc_deregister(&ov5650_device);
1472         kfree(stereo_ov5650_info);
1473         stereo_ov5650_info = NULL;
1474
1475         return 0;
1476 }
1477
1478 static int left_ov5650_probe(struct i2c_client *client,
1479                         const struct i2c_device_id *id)
1480 {
1481         int err;
1482         pr_info("%s: probing sensor.\n", __func__);
1483
1484         err = ov5650_probe_common();
1485         if (err)
1486                 return err;
1487
1488         stereo_ov5650_info->left.pdata = client->dev.platform_data;
1489         stereo_ov5650_info->left.i2c_client = client;
1490         mutex_init(&stereo_ov5650_info->mutex_le);
1491         mutex_init(&stereo_ov5650_info->mutex_ri);
1492
1493         return 0;
1494 }
1495
1496 static int left_ov5650_remove(struct i2c_client *client)
1497 {
1498         if (stereo_ov5650_info) {
1499                 stereo_ov5650_info->left.i2c_client = NULL;
1500                 ov5650_remove_common(client);
1501         }
1502         return 0;
1503 }
1504
1505 static const struct i2c_device_id left_ov5650_id[] = {
1506         { "ov5650", 0 },
1507         { "ov5650L", 0 },
1508         { },
1509 };
1510
1511 MODULE_DEVICE_TABLE(i2c, left_ov5650_id);
1512
1513 static struct i2c_driver left_ov5650_i2c_driver = {
1514         .driver = {
1515                 .name = "ov5650",
1516                 .owner = THIS_MODULE,
1517         },
1518         .probe = left_ov5650_probe,
1519         .remove = left_ov5650_remove,
1520         .id_table = left_ov5650_id,
1521 };
1522
1523 static int right_ov5650_probe(struct i2c_client *client,
1524                         const struct i2c_device_id *id)
1525 {
1526         int err;
1527         pr_info("%s: probing sensor.\n", __func__);
1528
1529         err = ov5650_probe_common();
1530         if (err)
1531                 return err;
1532
1533         stereo_ov5650_info->right.pdata = client->dev.platform_data;
1534         stereo_ov5650_info->right.i2c_client = client;
1535
1536         return 0;
1537 }
1538
1539 static int right_ov5650_remove(struct i2c_client *client)
1540 {
1541         if (stereo_ov5650_info) {
1542                 stereo_ov5650_info->right.i2c_client = NULL;
1543                 ov5650_remove_common(client);
1544         }
1545         return 0;
1546 }
1547
1548 static const struct i2c_device_id right_ov5650_id[] = {
1549         { "ov5650R", 0 },
1550         { },
1551 };
1552
1553 MODULE_DEVICE_TABLE(i2c, right_ov5650_id);
1554
1555 static struct i2c_driver right_ov5650_i2c_driver = {
1556         .driver = {
1557                 .name = "ov5650R",
1558                 .owner = THIS_MODULE,
1559         },
1560         .probe = right_ov5650_probe,
1561         .remove = right_ov5650_remove,
1562         .id_table = right_ov5650_id,
1563 };
1564
1565 static int __init ov5650_init(void)
1566 {
1567         int ret;
1568         pr_info("ov5650 sensor driver loading\n");
1569         ret = i2c_add_driver(&left_ov5650_i2c_driver);
1570         if (ret)
1571                 return ret;
1572         return i2c_add_driver(&right_ov5650_i2c_driver);
1573 }
1574
1575 static void __exit ov5650_exit(void)
1576 {
1577         i2c_del_driver(&right_ov5650_i2c_driver);
1578         i2c_del_driver(&left_ov5650_i2c_driver);
1579 }
1580
1581 module_init(ov5650_init);
1582 module_exit(ov5650_exit);
1583 MODULE_LICENSE("GPL v2");