Skip to contentSkip to conversation

mfd: arizona: Don't use regmap_read_poll_timeout

PATCH Superseded Charles Keepax 3 participants 5 messages May 11, 2018 – Jun 4, 2018

Subsystems (2)

v1 of 3 Superseded Latest is v3 →

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 mfd: arizona: Don't use regmap_read_poll_timeout

Some Arizona CODECs have a small timing window where they will NAK an I2C transaction if it happens before the boot done bit is set. This can cause the read of the register containing the boot done bit to fail until it is set. Since regmap_read_poll_timeout will abort polling if a read fails it can't be reliably used to poll the boot done bit over I2C.

Do a partial revert of ef84f885e037 ("mfd: arizona: Refactor arizona_poll_reg"), removing the regmap_read_poll_timeout but leaving the refactoring to make the arizona_poll_reg take more sensible arguments.

Fixes ef84f885e037 ("mfd: arizona: Refactor arizona_poll_reg")
---
 drivers/mfd/arizona-core.c | 29 +++++++++++++++++++++--------
 1 file changed, 21 insertions(+), 8 deletions(-)
drivers/mfd/arizona-core.c +21 -8
index 83f1c5a516d9..2ed29ce08dac 100644
@@ -240,18 +240,31 @@ static int arizona_poll_reg(struct arizona *arizona,
 			    int timeout_ms, unsigned int reg,
 			    unsigned int mask, unsigned int target)
 {
+	ktime_t timeout = ktime_add_us(ktime_get(), timeout_ms * 1000);
 	unsigned int val = 0;
 	int ret;
 
-	ret = regmap_read_poll_timeout(arizona->regmap,
-				       reg, val, ((val & mask) == target),
-				       ARIZONA_REG_POLL_DELAY_US,
-				       timeout_ms * 1000);
-	if (ret)
-		dev_err(arizona->dev, "Polling reg 0x%x timed out: %x\n",
-			reg, val);
+	while (true) {
+		ret = regmap_read(arizona->regmap, reg, &val);
 
-	return ret;
+		if ((val & mask) == target)
+			return 0;
+
+		if (ktime_compare(ktime_get(), timeout) > 0)
+			break;
+
+		usleep_range(ARIZONA_REG_POLL_DELAY_US / 2,
+			     ARIZONA_REG_POLL_DELAY_US);
+	}
+
+	if (ret) {
+		dev_err(arizona->dev, "Failed polling reg 0x%x: %d\n",
+			reg, ret);
+		return ret;
+	}
+
+	dev_err(arizona->dev, "Polling reg 0x%x timed out: %x\n", reg, val);
+	return -ETIMEDOUT;
 }
 
 static int arizona_wait_for_boot(struct arizona *arizona)
Signature · 1 line
2.11.0
#1 lore raw
Quoted 23 lines · Charles Keepax

Some Arizona CODECs have a small timing window where they will NAK an I2C transaction if it happens before the boot done bit is set. This can cause the read of the register containing the boot done bit to fail until it is set. Since regmap_read_poll_timeout will abort polling if a read fails it can't be reliably used to poll the boot done bit over I2C.

Do a partial revert of ef84f885e037 ("mfd: arizona: Refactor arizona_poll_reg"), removing the regmap_read_poll_timeout but leaving the refactoring to make the arizona_poll_reg take more sensible arguments.

Fixes ef84f885e037 ("mfd: arizona: Refactor arizona_poll_reg")
---
 drivers/mfd/arizona-core.c | 29 +++++++++++++++++++++--------
 1 file changed, 21 insertions(+), 8 deletions(-)
drivers/mfd/arizona-core.c +0 -0
index 83f1c5a516d9..2ed29ce08dac 100644

