如何为x86 linux实现GPIO中断处理程序?

Chu*_*kai 3 linux x86 kernel driver

我正在为x86 linux设备驱动程序.器件的引脚连接到PCH上的GPIO以产生中断.如何请求与该GPIO引脚相关的IRQ并安装中断处理程序?

use*_*443 5

您正在寻找的头文件是

#include <linux/gpio.h> 
Run Code Online (Sandbox Code Playgroud)

您需要做的第一件事是分配特定的GPIO.您可以使用此调用执行此操作:

#define GPIO //gpio number

...

if(gpio_request(GPIO, "Description"))
    //fail
    ...
Run Code Online (Sandbox Code Playgroud)

在获得GPIO引脚后,您可以获取它的IRQ

int irq = 0;
if((irq = gpio_to_irq(GPIO)) < 0 /*irq number can't be less than zero*/)
    //fail
    ...
Run Code Online (Sandbox Code Playgroud)

现在,您使用通常的内核例程注册IRQ处理程序.

#include <linux/interrupt.h>
...
int result = request_irq(irq, handler_function, 
                         IRQF_TRIGGER_LOW, /*here is where you set up on what event should the irq occur*/
                         "Description", "Device description");
if(result)
    //fail
    ...
Run Code Online (Sandbox Code Playgroud)

记住free_irqgpio_free进行模块清理时.如果不这样做,您将无法再次分配该GPIO引脚.