Linux LED subsystem development
 help / color / mirror / Atom feed
* [PATCH v5 0/6] platform/x86: thinkpad_acpi: Add a keyboard backlight auto-mode trigger
@ 2026-11-30 13:12 Qiling Zhu
  2026-11-30 13:12 ` [PATCH v5 1/6] platform/x86: thinkpad_acpi: Report auto keyboard backlight mode as off Qiling Zhu
                   ` (6 more replies)
  0 siblings, 7 replies; 8+ messages in thread
From: Qiling Zhu @ 2026-11-30 13:12 UTC (permalink / raw)
  To: Mark Pearson, Derek J. Clark, Rong Zhang, Hans de Goede,
	Ilpo Järvinen, Henrique de Moraes Holschuh
  Cc: platform-driver-x86, linux-kernel, ibm-acpi-devel, Lee Jones,
	Pavel Machek, Griffin Kroah-Hartman, linux-leds, vishnuocv,
	vsankar, Andrea Rabbaglietti

Hi all,

On supported ThinkPads, Fn+Space can select automatic keyboard
backlight control.  This series exposes that mode through the private
LED trigger "tpacpi-kbd-auto".  The first patch fixes an out-of-range
brightness value that was reported to make UPower stop tracking hotkey
changes on a ThinkPad X9-14 Gen 1.

This is v5 of the series.  Thank you, Rong and Mark, for reviewing v4.
The changes are small: the code at the end of the series is the same as
in v4 apart from one comment dropped in 5/6, and what moved is in the
patch boundaries and the commit messages.  For reference, v4 is here:

  https://lore.kernel.org/r/20261125084644.137506-1-kmatzkaczor244@gmail.com/

Changes since v4:

- 2/6 now contains the two changes that 4/6 used to make to the notify
  helpers, which let them work in EC levels, as Rong suggested while
  reviewing 4/6.  The commit message describes how this differs from
  the original inline code: a change between off and the auto mode is
  reported through brightness_hw_changed when the cached value holds
  the EC level, and a failed read of the level sends no notification.

- 4/6 is smaller accordingly and no longer touches the helpers; its
  commit message is adjusted to match.

- 5/6 drops the comment that repeated the one above
  TPACPI_KBD_LIGHT_AUTO_CAPABLE_BIT, as Rong suggested.

- 1/6, 3/6 and 6/6 are unchanged in code.

On the Reviewed-by tags: I have added the tags that Rong and Mark gave
on v4 and on earlier versions to the patches they reviewed, and kept
them where a patch changed a little afterwards.  My understanding is
that small changes do not invalidate a tag.  If a tag does not fit the
patch it is on now, please tell me and I will remove it.

As the end result is unchanged, I have not repeated the hardware tests
from v4: they cover s2idle and S4 on a ThinkPad X1 Carbon Gen 14, from
manual levels and from auto mode.  S3-capable models and other models
are still untested, and testing there would be very welcome.  Each
commit of this version builds without warnings.

The checkpatch.pl --strict warnings I see are for three commit IDs that
are missing from my shallow clone.

This series was developed with LLM assistance; I reviewed every design
decision against the framework sources and validated the behavior on
real hardware myself.

Regards,
Qiling Zhu

^ permalink raw reply	[flat|nested] 8+ messages in thread

* [PATCH v5 1/6] platform/x86: thinkpad_acpi: Report auto keyboard backlight mode as off
  2026-11-30 13:12 [PATCH v5 0/6] platform/x86: thinkpad_acpi: Add a keyboard backlight auto-mode trigger Qiling Zhu
