0003-buttons.patch 7.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269
  1. From ba453a89148ea2289e71741225273a02ae2db768 Mon Sep 17 00:00:00 2001
  2. From: Maximilian Luz <luzmaximilian@gmail.com>
  3. Date: Tue, 2 Jul 2019 23:49:56 +0200
  4. Subject: [PATCH 03/12] buttons
  5. ---
  6. drivers/input/misc/soc_button_array.c | 145 ++++++++++++++++++++--
  7. drivers/platform/x86/surfacepro3_button.c | 38 ++++++
  8. 2 files changed, 171 insertions(+), 12 deletions(-)
  9. diff --git a/drivers/input/misc/soc_button_array.c b/drivers/input/misc/soc_button_array.c
  10. index 55cd6e0b409c..bd8a8601cf0c 100644
  11. --- a/drivers/input/misc/soc_button_array.c
  12. +++ b/drivers/input/misc/soc_button_array.c
  13. @@ -29,6 +29,17 @@ struct soc_button_info {
  14. bool wakeup;
  15. };
  16. +/**
  17. + * struct soc_device_data - driver data for different device types
  18. + * @button_info: specifications of buttons, if NULL specification is assumed to
  19. + * be present in _DSD
  20. + * @check: device-specific check (NULL means all will be accepted)
  21. + */
  22. +struct soc_device_data {
  23. + struct soc_button_info *button_info;
  24. + int (*check)(struct device *dev);
  25. +};
  26. +
  27. /*
  28. * Some of the buttons like volume up/down are auto repeat, while others
  29. * are not. To support both, we register two platform devices, and put
  30. @@ -310,6 +321,7 @@ static int soc_button_probe(struct platform_device *pdev)
  31. {
  32. struct device *dev = &pdev->dev;
  33. const struct acpi_device_id *id;
  34. + struct soc_device_data *device_data;
  35. struct soc_button_info *button_info;
  36. struct soc_button_data *priv;
  37. struct platform_device *pd;
  38. @@ -320,18 +332,20 @@ static int soc_button_probe(struct platform_device *pdev)
  39. if (!id)
  40. return -ENODEV;
  41. - if (!id->driver_data) {
  42. + device_data = (struct soc_device_data *)id->driver_data;
  43. + if (device_data && device_data->check) {
  44. + error = device_data->check(dev);
  45. + if (error)
  46. + return error;
  47. + }
  48. +
  49. + if (device_data && device_data->button_info) {
  50. + button_info = (struct soc_button_info *)
  51. + device_data->button_info;
  52. + } else {
  53. button_info = soc_button_get_button_info(dev);
  54. if (IS_ERR(button_info))
  55. return PTR_ERR(button_info);
  56. - } else {
  57. - button_info = (struct soc_button_info *)id->driver_data;
  58. - }
  59. -
  60. - error = gpiod_count(dev, NULL);
  61. - if (error < 0) {
  62. - dev_dbg(dev, "no GPIO attached, ignoring...\n");
  63. - return -ENODEV;
  64. }
  65. priv = devm_kzalloc(dev, sizeof(*priv), GFP_KERNEL);
  66. @@ -357,12 +371,32 @@ static int soc_button_probe(struct platform_device *pdev)
  67. if (!priv->children[0] && !priv->children[1])
  68. return -ENODEV;
  69. - if (!id->driver_data)
  70. + if (!device_data || !device_data->button_info)
  71. devm_kfree(dev, button_info);
  72. return 0;
  73. }
  74. +
  75. +static int soc_device_check_generic(struct device *dev)
  76. +{
  77. + int gpios;
  78. +
  79. + gpios = gpiod_count(dev, NULL);
  80. + if (gpios < 0) {
  81. + dev_dbg(dev, "no GPIO attached, ignoring...\n");
  82. + return -ENODEV;
  83. + }
  84. +
  85. + return 0;
  86. +}
  87. +
  88. +static struct soc_device_data soc_device_ACPI0011 = {
  89. + .button_info = NULL,
  90. + .check = soc_device_check_generic,
  91. +};
  92. +
  93. +
  94. /*
  95. * Definition of buttons on the tablet. The ACPI index of each button
  96. * is defined in section 2.8.7.2 of "Windows ACPI Design Guide for SoC
  97. @@ -377,9 +411,96 @@ static struct soc_button_info soc_button_PNP0C40[] = {
  98. { }
  99. };
  100. +static struct soc_device_data soc_device_PNP0C40 = {
  101. + .button_info = soc_button_PNP0C40,
  102. + .check = soc_device_check_generic,
  103. +};
  104. +
  105. +
  106. +/*
  107. + * Special device check for Surface Book 2 and Surface Pro (2017).
  108. + * Both, the Surface Pro 4 (surfacepro3_button.c) and the above mentioned
  109. + * devices use MSHW0040 for power and volume buttons, however the way they
  110. + * have to be addressed differs. Make sure that we only load this drivers
  111. + * for the correct devices by checking the OEM Platform Revision provided by
  112. + * the _DSM method.
  113. + */
  114. +#define MSHW0040_DSM_REVISION 0x01
  115. +#define MSHW0040_DSM_GET_OMPR 0x02 // get OEM Platform Revision
  116. +static const guid_t MSHW0040_DSM_UUID =
  117. + GUID_INIT(0x6fd05c69, 0xcde3, 0x49f4, 0x95, 0xed, 0xab, 0x16, 0x65,
  118. + 0x49, 0x80, 0x35);
  119. +
  120. +#ifdef CONFIG_ACPI
  121. +
  122. +static int soc_device_check_MSHW0040(struct device *dev)
  123. +{
  124. + acpi_handle handle = ACPI_HANDLE(dev);
  125. + union acpi_object *result;
  126. + u64 oem_platform_rev = 0;
  127. + int gpios;
  128. +
  129. + // get OEM platform revision
  130. + result = acpi_evaluate_dsm_typed(handle, &MSHW0040_DSM_UUID,
  131. + MSHW0040_DSM_REVISION,
  132. + MSHW0040_DSM_GET_OMPR, NULL,
  133. + ACPI_TYPE_INTEGER);
  134. +
  135. + if (result) {
  136. + oem_platform_rev = result->integer.value;
  137. + ACPI_FREE(result);
  138. + }
  139. +
  140. + if (oem_platform_rev == 0)
  141. + return -ENODEV;
  142. +
  143. + dev_dbg(dev, "OEM Platform Revision %llu\n", oem_platform_rev);
  144. +
  145. + /*
  146. + * We are _really_ expecting GPIOs here. If we do not get any, this
  147. + * means the GPIO driver has not been loaded yet (which can happen).
  148. + * Try again later.
  149. + */
  150. + gpios = gpiod_count(dev, NULL);
  151. + if (gpios < 0)
  152. + return -EAGAIN;
  153. +
  154. + return 0;
  155. +}
  156. +
  157. +#else /* CONFIG_ACPI */
  158. +
  159. +static int soc_device_check_MSHW0040(struct device *dev)
  160. +{
  161. + return -ENODEV;
  162. +}
  163. +
  164. +#endif /* CONFIG_ACPI */
  165. +
  166. +/*
  167. + * Button infos for Microsoft Surface Book 2 and Surface Pro (2017).
  168. + * Obtained from DSDT/testing.
  169. + */
  170. +static struct soc_button_info soc_button_MSHW0040[] = {
  171. + { "power", 0, EV_KEY, KEY_POWER, false, true },
  172. + { "volume_up", 2, EV_KEY, KEY_VOLUMEUP, true, false },
  173. + { "volume_down", 4, EV_KEY, KEY_VOLUMEDOWN, true, false },
  174. + { }
  175. +};
  176. +
  177. +static struct soc_device_data soc_device_MSHW0040 = {
  178. + .button_info = soc_button_MSHW0040,
  179. + .check = soc_device_check_MSHW0040,
  180. +};
  181. +
  182. +
  183. static const struct acpi_device_id soc_button_acpi_match[] = {
  184. - { "PNP0C40", (unsigned long)soc_button_PNP0C40 },
  185. - { "ACPI0011", 0 },
  186. + { "PNP0C40", (unsigned long)&soc_device_PNP0C40 },
  187. + { "ACPI0011", (unsigned long)&soc_device_ACPI0011 },
  188. +
  189. + /* Microsoft Surface Devices (5th and 6th generation) */
  190. + { "MSHW0040", (unsigned long)&soc_device_MSHW0040 },
  191. +
  192. { }
  193. };
  194. diff --git a/drivers/platform/x86/surfacepro3_button.c b/drivers/platform/x86/surfacepro3_button.c
  195. index 1b491690ce07..eaec30380b11 100644
  196. --- a/drivers/platform/x86/surfacepro3_button.c
  197. +++ b/drivers/platform/x86/surfacepro3_button.c
  198. @@ -24,6 +24,12 @@
  199. #define SURFACE_BUTTON_OBJ_NAME "VGBI"
  200. #define SURFACE_BUTTON_DEVICE_NAME "Surface Pro 3/4 Buttons"
  201. +#define MSHW0040_DSM_REVISION 0x01
  202. +#define MSHW0040_DSM_GET_OMPR 0x02 // get OEM Platform Revision
  203. +static const guid_t MSHW0040_DSM_UUID =
  204. + GUID_INIT(0x6fd05c69, 0xcde3, 0x49f4, 0x95, 0xed, 0xab, 0x16, 0x65,
  205. + 0x49, 0x80, 0x35);
  206. +
  207. #define SURFACE_BUTTON_NOTIFY_TABLET_MODE 0xc8
  208. #define SURFACE_BUTTON_NOTIFY_PRESS_POWER 0xc6
  209. @@ -146,6 +152,34 @@ static int surface_button_resume(struct device *dev)
  210. }
  211. #endif
  212. +/*
  213. + * Surface Pro 4 and Surface Book 2 / Surface Pro 2017 use the same device
  214. + * ID (MSHW0040) for the power/volume buttons. Make sure this is the right
  215. + * device by checking for the _DSM method and OEM Platform Revision.
  216. + */
  217. +static int surface_button_check_MSHW0040(struct acpi_device *dev)
  218. +{
  219. + acpi_handle handle = dev->handle;
  220. + union acpi_object *result;
  221. + u64 oem_platform_rev = 0;
  222. +
  223. + // get OEM platform revision
  224. + result = acpi_evaluate_dsm_typed(handle, &MSHW0040_DSM_UUID,
  225. + MSHW0040_DSM_REVISION,
  226. + MSHW0040_DSM_GET_OMPR,
  227. + NULL, ACPI_TYPE_INTEGER);
  228. +
  229. + if (result) {
  230. + oem_platform_rev = result->integer.value;
  231. + ACPI_FREE(result);
  232. + }
  233. +
  234. + dev_dbg(&dev->dev, "OEM Platform Revision %llu\n", oem_platform_rev);
  235. +
  236. + return oem_platform_rev == 0 ? 0 : -ENODEV;
  237. +}
  238. +
  239. +
  240. static int surface_button_add(struct acpi_device *device)
  241. {
  242. struct surface_button *button;
  243. @@ -158,6 +192,10 @@ static int surface_button_add(struct acpi_device *device)
  244. strlen(SURFACE_BUTTON_OBJ_NAME)))
  245. return -ENODEV;
  246. + error = surface_button_check_MSHW0040(device);
  247. + if (error)
  248. + return error;
  249. +
  250. button = kzalloc(sizeof(struct surface_button), GFP_KERNEL);
  251. if (!button)
  252. return -ENOMEM;
  253. --
  254. 2.22.0