一、触摸屏驱动分析
-
static int __init tq2440ts_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时钟
-
-
base_addr=ioremap(S3C2410_PA_ADC,0x20); //将物理基地址转化成虚拟地址
-
if (base_addr == NULL)
-
{
-
printk(KERN_ERR "Failed to remap register block\n");
-
return -ENOMEM;
-
}
-
-
/* Configure GPIOs */
-
// tq2440_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_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); //设置可能的上报事件(同步信息)(按键信息)(abs是坐标)
-
dev->keybit[BITS_TO_LONGS(BTN_TOUCH)] = BIT(BTN_TOUCH);
-
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 = tq2440ts_name; //填充一些设备信息
-
dev->id.bustype = BUS_RS232;
-
dev->id.vendor = 0xDEAD;
-
dev->id.product = 0xBEEF;
-
dev->id.version = S3C2410TSVERSION;
-
-
if (request_irq(IRQ_ADC, stylus_action, IRQF_SHARED|IRQF_SAMPLE_RANDOM, tq2440ts_name, dev)) //注册ADC中断处理程序
-
{
-
printk(KERN_ERR "tq2440_ts.c: Could not allocate ts IRQ_ADC !\n");
-
iounmap(base_addr);
-
return -EIO;
-
}
-
if (request_irq(IRQ_TC, stylus_updown, IRQF_SAMPLE_RANDOM, tq2440ts_name, dev)) //注册TOUCH_SCREEN中断
-
{
-
printk(KERN_ERR "tq2440_ts.c: Could not allocate ts IRQ_ADC !\n");
-
iounmap(base_addr);
-
return -EIO;
-
}
-
-
printk(KERN_INFO "%s successfully loaded\n", tq2440ts_name);
-
-
input_register_device(dev); //注册输入型设备
-
-
return 0;
-
}
在手按下触摸屏时,会触发TC中断,stylus_updown:
-
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;
-
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():
-
static void touch_timer_fire(unsigned long data)
-
{
-
unsigned long data0;
-
unsigned long data1;
-
int updown;
-
-
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) //如果count不是0,4次AD转换后进入
-
{
-
long tmp;
-
-
tmp = xp;
-
xp = yp;
-
yp = tmp;
-
-
xp >>= 2; //这是取平均值
-
yp >>= 2;
-
-
input_report_abs(dev, ABS_X, xp); //可以上报XY坐标了
-
input_report_abs(dev, ABS_Y, yp);
-
-
input_report_key(dev, BTN_TOUCH, 1); //上报按键
-
input_report_abs(dev, ABS_PRESSURE, 1); //上报ABS按下
-
input_sync(dev); //上报结束
-
}
-
-
xp = 0;
-
yp = 0;
-
count = 0;
-
-
iowrite32(S3C2410_ADCTSC_PULL_UP_DISABLE | AUTOPST, base_addr+S3C2410_ADCTSC); //启动xy转换
-
iowrite32(ioread32(base_addr+S3C2410_ADCCON) | S3C2410_ADCCON_ENABLE_START, base_addr+S3C2410_ADCCON);
-
}
-
else
-
{
-
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);
-
}
-
}
-
}
调用adc中断(stylus_action):
-
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);
-
-
xp += data0 & S3C2410_ADCDAT0_XPDATA_MASK; //获取X、Y坐标
-
yp += data1 & S3C2410_ADCDAT1_YPDATA_MASK;
-
count++;
-
-
if (count < (1<<2)) //count<4,意味着获取了4次
-
{
-
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); //启动内核定时器,1个时间就调用touch_timer
-
iowrite32(WAIT4INT(1), base_addr+S3C2410_ADCTSC);
-
}
-
}
-
-
return IRQ_HANDLED;
-
}
touch_timer:
-
static struct timer_list touch_timer =
-
TIMER_INITIALIZER(touch_timer_fire, 0, 0); //又调用了touch_timer_fire,不过此时count是4了
二、触摸屏驱动编程
-
static void touch_timer_fire(unsigned long data)
-
{
-
unsigned long data0;
-
unsigned long data1;
-
int updown;
-
-
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;
-
/*交换XY坐标*/
-
tmp = xp;
-
xp = yp;
-
yp = tmp;
-
/*除以4*/
-
xp >>= 2;
-
yp >>= 2;
-
/*上报XY坐标*/
-
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);
-
-
printk("x=%d,y=%d\n",xp,yp);
-
}
-
-
xp = 0;
-
yp = 0;
-
-
count = 0;
-
/*1.启动自动转换*/
-
iowrite32(1<<2, base_addr+S3C2410_ADCTSC);
-
iowrite32((ioread32(base_addr+S3C2410_ADCCON)|1<<0), base_addr+S3C2410_ADCCON);
-
-
}
-
else
-
{
-
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);
-
}
-
}
-
}
阅读(692) | 评论(0) | 转发(0) |