2023-11-27 07:34:52

by Marcus Folkesson

[permalink] [raw]
Subject: [PATCH v2] iio: adc: mcp3911: simplify code with guard macro

Use the guard(mutex) macro for handle mutex lock/unlocks.

Signed-off-by: Marcus Folkesson <[email protected]>
---
Changes in v2:
- Return directly instead of goto label
- Link to v1: https://lore.kernel.org/r/[email protected]
---
drivers/iio/adc/mcp3911.c | 27 ++++++++++-----------------
1 file changed, 10 insertions(+), 17 deletions(-)

diff --git a/drivers/iio/adc/mcp3911.c b/drivers/iio/adc/mcp3911.c
index 974c5bd923a6..30836725ef9a 100644
--- a/drivers/iio/adc/mcp3911.c
+++ b/drivers/iio/adc/mcp3911.c
@@ -7,6 +7,7 @@
*/
#include <linux/bitfield.h>
#include <linux/bits.h>
+#include <linux/cleanup.h>
#include <linux/clk.h>
#include <linux/delay.h>
#include <linux/err.h>
@@ -168,13 +169,13 @@ static int mcp3911_read_raw(struct iio_dev *indio_dev,
struct mcp3911 *adc = iio_priv(indio_dev);
int ret = -EINVAL;

- mutex_lock(&adc->lock);
+ guard(mutex)(&adc->lock);
switch (mask) {
case IIO_CHAN_INFO_RAW:
ret = mcp3911_read(adc,
MCP3911_CHANNEL(channel->channel), val, 3);
if (ret)
- goto out;
+ return ret;

*val = sign_extend32(*val, 23);

@@ -185,14 +186,14 @@ static int mcp3911_read_raw(struct iio_dev *indio_dev,
ret = mcp3911_read(adc,
MCP3911_OFFCAL(channel->channel), val, 3);
if (ret)
- goto out;
+ return ret;

ret = IIO_VAL_INT;
break;
case IIO_CHAN_INFO_OVERSAMPLING_RATIO:
ret = mcp3911_read(adc, MCP3911_REG_CONFIG, val, 2);
if (ret)
- goto out;
+ return ret;

*val = FIELD_GET(MCP3911_CONFIG_OSR, *val);
*val = 32 << *val;
@@ -206,8 +207,6 @@ static int mcp3911_read_raw(struct iio_dev *indio_dev,
break;
}

-out:
- mutex_unlock(&adc->lock);
return ret;
}

@@ -218,7 +217,7 @@ static int mcp3911_write_raw(struct iio_dev *indio_dev,
struct mcp3911 *adc = iio_priv(indio_dev);
int ret = -EINVAL;

- mutex_lock(&adc->lock);
+ guard(mutex)(&adc->lock);
switch (mask) {
case IIO_CHAN_INFO_SCALE:
for (int i = 0; i < MCP3911_NUM_SCALES; i++) {
@@ -233,16 +232,14 @@ static int mcp3911_write_raw(struct iio_dev *indio_dev,
}
break;
case IIO_CHAN_INFO_OFFSET:
- if (val2 != 0) {
- ret = -EINVAL;
- goto out;
- }
+ if (val2 != 0)
+ return -EINVAL;

/* Write offset */
ret = mcp3911_write(adc, MCP3911_OFFCAL(channel->channel), val,
3);
if (ret)
- goto out;
+ return ret;

/* Enable offset*/
ret = mcp3911_update(adc, MCP3911_REG_STATUSCOM,
@@ -261,9 +258,6 @@ static int mcp3911_write_raw(struct iio_dev *indio_dev,
}
break;
}
-
-out:
- mutex_unlock(&adc->lock);
return ret;
}

@@ -350,7 +344,7 @@ static irqreturn_t mcp3911_trigger_handler(int irq, void *p)
int i = 0;
int ret;

- mutex_lock(&adc->lock);
+ guard(mutex)(&adc->lock);
adc->tx_buf = MCP3911_REG_READ(MCP3911_CHANNEL(0), adc->dev_addr);
ret = spi_sync_transfer(adc->spi, xfer, ARRAY_SIZE(xfer));
if (ret < 0) {
@@ -368,7 +362,6 @@ static irqreturn_t mcp3911_trigger_handler(int irq, void *p)
iio_push_to_buffers_with_timestamp(indio_dev, &adc->scan,
iio_get_time_ns(indio_dev));
out:
- mutex_unlock(&adc->lock);
iio_trigger_notify_done(indio_dev->trig);

return IRQ_HANDLED;

---
base-commit: ffc253263a1375a65fa6c9f62a893e9767fbebfa
change-id: 20231125-mcp3911-guard-866591e2c947

Best regards,
--
Marcus Folkesson <[email protected]>


2023-12-04 12:23:30

by Jonathan Cameron

[permalink] [raw]
Subject: Re: [PATCH v2] iio: adc: mcp3911: simplify code with guard macro

On Mon, 27 Nov 2023 08:38:22 +0100
Marcus Folkesson <[email protected]> wrote:

> Use the guard(mutex) macro for handle mutex lock/unlocks.
>
> Signed-off-by: Marcus Folkesson <[email protected]>

Oddly this doesn't apply. Also some comments inline.
> ---
> Changes in v2:
> - Return directly instead of goto label
> - Link to v1: https://lore.kernel.org/r/[email protected]
> ---
> drivers/iio/adc/mcp3911.c | 27 ++++++++++-----------------
> 1 file changed, 10 insertions(+), 17 deletions(-)
>
> diff --git a/drivers/iio/adc/mcp3911.c b/drivers/iio/adc/mcp3911.c
> index 974c5bd923a6..30836725ef9a 100644
> --- a/drivers/iio/adc/mcp3911.c
> +++ b/drivers/iio/adc/mcp3911.c
> @@ -7,6 +7,7 @@
> */
> #include <linux/bitfield.h>
> #include <linux/bits.h>
> +#include <linux/cleanup.h>
> #include <linux/clk.h>
> #include <linux/delay.h>
> #include <linux/err.h>
> @@ -168,13 +169,13 @@ static int mcp3911_read_raw(struct iio_dev *indio_dev,
> struct mcp3911 *adc = iio_priv(indio_dev);
> int ret = -EINVAL;
>
> - mutex_lock(&adc->lock);
> + guard(mutex)(&adc->lock);
> switch (mask) {
> case IIO_CHAN_INFO_RAW:
> ret = mcp3911_read(adc,
> MCP3911_CHANNEL(channel->channel), val, 3);
> if (ret)
> - goto out;
> + return ret;
>
> *val = sign_extend32(*val, 23);
This should go further. Return early in the good paths as well
return IIO_VAL_INT;
a few lines later than this.