Lines Matching +full:stop +full:- +full:ack
1 // SPDX-License-Identifier: GPL-2.0-or-later
3 * i2c-algo-pca.c i2c driver algorithms for PCA9564 adapters
15 #include <linux/i2c-algo-pca.h>
26 #define pca_outw(adap, reg, val) adap->write_byte(adap->data, reg, val)
27 #define pca_inw(adap, reg) adap->read_byte(adap->data, reg)
30 #define pca_clock(adap) adap->i2c_clock
33 #define pca_wait(adap) adap->wait_for_completion(adap->data)
37 if (adap->chip == I2C_PCA_CHIP_9665) { in pca_reset()
46 * After a reset we need to re-apply any configuration in pca_reset()
50 pca_outw(adap, I2C_PCA_IND, adap->bus_settings.mode); in pca_reset()
52 pca_outw(adap, I2C_PCA_IND, adap->bus_settings.tlow); in pca_reset()
54 pca_outw(adap, I2C_PCA_IND, adap->bus_settings.thi); in pca_reset()
58 adap->reset_chip(adap->data); in pca_reset()
59 pca_set_con(adap, I2C_PCA_CON_ENSIO | adap->bus_settings.clock_freq); in pca_reset()
94 * Generate a stop condition on the i2c bus
96 * returns after the stop condition has been generated
105 DEB2("=== STOP\n"); in pca_stop()
123 msg->addr, msg->flags & I2C_M_RD ? 'R' : 'W', addr); in pca_address()
157 __u8 *b, int ack) in pca_rx_byte() argument
160 DEB2("=== READ %#04x %s\n", *b, ack ? "ACK" : "NACK"); in pca_rx_byte()
164 * Setup ACK or NACK for next received byte and wait for it to arrive.
169 int ack) in pca_rx_ack() argument
175 if (ack) in pca_rx_ack()
186 struct i2c_algo_pca_data *adap = i2c_adap->algo_data; in pca_xfer()
193 unsigned long timeout = jiffies + i2c_adap->timeout; in pca_xfer()
199 dev_dbg(&i2c_adap->dev, "bus is not idle. status is " in pca_xfer()
201 return -EBUSY; in pca_xfer()
212 addr = (0x7f & msg->addr) ; in pca_xfer()
214 if (msg->flags & I2C_M_RD) in pca_xfer()
216 curmsg, msg->len, addr, (addr << 1) | 1); in pca_xfer()
219 curmsg, msg->len, addr, addr << 1, in pca_xfer()
220 msg->len == 0 ? "" : ", "); in pca_xfer()
221 for (i = 0; i < msg->len; i++) in pca_xfer()
222 printk("%#04x%s", msg->buf[i], i == msg->len - 1 ? "" : ", "); in pca_xfer()
229 ret = -EIO; in pca_xfer()
237 case 0xf8: /* On reset or stop the bus is idle */ in pca_xfer()
246 case 0x18: /* SLA+W has been transmitted; ACK has been received */ in pca_xfer()
247 case 0x28: /* Data byte in I2CDAT has been transmitted; ACK has been received */ in pca_xfer()
248 if (numbytes < msg->len) { in pca_xfer()
250 msg->buf[numbytes]); in pca_xfer()
261 case 0x20: /* SLA+W has been transmitted; NOT ACK has been received */ in pca_xfer()
262 DEB2("NOT ACK received after SLA+W\n"); in pca_xfer()
264 ret = -ENXIO; in pca_xfer()
267 case 0x40: /* SLA+R has been transmitted; ACK has been received */ in pca_xfer()
268 completed = pca_rx_ack(adap, msg->len > 1); in pca_xfer()
271 case 0x50: /* Data bytes has been received; ACK has been returned */ in pca_xfer()
272 if (numbytes < msg->len) { in pca_xfer()
273 pca_rx_byte(adap, &msg->buf[numbytes], 1); in pca_xfer()
276 numbytes < msg->len - 1); in pca_xfer()
286 case 0x48: /* SLA+R has been transmitted; NOT ACK has been received */ in pca_xfer()
287 DEB2("NOT ACK received after SLA+R\n"); in pca_xfer()
289 ret = -ENXIO; in pca_xfer()
292 case 0x30: /* Data byte in I2CDAT has been transmitted; NOT ACK has been received */ in pca_xfer()
293 DEB2("NOT ACK received after data byte\n"); in pca_xfer()
300 * The PCA9564 data sheet (2006-09-01) says "A in pca_xfer()
302 * bus becomes free (STOP or SCL and SDA high)" in pca_xfer()
311 case 0x58: /* Data byte has been received; NOT ACK has been returned */ in pca_xfer()
312 if (numbytes == msg->len - 1) { in pca_xfer()
313 pca_rx_byte(adap, &msg->buf[numbytes], 0); in pca_xfer()
320 DEB2("NOT ACK sent after data byte received. " in pca_xfer()
322 numbytes, msg->len); in pca_xfer()
327 case 0x70: /* Bus error - SDA stuck low */ in pca_xfer()
328 DEB2("BUS ERROR - SDA Stuck low\n"); in pca_xfer()
331 case 0x78: /* Bus error - SCL stuck low (PCA9665) */ in pca_xfer()
332 case 0x90: /* Bus error - SCL stuck low (PCA9564) */ in pca_xfer()
333 DEB2("BUS ERROR - SCL Stuck low\n"); in pca_xfer()
336 case 0x00: /* Bus error during master or slave mode due to illegal START or STOP condition */ in pca_xfer()
337 DEB2("BUS ERROR - Illegal START or STOP\n"); in pca_xfer()
341 dev_err(&i2c_adap->dev, "unhandled SIO state 0x%02x\n", state); in pca_xfer()
370 struct i2c_algo_pca_data *pca_data = adap->algo_data; in pca_probe_chip()
382 printk(KERN_INFO "%s: PCA9665 detected.\n", adap->name); in pca_probe_chip()
383 pca_data->chip = I2C_PCA_CHIP_9665; in pca_probe_chip()
385 printk(KERN_INFO "%s: PCA9564 detected.\n", adap->name); in pca_probe_chip()
386 pca_data->chip = I2C_PCA_CHIP_9564; in pca_probe_chip()
388 return pca_data->chip; in pca_probe_chip()
393 struct i2c_algo_pca_data *pca_data = adap->algo_data; in pca_init()
395 adap->algo = &pca_algo; in pca_init()
401 if (pca_data->i2c_clock > 7) { in pca_init()
402 switch (pca_data->i2c_clock) { in pca_init()
404 pca_data->i2c_clock = I2C_PCA_CON_330kHz; in pca_init()
407 pca_data->i2c_clock = I2C_PCA_CON_288kHz; in pca_init()
410 pca_data->i2c_clock = I2C_PCA_CON_217kHz; in pca_init()
413 pca_data->i2c_clock = I2C_PCA_CON_146kHz; in pca_init()
416 pca_data->i2c_clock = I2C_PCA_CON_88kHz; in pca_init()
419 pca_data->i2c_clock = I2C_PCA_CON_59kHz; in pca_init()
422 pca_data->i2c_clock = I2C_PCA_CON_44kHz; in pca_init()
425 pca_data->i2c_clock = I2C_PCA_CON_36kHz; in pca_init()
430 " Using default 59kHz.\n", adap->name); in pca_init()
431 pca_data->i2c_clock = I2C_PCA_CON_59kHz; in pca_init()
437 " Use the nominal frequency.\n", adap->name); in pca_init()
442 adap->name, freqs[clock]); in pca_init()
445 pca_data->bus_settings.clock_freq = clock; in pca_init()
463 if (pca_data->i2c_clock > 1265800) { in pca_init()
465 " Using 1265.8kHz.\n", adap->name); in pca_init()
466 pca_data->i2c_clock = 1265800; in pca_init()
469 if (pca_data->i2c_clock < 60300) { in pca_init()
471 " Using 60.3kHz.\n", adap->name); in pca_init()
472 pca_data->i2c_clock = 60300; in pca_init()
478 if (pca_data->i2c_clock > I2C_MAX_FAST_MODE_PLUS_FREQ) { in pca_init()
482 raise_fall_time = 22; /* Raise 11e-8s, Fall 11e-8s */ in pca_init()
483 } else if (pca_data->i2c_clock > I2C_MAX_FAST_MODE_FREQ) { in pca_init()
487 raise_fall_time = 22; /* Raise 11e-8s, Fall 11e-8s */ in pca_init()
488 } else if (pca_data->i2c_clock > I2C_MAX_STANDARD_MODE_FREQ) { in pca_init()
492 raise_fall_time = 58; /* Raise 29e-8s, Fall 29e-8s */ in pca_init()
497 raise_fall_time = 127; /* Raise 29e-8s, Fall 98e-8s */ in pca_init()
506 thi = 1000000 - clock * raise_fall_time; in pca_init()
507 thi /= (I2C_PCA_OSC_PER * clock) - tlow; in pca_init()
509 tlow = (1000000 - clock * raise_fall_time) * min_tlow; in pca_init()
515 pca_data->bus_settings.mode = mode; in pca_init()
516 pca_data->bus_settings.tlow = tlow; in pca_init()
517 pca_data->bus_settings.thi = thi; in pca_init()
522 "%s: Clock frequency is %dHz\n", adap->name, clock * 100); in pca_init()
558 MODULE_DESCRIPTION("I2C-Bus PCA9564/PCA9665 algorithm");