[4/4] backlight: pwm_bl: Set scale type for brightness curves specified in the DT
diff mbox series

Message ID 20190613194326.180889-5-mka@chromium.org
State New
Headers show
Series
  • backlight: Expose brightness curve type through sysfs
Related show

Commit Message

Matthias Kaehlcke June 13, 2019, 7:43 p.m. UTC
Check if a brightness curve specified in the device tree is linear or
not and set the corresponding property accordingly. This makes the
scale type available to userspace via the 'scale' sysfs attribute.

To determine if a curve is linear it is compared to a interpolated linear
curve between min and max brightness. The curve is considered linear if
no value deviates more than +/-5% of ${brightness_range} from their
interpolated value.

Signed-off-by: Matthias Kaehlcke <mka@chromium.org>
---
 drivers/video/backlight/pwm_bl.c | 25 +++++++++++++++++++++++++
 1 file changed, 25 insertions(+)

Comments

Daniel Thompson June 21, 2019, 1:10 p.m. UTC | #1
On 13/06/2019 20:43, Matthias Kaehlcke wrote:
> Check if a brightness curve specified in the device tree is linear or
> not and set the corresponding property accordingly. This makes the
> scale type available to userspace via the 'scale' sysfs attribute.
> 
> To determine if a curve is linear it is compared to a interpolated linear
> curve between min and max brightness. The curve is considered linear if
> no value deviates more than +/-5% of ${brightness_range} from their
> interpolated value.
> 
> Signed-off-by: Matthias Kaehlcke <mka@chromium.org>
> ---
>   drivers/video/backlight/pwm_bl.c | 25 +++++++++++++++++++++++++
>   1 file changed, 25 insertions(+)
> 
> diff --git a/drivers/video/backlight/pwm_bl.c b/drivers/video/backlight/pwm_bl.c
> index f067fe7aa35d..912407b6d67f 100644
> --- a/drivers/video/backlight/pwm_bl.c
> +++ b/drivers/video/backlight/pwm_bl.c
> @@ -404,6 +404,26 @@ int pwm_backlight_brightness_default(struct device *dev,
>   }
>   #endif
>   
> +static bool pwm_backlight_is_linear(struct platform_pwm_backlight_data *data)
> +{
> +	unsigned int nlevels = data->max_brightness + 1;
> +	unsigned int min_val = data->levels[0];
> +	unsigned int max_val = data->levels[nlevels - 1];
> +	unsigned int slope = (100 * (max_val - min_val)) / nlevels;

Why 100 (rather than a power of 2)?

It would also be good to have a comment here saying what the maximum 
quantization error is. Doesn't have to be over complex just mentioning 
something like the following (assuming you agree that its true ;-) ):

   Multiplying by XXX means that even in pathalogical cases such as
   (max_val - min_val) == nlevels then the error at max_val is less than
   1%.

With a suitable comment in the fixed point code:
Acked-by: Daniel Thompson <daniel.thompson@linaro.org>


Daniel.


