mirror of https://lore.kernel.org/lkml/
 help / color / mirror / Atom feed
* [PATCH v4 0/6] platform/x86: thinkpad_acpi: Add a keyboard backlight auto-mode trigger
@ 2026-11-25  8:46 Qiling Zhu
  2026-11-25  8:46 ` [PATCH v4 1/6] platform/x86: thinkpad_acpi: Report auto keyboard backlight mode as off Qiling Zhu
                   ` (5 more replies)
  0 siblings, 6 replies; 11+ messages in thread
From: Qiling Zhu @ 2026-11-25  8:46 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 v4 of the thinkpad_acpi keyboard backlight auto mode series.
Many thanks to Rong, Mark and Vishnu for the reviews and the firmware
side answers so far, and to Andrea for the report behind the first
patch.  I have tried to follow the v3 comments one by one; where I was
not sure how to read a comment, I say so below and would be glad to be
corrected.

As Rong asked, this version starts a new thread.  For reference, v3
is here:

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

and my reply with the sleep test data is here:

  https://lore.kernel.org/r/av14Sp6_nKwU5F-i@ThinkPad/

The series went from seven patches to six: the two suspend/resume
patches of v3 are replaced by one, which now sits before the trigger
patch.

  1/6  Report auto keyboard backlight mode as off          (stable)
  2/6  Extract kbdlight notify helpers                  (refactor)
  3/6  Don't poke keyboard backlight's LED core brightness (writes)
  4/6  Restore keyboard backlight level ourselves       (sleep/wake)
  5/6  Detect auto-mode capability via MLCG             (groundwork)
  6/6  Add a keyboard backlight auto-mode trigger         (feature)

Changes since v3, by comment:

- 1/6 does the mapping in a small helper with an int return type, as
  Rong suggested while reviewing the trigger patch; 4/6 reuses the
  helper for brightness notifications.  With 1/6 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, where the
  unmapped value used to be written back; 4/6 restores the level the EC
  had.  I kept 1/6 small so that it can go to stable on its own, and
  would like to hear if this trade-off looks wrong for stable.

- 2/6 takes the title Rong suggested, states the locking contract with
  lockdep_assert_held() instead of a comment, and drops the braces.
  The helpers are placed where the later patches need them, in the
  hope that no later patch has to add a forward declaration.

- 3/6 takes Rong's title.  Of the two software write paths, only the
  proc path reports changes through brightness_hw_changed; the sysfs
  path just writes the level and updates the cached one.  Fn+Space
  changes continue to be reported.  The text after the --- line is my
  attempt to explain why leaving the LED core's cached brightness alone
  seems acceptable to me; if the reasoning there falls short, please
  tell me.

- 4/6 replaces v3 5/7 and 7/7.  With the driver unloaded on my
  machine, the backlight went out when entering s2idle and
  hibernation; on wake from s2idle the EC was back at its previous
  level, and on wake from hibernation it was at 0 for each of the four
  starting levels.  So kbdlight_suspend() is gone and kbdlight_resume()
  writes the cached EC level back on resume, for every model rather
  than only auto mode ones.  The cached level becomes the raw EC level
  in this patch, so that resume treats the auto mode like any other
  level.  I did not add a check on LED_SUSPENDED: this LED does not set
  LED_CORE_SUSPENDRESUME, and with the suspend callback gone I do not
  see what would set the flag for it; my understanding of Rong's reply
  was that such a gate might not be needed.  The patch comes before the
  trigger patch so that no intermediate commit has the trigger together
  with the old LED core suspend/resume path.

- 5/6 uses a symbolic name for bit 20, as Rong asked.

- 6/6 routes trigger activation through the helper that also updates
  the cached level, which I believe addresses the stale cache Sashiko
  reported.  It also skips the trigger notification when the trigger
  could not be registered, pulls the registration into a small helper,
  and still makes the trigger the default one at init when the EC is
  already in auto mode, now with a comment on the case this is meant
  for, as Rong suggested on Nov 19.  The title was changed to say more
  precisely what the patch adds.

