Skip to contentSkip to conversation

dm: i2c: implement gpio-based I2C deblock

PATCH New Alexander Kochetkov 2 participants 5 messages Mar 27, 2018 – Apr 11, 2018 I2C

lore
Share & tools
lore
#0 lore raw

v2 cover dm: i2c: implement gpio-based I2C deblock

Hello, Heiko!

Here is try2 of my previous patch[1].

Done in v2:
- fix checkpatch warnings
- ENOSYS warning still present, but it was in original code also and for me it looks like -ENOSYS must be returned in case of missed i2c_deblock_gpio() implementation.
WARNING: ENOSYS means 'invalid syscall nr' and nothing else #138: FILE: drivers/i2c/i2c-uclass.c:552:
+ return -ENOSYS;
- fix travis build, results can be viewed here:
https://travis-ci.org/akochetkov/u-boot/builds/358822512
- add property description to i2c.txt

I have question. I declared PIN_SDA, PIN_SCL, PIN_COUNT constants for i2c-uclass.c. The same constants exist in the i2c-gpio.c. Should I place them into i2c.h or may leave as is?

[1] https://lists.denx.de/pipermail/u-boot/2018-March/thread.html#321755

Alexander Kochetkov (2):
dm: i2c: dts: Add gpios and pinctrl device tree properties
dm: i2c: implement gpio-based I2C deblock

 doc/device-tree-bindings/i2c/i2c.txt |   13 ++++
 drivers/i2c/i2c-uclass.c             |  118 +++++++++++++++++++++++++++++++---
 2 files changed, 122 insertions(+), 9 deletions(-)
Signature · 1 line
1.7.9.5
#1 lore raw

PATCH v2 1/2 dm: i2c: dts: Add gpios and pinctrl device tree properties

The commit describe usage of gpios and pinctrl device tree properties in order to enable gpio-based software deblocking.

---
 doc/device-tree-bindings/i2c/i2c.txt |   13 +++++++++++++
 1 file changed, 13 insertions(+)
doc/device-tree-bindings/i2c/i2c.txt +13 -0
index ea918dd..de818d4 100644
@@ -12,6 +12,11 @@ property which allows the chip offset length to be selected.
 Optional properties:
 - u-boot,i2c-offset-len - length of chip offset in bytes. If omitted the
     default value of 1 is used.
+- gpios = <sda ...>, <scl ...>;
+  pinctrl-names = "default", "gpio";
+  pinctrl-0 = <&i2c_xfer>;
+  pinctrl-1 = <&i2c_gpio>;
+    Pin description for I2C bus software deblocking.
 
 
 Example
@@ -26,3 +31,11 @@ i2c4: i2c at 12ca0000 {
 		ec-interrupt = <&gpx1 6 GPIO_ACTIVE_LOW>;
 	};
 };
+
+&i2c1 {
+	pinctrl-names = "default", "gpio";
+	pinctrl-0 = <&i2c1_xfer>;
+	pinctrl-1 = <&i2c1_gpio>;
+	gpios = <&gpio1 26 GPIO_ACTIVE_LOW>, /* SDA */
+		<&gpio1 27 GPIO_ACTIVE_LOW>; /* SCL */
+};
Signature · 1 line
1.7.9.5
#3 lore raw

PATCH v2 2/2 dm: i2c: implement gpio-based I2C deblock

In reply to Alexander Kochetkov

The commit implement a gpio-based software deblocking. The code extract I2C pins description from device tree, switch pins to GPIO mode, toggle SCL until slave release SDA, send I2C stop and switch I2C pins back to I2C mode.

---
 drivers/i2c/i2c-uclass.c |  118 ++++++++++++++++++++++++++++++++++++++++++----
 1 file changed, 109 insertions(+), 9 deletions(-)
drivers/i2c/i2c-uclass.c +109 -9
index 920811a..4ac6ef8 100644
@@ -11,9 +11,19 @@
 #include <malloc.h>
 #include <dm/device-internal.h>
 #include <dm/lists.h>
+#include <dm/pinctrl.h>
+#ifdef CONFIG_DM_GPIO
+#include <asm/gpio.h>
+#endif
 
 #define I2C_MAX_OFFSET_LEN	4
 
+enum {
+	PIN_SDA = 0,
+	PIN_SCL,
+	PIN_COUNT,
+};
+
 /* Useful debugging function */
 void i2c_dump_msgs(struct i2c_msg *msg, int nmsgs)
 {
@@ -445,20 +455,110 @@ int i2c_get_chip_offset_len(struct udevice *dev)
 	return chip->offset_len;
 }
 
