staging: iio: impedance-analyzer: ad5933: use guard(mutex) in ad5933_show()
Subsystems (4)
PATCH v2 1/1 staging: iio: impedance-analyzer: ad5933: use guard(mutex) in ad5933_show()
Use guard(mutex) to automatically manage the state lock in ad5933_show(). This simplifies the function by allowing direct returns from each switch case, eliminating the mutable return variables and manual mutex_unlock() calls.
---
Changes in v2:
- Extract range index into local variable 'ridx' to prevent static analysis warnings (Andy Shevchenko)
- Keep short return sysfs_emit() call on a single line (Andy Shevchenko)
- Split AD5933_OUT_RANGE_AVAIL arguments logically across lines (Andy Shevchenko)
- Remove redundant parentheses in AD5933_IN_PGA_GAIN ternary (Andy Shevchenko)
.../staging/iio/impedance-analyzer/ad5933.c | 38 ++++++++----------- 1 file changed, 15 insertions(+), 23 deletions(-)
drivers/staging/iio/impedance-analyzer/ad5933.c +15 -23
@@ -6,6 +6,7 @@*/ #include <linux/bits.h> +#include <linux/cleanup.h> #include <linux/clk.h> #include <linux/delay.h> #include <linux/device.h>
@@ -333,39 +334,30 @@ static ssize_t ad5933_show(struct device *dev,struct iio_dev *indio_dev =dev_to_iio_dev (dev);struct ad5933_state *st =iio_priv (indio_dev);struct iio_dev_attr *this_attr =to_iio_dev_attr (attr); -int ret =0 , len =0 ; +unsigned int ridx; + +guard (mutex)(&st->lock); -mutex_lock (&st->lock);switch ((u32 )this_attr->address) {case AD5933_OUT_RANGE : - len =sysfs_emit (buf,"%u\n" , - st->range_avail[(st->ctrl_hb >>1 ) &0x3 ]); -break ; + ridx = (st->ctrl_hb >>1 ) &0x03 ; +return sysfs_emit (buf,"%u\n" , st->range_avail[ridx]);case AD5933_OUT_RANGE_AVAIL : - len =sysfs_emit (buf,"%u %u %u %u\n" , st->range_avail[0 ], - st->range_avail[3 ], st->range_avail[2 ], - st->range_avail[1 ]); -break ; +return sysfs_emit (buf,"%u %u %u %u\n" , + st->range_avail[0 ], st->range_avail[3 ], + st->range_avail[2 ], st->range_avail[1 ]);case AD5933_OUT_SETTLING_CYCLES : - len =sysfs_emit (buf,"%d\n" , st->settling_cycles); -break ; +return sysfs_emit (buf,"%d\n" , st->settling_cycles);case AD5933_IN_PGA_GAIN : - len =sysfs_emit (buf,"%s\n" , - (st->ctrl_hb &AD5933_CTRL_PGA_GAIN_1 ) ? -"1" :"0.2" ); -break ; +return sysfs_emit (buf,"%s\n" , + st->ctrl_hb &AD5933_CTRL_PGA_GAIN_1 ?"1" :"0.2" );case AD5933_IN_PGA_GAIN_AVAIL : - len =sysfs_emit (buf,"1 0.2\n" ); -break ; +return sysfs_emit (buf,"1 0.2\n" );case AD5933_FREQ_POINTS : - len =sysfs_emit (buf,"%d\n" , st->freq_points); -break ; +return sysfs_emit (buf,"%d\n" , st->freq_points);default : - ret = -EINVAL ; +return -EINVAL ; } - -mutex_unlock (&st->lock); -return ret ? ret : len; } static ssize_t ad5933_store(struct device *dev,
Signature · 1 line
2.53.0
Quoted 4 lines · Nithishvelan M
Use guard(mutex) to automatically manage the state lock in
ad5933_show(). This simplifies the function by allowing direct returns from each switch case, eliminating the mutable return variables and manualmutex_unlock()calls.
...
drivers/staging/iio/impedance-analyzer/ad5933.c static ssize_t ad5933_show(struct device *dev, · line 353
Quoted 7 lines
drivers/staging/iio/impedance-analyzer/ad5933.c +2
-4case AD5933_IN_PGA_GAIN : - len =sysfs_emit (buf,"%s\n" , - (st->ctrl_hb &AD5933_CTRL_PGA_GAIN_1 ) ? -"1" :"0.2" ); -break ; +return sysfs_emit (buf,"%s\n" , + st->ctrl_hb &AD5933_CTRL_PGA_GAIN_1 ?"1" :"0.2" );
You can (re-)use ridx here as well
ridx = st->ctrl_hb & AD5933_CTRL_PGA_GAIN_1; return sysfs_emit(buf, "%s\n", ridx ? "1" : "0.2");
Signature · 2 lines
With Best Regards, Andy Shevchenko
Jonathan Cameron
maintainerQuoted 18 lines · On Mon, 19 Oct 2026 22:09:12 +0300 Andy Shevchenko…
Quoted 4 lines · Nithishvelan M
Use guard(mutex) to automatically manage the state lock in
ad5933_show(). This simplifies the function by allowing direct returns from each switch case, eliminating the mutable return variables and manualmutex_unlock()calls....
drivers/staging/iio/impedance-analyzer/ad5933.c static ssize_t ad5933_show(struct device *dev, · line 353
Quoted 7 lines
drivers/staging/iio/impedance-analyzer/ad5933.c +2
-4case AD5933_IN_PGA_GAIN : - len =sysfs_emit (buf,"%s\n" , - (st->ctrl_hb &AD5933_CTRL_PGA_GAIN_1 ) ? -"1" :"0.2" ); -break ; +return sysfs_emit (buf,"%s\n" , + st->ctrl_hb &AD5933_CTRL_PGA_GAIN_1 ?"1" :"0.2" );You can (re-)use ridx here as well
Quoted 3 lines
ridx = st->ctrl_hb & AD5933_CTRL_PGA_GAIN_1; return sysfs_emit(buf, "%s\n", ridx ? "1" : "0.2");
Maybe call it something else here as its not the range index that I can see.
Quoted 3 lines · Jonathan Cameron
Quoted 1 line · On Mon, 19 Oct 2026 22:09:12 +0300 Andy Shevchenko…
On Mon, Oct 19, 2026 at 03:02:48PM +0000, Nithishvelan M wrote:
...
Quoted 10 lines
Quoted 10 lines
drivers/staging/iio/impedance-analyzer/ad5933.c static ssize_t ad5933_show(struct device *dev, · line 353
Quoted 7 lines
drivers/staging/iio/impedance-analyzer/ad5933.c +2
-4case AD5933_IN_PGA_GAIN : - len =sysfs_emit (buf,"%s\n" , - (st->ctrl_hb &AD5933_CTRL_PGA_GAIN_1 ) ? -"1" :"0.2" ); -break ; +return sysfs_emit (buf,"%s\n" , + st->ctrl_hb &AD5933_CTRL_PGA_GAIN_1 ?"1" :"0.2" );You can (re-)use ridx here as well
Quoted 4 lines
Quoted 2 lines
ridx = st->ctrl_hb & AD5933_CTRL_PGA_GAIN_1; return sysfs_emit(buf, "%s\n", ridx ? "1" : "0.2");Maybe call it something else here as its not the range index that I can see.
True! I have no idea where the part of my thought went during the previous reply as I definitely kept in mind and wanted to add something like "(and this might need a variable renaming)".
Signature · 2 lines
With Best Regards, Andy Shevchenko