2 * Copyright (c) 2014 Google, Inc
4 * SPDX-License-Identifier: GPL-2.0+
12 #include <dm/device-internal.h>
15 DECLARE_GLOBAL_DATA_PTR;
17 #define I2C_MAX_OFFSET_LEN 4
19 /* Useful debugging function */
20 void i2c_dump_msgs(struct i2c_msg *msg, int nmsgs)
24 for (i = 0; i < nmsgs; i++) {
25 struct i2c_msg *m = &msg[i];
27 printf(" %s %x len=%x", m->flags & I2C_M_RD ? "R" : "W",
29 if (!(m->flags & I2C_M_RD))
30 printf(": %x", m->buf[0]);
36 * i2c_setup_offset() - Set up a new message with a chip offset
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.
45 static int i2c_setup_offset(struct dm_i2c_chip *chip, uint offset,
46 uint8_t offset_buf[], struct i2c_msg *msg)
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;
59 *offset_buf++ = offset >> (8 * offset_len);
64 static int i2c_read_bytewise(struct udevice *dev, uint offset,
65 uint8_t *buffer, int len)
67 struct dm_i2c_chip *chip = dev_get_parent_platdata(dev);
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];
75 for (i = 0; i < len; i++) {
76 if (i2c_setup_offset(chip, offset + i, offset_buf, msg))
79 ptr->addr = chip->chip_addr;
80 ptr->flags = msg->flags | I2C_M_RD;
82 ptr->buf = &buffer[i];
85 ret = ops->xfer(bus, msg, ptr - msg);
93 static int i2c_write_bytewise(struct udevice *dev, uint offset,
94 const uint8_t *buffer, int len)
96 struct dm_i2c_chip *chip = dev_get_parent_platdata(dev);
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];
104 for (i = 0; i < len; i++) {
105 if (i2c_setup_offset(chip, offset + i, buf, msg))
107 buf[msg->len++] = buffer[i];
109 ret = ops->xfer(bus, msg, 1);
117 int dm_i2c_read(struct udevice *dev, uint offset, uint8_t *buffer, int len)
119 struct dm_i2c_chip *chip = dev_get_parent_platdata(dev);
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];
128 if (chip->flags & DM_I2C_CHIP_RD_ADDRESS)
129 return i2c_read_bytewise(dev, offset, buffer, len);
131 if (!i2c_setup_offset(chip, offset, offset_buf, ptr))
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;
142 msg_count = ptr - msg;
144 return ops->xfer(bus, msg, msg_count);
147 int dm_i2c_write(struct udevice *dev, uint offset, const uint8_t *buffer,
150 struct dm_i2c_chip *chip = dev_get_parent_platdata(dev);
151 struct udevice *bus = dev_get_parent(dev);
152 struct dm_i2c_ops *ops = i2c_get_ops(bus);
153 struct i2c_msg msg[1];
158 if (chip->flags & DM_I2C_CHIP_WR_ADDRESS)
159 return i2c_write_bytewise(dev, offset, buffer, len);
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.
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.
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
179 uint8_t buf[I2C_MAX_OFFSET_LEN + len];
181 i2c_setup_offset(chip, offset, buf, msg);
183 memcpy(buf + chip->offset_len, buffer, len);
185 return ops->xfer(bus, msg, 1);
190 buf = malloc(I2C_MAX_OFFSET_LEN + len);
193 i2c_setup_offset(chip, offset, buf, msg);
195 memcpy(buf + chip->offset_len, buffer, len);
197 ret = ops->xfer(bus, msg, 1);
203 int dm_i2c_xfer(struct udevice *dev, struct i2c_msg *msg, int nmsgs)
205 struct udevice *bus = dev_get_parent(dev);
206 struct dm_i2c_ops *ops = i2c_get_ops(bus);
211 return ops->xfer(bus, msg, nmsgs);
214 int dm_i2c_reg_read(struct udevice *dev, uint offset)
219 ret = dm_i2c_read(dev, offset, &val, 1);
226 int dm_i2c_reg_write(struct udevice *dev, uint offset, uint value)
230 return dm_i2c_write(dev, offset, &val, 1);
234 * i2c_probe_chip() - probe for a chip on a bus
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
242 static int i2c_probe_chip(struct udevice *bus, uint chip_addr,
243 enum dm_i2c_chip_flags chip_flags)
245 struct dm_i2c_ops *ops = i2c_get_ops(bus);
246 struct i2c_msg msg[1];
249 if (ops->probe_chip) {
250 ret = ops->probe_chip(bus, chip_addr, chip_flags);
251 if (!ret || ret != -ENOSYS)
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;
264 return ops->xfer(bus, msg, 1);
267 static int i2c_bind_driver(struct udevice *bus, uint chip_addr, uint offset_len,
268 struct udevice **devp)
270 struct dm_i2c_chip *chip;
275 snprintf(name, sizeof(name), "generic_%x", chip_addr);
279 ret = device_bind_driver(bus, "i2c_generic_chip_drv", str, &dev);
280 debug("%s: device_bind_driver: ret=%d\n", __func__, ret);
284 /* Tell the device what we know about it */
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);
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
307 int i2c_get_chip(struct udevice *bus, uint chip_addr, uint offset_len,
308 struct udevice **devp)
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)) {
316 struct dm_i2c_chip *chip = dev_get_parent_platdata(dev);
319 if (chip->chip_addr == chip_addr) {
320 ret = device_probe(dev);
321 debug("found, ret=%d\n", ret);
328 debug("not found\n");
329 return i2c_bind_driver(bus, chip_addr, offset_len, devp);
332 int i2c_get_chip_for_busnum(int busnum, int chip_addr, uint offset_len,
333 struct udevice **devp)
338 ret = uclass_get_device_by_seq(UCLASS_I2C, busnum, &bus);
340 debug("Cannot find I2C bus %d\n", busnum);
343 ret = i2c_get_chip(bus, chip_addr, offset_len, devp);
345 debug("Cannot find I2C chip %02x on bus %d\n", chip_addr,
353 int dm_i2c_probe(struct udevice *bus, uint chip_addr, uint chip_flags,
354 struct udevice **devp)
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,
367 /* The chip was found, see if we have a driver, and probe it */
368 ret = i2c_get_chip(bus, chip_addr, 1, devp);
369 debug("%s: i2c_get_chip: ret=%d\n", __func__, ret);
374 int dm_i2c_set_bus_speed(struct udevice *bus, unsigned int speed)
376 struct dm_i2c_ops *ops = i2c_get_ops(bus);
377 struct dm_i2c_bus *i2c = dev_get_uclass_priv(bus);
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
385 if (ops->set_bus_speed) {
386 ret = ops->set_bus_speed(bus, speed);
390 i2c->speed_hz = speed;
395 int dm_i2c_get_bus_speed(struct udevice *bus)
397 struct dm_i2c_ops *ops = i2c_get_ops(bus);
398 struct dm_i2c_bus *i2c = dev_get_uclass_priv(bus);
400 if (!ops->get_bus_speed)
401 return i2c->speed_hz;
403 return ops->get_bus_speed(bus);
406 int i2c_set_chip_flags(struct udevice *dev, uint flags)
408 struct udevice *bus = dev->parent;
409 struct dm_i2c_chip *chip = dev_get_parent_platdata(dev);
410 struct dm_i2c_ops *ops = i2c_get_ops(bus);
413 if (ops->set_flags) {
414 ret = ops->set_flags(dev, flags);
423 int i2c_get_chip_flags(struct udevice *dev, uint *flagsp)
425 struct dm_i2c_chip *chip = dev_get_parent_platdata(dev);
427 *flagsp = chip->flags;
432 int i2c_set_chip_offset_len(struct udevice *dev, uint offset_len)
434 struct dm_i2c_chip *chip = dev_get_parent_platdata(dev);
436 if (offset_len > I2C_MAX_OFFSET_LEN)
438 chip->offset_len = offset_len;
443 int i2c_get_chip_offset_len(struct udevice *dev)
445 struct dm_i2c_chip *chip = dev_get_parent_platdata(dev);
447 return chip->offset_len;
450 int i2c_deblock(struct udevice *bus)
452 struct dm_i2c_ops *ops = i2c_get_ops(bus);
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.
460 * See https://patchwork.ozlabs.org/patch/399040/
465 return ops->deblock(bus);
468 #if CONFIG_IS_ENABLED(OF_CONTROL)
469 int i2c_chip_ofdata_to_platdata(struct udevice *dev, struct dm_i2c_chip *chip)
473 chip->offset_len = dev_read_u32_default(dev, "u-boot,i2c-offset-len",
476 addr = dev_read_u32_default(dev, "reg", -1);
478 debug("%s: I2C Node '%s' has no 'reg' property %s\n", __func__,
479 dev_read_name(dev), dev->name);
482 chip->chip_addr = addr;
488 static int i2c_post_probe(struct udevice *dev)
490 #if CONFIG_IS_ENABLED(OF_CONTROL)
491 struct dm_i2c_bus *i2c = dev_get_uclass_priv(dev);
493 i2c->speed_hz = dev_read_u32_default(dev, "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_valid(dev))
508 return i2c_chip_ofdata_to_platdata(dev, plat);
514 UCLASS_DRIVER(i2c) = {
517 .flags = DM_UC_FLAG_SEQ_ALIAS,
518 #if CONFIG_IS_ENABLED(OF_CONTROL)
519 .post_bind = dm_scan_fdt_dev,
521 .post_probe = i2c_post_probe,
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,
527 UCLASS_DRIVER(i2c_generic) = {
528 .id = UCLASS_I2C_GENERIC,
529 .name = "i2c_generic",
532 U_BOOT_DRIVER(i2c_generic_chip_drv) = {
533 .name = "i2c_generic_chip_drv",
534 .id = UCLASS_I2C_GENERIC,