diff options
author | Benjamin Tissoires <benjamin.tissoires@redhat.com> | 2016-11-25 11:11:41 -0500 |
---|---|---|
committer | Andy Shevchenko <andriy.shevchenko@linux.intel.com> | 2016-12-16 16:30:25 -0500 |
commit | 3dda3b3798f96d2974b5f60811142d3e25547807 (patch) | |
tree | b8cbcf8ad12078d58b98229ae303664d36d4dfe4 | |
parent | afc4715901f0dce3206837a7051af05abf5a1e06 (diff) |
platform/x86: Add custom surface3 platform device for controlling LID
The LID state provided by ACPI on the Surface 3 is not accurate.
The ACPI node doesn't get notified on LID open, which means the
LID input switch stays close most of the time.
Fortunatelly, there is a WMI method which directly queries the
GPIO underneath the LID state, so it's far more reliable than ACPI.
To get the notifications that the LID was opened/closed, we can
rely on the ACPI notification of the touchscreen: the DSDT shows
that the touchscreen will get notified on close/open as it also
controls its _STA method.
Note that we need to set the tag "power-switch" to the LID
input node through a udev rule for logind to accept it:
SUBSYSTEM=="input", KERNEL=="event*", KERNELS=="surface3-wmi", \
TAG+="power-switch"
Signed-off-by: Benjamin Tissoires <benjamin.tissoires@redhat.com>
Signed-off-by: Andy Shevchenko <andriy.shevchenko@linux.intel.com>
-rw-r--r-- | drivers/platform/x86/Kconfig | 12 | ||||
-rw-r--r-- | drivers/platform/x86/Makefile | 1 | ||||
-rw-r--r-- | drivers/platform/x86/surface3-wmi.c | 296 |
3 files changed, 309 insertions, 0 deletions
diff --git a/drivers/platform/x86/Kconfig b/drivers/platform/x86/Kconfig index 4639d970f8cb..0414d76dc15c 100644 --- a/drivers/platform/x86/Kconfig +++ b/drivers/platform/x86/Kconfig | |||
@@ -363,6 +363,18 @@ config IDEAPAD_LAPTOP | |||
363 | This is a driver for Lenovo IdeaPad netbooks contains drivers for | 363 | This is a driver for Lenovo IdeaPad netbooks contains drivers for |
364 | rfkill switch, hotkey, fan control and backlight control. | 364 | rfkill switch, hotkey, fan control and backlight control. |
365 | 365 | ||
366 | config SURFACE3_WMI | ||
367 | tristate "Surface 3 WMI Driver" | ||
368 | depends on ACPI_WMI | ||
369 | depends on DMI | ||
370 | depends on INPUT | ||
371 | depends on SPI | ||
372 | ---help--- | ||
373 | Say Y here if you have a Surface 3. | ||
374 | |||
375 | To compile this driver as a module, choose M here: the module will | ||
376 | be called surface3-wmi. | ||
377 | |||
366 | config THINKPAD_ACPI | 378 | config THINKPAD_ACPI |
367 | tristate "ThinkPad ACPI Laptop Extras" | 379 | tristate "ThinkPad ACPI Laptop Extras" |
368 | depends on ACPI | 380 | depends on ACPI |
diff --git a/drivers/platform/x86/Makefile b/drivers/platform/x86/Makefile index 2d6a587bded5..6cd3d909b759 100644 --- a/drivers/platform/x86/Makefile +++ b/drivers/platform/x86/Makefile | |||
@@ -34,6 +34,7 @@ obj-$(CONFIG_PANASONIC_LAPTOP) += panasonic-laptop.o | |||
34 | obj-$(CONFIG_INTEL_MENLOW) += intel_menlow.o | 34 | obj-$(CONFIG_INTEL_MENLOW) += intel_menlow.o |
35 | obj-$(CONFIG_ACPI_WMI) += wmi.o | 35 | obj-$(CONFIG_ACPI_WMI) += wmi.o |
36 | obj-$(CONFIG_MSI_WMI) += msi-wmi.o | 36 | obj-$(CONFIG_MSI_WMI) += msi-wmi.o |
37 | obj-$(CONFIG_SURFACE3_WMI) += surface3-wmi.o | ||
37 | obj-$(CONFIG_TOPSTAR_LAPTOP) += topstar-laptop.o | 38 | obj-$(CONFIG_TOPSTAR_LAPTOP) += topstar-laptop.o |
38 | 39 | ||
39 | # toshiba_acpi must link after wmi to ensure that wmi devices are found | 40 | # toshiba_acpi must link after wmi to ensure that wmi devices are found |
diff --git a/drivers/platform/x86/surface3-wmi.c b/drivers/platform/x86/surface3-wmi.c new file mode 100644 index 000000000000..5553b2b85e0a --- /dev/null +++ b/drivers/platform/x86/surface3-wmi.c | |||
@@ -0,0 +1,296 @@ | |||
1 | /* | ||
2 | * Driver for the LID cover switch of the Surface 3 | ||
3 | * | ||
4 | * Copyright (c) 2016 Red Hat Inc. | ||
5 | */ | ||
6 | |||
7 | /* | ||
8 | * This program is free software; you can redistribute it and/or modify it | ||
9 | * under the terms of the GNU General Public License as published by the Free | ||
10 | * Software Foundation; version 2 of the License. | ||
11 | */ | ||
12 | |||
13 | #include <linux/kernel.h> | ||
14 | #include <linux/module.h> | ||
15 | #include <linux/slab.h> | ||
16 | |||
17 | #include <linux/acpi.h> | ||
18 | #include <linux/dmi.h> | ||
19 | #include <linux/input.h> | ||
20 | #include <linux/mutex.h> | ||
21 | #include <linux/platform_device.h> | ||
22 | #include <linux/spi/spi.h> | ||
23 | |||
24 | MODULE_AUTHOR("Benjamin Tissoires <benjamin.tissoires@redhat.com>"); | ||
25 | MODULE_DESCRIPTION("Surface 3 platform driver"); | ||
26 | MODULE_LICENSE("GPL"); | ||
27 | |||
28 | #define ACPI_BUTTON_HID_LID "PNP0C0D" | ||
29 | #define SPI_CTL_OBJ_NAME "SPI" | ||
30 | #define SPI_TS_OBJ_NAME "NTRG" | ||
31 | |||
32 | #define SURFACE3_LID_GUID "F7CC25EC-D20B-404C-8903-0ED4359C18AE" | ||
33 | |||
34 | MODULE_ALIAS("wmi:" SURFACE3_LID_GUID); | ||
35 | |||
36 | static const struct dmi_system_id surface3_dmi_table[] = { | ||
37 | #if defined(CONFIG_X86) | ||
38 | { | ||
39 | .matches = { | ||
40 | DMI_MATCH(DMI_SYS_VENDOR, "Microsoft Corporation"), | ||
41 | DMI_MATCH(DMI_PRODUCT_NAME, "Surface 3"), | ||
42 | }, | ||
43 | }, | ||
44 | #endif | ||
45 | { } | ||
46 | }; | ||
47 | |||
48 | struct surface3_wmi { | ||
49 | struct acpi_device *touchscreen_adev; | ||
50 | struct acpi_device *pnp0c0d_adev; | ||
51 | struct acpi_hotplug_context hp; | ||
52 | struct input_dev *input; | ||
53 | }; | ||
54 | |||
55 | static struct platform_device *s3_wmi_pdev; | ||
56 | |||
57 | static struct surface3_wmi s3_wmi; | ||
58 | |||
59 | static DEFINE_MUTEX(s3_wmi_lock); | ||
60 | |||
61 | static int s3_wmi_query_block(const char *guid, int instance, int *ret) | ||
62 | { | ||
63 | acpi_status status; | ||
64 | union acpi_object *obj; | ||
65 | |||
66 | struct acpi_buffer output = { ACPI_ALLOCATE_BUFFER, NULL }; | ||
67 | |||
68 | mutex_lock(&s3_wmi_lock); | ||
69 | status = wmi_query_block(guid, instance, &output); | ||
70 | |||
71 | obj = output.pointer; | ||
72 | |||
73 | if (!obj || obj->type != ACPI_TYPE_INTEGER) { | ||
74 | if (obj) { | ||
75 | pr_err("query block returned object type: %d - buffer length:%d\n", | ||
76 | obj->type, | ||
77 | obj->type == ACPI_TYPE_BUFFER ? | ||
78 | obj->buffer.length : 0); | ||
79 | } | ||
80 | kfree(obj); | ||
81 | return -EINVAL; | ||
82 | } | ||
83 | *ret = obj->integer.value; | ||
84 | kfree(obj); | ||
85 | mutex_unlock(&s3_wmi_lock); | ||
86 | return 0; | ||
87 | } | ||
88 | |||
89 | static inline int s3_wmi_query_lid(int *ret) | ||
90 | { | ||
91 | return s3_wmi_query_block(SURFACE3_LID_GUID, 0, ret); | ||
92 | } | ||
93 | |||
94 | static int s3_wmi_send_lid_state(void) | ||
95 | { | ||
96 | int ret, lid_sw; | ||
97 | |||
98 | ret = s3_wmi_query_lid(&lid_sw); | ||
99 | if (ret) | ||
100 | return ret; | ||
101 | |||
102 | input_report_switch(s3_wmi.input, SW_LID, lid_sw); | ||
103 | input_sync(s3_wmi.input); | ||
104 | |||
105 | return 0; | ||
106 | } | ||
107 | |||
108 | static int s3_wmi_hp_notify(struct acpi_device *adev, u32 value) | ||
109 | { | ||
110 | return s3_wmi_send_lid_state(); | ||
111 | } | ||
112 | |||
113 | static acpi_status s3_wmi_attach_spi_device(acpi_handle handle, | ||
114 | u32 level, | ||
115 | void *data, | ||
116 | void **return_value) | ||
117 | { | ||
118 | struct acpi_device *adev, **ts_adev; | ||
119 | |||
120 | if (acpi_bus_get_device(handle, &adev)) | ||
121 | return AE_OK; | ||
122 | |||
123 | ts_adev = data; | ||
124 | |||
125 | if (strncmp(acpi_device_bid(adev), SPI_TS_OBJ_NAME, | ||
126 | strlen(SPI_TS_OBJ_NAME))) | ||
127 | return AE_OK; | ||
128 | |||
129 | if (*ts_adev) { | ||
130 | pr_err("duplicate entry %s\n", SPI_TS_OBJ_NAME); | ||
131 | return AE_OK; | ||
132 | } | ||
133 | |||
134 | *ts_adev = adev; | ||
135 | |||
136 | return AE_OK; | ||
137 | } | ||
138 | |||
139 | static int s3_wmi_check_platform_device(struct device *dev, void *data) | ||
140 | { | ||
141 | struct acpi_device *adev, *ts_adev; | ||
142 | acpi_handle handle; | ||
143 | acpi_status status; | ||
144 | |||
145 | /* ignore non ACPI devices */ | ||
146 | handle = ACPI_HANDLE(dev); | ||
147 | if (!handle || acpi_bus_get_device(handle, &adev)) | ||
148 | return 0; | ||
149 | |||
150 | /* check for LID ACPI switch */ | ||
151 | if (!strcmp(ACPI_BUTTON_HID_LID, acpi_device_hid(adev))) { | ||
152 | s3_wmi.pnp0c0d_adev = adev; | ||
153 | return 0; | ||
154 | } | ||
155 | |||
156 | /* ignore non SPI controllers */ | ||
157 | if (strncmp(acpi_device_bid(adev), SPI_CTL_OBJ_NAME, | ||
158 | strlen(SPI_CTL_OBJ_NAME))) | ||
159 | return 0; | ||
160 | |||
161 | status = acpi_walk_namespace(ACPI_TYPE_DEVICE, handle, 1, | ||
162 | s3_wmi_attach_spi_device, NULL, | ||
163 | &ts_adev, NULL); | ||
164 | if (ACPI_FAILURE(status)) | ||
165 | dev_warn(dev, "failed to enumerate SPI slaves\n"); | ||
166 | |||
167 | if (!ts_adev) | ||
168 | return 0; | ||
169 | |||
170 | s3_wmi.touchscreen_adev = ts_adev; | ||
171 | |||
172 | return 0; | ||
173 | } | ||
174 | |||
175 | static int s3_wmi_create_and_register_input(struct platform_device *pdev) | ||
176 | { | ||
177 | struct input_dev *input; | ||
178 | int error; | ||
179 | |||
180 | input = devm_input_allocate_device(&pdev->dev); | ||
181 | if (!input) | ||
182 | return -ENOMEM; | ||
183 | |||
184 | input->name = "Lid Switch"; | ||
185 | input->phys = "button/input0"; | ||
186 | input->id.bustype = BUS_HOST; | ||
187 | input->id.product = 0x0005; | ||
188 | |||
189 | input_set_capability(input, EV_SW, SW_LID); | ||
190 | |||
191 | error = input_register_device(input); | ||
192 | if (error) | ||
193 | goto out_err; | ||
194 | |||
195 | s3_wmi.input = input; | ||
196 | |||
197 | return 0; | ||
198 | out_err: | ||
199 | input_free_device(s3_wmi.input); | ||
200 | return error; | ||
201 | } | ||
202 | |||
203 | static int __init s3_wmi_probe(struct platform_device *pdev) | ||
204 | { | ||
205 | int error; | ||
206 | |||
207 | if (!dmi_check_system(surface3_dmi_table)) | ||
208 | return -ENODEV; | ||
209 | |||
210 | memset(&s3_wmi, 0, sizeof(s3_wmi)); | ||
211 | |||
212 | bus_for_each_dev(&platform_bus_type, NULL, NULL, | ||
213 | s3_wmi_check_platform_device); | ||
214 | |||
215 | if (!s3_wmi.touchscreen_adev) | ||
216 | return -ENODEV; | ||
217 | |||
218 | acpi_bus_trim(s3_wmi.pnp0c0d_adev); | ||
219 | |||
220 | error = s3_wmi_create_and_register_input(pdev); | ||
221 | if (error) | ||
222 | goto restore_acpi_lid; | ||
223 | |||
224 | acpi_initialize_hp_context(s3_wmi.touchscreen_adev, &s3_wmi.hp, | ||
225 | s3_wmi_hp_notify, NULL); | ||
226 | |||
227 | s3_wmi_send_lid_state(); | ||
228 | |||
229 | return 0; | ||
230 | |||
231 | restore_acpi_lid: | ||
232 | acpi_bus_scan(s3_wmi.pnp0c0d_adev->handle); | ||
233 | return error; | ||
234 | } | ||
235 | |||
236 | static int s3_wmi_remove(struct platform_device *device) | ||
237 | { | ||
238 | /* remove the hotplug context from the acpi device */ | ||
239 | s3_wmi.touchscreen_adev->hp = NULL; | ||
240 | |||
241 | /* reinstall the actual PNPC0C0D LID default handle */ | ||
242 | acpi_bus_scan(s3_wmi.pnp0c0d_adev->handle); | ||
243 | return 0; | ||
244 | } | ||
245 | |||
246 | #ifdef CONFIG_PM | ||
247 | static int s3_wmi_resume(struct device *dev) | ||
248 | { | ||
249 | s3_wmi_send_lid_state(); | ||
250 | return 0; | ||
251 | } | ||
252 | #endif | ||
253 | static SIMPLE_DEV_PM_OPS(s3_wmi_pm, NULL, s3_wmi_resume); | ||
254 | |||
255 | static struct platform_driver s3_wmi_driver = { | ||
256 | .driver = { | ||
257 | .name = "surface3-wmi", | ||
258 | .pm = &s3_wmi_pm, | ||
259 | }, | ||
260 | .remove = s3_wmi_remove, | ||
261 | }; | ||
262 | |||
263 | static int __init s3_wmi_init(void) | ||
264 | { | ||
265 | int error; | ||
266 | |||
267 | s3_wmi_pdev = platform_device_alloc("surface3-wmi", -1); | ||
268 | if (!s3_wmi_pdev) | ||
269 | return -ENOMEM; | ||
270 | |||
271 | error = platform_device_add(s3_wmi_pdev); | ||
272 | if (error) | ||
273 | goto err_device_put; | ||
274 | |||
275 | error = platform_driver_probe(&s3_wmi_driver, s3_wmi_probe); | ||
276 | if (error) | ||
277 | goto err_device_del; | ||
278 | |||
279 | pr_info("Surface 3 WMI Extras loaded\n"); | ||
280 | return 0; | ||
281 | |||
282 | err_device_del: | ||
283 | platform_device_del(s3_wmi_pdev); | ||
284 | err_device_put: | ||
285 | platform_device_put(s3_wmi_pdev); | ||
286 | return error; | ||
287 | } | ||
288 | |||
289 | static void __exit s3_wmi_exit(void) | ||
290 | { | ||
291 | platform_device_unregister(s3_wmi_pdev); | ||
292 | platform_driver_unregister(&s3_wmi_driver); | ||
293 | } | ||
294 | |||
295 | module_init(s3_wmi_init); | ||
296 | module_exit(s3_wmi_exit); | ||