[PATCH] platform/x86: ideapad-laptop: Unify keyboard backlight handling

From: Rong Zhang

Date: Fri Dec 04 2026 - 14:42:18 EST


There are multiple ACPI methods controlling the keyboard backlight:
HALS/SALS and KBLC. The former pair are legacy ones, while the latter is
present on modern models. Their divergences lead to inconvenience and
scattered code snippets.

Collect most KBLC code snippets together into a new helper method called
ideapad_kbd_bl_kblc(). HALS/SALS ones are collected into
ideapad_kbd_bl_legacy(), which is called by the former to simulate KBLC.

The KBLC code path is rewritten to transparently handle the type of
keyboard backlight, return value and success status.

Also rename and rearrange the pre-existing macros for KBLC to improve
readability and consistency. Based on my analysis to the DSDT tables of
different devices, some bit fields are added or changed. Particularly,
BIT(0) means success, the type parameter takes 12 bits instead of 4, and
the value parameter takes 16 bits instead of 4.

Since BIT(0) is now checked before returning from ideapad_kbd_bl_kblc(),
a QUERY_TYPE failure no longer prints a warning on devices with a
stubbed KBLC method, and the warning message itself now prints the
correct type (with only the relevant bit field).

Reported-by: Arnav Kapoor <kapoorarnav43@xxxxxxxxx>
Closes: https://msgid.link/20261126073107.765676-1-kapoorarnav43@xxxxxxxxx
Signed-off-by: Rong Zhang <i@xxxxxxxx>
---
drivers/platform/x86/lenovo/ideapad-laptop.c | 165 ++++++++++++++++-----------
1 file changed, 97 insertions(+), 68 deletions(-)

diff --git a/drivers/platform/x86/lenovo/ideapad-laptop.c b/drivers/platform/x86/lenovo/ideapad-laptop.c
index b225dfee18c5..a28e7f8dfb90 100644
--- a/drivers/platform/x86/lenovo/ideapad-laptop.c
+++ b/drivers/platform/x86/lenovo/ideapad-laptop.c
@@ -147,9 +147,11 @@ enum ymc_cmd {
};