@ 2026-11-30 13:12 ` Qiling Zhu
  2026-11-30 13:12 ` [PATCH v5 2/6] platform/x86: thinkpad_acpi: Extract kbdlight notify helpers Qiling Zhu
                   ` (5 subsequent siblings)
  6 siblings, 0 replies; 8+ messages in thread
From: Qiling Zhu @ 2026-11-30 13:12 UTC (permalink / raw)
  To: Mark Pearson, Derek J. Clark, Rong Zhang, Hans de Goede,
	Ilpo Järvinen, Henrique de Moraes Holschuh
  Cc: platform-driver-x86, linux-kernel, ibm-acpi-devel, Lee Jones,
	Pavel Machek, Griffin Kroah-Hartman, linux-leds, vishnuocv,
	vsankar, Andrea Rabbaglietti

The EC keyboard backlight level 3, which selects the auto mode, is
passed to the LED classdev verbatim by kbdlight_sysfs_get().  The LED
then reports a brightness of 3 with max_brightness 2, both through the
brightness sysfs attribute and through brightness_hw_changed
notifications.

This is not only cosmetic.  According to a report on a ThinkPad X9-14
Gen 1, UPower 1.91.x treats the out-of-range value as invalid and stops
watching brightness_hw_changed, so the desktop no longer follows the
Fn+Space hotkey after auto mode has been entered.

The auto mode is not a brightness level.  Report it as 0, which is
within the exported brightness range, in a new helper,
kbdlight_level_to_brightness(), as ideapad-laptop does for its auto
mode.

Fixes: bb28f3d51ff5 ("thinkpad_acpi: Add support for keyboard backlight")
Reported-by: Andrea Rabbaglietti <rabbagliettiandrea@gmail.com>
Closes: https://lore.kernel.org/r/CAENjc+w0Rqy=kDSew7XtNJCSvyPiXPJ5faWdmCr5RGYFhpunCA@mail.gmail.com
Cc: stable@vger.kernel.org
Signed-off-by: Qiling Zhu <kmatzkaczor244@gmail.com>
Reviewed-by: Rong Zhang <i@rong.moe>
Reviewed-by: Mark Pearson <mpearson-lenovo@squebb.ca>

---
With this patch on its own, on kernels that still use the LED core
suspend/resume path, a backlight that went to sleep in auto mode comes
back off after resume; before, it came back in auto mode, since that
path wrote the unmapped value back.  The rest of this series restores
the level the EC had.
---
 drivers/platform/x86/lenovo/thinkpad_acpi.c | 14 +++++++++++++-
 1 file changed, 13 insertions(+), 1 deletion(-)

diff --git a/drivers/platform/x86/lenovo/thinkpad_acpi.c b/drivers/platform/x86/lenovo/thinkpad_acpi.c
index 4f81855ad..31d3721bb 100644
--- a/drivers/platform/x86/lenovo/thinkpad_acpi.c
+++ b/drivers/platform/x86/lenovo/thinkpad_acpi.c
@@ -5102,6 +5102,9 @@ static struct ibm_struct video_driver_data = {
  * Keyboard backlight subdriver
  */
 
+/* EC level that selects the auto mode for the keyboard backlight */
+#define TPACPI_KBD_LIGHT_AUTO_LEVEL 3
+
 static enum led_brightness kbdlight_brightness;
 static DEFINE_MUTEX(kbdlight_mutex);
 
@@ -5135,6 +5138,15 @@ static int kbdlight_get_level(void)
 	return status & 0x3;
 }
 
+static int kbdlight_level_to_brightness(int level)
+{
+	/* Auto mode is not a brightness level; report it as off. */
+	if (level == TPACPI_KBD_LIGHT_AUTO_LEVEL)
+		return 0;
+
+	return level;
+}
+
 static bool kbdlight_is_supported(void)
 {
 	int status = 0;
@@ -5194,7 +5206,7 @@ static enum led_brightness kbdlight_sysfs_get(struct led_classdev *led_cdev)
 	if (level < 0)
 		return 0;
 
-	return level;
+	return kbdlight_level_to_brightness(level);
 }
 
 static struct tpacpi_led_classdev tpacpi_led_kbdlight = {
-- 
2.56.1


^ permalink raw reply related	[flat|nested] 8+ messages in thread

* [PATCH v5 2/6] platform/x86: thinkpad_acpi: Extract kbdlight notify helpers
  2026-11-30 13:12 [PATCH v5 0/6] platform/x86: thinkpad_acpi: Add a keyboard backlight auto-mode trigger Qiling Zhu
  2026-11-30 13:12 ` [PATCH v5 1/6] platform/x86: thinkpad_acpi: Report auto keyboard backlight mode as off Qiling Zhu
