1
0

bh_threaded.c 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215
  1. /*
  2. * bh_thread.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 a LED and another to turn it off
  9. */
  10. #include <linux/module.h>
  11. #include <linux/kernel.h>
  12. #include <linux/gpio.h>
  13. #include <linux/delay.h>
  14. #include <linux/interrupt.h>
  15. #include <linux/version.h>
  16. #if LINUX_VERSION_CODE >= KERNEL_VERSION(6, 10, 0)
  17. #define NO_GPIO_REQUEST_ARRAY
  18. #endif
  19. static int button_irqs[] = { -1, -1 };
  20. /* Define GPIOs for LEDs.
  21. * FIXME: Change the numbers for the GPIO on your board.
  22. */
  23. static struct gpio leds[] = { { 4, GPIOF_OUT_INIT_LOW, "LED 1" } };
  24. /* Define GPIOs for BUTTONS
  25. * FIXME: Change the numbers for the GPIO on your board.
  26. */
  27. static struct gpio buttons[] = {
  28. { 17, GPIOF_IN, "LED 1 ON BUTTON" },
  29. { 18, GPIOF_IN, "LED 1 OFF BUTTON" },
  30. };
  31. /* This happens immediately, when the IRQ is triggered */
  32. static irqreturn_t button_top_half(int irq, void *ident)
  33. {
  34. return IRQ_WAKE_THREAD;
  35. }
  36. /* This can happen at leisure, freeing up IRQs for other high priority task */
  37. static irqreturn_t button_bottom_half(int irq, void *ident)
  38. {
  39. pr_info("Bottom half task starts\n");
  40. mdelay(500); /* do something which takes a while */
  41. pr_info("Bottom half task ends\n");
  42. return IRQ_HANDLED;
  43. }
  44. static int __init bottomhalf_init(void)
  45. {
  46. int ret = 0;
  47. pr_info("%s\n", __func__);
  48. /* register LED gpios */
  49. #ifdef NO_GPIO_REQUEST_ARRAY
  50. ret = gpio_request(leds[0].gpio, leds[0].label);
  51. #else
  52. ret = gpio_request_array(leds, ARRAY_SIZE(leds));
  53. #endif
  54. if (ret) {
  55. pr_err("Unable to request GPIOs for LEDs: %d\n", ret);
  56. return ret;
  57. }
  58. /* register BUTTON gpios */
  59. #ifdef NO_GPIO_REQUEST_ARRAY
  60. ret = gpio_request(buttons[0].gpio, buttons[0].label);
  61. if (ret) {
  62. pr_err("Unable to request GPIOs for BUTTONs: %d\n", ret);
  63. goto fail1;
  64. }
  65. ret = gpio_request(buttons[1].gpio, buttons[1].label);
  66. if (ret) {
  67. pr_err("Unable to request GPIOs for BUTTONs: %d\n", ret);
  68. goto fail2;
  69. }
  70. #else
  71. ret = gpio_request_array(buttons, ARRAY_SIZE(buttons));
  72. if (ret) {
  73. pr_err("Unable to request GPIOs for BUTTONs: %d\n", ret);
  74. goto fail1;
  75. }
  76. #endif
  77. pr_info("Current button1 value: %d\n", gpio_get_value(buttons[0].gpio));
  78. ret = gpio_to_irq(buttons[0].gpio);
  79. if (ret < 0) {
  80. pr_err("Unable to request IRQ: %d\n", ret);
  81. #ifdef NO_GPIO_REQUEST_ARRAY
  82. goto fail3;
  83. #else
  84. goto fail2;
  85. #endif
  86. }
  87. button_irqs[0] = ret;
  88. pr_info("Successfully requested BUTTON1 IRQ # %d\n", button_irqs[0]);
  89. ret = request_threaded_irq(button_irqs[0], button_top_half,
  90. button_bottom_half,
  91. IRQF_TRIGGER_RISING | IRQF_TRIGGER_FALLING,
  92. "gpiomod#button1", &buttons[0]);
  93. if (ret) {
  94. pr_err("Unable to request IRQ: %d\n", ret);
  95. #ifdef NO_GPIO_REQUEST_ARRAY
  96. goto fail3;
  97. #else
  98. goto fail2;
  99. #endif
  100. }
  101. ret = gpio_to_irq(buttons[1].gpio);
  102. if (ret < 0) {
  103. pr_err("Unable to request IRQ: %d\n", ret);
  104. #ifdef NO_GPIO_REQUEST_ARRAY
  105. goto fail3;
  106. #else
  107. goto fail2;
  108. #endif
  109. }
  110. button_irqs[1] = ret;
  111. pr_info("Successfully requested BUTTON2 IRQ # %d\n", button_irqs[1]);
  112. ret = request_threaded_irq(button_irqs[1], button_top_half,
  113. button_bottom_half,
  114. IRQF_TRIGGER_RISING | IRQF_TRIGGER_FALLING,
  115. "gpiomod#button2", &buttons[1]);
  116. if (ret) {
  117. pr_err("Unable to request IRQ: %d\n", ret);
  118. #ifdef NO_GPIO_REQUEST_ARRAY
  119. goto fail4;
  120. #else
  121. goto fail3;
  122. #endif
  123. }
  124. return 0;
  125. /* cleanup what has been setup so far */
  126. #ifdef NO_GPIO_REQUEST_ARRAY
  127. fail4:
  128. free_irq(button_irqs[0], NULL);
  129. fail3:
  130. gpio_free(buttons[1].gpio);
  131. fail2:
  132. gpio_free(buttons[0].gpio);
  133. fail1:
  134. gpio_free(leds[0].gpio);
  135. #else
  136. fail3:
  137. free_irq(button_irqs[0], NULL);
  138. fail2:
  139. gpio_free_array(buttons, ARRAY_SIZE(leds));
  140. fail1:
  141. gpio_free_array(leds, ARRAY_SIZE(leds));
  142. #endif
  143. return ret;
  144. }
  145. static void __exit bottomhalf_exit(void)
  146. {
  147. pr_info("%s\n", __func__);
  148. /* free irqs */
  149. free_irq(button_irqs[0], NULL);
  150. free_irq(button_irqs[1], NULL);
  151. /* turn all LEDs off */
  152. #ifdef NO_GPIO_REQUEST_ARRAY
  153. gpio_set_value(leds[0].gpio, 0);
  154. #else
  155. int i;
  156. for (i = 0; i < ARRAY_SIZE(leds); i++)
  157. gpio_set_value(leds[i].gpio, 0);
  158. #endif
  159. /* unregister */
  160. #ifdef NO_GPIO_REQUEST_ARRAY
  161. gpio_free(leds[0].gpio);
  162. gpio_free(buttons[0].gpio);
  163. gpio_free(buttons[1].gpio);
  164. #else
  165. gpio_free_array(leds, ARRAY_SIZE(leds));
  166. gpio_free_array(buttons, ARRAY_SIZE(buttons));
  167. #endif
  168. }
  169. module_init(bottomhalf_init);
  170. module_exit(bottomhalf_exit);
  171. MODULE_LICENSE("GPL");
  172. MODULE_DESCRIPTION("Interrupt with top and bottom half");