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