]> git.ipfire.org Git - thirdparty/kernel/linux.git/commitdiff
Input: adp5589-keys - use guard notation when acquiring mutex
authorDmitry Torokhov <dmitry.torokhov@gmail.com>
Sun, 25 Aug 2024 05:16:05 +0000 (22:16 -0700)
committerDmitry Torokhov <dmitry.torokhov@gmail.com>
Thu, 3 Oct 2024 15:52:33 +0000 (08:52 -0700)
This makes the code more compact and error handling more robust
by ensuring that mutexes are released in all code paths when control
leaves critical section.

Link: https://lore.kernel.org/r/20240825051627.2848495-2-dmitry.torokhov@gmail.com
Signed-off-by: Dmitry Torokhov <dmitry.torokhov@gmail.com>
drivers/input/keyboard/adp5589-keys.c

index 8996e00cd63a8203ec53d46ccb922c21ddb47355..735d96b056d40b7050ce76409785a7f61a20351c 100644 (file)
@@ -404,7 +404,7 @@ static void adp5589_gpio_set_value(struct gpio_chip *chip,
        unsigned int bank = kpad->var->bank(kpad->gpiomap[off]);
        unsigned int bit = kpad->var->bit(kpad->gpiomap[off]);
 
-       mutex_lock(&kpad->gpio_lock);
+       guard(mutex)(&kpad->gpio_lock);
 
        if (val)
                kpad->dat_out[bank] |= bit;
@@ -413,8 +413,6 @@ static void adp5589_gpio_set_value(struct gpio_chip *chip,
 
        adp5589_write(kpad->client, kpad->var->reg(ADP5589_GPO_DATA_OUT_A) +
                      bank, kpad->dat_out[bank]);
-
-       mutex_unlock(&kpad->gpio_lock);
 }
 
 static int adp5589_gpio_direction_input(struct gpio_chip *chip, unsigned off)
@@ -422,18 +420,13 @@ static int adp5589_gpio_direction_input(struct gpio_chip *chip, unsigned off)
        struct adp5589_kpad *kpad = gpiochip_get_data(chip);
        unsigned int bank = kpad->var->bank(kpad->gpiomap[off]);
        unsigned int bit = kpad->var->bit(kpad->gpiomap[off]);
-       int ret;
 
-       mutex_lock(&kpad->gpio_lock);
+       guard(mutex)(&kpad->gpio_lock);
 
        kpad->dir[bank] &= ~bit;
-       ret = adp5589_write(kpad->client,
-                           kpad->var->reg(ADP5589_GPIO_DIRECTION_A) + bank,
-                           kpad->dir[bank]);
-
-       mutex_unlock(&kpad->gpio_lock);
-
-       return ret;
+       return adp5589_write(kpad->client,
+                            kpad->var->reg(ADP5589_GPIO_DIRECTION_A) + bank,
+                            kpad->dir[bank]);
 }
 
 static int adp5589_gpio_direction_output(struct gpio_chip *chip,
@@ -442,9 +435,9 @@ static int adp5589_gpio_direction_output(struct gpio_chip *chip,
        struct adp5589_kpad *kpad = gpiochip_get_data(chip);
        unsigned int bank = kpad->var->bank(kpad->gpiomap[off]);
        unsigned int bit = kpad->var->bit(kpad->gpiomap[off]);
-       int ret;
+       int error;
 
-       mutex_lock(&kpad->gpio_lock);
+       guard(mutex)(&kpad->gpio_lock);
 
        kpad->dir[bank] |= bit;
 
@@ -453,15 +446,19 @@ static int adp5589_gpio_direction_output(struct gpio_chip *chip,
        else
                kpad->dat_out[bank] &= ~bit;
 
-       ret = adp5589_write(kpad->client, kpad->var->reg(ADP5589_GPO_DATA_OUT_A)
-                           + bank, kpad->dat_out[bank]);
-       ret |= adp5589_write(kpad->client,
-                            kpad->var->reg(ADP5589_GPIO_DIRECTION_A) + bank,
-                            kpad->dir[bank]);
+       error = adp5589_write(kpad->client,
+                             kpad->var->reg(ADP5589_GPO_DATA_OUT_A) + bank,
+                             kpad->dat_out[bank]);
+       if (error)
+               return error;
 
-       mutex_unlock(&kpad->gpio_lock);
+       error = adp5589_write(kpad->client,
+                             kpad->var->reg(ADP5589_GPIO_DIRECTION_A) + bank,
+                             kpad->dir[bank]);
+       if (error)
+               return error;
 
-       return ret;
+       return 0;
 }
 
 static int adp5589_build_gpiomap(struct adp5589_kpad *kpad,