Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Linux 4.5 GPIO Interrupt Through Devicetree on Xilinx Zynq Platform

I am using a custom development board with a Zynq XC72010 used to run a Linux 4.5 kernel. I am developing a device driver for a chip we are testing in house and I am having a lot of issues trying to bind a GPIO line to a software IRQ. So far I have tried a few methods and exhausted any google search I can think of. The relevant parts of my devicetree configuration:

/ {
    compatible = "xlnx,zynq-7000";

    amba {
        compatible = "simple-bus";
        #address-cells = <1>;
        #size-cells = <1>;
        interrupt-parent = <&intc>;
        ranges;

        intc: interrupt-controller@f8f01000 {
            compatible = "arm,cortex-a9-gic";
            #interrupt-cells = <3>;
            interrupt-controller;
            reg = <0xF8F01000 0x1000>,
                  <0xF8F00100 0x100>;
        };

        i2c0: i2c@e0004000 {
            compatible = "cdns,i2c-r1p10";
            status = "disabled";
            clocks = <&clkc 38>;
            interrupt-parent = <&intc>;
            interrupts = <0 25 4>;
            reg = <0xe0004000 0x1000>;
            #address-cells = <1>;
            #size-cells = <0>;

            // I WANT INTERRUPT TO TRIGGER
            // ON THIS DEVICE (axi_gpio_0, pin 2)
            device: device@48 {
                compatible = "device,name";
                reg = <0x48>;
                reset-gpios = <&axi_gpio_0 1 0>;
                interrupt-parent = <&axi_gpio_0>;
                interrupt-gpios = <&axi_gpio_0 2 0>;
            };
        };
    };

    amba_pl {
        #address-cells = <1>;
        #size-cells = <1>;
        compatible = "simple-bus";
        ranges ;
        axi_gpio_0: gpio@41200000 {
            #gpio-cells = <2>;
            compatible = "xlnx,xps-gpio-1.00.a";
            gpio-controller;
            interrupt-parent = <&intc>;
            interrupts = <0 31 4>;
            reg = <0x41200000 0x10000>;
            xlnx,all-inputs = <0x0>;
            xlnx,all-inputs-2 = <0x0>;
            xlnx,all-outputs = <0x0>;
            xlnx,all-outputs-2 = <0x0>;
            xlnx,dout-default = <0x00000000>;
            xlnx,dout-default-2 = <0x00000000>;
            xlnx,gpio-width = <0x10>;
            xlnx,gpio2-width = <0x20>;
            xlnx,interrupt-present = <0x1>;
            xlnx,is-dual = <0x0>;
            xlnx,tri-default = <0xFFFFFFFF>;
            xlnx,tri-default-2 = <0xFFFFFFFF>;
    };
};

I am trying to assign an interrupt to pin 2 of 'axi_gpio_0' inside of 'device'.

Browsing google yielded 3 common methods for binding the interrupt in driver code:

/* Method 1 */
device->interrupt_gpio = devm_gpiod_get_optional(&i2c_client->dev,
                                        "interrupt", GPIOD_IN);

if(IS_ERR(device->interrupt_gpio))
    return PTR_ERR(device->interrupt_gpio);

printk("device: Interrupt GPIO = %d\n",desc_to_gpio(device->interrupt_gpio));

irq = gpiod_to_irq(device->interrupt_gpio);
printk("device: IRQ = %d\n",irq);

ret = devm_request_threaded_irq(&i2c_client->dev, irq,
    NULL, device_irq_thread, IRQF_ONESHOT | IRQF_TRIGGER_HIGH,
    "device", device);
if (ret != 0)
    dev_err(&i2c_client->dev, "Failed to request IRQ: %d\n", ret);



/* Method 2 */
device->interrupt_gpio = devm_gpiod_get_optional(&i2c_client->dev,
    "interrupt", GPIOD_ASIS);

if (IS_ERR(device->interrupt_gpio))
    return PTR_ERR(device->interrupt_gpio);


if (device->interrupt_gpio) {
    dev_info(&i2c_client->dev, "Found interrupt GPIO: %d\n",desc_to_gpio(device->interrupt_gpio));
    dev_info(&i2c_client->dev, "IRQ Number: %d\n",gpiod_to_irq(device->interrupt_gpio));

    gpio_request(desc_to_gpio(device->interrupt_gpio), "DEVICE_INT");    // Request a GPIO pin from the driver
    gpio_direction_input(desc_to_gpio(device->interrupt_gpio));           // Set GPIO as input
    gpio_set_debounce(desc_to_gpio(device->interrupt_gpio), 50);          // Set a 50ms debounce, adjust to your needs
    gpio_export(desc_to_gpio(device->interrupt_gpio), false);                    // The GPIO will appear in /sys/class/gpio

    ret = request_irq(gpiod_to_irq(device->interrupt_gpio),             // requested interrupt
                   (irq_handler_t) irqHandler,  // pointer to handler function
                   IRQF_TRIGGER_RISING,         // interrupt mode flag
                   "DEVICE_IRQ_HANDLER",        // used in /proc/interrupts
                   NULL);                       // the *dev_id shared interrupt lines, NULL is okay
    if (ret != 0) {
        dev_err(&i2c_client->dev,
            "Failed to request IRQ: %d\n", ret);
    }
}
else {
    dev_err(&i2c_client->dev, "Failed to get interrupt GPIO pin\n");
}



/* Method 3 */
dev_info(&i2c_client->dev, "IRQ requested: %d\n", i2c_client->irq);

ret = devm_request_threaded_irq(&i2c_client->dev, i2c_client->irq,
NULL, device_irq_thread, IRQF_ONESHOT | IRQF_TRIGGER_LOW,
"device", device);
if (ret != 0)
    dev_err(&i2c_client->dev, "Failed to request IRQ: %d\n", ret);

I tried combinations of all of these methods and various devicetree configurations, but none of them achieved the functionality I need.

Method 1 results in this output:

device: Interrupt GPIO = 892
device: IRQ = -6
device 0-0048: Failed to request IRQ: -22

Method 2 results in this output:

device 0-0048: Found interrupt GPIO: 892
device 0-0048: IRQ Number: -6
device 0-0048: Failed to request IRQ: -22

So, trying to use the descriptor GPIO and the old GPIO api's are both unsuccessful in binding an interrupt.

To try method 3, I tweaked the devicetree:

device: device@48 {
    compatible = "device,name";
    reg = <0x48>;
    interrupt-parent = <&axi_gpio_0>; // or <&intc>?
    interrupts = <0 2 0x02>; // trying to grab pin 2
};

Method 3 results in this output:

genirq: Setting trigger mode 2 for irq 168 failed (gic_set_type+0x0/0x48)
device 0-0048: IRQ requested: 168
genirq: Setting trigger mode 8 for irq 168 failed (gic_set_type+0x0/0x48)
device 0-0048: Failed to request IRQ: -22

It seems the problem is assigning a software interrupt to a specific GPIO in Linux. I don't see what I'm missing here. Any advice is appreciated.

EDIT 1:

I found out that Linux doesn't like low-level interrupts for whatever reason. Changing method 3 to:

device: device@48 {
    compatible = "device,name";
    reg = <0x48>;
    interrupt-parent = <&axi_gpio_0>;
    interrupts = <0 2 0x04>;
};

And driver code to:

dev_info(&i2c_client->dev, "IRQ requested: %d\n", i2c_client->irq);

ret = devm_request_threaded_irq(&i2c_client->dev, i2c_client->irq,
NULL, device_irq_thread, IRQF_ONESHOT | IRQF_TRIGGER_HIGH,
"device", device);
if (ret != 0)
    dev_err(&i2c_client->dev, "Failed to request IRQ: %d\n", ret);

allows me to successfully request an IRQ. However, my signal is active low, so this doesn't really help me much. Also, I'm not sure that this method is referencing axi_gpio_0 pin 2 as the interrupt signal. I can use both intc and axi_gpio_0 as interrupt-parent and it maps to the same IRQ number (I see this from cat /proc/interrupts). So, ignoring the polarity of the signal, how do I make sure that my registered interrupt is triggered based on the toggling of axi_gpio_0 pin 2?

EDIT 2:

I traced the issue with requesting an active-low interrupt to the driver for the interrupt controller: kernel/drivers/irqchip/irq-gic.c. This section of code is what causes the problem:

static int gic_set_type(struct irq_data *d, unsigned int type)
{
    void __iomem *base = gic_dist_base(d);
    unsigned int gicirq = gic_irq(d);

    /* Interrupt configuration for SGIs can't be changed */
    if (gicirq < 16)
        return -EINVAL;

    /* SPIs have restrictions on the supported types */
    if (gicirq >= 32 && type != IRQ_TYPE_LEVEL_HIGH &&
                type != IRQ_TYPE_EDGE_RISING)
        return -EINVAL;

    return gic_configure_irq(gicirq, type, base, NULL);
}

Hacking the kernel is not at all what I want to do, but commenting out:

/* SPIs have restrictions on the supported types */
/*if (gicirq >= 32 && type != IRQ_TYPE_LEVEL_HIGH &&
            type != IRQ_TYPE_EDGE_RISING)
return -EINVAL;*/

allows me to request an active-low interrupt. For testing purposes, this should work temporarily.

UPDATE:

I have successfully created an IRQ on a GPIO pin. My problem was with the GPIO controller I was using. The controller was a Xilinx IP block inside of the Zynq Programmable Logic block and this controller is unable to trigger interrupts on GPIO pins (for reasons unknown to me). I soldered the interrupt pin on the board I was working on to a GPIO pin on a different, more generic controller and now Linux is playing nicely with me.

To summarize, a GPIO controller that matches compatible = "xlnx,xps-gpio-1.00.a"; is unable to bind to software interrupts in Linux. If you have this problem, USE A DIFFERENT GPIO CONTROLLER.

Thanks everyone for the help.

like image 941
James Schulman Avatar asked Nov 09 '22 10:11

James Schulman


1 Answers

Using the device tree node of method 3, you should be able to retrieve the IRQ using irq_of_parse_and_map(i2c_client->dev.of_node, 0).

The retrieved IRQ can then be requested as you have done it with devm_request_threaded_irq().

like image 99
Longfield Avatar answered Nov 15 '22 06:11

Longfield