@ 2026-11-30 13:12 ` Qiling Zhu
  2026-11-30 13:12 ` [PATCH v5 3/6] platform/x86: thinkpad_acpi: Don't poke keyboard backlight's LED core brightness Qiling Zhu
                   ` (4 subsequent siblings)
  6 siblings, 0 replies; 8+ messages in thread
From: Qiling Zhu @ 2026-11-30 13:12 UTC (permalink / raw)
  To: Mark Pearson, Derek J. Clark, Rong Zhang, Hans de Goede,
	Ilpo Järvinen, Henrique de Moraes Holschuh
  Cc: platform-driver-x86, linux-kernel, ibm-acpi-devel, Lee Jones,
	Pavel Machek, Griffin Kroah-Hartman, linux-leds, vishnuocv,
	vsankar, Andrea Rabbaglietti

TP_HKEY_EV_KBD_LIGHT handling in tpacpi_driver_event() reads back the
brightness, compares it with the cached value and notifies the LED core
inline.  Extract this block into two helpers: kbdlight_notify_level()
takes an EC level, compares it with the cached value, updates the cache
and notifies the LED core, translating the level to a brightness on the
way, as kbdlight_sysfs_get() does; kbdlight_notify_change() reads the
level back and calls the former under kbdlight_mutex.

kbdlight_notify_level() expects the caller to hold kbdlight_mutex, so
that the proc write path can reuse it with the level it has just
written in a subsequent change.

The helpers work in EC levels rather than brightness values, as the
rest of the series builds on that.  This differs from the original code
in two situations: a change from off to the auto mode or back, which
reads back as the same brightness, now produces a notification
reporting 0 when the cached value holds the EC level, where the
original code sent none; and a failed read of the level now sends no
notification and leaves the cached value alone, where the original code
went on with a brightness of 0.

kbdlight_brightness is still an enum led_brightness at this point, and
kbdlight_init() still fills it in from the reported brightness; a later
change in this series makes it an int that tracks the EC level from
init on.

Signed-off-by: Qiling Zhu <kmatzkaczor244@gmail.com>
Reviewed-by: Rong Zhang <i@rong.moe>
Reviewed-by: Mark Pearson <mpearson-lenovo@squebb.ca>
---
 drivers/platform/x86/lenovo/thinkpad_acpi.c | 48 ++++++++++++++-------
 1 file changed, 32 insertions(+), 16 deletions(-)

diff --git a/drivers/platform/x86/lenovo/thinkpad_acpi.c b/drivers/platform/x86/lenovo/thinkpad_acpi.c
index 31d3721bb..3ef857542 100644
--- a/drivers/platform/x86/lenovo/thinkpad_acpi.c
+++ b/drivers/platform/x86/lenovo/thinkpad_acpi.c
@@ -5267,6 +5267,36 @@ static int kbdlight_set_level_and_update(int level)
 	return ret;
 }
 
+static void kbdlight_notify_level(int level)
+{
+	struct led_classdev *led_cdev = &tpacpi_led_kbdlight.led_classdev;
+
+	lockdep_assert_held(&kbdlight_mutex);
+
+	/*
+	 * Check the level actually changed, setting the level
+	 * through kbdlight_set_level() also triggers this event.
+	 */
+	if (level == kbdlight_brightness)
+		return;
+
+	kbdlight_brightness = level;
+	led_classdev_notify_brightness_hw_changed(led_cdev, kbdlight_level_to_brightness(level));
+}
+
+static void kbdlight_notify_change(void)
+{
+	int level;
+
+	guard(mutex)(&kbdlight_mutex);
+
+	level = kbdlight_get_level();
+	if (level < 0)
+		return;
+
+	kbdlight_notify_level(level);
+}
+
 static int kbdlight_read(struct seq_file *m)
 {
 	int level;
@@ -11593,22 +11623,8 @@ static bool tpacpi_driver_event(const unsigned int hkey_event)
 		/* Key events are suppressed by default hotkey_user_mask */
 		return false;
 	case TP_HKEY_EV_KBD_LIGHT:
-		if (tp_features.kbdlight) {
-			enum led_brightness brightness;
-
-			guard(mutex)(&kbdlight_mutex);
-
-			/*
-			 * Check the brightness actually changed, setting the brightness
-			 * through kbdlight_set_level() also triggers this event.
-			 */
-			brightness = kbdlight_sysfs_get(NULL);
-			if (kbdlight_brightness != brightness) {
-				kbdlight_brightness = brightness;
-				led_classdev_notify_brightness_hw_changed(
-					&tpacpi_led_kbdlight.led_classdev, brightness);
-			}
-		}
+		if (tp_features.kbdlight)
+			kbdlight_notify_change();
 		/* Key events are suppressed by default hotkey_user_mask */
 		return false;
 	case TP_HKEY_EV_DFR_CHANGE_ROW:
-- 
2.56.1


^ permalink raw reply related	[flat|nested] 8+ messages in thread

* [PATCH v5 3/6] platform/x86: thinkpad_acpi: Don't poke keyboard backlight's LED core brightness
  2026-11-30 13:12 [PATCH v5 0/6] platform/x86: thinkpad_acpi: Add a keyboard backlight auto-mode trigger Qiling Zhu
  2026-11-30 13:12 ` [PATCH v5 1/6] platform/x86: thinkpad_acpi: Report auto keyboard backlight mode as off Qiling Zhu
  2026-11-30 13:12 ` [PATCH v5 2/6] platform/x86: thinkpad_acpi: Extract kbdlight notify helpers Qiling Zhu
@ 2026-11-30 13:12 ` Qiling Zhu
  2026-11-30 13:12 ` [PATCH v5 4/6] platform/x86: thinkpad_acpi: Restore keyboard backlight level ourselves Qiling Zhu
                   ` (3 subsequent siblings)
  6 siblings, 0 replies; 8+ messages in thread
