1 // SPDX-License-Identifier: GPL-2.0+
3 * Copyright (c) 2014 Google, Inc
12 #include <dm/device-internal.h>
14 #include <dm/pinctrl.h>
15 #if CONFIG_IS_ENABLED(DM_GPIO)
18 #include <linux/delay.h>
20 #define I2C_MAX_OFFSET_LEN 4
28 /* Useful debugging function */
29 void i2c_dump_msgs(struct i2c_msg *msg, int nmsgs)
33 for (i = 0; i < nmsgs; i++) {
34 struct i2c_msg *m = &msg[i];
36 printf(" %s %x len=%x", m->flags & I2C_M_RD ? "R" : "W",
38 if (!(m->flags & I2C_M_RD))
39 printf(": %x", m->buf[0]);
45 * i2c_setup_offset() - Set up a new message with a chip offset
48 * @offset: Byte offset within chip
49 * @offset_buf: Place to put byte offset
50 * @msg: Message buffer
51 * @return 0 if OK, -EADDRNOTAVAIL if the offset length is 0. In that case the
52 * message is still set up but will not contain an offset.
54 static int i2c_setup_offset(struct dm_i2c_chip *chip, uint offset,
55 uint8_t offset_buf[], struct i2c_msg *msg)
57 int offset_len = chip->offset_len;
59 msg->addr = chip->chip_addr;
60 if (chip->chip_addr_offset_mask)
61 msg->addr |= (offset >> (8 * offset_len)) &
62 chip->chip_addr_offset_mask;
63 msg->flags = chip->flags & DM_I2C_CHIP_10BIT ? I2C_M_TEN : 0;
64 msg->len = chip->offset_len;
65 msg->buf = offset_buf;
67 return -EADDRNOTAVAIL;
68 assert(offset_len <= I2C_MAX_OFFSET_LEN);
71 *offset_buf++ = offset >> (8 * offset_len);
76 static int i2c_read_bytewise(struct udevice *dev, uint offset,
77 uint8_t *buffer, int len)
79 struct dm_i2c_chip *chip = dev_get_parent_platdata(dev);
80 struct udevice *bus = dev_get_parent(dev);
81 struct dm_i2c_ops *ops = i2c_get_ops(bus);
82 struct i2c_msg msg[2], *ptr;
83 uint8_t offset_buf[I2C_MAX_OFFSET_LEN];
87 for (i = 0; i < len; i++) {
88 if (i2c_setup_offset(chip, offset + i, offset_buf, msg))
91 ptr->addr = msg->addr;
92 ptr->flags = msg->flags | I2C_M_RD;
94 ptr->buf = &buffer[i];
97 ret = ops->xfer(bus, msg, ptr - msg);
105 static int i2c_write_bytewise(struct udevice *dev, uint offset,
106 const uint8_t *buffer, int len)
108 struct dm_i2c_chip *chip = dev_get_parent_platdata(dev);
109 struct udevice *bus = dev_get_parent(dev);
110 struct dm_i2c_ops *ops = i2c_get_ops(bus);
111 struct i2c_msg msg[1];
112 uint8_t buf[I2C_MAX_OFFSET_LEN + 1];
116 for (i = 0; i < len; i++) {
117 if (i2c_setup_offset(chip, offset + i, buf, msg))
119 buf[msg->len++] = buffer[i];
121 ret = ops->xfer(bus, msg, 1);
129 int dm_i2c_read(struct udevice *dev, uint offset, uint8_t *buffer, int len)
131 struct dm_i2c_chip *chip = dev_get_parent_platdata(dev);
132 struct udevice *bus = dev_get_parent(dev);
133 struct dm_i2c_ops *ops = i2c_get_ops(bus);
134 struct i2c_msg msg[2], *ptr;
135 uint8_t offset_buf[I2C_MAX_OFFSET_LEN];
140 if (chip->flags & DM_I2C_CHIP_RD_ADDRESS)
141 return i2c_read_bytewise(dev, offset, buffer, len);
143 if (!i2c_setup_offset(chip, offset, offset_buf, ptr))
147 ptr->addr = msg->addr;
148 ptr->flags = chip->flags & DM_I2C_CHIP_10BIT ? I2C_M_TEN : 0;
149 ptr->flags |= I2C_M_RD;
154 msg_count = ptr - msg;
156 return ops->xfer(bus, msg, msg_count);
159 int dm_i2c_write(struct udevice *dev, uint offset, const uint8_t *buffer,
162 struct dm_i2c_chip *chip = dev_get_parent_platdata(dev);
163 struct udevice *bus = dev_get_parent(dev);
164 struct dm_i2c_ops *ops = i2c_get_ops(bus);
165 struct i2c_msg msg[1];
170 if (chip->flags & DM_I2C_CHIP_WR_ADDRESS)
171 return i2c_write_bytewise(dev, offset, buffer, len);
173 * The simple approach would be to send two messages here: one to
174 * set the offset and one to write the bytes. However some drivers
175 * will not be expecting this, and some chips won't like how the
176 * driver presents this on the I2C bus.
178 * The API does not support separate offset and data. We could extend
179 * it with a flag indicating that there is data in the next message
180 * that needs to be processed in the same transaction. We could
181 * instead add an additional buffer to each message. For now, handle
182 * this in the uclass since it isn't clear what the impact on drivers
183 * would be with this extra complication. Unfortunately this means
184 * copying the message.
186 * Use the stack for small messages, malloc() for larger ones. We
187 * need to allow space for the offset (up to 4 bytes) and the message
191 uint8_t buf[I2C_MAX_OFFSET_LEN + len];
193 i2c_setup_offset(chip, offset, buf, msg);
195 memcpy(buf + chip->offset_len, buffer, len);
197 return ops->xfer(bus, msg, 1);
202 buf = malloc(I2C_MAX_OFFSET_LEN + len);
205 i2c_setup_offset(chip, offset, buf, msg);
207 memcpy(buf + chip->offset_len, buffer, len);
209 ret = ops->xfer(bus, msg, 1);
215 int dm_i2c_xfer(struct udevice *dev, struct i2c_msg *msg, int nmsgs)
217 struct udevice *bus = dev_get_parent(dev);
218 struct dm_i2c_ops *ops = i2c_get_ops(bus);
223 return ops->xfer(bus, msg, nmsgs);
226 int dm_i2c_reg_read(struct udevice *dev, uint offset)
231 ret = dm_i2c_read(dev, offset, &val, 1);
238 int dm_i2c_reg_write(struct udevice *dev, uint offset, uint value)
242 return dm_i2c_write(dev, offset, &val, 1);
246 * i2c_probe_chip() - probe for a chip on a bus
249 * @chip_addr: Chip address to probe
250 * @flags: Flags for the chip
251 * @return 0 if found, -ENOSYS if the driver is invalid, -EREMOTEIO if the chip
252 * does not respond to probe
254 static int i2c_probe_chip(struct udevice *bus, uint chip_addr,
255 enum dm_i2c_chip_flags chip_flags)
257 struct dm_i2c_ops *ops = i2c_get_ops(bus);
258 struct i2c_msg msg[1];
261 if (ops->probe_chip) {
262 ret = ops->probe_chip(bus, chip_addr, chip_flags);
263 if (!ret || ret != -ENOSYS)
270 /* Probe with a zero-length message */
271 msg->addr = chip_addr;
272 msg->flags = chip_flags & DM_I2C_CHIP_10BIT ? I2C_M_TEN : 0;
276 return ops->xfer(bus, msg, 1);
279 static int i2c_bind_driver(struct udevice *bus, uint chip_addr, uint offset_len,
280 struct udevice **devp)
282 struct dm_i2c_chip *chip;
287 snprintf(name, sizeof(name), "generic_%x", chip_addr);
291 ret = device_bind_driver(bus, "i2c_generic_chip_drv", str, &dev);
292 debug("%s: device_bind_driver: ret=%d\n", __func__, ret);
296 /* Tell the device what we know about it */
297 chip = dev_get_parent_platdata(dev);
298 chip->chip_addr = chip_addr;
299 chip->offset_len = offset_len;
300 ret = device_probe(dev);
301 debug("%s: device_probe: ret=%d\n", __func__, ret);
310 * If the device failed to probe, unbind it. There is nothing there
311 * on the bus so we don't want to leave it lying around
319 int i2c_get_chip(struct udevice *bus, uint chip_addr, uint offset_len,
320 struct udevice **devp)
324 debug("%s: Searching bus '%s' for address %02x: ", __func__,
325 bus->name, chip_addr);
326 for (device_find_first_child(bus, &dev); dev;
327 device_find_next_child(&dev)) {
328 struct dm_i2c_chip *chip = dev_get_parent_platdata(dev);
331 if (chip->chip_addr == (chip_addr &
332 ~chip->chip_addr_offset_mask)) {
333 ret = device_probe(dev);
334 debug("found, ret=%d\n", ret);
341 debug("not found\n");
342 return i2c_bind_driver(bus, chip_addr, offset_len, devp);
345 int i2c_get_chip_for_busnum(int busnum, int chip_addr, uint offset_len,
346 struct udevice **devp)
351 ret = uclass_get_device_by_seq(UCLASS_I2C, busnum, &bus);
353 debug("Cannot find I2C bus %d\n", busnum);
357 /* detect the presence of the chip on the bus */
358 ret = i2c_probe_chip(bus, chip_addr, 0);
359 debug("%s: bus='%s', address %02x, ret=%d\n", __func__, bus->name,
362 debug("Cannot detect I2C chip %02x on bus %d\n", chip_addr,
367 ret = i2c_get_chip(bus, chip_addr, offset_len, devp);
369 debug("Cannot find I2C chip %02x on bus %d\n", chip_addr,
377 int dm_i2c_probe(struct udevice *bus, uint chip_addr, uint chip_flags,
378 struct udevice **devp)
384 /* First probe that chip */
385 ret = i2c_probe_chip(bus, chip_addr, chip_flags);
386 debug("%s: bus='%s', address %02x, ret=%d\n", __func__, bus->name,
391 /* The chip was found, see if we have a driver, and probe it */
392 ret = i2c_get_chip(bus, chip_addr, 1, devp);
393 debug("%s: i2c_get_chip: ret=%d\n", __func__, ret);
398 int dm_i2c_set_bus_speed(struct udevice *bus, unsigned int speed)
400 struct dm_i2c_ops *ops = i2c_get_ops(bus);
401 struct dm_i2c_bus *i2c = dev_get_uclass_priv(bus);
405 * If we have a method, call it. If not then the driver probably wants
406 * to deal with speed changes on the next transfer. It can easily read
407 * the current speed from this uclass
409 if (ops->set_bus_speed) {
410 ret = ops->set_bus_speed(bus, speed);
414 i2c->speed_hz = speed;
419 int dm_i2c_get_bus_speed(struct udevice *bus)
421 struct dm_i2c_ops *ops = i2c_get_ops(bus);
422 struct dm_i2c_bus *i2c = dev_get_uclass_priv(bus);
424 if (!ops->get_bus_speed)
425 return i2c->speed_hz;
427 return ops->get_bus_speed(bus);
430 int i2c_set_chip_flags(struct udevice *dev, uint flags)
432 struct udevice *bus = dev->parent;
433 struct dm_i2c_chip *chip = dev_get_parent_platdata(dev);
434 struct dm_i2c_ops *ops = i2c_get_ops(bus);
437 if (ops->set_flags) {
438 ret = ops->set_flags(dev, flags);
447 int i2c_get_chip_flags(struct udevice *dev, uint *flagsp)
449 struct dm_i2c_chip *chip = dev_get_parent_platdata(dev);
451 *flagsp = chip->flags;
456 int i2c_set_chip_offset_len(struct udevice *dev, uint offset_len)
458 struct dm_i2c_chip *chip = dev_get_parent_platdata(dev);
460 if (offset_len > I2C_MAX_OFFSET_LEN)
462 chip->offset_len = offset_len;
467 int i2c_get_chip_offset_len(struct udevice *dev)
469 struct dm_i2c_chip *chip = dev_get_parent_platdata(dev);
471 return chip->offset_len;
474 int i2c_set_chip_addr_offset_mask(struct udevice *dev, uint mask)
476 struct dm_i2c_chip *chip = dev_get_parent_platdata(dev);
478 chip->chip_addr_offset_mask = mask;
483 uint i2c_get_chip_addr_offset_mask(struct udevice *dev)
485 struct dm_i2c_chip *chip = dev_get_parent_platdata(dev);
487 return chip->chip_addr_offset_mask;
490 #if CONFIG_IS_ENABLED(DM_GPIO)
491 static void i2c_gpio_set_pin(struct gpio_desc *pin, int bit)
494 dm_gpio_set_dir_flags(pin, GPIOD_IS_IN);
496 dm_gpio_set_dir_flags(pin, GPIOD_IS_OUT |
498 GPIOD_IS_OUT_ACTIVE);
501 static int i2c_gpio_get_pin(struct gpio_desc *pin)
503 return dm_gpio_get_value(pin);
506 int i2c_deblock_gpio_loop(struct gpio_desc *sda_pin,
507 struct gpio_desc *scl_pin,
508 unsigned int scl_count,
509 unsigned int start_count,
512 int i, ret = -EREMOTEIO;
514 i2c_gpio_set_pin(sda_pin, 1);
515 i2c_gpio_set_pin(scl_pin, 1);
518 /* Toggle SCL until slave release SDA */
519 for (; scl_count; --scl_count) {
520 i2c_gpio_set_pin(scl_pin, 1);
522 i2c_gpio_set_pin(scl_pin, 0);
524 if (i2c_gpio_get_pin(sda_pin)) {
530 if (!ret && start_count) {
531 for (i = 0; i < start_count; i++) {
532 /* Send start condition */
534 i2c_gpio_set_pin(sda_pin, 1);
536 i2c_gpio_set_pin(scl_pin, 1);
538 i2c_gpio_set_pin(sda_pin, 0);
540 i2c_gpio_set_pin(scl_pin, 0);
544 /* Then, send I2C stop */
545 i2c_gpio_set_pin(sda_pin, 0);
548 i2c_gpio_set_pin(scl_pin, 1);
551 i2c_gpio_set_pin(sda_pin, 1);
554 if (!i2c_gpio_get_pin(sda_pin) || !i2c_gpio_get_pin(scl_pin))
560 static int i2c_deblock_gpio(struct udevice *bus)
562 struct gpio_desc gpios[PIN_COUNT];
565 ret = gpio_request_list_by_name(bus, "gpios", gpios,
566 ARRAY_SIZE(gpios), GPIOD_IS_IN);
567 if (ret != ARRAY_SIZE(gpios)) {
568 debug("%s: I2C Node '%s' has no 'gpios' property %s\n",
569 __func__, dev_read_name(bus), bus->name);
571 gpio_free_list(bus, gpios, ret);
577 ret = pinctrl_select_state(bus, "gpio");
579 debug("%s: I2C Node '%s' has no 'gpio' pinctrl state. %s\n",
580 __func__, dev_read_name(bus), bus->name);
584 ret0 = i2c_deblock_gpio_loop(&gpios[PIN_SDA], &gpios[PIN_SCL], 9, 0, 5);
586 ret = pinctrl_select_state(bus, "default");
588 debug("%s: I2C Node '%s' has no 'default' pinctrl state. %s\n",
589 __func__, dev_read_name(bus), bus->name);
592 ret = !ret ? ret0 : ret;
595 gpio_free_list(bus, gpios, ARRAY_SIZE(gpios));
600 static int i2c_deblock_gpio(struct udevice *bus)
606 int i2c_deblock(struct udevice *bus)
608 struct dm_i2c_ops *ops = i2c_get_ops(bus);
611 return i2c_deblock_gpio(bus);
613 return ops->deblock(bus);
616 #if CONFIG_IS_ENABLED(OF_CONTROL) && !CONFIG_IS_ENABLED(OF_PLATDATA)
617 int i2c_chip_ofdata_to_platdata(struct udevice *dev, struct dm_i2c_chip *chip)
621 chip->offset_len = dev_read_u32_default(dev, "u-boot,i2c-offset-len",
624 addr = dev_read_u32_default(dev, "reg", -1);
626 debug("%s: I2C Node '%s' has no 'reg' property %s\n", __func__,
627 dev_read_name(dev), dev->name);
630 chip->chip_addr = addr;
636 static int i2c_pre_probe(struct udevice *dev)
638 #if CONFIG_IS_ENABLED(OF_CONTROL) && !CONFIG_IS_ENABLED(OF_PLATDATA)
639 struct dm_i2c_bus *i2c = dev_get_uclass_priv(dev);
640 unsigned int max = 0;
644 i2c->max_transaction_bytes = 0;
645 dev_for_each_subnode(node, dev) {
646 ret = ofnode_read_u32(node,
647 "u-boot,i2c-transaction-bytes",
649 if (!ret && max > i2c->max_transaction_bytes)
650 i2c->max_transaction_bytes = max;
653 debug("%s: I2C bus: %s max transaction bytes: %d\n", __func__,
654 dev->name, i2c->max_transaction_bytes);
659 static int i2c_post_probe(struct udevice *dev)
661 #if CONFIG_IS_ENABLED(OF_CONTROL) && !CONFIG_IS_ENABLED(OF_PLATDATA)
662 struct dm_i2c_bus *i2c = dev_get_uclass_priv(dev);
664 i2c->speed_hz = dev_read_u32_default(dev, "clock-frequency",
665 I2C_SPEED_STANDARD_RATE);
667 return dm_i2c_set_bus_speed(dev, i2c->speed_hz);
673 static int i2c_child_post_bind(struct udevice *dev)
675 #if CONFIG_IS_ENABLED(OF_CONTROL) && !CONFIG_IS_ENABLED(OF_PLATDATA)
676 struct dm_i2c_chip *plat = dev_get_parent_platdata(dev);
678 if (!dev_of_valid(dev))
680 return i2c_chip_ofdata_to_platdata(dev, plat);
690 static int i2c_post_bind(struct udevice *dev)
692 struct uclass *class = dev->uclass;
693 struct i2c_priv *priv = class->priv;
700 debug("%s: %s, req_seq=%d\n", __func__, dev->name, dev->req_seq);
702 /* if there is no alias ID, use the first free */
703 if (dev->req_seq == -1)
704 dev->req_seq = ++priv->max_id;
706 debug("%s: %s, new req_seq=%d\n", __func__, dev->name, dev->req_seq);
708 #if CONFIG_IS_ENABLED(OF_CONTROL) && !CONFIG_IS_ENABLED(OF_PLATDATA)
709 ret = dm_scan_fdt_dev(dev);
714 int i2c_uclass_init(struct uclass *class)
716 struct i2c_priv *priv = class->priv;
722 /* Get the last allocated alias. */
723 if (CONFIG_IS_ENABLED(OF_CONTROL) && !CONFIG_IS_ENABLED(OF_PLATDATA))
724 priv->max_id = dev_read_alias_highest_id("i2c");
728 debug("%s: highest alias id is %d\n", __func__, priv->max_id);
733 UCLASS_DRIVER(i2c) = {
736 .flags = DM_UC_FLAG_SEQ_ALIAS,
737 .post_bind = i2c_post_bind,
738 .init = i2c_uclass_init,
739 .priv_auto_alloc_size = sizeof(struct i2c_priv),
740 .pre_probe = i2c_pre_probe,
741 .post_probe = i2c_post_probe,
742 .per_device_auto_alloc_size = sizeof(struct dm_i2c_bus),
743 .per_child_platdata_auto_alloc_size = sizeof(struct dm_i2c_chip),
744 .child_post_bind = i2c_child_post_bind,
747 UCLASS_DRIVER(i2c_generic) = {
748 .id = UCLASS_I2C_GENERIC,
749 .name = "i2c_generic",
752 U_BOOT_DRIVER(i2c_generic_chip_drv) = {
753 .name = "i2c_generic_chip_drv",
754 .id = UCLASS_I2C_GENERIC,