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