surface3_button.c 6.2 KB

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