Re: [PATCH v4 6/6] platform/x86: thinkpad_acpi: Add a keyboard backlight auto-mode trigger

From: Rong Zhang

Date: Wed Nov 25 2026 - 08:24:25 EST


Hi Qiling,

On Wed, 2026-11-25 at 16:46 +0800, Qiling Zhu wrote:
> On ThinkPads that advertise the auto mode in MLCG bit 20, expose EC
> level 3 as a private LED trigger named "tpacpi-kbd-auto". Select
> LEDS_TRIGGERS so that the trigger is available in stripped-down
> configurations. If registering the trigger fails, register the
> keyboard backlight LED as before.
>
> Activating the trigger writes EC level 3 and records it in the cached
> level; whether the LED is hardware-controlled is derived from that
> cached level, so reading brightness returns -ENODATA while the trigger
> is active and the EC is in auto mode. If the EC is already in auto
> mode when the driver loads, make the trigger the default one, to
> reflect a selection made before the driver was loaded.
>
> A sysfs brightness write leaves auto mode through the LED core's own
> trigger removal. With CONFIG_LEDS_TRIGGERS_HW_CHANGED enabled, the LED
> core also switches between "none" and the trigger in response to
> Fn+Space, or to a procfs write that leaves auto mode.
>
> Deliberately unchanged: max_brightness stays 2.
>
> Signed-off-by: Qiling Zhu <kmatzkaczor244@xxxxxxxxx>

Reviewed-by: Rong Zhang <i@xxxxxxxx>

Thanks,
Rong

> ---
> drivers/platform/x86/lenovo/Kconfig | 1 +
> drivers/platform/x86/lenovo/thinkpad_acpi.c | 60 +++++++++++++++++++++
> 2 files changed, 61 insertions(+)
>
> diff --git a/drivers/platform/x86/lenovo/Kconfig b/drivers/platform/x86/lenovo/Kconfig
> index 516594993..77d159a3a 100644
> --- a/drivers/platform/x86/lenovo/Kconfig
> +++ b/drivers/platform/x86/lenovo/Kconfig
> @@ -83,6 +83,7 @@ config THINKPAD_ACPI
> select NVRAM
> select NEW_LEDS
> select LEDS_CLASS
> + select LEDS_TRIGGERS
> select INPUT_SPARSEKMAP
> help
> This is a driver for the IBM and Lenovo ThinkPad laptops. It adds
> diff --git a/drivers/platform/x86/lenovo/thinkpad_acpi.c b/drivers/platform/x86/lenovo/thinkpad_acpi.c
> index ee8f7429d..f9a835fe2 100644
> --- a/drivers/platform/x86/lenovo/thinkpad_acpi.c
> +++ b/drivers/platform/x86/lenovo/thinkpad_acpi.c
> @@ -5222,6 +5222,27 @@ static bool kbdlight_is_supported(void)
> return status & BIT(9);
> }
>
> +static struct led_hw_trigger_type kbdlight_auto_trigger_type;
> +
> +static int kbdlight_auto_trigger_activate(struct led_classdev *led_cdev)
> +{
> + return kbdlight_set_level_and_update(TPACPI_KBD_LIGHT_AUTO_LEVEL);
> +}
> +
> +static bool kbdlight_auto_trigger_hw_offloaded(struct led_classdev *led_cdev)
> +{
> + guard(mutex)(&kbdlight_mutex);
> +
> + return kbdlight_brightness == TPACPI_KBD_LIGHT_AUTO_LEVEL;
> +}
> +
> +static struct led_trigger kbdlight_auto_trigger = {
> + .name = "tpacpi-kbd-auto",
> + .trigger_type = &kbdlight_auto_trigger_type,
> + .activate = kbdlight_auto_trigger_activate,
> + .hw_offloaded = kbdlight_auto_trigger_hw_offloaded,
> +};
> +
> static int kbdlight_sysfs_set(struct led_classdev *led_cdev,
> enum led_brightness brightness)
> {
> @@ -5249,6 +5270,35 @@ static struct tpacpi_led_classdev tpacpi_led_kbdlight = {
> }
> };
>
> +static void __init kbdlight_init_auto_trigger(void)
> +{
> + struct led_classdev *led_cdev = &tpacpi_led_kbdlight.led_classdev;
> + int rc;
> +
> + rc = devm_led_trigger_register(&tpacpi_pdev->dev, &kbdlight_auto_trigger);
> + if (rc) {
> + /*
> + * Auto-mode trigger unavailable; basic backlight
> + * control keeps working.
> + */
> + dev_warn(&tpacpi_pdev->dev, "auto-mode trigger unavailable\n");
> + return;
> + }
> +
> + led_cdev->flags |= LED_TRIG_HW_CHANGED;
> + led_cdev->trigger_type = &kbdlight_auto_trigger_type;
> + led_cdev->hw_control_trigger = kbdlight_auto_trigger.name;
> +
> + /*
> + * The EC does not keep the level across boots, but it may already
> + * be in auto mode when the driver loads: Fn+Space works at the
> + * BIOS or disk-encryption prompt, and while the module is
> + * unloaded. Sync the initial trigger state with the EC then.
> + */
> + if (kbdlight_brightness == TPACPI_KBD_LIGHT_AUTO_LEVEL)
> + led_cdev->default_trigger = kbdlight_auto_trigger.name;
> +}
> +
> static int __init kbdlight_init(struct ibm_init_struct *iibm)
> {
> int rc;
> @@ -5269,6 +5319,9 @@ static int __init kbdlight_init(struct ibm_init_struct *iibm)
>
> tp_features.kbdlight = 1;
>
> + if (tp_features.kbdlight_auto)
> + kbdlight_init_auto_trigger();
> +
> rc = led_classdev_register(&tpacpi_pdev->dev,
> &tpacpi_led_kbdlight.led_classdev);
> if (rc < 0) {
> @@ -5289,6 +5342,7 @@ static void kbdlight_exit(void)
> static void kbdlight_notify_level(int level)
> {
> struct led_classdev *led_cdev = &tpacpi_led_kbdlight.led_classdev;
> + bool was_auto, is_auto;
>
> lockdep_assert_held(&kbdlight_mutex);
>
> @@ -5299,6 +5353,12 @@ static void kbdlight_notify_level(int level)
> if (level == kbdlight_brightness)
> return;
>
> + was_auto = kbdlight_brightness == TPACPI_KBD_LIGHT_AUTO_LEVEL;
> + is_auto = level == TPACPI_KBD_LIGHT_AUTO_LEVEL;
> +
> + if (is_auto != was_auto && led_cdev->hw_control_trigger)
> + led_trigger_notify_hw_control_changed(led_cdev, is_auto);
> +
> kbdlight_brightness = level;
> led_classdev_notify_brightness_hw_changed(led_cdev, kbdlight_level_to_brightness(level));
> }