soc_button_array.c 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219
  1. /*
  2. * Supports for the button array on SoC tablets originally running
  3. * Windows 8.
  4. *
  5. * (C) Copyright 2014 Intel Corporation
  6. *
  7. * This program is free software; you can redistribute it and/or
  8. * modify it under the terms of the GNU General Public License
  9. * as published by the Free Software Foundation; version 2
  10. * of the License.
  11. */
  12. #include <linux/module.h>
  13. #include <linux/input.h>
  14. #include <linux/init.h>
  15. #include <linux/kernel.h>
  16. #include <linux/acpi.h>
  17. #include <linux/gpio/consumer.h>
  18. #include <linux/gpio_keys.h>
  19. #include <linux/input.h>
  20. #include <linux/platform_device.h>
  21. #include <linux/pnp.h>
  22. /*
  23. * Definition of buttons on the tablet. The ACPI index of each button
  24. * is defined in section 2.8.7.2 of "Windows ACPI Design Guide for SoC
  25. * Platforms"
  26. */
  27. #define MAX_NBUTTONS 5
  28. struct soc_button_info {
  29. const char *name;
  30. int acpi_index;
  31. unsigned int event_type;
  32. unsigned int event_code;
  33. bool autorepeat;
  34. bool wakeup;
  35. };
  36. /*
  37. * Some of the buttons like volume up/down are auto repeat, while others
  38. * are not. To support both, we register two platform devices, and put
  39. * buttons into them based on whether the key should be auto repeat.
  40. */
  41. #define BUTTON_TYPES 2
  42. struct soc_button_data {
  43. struct platform_device *children[BUTTON_TYPES];
  44. };
  45. /*
  46. * Get the Nth GPIO number from the ACPI object.
  47. */
  48. static int soc_button_lookup_gpio(struct device *dev, int acpi_index)
  49. {
  50. struct gpio_desc *desc;
  51. int gpio;
  52. desc = gpiod_get_index(dev, KBUILD_MODNAME, acpi_index);
  53. if (IS_ERR(desc))
  54. return PTR_ERR(desc);
  55. gpio = desc_to_gpio(desc);
  56. gpiod_put(desc);
  57. return gpio;
  58. }
  59. static struct platform_device *
  60. soc_button_device_create(struct pnp_dev *pdev,
  61. const struct soc_button_info *button_info,
  62. bool autorepeat)
  63. {
  64. const struct soc_button_info *info;
  65. struct platform_device *pd;
  66. struct gpio_keys_button *gpio_keys;
  67. struct gpio_keys_platform_data *gpio_keys_pdata;
  68. int n_buttons = 0;
  69. int gpio;
  70. int error;
  71. gpio_keys_pdata = devm_kzalloc(&pdev->dev,
  72. sizeof(*gpio_keys_pdata) +
  73. sizeof(*gpio_keys) * MAX_NBUTTONS,
  74. GFP_KERNEL);
  75. gpio_keys = (void *)(gpio_keys_pdata + 1);
  76. for (info = button_info; info->name; info++) {
  77. if (info->autorepeat != autorepeat)
  78. continue;
  79. gpio = soc_button_lookup_gpio(&pdev->dev, info->acpi_index);
  80. if (gpio < 0)
  81. continue;
  82. gpio_keys[n_buttons].type = info->event_type;
  83. gpio_keys[n_buttons].code = info->event_code;
  84. gpio_keys[n_buttons].gpio = gpio;
  85. gpio_keys[n_buttons].active_low = 1;
  86. gpio_keys[n_buttons].desc = info->name;
  87. gpio_keys[n_buttons].wakeup = info->wakeup;
  88. n_buttons++;
  89. }
  90. if (n_buttons == 0) {
  91. error = -ENODEV;
  92. goto err_free_mem;
  93. }
  94. gpio_keys_pdata->buttons = gpio_keys;
  95. gpio_keys_pdata->nbuttons = n_buttons;
  96. gpio_keys_pdata->rep = autorepeat;
  97. pd = platform_device_alloc("gpio-keys", PLATFORM_DEVID_AUTO);
  98. if (!pd) {
  99. error = -ENOMEM;
  100. goto err_free_mem;
  101. }
  102. error = platform_device_add_data(pd, gpio_keys_pdata,
  103. sizeof(*gpio_keys_pdata));
  104. if (error)
  105. goto err_free_pdev;
  106. error = platform_device_add(pd);
  107. if (error)
  108. goto err_free_pdev;
  109. return pd;
  110. err_free_pdev:
  111. platform_device_put(pd);
  112. err_free_mem:
  113. devm_kfree(&pdev->dev, gpio_keys_pdata);
  114. return ERR_PTR(error);
  115. }
  116. static void soc_button_remove(struct pnp_dev *pdev)
  117. {
  118. struct soc_button_data *priv = pnp_get_drvdata(pdev);
  119. int i;
  120. for (i = 0; i < BUTTON_TYPES; i++)
  121. if (priv->children[i])
  122. platform_device_unregister(priv->children[i]);
  123. }
  124. static int soc_button_pnp_probe(struct pnp_dev *pdev,
  125. const struct pnp_device_id *id)
  126. {
  127. const struct soc_button_info *button_info = (void *)id->driver_data;
  128. struct soc_button_data *priv;
  129. struct platform_device *pd;
  130. int i;
  131. int error;
  132. priv = devm_kzalloc(&pdev->dev, sizeof(*priv), GFP_KERNEL);
  133. if (!priv)
  134. return -ENOMEM;
  135. pnp_set_drvdata(pdev, priv);
  136. for (i = 0; i < BUTTON_TYPES; i++) {
  137. pd = soc_button_device_create(pdev, button_info, i == 0);
  138. if (IS_ERR(pd)) {
  139. error = PTR_ERR(pd);
  140. if (error != -ENODEV) {
  141. soc_button_remove(pdev);
  142. return error;
  143. }
  144. continue;
  145. }
  146. priv->children[i] = pd;
  147. }
  148. if (!priv->children[0] && !priv->children[1])
  149. return -ENODEV;
  150. return 0;
  151. }
  152. static struct soc_button_info soc_button_PNP0C40[] = {
  153. { "power", 0, EV_KEY, KEY_POWER, false, true },
  154. { "home", 1, EV_KEY, KEY_HOME, false, true },
  155. { "volume_up", 2, EV_KEY, KEY_VOLUMEUP, true, false },
  156. { "volume_down", 3, EV_KEY, KEY_VOLUMEDOWN, true, false },
  157. { "rotation_lock", 4, EV_SW, SW_ROTATE_LOCK, false, false },
  158. { }
  159. };
  160. static const struct pnp_device_id soc_button_pnp_match[] = {
  161. { .id = "PNP0C40", .driver_data = (long)soc_button_PNP0C40 },
  162. { .id = "" }
  163. };
  164. MODULE_DEVICE_TABLE(pnp, soc_button_pnp_match);
  165. static struct pnp_driver soc_button_pnp_driver = {
  166. .name = KBUILD_MODNAME,
  167. .id_table = soc_button_pnp_match,
  168. .probe = soc_button_pnp_probe,
  169. .remove = soc_button_remove,
  170. };
  171. static int __init soc_button_init(void)
  172. {
  173. return pnp_register_driver(&soc_button_pnp_driver);
  174. }
  175. static void __exit soc_button_exit(void)
  176. {
  177. pnp_unregister_driver(&soc_button_pnp_driver);
  178. }
  179. module_init(soc_button_init);
  180. module_exit(soc_button_exit);
  181. MODULE_LICENSE("GPL");