aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorMatthias Kaehlcke <mka@chromium.org>2019-06-12 14:00:03 -0400
committerLee Jones <lee.jones@linaro.org>2019-06-27 02:09:05 -0400
commit73fbfc499448455f1e1c77717040e09e25f1d976 (patch)
tree52eda9b252823448aaaaf79db9379a2b896378c3
parent98b7404eb7d64e55f8fdd419cb3965a8abf0e217 (diff)
backlight: pwm_bl: Fix heuristic to determine number of brightness levels
With commit 88ba95bedb79 ("backlight: pwm_bl: Compute brightness of LED linearly to human eye") the number of set bits (aka hweight()) in the PWM period is used in the heuristic to determine the number of brightness levels, when the brightness table isn't specified in the DT. The number of set bits doesn't provide a reliable clue about the length of the period, instead change the heuristic to: nlevels = period / fls(period) Also limit the maximum number of brightness levels to 4096 to avoid excessively large tables. With this the number of levels increases monotonically with the PWM period, until the maximum of 4096 levels is reached: period (ns) # levels 100 16 500 62 1000 111 5000 416 10000 769 50000 3333 100000 4096 Fixes: 88ba95bedb79 ("backlight: pwm_bl: Compute brightness of LED linearly to human eye") Signed-off-by: Matthias Kaehlcke <mka@chromium.org> Acked-by: Daniel Thompson <daniel.thompson@linaro.org> Tested-by: Enric Balletbo i Serra <enric.balletbo@collabora.com> Signed-off-by: Lee Jones <lee.jones@linaro.org>
-rw-r--r--drivers/video/backlight/pwm_bl.c24
1 files changed, 6 insertions, 18 deletions
diff --git a/drivers/video/backlight/pwm_bl.c b/drivers/video/backlight/pwm_bl.c
index 1f7f8d5c0bf1..abfd1bca2c11 100644
--- a/drivers/video/backlight/pwm_bl.c
+++ b/drivers/video/backlight/pwm_bl.c
@@ -189,29 +189,17 @@ int pwm_backlight_brightness_default(struct device *dev,
189 struct platform_pwm_backlight_data *data, 189 struct platform_pwm_backlight_data *data,
190 unsigned int period) 190 unsigned int period)
191{ 191{
192 unsigned int counter = 0; 192 unsigned int i;
193 unsigned int i, n;
194 u64 retval; 193 u64 retval;
195 194
196 /* 195 /*
197 * Count the number of bits needed to represent the period number. The 196 * Once we have 4096 levels there's little point going much higher...
198 * number of bits is used to calculate the number of levels used for the 197 * neither interactive sliders nor animation benefits from having
199 * brightness-levels table, the purpose of this calculation is have a 198 * more values in the table.
200 * pre-computed table with enough levels to get linear brightness
201 * perception. The period is divided by the number of bits so for a
202 * 8-bit PWM we have 255 / 8 = 32 brightness levels or for a 16-bit PWM
203 * we have 65535 / 16 = 4096 brightness levels.
204 *
205 * Note that this method is based on empirical testing on different
206 * devices with PWM of 8 and 16 bits of resolution.
207 */ 199 */
208 n = period; 200 data->max_brightness =
209 while (n) { 201 min((int)DIV_ROUND_UP(period, fls(period)), 4096);
210 counter += n % 2;
211 n >>= 1;
212 }
213 202
214 data->max_brightness = DIV_ROUND_UP(period, counter);
215 data->levels = devm_kcalloc(dev, data->max_brightness, 203 data->levels = devm_kcalloc(dev, data->max_brightness,
216 sizeof(*data->levels), GFP_KERNEL); 204 sizeof(*data->levels), GFP_KERNEL);
217 if (!data->levels) 205 if (!data->levels)