On the Reviewed-by tags: the changes to 1/6 and 5/6 are small, but the
diffs are no longer the ones Mark reviewed, so I dropped his tags there
and noted it under the --- line.  Mark, when you have the time, could
you take another look at these two?

I could only test on a ThinkPad X1 Carbon Gen 14 (linux-next
next-20261030): the software paths (selecting and dropping the trigger,
brightness returning -ENODATA in auto mode), the trigger following
Fn+Space in and out of auto mode, and suspend/resume from manual
levels and from auto mode, with s2idle and with S4.  No S3-capable
machine and no other model was available to me, so testing elsewhere
would be very welcome.  Vishnu, Mark, thank you for offering.

One observation from testing with a desktop session, since it can look
like a driver bug at first: the level restored on wake was sometimes
overwritten again a few seconds later.  KDE's power management writes
back the keyboard brightness it last knew about after resume, and a
level written directly through sysfs does not appear to reach it;
changes made with Fn+Space or through the desktop itself did not seem
affected.  I do not think there is anything for this series to do
about it, but I wanted to mention it.

One naming note: kbdlight_brightness now holds the raw EC level,
including the auto mode, rather than a brightness; a comment above the
variable says so, and the name is unchanged.

The only 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] 11+ messages in thread

* [PATCH v4 1/6] platform/x86: thinkpad_acpi: Report auto keyboard backlight mode as off
  2026-11-25  8:46 [PATCH v4 0/6] platform/x86: thinkpad_acpi: Add a keyboard backlight auto-mode trigger Qiling Zhu
@ 2026-11-25  8:46 ` Qiling Zhu
  2026-11-25  8:46 ` [PATCH v4 2/6] platform/x86: thinkpad_acpi: Extract kbdlight notify helpers Qiling Zhu
                   ` (4 subsequent siblings)
  5 siblings, 0 replies; 11+ messages in thread
From: Qiling Zhu @ 2026-11-25  8:46 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>

---
Mark Pearson's Reviewed-by was given on the v3 version of this patch,
which did the mapping inline in kbdlight_sysfs_get().  The helper was
added at Rong's request, so the tag is not carried over.

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	[flat|nested] 11+ messages in thread

* [PATCH v4 2/6] platform/x86: thinkpad_acpi: Extract kbdlight notify helpers
  2026-11-25  8:46 [PATCH v4 0/6] platform/x86: thinkpad_acpi: Add a keyboard backlight auto-mode trigger Qiling Zhu
  2026-11-25  8:46 ` [PATCH v4 1/6] platform/x86: thinkpad_acpi: Report auto keyboard backlight mode as off Qiling Zhu
