bh_threaded.c 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151
  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. static int button_irqs[] = { -1, -1 };
  16. /* Define GPIOs for LEDs.
  17. * FIXME: Change the numbers for the GPIO on your board.
  18. */
  19. static struct gpio leds[] = { { 4, GPIOF_OUT_INIT_LOW, "LED 1" } };
  20. /* Define GPIOs for BUTTONS
  21. * FIXME: Change the numbers for the GPIO on your board.
  22. */
  23. static struct gpio buttons[] = {
  24. { 17, GPIOF_IN, "LED 1 ON BUTTON" },
  25. { 18, GPIOF_IN, "LED 1 OFF BUTTON" },
  26. };
  27. /* This happens immediately, when the IRQ is triggered */
  28. static irqreturn_t button_top_half(int irq, void *ident)
  29. {
  30. return IRQ_WAKE_THREAD;
  31. }
  32. /* This can happen at leisure, freeing up IRQs for other high priority task */
  33. static irqreturn_t button_bottom_half(int irq, void *ident)
  34. {
  35. pr_info("Bottom half task starts\n");
  36. mdelay(500); /* do something which takes a while */
  37. pr_info("Bottom half task ends\n");
  38. return IRQ_HANDLED;
  39. }
  40. static int __init bottomhalf_init(void)
  41. {
  42. int ret = 0;
  43. pr_info("%s\n", __func__);
  44. /* register LED gpios */
  45. ret = gpio_request_array(leds, ARRAY_SIZE(leds));
  46. if (ret) {
  47. pr_err("Unable to request GPIOs for LEDs: %d\n", ret);
  48. return ret;
  49. }
  50. /* register BUTTON gpios */
  51. ret = gpio_request_array(buttons, ARRAY_SIZE(buttons));
  52. if (ret) {
  53. pr_err("Unable to request GPIOs for BUTTONs: %d\n", ret);
  54. goto fail1;
  55. }
  56. pr_info("Current button1 value: %d\n", gpio_get_value(buttons[0].gpio));
  57. ret = gpio_to_irq(buttons[0].gpio);
  58. if (ret < 0) {
  59. pr_err("Unable to request IRQ: %d\n", ret);
  60. goto fail2;
  61. }
  62. button_irqs[0] = ret;
  63. pr_info("Successfully requested BUTTON1 IRQ # %d\n", button_irqs[0]);
  64. ret = request_threaded_irq(button_irqs[0], button_top_half,
  65. button_bottom_half,
  66. IRQF_TRIGGER_RISING | IRQF_TRIGGER_FALLING,
  67. "gpiomod#button1", &buttons[0]);
  68. if (ret) {
  69. pr_err("Unable to request IRQ: %d\n", ret);
  70. goto fail2;
  71. }
  72. ret = gpio_to_irq(buttons[1].gpio);
  73. if (ret < 0) {
  74. pr_err("Unable to request IRQ: %d\n", ret);
  75. goto fail2;
  76. }
  77. button_irqs[1] = ret;
  78. pr_info("Successfully requested BUTTON2 IRQ # %d\n", button_irqs[1]);
  79. ret = request_threaded_irq(button_irqs[1], button_top_half,
  80. button_bottom_half,
  81. IRQF_TRIGGER_RISING | IRQF_TRIGGER_FALLING,
  82. "gpiomod#button2", &buttons[1]);
  83. if (ret) {
  84. pr_err("Unable to request IRQ: %d\n", ret);
  85. goto fail3;
  86. }
  87. return 0;
  88. /* cleanup what has been setup so far */
  89. fail3:
  90. free_irq(button_irqs[0], NULL);
  91. fail2:
  92. gpio_free_array(buttons, ARRAY_SIZE(leds));
  93. fail1:
  94. gpio_free_array(leds, ARRAY_SIZE(leds));
  95. return ret;
  96. }
  97. static void __exit bottomhalf_exit(void)
  98. {
  99. int i;
  100. pr_info("%s\n", __func__);
  101. /* free irqs */
  102. free_irq(button_irqs[0], NULL);
  103. free_irq(button_irqs[1], NULL);
  104. /* turn all LEDs off */
  105. for (i = 0; i < ARRAY_SIZE(leds); i++)
  106. gpio_set_value(leds[i].gpio, 0);
  107. /* unregister */
  108. gpio_free_array(leds, ARRAY_SIZE(leds));
  109. gpio_free_array(buttons, ARRAY_SIZE(buttons));
  110. }
  111. module_init(bottomhalf_init);
  112. module_exit(bottomhalf_exit);
  113. MODULE_LICENSE("GPL");
  114. MODULE_DESCRIPTION("Interrupt with top and bottom half");