S3C2440觸摸屏驅動
12 |
#ifndef
__ASM_ARCH_REGS_ADC_H |
13 |
#define
__ASM_ARCH_REGS_ADC_H "regs-adc.h" |
15 |
#define
S3C2410_ADCREG(x) (x) |
17 |
#define
S3C2410_ADCCON S3C2410_ADCREG(0x00) |
18 |
#define
S3C2410_ADCTSC S3C2410_ADCREG(0x04) |
19 |
#define
S3C2410_ADCDLY S3C2410_ADCREG(0x08) |
20 |
#define
S3C2410_ADCDAT0 S3C2410_ADCREG(0x0C) |
21 |
#define
S3C2410_ADCDAT1 S3C2410_ADCREG(0x10) |
22 |
#define
S3C64XX_ADCUPDN S3C2410_ADCREG(0x14) |
23 |
#define
S3C64XX_ADCCLRINT S3C2410_ADCREG(0x18) |
24 |
#define
S3C64XX_ADCCLRINTPNDNUP S3C2410_ADCREG(0x20) |
28 |
#define
S3C64XX_ADCCON_RESSEL (1<<16) |
29 |
#define
S3C2410_ADCCON_ECFLG (1<<15) |
30 |
#define
S3C2410_ADCCON_PRSCEN (1<<14) |
31 |
#define
S3C2410_ADCCON_PRSCVL(x) (((x)&0xFF)<<6) |
32 |
#define
S3C2410_ADCCON_PRSCVLMASK (0xFF<<6) |
33 |
#define
S3C2410_ADCCON_SELMUX(x) (((x)&0x7)<<3) |
34 |
#define
S3C2410_ADCCON_MUXMASK (0x7<<3) |
35 |
#define
S3C2410_ADCCON_STDBM (1<<2) |
36 |
#define
S3C2410_ADCCON_READ_START (1<<1) |
37 |
#define
S3C2410_ADCCON_ENABLE_START (1<<0) |
38 |
#define
S3C2410_ADCCON_STARTMASK (0x3<<0) |
42 |
#define
S3C2410_ADCTSC_YM_SEN (1<<7) |
43 |
#define
S3C2410_ADCTSC_YP_SEN (1<<6) |
44 |
#define
S3C2410_ADCTSC_XM_SEN (1<<5) |
45 |
#define
S3C2410_ADCTSC_XP_SEN (1<<4) |
46 |
#define
S3C2410_ADCTSC_PULL_UP_DISABLE (1<<3) |
47 |
#define
S3C2410_ADCTSC_AUTO_PST (1<<2) |
48 |
#define
S3C2410_ADCTSC_XY_PST(x) (((x)&0x3)<<0) |
51 |
#define
S3C2410_ADCDAT0_UPDOWN (1<<15) |
52 |
#define
S3C2410_ADCDAT0_AUTO_PST (1<<14) |
53 |
#define
S3C2410_ADCDAT0_XY_PST (0x3<<12) |
54 |
#define
S3C2410_ADCDAT0_XPDATA_MASK (0x03FF) |
57 |
#define
S3C2410_ADCDAT1_UPDOWN (1<<15) |
58 |
#define
S3C2410_ADCDAT1_AUTO_PST (1<<14) |
59 |
#define
S3C2410_ADCDAT1_XY_PST (0x3<<12) |
60 |
#define
S3C2410_ADCDAT1_YPDATA_MASK (0x03FF) |
62 |
#endif
/** __ASM_ARCH_REGS_ADC_H */ |
一.硬件簡介
S3C2440觸摸屏接口與ADC接口集成在一起,觸摸屏X、Y座標所產生的模擬信號通過通道7、5輸入,2440提供觸摸屏接口有4種處理模式:普通轉換模式、分離的X/Y軸座標轉換模式、自動X/Y軸座標轉換模式、等待中斷模式。具體參考2440硬件手冊16章。
二.驅動實現
下面是觸摸屏驅動源碼,其中使用了linux輸入子系統input。暫時還沒研究這一塊,想深入瞭解可參考相關資料。
-
#include <linux/errno.h>
-
#include <linux/kernel.h>
-
#include <linux/module.h>
-
#include <linux/slab.h>
-
#include <linux/input.h>
-
#include <linux/init.h>
-
#include <linux/serio.h>
-
#include <linux/delay.h>
-
#include <linux/platform_device.h>
-
#include <linux/clk.h>
-
#include <linux/gpio.h>
-
#include <asm/io.h>
-
#include <asm/irq.h>
-
#include <plat/regs-adc.h>
-
#include <mach/regs-gpio.h>
-
-
#define S3C2410TSVERSION 0x0101
-
#define DEVICE_NAME "YC2440_TS"
-
-
-
#define WAIT4INT(x) (((x)>>8)|S3C2410_ADCTSC_YM_SEN|S3C2410_ADCTSC_YP_SEN|S3C2410_ADCTSC_XP_SEN|S3C2410_ADCTSC_XY_PST(3))
-
#define AUTOPST (S3C2410_ADCTSC_YM_SEN|S3C2410_ADCTSC_YP_SEN|S3C2410_ADCTSC_XP_SEN|\
-
S3C2410_ADCTSC_AUTO_PST|S3C2410_ADCTSC_XY_PST(0))
-
-
static struct input_dev *dev;
-
static long xp;
-
static long yp;
-
static int count;
-
-
extern struct semaphore ADC_LOCK;
-
static int ownADC = 0;
-
-
static struct clk *adc_clk;
-
-
static void __iomem *base_addr;
-
-
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;
-
-
tmp = xp;
-
xp = yp;
-
yp = tmp;
-
-
xp >>= 2;
-
yp >>= 2;
-
-
#ifdef CONFIG_TOUCHSCREEN_DEBUG
-
struct timeval tv;
-
do_gettimeofday(&tv);
-
printk(KERN_DEBUG "T:%06d, X:%03d, Y:%03d\n",(int)tv.tv_usec,xp,yp);
-
#endif
-
-
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;
-
-
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
-
{
-
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)
-
{
-
printk(KERN_INFO "up\n");
-
ownADC = 0;
-
up(&ADC_LOCK);
-
}
-
}
-
}
-
-
static struct timer_list touch_timer=TIMER_INITIALIZER(touch_timer_fire,0,0);
-
-
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)
-
{
-
printk(KERN_INFO "down\n");
-
touch_timer_fire(0);
-
}
-
else
-
{
-
printk(KERN_INFO "up-irq\n");
-
ownADC = 0;
-
up(&ADC_LOCK);
-
}
-
}
-
return IRQ_HANDLED;
-
}
-
-
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;
-
yp += data1 & S3C2410_ADCDAT1_YPDATA_MASK;
-
count++;
-
-
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);
-
iowrite32(WAIT4INT(1),base_addr+S3C2410_ADCTSC);
-
}
-
}
-
-
return IRQ_HANDLED;
-
}
-
-
static int __init s3c2440ts_init(void)
-
{
-
int ret;
-
-
adc_clk = clk_get(NULL,"adc");
-
if(!adc_clk)
-
{
-
printk(KERN_ERR "Failed to get adc clock\n");
-
return -ENOENT;
-
}
-
-
clk_enable(adc_clk);
-
-
base_addr = ioremap(S3C2410_PA_ADC,0x20);
-
if(base_addr==NULL)
-
{
-
printk(KERN_ERR "Failed to remap register\n");
-
ret = -EINVAL;
-
goto err_remap;
-
}
-
-
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);
-
-
dev = input_allocate_device();
-
if(!dev)
-
{
-
printk(KERN_ERR "unable to allocate input_dev\n");
-
ret = -ENOMEM;
-
goto err_alloc;
-
}
-
-
-
-
dev->evbit[0] = BIT(EV_SYN)|BIT(EV_KEY)|BIT(EV_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 = DEVICE_NAME;
-
dev->id.bustype = BUS_RS232;
-
dev->id.vendor = 0xDEAD;
-
dev->id.product = 0xBEEF;
-
dev->id.version = S3C2410TSVERSION;
-
-
-
ret = request_irq(IRQ_ADC,stylus_action,IRQF_SHARED|IRQF_SAMPLE_RANDOM,DEVICE_NAME,dev);
-
if(ret)
-
{
-
printk(KERN_ERR "request IRQ_ADC fail\n");
-
ret = -EINVAL;
-
goto err_alloc;
-
}
-
-
ret = request_irq(IRQ_TC,stylus_updown,IRQF_SAMPLE_RANDOM,DEVICE_NAME,dev);
-
if(ret)
-
{
-
printk(KERN_ERR "requers IRQ_TS fail\n");
-
ret = -EINVAL;
-
goto err_adcirq;
-
}
-
-
printk(KERN_INFO "%s successfully load\n",DEVICE_NAME);
-
-
-
input_register_device(dev);
-
return 0;
-
-
-
-
err_adcirq:
-
free_irq(IRQ_ADC,dev);
-
-
err_alloc:
-
iounmap(base_addr);
-
-
err_remap:
-
clk_disable(adc_clk);
-
clk_put(adc_clk);
-
-
return ret;
-
}
-
-
static void __exit s3c2440ts_exit(void)
-
{
-
disable_irq(IRQ_ADC);
-
disable_irq(IRQ_TC);
-
free_irq(IRQ_ADC,dev);
-
free_irq(IRQ_TC,dev);
-
-
if(adc_clk);
-
{
-
clk_disable(adc_clk);
-
clk_put(adc_clk);
-
adc_clk = NULL;
-
}
-
-
input_unregister_device(dev);
-
iounmap(base_addr);
-
}
-
-
module_init(s3c2440ts_init);
-
module_exit(s3c2440ts_exit);
-
-
MODULE_LICENSE("GPL");
-
MODULE_AUTHOR("Zechin Liao");
(1)如果觸摸屏感覺到觸摸,則觸發觸摸屏中斷即進入stylus_updown,獲取ADC_LOCK後判斷觸摸屏狀態爲按下,則調用touch_timer_fire啓動ADC轉換;
(2)當ADC轉換啓動後,觸發ADC中斷即進入stylus_action,如果這一次轉換的次數小於4,則重新啓動ADC進行轉換,如果4次完畢後,啓動1個時間滴答的定時器,停止ADC轉換,也就是說在這個時間滴答內,ADC轉換是停止的;
(3)這裏爲什麼要在1個時間滴答到來之前停止ADC的轉換呢?這是爲了防止屏幕抖動。
(4)如果1個時間滴答到來則進入定時器服務程序touch_timer_fire,判斷觸摸屏仍然處於按下狀態則上報事件和轉換的數據,並重啓ADC轉換,重複第(2)步;
(5)如果觸摸擡起了,則上報釋放事件,並將觸摸屏重新設置爲等待中斷狀態。
這裏採用動態編譯,如果把觸摸屏驅動編譯進內核,參考ADC驅動。
編輯Makefile:
-
ifneq ($(KERNELRELEASE),)
-
obj-m:=s3c2440_ts.o
-
else
-
KERNELDIR:=/home/liao/image/linux-2.6.32.2
-
PWD:=$(shell pwd)
-
all:
-
make -C $(KERNELDIR) M=$(PWD) modules ARCH=arm CROSS_COMPILE=arm-linux-
-
clean:
-
rm -rf *.ko *.o *.mod.c *.mod.o *.symvers
-
endif
# make
# insmod s3c2440_ts.ko
# cat /proc/devices 查看input設備號
# cat /bus/input/devices 查看觸摸屏設備信息
# mkdir /dev/input
# mknod /dev/input/event0 c 13 64 添加設備文件
三.測試
編寫、編譯下面測試程序,運行,點擊觸摸屏會打印採集的AD轉換值。這只是一個簡單應用,真正觸摸屏應用還需要座標轉換、校正,可參考tslib應用。
-
#include <stdio.h>
-
#include <stdlib.h>
-
#include <fcntl.h>
-
#include <linux/input.h>
-
-
struct input_event buff;
-
-
int main(void)
-
{
-
int fd;
-
int ret;
-
int count=0;
-
-
fd = open("/dev/input/event0",O_RDWR);
-
if(fd<0)
-
{
-
perror("can not open event0\n");
-
return -1;
-
}
-
-
while(1)
-
{
-
ret = read(fd,&buff,sizeof(struct input_event));
-
-
printf("type:%d\n code:%d\n,value:%d\n",buff.type,buff.code,buff.value);
-
count++;
-
printf("count=%d\n",count);
-
}
-
close(fd);
-
}