/*
* s3c2440_adc.h
*/
#ifndef _S3C2440_ADC_H_
#define _S3C2440_ADC_H_
#include <asm/arch/regs-adc.h>
#define START_ADC(ch, prescale) S3C2410_ADCCON_PRSCEN | S3C2410_ADCCON_PRSCVL(prescale) \
| S3C2410_ADCCON_SELMUX(ch) | S3C2410_ADCCON_ENABLE_START
#define ADC_WRITE_GETCH(data) (((data)>>16)&0x7)
#define ADC_WRITE_GETPRE(data) ((data)&0xff)
#endif /* _S3C2440_ADC_H_ */
/*
* s3c2440_adc.c
*
* Author: SeonKon Choi <BUSHI@MIZI.COM>
* Date : $Date: 2003/01/20 14:24:49 $
*
* $Revision: 1.1.2.6 $
*
2004-6-14 add a device by threewater<THREEWATER@UP-TECH.COM>
Fri Dec 03 2002 SeonKon Choi <BUSHI@MIZI.COM>
- initial
modify by lxm<lxm650@163.com> 2009-02-16
*
* This file is subject to the terms and conditions of the GNU General Public
* License. See the file COPYING in the main directory of this archive
* for more details.
*/
#include <linux/module.h>
#include <linux/fs.h> /* everything... */
#include <linux/cdev.h>
#include <linux/interrupt.h> /* request_irq() */
#include <asm/arch/regs-adc.h>
#include <asm/hardware/clock.h>
#include <asm/io.h>
#include <asm/uaccess.h> /* copy_to_user() */
#include "s3c2440_adc.h"
#undef DEBUG
#define DEBUG
#ifdef DEBUG
#define DPRINTK(x...) {printk(__FUNCTION__);printk("(%d): ",__LINE__);printk(x);}
#else
#define DPRINTK(x...) (void)(0)
#endif
#define VERSION_STRING "ADC driver for s3c2440"
#define DEVICE_NAME "s3c2440_adc"
#define ADC_CTL_BASE 0x58000000 /* 定义ADC寄存器基地址 */
#define S3C2440ADC_MAJOR 240 /* 预设的S3C2440ADC的主设备号 */
static int s3c2440adc_major = S3C2440ADC_MAJOR;
struct s3c2440adc_dev {
struct cdev cdev; /* cdev结构体 */
struct semaphore lock; /* 信号量 */
wait_queue_head_t wait; /* 定义等待队列头 */
int channel; /* 通道号 */
int prescale; /* 预分频系数 */
};
struct s3c2440adc_dev s3c2440adc_dev; /* 物理地址映射成虚拟地址后的基地址指针 */
struct s3c2440adc_dev *s3c2440adc_devp; /*设备结构体指针*/
static void __iomem *base_addr; /* 物理地址映射成虚拟地址后的基地址指针 */
static struct clk *adc_clock; /* 时钟 */
/* ADC转换完成后中断将调用的函数*/
static irqreturn_t adcdone_int_handler(int irq, void *dev_id)
{
wake_up(&s3c2440adc_dev.wait); /* 唤醒等待队列 */
return IRQ_HANDLED;
}
/* ADC驱动的写函数 */
static ssize_t s3c2440adc_write(struct file *file, const char *buffer, size_t count, loff_t * ppos)
{
unsigned long data;
if(count!=sizeof(data)){
//错误的输入数据大小
DPRINTK("the size of input data must be %d\n", sizeof(data));
return 0;
}
copy_from_user(&data, buffer, count);
s3c2440adc_dev.channel=ADC_WRITE_GETCH(data); /* 得到需进行AD转换的通道号 */
s3c2440adc_dev.prescale=ADC_WRITE_GETPRE(data); /* 得到预分频系数 */
DPRINTK("set adc channel=%d, prescale=0x%x\n", s3c2440adc_dev.channel, s3c2440adc_dev.prescale);
return count;
}
/* ADC驱动的读函数 */
static ssize_t s3c2440adc_read(struct file *filp, char *buffer, size_t count, loff_t *ppos)
{
unsigned long result = 0;
unsigned int data;
if (down_interruptible(&s3c2440adc_dev.lock))
return -ERESTARTSYS;
/* 启动AD转换 */
iowrite32(START_ADC(s3c2440adc_dev.channel, s3c2440adc_dev.prescale), base_addr + S3C2410_ADCCON);
interruptible_sleep_on(&s3c2440adc_dev.wait); /* 等待AD完成中断*/
data = ioread32(base_addr + S3C2410_ADCDAT0) & 0x3ff; /* 保留转换数据寄存器的低10位 */
result = ioread32(base_addr + S3C2410_ADCCON);
DPRINTK("AIN[%d] = 0x%x, 0x%x\n", s3c2440adc_dev.channel, data, (unsigned int)result);
copy_to_user(buffer, &data, sizeof(data)); /* 复制到用户空间 */
up(&s3c2440adc_dev.lock);
return sizeof(result);
}
/*文件打开函数*/
static int s3c2440adc_open(struct inode *inode, struct file *filp)
{
init_MUTEX(&s3c2440adc_dev.lock);
init_waitqueue_head(&(s3c2440adc_dev.wait));
filp->private_data = s3c2440adc_devp; /*将设备结构体指针赋值给文件私有数据指针*/
s3c2440adc_dev.channel=0;
s3c2440adc_dev.prescale=0xff;
base_addr = ioremap(ADC_CTL_BASE, 0x14);
adc_clock = clk_get(NULL, "adc");
if (!adc_clock) {
DPRINTK("failed to get adc clock source\n");
return -ENOENT;
}
clk_enable(adc_clock);
DPRINTK( "adc opened\n");
return 0;
}
static int s3c2440adc_release(struct inode *inode, struct file *filp)
{
DPRINTK( "adc closed\n");
return 0;
}
static struct file_operations s3c2440adc_fops = {
owner: THIS_MODULE,
open: s3c2440adc_open,
read: s3c2440adc_read,
write: s3c2440adc_write,
release: s3c2440adc_release,
};
/*初始化并注册cdev*/
static void s3c2440adc_setup_cdev(struct s3c2440adc_dev *dev, int index)
{
int err, devno = MKDEV(s3c2440adc_major, index);
cdev_init(&dev->cdev, &s3c2440adc_fops);
dev->cdev.owner = THIS_MODULE;
dev->cdev.ops = &s3c2440adc_fops;
err = cdev_add(&dev->cdev, devno, 1);
if (err)
DPRINTK("Error %d adding devno%d", err, index);
}
int __init s3c2440adc_init(void)
{
int result;
dev_t devno = MKDEV(s3c2440adc_major, 0);
/* 申请中断 */
result = request_irq(IRQ_ADC, adcdone_int_handler, SA_INTERRUPT, DEVICE_NAME, NULL);
/* 中断号被占用 */
if (result) {
DPRINTK("IRQ%d already in use\n", IRQ_ADC);
return result;
}
/* 申请设备号*/
if (s3c2440adc_major)
result = register_chrdev_region(devno, 1, "s3c2440adc");
else /* 动态申请设备号 */
{
result = alloc_chrdev_region(&devno, 0, 1, "s3c2440adc");
s3c2440adc_major = MAJOR(devno);
}
if (result < 0)
return result;
/* 动态申请设备结构体的内存*/
s3c2440adc_devp = kmalloc(sizeof(struct s3c2440adc_dev), GFP_KERNEL);
if (!s3c2440adc_devp) /*申请失败*/
{
result = -ENOMEM;
goto fail_malloc;
}
memset(s3c2440adc_devp, 0, sizeof(struct s3c2440adc_dev));
s3c2440adc_setup_cdev(s3c2440adc_devp, 0);
return 0;
fail_malloc: unregister_chrdev_region(devno, 1);
return result;
}
void __exit s3c2440adc_exit(void)
{
if (adc_clock) {
clk_disable(adc_clock);
//clk_unuse(adc_clock);
clk_put(adc_clock);
adc_clock = NULL;
}
cdev_del(&s3c2440adc_devp->cdev); /*注销cdev*/
kfree(s3c2440adc_devp); /*释放设备结构体内存*/
unregister_chrdev_region(MKDEV(s3c2440adc_major, 0), 1); /*释放设备号*/
iounmap(base_addr); /* 释放申请的虚拟地址 */
free_irq(IRQ_ADC, NULL); /*释放中断*/
}
MODULE_AUTHOR("lxm<lxm650@163.com>");
MODULE_LICENSE("Dual BSD/GPL");
MODULE_DESCRIPTION(VERSION_STRING);
module_init(s3c2440adc_init);
module_exit(s3c2440adc_exit);
makefile:
KERNELDIR = /work/utu-Linux2.6.24 # 鏇挎崲鎴愪綘瀵瑰簲鐨勫唴鏍哥洰褰?
PWD := $(shell pwd)
INSTALLDIR = /work/modules
CROSS_COMPILE = arm-linux-
CC = $(CROSS_COMPILE)gcc
obj-m := s3c2440_adc.o
modules:
$(MAKE) -C $(KERNELDIR) M=$(PWD) modules
modules_install:
cp s3c2440_adc.ko $(INSTALLDIR)
clean:
rm -rf *.o *~ core .depend .*.cmd *.ko *.mod.c .tmp_versions
.PHONY: modules modules_install clean
//测试程序:
/**********************************************
* *AD convert driver test function
* *date : created at 2009-02-16 by lxm
**********************************************/
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <sys/ioctl.h>
#include <errno.h>
#include <fcntl.h>
#include <sys/types.h>
int main(void)
{
int fd;
int chan, pres, result;
unsigned int data;
unsigned long setdata;
float d;
fd = open("/dev/s3c2440adc", O_RDWR);
if (fd < 0) {
printf("Failed to open adc_driver\n");
exit(1);
}
printf("Please select which chanel(0--7): \n");
scanf("%d", &chan);
printf("Please select prescale:\n");
scanf("%d", &pres);
setdata = chan << 16 | pres;
result = write(fd, &setdata, sizeof(setdata));
if (!(result == sizeof(data))) {
printf("wrong when writing!\n");
goto failed;
}
result = read(fd, &data, sizeof(data));
if (!(result == sizeof(data))) {
printf("Wrong size!\n");
goto failed;
}
d = (data * 3.3) / 1024.0;
printf("%8.4f\t", d);
printf("\n");
sleep(1);
}
failed:
close(fd);
return 0;
}