|
马上注册,结交更多好友,享用更多功能,让你轻松玩转社区。
您需要 登录 才可以下载或查看,没有账号?注册
x
本帖最后由 l641209079 于 2010-11-27 13:00 编辑
mini2440 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 <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
#define DEVICE_NAME "adc"
static void __iomem *base_addr;
typedef struct {
wait_queue_head_t wait;
int channel;//ADC通道
int prescale;//
}ADC_DEV;
DECLARE_MUTEX(ADC_LOCK);// 声明一个信号量name并初始化它的值为0,即声明一个互斥锁
static int OwnADC = 0;//ADC允许使用标志位
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
/*初始化ADC*/
#define PRESCALE_DIS (0 << 14) //ADC 预分频禁止
#define PRESCALE_EN (1 << 14) //ADC 预分频使能
#define PRSCVL(x) ((x) << 6) //ADC分频预设值x
#define ADC_INPUT(x) ((x) << 3) //ADC输入通道x
#define ADC_START (1 << 0) //ADC使能
#define ADC_ENDCVT (1 << 15) //ADC 结束标志位
#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; //取出ADC数据
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);//初始化AD
wait_event_interruptible(adcdev.wait, ev_adc);/*等待AD中断响应;中断响应后,
系统将调用adcdone_int_handler()函数*/
ev_adc = 0;
DPRINTK("AIN[%d] = 0x%04x, %d\n", adcdev.channel, adc_data, ADCCON & 0x80 ? 1:0);
// ADCCON & 0x80 表示什么意思没有弄懂??
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); //返回不能被复制的字节数;但如果完全复制成功,返回值为0
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; //prescale=(GCLK.freq/ (A/D converter freq))-1
DPRINTK( "adc opened\n");// #define DPRINTK(x...) {printk(__FUNCTION__"(%d):",__LINE__);printk(##x);}
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, // #define MISC_DYNAMIC_MINOR=255
.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); //作用:注销设备驱动
}
EXPORT_SYMBOL(ADC_LOCK);//声明一个全部文件可以调用的变量
module_init(dev_init);
module_exit(dev_exit);
MODULE_LICENSE("GPL");
MODULE_AUTHOR("FriendlyARM Inc.");
|
|