From: Qiling Zhu @ 2026-11-30 13:12 UTC (permalink / raw)
  To: Mark Pearson, Derek J. Clark, Rong Zhang, Hans de Goede,
	Ilpo Järvinen, Henrique de Moraes Holschuh
  Cc: platform-driver-x86, linux-kernel, ibm-acpi-devel, Lee Jones,
	Pavel Machek, Griffin Kroah-Hartman, linux-leds, vishnuocv,
	vsankar, Andrea Rabbaglietti

A write to /proc/acpi/ibm/kbdlight writes the EC level and updates the
cached level under kbdlight_mutex, but then assigns led_cdev->brightness
directly after releasing the lock, without telling the LED core.
Commit 06da5325d02e ("platform/x86: thinkpad_acpi: Stop setting
led_classdev brightness directly") already removed this pattern from the
sysfs path but had to keep it for the proc interface, which has no LED
core path of its own.

Stop writing to that cache.  kbdlight_set_level_and_update() now only
writes the EC level and updates the cached level, under kbdlight_mutex;
the sysfs path uses it, as a write that already goes through the LED core
does not need to be reported back to it.  The proc write path instead
calls kbdlight_notify_level(), which updates the cached level and reports
the new one through brightness_hw_changed, like commit 07f48f668fac
("platform/x86: ideapad-laptop: add FnLock LED class device") did for
the ideapad FnLock LED.

Signed-off-by: Qiling Zhu <kmatzkaczor244@gmail.com>
Reviewed-by: Rong Zhang <i@rong.moe>

---
After a procfs write, led_cdev->brightness can retain its previous
value.  This is acceptable:

- Reading the brightness attribute calls brightness_get(), which reads
  the level back from the EC and refreshes the cache.
- At this point in the series, kbdlight_suspend() also refreshes the
  cache before suspend, so led_classdev_resume() does not write a stale
  value back.
- Software blinking may pick up the old value as its "on" level, as it
  can after a Fn+Space change.
---
 drivers/platform/x86/lenovo/thinkpad_acpi.c | 44 ++++++++++++---------
 1 file changed, 26 insertions(+), 18 deletions(-)

diff --git a/drivers/platform/x86/lenovo/thinkpad_acpi.c b/drivers/platform/x86/lenovo/thinkpad_acpi.c
index 3ef857542..80601ed2c 100644
--- a/drivers/platform/x86/lenovo/thinkpad_acpi.c
+++ b/drivers/platform/x86/lenovo/thinkpad_acpi.c
@@ -5113,12 +5113,11 @@ static int kbdlight_set_level(int level)
 	if (!hkey_handle)
 		return -ENXIO;
 
-	guard(mutex)(&kbdlight_mutex);
+	lockdep_assert_held(&kbdlight_mutex);
 
 	if (!acpi_evalf(hkey_handle, NULL, "MLCS", "dd", level))
 		return -EIO;
 
-	kbdlight_brightness = level;
 	return 0;
 }
 
