public inbox for linux-kernel@vger.kernel.org
 help / color / mirror / Atom feed
* [PATCH] leds:lm3601x:calculate max_brightness and brightness properly
@ 2024-07-03 16:46 Jack Chen
  2024-07-04 16:50 ` Lee Jones
  0 siblings, 1 reply; 5+ messages in thread
From: Jack Chen @ 2024-07-03 16:46 UTC (permalink / raw)
  To: Pavel Machek, Lee Jones
  Cc: Mark Brown, Vadim Pasternak, Randy Dunlap, Jack Chen,
	linux-kernel, linux-leds

1) check the range of torch_current_max,
2) calcualtes max_brightness precisely,
3) lm3601x torch brightness register starts from 0 (2.4 mA).

Tested: tested with a lm36011 and it can set its brightness to lowest
value (2.4 mA)
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


^ permalink raw reply related	[flat|nested] 5+ messages in thread

* Re: [PATCH] leds:lm3601x:calculate max_brightness and brightness properly
  2024-07-03 16:46 [PATCH] leds:lm3601x:calculate max_brightness and brightness properly Jack Chen
@ 2024-07-04 16:50 ` Lee Jones
  2024-07-04 17:52   ` Jack Chen
  0 siblings, 1 reply; 5+ messages in thread
From: Lee Jones @ 2024-07-04 16:50 UTC (permalink / raw)
  To: Jack Chen
  Cc: Pavel Machek, Mark Brown, Vadim Pasternak, Randy Dunlap,
	linux-kernel, linux-leds

Subject line needs fixing.

`git log --oneline -- drivers/<subsystem>` is your friend.

> 1) check the range of torch_current_max,
> 2) calcualtes max_brightness precisely,
> 3) lm3601x torch brightness register starts from 0 (2.4 mA).

Please write this out as a proper paragraph.

This isn't really a numbered list type situation.

> Tested: tested with a lm36011 and it can set its brightness to lowest
> value (2.4 mA)

What is the Tested: trailer?  Again, please write this out properly.

> 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);

Why is there now a need to start adding/subtracting 1s to make the maths work?

>  	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(

This is no place to break a line.

Break after the '=' and wrap at 100-chars instead.

> +			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);

"Max torch current set too high (%d vs %d)"

> +		led->torch_current_max = LM3601X_MAX_TORCH_I_UA;
> +	}
> +
> +

2 lines?

>  	ret = fwnode_property_read_u32(child, "flash-max-microamp",
>  				&led->flash_current_max);
>  	if (ret) {
> -- 
> 2.45.2.803.g4e1b14247a-goog
> 

-- 
Lee Jones [李琼斯]

^ permalink raw reply	[flat|nested] 5+ messages in thread

* Re: [PATCH] leds:lm3601x:calculate max_brightness and brightness properly
  2024-07-04 16:50 ` Lee Jones
@ 2024-07-04 17:52   ` Jack Chen
  0 siblings, 0 replies; 5+ messages in thread
From: Jack Chen @ 2024-07-04 17:52 UTC (permalink / raw)
  To: Lee Jones
  Cc: Pavel Machek, Mark Brown, Vadim Pasternak, Randy Dunlap,
	linux-kernel, linux-leds

Many thanks to Lee for the quick response. I will submit a V2 patch later.
> > -     ret = regmap_write(led->regmap, LM3601X_LED_TORCH_REG, brightness);
> > +     ret = regmap_write(led->regmap, LM3601X_LED_TORCH_REG, brightness - 1);

> Why is there now a need to start adding/subtracting 1s to make the maths work?
This is because lm3601x torch brightness register takes 0 as the
minimum output current (2.4mA). But led_brightness uses 0 as LED_OFF.
So we need to add -1 offset to avoid blocking users who want to set
the torch output minimum.


On Thu, Jul 4, 2024 at 12:50 PM Lee Jones <lee@kernel.org> wrote:
>
> Subject line needs fixing.
>
> `git log --oneline -- drivers/<subsystem>` is your friend.
>
> > 1) check the range of torch_current_max,
> > 2) calcualtes max_brightness precisely,
> > 3) lm3601x torch brightness register starts from 0 (2.4 mA).
>
> Please write this out as a proper paragraph.
>
> This isn't really a numbered list type situation.
>
> > Tested: tested with a lm36011 and it can set its brightness to lowest
> > value (2.4 mA)
>
> What is the Tested: trailer?  Again, please write this out properly.
>
> > 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);
>
> Why is there now a need to start adding/subtracting 1s to make the maths work?
>
> >       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(
>
> This is no place to break a line.
>
> Break after the '=' and wrap at 100-chars instead.
>
> > +                     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);
>
> "Max torch current set too high (%d vs %d)"
>
> > +             led->torch_current_max = LM3601X_MAX_TORCH_I_UA;
> > +     }
> > +
> > +
>
> 2 lines?
>
> >       ret = fwnode_property_read_u32(child, "flash-max-microamp",
> >                               &led->flash_current_max);
> >       if (ret) {
> > --
> > 2.45.2.803.g4e1b14247a-goog
> >
>
> --
> Lee Jones [李琼斯]

^ permalink raw reply	[flat|nested] 5+ messages in thread

* [PATCH] leds: lm3601x: Calculate max_brightness and brightness properly
@ 2024-07-04 19:01 Jack Chen
  2024-07-04 19:20 ` Jack Chen
  0 siblings, 1 reply; 5+ messages in thread
From: Jack Chen @ 2024-07-04 19:01 UTC (permalink / raw)
  To: Pavel Machek, Lee Jones
  Cc: Mark Brown, Vadim Pasternak, Randy Dunlap, Jack Chen,
	linux-kernel, linux-leds

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


^ permalink raw reply related	[flat|nested] 5+ messages in thread

* Re: [PATCH] leds: lm3601x: Calculate max_brightness and brightness properly
  2024-07-04 19:01 [PATCH] leds: lm3601x: Calculate " Jack Chen
@ 2024-07-04 19:20 ` Jack Chen
  0 siblings, 0 replies; 5+ messages in thread
From: Jack Chen @ 2024-07-04 19:20 UTC (permalink / raw)
  To: Pavel Machek, Lee Jones
  Cc: Mark Brown, Vadim Pasternak, Randy Dunlap, linux-kernel,
	linux-leds

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
>

^ permalink raw reply	[flat|nested] 5+ messages in thread

end of thread, other threads:[~2024-07-04 19:20 UTC | newest]

Thread overview: 5+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2024-07-03 16:46 [PATCH] leds:lm3601x:calculate max_brightness and brightness properly Jack Chen
2024-07-04 16:50 ` Lee Jones
2024-07-04 17:52   ` Jack Chen
  -- strict thread matches above, loose matches on Subject: below --
2024-07-04 19:01 [PATCH] leds: lm3601x: Calculate " Jack Chen
2024-07-04 19:20 ` Jack Chen

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox