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