Skip to contentSkip to conversation

iio: dac: ad5360: convert to guard(mutex)

PATCH Superseded Menderes Sabaz 4 participants 9 messages Jan 28 – Jan 29

Subsystems (2)

v1 of 4 Superseded Latest is v4 →

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 iio: dac: ad5360: convert to guard(mutex)

Converting mutex_lock and mutex_unlock to guard(mutex) Simplify the locking logic by using guard(mutex).
This removes the need for explicit unlock calls in error paths and simplifies the code by removing goto labels.

---
 drivers/iio/dac/ad5360.c | 12 +++---------
 1 file changed, 3 insertions(+), 9 deletions(-)
drivers/iio/dac/ad5360.c +3 -9
index 8271849b1..a22bafa45 100644
@@ -209,10 +209,8 @@ static int ad5360_write(struct iio_dev *indio_dev, unsigned int cmd,
 	int ret;
 	struct ad5360_state *st = iio_priv(indio_dev);
 
-	mutex_lock(&st->lock);
+	guard(mutex)(&st->lock);
 	ret = ad5360_write_unlocked(indio_dev, cmd, addr, val, shift);
-	mutex_unlock(&st->lock);
-
 	return ret;
 }
 
@@ -232,7 +230,7 @@ static int ad5360_read(struct iio_dev *indio_dev, unsigned int type,
 		},
 	};
 
