backlight: pwm_bl: Fix heuristic to determine number of brightness levels
[sfrench/cifs-2.6.git] / drivers / video / backlight / pwm_bl.c
index 1f7f8d5c0bf176233ab69998579b4593caaf5cb6..abfd1bca2c11f9f1f92eb17f75b379dcf94a3dbc 100644 (file)
@@ -189,29 +189,17 @@ int pwm_backlight_brightness_default(struct device *dev,
                                     struct platform_pwm_backlight_data *data,
                                     unsigned int period)
 {
-       unsigned int counter = 0;
-       unsigned int i, n;
+       unsigned int i;
        u64 retval;
 
        /*
-        * Count the number of bits needed to represent the period number. The
-        * number of bits is used to calculate the number of levels used for the
-        * brightness-levels table, the purpose of this calculation is have a
-        * pre-computed table with enough levels to get linear brightness
-        * perception. The period is divided by the number of bits so for a
-        * 8-bit PWM we have 255 / 8 = 32 brightness levels or for a 16-bit PWM
-        * we have 65535 / 16 = 4096 brightness levels.
-        *
-        * Note that this method is based on empirical testing on different
-        * devices with PWM of 8 and 16 bits of resolution.
+        * Once we have 4096 levels there's little point going much higher...
+        * neither interactive sliders nor animation benefits from having
+        * more values in the table.
         */
-       n = period;
-       while (n) {
-               counter += n % 2;
-               n >>= 1;
-       }
+       data->max_brightness =
+               min((int)DIV_ROUND_UP(period, fls(period)), 4096);
 
-       data->max_brightness = DIV_ROUND_UP(period, counter);
        data->levels = devm_kcalloc(dev, data->max_brightness,
                                    sizeof(*data->levels), GFP_KERNEL);
        if (!data->levels)