一.硬件简介
Linux-2.6.32.2内核没有提供S3C2440的ADC驱动程序,ADC驱动属于字符设备驱动,下面就讲解驱动实现的过程。
在2440中,AD和触摸屏使用共同的A/D转换器,如下图所示,具体见2440芯片手册16章。
二.驱动实现:
驱动源码如下:
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#define DEVICE_NAME "adc"
static void __iomem *base_addr; //定义用来保存经虚拟映射的内存地址
typedef struct
{
wait_queue_head_t wait;
int channel;
int prescale;
}ADC_DEV;
DECLARE_MUTEX(ADC_LOCK); //信号量ADC_LOCK,AD、触摸屏互斥访问
static int ownADC = 0;
static ADC_DEV adcdev;
static volatile int ev_adc = 0;
static int adc_data;
static struct clk *adc_clk; //ADC所需时钟
//启动AD转换的宏www.linuxidc.com
#define START_ADC_AIN(ch,prescale)
do{
iowrite32(S3C2410_ADCCON_PRSCEN|S3C2410_ADCCON_PRSCVL(prescale)|S3C2410_ADCCON_SELMUX(ch),
base_addr+S3C2410_ADCCON);
iowrite32(ioread32(base_addr+S3C2410_ADCCON)|S3C2410_ADCCON_ENABLE_START,base_addr+S3C2410_ADCCON);
}while(0)
static irqreturn_t adc_handler(int irq,void *dev_id)
{
if(ownADC)
{
adc_data = ioread32(base_addr+S3C2410_ADCDAT0)&0x3ff;
ev_adc = 1; //表示有数据可读,唤醒等待队列
wake_up_interruptible(&adcdev.wait);
}
return IRQ_HANDLED;
}
static int s3c2440_adc_open(struct inode *inode,struct file *filp)
{
init_waitqueue_head(&adcdev.wait); //初始化队列
adcdev.channel = 0;
adcdev.prescale = 0xff;
return 0;
}
static int s3c2440_adc_close(struct inode *inode,struct file *filp)
{
return 0;
}
static ssize_t s3c2440_adc_read(struct file *filp,char *buffer,size_t count,loff_t *ppos)
{
int ret;
if(down_trylock(&ADC_LOCK)) //尝试获取信号量
{
return -EBUSY;
}
ownADC =1;
START_ADC_AIN(adcdev.channel,adcdev.prescale);
wait_event_interruptible(adcdev.wait,ev_adc); //等待有数据可读,中断处理中将唤醒此处
ev_adc = 0;
ret=copy_to_user(buffer,(char *)&adc_data,sizeof(adc_data));
ownADC = 0;
up(&ADC_LOCK); //释放信号量
return sizeof(adc_data);
}
static struct file_operations dev_fops=
{
.owner = THIS_MODULE,
.open = s3c2440_adc_open,
.read = s3c2440_adc_read,
.release = s3c2440_adc_close,
};
static struct miscdevice misc=
{
.minor = MISC_DYNAMIC_MINOR, // MISC_DYNAMIC_MINOR定义在include/linux/miscdevice.h
.name = DEVICE_NAME,
.fops = &dev_fops,
};
static int __init dev_init(void)
{
int ret;
/*将ADC的IO端口占用的这段IO空间映射到内存的虚拟地址, S3C2410_PA_ADC是ADC控制器的基地址,
定义在arch/arm/mach-s3c2410/include/mach/map.h*/
base_addr = ioremap(S3C2410_PA_ADC,0x20);
if(base_addr==NULL)
{
printk(KERN_ERR "Failed to remap registern");
return -ENOMEM;
}
/*从平台时钟队列中获取ADC的时钟,ADC的转换频率跟时钟有关www.linuxidc.com。定义在arch/arm/plat-s3c24xx/s3c2410-clock.c中*/
adc_clk = clk_get(NULL,"adc");
if(!adc_clk)
{
printk(KERN_ERR "Failed to get adc clkn");
return -ENOENT;
}
clk_enable(adc_clk);
iowrite32(0,base_addr+S3C2410_ADCTSC);
/*申请ADC中断,使用的是共享中断:IRQF_SHARED,为什么要使用共享中断,因为在触摸屏驱动中也使用了这个中断号。
中断服务程序为:adc_irq在下面实现,IRQ_ADC是ADC的中断号。共享中断最后一个参数不能为NULL*/
ret = request_irq(IRQ_ADC,adc_handler,IRQF_SHARED,DEVICE_NAME,&adcdev);
if(ret)
{
iounmap(base_addr);
return ret;
}
ret = misc_register(&misc); //ADC注册为misc设备
printk(KERN_INFO "initializedn");
return ret;
}
static void __exit dev_exit(void)
{
free_irq(IRQ_ADC,&adcdev);
iounmap(base_addr);
if(adc_clk)
{
clk_disable(adc_clk);
clk_put(adc_clk);
adc_clk = NULL;
}
misc_deregister(&misc);
}
EXPORT_SYMBOL(ADC_LOCK); //导出信号量ADC_LOCK,触摸屏驱动使用
module_init(dev_init);
module_exit(dev_exit);
MODULE_LICENSE("GPL");
MODULE_AUTHOR("Zechin Liao");
静态编译:
1.将上面s3c2440_adc.c文件拷贝至drivers/char目录;
2.打开drivers/char/Makefile添加相应内容:
obj-$(CONFIG_JS_RTC) += js-rtc.o
js-rtc-y = rtc.o
obj-$(CONFIG_S3C2440_ADC) += s3c2440_adc.o
3.打开drivers/char/Kconfig添加: