1 /* ------------------------------------------------------------------------- */
2 /* i2c-algo-bit.c i2c driver algorithms for bit-shift adapters */
3 /* ------------------------------------------------------------------------- */
4 /* Copyright (C) 1995-2000 Simon G. Vogl
6 This program is free software; you can redistribute it and/or modify
7 it under the terms of the GNU General Public License as published by
8 the Free Software Foundation; either version 2 of the License, or
9 (at your option) any later version.
11 This program is distributed in the hope that it will be useful,
12 but WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 GNU General Public License for more details.
16 You should have received a copy of the GNU General Public License
17 along with this program; if not, write to the Free Software
18 Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. */
19 /* ------------------------------------------------------------------------- */
21 /* With some changes from Kyösti Mälkki <kmalkki@cc.hut.fi> and even
22 Frodo Looijaard <frodol@dds.nl> */
24 /* $Id: i2c-algo-bit.c,v 1.1.1.1 2005/04/11 02:50:21 jack Exp $ */
26 #include <linux/kernel.h>
27 #include <linux/module.h>
28 #include <linux/delay.h>
29 #include <linux/slab.h>
30 #include <linux/version.h>
31 #include <linux/init.h>
32 #include <asm/uaccess.h>
33 #include <linux/ioport.h>
34 #include <linux/errno.h>
35 #include <linux/sched.h>
37 #include <linux/i2c.h>
38 #include <linux/i2c-algo-bit.h>
40 /* ----- global defines ----------------------------------------------- */
41 #define DEB(x) if (i2c_debug>=1) x;
42 #define DEB2(x) if (i2c_debug>=2) x;
43 #define DEBSTAT(x) if (i2c_debug>=3) x; /* print several statistical values*/
44 #define DEBPROTO(x) if (i2c_debug>=9) { x; }
45 /* debug the protocol by showing transferred bits */
47 /* debugging - slow down transfer to have a look at the data .. */
48 /* I use this with two leds&resistors, each one connected to sda,scl */
49 /* respectively. This makes sure that the algorithm works. Some chips */
50 /* might not like this, as they have an internal timeout of some mils */
52 #define SLO_IO jif=jiffies;while(time_before_eq(jiffies, jif+i2c_table[minor].veryslow))\
53 if (need_resched) schedule();
57 /* ----- global variables --------------------------------------------- */
66 static int bit_test; /* see if the line-setting functions work */
67 static int bit_scan; /* have a look at what's hanging 'round */
69 /* --- setting states on the bus with the right timing: --------------- */
71 #define setsda(adap,val) adap->setsda(adap->data, val)
72 #define setscl(adap,val) adap->setscl(adap->data, val)
73 #define getsda(adap) adap->getsda(adap->data)
74 #define getscl(adap) adap->getscl(adap->data)
76 static inline void sdalo(struct i2c_algo_bit_data *adap)
82 static inline void sdahi(struct i2c_algo_bit_data *adap)
88 static inline void scllo(struct i2c_algo_bit_data *adap)
98 * Raise scl line, and do checking for delays. This is necessary for slower
101 static inline int sclhi(struct i2c_algo_bit_data *adap)
107 udelay(adap->udelay);
109 /* Not all adapters have scl sense line... */
110 if (adap->getscl == NULL )
113 while (! getscl(adap) ) {
114 /* the hw knows how to read the clock line,
115 * so we wait until it actually gets high.
116 * This is safer as some chips may hold it low
117 * while they are processing data internally.
120 if (time_after_eq(jiffies, start+adap->timeout)) {
123 if (current->need_resched)
126 DEBSTAT(printk("needed %ld jiffies\n", jiffies-start));
134 /* --- other auxiliary functions -------------------------------------- */
135 static void i2c_start(struct i2c_algo_bit_data *adap)
137 /* assert: scl, sda are high */
138 DEBPROTO(printk("S "));
143 static void i2c_repstart(struct i2c_algo_bit_data *adap)
145 /* scl, sda may not be high */
146 DEBPROTO(printk(" Sr "));
149 udelay(adap->udelay);
156 static void i2c_stop(struct i2c_algo_bit_data *adap)
158 DEBPROTO(printk("P\n"));
159 /* assert: scl is low */
167 /* send a byte without start cond., look for arbitration,
168 check ackn. from slave */
170 * 1 if the device acknowledged
171 * 0 if the device did not ack
172 * -ETIMEDOUT if an error occurred (while raising the scl line)
174 static int i2c_outb(struct i2c_adapter *i2c_adap, char c)
179 struct i2c_algo_bit_data *adap = i2c_adap->algo_data;
181 /* assert: scl is low */
182 DEB2(printk(" i2c_outb:%2.2X\n",c&0xff));
183 for ( i=7 ; i>=0 ; i-- ) {
186 udelay(adap->udelay);
187 DEBPROTO(printk("%d",sb!=0));
188 if (sclhi(adap)<0) { /* timed out */
189 sdahi(adap); /* we don't want to block the net */
192 /* do arbitration here:
193 * if ( sb && ! getsda(adap) ) -> ouch! Get out of here.
196 udelay(adap->udelay);
199 if (sclhi(adap)<0){ /* timeout */
202 /* read ack: SDA should be pulled down by slave */
203 ack=getsda(adap); /* ack: sda is pulled low ->success. */
204 DEB2(printk(" i2c_outb: getsda() = 0x%2.2x\n", ~ack ));
206 DEBPROTO( printk("[%2.2x]",c&0xff) );
207 DEBPROTO(if (0==ack){ printk(" A ");} else printk(" NA ") );
209 return 0==ack; /* return 1 if device acked */
210 /* assert: scl is low (sda undef) */
214 static int i2c_inb(struct i2c_adapter *i2c_adap)
216 /* read byte via i2c port, without start/stop sequence */
217 /* acknowledge is sent in i2c_read. */
219 unsigned char indata=0;
220 struct i2c_algo_bit_data *adap = i2c_adap->algo_data;
222 /* assert: scl is low */
223 DEB2(printk("i2c_inb.\n"));
227 if (sclhi(adap)<0) { /* timeout */
235 /* assert: scl is low */
236 DEBPROTO(printk(" %2.2x", indata & 0xff));
237 return (int) (indata & 0xff);
241 * Sanity check for the adapter hardware - check the reaction of
242 * the bus lines only if it seems to be idle.
244 static int test_bus(struct i2c_algo_bit_data *adap, char* name) {
247 if (adap->getscl==NULL) {
248 printk("i2c-algo-bit.o: Warning: Adapter can't read from clock line - skipping test.\n");
252 printk("i2c-algo-bit.o: Adapter: %s scl: %d sda: %d -- testing...\n",
253 name,getscl(adap),getsda(adap));
255 printk("i2c-algo-bit.o: %s seems to be busy.\n",name);
259 printk("i2c-algo-bit.o:1 scl: %d sda: %d \n",getscl(adap),
261 if ( 0 != getsda(adap) ) {
262 printk("i2c-algo-bit.o: %s SDA stuck high!\n",name);
266 if ( 0 == getscl(adap) ) {
267 printk("i2c-algo-bit.o: %s SCL unexpected low while pulling SDA low!\n",
272 printk("i2c-algo-bit.o:2 scl: %d sda: %d \n",getscl(adap),
274 if ( 0 == getsda(adap) ) {
275 printk("i2c-algo-bit.o: %s SDA stuck low!\n",name);
279 if ( 0 == getscl(adap) ) {
280 printk("i2c-algo-bit.o: %s SCL unexpected low while SDA high!\n",
285 printk("i2c-algo-bit.o:3 scl: %d sda: %d \n",getscl(adap),
287 if ( 0 != getscl(adap) ) {
288 printk("i2c-algo-bit.o: %s SCL stuck high!\n",name);
292 if ( 0 == getsda(adap) ) {
293 printk("i2c-algo-bit.o: %s SDA unexpected low while pulling SCL low!\n",
298 printk("i2c-algo-bit.o:4 scl: %d sda: %d \n",getscl(adap),
300 if ( 0 == getscl(adap) ) {
301 printk("i2c-algo-bit.o: %s SCL stuck low!\n",name);
305 if ( 0 == getsda(adap) ) {
306 printk("i2c-algo-bit.o: %s SDA unexpected low while SCL high!\n",
310 printk("i2c-algo-bit.o: %s passed test.\n",name);
318 /* ----- Utility functions
321 /* try_address tries to contact a chip for a number of
322 * times before it gives up.
325 * 0 chip did not answer
326 * -x transmission error
328 static inline int try_address(struct i2c_adapter *i2c_adap,
329 unsigned char addr, int retries)
331 struct i2c_algo_bit_data *adap = i2c_adap->algo_data;
333 for (i=0;i<=retries;i++) {
334 ret = i2c_outb(i2c_adap,addr);
336 break; /* success! */
338 udelay(5/*adap->udelay*/);
339 if (i==retries) /* no success */
342 udelay(adap->udelay);
344 DEB2(if (i) printk("i2c-algo-bit.o: needed %d retries for %d\n",
349 static int sendbytes(struct i2c_adapter *i2c_adap,const char *buf, int count)
351 struct i2c_algo_bit_data *adap = i2c_adap->algo_data;
353 const char *temp = buf;
359 DEB2(printk("i2c-algo-bit.o: %s i2c_write: writing %2.2X\n",
360 i2c_adap->name, c&0xff));
361 retval = i2c_outb(i2c_adap,c);
366 } else { /* arbitration or no acknowledge */
367 printk("i2c-algo-bit.o: %s i2c_write: error - bailout.\n",
370 return (retval<0)? retval : -EFAULT;
371 /* got a better one ?? */
374 /* from asm/delay.h */
375 __delay(adap->mdelay * (loops_per_sec / 1000) );
381 static inline int readbytes(struct i2c_adapter *i2c_adap,char *buf,int count)
385 int rdcount=0; /* counts bytes read */
386 struct i2c_algo_bit_data *adap = i2c_adap->algo_data;
389 inval = i2c_inb(i2c_adap);
390 /*printk("%#02x ",inval); if ( ! (count % 16) ) printk("\n"); */
394 } else { /* read timed out */
395 printk("i2c-algo-bit.o: i2c_read: i2c_inb timed out.\n");
399 if ( count > 1 ) { /* send ack */
401 DEBPROTO(printk(" Am "));
403 sdahi(adap); /* neg. ack on last byte */
404 DEBPROTO(printk(" NAm "));
406 if (sclhi(adap)<0) { /* timeout */
408 printk("i2c-algo-bit.o: i2c_read: Timeout at ack\n");
419 /* doAddress initiates the transfer by generating the start condition (in
420 * try_address) and transmits the address in the necessary format to handle
421 * reads, writes as well as 10bit-addresses.
423 * 0 everything went okay, the chip ack'ed
424 * -x an error occurred (like: -EREMOTEIO if the device did not answer, or
425 * -ETIMEDOUT, for example if the lines are stuck...)
427 static inline int bit_doAddress(struct i2c_adapter *i2c_adap,
428 struct i2c_msg *msg, int retries)
430 unsigned short flags = msg->flags;
431 struct i2c_algo_bit_data *adap = i2c_adap->algo_data;
435 if ( (flags & I2C_M_TEN) ) {
436 /* a ten bit address */
437 addr = 0xf0 | (( msg->addr >> 7) & 0x03);
438 DEB2(printk("addr0: %d\n",addr));
439 /* try extended address code...*/
440 ret = try_address(i2c_adap, addr, retries);
442 printk("died at extended address code.\n");
445 /* the remaining 8 bit address */
446 ret = i2c_outb(i2c_adap,msg->addr & 0x7f);
448 /* the chip did not ack / xmission error occurred */
449 printk("died at 2nd address code.\n");
452 if ( flags & I2C_M_RD ) {
454 /* okay, now switch into reading mode */
456 ret = try_address(i2c_adap, addr, retries);
458 printk("died at extended address code.\n");
462 } else { /* normal 7bit address */
463 addr = ( msg->addr << 1 );
464 if (flags & I2C_M_RD )
466 if (flags & I2C_M_REV_DIR_ADDR )
468 ret = try_address(i2c_adap, addr, retries);
476 static int bit_xfer(struct i2c_adapter *i2c_adap,
477 struct i2c_msg msgs[], int num)
479 struct i2c_msg *pmsg;
480 struct i2c_algo_bit_data *adap = i2c_adap->algo_data;
485 for (i=0;i<num;i++) {
487 if (!(pmsg->flags & I2C_M_NOSTART)) {
491 ret = bit_doAddress(i2c_adap,pmsg,i2c_adap->retries);
493 DEB2(printk("i2c-algo-bit.o: NAK from device adr %#2x msg #%d\n"
495 return (ret<0) ? ret : -EREMOTEIO;
498 if (pmsg->flags & I2C_M_RD ) {
499 /* read bytes into buffer*/
500 ret = readbytes(i2c_adap,pmsg->buf,pmsg->len);
501 DEB2(printk("i2c-algo-bit.o: read %d bytes.\n",ret));
502 if (ret < pmsg->len ) {
503 return (ret<0)? ret : -EREMOTEIO;
506 /* write bytes from buffer */
507 ret = sendbytes(i2c_adap,pmsg->buf,pmsg->len);
508 DEB2(printk("i2c-algo-bit.o: wrote %d bytes.\n",ret));
509 if (ret < pmsg->len ) {
510 return (ret<0) ? ret : -EREMOTEIO;
518 static int algo_control(struct i2c_adapter *adapter,
519 unsigned int cmd, unsigned long arg)
524 static u32 bit_func(struct i2c_adapter *adap)
526 return I2C_FUNC_SMBUS_EMUL | I2C_FUNC_10BIT_ADDR |
527 I2C_FUNC_PROTOCOL_MANGLING;
531 /* -----exported algorithm data: ------------------------------------- */
533 static struct i2c_algorithm i2c_bit_algo = {
534 "Bit-shift algorithm",
538 NULL, /* slave_xmit */
539 NULL, /* slave_recv */
540 algo_control, /* ioctl */
541 bit_func, /* functionality */
545 * registering functions to load algorithms at runtime
547 int i2c_bit_add_bus(struct i2c_adapter *adap)
550 struct i2c_algo_bit_data *bit_adap = adap->algo_data;
553 int ret = test_bus(bit_adap, adap->name);
558 DEB2(printk("i2c-algo-bit.o: hw routines for %s registered.\n",
561 /* register new adapter to i2c module... */
563 adap->id |= i2c_bit_algo.id;
564 adap->algo = &i2c_bit_algo;
566 adap->timeout = 100; /* default values, should */
567 adap->retries = 3; /* be replaced by defines */
572 printk(KERN_INFO " i2c-algo-bit.o: scanning bus %s.\n",
574 for (i = 0x00; i < 0xff; i+=2) {
576 ack = i2c_outb(adap,i);
579 printk("(%02x)",i>>1);
589 i2c_add_adapter(adap);
595 int i2c_bit_del_bus(struct i2c_adapter *adap)
599 if ((res = i2c_del_adapter(adap)) < 0)
602 DEB2(printk("i2c-algo-bit.o: adapter unregistered: %s\n",adap->name));
610 int __init i2c_algo_bit_init (void)
612 printk("i2c-algo-bit.o: i2c bit algorithm module\n");
618 EXPORT_SYMBOL(i2c_bit_add_bus);
619 EXPORT_SYMBOL(i2c_bit_del_bus);
622 MODULE_AUTHOR("Simon G. Vogl <simon@tk.uni-linz.ac.at>");
623 MODULE_DESCRIPTION("I2C-Bus bit-banging algorithm");
624 MODULE_LICENSE("GPL");
626 MODULE_PARM(bit_test, "i");
627 MODULE_PARM(bit_scan, "i");
628 MODULE_PARM(i2c_debug,"i");
630 MODULE_PARM_DESC(bit_test, "Test the lines of the bus to see if it is stuck");
631 MODULE_PARM_DESC(bit_scan, "Scan for active chips on the bus");
632 MODULE_PARM_DESC(i2c_debug,
633 "debug level - 0 off; 1 normal; 2,3 more verbose; 9 bit-protocol");
635 int init_module(void)
637 return i2c_algo_bit_init();
640 void cleanup_module(void)