25 #include <linux/kernel.h>
26 #include <linux/module.h>
29 #include <linux/errno.h>
30 #include <linux/sched.h>
31 #include <linux/i2c.h>
38 #define bit_dbg(level, dev, format, args...) \
40 if (i2c_debug >= level) \
41 dev_dbg(dev, format, ##args); \
44 #define bit_dbg(level, dev, format, args...) \
52 MODULE_PARM_DESC(bit_test,
"lines testing - 0 off; 1 report; 2 fail if stuck");
58 "debug level - 0 off; 1 normal; 2 verbose; 3 very verbose");
63 #define setsda(adap, val) adap->setsda(adap->data, val)
64 #define setscl(adap, val) adap->setscl(adap->data, val)
65 #define getsda(adap) adap->getsda(adap->data)
66 #define getscl(adap) adap->getscl(adap->data)
119 pr_debug(
"i2c-algo-bit: needed %ld jiffies for SCL to go "
120 "high\n", jiffies - start);
175 for (i = 7; i >= 0; i--) {
179 if (sclhi(adap) < 0) {
180 bit_dbg(1, &i2c_adap->
dev,
"i2c_outb: 0x%02x, "
181 "timeout at bit #%d\n", (
int)c, i);
193 if (sclhi(adap) < 0) {
194 bit_dbg(1, &i2c_adap->
dev,
"i2c_outb: 0x%02x, "
195 "timeout at ack\n", (
int)c);
203 bit_dbg(2, &i2c_adap->
dev,
"i2c_outb: 0x%02x %s\n", (
int)c,
217 unsigned char indata = 0;
222 for (i = 0; i < 8; i++) {
223 if (sclhi(adap) < 0) {
224 bit_dbg(1, &i2c_adap->
dev,
"i2c_inb: timeout at bit "
255 pr_info(
"%s: Testing SDA only, SCL is not readable\n", name);
261 "%s: bus seems to be busy (scl=%d, sda=%d)\n",
275 "while pulling SDA low!\n", name);
288 "while pulling SDA high!\n", name);
301 "while pulling SCL low!\n", name);
314 "while pulling SCL high!\n", name);
321 pr_info(
"%s: Test OK\n", name);
343 static int try_address(
struct i2c_adapter *i2c_adap,
349 for (i = 0; i <=
retries; i++) {
351 if (ret == 1 || i == retries)
353 bit_dbg(3, &i2c_adap->
dev,
"emitting stop condition\n");
357 bit_dbg(3, &i2c_adap->
dev,
"emitting start condition\n");
361 bit_dbg(1, &i2c_adap->
dev,
"Used %d tries to %s client at "
362 "0x%02x: %s\n", i + 1,
363 addr & 1 ?
"read from" :
"write to", addr >> 1,
364 ret == 1 ?
"success" :
"failed, timeout?");
370 const unsigned char *
temp = msg->
buf;
380 if ((retval > 0) || (nak_ok && (retval == 0))) {
389 }
else if (retval == 0) {
390 dev_err(&i2c_adap->
dev,
"sendbytes: NAK bailout.\n");
401 dev_err(&i2c_adap->
dev,
"sendbytes: error %d\n",
409 static int acknak(
struct i2c_adapter *i2c_adap,
int is_ack)
417 if (sclhi(adap) < 0) {
418 dev_err(&i2c_adap->
dev,
"readbytes: ack/nak timeout\n");
429 unsigned char *temp = msg->
buf;
430 int count = msg->
len;
452 "block length (%d)\n", inval);
462 bit_dbg(2, &i2c_adap->
dev,
"readbytes: 0x%02x %s\n",
466 : (count ?
"A" :
"NA"));
469 inval = acknak(i2c_adap, count);
487 unsigned short flags = msg->
flags;
494 retries = nak_ok ? 0 : i2c_adap->
retries;
498 addr = 0xf0 | ((msg->
addr >> 7) & 0x06);
499 bit_dbg(2, &i2c_adap->
dev,
"addr0: %d\n", addr);
501 ret = try_address(i2c_adap, addr, retries);
502 if ((ret != 1) && !nak_ok) {
504 "died at extended address code\n");
509 if ((ret != 1) && !nak_ok) {
511 dev_err(&i2c_adap->
dev,
"died at 2nd address code\n");
515 bit_dbg(3, &i2c_adap->
dev,
"emitting repeated "
516 "start condition\n");
520 ret = try_address(i2c_adap, addr, retries);
521 if ((ret != 1) && !nak_ok) {
523 "died at repeated address code\n");
528 addr = msg->
addr << 1;
529 if (flags & I2C_M_RD)
533 ret = try_address(i2c_adap, addr, retries);
534 if ((ret != 1) && !nak_ok)
542 struct i2c_msg msgs[],
int num)
547 unsigned short nak_ok;
555 bit_dbg(3, &i2c_adap->
dev,
"emitting start condition\n");
557 for (i = 0; i < num; i++) {
563 "repeated start condition\n");
566 ret = bit_doAddress(i2c_adap, pmsg);
567 if ((ret != 0) && !nak_ok) {
569 "device addr 0x%02x msg #%d\n",
574 if (pmsg->
flags & I2C_M_RD) {
576 ret = readbytes(i2c_adap, pmsg);
578 bit_dbg(2, &i2c_adap->
dev,
"read %d byte%s\n",
579 ret, ret == 1 ?
"" :
"s");
580 if (ret < pmsg->len) {
587 ret = sendbytes(i2c_adap, pmsg);
589 bit_dbg(2, &i2c_adap->
dev,
"wrote %d byte%s\n",
590 ret, ret == 1 ?
"" :
"s");
591 if (ret < pmsg->len) {
601 bit_dbg(3, &i2c_adap->
dev,
"emitting stop condition\n");
621 .master_xfer = bit_xfer,
622 .functionality = bit_func,
629 static int __i2c_bit_add_bus(
struct i2c_adapter *adap,
636 ret = test_bus(adap);
637 if (bit_test >= 2 && ret < 0)
645 ret = add_adapter(adap);
651 dev_warn(&adap->
dev,
"Not I2C compliant: can't read SCL\n");