summaryrefslogtreecommitdiff
path: root/drivers/platform/surface/surface_gpe.c
blob: c219b840d491a2de85c01a26079061339fa70b9c (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
// SPDX-License-Identifier: GPL-2.0-or-later
/*
 * Surface GPE/Lid driver to enable wakeup from suspend via the lid by
 * properly configuring the respective GPEs. Required for wakeup via lid on
 * newer Intel-based Microsoft Surface devices.
 *
 * Copyright (C) 2020-2022 Maximilian Luz <luzmaximilian@gmail.com>
 */

#define pr_fmt(fmt) KBUILD_MODNAME ": " fmt

#include <linux/acpi.h>
#include <linux/dmi.h>
#include <linux/kernel.h>
#include <linux/module.h>
#include <linux/platform_device.h>

/*
 * Note: The GPE numbers for the lid devices found below have been obtained
 *       from ACPI/the DSDT table, specifically from the GPE handler for the
 *       lid.
 */

static const struct property_entry lid_device_props_l17[] = {
	PROPERTY_ENTRY_U32("gpe", 0x17),
	{},
};

static const struct property_entry lid_device_props_l4B[] = {
	PROPERTY_ENTRY_U32("gpe", 0x4B),
	{},
};

static const struct property_entry lid_device_props_l4D[] = {
	PROPERTY_ENTRY_U32("gpe", 0x4D),
	{},
};

static const struct property_entry lid_device_props_l4F[] = {
	PROPERTY_ENTRY_U32("gpe", 0x4F),
	{},
};

static const struct property_entry lid_device_props_l57[] = {
	PROPERTY_ENTRY_U32("gpe", 0x57),
	{},
};

/*
 * Note: When changing this, don't forget to check that the MODULE_ALIAS below
 *       still fits.
 */
static const struct dmi_system_id dmi_lid_device_table[] = {
	{
		.ident = "Surface Pro 4",
		.matches = {
			DMI_EXACT_MATCH(DMI_SYS_VENDOR, "Microsoft Corporation"),
			DMI_EXACT_MATCH(DMI_PRODUCT_NAME, "Surface Pro 4"),
		},
		.driver_data = (void *)lid_device_props_l17,
	},
	{
		.ident = "Surface Pro 5",
		.matches = {
			/*
			 * We match for SKU here due to generic product name
			 * "Surface Pro".
			 */
			DMI_EXACT_MATCH(DMI_SYS_VENDOR, "Microsoft Corporation"),
			DMI_EXACT_MATCH(DMI_PRODUCT_SKU, "Surface_Pro_1796"),
		},
		.driver_data = (void *)lid_device_props_l4F,
	},
	{
		.ident = "Surface Pro 5 (LTE)",
		.matches = {
			/*
			 * We match for SKU here due to generic product name
			 * "Surface Pro"
			 */
			DMI_EXACT_MATCH(DMI_SYS_VENDOR, "Microsoft Corporation"),
			DMI_EXACT_MATCH(DMI_PRODUCT_SKU, "Surface_Pro_1807"),
		},
		.driver_data = (void *)lid_device_props_l4F,
	},
	{
		.ident = "Surface Pro 6",
		.matches = {
			DMI_EXACT_MATCH(DMI_SYS_VENDOR, "Microsoft Corporation"),
			DMI_EXACT_MATCH(DMI_PRODUCT_NAME, "Surface Pro 6"),
		},
		.driver_data = (void *)lid_device_props_l4F,
	},
	{
		.ident = "Surface Pro 7",
		.matches = {
			DMI_EXACT_MATCH(DMI_SYS_VENDOR, "Microsoft Corporation"),
			DMI_EXACT_MATCH(DMI_PRODUCT_NAME, "Surface Pro 7"),
		},
		.driver_data = (void *)lid_device_props_l4D,
	},
	{
		.ident = "Surface Pro 8",
		.matches = {
			DMI_EXACT_MATCH(DMI_SYS_VENDOR, "Microsoft Corporation"),
			DMI_EXACT_MATCH(DMI_PRODUCT_NAME, "Surface Pro 8"),
		},
		.driver_data = (void *)lid_device_props_l4B,
	},
	{
		.ident = "Surface Book 1",
		.matches = {
			DMI_EXACT_MATCH(DMI_SYS_VENDOR, "Microsoft Corporation"),
			DMI_EXACT_MATCH(DMI_PRODUCT_NAME, "Surface Book"),
		},
		.driver_data = (void *)lid_device_props_l17,
	},
	{
		.ident = "Surface Book 2",
		.matches = {
			DMI_EXACT_MATCH(DMI_SYS_VENDOR, "Microsoft Corporation"),
			DMI_EXACT_MATCH(DMI_PRODUCT_NAME, "Surface Book 2"),
		},
		.driver_data = (void *)lid_device_props_l17,
	},
	{
		.ident = "Surface Book 3",
		.matches = {
			DMI_EXACT_MATCH(DMI_SYS_VENDOR, "Microsoft Corporation"),
			DMI_EXACT_MATCH(DMI_PRODUCT_NAME, "Surface Book 3"),
		},
		.driver_data = (void *)lid_device_props_l4D,
	},
	{
		.ident = "Surface Laptop 1",
		.matches = {
			DMI_EXACT_MATCH(DMI_SYS_VENDOR, "Microsoft Corporation"),
			DMI_EXACT_MATCH(DMI_PRODUCT_NAME, "Surface Laptop"),
		},
		.driver_data = (void *)lid_device_props_l57,
	},
	{
		.ident = "Surface Laptop 2",
		.matches = {
			DMI_EXACT_MATCH(DMI_SYS_VENDOR, "Microsoft Corporation"),
			DMI_EXACT_MATCH(DMI_PRODUCT_NAME, "Surface Laptop 2"),
		},
		.driver_data = (void *)lid_device_props_l57,
	},
	{
		.ident = "Surface Laptop 3 (Intel 13\")",
		.matches = {
			/*
			 * We match for SKU here due to different variants: The
			 * AMD (15") version does not rely on GPEs.
			 */
			DMI_EXACT_MATCH(DMI_SYS_VENDOR, "Microsoft Corporation"),
			DMI_EXACT_MATCH(DMI_PRODUCT_SKU, "Surface_Laptop_3_1867:1868"),
		},
		.driver_data = (void *)lid_device_props_l4D,
	},
	{
		.ident = "Surface Laptop 3 (Intel 15\")",
		.matches = {
			/*
			 * We match for SKU here due to different variants: The
			 * AMD (15") version does not rely on GPEs.
			 */
			DMI_EXACT_MATCH(DMI_SYS_VENDOR, "Microsoft Corporation"),
			DMI_EXACT_MATCH(DMI_PRODUCT_SKU, "Surface_Laptop_3_1872"),
		},
		.driver_data = (void *)lid_device_props_l4D,
	},
	{
		.ident = "Surface Laptop 4 (Intel 13\")",
		.matches = {
			/*
			 * We match for SKU here due to different variants: The
			 * AMD (15") version does not rely on GPEs.
			 */
			DMI_EXACT_MATCH(DMI_SYS_VENDOR, "Microsoft Corporation"),
			DMI_EXACT_MATCH(DMI_PRODUCT_SKU, "Surface_Laptop_4_1950:1951"),
		},
		.driver_data = (void *)lid_device_props_l4B,
	},
	{
		.ident = "Surface Laptop Studio",
		.matches = {
			DMI_EXACT_MATCH(DMI_SYS_VENDOR, "Microsoft Corporation"),
			DMI_EXACT_MATCH(DMI_PRODUCT_NAME, "Surface Laptop Studio"),
		},
		.driver_data = (void *)lid_device_props_l4B,
	},
	{ }
};

struct surface_lid_device {
	u32 gpe_number;
};

static int surface_lid_enable_wakeup(struct device *dev, bool enable)
{
	const struct surface_lid_device *lid = dev_get_drvdata(dev);
	int action = enable ? ACPI_GPE_ENABLE : ACPI_GPE_DISABLE;
	acpi_status status;

	status = acpi_set_gpe_wake_mask(NULL, lid->gpe_number, action);
	if (ACPI_FAILURE(status)) {
		dev_err(dev, "failed to set GPE wake mask: %s\n",
			acpi_format_exception(status));
		return -EINVAL;
	}

	return 0;
}

static int __maybe_unused surface_gpe_suspend(struct device *dev)
{
	return surface_lid_enable_wakeup(dev, true);
}

static int __maybe_unused surface_gpe_resume(struct device *dev)
{
	return surface_lid_enable_wakeup(dev, false);
}

static SIMPLE_DEV_PM_OPS(surface_gpe_pm, surface_gpe_suspend, surface_gpe_resume);

static int surface_gpe_probe(struct platform_device *pdev)
{
	struct surface_lid_device *lid;
	u32 gpe_number;
	acpi_status status;
	int ret;

	ret = device_property_read_u32(&pdev->dev, "gpe", &gpe_number);
	if (ret) {
		dev_err(&pdev->dev, "failed to read 'gpe' property: %d\n", ret);
		return ret;
	}

	lid = devm_kzalloc(&pdev->dev, sizeof(*lid), GFP_KERNEL);
	if (!lid)
		return -ENOMEM;

	lid->gpe_number = gpe_number;
	platform_set_drvdata(pdev, lid);

	status = acpi_mark_gpe_for_wake(NULL, gpe_number);
	if (ACPI_FAILURE(status)) {
		dev_err(&pdev->dev, "failed to mark GPE for wake: %s\n",
			acpi_format_exception(status));
		return -EINVAL;
	}

	status = acpi_enable_gpe(NULL, gpe_number);
	if (ACPI_FAILURE(status)) {
		dev_err(&pdev->dev, "failed to enable GPE: %s\n",
			acpi_format_exception(status));
		return -EINVAL;
	}

	ret = surface_lid_enable_wakeup(&pdev->dev, false);
	if (ret)
		acpi_disable_gpe(NULL, gpe_number);

	return ret;
}

static int surface_gpe_remove(struct platform_device *pdev)
{
	struct surface_lid_device *lid = dev_get_drvdata(&pdev->dev);

	/* restore default behavior without this module */
	surface_lid_enable_wakeup(&pdev->dev, false);
	acpi_disable_gpe(NULL, lid->gpe_number);

	return 0;
}

static struct platform_driver surface_gpe_driver = {
	.probe = surface_gpe_probe,
	.remove = surface_gpe_remove,
	.driver = {
		.name = "surface_gpe",
		.pm = &surface_gpe_pm,
		.probe_type = PROBE_PREFER_ASYNCHRONOUS,
	},
};

static struct platform_device *surface_gpe_device;

static int __init surface_gpe_init(void)
{
	const struct dmi_system_id *match;
	struct platform_device *pdev;
	struct fwnode_handle *fwnode;
	int status;

	match = dmi_first_match(dmi_lid_device_table);
	if (!match) {
		pr_info("no compatible Microsoft Surface device found, exiting\n");
		return -ENODEV;
	}

	status = platform_driver_register(&surface_gpe_driver);
	if (status)
		return status;

	fwnode = fwnode_create_software_node(match->driver_data, NULL);
	if (IS_ERR(fwnode)) {
		status = PTR_ERR(fwnode);
		goto err_node;
	}

	pdev = platform_device_alloc("surface_gpe", PLATFORM_DEVID_NONE);
	if (!pdev) {
		status = -ENOMEM;
		goto err_alloc;
	}

	pdev->dev.fwnode = fwnode;

	status = platform_device_add(pdev);
	if (status)
		goto err_add;

	surface_gpe_device = pdev;
	return 0;

err_add:
	platform_device_put(pdev);
err_alloc:
	fwnode_remove_software_node(fwnode);
err_node:
	platform_driver_unregister(&surface_gpe_driver);
	return status;
}
module_init(surface_gpe_init);

static void __exit surface_gpe_exit(void)
{
	struct fwnode_handle *fwnode = surface_gpe_device->dev.fwnode;

	platform_device_unregister(surface_gpe_device);
	platform_driver_unregister(&surface_gpe_driver);
	fwnode_remove_software_node(fwnode);
}
module_exit(surface_gpe_exit);

MODULE_AUTHOR("Maximilian Luz <luzmaximilian@gmail.com>");
MODULE_DESCRIPTION("Surface GPE/Lid Driver");
MODULE_LICENSE("GPL");
MODULE_ALIAS("dmi:*:svnMicrosoftCorporation:pnSurface*:*");