Skip to contentSkip to conversation

i2c: gpio: Add custom deblock sequence

PATCH New Marek Vasut 2 participants 3 messages Jan 24, 2020 – Jan 27, 2020 I2C

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 i2c: gpio: Add custom deblock sequence

Add custom deblock dequence for the I2C bus, needed on some devices. This sequence is issued once, when probing the driver, and is controlled by DT property, "i2c-gpio,deblock".

---
 drivers/i2c/i2c-gpio.c | 67 ++++++++++++++++++++++++++++++++++++++++++
 1 file changed, 67 insertions(+)
drivers/i2c/i2c-gpio.c +67 -0
index 4e8fa21473..46975ef5c8 100644
@@ -39,6 +39,11 @@ static int i2c_gpio_sda_get(struct gpio_desc *sda)
 	return dm_gpio_get_value(sda);
 }
 
+static int i2c_gpio_scl_get(struct gpio_desc *scl)
+{
+	return dm_gpio_get_value(scl);
+}
+
 static void i2c_gpio_sda_set(struct gpio_desc *sda, int bit)
 {
 	if (bit)
@@ -305,6 +310,67 @@ static int i2c_gpio_set_bus_speed(struct udevice *dev, unsigned int speed_hz)
 	return 0;
 }
 
+/*
+ * I2C is a synchronous protocol and resets of the processor in the middle
+ * of an access can block the I2C Bus until a powerdown of the full unit is
+ * done. This function toggles the SCL until the SCL and SCA line are
+ * released, but max. 16 times, after this a I2C start-sequence is sent.
+ * This I2C Deblocking mechanism was developed by Keymile in association
+ * with Anatech and Atmel in 1998.
+ */
+static void i2c_gpio_make_abort(struct udevice *dev)
+{
+	struct i2c_gpio_bus *bus = dev_get_priv(dev);
+	struct gpio_desc *scl = &bus->gpios[PIN_SCL];
+	struct gpio_desc *sda = &bus->gpios[PIN_SDA];
+	int scl_state = 0;
+	int sda_state = 0;
+	int i = 0;
+	int ret = 0;
+
+#define DELAY_ABORT_SEQ	62	/* @200kHz 9 clocks = 44us, 62us is ok */
+
+	i2c_gpio_scl_set(scl, 1);
+
+	if (!i2c_gpio_sda_get(sda)) {
+		ret = -1;
+		for (i = 0; i < 16; i++) {
+			i2c_gpio_scl_set(scl, 0);
+			udelay(DELAY_ABORT_SEQ);
+			i2c_gpio_scl_set(scl, 1);
+			udelay(DELAY_ABORT_SEQ);
+			scl_state = i2c_gpio_scl_get(scl);
+			sda_state = i2c_gpio_sda_get(sda);
+			if (scl_state && sda_state) {
+				ret = 0;
+				break;
+			}
+		}
+	}
+
+	if (!ret) {
+		for (i = 0; i < 5; i++) {
+			i2c_gpio_send_start(scl, sda, 2 * bus->udelay);
+			i2c_gpio_scl_set(scl, 0);
+		}
+	}
+
+	/* respect stop setup time */
+	udelay(DELAY_ABORT_SEQ);
+	i2c_gpio_scl_set(scl, 1);
+	udelay(DELAY_ABORT_SEQ);
+	i2c_gpio_sda_set(sda, 1);
+	i2c_gpio_sda_get(sda);
+}
+
+static int i2c_gpio_drv_probe(struct udevice *dev)
+{
+	if (dev_read_bool(dev, "i2c-gpio,deblock"))
+		i2c_gpio_make_abort(dev);
+
+	return 0;
+}
+
 static int i2c_gpio_ofdata_to_platdata(struct udevice *dev)
 {
 	struct i2c_gpio_bus *bus = dev_get_priv(dev);
@@ -341,6 +407,7 @@ U_BOOT_DRIVER(i2c_gpio) = {
 	.name	= "i2c-gpio",
 	.id	= UCLASS_I2C,
 	.of_match = i2c_gpio_ids,
+	.probe	= i2c_gpio_drv_probe,
 	.ofdata_to_platdata = i2c_gpio_ofdata_to_platdata,
 	.priv_auto_alloc_size = sizeof(struct i2c_gpio_bus),
 	.ops	= &i2c_gpio_ops,
Signature · 1 line
2.24.1
#1 lore raw

PATCH 1/1 i2c: gpio: Add custom deblock sequence

Hello Marek,

Am 24.01.2020 um 19:12 schrieb Marek Vasut:

Quoted 95 lines

Add custom deblock dequence for the I2C bus, needed on some devices. This sequence is issued once, when probing the driver, and is controlled by DT property, "i2c-gpio,deblock".

---
  drivers/i2c/i2c-gpio.c | 67 ++++++++++++++++++++++++++++++++++++++++++
  1 file changed, 67 insertions(+)
drivers/i2c/i2c-gpio.c +66 -0
index 4e8fa21473..46975ef5c8 100644
@@ -39,6 +39,11 @@ static int i2c_gpio_sda_get(struct gpio_desc *sda)
  	return dm_gpio_get_value(sda);
  }
 
+static int i2c_gpio_scl_get(struct gpio_desc *scl)
+{
+	return dm_gpio_get_value(scl);
+}
+
  static void i2c_gpio_sda_set(struct gpio_desc *sda, int bit)
  {
  	if (bit)
@@ -305,6 +310,67 @@ static int i2c_gpio_set_bus_speed(struct udevice *dev, unsigned int speed_hz)
  	return 0;
  }
 
+/*
+ * I2C is a synchronous protocol and resets of the processor in the middle
+ * of an access can block the I2C Bus until a powerdown of the full unit is
+ * done. This function toggles the SCL until the SCL and SCA line are
+ * released, but max. 16 times, after this a I2C start-sequence is sent.
+ * This I2C Deblocking mechanism was developed by Keymile in association
+ * with Anatech and Atmel in 1998.
+ */
+static void i2c_gpio_make_abort(struct udevice *dev)
+{
+	struct i2c_gpio_bus *bus = dev_get_priv(dev);
+	struct gpio_desc *scl = &bus->gpios[PIN_SCL];
+	struct gpio_desc *sda = &bus->gpios[PIN_SDA];
+	int scl_state = 0;
+	int sda_state = 0;
+	int i = 0;
+	int ret = 0;
+
+#define DELAY_ABORT_SEQ	62	/* @200kHz 9 clocks = 44us, 62us is ok */
+
+	i2c_gpio_scl_set(scl, 1);
+
+	if (!i2c_gpio_sda_get(sda)) {
+		ret = -1;
+		for (i = 0; i < 16; i++) {
+			i2c_gpio_scl_set(scl, 0);
+			udelay(DELAY_ABORT_SEQ);
+			i2c_gpio_scl_set(scl, 1);
+			udelay(DELAY_ABORT_SEQ);
+			scl_state = i2c_gpio_scl_get(scl);
+			sda_state = i2c_gpio_sda_get(sda);
+			if (scl_state && sda_state) {
+				ret = 0;
+				break;
+			}
+		}
+	}
+
+	if (!ret) {
+		for (i = 0; i < 5; i++) {
+			i2c_gpio_send_start(scl, sda, 2 * bus->udelay);
+			i2c_gpio_scl_set(scl, 0);
+		}
+	}
+
+	/* respect stop setup time */
+	udelay(DELAY_ABORT_SEQ);
+	i2c_gpio_scl_set(scl, 1);
+	udelay(DELAY_ABORT_SEQ);
+	i2c_gpio_sda_set(sda, 1);
+	i2c_gpio_sda_get(sda);
+}
+
+static int i2c_gpio_drv_probe(struct udevice *dev)
+{
+	if (dev_read_bool(dev, "i2c-gpio,deblock"))
+		i2c_gpio_make_abort(dev);
+
+	return 0;
+}
+
  static int i2c_gpio_ofdata_to_platdata(struct udevice *dev)
  {
  	struct i2c_gpio_bus *bus = dev_get_priv(dev);

drivers/i2c/i2c-gpio.c U_BOOT_DRIVER(i2c_gpio) = { · line 413

Quoted 7 lines
drivers/i2c/i2c-gpio.c +1 -0
@@ -341,6 +407,7 @@ U_BOOT_DRIVER(i2c_gpio) = {
  	.name	= "i2c-gpio",
  	.id	= UCLASS_I2C,
  	.of_match = i2c_gpio_ids,
+	.probe	= i2c_gpio_drv_probe,
  	.ofdata_to_platdata = i2c_gpio_ofdata_to_platdata,
  	.priv_auto_alloc_size = sizeof(struct i2c_gpio_bus),
  	.ops	= &i2c_gpio_ops,

Hmm.. why do you add this into the probe function and do not add it to:

https://gitlab.denx.de/u-boot/u-boot/blob/master/include/i2c.h#L382

which is called from:
https://gitlab.denx.de/u-boot/u-boot/blob/master/drivers/i2c/i2c-uclass.c#L586

Ok, i2c_deblock() currently is only used from do_i2c_reset(), but may it is worth to adapt drivers/i2c-core/i2c-uclass.c to call the deblock sequence also from i2c_pre_probe()?

May dependend on your proposed DT property "i2c-gpio,deblock" ?

But please update also the documentation in

u-boot:/doc/device-tree-bindings/i2c/i2c-gpio.txt

or if we go the more generic way:

u-boot:/doc/device-tree-bindings/i2c/i2c.txt

Thanks!

BTW: May it makes sense to adapt:
https://gitlab.denx.de/u-boot/u-boot/blob/master/drivers/i2c/i2c-uclass.c#L504

too?

bye,
Heiko

Signature · 3 lines
DENX Software Engineering GmbH,      Managing Director: Wolfgang Denk
HRB 165235 Munich, Office: Kirchenstr.5, D-82194 Groebenzell, Germany
Phone: +49-8142-66989-52   Fax: +49-8142-66989-80   Email: hs at denx.de
#2 lore raw

PATCH 1/1 i2c: gpio: Add custom deblock sequence

Quoted 111 lines · Heiko Schocher

Hello Marek,

Am 24.01.2020 um 19:12 schrieb Marek Vasut:

Quoted 97 lines

Add custom deblock dequence for the I2C bus, needed on some devices. This sequence is issued once, when probing the driver, and is controlled by DT property, "i2c-gpio,deblock".

---
  drivers/i2c/i2c-gpio.c | 67 ++++++++++++++++++++++++++++++++++++++++++

  1 file changed, 67 insertions(+)
drivers/i2c/i2c-gpio.c +4 -0
index 4e8fa21473..46975ef5c8 100644
@@ -39,6 +39,11 @@ static int i2c_gpio_sda_get(struct gpio_desc *sda)
       return dm_gpio_get_value(sda);
   }
   +static int i2c_gpio_scl_get(struct gpio_desc *scl)
+{
+    return dm_gpio_get_value(scl);
+}
+
   static void i2c_gpio_sda_set(struct gpio_desc *sda, int bit)
   {
       if (bit)
@@ -305,6 +310,67 @@ static int i2c_gpio_set_bus_speed(struct udevice
 *dev, unsigned int speed_hz)
       return 0;
   }

  +/*
+ * I2C is a synchronous protocol and resets of the processor in the middle
+ * of an access can block the I2C Bus until a powerdown of the full unit is
+ * done. This function toggles the SCL until the SCL and SCA line are
+ * released, but max. 16 times, after this a I2C start-sequence is sent.
+ * This I2C Deblocking mechanism was developed by Keymile in association
+ * with Anatech and Atmel in 1998.
+ */

drivers/i2c/i2c-gpio.c +11 -0
+static void i2c_gpio_make_abort(struct udevice *dev)
+{
+    struct i2c_gpio_bus *bus = dev_get_priv(dev);
+    struct gpio_desc *scl = &bus->gpios[PIN_SCL];
+    struct gpio_desc *sda = &bus->gpios[PIN_SDA];
+    int scl_state = 0;
+    int sda_state = 0;
+    int i = 0;
+    int ret = 0;
+
+#define DELAY_ABORT_SEQ    62    /* @200kHz 9 clocks = 44us, 62us is

ok */

drivers/i2c/i2c-gpio.c +42 -0
+
+    i2c_gpio_scl_set(scl, 1);
+
+    if (!i2c_gpio_sda_get(sda)) {
+        ret = -1;
+        for (i = 0; i < 16; i++) {
+            i2c_gpio_scl_set(scl, 0);
+            udelay(DELAY_ABORT_SEQ);
+            i2c_gpio_scl_set(scl, 1);
+            udelay(DELAY_ABORT_SEQ);
+            scl_state = i2c_gpio_scl_get(scl);
+            sda_state = i2c_gpio_sda_get(sda);
+            if (scl_state && sda_state) {
+                ret = 0;
+                break;
+            }
+        }
+    }
+
+    if (!ret) {
+        for (i = 0; i < 5; i++) {
+            i2c_gpio_send_start(scl, sda, 2 * bus->udelay);
+            i2c_gpio_scl_set(scl, 0);
+        }
+    }
+
+    /* respect stop setup time */
+    udelay(DELAY_ABORT_SEQ);
+    i2c_gpio_scl_set(scl, 1);
+    udelay(DELAY_ABORT_SEQ);
+    i2c_gpio_sda_set(sda, 1);
+    i2c_gpio_sda_get(sda);
+}
+
+static int i2c_gpio_drv_probe(struct udevice *dev)
+{
+    if (dev_read_bool(dev, "i2c-gpio,deblock"))
+        i2c_gpio_make_abort(dev);
+
+    return 0;
+}
+

  static int i2c_gpio_ofdata_to_platdata(struct udevice *dev) {
      struct i2c_gpio_bus *bus = dev_get_priv(dev);

drivers/i2c/i2c-gpio.c U_BOOT_DRIVER(i2c_gpio) = { · line 413

Quoted 7 lines
drivers/i2c/i2c-gpio.c +1 -0
@@ -341,6 +407,7 @@ U_BOOT_DRIVER(i2c_gpio) = {
       .name    = "i2c-gpio",
       .id    = UCLASS_I2C,
       .of_match = i2c_gpio_ids,
+    .probe    = i2c_gpio_drv_probe,
       .ofdata_to_platdata = i2c_gpio_ofdata_to_platdata,
       .priv_auto_alloc_size = sizeof(struct i2c_gpio_bus),
       .ops    = &i2c_gpio_ops,

Hmm.. why do you add this into the probe function and do not add it to:

Quoted 1 line

https://gitlab.denx.de/u-boot/u-boot/blob/master/include/i2c.h#L382

Because I wasn't aware of this callback.

Quoted 15 lines

which is called from:
https://gitlab.denx.de/u-boot/u-boot/blob/master/drivers/i2c/i2c-uclass.c#L586

Ok, i2c_deblock() currently is only used from do_i2c_reset(), but may it is worth to adapt drivers/i2c-core/i2c-uclass.c to call the deblock sequence also from i2c_pre_probe()?

May dependend on your proposed DT property "i2c-gpio,deblock" ?

But please update also the documentation in

u-boot:/doc/device-tree-bindings/i2c/i2c-gpio.txt

Quoted 8 lines

or if we go the more generic way:

u-boot:/doc/device-tree-bindings/i2c/i2c.txt

Thanks!

BTW: May it makes sense to adapt:
https://gitlab.denx.de/u-boot/u-boot/blob/master/drivers/i2c/i2c-uclass.c#L504

I need to take a look at this one.

Signature · 2 lines
Best regards,
Marek Vasut