[v1,09/11] leds: aw200xx: improve autodim calculation method

Message ID 20231006160437.15627-10-ddrokosov@salutedevices.com
State New
Headers
Series leds: aw200xx: several driver updates |

Commit Message

Dmitry Rokosov Oct. 6, 2023, 4:04 p.m. UTC
  From: George Stark <gnstark@salutedevices.com>

use DIV_ROUND_UP instead of coarse div

Signed-off-by: George Stark <gnstark@salutedevices.com>
Signed-off-by: Dmitry Rokosov <ddrokosov@salutedevices.com>
---
 drivers/leds/leds-aw200xx.c | 6 +++---
 1 file changed, 3 insertions(+), 3 deletions(-)
  

Comments

Andy Shevchenko Oct. 6, 2023, 6:03 p.m. UTC | #1
On Fri, Oct 6, 2023 at 7:05 PM Dmitry Rokosov
<ddrokosov@salutedevices.com> wrote:
>
> From: George Stark <gnstark@salutedevices.com>
>
> use DIV_ROUND_UP instead of coarse div

Please, respect English grammar and punctuation.
Refer to macros and functions as func() (note the parentheses).

...

>  #define AW200XX_REG_DIM2FADE(x) ((x) + 1)
> +#define AW200XX_REG_FADE2DIM(fade) \
> +       DIV_ROUND_UP((fade) * AW200XX_DIM_MAX, AW200XX_FADE_MAX)

Have you checked if the overflow is _now_ possible (compiling on
32-bit platforms as well)?
  
Dmitry Rokosov Oct. 9, 2023, 1:18 p.m. UTC | #2
On Fri, Oct 06, 2023 at 09:03:47PM +0300, Andy Shevchenko wrote:
> On Fri, Oct 6, 2023 at 7:05 PM Dmitry Rokosov
> <ddrokosov@salutedevices.com> wrote:
> >
> > From: George Stark <gnstark@salutedevices.com>
> >
> > use DIV_ROUND_UP instead of coarse div
> 
> Please, respect English grammar and punctuation.
> Refer to macros and functions as func() (note the parentheses).
> 
> ...
> 
> >  #define AW200XX_REG_DIM2FADE(x) ((x) + 1)
> > +#define AW200XX_REG_FADE2DIM(fade) \
> > +       DIV_ROUND_UP((fade) * AW200XX_DIM_MAX, AW200XX_FADE_MAX)
> 
> Have you checked if the overflow is _now_ possible (compiling on
> 32-bit platforms as well)?

I suppose we shouldn't carry on about overflow here because the value of
fade cannot exceed 255, and DIM_MAX is set at 63

You can find maximum values of fade and dim in the aw200xx driver
header:

#define AW200XX_DIM_MAX                  (BIT(6) - 1)
#define AW200XX_FADE_MAX                 (BIT(8) - 1)
  
Dmitry Rokosov Oct. 9, 2023, 2:02 p.m. UTC | #3
On Mon, Oct 09, 2023 at 04:18:08PM +0300, Dmitry Rokosov wrote:
> On Fri, Oct 06, 2023 at 09:03:47PM +0300, Andy Shevchenko wrote:
> > On Fri, Oct 6, 2023 at 7:05 PM Dmitry Rokosov
> > <ddrokosov@salutedevices.com> wrote:
> > >
> > > From: George Stark <gnstark@salutedevices.com>
> > >
> > > use DIV_ROUND_UP instead of coarse div
> > 
> > Please, respect English grammar and punctuation.
> > Refer to macros and functions as func() (note the parentheses).
> > 
> > ...
> > 
> > >  #define AW200XX_REG_DIM2FADE(x) ((x) + 1)
> > > +#define AW200XX_REG_FADE2DIM(fade) \
> > > +       DIV_ROUND_UP((fade) * AW200XX_DIM_MAX, AW200XX_FADE_MAX)
> > 
> > Have you checked if the overflow is _now_ possible (compiling on
> > 32-bit platforms as well)?
> 
> I suppose we shouldn't carry on about overflow here because the value of
> fade cannot exceed 255, and DIM_MAX is set at 63
> 
> You can find maximum values of fade and dim in the aw200xx driver
> header:
> 
> #define AW200XX_DIM_MAX                  (BIT(6) - 1)
> #define AW200XX_FADE_MAX                 (BIT(8) - 1)

I agree that checking if the fade is not greater than FADE_MAX will not
be excessive. The LED subsystem is capable of sending brightness levels
higher than 255
  
George Stark Oct. 16, 2023, 9:22 p.m. UTC | #4
On 10/9/23 17:02, Dmitry Rokosov wrote:
> On Mon, Oct 09, 2023 at 04:18:08PM +0300, Dmitry Rokosov wrote:
>> On Fri, Oct 06, 2023 at 09:03:47PM +0300, Andy Shevchenko wrote:
>>> On Fri, Oct 6, 2023 at 7:05 PM Dmitry Rokosov
>>> <ddrokosov@salutedevices.com> wrote:
>>>>
>>>> From: George Stark <gnstark@salutedevices.com>
>>>>
>>>> use DIV_ROUND_UP instead of coarse div
>>>
>>> Please, respect English grammar and punctuation.
>>> Refer to macros and functions as func() (note the parentheses).
>>>
>>> ...
>>>
>>>>   #define AW200XX_REG_DIM2FADE(x) ((x) + 1)
>>>> +#define AW200XX_REG_FADE2DIM(fade) \
>>>> +       DIV_ROUND_UP((fade) * AW200XX_DIM_MAX, AW200XX_FADE_MAX)
>>>
>>> Have you checked if the overflow is _now_ possible (compiling on
>>> 32-bit platforms as well)?
>>
>> I suppose we shouldn't carry on about overflow here because the value of
>> fade cannot exceed 255, and DIM_MAX is set at 63
>>
>> You can find maximum values of fade and dim in the aw200xx driver
>> header:
>>
>> #define AW200XX_DIM_MAX                  (BIT(6) - 1)
>> #define AW200XX_FADE_MAX                 (BIT(8) - 1)
> 
> I agree that checking if the fade is not greater than FADE_MAX will not
> be excessive. The LED subsystem is capable of sending brightness levels
> higher than 255
> 

Probably we should set max_brightness to AW200XX_FADE_MAX in 
led_classdev when adding leds.
  

Patch

diff --git a/drivers/leds/leds-aw200xx.c b/drivers/leds/leds-aw200xx.c
index 5a1a93ffe36c..09b8bc6724c7 100644
--- a/drivers/leds/leds-aw200xx.c
+++ b/drivers/leds/leds-aw200xx.c
@@ -87,6 +87,8 @@ 
 #define AW200XX_REG_DIM(x, columns) \
 	AW200XX_REG(AW200XX_PAGE4, AW200XX_LED2REG(x, columns) * 2)
 #define AW200XX_REG_DIM2FADE(x) ((x) + 1)
+#define AW200XX_REG_FADE2DIM(fade) \
+	DIV_ROUND_UP((fade) * AW200XX_DIM_MAX, AW200XX_FADE_MAX)
 
 /*
  * Duty ratio of display scan (see p.15 of datasheet for formula):
@@ -195,9 +197,7 @@  static int aw200xx_brightness_set(struct led_classdev *cdev,
 
 	dim = led->dim;
 	if (dim < 0)
-		dim = max_t(int,
-			    brightness / (AW200XX_FADE_MAX / AW200XX_DIM_MAX),
-			    1);
+		dim = AW200XX_REG_FADE2DIM(brightness);
 
 	ret = regmap_write(chip->regmap, reg, dim);
 	if (ret)