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\n ",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\n", (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\n",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\n");
- return -EINVAL;
- }
-
- #ifdef CONFIG_TOUCHSCREEN_S3C2410_DEBUG
- printk(DEBUG_LVL "Entering s3c2440ts_init\n");
- #endif
- //由於ADC轉換需要時鐘,這裡獲取時鐘
- 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);//使能時鐘
-
- #ifdef CONFIG_TOUCHSCREEN_S3C2410_DEBUG
- printk(DEBUG_LVL "got and enabled clock\n");
- #endif
- //通過ioremap實現物理地址到虛擬地址的轉換
- base_addr = ioremap(S3C2410_PA_ADC,0x20);
- if (base_addr == NULL) {
- printk(KERN_ERR "Failed to remap register block\n");
- return -ENOMEM;
- }
-
- //設置ADCCON控制寄存器為0x4c40,設置預分頻有效,預分頻值為B:110001 D:49
- //printk("ADCCON is 0x%x\n",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\n",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\n",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 !!\n");
- 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\n",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 !\n");
- 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 !\n");
- free_irq(IRQ_ADC, ts.dev);
- iounmap(base_addr);
- return -EIO;
- }
-
- printk(KERN_INFO "%s successfully loaded\n", 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\n",
- 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!\n");
- 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);如果沒有被按下,上報時間和坐標數據,將觸摸屏控制寄存器設置為中斷等待狀態
可見,觸摸屏驅動的服務是一個封閉的循環過程。