gpio_keys_polled.c 9.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381
  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. int nbuttons;
  126. nbuttons = device_get_child_node_count(dev);
  127. if (nbuttons == 0)
  128. return ERR_PTR(-EINVAL);
  129. pdata = devm_kzalloc(dev, sizeof(*pdata) + nbuttons * sizeof(*button),
  130. GFP_KERNEL);
  131. if (!pdata)
  132. return ERR_PTR(-ENOMEM);
  133. button = (struct gpio_keys_button *)(pdata + 1);
  134. pdata->buttons = button;
  135. pdata->nbuttons = nbuttons;
  136. pdata->rep = device_property_present(dev, "autorepeat");
  137. device_property_read_u32(dev, "poll-interval", &pdata->poll_interval);
  138. device_property_read_string(dev, "label", &pdata->name);
  139. device_for_each_child_node_scoped(dev, child) {
  140. if (fwnode_property_read_u32(child, "linux,code",
  141. &button->code)) {
  142. dev_err(dev, "button without keycode\n");
  143. return ERR_PTR(-EINVAL);
  144. }
  145. fwnode_property_read_string(child, "label", &button->desc);
  146. if (fwnode_property_read_u32(child, "linux,input-type",
  147. &button->type))
  148. button->type = EV_KEY;
  149. if (fwnode_property_read_u32(child, "linux,input-value",
  150. (u32 *)&button->value))
  151. button->value = 1;
  152. button->wakeup =
  153. fwnode_property_read_bool(child, "wakeup-source") ||
  154. /* legacy name */
  155. fwnode_property_read_bool(child, "gpio-key,wakeup");
  156. if (fwnode_property_read_u32(child, "debounce-interval",
  157. &button->debounce_interval))
  158. button->debounce_interval = 5;
  159. button++;
  160. }
  161. return pdata;
  162. }
  163. static void gpio_keys_polled_set_abs_params(struct input_dev *input,
  164. const struct gpio_keys_platform_data *pdata, unsigned int code)
  165. {
  166. int i, min = 0, max = 0;
  167. for (i = 0; i < pdata->nbuttons; i++) {
  168. const struct gpio_keys_button *button = &pdata->buttons[i];
  169. if (button->type != EV_ABS || button->code != code)
  170. continue;
  171. if (button->value < min)
  172. min = button->value;
  173. if (button->value > max)
  174. max = button->value;
  175. }
  176. input_set_abs_params(input, code, min, max, 0, 0);
  177. }
  178. static const struct of_device_id gpio_keys_polled_of_match[] = {
  179. { .compatible = "gpio-keys-polled", },
  180. { },
  181. };
  182. MODULE_DEVICE_TABLE(of, gpio_keys_polled_of_match);
  183. static int gpio_keys_polled_probe(struct platform_device *pdev)
  184. {
  185. struct device *dev = &pdev->dev;
  186. struct fwnode_handle *child = NULL;
  187. const struct gpio_keys_platform_data *pdata = dev_get_platdata(dev);
  188. struct gpio_keys_polled_dev *bdev;
  189. struct input_dev *input;
  190. int error;
  191. int i;
  192. if (!pdata) {
  193. pdata = gpio_keys_polled_get_devtree_pdata(dev);
  194. if (IS_ERR(pdata))
  195. return PTR_ERR(pdata);
  196. }
  197. if (!pdata->poll_interval) {
  198. dev_err(dev, "missing poll_interval value\n");
  199. return -EINVAL;
  200. }
  201. bdev = devm_kzalloc(dev, struct_size(bdev, data, pdata->nbuttons),
  202. GFP_KERNEL);
  203. if (!bdev) {
  204. dev_err(dev, "no memory for private data\n");
  205. return -ENOMEM;
  206. }
  207. input = devm_input_allocate_device(dev);
  208. if (!input) {
  209. dev_err(dev, "no memory for input device\n");
  210. return -ENOMEM;
  211. }
  212. input_set_drvdata(input, bdev);
  213. input->name = pdata->name ?: pdev->name;
  214. input->phys = DRV_NAME"/input0";
  215. input->id.bustype = BUS_HOST;
  216. input->id.vendor = 0x0001;
  217. input->id.product = 0x0001;
  218. input->id.version = 0x0100;
  219. input->open = gpio_keys_polled_open;
  220. input->close = gpio_keys_polled_close;
  221. __set_bit(EV_KEY, input->evbit);
  222. if (pdata->rep)
  223. __set_bit(EV_REP, input->evbit);
  224. for (i = 0; i < pdata->nbuttons; i++) {
  225. const struct gpio_keys_button *button = &pdata->buttons[i];
  226. struct gpio_keys_button_data *bdata = &bdev->data[i];
  227. unsigned int type = button->type ?: EV_KEY;
  228. if (button->wakeup) {
  229. dev_err(dev, DRV_NAME " does not support wakeup\n");
  230. fwnode_handle_put(child);
  231. return -EINVAL;
  232. }
  233. if (!dev_get_platdata(dev)) {
  234. /* No legacy static platform data */
  235. child = device_get_next_child_node(dev, child);
  236. if (!child) {
  237. dev_err(dev, "missing child device node\n");
  238. return -EINVAL;
  239. }
  240. bdata->gpiod = devm_fwnode_gpiod_get(dev, child,
  241. NULL, GPIOD_IN,
  242. button->desc);
  243. if (IS_ERR(bdata->gpiod)) {
  244. fwnode_handle_put(child);
  245. return dev_err_probe(dev, PTR_ERR(bdata->gpiod),
  246. "failed to get gpio\n");
  247. }
  248. } else if (gpio_is_valid(button->gpio)) {
  249. /*
  250. * Legacy GPIO number so request the GPIO here and
  251. * convert it to descriptor.
  252. */
  253. error = devm_gpio_request_one(dev, button->gpio, GPIOF_IN,
  254. button->desc ? : DRV_NAME);
  255. if (error)
  256. return dev_err_probe(dev, error,
  257. "unable to claim gpio %u\n",
  258. button->gpio);
  259. bdata->gpiod = gpio_to_desc(button->gpio);
  260. if (!bdata->gpiod) {
  261. dev_err(dev,
  262. "unable to convert gpio %u to descriptor\n",
  263. button->gpio);
  264. return -EINVAL;
  265. }
  266. if (button->active_low ^ gpiod_is_active_low(bdata->gpiod))
  267. gpiod_toggle_active_low(bdata->gpiod);
  268. }
  269. bdata->last_state = -1;
  270. bdata->threshold = DIV_ROUND_UP(button->debounce_interval,
  271. pdata->poll_interval);
  272. input_set_capability(input, type, button->code);
  273. if (type == EV_ABS)
  274. gpio_keys_polled_set_abs_params(input, pdata,
  275. button->code);
  276. }
  277. fwnode_handle_put(child);
  278. bdev->input = input;
  279. bdev->dev = dev;
  280. bdev->pdata = pdata;
  281. error = input_setup_polling(input, gpio_keys_polled_poll);
  282. if (error) {
  283. dev_err(dev, "unable to set up polling, err=%d\n", error);
  284. return error;
  285. }
  286. input_set_poll_interval(input, pdata->poll_interval);
  287. error = input_register_device(input);
  288. if (error) {
  289. dev_err(dev, "unable to register polled device, err=%d\n",
  290. error);
  291. return error;
  292. }
  293. /* report initial state of the buttons */
  294. for (i = 0; i < pdata->nbuttons; i++)
  295. gpio_keys_polled_check_state(input, &pdata->buttons[i],
  296. &bdev->data[i]);
  297. input_sync(input);
  298. return 0;
  299. }
  300. static struct platform_driver gpio_keys_polled_driver = {
  301. .probe = gpio_keys_polled_probe,
  302. .driver = {
  303. .name = DRV_NAME,
  304. .of_match_table = gpio_keys_polled_of_match,
  305. },
  306. };
  307. module_platform_driver(gpio_keys_polled_driver);
  308. MODULE_LICENSE("GPL v2");
  309. MODULE_AUTHOR("Gabor Juhos <juhosg@openwrt.org>");
  310. MODULE_DESCRIPTION("Polled GPIO Buttons driver");
  311. MODULE_ALIAS("platform:" DRV_NAME);