gpio_keys.c 6.8 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 <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_TRIGGER_RISING | IRQF_TRIGGER_FALLING,
  125. button->desc ? button->desc : "gpio_keys",
  126. bdata);
  127. if (error) {
  128. pr_err("gpio-keys: Unable to claim irq %d; error %d\n",
  129. irq, error);
  130. gpio_free(button->gpio);
  131. goto fail2;
  132. }
  133. if (button->wakeup)
  134. wakeup = 1;
  135. input_set_capability(input, type, button->code);
  136. }
  137. error = input_register_device(input);
  138. if (error) {
  139. pr_err("gpio-keys: Unable to register input device, "
  140. "error: %d\n", error);
  141. goto fail2;
  142. }
  143. device_init_wakeup(&pdev->dev, wakeup);
  144. return 0;
  145. fail2:
  146. while (--i >= 0) {
  147. free_irq(gpio_to_irq(pdata->buttons[i].gpio), &ddata->data[i]);
  148. if (pdata->buttons[i].debounce_interval)
  149. del_timer_sync(&ddata->data[i].timer);
  150. cancel_work_sync(&ddata->data[i].work);
  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, &ddata->data[i]);
  169. if (pdata->buttons[i].debounce_interval)
  170. del_timer_sync(&ddata->data[i].timer);
  171. cancel_work_sync(&ddata->data[i].work);
  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");