diff mbox series

leds: lm3601x: Calculate max_brightness and brightness properly

Message ID 20240704190157.712590-1-zenghuchen@google.com
State Superseded
Headers show
Series leds: lm3601x: Calculate max_brightness and brightness properly | expand

Commit Message

Jack Chen July 4, 2024, 7:01 p.m. UTC
The torch_current_max should be checked not exceeding the upper bound.
If it does, throw a warning message and set to LM3601X_MAX_TORCH_I_UA.

LM3601x torch brigthness register (LM3601X_LED_TORCH_REG) takes 0 as the
minimum output (2.4 mA). However, 0 of led_brightness means LED_OFF.
Adding a -1 offset to brightness before writing to brightness
register, so when users write minimum brightness (1), it sets lm3601x
output the minimum.

Signed-off-by: Jack Chen <zenghuchen@google.com>
---
 drivers/leds/flash/leds-lm3601x.c | 15 ++++++++++++---
 1 file changed, 12 insertions(+), 3 deletions(-)

Comments

Jack Chen July 4, 2024, 7:20 p.m. UTC | #1
Sorry, but I mistakenly sent the wrong patch.

Please take a look at the v2 patch instead: [PATCH v2] leds: lm3601x:
Calculate max_brightness and brightness properly.

I apologize for the error and any confusion it may have caused.

On Thu, Jul 4, 2024 at 3:02 PM Jack Chen <zenghuchen@google.com> wrote:
>
> The torch_current_max should be checked not exceeding the upper bound.
> If it does, throw a warning message and set to LM3601X_MAX_TORCH_I_UA.
>
> LM3601x torch brigthness register (LM3601X_LED_TORCH_REG) takes 0 as the
> minimum output (2.4 mA). However, 0 of led_brightness means LED_OFF.
> Adding a -1 offset to brightness before writing to brightness
> register, so when users write minimum brightness (1), it sets lm3601x
> output the minimum.
>
> Signed-off-by: Jack Chen <zenghuchen@google.com>
> ---
>  drivers/leds/flash/leds-lm3601x.c | 15 ++++++++++++---
>  1 file changed, 12 insertions(+), 3 deletions(-)
>
> diff --git a/drivers/leds/flash/leds-lm3601x.c b/drivers/leds/flash/leds-lm3601x.c
> index 7e93c447fec5..fc4df904ea90 100644
> --- a/drivers/leds/flash/leds-lm3601x.c
> +++ b/drivers/leds/flash/leds-lm3601x.c
> @@ -190,7 +190,7 @@ static int lm3601x_brightness_set(struct led_classdev *cdev,
>                 goto out;
>         }
>
> -       ret = regmap_write(led->regmap, LM3601X_LED_TORCH_REG, brightness);
> +       ret = regmap_write(led->regmap, LM3601X_LED_TORCH_REG, brightness - 1);
>         if (ret < 0)
>                 goto out;
>
> @@ -341,8 +341,9 @@ static int lm3601x_register_leds(struct lm3601x_led *led,
>
>         led_cdev = &led->fled_cdev.led_cdev;
>         led_cdev->brightness_set_blocking = lm3601x_brightness_set;
> -       led_cdev->max_brightness = DIV_ROUND_UP(led->torch_current_max,
> -                                               LM3601X_TORCH_REG_DIV);
> +       led_cdev->max_brightness = DIV_ROUND_UP(
> +                       led->torch_current_max - LM3601X_MIN_TORCH_I_UA + 1,
> +                       LM3601X_TORCH_REG_DIV);
>         led_cdev->flags |= LED_DEV_CAP_FLASH;
>
>         init_data.fwnode = fwnode;
> @@ -386,6 +387,14 @@ static int lm3601x_parse_node(struct lm3601x_led *led,
>                 goto out_err;
>         }
>
> +       if (led->torch_current_max > LM3601X_MAX_TORCH_I_UA) {
> +               dev_warn(&led->client->dev,
> +                        "led-max-microamp cannot be higher than %d\n",
> +                        LM3601X_MAX_TORCH_I_UA);
> +               led->torch_current_max = LM3601X_MAX_TORCH_I_UA;
> +       }
> +
> +
>         ret = fwnode_property_read_u32(child, "flash-max-microamp",
>                                 &led->flash_current_max);
>         if (ret) {
> --
> 2.45.2.803.g4e1b14247a-goog
>
diff mbox series

Patch

diff --git a/drivers/leds/flash/leds-lm3601x.c b/drivers/leds/flash/leds-lm3601x.c
index 7e93c447fec5..fc4df904ea90 100644
--- a/drivers/leds/flash/leds-lm3601x.c
+++ b/drivers/leds/flash/leds-lm3601x.c
@@ -190,7 +190,7 @@  static int lm3601x_brightness_set(struct led_classdev *cdev,
 		goto out;
 	}
 
-	ret = regmap_write(led->regmap, LM3601X_LED_TORCH_REG, brightness);
+	ret = regmap_write(led->regmap, LM3601X_LED_TORCH_REG, brightness - 1);
 	if (ret < 0)
 		goto out;
 
@@ -341,8 +341,9 @@  static int lm3601x_register_leds(struct lm3601x_led *led,
 
 	led_cdev = &led->fled_cdev.led_cdev;
 	led_cdev->brightness_set_blocking = lm3601x_brightness_set;
-	led_cdev->max_brightness = DIV_ROUND_UP(led->torch_current_max,
-						LM3601X_TORCH_REG_DIV);
+	led_cdev->max_brightness = DIV_ROUND_UP(
+			led->torch_current_max - LM3601X_MIN_TORCH_I_UA + 1,
+			LM3601X_TORCH_REG_DIV);
 	led_cdev->flags |= LED_DEV_CAP_FLASH;
 
 	init_data.fwnode = fwnode;
@@ -386,6 +387,14 @@  static int lm3601x_parse_node(struct lm3601x_led *led,
 		goto out_err;
 	}
 
+	if (led->torch_current_max > LM3601X_MAX_TORCH_I_UA) {
+		dev_warn(&led->client->dev,
+			 "led-max-microamp cannot be higher than %d\n",
+			 LM3601X_MAX_TORCH_I_UA);
+		led->torch_current_max = LM3601X_MAX_TORCH_I_UA;
+	}
+
+
 	ret = fwnode_property_read_u32(child, "flash-max-microamp",
 				&led->flash_current_max);
 	if (ret) {