好了,大体的格式确定了,必要的硬件基础也了解、建立了,该完成我们的作品了!那么看着这么多空函数,从哪个开始填起呢?我是这么选择的、
1、首先,惯例,我们要创建一个结构体能够描述这个ADC主要属性,通道、clk,当然还有在并发世界的等待队列,其他一些变量啊,同步时用的一些互斥体啦,即用即声明!
typedef struct { wait_queue_head_t wait; int channel; int prescale; }ADC_DEV; static ADC_DEV adcdev;
static struct clk *adc_clock; static int __init dev_init(void) { int ret; base_addr=ioremap(S3C2410_PA_ADC,0x20); //完成基地址的物理地址到虚拟地址转换 if (base_addr == NULL) { printk(KERN_ERR "Failed to remap register block\n"); return -ENOMEM; } adc_clock = clk_get(NULL, "adc"); //获得时钟来源,可参见arch/arm/plat-s3c目录下的clock.c if (!adc_clock) { printk(KERN_ERR "failed to get adc clock source\n"); return -ENOENT; } clk_enable(adc_clock); //使能时钟 ADCTSC = 0; //normal ADC ret = request_irq(IRQ_ADC, adcdone_int_handler, IRQF_SHARED, DEVICE_NAME, &adcdev);//注册中断 if (ret) { iounmap(base_addr); return ret; } ret = misc_register(&misc); //注册驱动为杂项 printk (DEVICE_NAME"\tinitialized\n"); return ret; }
3、dev_exit(void) 函数和dev_init(void)作用相反,想想就明白要完成什么样的工作,去地址转换,去时钟使能,去中断注册,去驱动注册,那么填写起来就简单多了!
static void __exit dev_exit(void) { free_irq(IRQ_ADC, &adcdev); //去中断 iounmap(base_addr); //去地址映射 if (adc_clock) { //关闭时钟 clk_disable(adc_clock); clk_put(adc_clock); adc_clock = NULL; } misc_deregister(&misc); //去驱动注册 }
4、做完整体的初始化,但是有些细节还是没有涉及,比如我们定义的ADC_DEV里面的通道选择,时钟分频等,没关系,这些可以在s3c2410_adc_open函数中解决!
static int s3c2410_adc_open(struct inode *inode, struct file *filp) { init_waitqueue_head(&(adcdev.wait)); //定义等待队列 adcdev.channel=0; //选择通道 adcdev.prescale=0xff; //分频 DPRINTK( "adc opened\n"); return 0; }
5、与open函数对应的是release,但是,open函数里面的操作完全没有必要再释放一次,因此,我们的release函数可以为空
static int s3c2410_adc_release(struct inode *inode, struct file *filp) { DPRINTK( "adc closed\n"); return 0; }
6、然后是最重要的read函数,完成的工作很多,包括互斥锁ADC_LOCK,变量OwnADC实现与触摸屏的互斥,用ev_adc来实现对中断等待标志!当然还要把数据送到上层。DECLARE_MUTEX(ADC_LOCK); static int OwnADC = 0; static volatile int ev_adc = 0; static SSIZE_T s3c2410_adc_read(struct file *filp, char *buffer, size_t count, loff_t *ppos) { char str[20]; int value; size_t len; if (down_trylock(&ADC_LOCK) == 0) { //互斥量来判断此刻能否拥有资源 OwnADC = 1; //置一个标志,表示可以拥有,以便中断的时候成功读取数据 START_ADC_AIN(adcdev.channel, adcdev.prescale);//开始转换 wait_event_interruptible(adcdev.wait, ev_adc);//等待中断,简单睡眠 ev_adc = 0; //中断完成后表示可以继续中断 DPRINTK("AIN[%d] = 0x%04x, %d\n", adcdev.channel, adc_data, ADCCON & 0x80 ? 1:0); value = adc_data; //取得数据 OwnADC = 0; //释放资源,等待下一次中断 up(&ADC_LOCK); //释放互斥量 } else { value = -1; } len = sprintf(str, "%d\n", value); if (count >= len) { int r = copy_to_user(buffer, str, len);//you know return r ? r : len; } else { return -EINVAL; } }
7、就剩最后一个函数了,adcdone_int_handler,这个函数在init的时候已经注册为中断函数了,他的实现很简单,没有传说中的上半部下半部之分,读出来一个10bit数据就OK了!感觉这个函数很简单,不用注释了吧。
static irqreturn_t adcdone_int_handler(int irq, void *dev_id) { if (OwnADC) { adc_data = ADCDAT0 & 0x3ff; ev_adc = 1; wake_up_interruptible(&adcdev.wait); } return IRQ_HANDLED; }
这时写过的总文件:
#include <linux/errno.h> #include <linux/kernel.h> #include <linux/module.h> #include <linux/slab.h> #include <linux/input.h> #include <linux/init.h> #include <linux/serio.h> #include <linux/delay.h> #include <linux/clk.h> #include <linux/wait.h> #include <linux/sched.h> #include <asm/io.h> #include <asm/irq.h> #include <asm/uaccess.h> #include <mach/regs-clock.h> #include <plat/regs-timer.h> #include <plat/regs-adc.h> #include <mach/regs-gpio.h> #include <linux/cdev.h> #include <linux/miscdevice.h> #include "s3c24xx-adc.h" #define DEVICE_NAME "adc" typedef struct { wait_queue_head_t wait; int channel; int prescale; }ADC_DEV; static ADC_DEV adcdev; DECLARE_MUTEX(ADC_LOCK); static int OwnADC = 0; static volatile int ev_adc = 0; static int adc_data; static struct clk *adc_clock; static void __iomem *base_addr; #define ADCCON (*(volatile unsigned long *)(base_addr + S3C2410_ADCCON)) //ADC control #define ADCTSC (*(volatile unsigned long *)(base_addr + S3C2410_ADCTSC)) //ADC touch screen control #define ADCDLY (*(volatile unsigned long *)(base_addr + S3C2410_ADCDLY)) //ADC start or Interval Delay #define ADCDAT0 (*(volatile unsigned long *)(base_addr + S3C2410_ADCDAT0)) //ADC conversion data 0 #define ADCDAT1 (*(volatile unsigned long *)(base_addr + S3C2410_ADCDAT1)) //ADC conversion data 1 #define ADCUPDN (*(volatile unsigned long *)(base_addr + 0x14)) //Stylus Up/Down interrupt status #define PRESCALE_DIS (0 << 14) #define PRESCALE_EN (1 << 14) #define PRSCVL(x) ((x) << 6) #define ADC_INPUT(x) ((x) << 3) #define ADC_START (1 << 0) #define ADC_ENDCVT (1 << 15) #define START_ADC_AIN(ch, prescale) \ do{ \ ADCCON = PRESCALE_EN | PRSCVL(prescale) | ADC_INPUT((ch)) ; \ ADCCON |= ADC_START; \ }while(0) static irqreturn_t adcdone_int_handler(int irq, void *dev_id) { if (OwnADC) { adc_data = ADCDAT0 & 0x3ff; ev_adc = 1; wake_up_interruptible(&adcdev.wait); } return IRQ_HANDLED; } static ssize_t s3c2410_adc_read(struct file *filp, char *buffer, size_t count, loff_t *ppos) { char str[20]; int value; size_t len; if (down_trylock(&ADC_LOCK) == 0) { //互斥量来判断此刻能否拥有资源 OwnADC = 1; //置一个标志,表示可以拥有,以便中断的时候成功读取数据 START_ADC_AIN(adcdev.channel, adcdev.prescale);//开始转换 wait_event_interruptible(adcdev.wait, ev_adc);//等待中断,简单睡眠 ev_adc = 0; //中断完成后表示可以继续中断 value = adc_data; //取得数据 OwnADC = 0; //释放资源,等待下一次中断 up(&ADC_LOCK); //释放互斥量 } else { value = -1; } len = sprintf(str, "%d\n", value); if (count >= len) { int r = copy_to_user(buffer, str, len);//you know return r ? r : len; } else { return -EINVAL; } } static int s3c2410_adc_open(struct inode *inode, struct file *filp) { init_waitqueue_head(&(adcdev.wait)); //定义等待队列 adcdev.channel=0; //选择通道 adcdev.prescale=0xff; //分频 return 0; } static int s3c2410_adc_release(struct inode *inode, struct file *filp) { return 0; } static struct file_operations dev_fops = { owner: THIS_MODULE, open: s3c2410_adc_open, read: s3c2410_adc_read, release: s3c2410_adc_release, }; static struct miscdevice misc = { .minor = MISC_DYNAMIC_MINOR, .name = DEVICE_NAME, .fops = &dev_fops, }; static int __init dev_init(void) { int ret; base_addr=ioremap(S3C2410_PA_ADC,0x20); //完成基地址的物理地址到虚拟地址转换 if (base_addr == NULL) { printk(KERN_ERR "Failed to remap register block\n"); return -ENOMEM; } adc_clock = clk_get(NULL, "adc"); //获得时钟来源,可参见arch/arm/plat-s3c目录下的clock.c if (!adc_clock) { printk(KERN_ERR "failed to get adc clock source\n"); return -ENOENT; } clk_enable(adc_clock); //使能时钟 ADCTSC = 0; //normal ADC ret = request_irq(IRQ_ADC, adcdone_int_handler, IRQF_SHARED, DEVICE_NAME, &adcdev);//注册中断 if (ret) { iounmap(base_addr); return ret; } ret = misc_register(&misc); //注册驱动为杂项 printk (DEVICE_NAME"\tinitialized\n"); return ret; } static void __exit dev_exit(void) { free_irq(IRQ_ADC, &adcdev); //去中断 iounmap(base_addr); //去地址映射 if (adc_clock) { //关闭时钟 clk_disable(adc_clock); clk_put(adc_clock); adc_clock = NULL; } misc_deregister(&misc); //去驱动注册 } EXPORT_SYMBOL(ADC_LOCK); module_init(dev_init); module_exit(dev_exit); MODULE_LICENSE("GPL"); MODULE_AUTHOR("lowkeyway");
这样,通过这三步走,一个成功的驱动建立啦!!!