@@ -5147,6 +5146,21 @@ static int kbdlight_level_to_brightness(int level)
 	return level;
 }
 
+static int kbdlight_set_level_and_update(int level)
+{
+	int ret;
+
+	guard(mutex)(&kbdlight_mutex);
+
+	ret = kbdlight_set_level(level);
+	if (ret)
+		return ret;
+
+	kbdlight_brightness = level;
+
+	return 0;
+}
+
 static bool kbdlight_is_supported(void)
 {
 	int status = 0;
@@ -5195,7 +5209,7 @@ static bool kbdlight_is_supported(void)
 static int kbdlight_sysfs_set(struct led_classdev *led_cdev,
 			enum led_brightness brightness)
 {
-	return kbdlight_set_level(brightness);
+	return kbdlight_set_level_and_update(brightness);
 }
 
 static enum led_brightness kbdlight_sysfs_get(struct led_classdev *led_cdev)
@@ -5253,20 +5267,6 @@ static void kbdlight_exit(void)
 	led_classdev_unregister(&tpacpi_led_kbdlight.led_classdev);
 }
 
-static int kbdlight_set_level_and_update(int level)
-{
-	int ret;
-	struct led_classdev *led_cdev;
-
-	ret = kbdlight_set_level(level);
-	led_cdev = &tpacpi_led_kbdlight.led_classdev;
-
-	if (ret == 0 && !(led_cdev->flags & LED_SUSPENDED))
-		led_cdev->brightness = level;
-
-	return ret;
-}
-
 static void kbdlight_notify_level(int level)
 {
 	struct led_classdev *led_cdev = &tpacpi_led_kbdlight.led_classdev;
@@ -5332,7 +5332,15 @@ static int kbdlight_write(char *buf)
 	if (level >= 3 || level < 0)
 		return -EINVAL;
 
-	return kbdlight_set_level_and_update(level);
+	guard(mutex)(&kbdlight_mutex);
+
+	res = kbdlight_set_level(level);
+	if (res)
+		return res;
+
+	kbdlight_notify_level(level);
+
+	return 0;
 }
 
 static void kbdlight_suspend(void)
-- 
2.56.1


^ permalink raw reply related	[flat|nested] 8+ messages in thread

* [PATCH v5 4/6] platform/x86: thinkpad_acpi: Restore keyboard backlight level ourselves
  2026-11-30 13:12 [PATCH v5 0/6] platform/x86: thinkpad_acpi: Add a keyboard backlight auto-mode trigger Qiling Zhu
                   ` (2 preceding siblings ...)
  2026-11-30 13:12 ` [PATCH v5 3/6] platform/x86: thinkpad_acpi: Don't poke keyboard backlight's LED core brightness Qiling Zhu
