[IMX6ULL 드라이버 개발 학습] 14. 리눅스 드라이버 개발 - GPIO 인터럽트(디바이스 트리 + GPIO 서브시스템)

코드 자체 가져오기 [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++){
    
    
		//获取GPIO
		gp_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;
}

그림 디스플레이 설명:
여기에 이미지 설명 삽입
테스트 결과:

여기에 이미지 설명 삽입

추천

출처blog.csdn.net/HuangChen666/article/details/131476986