From 9b87645a3d4a36fc5fa45b020991381d324b85eb Mon Sep 17 00:00:00 2001 From: Wolfgang Hottgenroth Date: Fri, 13 Jun 2025 10:50:29 +0100 Subject: [PATCH] interrupt added and working --- driver/counter.c | 85 +++++++++++++++++++++++++++++++++++++++++++++- driver/counter.dts | 4 +++ 2 files changed, 88 insertions(+), 1 deletion(-) diff --git a/driver/counter.c b/driver/counter.c index ff2ea69..68b6b4f 100644 --- a/driver/counter.c +++ b/driver/counter.c @@ -6,6 +6,8 @@ #include #include #include +#include +#include #include "ls7366r.h" @@ -207,11 +209,82 @@ static void dt_ls7366r_remove(struct spi_device *client) { } +/* interrupt stuff */ +static int dt_interrupt_probe(struct platform_device *pdev); +static void dt_interrupt_remove(struct platform_device *pdev); + +static struct of_device_id interrupt_driver_ids[] = { + { + .compatible = "hottis,intr", + }, + {}, +}; +MODULE_DEVICE_TABLE(of, interrupt_driver_ids); + +static struct platform_driver interrupt_driver = { + .probe = dt_interrupt_probe, + .remove = dt_interrupt_remove, + .driver = { + .name = "interrupt_device_driver", + .of_match_table = interrupt_driver_ids, + }, +}; + +static struct gpio_desc *counter_interrupt; +static int irq_number; + +// ISR +static irqreturn_t counter_isr(int irq, void *dev_id) { + printk("counter - interrupt received\n"); + return IRQ_HANDLED; +} + +static int dt_interrupt_probe(struct platform_device *pdev) { + struct device *dev = &pdev->dev; + + printk("counter - interrupt probing\n"); + + counter_interrupt = gpiod_get(dev, "intr", GPIOD_IN); + if (IS_ERR(counter_interrupt)) { + printk("counter - Error! Could not setup the GPIO intr\n"); + return -1 * IS_ERR(counter_interrupt); + } + + irq_number = gpiod_to_irq(counter_interrupt); + if (irq_number < 0) { + printk("counter - Error! Could not map GPIO to IRQ\n"); + gpiod_put(counter_interrupt); + return irq_number; + } + printk("counter - Info! IRQ %d assigned\n", irq_number); + + int ret = request_irq(irq_number, counter_isr, + IRQF_TRIGGER_RISING, "gpio19_irq", dev); + if (ret) { + printk("counter - Error! Could not request IRQ\n"); + gpiod_put(counter_interrupt); + return ret; + } + + printk("counter - Info! Interrupt assigned\n"); + + return 0; +} + +static void dt_interrupt_remove(struct platform_device *pdev) { + printk("counter - interrupt removing\n"); + struct device *dev = &pdev->dev; + free_irq(irq_number, dev); + gpiod_put(counter_interrupt); +} + + + /* module loading and unloading */ static int __init my_init(void) { printk("counter - Loading the leds driver...\n"); if(platform_driver_register(&leds_driver)) { - printk("dt_gpio - Error! Could not load driver\n"); + printk("dt_gpio - Error! Could not load leds driver\n"); return -1; } @@ -221,6 +294,13 @@ static int __init my_init(void) { return -1; } + printk("counter - Loading the interrupt driver...\n"); + if(platform_driver_register(&interrupt_driver)) { + printk("dt_gpio - Error! Could not load interrupt driver\n"); + return -1; + } + + printk("counter - red led on\n"); gpiod_set_value(red_led, 1); @@ -236,6 +316,9 @@ static void __exit my_exit(void) { printk("counter - Unloading the ls7366r driver...\n"); spi_unregister_driver(&ls7366r_driver); + + printk("counter - Unloading the interrupt driver...\n"); + platform_driver_unregister(&interrupt_driver); } module_init(my_init); diff --git a/driver/counter.dts b/driver/counter.dts index 289f687..615b71e 100644 --- a/driver/counter.dts +++ b/driver/counter.dts @@ -11,6 +11,10 @@ red-led-gpio = <&gpio 26 0>; blue-led-gpio = <&gpio 21 0>; }; + intr { + compatible = "hottis,intr"; + intr-gpio = <&gpio 19 0>; + }; }; };