0008-surface-gpe.patch 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394
  1. From 92deae5f20757e35b3ce78a80845649bb880d2cf Mon Sep 17 00:00:00 2001
  2. From: Maximilian Luz <luzmaximilian@gmail.com>
  3. Date: Sun, 16 Aug 2020 23:39:56 +0200
  4. Subject: [PATCH] platform/x86: Add Driver to set up lid GPEs on MS Surface
  5. device
  6. Conventionally, wake-up events for a specific device, in our case the
  7. lid device, are managed via the ACPI _PRW field. While this does not
  8. seem strictly necessary based on ACPI spec, the kernel disables GPE
  9. wakeups to avoid non-wakeup interrupts preventing suspend by default and
  10. only enables GPEs associated via the _PRW field with a wake-up capable
  11. device. This behavior has been introduced in commit
  12. f941d3e41da7f86bdb9dcc1977c2bcc6b89bfe47
  13. ACPI: EC / PM: Disable non-wakeup GPEs for suspend-to-idle
  14. and is described in more detail in its commit message.
  15. Unfortunately, on MS Surface devices, there is no _PRW field present on
  16. the lid device, thus no GPE is associated with it, and therefore the GPE
  17. responsible for sending the status-change notification to the lid gets
  18. disabled during suspend, making it impossible to wake the device via the
  19. lid.
  20. This patch introduces a pseudo-device and respective driver which, based
  21. on some DMI matching, mark the corresponding GPE of the lid device for
  22. wake and enable it during suspend. The behavior of this driver models
  23. the behavior of the ACPI/PM core for normal wakeup GPEs, properly
  24. declared via the _PRW field.
  25. Signed-off-by: Maximilian Luz <luzmaximilian@gmail.com>
  26. Patchset: surface-gpe
  27. ---
  28. drivers/platform/x86/Kconfig | 10 +
  29. drivers/platform/x86/Makefile | 1 +
  30. drivers/platform/x86/surface_gpe.c | 313 +++++++++++++++++++++++++++++
  31. 3 files changed, 324 insertions(+)
  32. create mode 100644 drivers/platform/x86/surface_gpe.c
  33. diff --git a/drivers/platform/x86/Kconfig b/drivers/platform/x86/Kconfig
  34. index 0d20ffdb5a67..4321ec171bcd 100644
  35. --- a/drivers/platform/x86/Kconfig
  36. +++ b/drivers/platform/x86/Kconfig
  37. @@ -1168,6 +1168,16 @@ config SURFACE_3_POWER_OPREGION
  38. Select this option to enable support for ACPI operation
  39. region of the Surface 3 battery platform driver.
  40. +config SURFACE_GPE
  41. + tristate "Surface GPE/Lid Support Driver"
  42. + depends on ACPI
  43. + depends on DMI
  44. + help
  45. + This driver marks the GPEs related to the ACPI lid device found on
  46. + Microsoft Surface devices as wakeup sources and prepares them
  47. + accordingly. It is required on those devices to allow wake-ups from
  48. + suspend by opening the lid.
  49. +
  50. config INTEL_PUNIT_IPC
  51. tristate "Intel P-Unit IPC Driver"
  52. ---help---
  53. diff --git a/drivers/platform/x86/Makefile b/drivers/platform/x86/Makefile
  54. index 2ea90039a3e4..49238e9d4abf 100644
  55. --- a/drivers/platform/x86/Makefile
  56. +++ b/drivers/platform/x86/Makefile
  57. @@ -82,6 +82,7 @@ obj-$(CONFIG_TOUCHSCREEN_DMI) += touchscreen_dmi.o
  58. obj-$(CONFIG_SURFACE_PRO3_BUTTON) += surfacepro3_button.o
  59. obj-$(CONFIG_SURFACE_3_BUTTON) += surface3_button.o
  60. obj-$(CONFIG_SURFACE_3_POWER_OPREGION) += surface3_power.o
  61. +obj-$(CONFIG_SURFACE_GPE) += surface_gpe.o
  62. obj-$(CONFIG_INTEL_PUNIT_IPC) += intel_punit_ipc.o
  63. obj-$(CONFIG_INTEL_BXTWC_PMIC_TMU) += intel_bxtwc_tmu.o
  64. obj-$(CONFIG_INTEL_TELEMETRY) += intel_telemetry_core.o \
  65. diff --git a/drivers/platform/x86/surface_gpe.c b/drivers/platform/x86/surface_gpe.c
  66. new file mode 100644
  67. index 000000000000..7eaaeacbf408
  68. --- /dev/null
  69. +++ b/drivers/platform/x86/surface_gpe.c
  70. @@ -0,0 +1,313 @@
  71. +// SPDX-License-Identifier: GPL-2.0-or-later
  72. +/*
  73. + * Surface GPE/Lid driver to enable wakeup from suspend via the lid by
  74. + * properly configuring the respective GPEs. Required for wakeup via lid on
  75. + * newer Intel-based Microsoft Surface devices.
  76. + *
  77. + * Copyright (C) 2020 Maximilian Luz <luzmaximilian@gmail.com>
  78. + */
  79. +
  80. +#include <linux/acpi.h>
  81. +#include <linux/dmi.h>
  82. +#include <linux/kernel.h>
  83. +#include <linux/module.h>
  84. +#include <linux/platform_device.h>
  85. +
  86. +
  87. +struct surface_lid_device {
  88. + u32 gpe_number;
  89. +};
  90. +
  91. +static const struct surface_lid_device lid_device_l17 = {
  92. + .gpe_number = 0x17,
  93. +};
  94. +
  95. +static const struct surface_lid_device lid_device_l4D = {
  96. + .gpe_number = 0x4D,
  97. +};
  98. +
  99. +static const struct surface_lid_device lid_device_l4F = {
  100. + .gpe_number = 0x4F,
  101. +};
  102. +
  103. +static const struct surface_lid_device lid_device_l57 = {
  104. + .gpe_number = 0x57,
  105. +};
  106. +
  107. +
  108. +// Note: When changing this don't forget to change the MODULE_ALIAS below.
  109. +static const struct dmi_system_id dmi_lid_device_table[] = {
  110. + {
  111. + .ident = "Surface Pro 4",
  112. + .matches = {
  113. + DMI_EXACT_MATCH(DMI_SYS_VENDOR, "Microsoft Corporation"),
  114. + DMI_EXACT_MATCH(DMI_PRODUCT_NAME, "Surface Pro 4"),
  115. + },
  116. + .driver_data = (void *)&lid_device_l17,
  117. + },
  118. + {
  119. + .ident = "Surface Pro 5",
  120. + .matches = {
  121. + /*
  122. + * We match for SKU here due to generic product name
  123. + * "Surface Pro".
  124. + */
  125. + DMI_EXACT_MATCH(DMI_SYS_VENDOR, "Microsoft Corporation"),
  126. + DMI_EXACT_MATCH(DMI_PRODUCT_SKU, "Surface_Pro_1796"),
  127. + },
  128. + .driver_data = (void *)&lid_device_l4F,
  129. + },
  130. + {
  131. + .ident = "Surface Pro 5 (LTE)",
  132. + .matches = {
  133. + /*
  134. + * We match for SKU here due to generic product name
  135. + * "Surface Pro"
  136. + */
  137. + DMI_EXACT_MATCH(DMI_SYS_VENDOR, "Microsoft Corporation"),
  138. + DMI_EXACT_MATCH(DMI_PRODUCT_SKU, "Surface_Pro_1807"),
  139. + },
  140. + .driver_data = (void *)&lid_device_l4F,
  141. + },
  142. + {
  143. + .ident = "Surface Pro 6",
  144. + .matches = {
  145. + DMI_EXACT_MATCH(DMI_SYS_VENDOR, "Microsoft Corporation"),
  146. + DMI_EXACT_MATCH(DMI_PRODUCT_NAME, "Surface Pro 6"),
  147. + },
  148. + .driver_data = (void *)&lid_device_l4F,
  149. + },
  150. + {
  151. + .ident = "Surface Pro 7",
  152. + .matches = {
  153. + DMI_EXACT_MATCH(DMI_SYS_VENDOR, "Microsoft Corporation"),
  154. + DMI_EXACT_MATCH(DMI_PRODUCT_NAME, "Surface Pro 7"),
  155. + },
  156. + .driver_data = (void *)&lid_device_l4D,
  157. + },
  158. + {
  159. + .ident = "Surface Book 1",
  160. + .matches = {
  161. + DMI_EXACT_MATCH(DMI_SYS_VENDOR, "Microsoft Corporation"),
  162. + DMI_EXACT_MATCH(DMI_PRODUCT_NAME, "Surface Book"),
  163. + },
  164. + .driver_data = (void *)&lid_device_l17,
  165. + },
  166. + {
  167. + .ident = "Surface Book 2",
  168. + .matches = {
  169. + DMI_EXACT_MATCH(DMI_SYS_VENDOR, "Microsoft Corporation"),
  170. + DMI_EXACT_MATCH(DMI_PRODUCT_NAME, "Surface Book 2"),
  171. + },
  172. + .driver_data = (void *)&lid_device_l17,
  173. + },
  174. + {
  175. + .ident = "Surface Book 3",
  176. + .matches = {
  177. + DMI_EXACT_MATCH(DMI_SYS_VENDOR, "Microsoft Corporation"),
  178. + DMI_EXACT_MATCH(DMI_PRODUCT_NAME, "Surface Book 3"),
  179. + },
  180. + .driver_data = (void *)&lid_device_l4D,
  181. + },
  182. + {
  183. + .ident = "Surface Laptop 1",
  184. + .matches = {
  185. + DMI_EXACT_MATCH(DMI_SYS_VENDOR, "Microsoft Corporation"),
  186. + DMI_EXACT_MATCH(DMI_PRODUCT_NAME, "Surface Laptop"),
  187. + },
  188. + .driver_data = (void *)&lid_device_l57,
  189. + },
  190. + {
  191. + .ident = "Surface Laptop 2",
  192. + .matches = {
  193. + DMI_EXACT_MATCH(DMI_SYS_VENDOR, "Microsoft Corporation"),
  194. + DMI_EXACT_MATCH(DMI_PRODUCT_NAME, "Surface Laptop 2"),
  195. + },
  196. + .driver_data = (void *)&lid_device_l57,
  197. + },
  198. + {
  199. + .ident = "Surface Laptop 3 (Intel 13\")",
  200. + .matches = {
  201. + /*
  202. + * We match for SKU here due to different variants: The
  203. + * AMD (15") version does not rely on GPEs.
  204. + */
  205. + DMI_EXACT_MATCH(DMI_SYS_VENDOR, "Microsoft Corporation"),
  206. + DMI_EXACT_MATCH(DMI_PRODUCT_SKU, "Surface_Laptop_3_1867:1868"),
  207. + },
  208. + .driver_data = (void *)&lid_device_l4D,
  209. + },
  210. + {
  211. + .ident = "Surface Laptop 3 (Intel 15\")",
  212. + .matches = {
  213. + /*
  214. + * We match for SKU here due to different variants: The
  215. + * AMD (15") version does not rely on GPEs.
  216. + */
  217. + DMI_EXACT_MATCH(DMI_SYS_VENDOR, "Microsoft Corporation"),
  218. + DMI_EXACT_MATCH(DMI_PRODUCT_SKU, "Surface_Laptop_3_1872"),
  219. + },
  220. + .driver_data = (void *)&lid_device_l4D,
  221. + },
  222. + { }
  223. +};
  224. +
  225. +
  226. +static int surface_lid_enable_wakeup(struct device *dev,
  227. + const struct surface_lid_device *lid,
  228. + bool enable)
  229. +{
  230. + int action = enable ? ACPI_GPE_ENABLE : ACPI_GPE_DISABLE;
  231. + acpi_status status;
  232. +
  233. + status = acpi_set_gpe_wake_mask(NULL, lid->gpe_number, action);
  234. + if (status) {
  235. + dev_err(dev, "failed to set GPE wake mask: %d\n", status);
  236. + return -EINVAL;
  237. + }
  238. +
  239. + return 0;
  240. +}
  241. +
  242. +static int __maybe_unused surface_gpe_suspend(struct device *dev)
  243. +{
  244. + const struct surface_lid_device *lid;
  245. +
  246. + lid = dev_get_platdata(dev);
  247. + return surface_lid_enable_wakeup(dev, lid, true);
  248. +}
  249. +
  250. +static int __maybe_unused surface_gpe_resume(struct device *dev)
  251. +{
  252. + const struct surface_lid_device *lid;
  253. +
  254. + lid = dev_get_platdata(dev);
  255. + return surface_lid_enable_wakeup(dev, lid, false);
  256. +}
  257. +
  258. +static SIMPLE_DEV_PM_OPS(surface_gpe_pm, surface_gpe_suspend, surface_gpe_resume);
  259. +
  260. +
  261. +static int surface_gpe_probe(struct platform_device *pdev)
  262. +{
  263. + const struct surface_lid_device *lid;
  264. + int status;
  265. +
  266. + lid = dev_get_platdata(&pdev->dev);
  267. + if (!lid)
  268. + return -ENODEV;
  269. +
  270. + status = acpi_mark_gpe_for_wake(NULL, lid->gpe_number);
  271. + if (status) {
  272. + dev_err(&pdev->dev, "failed to mark GPE for wake: %d\n", status);
  273. + return -EINVAL;
  274. + }
  275. +
  276. + status = acpi_enable_gpe(NULL, lid->gpe_number);
  277. + if (status) {
  278. + dev_err(&pdev->dev, "failed to enable GPE: %d\n", status);
  279. + return -EINVAL;
  280. + }
  281. +
  282. + status = surface_lid_enable_wakeup(&pdev->dev, lid, false);
  283. + if (status) {
  284. + acpi_disable_gpe(NULL, lid->gpe_number);
  285. + return status;
  286. + }
  287. +
  288. + return 0;
  289. +}
  290. +
  291. +static int surface_gpe_remove(struct platform_device *pdev)
  292. +{
  293. + struct surface_lid_device *lid = dev_get_platdata(&pdev->dev);
  294. +
  295. + /* restore default behavior without this module */
  296. + surface_lid_enable_wakeup(&pdev->dev, lid, false);
  297. + acpi_disable_gpe(NULL, lid->gpe_number);
  298. +
  299. + return 0;
  300. +}
  301. +
  302. +static struct platform_driver surface_gpe_driver = {
  303. + .probe = surface_gpe_probe,
  304. + .remove = surface_gpe_remove,
  305. + .driver = {
  306. + .name = "surface_gpe",
  307. + .pm = &surface_gpe_pm,
  308. + .probe_type = PROBE_PREFER_ASYNCHRONOUS,
  309. + },
  310. +};
  311. +
  312. +
  313. +static struct platform_device *surface_gpe_device;
  314. +
  315. +static int __init surface_gpe_init(void)
  316. +{
  317. + const struct dmi_system_id *match;
  318. + const struct surface_lid_device *lid;
  319. + struct platform_device *pdev;
  320. + int status;
  321. +
  322. + match = dmi_first_match(dmi_lid_device_table);
  323. + if (!match) {
  324. + pr_info(KBUILD_MODNAME": no device detected, exiting\n");
  325. + return 0;
  326. + }
  327. +
  328. + lid = match->driver_data;
  329. +
  330. + status = platform_driver_register(&surface_gpe_driver);
  331. + if (status)
  332. + return status;
  333. +
  334. + pdev = platform_device_alloc("surface_gpe", PLATFORM_DEVID_NONE);
  335. + if (!pdev) {
  336. + platform_driver_unregister(&surface_gpe_driver);
  337. + return -ENOMEM;
  338. + }
  339. +
  340. + status = platform_device_add_data(pdev, lid, sizeof(*lid));
  341. + if (status) {
  342. + platform_device_put(pdev);
  343. + platform_driver_unregister(&surface_gpe_driver);
  344. + return status;
  345. + }
  346. +
  347. + status = platform_device_add(pdev);
  348. + if (status) {
  349. + platform_device_put(pdev);
  350. + platform_driver_unregister(&surface_gpe_driver);
  351. + return status;
  352. + }
  353. +
  354. + surface_gpe_device = pdev;
  355. + return 0;
  356. +}
  357. +
  358. +static void __exit surface_gpe_exit(void)
  359. +{
  360. + if (!surface_gpe_device)
  361. + return;
  362. +
  363. + platform_device_unregister(surface_gpe_device);
  364. + platform_driver_unregister(&surface_gpe_driver);
  365. +}
  366. +
  367. +module_init(surface_gpe_init);
  368. +module_exit(surface_gpe_exit);
  369. +
  370. +MODULE_AUTHOR("Maximilian Luz <luzmaximilian@gmail.com>");
  371. +MODULE_DESCRIPTION("Surface GPE/Lid Driver");
  372. +MODULE_LICENSE("GPL");
  373. +
  374. +MODULE_ALIAS("dmi:*:svnMicrosoftCorporation:pnSurfacePro:*");
  375. +MODULE_ALIAS("dmi:*:svnMicrosoftCorporation:pnSurfacePro4:*");
  376. +MODULE_ALIAS("dmi:*:svnMicrosoftCorporation:pnSurfacePro6:*");
  377. +MODULE_ALIAS("dmi:*:svnMicrosoftCorporation:pnSurfacePro7:*");
  378. +MODULE_ALIAS("dmi:*:svnMicrosoftCorporation:pnSurfaceBook:*");
  379. +MODULE_ALIAS("dmi:*:svnMicrosoftCorporation:pnSurfaceBook2:*");
  380. +MODULE_ALIAS("dmi:*:svnMicrosoftCorporation:pnSurfaceBook3:*");
  381. +MODULE_ALIAS("dmi:*:svnMicrosoftCorporation:pnSurfaceLaptop:*");
  382. +MODULE_ALIAS("dmi:*:svnMicrosoftCorporation:pnSurfaceLaptop2:*");
  383. +MODULE_ALIAS("dmi:*:svnMicrosoftCorporation:pnSurfaceLaptop3:*");
  384. --
  385. 2.33.0