Googler | e00b8eb | 2019-07-08 16:37:07 -0700 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (c) 2014 Google, Inc |
Googler | 695f9d9 | 2023-09-11 15:38:29 +0800 | [diff] [blame^] | 3 | * |
| 4 | * SPDX-License-Identifier: GPL-2.0+ |
Googler | e00b8eb | 2019-07-08 16:37:07 -0700 | [diff] [blame] | 5 | */ |
| 6 | |
| 7 | #include <common.h> |
| 8 | #include <dm.h> |
| 9 | #include <errno.h> |
Googler | 695f9d9 | 2023-09-11 15:38:29 +0800 | [diff] [blame^] | 10 | #include <fdtdec.h> |
Googler | e00b8eb | 2019-07-08 16:37:07 -0700 | [diff] [blame] | 11 | #include <i2c.h> |
| 12 | #include <malloc.h> |
| 13 | #include <dm/device-internal.h> |
| 14 | #include <dm/lists.h> |
Googler | 695f9d9 | 2023-09-11 15:38:29 +0800 | [diff] [blame^] | 15 | #include <dm/root.h> |
| 16 | #ifdef CONFIG_SYS_I2C_MESON |
| 17 | #include <amlogic/i2c.h> |
Googler | e00b8eb | 2019-07-08 16:37:07 -0700 | [diff] [blame] | 18 | #endif |
| 19 | |
Googler | 695f9d9 | 2023-09-11 15:38:29 +0800 | [diff] [blame^] | 20 | DECLARE_GLOBAL_DATA_PTR; |
| 21 | |
Googler | e00b8eb | 2019-07-08 16:37:07 -0700 | [diff] [blame] | 22 | #define I2C_MAX_OFFSET_LEN 4 |
| 23 | |
Googler | e00b8eb | 2019-07-08 16:37:07 -0700 | [diff] [blame] | 24 | /** |
| 25 | * i2c_setup_offset() - Set up a new message with a chip offset |
| 26 | * |
| 27 | * @chip: Chip to use |
| 28 | * @offset: Byte offset within chip |
| 29 | * @offset_buf: Place to put byte offset |
| 30 | * @msg: Message buffer |
| 31 | * @return 0 if OK, -EADDRNOTAVAIL if the offset length is 0. In that case the |
| 32 | * message is still set up but will not contain an offset. |
| 33 | */ |
| 34 | static int i2c_setup_offset(struct dm_i2c_chip *chip, uint offset, |
| 35 | uint8_t offset_buf[], struct i2c_msg *msg) |
| 36 | { |
| 37 | int offset_len; |
| 38 | |
| 39 | msg->addr = chip->chip_addr; |
| 40 | msg->flags = chip->flags & DM_I2C_CHIP_10BIT ? I2C_M_TEN : 0; |
| 41 | msg->len = chip->offset_len; |
| 42 | msg->buf = offset_buf; |
| 43 | if (!chip->offset_len) |
| 44 | return -EADDRNOTAVAIL; |
| 45 | assert(chip->offset_len <= I2C_MAX_OFFSET_LEN); |
| 46 | offset_len = chip->offset_len; |
| 47 | while (offset_len--) |
| 48 | *offset_buf++ = offset >> (8 * offset_len); |
| 49 | |
| 50 | return 0; |
| 51 | } |
| 52 | |
| 53 | static int i2c_read_bytewise(struct udevice *dev, uint offset, |
| 54 | uint8_t *buffer, int len) |
| 55 | { |
Googler | 695f9d9 | 2023-09-11 15:38:29 +0800 | [diff] [blame^] | 56 | struct dm_i2c_chip *chip = dev_get_parentdata(dev); |
Googler | e00b8eb | 2019-07-08 16:37:07 -0700 | [diff] [blame] | 57 | struct udevice *bus = dev_get_parent(dev); |
| 58 | struct dm_i2c_ops *ops = i2c_get_ops(bus); |
| 59 | struct i2c_msg msg[2], *ptr; |
| 60 | uint8_t offset_buf[I2C_MAX_OFFSET_LEN]; |
| 61 | int ret; |
| 62 | int i; |
| 63 | |
| 64 | for (i = 0; i < len; i++) { |
| 65 | if (i2c_setup_offset(chip, offset + i, offset_buf, msg)) |
| 66 | return -EINVAL; |
| 67 | ptr = msg + 1; |
| 68 | ptr->addr = chip->chip_addr; |
| 69 | ptr->flags = msg->flags | I2C_M_RD; |
| 70 | ptr->len = 1; |
| 71 | ptr->buf = &buffer[i]; |
| 72 | ptr++; |
| 73 | |
| 74 | ret = ops->xfer(bus, msg, ptr - msg); |
| 75 | if (ret) |
| 76 | return ret; |
| 77 | } |
| 78 | |
| 79 | return 0; |
| 80 | } |
| 81 | |
| 82 | static int i2c_write_bytewise(struct udevice *dev, uint offset, |
| 83 | const uint8_t *buffer, int len) |
| 84 | { |
Googler | 695f9d9 | 2023-09-11 15:38:29 +0800 | [diff] [blame^] | 85 | struct dm_i2c_chip *chip = dev_get_parentdata(dev); |
Googler | e00b8eb | 2019-07-08 16:37:07 -0700 | [diff] [blame] | 86 | struct udevice *bus = dev_get_parent(dev); |
| 87 | struct dm_i2c_ops *ops = i2c_get_ops(bus); |
| 88 | struct i2c_msg msg[1]; |
| 89 | uint8_t buf[I2C_MAX_OFFSET_LEN + 1]; |
| 90 | int ret; |
| 91 | int i; |
| 92 | |
| 93 | for (i = 0; i < len; i++) { |
| 94 | if (i2c_setup_offset(chip, offset + i, buf, msg)) |
| 95 | return -EINVAL; |
| 96 | buf[msg->len++] = buffer[i]; |
| 97 | |
| 98 | ret = ops->xfer(bus, msg, 1); |
| 99 | if (ret) |
| 100 | return ret; |
| 101 | } |
| 102 | |
| 103 | return 0; |
| 104 | } |
| 105 | |
Googler | 695f9d9 | 2023-09-11 15:38:29 +0800 | [diff] [blame^] | 106 | int i2c_read(struct udevice *dev, uint offset, uint8_t *buffer, int len) |
Googler | e00b8eb | 2019-07-08 16:37:07 -0700 | [diff] [blame] | 107 | { |
Googler | 695f9d9 | 2023-09-11 15:38:29 +0800 | [diff] [blame^] | 108 | struct dm_i2c_chip *chip = dev_get_parentdata(dev); |
Googler | e00b8eb | 2019-07-08 16:37:07 -0700 | [diff] [blame] | 109 | struct udevice *bus = dev_get_parent(dev); |
| 110 | struct dm_i2c_ops *ops = i2c_get_ops(bus); |
| 111 | struct i2c_msg msg[2], *ptr; |
| 112 | uint8_t offset_buf[I2C_MAX_OFFSET_LEN]; |
| 113 | int msg_count; |
| 114 | |
| 115 | if (!ops->xfer) |
| 116 | return -ENOSYS; |
| 117 | if (chip->flags & DM_I2C_CHIP_RD_ADDRESS) |
| 118 | return i2c_read_bytewise(dev, offset, buffer, len); |
| 119 | ptr = msg; |
| 120 | if (!i2c_setup_offset(chip, offset, offset_buf, ptr)) |
| 121 | ptr++; |
| 122 | |
| 123 | if (len) { |
| 124 | ptr->addr = chip->chip_addr; |
| 125 | ptr->flags = chip->flags & DM_I2C_CHIP_10BIT ? I2C_M_TEN : 0; |
| 126 | ptr->flags |= I2C_M_RD; |
| 127 | ptr->len = len; |
| 128 | ptr->buf = buffer; |
| 129 | ptr++; |
| 130 | } |
| 131 | msg_count = ptr - msg; |
| 132 | |
| 133 | return ops->xfer(bus, msg, msg_count); |
| 134 | } |
| 135 | |
Googler | 695f9d9 | 2023-09-11 15:38:29 +0800 | [diff] [blame^] | 136 | int i2c_write(struct udevice *dev, uint offset, const uint8_t *buffer, int len) |
Googler | e00b8eb | 2019-07-08 16:37:07 -0700 | [diff] [blame] | 137 | { |
Googler | 695f9d9 | 2023-09-11 15:38:29 +0800 | [diff] [blame^] | 138 | struct dm_i2c_chip *chip = dev_get_parentdata(dev); |
Googler | e00b8eb | 2019-07-08 16:37:07 -0700 | [diff] [blame] | 139 | struct udevice *bus = dev_get_parent(dev); |
| 140 | struct dm_i2c_ops *ops = i2c_get_ops(bus); |
| 141 | struct i2c_msg msg[1]; |
| 142 | |
| 143 | if (!ops->xfer) |
| 144 | return -ENOSYS; |
| 145 | |
| 146 | if (chip->flags & DM_I2C_CHIP_WR_ADDRESS) |
| 147 | return i2c_write_bytewise(dev, offset, buffer, len); |
| 148 | /* |
| 149 | * The simple approach would be to send two messages here: one to |
| 150 | * set the offset and one to write the bytes. However some drivers |
| 151 | * will not be expecting this, and some chips won't like how the |
| 152 | * driver presents this on the I2C bus. |
| 153 | * |
| 154 | * The API does not support separate offset and data. We could extend |
| 155 | * it with a flag indicating that there is data in the next message |
| 156 | * that needs to be processed in the same transaction. We could |
| 157 | * instead add an additional buffer to each message. For now, handle |
| 158 | * this in the uclass since it isn't clear what the impact on drivers |
| 159 | * would be with this extra complication. Unfortunately this means |
| 160 | * copying the message. |
| 161 | * |
| 162 | * Use the stack for small messages, malloc() for larger ones. We |
| 163 | * need to allow space for the offset (up to 4 bytes) and the message |
| 164 | * itself. |
| 165 | */ |
| 166 | if (len < 64) { |
| 167 | uint8_t buf[I2C_MAX_OFFSET_LEN + len]; |
| 168 | |
| 169 | i2c_setup_offset(chip, offset, buf, msg); |
| 170 | msg->len += len; |
| 171 | memcpy(buf + chip->offset_len, buffer, len); |
| 172 | |
| 173 | return ops->xfer(bus, msg, 1); |
| 174 | } else { |
| 175 | uint8_t *buf; |
| 176 | int ret; |
| 177 | |
| 178 | buf = malloc(I2C_MAX_OFFSET_LEN + len); |
| 179 | if (!buf) |
| 180 | return -ENOMEM; |
| 181 | i2c_setup_offset(chip, offset, buf, msg); |
| 182 | msg->len += len; |
| 183 | memcpy(buf + chip->offset_len, buffer, len); |
| 184 | |
| 185 | ret = ops->xfer(bus, msg, 1); |
| 186 | free(buf); |
| 187 | return ret; |
| 188 | } |
| 189 | } |
| 190 | |
Googler | e00b8eb | 2019-07-08 16:37:07 -0700 | [diff] [blame] | 191 | /** |
| 192 | * i2c_probe_chip() - probe for a chip on a bus |
| 193 | * |
| 194 | * @bus: Bus to probe |
| 195 | * @chip_addr: Chip address to probe |
| 196 | * @flags: Flags for the chip |
| 197 | * @return 0 if found, -ENOSYS if the driver is invalid, -EREMOTEIO if the chip |
| 198 | * does not respond to probe |
| 199 | */ |
| 200 | static int i2c_probe_chip(struct udevice *bus, uint chip_addr, |
| 201 | enum dm_i2c_chip_flags chip_flags) |
| 202 | { |
| 203 | struct dm_i2c_ops *ops = i2c_get_ops(bus); |
| 204 | struct i2c_msg msg[1]; |
| 205 | int ret; |
| 206 | |
| 207 | if (ops->probe_chip) { |
| 208 | ret = ops->probe_chip(bus, chip_addr, chip_flags); |
| 209 | if (!ret || ret != -ENOSYS) |
| 210 | return ret; |
| 211 | } |
| 212 | |
| 213 | if (!ops->xfer) |
| 214 | return -ENOSYS; |
| 215 | |
| 216 | /* Probe with a zero-length message */ |
| 217 | msg->addr = chip_addr; |
| 218 | msg->flags = chip_flags & DM_I2C_CHIP_10BIT ? I2C_M_TEN : 0; |
| 219 | msg->len = 0; |
| 220 | msg->buf = NULL; |
| 221 | |
| 222 | return ops->xfer(bus, msg, 1); |
| 223 | } |
| 224 | |
Googler | 695f9d9 | 2023-09-11 15:38:29 +0800 | [diff] [blame^] | 225 | static int i2c_bind_driver(struct udevice *bus, uint chip_addr, |
Googler | e00b8eb | 2019-07-08 16:37:07 -0700 | [diff] [blame] | 226 | struct udevice **devp) |
| 227 | { |
Googler | 695f9d9 | 2023-09-11 15:38:29 +0800 | [diff] [blame^] | 228 | struct dm_i2c_chip chip; |
Googler | e00b8eb | 2019-07-08 16:37:07 -0700 | [diff] [blame] | 229 | char name[30], *str; |
| 230 | struct udevice *dev; |
| 231 | int ret; |
| 232 | |
| 233 | snprintf(name, sizeof(name), "generic_%x", chip_addr); |
| 234 | str = strdup(name); |
Googler | e00b8eb | 2019-07-08 16:37:07 -0700 | [diff] [blame] | 235 | ret = device_bind_driver(bus, "i2c_generic_chip_drv", str, &dev); |
| 236 | debug("%s: device_bind_driver: ret=%d\n", __func__, ret); |
| 237 | if (ret) |
| 238 | goto err_bind; |
| 239 | |
| 240 | /* Tell the device what we know about it */ |
Googler | 695f9d9 | 2023-09-11 15:38:29 +0800 | [diff] [blame^] | 241 | memset(&chip, '\0', sizeof(chip)); |
| 242 | chip.chip_addr = chip_addr; |
| 243 | chip.offset_len = 1; /* we assume */ |
| 244 | ret = device_probe_child(dev, &chip); |
| 245 | debug("%s: device_probe_child: ret=%d\n", __func__, ret); |
Googler | e00b8eb | 2019-07-08 16:37:07 -0700 | [diff] [blame] | 246 | if (ret) |
| 247 | goto err_probe; |
| 248 | |
| 249 | *devp = dev; |
| 250 | return 0; |
| 251 | |
| 252 | err_probe: |
Googler | e00b8eb | 2019-07-08 16:37:07 -0700 | [diff] [blame] | 253 | device_unbind(dev); |
| 254 | err_bind: |
| 255 | free(str); |
| 256 | return ret; |
| 257 | } |
| 258 | |
Googler | 695f9d9 | 2023-09-11 15:38:29 +0800 | [diff] [blame^] | 259 | int i2c_get_chip(struct udevice *bus, uint chip_addr, struct udevice **devp) |
Googler | e00b8eb | 2019-07-08 16:37:07 -0700 | [diff] [blame] | 260 | { |
| 261 | struct udevice *dev; |
| 262 | |
| 263 | debug("%s: Searching bus '%s' for address %02x: ", __func__, |
| 264 | bus->name, chip_addr); |
| 265 | for (device_find_first_child(bus, &dev); dev; |
| 266 | device_find_next_child(&dev)) { |
Googler | 695f9d9 | 2023-09-11 15:38:29 +0800 | [diff] [blame^] | 267 | struct dm_i2c_chip store; |
| 268 | struct dm_i2c_chip *chip = dev_get_parentdata(dev); |
Googler | e00b8eb | 2019-07-08 16:37:07 -0700 | [diff] [blame] | 269 | int ret; |
| 270 | |
Googler | 695f9d9 | 2023-09-11 15:38:29 +0800 | [diff] [blame^] | 271 | if (!chip) { |
| 272 | chip = &store; |
| 273 | i2c_chip_ofdata_to_platdata(gd->fdt_blob, |
| 274 | dev->of_offset, chip); |
| 275 | } |
Googler | e00b8eb | 2019-07-08 16:37:07 -0700 | [diff] [blame] | 276 | if (chip->chip_addr == chip_addr) { |
| 277 | ret = device_probe(dev); |
| 278 | debug("found, ret=%d\n", ret); |
| 279 | if (ret) |
| 280 | return ret; |
| 281 | *devp = dev; |
| 282 | return 0; |
| 283 | } |
| 284 | } |
| 285 | debug("not found\n"); |
Googler | 695f9d9 | 2023-09-11 15:38:29 +0800 | [diff] [blame^] | 286 | return i2c_bind_driver(bus, chip_addr, devp); |
Googler | e00b8eb | 2019-07-08 16:37:07 -0700 | [diff] [blame] | 287 | } |
| 288 | |
Googler | 695f9d9 | 2023-09-11 15:38:29 +0800 | [diff] [blame^] | 289 | int i2c_get_chip_for_busnum(int busnum, int chip_addr, struct udevice **devp) |
Googler | e00b8eb | 2019-07-08 16:37:07 -0700 | [diff] [blame] | 290 | { |
| 291 | struct udevice *bus; |
| 292 | int ret; |
| 293 | |
| 294 | ret = uclass_get_device_by_seq(UCLASS_I2C, busnum, &bus); |
| 295 | if (ret) { |
| 296 | debug("Cannot find I2C bus %d\n", busnum); |
| 297 | return ret; |
| 298 | } |
Googler | 695f9d9 | 2023-09-11 15:38:29 +0800 | [diff] [blame^] | 299 | ret = i2c_get_chip(bus, chip_addr, devp); |
Googler | e00b8eb | 2019-07-08 16:37:07 -0700 | [diff] [blame] | 300 | if (ret) { |
| 301 | debug("Cannot find I2C chip %02x on bus %d\n", chip_addr, |
| 302 | busnum); |
| 303 | return ret; |
| 304 | } |
| 305 | |
| 306 | return 0; |
| 307 | } |
| 308 | |
Googler | 695f9d9 | 2023-09-11 15:38:29 +0800 | [diff] [blame^] | 309 | int i2c_probe(struct udevice *bus, uint chip_addr, uint chip_flags, |
| 310 | struct udevice **devp) |
Googler | e00b8eb | 2019-07-08 16:37:07 -0700 | [diff] [blame] | 311 | { |
| 312 | int ret; |
| 313 | |
| 314 | *devp = NULL; |
| 315 | |
| 316 | /* First probe that chip */ |
| 317 | ret = i2c_probe_chip(bus, chip_addr, chip_flags); |
| 318 | debug("%s: bus='%s', address %02x, ret=%d\n", __func__, bus->name, |
| 319 | chip_addr, ret); |
| 320 | if (ret) |
| 321 | return ret; |
| 322 | |
| 323 | /* The chip was found, see if we have a driver, and probe it */ |
Googler | 695f9d9 | 2023-09-11 15:38:29 +0800 | [diff] [blame^] | 324 | ret = i2c_get_chip(bus, chip_addr, devp); |
Googler | e00b8eb | 2019-07-08 16:37:07 -0700 | [diff] [blame] | 325 | debug("%s: i2c_get_chip: ret=%d\n", __func__, ret); |
| 326 | |
| 327 | return ret; |
| 328 | } |
| 329 | |
Googler | 695f9d9 | 2023-09-11 15:38:29 +0800 | [diff] [blame^] | 330 | int i2c_set_bus_speed(struct udevice *bus, unsigned int speed) |
Googler | e00b8eb | 2019-07-08 16:37:07 -0700 | [diff] [blame] | 331 | { |
| 332 | struct dm_i2c_ops *ops = i2c_get_ops(bus); |
Googler | 695f9d9 | 2023-09-11 15:38:29 +0800 | [diff] [blame^] | 333 | struct dm_i2c_bus *i2c = bus->uclass_priv; |
Googler | e00b8eb | 2019-07-08 16:37:07 -0700 | [diff] [blame] | 334 | int ret; |
| 335 | |
| 336 | /* |
| 337 | * If we have a method, call it. If not then the driver probably wants |
| 338 | * to deal with speed changes on the next transfer. It can easily read |
| 339 | * the current speed from this uclass |
| 340 | */ |
| 341 | if (ops->set_bus_speed) { |
| 342 | ret = ops->set_bus_speed(bus, speed); |
| 343 | if (ret) |
| 344 | return ret; |
| 345 | } |
| 346 | i2c->speed_hz = speed; |
| 347 | |
| 348 | return 0; |
| 349 | } |
| 350 | |
Googler | 695f9d9 | 2023-09-11 15:38:29 +0800 | [diff] [blame^] | 351 | /* |
| 352 | * i2c_get_bus_speed: |
| 353 | * |
| 354 | * Returns speed of selected I2C bus in Hz |
| 355 | */ |
| 356 | int i2c_get_bus_speed(struct udevice *bus) |
Googler | e00b8eb | 2019-07-08 16:37:07 -0700 | [diff] [blame] | 357 | { |
| 358 | struct dm_i2c_ops *ops = i2c_get_ops(bus); |
Googler | 695f9d9 | 2023-09-11 15:38:29 +0800 | [diff] [blame^] | 359 | struct dm_i2c_bus *i2c = bus->uclass_priv; |
Googler | e00b8eb | 2019-07-08 16:37:07 -0700 | [diff] [blame] | 360 | |
| 361 | if (!ops->get_bus_speed) |
| 362 | return i2c->speed_hz; |
| 363 | |
| 364 | return ops->get_bus_speed(bus); |
| 365 | } |
| 366 | |
| 367 | int i2c_set_chip_flags(struct udevice *dev, uint flags) |
| 368 | { |
| 369 | struct udevice *bus = dev->parent; |
Googler | 695f9d9 | 2023-09-11 15:38:29 +0800 | [diff] [blame^] | 370 | struct dm_i2c_chip *chip = dev_get_parentdata(dev); |
Googler | e00b8eb | 2019-07-08 16:37:07 -0700 | [diff] [blame] | 371 | struct dm_i2c_ops *ops = i2c_get_ops(bus); |
| 372 | int ret; |
| 373 | |
| 374 | if (ops->set_flags) { |
| 375 | ret = ops->set_flags(dev, flags); |
| 376 | if (ret) |
| 377 | return ret; |
| 378 | } |
| 379 | chip->flags = flags; |
| 380 | |
| 381 | return 0; |
| 382 | } |
| 383 | |
| 384 | int i2c_get_chip_flags(struct udevice *dev, uint *flagsp) |
| 385 | { |
Googler | 695f9d9 | 2023-09-11 15:38:29 +0800 | [diff] [blame^] | 386 | struct dm_i2c_chip *chip = dev_get_parentdata(dev); |
Googler | e00b8eb | 2019-07-08 16:37:07 -0700 | [diff] [blame] | 387 | |
| 388 | *flagsp = chip->flags; |
| 389 | |
| 390 | return 0; |
| 391 | } |
| 392 | |
| 393 | int i2c_set_chip_offset_len(struct udevice *dev, uint offset_len) |
| 394 | { |
Googler | 695f9d9 | 2023-09-11 15:38:29 +0800 | [diff] [blame^] | 395 | struct dm_i2c_chip *chip = dev_get_parentdata(dev); |
Googler | e00b8eb | 2019-07-08 16:37:07 -0700 | [diff] [blame] | 396 | |
| 397 | if (offset_len > I2C_MAX_OFFSET_LEN) |
| 398 | return -EINVAL; |
| 399 | chip->offset_len = offset_len; |
| 400 | |
| 401 | return 0; |
| 402 | } |
| 403 | |
Googler | e00b8eb | 2019-07-08 16:37:07 -0700 | [diff] [blame] | 404 | int i2c_deblock(struct udevice *bus) |
| 405 | { |
| 406 | struct dm_i2c_ops *ops = i2c_get_ops(bus); |
| 407 | |
Googler | 695f9d9 | 2023-09-11 15:38:29 +0800 | [diff] [blame^] | 408 | /* |
| 409 | * We could implement a software deblocking here if we could get |
| 410 | * access to the GPIOs used by I2C, and switch them to GPIO mode |
| 411 | * and then back to I2C. This is somewhat beyond our powers in |
| 412 | * driver model at present, so for now just fail. |
| 413 | * |
| 414 | * See https://patchwork.ozlabs.org/patch/399040/ |
| 415 | */ |
Googler | e00b8eb | 2019-07-08 16:37:07 -0700 | [diff] [blame] | 416 | if (!ops->deblock) |
Googler | 695f9d9 | 2023-09-11 15:38:29 +0800 | [diff] [blame^] | 417 | return -ENOSYS; |
Googler | e00b8eb | 2019-07-08 16:37:07 -0700 | [diff] [blame] | 418 | |
| 419 | return ops->deblock(bus); |
| 420 | } |
| 421 | |
Googler | 695f9d9 | 2023-09-11 15:38:29 +0800 | [diff] [blame^] | 422 | int i2c_chip_ofdata_to_platdata(const void *blob, int node, |
| 423 | struct dm_i2c_chip *chip) |
Googler | e00b8eb | 2019-07-08 16:37:07 -0700 | [diff] [blame] | 424 | { |
Googler | 695f9d9 | 2023-09-11 15:38:29 +0800 | [diff] [blame^] | 425 | chip->offset_len = 1; /* default */ |
Googler | e00b8eb | 2019-07-08 16:37:07 -0700 | [diff] [blame] | 426 | chip->flags = 0; |
Googler | 695f9d9 | 2023-09-11 15:38:29 +0800 | [diff] [blame^] | 427 | chip->chip_addr = fdtdec_get_int(gd->fdt_blob, node, "reg", -1); |
| 428 | if (chip->chip_addr == -1) { |
| 429 | debug("%s: I2C Node '%s' has no 'reg' property\n", __func__, |
| 430 | fdt_get_name(blob, node, NULL)); |
Googler | e00b8eb | 2019-07-08 16:37:07 -0700 | [diff] [blame] | 431 | return -EINVAL; |
| 432 | } |
Googler | e00b8eb | 2019-07-08 16:37:07 -0700 | [diff] [blame] | 433 | |
| 434 | return 0; |
| 435 | } |
Googler | e00b8eb | 2019-07-08 16:37:07 -0700 | [diff] [blame] | 436 | |
| 437 | static int i2c_post_probe(struct udevice *dev) |
| 438 | { |
Googler | 695f9d9 | 2023-09-11 15:38:29 +0800 | [diff] [blame^] | 439 | struct dm_i2c_bus *i2c = dev->uclass_priv; |
Googler | e00b8eb | 2019-07-08 16:37:07 -0700 | [diff] [blame] | 440 | |
Googler | 695f9d9 | 2023-09-11 15:38:29 +0800 | [diff] [blame^] | 441 | i2c->speed_hz = fdtdec_get_int(gd->fdt_blob, dev->of_offset, |
| 442 | "clock-frequency", 100000); |
Googler | e00b8eb | 2019-07-08 16:37:07 -0700 | [diff] [blame] | 443 | |
Googler | 695f9d9 | 2023-09-11 15:38:29 +0800 | [diff] [blame^] | 444 | #ifdef CONFIG_SYS_I2C_MESON |
| 445 | i2c->speed_hz = ((struct meson_i2c_platdata *)dev->platdata)->clock_frequency; |
Googler | fc3e29a | 2022-11-22 14:17:45 +0800 | [diff] [blame] | 446 | #endif |
Googler | 695f9d9 | 2023-09-11 15:38:29 +0800 | [diff] [blame^] | 447 | |
| 448 | return i2c_set_bus_speed(dev, i2c->speed_hz); |
Googler | fc3e29a | 2022-11-22 14:17:45 +0800 | [diff] [blame] | 449 | } |
Googler | 15b35d9 | 2022-10-03 17:48:21 +0800 | [diff] [blame] | 450 | |
Googler | 695f9d9 | 2023-09-11 15:38:29 +0800 | [diff] [blame^] | 451 | static unsigned int i2c_req_seq = 0; |
| 452 | |
| 453 | int i2c_post_bind(struct udevice *dev) |
Googler | 25e92cf | 2023-12-13 10:05:01 +0000 | [diff] [blame] | 454 | { |
Googler | 695f9d9 | 2023-09-11 15:38:29 +0800 | [diff] [blame^] | 455 | #ifdef CONFIG_OF_CONTROL |
| 456 | /* Scan the bus for devices */ |
| 457 | return dm_scan_fdt_node(dev, gd->fdt_blob, dev->of_offset, false); |
Googler | 25e92cf | 2023-12-13 10:05:01 +0000 | [diff] [blame] | 458 | #else |
Googler | 695f9d9 | 2023-09-11 15:38:29 +0800 | [diff] [blame^] | 459 | dev->req_seq = i2c_req_seq++; |
Googler | 25e92cf | 2023-12-13 10:05:01 +0000 | [diff] [blame] | 460 | return 0; |
| 461 | #endif |
Googler | 695f9d9 | 2023-09-11 15:38:29 +0800 | [diff] [blame^] | 462 | |
Googler | 25e92cf | 2023-12-13 10:05:01 +0000 | [diff] [blame] | 463 | } |
Googler | 40bc9d0 | 2023-12-15 16:42:49 +0800 | [diff] [blame] | 464 | |
Googler | 695f9d9 | 2023-09-11 15:38:29 +0800 | [diff] [blame^] | 465 | |
Googler | e00b8eb | 2019-07-08 16:37:07 -0700 | [diff] [blame] | 466 | UCLASS_DRIVER(i2c) = { |
| 467 | .id = UCLASS_I2C, |
| 468 | .name = "i2c", |
Googler | 25e92cf | 2023-12-13 10:05:01 +0000 | [diff] [blame] | 469 | .per_device_auto_alloc_size = sizeof(struct dm_i2c_bus), |
Googler | 695f9d9 | 2023-09-11 15:38:29 +0800 | [diff] [blame^] | 470 | .post_bind = i2c_post_bind, |
| 471 | .post_probe = i2c_post_probe, |
Googler | e00b8eb | 2019-07-08 16:37:07 -0700 | [diff] [blame] | 472 | }; |
| 473 | |
| 474 | UCLASS_DRIVER(i2c_generic) = { |
| 475 | .id = UCLASS_I2C_GENERIC, |
| 476 | .name = "i2c_generic", |
| 477 | }; |
| 478 | |
| 479 | U_BOOT_DRIVER(i2c_generic_chip_drv) = { |
| 480 | .name = "i2c_generic_chip_drv", |
| 481 | .id = UCLASS_I2C_GENERIC, |
| 482 | }; |