From 32052904ae30a14707278cd87062750c67a1c8e6 Mon Sep 17 00:00:00 2001 From: Rodrigo Alencar Date: Thu, 16 Jul 2026 13:14:22 +0100 Subject: [PATCH] iio: dac: ad5686: read_raw/write_raw: use guard(mutex)() Use guarded mutex lock to facilitate code review when adding new attributes. This will allow for early returns, avoiding error-prone locking and unlocking in error paths. This also adds missing include linux/cleanup.h. Gain-control support will allow the scale attribute to be configurable. Reviewed-by: Maxwell Doose Reviewed-by: Joshua Crofts Reviewed-by: Andy Shevchenko Reviewed-by: David Lechner Signed-off-by: Rodrigo Alencar Signed-off-by: Jonathan Cameron --- drivers/iio/dac/ad5686.c | 19 ++++++++----------- 1 file changed, 8 insertions(+), 11 deletions(-) diff --git a/drivers/iio/dac/ad5686.c b/drivers/iio/dac/ad5686.c index 316f9ccf54d9..df32f46db81e 100644 --- a/drivers/iio/dac/ad5686.c +++ b/drivers/iio/dac/ad5686.c @@ -8,6 +8,7 @@ #include #include #include +#include #include #include #include @@ -177,11 +178,11 @@ static int ad5686_read_raw(struct iio_dev *indio_dev, struct ad5686_state *st = iio_priv(indio_dev); int ret; + guard(mutex)(&st->lock); + switch (m) { case IIO_CHAN_INFO_RAW: - mutex_lock(&st->lock); ret = ad5686_read(st, chan->address); - mutex_unlock(&st->lock); if (ret < 0) return ret; *val = (ret >> chan->scan_type.shift) & @@ -202,23 +203,19 @@ static int ad5686_write_raw(struct iio_dev *indio_dev, long mask) { struct ad5686_state *st = iio_priv(indio_dev); - int ret; + + guard(mutex)(&st->lock); switch (mask) { case IIO_CHAN_INFO_RAW: if (val >= (1 << chan->scan_type.realbits) || val < 0) return -EINVAL; - mutex_lock(&st->lock); - ret = ad5686_write(st, AD5686_CMD_WRITE_INPUT_N_UPDATE_N, - chan->address, val << chan->scan_type.shift); - mutex_unlock(&st->lock); - break; + return ad5686_write(st, AD5686_CMD_WRITE_INPUT_N_UPDATE_N, + chan->address, val << chan->scan_type.shift); default: - ret = -EINVAL; + return -EINVAL; } - - return ret; } static const struct iio_info ad5686_info = {