+#ifdef CONFIG_DM_GPIO
+static void i2c_gpio_set_pin(struct gpio_desc *pin, int bit)
+{
+	if (bit)
+		dm_gpio_set_dir_flags(pin, GPIOD_IS_IN);
+	else
+		dm_gpio_set_dir_flags(pin, GPIOD_IS_OUT |
+					   GPIOD_ACTIVE_LOW |
+					   GPIOD_IS_OUT_ACTIVE);
+}
+
+static int i2c_gpio_get_pin(struct gpio_desc *pin)
+{
+	return dm_gpio_get_value(pin);
+}
+
+static int i2c_deblock_gpio_loop(struct gpio_desc *sda_pin,
+				 struct gpio_desc *scl_pin)
+{
+	int counter = 9;
+	int ret = 0;
+
+	i2c_gpio_set_pin(sda_pin, 1);
+	i2c_gpio_set_pin(scl_pin, 1);
+	udelay(5);
+
+	/*  Toggle SCL until slave release SDA */
+	while (counter-- >= 0) {
+		i2c_gpio_set_pin(scl_pin, 1);
+		udelay(5);
+		i2c_gpio_set_pin(scl_pin, 0);
+		udelay(5);
+		if (i2c_gpio_get_pin(sda_pin))
+			break;
+	}
+
+	/* Then, send I2C stop */
+	i2c_gpio_set_pin(sda_pin, 0);
+	udelay(5);
+
+	i2c_gpio_set_pin(scl_pin, 1);
+	udelay(5);
+
+	i2c_gpio_set_pin(sda_pin, 1);
+	udelay(5);
+
+	if (!i2c_gpio_get_pin(sda_pin) || !i2c_gpio_get_pin(scl_pin))
+		ret = -EREMOTEIO;
+
+	return ret;
+}
+
+static int i2c_deblock_gpio(struct udevice *bus)
+{
+	struct gpio_desc gpios[PIN_COUNT];
+	int ret, ret0;
+
+	ret = gpio_request_list_by_name(bus, "gpios", gpios,
+					ARRAY_SIZE(gpios), GPIOD_IS_IN);
+	if (ret != ARRAY_SIZE(gpios)) {
+		debug("%s: I2C Node '%s' has no 'gpios' property %s\n",
+		      __func__, dev_read_name(bus), bus->name);
+		if (ret >= 0) {
+			gpio_free_list(bus, gpios, ret);
+			ret = -ENOENT;
+		}
+		goto out;
+	}
+
+	ret = pinctrl_select_state(bus, "gpio");
+	if (ret) {
+		debug("%s: I2C Node '%s' has no 'gpio' pinctrl state. %s\n",
+		      __func__, dev_read_name(bus), bus->name);
+		goto out_no_pinctrl;
+	}
+
+	ret0 = i2c_deblock_gpio_loop(&gpios[PIN_SDA], &gpios[PIN_SCL]);
+
+	ret = pinctrl_select_state(bus, "default");
+	if (ret) {
+		debug("%s: I2C Node '%s' has no 'default' pinctrl state. %s\n",
+		      __func__, dev_read_name(bus), bus->name);
+	}
+
+	ret = !ret ? ret0 : ret;
+
+out_no_pinctrl:
+	gpio_free_list(bus, gpios, ARRAY_SIZE(gpios));
+out:
+	return ret;
+}
+#else
+static int i2c_deblock_gpio(struct udevice *bus)
+{
+	return -ENOSYS;
+}
+#endif // CONFIG_DM_GPIO
+
 int i2c_deblock(struct udevice *bus)
 {
 	struct dm_i2c_ops *ops = i2c_get_ops(bus);
 
-	/*
-	 * We could implement a software deblocking here if we could get
-	 * access to the GPIOs used by I2C, and switch them to GPIO mode
-	 * and then back to I2C. This is somewhat beyond our powers in
-	 * driver model at present, so for now just fail.
-	 *
-	 * See https://patchwork.ozlabs.org/patch/399040/
-	 */
 	if (!ops->deblock)
-		return -ENOSYS;
+		return i2c_deblock_gpio(bus);
 
 	return ops->deblock(bus);
 }
Signature · 1 line
1.7.9.5
#2 lore raw

PATCH v2 1/2 dm: i2c: dts: Add gpios and pinctrl device tree properties

In reply to Alexander Kochetkov

Hello Alexander,

Am 27.03.2018 um 16:52 schrieb Alexander Kochetkov:

Quoted 7 lines

The commit describe usage of gpios and pinctrl device tree properties in order to enable gpio-based software deblocking.

---
  doc/device-tree-bindings/i2c/i2c.txt |   13 +++++++++++++
  1 file changed, 13 insertions(+)

Applied to u-boot-i2c master

Thanks!

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
#4 lore raw

PATCH v2 2/2 dm: i2c: implement gpio-based I2C deblock

In reply to Alexander Kochetkov

Hello Alexander,

Am 27.03.2018 um 16:52 schrieb Alexander Kochetkov:

Quoted 9 lines

The commit implement a gpio-based software deblocking. The code extract I2C pins description from device tree, switch pins to GPIO mode, toggle SCL until slave release SDA, send I2C stop and switch I2C pins back to I2C mode.

---
  drivers/i2c/i2c-uclass.c |  118 ++++++++++++++++++++++++++++++++++++++++++----
  1 file changed, 109 insertions(+), 9 deletions(-)

Applied to u-boot-i2c master

Thanks!

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