@ 2026-11-25  8:46 ` Qiling Zhu
  2026-11-25  8:46 ` [PATCH v4 3/6] platform/x86: thinkpad_acpi: Don't poke keyboard backlight's LED core brightness Qiling Zhu
                   ` (3 subsequent siblings)
  5 siblings, 0 replies; 11+ messages in thread
From: Qiling Zhu @ 2026-11-25  8:46 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 a brightness, compares it with the cached value, updates the cache
and notifies the LED core; kbdlight_notify_change() reads the brightness
back and calls the former under kbdlight_mutex, like the original inline
code did.

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

No functional change intended.

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

diff --git a/drivers/platform/x86/lenovo/thinkpad_acpi.c b/drivers/platform/x86/lenovo/thinkpad_acpi.c
index 31d3721bb..b4b3d7dcc 100644
--- a/drivers/platform/x86/lenovo/thinkpad_acpi.c
+++ b/drivers/platform/x86/lenovo/thinkpad_acpi.c
@@ -5267,6 +5267,30 @@ 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, level);
+}
+
+static void kbdlight_notify_change(void)
+{
+	guard(mutex)(&kbdlight_mutex);
+
+	kbdlight_notify_level(kbdlight_sysfs_get(NULL));
+}
+
 static int kbdlight_read(struct seq_file *m)
 {
 	int level;
@@ -11593,22 +11617,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	[flat|nested] 11+ messages in thread

* [PATCH v4 3/6] platform/x86: thinkpad_acpi: Don't poke keyboard backlight's LED core brightness
  2026-11-25  8:46 [PATCH v4 0/6] platform/x86: thinkpad_acpi: Add a keyboard backlight auto-mode trigger Qiling Zhu
  2026-11-25  8:46 ` [PATCH v4 1/6] platform/x86: thinkpad_acpi: Report auto keyboard backlight mode as off Qiling Zhu
  2026-11-25  8:46 ` [PATCH v4 2/6] platform/x86: thinkpad_acpi: Extract kbdlight notify helpers Qiling Zhu
@ 2026-11-25  8:46 ` Qiling Zhu
  2026-11-25 13:11   ` Rong Zhang
  2026-11-25  8:46 ` [PATCH v4 4/6] platform/x86: thinkpad_acpi: Restore keyboard backlight level ourselves Qiling Zhu
                   ` (2 subsequent siblings)
  5 siblings, 1 reply; 11+ messages in thread
From: Qiling Zhu @ 2026-11-25  8:46 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>

---
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 b4b3d7dcc..16bf2267d 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;
@@ -5326,7 +5326,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	[flat|nested] 11+ messages in thread

* [PATCH v4 4/6] platform/x86: thinkpad_acpi: Restore keyboard backlight level ourselves
  2026-11-25  8:46 [PATCH v4 0/6] platform/x86: thinkpad_acpi: Add a keyboard backlight auto-mode trigger Qiling Zhu
                   ` (2 preceding siblings ...)
  2026-11-25  8:46 ` [PATCH v4 3/6] platform/x86: thinkpad_acpi: Don't poke keyboard backlight's LED core brightness Qiling Zhu
@ 2026-11-25  8:46 ` Qiling Zhu
  2026-11-25 13:16   ` Rong Zhang
  2026-11-25  8:46 ` [PATCH v4 5/6] platform/x86: thinkpad_acpi: Detect auto-mode capability via MLCG Qiling Zhu
  2026-11-25  8:46 ` [PATCH v4 6/6] platform/x86: thinkpad_acpi: Add a keyboard backlight auto-mode trigger Qiling Zhu
  5 siblings, 1 reply; 11+ messages in thread
From: Qiling Zhu @ 2026-11-25  8:46 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; translate it only when reporting a
brightness.

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

Signed-off-by: Qiling Zhu <kmatzkaczor244@gmail.com>
---
 drivers/platform/x86/lenovo/thinkpad_acpi.c | 38 +++++++++++----------
 1 file changed, 20 insertions(+), 18 deletions(-)

diff --git a/drivers/platform/x86/lenovo/thinkpad_acpi.c b/drivers/platform/x86/lenovo/thinkpad_acpi.c
index 16bf2267d..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,
@@ -5281,14 +5289,20 @@ static void kbdlight_notify_level(int level)
 		return;
 
 	kbdlight_brightness = level;
-	led_classdev_notify_brightness_hw_changed(led_cdev, 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);
 
-	kbdlight_notify_level(kbdlight_sysfs_get(NULL));
+	level = kbdlight_get_level();
+	if (level < 0)
+		return;
+
+	kbdlight_notify_level(level);
 }
 
 static int kbdlight_read(struct seq_file *m)
@@ -5337,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	[flat|nested] 11+ messages in thread

* [PATCH v4 5/6] platform/x86: thinkpad_acpi: Detect auto-mode capability via MLCG
  2026-11-25  8:46 [PATCH v4 0/6] platform/x86: thinkpad_acpi: Add a keyboard backlight auto-mode trigger Qiling Zhu
                   ` (3 preceding siblings ...)
  2026-11-25  8:46 ` [PATCH v4 4/6] platform/x86: thinkpad_acpi: Restore keyboard backlight level ourselves Qiling Zhu
@ 2026-11-25  8:46 ` Qiling Zhu
  2026-11-25 13:17   ` Rong Zhang
  2026-11-25  8:46 ` [PATCH v4 6/6] platform/x86: thinkpad_acpi: Add a keyboard backlight auto-mode trigger Qiling Zhu
  5 siblings, 1 reply; 11+ messages in thread
