Commit 068ae320 authored by Guido Gunther's avatar Guido Gunther
Browse files

leds: turn off regulator when brightness goes to 0


Signed-off-by: Guido Gunther's avatarGuido Günther <guido.gunther@puri.sm>
parent 99b0129d
Pipeline #50100 passed with stage
in 46 minutes and 55 seconds
......@@ -120,6 +120,7 @@ struct lm3692x_led {
char label[LED_MAX_NAME_SIZE];
int led_enable;
int model_id;
bool enabled;
};
static const struct reg_default lm3692x_reg_defs[] = {
......@@ -168,48 +169,42 @@ static int lm3692x_fault_check(struct lm3692x_led *led)
return read_buf;
}
static int lm3692x_brightness_set(struct led_classdev *led_cdev,
enum led_brightness brt_val)
static int lm3692x_led_disable(struct lm3692x_led *led)
{
struct lm3692x_led *led =
container_of(led_cdev, struct lm3692x_led, led_dev);
int ret;
int led_brightness_lsb = (brt_val >> 5);
mutex_lock(&led->lock);
if (!led->enabled)
return 0;
ret = lm3692x_fault_check(led);
ret = regmap_update_bits(led->regmap, LM3692X_EN, LM3692X_DEVICE_EN, 0);
if (ret) {
dev_err(&led->client->dev, "Cannot read/clear faults: %d\n", ret);
goto out;
dev_err(&led->client->dev, "Failed to disable regulator: %d\n",
ret);
return ret;
}
if (brt_val > 0xcc) { /* l5 */
brt_val = 0xcc;
led_brightness_lsb = 0x04;
}
if (led->enable_gpio)
gpiod_direction_output(led->enable_gpio, 0);
ret = regmap_write(led->regmap, LM3692X_BRT_MSB, brt_val);
if (ret) {
dev_err(&led->client->dev, "Cannot write MSB: %d\n", ret);
goto out;
if (led->regulator) {
ret = regulator_disable(led->regulator);
if (ret)
dev_err(&led->client->dev,
"Failed to disable regulator: %d\n", ret);
}
ret = regmap_write(led->regmap, LM3692X_BRT_LSB, led_brightness_lsb);
if (ret) {
dev_err(&led->client->dev, "Cannot write LSB: %d\n", ret);
goto out;
}
out:
mutex_unlock(&led->lock);
led->enabled = false;
return ret;
}
static int lm3692x_init(struct lm3692x_led *led)
static int lm3692x_led_enable(struct lm3692x_led *led)
{
int enable_state;
int ret;
if (led->enabled)
return 0;
if (led->regulator) {
ret = regulator_enable(led->regulator);
if (ret) {
......@@ -315,6 +310,7 @@ static int lm3692x_init(struct lm3692x_led *led)
ret = regmap_update_bits(led->regmap, LM3692X_EN, LM3692X_ENABLE_MASK,
enable_state | LM3692X_DEVICE_EN);
led->enabled = true;
return ret;
out:
dev_err(&led->client->dev, "Fail writing initialization values\n");
......@@ -328,6 +324,49 @@ static int lm3692x_init(struct lm3692x_led *led)
return ret;
}
static int lm3692x_brightness_set(struct led_classdev *led_cdev,
enum led_brightness brt_val)
{
struct lm3692x_led *led =
container_of(led_cdev, struct lm3692x_led, led_dev);
int ret;
int led_brightness_lsb = (brt_val >> 5);
if (brt_val == 0) {
return lm3692x_led_disable(led);
} else {
lm3692x_led_enable(led);
}
mutex_lock(&led->lock);
ret = lm3692x_fault_check(led);
if (ret) {
dev_err(&led->client->dev, "Cannot read/clear faults: %d\n", ret);
goto out;
}
if (brt_val > 0xcc) { /* l5 */
brt_val = 0xcc;
led_brightness_lsb = 0x04;
}
ret = regmap_write(led->regmap, LM3692X_BRT_MSB, brt_val);
if (ret) {
dev_err(&led->client->dev, "Cannot write MSB: %d\n", ret);
goto out;
}
ret = regmap_write(led->regmap, LM3692X_BRT_LSB, led_brightness_lsb);
if (ret) {
dev_err(&led->client->dev, "Cannot write LSB: %d\n", ret);
goto out;
}
out:
mutex_unlock(&led->lock);
return ret;
}
static int lm3692x_probe_dt(struct lm3692x_led *led)
{
struct fwnode_handle *child = NULL;
......@@ -421,7 +460,7 @@ static int lm3692x_probe(struct i2c_client *client,
if (ret)
return ret;
ret = lm3692x_init(led);
ret = lm3692x_led_enable(led);
if (ret)
return ret;
......@@ -433,23 +472,9 @@ static int lm3692x_remove(struct i2c_client *client)
struct lm3692x_led *led = i2c_get_clientdata(client);
int ret;
ret = regmap_update_bits(led->regmap, LM3692X_EN, LM3692X_DEVICE_EN, 0);
if (ret) {
dev_err(&led->client->dev, "Failed to disable regulator: %d\n",
ret);
ret = lm3692x_led_disable (led);
if (!ret)
return ret;
}
if (led->enable_gpio)
gpiod_direction_output(led->enable_gpio, 0);
if (led->regulator) {
ret = regulator_disable(led->regulator);
if (ret)
dev_err(&led->client->dev,
"Failed to disable regulator: %d\n", ret);
}
mutex_destroy(&led->lock);
return 0;
......
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