1 // SPDX-License-Identifier: GPL-2.0+
3 * Copyright (c) 2014 Google, Inc
6 #define LOG_CATEGORY UCLASS_I2C
13 #include <acpi/acpi_device.h>
15 #include <dm/device-internal.h>
17 #include <dm/pinctrl.h>
18 #if CONFIG_IS_ENABLED(DM_GPIO)
21 #include <linux/delay.h>
24 #define I2C_MAX_OFFSET_LEN 4
32 /* Useful debugging function */
33 void i2c_dump_msgs(struct i2c_msg *msg, int nmsgs)
37 for (i = 0; i < nmsgs; i++) {
38 struct i2c_msg *m = &msg[i];
40 printf(" %s %x len=%x", m->flags & I2C_M_RD ? "R" : "W",
42 if (!(m->flags & I2C_M_RD))
43 printf(": %x", m->buf[0]);
49 * i2c_setup_offset() - Set up a new message with a chip offset
52 * @offset: Byte offset within chip
53 * @offset_buf: Place to put byte offset
54 * @msg: Message buffer
55 * Return: 0 if OK, -EADDRNOTAVAIL if the offset length is 0. In that case the
56 * message is still set up but will not contain an offset.
58 static int i2c_setup_offset(struct dm_i2c_chip *chip, uint offset,
59 uint8_t offset_buf[], struct i2c_msg *msg)
61 int offset_len = chip->offset_len;
63 msg->addr = chip->chip_addr;
64 if (chip->chip_addr_offset_mask)
65 msg->addr |= (offset >> (8 * offset_len)) &
66 chip->chip_addr_offset_mask;
67 msg->flags = chip->flags & DM_I2C_CHIP_10BIT ? I2C_M_TEN : 0;
68 msg->len = chip->offset_len;
69 msg->buf = offset_buf;
71 return -EADDRNOTAVAIL;
72 assert(offset_len <= I2C_MAX_OFFSET_LEN);
75 *offset_buf++ = offset >> (8 * offset_len);
80 static int i2c_read_bytewise(struct udevice *dev, uint offset,
81 uint8_t *buffer, int len)
83 struct dm_i2c_chip *chip = dev_get_parent_plat(dev);
84 struct udevice *bus = dev_get_parent(dev);
85 struct dm_i2c_ops *ops = i2c_get_ops(bus);
86 struct i2c_msg msg[2], *ptr;
87 uint8_t offset_buf[I2C_MAX_OFFSET_LEN];
91 for (i = 0; i < len; i++) {
92 if (i2c_setup_offset(chip, offset + i, offset_buf, msg))
95 ptr->addr = msg->addr;
96 ptr->flags = msg->flags | I2C_M_RD;
98 ptr->buf = &buffer[i];
101 ret = ops->xfer(bus, msg, ptr - msg);
109 static int i2c_write_bytewise(struct udevice *dev, uint offset,
110 const uint8_t *buffer, int len)
112 struct dm_i2c_chip *chip = dev_get_parent_plat(dev);
113 struct udevice *bus = dev_get_parent(dev);
114 struct dm_i2c_ops *ops = i2c_get_ops(bus);
115 struct i2c_msg msg[1];
116 uint8_t buf[I2C_MAX_OFFSET_LEN + 1];
120 for (i = 0; i < len; i++) {
121 if (i2c_setup_offset(chip, offset + i, buf, msg))
123 buf[msg->len++] = buffer[i];
125 ret = ops->xfer(bus, msg, 1);
133 int dm_i2c_read(struct udevice *dev, uint offset, uint8_t *buffer, int len)
135 struct dm_i2c_chip *chip = dev_get_parent_plat(dev);
136 struct udevice *bus = dev_get_parent(dev);
137 struct dm_i2c_ops *ops = i2c_get_ops(bus);
138 struct i2c_msg msg[2], *ptr;
139 uint8_t offset_buf[I2C_MAX_OFFSET_LEN];
144 if (chip->flags & DM_I2C_CHIP_RD_ADDRESS)
145 return i2c_read_bytewise(dev, offset, buffer, len);
147 if (!i2c_setup_offset(chip, offset, offset_buf, ptr))
151 ptr->addr = msg->addr;
152 ptr->flags = chip->flags & DM_I2C_CHIP_10BIT ? I2C_M_TEN : 0;
153 ptr->flags |= I2C_M_RD;
158 msg_count = ptr - msg;
160 return ops->xfer(bus, msg, msg_count);
163 int dm_i2c_write(struct udevice *dev, uint offset, const uint8_t *buffer,
166 struct dm_i2c_chip *chip = dev_get_parent_plat(dev);
167 struct udevice *bus = dev_get_parent(dev);
168 struct dm_i2c_ops *ops = i2c_get_ops(bus);
169 struct i2c_msg msg[1];
170 uint8_t _buf[I2C_MAX_OFFSET_LEN + 64];
177 if (chip->flags & DM_I2C_CHIP_WR_ADDRESS)
178 return i2c_write_bytewise(dev, offset, buffer, len);
180 * The simple approach would be to send two messages here: one to
181 * set the offset and one to write the bytes. However some drivers
182 * will not be expecting this, and some chips won't like how the
183 * driver presents this on the I2C bus.
185 * The API does not support separate offset and data. We could extend
186 * it with a flag indicating that there is data in the next message
187 * that needs to be processed in the same transaction. We could
188 * instead add an additional buffer to each message. For now, handle
189 * this in the uclass since it isn't clear what the impact on drivers
190 * would be with this extra complication. Unfortunately this means
191 * copying the message.
193 * Use the stack for small messages, malloc() for larger ones. We
194 * need to allow space for the offset (up to 4 bytes) and the message
197 if (len > sizeof(_buf) - I2C_MAX_OFFSET_LEN) {
198 buf = malloc(I2C_MAX_OFFSET_LEN + len);
203 i2c_setup_offset(chip, offset, buf, msg);
205 memcpy(buf + chip->offset_len, buffer, len);
207 ret = ops->xfer(bus, msg, 1);
213 int dm_i2c_xfer(struct udevice *dev, struct i2c_msg *msg, int nmsgs)
215 struct udevice *bus = dev_get_parent(dev);
216 struct dm_i2c_ops *ops = i2c_get_ops(bus);
221 return ops->xfer(bus, msg, nmsgs);
224 int dm_i2c_reg_read(struct udevice *dev, uint offset)
229 ret = dm_i2c_read(dev, offset, &val, 1);
236 int dm_i2c_reg_write(struct udevice *dev, uint offset, uint value)
240 return dm_i2c_write(dev, offset, &val, 1);
243 int dm_i2c_reg_clrset(struct udevice *dev, uint offset, u32 clr, u32 set)
248 ret = dm_i2c_read(dev, offset, &val, 1);
255 return dm_i2c_write(dev, offset, &val, 1);
259 * i2c_probe_chip() - probe for a chip on a bus
262 * @chip_addr: Chip address to probe
263 * @flags: Flags for the chip
264 * Return: 0 if found, -ENOSYS if the driver is invalid, -EREMOTEIO if the chip
265 * does not respond to probe
267 static int i2c_probe_chip(struct udevice *bus, uint chip_addr,
268 enum dm_i2c_chip_flags chip_flags)
270 struct dm_i2c_ops *ops = i2c_get_ops(bus);
271 struct i2c_msg msg[1];
274 if (ops->probe_chip) {
275 ret = ops->probe_chip(bus, chip_addr, chip_flags);
283 /* Probe with a zero-length message */
284 msg->addr = chip_addr;
285 msg->flags = chip_flags & DM_I2C_CHIP_10BIT ? I2C_M_TEN : 0;
289 return ops->xfer(bus, msg, 1);
292 static int i2c_bind_driver(struct udevice *bus, uint chip_addr, uint offset_len,
293 struct udevice **devp)
295 struct dm_i2c_chip *chip;
300 snprintf(name, sizeof(name), "generic_%x", chip_addr);
304 ret = device_bind_driver(bus, "i2c_generic_chip_drv", str, &dev);
305 debug("%s: device_bind_driver: ret=%d\n", __func__, ret);
309 /* Tell the device what we know about it */
310 chip = dev_get_parent_plat(dev);
311 chip->chip_addr = chip_addr;
312 chip->offset_len = offset_len;
313 ret = device_probe(dev);
314 debug("%s: device_probe: ret=%d\n", __func__, ret);
323 * If the device failed to probe, unbind it. There is nothing there
324 * on the bus so we don't want to leave it lying around
332 int i2c_get_chip(struct udevice *bus, uint chip_addr, uint offset_len,
333 struct udevice **devp)
337 debug("%s: Searching bus '%s' for address %02x: ", __func__,
338 bus->name, chip_addr);
339 for (device_find_first_child(bus, &dev); dev;
340 device_find_next_child(&dev)) {
341 struct dm_i2c_chip *chip = dev_get_parent_plat(dev);
344 if (chip->chip_addr == (chip_addr &
345 ~chip->chip_addr_offset_mask)) {
346 ret = device_probe(dev);
347 debug("found, ret=%d\n", ret);
354 debug("not found\n");
355 return i2c_bind_driver(bus, chip_addr, offset_len, devp);
358 int i2c_get_chip_for_busnum(int busnum, int chip_addr, uint offset_len,
359 struct udevice **devp)
364 ret = uclass_get_device_by_seq(UCLASS_I2C, busnum, &bus);
366 debug("Cannot find I2C bus %d\n", busnum);
370 /* detect the presence of the chip on the bus */
371 ret = i2c_probe_chip(bus, chip_addr, 0);
372 debug("%s: bus='%s', address %02x, ret=%d\n", __func__, bus->name,
375 debug("Cannot detect I2C chip %02x on bus %d\n", chip_addr,
380 ret = i2c_get_chip(bus, chip_addr, offset_len, devp);
382 debug("Cannot find I2C chip %02x on bus %d\n", chip_addr,
390 /* Find and probe I2C bus based on a chip attached to it */
391 static int i2c_get_parent_bus(ofnode chip, struct udevice **devp)
397 node = ofnode_get_parent(chip);
398 if (!ofnode_valid(node))
401 ret = uclass_get_device_by_ofnode(UCLASS_I2C, node, &dev);
411 int i2c_get_chip_by_phandle(const struct udevice *parent, const char *prop_name,
412 struct udevice **devp)
416 struct udevice *bus, *chip;
420 debug("%s: Searching I2C chip for phandle \"%s\"\n",
421 __func__, prop_name);
423 dev_name = strdup(prop_name);
429 ret = dev_read_u32(parent, prop_name, &phandle);
433 node = ofnode_get_by_phandle(phandle);
434 if (!ofnode_valid(node)) {
439 ret = i2c_get_parent_bus(node, &bus);
443 ret = device_bind_driver_to_node(bus, "i2c_generic_chip_drv",
444 dev_name, node, &chip);
448 ret = device_probe(chip);
454 debug("%s succeeded\n", __func__);
460 debug("%s failed, ret = %d\n", __func__, ret);
465 int dm_i2c_probe(struct udevice *bus, uint chip_addr, uint chip_flags,
466 struct udevice **devp)
472 /* First probe that chip */
473 ret = i2c_probe_chip(bus, chip_addr, chip_flags);
474 debug("%s: bus='%s', address %02x, ret=%d\n", __func__, bus->name,
479 /* The chip was found, see if we have a driver, and probe it */
480 ret = i2c_get_chip(bus, chip_addr, 1, devp);
481 debug("%s: i2c_get_chip: ret=%d\n", __func__, ret);
486 int dm_i2c_set_bus_speed(struct udevice *bus, unsigned int speed)
488 struct dm_i2c_ops *ops = i2c_get_ops(bus);
489 struct dm_i2c_bus *i2c = dev_get_uclass_priv(bus);
493 * If we have a method, call it. If not then the driver probably wants
494 * to deal with speed changes on the next transfer. It can easily read
495 * the current speed from this uclass
497 if (ops->set_bus_speed) {
498 ret = ops->set_bus_speed(bus, speed);
502 i2c->speed_hz = speed;
507 int dm_i2c_get_bus_speed(struct udevice *bus)
509 struct dm_i2c_ops *ops = i2c_get_ops(bus);
510 struct dm_i2c_bus *i2c = dev_get_uclass_priv(bus);
512 if (!ops->get_bus_speed)
513 return i2c->speed_hz;
515 return ops->get_bus_speed(bus);
518 int i2c_set_chip_flags(struct udevice *dev, uint flags)
520 struct udevice *bus = dev->parent;
521 struct dm_i2c_chip *chip = dev_get_parent_plat(dev);
522 struct dm_i2c_ops *ops = i2c_get_ops(bus);
525 if (ops->set_flags) {
526 ret = ops->set_flags(dev, flags);
535 int i2c_get_chip_flags(struct udevice *dev, uint *flagsp)
537 struct dm_i2c_chip *chip = dev_get_parent_plat(dev);
539 *flagsp = chip->flags;
544 int i2c_set_chip_offset_len(struct udevice *dev, uint offset_len)
546 struct dm_i2c_chip *chip = dev_get_parent_plat(dev);
548 if (offset_len > I2C_MAX_OFFSET_LEN)
549 return log_ret(-EINVAL);
550 chip->offset_len = offset_len;
555 int i2c_get_chip_offset_len(struct udevice *dev)
557 struct dm_i2c_chip *chip = dev_get_parent_plat(dev);
559 return chip->offset_len;
562 int i2c_set_chip_addr_offset_mask(struct udevice *dev, uint mask)
564 struct dm_i2c_chip *chip = dev_get_parent_plat(dev);
566 chip->chip_addr_offset_mask = mask;
571 uint i2c_get_chip_addr_offset_mask(struct udevice *dev)
573 struct dm_i2c_chip *chip = dev_get_parent_plat(dev);
575 return chip->chip_addr_offset_mask;
578 #if CONFIG_IS_ENABLED(DM_GPIO)
579 static void i2c_gpio_set_pin(struct gpio_desc *pin, int bit)
582 dm_gpio_set_dir_flags(pin, GPIOD_IS_IN);
584 dm_gpio_set_dir_flags(pin, GPIOD_IS_OUT |
585 GPIOD_IS_OUT_ACTIVE);
588 static int i2c_gpio_get_pin(struct gpio_desc *pin)
590 /* DTS need config GPIO_ACTIVE_LOW */
591 return !dm_gpio_get_value(pin);
594 int i2c_deblock_gpio_loop(struct gpio_desc *sda_pin,
595 struct gpio_desc *scl_pin,
596 unsigned int scl_count,
597 unsigned int start_count,
600 int i, ret = -EREMOTEIO;
602 i2c_gpio_set_pin(sda_pin, 1);
603 i2c_gpio_set_pin(scl_pin, 1);
606 /* Toggle SCL until slave release SDA */
607 for (; scl_count; --scl_count) {
608 i2c_gpio_set_pin(scl_pin, 1);
610 i2c_gpio_set_pin(scl_pin, 0);
612 if (i2c_gpio_get_pin(sda_pin)) {
618 if (!ret && start_count) {
619 for (i = 0; i < start_count; i++) {
620 /* Send start condition */
622 i2c_gpio_set_pin(sda_pin, 1);
624 i2c_gpio_set_pin(scl_pin, 1);
626 i2c_gpio_set_pin(sda_pin, 0);
628 i2c_gpio_set_pin(scl_pin, 0);
632 /* Then, send I2C stop */
633 i2c_gpio_set_pin(sda_pin, 0);
636 i2c_gpio_set_pin(scl_pin, 1);
639 i2c_gpio_set_pin(sda_pin, 1);
642 if (!i2c_gpio_get_pin(sda_pin) || !i2c_gpio_get_pin(scl_pin))
648 static int i2c_deblock_gpio(struct udevice *bus)
650 struct gpio_desc gpios[PIN_COUNT];
653 ret = gpio_request_list_by_name(bus, "gpios", gpios,
654 ARRAY_SIZE(gpios), GPIOD_IS_IN);
655 if (ret != ARRAY_SIZE(gpios)) {
656 debug("%s: I2C Node '%s' has no 'gpios' property %s\n",
657 __func__, dev_read_name(bus), bus->name);
659 gpio_free_list(bus, gpios, ret);
665 ret = pinctrl_select_state(bus, "gpio");
667 debug("%s: I2C Node '%s' has no 'gpio' pinctrl state. %s\n",
668 __func__, dev_read_name(bus), bus->name);
672 ret0 = i2c_deblock_gpio_loop(&gpios[PIN_SDA], &gpios[PIN_SCL], 9, 0, 5);
674 ret = pinctrl_select_state(bus, "default");
676 debug("%s: I2C Node '%s' has no 'default' pinctrl state. %s\n",
677 __func__, dev_read_name(bus), bus->name);
680 ret = !ret ? ret0 : ret;
683 gpio_free_list(bus, gpios, ARRAY_SIZE(gpios));
688 static int i2c_deblock_gpio(struct udevice *bus)
694 int i2c_deblock(struct udevice *bus)
696 struct dm_i2c_ops *ops = i2c_get_ops(bus);
699 return i2c_deblock_gpio(bus);
701 return ops->deblock(bus);
704 #if CONFIG_IS_ENABLED(OF_REAL)
705 int i2c_chip_of_to_plat(struct udevice *dev, struct dm_i2c_chip *chip)
709 chip->offset_len = dev_read_u32_default(dev, "u-boot,i2c-offset-len",
712 addr = dev_read_u32_default(dev, "reg", -1);
714 debug("%s: I2C Node '%s' has no 'reg' property %s\n", __func__,
715 dev_read_name(dev), dev->name);
716 return log_ret(-EINVAL);
718 chip->chip_addr = addr;
724 static int i2c_pre_probe(struct udevice *dev)
726 #if CONFIG_IS_ENABLED(OF_REAL)
727 struct dm_i2c_bus *i2c = dev_get_uclass_priv(dev);
728 unsigned int max = 0;
732 i2c->max_transaction_bytes = 0;
733 dev_for_each_subnode(node, dev) {
734 ret = ofnode_read_u32(node,
735 "u-boot,i2c-transaction-bytes",
737 if (!ret && max > i2c->max_transaction_bytes)
738 i2c->max_transaction_bytes = max;
741 debug("%s: I2C bus: %s max transaction bytes: %d\n", __func__,
742 dev->name, i2c->max_transaction_bytes);
747 static int i2c_post_probe(struct udevice *dev)
749 #if CONFIG_IS_ENABLED(OF_REAL)
750 struct dm_i2c_bus *i2c = dev_get_uclass_priv(dev);
752 i2c->speed_hz = dev_read_u32_default(dev, "clock-frequency",
753 I2C_SPEED_STANDARD_RATE);
755 return dm_i2c_set_bus_speed(dev, i2c->speed_hz);
761 static int i2c_child_post_bind(struct udevice *dev)
763 #if CONFIG_IS_ENABLED(OF_REAL)
764 struct dm_i2c_chip *plat = dev_get_parent_plat(dev);
766 if (!dev_has_ofnode(dev))
768 return i2c_chip_of_to_plat(dev, plat);
774 static int i2c_post_bind(struct udevice *dev)
778 debug("%s: %s, seq=%d\n", __func__, dev->name, dev_seq(dev));
780 #if CONFIG_IS_ENABLED(OF_REAL)
781 ret = dm_scan_fdt_dev(dev);
786 UCLASS_DRIVER(i2c) = {
789 .flags = DM_UC_FLAG_SEQ_ALIAS,
790 .post_bind = i2c_post_bind,
791 .pre_probe = i2c_pre_probe,
792 .post_probe = i2c_post_probe,
793 .per_device_auto = sizeof(struct dm_i2c_bus),
794 .per_child_plat_auto = sizeof(struct dm_i2c_chip),
795 .child_post_bind = i2c_child_post_bind,
798 UCLASS_DRIVER(i2c_generic) = {
799 .id = UCLASS_I2C_GENERIC,
800 .name = "i2c_generic",
803 static const struct udevice_id generic_chip_i2c_ids[] = {
804 { .compatible = "i2c-chip", .data = I2C_DEVICE_GENERIC },
805 #if CONFIG_IS_ENABLED(ACPIGEN)
806 { .compatible = "hid-over-i2c", .data = I2C_DEVICE_HID_OVER_I2C },
811 U_BOOT_DRIVER(i2c_generic_chip_drv) = {
812 .name = "i2c_generic_chip_drv",
813 .id = UCLASS_I2C_GENERIC,
814 .of_match = generic_chip_i2c_ids,
815 #if CONFIG_IS_ENABLED(ACPIGEN)
816 .of_to_plat = acpi_i2c_of_to_plat,
817 .priv_auto = sizeof(struct acpi_i2c_priv),
819 ACPI_OPS_PTR(&acpi_i2c_ops)