Re: [PATCH v5 3/5] iio:temperature:mlx90632: Convert polling while loop to do-while

From: Crt Mori
Date: Thu Aug 13 2020 - 09:04:39 EST


On Thu, 13 Aug 2020 at 13:24, Andy Shevchenko <andy.shevchenko@xxxxxxxxx> wrote:
>
> On Thu, Aug 13, 2020 at 2:14 PM Crt Mori <cmo@xxxxxxxxxxx> wrote:
> >
> > On Thu, 13 Aug 2020 at 13:03, Andy Shevchenko <andy.shevchenko@xxxxxxxxx> wrote:
> > >
> > > On Thu, Aug 13, 2020 at 10:53 AM Crt Mori <cmo@xxxxxxxxxxx> wrote:
> > > >
> > > > Reduce number of lines and improve readability to convert polling while
> > > > loops to do-while. The iopoll.h interface was not used, because we
> > > > require more than 20ms timeout, because time for sensor to perform a
> > > > measurement is around 10ms and it needs to perform measurements for each
> > > > channel (which currently is 3).
> > >
> > > I don't see how it prevents using iopoll.h. It uses usleep_range()
> > > under the hood in the same way you did here, but open coded.
> > >
> >
> > One loop is indeed 10ms and that is not the problem, the problem is
> > that timeout is at least 3 calls of this data ready (3 channels), so
> > that is at minimum 30ms of timeout, or it could even be 4 in worse
> > case scenario and that is outside of the range for usleep to measure.
> > So in case of the other loop, where we wait 200ms for channel refresh
> > it is also out of scope. Timeout should be in number of tries or in
> > msleep range if you ask me.
>
> I still didn't buy it. You have in both cases usleep_range(). Why in
> your case it's okay and in regmap_read_poll_timeout() is not?
>

I tried and it did not work, so then I read the manual. Looking into

* regmap_read_poll_timeout_atomic - Poll until a condition is met or a
timeout occurs
...
* @delay_us: Time to udelay between reads in us (0 tight-loops).
* Should be less than ~10us since udelay is used
* (see Documentation/timers/timers-howto.rst).
* @timeout_us: Timeout in us, 0 means never timeout


So I went to read Documentation/timers/timers-howto.rst

SLEEPING FOR ~USECS OR SMALL MSECS ( 10us - 20ms):
* Use usleep_range

- Why not msleep for (1ms - 20ms)?
Explained originally here:
http://lkml.org/lkml/2007/8/3/250

msleep(1~20) may not do what the caller intends, and
will often sleep longer (~20 ms actual sleep for any
value given in the 1~20ms range). In many cases this
is not the desired behavior.

Since I am above the 20ms range, it is too much for usleep_range and
that proved to be a case as I got -ETIMEOUT and the desired channels
were not read.
> > > ...
> > >
> > > > - while (tries-- > 0) {
> > > > + do {
> > > > ret = regmap_read(data->regmap, MLX90632_REG_STATUS,
> > > > &reg_status);
> > > > if (ret < 0)
> > > > return ret;
> > > > - if (reg_status & MLX90632_STAT_DATA_RDY)
> > > > - break;
> > > > usleep_range(10000, 11000);
> > > > - }
> > > > + } while (!(reg_status & MLX90632_STAT_DATA_RDY) && tries--);
> > > >
> > > > if (tries < 0) {
> > > > dev_err(&data->client->dev, "data not ready");
>
> --
> With Best Regards,
> Andy Shevchenko