bottomhalf.c 3.8 KB

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