1 // SPDX-License-Identifier: GPL-2.0+
3 * Copyright (c) 2014 Google, Inc
11 #include <dm/device-internal.h>
13 #include <dm/pinctrl.h>
14 #if CONFIG_IS_ENABLED(DM_GPIO)
18 #define I2C_MAX_OFFSET_LEN 4
26 /* Useful debugging function */
27 void i2c_dump_msgs(struct i2c_msg *msg, int nmsgs)
31 for (i = 0; i < nmsgs; i++) {
32 struct i2c_msg *m = &msg[i];
34 printf(" %s %x len=%x", m->flags & I2C_M_RD ? "R" : "W",
36 if (!(m->flags & I2C_M_RD))
37 printf(": %x", m->buf[0]);
43 * i2c_setup_offset() - Set up a new message with a chip offset
46 * @offset: Byte offset within chip
47 * @offset_buf: Place to put byte offset
48 * @msg: Message buffer
49 * @return 0 if OK, -EADDRNOTAVAIL if the offset length is 0. In that case the
50 * message is still set up but will not contain an offset.
52 static int i2c_setup_offset(struct dm_i2c_chip *chip, uint offset,
53 uint8_t offset_buf[], struct i2c_msg *msg)
55 int offset_len = chip->offset_len;
57 msg->addr = chip->chip_addr;
58 if (chip->chip_addr_offset_mask)
59 msg->addr |= (offset >> (8 * offset_len)) &
60 chip->chip_addr_offset_mask;
61 msg->flags = chip->flags & DM_I2C_CHIP_10BIT ? I2C_M_TEN : 0;
62 msg->len = chip->offset_len;
63 msg->buf = offset_buf;
65 return -EADDRNOTAVAIL;
66 assert(offset_len <= I2C_MAX_OFFSET_LEN);
69 *offset_buf++ = offset >> (8 * offset_len);
74 static int i2c_read_bytewise(struct udevice *dev, uint offset,
75 uint8_t *buffer, int len)
77 struct dm_i2c_chip *chip = dev_get_parent_platdata(dev);
78 struct udevice *bus = dev_get_parent(dev);
79 struct dm_i2c_ops *ops = i2c_get_ops(bus);
80 struct i2c_msg msg[2], *ptr;
81 uint8_t offset_buf[I2C_MAX_OFFSET_LEN];
85 for (i = 0; i < len; i++) {
86 if (i2c_setup_offset(chip, offset + i, offset_buf, msg))
89 ptr->addr = msg->addr;
90 ptr->flags = msg->flags | I2C_M_RD;
92 ptr->buf = &buffer[i];
95 ret = ops->xfer(bus, msg, ptr - msg);
103 static int i2c_write_bytewise(struct udevice *dev, uint offset,
104 const uint8_t *buffer, int len)
106 struct dm_i2c_chip *chip = dev_get_parent_platdata(dev);
107 struct udevice *bus = dev_get_parent(dev);
108 struct dm_i2c_ops *ops = i2c_get_ops(bus);
109 struct i2c_msg msg[1];
110 uint8_t buf[I2C_MAX_OFFSET_LEN + 1];
114 for (i = 0; i < len; i++) {
115 if (i2c_setup_offset(chip, offset + i, buf, msg))
117 buf[msg->len++] = buffer[i];
119 ret = ops->xfer(bus, msg, 1);
127 int dm_i2c_read(struct udevice *dev, uint offset, uint8_t *buffer, int len)
129 struct dm_i2c_chip *chip = dev_get_parent_platdata(dev);
130 struct udevice *bus = dev_get_parent(dev);
131 struct dm_i2c_ops *ops = i2c_get_ops(bus);
132 struct i2c_msg msg[2], *ptr;
133 uint8_t offset_buf[I2C_MAX_OFFSET_LEN];
138 if (chip->flags & DM_I2C_CHIP_RD_ADDRESS)
139 return i2c_read_bytewise(dev, offset, buffer, len);
141 if (!i2c_setup_offset(chip, offset, offset_buf, ptr))
145 ptr->addr = msg->addr;
146 ptr->flags = chip->flags & DM_I2C_CHIP_10BIT ? I2C_M_TEN : 0;
147 ptr->flags |= I2C_M_RD;
152 msg_count = ptr - msg;
154 return ops->xfer(bus, msg, msg_count);
157 int dm_i2c_write(struct udevice *dev, uint offset, const uint8_t *buffer,
160 struct dm_i2c_chip *chip = dev_get_parent_platdata(dev);
161 struct udevice *bus = dev_get_parent(dev);
162 struct dm_i2c_ops *ops = i2c_get_ops(bus);
163 struct i2c_msg msg[1];
168 if (chip->flags & DM_I2C_CHIP_WR_ADDRESS)
169 return i2c_write_bytewise(dev, offset, buffer, len);
171 * The simple approach would be to send two messages here: one to
172 * set the offset and one to write the bytes. However some drivers
173 * will not be expecting this, and some chips won't like how the
174 * driver presents this on the I2C bus.
176 * The API does not support separate offset and data. We could extend
177 * it with a flag indicating that there is data in the next message
178 * that needs to be processed in the same transaction. We could
179 * instead add an additional buffer to each message. For now, handle
180 * this in the uclass since it isn't clear what the impact on drivers
181 * would be with this extra complication. Unfortunately this means
182 * copying the message.
184 * Use the stack for small messages, malloc() for larger ones. We
185 * need to allow space for the offset (up to 4 bytes) and the message
189 uint8_t buf[I2C_MAX_OFFSET_LEN + len];
191 i2c_setup_offset(chip, offset, buf, msg);
193 memcpy(buf + chip->offset_len, buffer, len);
195 return ops->xfer(bus, msg, 1);
200 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);
244 * i2c_probe_chip() - probe for a chip on a bus
247 * @chip_addr: Chip address to probe
248 * @flags: Flags for the chip
249 * @return 0 if found, -ENOSYS if the driver is invalid, -EREMOTEIO if the chip
250 * does not respond to probe
252 static int i2c_probe_chip(struct udevice *bus, uint chip_addr,
253 enum dm_i2c_chip_flags chip_flags)
255 struct dm_i2c_ops *ops = i2c_get_ops(bus);
256 struct i2c_msg msg[1];
259 if (ops->probe_chip) {
260 ret = ops->probe_chip(bus, chip_addr, chip_flags);
261 if (!ret || ret != -ENOSYS)
268 /* Probe with a zero-length message */
269 msg->addr = chip_addr;
270 msg->flags = chip_flags & DM_I2C_CHIP_10BIT ? I2C_M_TEN : 0;
274 return ops->xfer(bus, msg, 1);
277 static int i2c_bind_driver(struct udevice *bus, uint chip_addr, uint offset_len,
278 struct udevice **devp)
280 struct dm_i2c_chip *chip;
285 snprintf(name, sizeof(name), "generic_%x", chip_addr);
289 ret = device_bind_driver(bus, "i2c_generic_chip_drv", str, &dev);
290 debug("%s: device_bind_driver: ret=%d\n", __func__, ret);
294 /* Tell the device what we know about it */
295 chip = dev_get_parent_platdata(dev);
296 chip->chip_addr = chip_addr;
297 chip->offset_len = offset_len;
298 ret = device_probe(dev);
299 debug("%s: device_probe: ret=%d\n", __func__, ret);
308 * If the device failed to probe, unbind it. There is nothing there
309 * on the bus so we don't want to leave it lying around
317 int i2c_get_chip(struct udevice *bus, uint chip_addr, uint offset_len,
318 struct udevice **devp)
322 debug("%s: Searching bus '%s' for address %02x: ", __func__,
323 bus->name, chip_addr);
324 for (device_find_first_child(bus, &dev); dev;
325 device_find_next_child(&dev)) {
326 struct dm_i2c_chip *chip = dev_get_parent_platdata(dev);
329 if (chip->chip_addr == (chip_addr &
330 ~chip->chip_addr_offset_mask)) {
331 ret = device_probe(dev);
332 debug("found, ret=%d\n", ret);
339 debug("not found\n");
340 return i2c_bind_driver(bus, chip_addr, offset_len, devp);
343 int i2c_get_chip_for_busnum(int busnum, int chip_addr, uint offset_len,
344 struct udevice **devp)
349 ret = uclass_get_device_by_seq(UCLASS_I2C, busnum, &bus);
351 debug("Cannot find I2C bus %d\n", busnum);
355 /* detect the presence of the chip on the bus */
356 ret = i2c_probe_chip(bus, chip_addr, 0);
357 debug("%s: bus='%s', address %02x, ret=%d\n", __func__, bus->name,
360 debug("Cannot detect I2C chip %02x on bus %d\n", chip_addr,
365 ret = i2c_get_chip(bus, chip_addr, offset_len, devp);
367 debug("Cannot find I2C chip %02x on bus %d\n", chip_addr,
375 int dm_i2c_probe(struct udevice *bus, uint chip_addr, uint chip_flags,
376 struct udevice **devp)
382 /* First probe that chip */
383 ret = i2c_probe_chip(bus, chip_addr, chip_flags);
384 debug("%s: bus='%s', address %02x, ret=%d\n", __func__, bus->name,
389 /* The chip was found, see if we have a driver, and probe it */
390 ret = i2c_get_chip(bus, chip_addr, 1, devp);
391 debug("%s: i2c_get_chip: ret=%d\n", __func__, ret);
396 int dm_i2c_set_bus_speed(struct udevice *bus, unsigned int speed)
398 struct dm_i2c_ops *ops = i2c_get_ops(bus);
399 struct dm_i2c_bus *i2c = dev_get_uclass_priv(bus);
403 * If we have a method, call it. If not then the driver probably wants
404 * to deal with speed changes on the next transfer. It can easily read
405 * the current speed from this uclass
407 if (ops->set_bus_speed) {
408 ret = ops->set_bus_speed(bus, speed);
412 i2c->speed_hz = speed;
417 int dm_i2c_get_bus_speed(struct udevice *bus)
419 struct dm_i2c_ops *ops = i2c_get_ops(bus);
420 struct dm_i2c_bus *i2c = dev_get_uclass_priv(bus);
422 if (!ops->get_bus_speed)
423 return i2c->speed_hz;
425 return ops->get_bus_speed(bus);
428 int i2c_set_chip_flags(struct udevice *dev, uint flags)
430 struct udevice *bus = dev->parent;
431 struct dm_i2c_chip *chip = dev_get_parent_platdata(dev);
432 struct dm_i2c_ops *ops = i2c_get_ops(bus);
435 if (ops->set_flags) {
436 ret = ops->set_flags(dev, flags);
445 int i2c_get_chip_flags(struct udevice *dev, uint *flagsp)
447 struct dm_i2c_chip *chip = dev_get_parent_platdata(dev);
449 *flagsp = chip->flags;
454 int i2c_set_chip_offset_len(struct udevice *dev, uint offset_len)
456 struct dm_i2c_chip *chip = dev_get_parent_platdata(dev);
458 if (offset_len > I2C_MAX_OFFSET_LEN)
460 chip->offset_len = offset_len;
465 int i2c_get_chip_offset_len(struct udevice *dev)
467 struct dm_i2c_chip *chip = dev_get_parent_platdata(dev);
469 return chip->offset_len;
472 int i2c_set_chip_addr_offset_mask(struct udevice *dev, uint mask)
474 struct dm_i2c_chip *chip = dev_get_parent_platdata(dev);
476 chip->chip_addr_offset_mask = mask;
481 uint i2c_get_chip_addr_offset_mask(struct udevice *dev)
483 struct dm_i2c_chip *chip = dev_get_parent_platdata(dev);
485 return chip->chip_addr_offset_mask;
488 #if CONFIG_IS_ENABLED(DM_GPIO)
489 static void i2c_gpio_set_pin(struct gpio_desc *pin, int bit)
492 dm_gpio_set_dir_flags(pin, GPIOD_IS_IN);
494 dm_gpio_set_dir_flags(pin, GPIOD_IS_OUT |
496 GPIOD_IS_OUT_ACTIVE);
499 static int i2c_gpio_get_pin(struct gpio_desc *pin)
501 return dm_gpio_get_value(pin);
504 static int i2c_deblock_gpio_loop(struct gpio_desc *sda_pin,
505 struct gpio_desc *scl_pin)
510 i2c_gpio_set_pin(sda_pin, 1);
511 i2c_gpio_set_pin(scl_pin, 1);
514 /* Toggle SCL until slave release SDA */
515 while (counter-- >= 0) {
516 i2c_gpio_set_pin(scl_pin, 1);
518 i2c_gpio_set_pin(scl_pin, 0);
520 if (i2c_gpio_get_pin(sda_pin))
524 /* Then, send I2C stop */
525 i2c_gpio_set_pin(sda_pin, 0);
528 i2c_gpio_set_pin(scl_pin, 1);
531 i2c_gpio_set_pin(sda_pin, 1);
534 if (!i2c_gpio_get_pin(sda_pin) || !i2c_gpio_get_pin(scl_pin))
540 static int i2c_deblock_gpio(struct udevice *bus)
542 struct gpio_desc gpios[PIN_COUNT];
545 ret = gpio_request_list_by_name(bus, "gpios", gpios,
546 ARRAY_SIZE(gpios), GPIOD_IS_IN);
547 if (ret != ARRAY_SIZE(gpios)) {
548 debug("%s: I2C Node '%s' has no 'gpios' property %s\n",
549 __func__, dev_read_name(bus), bus->name);
551 gpio_free_list(bus, gpios, ret);
557 ret = pinctrl_select_state(bus, "gpio");
559 debug("%s: I2C Node '%s' has no 'gpio' pinctrl state. %s\n",
560 __func__, dev_read_name(bus), bus->name);
564 ret0 = i2c_deblock_gpio_loop(&gpios[PIN_SDA], &gpios[PIN_SCL]);
566 ret = pinctrl_select_state(bus, "default");
568 debug("%s: I2C Node '%s' has no 'default' pinctrl state. %s\n",
569 __func__, dev_read_name(bus), bus->name);
572 ret = !ret ? ret0 : ret;
575 gpio_free_list(bus, gpios, ARRAY_SIZE(gpios));
580 static int i2c_deblock_gpio(struct udevice *bus)
586 int i2c_deblock(struct udevice *bus)
588 struct dm_i2c_ops *ops = i2c_get_ops(bus);
591 return i2c_deblock_gpio(bus);
593 return ops->deblock(bus);
596 #if CONFIG_IS_ENABLED(OF_CONTROL) && !CONFIG_IS_ENABLED(OF_PLATDATA)
597 int i2c_chip_ofdata_to_platdata(struct udevice *dev, struct dm_i2c_chip *chip)
601 chip->offset_len = dev_read_u32_default(dev, "u-boot,i2c-offset-len",
604 addr = dev_read_u32_default(dev, "reg", -1);
606 debug("%s: I2C Node '%s' has no 'reg' property %s\n", __func__,
607 dev_read_name(dev), dev->name);
610 chip->chip_addr = addr;
616 static int i2c_pre_probe(struct udevice *dev)
618 #if CONFIG_IS_ENABLED(OF_CONTROL) && !CONFIG_IS_ENABLED(OF_PLATDATA)
619 struct dm_i2c_bus *i2c = dev_get_uclass_priv(dev);
620 unsigned int max = 0;
624 i2c->max_transaction_bytes = 0;
625 dev_for_each_subnode(node, dev) {
626 ret = ofnode_read_u32(node,
627 "u-boot,i2c-transaction-bytes",
629 if (!ret && max > i2c->max_transaction_bytes)
630 i2c->max_transaction_bytes = max;
633 debug("%s: I2C bus: %s max transaction bytes: %d\n", __func__,
634 dev->name, i2c->max_transaction_bytes);
639 static int i2c_post_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);
644 i2c->speed_hz = dev_read_u32_default(dev, "clock-frequency", 100000);
646 return dm_i2c_set_bus_speed(dev, i2c->speed_hz);
652 static int i2c_child_post_bind(struct udevice *dev)
654 #if CONFIG_IS_ENABLED(OF_CONTROL) && !CONFIG_IS_ENABLED(OF_PLATDATA)
655 struct dm_i2c_chip *plat = dev_get_parent_platdata(dev);
657 if (!dev_of_valid(dev))
659 return i2c_chip_ofdata_to_platdata(dev, plat);
669 static int i2c_post_bind(struct udevice *dev)
671 struct uclass *class = dev->uclass;
672 struct i2c_priv *priv = class->priv;
679 debug("%s: %s, req_seq=%d\n", __func__, dev->name, dev->req_seq);
681 /* if there is no alias ID, use the first free */
682 if (dev->req_seq == -1)
683 dev->req_seq = ++priv->max_id;
685 debug("%s: %s, new req_seq=%d\n", __func__, dev->name, dev->req_seq);
687 #if CONFIG_IS_ENABLED(OF_CONTROL) && !CONFIG_IS_ENABLED(OF_PLATDATA)
688 ret = dm_scan_fdt_dev(dev);
693 int i2c_uclass_init(struct uclass *class)
695 struct i2c_priv *priv = class->priv;
701 /* Get the last allocated alias. */
702 #if CONFIG_IS_ENABLED(OF_CONTROL)
703 priv->max_id = dev_read_alias_highest_id("i2c");
708 debug("%s: highest alias id is %d\n", __func__, priv->max_id);
713 UCLASS_DRIVER(i2c) = {
716 .flags = DM_UC_FLAG_SEQ_ALIAS,
717 .post_bind = i2c_post_bind,
718 .init = i2c_uclass_init,
719 .priv_auto_alloc_size = sizeof(struct i2c_priv),
720 .pre_probe = i2c_pre_probe,
721 .post_probe = i2c_post_probe,
722 .per_device_auto_alloc_size = sizeof(struct dm_i2c_bus),
723 .per_child_platdata_auto_alloc_size = sizeof(struct dm_i2c_chip),
724 .child_post_bind = i2c_child_post_bind,
727 UCLASS_DRIVER(i2c_generic) = {
728 .id = UCLASS_I2C_GENERIC,
729 .name = "i2c_generic",
732 U_BOOT_DRIVER(i2c_generic_chip_drv) = {
733 .name = "i2c_generic_chip_drv",
734 .id = UCLASS_I2C_GENERIC,