gpio_keys.c 6.7 KB

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