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)
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)
56 int offset_len = chip->offset_len;
58 msg->addr = chip->chip_addr;
59 if (chip->chip_addr_offset_mask)
60 msg->addr |= (offset >> (8 * offset_len)) &
61 chip->chip_addr_offset_mask;
62 msg->flags = chip->flags & DM_I2C_CHIP_10BIT ? I2C_M_TEN : 0;
63 msg->len = chip->offset_len;
64 msg->buf = offset_buf;
66 return -EADDRNOTAVAIL;
67 assert(offset_len <= I2C_MAX_OFFSET_LEN);
70 *offset_buf++ = offset >> (8 * offset_len);
75 static int i2c_read_bytewise(struct udevice *dev, uint offset,
76 uint8_t *buffer, int len)
78 struct dm_i2c_chip *chip = dev_get_parent_platdata(dev);
79 struct udevice *bus = dev_get_parent(dev);
80 struct dm_i2c_ops *ops = i2c_get_ops(bus);
81 struct i2c_msg msg[2], *ptr;
82 uint8_t offset_buf[I2C_MAX_OFFSET_LEN];
86 for (i = 0; i < len; i++) {
87 if (i2c_setup_offset(chip, offset + i, offset_buf, msg))
90 ptr->addr = msg->addr;
91 ptr->flags = msg->flags | I2C_M_RD;
93 ptr->buf = &buffer[i];
96 ret = ops->xfer(bus, msg, ptr - msg);
104 static int i2c_write_bytewise(struct udevice *dev, uint offset,
105 const uint8_t *buffer, int len)
107 struct dm_i2c_chip *chip = dev_get_parent_platdata(dev);
108 struct udevice *bus = dev_get_parent(dev);
109 struct dm_i2c_ops *ops = i2c_get_ops(bus);
110 struct i2c_msg msg[1];
111 uint8_t buf[I2C_MAX_OFFSET_LEN + 1];
115 for (i = 0; i < len; i++) {
116 if (i2c_setup_offset(chip, offset + i, buf, msg))
118 buf[msg->len++] = buffer[i];
120 ret = ops->xfer(bus, msg, 1);
128 int dm_i2c_read(struct udevice *dev, uint offset, uint8_t *buffer, int len)
130 struct dm_i2c_chip *chip = dev_get_parent_platdata(dev);
131 struct udevice *bus = dev_get_parent(dev);
132 struct dm_i2c_ops *ops = i2c_get_ops(bus);
133 struct i2c_msg msg[2], *ptr;
134 uint8_t offset_buf[I2C_MAX_OFFSET_LEN];
139 if (chip->flags & DM_I2C_CHIP_RD_ADDRESS)
140 return i2c_read_bytewise(dev, offset, buffer, len);
142 if (!i2c_setup_offset(chip, offset, offset_buf, ptr))
146 ptr->addr = msg->addr;
147 ptr->flags = chip->flags & DM_I2C_CHIP_10BIT ? I2C_M_TEN : 0;
148 ptr->flags |= I2C_M_RD;
153 msg_count = ptr - msg;
155 return ops->xfer(bus, msg, msg_count);
158 int dm_i2c_write(struct udevice *dev, uint offset, const uint8_t *buffer,
161 struct dm_i2c_chip *chip = dev_get_parent_platdata(dev);
162 struct udevice *bus = dev_get_parent(dev);
163 struct dm_i2c_ops *ops = i2c_get_ops(bus);
164 struct i2c_msg msg[1];
169 if (chip->flags & DM_I2C_CHIP_WR_ADDRESS)
170 return i2c_write_bytewise(dev, offset, buffer, len);
172 * The simple approach would be to send two messages here: one to
173 * set the offset and one to write the bytes. However some drivers
174 * will not be expecting this, and some chips won't like how the
175 * driver presents this on the I2C bus.
177 * The API does not support separate offset and data. We could extend
178 * it with a flag indicating that there is data in the next message
179 * that needs to be processed in the same transaction. We could
180 * instead add an additional buffer to each message. For now, handle
181 * this in the uclass since it isn't clear what the impact on drivers
182 * would be with this extra complication. Unfortunately this means
183 * copying the message.
185 * Use the stack for small messages, malloc() for larger ones. We
186 * need to allow space for the offset (up to 4 bytes) and the message
190 uint8_t buf[I2C_MAX_OFFSET_LEN + len];
192 i2c_setup_offset(chip, offset, buf, msg);
194 memcpy(buf + chip->offset_len, buffer, len);
196 return ops->xfer(bus, msg, 1);
201 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);
245 * i2c_probe_chip() - probe for a chip on a bus
248 * @chip_addr: Chip address to probe
249 * @flags: Flags for the chip
250 * @return 0 if found, -ENOSYS if the driver is invalid, -EREMOTEIO if the chip
251 * does not respond to probe
253 static int i2c_probe_chip(struct udevice *bus, uint chip_addr,
254 enum dm_i2c_chip_flags chip_flags)
256 struct dm_i2c_ops *ops = i2c_get_ops(bus);
257 struct i2c_msg msg[1];
260 if (ops->probe_chip) {
261 ret = ops->probe_chip(bus, chip_addr, chip_flags);
262 if (!ret || ret != -ENOSYS)
269 /* Probe with a zero-length message */
270 msg->addr = chip_addr;
271 msg->flags = chip_flags & DM_I2C_CHIP_10BIT ? I2C_M_TEN : 0;
275 return ops->xfer(bus, msg, 1);
278 static int i2c_bind_driver(struct udevice *bus, uint chip_addr, uint offset_len,
279 struct udevice **devp)
281 struct dm_i2c_chip *chip;
286 snprintf(name, sizeof(name), "generic_%x", chip_addr);
290 ret = device_bind_driver(bus, "i2c_generic_chip_drv", str, &dev);
291 debug("%s: device_bind_driver: ret=%d\n", __func__, ret);
295 /* Tell the device what we know about it */
296 chip = dev_get_parent_platdata(dev);
297 chip->chip_addr = chip_addr;
298 chip->offset_len = offset_len;
299 ret = device_probe(dev);
300 debug("%s: device_probe: ret=%d\n", __func__, ret);
309 * If the device failed to probe, unbind it. There is nothing there
310 * on the bus so we don't want to leave it lying around
318 int i2c_get_chip(struct udevice *bus, uint chip_addr, uint offset_len,
319 struct udevice **devp)
323 debug("%s: Searching bus '%s' for address %02x: ", __func__,
324 bus->name, chip_addr);
325 for (device_find_first_child(bus, &dev); dev;
326 device_find_next_child(&dev)) {
327 struct dm_i2c_chip *chip = dev_get_parent_platdata(dev);
330 if (chip->chip_addr == (chip_addr &
331 ~chip->chip_addr_offset_mask)) {
332 ret = device_probe(dev);
333 debug("found, ret=%d\n", ret);
340 debug("not found\n");
341 return i2c_bind_driver(bus, chip_addr, offset_len, devp);
344 int i2c_get_chip_for_busnum(int busnum, int chip_addr, uint offset_len,
345 struct udevice **devp)
350 ret = uclass_get_device_by_seq(UCLASS_I2C, busnum, &bus);
352 debug("Cannot find I2C bus %d\n", busnum);
356 /* detect the presence of the chip on the bus */
357 ret = i2c_probe_chip(bus, chip_addr, 0);
358 debug("%s: bus='%s', address %02x, ret=%d\n", __func__, bus->name,
361 debug("Cannot detect I2C chip %02x on bus %d\n", chip_addr,
366 ret = i2c_get_chip(bus, chip_addr, offset_len, devp);
368 debug("Cannot find I2C chip %02x on bus %d\n", chip_addr,
376 int dm_i2c_probe(struct udevice *bus, uint chip_addr, uint chip_flags,
377 struct udevice **devp)
383 /* First probe that chip */
384 ret = i2c_probe_chip(bus, chip_addr, chip_flags);
385 debug("%s: bus='%s', address %02x, ret=%d\n", __func__, bus->name,
390 /* The chip was found, see if we have a driver, and probe it */
391 ret = i2c_get_chip(bus, chip_addr, 1, devp);
392 debug("%s: i2c_get_chip: ret=%d\n", __func__, ret);
397 int dm_i2c_set_bus_speed(struct udevice *bus, unsigned int speed)
399 struct dm_i2c_ops *ops = i2c_get_ops(bus);
400 struct dm_i2c_bus *i2c = dev_get_uclass_priv(bus);
404 * If we have a method, call it. If not then the driver probably wants
405 * to deal with speed changes on the next transfer. It can easily read
406 * the current speed from this uclass
408 if (ops->set_bus_speed) {
409 ret = ops->set_bus_speed(bus, speed);
413 i2c->speed_hz = speed;
418 int dm_i2c_get_bus_speed(struct udevice *bus)
420 struct dm_i2c_ops *ops = i2c_get_ops(bus);
421 struct dm_i2c_bus *i2c = dev_get_uclass_priv(bus);
423 if (!ops->get_bus_speed)
424 return i2c->speed_hz;
426 return ops->get_bus_speed(bus);
429 int i2c_set_chip_flags(struct udevice *dev, uint flags)
431 struct udevice *bus = dev->parent;
432 struct dm_i2c_chip *chip = dev_get_parent_platdata(dev);
433 struct dm_i2c_ops *ops = i2c_get_ops(bus);
436 if (ops->set_flags) {
437 ret = ops->set_flags(dev, flags);
446 int i2c_get_chip_flags(struct udevice *dev, uint *flagsp)
448 struct dm_i2c_chip *chip = dev_get_parent_platdata(dev);
450 *flagsp = chip->flags;
455 int i2c_set_chip_offset_len(struct udevice *dev, uint offset_len)
457 struct dm_i2c_chip *chip = dev_get_parent_platdata(dev);
459 if (offset_len > I2C_MAX_OFFSET_LEN)
461 chip->offset_len = offset_len;
466 int i2c_get_chip_offset_len(struct udevice *dev)
468 struct dm_i2c_chip *chip = dev_get_parent_platdata(dev);
470 return chip->offset_len;
473 int i2c_set_chip_addr_offset_mask(struct udevice *dev, uint mask)
475 struct dm_i2c_chip *chip = dev_get_parent_platdata(dev);
477 chip->chip_addr_offset_mask = mask;
482 uint i2c_get_chip_addr_offset_mask(struct udevice *dev)
484 struct dm_i2c_chip *chip = dev_get_parent_platdata(dev);
486 return chip->chip_addr_offset_mask;
489 #if CONFIG_IS_ENABLED(DM_GPIO)
490 static void i2c_gpio_set_pin(struct gpio_desc *pin, int bit)
493 dm_gpio_set_dir_flags(pin, GPIOD_IS_IN);
495 dm_gpio_set_dir_flags(pin, GPIOD_IS_OUT |
497 GPIOD_IS_OUT_ACTIVE);
500 static int i2c_gpio_get_pin(struct gpio_desc *pin)
502 return dm_gpio_get_value(pin);
505 int i2c_deblock_gpio_loop(struct gpio_desc *sda_pin,
506 struct gpio_desc *scl_pin,
507 unsigned int scl_count,
508 unsigned int start_count,
511 int i, ret = -EREMOTEIO;
513 i2c_gpio_set_pin(sda_pin, 1);
514 i2c_gpio_set_pin(scl_pin, 1);
517 /* Toggle SCL until slave release SDA */
518 while (scl_count-- >= 0) {
519 i2c_gpio_set_pin(scl_pin, 1);
521 i2c_gpio_set_pin(scl_pin, 0);
523 if (i2c_gpio_get_pin(sda_pin)) {
529 if (!ret && start_count) {
530 for (i = 0; i < start_count; i++) {
531 /* Send start condition */
533 i2c_gpio_set_pin(sda_pin, 1);
535 i2c_gpio_set_pin(scl_pin, 1);
537 i2c_gpio_set_pin(sda_pin, 0);
539 i2c_gpio_set_pin(scl_pin, 0);
543 /* Then, send I2C stop */
544 i2c_gpio_set_pin(sda_pin, 0);
547 i2c_gpio_set_pin(scl_pin, 1);
550 i2c_gpio_set_pin(sda_pin, 1);
553 if (!i2c_gpio_get_pin(sda_pin) || !i2c_gpio_get_pin(scl_pin))
559 static int i2c_deblock_gpio(struct udevice *bus)
561 struct gpio_desc gpios[PIN_COUNT];
564 ret = gpio_request_list_by_name(bus, "gpios", gpios,
565 ARRAY_SIZE(gpios), GPIOD_IS_IN);
566 if (ret != ARRAY_SIZE(gpios)) {
567 debug("%s: I2C Node '%s' has no 'gpios' property %s\n",
568 __func__, dev_read_name(bus), bus->name);
570 gpio_free_list(bus, gpios, ret);
576 ret = pinctrl_select_state(bus, "gpio");
578 debug("%s: I2C Node '%s' has no 'gpio' pinctrl state. %s\n",
579 __func__, dev_read_name(bus), bus->name);
583 ret0 = i2c_deblock_gpio_loop(&gpios[PIN_SDA], &gpios[PIN_SCL], 9, 0, 5);
585 ret = pinctrl_select_state(bus, "default");
587 debug("%s: I2C Node '%s' has no 'default' pinctrl state. %s\n",
588 __func__, dev_read_name(bus), bus->name);
591 ret = !ret ? ret0 : ret;
594 gpio_free_list(bus, gpios, ARRAY_SIZE(gpios));
599 static int i2c_deblock_gpio(struct udevice *bus)
605 int i2c_deblock(struct udevice *bus)
607 struct dm_i2c_ops *ops = i2c_get_ops(bus);
610 return i2c_deblock_gpio(bus);
612 return ops->deblock(bus);
615 #if CONFIG_IS_ENABLED(OF_CONTROL) && !CONFIG_IS_ENABLED(OF_PLATDATA)
616 int i2c_chip_ofdata_to_platdata(struct udevice *dev, struct dm_i2c_chip *chip)
620 chip->offset_len = dev_read_u32_default(dev, "u-boot,i2c-offset-len",
623 addr = dev_read_u32_default(dev, "reg", -1);
625 debug("%s: I2C Node '%s' has no 'reg' property %s\n", __func__,
626 dev_read_name(dev), dev->name);
629 chip->chip_addr = addr;
635 static int i2c_pre_probe(struct udevice *dev)
637 #if CONFIG_IS_ENABLED(OF_CONTROL) && !CONFIG_IS_ENABLED(OF_PLATDATA)
638 struct dm_i2c_bus *i2c = dev_get_uclass_priv(dev);
639 unsigned int max = 0;
643 i2c->max_transaction_bytes = 0;
644 dev_for_each_subnode(node, dev) {
645 ret = ofnode_read_u32(node,
646 "u-boot,i2c-transaction-bytes",
648 if (!ret && max > i2c->max_transaction_bytes)
649 i2c->max_transaction_bytes = max;
652 debug("%s: I2C bus: %s max transaction bytes: %d\n", __func__,
653 dev->name, i2c->max_transaction_bytes);
658 static int i2c_post_probe(struct udevice *dev)
660 #if CONFIG_IS_ENABLED(OF_CONTROL) && !CONFIG_IS_ENABLED(OF_PLATDATA)
661 struct dm_i2c_bus *i2c = dev_get_uclass_priv(dev);
663 i2c->speed_hz = dev_read_u32_default(dev, "clock-frequency",
664 I2C_SPEED_STANDARD_RATE);
666 return dm_i2c_set_bus_speed(dev, i2c->speed_hz);
672 static int i2c_child_post_bind(struct udevice *dev)
674 #if CONFIG_IS_ENABLED(OF_CONTROL) && !CONFIG_IS_ENABLED(OF_PLATDATA)
675 struct dm_i2c_chip *plat = dev_get_parent_platdata(dev);
677 if (!dev_of_valid(dev))
679 return i2c_chip_ofdata_to_platdata(dev, plat);
689 static int i2c_post_bind(struct udevice *dev)
691 struct uclass *class = dev->uclass;
692 struct i2c_priv *priv = class->priv;
699 debug("%s: %s, req_seq=%d\n", __func__, dev->name, dev->req_seq);
701 /* if there is no alias ID, use the first free */
702 if (dev->req_seq == -1)
703 dev->req_seq = ++priv->max_id;
705 debug("%s: %s, new req_seq=%d\n", __func__, dev->name, dev->req_seq);
707 #if CONFIG_IS_ENABLED(OF_CONTROL) && !CONFIG_IS_ENABLED(OF_PLATDATA)
708 ret = dm_scan_fdt_dev(dev);
713 int i2c_uclass_init(struct uclass *class)
715 struct i2c_priv *priv = class->priv;
721 /* Get the last allocated alias. */
722 if (CONFIG_IS_ENABLED(OF_CONTROL) && !CONFIG_IS_ENABLED(OF_PLATDATA))
723 priv->max_id = dev_read_alias_highest_id("i2c");
727 debug("%s: highest alias id is %d\n", __func__, priv->max_id);
732 UCLASS_DRIVER(i2c) = {
735 .flags = DM_UC_FLAG_SEQ_ALIAS,
736 .post_bind = i2c_post_bind,
737 .init = i2c_uclass_init,
738 .priv_auto_alloc_size = sizeof(struct i2c_priv),
739 .pre_probe = i2c_pre_probe,
740 .post_probe = i2c_post_probe,
741 .per_device_auto_alloc_size = sizeof(struct dm_i2c_bus),
742 .per_child_platdata_auto_alloc_size = sizeof(struct dm_i2c_chip),
743 .child_post_bind = i2c_child_post_bind,
746 UCLASS_DRIVER(i2c_generic) = {
747 .id = UCLASS_I2C_GENERIC,
748 .name = "i2c_generic",
751 U_BOOT_DRIVER(i2c_generic_chip_drv) = {
752 .name = "i2c_generic_chip_drv",
753 .id = UCLASS_I2C_GENERIC,