V4L/DVB (10036): m5602 - ov9650: Prepare the sensor to set multiple resolutions
[pandora-kernel.git] / drivers / media / video / gspca / m5602 / m5602_ov9650.c
1 /*
2  * Driver for the ov9650 sensor
3  *
4  * Copyright (C) 2008 Erik AndrĂ©n
5  * Copyright (C) 2007 Ilyes Gouta. Based on the m5603x Linux Driver Project.
6  * Copyright (C) 2005 m5603x Linux Driver Project <m5602@x3ng.com.br>
7  *
8  * Portions of code to USB interface and ALi driver software,
9  * Copyright (c) 2006 Willem Duinker
10  * v4l2 interface modeled after the V4L2 driver
11  * for SN9C10x PC Camera Controllers
12  *
13  * This program is free software; you can redistribute it and/or
14  * modify it under the terms of the GNU General Public License as
15  * published by the Free Software Foundation, version 2.
16  *
17  */
18
19 #include "m5602_ov9650.h"
20
21 /* Vertically and horizontally flips the image if matched, needed for machines
22    where the sensor is mounted upside down */
23 static
24     const
25         struct dmi_system_id ov9650_flip_dmi_table[] = {
26         {
27                 .ident = "ASUS A6VC",
28                 .matches = {
29                         DMI_MATCH(DMI_SYS_VENDOR, "ASUSTeK Computer Inc."),
30                         DMI_MATCH(DMI_PRODUCT_NAME, "A6VC")
31                 }
32         },
33         {
34                 .ident = "ASUS A6VM",
35                 .matches = {
36                         DMI_MATCH(DMI_SYS_VENDOR, "ASUSTeK Computer Inc."),
37                         DMI_MATCH(DMI_PRODUCT_NAME, "A6VM")
38                 }
39         },
40         {
41                 .ident = "ASUS A6JC",
42                 .matches = {
43                         DMI_MATCH(DMI_SYS_VENDOR, "ASUSTeK Computer Inc."),
44                         DMI_MATCH(DMI_PRODUCT_NAME, "A6JC")
45                 }
46         },
47         {
48                 .ident = "ASUS A6Ja",
49                 .matches = {
50                         DMI_MATCH(DMI_SYS_VENDOR, "ASUSTeK Computer Inc."),
51                         DMI_MATCH(DMI_PRODUCT_NAME, "A6J")
52                 }
53         },
54         {
55                 .ident = "ASUS A6Kt",
56                 .matches = {
57                         DMI_MATCH(DMI_SYS_VENDOR, "ASUSTeK Computer Inc."),
58                         DMI_MATCH(DMI_PRODUCT_NAME, "A6Kt")
59                 }
60         },
61         {
62                 .ident = "Alienware Aurora m9700",
63                 .matches = {
64                         DMI_MATCH(DMI_SYS_VENDOR, "alienware"),
65                         DMI_MATCH(DMI_PRODUCT_NAME, "Aurora m9700")
66                 }
67         },
68         { }
69 };
70
71 static void ov9650_dump_registers(struct sd *sd);
72
73 int ov9650_probe(struct sd *sd)
74 {
75         u8 prod_id = 0, ver_id = 0, i;
76
77         if (force_sensor) {
78                 if (force_sensor == OV9650_SENSOR) {
79                         info("Forcing an %s sensor", ov9650.name);
80                         goto sensor_found;
81                 }
82                 /* If we want to force another sensor,
83                    don't try to probe this one */
84                 return -ENODEV;
85         }
86
87         info("Probing for an ov9650 sensor");
88
89         /* Run the pre-init to actually probe the unit */
90         for (i = 0; i < ARRAY_SIZE(preinit_ov9650); i++) {
91                 u8 data = preinit_ov9650[i][2];
92                 if (preinit_ov9650[i][0] == SENSOR)
93                         m5602_write_sensor(sd,
94                                             preinit_ov9650[i][1], &data, 1);
95                 else
96                         m5602_write_bridge(sd, preinit_ov9650[i][1], data);
97         }
98
99         if (m5602_read_sensor(sd, OV9650_PID, &prod_id, 1))
100                 return -ENODEV;
101
102         if (m5602_read_sensor(sd, OV9650_VER, &ver_id, 1))
103                 return -ENODEV;
104
105         if ((prod_id == 0x96) && (ver_id == 0x52)) {
106                 info("Detected an ov9650 sensor");
107                 goto sensor_found;
108         }
109
110         return -ENODEV;
111
112 sensor_found:
113         sd->gspca_dev.cam.cam_mode = ov9650.modes;
114         sd->gspca_dev.cam.nmodes = ov9650.nmodes;
115         sd->desc->ctrls = ov9650.ctrls;
116         sd->desc->nctrls = ov9650.nctrls;
117         return 0;
118 }
119
120 int ov9650_init(struct sd *sd)
121 {
122         int i, err = 0;
123         u8 data;
124
125         if (dump_sensor)
126                 ov9650_dump_registers(sd);
127
128         for (i = 0; i < ARRAY_SIZE(init_ov9650) && !err; i++) {
129                 data = init_ov9650[i][2];
130                 if (init_ov9650[i][0] == SENSOR)
131                         err = m5602_write_sensor(sd, init_ov9650[i][1],
132                                                   &data, 1);
133                 else
134                         err = m5602_write_bridge(sd, init_ov9650[i][1], data);
135         }
136
137         if (dmi_check_system(ov9650_flip_dmi_table) && !err) {
138                 info("vflip quirk active");
139                 data = 0x30;
140                 err = m5602_write_sensor(sd, OV9650_MVFP, &data, 1);
141         }
142
143         return err;
144 }
145
146 int ov9650_start(struct sd *sd)
147 {
148         int i, err = 0;
149         struct cam *cam = &sd->gspca_dev.cam;
150
151         switch (cam->cam_mode[sd->gspca_dev.curr_mode].width)
152         {
153         default:
154         case 640:
155                 PDEBUG(D_V4L2, "Configuring camera for VGA mode");
156
157                 for (i = 0; i < ARRAY_SIZE(VGA_ov9650) && !err; i++) {
158                         u8 data = VGA_ov9650[i][2];
159                         if (VGA_ov9650[i][0] == SENSOR)
160                                 err = m5602_write_sensor(sd,
161                                         VGA_ov9650[i][1], &data, 1);
162                         else
163                                 err = m5602_write_bridge(sd, VGA_ov9650[i][1], data);
164                 }
165                 break;
166         }
167         return err;
168 }
169
170 int ov9650_power_down(struct sd *sd)
171 {
172         int i, err = 0;
173         for (i = 0; i < ARRAY_SIZE(power_down_ov9650) && !err; i++) {
174                 u8 data = power_down_ov9650[i][2];
175                 if (power_down_ov9650[i][0] == SENSOR)
176                         err = m5602_write_sensor(sd,
177                                             power_down_ov9650[i][1], &data, 1);
178                 else
179                         err = m5602_write_bridge(sd, power_down_ov9650[i][1],
180                                                  data);
181         }
182
183         return err;
184 }
185
186 int ov9650_get_exposure(struct gspca_dev *gspca_dev, __s32 *val)
187 {
188         struct sd *sd = (struct sd *) gspca_dev;
189         u8 i2c_data;
190         int err;
191
192         err = m5602_read_sensor(sd, OV9650_COM1, &i2c_data, 1);
193         if (err < 0)
194                 goto out;
195         *val = i2c_data & 0x03;
196
197         err = m5602_read_sensor(sd, OV9650_AECH, &i2c_data, 1);
198         if (err < 0)
199                 goto out;
200         *val |= (i2c_data << 2);
201
202         err = m5602_read_sensor(sd, OV9650_AECHM, &i2c_data, 1);
203         if (err < 0)
204                 goto out;
205         *val |= (i2c_data & 0x3f) << 10;
206
207         PDEBUG(D_V4L2, "Read exposure %d", *val);
208 out:
209         return err;
210 }
211
212 int ov9650_set_exposure(struct gspca_dev *gspca_dev, __s32 val)
213 {
214         struct sd *sd = (struct sd *) gspca_dev;
215         u8 i2c_data;
216         int err;
217
218         PDEBUG(D_V4L2, "Set exposure to %d",
219                val & 0xffff);
220
221         /* The 6 MSBs */
222         i2c_data = (val >> 10) & 0x3f;
223         err = m5602_write_sensor(sd, OV9650_AECHM,
224                                   &i2c_data, 1);
225         if (err < 0)
226                 goto out;
227
228         /* The 8 middle bits */
229         i2c_data = (val >> 2) & 0xff;
230         err = m5602_write_sensor(sd, OV9650_AECH,
231                                   &i2c_data, 1);
232         if (err < 0)
233                 goto out;
234
235         /* The 2 LSBs */
236         i2c_data = val & 0x03;
237         err = m5602_write_sensor(sd, OV9650_COM1, &i2c_data, 1);
238
239 out:
240         return err;
241 }
242
243 int ov9650_get_gain(struct gspca_dev *gspca_dev, __s32 *val)
244 {
245         int err;
246         u8 i2c_data;
247         struct sd *sd = (struct sd *) gspca_dev;
248
249         m5602_read_sensor(sd, OV9650_VREF, &i2c_data, 1);
250         *val = (i2c_data & 0x03) << 8;
251
252         err = m5602_read_sensor(sd, OV9650_GAIN, &i2c_data, 1);
253         *val |= i2c_data;
254         PDEBUG(D_V4L2, "Read gain %d", *val);
255         return err;
256 }
257
258 int ov9650_set_gain(struct gspca_dev *gspca_dev, __s32 val)
259 {
260         int err;
261         u8 i2c_data;
262         struct sd *sd = (struct sd *) gspca_dev;
263
264         /* The 2 MSB */
265         /* Read the OV9650_VREF register first to avoid
266            corrupting the VREF high and low bits */
267         m5602_read_sensor(sd, OV9650_VREF, &i2c_data, 1);
268         /* Mask away all uninteresting bits */
269         i2c_data = ((val & 0x0300) >> 2) |
270                         (i2c_data & 0x3F);
271         err = m5602_write_sensor(sd, OV9650_VREF, &i2c_data, 1);
272
273         /* The 8 LSBs */
274         i2c_data = val & 0xff;
275         err = m5602_write_sensor(sd, OV9650_GAIN, &i2c_data, 1);
276         return err;
277 }
278
279 int ov9650_get_red_balance(struct gspca_dev *gspca_dev, __s32 *val)
280 {
281         int err;
282         u8 i2c_data;
283         struct sd *sd = (struct sd *) gspca_dev;
284
285         err = m5602_read_sensor(sd, OV9650_RED, &i2c_data, 1);
286         *val = i2c_data;
287
288         PDEBUG(D_V4L2, "Read red gain %d", *val);
289
290         return err;
291 }
292
293 int ov9650_set_red_balance(struct gspca_dev *gspca_dev, __s32 val)
294 {
295         int err;
296         u8 i2c_data;
297         struct sd *sd = (struct sd *) gspca_dev;
298
299         PDEBUG(D_V4L2, "Set red gain to %d",
300                              val & 0xff);
301
302         i2c_data = val & 0xff;
303         err = m5602_write_sensor(sd, OV9650_RED, &i2c_data, 1);
304
305         return err;
306 }
307
308 int ov9650_get_blue_balance(struct gspca_dev *gspca_dev, __s32 *val)
309 {
310         int err;
311         u8 i2c_data;
312         struct sd *sd = (struct sd *) gspca_dev;
313
314         err = m5602_read_sensor(sd, OV9650_BLUE, &i2c_data, 1);
315         *val = i2c_data;
316
317         PDEBUG(D_V4L2, "Read blue gain %d", *val);
318
319         return err;
320 }
321
322 int ov9650_set_blue_balance(struct gspca_dev *gspca_dev, __s32 val)
323 {
324         int err;
325         u8 i2c_data;
326         struct sd *sd = (struct sd *) gspca_dev;
327
328         PDEBUG(D_V4L2, "Set blue gain to %d",
329                val & 0xff);
330
331         i2c_data = val & 0xff;
332         err = m5602_write_sensor(sd, OV9650_BLUE, &i2c_data, 1);
333
334         return err;
335 }
336
337 int ov9650_get_hflip(struct gspca_dev *gspca_dev, __s32 *val)
338 {
339         int err;
340         u8 i2c_data;
341         struct sd *sd = (struct sd *) gspca_dev;
342
343         err = m5602_read_sensor(sd, OV9650_MVFP, &i2c_data, 1);
344         if (dmi_check_system(ov9650_flip_dmi_table))
345                 *val = ((i2c_data & OV9650_HFLIP) >> 5) ? 0 : 1;
346         else
347                 *val = (i2c_data & OV9650_HFLIP) >> 5;
348         PDEBUG(D_V4L2, "Read horizontal flip %d", *val);
349
350         return err;
351 }
352
353 int ov9650_set_hflip(struct gspca_dev *gspca_dev, __s32 val)
354 {
355         int err;
356         u8 i2c_data;
357         struct sd *sd = (struct sd *) gspca_dev;
358
359         PDEBUG(D_V4L2, "Set horizontal flip to %d", val);
360         err = m5602_read_sensor(sd, OV9650_MVFP, &i2c_data, 1);
361         if (err < 0)
362                 goto out;
363
364         if (dmi_check_system(ov9650_flip_dmi_table))
365                 i2c_data = ((i2c_data & 0xdf) |
366                            (((val ? 0 : 1) & 0x01) << 5));
367         else
368                 i2c_data = ((i2c_data & 0xdf) |
369                            ((val & 0x01) << 5));
370
371         err = m5602_write_sensor(sd, OV9650_MVFP, &i2c_data, 1);
372 out:
373         return err;
374 }
375
376 int ov9650_get_vflip(struct gspca_dev *gspca_dev, __s32 *val)
377 {
378         int err;
379         u8 i2c_data;
380         struct sd *sd = (struct sd *) gspca_dev;
381
382         err = m5602_read_sensor(sd, OV9650_MVFP, &i2c_data, 1);
383         if (dmi_check_system(ov9650_flip_dmi_table))
384                 *val = ((i2c_data & 0x10) >> 4) ? 0 : 1;
385         else
386                 *val = (i2c_data & 0x10) >> 4;
387         PDEBUG(D_V4L2, "Read vertical flip %d", *val);
388
389         return err;
390 }
391
392 int ov9650_set_vflip(struct gspca_dev *gspca_dev, __s32 val)
393 {
394         int err;
395         u8 i2c_data;
396         struct sd *sd = (struct sd *) gspca_dev;
397
398         PDEBUG(D_V4L2, "Set vertical flip to %d", val);
399         err = m5602_read_sensor(sd, OV9650_MVFP, &i2c_data, 1);
400         if (err < 0)
401                 goto out;
402
403         if (dmi_check_system(ov9650_flip_dmi_table))
404                 i2c_data = ((i2c_data & 0xef) |
405                                 (((val ? 0 : 1) & 0x01) << 4));
406         else
407                 i2c_data = ((i2c_data & 0xef) |
408                                 ((val & 0x01) << 4));
409
410         err = m5602_write_sensor(sd, OV9650_MVFP, &i2c_data, 1);
411 out:
412         return err;
413 }
414
415 int ov9650_get_brightness(struct gspca_dev *gspca_dev, __s32 *val)
416 {
417         int err;
418         u8 i2c_data;
419         struct sd *sd = (struct sd *) gspca_dev;
420
421         err = m5602_read_sensor(sd, OV9650_VREF, &i2c_data, 1);
422         if (err < 0)
423                 goto out;
424         *val = (i2c_data & 0x03) << 8;
425
426         err = m5602_read_sensor(sd, OV9650_GAIN, &i2c_data, 1);
427         *val |= i2c_data;
428         PDEBUG(D_V4L2, "Read gain %d", *val);
429 out:
430         return err;
431 }
432
433 int ov9650_set_brightness(struct gspca_dev *gspca_dev, __s32 val)
434 {
435         int err;
436         u8 i2c_data;
437         struct sd *sd = (struct sd *) gspca_dev;
438
439         PDEBUG(D_V4L2, "Set gain to %d", val & 0x3ff);
440
441         /* Read the OV9650_VREF register first to avoid
442                 corrupting the VREF high and low bits */
443         err = m5602_read_sensor(sd, OV9650_VREF, &i2c_data, 1);
444         if (err < 0)
445                 goto out;
446
447         /* Mask away all uninteresting bits */
448         i2c_data = ((val & 0x0300) >> 2) | (i2c_data & 0x3F);
449         err = m5602_write_sensor(sd, OV9650_VREF, &i2c_data, 1);
450         if (err < 0)
451                 goto out;
452
453         /* The 8 LSBs */
454         i2c_data = val & 0xff;
455         err = m5602_write_sensor(sd, OV9650_GAIN, &i2c_data, 1);
456
457 out:
458         return err;
459 }
460
461 int ov9650_get_auto_white_balance(struct gspca_dev *gspca_dev, __s32 *val)
462 {
463         int err;
464         u8 i2c_data;
465         struct sd *sd = (struct sd *) gspca_dev;
466
467         err = m5602_read_sensor(sd, OV9650_COM8, &i2c_data, 1);
468         *val = (i2c_data & OV9650_AWB_EN) >> 1;
469         PDEBUG(D_V4L2, "Read auto white balance %d", *val);
470
471         return err;
472 }
473
474 int ov9650_set_auto_white_balance(struct gspca_dev *gspca_dev, __s32 val)
475 {
476         int err;
477         u8 i2c_data;
478         struct sd *sd = (struct sd *) gspca_dev;
479
480         PDEBUG(D_V4L2, "Set auto white balance to %d", val);
481         err = m5602_read_sensor(sd, OV9650_COM8, &i2c_data, 1);
482         if (err < 0)
483                 goto out;
484
485         i2c_data = ((i2c_data & 0xfd) | ((val & 0x01) << 1));
486         err = m5602_write_sensor(sd, OV9650_COM8, &i2c_data, 1);
487 out:
488         return err;
489 }
490
491 int ov9650_get_auto_gain(struct gspca_dev *gspca_dev, __s32 *val)
492 {
493         int err;
494         u8 i2c_data;
495         struct sd *sd = (struct sd *) gspca_dev;
496
497         err = m5602_read_sensor(sd, OV9650_COM8, &i2c_data, 1);
498         *val = (i2c_data & OV9650_AGC_EN) >> 2;
499         PDEBUG(D_V4L2, "Read auto gain control %d", *val);
500
501         return err;
502 }
503
504 int ov9650_set_auto_gain(struct gspca_dev *gspca_dev, __s32 val)
505 {
506         int err;
507         u8 i2c_data;
508         struct sd *sd = (struct sd *) gspca_dev;
509
510         PDEBUG(D_V4L2, "Set auto gain control to %d", val);
511         err = m5602_read_sensor(sd, OV9650_COM8, &i2c_data, 1);
512         if (err < 0)
513                 goto out;
514
515         i2c_data = ((i2c_data & 0xfb) | ((val & 0x01) << 2));
516         err = m5602_write_sensor(sd, OV9650_COM8, &i2c_data, 1);
517 out:
518         return err;
519 }
520
521 static void ov9650_dump_registers(struct sd *sd)
522 {
523         int address;
524         info("Dumping the ov9650 register state");
525         for (address = 0; address < 0xa9; address++) {
526                 u8 value;
527                 m5602_read_sensor(sd, address, &value, 1);
528                 info("register 0x%x contains 0x%x",
529                      address, value);
530         }
531
532         info("ov9650 register state dump complete");
533
534         info("Probing for which registers that are read/write");
535         for (address = 0; address < 0xff; address++) {
536                 u8 old_value, ctrl_value;
537                 u8 test_value[2] = {0xff, 0xff};
538
539                 m5602_read_sensor(sd, address, &old_value, 1);
540                 m5602_write_sensor(sd, address, test_value, 1);
541                 m5602_read_sensor(sd, address, &ctrl_value, 1);
542
543                 if (ctrl_value == test_value[0])
544                         info("register 0x%x is writeable", address);
545                 else
546                         info("register 0x%x is read only", address);
547
548                 /* Restore original value */
549                 m5602_write_sensor(sd, address, &old_value, 1);
550         }
551 }