]> git.ipfire.org Git - thirdparty/linux.git/commitdiff
iio: gyro: adxrs290: Use guard(mutex) in lieu of manual lock+unlock
authorGuilherme Dias <guilhermeabreu200105@usp.br>
Mon, 4 May 2026 19:04:25 +0000 (16:04 -0300)
committerJonathan Cameron <jic23@kernel.org>
Sun, 31 May 2026 09:59:34 +0000 (10:59 +0100)
Use guard(mutex)() to automatically release the lock on scope exit,
simplifying the error handling path and removing the need for
explicit unlock and goto-based cleanup.

Signed-off-by: Guilherme Dias <guilhermeabreu200105@usp.br>
Co-developed-by: João Paulo Menezes Linaris <jplinaris@usp.br>
Signed-off-by: João Paulo Menezes Linaris <jplinaris@usp.br>
Reviewed-by: Andy Shevchenko <andriy.shevchenko@intel.com>
Reviewed-by: Maxwell Doose <m32285159@gmail.com>
Signed-off-by: Jonathan Cameron <jic23@kernel.org>
drivers/iio/gyro/adxrs290.c

index 3efe385ebedc4c3fd73bc6f121ee5fdc9a1e0b70..35928383d4f252bd18f8d691e2c1425e43ac1d6b 100644 (file)
@@ -8,6 +8,7 @@
 
 #include <linux/bitfield.h>
 #include <linux/bitops.h>
+#include <linux/cleanup.h>
 #include <linux/delay.h>
 #include <linux/device.h>
 #include <linux/kernel.h>
@@ -115,65 +116,53 @@ static const int adxrs290_hpf_3db_freq_hz_table[][2] = {
 static int adxrs290_get_rate_data(struct iio_dev *indio_dev, const u8 cmd, int *val)
 {
        struct adxrs290_state *st = iio_priv(indio_dev);
-       int ret = 0;
        int temp;
 
-       mutex_lock(&st->lock);
+       guard(mutex)(&st->lock);
+
        temp = spi_w8r16(st->spi, cmd);
-       if (temp < 0) {
-               ret = temp;
-               goto err_unlock;
-       }
+       if (temp < 0)
+               return temp;
 
        *val = sign_extend32(temp, 15);
 
-err_unlock:
-       mutex_unlock(&st->lock);
-       return ret;
+       return 0;
 }
 
 static int adxrs290_get_temp_data(struct iio_dev *indio_dev, int *val)
 {
        const u8 cmd = ADXRS290_READ_REG(ADXRS290_REG_TEMP0);
        struct adxrs290_state *st = iio_priv(indio_dev);
-       int ret = 0;
        int temp;
 
-       mutex_lock(&st->lock);
+       guard(mutex)(&st->lock);
+
        temp = spi_w8r16(st->spi, cmd);
-       if (temp < 0) {
-               ret = temp;
-               goto err_unlock;
-       }
+       if (temp < 0)
+               return temp;
 
        /* extract lower 12 bits temperature reading */
        *val = sign_extend32(temp, 11);
 
-err_unlock:
-       mutex_unlock(&st->lock);
-       return ret;
+       return 0;
 }
 
 static int adxrs290_get_3db_freq(struct iio_dev *indio_dev, u8 *val, u8 *val2)
 {
        const u8 cmd = ADXRS290_READ_REG(ADXRS290_REG_FILTER);
        struct adxrs290_state *st = iio_priv(indio_dev);
-       int ret = 0;
        short temp;
 
-       mutex_lock(&st->lock);
+       guard(mutex)(&st->lock);
+
        temp = spi_w8r8(st->spi, cmd);
-       if (temp < 0) {
-               ret = temp;
-               goto err_unlock;
-       }
+       if (temp < 0)
+               return temp;
 
        *val = FIELD_GET(ADXRS290_LPF_MASK, temp);
        *val2 = FIELD_GET(ADXRS290_HPF_MASK, temp);
 
-err_unlock:
-       mutex_unlock(&st->lock);
-       return ret;
+       return 0;
 }
 
 static int adxrs290_spi_write_reg(struct spi_device *spi, const u8 reg,
@@ -220,11 +209,11 @@ static int adxrs290_set_mode(struct iio_dev *indio_dev, enum adxrs290_mode mode)
        if (st->mode == mode)
                return 0;
 
-       mutex_lock(&st->lock);
+       guard(mutex)(&st->lock);
 
        ret = spi_w8r8(st->spi, ADXRS290_READ_REG(ADXRS290_REG_POWER_CTL));
        if (ret < 0)
-               goto out_unlock;
+               return ret;
 
        val = ret;
 
@@ -236,21 +225,18 @@ static int adxrs290_set_mode(struct iio_dev *indio_dev, enum adxrs290_mode mode)
                val |= ADXRS290_MEASUREMENT;
                break;
        default:
-               ret = -EINVAL;
-               goto out_unlock;
+               return -EINVAL;
        }
 
        ret = adxrs290_spi_write_reg(st->spi, ADXRS290_REG_POWER_CTL, val);
        if (ret < 0) {
                dev_err(&st->spi->dev, "unable to set mode: %d\n", ret);
-               goto out_unlock;
+               return ret;
        }
 
        /* update cached mode */
        st->mode = mode;
 
-out_unlock:
-       mutex_unlock(&st->lock);
        return ret;
 }
 
@@ -506,19 +492,20 @@ static irqreturn_t adxrs290_trigger_handler(int irq, void *p)
        u8 tx = ADXRS290_READ_REG(ADXRS290_REG_DATAX0);
        int ret;
 
-       mutex_lock(&st->lock);
+       do {
+               guard(mutex)(&st->lock);
 
-       /* exercise a bulk data capture starting from reg DATAX0... */
-       ret = spi_write_then_read(st->spi, &tx, sizeof(tx), st->buffer.channels,
-                                 sizeof(st->buffer.channels));
-       if (ret < 0)
-               goto out_unlock_notify;
+               /* exercise a bulk data capture starting from reg DATAX0... */
+               ret = spi_write_then_read(st->spi, &tx, sizeof(tx),
+                                         st->buffer.channels,
+                                         sizeof(st->buffer.channels));
+               if (ret < 0)
+                       break;
 
-       iio_push_to_buffers_with_timestamp(indio_dev, &st->buffer,
-                                          pf->timestamp);
+               iio_push_to_buffers_with_timestamp(indio_dev, &st->buffer,
+                                                  pf->timestamp);
+       } while (0);
 
-out_unlock_notify:
-       mutex_unlock(&st->lock);
        iio_trigger_notify_done(indio_dev->trig);
 
        return IRQ_HANDLED;