Lines Matching +full:lines +full:- +full:initial +full:- +full:states
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
17 #include <linux/i2c-algo-bit.h>
20 /* ----- global defines ----------------------------------------------- */
33 /* ----- global variables --------------------------------------------- */
35 static int bit_test; /* see if the line-setting functions work */
37 MODULE_PARM_DESC(bit_test, "lines testing - 0 off; 1 report; 2 fail if stuck");
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)
56 udelay((adap->udelay + 1) / 2); in sdalo()
62 udelay((adap->udelay + 1) / 2); in sdahi()
68 udelay(adap->udelay / 2); in scllo()
82 if (!adap->getscl) in sclhi()
92 if (time_after(jiffies, start + adap->timeout)) { in sclhi()
98 return -ETIMEDOUT; in sclhi()
104 pr_debug("i2c-algo-bit: needed %ld jiffies for SCL to go high\n", in sclhi()
105 jiffies - start); in sclhi()
109 udelay(adap->udelay); in sclhi()
114 /* --- other auxiliary functions -------------------------------------- */
119 udelay(adap->udelay); in i2c_start()
129 udelay(adap->udelay); in i2c_repstart()
140 udelay(adap->udelay); in i2c_stop()
150 * -ETIMEDOUT if an error occurred (while raising the scl line)
157 struct i2c_algo_bit_data *adap = i2c_adap->algo_data; in i2c_outb()
160 for (i = 7; i >= 0; i--) { in i2c_outb()
163 udelay((adap->udelay + 1) / 2); in i2c_outb()
165 bit_dbg(1, &i2c_adap->dev, in i2c_outb()
168 return -ETIMEDOUT; in i2c_outb()
171 * if (sb && !getsda(adap)) -> ouch! Get out of here. in i2c_outb()
180 bit_dbg(1, &i2c_adap->dev, in i2c_outb()
182 return -ETIMEDOUT; in i2c_outb()
188 ack = !getsda(adap); /* ack: sda is pulled low -> success */ in i2c_outb()
189 bit_dbg(2, &i2c_adap->dev, "i2c_outb: 0x%02x %s\n", (int)c, in i2c_outb()
204 struct i2c_algo_bit_data *adap = i2c_adap->algo_data; in i2c_inb()
210 bit_dbg(1, &i2c_adap->dev, in i2c_inb()
212 7 - i); in i2c_inb()
213 return -ETIMEDOUT; in i2c_inb()
219 udelay(i == 7 ? adap->udelay / 2 : adap->udelay); in i2c_inb()
226 * Sanity check for the adapter hardware - check the reaction of
227 * the bus lines only if it seems to be idle.
231 struct i2c_algo_bit_data *adap = i2c_adap->algo_data; in test_bus()
232 const char *name = i2c_adap->name; in test_bus()
235 if (adap->pre_xfer) { in test_bus()
236 ret = adap->pre_xfer(i2c_adap); in test_bus()
238 return -ENODEV; in test_bus()
241 if (adap->getscl == NULL) in test_bus()
245 scl = (adap->getscl == NULL) ? 1 : getscl(adap); in test_bus()
255 scl = (adap->getscl == NULL) ? 1 : getscl(adap); in test_bus()
269 scl = (adap->getscl == NULL) ? 1 : getscl(adap); in test_bus()
283 scl = (adap->getscl == NULL) ? 0 : getscl(adap); in test_bus()
297 scl = (adap->getscl == NULL) ? 1 : getscl(adap); in test_bus()
309 if (adap->post_xfer) in test_bus()
310 adap->post_xfer(i2c_adap); in test_bus()
318 if (adap->post_xfer) in test_bus()
319 adap->post_xfer(i2c_adap); in test_bus()
321 return -ENODEV; in test_bus()
324 /* ----- Utility functions
332 * -x transmission error
337 struct i2c_algo_bit_data *adap = i2c_adap->algo_data; in try_address()
344 bit_dbg(3, &i2c_adap->dev, "emitting stop condition\n"); in try_address()
346 udelay(adap->udelay); in try_address()
348 bit_dbg(3, &i2c_adap->dev, "emitting start condition\n"); in try_address()
352 bit_dbg(1, &i2c_adap->dev, in try_address()
361 const unsigned char *temp = msg->buf; in sendbytes()
362 int count = msg->len; in sendbytes()
363 unsigned short nak_ok = msg->flags & I2C_M_IGNORE_NAK; in sendbytes()
372 count--; in sendbytes()
381 dev_err(&i2c_adap->dev, "sendbytes: NAK bailout.\n"); in sendbytes()
382 return -EIO; in sendbytes()
392 dev_err(&i2c_adap->dev, "sendbytes: error %d\n", in sendbytes()
402 struct i2c_algo_bit_data *adap = i2c_adap->algo_data; in acknak()
407 udelay((adap->udelay + 1) / 2); in acknak()
409 dev_err(&i2c_adap->dev, "readbytes: ack/nak timeout\n"); in acknak()
410 return -ETIMEDOUT; in acknak()
420 unsigned char *temp = msg->buf; in readbytes()
421 int count = msg->len; in readbytes()
422 const unsigned flags = msg->flags; in readbytes()
434 count--; in readbytes()
442 dev_err(&i2c_adap->dev, in readbytes()
445 return -EPROTO; in readbytes()
451 msg->len += inval; in readbytes()
454 bit_dbg(2, &i2c_adap->dev, "readbytes: 0x%02x %s\n", in readbytes()
471 * reads, writes as well as 10bit-addresses.
474 * -x an error occurred (like: -ENXIO if the device did not answer, or
475 * -ETIMEDOUT, for example if the lines are stuck...)
479 unsigned short flags = msg->flags; in bit_doAddress()
480 unsigned short nak_ok = msg->flags & I2C_M_IGNORE_NAK; in bit_doAddress()
481 struct i2c_algo_bit_data *adap = i2c_adap->algo_data; in bit_doAddress()
486 retries = nak_ok ? 0 : i2c_adap->retries; in bit_doAddress()
490 addr = 0xf0 | ((msg->addr >> 7) & 0x06); in bit_doAddress()
491 bit_dbg(2, &i2c_adap->dev, "addr0: %d\n", addr); in bit_doAddress()
495 dev_err(&i2c_adap->dev, in bit_doAddress()
497 return -ENXIO; in bit_doAddress()
500 ret = i2c_outb(i2c_adap, msg->addr & 0xff); in bit_doAddress()
503 dev_err(&i2c_adap->dev, "died at 2nd address code\n"); in bit_doAddress()
504 return -ENXIO; in bit_doAddress()
507 bit_dbg(3, &i2c_adap->dev, in bit_doAddress()
514 dev_err(&i2c_adap->dev, in bit_doAddress()
516 return -EIO; in bit_doAddress()
525 return -ENXIO; in bit_doAddress()
535 struct i2c_algo_bit_data *adap = i2c_adap->algo_data; in bit_xfer()
539 if (adap->pre_xfer) { in bit_xfer()
540 ret = adap->pre_xfer(i2c_adap); in bit_xfer()
545 bit_dbg(3, &i2c_adap->dev, "emitting start condition\n"); in bit_xfer()
549 nak_ok = pmsg->flags & I2C_M_IGNORE_NAK; in bit_xfer()
550 if (!(pmsg->flags & I2C_M_NOSTART)) { in bit_xfer()
552 if (msgs[i - 1].flags & I2C_M_STOP) { in bit_xfer()
553 bit_dbg(3, &i2c_adap->dev, in bit_xfer()
558 bit_dbg(3, &i2c_adap->dev, in bit_xfer()
565 bit_dbg(1, &i2c_adap->dev, in bit_xfer()
571 if (pmsg->flags & I2C_M_RD) { in bit_xfer()
575 bit_dbg(2, &i2c_adap->dev, "read %d byte%s\n", in bit_xfer()
577 if (ret < pmsg->len) { in bit_xfer()
579 ret = -EIO; in bit_xfer()
586 bit_dbg(2, &i2c_adap->dev, "wrote %d byte%s\n", in bit_xfer()
588 if (ret < pmsg->len) { in bit_xfer()
590 ret = -EIO; in bit_xfer()
598 bit_dbg(3, &i2c_adap->dev, "emitting stop condition\n"); in bit_xfer()
601 if (adap->post_xfer) in bit_xfer()
602 adap->post_xfer(i2c_adap); in bit_xfer()
615 struct i2c_algo_bit_data *adap = i2c_adap->algo_data; in bit_xfer_atomic()
617 if (!adap->can_do_atomic) in bit_xfer_atomic()
618 dev_warn(&i2c_adap->dev, "not flagged for atomic transfers\n"); in bit_xfer_atomic()
630 /* -----exported algorithm data: ------------------------------------- */
649 struct i2c_algo_bit_data *bit_adap = adap->algo_data; in __i2c_bit_add_bus()
655 return -ENODEV; in __i2c_bit_add_bus()
659 adap->algo = &i2c_bit_algo; in __i2c_bit_add_bus()
660 adap->retries = 3; in __i2c_bit_add_bus()
661 if (bit_adap->getscl == NULL) in __i2c_bit_add_bus()
662 adap->quirks = &i2c_bit_quirk_no_clk_stretch; in __i2c_bit_add_bus()
665 * We tried forcing SCL/SDA to an initial state here. But that caused a in __i2c_bit_add_bus()
674 if (bit_adap->getscl == NULL) { in __i2c_bit_add_bus()
675 dev_warn(&adap->dev, "Not I2C compliant: can't read SCL\n"); in __i2c_bit_add_bus()
676 dev_warn(&adap->dev, "Bus may be unreliable\n"); in __i2c_bit_add_bus()
693 MODULE_AUTHOR("Simon G. Vogl <simon@tk.uni-linz.ac.at>");
694 MODULE_DESCRIPTION("I2C-Bus bit-banging algorithm");