Skip to content

Commit e82882c

Browse files
soyersoyerjwrdegoede
authored andcommitted
platform/x86: Add driver for Yoga Tablet Mode switch
This WMI driver for the tablet mode control switch for Lenovo Yoga notebooks was originally written by Gergo Koteles. The mode is mapped to a SW_TABLET_MODE switch capable input device. Andrew followed the suggestions that were posted in reply to Gergo's RFC patch, and on the v1 & v2 versions of this patch to follow-up and get it merged. Changes from Gergo's RFC: - Refactored obtaining a reference to the EC ACPI device needed for the quirk implementation as suggested by Hans de Goede - Applied small fixes and switched to always registering handles with the driver for automatic cleanup as suggested by Barnabás Pőcze. - Merged the lenovo_ymc_trigger_ec function with the ideapad_trigger_ymc_next_read function since it was no longer external. - Added the word "Tablet" to the driver description to hopefully make it more clear. - Fixed the LENOVO_YMC_QUERY_METHOD ID and the name string for the EC APCI device trigged for the quirk - Triggered the input event on probe so that the initial tablet mode state when the driver is loaded is reported to userspace as suggested by Armin Wolf. - Restricted the permissions of the ec_trigger parameter as suggested by Armin Wolf. Also updated the description. We have tested this on the Yoga 7 14AIL7 for the non-quirk path and on the Yoga 7 14ARB7 which has the firmware bug that requires triggering the embedded controller to send the mode change events. This workaround is also used by the Windows drivers. According to reports at https://github.com/lukas-w/yoga-usage-mode, which uses the same WMI devices, the following models should also work: Yoga C940, Ideapad flex 14API, Yoga 9 14IAP7, Yoga 7 14ARB7, etc. Signed-off-by: Gergo Koteles <[email protected]> Co-developed-by: Andrew Kallmeyer <[email protected]> Signed-off-by: Andrew Kallmeyer <[email protected]> Link: https://lore.kernel.org/r/[email protected]/ Link: https://lore.kernel.org/r/[email protected]/ Link: https://lore.kernel.org/r/[email protected]/ Tested-by: André Apitzsch <[email protected]> Link: https://lore.kernel.org/r/[email protected] Reviewed-by: Hans de Goede <[email protected]> Signed-off-by: Hans de Goede <[email protected]>
1 parent 0de0ab9 commit e82882c

File tree

4 files changed

+198
-0
lines changed

4 files changed

+198
-0
lines changed

drivers/platform/x86/Kconfig

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -461,6 +461,15 @@ config IDEAPAD_LAPTOP
461461
This is a driver for Lenovo IdeaPad netbooks contains drivers for
462462
rfkill switch, hotkey, fan control and backlight control.
463463

464+
config LENOVO_YMC
465+
tristate "Lenovo Yoga Tablet Mode Control"
466+
depends on ACPI_WMI
467+
depends on INPUT
468+
select INPUT_SPARSEKMAP
469+
help
470+
This driver maps the Tablet Mode Control switch to SW_TABLET_MODE input
471+
events for Lenovo Yoga notebooks.
472+
464473
config SENSORS_HDAPS
465474
tristate "Thinkpad Hard Drive Active Protection System (hdaps)"
466475
depends on INPUT

drivers/platform/x86/Makefile

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -62,6 +62,7 @@ obj-$(CONFIG_UV_SYSFS) += uv_sysfs.o
6262
# IBM Thinkpad and Lenovo
6363
obj-$(CONFIG_IBM_RTL) += ibm_rtl.o
6464
obj-$(CONFIG_IDEAPAD_LAPTOP) += ideapad-laptop.o
65+
obj-$(CONFIG_LENOVO_YMC) += lenovo-ymc.o
6566
obj-$(CONFIG_SENSORS_HDAPS) += hdaps.o
6667
obj-$(CONFIG_THINKPAD_ACPI) += thinkpad_acpi.o
6768
obj-$(CONFIG_THINKPAD_LMI) += think-lmi.o

