[PATCH v3 10/10] iio: accel: mma8452: use guard() to release mutexes

Sanjay Chitroda
2026-05-05T17:47:28Z

From: Sanjay Chitroda <sanjayembeddedse@gmail.com>

Replace explicit mutex_lock() and mutex_unlock() with the guard() and
scoped_guard() macro for cleaner and safer mutex handling.

Signed-off-by: Sanjay Chitroda <sanjayembeddedse@gmail.com>
---
Changes in v3:
- Following input from Jonathan extended mutex scope for
IIO_CHAN_INFO_RAW case to include math operation under lock
- Link to v2: https://lore.kernel.org/all/20260422165643.2148195-7-sanjayembedded@gmail.com/
---
drivers/iio/accel/mma8452.c | 29 +++++++++++------------------
1 file changed, 11 insertions(+), 18 deletions(-)

diff --git a/drivers/iio/accel/mma8452.c b/drivers/iio/accel/mma8452.c
index 3bc53cfdac24..1370674d71c6 100644
--- a/drivers/iio/accel/mma8452.c
+++ b/drivers/iio/accel/mma8452.c
@@ -18,6 +18,7 @@
* TODO: orientation events
*/

+#include <linux/cleanup.h>
#include <linux/delay.h>
#include <linux/i2c.h>
#include <linux/mod_devicetable.h>
@@ -478,10 +479,9 @@ static int mma8452_read_raw(struct iio_dev *indio_dev,
IIO_DEV_ACQUIRE_DIRECT_MODE(indio_dev, claim);
if (IIO_DEV_ACQUIRE_FAILED(claim))
return -EBUSY;
+ guard(mutex)(&data->lock);

- mutex_lock(&data->lock);
ret = mma8452_read(data, buffer);
- mutex_unlock(&data->lock);
if (ret < 0)
return ret;

@@ -579,36 +579,30 @@ static int mma8452_change_config(struct mma8452_data *data, u8 reg, u8 val)
int ret;
int is_active;

- mutex_lock(&data->lock);
+ guard(mutex)(&data->lock);

is_active = mma8452_is_active(data);
- if (is_active < 0) {
- ret = is_active;
- goto fail;
- }
+ if (is_active < 0)
+ return is_active;

/* config can only be changed when in standby */
if (is_active > 0) {
ret = mma8452_standby(data);
if (ret < 0)
- goto fail;
+ return ret;
}

ret = i2c_smbus_write_byte_data(data->client, reg, val);
if (ret < 0)
- goto fail;
+ return ret;

if (is_active > 0) {
ret = mma8452_active(data);
if (ret < 0)
- goto fail;
+ return ret;
}

- ret = 0;
-fail:
- mutex_unlock(&data->lock);
-
- return ret;
+ return 0;
}

static int mma8452_set_power_mode(struct mma8452_data *data, u8 mode)
@@ -1730,9 +1724,8 @@ static int mma8452_runtime_suspend(struct device *dev)
struct mma8452_data *data = iio_priv(indio_dev);
int ret;

- mutex_lock(&data->lock);
- ret = mma8452_standby(data);
- mutex_unlock(&data->lock);
+ scoped_guard(mutex, &data->lock)
+ ret = mma8452_standby(data);
if (ret < 0) {
dev_err(dev, "powering off device failed\n");
return -EAGAIN;
--
2.34.1