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