diff mbox series

[5/6] iio: adc: ti-adc128s052: Simplify using guard(mutex)

Message ID 93a9d7ab74cd045949a2e2b6301f29c7d83d72ea.1742474322.git.mazziesaccount@gmail.com (mailing list archive)
State Changes Requested
Headers show
Series Support ROHM BD79104 ADC | expand

Commit Message

Matti Vaittinen March 31, 2025, 8:03 a.m. UTC
Error path in ADC reading function can be slighly simplified using the
guard(mutex). Do just that.

Also, document the mutex purpose while at it.

Signed-off-by: Matti Vaittinen <mazziesaccount@gmail.com>
---
 drivers/iio/adc/ti-adc128s052.c | 11 ++++-------
 1 file changed, 4 insertions(+), 7 deletions(-)

Comments

Jonathan Cameron March 31, 2025, 11:14 a.m. UTC | #1
On Mon, 31 Mar 2025 11:03:44 +0300
Matti Vaittinen <mazziesaccount@gmail.com> wrote:

> Error path in ADC reading function can be slighly simplified using the
> guard(mutex). Do just that.
> 
> Also, document the mutex purpose while at it.
Ah. I should have read on before earlier comment!

That's what I get for an efficient linear pass of patches :)
> 
> Signed-off-by: Matti Vaittinen <mazziesaccount@gmail.com>
> ---
>  drivers/iio/adc/ti-adc128s052.c | 11 ++++-------
>  1 file changed, 4 insertions(+), 7 deletions(-)
> 
> diff --git a/drivers/iio/adc/ti-adc128s052.c b/drivers/iio/adc/ti-adc128s052.c
> index 90b23c68daea..c68ee4e17a03 100644
> --- a/drivers/iio/adc/ti-adc128s052.c
> +++ b/drivers/iio/adc/ti-adc128s052.c
> @@ -9,6 +9,7 @@
>   * https://www.ti.com/lit/ds/symlink/adc124s021.pdf
>   */
>  
> +#include <linux/cleanup.h>
>  #include <linux/err.h>
>  #include <linux/iio/iio.h>
>  #include <linux/mod_devicetable.h>
> @@ -26,6 +27,7 @@ struct adc128 {
>  	struct spi_device *spi;
>  
>  	struct regulator *reg;
> +	/* Serialize the SPI 'write-channel + read data' accesses */
>  	struct mutex lock;
>  
>  	__be16 buffer __aligned(IIO_DMA_MINALIGN);
> @@ -39,18 +41,13 @@ static int adc128_adc_conversion(struct adc128 *adc, u8 channel)
>  	msg[0] = channel << 3;
>  	msg[1] = 0;
>  
> -	mutex_lock(&adc->lock);
> +	guard(mutex)(&adc->lock);
As per earlier comment, I think you need to protect msg as well.

>  
>  	ret = spi_write(adc->spi, msg, 2);
> -	if (ret < 0) {
> -		mutex_unlock(&adc->lock);
> +	if (ret < 0)
>  		return ret;
> -	}
>  
>  	ret = spi_read(adc->spi, &adc->buffer, 2);
> -
> -	mutex_unlock(&adc->lock);
> -
>  	if (ret < 0)
>  		return ret;
>
diff mbox series

Patch

diff --git a/drivers/iio/adc/ti-adc128s052.c b/drivers/iio/adc/ti-adc128s052.c
index 90b23c68daea..c68ee4e17a03 100644
--- a/drivers/iio/adc/ti-adc128s052.c
+++ b/drivers/iio/adc/ti-adc128s052.c
@@ -9,6 +9,7 @@ 
  * https://www.ti.com/lit/ds/symlink/adc124s021.pdf
  */
 
+#include <linux/cleanup.h>
 #include <linux/err.h>
 #include <linux/iio/iio.h>
 #include <linux/mod_devicetable.h>
@@ -26,6 +27,7 @@  struct adc128 {
 	struct spi_device *spi;
 
 	struct regulator *reg;
+	/* Serialize the SPI 'write-channel + read data' accesses */
 	struct mutex lock;
 
 	__be16 buffer __aligned(IIO_DMA_MINALIGN);
@@ -39,18 +41,13 @@  static int adc128_adc_conversion(struct adc128 *adc, u8 channel)
 	msg[0] = channel << 3;
 	msg[1] = 0;
 
-	mutex_lock(&adc->lock);
+	guard(mutex)(&adc->lock);
 
 	ret = spi_write(adc->spi, msg, 2);
-	if (ret < 0) {
-		mutex_unlock(&adc->lock);
+	if (ret < 0)
 		return ret;
-	}
 
 	ret = spi_read(adc->spi, &adc->buffer, 2);
-
-	mutex_unlock(&adc->lock);
-
 	if (ret < 0)
 		return ret;