Skip to content

Commit 8a36138

Browse files
r1chard-lyuBartosz Golaszewski
authored andcommitted
gpio: max732x: use guard(mutex) to simplify locking
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. While at it: order includes alphabetically and add missing ones. Signed-off-by: Richard Lyu <richard.lyu@suse.com> Link: https://patch.msgid.link/20260311085924.191288-1-richard.lyu@suse.com [Bartosz: tweak commit message, add err.h and device.h to includes] Signed-off-by: Bartosz Golaszewski <bartosz.golaszewski@oss.qualcomm.com>
1 parent 696e9ba commit 8a36138

1 file changed

Lines changed: 12 additions & 12 deletions

File tree

drivers/gpio/gpio-max732x.c

Lines changed: 12 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -10,14 +10,18 @@
1010
* Derived from drivers/gpio/pca953x.c
1111
*/
1212

13-
#include <linux/module.h>
14-
#include <linux/init.h>
15-
#include <linux/slab.h>
16-
#include <linux/string.h>
13+
#include <linux/cleanup.h>
14+
#include <linux/err.h>
15+
#include <linux/device.h>
1716
#include <linux/gpio/driver.h>
18-
#include <linux/interrupt.h>
1917
#include <linux/i2c.h>
18+
#include <linux/init.h>
19+
#include <linux/interrupt.h>
20+
#include <linux/module.h>
21+
#include <linux/mutex.h>
2022
#include <linux/platform_data/max732x.h>
23+
#include <linux/slab.h>
24+
#include <linux/string.h>
2125

2226
/*
2327
* Each port of MAX732x (including MAX7319) falls into one of the
@@ -207,22 +211,20 @@ static void max732x_gpio_set_mask(struct gpio_chip *gc, unsigned off, int mask,
207211
uint8_t reg_out;
208212
int ret;
209213

210-
mutex_lock(&chip->lock);
214+
guard(mutex)(&chip->lock);
211215

212216
reg_out = (off > 7) ? chip->reg_out[1] : chip->reg_out[0];
213217
reg_out = (reg_out & ~mask) | (val & mask);
214218

215219
ret = max732x_writeb(chip, is_group_a(chip, off), reg_out);
216220
if (ret < 0)
217-
goto out;
221+
return;
218222

219223
/* update the shadow register then */
220224
if (off > 7)
221225
chip->reg_out[1] = reg_out;
222226
else
223227
chip->reg_out[0] = reg_out;
224-
out:
225-
mutex_unlock(&chip->lock);
226228
}
227229

228230
static int max732x_gpio_set_value(struct gpio_chip *gc, unsigned int off,
@@ -329,7 +331,7 @@ static void max732x_irq_update_mask(struct max732x_chip *chip)
329331
if (chip->irq_features == INT_NO_MASK)
330332
return;
331333

332-
mutex_lock(&chip->lock);
334+
guard(mutex)(&chip->lock);
333335

334336
switch (chip->irq_features) {
335337
case INT_INDEP_MASK:
@@ -342,8 +344,6 @@ static void max732x_irq_update_mask(struct max732x_chip *chip)
342344
max732x_writeb(chip, 1, (uint8_t)msg);
343345
break;
344346
}
345-
346-
mutex_unlock(&chip->lock);
347347
}
348348

349349
static void max732x_irq_mask(struct irq_data *d)

0 commit comments

Comments
 (0)