1 // SPDX-License-Identifier: GPL-2.0+
3 * Copyright (c) 2014 Google, Inc
6 #define LOG_CATEGORY UCLASS_I2C
14 #include <acpi/acpi_device.h>
16 #include <dm/device-internal.h>
18 #include <dm/pinctrl.h>
19 #if CONFIG_IS_ENABLED(DM_GPIO)
22 #include <linux/delay.h>
25 #define I2C_MAX_OFFSET_LEN 4
33 /* Useful debugging function */
34 void i2c_dump_msgs(struct i2c_msg *msg, int nmsgs)
38 for (i = 0; i < nmsgs; i++) {
39 struct i2c_msg *m = &msg[i];
41 printf(" %s %x len=%x", m->flags & I2C_M_RD ? "R" : "W",
43 if (!(m->flags & I2C_M_RD))
44 printf(": %x", m->buf[0]);
50 * i2c_setup_offset() - Set up a new message with a chip offset
53 * @offset: Byte offset within chip
54 * @offset_buf: Place to put byte offset
55 * @msg: Message buffer
56 * Return: 0 if OK, -EADDRNOTAVAIL if the offset length is 0. In that case the
57 * message is still set up but will not contain an offset.
59 static int i2c_setup_offset(struct dm_i2c_chip *chip, uint offset,
60 uint8_t offset_buf[], struct i2c_msg *msg)
62 int offset_len = chip->offset_len;
64 msg->addr = chip->chip_addr;
65 if (chip->chip_addr_offset_mask)
66 msg->addr |= (offset >> (8 * offset_len)) &
67 chip->chip_addr_offset_mask;
68 msg->flags = chip->flags & DM_I2C_CHIP_10BIT ? I2C_M_TEN : 0;
69 msg->len = chip->offset_len;
70 msg->buf = offset_buf;
72 return -EADDRNOTAVAIL;
73 assert(offset_len <= I2C_MAX_OFFSET_LEN);
76 *offset_buf++ = offset >> (8 * offset_len);
81 static int i2c_read_bytewise(struct udevice *dev, uint offset,
82 uint8_t *buffer, int len)
84 struct dm_i2c_chip *chip = dev_get_parent_plat(dev);
85 struct udevice *bus = dev_get_parent(dev);
86 struct dm_i2c_ops *ops = i2c_get_ops(bus);
87 struct i2c_msg msg[2], *ptr;
88 uint8_t offset_buf[I2C_MAX_OFFSET_LEN];
92 for (i = 0; i < len; i++) {
93 if (i2c_setup_offset(chip, offset + i, offset_buf, msg))
96 ptr->addr = msg->addr;
97 ptr->flags = msg->flags | I2C_M_RD;
99 ptr->buf = &buffer[i];
102 ret = ops->xfer(bus, msg, ptr - msg);
110 static int i2c_write_bytewise(struct udevice *dev, uint offset,
111 const uint8_t *buffer, int len)
113 struct dm_i2c_chip *chip = dev_get_parent_plat(dev);
114 struct udevice *bus = dev_get_parent(dev);
115 struct dm_i2c_ops *ops = i2c_get_ops(bus);
116 struct i2c_msg msg[1];
117 uint8_t buf[I2C_MAX_OFFSET_LEN + 1];
121 for (i = 0; i < len; i++) {
122 if (i2c_setup_offset(chip, offset + i, buf, msg))
124 buf[msg->len++] = buffer[i];
126 ret = ops->xfer(bus, msg, 1);
134 int dm_i2c_read(struct udevice *dev, uint offset, uint8_t *buffer, int len)
136 struct dm_i2c_chip *chip = dev_get_parent_plat(dev);
137 struct udevice *bus = dev_get_parent(dev);
138 struct dm_i2c_ops *ops = i2c_get_ops(bus);
139 struct i2c_msg msg[2], *ptr;
140 uint8_t offset_buf[I2C_MAX_OFFSET_LEN];
145 if (chip->flags & DM_I2C_CHIP_RD_ADDRESS)
146 return i2c_read_bytewise(dev, offset, buffer, len);
148 if (!i2c_setup_offset(chip, offset, offset_buf, ptr))
152 ptr->addr = msg->addr;
153 ptr->flags = chip->flags & DM_I2C_CHIP_10BIT ? I2C_M_TEN : 0;
154 ptr->flags |= I2C_M_RD;
159 msg_count = ptr - msg;
161 return ops->xfer(bus, msg, msg_count);
164 int dm_i2c_write(struct udevice *dev, uint offset, const uint8_t *buffer,
167 struct dm_i2c_chip *chip = dev_get_parent_plat(dev);
168 struct udevice *bus = dev_get_parent(dev);
169 struct dm_i2c_ops *ops = i2c_get_ops(bus);
170 struct i2c_msg msg[1];
171 uint8_t _buf[I2C_MAX_OFFSET_LEN + 64];
178 if (chip->flags & DM_I2C_CHIP_WR_ADDRESS)
179 return i2c_write_bytewise(dev, offset, buffer, len);
181 * The simple approach would be to send two messages here: one to
182 * set the offset and one to write the bytes. However some drivers
183 * will not be expecting this, and some chips won't like how the
184 * driver presents this on the I2C bus.
186 * The API does not support separate offset and data. We could extend
187 * it with a flag indicating that there is data in the next message
188 * that needs to be processed in the same transaction. We could
189 * instead add an additional buffer to each message. For now, handle
190 * this in the uclass since it isn't clear what the impact on drivers
191 * would be with this extra complication. Unfortunately this means
192 * copying the message.
194 * Use the stack for small messages, malloc() for larger ones. We
195 * need to allow space for the offset (up to 4 bytes) and the message
198 if (len > sizeof(_buf) - I2C_MAX_OFFSET_LEN) {
199 buf = malloc(I2C_MAX_OFFSET_LEN + len);
204 i2c_setup_offset(chip, offset, buf, msg);
206 memcpy(buf + chip->offset_len, buffer, len);
208 ret = ops->xfer(bus, msg, 1);
214 int dm_i2c_xfer(struct udevice *dev, struct i2c_msg *msg, int nmsgs)
216 struct udevice *bus = dev_get_parent(dev);
217 struct dm_i2c_ops *ops = i2c_get_ops(bus);
222 return ops->xfer(bus, msg, nmsgs);
225 int dm_i2c_reg_read(struct udevice *dev, uint offset)
230 ret = dm_i2c_read(dev, offset, &val, 1);
237 int dm_i2c_reg_write(struct udevice *dev, uint offset, uint value)
241 return dm_i2c_write(dev, offset, &val, 1);
244 int dm_i2c_reg_clrset(struct udevice *dev, uint offset, u32 clr, u32 set)
249 ret = dm_i2c_read(dev, offset, &val, 1);
256 return dm_i2c_write(dev, offset, &val, 1);
260 * i2c_probe_chip() - probe for a chip on a bus
263 * @chip_addr: Chip address to probe
264 * @flags: Flags for the chip
265 * Return: 0 if found, -ENOSYS if the driver is invalid, -EREMOTEIO if the chip
266 * does not respond to probe
268 static int i2c_probe_chip(struct udevice *bus, uint chip_addr,
269 enum dm_i2c_chip_flags chip_flags)
271 struct dm_i2c_ops *ops = i2c_get_ops(bus);
272 struct i2c_msg msg[1];
275 if (ops->probe_chip) {
276 ret = ops->probe_chip(bus, chip_addr, chip_flags);
284 /* Probe with a zero-length message */
285 msg->addr = chip_addr;
286 msg->flags = chip_flags & DM_I2C_CHIP_10BIT ? I2C_M_TEN : 0;
290 return ops->xfer(bus, msg, 1);
293 static int i2c_bind_driver(struct udevice *bus, uint chip_addr, uint offset_len,
294 struct udevice **devp)
296 struct dm_i2c_chip *chip;
301 snprintf(name, sizeof(name), "generic_%x", chip_addr);
305 ret = device_bind_driver(bus, "i2c_generic_chip_drv", str, &dev);
306 debug("%s: device_bind_driver: ret=%d\n", __func__, ret);
310 /* Tell the device what we know about it */
311 chip = dev_get_parent_plat(dev);
312 chip->chip_addr = chip_addr;
313 chip->offset_len = offset_len;
314 ret = device_probe(dev);
315 debug("%s: device_probe: ret=%d\n", __func__, ret);
324 * If the device failed to probe, unbind it. There is nothing there
325 * on the bus so we don't want to leave it lying around
333 int i2c_get_chip(struct udevice *bus, uint chip_addr, uint offset_len,
334 struct udevice **devp)
338 debug("%s: Searching bus '%s' for address %02x: ", __func__,
339 bus->name, chip_addr);
340 for (device_find_first_child(bus, &dev); dev;
341 device_find_next_child(&dev)) {
342 struct dm_i2c_chip *chip = dev_get_parent_plat(dev);
345 if (chip->chip_addr == (chip_addr &
346 ~chip->chip_addr_offset_mask)) {
347 ret = device_probe(dev);
348 debug("found, ret=%d\n", ret);
355 debug("not found\n");
356 return i2c_bind_driver(bus, chip_addr, offset_len, devp);
359 int i2c_get_chip_for_busnum(int busnum, int chip_addr, uint offset_len,
360 struct udevice **devp)
365 ret = uclass_get_device_by_seq(UCLASS_I2C, busnum, &bus);
367 debug("Cannot find I2C bus %d\n", busnum);
371 /* detect the presence of the chip on the bus */
372 ret = i2c_probe_chip(bus, chip_addr, 0);
373 debug("%s: bus='%s', address %02x, ret=%d\n", __func__, bus->name,
376 debug("Cannot detect I2C chip %02x on bus %d\n", chip_addr,
381 ret = i2c_get_chip(bus, chip_addr, offset_len, devp);
383 debug("Cannot find I2C chip %02x on bus %d\n", chip_addr,
391 /* Find and probe I2C bus based on a chip attached to it */
392 static int i2c_get_parent_bus(ofnode chip, struct udevice **devp)
398 node = ofnode_get_parent(chip);
399 if (!ofnode_valid(node))
402 ret = uclass_get_device_by_ofnode(UCLASS_I2C, node, &dev);
412 int i2c_get_chip_by_phandle(const struct udevice *parent, const char *prop_name,
413 struct udevice **devp)
417 struct udevice *bus, *chip;
421 debug("%s: Searching I2C chip for phandle \"%s\"\n",
422 __func__, prop_name);
424 dev_name = strdup(prop_name);
430 ret = dev_read_u32(parent, prop_name, &phandle);
434 node = ofnode_get_by_phandle(phandle);
435 if (!ofnode_valid(node)) {
440 ret = i2c_get_parent_bus(node, &bus);
444 ret = device_bind_driver_to_node(bus, "i2c_generic_chip_drv",
445 dev_name, node, &chip);
449 ret = device_probe(chip);
455 debug("%s succeeded\n", __func__);
461 debug("%s failed, ret = %d\n", __func__, ret);
466 int dm_i2c_probe(struct udevice *bus, uint chip_addr, uint chip_flags,
467 struct udevice **devp)
473 /* First probe that chip */
474 ret = i2c_probe_chip(bus, chip_addr, chip_flags);
475 debug("%s: bus='%s', address %02x, ret=%d\n", __func__, bus->name,
480 /* The chip was found, see if we have a driver, and probe it */
481 ret = i2c_get_chip(bus, chip_addr, 1, devp);
482 debug("%s: i2c_get_chip: ret=%d\n", __func__, ret);
487 int dm_i2c_set_bus_speed(struct udevice *bus, unsigned int speed)
489 struct dm_i2c_ops *ops = i2c_get_ops(bus);
490 struct dm_i2c_bus *i2c = dev_get_uclass_priv(bus);
494 * If we have a method, call it. If not then the driver probably wants
495 * to deal with speed changes on the next transfer. It can easily read
496 * the current speed from this uclass
498 if (ops->set_bus_speed) {
499 ret = ops->set_bus_speed(bus, speed);
503 i2c->speed_hz = speed;
508 int dm_i2c_get_bus_speed(struct udevice *bus)
510 struct dm_i2c_ops *ops = i2c_get_ops(bus);
511 struct dm_i2c_bus *i2c = dev_get_uclass_priv(bus);
513 if (!ops->get_bus_speed)
514 return i2c->speed_hz;
516 return ops->get_bus_speed(bus);
519 int i2c_set_chip_flags(struct udevice *dev, uint flags)
521 struct udevice *bus = dev->parent;
522 struct dm_i2c_chip *chip = dev_get_parent_plat(dev);
523 struct dm_i2c_ops *ops = i2c_get_ops(bus);
526 if (ops->set_flags) {
527 ret = ops->set_flags(dev, flags);
536 int i2c_get_chip_flags(struct udevice *dev, uint *flagsp)
538 struct dm_i2c_chip *chip = dev_get_parent_plat(dev);
540 *flagsp = chip->flags;
545 int i2c_set_chip_offset_len(struct udevice *dev, uint offset_len)
547 struct dm_i2c_chip *chip = dev_get_parent_plat(dev);
549 if (offset_len > I2C_MAX_OFFSET_LEN)
550 return log_ret(-EINVAL);
551 chip->offset_len = offset_len;
556 int i2c_get_chip_offset_len(struct udevice *dev)
558 struct dm_i2c_chip *chip = dev_get_parent_plat(dev);
560 return chip->offset_len;
563 int i2c_set_chip_addr_offset_mask(struct udevice *dev, uint mask)
565 struct dm_i2c_chip *chip = dev_get_parent_plat(dev);
567 chip->chip_addr_offset_mask = mask;
572 uint i2c_get_chip_addr_offset_mask(struct udevice *dev)
574 struct dm_i2c_chip *chip = dev_get_parent_plat(dev);
576 return chip->chip_addr_offset_mask;
579 #if CONFIG_IS_ENABLED(DM_GPIO)
580 static void i2c_gpio_set_pin(struct gpio_desc *pin, int bit)
583 dm_gpio_set_dir_flags(pin, GPIOD_IS_IN);
585 dm_gpio_set_dir_flags(pin, GPIOD_IS_OUT |
586 GPIOD_IS_OUT_ACTIVE);
589 static int i2c_gpio_get_pin(struct gpio_desc *pin)
591 /* DTS need config GPIO_ACTIVE_LOW */
592 return !dm_gpio_get_value(pin);
595 int i2c_deblock_gpio_loop(struct gpio_desc *sda_pin,
596 struct gpio_desc *scl_pin,
597 unsigned int scl_count,
598 unsigned int start_count,
601 int i, ret = -EREMOTEIO;
603 i2c_gpio_set_pin(sda_pin, 1);
604 i2c_gpio_set_pin(scl_pin, 1);
607 /* Toggle SCL until slave release SDA */
608 for (; scl_count; --scl_count) {
609 i2c_gpio_set_pin(scl_pin, 1);
611 i2c_gpio_set_pin(scl_pin, 0);
613 if (i2c_gpio_get_pin(sda_pin)) {
619 if (!ret && start_count) {
620 for (i = 0; i < start_count; i++) {
621 /* Send start condition */
623 i2c_gpio_set_pin(sda_pin, 1);
625 i2c_gpio_set_pin(scl_pin, 1);
627 i2c_gpio_set_pin(sda_pin, 0);
629 i2c_gpio_set_pin(scl_pin, 0);
633 /* Then, send I2C stop */
634 i2c_gpio_set_pin(sda_pin, 0);
637 i2c_gpio_set_pin(scl_pin, 1);
640 i2c_gpio_set_pin(sda_pin, 1);
643 if (!i2c_gpio_get_pin(sda_pin) || !i2c_gpio_get_pin(scl_pin))
649 static int i2c_deblock_gpio(struct udevice *bus)
651 struct gpio_desc gpios[PIN_COUNT];
654 ret = gpio_request_list_by_name(bus, "gpios", gpios,
655 ARRAY_SIZE(gpios), GPIOD_IS_IN);
656 if (ret != ARRAY_SIZE(gpios)) {
657 debug("%s: I2C Node '%s' has no 'gpios' property %s\n",
658 __func__, dev_read_name(bus), bus->name);
660 gpio_free_list(bus, gpios, ret);
666 ret = pinctrl_select_state(bus, "gpio");
668 debug("%s: I2C Node '%s' has no 'gpio' pinctrl state. %s\n",
669 __func__, dev_read_name(bus), bus->name);
673 ret0 = i2c_deblock_gpio_loop(&gpios[PIN_SDA], &gpios[PIN_SCL], 9, 0, 5);
675 ret = pinctrl_select_state(bus, "default");
677 debug("%s: I2C Node '%s' has no 'default' pinctrl state. %s\n",
678 __func__, dev_read_name(bus), bus->name);
681 ret = !ret ? ret0 : ret;
684 gpio_free_list(bus, gpios, ARRAY_SIZE(gpios));
689 static int i2c_deblock_gpio(struct udevice *bus)
695 int i2c_deblock(struct udevice *bus)
697 struct dm_i2c_ops *ops = i2c_get_ops(bus);
700 return i2c_deblock_gpio(bus);
702 return ops->deblock(bus);
705 #if CONFIG_IS_ENABLED(OF_REAL)
706 int i2c_chip_of_to_plat(struct udevice *dev, struct dm_i2c_chip *chip)
710 chip->offset_len = dev_read_u32_default(dev, "u-boot,i2c-offset-len",
713 addr = dev_read_u32_default(dev, "reg", -1);
715 debug("%s: I2C Node '%s' has no 'reg' property %s\n", __func__,
716 dev_read_name(dev), dev->name);
717 return log_ret(-EINVAL);
719 chip->chip_addr = addr;
725 static int i2c_pre_probe(struct udevice *dev)
727 #if CONFIG_IS_ENABLED(OF_REAL)
728 struct dm_i2c_bus *i2c = dev_get_uclass_priv(dev);
729 unsigned int max = 0;
733 i2c->max_transaction_bytes = 0;
734 dev_for_each_subnode(node, dev) {
735 ret = ofnode_read_u32(node,
736 "u-boot,i2c-transaction-bytes",
738 if (!ret && max > i2c->max_transaction_bytes)
739 i2c->max_transaction_bytes = max;
742 debug("%s: I2C bus: %s max transaction bytes: %d\n", __func__,
743 dev->name, i2c->max_transaction_bytes);
748 static int i2c_post_probe(struct udevice *dev)
750 #if CONFIG_IS_ENABLED(OF_REAL)
751 struct dm_i2c_bus *i2c = dev_get_uclass_priv(dev);
753 i2c->speed_hz = dev_read_u32_default(dev, "clock-frequency",
754 I2C_SPEED_STANDARD_RATE);
756 return dm_i2c_set_bus_speed(dev, i2c->speed_hz);
762 static int i2c_child_post_bind(struct udevice *dev)
764 #if CONFIG_IS_ENABLED(OF_REAL)
765 struct dm_i2c_chip *plat = dev_get_parent_plat(dev);
767 if (!dev_has_ofnode(dev))
769 return i2c_chip_of_to_plat(dev, plat);
775 static int i2c_post_bind(struct udevice *dev)
779 debug("%s: %s, seq=%d\n", __func__, dev->name, dev_seq(dev));
781 #if CONFIG_IS_ENABLED(OF_REAL)
782 ret = dm_scan_fdt_dev(dev);
787 UCLASS_DRIVER(i2c) = {
790 .flags = DM_UC_FLAG_SEQ_ALIAS,
791 .post_bind = i2c_post_bind,
792 .pre_probe = i2c_pre_probe,
793 .post_probe = i2c_post_probe,
794 .per_device_auto = sizeof(struct dm_i2c_bus),
795 .per_child_plat_auto = sizeof(struct dm_i2c_chip),
796 .child_post_bind = i2c_child_post_bind,
799 UCLASS_DRIVER(i2c_generic) = {
800 .id = UCLASS_I2C_GENERIC,
801 .name = "i2c_generic",
804 static const struct udevice_id generic_chip_i2c_ids[] = {
805 { .compatible = "i2c-chip", .data = I2C_DEVICE_GENERIC },
806 #if CONFIG_IS_ENABLED(ACPIGEN)
807 { .compatible = "hid-over-i2c", .data = I2C_DEVICE_HID_OVER_I2C },
812 U_BOOT_DRIVER(i2c_generic_chip_drv) = {
813 .name = "i2c_generic_chip_drv",
814 .id = UCLASS_I2C_GENERIC,
815 .of_match = generic_chip_i2c_ids,
816 #if CONFIG_IS_ENABLED(ACPIGEN)
817 .of_to_plat = acpi_i2c_of_to_plat,
818 .priv_auto = sizeof(struct acpi_i2c_priv),
820 ACPI_OPS_PTR(&acpi_i2c_ops)