Commit 4137d94f authored by Christian Marangi's avatar Christian Marangi Committed by Lee Jones

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: default avatarMarkus Elfring <Markus.Elfring@web.de>
Signed-off-by: default avatarChristian Marangi <ansuelsmth@gmail.com>
Link: https://lore.kernel.org/r/20240626221520.2846-3-ansuelsmth@gmail.comSigned-off-by: default avatarLee Jones <lee@kernel.org>
parent 8eac0379
...@@ -9,6 +9,7 @@ ...@@ -9,6 +9,7 @@
* Milo(Woogyom) Kim <milo.kim@ti.com> * Milo(Woogyom) Kim <milo.kim@ti.com>
*/ */
#include <linux/cleanup.h>
#include <linux/delay.h> #include <linux/delay.h>
#include <linux/firmware.h> #include <linux/firmware.h>
#include <linux/i2c.h> #include <linux/i2c.h>
...@@ -185,9 +186,9 @@ static ssize_t lp5521_selftest(struct device *dev, ...@@ -185,9 +186,9 @@ static ssize_t lp5521_selftest(struct device *dev,
struct lp55xx_chip *chip = led->chip; struct lp55xx_chip *chip = led->chip;
int ret; int ret;
mutex_lock(&chip->lock); guard(mutex)(&chip->lock);
ret = lp5521_run_selftest(chip, buf); ret = lp5521_run_selftest(chip, buf);
mutex_unlock(&chip->lock);
return sysfs_emit(buf, "%s\n", ret ? "FAIL" : "OK"); return sysfs_emit(buf, "%s\n", ret ? "FAIL" : "OK");
} }
......
...@@ -9,6 +9,7 @@ ...@@ -9,6 +9,7 @@
* Milo(Woogyom) Kim <milo.kim@ti.com> * Milo(Woogyom) Kim <milo.kim@ti.com>
*/ */
#include <linux/cleanup.h>
#include <linux/delay.h> #include <linux/delay.h>
#include <linux/firmware.h> #include <linux/firmware.h>
#include <linux/i2c.h> #include <linux/i2c.h>
...@@ -188,16 +189,16 @@ static ssize_t lp5523_selftest(struct device *dev, ...@@ -188,16 +189,16 @@ static ssize_t lp5523_selftest(struct device *dev,
int ret, pos = 0; int ret, pos = 0;
u8 status, adc, vdd, i; u8 status, adc, vdd, i;
mutex_lock(&chip->lock); guard(mutex)(&chip->lock);
ret = lp55xx_read(chip, LP5523_REG_STATUS, &status); ret = lp55xx_read(chip, LP5523_REG_STATUS, &status);
if (ret < 0) if (ret < 0)
goto fail; return sysfs_emit(buf, "FAIL\n");
/* Check that ext clock is really in use if requested */ /* Check that ext clock is really in use if requested */
if (pdata->clock_mode == LP55XX_CLOCK_EXT) { if (pdata->clock_mode == LP55XX_CLOCK_EXT) {
if ((status & LP5523_EXT_CLK_USED) == 0) if ((status & LP5523_EXT_CLK_USED) == 0)
goto fail; return sysfs_emit(buf, "FAIL\n");
} }
/* Measure VDD (i.e. VBAT) first (channel 16 corresponds to VDD) */ /* Measure VDD (i.e. VBAT) first (channel 16 corresponds to VDD) */
...@@ -205,14 +206,14 @@ static ssize_t lp5523_selftest(struct device *dev, ...@@ -205,14 +206,14 @@ static ssize_t lp5523_selftest(struct device *dev,
usleep_range(3000, 6000); /* ADC conversion time is typically 2.7 ms */ usleep_range(3000, 6000); /* ADC conversion time is typically 2.7 ms */
ret = lp55xx_read(chip, LP5523_REG_STATUS, &status); ret = lp55xx_read(chip, LP5523_REG_STATUS, &status);
if (ret < 0) if (ret < 0)
goto fail; return sysfs_emit(buf, "FAIL\n");
if (!(status & LP5523_LEDTEST_DONE)) if (!(status & LP5523_LEDTEST_DONE))
usleep_range(3000, 6000); /* Was not ready. Wait little bit */ usleep_range(3000, 6000); /* Was not ready. Wait little bit */
ret = lp55xx_read(chip, LP5523_REG_LED_TEST_ADC, &vdd); ret = lp55xx_read(chip, LP5523_REG_LED_TEST_ADC, &vdd);
if (ret < 0) if (ret < 0)
goto fail; return sysfs_emit(buf, "FAIL\n");
vdd--; /* There may be some fluctuation in measurement */ vdd--; /* There may be some fluctuation in measurement */
...@@ -235,14 +236,14 @@ static ssize_t lp5523_selftest(struct device *dev, ...@@ -235,14 +236,14 @@ static ssize_t lp5523_selftest(struct device *dev,
usleep_range(3000, 6000); usleep_range(3000, 6000);
ret = lp55xx_read(chip, LP5523_REG_STATUS, &status); ret = lp55xx_read(chip, LP5523_REG_STATUS, &status);
if (ret < 0) if (ret < 0)
goto fail; return sysfs_emit(buf, "FAIL\n");
if (!(status & LP5523_LEDTEST_DONE)) if (!(status & LP5523_LEDTEST_DONE))
usleep_range(3000, 6000); /* Was not ready. Wait. */ usleep_range(3000, 6000); /* Was not ready. Wait. */
ret = lp55xx_read(chip, LP5523_REG_LED_TEST_ADC, &adc); ret = lp55xx_read(chip, LP5523_REG_LED_TEST_ADC, &adc);
if (ret < 0) if (ret < 0)
goto fail; return sysfs_emit(buf, "FAIL\n");
if (adc >= vdd || adc < LP5523_ADC_SHORTCIRC_LIM) if (adc >= vdd || adc < LP5523_ADC_SHORTCIRC_LIM)
pos += sysfs_emit_at(buf, pos, "LED %d FAIL\n", pos += sysfs_emit_at(buf, pos, "LED %d FAIL\n",
...@@ -256,16 +257,8 @@ static ssize_t lp5523_selftest(struct device *dev, ...@@ -256,16 +257,8 @@ static ssize_t lp5523_selftest(struct device *dev,
led->led_current); led->led_current);
led++; led++;
} }
if (pos == 0)
pos = sysfs_emit(buf, "OK\n");
goto release_lock;
fail:
pos = sysfs_emit(buf, "FAIL\n");
release_lock: return pos == 0 ? sysfs_emit(buf, "OK\n") : pos;
mutex_unlock(&chip->lock);
return pos;
} }
LP55XX_DEV_ATTR_ENGINE_MODE(1); LP55XX_DEV_ATTR_ENGINE_MODE(1);
......
...@@ -7,6 +7,7 @@ ...@@ -7,6 +7,7 @@
* Author: Milo(Woogyom) Kim <milo.kim@ti.com> * Author: Milo(Woogyom) Kim <milo.kim@ti.com>
*/ */
#include <linux/cleanup.h>
#include <linux/delay.h> #include <linux/delay.h>
#include <linux/firmware.h> #include <linux/firmware.h>
#include <linux/i2c.h> #include <linux/i2c.h>
...@@ -171,9 +172,9 @@ static int lp5562_led_brightness(struct lp55xx_led *led) ...@@ -171,9 +172,9 @@ static int lp5562_led_brightness(struct lp55xx_led *led)
}; };
int ret; int ret;
mutex_lock(&chip->lock); guard(mutex)(&chip->lock);
ret = lp55xx_write(chip, addr[led->chan_nr], led->brightness); ret = lp55xx_write(chip, addr[led->chan_nr], led->brightness);
mutex_unlock(&chip->lock);
return ret; return ret;
} }
...@@ -268,9 +269,9 @@ static ssize_t lp5562_store_pattern(struct device *dev, ...@@ -268,9 +269,9 @@ static ssize_t lp5562_store_pattern(struct device *dev,
if (mode > num_patterns || !ptn) if (mode > num_patterns || !ptn)
return -EINVAL; return -EINVAL;
mutex_lock(&chip->lock); guard(mutex)(&chip->lock);
ret = lp5562_run_predef_led_pattern(chip, mode); ret = lp5562_run_predef_led_pattern(chip, mode);
mutex_unlock(&chip->lock);
if (ret) if (ret)
return ret; return ret;
...@@ -320,9 +321,9 @@ static ssize_t lp5562_store_engine_mux(struct device *dev, ...@@ -320,9 +321,9 @@ static ssize_t lp5562_store_engine_mux(struct device *dev,
return -EINVAL; return -EINVAL;
} }
mutex_lock(&chip->lock); guard(mutex)(&chip->lock);
lp55xx_update_bits(chip, LP5562_REG_ENG_SEL, mask, val); lp55xx_update_bits(chip, LP5562_REG_ENG_SEL, mask, val);
mutex_unlock(&chip->lock);
return len; return len;
} }
......
...@@ -4,6 +4,7 @@ ...@@ -4,6 +4,7 @@
*/ */
#include <linux/bitfield.h> #include <linux/bitfield.h>
#include <linux/cleanup.h>
#include <linux/delay.h> #include <linux/delay.h>
#include <linux/firmware.h> #include <linux/firmware.h>
#include <linux/i2c.h> #include <linux/i2c.h>
...@@ -396,17 +397,17 @@ static ssize_t lp5569_selftest(struct device *dev, ...@@ -396,17 +397,17 @@ static ssize_t lp5569_selftest(struct device *dev,
struct lp55xx_chip *chip = led->chip; struct lp55xx_chip *chip = led->chip;
int i, pos = 0; int i, pos = 0;
mutex_lock(&chip->lock); guard(mutex)(&chip->lock);
/* Test LED Open */ /* Test LED Open */
pos = lp5569_led_open_test(led, buf); pos = lp5569_led_open_test(led, buf);
if (pos < 0) if (pos < 0)
goto fail; return sprintf(buf, "FAIL\n");
/* Test LED Shorted */ /* Test LED Shorted */
pos += lp5569_led_short_test(led, buf); pos += lp5569_led_short_test(led, buf);
if (pos < 0) if (pos < 0)
goto fail; return sprintf(buf, "FAIL\n");
for (i = 0; i < chip->pdata->num_channels; i++) { for (i = 0; i < chip->pdata->num_channels; i++) {
/* Restore current */ /* Restore current */
...@@ -419,16 +420,7 @@ static ssize_t lp5569_selftest(struct device *dev, ...@@ -419,16 +420,7 @@ static ssize_t lp5569_selftest(struct device *dev,
led++; led++;
} }
if (pos == 0) return pos == 0 ? sysfs_emit(buf, "OK\n") : pos;
pos = sysfs_emit(buf, "OK\n");
goto release_lock;
fail:
pos = sysfs_emit(buf, "FAIL\n");
release_lock:
mutex_unlock(&chip->lock);
return pos;
} }
LP55XX_DEV_ATTR_ENGINE_MODE(1); LP55XX_DEV_ATTR_ENGINE_MODE(1);
......
...@@ -10,6 +10,7 @@ ...@@ -10,6 +10,7 @@
*/ */
#include <linux/bitfield.h> #include <linux/bitfield.h>
#include <linux/cleanup.h>
#include <linux/clk.h> #include <linux/clk.h>
#include <linux/delay.h> #include <linux/delay.h>
#include <linux/firmware.h> #include <linux/firmware.h>
...@@ -272,10 +273,10 @@ int lp55xx_led_brightness(struct lp55xx_led *led) ...@@ -272,10 +273,10 @@ int lp55xx_led_brightness(struct lp55xx_led *led)
const struct lp55xx_device_config *cfg = chip->cfg; const struct lp55xx_device_config *cfg = chip->cfg;
int ret; int ret;
mutex_lock(&chip->lock); guard(mutex)(&chip->lock);
ret = lp55xx_write(chip, cfg->reg_led_pwm_base.addr + led->chan_nr, ret = lp55xx_write(chip, cfg->reg_led_pwm_base.addr + led->chan_nr,
led->brightness); led->brightness);
mutex_unlock(&chip->lock);
return ret; return ret;
} }
EXPORT_SYMBOL_GPL(lp55xx_led_brightness); EXPORT_SYMBOL_GPL(lp55xx_led_brightness);
...@@ -287,7 +288,8 @@ int lp55xx_multicolor_brightness(struct lp55xx_led *led) ...@@ -287,7 +288,8 @@ int lp55xx_multicolor_brightness(struct lp55xx_led *led)
int ret; int ret;
int i; int i;
mutex_lock(&chip->lock); guard(mutex)(&chip->lock);
for (i = 0; i < led->mc_cdev.num_colors; i++) { for (i = 0; i < led->mc_cdev.num_colors; i++) {
ret = lp55xx_write(chip, ret = lp55xx_write(chip,
cfg->reg_led_pwm_base.addr + cfg->reg_led_pwm_base.addr +
...@@ -296,7 +298,7 @@ int lp55xx_multicolor_brightness(struct lp55xx_led *led) ...@@ -296,7 +298,7 @@ int lp55xx_multicolor_brightness(struct lp55xx_led *led)
if (ret) if (ret)
break; break;
} }
mutex_unlock(&chip->lock);
return ret; return ret;
} }
EXPORT_SYMBOL_GPL(lp55xx_multicolor_brightness); EXPORT_SYMBOL_GPL(lp55xx_multicolor_brightness);
...@@ -404,9 +406,9 @@ static ssize_t led_current_store(struct device *dev, ...@@ -404,9 +406,9 @@ static ssize_t led_current_store(struct device *dev,
if (!chip->cfg->set_led_current) if (!chip->cfg->set_led_current)
return len; return len;
mutex_lock(&chip->lock); guard(mutex)(&chip->lock);
chip->cfg->set_led_current(led, (u8)curr); chip->cfg->set_led_current(led, (u8)curr);
mutex_unlock(&chip->lock);
return len; return len;
} }
...@@ -541,14 +543,12 @@ static void lp55xx_firmware_loaded(const struct firmware *fw, void *context) ...@@ -541,14 +543,12 @@ static void lp55xx_firmware_loaded(const struct firmware *fw, void *context)
} }
/* handling firmware data is chip dependent */ /* handling firmware data is chip dependent */
mutex_lock(&chip->lock); scoped_guard(mutex, &chip->lock) {
chip->engines[idx - 1].mode = LP55XX_ENGINE_LOAD;
chip->engines[idx - 1].mode = LP55XX_ENGINE_LOAD; chip->fw = fw;
chip->fw = fw; if (chip->cfg->firmware_cb)
if (chip->cfg->firmware_cb) chip->cfg->firmware_cb(chip);
chip->cfg->firmware_cb(chip); }
mutex_unlock(&chip->lock);
/* firmware should be released for other channel use */ /* firmware should be released for other channel use */
release_firmware(chip->fw); release_firmware(chip->fw);
...@@ -592,10 +592,10 @@ static ssize_t select_engine_store(struct device *dev, ...@@ -592,10 +592,10 @@ static ssize_t select_engine_store(struct device *dev,
case LP55XX_ENGINE_1: case LP55XX_ENGINE_1:
case LP55XX_ENGINE_2: case LP55XX_ENGINE_2:
case LP55XX_ENGINE_3: case LP55XX_ENGINE_3:
mutex_lock(&chip->lock); scoped_guard(mutex, &chip->lock) {
chip->engine_idx = val; chip->engine_idx = val;
ret = lp55xx_request_firmware(chip); ret = lp55xx_request_firmware(chip);
mutex_unlock(&chip->lock); }
break; break;
default: default:
dev_err(dev, "%lu: invalid engine index. (1, 2, 3)\n", val); dev_err(dev, "%lu: invalid engine index. (1, 2, 3)\n", val);
...@@ -634,9 +634,9 @@ static ssize_t run_engine_store(struct device *dev, ...@@ -634,9 +634,9 @@ static ssize_t run_engine_store(struct device *dev,
return len; return len;
} }
mutex_lock(&chip->lock); guard(mutex)(&chip->lock);
lp55xx_run_engine(chip, true); lp55xx_run_engine(chip, true);
mutex_unlock(&chip->lock);
return len; return len;
} }
...@@ -673,7 +673,7 @@ ssize_t lp55xx_store_engine_mode(struct device *dev, ...@@ -673,7 +673,7 @@ ssize_t lp55xx_store_engine_mode(struct device *dev,
const struct lp55xx_device_config *cfg = chip->cfg; const struct lp55xx_device_config *cfg = chip->cfg;
struct lp55xx_engine *engine = &chip->engines[nr - 1]; struct lp55xx_engine *engine = &chip->engines[nr - 1];
mutex_lock(&chip->lock); guard(mutex)(&chip->lock);
chip->engine_idx = nr; chip->engine_idx = nr;
...@@ -689,8 +689,6 @@ ssize_t lp55xx_store_engine_mode(struct device *dev, ...@@ -689,8 +689,6 @@ ssize_t lp55xx_store_engine_mode(struct device *dev,
engine->mode = LP55XX_ENGINE_DISABLED; engine->mode = LP55XX_ENGINE_DISABLED;
} }
mutex_unlock(&chip->lock);
return len; return len;
} }
EXPORT_SYMBOL_GPL(lp55xx_store_engine_mode); EXPORT_SYMBOL_GPL(lp55xx_store_engine_mode);
...@@ -703,14 +701,12 @@ ssize_t lp55xx_store_engine_load(struct device *dev, ...@@ -703,14 +701,12 @@ ssize_t lp55xx_store_engine_load(struct device *dev,
struct lp55xx_chip *chip = led->chip; struct lp55xx_chip *chip = led->chip;
int ret; int ret;
mutex_lock(&chip->lock); guard(mutex)(&chip->lock);
chip->engine_idx = nr; chip->engine_idx = nr;
lp55xx_load_engine(chip); lp55xx_load_engine(chip);
ret = lp55xx_update_program_memory(chip, buf, len); ret = lp55xx_update_program_memory(chip, buf, len);
mutex_unlock(&chip->lock);
return ret; return ret;
} }
EXPORT_SYMBOL_GPL(lp55xx_store_engine_load); EXPORT_SYMBOL_GPL(lp55xx_store_engine_load);
...@@ -799,26 +795,21 @@ ssize_t lp55xx_store_engine_leds(struct device *dev, ...@@ -799,26 +795,21 @@ ssize_t lp55xx_store_engine_leds(struct device *dev,
struct lp55xx_chip *chip = led->chip; struct lp55xx_chip *chip = led->chip;
struct lp55xx_engine *engine = &chip->engines[nr - 1]; struct lp55xx_engine *engine = &chip->engines[nr - 1];
u16 mux = 0; u16 mux = 0;
ssize_t ret;
if (lp55xx_mux_parse(chip, buf, &mux, len)) if (lp55xx_mux_parse(chip, buf, &mux, len))
return -EINVAL; return -EINVAL;
mutex_lock(&chip->lock); guard(mutex)(&chip->lock);
chip->engine_idx = nr; chip->engine_idx = nr;
ret = -EINVAL;
if (engine->mode != LP55XX_ENGINE_LOAD) if (engine->mode != LP55XX_ENGINE_LOAD)
goto leave; return -EINVAL;
if (lp55xx_load_mux(chip, mux, nr)) if (lp55xx_load_mux(chip, mux, nr))
goto leave; return -EINVAL;
ret = len; return len;
leave:
mutex_unlock(&chip->lock);
return ret;
} }
EXPORT_SYMBOL_GPL(lp55xx_store_engine_leds); EXPORT_SYMBOL_GPL(lp55xx_store_engine_leds);
...@@ -832,9 +823,9 @@ ssize_t lp55xx_show_master_fader(struct device *dev, ...@@ -832,9 +823,9 @@ ssize_t lp55xx_show_master_fader(struct device *dev,
int ret; int ret;
u8 val; u8 val;
mutex_lock(&chip->lock); guard(mutex)(&chip->lock);
ret = lp55xx_read(chip, cfg->reg_master_fader_base.addr + nr - 1, &val); ret = lp55xx_read(chip, cfg->reg_master_fader_base.addr + nr - 1, &val);
mutex_unlock(&chip->lock);
return ret ? ret : sysfs_emit(buf, "%u\n", val); return ret ? ret : sysfs_emit(buf, "%u\n", val);
} }
...@@ -856,10 +847,10 @@ ssize_t lp55xx_store_master_fader(struct device *dev, ...@@ -856,10 +847,10 @@ ssize_t lp55xx_store_master_fader(struct device *dev,
if (val > 0xff) if (val > 0xff)
return -EINVAL; return -EINVAL;
mutex_lock(&chip->lock); guard(mutex)(&chip->lock);
ret = lp55xx_write(chip, cfg->reg_master_fader_base.addr + nr - 1, ret = lp55xx_write(chip, cfg->reg_master_fader_base.addr + nr - 1,
(u8)val); (u8)val);
mutex_unlock(&chip->lock);
return ret ? ret : len; return ret ? ret : len;
} }
...@@ -875,25 +866,22 @@ ssize_t lp55xx_show_master_fader_leds(struct device *dev, ...@@ -875,25 +866,22 @@ ssize_t lp55xx_show_master_fader_leds(struct device *dev,
int i, ret, pos = 0; int i, ret, pos = 0;
u8 val; u8 val;
mutex_lock(&chip->lock); guard(mutex)(&chip->lock);
for (i = 0; i < cfg->max_channel; i++) { for (i = 0; i < cfg->max_channel; i++) {
ret = lp55xx_read(chip, cfg->reg_led_ctrl_base.addr + i, &val); ret = lp55xx_read(chip, cfg->reg_led_ctrl_base.addr + i, &val);
if (ret) if (ret)
goto leave; return ret;
val = FIELD_GET(LP55xx_FADER_MAPPING_MASK, val); val = FIELD_GET(LP55xx_FADER_MAPPING_MASK, val);
if (val > FIELD_MAX(LP55xx_FADER_MAPPING_MASK)) { if (val > FIELD_MAX(LP55xx_FADER_MAPPING_MASK)) {
ret = -EINVAL; return -EINVAL;
goto leave;
} }
buf[pos++] = val + '0'; buf[pos++] = val + '0';
} }
buf[pos++] = '\n'; buf[pos++] = '\n';
ret = pos;
leave: return pos;
mutex_unlock(&chip->lock);
return ret;
} }
EXPORT_SYMBOL_GPL(lp55xx_show_master_fader_leds); EXPORT_SYMBOL_GPL(lp55xx_show_master_fader_leds);
...@@ -909,7 +897,7 @@ ssize_t lp55xx_store_master_fader_leds(struct device *dev, ...@@ -909,7 +897,7 @@ ssize_t lp55xx_store_master_fader_leds(struct device *dev,
n = min_t(int, len, cfg->max_channel); n = min_t(int, len, cfg->max_channel);
mutex_lock(&chip->lock); guard(mutex)(&chip->lock);
for (i = 0; i < n; i++) { for (i = 0; i < n; i++) {
if (buf[i] >= '0' && buf[i] <= '3') { if (buf[i] >= '0' && buf[i] <= '3') {
...@@ -919,16 +907,13 @@ ssize_t lp55xx_store_master_fader_leds(struct device *dev, ...@@ -919,16 +907,13 @@ ssize_t lp55xx_store_master_fader_leds(struct device *dev,
LP55xx_FADER_MAPPING_MASK, LP55xx_FADER_MAPPING_MASK,
val); val);
if (ret) if (ret)
goto leave; return ret;
} else { } else {
ret = -EINVAL; return -EINVAL;
goto leave;
} }
} }
ret = len;
leave: return len;
mutex_unlock(&chip->lock);
return ret;
} }
EXPORT_SYMBOL_GPL(lp55xx_store_master_fader_leds); EXPORT_SYMBOL_GPL(lp55xx_store_master_fader_leds);
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment