代码自取【14.key_tree_pinctrl_gpios_interrupt】:
https://gitee.com/chenshao777/imx6-ull_-drivers
主要接口函数:
1. of_gpio_count(获得GPIO的数量)
static inline int of_gpio_count(struct device_node *np)
2. kzalloc(向内核申请空间)
static inline void *kzalloc(size_t size, gfp_t flags)
3. of_get_gpio(获取GPIO子系统标号)
static inline int of_get_gpio(struct device_node *np, int index)
4. gpio_to_irq(根据GPIO子系统标号得到软件中断号)
static inline int gpio_to_irq(unsigned gpio)
5. request_irq(根据软件中断号发起请求中断)
static inline int __must_check
request_irq(unsigned int irq, irq_handler_t handler, unsigned long flags,const char *name, void *dev)
6. free_irq(释放中断)
void free_irq(unsigned int irq, void *dev_id)
针对正点原子IMX6ULL阿尔法开发板
设备树的写法:
hc_key {compatible = "hc-key";pinctrl-name = "default";pinctrl-0 = <&pinctrl_key>;gpios = <&gpio1 1 GPIO_ACTIVE_LOW&gpio1 18 GPIO_ACTIVE_LOW>;status = "okay";
};
驱动主要部分代码:
/* 按键中断函数 */
static irqreturn_t key_irq_handler(int irq, void *dev)
{struct gpio_irq *girq = dev;printk("key_irq happend, irq = %d, gpio = %d\n", girq->irq, girq->gpio);return IRQ_HANDLED;
}.........
.........
........./* 匹配内核根据设备树生成的platform_device */
static int my_probe(struct platform_device *pdev)
{struct device_node *node = pdev->dev.of_node;int gpio_cnt,i,err;/* 获得GPIO个数 */gpio_cnt = of_gpio_count(node);/* 向内核申请空间,存储gpio号和irq */gp_irq = kzalloc(gpio_cnt * sizeof(struct gpio_irq), GFP_KERNEL);for(i = 0; i < gpio_cnt; i++){//获取GPIOgp_irq[i].gpio = of_get_gpio(node, i);//GPIO转换成中断号gp_irq[i].irq = gpio_to_irq(gp_irq[i].gpio);//中断请求err = request_irq(gp_irq[i].irq, key_irq_handler, IRQF_TRIGGER_FALLING, "hc_key_irq", &gp_irq[i]);}printk("my_probe run\n");return 0;
}
图片展示说明:
测试结果: