V4L/DVB (10013): Convert all sensors to use the unified write sensor function
[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 };
63
64 static void ov9650_dump_registers(struct sd *sd);
65
66 int ov9650_read_sensor(struct sd *sd, const u8 address,
67                       u8 *i2c_data, const u8 len)
68 {
69         int err, i;
70
71         /* The ov9650 registers have a max depth of one byte */
72         if (len > 1 || !len)
73                 return -EINVAL;
74
75         do {
76                 err = m5602_read_bridge(sd, M5602_XB_I2C_STATUS, i2c_data);
77         } while ((*i2c_data & I2C_BUSY) && !err);
78
79         err = m5602_write_bridge(sd, M5602_XB_I2C_DEV_ADDR,
80                                  ov9650.i2c_slave_id);
81         if (err < 0)
82                 goto out;
83
84         err = m5602_write_bridge(sd, M5602_XB_I2C_REG_ADDR, address);
85         if (err < 0)
86                 goto out;
87
88         err = m5602_write_bridge(sd, M5602_XB_I2C_CTRL, 0x10 + len);
89         if (err < 0)
90                 goto out;
91
92         err = m5602_write_bridge(sd, M5602_XB_I2C_CTRL, 0x08);
93
94         for (i = 0; (i < len) && !err; i++) {
95                 err = m5602_read_bridge(sd, M5602_XB_I2C_DATA, &(i2c_data[i]));
96
97                 PDEBUG(D_CONF, "Reading sensor register "
98                                "0x%x containing 0x%x ", address, *i2c_data);
99         }
100
101 out:
102         return (err < 0) ? err : 0;
103 }
104
105 int ov9650_write_sensor(struct sd *sd, const u8 address,
106                         u8 *i2c_data, const u8 len)
107 {
108         int err, i;
109         u8 *p;
110         struct usb_device *udev = sd->gspca_dev.dev;
111         __u8 *buf = sd->gspca_dev.usb_buf;
112
113         /* The ov9650 only supports one byte writes */
114         if (len > 1 || !len)
115                 return -EINVAL;
116
117         memcpy(buf, sensor_urb_skeleton,
118                sizeof(sensor_urb_skeleton));
119
120         buf[11] = sd->sensor->i2c_slave_id;
121         buf[15] = address;
122
123         /* Special case larger sensor writes */
124         p = buf + 16;
125
126         /* Copy a four byte sequence for each byte to write over the I2C bus */
127         for (i = 0; i < len; i++) {
128                 memcpy(p, sensor_urb_skeleton + 16, 4);
129                 p[3] = i2c_data[i];
130                 p += 4;
131                 PDEBUG(D_CONF, "Writing sensor register 0x%x with 0x%x",
132                        address, i2c_data[i]);
133         }
134
135         /* Copy the tailer */
136         memcpy(p, sensor_urb_skeleton + 20, 4);
137
138         /* Set the total length */
139         p[3] = 0x10 + len;
140
141         err = usb_control_msg(udev, usb_sndctrlpipe(udev, 0),
142                               0x04, 0x40, 0x19,
143                               0x0000, buf,
144                               20 + len * 4, M5602_URB_MSG_TIMEOUT);
145
146         return (err < 0) ? err : 0;
147 }
148
149 int ov9650_probe(struct sd *sd)
150 {
151         u8 prod_id = 0, ver_id = 0, i;
152
153         if (force_sensor) {
154                 if (force_sensor == OV9650_SENSOR) {
155                         info("Forcing an %s sensor", ov9650.name);
156                         goto sensor_found;
157                 }
158                 /* If we want to force another sensor,
159                    don't try to probe this one */
160                 return -ENODEV;
161         }
162
163         info("Probing for an ov9650 sensor");
164
165         /* Run the pre-init to actually probe the unit */
166         for (i = 0; i < ARRAY_SIZE(preinit_ov9650); i++) {
167                 u8 data = preinit_ov9650[i][2];
168                 if (preinit_ov9650[i][0] == SENSOR)
169                         m5602_write_sensor(sd,
170                                             preinit_ov9650[i][1], &data, 1);
171                 else
172                         m5602_write_bridge(sd, preinit_ov9650[i][1], data);
173         }
174
175         if (ov9650_read_sensor(sd, OV9650_PID, &prod_id, 1))
176                 return -ENODEV;
177
178         if (ov9650_read_sensor(sd, OV9650_VER, &ver_id, 1))
179                 return -ENODEV;
180
181         if ((prod_id == 0x96) && (ver_id == 0x52)) {
182                 info("Detected an ov9650 sensor");
183                 goto sensor_found;
184         }
185
186         return -ENODEV;
187
188 sensor_found:
189         sd->gspca_dev.cam.cam_mode = ov9650.modes;
190         sd->gspca_dev.cam.nmodes = ov9650.nmodes;
191         sd->desc->ctrls = ov9650.ctrls;
192         sd->desc->nctrls = ov9650.nctrls;
193         return 0;
194 }
195
196 int ov9650_init(struct sd *sd)
197 {
198         int i, err = 0;
199         u8 data;
200
201         if (dump_sensor)
202                 ov9650_dump_registers(sd);
203
204         for (i = 0; i < ARRAY_SIZE(init_ov9650) && !err; i++) {
205                 data = init_ov9650[i][2];
206                 if (init_ov9650[i][0] == SENSOR)
207                         err = m5602_write_sensor(sd, init_ov9650[i][1],
208                                                   &data, 1);
209                 else
210                         err = m5602_write_bridge(sd, init_ov9650[i][1], data);
211         }
212
213         if (dmi_check_system(ov9650_flip_dmi_table) && !err) {
214                 info("vflip quirk active");
215                 data = 0x30;
216                 err = m5602_write_sensor(sd, OV9650_MVFP, &data, 1);
217         }
218
219         return err;
220 }
221
222 int ov9650_power_down(struct sd *sd)
223 {
224         int i, err = 0;
225         for (i = 0; i < ARRAY_SIZE(power_down_ov9650) && !err; i++) {
226                 u8 data = power_down_ov9650[i][2];
227                 if (power_down_ov9650[i][0] == SENSOR)
228                         err = m5602_write_sensor(sd,
229                                             power_down_ov9650[i][1], &data, 1);
230                 else
231                         err = m5602_write_bridge(sd, power_down_ov9650[i][1],
232                                                  data);
233         }
234
235         return err;
236 }
237
238 int ov9650_get_exposure(struct gspca_dev *gspca_dev, __s32 *val)
239 {
240         struct sd *sd = (struct sd *) gspca_dev;
241         u8 i2c_data;
242         int err;
243
244         err = ov9650_read_sensor(sd, OV9650_COM1, &i2c_data, 1);
245         if (err < 0)
246                 goto out;
247         *val = i2c_data & 0x03;
248
249         err = ov9650_read_sensor(sd, OV9650_AECH, &i2c_data, 1);
250         if (err < 0)
251                 goto out;
252         *val |= (i2c_data << 2);
253
254         err = ov9650_read_sensor(sd, OV9650_AECHM, &i2c_data, 1);
255         if (err < 0)
256                 goto out;
257         *val |= (i2c_data & 0x3f) << 10;
258
259         PDEBUG(D_V4L2, "Read exposure %d", *val);
260 out:
261         return err;
262 }
263
264 int ov9650_set_exposure(struct gspca_dev *gspca_dev, __s32 val)
265 {
266         struct sd *sd = (struct sd *) gspca_dev;
267         u8 i2c_data;
268         int err;
269
270         PDEBUG(D_V4L2, "Set exposure to %d",
271                val & 0xffff);
272
273         /* The 6 MSBs */
274         i2c_data = (val >> 10) & 0x3f;
275         err = m5602_write_sensor(sd, OV9650_AECHM,
276                                   &i2c_data, 1);
277         if (err < 0)
278                 goto out;
279
280         /* The 8 middle bits */
281         i2c_data = (val >> 2) & 0xff;
282         err = m5602_write_sensor(sd, OV9650_AECH,
283                                   &i2c_data, 1);
284         if (err < 0)
285                 goto out;
286
287         /* The 2 LSBs */
288         i2c_data = val & 0x03;
289         err = m5602_write_sensor(sd, OV9650_COM1, &i2c_data, 1);
290
291 out:
292         return err;
293 }
294
295 int ov9650_get_gain(struct gspca_dev *gspca_dev, __s32 *val)
296 {
297         int err;
298         u8 i2c_data;
299         struct sd *sd = (struct sd *) gspca_dev;
300
301         ov9650_read_sensor(sd, OV9650_VREF, &i2c_data, 1);
302         *val = (i2c_data & 0x03) << 8;
303
304         err = ov9650_read_sensor(sd, OV9650_GAIN, &i2c_data, 1);
305         *val |= i2c_data;
306         PDEBUG(D_V4L2, "Read gain %d", *val);
307         return err;
308 }
309
310 int ov9650_set_gain(struct gspca_dev *gspca_dev, __s32 val)
311 {
312         int err;
313         u8 i2c_data;
314         struct sd *sd = (struct sd *) gspca_dev;
315
316         /* The 2 MSB */
317         /* Read the OV9650_VREF register first to avoid
318            corrupting the VREF high and low bits */
319         ov9650_read_sensor(sd, OV9650_VREF, &i2c_data, 1);
320         /* Mask away all uninteresting bits */
321         i2c_data = ((val & 0x0300) >> 2) |
322                         (i2c_data & 0x3F);
323         err = m5602_write_sensor(sd, OV9650_VREF, &i2c_data, 1);
324
325         /* The 8 LSBs */
326         i2c_data = val & 0xff;
327         err = m5602_write_sensor(sd, OV9650_GAIN, &i2c_data, 1);
328         return err;
329 }
330
331 int ov9650_get_red_balance(struct gspca_dev *gspca_dev, __s32 *val)
332 {
333         int err;
334         u8 i2c_data;
335         struct sd *sd = (struct sd *) gspca_dev;
336
337         err = ov9650_read_sensor(sd, OV9650_RED, &i2c_data, 1);
338         *val = i2c_data;
339
340         PDEBUG(D_V4L2, "Read red gain %d", *val);
341
342         return err;
343 }
344
345 int ov9650_set_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         PDEBUG(D_V4L2, "Set red gain to %d",
352                              val & 0xff);
353
354         i2c_data = val & 0xff;
355         err = m5602_write_sensor(sd, OV9650_RED, &i2c_data, 1);
356
357         return err;
358 }
359
360 int ov9650_get_blue_balance(struct gspca_dev *gspca_dev, __s32 *val)
361 {
362         int err;
363         u8 i2c_data;
364         struct sd *sd = (struct sd *) gspca_dev;
365
366         err = ov9650_read_sensor(sd, OV9650_BLUE, &i2c_data, 1);
367         *val = i2c_data;
368
369         PDEBUG(D_V4L2, "Read blue gain %d", *val);
370
371         return err;
372 }
373
374 int ov9650_set_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         PDEBUG(D_V4L2, "Set blue gain to %d",
381                val & 0xff);
382
383         i2c_data = val & 0xff;
384         err = m5602_write_sensor(sd, OV9650_BLUE, &i2c_data, 1);
385
386         return err;
387 }
388
389 int ov9650_get_hflip(struct gspca_dev *gspca_dev, __s32 *val)
390 {
391         int err;
392         u8 i2c_data;
393         struct sd *sd = (struct sd *) gspca_dev;
394
395         err = ov9650_read_sensor(sd, OV9650_MVFP, &i2c_data, 1);
396         if (dmi_check_system(ov9650_flip_dmi_table))
397                 *val = ((i2c_data & OV9650_HFLIP) >> 5) ? 0 : 1;
398         else
399                 *val = (i2c_data & OV9650_HFLIP) >> 5;
400         PDEBUG(D_V4L2, "Read horizontal flip %d", *val);
401
402         return err;
403 }
404
405 int ov9650_set_hflip(struct gspca_dev *gspca_dev, __s32 val)
406 {
407         int err;
408         u8 i2c_data;
409         struct sd *sd = (struct sd *) gspca_dev;
410
411         PDEBUG(D_V4L2, "Set horizontal flip to %d", val);
412         err = ov9650_read_sensor(sd, OV9650_MVFP, &i2c_data, 1);
413         if (err < 0)
414                 goto out;
415
416         if (dmi_check_system(ov9650_flip_dmi_table))
417                 i2c_data = ((i2c_data & 0xdf) |
418                            (((val ? 0 : 1) & 0x01) << 5));
419         else
420                 i2c_data = ((i2c_data & 0xdf) |
421                            ((val & 0x01) << 5));
422
423         err = m5602_write_sensor(sd, OV9650_MVFP, &i2c_data, 1);
424 out:
425         return err;
426 }
427
428 int ov9650_get_vflip(struct gspca_dev *gspca_dev, __s32 *val)
429 {
430         int err;
431         u8 i2c_data;
432         struct sd *sd = (struct sd *) gspca_dev;
433
434         err = ov9650_read_sensor(sd, OV9650_MVFP, &i2c_data, 1);
435         if (dmi_check_system(ov9650_flip_dmi_table))
436                 *val = ((i2c_data & 0x10) >> 4) ? 0 : 1;
437         else
438                 *val = (i2c_data & 0x10) >> 4;
439         PDEBUG(D_V4L2, "Read vertical flip %d", *val);
440
441         return err;
442 }
443
444 int ov9650_set_vflip(struct gspca_dev *gspca_dev, __s32 val)
445 {
446         int err;
447         u8 i2c_data;
448         struct sd *sd = (struct sd *) gspca_dev;
449
450         PDEBUG(D_V4L2, "Set vertical flip to %d", val);
451         err = ov9650_read_sensor(sd, OV9650_MVFP, &i2c_data, 1);
452         if (err < 0)
453                 goto out;
454
455         if (dmi_check_system(ov9650_flip_dmi_table))
456                 i2c_data = ((i2c_data & 0xef) |
457                                 (((val ? 0 : 1) & 0x01) << 4));
458         else
459                 i2c_data = ((i2c_data & 0xef) |
460                                 ((val & 0x01) << 4));
461
462         err = m5602_write_sensor(sd, OV9650_MVFP, &i2c_data, 1);
463 out:
464         return err;
465 }
466
467 int ov9650_get_brightness(struct gspca_dev *gspca_dev, __s32 *val)
468 {
469         int err;
470         u8 i2c_data;
471         struct sd *sd = (struct sd *) gspca_dev;
472
473         err = ov9650_read_sensor(sd, OV9650_VREF, &i2c_data, 1);
474         if (err < 0)
475                 goto out;
476         *val = (i2c_data & 0x03) << 8;
477
478         err = ov9650_read_sensor(sd, OV9650_GAIN, &i2c_data, 1);
479         *val |= i2c_data;
480         PDEBUG(D_V4L2, "Read gain %d", *val);
481 out:
482         return err;
483 }
484
485 int ov9650_set_brightness(struct gspca_dev *gspca_dev, __s32 val)
486 {
487         int err;
488         u8 i2c_data;
489         struct sd *sd = (struct sd *) gspca_dev;
490
491         PDEBUG(D_V4L2, "Set gain to %d", val & 0x3ff);
492
493         /* Read the OV9650_VREF register first to avoid
494                 corrupting the VREF high and low bits */
495         err = ov9650_read_sensor(sd, OV9650_VREF, &i2c_data, 1);
496         if (err < 0)
497                 goto out;
498
499         /* Mask away all uninteresting bits */
500         i2c_data = ((val & 0x0300) >> 2) | (i2c_data & 0x3F);
501         err = m5602_write_sensor(sd, OV9650_VREF, &i2c_data, 1);
502         if (err < 0)
503                 goto out;
504
505         /* The 8 LSBs */
506         i2c_data = val & 0xff;
507         err = m5602_write_sensor(sd, OV9650_GAIN, &i2c_data, 1);
508
509 out:
510         return err;
511 }
512
513 int ov9650_get_auto_white_balance(struct gspca_dev *gspca_dev, __s32 *val)
514 {
515         int err;
516         u8 i2c_data;
517         struct sd *sd = (struct sd *) gspca_dev;
518
519         err = ov9650_read_sensor(sd, OV9650_COM8, &i2c_data, 1);
520         *val = (i2c_data & OV9650_AWB_EN) >> 1;
521         PDEBUG(D_V4L2, "Read auto white balance %d", *val);
522
523         return err;
524 }
525
526 int ov9650_set_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         PDEBUG(D_V4L2, "Set auto white balance to %d", val);
533         err = ov9650_read_sensor(sd, OV9650_COM8, &i2c_data, 1);
534         if (err < 0)
535                 goto out;
536
537         i2c_data = ((i2c_data & 0xfd) | ((val & 0x01) << 1));
538         err = m5602_write_sensor(sd, OV9650_COM8, &i2c_data, 1);
539 out:
540         return err;
541 }
542
543 int ov9650_get_auto_gain(struct gspca_dev *gspca_dev, __s32 *val)
544 {
545         int err;
546         u8 i2c_data;
547         struct sd *sd = (struct sd *) gspca_dev;
548
549         err = ov9650_read_sensor(sd, OV9650_COM8, &i2c_data, 1);
550         *val = (i2c_data & OV9650_AGC_EN) >> 2;
551         PDEBUG(D_V4L2, "Read auto gain control %d", *val);
552
553         return err;
554 }
555
556 int ov9650_set_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         PDEBUG(D_V4L2, "Set auto gain control to %d", val);
563         err = ov9650_read_sensor(sd, OV9650_COM8, &i2c_data, 1);
564         if (err < 0)
565                 goto out;
566
567         i2c_data = ((i2c_data & 0xfb) | ((val & 0x01) << 2));
568         err = m5602_write_sensor(sd, OV9650_COM8, &i2c_data, 1);
569 out:
570         return err;
571 }
572
573 static void ov9650_dump_registers(struct sd *sd)
574 {
575         int address;
576         info("Dumping the ov9650 register state");
577         for (address = 0; address < 0xa9; address++) {
578                 u8 value;
579                 ov9650_read_sensor(sd, address, &value, 1);
580                 info("register 0x%x contains 0x%x",
581                      address, value);
582         }
583
584         info("ov9650 register state dump complete");
585
586         info("Probing for which registers that are read/write");
587         for (address = 0; address < 0xff; address++) {
588                 u8 old_value, ctrl_value;
589                 u8 test_value[2] = {0xff, 0xff};
590
591                 ov9650_read_sensor(sd, address, &old_value, 1);
592                 m5602_write_sensor(sd, address, test_value, 1);
593                 ov9650_read_sensor(sd, address, &ctrl_value, 1);
594
595                 if (ctrl_value == test_value[0])
596                         info("register 0x%x is writeable", address);
597                 else
598                         info("register 0x%x is read only", address);
599
600                 /* Restore original value */
601                 m5602_write_sensor(sd, address, &old_value, 1);
602         }
603 }