2 * Copyright (c) 2014 Google, Inc
4 * SPDX-License-Identifier: GPL-2.0+
12 #include <dm/device-internal.h>
14 #include <dm/pinctrl.h>
19 #define I2C_MAX_OFFSET_LEN 4
27 /* Useful debugging function */
28 void i2c_dump_msgs(struct i2c_msg *msg, int nmsgs)
32 for (i = 0; i < nmsgs; i++) {
33 struct i2c_msg *m = &msg[i];
35 printf(" %s %x len=%x", m->flags & I2C_M_RD ? "R" : "W",
37 if (!(m->flags & I2C_M_RD))
38 printf(": %x", m->buf[0]);
44 * i2c_setup_offset() - Set up a new message with a chip offset
47 * @offset: Byte offset within chip
48 * @offset_buf: Place to put byte offset
49 * @msg: Message buffer
50 * @return 0 if OK, -EADDRNOTAVAIL if the offset length is 0. In that case the
51 * message is still set up but will not contain an offset.
53 static int i2c_setup_offset(struct dm_i2c_chip *chip, uint offset,
54 uint8_t offset_buf[], struct i2c_msg *msg)
58 msg->addr = chip->chip_addr;
59 msg->flags = chip->flags & DM_I2C_CHIP_10BIT ? I2C_M_TEN : 0;
60 msg->len = chip->offset_len;
61 msg->buf = offset_buf;
62 if (!chip->offset_len)
63 return -EADDRNOTAVAIL;
64 assert(chip->offset_len <= I2C_MAX_OFFSET_LEN);
65 offset_len = chip->offset_len;
67 *offset_buf++ = offset >> (8 * offset_len);
72 static int i2c_read_bytewise(struct udevice *dev, uint offset,
73 uint8_t *buffer, int len)
75 struct dm_i2c_chip *chip = dev_get_parent_platdata(dev);
76 struct udevice *bus = dev_get_parent(dev);
77 struct dm_i2c_ops *ops = i2c_get_ops(bus);
78 struct i2c_msg msg[2], *ptr;
79 uint8_t offset_buf[I2C_MAX_OFFSET_LEN];
83 for (i = 0; i < len; i++) {
84 if (i2c_setup_offset(chip, offset + i, offset_buf, msg))
87 ptr->addr = chip->chip_addr;
88 ptr->flags = msg->flags | I2C_M_RD;
90 ptr->buf = &buffer[i];
93 ret = ops->xfer(bus, msg, ptr - msg);
101 static int i2c_write_bytewise(struct udevice *dev, uint offset,
102 const uint8_t *buffer, int len)
104 struct dm_i2c_chip *chip = dev_get_parent_platdata(dev);
105 struct udevice *bus = dev_get_parent(dev);
106 struct dm_i2c_ops *ops = i2c_get_ops(bus);
107 struct i2c_msg msg[1];
108 uint8_t buf[I2C_MAX_OFFSET_LEN + 1];
112 for (i = 0; i < len; i++) {
113 if (i2c_setup_offset(chip, offset + i, buf, msg))
115 buf[msg->len++] = buffer[i];
117 ret = ops->xfer(bus, msg, 1);
125 int dm_i2c_read(struct udevice *dev, uint offset, uint8_t *buffer, int len)
127 struct dm_i2c_chip *chip = dev_get_parent_platdata(dev);
128 struct udevice *bus = dev_get_parent(dev);
129 struct dm_i2c_ops *ops = i2c_get_ops(bus);
130 struct i2c_msg msg[2], *ptr;
131 uint8_t offset_buf[I2C_MAX_OFFSET_LEN];
136 if (chip->flags & DM_I2C_CHIP_RD_ADDRESS)
137 return i2c_read_bytewise(dev, offset, buffer, len);
139 if (!i2c_setup_offset(chip, offset, offset_buf, ptr))
143 ptr->addr = chip->chip_addr;
144 ptr->flags = chip->flags & DM_I2C_CHIP_10BIT ? I2C_M_TEN : 0;
145 ptr->flags |= I2C_M_RD;
150 msg_count = ptr - msg;
152 return ops->xfer(bus, msg, msg_count);
155 int dm_i2c_write(struct udevice *dev, uint offset, const uint8_t *buffer,
158 struct dm_i2c_chip *chip = dev_get_parent_platdata(dev);
159 struct udevice *bus = dev_get_parent(dev);
160 struct dm_i2c_ops *ops = i2c_get_ops(bus);
161 struct i2c_msg msg[1];
166 if (chip->flags & DM_I2C_CHIP_WR_ADDRESS)
167 return i2c_write_bytewise(dev, offset, buffer, len);
169 * The simple approach would be to send two messages here: one to
170 * set the offset and one to write the bytes. However some drivers
171 * will not be expecting this, and some chips won't like how the
172 * driver presents this on the I2C bus.
174 * The API does not support separate offset and data. We could extend
175 * it with a flag indicating that there is data in the next message
176 * that needs to be processed in the same transaction. We could
177 * instead add an additional buffer to each message. For now, handle
178 * this in the uclass since it isn't clear what the impact on drivers
179 * would be with this extra complication. Unfortunately this means
180 * copying the message.
182 * Use the stack for small messages, malloc() for larger ones. We
183 * need to allow space for the offset (up to 4 bytes) and the message
187 uint8_t buf[I2C_MAX_OFFSET_LEN + len];
189 i2c_setup_offset(chip, offset, buf, msg);
191 memcpy(buf + chip->offset_len, buffer, len);
193 return ops->xfer(bus, msg, 1);
198 buf = malloc(I2C_MAX_OFFSET_LEN + len);
201 i2c_setup_offset(chip, offset, buf, msg);
203 memcpy(buf + chip->offset_len, buffer, len);
205 ret = ops->xfer(bus, msg, 1);
211 int dm_i2c_xfer(struct udevice *dev, struct i2c_msg *msg, int nmsgs)
213 struct udevice *bus = dev_get_parent(dev);
214 struct dm_i2c_ops *ops = i2c_get_ops(bus);
219 return ops->xfer(bus, msg, nmsgs);
222 int dm_i2c_reg_read(struct udevice *dev, uint offset)
227 ret = dm_i2c_read(dev, offset, &val, 1);
234 int dm_i2c_reg_write(struct udevice *dev, uint offset, uint value)
238 return dm_i2c_write(dev, offset, &val, 1);
242 * i2c_probe_chip() - probe for a chip on a bus
245 * @chip_addr: Chip address to probe
246 * @flags: Flags for the chip
247 * @return 0 if found, -ENOSYS if the driver is invalid, -EREMOTEIO if the chip
248 * does not respond to probe
250 static int i2c_probe_chip(struct udevice *bus, uint chip_addr,
251 enum dm_i2c_chip_flags chip_flags)
253 struct dm_i2c_ops *ops = i2c_get_ops(bus);
254 struct i2c_msg msg[1];
257 if (ops->probe_chip) {
258 ret = ops->probe_chip(bus, chip_addr, chip_flags);
259 if (!ret || ret != -ENOSYS)
266 /* Probe with a zero-length message */
267 msg->addr = chip_addr;
268 msg->flags = chip_flags & DM_I2C_CHIP_10BIT ? I2C_M_TEN : 0;
272 return ops->xfer(bus, msg, 1);
275 static int i2c_bind_driver(struct udevice *bus, uint chip_addr, uint offset_len,
276 struct udevice **devp)
278 struct dm_i2c_chip *chip;
283 snprintf(name, sizeof(name), "generic_%x", chip_addr);
287 ret = device_bind_driver(bus, "i2c_generic_chip_drv", str, &dev);
288 debug("%s: device_bind_driver: ret=%d\n", __func__, ret);
292 /* Tell the device what we know about it */
293 chip = dev_get_parent_platdata(dev);
294 chip->chip_addr = chip_addr;
295 chip->offset_len = offset_len;
296 ret = device_probe(dev);
297 debug("%s: device_probe: ret=%d\n", __func__, ret);
306 * If the device failed to probe, unbind it. There is nothing there
307 * on the bus so we don't want to leave it lying around
315 int i2c_get_chip(struct udevice *bus, uint chip_addr, uint offset_len,
316 struct udevice **devp)
320 debug("%s: Searching bus '%s' for address %02x: ", __func__,
321 bus->name, chip_addr);
322 for (device_find_first_child(bus, &dev); dev;
323 device_find_next_child(&dev)) {
324 struct dm_i2c_chip *chip = dev_get_parent_platdata(dev);
327 if (chip->chip_addr == chip_addr) {
328 ret = device_probe(dev);
329 debug("found, ret=%d\n", ret);
336 debug("not found\n");
337 return i2c_bind_driver(bus, chip_addr, offset_len, devp);
340 int i2c_get_chip_for_busnum(int busnum, int chip_addr, uint offset_len,
341 struct udevice **devp)
346 ret = uclass_get_device_by_seq(UCLASS_I2C, busnum, &bus);
348 debug("Cannot find I2C bus %d\n", busnum);
351 ret = i2c_get_chip(bus, chip_addr, offset_len, devp);
353 debug("Cannot find I2C chip %02x on bus %d\n", chip_addr,
361 int dm_i2c_probe(struct udevice *bus, uint chip_addr, uint chip_flags,
362 struct udevice **devp)
368 /* First probe that chip */
369 ret = i2c_probe_chip(bus, chip_addr, chip_flags);
370 debug("%s: bus='%s', address %02x, ret=%d\n", __func__, bus->name,
375 /* The chip was found, see if we have a driver, and probe it */
376 ret = i2c_get_chip(bus, chip_addr, 1, devp);
377 debug("%s: i2c_get_chip: ret=%d\n", __func__, ret);
382 int dm_i2c_set_bus_speed(struct udevice *bus, unsigned int speed)
384 struct dm_i2c_ops *ops = i2c_get_ops(bus);
385 struct dm_i2c_bus *i2c = dev_get_uclass_priv(bus);
389 * If we have a method, call it. If not then the driver probably wants
390 * to deal with speed changes on the next transfer. It can easily read
391 * the current speed from this uclass
393 if (ops->set_bus_speed) {
394 ret = ops->set_bus_speed(bus, speed);
398 i2c->speed_hz = speed;
403 int dm_i2c_get_bus_speed(struct udevice *bus)
405 struct dm_i2c_ops *ops = i2c_get_ops(bus);
406 struct dm_i2c_bus *i2c = dev_get_uclass_priv(bus);
408 if (!ops->get_bus_speed)
409 return i2c->speed_hz;
411 return ops->get_bus_speed(bus);
414 int i2c_set_chip_flags(struct udevice *dev, uint flags)
416 struct udevice *bus = dev->parent;
417 struct dm_i2c_chip *chip = dev_get_parent_platdata(dev);
418 struct dm_i2c_ops *ops = i2c_get_ops(bus);
421 if (ops->set_flags) {
422 ret = ops->set_flags(dev, flags);
431 int i2c_get_chip_flags(struct udevice *dev, uint *flagsp)
433 struct dm_i2c_chip *chip = dev_get_parent_platdata(dev);
435 *flagsp = chip->flags;
440 int i2c_set_chip_offset_len(struct udevice *dev, uint offset_len)
442 struct dm_i2c_chip *chip = dev_get_parent_platdata(dev);
444 if (offset_len > I2C_MAX_OFFSET_LEN)
446 chip->offset_len = offset_len;
451 int i2c_get_chip_offset_len(struct udevice *dev)
453 struct dm_i2c_chip *chip = dev_get_parent_platdata(dev);
455 return chip->offset_len;
458 #ifdef CONFIG_DM_GPIO
459 static void i2c_gpio_set_pin(struct gpio_desc *pin, int bit)
462 dm_gpio_set_dir_flags(pin, GPIOD_IS_IN);
464 dm_gpio_set_dir_flags(pin, GPIOD_IS_OUT |
466 GPIOD_IS_OUT_ACTIVE);
469 static int i2c_gpio_get_pin(struct gpio_desc *pin)
471 return dm_gpio_get_value(pin);
474 static int i2c_deblock_gpio_loop(struct gpio_desc *sda_pin,
475 struct gpio_desc *scl_pin)
480 i2c_gpio_set_pin(sda_pin, 1);
481 i2c_gpio_set_pin(scl_pin, 1);
484 /* Toggle SCL until slave release SDA */
485 while (counter-- >= 0) {
486 i2c_gpio_set_pin(scl_pin, 1);
488 i2c_gpio_set_pin(scl_pin, 0);
490 if (i2c_gpio_get_pin(sda_pin))
494 /* Then, send I2C stop */
495 i2c_gpio_set_pin(sda_pin, 0);
498 i2c_gpio_set_pin(scl_pin, 1);
501 i2c_gpio_set_pin(sda_pin, 1);
504 if (!i2c_gpio_get_pin(sda_pin) || !i2c_gpio_get_pin(scl_pin))
510 static int i2c_deblock_gpio(struct udevice *bus)
512 struct gpio_desc gpios[PIN_COUNT];
515 ret = gpio_request_list_by_name(bus, "gpios", gpios,
516 ARRAY_SIZE(gpios), GPIOD_IS_IN);
517 if (ret != ARRAY_SIZE(gpios)) {
518 debug("%s: I2C Node '%s' has no 'gpios' property %s\n",
519 __func__, dev_read_name(bus), bus->name);
521 gpio_free_list(bus, gpios, ret);
527 ret = pinctrl_select_state(bus, "gpio");
529 debug("%s: I2C Node '%s' has no 'gpio' pinctrl state. %s\n",
530 __func__, dev_read_name(bus), bus->name);
534 ret0 = i2c_deblock_gpio_loop(&gpios[PIN_SDA], &gpios[PIN_SCL]);
536 ret = pinctrl_select_state(bus, "default");
538 debug("%s: I2C Node '%s' has no 'default' pinctrl state. %s\n",
539 __func__, dev_read_name(bus), bus->name);
542 ret = !ret ? ret0 : ret;
545 gpio_free_list(bus, gpios, ARRAY_SIZE(gpios));
550 static int i2c_deblock_gpio(struct udevice *bus)
554 #endif // CONFIG_DM_GPIO
556 int i2c_deblock(struct udevice *bus)
558 struct dm_i2c_ops *ops = i2c_get_ops(bus);
561 return i2c_deblock_gpio(bus);
563 return ops->deblock(bus);
566 #if CONFIG_IS_ENABLED(OF_CONTROL)
567 int i2c_chip_ofdata_to_platdata(struct udevice *dev, struct dm_i2c_chip *chip)
571 chip->offset_len = dev_read_u32_default(dev, "u-boot,i2c-offset-len",
574 addr = dev_read_u32_default(dev, "reg", -1);
576 debug("%s: I2C Node '%s' has no 'reg' property %s\n", __func__,
577 dev_read_name(dev), dev->name);
580 chip->chip_addr = addr;
586 static int i2c_post_probe(struct udevice *dev)
588 #if CONFIG_IS_ENABLED(OF_CONTROL)
589 struct dm_i2c_bus *i2c = dev_get_uclass_priv(dev);
591 i2c->speed_hz = dev_read_u32_default(dev, "clock-frequency", 100000);
593 return dm_i2c_set_bus_speed(dev, i2c->speed_hz);
599 static int i2c_child_post_bind(struct udevice *dev)
601 #if CONFIG_IS_ENABLED(OF_CONTROL)
602 struct dm_i2c_chip *plat = dev_get_parent_platdata(dev);
604 if (!dev_of_valid(dev))
606 return i2c_chip_ofdata_to_platdata(dev, plat);
612 UCLASS_DRIVER(i2c) = {
615 .flags = DM_UC_FLAG_SEQ_ALIAS,
616 #if CONFIG_IS_ENABLED(OF_CONTROL)
617 .post_bind = dm_scan_fdt_dev,
619 .post_probe = i2c_post_probe,
620 .per_device_auto_alloc_size = sizeof(struct dm_i2c_bus),
621 .per_child_platdata_auto_alloc_size = sizeof(struct dm_i2c_chip),
622 .child_post_bind = i2c_child_post_bind,
625 UCLASS_DRIVER(i2c_generic) = {
626 .id = UCLASS_I2C_GENERIC,
627 .name = "i2c_generic",
630 U_BOOT_DRIVER(i2c_generic_chip_drv) = {
631 .name = "i2c_generic_chip_drv",
632 .id = UCLASS_I2C_GENERIC,