gpio_keys.c 6.4 KB

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