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