Get rid of 'remove_new' relic from platform driver struct
[linux.git] / drivers / platform / surface / surface_gpe.c
blobb359413903b13c4f8e8b284ef7ae6f6db3f47d72
1 // SPDX-License-Identifier: GPL-2.0-or-later
2 /*
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.
7 * Copyright (C) 2020-2022 Maximilian Luz <luzmaximilian@gmail.com>
8 */
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
21 * lid.
24 static const struct property_entry lid_device_props_l17[] = {
25 PROPERTY_ENTRY_U32("gpe", 0x17),
26 {},
29 static const struct property_entry lid_device_props_l4B[] = {
30 PROPERTY_ENTRY_U32("gpe", 0x4B),
31 {},
34 static const struct property_entry lid_device_props_l4D[] = {
35 PROPERTY_ENTRY_U32("gpe", 0x4D),
36 {},
39 static const struct property_entry lid_device_props_l4F[] = {
40 PROPERTY_ENTRY_U32("gpe", 0x4F),
41 {},
44 static const struct property_entry lid_device_props_l57[] = {
45 PROPERTY_ENTRY_U32("gpe", 0x57),
46 {},
50 * Note: When changing this, don't forget to check that the MODULE_ALIAS below
51 * still fits.
53 static const struct dmi_system_id dmi_lid_device_table[] = {
55 .ident = "Surface Pro 4",
56 .matches = {
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",
64 .matches = {
66 * We match for SKU here due to generic product name
67 * "Surface Pro".
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)",
76 .matches = {
78 * We match for SKU here due to generic product name
79 * "Surface Pro"
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",
88 .matches = {
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",
96 .matches = {
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 Pro 8",
104 .matches = {
105 DMI_EXACT_MATCH(DMI_SYS_VENDOR, "Microsoft Corporation"),
106 DMI_EXACT_MATCH(DMI_PRODUCT_NAME, "Surface Pro 8"),
108 .driver_data = (void *)lid_device_props_l4B,
111 .ident = "Surface Book 1",
112 .matches = {
113 DMI_EXACT_MATCH(DMI_SYS_VENDOR, "Microsoft Corporation"),
114 DMI_EXACT_MATCH(DMI_PRODUCT_NAME, "Surface Book"),
116 .driver_data = (void *)lid_device_props_l17,
119 .ident = "Surface Book 2",
120 .matches = {
121 DMI_EXACT_MATCH(DMI_SYS_VENDOR, "Microsoft Corporation"),
122 DMI_EXACT_MATCH(DMI_PRODUCT_NAME, "Surface Book 2"),
124 .driver_data = (void *)lid_device_props_l17,
127 .ident = "Surface Book 3",
128 .matches = {
129 DMI_EXACT_MATCH(DMI_SYS_VENDOR, "Microsoft Corporation"),
130 DMI_EXACT_MATCH(DMI_PRODUCT_NAME, "Surface Book 3"),
132 .driver_data = (void *)lid_device_props_l4D,
135 .ident = "Surface Laptop 1",
136 .matches = {
137 DMI_EXACT_MATCH(DMI_SYS_VENDOR, "Microsoft Corporation"),
138 DMI_EXACT_MATCH(DMI_PRODUCT_NAME, "Surface Laptop"),
140 .driver_data = (void *)lid_device_props_l57,
143 .ident = "Surface Laptop 2",
144 .matches = {
145 DMI_EXACT_MATCH(DMI_SYS_VENDOR, "Microsoft Corporation"),
146 DMI_EXACT_MATCH(DMI_PRODUCT_NAME, "Surface Laptop 2"),
148 .driver_data = (void *)lid_device_props_l57,
151 .ident = "Surface Laptop 3 (Intel 13\")",
152 .matches = {
154 * We match for SKU here due to different variants: The
155 * AMD (15") version does not rely on GPEs.
157 DMI_EXACT_MATCH(DMI_SYS_VENDOR, "Microsoft Corporation"),
158 DMI_EXACT_MATCH(DMI_PRODUCT_SKU, "Surface_Laptop_3_1867:1868"),
160 .driver_data = (void *)lid_device_props_l4D,
163 .ident = "Surface Laptop 3 (Intel 15\")",
164 .matches = {
166 * We match for SKU here due to different variants: The
167 * AMD (15") version does not rely on GPEs.
169 DMI_EXACT_MATCH(DMI_SYS_VENDOR, "Microsoft Corporation"),
170 DMI_EXACT_MATCH(DMI_PRODUCT_SKU, "Surface_Laptop_3_1872"),
172 .driver_data = (void *)lid_device_props_l4D,
175 .ident = "Surface Laptop 4 (Intel 13\")",
176 .matches = {
178 * We match for SKU here due to different variants: The
179 * AMD (15") version does not rely on GPEs.
181 DMI_EXACT_MATCH(DMI_SYS_VENDOR, "Microsoft Corporation"),
182 DMI_EXACT_MATCH(DMI_PRODUCT_SKU, "Surface_Laptop_4_1950:1951"),
184 .driver_data = (void *)lid_device_props_l4B,
187 .ident = "Surface Laptop Studio",
188 .matches = {
189 DMI_EXACT_MATCH(DMI_SYS_VENDOR, "Microsoft Corporation"),
190 DMI_EXACT_MATCH(DMI_PRODUCT_NAME, "Surface Laptop Studio"),
192 .driver_data = (void *)lid_device_props_l4B,
197 struct surface_lid_device {
198 u32 gpe_number;
201 static int surface_lid_enable_wakeup(struct device *dev, bool enable)
203 const struct surface_lid_device *lid = dev_get_drvdata(dev);
204 int action = enable ? ACPI_GPE_ENABLE : ACPI_GPE_DISABLE;
205 acpi_status status;
207 status = acpi_set_gpe_wake_mask(NULL, lid->gpe_number, action);
208 if (ACPI_FAILURE(status)) {
209 dev_err(dev, "failed to set GPE wake mask: %s\n",
210 acpi_format_exception(status));
211 return -EINVAL;
214 return 0;
217 static int __maybe_unused surface_gpe_suspend(struct device *dev)
219 return surface_lid_enable_wakeup(dev, true);
222 static int __maybe_unused surface_gpe_resume(struct device *dev)
224 return surface_lid_enable_wakeup(dev, false);
227 static SIMPLE_DEV_PM_OPS(surface_gpe_pm, surface_gpe_suspend, surface_gpe_resume);
229 static int surface_gpe_probe(struct platform_device *pdev)
231 struct surface_lid_device *lid;
232 u32 gpe_number;
233 acpi_status status;
234 int ret;
236 ret = device_property_read_u32(&pdev->dev, "gpe", &gpe_number);
237 if (ret) {
238 dev_err(&pdev->dev, "failed to read 'gpe' property: %d\n", ret);
239 return ret;
242 lid = devm_kzalloc(&pdev->dev, sizeof(*lid), GFP_KERNEL);
243 if (!lid)
244 return -ENOMEM;
246 lid->gpe_number = gpe_number;
247 platform_set_drvdata(pdev, lid);
249 status = acpi_mark_gpe_for_wake(NULL, gpe_number);
250 if (ACPI_FAILURE(status)) {
251 dev_err(&pdev->dev, "failed to mark GPE for wake: %s\n",
252 acpi_format_exception(status));
253 return -EINVAL;
256 status = acpi_enable_gpe(NULL, gpe_number);
257 if (ACPI_FAILURE(status)) {
258 dev_err(&pdev->dev, "failed to enable GPE: %s\n",
259 acpi_format_exception(status));
260 return -EINVAL;
263 ret = surface_lid_enable_wakeup(&pdev->dev, false);
264 if (ret)
265 acpi_disable_gpe(NULL, gpe_number);
267 return ret;
270 static void surface_gpe_remove(struct platform_device *pdev)
272 struct surface_lid_device *lid = dev_get_drvdata(&pdev->dev);
274 /* restore default behavior without this module */
275 surface_lid_enable_wakeup(&pdev->dev, false);
276 acpi_disable_gpe(NULL, lid->gpe_number);
279 static struct platform_driver surface_gpe_driver = {
280 .probe = surface_gpe_probe,
281 .remove = surface_gpe_remove,
282 .driver = {
283 .name = "surface_gpe",
284 .pm = &surface_gpe_pm,
285 .probe_type = PROBE_PREFER_ASYNCHRONOUS,
289 static struct platform_device *surface_gpe_device;
291 static int __init surface_gpe_init(void)
293 const struct dmi_system_id *match;
294 struct platform_device *pdev;
295 struct fwnode_handle *fwnode;
296 int status;
298 match = dmi_first_match(dmi_lid_device_table);
299 if (!match) {
300 pr_info("no compatible Microsoft Surface device found, exiting\n");
301 return -ENODEV;
304 status = platform_driver_register(&surface_gpe_driver);
305 if (status)
306 return status;
308 fwnode = fwnode_create_software_node(match->driver_data, NULL);
309 if (IS_ERR(fwnode)) {
310 status = PTR_ERR(fwnode);
311 goto err_node;
314 pdev = platform_device_alloc("surface_gpe", PLATFORM_DEVID_NONE);
315 if (!pdev) {
316 status = -ENOMEM;
317 goto err_alloc;
320 pdev->dev.fwnode = fwnode;
322 status = platform_device_add(pdev);
323 if (status)
324 goto err_add;
326 surface_gpe_device = pdev;
327 return 0;
329 err_add:
330 platform_device_put(pdev);
331 err_alloc:
332 fwnode_remove_software_node(fwnode);
333 err_node:
334 platform_driver_unregister(&surface_gpe_driver);
335 return status;
337 module_init(surface_gpe_init);
339 static void __exit surface_gpe_exit(void)
341 struct fwnode_handle *fwnode = surface_gpe_device->dev.fwnode;
343 platform_device_unregister(surface_gpe_device);
344 platform_driver_unregister(&surface_gpe_driver);
345 fwnode_remove_software_node(fwnode);
347 module_exit(surface_gpe_exit);
349 MODULE_AUTHOR("Maximilian Luz <luzmaximilian@gmail.com>");
350 MODULE_DESCRIPTION("Surface GPE/Lid Driver");
351 MODULE_LICENSE("GPL");
352 MODULE_ALIAS("dmi:*:svnMicrosoftCorporation:pnSurface*:*");