1 // SPDX-License-Identifier: GPL-2.0+
3 * i2c-algo-bit.c: i2c driver algorithms for bit-shift adapters
5 * Copyright (C) 1995-2000 Simon G. Vogl
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>
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/i2c.h>
17 #include <linux/i2c-algo-bit.h>
20 /* ----- global defines ----------------------------------------------- */
23 #define bit_dbg(level, dev, format, args...) \
25 if (i2c_debug >= level) \
26 dev_dbg(dev, format, ##args); \
29 #define bit_dbg(level, dev, format, args...) \
33 /* ----- global variables --------------------------------------------- */
35 static int bit_test
; /* see if the line-setting functions work */
36 module_param(bit_test
, int, S_IRUGO
);
37 MODULE_PARM_DESC(bit_test
, "lines testing - 0 off; 1 report; 2 fail if stuck");
40 static int i2c_debug
= 1;
41 module_param(i2c_debug
, int, S_IRUGO
| S_IWUSR
);
42 MODULE_PARM_DESC(i2c_debug
,
43 "debug level - 0 off; 1 normal; 2 verbose; 3 very verbose");
46 /* --- setting states on the bus with the right timing: --------------- */
48 #define setsda(adap, val) adap->setsda(adap->data, val)
49 #define setscl(adap, val) adap->setscl(adap->data, val)
50 #define getsda(adap) adap->getsda(adap->data)
51 #define getscl(adap) adap->getscl(adap->data)
53 static inline void sdalo(struct i2c_algo_bit_data
*adap
)
56 udelay((adap
->udelay
+ 1) / 2);
59 static inline void sdahi(struct i2c_algo_bit_data
*adap
)
62 udelay((adap
->udelay
+ 1) / 2);
65 static inline void scllo(struct i2c_algo_bit_data
*adap
)
68 udelay(adap
->udelay
/ 2);
72 * Raise scl line, and do checking for delays. This is necessary for slower
75 static int sclhi(struct i2c_algo_bit_data
*adap
)
81 /* Not all adapters have scl sense line... */
86 while (!getscl(adap
)) {
87 /* This hw knows how to read the clock line, so we wait
88 * until it actually gets high. This is safer as some
89 * chips may hold it low ("clock stretching") while they
90 * are processing data internally.
92 if (time_after(jiffies
, start
+ adap
->timeout
)) {
93 /* Test one last time, as we may have been preempted
94 * between last check and timeout test.
103 if (jiffies
!= start
&& i2c_debug
>= 3)
104 pr_debug("i2c-algo-bit: needed %ld jiffies for SCL to go high\n",
109 udelay(adap
->udelay
);
114 /* --- other auxiliary functions -------------------------------------- */
115 static void i2c_start(struct i2c_algo_bit_data
*adap
)
117 /* assert: scl, sda are high */
119 udelay(adap
->udelay
);
123 static void i2c_repstart(struct i2c_algo_bit_data
*adap
)
125 /* assert: scl is low */
129 udelay(adap
->udelay
);
134 static void i2c_stop(struct i2c_algo_bit_data
*adap
)
136 /* assert: scl is low */
140 udelay(adap
->udelay
);
145 /* send a byte without start cond., look for arbitration,
146 check ackn. from slave */
148 * 1 if the device acknowledged
149 * 0 if the device did not ack
150 * -ETIMEDOUT if an error occurred (while raising the scl line)
152 static int i2c_outb(struct i2c_adapter
*i2c_adap
, unsigned char c
)
157 struct i2c_algo_bit_data
*adap
= i2c_adap
->algo_data
;
159 /* assert: scl is low */
160 for (i
= 7; i
>= 0; i
--) {
163 udelay((adap
->udelay
+ 1) / 2);
164 if (sclhi(adap
) < 0) { /* timed out */
165 bit_dbg(1, &i2c_adap
->dev
,
166 "i2c_outb: 0x%02x, timeout at bit #%d\n",
170 /* FIXME do arbitration here:
171 * if (sb && !getsda(adap)) -> ouch! Get out of here.
173 * Report a unique code, so higher level code can retry
174 * the whole (combined) message and *NOT* issue STOP.
179 if (sclhi(adap
) < 0) { /* timeout */
180 bit_dbg(1, &i2c_adap
->dev
,
181 "i2c_outb: 0x%02x, timeout at ack\n", (int)c
);
185 /* read ack: SDA should be pulled down by slave, or it may
186 * NAK (usually to report problems with the data we wrote).
187 * Always report ACK if SDA is write-only.
189 ack
= !adap
->getsda
|| !getsda(adap
); /* ack: sda is pulled low -> success */
190 bit_dbg(2, &i2c_adap
->dev
, "i2c_outb: 0x%02x %s\n", (int)c
,
195 /* assert: scl is low (sda undef) */
199 static int i2c_inb(struct i2c_adapter
*i2c_adap
)
201 /* read byte via i2c port, without start/stop sequence */
202 /* acknowledge is sent in i2c_read. */
204 unsigned char indata
= 0;
205 struct i2c_algo_bit_data
*adap
= i2c_adap
->algo_data
;
207 /* assert: scl is low */
209 for (i
= 0; i
< 8; i
++) {
210 if (sclhi(adap
) < 0) { /* timeout */
211 bit_dbg(1, &i2c_adap
->dev
,
212 "i2c_inb: timeout at bit #%d\n",
220 udelay(i
== 7 ? adap
->udelay
/ 2 : adap
->udelay
);
222 /* assert: scl is low */
227 * Sanity check for the adapter hardware - check the reaction of
228 * the bus lines only if it seems to be idle.
230 static int test_bus(struct i2c_adapter
*i2c_adap
)
232 struct i2c_algo_bit_data
*adap
= i2c_adap
->algo_data
;
233 const char *name
= i2c_adap
->name
;
236 if (adap
->pre_xfer
) {
237 ret
= adap
->pre_xfer(i2c_adap
);
242 if (adap
->getsda
== NULL
)
243 pr_info("%s: SDA is write-only, testing not possible\n", name
);
244 if (adap
->getscl
== NULL
)
245 pr_info("%s: SCL is write-only, testing not possible\n", name
);
247 sda
= adap
->getsda
? getsda(adap
) : 1;
248 scl
= adap
->getscl
? getscl(adap
) : 1;
250 pr_warn("%s: bus seems to be busy (scl=%d, sda=%d)\n", name
, scl
, sda
);
255 if (adap
->getsda
&& getsda(adap
)) {
256 pr_warn("%s: SDA stuck high!\n", name
);
259 if (adap
->getscl
&& !getscl(adap
)) {
260 pr_warn("%s: SCL unexpected low while pulling SDA low!\n", name
);
265 if (adap
->getsda
&& !getsda(adap
)) {
266 pr_warn("%s: SDA stuck low!\n", name
);
269 if (adap
->getscl
&& !getscl(adap
)) {
270 pr_warn("%s: SCL unexpected low while pulling SDA high!\n", name
);
275 if (adap
->getscl
&& getscl(adap
)) {
276 pr_warn("%s: SCL stuck high!\n", name
);
279 if (adap
->getsda
&& !getsda(adap
)) {
280 pr_warn("%s: SDA unexpected low while pulling SCL low!\n", name
);
285 if (adap
->getscl
&& !getscl(adap
)) {
286 pr_warn("%s: SCL stuck low!\n", name
);
289 if (adap
->getsda
&& !getsda(adap
)) {
290 pr_warn("%s: SDA unexpected low while pulling SCL high!\n", name
);
295 adap
->post_xfer(i2c_adap
);
297 pr_info("%s: Test OK\n", name
);
304 adap
->post_xfer(i2c_adap
);
309 /* ----- Utility functions
312 /* try_address tries to contact a chip for a number of
313 * times before it gives up.
316 * 0 chip did not answer
317 * -x transmission error
319 static int try_address(struct i2c_adapter
*i2c_adap
,
320 unsigned char addr
, int retries
)
322 struct i2c_algo_bit_data
*adap
= i2c_adap
->algo_data
;
325 for (i
= 0; i
<= retries
; i
++) {
326 ret
= i2c_outb(i2c_adap
, addr
);
327 if (ret
== 1 || i
== retries
)
329 bit_dbg(3, &i2c_adap
->dev
, "emitting stop condition\n");
331 udelay(adap
->udelay
);
333 bit_dbg(3, &i2c_adap
->dev
, "emitting start condition\n");
337 bit_dbg(1, &i2c_adap
->dev
,
338 "Used %d tries to %s client at 0x%02x: %s\n", i
+ 1,
339 addr
& 1 ? "read from" : "write to", addr
>> 1,
340 ret
== 1 ? "success" : "failed, timeout?");
344 static int sendbytes(struct i2c_adapter
*i2c_adap
, struct i2c_msg
*msg
)
346 const unsigned char *temp
= msg
->buf
;
347 int count
= msg
->len
;
348 unsigned short nak_ok
= msg
->flags
& I2C_M_IGNORE_NAK
;
353 retval
= i2c_outb(i2c_adap
, *temp
);
355 /* OK/ACK; or ignored NAK */
356 if ((retval
> 0) || (nak_ok
&& (retval
== 0))) {
361 /* A slave NAKing the master means the slave didn't like
362 * something about the data it saw. For example, maybe
363 * the SMBus PEC was wrong.
365 } else if (retval
== 0) {
366 dev_err(&i2c_adap
->dev
, "sendbytes: NAK bailout.\n");
369 /* Timeout; or (someday) lost arbitration
371 * FIXME Lost ARB implies retrying the transaction from
372 * the first message, after the "winning" master issues
373 * its STOP. As a rule, upper layer code has no reason
374 * to know or care about this ... it is *NOT* an error.
377 dev_err(&i2c_adap
->dev
, "sendbytes: error %d\n",
385 static int acknak(struct i2c_adapter
*i2c_adap
, int is_ack
)
387 struct i2c_algo_bit_data
*adap
= i2c_adap
->algo_data
;
389 /* assert: sda is high */
390 if (is_ack
) /* send ack */
392 udelay((adap
->udelay
+ 1) / 2);
393 if (sclhi(adap
) < 0) { /* timeout */
394 dev_err(&i2c_adap
->dev
, "readbytes: ack/nak timeout\n");
401 static int readbytes(struct i2c_adapter
*i2c_adap
, struct i2c_msg
*msg
)
404 int rdcount
= 0; /* counts bytes read */
405 unsigned char *temp
= msg
->buf
;
406 int count
= msg
->len
;
407 const unsigned flags
= msg
->flags
;
408 struct i2c_algo_bit_data
*adap
= i2c_adap
->algo_data
;
414 inval
= i2c_inb(i2c_adap
);
418 } else { /* read timed out */
425 /* Some SMBus transactions require that we receive the
426 transaction length as the first read byte. */
427 if (rdcount
== 1 && (flags
& I2C_M_RECV_LEN
)) {
428 if (inval
<= 0 || inval
> I2C_SMBUS_BLOCK_MAX
) {
429 if (!(flags
& I2C_M_NO_RD_ACK
))
431 dev_err(&i2c_adap
->dev
,
432 "readbytes: invalid block length (%d)\n",
436 /* The original count value accounts for the extra
437 bytes, that is, either 1 for a regular transaction,
438 or 2 for a PEC transaction. */
443 bit_dbg(2, &i2c_adap
->dev
, "readbytes: 0x%02x %s\n",
445 (flags
& I2C_M_NO_RD_ACK
)
447 : (count
? "A" : "NA"));
449 if (!(flags
& I2C_M_NO_RD_ACK
)) {
450 inval
= acknak(i2c_adap
, count
);
458 /* doAddress initiates the transfer by generating the start condition (in
459 * try_address) and transmits the address in the necessary format to handle
460 * reads, writes as well as 10bit-addresses.
462 * 0 everything went okay, the chip ack'ed, or IGNORE_NAK flag was set
463 * -x an error occurred (like: -ENXIO if the device did not answer, or
464 * -ETIMEDOUT, for example if the lines are stuck...)
466 static int bit_doAddress(struct i2c_adapter
*i2c_adap
, struct i2c_msg
*msg
)
468 unsigned short flags
= msg
->flags
;
469 unsigned short nak_ok
= msg
->flags
& I2C_M_IGNORE_NAK
;
470 struct i2c_algo_bit_data
*adap
= i2c_adap
->algo_data
;
475 retries
= nak_ok
? 0 : i2c_adap
->retries
;
477 if (flags
& I2C_M_TEN
) {
478 /* a ten bit address */
479 addr
= 0xf0 | ((msg
->addr
>> 7) & 0x06);
480 bit_dbg(2, &i2c_adap
->dev
, "addr0: %d\n", addr
);
481 /* try extended address code...*/
482 ret
= try_address(i2c_adap
, addr
, retries
);
483 if ((ret
!= 1) && !nak_ok
) {
484 dev_err(&i2c_adap
->dev
,
485 "died at extended address code\n");
488 /* the remaining 8 bit address */
489 ret
= i2c_outb(i2c_adap
, msg
->addr
& 0xff);
490 if ((ret
!= 1) && !nak_ok
) {
491 /* the chip did not ack / xmission error occurred */
492 dev_err(&i2c_adap
->dev
, "died at 2nd address code\n");
495 if (flags
& I2C_M_RD
) {
496 bit_dbg(3, &i2c_adap
->dev
,
497 "emitting repeated start condition\n");
499 /* okay, now switch into reading mode */
501 ret
= try_address(i2c_adap
, addr
, retries
);
502 if ((ret
!= 1) && !nak_ok
) {
503 dev_err(&i2c_adap
->dev
,
504 "died at repeated address code\n");
508 } else { /* normal 7bit address */
509 addr
= i2c_8bit_addr_from_msg(msg
);
510 if (flags
& I2C_M_REV_DIR_ADDR
)
512 ret
= try_address(i2c_adap
, addr
, retries
);
513 if ((ret
!= 1) && !nak_ok
)
520 static int bit_xfer(struct i2c_adapter
*i2c_adap
,
521 struct i2c_msg msgs
[], int num
)
523 struct i2c_msg
*pmsg
;
524 struct i2c_algo_bit_data
*adap
= i2c_adap
->algo_data
;
526 unsigned short nak_ok
;
528 if (adap
->pre_xfer
) {
529 ret
= adap
->pre_xfer(i2c_adap
);
534 bit_dbg(3, &i2c_adap
->dev
, "emitting start condition\n");
536 for (i
= 0; i
< num
; i
++) {
538 nak_ok
= pmsg
->flags
& I2C_M_IGNORE_NAK
;
539 if (!(pmsg
->flags
& I2C_M_NOSTART
)) {
541 if (msgs
[i
- 1].flags
& I2C_M_STOP
) {
542 bit_dbg(3, &i2c_adap
->dev
,
543 "emitting enforced stop/start condition\n");
547 bit_dbg(3, &i2c_adap
->dev
,
548 "emitting repeated start condition\n");
552 ret
= bit_doAddress(i2c_adap
, pmsg
);
553 if ((ret
!= 0) && !nak_ok
) {
554 bit_dbg(1, &i2c_adap
->dev
,
555 "NAK from device addr 0x%02x msg #%d\n",
560 if (pmsg
->flags
& I2C_M_RD
) {
561 /* read bytes into buffer*/
562 ret
= readbytes(i2c_adap
, pmsg
);
564 bit_dbg(2, &i2c_adap
->dev
, "read %d byte%s\n",
565 ret
, ret
== 1 ? "" : "s");
566 if (ret
< pmsg
->len
) {
572 /* write bytes from buffer */
573 ret
= sendbytes(i2c_adap
, pmsg
);
575 bit_dbg(2, &i2c_adap
->dev
, "wrote %d byte%s\n",
576 ret
, ret
== 1 ? "" : "s");
577 if (ret
< pmsg
->len
) {
587 bit_dbg(3, &i2c_adap
->dev
, "emitting stop condition\n");
591 adap
->post_xfer(i2c_adap
);
596 * We print a warning when we are not flagged to support atomic transfers but
597 * will try anyhow. That's what the I2C core would do as well. Sadly, we can't
598 * modify the algorithm struct at probe time because this struct is exported
601 static int bit_xfer_atomic(struct i2c_adapter
*i2c_adap
, struct i2c_msg msgs
[],
604 struct i2c_algo_bit_data
*adap
= i2c_adap
->algo_data
;
606 if (!adap
->can_do_atomic
)
607 dev_warn(&i2c_adap
->dev
, "not flagged for atomic transfers\n");
609 return bit_xfer(i2c_adap
, msgs
, num
);
612 static u32
bit_func(struct i2c_adapter
*adap
)
614 return I2C_FUNC_I2C
| I2C_FUNC_NOSTART
| I2C_FUNC_SMBUS_EMUL_ALL
|
615 I2C_FUNC_10BIT_ADDR
| I2C_FUNC_PROTOCOL_MANGLING
;
619 /* -----exported algorithm data: ------------------------------------- */
621 const struct i2c_algorithm i2c_bit_algo
= {
622 .master_xfer
= bit_xfer
,
623 .master_xfer_atomic
= bit_xfer_atomic
,
624 .functionality
= bit_func
,
626 EXPORT_SYMBOL(i2c_bit_algo
);
628 static const struct i2c_adapter_quirks i2c_bit_quirk_no_clk_stretch
= {
629 .flags
= I2C_AQ_NO_CLK_STRETCH
,
633 * registering functions to load algorithms at runtime
635 static int __i2c_bit_add_bus(struct i2c_adapter
*adap
,
636 int (*add_adapter
)(struct i2c_adapter
*))
638 struct i2c_algo_bit_data
*bit_adap
= adap
->algo_data
;
642 ret
= test_bus(adap
);
643 if (bit_test
>= 2 && ret
< 0)
647 /* register new adapter to i2c module... */
648 adap
->algo
= &i2c_bit_algo
;
650 if (bit_adap
->getscl
== NULL
)
651 adap
->quirks
= &i2c_bit_quirk_no_clk_stretch
;
654 * We tried forcing SCL/SDA to an initial state here. But that caused a
655 * regression, sadly. Check Bugzilla #200045 for details.
658 ret
= add_adapter(adap
);
662 if (bit_adap
->getsda
== NULL
)
663 dev_warn(&adap
->dev
, "Not I2C compliant: can't read SDA\n");
665 if (bit_adap
->getscl
== NULL
)
666 dev_warn(&adap
->dev
, "Not I2C compliant: can't read SCL\n");
668 if (bit_adap
->getsda
== NULL
|| bit_adap
->getscl
== NULL
)
669 dev_warn(&adap
->dev
, "Bus may be unreliable\n");
674 int i2c_bit_add_bus(struct i2c_adapter
*adap
)
676 return __i2c_bit_add_bus(adap
, i2c_add_adapter
);
678 EXPORT_SYMBOL(i2c_bit_add_bus
);
680 int i2c_bit_add_numbered_bus(struct i2c_adapter
*adap
)
682 return __i2c_bit_add_bus(adap
, i2c_add_numbered_adapter
);
684 EXPORT_SYMBOL(i2c_bit_add_numbered_bus
);
686 MODULE_AUTHOR("Simon G. Vogl <simon@tk.uni-linz.ac.at>");
687 MODULE_DESCRIPTION("I2C-Bus bit-banging algorithm");
688 MODULE_LICENSE("GPL");