Skip to contentSkip to conversation

i2c: imx: implement bus recovery

PATCH Superseded Gao Pan 4 participants 12 messages Jul 17, 2015 – Sep 9, 2015 9 messages not in the archive

Subsystems (4)

v2 of 4 Superseded Latest is v4 →

lore
Share & tools
lore

Gao Pan

author
#0 lore raw

PATCH v2 1/1 i2c: imx: implement bus recovery

Implement bus recovery methods for i2c-imx so we can recover from situations where SCL/SDA are stuck low.

Once i2c bus SCL/SDA are stuck low during transfer, config the i2c pinctrl to gpio mode by calling pinctrl sleep set function, and then use GPIO to emulate the i2c protocol to send nine dummy clock to recover i2c device. After recovery, set i2c pinctrl to default group setting.

V2:
As Uwe Kleine-König's suggestion, the version do below changes:
-replace of_get_named_gpio() with devm_gpiod_get_optional() -move gpio_direction_input() and gpio_direction_output() call to the
prepare callback
-use 0 and 1 as return value for the get_scl and get_sda callbacks

---
 Documentation/devicetree/bindings/i2c/i2c-imx.txt |   4 +
 drivers/i2c/busses/i2c-imx.c                      | 104 ++++++++++++++++++++--
 2 files changed, 100 insertions(+), 8 deletions(-)
Documentation/devicetree/bindings/i2c/i2c-imx.txt +4 -0
index ce4311d..a9fe525 100644
@@ -14,6 +14,8 @@ Optional properties:
   The absence of the propoerty indicates the default frequency 100 kHz.
 - dmas: A list of two dma specifiers, one for each entry in dma-names.
 - dma-names: should contain "tx" and "rx".
+- scl-gpio: specify the gpio related to SCL pin
+- sda-gpio: specify the gpio related to SDA pin
 
 Examples:
 
@@ -37,4 +39,6 @@ i2c0: i2c@40066000 { /* i2c0 on vf610 */
 	dmas = <&edma0 0 50>,
 		<&edma0 0 51>;
 	dma-names = "rx","tx";
+	scl-gpio = <&gpio5 26 0>;
+	sda-gpio = <&gpio5 27 0>;
 };
drivers/i2c/busses/i2c-imx.c +96 -8

158 lines not on this page · raw message

Signature · 1 line
1.9.1
#2 lore raw
In reply to a message not in the archive

Ping...

Quoted 220 lines

-----Original Message-----
From: Gao Pan [mailto:[email protected]]
Sent: Friday, July 17, 2015 10:37 AM
To: [email protected]
Cc: [email protected]; Li Frank-B20596; Duan Fugang-B38611; Gao Pan-B54642; [email protected]
Subject: [Patch v2] i2c: imx: implement bus recovery

Implement bus recovery methods for i2c-imx so we can recover from situations where SCL/SDA are stuck low.

Once i2c bus SCL/SDA are stuck low during transfer, config the i2c pinctrl to gpio mode by calling pinctrl sleep set function, and then use GPIO to emulate the i2c protocol to send nine dummy clock to recover i2c device. After recovery, set i2c pinctrl to default group setting.

V2:
As Uwe Kleine-König's suggestion, the version do below changes:
-replace of_get_named_gpio() with devm_gpiod_get_optional() -move
gpio_direction_input() and gpio_direction_output() call to the
prepare callback
-use 0 and 1 as return value for the get_scl and get_sda callbacks

---
 Documentation/devicetree/bindings/i2c/i2c-imx.txt |   4 +
 drivers/i2c/busses/i2c-imx.c                      | 104
++++++++++++++++++++--
 2 files changed, 100 insertions(+), 8 deletions(-)
Documentation/devicetree/bindings/i2c/i2c-imx.txt +4 -0
index ce4311d..a9fe525 100644
@@ -14,6 +14,8 @@ Optional properties:
   The absence of the propoerty indicates the default frequency 100 kHz.
 - dmas: A list of two dma specifiers, one for each entry in dma-names.
 - dma-names: should contain "tx" and "rx".
+- scl-gpio: specify the gpio related to SCL pin
+- sda-gpio: specify the gpio related to SDA pin
 
 Examples:
 
@@ -37,4 +39,6 @@ i2c0: i2c@40066000 { /* i2c0 on vf610 */
 	dmas = <&edma0 0 50>,
 		<&edma0 0 51>;
 	dma-names = "rx","tx";
+	scl-gpio = <&gpio5 26 0>;
+	sda-gpio = <&gpio5 27 0>;
 };
drivers/i2c/busses/i2c-imx.c +2 -0
index 785aa67..4f75370 100644
@@ -40,6 +40,7 @@
 #include <linux/dmapool.h>
 #include <linux/err.h>
 #include <linux/errno.h>
+#include <linux/gpio.h>
 #include <linux/i2c.h>
 #include <linux/init.h>
 #include <linux/interrupt.h>
@@ -48,6 +49,7 @@
 #include <linux/module.h>
 #include <linux/of.h>
 #include <linux/of_device.h>
+#include <linux/of_gpio.h>
 #include <linux/of_dma.h>
 #include <linux/platform_data/i2c-imx.h>  #include
 <linux/platform_device.h> @@ -175,6 +177,11 @@ enum imx_i2c_type {
drivers/i2c/busses/i2c-imx.c +7 -1
 	VF610_I2C,
 };
 
