Import 2.2.8pre2
[davej-history.git] / drivers / char / i2c.c
blobc85b95bc3a1adf413792af3ae2df5b2bf7a1c0fa
1 /*
2 * Generic i2c interface for linux
4 * (c) 1998 Gerd Knorr <kraxel@cs.tu-berlin.de>
6 */
8 #include <linux/config.h>
9 #include <linux/module.h>
10 #include <linux/kernel.h>
11 #include <linux/errno.h>
12 #include <linux/types.h>
13 #include <linux/string.h>
14 #include <linux/delay.h>
15 #include <linux/locks.h>
16 #include <linux/sched.h>
17 #include <linux/malloc.h>
18 #include <linux/i2c.h>
20 #define REGPRINT(x) if (verbose) (x)
21 #define I2C_DEBUG(x) if (i2c_debug) (x)
23 static int scan = 0;
24 static int verbose = 0;
25 static int i2c_debug = 0;
27 #if LINUX_VERSION_CODE >= 0x020117
28 MODULE_PARM(scan,"i");
29 MODULE_PARM(verbose,"i");
30 MODULE_PARM(i2c_debug,"i");
31 #endif
33 /* ----------------------------------------------------------------------- */
35 static struct i2c_bus *busses[I2C_BUS_MAX];
36 static struct i2c_driver *drivers[I2C_DRIVER_MAX];
37 static int bus_count = 0, driver_count = 0;
39 #ifdef CONFIG_VIDEO_BT848
40 extern int i2c_tuner_init(void);
41 extern int msp3400c_init(void);
42 #endif
44 int i2c_init(void)
46 printk(KERN_INFO "i2c: initialized%s\n",
47 scan ? " (i2c bus scan enabled)" : "");
48 /* anything to do here ? */
49 #ifdef CONFIG_VIDEO_BT848
50 i2c_tuner_init();
51 msp3400c_init();
52 #endif
53 return 0;
56 /* ----------------------------------------------------------------------- */
58 static void i2c_attach_device(struct i2c_bus *bus, struct i2c_driver *driver)
60 struct i2c_device *device;
61 int i,j,ack=1;
62 unsigned char addr;
63 LOCK_FLAGS;
65 /* probe for device */
66 LOCK_I2C_BUS(bus);
67 for (addr = driver->addr_l; addr <= driver->addr_h; addr += 2)
69 i2c_start(bus);
70 ack = i2c_sendbyte(bus,addr,0);
71 i2c_stop(bus);
72 if (!ack)
73 break;
75 UNLOCK_I2C_BUS(bus);
76 if (ack)
77 return;
79 /* got answer */
80 for (i = 0; i < I2C_DEVICE_MAX; i++)
81 if (NULL == driver->devices[i])
82 break;
83 if (I2C_DEVICE_MAX == i)
84 return;
86 for (j = 0; j < I2C_DEVICE_MAX; j++)
87 if (NULL == bus->devices[j])
88 break;
89 if (I2C_DEVICE_MAX == j)
90 return;
92 if (NULL == (device = kmalloc(sizeof(struct i2c_device),GFP_KERNEL)))
93 return;
94 device->bus = bus;
95 device->driver = driver;
96 device->addr = addr;
98 /* Attach */
100 if (driver->attach(device)!=0)
102 kfree(device);
103 return;
105 driver->devices[i] = device;
106 driver->devcount++;
107 bus->devices[j] = device;
108 bus->devcount++;
110 if (bus->attach_inform)
111 bus->attach_inform(bus,driver->id);
112 REGPRINT(printk("i2c: device attached: %s (addr=0x%02x, bus=%s, driver=%s)\n",device->name,addr,bus->name,driver->name));
115 static void i2c_detach_device(struct i2c_device *device)
117 int i;
119 if (device->bus->detach_inform)
120 device->bus->detach_inform(device->bus,device->driver->id);
121 device->driver->detach(device);
123 for (i = 0; i < I2C_DEVICE_MAX; i++)
124 if (device == device->driver->devices[i])
125 break;
126 if (I2C_DEVICE_MAX == i)
128 printk(KERN_WARNING "i2c: detach_device #1: device not found: %s\n",
129 device->name);
130 return;
132 device->driver->devices[i] = NULL;
133 device->driver->devcount--;
135 for (i = 0; i < I2C_DEVICE_MAX; i++)
136 if (device == device->bus->devices[i])
137 break;
138 if (I2C_DEVICE_MAX == i)
140 printk(KERN_WARNING "i2c: detach_device #2: device not found: %s\n",
141 device->name);
142 return;
144 device->bus->devices[i] = NULL;
145 device->bus->devcount--;
147 REGPRINT(printk("i2c: device detached: %s (addr=0x%02x, bus=%s, driver=%s)\n",device->name,device->addr,device->bus->name,device->driver->name));
148 kfree(device);
151 /* ----------------------------------------------------------------------- */
153 int i2c_register_bus(struct i2c_bus *bus)
155 int i,ack;
156 LOCK_FLAGS;
158 memset(bus->devices,0,sizeof(bus->devices));
159 bus->devcount = 0;
161 for (i = 0; i < I2C_BUS_MAX; i++)
162 if (NULL == busses[i])
163 break;
164 if (I2C_BUS_MAX == i)
165 return -ENOMEM;
167 busses[i] = bus;
168 bus_count++;
169 REGPRINT(printk("i2c: bus registered: %s\n",bus->name));
171 MOD_INC_USE_COUNT;
173 if (scan)
175 /* scan whole i2c bus */
176 LOCK_I2C_BUS(bus);
177 for (i = 0; i < 256; i+=2)
179 i2c_start(bus);
180 ack = i2c_sendbyte(bus,i,0);
181 i2c_stop(bus);
182 if (!ack)
184 printk(KERN_INFO "i2c: scanning bus %s: found device at addr=0x%02x\n",
185 bus->name,i);
188 UNLOCK_I2C_BUS(bus);
191 /* probe available drivers */
192 for (i = 0; i < I2C_DRIVER_MAX; i++)
193 if (drivers[i])
194 i2c_attach_device(bus,drivers[i]);
195 return 0;
198 int i2c_unregister_bus(struct i2c_bus *bus)
200 int i;
202 /* detach devices */
203 for (i = 0; i < I2C_DEVICE_MAX; i++)
204 if (bus->devices[i])
205 i2c_detach_device(bus->devices[i]);
207 for (i = 0; i < I2C_BUS_MAX; i++)
208 if (bus == busses[i])
209 break;
210 if (I2C_BUS_MAX == i)
212 printk(KERN_WARNING "i2c: unregister_bus #1: bus not found: %s\n",
213 bus->name);
214 return -ENODEV;
217 MOD_DEC_USE_COUNT;
219 busses[i] = NULL;
220 bus_count--;
221 REGPRINT(printk("i2c: bus unregistered: %s\n",bus->name));
223 return 0;
226 /* ----------------------------------------------------------------------- */
228 int i2c_register_driver(struct i2c_driver *driver)
230 int i;
232 memset(driver->devices,0,sizeof(driver->devices));
233 driver->devcount = 0;
235 for (i = 0; i < I2C_DRIVER_MAX; i++)
236 if (NULL == drivers[i])
237 break;
238 if (I2C_DRIVER_MAX == i)
239 return -ENOMEM;
241 drivers[i] = driver;
242 driver_count++;
244 MOD_INC_USE_COUNT;
246 REGPRINT(printk("i2c: driver registered: %s\n",driver->name));
248 /* Probe available busses */
249 for (i = 0; i < I2C_BUS_MAX; i++)
250 if (busses[i])
251 i2c_attach_device(busses[i],driver);
253 return 0;
256 int i2c_unregister_driver(struct i2c_driver *driver)
258 int i;
260 /* detach devices */
261 for (i = 0; i < I2C_DEVICE_MAX; i++)
262 if (driver->devices[i])
263 i2c_detach_device(driver->devices[i]);
265 for (i = 0; i < I2C_DRIVER_MAX; i++)
266 if (driver == drivers[i])
267 break;
268 if (I2C_DRIVER_MAX == i)
270 printk(KERN_WARNING "i2c: unregister_driver: driver not found: %s\n",
271 driver->name);
272 return -ENODEV;
275 MOD_DEC_USE_COUNT;
277 drivers[i] = NULL;
278 driver_count--;
279 REGPRINT(printk("i2c: driver unregistered: %s\n",driver->name));
281 return 0;
284 /* ----------------------------------------------------------------------- */
286 int i2c_control_device(struct i2c_bus *bus, int id,
287 unsigned int cmd, void *arg)
289 int i;
291 for (i = 0; i < I2C_DEVICE_MAX; i++)
292 if (bus->devices[i] && bus->devices[i]->driver->id == id)
293 break;
294 if (i == I2C_DEVICE_MAX)
295 return -ENODEV;
296 if (NULL == bus->devices[i]->driver->command)
297 return -ENODEV;
298 return bus->devices[i]->driver->command(bus->devices[i],cmd,arg);
301 /* ----------------------------------------------------------------------- */
303 #define I2C_SET(bus,ctrl,data) (bus->i2c_setlines(bus,ctrl,data))
304 #define I2C_GET(bus) (bus->i2c_getdataline(bus))
306 void i2c_start(struct i2c_bus *bus)
308 I2C_SET(bus,0,1);
309 I2C_SET(bus,1,1);
310 I2C_SET(bus,1,0);
311 I2C_SET(bus,0,0);
312 I2C_DEBUG(printk("%s: < ",bus->name));
315 void i2c_stop(struct i2c_bus *bus)
317 I2C_SET(bus,0,0);
318 I2C_SET(bus,1,0);
319 I2C_SET(bus,1,1);
320 I2C_DEBUG(printk(">\n"));
323 void i2c_one(struct i2c_bus *bus)
325 I2C_SET(bus,0,1);
326 I2C_SET(bus,1,1);
327 I2C_SET(bus,0,1);
330 void i2c_zero(struct i2c_bus *bus)
332 I2C_SET(bus,0,0);
333 I2C_SET(bus,1,0);
334 I2C_SET(bus,0,0);
337 int i2c_ack(struct i2c_bus *bus)
339 int ack;
341 I2C_SET(bus,0,1);
342 I2C_SET(bus,1,1);
343 ack = I2C_GET(bus);
344 I2C_SET(bus,0,1);
345 return ack;
348 int i2c_sendbyte(struct i2c_bus *bus,unsigned char data,int wait_for_ack)
350 int i, ack;
352 I2C_SET(bus,0,0);
353 for (i=7; i>=0; i--)
354 (data&(1<<i)) ? i2c_one(bus) : i2c_zero(bus);
355 if (wait_for_ack)
356 udelay(wait_for_ack);
357 ack=i2c_ack(bus);
358 I2C_DEBUG(printk("%02x%c ",(int)data,ack?'-':'+'));
359 return ack;
362 unsigned char i2c_readbyte(struct i2c_bus *bus,int last)
364 int i;
365 unsigned char data=0;
367 I2C_SET(bus,0,1);
368 for (i=7; i>=0; i--)
370 I2C_SET(bus,1,1);
371 if (I2C_GET(bus))
372 data |= (1<<i);
373 I2C_SET(bus,0,1);
375 last ? i2c_one(bus) : i2c_zero(bus);
376 I2C_DEBUG(printk("=%02x%c ",(int)data,last?'-':'+'));
377 return data;
380 /* ----------------------------------------------------------------------- */
382 int i2c_read(struct i2c_bus *bus, unsigned char addr)
384 int ret;
386 if (bus->i2c_read)
387 return bus->i2c_read(bus, addr);
389 i2c_start(bus);
390 i2c_sendbyte(bus,addr,0);
391 ret = i2c_readbyte(bus,1);
392 i2c_stop(bus);
393 return ret;
396 int i2c_write(struct i2c_bus *bus, unsigned char addr,
397 unsigned char data1, unsigned char data2, int both)
399 int ack;
401 if (bus->i2c_write)
402 return bus->i2c_write(bus, addr, data1, data2, both);
404 i2c_start(bus);
405 i2c_sendbyte(bus,addr,0);
406 ack = i2c_sendbyte(bus,data1,0);
407 if (both)
408 ack = i2c_sendbyte(bus,data2,0);
409 i2c_stop(bus);
410 return ack ? -1 : 0 ;
413 /* ----------------------------------------------------------------------- */
415 #ifdef MODULE
417 #if LINUX_VERSION_CODE >= 0x020100
418 EXPORT_SYMBOL(i2c_register_bus);
419 EXPORT_SYMBOL(i2c_unregister_bus);
420 EXPORT_SYMBOL(i2c_register_driver);
421 EXPORT_SYMBOL(i2c_unregister_driver);
422 EXPORT_SYMBOL(i2c_control_device);
423 EXPORT_SYMBOL(i2c_start);
424 EXPORT_SYMBOL(i2c_stop);
425 EXPORT_SYMBOL(i2c_one);
426 EXPORT_SYMBOL(i2c_zero);
427 EXPORT_SYMBOL(i2c_ack);
428 EXPORT_SYMBOL(i2c_sendbyte);
429 EXPORT_SYMBOL(i2c_readbyte);
430 EXPORT_SYMBOL(i2c_read);
431 EXPORT_SYMBOL(i2c_write);
432 #endif
434 int init_module(void)
436 return i2c_init();
439 void cleanup_module(void)
442 #endif