Skip to contentSkip to conversation

i2c: correct I2C deblock logic

PATCH Superseded [email protected] 3 participants 11 messages Feb 10, 2023 – Mar 23, 2023 I2C

v1 of 3 Superseded Latest is v3 →

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 i2c: correct I2C deblock logic

From: Haibo Chen <[email protected]>

Current code use dm_gpio_get_value() to get SDA and SCL value, and the value depends on the flag GPIOD_ACTIVE_LOW. When toggle SCL to wait slave release SDA, the SDA are config as GPIOD_IS_IN, and whether contain the GPIOD_ACTIVE_LOW depends on the DTS setting. Usually, for I2C GPIO, we use GPIOD_ACTIVE_LOW flag in DTS, so if the SDA is in low level, then dm_gpio_get_value() will return 1, current code logic will stop the SCL toggle wrongly, cause the I2C deblock not work as expect.

This patch force set the GPIOD_ACTIVE_LOW for both GPIOD_IS_IN and GPIOD_IS_OUT, and make the return value of i2c_gpio_get_pin() eaqual to the physical voltage logic level.

Fixes aa54192d4a87 ("dm: i2c: implement gpio-based I2C deblock")
---
 drivers/i2c/i2c-uclass.c | 5 +++--
 1 file changed, 3 insertions(+), 2 deletions(-)
