Request a managed IRQ with devm_request_irq
Register an interrupt handler with devm_request_irq() so the driver core frees the IRQ on unbind, and see the registered name in /proc/interrupts.
Every request_irq() needs a matching free_irq() on each error path and in remove(). devm_request_irq() ties the interrupt to the device, so the driver core releases it on a failed probe and on unbind. The arguments are the device, the IRQ number, the handler, flags, a name for /proc/interrupts, and a cookie that is passed back to the handler.
#include <linux/interrupt.h>
#include <linux/platform_device.h>
static irqreturn_t demo_isr(int irq, void *dev_id)
{
struct demo *d = dev_id;
d->count++;
return IRQ_HANDLED;
}
static int demo_probe(struct platform_device *pdev)
{
struct demo *d = devm_kzalloc(&pdev->dev, sizeof(*d), GFP_KERNEL);
int irq = platform_get_irq(pdev, 0);
if (!d || irq < 0)
return d ? irq : -ENOMEM;
return devm_request_irq(&pdev->dev, irq, demo_isr, 0,
dev_name(&pdev->dev), d);
}After a few interrupts, /proc/interrupts has a row named after the device:
42: 117 0 GICv3 45 Level 10000000.demoThe counts and controller vary by board. The trap is ordering. Devres frees resources after remove() returns, so the interrupt can still fire while your remove code has already torn down the state the handler touches. Quiesce the hardware at the start of remove(), or call devm_free_irq() yourself at that point.
Do not pass a pointer to stack memory as the cookie; it must outlive the registration. See the interrupt API and devres.