这个驱动比较简单,属于字符设备,它位于drivers/char 目录下,驱动程序的文件名为:mini2440_adc.c。内容如下:
#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"
#undef DEBUG
//#define DEBUG
#ifdef DEBUG
#define DPRINTK(x...) {printk(__FUNCTION__"(%d): ",__LINE__);printk(##x);}
#else
#define DPRINTK(x...) (void)(0)
#endif
//;定义ADC 转换设备名称,将出现在/dev/adc
#define DEVICE_NAME "adc"
static void __iomem *base_addr;
//;定义ADC 设备结构
typedef struct {
wait_queue_head_t wait;
int channel;
int prescale;
}ADC_DEV;
//;声明全局信号量,以便和触摸屏驱动程序共享A/D 转换器
DECLARE_MUTEX(ADC_LOCK);
//;ADC 驱动是否拥有A/D 转换器资源的状态变量
static int OwnADC = 0;
static ADC_DEV adcdev;
static volatile int ev_adc = 0;
static int adc_data;
static struct clk *adc_clock;
//;定义ADC 相关的寄存器
#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)
//;定义“开启AD 输入”宏,因为比较简单,故没有做成函数
#define START_ADC_AIN(ch, prescale) \
do{ \
ADCCON = PRESCALE_EN | PRSCVL(prescale) | ADC_INPUT((ch)) ; \
ADCCON |= ADC_START; \
}while(0)
//;ADC 中断处理函数
static irqreturn_t adcdone_int_handler(int irq, void *dev_id)
{
//;如果ADC 驱动拥有“A/D 转换器”资源,则从ADC 寄存器读取转换结果
if (OwnADC) {
adc_data = ADCDAT0 & 0x3ff;
ev_adc = 1;
wake_up_interruptible(&adcdev.wait);
}
return IRQ_HANDLED;
}
//;ADC 读函数,一般对应于用户层/应用层的设备读函数(read)
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;
//;判断“A/D 转换器”资源是否可用
if (down_trylock(&ADC_LOCK) == 0) {
OwnADC = 1; //标记“A/D 转换器”资源状态为可用
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,以便传递到用户层/应用层
value = adc_data;
//;释放“A/D 转换器”资源
OwnADC = 0;
up(&ADC_LOCK);
} else {
//;没有“A/D 转换器”资源,赋值为“-1”
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;
}
}
//;打开ADC 设备的函数,一般对应于用户态程序的open
static int s3c2410_adc_open(struct inode *inode, struct file *filp)
{
//;初始化中断队列
init_waitqueue_head(&(adcdev.wait));
//;缺省通道为“0”
adcdev.channel=0;
adcdev.prescale=0xff;
DPRINTK( "adc opened\n");
return 0;
}
static int s3c2410_adc_release(struct inode *inode, struct file *filp)
{
DPRINTK( "adc closed\n");
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");
if (!adc_clock) {
printk(KERN_ERR "failed to get adc clock source\n");
return -ENOENT;
}
clk_enable(adc_clock);
/* normal ADC */
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"\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);
}
//;导出信号量“ADC_LOCK”,以便触摸屏驱动使用
EXPORT_SYMBOL(ADC_LOCK);
module_init(dev_init);
module_exit(dev_exit);
MODULE_LICENSE("GPL");
MODULE_AUTHOR("FriendlyARM Inc.");
上面的驱动程序中还包含了一个简单的头文件“s3c24xx-adc.h”,在drivers/char目录下新建一个s3c24xx-adc.h,内容为:
#ifndef _S3C2410_ADC_H_
#define _S3C2410_ADC_H_
#define ADC_WRITE(ch, prescale) ((ch)<<16|(prescale))
#define ADC_WRITE_GETCH(data) (((data)>>16)&0x7)
#define ADC_WRITE_GETPRE(data) ((data)&0xff)
#endif /* _S3C2410_ADC_H_ */
然后打开drivers/char/Makefile 文件,在大概114 行加入ADC 驱动程序目标模块:
obj-$(CONFIG_JS_RTC) += js-rtc.o
js-rtc-y = rtc.o
obj-$(CONFIG_MINI2440_ADC) += mini2440_adc.o
再打开drivers/char/Kconfig 文件,加入ADC 驱动配置选项:
config DEVKMEM
bool "/dev/kmem virtual device support"
default y
help
Say Y here if you want to support the /dev/kmem device. The
/dev/kmem device is rarely used, but can be used for certain
kind of kernel debugging operations.
When in doubt, say "N".
config MINI2440_ADC
bool "ADC driver for FriendlyARM Mini2440 development boards"
depends on MACH_MINI2440
default y if MACH_MINI2440
help
this is ADC driver for FriendlyARM Mini2440 development boards
Notes: the touch-screen-driver required this option
config BFIN_JTAG_COMM
tristate "Blackfin JTAG Communication"
depends on BLACKFIN
这样,我们就在内核中添加了ADC 驱动,现在内核源代码目录的命令行执行:make
menuconfig,依次选择如下子菜单项,找到刚刚添加的ADC 驱动配置选项:
Device Drivers --->
Character devices --->
编译好内核下载之后,使用友善之臂提供的文件系统会有一个adc-test命令,旋转开发板上的W1 可调电阻,可以看到ADC 转换的结果也在变动
(清悠我心:http://hi.baidu.com/%E6%B8%85%E6%82%A0%E6%88%91%E5%BF%83/home)
文章评论(0条评论)
登录后参与讨论