bottomhalf.c 3.9 KB

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