Skip to contentSkip to conversation

iio: adc: ad7280a: use cleanup helpers guard() and scoped_guard() for mutex locking and improve code readability

PATCH In review Lucas Ivars Cadima Ciziks 3 participants 7 messages Apr 24 – Apr 27

Subsystems (2)
lore
Share & tools
lore
#0 lore raw

v5 cover iio: adc: ad7280a: use cleanup helpers guard() and scoped_guard() for mutex locking and improve code readability

This series converts the ad7280a driver to use the cleanup helpers guard() and scoped_guard() for mutex locking, replacing the manual mutex_lock()/mutex_unlock() calls.

A preparatory patch is added to extract chan->address bit fields into named local variables across ad7280_read_raw(), ad7280_show_balance_timer() and ad7280_store_balance_timer(), improving readability as suggested by Andy Shevchenko.

Lucas Ivars Cadima Ciziks (2):
iio: adc: ad7280a: Extract chan->address bit fields into named local
variables
iio: adc: ad7280a: use cleanup helpers guard() and scoped_guard() for
mutex locking

 drivers/iio/adc/ad7280a.c | 70 ++++++++++++++++++---------------------
 1 file changed, 32 insertions(+), 38 deletions(-)
Signature · 1 line
2.47.3
#1 lore raw

PATCH v5 1/2 iio: adc: ad7280a: Extract chan->address bit fields into named local variables

Extract the upper and lower bytes of chan->address into named local variables devaddr and ch across ad7280_read_raw(), ad7280_show_balance_timer() and ad7280_store_balance_timer() to improve readability and avoid inline bit manipulation in function calls.

---
v5:
- Add preparatory patch extracting chan->address bit fields into
named local variables, as suggested by Andy Shevchenko.
- Link to v4: https://lore.kernel.org/linux-iio/[email protected]/

v4:
- Improve readability in ad7280_show_balance_timer() by extracting

    chan->address bit fields into named local variables, as suggested by Andy Shevchenko.

- Link to v3: https://lore.kernel.org/linux-iio/[email protected]/

v3:
- Restore scoped_guard() in ad7280_show_balance_timer() instead of guard() to avoid holding the mutex during post-processing, as
suggested by Andy Shevchenko.
- Link to v2: https://lore.kernel.org/linux-iio/[email protected]/

v2:
- Replace scoped_guard with guard as suggested by Andy Shevchenko (Code Legibility).
- Fix indentation issues.
- Link to v1: https://lore.kernel.org/linux-iio/[email protected]/

---
 drivers/iio/adc/ad7280a.c | 17 ++++++++++-------
 1 file changed, 10 insertions(+), 7 deletions(-)
