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         {0x3500, 0x00},
645         {0x3501, 0x13},
646         {0x3502, 0x80},
647         {0x350b, 0x7f},
648
649         {0x3815, 0x81},
650         {0x3824, 0x23},
651         {0x3825, 0x20},
652         {0x3826, 0x00},
653         {0x3827, 0x08},
654         {0x370d, 0xc2},
655         {0x3a08, 0x17},
656         {0x3a09, 0x64},
657         {0x3a0a, 0x13},
658         {0x3a0b, 0x80},
659         {0x3a00, 0x58},
660         {0x3a1a, 0x06},
661         {0x3503, 0x33},
662         {0x3623, 0x01},
663         {0x3633, 0x24},
664         {0x3c01, 0x34},
665         {0x3c04, 0x28},
666         {0x3c05, 0x98},
667         {0x3c07, 0x07},
668         {0x3c09, 0xc2},
669         {0x4000, 0x05},
670         {0x401d, 0x08},
671         {0x4001, 0x02},
672         {0x401c, 0x42},
673         {0x5046, 0x09},
674         {0x3810, 0x40},
675         {0x3836, 0x41},
676         {0x505f, 0x04},
677         {0x5001, 0x00},
678         {0x5002, 0x02},
679         {0x503d, 0x00},
680         {0x5901, 0x08},
681         {0x585a, 0x01},
682         {0x585b, 0x2c},
683         {0x585c, 0x01},
684         {0x585d, 0x93},
685         {0x585e, 0x01},
686         {0x585f, 0x90},
687         {0x5860, 0x01},
688         {0x5861, 0x0d},
689         {0x5180, 0xc0},
690         {0x5184, 0x00},
691         {0x470a, 0x00},
692         {0x470b, 0x00},
693         {0x470c, 0x00},
694         {0x300f, 0x8e},
695         {0x3603, 0xa7},
696         {0x3632, 0x55},
697         {0x3620, 0x56},
698         {0x3621, 0xaf},
699         {0x3818, 0xc3},
700         {0x3631, 0x36},
701         {0x3632, 0x5f},
702         {0x3711, 0x24},
703         {0x401f, 0x03},
704
705         {0x3011, 0x14},
706         {0x3007, 0x3B},
707         {0x300f, 0x8f},
708         {0x4801, 0x0f},
709         {0x3003, 0x03},
710         {0x300e, 0x0c},
711         {0x3010, 0x15},
712         {0x4803, 0x50},
713         {0x4800, 0x24},
714         {0x4837, 0x40},
715         {0x3815, 0x82},
716
717         {OV5650_TABLE_END, 0x0000}
718 };
719
720 static struct ov5650_reg mode_end[] = {
721         {0x3212, 0x00},
722         {0x3003, 0x01},
723         {0x3212, 0x10},
724         {0x3212, 0xa0},
725         {0x3008, 0x02},
726
727         {OV5650_TABLE_END, 0x0000}
728 };
729
730 enum {
731         OV5650_MODE_2592x1944,
732         OV5650_MODE_1296x972,
733         OV5650_MODE_2080x1164,
734         OV5650_MODE_1920x1080,
735         OV5650_MODE_1264x704,
736         OV5650_MODE_320x240,
737         OV5650_MODE_INVALID
738 };
739
740 static struct ov5650_reg *mode_table[] = {
741         [OV5650_MODE_2592x1944] = mode_2592x1944,
742         [OV5650_MODE_1296x972]  = mode_1296x972,
743         [OV5650_MODE_2080x1164] = mode_2080x1164,
744         [OV5650_MODE_1920x1080] = mode_1920x1080,
745         [OV5650_MODE_1264x704]  = mode_1264x704,
746         [OV5650_MODE_320x240]   = mode_320x240
747 };
748
749 static inline void ov5650_get_frame_length_regs(struct ov5650_reg *regs,
750                                                 u32 frame_length)
751 {
752         regs->addr = 0x380e;
753         regs->val = (frame_length >> 8) & 0xff;
754         (regs + 1)->addr = 0x380f;
755         (regs + 1)->val = (frame_length) & 0xff;
756 }
757
758 static inline void ov5650_get_coarse_time_regs(struct ov5650_reg *regs,
759                                                 u32 coarse_time)
760 {
761         regs->addr = 0x3500;
762         regs->val = (coarse_time >> 12) & 0xff;
763         (regs + 1)->addr = 0x3501;
764         (regs + 1)->val = (coarse_time >> 4) & 0xff;
765         (regs + 2)->addr = 0x3502;
766         (regs + 2)->val = (coarse_time & 0xf) << 4;
767 }
768
769 static inline void ov5650_get_gain_reg(struct ov5650_reg *regs, u16 gain)
770 {
771         regs->addr = 0x350b;
772         regs->val = gain;
773 }
774
775 static int ov5650_read_reg(struct i2c_client *client, u16 addr, u8 *val)
776 {
777         int err;
778         struct i2c_msg msg[2];
779         unsigned char data[3];
780
781         if (!client->adapter)
782                 return -ENODEV;
783
784         msg[0].addr = client->addr;
785         msg[0].flags = 0;
786         msg[0].len = 2;
787         msg[0].buf = data;
788
789         /* high byte goes out first */
790         data[0] = (u8) (addr >> 8);
791         data[1] = (u8) (addr & 0xff);
792
793         msg[1].addr = client->addr;
794         msg[1].flags = I2C_M_RD;
795         msg[1].len = 1;
796         msg[1].buf = data + 2;
797
798         err = i2c_transfer(client->adapter, msg, 2);
799
800         if (err != 2)
801                 return -EINVAL;
802
803         *val = data[2];
804
805         return 0;
806 }
807
808 static int ov5650_read_reg_helper(struct ov5650_info *info,
809                                         u16 addr, u8 *val)
810 {
811         int ret;
812         switch (info->camera_mode) {
813         case Main:
814         case StereoCameraMode_Left:
815                 ret = ov5650_read_reg(info->left.i2c_client, addr, val);
816                 break;
817         case StereoCameraMode_Stereo:
818                 ret = ov5650_read_reg(info->left.i2c_client, addr, val);
819                 if (ret)
820                         break;
821                 ret = ov5650_read_reg(info->right.i2c_client, addr, val);
822                 break;
823         case StereoCameraMode_Right:
824                 ret = ov5650_read_reg(info->right.i2c_client, addr, val);
825                 break;
826         default:
827                 return -1;
828         }
829         return ret;
830 }
831
832 static int ov5650_write_reg(struct i2c_client *client, u16 addr, u8 val)
833 {
834         int err;
835         struct i2c_msg msg;
836         unsigned char data[3];
837
838         if (!client->adapter)
839                 return -ENODEV;
840
841         data[0] = (u8) (addr >> 8);
842         data[1] = (u8) (addr & 0xff);
843         data[2] = (u8) (val & 0xff);
844
845         msg.addr = client->addr;
846         msg.flags = 0;
847         msg.len = 3;
848         msg.buf = data;
849
850         err = i2c_transfer(client->adapter, &msg, 1);
851         if (err == 1)
852                 return 0;
853
854         pr_err("ov5650: i2c transfer failed, retrying %x %x\n", addr, val);
855
856         return err;
857 }
858
859 static int ov5650_write_reg_helper(struct ov5650_info *info,
860                                         u16 addr, u8 val)
861 {
862         int ret;
863         switch (info->camera_mode) {
864         case Main:
865         case StereoCameraMode_Left:
866                 ret = ov5650_write_reg(info->left.i2c_client, addr, val);
867                 break;
868         case StereoCameraMode_Stereo:
869                 ret = ov5650_write_reg(info->left.i2c_client, addr, val);
870                 if (ret)
871                         break;
872                 ret = ov5650_write_reg(info->right.i2c_client, addr, val);
873                 break;
874         case StereoCameraMode_Right:
875                 ret = ov5650_write_reg(info->right.i2c_client, addr, val);
876                 break;
877         default:
878                 return -1;
879         }
880         return ret;
881 }
882
883 static int ov5650_write_bulk_reg(struct i2c_client *client, u8 *data, int len)
884 {
885         int err;
886         struct i2c_msg msg;
887
888         if (!client->adapter)
889                 return -ENODEV;
890
891         msg.addr = client->addr;
892         msg.flags = 0;
893         msg.len = len;
894         msg.buf = data;
895
896         err = i2c_transfer(client->adapter, &msg, 1);
897         if (err == 1)
898                 return 0;
899
900         pr_err("ov5650: i2c bulk transfer failed at %x\n",
901                 (int)data[0] << 8 | data[1]);
902
903         return err;
904 }
905
906 static int ov5650_write_bulk_reg_helper(struct ov5650_info *info, int len)
907 {
908         int ret;
909         switch (info->camera_mode) {
910         case Main:
911         case StereoCameraMode_Left:
912                 ret = ov5650_write_bulk_reg(info->left.i2c_client,
913                                         info->i2c_trans_buf, len);
914                 break;
915         case StereoCameraMode_Stereo:
916                 ret = ov5650_write_bulk_reg(info->left.i2c_client,
917                                         info->i2c_trans_buf, len);
918                 if (ret)
919                         break;
920                 ret = ov5650_write_bulk_reg(info->right.i2c_client,
921                                         info->i2c_trans_buf, len);
922                 break;
923         case StereoCameraMode_Right:
924                 ret = ov5650_write_bulk_reg(info->right.i2c_client,
925                                         info->i2c_trans_buf, len);
926                 break;
927         default:
928                 return -1;
929         }
930         return ret;
931 }
932
933 static int ov5650_write_table(struct ov5650_info *info,
934                                 const struct ov5650_reg table[],
935                                 const struct ov5650_reg override_list[],
936                                 int num_override_regs)
937 {
938         int err;
939         const struct ov5650_reg *next, *n_next;
940         u8 *b_ptr = info->i2c_trans_buf;
941         unsigned int buf_filled = 0;
942         unsigned int i;
943         u16 val;
944
945         for (next = table; next->addr != OV5650_TABLE_END; next++) {
946                 if (next->addr == OV5650_TABLE_WAIT_MS) {
947                         msleep(next->val);
948                         continue;
949                 }
950
951                 val = next->val;
952                 /* When an override list is passed in, replace the reg */
953                 /* value to write if the reg is in the list            */
954                 if (override_list) {
955                         for (i = 0; i < num_override_regs; i++) {
956                                 if (next->addr == override_list[i].addr) {
957                                         val = override_list[i].val;
958                                         break;
959                                 }
960                         }
961                 }
962
963                 if (!buf_filled) {
964                         b_ptr = info->i2c_trans_buf;
965                         *b_ptr++ = next->addr >> 8;
966                         *b_ptr++ = next->addr & 0xff;
967                         buf_filled = 2;
968                 }
969                 *b_ptr++ = val;
970                 buf_filled++;
971
972                 n_next = next + 1;
973                 if (n_next->addr != OV5650_TABLE_END &&
974                         n_next->addr != OV5650_TABLE_WAIT_MS &&
975                         buf_filled < SIZEOF_I2C_TRANSBUF &&
976                         n_next->addr == next->addr + 1) {
977                         continue;
978                 }
979
980                 err = ov5650_write_bulk_reg_helper(info, buf_filled);
981                 if (err)
982                         return err;
983
984                 buf_filled = 0;
985         }
986         return 0;
987 }
988
989 static int ov5650_set_mode(struct ov5650_info *info, struct ov5650_mode *mode)
990 {
991         int sensor_mode;
992         int err;
993         struct ov5650_reg reg_list[6];
994
995         pr_info("%s: xres %u yres %u framelength %u coarsetime %u gain %u\n",
996                 __func__, mode->xres, mode->yres, mode->frame_length,
997                 mode->coarse_time, mode->gain);
998         if (mode->xres == 2592 && mode->yres == 1944)
999                 sensor_mode = OV5650_MODE_2592x1944;
1000         else if (mode->xres == 1296 && mode->yres == 972)
1001                 sensor_mode = OV5650_MODE_1296x972;
1002         else if (mode->xres == 2080 && mode->yres == 1164)
1003                 sensor_mode = OV5650_MODE_2080x1164;
1004         else if (mode->xres == 1920 && mode->yres == 1080)
1005                 sensor_mode = OV5650_MODE_1920x1080;
1006         else if (mode->xres == 1264 && mode->yres == 704)
1007                 sensor_mode = OV5650_MODE_1264x704;
1008         else if (mode->xres == 320 && mode->yres == 240)
1009                 sensor_mode = OV5650_MODE_320x240;
1010         else {
1011                 pr_err("%s: invalid resolution supplied to set mode %d %d\n",
1012                        __func__, mode->xres, mode->yres);
1013                 return -EINVAL;
1014         }
1015
1016         /* get a list of override regs for the asking frame length, */
1017         /* coarse integration time, and gain.                       */
1018         ov5650_get_frame_length_regs(reg_list, mode->frame_length);
1019         ov5650_get_coarse_time_regs(reg_list + 2, mode->coarse_time);
1020         ov5650_get_gain_reg(reg_list + 5, mode->gain);
1021
1022         err = ov5650_write_table(info, reset_seq, NULL, 0);
1023         if (err)
1024                 return err;
1025
1026         err = ov5650_write_table(info, mode_start, NULL, 0);
1027         if (err)
1028                 return err;
1029
1030         err = ov5650_write_table(info, mode_table[sensor_mode],
1031                 reg_list, 6);
1032         if (err)
1033                 return err;
1034
1035         err = ov5650_write_table(info, mode_end, NULL, 0);
1036         if (err)
1037                 return err;
1038
1039         info->mode = sensor_mode;
1040         return 0;
1041 }
1042
1043 static int ov5650_set_frame_length(struct ov5650_info *info, u32 frame_length)
1044 {
1045         int ret;
1046         struct ov5650_reg reg_list[2];
1047         u8 *b_ptr = info->i2c_trans_buf;
1048
1049         ov5650_get_frame_length_regs(reg_list, frame_length);
1050
1051         *b_ptr++ = reg_list[0].addr >> 8;
1052         *b_ptr++ = reg_list[0].addr & 0xff;
1053         *b_ptr++ = reg_list[0].val & 0xff;
1054         *b_ptr++ = reg_list[1].val & 0xff;
1055         ret = ov5650_write_bulk_reg_helper(info, 4);
1056
1057         return ret;
1058 }
1059
1060 static int ov5650_set_coarse_time(struct ov5650_info *info, u32 coarse_time)
1061 {
1062         int ret;
1063         struct ov5650_reg reg_list[3];
1064         u8 *b_ptr = info->i2c_trans_buf;
1065
1066         ov5650_get_coarse_time_regs(reg_list, coarse_time);
1067
1068         *b_ptr++ = reg_list[0].addr >> 8;
1069         *b_ptr++ = reg_list[0].addr & 0xff;
1070         *b_ptr++ = reg_list[0].val & 0xff;
1071         *b_ptr++ = reg_list[1].val & 0xff;
1072         *b_ptr++ = reg_list[2].val & 0xff;
1073         ret = ov5650_write_bulk_reg_helper(info, 5);
1074
1075         return ret;
1076 }
1077
1078 static int ov5650_set_gain(struct ov5650_info *info, u16 gain)
1079 {
1080         int ret;
1081         struct ov5650_reg reg_list;
1082
1083         ov5650_get_gain_reg(&reg_list, gain);
1084         ret = ov5650_write_reg_helper(info, reg_list.addr, reg_list.val);
1085
1086         return ret;
1087 }
1088
1089 static int ov5650_set_group_hold(struct ov5650_info *info, struct ov5650_ae *ae)
1090 {
1091         int ret;
1092         int count = 0;
1093         bool groupHoldEnabled = false;
1094
1095         if (ae->gain_enable)
1096                 count++;
1097         if (ae->coarse_time_enable)
1098                 count++;
1099         if (ae->frame_length_enable)
1100                 count++;
1101         if (count >= 2)
1102                 groupHoldEnabled = true;
1103
1104         if (groupHoldEnabled) {
1105                 ret = ov5650_write_reg_helper(info, 0x3212, 0x01);
1106                 if (ret)
1107                         return ret;
1108         }
1109
1110         if (ae->gain_enable)
1111                 ov5650_set_gain(info, ae->gain);
1112         if (ae->coarse_time_enable)
1113                 ov5650_set_coarse_time(info, ae->coarse_time);
1114         if (ae->frame_length_enable)
1115                 ov5650_set_frame_length(info, ae->frame_length);
1116
1117         if (groupHoldEnabled) {
1118                 ret = ov5650_write_reg_helper(info, 0x3212, 0x11);
1119                 if (ret)
1120                         return ret;
1121
1122                 ret = ov5650_write_reg_helper(info, 0x3212, 0xa1);
1123                 if (ret)
1124                         return ret;
1125         }
1126
1127         return 0;
1128 }
1129
1130
1131 static int ov5650_set_binning(struct ov5650_info *info, u8 enable)
1132 {
1133         s32 ret;
1134         u8  array_ctrl_reg, analog_ctrl_reg, timing_reg;
1135         u32 val;
1136
1137         if (info->mode == OV5650_MODE_2592x1944
1138          || info->mode == OV5650_MODE_2080x1164
1139          || info->mode >= OV5650_MODE_INVALID) {
1140                 return -EINVAL;
1141         }
1142
1143         ov5650_read_reg_helper(info, OV5650_ARRAY_CONTROL_01, &array_ctrl_reg);
1144         ov5650_read_reg_helper(info, OV5650_ANALOG_CONTROL_D, &analog_ctrl_reg);
1145         ov5650_read_reg_helper(info, OV5650_TIMING_TC_REG_18, &timing_reg);
1146
1147         ret = ov5650_write_reg_helper(info,
1148                         OV5650_SRM_GRUP_ACCESS,
1149                         OV5650_GROUP_ID(3));
1150         if (ret < 0)
1151                 return -EIO;
1152
1153         if (!enable) {
1154                 ret = ov5650_write_reg_helper(info,
1155                         OV5650_ARRAY_CONTROL_01,
1156                         array_ctrl_reg |
1157                         (OV5650_H_BINNING_BIT | OV5650_H_SUBSAMPLING_BIT));
1158
1159                 if (ret < 0)
1160                         goto exit;
1161
1162                 ret = ov5650_write_reg_helper(info,
1163                         OV5650_ANALOG_CONTROL_D,
1164                         analog_ctrl_reg & ~OV5650_V_BINNING_BIT);
1165
1166                 if (ret < 0)
1167                         goto exit;
1168
1169                 ret = ov5650_write_reg_helper(info,
1170                         OV5650_TIMING_TC_REG_18,
1171                         timing_reg | OV5650_V_SUBSAMPLING_BIT);
1172
1173                 if (ret < 0)
1174                         goto exit;
1175
1176                 if (info->mode == OV5650_MODE_1296x972)
1177                         val = 0x1A2;
1178                 else
1179                         /* FIXME: this value is not verified yet. */
1180                         val = 0x1A8;
1181
1182                 ret = ov5650_write_reg_helper(info,
1183                         OV5650_TIMING_CONTROL_HS_HIGH,
1184                         (val >> 8));
1185
1186                 if (ret < 0)
1187                         goto exit;
1188
1189                 ret = ov5650_write_reg_helper(info,
1190                         OV5650_TIMING_CONTROL_HS_LOW,
1191                         (val & 0xFF));
1192         } else {
1193                 ret = ov5650_write_reg_helper(info,
1194                         OV5650_ARRAY_CONTROL_01,
1195                         (array_ctrl_reg | OV5650_H_BINNING_BIT)
1196                         & ~OV5650_H_SUBSAMPLING_BIT);
1197
1198                 if (ret < 0)
1199                         goto exit;
1200
1201                 ret = ov5650_write_reg_helper(info,
1202                         OV5650_ANALOG_CONTROL_D,
1203                         analog_ctrl_reg | OV5650_V_BINNING_BIT);
1204
1205                 if (ret < 0)
1206                         goto exit;
1207
1208                 ret = ov5650_write_reg_helper(info,
1209                         OV5650_TIMING_TC_REG_18,
1210                         timing_reg | OV5650_V_SUBSAMPLING_BIT);
1211
1212                 if (ret < 0)
1213                         goto exit;
1214
1215                 if (info->mode == OV5650_MODE_1296x972)
1216                         val = 0x33C;
1217                 else
1218                         val = 0x254;
1219
1220                 ret = ov5650_write_reg_helper(info,
1221                         OV5650_TIMING_CONTROL_HS_HIGH,
1222                         (val >> 8));
1223
1224                 if (ret < 0)
1225                         goto exit;
1226
1227                 ret = ov5650_write_reg_helper(info,
1228                         OV5650_TIMING_CONTROL_HS_LOW,
1229                         (val & 0xFF));
1230         }
1231
1232 exit:
1233         ret = ov5650_write_reg_helper(info,
1234                 OV5650_SRM_GRUP_ACCESS,
1235                 (OV5650_GROUP_HOLD_END_BIT | OV5650_GROUP_ID(3)));
1236
1237         ret |= ov5650_write_reg_helper(info,
1238                 OV5650_SRM_GRUP_ACCESS,
1239                 (OV5650_GROUP_HOLD_BIT | OV5650_GROUP_LAUNCH_BIT |
1240                 OV5650_GROUP_ID(3)));
1241
1242         return ret;
1243 }
1244
1245 static int ov5650_test_pattern(struct ov5650_info *info,
1246                                enum ov5650_test_pattern pattern)
1247 {
1248         if (pattern >= ARRAY_SIZE(test_pattern_modes))
1249                 return -EINVAL;
1250
1251         return ov5650_write_table(info,
1252                                   test_pattern_modes[pattern],
1253                                   NULL, 0);
1254 }
1255
1256 static int set_power_helper(struct ov5650_platform_data *pdata,
1257                                 int powerLevel, int *ref_cnt)
1258 {
1259         if (pdata) {
1260                 if (powerLevel && pdata->power_on) {
1261                         if (*ref_cnt == 0)
1262                                 pdata->power_on();
1263                         *ref_cnt = *ref_cnt + 1;
1264                 }
1265                 else if (pdata->power_off) {
1266                         *ref_cnt = *ref_cnt - 1;
1267                         if (*ref_cnt <= 0)
1268                                 pdata->power_off();
1269                 }
1270         }
1271         return 0;
1272 }
1273
1274 static int ov5650_set_power(struct ov5650_info *info, int powerLevel)
1275 {
1276         pr_info("%s: powerLevel=%d camera mode=%d\n", __func__, powerLevel,
1277                         info->camera_mode);
1278
1279         if (StereoCameraMode_Left & info->camera_mode) {
1280                 mutex_lock(&info->mutex_le);
1281                 set_power_helper(info->left.pdata, powerLevel,
1282                         &info->power_refcnt_le);
1283                 mutex_unlock(&info->mutex_le);
1284         }
1285
1286         if (StereoCameraMode_Right & info->camera_mode) {
1287                 mutex_lock(&info->mutex_ri);
1288                 set_power_helper(info->right.pdata, powerLevel,
1289                         &info->power_refcnt_ri);
1290                 mutex_unlock(&info->mutex_ri);
1291         }
1292
1293         return 0;
1294 }
1295
1296 static int ov5650_get_sensor_id(struct ov5650_info *info)
1297 {
1298         int ret = 0;
1299         int i;
1300         u8  bak;
1301
1302         pr_info("%s\n", __func__);
1303         if (info->sensor_data.fuse_id_size)
1304                 return 0;
1305
1306         ov5650_set_power(info, 1);
1307
1308         for (i = 0; i < 5; i++) {
1309                 ret |= ov5650_write_reg_helper(info, 0x3d00, i);
1310                 ret |= ov5650_read_reg_helper(info, 0x3d04,
1311                                 &bak);
1312                 info->sensor_data.fuse_id[i] = bak;
1313         }
1314
1315         if (!ret)
1316                 info->sensor_data.fuse_id_size = i;
1317
1318         ov5650_set_power(info, 0);
1319         return ret;
1320 }
1321
1322 static long ov5650_ioctl(struct file *file,
1323                          unsigned int cmd, unsigned long arg)
1324 {
1325         int err;
1326         struct ov5650_info *info = file->private_data;
1327
1328         switch (cmd) {
1329         case OV5650_IOCTL_SET_CAMERA_MODE:
1330         {
1331                 if (info->camera_mode != arg) {
1332                         err = ov5650_set_power(info, 0);
1333                         if (err) {
1334                                 pr_info("%s %d\n", __func__, __LINE__);
1335                                 return err;
1336                         }
1337                         info->camera_mode = arg;
1338                         err = ov5650_set_power(info, 1);
1339                         if (err)
1340                                 return err;
1341                 }
1342                 return 0;
1343         }
1344         case OV5650_IOCTL_SYNC_SENSORS:
1345                 if (info->right.pdata->synchronize_sensors)
1346                         info->right.pdata->synchronize_sensors();
1347                 return 0;
1348         case OV5650_IOCTL_SET_MODE:
1349         {
1350                 struct ov5650_mode mode;
1351                 if (copy_from_user(&mode,
1352                                    (const void __user *)arg,
1353                                    sizeof(struct ov5650_mode))) {
1354                         pr_info("%s %d\n", __func__, __LINE__);
1355                         return -EFAULT;
1356                 }
1357
1358                 return ov5650_set_mode(info, &mode);
1359         }
1360         case OV5650_IOCTL_SET_FRAME_LENGTH:
1361                 return ov5650_set_frame_length(info, (u32)arg);
1362         case OV5650_IOCTL_SET_COARSE_TIME:
1363                 return ov5650_set_coarse_time(info, (u32)arg);
1364         case OV5650_IOCTL_SET_GAIN:
1365                 return ov5650_set_gain(info, (u16)arg);
1366         case OV5650_IOCTL_SET_BINNING:
1367                 return ov5650_set_binning(info, (u8)arg);
1368         case OV5650_IOCTL_GET_STATUS:
1369         {
1370                 u16 status = 0;
1371                 if (copy_to_user((void __user *)arg, &status,
1372                                  2)) {
1373                         pr_info("%s %d\n", __func__, __LINE__);
1374                         return -EFAULT;
1375                 }
1376                 return 0;
1377         }
1378         case OV5650_IOCTL_TEST_PATTERN:
1379         {
1380                 err = ov5650_test_pattern(info, (enum ov5650_test_pattern) arg);
1381                 if (err)
1382                         pr_err("%s %d %d\n", __func__, __LINE__, err);
1383                 return err;
1384         }
1385         case OV5650_IOCTL_SET_GROUP_HOLD:
1386         {
1387                 struct ov5650_ae ae;
1388                 if (copy_from_user(&ae,
1389                                 (const void __user *)arg,
1390                                 sizeof(struct ov5650_ae))) {
1391                         pr_info("%s %d\n", __func__, __LINE__);
1392                         return -EFAULT;
1393                 }
1394                 return ov5650_set_group_hold(info, &ae);
1395         }
1396         case OV5650_IOCTL_GET_SENSORDATA:
1397         {
1398                 err = ov5650_get_sensor_id(info);
1399                 if (err) {
1400                         pr_err("%s %d %d\n", __func__, __LINE__, err);
1401                         return err;
1402                 }
1403                 if (copy_to_user((void __user *)arg,
1404                                 &info->sensor_data,
1405                                 sizeof(struct ov5650_sensordata))) {
1406                         pr_info("%s %d\n", __func__, __LINE__);
1407                         return -EFAULT;
1408                 }
1409                 return 0;
1410         }
1411         default:
1412                 return -EINVAL;
1413         }
1414         return 0;
1415 }
1416
1417 static int ov5650_open(struct inode *inode, struct file *file)
1418 {
1419         pr_info("%s\n", __func__);
1420         file->private_data = stereo_ov5650_info;
1421         ov5650_set_power(stereo_ov5650_info, 1);
1422         return 0;
1423 }
1424
1425 int ov5650_release(struct inode *inode, struct file *file)
1426 {
1427         struct ov5650_info *info = file->private_data;
1428
1429         ov5650_set_power(info, 0);
1430         file->private_data = NULL;
1431         return 0;
1432 }
1433
1434
1435 static const struct file_operations ov5650_fileops = {
1436         .owner = THIS_MODULE,
1437         .open = ov5650_open,
1438         .unlocked_ioctl = ov5650_ioctl,
1439         .release = ov5650_release,
1440 };
1441
1442 static struct miscdevice ov5650_device = {
1443         .minor = MISC_DYNAMIC_MINOR,
1444         .name = "ov5650",
1445         .fops = &ov5650_fileops,
1446 };
1447
1448 static int ov5650_probe_common(void)
1449 {
1450         int err;
1451
1452         if (!stereo_ov5650_info) {
1453                 stereo_ov5650_info = kzalloc(sizeof(struct ov5650_info),
1454                                         GFP_KERNEL);
1455                 if (!stereo_ov5650_info) {
1456                         pr_err("ov5650: Unable to allocate memory!\n");
1457                         return -ENOMEM;
1458                 }
1459
1460                 err = misc_register(&ov5650_device);
1461                 if (err) {
1462                         pr_err("ov5650: Unable to register misc device!\n");
1463                         kfree(stereo_ov5650_info);
1464                         return err;
1465                 }
1466         }
1467         return 0;
1468 }
1469
1470 static int ov5650_remove_common(struct i2c_client *client)
1471 {
1472         if (stereo_ov5650_info->left.i2c_client ||
1473                 stereo_ov5650_info->right.i2c_client)
1474                 return 0;
1475
1476         misc_deregister(&ov5650_device);
1477         kfree(stereo_ov5650_info);
1478         stereo_ov5650_info = NULL;
1479
1480         return 0;
1481 }
1482
1483 static int left_ov5650_probe(struct i2c_client *client,
1484                         const struct i2c_device_id *id)
1485 {
1486         int err;
1487         pr_info("%s: probing sensor.\n", __func__);
1488
1489         err = ov5650_probe_common();
1490         if (err)
1491                 return err;
1492
1493         stereo_ov5650_info->left.pdata = client->dev.platform_data;
1494         stereo_ov5650_info->left.i2c_client = client;
1495         mutex_init(&stereo_ov5650_info->mutex_le);
1496         mutex_init(&stereo_ov5650_info->mutex_ri);
1497
1498         return 0;
1499 }
1500
1501 static int left_ov5650_remove(struct i2c_client *client)
1502 {
1503         if (stereo_ov5650_info) {
1504                 stereo_ov5650_info->left.i2c_client = NULL;
1505                 ov5650_remove_common(client);
1506         }
1507         return 0;
1508 }
1509
1510 static const struct i2c_device_id left_ov5650_id[] = {
1511         { "ov5650", 0 },
1512         { "ov5650L", 0 },
1513         { },
1514 };
1515
1516 MODULE_DEVICE_TABLE(i2c, left_ov5650_id);
1517
1518 static struct i2c_driver left_ov5650_i2c_driver = {
1519         .driver = {
1520                 .name = "ov5650",
1521                 .owner = THIS_MODULE,
1522         },
1523         .probe = left_ov5650_probe,
1524         .remove = left_ov5650_remove,
1525         .id_table = left_ov5650_id,
1526 };
1527
1528 static int right_ov5650_probe(struct i2c_client *client,
1529                         const struct i2c_device_id *id)
1530 {
1531         int err;
1532         pr_info("%s: probing sensor.\n", __func__);
1533
1534         err = ov5650_probe_common();
1535         if (err)
1536                 return err;
1537
1538         stereo_ov5650_info->right.pdata = client->dev.platform_data;
1539         stereo_ov5650_info->right.i2c_client = client;
1540
1541         return 0;
1542 }
1543
1544 static int right_ov5650_remove(struct i2c_client *client)
1545 {
1546         if (stereo_ov5650_info) {
1547                 stereo_ov5650_info->right.i2c_client = NULL;
1548                 ov5650_remove_common(client);
1549         }
1550         return 0;
1551 }
1552
1553 static const struct i2c_device_id right_ov5650_id[] = {
1554         { "ov5650R", 0 },
1555         { },
1556 };
1557
1558 MODULE_DEVICE_TABLE(i2c, right_ov5650_id);
1559
1560 static struct i2c_driver right_ov5650_i2c_driver = {
1561         .driver = {
1562                 .name = "ov5650R",
1563                 .owner = THIS_MODULE,
1564         },
1565         .probe = right_ov5650_probe,
1566         .remove = right_ov5650_remove,
1567         .id_table = right_ov5650_id,
1568 };
1569
1570 static int __init ov5650_init(void)
1571 {
1572         int ret;
1573         pr_info("ov5650 sensor driver loading\n");
1574         ret = i2c_add_driver(&left_ov5650_i2c_driver);
1575         if (ret)
1576                 return ret;
1577         return i2c_add_driver(&right_ov5650_i2c_driver);
1578 }
1579
1580 static void __exit ov5650_exit(void)
1581 {
1582         i2c_del_driver(&right_ov5650_i2c_driver);
1583         i2c_del_driver(&left_ov5650_i2c_driver);
1584 }
1585
1586 module_init(ov5650_init);
1587 module_exit(ov5650_exit);
1588 MODULE_LICENSE("GPL v2");