+struct imx_i2c_pinctrl {
+	struct gpio_desc *sda;
+	struct gpio_desc *scl;
+};
+
 struct imx_i2c_hwdata {
 	enum imx_i2c_type	devtype;
 	unsigned		regshift;
@@ -206,6 +213,7 @@ struct imx_i2c_struct {
 	unsigned int		ifdr; /* IMX_I2C_IFDR */
 	unsigned int		cur_clk;
 	unsigned int		bitrate;
+	struct imx_i2c_pinctrl	pins;
 	const struct imx_i2c_hwdata	*hwdata;
 
 	struct imx_i2c_dma	*dma;
@@ -436,7 +444,7 @@ static int i2c_imx_bus_busy(struct imx_i2c_struct
 *i2c_imx, int for_busy)
 		if (time_after(jiffies, orig_jiffies + msecs_to_jiffies(500)))
 {
 			dev_dbg(&i2c_imx->adapter.dev,
 				"<%s> I2C bus is busy\n", __func__);
-			return -ETIMEDOUT;
+			return i2c_recover_bus(&i2c_imx->adapter);
 		}
drivers/i2c/busses/i2c-imx.c +1 -1
 		schedule();
 	}
@@ -450,7 +458,7 @@ static int i2c_imx_trx_complete(struct imx_i2c_struct
 *i2c_imx)
 
 	if (unlikely(!(i2c_imx->i2csr & I2SR_IIF))) {
 		dev_dbg(&i2c_imx->adapter.dev, "<%s> Timeout\n", __func__);
-		return -ETIMEDOUT;
+		return i2c_recover_bus(&i2c_imx->adapter);
 	}
 	dev_dbg(&i2c_imx->adapter.dev, "<%s> TRX complete\n", __func__);
drivers/i2c/busses/i2c-imx.c +54 -0
 	i2c_imx->i2csr = 0;
@@ -956,6 +964,67 @@ fail0:
 	return (result < 0) ? result : num;
 }
 
