[PATCH v3 6/8] platform/x86: ideapad-laptop: Trigger the EC on YMC events following Windows behavior

From: Rong Zhang

Date: Wed Nov 25 2026 - 13:10:51 EST


Currently, the ideapad-laptop driver triggers the EC with YMC command
0x01, which is necessary for existing devices in the DMI quirk table to
clear the pending event to allow further events to be emitted.

Newer models do not require command 0x01 anymore (writing it is a
no-op), but some of them require other commands to properly synchronize
and set the hardware state.

It turned out that Lenovo's YMC driver for Windows (YMC.exe) writes YMC
command 0x08/0x09 on fold/unfold YMC events, in which "unfold" means
laptop mode and "fold" means other modes.

Adopt the said sequence and combine it with command 0x01 to make
ymc_ec_trigger work on all relevant devices. For the sake of caution,
write command 0x08 only for the known fold postures. For all other
postures (including unknown ones), write 0x09 to make sure the touchpad
and keyboard are still functional. Finally, 0x01 is always written to
ensure the pending event is cleared.

Reported-by: Mahir Faisal <mahirfaisalantiq33@xxxxxxxxx>
Closes: https://msgid.link/CAGAHqH6o0TUa1HXMrNV6e+HNYwU3xxHkOM665756n8Cp3AzhVQ@xxxxxxxxxxxxxx
Reviewed-by: Mark Pearson <mpearson-lenovo@xxxxxxxxx>
Signed-off-by: Rong Zhang <i@xxxxxxxx>
---
Changes in v3:
- Always write YMC command 0x01 at last (thanks Gergo Koteles)
- Drop Eduard Aliman's Tested-by: due to the change above

Changes in v2:
- Fix typos (thanks Mark Pearson)
---
drivers/platform/x86/lenovo/ideapad-laptop.c | 60 ++++++++++++++++++++++++----
1 file changed, 53 insertions(+), 7 deletions(-)

diff --git a/drivers/platform/x86/lenovo/ideapad-laptop.c b/drivers/platform/x86/lenovo/ideapad-laptop.c
index 91a8d11c79c5..f3467cd8df36 100644
--- a/drivers/platform/x86/lenovo/ideapad-laptop.c
+++ b/drivers/platform/x86/lenovo/ideapad-laptop.c
@@ -133,6 +133,12 @@ enum {
VPCCMD_W_BL_POWER = 0x33,
};

+enum ymc_cmd {
+ YMC_CMD_CLEAR = 0x01,
+ YMC_CMD_FOLD = 0x08,
+ YMC_CMD_UNFOLD = 0x09,
+};
+
/*
* These correspond to the number of supported states - 1
* Future keyboard types may need a new system, if there's a collision
@@ -1870,10 +1876,11 @@ static const struct dmi_system_id ymc_ec_trigger_quirk_dmi_table[] = {
{ }
};

-static void ideapad_laptop_trigger_ec(void)
+static void ideapad_laptop_trigger_ec(u32 code)
{
struct ideapad_private *priv;
- int ret;
+ enum ymc_cmd cmd;
+ int ret[2];

guard(mutex)(&ideapad_shared_mutex);

@@ -1884,18 +1891,57 @@ static void ideapad_laptop_trigger_ec(void)
if (!priv->features.ymc_ec_trigger)
return;

- scoped_guard(mutex, &priv->vpc_mutex)
- ret = write_ec_cmd(priv->adev->handle, VPCCMD_W_YMC, 1);
- if (ret)
- dev_warn(&priv->platform_device->dev, "Could not write YMC: %d\n", ret);
+ switch (code) {
+ case YMC_POSTURE_TABLET:
+ case YMC_POSTURE_DRAWING_BOARD:
+ case YMC_POSTURE_TENT:
+ /*
+ * Sync keyboard backlight and disable the touchapd and keyboard
+ * only when the device is folded...
+ */
+ cmd = YMC_CMD_FOLD;
+ break;
+ default:
+ /*
+ * ...IOW, whenever there is a chance that the device may be
+ * unfolded, e.g., in laptop mode or any other unknown modes,
+ * sync keyboard backlight and enable the touchapd and keyboard,
+ * so that users don't lose input.
+ */
+ cmd = YMC_CMD_UNFOLD;
+ break;
+ }
+
+ /*
+ * Old and new models behave differently:
+ *
+ * - Old: YMC_CMD_CLEAR clears the pending event to allow further events
+ * to be emitted. Any other YMC commands are no-op.
+ * - New: YMC_CMD_CLEAR is not required anymore and becomes a no-op.
+ * However, YMC_CMD_FOLD/YMC_CMD_UNFOLD must be sent on event
+ * (see above).
+ *
+ * Take the union of the two by writing both commands on both categories
+ * of devices.
+ */
+ scoped_guard(mutex, &priv->vpc_mutex) {
+ ret[0] = write_ec_cmd(priv->adev->handle, VPCCMD_W_YMC, cmd);
+ ret[1] = write_ec_cmd(priv->adev->handle, VPCCMD_W_YMC, YMC_CMD_CLEAR);
+ }
+ if (ret[0] || ret[1]) {
+ dev_warn(&priv->platform_device->dev,
+ "Could not write YMC: %d/%d\n", ret[0], ret[1]);
+ }
}

static int ideapad_laptop_nb_notify(struct notifier_block *nb,
unsigned long action, void *data)
{
+ const u32 *p = data;
+
switch (action) {
case IDEAPAD_LAPTOP_YMC_EVENT:
- ideapad_laptop_trigger_ec();
+ ideapad_laptop_trigger_ec(*p);
break;
}


--
2.55.0