1 // SPDX-License-Identifier: GPL-2.0+ 2 /* 3 * i2c-algo-bit.c: i2c driver algorithms for bit-shift adapters 4 * 5 * Copyright (C) 1995-2000 Simon G. Vogl 6 * 7 * With some changes from Frodo Looijaard <frodol@dds.nl>, Kyösti Mälkki 8 * <kmalkki@cc.hut.fi> and Jean Delvare <jdelvare@suse.de> 9 */ 10 11 #include <linux/kernel.h> 12 #include <linux/module.h> 13 #include <linux/delay.h> 14 #include <linux/errno.h> 15 #include <linux/sched.h> 16 #include <linux/string_choices.h> 17 #include <linux/i2c.h> 18 #include <linux/i2c-algo-bit.h> 19 20 21 /* ----- global defines ----------------------------------------------- */ 22 23 #ifdef DEBUG 24 #define bit_dbg(level, dev, format, args...) \ 25 do { \ 26 if (i2c_debug >= level) \ 27 dev_dbg(dev, format, ##args); \ 28 } while (0) 29 #else 30 #define bit_dbg(level, dev, format, args...) \ 31 do {} while (0) 32 #endif /* DEBUG */ 33 34 /* ----- global variables --------------------------------------------- */ 35 36 static int bit_test; /* see if the line-setting functions work */ 37 module_param(bit_test, int, S_IRUGO); 38 MODULE_PARM_DESC(bit_test, "lines testing - 0 off; 1 report; 2 fail if stuck"); 39 40 #ifdef DEBUG 41 static int i2c_debug = 1; 42 module_param(i2c_debug, int, S_IRUGO | S_IWUSR); 43 MODULE_PARM_DESC(i2c_debug, 44 "debug level - 0 off; 1 normal; 2 verbose; 3 very verbose"); 45 #endif 46 47 /* --- setting states on the bus with the right timing: --------------- */ 48 49 #define setsda(adap, val) adap->setsda(adap->data, val) 50 #define setscl(adap, val) adap->setscl(adap->data, val) 51 #define getsda(adap) adap->getsda(adap->data) 52 #define getscl(adap) adap->getscl(adap->data) 53 54 static inline void sdalo(struct i2c_algo_bit_data *adap) 55 { 56 setsda(adap, 0); 57 udelay((adap->udelay + 1) / 2); 58 } 59 60 static inline void sdahi(struct i2c_algo_bit_data *adap) 61 { 62 setsda(adap, 1); 63 udelay((adap->udelay + 1) / 2); 64 } 65 66 static inline void scllo(struct i2c_algo_bit_data *adap) 67 { 68 setscl(adap, 0); 69 udelay(adap->udelay / 2); 70 } 71 72 /* 73 * Raise scl line, and do checking for delays. This is necessary for slower 74 * devices. 75 */ 76 static int sclhi(struct i2c_algo_bit_data *adap) 77 { 78 unsigned long start; 79 80 setscl(adap, 1); 81 82 /* Not all adapters have scl sense line... */ 83 if (!adap->getscl) 84 goto done; 85 86 start = jiffies; 87 while (!getscl(adap)) { 88 /* This hw knows how to read the clock line, so we wait 89 * until it actually gets high. This is safer as some 90 * chips may hold it low ("clock stretching") while they 91 * are processing data internally. 92 */ 93 if (time_after(jiffies, start + adap->timeout)) { 94 /* Test one last time, as we may have been preempted 95 * between last check and timeout test. 96 */ 97 if (getscl(adap)) 98 break; 99 return -ETIMEDOUT; 100 } 101 cpu_relax(); 102 } 103 #ifdef DEBUG 104 if (jiffies != start && i2c_debug >= 3) 105 pr_debug("i2c-algo-bit: needed %ld jiffies for SCL to go high\n", 106 jiffies - start); 107 #endif 108 109 done: 110 udelay(adap->udelay); 111 return 0; 112 } 113 114 115 /* --- other auxiliary functions -------------------------------------- */ 116 static void i2c_start(struct i2c_algo_bit_data *adap) 117 { 118 /* assert: scl, sda are high */ 119 setsda(adap, 0); 120 udelay(adap->udelay); 121 scllo(adap); 122 } 123 124 static void i2c_repstart(struct i2c_algo_bit_data *adap) 125 { 126 /* assert: scl is low */ 127 sdahi(adap); 128 sclhi(adap); 129 setsda(adap, 0); 130 udelay(adap->udelay); 131 scllo(adap); 132 } 133 134 135 static void i2c_stop(struct i2c_algo_bit_data *adap) 136 { 137 /* assert: scl is low */ 138 sdalo(adap); 139 sclhi(adap); 140 setsda(adap, 1); 141 udelay(adap->udelay); 142 } 143 144 145 146 /* send a byte without start cond., look for arbitration, 147 check ackn. from slave */ 148 /* returns: 149 * 1 if the device acknowledged 150 * 0 if the device did not ack 151 * -ETIMEDOUT if an error occurred (while raising the scl line) 152 */ 153 static int i2c_outb(struct i2c_adapter *i2c_adap, unsigned char c) 154 { 155 int i; 156 int sb; 157 int ack; 158 struct i2c_algo_bit_data *adap = i2c_adap->algo_data; 159 160 /* assert: scl is low */ 161 for (i = 7; i >= 0; i--) { 162 sb = (c >> i) & 1; 163 setsda(adap, sb); 164 udelay((adap->udelay + 1) / 2); 165 if (sclhi(adap) < 0) { /* timed out */ 166 bit_dbg(1, &i2c_adap->dev, 167 "i2c_outb: 0x%02x, timeout at bit #%d\n", 168 (int)c, i); 169 return -ETIMEDOUT; 170 } 171 /* FIXME do arbitration here: 172 * if (sb && !getsda(adap)) -> ouch! Get out of here. 173 * 174 * Report a unique code, so higher level code can retry 175 * the whole (combined) message and *NOT* issue STOP. 176 */ 177 scllo(adap); 178 } 179 sdahi(adap); 180 if (sclhi(adap) < 0) { /* timeout */ 181 bit_dbg(1, &i2c_adap->dev, 182 "i2c_outb: 0x%02x, timeout at ack\n", (int)c); 183 return -ETIMEDOUT; 184 } 185 186 /* read ack: SDA should be pulled down by slave, or it may 187 * NAK (usually to report problems with the data we wrote). 188 * Always report ACK if SDA is write-only. 189 */ 190 ack = !adap->getsda || !getsda(adap); /* ack: sda is pulled low -> success */ 191 bit_dbg(2, &i2c_adap->dev, "i2c_outb: 0x%02x %s\n", (int)c, 192 ack ? "A" : "NA"); 193 194 scllo(adap); 195 return ack; 196 /* assert: scl is low (sda undef) */ 197 } 198 199 200 static int i2c_inb(struct i2c_adapter *i2c_adap) 201 { 202 /* read byte via i2c port, without start/stop sequence */ 203 /* acknowledge is sent in i2c_read. */ 204 int i; 205 unsigned char indata = 0; 206 struct i2c_algo_bit_data *adap = i2c_adap->algo_data; 207 208 /* assert: scl is low */ 209 sdahi(adap); 210 for (i = 0; i < 8; i++) { 211 if (sclhi(adap) < 0) { /* timeout */ 212 bit_dbg(1, &i2c_adap->dev, 213 "i2c_inb: timeout at bit #%d\n", 214 7 - i); 215 return -ETIMEDOUT; 216 } 217 indata *= 2; 218 if (getsda(adap)) 219 indata |= 0x01; 220 setscl(adap, 0); 221 udelay(i == 7 ? adap->udelay / 2 : adap->udelay); 222 } 223 /* assert: scl is low */ 224 return indata; 225 } 226 227 /* 228 * Sanity check for the adapter hardware - check the reaction of 229 * the bus lines only if it seems to be idle. 230 */ 231 static int test_bus(struct i2c_adapter *i2c_adap) 232 { 233 struct i2c_algo_bit_data *adap = i2c_adap->algo_data; 234 const char *name = i2c_adap->name; 235 int scl, sda, ret; 236 237 if (adap->pre_xfer) { 238 ret = adap->pre_xfer(i2c_adap); 239 if (ret < 0) 240 return -ENODEV; 241 } 242 243 if (adap->getsda == NULL) 244 pr_info("%s: SDA is write-only, testing not possible\n", name); 245 if (adap->getscl == NULL) 246 pr_info("%s: SCL is write-only, testing not possible\n", name); 247 248 sda = adap->getsda ? getsda(adap) : 1; 249 scl = adap->getscl ? getscl(adap) : 1; 250 if (!scl || !sda) { 251 pr_warn("%s: bus seems to be busy (scl=%d, sda=%d)\n", name, scl, sda); 252 goto bailout; 253 } 254 255 sdalo(adap); 256 if (adap->getsda && getsda(adap)) { 257 pr_warn("%s: SDA stuck high!\n", name); 258 goto bailout; 259 } 260 if (adap->getscl && !getscl(adap)) { 261 pr_warn("%s: SCL unexpected low while pulling SDA low!\n", name); 262 goto bailout; 263 } 264 265 sdahi(adap); 266 if (adap->getsda && !getsda(adap)) { 267 pr_warn("%s: SDA stuck low!\n", name); 268 goto bailout; 269 } 270 if (adap->getscl && !getscl(adap)) { 271 pr_warn("%s: SCL unexpected low while pulling SDA high!\n", name); 272 goto bailout; 273 } 274 275 scllo(adap); 276 if (adap->getscl && getscl(adap)) { 277 pr_warn("%s: SCL stuck high!\n", name); 278 goto bailout; 279 } 280 if (adap->getsda && !getsda(adap)) { 281 pr_warn("%s: SDA unexpected low while pulling SCL low!\n", name); 282 goto bailout; 283 } 284 285 sclhi(adap); 286 if (adap->getscl && !getscl(adap)) { 287 pr_warn("%s: SCL stuck low!\n", name); 288 goto bailout; 289 } 290 if (adap->getsda && !getsda(adap)) { 291 pr_warn("%s: SDA unexpected low while pulling SCL high!\n", name); 292 goto bailout; 293 } 294 295 if (adap->post_xfer) 296 adap->post_xfer(i2c_adap); 297 298 pr_info("%s: Test OK\n", name); 299 return 0; 300 bailout: 301 sdahi(adap); 302 sclhi(adap); 303 304 if (adap->post_xfer) 305 adap->post_xfer(i2c_adap); 306 307 return -ENODEV; 308 } 309 310 /* ----- Utility functions 311 */ 312 313 /* try_address tries to contact a chip for a number of 314 * times before it gives up. 315 * return values: 316 * 1 chip answered 317 * 0 chip did not answer 318 * -x transmission error 319 */ 320 static int try_address(struct i2c_adapter *i2c_adap, 321 unsigned char addr, int retries) 322 { 323 struct i2c_algo_bit_data *adap = i2c_adap->algo_data; 324 int i, ret = 0; 325 326 for (i = 0; i <= retries; i++) { 327 ret = i2c_outb(i2c_adap, addr); 328 if (ret == 1 || i == retries) 329 break; 330 bit_dbg(3, &i2c_adap->dev, "emitting stop condition\n"); 331 i2c_stop(adap); 332 udelay(adap->udelay); 333 yield(); 334 bit_dbg(3, &i2c_adap->dev, "emitting start condition\n"); 335 i2c_start(adap); 336 } 337 if (i && ret) 338 bit_dbg(1, &i2c_adap->dev, 339 "Used %d tries to %s client at 0x%02x: %s\n", i + 1, 340 addr & 1 ? "read from" : "write to", addr >> 1, 341 ret == 1 ? "success" : "failed, timeout?"); 342 return ret; 343 } 344 345 static int sendbytes(struct i2c_adapter *i2c_adap, struct i2c_msg *msg) 346 { 347 const unsigned char *temp = msg->buf; 348 int count = msg->len; 349 unsigned short nak_ok = msg->flags & I2C_M_IGNORE_NAK; 350 int retval; 351 int wrcount = 0; 352 353 while (count > 0) { 354 retval = i2c_outb(i2c_adap, *temp); 355 356 /* OK/ACK; or ignored NAK */ 357 if ((retval > 0) || (nak_ok && (retval == 0))) { 358 count--; 359 temp++; 360 wrcount++; 361 362 /* A slave NAKing the master means the slave didn't like 363 * something about the data it saw. For example, maybe 364 * the SMBus PEC was wrong. 365 */ 366 } else if (retval == 0) { 367 dev_err(&i2c_adap->dev, "sendbytes: NAK bailout.\n"); 368 return -EIO; 369 370 /* Timeout; or (someday) lost arbitration 371 * 372 * FIXME Lost ARB implies retrying the transaction from 373 * the first message, after the "winning" master issues 374 * its STOP. As a rule, upper layer code has no reason 375 * to know or care about this ... it is *NOT* an error. 376 */ 377 } else { 378 dev_err(&i2c_adap->dev, "sendbytes: error %d\n", 379 retval); 380 return retval; 381 } 382 } 383 return wrcount; 384 } 385 386 static int acknak(struct i2c_adapter *i2c_adap, int is_ack) 387 { 388 struct i2c_algo_bit_data *adap = i2c_adap->algo_data; 389 390 /* assert: sda is high */ 391 if (is_ack) /* send ack */ 392 setsda(adap, 0); 393 udelay((adap->udelay + 1) / 2); 394 if (sclhi(adap) < 0) { /* timeout */ 395 dev_err(&i2c_adap->dev, "readbytes: ack/nak timeout\n"); 396 return -ETIMEDOUT; 397 } 398 scllo(adap); 399 return 0; 400 } 401 402 static int readbytes(struct i2c_adapter *i2c_adap, struct i2c_msg *msg) 403 { 404 int inval; 405 int rdcount = 0; /* counts bytes read */ 406 unsigned char *temp = msg->buf; 407 int count = msg->len; 408 const unsigned flags = msg->flags; 409 struct i2c_algo_bit_data *adap = i2c_adap->algo_data; 410 411 if (!adap->getsda) 412 return -EOPNOTSUPP; 413 414 while (count > 0) { 415 inval = i2c_inb(i2c_adap); 416 if (inval >= 0) { 417 *temp = inval; 418 rdcount++; 419 } else { /* read timed out */ 420 break; 421 } 422 423 temp++; 424 count--; 425 426 /* Some SMBus transactions require that we receive the 427 transaction length as the first read byte. */ 428 if (rdcount == 1 && (flags & I2C_M_RECV_LEN)) { 429 if (inval <= 0 || inval > I2C_SMBUS_BLOCK_MAX) { 430 if (!(flags & I2C_M_NO_RD_ACK)) 431 acknak(i2c_adap, 0); 432 dev_err(&i2c_adap->dev, 433 "readbytes: invalid block length (%d)\n", 434 inval); 435 return -EPROTO; 436 } 437 /* The original count value accounts for the extra 438 bytes, that is, either 1 for a regular transaction, 439 or 2 for a PEC transaction. */ 440 count += inval; 441 msg->len += inval; 442 } 443 444 bit_dbg(2, &i2c_adap->dev, "readbytes: 0x%02x %s\n", 445 inval, 446 (flags & I2C_M_NO_RD_ACK) 447 ? "(no ack/nak)" 448 : (count ? "A" : "NA")); 449 450 if (!(flags & I2C_M_NO_RD_ACK)) { 451 inval = acknak(i2c_adap, count); 452 if (inval < 0) 453 return inval; 454 } 455 } 456 return rdcount; 457 } 458 459 /* doAddress initiates the transfer by generating the start condition (in 460 * try_address) and transmits the address in the necessary format to handle 461 * reads, writes as well as 10bit-addresses. 462 * returns: 463 * 0 everything went okay, the chip ack'ed, or IGNORE_NAK flag was set 464 * -x an error occurred (like: -ENXIO if the device did not answer, or 465 * -ETIMEDOUT, for example if the lines are stuck...) 466 */ 467 static int bit_doAddress(struct i2c_adapter *i2c_adap, struct i2c_msg *msg) 468 { 469 unsigned short flags = msg->flags; 470 unsigned short nak_ok = msg->flags & I2C_M_IGNORE_NAK; 471 struct i2c_algo_bit_data *adap = i2c_adap->algo_data; 472 473 unsigned char addr; 474 int ret, retries; 475 476 retries = nak_ok ? 0 : i2c_adap->retries; 477 478 if (flags & I2C_M_TEN) { 479 /* a ten bit address */ 480 addr = 0xf0 | ((msg->addr >> 7) & 0x06); 481 bit_dbg(2, &i2c_adap->dev, "addr0: %d\n", addr); 482 /* try extended address code...*/ 483 ret = try_address(i2c_adap, addr, retries); 484 if ((ret != 1) && !nak_ok) { 485 dev_err(&i2c_adap->dev, 486 "died at extended address code\n"); 487 return -ENXIO; 488 } 489 /* the remaining 8 bit address */ 490 ret = i2c_outb(i2c_adap, msg->addr & 0xff); 491 if ((ret != 1) && !nak_ok) { 492 /* the chip did not ack / xmission error occurred */ 493 dev_err(&i2c_adap->dev, "died at 2nd address code\n"); 494 return -ENXIO; 495 } 496 if (flags & I2C_M_RD) { 497 bit_dbg(3, &i2c_adap->dev, 498 "emitting repeated start condition\n"); 499 i2c_repstart(adap); 500 /* okay, now switch into reading mode */ 501 addr |= 0x01; 502 ret = try_address(i2c_adap, addr, retries); 503 if ((ret != 1) && !nak_ok) { 504 dev_err(&i2c_adap->dev, 505 "died at repeated address code\n"); 506 return -EIO; 507 } 508 } 509 } else { /* normal 7bit address */ 510 addr = i2c_8bit_addr_from_msg(msg); 511 if (flags & I2C_M_REV_DIR_ADDR) 512 addr ^= 1; 513 ret = try_address(i2c_adap, addr, retries); 514 if ((ret != 1) && !nak_ok) 515 return -ENXIO; 516 } 517 518 return 0; 519 } 520 521 static int bit_xfer(struct i2c_adapter *i2c_adap, 522 struct i2c_msg msgs[], int num) 523 { 524 struct i2c_msg *pmsg; 525 struct i2c_algo_bit_data *adap = i2c_adap->algo_data; 526 int i, ret; 527 unsigned short nak_ok; 528 529 if (adap->pre_xfer) { 530 ret = adap->pre_xfer(i2c_adap); 531 if (ret < 0) 532 return ret; 533 } 534 535 bit_dbg(3, &i2c_adap->dev, "emitting start condition\n"); 536 i2c_start(adap); 537 for (i = 0; i < num; i++) { 538 pmsg = &msgs[i]; 539 nak_ok = pmsg->flags & I2C_M_IGNORE_NAK; 540 if (!(pmsg->flags & I2C_M_NOSTART)) { 541 if (i) { 542 if (msgs[i - 1].flags & I2C_M_STOP) { 543 bit_dbg(3, &i2c_adap->dev, 544 "emitting enforced stop/start condition\n"); 545 i2c_stop(adap); 546 i2c_start(adap); 547 } else { 548 bit_dbg(3, &i2c_adap->dev, 549 "emitting repeated start condition\n"); 550 i2c_repstart(adap); 551 } 552 } 553 ret = bit_doAddress(i2c_adap, pmsg); 554 if ((ret != 0) && !nak_ok) { 555 bit_dbg(1, &i2c_adap->dev, 556 "NAK from device addr 0x%02x msg #%d\n", 557 msgs[i].addr, i); 558 goto bailout; 559 } 560 } 561 if (pmsg->flags & I2C_M_RD) { 562 /* read bytes into buffer*/ 563 ret = readbytes(i2c_adap, pmsg); 564 if (ret >= 1) 565 bit_dbg(2, &i2c_adap->dev, "read %d byte%s\n", 566 ret, str_plural(ret)); 567 if (ret < pmsg->len) { 568 if (ret >= 0) 569 ret = -EIO; 570 goto bailout; 571 } 572 } else { 573 /* write bytes from buffer */ 574 ret = sendbytes(i2c_adap, pmsg); 575 if (ret >= 1) 576 bit_dbg(2, &i2c_adap->dev, "wrote %d byte%s\n", 577 ret, str_plural(ret)); 578 if (ret < pmsg->len) { 579 if (ret >= 0) 580 ret = -EIO; 581 goto bailout; 582 } 583 } 584 } 585 ret = i; 586 587 bailout: 588 bit_dbg(3, &i2c_adap->dev, "emitting stop condition\n"); 589 i2c_stop(adap); 590 591 if (adap->post_xfer) 592 adap->post_xfer(i2c_adap); 593 return ret; 594 } 595 596 /* 597 * We print a warning when we are not flagged to support atomic transfers but 598 * will try anyhow. That's what the I2C core would do as well. Sadly, we can't 599 * modify the algorithm struct at probe time because this struct is exported 600 * 'const'. 601 */ 602 static int bit_xfer_atomic(struct i2c_adapter *i2c_adap, struct i2c_msg msgs[], 603 int num) 604 { 605 struct i2c_algo_bit_data *adap = i2c_adap->algo_data; 606 607 if (!adap->can_do_atomic) 608 dev_warn(&i2c_adap->dev, "not flagged for atomic transfers\n"); 609 610 return bit_xfer(i2c_adap, msgs, num); 611 } 612 613 static u32 bit_func(struct i2c_adapter *adap) 614 { 615 return I2C_FUNC_I2C | I2C_FUNC_NOSTART | I2C_FUNC_SMBUS_EMUL_ALL | 616 I2C_FUNC_10BIT_ADDR | I2C_FUNC_PROTOCOL_MANGLING; 617 } 618 619 620 /* -----exported algorithm data: ------------------------------------- */ 621 622 const struct i2c_algorithm i2c_bit_algo = { 623 .xfer = bit_xfer, 624 .xfer_atomic = bit_xfer_atomic, 625 .functionality = bit_func, 626 }; 627 EXPORT_SYMBOL(i2c_bit_algo); 628 629 static const struct i2c_adapter_quirks i2c_bit_quirk_no_clk_stretch = { 630 .flags = I2C_AQ_NO_CLK_STRETCH, 631 }; 632 633 /* 634 * registering functions to load algorithms at runtime 635 */ 636 static int __i2c_bit_add_bus(struct i2c_adapter *adap, 637 int (*add_adapter)(struct i2c_adapter *)) 638 { 639 struct i2c_algo_bit_data *bit_adap = adap->algo_data; 640 int ret; 641 642 if (bit_test) { 643 ret = test_bus(adap); 644 if (bit_test >= 2 && ret < 0) 645 return -ENODEV; 646 } 647 648 /* register new adapter to i2c module... */ 649 adap->algo = &i2c_bit_algo; 650 adap->retries = 3; 651 if (bit_adap->getscl == NULL) 652 adap->quirks = &i2c_bit_quirk_no_clk_stretch; 653 654 /* 655 * We tried forcing SCL/SDA to an initial state here. But that caused a 656 * regression, sadly. Check Bugzilla #200045 for details. 657 */ 658 659 ret = add_adapter(adap); 660 if (ret < 0) 661 return ret; 662 663 if (bit_adap->getsda == NULL) 664 dev_warn(&adap->dev, "Not I2C compliant: can't read SDA\n"); 665 666 if (bit_adap->getscl == NULL) 667 dev_warn(&adap->dev, "Not I2C compliant: can't read SCL\n"); 668 669 if (bit_adap->getsda == NULL || bit_adap->getscl == NULL) 670 dev_warn(&adap->dev, "Bus may be unreliable\n"); 671 672 return 0; 673 } 674 675 int i2c_bit_add_bus(struct i2c_adapter *adap) 676 { 677 return __i2c_bit_add_bus(adap, i2c_add_adapter); 678 } 679 EXPORT_SYMBOL(i2c_bit_add_bus); 680 681 int i2c_bit_add_numbered_bus(struct i2c_adapter *adap) 682 { 683 return __i2c_bit_add_bus(adap, i2c_add_numbered_adapter); 684 } 685 EXPORT_SYMBOL(i2c_bit_add_numbered_bus); 686 687 MODULE_AUTHOR("Simon G. Vogl <simon@tk.uni-linz.ac.at>"); 688 MODULE_DESCRIPTION("I2C-Bus bit-banging algorithm"); 689 MODULE_LICENSE("GPL"); 690