[PATCH] gpio: max732x: use guard(mutex) to simplify locking

Richard Lyu posted 1 patch 3 weeks, 6 days ago
There is a newer version of this series
drivers/gpio/gpio-max732x.c | 11 ++++-------
1 file changed, 4 insertions(+), 7 deletions(-)
[PATCH] gpio: max732x: use guard(mutex) to simplify locking
Posted by Richard Lyu 3 weeks, 6 days ago
Convert the max732x driver to use the RAII-based guard(mutex) macro from
<linux/cleanup.h>. This change replaces manual mutex_lock() and
mutex_unlock() calls, allowing the chip lock to be managed automatically
based on function scope.

Refactor max732x_gpio_set_mask() and max732x_irq_update_mask() to
improve code readability. This allows for direct returns and removes
the redundant 'out' label in the set_mask function, resulting in
cleaner and more maintainable code.

Signed-off-by: Richard Lyu <richard.lyu@suse.com>
---
 drivers/gpio/gpio-max732x.c | 11 ++++-------
 1 file changed, 4 insertions(+), 7 deletions(-)

diff --git a/drivers/gpio/gpio-max732x.c b/drivers/gpio/gpio-max732x.c
index a61d670ceeda..8930f49576ed 100644
--- a/drivers/gpio/gpio-max732x.c
+++ b/drivers/gpio/gpio-max732x.c
@@ -18,6 +18,7 @@
 #include <linux/interrupt.h>
 #include <linux/i2c.h>
 #include <linux/platform_data/max732x.h>
+#include <linux/cleanup.h>
 
 /*
  * Each port of MAX732x (including MAX7319) falls into one of the
@@ -207,22 +208,20 @@ static void max732x_gpio_set_mask(struct gpio_chip *gc, unsigned off, int mask,
 	uint8_t reg_out;
 	int ret;
 
-	mutex_lock(&chip->lock);
+	guard(mutex)(&chip->lock);
 
 	reg_out = (off > 7) ? chip->reg_out[1] : chip->reg_out[0];
 	reg_out = (reg_out & ~mask) | (val & mask);
 
 	ret = max732x_writeb(chip, is_group_a(chip, off), reg_out);
 	if (ret < 0)
-		goto out;
+		return;
 
 	/* update the shadow register then */
 	if (off > 7)
 		chip->reg_out[1] = reg_out;
 	else
 		chip->reg_out[0] = reg_out;
-out:
-	mutex_unlock(&chip->lock);
 }
 
 static int max732x_gpio_set_value(struct gpio_chip *gc, unsigned int off,
@@ -329,7 +328,7 @@ static void max732x_irq_update_mask(struct max732x_chip *chip)
 	if (chip->irq_features == INT_NO_MASK)
 		return;
 
-	mutex_lock(&chip->lock);
+	guard(mutex)(&chip->lock);
 
 	switch (chip->irq_features) {
 	case INT_INDEP_MASK:
@@ -342,8 +341,6 @@ static void max732x_irq_update_mask(struct max732x_chip *chip)
 		max732x_writeb(chip, 1, (uint8_t)msg);
 		break;
 	}
-
-	mutex_unlock(&chip->lock);
 }
 
 static void max732x_irq_mask(struct irq_data *d)
-- 
2.51.0
Re: [PATCH] gpio: max732x: use guard(mutex) to simplify locking
Posted by Bartosz Golaszewski 3 weeks, 6 days ago
On Wed, 11 Mar 2026 04:12:12 +0100, Richard Lyu <richard.lyu@suse.com> said:
> Convert the max732x driver to use the RAII-based guard(mutex) macro from
> <linux/cleanup.h>. This change replaces manual mutex_lock() and
> mutex_unlock() calls, allowing the chip lock to be managed automatically
> based on function scope.
>
> Refactor max732x_gpio_set_mask() and max732x_irq_update_mask() to
> improve code readability. This allows for direct returns and removes
> the redundant 'out' label in the set_mask function, resulting in
> cleaner and more maintainable code.
>
> Signed-off-by: Richard Lyu <richard.lyu@suse.com>
> ---
>  drivers/gpio/gpio-max732x.c | 11 ++++-------
>  1 file changed, 4 insertions(+), 7 deletions(-)
>
> diff --git a/drivers/gpio/gpio-max732x.c b/drivers/gpio/gpio-max732x.c
> index a61d670ceeda..8930f49576ed 100644
> --- a/drivers/gpio/gpio-max732x.c
> +++ b/drivers/gpio/gpio-max732x.c
> @@ -18,6 +18,7 @@
>  #include <linux/interrupt.h>
>  #include <linux/i2c.h>
>  #include <linux/platform_data/max732x.h>
> +#include <linux/cleanup.h>
>

While touching this, could you please also order includes alphabetically and
add linux/mutex.h which seems to be missing?

Bart