regulator: rpi-panel-attiny: use lock guards for the state mutex
authorBartosz Golaszewski <bartosz.golaszewski@linaro.org>
Tue, 8 Apr 2025 07:36:30 +0000 (09:36 +0200)
committerMark Brown <broonie@kernel.org>
Tue, 8 Apr 2025 11:05:42 +0000 (12:05 +0100)
Use mutex lock guards from linux/cleanup.h to simplify the driver code.

Note that ret must be initialized in order to avoid a build warning as
scoped_guard() is implemented as a for loop.

Signed-off-by: Bartosz Golaszewski <bartosz.golaszewski@linaro.org>
Link: https://patch.msgid.link/20250408-gpiochip-set-rv-regulator-v1-3-a18b6154b31a@linaro.org
Signed-off-by: Mark Brown <broonie@kernel.org>
drivers/regulator/rpi-panel-attiny-regulator.c

index 5ff34893956090d6d488c426dc5227fc9dda4dc9..43a7c4737cb43d3905c1a26592a8fd0741785202 100644 (file)
@@ -6,6 +6,7 @@
  */
 
 #include <linux/backlight.h>
+#include <linux/cleanup.h>
 #include <linux/err.h>
 #include <linux/gpio/driver.h>
 #include <linux/i2c.h>
@@ -94,7 +95,7 @@ static int attiny_lcd_power_enable(struct regulator_dev *rdev)
 {
        struct attiny_lcd *state = rdev_get_drvdata(rdev);
 
-       mutex_lock(&state->lock);
+       guard(mutex)(&state->lock);
 
        /* Ensure bridge, and tp stay in reset */
        attiny_set_port_state(state, REG_PORTC, 0);
@@ -115,8 +116,6 @@ static int attiny_lcd_power_enable(struct regulator_dev *rdev)
 
        msleep(80);
 
-       mutex_unlock(&state->lock);
-
        return 0;
 }
 
@@ -124,7 +123,7 @@ static int attiny_lcd_power_disable(struct regulator_dev *rdev)
 {
        struct attiny_lcd *state = rdev_get_drvdata(rdev);
 
-       mutex_lock(&state->lock);
+       guard(mutex)(&state->lock);
 
        regmap_write(rdev->regmap, REG_PWM, 0);
        usleep_range(5000, 10000);
@@ -136,8 +135,6 @@ static int attiny_lcd_power_disable(struct regulator_dev *rdev)
        attiny_set_port_state(state, REG_PORTC, 0);
        msleep(30);
 
-       mutex_unlock(&state->lock);
-
        return 0;
 }
 
@@ -145,19 +142,17 @@ static int attiny_lcd_power_is_enabled(struct regulator_dev *rdev)
 {
        struct attiny_lcd *state = rdev_get_drvdata(rdev);
        unsigned int data;
-       int ret, i;
-
-       mutex_lock(&state->lock);
-
-       for (i = 0; i < 10; i++) {
-               ret = regmap_read(rdev->regmap, REG_PORTC, &data);
-               if (!ret)
-                       break;
-               usleep_range(10000, 12000);
+       int ret = 0, i;
+
+       scoped_guard(mutex, &state->lock) {
+               for (i = 0; i < 10; i++) {
+                       ret = regmap_read(rdev->regmap, REG_PORTC, &data);
+                       if (!ret)
+                               break;
+                       usleep_range(10000, 12000);
+               }
        }
 
-       mutex_unlock(&state->lock);
-
        if (ret < 0)
                return ret;
 
@@ -190,7 +185,7 @@ static int attiny_update_status(struct backlight_device *bl)
        int brightness = backlight_get_brightness(bl);
        int ret, i;
 
-       mutex_lock(&state->lock);
+       guard(mutex)(&state->lock);
 
        for (i = 0; i < 10; i++) {
                ret = regmap_write(regmap, REG_PWM, brightness);
@@ -198,8 +193,6 @@ static int attiny_update_status(struct backlight_device *bl)
                        break;
        }
 
-       mutex_unlock(&state->lock);
-
        return ret;
 }
 
@@ -217,7 +210,7 @@ static void attiny_gpio_set(struct gpio_chip *gc, unsigned int off, int val)
        struct attiny_lcd *state = gpiochip_get_data(gc);
        u8 last_val;
 
-       mutex_lock(&state->lock);
+       guard(mutex)(&state->lock);
 
        last_val = attiny_get_port_state(state, mappings[off].reg);
        if (val)
@@ -239,8 +232,6 @@ static void attiny_gpio_set(struct gpio_chip *gc, unsigned int off, int val)
 
                msleep(100);
        }
-
-       mutex_unlock(&state->lock);
 }
 
 static int attiny_i2c_read(struct i2c_client *client, u8 reg, unsigned int *buf)