bottomhalf.c 5.3 KB

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