Re: [PATCH] staging: iio: frequency: ad9832: use guard(mutex)
From: Dan Carpenter
Date: Wed Nov 11 2026 - 02:06:51 EST
On Tue, Nov 10, 2026 at 04:47:11PM +0000, Nithishvelan M wrote:
> Use guard(mutex) to automatically manage the state lock in
> ad9832_write(). This simplifies error handling by allowing direct
> returns from each switch case and error branch, eliminating the
> manual mutex_unlock() calls and the goto error label.
>
> Signed-off-by: Nithishvelan M <mnithishvelan@xxxxxxxxx>
> ---
> drivers/staging/iio/frequency/ad9832.c | 47 +++++++++++++++++---------
> 1 file changed, 31 insertions(+), 16 deletions(-)
>
> diff --git a/drivers/staging/iio/frequency/ad9832.c b/drivers/staging/iio/frequency/ad9832.c
> index 35f823f24..227dcd739 100644
> --- a/drivers/staging/iio/frequency/ad9832.c
> +++ b/drivers/staging/iio/frequency/ad9832.c
> @@ -8,6 +8,7 @@
> #include <linux/array_size.h>
> #include <linux/bitfield.h>
> #include <linux/bitops.h>
> +#include <linux/cleanup.h>
> #include <linux/clk.h>
> #include <linux/dev_printk.h>
> #include <linux/err.h>
> @@ -188,52 +189,65 @@ static ssize_t ad9832_write(struct device *dev, struct device_attribute *attr,
>
> ret = kstrtoul(buf, 10, &val);
> if (ret)
> - goto error_ret;
> + return ret;
> +
> + guard(mutex)(&st->lock);
>
> - mutex_lock(&st->lock);
> switch ((u32)this_attr->address) {
> case AD9832_FREQ0HM:
> case AD9832_FREQ1HM:
> ret = ad9832_write_frequency(st, this_attr->address, val);
> + if (ret)
> + return ret;
> +
> break;
> case AD9832_PHASE0H:
> case AD9832_PHASE1H:
> case AD9832_PHASE2H:
> case AD9832_PHASE3H:
> ret = ad9832_write_phase(st, this_attr->address, val);
> + if (ret)
> + return ret;
> +
> break;
> case AD9832_PINCTRL_EN:
> st->ctrl_ss &= ~AD9832_SELSRC;
> st->ctrl_ss |= FIELD_PREP(AD9832_SELSRC, val ? 0 : 1);
>
> st->data = cpu_to_be16(FIELD_PREP(AD9832_CMD_MSK, AD9832_CMD_SYNCSELSRC) |
> - st->ctrl_ss);
> + st->ctrl_ss);
Please don't make these sorts of unrelated white space changes. They
just make the review more complicated.
regards,
dan carpenter