1 // SPDX-License-Identifier: GPL-2.0-or-later
3 * Surface GPE/Lid driver to enable wakeup from suspend via the lid by
4 * properly configuring the respective GPEs. Required for wakeup via lid on
5 * newer Intel-based Microsoft Surface devices.
10 #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
12 #include <linux/acpi.h>
13 #include <linux/dmi.h>
14 #include <linux/kernel.h>
15 #include <linux/module.h>
16 #include <linux/platform_device.h>
19 * Note: The GPE numbers for the lid devices found below have been obtained
20 * from ACPI/the DSDT table, specifically from the GPE handler for the
24 static const struct property_entry lid_device_props_l17[] = {
25 PROPERTY_ENTRY_U32("gpe", 0x17),
29 static const struct property_entry lid_device_props_l4B[] = {
30 PROPERTY_ENTRY_U32("gpe", 0x4B),
34 static const struct property_entry lid_device_props_l4D[] = {
35 PROPERTY_ENTRY_U32("gpe", 0x4D),
39 static const struct property_entry lid_device_props_l4F[] = {
40 PROPERTY_ENTRY_U32("gpe", 0x4F),
44 static const struct property_entry lid_device_props_l57[] = {
45 PROPERTY_ENTRY_U32("gpe", 0x57),
50 * Note: When changing this, don't forget to check that the MODULE_ALIAS below
53 static const struct dmi_system_id dmi_lid_device_table[] = {
55 .ident = "Surface Pro 4",
57 DMI_EXACT_MATCH(DMI_SYS_VENDOR, "Microsoft Corporation"),
58 DMI_EXACT_MATCH(DMI_PRODUCT_NAME, "Surface Pro 4"),
60 .driver_data = (void *)lid_device_props_l17,
63 .ident = "Surface Pro 5",
66 * We match for SKU here due to generic product name
69 DMI_EXACT_MATCH(DMI_SYS_VENDOR, "Microsoft Corporation"),
70 DMI_EXACT_MATCH(DMI_PRODUCT_SKU, "Surface_Pro_1796"),
72 .driver_data = (void *)lid_device_props_l4F,
75 .ident = "Surface Pro 5 (LTE)",
78 * We match for SKU here due to generic product name
81 DMI_EXACT_MATCH(DMI_SYS_VENDOR, "Microsoft Corporation"),
82 DMI_EXACT_MATCH(DMI_PRODUCT_SKU, "Surface_Pro_1807"),
84 .driver_data = (void *)lid_device_props_l4F,
87 .ident = "Surface Pro 6",
89 DMI_EXACT_MATCH(DMI_SYS_VENDOR, "Microsoft Corporation"),
90 DMI_EXACT_MATCH(DMI_PRODUCT_NAME, "Surface Pro 6"),
92 .driver_data = (void *)lid_device_props_l4F,
95 .ident = "Surface Pro 7",
97 DMI_EXACT_MATCH(DMI_SYS_VENDOR, "Microsoft Corporation"),
98 DMI_EXACT_MATCH(DMI_PRODUCT_NAME, "Surface Pro 7"),
100 .driver_data = (void *)lid_device_props_l4D,
103 .ident = "Surface Book 1",
105 DMI_EXACT_MATCH(DMI_SYS_VENDOR, "Microsoft Corporation"),
106 DMI_EXACT_MATCH(DMI_PRODUCT_NAME, "Surface Book"),
108 .driver_data = (void *)lid_device_props_l17,
111 .ident = "Surface Book 2",
113 DMI_EXACT_MATCH(DMI_SYS_VENDOR, "Microsoft Corporation"),
114 DMI_EXACT_MATCH(DMI_PRODUCT_NAME, "Surface Book 2"),
116 .driver_data = (void *)lid_device_props_l17,
119 .ident = "Surface Book 3",
121 DMI_EXACT_MATCH(DMI_SYS_VENDOR, "Microsoft Corporation"),
122 DMI_EXACT_MATCH(DMI_PRODUCT_NAME, "Surface Book 3"),
124 .driver_data = (void *)lid_device_props_l4D,
127 .ident = "Surface Laptop 1",
129 DMI_EXACT_MATCH(DMI_SYS_VENDOR, "Microsoft Corporation"),
130 DMI_EXACT_MATCH(DMI_PRODUCT_NAME, "Surface Laptop"),
132 .driver_data = (void *)lid_device_props_l57,
135 .ident = "Surface Laptop 2",
137 DMI_EXACT_MATCH(DMI_SYS_VENDOR, "Microsoft Corporation"),
138 DMI_EXACT_MATCH(DMI_PRODUCT_NAME, "Surface Laptop 2"),
140 .driver_data = (void *)lid_device_props_l57,
143 .ident = "Surface Laptop 3 (Intel 13\")",
146 * We match for SKU here due to different variants: The
147 * AMD (15") version does not rely on GPEs.
149 DMI_EXACT_MATCH(DMI_SYS_VENDOR, "Microsoft Corporation"),
150 DMI_EXACT_MATCH(DMI_PRODUCT_SKU, "Surface_Laptop_3_1867:1868"),
152 .driver_data = (void *)lid_device_props_l4D,
155 .ident = "Surface Laptop 3 (Intel 15\")",
158 * We match for SKU here due to different variants: The
159 * AMD (15") version does not rely on GPEs.
161 DMI_EXACT_MATCH(DMI_SYS_VENDOR, "Microsoft Corporation"),
162 DMI_EXACT_MATCH(DMI_PRODUCT_SKU, "Surface_Laptop_3_1872"),
164 .driver_data = (void *)lid_device_props_l4D,
167 .ident = "Surface Laptop Studio",
169 DMI_EXACT_MATCH(DMI_SYS_VENDOR, "Microsoft Corporation"),
170 DMI_EXACT_MATCH(DMI_PRODUCT_NAME, "Surface Laptop Studio"),
172 .driver_data = (void *)lid_device_props_l4B,
177 struct surface_lid_device {
181 static int surface_lid_enable_wakeup(struct device *dev, bool enable)
183 const struct surface_lid_device *lid = dev_get_drvdata(dev);
184 int action = enable ? ACPI_GPE_ENABLE : ACPI_GPE_DISABLE;
187 status = acpi_set_gpe_wake_mask(NULL, lid->gpe_number, action);
188 if (ACPI_FAILURE(status)) {
189 dev_err(dev, "failed to set GPE wake mask: %s\n",
190 acpi_format_exception(status));
197 static int __maybe_unused surface_gpe_suspend(struct device *dev)
199 return surface_lid_enable_wakeup(dev, true);
202 static int __maybe_unused surface_gpe_resume(struct device *dev)
204 return surface_lid_enable_wakeup(dev, false);
207 static SIMPLE_DEV_PM_OPS(surface_gpe_pm, surface_gpe_suspend, surface_gpe_resume);
209 static int surface_gpe_probe(struct platform_device *pdev)
211 struct surface_lid_device *lid;
216 ret = device_property_read_u32(&pdev->dev, "gpe", &gpe_number);
218 dev_err(&pdev->dev, "failed to read 'gpe' property: %d\n", ret);
222 lid = devm_kzalloc(&pdev->dev, sizeof(*lid), GFP_KERNEL);
226 lid->gpe_number = gpe_number;
227 platform_set_drvdata(pdev, lid);
229 status = acpi_mark_gpe_for_wake(NULL, gpe_number);
230 if (ACPI_FAILURE(status)) {
231 dev_err(&pdev->dev, "failed to mark GPE for wake: %s\n",
232 acpi_format_exception(status));
236 status = acpi_enable_gpe(NULL, gpe_number);
237 if (ACPI_FAILURE(status)) {
238 dev_err(&pdev->dev, "failed to enable GPE: %s\n",
239 acpi_format_exception(status));
243 ret = surface_lid_enable_wakeup(&pdev->dev, false);
245 acpi_disable_gpe(NULL, gpe_number);
250 static int surface_gpe_remove(struct platform_device *pdev)
252 struct surface_lid_device *lid = dev_get_drvdata(&pdev->dev);
254 /* restore default behavior without this module */
255 surface_lid_enable_wakeup(&pdev->dev, false);
256 acpi_disable_gpe(NULL, lid->gpe_number);
261 static struct platform_driver surface_gpe_driver = {
262 .probe = surface_gpe_probe,
263 .remove = surface_gpe_remove,
265 .name = "surface_gpe",
266 .pm = &surface_gpe_pm,
267 .probe_type = PROBE_PREFER_ASYNCHRONOUS,
271 static struct platform_device *surface_gpe_device;
273 static int __init surface_gpe_init(void)
275 const struct dmi_system_id *match;
276 struct platform_device *pdev;
277 struct fwnode_handle *fwnode;
280 match = dmi_first_match(dmi_lid_device_table);
282 pr_info("no compatible Microsoft Surface device found, exiting\n");
286 status = platform_driver_register(&surface_gpe_driver);
290 fwnode = fwnode_create_software_node(match->driver_data, NULL);
291 if (IS_ERR(fwnode)) {
292 status = PTR_ERR(fwnode);
296 pdev = platform_device_alloc("surface_gpe", PLATFORM_DEVID_NONE);
302 pdev->dev.fwnode = fwnode;
304 status = platform_device_add(pdev);
308 surface_gpe_device = pdev;
312 platform_device_put(pdev);
314 fwnode_remove_software_node(fwnode);
316 platform_driver_unregister(&surface_gpe_driver);
319 module_init(surface_gpe_init);
321 static void __exit surface_gpe_exit(void)
323 struct fwnode_handle *fwnode = surface_gpe_device->dev.fwnode;
325 platform_device_unregister(surface_gpe_device);
326 platform_driver_unregister(&surface_gpe_driver);
327 fwnode_remove_software_node(fwnode);
329 module_exit(surface_gpe_exit);
332 MODULE_DESCRIPTION("Surface GPE/Lid Driver");
333 MODULE_LICENSE("GPL");
334 MODULE_ALIAS("dmi:*:svnMicrosoftCorporation:pnSurface*:*");