mail archive of the barebox mailing list
 help / color / mirror / Atom feed
From: Ahmad Fatoum <a.fatoum@pengutronix.de>
To: Ingo Reitz <9l@9lo.re>, barebox@lists.infradead.org
Subject: Re: [PATCH] video: backlicht-pwm: parse num-interpolated-steps
Date: Mon, 20 Jul 2026 10:32:55 +0200	[thread overview]
Message-ID: <4402a71c-d1fd-4dd1-bbff-74cf52758ae3@pengutronix.de> (raw)
In-Reply-To: <20260712213724.186498-1-9l@9lo.re>

On 7/12/26 23:38, Ingo Reitz wrote:
> Port the parsing logic for num-interpolated-steps from linux, which populates
> brightness-levels between 2 brightness level by linear interpolation.
> This is useful if the amount of brightness levels exeeds what can
> practically can be written into the devicetree array.
> 
> Signed-off-by: Ingo Reitz <9l@9lo.re>

Reviewed-by: Ahmad Fatoum <a.fatoum@pengutronix.de>

Title (s/backlight/backlight/) can be fixed up on apply time perhaps? 

Cheers,
Ahmad

> ---
>  drivers/video/backlight-pwm.c | 76 ++++++++++++++++++++++++++++++++++-
>  1 file changed, 75 insertions(+), 1 deletion(-)
> 
> diff --git a/drivers/video/backlight-pwm.c b/drivers/video/backlight-pwm.c
> index 14fad55637..581fa0237c 100644
> --- a/drivers/video/backlight-pwm.c
> +++ b/drivers/video/backlight-pwm.c
> @@ -12,6 +12,7 @@
>  #include <linux/err.h>
>  #include <of.h>
>  #include <regulator.h>
> +#include <linux/device.h>
>  #include <linux/gpio/consumer.h>
>  #include <linux/math64.h>
>  
> @@ -101,7 +102,9 @@ static int pwm_backlight_parse_dt(struct device *dev,
>  {
>  	struct device_node *node = dev->of_node;
>  	struct property *prop;
> -	int length;
> +	unsigned int length;
> +
> 	unsigned int num_steps = 0;
> +	unsigned int *table;
>  	u32 value;
>  	int ret, i;
>  
> @@ -134,6 +137,77 @@ static int pwm_backlight_parse_dt(struct device *dev,
>  		if (ret < 0)
>  			return ret;
>  
> +		/*
> +		 * This property is optional, if is set enables linear
> +		 * interpolation between each of the values of brightness levels
> +		 * and creates a new pre-computed table.
> +		 */
> +		of_property_read_u32(node, "num-interpolated-steps",
> +				     &num_steps);
> +
> +		if (num_steps) {
> +			unsigned int num_input_levels = length;
> +			unsigned int i;
> +			u32 x1, x2, x, dx;
> +			u32 y1, y2;
> +			s64 dy;
> +
> +			/*
> +			 * Make sure that there is at least two entries in the
> +			 * brightness-levels table, otherwise we can't interpolate
> +			 * between two points.
> +			 */
> +			if (num_input_levels < 2) {
> +				dev_err(dev, "can't interpolate\n");
> +				return -EINVAL;
> +			}
> +
> +			/*
> +			 * Recalculate the number of brightness levels, now
> +			 * taking in consideration the number o
> f interpolated
> +			 * steps between two levels.
> +			 */
> +			length = (num_input_levels - 1) * num_steps + 1;
> +			dev_dbg(dev, "new number of brightness levels: %d\n",
> +				length);
> +
> +			/*
> +			 * Create a new table of brightness levels with all the
> +			 * interpolated steps.
> +			 */
> +			table = devm_kcalloc(dev, length, sizeof(*table),
> +					     GFP_KERNEL);
> +			if (!table)
> +				return -ENOMEM;
> +			/*
> +			 * Fill the interpolated table[x] = y
> +			 * by draw lines between each (x1, y1) to (x2, y2).
> +			 */
> +			dx = num_steps;
> +			for (i = 0; i < num_input_levels - 1; i++) {
> +				x1 = i * dx;
> +				x2 = x1 + dx;
> +				y1 = pwm_backlight->levels[i];
> +				y2 = pwm_backlight->levels[i + 1];
> +				dy = (s64)y2 - y1;
> +
> +				for (x = x1; x < x2; x++) {
> +					table[x] =
> +						y1 + div_s64(dy * (x - x1), dx);
> +				}
> +			}
> +			/* Fill in the last point, since no line starts here. */
> +			table[x2] = y2;
> +
> +			/*
> +			 * As we use interpolation lets remove current
> 
> +			 * brightness levels table and replace for the
> +			 * new interpolated table.
> +			 */
> +			devm_kfree(dev, pwm_backlight->levels);
> +			pwm_backlight->levels = table;
> +		}
> +
>  		pwm_backlight->backlight.brightness_max = length - 1;
>  
>  		for (i = 0; i < length; i++)


-- 
Pengutronix e.K.                           |                             |
Steuerwalder Str. 21                       | http://www.pengutronix.de/  |
31137 Hildesheim, Germany                  | Phone: +49-5121-206917-0    |
Amtsgericht Hildesheim, HRA 2686           | Fax:   +49-5121-206917-5555 |



      reply	other threads:[~2026-07-20  8:34 UTC|newest]

Thread overview: 2+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2026-07-12 21:38 Ingo Reitz
2026-07-20  8:32 ` Ahmad Fatoum [this message]

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=4402a71c-d1fd-4dd1-bbff-74cf52758ae3@pengutronix.de \
    --to=a.fatoum@pengutronix.de \
    --cc=9l@9lo.re \
    --cc=barebox@lists.infradead.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox