Skip to content

Commit 50faedd

Browse files
Bartosz Golaszewskibroonie
authored andcommitted
regulator: rpi-panel-attiny: use lock guards for the state mutex
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 <[email protected]> Link: https://patch.msgid.link/[email protected] Signed-off-by: Mark Brown <[email protected]>
1 parent 1326e29 commit 50faedd

File tree

1 file changed

+14
-23
lines changed

1 file changed

+14
-23
lines changed

drivers/regulator/rpi-panel-attiny-regulator.c

Lines changed: 14 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
*/
77

88
#include <linux/backlight.h>
9+
#include <linux/cleanup.h>
910
#include <linux/err.h>
1011
#include <linux/gpio/driver.h>
1112
#include <linux/i2c.h>
@@ -94,7 +95,7 @@ static int attiny_lcd_power_enable(struct regulator_dev *rdev)
9495
{
9596
struct attiny_lcd *state = rdev_get_drvdata(rdev);
9697

97-
mutex_lock(&state->lock);
98+
guard(mutex)(&state->lock);
9899

99100
/* Ensure bridge, and tp stay in reset */
100101
attiny_set_port_state(state, REG_PORTC, 0);
@@ -115,16 +116,14 @@ static int attiny_lcd_power_enable(struct regulator_dev *rdev)
115116

116117
msleep(80);
117118

118-
mutex_unlock(&state->lock);
119-
120119
return 0;
121120
}
122121

123122
static int attiny_lcd_power_disable(struct regulator_dev *rdev)
124123
{
125124
struct attiny_lcd *state = rdev_get_drvdata(rdev);
126125

127-
mutex_lock(&state->lock);
126+
guard(mutex)(&state->lock);
128127

129128
regmap_write(rdev->regmap, REG_PWM, 0);
130129
usleep_range(5000, 10000);
@@ -136,28 +135,24 @@ static int attiny_lcd_power_disable(struct regulator_dev *rdev)
136135
attiny_set_port_state(state, REG_PORTC, 0);
137136
msleep(30);
138137

139-
mutex_unlock(&state->lock);
140-
141138
return 0;
142139
}
143140

144141
static int attiny_lcd_power_is_enabled(struct regulator_dev *rdev)
145142
{
146143
struct attiny_lcd *state = rdev_get_drvdata(rdev);
147144
unsigned int data;
148-
int ret, i;
149-
150-
mutex_lock(&state->lock);
151-
152-
for (i = 0; i < 10; i++) {
153-
ret = regmap_read(rdev->regmap, REG_PORTC, &data);
154-
if (!ret)
155-
break;
156-
usleep_range(10000, 12000);
145+
int ret = 0, i;
146+
147+
scoped_guard(mutex, &state->lock) {
148+
for (i = 0; i < 10; i++) {
149+
ret = regmap_read(rdev->regmap, REG_PORTC, &data);
150+
if (!ret)
151+
break;
152+
usleep_range(10000, 12000);
153+
}
157154
}
158155

159-
mutex_unlock(&state->lock);
160-
161156
if (ret < 0)
162157
return ret;
163158

@@ -190,16 +185,14 @@ static int attiny_update_status(struct backlight_device *bl)
190185
int brightness = backlight_get_brightness(bl);
191186
int ret, i;
192187

193-
mutex_lock(&state->lock);
188+
guard(mutex)(&state->lock);
194189

195190
for (i = 0; i < 10; i++) {
196191
ret = regmap_write(regmap, REG_PWM, brightness);
197192
if (!ret)
198193
break;
199194
}
200195

201-
mutex_unlock(&state->lock);
202-
203196
return ret;
204197
}
205198

@@ -217,7 +210,7 @@ static void attiny_gpio_set(struct gpio_chip *gc, unsigned int off, int val)
217210
struct attiny_lcd *state = gpiochip_get_data(gc);
218211
u8 last_val;
219212

220-
mutex_lock(&state->lock);
213+
guard(mutex)(&state->lock);
221214

222215
last_val = attiny_get_port_state(state, mappings[off].reg);
223216
if (val)
@@ -239,8 +232,6 @@ static void attiny_gpio_set(struct gpio_chip *gc, unsigned int off, int val)
239232

240233
msleep(100);
241234
}
242-
243-
mutex_unlock(&state->lock);
244235
}
245236

246237
static int attiny_i2c_read(struct i2c_client *client, u8 reg, unsigned int *buf)

0 commit comments

Comments
 (0)