bottomhalf.c 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233
  1. /*
  2. * bottomhalf.c - Top and bottom half interrupt handling
  3. *
  4. * Based upon the RPi example by Stefan Wendler (devnull@kaltpost.de)
  5. * from:
  6. * https://github.com/wendlers/rpi-kmod-samples
  7. *
  8. * Press one button to turn on an LED and another to turn it off
  9. */
  10. #include <linux/delay.h>
  11. #include <linux/gpio.h>
  12. #include <linux/interrupt.h>
  13. #include <linux/module.h>
  14. #include <linux/printk.h>
  15. #include <linux/init.h>
  16. #include <linux/version.h>
  17. #include <linux/workqueue.h>
  18. #if LINUX_VERSION_CODE >= KERNEL_VERSION(6, 10, 0)
  19. #define NO_GPIO_REQUEST_ARRAY
  20. #endif
  21. /* Macro DECLARE_TASKLET_OLD exists for compatibility.
  22. * See https://lwn.net/Articles/830964/
  23. */
  24. #ifndef DECLARE_TASKLET_OLD
  25. #define DECLARE_TASKLET_OLD(arg1, arg2) DECLARE_TASKLET(arg1, arg2, 0L)
  26. #endif
  27. static int button_irqs[] = { -1, -1 };
  28. /* Define GPIOs for LEDs.
  29. * TODO: Change the numbers for the GPIO on your board.
  30. */
  31. static struct gpio leds[] = { { 4, GPIOF_OUT_INIT_LOW, "LED 1" } };
  32. /* Define GPIOs for BUTTONS
  33. * TODO: Change the numbers for the GPIO on your board.
  34. */
  35. static struct gpio buttons[] = {
  36. { 17, GPIOF_IN, "LED 1 ON BUTTON" },
  37. { 18, GPIOF_IN, "LED 1 OFF BUTTON" },
  38. };
  39. /* Workqueue function containing some non-trivial amount of processing */
  40. static void bottomhalf_work_fn(struct work_struct *work)
  41. {
  42. pr_info("Bottom half workqueue starts\n");
  43. /* do something which takes a while */
  44. msleep(500);
  45. pr_info("Bottom half workqueue ends\n");
  46. }
  47. static DECLARE_WORK(bottomhalf_work, bottomhalf_work_fn);
  48. /* interrupt function triggered when a button is pressed */
  49. static irqreturn_t button_isr(int irq, void *data)
  50. {
  51. /* Do something quickly right now */
  52. if (irq == button_irqs[0] && !gpio_get_value(leds[0].gpio))
  53. gpio_set_value(leds[0].gpio, 1);
  54. else if (irq == button_irqs[1] && gpio_get_value(leds[0].gpio))
  55. gpio_set_value(leds[0].gpio, 0);
  56. /* Do the rest at leisure via the scheduler */
  57. schedule_work(&bottomhalf_work);
  58. return IRQ_HANDLED;
  59. }
  60. static int __init bottomhalf_init(void)
  61. {
  62. int ret = 0;
  63. pr_info("%s\n", __func__);
  64. /* register LED gpios */
  65. #ifdef NO_GPIO_REQUEST_ARRAY
  66. ret = gpio_request(leds[0].gpio, leds[0].label);
  67. #else
  68. ret = gpio_request_array(leds, ARRAY_SIZE(leds));
  69. #endif
  70. if (ret) {
  71. pr_err("Unable to request GPIOs for LEDs: %d\n", ret);
  72. return ret;
  73. }
  74. /* register BUTTON gpios */
  75. #ifdef NO_GPIO_REQUEST_ARRAY
  76. ret = gpio_request(buttons[0].gpio, buttons[0].label);
  77. if (ret) {
  78. pr_err("Unable to request GPIOs for BUTTONs: %d\n", ret);
  79. goto fail1;
  80. }
  81. ret = gpio_request(buttons[1].gpio, buttons[1].label);
  82. if (ret) {
  83. pr_err("Unable to request GPIOs for BUTTONs: %d\n", ret);
  84. goto fail2;
  85. }
  86. #else
  87. ret = gpio_request_array(buttons, ARRAY_SIZE(buttons));
  88. if (ret) {
  89. pr_err("Unable to request GPIOs for BUTTONs: %d\n", ret);
  90. goto fail1;
  91. }
  92. #endif
  93. pr_info("Current button1 value: %d\n", gpio_get_value(buttons[0].gpio));
  94. ret = gpio_to_irq(buttons[0].gpio);
  95. if (ret < 0) {
  96. pr_err("Unable to request IRQ: %d\n", ret);
  97. #ifdef NO_GPIO_REQUEST_ARRAY
  98. goto fail3;
  99. #else
  100. goto fail2;
  101. #endif
  102. }
  103. button_irqs[0] = ret;
  104. pr_info("Successfully requested BUTTON1 IRQ # %d\n", button_irqs[0]);
  105. ret = request_irq(button_irqs[0], button_isr,
  106. IRQF_TRIGGER_RISING | IRQF_TRIGGER_FALLING,
  107. "gpiomod#button1", NULL);
  108. if (ret) {
  109. pr_err("Unable to request IRQ: %d\n", ret);
  110. #ifdef NO_GPIO_REQUEST_ARRAY
  111. goto fail3;
  112. #else
  113. goto fail2;
  114. #endif
  115. }
  116. ret = gpio_to_irq(buttons[1].gpio);
  117. if (ret < 0) {
  118. pr_err("Unable to request IRQ: %d\n", ret);
  119. #ifdef NO_GPIO_REQUEST_ARRAY
  120. goto fail3;
  121. #else
  122. goto fail2;
  123. #endif
  124. }
  125. button_irqs[1] = ret;
  126. pr_info("Successfully requested BUTTON2 IRQ # %d\n", button_irqs[1]);
  127. ret = request_irq(button_irqs[1], button_isr,
  128. IRQF_TRIGGER_RISING | IRQF_TRIGGER_FALLING,
  129. "gpiomod#button2", NULL);
  130. if (ret) {
  131. pr_err("Unable to request IRQ: %d\n", ret);
  132. #ifdef NO_GPIO_REQUEST_ARRAY
  133. goto fail4;
  134. #else
  135. goto fail3;
  136. #endif
  137. }
  138. return 0;
  139. /* cleanup what has been setup so far */
  140. #ifdef NO_GPIO_REQUEST_ARRAY
  141. fail4:
  142. free_irq(button_irqs[0], NULL);
  143. fail3:
  144. gpio_free(buttons[1].gpio);
  145. fail2:
  146. gpio_free(buttons[0].gpio);
  147. fail1:
  148. gpio_free(leds[0].gpio);
  149. #else
  150. fail3:
  151. free_irq(button_irqs[0], NULL);
  152. fail2:
  153. gpio_free_array(buttons, ARRAY_SIZE(leds));
  154. fail1:
  155. gpio_free_array(leds, ARRAY_SIZE(leds));
  156. #endif
  157. return ret;
  158. }
  159. static void __exit bottomhalf_exit(void)
  160. {
  161. pr_info("%s\n", __func__);
  162. /* free irqs */
  163. free_irq(button_irqs[0], NULL);
  164. free_irq(button_irqs[1], NULL);
  165. /* turn all LEDs off */
  166. #ifdef NO_GPIO_REQUEST_ARRAY
  167. gpio_set_value(leds[0].gpio, 0);
  168. #else
  169. int i;
  170. for (i = 0; i < ARRAY_SIZE(leds); i++)
  171. gpio_set_value(leds[i].gpio, 0);
  172. #endif
  173. /* unregister */
  174. #ifdef NO_GPIO_REQUEST_ARRAY
  175. gpio_free(leds[0].gpio);
  176. gpio_free(buttons[0].gpio);
  177. gpio_free(buttons[1].gpio);
  178. #else
  179. gpio_free_array(leds, ARRAY_SIZE(leds));
  180. gpio_free_array(buttons, ARRAY_SIZE(buttons));
  181. #endif
  182. }
  183. module_init(bottomhalf_init);
  184. module_exit(bottomhalf_exit);
  185. MODULE_LICENSE("GPL");
  186. MODULE_DESCRIPTION("Interrupt with top and bottom half");