Lines Matching +full:stop +full:- +full:ack
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()
149 * 0 if the device did not ack
150 * -ETIMEDOUT if an error occurred (while raising the scl line)
156 int ack; in i2c_outb() local
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()
174 * the whole (combined) message and *NOT* issue STOP. in i2c_outb()
180 bit_dbg(1, &i2c_adap->dev, in i2c_outb()
181 "i2c_outb: 0x%02x, timeout at ack\n", (int)c); in i2c_outb()
182 return -ETIMEDOUT; in i2c_outb()
185 /* read ack: SDA should be pulled down by slave, or it may in i2c_outb()
187 * Always report ACK if SDA is write-only. in i2c_outb()
189 ack = !adap->getsda || !getsda(adap); /* ack: sda is pulled low -> success */ in i2c_outb()
190 bit_dbg(2, &i2c_adap->dev, "i2c_outb: 0x%02x %s\n", (int)c, in i2c_outb()
191 ack ? "A" : "NA"); in i2c_outb()
194 return ack; in i2c_outb()
201 /* read byte via i2c port, without start/stop sequence */ in i2c_inb()
205 struct i2c_algo_bit_data *adap = i2c_adap->algo_data; in i2c_inb()
211 bit_dbg(1, &i2c_adap->dev, in i2c_inb()
213 7 - i); in i2c_inb()
214 return -ETIMEDOUT; in i2c_inb()
220 udelay(i == 7 ? adap->udelay / 2 : adap->udelay); in i2c_inb()
227 * Sanity check for the adapter hardware - check the reaction of
232 struct i2c_algo_bit_data *adap = i2c_adap->algo_data; in test_bus()
233 const char *name = i2c_adap->name; in test_bus()
236 if (adap->pre_xfer) { in test_bus()
237 ret = adap->pre_xfer(i2c_adap); in test_bus()
239 return -ENODEV; in test_bus()
242 if (adap->getsda == NULL) in test_bus()
243 pr_info("%s: SDA is write-only, testing not possible\n", name); in test_bus()
244 if (adap->getscl == NULL) in test_bus()
245 pr_info("%s: SCL is write-only, testing not possible\n", name); in test_bus()
247 sda = adap->getsda ? getsda(adap) : 1; in test_bus()
248 scl = adap->getscl ? getscl(adap) : 1; in test_bus()
255 if (adap->getsda && getsda(adap)) { in test_bus()
259 if (adap->getscl && !getscl(adap)) { in test_bus()
265 if (adap->getsda && !getsda(adap)) { in test_bus()
269 if (adap->getscl && !getscl(adap)) { in test_bus()
275 if (adap->getscl && getscl(adap)) { in test_bus()
279 if (adap->getsda && !getsda(adap)) { in test_bus()
285 if (adap->getscl && !getscl(adap)) { in test_bus()
289 if (adap->getsda && !getsda(adap)) { in test_bus()
294 if (adap->post_xfer) in test_bus()
295 adap->post_xfer(i2c_adap); in test_bus()
303 if (adap->post_xfer) in test_bus()
304 adap->post_xfer(i2c_adap); in test_bus()
306 return -ENODEV; in test_bus()
309 /* ----- Utility functions
317 * -x transmission error
322 struct i2c_algo_bit_data *adap = i2c_adap->algo_data; in try_address()
329 bit_dbg(3, &i2c_adap->dev, "emitting stop condition\n"); in try_address()
331 udelay(adap->udelay); in try_address()
333 bit_dbg(3, &i2c_adap->dev, "emitting start condition\n"); in try_address()
337 bit_dbg(1, &i2c_adap->dev, in try_address()
346 const unsigned char *temp = msg->buf; in sendbytes()
347 int count = msg->len; in sendbytes()
348 unsigned short nak_ok = msg->flags & I2C_M_IGNORE_NAK; in sendbytes()
355 /* OK/ACK; or ignored NAK */ in sendbytes()
357 count--; in sendbytes()
366 dev_err(&i2c_adap->dev, "sendbytes: NAK bailout.\n"); in sendbytes()
367 return -EIO; in sendbytes()
373 * its STOP. As a rule, upper layer code has no reason in sendbytes()
377 dev_err(&i2c_adap->dev, "sendbytes: error %d\n", in sendbytes()
387 struct i2c_algo_bit_data *adap = i2c_adap->algo_data; in acknak()
390 if (is_ack) /* send ack */ in acknak()
392 udelay((adap->udelay + 1) / 2); in acknak()
394 dev_err(&i2c_adap->dev, "readbytes: ack/nak timeout\n"); in acknak()
395 return -ETIMEDOUT; in acknak()
405 unsigned char *temp = msg->buf; in readbytes()
406 int count = msg->len; in readbytes()
407 const unsigned flags = msg->flags; in readbytes()
408 struct i2c_algo_bit_data *adap = i2c_adap->algo_data; in readbytes()
410 if (!adap->getsda) in readbytes()
411 return -EOPNOTSUPP; in readbytes()
423 count--; in readbytes()
431 dev_err(&i2c_adap->dev, in readbytes()
434 return -EPROTO; in readbytes()
440 msg->len += inval; in readbytes()
443 bit_dbg(2, &i2c_adap->dev, "readbytes: 0x%02x %s\n", in readbytes()
446 ? "(no ack/nak)" in readbytes()
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...)
468 unsigned short flags = msg->flags; in bit_doAddress()
469 unsigned short nak_ok = msg->flags & I2C_M_IGNORE_NAK; in bit_doAddress()
470 struct i2c_algo_bit_data *adap = i2c_adap->algo_data; in bit_doAddress()
475 retries = nak_ok ? 0 : i2c_adap->retries; in bit_doAddress()
479 addr = 0xf0 | ((msg->addr >> 7) & 0x06); in bit_doAddress()
480 bit_dbg(2, &i2c_adap->dev, "addr0: %d\n", addr); in bit_doAddress()
484 dev_err(&i2c_adap->dev, in bit_doAddress()
486 return -ENXIO; in bit_doAddress()
489 ret = i2c_outb(i2c_adap, msg->addr & 0xff); in bit_doAddress()
491 /* the chip did not ack / xmission error occurred */ in bit_doAddress()
492 dev_err(&i2c_adap->dev, "died at 2nd address code\n"); in bit_doAddress()
493 return -ENXIO; in bit_doAddress()
496 bit_dbg(3, &i2c_adap->dev, in bit_doAddress()
503 dev_err(&i2c_adap->dev, in bit_doAddress()
505 return -EIO; in bit_doAddress()
514 return -ENXIO; in bit_doAddress()
524 struct i2c_algo_bit_data *adap = i2c_adap->algo_data; in bit_xfer()
528 if (adap->pre_xfer) { in bit_xfer()
529 ret = adap->pre_xfer(i2c_adap); in bit_xfer()
534 bit_dbg(3, &i2c_adap->dev, "emitting start condition\n"); in bit_xfer()
538 nak_ok = pmsg->flags & I2C_M_IGNORE_NAK; in bit_xfer()
539 if (!(pmsg->flags & I2C_M_NOSTART)) { in bit_xfer()
541 if (msgs[i - 1].flags & I2C_M_STOP) { in bit_xfer()
542 bit_dbg(3, &i2c_adap->dev, in bit_xfer()
543 "emitting enforced stop/start condition\n"); in bit_xfer()
547 bit_dbg(3, &i2c_adap->dev, in bit_xfer()
554 bit_dbg(1, &i2c_adap->dev, in bit_xfer()
560 if (pmsg->flags & I2C_M_RD) { in bit_xfer()
564 bit_dbg(2, &i2c_adap->dev, "read %d byte%s\n", in bit_xfer()
566 if (ret < pmsg->len) { in bit_xfer()
568 ret = -EIO; in bit_xfer()
575 bit_dbg(2, &i2c_adap->dev, "wrote %d byte%s\n", in bit_xfer()
577 if (ret < pmsg->len) { in bit_xfer()
579 ret = -EIO; in bit_xfer()
587 bit_dbg(3, &i2c_adap->dev, "emitting stop condition\n"); in bit_xfer()
590 if (adap->post_xfer) in bit_xfer()
591 adap->post_xfer(i2c_adap); in bit_xfer()
604 struct i2c_algo_bit_data *adap = i2c_adap->algo_data; in bit_xfer_atomic()
606 if (!adap->can_do_atomic) in bit_xfer_atomic()
607 dev_warn(&i2c_adap->dev, "not flagged for atomic transfers\n"); in bit_xfer_atomic()
619 /* -----exported algorithm data: ------------------------------------- */
638 struct i2c_algo_bit_data *bit_adap = adap->algo_data; in __i2c_bit_add_bus()
644 return -ENODEV; in __i2c_bit_add_bus()
648 adap->algo = &i2c_bit_algo; in __i2c_bit_add_bus()
649 adap->retries = 3; in __i2c_bit_add_bus()
650 if (bit_adap->getscl == NULL) in __i2c_bit_add_bus()
651 adap->quirks = &i2c_bit_quirk_no_clk_stretch; in __i2c_bit_add_bus()
662 if (bit_adap->getsda == NULL) in __i2c_bit_add_bus()
663 dev_warn(&adap->dev, "Not I2C compliant: can't read SDA\n"); in __i2c_bit_add_bus()
665 if (bit_adap->getscl == NULL) in __i2c_bit_add_bus()
666 dev_warn(&adap->dev, "Not I2C compliant: can't read SCL\n"); in __i2c_bit_add_bus()
668 if (bit_adap->getsda == NULL || bit_adap->getscl == NULL) in __i2c_bit_add_bus()
669 dev_warn(&adap->dev, "Bus may be unreliable\n"); in __i2c_bit_add_bus()
686 MODULE_AUTHOR("Simon G. Vogl <simon@tk.uni-linz.ac.at>");
687 MODULE_DESCRIPTION("I2C-Bus bit-banging algorithm");