Skip to content

Commit 4137d94

Browse files
Ansuellag-linaro
authored andcommitted
leds: leds-lp55xx: Convert mutex lock/unlock to guard API
Convert any entry of mutex lock/unlock to guard API and simplify code. With the use of guard API, handling for selttest functions can be greatly simplified. Suggested-by: Markus Elfring <[email protected]> Signed-off-by: Christian Marangi <[email protected]> Link: https://lore.kernel.org/r/[email protected] Signed-off-by: Lee Jones <[email protected]>
1 parent 8eac037 commit 4137d94

File tree

5 files changed

+64
-92
lines changed

5 files changed

+64
-92
lines changed

drivers/leds/leds-lp5521.c

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
* Milo(Woogyom) Kim <[email protected]>
1010
*/
1111

12+
#include <linux/cleanup.h>
1213
#include <linux/delay.h>
1314
#include <linux/firmware.h>
1415
#include <linux/i2c.h>
@@ -185,9 +186,9 @@ static ssize_t lp5521_selftest(struct device *dev,
185186
struct lp55xx_chip *chip = led->chip;
186187
int ret;
187188

188-
mutex_lock(&chip->lock);
189+
guard(mutex)(&chip->lock);
190+
189191
ret = lp5521_run_selftest(chip, buf);
190-
mutex_unlock(&chip->lock);
191192

192193
return sysfs_emit(buf, "%s\n", ret ? "FAIL" : "OK");
193194
}

drivers/leds/leds-lp5523.c

Lines changed: 9 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
* Milo(Woogyom) Kim <[email protected]>
1010
*/
1111

12+
#include <linux/cleanup.h>
1213
#include <linux/delay.h>
1314
#include <linux/firmware.h>
1415
#include <linux/i2c.h>
@@ -188,31 +189,31 @@ static ssize_t lp5523_selftest(struct device *dev,
188189
int ret, pos = 0;
189190
u8 status, adc, vdd, i;
190191

191-
mutex_lock(&chip->lock);
192+
guard(mutex)(&chip->lock);
192193

193194
ret = lp55xx_read(chip, LP5523_REG_STATUS, &status);
194195
if (ret < 0)
195-
goto fail;
196+
return sysfs_emit(buf, "FAIL\n");
196197

197198
/* Check that ext clock is really in use if requested */
198199
if (pdata->clock_mode == LP55XX_CLOCK_EXT) {
199200
if ((status & LP5523_EXT_CLK_USED) == 0)
200-
goto fail;
201+
return sysfs_emit(buf, "FAIL\n");
201202
}
202203

203204
/* Measure VDD (i.e. VBAT) first (channel 16 corresponds to VDD) */
204205
lp55xx_write(chip, LP5523_REG_LED_TEST_CTRL, LP5523_EN_LEDTEST | 16);
205206
usleep_range(3000, 6000); /* ADC conversion time is typically 2.7 ms */
206207
ret = lp55xx_read(chip, LP5523_REG_STATUS, &status);
207208
if (ret < 0)
208-
goto fail;
209+
return sysfs_emit(buf, "FAIL\n");
209210

210211
if (!(status & LP5523_LEDTEST_DONE))
211212
usleep_range(3000, 6000); /* Was not ready. Wait little bit */
212213

213214
ret = lp55xx_read(chip, LP5523_REG_LED_TEST_ADC, &vdd);
214215
if (ret < 0)
215-
goto fail;
216+
return sysfs_emit(buf, "FAIL\n");
216217

217218
vdd--; /* There may be some fluctuation in measurement */
218219

@@ -235,14 +236,14 @@ static ssize_t lp5523_selftest(struct device *dev,
235236
usleep_range(3000, 6000);
236237
ret = lp55xx_read(chip, LP5523_REG_STATUS, &status);
237238
if (ret < 0)
238-
goto fail;
239+
return sysfs_emit(buf, "FAIL\n");
239240

240241
if (!(status & LP5523_LEDTEST_DONE))
241242
usleep_range(3000, 6000); /* Was not ready. Wait. */
242243

243244
ret = lp55xx_read(chip, LP5523_REG_LED_TEST_ADC, &adc);
244245
if (ret < 0)
245-
goto fail;
246+
return sysfs_emit(buf, "FAIL\n");
246247

247248
if (adc >= vdd || adc < LP5523_ADC_SHORTCIRC_LIM)
248249
pos += sysfs_emit_at(buf, pos, "LED %d FAIL\n",
@@ -256,16 +257,8 @@ static ssize_t lp5523_selftest(struct device *dev,
256257
led->led_current);
257258
led++;
258259
}
259-
if (pos == 0)
260-
pos = sysfs_emit(buf, "OK\n");
261-
goto release_lock;
262-
fail:
263-
pos = sysfs_emit(buf, "FAIL\n");
264260

265-
release_lock:
266-
mutex_unlock(&chip->lock);
267-
268-
return pos;
261+
return pos == 0 ? sysfs_emit(buf, "OK\n") : pos;
269262
}
270263

271264
LP55XX_DEV_ATTR_ENGINE_MODE(1);

drivers/leds/leds-lp5562.c

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
* Author: Milo(Woogyom) Kim <[email protected]>
88
*/
99

10+
#include <linux/cleanup.h>
1011
#include <linux/delay.h>
1112
#include <linux/firmware.h>
1213
#include <linux/i2c.h>
@@ -171,9 +172,9 @@ static int lp5562_led_brightness(struct lp55xx_led *led)
171172
};
172173
int ret;
173174

