Skip to content

Commit

Permalink
iio: ltc2983: fix device probe
Browse files Browse the repository at this point in the history
commit b76d26d upstream.

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.

Fix 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: f110f31 ("iio: temperature: Add support for LTC2983")
Reported-and-tested-by: Drew Fustini <drew@pdp7.com>
Reviewed-by: Drew Fustini <drew@pdp7.com>
Signed-off-by: Nuno Sá <nuno.sa@analog.com>
Reviewed-by: Andy Shevchenko <andy.shevchenko@gmail.com>
Cc: <Stable@vger.kernel.org>
Link: https://lore.kernel.org/r/20210811133220.190264-2-nuno.sa@analog.com
Signed-off-by: Jonathan Cameron <Jonathan.Cameron@huawei.com>
Signed-off-by: Greg Kroah-Hartman <gregkh@linuxfoundation.org>
  • Loading branch information
nunojsa authored and gregkh committed Sep 18, 2021
1 parent e10e3a4 commit 9c1887c
Showing 1 changed file with 14 additions and 16 deletions.
30 changes: 14 additions & 16 deletions drivers/iio/temperature/ltc2983.c
Expand Up @@ -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) \
Expand Down Expand Up @@ -1362,17 +1364,16 @@ 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;
int ret;
unsigned long time;

/* make sure the device is up */
time = wait_for_completion_timeout(&st->completion,
msecs_to_jiffies(250));

if (!time) {
/* make sure the device is up: start bit (7) is 0 and done bit (6) is 1 */
ret = regmap_read_poll_timeout(st->regmap, LTC2983_STATUS_REG, status,
LTC2983_STATUS_UP(status) == 1, 25000,
25000 * 10);
if (ret) {
dev_err(&st->spi->dev, "Device startup timed out\n");
return -ETIMEDOUT;
return ret;
}

st->iio_chan = devm_kzalloc(&st->spi->dev,
Expand Down Expand Up @@ -1492,21 +1493,18 @@ 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) {
dev_err(&spi->dev, "failed to request an irq, %d", ret);
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;
Expand Down

0 comments on commit 9c1887c

Please sign in to comment.