Skip to contentSkip to conversation

Fix ltc2983 probing

PATCH Superseded Nuno Sá 2 participants 5 messages Aug 10, 2021 – Aug 11, 2021 1 message not in the archive

Subsystems (2)

v1 of 2 Superseded Latest is v2 →

lore
Share & tools
lore

Nuno Sá

author maintainer
#0 lore raw

cover Fix ltc2983 probing

The issue was reported by Drew [1]. I tested this fix on a rpi and it works. It would be nice to have a tested-by tag for this on your platform Drew.

On top of the reported issue, the patch also fixes device binding and unbinding since that would also fail (that also obviously failed on rpi). The same goes for module unloading and loading.

[1]: https://marc.info/?l=linux-iio&m=162829198205250&w=2

Nuno Sá (1):
iio: ltc2983: fix device probe

 drivers/iio/temperature/ltc2983.c | 31 +++++++++++++++++++------------
 1 file changed, 19 insertions(+), 12 deletions(-)
Signature · 1 line
2.32.0
author maintainer
#1 lore raw

PATCH 1/1 iio: ltc2983: fix device probe

There is no reason to assume that the irq rising edge (indicating that the device start up phase is done) will happen after we request the irq. If the device is already up by the time we request it, the call to 'wait_for_completion_timeout()' will timeout and we will fail the device probe even though there's nothing wrong.

This patch fixes it by just polling the status register until we get the indication that the device is up and running. As a side effect of this fix, requesting the irq is also moved to after the setup function.

Fixes f110f3188e563 ("iio: temperature: Add support for LTC2983")
---
 drivers/iio/temperature/ltc2983.c | 31 +++++++++++++++++++------------
 1 file changed, 19 insertions(+), 12 deletions(-)
drivers/iio/temperature/ltc2983.c +19 -12
index 3b5ba26d7d86..c6c4877bdcff 100644
@@ -89,6 +89,8 @@
 
 #define	LTC2983_STATUS_START_MASK	BIT(7)
 #define	LTC2983_STATUS_START(x)		FIELD_PREP(LTC2983_STATUS_START_MASK, x)
+#define        LTC2983_STATUS_UP_MASK	GENMASK(7, 6)
+#define        LTC2983_STATUS_UP(reg)	FIELD_GET(LTC2983_STATUS_UP_MASK, reg)
 
 #define	LTC2983_STATUS_CHAN_SEL_MASK	GENMASK(4, 0)
 #define	LTC2983_STATUS_CHAN_SEL(x) \