drivers/iio/adc/ad7280a.c +10 -7
index ba12a3796e2b..7f43da8e3340 100644
@@ -516,12 +516,13 @@ static ssize_t ad7280_show_balance_timer(struct iio_dev *indio_dev,
 					 char *buf)
 {
 	struct ad7280_state *st = iio_priv(indio_dev);
+	u8 devaddr = chan->address >> 8;
+	u8 ch = chan->address & 0xFF;
 	unsigned int msecs;
 	int ret;
 
 	mutex_lock(&st->lock);
-	ret = ad7280_read_reg(st, chan->address >> 8,
-			      (chan->address & 0xFF) + AD7280A_CB1_TIMER_REG);
+	ret = ad7280_read_reg(st, devaddr, ch + AD7280A_CB1_TIMER_REG);
 	mutex_unlock(&st->lock);
 
 	if (ret < 0)
@@ -538,6 +539,8 @@ static ssize_t ad7280_store_balance_timer(struct iio_dev *indio_dev,
 					  const char *buf, size_t len)
 {
 	struct ad7280_state *st = iio_priv(indio_dev);
+	u8 devaddr = chan->address >> 8;
+	u8 ch = chan->address & 0xFF;
 	int val, val2;
 	int ret;
 
@@ -552,8 +555,7 @@ static ssize_t ad7280_store_balance_timer(struct iio_dev *indio_dev,
 		return -EINVAL;
 
 	mutex_lock(&st->lock);
-	ret = ad7280_write(st, chan->address >> 8,
-			   (chan->address & 0xFF) + AD7280A_CB1_TIMER_REG, 0,
+	ret = ad7280_write(st, devaddr, ch + AD7280A_CB1_TIMER_REG, 0,
 			   FIELD_PREP(AD7280A_CB_TIMER_VAL_MSK, val));
 	mutex_unlock(&st->lock);
 
@@ -881,6 +883,8 @@ static int ad7280_read_raw(struct iio_dev *indio_dev,
 			   long m)
 {
 	struct ad7280_state *st = iio_priv(indio_dev);
+	u8 devaddr = chan->address >> 8;
+	u8 ch = chan->address & 0xFF;
 	int ret;
 
 	switch (m) {
@@ -889,8 +893,7 @@ static int ad7280_read_raw(struct iio_dev *indio_dev,
 		if (chan->address == AD7280A_ALL_CELLS)
 			ret = ad7280_read_all_channels(st, st->scan_cnt, NULL);
 		else
-			ret = ad7280_read_channel(st, chan->address >> 8,
-						  chan->address & 0xFF);
+			ret = ad7280_read_channel(st, devaddr, ch);
 		mutex_unlock(&st->lock);
 
 		if (ret < 0)
@@ -900,7 +903,7 @@ static int ad7280_read_raw(struct iio_dev *indio_dev,
 
 		return IIO_VAL_INT;
 	case IIO_CHAN_INFO_SCALE:
-		if ((chan->address & 0xFF) <= AD7280A_CELL_VOLTAGE_6_REG)
+		if (ch <= AD7280A_CELL_VOLTAGE_6_REG)
 			*val = 4000;
 		else
 			*val = 5000;
Signature · 1 line
2.47.3
Reviewed-by on v5 1/2
#2 lore raw

Re: [PATCH v5 1/2] iio: adc: ad7280a: Extract chan->address bit fields into named local variables

Quoted 10 lines · Lucas Ivars Cadima Ciziks

Extract the upper and lower bytes of chan->address into named local variables devaddr and ch across ad7280_read_raw(), ad7280_show_balance_timer() and ad7280_store_balance_timer() to improve readability and avoid inline bit manipulation in function calls.

Signature · 2 lines
With Best Regards,
Andy Shevchenko
#3 lore raw

PATCH v5 2/2 iio: adc: ad7280a: use cleanup helpers guard() and scoped_guard() for mutex locking

In reply to Lucas Ivars Cadima Ciziks

Replace open-coded mutex_lock/unlock pairs with the cleanup-based guard() and scoped_guard() helpers in ad7280a_write_thresh(), ad7280_show_balance_timer(), ad7280_store_balance_sw(), ad7280_store_balance_timer() and ad7280_read_raw().

This removes the need for the err_unlock label and explicit mutex_unlock() calls, as the lock is now automatically released when the function returns or the guarded scope exits, regardless of the exit path.

---
v5:
- Add preparatory patch extracting chan->address bit fields into
named local variables, as suggested by Andy Shevchenko.
- Link to v4: https://lore.kernel.org/linux-iio/[email protected]/

v4:
- Improve readability in ad7280_show_balance_timer() by extracting

    chan->address bit fields into named local variables, as suggested by Andy Shevchenko.

- Link to v3: https://lore.kernel.org/linux-iio/[email protected]/

v3:
- Restore scoped_guard() in ad7280_show_balance_timer() instead of guard() to avoid holding the mutex during post-processing, as
suggested by Andy Shevchenko.
- Link to v2: https://lore.kernel.org/linux-iio/[email protected]/

v2:
- Replace scoped_guard with guard as suggested by Andy Shevchenko (Code Legibility).
- Fix indentation issues.
- Link to v1: https://lore.kernel.org/linux-iio/[email protected]/

---
 drivers/iio/adc/ad7280a.c | 55 ++++++++++++++++-----------------------
 1 file changed, 23 insertions(+), 32 deletions(-)
drivers/iio/adc/ad7280a.c +23 -32
index 7f43da8e3340..56258b1118a4 100644
@@ -496,7 +496,8 @@ static ssize_t ad7280_store_balance_sw(struct iio_dev *indio_dev,
 	devaddr = chan->address >> 8;
 	ch = chan->address & 0xFF;
 
-	mutex_lock(&st->lock);
+	guard(mutex)(&st->lock);
+
 	if (readin)
 		st->cb_mask[devaddr] |= BIT(ch);
 	else
@@ -505,7 +506,6 @@ static ssize_t ad7280_store_balance_sw(struct iio_dev *indio_dev,
 	ret = ad7280_write(st, devaddr, AD7280A_CELL_BALANCE_REG, 0,
 			   FIELD_PREP(AD7280A_CELL_BALANCE_CHAN_BITMAP_MSK,
 				      st->cb_mask[devaddr]));
-	mutex_unlock(&st->lock);
 
 	return ret ? ret : len;
 }
@@ -521,9 +521,8 @@ static ssize_t ad7280_show_balance_timer(struct iio_dev *indio_dev,
 	unsigned int msecs;
 	int ret;
 
-	mutex_lock(&st->lock);
-	ret = ad7280_read_reg(st, devaddr, ch + AD7280A_CB1_TIMER_REG);
-	mutex_unlock(&st->lock);
+	scoped_guard(mutex, &st->lock)
+		ret = ad7280_read_reg(st, devaddr, ch + AD7280A_CB1_TIMER_REG);
 
 	if (ret < 0)
 		return ret;
@@ -554,10 +553,10 @@ static ssize_t ad7280_store_balance_timer(struct iio_dev *indio_dev,
 	if (val > 31)
 		return -EINVAL;
 
-	mutex_lock(&st->lock);
+	guard(mutex)(&st->lock);
+
 	ret = ad7280_write(st, devaddr, ch + AD7280A_CB1_TIMER_REG, 0,
 			   FIELD_PREP(AD7280A_CB_TIMER_VAL_MSK, val));
-	mutex_unlock(&st->lock);
 
 	return ret ? ret : len;
 }
@@ -739,7 +738,8 @@ static int ad7280a_write_thresh(struct iio_dev *indio_dev,
 	if (val2 != 0)
 		return -EINVAL;
 
-	mutex_lock(&st->lock);
+	guard(mutex)(&st->lock);
+
 	switch (chan->type) {
 	case IIO_VOLTAGE:
 		value = ((val - 1000) * 100) / 1568; /* LSB 15.68mV */
@@ -750,22 +750,20 @@ static int ad7280a_write_thresh(struct iio_dev *indio_dev,
 			ret = ad7280_write(st, AD7280A_DEVADDR_MASTER, addr,
 					   1, value);
 			if (ret)
-				break;
+				return ret;
 			st->cell_threshhigh = value;
-			break;
+			return 0;
 		case IIO_EV_DIR_FALLING:
 			addr = AD7280A_CELL_UNDERVOLTAGE_REG;
 			ret = ad7280_write(st, AD7280A_DEVADDR_MASTER, addr,
 					   1, value);
 			if (ret)
-				break;
+				return ret;
 			st->cell_threshlow = value;
-			break;
+			return 0;
 		default:
-			ret = -EINVAL;
-			goto err_unlock;
+			return -EINVAL;
 		}
-		break;
 	case IIO_TEMP:
 		value = (val * 10) / 196; /* LSB 19.6mV */
 		value = clamp(value, 0L, 0xFFL);
@@ -775,31 +773,23 @@ static int ad7280a_write_thresh(struct iio_dev *indio_dev,
 			ret = ad7280_write(st, AD7280A_DEVADDR_MASTER, addr,
 					   1, value);
 			if (ret)
-				break;
+				return ret;
 			st->aux_threshhigh = value;
-			break;
+			return 0;
 		case IIO_EV_DIR_FALLING:
 			addr = AD7280A_AUX_ADC_UNDERVOLTAGE_REG;
 			ret = ad7280_write(st, AD7280A_DEVADDR_MASTER, addr,
 					   1, value);
 			if (ret)
-				break;
+				return ret;
 			st->aux_threshlow = value;
-			break;
+			return 0;
 		default:
-			ret = -EINVAL;
-			goto err_unlock;
+			return -EINVAL;
 		}
-		break;
 	default:
-		ret = -EINVAL;
-		goto err_unlock;
+		return -EINVAL;
 	}
-
-err_unlock:
-	mutex_unlock(&st->lock);
-
-	return ret;
 }
 
 static irqreturn_t ad7280_event_handler(int irq, void *private)
@@ -888,13 +878,13 @@ static int ad7280_read_raw(struct iio_dev *indio_dev,
 	int ret;
 
 	switch (m) {
-	case IIO_CHAN_INFO_RAW:
-		mutex_lock(&st->lock);
+	case IIO_CHAN_INFO_RAW: {
+		guard(mutex)(&st->lock);
+
 		if (chan->address == AD7280A_ALL_CELLS)
 			ret = ad7280_read_all_channels(st, st->scan_cnt, NULL);
 		else
 			ret = ad7280_read_channel(st, devaddr, ch);
-		mutex_unlock(&st->lock);
 
 		if (ret < 0)
 			return ret;
@@ -902,6 +892,7 @@ static int ad7280_read_raw(struct iio_dev *indio_dev,
 		*val = ret;
 
 		return IIO_VAL_INT;
+	}
 	case IIO_CHAN_INFO_SCALE:
 		if (ch <= AD7280A_CELL_VOLTAGE_6_REG)
 			*val = 4000;
Signature · 1 line
2.47.3
Reviewed-by on v5 2/2
#4 lore raw

Re: [PATCH v5 2/2] iio: adc: ad7280a: use cleanup helpers guard() and scoped_guard() for mutex locking

Quoted 9 lines · Lucas Ivars Cadima Ciziks

Replace open-coded mutex_lock/unlock pairs with the cleanup-based guard() and scoped_guard() helpers in ad7280a_write_thresh(), ad7280_show_balance_timer(), ad7280_store_balance_sw(), ad7280_store_balance_timer() and ad7280_read_raw().

This removes the need for the err_unlock label and explicit mutex_unlock() calls, as the lock is now automatically released when the function returns or the guarded scope exits, regardless of the exit path.

...

drivers/iio/adc/ad7280a.c static ssize_t ad7280_show_balance_timer(struct iio_dev *indio_dev, · line 525

Quoted 10 lines
drivers/iio/adc/ad7280a.c +2 -3
 	unsigned int msecs;
 	int ret;
 
-	mutex_lock(&st->lock);
-	ret = ad7280_read_reg(st, devaddr, ch + AD7280A_CB1_TIMER_REG);
-	mutex_unlock(&st->lock);
+	scoped_guard(mutex, &st->lock)
+		ret = ad7280_read_reg(st, devaddr, ch + AD7280A_CB1_TIMER_REG);

Now this blank line may be removed.

Quoted 2 lines
(quoted diff) +0 -0
 	if (ret < 0)
 		return ret;
Signature · 2 lines
With Best Regards,
Andy Shevchenko
maintainer
#5 lore raw

Re: [PATCH v5 2/2] iio: adc: ad7280a: use cleanup helpers guard() and scoped_guard() for mutex locking

Quoted 26 lines · On Fri, 24 Apr 2026 18:44:52 +0300 Andy Shevchenko…
Quoted 9 lines · Lucas Ivars Cadima Ciziks

Replace open-coded mutex_lock/unlock pairs with the cleanup-based guard() and scoped_guard() helpers in ad7280a_write_thresh(), ad7280_show_balance_timer(), ad7280_store_balance_sw(), ad7280_store_balance_timer() and ad7280_read_raw().

This removes the need for the err_unlock label and explicit mutex_unlock() calls, as the lock is now automatically released when the function returns or the guarded scope exits, regardless of the exit path.

...

drivers/iio/adc/ad7280a.c static ssize_t ad7280_show_balance_timer(struct iio_dev *indio_dev, · line 525

Quoted 10 lines
drivers/iio/adc/ad7280a.c +2 -3
 	unsigned int msecs;
 	int ret;
 
-	mutex_lock(&st->lock);
-	ret = ad7280_read_reg(st, devaddr, ch + AD7280A_CB1_TIMER_REG);
-	mutex_unlock(&st->lock);
+	scoped_guard(mutex, &st->lock)
+		ret = ad7280_read_reg(st, devaddr, ch + AD7280A_CB1_TIMER_REG);  
Quoted 1 line

Now this blank line may be removed.

Tweaked.

Series applied to the testing branch of iio.git which I'll rebase on rc1 once it is available.

Thanks,

Jonathan

Quoted 4 lines
Quoted 3 lines
(quoted diff) +0 -0
 	if (ret < 0)
 		return ret;  
#6 lore raw

Re: [PATCH v5 2/2] iio: adc: ad7280a: use cleanup helpers guard() and scoped_guard() for mutex locking

Quoted 2 lines · Jonathan Cameron

Series applied to the testing branch of iio.git which I'll rebase on rc1 once it is available.

Thank you both for the review and for applying the series, Jonathan and Andy!

Lucas