/*
- * The enumeration has two purposes:
+ * The enumeration has multiple purposes:
* - as an internal identifier for all known types of keyboard backlight
- * - as a mandatory parameter of the KBLC command
+ * - as a mandatory parameter of the KBLC GET/SET command
+ * - (the latter two) as the return value of KBLC QUERY_TYPE command
+ * - as the max hardware brightness
*
* For each type, the hardware brightness values are defined as follows:
* +--------------------------+----------+-----+------+------+
@@ -173,25 +175,27 @@ enum ymc_cmd {
* | Write | off | low | high |
* +--------------------------+----------+-----+------+
*/
-enum {
+enum kbd_bl_type {
KBD_BL_STANDARD = 1,
KBD_BL_TRISTATE = 2,
KBD_BL_TRISTATE_AUTO = 3,
};

+enum kbd_bl_op {
+ KBD_BL_OP_QUERY_TYPE = 1,
+ KBD_BL_OP_GET_BRIGHTNESS = 2,
+ KBD_BL_OP_SET_BRIGHTNESS = 3,
+};
+
+#define KBD_BL_PARAM_VALUE GENMASK(31, 16)
+#define KBD_BL_PARAM_TYPE GENMASK(15, 4)
+#define KBD_BL_PARAM_OP GENMASK(3, 0)
+
+#define KBD_BL_RETURN_VALUE GENMASK(15, 1)
+#define KBD_BL_RETURN_SUCCESS BIT(0)
+
#define KBD_BL_AUTO_MODE_HW_BRIGHTNESS 3

-#define KBD_BL_QUERY_TYPE 0x1
-#define KBD_BL_TRISTATE_TYPE 0x5
-#define KBD_BL_TRISTATE_AUTO_TYPE 0x7
-
-#define KBD_BL_COMMAND_GET 0x2
-#define KBD_BL_COMMAND_SET 0x3
-#define KBD_BL_COMMAND_TYPE GENMASK(7, 4)
-
-#define KBD_BL_GET_BRIGHTNESS GENMASK(15, 1)
-#define KBD_BL_SET_BRIGHTNESS GENMASK(19, 16)
-
#define KBD_BL_KBLC_CHANGED_EVENT 12

struct ideapad_dytc_priv {
@@ -240,7 +244,7 @@ struct ideapad_private {
* >0: initializion finished
* <0: unregisteration ongoing/finished
*/
- int type;
+ enum kbd_bl_type type;
struct led_classdev led;
unsigned int last_hw_brightness;
struct mutex mutex; /* protects brightness tracking */
@@ -412,11 +416,6 @@ static int exec_sals(acpi_handle handle, unsigned long arg)
return exec_simple_method(handle, "SALS", arg);
}

-static int exec_kblc(acpi_handle handle, unsigned long arg)
-{
- return exec_simple_method(handle, "KBLC", arg);
-}
-
static int eval_kblc(acpi_handle handle, unsigned long cmd, unsigned long *res)
{
return eval_int_with_arg(handle, "KBLC", cmd, res);
@@ -1820,9 +1819,61 @@ static void ideapad_backlight_notify_brightness(struct ideapad_private *priv)
/*
* keyboard backlight
*/
-static int ideapad_kbd_bl_check_tristate(int type)
+static int ideapad_kbd_bl_legacy(struct ideapad_private *priv, enum kbd_bl_op op, u16 value)
{
- return (type == KBD_BL_TRISTATE) || (type == KBD_BL_TRISTATE_AUTO);
+ unsigned long res;
+ int ret;
+
+ switch (op) {
+ case KBD_BL_OP_SET_BRIGHTNESS:
+ return exec_sals(priv->adev->handle, value ? SALS_KBD_BL_ON : SALS_KBD_BL_OFF);
+ case KBD_BL_OP_GET_BRIGHTNESS:
+ ret = eval_hals(priv->adev->handle, &res);
+ return ret < 0 ? ret : test_bit(HALS_KBD_BL_STATE_BIT, &res);
+ default:
+ return -EINVAL;
+ }
+}
+
+static int ideapad_kbd_bl_kblc(struct ideapad_private *priv, enum kbd_bl_op op, u16 value)
+{
+ unsigned long cmd = 0, res;
+ int ret;
+
+ switch (op) {
+ case KBD_BL_OP_SET_BRIGHTNESS:
+ if (value > priv->kbd_bl.type)
+ return -EINVAL;
+
+ cmd |= FIELD_PREP(KBD_BL_PARAM_VALUE, value);
+ fallthrough;
+ case KBD_BL_OP_GET_BRIGHTNESS:
+ switch (priv->kbd_bl.type) {
+ case KBD_BL_TRISTATE:
+ case KBD_BL_TRISTATE_AUTO:
+ cmd |= FIELD_PREP(KBD_BL_PARAM_TYPE, priv->kbd_bl.type);
+ break;
+ case KBD_BL_STANDARD:
+ /* KBLC does not define KBD_BL_STANDARD, simulate it. */
+ return ideapad_kbd_bl_legacy(priv, op, value);
+ default:
+ return -EINVAL;
+ }
+ fallthrough;
+ case KBD_BL_OP_QUERY_TYPE:
+ cmd |= FIELD_PREP(KBD_BL_PARAM_OP, op);
+ break;
+ default:
+ return -EINVAL;
+ }
+
+ ret = eval_kblc(priv->adev->handle, cmd, &res);
+ if (ret < 0)
+ return ret;
+ if (!(FIELD_GET(KBD_BL_RETURN_SUCCESS, res)))
+ return -ENXIO;
+
+ return FIELD_GET(KBD_BL_RETURN_VALUE, res);
}

static int ideapad_kbd_bl_brightness_parse(struct ideapad_private *priv, unsigned int hw_brightness)
@@ -1844,26 +1895,7 @@ static int ideapad_kbd_bl_brightness_parse(struct ideapad_private *priv, unsigne

static int ideapad_kbd_bl_hw_brightness_get(struct ideapad_private *priv)
{
- unsigned long value;
- int err;
-
- if (ideapad_kbd_bl_check_tristate(priv->kbd_bl.type)) {
- err = eval_kblc(priv->adev->handle,
- FIELD_PREP(KBD_BL_COMMAND_TYPE, priv->kbd_bl.type) |
- KBD_BL_COMMAND_GET,
- &value);
-
- if (err)
- return err;
-
- return FIELD_GET(KBD_BL_GET_BRIGHTNESS, value);
- }
-
- err = eval_hals(priv->adev->handle, &value);
- if (err)
- return err;
-
- return !!test_bit(HALS_KBD_BL_STATE_BIT, &value);
+ return ideapad_kbd_bl_kblc(priv, KBD_BL_OP_GET_BRIGHTNESS, 0);
}

static int ideapad_kbd_bl_brightness_get(struct ideapad_private *priv)
@@ -1886,23 +1918,13 @@ static enum led_brightness ideapad_kbd_bl_led_cdev_brightness_get(struct led_cla
static int ideapad_kbd_bl_hw_brightness_set(struct ideapad_private *priv,
unsigned int hw_brightness)
{
- unsigned long value;
- int type = priv->kbd_bl.type;
- int err;
+ int ret;

guard(mutex)(&priv->kbd_bl.mutex);

- if (ideapad_kbd_bl_check_tristate(type)) {
- value = FIELD_PREP(KBD_BL_SET_BRIGHTNESS, hw_brightness) |
- FIELD_PREP(KBD_BL_COMMAND_TYPE, type) |
- KBD_BL_COMMAND_SET;
- err = exec_kblc(priv->adev->handle, value);
- } else {
- value = hw_brightness ? SALS_KBD_BL_ON : SALS_KBD_BL_OFF;
- err = exec_sals(priv->adev->handle, value);
- }
- if (err)
- return err;
+ ret = ideapad_kbd_bl_kblc(priv, KBD_BL_OP_SET_BRIGHTNESS, hw_brightness);
+ if (ret < 0)
+ return ret;

priv->kbd_bl.last_hw_brightness = hw_brightness;

@@ -2684,18 +2706,25 @@ static int ideapad_check_features(struct ideapad_private *priv)
}

if (acpi_has_method(handle, "KBLC")) {
- if (!eval_kblc(priv->adev->handle, KBD_BL_QUERY_TYPE, &val)) {
- if (val == KBD_BL_TRISTATE_TYPE) {
- priv->features.kbd_bl = true;
- priv->kbd_bl.type = KBD_BL_TRISTATE;
- } else if (val == KBD_BL_TRISTATE_AUTO_TYPE) {
- priv->features.kbd_bl = true;
- priv->kbd_bl.type = KBD_BL_TRISTATE_AUTO;
- } else {
- dev_warn(&priv->platform_device->dev,
- "Unknown keyboard type: %lu",
- val);
- }
+ int ret = ideapad_kbd_bl_kblc(priv, KBD_BL_OP_QUERY_TYPE, 0);
+
+ switch (ret) {
+ case KBD_BL_TRISTATE_AUTO:
+ case KBD_BL_TRISTATE:
+ priv->features.kbd_bl = true;
+ priv->kbd_bl.type = ret;
+ break;
+ /* KBLC does not define KBD_BL_STANDARD. */
+ default:
+ /*
+ * Stubbed method. The device either has no keyboard
+ * backlight or exposes it via a non-ACPI interface.
+ */
+ if (ret < 0)
+ break;
+
+ dev_warn(&priv->platform_device->dev, "Unknown keyboard type: %d\n", ret);
+ break;
}
}


---
base-commit: fea9dfe6c4d93dc3f4b4b3534dc25bf283eaef08
change-id: ff0c0f96-ideapad-refine-kblc-b84742028289

Thanks,
Rong