2 * Copyright (c) 2014 Google, Inc
4 * SPDX-License-Identifier: GPL-2.0+
13 #include <dm/device-internal.h>
16 DECLARE_GLOBAL_DATA_PTR;
18 #define I2C_MAX_OFFSET_LEN 4
20 /* Useful debugging function */
21 void i2c_dump_msgs(struct i2c_msg *msg, int nmsgs)
25 for (i = 0; i < nmsgs; i++) {
26 struct i2c_msg *m = &msg[i];
28 printf(" %s %x len=%x", m->flags & I2C_M_RD ? "R" : "W",
30 if (!(m->flags & I2C_M_RD))
31 printf(": %x", m->buf[0]);
37 * i2c_setup_offset() - Set up a new message with a chip offset
40 * @offset: Byte offset within chip
41 * @offset_buf: Place to put byte offset
42 * @msg: Message buffer
43 * @return 0 if OK, -EADDRNOTAVAIL if the offset length is 0. In that case the
44 * message is still set up but will not contain an offset.
46 static int i2c_setup_offset(struct dm_i2c_chip *chip, uint offset,
47 uint8_t offset_buf[], struct i2c_msg *msg)
51 msg->addr = chip->chip_addr;
52 msg->flags = chip->flags & DM_I2C_CHIP_10BIT ? I2C_M_TEN : 0;
53 msg->len = chip->offset_len;
54 msg->buf = offset_buf;
55 if (!chip->offset_len)
56 return -EADDRNOTAVAIL;
57 assert(chip->offset_len <= I2C_MAX_OFFSET_LEN);
58 offset_len = chip->offset_len;
60 *offset_buf++ = offset >> (8 * offset_len);
65 static int i2c_read_bytewise(struct udevice *dev, uint offset,
66 uint8_t *buffer, int len)
68 struct dm_i2c_chip *chip = dev_get_parent_platdata(dev);
69 struct udevice *bus = dev_get_parent(dev);
70 struct dm_i2c_ops *ops = i2c_get_ops(bus);
71 struct i2c_msg msg[2], *ptr;
72 uint8_t offset_buf[I2C_MAX_OFFSET_LEN];
76 for (i = 0; i < len; i++) {
77 if (i2c_setup_offset(chip, offset + i, offset_buf, msg))
80 ptr->addr = chip->chip_addr;
81 ptr->flags = msg->flags | I2C_M_RD;
83 ptr->buf = &buffer[i];
86 ret = ops->xfer(bus, msg, ptr - msg);
94 static int i2c_write_bytewise(struct udevice *dev, uint offset,
95 const uint8_t *buffer, int len)
97 struct dm_i2c_chip *chip = dev_get_parent_platdata(dev);
98 struct udevice *bus = dev_get_parent(dev);
99 struct dm_i2c_ops *ops = i2c_get_ops(bus);
100 struct i2c_msg msg[1];
101 uint8_t buf[I2C_MAX_OFFSET_LEN + 1];
105 for (i = 0; i < len; i++) {
106 if (i2c_setup_offset(chip, offset + i, buf, msg))
108 buf[msg->len++] = buffer[i];
110 ret = ops->xfer(bus, msg, 1);
118 int dm_i2c_read(struct udevice *dev, uint offset, uint8_t *buffer, int len)
120 struct dm_i2c_chip *chip = dev_get_parent_platdata(dev);
121 struct udevice *bus = dev_get_parent(dev);
122 struct dm_i2c_ops *ops = i2c_get_ops(bus);
123 struct i2c_msg msg[2], *ptr;
124 uint8_t offset_buf[I2C_MAX_OFFSET_LEN];
129 if (chip->flags & DM_I2C_CHIP_RD_ADDRESS)
130 return i2c_read_bytewise(dev, offset, buffer, len);
132 if (!i2c_setup_offset(chip, offset, offset_buf, ptr))
136 ptr->addr = chip->chip_addr;
137 ptr->flags = chip->flags & DM_I2C_CHIP_10BIT ? I2C_M_TEN : 0;
138 ptr->flags |= I2C_M_RD;
143 msg_count = ptr - msg;
145 return ops->xfer(bus, msg, msg_count);
148 int dm_i2c_write(struct udevice *dev, uint offset, const uint8_t *buffer,
151 struct dm_i2c_chip *chip = dev_get_parent_platdata(dev);
152 struct udevice *bus = dev_get_parent(dev);
153 struct dm_i2c_ops *ops = i2c_get_ops(bus);
154 struct i2c_msg msg[1];
159 if (chip->flags & DM_I2C_CHIP_WR_ADDRESS)
160 return i2c_write_bytewise(dev, offset, buffer, len);
162 * The simple approach would be to send two messages here: one to
163 * set the offset and one to write the bytes. However some drivers
164 * will not be expecting this, and some chips won't like how the
165 * driver presents this on the I2C bus.
167 * The API does not support separate offset and data. We could extend
168 * it with a flag indicating that there is data in the next message
169 * that needs to be processed in the same transaction. We could
170 * instead add an additional buffer to each message. For now, handle
171 * this in the uclass since it isn't clear what the impact on drivers
172 * would be with this extra complication. Unfortunately this means
173 * copying the message.
175 * Use the stack for small messages, malloc() for larger ones. We
176 * need to allow space for the offset (up to 4 bytes) and the message
180 uint8_t buf[I2C_MAX_OFFSET_LEN + len];
182 i2c_setup_offset(chip, offset, buf, msg);
184 memcpy(buf + chip->offset_len, buffer, len);
186 return ops->xfer(bus, msg, 1);
191 buf = malloc(I2C_MAX_OFFSET_LEN + len);
194 i2c_setup_offset(chip, offset, buf, msg);
196 memcpy(buf + chip->offset_len, buffer, len);
198 ret = ops->xfer(bus, msg, 1);
204 int dm_i2c_xfer(struct udevice *dev, struct i2c_msg *msg, int nmsgs)
206 struct udevice *bus = dev_get_parent(dev);
207 struct dm_i2c_ops *ops = i2c_get_ops(bus);
212 return ops->xfer(bus, msg, nmsgs);
215 int dm_i2c_reg_read(struct udevice *dev, uint offset)
220 ret = dm_i2c_read(dev, offset, &val, 1);
227 int dm_i2c_reg_write(struct udevice *dev, uint offset, uint value)
231 return dm_i2c_write(dev, offset, &val, 1);
235 * i2c_probe_chip() - probe for a chip on a bus
238 * @chip_addr: Chip address to probe
239 * @flags: Flags for the chip
240 * @return 0 if found, -ENOSYS if the driver is invalid, -EREMOTEIO if the chip
241 * does not respond to probe
243 static int i2c_probe_chip(struct udevice *bus, uint chip_addr,
244 enum dm_i2c_chip_flags chip_flags)
246 struct dm_i2c_ops *ops = i2c_get_ops(bus);
247 struct i2c_msg msg[1];
250 if (ops->probe_chip) {
251 ret = ops->probe_chip(bus, chip_addr, chip_flags);
252 if (!ret || ret != -ENOSYS)
259 /* Probe with a zero-length message */
260 msg->addr = chip_addr;
261 msg->flags = chip_flags & DM_I2C_CHIP_10BIT ? I2C_M_TEN : 0;
265 return ops->xfer(bus, msg, 1);
268 static int i2c_bind_driver(struct udevice *bus, uint chip_addr, uint offset_len,
269 struct udevice **devp)
271 struct dm_i2c_chip *chip;
276 snprintf(name, sizeof(name), "generic_%x", chip_addr);
280 ret = device_bind_driver(bus, "i2c_generic_chip_drv", str, &dev);
281 debug("%s: device_bind_driver: ret=%d\n", __func__, ret);
285 /* Tell the device what we know about it */
286 chip = dev_get_parent_platdata(dev);
287 chip->chip_addr = chip_addr;
288 chip->offset_len = offset_len;
289 ret = device_probe(dev);
290 debug("%s: device_probe: ret=%d\n", __func__, ret);
299 * If the device failed to probe, unbind it. There is nothing there
300 * on the bus so we don't want to leave it lying around
308 int i2c_get_chip(struct udevice *bus, uint chip_addr, uint offset_len,
309 struct udevice **devp)
313 debug("%s: Searching bus '%s' for address %02x: ", __func__,
314 bus->name, chip_addr);
315 for (device_find_first_child(bus, &dev); dev;
316 device_find_next_child(&dev)) {
317 struct dm_i2c_chip *chip = dev_get_parent_platdata(dev);
320 if (chip->chip_addr == chip_addr) {
321 ret = device_probe(dev);
322 debug("found, ret=%d\n", ret);
329 debug("not found\n");
330 return i2c_bind_driver(bus, chip_addr, offset_len, devp);
333 int i2c_get_chip_for_busnum(int busnum, int chip_addr, uint offset_len,
334 struct udevice **devp)
339 ret = uclass_get_device_by_seq(UCLASS_I2C, busnum, &bus);
341 debug("Cannot find I2C bus %d\n", busnum);
344 ret = i2c_get_chip(bus, chip_addr, offset_len, devp);
346 debug("Cannot find I2C chip %02x on bus %d\n", chip_addr,
354 int dm_i2c_probe(struct udevice *bus, uint chip_addr, uint chip_flags,
355 struct udevice **devp)
361 /* First probe that chip */
362 ret = i2c_probe_chip(bus, chip_addr, chip_flags);
363 debug("%s: bus='%s', address %02x, ret=%d\n", __func__, bus->name,
368 /* The chip was found, see if we have a driver, and probe it */
369 ret = i2c_get_chip(bus, chip_addr, 1, devp);
370 debug("%s: i2c_get_chip: ret=%d\n", __func__, ret);
375 int dm_i2c_set_bus_speed(struct udevice *bus, unsigned int speed)
377 struct dm_i2c_ops *ops = i2c_get_ops(bus);
378 struct dm_i2c_bus *i2c = dev_get_uclass_priv(bus);
382 * If we have a method, call it. If not then the driver probably wants
383 * to deal with speed changes on the next transfer. It can easily read
384 * the current speed from this uclass
386 if (ops->set_bus_speed) {
387 ret = ops->set_bus_speed(bus, speed);
391 i2c->speed_hz = speed;
396 int dm_i2c_get_bus_speed(struct udevice *bus)
398 struct dm_i2c_ops *ops = i2c_get_ops(bus);
399 struct dm_i2c_bus *i2c = dev_get_uclass_priv(bus);
401 if (!ops->get_bus_speed)
402 return i2c->speed_hz;
404 return ops->get_bus_speed(bus);
407 int i2c_set_chip_flags(struct udevice *dev, uint flags)
409 struct udevice *bus = dev->parent;
410 struct dm_i2c_chip *chip = dev_get_parent_platdata(dev);
411 struct dm_i2c_ops *ops = i2c_get_ops(bus);
414 if (ops->set_flags) {
415 ret = ops->set_flags(dev, flags);
424 int i2c_get_chip_flags(struct udevice *dev, uint *flagsp)
426 struct dm_i2c_chip *chip = dev_get_parent_platdata(dev);
428 *flagsp = chip->flags;
433 int i2c_set_chip_offset_len(struct udevice *dev, uint offset_len)
435 struct dm_i2c_chip *chip = dev_get_parent_platdata(dev);
437 if (offset_len > I2C_MAX_OFFSET_LEN)
439 chip->offset_len = offset_len;
444 int i2c_get_chip_offset_len(struct udevice *dev)
446 struct dm_i2c_chip *chip = dev_get_parent_platdata(dev);
448 return chip->offset_len;
451 int i2c_deblock(struct udevice *bus)
453 struct dm_i2c_ops *ops = i2c_get_ops(bus);
456 * We could implement a software deblocking here if we could get
457 * access to the GPIOs used by I2C, and switch them to GPIO mode
458 * and then back to I2C. This is somewhat beyond our powers in
459 * driver model at present, so for now just fail.
461 * See https://patchwork.ozlabs.org/patch/399040/
466 return ops->deblock(bus);
469 #if CONFIG_IS_ENABLED(OF_CONTROL)
470 int i2c_chip_ofdata_to_platdata(const void *blob, int node,
471 struct dm_i2c_chip *chip)
473 chip->offset_len = fdtdec_get_int(gd->fdt_blob, node,
474 "u-boot,i2c-offset-len", 1);
476 chip->chip_addr = fdtdec_get_int(gd->fdt_blob, node, "reg", -1);
477 if (chip->chip_addr == -1) {
478 debug("%s: I2C Node '%s' has no 'reg' property\n", __func__,
479 fdt_get_name(blob, node, NULL));
487 static int i2c_post_probe(struct udevice *dev)
489 #if CONFIG_IS_ENABLED(OF_CONTROL)
490 struct dm_i2c_bus *i2c = dev_get_uclass_priv(dev);
492 i2c->speed_hz = fdtdec_get_int(gd->fdt_blob, dev_of_offset(dev),
493 "clock-frequency", 100000);
495 return dm_i2c_set_bus_speed(dev, i2c->speed_hz);
501 static int i2c_child_post_bind(struct udevice *dev)
503 #if CONFIG_IS_ENABLED(OF_CONTROL)
504 struct dm_i2c_chip *plat = dev_get_parent_platdata(dev);
506 if (dev_of_offset(dev) == -1)
509 return i2c_chip_ofdata_to_platdata(gd->fdt_blob, dev_of_offset(dev),
516 UCLASS_DRIVER(i2c) = {
519 .flags = DM_UC_FLAG_SEQ_ALIAS,
520 #if CONFIG_IS_ENABLED(OF_CONTROL)
521 .post_bind = dm_scan_fdt_dev,
523 .post_probe = i2c_post_probe,
524 .per_device_auto_alloc_size = sizeof(struct dm_i2c_bus),
525 .per_child_platdata_auto_alloc_size = sizeof(struct dm_i2c_chip),
526 .child_post_bind = i2c_child_post_bind,
529 UCLASS_DRIVER(i2c_generic) = {
530 .id = UCLASS_I2C_GENERIC,
531 .name = "i2c_generic",
534 U_BOOT_DRIVER(i2c_generic_chip_drv) = {
535 .name = "i2c_generic_chip_drv",
536 .id = UCLASS_I2C_GENERIC,