linux2.6.30.4中,系统已经自带有了ADC通用驱动文件---arch/arm/plat-s3c24xx/adc.c,它是以平台驱动设备模型的架构来编写的,里面是一些比较通用稳定的代码,但是linux2.6.30.4版本的ADC通用驱动文件并不完善,居然没有读函数。后来去看了linux3.8版本的ADC通用文件----arch/arm/plat-samsung/adc.c才是比较完善的。
但是本节并不是分析这个文件,而是以另外一种架构来编写ADC驱动,因为ADC驱动实在是比较简单,就没有使用平台驱动设备模型为架构来编写了,这次我们使用的是混杂(misc)设备驱动。
问:什么是misc设备驱动?
答:miscdevice共享一个主设备号MISC_MAJOR(10),但次设备号不同。所有的miscdevice设备形成一条链表,对设备访问时内核根据设备号来查找对应的miscdevice设备,然后调用其file_operations结构体中注册的文件操作接口进行操作。
- struct miscdevice {
- int minor;
- const char *name;
- const struct file_operations *fops;
- struct list_head list;
- struct device *parent;
- struct device *this_device;
- };
dev_init入口函数分析:
- 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");
- if (!adc_clock)
- {
- printk(KERN_ERR "failed to get adc clock source\n");
- return -ENOENT;
- }
- clk_enable(adc_clock);
-
- ADCTSC = 0;
-
- 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" initialized\n");
- return ret;
- }
首先是映射ADC寄存器地址将其转换为虚拟地址,然后获得ADC时钟并使能ADC时钟,接着申请ADC中断,其中断处理函数为
adcdone_int_handler,而flags为IRQF_SHARED,即共享中断,因为触摸屏里也要申请ADC中断,最后注册一个混杂设备。
当应用程序open ("/dev/adc",...)时,就会调用到驱动里面的open函数,那么我们来看看open函数做了什么?
- static int tq2440_adc_open(struct inode *inode, struct file *filp)
- {
-
- init_waitqueue_head(&(adcdev.wait));
-
-
- adcdev.channel=2;
- adcdev.prescale=0xff;
-
- DPRINTK( "ADC opened\n");
- return 0;
- }
很简单,先初始化一个等待队列头,因为入口函数里既然有申请ADC中断,那么肯定要使用等待队列,接着设置ADC通道,因为TQ2440的ADC输入通道默认是2,设置预分频值为0xff。
当应用程序read时,就会调用到驱动里面的read函数,那么我们来看看read函数做了些什么?
- static ssize_t tq2440_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)
- {
-
- ADC_enable = 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;
- sprintf(str,"%5d", adc_data);
- copy_to_user(buffer, (char *)&adc_data, sizeof(adc_data));
-
- ADC_enable = 0;
- up(&ADC_LOCK);
- }
- else
- {
-
- value = -1;
- }
-
-
- len = sprintf(str, "%d\n", value);
- if (count >= len)
- {
-
- int r = copy_to_user(buffer, str, len);
- return r ? r : len;
- }
- else
- {
- return -EINVAL;
- }
- }
tq2440_adc_read
函数首先尝试获得ADC_LOCK信号量,因为触摸屏驱动也有使用ADC资源,两者互有竞争关系,获得ADC资源后,使能预分频,选择ADC通道,最后启动ADC转换,接着就调用wait_event_interruptible 函数进行等待,直到ev_adc>0进程才会继续往下跑,
往下跑就会将
adc_data数据读出来,调用
copy_to_user函数将ADC数据传给应用空间,最后释放
ADC_LOCK信号量。
问:什么时候ev_adc>0?默认ev_adc = 0
答:在adcdone_int_handler中断处理函数里,等数据读出后,ev_adc被设置为1。
ADC中断处理函数adcdone_int_handler
-
- static irqreturn_t adcdone_int_handler(int irq, void *dev_id)
- {
-
- if (ADC_enable)
- {
-
- adc_data = ADCDAT0 & 0x3ff;
-
-
- ev_adc = 1;
- wake_up_interruptible(&adcdev.wait);
- }
- return IRQ_HANDLED;
- }
当AD转换完成后就会触发ADC中断,就会进入adcdone_int_handler,这个函数就会讲AD转换数据读到adc_data,接着将唤醒标志位ev_adc置1,最后调用wake_up_interruptible函数唤醒adcdev.wait等待队列。
总结一下ADC的工作流程:
一、open函数里,设置模拟输入通道,设置预分频值
二、read函数里,启动AD转换,进程休眠
三、adc_irq函数里,AD转换结束后触发ADC中断,在ADC中断处理函数将数据读出,唤醒进程
四、read函数里,进程被唤醒后,将adc转换数据传给应用程序
ADC驱动参考源码:
-
-
-
-
-
-
-
- #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 <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 "tq2440_adc.h"
-
- #undef DEBUG
-
- #ifdef DEBUG
- #define DPRINTK(x...) {printk(KERN_DEBUG "EmbedSky_adc: " x);}
- #else
- #define DPRINTK(x...) (void)(0)
- #endif
-
- #define DEVICE_NAME "adc" /* 设备节点: /dev/adc */
-
- static void __iomem *base_addr;
-
- typedef struct
- {
- wait_queue_head_t wait;
- int channel;
- int prescale;
- }ADC_DEV;
-
- DECLARE_MUTEX(ADC_LOCK);
- static int ADC_enable = 0;
-
- static ADC_DEV adcdev;
- static volatile int ev_adc = 0;
- static int adc_data;
-
- static struct clk *adc_clock;
-
- #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 (ADC_enable)
- {
-
- adc_data = ADCDAT0 & 0x3ff;
-
-
- ev_adc = 1;
- wake_up_interruptible(&adcdev.wait);
- }
- return IRQ_HANDLED;
- }
-
- static ssize_t tq2440_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)
- {
-
- ADC_enable = 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;
- sprintf(str,"%5d", adc_data);
- copy_to_user(buffer, (char *)&adc_data, sizeof(adc_data));
-
- ADC_enable = 0;
- up(&ADC_LOCK);
- }
- else
- {
-
- value = -1;
- }
-
-
- len = sprintf(str, "%d\n", value);
- if (count >= len)
- {
-
- int r = copy_to_user(buffer, str, len);
- return r ? r : len;
- }
- else
- {
- return -EINVAL;
- }
- }
-
- static int tq2440_adc_open(struct inode *inode, struct file *filp)
- {
-
- init_waitqueue_head(&(adcdev.wait));
-
-
- adcdev.channel=2;
- adcdev.prescale=0xff;
-
- DPRINTK( "ADC opened\n");
- return 0;
- }
-
- static int tq2440_adc_release(struct inode *inode, struct file *filp)
- {
- DPRINTK( "ADC closed\n");
- return 0;
- }
-
-
- static struct file_operations dev_fops = {
- owner: THIS_MODULE,
- open: tq2440_adc_open,
- read: tq2440_adc_read,
- release: tq2440_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");
- if (!adc_clock)
- {
- printk(KERN_ERR "failed to get adc clock source\n");
- return -ENOENT;
- }
- clk_enable(adc_clock);
-
- ADCTSC = 0;
-
- 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" initialized\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("www.embedsky.net");
- MODULE_DESCRIPTION("ADC Drivers for EmbedSky SKY2440/TQ2440 Board and support touch");
ADC应用测试参考源码:
-
-
-
-
-
-
-
- #include <stdio.h>
- #include <unistd.h>
- #include <stdlib.h>
- #include <sys/types.h>
- #include <sys/stat.h>
- #include <sys/ioctl.h>
- #include <fcntl.h>
- #include <linux/fs.h>
- #include <errno.h>
- #include <string.h>
-
- int main(void)
- {
- int fd ;
- char temp = 1;
-
- fd = open("/dev/adc", 0);
- if (fd < 0)
- {
- perror("open ADC device !");
- exit(1);
- }
-
- for( ; ; )
- {
- char buffer[30];
- int len ;
-
- len = read(fd, buffer, sizeof buffer -1);
- if (len > 0)
- {
- buffer[len] = '\0';
- int value;
- sscanf(buffer, "%d", &value);
- printf("ADC Value: %d\n", value);
- }
- else
- {
- perror("read ADC device !");
- exit(1);
- }
- sleep(1);
- }
- adcstop:
- close(fd);
- }
测试结果:
- [WJ2440]# ./adc_test
- ADC Value: 693
- ADC Value: 695
- ADC Value: 694
- ADC Value: 695
- ADC Value: 702
- ADC Value: 740
- ADC Value: 768
- ADC Value: 775
- ADC Value: 820
- ADC Value: 844
- ADC Value: 887
- ADC Value: 937
- ADC Value: 978
- ADC Value: 1000
- ADC Value: 1023
- ADC Value: 1023
- ADC Value: 1023