0003-buttons.patch 7.5 KB

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