首页 > 代码库 > 驱动笔记 - platform中断程序

驱动笔记 - platform中断程序

platform_device:

#include <linux/module.h>#include <linux/types.h>#include <linux/fs.h>#include <linux/init.h>#include <linux/platform_device.h>#include <linux/interrupt.h>#include <linux/device.h>#include <linux/io.h>#define DEVICE_NAME "ok6410_plat_btn"/*平台资源的定义,按键中断*/static struct resource s3c_buttons_resource[] = {	[0]={		.start = IRQ_EINT(0),		.end   = IRQ_EINT(0),		.flags = IORESOURCE_IRQ,	},	[1]={		.start = IRQ_EINT(1),		.end   = IRQ_EINT(1),		.flags = IORESOURCE_IRQ,	},	[2]={		.start = IRQ_EINT(2),		.end   = IRQ_EINT(2),		.flags = IORESOURCE_IRQ,	},	[3]={		.start = IRQ_EINT(3),		.end   = IRQ_EINT(3),		.flags = IORESOURCE_IRQ,	},	[4]={		.start = IRQ_EINT(4),		.end   = IRQ_EINT(4),		.flags = IORESOURCE_IRQ,	},	[5]={		.start = IRQ_EINT(5),		.end   = IRQ_EINT(5),		.flags = IORESOURCE_IRQ,	}};static struct platform_device *s3c_buttons;static int __init platform_init(void){    s3c_buttons = platform_device_alloc(DEVICE_NAME,-1);	//为平台设备s3c_buttons添加平台资源    platform_device_add_resources(s3c_buttons,&s3c_buttons_resource,6);    /*平台设备的注册*/    platform_device_add(s3c_buttons);}static void __exit platform_exit(void){    platform_device_unregister(s3c_buttons);}module_init(platform_init);module_exit(platform_exit);MODULE_AUTHOR("Sola");MODULE_LICENSE("GPL");MODULE_ALIAS("platform:ok6410_buttons");

  

platform_driver:

#include <linux/module.h>#include <linux/types.h>#include <linux/miscdevice.h>#include <linux/fs.h>#include <linux/init.h>#include <linux/platform_device.h>#include <linux/interrupt.h>#include <linux/clk.h>#include <linux/uaccess.h>#include <linux/io.h>#include <mach/map.h>#include <linux/poll.h>#include <linux/irq.h>#include <asm/unistd.h>#include <linux/device.h>#define DRIVER_NAME "ok6410_plat_btn"//定义并初始化等待队列头static DECLARE_WAIT_QUEUE_HEAD(button_waitq);static volatile int ev_press = 0;static int key_value;static struct device     *buttons_dev;	/* platform device attached to */static struct resource   *buttons_irq;static int button_irqs[6];//中断号/*按键中断处理函数*/static irqreturn_t buttons_interrupt(int irq, void *dev_id){	int i;	for(i=0; i<6; i++){		if(irq == button_irqs[i]){			//printk("==>interrput number:%d\n",irq);  			key_value = http://www.mamicode.com/i;"buttons"static struct miscdevice ok6410_miscdev = {	    .minor = MISC_DYNAMIC_MINOR,    .name ="buttons",    .fops = &ok6410_buttons_fops,//操作集};/* device interface ,当发现匹配设备时会调用此函数*/static int ok6410_buttons_probe(struct platform_device *pdev){	struct resource *res;	struct device *dev;	int ret;	int size;	int i;		printk("probe:%s\n", __func__);	dev = &pdev->dev;	buttons_dev = &pdev->dev;		/*平台资源获取*/ /*get irq number*/  for(i=0; i<6; i++){	//获取中断号	 buttons_irq = platform_get_resource(pdev,IORESOURCE_IRQ,i);	 if(buttons_irq == NULL){	   	dev_err(dev,"no irq resource specified\n");		  ret = -ENOENT;		return ret;	}	button_irqs[i] = buttons_irq->start;	//printk("button_irqs[%d]=%d\n",i,button_irqs[i]);  	}		//注册混杂设备	ret = misc_register(&ok6410_miscdev);				return ret;}static int ok6410_buttons_remove(struct platform_device *dev){		misc_deregister(&ok6410_miscdev);	return 0;}/*平台驱动定义*/static struct platform_driver ok6410_buttons_driver = {	.probe		= ok6410_buttons_probe,//探针函数	.remove		= ok6410_buttons_remove,	.driver		= {		.owner	= THIS_MODULE,		.name	= DRIVER_NAME,	},};static char banner[] __initdata =	 "http://www.mamicode.com/ok6410 Buttons Driver/n";static int __init buttons_init(void){	printk(banner);	/*平台驱动注册*/	platform_driver_register(&ok6410_buttons_driver);	return 0;}static void __exit buttons_exit(void){	//平台驱动的注销	platform_driver_unregister(&ok6410_buttons_driver);}module_init(buttons_init);module_exit(buttons_exit);MODULE_LICENSE("GPL");

  

 

驱动笔记 - platform中断程序