Linux设备驱动工程师之路——触摸屏驱动s3c2410_ts.c分析
K-Style
转载请自 http://blog.csdn.net/ayangke
一、触摸屏硬件知识
1.模块原理图
S3C2440有8路的ADC通道其中触摸屏控制器接口XP,XM,YP,YM与四路ADC通道复用四个IO引脚。从原理图看出8路ADC只有一个A/D转换器,通过一个8选1开关MUX来选通哪一路A/D通道进行转换。触摸屏控制会产生两个中断,一个触摸屏中断INT_ADC,一个ADC_转换完成中断INT_ADC。ADC需要时钟才能工作,因为它需要设置采样率。
再复习一下ARM裸机实验时触摸屏寄存器操作流程
流程:
初始化
1设置采样延时和分频值ADCDLY ADCCON
2中断相关设置
3设置触摸屏AD转换为等待中断模式 ADCTSC
中断服务函数
4清相关挂起寄存器
5设置转换模式,一般为连续x,y转换
6 启动转换
7 转换完成后读取x y坐标ADCDAT0ADCDAT1
8 设置触摸屏AD转换为等待中断模式,设置等待弹起中断ADCTSC
9 弹起中断发生后,设置触摸屏转换为等待中断模式,等待下一次触笔按下rADCTSC
程序
二、触摸屏驱动程序
1.模块初始化
[html] view plain copy print ?
- static int __init s3c2410ts_init(void)
- {
- struct input_dev *input_dev;
-
- //获取时钟
- 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);
- //使能时钟
- //需要时钟的是因为触摸屏要用到ADC转换,而完成ADC转换则需要时钟(采用时间)
-
- //映射ADC的IO内存
- base_addr=ioremap(S3C2410_PA_ADC,0x20);
- if (base_addr == NULL) {
- printk(KERN_ERR "Failed to remap register block\n");
- return -ENOMEM;
- }
-
- //初始化触摸屏的IO引脚
- /* Configure GPIOs */
- s3c2410_ts_connect();
-
- //设置预分频值
- iowrite32(S3C2410_ADCCON_PRSCEN | S3C2410_ADCCON_PRSCVL(0xFF),\
- base_addr+S3C2410_ADCCON);
- iowrite32(0xffff, base_addr+S3C2410_ADCDLY);//设置采用延时
- iowrite32(WAIT4INT(0), base_addr+S3C2410_ADCTSC);
- //设置触摸屏控制器为等待按下中断
-
- /* Initialise input stuff */
- //申请一个input设备
- input_dev = input_allocate_device();
-
- if (!input_dev) {
- printk(KERN_ERR "Unable to allocate the input device !!\n");
- return -ENOMEM;
- }
-
- dev = input_dev;
-
- //设置可被支持的事件为同步、按键、绝对坐标事件
- dev->evbit[0] = BIT(EV_SYN) | BIT(EV_KEY) | BIT(EV_ABS);
- //设置按键时间类型为触摸屏
- dev->keybit[BITS_TO_LONGS(BTN_TOUCH)] = BIT(BTN_TOUCH);
-
- //设置事件数值范围X,Y坐标范围为0到3FF,按键数值范围从0,到1
- input_set_abs_params(dev, ABS_X, 0, 0x3FF, 0, 0);
- input_set_abs_params(dev, ABS_Y, 0, 0x3FF, 0, 0);
- input_set_abs_params(dev, ABS_PRESSURE, 0, 1, 0, 0);
-
- //设置设备的身份信息
- dev->name = s3c2410ts_name;
- dev->id.bustype = BUS_RS232;
- dev->id.vendor = 0xDEAD;
- dev->id.product = 0xBEEF;
- dev->id.version = S3C2410TSVERSION;
-
-
- /* Get irqs */
- //获取触摸屏中断IRQ_TC,ADC转换完成中断IRQ_ADC
- if (request_irq(IRQ_ADC, stylus_action, IRQF_SHARED|IRQF_SAMPLE_RANDOM,
- "s3c2410_action", dev)) {
- printk(KERN_ERR "s3c2410_ts.c: Could not allocate ts IRQ_ADC !\n");
- iounmap(base_addr);
- return -EIO;
- }
- if (request_irq(IRQ_TC, stylus_updown, IRQF_SAMPLE_RANDOM,
- "s3c2410_action", dev)) {
- printk(KERN_ERR "s3c2410_ts.c: Could not allocate ts IRQ_TC !\n");
- iounmap(base_addr);
- return -EIO;
- }
-
- printk(KERN_INFO "%s successfully loaded\n", s3c2410ts_name);
-
- /* All went ok, so register to the input system */
- //注册设备
- input_register_device(dev);
-
- return 0;
- }
static int __init s3c2410ts_init(void)
{
struct input_dev *input_dev;
//获取时钟
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);
//使能时钟
//需要时钟的是因为触摸屏要用到ADC转换,而完成ADC转换则需要时钟(采用时间)
//映射ADC的IO内存
base_addr=ioremap(S3C2410_PA_ADC,0x20);
if (base_addr == NULL) {
printk(KERN_ERR "Failed to remap register block\n");
return -ENOMEM;
}
//初始化触摸屏的IO引脚
/* Configure GPIOs */
s3c2410_ts_connect();
//设置预分频值
iowrite32(S3C2410_ADCCON_PRSCEN | S3C2410_ADCCON_PRSCVL(0xFF),\
base_addr+S3C2410_ADCCON);
iowrite32(0xffff, base_addr+S3C2410_ADCDLY);//设置采用延时
iowrite32(WAIT4INT(0), base_addr+S3C2410_ADCTSC);
//设置触摸屏控制器为等待按下中断
/* Initialise input stuff */
//申请一个input设备
input_dev = input_allocate_device();
if (!input_dev) {
printk(KERN_ERR "Unable to allocate the input device !!\n");
return -ENOMEM;
}
dev = input_dev;
//设置可被支持的事件为同步、按键、绝对坐标事件
dev->evbit[0] = BIT(EV_SYN) | BIT(EV_KEY) | BIT(EV_ABS);
//设置按键时间类型为触摸屏
dev->keybit[BITS_TO_LONGS(BTN_TOUCH)] = BIT(BTN_TOUCH);
//设置事件数值范围X,Y坐标范围为0到3FF,按键数值范围从0,到1
input_set_abs_params(dev, ABS_X, 0, 0x3FF, 0, 0);
input_set_abs_params(dev, ABS_Y, 0, 0x3FF, 0, 0);
input_set_abs_params(dev, ABS_PRESSURE, 0, 1, 0, 0);
//设置设备的身份信息
dev->name = s3c2410ts_name;
dev->id.bustype = BUS_RS232;
dev->id.vendor = 0xDEAD;
dev->id.product = 0xBEEF;
dev->id.version = S3C2410TSVERSION;
/* Get irqs */
//获取触摸屏中断IRQ_TC,ADC转换完成中断IRQ_ADC
if (request_irq(IRQ_ADC, stylus_action, IRQF_SHARED|IRQF_SAMPLE_RANDOM,
"s3c2410_action", dev)) {
printk(KERN_ERR "s3c2410_ts.c: Could not allocate ts IRQ_ADC !\n");
iounmap(base_addr);
return -EIO;
}
if (request_irq(IRQ_TC, stylus_updown, IRQF_SAMPLE_RANDOM,
"s3c2410_action", dev)) {
printk(KERN_ERR "s3c2410_ts.c: Could not allocate ts IRQ_TC !\n");
iounmap(base_addr);
return -EIO;
}
printk(KERN_INFO "%s successfully loaded\n", s3c2410ts_name);
/* All went ok, so register to the input system */
//注册设备
input_register_device(dev);
return 0;
}
我们再来看看模块初始化函数中初始化触摸屏的IO引脚的s3c2410_ts_connect();函数
[html] view plain copy print ?
- static inline void s3c2410_ts_connect(void)
- {
- s3c2410_gpio_cfgpin(S3C2410_GPG12, S3C2410_GPG12_XMON);
- s3c2410_gpio_cfgpin(S3C2410_GPG13, S3C2410_GPG13_nXPON);
- s3c2410_gpio_cfgpin(S3C2410_GPG14, S3C2410_GPG14_YMON);
- s3c2410_gpio_cfgpin(S3C2410_GPG15, S3C2410_GPG15_nYPON);
- }
static inline void s3c2410_ts_connect(void)
{
s3c2410_gpio_cfgpin(S3C2410_GPG12, S3C2410_GPG12_XMON);
s3c2410_gpio_cfgpin(S3C2410_GPG13, S3C2410_GPG13_nXPON);
s3c2410_gpio_cfgpin(S3C2410_GPG14, S3C2410_GPG14_YMON);
s3c2410_gpio_cfgpin(S3C2410_GPG15, S3C2410_GPG15_nYPON);
}
触摸屏控制器接口是与IO端口复用的。
2.触摸屏中断服务函数
[html] view plain copy print ?
- static irqreturn_t stylus_updown(int irq, void *dev_id)
- {
- unsigned long data0;
- unsigned long data1;
- int updown;
-
- if (down_trylock(&ADC_LOCK) == 0) {
- OwnADC = 1;
-
- //读取ADCDAT0和ADCDAT1寄存器,判断是按下中断还是弹起中断
- //ADCDAT0和ADCDAT1查手册可知其第15位当按下时为0,弹起为1
- data0 = ioread32(base_addr+S3C2410_ADCDAT0);
- data1 = ioread32(base_addr+S3C2410_ADCDAT1);
-
- updown = (!(data0 & S3C2410_ADCDAT0_UPDOWN)) && (!(data1 & S3C2410_ADCDAT0_UPDOWN));
-
- if (updown) {
- touch_timer_fire(0);//当判断为按下时
- } else {
- OwnADC = 0;
- up(&ADC_LOCK);
- }
- }
-
- return IRQ_HANDLED;
- }
static irqreturn_t stylus_updown(int irq, void *dev_id)
{
unsigned long data0;
unsigned long data1;
int updown;
if (down_trylock(&ADC_LOCK) == 0) {
OwnADC = 1;
//读取ADCDAT0和ADCDAT1寄存器,判断是按下中断还是弹起中断
//ADCDAT0和ADCDAT1查手册可知其第15位当按下时为0,弹起为1
data0 = ioread32(base_addr+S3C2410_ADCDAT0);
data1 = ioread32(base_addr+S3C2410_ADCDAT1);
updown = (!(data0 & S3C2410_ADCDAT0_UPDOWN)) && (!(data1 & S3C2410_ADCDAT0_UPDOWN));
if (updown) {
touch_timer_fire(0);//当判断为按下时
} else {
OwnADC = 0;
up(&ADC_LOCK);
}
}
return IRQ_HANDLED;
}
我们再来看touch_timer_fire(0);这个函数
[html] view plain copy print ?
- static void touch_timer_fire(unsigned long data)
- {
- unsigned long data0;
- unsigned long data1;
- int updown;
-
- //读取ADCDAT0和ADCDAT1判断为弹起还是按下
- data0 = ioread32(base_addr+S3C2410_ADCDAT0);
- data1 = ioread32(base_addr+S3C2410_ADCDAT1);
-
- updown = (!(data0 & S3C2410_ADCDAT0_UPDOWN)) && (!(data1 & S3C2410_ADCDAT0_UPDOWN));
-
- //当按下时
- if (updown) {
- if (count != 0) {
- //当两次转换完成后
- long tmp;
-
- tmp = xp;
- xp = yp;
- yp = tmp;
-
- xp >>= 2;
- yp >>= 2;
- //求取两次转换坐标的平均值,然后报告
-
- input_report_abs(dev, ABS_X, xp);
- input_report_abs(dev, ABS_Y, yp);
-
- input_report_key(dev, BTN_TOUCH, 1);
- input_report_abs(dev, ABS_PRESSURE, 1);
- input_sync(dev);
- }
-
- xp = 0;
- yp = 0;
- count = 0;
-
- //启动ADC转换
- //设置ADCTSC,转换模式
- iowrite32(S3C2410_ADCTSC_PULL_UP_DISABLE | AUTOPST, base_addr+S3C2410_ADCTSC);
- //设置ADCCON,启动转换
- iowrite32(ioread32(base_addr+S3C2410_ADCCON) | S3C2410_ADCCON_ENABLE_START, base_addr+S3C2410_ADCCON);
- } else {
- //如果是弹起了将count清0,并报告弹起事件
- count = 0;
-
- input_report_key(dev, BTN_TOUCH, 0);
- input_report_abs(dev, ABS_PRESSURE, 0);
- input_sync(dev);
-
- //再次设置触摸屏为等待中断模式
- iowrite32(WAIT4INT(0), base_addr+S3C2410_ADCTSC);
- if (OwnADC) {
- OwnADC = 0;
- up(&ADC_LOCK);
- }
- }
static void touch_timer_fire(unsigned long data)
{
unsigned long data0;
unsigned long data1;
int updown;
//读取ADCDAT0和ADCDAT1判断为弹起还是按下
data0 = ioread32(base_addr+S3C2410_ADCDAT0);
data1 = ioread32(base_addr+S3C2410_ADCDAT1);
updown = (!(data0 & S3C2410_ADCDAT0_UPDOWN)) && (!(data1 & S3C2410_ADCDAT0_UPDOWN));
//当按下时
if (updown) {
if (count != 0) {
//当两次转换完成后
long tmp;
tmp = xp;
xp = yp;
yp = tmp;
xp >>= 2;
yp >>= 2;
//求取两次转换坐标的平均值,然后报告
input_report_abs(dev, ABS_X, xp);
input_report_abs(dev, ABS_Y, yp);
input_report_key(dev, BTN_TOUCH, 1);
input_report_abs(dev, ABS_PRESSURE, 1);
input_sync(dev);
}
xp = 0;
yp = 0;
count = 0;
//启动ADC转换
//设置ADCTSC,转换模式
iowrite32(S3C2410_ADCTSC_PULL_UP_DISABLE | AUTOPST, base_addr+S3C2410_ADCTSC);
//设置ADCCON,启动转换
iowrite32(ioread32(base_addr+S3C2410_ADCCON) | S3C2410_ADCCON_ENABLE_START, base_addr+S3C2410_ADCCON);
} else {
//如果是弹起了将count清0,并报告弹起事件
count = 0;
input_report_key(dev, BTN_TOUCH, 0);
input_report_abs(dev, ABS_PRESSURE, 0);
input_sync(dev);
//再次设置触摸屏为等待中断模式
iowrite32(WAIT4INT(0), base_addr+S3C2410_ADCTSC);
if (OwnADC) {
OwnADC = 0;
up(&ADC_LOCK);
}
}
3.ADC中断
[html] view plain copy print ?
- static irqreturn_t stylus_action(int irq, void *dev_id)
- {
- unsigned long data0;
- unsigned long data1;
-
- if (OwnADC) {
- data0 = ioread32(base_addr+S3C2410_ADCDAT0);
- data1 = ioread32(base_addr+S3C2410_ADCDAT1);
-
- //读取转换后X,Y值
- xp += data0 & S3C2410_ADCDAT0_XPDATA_MASK;
- yp += data1 & S3C2410_ADCDAT1_YPDATA_MASK;
- count++;
-
- //通过对count的判断进行两次转换,两次转换后求和的值将在触摸屏中断里面报告时间的时候除以2求得平均值然后上报。
- if (count < (1<<2)) {
- iowrite32(S3C2410_ADCTSC_PULL_UP_DISABLE | AUTOPST, base_addr+S3C2410_ADCTSC);
- iowrite32(ioread32(base_addr+S3C2410_ADCCON) | S3C2410_ADCCON_ENABLE_START, base_addr+S3C2410_ADCCON);
- } else {
- //当两次转换完成时
- mod_timer(&touch_timer, jiffies+1);//设置定时器的定时值为一个时钟滴答,一个滴答之后touch_timer_fire函数被调用用来报告时间数值
- /*该定时器的初始化如下
- static struct timer_list touch_timer =
- TIMER_INITIALIZER(touch_timer_fire, 0, 0);
- */
- iowrite32(WAIT4INT(1), base_addr+S3C2410_ADCTSC);//重新设置触摸屏为等待中断模式
- }
- }
-
- return IRQ_HANDLED;
- }