gpio_keys.c 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216
  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/version.h>
  12. #include <linux/init.h>
  13. #include <linux/fs.h>
  14. #include <linux/interrupt.h>
  15. #include <linux/irq.h>
  16. #include <linux/sched.h>
  17. #include <linux/pm.h>
  18. #include <linux/sysctl.h>
  19. #include <linux/proc_fs.h>
  20. #include <linux/delay.h>
  21. #include <linux/platform_device.h>
  22. #include <linux/input.h>
  23. #include <linux/gpio_keys.h>
  24. #include <asm/gpio.h>
  25. static irqreturn_t gpio_keys_isr(int irq, void *dev_id)
  26. {
  27. int i;
  28. struct platform_device *pdev = dev_id;
  29. struct gpio_keys_platform_data *pdata = pdev->dev.platform_data;
  30. struct input_dev *input = platform_get_drvdata(pdev);
  31. for (i = 0; i < pdata->nbuttons; i++) {
  32. struct gpio_keys_button *button = &pdata->buttons[i];
  33. int gpio = button->gpio;
  34. if (irq == gpio_to_irq(gpio)) {
  35. unsigned int type = button->type ?: EV_KEY;
  36. int state = (gpio_get_value(gpio) ? 1 : 0) ^ button->active_low;
  37. input_event(input, type, button->code, !!state);
  38. input_sync(input);
  39. }
  40. }
  41. return IRQ_HANDLED;
  42. }
  43. static int __devinit gpio_keys_probe(struct platform_device *pdev)
  44. {
  45. struct gpio_keys_platform_data *pdata = pdev->dev.platform_data;
  46. struct input_dev *input;
  47. int i, error;
  48. int wakeup = 0;
  49. input = input_allocate_device();
  50. if (!input)
  51. return -ENOMEM;
  52. platform_set_drvdata(pdev, input);
  53. input->evbit[0] = BIT_MASK(EV_KEY);
  54. input->name = pdev->name;
  55. input->phys = "gpio-keys/input0";
  56. input->dev.parent = &pdev->dev;
  57. input->id.bustype = BUS_HOST;
  58. input->id.vendor = 0x0001;
  59. input->id.product = 0x0001;
  60. input->id.version = 0x0100;
  61. for (i = 0; i < pdata->nbuttons; i++) {
  62. struct gpio_keys_button *button = &pdata->buttons[i];
  63. int irq = gpio_to_irq(button->gpio);
  64. unsigned int type = button->type ?: EV_KEY;
  65. if (irq < 0) {
  66. error = irq;
  67. printk(KERN_ERR
  68. "gpio-keys: "
  69. "Unable to get irq number for GPIO %d,"
  70. "error %d\n",
  71. button->gpio, error);
  72. goto fail;
  73. }
  74. error = request_irq(irq, gpio_keys_isr,
  75. IRQF_SAMPLE_RANDOM | IRQF_TRIGGER_RISING |
  76. IRQF_TRIGGER_FALLING,
  77. button->desc ? button->desc : "gpio_keys",
  78. pdev);
  79. if (error) {
  80. printk(KERN_ERR
  81. "gpio-keys: Unable to claim irq %d; error %d\n",
  82. irq, error);
  83. goto fail;
  84. }
  85. if (button->wakeup)
  86. wakeup = 1;
  87. input_set_capability(input, type, button->code);
  88. }
  89. error = input_register_device(input);
  90. if (error) {
  91. printk(KERN_ERR
  92. "gpio-keys: Unable to register input device, "
  93. "error: %d\n", error);
  94. goto fail;
  95. }
  96. device_init_wakeup(&pdev->dev, wakeup);
  97. return 0;
  98. fail:
  99. while (--i >= 0)
  100. free_irq(gpio_to_irq(pdata->buttons[i].gpio), pdev);
  101. platform_set_drvdata(pdev, NULL);
  102. input_free_device(input);
  103. return error;
  104. }
  105. static int __devexit gpio_keys_remove(struct platform_device *pdev)
  106. {
  107. struct gpio_keys_platform_data *pdata = pdev->dev.platform_data;
  108. struct input_dev *input = platform_get_drvdata(pdev);
  109. int i;
  110. device_init_wakeup(&pdev->dev, 0);
  111. for (i = 0; i < pdata->nbuttons; i++) {
  112. int irq = gpio_to_irq(pdata->buttons[i].gpio);
  113. free_irq(irq, pdev);
  114. }
  115. input_unregister_device(input);
  116. return 0;
  117. }
  118. #ifdef CONFIG_PM
  119. static int gpio_keys_suspend(struct platform_device *pdev, pm_message_t state)
  120. {
  121. struct gpio_keys_platform_data *pdata = pdev->dev.platform_data;
  122. int i;
  123. if (device_may_wakeup(&pdev->dev)) {
  124. for (i = 0; i < pdata->nbuttons; i++) {
  125. struct gpio_keys_button *button = &pdata->buttons[i];
  126. if (button->wakeup) {
  127. int irq = gpio_to_irq(button->gpio);
  128. enable_irq_wake(irq);
  129. }
  130. }
  131. }
  132. return 0;
  133. }
  134. static int gpio_keys_resume(struct platform_device *pdev)
  135. {
  136. struct gpio_keys_platform_data *pdata = pdev->dev.platform_data;
  137. int i;
  138. if (device_may_wakeup(&pdev->dev)) {
  139. for (i = 0; i < pdata->nbuttons; i++) {
  140. struct gpio_keys_button *button = &pdata->buttons[i];
  141. if (button->wakeup) {
  142. int irq = gpio_to_irq(button->gpio);
  143. disable_irq_wake(irq);
  144. }
  145. }
  146. }
  147. return 0;
  148. }
  149. #else
  150. #define gpio_keys_suspend NULL
  151. #define gpio_keys_resume NULL
  152. #endif
  153. struct platform_driver gpio_keys_device_driver = {
  154. .probe = gpio_keys_probe,
  155. .remove = __devexit_p(gpio_keys_remove),
  156. .suspend = gpio_keys_suspend,
  157. .resume = gpio_keys_resume,
  158. .driver = {
  159. .name = "gpio-keys",
  160. }
  161. };
  162. static int __init gpio_keys_init(void)
  163. {
  164. return platform_driver_register(&gpio_keys_device_driver);
  165. }
  166. static void __exit gpio_keys_exit(void)
  167. {
  168. platform_driver_unregister(&gpio_keys_device_driver);
  169. }
  170. module_init(gpio_keys_init);
  171. module_exit(gpio_keys_exit);
  172. MODULE_LICENSE("GPL");
  173. MODULE_AUTHOR("Phil Blundell <pb@handhelds.org>");
  174. MODULE_DESCRIPTION("Keyboard driver for CPU GPIOs");