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