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 Frodo Looijaard <frodol@dds.nl>, Kyösti Mälkki
22 <kmalkki@cc.hut.fi> and Jean Delvare <khali@linux-fr.org> */
24 #include <linux/kernel.h>
25 #include <linux/module.h>
26 #include <linux/delay.h>
27 #include <linux/slab.h>
28 #include <linux/init.h>
29 #include <linux/errno.h>
30 #include <linux/sched.h>
31 #include <linux/i2c.h>
32 #include <linux/i2c-algo-bit.h>
35 /* ----- global defines ----------------------------------------------- */
36 #define DEB(x) if (i2c_debug>=1) x;
37 #define DEB2(x) if (i2c_debug>=2) x;
38 #define DEBSTAT(x) if (i2c_debug>=3) x; /* print several statistical values*/
39 #define DEBPROTO(x) if (i2c_debug>=9) { x; }
40 /* debug the protocol by showing transferred bits */
43 /* ----- global variables --------------------------------------------- */
48 static int bit_test; /* see if the line-setting functions work */
50 /* --- setting states on the bus with the right timing: --------------- */
52 #define setsda(adap,val) adap->setsda(adap->data, val)
53 #define setscl(adap,val) adap->setscl(adap->data, val)
54 #define getsda(adap) adap->getsda(adap->data)
55 #define getscl(adap) adap->getscl(adap->data)
57 static inline void sdalo(struct i2c_algo_bit_data *adap)
63 static inline void sdahi(struct i2c_algo_bit_data *adap)
69 static inline void scllo(struct i2c_algo_bit_data *adap)
76 * Raise scl line, and do checking for delays. This is necessary for slower
79 static int sclhi(struct i2c_algo_bit_data *adap)
85 /* Not all adapters have scl sense line... */
90 while (! getscl(adap) ) {
91 /* the hw knows how to read the clock line,
92 * so we wait until it actually gets high.
93 * This is safer as some chips may hold it low
94 * while they are processing data internally.
96 if (time_after_eq(jiffies, start+adap->timeout)) {
101 DEBSTAT(printk(KERN_DEBUG "needed %ld jiffies\n", jiffies-start));
104 udelay(adap->udelay);
109 /* --- other auxiliary functions -------------------------------------- */
110 static void i2c_start(struct i2c_algo_bit_data *adap)
112 /* assert: scl, sda are high */
113 DEBPROTO(printk("S "));
118 static void i2c_repstart(struct i2c_algo_bit_data *adap)
120 /* scl, sda may not be high */
121 DEBPROTO(printk(" Sr "));
130 static void i2c_stop(struct i2c_algo_bit_data *adap)
132 DEBPROTO(printk("P\n"));
133 /* assert: scl is low */
141 /* send a byte without start cond., look for arbitration,
142 check ackn. from slave */
144 * 1 if the device acknowledged
145 * 0 if the device did not ack
146 * -ETIMEDOUT if an error occurred (while raising the scl line)
148 static int i2c_outb(struct i2c_adapter *i2c_adap, char c)
153 struct i2c_algo_bit_data *adap = i2c_adap->algo_data;
155 /* assert: scl is low */
156 for ( i=7 ; i>=0 ; i-- ) {
159 udelay(adap->udelay);
160 DEBPROTO(printk(KERN_DEBUG "%d",sb!=0));
161 if (sclhi(adap)<0) { /* timed out */
162 sdahi(adap); /* we don't want to block the net */
163 DEB2(printk(KERN_DEBUG " i2c_outb: 0x%02x, timeout at bit #%d\n", c&0xff, i));
166 /* do arbitration here:
167 * if ( sb && ! getsda(adap) ) -> ouch! Get out of here.
170 udelay(adap->udelay);
173 if (sclhi(adap)<0){ /* timeout */
174 DEB2(printk(KERN_DEBUG " i2c_outb: 0x%02x, timeout at ack\n", c&0xff));
177 /* read ack: SDA should be pulled down by slave */
178 ack=getsda(adap); /* ack: sda is pulled low ->success. */
179 DEB2(printk(KERN_DEBUG " i2c_outb: 0x%02x , getsda() = %d\n", c & 0xff, ack));
181 DEBPROTO( printk(KERN_DEBUG "[%2.2x]",c&0xff) );
182 DEBPROTO(if (0==ack){ printk(KERN_DEBUG " A ");} else printk(KERN_DEBUG " NA ") );
184 return 0==ack; /* return 1 if device acked */
185 /* assert: scl is low (sda undef) */
189 static int i2c_inb(struct i2c_adapter *i2c_adap)
191 /* read byte via i2c port, without start/stop sequence */
192 /* acknowledge is sent in i2c_read. */
194 unsigned char indata=0;
195 struct i2c_algo_bit_data *adap = i2c_adap->algo_data;
197 /* assert: scl is low */
200 if (sclhi(adap)<0) { /* timeout */
201 DEB2(printk(KERN_DEBUG " i2c_inb: timeout at bit #%d\n", 7-i));
209 /* assert: scl is low */
210 DEB2(printk(KERN_DEBUG "i2c_inb: 0x%02x\n", indata & 0xff));
212 DEBPROTO(printk(KERN_DEBUG " 0x%02x", indata & 0xff));
213 return (int) (indata & 0xff);
217 * Sanity check for the adapter hardware - check the reaction of
218 * the bus lines only if it seems to be idle.
220 static int test_bus(struct i2c_algo_bit_data *adap, char* name) {
223 if (adap->getscl==NULL)
224 printk(KERN_INFO "i2c-algo-bit.o: Testing SDA only, "
225 "SCL is not readable.\n");
228 scl=(adap->getscl==NULL?1:getscl(adap));
229 printk(KERN_DEBUG "i2c-algo-bit.o: (0) scl=%d, sda=%d\n",scl,sda);
231 printk(KERN_WARNING "i2c-algo-bit.o: %s seems to be busy.\n", name);
237 scl=(adap->getscl==NULL?1:getscl(adap));
238 printk(KERN_DEBUG "i2c-algo-bit.o: (1) scl=%d, sda=%d\n",scl,sda);
240 printk(KERN_WARNING "i2c-algo-bit.o: SDA stuck high!\n");
244 printk(KERN_WARNING "i2c-algo-bit.o: SCL unexpected low "
245 "while pulling SDA low!\n");
251 scl=(adap->getscl==NULL?1:getscl(adap));
252 printk(KERN_DEBUG "i2c-algo-bit.o: (2) scl=%d, sda=%d\n",scl,sda);
254 printk(KERN_WARNING "i2c-algo-bit.o: SDA stuck low!\n");
258 printk(KERN_WARNING "i2c-algo-bit.o: SCL unexpected low "
259 "while pulling SDA high!\n");
265 scl=(adap->getscl==NULL?0:getscl(adap));
266 printk(KERN_DEBUG "i2c-algo-bit.o: (3) scl=%d, sda=%d\n",scl,sda);
268 printk(KERN_WARNING "i2c-algo-bit.o: SCL stuck high!\n");
272 printk(KERN_WARNING "i2c-algo-bit.o: SDA unexpected low "
273 "while pulling SCL low!\n");
279 scl=(adap->getscl==NULL?1:getscl(adap));
280 printk(KERN_DEBUG "i2c-algo-bit.o: (4) scl=%d, sda=%d\n",scl,sda);
282 printk(KERN_WARNING "i2c-algo-bit.o: SCL stuck low!\n");
286 printk(KERN_WARNING "i2c-algo-bit.o: SDA unexpected low "
287 "while pulling SCL high!\n");
290 printk(KERN_INFO "i2c-algo-bit.o: %s passed test.\n",name);
298 /* ----- Utility functions
301 /* try_address tries to contact a chip for a number of
302 * times before it gives up.
305 * 0 chip did not answer
306 * -x transmission error
308 static int try_address(struct i2c_adapter *i2c_adap,
309 unsigned char addr, int retries)
311 struct i2c_algo_bit_data *adap = i2c_adap->algo_data;
313 for (i=0;i<=retries;i++) {
314 ret = i2c_outb(i2c_adap,addr);
316 break; /* success! */
318 udelay(5/*adap->udelay*/);
319 if (i==retries) /* no success */
322 udelay(adap->udelay);
325 printk(KERN_DEBUG "i2c-algo-bit.o: Used %d tries to %s client at 0x%02x : %s\n",
326 i+1, addr & 1 ? "read" : "write", addr>>1,
327 ret==1 ? "success" : ret==0 ? "no ack" : "failed, timeout?" )
332 static int sendbytes(struct i2c_adapter *i2c_adap, struct i2c_msg *msg)
334 struct i2c_algo_bit_data *adap = i2c_adap->algo_data;
336 const char *temp = msg->buf;
337 int count = msg->len;
338 unsigned short nak_ok = msg->flags & I2C_M_IGNORE_NAK;
344 DEB2(dev_dbg(&i2c_adap->dev, "sendbytes: writing %2.2X\n", c&0xff));
345 retval = i2c_outb(i2c_adap,c);
346 if ((retval>0) || (nak_ok && (retval==0))) { /* ok or ignored NAK */
350 } else { /* arbitration or no acknowledge */
351 dev_err(&i2c_adap->dev, "sendbytes: error - bailout.\n");
353 return (retval<0)? retval : -EFAULT;
354 /* got a better one ?? */
360 static int readbytes(struct i2c_adapter *i2c_adap, struct i2c_msg *msg)
363 int rdcount=0; /* counts bytes read */
364 struct i2c_algo_bit_data *adap = i2c_adap->algo_data;
365 char *temp = msg->buf;
366 int count = msg->len;
369 inval = i2c_inb(i2c_adap);
373 } else { /* read timed out */
374 printk(KERN_ERR "i2c-algo-bit.o: readbytes: i2c_inb timed out.\n");
381 if (msg->flags & I2C_M_NO_RD_ACK)
384 if ( count > 0 ) { /* send ack */
386 DEBPROTO(printk(" Am "));
388 sdahi(adap); /* neg. ack on last byte */
389 DEBPROTO(printk(" NAm "));
391 if (sclhi(adap)<0) { /* timeout */
393 printk(KERN_ERR "i2c-algo-bit.o: readbytes: Timeout at ack\n");
402 /* doAddress initiates the transfer by generating the start condition (in
403 * try_address) and transmits the address in the necessary format to handle
404 * reads, writes as well as 10bit-addresses.
406 * 0 everything went okay, the chip ack'ed, or IGNORE_NAK flag was set
407 * -x an error occurred (like: -EREMOTEIO if the device did not answer, or
408 * -ETIMEDOUT, for example if the lines are stuck...)
410 static int bit_doAddress(struct i2c_adapter *i2c_adap, struct i2c_msg *msg)
412 unsigned short flags = msg->flags;
413 unsigned short nak_ok = msg->flags & I2C_M_IGNORE_NAK;
414 struct i2c_algo_bit_data *adap = i2c_adap->algo_data;
419 retries = nak_ok ? 0 : i2c_adap->retries;
421 if ( (flags & I2C_M_TEN) ) {
422 /* a ten bit address */
423 addr = 0xf0 | (( msg->addr >> 7) & 0x03);
424 DEB2(printk(KERN_DEBUG "addr0: %d\n",addr));
425 /* try extended address code...*/
426 ret = try_address(i2c_adap, addr, retries);
427 if ((ret != 1) && !nak_ok) {
428 printk(KERN_ERR "died at extended address code.\n");
431 /* the remaining 8 bit address */
432 ret = i2c_outb(i2c_adap,msg->addr & 0x7f);
433 if ((ret != 1) && !nak_ok) {
434 /* the chip did not ack / xmission error occurred */
435 printk(KERN_ERR "died at 2nd address code.\n");
438 if ( flags & I2C_M_RD ) {
440 /* okay, now switch into reading mode */
442 ret = try_address(i2c_adap, addr, retries);
443 if ((ret!=1) && !nak_ok) {
444 printk(KERN_ERR "died at extended address code.\n");
448 } else { /* normal 7bit address */
449 addr = ( msg->addr << 1 );
450 if (flags & I2C_M_RD )
452 if (flags & I2C_M_REV_DIR_ADDR )
454 ret = try_address(i2c_adap, addr, retries);
455 if ((ret!=1) && !nak_ok)
462 static int bit_xfer(struct i2c_adapter *i2c_adap,
463 struct i2c_msg msgs[], int num)
465 struct i2c_msg *pmsg;
466 struct i2c_algo_bit_data *adap = i2c_adap->algo_data;
469 unsigned short nak_ok;
472 for (i=0;i<num;i++) {
474 nak_ok = pmsg->flags & I2C_M_IGNORE_NAK;
475 if (!(pmsg->flags & I2C_M_NOSTART)) {
479 ret = bit_doAddress(i2c_adap, pmsg);
480 if ((ret != 0) && !nak_ok) {
481 DEB2(printk(KERN_DEBUG "i2c-algo-bit.o: NAK from device addr %2.2x msg #%d\n"
483 return (ret<0) ? ret : -EREMOTEIO;
486 if (pmsg->flags & I2C_M_RD ) {
487 /* read bytes into buffer*/
488 ret = readbytes(i2c_adap, pmsg);
489 DEB2(printk(KERN_DEBUG "i2c-algo-bit.o: read %d bytes.\n",ret));
490 if (ret < pmsg->len ) {
491 return (ret<0)? ret : -EREMOTEIO;
494 /* write bytes from buffer */
495 ret = sendbytes(i2c_adap, pmsg);
496 DEB2(printk(KERN_DEBUG "i2c-algo-bit.o: wrote %d bytes.\n",ret));
497 if (ret < pmsg->len ) {
498 return (ret<0) ? ret : -EREMOTEIO;
506 static u32 bit_func(struct i2c_adapter *adap)
508 return I2C_FUNC_I2C | I2C_FUNC_SMBUS_EMUL |
509 I2C_FUNC_10BIT_ADDR | I2C_FUNC_PROTOCOL_MANGLING;
513 /* -----exported algorithm data: ------------------------------------- */
515 static const struct i2c_algorithm i2c_bit_algo = {
516 .master_xfer = bit_xfer,
517 .functionality = bit_func,
521 * registering functions to load algorithms at runtime
523 int i2c_bit_add_bus(struct i2c_adapter *adap)
525 struct i2c_algo_bit_data *bit_adap = adap->algo_data;
528 int ret = test_bus(bit_adap, adap->name);
533 DEB2(dev_dbg(&adap->dev, "hw routines registered.\n"));
535 /* register new adapter to i2c module... */
536 adap->algo = &i2c_bit_algo;
538 adap->timeout = 100; /* default values, should */
539 adap->retries = 3; /* be replaced by defines */
541 return i2c_add_adapter(adap);
543 EXPORT_SYMBOL(i2c_bit_add_bus);
545 MODULE_AUTHOR("Simon G. Vogl <simon@tk.uni-linz.ac.at>");
546 MODULE_DESCRIPTION("I2C-Bus bit-banging algorithm");
547 MODULE_LICENSE("GPL");
549 module_param(bit_test, bool, 0);
550 module_param(i2c_debug, int, S_IRUGO | S_IWUSR);
552 MODULE_PARM_DESC(bit_test, "Test the lines of the bus to see if it is stuck");
553 MODULE_PARM_DESC(i2c_debug,
554 "debug level - 0 off; 1 normal; 2,3 more verbose; 9 bit-protocol");