2 * Generic i2c interface for linux
4 * (c) 1998 Gerd Knorr <kraxel@cs.tu-berlin.de>
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)
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");
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);
46 printk(KERN_INFO
"i2c: initialized%s\n",
47 scan
? " (i2c bus scan enabled)" : "");
48 /* anything to do here ? */
49 #ifdef CONFIG_VIDEO_BT848
56 /* ----------------------------------------------------------------------- */
58 static void i2c_attach_device(struct i2c_bus
*bus
, struct i2c_driver
*driver
)
60 struct i2c_device
*device
;
65 /* probe for device */
67 for (addr
= driver
->addr_l
; addr
<= driver
->addr_h
; addr
+= 2)
70 ack
= i2c_sendbyte(bus
,addr
,0);
80 for (i
= 0; i
< I2C_DEVICE_MAX
; i
++)
81 if (NULL
== driver
->devices
[i
])
83 if (I2C_DEVICE_MAX
== i
)
86 for (j
= 0; j
< I2C_DEVICE_MAX
; j
++)
87 if (NULL
== bus
->devices
[j
])
89 if (I2C_DEVICE_MAX
== j
)
92 if (NULL
== (device
= kmalloc(sizeof(struct i2c_device
),GFP_KERNEL
)))
95 device
->driver
= driver
;
100 if (driver
->attach(device
)!=0)
105 driver
->devices
[i
] = device
;
107 bus
->devices
[j
] = device
;
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
)
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
])
126 if (I2C_DEVICE_MAX
== i
)
128 printk(KERN_WARNING
"i2c: detach_device #1: device not found: %s\n",
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
])
138 if (I2C_DEVICE_MAX
== i
)
140 printk(KERN_WARNING
"i2c: detach_device #2: device not found: %s\n",
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
));
151 /* ----------------------------------------------------------------------- */
153 int i2c_register_bus(struct i2c_bus
*bus
)
158 memset(bus
->devices
,0,sizeof(bus
->devices
));
161 for (i
= 0; i
< I2C_BUS_MAX
; i
++)
162 if (NULL
== busses
[i
])
164 if (I2C_BUS_MAX
== i
)
169 REGPRINT(printk("i2c: bus registered: %s\n",bus
->name
));
175 /* scan whole i2c bus */
177 for (i
= 0; i
< 256; i
+=2)
180 ack
= i2c_sendbyte(bus
,i
,0);
184 printk(KERN_INFO
"i2c: scanning bus %s: found device at addr=0x%02x\n",
191 /* probe available drivers */
192 for (i
= 0; i
< I2C_DRIVER_MAX
; i
++)
194 i2c_attach_device(bus
,drivers
[i
]);
198 int i2c_unregister_bus(struct i2c_bus
*bus
)
203 for (i
= 0; i
< I2C_DEVICE_MAX
; i
++)
205 i2c_detach_device(bus
->devices
[i
]);
207 for (i
= 0; i
< I2C_BUS_MAX
; i
++)
208 if (bus
== busses
[i
])
210 if (I2C_BUS_MAX
== i
)
212 printk(KERN_WARNING
"i2c: unregister_bus #1: bus not found: %s\n",
221 REGPRINT(printk("i2c: bus unregistered: %s\n",bus
->name
));
226 /* ----------------------------------------------------------------------- */
228 int i2c_register_driver(struct i2c_driver
*driver
)
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
])
238 if (I2C_DRIVER_MAX
== i
)
246 REGPRINT(printk("i2c: driver registered: %s\n",driver
->name
));
248 /* Probe available busses */
249 for (i
= 0; i
< I2C_BUS_MAX
; i
++)
251 i2c_attach_device(busses
[i
],driver
);
256 int i2c_unregister_driver(struct i2c_driver
*driver
)
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
])
268 if (I2C_DRIVER_MAX
== i
)
270 printk(KERN_WARNING
"i2c: unregister_driver: driver not found: %s\n",
279 REGPRINT(printk("i2c: driver unregistered: %s\n",driver
->name
));
284 /* ----------------------------------------------------------------------- */
286 int i2c_control_device(struct i2c_bus
*bus
, int id
,
287 unsigned int cmd
, void *arg
)
291 for (i
= 0; i
< I2C_DEVICE_MAX
; i
++)
292 if (bus
->devices
[i
] && bus
->devices
[i
]->driver
->id
== id
)
294 if (i
== I2C_DEVICE_MAX
)
296 if (NULL
== bus
->devices
[i
]->driver
->command
)
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
)
312 I2C_DEBUG(printk("%s: < ",bus
->name
));
315 void i2c_stop(struct i2c_bus
*bus
)
320 I2C_DEBUG(printk(">\n"));
323 void i2c_one(struct i2c_bus
*bus
)
330 void i2c_zero(struct i2c_bus
*bus
)
337 int i2c_ack(struct i2c_bus
*bus
)
348 int i2c_sendbyte(struct i2c_bus
*bus
,unsigned char data
,int wait_for_ack
)
354 (data
&(1<<i
)) ? i2c_one(bus
) : i2c_zero(bus
);
356 udelay(wait_for_ack
);
358 I2C_DEBUG(printk("%02x%c ",(int)data
,ack
?'-':'+'));
362 unsigned char i2c_readbyte(struct i2c_bus
*bus
,int last
)
365 unsigned char data
=0;
375 last
? i2c_one(bus
) : i2c_zero(bus
);
376 I2C_DEBUG(printk("=%02x%c ",(int)data
,last
?'-':'+'));
380 /* ----------------------------------------------------------------------- */
382 int i2c_read(struct i2c_bus
*bus
, unsigned char addr
)
387 return bus
->i2c_read(bus
, addr
);
390 i2c_sendbyte(bus
,addr
,0);
391 ret
= i2c_readbyte(bus
,1);
396 int i2c_write(struct i2c_bus
*bus
, unsigned char addr
,
397 unsigned char data1
, unsigned char data2
, int both
)
402 return bus
->i2c_write(bus
, addr
, data1
, data2
, both
);
405 i2c_sendbyte(bus
,addr
,0);
406 ack
= i2c_sendbyte(bus
,data1
,0);
408 ack
= i2c_sendbyte(bus
,data2
,0);
410 return ack
? -1 : 0 ;
413 /* ----------------------------------------------------------------------- */
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
);
434 int init_module(void)
439 void cleanup_module(void)