[PATCH] dm: i2c-gpio: add support for clock stretching

Michael Auchter michael.auchter at ni.com
Thu Jan 23 23:30:20 CET 2020


This adds support for clock stretching to the i2c-gpio driver. This is
accomplished by switching the GPIO used for the SCL line to an input
when it should be driven high, and polling on the SCL line value until
it goes high (indicating that the I2C slave is no longer pulling it
low).

Signed-off-by: Michael Auchter <michael.auchter at ni.com>
Cc: Heiko Schocher <hs at denx.de>
---
 drivers/i2c/i2c-gpio.c | 15 +++++++++++----
 1 file changed, 11 insertions(+), 4 deletions(-)

diff --git a/drivers/i2c/i2c-gpio.c b/drivers/i2c/i2c-gpio.c
index 4e8fa21473..80ac26e583 100644
--- a/drivers/i2c/i2c-gpio.c
+++ b/drivers/i2c/i2c-gpio.c
@@ -49,11 +49,18 @@ static void i2c_gpio_sda_set(struct gpio_desc *sda, int bit)
 
 static void i2c_gpio_scl_set(struct gpio_desc *scl, int bit)
 {
-	ulong flags = GPIOD_IS_OUT;
+	int count = 0;
 
-	if (bit)
-		flags |= GPIOD_IS_OUT_ACTIVE;
-	dm_gpio_set_dir_flags(scl, flags);
+	if (bit) {
+		dm_gpio_set_dir_flags(scl, GPIOD_IS_IN);
+		while (!dm_gpio_get_value(scl) && count++ < 100000)
+			udelay(1);
+
+		if (!dm_gpio_get_value(scl))
+			pr_err("timeout waiting on slave to release scl\n");
+	} else {
+		dm_gpio_set_dir_flags(scl, GPIOD_IS_OUT);
+	}
 }
 
 static void i2c_gpio_write_bit(struct gpio_desc *scl, struct gpio_desc *sda,
-- 
2.24.1



More information about the U-Boot mailing list