出处:http://blog.csdn.net/geekcome/article/details/6580981
硬件平台:FL2440
内核版本:2.6.28
主机平台:Ubuntu 11.04
内核版本:2.6.39
原创作品,转载请标明出处http://blog.csdn.net/yming0221/article/details/6580981
1、下面是ADC和触摸屏接口的模块图
当触摸屏接口使用时,XM或YM接触摸屏接口的地
当触摸屏接口不使用时,XM或YM接模拟信号,做普通ADC使用。
2、触摸屏接口的几种操作模式
(1) 正常转换模式
通过设置ADCCON(adc控制寄存器)来完成初始化,并对ADCDAT0数据寄存器进行操作。
(2) 分离XY坐标模式
X坐标模式写X坐标转换数据到ADCDAT0,触摸屏接口产生中断到中断控制寄存器。Y坐标模式写Y坐标转换数据到ADCDAT1,触摸屏接口产生中断到中断控制寄存器。两种模
式可以选择一种模式工作。
相应的引脚连接:
(3) 自动XY坐标模式
触摸屏控制器连续的转换X和Y的坐标,在X坐标转换后的值存入ADCDAT0后,自动将Y坐标转换后的值存入ADCDAT1,触摸屏接口产生中断到中断控制器。
相应的引脚连接:
(4) 等待中断模式
当光标被按下,触摸屏控制器产生中断IRQ_TC,当产生中断信号时,等待中断模式必须被清除。
引脚定义如下:
3、下面是s3c2440触摸屏驱动的分析
- //#define CONFIG_TOUCHSCREEN_S3C2410_DEBUG
- #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 <asm/io.h>
- #include <asm/irq.h>
- #include <mach/regs-gpio.h>
- #include <mach/s3c2410_ts.h>
- #include <plat/regs-adc.h>
- #define TRUE 1 //CoAsia added
- #define FALSE 0 //CoAsia added
- #define FILTER_LIMIT 25 //CoAsia added
- /* For ts.dev.id.version */
- #define S3C2410TSVERSION 0x0101
- #define TSC_SLEEP (S3C2410_ADCTSC_PULL_UP_DISABLE | S3C2410_ADCTSC_XY_PST(0))
- #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))
- #define DEBUG_LVL "<3>" //KERN_DEBUG
- static char *s3c2440ts_name = "s3c2440 TouchScreen";
- /*
- * Per-touchscreen data.
- */
- //定义s3c2440触摸屏使用的数据结构体
- struct s3c2440ts {
- struct input_dev *dev;
- long xp;
- long yp;
- int count;
- int shift;
- };
- static struct s3c2440ts ts;
- static struct clk *adc_clock;
- //__iomem声明地址空间是设备地址映射空间
- static void __iomem *base_addr;
- //函数声明
- static void touch_timer_fire(unsigned long data);
- static irqreturn_t tc_irq(int irq, void *dev_id);
- static irqreturn_t adc_irq(int irq, void *dev_id);
- static int __init s3c2440ts_probe(struct platform_device *pdev);
- static int s3c2440ts_remove(struct platform_device *pdev);
- static int s3c2440ts_resume(struct platform_device *pdev);
- //定义定时器
- static struct timer_list touch_timer =
- TIMER_INITIALIZER(touch_timer_fire, 0, 0);
- //IRQ_TC中断处理函数
- static irqreturn_t tc_irq(int irq, void *dev_id)
- {
- //data0,data1用于存放读取的ADCDAT数据寄存器的值
- unsigned long data0;
- unsigned long data1;
- int updown;//用于存放光标的按下或提起的状态
- //读取ADCDAT0、ADCDAT1数据寄存器的值
- data0 = readl(base_addr+S3C2410_ADCDAT0);
- data1 = readl(base_addr+S3C2410_ADCDAT1);
- //查看数据寄存器的第15位的值
- updown = (!(data0 & S3C2410_ADCDAT0_UPDOWN)) && (!(data1 & S3C2410_ADCDAT0_UPDOWN));
- /* 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. */
- //如果data0和data1的第15位都是0,则updown为1,则通过函数touch_timer_fire()函数来启动ADC转换
- if (updown)
- touch_timer_fire(0);
- return IRQ_HANDLED;
- }
- static void touch_timer_fire(unsigned long data)
- {
- //用于存储数据寄存器ADCDAT0、ADCDAT1的值
- unsigned long data0;
- unsigned long data1;
- //用于存放光标是否被按下
- int updown;
- data0 = readl(base_addr+S3C2410_ADCDAT0);
- data1 = readl(base_addr+S3C2410_ADCDAT1);
- updown = (!(data0 & S3C2410_ADCDAT0_UPDOWN)) && (!(data1 & S3C2410_ADCDAT0_UPDOWN));
- //printk("The number of 'updown' is %d ",updown);
- //如果光标被按下,执行
- if (updown)
- {
- //ts.count!=0表示ADC已经转换过,下面就报告事件和光标位置数据
- if (ts.count != 0)
- {
- ts.xp >>= ts.shift;//这里shift为2,这里实际上是求均值,四次的和/4,这样定位更加准确
- ts.yp >>= ts.shift;
- #ifdef CONFIG_TOUCHSCREEN_S3C2410_DEBUG
- {
- struct timeval tv;
- do_gettimeofday(&tv);
- printk(DEBUG_LVL "T: %06d, X: %03ld, Y: %03ld ", (int)tv.tv_usec, ts.xp, ts.yp);
- }
- #endif
- /*
- 下面的函数位于/include/linux/input.h,作用是报告事件
- static inline void input_report_abs(struct input_dev *dev, unsigned int code, int value)
- {
- input_event(dev, EV_ABS, code, value);
- }
- */
- //报告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);
- }
- //现在光标被按下,并且ADC转换没有启动
- ts.xp = 0;
- ts.yp = 0;
- ts.count = 0;
- //设置触摸屏控制寄存器的值为 0xdc B:1101 1100,设置控制寄存器上拉无效,自动转换X,Y坐标
- //printk("S3C2410_ADCTSC: 0x%x ",S3C2410_ADCTSC_PULL_UP_DISABLE | AUTOPST);
- writel(S3C2410_ADCTSC_PULL_UP_DISABLE | AUTOPST, base_addr+S3C2410_ADCTSC);
- //启动ADC转换
- writel(readl(base_addr+S3C2410_ADCCON) | S3C2410_ADCCON_ENABLE_START, base_addr+S3C2410_ADCCON);
- }
- else//光标没有被按下
- {
- ts.count = 0;
- //报告事件及光标的位置状态
- input_report_key(ts.dev, BTN_TOUCH, 0);
- input_report_abs(ts.dev, ABS_PRESSURE, 0);
- //等待接收方的应答,用于同步
- input_sync(ts.dev);
- //设置触摸屏控制寄存器为等待中断模式
- writel(WAIT4INT(0), base_addr+S3C2410_ADCTSC);
- }
- }
- static irqreturn_t adc_irq(int irq, void *dev_id)
- {
- //用于存放数据寄存器的数据
- unsigned long data0;
- unsigned long data1;
- //读取数据,这次主要读取的是位置数据
- data0 = readl(base_addr+S3C2410_ADCDAT0);
- data1 = readl(base_addr+S3C2410_ADCDAT1);
- ts.xp += data0 & S3C2410_ADCDAT0_XPDATA_MASK;//累加四次准换结果的X坐标和
- ts.yp += data1 & S3C2410_ADCDAT1_YPDATA_MASK;//累加四次准换结果的Y坐标和
- ts.count++;//转换次数加一
- //如果转换次数小于4
- if (ts.count < (1<<ts.shift))
- {
- //再次设置触摸屏控制寄存器上拉不使能、自动X、Y转换模式
- writel(S3C2410_ADCTSC_PULL_UP_DISABLE | AUTOPST, base_addr+S3C2410_ADCTSC);
- //再次启动ADC转换
- writel(readl(base_addr+S3C2410_ADCCON) | S3C2410_ADCCON_ENABLE_START, base_addr+S3C2410_ADCCON);
- }
- else//这时,ADC转换四次完成,延迟一个系统滴答,执行touch_timer_fire()函数
- {
- mod_timer(&touch_timer, jiffies+1);
- writel(WAIT4INT(1), base_addr+S3C2410_ADCTSC);
- }
- return IRQ_HANDLED;
- }
- /*
- * The functions for inserting/removing us as a module.
- */
- /*
- 该结构体定义在/include/linux/platform_device.h
- struct platform_device {
- const char * name;
- int id;
- struct device dev;
- u32 num_resources;
- struct resource * resource;
- };
- */
- static int __init s3c2440ts_probe(struct platform_device *pdev)
- {
- int rc;
- /*
- 下面结构体定义在/include/mach/s3c2410_ts.h
- struct s3c2410_ts_mach_info {
- int delay;
- int presc;
- int oversampling_shift;
- };
- */
- struct s3c2410_ts_mach_info *info;
- struct input_dev *input_dev;
- /*
- void *platform_data;//Platform specific data, device core doesn't touch it
- */
- info = ( struct s3c2440_ts_mach_info *)pdev->dev.platform_data;
- if (!info)
- {
- printk(KERN_ERR "Hm... too bad : no platform data for ts ");
- return -EINVAL;
- }
- #ifdef CONFIG_TOUCHSCREEN_S3C2410_DEBUG
- printk(DEBUG_LVL "Entering s3c2440ts_init ");
- #endif
- //由于ADC转换需要时钟,这里获取时钟
- adc_clock = clk_get(NULL, "adc");
- if (!adc_clock) {
- printk(KERN_ERR "failed to get adc clock source ");
- return -ENOENT;
- }
- clk_enable(adc_clock);//使能时钟
- #ifdef CONFIG_TOUCHSCREEN_S3C2410_DEBUG
- printk(DEBUG_LVL "got and enabled clock ");
- #endif
- //通过ioremap实现物理地址到虚拟地址的转换
- base_addr = ioremap(S3C2410_PA_ADC,0x20);
- if (base_addr == NULL) {
- printk(KERN_ERR "Failed to remap register block ");
- return -ENOMEM;
- }
- //设置ADCCON控制寄存器为0x4c40,设置预分频有效,预分频值为B:110001 D:49
- //printk("ADCCON is 0x%x ",S3C2410_ADCCON_PRSCEN | S3C2410_ADCCON_PRSCVL(info->presc&0xFF));
- if ((info->presc&0xff) > 0)
- writel(S3C2410_ADCCON_PRSCEN | S3C2410_ADCCON_PRSCVL(info->presc&0xFF),
- base_addr+S3C2410_ADCCON);
- else
- writel(0,base_addr+S3C2410_ADCCON);
- /* Initialise registers */
- /*
- 设置ADC开始延时寄存器ADCDLY: 0x4e20
- */
- //printk("ADCDLY: 0x%x ",info->delay & 0xffff);
- if ((info->delay&0xffff) > 0)
- writel(info->delay & 0xffff, base_addr+S3C2410_ADCDLY);
- /*
- 设置ADC触摸屏控制寄存器ADC_TSC: 0xd3 B:1101 0011
- [8]检测光标按下中断信号
- [7]YM输出驱动有效(GND)
- [6]YP输出驱动无效(AIN5)
- [5]XM输出驱动无效(Hi-z)
- [4]XP输出驱动无效(AIN7)
- [3]XP上拉有效
- [2]普通ADC转换
- [0:1]等待中断模式 测量X和Y的坐标
- */
- //printk("ADC_TSC: 0x%x ",WAIT4INT(0));
- writel(WAIT4INT(0), base_addr+S3C2410_ADCTSC);
- /* Initialise input stuff */
- memset(&ts, 0, sizeof(struct s3c2440ts));
- /*
- 下面的函数
- 为新的输入设备分配内存。
- 使用free_device()释放没有被注册的函数,使用input_unregister_device()解除已经注册的设备
- 定义在/drivers/input/input.c
- struct input_dev *input_allocate_device(void)
- {
- struct input_dev *dev;
- dev = kzalloc(sizeof(struct input_dev), GFP_KERNEL);
- if (dev) {
- dev->dev.type = &input_dev_type;
- dev->dev.class = &input_class;
- device_initialize(&dev->dev);
- mutex_init(&dev->mutex);
- spin_lock_init(&dev->event_lock);
- INIT_LIST_HEAD(&dev->h_list);
- INIT_LIST_HEAD(&dev->node);
- __module_get(THIS_MODULE);
- }
- return dev;
- }
- */
- input_dev = input_allocate_device();
- if (!input_dev) {
- printk(KERN_ERR "Unable to allocate the input device !! ");
- return -ENOMEM;
- }
- //下面初始化输入设备信息
- ts.dev = input_dev;
- ts.dev->evbit[0] = BIT_MASK(EV_SYN) | BIT_MASK(EV_KEY) |
- BIT_MASK(EV_ABS);
- ts.dev->keybit[BIT_WORD(BTN_TOUCH)] = BIT_MASK(BTN_TOUCH);
- input_set_abs_params(ts.dev, ABS_X, 0, 0x3FF, 0, 0);
- input_set_abs_params(ts.dev, ABS_Y, 0, 0x3FF, 0, 0);
- input_set_abs_params(ts.dev, ABS_PRESSURE, 0, 1, 0, 0);
- //ts.dev->private = &ts;
- ts.dev->name = s3c2440ts_name;
- ts.dev->id.bustype = BUS_RS232;
- ts.dev->id.vendor = 0xDEAD;
- ts.dev->id.product = 0xBEEF;
- ts.dev->id.version = S3C2410TSVERSION;
- ts.shift = info->oversampling_shift;
- //printk("shift: %d ",ts.shift);
- /* Get irqs */
- //申请ADC中断,注意,中断类型为IRQF_SAMPLE_RANDOM | IRQF_SHARED,这样在使用触摸屏的时候
- //可以调试自己的ADC转换驱动,中断处理函数为adc_irq
- if (request_irq(IRQ_ADC, adc_irq, IRQF_SAMPLE_RANDOM | IRQF_SHARED,
- "s3c2440_action", ts.dev)) {
- printk(KERN_ERR "s3c2440_ts.c: Could not allocate ts IRQ_ADC ! ");
- iounmap(base_addr);
- return -EIO;
- }
- //申请TC中断,中断处理函数为tc_irq
- if (request_irq(IRQ_TC, tc_irq, IRQF_SAMPLE_RANDOM,
- "s3c2440_action", ts.dev)) {
- printk(KERN_ERR "s3c2440_ts.c: Could not allocate ts IRQ_TC ! ");
- free_irq(IRQ_ADC, ts.dev);
- iounmap(base_addr);
- return -EIO;
- }
- printk(KERN_INFO "%s successfully loaded ", s3c2440ts_name);
- /* All went ok, so register to the input system */
- /*这里注册设备
- 函数功能:
- * This function registers device with input core. The device must be
- * allocated with input_allocate_device() and all it's capabilities
- * set up before registering.
- * If function fails the device must be freed with input_free_device().
- * Once device has been successfully registered it can be unregistered
- * with input_unregister_device(); input_free_device() should not be
- * called in this case.
- 函数原型如下:
- int input_register_device(struct input_dev *dev)
- {
- static atomic_t input_no = ATOMIC_INIT(0);
- struct input_handler *handler;
- const char *path;
- int error;
- __set_bit(EV_SYN, dev->evbit);
- init_timer(&dev->timer);
- if (!dev->rep[REP_DELAY] && !dev->rep[REP_PERIOD]) {
- dev->timer.data = (long) dev;
- dev->timer.function = input_repeat_key;
- dev->rep[REP_DELAY] = 250;
- dev->rep[REP_PERIOD] = 33;
- }
- if (!dev->getkeycode)
- dev->getkeycode = input_default_getkeycode;
- if (!dev->setkeycode)
- dev->setkeycode = input_default_setkeycode;
- snprintf(dev->dev.bus_id, sizeof(dev->dev.bus_id),
- "input%ld", (unsigned long) atomic_inc_return(&input_no) - 1);
- error = device_add(&dev->dev);
- if (error)
- return error;
- path = kobject_get_path(&dev->dev.kobj, GFP_KERNEL);
- printk(KERN_INFO "input: %s as %s ",
- dev->name ? dev->name : "Unspecified device", path ? path : "N/A");
- kfree(path);
- error = mutex_lock_interruptible(&input_mutex);
- if (error) {
- device_del(&dev->dev);
- return error;
- }
- list_add_tail(&dev->node, &input_dev_list);
- list_for_each_entry(handler, &input_handler_list, node)
- input_attach_handler(dev, handler);
- input_wakeup_procfs_readers();
- mutex_unlock(&input_mutex);
- return 0;
- }
- */
- rc = input_register_device(ts.dev);
- if (rc) {
- free_irq(IRQ_TC, ts.dev);
- free_irq(IRQ_ADC, ts.dev);
- clk_disable(adc_clock);
- iounmap(base_addr);
- return -EIO;
- }
- return 0;
- }
- static int s3c2440ts_remove(struct platform_device *pdev)
- {
- 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_put(adc_clock);
- adc_clock = NULL;
- }
- input_unregister_device(ts.dev);
- iounmap(base_addr);
- return 0;
- }
- #ifdef CONFIG_PM
- static int s3c2440ts_suspend(struct platform_device *pdev, pm_message_t state)
- {
- writel(TSC_SLEEP, base_addr+S3C2410_ADCTSC);
- writel(readl(base_addr+S3C2410_ADCCON) | S3C2410_ADCCON_STDBM,
- base_addr+S3C2410_ADCCON);
- disable_irq(IRQ_ADC);
- disable_irq(IRQ_TC);
- clk_disable(adc_clock);
- return 0;
- }
- static int s3c2440ts_resume(struct platform_device *pdev)
- {
- struct s3c2440_ts_mach_info *info =
- ( struct s3c2440_ts_mach_info *)pdev->dev.platform_data;
- clk_enable(adc_clock);
- msleep(1);
- enable_irq(IRQ_ADC);
- enable_irq(IRQ_TC);
- if ((info->presc&0xff) > 0)
- writel(S3C2410_ADCCON_PRSCEN | S3C2410_ADCCON_PRSCVL(info->presc&0xFF),
- base_addr+S3C2410_ADCCON);
- else
- writel(0,base_addr+S3C2410_ADCCON);
- /* Initialise registers */
- if ((info->delay&0xffff) > 0)
- writel(info->delay & 0xffff, base_addr+S3C2410_ADCDLY);
- writel(WAIT4INT(0), base_addr+S3C2410_ADCTSC);
- return 0;
- }
- #else
- #define s3c2440ts_suspend NULL
- #define s3c2440ts_resume NULL
- #endif
- /*
- 下面是/linux/platform_device.h定义的platform_driver结构体
- struct platform_driver {
- int (*probe)(struct platform_device *);//设备的检测,所以需要先前的设备注册
- int (*remove)(struct platform_device *);//删除该设备
- void (*shutdown)(struct platform_device *); //关闭该设备
- int (*suspend)(struct platform_device *, pm_message_t state);
- int (*suspend_late)(struct platform_device *, pm_message_t state);
- int (*resume_early)(struct platform_device *);
- int (*resume)(struct platform_device *);
- struct pm_ext_ops *pm;
- struct device_driver driver;//设备驱动,定义在include/linux/device.h中
- };
- 内核提供的platform_driver结构体的注册函数为platform_driver_register(),该函数定义在driver/base/platform.c中
- */
- static struct platform_driver s3c2440ts_driver = {
- .driver = {
- .name = "s3c2440-ts",
- .owner = THIS_MODULE,
- },
- .probe = s3c2440ts_probe,
- .remove = s3c2440ts_remove,
- .suspend = s3c2440ts_suspend,
- .resume = s3c2440ts_resume,
- };
- static int __init s3c2440ts_init(void)
- {
- int rc;
- rc = platform_driver_register(&s3c2440ts_driver);
- if (rc < 0)
- printk(KERN_ERR "platform_driver_register error! ");
- return rc;
- }
- static void __exit s3c2440ts_exit(void)
- {
- platform_driver_unregister(&s3c2440ts_driver);
- }
- module_init(s3c2440ts_init);
- module_exit(s3c2440ts_exit);
- MODULE_AUTHOR("YANMING");
- MODULE_DESCRIPTION("My s3c2440 touchscreen driver");
- MODULE_LICENSE("GPL");
4、分析完成后对触摸屏的工作过程就有了一个比较明确的认识
从触摸屏被按下到系统相应的过程如下:
(1) 当触摸屏感觉到触摸,触发IRQ_TC中断,然后读取触摸屏控制寄存器的值,判断是否被按下,如果被按下,启动定时器,执行touch_timer_fire()函数启动ADC转换。
(2) ADC转换完成后,会触发IRQ_ADC中断,执行相应的中断处理函数,如果ADC转换次数小于4,再次启动ADC转换;如果ADC转换次数为4,则启动一个系统滴答定时器,执行touch_timer_fire()函数
(3) 执行定时器服务程序时,如果此时触摸屏仍被按下,则上报事件和坐标数据,重复(2);如果没有被按下,上报时间和坐标数据,将触摸屏控制寄存器设置为中断等待状态
可见,触摸屏驱动的服务是一个封闭的循环过程。