surface_gpe.c 8.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354
  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.
  6. *
  7. * Copyright (C) 2020-2022 Maximilian Luz <[email protected]>
  8. */
  9. #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
  10. #include <linux/acpi.h>
  11. #include <linux/dmi.h>
  12. #include <linux/kernel.h>
  13. #include <linux/module.h>
  14. #include <linux/platform_device.h>
  15. /*
  16. * Note: The GPE numbers for the lid devices found below have been obtained
  17. * from ACPI/the DSDT table, specifically from the GPE handler for the
  18. * lid.
  19. */
  20. static const struct property_entry lid_device_props_l17[] = {
  21. PROPERTY_ENTRY_U32("gpe", 0x17),
  22. {},
  23. };
  24. static const struct property_entry lid_device_props_l4B[] = {
  25. PROPERTY_ENTRY_U32("gpe", 0x4B),
  26. {},
  27. };
  28. static const struct property_entry lid_device_props_l4D[] = {
  29. PROPERTY_ENTRY_U32("gpe", 0x4D),
  30. {},
  31. };
  32. static const struct property_entry lid_device_props_l4F[] = {
  33. PROPERTY_ENTRY_U32("gpe", 0x4F),
  34. {},
  35. };
  36. static const struct property_entry lid_device_props_l57[] = {
  37. PROPERTY_ENTRY_U32("gpe", 0x57),
  38. {},
  39. };
  40. /*
  41. * Note: When changing this, don't forget to check that the MODULE_ALIAS below
  42. * still fits.
  43. */
  44. static const struct dmi_system_id dmi_lid_device_table[] = {
  45. {
  46. .ident = "Surface Pro 4",
  47. .matches = {
  48. DMI_EXACT_MATCH(DMI_SYS_VENDOR, "Microsoft Corporation"),
  49. DMI_EXACT_MATCH(DMI_PRODUCT_NAME, "Surface Pro 4"),
  50. },
  51. .driver_data = (void *)lid_device_props_l17,
  52. },
  53. {
  54. .ident = "Surface Pro 5",
  55. .matches = {
  56. /*
  57. * We match for SKU here due to generic product name
  58. * "Surface Pro".
  59. */
  60. DMI_EXACT_MATCH(DMI_SYS_VENDOR, "Microsoft Corporation"),
  61. DMI_EXACT_MATCH(DMI_PRODUCT_SKU, "Surface_Pro_1796"),
  62. },
  63. .driver_data = (void *)lid_device_props_l4F,
  64. },
  65. {
  66. .ident = "Surface Pro 5 (LTE)",
  67. .matches = {
  68. /*
  69. * We match for SKU here due to generic product name
  70. * "Surface Pro"
  71. */
  72. DMI_EXACT_MATCH(DMI_SYS_VENDOR, "Microsoft Corporation"),
  73. DMI_EXACT_MATCH(DMI_PRODUCT_SKU, "Surface_Pro_1807"),
  74. },
  75. .driver_data = (void *)lid_device_props_l4F,
  76. },
  77. {
  78. .ident = "Surface Pro 6",
  79. .matches = {
  80. DMI_EXACT_MATCH(DMI_SYS_VENDOR, "Microsoft Corporation"),
  81. DMI_EXACT_MATCH(DMI_PRODUCT_NAME, "Surface Pro 6"),
  82. },
  83. .driver_data = (void *)lid_device_props_l4F,
  84. },
  85. {
  86. .ident = "Surface Pro 7",
  87. .matches = {
  88. DMI_EXACT_MATCH(DMI_SYS_VENDOR, "Microsoft Corporation"),
  89. DMI_EXACT_MATCH(DMI_PRODUCT_NAME, "Surface Pro 7"),
  90. },
  91. .driver_data = (void *)lid_device_props_l4D,
  92. },
  93. {
  94. .ident = "Surface Pro 8",
  95. .matches = {
  96. DMI_EXACT_MATCH(DMI_SYS_VENDOR, "Microsoft Corporation"),
  97. DMI_EXACT_MATCH(DMI_PRODUCT_NAME, "Surface Pro 8"),
  98. },
  99. .driver_data = (void *)lid_device_props_l4B,
  100. },
  101. {
  102. .ident = "Surface Book 1",
  103. .matches = {
  104. DMI_EXACT_MATCH(DMI_SYS_VENDOR, "Microsoft Corporation"),
  105. DMI_EXACT_MATCH(DMI_PRODUCT_NAME, "Surface Book"),
  106. },
  107. .driver_data = (void *)lid_device_props_l17,
  108. },
  109. {
  110. .ident = "Surface Book 2",
  111. .matches = {
  112. DMI_EXACT_MATCH(DMI_SYS_VENDOR, "Microsoft Corporation"),
  113. DMI_EXACT_MATCH(DMI_PRODUCT_NAME, "Surface Book 2"),
  114. },
  115. .driver_data = (void *)lid_device_props_l17,
  116. },
  117. {
  118. .ident = "Surface Book 3",
  119. .matches = {
  120. DMI_EXACT_MATCH(DMI_SYS_VENDOR, "Microsoft Corporation"),
  121. DMI_EXACT_MATCH(DMI_PRODUCT_NAME, "Surface Book 3"),
  122. },
  123. .driver_data = (void *)lid_device_props_l4D,
  124. },
  125. {
  126. .ident = "Surface Laptop 1",
  127. .matches = {
  128. DMI_EXACT_MATCH(DMI_SYS_VENDOR, "Microsoft Corporation"),
  129. DMI_EXACT_MATCH(DMI_PRODUCT_NAME, "Surface Laptop"),
  130. },
  131. .driver_data = (void *)lid_device_props_l57,
  132. },
  133. {
  134. .ident = "Surface Laptop 2",
  135. .matches = {
  136. DMI_EXACT_MATCH(DMI_SYS_VENDOR, "Microsoft Corporation"),
  137. DMI_EXACT_MATCH(DMI_PRODUCT_NAME, "Surface Laptop 2"),
  138. },
  139. .driver_data = (void *)lid_device_props_l57,
  140. },
  141. {
  142. .ident = "Surface Laptop 3 (Intel 13\")",
  143. .matches = {
  144. /*
  145. * We match for SKU here due to different variants: The
  146. * AMD (15") version does not rely on GPEs.
  147. */
  148. DMI_EXACT_MATCH(DMI_SYS_VENDOR, "Microsoft Corporation"),
  149. DMI_EXACT_MATCH(DMI_PRODUCT_SKU, "Surface_Laptop_3_1867:1868"),
  150. },
  151. .driver_data = (void *)lid_device_props_l4D,
  152. },
  153. {
  154. .ident = "Surface Laptop 3 (Intel 15\")",
  155. .matches = {
  156. /*
  157. * We match for SKU here due to different variants: The
  158. * AMD (15") version does not rely on GPEs.
  159. */
  160. DMI_EXACT_MATCH(DMI_SYS_VENDOR, "Microsoft Corporation"),
  161. DMI_EXACT_MATCH(DMI_PRODUCT_SKU, "Surface_Laptop_3_1872"),
  162. },
  163. .driver_data = (void *)lid_device_props_l4D,
  164. },
  165. {
  166. .ident = "Surface Laptop 4 (Intel 13\")",
  167. .matches = {
  168. /*
  169. * We match for SKU here due to different variants: The
  170. * AMD (15") version does not rely on GPEs.
  171. */
  172. DMI_EXACT_MATCH(DMI_SYS_VENDOR, "Microsoft Corporation"),
  173. DMI_EXACT_MATCH(DMI_PRODUCT_SKU, "Surface_Laptop_4_1950:1951"),
  174. },
  175. .driver_data = (void *)lid_device_props_l4B,
  176. },
  177. {
  178. .ident = "Surface Laptop Studio",
  179. .matches = {
  180. DMI_EXACT_MATCH(DMI_SYS_VENDOR, "Microsoft Corporation"),
  181. DMI_EXACT_MATCH(DMI_PRODUCT_NAME, "Surface Laptop Studio"),
  182. },
  183. .driver_data = (void *)lid_device_props_l4B,
  184. },
  185. { }
  186. };
  187. struct surface_lid_device {
  188. u32 gpe_number;
  189. };
  190. static int surface_lid_enable_wakeup(struct device *dev, bool enable)
  191. {
  192. const struct surface_lid_device *lid = dev_get_drvdata(dev);
  193. int action = enable ? ACPI_GPE_ENABLE : ACPI_GPE_DISABLE;
  194. acpi_status status;
  195. status = acpi_set_gpe_wake_mask(NULL, lid->gpe_number, action);
  196. if (ACPI_FAILURE(status)) {
  197. dev_err(dev, "failed to set GPE wake mask: %s\n",
  198. acpi_format_exception(status));
  199. return -EINVAL;
  200. }
  201. return 0;
  202. }
  203. static int __maybe_unused surface_gpe_suspend(struct device *dev)
  204. {
  205. return surface_lid_enable_wakeup(dev, true);
  206. }
  207. static int __maybe_unused surface_gpe_resume(struct device *dev)
  208. {
  209. return surface_lid_enable_wakeup(dev, false);
  210. }
  211. static SIMPLE_DEV_PM_OPS(surface_gpe_pm, surface_gpe_suspend, surface_gpe_resume);
  212. static int surface_gpe_probe(struct platform_device *pdev)
  213. {
  214. struct surface_lid_device *lid;
  215. u32 gpe_number;
  216. acpi_status status;
  217. int ret;
  218. ret = device_property_read_u32(&pdev->dev, "gpe", &gpe_number);
  219. if (ret) {
  220. dev_err(&pdev->dev, "failed to read 'gpe' property: %d\n", ret);
  221. return ret;
  222. }
  223. lid = devm_kzalloc(&pdev->dev, sizeof(*lid), GFP_KERNEL);
  224. if (!lid)
  225. return -ENOMEM;
  226. lid->gpe_number = gpe_number;
  227. platform_set_drvdata(pdev, lid);
  228. status = acpi_mark_gpe_for_wake(NULL, gpe_number);
  229. if (ACPI_FAILURE(status)) {
  230. dev_err(&pdev->dev, "failed to mark GPE for wake: %s\n",
  231. acpi_format_exception(status));
  232. return -EINVAL;
  233. }
  234. status = acpi_enable_gpe(NULL, gpe_number);
  235. if (ACPI_FAILURE(status)) {
  236. dev_err(&pdev->dev, "failed to enable GPE: %s\n",
  237. acpi_format_exception(status));
  238. return -EINVAL;
  239. }
  240. ret = surface_lid_enable_wakeup(&pdev->dev, false);
  241. if (ret)
  242. acpi_disable_gpe(NULL, gpe_number);
  243. return ret;
  244. }
  245. static int surface_gpe_remove(struct platform_device *pdev)
  246. {
  247. struct surface_lid_device *lid = dev_get_drvdata(&pdev->dev);
  248. /* restore default behavior without this module */
  249. surface_lid_enable_wakeup(&pdev->dev, false);
  250. acpi_disable_gpe(NULL, lid->gpe_number);
  251. return 0;
  252. }
  253. static struct platform_driver surface_gpe_driver = {
  254. .probe = surface_gpe_probe,
  255. .remove = surface_gpe_remove,
  256. .driver = {
  257. .name = "surface_gpe",
  258. .pm = &surface_gpe_pm,
  259. .probe_type = PROBE_PREFER_ASYNCHRONOUS,
  260. },
  261. };
  262. static struct platform_device *surface_gpe_device;
  263. static int __init surface_gpe_init(void)
  264. {
  265. const struct dmi_system_id *match;
  266. struct platform_device *pdev;
  267. struct fwnode_handle *fwnode;
  268. int status;
  269. match = dmi_first_match(dmi_lid_device_table);
  270. if (!match) {
  271. pr_info("no compatible Microsoft Surface device found, exiting\n");
  272. return -ENODEV;
  273. }
  274. status = platform_driver_register(&surface_gpe_driver);
  275. if (status)
  276. return status;
  277. fwnode = fwnode_create_software_node(match->driver_data, NULL);
  278. if (IS_ERR(fwnode)) {
  279. status = PTR_ERR(fwnode);
  280. goto err_node;
  281. }
  282. pdev = platform_device_alloc("surface_gpe", PLATFORM_DEVID_NONE);
  283. if (!pdev) {
  284. status = -ENOMEM;
  285. goto err_alloc;
  286. }
  287. pdev->dev.fwnode = fwnode;
  288. status = platform_device_add(pdev);
  289. if (status)
  290. goto err_add;
  291. surface_gpe_device = pdev;
  292. return 0;
  293. err_add:
  294. platform_device_put(pdev);
  295. err_alloc:
  296. fwnode_remove_software_node(fwnode);
  297. err_node:
  298. platform_driver_unregister(&surface_gpe_driver);
  299. return status;
  300. }
  301. module_init(surface_gpe_init);
  302. static void __exit surface_gpe_exit(void)
  303. {
  304. struct fwnode_handle *fwnode = surface_gpe_device->dev.fwnode;
  305. platform_device_unregister(surface_gpe_device);
  306. platform_driver_unregister(&surface_gpe_driver);
  307. fwnode_remove_software_node(fwnode);
  308. }
  309. module_exit(surface_gpe_exit);
  310. MODULE_AUTHOR("Maximilian Luz <[email protected]>");
  311. MODULE_DESCRIPTION("Surface GPE/Lid Driver");
  312. MODULE_LICENSE("GPL");
  313. MODULE_ALIAS("dmi:*:svnMicrosoftCorporation:pnSurface*:*");