bottomhalf.c 4.1 KB

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