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