原创 【转】mini2440 adc驱动代码

2011-10-11 23:28 1298 6 6 分类: EDA/ IP/ 设计与制造

头文件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_WRITH_GETPRE(data) ((data) & 0xff)
#endif

文件mini2440_adc.c

#include <linux/errno.h>
#include <linux/kernel.h>
#include <linux/module.h>
#include <linux/init.h>
#include <linux/slab.h>
#include <linux/input.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(fmt,arg...) {printk("%s%d):",__FNUCTION__,__LINE__);printk(fmt,##arg);}
#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);

static int ownADC = 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))
#define ADCTSC   (*(volatile unsigned long *)(base_addr + S3C2410_ADCTSC))
#define ADCDLY   (*(volatile unsigned long *)(base_addr + S3C2410_ADCDLY))
#define ADCDAT0 (*(volatile unsigned long *)(base_addr + S3C2410_ADCDAT0))
#define ADCDAT1 (*(volatile unsigned long *)(base_addr + S3C2410_ADCDAT1))
#define ADCUPDN (*(volatile unsigned long *)(base_addr + 0X14))

#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;
   START_ADC_AIN(adcdev.channel,adcdev.prescale);
   wait_event_interruptible(adcdev.wait,ev_adc);

   ev_adc = 0;

   printk("<0>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;

}

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;
}


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("<0>,Failed to remap register block\n");
   return -ENOMEM;
}

adc_clock = clk_get(NULL,"adc");

if(!adc_clock)
{
   printk("<0> failed to get adc clock soruce\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("<0>\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");

打开driver/char/Makefile 加入ADC驱动程序目标模块

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

PARTNER CONTENT

文章评论0条评论)

登录后参与讨论
EE直播间
更多
我要评论
0
6
关闭 站长推荐上一条 /3 下一条