On Tue, Oct 10, 2023 at 11:17 AM Jonathan Cameron <jic23@xxxxxxxxxx> wrote: > > On Thu, 5 Oct 2023 19:50:34 -0500 > David Lechner <dlechner@xxxxxxxxxxxx> wrote: > > > We can simplify the code and get rid of most of the gotos by using > > guard(mutex) from cleanup.h. > You could consider scoped_guard() for a few cases in here, but perhaps > it's better to be consistent and always use the guard() version. Yes, there it doesn't look like there are any cases where there is any long-running operation that could be done after unlocking the mutex, so I went with the simpler approach everywhere. > > There is a small timing question wrt to the gpio manipulation inline. > > > > > Signed-off-by: David Lechner <dlechner@xxxxxxxxxxxx> > > --- > > > > v4 changes: New patch in v4. > > > > drivers/staging/iio/resolver/ad2s1210.c | 157 ++++++++++---------------------- > > 1 file changed, 50 insertions(+), 107 deletions(-) > > > > diff --git a/drivers/staging/iio/resolver/ad2s1210.c b/drivers/staging/iio/resolver/ad2s1210.c > > index c4e1bc22e8b0..c4e0ffa92dc2 100644 > > --- a/drivers/staging/iio/resolver/ad2s1210.c > > +++ b/drivers/staging/iio/resolver/ad2s1210.c > > @@ -47,6 +47,7 @@ > > > > #include <linux/bitfield.h> > > #include <linux/bits.h> > > +#include <linux/cleanup.h> > > #include <linux/clk.h> > > #include <linux/delay.h> > > #include <linux/device.h> > > @@ -404,11 +405,13 @@ static int ad2s1210_single_conversion(struct iio_dev *indio_dev, > > s64 timestamp; > > int ret; > > > > - mutex_lock(&st->lock); > > + guard(mutex)(&st->lock); > > + > > gpiod_set_value(st->sample_gpio, 1); > > timestamp = iio_get_time_ns(indio_dev); > > /* delay (6 * tck + 20) nano seconds */ > > udelay(1); > > + gpiod_set_value(st->sample_gpio, 0); > > > > switch (chan->type) { > > case IIO_ANGL: > > @@ -418,14 +421,13 @@ static int ad2s1210_single_conversion(struct iio_dev *indio_dev, > > ret = ad2s1210_set_mode(st, MOD_VEL); > > break; > > default: > > - ret = -EINVAL; > > - break; > > + return -EINVAL; > > } > > if (ret < 0) > > - goto error_ret; > > + return ret; > > ret = spi_read(st->sdev, &st->sample, 3); > > if (ret < 0) > > - goto error_ret; > > + return ret; > > > > switch (chan->type) { > > case IIO_ANGL: > > @@ -437,17 +439,11 @@ static int ad2s1210_single_conversion(struct iio_dev *indio_dev, > > ret = IIO_VAL_INT; > > break; > > default: > > - ret = -EINVAL; > > - break; > > + return -EINVAL; > > } > > > > ad2s1210_push_events(indio_dev, st->sample.fault, timestamp); > > > > -error_ret: > > - gpiod_set_value(st->sample_gpio, 0); > > - /* delay (2 * tck + 20) nano seconds */ > > - udelay(1); > > Dropping this delay isn't obviously safe (though it probably is given stuff done before we exit). > I assume there are no rules on holding the gpio down for the register read. Correct. The SAMPLE gpio only needs to be held for a short time (~350 nanoseconds) to latch in the current values, then it doesn't matter when it is released. (Figure 35 in datasheet) > > If nothing else I think the patch description needs to made an argument for why it is fine. The longest possible delay needed after releasing the SAMPLE line before reasserting is ~350 nanoseconds. Is there a rule of thumb for deciding when there are enough instructions that no processor could execute them faster than this vs. when we should add an explicit delay? I think I will consider adding a patch in the next round to refactor the SAMPLE toggle to a separate function so we can be sure it is handled the same in all cases. > > > - mutex_unlock(&st->lock); > > return ret; > > } > >