Portability cleanup as required by Linus.
[linux-2.6/linux-mips.git] / drivers / char / i2c-old.c
blobbd9750fc33642e2d96740579255cbcd825728f8e
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-old.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
43 #ifdef CONFIG_VIDEO_BUZ
44 extern int saa7111_init(void);
45 extern int saa7185_init(void);
46 #endif
47 #ifdef CONFIG_VIDEO_LML33
48 extern int bt819_init(void);
49 extern int bt856_init(void);
50 #endif
52 int i2c_init(void)
54 printk(KERN_INFO "i2c: initialized%s\n",
55 scan ? " (i2c bus scan enabled)" : "");
56 /* anything to do here ? */
57 #ifdef CONFIG_VIDEO_BT848
58 i2c_tuner_init();
59 msp3400c_init();
60 #endif
61 #ifdef CONFIG_VIDEO_BUZ
62 saa7111_init();
63 saa7185_init();
64 #endif
65 #ifdef CONFIG_VIDEO_LML33
66 bt819_init();
67 bt856_init();
68 #endif
69 return 0;
72 /* ----------------------------------------------------------------------- */
74 static void i2c_attach_device(struct i2c_bus *bus, struct i2c_driver *driver)
76 struct i2c_device *device;
77 int i,j,ack=1;
78 unsigned char addr;
79 LOCK_FLAGS;
81 /* probe for device */
82 LOCK_I2C_BUS(bus);
83 for (addr = driver->addr_l; addr <= driver->addr_h; addr += 2)
85 i2c_start(bus);
86 ack = i2c_sendbyte(bus,addr,0);
87 i2c_stop(bus);
88 if (!ack)
89 break;
91 UNLOCK_I2C_BUS(bus);
92 if (ack)
93 return;
95 /* got answer */
96 for (i = 0; i < I2C_DEVICE_MAX; i++)
97 if (NULL == driver->devices[i])
98 break;
99 if (I2C_DEVICE_MAX == i)
100 return;
102 for (j = 0; j < I2C_DEVICE_MAX; j++)
103 if (NULL == bus->devices[j])
104 break;
105 if (I2C_DEVICE_MAX == j)
106 return;
108 if (NULL == (device = kmalloc(sizeof(struct i2c_device),GFP_KERNEL)))
109 return;
110 device->bus = bus;
111 device->driver = driver;
112 device->addr = addr;
114 /* Attach */
116 if (driver->attach(device)!=0)
118 kfree(device);
119 return;
121 driver->devices[i] = device;
122 driver->devcount++;
123 bus->devices[j] = device;
124 bus->devcount++;
126 if (bus->attach_inform)
127 bus->attach_inform(bus,driver->id);
128 REGPRINT(printk("i2c: device attached: %s (addr=0x%02x, bus=%s, driver=%s)\n",device->name,addr,bus->name,driver->name));
131 static void i2c_detach_device(struct i2c_device *device)
133 int i;
135 if (device->bus->detach_inform)
136 device->bus->detach_inform(device->bus,device->driver->id);
137 device->driver->detach(device);
139 for (i = 0; i < I2C_DEVICE_MAX; i++)
140 if (device == device->driver->devices[i])
141 break;
142 if (I2C_DEVICE_MAX == i)
144 printk(KERN_WARNING "i2c: detach_device #1: device not found: %s\n",
145 device->name);
146 return;
148 device->driver->devices[i] = NULL;
149 device->driver->devcount--;
151 for (i = 0; i < I2C_DEVICE_MAX; i++)
152 if (device == device->bus->devices[i])
153 break;
154 if (I2C_DEVICE_MAX == i)
156 printk(KERN_WARNING "i2c: detach_device #2: device not found: %s\n",
157 device->name);
158 return;
160 device->bus->devices[i] = NULL;
161 device->bus->devcount--;
163 REGPRINT(printk("i2c: device detached: %s (addr=0x%02x, bus=%s, driver=%s)\n",device->name,device->addr,device->bus->name,device->driver->name));
164 kfree(device);
167 /* ----------------------------------------------------------------------- */
169 int i2c_register_bus(struct i2c_bus *bus)
171 int i,ack;
172 LOCK_FLAGS;
174 memset(bus->devices,0,sizeof(bus->devices));
175 bus->devcount = 0;
177 for (i = 0; i < I2C_BUS_MAX; i++)
178 if (NULL == busses[i])
179 break;
180 if (I2C_BUS_MAX == i)
181 return -ENOMEM;
183 busses[i] = bus;
184 bus_count++;
185 REGPRINT(printk("i2c: bus registered: %s\n",bus->name));
187 MOD_INC_USE_COUNT;
189 if (scan)
191 /* scan whole i2c bus */
192 LOCK_I2C_BUS(bus);
193 for (i = 0; i < 256; i+=2)
195 i2c_start(bus);
196 ack = i2c_sendbyte(bus,i,0);
197 i2c_stop(bus);
198 if (!ack)
200 printk(KERN_INFO "i2c: scanning bus %s: found device at addr=0x%02x\n",
201 bus->name,i);
204 UNLOCK_I2C_BUS(bus);
207 /* probe available drivers */
208 for (i = 0; i < I2C_DRIVER_MAX; i++)
209 if (drivers[i])
210 i2c_attach_device(bus,drivers[i]);
211 return 0;
214 int i2c_unregister_bus(struct i2c_bus *bus)
216 int i;
218 /* detach devices */
219 for (i = 0; i < I2C_DEVICE_MAX; i++)
220 if (bus->devices[i])
221 i2c_detach_device(bus->devices[i]);
223 for (i = 0; i < I2C_BUS_MAX; i++)
224 if (bus == busses[i])
225 break;
226 if (I2C_BUS_MAX == i)
228 printk(KERN_WARNING "i2c: unregister_bus #1: bus not found: %s\n",
229 bus->name);
230 return -ENODEV;
233 MOD_DEC_USE_COUNT;
235 busses[i] = NULL;
236 bus_count--;
237 REGPRINT(printk("i2c: bus unregistered: %s\n",bus->name));
239 return 0;
242 /* ----------------------------------------------------------------------- */
244 int i2c_register_driver(struct i2c_driver *driver)
246 int i;
248 memset(driver->devices,0,sizeof(driver->devices));
249 driver->devcount = 0;
251 for (i = 0; i < I2C_DRIVER_MAX; i++)
252 if (NULL == drivers[i])
253 break;
254 if (I2C_DRIVER_MAX == i)
255 return -ENOMEM;
257 drivers[i] = driver;
258 driver_count++;
260 MOD_INC_USE_COUNT;
262 REGPRINT(printk("i2c: driver registered: %s\n",driver->name));
264 /* Probe available busses */
265 for (i = 0; i < I2C_BUS_MAX; i++)
266 if (busses[i])
267 i2c_attach_device(busses[i],driver);
269 return 0;
272 int i2c_unregister_driver(struct i2c_driver *driver)
274 int i;
276 /* detach devices */
277 for (i = 0; i < I2C_DEVICE_MAX; i++)
278 if (driver->devices[i])
279 i2c_detach_device(driver->devices[i]);
281 for (i = 0; i < I2C_DRIVER_MAX; i++)
282 if (driver == drivers[i])
283 break;
284 if (I2C_DRIVER_MAX == i)
286 printk(KERN_WARNING "i2c: unregister_driver: driver not found: %s\n",
287 driver->name);
288 return -ENODEV;
291 MOD_DEC_USE_COUNT;
293 drivers[i] = NULL;
294 driver_count--;
295 REGPRINT(printk("i2c: driver unregistered: %s\n",driver->name));
297 return 0;
300 /* ----------------------------------------------------------------------- */
302 int i2c_control_device(struct i2c_bus *bus, int id,
303 unsigned int cmd, void *arg)
305 int i;
307 for (i = 0; i < I2C_DEVICE_MAX; i++)
308 if (bus->devices[i] && bus->devices[i]->driver->id == id)
309 break;
310 if (i == I2C_DEVICE_MAX)
311 return -ENODEV;
312 if (NULL == bus->devices[i]->driver->command)
313 return -ENODEV;
314 return bus->devices[i]->driver->command(bus->devices[i],cmd,arg);
317 /* ----------------------------------------------------------------------- */
319 #define I2C_SET(bus,ctrl,data) (bus->i2c_setlines(bus,ctrl,data))
320 #define I2C_GET(bus) (bus->i2c_getdataline(bus))
322 void i2c_start(struct i2c_bus *bus)
324 I2C_SET(bus,0,1);
325 I2C_SET(bus,1,1);
326 I2C_SET(bus,1,0);
327 I2C_SET(bus,0,0);
328 I2C_DEBUG(printk("%s: < ",bus->name));
331 void i2c_stop(struct i2c_bus *bus)
333 I2C_SET(bus,0,0);
334 I2C_SET(bus,1,0);
335 I2C_SET(bus,1,1);
336 I2C_DEBUG(printk(">\n"));
339 void i2c_one(struct i2c_bus *bus)
341 I2C_SET(bus,0,1);
342 I2C_SET(bus,1,1);
343 I2C_SET(bus,0,1);
346 void i2c_zero(struct i2c_bus *bus)
348 I2C_SET(bus,0,0);
349 I2C_SET(bus,1,0);
350 I2C_SET(bus,0,0);
353 int i2c_ack(struct i2c_bus *bus)
355 int ack;
357 I2C_SET(bus,0,1);
358 I2C_SET(bus,1,1);
359 ack = I2C_GET(bus);
360 I2C_SET(bus,0,1);
361 return ack;
364 int i2c_sendbyte(struct i2c_bus *bus,unsigned char data,int wait_for_ack)
366 int i, ack;
368 I2C_SET(bus,0,0);
369 for (i=7; i>=0; i--)
370 (data&(1<<i)) ? i2c_one(bus) : i2c_zero(bus);
371 if (wait_for_ack)
372 udelay(wait_for_ack);
373 ack=i2c_ack(bus);
374 I2C_DEBUG(printk("%02x%c ",(int)data,ack?'-':'+'));
375 return ack;
378 unsigned char i2c_readbyte(struct i2c_bus *bus,int last)
380 int i;
381 unsigned char data=0;
383 I2C_SET(bus,0,1);
384 for (i=7; i>=0; i--)
386 I2C_SET(bus,1,1);
387 if (I2C_GET(bus))
388 data |= (1<<i);
389 I2C_SET(bus,0,1);
391 last ? i2c_one(bus) : i2c_zero(bus);
392 I2C_DEBUG(printk("=%02x%c ",(int)data,last?'-':'+'));
393 return data;
396 /* ----------------------------------------------------------------------- */
398 int i2c_read(struct i2c_bus *bus, unsigned char addr)
400 int ret;
402 if (bus->i2c_read)
403 return bus->i2c_read(bus, addr);
405 i2c_start(bus);
406 i2c_sendbyte(bus,addr,0);
407 ret = i2c_readbyte(bus,1);
408 i2c_stop(bus);
409 return ret;
412 int i2c_write(struct i2c_bus *bus, unsigned char addr,
413 unsigned char data1, unsigned char data2, int both)
415 int ack;
417 if (bus->i2c_write)
418 return bus->i2c_write(bus, addr, data1, data2, both);
420 i2c_start(bus);
421 i2c_sendbyte(bus,addr,0);
422 ack = i2c_sendbyte(bus,data1,0);
423 if (both)
424 ack = i2c_sendbyte(bus,data2,0);
425 i2c_stop(bus);
426 return ack ? -1 : 0 ;
429 /* ----------------------------------------------------------------------- */
431 #ifdef MODULE
433 #if LINUX_VERSION_CODE >= 0x020100
434 EXPORT_SYMBOL(i2c_register_bus);
435 EXPORT_SYMBOL(i2c_unregister_bus);
436 EXPORT_SYMBOL(i2c_register_driver);
437 EXPORT_SYMBOL(i2c_unregister_driver);
438 EXPORT_SYMBOL(i2c_control_device);
439 EXPORT_SYMBOL(i2c_start);
440 EXPORT_SYMBOL(i2c_stop);
441 EXPORT_SYMBOL(i2c_one);
442 EXPORT_SYMBOL(i2c_zero);
443 EXPORT_SYMBOL(i2c_ack);
444 EXPORT_SYMBOL(i2c_sendbyte);
445 EXPORT_SYMBOL(i2c_readbyte);
446 EXPORT_SYMBOL(i2c_read);
447 EXPORT_SYMBOL(i2c_write);
448 #endif
450 int init_module(void)
452 return i2c_init();
455 void cleanup_module(void)
458 #endif