drivers/i2c/i2c-uclass.c +3 -2
index 8d9a89ed89..4dc707c659 100644
@@ -505,7 +505,8 @@ uint i2c_get_chip_addr_offset_mask(struct udevice *dev)
 static void i2c_gpio_set_pin(struct gpio_desc *pin, int bit)
 {
 	if (bit)
-		dm_gpio_set_dir_flags(pin, GPIOD_IS_IN);
+		dm_gpio_set_dir_flags(pin, GPIOD_IS_IN |
+					   GPIOD_ACTIVE_LOW);
 	else
 		dm_gpio_set_dir_flags(pin, GPIOD_IS_OUT |
 					   GPIOD_ACTIVE_LOW |
@@ -514,7 +515,7 @@ static void i2c_gpio_set_pin(struct gpio_desc *pin, int bit)
 
 static int i2c_gpio_get_pin(struct gpio_desc *pin)
 {
-	return dm_gpio_get_value(pin);
+	return !dm_gpio_get_value(pin);
 }
 
 int i2c_deblock_gpio_loop(struct gpio_desc *sda_pin,
Signature · 1 line
2.34.1
author
#1 lore raw
Quoted 18 lines

-----Original Message-----
From: Bough Chen <[email protected]>
Sent: 2023年2月10日 17:27
To: [email protected]; [email protected]; [email protected]
Cc: [email protected]; dl-uboot-imx <[email protected]>; [email protected]; Bough Chen <[email protected]>
Subject: [PATCH] i2c: correct I2C deblock logic

From: Haibo Chen <[email protected]>

Current code use dm_gpio_get_value() to get SDA and SCL value, and the value depends on the flag GPIOD_ACTIVE_LOW. When toggle SCL to wait slave release SDA, the SDA are config as GPIOD_IS_IN, and whether contain the GPIOD_ACTIVE_LOW depends on the DTS setting. Usually, for I2C GPIO, we use GPIOD_ACTIVE_LOW flag in DTS, so if the SDA is in low level, then dm_gpio_get_value() will return 1, current code logic will stop the SCL toggle wrongly, cause the I2C deblock not work as expect.

Quoted 4 lines

This patch force set the GPIOD_ACTIVE_LOW for both GPIOD_IS_IN and GPIOD_IS_OUT, and make the return value of i2c_gpio_get_pin() eaqual to the physical voltage logic level.

Hi,

Any comments for this patch, just a reminder in case you miss it.

Best Regards
Haibo Chen

Quoted 30 lines
Fixes aa54192d4a87 ("dm: i2c: implement gpio-based I2C deblock")
---
 drivers/i2c/i2c-uclass.c | 5 +++--
 1 file changed, 3 insertions(+), 2 deletions(-)
drivers/i2c/i2c-uclass.c index renamed from drivers/i2c/i2c-uclass.c +0 -0

8d9a89ed89..4dc707c659 100644

drivers/i2c/i2c-uclass.c +3 -3
@@ -505,7 +505,8 @@ uint i2c_get_chip_addr_offset_mask(struct udevice
 *dev)  static void i2c_gpio_set_pin(struct gpio_desc *pin, int bit)  {
 	if (bit)
-		dm_gpio_set_dir_flags(pin, GPIOD_IS_IN);
+		dm_gpio_set_dir_flags(pin, GPIOD_IS_IN |
+					   GPIOD_ACTIVE_LOW);
 	else
 		dm_gpio_set_dir_flags(pin, GPIOD_IS_OUT |
 					   GPIOD_ACTIVE_LOW |
@@ -514,7 +515,7 @@ static void i2c_gpio_set_pin(struct gpio_desc *pin, int
 bit)
 
 static int i2c_gpio_get_pin(struct gpio_desc *pin)  {
-	return dm_gpio_get_value(pin);
+	return !dm_gpio_get_value(pin);
 }
 
 int i2c_deblock_gpio_loop(struct gpio_desc *sda_pin,
--

2.34.1

#2 lore raw

Hi!

Quoted 18 lines · Bough Chen
Quoted 18 lines

-----Original Message-----
From: Bough Chen <[email protected]>
Sent: 2023年2月10日 17:27
To: [email protected]; [email protected]; [email protected]
Cc: [email protected]; dl-uboot-imx <[email protected]>; [email protected]; Bough Chen <[email protected]>
Subject: [PATCH] i2c: correct I2C deblock logic

From: Haibo Chen <[email protected]>

Current code use dm_gpio_get_value() to get SDA and SCL value, and the value depends on the flag GPIOD_ACTIVE_LOW. When toggle SCL to wait slave release SDA, the SDA are config as GPIOD_IS_IN, and whether contain the GPIOD_ACTIVE_LOW depends on the DTS setting. Usually, for I2C GPIO, we use GPIOD_ACTIVE_LOW flag in DTS, so if the SDA is in low level, then dm_gpio_get_value() will return 1, current code logic will stop the SCL toggle wrongly, cause the I2C deblock not work as expect.

Quoted 8 lines
Quoted 4 lines

This patch force set the GPIOD_ACTIVE_LOW for both GPIOD_IS_IN and GPIOD_IS_OUT, and make the return value of i2c_gpio_get_pin() eaqual to the physical voltage logic level.

Hi,

Any comments for this patch, just a reminder in case you miss it.

Indeed, I missed this patch, sorry!
Your explanation sounds reasonable to me, but I wonder if nobody tapped into this problem... it would be good to have some test reports from others! Also added Simon, as he did a lot of work in this space.

Thanks!

bye,
Heiko

Quoted 35 lines

Best Regards
Haibo Chen

Quoted 30 lines
Fixes aa54192d4a87 ("dm: i2c: implement gpio-based I2C deblock")
---
 drivers/i2c/i2c-uclass.c | 5 +++--
 1 file changed, 3 insertions(+), 2 deletions(-)
drivers/i2c/i2c-uclass.c index renamed from drivers/i2c/i2c-uclass.c +0 -0

8d9a89ed89..4dc707c659 100644

drivers/i2c/i2c-uclass.c +3 -3
@@ -505,7 +505,8 @@ uint i2c_get_chip_addr_offset_mask(struct udevice
 *dev)  static void i2c_gpio_set_pin(struct gpio_desc *pin, int bit)  {
 	if (bit)
-		dm_gpio_set_dir_flags(pin, GPIOD_IS_IN);
+		dm_gpio_set_dir_flags(pin, GPIOD_IS_IN |
+					   GPIOD_ACTIVE_LOW);
 	else
 		dm_gpio_set_dir_flags(pin, GPIOD_IS_OUT |
 					   GPIOD_ACTIVE_LOW |
@@ -514,7 +515,7 @@ static void i2c_gpio_set_pin(struct gpio_desc *pin, int
 bit)
 
 static int i2c_gpio_get_pin(struct gpio_desc *pin)  {
-	return dm_gpio_get_value(pin);
+	return !dm_gpio_get_value(pin);
 }
 
 int i2c_deblock_gpio_loop(struct gpio_desc *sda_pin,
--

2.34.1

Signature · 3 lines
DENX Software Engineering GmbH,      Managing Director: Erika Unter
HRB 165235 Munich, Office: Kirchenstr.5, D-82194 Groebenzell, Germany
Phone: +49-8142-66989-52   Fax: +49-8142-66989-80   Email: [email protected]
#3 lore raw

Hello!

The patch doesn’t add new functionality to the code.
May be it makes code more readable. But in later case the patch description should be corrected and Fixes tag removed.

The flag GPIOD_ACTIVE_LOW affects return value dm_gpio_get_value(). And return value doesn’t depends on the DTS settings. It depends on the flag passed to dm_gpio_set_dir_flags(). Usually the code parses DTS and provide the correct flag to the dm_gpio_set_dir_flags().

So the patch adds flag GPIOD_ACTIVE_LOW to the dm_gpio_set_dir_flags() and as expected this negates the return value of dm_gpio_get_value(). So in order to keep the code correct the patch also fixes adds negotiation to dm_gpio_get_value().

Alexander.

Quoted 81 lines

20 марта 2023 г., в 10:44, Heiko Schocher <[email protected]> написал(а):

Hi!

Quoted 18 lines · Bough Chen
Quoted 18 lines

-----Original Message-----
From: Bough Chen <[email protected]>
Sent: 2023年2月10日 17:27
To: [email protected]; [email protected]; [email protected]
Cc: [email protected]; dl-uboot-imx <[email protected]>; [email protected]; Bough Chen <[email protected]>
Subject: [PATCH] i2c: correct I2C deblock logic

From: Haibo Chen <[email protected]>

Current code use dm_gpio_get_value() to get SDA and SCL value, and the value depends on the flag GPIOD_ACTIVE_LOW. When toggle SCL to wait slave release SDA, the SDA are config as GPIOD_IS_IN, and whether contain the GPIOD_ACTIVE_LOW depends on the DTS setting. Usually, for I2C GPIO, we use GPIOD_ACTIVE_LOW flag in DTS, so if the SDA is in low level, then dm_gpio_get_value() will return 1, current code logic will stop the SCL toggle wrongly, cause the I2C deblock not work as expect.

Quoted 8 lines
Quoted 4 lines

This patch force set the GPIOD_ACTIVE_LOW for both GPIOD_IS_IN and GPIOD_IS_OUT, and make the return value of i2c_gpio_get_pin() eaqual to the physical voltage logic level.

Hi,

Any comments for this patch, just a reminder in case you miss it.

Indeed, I missed this patch, sorry!
Your explanation sounds reasonable to me, but I wonder if nobody tapped into this problem... it would be good to have some test reports from others! Also added Simon, as he did a lot of work in this space.

Thanks!

bye,
Heiko

Quoted 35 lines

Best Regards
Haibo Chen

Quoted 30 lines
Fixes aa54192d4a87 ("dm: i2c: implement gpio-based I2C deblock")

---
drivers/i2c/i2c-uclass.c | 5 +++--

1 file changed, 3 insertions(+), 2 deletions(-)
drivers/i2c/i2c-uclass.c index renamed from drivers/i2c/i2c-uclass.c +0 -0

8d9a89ed89..4dc707c659 100644

drivers/i2c/i2c-uclass.c +3 -3
@@ -505,7 +505,8 @@ uint i2c_get_chip_addr_offset_mask(struct udevice
 *dev)  static void i2c_gpio_set_pin(struct gpio_desc *pin, int bit)  {
 	if (bit)
-		dm_gpio_set_dir_flags(pin, GPIOD_IS_IN);
+		dm_gpio_set_dir_flags(pin, GPIOD_IS_IN |
+					   GPIOD_ACTIVE_LOW);
 	else
 		dm_gpio_set_dir_flags(pin, GPIOD_IS_OUT |
 					   GPIOD_ACTIVE_LOW |
@@ -514,7 +515,7 @@ static void i2c_gpio_set_pin(struct gpio_desc *pin, int
 bit)
 
 static int i2c_gpio_get_pin(struct gpio_desc *pin)  {
-	return dm_gpio_get_value(pin);
+	return !dm_gpio_get_value(pin);
 }
 
 int i2c_deblock_gpio_loop(struct gpio_desc *sda_pin,
--

2.34.1

Signature · 3 lines
DENX Software Engineering GmbH,      Managing Director: Erika Unter
HRB 165235 Munich, Office: Kirchenstr.5, D-82194 Groebenzell, Germany
Phone: +49-8142-66989-52   Fax: +49-8142-66989-80   Email: [email protected]
author
#4 lore raw
Quoted 9 lines

-----Original Message-----
From: Alexander Kochetkov <[email protected]>
Sent: 2023年3月20日 16:03
To: [email protected]
Cc: Bough Chen <[email protected]>; [email protected]; [email protected]; dl-uboot-imx <[email protected]>; [email protected]; Simon Glass <[email protected]>
Subject: Re: [PATCH] i2c: correct I2C deblock logic

Quoted 5 lines

Hello!

The patch doesn’t add new functionality to the code.
May be it makes code more readable. But in later case the patch description should be corrected and Fixes tag removed.

Hi Alexander,

This patch not only make the code more readable, but also really fix a bug.
In our current code, we will call i2c_gpio_set_pin(scl_pin, 1) and then i2c_gpio_set_pin(scl_pin, 0).
After the i2c_gpio_set_pin(scl_pin, 0), the flags contain GPIOD_ACTIVE_LOW.
Though for SDA, it will first call i2c_gpio_set_pin(sda_pin, 0) then i2c_gpio_set_pin(sda_pin, 1), but
i2c_gpio_set_pin-> dm_gpio_set_dir_flags-> dm_gpio_clrset_flags, it only clear GPIOD_MASK_DIR, this has no impact with GPIOD_ACTIVE_LOW. So this GPIOD_ACTIVE_LOW keep in flags. Then call i2c_gpio_get_pin by using dm_gpio_get_value(pin) will get the wrong data, let the function i2c_deblock_gpio_loop always return -EREMOTEIO

Best Regards
Haibo Chen

Quoted 100 lines

The flag GPIOD_ACTIVE_LOW affects return value dm_gpio_get_value(). And return value doesn’t depends on the DTS settings. It depends on the flag passed to dm_gpio_set_dir_flags(). Usually the code parses DTS and provide the correct flag to the dm_gpio_set_dir_flags().

So the patch adds flag GPIOD_ACTIVE_LOW to the dm_gpio_set_dir_flags() and as expected this negates the return value of dm_gpio_get_value(). So in order to keep the code correct the patch also fixes adds negotiation to dm_gpio_get_value().

Alexander.

Quoted 85 lines

20 марта 2023 г., в 10:44, Heiko Schocher <[email protected]> написал(а):

Hi!

Quoted 19 lines · Bough Chen
Quoted 19 lines

-----Original Message-----
From: Bough Chen <[email protected]>
Sent: 2023年2月10日 17:27
To: [email protected]; [email protected]; [email protected]
Cc: [email protected]; dl-uboot-imx <[email protected]>; [email protected]; Bough Chen <[email protected]>
Subject: [PATCH] i2c: correct I2C deblock logic

From: Haibo Chen <[email protected]>

Current code use dm_gpio_get_value() to get SDA and SCL value, and the value depends on the flag GPIOD_ACTIVE_LOW. When toggle SCL to wait slave release SDA, the SDA are config as GPIOD_IS_IN, and whether contain the GPIOD_ACTIVE_LOW depends on the DTS setting. Usually, for I2C GPIO, we use GPIOD_ACTIVE_LOW flag in DTS, so if the SDA is in low level, then
dm_gpio_get_value() will return 1, current code logic will stop the SCL toggle wrongly, cause the I2C deblock not work as expect.

Quoted 8 lines
Quoted 4 lines

This patch force set the GPIOD_ACTIVE_LOW for both GPIOD_IS_IN and GPIOD_IS_OUT, and make the return value of i2c_gpio_get_pin() eaqual to the physical voltage logic level.

Hi,

Any comments for this patch, just a reminder in case you miss it.

Indeed, I missed this patch, sorry!
Your explanation sounds reasonable to me, but I wonder if nobody tapped into this problem... it would be good to have some test reports from others! Also added Simon, as he did a lot of work in this space.

Thanks!

bye,
Heiko

Quoted 29 lines

Best Regards
Haibo Chen

Quoted 25 lines
Fixes aa54192d4a87 ("dm: i2c: implement gpio-based I2C deblock")

---
drivers/i2c/i2c-uclass.c | 5 +++--

1 file changed, 3 insertions(+), 2 deletions(-)
drivers/i2c/i2c-uclass.c +0 -0

index
8d9a89ed89..4dc707c659 100644

drivers/i2c/i2c-uclass.c +0 -0
@@ -505,7 +505,8 @@ uint i2c_get_chip_addr_offset_mask(struct

udevice
*dev) static void i2c_gpio_set_pin(struct gpio_desc *pin, int bit) {
if (bit)

drivers/i2c/i2c-uclass.c +2 -1
-		dm_gpio_set_dir_flags(pin, GPIOD_IS_IN);
+		dm_gpio_set_dir_flags(pin, GPIOD_IS_IN |
+					   GPIOD_ACTIVE_LOW);

else
dm_gpio_set_dir_flags(pin, GPIOD_IS_OUT |
GPIOD_ACTIVE_LOW |

drivers/i2c/i2c-uclass.c +0 -0
@@ -514,7 +515,7 @@ static void i2c_gpio_set_pin(struct gpio_desc
 *pin, int
 bit)
 
Quoted 9 lines
Quoted 8 lines
drivers/i2c/i2c-uclass.c +1 -1
 static int i2c_gpio_get_pin(struct gpio_desc *pin)  {
-	return dm_gpio_get_value(pin);
+	return !dm_gpio_get_value(pin);
 }
 

int i2c_deblock_gpio_loop(struct gpio_desc *sda_pin, --
2.34.1

--
DENX Software Engineering GmbH, Managing Director: Erika Unter
HRB 165235 Munich, Office: Kirchenstr.5, D-82194 Groebenzell, Germany
Phone: +49-8142-66989-52 Fax: +49-8142-66989-80 Email: [email protected]

#5 lore raw

Hi Haibo.

Nice catch! Agree with you patch. But may be fix problem in general?
I suggest to add GPIOD_ACTIVE_LOW to the GPIOD_MASK_DIR mask to fix problems 1, 2, 4. Not sure if that logically correct. What do you think?

I grepped the code for ‘dm_gpio_set_dir_flags’ and found another places affected by the bug.

1. u-boot/drivers/spi/mxc_spi.c:
ret = dm_gpio_set_dir_flags(&mxcs->cs_gpios[i],

                                              GPIOD_IS_OUT | GPIOD_ACTIVE_LOW);

Here GPIOD_ACTIVE_LOW is passed to dm_gpio_set_dir_flags() and filtered out by dm_gpio_set_dir_flags(). GPIOD_ACTIVE_LOW can be added to GPIOD_MASK_DIR mask to fix that.

2. u-boot/drivers/spi/mvebu_a3700_spi.c:
ret = dm_gpio_set_dir_flags(&plat->cs_gpios[i],

                                             GPIOD_IS_OUT | GPIOD_ACTIVE_LOW);

Same as 1.

3. u-boot/drivers/phy/allwinner/phy-sun4i-usb.c:
ret = dm_gpio_set_dir_flags(&phy->gpio_id_det,

                                             GPIOD_IS_IN | GPIOD_PULL_UP);

Here GPIOD_PULL_UP is passed to dm_gpio_set_dir_flags() and filtered out by dm_gpio_set_dir_flags(). Looks, like we have to introduce dm_gpio_set_pull_flags() and use it together with dm_gpio_set_dir_flags().

4. u-boot/drivers/i2c/i2c-uclass.c
dm_gpio_set_dir_flags(pin, GPIOD_IS_OUT |
GPIOD_ACTIVE_LOW | GPIOD_IS_OUT_ACTIVE);
Same as 1. Your patch cover that bug.

Quoted 130 lines

21 марта 2023 г., в 11:37, Bough Chen <[email protected]> написал(а):

Quoted 9 lines

-----Original Message-----
From: Alexander Kochetkov <[email protected]>
Sent: 2023年3月20日 16:03
To: [email protected]
Cc: Bough Chen <[email protected]>; [email protected]; [email protected]; dl-uboot-imx <[email protected]>; [email protected]; Simon Glass <[email protected]>
Subject: Re: [PATCH] i2c: correct I2C deblock logic

Quoted 5 lines

Hello!

The patch doesn’t add new functionality to the code.
May be it makes code more readable. But in later case the patch description should be corrected and Fixes tag removed.

Hi Alexander,

This patch not only make the code more readable, but also really fix a bug.
In our current code, we will call i2c_gpio_set_pin(scl_pin, 1) and then i2c_gpio_set_pin(scl_pin, 0).
After the i2c_gpio_set_pin(scl_pin, 0), the flags contain GPIOD_ACTIVE_LOW.
Though for SDA, it will first call i2c_gpio_set_pin(sda_pin, 0) then i2c_gpio_set_pin(sda_pin, 1), but
i2c_gpio_set_pin-> dm_gpio_set_dir_flags-> dm_gpio_clrset_flags, it only clear GPIOD_MASK_DIR, this has no impact with GPIOD_ACTIVE_LOW. So this GPIOD_ACTIVE_LOW keep in flags. Then call i2c_gpio_get_pin by using dm_gpio_get_value(pin) will get the wrong data, let the function i2c_deblock_gpio_loop always return -EREMOTEIO

Best Regards
Haibo Chen

Quoted 100 lines

The flag GPIOD_ACTIVE_LOW affects return value dm_gpio_get_value(). And return value doesn’t depends on the DTS settings. It depends on the flag passed to dm_gpio_set_dir_flags(). Usually the code parses DTS and provide the correct flag to the dm_gpio_set_dir_flags().

So the patch adds flag GPIOD_ACTIVE_LOW to the dm_gpio_set_dir_flags() and as expected this negates the return value of dm_gpio_get_value(). So in order to keep the code correct the patch also fixes adds negotiation to dm_gpio_get_value().

Alexander.

Quoted 85 lines

20 марта 2023 г., в 10:44, Heiko Schocher <[email protected]> написал(а):

Hi!

Quoted 19 lines · Bough Chen
Quoted 19 lines

-----Original Message-----
From: Bough Chen <[email protected]>
Sent: 2023年2月10日 17:27
To: [email protected]; [email protected]; [email protected]
Cc: [email protected]; dl-uboot-imx <[email protected]>; [email protected]; Bough Chen <[email protected]>
Subject: [PATCH] i2c: correct I2C deblock logic

From: Haibo Chen <[email protected]>

Current code use dm_gpio_get_value() to get SDA and SCL value, and the value depends on the flag GPIOD_ACTIVE_LOW. When toggle SCL to wait slave release SDA, the SDA are config as GPIOD_IS_IN, and whether contain the GPIOD_ACTIVE_LOW depends on the DTS setting. Usually, for I2C GPIO, we use GPIOD_ACTIVE_LOW flag in DTS, so if the SDA is in low level, then
dm_gpio_get_value() will return 1, current code logic will stop the SCL toggle wrongly, cause the I2C deblock not work as expect.

Quoted 8 lines
Quoted 4 lines

This patch force set the GPIOD_ACTIVE_LOW for both GPIOD_IS_IN and GPIOD_IS_OUT, and make the return value of i2c_gpio_get_pin() eaqual to the physical voltage logic level.

Hi,

Any comments for this patch, just a reminder in case you miss it.

Indeed, I missed this patch, sorry!
Your explanation sounds reasonable to me, but I wonder if nobody tapped into this problem... it would be good to have some test reports from others! Also added Simon, as he did a lot of work in this space.

Thanks!

bye,
Heiko

Quoted 29 lines

Best Regards
Haibo Chen

Quoted 25 lines
Fixes aa54192d4a87 ("dm: i2c: implement gpio-based I2C deblock")

---
drivers/i2c/i2c-uclass.c | 5 +++--

1 file changed, 3 insertions(+), 2 deletions(-)
drivers/i2c/i2c-uclass.c +0 -0

index
8d9a89ed89..4dc707c659 100644

drivers/i2c/i2c-uclass.c +0 -0
@@ -505,7 +505,8 @@ uint i2c_get_chip_addr_offset_mask(struct

udevice
*dev) static void i2c_gpio_set_pin(struct gpio_desc *pin, int bit) { if (bit)
- dm_gpio_set_dir_flags(pin, GPIOD_IS_IN);
+ dm_gpio_set_dir_flags(pin, GPIOD_IS_IN |
+ GPIOD_ACTIVE_LOW);
else
dm_gpio_set_dir_flags(pin, GPIOD_IS_OUT |
GPIOD_ACTIVE_LOW |

drivers/i2c/i2c-uclass.c +0 -0
@@ -514,7 +515,7 @@ static void i2c_gpio_set_pin(struct gpio_desc
 *pin, int
 bit)
 
Quoted 9 lines
Quoted 8 lines
drivers/i2c/i2c-uclass.c +1 -1
 static int i2c_gpio_get_pin(struct gpio_desc *pin)  {
- return dm_gpio_get_value(pin);
+ return !dm_gpio_get_value(pin);
 }
 

int i2c_deblock_gpio_loop(struct gpio_desc *sda_pin, --
2.34.1

--
DENX Software Engineering GmbH, Managing Director: Erika Unter
HRB 165235 Munich, Office: Kirchenstr.5, D-82194 Groebenzell, Germany
Phone: +49-8142-66989-52 Fax: +49-8142-66989-80 Email: [email protected]

author
#6 lore raw
Quoted 9 lines

-----Original Message-----
From: Alexander Kochetkov <[email protected]>
Sent: 2023年3月21日 17:50
To: Bough Chen <[email protected]>
Cc: [email protected]; [email protected]; [email protected]; dl-uboot-imx <[email protected]>; [email protected]; Simon Glass <[email protected]>
Subject: Re: [PATCH] i2c: correct I2C deblock logic

Quoted 6 lines

Hi Haibo.

Nice catch! Agree with you patch. But may be fix problem in general? I suggest to add GPIOD_ACTIVE_LOW to the GPIOD_MASK_DIR mask to fix problems 1, 2, 4.
Not sure if that logically correct. What do you think?

Hi Alexander

I will have a try, and test on my side.

Best Regards
Haibo Chen

Quoted 175 lines

I grepped the code for ‘dm_gpio_set_dir_flags’ and found another places affected by the bug.

1. u-boot/drivers/spi/mxc_spi.c:
ret = dm_gpio_set_dir_flags(&mxcs->cs_gpios[i],
GPIOD_IS_OUT |
GPIOD_ACTIVE_LOW);

Here GPIOD_ACTIVE_LOW is passed to dm_gpio_set_dir_flags() and filtered out by dm_gpio_set_dir_flags().
GPIOD_ACTIVE_LOW can be added to GPIOD_MASK_DIR mask to fix that.

2. u-boot/drivers/spi/mvebu_a3700_spi.c:
ret = dm_gpio_set_dir_flags(&plat->cs_gpios[i],
GPIOD_IS_OUT |
GPIOD_ACTIVE_LOW); Same as 1.

3. u-boot/drivers/phy/allwinner/phy-sun4i-usb.c:
ret = dm_gpio_set_dir_flags(&phy->gpio_id_det,
GPIOD_IS_IN |
GPIOD_PULL_UP);

Here GPIOD_PULL_UP is passed to dm_gpio_set_dir_flags() and filtered out by dm_gpio_set_dir_flags().
Looks, like we have to introduce dm_gpio_set_pull_flags() and use it together with dm_gpio_set_dir_flags().

4. u-boot/drivers/i2c/i2c-uclass.c
dm_gpio_set_dir_flags(pin, GPIOD_IS_OUT |

GPIOD_ACTIVE_LOW |

GPIOD_IS_OUT_ACTIVE); Same as 1. Your patch cover that bug.

Quoted 1 line

21 марта 2023 г., в 11:37, Bough Chen <[email protected]>

написал(а):

Quoted 12 lines
Quoted 12 lines

-----Original Message-----
From: Alexander Kochetkov <[email protected]>
Sent: 2023年3月20日 16:03
To: [email protected]
Cc: Bough Chen <[email protected]>; [email protected]; [email protected]; dl-uboot-imx <[email protected]>; [email protected]; Simon Glass <[email protected]>
Subject: Re: [PATCH] i2c: correct I2C deblock logic

Hello!

Quoted 8 lines
Quoted 3 lines

The patch doesn’t add new functionality to the code. May be it makes code more readable. But in later case the patch description should be corrected and Fixes tag removed.

Hi Alexander,

This patch not only make the code more readable, but also really fix a bug. In our current code, we will call i2c_gpio_set_pin(scl_pin, 1) and then

i2c_gpio_set_pin(scl_pin, 0).

Quoted 3 lines

After the i2c_gpio_set_pin(scl_pin, 0), the flags contain GPIOD_ACTIVE_LOW. Though for SDA, it will first call i2c_gpio_set_pin(sda_pin, 0) then i2c_gpio_set_pin(sda_pin, 1), but i2c_gpio_set_pin-> dm_gpio_set_dir_flags->

dm_gpio_clrset_flags, it only clear GPIOD_MASK_DIR, this has no impact with GPIOD_ACTIVE_LOW. So this GPIOD_ACTIVE_LOW keep in flags.

Quoted 4 lines

Then call i2c_gpio_get_pin by using dm_gpio_get_value(pin) will get the wrong data, let the function i2c_deblock_gpio_loop always return -EREMOTEIO

Quoted 103 lines

Best Regards
Haibo Chen

Quoted 100 lines

The flag GPIOD_ACTIVE_LOW affects return value dm_gpio_get_value(). And return value doesn’t depends on the DTS settings. It depends on the flag passed to dm_gpio_set_dir_flags(). Usually the code parses DTS and provide the correct flag to the dm_gpio_set_dir_flags().

So the patch adds flag GPIOD_ACTIVE_LOW to the dm_gpio_set_dir_flags() and as expected this negates the return value of dm_gpio_get_value(). So in order to keep the code correct the patch also fixes adds negotiation to dm_gpio_get_value().

Alexander.

Quoted 85 lines

20 марта 2023 г., в 10:44, Heiko Schocher <[email protected]> написал(а):

Hi!

Quoted 19 lines · Bough Chen
Quoted 19 lines

-----Original Message-----
From: Bough Chen <[email protected]>
Sent: 2023年2月10日 17:27
To: [email protected]; [email protected]; [email protected]
Cc: [email protected]; dl-uboot-imx <[email protected]>; [email protected]; Bough Chen <[email protected]>
Subject: [PATCH] i2c: correct I2C deblock logic

From: Haibo Chen <[email protected]>

Current code use dm_gpio_get_value() to get SDA and SCL value, and the value depends on the flag GPIOD_ACTIVE_LOW. When toggle SCL to wait slave release SDA, the SDA are config as GPIOD_IS_IN, and whether contain the GPIOD_ACTIVE_LOW depends on the DTS setting. Usually, for I2C GPIO, we use GPIOD_ACTIVE_LOW flag in DTS, so if the SDA is in low level, then
dm_gpio_get_value() will return 1, current code logic will stop the SCL toggle wrongly, cause the I2C deblock not work as expect.

Quoted 8 lines
Quoted 4 lines

This patch force set the GPIOD_ACTIVE_LOW for both GPIOD_IS_IN and GPIOD_IS_OUT, and make the return value of i2c_gpio_get_pin() eaqual to the physical voltage logic level.

Hi,

Any comments for this patch, just a reminder in case you miss it.

Indeed, I missed this patch, sorry!
Your explanation sounds reasonable to me, but I wonder if nobody tapped into this problem... it would be good to have some test reports from others! Also added Simon, as he did a lot of work in this space.

Thanks!

bye,
Heiko

Quoted 29 lines

Best Regards
Haibo Chen

Quoted 25 lines
Fixes aa54192d4a87 ("dm: i2c: implement gpio-based I2C deblock")

---
drivers/i2c/i2c-uclass.c | 5 +++--

1 file changed, 3 insertions(+), 2 deletions(-)
drivers/i2c/i2c-uclass.c +0 -0

index
8d9a89ed89..4dc707c659 100644

drivers/i2c/i2c-uclass.c +0 -0
@@ -505,7 +505,8 @@ uint i2c_get_chip_addr_offset_mask(struct

udevice
*dev) static void i2c_gpio_set_pin(struct gpio_desc *pin, int bit) { if (bit)
- dm_gpio_set_dir_flags(pin, GPIOD_IS_IN);
+ dm_gpio_set_dir_flags(pin, GPIOD_IS_IN |
+ GPIOD_ACTIVE_LOW);
else
dm_gpio_set_dir_flags(pin, GPIOD_IS_OUT |
GPIOD_ACTIVE_LOW |

drivers/i2c/i2c-uclass.c +0 -0
@@ -514,7 +515,7 @@ static void i2c_gpio_set_pin(struct gpio_desc
 *pin, int
 bit)
 
Quoted 9 lines
Quoted 8 lines
drivers/i2c/i2c-uclass.c +1 -1
 static int i2c_gpio_get_pin(struct gpio_desc *pin)  {
- return dm_gpio_get_value(pin);
+ return !dm_gpio_get_value(pin);
 }
 

int i2c_deblock_gpio_loop(struct gpio_desc *sda_pin, --
2.34.1

--
DENX Software Engineering GmbH, Managing Director: Erika Unter
HRB 165235 Munich, Office: Kirchenstr.5, D-82194 Groebenzell, Germany
Phone: +49-8142-66989-52 Fax: +49-8142-66989-80 Email:

[email protected]

Quoted 1 line
#7 lore raw
In reply to [email protected]

Hello Haibo Chen!

Setting GPIOD_ACTIVE_LOW has no effect. It filtered out by dm_gpio_set_dir_flags().

Quoted 6 lines

if (bit)
- dm_gpio_set_dir_flags(pin, GPIOD_IS_IN);
+ dm_gpio_set_dir_flags(pin, GPIOD_IS_IN |
+ GPIOD_ACTIVE_LOW);

Here in original code GPIOD_ACTIVE_LOW has not effect.

else
dm_gpio_set_dir_flags(pin, GPIOD_IS_OUT |
GPIOD_ACTIVE_LOW | GPIOD_IS_OUT_ACTIVE);

This code actually fix problem with your DTS-settings:

Quoted 1 line

+ return !dm_gpio_get_value(pin);

Can you debug and check using oscilloscope the code like this:

static void i2c_gpio_set_pin(struct gpio_desc *pin, int bit)

{
    ulong flags;
    ulong active;

    if (bit) {
        dm_gpio_set_dir_flags(pin, GPIOD_IS_IN);
    } else {
        dm_gpio_get_flags(pin, &flags);
        active = (flags & GPIOD_ACTIVE_LOW) ? GPIOD_IS_OUT_ACTIVE : 0;
        dm_gpio_set_dir_flags(pin, GPIOD_IS_OUT | active);
    }
}

static int i2c_gpio_get_pin(struct gpio_desc *pin)

{
     ulong flags;
     int value;

     value = dm_gpio_get_value(pin);
     return (flags & GPIOD_ACTIVE_LOW) ? !value : value;
}
Quoted 48 lines

10 февр. 2023 г., в 12:27, [email protected] написал(а):

From: Haibo Chen <[email protected]>

Current code use dm_gpio_get_value() to get SDA and SCL value, and the value depends on the flag GPIOD_ACTIVE_LOW. When toggle SCL to wait slave release SDA, the SDA are config as GPIOD_IS_IN, and whether contain the GPIOD_ACTIVE_LOW depends on the DTS setting. Usually, for I2C GPIO, we use GPIOD_ACTIVE_LOW flag in DTS, so if the SDA is in low level, then dm_gpio_get_value() will return 1, current code logic will stop the SCL toggle wrongly, cause the I2C deblock not work as expect.

This patch force set the GPIOD_ACTIVE_LOW for both GPIOD_IS_IN and GPIOD_IS_OUT, and make the return value of i2c_gpio_get_pin() eaqual to the physical voltage logic level.

Fixes aa54192d4a87 ("dm: i2c: implement gpio-based I2C deblock")

---
drivers/i2c/i2c-uclass.c | 5 +++--

1 file changed, 3 insertions(+), 2 deletions(-)
drivers/i2c/i2c-uclass.c +2 -1
index 8d9a89ed89..4dc707c659 100644
@@ -505,7 +505,8 @@ uint i2c_get_chip_addr_offset_mask(struct udevice *dev)
 static void i2c_gpio_set_pin(struct gpio_desc *pin, int bit)
 {
 if (bit)
- dm_gpio_set_dir_flags(pin, GPIOD_IS_IN);
+ dm_gpio_set_dir_flags(pin, GPIOD_IS_IN |
+   GPIOD_ACTIVE_LOW);

else
dm_gpio_set_dir_flags(pin, GPIOD_IS_OUT |
GPIOD_ACTIVE_LOW |

drivers/i2c/i2c-uclass.c +1 -1
@@ -514,7 +515,7 @@ static void i2c_gpio_set_pin(struct gpio_desc *pin, int bit)
 
 static int i2c_gpio_get_pin(struct gpio_desc *pin)
 {
- return dm_gpio_get_value(pin);
+ return !dm_gpio_get_value(pin);
 }
 
 int i2c_deblock_gpio_loop(struct gpio_desc *sda_pin,
Signature · 1 line
2.34.1
#8 lore raw

Or even simpler. Like your original patch. If we take into accout Patrik’s comment from another message:

Quoted 1 line

but if I assume that GPIO_ACTIVE_HIGH is NOT activated in DT

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_IS_OUT_ACTIVE);

}
}

static int i2c_gpio_get_pin(struct gpio_desc *pin)

{
    return !dm_gpio_get_value(pin);
}
Quoted 99 lines

23 марта 2023 г., в 11:43, Alexander Kochetkov <[email protected]> написал(а):

Hello Haibo Chen!

Setting GPIOD_ACTIVE_LOW has no effect. It filtered out by dm_gpio_set_dir_flags().

Quoted 6 lines

if (bit)
- dm_gpio_set_dir_flags(pin, GPIOD_IS_IN);
+ dm_gpio_set_dir_flags(pin, GPIOD_IS_IN |
+ GPIOD_ACTIVE_LOW);

Here in original code GPIOD_ACTIVE_LOW has not effect.

else
dm_gpio_set_dir_flags(pin, GPIOD_IS_OUT |

                                               GPIOD_ACTIVE_LOW |
                                               GPIOD_IS_OUT_ACTIVE);

This code actually fix problem with your DTS-settings:

Quoted 1 line

+ return !dm_gpio_get_value(pin);

Can you debug and check using oscilloscope the code like this:

static void i2c_gpio_set_pin(struct gpio_desc *pin, int bit)

{
   ulong flags;
   ulong active;

   if (bit) {
       dm_gpio_set_dir_flags(pin, GPIOD_IS_IN);
   } else {
       dm_gpio_get_flags(pin, &flags);
       active = (flags & GPIOD_ACTIVE_LOW) ? GPIOD_IS_OUT_ACTIVE : 0;
       dm_gpio_set_dir_flags(pin, GPIOD_IS_OUT | active);
   }
}

static int i2c_gpio_get_pin(struct gpio_desc *pin)

{
    ulong flags;
    int value;

    value = dm_gpio_get_value(pin);
    return (flags & GPIOD_ACTIVE_LOW) ? !value : value;
}
Quoted 48 lines

10 февр. 2023 г., в 12:27, [email protected] написал(а):

From: Haibo Chen <[email protected]>

Current code use dm_gpio_get_value() to get SDA and SCL value, and the value depends on the flag GPIOD_ACTIVE_LOW. When toggle SCL to wait slave release SDA, the SDA are config as GPIOD_IS_IN, and whether contain the GPIOD_ACTIVE_LOW depends on the DTS setting. Usually, for I2C GPIO, we use GPIOD_ACTIVE_LOW flag in DTS, so if the SDA is in low level, then dm_gpio_get_value() will return 1, current code logic will stop the SCL toggle wrongly, cause the I2C deblock not work as expect.

This patch force set the GPIOD_ACTIVE_LOW for both GPIOD_IS_IN and GPIOD_IS_OUT, and make the return value of i2c_gpio_get_pin() eaqual to the physical voltage logic level.

Fixes aa54192d4a87 ("dm: i2c: implement gpio-based I2C deblock")

---
drivers/i2c/i2c-uclass.c | 5 +++--

1 file changed, 3 insertions(+), 2 deletions(-)
drivers/i2c/i2c-uclass.c +2 -1
index 8d9a89ed89..4dc707c659 100644
@@ -505,7 +505,8 @@ uint i2c_get_chip_addr_offset_mask(struct udevice *dev)
 static void i2c_gpio_set_pin(struct gpio_desc *pin, int bit)
 {
 if (bit)
- dm_gpio_set_dir_flags(pin, GPIOD_IS_IN);
+ dm_gpio_set_dir_flags(pin, GPIOD_IS_IN |
+   GPIOD_ACTIVE_LOW);

else
dm_gpio_set_dir_flags(pin, GPIOD_IS_OUT |
GPIOD_ACTIVE_LOW |

drivers/i2c/i2c-uclass.c +1 -1
@@ -514,7 +515,7 @@ static void i2c_gpio_set_pin(struct gpio_desc *pin, int bit)
 
 static int i2c_gpio_get_pin(struct gpio_desc *pin)
 {
- return dm_gpio_get_value(pin);
+ return !dm_gpio_get_value(pin);
 }
 
 int i2c_deblock_gpio_loop(struct gpio_desc *sda_pin,
Signature · 1 line
2.34.1
author
#9 lore raw
Quoted 21 lines

-----Original Message-----
From: Alexander Kochetkov <[email protected]>
Sent: 2023年3月23日 17:34
To: Bough Chen <[email protected]>
Cc: [email protected]; [email protected]; [email protected]; dl-uboot-imx <[email protected]>; [email protected]
Subject: Re: [PATCH] i2c: correct I2C deblock logic

Or even simpler. Like your original patch. If we take into accout Patrik’s comment from another message:

Quoted 1 line

but if I assume that GPIO_ACTIVE_HIGH is NOT activated in DT

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_IS_OUT_ACTIVE);
   }
}
Quoted 3 lines
static int i2c_gpio_get_pin(struct gpio_desc *pin) {
    return !dm_gpio_get_value(pin);
}

If use this code, must make sure dts use GPIO_ACTIVE_LOW, otherwise
dm_gpio_set_dir_flags(pin, GPIOD_IS_OUT | GPIOD_IS_OUT_ACTIVE) will config gpio output high, this is not what we want.

And here use !dm_gpio_get_value(pin), this already hit use GPIO_ACTIVE_LOW, why not directly use GPIOD_ACTIVE_LOW when call dm_gpio_set_dir_flags, this make code more readable, and make sure the code logic can work and do not depends on dts setting.

Best Regards
Haibo Chen

Quoted 102 lines
Quoted 3 lines

23 марта 2023 г., в 11:43, Alexander Kochetkov <[email protected]>

написал(а):

Quoted 4 lines

Hello Haibo Chen!

Setting GPIOD_ACTIVE_LOW has no effect. It filtered out by

dm_gpio_set_dir_flags().

Quoted 7 lines
Quoted 6 lines

if (bit)
- dm_gpio_set_dir_flags(pin, GPIOD_IS_IN);
+ dm_gpio_set_dir_flags(pin, GPIOD_IS_IN |
+ GPIOD_ACTIVE_LOW);

Quoted 5 lines

Here in original code GPIOD_ACTIVE_LOW has not effect.

else
dm_gpio_set_dir_flags(pin, GPIOD_IS_OUT |

GPIOD_ACTIVE_LOW |

Quoted 1 line

GPIOD_IS_OUT_ACTIVE);

Quoted 8 lines

This code actually fix problem with your DTS-settings:

Quoted 1 line

+ return !dm_gpio_get_value(pin);

Can you debug and check using oscilloscope the code like this:

Quoted 9 lines
static void i2c_gpio_set_pin(struct gpio_desc *pin, int bit) {
   ulong flags;
   ulong active;

   if (bit) {
       dm_gpio_set_dir_flags(pin, GPIOD_IS_IN);
   } else {
       dm_gpio_get_flags(pin, &flags);
       active = (flags & GPIOD_ACTIVE_LOW) ? GPIOD_IS_OUT_ACTIVE :

0;

Quoted 60 lines
       dm_gpio_set_dir_flags(pin, GPIOD_IS_OUT | active);

}
}

static int i2c_gpio_get_pin(struct gpio_desc *pin) {
    ulong flags;
    int value;

    value = dm_gpio_get_value(pin);
    return (flags & GPIOD_ACTIVE_LOW) ? !value : value; }
Quoted 47 lines

10 февр. 2023 г., в 12:27, [email protected] написал(а):

From: Haibo Chen <[email protected]>

Current code use dm_gpio_get_value() to get SDA and SCL value, and the value depends on the flag GPIOD_ACTIVE_LOW. When toggle SCL to wait slave release SDA, the SDA are config as GPIOD_IS_IN, and whether contain the GPIOD_ACTIVE_LOW depends on the DTS setting. Usually, for I2C GPIO, we use GPIOD_ACTIVE_LOW flag in DTS, so if the SDA is in low level, then
dm_gpio_get_value() will return 1, current code logic will stop the SCL toggle wrongly, cause the I2C deblock not work as expect.

This patch force set the GPIOD_ACTIVE_LOW for both GPIOD_IS_IN and GPIOD_IS_OUT, and make the return value of i2c_gpio_get_pin() eaqual to the physical voltage logic level.

Fixes aa54192d4a87 ("dm: i2c: implement gpio-based I2C deblock")

---
drivers/i2c/i2c-uclass.c | 5 +++--

1 file changed, 3 insertions(+), 2 deletions(-)
drivers/i2c/i2c-uclass.c +2 -1
index 8d9a89ed89..4dc707c659 100644
@@ -505,7 +505,8 @@ uint i2c_get_chip_addr_offset_mask(struct udevice
 *dev) static void i2c_gpio_set_pin(struct gpio_desc *pin, int bit) {
 if (bit)
- dm_gpio_set_dir_flags(pin, GPIOD_IS_IN);
+ dm_gpio_set_dir_flags(pin, GPIOD_IS_IN |
+   GPIOD_ACTIVE_LOW);

else
dm_gpio_set_dir_flags(pin, GPIOD_IS_OUT | GPIOD_ACTIVE_LOW | @@ -514,7 +515,7 @@ static void i2c_gpio_set_pin(struct gpio_desc *pin, int bit)

static int i2c_gpio_get_pin(struct gpio_desc *pin) {
- return dm_gpio_get_value(pin);
+ return !dm_gpio_get_value(pin);
}

int i2c_deblock_gpio_loop(struct gpio_desc *sda_pin, --
2.34.1

#10 lore raw
Quoted 2 lines

If use this code, must make sure dts use GPIO_ACTIVE_LOW, otherwise
dm_gpio_set_dir_flags(pin, GPIOD_IS_OUT | GPIOD_IS_OUT_ACTIVE) will config gpio output high, this is not what we want.

Patrik told that if we don’t have GPIO_ACTIVE_LOW in the DTS, this is the case where hardware has some sort of signal inversion inside. May be there is no such hardware now.
When we set gpio output active, it became high inside hardware, hardware set i2c line low.
If you think that previous version of my today’s patch is more correct, try it.

Quoted 4 lines

And here use !dm_gpio_get_value(pin), this already hit use GPIO_ACTIVE_LOW, why not directly use GPIOD_ACTIVE_LOW when call dm_gpio_set_dir_flags, this make code more readable, and make sure the code logic can work and do not depends on dts setting.

Patric told, that we must not use GPIOD_ACTIVE_LOW in client code. So you must drop it. From any patch you send. GPIOD_ACTIVE_LOW is not applied to gpio flags when you call dm_gpio_set_dir_flags(). It is configured from DTS. You think it is applied, but actually not. It’s your finding. Here your words:

Quoted 138 lines

i2c_gpio_set_pin-> dm_gpio_set_dir_flags-> dm_gpio_clrset_flags, it only clear GPIOD_MASK_DIR, this has no impact with GPIOD_ACTIVE_LOW. So this GPIOD_ACTIVE_LOW keep in flags.

23 марта 2023 г., в 13:41, Bough Chen <[email protected]> написал(а):

Quoted 18 lines

-----Original Message-----
From: Alexander Kochetkov <[email protected] <mailto:[email protected]>>
Sent: 2023年3月23日 17:34
To: Bough Chen <[email protected] <mailto:[email protected]>>
Cc: [email protected] <mailto:[email protected]>; [email protected] <mailto:[email protected]>; [email protected] <mailto:[email protected]>; dl-uboot-imx <[email protected] <mailto:[email protected]>>; [email protected] <mailto:[email protected]>
Subject: Re: [PATCH] i2c: correct I2C deblock logic

Or even simpler. Like your original patch. If we take into accout Patrik’s comment from another message:

Quoted 1 line

but if I assume that GPIO_ACTIVE_HIGH is NOT activated in DT

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_IS_OUT_ACTIVE);
Quoted 6 lines

}
}

static int i2c_gpio_get_pin(struct gpio_desc *pin) {
   return !dm_gpio_get_value(pin);
}

And here use !dm_gpio_get_value(pin), this already hit use GPIO_ACTIVE_LOW, why not directly use GPIOD_ACTIVE_LOW when call dm_gpio_set_dir_flags, this make code more readable, and make sure the code logic can work and do not depends on dts setting.

Best Regards
Haibo Chen

Quoted 100 lines
Quoted 3 lines

23 марта 2023 г., в 11:43, Alexander Kochetkov <[email protected]>

написал(а):

Quoted 4 lines

Hello Haibo Chen!

Setting GPIOD_ACTIVE_LOW has no effect. It filtered out by

dm_gpio_set_dir_flags().

Quoted 7 lines
Quoted 6 lines

if (bit)
- dm_gpio_set_dir_flags(pin, GPIOD_IS_IN);
+ dm_gpio_set_dir_flags(pin, GPIOD_IS_IN |
+ GPIOD_ACTIVE_LOW);

Quoted 5 lines

Here in original code GPIOD_ACTIVE_LOW has not effect.

else
dm_gpio_set_dir_flags(pin, GPIOD_IS_OUT |

GPIOD_ACTIVE_LOW |

Quoted 1 line

GPIOD_IS_OUT_ACTIVE);

Quoted 12 lines

This code actually fix problem with your DTS-settings:

Quoted 1 line

+ return !dm_gpio_get_value(pin);

Can you debug and check using oscilloscope the code like this:

static void i2c_gpio_set_pin(struct gpio_desc *pin, int bit) {
ulong flags;
ulong active;

Quoted 5 lines
  if (bit) {
      dm_gpio_set_dir_flags(pin, GPIOD_IS_IN);
  } else {
      dm_gpio_get_flags(pin, &flags);
      active = (flags & GPIOD_ACTIVE_LOW) ? GPIOD_IS_OUT_ACTIVE :

0;

Quoted 58 lines
      dm_gpio_set_dir_flags(pin, GPIOD_IS_OUT | active);

}
}

static int i2c_gpio_get_pin(struct gpio_desc *pin) {
   ulong flags;
   int value;

   value = dm_gpio_get_value(pin);
   return (flags & GPIOD_ACTIVE_LOW) ? !value : value; }
Quoted 46 lines

10 февр. 2023 г., в 12:27, [email protected] написал(а):

From: Haibo Chen <[email protected]>

Current code use dm_gpio_get_value() to get SDA and SCL value, and the value depends on the flag GPIOD_ACTIVE_LOW. When toggle SCL to wait slave release SDA, the SDA are config as GPIOD_IS_IN, and whether contain the GPIOD_ACTIVE_LOW depends on the DTS setting. Usually, for I2C GPIO, we use GPIOD_ACTIVE_LOW flag in DTS, so if the SDA is in low level, then
dm_gpio_get_value() will return 1, current code logic will stop the SCL toggle wrongly, cause the I2C deblock not work as expect.

This patch force set the GPIOD_ACTIVE_LOW for both GPIOD_IS_IN and GPIOD_IS_OUT, and make the return value of i2c_gpio_get_pin() eaqual to the physical voltage logic level.

Fixes aa54192d4a87 ("dm: i2c: implement gpio-based I2C deblock")

---
drivers/i2c/i2c-uclass.c | 5 +++--

1 file changed, 3 insertions(+), 2 deletions(-)
drivers/i2c/i2c-uclass.c +2 -1
index 8d9a89ed89..4dc707c659 100644
@@ -505,7 +505,8 @@ uint i2c_get_chip_addr_offset_mask(struct udevice
 *dev) static void i2c_gpio_set_pin(struct gpio_desc *pin, int bit) {
 if (bit)
- dm_gpio_set_dir_flags(pin, GPIOD_IS_IN);
+ dm_gpio_set_dir_flags(pin, GPIOD_IS_IN |
+   GPIOD_ACTIVE_LOW);

else
dm_gpio_set_dir_flags(pin, GPIOD_IS_OUT | GPIOD_ACTIVE_LOW | @@ -514,7 +515,7 @@ static void i2c_gpio_set_pin(struct gpio_desc *pin, int bit)

static int i2c_gpio_get_pin(struct gpio_desc *pin) {
- return dm_gpio_get_value(pin);
+ return !dm_gpio_get_value(pin);
}

int i2c_deblock_gpio_loop(struct gpio_desc *sda_pin, --
2.34.1