gpio_keys_polled.c 9.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391
  1. // SPDX-License-Identifier: GPL-2.0-only
  2. /*
  3. * Driver for buttons on GPIO lines not capable of generating interrupts
  4. *
  5. * Copyright (C) 2007-2010 Gabor Juhos <juhosg@openwrt.org>
  6. * Copyright (C) 2010 Nuno Goncalves <nunojpg@gmail.com>
  7. *
  8. * This file was based on: /drivers/input/misc/cobalt_btns.c
  9. * Copyright (C) 2007 Yoichi Yuasa <yoichi_yuasa@tripeaks.co.jp>
  10. *
  11. * also was based on: /drivers/input/keyboard/gpio_keys.c
  12. * Copyright 2005 Phil Blundell
  13. */
  14. #include <linux/kernel.h>
  15. #include <linux/module.h>
  16. #include <linux/slab.h>
  17. #include <linux/input.h>
  18. #include <linux/ioport.h>
  19. #include <linux/platform_device.h>
  20. #include <linux/gpio.h>
  21. #include <linux/gpio/consumer.h>
  22. #include <linux/gpio_keys.h>
  23. #include <linux/property.h>
  24. #define DRV_NAME "gpio-keys-polled"
  25. struct gpio_keys_button_data {
  26. struct gpio_desc *gpiod;
  27. int last_state;
  28. int count;
  29. int threshold;
  30. };
  31. struct gpio_keys_polled_dev {
  32. struct input_dev *input;
  33. struct device *dev;
  34. const struct gpio_keys_platform_data *pdata;
  35. unsigned long rel_axis_seen[BITS_TO_LONGS(REL_CNT)];
  36. unsigned long abs_axis_seen[BITS_TO_LONGS(ABS_CNT)];
  37. struct gpio_keys_button_data data[];
  38. };
  39. static void gpio_keys_button_event(struct input_dev *input,
  40. const struct gpio_keys_button *button,
  41. int state)
  42. {
  43. struct gpio_keys_polled_dev *bdev = input_get_drvdata(input);
  44. unsigned int type = button->type ?: EV_KEY;
  45. if (type == EV_REL) {
  46. if (state) {
  47. input_event(input, type, button->code, button->value);
  48. __set_bit(button->code, bdev->rel_axis_seen);
  49. }
  50. } else if (type == EV_ABS) {
  51. if (state) {
  52. input_event(input, type, button->code, button->value);
  53. __set_bit(button->code, bdev->abs_axis_seen);
  54. }
  55. } else {
  56. input_event(input, type, button->code, state);
  57. input_sync(input);
  58. }
  59. }
  60. static void gpio_keys_polled_check_state(struct input_dev *input,
  61. const struct gpio_keys_button *button,
  62. struct gpio_keys_button_data *bdata)
  63. {
  64. int state;
  65. state = gpiod_get_value_cansleep(bdata->gpiod);
  66. if (state < 0) {
  67. dev_err(input->dev.parent,
  68. "failed to get gpio state: %d\n", state);
  69. } else {
  70. gpio_keys_button_event(input, button, state);
  71. if (state != bdata->last_state) {
  72. bdata->count = 0;
  73. bdata->last_state = state;
  74. }
  75. }
  76. }
  77. static void gpio_keys_polled_poll(struct input_dev *input)
  78. {
  79. struct gpio_keys_polled_dev *bdev = input_get_drvdata(input);
  80. const struct gpio_keys_platform_data *pdata = bdev->pdata;
  81. int i;
  82. memset(bdev->rel_axis_seen, 0, sizeof(bdev->rel_axis_seen));
  83. memset(bdev->abs_axis_seen, 0, sizeof(bdev->abs_axis_seen));
  84. for (i = 0; i < pdata->nbuttons; i++) {
  85. struct gpio_keys_button_data *bdata = &bdev->data[i];
  86. if (bdata->count < bdata->threshold) {
  87. bdata->count++;
  88. gpio_keys_button_event(input, &pdata->buttons[i],
  89. bdata->last_state);
  90. } else {
  91. gpio_keys_polled_check_state(input, &pdata->buttons[i],
  92. bdata);
  93. }
  94. }
  95. for_each_set_bit(i, input->relbit, REL_CNT) {
  96. if (!test_bit(i, bdev->rel_axis_seen))
  97. input_event(input, EV_REL, i, 0);
  98. }
  99. for_each_set_bit(i, input->absbit, ABS_CNT) {
  100. if (!test_bit(i, bdev->abs_axis_seen))
  101. input_event(input, EV_ABS, i, 0);
  102. }
  103. input_sync(input);
  104. }
  105. static int gpio_keys_polled_open(struct input_dev *input)
  106. {
  107. struct gpio_keys_polled_dev *bdev = input_get_drvdata(input);
  108. const struct gpio_keys_platform_data *pdata = bdev->pdata;
  109. if (pdata->enable)
  110. pdata->enable(bdev->dev);
  111. return 0;
  112. }
  113. static void gpio_keys_polled_close(struct input_dev *input)
  114. {
  115. struct gpio_keys_polled_dev *bdev = input_get_drvdata(input);
  116. const struct gpio_keys_platform_data *pdata = bdev->pdata;
  117. if (pdata->disable)
  118. pdata->disable(bdev->dev);
  119. }
  120. static struct gpio_keys_platform_data *
  121. gpio_keys_polled_get_devtree_pdata(struct device *dev)
  122. {
  123. struct gpio_keys_platform_data *pdata;
  124. struct gpio_keys_button *button;
  125. struct fwnode_handle *child;
  126. int nbuttons;
  127. nbuttons = device_get_child_node_count(dev);
  128. if (nbuttons == 0)
  129. return ERR_PTR(-EINVAL);
  130. pdata = devm_kzalloc(dev, sizeof(*pdata) + nbuttons * sizeof(*button),
  131. GFP_KERNEL);
  132. if (!pdata)
  133. return ERR_PTR(-ENOMEM);
  134. button = (struct gpio_keys_button *)(pdata + 1);
  135. pdata->buttons = button;
  136. pdata->nbuttons = nbuttons;
  137. pdata->rep = device_property_present(dev, "autorepeat");
  138. device_property_read_u32(dev, "poll-interval", &pdata->poll_interval);
  139. device_property_read_string(dev, "label", &pdata->name);
  140. device_for_each_child_node(dev, child) {
  141. if (fwnode_property_read_u32(child, "linux,code",
  142. &button->code)) {
  143. dev_err(dev, "button without keycode\n");
  144. fwnode_handle_put(child);
  145. return ERR_PTR(-EINVAL);
  146. }
  147. fwnode_property_read_string(child, "label", &button->desc);
  148. if (fwnode_property_read_u32(child, "linux,input-type",
  149. &button->type))
  150. button->type = EV_KEY;
  151. if (fwnode_property_read_u32(child, "linux,input-value",
  152. (u32 *)&button->value))
  153. button->value = 1;
  154. button->wakeup =
  155. fwnode_property_read_bool(child, "wakeup-source") ||
  156. /* legacy name */
  157. fwnode_property_read_bool(child, "gpio-key,wakeup");
  158. if (fwnode_property_read_u32(child, "debounce-interval",
  159. &button->debounce_interval))
  160. button->debounce_interval = 5;
  161. button++;
  162. }
  163. return pdata;
  164. }
  165. static void gpio_keys_polled_set_abs_params(struct input_dev *input,
  166. const struct gpio_keys_platform_data *pdata, unsigned int code)
  167. {
  168. int i, min = 0, max = 0;
  169. for (i = 0; i < pdata->nbuttons; i++) {
  170. const struct gpio_keys_button *button = &pdata->buttons[i];
  171. if (button->type != EV_ABS || button->code != code)
  172. continue;
  173. if (button->value < min)
  174. min = button->value;
  175. if (button->value > max)
  176. max = button->value;
  177. }
  178. input_set_abs_params(input, code, min, max, 0, 0);
  179. }
  180. static const struct of_device_id gpio_keys_polled_of_match[] = {
  181. { .compatible = "gpio-keys-polled", },
  182. { },
  183. };
  184. MODULE_DEVICE_TABLE(of, gpio_keys_polled_of_match);
  185. static int gpio_keys_polled_probe(struct platform_device *pdev)
  186. {
  187. struct device *dev = &pdev->dev;
  188. struct fwnode_handle *child = NULL;
  189. const struct gpio_keys_platform_data *pdata = dev_get_platdata(dev);
  190. struct gpio_keys_polled_dev *bdev;
  191. struct input_dev *input;
  192. int error;
  193. int i;
  194. if (!pdata) {
  195. pdata = gpio_keys_polled_get_devtree_pdata(dev);
  196. if (IS_ERR(pdata))
  197. return PTR_ERR(pdata);
  198. }
  199. if (!pdata->poll_interval) {
  200. dev_err(dev, "missing poll_interval value\n");
  201. return -EINVAL;
  202. }
  203. bdev = devm_kzalloc(dev, struct_size(bdev, data, pdata->nbuttons),
  204. GFP_KERNEL);
  205. if (!bdev) {
  206. dev_err(dev, "no memory for private data\n");
  207. return -ENOMEM;
  208. }
  209. input = devm_input_allocate_device(dev);
  210. if (!input) {
  211. dev_err(dev, "no memory for input device\n");
  212. return -ENOMEM;
  213. }
  214. input_set_drvdata(input, bdev);
  215. input->name = pdata->name ?: pdev->name;
  216. input->phys = DRV_NAME"/input0";
  217. input->id.bustype = BUS_HOST;
  218. input->id.vendor = 0x0001;
  219. input->id.product = 0x0001;
  220. input->id.version = 0x0100;
  221. input->open = gpio_keys_polled_open;
  222. input->close = gpio_keys_polled_close;
  223. __set_bit(EV_KEY, input->evbit);
  224. if (pdata->rep)
  225. __set_bit(EV_REP, input->evbit);
  226. for (i = 0; i < pdata->nbuttons; i++) {
  227. const struct gpio_keys_button *button = &pdata->buttons[i];
  228. struct gpio_keys_button_data *bdata = &bdev->data[i];
  229. unsigned int type = button->type ?: EV_KEY;
  230. if (button->wakeup) {
  231. dev_err(dev, DRV_NAME " does not support wakeup\n");
  232. fwnode_handle_put(child);
  233. return -EINVAL;
  234. }
  235. if (!dev_get_platdata(dev)) {
  236. /* No legacy static platform data */
  237. child = device_get_next_child_node(dev, child);
  238. if (!child) {
  239. dev_err(dev, "missing child device node\n");
  240. return -EINVAL;
  241. }
  242. bdata->gpiod = devm_fwnode_gpiod_get(dev, child,
  243. NULL, GPIOD_IN,
  244. button->desc);
  245. if (IS_ERR(bdata->gpiod)) {
  246. error = PTR_ERR(bdata->gpiod);
  247. if (error != -EPROBE_DEFER)
  248. dev_err(dev,
  249. "failed to get gpio: %d\n",
  250. error);
  251. fwnode_handle_put(child);
  252. return error;
  253. }
  254. } else if (gpio_is_valid(button->gpio)) {
  255. /*
  256. * Legacy GPIO number so request the GPIO here and
  257. * convert it to descriptor.
  258. */
  259. unsigned flags = GPIOF_IN;
  260. if (button->active_low)
  261. flags |= GPIOF_ACTIVE_LOW;
  262. error = devm_gpio_request_one(dev, button->gpio,
  263. flags, button->desc ? : DRV_NAME);
  264. if (error) {
  265. dev_err(dev,
  266. "unable to claim gpio %u, err=%d\n",
  267. button->gpio, error);
  268. return error;
  269. }
  270. bdata->gpiod = gpio_to_desc(button->gpio);
  271. if (!bdata->gpiod) {
  272. dev_err(dev,
  273. "unable to convert gpio %u to descriptor\n",
  274. button->gpio);
  275. return -EINVAL;
  276. }
  277. }
  278. bdata->last_state = -1;
  279. bdata->threshold = DIV_ROUND_UP(button->debounce_interval,
  280. pdata->poll_interval);
  281. input_set_capability(input, type, button->code);
  282. if (type == EV_ABS)
  283. gpio_keys_polled_set_abs_params(input, pdata,
  284. button->code);
  285. }
  286. fwnode_handle_put(child);
  287. bdev->input = input;
  288. bdev->dev = dev;
  289. bdev->pdata = pdata;
  290. error = input_setup_polling(input, gpio_keys_polled_poll);
  291. if (error) {
  292. dev_err(dev, "unable to set up polling, err=%d\n", error);
  293. return error;
  294. }
  295. input_set_poll_interval(input, pdata->poll_interval);
  296. error = input_register_device(input);
  297. if (error) {
  298. dev_err(dev, "unable to register polled device, err=%d\n",
  299. error);
  300. return error;
  301. }
  302. /* report initial state of the buttons */
  303. for (i = 0; i < pdata->nbuttons; i++)
  304. gpio_keys_polled_check_state(input, &pdata->buttons[i],
  305. &bdev->data[i]);
  306. input_sync(input);
  307. return 0;
  308. }
  309. static struct platform_driver gpio_keys_polled_driver = {
  310. .probe = gpio_keys_polled_probe,
  311. .driver = {
  312. .name = DRV_NAME,
  313. .of_match_table = gpio_keys_polled_of_match,
  314. },
  315. };
  316. module_platform_driver(gpio_keys_polled_driver);
  317. MODULE_LICENSE("GPL v2");
  318. MODULE_AUTHOR("Gabor Juhos <juhosg@openwrt.org>");
  319. MODULE_DESCRIPTION("Polled GPIO Buttons driver");
  320. MODULE_ALIAS("platform:" DRV_NAME);