1 /* Shared Code for OmniVision Camera Chip Drivers
3 * Copyright (c) 2004 Mark McClelland <mark@alpha.dyndns.org>
4 * http://alpha.dyndns.org/ov511/
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.
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"
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"
24 #define PINFO(fmt, args...) printk(KERN_INFO "ovcamchip: " fmt "\n" , ## args);
25 #define PERROR(fmt, args...) printk(KERN_ERR "ovcamchip: " fmt "\n" , ## args);
28 int ovcamchip_debug = 0;
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");
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. */
39 module_param(mono, int, 0);
40 MODULE_PARM_DESC(mono,
41 "1=chips are monochrome (OVx1xx), 0=force color, -1=autodetect (default)");
43 MODULE_AUTHOR(DRIVER_AUTHOR);
44 MODULE_DESCRIPTION(DRIVER_DESC);
45 MODULE_LICENSE("GPL");
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 */
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;
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",
70 /* Forward declarations */
71 static struct i2c_driver driver;
72 static struct i2c_client client_template;
74 /* ----------------------------------------------------------------------- */
76 int ov_write_regvals(struct i2c_client *c, struct ovcamchip_regvals *rvals)
80 while (rvals->reg != 0xff) {
81 rc = ov_write(c, rvals->reg, rvals->val);
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".
95 int ov_write_mask(struct i2c_client *c,
101 unsigned char oldval, newval;
106 rc = ov_read(c, reg, &oldval);
110 oldval &= (~mask); /* Clear the masked bits */
111 value &= mask; /* Enforce mask on value */
112 newval = oldval | value; /* Set the desired bits */
115 return ov_write(c, reg, newval);
118 /* ----------------------------------------------------------------------- */
120 /* Reset the chip and ensure that I2C is synchronized. Returns <0 if failure.
122 static int init_camchip(struct i2c_client *c)
125 unsigned char high, low;
128 ov_write(c, 0x12, 0x80);
130 /* Wait for it to initialize */
131 set_current_state(TASK_UNINTERRUPTIBLE);
132 schedule_timeout(1 + 150 * HZ / 1000);
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) {
145 ov_write(c, 0x12, 0x80);
147 /* Wait for it to initialize */
148 set_current_state(TASK_UNINTERRUPTIBLE);
149 schedule_timeout(1 + 150 * HZ / 1000);
151 /* Dummy read to sync I2C */
152 ov_read(c, 0x00, &low);
158 PDEBUG(1, "I2C synced in %d attempt(s)", i);
163 /* This detects the OV7610, OV7620, or OV76BE chip. */
164 static int ov7xx0_detect(struct i2c_client *c)
166 struct ovcamchip *ov = i2c_get_clientdata(c);
172 /* Detect chip (sub)type */
173 rc = ov_read(c, GENERIC_REG_COM_I, &val);
175 PERROR("Error detecting ov7xx0 type");
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);
185 PERROR("Error detecting ov7xx0 type");
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
194 ov->subtype = CC_OV7620;
196 PINFO("Camera chip is an OV76BE");
197 ov->subtype = CC_OV76BE;
199 } else if ((val & 3) == 0) {
200 PINFO("Camera chip is an OV7620");
201 ov->subtype = CC_OV7620;
203 PERROR("Unknown camera chip version: %d", val & 3);
207 if (ov->subtype == CC_OV76BE)
208 ov->sops = &ov76be_ops;
209 else if (ov->subtype == CC_OV7620)
210 ov->sops = &ov7x20_ops;
212 ov->sops = &ov7x10_ops;
217 /* This detects the OV6620, OV6630, OV6630AE, or OV6630AF chip. */
218 static int ov6xx0_detect(struct i2c_client *c)
220 struct ovcamchip *ov = i2c_get_clientdata(c);
226 /* Detect chip (sub)type */
227 rc = ov_read(c, GENERIC_REG_COM_I, &val);
229 PERROR("Error detecting ov6xx0 type");
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");
247 if (ov->subtype == CC_OV6620)
248 ov->sops = &ov6x20_ops;
250 ov->sops = &ov6x30_ops;
255 static int ovcamchip_detect(struct i2c_client *c)
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. */
263 PDEBUG(3, "Testing for 0V7xx0");
264 c->addr = OV7xx0_SID;
265 if (init_camchip(c) < 0) {
267 PDEBUG(3, "Testing for 0V6xx0");
268 c->addr = OV6xx0_SID;
269 if (init_camchip(c) < 0) {
272 if (ov6xx0_detect(c) < 0) {
273 PERROR("Failed to init OV6xx0");
278 if (ov7xx0_detect(c) < 0) {
279 PERROR("Failed to init OV7xx0");
287 /* ----------------------------------------------------------------------- */
289 static int ovcamchip_attach(struct i2c_adapter *adap)
292 struct ovcamchip *ov;
293 struct i2c_client *c;
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. */
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);
307 PDEBUG(1, "Adapter ID 0x%06x rejected", adap->id);
311 c = kmalloc(sizeof *c, GFP_KERNEL);
316 memcpy(c, &client_template, sizeof *c);
318 strcpy(i2c_clientname(c), "OV????");
320 ov = kmalloc(sizeof *ov, GFP_KERNEL);
325 memset(ov, 0, sizeof *ov);
326 i2c_set_clientdata(c, ov);
328 rc = ovcamchip_detect(c);
332 strcpy(i2c_clientname(c), chip_names[ov->subtype]);
334 PDEBUG(1, "Camera chip detection complete");
336 i2c_attach_client(c);
344 PDEBUG(1, "returning %d", rc);
348 static int ovcamchip_detach(struct i2c_client *c)
350 struct ovcamchip *ov = i2c_get_clientdata(c);
353 rc = ov->sops->free(c);
357 i2c_detach_client(c);
364 static int ovcamchip_command(struct i2c_client *c, unsigned int cmd, void *arg)
366 struct ovcamchip *ov = i2c_get_clientdata(c);
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");
376 case OVCAMCHIP_CMD_Q_SUBTYPE:
378 *(int *)arg = ov->subtype;
381 case OVCAMCHIP_CMD_INITIALIZE:
386 ov->mono = *(int *)arg;
391 if (ov->subtype != CC_OV7620)
392 dev_warn(&c->dev, "Warning: Monochrome not "
393 "implemented for this chip\n");
395 dev_info(&c->dev, "Initializing chip as "
399 rc = ov->sops->init(c);
407 return ov->sops->command(c, cmd, arg);
411 /* ----------------------------------------------------------------------- */
413 static struct i2c_driver driver = {
414 .owner = THIS_MODULE,
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,
424 static struct i2c_client client_template = {
425 I2C_DEVNAME("(unset)"),
430 static int __init ovcamchip_init(void)
433 ovcamchip_debug = debug;
436 PINFO(DRIVER_VERSION " : " DRIVER_DESC);
437 return i2c_add_driver(&driver);
440 static void __exit ovcamchip_exit(void)
442 i2c_del_driver(&driver);
445 module_init(ovcamchip_init);
446 module_exit(ovcamchip_exit);