soc_button_array.c 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222
  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/acpi.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, GPIOD_ASIS);
  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 platform_device *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. if (!gpio_keys_pdata)
  75. return ERR_PTR(-ENOMEM);
  76. gpio_keys = (void *)(gpio_keys_pdata + 1);
  77. for (info = button_info; info->name; info++) {
  78. if (info->autorepeat != autorepeat)
  79. continue;
  80. gpio = soc_button_lookup_gpio(&pdev->dev, info->acpi_index);
  81. if (gpio < 0)
  82. continue;
  83. gpio_keys[n_buttons].type = info->event_type;
  84. gpio_keys[n_buttons].code = info->event_code;
  85. gpio_keys[n_buttons].gpio = gpio;
  86. gpio_keys[n_buttons].active_low = 1;
  87. gpio_keys[n_buttons].desc = info->name;
  88. gpio_keys[n_buttons].wakeup = info->wakeup;
  89. n_buttons++;
  90. }
  91. if (n_buttons == 0) {
  92. error = -ENODEV;
  93. goto err_free_mem;
  94. }
  95. gpio_keys_pdata->buttons = gpio_keys;
  96. gpio_keys_pdata->nbuttons = n_buttons;
  97. gpio_keys_pdata->rep = autorepeat;
  98. pd = platform_device_alloc("gpio-keys", PLATFORM_DEVID_AUTO);
  99. if (!pd) {
  100. error = -ENOMEM;
  101. goto err_free_mem;
  102. }
  103. error = platform_device_add_data(pd, gpio_keys_pdata,
  104. sizeof(*gpio_keys_pdata));
  105. if (error)
  106. goto err_free_pdev;
  107. error = platform_device_add(pd);
  108. if (error)
  109. goto err_free_pdev;
  110. return pd;
  111. err_free_pdev:
  112. platform_device_put(pd);
  113. err_free_mem:
  114. devm_kfree(&pdev->dev, gpio_keys_pdata);
  115. return ERR_PTR(error);
  116. }
  117. static int soc_button_remove(struct platform_device *pdev)
  118. {
  119. struct soc_button_data *priv = platform_get_drvdata(pdev);
  120. int i;
  121. for (i = 0; i < BUTTON_TYPES; i++)
  122. if (priv->children[i])
  123. platform_device_unregister(priv->children[i]);
  124. return 0;
  125. }
  126. static int soc_button_probe(struct platform_device *pdev)
  127. {
  128. struct device *dev = &pdev->dev;
  129. const struct acpi_device_id *id;
  130. struct soc_button_info *button_info;
  131. struct soc_button_data *priv;
  132. struct platform_device *pd;
  133. int i;
  134. int error;
  135. id = acpi_match_device(dev->driver->acpi_match_table, dev);
  136. if (!id)
  137. return -ENODEV;
  138. button_info = (struct soc_button_info *)id->driver_data;
  139. priv = devm_kzalloc(&pdev->dev, sizeof(*priv), GFP_KERNEL);
  140. if (!priv)
  141. return -ENOMEM;
  142. platform_set_drvdata(pdev, priv);
  143. for (i = 0; i < BUTTON_TYPES; i++) {
  144. pd = soc_button_device_create(pdev, button_info, i == 0);
  145. if (IS_ERR(pd)) {
  146. error = PTR_ERR(pd);
  147. if (error != -ENODEV) {
  148. soc_button_remove(pdev);
  149. return error;
  150. }
  151. continue;
  152. }
  153. priv->children[i] = pd;
  154. }
  155. if (!priv->children[0] && !priv->children[1])
  156. return -ENODEV;
  157. return 0;
  158. }
  159. static struct soc_button_info soc_button_PNP0C40[] = {
  160. { "power", 0, EV_KEY, KEY_POWER, false, true },
  161. { "home", 1, EV_KEY, KEY_LEFTMETA, false, true },
  162. { "volume_up", 2, EV_KEY, KEY_VOLUMEUP, true, false },
  163. { "volume_down", 3, EV_KEY, KEY_VOLUMEDOWN, true, false },
  164. { "rotation_lock", 4, EV_SW, SW_ROTATE_LOCK, false, false },
  165. { }
  166. };
  167. static const struct acpi_device_id soc_button_acpi_match[] = {
  168. { "PNP0C40", (unsigned long)soc_button_PNP0C40 },
  169. { }
  170. };
  171. MODULE_DEVICE_TABLE(acpi, soc_button_acpi_match);
  172. static struct platform_driver soc_button_driver = {
  173. .probe = soc_button_probe,
  174. .remove = soc_button_remove,
  175. .driver = {
  176. .name = KBUILD_MODNAME,
  177. .acpi_match_table = ACPI_PTR(soc_button_acpi_match),
  178. },
  179. };
  180. module_platform_driver(soc_button_driver);
  181. MODULE_LICENSE("GPL");