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