Linux kernel mirror (for testing) git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git
kernel os linux

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>

authored by

Richard Lyu and committed by
Bartosz Golaszewski
8a361389 696e9ba9

+12 -12
+12 -12
drivers/gpio/gpio-max732x.c
··· 10 10 * Derived from drivers/gpio/pca953x.c 11 11 */ 12 12 13 - #include <linux/module.h> 13 + #include <linux/cleanup.h> 14 + #include <linux/err.h> 15 + #include <linux/device.h> 16 + #include <linux/gpio/driver.h> 17 + #include <linux/i2c.h> 14 18 #include <linux/init.h> 19 + #include <linux/interrupt.h> 20 + #include <linux/module.h> 21 + #include <linux/mutex.h> 22 + #include <linux/platform_data/max732x.h> 15 23 #include <linux/slab.h> 16 24 #include <linux/string.h> 17 - #include <linux/gpio/driver.h> 18 - #include <linux/interrupt.h> 19 - #include <linux/i2c.h> 20 - #include <linux/platform_data/max732x.h> 21 25 22 26 /* 23 27 * Each port of MAX732x (including MAX7319) falls into one of the ··· 211 207 uint8_t reg_out; 212 208 int ret; 213 209 214 - mutex_lock(&chip->lock); 210 + guard(mutex)(&chip->lock); 215 211 216 212 reg_out = (off > 7) ? chip->reg_out[1] : chip->reg_out[0]; 217 213 reg_out = (reg_out & ~mask) | (val & mask); 218 214 219 215 ret = max732x_writeb(chip, is_group_a(chip, off), reg_out); 220 216 if (ret < 0) 221 - goto out; 217 + return; 222 218 223 219 /* update the shadow register then */ 224 220 if (off > 7) 225 221 chip->reg_out[1] = reg_out; 226 222 else 227 223 chip->reg_out[0] = reg_out; 228 - out: 229 - mutex_unlock(&chip->lock); 230 224 } 231 225 232 226 static int max732x_gpio_set_value(struct gpio_chip *gc, unsigned int off, ··· 331 329 if (chip->irq_features == INT_NO_MASK) 332 330 return; 333 331 334 - mutex_lock(&chip->lock); 332 + guard(mutex)(&chip->lock); 335 333 336 334 switch (chip->irq_features) { 337 335 case INT_INDEP_MASK: ··· 344 342 max732x_writeb(chip, 1, (uint8_t)msg); 345 343 break; 346 344 } 347 - 348 - mutex_unlock(&chip->lock); 349 345 } 350 346 351 347 static void max732x_irq_mask(struct irq_data *d)