平台设备驱动例程-注册一个按键中断

1.使用platform_get_irq()获取中断号。头文件:

2.使用request_irq()注册中断处理函数。头文件:

3.使用gpio_get_value()读取按键电平。头文件:

4.使用S3C2410_GPG(x)得到IO口编号。头文件:



#include 
#include 
#include 

#include 
#include 
#include 
#include 
#include 

#include 
#include 


#include 





int but_irqno1;
struct resource *dev_button_resource;


irqreturn_t input_button_handler(int irqnum, void *data)
{
	printk("KERNEL>> irq number = %d\n",irqnum);
	//int value = S3C2410_GPG(0);
	int value = gpio_get_value(S3C2410_GPG(0));
//	int value = s3c2410_gpio_getpin(0);
	if(value)	// rising edge
	{
		printk("KERNEL>> key up\n");
	}
	else		// falling edge
	{
		printk("KERNEL>> key down\n");
	}
	return(IRQ_HANDLED);
}

int input_button_probe(struct platform_device *pdev)
{
	int ret;
	
	printk("_______________%s_____________________\n",__func__);
	// 1.获取设备资源
	but_irqno1 = platform_get_irq(pdev,0);
	printk("button irqno1 = %d\n",but_irqno1);
	// 2. 注册设备节点

	
	// 3.硬件初始化 - 申请中断
	// para1 - 中断号.IRQ_EINT(x),find in file irqs.h
	// prar2 - 中断处理方法:irqreturn_t (*irq_handler_t)(int, void *);
	// para3 - 中断触发方式.
	// PARA4 - 在proc文件系统中显示的名称:/proc/interrupts
	// para5 - 传递给中断处理函数的参数
	ret = request_irq(but_irqno1,input_button_handler,IRQF_TRIGGER_FALLING|IRQF_TRIGGER_RISING,"input_button_irq",NULL);
	if(0!=ret)
	{
		printk("requet irq failed!\n");
		ret = -EBUSY;
		goto err_device_desdroy;
	}

	return 0;
	
err_device_desdroy:

//err_class_desdroy:

//err_unregister:

	return ret;
}


int input_button_remove(struct platform_device *pdev)
{
	printk("_______________%s_____________________\n",__func__);

	free_irq(but_irqno1,NULL);
	return 0;
}


static const struct platform_device_id input_button_drv_idb[] = 
{
	{ "input_button_evdev", 0, },
	{ },
};

	
// 1.申请pdriver对象
// 2.初始化pdriver对象
struct platform_driver input_button_drv = 
{
	.probe = input_button_probe,
	.remove = input_button_remove,
	.driver = 
			{
				.name = "input_button_evdrv",
			},
	.id_table = (struct platform_device_id*)input_button_drv_idb,
};


// 3.注册pdriver对象

int __init input_button_drv_init(void)
{
	return(platform_driver_register(&input_button_drv));
}


void __exit input_button_drv_exit(void)
{
	platform_driver_unregister(&input_button_drv);
}




module_init(input_button_drv_init);
module_exit(input_button_drv_exit);
MODULE_LICENSE("GPL");





你可能感兴趣的:(linux学习)