Przemyslaw Marczak | d3aa7e1 | 2015-03-31 18:57:18 +0200 | [diff] [blame] | 1 | /* |
| 2 | * (C) Copyright 2015, Samsung Electronics |
| 3 | * Przemyslaw Marczak <p.marczak@samsung.com> |
| 4 | * |
| 5 | * This file is based on: drivers/i2c/soft-i2c.c, |
| 6 | * with added driver-model support and code cleanup. |
| 7 | */ |
| 8 | #include <common.h> |
| 9 | #include <errno.h> |
| 10 | #include <dm.h> |
| 11 | #include <i2c.h> |
Simon Glass | 0f2af88 | 2020-05-10 11:40:05 -0600 | [diff] [blame] | 12 | #include <log.h> |
Przemyslaw Marczak | d3aa7e1 | 2015-03-31 18:57:18 +0200 | [diff] [blame] | 13 | #include <asm/gpio.h> |
Simon Glass | dbd7954 | 2020-05-10 11:40:11 -0600 | [diff] [blame] | 14 | #include <linux/delay.h> |
Przemyslaw Marczak | d3aa7e1 | 2015-03-31 18:57:18 +0200 | [diff] [blame] | 15 | |
| 16 | #define DEFAULT_UDELAY 5 |
| 17 | #define RETRIES 0 |
| 18 | #define I2C_ACK 0 |
| 19 | #define I2C_NOACK 1 |
| 20 | |
Przemyslaw Marczak | d3aa7e1 | 2015-03-31 18:57:18 +0200 | [diff] [blame] | 21 | enum { |
| 22 | PIN_SDA = 0, |
| 23 | PIN_SCL, |
| 24 | PIN_COUNT, |
| 25 | }; |
| 26 | |
| 27 | struct i2c_gpio_bus { |
| 28 | /** |
| 29 | * udelay - delay [us] between GPIO toggle operations, |
| 30 | * which is 1/4 of I2C speed clock period. |
| 31 | */ |
| 32 | int udelay; |
| 33 | /* sda, scl */ |
| 34 | struct gpio_desc gpios[PIN_COUNT]; |
Michael Auchter | 5f6b185 | 2020-02-07 10:55:31 -0600 | [diff] [blame] | 35 | |
| 36 | int (*get_sda)(struct i2c_gpio_bus *bus); |
| 37 | void (*set_sda)(struct i2c_gpio_bus *bus, int bit); |
| 38 | void (*set_scl)(struct i2c_gpio_bus *bus, int bit); |
Przemyslaw Marczak | d3aa7e1 | 2015-03-31 18:57:18 +0200 | [diff] [blame] | 39 | }; |
| 40 | |
Michael Auchter | 5f6b185 | 2020-02-07 10:55:31 -0600 | [diff] [blame] | 41 | static int i2c_gpio_sda_get(struct i2c_gpio_bus *bus) |
Przemyslaw Marczak | d3aa7e1 | 2015-03-31 18:57:18 +0200 | [diff] [blame] | 42 | { |
Michael Auchter | 5f6b185 | 2020-02-07 10:55:31 -0600 | [diff] [blame] | 43 | struct gpio_desc *sda = &bus->gpios[PIN_SDA]; |
| 44 | |
Przemyslaw Marczak | d3aa7e1 | 2015-03-31 18:57:18 +0200 | [diff] [blame] | 45 | return dm_gpio_get_value(sda); |
| 46 | } |
| 47 | |
Michael Auchter | 5f6b185 | 2020-02-07 10:55:31 -0600 | [diff] [blame] | 48 | static void i2c_gpio_sda_set(struct i2c_gpio_bus *bus, int bit) |
Przemyslaw Marczak | d3aa7e1 | 2015-03-31 18:57:18 +0200 | [diff] [blame] | 49 | { |
Michael Auchter | 5f6b185 | 2020-02-07 10:55:31 -0600 | [diff] [blame] | 50 | struct gpio_desc *sda = &bus->gpios[PIN_SDA]; |
| 51 | |
Axel Lin | 0547483 | 2015-04-26 11:05:38 +0800 | [diff] [blame] | 52 | if (bit) |
Harm Berntsen | f5989c1 | 2020-11-29 09:47:15 +0000 | [diff] [blame] | 53 | sda->flags = (sda->flags & ~GPIOD_IS_OUT) | GPIOD_IS_IN; |
Axel Lin | 0547483 | 2015-04-26 11:05:38 +0800 | [diff] [blame] | 54 | else |
Harm Berntsen | f5989c1 | 2020-11-29 09:47:15 +0000 | [diff] [blame] | 55 | sda->flags = (sda->flags & (~GPIOD_IS_IN & ~GPIOD_IS_OUT_ACTIVE)) | GPIOD_IS_OUT; |
| 56 | dm_gpio_set_dir(sda); |
Przemyslaw Marczak | d3aa7e1 | 2015-03-31 18:57:18 +0200 | [diff] [blame] | 57 | } |
| 58 | |
Michael Auchter | 5f6b185 | 2020-02-07 10:55:31 -0600 | [diff] [blame] | 59 | static void i2c_gpio_scl_set(struct i2c_gpio_bus *bus, int bit) |
Przemyslaw Marczak | d3aa7e1 | 2015-03-31 18:57:18 +0200 | [diff] [blame] | 60 | { |
Michael Auchter | 5f6b185 | 2020-02-07 10:55:31 -0600 | [diff] [blame] | 61 | struct gpio_desc *scl = &bus->gpios[PIN_SCL]; |
Heiko Schocher | 48f39d8 | 2020-03-16 07:55:06 +0100 | [diff] [blame] | 62 | int count = 0; |
| 63 | |
| 64 | if (bit) { |
Harm Berntsen | f5989c1 | 2020-11-29 09:47:15 +0000 | [diff] [blame] | 65 | scl->flags = (scl->flags & ~GPIOD_IS_OUT) | GPIOD_IS_IN; |
| 66 | dm_gpio_set_dir(scl); |
Heiko Schocher | 48f39d8 | 2020-03-16 07:55:06 +0100 | [diff] [blame] | 67 | while (!dm_gpio_get_value(scl) && count++ < 100000) |
| 68 | udelay(1); |
| 69 | |
| 70 | if (!dm_gpio_get_value(scl)) |
| 71 | pr_err("timeout waiting on slave to release scl\n"); |
| 72 | } else { |
Harm Berntsen | f5989c1 | 2020-11-29 09:47:15 +0000 | [diff] [blame] | 73 | scl->flags = (scl->flags & (~GPIOD_IS_IN & ~GPIOD_IS_OUT_ACTIVE)) | GPIOD_IS_OUT; |
| 74 | dm_gpio_set_dir(scl); |
Heiko Schocher | 48f39d8 | 2020-03-16 07:55:06 +0100 | [diff] [blame] | 75 | } |
| 76 | } |
| 77 | |
| 78 | /* variant for output only gpios which cannot support clock stretching */ |
| 79 | static void i2c_gpio_scl_set_output_only(struct i2c_gpio_bus *bus, int bit) |
| 80 | { |
| 81 | struct gpio_desc *scl = &bus->gpios[PIN_SCL]; |
Harm Berntsen | f5989c1 | 2020-11-29 09:47:15 +0000 | [diff] [blame] | 82 | scl->flags = (scl->flags & (~GPIOD_IS_IN & ~GPIOD_IS_OUT_ACTIVE)) | GPIOD_IS_OUT; |
Axel Lin | 0547483 | 2015-04-26 11:05:38 +0800 | [diff] [blame] | 83 | |
| 84 | if (bit) |
Harm Berntsen | f5989c1 | 2020-11-29 09:47:15 +0000 | [diff] [blame] | 85 | scl->flags |= GPIOD_IS_OUT_ACTIVE; |
| 86 | dm_gpio_set_dir(scl); |
Przemyslaw Marczak | d3aa7e1 | 2015-03-31 18:57:18 +0200 | [diff] [blame] | 87 | } |
| 88 | |
Michael Auchter | 5f6b185 | 2020-02-07 10:55:31 -0600 | [diff] [blame] | 89 | static void i2c_gpio_write_bit(struct i2c_gpio_bus *bus, int delay, uchar bit) |
Przemyslaw Marczak | d3aa7e1 | 2015-03-31 18:57:18 +0200 | [diff] [blame] | 90 | { |
Michael Auchter | 5f6b185 | 2020-02-07 10:55:31 -0600 | [diff] [blame] | 91 | bus->set_scl(bus, 0); |
Przemyslaw Marczak | d3aa7e1 | 2015-03-31 18:57:18 +0200 | [diff] [blame] | 92 | udelay(delay); |
Michael Auchter | 5f6b185 | 2020-02-07 10:55:31 -0600 | [diff] [blame] | 93 | bus->set_sda(bus, bit); |
Przemyslaw Marczak | d3aa7e1 | 2015-03-31 18:57:18 +0200 | [diff] [blame] | 94 | udelay(delay); |
Michael Auchter | 5f6b185 | 2020-02-07 10:55:31 -0600 | [diff] [blame] | 95 | bus->set_scl(bus, 1); |
Przemyslaw Marczak | d3aa7e1 | 2015-03-31 18:57:18 +0200 | [diff] [blame] | 96 | udelay(2 * delay); |
| 97 | } |
| 98 | |
Michael Auchter | 5f6b185 | 2020-02-07 10:55:31 -0600 | [diff] [blame] | 99 | static int i2c_gpio_read_bit(struct i2c_gpio_bus *bus, int delay) |
Przemyslaw Marczak | d3aa7e1 | 2015-03-31 18:57:18 +0200 | [diff] [blame] | 100 | { |
| 101 | int value; |
| 102 | |
Michael Auchter | 5f6b185 | 2020-02-07 10:55:31 -0600 | [diff] [blame] | 103 | bus->set_scl(bus, 1); |
Przemyslaw Marczak | d3aa7e1 | 2015-03-31 18:57:18 +0200 | [diff] [blame] | 104 | udelay(delay); |
Michael Auchter | 5f6b185 | 2020-02-07 10:55:31 -0600 | [diff] [blame] | 105 | value = bus->get_sda(bus); |
Przemyslaw Marczak | d3aa7e1 | 2015-03-31 18:57:18 +0200 | [diff] [blame] | 106 | udelay(delay); |
Michael Auchter | 5f6b185 | 2020-02-07 10:55:31 -0600 | [diff] [blame] | 107 | bus->set_scl(bus, 0); |
Przemyslaw Marczak | d3aa7e1 | 2015-03-31 18:57:18 +0200 | [diff] [blame] | 108 | udelay(2 * delay); |
| 109 | |
| 110 | return value; |
| 111 | } |
| 112 | |
| 113 | /* START: High -> Low on SDA while SCL is High */ |
Michael Auchter | 5f6b185 | 2020-02-07 10:55:31 -0600 | [diff] [blame] | 114 | static void i2c_gpio_send_start(struct i2c_gpio_bus *bus, int delay) |
Przemyslaw Marczak | d3aa7e1 | 2015-03-31 18:57:18 +0200 | [diff] [blame] | 115 | { |
| 116 | udelay(delay); |
Michael Auchter | 5f6b185 | 2020-02-07 10:55:31 -0600 | [diff] [blame] | 117 | bus->set_sda(bus, 1); |
Przemyslaw Marczak | d3aa7e1 | 2015-03-31 18:57:18 +0200 | [diff] [blame] | 118 | udelay(delay); |
Michael Auchter | 5f6b185 | 2020-02-07 10:55:31 -0600 | [diff] [blame] | 119 | bus->set_scl(bus, 1); |
Przemyslaw Marczak | d3aa7e1 | 2015-03-31 18:57:18 +0200 | [diff] [blame] | 120 | udelay(delay); |
Michael Auchter | 5f6b185 | 2020-02-07 10:55:31 -0600 | [diff] [blame] | 121 | bus->set_sda(bus, 0); |
Przemyslaw Marczak | d3aa7e1 | 2015-03-31 18:57:18 +0200 | [diff] [blame] | 122 | udelay(delay); |
| 123 | } |
| 124 | |
| 125 | /* STOP: Low -> High on SDA while SCL is High */ |
Michael Auchter | 5f6b185 | 2020-02-07 10:55:31 -0600 | [diff] [blame] | 126 | static void i2c_gpio_send_stop(struct i2c_gpio_bus *bus, int delay) |
Przemyslaw Marczak | d3aa7e1 | 2015-03-31 18:57:18 +0200 | [diff] [blame] | 127 | { |
Michael Auchter | 5f6b185 | 2020-02-07 10:55:31 -0600 | [diff] [blame] | 128 | bus->set_scl(bus, 0); |
Przemyslaw Marczak | d3aa7e1 | 2015-03-31 18:57:18 +0200 | [diff] [blame] | 129 | udelay(delay); |
Michael Auchter | 5f6b185 | 2020-02-07 10:55:31 -0600 | [diff] [blame] | 130 | bus->set_sda(bus, 0); |
Przemyslaw Marczak | d3aa7e1 | 2015-03-31 18:57:18 +0200 | [diff] [blame] | 131 | udelay(delay); |
Michael Auchter | 5f6b185 | 2020-02-07 10:55:31 -0600 | [diff] [blame] | 132 | bus->set_scl(bus, 1); |
Przemyslaw Marczak | d3aa7e1 | 2015-03-31 18:57:18 +0200 | [diff] [blame] | 133 | udelay(delay); |
Michael Auchter | 5f6b185 | 2020-02-07 10:55:31 -0600 | [diff] [blame] | 134 | bus->set_sda(bus, 1); |
Przemyslaw Marczak | d3aa7e1 | 2015-03-31 18:57:18 +0200 | [diff] [blame] | 135 | udelay(delay); |
| 136 | } |
| 137 | |
| 138 | /* ack should be I2C_ACK or I2C_NOACK */ |
Michael Auchter | 5f6b185 | 2020-02-07 10:55:31 -0600 | [diff] [blame] | 139 | static void i2c_gpio_send_ack(struct i2c_gpio_bus *bus, int delay, int ack) |
Przemyslaw Marczak | d3aa7e1 | 2015-03-31 18:57:18 +0200 | [diff] [blame] | 140 | { |
Michael Auchter | 5f6b185 | 2020-02-07 10:55:31 -0600 | [diff] [blame] | 141 | i2c_gpio_write_bit(bus, delay, ack); |
| 142 | bus->set_scl(bus, 0); |
Przemyslaw Marczak | d3aa7e1 | 2015-03-31 18:57:18 +0200 | [diff] [blame] | 143 | udelay(delay); |
| 144 | } |
| 145 | |
| 146 | /** |
| 147 | * Send a reset sequence consisting of 9 clocks with the data signal high |
| 148 | * to clock any confused device back into an idle state. Also send a |
| 149 | * <stop> at the end of the sequence for belts & suspenders. |
| 150 | */ |
Michael Auchter | 5f6b185 | 2020-02-07 10:55:31 -0600 | [diff] [blame] | 151 | static void i2c_gpio_send_reset(struct i2c_gpio_bus *bus, int delay) |
Przemyslaw Marczak | d3aa7e1 | 2015-03-31 18:57:18 +0200 | [diff] [blame] | 152 | { |
| 153 | int j; |
| 154 | |
| 155 | for (j = 0; j < 9; j++) |
Michael Auchter | 5f6b185 | 2020-02-07 10:55:31 -0600 | [diff] [blame] | 156 | i2c_gpio_write_bit(bus, delay, 1); |
Przemyslaw Marczak | d3aa7e1 | 2015-03-31 18:57:18 +0200 | [diff] [blame] | 157 | |
Michael Auchter | 5f6b185 | 2020-02-07 10:55:31 -0600 | [diff] [blame] | 158 | i2c_gpio_send_stop(bus, delay); |
Przemyslaw Marczak | d3aa7e1 | 2015-03-31 18:57:18 +0200 | [diff] [blame] | 159 | } |
| 160 | |
| 161 | /* Set sda high with low clock, before reading slave data */ |
Michael Auchter | 5f6b185 | 2020-02-07 10:55:31 -0600 | [diff] [blame] | 162 | static void i2c_gpio_sda_high(struct i2c_gpio_bus *bus, int delay) |
Przemyslaw Marczak | d3aa7e1 | 2015-03-31 18:57:18 +0200 | [diff] [blame] | 163 | { |
Michael Auchter | 5f6b185 | 2020-02-07 10:55:31 -0600 | [diff] [blame] | 164 | bus->set_scl(bus, 0); |
Przemyslaw Marczak | d3aa7e1 | 2015-03-31 18:57:18 +0200 | [diff] [blame] | 165 | udelay(delay); |
Michael Auchter | 5f6b185 | 2020-02-07 10:55:31 -0600 | [diff] [blame] | 166 | bus->set_sda(bus, 1); |
Przemyslaw Marczak | d3aa7e1 | 2015-03-31 18:57:18 +0200 | [diff] [blame] | 167 | udelay(delay); |
| 168 | } |
| 169 | |
| 170 | /* Send 8 bits and look for an acknowledgement */ |
Michael Auchter | 5f6b185 | 2020-02-07 10:55:31 -0600 | [diff] [blame] | 171 | static int i2c_gpio_write_byte(struct i2c_gpio_bus *bus, int delay, uchar data) |
Przemyslaw Marczak | d3aa7e1 | 2015-03-31 18:57:18 +0200 | [diff] [blame] | 172 | { |
| 173 | int j; |
| 174 | int nack; |
| 175 | |
| 176 | for (j = 0; j < 8; j++) { |
Michael Auchter | 5f6b185 | 2020-02-07 10:55:31 -0600 | [diff] [blame] | 177 | i2c_gpio_write_bit(bus, delay, data & 0x80); |
Przemyslaw Marczak | d3aa7e1 | 2015-03-31 18:57:18 +0200 | [diff] [blame] | 178 | data <<= 1; |
| 179 | } |
| 180 | |
| 181 | udelay(delay); |
| 182 | |
| 183 | /* Look for an <ACK>(negative logic) and return it */ |
Michael Auchter | 5f6b185 | 2020-02-07 10:55:31 -0600 | [diff] [blame] | 184 | i2c_gpio_sda_high(bus, delay); |
| 185 | nack = i2c_gpio_read_bit(bus, delay); |
Przemyslaw Marczak | d3aa7e1 | 2015-03-31 18:57:18 +0200 | [diff] [blame] | 186 | |
| 187 | return nack; /* not a nack is an ack */ |
| 188 | } |
| 189 | |
| 190 | /** |
| 191 | * if ack == I2C_ACK, ACK the byte so can continue reading, else |
| 192 | * send I2C_NOACK to end the read. |
| 193 | */ |
Michael Auchter | 5f6b185 | 2020-02-07 10:55:31 -0600 | [diff] [blame] | 194 | static uchar i2c_gpio_read_byte(struct i2c_gpio_bus *bus, int delay, int ack) |
Przemyslaw Marczak | d3aa7e1 | 2015-03-31 18:57:18 +0200 | [diff] [blame] | 195 | { |
| 196 | int data; |
| 197 | int j; |
| 198 | |
Michael Auchter | 5f6b185 | 2020-02-07 10:55:31 -0600 | [diff] [blame] | 199 | i2c_gpio_sda_high(bus, delay); |
Przemyslaw Marczak | d3aa7e1 | 2015-03-31 18:57:18 +0200 | [diff] [blame] | 200 | data = 0; |
| 201 | for (j = 0; j < 8; j++) { |
| 202 | data <<= 1; |
Michael Auchter | 5f6b185 | 2020-02-07 10:55:31 -0600 | [diff] [blame] | 203 | data |= i2c_gpio_read_bit(bus, delay); |
Przemyslaw Marczak | d3aa7e1 | 2015-03-31 18:57:18 +0200 | [diff] [blame] | 204 | } |
Michael Auchter | 5f6b185 | 2020-02-07 10:55:31 -0600 | [diff] [blame] | 205 | i2c_gpio_send_ack(bus, delay, ack); |
Przemyslaw Marczak | d3aa7e1 | 2015-03-31 18:57:18 +0200 | [diff] [blame] | 206 | |
| 207 | return data; |
| 208 | } |
| 209 | |
| 210 | /* send start and the slave chip address */ |
Michael Auchter | 5f6b185 | 2020-02-07 10:55:31 -0600 | [diff] [blame] | 211 | int i2c_send_slave_addr(struct i2c_gpio_bus *bus, int delay, uchar chip) |
Przemyslaw Marczak | d3aa7e1 | 2015-03-31 18:57:18 +0200 | [diff] [blame] | 212 | { |
Michael Auchter | 5f6b185 | 2020-02-07 10:55:31 -0600 | [diff] [blame] | 213 | i2c_gpio_send_start(bus, delay); |
Przemyslaw Marczak | d3aa7e1 | 2015-03-31 18:57:18 +0200 | [diff] [blame] | 214 | |
Michael Auchter | 5f6b185 | 2020-02-07 10:55:31 -0600 | [diff] [blame] | 215 | if (i2c_gpio_write_byte(bus, delay, chip)) { |
| 216 | i2c_gpio_send_stop(bus, delay); |
Przemyslaw Marczak | d3aa7e1 | 2015-03-31 18:57:18 +0200 | [diff] [blame] | 217 | return -EIO; |
| 218 | } |
| 219 | |
| 220 | return 0; |
| 221 | } |
| 222 | |
| 223 | static int i2c_gpio_write_data(struct i2c_gpio_bus *bus, uchar chip, |
| 224 | uchar *buffer, int len, |
| 225 | bool end_with_repeated_start) |
| 226 | { |
Przemyslaw Marczak | d3aa7e1 | 2015-03-31 18:57:18 +0200 | [diff] [blame] | 227 | unsigned int delay = bus->udelay; |
| 228 | int failures = 0; |
| 229 | |
| 230 | debug("%s: chip %x buffer %p len %d\n", __func__, chip, buffer, len); |
| 231 | |
Michael Auchter | 5f6b185 | 2020-02-07 10:55:31 -0600 | [diff] [blame] | 232 | if (i2c_send_slave_addr(bus, delay, chip << 1)) { |
Przemyslaw Marczak | d3aa7e1 | 2015-03-31 18:57:18 +0200 | [diff] [blame] | 233 | debug("i2c_write, no chip responded %02X\n", chip); |
| 234 | return -EIO; |
| 235 | } |
| 236 | |
| 237 | while (len-- > 0) { |
Michael Auchter | 5f6b185 | 2020-02-07 10:55:31 -0600 | [diff] [blame] | 238 | if (i2c_gpio_write_byte(bus, delay, *buffer++)) |
Przemyslaw Marczak | d3aa7e1 | 2015-03-31 18:57:18 +0200 | [diff] [blame] | 239 | failures++; |
| 240 | } |
| 241 | |
| 242 | if (!end_with_repeated_start) { |
Michael Auchter | 5f6b185 | 2020-02-07 10:55:31 -0600 | [diff] [blame] | 243 | i2c_gpio_send_stop(bus, delay); |
Przemyslaw Marczak | d3aa7e1 | 2015-03-31 18:57:18 +0200 | [diff] [blame] | 244 | return failures; |
| 245 | } |
| 246 | |
Michael Auchter | 5f6b185 | 2020-02-07 10:55:31 -0600 | [diff] [blame] | 247 | if (i2c_send_slave_addr(bus, delay, (chip << 1) | 0x1)) { |
Przemyslaw Marczak | d3aa7e1 | 2015-03-31 18:57:18 +0200 | [diff] [blame] | 248 | debug("i2c_write, no chip responded %02X\n", chip); |
| 249 | return -EIO; |
| 250 | } |
| 251 | |
| 252 | return failures; |
| 253 | } |
| 254 | |
| 255 | static int i2c_gpio_read_data(struct i2c_gpio_bus *bus, uchar chip, |
| 256 | uchar *buffer, int len) |
| 257 | { |
Przemyslaw Marczak | d3aa7e1 | 2015-03-31 18:57:18 +0200 | [diff] [blame] | 258 | unsigned int delay = bus->udelay; |
| 259 | |
| 260 | debug("%s: chip %x buffer: %p len %d\n", __func__, chip, buffer, len); |
| 261 | |
| 262 | while (len-- > 0) |
Michael Auchter | 5f6b185 | 2020-02-07 10:55:31 -0600 | [diff] [blame] | 263 | *buffer++ = i2c_gpio_read_byte(bus, delay, len == 0); |
Przemyslaw Marczak | d3aa7e1 | 2015-03-31 18:57:18 +0200 | [diff] [blame] | 264 | |
Michael Auchter | 5f6b185 | 2020-02-07 10:55:31 -0600 | [diff] [blame] | 265 | i2c_gpio_send_stop(bus, delay); |
Przemyslaw Marczak | d3aa7e1 | 2015-03-31 18:57:18 +0200 | [diff] [blame] | 266 | |
| 267 | return 0; |
| 268 | } |
| 269 | |
| 270 | static int i2c_gpio_xfer(struct udevice *dev, struct i2c_msg *msg, int nmsgs) |
| 271 | { |
| 272 | struct i2c_gpio_bus *bus = dev_get_priv(dev); |
| 273 | int ret; |
| 274 | |
| 275 | for (; nmsgs > 0; nmsgs--, msg++) { |
| 276 | bool next_is_read = nmsgs > 1 && (msg[1].flags & I2C_M_RD); |
| 277 | |
| 278 | if (msg->flags & I2C_M_RD) { |
| 279 | ret = i2c_gpio_read_data(bus, msg->addr, msg->buf, |
| 280 | msg->len); |
| 281 | } else { |
| 282 | ret = i2c_gpio_write_data(bus, msg->addr, msg->buf, |
| 283 | msg->len, next_is_read); |
| 284 | } |
| 285 | |
| 286 | if (ret) |
| 287 | return -EREMOTEIO; |
| 288 | } |
| 289 | |
| 290 | return 0; |
| 291 | } |
| 292 | |
| 293 | static int i2c_gpio_probe(struct udevice *dev, uint chip, uint chip_flags) |
| 294 | { |
| 295 | struct i2c_gpio_bus *bus = dev_get_priv(dev); |
Przemyslaw Marczak | d3aa7e1 | 2015-03-31 18:57:18 +0200 | [diff] [blame] | 296 | unsigned int delay = bus->udelay; |
| 297 | int ret; |
| 298 | |
Michael Auchter | 5f6b185 | 2020-02-07 10:55:31 -0600 | [diff] [blame] | 299 | i2c_gpio_send_start(bus, delay); |
| 300 | ret = i2c_gpio_write_byte(bus, delay, (chip << 1) | 0); |
| 301 | i2c_gpio_send_stop(bus, delay); |
Przemyslaw Marczak | d3aa7e1 | 2015-03-31 18:57:18 +0200 | [diff] [blame] | 302 | |
| 303 | debug("%s: bus: %d (%s) chip: %x flags: %x ret: %d\n", |
Simon Glass | 75e534b | 2020-12-16 21:20:07 -0700 | [diff] [blame] | 304 | __func__, dev_seq(dev), dev->name, chip, chip_flags, ret); |
Przemyslaw Marczak | d3aa7e1 | 2015-03-31 18:57:18 +0200 | [diff] [blame] | 305 | |
| 306 | return ret; |
| 307 | } |
| 308 | |
| 309 | static int i2c_gpio_set_bus_speed(struct udevice *dev, unsigned int speed_hz) |
| 310 | { |
| 311 | struct i2c_gpio_bus *bus = dev_get_priv(dev); |
Przemyslaw Marczak | d3aa7e1 | 2015-03-31 18:57:18 +0200 | [diff] [blame] | 312 | |
| 313 | bus->udelay = 1000000 / (speed_hz << 2); |
| 314 | |
Michael Auchter | 5f6b185 | 2020-02-07 10:55:31 -0600 | [diff] [blame] | 315 | i2c_gpio_send_reset(bus, bus->udelay); |
Przemyslaw Marczak | d3aa7e1 | 2015-03-31 18:57:18 +0200 | [diff] [blame] | 316 | |
| 317 | return 0; |
| 318 | } |
| 319 | |
Marek Vasut | 9f52cc4 | 2020-02-07 16:57:52 +0100 | [diff] [blame] | 320 | static int i2c_gpio_drv_probe(struct udevice *dev) |
| 321 | { |
| 322 | if (dev_read_bool(dev, "i2c-gpio,deblock")) { |
| 323 | /* @200kHz 9 clocks = 44us, 62us is ok */ |
| 324 | const unsigned int DELAY_ABORT_SEQ = 62; |
| 325 | struct i2c_gpio_bus *bus = dev_get_priv(dev); |
| 326 | |
| 327 | return i2c_deblock_gpio_loop(&bus->gpios[PIN_SDA], |
| 328 | &bus->gpios[PIN_SCL], |
| 329 | 16, 5, DELAY_ABORT_SEQ); |
| 330 | } |
| 331 | |
| 332 | return 0; |
| 333 | } |
| 334 | |
Simon Glass | aad29ae | 2020-12-03 16:55:21 -0700 | [diff] [blame] | 335 | static int i2c_gpio_of_to_plat(struct udevice *dev) |
Przemyslaw Marczak | d3aa7e1 | 2015-03-31 18:57:18 +0200 | [diff] [blame] | 336 | { |
| 337 | struct i2c_gpio_bus *bus = dev_get_priv(dev); |
Przemyslaw Marczak | d3aa7e1 | 2015-03-31 18:57:18 +0200 | [diff] [blame] | 338 | int ret; |
| 339 | |
| 340 | ret = gpio_request_list_by_name(dev, "gpios", bus->gpios, |
| 341 | ARRAY_SIZE(bus->gpios), 0); |
| 342 | if (ret < 0) |
| 343 | goto error; |
| 344 | |
Patrick Delaunay | 008573b | 2020-09-10 18:25:09 +0200 | [diff] [blame] | 345 | bus->udelay = dev_read_u32_default(dev, "i2c-gpio,delay-us", |
| 346 | DEFAULT_UDELAY); |
Przemyslaw Marczak | d3aa7e1 | 2015-03-31 18:57:18 +0200 | [diff] [blame] | 347 | |
Michael Auchter | 5f6b185 | 2020-02-07 10:55:31 -0600 | [diff] [blame] | 348 | bus->get_sda = i2c_gpio_sda_get; |
| 349 | bus->set_sda = i2c_gpio_sda_set; |
Patrick Delaunay | 008573b | 2020-09-10 18:25:09 +0200 | [diff] [blame] | 350 | if (dev_read_bool(dev, "i2c-gpio,scl-output-only")) |
Heiko Schocher | 48f39d8 | 2020-03-16 07:55:06 +0100 | [diff] [blame] | 351 | bus->set_scl = i2c_gpio_scl_set_output_only; |
| 352 | else |
| 353 | bus->set_scl = i2c_gpio_scl_set; |
Michael Auchter | 5f6b185 | 2020-02-07 10:55:31 -0600 | [diff] [blame] | 354 | |
Przemyslaw Marczak | d3aa7e1 | 2015-03-31 18:57:18 +0200 | [diff] [blame] | 355 | return 0; |
| 356 | error: |
Masahiro Yamada | 81e1042 | 2017-09-16 14:10:41 +0900 | [diff] [blame] | 357 | pr_err("Can't get %s gpios! Error: %d", dev->name, ret); |
Przemyslaw Marczak | d3aa7e1 | 2015-03-31 18:57:18 +0200 | [diff] [blame] | 358 | return ret; |
| 359 | } |
| 360 | |
| 361 | static const struct dm_i2c_ops i2c_gpio_ops = { |
| 362 | .xfer = i2c_gpio_xfer, |
| 363 | .probe_chip = i2c_gpio_probe, |
| 364 | .set_bus_speed = i2c_gpio_set_bus_speed, |
| 365 | }; |
| 366 | |
| 367 | static const struct udevice_id i2c_gpio_ids[] = { |
| 368 | { .compatible = "i2c-gpio" }, |
| 369 | { } |
| 370 | }; |
| 371 | |
| 372 | U_BOOT_DRIVER(i2c_gpio) = { |
| 373 | .name = "i2c-gpio", |
| 374 | .id = UCLASS_I2C, |
| 375 | .of_match = i2c_gpio_ids, |
Marek Vasut | 9f52cc4 | 2020-02-07 16:57:52 +0100 | [diff] [blame] | 376 | .probe = i2c_gpio_drv_probe, |
Simon Glass | aad29ae | 2020-12-03 16:55:21 -0700 | [diff] [blame] | 377 | .of_to_plat = i2c_gpio_of_to_plat, |
Simon Glass | 8a2b47f | 2020-12-03 16:55:17 -0700 | [diff] [blame] | 378 | .priv_auto = sizeof(struct i2c_gpio_bus), |
Przemyslaw Marczak | d3aa7e1 | 2015-03-31 18:57:18 +0200 | [diff] [blame] | 379 | .ops = &i2c_gpio_ops, |
| 380 | }; |