soc_button_array.c 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390
  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/gpio.h>
  20. #include <linux/platform_device.h>
  21. struct soc_button_info {
  22. const char *name;
  23. int acpi_index;
  24. unsigned int event_type;
  25. unsigned int event_code;
  26. bool autorepeat;
  27. bool wakeup;
  28. };
  29. /*
  30. * Some of the buttons like volume up/down are auto repeat, while others
  31. * are not. To support both, we register two platform devices, and put
  32. * buttons into them based on whether the key should be auto repeat.
  33. */
  34. #define BUTTON_TYPES 2
  35. struct soc_button_data {
  36. struct platform_device *children[BUTTON_TYPES];
  37. };
  38. /*
  39. * Get the Nth GPIO number from the ACPI object.
  40. */
  41. static int soc_button_lookup_gpio(struct device *dev, int acpi_index)
  42. {
  43. struct gpio_desc *desc;
  44. int gpio;
  45. desc = gpiod_get_index(dev, NULL, acpi_index, GPIOD_ASIS);
  46. if (IS_ERR(desc))
  47. return PTR_ERR(desc);
  48. gpio = desc_to_gpio(desc);
  49. gpiod_put(desc);
  50. return gpio;
  51. }
  52. static struct platform_device *
  53. soc_button_device_create(struct platform_device *pdev,
  54. const struct soc_button_info *button_info,
  55. bool autorepeat)
  56. {
  57. const struct soc_button_info *info;
  58. struct platform_device *pd;
  59. struct gpio_keys_button *gpio_keys;
  60. struct gpio_keys_platform_data *gpio_keys_pdata;
  61. int n_buttons = 0;
  62. int gpio;
  63. int error;
  64. for (info = button_info; info->name; info++)
  65. if (info->autorepeat == autorepeat)
  66. n_buttons++;
  67. gpio_keys_pdata = devm_kzalloc(&pdev->dev,
  68. sizeof(*gpio_keys_pdata) +
  69. sizeof(*gpio_keys) * n_buttons,
  70. GFP_KERNEL);
  71. if (!gpio_keys_pdata)
  72. return ERR_PTR(-ENOMEM);
  73. gpio_keys = (void *)(gpio_keys_pdata + 1);
  74. n_buttons = 0;
  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_is_valid(gpio))
  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. /* These devices often use cheap buttons, use 50 ms debounce */
  88. gpio_keys[n_buttons].debounce_interval = 50;
  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_get_acpi_object_int(const union acpi_object *obj)
  118. {
  119. if (obj->type != ACPI_TYPE_INTEGER)
  120. return -1;
  121. return obj->integer.value;
  122. }
  123. /* Parse a single ACPI0011 _DSD button descriptor */
  124. static int soc_button_parse_btn_desc(struct device *dev,
  125. const union acpi_object *desc,
  126. int collection_uid,
  127. struct soc_button_info *info)
  128. {
  129. int upage, usage;
  130. if (desc->type != ACPI_TYPE_PACKAGE ||
  131. desc->package.count != 5 ||
  132. /* First byte should be 1 (control) */
  133. soc_button_get_acpi_object_int(&desc->package.elements[0]) != 1 ||
  134. /* Third byte should be collection uid */
  135. soc_button_get_acpi_object_int(&desc->package.elements[2]) !=
  136. collection_uid) {
  137. dev_err(dev, "Invalid ACPI Button Descriptor\n");
  138. return -ENODEV;
  139. }
  140. info->event_type = EV_KEY;
  141. info->acpi_index =
  142. soc_button_get_acpi_object_int(&desc->package.elements[1]);
  143. upage = soc_button_get_acpi_object_int(&desc->package.elements[3]);
  144. usage = soc_button_get_acpi_object_int(&desc->package.elements[4]);
  145. /*
  146. * The UUID: fa6bd625-9ce8-470d-a2c7-b3ca36c4282e descriptors use HID
  147. * usage page and usage codes, but otherwise the device is not HID
  148. * compliant: it uses one irq per button instead of generating HID
  149. * input reports and some buttons should generate wakeups where as
  150. * others should not, so we cannot use the HID subsystem.
  151. *
  152. * Luckily all devices only use a few usage page + usage combinations,
  153. * so we can simply check for the known combinations here.
  154. */
  155. if (upage == 0x01 && usage == 0x81) {
  156. info->name = "power";
  157. info->event_code = KEY_POWER;
  158. info->wakeup = true;
  159. } else if (upage == 0x07 && usage == 0xe3) {
  160. info->name = "home";
  161. info->event_code = KEY_LEFTMETA;
  162. info->wakeup = true;
  163. } else if (upage == 0x0c && usage == 0xe9) {
  164. info->name = "volume_up";
  165. info->event_code = KEY_VOLUMEUP;
  166. info->autorepeat = true;
  167. } else if (upage == 0x0c && usage == 0xea) {
  168. info->name = "volume_down";
  169. info->event_code = KEY_VOLUMEDOWN;
  170. info->autorepeat = true;
  171. } else {
  172. dev_warn(dev, "Unknown button index %d upage %02x usage %02x, ignoring\n",
  173. info->acpi_index, upage, usage);
  174. info->name = "unknown";
  175. info->event_code = KEY_RESERVED;
  176. }
  177. return 0;
  178. }
  179. /* ACPI0011 _DSD btns descriptors UUID: fa6bd625-9ce8-470d-a2c7-b3ca36c4282e */
  180. static const u8 btns_desc_uuid[16] = {
  181. 0x25, 0xd6, 0x6b, 0xfa, 0xe8, 0x9c, 0x0d, 0x47,
  182. 0xa2, 0xc7, 0xb3, 0xca, 0x36, 0xc4, 0x28, 0x2e
  183. };
  184. /* Parse ACPI0011 _DSD button descriptors */
  185. static struct soc_button_info *soc_button_get_button_info(struct device *dev)
  186. {
  187. struct acpi_buffer buf = { ACPI_ALLOCATE_BUFFER };
  188. const union acpi_object *desc, *el0, *uuid, *btns_desc = NULL;
  189. struct soc_button_info *button_info;
  190. acpi_status status;
  191. int i, btn, collection_uid = -1;
  192. status = acpi_evaluate_object_typed(ACPI_HANDLE(dev), "_DSD", NULL,
  193. &buf, ACPI_TYPE_PACKAGE);
  194. if (ACPI_FAILURE(status)) {
  195. dev_err(dev, "ACPI _DSD object not found\n");
  196. return ERR_PTR(-ENODEV);
  197. }
  198. /* Look for the Button Descriptors UUID */
  199. desc = buf.pointer;
  200. for (i = 0; (i + 1) < desc->package.count; i += 2) {
  201. uuid = &desc->package.elements[i];
  202. if (uuid->type != ACPI_TYPE_BUFFER ||
  203. uuid->buffer.length != 16 ||
  204. desc->package.elements[i + 1].type != ACPI_TYPE_PACKAGE) {
  205. break;
  206. }
  207. if (memcmp(uuid->buffer.pointer, btns_desc_uuid, 16) == 0) {
  208. btns_desc = &desc->package.elements[i + 1];
  209. break;
  210. }
  211. }
  212. if (!btns_desc) {
  213. dev_err(dev, "ACPI Button Descriptors not found\n");
  214. return ERR_PTR(-ENODEV);
  215. }
  216. /* The first package describes the collection */
  217. el0 = &btns_desc->package.elements[0];
  218. if (el0->type == ACPI_TYPE_PACKAGE &&
  219. el0->package.count == 5 &&
  220. /* First byte should be 0 (collection) */
  221. soc_button_get_acpi_object_int(&el0->package.elements[0]) == 0 &&
  222. /* Third byte should be 0 (top level collection) */
  223. soc_button_get_acpi_object_int(&el0->package.elements[2]) == 0) {
  224. collection_uid = soc_button_get_acpi_object_int(
  225. &el0->package.elements[1]);
  226. }
  227. if (collection_uid == -1) {
  228. dev_err(dev, "Invalid Button Collection Descriptor\n");
  229. return ERR_PTR(-ENODEV);
  230. }
  231. /* There are package.count - 1 buttons + 1 terminating empty entry */
  232. button_info = devm_kcalloc(dev, btns_desc->package.count,
  233. sizeof(*button_info), GFP_KERNEL);
  234. if (!button_info)
  235. return ERR_PTR(-ENOMEM);
  236. /* Parse the button descriptors */
  237. for (i = 1, btn = 0; i < btns_desc->package.count; i++, btn++) {
  238. if (soc_button_parse_btn_desc(dev,
  239. &btns_desc->package.elements[i],
  240. collection_uid,
  241. &button_info[btn]))
  242. return ERR_PTR(-ENODEV);
  243. }
  244. return button_info;
  245. }
  246. static int soc_button_remove(struct platform_device *pdev)
  247. {
  248. struct soc_button_data *priv = platform_get_drvdata(pdev);
  249. int i;
  250. for (i = 0; i < BUTTON_TYPES; i++)
  251. if (priv->children[i])
  252. platform_device_unregister(priv->children[i]);
  253. return 0;
  254. }
  255. static int soc_button_probe(struct platform_device *pdev)
  256. {
  257. struct device *dev = &pdev->dev;
  258. const struct acpi_device_id *id;
  259. struct soc_button_info *button_info;
  260. struct soc_button_data *priv;
  261. struct platform_device *pd;
  262. int i;
  263. int error;
  264. id = acpi_match_device(dev->driver->acpi_match_table, dev);
  265. if (!id)
  266. return -ENODEV;
  267. if (!id->driver_data) {
  268. button_info = soc_button_get_button_info(dev);
  269. if (IS_ERR(button_info))
  270. return PTR_ERR(button_info);
  271. } else {
  272. button_info = (struct soc_button_info *)id->driver_data;
  273. }
  274. error = gpiod_count(dev, NULL);
  275. if (error < 0) {
  276. dev_dbg(dev, "no GPIO attached, ignoring...\n");
  277. return error;
  278. }
  279. priv = devm_kzalloc(dev, sizeof(*priv), GFP_KERNEL);
  280. if (!priv)
  281. return -ENOMEM;
  282. platform_set_drvdata(pdev, priv);
  283. for (i = 0; i < BUTTON_TYPES; i++) {
  284. pd = soc_button_device_create(pdev, button_info, i == 0);
  285. if (IS_ERR(pd)) {
  286. error = PTR_ERR(pd);
  287. if (error != -ENODEV) {
  288. soc_button_remove(pdev);
  289. return error;
  290. }
  291. continue;
  292. }
  293. priv->children[i] = pd;
  294. }
  295. if (!priv->children[0] && !priv->children[1])
  296. return -ENODEV;
  297. if (!id->driver_data)
  298. devm_kfree(dev, button_info);
  299. return 0;
  300. }
  301. /*
  302. * Definition of buttons on the tablet. The ACPI index of each button
  303. * is defined in section 2.8.7.2 of "Windows ACPI Design Guide for SoC
  304. * Platforms"
  305. */
  306. static struct soc_button_info soc_button_PNP0C40[] = {
  307. { "power", 0, EV_KEY, KEY_POWER, false, true },
  308. { "home", 1, EV_KEY, KEY_LEFTMETA, false, true },
  309. { "volume_up", 2, EV_KEY, KEY_VOLUMEUP, true, false },
  310. { "volume_down", 3, EV_KEY, KEY_VOLUMEDOWN, true, false },
  311. { "rotation_lock", 4, EV_SW, SW_ROTATE_LOCK, false, false },
  312. { }
  313. };
  314. static const struct acpi_device_id soc_button_acpi_match[] = {
  315. { "PNP0C40", (unsigned long)soc_button_PNP0C40 },
  316. { "ACPI0011", 0 },
  317. { }
  318. };
  319. MODULE_DEVICE_TABLE(acpi, soc_button_acpi_match);
  320. static struct platform_driver soc_button_driver = {
  321. .probe = soc_button_probe,
  322. .remove = soc_button_remove,
  323. .driver = {
  324. .name = KBUILD_MODNAME,
  325. .acpi_match_table = ACPI_PTR(soc_button_acpi_match),
  326. },
  327. };
  328. module_platform_driver(soc_button_driver);
  329. MODULE_LICENSE("GPL");