bottomhalf.c 4.1 KB

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