mail archive of the barebox mailing list
 help / color / mirror / Atom feed
From: iw3gtf@arcor.de
To: barebox@lists.infradead.org
Subject: [PATCH 4/4] video/backlight-pwm: properly handle the case of an empty 'brightness-levels' in the device tree.
Date: Thu, 14 Jul 2016 16:17:18 +0200	[thread overview]
Message-ID: <20160714141718.20121-5-iw3gtf@arcor.de> (raw)
In-Reply-To: <20160714141718.20121-1-iw3gtf@arcor.de>
In-Reply-To: <20160713051805.GZ20657@pengutronix.de>

In case of an empty 'brightness-levels' array in the device tree or
a non empty one but containing only zeros the value of
'pwm_backlight->scale' would remain 0 possibly causing a division by zero
in the function compute_duty_cycle().

To fix it we check the computed value in case we actually have a 'brightness-levels'
array in the device tree otherwise we implicitly assume a simple array
of the form { 0, 1, 2, ..., 100 } and set the scale to 100.
---
 drivers/video/backlight-pwm.c | 20 +++++++++++++-------
 1 file changed, 13 insertions(+), 7 deletions(-)

diff --git a/drivers/video/backlight-pwm.c b/drivers/video/backlight-pwm.c
index 5ff40a9..7f1715d 100644
--- a/drivers/video/backlight-pwm.c
+++ b/drivers/video/backlight-pwm.c
@@ -128,6 +128,13 @@ static int pwm_backlight_parse_dt(struct device_d *dev,
 	if (!node)
 		return -ENODEV;
 
+	ret = of_property_read_u32(node, "default-brightness-level",
+					   &value);
+	if (ret < 0)
+		return ret;
+
+	pwm_backlight->backlight.brightness_default = value;
+
 	/* determine the number of brightness levels */
 	prop = of_find_property(node, "brightness-levels", &length);
 	if (!prop)
@@ -153,14 +160,13 @@ static int pwm_backlight_parse_dt(struct device_d *dev,
 			if (pwm_backlight->levels[i] > pwm_backlight->scale)
 				pwm_backlight->scale = pwm_backlight->levels[i];
 
-		ret = of_property_read_u32(node, "default-brightness-level",
-					   &value);
-		if (ret < 0)
-			return ret;
-
-		pwm_backlight->backlight.brightness_default = value;
-		pwm_backlight->backlight.brightness_max = pwm_backlight->scale;
+		if (pwm_backlight->scale == 0)
+			return -EINVAL;
+	} else {
+		/* We implicitly assume here a linear levels array { 0, 1, 2, ... 100 } */
+		pwm_backlight->scale = 100;
 	}
+	pwm_backlight->backlight.brightness_max = pwm_backlight->scale;
 
 	pwm_backlight->enable_gpio = of_get_named_gpio_flags(node, "enable-gpios", 0, &flags);
 
-- 
2.9.1


_______________________________________________
barebox mailing list
barebox@lists.infradead.org
http://lists.infradead.org/mailman/listinfo/barebox

  parent reply	other threads:[~2016-07-14 14:17 UTC|newest]

Thread overview: 9+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2016-07-11 11:51 problem configuring backlight brightness levels in " giorgio.nicole
2016-07-13  5:18 ` Sascha Hauer
2016-07-14 14:17   ` [PATCH 0/4] fixes some problems found in the backligth-pwm implementation iw3gtf
2016-07-14 14:17     ` [PATCH 1/4] video/backlight-pwm: fixed a loop index going out of range iw3gtf
2016-07-14 14:17     ` [PATCH 2/4] video/backlight-pwm: fix the value of 'brightness_max' iw3gtf
2016-07-14 14:17     ` [PATCH 3/4] video/backlight-pwm: code readability improvement iw3gtf
2016-07-14 14:17     ` iw3gtf [this message]
2016-07-15  6:07     ` [PATCH 0/4] fixes some problems found in the backligth-pwm implementation Sascha Hauer
2016-07-25  8:37     ` Sascha Hauer

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=20160714141718.20121-5-iw3gtf@arcor.de \
    --to=iw3gtf@arcor.de \
    --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