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