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