Rev 1963 | Rev 3120 | Go to most recent revision | Show entire file | Regard whitespace | Details | Blame | Last modification | View Log | RSS feed
Rev 1963 | Rev 3031 | ||
---|---|---|---|
Line 13... | Line 13... | ||
13 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
13 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
14 | GNU General Public License for more details. |
14 | GNU General Public License for more details. |
Line 15... | Line 15... | ||
15 | 15 | ||
16 | You should have received a copy of the GNU General Public License |
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 |
17 | along with this program; if not, write to the Free Software |
- | 18 | Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, |
|
18 | Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. |
19 | MA 02110-1301 USA. |
Line 19... | Line 20... | ||
19 | * ------------------------------------------------------------------------- */ |
20 | * ------------------------------------------------------------------------- */ |
20 | 21 | ||
Line 21... | Line 22... | ||
21 | /* With some changes from Frodo Looijaard |
22 | /* With some changes from Frodo Looijaard |
22 |
|
23 |
|
23 | 24 | ||
- | 25 | #include |
|
24 | #include |
26 | #include |
25 | #include |
27 | #include |
26 | #include |
28 | #include |
27 | #include |
29 | #include |
Line -... | Line 30... | ||
- | 30 | #include |
|
Line 28... | Line 31... | ||
28 | #include |
31 | #include |
Line 29... | Line 32... | ||
29 | #include |
32 | #include |
30 | #include |
33 | |
Line 38... | Line 41... | ||
38 | if (i2c_debug >= level) \ |
41 | if (i2c_debug >= level) \ |
39 | dev_dbg(dev, format, ##args); \ |
42 | dev_dbg(dev, format, ##args); \ |
40 | } while (0) |
43 | } while (0) |
41 | #else |
44 | #else |
42 | #define bit_dbg(level, dev, format, args...) \ |
45 | #define bit_dbg(level, dev, format, args...) \ |
43 | do {} while (0) |
46 | do { /* dbgprintf(format, ##args); */ } while (0) |
44 | #endif /* DEBUG */ |
47 | #endif /* DEBUG */ |
Line 45... | Line 48... | ||
45 | 48 | ||
Line 46... | Line 49... | ||
46 | /* ----- global variables --------------------------------------------- */ |
49 | /* ----- global variables --------------------------------------------- */ |
Line -... | Line 50... | ||
- | 50 | ||
- | 51 | static int bit_test = 0; /* see if the line-setting functions work */ |
|
- | 52 | ||
- | 53 | #ifdef DEBUG |
|
- | 54 | static int i2c_debug = 1; |
|
- | 55 | module_param(i2c_debug, int, S_IRUGO | S_IWUSR); |
|
Line 47... | Line 56... | ||
47 | 56 | MODULE_PARM_DESC(i2c_debug, |
|
Line 48... | Line 57... | ||
48 | static int bit_test; /* see if the line-setting functions work */ |
57 | "debug level - 0 off; 1 normal; 2 verbose; 3 very verbose"); |
49 | 58 | #endif |
|
Line 85... | Line 94... | ||
85 | 94 | ||
86 | /* Not all adapters have scl sense line... */ |
95 | /* Not all adapters have scl sense line... */ |
87 | if (!adap->getscl) |
96 | if (!adap->getscl) |
Line 88... | Line 97... | ||
88 | goto done; |
97 | goto done; |
89 | 98 | ||
90 | // start = jiffies; |
99 | start = GetTimerTicks(); |
91 | while (!getscl(adap)) { |
100 | while (!getscl(adap)) { |
92 | /* This hw knows how to read the clock line, so we wait |
101 | /* This hw knows how to read the clock line, so we wait |
93 | * until it actually gets high. This is safer as some |
102 | * until it actually gets high. This is safer as some |
94 | * chips may hold it low ("clock stretching") while they |
103 | * chips may hold it low ("clock stretching") while they |
95 | * are processing data internally. |
104 | * are processing data internally. |
- | 105 | */ |
|
96 | */ |
106 | if (time_after(GetTimerTicks(), start + adap->timeout)) { |
97 | // if (time_after(jiffies, start + adap->timeout)) |
107 | /* Test one last time, as we may have been preempted |
- | 108 | * between last check and timeout test. |
|
- | 109 | */ |
|
98 | // return -ETIMEDOUT; |
110 | if (getscl(adap)) |
99 | 111 | break; |
|
100 | udelay(adap->udelay); |
112 | return -ETIMEDOUT; |
101 | 113 | } |
|
102 | // cond_resched(); |
- | |
103 | } |
- | |
104 | #ifdef DEBUG |
- | |
105 | if (jiffies != start && i2c_debug >= 3) |
- | |
106 | pr_debug("i2c-algo-bit: needed %ld jiffies for SCL to go " |
- | |
107 | "high\n", jiffies - start); |
- | |
108 | #endif |
114 | udelay(1); |
109 | 115 | } |
|
110 | done: |
116 | done: |
111 | udelay(adap->udelay); |
117 | udelay(adap->udelay); |
Line 237... | Line 243... | ||
237 | if (ret < 0) |
243 | if (ret < 0) |
238 | return -ENODEV; |
244 | return -ENODEV; |
239 | } |
245 | } |
Line 240... | Line 246... | ||
240 | 246 | ||
241 | if (adap->getscl == NULL) |
247 | if (adap->getscl == NULL) |
Line 242... | Line 248... | ||
242 | pr_info("%s: Testing SDA only, SCL is not readable\n", name); |
248 | dbgprintf("%s: Testing SDA only, SCL is not readable\n", name); |
243 | 249 | ||
244 | sda = getsda(adap); |
250 | sda = getsda(adap); |
- | 251 | scl = (adap->getscl == NULL) ? 1 : getscl(adap); |
|
245 | scl = (adap->getscl == NULL) ? 1 : getscl(adap); |
252 | if (!scl || !sda) { |
- | 253 | printk(KERN_WARNING |
|
246 | if (!scl || !sda) { |
254 | "%s: bus seems to be busy (scl=%d, sda=%d)\n", |
247 | printk(KERN_WARNING "%s: bus seems to be busy\n", name); |
255 | name, scl, sda); |
Line 248... | Line 256... | ||
248 | goto bailout; |
256 | goto bailout; |
249 | } |
257 | } |
Line 301... | Line 309... | ||
301 | } |
309 | } |
Line 302... | Line 310... | ||
302 | 310 | ||
303 | if (adap->post_xfer) |
311 | if (adap->post_xfer) |
Line 304... | Line 312... | ||
304 | adap->post_xfer(i2c_adap); |
312 | adap->post_xfer(i2c_adap); |
305 | 313 | ||
306 | pr_info("%s: Test OK\n", name); |
314 | dbgprintf("%s: Test OK\n", name); |
307 | return 0; |
315 | return 0; |
308 | bailout: |
316 | bailout: |
Line 370... | Line 378... | ||
370 | /* A slave NAKing the master means the slave didn't like |
378 | /* A slave NAKing the master means the slave didn't like |
371 | * something about the data it saw. For example, maybe |
379 | * something about the data it saw. For example, maybe |
372 | * the SMBus PEC was wrong. |
380 | * the SMBus PEC was wrong. |
373 | */ |
381 | */ |
374 | } else if (retval == 0) { |
382 | } else if (retval == 0) { |
375 | // dev_err(&i2c_adap->dev, "sendbytes: NAK bailout.\n"); |
383 | dev_err(&i2c_adap->dev, "sendbytes: NAK bailout.\n"); |
376 | return -EIO; |
384 | return -EIO; |
Line 377... | Line 385... | ||
377 | 385 | ||
378 | /* Timeout; or (someday) lost arbitration |
386 | /* Timeout; or (someday) lost arbitration |
379 | * |
387 | * |
380 | * FIXME Lost ARB implies retrying the transaction from |
388 | * FIXME Lost ARB implies retrying the transaction from |
381 | * the first message, after the "winning" master issues |
389 | * the first message, after the "winning" master issues |
382 | * its STOP. As a rule, upper layer code has no reason |
390 | * its STOP. As a rule, upper layer code has no reason |
383 | * to know or care about this ... it is *NOT* an error. |
391 | * to know or care about this ... it is *NOT* an error. |
384 | */ |
392 | */ |
385 | } else { |
393 | } else { |
386 | // dev_err(&i2c_adap->dev, "sendbytes: error %d\n", |
394 | dev_err(&i2c_adap->dev, "sendbytes: error %d\n", |
387 | // retval); |
395 | retval); |
388 | return retval; |
396 | return retval; |
389 | } |
397 | } |
390 | } |
398 | } |
391 | return wrcount; |
399 | return wrcount; |
Line 398... | Line 406... | ||
398 | /* assert: sda is high */ |
406 | /* assert: sda is high */ |
399 | if (is_ack) /* send ack */ |
407 | if (is_ack) /* send ack */ |
400 | setsda(adap, 0); |
408 | setsda(adap, 0); |
401 | udelay((adap->udelay + 1) / 2); |
409 | udelay((adap->udelay + 1) / 2); |
402 | if (sclhi(adap) < 0) { /* timeout */ |
410 | if (sclhi(adap) < 0) { /* timeout */ |
403 | // dev_err(&i2c_adap->dev, "readbytes: ack/nak timeout\n"); |
411 | dev_err(&i2c_adap->dev, "readbytes: ack/nak timeout\n"); |
404 | // return -ETIMEDOUT; |
412 | return -ETIMEDOUT; |
405 | } |
413 | } |
406 | scllo(adap); |
414 | scllo(adap); |
407 | return 0; |
415 | return 0; |
408 | } |
416 | } |
Line 431... | Line 439... | ||
431 | transaction length as the first read byte. */ |
439 | transaction length as the first read byte. */ |
432 | if (rdcount == 1 && (flags & I2C_M_RECV_LEN)) { |
440 | if (rdcount == 1 && (flags & I2C_M_RECV_LEN)) { |
433 | if (inval <= 0 || inval > I2C_SMBUS_BLOCK_MAX) { |
441 | if (inval <= 0 || inval > I2C_SMBUS_BLOCK_MAX) { |
434 | if (!(flags & I2C_M_NO_RD_ACK)) |
442 | if (!(flags & I2C_M_NO_RD_ACK)) |
435 | acknak(i2c_adap, 0); |
443 | acknak(i2c_adap, 0); |
436 | // dev_err(&i2c_adap->dev, "readbytes: invalid " |
444 | dev_err(&i2c_adap->dev, "readbytes: invalid " |
437 | // "block length (%d)\n", inval); |
445 | "block length (%d)\n", inval); |
438 | return -EREMOTEIO; |
446 | return -EPROTO; |
439 | } |
447 | } |
440 | /* The original count value accounts for the extra |
448 | /* The original count value accounts for the extra |
441 | bytes, that is, either 1 for a regular transaction, |
449 | bytes, that is, either 1 for a regular transaction, |
442 | or 2 for a PEC transaction. */ |
450 | or 2 for a PEC transaction. */ |
443 | count += inval; |
451 | count += inval; |
Line 462... | Line 470... | ||
462 | /* doAddress initiates the transfer by generating the start condition (in |
470 | /* doAddress initiates the transfer by generating the start condition (in |
463 | * try_address) and transmits the address in the necessary format to handle |
471 | * try_address) and transmits the address in the necessary format to handle |
464 | * reads, writes as well as 10bit-addresses. |
472 | * reads, writes as well as 10bit-addresses. |
465 | * returns: |
473 | * returns: |
466 | * 0 everything went okay, the chip ack'ed, or IGNORE_NAK flag was set |
474 | * 0 everything went okay, the chip ack'ed, or IGNORE_NAK flag was set |
467 | * -x an error occurred (like: -EREMOTEIO if the device did not answer, or |
475 | * -x an error occurred (like: -ENXIO if the device did not answer, or |
468 | * -ETIMEDOUT, for example if the lines are stuck...) |
476 | * -ETIMEDOUT, for example if the lines are stuck...) |
469 | */ |
477 | */ |
470 | static int bit_doAddress(struct i2c_adapter *i2c_adap, struct i2c_msg *msg) |
478 | static int bit_doAddress(struct i2c_adapter *i2c_adap, struct i2c_msg *msg) |
471 | { |
479 | { |
472 | unsigned short flags = msg->flags; |
480 | unsigned short flags = msg->flags; |
Line 478... | Line 486... | ||
478 | 486 | ||
Line 479... | Line 487... | ||
479 | retries = nak_ok ? 0 : i2c_adap->retries; |
487 | retries = nak_ok ? 0 : i2c_adap->retries; |
480 | 488 | ||
481 | if (flags & I2C_M_TEN) { |
489 | if (flags & I2C_M_TEN) { |
482 | /* a ten bit address */ |
490 | /* a ten bit address */ |
483 | addr = 0xf0 | ((msg->addr >> 7) & 0x03); |
491 | addr = 0xf0 | ((msg->addr >> 7) & 0x06); |
484 | bit_dbg(2, &i2c_adap->dev, "addr0: %d\n", addr); |
492 | bit_dbg(2, &i2c_adap->dev, "addr0: %d\n", addr); |
485 | /* try extended address code...*/ |
493 | /* try extended address code...*/ |
486 | ret = try_address(i2c_adap, addr, retries); |
494 | ret = try_address(i2c_adap, addr, retries); |
487 | if ((ret != 1) && !nak_ok) { |
495 | if ((ret != 1) && !nak_ok) { |
488 | // dev_err(&i2c_adap->dev, |
496 | dev_err(&i2c_adap->dev, |
489 | // "died at extended address code\n"); |
497 | "died at extended address code\n"); |
490 | return -EREMOTEIO; |
498 | return -ENXIO; |
491 | } |
499 | } |
492 | /* the remaining 8 bit address */ |
500 | /* the remaining 8 bit address */ |
493 | ret = i2c_outb(i2c_adap, msg->addr & 0x7f); |
501 | ret = i2c_outb(i2c_adap, msg->addr & 0xff); |
494 | if ((ret != 1) && !nak_ok) { |
502 | if ((ret != 1) && !nak_ok) { |
495 | /* the chip did not ack / xmission error occurred */ |
503 | /* the chip did not ack / xmission error occurred */ |
496 | // dev_err(&i2c_adap->dev, "died at 2nd address code\n"); |
504 | dev_err(&i2c_adap->dev, "died at 2nd address code\n"); |
497 | return -EREMOTEIO; |
505 | return -ENXIO; |
498 | } |
506 | } |
499 | if (flags & I2C_M_RD) { |
507 | if (flags & I2C_M_RD) { |
500 | bit_dbg(3, &i2c_adap->dev, "emitting repeated " |
508 | bit_dbg(3, &i2c_adap->dev, "emitting repeated " |
501 | "start condition\n"); |
509 | "start condition\n"); |
502 | i2c_repstart(adap); |
510 | i2c_repstart(adap); |
503 | /* okay, now switch into reading mode */ |
511 | /* okay, now switch into reading mode */ |
504 | addr |= 0x01; |
512 | addr |= 0x01; |
505 | ret = try_address(i2c_adap, addr, retries); |
513 | ret = try_address(i2c_adap, addr, retries); |
506 | if ((ret != 1) && !nak_ok) { |
514 | if ((ret != 1) && !nak_ok) { |
507 | // dev_err(&i2c_adap->dev, |
515 | dev_err(&i2c_adap->dev, |
508 | // "died at repeated address code\n"); |
516 | "died at repeated address code\n"); |
509 | return -EREMOTEIO; |
517 | return -EIO; |
510 | } |
518 | } |
511 | } |
519 | } |
512 | } else { /* normal 7bit address */ |
520 | } else { /* normal 7bit address */ |
Line 529... | Line 537... | ||
529 | struct i2c_msg *pmsg; |
537 | struct i2c_msg *pmsg; |
530 | struct i2c_algo_bit_data *adap = i2c_adap->algo_data; |
538 | struct i2c_algo_bit_data *adap = i2c_adap->algo_data; |
531 | int i, ret; |
539 | int i, ret; |
532 | unsigned short nak_ok; |
540 | unsigned short nak_ok; |
Line 533... | Line -... | ||
533 | - | ||
534 | //ENTER(); |
541 | |
535 | if (adap->pre_xfer) { |
542 | if (adap->pre_xfer) { |
536 | ret = adap->pre_xfer(i2c_adap); |
543 | ret = adap->pre_xfer(i2c_adap); |
537 | if (ret < 0) |
544 | if (ret < 0) |
538 | return ret; |
545 | return ret; |
Line 563... | Line 570... | ||
563 | if (ret >= 1) |
570 | if (ret >= 1) |
564 | bit_dbg(2, &i2c_adap->dev, "read %d byte%s\n", |
571 | bit_dbg(2, &i2c_adap->dev, "read %d byte%s\n", |
565 | ret, ret == 1 ? "" : "s"); |
572 | ret, ret == 1 ? "" : "s"); |
566 | if (ret < pmsg->len) { |
573 | if (ret < pmsg->len) { |
567 | if (ret >= 0) |
574 | if (ret >= 0) |
568 | ret = -EREMOTEIO; |
575 | ret = -EIO; |
569 | goto bailout; |
576 | goto bailout; |
570 | } |
577 | } |
571 | } else { |
578 | } else { |
572 | /* write bytes from buffer */ |
579 | /* write bytes from buffer */ |
573 | ret = sendbytes(i2c_adap, pmsg); |
580 | ret = sendbytes(i2c_adap, pmsg); |
574 | if (ret >= 1) |
581 | if (ret >= 1) |
575 | bit_dbg(2, &i2c_adap->dev, "wrote %d byte%s\n", |
582 | bit_dbg(2, &i2c_adap->dev, "wrote %d byte%s\n", |
576 | ret, ret == 1 ? "" : "s"); |
583 | ret, ret == 1 ? "" : "s"); |
577 | if (ret < pmsg->len) { |
584 | if (ret < pmsg->len) { |
578 | if (ret >= 0) |
585 | if (ret >= 0) |
579 | ret = -EREMOTEIO; |
586 | ret = -EIO; |
580 | goto bailout; |
587 | goto bailout; |
581 | } |
588 | } |
582 | } |
589 | } |
583 | } |
590 | } |
584 | ret = i; |
591 | ret = i; |
Line 585... | Line 592... | ||
585 | 592 | ||
586 | bailout: |
593 | bailout: |
587 | bit_dbg(3, &i2c_adap->dev, "emitting stop condition\n"); |
594 | bit_dbg(3, &i2c_adap->dev, "emitting stop condition\n"); |
588 | i2c_stop(adap); |
- | |
Line 589... | Line 595... | ||
589 | // LEAVE(); |
595 | i2c_stop(adap); |
590 | 596 | ||
591 | if (adap->post_xfer) |
597 | if (adap->post_xfer) |
592 | adap->post_xfer(i2c_adap); |
598 | adap->post_xfer(i2c_adap); |
Line 593... | Line 599... | ||
593 | return ret; |
599 | return ret; |
594 | } |
600 | } |
595 | 601 | ||
596 | static u32 bit_func(struct i2c_adapter *adap) |
602 | static u32 bit_func(struct i2c_adapter *adap) |
597 | { |
603 | { |
598 | return I2C_FUNC_I2C | I2C_FUNC_SMBUS_EMUL | |
604 | return I2C_FUNC_I2C | I2C_FUNC_NOSTART | I2C_FUNC_SMBUS_EMUL | |
599 | I2C_FUNC_SMBUS_READ_BLOCK_DATA | |
605 | I2C_FUNC_SMBUS_READ_BLOCK_DATA | |
Line 600... | Line 606... | ||
600 | I2C_FUNC_SMBUS_BLOCK_PROC_CALL | |
606 | I2C_FUNC_SMBUS_BLOCK_PROC_CALL | |
Line 601... | Line 607... | ||
601 | I2C_FUNC_10BIT_ADDR | I2C_FUNC_PROTOCOL_MANGLING; |
607 | I2C_FUNC_10BIT_ADDR | I2C_FUNC_PROTOCOL_MANGLING; |
602 | } |
608 | } |
603 | 609 | ||
604 | 610 | ||
- | 611 | /* -----exported algorithm data: ------------------------------------- */ |
|
Line 605... | Line 612... | ||
605 | /* -----exported algorithm data: ------------------------------------- */ |
612 | |
606 | 613 | const struct i2c_algorithm i2c_bit_algo = { |
|
607 | static const struct i2c_algorithm i2c_bit_algo = { |
614 | .master_xfer = bit_xfer, |
608 | .master_xfer = bit_xfer, |
615 | .functionality = bit_func, |
Line 618... | Line 625... | ||
618 | struct i2c_algo_bit_data *bit_adap = adap->algo_data; |
625 | struct i2c_algo_bit_data *bit_adap = adap->algo_data; |
619 | int ret; |
626 | int ret; |
Line 620... | Line 627... | ||
620 | 627 | ||
621 | if (bit_test) { |
628 | if (bit_test) { |
622 | ret = test_bus(adap); |
629 | ret = test_bus(adap); |
623 | if (ret < 0) |
630 | if (bit_test >= 2 && ret < 0) |
624 | return -ENODEV; |
631 | return -ENODEV; |
Line 625... | Line 632... | ||
625 | } |
632 | } |
626 | 633 | ||
627 | /* register new adapter to i2c module... */ |
634 | /* register new adapter to i2c module... */ |
Line -... | Line 635... | ||
- | 635 | adap->algo = &i2c_bit_algo; |
|
- | 636 | adap->retries = 3; |
|
- | 637 | ||
- | 638 | /* Complain if SCL can't be read */ |
|
- | 639 | if (bit_adap->getscl == NULL) { |
|
628 | adap->algo = &i2c_bit_algo; |
640 | dev_warn(&adap->dev, "Not I2C compliant: can't read SCL\n"); |
629 | adap->retries = 3; |
641 | dev_warn(&adap->dev, "Bus may be unreliable\n"); |
Line 630... | Line 642... | ||
630 | 642 | } |
|
631 | return 0; |
643 | return 0; |