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);
- }