Skip to content

Commit 16dc8bc

Browse files
superm1alexdeucher
authored andcommitted
drm/amd/display: Export full brightness range to userspace
[WHY] Userspace currently is offered a range from 0-0xFF but the PWM is programmed from 0-0xFFFF. This can be limiting to some software that wants to apply greater granularity. [HOW] Convert internally to firmware values only when mapping custom brightness curves because these are in 0-0xFF range. Advertise full PWM range to userspace. Cc: Mario Limonciello <mario.limonciello@amd.com> Cc: Alex Deucher <alexander.deucher@amd.com> Reviewed-by: Roman Li <roman.li@amd.com> Signed-off-by: Mario Limonciello <mario.limonciello@amd.com> Signed-off-by: Alex Hung <alex.hung@amd.com> Tested-by: Daniel Wheeler <daniel.wheeler@amd.com> Signed-off-by: Alex Deucher <alexander.deucher@amd.com> (cherry picked from commit 8dbd72c) Cc: stable@vger.kernel.org
1 parent ffcaed1 commit 16dc8bc

1 file changed

Lines changed: 27 additions & 14 deletions

File tree

drivers/gpu/drm/amd/display/amdgpu_dm/amdgpu_dm.c

Lines changed: 27 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -4718,9 +4718,23 @@ static int get_brightness_range(const struct amdgpu_dm_backlight_caps *caps,
47184718
return 1;
47194719
}
47204720

4721+
/* Rescale from [min..max] to [0..AMDGPU_MAX_BL_LEVEL] */
4722+
static inline u32 scale_input_to_fw(int min, int max, u64 input)
4723+
{
4724+
return DIV_ROUND_CLOSEST_ULL(input * AMDGPU_MAX_BL_LEVEL, max - min);
4725+
}
4726+
4727+
/* Rescale from [0..AMDGPU_MAX_BL_LEVEL] to [min..max] */
4728+
static inline u32 scale_fw_to_input(int min, int max, u64 input)
4729+
{
4730+
return min + DIV_ROUND_CLOSEST_ULL(input * (max - min), AMDGPU_MAX_BL_LEVEL);
4731+
}
4732+
47214733
static void convert_custom_brightness(const struct amdgpu_dm_backlight_caps *caps,
4722-
uint32_t *brightness)
4734+
unsigned int min, unsigned int max,
4735+
uint32_t *user_brightness)
47234736
{
4737+
u32 brightness = scale_input_to_fw(min, max, *user_brightness);
47244738
u8 prev_signal = 0, prev_lum = 0;
47254739
int i = 0;
47264740

@@ -4731,7 +4745,7 @@ static void convert_custom_brightness(const struct amdgpu_dm_backlight_caps *cap
47314745
return;
47324746

47334747
/* choose start to run less interpolation steps */
4734-
if (caps->luminance_data[caps->data_points/2].input_signal > *brightness)
4748+
if (caps->luminance_data[caps->data_points/2].input_signal > brightness)
47354749
i = caps->data_points/2;
47364750
do {
47374751
u8 signal = caps->luminance_data[i].input_signal;
@@ -4742,17 +4756,18 @@ static void convert_custom_brightness(const struct amdgpu_dm_backlight_caps *cap
47424756
* brightness < signal: interpolate between previous and current luminance numerator
47434757
* brightness > signal: find next data point
47444758
*/
4745-
if (*brightness > signal) {
4759+
if (brightness > signal) {
47464760
prev_signal = signal;
47474761
prev_lum = lum;
47484762
i++;
47494763
continue;
47504764
}
4751-
if (*brightness < signal)
4765+
if (brightness < signal)
47524766
lum = prev_lum + DIV_ROUND_CLOSEST((lum - prev_lum) *
4753-
(*brightness - prev_signal),
4767+
(brightness - prev_signal),
47544768
signal - prev_signal);
4755-
*brightness = DIV_ROUND_CLOSEST(lum * *brightness, 101);
4769+
*user_brightness = scale_fw_to_input(min, max,
4770+
DIV_ROUND_CLOSEST(lum * brightness, 101));
47564771
return;
47574772
} while (i < caps->data_points);
47584773
}
@@ -4765,11 +4780,10 @@ static u32 convert_brightness_from_user(const struct amdgpu_dm_backlight_caps *c
47654780
if (!get_brightness_range(caps, &min, &max))
47664781
return brightness;
47674782

4768-
convert_custom_brightness(caps, &brightness);
4783+
convert_custom_brightness(caps, min, max, &brightness);
47694784

4770-
// Rescale 0..255 to min..max
4771-
return min + DIV_ROUND_CLOSEST((max - min) * brightness,
4772-
AMDGPU_MAX_BL_LEVEL);
4785+
// Rescale 0..max to min..max
4786+
return min + DIV_ROUND_CLOSEST_ULL((u64)(max - min) * brightness, max);
47734787
}
47744788

47754789
static u32 convert_brightness_to_user(const struct amdgpu_dm_backlight_caps *caps,
@@ -4782,8 +4796,8 @@ static u32 convert_brightness_to_user(const struct amdgpu_dm_backlight_caps *cap
47824796

47834797
if (brightness < min)
47844798
return 0;
4785-
// Rescale min..max to 0..255
4786-
return DIV_ROUND_CLOSEST(AMDGPU_MAX_BL_LEVEL * (brightness - min),
4799+
// Rescale min..max to 0..max
4800+
return DIV_ROUND_CLOSEST_ULL((u64)max * (brightness - min),
47874801
max - min);
47884802
}
47894803

@@ -4933,11 +4947,10 @@ amdgpu_dm_register_backlight_device(struct amdgpu_dm_connector *aconnector)
49334947
drm_dbg(drm, "Backlight caps: min: %d, max: %d, ac %d, dc %d\n", min, max,
49344948
caps->ac_level, caps->dc_level);
49354949
} else
4936-
props.brightness = AMDGPU_MAX_BL_LEVEL;
4950+
props.brightness = props.max_brightness = AMDGPU_MAX_BL_LEVEL;
49374951

49384952
if (caps->data_points && !(amdgpu_dc_debug_mask & DC_DISABLE_CUSTOM_BRIGHTNESS_CURVE))
49394953
drm_info(drm, "Using custom brightness curve\n");
4940-
props.max_brightness = AMDGPU_MAX_BL_LEVEL;
49414954
props.type = BACKLIGHT_RAW;
49424955

49434956
snprintf(bl_name, sizeof(bl_name), "amdgpu_bl%d",

0 commit comments

Comments
 (0)