@@ -1362,13 +1364,21 @@ static int ltc2983_parse_dt(struct ltc2983_data *st)
 
 static int ltc2983_setup(struct ltc2983_data *st, bool assign_iio)
 {
-	u32 iio_chan_t = 0, iio_chan_v = 0, chan, iio_idx = 0;
+	u32 iio_chan_t = 0, iio_chan_v = 0, chan, iio_idx = 0, status = 0;
 	int ret;
-	unsigned long time;
+	unsigned long time = 10;
 
 	/* make sure the device is up */
-	time = wait_for_completion_timeout(&st->completion,
-					    msecs_to_jiffies(250));
+	do {
+		ret = regmap_read(st->regmap, LTC2983_STATUS_REG, &status);
+		if (ret)
+			return ret;
+		/* start bit (7) is 0 and done bit (6) is 1 */
+		if (LTC2983_STATUS_UP(status) == 1)
+			break;
+
+		msleep(25);
+	} while (--time);
 
 	if (!time) {
 		dev_err(&st->spi->dev, "Device startup timed out\n");
@@ -1492,10 +1502,11 @@ static int ltc2983_probe(struct spi_device *spi)
 	ret = ltc2983_parse_dt(st);
 	if (ret)
 		return ret;
-	/*
-	 * let's request the irq now so it is used to sync the device
-	 * startup in ltc2983_setup()
-	 */
+
+	ret = ltc2983_setup(st, true);
+	if (ret)
+		return ret;
+
 	ret = devm_request_irq(&spi->dev, spi->irq, ltc2983_irq_handler,
 			       IRQF_TRIGGER_RISING, name, st);
 	if (ret) {
@@ -1503,10 +1514,6 @@ static int ltc2983_probe(struct spi_device *spi)
 		return ret;
 	}
 
-	ret = ltc2983_setup(st, true);
-	if (ret)
-		return ret;
-
 	indio_dev->name = name;
 	indio_dev->num_channels = st->iio_channels;
 	indio_dev->channels = st->iio_chan;
Signature · 1 line
2.32.0
Reviewed-by Tested-by on v1 1/1
#2 lore raw

Re: [PATCH 1/1] iio: ltc2983: fix device probe

Quoted 86 lines · Nuno Sá

There is no reason to assume that the irq rising edge (indicating that the device start up phase is done) will happen after we request the irq. If the device is already up by the time we request it, the call to 'wait_for_completion_timeout()' will timeout and we will fail the device probe even though there's nothing wrong.

This patch fixes it by just polling the status register until we get the indication that the device is up and running. As a side effect of this fix, requesting the irq is also moved to after the setup function.

Fixes f110f3188e563 ("iio: temperature: Add support for LTC2983")
---
 drivers/iio/temperature/ltc2983.c | 31 +++++++++++++++++++------------
 1 file changed, 19 insertions(+), 12 deletions(-)
drivers/iio/temperature/ltc2983.c +19 -12
index 3b5ba26d7d86..c6c4877bdcff 100644
@@ -89,6 +89,8 @@
 
 #define	LTC2983_STATUS_START_MASK	BIT(7)
 #define	LTC2983_STATUS_START(x)		FIELD_PREP(LTC2983_STATUS_START_MASK, x)
+#define        LTC2983_STATUS_UP_MASK	GENMASK(7, 6)
+#define        LTC2983_STATUS_UP(reg)	FIELD_GET(LTC2983_STATUS_UP_MASK, reg)
 
 #define	LTC2983_STATUS_CHAN_SEL_MASK	GENMASK(4, 0)
 #define	LTC2983_STATUS_CHAN_SEL(x) \
@@ -1362,13 +1364,21 @@ static int ltc2983_parse_dt(struct ltc2983_data *st)
 
 static int ltc2983_setup(struct ltc2983_data *st, bool assign_iio)
 {
-	u32 iio_chan_t = 0, iio_chan_v = 0, chan, iio_idx = 0;
+	u32 iio_chan_t = 0, iio_chan_v = 0, chan, iio_idx = 0, status = 0;
 	int ret;
-	unsigned long time;
+	unsigned long time = 10;
 
 	/* make sure the device is up */
-	time = wait_for_completion_timeout(&st->completion,
-					    msecs_to_jiffies(250));
+	do {
+		ret = regmap_read(st->regmap, LTC2983_STATUS_REG, &status);
+		if (ret)
+			return ret;
+		/* start bit (7) is 0 and done bit (6) is 1 */
+		if (LTC2983_STATUS_UP(status) == 1)
+			break;
+
+		msleep(25);
+	} while (--time);
 
 	if (!time) {
 		dev_err(&st->spi->dev, "Device startup timed out\n");
@@ -1492,10 +1502,11 @@ static int ltc2983_probe(struct spi_device *spi)
 	ret = ltc2983_parse_dt(st);
 	if (ret)
 		return ret;
-	/*
-	 * let's request the irq now so it is used to sync the device
-	 * startup in ltc2983_setup()
-	 */
+
+	ret = ltc2983_setup(st, true);
+	if (ret)
+		return ret;
+
 	ret = devm_request_irq(&spi->dev, spi->irq, ltc2983_irq_handler,
 			       IRQF_TRIGGER_RISING, name, st);
 	if (ret) {
@@ -1503,10 +1514,6 @@ static int ltc2983_probe(struct spi_device *spi)
 		return ret;
 	}
 
-	ret = ltc2983_setup(st, true);
-	if (ret)
-		return ret;
-
 	indio_dev->name = name;
 	indio_dev->num_channels = st->iio_channels;
 	indio_dev->channels = st->iio_chan;
Signature · 1 line
2.32.0

I have tested this on my custom Zynq-7000 board and the probe completes okay. The read of LTC2983_STATUS_REG returns 0x40 on the first attempt which is good. I am also able to read channels through sysfs. I have not tried suspend and resume as that is not configured on this system.

Thanks for fixing,
Drew

author maintainer
#3 lore raw

RE: [PATCH 1/1] iio: ltc2983: fix device probe

Quoted 107 lines

-----Original Message-----
From: Drew Fustini <[email protected]>
Sent: Wednesday, August 11, 2021 12:03 AM
To: Sa, Nuno <[email protected]>
Cc: [email protected]; =Jonathan Cameron <[email protected]>; Lars-Peter Clausen <[email protected]>
Subject: Re: [PATCH 1/1] iio: ltc2983: fix device probe

Quoted 2 lines · Nuno Sá

There is no reason to assume that the irq rising edge (indicating that the device start up phase is done) will happen after we request the

irq.

Quoted 2 lines

If the device is already up by the time we request it, the call to 'wait_for_completion_timeout()' will timeout and we will fail the

device

Quoted 11 lines

probe even though there's nothing wrong.

This patch fixes it by just polling the status register until we get the indication that the device is up and running. As a side effect of this fix, requesting the irq is also moved to after the setup function.

Fixes f110f3188e563 ("iio: temperature: Add support for LTC2983")
---
 drivers/iio/temperature/ltc2983.c | 31 +++++++++++++++++++------

------

Review

Quoted 3 lines
 1 file changed, 19 insertions(+), 12 deletions(-)
(quoted diff) +0 -0

b/drivers/iio/temperature/ltc2983.c

drivers/iio/temperature/ltc2983.c line 91

Quoted 7 lines

index 3b5ba26d7d86..c6c4877bdcff 100644

drivers/iio/temperature/ltc2983.c +0 -0
@@ -89,6 +89,8 @@
 
 #define	LTC2983_STATUS_START_MASK	BIT(7)
 #define	LTC2983_STATUS_START(x)
	FIELD_PREP(LTC2983_STATUS_START_MASK, x)

drivers/iio/temperature/ltc2983.c line 93

Quoted 2 lines
drivers/iio/temperature/ltc2983.c +2 -0
+#define        LTC2983_STATUS_UP_MASK	GENMASK(7, 6)
+#define        LTC2983_STATUS_UP(reg)
	FIELD_GET(LTC2983_STATUS_UP_MASK, reg)

drivers/iio/temperature/ltc2983.c static int ltc2983_parse_dt(struct

Quoted 4 lines
drivers/iio/temperature/ltc2983.c +0 -0
 #define	LTC2983_STATUS_CHAN_SEL_MASK	GENMASK(4, 0)
 #define	LTC2983_STATUS_CHAN_SEL(x) \
@@ -1362,13 +1364,21 @@ static int ltc2983_parse_dt(struct

ltc2983_data *st)

Quoted 4 lines
drivers/iio/temperature/ltc2983.c +0 -1
 static int ltc2983_setup(struct ltc2983_data *st, bool assign_iio)
 {
-	u32 iio_chan_t = 0, iio_chan_v = 0, chan, iio_idx = 0;

drivers/iio/temperature/ltc2983.c static int ltc2983_parse_dt(struct ltc2983_data *st)

Quoted 10 lines
drivers/iio/temperature/ltc2983.c +4 -3
+	u32 iio_chan_t = 0, iio_chan_v = 0, chan, iio_idx = 0, status = 0;
 	int ret;
-	unsigned long time;
+	unsigned long time = 10;
 
 	/* make sure the device is up */
-	time = wait_for_completion_timeout(&st->completion,
-					    msecs_to_jiffies(250));
+	do {
+		ret = regmap_read(st->regmap,

LTC2983_STATUS_REG, &status);

drivers/iio/temperature/ltc2983.c static int ltc2983_probe(struct spi_device

Quoted 12 lines
drivers/iio/temperature/ltc2983.c +8 -0
+		if (ret)
+			return ret;
+		/* start bit (7) is 0 and done bit (6) is 1 */
+		if (LTC2983_STATUS_UP(status) == 1)
+			break;
+
+		msleep(25);
+	} while (--time);
 
 	if (!time) {
 		dev_err(&st->spi->dev, "Device startup timed out\n");
@@ -1492,10 +1502,11 @@ static int ltc2983_probe(struct spi_device

*spi)

drivers/iio/temperature/ltc2983.c static int ltc2983_probe(struct spi_device *spi)

Quoted 13 lines
drivers/iio/temperature/ltc2983.c +5 -4
 	ret = ltc2983_parse_dt(st);
 	if (ret)
 		return ret;
-	/*
-	 * let's request the irq now so it is used to sync the device
-	 * startup in ltc2983_setup()
-	 */
+
+	ret = ltc2983_setup(st, true);
+	if (ret)
+		return ret;
+
 	ret = devm_request_irq(&spi->dev, spi->irq,

ltc2983_irq_handler,

drivers/iio/temperature/ltc2983.c static int ltc2983_probe(struct spi_device

Quoted 3 lines
drivers/iio/temperature/ltc2983.c +0 -0
 			       IRQF_TRIGGER_RISING, name, st);
 	if (ret) {
@@ -1503,10 +1514,6 @@ static int ltc2983_probe(struct spi_device

*spi)

Quoted 12 lines
drivers/iio/temperature/ltc2983.c +0 -5
 		return ret;
 	}
 
-	ret = ltc2983_setup(st, true);
-	if (ret)
-		return ret;
-
 	indio_dev->name = name;
 	indio_dev->num_channels = st->iio_channels;
 	indio_dev->channels = st->iio_chan;
--

2.32.0

Quoted 8 lines
Quoted 1 line

I have tested this on my custom Zynq-7000 board and the probe completes
okay. The read of LTC2983_STATUS_REG returns 0x40 on the first attempt
which is good. I am also able to read channels through sysfs. I have not tried suspend and resume as that is not configured on this system.

Hi,

Thanks for testing. During my tests I kind of emulated this by adding a devm_action which would put the device to sleep on the unbind path. Hence, unbinding + binding the device will cause a sleep + wake up on it. In this case I saw ~75 ms for it to come up. I read some 0's, 0x80 (as specified in the datasheet) and lastly 0x40.

Jonathan, I noticed some odd indentation in the new defines. If there's no other reason for a v2, it would be nice if you could fix this when applying :)

As a side note, I'm also aware that you prefer to see FIELD_{PREP|GET} applied inline in the code but in this case I made it like this to be consistent with the rest of the driver.

- Nuno Sá

A message that is not in the archive (sent off-list or not yet received): CAHp75VfjuoTywEc_HP8OwZn+gDVF+HJti=8itc-_bwSniB5ubA@mail.gmail.com

author maintainer
#5 lore raw

RE: [PATCH 1/1] iio: ltc2983: fix device probe

Quoted 89 lines

-----Original Message-----
From: Andy Shevchenko <[email protected]>
Sent: Wednesday, August 11, 2021 8:15 AM
To: Sa, Nuno <[email protected]>
Cc: [email protected]; =Jonathan Cameron <[email protected]>; Lars-Peter Clausen <[email protected]>; Drew Fustini <[email protected]>
Subject: Re: [PATCH 1/1] iio: ltc2983: fix device probe

On Tuesday, August 10, 2021, Nuno Sá <[email protected] <mailto:[email protected]> > wrote:

There is no reason to assume that the irq rising edge (indicating
that
the device start up phase is done) will happen after we request
the irq.
If the device is already up by the time we request it, the call to 'wait_for_completion_timeout()' will timeout and we will fail
the device
probe even though there's nothing wrong.

This patch fixes it by just polling the status register until we get
the
indication that the device is up and running. As a side effect of
this

	fix, requesting the irq is also moved to after the setup function.

	Fixes: f110f3188e563 ("iio: temperature: Add support for

LTC2983")

<mailto:[email protected]> >

<mailto:[email protected]> >

	---
	 drivers/iio/temperature/ltc2983.c | 31
+++++++++++++++++++------------
	 1 file changed, 19 insertions(+), 12 deletions(-)
	diff --git a/drivers/iio/temperature/ltc2983.c
b/drivers/iio/temperature/ltc2983.c
	index 3b5ba26d7d86..c6c4877bdcff 100644
	--- a/drivers/iio/temperature/ltc2983.c
	+++ b/drivers/iio/temperature/ltc2983.c
	@@ -89,6 +89,8 @@

	 #define        LTC2983_STATUS_START_MASK       BIT(7)
	 #define        LTC2983_STATUS_START(x)
FIELD_PREP(LTC2983_STATUS_START_MASK, x)
	+#define        LTC2983_STATUS_UP_MASK  GENMASK(7, 6)
	+#define        LTC2983_STATUS_UP(reg)

FIELD_GET(LTC2983_STATUS_UP_MASK, reg)

	 #define        LTC2983_STATUS_CHAN_SEL_MASK
GENMASK(4, 0)
	 #define        LTC2983_STATUS_CHAN_SEL(x) \
	@@ -1362,13 +1364,21 @@ static int ltc2983_parse_dt(struct

ltc2983_data *st)

static int ltc2983_setup(struct ltc2983_data *st, bool assign_iio) {
- u32 iio_chan_t = 0, iio_chan_v = 0, chan, iio_idx = 0;
+ u32 iio_chan_t = 0, iio_chan_v = 0, chan, iio_idx = 0, status
= 0;
int ret;
- unsigned long time;
+ unsigned long time = 10;

/* make sure the device is up */
- time = wait_for_completion_timeout(&st->completion,
- msecs_to_jiffies(250));
+ do {
+ ret = regmap_read(st->regmap,
LTC2983_STATUS_REG, &status);
+ if (ret)
+ return ret;
+ /* start bit (7) is 0 and done bit (6) is 1 */
+ if (LTC2983_STATUS_UP(status) == 1)
+ break;
+
+ msleep(25);
+ } while (--time);

Quoted 1 line

NIH regmap_read_poll_timeout()

Nice helper. Will send a v2 with it...

- Nuno Sá