|
| 1 | +/* |
| 2 | + * bh_thread.c - Top and bottom half interrupt handling |
| 3 | + * |
| 4 | + * Based upon the RPi example by Stefan Wendler ([email protected]) |
| 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 | + |
| 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 | + |
| 17 | +static int button_irqs[] = { -1, -1 }; |
| 18 | + |
| 19 | +/* Define GPIOs for LEDs. |
| 20 | + * FIXME: Change the numbers for the GPIO on your board. |
| 21 | + */ |
| 22 | +static struct gpio leds[] = { { 4, GPIOF_OUT_INIT_LOW, "LED 1" } }; |
| 23 | + |
| 24 | +/* Define GPIOs for BUTTONS |
| 25 | + * FIXME: Change the numbers for the GPIO on your board. |
| 26 | + */ |
| 27 | +static struct gpio buttons[] = { |
| 28 | + { 17, GPIOF_IN, "LED 1 ON BUTTON" }, |
| 29 | + { 18, GPIOF_IN, "LED 1 OFF BUTTON" }, |
| 30 | +}; |
| 31 | + |
| 32 | +/* This happens immediately, when the IRQ is triggered */ |
| 33 | +static irqreturn_t button_top_half(int irq, void *ident) |
| 34 | +{ |
| 35 | + return IRQ_WAKE_THREAD; |
| 36 | +} |
| 37 | + |
| 38 | +/* This can happen at leisure, freeing up IRQs for other high priority task */ |
| 39 | +static irqreturn_t button_bottom_half(int irq, void *ident) |
| 40 | +{ |
| 41 | + pr_info("Bottom half task starts\n"); |
| 42 | + mdelay(500); /* do something which takes a while */ |
| 43 | + pr_info("Bottom half task ends\n"); |
| 44 | + return IRQ_HANDLED; |
| 45 | +} |
| 46 | + |
| 47 | +static int __init bottomhalf_init(void) |
| 48 | +{ |
| 49 | + int ret = 0; |
| 50 | + |
| 51 | + pr_info("%s\n", __func__); |
| 52 | + |
| 53 | + /* register LED gpios */ |
| 54 | + ret = gpio_request_array(leds, ARRAY_SIZE(leds)); |
| 55 | + |
| 56 | + if (ret) { |
| 57 | + pr_err("Unable to request GPIOs for LEDs: %d\n", ret); |
| 58 | + return ret; |
| 59 | + } |
| 60 | + |
| 61 | + /* register BUTTON gpios */ |
| 62 | + ret = gpio_request_array(buttons, ARRAY_SIZE(buttons)); |
| 63 | + |
| 64 | + if (ret) { |
| 65 | + pr_err("Unable to request GPIOs for BUTTONs: %d\n", ret); |
| 66 | + goto fail1; |
| 67 | + } |
| 68 | + |
| 69 | + pr_info("Current button1 value: %d\n", gpio_get_value(buttons[0].gpio)); |
| 70 | + |
| 71 | + ret = gpio_to_irq(buttons[0].gpio); |
| 72 | + |
| 73 | + if (ret < 0) { |
| 74 | + pr_err("Unable to request IRQ: %d\n", ret); |
| 75 | + goto fail2; |
| 76 | + } |
| 77 | + |
| 78 | + button_irqs[0] = ret; |
| 79 | + |
| 80 | + pr_info("Successfully requested BUTTON1 IRQ # %d\n", button_irqs[0]); |
| 81 | + |
| 82 | + ret = request_threaded_irq( |
| 83 | + gpio_to_irq(button_irqs[0]), button_top_half, button_bottom_half, |
| 84 | + IRQF_TRIGGER_RISING | IRQF_TRIGGER_FALLING, "gpiomod#button1", NULL); |
| 85 | + |
| 86 | + if (ret) { |
| 87 | + pr_err("Unable to request IRQ: %d\n", ret); |
| 88 | + goto fail2; |
| 89 | + } |
| 90 | + |
| 91 | + ret = gpio_to_irq(buttons[1].gpio); |
| 92 | + |
| 93 | + if (ret < 0) { |
| 94 | + pr_err("Unable to request IRQ: %d\n", ret); |
| 95 | + goto fail2; |
| 96 | + } |
| 97 | + |
| 98 | + button_irqs[1] = ret; |
| 99 | + |
| 100 | + pr_info("Successfully requested BUTTON2 IRQ # %d\n", button_irqs[1]); |
| 101 | + |
| 102 | + ret = request_threaded_irq( |
| 103 | + gpio_to_irq(button_irqs[1]), button_top_half, button_bottom_half, |
| 104 | + IRQF_TRIGGER_RISING | IRQF_TRIGGER_FALLING, "gpiomod#button2", NULL); |
| 105 | + |
| 106 | + if (ret) { |
| 107 | + pr_err("Unable to request IRQ: %d\n", ret); |
| 108 | + goto fail3; |
| 109 | + } |
| 110 | + |
| 111 | + return 0; |
| 112 | + |
| 113 | +/* cleanup what has been setup so far */ |
| 114 | +fail3: |
| 115 | + free_irq(button_irqs[0], NULL); |
| 116 | + |
| 117 | +fail2: |
| 118 | + gpio_free_array(buttons, ARRAY_SIZE(leds)); |
| 119 | + |
| 120 | +fail1: |
| 121 | + gpio_free_array(leds, ARRAY_SIZE(leds)); |
| 122 | + |
| 123 | + return ret; |
| 124 | +} |
| 125 | + |
| 126 | +static void __exit bottomhalf_exit(void) |
| 127 | +{ |
| 128 | + int i; |
| 129 | + |
| 130 | + pr_info("%s\n", __func__); |
| 131 | + |
| 132 | + /* free irqs */ |
| 133 | + free_irq(button_irqs[0], NULL); |
| 134 | + free_irq(button_irqs[1], NULL); |
| 135 | + |
| 136 | + /* turn all LEDs off */ |
| 137 | + for (i = 0; i < ARRAY_SIZE(leds); i++) |
| 138 | + gpio_set_value(leds[i].gpio, 0); |
| 139 | + |
| 140 | + /* unregister */ |
| 141 | + gpio_free_array(leds, ARRAY_SIZE(leds)); |
| 142 | + gpio_free_array(buttons, ARRAY_SIZE(buttons)); |
| 143 | +} |
| 144 | + |
| 145 | +module_init(bottomhalf_init); |
| 146 | +module_exit(bottomhalf_exit); |
| 147 | + |
| 148 | +MODULE_LICENSE("GPL"); |
| 149 | +MODULE_DESCRIPTION("Interrupt with top and bottom half"); |
0 commit comments