@ 2026-11-30 13:12 ` Qiling Zhu
  2026-11-30 13:12 ` [PATCH v5 5/6] platform/x86: thinkpad_acpi: Detect auto-mode capability via MLCG Qiling Zhu
                   ` (2 subsequent siblings)
  6 siblings, 0 replies; 8+ messages in thread
From: Qiling Zhu @ 2026-11-30 13:12 UTC (permalink / raw)
  To: Mark Pearson, Derek J. Clark, Rong Zhang, Hans de Goede,
	Ilpo Järvinen, Henrique de Moraes Holschuh
  Cc: platform-driver-x86, linux-kernel, ibm-acpi-devel, Lee Jones,
	Pavel Machek, Griffin Kroah-Hartman, linux-leds, vishnuocv,
	vsankar, Andrea Rabbaglietti

kbdlight_suspend() and kbdlight_resume() leave the keyboard backlight to
the LED core across sleep: the LED core caches the current brightness
and turns the backlight off on suspend, and writes that brightness back
on resume.

On a ThinkPad X1 Carbon Gen 14 with thinkpad_acpi unloaded, the firmware
turned the backlight off when entering both s2idle and hibernation.
After waking from s2idle the EC was back at its previous level, for each
of the four levels tested; after waking from hibernation it was at 0.

Drop kbdlight_suspend() and leave turning the backlight off to the
firmware.  On resume, write the cached level back to the EC on all
models, instead of relying on the LED core: this brings the backlight
back where the firmware came up with it off, and changes nothing
where the EC already has that level.  For this to hold for every
mode, have kbdlight_brightness track the EC level itself, 0 to 3,
rather than the brightness reported to the LED core, in which the auto
mode (level 3) shows up as 0.  kbdlight_init() accordingly reads the
level from the EC instead of the brightness; the notify helpers already
work in terms of EC levels.

Only s2idle and S4 were available on the test machine; S3-capable
models were not tested.

Signed-off-by: Qiling Zhu <kmatzkaczor244@gmail.com>
Reviewed-by: Rong Zhang <i@rong.moe>
---
 drivers/platform/x86/lenovo/thinkpad_acpi.c | 28 +++++++++------------
 1 file changed, 12 insertions(+), 16 deletions(-)

diff --git a/drivers/platform/x86/lenovo/thinkpad_acpi.c b/drivers/platform/x86/lenovo/thinkpad_acpi.c
index 80601ed2c..764b1efa6 100644
--- a/drivers/platform/x86/lenovo/thinkpad_acpi.c
+++ b/drivers/platform/x86/lenovo/thinkpad_acpi.c
@@ -5105,7 +5105,12 @@ static struct ibm_struct video_driver_data = {
 /* EC level that selects the auto mode for the keyboard backlight */
 #define TPACPI_KBD_LIGHT_AUTO_LEVEL 3
 
-static enum led_brightness kbdlight_brightness;
+/*
+ * Cached EC level, including TPACPI_KBD_LIGHT_AUTO_LEVEL.  All state
+ * tracking compares EC levels; the auto mode is translated to a
+ * brightness value only when reporting to the LED core.
+ */
+static int kbdlight_brightness;
 static DEFINE_MUTEX(kbdlight_mutex);
 
 static int kbdlight_set_level(int level)
@@ -5247,7 +5252,10 @@ static int __init kbdlight_init(struct ibm_init_struct *iibm)
 		return -ENODEV;
 	}
 
-	kbdlight_brightness = kbdlight_sysfs_get(NULL);
+	kbdlight_brightness = kbdlight_get_level();
+	if (kbdlight_brightness < 0)
+		kbdlight_brightness = 0;
+
 	tp_features.kbdlight = 1;
 
 	rc = led_classdev_register(&tpacpi_pdev->dev,
@@ -5343,31 +5351,19 @@ static int kbdlight_write(char *buf)
 	return 0;
 }
 
-static void kbdlight_suspend(void)
-{
-	struct led_classdev *led_cdev;
-
-	if (!tp_features.kbdlight)
-		return;
-
-	led_cdev = &tpacpi_led_kbdlight.led_classdev;
-	led_update_brightness(led_cdev);
-	led_classdev_suspend(led_cdev);
-}
-
 static void kbdlight_resume(void)
 {
 	if (!tp_features.kbdlight)
 		return;
 
-	led_classdev_resume(&tpacpi_led_kbdlight.led_classdev);
+	guard(mutex)(&kbdlight_mutex);
+	kbdlight_set_level(kbdlight_brightness);
 }
 
 static struct ibm_struct kbdlight_driver_data = {
 	.name = "kbdlight",
 	.read = kbdlight_read,
 	.write = kbdlight_write,
-	.suspend = kbdlight_suspend,
 	.resume = kbdlight_resume,
 	.exit = kbdlight_exit,
 };
-- 
2.56.1


^ permalink raw reply related	[flat|nested] 8+ messages in thread

* [PATCH v5 5/6] platform/x86: thinkpad_acpi: Detect auto-mode capability via MLCG
  2026-11-30 13:12 [PATCH v5 0/6] platform/x86: thinkpad_acpi: Add a keyboard backlight auto-mode trigger Qiling Zhu
                   ` (3 preceding siblings ...)
  2026-11-30 13:12 ` [PATCH v5 4/6] platform/x86: thinkpad_acpi: Restore keyboard backlight level ourselves Qiling Zhu
@ 2026-11-30 13:12 ` Qiling Zhu
  2026-11-30 13:12 ` [PATCH v5 6/6] platform/x86: thinkpad_acpi: Add a keyboard backlight auto-mode trigger Qiling Zhu
  2026-11-30 13:28 ` [PATCH v5 0/6] " sashiko-bot
  6 siblings, 0 replies; 8+ messages in thread
