soc_button_array.c 5.0 KB

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