drivers/platform/x86/ideapad-laptop.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,7 @@ enum {
3535
VPCCMD_W_FAN,
3636
VPCCMD_R_RF,
3737
VPCCMD_W_RF,
38+
VPCCMD_W_YMC = 0x2A,
3839
VPCCMD_R_FAN = 0x2B,
3940
VPCCMD_R_SPECIAL_BUTTONS = 0x31,
4041
VPCCMD_W_BL_POWER = 0x33,

drivers/platform/x86/lenovo-ymc.c

Lines changed: 187 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,187 @@
1+
// SPDX-License-Identifier: GPL-2.0-or-later
2+
/*
3+
* lenovo-ymc.c - Lenovo Yoga Mode Control driver
4+
*
5+
* Copyright © 2022 Gergo Koteles <[email protected]>
6+
*/
7+
8+
#define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
9+
10+
#include <linux/acpi.h>
11+
#include <linux/dmi.h>
12+
#include <linux/input.h>
13+
#include <linux/input/sparse-keymap.h>
14+
#include <linux/wmi.h>
15+
#include "ideapad-laptop.h"
16+
17+
#define LENOVO_YMC_EVENT_GUID "06129D99-6083-4164-81AD-F092F9D773A6"
18+
#define LENOVO_YMC_QUERY_GUID "09B0EE6E-C3FD-4243-8DA1-7911FF80BB8C"
19+
20+
#define LENOVO_YMC_QUERY_INSTANCE 0
21+
#define LENOVO_YMC_QUERY_METHOD 0x01
22+
23+
static bool ec_trigger __read_mostly;
24+
module_param(ec_trigger, bool, 0444);
25+
MODULE_PARM_DESC(ec_trigger, "Enable EC triggering work-around to force emitting tablet mode events");
26+
27+
static const struct dmi_system_id ec_trigger_quirk_dmi_table[] = {
28+
{
29+
/* Lenovo Yoga 7 14ARB7 */
30+
.matches = {
31+
DMI_MATCH(DMI_SYS_VENDOR, "LENOVO"),
32+
DMI_MATCH(DMI_PRODUCT_NAME, "82QF"),
33+
},
34+
},
35+
{ }
36+
};
37+
38+
struct lenovo_ymc_private {
39+
struct input_dev *input_dev;
40+
struct acpi_device *ec_acpi_dev;
41+
};
42+
43+
static void lenovo_ymc_trigger_ec(struct wmi_device *wdev, struct lenovo_ymc_private *priv)
44+
{
45+
int err;
46+
47+
if (!priv->ec_acpi_dev)
48+
return;
49+
50+
err = write_ec_cmd(priv->ec_acpi_dev->handle, VPCCMD_W_YMC, 1);
51+
if (err)
52+
dev_warn(&wdev->dev, "Could not write YMC: %d\n", err);
53+
}
54+
55+
static const struct key_entry lenovo_ymc_keymap[] = {
56+
/* Laptop */
57+
{ KE_SW, 0x01, { .sw = { SW_TABLET_MODE, 0 } } },
58+
/* Tablet */
59+
{ KE_SW, 0x02, { .sw = { SW_TABLET_MODE, 1 } } },
60+
/* Drawing Board */
61+
{ KE_SW, 0x03, { .sw = { SW_TABLET_MODE, 1 } } },
62+
/* Tent */
63+
{ KE_SW, 0x04, { .sw = { SW_TABLET_MODE, 1 } } },
64+
{ KE_END },
65+
};
66+
67+
static void lenovo_ymc_notify(struct wmi_device *wdev, union acpi_object *data)
68+
{
69+
struct lenovo_ymc_private *priv = dev_get_drvdata(&wdev->dev);
70+
u32 input_val = 0;
71+
struct acpi_buffer input = { sizeof(input_val), &input_val };
72+
struct acpi_buffer output = { ACPI_ALLOCATE_BUFFER, NULL };
73+
union acpi_object *obj;
74+
acpi_status status;
75+
int code;
76+
77+
status = wmi_evaluate_method(LENOVO_YMC_QUERY_GUID,
78+
LENOVO_YMC_QUERY_INSTANCE,
79+
LENOVO_YMC_QUERY_METHOD,
80+
&input, &output);
81+
82+
if (ACPI_FAILURE(status)) {
83+
dev_warn(&wdev->dev,
84+
"Failed to evaluate query method: %s\n",
85+
acpi_format_exception(status));
86+
return;
87+
}
88+
89+
obj = output.pointer;
90+
91+
if (obj->type != ACPI_TYPE_INTEGER) {
92+
dev_warn(&wdev->dev,
93+
"WMI event data is not an integer\n");
94+
goto free_obj;
95+
}
96+
code = obj->integer.value;
97+
98+
if (!sparse_keymap_report_event(priv->input_dev, code, 1, true))
99+
dev_warn(&wdev->dev, "Unknown key %d pressed\n", code);
100+
101+
free_obj:
102+
kfree(obj);
103+
lenovo_ymc_trigger_ec(wdev, priv);
104+
}
105+
106+
static void acpi_dev_put_helper(void *p) { acpi_dev_put(p); }
107+
108+
static int lenovo_ymc_probe(struct wmi_device *wdev, const void *ctx)
109+
{
110+
struct lenovo_ymc_private *priv;
111+
struct input_dev *input_dev;
112+
int err;
113+
114+
ec_trigger |= dmi_check_system(ec_trigger_quirk_dmi_table);
115+
116+
priv = devm_kzalloc(&wdev->dev, sizeof(*priv), GFP_KERNEL);
117+
if (!priv)
118+
return -ENOMEM;
119+
120+
if (ec_trigger) {
121+
pr_debug("Lenovo YMC enable EC triggering.\n");
122+
priv->ec_acpi_dev = acpi_dev_get_first_match_dev("VPC2004", NULL, -1);
123+
124+
if (!priv->ec_acpi_dev) {
125+
dev_err(&wdev->dev, "Could not find EC ACPI device.\n");
126+
return -ENODEV;
127+
}
128+
err = devm_add_action_or_reset(&wdev->dev,
129+
acpi_dev_put_helper, priv->ec_acpi_dev);
130+
if (err) {
131+
dev_err(&wdev->dev,
132+
"Could not clean up EC ACPI device: %d\n", err);
133+
return err;
134+
}
135+
}
136+
137+
input_dev = devm_input_allocate_device(&wdev->dev);
138+
if (!input_dev)
139+
return -ENOMEM;
140+
141+
input_dev->name = "Lenovo Yoga Tablet Mode Control switch";
142+
input_dev->phys = LENOVO_YMC_EVENT_GUID "/input0";
143+
input_dev->id.bustype = BUS_HOST;
144+
input_dev->dev.parent = &wdev->dev;
145+
err = sparse_keymap_setup(input_dev, lenovo_ymc_keymap, NULL);
146+
if (err) {
147+
dev_err(&wdev->dev,
148+
"Could not set up input device keymap: %d\n", err);
149+
return err;
150+
}
151+
152+
err = input_register_device(input_dev);
153+
if (err) {
154+
dev_err(&wdev->dev,
155+
"Could not register input device: %d\n", err);
156+
return err;
157+
}
158+
159+
priv->input_dev = input_dev;
160+
dev_set_drvdata(&wdev->dev, priv);
161+
162+
/* Report the state for the first time on probe */
163+
lenovo_ymc_trigger_ec(wdev, priv);
164+
lenovo_ymc_notify(wdev, NULL);
165+
return 0;
166+
}
167+
168+
static const struct wmi_device_id lenovo_ymc_wmi_id_table[] = {
169+
{ .guid_string = LENOVO_YMC_EVENT_GUID },
170+
{ }
171+
};
172+
MODULE_DEVICE_TABLE(wmi, lenovo_ymc_wmi_id_table);
173+
174+
static struct wmi_driver lenovo_ymc_driver = {
175+
.driver = {
176+
.name = "lenovo-ymc",
177+
},
178+
.id_table = lenovo_ymc_wmi_id_table,
179+
.probe = lenovo_ymc_probe,
180+
.notify = lenovo_ymc_notify,
181+
};
182+
183+
module_wmi_driver(lenovo_ymc_driver);
184+
185+
MODULE_AUTHOR("Gergo Koteles <[email protected]>");
186+
MODULE_DESCRIPTION("Lenovo Yoga Mode Control driver");
187+
MODULE_LICENSE("GPL");

0 commit comments

Comments
 (0)