soc_button_array.c 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221
  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. /*
  21. * Definition of buttons on the tablet. The ACPI index of each button
  22. * is defined in section 2.8.7.2 of "Windows ACPI Design Guide for SoC
  23. * Platforms"
  24. */
  25. #define MAX_NBUTTONS 5
  26. struct soc_button_info {
  27. const char *name;
  28. int acpi_index;
  29. unsigned int event_type;
  30. unsigned int event_code;
  31. bool autorepeat;
  32. bool wakeup;
  33. };
  34. /*
  35. * Some of the buttons like volume up/down are auto repeat, while others
  36. * are not. To support both, we register two platform devices, and put
  37. * buttons into them based on whether the key should be auto repeat.
  38. */
  39. #define BUTTON_TYPES 2
  40. struct soc_button_data {
  41. struct platform_device *children[BUTTON_TYPES];
  42. };
  43. /*
  44. * Get the Nth GPIO number from the ACPI object.
  45. */
  46. static int soc_button_lookup_gpio(struct device *dev, int acpi_index)
  47. {
  48. struct gpio_desc *desc;
  49. int gpio;
  50. desc = gpiod_get_index(dev, KBUILD_MODNAME, acpi_index, GPIOD_ASIS);
  51. if (IS_ERR(desc))
  52. return PTR_ERR(desc);
  53. gpio = desc_to_gpio(desc);
  54. gpiod_put(desc);
  55. return gpio;
  56. }
  57. static struct platform_device *
  58. soc_button_device_create(struct platform_device *pdev,
  59. const struct soc_button_info *button_info,
  60. bool autorepeat)
  61. {
  62. const struct soc_button_info *info;
  63. struct platform_device *pd;
  64. struct gpio_keys_button *gpio_keys;
  65. struct gpio_keys_platform_data *gpio_keys_pdata;
  66. int n_buttons = 0;
  67. int gpio;
  68. int error;
  69. gpio_keys_pdata = devm_kzalloc(&pdev->dev,
  70. sizeof(*gpio_keys_pdata) +
  71. sizeof(*gpio_keys) * MAX_NBUTTONS,
  72. GFP_KERNEL);
  73. if (!gpio_keys_pdata)
  74. return ERR_PTR(-ENOMEM);
  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 int soc_button_remove(struct platform_device *pdev)
  117. {
  118. struct soc_button_data *priv = platform_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. return 0;
  124. }
  125. static int soc_button_probe(struct platform_device *pdev)
  126. {
  127. struct device *dev = &pdev->dev;
  128. const struct acpi_device_id *id;
  129. struct soc_button_info *button_info;
  130. struct soc_button_data *priv;
  131. struct platform_device *pd;
  132. int i;
  133. int error;
  134. id = acpi_match_device(dev->driver->acpi_match_table, dev);
  135. if (!id)
  136. return -ENODEV;
  137. button_info = (struct soc_button_info *)id->driver_data;
  138. priv = devm_kzalloc(&pdev->dev, sizeof(*priv), GFP_KERNEL);
  139. if (!priv)
  140. return -ENOMEM;
  141. platform_set_drvdata(pdev, priv);
  142. for (i = 0; i < BUTTON_TYPES; i++) {
  143. pd = soc_button_device_create(pdev, button_info, i == 0);
  144. if (IS_ERR(pd)) {
  145. error = PTR_ERR(pd);
  146. if (error != -ENODEV) {
  147. soc_button_remove(pdev);
  148. return error;
  149. }
  150. continue;
  151. }
  152. priv->children[i] = pd;
  153. }
  154. if (!priv->children[0] && !priv->children[1])
  155. return -ENODEV;
  156. return 0;
  157. }
  158. static struct soc_button_info soc_button_PNP0C40[] = {
  159. { "power", 0, EV_KEY, KEY_POWER, false, true },
  160. { "home", 1, EV_KEY, KEY_LEFTMETA, false, true },
  161. { "volume_up", 2, EV_KEY, KEY_VOLUMEUP, true, false },
  162. { "volume_down", 3, EV_KEY, KEY_VOLUMEDOWN, true, false },
  163. { "rotation_lock", 4, EV_SW, SW_ROTATE_LOCK, false, false },
  164. { }
  165. };
  166. static const struct acpi_device_id soc_button_acpi_match[] = {
  167. { "PNP0C40", (unsigned long)soc_button_PNP0C40 },
  168. { }
  169. };
  170. MODULE_DEVICE_TABLE(acpi, soc_button_acpi_match);
  171. static struct platform_driver soc_button_driver = {
  172. .probe = soc_button_probe,
  173. .remove = soc_button_remove,
  174. .driver = {
  175. .name = KBUILD_MODNAME,
  176. .acpi_match_table = ACPI_PTR(soc_button_acpi_match),
  177. },
  178. };
  179. module_platform_driver(soc_button_driver);
  180. MODULE_LICENSE("GPL");