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 <soyer@irl.hu>
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 force;
24 module_param(force, bool, 0444);
25 MODULE_PARM_DESC(force, "Force loading on boards without a convertible DMI chassis-type");
26
27 static const struct dmi_system_id allowed_chasis_types_dmi_table[] = {
28 {
29 .matches = {
30 DMI_EXACT_MATCH(DMI_CHASSIS_TYPE, "31" /* Convertible */),
31 },
32 },
33 {
34 .matches = {
35 DMI_EXACT_MATCH(DMI_CHASSIS_TYPE, "32" /* Detachable */),
36 },
37 },
38 { }
39 };
40
41 struct lenovo_ymc_private {
42 struct input_dev *input_dev;
43 };
44
45 static const struct key_entry lenovo_ymc_keymap[] = {
46 /* Ignore the uninitialized state */
47 { KE_IGNORE, 0x00 },
48 /* Laptop */
49 { KE_SW, 0x01, { .sw = { SW_TABLET_MODE, 0 } } },
50 /* Tablet */
51 { KE_SW, 0x02, { .sw = { SW_TABLET_MODE, 1 } } },
52 /* Drawing Board */
53 { KE_SW, 0x03, { .sw = { SW_TABLET_MODE, 1 } } },
54 /* Tent */
55 { KE_SW, 0x04, { .sw = { SW_TABLET_MODE, 1 } } },
56 { KE_END },
57 };
58
lenovo_ymc_notify(struct wmi_device * wdev,union acpi_object * data)59 static void lenovo_ymc_notify(struct wmi_device *wdev, union acpi_object *data)
60 {
61 struct lenovo_ymc_private *priv = dev_get_drvdata(&wdev->dev);
62 u32 input_val = 0;
63 struct acpi_buffer input = { sizeof(input_val), &input_val };
64 struct acpi_buffer output = { ACPI_ALLOCATE_BUFFER, NULL };
65 union acpi_object *obj;
66 acpi_status status;
67 int code;
68
69 status = wmi_evaluate_method(LENOVO_YMC_QUERY_GUID,
70 LENOVO_YMC_QUERY_INSTANCE,
71 LENOVO_YMC_QUERY_METHOD,
72 &input, &output);
73
74 if (ACPI_FAILURE(status)) {
75 dev_warn(&wdev->dev,
76 "Failed to evaluate query method: %s\n",
77 acpi_format_exception(status));
78 return;
79 }
80
81 obj = output.pointer;
82
83 if (obj->type != ACPI_TYPE_INTEGER) {
84 dev_warn(&wdev->dev,
85 "WMI event data is not an integer\n");
86 goto free_obj;
87 }
88 code = obj->integer.value;
89
90 if (!sparse_keymap_report_event(priv->input_dev, code, 1, true))
91 dev_warn(&wdev->dev, "Unknown key %d pressed\n", code);
92
93 free_obj:
94 kfree(obj);
95 ideapad_laptop_call_notifier(IDEAPAD_LAPTOP_YMC_EVENT, &code);
96 }
97
lenovo_ymc_probe(struct wmi_device * wdev,const void * ctx)98 static int lenovo_ymc_probe(struct wmi_device *wdev, const void *ctx)
99 {
100 struct lenovo_ymc_private *priv;
101 struct input_dev *input_dev;
102 int err;
103
104 if (!dmi_check_system(allowed_chasis_types_dmi_table)) {
105 if (force)
106 dev_info(&wdev->dev, "Force loading Lenovo YMC support\n");
107 else
108 return -ENODEV;
109 }
110
111 priv = devm_kzalloc(&wdev->dev, sizeof(*priv), GFP_KERNEL);
112 if (!priv)
113 return -ENOMEM;
114
115 input_dev = devm_input_allocate_device(&wdev->dev);
116 if (!input_dev)
117 return -ENOMEM;
118
119 input_dev->name = "Lenovo Yoga Tablet Mode Control switch";
120 input_dev->phys = LENOVO_YMC_EVENT_GUID "/input0";
121 input_dev->id.bustype = BUS_HOST;
122 input_dev->dev.parent = &wdev->dev;
123 err = sparse_keymap_setup(input_dev, lenovo_ymc_keymap, NULL);
124 if (err) {
125 dev_err(&wdev->dev,
126 "Could not set up input device keymap: %d\n", err);
127 return err;
128 }
129
130 err = input_register_device(input_dev);
131 if (err) {
132 dev_err(&wdev->dev,
133 "Could not register input device: %d\n", err);
134 return err;
135 }
136
137 priv->input_dev = input_dev;
138 dev_set_drvdata(&wdev->dev, priv);
139
140 /* Report the state for the first time on probe */
141 lenovo_ymc_notify(wdev, NULL);
142 return 0;
143 }
144
145 static const struct wmi_device_id lenovo_ymc_wmi_id_table[] = {
146 { .guid_string = LENOVO_YMC_EVENT_GUID },
147 { }
148 };
149 MODULE_DEVICE_TABLE(wmi, lenovo_ymc_wmi_id_table);
150
151 static struct wmi_driver lenovo_ymc_driver = {
152 .driver = {
153 .name = "lenovo-ymc",
154 },
155 .id_table = lenovo_ymc_wmi_id_table,
156 .probe = lenovo_ymc_probe,
157 .notify = lenovo_ymc_notify,
158 };
159
160 module_wmi_driver(lenovo_ymc_driver);
161
162 MODULE_AUTHOR("Gergo Koteles <soyer@irl.hu>");
163 MODULE_DESCRIPTION("Lenovo Yoga Mode Control driver");
164 MODULE_LICENSE("GPL");
165 MODULE_IMPORT_NS("IDEAPAD_LAPTOP");
166