1 // SPDX-License-Identifier: GPL-2.0+
3 * Copyright (c) 2014 Google, Inc
12 #include <acpi/acpi_device.h>
14 #include <dm/device-internal.h>
16 #include <dm/pinctrl.h>
17 #if CONFIG_IS_ENABLED(DM_GPIO)
20 #include <linux/delay.h>
23 #define I2C_MAX_OFFSET_LEN 4
31 /* Useful debugging function */
32 void i2c_dump_msgs(struct i2c_msg *msg, int nmsgs)
36 for (i = 0; i < nmsgs; i++) {
37 struct i2c_msg *m = &msg[i];
39 printf(" %s %x len=%x", m->flags & I2C_M_RD ? "R" : "W",
41 if (!(m->flags & I2C_M_RD))
42 printf(": %x", m->buf[0]);
48 * i2c_setup_offset() - Set up a new message with a chip offset
51 * @offset: Byte offset within chip
52 * @offset_buf: Place to put byte offset
53 * @msg: Message buffer
54 * @return 0 if OK, -EADDRNOTAVAIL if the offset length is 0. In that case the
55 * message is still set up but will not contain an offset.
57 static int i2c_setup_offset(struct dm_i2c_chip *chip, uint offset,
58 uint8_t offset_buf[], struct i2c_msg *msg)
60 int offset_len = chip->offset_len;
62 msg->addr = chip->chip_addr;
63 if (chip->chip_addr_offset_mask)
64 msg->addr |= (offset >> (8 * offset_len)) &
65 chip->chip_addr_offset_mask;
66 msg->flags = chip->flags & DM_I2C_CHIP_10BIT ? I2C_M_TEN : 0;
67 msg->len = chip->offset_len;
68 msg->buf = offset_buf;
70 return -EADDRNOTAVAIL;
71 assert(offset_len <= I2C_MAX_OFFSET_LEN);
74 *offset_buf++ = offset >> (8 * offset_len);
79 static int i2c_read_bytewise(struct udevice *dev, uint offset,
80 uint8_t *buffer, int len)
82 struct dm_i2c_chip *chip = dev_get_parent_plat(dev);
83 struct udevice *bus = dev_get_parent(dev);
84 struct dm_i2c_ops *ops = i2c_get_ops(bus);
85 struct i2c_msg msg[2], *ptr;
86 uint8_t offset_buf[I2C_MAX_OFFSET_LEN];
90 for (i = 0; i < len; i++) {
91 if (i2c_setup_offset(chip, offset + i, offset_buf, msg))
94 ptr->addr = msg->addr;
95 ptr->flags = msg->flags | I2C_M_RD;
97 ptr->buf = &buffer[i];
100 ret = ops->xfer(bus, msg, ptr - msg);
108 static int i2c_write_bytewise(struct udevice *dev, uint offset,
109 const uint8_t *buffer, int len)
111 struct dm_i2c_chip *chip = dev_get_parent_plat(dev);
112 struct udevice *bus = dev_get_parent(dev);
113 struct dm_i2c_ops *ops = i2c_get_ops(bus);
114 struct i2c_msg msg[1];
115 uint8_t buf[I2C_MAX_OFFSET_LEN + 1];
119 for (i = 0; i < len; i++) {
120 if (i2c_setup_offset(chip, offset + i, buf, msg))
122 buf[msg->len++] = buffer[i];
124 ret = ops->xfer(bus, msg, 1);
132 int dm_i2c_read(struct udevice *dev, uint offset, uint8_t *buffer, int len)
134 struct dm_i2c_chip *chip = dev_get_parent_plat(dev);
135 struct udevice *bus = dev_get_parent(dev);
136 struct dm_i2c_ops *ops = i2c_get_ops(bus);
137 struct i2c_msg msg[2], *ptr;
138 uint8_t offset_buf[I2C_MAX_OFFSET_LEN];
143 if (chip->flags & DM_I2C_CHIP_RD_ADDRESS)
144 return i2c_read_bytewise(dev, offset, buffer, len);
146 if (!i2c_setup_offset(chip, offset, offset_buf, ptr))
150 ptr->addr = msg->addr;
151 ptr->flags = chip->flags & DM_I2C_CHIP_10BIT ? I2C_M_TEN : 0;
152 ptr->flags |= I2C_M_RD;
157 msg_count = ptr - msg;
159 return ops->xfer(bus, msg, msg_count);
162 int dm_i2c_write(struct udevice *dev, uint offset, const uint8_t *buffer,
165 struct dm_i2c_chip *chip = dev_get_parent_plat(dev);
166 struct udevice *bus = dev_get_parent(dev);
167 struct dm_i2c_ops *ops = i2c_get_ops(bus);
168 struct i2c_msg msg[1];
173 if (chip->flags & DM_I2C_CHIP_WR_ADDRESS)
174 return i2c_write_bytewise(dev, offset, buffer, len);
176 * The simple approach would be to send two messages here: one to
177 * set the offset and one to write the bytes. However some drivers
178 * will not be expecting this, and some chips won't like how the
179 * driver presents this on the I2C bus.
181 * The API does not support separate offset and data. We could extend
182 * it with a flag indicating that there is data in the next message
183 * that needs to be processed in the same transaction. We could
184 * instead add an additional buffer to each message. For now, handle
185 * this in the uclass since it isn't clear what the impact on drivers
186 * would be with this extra complication. Unfortunately this means
187 * copying the message.
189 * Use the stack for small messages, malloc() for larger ones. We
190 * need to allow space for the offset (up to 4 bytes) and the message
194 uint8_t buf[I2C_MAX_OFFSET_LEN + len];
196 i2c_setup_offset(chip, offset, buf, msg);
198 memcpy(buf + chip->offset_len, buffer, len);
200 return ops->xfer(bus, msg, 1);
205 buf = malloc(I2C_MAX_OFFSET_LEN + len);
208 i2c_setup_offset(chip, offset, buf, msg);
210 memcpy(buf + chip->offset_len, buffer, len);
212 ret = ops->xfer(bus, msg, 1);
218 int dm_i2c_xfer(struct udevice *dev, struct i2c_msg *msg, int nmsgs)
220 struct udevice *bus = dev_get_parent(dev);
221 struct dm_i2c_ops *ops = i2c_get_ops(bus);
226 return ops->xfer(bus, msg, nmsgs);
229 int dm_i2c_reg_read(struct udevice *dev, uint offset)
234 ret = dm_i2c_read(dev, offset, &val, 1);
241 int dm_i2c_reg_write(struct udevice *dev, uint offset, uint value)
245 return dm_i2c_write(dev, offset, &val, 1);
249 * i2c_probe_chip() - probe for a chip on a bus
252 * @chip_addr: Chip address to probe
253 * @flags: Flags for the chip
254 * @return 0 if found, -ENOSYS if the driver is invalid, -EREMOTEIO if the chip
255 * does not respond to probe
257 static int i2c_probe_chip(struct udevice *bus, uint chip_addr,
258 enum dm_i2c_chip_flags chip_flags)
260 struct dm_i2c_ops *ops = i2c_get_ops(bus);
261 struct i2c_msg msg[1];
264 if (ops->probe_chip) {
265 ret = ops->probe_chip(bus, chip_addr, chip_flags);
266 if (!ret || ret != -ENOSYS)
273 /* Probe with a zero-length message */
274 msg->addr = chip_addr;
275 msg->flags = chip_flags & DM_I2C_CHIP_10BIT ? I2C_M_TEN : 0;
279 return ops->xfer(bus, msg, 1);
282 static int i2c_bind_driver(struct udevice *bus, uint chip_addr, uint offset_len,
283 struct udevice **devp)
285 struct dm_i2c_chip *chip;
290 snprintf(name, sizeof(name), "generic_%x", chip_addr);
294 ret = device_bind_driver(bus, "i2c_generic_chip_drv", str, &dev);
295 debug("%s: device_bind_driver: ret=%d\n", __func__, ret);
299 /* Tell the device what we know about it */
300 chip = dev_get_parent_plat(dev);
301 chip->chip_addr = chip_addr;
302 chip->offset_len = offset_len;
303 ret = device_probe(dev);
304 debug("%s: device_probe: ret=%d\n", __func__, ret);
313 * If the device failed to probe, unbind it. There is nothing there
314 * on the bus so we don't want to leave it lying around
322 int i2c_get_chip(struct udevice *bus, uint chip_addr, uint offset_len,
323 struct udevice **devp)
327 debug("%s: Searching bus '%s' for address %02x: ", __func__,
328 bus->name, chip_addr);
329 for (device_find_first_child(bus, &dev); dev;
330 device_find_next_child(&dev)) {
331 struct dm_i2c_chip *chip = dev_get_parent_plat(dev);
334 if (chip->chip_addr == (chip_addr &
335 ~chip->chip_addr_offset_mask)) {
336 ret = device_probe(dev);
337 debug("found, ret=%d\n", ret);
344 debug("not found\n");
345 return i2c_bind_driver(bus, chip_addr, offset_len, devp);
348 int i2c_get_chip_for_busnum(int busnum, int chip_addr, uint offset_len,
349 struct udevice **devp)
354 ret = uclass_get_device_by_seq(UCLASS_I2C, busnum, &bus);
356 debug("Cannot find I2C bus %d\n", busnum);
360 /* detect the presence of the chip on the bus */
361 ret = i2c_probe_chip(bus, chip_addr, 0);
362 debug("%s: bus='%s', address %02x, ret=%d\n", __func__, bus->name,
365 debug("Cannot detect I2C chip %02x on bus %d\n", chip_addr,
370 ret = i2c_get_chip(bus, chip_addr, offset_len, devp);
372 debug("Cannot find I2C chip %02x on bus %d\n", chip_addr,
380 int dm_i2c_probe(struct udevice *bus, uint chip_addr, uint chip_flags,
381 struct udevice **devp)
387 /* First probe that chip */
388 ret = i2c_probe_chip(bus, chip_addr, chip_flags);
389 debug("%s: bus='%s', address %02x, ret=%d\n", __func__, bus->name,
394 /* The chip was found, see if we have a driver, and probe it */
395 ret = i2c_get_chip(bus, chip_addr, 1, devp);
396 debug("%s: i2c_get_chip: ret=%d\n", __func__, ret);
401 int dm_i2c_set_bus_speed(struct udevice *bus, unsigned int speed)
403 struct dm_i2c_ops *ops = i2c_get_ops(bus);
404 struct dm_i2c_bus *i2c = dev_get_uclass_priv(bus);
408 * If we have a method, call it. If not then the driver probably wants
409 * to deal with speed changes on the next transfer. It can easily read
410 * the current speed from this uclass
412 if (ops->set_bus_speed) {
413 ret = ops->set_bus_speed(bus, speed);
417 i2c->speed_hz = speed;
422 int dm_i2c_get_bus_speed(struct udevice *bus)
424 struct dm_i2c_ops *ops = i2c_get_ops(bus);
425 struct dm_i2c_bus *i2c = dev_get_uclass_priv(bus);
427 if (!ops->get_bus_speed)
428 return i2c->speed_hz;
430 return ops->get_bus_speed(bus);
433 int i2c_set_chip_flags(struct udevice *dev, uint flags)
435 struct udevice *bus = dev->parent;
436 struct dm_i2c_chip *chip = dev_get_parent_plat(dev);
437 struct dm_i2c_ops *ops = i2c_get_ops(bus);
440 if (ops->set_flags) {
441 ret = ops->set_flags(dev, flags);
450 int i2c_get_chip_flags(struct udevice *dev, uint *flagsp)
452 struct dm_i2c_chip *chip = dev_get_parent_plat(dev);
454 *flagsp = chip->flags;
459 int i2c_set_chip_offset_len(struct udevice *dev, uint offset_len)
461 struct dm_i2c_chip *chip = dev_get_parent_plat(dev);
463 if (offset_len > I2C_MAX_OFFSET_LEN)
464 return log_ret(-EINVAL);
465 chip->offset_len = offset_len;
470 int i2c_get_chip_offset_len(struct udevice *dev)
472 struct dm_i2c_chip *chip = dev_get_parent_plat(dev);
474 return chip->offset_len;
477 int i2c_set_chip_addr_offset_mask(struct udevice *dev, uint mask)
479 struct dm_i2c_chip *chip = dev_get_parent_plat(dev);
481 chip->chip_addr_offset_mask = mask;
486 uint i2c_get_chip_addr_offset_mask(struct udevice *dev)
488 struct dm_i2c_chip *chip = dev_get_parent_plat(dev);
490 return chip->chip_addr_offset_mask;
493 #if CONFIG_IS_ENABLED(DM_GPIO)
494 static void i2c_gpio_set_pin(struct gpio_desc *pin, int bit)
497 dm_gpio_set_dir_flags(pin, GPIOD_IS_IN);
499 dm_gpio_set_dir_flags(pin, GPIOD_IS_OUT |
501 GPIOD_IS_OUT_ACTIVE);
504 static int i2c_gpio_get_pin(struct gpio_desc *pin)
506 return dm_gpio_get_value(pin);
509 int i2c_deblock_gpio_loop(struct gpio_desc *sda_pin,
510 struct gpio_desc *scl_pin,
511 unsigned int scl_count,
512 unsigned int start_count,
515 int i, ret = -EREMOTEIO;
517 i2c_gpio_set_pin(sda_pin, 1);
518 i2c_gpio_set_pin(scl_pin, 1);
521 /* Toggle SCL until slave release SDA */
522 for (; scl_count; --scl_count) {
523 i2c_gpio_set_pin(scl_pin, 1);
525 i2c_gpio_set_pin(scl_pin, 0);
527 if (i2c_gpio_get_pin(sda_pin)) {
533 if (!ret && start_count) {
534 for (i = 0; i < start_count; i++) {
535 /* Send start condition */
537 i2c_gpio_set_pin(sda_pin, 1);
539 i2c_gpio_set_pin(scl_pin, 1);
541 i2c_gpio_set_pin(sda_pin, 0);
543 i2c_gpio_set_pin(scl_pin, 0);
547 /* Then, send I2C stop */
548 i2c_gpio_set_pin(sda_pin, 0);
551 i2c_gpio_set_pin(scl_pin, 1);
554 i2c_gpio_set_pin(sda_pin, 1);
557 if (!i2c_gpio_get_pin(sda_pin) || !i2c_gpio_get_pin(scl_pin))
563 static int i2c_deblock_gpio(struct udevice *bus)
565 struct gpio_desc gpios[PIN_COUNT];
568 ret = gpio_request_list_by_name(bus, "gpios", gpios,
569 ARRAY_SIZE(gpios), GPIOD_IS_IN);
570 if (ret != ARRAY_SIZE(gpios)) {
571 debug("%s: I2C Node '%s' has no 'gpios' property %s\n",
572 __func__, dev_read_name(bus), bus->name);
574 gpio_free_list(bus, gpios, ret);
580 ret = pinctrl_select_state(bus, "gpio");
582 debug("%s: I2C Node '%s' has no 'gpio' pinctrl state. %s\n",
583 __func__, dev_read_name(bus), bus->name);
587 ret0 = i2c_deblock_gpio_loop(&gpios[PIN_SDA], &gpios[PIN_SCL], 9, 0, 5);
589 ret = pinctrl_select_state(bus, "default");
591 debug("%s: I2C Node '%s' has no 'default' pinctrl state. %s\n",
592 __func__, dev_read_name(bus), bus->name);
595 ret = !ret ? ret0 : ret;
598 gpio_free_list(bus, gpios, ARRAY_SIZE(gpios));
603 static int i2c_deblock_gpio(struct udevice *bus)
609 int i2c_deblock(struct udevice *bus)
611 struct dm_i2c_ops *ops = i2c_get_ops(bus);
614 return i2c_deblock_gpio(bus);
616 return ops->deblock(bus);
619 #if CONFIG_IS_ENABLED(OF_CONTROL) && !CONFIG_IS_ENABLED(OF_PLATDATA)
620 int i2c_chip_of_to_plat(struct udevice *dev, struct dm_i2c_chip *chip)
624 chip->offset_len = dev_read_u32_default(dev, "u-boot,i2c-offset-len",
627 addr = dev_read_u32_default(dev, "reg", -1);
629 debug("%s: I2C Node '%s' has no 'reg' property %s\n", __func__,
630 dev_read_name(dev), dev->name);
631 return log_ret(-EINVAL);
633 chip->chip_addr = addr;
639 static int i2c_pre_probe(struct udevice *dev)
641 #if CONFIG_IS_ENABLED(OF_CONTROL) && !CONFIG_IS_ENABLED(OF_PLATDATA)
642 struct dm_i2c_bus *i2c = dev_get_uclass_priv(dev);
643 unsigned int max = 0;
647 i2c->max_transaction_bytes = 0;
648 dev_for_each_subnode(node, dev) {
649 ret = ofnode_read_u32(node,
650 "u-boot,i2c-transaction-bytes",
652 if (!ret && max > i2c->max_transaction_bytes)
653 i2c->max_transaction_bytes = max;
656 debug("%s: I2C bus: %s max transaction bytes: %d\n", __func__,
657 dev->name, i2c->max_transaction_bytes);
662 static int i2c_post_probe(struct udevice *dev)
664 #if CONFIG_IS_ENABLED(OF_CONTROL) && !CONFIG_IS_ENABLED(OF_PLATDATA)
665 struct dm_i2c_bus *i2c = dev_get_uclass_priv(dev);
667 i2c->speed_hz = dev_read_u32_default(dev, "clock-frequency",
668 I2C_SPEED_STANDARD_RATE);
670 return dm_i2c_set_bus_speed(dev, i2c->speed_hz);
676 static int i2c_child_post_bind(struct udevice *dev)
678 #if CONFIG_IS_ENABLED(OF_CONTROL) && !CONFIG_IS_ENABLED(OF_PLATDATA)
679 struct dm_i2c_chip *plat = dev_get_parent_plat(dev);
681 if (!dev_of_valid(dev))
683 return i2c_chip_of_to_plat(dev, plat);
689 static int i2c_post_bind(struct udevice *dev)
693 debug("%s: %s, seq=%d\n", __func__, dev->name, dev_seq(dev));
695 #if CONFIG_IS_ENABLED(OF_CONTROL) && !CONFIG_IS_ENABLED(OF_PLATDATA)
696 ret = dm_scan_fdt_dev(dev);
701 UCLASS_DRIVER(i2c) = {
704 .flags = DM_UC_FLAG_SEQ_ALIAS,
705 .post_bind = i2c_post_bind,
706 .pre_probe = i2c_pre_probe,
707 .post_probe = i2c_post_probe,
708 .per_device_auto = sizeof(struct dm_i2c_bus),
709 .per_child_plat_auto = sizeof(struct dm_i2c_chip),
710 .child_post_bind = i2c_child_post_bind,
713 UCLASS_DRIVER(i2c_generic) = {
714 .id = UCLASS_I2C_GENERIC,
715 .name = "i2c_generic",
718 static const struct udevice_id generic_chip_i2c_ids[] = {
719 { .compatible = "i2c-chip", .data = I2C_DEVICE_GENERIC },
720 #if CONFIG_IS_ENABLED(ACPIGEN)
721 { .compatible = "hid-over-i2c", .data = I2C_DEVICE_HID_OVER_I2C },
726 U_BOOT_DRIVER(i2c_generic_chip_drv) = {
727 .name = "i2c_generic_chip_drv",
728 .id = UCLASS_I2C_GENERIC,
729 .of_match = generic_chip_i2c_ids,
730 #if CONFIG_IS_ENABLED(ACPIGEN)
731 .of_to_plat = acpi_i2c_of_to_plat,
732 .priv_auto = sizeof(struct acpi_i2c_priv),
734 ACPI_OPS_PTR(&acpi_i2c_ops)