# How to apply for GPIO interrupt in kernel



## Ayanami (May 27, 2020)

Hi everybody,
Recently I transplanted an i2c device to FreeBSD on Raspberry 3b+, and needed to use a GPIO interrupt function. When using the gpio_alloc_intr_resource () function, it always returned NULL. I don’t know what went wrong.
And attach DTS configuration :

```
touchscreen@5d {
     compatible = "goodix,gt9886";
     reg = <0x5d>;
     interrupt-parent = <&gpio>;
     interrupts = <6 IRQ_TYPE_EDGE_FALLING>;
     goodix,irq-gpio = <&gpio 6 GPIO_ACTIVE_HIGH>;
};
```


----------



## Alexander Mishin (Aug 23, 2021)

Sorry, for I late.

It depends on a dev You are passing to the function. I did it somehow like this:

```
struct yourdriver_softc *sc = device_get_softc(dev);
uint32_t pincaps;
int err;

if ((err = gpio_pin_getcaps(sc->pin, &pincaps)) != 0) {
    device_printf(dev, "Cannot query capabilities of gpio pin\n");
    yourdriver_detach(dev);
    return (err);
}
if ((pincaps & GPIO_INTR_EDGE_BOTH) == 0) {
    device_printf(dev, "Pin cannot be configured for both signal edges interrupt\n");
    yourdriver_detach(dev);
    return (ENOTSUP);
}
if ((sc->intr_res = gpio_alloc_intr_resource(sc->pin->dev, &sc->intr_rid, RF_ACTIVE,
    sc->pin, GPIO_INTR_EDGE_BOTH)) == NULL) {
        device_printf(dev, "Cannot allocate an IRQ for the GPIO\n");
        yourdriver_detach(dev);
        return (ENOTSUP);
}
err = bus_setup_intr(sc->pin->dev, sc->intr_res, INTR_TYPE_MISC | INTR_MPSAFE,
    NULL, rcrecv_ihandler, sc, &sc->intr_cookie);
```

Note, first argument is not a _dev_, not a *xxxbus* (i.e. as a parent of the device), but a _pin->dev_. Works fine in both cases, for device on either *simplebus* or *gpiobus*.
To be precise, it works also with first argument is a _dev_ on a *simplebus*.

Works fine for me on H3 and H2+ under FreeBSD13-stable.


----------

