2021-07-22 06:35:24 +08:00
|
|
|
/*
|
2021-08-08 01:24:59 +08:00
|
|
|
* intrpt.c - Handling GPIO with interrupts
|
2021-07-22 06:35:24 +08:00
|
|
|
*
|
2021-08-08 01:24:59 +08:00
|
|
|
* Based upon the RPi example by Stefan Wendler (devnull@kaltpost.de)
|
|
|
|
* from:
|
|
|
|
* https://github.com/wendlers/rpi-kmod-samples
|
2021-07-22 06:35:24 +08:00
|
|
|
*
|
2021-08-08 01:24:59 +08:00
|
|
|
* Press one button to turn on a LED and another to turn it off.
|
2021-07-22 06:35:24 +08:00
|
|
|
*/
|
|
|
|
|
|
|
|
#include <linux/gpio.h>
|
|
|
|
#include <linux/interrupt.h>
|
2023-02-23 18:41:23 +08:00
|
|
|
#include <linux/kernel.h> /* for ARRAY_SIZE() */
|
2021-07-22 06:58:13 +08:00
|
|
|
#include <linux/module.h>
|
2023-02-23 18:41:23 +08:00
|
|
|
#include <linux/printk.h>
|
2021-07-22 06:35:24 +08:00
|
|
|
|
2021-09-02 15:15:07 +08:00
|
|
|
static int button_irqs[] = { -1, -1 };
|
2021-07-22 06:35:24 +08:00
|
|
|
|
|
|
|
/* Define GPIOs for LEDs.
|
2021-08-08 01:24:59 +08:00
|
|
|
* TODO: Change the numbers for the GPIO on your board.
|
|
|
|
*/
|
2021-09-02 15:15:07 +08:00
|
|
|
static struct gpio leds[] = { { 4, GPIOF_OUT_INIT_LOW, "LED 1" } };
|
2021-07-22 06:35:24 +08:00
|
|
|
|
|
|
|
/* Define GPIOs for BUTTONS
|
2021-08-08 01:24:59 +08:00
|
|
|
* TODO: Change the numbers for the GPIO on your board.
|
|
|
|
*/
|
2021-09-02 15:15:07 +08:00
|
|
|
static struct gpio buttons[] = { { 17, GPIOF_IN, "LED 1 ON BUTTON" },
|
|
|
|
{ 18, GPIOF_IN, "LED 1 OFF BUTTON" } };
|
2021-07-22 06:35:24 +08:00
|
|
|
|
2021-08-08 01:24:59 +08:00
|
|
|
/* interrupt function triggered when a button is pressed. */
|
2021-07-22 06:35:24 +08:00
|
|
|
static irqreturn_t button_isr(int irq, void *data)
|
|
|
|
{
|
|
|
|
/* first button */
|
|
|
|
if (irq == button_irqs[0] && !gpio_get_value(leds[0].gpio))
|
2021-07-22 06:58:13 +08:00
|
|
|
gpio_set_value(leds[0].gpio, 1);
|
2021-07-22 06:35:24 +08:00
|
|
|
/* second button */
|
2021-07-22 06:58:13 +08:00
|
|
|
else if (irq == button_irqs[1] && gpio_get_value(leds[0].gpio))
|
|
|
|
gpio_set_value(leds[0].gpio, 0);
|
2021-07-22 06:35:24 +08:00
|
|
|
|
|
|
|
return IRQ_HANDLED;
|
|
|
|
}
|
|
|
|
|
2021-08-08 00:29:24 +08:00
|
|
|
static int __init intrpt_init(void)
|
2021-07-22 06:35:24 +08:00
|
|
|
{
|
|
|
|
int ret = 0;
|
|
|
|
|
|
|
|
pr_info("%s\n", __func__);
|
|
|
|
|
|
|
|
/* register LED gpios */
|
|
|
|
ret = gpio_request_array(leds, ARRAY_SIZE(leds));
|
|
|
|
|
|
|
|
if (ret) {
|
|
|
|
pr_err("Unable to request GPIOs for LEDs: %d\n", ret);
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* register BUTTON gpios */
|
|
|
|
ret = gpio_request_array(buttons, ARRAY_SIZE(buttons));
|
|
|
|
|
|
|
|
if (ret) {
|
|
|
|
pr_err("Unable to request GPIOs for BUTTONs: %d\n", ret);
|
|
|
|
goto fail1;
|
|
|
|
}
|
|
|
|
|
2021-07-22 06:58:13 +08:00
|
|
|
pr_info("Current button1 value: %d\n", gpio_get_value(buttons[0].gpio));
|
2021-07-22 06:35:24 +08:00
|
|
|
|
|
|
|
ret = gpio_to_irq(buttons[0].gpio);
|
|
|
|
|
|
|
|
if (ret < 0) {
|
|
|
|
pr_err("Unable to request IRQ: %d\n", ret);
|
|
|
|
goto fail2;
|
|
|
|
}
|
|
|
|
|
|
|
|
button_irqs[0] = ret;
|
|
|
|
|
2021-07-22 06:58:13 +08:00
|
|
|
pr_info("Successfully requested BUTTON1 IRQ # %d\n", button_irqs[0]);
|
2021-07-22 06:35:24 +08:00
|
|
|
|
|
|
|
ret = request_irq(button_irqs[0], button_isr,
|
|
|
|
IRQF_TRIGGER_RISING | IRQF_TRIGGER_FALLING,
|
|
|
|
"gpiomod#button1", NULL);
|
|
|
|
|
|
|
|
if (ret) {
|
|
|
|
pr_err("Unable to request IRQ: %d\n", ret);
|
|
|
|
goto fail2;
|
|
|
|
}
|
|
|
|
|
|
|
|
ret = gpio_to_irq(buttons[1].gpio);
|
|
|
|
|
|
|
|
if (ret < 0) {
|
|
|
|
pr_err("Unable to request IRQ: %d\n", ret);
|
|
|
|
goto fail2;
|
|
|
|
}
|
|
|
|
|
|
|
|
button_irqs[1] = ret;
|
|
|
|
|
2021-07-22 06:58:13 +08:00
|
|
|
pr_info("Successfully requested BUTTON2 IRQ # %d\n", button_irqs[1]);
|
2021-07-22 06:35:24 +08:00
|
|
|
|
|
|
|
ret = request_irq(button_irqs[1], button_isr,
|
|
|
|
IRQF_TRIGGER_RISING | IRQF_TRIGGER_FALLING,
|
|
|
|
"gpiomod#button2", NULL);
|
|
|
|
|
|
|
|
if (ret) {
|
|
|
|
pr_err("Unable to request IRQ: %d\n", ret);
|
|
|
|
goto fail3;
|
|
|
|
}
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
|
|
|
|
/* cleanup what has been setup so far */
|
|
|
|
fail3:
|
|
|
|
free_irq(button_irqs[0], NULL);
|
|
|
|
|
|
|
|
fail2:
|
|
|
|
gpio_free_array(buttons, ARRAY_SIZE(leds));
|
|
|
|
|
|
|
|
fail1:
|
|
|
|
gpio_free_array(leds, ARRAY_SIZE(leds));
|
|
|
|
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
2021-08-08 00:29:24 +08:00
|
|
|
static void __exit intrpt_exit(void)
|
2021-07-22 06:35:24 +08:00
|
|
|
{
|
|
|
|
int i;
|
|
|
|
|
|
|
|
pr_info("%s\n", __func__);
|
|
|
|
|
|
|
|
/* free irqs */
|
|
|
|
free_irq(button_irqs[0], NULL);
|
|
|
|
free_irq(button_irqs[1], NULL);
|
|
|
|
|
|
|
|
/* turn all LEDs off */
|
|
|
|
for (i = 0; i < ARRAY_SIZE(leds); i++)
|
|
|
|
gpio_set_value(leds[i].gpio, 0);
|
|
|
|
|
|
|
|
/* unregister */
|
|
|
|
gpio_free_array(leds, ARRAY_SIZE(leds));
|
|
|
|
gpio_free_array(buttons, ARRAY_SIZE(buttons));
|
|
|
|
}
|
|
|
|
|
2021-08-08 00:29:24 +08:00
|
|
|
module_init(intrpt_init);
|
|
|
|
module_exit(intrpt_exit);
|
|
|
|
|
2021-07-22 06:35:24 +08:00
|
|
|
MODULE_LICENSE("GPL");
|
|
|
|
MODULE_DESCRIPTION("Handle some GPIO interrupts");
|