bottomhalf.c 3.9 KB

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