> +	unsigned int margin = (max_val - min_val) / 20; /* 5% */
> +	int i;
> +
> +	for (i = 1; i < nlevels; i++) {
> +		unsigned int linear_value = min_val + ((i * slope) / 100);
> +		unsigned int delta = abs(linear_value - data->levels[i]);
> +
> +		if (delta > margin)
> +			return false;
> +	}
> +
> +	return true;
> +}
> +
>   static int pwm_backlight_initial_power_state(const struct pwm_bl_data *pb)
>   {
>   	struct device_node *node = pb->dev->of_node;
> @@ -567,6 +587,11 @@ static int pwm_backlight_probe(struct platform_device *pdev)
>   
>   			pb->levels = data->levels;
>   		}
> +
> +		if (pwm_backlight_is_linear(data))
> +			props.scale = BACKLIGHT_SCALE_LINEAR;
> +		else
> +			props.scale = BACKLIGHT_SCALE_NON_LINEAR;
>   	} else if (!data->max_brightness) {
>   		/*
>   		 * If no brightness levels are provided and max_brightness is
>
Matthias Kaehlcke June 24, 2019, 6:54 p.m. UTC | #2
Hi Daniel,

On Fri, Jun 21, 2019 at 02:10:19PM +0100, Daniel Thompson wrote:
> On 13/06/2019 20:43, Matthias Kaehlcke wrote:
> > Check if a brightness curve specified in the device tree is linear or
> > not and set the corresponding property accordingly. This makes the
> > scale type available to userspace via the 'scale' sysfs attribute.
> > 
> > To determine if a curve is linear it is compared to a interpolated linear
> > curve between min and max brightness. The curve is considered linear if
> > no value deviates more than +/-5% of ${brightness_range} from their
> > interpolated value.
> > 
> > Signed-off-by: Matthias Kaehlcke <mka@chromium.org>
> > ---
> >   drivers/video/backlight/pwm_bl.c | 25 +++++++++++++++++++++++++
> >   1 file changed, 25 insertions(+)
> > 
> > diff --git a/drivers/video/backlight/pwm_bl.c b/drivers/video/backlight/pwm_bl.c
> > index f067fe7aa35d..912407b6d67f 100644
> > --- a/drivers/video/backlight/pwm_bl.c
> > +++ b/drivers/video/backlight/pwm_bl.c
> > @@ -404,6 +404,26 @@ int pwm_backlight_brightness_default(struct device *dev,
> >   }
> >   #endif
> > +static bool pwm_backlight_is_linear(struct platform_pwm_backlight_data *data)
> > +{
> > +	unsigned int nlevels = data->max_brightness + 1;
> > +	unsigned int min_val = data->levels[0];
> > +	unsigned int max_val = data->levels[nlevels - 1];
> > +	unsigned int slope = (100 * (max_val - min_val)) / nlevels;
> 
> Why 100 (rather than a power of 2)?

I guess it came from the decimal part of my brain, I can change it to
128 ;-)

> It would also be good to have a comment here saying what the maximum
> quantization error is. Doesn't have to be over complex just mentioning
> something like the following (assuming you agree that its true ;-) ):
> 
>   Multiplying by XXX means that even in pathalogical cases such as
>   (max_val - min_val) == nlevels then the error at max_val is less than
>   1%.

Sounds good, thanks for the suggestion!

> With a suitable comment in the fixed point code:
> Acked-by: Daniel Thompson <daniel.thompson@linaro.org>

Thanks

Patch
diff mbox series

diff --git a/drivers/video/backlight/pwm_bl.c b/drivers/video/backlight/pwm_bl.c
index f067fe7aa35d..912407b6d67f 100644
--- a/drivers/video/backlight/pwm_bl.c
+++ b/drivers/video/backlight/pwm_bl.c
@@ -404,6 +404,26 @@  int pwm_backlight_brightness_default(struct device *dev,
 }
 #endif
 
+static bool pwm_backlight_is_linear(struct platform_pwm_backlight_data *data)
+{
+	unsigned int nlevels = data->max_brightness + 1;
+	unsigned int min_val = data->levels[0];
+	unsigned int max_val = data->levels[nlevels - 1];
+	unsigned int slope = (100 * (max_val - min_val)) / nlevels;
+	unsigned int margin = (max_val - min_val) / 20; /* 5% */
+	int i;
+
+	for (i = 1; i < nlevels; i++) {
+		unsigned int linear_value = min_val + ((i * slope) / 100);
+		unsigned int delta = abs(linear_value - data->levels[i]);
+
+		if (delta > margin)
+			return false;
+	}
+
+	return true;
+}
+
 static int pwm_backlight_initial_power_state(const struct pwm_bl_data *pb)
 {
 	struct device_node *node = pb->dev->of_node;
@@ -567,6 +587,11 @@  static int pwm_backlight_probe(struct platform_device *pdev)
 
 			pb->levels = data->levels;
 		}
+
+		if (pwm_backlight_is_linear(data))
+			props.scale = BACKLIGHT_SCALE_LINEAR;
+		else
+			props.scale = BACKLIGHT_SCALE_NON_LINEAR;
 	} else if (!data->max_brightness) {
 		/*
 		 * If no brightness levels are provided and max_brightness is