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