Tom Rini | 10e4779 | 2018-05-06 17:58:06 -0400 | [diff] [blame] | 1 | // SPDX-License-Identifier: GPL-2.0+ |
Simon Glass | c7a5890 | 2014-12-10 08:55:47 -0700 | [diff] [blame] | 2 | /* |
| 3 | * Copyright (c) 2014 Google, Inc |
Simon Glass | c7a5890 | 2014-12-10 08:55:47 -0700 | [diff] [blame] | 4 | */ |
| 5 | |
| 6 | #include <common.h> |
| 7 | #include <dm.h> |
| 8 | #include <errno.h> |
Simon Glass | c7a5890 | 2014-12-10 08:55:47 -0700 | [diff] [blame] | 9 | #include <i2c.h> |
| 10 | #include <malloc.h> |
| 11 | #include <dm/device-internal.h> |
| 12 | #include <dm/lists.h> |
Alexander Kochetkov | dc96eb7 | 2018-03-27 17:52:27 +0300 | [diff] [blame] | 13 | #include <dm/pinctrl.h> |
Simon Glass | fa4689a | 2019-12-06 21:41:35 -0700 | [diff] [blame] | 14 | #if CONFIG_IS_ENABLED(DM_GPIO) |
Alexander Kochetkov | dc96eb7 | 2018-03-27 17:52:27 +0300 | [diff] [blame] | 15 | #include <asm/gpio.h> |
| 16 | #endif |
Simon Glass | c7a5890 | 2014-12-10 08:55:47 -0700 | [diff] [blame] | 17 | |
Simon Glass | c7a5890 | 2014-12-10 08:55:47 -0700 | [diff] [blame] | 18 | #define I2C_MAX_OFFSET_LEN 4 |
| 19 | |
Alexander Kochetkov | dc96eb7 | 2018-03-27 17:52:27 +0300 | [diff] [blame] | 20 | enum { |
| 21 | PIN_SDA = 0, |
| 22 | PIN_SCL, |
| 23 | PIN_COUNT, |
| 24 | }; |
| 25 | |
Simon Glass | a8e64c4 | 2015-07-02 18:15:39 -0600 | [diff] [blame] | 26 | /* Useful debugging function */ |
| 27 | void i2c_dump_msgs(struct i2c_msg *msg, int nmsgs) |
| 28 | { |
| 29 | int i; |
| 30 | |
| 31 | for (i = 0; i < nmsgs; i++) { |
| 32 | struct i2c_msg *m = &msg[i]; |
| 33 | |
| 34 | printf(" %s %x len=%x", m->flags & I2C_M_RD ? "R" : "W", |
| 35 | msg->addr, msg->len); |
| 36 | if (!(m->flags & I2C_M_RD)) |
| 37 | printf(": %x", m->buf[0]); |
| 38 | printf("\n"); |
| 39 | } |
| 40 | } |
| 41 | |
Simon Glass | c7a5890 | 2014-12-10 08:55:47 -0700 | [diff] [blame] | 42 | /** |
| 43 | * i2c_setup_offset() - Set up a new message with a chip offset |
| 44 | * |
| 45 | * @chip: Chip to use |
| 46 | * @offset: Byte offset within chip |
| 47 | * @offset_buf: Place to put byte offset |
| 48 | * @msg: Message buffer |
| 49 | * @return 0 if OK, -EADDRNOTAVAIL if the offset length is 0. In that case the |
| 50 | * message is still set up but will not contain an offset. |
| 51 | */ |
| 52 | static int i2c_setup_offset(struct dm_i2c_chip *chip, uint offset, |
| 53 | uint8_t offset_buf[], struct i2c_msg *msg) |
| 54 | { |
Robert Beckett | 5f8e41a | 2019-10-28 17:44:57 +0000 | [diff] [blame] | 55 | int offset_len = chip->offset_len; |
Simon Glass | c7a5890 | 2014-12-10 08:55:47 -0700 | [diff] [blame] | 56 | |
| 57 | msg->addr = chip->chip_addr; |
Robert Beckett | 5f8e41a | 2019-10-28 17:44:57 +0000 | [diff] [blame] | 58 | if (chip->chip_addr_offset_mask) |
| 59 | msg->addr |= (offset >> (8 * offset_len)) & |
| 60 | chip->chip_addr_offset_mask; |
Simon Glass | c7a5890 | 2014-12-10 08:55:47 -0700 | [diff] [blame] | 61 | msg->flags = chip->flags & DM_I2C_CHIP_10BIT ? I2C_M_TEN : 0; |
| 62 | msg->len = chip->offset_len; |
| 63 | msg->buf = offset_buf; |
Robert Beckett | 5f8e41a | 2019-10-28 17:44:57 +0000 | [diff] [blame] | 64 | if (!offset_len) |
Simon Glass | c7a5890 | 2014-12-10 08:55:47 -0700 | [diff] [blame] | 65 | return -EADDRNOTAVAIL; |
Robert Beckett | 5f8e41a | 2019-10-28 17:44:57 +0000 | [diff] [blame] | 66 | assert(offset_len <= I2C_MAX_OFFSET_LEN); |
| 67 | |
Simon Glass | c7a5890 | 2014-12-10 08:55:47 -0700 | [diff] [blame] | 68 | while (offset_len--) |
| 69 | *offset_buf++ = offset >> (8 * offset_len); |
| 70 | |
| 71 | return 0; |
| 72 | } |
| 73 | |
| 74 | static int i2c_read_bytewise(struct udevice *dev, uint offset, |
| 75 | uint8_t *buffer, int len) |
| 76 | { |
Simon Glass | 713c3f0 | 2015-01-25 08:27:13 -0700 | [diff] [blame] | 77 | struct dm_i2c_chip *chip = dev_get_parent_platdata(dev); |
Simon Glass | c7a5890 | 2014-12-10 08:55:47 -0700 | [diff] [blame] | 78 | struct udevice *bus = dev_get_parent(dev); |
| 79 | struct dm_i2c_ops *ops = i2c_get_ops(bus); |
| 80 | struct i2c_msg msg[2], *ptr; |
| 81 | uint8_t offset_buf[I2C_MAX_OFFSET_LEN]; |
| 82 | int ret; |
| 83 | int i; |
| 84 | |
| 85 | for (i = 0; i < len; i++) { |
| 86 | if (i2c_setup_offset(chip, offset + i, offset_buf, msg)) |
| 87 | return -EINVAL; |
| 88 | ptr = msg + 1; |
Robert Beckett | 5f8e41a | 2019-10-28 17:44:57 +0000 | [diff] [blame] | 89 | ptr->addr = msg->addr; |
Simon Glass | c7a5890 | 2014-12-10 08:55:47 -0700 | [diff] [blame] | 90 | ptr->flags = msg->flags | I2C_M_RD; |
| 91 | ptr->len = 1; |
| 92 | ptr->buf = &buffer[i]; |
| 93 | ptr++; |
| 94 | |
| 95 | ret = ops->xfer(bus, msg, ptr - msg); |
| 96 | if (ret) |
| 97 | return ret; |
| 98 | } |
| 99 | |
| 100 | return 0; |
| 101 | } |
| 102 | |
| 103 | static int i2c_write_bytewise(struct udevice *dev, uint offset, |
| 104 | const uint8_t *buffer, int len) |
| 105 | { |
Simon Glass | 713c3f0 | 2015-01-25 08:27:13 -0700 | [diff] [blame] | 106 | struct dm_i2c_chip *chip = dev_get_parent_platdata(dev); |
Simon Glass | c7a5890 | 2014-12-10 08:55:47 -0700 | [diff] [blame] | 107 | struct udevice *bus = dev_get_parent(dev); |
| 108 | struct dm_i2c_ops *ops = i2c_get_ops(bus); |
| 109 | struct i2c_msg msg[1]; |
| 110 | uint8_t buf[I2C_MAX_OFFSET_LEN + 1]; |
| 111 | int ret; |
| 112 | int i; |
| 113 | |
| 114 | for (i = 0; i < len; i++) { |
| 115 | if (i2c_setup_offset(chip, offset + i, buf, msg)) |
| 116 | return -EINVAL; |
| 117 | buf[msg->len++] = buffer[i]; |
| 118 | |
| 119 | ret = ops->xfer(bus, msg, 1); |
| 120 | if (ret) |
| 121 | return ret; |
| 122 | } |
| 123 | |
| 124 | return 0; |
| 125 | } |
| 126 | |
Simon Glass | 7d72276 | 2015-01-12 18:02:07 -0700 | [diff] [blame] | 127 | int dm_i2c_read(struct udevice *dev, uint offset, uint8_t *buffer, int len) |
Simon Glass | c7a5890 | 2014-12-10 08:55:47 -0700 | [diff] [blame] | 128 | { |
Simon Glass | 713c3f0 | 2015-01-25 08:27:13 -0700 | [diff] [blame] | 129 | struct dm_i2c_chip *chip = dev_get_parent_platdata(dev); |
Simon Glass | c7a5890 | 2014-12-10 08:55:47 -0700 | [diff] [blame] | 130 | struct udevice *bus = dev_get_parent(dev); |
| 131 | struct dm_i2c_ops *ops = i2c_get_ops(bus); |
| 132 | struct i2c_msg msg[2], *ptr; |
| 133 | uint8_t offset_buf[I2C_MAX_OFFSET_LEN]; |
| 134 | int msg_count; |
| 135 | |
| 136 | if (!ops->xfer) |
| 137 | return -ENOSYS; |
| 138 | if (chip->flags & DM_I2C_CHIP_RD_ADDRESS) |
| 139 | return i2c_read_bytewise(dev, offset, buffer, len); |
| 140 | ptr = msg; |
| 141 | if (!i2c_setup_offset(chip, offset, offset_buf, ptr)) |
| 142 | ptr++; |
| 143 | |
| 144 | if (len) { |
Robert Beckett | 5f8e41a | 2019-10-28 17:44:57 +0000 | [diff] [blame] | 145 | ptr->addr = msg->addr; |
Simon Glass | c7a5890 | 2014-12-10 08:55:47 -0700 | [diff] [blame] | 146 | ptr->flags = chip->flags & DM_I2C_CHIP_10BIT ? I2C_M_TEN : 0; |
| 147 | ptr->flags |= I2C_M_RD; |
| 148 | ptr->len = len; |
| 149 | ptr->buf = buffer; |
| 150 | ptr++; |
| 151 | } |
| 152 | msg_count = ptr - msg; |
| 153 | |
| 154 | return ops->xfer(bus, msg, msg_count); |
| 155 | } |
| 156 | |
Simon Glass | 7d72276 | 2015-01-12 18:02:07 -0700 | [diff] [blame] | 157 | int dm_i2c_write(struct udevice *dev, uint offset, const uint8_t *buffer, |
| 158 | int len) |
Simon Glass | c7a5890 | 2014-12-10 08:55:47 -0700 | [diff] [blame] | 159 | { |
Simon Glass | 713c3f0 | 2015-01-25 08:27:13 -0700 | [diff] [blame] | 160 | struct dm_i2c_chip *chip = dev_get_parent_platdata(dev); |
Simon Glass | c7a5890 | 2014-12-10 08:55:47 -0700 | [diff] [blame] | 161 | struct udevice *bus = dev_get_parent(dev); |
| 162 | struct dm_i2c_ops *ops = i2c_get_ops(bus); |
| 163 | struct i2c_msg msg[1]; |
| 164 | |
| 165 | if (!ops->xfer) |
| 166 | return -ENOSYS; |
| 167 | |
| 168 | if (chip->flags & DM_I2C_CHIP_WR_ADDRESS) |
| 169 | return i2c_write_bytewise(dev, offset, buffer, len); |
| 170 | /* |
| 171 | * The simple approach would be to send two messages here: one to |
| 172 | * set the offset and one to write the bytes. However some drivers |
| 173 | * will not be expecting this, and some chips won't like how the |
| 174 | * driver presents this on the I2C bus. |
| 175 | * |
| 176 | * The API does not support separate offset and data. We could extend |
| 177 | * it with a flag indicating that there is data in the next message |
| 178 | * that needs to be processed in the same transaction. We could |
| 179 | * instead add an additional buffer to each message. For now, handle |
| 180 | * this in the uclass since it isn't clear what the impact on drivers |
| 181 | * would be with this extra complication. Unfortunately this means |
| 182 | * copying the message. |
| 183 | * |
| 184 | * Use the stack for small messages, malloc() for larger ones. We |
| 185 | * need to allow space for the offset (up to 4 bytes) and the message |
| 186 | * itself. |
| 187 | */ |
| 188 | if (len < 64) { |
| 189 | uint8_t buf[I2C_MAX_OFFSET_LEN + len]; |
| 190 | |
| 191 | i2c_setup_offset(chip, offset, buf, msg); |
| 192 | msg->len += len; |
| 193 | memcpy(buf + chip->offset_len, buffer, len); |
| 194 | |
| 195 | return ops->xfer(bus, msg, 1); |
| 196 | } else { |
| 197 | uint8_t *buf; |
| 198 | int ret; |
| 199 | |
| 200 | buf = malloc(I2C_MAX_OFFSET_LEN + len); |
| 201 | if (!buf) |
| 202 | return -ENOMEM; |
| 203 | i2c_setup_offset(chip, offset, buf, msg); |
| 204 | msg->len += len; |
| 205 | memcpy(buf + chip->offset_len, buffer, len); |
| 206 | |
| 207 | ret = ops->xfer(bus, msg, 1); |
| 208 | free(buf); |
| 209 | return ret; |
| 210 | } |
| 211 | } |
| 212 | |
Simon Glass | f281885 | 2015-07-02 18:15:42 -0600 | [diff] [blame] | 213 | int dm_i2c_xfer(struct udevice *dev, struct i2c_msg *msg, int nmsgs) |
| 214 | { |
| 215 | struct udevice *bus = dev_get_parent(dev); |
| 216 | struct dm_i2c_ops *ops = i2c_get_ops(bus); |
| 217 | |
| 218 | if (!ops->xfer) |
| 219 | return -ENOSYS; |
| 220 | |
| 221 | return ops->xfer(bus, msg, nmsgs); |
| 222 | } |
| 223 | |
Simon Glass | 0c70261 | 2015-04-20 12:37:14 -0600 | [diff] [blame] | 224 | int dm_i2c_reg_read(struct udevice *dev, uint offset) |
| 225 | { |
| 226 | uint8_t val; |
| 227 | int ret; |
| 228 | |
| 229 | ret = dm_i2c_read(dev, offset, &val, 1); |
| 230 | if (ret < 0) |
| 231 | return ret; |
| 232 | |
| 233 | return val; |
| 234 | } |
| 235 | |
| 236 | int dm_i2c_reg_write(struct udevice *dev, uint offset, uint value) |
| 237 | { |
| 238 | uint8_t val = value; |
| 239 | |
| 240 | return dm_i2c_write(dev, offset, &val, 1); |
| 241 | } |
| 242 | |
Simon Glass | c7a5890 | 2014-12-10 08:55:47 -0700 | [diff] [blame] | 243 | /** |
| 244 | * i2c_probe_chip() - probe for a chip on a bus |
| 245 | * |
| 246 | * @bus: Bus to probe |
| 247 | * @chip_addr: Chip address to probe |
| 248 | * @flags: Flags for the chip |
| 249 | * @return 0 if found, -ENOSYS if the driver is invalid, -EREMOTEIO if the chip |
| 250 | * does not respond to probe |
| 251 | */ |
| 252 | static int i2c_probe_chip(struct udevice *bus, uint chip_addr, |
| 253 | enum dm_i2c_chip_flags chip_flags) |
| 254 | { |
| 255 | struct dm_i2c_ops *ops = i2c_get_ops(bus); |
| 256 | struct i2c_msg msg[1]; |
| 257 | int ret; |
| 258 | |
| 259 | if (ops->probe_chip) { |
| 260 | ret = ops->probe_chip(bus, chip_addr, chip_flags); |
| 261 | if (!ret || ret != -ENOSYS) |
| 262 | return ret; |
| 263 | } |
| 264 | |
| 265 | if (!ops->xfer) |
| 266 | return -ENOSYS; |
| 267 | |
| 268 | /* Probe with a zero-length message */ |
| 269 | msg->addr = chip_addr; |
| 270 | msg->flags = chip_flags & DM_I2C_CHIP_10BIT ? I2C_M_TEN : 0; |
| 271 | msg->len = 0; |
| 272 | msg->buf = NULL; |
| 273 | |
| 274 | return ops->xfer(bus, msg, 1); |
| 275 | } |
| 276 | |
Simon Glass | a2723ae | 2015-01-25 08:26:55 -0700 | [diff] [blame] | 277 | static int i2c_bind_driver(struct udevice *bus, uint chip_addr, uint offset_len, |
Simon Glass | c7a5890 | 2014-12-10 08:55:47 -0700 | [diff] [blame] | 278 | struct udevice **devp) |
| 279 | { |
Simon Glass | 713c3f0 | 2015-01-25 08:27:13 -0700 | [diff] [blame] | 280 | struct dm_i2c_chip *chip; |
Simon Glass | c7a5890 | 2014-12-10 08:55:47 -0700 | [diff] [blame] | 281 | char name[30], *str; |
| 282 | struct udevice *dev; |
| 283 | int ret; |
| 284 | |
| 285 | snprintf(name, sizeof(name), "generic_%x", chip_addr); |
| 286 | str = strdup(name); |
Simon Glass | b75c449 | 2015-02-18 14:10:28 -0700 | [diff] [blame] | 287 | if (!str) |
| 288 | return -ENOMEM; |
Simon Glass | c7a5890 | 2014-12-10 08:55:47 -0700 | [diff] [blame] | 289 | ret = device_bind_driver(bus, "i2c_generic_chip_drv", str, &dev); |
| 290 | debug("%s: device_bind_driver: ret=%d\n", __func__, ret); |
| 291 | if (ret) |
| 292 | goto err_bind; |
| 293 | |
| 294 | /* Tell the device what we know about it */ |
Simon Glass | 713c3f0 | 2015-01-25 08:27:13 -0700 | [diff] [blame] | 295 | chip = dev_get_parent_platdata(dev); |
| 296 | chip->chip_addr = chip_addr; |
| 297 | chip->offset_len = offset_len; |
| 298 | ret = device_probe(dev); |
| 299 | debug("%s: device_probe: ret=%d\n", __func__, ret); |
Simon Glass | c7a5890 | 2014-12-10 08:55:47 -0700 | [diff] [blame] | 300 | if (ret) |
| 301 | goto err_probe; |
| 302 | |
| 303 | *devp = dev; |
| 304 | return 0; |
| 305 | |
| 306 | err_probe: |
Simon Glass | 713c3f0 | 2015-01-25 08:27:13 -0700 | [diff] [blame] | 307 | /* |
| 308 | * If the device failed to probe, unbind it. There is nothing there |
| 309 | * on the bus so we don't want to leave it lying around |
| 310 | */ |
Simon Glass | c7a5890 | 2014-12-10 08:55:47 -0700 | [diff] [blame] | 311 | device_unbind(dev); |
| 312 | err_bind: |
| 313 | free(str); |
| 314 | return ret; |
| 315 | } |
| 316 | |
Simon Glass | a2723ae | 2015-01-25 08:26:55 -0700 | [diff] [blame] | 317 | int i2c_get_chip(struct udevice *bus, uint chip_addr, uint offset_len, |
| 318 | struct udevice **devp) |
Simon Glass | c7a5890 | 2014-12-10 08:55:47 -0700 | [diff] [blame] | 319 | { |
| 320 | struct udevice *dev; |
| 321 | |
| 322 | debug("%s: Searching bus '%s' for address %02x: ", __func__, |
| 323 | bus->name, chip_addr); |
| 324 | for (device_find_first_child(bus, &dev); dev; |
| 325 | device_find_next_child(&dev)) { |
Simon Glass | 713c3f0 | 2015-01-25 08:27:13 -0700 | [diff] [blame] | 326 | struct dm_i2c_chip *chip = dev_get_parent_platdata(dev); |
Simon Glass | c7a5890 | 2014-12-10 08:55:47 -0700 | [diff] [blame] | 327 | int ret; |
| 328 | |
Robert Beckett | 5f8e41a | 2019-10-28 17:44:57 +0000 | [diff] [blame] | 329 | if (chip->chip_addr == (chip_addr & |
| 330 | ~chip->chip_addr_offset_mask)) { |
Simon Glass | c7a5890 | 2014-12-10 08:55:47 -0700 | [diff] [blame] | 331 | ret = device_probe(dev); |
| 332 | debug("found, ret=%d\n", ret); |
| 333 | if (ret) |
| 334 | return ret; |
| 335 | *devp = dev; |
| 336 | return 0; |
| 337 | } |
| 338 | } |
| 339 | debug("not found\n"); |
Simon Glass | a2723ae | 2015-01-25 08:26:55 -0700 | [diff] [blame] | 340 | return i2c_bind_driver(bus, chip_addr, offset_len, devp); |
Simon Glass | c7a5890 | 2014-12-10 08:55:47 -0700 | [diff] [blame] | 341 | } |
| 342 | |
Simon Glass | a2723ae | 2015-01-25 08:26:55 -0700 | [diff] [blame] | 343 | int i2c_get_chip_for_busnum(int busnum, int chip_addr, uint offset_len, |
| 344 | struct udevice **devp) |
Simon Glass | c7a5890 | 2014-12-10 08:55:47 -0700 | [diff] [blame] | 345 | { |
| 346 | struct udevice *bus; |
| 347 | int ret; |
| 348 | |
| 349 | ret = uclass_get_device_by_seq(UCLASS_I2C, busnum, &bus); |
| 350 | if (ret) { |
| 351 | debug("Cannot find I2C bus %d\n", busnum); |
| 352 | return ret; |
| 353 | } |
Jean-Jacques Hiblot | ac9ab24 | 2018-12-07 14:50:38 +0100 | [diff] [blame] | 354 | |
| 355 | /* detect the presence of the chip on the bus */ |
| 356 | ret = i2c_probe_chip(bus, chip_addr, 0); |
| 357 | debug("%s: bus='%s', address %02x, ret=%d\n", __func__, bus->name, |
| 358 | chip_addr, ret); |
| 359 | if (ret) { |
| 360 | debug("Cannot detect I2C chip %02x on bus %d\n", chip_addr, |
| 361 | busnum); |
| 362 | return ret; |
| 363 | } |
| 364 | |
Simon Glass | a2723ae | 2015-01-25 08:26:55 -0700 | [diff] [blame] | 365 | ret = i2c_get_chip(bus, chip_addr, offset_len, devp); |
Simon Glass | c7a5890 | 2014-12-10 08:55:47 -0700 | [diff] [blame] | 366 | if (ret) { |
| 367 | debug("Cannot find I2C chip %02x on bus %d\n", chip_addr, |
| 368 | busnum); |
| 369 | return ret; |
| 370 | } |
| 371 | |
| 372 | return 0; |
| 373 | } |
| 374 | |
Simon Glass | 7d72276 | 2015-01-12 18:02:07 -0700 | [diff] [blame] | 375 | int dm_i2c_probe(struct udevice *bus, uint chip_addr, uint chip_flags, |
| 376 | struct udevice **devp) |
Simon Glass | c7a5890 | 2014-12-10 08:55:47 -0700 | [diff] [blame] | 377 | { |
| 378 | int ret; |
| 379 | |
| 380 | *devp = NULL; |
| 381 | |
| 382 | /* First probe that chip */ |
| 383 | ret = i2c_probe_chip(bus, chip_addr, chip_flags); |
| 384 | debug("%s: bus='%s', address %02x, ret=%d\n", __func__, bus->name, |
| 385 | chip_addr, ret); |
| 386 | if (ret) |
| 387 | return ret; |
| 388 | |
| 389 | /* The chip was found, see if we have a driver, and probe it */ |
Simon Glass | a2723ae | 2015-01-25 08:26:55 -0700 | [diff] [blame] | 390 | ret = i2c_get_chip(bus, chip_addr, 1, devp); |
Simon Glass | c7a5890 | 2014-12-10 08:55:47 -0700 | [diff] [blame] | 391 | debug("%s: i2c_get_chip: ret=%d\n", __func__, ret); |
| 392 | |
| 393 | return ret; |
| 394 | } |
| 395 | |
Simon Glass | e4e8ff2 | 2015-02-05 21:41:32 -0700 | [diff] [blame] | 396 | int dm_i2c_set_bus_speed(struct udevice *bus, unsigned int speed) |
Simon Glass | c7a5890 | 2014-12-10 08:55:47 -0700 | [diff] [blame] | 397 | { |
| 398 | struct dm_i2c_ops *ops = i2c_get_ops(bus); |
Simon Glass | de0977b | 2015-03-05 12:25:20 -0700 | [diff] [blame] | 399 | struct dm_i2c_bus *i2c = dev_get_uclass_priv(bus); |
Simon Glass | c7a5890 | 2014-12-10 08:55:47 -0700 | [diff] [blame] | 400 | int ret; |
| 401 | |
| 402 | /* |
| 403 | * If we have a method, call it. If not then the driver probably wants |
| 404 | * to deal with speed changes on the next transfer. It can easily read |
| 405 | * the current speed from this uclass |
| 406 | */ |
| 407 | if (ops->set_bus_speed) { |
| 408 | ret = ops->set_bus_speed(bus, speed); |
| 409 | if (ret) |
| 410 | return ret; |
| 411 | } |
| 412 | i2c->speed_hz = speed; |
| 413 | |
| 414 | return 0; |
| 415 | } |
| 416 | |
Simon Glass | e4e8ff2 | 2015-02-05 21:41:32 -0700 | [diff] [blame] | 417 | int dm_i2c_get_bus_speed(struct udevice *bus) |
Simon Glass | c7a5890 | 2014-12-10 08:55:47 -0700 | [diff] [blame] | 418 | { |
| 419 | struct dm_i2c_ops *ops = i2c_get_ops(bus); |
Simon Glass | de0977b | 2015-03-05 12:25:20 -0700 | [diff] [blame] | 420 | struct dm_i2c_bus *i2c = dev_get_uclass_priv(bus); |
Simon Glass | c7a5890 | 2014-12-10 08:55:47 -0700 | [diff] [blame] | 421 | |
| 422 | if (!ops->get_bus_speed) |
| 423 | return i2c->speed_hz; |
| 424 | |
| 425 | return ops->get_bus_speed(bus); |
| 426 | } |
| 427 | |
| 428 | int i2c_set_chip_flags(struct udevice *dev, uint flags) |
| 429 | { |
| 430 | struct udevice *bus = dev->parent; |
Simon Glass | 713c3f0 | 2015-01-25 08:27:13 -0700 | [diff] [blame] | 431 | struct dm_i2c_chip *chip = dev_get_parent_platdata(dev); |
Simon Glass | c7a5890 | 2014-12-10 08:55:47 -0700 | [diff] [blame] | 432 | struct dm_i2c_ops *ops = i2c_get_ops(bus); |
| 433 | int ret; |
| 434 | |
| 435 | if (ops->set_flags) { |
| 436 | ret = ops->set_flags(dev, flags); |
| 437 | if (ret) |
| 438 | return ret; |
| 439 | } |
| 440 | chip->flags = flags; |
| 441 | |
| 442 | return 0; |
| 443 | } |
| 444 | |
| 445 | int i2c_get_chip_flags(struct udevice *dev, uint *flagsp) |
| 446 | { |
Simon Glass | 713c3f0 | 2015-01-25 08:27:13 -0700 | [diff] [blame] | 447 | struct dm_i2c_chip *chip = dev_get_parent_platdata(dev); |
Simon Glass | c7a5890 | 2014-12-10 08:55:47 -0700 | [diff] [blame] | 448 | |
| 449 | *flagsp = chip->flags; |
| 450 | |
| 451 | return 0; |
| 452 | } |
| 453 | |
| 454 | int i2c_set_chip_offset_len(struct udevice *dev, uint offset_len) |
| 455 | { |
Simon Glass | 713c3f0 | 2015-01-25 08:27:13 -0700 | [diff] [blame] | 456 | struct dm_i2c_chip *chip = dev_get_parent_platdata(dev); |
Simon Glass | c7a5890 | 2014-12-10 08:55:47 -0700 | [diff] [blame] | 457 | |
| 458 | if (offset_len > I2C_MAX_OFFSET_LEN) |
| 459 | return -EINVAL; |
| 460 | chip->offset_len = offset_len; |
| 461 | |
| 462 | return 0; |
| 463 | } |
| 464 | |
Simon Glass | 9a1589a | 2015-05-04 11:30:58 -0600 | [diff] [blame] | 465 | int i2c_get_chip_offset_len(struct udevice *dev) |
| 466 | { |
| 467 | struct dm_i2c_chip *chip = dev_get_parent_platdata(dev); |
| 468 | |
| 469 | return chip->offset_len; |
| 470 | } |
| 471 | |
Robert Beckett | 5f8e41a | 2019-10-28 17:44:57 +0000 | [diff] [blame] | 472 | int i2c_set_chip_addr_offset_mask(struct udevice *dev, uint mask) |
| 473 | { |
| 474 | struct dm_i2c_chip *chip = dev_get_parent_platdata(dev); |
| 475 | |
| 476 | chip->chip_addr_offset_mask = mask; |
| 477 | |
| 478 | return 0; |
| 479 | } |
| 480 | |
| 481 | uint i2c_get_chip_addr_offset_mask(struct udevice *dev) |
| 482 | { |
| 483 | struct dm_i2c_chip *chip = dev_get_parent_platdata(dev); |
| 484 | |
| 485 | return chip->chip_addr_offset_mask; |
| 486 | } |
| 487 | |
Simon Glass | fa4689a | 2019-12-06 21:41:35 -0700 | [diff] [blame] | 488 | #if CONFIG_IS_ENABLED(DM_GPIO) |
Alexander Kochetkov | dc96eb7 | 2018-03-27 17:52:27 +0300 | [diff] [blame] | 489 | static void i2c_gpio_set_pin(struct gpio_desc *pin, int bit) |
| 490 | { |
| 491 | if (bit) |
| 492 | dm_gpio_set_dir_flags(pin, GPIOD_IS_IN); |
| 493 | else |
| 494 | dm_gpio_set_dir_flags(pin, GPIOD_IS_OUT | |
| 495 | GPIOD_ACTIVE_LOW | |
| 496 | GPIOD_IS_OUT_ACTIVE); |
| 497 | } |
| 498 | |
| 499 | static int i2c_gpio_get_pin(struct gpio_desc *pin) |
| 500 | { |
| 501 | return dm_gpio_get_value(pin); |
| 502 | } |
| 503 | |
Marek Vasut | e2af341 | 2020-02-07 16:57:50 +0100 | [diff] [blame] | 504 | int i2c_deblock_gpio_loop(struct gpio_desc *sda_pin, |
| 505 | struct gpio_desc *scl_pin, |
| 506 | unsigned int scl_count, |
Marek Vasut | 39de925 | 2020-02-07 16:57:51 +0100 | [diff] [blame^] | 507 | unsigned int start_count, |
Marek Vasut | e2af341 | 2020-02-07 16:57:50 +0100 | [diff] [blame] | 508 | unsigned int delay) |
Alexander Kochetkov | dc96eb7 | 2018-03-27 17:52:27 +0300 | [diff] [blame] | 509 | { |
Marek Vasut | 39de925 | 2020-02-07 16:57:51 +0100 | [diff] [blame^] | 510 | int i, ret = -EREMOTEIO; |
Alexander Kochetkov | dc96eb7 | 2018-03-27 17:52:27 +0300 | [diff] [blame] | 511 | |
| 512 | i2c_gpio_set_pin(sda_pin, 1); |
| 513 | i2c_gpio_set_pin(scl_pin, 1); |
Marek Vasut | 2ffffc3 | 2020-02-07 16:57:49 +0100 | [diff] [blame] | 514 | udelay(delay); |
Alexander Kochetkov | dc96eb7 | 2018-03-27 17:52:27 +0300 | [diff] [blame] | 515 | |
| 516 | /* Toggle SCL until slave release SDA */ |
Marek Vasut | 2ffffc3 | 2020-02-07 16:57:49 +0100 | [diff] [blame] | 517 | while (scl_count-- >= 0) { |
Alexander Kochetkov | dc96eb7 | 2018-03-27 17:52:27 +0300 | [diff] [blame] | 518 | i2c_gpio_set_pin(scl_pin, 1); |
Marek Vasut | 2ffffc3 | 2020-02-07 16:57:49 +0100 | [diff] [blame] | 519 | udelay(delay); |
Alexander Kochetkov | dc96eb7 | 2018-03-27 17:52:27 +0300 | [diff] [blame] | 520 | i2c_gpio_set_pin(scl_pin, 0); |
Marek Vasut | 2ffffc3 | 2020-02-07 16:57:49 +0100 | [diff] [blame] | 521 | udelay(delay); |
Marek Vasut | 39de925 | 2020-02-07 16:57:51 +0100 | [diff] [blame^] | 522 | if (i2c_gpio_get_pin(sda_pin)) { |
| 523 | ret = 0; |
Alexander Kochetkov | dc96eb7 | 2018-03-27 17:52:27 +0300 | [diff] [blame] | 524 | break; |
Marek Vasut | 39de925 | 2020-02-07 16:57:51 +0100 | [diff] [blame^] | 525 | } |
| 526 | } |
| 527 | |
| 528 | if (!ret && start_count) { |
| 529 | for (i = 0; i < start_count; i++) { |
| 530 | /* Send start condition */ |
| 531 | udelay(delay); |
| 532 | i2c_gpio_set_pin(sda_pin, 1); |
| 533 | udelay(delay); |
| 534 | i2c_gpio_set_pin(scl_pin, 1); |
| 535 | udelay(delay); |
| 536 | i2c_gpio_set_pin(sda_pin, 0); |
| 537 | udelay(delay); |
| 538 | i2c_gpio_set_pin(scl_pin, 0); |
| 539 | } |
Alexander Kochetkov | dc96eb7 | 2018-03-27 17:52:27 +0300 | [diff] [blame] | 540 | } |
| 541 | |
| 542 | /* Then, send I2C stop */ |
| 543 | i2c_gpio_set_pin(sda_pin, 0); |
Marek Vasut | 2ffffc3 | 2020-02-07 16:57:49 +0100 | [diff] [blame] | 544 | udelay(delay); |
Alexander Kochetkov | dc96eb7 | 2018-03-27 17:52:27 +0300 | [diff] [blame] | 545 | |
| 546 | i2c_gpio_set_pin(scl_pin, 1); |
Marek Vasut | 2ffffc3 | 2020-02-07 16:57:49 +0100 | [diff] [blame] | 547 | udelay(delay); |
Alexander Kochetkov | dc96eb7 | 2018-03-27 17:52:27 +0300 | [diff] [blame] | 548 | |
| 549 | i2c_gpio_set_pin(sda_pin, 1); |
Marek Vasut | 2ffffc3 | 2020-02-07 16:57:49 +0100 | [diff] [blame] | 550 | udelay(delay); |
Alexander Kochetkov | dc96eb7 | 2018-03-27 17:52:27 +0300 | [diff] [blame] | 551 | |
| 552 | if (!i2c_gpio_get_pin(sda_pin) || !i2c_gpio_get_pin(scl_pin)) |
| 553 | ret = -EREMOTEIO; |
| 554 | |
| 555 | return ret; |
| 556 | } |
| 557 | |
| 558 | static int i2c_deblock_gpio(struct udevice *bus) |
| 559 | { |
| 560 | struct gpio_desc gpios[PIN_COUNT]; |
| 561 | int ret, ret0; |
| 562 | |
| 563 | ret = gpio_request_list_by_name(bus, "gpios", gpios, |
| 564 | ARRAY_SIZE(gpios), GPIOD_IS_IN); |
| 565 | if (ret != ARRAY_SIZE(gpios)) { |
| 566 | debug("%s: I2C Node '%s' has no 'gpios' property %s\n", |
| 567 | __func__, dev_read_name(bus), bus->name); |
| 568 | if (ret >= 0) { |
| 569 | gpio_free_list(bus, gpios, ret); |
| 570 | ret = -ENOENT; |
| 571 | } |
| 572 | goto out; |
| 573 | } |
| 574 | |
| 575 | ret = pinctrl_select_state(bus, "gpio"); |
| 576 | if (ret) { |
| 577 | debug("%s: I2C Node '%s' has no 'gpio' pinctrl state. %s\n", |
| 578 | __func__, dev_read_name(bus), bus->name); |
| 579 | goto out_no_pinctrl; |
| 580 | } |
| 581 | |
Marek Vasut | 39de925 | 2020-02-07 16:57:51 +0100 | [diff] [blame^] | 582 | ret0 = i2c_deblock_gpio_loop(&gpios[PIN_SDA], &gpios[PIN_SCL], 9, 0, 5); |
Alexander Kochetkov | dc96eb7 | 2018-03-27 17:52:27 +0300 | [diff] [blame] | 583 | |
| 584 | ret = pinctrl_select_state(bus, "default"); |
| 585 | if (ret) { |
| 586 | debug("%s: I2C Node '%s' has no 'default' pinctrl state. %s\n", |
| 587 | __func__, dev_read_name(bus), bus->name); |
| 588 | } |
| 589 | |
| 590 | ret = !ret ? ret0 : ret; |
| 591 | |
| 592 | out_no_pinctrl: |
| 593 | gpio_free_list(bus, gpios, ARRAY_SIZE(gpios)); |
| 594 | out: |
| 595 | return ret; |
| 596 | } |
| 597 | #else |
| 598 | static int i2c_deblock_gpio(struct udevice *bus) |
| 599 | { |
| 600 | return -ENOSYS; |
| 601 | } |
Simon Glass | fa4689a | 2019-12-06 21:41:35 -0700 | [diff] [blame] | 602 | #endif /* DM_GPIO */ |
Alexander Kochetkov | dc96eb7 | 2018-03-27 17:52:27 +0300 | [diff] [blame] | 603 | |
Simon Glass | c7a5890 | 2014-12-10 08:55:47 -0700 | [diff] [blame] | 604 | int i2c_deblock(struct udevice *bus) |
| 605 | { |
| 606 | struct dm_i2c_ops *ops = i2c_get_ops(bus); |
| 607 | |
Simon Glass | c7a5890 | 2014-12-10 08:55:47 -0700 | [diff] [blame] | 608 | if (!ops->deblock) |
Alexander Kochetkov | dc96eb7 | 2018-03-27 17:52:27 +0300 | [diff] [blame] | 609 | return i2c_deblock_gpio(bus); |
Simon Glass | c7a5890 | 2014-12-10 08:55:47 -0700 | [diff] [blame] | 610 | |
| 611 | return ops->deblock(bus); |
| 612 | } |
| 613 | |
Adam Ford | 1004fcd | 2018-08-20 20:24:34 -0500 | [diff] [blame] | 614 | #if CONFIG_IS_ENABLED(OF_CONTROL) && !CONFIG_IS_ENABLED(OF_PLATDATA) |
Simon Glass | 654e3a0 | 2017-05-18 20:09:30 -0600 | [diff] [blame] | 615 | int i2c_chip_ofdata_to_platdata(struct udevice *dev, struct dm_i2c_chip *chip) |
Simon Glass | c7a5890 | 2014-12-10 08:55:47 -0700 | [diff] [blame] | 616 | { |
Simon Glass | 654e3a0 | 2017-05-18 20:09:30 -0600 | [diff] [blame] | 617 | int addr; |
| 618 | |
| 619 | chip->offset_len = dev_read_u32_default(dev, "u-boot,i2c-offset-len", |
| 620 | 1); |
Simon Glass | c7a5890 | 2014-12-10 08:55:47 -0700 | [diff] [blame] | 621 | chip->flags = 0; |
Simon Glass | 654e3a0 | 2017-05-18 20:09:30 -0600 | [diff] [blame] | 622 | addr = dev_read_u32_default(dev, "reg", -1); |
| 623 | if (addr == -1) { |
| 624 | debug("%s: I2C Node '%s' has no 'reg' property %s\n", __func__, |
| 625 | dev_read_name(dev), dev->name); |
Simon Glass | c7a5890 | 2014-12-10 08:55:47 -0700 | [diff] [blame] | 626 | return -EINVAL; |
| 627 | } |
Simon Glass | 654e3a0 | 2017-05-18 20:09:30 -0600 | [diff] [blame] | 628 | chip->chip_addr = addr; |
Simon Glass | c7a5890 | 2014-12-10 08:55:47 -0700 | [diff] [blame] | 629 | |
| 630 | return 0; |
| 631 | } |
Mugunthan V N | bbf1ccf | 2016-07-18 15:10:58 +0530 | [diff] [blame] | 632 | #endif |
Simon Glass | c7a5890 | 2014-12-10 08:55:47 -0700 | [diff] [blame] | 633 | |
Lukasz Majewski | bc9aad6 | 2019-04-04 12:35:34 +0200 | [diff] [blame] | 634 | static int i2c_pre_probe(struct udevice *dev) |
| 635 | { |
| 636 | #if CONFIG_IS_ENABLED(OF_CONTROL) && !CONFIG_IS_ENABLED(OF_PLATDATA) |
| 637 | struct dm_i2c_bus *i2c = dev_get_uclass_priv(dev); |
| 638 | unsigned int max = 0; |
| 639 | ofnode node; |
| 640 | int ret; |
| 641 | |
| 642 | i2c->max_transaction_bytes = 0; |
| 643 | dev_for_each_subnode(node, dev) { |
| 644 | ret = ofnode_read_u32(node, |
| 645 | "u-boot,i2c-transaction-bytes", |
| 646 | &max); |
| 647 | if (!ret && max > i2c->max_transaction_bytes) |
| 648 | i2c->max_transaction_bytes = max; |
| 649 | } |
| 650 | |
| 651 | debug("%s: I2C bus: %s max transaction bytes: %d\n", __func__, |
| 652 | dev->name, i2c->max_transaction_bytes); |
| 653 | #endif |
| 654 | return 0; |
| 655 | } |
| 656 | |
Simon Glass | c7a5890 | 2014-12-10 08:55:47 -0700 | [diff] [blame] | 657 | static int i2c_post_probe(struct udevice *dev) |
| 658 | { |
Adam Ford | 1004fcd | 2018-08-20 20:24:34 -0500 | [diff] [blame] | 659 | #if CONFIG_IS_ENABLED(OF_CONTROL) && !CONFIG_IS_ENABLED(OF_PLATDATA) |
Simon Glass | de0977b | 2015-03-05 12:25:20 -0700 | [diff] [blame] | 660 | struct dm_i2c_bus *i2c = dev_get_uclass_priv(dev); |
Simon Glass | c7a5890 | 2014-12-10 08:55:47 -0700 | [diff] [blame] | 661 | |
Simon Glass | f0c99c5 | 2020-01-23 11:48:22 -0700 | [diff] [blame] | 662 | i2c->speed_hz = dev_read_u32_default(dev, "clock-frequency", |
| 663 | I2C_SPEED_STANDARD_RATE); |
Simon Glass | c7a5890 | 2014-12-10 08:55:47 -0700 | [diff] [blame] | 664 | |
Simon Glass | e4e8ff2 | 2015-02-05 21:41:32 -0700 | [diff] [blame] | 665 | return dm_i2c_set_bus_speed(dev, i2c->speed_hz); |
Mugunthan V N | bbf1ccf | 2016-07-18 15:10:58 +0530 | [diff] [blame] | 666 | #else |
| 667 | return 0; |
| 668 | #endif |
Simon Glass | c7a5890 | 2014-12-10 08:55:47 -0700 | [diff] [blame] | 669 | } |
| 670 | |
Simon Glass | 713c3f0 | 2015-01-25 08:27:13 -0700 | [diff] [blame] | 671 | static int i2c_child_post_bind(struct udevice *dev) |
| 672 | { |
Adam Ford | 1004fcd | 2018-08-20 20:24:34 -0500 | [diff] [blame] | 673 | #if CONFIG_IS_ENABLED(OF_CONTROL) && !CONFIG_IS_ENABLED(OF_PLATDATA) |
Simon Glass | 713c3f0 | 2015-01-25 08:27:13 -0700 | [diff] [blame] | 674 | struct dm_i2c_chip *plat = dev_get_parent_platdata(dev); |
| 675 | |
Simon Glass | 654e3a0 | 2017-05-18 20:09:30 -0600 | [diff] [blame] | 676 | if (!dev_of_valid(dev)) |
Simon Glass | 713c3f0 | 2015-01-25 08:27:13 -0700 | [diff] [blame] | 677 | return 0; |
Simon Glass | 654e3a0 | 2017-05-18 20:09:30 -0600 | [diff] [blame] | 678 | return i2c_chip_ofdata_to_platdata(dev, plat); |
Mugunthan V N | bbf1ccf | 2016-07-18 15:10:58 +0530 | [diff] [blame] | 679 | #else |
| 680 | return 0; |
| 681 | #endif |
Simon Glass | 713c3f0 | 2015-01-25 08:27:13 -0700 | [diff] [blame] | 682 | } |
| 683 | |
Michal Simek | 54802408 | 2019-01-31 16:31:01 +0100 | [diff] [blame] | 684 | struct i2c_priv { |
| 685 | int max_id; |
| 686 | }; |
| 687 | |
Michal Simek | f7ab620 | 2019-01-31 16:31:02 +0100 | [diff] [blame] | 688 | static int i2c_post_bind(struct udevice *dev) |
| 689 | { |
| 690 | struct uclass *class = dev->uclass; |
| 691 | struct i2c_priv *priv = class->priv; |
| 692 | int ret = 0; |
| 693 | |
| 694 | /* Just for sure */ |
| 695 | if (!priv) |
| 696 | return -ENOMEM; |
| 697 | |
| 698 | debug("%s: %s, req_seq=%d\n", __func__, dev->name, dev->req_seq); |
| 699 | |
| 700 | /* if there is no alias ID, use the first free */ |
| 701 | if (dev->req_seq == -1) |
| 702 | dev->req_seq = ++priv->max_id; |
| 703 | |
| 704 | debug("%s: %s, new req_seq=%d\n", __func__, dev->name, dev->req_seq); |
| 705 | |
| 706 | #if CONFIG_IS_ENABLED(OF_CONTROL) && !CONFIG_IS_ENABLED(OF_PLATDATA) |
| 707 | ret = dm_scan_fdt_dev(dev); |
| 708 | #endif |
| 709 | return ret; |
| 710 | } |
| 711 | |
Michal Simek | 54802408 | 2019-01-31 16:31:01 +0100 | [diff] [blame] | 712 | int i2c_uclass_init(struct uclass *class) |
| 713 | { |
| 714 | struct i2c_priv *priv = class->priv; |
| 715 | |
| 716 | /* Just for sure */ |
| 717 | if (!priv) |
| 718 | return -ENOMEM; |
| 719 | |
| 720 | /* Get the last allocated alias. */ |
Simon Glass | f0c99c5 | 2020-01-23 11:48:22 -0700 | [diff] [blame] | 721 | if (CONFIG_IS_ENABLED(OF_CONTROL) && !CONFIG_IS_ENABLED(OF_PLATDATA)) |
| 722 | priv->max_id = dev_read_alias_highest_id("i2c"); |
| 723 | else |
| 724 | priv->max_id = -1; |
Michal Simek | 54802408 | 2019-01-31 16:31:01 +0100 | [diff] [blame] | 725 | |
| 726 | debug("%s: highest alias id is %d\n", __func__, priv->max_id); |
| 727 | |
| 728 | return 0; |
| 729 | } |
| 730 | |
Simon Glass | c7a5890 | 2014-12-10 08:55:47 -0700 | [diff] [blame] | 731 | UCLASS_DRIVER(i2c) = { |
| 732 | .id = UCLASS_I2C, |
| 733 | .name = "i2c", |
Simon Glass | 0ccb097 | 2015-01-25 08:27:05 -0700 | [diff] [blame] | 734 | .flags = DM_UC_FLAG_SEQ_ALIAS, |
Michal Simek | f7ab620 | 2019-01-31 16:31:02 +0100 | [diff] [blame] | 735 | .post_bind = i2c_post_bind, |
Michal Simek | 54802408 | 2019-01-31 16:31:01 +0100 | [diff] [blame] | 736 | .init = i2c_uclass_init, |
| 737 | .priv_auto_alloc_size = sizeof(struct i2c_priv), |
Lukasz Majewski | bc9aad6 | 2019-04-04 12:35:34 +0200 | [diff] [blame] | 738 | .pre_probe = i2c_pre_probe, |
Simon Glass | c7a5890 | 2014-12-10 08:55:47 -0700 | [diff] [blame] | 739 | .post_probe = i2c_post_probe, |
Simon Glass | 713c3f0 | 2015-01-25 08:27:13 -0700 | [diff] [blame] | 740 | .per_device_auto_alloc_size = sizeof(struct dm_i2c_bus), |
| 741 | .per_child_platdata_auto_alloc_size = sizeof(struct dm_i2c_chip), |
| 742 | .child_post_bind = i2c_child_post_bind, |
Simon Glass | c7a5890 | 2014-12-10 08:55:47 -0700 | [diff] [blame] | 743 | }; |
| 744 | |
| 745 | UCLASS_DRIVER(i2c_generic) = { |
| 746 | .id = UCLASS_I2C_GENERIC, |
| 747 | .name = "i2c_generic", |
| 748 | }; |
| 749 | |
| 750 | U_BOOT_DRIVER(i2c_generic_chip_drv) = { |
| 751 | .name = "i2c_generic_chip_drv", |
| 752 | .id = UCLASS_I2C_GENERIC, |
| 753 | }; |