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