174-
mutex_lock(&chip->lock);
175+
guard(mutex)(&chip->lock);
176+
175177
ret = lp55xx_write(chip, addr[led->chan_nr], led->brightness);
176-
mutex_unlock(&chip->lock);
177178

178179
return ret;
179180
}
@@ -268,9 +269,9 @@ static ssize_t lp5562_store_pattern(struct device *dev,
268269
if (mode > num_patterns || !ptn)
269270
return -EINVAL;
270271

271-
mutex_lock(&chip->lock);
272+
guard(mutex)(&chip->lock);
273+
272274
ret = lp5562_run_predef_led_pattern(chip, mode);
273-
mutex_unlock(&chip->lock);
274275

275276
if (ret)
276277
return ret;
@@ -320,9 +321,9 @@ static ssize_t lp5562_store_engine_mux(struct device *dev,
320321
return -EINVAL;
321322
}
322323

323-
mutex_lock(&chip->lock);
324+
guard(mutex)(&chip->lock);
325+
324326
lp55xx_update_bits(chip, LP5562_REG_ENG_SEL, mask, val);
325-
mutex_unlock(&chip->lock);
326327

327328
return len;
328329
}

drivers/leds/leds-lp5569.c

Lines changed: 5 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
*/
55

66
#include <linux/bitfield.h>
7+
#include <linux/cleanup.h>
78
#include <linux/delay.h>
89
#include <linux/firmware.h>
910
#include <linux/i2c.h>
@@ -396,17 +397,17 @@ static ssize_t lp5569_selftest(struct device *dev,
396397
struct lp55xx_chip *chip = led->chip;
397398
int i, pos = 0;
398399

399-
mutex_lock(&chip->lock);
400+
guard(mutex)(&chip->lock);
400401

401402
/* Test LED Open */
402403
pos = lp5569_led_open_test(led, buf);
403404
if (pos < 0)
404-
goto fail;
405+
return sprintf(buf, "FAIL\n");
405406

406407
/* Test LED Shorted */
407408
pos += lp5569_led_short_test(led, buf);
408409
if (pos < 0)
409-
goto fail;
410+
return sprintf(buf, "FAIL\n");
410411

411412
for (i = 0; i < chip->pdata->num_channels; i++) {
412413
/* Restore current */
@@ -419,16 +420,7 @@ static ssize_t lp5569_selftest(struct device *dev,
419420
led++;
420421
}
421422

422-
if (pos == 0)
423-
pos = sysfs_emit(buf, "OK\n");
424-
goto release_lock;
425-
fail:
426-
pos = sysfs_emit(buf, "FAIL\n");
427-
428-
release_lock:
429-
mutex_unlock(&chip->lock);
430-
431-
return pos;
423+
return pos == 0 ? sysfs_emit(buf, "OK\n") : pos;
432424
}
433425

434426
LP55XX_DEV_ATTR_ENGINE_MODE(1);

0 commit comments

Comments
 (0)