bottomhalf.c 3.8 KB

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