Wolfram Sang | 738ac06 | 2019-01-19 13:16:54 +0100 | [diff] [blame] | 1 | // SPDX-License-Identifier: GPL-2.0+ |
| 2 | /* |
| 3 | * i2c-algo-bit.c: i2c driver algorithms for bit-shift adapters |
| 4 | * |
David Brownell | cf978ab | 2008-01-27 18:14:46 +0100 | [diff] [blame] | 5 | * Copyright (C) 1995-2000 Simon G. Vogl |
Wolfram Sang | 738ac06 | 2019-01-19 13:16:54 +0100 | [diff] [blame] | 6 | * |
| 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> |
| 9 | */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 10 | |
| 11 | #include <linux/kernel.h> |
| 12 | #include <linux/module.h> |
| 13 | #include <linux/delay.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 14 | #include <linux/errno.h> |
| 15 | #include <linux/sched.h> |
| 16 | #include <linux/i2c.h> |
| 17 | #include <linux/i2c-algo-bit.h> |
| 18 | |
| 19 | |
| 20 | /* ----- global defines ----------------------------------------------- */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 21 | |
Jean Delvare | 494dbb6 | 2007-05-01 23:26:33 +0200 | [diff] [blame] | 22 | #ifdef DEBUG |
| 23 | #define bit_dbg(level, dev, format, args...) \ |
| 24 | do { \ |
| 25 | if (i2c_debug >= level) \ |
| 26 | dev_dbg(dev, format, ##args); \ |
| 27 | } while (0) |
| 28 | #else |
| 29 | #define bit_dbg(level, dev, format, args...) \ |
| 30 | do {} while (0) |
| 31 | #endif /* DEBUG */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 32 | |
| 33 | /* ----- global variables --------------------------------------------- */ |
| 34 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 35 | static int bit_test; /* see if the line-setting functions work */ |
Jean Delvare | 1bddab7 | 2011-10-30 13:47:25 +0100 | [diff] [blame] | 36 | module_param(bit_test, int, S_IRUGO); |
| 37 | MODULE_PARM_DESC(bit_test, "lines testing - 0 off; 1 report; 2 fail if stuck"); |
Jean Delvare | 494dbb6 | 2007-05-01 23:26:33 +0200 | [diff] [blame] | 38 | |
| 39 | #ifdef DEBUG |
| 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"); |
| 44 | #endif |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 45 | |
| 46 | /* --- setting states on the bus with the right timing: --------------- */ |
| 47 | |
David Brownell | cf978ab | 2008-01-27 18:14:46 +0100 | [diff] [blame] | 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) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 52 | |
| 53 | static inline void sdalo(struct i2c_algo_bit_data *adap) |
| 54 | { |
David Brownell | cf978ab | 2008-01-27 18:14:46 +0100 | [diff] [blame] | 55 | setsda(adap, 0); |
Jean Delvare | 424ed67 | 2007-05-01 23:26:33 +0200 | [diff] [blame] | 56 | udelay((adap->udelay + 1) / 2); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 57 | } |
| 58 | |
| 59 | static inline void sdahi(struct i2c_algo_bit_data *adap) |
| 60 | { |
David Brownell | cf978ab | 2008-01-27 18:14:46 +0100 | [diff] [blame] | 61 | setsda(adap, 1); |
Jean Delvare | 424ed67 | 2007-05-01 23:26:33 +0200 | [diff] [blame] | 62 | udelay((adap->udelay + 1) / 2); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 63 | } |
| 64 | |
| 65 | static inline void scllo(struct i2c_algo_bit_data *adap) |
| 66 | { |
David Brownell | cf978ab | 2008-01-27 18:14:46 +0100 | [diff] [blame] | 67 | setscl(adap, 0); |
Jean Delvare | 424ed67 | 2007-05-01 23:26:33 +0200 | [diff] [blame] | 68 | udelay(adap->udelay / 2); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 69 | } |
| 70 | |
| 71 | /* |
| 72 | * Raise scl line, and do checking for delays. This is necessary for slower |
| 73 | * devices. |
| 74 | */ |
Jean Delvare | 7b288a0 | 2006-09-03 22:22:12 +0200 | [diff] [blame] | 75 | static int sclhi(struct i2c_algo_bit_data *adap) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 76 | { |
| 77 | unsigned long start; |
| 78 | |
David Brownell | cf978ab | 2008-01-27 18:14:46 +0100 | [diff] [blame] | 79 | setscl(adap, 1); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 80 | |
| 81 | /* Not all adapters have scl sense line... */ |
Jean Delvare | 7b288a0 | 2006-09-03 22:22:12 +0200 | [diff] [blame] | 82 | if (!adap->getscl) |
| 83 | goto done; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 84 | |
David Brownell | cf978ab | 2008-01-27 18:14:46 +0100 | [diff] [blame] | 85 | start = jiffies; |
| 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. |
| 91 | */ |
Ville Syrjala | 8ee161c | 2012-03-15 18:11:05 +0100 | [diff] [blame] | 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. |
| 95 | */ |
| 96 | if (getscl(adap)) |
| 97 | break; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 98 | return -ETIMEDOUT; |
Ville Syrjala | 8ee161c | 2012-03-15 18:11:05 +0100 | [diff] [blame] | 99 | } |
Jean Delvare | 41101a3 | 2012-03-26 21:47:19 +0200 | [diff] [blame] | 100 | cpu_relax(); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 101 | } |
Jean Delvare | 494dbb6 | 2007-05-01 23:26:33 +0200 | [diff] [blame] | 102 | #ifdef DEBUG |
| 103 | if (jiffies != start && i2c_debug >= 3) |
Jan Kundrát | 1204d12 | 2018-08-28 10:07:40 +0200 | [diff] [blame] | 104 | pr_debug("i2c-algo-bit: needed %ld jiffies for SCL to go high\n", |
| 105 | jiffies - start); |
Jean Delvare | 494dbb6 | 2007-05-01 23:26:33 +0200 | [diff] [blame] | 106 | #endif |
Jean Delvare | 7b288a0 | 2006-09-03 22:22:12 +0200 | [diff] [blame] | 107 | |
| 108 | done: |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 109 | udelay(adap->udelay); |
| 110 | return 0; |
David Brownell | cf978ab | 2008-01-27 18:14:46 +0100 | [diff] [blame] | 111 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 112 | |
| 113 | |
| 114 | /* --- other auxiliary functions -------------------------------------- */ |
David Brownell | cf978ab | 2008-01-27 18:14:46 +0100 | [diff] [blame] | 115 | static void i2c_start(struct i2c_algo_bit_data *adap) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 116 | { |
| 117 | /* assert: scl, sda are high */ |
Jean Delvare | 424ed67 | 2007-05-01 23:26:33 +0200 | [diff] [blame] | 118 | setsda(adap, 0); |
| 119 | udelay(adap->udelay); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 120 | scllo(adap); |
| 121 | } |
| 122 | |
David Brownell | cf978ab | 2008-01-27 18:14:46 +0100 | [diff] [blame] | 123 | static void i2c_repstart(struct i2c_algo_bit_data *adap) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 124 | { |
Jean Delvare | 424ed67 | 2007-05-01 23:26:33 +0200 | [diff] [blame] | 125 | /* assert: scl is low */ |
Jean Delvare | 424ed67 | 2007-05-01 23:26:33 +0200 | [diff] [blame] | 126 | sdahi(adap); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 127 | sclhi(adap); |
Jean Delvare | 424ed67 | 2007-05-01 23:26:33 +0200 | [diff] [blame] | 128 | setsda(adap, 0); |
| 129 | udelay(adap->udelay); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 130 | scllo(adap); |
| 131 | } |
| 132 | |
| 133 | |
David Brownell | cf978ab | 2008-01-27 18:14:46 +0100 | [diff] [blame] | 134 | static void i2c_stop(struct i2c_algo_bit_data *adap) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 135 | { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 136 | /* assert: scl is low */ |
| 137 | sdalo(adap); |
David Brownell | cf978ab | 2008-01-27 18:14:46 +0100 | [diff] [blame] | 138 | sclhi(adap); |
Jean Delvare | 424ed67 | 2007-05-01 23:26:33 +0200 | [diff] [blame] | 139 | setsda(adap, 1); |
| 140 | udelay(adap->udelay); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 141 | } |
| 142 | |
| 143 | |
| 144 | |
David Brownell | cf978ab | 2008-01-27 18:14:46 +0100 | [diff] [blame] | 145 | /* send a byte without start cond., look for arbitration, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 146 | check ackn. from slave */ |
| 147 | /* returns: |
| 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) |
| 151 | */ |
Jean Delvare | 494dbb6 | 2007-05-01 23:26:33 +0200 | [diff] [blame] | 152 | static int i2c_outb(struct i2c_adapter *i2c_adap, unsigned char c) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 153 | { |
| 154 | int i; |
| 155 | int sb; |
| 156 | int ack; |
| 157 | struct i2c_algo_bit_data *adap = i2c_adap->algo_data; |
| 158 | |
| 159 | /* assert: scl is low */ |
David Brownell | cf978ab | 2008-01-27 18:14:46 +0100 | [diff] [blame] | 160 | for (i = 7; i >= 0; i--) { |
Jean Delvare | 494dbb6 | 2007-05-01 23:26:33 +0200 | [diff] [blame] | 161 | sb = (c >> i) & 1; |
David Brownell | cf978ab | 2008-01-27 18:14:46 +0100 | [diff] [blame] | 162 | setsda(adap, sb); |
Jean Delvare | 424ed67 | 2007-05-01 23:26:33 +0200 | [diff] [blame] | 163 | udelay((adap->udelay + 1) / 2); |
David Brownell | cf978ab | 2008-01-27 18:14:46 +0100 | [diff] [blame] | 164 | if (sclhi(adap) < 0) { /* timed out */ |
Jan Kundrát | 1204d12 | 2018-08-28 10:07:40 +0200 | [diff] [blame] | 165 | bit_dbg(1, &i2c_adap->dev, |
| 166 | "i2c_outb: 0x%02x, timeout at bit #%d\n", |
| 167 | (int)c, i); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 168 | return -ETIMEDOUT; |
David Brownell | cf978ab | 2008-01-27 18:14:46 +0100 | [diff] [blame] | 169 | } |
| 170 | /* FIXME do arbitration here: |
| 171 | * if (sb && !getsda(adap)) -> ouch! Get out of here. |
| 172 | * |
| 173 | * Report a unique code, so higher level code can retry |
| 174 | * the whole (combined) message and *NOT* issue STOP. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 175 | */ |
Jean Delvare | 424ed67 | 2007-05-01 23:26:33 +0200 | [diff] [blame] | 176 | scllo(adap); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 177 | } |
| 178 | sdahi(adap); |
David Brownell | cf978ab | 2008-01-27 18:14:46 +0100 | [diff] [blame] | 179 | if (sclhi(adap) < 0) { /* timeout */ |
Jan Kundrát | 1204d12 | 2018-08-28 10:07:40 +0200 | [diff] [blame] | 180 | bit_dbg(1, &i2c_adap->dev, |
| 181 | "i2c_outb: 0x%02x, timeout at ack\n", (int)c); |
Jean Delvare | 494dbb6 | 2007-05-01 23:26:33 +0200 | [diff] [blame] | 182 | return -ETIMEDOUT; |
David Brownell | cf978ab | 2008-01-27 18:14:46 +0100 | [diff] [blame] | 183 | } |
| 184 | |
| 185 | /* read ack: SDA should be pulled down by slave, or it may |
| 186 | * NAK (usually to report problems with the data we wrote). |
Heiner Kallweit | 9dfee14 | 2023-01-18 22:54:03 +0100 | [diff] [blame] | 187 | * Always report ACK if SDA is write-only. |
David Brownell | cf978ab | 2008-01-27 18:14:46 +0100 | [diff] [blame] | 188 | */ |
Heiner Kallweit | 9dfee14 | 2023-01-18 22:54:03 +0100 | [diff] [blame] | 189 | ack = !adap->getsda || !getsda(adap); /* ack: sda is pulled low -> success */ |
Jean Delvare | 494dbb6 | 2007-05-01 23:26:33 +0200 | [diff] [blame] | 190 | bit_dbg(2, &i2c_adap->dev, "i2c_outb: 0x%02x %s\n", (int)c, |
| 191 | ack ? "A" : "NA"); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 192 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 193 | scllo(adap); |
Jean Delvare | 494dbb6 | 2007-05-01 23:26:33 +0200 | [diff] [blame] | 194 | return ack; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 195 | /* assert: scl is low (sda undef) */ |
| 196 | } |
| 197 | |
| 198 | |
David Brownell | cf978ab | 2008-01-27 18:14:46 +0100 | [diff] [blame] | 199 | static int i2c_inb(struct i2c_adapter *i2c_adap) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 200 | { |
| 201 | /* read byte via i2c port, without start/stop sequence */ |
| 202 | /* acknowledge is sent in i2c_read. */ |
| 203 | int i; |
David Brownell | cf978ab | 2008-01-27 18:14:46 +0100 | [diff] [blame] | 204 | unsigned char indata = 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 205 | struct i2c_algo_bit_data *adap = i2c_adap->algo_data; |
| 206 | |
| 207 | /* assert: scl is low */ |
| 208 | sdahi(adap); |
David Brownell | cf978ab | 2008-01-27 18:14:46 +0100 | [diff] [blame] | 209 | for (i = 0; i < 8; i++) { |
| 210 | if (sclhi(adap) < 0) { /* timeout */ |
Jan Kundrát | 1204d12 | 2018-08-28 10:07:40 +0200 | [diff] [blame] | 211 | bit_dbg(1, &i2c_adap->dev, |
| 212 | "i2c_inb: timeout at bit #%d\n", |
| 213 | 7 - i); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 214 | return -ETIMEDOUT; |
David Brownell | cf978ab | 2008-01-27 18:14:46 +0100 | [diff] [blame] | 215 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 216 | indata *= 2; |
David Brownell | cf978ab | 2008-01-27 18:14:46 +0100 | [diff] [blame] | 217 | if (getsda(adap)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 218 | indata |= 0x01; |
Jean Delvare | 424ed67 | 2007-05-01 23:26:33 +0200 | [diff] [blame] | 219 | setscl(adap, 0); |
| 220 | udelay(i == 7 ? adap->udelay / 2 : adap->udelay); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 221 | } |
| 222 | /* assert: scl is low */ |
Jean Delvare | 494dbb6 | 2007-05-01 23:26:33 +0200 | [diff] [blame] | 223 | return indata; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 224 | } |
| 225 | |
| 226 | /* |
| 227 | * Sanity check for the adapter hardware - check the reaction of |
| 228 | * the bus lines only if it seems to be idle. |
| 229 | */ |
Alex Deucher | d3b3e15 | 2011-04-17 10:20:19 +0200 | [diff] [blame] | 230 | static int test_bus(struct i2c_adapter *i2c_adap) |
David Brownell | cf978ab | 2008-01-27 18:14:46 +0100 | [diff] [blame] | 231 | { |
Alex Deucher | d3b3e15 | 2011-04-17 10:20:19 +0200 | [diff] [blame] | 232 | struct i2c_algo_bit_data *adap = i2c_adap->algo_data; |
| 233 | const char *name = i2c_adap->name; |
| 234 | int scl, sda, ret; |
| 235 | |
| 236 | if (adap->pre_xfer) { |
| 237 | ret = adap->pre_xfer(i2c_adap); |
| 238 | if (ret < 0) |
| 239 | return -ENODEV; |
| 240 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 241 | |
Heiner Kallweit | 9dfee14 | 2023-01-18 22:54:03 +0100 | [diff] [blame] | 242 | if (adap->getsda == NULL) |
| 243 | pr_info("%s: SDA is write-only, testing not possible\n", name); |
David Brownell | cf978ab | 2008-01-27 18:14:46 +0100 | [diff] [blame] | 244 | if (adap->getscl == NULL) |
Heiner Kallweit | 9dfee14 | 2023-01-18 22:54:03 +0100 | [diff] [blame] | 245 | pr_info("%s: SCL is write-only, testing not possible\n", name); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 246 | |
Heiner Kallweit | 9dfee14 | 2023-01-18 22:54:03 +0100 | [diff] [blame] | 247 | sda = adap->getsda ? getsda(adap) : 1; |
| 248 | scl = adap->getscl ? getscl(adap) : 1; |
David Brownell | cf978ab | 2008-01-27 18:14:46 +0100 | [diff] [blame] | 249 | if (!scl || !sda) { |
Heiner Kallweit | 9dfee14 | 2023-01-18 22:54:03 +0100 | [diff] [blame] | 250 | pr_warn("%s: bus seems to be busy (scl=%d, sda=%d)\n", name, scl, sda); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 251 | goto bailout; |
| 252 | } |
| 253 | |
| 254 | sdalo(adap); |
Heiner Kallweit | 9dfee14 | 2023-01-18 22:54:03 +0100 | [diff] [blame] | 255 | if (adap->getsda && getsda(adap)) { |
| 256 | pr_warn("%s: SDA stuck high!\n", name); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 257 | goto bailout; |
| 258 | } |
Heiner Kallweit | 9dfee14 | 2023-01-18 22:54:03 +0100 | [diff] [blame] | 259 | if (adap->getscl && !getscl(adap)) { |
| 260 | pr_warn("%s: SCL unexpected low while pulling SDA low!\n", name); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 261 | goto bailout; |
David Brownell | cf978ab | 2008-01-27 18:14:46 +0100 | [diff] [blame] | 262 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 263 | |
| 264 | sdahi(adap); |
Heiner Kallweit | 9dfee14 | 2023-01-18 22:54:03 +0100 | [diff] [blame] | 265 | if (adap->getsda && !getsda(adap)) { |
| 266 | pr_warn("%s: SDA stuck low!\n", name); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 267 | goto bailout; |
| 268 | } |
Heiner Kallweit | 9dfee14 | 2023-01-18 22:54:03 +0100 | [diff] [blame] | 269 | if (adap->getscl && !getscl(adap)) { |
| 270 | pr_warn("%s: SCL unexpected low while pulling SDA high!\n", name); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 271 | goto bailout; |
| 272 | } |
| 273 | |
| 274 | scllo(adap); |
Heiner Kallweit | 9dfee14 | 2023-01-18 22:54:03 +0100 | [diff] [blame] | 275 | if (adap->getscl && getscl(adap)) { |
| 276 | pr_warn("%s: SCL stuck high!\n", name); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 277 | goto bailout; |
| 278 | } |
Heiner Kallweit | 9dfee14 | 2023-01-18 22:54:03 +0100 | [diff] [blame] | 279 | if (adap->getsda && !getsda(adap)) { |
| 280 | pr_warn("%s: SDA unexpected low while pulling SCL low!\n", name); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 281 | goto bailout; |
| 282 | } |
David Brownell | cf978ab | 2008-01-27 18:14:46 +0100 | [diff] [blame] | 283 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 284 | sclhi(adap); |
Heiner Kallweit | 9dfee14 | 2023-01-18 22:54:03 +0100 | [diff] [blame] | 285 | if (adap->getscl && !getscl(adap)) { |
| 286 | pr_warn("%s: SCL stuck low!\n", name); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 287 | goto bailout; |
| 288 | } |
Heiner Kallweit | 9dfee14 | 2023-01-18 22:54:03 +0100 | [diff] [blame] | 289 | if (adap->getsda && !getsda(adap)) { |
| 290 | pr_warn("%s: SDA unexpected low while pulling SCL high!\n", name); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 291 | goto bailout; |
| 292 | } |
Alex Deucher | d3b3e15 | 2011-04-17 10:20:19 +0200 | [diff] [blame] | 293 | |
| 294 | if (adap->post_xfer) |
| 295 | adap->post_xfer(i2c_adap); |
| 296 | |
Jean Delvare | 494dbb6 | 2007-05-01 23:26:33 +0200 | [diff] [blame] | 297 | pr_info("%s: Test OK\n", name); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 298 | return 0; |
| 299 | bailout: |
| 300 | sdahi(adap); |
| 301 | sclhi(adap); |
Alex Deucher | d3b3e15 | 2011-04-17 10:20:19 +0200 | [diff] [blame] | 302 | |
| 303 | if (adap->post_xfer) |
| 304 | adap->post_xfer(i2c_adap); |
| 305 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 306 | return -ENODEV; |
| 307 | } |
| 308 | |
| 309 | /* ----- Utility functions |
| 310 | */ |
| 311 | |
| 312 | /* try_address tries to contact a chip for a number of |
| 313 | * times before it gives up. |
| 314 | * return values: |
| 315 | * 1 chip answered |
| 316 | * 0 chip did not answer |
| 317 | * -x transmission error |
| 318 | */ |
Jean Delvare | 7b288a0 | 2006-09-03 22:22:12 +0200 | [diff] [blame] | 319 | static int try_address(struct i2c_adapter *i2c_adap, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 320 | unsigned char addr, int retries) |
| 321 | { |
| 322 | struct i2c_algo_bit_data *adap = i2c_adap->algo_data; |
David Brownell | 9714034 | 2008-07-14 22:38:25 +0200 | [diff] [blame] | 323 | int i, ret = 0; |
David Brownell | cf978ab | 2008-01-27 18:14:46 +0100 | [diff] [blame] | 324 | |
| 325 | for (i = 0; i <= retries; i++) { |
| 326 | ret = i2c_outb(i2c_adap, addr); |
Jean Delvare | 1ecac07 | 2007-05-01 23:26:28 +0200 | [diff] [blame] | 327 | if (ret == 1 || i == retries) |
| 328 | break; |
Jean Delvare | 494dbb6 | 2007-05-01 23:26:33 +0200 | [diff] [blame] | 329 | bit_dbg(3, &i2c_adap->dev, "emitting stop condition\n"); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 330 | i2c_stop(adap); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 331 | udelay(adap->udelay); |
Jean Delvare | 424ed67 | 2007-05-01 23:26:33 +0200 | [diff] [blame] | 332 | yield(); |
Jean Delvare | 494dbb6 | 2007-05-01 23:26:33 +0200 | [diff] [blame] | 333 | bit_dbg(3, &i2c_adap->dev, "emitting start condition\n"); |
Jean Delvare | 424ed67 | 2007-05-01 23:26:33 +0200 | [diff] [blame] | 334 | i2c_start(adap); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 335 | } |
Jean Delvare | 494dbb6 | 2007-05-01 23:26:33 +0200 | [diff] [blame] | 336 | if (i && ret) |
Jan Kundrát | 1204d12 | 2018-08-28 10:07:40 +0200 | [diff] [blame] | 337 | bit_dbg(1, &i2c_adap->dev, |
| 338 | "Used %d tries to %s client at 0x%02x: %s\n", i + 1, |
Jean Delvare | 494dbb6 | 2007-05-01 23:26:33 +0200 | [diff] [blame] | 339 | addr & 1 ? "read from" : "write to", addr >> 1, |
| 340 | ret == 1 ? "success" : "failed, timeout?"); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 341 | return ret; |
| 342 | } |
| 343 | |
| 344 | static int sendbytes(struct i2c_adapter *i2c_adap, struct i2c_msg *msg) |
| 345 | { |
Jean Delvare | 494dbb6 | 2007-05-01 23:26:33 +0200 | [diff] [blame] | 346 | const unsigned char *temp = msg->buf; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 347 | int count = msg->len; |
David Brownell | cf978ab | 2008-01-27 18:14:46 +0100 | [diff] [blame] | 348 | unsigned short nak_ok = msg->flags & I2C_M_IGNORE_NAK; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 349 | int retval; |
David Brownell | cf978ab | 2008-01-27 18:14:46 +0100 | [diff] [blame] | 350 | int wrcount = 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 351 | |
| 352 | while (count > 0) { |
Jean Delvare | 494dbb6 | 2007-05-01 23:26:33 +0200 | [diff] [blame] | 353 | retval = i2c_outb(i2c_adap, *temp); |
David Brownell | cf978ab | 2008-01-27 18:14:46 +0100 | [diff] [blame] | 354 | |
| 355 | /* OK/ACK; or ignored NAK */ |
| 356 | if ((retval > 0) || (nak_ok && (retval == 0))) { |
| 357 | count--; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 358 | temp++; |
| 359 | wrcount++; |
David Brownell | bf3e2d1 | 2008-01-27 18:14:46 +0100 | [diff] [blame] | 360 | |
| 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. |
| 364 | */ |
| 365 | } else if (retval == 0) { |
| 366 | dev_err(&i2c_adap->dev, "sendbytes: NAK bailout.\n"); |
| 367 | return -EIO; |
| 368 | |
| 369 | /* Timeout; or (someday) lost arbitration |
| 370 | * |
| 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. |
| 375 | */ |
| 376 | } else { |
| 377 | dev_err(&i2c_adap->dev, "sendbytes: error %d\n", |
| 378 | retval); |
| 379 | return retval; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 380 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 381 | } |
| 382 | return wrcount; |
| 383 | } |
| 384 | |
David Brownell | 939bc49 | 2007-09-09 22:29:14 +0200 | [diff] [blame] | 385 | static int acknak(struct i2c_adapter *i2c_adap, int is_ack) |
| 386 | { |
| 387 | struct i2c_algo_bit_data *adap = i2c_adap->algo_data; |
| 388 | |
| 389 | /* assert: sda is high */ |
| 390 | if (is_ack) /* send ack */ |
| 391 | setsda(adap, 0); |
| 392 | udelay((adap->udelay + 1) / 2); |
| 393 | if (sclhi(adap) < 0) { /* timeout */ |
| 394 | dev_err(&i2c_adap->dev, "readbytes: ack/nak timeout\n"); |
| 395 | return -ETIMEDOUT; |
| 396 | } |
| 397 | scllo(adap); |
| 398 | return 0; |
| 399 | } |
| 400 | |
Jean Delvare | 7b288a0 | 2006-09-03 22:22:12 +0200 | [diff] [blame] | 401 | static int readbytes(struct i2c_adapter *i2c_adap, struct i2c_msg *msg) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 402 | { |
| 403 | int inval; |
David Brownell | cf978ab | 2008-01-27 18:14:46 +0100 | [diff] [blame] | 404 | int rdcount = 0; /* counts bytes read */ |
Jean Delvare | 494dbb6 | 2007-05-01 23:26:33 +0200 | [diff] [blame] | 405 | unsigned char *temp = msg->buf; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 406 | int count = msg->len; |
David Brownell | 939bc49 | 2007-09-09 22:29:14 +0200 | [diff] [blame] | 407 | const unsigned flags = msg->flags; |
Heiner Kallweit | 9dfee14 | 2023-01-18 22:54:03 +0100 | [diff] [blame] | 408 | struct i2c_algo_bit_data *adap = i2c_adap->algo_data; |
| 409 | |
| 410 | if (!adap->getsda) |
| 411 | return -EOPNOTSUPP; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 412 | |
| 413 | while (count > 0) { |
| 414 | inval = i2c_inb(i2c_adap); |
David Brownell | cf978ab | 2008-01-27 18:14:46 +0100 | [diff] [blame] | 415 | if (inval >= 0) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 416 | *temp = inval; |
| 417 | rdcount++; |
| 418 | } else { /* read timed out */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 419 | break; |
| 420 | } |
| 421 | |
| 422 | temp++; |
| 423 | count--; |
| 424 | |
Jean Delvare | 3c4bb24 | 2007-05-01 23:26:29 +0200 | [diff] [blame] | 425 | /* Some SMBus transactions require that we receive the |
| 426 | transaction length as the first read byte. */ |
David Brownell | 939bc49 | 2007-09-09 22:29:14 +0200 | [diff] [blame] | 427 | if (rdcount == 1 && (flags & I2C_M_RECV_LEN)) { |
Jean Delvare | 3c4bb24 | 2007-05-01 23:26:29 +0200 | [diff] [blame] | 428 | if (inval <= 0 || inval > I2C_SMBUS_BLOCK_MAX) { |
David Brownell | 939bc49 | 2007-09-09 22:29:14 +0200 | [diff] [blame] | 429 | if (!(flags & I2C_M_NO_RD_ACK)) |
| 430 | acknak(i2c_adap, 0); |
Jan Kundrát | 1204d12 | 2018-08-28 10:07:40 +0200 | [diff] [blame] | 431 | dev_err(&i2c_adap->dev, |
| 432 | "readbytes: invalid block length (%d)\n", |
| 433 | inval); |
Jean Delvare | abc01b2 | 2011-10-30 13:47:25 +0100 | [diff] [blame] | 434 | return -EPROTO; |
Jean Delvare | 3c4bb24 | 2007-05-01 23:26:29 +0200 | [diff] [blame] | 435 | } |
| 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. */ |
| 439 | count += inval; |
| 440 | msg->len += inval; |
| 441 | } |
David Brownell | 939bc49 | 2007-09-09 22:29:14 +0200 | [diff] [blame] | 442 | |
| 443 | bit_dbg(2, &i2c_adap->dev, "readbytes: 0x%02x %s\n", |
| 444 | inval, |
| 445 | (flags & I2C_M_NO_RD_ACK) |
| 446 | ? "(no ack/nak)" |
| 447 | : (count ? "A" : "NA")); |
| 448 | |
| 449 | if (!(flags & I2C_M_NO_RD_ACK)) { |
| 450 | inval = acknak(i2c_adap, count); |
| 451 | if (inval < 0) |
| 452 | return inval; |
| 453 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 454 | } |
| 455 | return rdcount; |
| 456 | } |
| 457 | |
| 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. |
| 461 | * returns: |
| 462 | * 0 everything went okay, the chip ack'ed, or IGNORE_NAK flag was set |
Jean Delvare | abc01b2 | 2011-10-30 13:47:25 +0100 | [diff] [blame] | 463 | * -x an error occurred (like: -ENXIO if the device did not answer, or |
David Brownell | cf978ab | 2008-01-27 18:14:46 +0100 | [diff] [blame] | 464 | * -ETIMEDOUT, for example if the lines are stuck...) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 465 | */ |
Jean Delvare | 7b288a0 | 2006-09-03 22:22:12 +0200 | [diff] [blame] | 466 | static int bit_doAddress(struct i2c_adapter *i2c_adap, struct i2c_msg *msg) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 467 | { |
| 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; |
| 471 | |
| 472 | unsigned char addr; |
| 473 | int ret, retries; |
| 474 | |
| 475 | retries = nak_ok ? 0 : i2c_adap->retries; |
David Brownell | cf978ab | 2008-01-27 18:14:46 +0100 | [diff] [blame] | 476 | |
| 477 | if (flags & I2C_M_TEN) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 478 | /* a ten bit address */ |
Jeffrey (Sheng-Hui) Chu | cc6bcf7 | 2011-11-23 11:33:07 +0100 | [diff] [blame] | 479 | addr = 0xf0 | ((msg->addr >> 7) & 0x06); |
Jean Delvare | 494dbb6 | 2007-05-01 23:26:33 +0200 | [diff] [blame] | 480 | bit_dbg(2, &i2c_adap->dev, "addr0: %d\n", addr); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 481 | /* try extended address code...*/ |
| 482 | ret = try_address(i2c_adap, addr, retries); |
| 483 | if ((ret != 1) && !nak_ok) { |
Jean Delvare | 494dbb6 | 2007-05-01 23:26:33 +0200 | [diff] [blame] | 484 | dev_err(&i2c_adap->dev, |
| 485 | "died at extended address code\n"); |
Jean Delvare | abc01b2 | 2011-10-30 13:47:25 +0100 | [diff] [blame] | 486 | return -ENXIO; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 487 | } |
| 488 | /* the remaining 8 bit address */ |
Jeffrey (Sheng-Hui) Chu | cc6bcf7 | 2011-11-23 11:33:07 +0100 | [diff] [blame] | 489 | ret = i2c_outb(i2c_adap, msg->addr & 0xff); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 490 | if ((ret != 1) && !nak_ok) { |
| 491 | /* the chip did not ack / xmission error occurred */ |
Jean Delvare | 494dbb6 | 2007-05-01 23:26:33 +0200 | [diff] [blame] | 492 | dev_err(&i2c_adap->dev, "died at 2nd address code\n"); |
Jean Delvare | abc01b2 | 2011-10-30 13:47:25 +0100 | [diff] [blame] | 493 | return -ENXIO; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 494 | } |
David Brownell | cf978ab | 2008-01-27 18:14:46 +0100 | [diff] [blame] | 495 | if (flags & I2C_M_RD) { |
Jan Kundrát | 1204d12 | 2018-08-28 10:07:40 +0200 | [diff] [blame] | 496 | bit_dbg(3, &i2c_adap->dev, |
| 497 | "emitting repeated start condition\n"); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 498 | i2c_repstart(adap); |
| 499 | /* okay, now switch into reading mode */ |
| 500 | addr |= 0x01; |
| 501 | ret = try_address(i2c_adap, addr, retries); |
David Brownell | cf978ab | 2008-01-27 18:14:46 +0100 | [diff] [blame] | 502 | if ((ret != 1) && !nak_ok) { |
Jean Delvare | 494dbb6 | 2007-05-01 23:26:33 +0200 | [diff] [blame] | 503 | dev_err(&i2c_adap->dev, |
| 504 | "died at repeated address code\n"); |
Jean Delvare | abc01b2 | 2011-10-30 13:47:25 +0100 | [diff] [blame] | 505 | return -EIO; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 506 | } |
| 507 | } |
| 508 | } else { /* normal 7bit address */ |
Peter Rosin | ac6d529 | 2018-05-16 09:16:46 +0200 | [diff] [blame] | 509 | addr = i2c_8bit_addr_from_msg(msg); |
David Brownell | cf978ab | 2008-01-27 18:14:46 +0100 | [diff] [blame] | 510 | if (flags & I2C_M_REV_DIR_ADDR) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 511 | addr ^= 1; |
| 512 | ret = try_address(i2c_adap, addr, retries); |
David Brownell | cf978ab | 2008-01-27 18:14:46 +0100 | [diff] [blame] | 513 | if ((ret != 1) && !nak_ok) |
David Brownell | 9714034 | 2008-07-14 22:38:25 +0200 | [diff] [blame] | 514 | return -ENXIO; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 515 | } |
| 516 | |
| 517 | return 0; |
| 518 | } |
| 519 | |
| 520 | static int bit_xfer(struct i2c_adapter *i2c_adap, |
| 521 | struct i2c_msg msgs[], int num) |
| 522 | { |
| 523 | struct i2c_msg *pmsg; |
| 524 | struct i2c_algo_bit_data *adap = i2c_adap->algo_data; |
David Brownell | cf978ab | 2008-01-27 18:14:46 +0100 | [diff] [blame] | 525 | int i, ret; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 526 | unsigned short nak_ok; |
| 527 | |
Jean Delvare | 0a9c147 | 2010-03-13 20:56:56 +0100 | [diff] [blame] | 528 | if (adap->pre_xfer) { |
| 529 | ret = adap->pre_xfer(i2c_adap); |
| 530 | if (ret < 0) |
| 531 | return ret; |
| 532 | } |
| 533 | |
Jean Delvare | 494dbb6 | 2007-05-01 23:26:33 +0200 | [diff] [blame] | 534 | bit_dbg(3, &i2c_adap->dev, "emitting start condition\n"); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 535 | i2c_start(adap); |
David Brownell | cf978ab | 2008-01-27 18:14:46 +0100 | [diff] [blame] | 536 | for (i = 0; i < num; i++) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 537 | pmsg = &msgs[i]; |
David Brownell | cf978ab | 2008-01-27 18:14:46 +0100 | [diff] [blame] | 538 | nak_ok = pmsg->flags & I2C_M_IGNORE_NAK; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 539 | if (!(pmsg->flags & I2C_M_NOSTART)) { |
| 540 | if (i) { |
Jean Delvare | 606fd27 | 2017-06-21 09:24:02 +0200 | [diff] [blame] | 541 | if (msgs[i - 1].flags & I2C_M_STOP) { |
| 542 | bit_dbg(3, &i2c_adap->dev, |
| 543 | "emitting enforced stop/start condition\n"); |
| 544 | i2c_stop(adap); |
| 545 | i2c_start(adap); |
| 546 | } else { |
| 547 | bit_dbg(3, &i2c_adap->dev, |
| 548 | "emitting repeated start condition\n"); |
| 549 | i2c_repstart(adap); |
| 550 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 551 | } |
| 552 | ret = bit_doAddress(i2c_adap, pmsg); |
| 553 | if ((ret != 0) && !nak_ok) { |
Jan Kundrát | 1204d12 | 2018-08-28 10:07:40 +0200 | [diff] [blame] | 554 | bit_dbg(1, &i2c_adap->dev, |
| 555 | "NAK from device addr 0x%02x msg #%d\n", |
Jean Delvare | 494dbb6 | 2007-05-01 23:26:33 +0200 | [diff] [blame] | 556 | msgs[i].addr, i); |
Jean Delvare | 1ecac07 | 2007-05-01 23:26:28 +0200 | [diff] [blame] | 557 | goto bailout; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 558 | } |
| 559 | } |
David Brownell | cf978ab | 2008-01-27 18:14:46 +0100 | [diff] [blame] | 560 | if (pmsg->flags & I2C_M_RD) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 561 | /* read bytes into buffer*/ |
| 562 | ret = readbytes(i2c_adap, pmsg); |
Jean Delvare | 494dbb6 | 2007-05-01 23:26:33 +0200 | [diff] [blame] | 563 | if (ret >= 1) |
| 564 | bit_dbg(2, &i2c_adap->dev, "read %d byte%s\n", |
| 565 | ret, ret == 1 ? "" : "s"); |
Jean Delvare | 1ecac07 | 2007-05-01 23:26:28 +0200 | [diff] [blame] | 566 | if (ret < pmsg->len) { |
| 567 | if (ret >= 0) |
Jean Delvare | abc01b2 | 2011-10-30 13:47:25 +0100 | [diff] [blame] | 568 | ret = -EIO; |
Jean Delvare | 1ecac07 | 2007-05-01 23:26:28 +0200 | [diff] [blame] | 569 | goto bailout; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 570 | } |
| 571 | } else { |
| 572 | /* write bytes from buffer */ |
| 573 | ret = sendbytes(i2c_adap, pmsg); |
Jean Delvare | 494dbb6 | 2007-05-01 23:26:33 +0200 | [diff] [blame] | 574 | if (ret >= 1) |
| 575 | bit_dbg(2, &i2c_adap->dev, "wrote %d byte%s\n", |
| 576 | ret, ret == 1 ? "" : "s"); |
Jean Delvare | 1ecac07 | 2007-05-01 23:26:28 +0200 | [diff] [blame] | 577 | if (ret < pmsg->len) { |
| 578 | if (ret >= 0) |
Jean Delvare | abc01b2 | 2011-10-30 13:47:25 +0100 | [diff] [blame] | 579 | ret = -EIO; |
Jean Delvare | 1ecac07 | 2007-05-01 23:26:28 +0200 | [diff] [blame] | 580 | goto bailout; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 581 | } |
| 582 | } |
| 583 | } |
Jean Delvare | 1ecac07 | 2007-05-01 23:26:28 +0200 | [diff] [blame] | 584 | ret = i; |
| 585 | |
| 586 | bailout: |
Jean Delvare | 494dbb6 | 2007-05-01 23:26:33 +0200 | [diff] [blame] | 587 | bit_dbg(3, &i2c_adap->dev, "emitting stop condition\n"); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 588 | i2c_stop(adap); |
Jean Delvare | 0a9c147 | 2010-03-13 20:56:56 +0100 | [diff] [blame] | 589 | |
| 590 | if (adap->post_xfer) |
| 591 | adap->post_xfer(i2c_adap); |
Jean Delvare | 1ecac07 | 2007-05-01 23:26:28 +0200 | [diff] [blame] | 592 | return ret; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 593 | } |
| 594 | |
Wolfram Sang | 8927fbf | 2019-04-03 14:40:18 +0200 | [diff] [blame] | 595 | /* |
| 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 |
| 599 | * 'const'. |
| 600 | */ |
| 601 | static int bit_xfer_atomic(struct i2c_adapter *i2c_adap, struct i2c_msg msgs[], |
| 602 | int num) |
| 603 | { |
| 604 | struct i2c_algo_bit_data *adap = i2c_adap->algo_data; |
| 605 | |
| 606 | if (!adap->can_do_atomic) |
| 607 | dev_warn(&i2c_adap->dev, "not flagged for atomic transfers\n"); |
| 608 | |
| 609 | return bit_xfer(i2c_adap, msgs, num); |
| 610 | } |
| 611 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 612 | static u32 bit_func(struct i2c_adapter *adap) |
| 613 | { |
Wolfram Sang | 58d2330 | 2021-01-09 13:43:10 +0100 | [diff] [blame] | 614 | return I2C_FUNC_I2C | I2C_FUNC_NOSTART | I2C_FUNC_SMBUS_EMUL_ALL | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 615 | I2C_FUNC_10BIT_ADDR | I2C_FUNC_PROTOCOL_MANGLING; |
| 616 | } |
| 617 | |
| 618 | |
| 619 | /* -----exported algorithm data: ------------------------------------- */ |
| 620 | |
Daniel Vetter | b0209b3 | 2012-02-28 00:39:39 +0100 | [diff] [blame] | 621 | const struct i2c_algorithm i2c_bit_algo = { |
Wolfram Sang | 8927fbf | 2019-04-03 14:40:18 +0200 | [diff] [blame] | 622 | .master_xfer = bit_xfer, |
| 623 | .master_xfer_atomic = bit_xfer_atomic, |
| 624 | .functionality = bit_func, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 625 | }; |
Daniel Vetter | b0209b3 | 2012-02-28 00:39:39 +0100 | [diff] [blame] | 626 | EXPORT_SYMBOL(i2c_bit_algo); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 627 | |
Michele Curti | ef51d3f | 2016-04-27 21:37:51 +0200 | [diff] [blame] | 628 | static const struct i2c_adapter_quirks i2c_bit_quirk_no_clk_stretch = { |
Nicola Corna | a94d306 | 2015-10-29 12:34:24 +0100 | [diff] [blame] | 629 | .flags = I2C_AQ_NO_CLK_STRETCH, |
| 630 | }; |
| 631 | |
David Brownell | cf978ab | 2008-01-27 18:14:46 +0100 | [diff] [blame] | 632 | /* |
| 633 | * registering functions to load algorithms at runtime |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 634 | */ |
Jean Delvare | f451171 | 2011-01-10 22:11:23 +0100 | [diff] [blame] | 635 | static int __i2c_bit_add_bus(struct i2c_adapter *adap, |
| 636 | int (*add_adapter)(struct i2c_adapter *)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 637 | { |
| 638 | struct i2c_algo_bit_data *bit_adap = adap->algo_data; |
Jean Delvare | af5a60b | 2011-01-10 22:11:23 +0100 | [diff] [blame] | 639 | int ret; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 640 | |
| 641 | if (bit_test) { |
Alex Deucher | d3b3e15 | 2011-04-17 10:20:19 +0200 | [diff] [blame] | 642 | ret = test_bus(adap); |
Jean Delvare | 1bddab7 | 2011-10-30 13:47:25 +0100 | [diff] [blame] | 643 | if (bit_test >= 2 && ret < 0) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 644 | return -ENODEV; |
| 645 | } |
| 646 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 647 | /* register new adapter to i2c module... */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 648 | adap->algo = &i2c_bit_algo; |
Jean Delvare | 8fcfef6 | 2009-03-28 21:34:43 +0100 | [diff] [blame] | 649 | adap->retries = 3; |
Nicola Corna | a94d306 | 2015-10-29 12:34:24 +0100 | [diff] [blame] | 650 | if (bit_adap->getscl == NULL) |
| 651 | adap->quirks = &i2c_bit_quirk_no_clk_stretch; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 652 | |
Wolfram Sang | 2173ed0 | 2018-06-16 22:37:57 +0900 | [diff] [blame] | 653 | /* |
| 654 | * We tried forcing SCL/SDA to an initial state here. But that caused a |
| 655 | * regression, sadly. Check Bugzilla #200045 for details. |
| 656 | */ |
| 657 | |
Jean Delvare | af5a60b | 2011-01-10 22:11:23 +0100 | [diff] [blame] | 658 | ret = add_adapter(adap); |
| 659 | if (ret < 0) |
| 660 | return ret; |
| 661 | |
Heiner Kallweit | 9dfee14 | 2023-01-18 22:54:03 +0100 | [diff] [blame] | 662 | if (bit_adap->getsda == NULL) |
| 663 | dev_warn(&adap->dev, "Not I2C compliant: can't read SDA\n"); |
| 664 | |
| 665 | if (bit_adap->getscl == NULL) |
Jean Delvare | af5a60b | 2011-01-10 22:11:23 +0100 | [diff] [blame] | 666 | dev_warn(&adap->dev, "Not I2C compliant: can't read SCL\n"); |
Heiner Kallweit | 9dfee14 | 2023-01-18 22:54:03 +0100 | [diff] [blame] | 667 | |
| 668 | if (bit_adap->getsda == NULL || bit_adap->getscl == NULL) |
Jean Delvare | af5a60b | 2011-01-10 22:11:23 +0100 | [diff] [blame] | 669 | dev_warn(&adap->dev, "Bus may be unreliable\n"); |
Heiner Kallweit | 9dfee14 | 2023-01-18 22:54:03 +0100 | [diff] [blame] | 670 | |
Jean Delvare | af5a60b | 2011-01-10 22:11:23 +0100 | [diff] [blame] | 671 | return 0; |
Jean Delvare | 0f3b483 | 2007-05-01 23:26:31 +0200 | [diff] [blame] | 672 | } |
| 673 | |
| 674 | int i2c_bit_add_bus(struct i2c_adapter *adap) |
| 675 | { |
Jean Delvare | f451171 | 2011-01-10 22:11:23 +0100 | [diff] [blame] | 676 | return __i2c_bit_add_bus(adap, i2c_add_adapter); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 677 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 678 | EXPORT_SYMBOL(i2c_bit_add_bus); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 679 | |
Jean Delvare | 0f3b483 | 2007-05-01 23:26:31 +0200 | [diff] [blame] | 680 | int i2c_bit_add_numbered_bus(struct i2c_adapter *adap) |
| 681 | { |
Jean Delvare | f451171 | 2011-01-10 22:11:23 +0100 | [diff] [blame] | 682 | return __i2c_bit_add_bus(adap, i2c_add_numbered_adapter); |
Jean Delvare | 0f3b483 | 2007-05-01 23:26:31 +0200 | [diff] [blame] | 683 | } |
| 684 | EXPORT_SYMBOL(i2c_bit_add_numbered_bus); |
| 685 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 686 | MODULE_AUTHOR("Simon G. Vogl <simon@tk.uni-linz.ac.at>"); |
| 687 | MODULE_DESCRIPTION("I2C-Bus bit-banging algorithm"); |
| 688 | MODULE_LICENSE("GPL"); |