This commit was manufactured by cvs2svn to create branch 'vserver'.
[linux-2.6.git] / drivers / media / video / ovcamchip / ovcamchip_core.c
1 /* Shared Code for OmniVision Camera Chip Drivers
2  *
3  * Copyright (c) 2004 Mark McClelland <mark@alpha.dyndns.org>
4  * http://alpha.dyndns.org/ov511/
5  *
6  * This program is free software; you can redistribute it and/or modify it
7  * under the terms of the GNU General Public License as published by the
8  * Free Software Foundation; either version 2 of the License, or (at your
9  * option) any later version. NO WARRANTY OF ANY KIND is expressed or implied.
10  */
11
12 #define DEBUG
13
14 #include <linux/init.h>
15 #include <linux/module.h>
16 #include <linux/moduleparam.h>
17 #include <linux/slab.h>
18 #include "ovcamchip_priv.h"
19
20 #define DRIVER_VERSION "v2.27 for Linux 2.6"
21 #define DRIVER_AUTHOR "Mark McClelland <mark@alpha.dyndns.org>"
22 #define DRIVER_DESC "OV camera chip I2C driver"
23
24 #define PINFO(fmt, args...) printk(KERN_INFO "ovcamchip: " fmt "\n" , ## args);
25 #define PERROR(fmt, args...) printk(KERN_ERR "ovcamchip: " fmt "\n" , ## args);
26
27 #ifdef DEBUG
28 int ovcamchip_debug = 0;
29 static int debug;
30 module_param(debug, int, 0);
31 MODULE_PARM_DESC(debug,
32   "Debug level: 0=none, 1=inits, 2=warning, 3=config, 4=functions, 5=all");
33 #endif
34
35 /* By default, let bridge driver tell us if chip is monochrome. mono=0
36  * will ignore that and always treat chips as color. mono=1 will force
37  * monochrome mode for all chips. */
38 static int mono = -1;
39 module_param(mono, int, 0);
40 MODULE_PARM_DESC(mono,
41   "1=chips are monochrome (OVx1xx), 0=force color, -1=autodetect (default)");
42
43 MODULE_AUTHOR(DRIVER_AUTHOR);
44 MODULE_DESCRIPTION(DRIVER_DESC);
45 MODULE_LICENSE("GPL");
46
47 /* Registers common to all chips, that are needed for detection */
48 #define GENERIC_REG_ID_HIGH       0x1C  /* manufacturer ID MSB */
49 #define GENERIC_REG_ID_LOW        0x1D  /* manufacturer ID LSB */
50 #define GENERIC_REG_COM_I         0x29  /* misc ID bits */
51
52 extern struct ovcamchip_ops ov6x20_ops;
53 extern struct ovcamchip_ops ov6x30_ops;
54 extern struct ovcamchip_ops ov7x10_ops;
55 extern struct ovcamchip_ops ov7x20_ops;
56 extern struct ovcamchip_ops ov76be_ops;
57
58 static char *chip_names[NUM_CC_TYPES] = {
59         [CC_UNKNOWN]    = "Unknown chip",
60         [CC_OV76BE]     = "OV76BE",
61         [CC_OV7610]     = "OV7610",
62         [CC_OV7620]     = "OV7620",
63         [CC_OV7620AE]   = "OV7620AE",
64         [CC_OV6620]     = "OV6620",
65         [CC_OV6630]     = "OV6630",
66         [CC_OV6630AE]   = "OV6630AE",
67         [CC_OV6630AF]   = "OV6630AF",
68 };
69
70 /* Forward declarations */
71 static struct i2c_driver driver;
72 static struct i2c_client client_template;
73
74 /* ----------------------------------------------------------------------- */
75
76 int ov_write_regvals(struct i2c_client *c, struct ovcamchip_regvals *rvals)
77 {
78         int rc;
79
80         while (rvals->reg != 0xff) {
81                 rc = ov_write(c, rvals->reg, rvals->val);
82                 if (rc < 0)
83                         return rc;
84                 rvals++;
85         }
86
87         return 0;
88 }
89
90 /* Writes bits at positions specified by mask to an I2C reg. Bits that are in
91  * the same position as 1's in "mask" are cleared and set to "value". Bits
92  * that are in the same position as 0's in "mask" are preserved, regardless
93  * of their respective state in "value".
94  */
95 int ov_write_mask(struct i2c_client *c,
96                   unsigned char reg,
97                   unsigned char value,
98                   unsigned char mask)
99 {
100         int rc;
101         unsigned char oldval, newval;
102
103         if (mask == 0xff) {
104                 newval = value;
105         } else {
106                 rc = ov_read(c, reg, &oldval);
107                 if (rc < 0)
108                         return rc;
109
110                 oldval &= (~mask);              /* Clear the masked bits */
111                 value &= mask;                  /* Enforce mask on value */
112                 newval = oldval | value;        /* Set the desired bits */
113         }
114
115         return ov_write(c, reg, newval);
116 }
117
118 /* ----------------------------------------------------------------------- */
119
120 /* Reset the chip and ensure that I2C is synchronized. Returns <0 if failure.
121  */
122 static int init_camchip(struct i2c_client *c)
123 {
124         int i, success;
125         unsigned char high, low;
126
127         /* Reset the chip */
128         ov_write(c, 0x12, 0x80);
129
130         /* Wait for it to initialize */
131         set_current_state(TASK_UNINTERRUPTIBLE);
132         schedule_timeout(1 + 150 * HZ / 1000);
133
134         for (i = 0, success = 0; i < I2C_DETECT_RETRIES && !success; i++) {
135                 if (ov_read(c, GENERIC_REG_ID_HIGH, &high) >= 0) {
136                         if (ov_read(c, GENERIC_REG_ID_LOW, &low) >= 0) {
137                                 if (high == 0x7F && low == 0xA2) {
138                                         success = 1;
139                                         continue;
140                                 }
141                         }
142                 }
143
144                 /* Reset the chip */
145                 ov_write(c, 0x12, 0x80);
146
147                 /* Wait for it to initialize */
148                 set_current_state(TASK_UNINTERRUPTIBLE);
149                 schedule_timeout(1 + 150 * HZ / 1000);
150
151                 /* Dummy read to sync I2C */
152                 ov_read(c, 0x00, &low);
153         }
154
155         if (!success)
156                 return -EIO;
157
158         PDEBUG(1, "I2C synced in %d attempt(s)", i);
159
160         return 0;
161 }
162
163 /* This detects the OV7610, OV7620, or OV76BE chip. */
164 static int ov7xx0_detect(struct i2c_client *c)
165 {
166         struct ovcamchip *ov = i2c_get_clientdata(c);
167         int rc;
168         unsigned char val;
169
170         PDEBUG(4, "");
171
172         /* Detect chip (sub)type */
173         rc = ov_read(c, GENERIC_REG_COM_I, &val);
174         if (rc < 0) {
175                 PERROR("Error detecting ov7xx0 type");
176                 return rc;
177         }
178
179         if ((val & 3) == 3) {
180                 PINFO("Camera chip is an OV7610");
181                 ov->subtype = CC_OV7610;
182         } else if ((val & 3) == 1) {
183                 rc = ov_read(c, 0x15, &val);
184                 if (rc < 0) {
185                         PERROR("Error detecting ov7xx0 type");
186                         return rc;
187                 }
188
189                 if (val & 1) {
190                         PINFO("Camera chip is an OV7620AE");
191                         /* OV7620 is a close enough match for now. There are
192                          * some definite differences though, so this should be
193                          * fixed */
194                         ov->subtype = CC_OV7620;
195                 } else {
196                         PINFO("Camera chip is an OV76BE");
197                         ov->subtype = CC_OV76BE;
198                 }
199         } else if ((val & 3) == 0) {
200                 PINFO("Camera chip is an OV7620");
201                 ov->subtype = CC_OV7620;
202         } else {
203                 PERROR("Unknown camera chip version: %d", val & 3);
204                 return -ENOSYS;
205         }
206
207         if (ov->subtype == CC_OV76BE)
208                 ov->sops = &ov76be_ops;
209         else if (ov->subtype == CC_OV7620)
210                 ov->sops = &ov7x20_ops;
211         else
212                 ov->sops = &ov7x10_ops;
213
214         return 0;
215 }
216
217 /* This detects the OV6620, OV6630, OV6630AE, or OV6630AF chip. */
218 static int ov6xx0_detect(struct i2c_client *c)
219 {
220         struct ovcamchip *ov = i2c_get_clientdata(c);
221         int rc;
222         unsigned char val;
223
224         PDEBUG(4, "");
225
226         /* Detect chip (sub)type */
227         rc = ov_read(c, GENERIC_REG_COM_I, &val);
228         if (rc < 0) {
229                 PERROR("Error detecting ov6xx0 type");
230                 return -1;
231         }
232
233         if ((val & 3) == 0) {
234                 ov->subtype = CC_OV6630;
235                 PINFO("Camera chip is an OV6630");
236         } else if ((val & 3) == 1) {
237                 ov->subtype = CC_OV6620;
238                 PINFO("Camera chip is an OV6620");
239         } else if ((val & 3) == 2) {
240                 ov->subtype = CC_OV6630;
241                 PINFO("Camera chip is an OV6630AE");
242         } else if ((val & 3) == 3) {
243                 ov->subtype = CC_OV6630;
244                 PINFO("Camera chip is an OV6630AF");
245         }
246
247         if (ov->subtype == CC_OV6620)
248                 ov->sops = &ov6x20_ops;
249         else
250                 ov->sops = &ov6x30_ops;
251
252         return 0;
253 }
254
255 static int ovcamchip_detect(struct i2c_client *c)
256 {
257         /* Ideally we would just try a single register write and see if it NAKs.
258          * That isn't possible since the OV518 can't report I2C transaction
259          * failures. So, we have to try to initialize the chip (i.e. reset it
260          * and check the ID registers) to detect its presence. */
261
262         /* Test for 7xx0 */
263         PDEBUG(3, "Testing for 0V7xx0");
264         c->addr = OV7xx0_SID;
265         if (init_camchip(c) < 0) {
266                 /* Test for 6xx0 */
267                 PDEBUG(3, "Testing for 0V6xx0");
268                 c->addr = OV6xx0_SID;
269                 if (init_camchip(c) < 0) {
270                         return -ENODEV;
271                 } else {
272                         if (ov6xx0_detect(c) < 0) {
273                                 PERROR("Failed to init OV6xx0");
274                                 return -EIO;
275                         }
276                 }
277         } else {
278                 if (ov7xx0_detect(c) < 0) {
279                         PERROR("Failed to init OV7xx0");
280                         return -EIO;
281                 }
282         }
283
284         return 0;
285 }
286
287 /* ----------------------------------------------------------------------- */
288
289 static int ovcamchip_attach(struct i2c_adapter *adap)
290 {
291         int rc = 0;
292         struct ovcamchip *ov;
293         struct i2c_client *c;
294
295         /* I2C is not a PnP bus, so we can never be certain that we're talking
296          * to the right chip. To prevent damage to EEPROMS and such, only
297          * attach to adapters that are known to contain OV camera chips. */
298
299         switch (adap->id) {
300         case (I2C_ALGO_SMBUS | I2C_HW_SMBUS_OV511):
301         case (I2C_ALGO_SMBUS | I2C_HW_SMBUS_OV518):
302         case (I2C_ALGO_SMBUS | I2C_HW_SMBUS_OVFX2):
303         case (I2C_ALGO_SMBUS | I2C_HW_SMBUS_W9968CF):
304                 PDEBUG(1, "Adapter ID 0x%06x accepted", adap->id);
305                 break;
306         default:
307                 PDEBUG(1, "Adapter ID 0x%06x rejected", adap->id);
308                 return -ENODEV;
309         }
310
311         c = kmalloc(sizeof *c, GFP_KERNEL);
312         if (!c) {
313                 rc = -ENOMEM;
314                 goto no_client;
315         }
316         memcpy(c, &client_template, sizeof *c);
317         c->adapter = adap;
318         strcpy(i2c_clientname(c), "OV????");
319
320         ov = kmalloc(sizeof *ov, GFP_KERNEL);
321         if (!ov) {
322                 rc = -ENOMEM;
323                 goto no_ov;
324         }
325         memset(ov, 0, sizeof *ov);
326         i2c_set_clientdata(c, ov);
327
328         rc = ovcamchip_detect(c);
329         if (rc < 0)
330                 goto error;
331
332         strcpy(i2c_clientname(c), chip_names[ov->subtype]);
333
334         PDEBUG(1, "Camera chip detection complete");
335
336         i2c_attach_client(c);
337
338         return rc;
339 error:
340         kfree(ov);
341 no_ov:
342         kfree(c);
343 no_client:
344         PDEBUG(1, "returning %d", rc);
345         return rc;
346 }
347
348 static int ovcamchip_detach(struct i2c_client *c)
349 {
350         struct ovcamchip *ov = i2c_get_clientdata(c);
351         int rc;
352
353         rc = ov->sops->free(c);
354         if (rc < 0)
355                 return rc;
356
357         i2c_detach_client(c);
358
359         kfree(ov);
360         kfree(c);
361         return 0;
362 }
363
364 static int ovcamchip_command(struct i2c_client *c, unsigned int cmd, void *arg)
365 {
366         struct ovcamchip *ov = i2c_get_clientdata(c);
367
368         if (!ov->initialized &&
369             cmd != OVCAMCHIP_CMD_Q_SUBTYPE &&
370             cmd != OVCAMCHIP_CMD_INITIALIZE) {
371                 dev_err(&c->dev, "ERROR: Camera chip not initialized yet!\n");
372                 return -EPERM;
373         }
374
375         switch (cmd) {
376         case OVCAMCHIP_CMD_Q_SUBTYPE:
377         {
378                 *(int *)arg = ov->subtype;
379                 return 0;
380         }
381         case OVCAMCHIP_CMD_INITIALIZE:
382         {
383                 int rc;
384
385                 if (mono == -1)
386                         ov->mono = *(int *)arg;
387                 else
388                         ov->mono = mono;
389
390                 if (ov->mono) {
391                         if (ov->subtype != CC_OV7620)
392                                 dev_warn(&c->dev, "Warning: Monochrome not "
393                                         "implemented for this chip\n");
394                         else
395                                 dev_info(&c->dev, "Initializing chip as "
396                                         "monochrome\n");
397                 }
398
399                 rc = ov->sops->init(c);
400                 if (rc < 0)
401                         return rc;
402
403                 ov->initialized = 1;
404                 return 0;
405         }
406         default:
407                 return ov->sops->command(c, cmd, arg);
408         }
409 }
410
411 /* ----------------------------------------------------------------------- */
412
413 static struct i2c_driver driver = {
414         .owner =                THIS_MODULE,
415         .name =                 "ovcamchip",
416         .id =                   I2C_DRIVERID_OVCAMCHIP,
417         .class =                I2C_CLASS_CAM_DIGITAL,
418         .flags =                I2C_DF_NOTIFY,
419         .attach_adapter =       ovcamchip_attach,
420         .detach_client =        ovcamchip_detach,
421         .command =              ovcamchip_command,
422 };
423
424 static struct i2c_client client_template = {
425         I2C_DEVNAME("(unset)"),
426         .id =           -1,
427         .driver =       &driver,
428 };
429
430 static int __init ovcamchip_init(void)
431 {
432 #ifdef DEBUG
433         ovcamchip_debug = debug;
434 #endif
435
436         PINFO(DRIVER_VERSION " : " DRIVER_DESC);
437         return i2c_add_driver(&driver);
438 }
439
440 static void __exit ovcamchip_exit(void)
441 {
442         i2c_del_driver(&driver);
443 }
444
445 module_init(ovcamchip_init);
446 module_exit(ovcamchip_exit);