分类:
2012-02-09 10:59:02
原文地址:电阻式触摸屏的驱动程序和详细注释 作者:hyouyan
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
/* For ts.dev.id.version */
#define S
#define WAIT4INT(x) (((x)<<8) | S
#define AUTOPST (S
#define DEBUG_LVL KERN_DEBUG
MODULE_AUTHOR("Arnaud Patard
MODULE_DESCRIPTION("s
MODULE_LICENSE("GPL");
/*
* Definitions & global arrays.
*/
static char *s
/*
* Per-touchscreen data.
*/
struct s
struct input_dev dev;
long xp;
long yp;
int count;
int shift;
char phys[32];
};
static struct s
static void __iomem *base_addr;
static inline void s
{
s
s
s
s
}
static void touch_timer_fire(unsigned long data)
{
unsigned long data0;
unsigned long data1;
int updown;
/*
0 = Stylus down state
1 = Stylus up state
*/
data0 = readl(base_addr+S
data1 = readl(base_addr+S
updown = (!(data0 & S
/*
更新stylus状态寄存器updown:
1 = down
0 = up
*/
/*
touch_timer_fire这个函数主要实现以下功能:
1、stylus down的时候,在中断函数stylus_updown里面被调用,
此时缓存区没有数据,ts.count为0,所以只是简单的设置ad转换的模式,然后开启ad转换。
2、但ADC中断函数stylus_action把缓冲区填满的时候,作为中断后半段函数稍后被调用,
此时ts.count为4,算出其平均值后,交给事件处理层(Event Handler)处理,
主要是填写缓冲,然后唤醒等待输入数据的进程。
3、stylus抬起,等到缓冲区填满后(可能会包含一些无用的数据)被调用,
这时候判断出stylus up,报告stylus up事件,重新等待stylus down。
if (updown) {
if (ts.count != 0) { <功能2>
/* 求平均值 */
ts.xp >>= ts.shift;
ts.yp >>= ts.shift;
#ifdef CONFIG_TOUCHSCREEN_S
{
struct timeval tv;
do_gettimeofday(&tv);
printk(DEBUG_LVL "T: %06d, X: %03ld, Y: %03ld\n", (int)tv.tv_usec, ts.xp, ts.yp);
}
#endif
/* 报告x、y的绝对坐标值 */
input_report_abs(&ts.dev, ABS_X, ts.xp);
input_report_abs(&ts.dev, ABS_Y, ts.yp);
/* 报告按键事件,键值为1(代表触摸屏对应的按键被按下) */
input_report_key(&ts.dev, BTN_TOUCH, 1);
/* 报告触摸屏的状态,1表明触摸屏被按下 */
input_report_abs(&ts.dev, ABS_PRESSURE, 1);
/* 等待接收方受到数据后回复确认,用于同步 */
input_sync(&ts.dev);
}
<功能1>
ts.xp = 0;
ts.yp = 0;
ts.count = 0;
writel(S
/* 设置触摸屏的模式为AUTOPST */
writel(readl(base_addr+S
/* 启动ADC转换 */
}
else { <功能3>
ts.count = 0;
/* 报告按键事件,键值为1(代表触摸屏对应的按键被释放) */
input_report_key(&ts.dev, BTN_TOUCH, 0);
/* 报告触摸屏的状态,0表明触摸屏没被按下 */
input_report_abs(&ts.dev, ABS_PRESSURE, 0);
/* 等待接收方受到数据后回复确认,用于同步 */
input_sync(&ts.dev);
/* 进入s
writel(WAIT4INT(0), base_addr+S
}
}
static struct timer_list touch_timer =
TIMER_INITIALIZER(touch_timer_fire, 0, 0);
static irqreturn_t stylus_updown(int irq, void *dev_id, struct pt_regs *regs)
{
unsigned long data0;
unsigned long data1;
int updown;
/*
读取ADCDAT0/1寄存器,判断Stylus的状态:
0 = Stylus down state
1 = Stylus up state
*/
data0 = readl(base_addr+S
data1 = readl(base_addr+S
updown = (!(data0 & S
/*
更新stylus状态寄存器updown:
1 = down
0 = up
*/
/* TODO we should never get an interrupt with updown set while
* the timer is running, but maybe we ought to verify that the
* timer isn't running anyways. */
/* 判断出stylus down,调用touch_timer_fire函数 */
if (updown)
touch_timer_fire(0);
return IRQ_HANDLED;
}
static irqreturn_t stylus_action(int irq, void *dev_id, struct pt_regs *regs)
{
unsigned long data0;
unsigned long data1;
data0 = readl(base_addr+S
data1 = readl(base_addr+S
/*
触摸屏的XY线接的是反的,所以只好这样做了
另外,可以参考这个方法:
*/
/**************************modify by lfc********************/
ts.yp += data0 & S
ts.xp += data1 & S
/***********************************************************/
ts.count++;
if (ts.count < (1<
writel(S
writel(readl(base_addr+S
} else {
/* 缓冲区满,激活下半部处理程序touch_timer_fire,处理接收数据 */
mod_timer(&touch_timer, jiffies+1);
writel(WAIT4INT(1), base_addr+S
}
return IRQ_HANDLED;
}
static struct clk *adc_clock;
/*
* The functions for inserting/removing us as a module.
*/
static int __init s
{
struct s
info = ( struct s
注:
s
if (!info)
{
printk(KERN_ERR "Hm... too bad : no platform data for ts\n");
return -EINVAL;
}
#ifdef CONFIG_TOUCHSCREEN_S
printk(DEBUG_LVL "Entering s
#endif
adc_clock = clk_get(NULL, "adc");
if (!adc_clock) {
printk(KERN_ERR "failed to get adc clock source\n");
return -ENOENT;
}
clk_use(adc_clock);//这个在高版本下已经不需要了
clk_enable(adc_clock);
#ifdef CONFIG_TOUCHSCREEN_S
printk(DEBUG_LVL "got and enabled clock\n");
#endif
base_addr=ioremap(S
if (base_addr == NULL) {
printk(KERN_ERR "Failed to remap register block\n");
return -ENOMEM;
}
/* Configure GPIOs */
s
/*以下根据我们提供的s
/* Set the prscvl*/
if ((info->presc&0xff) > 0)
writel(S
base_addr+S
else
writel(0,base_addr+S
/* Initialise the adcdly registers */
if ((info->delay&0xffff) > 0)
writel(info->delay & 0xffff, base_addr+S
/* 进入s
writel(WAIT4INT(0), base_addr+S
/* Initialise input stuff */
memset(&ts, 0, sizeof(struct s
/* 以下配置2.6内核划分出来的输入设备 */
init_input_dev(&ts.dev);
ts.dev.evbit[0] = BIT(EV_SYN) | BIT(EV_KEY) | BIT(EV_ABS);
/*
evbit字段用来定义该输入设备可以支持的(产生和响应)的事件的类型,
在此触摸屏设置为支持同步(EN_SYN)、按键(EN_KEY)、绝对坐标(EV_ABS)事件
*/
ts.dev.keybit[LONG(BTN_TOUCH)] = BIT(BTN_TOUCH);
/* 设置所支持的按键(键值),触摸屏可以看成只有一个按键的设备 */
input_set_abs_params(&ts.dev, ABS_X, 0, 0x3FF, 0, 0);
/* 设置绝对坐标x的最小最大值,在这是0-0x3FF */
input_set_abs_params(&ts.dev, ABS_Y, 0, 0x3FF, 0, 0);
/* 设置绝对坐标y的最小最大值,在这是0-0x3FF */
input_set_abs_params(&ts.dev, ABS_PRESSURE, 0, 1, 0, 0);
sprintf(ts.phys, "ts0");
ts.dev.private = &ts;
ts.dev.name = s
ts.dev.phys = ts.phys;
ts.dev.id.bustype = BUS_RS232;
ts.dev.id.vendor = 0xDEAD;
ts.dev.id.product = 0xBEEF;
ts.dev.id.version = S
ts.shift = info->oversampling_shift;
/*
这个比较重要,配置输入数据的缓存区大小,
在这里oversampling_shift设为2,也就是缓存区的大小为4(1<<2)
*/
/* ADC转换中断,转换结束后触发 */
if (request_irq(IRQ_ADC, stylus_action, SA_SAMPLE_RANDOM,
"s
printk(KERN_ERR "s
iounmap(base_addr);
return -EIO;
}
/*
检测stylus updown的中断,设置为Waiting for Interrupt Mode时,
The controller generates Interrupt (INT_TC) signals when the Stylus is down on Touch Screen Panel,还记得吧^_^
*/
if (request_irq(IRQ_TC, stylus_updown, SA_SAMPLE_RANDOM,
"s
printk(KERN_ERR "s
iounmap(base_addr);
return -EIO;
}
printk(KERN_INFO "%s successfully loaded\n", s
/* All went ok, so register to the input system */
input_register_device(&ts.dev);//注册输入设备
return 0;
}
/* 好了,一切都准备好了,等待stylus updown的发生然后进入IRQ_TC中断处理程序吧^_^ */
static int s
{
disable_irq(IRQ_ADC);
disable_irq(IRQ_TC);
free_irq(IRQ_TC,&ts.dev);
free_irq(IRQ_ADC,&ts.dev);
if (adc_clock) {
clk_disable(adc_clock);
clk_unuse(adc_clock);
clk_put(adc_clock);
adc_clock = NULL;
}
input_unregister_device(&ts.dev);
iounmap(base_addr);
return 0;
}
static struct device_driver s
.name = "s
.bus = &platform_bus_type,
.probe = s
.remove = s
};
int __init s
{
return driver_register(&s
}
void __exit s
{
driver_unregister(&s
}
module_init(s
module_exit(s
附录:
见/arch/arm/mach-s
static struct s
.delay = 10000,
.presc = 49,
.oversampling_shift = 2,
};
struct platform_device s
.name = "s
.id = -1,
.dev = {
.platform_data = &sbc2410_ts_platdata,
}
};
EXPORT_SYMBOL(s