gpio_keys.c 6.9 KB

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