+static int i2c_imx_get_scl(struct i2c_adapter *adap) {
+	struct imx_i2c_struct *i2c_imx;
+
+	i2c_imx = container_of(adap, struct imx_i2c_struct, adapter);
+	if (i2c_imx->pins.scl)
+		return gpiod_get_value(i2c_imx->pins.scl);
+
+	return 1;
+}
+
+static int i2c_imx_get_sda(struct i2c_adapter *adap) {
+	struct imx_i2c_struct *i2c_imx;
+
+	i2c_imx = container_of(adap, struct imx_i2c_struct, adapter);
+	if (i2c_imx->pins.sda)
+		return gpiod_get_value(i2c_imx->pins.sda);
+
+	return 1;
+}
+
+static void i2c_imx_set_scl(struct i2c_adapter *adap, int val) {
+	struct imx_i2c_struct *i2c_imx;
+
+	i2c_imx = container_of(adap, struct imx_i2c_struct, adapter);
+	if (i2c_imx->pins.scl)
+		gpiod_set_value(i2c_imx->pins.scl, val); }
+
+static void i2c_imx_prepare_recovery(struct i2c_adapter *adap) {
+	struct imx_i2c_struct *i2c_imx;
+
+	i2c_imx = container_of(adap, struct imx_i2c_struct, adapter);
+	if (i2c_imx->pins.sda && i2c_imx->pins.scl) {
+		pinctrl_pm_select_sleep_state(&adap->dev);
+		gpiod_direction_input(i2c_imx->pins.sda);
+		gpiod_direction_output(i2c_imx->pins.scl, 1);
+	}
+}
+
+static void i2c_imx_unprepare_recovery(struct i2c_adapter *adap) {
+	struct imx_i2c_struct *i2c_imx;
+
+	i2c_imx = container_of(adap, struct imx_i2c_struct, adapter);
+	if (i2c_imx->pins.sda && i2c_imx->pins.scl)
+		pinctrl_pm_select_default_state(&adap->dev);
+}
+
+static struct i2c_bus_recovery_info i2c_imx_bus_recovery_info = {
+	.get_scl = i2c_imx_get_scl,
+	.get_sda = i2c_imx_get_sda,
+	.set_scl = i2c_imx_set_scl,
+	.prepare_recovery = i2c_imx_prepare_recovery,
+	.unprepare_recovery = i2c_imx_unprepare_recovery,
+	.recover_bus = i2c_generic_scl_recovery, };
+
 static u32 i2c_imx_func(struct i2c_adapter *adapter)  {
 	return I2C_FUNC_I2C | I2C_FUNC_SMBUS_EMUL @@ -1004,12 +1073,13 @@
 static int i2c_imx_probe(struct platform_device *pdev)
 
 	/* Setup i2c_imx driver structure */
 	strlcpy(i2c_imx->adapter.name, pdev->name, sizeof(i2c_imx-
Quoted 1 line

adapter.name));

drivers/i2c/busses/i2c-imx.c +7 -6
-	i2c_imx->adapter.owner		= THIS_MODULE;
-	i2c_imx->adapter.algo		= &i2c_imx_algo;
-	i2c_imx->adapter.dev.parent	= &pdev->dev;
-	i2c_imx->adapter.nr		= pdev->id;
-	i2c_imx->adapter.dev.of_node	= pdev->dev.of_node;
-	i2c_imx->base			= base;
+	i2c_imx->adapter.owner = THIS_MODULE;
+	i2c_imx->adapter.algo = &i2c_imx_algo;
+	i2c_imx->adapter.dev.parent = &pdev->dev;
+	i2c_imx->adapter.nr = pdev->id;
+	i2c_imx->adapter.dev.of_node = pdev->dev.of_node;
+	i2c_imx->adapter.bus_recovery_info = &i2c_imx_bus_recovery_info;
+	i2c_imx->base = base;
 
 	/* Get I2C clock */
 	i2c_imx->clk = devm_clk_get(&pdev->dev, NULL); @@ -1037,6 +1107,24

@@ static int i2c_imx_probe(struct platform_device *pdev)

drivers/i2c/busses/i2c-imx.c +18 -1
 	/* Set up adapter data */
 	i2c_set_adapdata(&i2c_imx->adapter, i2c_imx);
 
+	/* Init recover pins */
+	i2c_imx->pins.sda =
+		devm_gpiod_get_optional(&pdev->dev, "sda-gpio", 0);
+	i2c_imx->pins.scl =
+		devm_gpiod_get_optional(&pdev->dev, "scl-gpio", 0);
+
+	if (IS_ERR(i2c_imx->pins.sda)) {
+		ret = PTR_ERR(i2c_imx->pins.sda);
+		dev_err(&pdev->dev, "Failed to get sda gpio: %d\n", ret);
+		i2c_imx->pins.sda = NULL;
+	}
+
+	if (IS_ERR(i2c_imx->pins.scl)) {
+		ret = PTR_ERR(i2c_imx->pins.scl);
+		dev_err(&pdev->dev, "Failed to get scl gpio: %d\n", ret);
+		i2c_imx->pins.scl = NULL;
+	}
+
 	/* Set up clock divider */
 	i2c_imx->bitrate = IMX_I2C_BIT_RATE;
 	ret = of_property_read_u32(pdev->dev.of_node,
--

1.9.1

#3 lore raw
In reply to a message not in the archive

Hello,

Quoted 8 lines · Gao Pan

Implement bus recovery methods for i2c-imx so we can recover from situations where SCL/SDA are stuck low.

Once i2c bus SCL/SDA are stuck low during transfer, config the i2c pinctrl to gpio mode by calling pinctrl sleep set function, and then use GPIO to emulate the i2c protocol to send nine dummy clock to recover i2c device. After recovery, set i2c pinctrl to default group setting.

Quoted 6 lines

V2:
As Uwe Kleine-König's suggestion, the version do below changes:
-replace of_get_named_gpio() with devm_gpiod_get_optional() -move gpio_direction_input() and gpio_direction_output() call to the
prepare callback
-use 0 and 1 as return value for the get_scl and get_sda callbacks

this section usally goes after the triple-dash below. Otherwise this is included in the commit log which it shouldn't.

Documentation/devicetree/bindings/i2c/i2c-imx.txt Optional properties: · line 18

Quoted 10 lines
Documentation/devicetree/bindings/i2c/i2c-imx.txt +2 -0
index ce4311d..a9fe525 100644
@@ -14,6 +14,8 @@ Optional properties:
   The absence of the propoerty indicates the default frequency 100 kHz.
 - dmas: A list of two dma specifiers, one for each entry in dma-names.
 - dma-names: should contain "tx" and "rx".
+- scl-gpio: specify the gpio related to SCL pin
+- sda-gpio: specify the gpio related to SDA pin

In the meantime I learned that "...-gpios" is the recommended name for such properties, even if conceptually there can only be a single gpio here. (You might want to help to change that?!)

Quoted 10 lines
Documentation/devicetree/bindings/i2c/i2c-imx.txt +1 -1
@@ -436,7 +444,7 @@ static int i2c_imx_bus_busy(struct imx_i2c_struct *i2c_imx, int for_busy)
 		if (time_after(jiffies, orig_jiffies + msecs_to_jiffies(500))) {
 			dev_dbg(&i2c_imx->adapter.dev,
 				"<%s> I2C bus is busy\n", __func__);
-			return -ETIMEDOUT;
+			return i2c_recover_bus(&i2c_imx->adapter);
 		}
 		schedule();
 	}

Documentation/devicetree/bindings/i2c/i2c-imx.txt static int i2c_imx_trx_complete(struct imx_i2c_struct *i2c_imx) · line 461

Quoted 5 lines
Documentation/devicetree/bindings/i2c/i2c-imx.txt +1 -1
@@ -450,7 +458,7 @@ static int i2c_imx_trx_complete(struct imx_i2c_struct *i2c_imx)
 
 	if (unlikely(!(i2c_imx->i2csr & I2SR_IIF))) {
 		dev_dbg(&i2c_imx->adapter.dev, "<%s> Timeout\n", __func__);
-		return -ETIMEDOUT;
+		return i2c_recover_bus(&i2c_imx->adapter);

I'm still not convinced that doing that here unconditionally is a good idea. Wolfram will know for sure.

Documentation/devicetree/bindings/i2c/i2c-imx.txt static int i2c_imx_trx_complete(struct imx_i2c_struct *i2c_imx)

Quoted 7 lines
Documentation/devicetree/bindings/i2c/i2c-imx.txt +7 -0
+static void i2c_imx_prepare_recovery(struct i2c_adapter *adap)
+{
+	struct imx_i2c_struct *i2c_imx;
+
+	i2c_imx = container_of(adap, struct imx_i2c_struct, adapter);
+	if (i2c_imx->pins.sda && i2c_imx->pins.scl) {
+		pinctrl_pm_select_sleep_state(&adap->dev);

Your requirement that the sleep state should configure the pins as gpio is strange. Maybe better introduce a dedicated state for recovery? At least you should document this requirement.

Quoted 6 lines
Documentation/devicetree/bindings/i2c/i2c-imx.txt +0 -2
@@ -1004,12 +1073,13 @@ static int i2c_imx_probe(struct platform_device *pdev)
 
 	/* Setup i2c_imx driver structure */
 	strlcpy(i2c_imx->adapter.name, pdev->name, sizeof(i2c_imx->adapter.name));
-	i2c_imx->adapter.owner		= THIS_MODULE;
-	i2c_imx->adapter.algo		= &i2c_imx_algo;

Documentation/devicetree/bindings/i2c/i2c-imx.txt static int i2c_imx_probe(struct platform_device *pdev) · line 1081

Quoted 10 lines
Documentation/devicetree/bindings/i2c/i2c-imx.txt +6 -4
-	i2c_imx->adapter.dev.parent	= &pdev->dev;
-	i2c_imx->adapter.nr		= pdev->id;
-	i2c_imx->adapter.dev.of_node	= pdev->dev.of_node;
-	i2c_imx->base			= base;
+	i2c_imx->adapter.owner = THIS_MODULE;
+	i2c_imx->adapter.algo = &i2c_imx_algo;
+	i2c_imx->adapter.dev.parent = &pdev->dev;
+	i2c_imx->adapter.nr = pdev->id;
+	i2c_imx->adapter.dev.of_node = pdev->dev.of_node;
+	i2c_imx->adapter.bus_recovery_info = &i2c_imx_bus_recovery_info;

What happens if the device tree only specifies a gpio for sda but not scl? The recovery stuff is still hooked up, right? What about only doing that if both properties are valid? This would also get rid of the tests for validity in the recovery callbacks.

Documentation/devicetree/bindings/i2c/i2c-imx.txt static int i2c_imx_probe(struct platform_device *pdev) · line 1114

Quoted 9 lines
Documentation/devicetree/bindings/i2c/i2c-imx.txt +5 -0
@@ -1037,6 +1107,24 @@ static int i2c_imx_probe(struct platform_device *pdev)
 	/* Set up adapter data */
 	i2c_set_adapdata(&i2c_imx->adapter, i2c_imx);
 
+	/* Init recover pins */
+	i2c_imx->pins.sda =
+		devm_gpiod_get_optional(&pdev->dev, "sda-gpio", 0);
+	i2c_imx->pins.scl =
+		devm_gpiod_get_optional(&pdev->dev, "scl-gpio", 0);

0 is wrong here, better use GPIOD_ASIS. Is there a reason not to configure the direction as input already here?

Documentation/devicetree/bindings/i2c/i2c-imx.txt static int i2c_imx_probe(struct platform_device *pdev) · line 1120

Quoted 6 lines
Documentation/devicetree/bindings/i2c/i2c-imx.txt +6 -0
+
+	if (IS_ERR(i2c_imx->pins.sda)) {
+		ret = PTR_ERR(i2c_imx->pins.sda);
+		dev_err(&pdev->dev, "Failed to get sda gpio: %d\n", ret);
+		i2c_imx->pins.sda = NULL;
+	}

As you're using devm_gpiod_get_optional there is no justification to ignore the error code.

Best regards
Uwe

Signature · 2 lines
Pengutronix e.K.                           | Uwe Kleine-König            |
Industrial Linux Solutions                 | http://www.pengutronix.de/  |
#5 lore raw
In reply to a message not in the archive

From: Uwe Kleine-König <mailto:[email protected]> Sent: Thursday, August 13, 2015 4:15 PM

Quoted 23 lines

To: Gao Pan-B54642
Cc: wsa-z923LK4zBo2bacvFa/[email protected]; [email protected]; Li Frank-B20596; Duan Fugang-B38611
Subject: Re: [Patch v2] i2c: imx: implement bus recovery

Hello,

Quoted 7 lines · Gao Pan

Implement bus recovery methods for i2c-imx so we can recover from situations where SCL/SDA are stuck low.

Once i2c bus SCL/SDA are stuck low during transfer, config the i2c pinctrl to gpio mode by calling pinctrl sleep set function, and then use GPIO to emulate the i2c protocol to send nine dummy clock to recover i2c device. After recovery, set i2c pinctrl to default group

setting.

Quoted 7 lines

V2:
As Uwe Kleine-König's suggestion, the version do below changes:
-replace of_get_named_gpio() with devm_gpiod_get_optional() -move
gpio_direction_input() and gpio_direction_output() call to the
prepare callback
-use 0 and 1 as return value for the get_scl and get_sda callbacks

Quoted 2 lines

this section usally goes after the triple-dash below. Otherwise this is included in the commit log which it shouldn't.

Thanks, will change it in the next version.

Quoted 7 lines
Quoted 7 lines
Documentation/devicetree/bindings/i2c/i2c-imx.txt +0 -0
index ce4311d..a9fe525 100644
@@ -14,6 +14,8 @@ Optional properties:
   The absence of the propoerty indicates the default frequency 100 kHz.
Quoted 8 lines

Documentation/devicetree/bindings/i2c/i2c-imx.txt Optional properties: · line 18

Quoted 4 lines
Documentation/devicetree/bindings/i2c/i2c-imx.txt +2 -0
 - dmas: A list of two dma specifiers, one for each entry in dma-names.
 - dma-names: should contain "tx" and "rx".
+- scl-gpio: specify the gpio related to SCL pin
+- sda-gpio: specify the gpio related to SDA pin

In the meantime I learned that "...-gpios" is the recommended name for such properties, even if conceptually there can only be a single gpio here. (You might want to help to change that?!)

Thanks, will change it in the next version.

Quoted 14 lines

Documentation/devicetree/bindings/i2c/i2c-imx.txt static int i2c_imx_bus_busy(struct imx_i2c_struct

Quoted 1 line
Documentation/devicetree/bindings/i2c/i2c-imx.txt +0 -0
@@ -436,7 +444,7 @@ static int i2c_imx_bus_busy(struct imx_i2c_struct

*i2c_imx, int for_busy)

Documentation/devicetree/bindings/i2c/i2c-imx.txt static int i2c_imx_bus_busy(struct imx_i2c_struct · line 444

Quoted 1 line
Documentation/devicetree/bindings/i2c/i2c-imx.txt +0 -0
 		if (time_after(jiffies, orig_jiffies + msecs_to_jiffies(500)))

{

Quoted 10 lines
Documentation/devicetree/bindings/i2c/i2c-imx.txt +1 -1
 			dev_dbg(&i2c_imx->adapter.dev,
 				"<%s> I2C bus is busy\n", __func__);
-			return -ETIMEDOUT;
+			return i2c_recover_bus(&i2c_imx->adapter);
 		}
 		schedule();
 	}
@@ -450,7 +458,7 @@ static int i2c_imx_trx_complete(struct
 imx_i2c_struct *i2c_imx)
 
Quoted 7 lines

Documentation/devicetree/bindings/i2c/i2c-imx.txt static int i2c_imx_trx_complete(struct · line 462

Quoted 4 lines
Documentation/devicetree/bindings/i2c/i2c-imx.txt +1 -1
 	if (unlikely(!(i2c_imx->i2csr & I2SR_IIF))) {
 		dev_dbg(&i2c_imx->adapter.dev, "<%s> Timeout\n", __func__);
-		return -ETIMEDOUT;
+		return i2c_recover_bus(&i2c_imx->adapter);

I'm still not convinced that doing that here unconditionally is a good idea. Wolfram will know for sure.

Thanks, I will remove this change in next version.

Quoted 10 lines

Documentation/devicetree/bindings/i2c/i2c-imx.txt static int i2c_imx_trx_complete(struct

Quoted 6 lines
Documentation/devicetree/bindings/i2c/i2c-imx.txt +6 -0
+static void i2c_imx_prepare_recovery(struct i2c_adapter *adap) {
+	struct imx_i2c_struct *i2c_imx;
+
+	i2c_imx = container_of(adap, struct imx_i2c_struct, adapter);
+	if (i2c_imx->pins.sda && i2c_imx->pins.scl) {
+		pinctrl_pm_select_sleep_state(&adap->dev);

Your requirement that the sleep state should configure the pins as gpio is strange. Maybe better introduce a dedicated state for recovery? At least you should document this requirement.

In general, pinctrl sleep mode is gpio function. I will add this in binding doc. Thanks.

Quoted 16 lines
Quoted 16 lines
Documentation/devicetree/bindings/i2c/i2c-imx.txt +3 -6
@@ -1004,12 +1073,13 @@ static int i2c_imx_probe(struct
 platform_device *pdev)
 
 	/* Setup i2c_imx driver structure */
 	strlcpy(i2c_imx->adapter.name, pdev->name, sizeof(i2c_imx-
 adapter.name));
-	i2c_imx->adapter.owner		= THIS_MODULE;
-	i2c_imx->adapter.algo		= &i2c_imx_algo;
-	i2c_imx->adapter.dev.parent	= &pdev->dev;
-	i2c_imx->adapter.nr		= pdev->id;
-	i2c_imx->adapter.dev.of_node	= pdev->dev.of_node;
-	i2c_imx->base			= base;
+	i2c_imx->adapter.owner = THIS_MODULE;
+	i2c_imx->adapter.algo = &i2c_imx_algo;
+	i2c_imx->adapter.dev.parent = &pdev->dev;
Quoted 8 lines

Documentation/devicetree/bindings/i2c/i2c-imx.txt static int i2c_imx_probe(struct · line 1083

Quoted 3 lines
Documentation/devicetree/bindings/i2c/i2c-imx.txt +3 -0
+	i2c_imx->adapter.nr = pdev->id;
+	i2c_imx->adapter.dev.of_node = pdev->dev.of_node;
+	i2c_imx->adapter.bus_recovery_info = &i2c_imx_bus_recovery_info;

What happens if the device tree only specifies a gpio for sda but not scl? The recovery stuff is still hooked up, right? What about only doing that if both properties are valid? This would also get rid of the tests for validity in the recovery callbacks.

It requires device tree to specify gpio for sda and scl in both. Once only sda is defined, the driver doesn't carry out the recovery process.

Quoted 6 lines

Documentation/devicetree/bindings/i2c/i2c-imx.txt static int i2c_imx_probe(struct platform_device

Quoted 2 lines
Documentation/devicetree/bindings/i2c/i2c-imx.txt +0 -0
@@ -1037,6 +1107,24 @@ static int i2c_imx_probe(struct platform_device

*pdev)

Quoted 3 lines
Documentation/devicetree/bindings/i2c/i2c-imx.txt +0 -0
 	/* Set up adapter data */
 	i2c_set_adapdata(&i2c_imx->adapter, i2c_imx);
 
Quoted 8 lines

Documentation/devicetree/bindings/i2c/i2c-imx.txt static int i2c_imx_probe(struct platform_device · line 1114

Quoted 5 lines
Documentation/devicetree/bindings/i2c/i2c-imx.txt +5 -0
+	/* Init recover pins */
+	i2c_imx->pins.sda =
+		devm_gpiod_get_optional(&pdev->dev, "sda-gpio", 0);
+	i2c_imx->pins.scl =
+		devm_gpiod_get_optional(&pdev->dev, "scl-gpio", 0);

0 is wrong here, better use GPIOD_ASIS. Is there a reason not to configure the direction as input already here?

Thanks, 0 is inappropriate here. Actually, we should use GPIOD_OUT_HIGH to set the gpios high.

Quoted 9 lines

Documentation/devicetree/bindings/i2c/i2c-imx.txt static int i2c_imx_probe(struct platform_device · line 1120

Quoted 6 lines
Documentation/devicetree/bindings/i2c/i2c-imx.txt +6 -0
+
+	if (IS_ERR(i2c_imx->pins.sda)) {
+		ret = PTR_ERR(i2c_imx->pins.sda);
+		dev_err(&pdev->dev, "Failed to get sda gpio: %d\n", ret);
+		i2c_imx->pins.sda = NULL;
+	}

As you're using devm_gpiod_get_optional there is no justification to ignore the error code.

Thanks, will change it in next version.

#7 lore raw
In reply to a message not in the archive

Hello,

Cc += Linus Walleij

Quoted 12 lines · Gao Pandy

From: Uwe Kleine-König <mailto:[email protected]> Sent: Thursday, August 13, 2015 4:15 PM

Quoted 10 lines

drivers/i2c/busses/i2c-imx.c fail0: · line 1004

Quoted 6 lines
drivers/i2c/busses/i2c-imx.c +6 -0
+static void i2c_imx_prepare_recovery(struct i2c_adapter *adap) {
+	struct imx_i2c_struct *i2c_imx;
+
+	i2c_imx = container_of(adap, struct imx_i2c_struct, adapter);
+	if (i2c_imx->pins.sda && i2c_imx->pins.scl) {
+		pinctrl_pm_select_sleep_state(&adap->dev);

Your requirement that the sleep state should configure the pins as gpio is strange. Maybe better introduce a dedicated state for recovery? At least you should document this requirement.

Quoted 1 line

In general, pinctrl sleep mode is gpio function. I will add this in binding doc. Thanks.

Linus, do you have to say something here? It might be right to have the gpio function as configuration for sleep mode, but it doesn't look right for me to use this for recovery purposes.

Quoted 25 lines
Quoted 16 lines
Quoted 16 lines
drivers/i2c/busses/i2c-imx.c +3 -6
@@ -1004,12 +1073,13 @@ static int i2c_imx_probe(struct
 platform_device *pdev)
 
 	/* Setup i2c_imx driver structure */
 	strlcpy(i2c_imx->adapter.name, pdev->name, sizeof(i2c_imx-
 adapter.name));
-	i2c_imx->adapter.owner		= THIS_MODULE;
-	i2c_imx->adapter.algo		= &i2c_imx_algo;
-	i2c_imx->adapter.dev.parent	= &pdev->dev;
-	i2c_imx->adapter.nr		= pdev->id;
-	i2c_imx->adapter.dev.of_node	= pdev->dev.of_node;
-	i2c_imx->base			= base;
+	i2c_imx->adapter.owner = THIS_MODULE;
+	i2c_imx->adapter.algo = &i2c_imx_algo;
+	i2c_imx->adapter.dev.parent = &pdev->dev;
Quoted 8 lines

drivers/i2c/busses/i2c-imx.c static int i2c_imx_probe(struct platform_device *pdev) · line 1081

Quoted 3 lines
drivers/i2c/busses/i2c-imx.c +3 -0
+	i2c_imx->adapter.nr = pdev->id;
+	i2c_imx->adapter.dev.of_node = pdev->dev.of_node;
+	i2c_imx->adapter.bus_recovery_info = &i2c_imx_bus_recovery_info;

What happens if the device tree only specifies a gpio for sda but not scl? The recovery stuff is still hooked up, right? What about only doing that if both properties are valid? This would also get rid of the tests for validity in the recovery callbacks.

Quoted 2 lines

It requires device tree to specify gpio for sda and scl in both. Once only sda is defined, the driver doesn't carry out the recovery process.

Right, and still it is convinced it is able to recover the bus, right? Can you please change this?

Quoted 15 lines
Quoted 6 lines

Review static int i2c_imx_probe(struct platform_device

Quoted 2 lines
(quoted diff) +0 -0
@@ -1037,6 +1107,24 @@ static int i2c_imx_probe(struct platform_device

*pdev)

Quoted 3 lines
drivers/i2c/busses/i2c-imx.c +0 -0
 	/* Set up adapter data */
 	i2c_set_adapdata(&i2c_imx->adapter, i2c_imx);
 
Quoted 8 lines

drivers/i2c/busses/i2c-imx.c static int i2c_imx_probe(struct platform_device *pdev) · line 1114

Quoted 5 lines
drivers/i2c/busses/i2c-imx.c +5 -0
+	/* Init recover pins */
+	i2c_imx->pins.sda =
+		devm_gpiod_get_optional(&pdev->dev, "sda-gpio", 0);
+	i2c_imx->pins.scl =
+		devm_gpiod_get_optional(&pdev->dev, "scl-gpio", 0);

0 is wrong here, better use GPIOD_ASIS. Is there a reason not to configure the direction as input already here?

Quoted 2 lines

Thanks, 0 is inappropriate here. Actually, we should use GPIOD_OUT_HIGH to set the gpios high.

Are you sure? Consider the bus is blocked and you switch to gpio mode to do recovery you have a device pulling SDA to 0 and the imx pulling it to
1. That's wrong, isn't it?

Best regards
Uwe

Signature · 2 lines
Pengutronix e.K.                           | Uwe Kleine-König            |
Industrial Linux Solutions                 | http://www.pengutronix.de/  |
#9 lore raw
In reply to a message not in the archive

From: [email protected] <mailto:[email protected]> Sent: Wednesday, August 19, 2015 3:02 PM

Quoted 23 lines

To: Gao Pan-B54642
Cc: wsa-z923LK4zBo2bacvFa/[email protected]; [email protected]; Li Frank-B20596; Duan Fugang-B38611; Linus Walleij
Subject: Re: [Patch v2] i2c: imx: implement bus recovery

Hello,

Cc += Linus Walleij

Quoted 13 lines · Gao Pandy

From: Uwe Kleine-König <mailto:[email protected]> Sent:
Thursday, August 13, 2015 4:15 PM

Quoted 10 lines

Review

Quoted 6 lines
(quoted diff) +6 -0
+static void i2c_imx_prepare_recovery(struct i2c_adapter *adap) {
+	struct imx_i2c_struct *i2c_imx;
+
+	i2c_imx = container_of(adap, struct imx_i2c_struct, adapter);
+	if (i2c_imx->pins.sda && i2c_imx->pins.scl) {
+		pinctrl_pm_select_sleep_state(&adap->dev);

Your requirement that the sleep state should configure the pins as gpio is strange. Maybe better introduce a dedicated state for recovery? At least you should document this requirement.

Quoted 6 lines
Quoted 1 line

In general, pinctrl sleep mode is gpio function. I will add this in

binding doc. Thanks.

Linus, do you have to say something here? It might be right to have the gpio function as configuration for sleep mode, but it doesn't look right for me to use this for recovery purposes.

Thanks for your precise review.

Quoted 27 lines
Quoted 4 lines
Quoted 4 lines
Quoted 4 lines
(quoted diff) +0 -0
@@ -1004,12 +1073,13 @@ static int i2c_imx_probe(struct
 platform_device *pdev)
 
Quoted 15 lines
Quoted 7 lines
Quoted 7 lines
(quoted diff) +0 -4
 	/* Setup i2c_imx driver structure */
 	strlcpy(i2c_imx->adapter.name, pdev->name, sizeof(i2c_imx-
 adapter.name));
-	i2c_imx->adapter.owner		= THIS_MODULE;
-	i2c_imx->adapter.algo		= &i2c_imx_algo;
-	i2c_imx->adapter.dev.parent	= &pdev->dev;
-	i2c_imx->adapter.nr		= pdev->id;
Quoted 8 lines

Review static int i2c_imx_probe(struct · line 1083

Quoted 8 lines
(quoted diff) +6 -2
-	i2c_imx->adapter.dev.of_node	= pdev->dev.of_node;
-	i2c_imx->base			= base;
+	i2c_imx->adapter.owner = THIS_MODULE;
+	i2c_imx->adapter.algo = &i2c_imx_algo;
+	i2c_imx->adapter.dev.parent = &pdev->dev;
+	i2c_imx->adapter.nr = pdev->id;
+	i2c_imx->adapter.dev.of_node = pdev->dev.of_node;
+	i2c_imx->adapter.bus_recovery_info =

&i2c_imx_bus_recovery_info;

Quoted 2 lines
Quoted 2 lines

What happens if the device tree only specifies a gpio for sda but not

scl?

Quoted 4 lines
Quoted 3 lines

The recovery stuff is still hooked up, right? What about only doing that if both properties are valid? This would also get rid of the tests for validity in the recovery callbacks.

Quoted 5 lines
Quoted 2 lines

It requires device tree to specify gpio for sda and scl in both. Once only sda is defined, the driver doesn't carry out the recovery process.

Right, and still it is convinced it is able to recover the bus, right? Can you please change this?

Yes, you are right, will change it in the next version.

Quoted 16 lines
Quoted 16 lines
Quoted 15 lines
Quoted 3 lines
(quoted diff) +0 -0
@@ -1037,6 +1107,24 @@ static int i2c_imx_probe(struct

platform_device

*pdev)

Review

Quoted 8 lines
(quoted diff) +5 -0
 	/* Set up adapter data */
 	i2c_set_adapdata(&i2c_imx->adapter, i2c_imx);
 
+	/* Init recover pins */
+	i2c_imx->pins.sda =
+		devm_gpiod_get_optional(&pdev->dev, "sda-gpio", 0);
+	i2c_imx->pins.scl =
+		devm_gpiod_get_optional(&pdev->dev, "scl-gpio", 0);

0 is wrong here, better use GPIOD_ASIS. Is there a reason not to configure the direction as input already here?

Quoted 6 lines
Quoted 2 lines

Thanks, 0 is inappropriate here. Actually, we should use GPIOD_OUT_HIGH to set the gpios high.

Are you sure? Consider the bus is blocked and you switch to gpio mode to do recovery you have a device pulling SDA to 0 and the imx pulling it to
1. That's wrong, isn't it?

Yes, you are right. The gpios should be set low. Thanks.

#11 lore raw
In reply to a message not in the archive

Hello,

Quoted 13 lines · Gao Pandy
Quoted 13 lines · Gao Pandy
Quoted 12 lines
Quoted 11 lines

Review

Quoted 8 lines
(quoted diff) +5 -0
 	/* Set up adapter data */
 	i2c_set_adapdata(&i2c_imx->adapter, i2c_imx);
 
+	/* Init recover pins */
+	i2c_imx->pins.sda =
+		devm_gpiod_get_optional(&pdev->dev, "sda-gpio", 0);
+	i2c_imx->pins.scl =
+		devm_gpiod_get_optional(&pdev->dev, "scl-gpio", 0);

0 is wrong here, better use GPIOD_ASIS. Is there a reason not to configure the direction as input already here?

Quoted 8 lines
Quoted 6 lines
Quoted 2 lines

Thanks, 0 is inappropriate here. Actually, we should use GPIOD_OUT_HIGH to set the gpios high.

Are you sure? Consider the bus is blocked and you switch to gpio mode to do recovery you have a device pulling SDA to 0 and the imx pulling it to
1. That's wrong, isn't it?

Yes, you are right. The gpios should be set low. Thanks.

Then on switching to the gpio function from an idle bus (which should be the normal case) you pull both lines low which depending on the timing might even be recognised as a start condition. I'm convinced the right thing to select here is GPIOD_IN.

Best regards
Uwe

Signature · 2 lines
Pengutronix e.K.                           | Uwe Kleine-König            |
Industrial Linux Solutions                 | http://www.pengutronix.de/  |
#12 lore raw
In reply to a message not in the archive
Quoted 13 lines · Uwe Kleine-König
Quoted 12 lines · Gao Pandy

From: Uwe Kleine-König <mailto:[email protected]> Sent: Thursday, August 13, 2015 4:15 PM

Quoted 10 lines

Review

Quoted 6 lines
(quoted diff) +6 -0
+static void i2c_imx_prepare_recovery(struct i2c_adapter *adap) {
+ struct imx_i2c_struct *i2c_imx;
+
+ i2c_imx = container_of(adap, struct imx_i2c_struct, adapter);
+ if (i2c_imx->pins.sda && i2c_imx->pins.scl) {
+         pinctrl_pm_select_sleep_state(&adap->dev);

Your requirement that the sleep state should configure the pins as gpio is strange. Maybe better introduce a dedicated state for recovery? At least you should document this requirement.

Quoted 5 lines
Quoted 1 line

In general, pinctrl sleep mode is gpio function. I will add this in binding doc. Thanks.

Linus, do you have to say something here? It might be right to have the gpio function as configuration for sleep mode, but it doesn't look right for me to use this for recovery purposes.

What it usually means is that the pin has a function mode such that an asynchronous edge detector is connected to it on the outer pad ring, maybe in tristate or some pull-up/down configuration. This makes is possible for the system to power
off completely until an event occurs there with only some very peripheral electronics powered up.

I think the terminology depend on the use case. See the section "GPIO mode pitfalls" in Documentation/pinctrl.txt

If the use case is around the i2c traffic, it is a mode related to I2C, and if this mode is called "GPIO mode" in the data sheet
is irrelevant, because it is obviously not used for the generic input/output but the specific I2C. The terminology should be made familiar to whoever needs to go in and read the code later.

Yours,
Linus Walleij

#14 lore raw
In reply to a message not in the archive

Hallo Linus,

Quoted 28 lines · Linus Walleij
Quoted 13 lines · Uwe Kleine-König
Quoted 12 lines · Gao Pandy

From: Uwe Kleine-König <mailto:[email protected]> Sent: Thursday, August 13, 2015 4:15 PM

Quoted 10 lines

Review

Quoted 6 lines
(quoted diff) +6 -0
+static void i2c_imx_prepare_recovery(struct i2c_adapter *adap) {
+ struct imx_i2c_struct *i2c_imx;
+
+ i2c_imx = container_of(adap, struct imx_i2c_struct, adapter);
+ if (i2c_imx->pins.sda && i2c_imx->pins.scl) {
+         pinctrl_pm_select_sleep_state(&adap->dev);

Your requirement that the sleep state should configure the pins as gpio is strange. Maybe better introduce a dedicated state for recovery? At least you should document this requirement.

Quoted 5 lines
Quoted 1 line

In general, pinctrl sleep mode is gpio function. I will add this in binding doc. Thanks.

Linus, do you have to say something here? It might be right to have the gpio function as configuration for sleep mode, but it doesn't look right for me to use this for recovery purposes.

What it usually means is that the pin has a function mode such that an asynchronous edge detector is connected to it on the outer pad ring, maybe in tristate or some pull-up/down configuration. This makes is possible for the system to power
off completely until an event occurs there with only some very peripheral electronics powered up.

Quoted 8 lines

I think the terminology depend on the use case. See the section "GPIO mode pitfalls" in Documentation/pinctrl.txt

If the use case is around the i2c traffic, it is a mode related to I2C, and if this mode is called "GPIO mode" in the data sheet
is irrelevant, because it is obviously not used for the generic input/output but the specific I2C. The terminology should be made familiar to whoever needs to go in and read the code later.

The background info that was obviously missing from the part of the thread I sent to you is that pinctrl_pm_select_sleep_state is used to prepare bitbanging on the i2c bus to do bus recovery. (The controller doesn't implement this, so we have to resort to manually drive the pins.)

Is this good enough? I'd like to see implemented a separate pinctrl set for bitbanging instead of relying on the sleep state being the right thing to enable gpio functionality.

Best regards
Uwe

Signature · 2 lines
Pengutronix e.K.                           | Uwe Kleine-König            |
Industrial Linux Solutions                 | http://www.pengutronix.de/  |
#16 lore raw
In reply to a message not in the archive
Quoted 8 lines · Uwe Kleine-König

[Me]

Quoted 5 lines

If the use case is around the i2c traffic, it is a mode related to I2C, and if this mode is called "GPIO mode" in the data sheet
is irrelevant, because it is obviously not used for the generic input/output but the specific I2C. The terminology should be made familiar to whoever needs to go in and read the code later.

Quoted 5 lines

The background info that was obviously missing from the part of the thread I sent to you is that pinctrl_pm_select_sleep_state is used to prepare bitbanging on the i2c bus to do bus recovery. (The controller doesn't implement this, so we have to resort to manually drive the pins.)

I don't understand. What does "manually" mean in this context? Code examples for "manual" and "automatic"?

Generally the word "manual" is so ambigous that we should refrain from using it, to me that means something like hammering on a keyboard to alter things through sysfs or debugfs but I hardly believe this is what you mean.

Quoted 3 lines

Is this good enough? I'd like to see implemented a separate pinctrl set for bitbanging instead of relying on the sleep state being the right thing to enable gpio functionality.

This sounds right but I need to see the two different code sets to understand. Now my head is all fuzzy...

Yours,
Linus Walleij

#18 lore raw
In reply to a message not in the archive

Hello,

Quoted 10 lines · Linus Walleij
Quoted 8 lines · Uwe Kleine-König

[Me]

Quoted 5 lines

If the use case is around the i2c traffic, it is a mode related to I2C, and if this mode is called "GPIO mode" in the data sheet
is irrelevant, because it is obviously not used for the generic input/output but the specific I2C. The terminology should be made familiar to whoever needs to go in and read the code later.

Quoted 8 lines
Quoted 5 lines

The background info that was obviously missing from the part of the thread I sent to you is that pinctrl_pm_select_sleep_state is used to prepare bitbanging on the i2c bus to do bus recovery. (The controller doesn't implement this, so we have to resort to manually drive the pins.)

I don't understand. What does "manually" mean in this context? Code examples for "manual" and "automatic"?

Normally i2c operations are done using the i2c core in the SoC and using the i2c function of the respective pins. Recovery
however must be done (manually) by configuring the lines as GPIO and using a sequence of calls to gpio_[gs]et_value.

The sleep state of the i2c bus device (usually?) configures the i2c lines as GPIO. Is it ok to (ab)use the sleep state to prepare for the recovery procedure or should we better introduce a dedicated gpio mode pinctrl?

Best regards
Uwe

Signature · 2 lines
Pengutronix e.K.                           | Uwe Kleine-König            |
Industrial Linux Solutions                 | http://www.pengutronix.de/  |
#20 lore raw
In reply to a message not in the archive
Quoted 9 lines · Uwe Kleine-König

[Me]

Quoted 2 lines

I don't understand. What does "manually" mean in this context? Code examples for "manual" and "automatic"?

Normally i2c operations are done using the i2c core in the SoC and using the i2c function of the respective pins. Recovery
however must be done (manually) by configuring the lines as GPIO and using a sequence of calls to gpio_[gs]et_value.

Quoted 4 lines

The sleep state of the i2c bus device (usually?) configures the i2c lines as GPIO. Is it ok to (ab)use the sleep state to prepare for the recovery procedure or should we better introduce a dedicated gpio mode pinctrl?

OK now I understand (I think).

I think it's OK to use tyhe sleep state as long as there are comments in the code explaining what is going on. It is something extraordinary after all.

I guess this means that you're using GPIO orthogonally to I2C, and thus the pin controller does not have the .strict property set. There should be a comment in the struct pinmux_ops of the driver saying that this pin controller cannot be used as strict. Need to keep the information there so people don't get confused.

Yours,
Linus Walleij