[PATCH 2/2] platform/x86: dell-laptop: Fix token-based keyboard brightness readback

From: Yury Norov

Date: Fri Sep 25 2026 - 15:33:43 EST


kbd_get_first_active_token_bit() returns an index into kbd_tokens, while
LED brightness is an ordinal among the supported tokens. These differ
when supported token bits are not consecutive starting at bit 0.

Count the supported token bits below the active token with hweight16()
to recover its zero-based brightness ordinal.

Return zero directly when ret == 0: token bit 0 has no supported tokens
below it, so its brightness ordinal is zero.

Signed-off-by: Yury Norov <ynorov@xxxxxxxxxx>
---
drivers/platform/x86/dell/dell-laptop.c | 10 +++-------
1 file changed, 3 insertions(+), 7 deletions(-)

diff --git a/drivers/platform/x86/dell/dell-laptop.c b/drivers/platform/x86/dell/dell-laptop.c
index 03d3bb70b0d1..55fd48222e5e 100644
--- a/drivers/platform/x86/dell/dell-laptop.c
+++ b/drivers/platform/x86/dell/dell-laptop.c
@@ -2003,7 +2003,6 @@ static const struct attribute_group *kbd_led_groups[] = {
static enum led_brightness kbd_led_level_get(struct led_classdev *led_cdev)
{
int ret;
- u16 num;
struct kbd_state state;

if (kbd_get_max_level()) {
@@ -2018,13 +2017,10 @@ static enum led_brightness kbd_led_level_get(struct led_classdev *led_cdev)

if (kbd_get_valid_token_counts()) {
ret = kbd_get_first_active_token_bit();
- if (ret < 0)
- return 0;
- for (num = kbd_token_bits; num != 0 && ret > 0; --ret)
- num &= num - 1; /* clear the first bit set */
- if (num == 0)
+ if (ret <= 0)
return 0;
- return ffs(num) - 1;
+ /* Brightness is the active token's rank among supported tokens. */
+ return hweight16(kbd_token_bits & GENMASK(ret - 1, 0));
}

pr_warn("Keyboard brightness level control not supported\n");
--
2.53.0