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