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.
Unified · Split
| @@ -387,6 +387,31 @@ int pwm_backlight_brightness_default(struct device *dev, · 386 unmodified lines | |||
| 387 | } | 387 | } |
| 388 | #endif | 388 | #endif |
| 389 | 389 | ||
| 390 | static bool pwm_backlight_is_linear(struct platform_pwm_backlight_data *data) | ||
| 391 | { | ||
| 392 | unsigned int nlevels = data->max_brightness + 1; | ||
| 393 | unsigned int min_val = data->levels[0]; | ||
| 394 | unsigned int max_val = data->levels[nlevels - 1]; | ||
| 395 | /* | ||
| 396 | * Multiplying by 128 means that even in pathological cases such | ||
| 397 | * as (max_val - min_val) == nlevels the error at max_val is less | ||
| 398 | * than 1%. | ||
| 399 | */ | ||
| 400 | unsigned int slope = (128 * (max_val - min_val)) / nlevels; | ||
| 401 | unsigned int margin = (max_val - min_val) / 20; /* 5% */ | ||
| 402 | int i; | ||
| 403 | |||
| 404 | for (i = 1; i < nlevels; i++) { | ||
| 405 | unsigned int linear_value = min_val + ((i * slope) / 128); | ||
| 406 | unsigned int delta = abs(linear_value - data->levels[i]); | ||
| 407 | |||
| 408 | if (delta > margin) | ||
| 409 | return false; | ||
| 410 | } | ||
| 411 | |||
| 412 | return true; | ||
| 413 | } | ||
| 414 | |||
| 390 | static int pwm_backlight_initial_power_state(const struct pwm_bl_data *pb) | 415 | static int pwm_backlight_initial_power_state(const struct pwm_bl_data *pb) |
| 391 | { | 416 | { |
| 392 | struct device_node *node = pb->dev->of_node; | 417 | struct device_node *node = pb->dev->of_node; |
| @@ -550,6 +575,11 @@ static int pwm_backlight_probe(struct platform_device *pdev) · 157 unmodified lines | |||
| 550 | 575 | ||
| 551 | pb->levels = data->levels; | 576 | pb->levels = data->levels; |
| 552 | } | 577 | } |
| 578 | |||
| 579 | if (pwm_backlight_is_linear(data)) | ||
| 580 | props.scale = BACKLIGHT_SCALE_LINEAR; | ||
| 581 | else | ||
| 582 | props.scale = BACKLIGHT_SCALE_NON_LINEAR; | ||
| 553 | } else if (!data->max_brightness) { | 583 | } else if (!data->max_brightness) { |
| 554 | /* | 584 | /* |
| 555 | * If no brightness levels are provided and max_brightness is | 585 | * If no brightness levels are provided and max_brightness is |