gpio_keys.c 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287
  1. /*
  2. * Driver for keys on GPIO lines capable of generating interrupts.
  3. *
  4. * Copyright 2005 Phil Blundell
  5. *
  6. * This program is free software; you can redistribute it and/or modify
  7. * it under the terms of the GNU General Public License version 2 as
  8. * published by the Free Software Foundation.
  9. */
  10. #include <linux/module.h>
  11. #include <linux/version.h>
  12. #include <linux/init.h>
  13. #include <linux/fs.h>
  14. #include <linux/interrupt.h>
  15. #include <linux/irq.h>
  16. #include <linux/sched.h>
  17. #include <linux/pm.h>
  18. #include <linux/sysctl.h>
  19. #include <linux/proc_fs.h>
  20. #include <linux/delay.h>
  21. #include <linux/platform_device.h>
  22. #include <linux/input.h>
  23. #include <linux/gpio_keys.h>
  24. #include <asm/gpio.h>
  25. struct gpio_button_data {
  26. struct gpio_keys_button *button;
  27. struct input_dev *input;
  28. struct timer_list timer;
  29. };
  30. struct gpio_keys_drvdata {
  31. struct input_dev *input;
  32. struct gpio_button_data data[0];
  33. };
  34. static void gpio_keys_report_event(struct gpio_keys_button *button,
  35. struct input_dev *input)
  36. {
  37. unsigned int type = button->type ?: EV_KEY;
  38. int state = (gpio_get_value(button->gpio) ? 1 : 0) ^ button->active_low;
  39. input_event(input, type, button->code, !!state);
  40. input_sync(input);
  41. }
  42. static void gpio_check_button(unsigned long _data)
  43. {
  44. struct gpio_button_data *data = (struct gpio_button_data *)_data;
  45. gpio_keys_report_event(data->button, data->input);
  46. }
  47. static irqreturn_t gpio_keys_isr(int irq, void *dev_id)
  48. {
  49. struct platform_device *pdev = dev_id;
  50. struct gpio_keys_platform_data *pdata = pdev->dev.platform_data;
  51. struct gpio_keys_drvdata *ddata = platform_get_drvdata(pdev);
  52. int i;
  53. for (i = 0; i < pdata->nbuttons; i++) {
  54. struct gpio_keys_button *button = &pdata->buttons[i];
  55. if (irq == gpio_to_irq(button->gpio)) {
  56. struct gpio_button_data *bdata = &ddata->data[i];
  57. if (button->debounce_interval)
  58. mod_timer(&bdata->timer,
  59. jiffies +
  60. msecs_to_jiffies(button->debounce_interval));
  61. else
  62. gpio_keys_report_event(button, bdata->input);
  63. return IRQ_HANDLED;
  64. }
  65. }
  66. return IRQ_NONE;
  67. }
  68. static int __devinit gpio_keys_probe(struct platform_device *pdev)
  69. {
  70. struct gpio_keys_platform_data *pdata = pdev->dev.platform_data;
  71. struct gpio_keys_drvdata *ddata;
  72. struct input_dev *input;
  73. int i, error;
  74. int wakeup = 0;
  75. ddata = kzalloc(sizeof(struct gpio_keys_drvdata) +
  76. pdata->nbuttons * sizeof(struct gpio_button_data),
  77. GFP_KERNEL);
  78. input = input_allocate_device();
  79. if (!ddata || !input) {
  80. error = -ENOMEM;
  81. goto fail1;
  82. }
  83. platform_set_drvdata(pdev, ddata);
  84. input->name = pdev->name;
  85. input->phys = "gpio-keys/input0";
  86. input->dev.parent = &pdev->dev;
  87. input->id.bustype = BUS_HOST;
  88. input->id.vendor = 0x0001;
  89. input->id.product = 0x0001;
  90. input->id.version = 0x0100;
  91. ddata->input = input;
  92. for (i = 0; i < pdata->nbuttons; i++) {
  93. struct gpio_keys_button *button = &pdata->buttons[i];
  94. struct gpio_button_data *bdata = &ddata->data[i];
  95. int irq;
  96. unsigned int type = button->type ?: EV_KEY;
  97. bdata->input = input;
  98. bdata->button = button;
  99. setup_timer(&bdata->timer,
  100. gpio_check_button, (unsigned long)bdata);
  101. error = gpio_request(button->gpio, button->desc ?: "gpio_keys");
  102. if (error < 0) {
  103. pr_err("gpio-keys: failed to request GPIO %d,"
  104. " error %d\n", button->gpio, error);
  105. goto fail2;
  106. }
  107. error = gpio_direction_input(button->gpio);
  108. if (error < 0) {
  109. pr_err("gpio-keys: failed to configure input"
  110. " direction for GPIO %d, error %d\n",
  111. button->gpio, error);
  112. gpio_free(button->gpio);
  113. goto fail2;
  114. }
  115. irq = gpio_to_irq(button->gpio);
  116. if (irq < 0) {
  117. error = irq;
  118. pr_err("gpio-keys: Unable to get irq number"
  119. " for GPIO %d, error %d\n",
  120. button->gpio, error);
  121. gpio_free(button->gpio);
  122. goto fail2;
  123. }
  124. error = request_irq(irq, gpio_keys_isr,
  125. IRQF_SAMPLE_RANDOM | IRQF_TRIGGER_RISING |
  126. IRQF_TRIGGER_FALLING,
  127. button->desc ? button->desc : "gpio_keys",
  128. pdev);
  129. if (error) {
  130. pr_err("gpio-keys: Unable to claim irq %d; error %d\n",
  131. irq, error);
  132. gpio_free(button->gpio);
  133. goto fail2;
  134. }
  135. if (button->wakeup)
  136. wakeup = 1;
  137. input_set_capability(input, type, button->code);
  138. }
  139. error = input_register_device(input);
  140. if (error) {
  141. pr_err("gpio-keys: Unable to register input device, "
  142. "error: %d\n", error);
  143. goto fail2;
  144. }
  145. device_init_wakeup(&pdev->dev, wakeup);
  146. return 0;
  147. fail2:
  148. while (--i >= 0) {
  149. free_irq(gpio_to_irq(pdata->buttons[i].gpio), pdev);
  150. if (pdata->buttons[i].debounce_interval)
  151. del_timer_sync(&ddata->data[i].timer);
  152. gpio_free(pdata->buttons[i].gpio);
  153. }
  154. platform_set_drvdata(pdev, NULL);
  155. fail1:
  156. input_free_device(input);
  157. kfree(ddata);
  158. return error;
  159. }
  160. static int __devexit gpio_keys_remove(struct platform_device *pdev)
  161. {
  162. struct gpio_keys_platform_data *pdata = pdev->dev.platform_data;
  163. struct gpio_keys_drvdata *ddata = platform_get_drvdata(pdev);
  164. struct input_dev *input = ddata->input;
  165. int i;
  166. device_init_wakeup(&pdev->dev, 0);
  167. for (i = 0; i < pdata->nbuttons; i++) {
  168. int irq = gpio_to_irq(pdata->buttons[i].gpio);
  169. free_irq(irq, pdev);
  170. if (pdata->buttons[i].debounce_interval)
  171. del_timer_sync(&ddata->data[i].timer);
  172. gpio_free(pdata->buttons[i].gpio);
  173. }
  174. input_unregister_device(input);
  175. return 0;
  176. }
  177. #ifdef CONFIG_PM
  178. static int gpio_keys_suspend(struct platform_device *pdev, pm_message_t state)
  179. {
  180. struct gpio_keys_platform_data *pdata = pdev->dev.platform_data;
  181. int i;
  182. if (device_may_wakeup(&pdev->dev)) {
  183. for (i = 0; i < pdata->nbuttons; i++) {
  184. struct gpio_keys_button *button = &pdata->buttons[i];
  185. if (button->wakeup) {
  186. int irq = gpio_to_irq(button->gpio);
  187. enable_irq_wake(irq);
  188. }
  189. }
  190. }
  191. return 0;
  192. }
  193. static int gpio_keys_resume(struct platform_device *pdev)
  194. {
  195. struct gpio_keys_platform_data *pdata = pdev->dev.platform_data;
  196. int i;
  197. if (device_may_wakeup(&pdev->dev)) {
  198. for (i = 0; i < pdata->nbuttons; i++) {
  199. struct gpio_keys_button *button = &pdata->buttons[i];
  200. if (button->wakeup) {
  201. int irq = gpio_to_irq(button->gpio);
  202. disable_irq_wake(irq);
  203. }
  204. }
  205. }
  206. return 0;
  207. }
  208. #else
  209. #define gpio_keys_suspend NULL
  210. #define gpio_keys_resume NULL
  211. #endif
  212. static struct platform_driver gpio_keys_device_driver = {
  213. .probe = gpio_keys_probe,
  214. .remove = __devexit_p(gpio_keys_remove),
  215. .suspend = gpio_keys_suspend,
  216. .resume = gpio_keys_resume,
  217. .driver = {
  218. .name = "gpio-keys",
  219. .owner = THIS_MODULE,
  220. }
  221. };
  222. static int __init gpio_keys_init(void)
  223. {
  224. return platform_driver_register(&gpio_keys_device_driver);
  225. }
  226. static void __exit gpio_keys_exit(void)
  227. {
  228. platform_driver_unregister(&gpio_keys_device_driver);
  229. }
  230. module_init(gpio_keys_init);
  231. module_exit(gpio_keys_exit);
  232. MODULE_LICENSE("GPL");
  233. MODULE_AUTHOR("Phil Blundell <pb@handhelds.org>");
  234. MODULE_DESCRIPTION("Keyboard driver for CPU GPIOs");
  235. MODULE_ALIAS("platform:gpio-keys");