From: Qiling Zhu @ 2026-11-30 13:12 UTC (permalink / raw)
  To: Mark Pearson, Derek J. Clark, Rong Zhang, Hans de Goede,
	Ilpo Järvinen, Henrique de Moraes Holschuh
  Cc: platform-driver-x86, linux-kernel, ibm-acpi-devel, Lee Jones,
	Pavel Machek, Griffin Kroah-Hartman, linux-leds, vishnuocv,
	vsankar, Andrea Rabbaglietti

MLCG bit 20 indicates whether the EC supports the auto mode of the
keyboard backlight (0 = manual levels only, 1 = auto mode available), as
defined by the firmware.

Record it in tp_features.kbdlight_auto, for use when registering the
auto-mode trigger in a subsequent change.

Link: https://lore.kernel.org/r/CABxCQKuEP5wMSstO0MK1MhaqXTKrPhUSN3V2mrE_42j4cXsXZQ@mail.gmail.com
Signed-off-by: Qiling Zhu <kmatzkaczor244@gmail.com>
Reviewed-by: Rong Zhang <i@rong.moe>
Reviewed-by: Mark Pearson <mpearson-lenovo@squebb.ca>
---
 drivers/platform/x86/lenovo/thinkpad_acpi.c | 7 +++++++
 1 file changed, 7 insertions(+)

diff --git a/drivers/platform/x86/lenovo/thinkpad_acpi.c b/drivers/platform/x86/lenovo/thinkpad_acpi.c
index 764b1efa6..c81f36775 100644
--- a/drivers/platform/x86/lenovo/thinkpad_acpi.c
+++ b/drivers/platform/x86/lenovo/thinkpad_acpi.c
@@ -363,6 +363,7 @@ static struct {
 		TP_HOTKEY_TABLET_USES_GMMS,
 	} hotkey_tablet;
 	u32 kbdlight:1;
+	u32 kbdlight_auto:1;
 	u32 light:1;
 	u32 light_status:1;
 	u32 bright_acpimode:1;
