V4L/DVB (11413): gspca - m5602-mt9m111: Separate mode vectors per sensor.
[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 struct v4l2_pix_format ov9650_modes[] = {
72         {
73                 176,
74                 144,
75                 V4L2_PIX_FMT_SBGGR8,
76                 V4L2_FIELD_NONE,
77                 .sizeimage =
78                         176 * 144,
79                 .bytesperline = 176,
80                 .colorspace = V4L2_COLORSPACE_SRGB,
81                 .priv = 0
82         }, {
83                 320,
84                 240,
85                 V4L2_PIX_FMT_SBGGR8,
86                 V4L2_FIELD_NONE,
87                 .sizeimage =
88                         320 * 240,
89                 .bytesperline = 320,
90                 .colorspace = V4L2_COLORSPACE_SRGB,
91                 .priv = 0
92         }, {
93                 352,
94                 288,
95                 V4L2_PIX_FMT_SBGGR8,
96                 V4L2_FIELD_NONE,
97                 .sizeimage =
98                         352 * 288,
99                 .bytesperline = 352,
100                 .colorspace = V4L2_COLORSPACE_SRGB,
101                 .priv = 0
102         }, {
103                 640,
104                 480,
105                 V4L2_PIX_FMT_SBGGR8,
106                 V4L2_FIELD_NONE,
107                 .sizeimage =
108                         640 * 480,
109                 .bytesperline = 640,
110                 .colorspace = V4L2_COLORSPACE_SRGB,
111                 .priv = 0
112         }
113 };
114
115 static void ov9650_dump_registers(struct sd *sd);
116
117 int ov9650_probe(struct sd *sd)
118 {
119         u8 prod_id = 0, ver_id = 0, i;
120
121         if (force_sensor) {
122                 if (force_sensor == OV9650_SENSOR) {
123                         info("Forcing an %s sensor", ov9650.name);
124                         goto sensor_found;
125                 }
126                 /* If we want to force another sensor,
127                    don't try to probe this one */
128                 return -ENODEV;
129         }
130
131         info("Probing for an ov9650 sensor");
132
133         /* Run the pre-init to actually probe the unit */
134         for (i = 0; i < ARRAY_SIZE(preinit_ov9650); i++) {
135                 u8 data = preinit_ov9650[i][2];
136                 if (preinit_ov9650[i][0] == SENSOR)
137                         m5602_write_sensor(sd,
138                                             preinit_ov9650[i][1], &data, 1);
139                 else
140                         m5602_write_bridge(sd, preinit_ov9650[i][1], data);
141         }
142
143         if (m5602_read_sensor(sd, OV9650_PID, &prod_id, 1))
144                 return -ENODEV;
145
146         if (m5602_read_sensor(sd, OV9650_VER, &ver_id, 1))
147                 return -ENODEV;
148
149         if ((prod_id == 0x96) && (ver_id == 0x52)) {
150                 info("Detected an ov9650 sensor");
151                 goto sensor_found;
152         }
153
154         return -ENODEV;
155
156 sensor_found:
157 //      sd->gspca_dev.cam.cam_mode = ov9650.modes;
158 //      sd->gspca_dev.cam.nmodes = ov9650.nmodes;
159         sd->gspca_dev.cam.cam_mode = ov9650_modes;
160         sd->gspca_dev.cam.nmodes = ARRAY_SIZE(ov9650_modes);
161
162         sd->desc->ctrls = ov9650.ctrls;
163         sd->desc->nctrls = ARRAY_SIZE(ov9650_ctrls);
164         return 0;
165 }
166
167 int ov9650_init(struct sd *sd)
168 {
169         int i, err = 0;
170         u8 data;
171
172         if (dump_sensor)
173                 ov9650_dump_registers(sd);
174
175         for (i = 0; i < ARRAY_SIZE(init_ov9650) && !err; i++) {
176                 data = init_ov9650[i][2];
177                 if (init_ov9650[i][0] == SENSOR)
178                         err = m5602_write_sensor(sd, init_ov9650[i][1],
179                                                   &data, 1);
180                 else
181                         err = m5602_write_bridge(sd, init_ov9650[i][1], data);
182         }
183
184         if (dmi_check_system(ov9650_flip_dmi_table) && !err) {
185                 info("vflip quirk active");
186                 data = 0x30;
187                 err = m5602_write_sensor(sd, OV9650_MVFP, &data, 1);
188         }
189
190         return err;
191 }
192
193 int ov9650_start(struct sd *sd)
194 {
195         int i, err = 0;
196         struct cam *cam = &sd->gspca_dev.cam;
197
198         err = ov9650_init(sd);
199         if (err < 0)
200                 return err;
201
202         for (i = 0; i < ARRAY_SIZE(res_init_ov9650) && !err; i++) {
203                 if (res_init_ov9650[i][0] == BRIDGE)
204                         err = m5602_write_bridge(sd, res_init_ov9650[i][1],
205                                 res_init_ov9650[i][2]);
206                 else if (res_init_ov9650[i][0] == SENSOR) {
207                         u8 data = res_init_ov9650[i][2];
208                         err = m5602_write_sensor(sd,
209                                 res_init_ov9650[i][1], &data, 1);
210                 }
211         }
212         if (err < 0)
213                 return err;
214
215         switch (cam->cam_mode[sd->gspca_dev.curr_mode].width) {
216         case 640:
217                 PDEBUG(D_V4L2, "Configuring camera for VGA mode");
218
219                 for (i = 0; i < ARRAY_SIZE(VGA_ov9650) && !err; i++) {
220                         if (VGA_ov9650[i][0] == SENSOR) {
221                                 u8 data = VGA_ov9650[i][2];
222
223                                 err = m5602_write_sensor(sd,
224                                         VGA_ov9650[i][1], &data, 1);
225                         } else {
226                                 err = m5602_write_bridge(sd, VGA_ov9650[i][1],
227                                                 VGA_ov9650[i][2]);
228                         }
229                 }
230                 break;
231
232         case 352:
233                 PDEBUG(D_V4L2, "Configuring camera for CIF mode");
234
235                 for (i = 0; i < ARRAY_SIZE(CIF_ov9650) && !err; i++) {
236                         if (CIF_ov9650[i][0] == SENSOR) {
237                                 u8 data = CIF_ov9650[i][2];
238
239                                 err = m5602_write_sensor(sd,
240                                         CIF_ov9650[i][1], &data, 1);
241                         } else {
242                                 err = m5602_write_bridge(sd, CIF_ov9650[i][1],
243                                                 CIF_ov9650[i][2]);
244                         }
245                 }
246                 break;
247
248         case 320:
249                 PDEBUG(D_V4L2, "Configuring camera for QVGA mode");
250
251                 for (i = 0; i < ARRAY_SIZE(QVGA_ov9650) && !err; i++) {
252                         if (QVGA_ov9650[i][0] == SENSOR) {
253                                 u8 data = QVGA_ov9650[i][2];
254
255                                 err = m5602_write_sensor(sd,
256                                         QVGA_ov9650[i][1], &data, 1);
257                         } else {
258                                 err = m5602_write_bridge(sd, QVGA_ov9650[i][1],
259                                                 QVGA_ov9650[i][2]);
260                         }
261                 }
262                 break;
263
264         case 176:
265                 PDEBUG(D_V4L2, "Configuring camera for QCIF mode");
266
267                 for (i = 0; i < ARRAY_SIZE(QCIF_ov9650) && !err; i++) {
268                         if (QCIF_ov9650[i][0] == SENSOR) {
269                                 u8 data = QCIF_ov9650[i][2];
270                                 err = m5602_write_sensor(sd,
271                                         QCIF_ov9650[i][1], &data, 1);
272                         } else {
273                                 err = m5602_write_bridge(sd, QCIF_ov9650[i][1],
274                                                 QCIF_ov9650[i][2]);
275                         }
276                 }
277                 break;
278
279         }
280         return err;
281 }
282
283 int ov9650_stop(struct sd *sd)
284 {
285         u8 data = OV9650_SOFT_SLEEP | OV9650_OUTPUT_DRIVE_2X;
286         return m5602_write_sensor(sd, OV9650_COM2, &data, 1);
287 }
288
289 int ov9650_power_down(struct sd *sd)
290 {
291         int i, err = 0;
292         for (i = 0; i < ARRAY_SIZE(power_down_ov9650) && !err; i++) {
293                 u8 data = power_down_ov9650[i][2];
294                 if (power_down_ov9650[i][0] == SENSOR)
295                         err = m5602_write_sensor(sd,
296                                             power_down_ov9650[i][1], &data, 1);
297                 else
298                         err = m5602_write_bridge(sd, power_down_ov9650[i][1],
299                                                  data);
300         }
301
302         return err;
303 }
304
305 int ov9650_get_exposure(struct gspca_dev *gspca_dev, __s32 *val)
306 {
307         struct sd *sd = (struct sd *) gspca_dev;
308         u8 i2c_data;
309         int err;
310
311         err = m5602_read_sensor(sd, OV9650_COM1, &i2c_data, 1);
312         if (err < 0)
313                 return err;
314         *val = i2c_data & 0x03;
315
316         err = m5602_read_sensor(sd, OV9650_AECH, &i2c_data, 1);
317         if (err < 0)
318                 return err;
319         *val |= (i2c_data << 2);
320
321         err = m5602_read_sensor(sd, OV9650_AECHM, &i2c_data, 1);
322         if (err < 0)
323                 return err;
324         *val |= (i2c_data & 0x3f) << 10;
325
326         PDEBUG(D_V4L2, "Read exposure %d", *val);
327
328         return err;
329 }
330
331 int ov9650_set_exposure(struct gspca_dev *gspca_dev, __s32 val)
332 {
333         struct sd *sd = (struct sd *) gspca_dev;
334         u8 i2c_data;
335         int err;
336
337         PDEBUG(D_V4L2, "Set exposure to %d",
338                val & 0xffff);
339
340         /* The 6 MSBs */
341         i2c_data = (val >> 10) & 0x3f;
342         err = m5602_write_sensor(sd, OV9650_AECHM,
343                                   &i2c_data, 1);
344         if (err < 0)
345                 return err;
346
347         /* The 8 middle bits */
348         i2c_data = (val >> 2) & 0xff;
349         err = m5602_write_sensor(sd, OV9650_AECH,
350                                   &i2c_data, 1);
351         if (err < 0)
352                 return err;
353
354         /* The 2 LSBs */
355         i2c_data = val & 0x03;
356         err = m5602_write_sensor(sd, OV9650_COM1, &i2c_data, 1);
357
358         return err;
359 }
360
361 int ov9650_get_gain(struct gspca_dev *gspca_dev, __s32 *val)
362 {
363         int err;
364         u8 i2c_data;
365         struct sd *sd = (struct sd *) gspca_dev;
366
367         m5602_read_sensor(sd, OV9650_VREF, &i2c_data, 1);
368         *val = (i2c_data & 0x03) << 8;
369
370         err = m5602_read_sensor(sd, OV9650_GAIN, &i2c_data, 1);
371         *val |= i2c_data;
372         PDEBUG(D_V4L2, "Read gain %d", *val);
373         return err;
374 }
375
376 int ov9650_set_gain(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         /* The 2 MSB */
383         /* Read the OV9650_VREF register first to avoid
384            corrupting the VREF high and low bits */
385         m5602_read_sensor(sd, OV9650_VREF, &i2c_data, 1);
386         /* Mask away all uninteresting bits */
387         i2c_data = ((val & 0x0300) >> 2) |
388                         (i2c_data & 0x3F);
389         err = m5602_write_sensor(sd, OV9650_VREF, &i2c_data, 1);
390
391         /* The 8 LSBs */
392         i2c_data = val & 0xff;
393         err = m5602_write_sensor(sd, OV9650_GAIN, &i2c_data, 1);
394         return err;
395 }
396
397 int ov9650_get_red_balance(struct gspca_dev *gspca_dev, __s32 *val)
398 {
399         int err;
400         u8 i2c_data;
401         struct sd *sd = (struct sd *) gspca_dev;
402
403         err = m5602_read_sensor(sd, OV9650_RED, &i2c_data, 1);
404         *val = i2c_data;
405
406         PDEBUG(D_V4L2, "Read red gain %d", *val);
407
408         return err;
409 }
410
411 int ov9650_set_red_balance(struct gspca_dev *gspca_dev, __s32 val)
412 {
413         int err;
414         u8 i2c_data;
415         struct sd *sd = (struct sd *) gspca_dev;
416
417         PDEBUG(D_V4L2, "Set red gain to %d",
418                              val & 0xff);
419
420         i2c_data = val & 0xff;
421         err = m5602_write_sensor(sd, OV9650_RED, &i2c_data, 1);
422
423         return err;
424 }
425
426 int ov9650_get_blue_balance(struct gspca_dev *gspca_dev, __s32 *val)
427 {
428         int err;
429         u8 i2c_data;
430         struct sd *sd = (struct sd *) gspca_dev;
431
432         err = m5602_read_sensor(sd, OV9650_BLUE, &i2c_data, 1);
433         *val = i2c_data;
434
435         PDEBUG(D_V4L2, "Read blue gain %d", *val);
436
437         return err;
438 }
439
440 int ov9650_set_blue_balance(struct gspca_dev *gspca_dev, __s32 val)
441 {
442         int err;
443         u8 i2c_data;
444         struct sd *sd = (struct sd *) gspca_dev;
445
446         PDEBUG(D_V4L2, "Set blue gain to %d",
447                val & 0xff);
448
449         i2c_data = val & 0xff;
450         err = m5602_write_sensor(sd, OV9650_BLUE, &i2c_data, 1);
451
452         return err;
453 }
454
455 int ov9650_get_hflip(struct gspca_dev *gspca_dev, __s32 *val)
456 {
457         int err;
458         u8 i2c_data;
459         struct sd *sd = (struct sd *) gspca_dev;
460
461         err = m5602_read_sensor(sd, OV9650_MVFP, &i2c_data, 1);
462         if (dmi_check_system(ov9650_flip_dmi_table))
463                 *val = ((i2c_data & OV9650_HFLIP) >> 5) ? 0 : 1;
464         else
465                 *val = (i2c_data & OV9650_HFLIP) >> 5;
466         PDEBUG(D_V4L2, "Read horizontal flip %d", *val);
467
468         return err;
469 }
470
471 int ov9650_set_hflip(struct gspca_dev *gspca_dev, __s32 val)
472 {
473         int err;
474         u8 i2c_data;
475         struct sd *sd = (struct sd *) gspca_dev;
476
477         PDEBUG(D_V4L2, "Set horizontal flip to %d", val);
478         err = m5602_read_sensor(sd, OV9650_MVFP, &i2c_data, 1);
479         if (err < 0)
480                 return err;
481
482         if (dmi_check_system(ov9650_flip_dmi_table))
483                 i2c_data = ((i2c_data & 0xdf) |
484                            (((val ? 0 : 1) & 0x01) << 5));
485         else
486                 i2c_data = ((i2c_data & 0xdf) |
487                            ((val & 0x01) << 5));
488
489         err = m5602_write_sensor(sd, OV9650_MVFP, &i2c_data, 1);
490
491         return err;
492 }
493
494 int ov9650_get_vflip(struct gspca_dev *gspca_dev, __s32 *val)
495 {
496         int err;
497         u8 i2c_data;
498         struct sd *sd = (struct sd *) gspca_dev;
499
500         err = m5602_read_sensor(sd, OV9650_MVFP, &i2c_data, 1);
501         if (dmi_check_system(ov9650_flip_dmi_table))
502                 *val = ((i2c_data & 0x10) >> 4) ? 0 : 1;
503         else
504                 *val = (i2c_data & 0x10) >> 4;
505         PDEBUG(D_V4L2, "Read vertical flip %d", *val);
506
507         return err;
508 }
509
510 int ov9650_set_vflip(struct gspca_dev *gspca_dev, __s32 val)
511 {
512         int err;
513         u8 i2c_data;
514         struct sd *sd = (struct sd *) gspca_dev;
515
516         PDEBUG(D_V4L2, "Set vertical flip to %d", val);
517         err = m5602_read_sensor(sd, OV9650_MVFP, &i2c_data, 1);
518         if (err < 0)
519                 return err;
520
521         if (dmi_check_system(ov9650_flip_dmi_table))
522                 i2c_data = ((i2c_data & 0xef) |
523                                 (((val ? 0 : 1) & 0x01) << 4));
524         else
525                 i2c_data = ((i2c_data & 0xef) |
526                                 ((val & 0x01) << 4));
527
528         err = m5602_write_sensor(sd, OV9650_MVFP, &i2c_data, 1);
529
530         return err;
531 }
532
533 int ov9650_get_brightness(struct gspca_dev *gspca_dev, __s32 *val)
534 {
535         int err;
536         u8 i2c_data;
537         struct sd *sd = (struct sd *) gspca_dev;
538
539         err = m5602_read_sensor(sd, OV9650_VREF, &i2c_data, 1);
540         if (err < 0)
541                 return err;
542         *val = (i2c_data & 0x03) << 8;
543
544         err = m5602_read_sensor(sd, OV9650_GAIN, &i2c_data, 1);
545         *val |= i2c_data;
546         PDEBUG(D_V4L2, "Read gain %d", *val);
547
548         return err;
549 }
550
551 int ov9650_set_brightness(struct gspca_dev *gspca_dev, __s32 val)
552 {
553         int err;
554         u8 i2c_data;
555         struct sd *sd = (struct sd *) gspca_dev;
556
557         PDEBUG(D_V4L2, "Set gain to %d", val & 0x3ff);
558
559         /* Read the OV9650_VREF register first to avoid
560                 corrupting the VREF high and low bits */
561         err = m5602_read_sensor(sd, OV9650_VREF, &i2c_data, 1);
562         if (err < 0)
563                 return err;
564
565         /* Mask away all uninteresting bits */
566         i2c_data = ((val & 0x0300) >> 2) | (i2c_data & 0x3F);
567         err = m5602_write_sensor(sd, OV9650_VREF, &i2c_data, 1);
568         if (err < 0)
569                 return err;
570
571         /* The 8 LSBs */
572         i2c_data = val & 0xff;
573         err = m5602_write_sensor(sd, OV9650_GAIN, &i2c_data, 1);
574
575         return err;
576 }
577
578 int ov9650_get_auto_white_balance(struct gspca_dev *gspca_dev, __s32 *val)
579 {
580         int err;
581         u8 i2c_data;
582         struct sd *sd = (struct sd *) gspca_dev;
583
584         err = m5602_read_sensor(sd, OV9650_COM8, &i2c_data, 1);
585         *val = (i2c_data & OV9650_AWB_EN) >> 1;
586         PDEBUG(D_V4L2, "Read auto white balance %d", *val);
587
588         return err;
589 }
590
591 int ov9650_set_auto_white_balance(struct gspca_dev *gspca_dev, __s32 val)
592 {
593         int err;
594         u8 i2c_data;
595         struct sd *sd = (struct sd *) gspca_dev;
596
597         PDEBUG(D_V4L2, "Set auto white balance to %d", val);
598         err = m5602_read_sensor(sd, OV9650_COM8, &i2c_data, 1);
599         if (err < 0)
600                 return err;
601
602         i2c_data = ((i2c_data & 0xfd) | ((val & 0x01) << 1));
603         err = m5602_write_sensor(sd, OV9650_COM8, &i2c_data, 1);
604
605         return err;
606 }
607
608 int ov9650_get_auto_gain(struct gspca_dev *gspca_dev, __s32 *val)
609 {
610         int err;
611         u8 i2c_data;
612         struct sd *sd = (struct sd *) gspca_dev;
613
614         err = m5602_read_sensor(sd, OV9650_COM8, &i2c_data, 1);
615         *val = (i2c_data & OV9650_AGC_EN) >> 2;
616         PDEBUG(D_V4L2, "Read auto gain control %d", *val);
617
618         return err;
619 }
620
621 int ov9650_set_auto_gain(struct gspca_dev *gspca_dev, __s32 val)
622 {
623         int err;
624         u8 i2c_data;
625         struct sd *sd = (struct sd *) gspca_dev;
626
627         PDEBUG(D_V4L2, "Set auto gain control to %d", val);
628         err = m5602_read_sensor(sd, OV9650_COM8, &i2c_data, 1);
629         if (err < 0)
630                 return err;
631
632         i2c_data = ((i2c_data & 0xfb) | ((val & 0x01) << 2));
633         err = m5602_write_sensor(sd, OV9650_COM8, &i2c_data, 1);
634
635         return err;
636 }
637
638 static void ov9650_dump_registers(struct sd *sd)
639 {
640         int address;
641         info("Dumping the ov9650 register state");
642         for (address = 0; address < 0xa9; address++) {
643                 u8 value;
644                 m5602_read_sensor(sd, address, &value, 1);
645                 info("register 0x%x contains 0x%x",
646                      address, value);
647         }
648
649         info("ov9650 register state dump complete");
650
651         info("Probing for which registers that are read/write");
652         for (address = 0; address < 0xff; address++) {
653                 u8 old_value, ctrl_value;
654                 u8 test_value[2] = {0xff, 0xff};
655
656                 m5602_read_sensor(sd, address, &old_value, 1);
657                 m5602_write_sensor(sd, address, test_value, 1);
658                 m5602_read_sensor(sd, address, &ctrl_value, 1);
659
660                 if (ctrl_value == test_value[0])
661                         info("register 0x%x is writeable", address);
662                 else
663                         info("register 0x%x is read only", address);
664
665                 /* Restore original value */
666                 m5602_write_sensor(sd, address, &old_value, 1);
667         }
668 }