drivers/mfd/arizona-core.c static int arizona_poll_reg(struct arizona *arizona, · line 243

Quoted 4 lines
drivers/mfd/arizona-core.c +1 -0
@@ -240,18 +240,31 @@ static int arizona_poll_reg(struct arizona *arizona,
 			    int timeout_ms, unsigned int reg,
 			    unsigned int mask, unsigned int target)
 {
+	ktime_t timeout = ktime_add_us(ktime_get(), timeout_ms * 1000);

USEC_PER_MSEC ?

Quoted 35 lines
drivers/mfd/arizona-core.c +20 -8
 	unsigned int val = 0;
 	int ret;
 
-	ret = regmap_read_poll_timeout(arizona->regmap,
-				       reg, val, ((val & mask) == target),
-				       ARIZONA_REG_POLL_DELAY_US,
-				       timeout_ms * 1000);
-	if (ret)
-		dev_err(arizona->dev, "Polling reg 0x%x timed out: %x\n",
-			reg, val);
+	while (true) {
+		ret = regmap_read(arizona->regmap, reg, &val);
 
-	return ret;
+		if ((val & mask) == target)
+			return 0;
+
+		if (ktime_compare(ktime_get(), timeout) > 0)
+			break;
+
+		usleep_range(ARIZONA_REG_POLL_DELAY_US / 2,
+			     ARIZONA_REG_POLL_DELAY_US);
+	}
+
+	if (ret) {
+		dev_err(arizona->dev, "Failed polling reg 0x%x: %d\n",
+			reg, ret);
+		return ret;
+	}
+
+	dev_err(arizona->dev, "Polling reg 0x%x timed out: %x\n", reg, val);
+	return -ETIMEDOUT;
 }
 
 static int arizona_wait_for_boot(struct arizona *arizona)
Signature · 4 lines
Lee Jones [李琼斯]
Linaro Services Technical Lead
Linaro.org │ Open source software for ARM SoCs
Follow Linaro: Facebook | Twitter | Blog
#2 lore raw
Quoted 5 lines · Lee Jones
Quoted 3 lines · Charles Keepax
drivers/mfd/arizona-core.c +2 -0
+     while (true) {
+             ret = regmap_read(arizona->regmap, reg, &val);
Quoted 11 lines

drivers/mfd/arizona-core.c static int arizona_poll_reg(struct arizona *arizona, · line 258

Quoted 9 lines
drivers/mfd/arizona-core.c +9 -0
+             if ((val & mask) == target)
+                     return 0;
+
+             if (ktime_compare(ktime_get(), timeout) > 0)
+                     break;
+
+             usleep_range(ARIZONA_REG_POLL_DELAY_US / 2,
+                          ARIZONA_REG_POLL_DELAY_US);
+     }

From my point of view infinite loops not good for readability and maintenance.

Perhaps

do {
...
} while (ktime_compare(...));

?

Signature · 2 lines
With Best Regards,
Andy Shevchenko
#3 lore raw
Quoted 17 lines · Andy Shevchenko
Quoted 5 lines · Lee Jones
Quoted 3 lines · Charles Keepax
drivers/mfd/arizona-core.c +2 -0
+     while (true) {
+             ret = regmap_read(arizona->regmap, reg, &val);
Quoted 11 lines

drivers/mfd/arizona-core.c static int arizona_poll_reg(struct arizona *arizona, · line 258

Quoted 9 lines
drivers/mfd/arizona-core.c +9 -0
+             if ((val & mask) == target)
+                     return 0;
+
+             if (ktime_compare(ktime_get(), timeout) > 0)
+                     break;
+
+             usleep_range(ARIZONA_REG_POLL_DELAY_US / 2,
+                          ARIZONA_REG_POLL_DELAY_US);
+     }

From my point of view infinite loops not good for readability and maintenance.

Quoted 7 lines

Perhaps

do {
...
} while (ktime_compare(...));

?

I would rather not do it that way since then it becomes impossible to have a single poll with no delays happening. I can refactor to remove the while(true) but it will make the code a bit bigger, as I will probably need to duplicate some code outside the loop.

Thanks,
Charles

#4 lore raw
Quoted 24 lines · Charles Keepax
Quoted 23 lines · Andy Shevchenko
Quoted 5 lines · Lee Jones
Quoted 3 lines · Charles Keepax
drivers/mfd/arizona-core.c +2 -0
+     while (true) {
+             ret = regmap_read(arizona->regmap, reg, &val);
Quoted 11 lines

drivers/mfd/arizona-core.c static int arizona_poll_reg(struct arizona *arizona, · line 258

Quoted 9 lines
drivers/mfd/arizona-core.c +9 -0
+             if ((val & mask) == target)
+                     return 0;
+
+             if (ktime_compare(ktime_get(), timeout) > 0)
+                     break;
+
+             usleep_range(ARIZONA_REG_POLL_DELAY_US / 2,
+                          ARIZONA_REG_POLL_DELAY_US);
+     }

From my point of view infinite loops not good for readability and maintenance.

Perhaps

do {
...
} while (ktime_compare(...));

Quoted 8 lines
Quoted 1 line

?

I would rather not do it that way since then it becomes impossible to have a single poll with no delays happening. I can refactor to remove the while(true) but it will make the code a bit bigger, as I will probably need to duplicate some code outside the loop.

Looking again I think it is fine if I do the ktime_compare at the top of the loop though. Will respin.

Thanks,
Charles