@@ -5105,6 +5106,9 @@ static struct ibm_struct video_driver_data = {
 /* EC level that selects the auto mode for the keyboard backlight */
 #define TPACPI_KBD_LIGHT_AUTO_LEVEL 3
 
+/* MLCG bit that indicates whether the EC supports the auto mode */
+#define TPACPI_KBD_LIGHT_AUTO_CAPABLE_BIT 20
+
 /*
  * Cached EC level, including TPACPI_KBD_LIGHT_AUTO_LEVEL.  All state
  * tracking compares EC levels; the auto mode is translated to a
@@ -5189,6 +5193,9 @@ static bool kbdlight_is_supported(void)
 	}
 
 	vdbg_printk(TPACPI_DBG_INIT, "kbdlight MLCG returned 0x%x\n", status);
+
+	tp_features.kbdlight_auto = !!(status & BIT(TPACPI_KBD_LIGHT_AUTO_CAPABLE_BIT));
+
 	/*
 	 * Guessed test for keyboard backlight:
 	 *
-- 
2.56.1


^ permalink raw reply related	[flat|nested] 8+ messages in thread

* [PATCH v5 6/6] platform/x86: thinkpad_acpi: Add a keyboard backlight auto-mode trigger
  2026-11-30 13:12 [PATCH v5 0/6] platform/x86: thinkpad_acpi: Add a keyboard backlight auto-mode trigger Qiling Zhu
                   ` (4 preceding siblings ...)
  2026-11-30 13:12 ` [PATCH v5 5/6] platform/x86: thinkpad_acpi: Detect auto-mode capability via MLCG Qiling Zhu
@ 2026-11-30 13:12 ` Qiling Zhu
  2026-11-30 13:28 ` [PATCH v5 0/6] " sashiko-bot
  6 siblings, 0 replies; 8+ messages in thread
From: Qiling Zhu @ 2026-11-30 13:12 UTC (permalink / raw)
  To: Mark Pearson, Derek J. Clark, Rong Zhang, Hans de Goede,
	Ilpo Järvinen, Henrique de Moraes Holschuh
  Cc: platform-driver-x86, linux-kernel, ibm-acpi-devel, Lee Jones,
	Pavel Machek, Griffin Kroah-Hartman, linux-leds, vishnuocv,
	vsankar, Andrea Rabbaglietti

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@gmail.com>
Reviewed-by: Rong Zhang <i@rong.moe>
---
 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 c81f36775..f8d2f2eb6 100644
--- a/drivers/platform/x86/lenovo/thinkpad_acpi.c
+++ b/drivers/platform/x86/lenovo/thinkpad_acpi.c
@@ -5218,6 +5218,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)
 {
@@ -5245,6 +5266,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;
@@ -5265,6 +5315,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) {
@@ -5285,6 +5338,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);
 
@@ -5295,6 +5349,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));
 }
-- 
2.56.1


^ permalink raw reply related	[flat|nested] 8+ messages in thread

* Re: [PATCH v5 0/6] platform/x86: thinkpad_acpi: Add a keyboard backlight auto-mode trigger
  2026-11-30 13:12 [PATCH v5 0/6] platform/x86: thinkpad_acpi: Add a keyboard backlight auto-mode trigger Qiling Zhu
                   ` (5 preceding siblings ...)
  2026-11-30 13:12 ` [PATCH v5 6/6] platform/x86: thinkpad_acpi: Add a keyboard backlight auto-mode trigger Qiling Zhu
@ 2026-11-30 13:28 ` sashiko-bot
  6 siblings, 0 replies; 8+ messages in thread
From: sashiko-bot @ 2026-11-30 13:28 UTC (permalink / raw)
  To: Qiling Zhu; +Cc: linux-leds, lee

Sashiko has reviewed this 6-patch series and found no issues.

-- 
Sashiko AI review · https://sashiko.dev/#/patchset/20261130131252.80436-1-kmatzkaczor244@gmail.com

^ permalink raw reply	[flat|nested] 8+ messages in thread

end of thread, other threads:[~2026-11-30 13:28 UTC | newest]

Thread overview: 8+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2026-11-30 13:12 [PATCH v5 0/6] platform/x86: thinkpad_acpi: Add a keyboard backlight auto-mode trigger Qiling Zhu
2026-11-30 13:12 ` [PATCH v5 1/6] platform/x86: thinkpad_acpi: Report auto keyboard backlight mode as off Qiling Zhu
2026-11-30 13:12 ` [PATCH v5 2/6] platform/x86: thinkpad_acpi: Extract kbdlight notify helpers Qiling Zhu
2026-11-30 13:12 ` [PATCH v5 3/6] platform/x86: thinkpad_acpi: Don't poke keyboard backlight's LED core brightness Qiling Zhu
2026-11-30 13:12 ` [PATCH v5 4/6] platform/x86: thinkpad_acpi: Restore keyboard backlight level ourselves Qiling Zhu
2026-11-30 13:12 ` [PATCH v5 5/6] platform/x86: thinkpad_acpi: Detect auto-mode capability via MLCG Qiling Zhu
2026-11-30 13:12 ` [PATCH v5 6/6] platform/x86: thinkpad_acpi: Add a keyboard backlight auto-mode trigger Qiling Zhu
2026-11-30 13:28 ` [PATCH v5 0/6] " sashiko-bot

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox