gpio_keys.c 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292
  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 input_dev *input;
  68. int i, error;
  69. int wakeup = 0;
  70. ddata = kzalloc(sizeof(struct gpio_keys_drvdata) +
  71. pdata->nbuttons * sizeof(struct gpio_button_data),
  72. GFP_KERNEL);
  73. input = input_allocate_device();
  74. if (!ddata || !input) {
  75. error = -ENOMEM;
  76. goto fail1;
  77. }
  78. platform_set_drvdata(pdev, ddata);
  79. input->name = pdev->name;
  80. input->phys = "gpio-keys/input0";
  81. input->dev.parent = &pdev->dev;
  82. input->id.bustype = BUS_HOST;
  83. input->id.vendor = 0x0001;
  84. input->id.product = 0x0001;
  85. input->id.version = 0x0100;
  86. /* Enable auto repeat feature of Linux input subsystem */
  87. if (pdata->rep)
  88. __set_bit(EV_REP, input->evbit);
  89. ddata->input = input;
  90. for (i = 0; i < pdata->nbuttons; i++) {
  91. struct gpio_keys_button *button = &pdata->buttons[i];
  92. struct gpio_button_data *bdata = &ddata->data[i];
  93. int irq;
  94. unsigned int type = button->type ?: EV_KEY;
  95. bdata->input = input;
  96. bdata->button = button;
  97. setup_timer(&bdata->timer,
  98. gpio_keys_timer, (unsigned long)bdata);
  99. INIT_WORK(&bdata->work, gpio_keys_report_event);
  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_SHARED |
  125. IRQF_TRIGGER_RISING | IRQF_TRIGGER_FALLING,
  126. button->desc ? button->desc : "gpio_keys",
  127. bdata);
  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), &ddata->data[i]);
  149. if (pdata->buttons[i].debounce_interval)
  150. del_timer_sync(&ddata->data[i].timer);
  151. cancel_work_sync(&ddata->data[i].work);
  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, &ddata->data[i]);
  170. if (pdata->buttons[i].debounce_interval)
  171. del_timer_sync(&ddata->data[i].timer);
  172. cancel_work_sync(&ddata->data[i].work);
  173. gpio_free(pdata->buttons[i].gpio);
  174. }
  175. input_unregister_device(input);
  176. return 0;
  177. }
  178. #ifdef CONFIG_PM
  179. static int gpio_keys_suspend(struct device *dev)
  180. {
  181. struct platform_device *pdev = to_platform_device(dev);
  182. struct gpio_keys_platform_data *pdata = pdev->dev.platform_data;
  183. int i;
  184. if (device_may_wakeup(&pdev->dev)) {
  185. for (i = 0; i < pdata->nbuttons; i++) {
  186. struct gpio_keys_button *button = &pdata->buttons[i];
  187. if (button->wakeup) {
  188. int irq = gpio_to_irq(button->gpio);
  189. enable_irq_wake(irq);
  190. }
  191. }
  192. }
  193. return 0;
  194. }
  195. static int gpio_keys_resume(struct device *dev)
  196. {
  197. struct platform_device *pdev = to_platform_device(dev);
  198. struct gpio_keys_platform_data *pdata = pdev->dev.platform_data;
  199. int i;
  200. if (device_may_wakeup(&pdev->dev)) {
  201. for (i = 0; i < pdata->nbuttons; i++) {
  202. struct gpio_keys_button *button = &pdata->buttons[i];
  203. if (button->wakeup) {
  204. int irq = gpio_to_irq(button->gpio);
  205. disable_irq_wake(irq);
  206. }
  207. }
  208. }
  209. return 0;
  210. }
  211. static const struct dev_pm_ops gpio_keys_pm_ops = {
  212. .suspend = gpio_keys_suspend,
  213. .resume = gpio_keys_resume,
  214. };
  215. #endif
  216. static struct platform_driver gpio_keys_device_driver = {
  217. .probe = gpio_keys_probe,
  218. .remove = __devexit_p(gpio_keys_remove),
  219. .driver = {
  220. .name = "gpio-keys",
  221. .owner = THIS_MODULE,
  222. #ifdef CONFIG_PM
  223. .pm = &gpio_keys_pm_ops,
  224. #endif
  225. }
  226. };
  227. static int __init gpio_keys_init(void)
  228. {
  229. return platform_driver_register(&gpio_keys_device_driver);
  230. }
  231. static void __exit gpio_keys_exit(void)
  232. {
  233. platform_driver_unregister(&gpio_keys_device_driver);
  234. }
  235. module_init(gpio_keys_init);
  236. module_exit(gpio_keys_exit);
  237. MODULE_LICENSE("GPL");
  238. MODULE_AUTHOR("Phil Blundell <pb@handhelds.org>");
  239. MODULE_DESCRIPTION("Keyboard driver for CPU GPIOs");
  240. MODULE_ALIAS("platform:gpio-keys");