-	mutex_lock(&st->lock);
+	guard(mutex)(&st->lock);
 
 	st->data[0].d32 = cpu_to_be32(AD5360_CMD(AD5360_CMD_SPECIAL_FUNCTION) |
 		AD5360_ADDR(AD5360_REG_SF_READBACK) |
@@ -243,8 +241,6 @@ static int ad5360_read(struct iio_dev *indio_dev, unsigned int type,
 	if (ret >= 0)
 		ret = be32_to_cpu(st->data[1].d32) & 0xffff;
 
-	mutex_unlock(&st->lock);
-
 	return ret;
 }
 
@@ -264,7 +260,7 @@ static int ad5360_update_ctrl(struct iio_dev *indio_dev, unsigned int set,
 	struct ad5360_state *st = iio_priv(indio_dev);
 	int ret;
 
-	mutex_lock(&st->lock);
+	guard(mutex)(&st->lock);
 
 	st->ctrl |= set;
 	st->ctrl &= ~clr;
@@ -272,8 +268,6 @@ static int ad5360_update_ctrl(struct iio_dev *indio_dev, unsigned int set,
 	ret = ad5360_write_unlocked(indio_dev, AD5360_CMD_SPECIAL_FUNCTION,
 			AD5360_REG_SF_CTRL, st->ctrl, 0);
 
-	mutex_unlock(&st->lock);
-
 	return ret;
 }
 
Signature · 1 line
2.52.0
reviewer
#1 lore raw
Quoted 16 lines · Menderes Sabaz

Converting mutex_lock and mutex_unlock to guard(mutex) Simplify the locking logic by using guard(mutex).
This removes the need for explicit unlock calls in error paths and simplifies the code by removing goto labels.

---
 drivers/iio/dac/ad5360.c | 12 +++---------
 1 file changed, 3 insertions(+), 9 deletions(-)
drivers/iio/dac/ad5360.c +0 -0
index 8271849b1..a22bafa45 100644
@@ -209,10 +209,8 @@ static int ad5360_write(struct iio_dev *indio_dev, unsigned int cmd,
 	int ret;

drivers/iio/dac/ad5360.c static int ad5360_write(struct iio_dev *indio_dev, unsigned int cmd, · line 216

Quoted 10 lines
drivers/iio/dac/ad5360.c +1 -3
 	struct ad5360_state *st = iio_priv(indio_dev);
 
-	mutex_lock(&st->lock);
+	guard(mutex)(&st->lock);
 	ret = ad5360_write_unlocked(indio_dev, cmd, addr, val, shift);
-	mutex_unlock(&st->lock);
-
 	return ret;
 }
 

All of the return ret can be removed now and function calls can return directly. Otherwise, this isn't really an improvement. Same applies below.

Quoted 36 lines
drivers/iio/dac/ad5360.c +2 -6
@@ -232,7 +230,7 @@ static int ad5360_read(struct iio_dev *indio_dev, unsigned int type,
 		},
 	};
 
-	mutex_lock(&st->lock);
+	guard(mutex)(&st->lock);
 
 	st->data[0].d32 = cpu_to_be32(AD5360_CMD(AD5360_CMD_SPECIAL_FUNCTION) |
 		AD5360_ADDR(AD5360_REG_SF_READBACK) |
@@ -243,8 +241,6 @@ static int ad5360_read(struct iio_dev *indio_dev, unsigned int type,
 	if (ret >= 0)
 		ret = be32_to_cpu(st->data[1].d32) & 0xffff;
 
-	mutex_unlock(&st->lock);
-
 	return ret;
 }
 
@@ -264,7 +260,7 @@ static int ad5360_update_ctrl(struct iio_dev *indio_dev, unsigned int set,
 	struct ad5360_state *st = iio_priv(indio_dev);
 	int ret;
 
-	mutex_lock(&st->lock);
+	guard(mutex)(&st->lock);
 
 	st->ctrl |= set;
 	st->ctrl &= ~clr;
@@ -272,8 +268,6 @@ static int ad5360_update_ctrl(struct iio_dev *indio_dev, unsigned int set,
 	ret = ad5360_write_unlocked(indio_dev, AD5360_CMD_SPECIAL_FUNCTION,
 			AD5360_REG_SF_CTRL, st->ctrl, 0);
 
-	mutex_unlock(&st->lock);
-
 	return ret;
 }
 
#2 lore raw

PATCH v2 1/1 iio: dac: ad5360: convert to guard(mutex)

In reply to Menderes Sabaz

Converting mutex_lock and mutex_unlock to guard(mutex) Simplify the locking logic by using guard(mutex).
This removes the need for explicit unlock calls in error paths and simplifies the code by removing goto labels.

---
Changes in v2:
- Removed unnecessary local variables

 drivers/iio/dac/ad5360.c | 27 ++++++++-------------------
 1 file changed, 8 insertions(+), 19 deletions(-)
drivers/iio/dac/ad5360.c +8 -19
index 8271849b1..15df5dd2a 100644
@@ -206,14 +206,10 @@ static int ad5360_write_unlocked(struct iio_dev *indio_dev,
 static int ad5360_write(struct iio_dev *indio_dev, unsigned int cmd,
 	unsigned int addr, unsigned int val, unsigned int shift)
 {
-	int ret;
 	struct ad5360_state *st = iio_priv(indio_dev);
 
-	mutex_lock(&st->lock);
-	ret = ad5360_write_unlocked(indio_dev, cmd, addr, val, shift);
-	mutex_unlock(&st->lock);
-
-	return ret;
+	guard(mutex)(&st->lock);
+	return ad5360_write_unlocked(indio_dev, cmd, addr, val, shift);
 }
 
 static int ad5360_read(struct iio_dev *indio_dev, unsigned int type,
@@ -232,7 +228,7 @@ static int ad5360_read(struct iio_dev *indio_dev, unsigned int type,
 		},
 	};
 
-	mutex_lock(&st->lock);
+	guard(mutex)(&st->lock);
 
 	st->data[0].d32 = cpu_to_be32(AD5360_CMD(AD5360_CMD_SPECIAL_FUNCTION) |
 		AD5360_ADDR(AD5360_REG_SF_READBACK) |
@@ -240,12 +236,10 @@ static int ad5360_read(struct iio_dev *indio_dev, unsigned int type,
 		AD5360_READBACK_ADDR(addr));
 
 	ret = spi_sync_transfer(st->spi, t, ARRAY_SIZE(t));
-	if (ret >= 0)
-		ret = be32_to_cpu(st->data[1].d32) & 0xffff;
-
-	mutex_unlock(&st->lock);
+	if (ret < 0)
+		return ret;
 
-	return ret;
+	return be32_to_cpu(st->data[1].d32) & 0xffff;
 }
 
 static ssize_t ad5360_read_dac_powerdown(struct device *dev,
@@ -262,19 +256,14 @@ static int ad5360_update_ctrl(struct iio_dev *indio_dev, unsigned int set,
 	unsigned int clr)
 {
 	struct ad5360_state *st = iio_priv(indio_dev);
-	int ret;
 
-	mutex_lock(&st->lock);
+	guard(mutex)(&st->lock);
 
 	st->ctrl |= set;
 	st->ctrl &= ~clr;
 
-	ret = ad5360_write_unlocked(indio_dev, AD5360_CMD_SPECIAL_FUNCTION,
+	return ad5360_write_unlocked(indio_dev, AD5360_CMD_SPECIAL_FUNCTION,
 			AD5360_REG_SF_CTRL, st->ctrl, 0);
-
-	mutex_unlock(&st->lock);
-
-	return ret;
 }
 
 static ssize_t ad5360_write_dac_powerdown(struct device *dev,
Signature · 1 line
2.52.0
#3 lore raw
Quoted 4 lines · Menderes Sabaz

Converting mutex_lock and mutex_unlock to guard(mutex) Simplify the locking logic by using guard(mutex).
This removes the need for explicit unlock calls in error paths and simplifies the code by removing goto labels.

...

Quoted 2 lines

Changes in v2:
- Removed unnecessary local variables

Avoid sending v<N+1> in the v<N> email threads.

...

drivers/iio/dac/ad5360.c static int ad5360_update_ctrl(struct iio_dev *indio_dev, unsigned int set, · line 266

Quoted 3 lines
drivers/iio/dac/ad5360.c +1 -1
-	ret = ad5360_write_unlocked(indio_dev, AD5360_CMD_SPECIAL_FUNCTION,
+	return ad5360_write_unlocked(indio_dev, AD5360_CMD_SPECIAL_FUNCTION,
 			AD5360_REG_SF_CTRL, st->ctrl, 0);

The second line is wrongly indented.

Signature · 2 lines
With Best Regards,
Andy Shevchenko
reviewer Reviewed-by on v2 1/1
#4 lore raw
In reply to Menderes Sabaz
Quoted 1 line · Menderes Sabaz

Converting mutex_lock and mutex_unlock to guard(mutex)

Usually we phrase this as "Covert" instead of "Converting". And should have period at end of sentence.

Quoted 1 line

Simplify the locking logic by using guard(mutex).

This sentence is a bit redundant.

Quoted 2 lines

This removes the need for explicit unlock calls in error paths and simplifies the code by removing goto labels.

Can leave out this sentence since it doesn't apply to this patch (e.g. no gotos were removed).

Quoted 66 lines

---
Changes in v2:
- Removed unnecessary local variables

 drivers/iio/dac/ad5360.c | 27 ++++++++-------------------
 1 file changed, 8 insertions(+), 19 deletions(-)
drivers/iio/dac/ad5360.c +7 -14
index 8271849b1..15df5dd2a 100644
@@ -206,14 +206,10 @@ static int ad5360_write_unlocked(struct iio_dev *indio_dev,
 static int ad5360_write(struct iio_dev *indio_dev, unsigned int cmd,
 	unsigned int addr, unsigned int val, unsigned int shift)
 {
-	int ret;
 	struct ad5360_state *st = iio_priv(indio_dev);
 
-	mutex_lock(&st->lock);
-	ret = ad5360_write_unlocked(indio_dev, cmd, addr, val, shift);
-	mutex_unlock(&st->lock);
-
-	return ret;
+	guard(mutex)(&st->lock);
+	return ad5360_write_unlocked(indio_dev, cmd, addr, val, shift);
 }
 
 static int ad5360_read(struct iio_dev *indio_dev, unsigned int type,
@@ -232,7 +228,7 @@ static int ad5360_read(struct iio_dev *indio_dev, unsigned int type,
 		},
 	};
 
-	mutex_lock(&st->lock);
+	guard(mutex)(&st->lock);
 
 	st->data[0].d32 = cpu_to_be32(AD5360_CMD(AD5360_CMD_SPECIAL_FUNCTION) |
 		AD5360_ADDR(AD5360_REG_SF_READBACK) |
@@ -240,12 +236,10 @@ static int ad5360_read(struct iio_dev *indio_dev, unsigned int type,
 		AD5360_READBACK_ADDR(addr));
 
 	ret = spi_sync_transfer(st->spi, t, ARRAY_SIZE(t));
-	if (ret >= 0)
-		ret = be32_to_cpu(st->data[1].d32) & 0xffff;
-
-	mutex_unlock(&st->lock);
+	if (ret < 0)
+		return ret;
 
-	return ret;
+	return be32_to_cpu(st->data[1].d32) & 0xffff;
 }
 
 static ssize_t ad5360_read_dac_powerdown(struct device *dev,
@@ -262,19 +256,14 @@ static int ad5360_update_ctrl(struct iio_dev *indio_dev, unsigned int set,
 	unsigned int clr)
 {
 	struct ad5360_state *st = iio_priv(indio_dev);
-	int ret;
 
-	mutex_lock(&st->lock);
+	guard(mutex)(&st->lock);
 
 	st->ctrl |= set;
 	st->ctrl &= ~clr;
 

drivers/iio/dac/ad5360.c static int ad5360_update_ctrl(struct iio_dev *indio_dev, unsigned int set, · line 269

Quoted 10 lines
drivers/iio/dac/ad5360.c +1 -5
-	ret = ad5360_write_unlocked(indio_dev, AD5360_CMD_SPECIAL_FUNCTION,
+	return ad5360_write_unlocked(indio_dev, AD5360_CMD_SPECIAL_FUNCTION,
 			AD5360_REG_SF_CTRL, st->ctrl, 0);
-
-	mutex_unlock(&st->lock);
-
-	return ret;
 }
 
 static ssize_t ad5360_write_dac_powerdown(struct device *dev,

Patch looks better now.

With a bit more tidying (commit message and alignment that Andy pointed out):

#5 lore raw

PATCH 1/1 iio: dac: ad5360: converting to guard(mutex)

In reply to Menderes Sabaz

Converting mutex_lock and mutex_unlock to guard(mutex)

---
 drivers/iio/dac/ad5360.c | 29 +++++++++--------------------
 1 file changed, 9 insertions(+), 20 deletions(-)
drivers/iio/dac/ad5360.c +9 -20
index 8271849b1..e79ad64d7 100644
@@ -206,14 +206,10 @@ static int ad5360_write_unlocked(struct iio_dev *indio_dev,
 static int ad5360_write(struct iio_dev *indio_dev, unsigned int cmd,
 	unsigned int addr, unsigned int val, unsigned int shift)
 {
-	int ret;
 	struct ad5360_state *st = iio_priv(indio_dev);
 
-	mutex_lock(&st->lock);
-	ret = ad5360_write_unlocked(indio_dev, cmd, addr, val, shift);
-	mutex_unlock(&st->lock);
-
-	return ret;
+	guard(mutex)(&st->lock);
+	return ad5360_write_unlocked(indio_dev, cmd, addr, val, shift);
 }
 
 static int ad5360_read(struct iio_dev *indio_dev, unsigned int type,
@@ -232,7 +228,7 @@ static int ad5360_read(struct iio_dev *indio_dev, unsigned int type,
 		},
 	};
 
-	mutex_lock(&st->lock);
+	guard(mutex)(&st->lock);
 
 	st->data[0].d32 = cpu_to_be32(AD5360_CMD(AD5360_CMD_SPECIAL_FUNCTION) |
 		AD5360_ADDR(AD5360_REG_SF_READBACK) |
@@ -240,12 +236,10 @@ static int ad5360_read(struct iio_dev *indio_dev, unsigned int type,
 		AD5360_READBACK_ADDR(addr));
 
 	ret = spi_sync_transfer(st->spi, t, ARRAY_SIZE(t));
-	if (ret >= 0)
-		ret = be32_to_cpu(st->data[1].d32) & 0xffff;
-
-	mutex_unlock(&st->lock);
+	if (ret < 0)
+		return ret;
 
-	return ret;
+	return be32_to_cpu(st->data[1].d32) & 0xffff;
 }
 
 static ssize_t ad5360_read_dac_powerdown(struct device *dev,
@@ -262,19 +256,14 @@ static int ad5360_update_ctrl(struct iio_dev *indio_dev, unsigned int set,
 	unsigned int clr)
 {
 	struct ad5360_state *st = iio_priv(indio_dev);
-	int ret;
 
-	mutex_lock(&st->lock);
+	guard(mutex)(&st->lock);
 
 	st->ctrl |= set;
 	st->ctrl &= ~clr;
 
-	ret = ad5360_write_unlocked(indio_dev, AD5360_CMD_SPECIAL_FUNCTION,
-			AD5360_REG_SF_CTRL, st->ctrl, 0);
-
-	mutex_unlock(&st->lock);
-
-	return ret;
+	return ad5360_write_unlocked(indio_dev, AD5360_CMD_SPECIAL_FUNCTION,
+		AD5360_REG_SF_CTRL, st->ctrl, 0);
 }
 
 static ssize_t ad5360_write_dac_powerdown(struct device *dev,
Signature · 1 line
2.52.0
#6 lore raw

Re: [PATCH] iio: dac: ad5360: converting to guard(mutex)

Quoted 1 line · Menderes Sabaz

Converting mutex_lock and mutex_unlock to guard(mutex)

Refer to the functions as func() and don't forget period at the end.

Converting mutex_lock() and mutex_unlock() to guard(mutex)().

...

drivers/iio/dac/ad5360.c static int ad5360_update_ctrl(struct iio_dev *indio_dev, unsigned int set, · line 266

Quoted 2 lines
drivers/iio/dac/ad5360.c +2 -0
+	return ad5360_write_unlocked(indio_dev, AD5360_CMD_SPECIAL_FUNCTION,
+		AD5360_REG_SF_CTRL, st->ctrl, 0);

Wrong indentation.

Signature · 2 lines
With Best Regards,
Andy Shevchenko
maintainer Applied
#7 lore raw
In reply to Menderes Sabaz
Quoted 6 lines · On Wed, 28 Jan 2026 18:32:21 +0300 Menderes Sabaz <[email protected]>…

Converting mutex_lock and mutex_unlock to guard(mutex) Simplify the locking logic by using guard(mutex).
This removes the need for explicit unlock calls in error paths and simplifies the code by removing goto labels.

Hi Menderes,

Applied to the togreg branch of iio.git.

Thanks,

Jonathan

maintainer
#8 lore raw
Quoted 8 lines · On Thu, 29 Jan 2026 16:16:21 +0000 Jonathan Cameron <[email protected]> wrote:
Quoted 5 lines · On Wed, 28 Jan 2026 18:32:21 +0300 Menderes Sabaz <[email protected]>…

Converting mutex_lock and mutex_unlock to guard(mutex) Simplify the locking logic by using guard(mutex).
This removes the need for explicit unlock calls in error paths and simplifies the code by removing goto labels.

Quoted 5 lines
Quoted 1 line

Hi Menderes,

Applied to the togreg branch of iio.git.

Dropped again. For some reason my local email hadn't picked up the rest of the discussion and I only noticed when I looked at patchwork.

Quoted 5 lines

Thanks,

Jonathan