2 * Copyright (c) 2014 Google, Inc
4 * SPDX-License-Identifier: GPL-2.0+
13 #include <dm/device-internal.h>
17 DECLARE_GLOBAL_DATA_PTR;
19 #define I2C_MAX_OFFSET_LEN 4
21 /* Useful debugging function */
22 void i2c_dump_msgs(struct i2c_msg *msg, int nmsgs)
26 for (i = 0; i < nmsgs; i++) {
27 struct i2c_msg *m = &msg[i];
29 printf(" %s %x len=%x", m->flags & I2C_M_RD ? "R" : "W",
31 if (!(m->flags & I2C_M_RD))
32 printf(": %x", m->buf[0]);
38 * i2c_setup_offset() - Set up a new message with a chip offset
41 * @offset: Byte offset within chip
42 * @offset_buf: Place to put byte offset
43 * @msg: Message buffer
44 * @return 0 if OK, -EADDRNOTAVAIL if the offset length is 0. In that case the
45 * message is still set up but will not contain an offset.
47 static int i2c_setup_offset(struct dm_i2c_chip *chip, uint offset,
48 uint8_t offset_buf[], struct i2c_msg *msg)
52 msg->addr = chip->chip_addr;
53 msg->flags = chip->flags & DM_I2C_CHIP_10BIT ? I2C_M_TEN : 0;
54 msg->len = chip->offset_len;
55 msg->buf = offset_buf;
56 if (!chip->offset_len)
57 return -EADDRNOTAVAIL;
58 assert(chip->offset_len <= I2C_MAX_OFFSET_LEN);
59 offset_len = chip->offset_len;
61 *offset_buf++ = offset >> (8 * offset_len);
66 static int i2c_read_bytewise(struct udevice *dev, uint offset,
67 uint8_t *buffer, int len)
69 struct dm_i2c_chip *chip = dev_get_parent_platdata(dev);
70 struct udevice *bus = dev_get_parent(dev);
71 struct dm_i2c_ops *ops = i2c_get_ops(bus);
72 struct i2c_msg msg[2], *ptr;
73 uint8_t offset_buf[I2C_MAX_OFFSET_LEN];
77 for (i = 0; i < len; i++) {
78 if (i2c_setup_offset(chip, offset + i, offset_buf, msg))
81 ptr->addr = chip->chip_addr;
82 ptr->flags = msg->flags | I2C_M_RD;
84 ptr->buf = &buffer[i];
87 ret = ops->xfer(bus, msg, ptr - msg);
95 static int i2c_write_bytewise(struct udevice *dev, uint offset,
96 const uint8_t *buffer, int len)
98 struct dm_i2c_chip *chip = dev_get_parent_platdata(dev);
99 struct udevice *bus = dev_get_parent(dev);
100 struct dm_i2c_ops *ops = i2c_get_ops(bus);
101 struct i2c_msg msg[1];
102 uint8_t buf[I2C_MAX_OFFSET_LEN + 1];
106 for (i = 0; i < len; i++) {
107 if (i2c_setup_offset(chip, offset + i, buf, msg))
109 buf[msg->len++] = buffer[i];
111 ret = ops->xfer(bus, msg, 1);
119 int dm_i2c_read(struct udevice *dev, uint offset, uint8_t *buffer, int len)
121 struct dm_i2c_chip *chip = dev_get_parent_platdata(dev);
122 struct udevice *bus = dev_get_parent(dev);
123 struct dm_i2c_ops *ops = i2c_get_ops(bus);
124 struct i2c_msg msg[2], *ptr;
125 uint8_t offset_buf[I2C_MAX_OFFSET_LEN];
130 if (chip->flags & DM_I2C_CHIP_RD_ADDRESS)
131 return i2c_read_bytewise(dev, offset, buffer, len);
133 if (!i2c_setup_offset(chip, offset, offset_buf, ptr))
137 ptr->addr = chip->chip_addr;
138 ptr->flags = chip->flags & DM_I2C_CHIP_10BIT ? I2C_M_TEN : 0;
139 ptr->flags |= I2C_M_RD;
144 msg_count = ptr - msg;
146 return ops->xfer(bus, msg, msg_count);
149 int dm_i2c_write(struct udevice *dev, uint offset, const uint8_t *buffer,
152 struct dm_i2c_chip *chip = dev_get_parent_platdata(dev);
153 struct udevice *bus = dev_get_parent(dev);
154 struct dm_i2c_ops *ops = i2c_get_ops(bus);
155 struct i2c_msg msg[1];
160 if (chip->flags & DM_I2C_CHIP_WR_ADDRESS)
161 return i2c_write_bytewise(dev, offset, buffer, len);
163 * The simple approach would be to send two messages here: one to
164 * set the offset and one to write the bytes. However some drivers
165 * will not be expecting this, and some chips won't like how the
166 * driver presents this on the I2C bus.
168 * The API does not support separate offset and data. We could extend
169 * it with a flag indicating that there is data in the next message
170 * that needs to be processed in the same transaction. We could
171 * instead add an additional buffer to each message. For now, handle
172 * this in the uclass since it isn't clear what the impact on drivers
173 * would be with this extra complication. Unfortunately this means
174 * copying the message.
176 * Use the stack for small messages, malloc() for larger ones. We
177 * need to allow space for the offset (up to 4 bytes) and the message
181 uint8_t buf[I2C_MAX_OFFSET_LEN + len];
183 i2c_setup_offset(chip, offset, buf, msg);
185 memcpy(buf + chip->offset_len, buffer, len);
187 return ops->xfer(bus, msg, 1);
192 buf = malloc(I2C_MAX_OFFSET_LEN + len);
195 i2c_setup_offset(chip, offset, buf, msg);
197 memcpy(buf + chip->offset_len, buffer, len);
199 ret = ops->xfer(bus, msg, 1);
205 int dm_i2c_xfer(struct udevice *dev, struct i2c_msg *msg, int nmsgs)
207 struct udevice *bus = dev_get_parent(dev);
208 struct dm_i2c_ops *ops = i2c_get_ops(bus);
213 return ops->xfer(bus, msg, nmsgs);
216 int dm_i2c_reg_read(struct udevice *dev, uint offset)
221 ret = dm_i2c_read(dev, offset, &val, 1);
228 int dm_i2c_reg_write(struct udevice *dev, uint offset, uint value)
232 return dm_i2c_write(dev, offset, &val, 1);
236 * i2c_probe_chip() - probe for a chip on a bus
239 * @chip_addr: Chip address to probe
240 * @flags: Flags for the chip
241 * @return 0 if found, -ENOSYS if the driver is invalid, -EREMOTEIO if the chip
242 * does not respond to probe
244 static int i2c_probe_chip(struct udevice *bus, uint chip_addr,
245 enum dm_i2c_chip_flags chip_flags)
247 struct dm_i2c_ops *ops = i2c_get_ops(bus);
248 struct i2c_msg msg[1];
251 if (ops->probe_chip) {
252 ret = ops->probe_chip(bus, chip_addr, chip_flags);
253 if (!ret || ret != -ENOSYS)
260 /* Probe with a zero-length message */
261 msg->addr = chip_addr;
262 msg->flags = chip_flags & DM_I2C_CHIP_10BIT ? I2C_M_TEN : 0;
266 return ops->xfer(bus, msg, 1);
269 static int i2c_bind_driver(struct udevice *bus, uint chip_addr, uint offset_len,
270 struct udevice **devp)
272 struct dm_i2c_chip *chip;
277 snprintf(name, sizeof(name), "generic_%x", chip_addr);
281 ret = device_bind_driver(bus, "i2c_generic_chip_drv", str, &dev);
282 debug("%s: device_bind_driver: ret=%d\n", __func__, ret);
286 /* Tell the device what we know about it */
287 chip = dev_get_parent_platdata(dev);
288 chip->chip_addr = chip_addr;
289 chip->offset_len = offset_len;
290 ret = device_probe(dev);
291 debug("%s: device_probe: ret=%d\n", __func__, ret);
300 * If the device failed to probe, unbind it. There is nothing there
301 * on the bus so we don't want to leave it lying around
309 int i2c_get_chip(struct udevice *bus, uint chip_addr, uint offset_len,
310 struct udevice **devp)
314 debug("%s: Searching bus '%s' for address %02x: ", __func__,
315 bus->name, chip_addr);
316 for (device_find_first_child(bus, &dev); dev;
317 device_find_next_child(&dev)) {
318 struct dm_i2c_chip *chip = dev_get_parent_platdata(dev);
321 if (chip->chip_addr == chip_addr) {
322 ret = device_probe(dev);
323 debug("found, ret=%d\n", ret);
330 debug("not found\n");
331 return i2c_bind_driver(bus, chip_addr, offset_len, devp);
334 int i2c_get_chip_for_busnum(int busnum, int chip_addr, uint offset_len,
335 struct udevice **devp)
340 ret = uclass_get_device_by_seq(UCLASS_I2C, busnum, &bus);
342 debug("Cannot find I2C bus %d\n", busnum);
345 ret = i2c_get_chip(bus, chip_addr, offset_len, devp);
347 debug("Cannot find I2C chip %02x on bus %d\n", chip_addr,
355 int dm_i2c_probe(struct udevice *bus, uint chip_addr, uint chip_flags,
356 struct udevice **devp)
362 /* First probe that chip */
363 ret = i2c_probe_chip(bus, chip_addr, chip_flags);
364 debug("%s: bus='%s', address %02x, ret=%d\n", __func__, bus->name,
369 /* The chip was found, see if we have a driver, and probe it */
370 ret = i2c_get_chip(bus, chip_addr, 1, devp);
371 debug("%s: i2c_get_chip: ret=%d\n", __func__, ret);
376 int dm_i2c_set_bus_speed(struct udevice *bus, unsigned int speed)
378 struct dm_i2c_ops *ops = i2c_get_ops(bus);
379 struct dm_i2c_bus *i2c = dev_get_uclass_priv(bus);
383 * If we have a method, call it. If not then the driver probably wants
384 * to deal with speed changes on the next transfer. It can easily read
385 * the current speed from this uclass
387 if (ops->set_bus_speed) {
388 ret = ops->set_bus_speed(bus, speed);
392 i2c->speed_hz = speed;
397 int dm_i2c_get_bus_speed(struct udevice *bus)
399 struct dm_i2c_ops *ops = i2c_get_ops(bus);
400 struct dm_i2c_bus *i2c = dev_get_uclass_priv(bus);
402 if (!ops->get_bus_speed)
403 return i2c->speed_hz;
405 return ops->get_bus_speed(bus);
408 int i2c_set_chip_flags(struct udevice *dev, uint flags)
410 struct udevice *bus = dev->parent;
411 struct dm_i2c_chip *chip = dev_get_parent_platdata(dev);
412 struct dm_i2c_ops *ops = i2c_get_ops(bus);
415 if (ops->set_flags) {
416 ret = ops->set_flags(dev, flags);
425 int i2c_get_chip_flags(struct udevice *dev, uint *flagsp)
427 struct dm_i2c_chip *chip = dev_get_parent_platdata(dev);
429 *flagsp = chip->flags;
434 int i2c_set_chip_offset_len(struct udevice *dev, uint offset_len)
436 struct dm_i2c_chip *chip = dev_get_parent_platdata(dev);
438 if (offset_len > I2C_MAX_OFFSET_LEN)
440 chip->offset_len = offset_len;
445 int i2c_get_chip_offset_len(struct udevice *dev)
447 struct dm_i2c_chip *chip = dev_get_parent_platdata(dev);
449 return chip->offset_len;
452 int i2c_deblock(struct udevice *bus)
454 struct dm_i2c_ops *ops = i2c_get_ops(bus);
457 * We could implement a software deblocking here if we could get
458 * access to the GPIOs used by I2C, and switch them to GPIO mode
459 * and then back to I2C. This is somewhat beyond our powers in
460 * driver model at present, so for now just fail.
462 * See https://patchwork.ozlabs.org/patch/399040/
467 return ops->deblock(bus);
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));
486 static int i2c_post_probe(struct udevice *dev)
488 struct dm_i2c_bus *i2c = dev_get_uclass_priv(dev);
490 i2c->speed_hz = fdtdec_get_int(gd->fdt_blob, dev->of_offset,
491 "clock-frequency", 100000);
493 return dm_i2c_set_bus_speed(dev, i2c->speed_hz);
496 static int i2c_post_bind(struct udevice *dev)
498 /* Scan the bus for devices */
499 return dm_scan_fdt_node(dev, gd->fdt_blob, dev->of_offset, false);
502 static int i2c_child_post_bind(struct udevice *dev)
504 struct dm_i2c_chip *plat = dev_get_parent_platdata(dev);
506 if (dev->of_offset == -1)
509 return i2c_chip_ofdata_to_platdata(gd->fdt_blob, dev->of_offset, plat);
512 UCLASS_DRIVER(i2c) = {
515 .flags = DM_UC_FLAG_SEQ_ALIAS,
516 .post_bind = i2c_post_bind,
517 .post_probe = i2c_post_probe,
518 .per_device_auto_alloc_size = sizeof(struct dm_i2c_bus),
519 .per_child_platdata_auto_alloc_size = sizeof(struct dm_i2c_chip),
520 .child_post_bind = i2c_child_post_bind,
523 UCLASS_DRIVER(i2c_generic) = {
524 .id = UCLASS_I2C_GENERIC,
525 .name = "i2c_generic",
528 U_BOOT_DRIVER(i2c_generic_chip_drv) = {
529 .name = "i2c_generic_chip_drv",
530 .id = UCLASS_I2C_GENERIC,