gpio_keys.c 7.4 KB

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