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 <i2c.h> 11c6202d85SSimon Glass #include <malloc.h> 12c6202d85SSimon Glass #include <dm/device-internal.h> 13c6202d85SSimon Glass #include <dm/lists.h> 14*aa54192dSAlexander Kochetkov #include <dm/pinctrl.h> 15*aa54192dSAlexander Kochetkov #ifdef CONFIG_DM_GPIO 16*aa54192dSAlexander Kochetkov #include <asm/gpio.h> 17*aa54192dSAlexander Kochetkov #endif 18c6202d85SSimon Glass 19c6202d85SSimon Glass #define I2C_MAX_OFFSET_LEN 4 20c6202d85SSimon Glass 21*aa54192dSAlexander Kochetkov enum { 22*aa54192dSAlexander Kochetkov PIN_SDA = 0, 23*aa54192dSAlexander Kochetkov PIN_SCL, 24*aa54192dSAlexander Kochetkov PIN_COUNT, 25*aa54192dSAlexander Kochetkov }; 26*aa54192dSAlexander Kochetkov 277d7db222SSimon Glass /* Useful debugging function */ 287d7db222SSimon Glass void i2c_dump_msgs(struct i2c_msg *msg, int nmsgs) 297d7db222SSimon Glass { 307d7db222SSimon Glass int i; 317d7db222SSimon Glass 327d7db222SSimon Glass for (i = 0; i < nmsgs; i++) { 337d7db222SSimon Glass struct i2c_msg *m = &msg[i]; 347d7db222SSimon Glass 357d7db222SSimon Glass printf(" %s %x len=%x", m->flags & I2C_M_RD ? "R" : "W", 367d7db222SSimon Glass msg->addr, msg->len); 377d7db222SSimon Glass if (!(m->flags & I2C_M_RD)) 387d7db222SSimon Glass printf(": %x", m->buf[0]); 397d7db222SSimon Glass printf("\n"); 407d7db222SSimon Glass } 417d7db222SSimon Glass } 427d7db222SSimon Glass 43c6202d85SSimon Glass /** 44c6202d85SSimon Glass * i2c_setup_offset() - Set up a new message with a chip offset 45c6202d85SSimon Glass * 46c6202d85SSimon Glass * @chip: Chip to use 47c6202d85SSimon Glass * @offset: Byte offset within chip 48c6202d85SSimon Glass * @offset_buf: Place to put byte offset 49c6202d85SSimon Glass * @msg: Message buffer 50c6202d85SSimon Glass * @return 0 if OK, -EADDRNOTAVAIL if the offset length is 0. In that case the 51c6202d85SSimon Glass * message is still set up but will not contain an offset. 52c6202d85SSimon Glass */ 53c6202d85SSimon Glass static int i2c_setup_offset(struct dm_i2c_chip *chip, uint offset, 54c6202d85SSimon Glass uint8_t offset_buf[], struct i2c_msg *msg) 55c6202d85SSimon Glass { 56c6202d85SSimon Glass int offset_len; 57c6202d85SSimon Glass 58c6202d85SSimon Glass msg->addr = chip->chip_addr; 59c6202d85SSimon Glass msg->flags = chip->flags & DM_I2C_CHIP_10BIT ? I2C_M_TEN : 0; 60c6202d85SSimon Glass msg->len = chip->offset_len; 61c6202d85SSimon Glass msg->buf = offset_buf; 62c6202d85SSimon Glass if (!chip->offset_len) 63c6202d85SSimon Glass return -EADDRNOTAVAIL; 64c6202d85SSimon Glass assert(chip->offset_len <= I2C_MAX_OFFSET_LEN); 65c6202d85SSimon Glass offset_len = chip->offset_len; 66c6202d85SSimon Glass while (offset_len--) 67c6202d85SSimon Glass *offset_buf++ = offset >> (8 * offset_len); 68c6202d85SSimon Glass 69c6202d85SSimon Glass return 0; 70c6202d85SSimon Glass } 71c6202d85SSimon Glass 72c6202d85SSimon Glass static int i2c_read_bytewise(struct udevice *dev, uint offset, 73c6202d85SSimon Glass uint8_t *buffer, int len) 74c6202d85SSimon Glass { 75e6f66ec0SSimon Glass struct dm_i2c_chip *chip = dev_get_parent_platdata(dev); 76c6202d85SSimon Glass struct udevice *bus = dev_get_parent(dev); 77c6202d85SSimon Glass struct dm_i2c_ops *ops = i2c_get_ops(bus); 78c6202d85SSimon Glass struct i2c_msg msg[2], *ptr; 79c6202d85SSimon Glass uint8_t offset_buf[I2C_MAX_OFFSET_LEN]; 80c6202d85SSimon Glass int ret; 81c6202d85SSimon Glass int i; 82c6202d85SSimon Glass 83c6202d85SSimon Glass for (i = 0; i < len; i++) { 84c6202d85SSimon Glass if (i2c_setup_offset(chip, offset + i, offset_buf, msg)) 85c6202d85SSimon Glass return -EINVAL; 86c6202d85SSimon Glass ptr = msg + 1; 87c6202d85SSimon Glass ptr->addr = chip->chip_addr; 88c6202d85SSimon Glass ptr->flags = msg->flags | I2C_M_RD; 89c6202d85SSimon Glass ptr->len = 1; 90c6202d85SSimon Glass ptr->buf = &buffer[i]; 91c6202d85SSimon Glass ptr++; 92c6202d85SSimon Glass 93c6202d85SSimon Glass ret = ops->xfer(bus, msg, ptr - msg); 94c6202d85SSimon Glass if (ret) 95c6202d85SSimon Glass return ret; 96c6202d85SSimon Glass } 97c6202d85SSimon Glass 98c6202d85SSimon Glass return 0; 99c6202d85SSimon Glass } 100c6202d85SSimon Glass 101c6202d85SSimon Glass static int i2c_write_bytewise(struct udevice *dev, uint offset, 102c6202d85SSimon Glass const uint8_t *buffer, int len) 103c6202d85SSimon Glass { 104e6f66ec0SSimon Glass struct dm_i2c_chip *chip = dev_get_parent_platdata(dev); 105c6202d85SSimon Glass struct udevice *bus = dev_get_parent(dev); 106c6202d85SSimon Glass struct dm_i2c_ops *ops = i2c_get_ops(bus); 107c6202d85SSimon Glass struct i2c_msg msg[1]; 108c6202d85SSimon Glass uint8_t buf[I2C_MAX_OFFSET_LEN + 1]; 109c6202d85SSimon Glass int ret; 110c6202d85SSimon Glass int i; 111c6202d85SSimon Glass 112c6202d85SSimon Glass for (i = 0; i < len; i++) { 113c6202d85SSimon Glass if (i2c_setup_offset(chip, offset + i, buf, msg)) 114c6202d85SSimon Glass return -EINVAL; 115c6202d85SSimon Glass buf[msg->len++] = buffer[i]; 116c6202d85SSimon Glass 117c6202d85SSimon Glass ret = ops->xfer(bus, msg, 1); 118c6202d85SSimon Glass if (ret) 119c6202d85SSimon Glass return ret; 120c6202d85SSimon Glass } 121c6202d85SSimon Glass 122c6202d85SSimon Glass return 0; 123c6202d85SSimon Glass } 124c6202d85SSimon Glass 125f9a4c2daSSimon Glass int dm_i2c_read(struct udevice *dev, uint offset, uint8_t *buffer, int len) 126c6202d85SSimon Glass { 127e6f66ec0SSimon Glass struct dm_i2c_chip *chip = dev_get_parent_platdata(dev); 128c6202d85SSimon Glass struct udevice *bus = dev_get_parent(dev); 129c6202d85SSimon Glass struct dm_i2c_ops *ops = i2c_get_ops(bus); 130c6202d85SSimon Glass struct i2c_msg msg[2], *ptr; 131c6202d85SSimon Glass uint8_t offset_buf[I2C_MAX_OFFSET_LEN]; 132c6202d85SSimon Glass int msg_count; 133c6202d85SSimon Glass 134c6202d85SSimon Glass if (!ops->xfer) 135c6202d85SSimon Glass return -ENOSYS; 136c6202d85SSimon Glass if (chip->flags & DM_I2C_CHIP_RD_ADDRESS) 137c6202d85SSimon Glass return i2c_read_bytewise(dev, offset, buffer, len); 138c6202d85SSimon Glass ptr = msg; 139c6202d85SSimon Glass if (!i2c_setup_offset(chip, offset, offset_buf, ptr)) 140c6202d85SSimon Glass ptr++; 141c6202d85SSimon Glass 142c6202d85SSimon Glass if (len) { 143c6202d85SSimon Glass ptr->addr = chip->chip_addr; 144c6202d85SSimon Glass ptr->flags = chip->flags & DM_I2C_CHIP_10BIT ? I2C_M_TEN : 0; 145c6202d85SSimon Glass ptr->flags |= I2C_M_RD; 146c6202d85SSimon Glass ptr->len = len; 147c6202d85SSimon Glass ptr->buf = buffer; 148c6202d85SSimon Glass ptr++; 149c6202d85SSimon Glass } 150c6202d85SSimon Glass msg_count = ptr - msg; 151c6202d85SSimon Glass 152c6202d85SSimon Glass return ops->xfer(bus, msg, msg_count); 153c6202d85SSimon Glass } 154c6202d85SSimon Glass 155f9a4c2daSSimon Glass int dm_i2c_write(struct udevice *dev, uint offset, const uint8_t *buffer, 156f9a4c2daSSimon Glass int len) 157c6202d85SSimon Glass { 158e6f66ec0SSimon Glass struct dm_i2c_chip *chip = dev_get_parent_platdata(dev); 159c6202d85SSimon Glass struct udevice *bus = dev_get_parent(dev); 160c6202d85SSimon Glass struct dm_i2c_ops *ops = i2c_get_ops(bus); 161c6202d85SSimon Glass struct i2c_msg msg[1]; 162c6202d85SSimon Glass 163c6202d85SSimon Glass if (!ops->xfer) 164c6202d85SSimon Glass return -ENOSYS; 165c6202d85SSimon Glass 166c6202d85SSimon Glass if (chip->flags & DM_I2C_CHIP_WR_ADDRESS) 167c6202d85SSimon Glass return i2c_write_bytewise(dev, offset, buffer, len); 168c6202d85SSimon Glass /* 169c6202d85SSimon Glass * The simple approach would be to send two messages here: one to 170c6202d85SSimon Glass * set the offset and one to write the bytes. However some drivers 171c6202d85SSimon Glass * will not be expecting this, and some chips won't like how the 172c6202d85SSimon Glass * driver presents this on the I2C bus. 173c6202d85SSimon Glass * 174c6202d85SSimon Glass * The API does not support separate offset and data. We could extend 175c6202d85SSimon Glass * it with a flag indicating that there is data in the next message 176c6202d85SSimon Glass * that needs to be processed in the same transaction. We could 177c6202d85SSimon Glass * instead add an additional buffer to each message. For now, handle 178c6202d85SSimon Glass * this in the uclass since it isn't clear what the impact on drivers 179c6202d85SSimon Glass * would be with this extra complication. Unfortunately this means 180c6202d85SSimon Glass * copying the message. 181c6202d85SSimon Glass * 182c6202d85SSimon Glass * Use the stack for small messages, malloc() for larger ones. We 183c6202d85SSimon Glass * need to allow space for the offset (up to 4 bytes) and the message 184c6202d85SSimon Glass * itself. 185c6202d85SSimon Glass */ 186c6202d85SSimon Glass if (len < 64) { 187c6202d85SSimon Glass uint8_t buf[I2C_MAX_OFFSET_LEN + len]; 188c6202d85SSimon Glass 189c6202d85SSimon Glass i2c_setup_offset(chip, offset, buf, msg); 190c6202d85SSimon Glass msg->len += len; 191c6202d85SSimon Glass memcpy(buf + chip->offset_len, buffer, len); 192c6202d85SSimon Glass 193c6202d85SSimon Glass return ops->xfer(bus, msg, 1); 194c6202d85SSimon Glass } else { 195c6202d85SSimon Glass uint8_t *buf; 196c6202d85SSimon Glass int ret; 197c6202d85SSimon Glass 198c6202d85SSimon Glass buf = malloc(I2C_MAX_OFFSET_LEN + len); 199c6202d85SSimon Glass if (!buf) 200c6202d85SSimon Glass return -ENOMEM; 201c6202d85SSimon Glass i2c_setup_offset(chip, offset, buf, msg); 202c6202d85SSimon Glass msg->len += len; 203c6202d85SSimon Glass memcpy(buf + chip->offset_len, buffer, len); 204c6202d85SSimon Glass 205c6202d85SSimon Glass ret = ops->xfer(bus, msg, 1); 206c6202d85SSimon Glass free(buf); 207c6202d85SSimon Glass return ret; 208c6202d85SSimon Glass } 209c6202d85SSimon Glass } 210c6202d85SSimon Glass 211df358c6bSSimon Glass int dm_i2c_xfer(struct udevice *dev, struct i2c_msg *msg, int nmsgs) 212df358c6bSSimon Glass { 213df358c6bSSimon Glass struct udevice *bus = dev_get_parent(dev); 214df358c6bSSimon Glass struct dm_i2c_ops *ops = i2c_get_ops(bus); 215df358c6bSSimon Glass 216df358c6bSSimon Glass if (!ops->xfer) 217df358c6bSSimon Glass return -ENOSYS; 218df358c6bSSimon Glass 219df358c6bSSimon Glass return ops->xfer(bus, msg, nmsgs); 220df358c6bSSimon Glass } 221df358c6bSSimon Glass 222ba3864f8SSimon Glass int dm_i2c_reg_read(struct udevice *dev, uint offset) 223ba3864f8SSimon Glass { 224ba3864f8SSimon Glass uint8_t val; 225ba3864f8SSimon Glass int ret; 226ba3864f8SSimon Glass 227ba3864f8SSimon Glass ret = dm_i2c_read(dev, offset, &val, 1); 228ba3864f8SSimon Glass if (ret < 0) 229ba3864f8SSimon Glass return ret; 230ba3864f8SSimon Glass 231ba3864f8SSimon Glass return val; 232ba3864f8SSimon Glass } 233ba3864f8SSimon Glass 234ba3864f8SSimon Glass int dm_i2c_reg_write(struct udevice *dev, uint offset, uint value) 235ba3864f8SSimon Glass { 236ba3864f8SSimon Glass uint8_t val = value; 237ba3864f8SSimon Glass 238ba3864f8SSimon Glass return dm_i2c_write(dev, offset, &val, 1); 239ba3864f8SSimon Glass } 240ba3864f8SSimon Glass 241c6202d85SSimon Glass /** 242c6202d85SSimon Glass * i2c_probe_chip() - probe for a chip on a bus 243c6202d85SSimon Glass * 244c6202d85SSimon Glass * @bus: Bus to probe 245c6202d85SSimon Glass * @chip_addr: Chip address to probe 246c6202d85SSimon Glass * @flags: Flags for the chip 247c6202d85SSimon Glass * @return 0 if found, -ENOSYS if the driver is invalid, -EREMOTEIO if the chip 248c6202d85SSimon Glass * does not respond to probe 249c6202d85SSimon Glass */ 250c6202d85SSimon Glass static int i2c_probe_chip(struct udevice *bus, uint chip_addr, 251c6202d85SSimon Glass enum dm_i2c_chip_flags chip_flags) 252c6202d85SSimon Glass { 253c6202d85SSimon Glass struct dm_i2c_ops *ops = i2c_get_ops(bus); 254c6202d85SSimon Glass struct i2c_msg msg[1]; 255c6202d85SSimon Glass int ret; 256c6202d85SSimon Glass 257c6202d85SSimon Glass if (ops->probe_chip) { 258c6202d85SSimon Glass ret = ops->probe_chip(bus, chip_addr, chip_flags); 259c6202d85SSimon Glass if (!ret || ret != -ENOSYS) 260c6202d85SSimon Glass return ret; 261c6202d85SSimon Glass } 262c6202d85SSimon Glass 263c6202d85SSimon Glass if (!ops->xfer) 264c6202d85SSimon Glass return -ENOSYS; 265c6202d85SSimon Glass 266c6202d85SSimon Glass /* Probe with a zero-length message */ 267c6202d85SSimon Glass msg->addr = chip_addr; 268c6202d85SSimon Glass msg->flags = chip_flags & DM_I2C_CHIP_10BIT ? I2C_M_TEN : 0; 269c6202d85SSimon Glass msg->len = 0; 270c6202d85SSimon Glass msg->buf = NULL; 271c6202d85SSimon Glass 272c6202d85SSimon Glass return ops->xfer(bus, msg, 1); 273c6202d85SSimon Glass } 274c6202d85SSimon Glass 27525ab4b03SSimon Glass static int i2c_bind_driver(struct udevice *bus, uint chip_addr, uint offset_len, 276c6202d85SSimon Glass struct udevice **devp) 277c6202d85SSimon Glass { 278e6f66ec0SSimon Glass struct dm_i2c_chip *chip; 279c6202d85SSimon Glass char name[30], *str; 280c6202d85SSimon Glass struct udevice *dev; 281c6202d85SSimon Glass int ret; 282c6202d85SSimon Glass 283c6202d85SSimon Glass snprintf(name, sizeof(name), "generic_%x", chip_addr); 284c6202d85SSimon Glass str = strdup(name); 2855c2d23bfSSimon Glass if (!str) 2865c2d23bfSSimon Glass return -ENOMEM; 287c6202d85SSimon Glass ret = device_bind_driver(bus, "i2c_generic_chip_drv", str, &dev); 288c6202d85SSimon Glass debug("%s: device_bind_driver: ret=%d\n", __func__, ret); 289c6202d85SSimon Glass if (ret) 290c6202d85SSimon Glass goto err_bind; 291c6202d85SSimon Glass 292c6202d85SSimon Glass /* Tell the device what we know about it */ 293e6f66ec0SSimon Glass chip = dev_get_parent_platdata(dev); 294e6f66ec0SSimon Glass chip->chip_addr = chip_addr; 295e6f66ec0SSimon Glass chip->offset_len = offset_len; 296e6f66ec0SSimon Glass ret = device_probe(dev); 297e6f66ec0SSimon Glass debug("%s: device_probe: ret=%d\n", __func__, ret); 298c6202d85SSimon Glass if (ret) 299c6202d85SSimon Glass goto err_probe; 300c6202d85SSimon Glass 301c6202d85SSimon Glass *devp = dev; 302c6202d85SSimon Glass return 0; 303c6202d85SSimon Glass 304c6202d85SSimon Glass err_probe: 305e6f66ec0SSimon Glass /* 306e6f66ec0SSimon Glass * If the device failed to probe, unbind it. There is nothing there 307e6f66ec0SSimon Glass * on the bus so we don't want to leave it lying around 308e6f66ec0SSimon Glass */ 309c6202d85SSimon Glass device_unbind(dev); 310c6202d85SSimon Glass err_bind: 311c6202d85SSimon Glass free(str); 312c6202d85SSimon Glass return ret; 313c6202d85SSimon Glass } 314c6202d85SSimon Glass 31525ab4b03SSimon Glass int i2c_get_chip(struct udevice *bus, uint chip_addr, uint offset_len, 31625ab4b03SSimon Glass struct udevice **devp) 317c6202d85SSimon Glass { 318c6202d85SSimon Glass struct udevice *dev; 319c6202d85SSimon Glass 320c6202d85SSimon Glass debug("%s: Searching bus '%s' for address %02x: ", __func__, 321c6202d85SSimon Glass bus->name, chip_addr); 322c6202d85SSimon Glass for (device_find_first_child(bus, &dev); dev; 323c6202d85SSimon Glass device_find_next_child(&dev)) { 324e6f66ec0SSimon Glass struct dm_i2c_chip *chip = dev_get_parent_platdata(dev); 325c6202d85SSimon Glass int ret; 326c6202d85SSimon Glass 327c6202d85SSimon Glass if (chip->chip_addr == chip_addr) { 328c6202d85SSimon Glass ret = device_probe(dev); 329c6202d85SSimon Glass debug("found, ret=%d\n", ret); 330c6202d85SSimon Glass if (ret) 331c6202d85SSimon Glass return ret; 332c6202d85SSimon Glass *devp = dev; 333c6202d85SSimon Glass return 0; 334c6202d85SSimon Glass } 335c6202d85SSimon Glass } 336c6202d85SSimon Glass debug("not found\n"); 33725ab4b03SSimon Glass return i2c_bind_driver(bus, chip_addr, offset_len, devp); 338c6202d85SSimon Glass } 339c6202d85SSimon Glass 34025ab4b03SSimon Glass int i2c_get_chip_for_busnum(int busnum, int chip_addr, uint offset_len, 34125ab4b03SSimon Glass struct udevice **devp) 342c6202d85SSimon Glass { 343c6202d85SSimon Glass struct udevice *bus; 344c6202d85SSimon Glass int ret; 345c6202d85SSimon Glass 346c6202d85SSimon Glass ret = uclass_get_device_by_seq(UCLASS_I2C, busnum, &bus); 347c6202d85SSimon Glass if (ret) { 348c6202d85SSimon Glass debug("Cannot find I2C bus %d\n", busnum); 349c6202d85SSimon Glass return ret; 350c6202d85SSimon Glass } 35125ab4b03SSimon Glass ret = i2c_get_chip(bus, chip_addr, offset_len, devp); 352c6202d85SSimon Glass if (ret) { 353c6202d85SSimon Glass debug("Cannot find I2C chip %02x on bus %d\n", chip_addr, 354c6202d85SSimon Glass busnum); 355c6202d85SSimon Glass return ret; 356c6202d85SSimon Glass } 357c6202d85SSimon Glass 358c6202d85SSimon Glass return 0; 359c6202d85SSimon Glass } 360c6202d85SSimon Glass 361f9a4c2daSSimon Glass int dm_i2c_probe(struct udevice *bus, uint chip_addr, uint chip_flags, 362c6202d85SSimon Glass struct udevice **devp) 363c6202d85SSimon Glass { 364c6202d85SSimon Glass int ret; 365c6202d85SSimon Glass 366c6202d85SSimon Glass *devp = NULL; 367c6202d85SSimon Glass 368c6202d85SSimon Glass /* First probe that chip */ 369c6202d85SSimon Glass ret = i2c_probe_chip(bus, chip_addr, chip_flags); 370c6202d85SSimon Glass debug("%s: bus='%s', address %02x, ret=%d\n", __func__, bus->name, 371c6202d85SSimon Glass chip_addr, ret); 372c6202d85SSimon Glass if (ret) 373c6202d85SSimon Glass return ret; 374c6202d85SSimon Glass 375c6202d85SSimon Glass /* The chip was found, see if we have a driver, and probe it */ 37625ab4b03SSimon Glass ret = i2c_get_chip(bus, chip_addr, 1, devp); 377c6202d85SSimon Glass debug("%s: i2c_get_chip: ret=%d\n", __func__, ret); 378c6202d85SSimon Glass 379c6202d85SSimon Glass return ret; 380c6202d85SSimon Glass } 381c6202d85SSimon Glass 382ca88b9b9SSimon Glass int dm_i2c_set_bus_speed(struct udevice *bus, unsigned int speed) 383c6202d85SSimon Glass { 384c6202d85SSimon Glass struct dm_i2c_ops *ops = i2c_get_ops(bus); 385e564f054SSimon Glass struct dm_i2c_bus *i2c = dev_get_uclass_priv(bus); 386c6202d85SSimon Glass int ret; 387c6202d85SSimon Glass 388c6202d85SSimon Glass /* 389c6202d85SSimon Glass * If we have a method, call it. If not then the driver probably wants 390c6202d85SSimon Glass * to deal with speed changes on the next transfer. It can easily read 391c6202d85SSimon Glass * the current speed from this uclass 392c6202d85SSimon Glass */ 393c6202d85SSimon Glass if (ops->set_bus_speed) { 394c6202d85SSimon Glass ret = ops->set_bus_speed(bus, speed); 395c6202d85SSimon Glass if (ret) 396c6202d85SSimon Glass return ret; 397c6202d85SSimon Glass } 398c6202d85SSimon Glass i2c->speed_hz = speed; 399c6202d85SSimon Glass 400c6202d85SSimon Glass return 0; 401c6202d85SSimon Glass } 402c6202d85SSimon Glass 403ca88b9b9SSimon Glass int dm_i2c_get_bus_speed(struct udevice *bus) 404c6202d85SSimon Glass { 405c6202d85SSimon Glass struct dm_i2c_ops *ops = i2c_get_ops(bus); 406e564f054SSimon Glass struct dm_i2c_bus *i2c = dev_get_uclass_priv(bus); 407c6202d85SSimon Glass 408c6202d85SSimon Glass if (!ops->get_bus_speed) 409c6202d85SSimon Glass return i2c->speed_hz; 410c6202d85SSimon Glass 411c6202d85SSimon Glass return ops->get_bus_speed(bus); 412c6202d85SSimon Glass } 413c6202d85SSimon Glass 414c6202d85SSimon Glass int i2c_set_chip_flags(struct udevice *dev, uint flags) 415c6202d85SSimon Glass { 416c6202d85SSimon Glass struct udevice *bus = dev->parent; 417e6f66ec0SSimon Glass struct dm_i2c_chip *chip = dev_get_parent_platdata(dev); 418c6202d85SSimon Glass struct dm_i2c_ops *ops = i2c_get_ops(bus); 419c6202d85SSimon Glass int ret; 420c6202d85SSimon Glass 421c6202d85SSimon Glass if (ops->set_flags) { 422c6202d85SSimon Glass ret = ops->set_flags(dev, flags); 423c6202d85SSimon Glass if (ret) 424c6202d85SSimon Glass return ret; 425c6202d85SSimon Glass } 426c6202d85SSimon Glass chip->flags = flags; 427c6202d85SSimon Glass 428c6202d85SSimon Glass return 0; 429c6202d85SSimon Glass } 430c6202d85SSimon Glass 431c6202d85SSimon Glass int i2c_get_chip_flags(struct udevice *dev, uint *flagsp) 432c6202d85SSimon Glass { 433e6f66ec0SSimon Glass struct dm_i2c_chip *chip = dev_get_parent_platdata(dev); 434c6202d85SSimon Glass 435c6202d85SSimon Glass *flagsp = chip->flags; 436c6202d85SSimon Glass 437c6202d85SSimon Glass return 0; 438c6202d85SSimon Glass } 439c6202d85SSimon Glass 440c6202d85SSimon Glass int i2c_set_chip_offset_len(struct udevice *dev, uint offset_len) 441c6202d85SSimon Glass { 442e6f66ec0SSimon Glass struct dm_i2c_chip *chip = dev_get_parent_platdata(dev); 443c6202d85SSimon Glass 444c6202d85SSimon Glass if (offset_len > I2C_MAX_OFFSET_LEN) 445c6202d85SSimon Glass return -EINVAL; 446c6202d85SSimon Glass chip->offset_len = offset_len; 447c6202d85SSimon Glass 448c6202d85SSimon Glass return 0; 449c6202d85SSimon Glass } 450c6202d85SSimon Glass 45101501804SSimon Glass int i2c_get_chip_offset_len(struct udevice *dev) 45201501804SSimon Glass { 45301501804SSimon Glass struct dm_i2c_chip *chip = dev_get_parent_platdata(dev); 45401501804SSimon Glass 45501501804SSimon Glass return chip->offset_len; 45601501804SSimon Glass } 45701501804SSimon Glass 458*aa54192dSAlexander Kochetkov #ifdef CONFIG_DM_GPIO 459*aa54192dSAlexander Kochetkov static void i2c_gpio_set_pin(struct gpio_desc *pin, int bit) 460*aa54192dSAlexander Kochetkov { 461*aa54192dSAlexander Kochetkov if (bit) 462*aa54192dSAlexander Kochetkov dm_gpio_set_dir_flags(pin, GPIOD_IS_IN); 463*aa54192dSAlexander Kochetkov else 464*aa54192dSAlexander Kochetkov dm_gpio_set_dir_flags(pin, GPIOD_IS_OUT | 465*aa54192dSAlexander Kochetkov GPIOD_ACTIVE_LOW | 466*aa54192dSAlexander Kochetkov GPIOD_IS_OUT_ACTIVE); 467*aa54192dSAlexander Kochetkov } 468*aa54192dSAlexander Kochetkov 469*aa54192dSAlexander Kochetkov static int i2c_gpio_get_pin(struct gpio_desc *pin) 470*aa54192dSAlexander Kochetkov { 471*aa54192dSAlexander Kochetkov return dm_gpio_get_value(pin); 472*aa54192dSAlexander Kochetkov } 473*aa54192dSAlexander Kochetkov 474*aa54192dSAlexander Kochetkov static int i2c_deblock_gpio_loop(struct gpio_desc *sda_pin, 475*aa54192dSAlexander Kochetkov struct gpio_desc *scl_pin) 476*aa54192dSAlexander Kochetkov { 477*aa54192dSAlexander Kochetkov int counter = 9; 478*aa54192dSAlexander Kochetkov int ret = 0; 479*aa54192dSAlexander Kochetkov 480*aa54192dSAlexander Kochetkov i2c_gpio_set_pin(sda_pin, 1); 481*aa54192dSAlexander Kochetkov i2c_gpio_set_pin(scl_pin, 1); 482*aa54192dSAlexander Kochetkov udelay(5); 483*aa54192dSAlexander Kochetkov 484*aa54192dSAlexander Kochetkov /* Toggle SCL until slave release SDA */ 485*aa54192dSAlexander Kochetkov while (counter-- >= 0) { 486*aa54192dSAlexander Kochetkov i2c_gpio_set_pin(scl_pin, 1); 487*aa54192dSAlexander Kochetkov udelay(5); 488*aa54192dSAlexander Kochetkov i2c_gpio_set_pin(scl_pin, 0); 489*aa54192dSAlexander Kochetkov udelay(5); 490*aa54192dSAlexander Kochetkov if (i2c_gpio_get_pin(sda_pin)) 491*aa54192dSAlexander Kochetkov break; 492*aa54192dSAlexander Kochetkov } 493*aa54192dSAlexander Kochetkov 494*aa54192dSAlexander Kochetkov /* Then, send I2C stop */ 495*aa54192dSAlexander Kochetkov i2c_gpio_set_pin(sda_pin, 0); 496*aa54192dSAlexander Kochetkov udelay(5); 497*aa54192dSAlexander Kochetkov 498*aa54192dSAlexander Kochetkov i2c_gpio_set_pin(scl_pin, 1); 499*aa54192dSAlexander Kochetkov udelay(5); 500*aa54192dSAlexander Kochetkov 501*aa54192dSAlexander Kochetkov i2c_gpio_set_pin(sda_pin, 1); 502*aa54192dSAlexander Kochetkov udelay(5); 503*aa54192dSAlexander Kochetkov 504*aa54192dSAlexander Kochetkov if (!i2c_gpio_get_pin(sda_pin) || !i2c_gpio_get_pin(scl_pin)) 505*aa54192dSAlexander Kochetkov ret = -EREMOTEIO; 506*aa54192dSAlexander Kochetkov 507*aa54192dSAlexander Kochetkov return ret; 508*aa54192dSAlexander Kochetkov } 509*aa54192dSAlexander Kochetkov 510*aa54192dSAlexander Kochetkov static int i2c_deblock_gpio(struct udevice *bus) 511*aa54192dSAlexander Kochetkov { 512*aa54192dSAlexander Kochetkov struct gpio_desc gpios[PIN_COUNT]; 513*aa54192dSAlexander Kochetkov int ret, ret0; 514*aa54192dSAlexander Kochetkov 515*aa54192dSAlexander Kochetkov ret = gpio_request_list_by_name(bus, "gpios", gpios, 516*aa54192dSAlexander Kochetkov ARRAY_SIZE(gpios), GPIOD_IS_IN); 517*aa54192dSAlexander Kochetkov if (ret != ARRAY_SIZE(gpios)) { 518*aa54192dSAlexander Kochetkov debug("%s: I2C Node '%s' has no 'gpios' property %s\n", 519*aa54192dSAlexander Kochetkov __func__, dev_read_name(bus), bus->name); 520*aa54192dSAlexander Kochetkov if (ret >= 0) { 521*aa54192dSAlexander Kochetkov gpio_free_list(bus, gpios, ret); 522*aa54192dSAlexander Kochetkov ret = -ENOENT; 523*aa54192dSAlexander Kochetkov } 524*aa54192dSAlexander Kochetkov goto out; 525*aa54192dSAlexander Kochetkov } 526*aa54192dSAlexander Kochetkov 527*aa54192dSAlexander Kochetkov ret = pinctrl_select_state(bus, "gpio"); 528*aa54192dSAlexander Kochetkov if (ret) { 529*aa54192dSAlexander Kochetkov debug("%s: I2C Node '%s' has no 'gpio' pinctrl state. %s\n", 530*aa54192dSAlexander Kochetkov __func__, dev_read_name(bus), bus->name); 531*aa54192dSAlexander Kochetkov goto out_no_pinctrl; 532*aa54192dSAlexander Kochetkov } 533*aa54192dSAlexander Kochetkov 534*aa54192dSAlexander Kochetkov ret0 = i2c_deblock_gpio_loop(&gpios[PIN_SDA], &gpios[PIN_SCL]); 535*aa54192dSAlexander Kochetkov 536*aa54192dSAlexander Kochetkov ret = pinctrl_select_state(bus, "default"); 537*aa54192dSAlexander Kochetkov if (ret) { 538*aa54192dSAlexander Kochetkov debug("%s: I2C Node '%s' has no 'default' pinctrl state. %s\n", 539*aa54192dSAlexander Kochetkov __func__, dev_read_name(bus), bus->name); 540*aa54192dSAlexander Kochetkov } 541*aa54192dSAlexander Kochetkov 542*aa54192dSAlexander Kochetkov ret = !ret ? ret0 : ret; 543*aa54192dSAlexander Kochetkov 544*aa54192dSAlexander Kochetkov out_no_pinctrl: 545*aa54192dSAlexander Kochetkov gpio_free_list(bus, gpios, ARRAY_SIZE(gpios)); 546*aa54192dSAlexander Kochetkov out: 547*aa54192dSAlexander Kochetkov return ret; 548*aa54192dSAlexander Kochetkov } 549*aa54192dSAlexander Kochetkov #else 550*aa54192dSAlexander Kochetkov static int i2c_deblock_gpio(struct udevice *bus) 551*aa54192dSAlexander Kochetkov { 552*aa54192dSAlexander Kochetkov return -ENOSYS; 553*aa54192dSAlexander Kochetkov } 554*aa54192dSAlexander Kochetkov #endif // CONFIG_DM_GPIO 555*aa54192dSAlexander Kochetkov 556c6202d85SSimon Glass int i2c_deblock(struct udevice *bus) 557c6202d85SSimon Glass { 558c6202d85SSimon Glass struct dm_i2c_ops *ops = i2c_get_ops(bus); 559c6202d85SSimon Glass 560c6202d85SSimon Glass if (!ops->deblock) 561*aa54192dSAlexander Kochetkov return i2c_deblock_gpio(bus); 562c6202d85SSimon Glass 563c6202d85SSimon Glass return ops->deblock(bus); 564c6202d85SSimon Glass } 565c6202d85SSimon Glass 5665142ac79SMugunthan V N #if CONFIG_IS_ENABLED(OF_CONTROL) 5671704308eSSimon Glass int i2c_chip_ofdata_to_platdata(struct udevice *dev, struct dm_i2c_chip *chip) 568c6202d85SSimon Glass { 5691704308eSSimon Glass int addr; 5701704308eSSimon Glass 5711704308eSSimon Glass chip->offset_len = dev_read_u32_default(dev, "u-boot,i2c-offset-len", 5721704308eSSimon Glass 1); 573c6202d85SSimon Glass chip->flags = 0; 5741704308eSSimon Glass addr = dev_read_u32_default(dev, "reg", -1); 5751704308eSSimon Glass if (addr == -1) { 5761704308eSSimon Glass debug("%s: I2C Node '%s' has no 'reg' property %s\n", __func__, 5771704308eSSimon Glass dev_read_name(dev), dev->name); 578c6202d85SSimon Glass return -EINVAL; 579c6202d85SSimon Glass } 5801704308eSSimon Glass chip->chip_addr = addr; 581c6202d85SSimon Glass 582c6202d85SSimon Glass return 0; 583c6202d85SSimon Glass } 5845142ac79SMugunthan V N #endif 585c6202d85SSimon Glass 586c6202d85SSimon Glass static int i2c_post_probe(struct udevice *dev) 587c6202d85SSimon Glass { 5885142ac79SMugunthan V N #if CONFIG_IS_ENABLED(OF_CONTROL) 589e564f054SSimon Glass struct dm_i2c_bus *i2c = dev_get_uclass_priv(dev); 590c6202d85SSimon Glass 5911704308eSSimon Glass i2c->speed_hz = dev_read_u32_default(dev, "clock-frequency", 100000); 592c6202d85SSimon Glass 593ca88b9b9SSimon Glass return dm_i2c_set_bus_speed(dev, i2c->speed_hz); 5945142ac79SMugunthan V N #else 5955142ac79SMugunthan V N return 0; 5965142ac79SMugunthan V N #endif 597c6202d85SSimon Glass } 598c6202d85SSimon Glass 599e6f66ec0SSimon Glass static int i2c_child_post_bind(struct udevice *dev) 600e6f66ec0SSimon Glass { 6015142ac79SMugunthan V N #if CONFIG_IS_ENABLED(OF_CONTROL) 602e6f66ec0SSimon Glass struct dm_i2c_chip *plat = dev_get_parent_platdata(dev); 603e6f66ec0SSimon Glass 6041704308eSSimon Glass if (!dev_of_valid(dev)) 605e6f66ec0SSimon Glass return 0; 6061704308eSSimon Glass return i2c_chip_ofdata_to_platdata(dev, plat); 6075142ac79SMugunthan V N #else 6085142ac79SMugunthan V N return 0; 6095142ac79SMugunthan V N #endif 610e6f66ec0SSimon Glass } 611e6f66ec0SSimon Glass 612c6202d85SSimon Glass UCLASS_DRIVER(i2c) = { 613c6202d85SSimon Glass .id = UCLASS_I2C, 614c6202d85SSimon Glass .name = "i2c", 6159cc36a2bSSimon Glass .flags = DM_UC_FLAG_SEQ_ALIAS, 61691195485SSimon Glass #if CONFIG_IS_ENABLED(OF_CONTROL) 61791195485SSimon Glass .post_bind = dm_scan_fdt_dev, 61891195485SSimon Glass #endif 619c6202d85SSimon Glass .post_probe = i2c_post_probe, 620e6f66ec0SSimon Glass .per_device_auto_alloc_size = sizeof(struct dm_i2c_bus), 621e6f66ec0SSimon Glass .per_child_platdata_auto_alloc_size = sizeof(struct dm_i2c_chip), 622e6f66ec0SSimon Glass .child_post_bind = i2c_child_post_bind, 623c6202d85SSimon Glass }; 624c6202d85SSimon Glass 625c6202d85SSimon Glass UCLASS_DRIVER(i2c_generic) = { 626c6202d85SSimon Glass .id = UCLASS_I2C_GENERIC, 627c6202d85SSimon Glass .name = "i2c_generic", 628c6202d85SSimon Glass }; 629c6202d85SSimon Glass 630c6202d85SSimon Glass U_BOOT_DRIVER(i2c_generic_chip_drv) = { 631c6202d85SSimon Glass .name = "i2c_generic_chip_drv", 632c6202d85SSimon Glass .id = UCLASS_I2C_GENERIC, 633c6202d85SSimon Glass }; 634