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.
15 * ------------------------------------------------------------------------- */
17 /* With some changes from Frodo Looijaard <frodol@dds.nl>, Kyösti Mälkki
18 <kmalkki@cc.hut.fi> and Jean Delvare <jdelvare@suse.de> */
20 #include <linux/kernel.h>
21 #include <linux/module.h>
22 #include <linux/delay.h>
23 #include <linux/errno.h>
24 #include <linux/sched.h>
25 #include <linux/i2c.h>
26 #include <linux/i2c-algo-bit.h>
29 /* ----- global defines ----------------------------------------------- */
32 #define bit_dbg(level, dev, format, args...) \
34 if (i2c_debug >= level) \
35 dev_dbg(dev, format, ##args); \
38 #define bit_dbg(level, dev, format, args...) \
42 /* ----- global variables --------------------------------------------- */
44 static int bit_test
; /* see if the line-setting functions work */
45 module_param(bit_test
, int, S_IRUGO
);
46 MODULE_PARM_DESC(bit_test
, "lines testing - 0 off; 1 report; 2 fail if stuck");
49 static int i2c_debug
= 1;
50 module_param(i2c_debug
, int, S_IRUGO
| S_IWUSR
);
51 MODULE_PARM_DESC(i2c_debug
,
52 "debug level - 0 off; 1 normal; 2 verbose; 3 very verbose");
55 /* --- setting states on the bus with the right timing: --------------- */
57 #define setsda(adap, val) adap->setsda(adap->data, val)
58 #define setscl(adap, val) adap->setscl(adap->data, val)
59 #define getsda(adap) adap->getsda(adap->data)
60 #define getscl(adap) adap->getscl(adap->data)
62 static inline void sdalo(struct i2c_algo_bit_data
*adap
)
65 udelay((adap
->udelay
+ 1) / 2);
68 static inline void sdahi(struct i2c_algo_bit_data
*adap
)
71 udelay((adap
->udelay
+ 1) / 2);
74 static inline void scllo(struct i2c_algo_bit_data
*adap
)
77 udelay(adap
->udelay
/ 2);
81 * Raise scl line, and do checking for delays. This is necessary for slower
84 static int sclhi(struct i2c_algo_bit_data
*adap
)
90 /* Not all adapters have scl sense line... */
95 while (!getscl(adap
)) {
96 /* This hw knows how to read the clock line, so we wait
97 * until it actually gets high. This is safer as some
98 * chips may hold it low ("clock stretching") while they
99 * are processing data internally.
101 if (time_after(jiffies
, start
+ adap
->timeout
)) {
102 /* Test one last time, as we may have been preempted
103 * between last check and timeout test.
112 if (jiffies
!= start
&& i2c_debug
>= 3)
113 pr_debug("i2c-algo-bit: needed %ld jiffies for SCL to go "
114 "high\n", jiffies
- start
);
118 udelay(adap
->udelay
);
123 /* --- other auxiliary functions -------------------------------------- */
124 static void i2c_start(struct i2c_algo_bit_data
*adap
)
126 /* assert: scl, sda are high */
128 udelay(adap
->udelay
);
132 static void i2c_repstart(struct i2c_algo_bit_data
*adap
)
134 /* assert: scl is low */
138 udelay(adap
->udelay
);
143 static void i2c_stop(struct i2c_algo_bit_data
*adap
)
145 /* assert: scl is low */
149 udelay(adap
->udelay
);
154 /* send a byte without start cond., look for arbitration,
155 check ackn. from slave */
157 * 1 if the device acknowledged
158 * 0 if the device did not ack
159 * -ETIMEDOUT if an error occurred (while raising the scl line)
161 static int i2c_outb(struct i2c_adapter
*i2c_adap
, unsigned char c
)
166 struct i2c_algo_bit_data
*adap
= i2c_adap
->algo_data
;
168 /* assert: scl is low */
169 for (i
= 7; i
>= 0; i
--) {
172 udelay((adap
->udelay
+ 1) / 2);
173 if (sclhi(adap
) < 0) { /* timed out */
174 bit_dbg(1, &i2c_adap
->dev
, "i2c_outb: 0x%02x, "
175 "timeout at bit #%d\n", (int)c
, i
);
178 /* FIXME do arbitration here:
179 * if (sb && !getsda(adap)) -> ouch! Get out of here.
181 * Report a unique code, so higher level code can retry
182 * the whole (combined) message and *NOT* issue STOP.
187 if (sclhi(adap
) < 0) { /* timeout */
188 bit_dbg(1, &i2c_adap
->dev
, "i2c_outb: 0x%02x, "
189 "timeout at ack\n", (int)c
);
193 /* read ack: SDA should be pulled down by slave, or it may
194 * NAK (usually to report problems with the data we wrote).
196 ack
= !getsda(adap
); /* ack: sda is pulled low -> success */
197 bit_dbg(2, &i2c_adap
->dev
, "i2c_outb: 0x%02x %s\n", (int)c
,
202 /* assert: scl is low (sda undef) */
206 static int i2c_inb(struct i2c_adapter
*i2c_adap
)
208 /* read byte via i2c port, without start/stop sequence */
209 /* acknowledge is sent in i2c_read. */
211 unsigned char indata
= 0;
212 struct i2c_algo_bit_data
*adap
= i2c_adap
->algo_data
;
214 /* assert: scl is low */
216 for (i
= 0; i
< 8; i
++) {
217 if (sclhi(adap
) < 0) { /* timeout */
218 bit_dbg(1, &i2c_adap
->dev
, "i2c_inb: timeout at bit "
226 udelay(i
== 7 ? adap
->udelay
/ 2 : adap
->udelay
);
228 /* assert: scl is low */
233 * Sanity check for the adapter hardware - check the reaction of
234 * the bus lines only if it seems to be idle.
236 static int test_bus(struct i2c_adapter
*i2c_adap
)
238 struct i2c_algo_bit_data
*adap
= i2c_adap
->algo_data
;
239 const char *name
= i2c_adap
->name
;
242 if (adap
->pre_xfer
) {
243 ret
= adap
->pre_xfer(i2c_adap
);
248 if (adap
->getscl
== NULL
)
249 pr_info("%s: Testing SDA only, SCL is not readable\n", name
);
252 scl
= (adap
->getscl
== NULL
) ? 1 : getscl(adap
);
255 "%s: bus seems to be busy (scl=%d, sda=%d)\n",
262 scl
= (adap
->getscl
== NULL
) ? 1 : getscl(adap
);
264 printk(KERN_WARNING
"%s: SDA stuck high!\n", name
);
268 printk(KERN_WARNING
"%s: SCL unexpected low "
269 "while pulling SDA low!\n", name
);
275 scl
= (adap
->getscl
== NULL
) ? 1 : getscl(adap
);
277 printk(KERN_WARNING
"%s: SDA stuck low!\n", name
);
281 printk(KERN_WARNING
"%s: SCL unexpected low "
282 "while pulling SDA high!\n", name
);
288 scl
= (adap
->getscl
== NULL
) ? 0 : getscl(adap
);
290 printk(KERN_WARNING
"%s: SCL stuck high!\n", name
);
294 printk(KERN_WARNING
"%s: SDA unexpected low "
295 "while pulling SCL low!\n", name
);
301 scl
= (adap
->getscl
== NULL
) ? 1 : getscl(adap
);
303 printk(KERN_WARNING
"%s: SCL stuck low!\n", name
);
307 printk(KERN_WARNING
"%s: SDA unexpected low "
308 "while pulling SCL high!\n", name
);
313 adap
->post_xfer(i2c_adap
);
315 pr_info("%s: Test OK\n", name
);
322 adap
->post_xfer(i2c_adap
);
327 /* ----- Utility functions
330 /* try_address tries to contact a chip for a number of
331 * times before it gives up.
334 * 0 chip did not answer
335 * -x transmission error
337 static int try_address(struct i2c_adapter
*i2c_adap
,
338 unsigned char addr
, int retries
)
340 struct i2c_algo_bit_data
*adap
= i2c_adap
->algo_data
;
343 for (i
= 0; i
<= retries
; i
++) {
344 ret
= i2c_outb(i2c_adap
, addr
);
345 if (ret
== 1 || i
== retries
)
347 bit_dbg(3, &i2c_adap
->dev
, "emitting stop condition\n");
349 udelay(adap
->udelay
);
351 bit_dbg(3, &i2c_adap
->dev
, "emitting start condition\n");
355 bit_dbg(1, &i2c_adap
->dev
, "Used %d tries to %s client at "
356 "0x%02x: %s\n", i
+ 1,
357 addr
& 1 ? "read from" : "write to", addr
>> 1,
358 ret
== 1 ? "success" : "failed, timeout?");
362 static int sendbytes(struct i2c_adapter
*i2c_adap
, struct i2c_msg
*msg
)
364 const unsigned char *temp
= msg
->buf
;
365 int count
= msg
->len
;
366 unsigned short nak_ok
= msg
->flags
& I2C_M_IGNORE_NAK
;
371 retval
= i2c_outb(i2c_adap
, *temp
);
373 /* OK/ACK; or ignored NAK */
374 if ((retval
> 0) || (nak_ok
&& (retval
== 0))) {
379 /* A slave NAKing the master means the slave didn't like
380 * something about the data it saw. For example, maybe
381 * the SMBus PEC was wrong.
383 } else if (retval
== 0) {
384 dev_err(&i2c_adap
->dev
, "sendbytes: NAK bailout.\n");
387 /* Timeout; or (someday) lost arbitration
389 * FIXME Lost ARB implies retrying the transaction from
390 * the first message, after the "winning" master issues
391 * its STOP. As a rule, upper layer code has no reason
392 * to know or care about this ... it is *NOT* an error.
395 dev_err(&i2c_adap
->dev
, "sendbytes: error %d\n",
403 static int acknak(struct i2c_adapter
*i2c_adap
, int is_ack
)
405 struct i2c_algo_bit_data
*adap
= i2c_adap
->algo_data
;
407 /* assert: sda is high */
408 if (is_ack
) /* send ack */
410 udelay((adap
->udelay
+ 1) / 2);
411 if (sclhi(adap
) < 0) { /* timeout */
412 dev_err(&i2c_adap
->dev
, "readbytes: ack/nak timeout\n");
419 static int readbytes(struct i2c_adapter
*i2c_adap
, struct i2c_msg
*msg
)
422 int rdcount
= 0; /* counts bytes read */
423 unsigned char *temp
= msg
->buf
;
424 int count
= msg
->len
;
425 const unsigned flags
= msg
->flags
;
428 inval
= i2c_inb(i2c_adap
);
432 } else { /* read timed out */
439 /* Some SMBus transactions require that we receive the
440 transaction length as the first read byte. */
441 if (rdcount
== 1 && (flags
& I2C_M_RECV_LEN
)) {
442 if (inval
<= 0 || inval
> I2C_SMBUS_BLOCK_MAX
) {
443 if (!(flags
& I2C_M_NO_RD_ACK
))
445 dev_err(&i2c_adap
->dev
, "readbytes: invalid "
446 "block length (%d)\n", inval
);
449 /* The original count value accounts for the extra
450 bytes, that is, either 1 for a regular transaction,
451 or 2 for a PEC transaction. */
456 bit_dbg(2, &i2c_adap
->dev
, "readbytes: 0x%02x %s\n",
458 (flags
& I2C_M_NO_RD_ACK
)
460 : (count
? "A" : "NA"));
462 if (!(flags
& I2C_M_NO_RD_ACK
)) {
463 inval
= acknak(i2c_adap
, count
);
471 /* doAddress initiates the transfer by generating the start condition (in
472 * try_address) and transmits the address in the necessary format to handle
473 * reads, writes as well as 10bit-addresses.
475 * 0 everything went okay, the chip ack'ed, or IGNORE_NAK flag was set
476 * -x an error occurred (like: -ENXIO if the device did not answer, or
477 * -ETIMEDOUT, for example if the lines are stuck...)
479 static int bit_doAddress(struct i2c_adapter
*i2c_adap
, struct i2c_msg
*msg
)
481 unsigned short flags
= msg
->flags
;
482 unsigned short nak_ok
= msg
->flags
& I2C_M_IGNORE_NAK
;
483 struct i2c_algo_bit_data
*adap
= i2c_adap
->algo_data
;
488 retries
= nak_ok
? 0 : i2c_adap
->retries
;
490 if (flags
& I2C_M_TEN
) {
491 /* a ten bit address */
492 addr
= 0xf0 | ((msg
->addr
>> 7) & 0x06);
493 bit_dbg(2, &i2c_adap
->dev
, "addr0: %d\n", addr
);
494 /* try extended address code...*/
495 ret
= try_address(i2c_adap
, addr
, retries
);
496 if ((ret
!= 1) && !nak_ok
) {
497 dev_err(&i2c_adap
->dev
,
498 "died at extended address code\n");
501 /* the remaining 8 bit address */
502 ret
= i2c_outb(i2c_adap
, msg
->addr
& 0xff);
503 if ((ret
!= 1) && !nak_ok
) {
504 /* the chip did not ack / xmission error occurred */
505 dev_err(&i2c_adap
->dev
, "died at 2nd address code\n");
508 if (flags
& I2C_M_RD
) {
509 bit_dbg(3, &i2c_adap
->dev
, "emitting repeated "
510 "start condition\n");
512 /* okay, now switch into reading mode */
514 ret
= try_address(i2c_adap
, addr
, retries
);
515 if ((ret
!= 1) && !nak_ok
) {
516 dev_err(&i2c_adap
->dev
,
517 "died at repeated address code\n");
521 } else { /* normal 7bit address */
522 addr
= msg
->addr
<< 1;
523 if (flags
& I2C_M_RD
)
525 if (flags
& I2C_M_REV_DIR_ADDR
)
527 ret
= try_address(i2c_adap
, addr
, retries
);
528 if ((ret
!= 1) && !nak_ok
)
535 static int bit_xfer(struct i2c_adapter
*i2c_adap
,
536 struct i2c_msg msgs
[], int num
)
538 struct i2c_msg
*pmsg
;
539 struct i2c_algo_bit_data
*adap
= i2c_adap
->algo_data
;
541 unsigned short nak_ok
;
543 if (adap
->pre_xfer
) {
544 ret
= adap
->pre_xfer(i2c_adap
);
549 bit_dbg(3, &i2c_adap
->dev
, "emitting start condition\n");
551 for (i
= 0; i
< num
; i
++) {
553 nak_ok
= pmsg
->flags
& I2C_M_IGNORE_NAK
;
554 if (!(pmsg
->flags
& I2C_M_NOSTART
)) {
556 bit_dbg(3, &i2c_adap
->dev
, "emitting "
557 "repeated start condition\n");
560 ret
= bit_doAddress(i2c_adap
, pmsg
);
561 if ((ret
!= 0) && !nak_ok
) {
562 bit_dbg(1, &i2c_adap
->dev
, "NAK from "
563 "device addr 0x%02x msg #%d\n",
568 if (pmsg
->flags
& I2C_M_RD
) {
569 /* read bytes into buffer*/
570 ret
= readbytes(i2c_adap
, pmsg
);
572 bit_dbg(2, &i2c_adap
->dev
, "read %d byte%s\n",
573 ret
, ret
== 1 ? "" : "s");
574 if (ret
< pmsg
->len
) {
580 /* write bytes from buffer */
581 ret
= sendbytes(i2c_adap
, pmsg
);
583 bit_dbg(2, &i2c_adap
->dev
, "wrote %d byte%s\n",
584 ret
, ret
== 1 ? "" : "s");
585 if (ret
< pmsg
->len
) {
595 bit_dbg(3, &i2c_adap
->dev
, "emitting stop condition\n");
599 adap
->post_xfer(i2c_adap
);
603 static u32
bit_func(struct i2c_adapter
*adap
)
605 return I2C_FUNC_I2C
| I2C_FUNC_NOSTART
| I2C_FUNC_SMBUS_EMUL
|
606 I2C_FUNC_SMBUS_READ_BLOCK_DATA
|
607 I2C_FUNC_SMBUS_BLOCK_PROC_CALL
|
608 I2C_FUNC_10BIT_ADDR
| I2C_FUNC_PROTOCOL_MANGLING
;
612 /* -----exported algorithm data: ------------------------------------- */
614 const struct i2c_algorithm i2c_bit_algo
= {
615 .master_xfer
= bit_xfer
,
616 .functionality
= bit_func
,
618 EXPORT_SYMBOL(i2c_bit_algo
);
621 * registering functions to load algorithms at runtime
623 static int __i2c_bit_add_bus(struct i2c_adapter
*adap
,
624 int (*add_adapter
)(struct i2c_adapter
*))
626 struct i2c_algo_bit_data
*bit_adap
= adap
->algo_data
;
630 ret
= test_bus(adap
);
631 if (bit_test
>= 2 && ret
< 0)
635 /* register new adapter to i2c module... */
636 adap
->algo
= &i2c_bit_algo
;
639 ret
= add_adapter(adap
);
643 /* Complain if SCL can't be read */
644 if (bit_adap
->getscl
== NULL
) {
645 dev_warn(&adap
->dev
, "Not I2C compliant: can't read SCL\n");
646 dev_warn(&adap
->dev
, "Bus may be unreliable\n");
651 int i2c_bit_add_bus(struct i2c_adapter
*adap
)
653 return __i2c_bit_add_bus(adap
, i2c_add_adapter
);
655 EXPORT_SYMBOL(i2c_bit_add_bus
);
657 int i2c_bit_add_numbered_bus(struct i2c_adapter
*adap
)
659 return __i2c_bit_add_bus(adap
, i2c_add_numbered_adapter
);
661 EXPORT_SYMBOL(i2c_bit_add_numbered_bus
);
663 MODULE_AUTHOR("Simon G. Vogl <simon@tk.uni-linz.ac.at>");
664 MODULE_DESCRIPTION("I2C-Bus bit-banging algorithm");
665 MODULE_LICENSE("GPL");