bh_threaded.c 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149
  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(
  65. button_irqs[0], button_top_half, button_bottom_half,
  66. IRQF_TRIGGER_RISING | IRQF_TRIGGER_FALLING, "gpiomod#button1", &buttons[0]);
  67. if (ret) {
  68. pr_err("Unable to request IRQ: %d\n", ret);
  69. goto fail2;
  70. }
  71. ret = gpio_to_irq(buttons[1].gpio);
  72. if (ret < 0) {
  73. pr_err("Unable to request IRQ: %d\n", ret);
  74. goto fail2;
  75. }
  76. button_irqs[1] = ret;
  77. pr_info("Successfully requested BUTTON2 IRQ # %d\n", button_irqs[1]);
  78. ret = request_threaded_irq(
  79. button_irqs[1], button_top_half, button_bottom_half,
  80. IRQF_TRIGGER_RISING | IRQF_TRIGGER_FALLING, "gpiomod#button2", &buttons[1]);
  81. if (ret) {
  82. pr_err("Unable to request IRQ: %d\n", ret);
  83. goto fail3;
  84. }
  85. return 0;
  86. /* cleanup what has been setup so far */
  87. fail3:
  88. free_irq(button_irqs[0], NULL);
  89. fail2:
  90. gpio_free_array(buttons, ARRAY_SIZE(leds));
  91. fail1:
  92. gpio_free_array(leds, ARRAY_SIZE(leds));
  93. return ret;
  94. }
  95. static void __exit bottomhalf_exit(void)
  96. {
  97. int i;
  98. pr_info("%s\n", __func__);
  99. /* free irqs */
  100. free_irq(button_irqs[0], NULL);
  101. free_irq(button_irqs[1], NULL);
  102. /* turn all LEDs off */
  103. for (i = 0; i < ARRAY_SIZE(leds); i++)
  104. gpio_set_value(leds[i].gpio, 0);
  105. /* unregister */
  106. gpio_free_array(leds, ARRAY_SIZE(leds));
  107. gpio_free_array(buttons, ARRAY_SIZE(buttons));
  108. }
  109. module_init(bottomhalf_init);
  110. module_exit(bottomhalf_exit);
  111. MODULE_LICENSE("GPL");
  112. MODULE_DESCRIPTION("Interrupt with top and bottom half");