|
以下 是整个驱动代码,版主你帮忙看一下啊。
/*************************************
NAME:EmbedSky_beep.c
COPYRIGHT:www.embedsky.net
*************************************/
#include <linux/module.h>
#include <linux/kernel.h>
#include <linux/fs.h>
#include <linux/init.h>
#include <linux/device.h>
#include <linux/miscdevice.h>
#include <linux/delay.h>
#include <linux/mm.h>
#include <asm/irq.h>
#include <asm/plat-s3c/regs-timer.h>
#include <asm/plat-s3c/regs-adc.h>
#include <asm/arch/regs-gpio.h>
#include <asm/arch/map.h>
#include <asm/arch/regs-irq.h>
#include <asm/io.h>
#include <asm/hardware.h>
#include <asm/uaccess.h>
#include <asm/system.h>
#include <linux/errno.h>
#include <linux/slab.h>
#include <linux/input.h>
#include <linux/serio.h>
#include <linux/clk.h>
#include <linux/cdev.h>
#include <linux/miscdevice.h>
#define ADC_WRITE(ch, prescale) ((ch) << 16 | (prescale))
#define ADC_WRITE_GETCH(data) (((data) >> 16) & 0x7)
#define ADC_WRITE_GETPRE(data) ((data) & 0xff)
#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);
static int ADC_OK = 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)
static irqreturn_t adcdone_int_handler(int irq, void *dev_id)
{
if (ADC_OK)
{
adc_data = ADCDAT0 & 0x3ff;
// adc_data=0x01;
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)
{
ADC_OK = 1;
ADCCON = PRESCALE_EN | PRSCVL(adcdev.prescale) | ADC_INPUT((adcdev.channel)) ;
ADCCON |= ADC_START;
wait_event_interruptible(adcdev.wait, ev_adc);
ev_adc = 0;
// adc_data=0x01;
printk("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_OK = 0;
up(&ADC_LOCK);
}
else
{
value = -1;
printk("there is no mutex\n");
}
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=2; //AIN 2 or (0~7)
adcdev.prescale=0xff;
printk( "EmbedSky-ADC opened\n");
return 0;
}
static int s3c2410_adc_release(struct inode *inode, struct file *filp)
{
printk( "EmbedSky-ADC closed\n");
return 0;
}
/* 杩欎釜缁撴瀯鏄?瓧绗﹁?澶囬┍鍔ㄧ▼搴忕殑鏍稿績
* 褰撳簲鐢ㄧ▼搴忔搷浣滆?澶囨枃浠舵椂鎵 |
|