1 // SPDX-License-Identifier: GPL-2.0-or-later
3 * lenovo-ymc.c - Lenovo Yoga Mode Control driver
5 * Copyright © 2022 Gergo Koteles <soyer@irl.hu>
8 #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
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"
17 #define LENOVO_YMC_EVENT_GUID "06129D99-6083-4164-81AD-F092F9D773A6"
18 #define LENOVO_YMC_QUERY_GUID "09B0EE6E-C3FD-4243-8DA1-7911FF80BB8C"
20 #define LENOVO_YMC_QUERY_INSTANCE 0
21 #define LENOVO_YMC_QUERY_METHOD 0x01
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");
27 static const struct dmi_system_id ec_trigger_quirk_dmi_table[] = {
29 /* Lenovo Yoga 7 14ARB7 */
31 DMI_MATCH(DMI_SYS_VENDOR, "LENOVO"),
32 DMI_MATCH(DMI_PRODUCT_NAME, "82QF"),
38 struct lenovo_ymc_private {
39 struct input_dev *input_dev;
40 struct acpi_device *ec_acpi_dev;
43 static void lenovo_ymc_trigger_ec(struct wmi_device *wdev, struct lenovo_ymc_private *priv)
47 if (!priv->ec_acpi_dev)
50 err = write_ec_cmd(priv->ec_acpi_dev->handle, VPCCMD_W_YMC, 1);
52 dev_warn(&wdev->dev, "Could not write YMC: %d\n", err);
55 static const struct key_entry lenovo_ymc_keymap[] = {
57 { KE_SW, 0x01, { .sw = { SW_TABLET_MODE, 0 } } },
59 { KE_SW, 0x02, { .sw = { SW_TABLET_MODE, 1 } } },
61 { KE_SW, 0x03, { .sw = { SW_TABLET_MODE, 1 } } },
63 { KE_SW, 0x04, { .sw = { SW_TABLET_MODE, 1 } } },
67 static void lenovo_ymc_notify(struct wmi_device *wdev, union acpi_object *data)
69 struct lenovo_ymc_private *priv = dev_get_drvdata(&wdev->dev);
71 struct acpi_buffer input = { sizeof(input_val), &input_val };
72 struct acpi_buffer output = { ACPI_ALLOCATE_BUFFER, NULL };
73 union acpi_object *obj;
77 status = wmi_evaluate_method(LENOVO_YMC_QUERY_GUID,
78 LENOVO_YMC_QUERY_INSTANCE,
79 LENOVO_YMC_QUERY_METHOD,
82 if (ACPI_FAILURE(status)) {
84 "Failed to evaluate query method: %s\n",
85 acpi_format_exception(status));
91 if (obj->type != ACPI_TYPE_INTEGER) {
93 "WMI event data is not an integer\n");
96 code = obj->integer.value;
98 if (!sparse_keymap_report_event(priv->input_dev, code, 1, true))
99 dev_warn(&wdev->dev, "Unknown key %d pressed\n", code);
103 lenovo_ymc_trigger_ec(wdev, priv);
106 static void acpi_dev_put_helper(void *p) { acpi_dev_put(p); }
108 static int lenovo_ymc_probe(struct wmi_device *wdev, const void *ctx)
110 struct lenovo_ymc_private *priv;
111 struct input_dev *input_dev;
114 ec_trigger |= dmi_check_system(ec_trigger_quirk_dmi_table);
116 priv = devm_kzalloc(&wdev->dev, sizeof(*priv), GFP_KERNEL);
121 pr_debug("Lenovo YMC enable EC triggering.\n");
122 priv->ec_acpi_dev = acpi_dev_get_first_match_dev("VPC2004", NULL, -1);
124 if (!priv->ec_acpi_dev) {
125 dev_err(&wdev->dev, "Could not find EC ACPI device.\n");
128 err = devm_add_action_or_reset(&wdev->dev,
129 acpi_dev_put_helper, priv->ec_acpi_dev);
132 "Could not clean up EC ACPI device: %d\n", err);
137 input_dev = devm_input_allocate_device(&wdev->dev);
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);
148 "Could not set up input device keymap: %d\n", err);
152 err = input_register_device(input_dev);
155 "Could not register input device: %d\n", err);
159 priv->input_dev = input_dev;
160 dev_set_drvdata(&wdev->dev, priv);
162 /* Report the state for the first time on probe */
163 lenovo_ymc_trigger_ec(wdev, priv);
164 lenovo_ymc_notify(wdev, NULL);
168 static const struct wmi_device_id lenovo_ymc_wmi_id_table[] = {
169 { .guid_string = LENOVO_YMC_EVENT_GUID },
172 MODULE_DEVICE_TABLE(wmi, lenovo_ymc_wmi_id_table);
174 static struct wmi_driver lenovo_ymc_driver = {
176 .name = "lenovo-ymc",
178 .id_table = lenovo_ymc_wmi_id_table,
179 .probe = lenovo_ymc_probe,
180 .notify = lenovo_ymc_notify,
183 module_wmi_driver(lenovo_ymc_driver);
185 MODULE_AUTHOR("Gergo Koteles <soyer@irl.hu>");
186 MODULE_DESCRIPTION("Lenovo Yoga Mode Control driver");
187 MODULE_LICENSE("GPL");