Tablet mode switching events are incorrect on Lenovo Yoga 7 2-in-1 laptop

I’m using a Lenovo Yoga 7 14ARP8. Currently on NixOS, but this issue seems to happen on other distros including Fedora.

The issue

This laptop has a 360 degree hinge that allows it to become a tablet of sorts. When flipped above 180 degrees on Windows the keyboard and touchpad are disabled (And the backlight turns off).

While completly flipping the keyboard around 360 degrees has a similar effect (without turning off backlight) and triggers the “Tablet mode” on KDE Plasma, the keyboard and touchpad continue to be enabled anywhere between 180 and 360 degrees, meaning if I set the keyboard to be facing the ground as a stand, random keys may be pressed.

Debugging the events

The first thing I tried was running a sudo libinput debug-events and flipping the keyboard while it’s running. The result is this:

As I flip it to 360:
-event12 SWITCH_TOGGLE +4294967.295s switch tablet-mode state 1

As I flip it away from 360:
event12 SWITCH_TOGGLE +2.300s switch tablet-mode state 0

Going beyond 180 does not affect the “tablet-mode” state.

The responsible modules

It seems this switch is controlled by the lenovo_ymc module, but there’s also a ymc_ec_trigger option in the ideapad_laptop module, however enabling that didn’t fix the issue. ideapad_laptop seems to have fixes for the previous gen of this laptop (Yoga 7 14ARP7) but not this one.

Within the lenovo_ymc module there is this code:

static const struct key_entry lenovo_ymc_keymap[] = {
    /* Laptop */
    { KE_SW, 0x01, { .sw = { SW_TABLET_MODE, 0 } } },
    /* Tablet */
    { KE_SW, 0x02, { .sw = { SW_TABLET_MODE, 1 } } },
    /* Drawing Board */
    { KE_SW, 0x03, { .sw = { SW_TABLET_MODE, 1 } } },
    /* Tent */
    { KE_SW, 0x04, { .sw = { SW_TABLET_MODE, 1 } } },
    { KE_END },
};

I’m not sure how it determines when the laptop is in “Tablet” mode, but should be sending the event even in “Tent” mode or “Drawing Board” mode. It seems though those aren’t getting triggered or are skipped.

I am thinking of reporting this as a bug to the linux kernel, but I wanted to post it here in case anyone can help.