drivers/gpio/gpio-max732x.c | 22 ++++++++++------------ 1 file changed, 10 insertions(+), 12 deletions(-)
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>
---
v2:
- Sort includes alphabetically as requested by Bartosz.
- Add missing include <linux/mutex.h>.
- Link: https://lore.kernel.org/all/CAMRc=MfioFeJD=s_w0PQ2jD4npHr9y7nMhu3bXUwrr0D8roPgQ@mail.gmail.com/
---
drivers/gpio/gpio-max732x.c | 22 ++++++++++------------
1 file changed, 10 insertions(+), 12 deletions(-)
diff --git a/drivers/gpio/gpio-max732x.c b/drivers/gpio/gpio-max732x.c
index a61d670ceeda..257742f95949 100644
--- a/drivers/gpio/gpio-max732x.c
+++ b/drivers/gpio/gpio-max732x.c
@@ -10,14 +10,16 @@
* Derived from drivers/gpio/pca953x.c
*/
-#include <linux/module.h>
-#include <linux/init.h>
-#include <linux/slab.h>
-#include <linux/string.h>
+#include <linux/cleanup.h>
#include <linux/gpio/driver.h>
-#include <linux/interrupt.h>
#include <linux/i2c.h>
+#include <linux/init.h>
+#include <linux/interrupt.h>
+#include <linux/module.h>
+#include <linux/mutex.h>
#include <linux/platform_data/max732x.h>
+#include <linux/slab.h>
+#include <linux/string.h>
/*
* Each port of MAX732x (including MAX7319) falls into one of the
@@ -207,22 +209,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 +329,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 +342,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
On Wed, 11 Mar 2026 16:59:26 +0800, Richard Lyu wrote:
> 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.
>
> [...]
Applied, thanks!
[1/1] gpio: max732x: use guard(mutex) to simplify locking
https://git.kernel.org/brgl/c/8a3613898ff3b7eb9eed252f41aebcc1d7af4a31
Best regards,
--
Bartosz Golaszewski <bartosz.golaszewski@oss.qualcomm.com>
© 2016 - 2026 Red Hat, Inc.