V4L/DVB (11407): gspca - m5602-ov9650: Clean up ov9650_start() function.
[safe/jmp/linux-2.6] / 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         return err;
143 }
144
145 int ov9650_start(struct sd *sd)
146 {
147         int i, err = 0;
148         struct cam *cam = &sd->gspca_dev.cam;
149
150         for (i = 0; i < ARRAY_SIZE(res_init_ov9650) && !err; i++)
151                 err = m5602_write_bridge(sd, res_init_ov9650[i][0], res_init_ov9650[i][1]);
152         if (err < 0)
153                 return err;
154
155         switch (cam->cam_mode[sd->gspca_dev.curr_mode].width)
156         {
157         case 640:
158                 PDEBUG(D_V4L2, "Configuring camera for VGA mode");
159
160                 for (i = 0; i < ARRAY_SIZE(VGA_ov9650) && !err; i++) {
161                         if (VGA_ov9650[i][0] == SENSOR) {
162                                 u8 data = VGA_ov9650[i][2];
163
164                                 err = m5602_write_sensor(sd,
165                                         VGA_ov9650[i][1], &data, 1);
166                         } else {
167                                 err = m5602_write_bridge(sd, VGA_ov9650[i][1], VGA_ov9650[i][2]);
168                         }
169                 }
170                 break;
171
172         case 352:
173                 PDEBUG(D_V4L2, "Configuring camera for CIF mode");
174
175                 for (i = 0; i < ARRAY_SIZE(CIF_ov9650) && !err; i++) {
176                         if (CIF_ov9650[i][0] == SENSOR) {
177                                 u8 data = CIF_ov9650[i][2];
178
179                                 err = m5602_write_sensor(sd,
180                                         CIF_ov9650[i][1], &data, 1);
181                         } else {
182                                 err = m5602_write_bridge(sd, CIF_ov9650[i][1], CIF_ov9650[i][2]);
183                         }
184                 }
185                 break;
186
187         case 320:
188                 PDEBUG(D_V4L2, "Configuring camera for QVGA mode");
189
190                 for (i = 0; i < ARRAY_SIZE(QVGA_ov9650) && !err; i++) {
191                         if (QVGA_ov9650[i][0] == SENSOR) {
192                                 u8 data = QVGA_ov9650[i][2];
193
194                                 err = m5602_write_sensor(sd,
195                                         QVGA_ov9650[i][1], &data, 1);
196                         } else {
197                                 err = m5602_write_bridge(sd, QVGA_ov9650[i][1], QVGA_ov9650[i][2]);
198                         }
199                 }
200                 break;
201
202         case 176:
203                 PDEBUG(D_V4L2, "Configuring camera for QCIF mode");
204
205                 for (i = 0; i < ARRAY_SIZE(QCIF_ov9650) && !err; i++) {
206                         if (QCIF_ov9650[i][0] == SENSOR) {
207                                 u8 data = QCIF_ov9650[i][2];
208                                 err = m5602_write_sensor(sd,
209                                         QCIF_ov9650[i][1], &data, 1);
210                         } else {
211                                 err = m5602_write_bridge(sd, QCIF_ov9650[i][1], QCIF_ov9650[i][2]);
212                         }
213                 }
214                 break;
215
216         }
217         return err;
218 }
219
220 int ov9650_power_down(struct sd *sd)
221 {
222         int i, err = 0;
223         for (i = 0; i < ARRAY_SIZE(power_down_ov9650) && !err; i++) {
224                 u8 data = power_down_ov9650[i][2];
225                 if (power_down_ov9650[i][0] == SENSOR)
226                         err = m5602_write_sensor(sd,
227                                             power_down_ov9650[i][1], &data, 1);
228                 else
229                         err = m5602_write_bridge(sd, power_down_ov9650[i][1],
230                                                  data);
231         }
232
233         return err;
234 }
235
236 int ov9650_get_exposure(struct gspca_dev *gspca_dev, __s32 *val)
237 {
238         struct sd *sd = (struct sd *) gspca_dev;
239         u8 i2c_data;
240         int err;
241
242         err = m5602_read_sensor(sd, OV9650_COM1, &i2c_data, 1);
243         if (err < 0)
244                 return err;
245         *val = i2c_data & 0x03;
246
247         err = m5602_read_sensor(sd, OV9650_AECH, &i2c_data, 1);
248         if (err < 0)
249                 return err;
250         *val |= (i2c_data << 2);
251
252         err = m5602_read_sensor(sd, OV9650_AECHM, &i2c_data, 1);
253         if (err < 0)
254                 return err;
255         *val |= (i2c_data & 0x3f) << 10;
256
257         PDEBUG(D_V4L2, "Read exposure %d", *val);
258
259         return err;
260 }
261
262 int ov9650_set_exposure(struct gspca_dev *gspca_dev, __s32 val)
263 {
264         struct sd *sd = (struct sd *) gspca_dev;
265         u8 i2c_data;
266         int err;
267
268         PDEBUG(D_V4L2, "Set exposure to %d",
269                val & 0xffff);
270
271         /* The 6 MSBs */
272         i2c_data = (val >> 10) & 0x3f;
273         err = m5602_write_sensor(sd, OV9650_AECHM,
274                                   &i2c_data, 1);
275         if (err < 0)
276                 return err;
277
278         /* The 8 middle bits */
279         i2c_data = (val >> 2) & 0xff;
280         err = m5602_write_sensor(sd, OV9650_AECH,
281                                   &i2c_data, 1);
282         if (err < 0)
283                 return err;
284
285         /* The 2 LSBs */
286         i2c_data = val & 0x03;
287         err = m5602_write_sensor(sd, OV9650_COM1, &i2c_data, 1);
288
289         return err;
290 }
291
292 int ov9650_get_gain(struct gspca_dev *gspca_dev, __s32 *val)
293 {
294         int err;
295         u8 i2c_data;
296         struct sd *sd = (struct sd *) gspca_dev;
297
298         m5602_read_sensor(sd, OV9650_VREF, &i2c_data, 1);
299         *val = (i2c_data & 0x03) << 8;
300
301         err = m5602_read_sensor(sd, OV9650_GAIN, &i2c_data, 1);
302         *val |= i2c_data;
303         PDEBUG(D_V4L2, "Read gain %d", *val);
304         return err;
305 }
306
307 int ov9650_set_gain(struct gspca_dev *gspca_dev, __s32 val)
308 {
309         int err;
310         u8 i2c_data;
311         struct sd *sd = (struct sd *) gspca_dev;
312
313         /* The 2 MSB */
314         /* Read the OV9650_VREF register first to avoid
315            corrupting the VREF high and low bits */
316         m5602_read_sensor(sd, OV9650_VREF, &i2c_data, 1);
317         /* Mask away all uninteresting bits */
318         i2c_data = ((val & 0x0300) >> 2) |
319                         (i2c_data & 0x3F);
320         err = m5602_write_sensor(sd, OV9650_VREF, &i2c_data, 1);
321
322         /* The 8 LSBs */
323         i2c_data = val & 0xff;
324         err = m5602_write_sensor(sd, OV9650_GAIN, &i2c_data, 1);
325         return err;
326 }
327
328 int ov9650_get_red_balance(struct gspca_dev *gspca_dev, __s32 *val)
329 {
330         int err;
331         u8 i2c_data;
332         struct sd *sd = (struct sd *) gspca_dev;
333
334         err = m5602_read_sensor(sd, OV9650_RED, &i2c_data, 1);
335         *val = i2c_data;
336
337         PDEBUG(D_V4L2, "Read red gain %d", *val);
338
339         return err;
340 }
341
342 int ov9650_set_red_balance(struct gspca_dev *gspca_dev, __s32 val)
343 {
344         int err;
345         u8 i2c_data;
346         struct sd *sd = (struct sd *) gspca_dev;
347
348         PDEBUG(D_V4L2, "Set red gain to %d",
349                              val & 0xff);
350
351         i2c_data = val & 0xff;
352         err = m5602_write_sensor(sd, OV9650_RED, &i2c_data, 1);
353
354         return err;
355 }
356
357 int ov9650_get_blue_balance(struct gspca_dev *gspca_dev, __s32 *val)
358 {
359         int err;
360         u8 i2c_data;
361         struct sd *sd = (struct sd *) gspca_dev;
362
363         err = m5602_read_sensor(sd, OV9650_BLUE, &i2c_data, 1);
364         *val = i2c_data;
365
366         PDEBUG(D_V4L2, "Read blue gain %d", *val);
367
368         return err;
369 }
370
371 int ov9650_set_blue_balance(struct gspca_dev *gspca_dev, __s32 val)
372 {
373         int err;
374         u8 i2c_data;
375         struct sd *sd = (struct sd *) gspca_dev;
376
377         PDEBUG(D_V4L2, "Set blue gain to %d",
378                val & 0xff);
379
380         i2c_data = val & 0xff;
381         err = m5602_write_sensor(sd, OV9650_BLUE, &i2c_data, 1);
382
383         return err;
384 }
385
386 int ov9650_get_hflip(struct gspca_dev *gspca_dev, __s32 *val)
387 {
388         int err;
389         u8 i2c_data;
390         struct sd *sd = (struct sd *) gspca_dev;
391
392         err = m5602_read_sensor(sd, OV9650_MVFP, &i2c_data, 1);
393         if (dmi_check_system(ov9650_flip_dmi_table))
394                 *val = ((i2c_data & OV9650_HFLIP) >> 5) ? 0 : 1;
395         else
396                 *val = (i2c_data & OV9650_HFLIP) >> 5;
397         PDEBUG(D_V4L2, "Read horizontal flip %d", *val);
398
399         return err;
400 }
401
402 int ov9650_set_hflip(struct gspca_dev *gspca_dev, __s32 val)
403 {
404         int err;
405         u8 i2c_data;
406         struct sd *sd = (struct sd *) gspca_dev;
407
408         PDEBUG(D_V4L2, "Set horizontal flip to %d", val);
409         err = m5602_read_sensor(sd, OV9650_MVFP, &i2c_data, 1);
410         if (err < 0)
411                 return err;
412
413         if (dmi_check_system(ov9650_flip_dmi_table))
414                 i2c_data = ((i2c_data & 0xdf) |
415                            (((val ? 0 : 1) & 0x01) << 5));
416         else
417                 i2c_data = ((i2c_data & 0xdf) |
418                            ((val & 0x01) << 5));
419
420         err = m5602_write_sensor(sd, OV9650_MVFP, &i2c_data, 1);
421
422         return err;
423 }
424
425 int ov9650_get_vflip(struct gspca_dev *gspca_dev, __s32 *val)
426 {
427         int err;
428         u8 i2c_data;
429         struct sd *sd = (struct sd *) gspca_dev;
430
431         err = m5602_read_sensor(sd, OV9650_MVFP, &i2c_data, 1);
432         if (dmi_check_system(ov9650_flip_dmi_table))
433                 *val = ((i2c_data & 0x10) >> 4) ? 0 : 1;
434         else
435                 *val = (i2c_data & 0x10) >> 4;
436         PDEBUG(D_V4L2, "Read vertical flip %d", *val);
437
438         return err;
439 }
440
441 int ov9650_set_vflip(struct gspca_dev *gspca_dev, __s32 val)
442 {
443         int err;
444         u8 i2c_data;
445         struct sd *sd = (struct sd *) gspca_dev;
446
447         PDEBUG(D_V4L2, "Set vertical flip to %d", val);
448         err = m5602_read_sensor(sd, OV9650_MVFP, &i2c_data, 1);
449         if (err < 0)
450                 return err;
451
452         if (dmi_check_system(ov9650_flip_dmi_table))
453                 i2c_data = ((i2c_data & 0xef) |
454                                 (((val ? 0 : 1) & 0x01) << 4));
455         else
456                 i2c_data = ((i2c_data & 0xef) |
457                                 ((val & 0x01) << 4));
458
459         err = m5602_write_sensor(sd, OV9650_MVFP, &i2c_data, 1);
460
461         return err;
462 }
463
464 int ov9650_get_brightness(struct gspca_dev *gspca_dev, __s32 *val)
465 {
466         int err;
467         u8 i2c_data;
468         struct sd *sd = (struct sd *) gspca_dev;
469
470         err = m5602_read_sensor(sd, OV9650_VREF, &i2c_data, 1);
471         if (err < 0)
472                 return err;
473         *val = (i2c_data & 0x03) << 8;
474
475         err = m5602_read_sensor(sd, OV9650_GAIN, &i2c_data, 1);
476         *val |= i2c_data;
477         PDEBUG(D_V4L2, "Read gain %d", *val);
478
479         return err;
480 }
481
482 int ov9650_set_brightness(struct gspca_dev *gspca_dev, __s32 val)
483 {
484         int err;
485         u8 i2c_data;
486         struct sd *sd = (struct sd *) gspca_dev;
487
488         PDEBUG(D_V4L2, "Set gain to %d", val & 0x3ff);
489
490         /* Read the OV9650_VREF register first to avoid
491                 corrupting the VREF high and low bits */
492         err = m5602_read_sensor(sd, OV9650_VREF, &i2c_data, 1);
493         if (err < 0)
494                 return err;
495
496         /* Mask away all uninteresting bits */
497         i2c_data = ((val & 0x0300) >> 2) | (i2c_data & 0x3F);
498         err = m5602_write_sensor(sd, OV9650_VREF, &i2c_data, 1);
499         if (err < 0)
500                 return err;
501
502         /* The 8 LSBs */
503         i2c_data = val & 0xff;
504         err = m5602_write_sensor(sd, OV9650_GAIN, &i2c_data, 1);
505
506         return err;
507 }
508
509 int ov9650_get_auto_white_balance(struct gspca_dev *gspca_dev, __s32 *val)
510 {
511         int err;
512         u8 i2c_data;
513         struct sd *sd = (struct sd *) gspca_dev;
514
515         err = m5602_read_sensor(sd, OV9650_COM8, &i2c_data, 1);
516         *val = (i2c_data & OV9650_AWB_EN) >> 1;
517         PDEBUG(D_V4L2, "Read auto white balance %d", *val);
518
519         return err;
520 }
521
522 int ov9650_set_auto_white_balance(struct gspca_dev *gspca_dev, __s32 val)
523 {
524         int err;
525         u8 i2c_data;
526         struct sd *sd = (struct sd *) gspca_dev;
527
528         PDEBUG(D_V4L2, "Set auto white balance to %d", val);
529         err = m5602_read_sensor(sd, OV9650_COM8, &i2c_data, 1);
530         if (err < 0)
531                 return err;
532
533         i2c_data = ((i2c_data & 0xfd) | ((val & 0x01) << 1));
534         err = m5602_write_sensor(sd, OV9650_COM8, &i2c_data, 1);
535
536         return err;
537 }
538
539 int ov9650_get_auto_gain(struct gspca_dev *gspca_dev, __s32 *val)
540 {
541         int err;
542         u8 i2c_data;
543         struct sd *sd = (struct sd *) gspca_dev;
544
545         err = m5602_read_sensor(sd, OV9650_COM8, &i2c_data, 1);
546         *val = (i2c_data & OV9650_AGC_EN) >> 2;
547         PDEBUG(D_V4L2, "Read auto gain control %d", *val);
548
549         return err;
550 }
551
552 int ov9650_set_auto_gain(struct gspca_dev *gspca_dev, __s32 val)
553 {
554         int err;
555         u8 i2c_data;
556         struct sd *sd = (struct sd *) gspca_dev;
557
558         PDEBUG(D_V4L2, "Set auto gain control to %d", val);
559         err = m5602_read_sensor(sd, OV9650_COM8, &i2c_data, 1);
560         if (err < 0)
561                 return err;
562
563         i2c_data = ((i2c_data & 0xfb) | ((val & 0x01) << 2));
564         err = m5602_write_sensor(sd, OV9650_COM8, &i2c_data, 1);
565
566         return err;
567 }
568
569 static void ov9650_dump_registers(struct sd *sd)
570 {
571         int address;
572         info("Dumping the ov9650 register state");
573         for (address = 0; address < 0xa9; address++) {
574                 u8 value;
575                 m5602_read_sensor(sd, address, &value, 1);
576                 info("register 0x%x contains 0x%x",
577                      address, value);
578         }
579
580         info("ov9650 register state dump complete");
581
582         info("Probing for which registers that are read/write");
583         for (address = 0; address < 0xff; address++) {
584                 u8 old_value, ctrl_value;
585                 u8 test_value[2] = {0xff, 0xff};
586
587                 m5602_read_sensor(sd, address, &old_value, 1);
588                 m5602_write_sensor(sd, address, test_value, 1);
589                 m5602_read_sensor(sd, address, &ctrl_value, 1);
590
591                 if (ctrl_value == test_value[0])
592                         info("register 0x%x is writeable", address);
593                 else
594                         info("register 0x%x is read only", address);
595
596                 /* Restore original value */
597                 m5602_write_sensor(sd, address, &old_value, 1);
598         }
599 }