Re: [PATCH v4 17/17] staging: iio: resolver: ad2s1210: simplify code with guard(mutex)

From: Jonathan Cameron
Date: Tue Oct 10 2023 - 12:17:33 EST


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.

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.

If nothing else I think the patch description needs to made an argument for why it is fine.

> - mutex_unlock(&st->lock);
> return ret;
> }
>