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