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