surface3_button.c 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247
  1. // SPDX-License-Identifier: GPL-2.0-only
  2. /*
  3. * Supports for the button array on the Surface tablets.
  4. *
  5. * (C) Copyright 2016 Red Hat, Inc
  6. *
  7. * Based on soc_button_array.c:
  8. *
  9. * {C} Copyright 2014 Intel Corporation
  10. */
  11. #include <linux/module.h>
  12. #include <linux/input.h>
  13. #include <linux/init.h>
  14. #include <linux/kernel.h>
  15. #include <linux/i2c.h>
  16. #include <linux/slab.h>
  17. #include <linux/acpi.h>
  18. #include <linux/gpio/consumer.h>
  19. #include <linux/gpio_keys.h>
  20. #include <linux/gpio.h>
  21. #include <linux/platform_device.h>
  22. #define SURFACE_BUTTON_OBJ_NAME "TEV2"
  23. #define MAX_NBUTTONS 4
  24. /*
  25. * Some of the buttons like volume up/down are auto repeat, while others
  26. * are not. To support both, we register two platform devices, and put
  27. * buttons into them based on whether the key should be auto repeat.
  28. */
  29. #define BUTTON_TYPES 2
  30. /*
  31. * Power button, Home button, Volume buttons support is supposed to
  32. * be covered by drivers/input/misc/soc_button_array.c, which is implemented
  33. * according to "Windows ACPI Design Guide for SoC Platforms".
  34. * However surface 3 seems not to obey the specs, instead it uses
  35. * device TEV2(MSHW0028) for declaring the GPIOs. The gpios are also slightly
  36. * different in which the Home button is active high.
  37. * Compared to surfacepro3_button.c which also handles MSHW0028, the Surface 3
  38. * is a reduce platform and thus uses GPIOs, not ACPI events.
  39. * We choose an I2C driver here because we need to access the resources
  40. * declared under the device node, while surfacepro3_button.c only needs
  41. * the ACPI companion node.
  42. */
  43. static const struct acpi_device_id surface3_acpi_match[] = {
  44. { "MSHW0028", 0 },
  45. { }
  46. };
  47. MODULE_DEVICE_TABLE(acpi, surface3_acpi_match);
  48. struct surface3_button_info {
  49. const char *name;
  50. int acpi_index;
  51. unsigned int event_type;
  52. unsigned int event_code;
  53. bool autorepeat;
  54. bool wakeup;
  55. bool active_low;
  56. };
  57. struct surface3_button_data {
  58. struct platform_device *children[BUTTON_TYPES];
  59. };
  60. /*
  61. * Get the Nth GPIO number from the ACPI object.
  62. */
  63. static int surface3_button_lookup_gpio(struct device *dev, int acpi_index)
  64. {
  65. struct gpio_desc *desc;
  66. int gpio;
  67. desc = gpiod_get_index(dev, NULL, acpi_index, GPIOD_ASIS);
  68. if (IS_ERR(desc))
  69. return PTR_ERR(desc);
  70. gpio = desc_to_gpio(desc);
  71. gpiod_put(desc);
  72. return gpio;
  73. }
  74. static struct platform_device *
  75. surface3_button_device_create(struct i2c_client *client,
  76. const struct surface3_button_info *button_info,
  77. bool autorepeat)
  78. {
  79. const struct surface3_button_info *info;
  80. struct platform_device *pd;
  81. struct gpio_keys_button *gpio_keys;
  82. struct gpio_keys_platform_data *gpio_keys_pdata;
  83. int n_buttons = 0;
  84. int gpio;
  85. int error;
  86. gpio_keys_pdata = devm_kzalloc(&client->dev,
  87. sizeof(*gpio_keys_pdata) +
  88. sizeof(*gpio_keys) * MAX_NBUTTONS,
  89. GFP_KERNEL);
  90. if (!gpio_keys_pdata)
  91. return ERR_PTR(-ENOMEM);
  92. gpio_keys = (void *)(gpio_keys_pdata + 1);
  93. for (info = button_info; info->name; info++) {
  94. if (info->autorepeat != autorepeat)
  95. continue;
  96. gpio = surface3_button_lookup_gpio(&client->dev,
  97. info->acpi_index);
  98. if (!gpio_is_valid(gpio))
  99. continue;
  100. gpio_keys[n_buttons].type = info->event_type;
  101. gpio_keys[n_buttons].code = info->event_code;
  102. gpio_keys[n_buttons].gpio = gpio;
  103. gpio_keys[n_buttons].active_low = info->active_low;
  104. gpio_keys[n_buttons].desc = info->name;
  105. gpio_keys[n_buttons].wakeup = info->wakeup;
  106. n_buttons++;
  107. }
  108. if (n_buttons == 0) {
  109. error = -ENODEV;
  110. goto err_free_mem;
  111. }
  112. gpio_keys_pdata->buttons = gpio_keys;
  113. gpio_keys_pdata->nbuttons = n_buttons;
  114. gpio_keys_pdata->rep = autorepeat;
  115. pd = platform_device_alloc("gpio-keys", PLATFORM_DEVID_AUTO);
  116. if (!pd) {
  117. error = -ENOMEM;
  118. goto err_free_mem;
  119. }
  120. error = platform_device_add_data(pd, gpio_keys_pdata,
  121. sizeof(*gpio_keys_pdata));
  122. if (error)
  123. goto err_free_pdev;
  124. error = platform_device_add(pd);
  125. if (error)
  126. goto err_free_pdev;
  127. return pd;
  128. err_free_pdev:
  129. platform_device_put(pd);
  130. err_free_mem:
  131. devm_kfree(&client->dev, gpio_keys_pdata);
  132. return ERR_PTR(error);
  133. }
  134. static int surface3_button_remove(struct i2c_client *client)
  135. {
  136. struct surface3_button_data *priv = i2c_get_clientdata(client);
  137. int i;
  138. for (i = 0; i < BUTTON_TYPES; i++)
  139. if (priv->children[i])
  140. platform_device_unregister(priv->children[i]);
  141. return 0;
  142. }
  143. static struct surface3_button_info surface3_button_surface3[] = {
  144. { "power", 0, EV_KEY, KEY_POWER, false, true, true },
  145. { "home", 1, EV_KEY, KEY_LEFTMETA, false, true, false },
  146. { "volume_up", 2, EV_KEY, KEY_VOLUMEUP, true, false, true },
  147. { "volume_down", 3, EV_KEY, KEY_VOLUMEDOWN, true, false, true },
  148. { }
  149. };
  150. static int surface3_button_probe(struct i2c_client *client,
  151. const struct i2c_device_id *id)
  152. {
  153. struct device *dev = &client->dev;
  154. struct surface3_button_data *priv;
  155. struct platform_device *pd;
  156. int i;
  157. int error;
  158. if (strncmp(acpi_device_bid(ACPI_COMPANION(&client->dev)),
  159. SURFACE_BUTTON_OBJ_NAME,
  160. strlen(SURFACE_BUTTON_OBJ_NAME)))
  161. return -ENODEV;
  162. error = gpiod_count(dev, NULL);
  163. if (error < 0) {
  164. dev_dbg(dev, "no GPIO attached, ignoring...\n");
  165. return error;
  166. }
  167. priv = devm_kzalloc(dev, sizeof(*priv), GFP_KERNEL);
  168. if (!priv)
  169. return -ENOMEM;
  170. i2c_set_clientdata(client, priv);
  171. for (i = 0; i < BUTTON_TYPES; i++) {
  172. pd = surface3_button_device_create(client,
  173. surface3_button_surface3,
  174. i == 0);
  175. if (IS_ERR(pd)) {
  176. error = PTR_ERR(pd);
  177. if (error != -ENODEV) {
  178. surface3_button_remove(client);
  179. return error;
  180. }
  181. continue;
  182. }
  183. priv->children[i] = pd;
  184. }
  185. if (!priv->children[0] && !priv->children[1])
  186. return -ENODEV;
  187. return 0;
  188. }
  189. static const struct i2c_device_id surface3_id[] = {
  190. { }
  191. };
  192. MODULE_DEVICE_TABLE(i2c, surface3_id);
  193. static struct i2c_driver surface3_driver = {
  194. .probe = surface3_button_probe,
  195. .remove = surface3_button_remove,
  196. .id_table = surface3_id,
  197. .driver = {
  198. .name = "surface3",
  199. .acpi_match_table = ACPI_PTR(surface3_acpi_match),
  200. },
  201. };
  202. module_i2c_driver(surface3_driver);
  203. MODULE_AUTHOR("Benjamin Tissoires <benjamin.tissoires@gmail.com>");
  204. MODULE_DESCRIPTION("surface3 button array driver");
  205. MODULE_LICENSE("GPL v2");