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