From: Qiling Zhu @ 2026-11-25  8:46 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>

---
Mark Pearson's Reviewed-by was given on the v3 version of this patch,
which tested BIT(20) directly.  The symbolic name was added at Rong's
request, so the tag is not carried over.
---
 drivers/platform/x86/lenovo/thinkpad_acpi.c | 11 +++++++++++
 1 file changed, 11 insertions(+)

diff --git a/drivers/platform/x86/lenovo/thinkpad_acpi.c b/drivers/platform/x86/lenovo/thinkpad_acpi.c
index 764b1efa6..ee8f7429d 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,13 @@ static bool kbdlight_is_supported(void)
 	}
 
 	vdbg_printk(TPACPI_DBG_INIT, "kbdlight MLCG returned 0x%x\n", status);
+
+	/*
+	 * MLCG bit 20 indicates auto mode support: 0 = manual levels only,
+	 * 1 = auto mode available (per the firmware definition).
+	 */
+	tp_features.kbdlight_auto = !!(status & BIT(TPACPI_KBD_LIGHT_AUTO_CAPABLE_BIT));
+
 	/*
 	 * Guessed test for keyboard backlight:
 	 *
-- 
2.56.1


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

* [PATCH v4 6/6] platform/x86: thinkpad_acpi: Add a keyboard backlight auto-mode trigger
  2026-11-25  8:46 [PATCH v4 0/6] platform/x86: thinkpad_acpi: Add a keyboard backlight auto-mode trigger Qiling Zhu
                   ` (4 preceding siblings ...)
  2026-11-25  8:46 ` [PATCH v4 5/6] platform/x86: thinkpad_acpi: Detect auto-mode capability via MLCG Qiling Zhu
@ 2026-11-25  8:46 ` Qiling Zhu
  2026-11-25 13:17   ` Rong Zhang
  5 siblings, 1 reply; 11+ messages in thread
From: Qiling Zhu @ 2026-11-25  8:46 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>
---
 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));
 }
-- 
2.56.1


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

* Re: [PATCH v4 3/6] platform/x86: thinkpad_acpi: Don't poke keyboard backlight's LED core brightness
  2026-11-25  8:46 ` [PATCH v4 3/6] platform/x86: thinkpad_acpi: Don't poke keyboard backlight's LED core brightness Qiling Zhu
@ 2026-11-25 13:11   ` Rong Zhang
  0 siblings, 0 replies; 11+ messages in thread
From: Rong Zhang @ 2026-11-25 13:11 UTC (permalink / raw)
  To: Qiling Zhu, Mark Pearson, Derek J. Clark, 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 Qiling,

On Wed, 2026-11-25 at 16:46 +0800, Qiling Zhu wrote:
> 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>

Thanks,
Rong

> 
> ---
> 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 b4b3d7dcc..16bf2267d 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;
> @@ -5326,7 +5326,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)

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

* Re: [PATCH v4 4/6] platform/x86: thinkpad_acpi: Restore keyboard backlight level ourselves
  2026-11-25  8:46 ` [PATCH v4 4/6] platform/x86: thinkpad_acpi: Restore keyboard backlight level ourselves Qiling Zhu
@ 2026-11-25 13:16   ` Rong Zhang
  0 siblings, 0 replies; 11+ messages in thread
From: Rong Zhang @ 2026-11-25 13:16 UTC (permalink / raw)
  To: Qiling Zhu, Mark Pearson, Derek J. Clark, 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 Qiling,

On Wed, 2026-11-25 at 16:46 +0800, Qiling Zhu wrote:
> 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; translate it only when reporting a
> brightness.
> 
> Only s2idle and S4 were available on the test machine; S3-capable
> models were not tested.
> 
> Signed-off-by: Qiling Zhu <kmatzkaczor244@gmail.com>
> ---
>  drivers/platform/x86/lenovo/thinkpad_acpi.c | 38 +++++++++++----------
>  1 file changed, 20 insertions(+), 18 deletions(-)
> 
> diff --git a/drivers/platform/x86/lenovo/thinkpad_acpi.c b/drivers/platform/x86/lenovo/thinkpad_acpi.c
> index 16bf2267d..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,
> @@ -5281,14 +5289,20 @@ static void kbdlight_notify_level(int level)
>  		return;
>  
>  	kbdlight_brightness = level;
> -	led_classdev_notify_brightness_hw_changed(led_cdev, 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);
>  
> -	kbdlight_notify_level(kbdlight_sysfs_get(NULL));
> +	level = kbdlight_get_level();
> +	if (level < 0)
> +		return;
> +
> +	kbdlight_notify_level(level);

I believe these changes doesn't affect the behavior of
TP_HKEY_EV_KBD_LIGHT. Usually you can move the changes to the factor-out
patch (i.e., PATCH 2) to reduce back-and-forth changes, with a brief
explanation added to the commit message there.

With them moved:

Reviewed-by: Rong Zhang <i@rong.moe>

Note that you don't need to drop my R-b from PATCH 2.

Thanks,
Rong

>  }
>  
>  static int kbdlight_read(struct seq_file *m)
> @@ -5337,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,
>  };

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

* Re: [PATCH v4 5/6] platform/x86: thinkpad_acpi: Detect auto-mode capability via MLCG
  2026-11-25  8:46 ` [PATCH v4 5/6] platform/x86: thinkpad_acpi: Detect auto-mode capability via MLCG Qiling Zhu
@ 2026-11-25 13:17   ` Rong Zhang
  0 siblings, 0 replies; 11+ messages in thread
From: Rong Zhang @ 2026-11-25 13:17 UTC (permalink / raw)
  To: Qiling Zhu, Mark Pearson, Derek J. Clark, 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 Qiling,

On Wed, 2026-11-25 at 16:46 +0800, Qiling Zhu wrote:
> 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>
> 
> ---
> Mark Pearson's Reviewed-by was given on the v3 version of this patch,
> which tested BIT(20) directly.  The symbolic name was added at Rong's
> request, so the tag is not carried over.

Usually you don't need to drop a tag when your implementation doesn't
change a lot. Adding a symbolic macro is just a trivial change to improve
readability and maintainability.

> ---
>  drivers/platform/x86/lenovo/thinkpad_acpi.c | 11 +++++++++++
>  1 file changed, 11 insertions(+)
> 
> diff --git a/drivers/platform/x86/lenovo/thinkpad_acpi.c b/drivers/platform/x86/lenovo/thinkpad_acpi.c
> index 764b1efa6..ee8f7429d 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,13 @@ static bool kbdlight_is_supported(void)
>  	}
>  
>  	vdbg_printk(TPACPI_DBG_INIT, "kbdlight MLCG returned 0x%x\n", status);
> +
> +	/*
> +	 * MLCG bit 20 indicates auto mode support: 0 = manual levels only,
> +	 * 1 = auto mode available (per the firmware definition).
> +	 */

Usually you don't need to repeat what has been expressed by the code and
symbolic name themselves, so this comment can be dropped in favor of the
comment of TPACPI_KBD_LIGHT_AUTO_CAPABLE_BIT.

Thanks,
Rong

> +	tp_features.kbdlight_auto = !!(status & BIT(TPACPI_KBD_LIGHT_AUTO_CAPABLE_BIT));
> +
>  	/*
>  	 * Guessed test for keyboard backlight:
>  	 *

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

* Re: [PATCH v4 6/6] platform/x86: thinkpad_acpi: Add a keyboard backlight auto-mode trigger
  2026-11-25  8:46 ` [PATCH v4 6/6] platform/x86: thinkpad_acpi: Add a keyboard backlight auto-mode trigger Qiling Zhu
@ 2026-11-25 13:17   ` Rong Zhang
  0 siblings, 0 replies; 11+ messages in thread
From: Rong Zhang @ 2026-11-25 13:17 UTC (permalink / raw)
  To: Qiling Zhu, Mark Pearson, Derek J. Clark, 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 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@gmail.com>

Reviewed-by: Rong Zhang <i@rong.moe>

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));
>  }

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

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

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

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

all inboxes | Powered by JetHome®