6410触摸屏驱动分析(s3c-ts.c)(Linux)(分析)

转自:http://www.cnblogs.com/liu_xf/archive/2011/06/22/2086750.html


摘要:

分析内核s3c-ts.c源码,看它是如何采集坐标信息及防抖动处理的。

介绍:

直接上源码吧,完全注释:


[cpp]  view plain copy
  1. 001 /* linux/drivers/input/touchscreen/s3c-ts.c   
  2.  
  3. 002  *   
  4.  
  5. 003  * This program is free software; you can redistribute it and/or modify   
  6.  
  7. 004  * it under the terms of the GNU General Public License as published by   
  8.  
  9. 005  * the Free Software Foundation; either version 2 of the License, or   
  10.  
  11. 006  * (at your option) any later version.   
  12.  
  13. 007  *   
  14.  
  15. 008  * This program is distributed in the hope that it will be useful,   
  16.  
  17. 009  * but WITHOUT ANY WARRANTY; without even the implied warranty of   
  18.  
  19. 010  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the   
  20.  
  21. 011  * GNU General Public License for more details.   
  22.  
  23. 012  *   
  24.  
  25. 013  * You should have received a copy of the GNU General Public License   
  26.  
  27. 014  * along with this program; if not, write to the Free Software   
  28.  
  29. 015  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA   
  30.  
  31. 016  *   
  32.  
  33. 017  * a misc driver for mini6410 touch screen   
  34.  
  35. 018  *  by FriendlyARM 2010   
  36.  
  37. 019  *   
  38.  
  39. 020  * Based on following software:   
  40.  
  41. 021  *   
  42.  
  43. 022  ** Copyright (c) 2004 Arnaud Patard <arnaud.patard@rtp-net.org>   
  44.  
  45. 023  ** iPAQ H1940 touchscreen support   
  46.  
  47. 024  **   
  48.  
  49. 025  ** ChangeLog   
  50.  
  51. 026  **   
  52.  
  53. 027  ** 2004-09-05: Herbert Potzl <herbert@13thfloor.at>   
  54.  
  55. 028  ** - added clock (de-)allocation code   
  56.  
  57. 029  **   
  58.  
  59. 030  ** 2005-03-06: Arnaud Patard <arnaud.patard@rtp-net.org>   
  60.  
  61. 031  **      - h1940_ -> s3c24xx (this driver is now also used on the n30   
  62.  
  63. 032  **        machines :P)   
  64.  
  65. 033  **      - Debug messages are now enabled with the config option   
  66.  
  67. 034  **        TOUCHSCREEN_S3C_DEBUG   
  68.  
  69. 035  **      - Changed the way the value are read   
  70.  
  71. 036  **      - Input subsystem should now work   
  72.  
  73. 037  **      - Use ioremap and readl/writel   
  74.  
  75. 038  **   
  76.  
  77. 039  ** 2005-03-23: Arnaud Patard <arnaud.patard@rtp-net.org>   
  78.  
  79. 040  **      - Make use of some undocumented features of the touchscreen   
  80.  
  81. 041  **        controller   
  82.  
  83. 042  **   
  84.  
  85. 043  ** 2006-09-05: Ryu Euiyoul <ryu.real@gmail.com>   
  86.  
  87. 044  **      - added power management suspend and resume code   
  88.  
  89. 045  *   
  90.  
  91. 046  */   
  92.   
  93. 047      
  94.   
  95. 048 #include <linux/errno.h>    
  96.   
  97. 049 #include <linux/kernel.h>    
  98.   
  99. 050 #include <linux/module.h>    
  100.   
  101. 051 #include <linux/slab.h>    
  102.   
  103. 052 #include <linux/input.h>    
  104.   
  105. 053 #include <linux/init.h>    
  106.   
  107. 054 #include <linux/serio.h>    
  108.   
  109. 055 #include <linux/delay.h>    
  110.   
  111. 056 #include <linux/platform_device.h>    
  112.   
  113. 057 #include <linux/clk.h>    
  114.   
  115. 058 #include <linux/fs.h>    
  116.   
  117. 059 #include <linux/poll.h>    
  118.   
  119. 060 #include <linux/irq.h>    
  120.   
  121. 061 #include <linux/interrupt.h>    
  122.   
  123. 062 #include <linux/cdev.h>    
  124.   
  125. 063 #include <linux/miscdevice.h>    
  126.   
  127. 064      
  128.   
  129. 065 #include <asm/uaccess.h>    
  130.   
  131. 066 #include <asm/io.h>    
  132.   
  133. 067 #include <asm/irq.h>    
  134.   
  135. 068 #include <mach/hardware.h>    
  136.   
  137. 069      
  138.   
  139. 070 #include <plat/regs-adc.h>    
  140.   
  141. 071 #include <mach/irqs.h>    
  142.   
  143. 072 #include <mach/map.h>    
  144.   
  145. 073 #include <mach/regs-clock.h>    
  146.   
  147. 074 #include <mach/regs-gpio.h>    
  148.   
  149. 075 #include <mach/gpio-bank-a.h>    
  150.   
  151. 076 #include <mach/ts.h>    
  152.   
  153. 077      
  154.   
  155. 078 #define CONFIG_TOUCHSCREEN_S3C_DEBUG    
  156.   
  157. 079 #undef CONFIG_TOUCHSCREEN_S3C_DEBUG    
  158.   
  159. 080 #define DEBUG_LVL       KERN_DEBUG    
  160.   
  161. 081      
  162.   
  163. 082      
  164.   
  165. 083 #ifdef CONFIG_MINI6410_ADC    
  166.   
  167. 084 DEFINE_SEMAPHORE(ADC_LOCK);  //定义并初始化了一个信号量    
  168.   
  169. 085                           //37内核就没有DECLARE_MUTEX了吧,功能应该是一样的    
  170.   
  171. 086      
  172.   
  173. 087      
  174.   
  175. 088 /* Indicate who is using the ADC controller */   
  176.   
  177. 089 //ADC的状态,防止触摸屏转换时,ADC正在被使用    
  178.   
  179. 090 #define LOCK_FREE       0    
  180.   
  181. 091 #define LOCK_TS         1    
  182.   
  183. 092 #define LOCK_ADC        2    
  184.   
  185. 093 static int adc_lock_id = LOCK_FREE;    
  186.   
  187. 094      
  188.   
  189. 095 #define ADC_free()      (adc_lock_id == LOCK_FREE)      
  190.   
  191. 096 #define ADC_locked4TS() (adc_lock_id == LOCK_TS)    
  192.   
  193. 097      
  194.   
  195. 098 //==    
  196.   
  197. 099 static inline int s3c_ts_adc_lock(int id) {    
  198.   
  199. 100     int ret;    
  200.   
  201. 101      
  202.   
  203. 102     ret = down_trylock(&ADC_LOCK); //获取自旋锁    
  204.   
  205. 103     if (!ret) {    
  206.   
  207. 104         adc_lock_id = id;    
  208.   
  209. 105     }    
  210.   
  211. 106      
  212.   
  213. 107     return ret;  //返回状态  1:失败  0:成功    
  214.   
  215. 108 }    
  216.   
  217. 109 //--    
  218.   
  219. 110      
  220.   
  221. 111 static inline void s3c_ts_adc_unlock(void) {    
  222.   
  223. 112     adc_lock_id = 0;    
  224.   
  225. 113     up(&ADC_LOCK);  //释放自旋锁    
  226.   
  227. 114 }    
  228.   
  229. 115 #endif    
  230.   
  231. 116      
  232.   
  233. 117      
  234.   
  235. 118 /* Touchscreen default configuration */   
  236.   
  237. 119 struct s3c_ts_mach_info s3c_ts_default_cfg __initdata = {    
  238.   
  239. 120     .delay              = 10000,  //转换延时    
  240.   
  241. 121     .presc              = 49,  //转换时钟分频    
  242.   
  243. 122     .oversampling_shift = 2,      //转换次数  4次    
  244.   
  245. 123     .resol_bit          = 12, //转换精度    
  246.   
  247. 124     .s3c_adc_con        = ADC_TYPE_2   //6410是type2    
  248.   
  249. 125 };    
  250.   
  251. 126 /*   
  252.  
  253. 127 struct s3c_ts_mach_info s3c_ts_default_cfg __initdata = {   
  254.  
  255. 128     .delay              = 10000,   
  256.  
  257. 129     .presc              = 49,   
  258.  
  259. 130     .oversampling_shift = 2,   
  260.  
  261. 131     .resol_bit          = 10   
  262.  
  263. 132 };   
  264.  
  265. 133 */   
  266.   
  267. 134 /*   
  268.  
  269. 135  * Definitions & global arrays.   
  270.  
  271. 136  */   
  272.   
  273. 137 #define DEVICE_NAME     "touchscreen"    
  274.   
  275. 138 static DECLARE_WAIT_QUEUE_HEAD(ts_waitq); //定义并初始化一个等待队列    
  276.   
  277. 139      
  278.   
  279. 140 typedef unsigned        TS_EVENT;    
  280.   
  281. 141 #define NR_EVENTS       64     //触摸屏fifo大小    
  282.   
  283. 142      
  284.   
  285. 143 static TS_EVENT         events[NR_EVENTS];    
  286.   
  287. 144 static int              evt_head, evt_tail; //fifo的头的尾    
  288.   
  289. 145                                                             //驱动写fifo时evt_head++,应用读fifo时 evt_tail++    
  290.   
  291. 146      
  292.   
  293. 147 #define ts_evt_pending()    ((volatile u8)(evt_head != evt_tail))   //相等就表示满了    
  294.   
  295. 148 #define ts_evt_get()        (events + evt_tail)    
  296.   
  297. 149 #define ts_evt_pull()       (evt_tail = (evt_tail + 1) & (NR_EVENTS - 1))    
  298.   
  299. 150 #define ts_evt_clear()      (evt_head = evt_tail = 0)    
  300.   
  301. 151      
  302.   
  303. 152 //将AD转换的值放入FIFO    
  304.   
  305. 153 //这里是一个先进先出的fifo    
  306.   
  307. 154 //只要有数据被添加进来,就会唤醒ts_waitq进程    
  308.   
  309. 155 static void ts_evt_add(unsigned x, unsigned y, unsigned down) {    
  310.   
  311. 156     unsigned ts_event;    
  312.   
  313. 157     int next_head;    
  314.   
  315. 158      
  316.   
  317. 159     ts_event = ((x << 16) | (y)) | (down << 31);    
  318.   
  319. 160     next_head = (evt_head + 1) & (NR_EVENTS - 1);    
  320.   
  321. 161         //没满就装入    
  322.   
  323. 162     if (next_head != evt_tail) {    
  324.   
  325. 163         events[evt_head] = ts_event;    
  326.   
  327. 164         evt_head = next_head;    
  328.   
  329. 165         //printk("====>Add ... [ %4d,  %4d ]%s\n", x, y, down ? "":" ~~~");    
  330.   
  331. 166      
  332.   
  333. 167         /* wake up any read call */   
  334.   
  335. 168         if (waitqueue_active(&ts_waitq)) { //判斷等待隊列是否有進程睡眠    
  336.   
  337. 169             wake_up_interruptible(&ts_waitq);  //唤醒ts_waitq等待队列中所有interruptible类型的进程    
  338.   
  339. 170         }    
  340.   
  341. 171     } else {    
  342.   
  343. 172         /* drop the event and try to wakeup readers */   
  344.   
  345. 173         printk(KERN_WARNING "mini6410-ts: touch event buffer full");    
  346.   
  347. 174         wake_up_interruptible(&ts_waitq);    
  348.   
  349. 175     }    
  350.   
  351. 176 }    
  352.   
  353. 177      
  354.   
  355. 178 static unsigned int s3c_ts_poll( struct file *file, struct poll_table_struct *wait)    
  356.   
  357. 179 {    
  358.   
  359. 180     unsigned int mask = 0;    
  360.   
  361. 181      
  362.   
  363. 182     //将ts_waitq等待队列添加到poll_table里去    
  364.   
  365. 183     poll_wait(file, &ts_waitq, wait);     
  366.   
  367. 184     //返回掩码                                      
  368.   
  369. 185     if (ts_evt_pending())    
  370.   
  371. 186         mask |= POLLIN | POLLRDNORM;  //返回设备可读    
  372.   
  373. 187      
  374.   
  375. 188     return mask;    
  376.   
  377. 189 }    
  378.   
  379. 190      
  380.   
  381. 191 //读 系统调用==    
  382.   
  383. 192 static int s3c_ts_read(struct file *filp, char __user *buff, size_t count, loff_t *offp)    
  384.   
  385. 193 {    
  386.   
  387. 194     DECLARE_WAITQUEUE(wait, current); //把当前进程加到定义的等待队列头wait中     
  388.   
  389. 195     char *ptr = buff;    
  390.   
  391. 196     int err = 0;    
  392.   
  393. 197      
  394.   
  395. 198     add_wait_queue(&ts_waitq, &wait); //把wait入到等待队列头中。该队列会在进程等待的条件满足时唤醒它。    
  396.   
  397. 199                                       //我们必须在其他地方写相关代码,在事件发生时,对等的队列执行wake_up()操作。    
  398.   
  399. 200                                       //这里是在ts_evt_add里wake_up    
  400.   
  401. 201     while (count >= sizeof(TS_EVENT)) {    
  402.   
  403. 202         err = -ERESTARTSYS;    
  404.   
  405. 203         if (signal_pending(current)) //如果是信号唤醒    参考http://www.360doc.com/content/10/1009/17/1317564_59632874.shtml    
  406.   
  407. 204             break;    
  408.   
  409. 205      
  410.   
  411. 206         if (ts_evt_pending()) {    
  412.   
  413. 207             TS_EVENT *evt = ts_evt_get();    
  414.   
  415. 208      
  416.   
  417. 209             err = copy_to_user(ptr, evt, sizeof(TS_EVENT));    
  418.   
  419. 210             ts_evt_pull();    
  420.   
  421. 211      
  422.   
  423. 212             if (err)    
  424.   
  425. 213                 break;    
  426.   
  427. 214      
  428.   
  429. 215             ptr += sizeof(TS_EVENT);    
  430.   
  431. 216             count -= sizeof(TS_EVENT);    
  432.   
  433. 217             continue;    
  434.   
  435. 218         }    
  436.   
  437. 219      
  438.   
  439. 220         set_current_state(TASK_INTERRUPTIBLE); //改变进程状态为可中断的睡眠    
  440.   
  441. 221         err = -EAGAIN;    
  442.   
  443. 222         if (filp->f_flags & O_NONBLOCK) //如果上层调用是非阻塞方式,则不阻塞该进程,直接返回EAGAIN    
  444.   
  445. 223             break;    
  446.   
  447. 224         schedule();  //本进程在此处交出CPU控制权,等待被唤醒    
  448.   
  449. 225                       //进程调度的意思侧重于把当前任务从CPU拿掉,再从就绪队列中按照调度算法取一就绪进程占用CPU    
  450.   
  451. 226     }    
  452.   
  453. 227     current->state = TASK_RUNNING;    
  454.   
  455. 228     remove_wait_queue(&ts_waitq, &wait);    
  456.   
  457. 229      
  458.   
  459. 230     return ptr == buff ? err : ptr - buff;    
  460.   
  461. 231 }    
  462.   
  463. 232 //--    
  464.   
  465. 233      
  466.   
  467. 234 static int s3c_ts_open(struct inode *inode, struct file *filp) {    
  468.   
  469. 235     /* flush event queue */   
  470.   
  471. 236     ts_evt_clear();    
  472.   
  473. 237      
  474.   
  475. 238     return 0;    
  476.   
  477. 239 }    
  478.   
  479. 240      
  480.   
  481. 241 //当应用程序操作设备文件时调用的open read等函数,最终会调用这个结构体中对应的函数    
  482.   
  483. 242 static struct file_operations dev_fops = {    
  484.   
  485. 243     .owner              = THIS_MODULE,    
  486.   
  487. 244     .read               = s3c_ts_read,    
  488.   
  489. 245     .poll               = s3c_ts_poll,  //select系统调用    
  490.   
  491. 246     .open               = s3c_ts_open,    
  492.   
  493. 247 };    
  494.   
  495. 248      
  496.   
  497. 249 //设备号,设备名,注册的时候用到这个数组    
  498.   
  499. 250 //混杂设备主设备号为10    
  500.   
  501. 251 static struct miscdevice misc = {    
  502.   
  503. 252         .minor              = MISC_DYNAMIC_MINOR, //自动分配次设置号    
  504.   
  505. 253     //.minor                = 180,     
  506.   
  507. 254     .name               = DEVICE_NAME,    
  508.   
  509. 255     .fops               = &dev_fops,    
  510.   
  511. 256 };    
  512.   
  513. 257      
  514.   
  515. 258 //x为0时为等待按下中断,x为1是为等待抬起中断    
  516.   
  517. 259 #define WAIT4INT(x)     (((x) << 8) | \    
  518.   
  519. 260         S3C_ADCTSC_YM_SEN | S3C_ADCTSC_YP_SEN | S3C_ADCTSC_XP_SEN | \    
  520.   
  521. 261         S3C_ADCTSC_XY_PST(3))    
  522.   
  523. 262      
  524.   
  525. 263 //自动连续测量X坐标和Y坐标    
  526.   
  527. 264 #define AUTOPST         (S3C_ADCTSC_YM_SEN | S3C_ADCTSC_YP_SEN | S3C_ADCTSC_XP_SEN | \    
  528.   
  529. 265         S3C_ADCTSC_AUTO_PST | S3C_ADCTSC_XY_PST(0))    
  530.   
  531. 266      
  532.   
  533. 267 static void __iomem     *ts_base;    
  534.   
  535. 268 static struct resource  *ts_mem;    
  536.   
  537. 269 static struct resource  *ts_irq;    
  538.   
  539. 270 static struct clk       *ts_clock;    
  540.   
  541. 271 static struct s3c_ts_info   *ts;    
  542.   
  543. 272      
  544.   
  545. 273 /**   
  546.  
  547. 274  * get_down - return the down state of the pen   
  548.  
  549. 275  * @data0: The data read from ADCDAT0 register.   
  550.  
  551. 276  * @data1: The data read from ADCDAT1 register.   
  552.  
  553. 277  *   
  554.  
  555. 278  * Return non-zero if both readings show that the pen is down.   
  556.  
  557. 279  */   
  558.   
  559. 280 static inline bool get_down(unsigned long data0, unsigned long data1)    
  560.   
  561. 281 {    
  562.   
  563. 282     /* returns true if both data values show stylus down */   
  564.   
  565. 283     return (!(data0 & S3C_ADCDAT0_UPDOWN) && !(data1 & S3C_ADCDAT1_UPDOWN)); //判断data0,data1最高位是否仍为"0",为“0”表示触摸笔状态保持为down    
  566.   
  567. 284 }    
  568.   
  569. 285      
  570.   
  571. 286      
  572.   
  573. 287 /*===========================================================================================   
  574.  
  575. 288     touch_timer_fire这个函数主要实现以下功能:   
  576.  
  577. 289         
  578.  
  579. 290     1、  触摸笔开始点击的时候, 在中断函数stylus_updown里面被调用,   
  580.  
  581. 291          此时缓存区没有数据,ts.count为0,   并且开启AD转换,而后进入 AD 中断   
  582.  
  583. 292              
  584.  
  585. 293     2、  ADC中断函数stylus_action把缓冲区填满的时候,作为中断后半段函数稍后被调用(由内核定时器触发中断),   
  586.  
  587. 294          此时ts.count为4,算出其平均值后,交给事件处理层(Event Handler)处理,   
  588.  
  589. 295          主要是填写缓冲,然后唤醒等待输入数据的进程。   
  590.  
  591. 296              
  592.  
  593. 297     3、  stylus抬起,等到缓冲区填满后(可能会包含一些无用的数据)被调用,   
  594.  
  595. 298          这时候判断出stylus up,报告stylus up事件,重新等待stylus down。   
  596.  
  597. 299 ============================================================================================*/   
  598.   
  599. 300      
  600.   
  601. 301 static void touch_timer_fire(unsigned long data) {    
  602.   
  603. 302     unsigned long data0;    
  604.   
  605. 303     unsigned long data1;    
  606.   
  607. 304     int pendown;    
  608.   
  609. 305      
  610.   
  611. 306 #ifdef CONFIG_MINI6410_ADC    
  612.   
  613. 307     if (!ADC_locked4TS()) {    
  614.   
  615. 308         /* Note: pen UP interrupt detected and handled, the lock is released,   
  616.  
  617. 309          * so do nothing in the timer which started by ADC ISR. */   
  618.   
  619. 310         return;    
  620.   
  621. 311     }    
  622.   
  623. 312 #endif    
  624.   
  625. 313      
  626.   
  627. 314     data0 = readl(ts_base + S3C_ADCDAT0);    
  628.   
  629. 315     data1 = readl(ts_base + S3C_ADCDAT1);//读取AD转换数据的值    
  630.   
  631. 316      
  632.   
  633. 317     pendown = get_down(data0, data1);    
  634.   
  635. 318      
  636.   
  637. 319     if (pendown) {    
  638.   
  639. 320         if (ts->count == (1 << ts->shift)) {  //定时器触发touch_timer_fire中断时执行这个括号里    
  640.   
  641. 321 #ifdef CONFIG_TOUCHSCREEN_S3C_DEBUG    
  642.   
  643. 322             {    
  644.   
  645. 323                 struct timeval tv;    
  646.   
  647. 324                 do_gettimeofday(&tv);    
  648.   
  649. 325                 printk(KERN_INFO "T: %06d, X: %03ld, Y: %03ld\n",    
  650.   
  651. 326                         (int)tv.tv_usec, ts->xp, ts->yp);    
  652.   
  653. 327             }    
  654.   
  655. 328 #endif    
  656.   
  657. 329      
  658.   
  659. 330             ts_evt_add((ts->xp >> ts->shift), (ts->yp >> ts->shift), 1);//求平均,并写入fifo    
  660.   
  661. 331      
  662.   
  663. 332             ts->xp = 0;    
  664.   
  665. 333             ts->yp = 0;    
  666.   
  667. 334             ts->count = 0;    
  668.   
  669. 335         }    
  670.   
  671. 336      
  672.   
  673. 337         /* start automatic sequencing A/D conversion */   
  674.   
  675. 338         //每次按下有四次AD转换,以下为在按下中断中触发的第一次AD转换,其余三次在AD转换中断处理函数中触发    
  676.   
  677. 339         //AUTOPST表示自动连续测量 以得到X位置,Y位置    
  678.   
  679. 340         writel(S3C_ADCTSC_PULL_UP_DISABLE | AUTOPST, ts_base + S3C_ADCTSC);    
  680.   
  681. 341         // 启动D转换,转换后会产生中断IRQ_ADC    
  682.   
  683. 342         writel(readl(ts_base + S3C_ADCCON) | S3C_ADCCON_ENABLE_START,    
  684.   
  685. 343                 ts_base + S3C_ADCCON);    
  686.   
  687. 344              
  688.   
  689. 345     } else {  //如果是松开,报告其触摸笔状态    
  690.   
  691. 346         ts->xp = 0;    
  692.   
  693. 347         ts->yp = 0;    
  694.   
  695. 348         ts->count = 0;    
  696.   
  697. 349      
  698.   
  699. 350         ts_evt_add(0, 0, 0);    
  700.   
  701. 351      
  702.   
  703. 352         /* PEN is UP, Let's wait the PEN DOWN interrupt */   
  704.   
  705. 353         writel(WAIT4INT(0), ts_base + S3C_ADCTSC);  // 设置INT 位,等待 DOWN 中断    
  706.   
  707. 354      
  708.   
  709. 355 #ifdef CONFIG_MINI6410_ADC    
  710.   
  711. 356         if (ADC_locked4TS()) {    
  712.   
  713. 357             s3c_ts_adc_unlock();    
  714.   
  715. 358         }    
  716.   
  717. 359 #endif    
  718.   
  719. 360     }    
  720.   
  721. 361 }    
  722.   
  723. 362      
  724.   
  725. 363 static DEFINE_TIMER(touch_timer, touch_timer_fire, 0, 0);    
  726.   
  727. 364      
  728.   
  729. 365 //触摸屏按下松开中断服务==    
  730.   
  731. 366 static irqreturn_t stylus_updown(int irqno, void *param)    
  732.   
  733. 367 {    
  734.   
  735. 368 #ifdef CONFIG_TOUCHSCREEN_S3C_DEBUG    
  736.   
  737. 369     unsigned long data0;    
  738.   
  739. 370     unsigned long data1;    
  740.   
  741. 371     int is_waiting_up;    
  742.   
  743. 372     int pendown;    
  744.   
  745. 373 #endif    
  746.   
  747. 374      
  748.   
  749. 375 #ifdef CONFIG_MINI6410_ADC    
  750.   
  751. 376     if (!ADC_locked4TS()) {    
  752.   
  753. 377         if (s3c_ts_adc_lock(LOCK_TS)) {    
  754.   
  755. 378             /* Locking ADC controller failed */   
  756.   
  757. 379             printk("Lock ADC failed, %d\n", adc_lock_id);    
  758.   
  759. 380             return IRQ_HANDLED;    
  760.   
  761. 381         }    
  762.   
  763. 382     }    
  764.   
  765. 383 #endif    
  766.   
  767. 384      
  768.   
  769. 385 #ifdef CONFIG_TOUCHSCREEN_S3C_DEBUG    
  770.   
  771. 386     data0 = readl(ts_base + S3C_ADCDAT0);    
  772.   
  773. 387     data1 = readl(ts_base + S3C_ADCDAT1);    
  774.   
  775. 388      
  776.   
  777. 389     is_waiting_up = readl(ts_base + S3C_ADCTSC) & (1 << 8);    
  778.   
  779. 390     pendown = get_down(data0, data1);    
  780.   
  781. 391      
  782.   
  783. 392     printk("P: %d <--> %c\n", pendown, is_waiting_up ? 'u':'d');    
  784.   
  785. 393 #endif    
  786.   
  787. 394         //执行如下语句否则不断产生中断从而把系统卡死    
  788.   
  789. 395     if (ts->s3c_adc_con == ADC_TYPE_2) {    
  790.   
  791. 396         /* Clear ADC and PEN Down/UP interrupt */   
  792.   
  793. 397         __raw_writel(0x0, ts_base + S3C_ADCCLRWK);    
  794.   
  795. 398         __raw_writel(0x0, ts_base + S3C_ADCCLRINT);    
  796.   
  797. 399     }    
  798.   
  799. 400      
  800.   
  801. 401     /* TODO we should never get an interrupt with pendown set while   
  802.  
  803. 402      * the timer is running, but maybe we ought to verify that the   
  804.  
  805. 403      * timer isn't running anyways. */   
  806.   
  807. 404      
  808.   
  809. 405     touch_timer_fire(1);    
  810.   
  811. 406      
  812.   
  813. 407     return IRQ_HANDLED;    
  814.   
  815. 408 }    
  816.   
  817. 409      
  818.   
  819. 410 //ad转换结束中断服务程序==    
  820.   
  821. 411 static irqreturn_t stylus_action(int irqno, void *param)    
  822.   
  823. 412 {    
  824.   
  825. 413     unsigned long data0;    
  826.   
  827. 414     unsigned long data1;    
  828.   
  829. 415      
  830.   
  831. 416 #ifdef CONFIG_MINI6410_ADC    
  832.   
  833. 417     if (!ADC_locked4TS()) {    
  834.   
  835. 418         if (ADC_free()) {    
  836.   
  837. 419             printk("Unexpected\n");    
  838.   
  839. 420      
  840.   
  841. 421             /* Clear ADC interrupt */   
  842.   
  843. 422             __raw_writel(0x0, ts_base + S3C_ADCCLRINT);    
  844.   
  845. 423         }    
  846.   
  847. 424      
  848.   
  849. 425         return IRQ_HANDLED;    
  850.   
  851. 426     }    
  852.   
  853. 427 #endif    
  854.   
  855. 428      
  856.   
  857. 429     data0 = readl(ts_base + S3C_ADCDAT0);    
  858.   
  859. 430     data1 = readl(ts_base + S3C_ADCDAT1);    
  860.   
  861. 431      
  862.   
  863. 432     if (ts->resol_bit == 12) {    
  864.   
  865. 433 #if defined(CONFIG_TOUCHSCREEN_NEW)    
  866.   
  867. 434         ts->yp += S3C_ADCDAT0_XPDATA_MASK_12BIT - (data0 & S3C_ADCDAT0_XPDATA_MASK_12BIT);    
  868.   
  869. 435         ts->xp += S3C_ADCDAT1_YPDATA_MASK_12BIT - (data1 & S3C_ADCDAT1_YPDATA_MASK_12BIT);    
  870.   
  871. 436 #else     
  872.   
  873. 437         ts->xp += data0 & S3C_ADCDAT0_XPDATA_MASK_12BIT;    
  874.   
  875. 438         ts->yp += data1 & S3C_ADCDAT1_YPDATA_MASK_12BIT;    
  876.   
  877. 439 #endif    
  878.   
  879. 440     } else {    
  880.   
  881. 441 #if defined(CONFIG_TOUCHSCREEN_NEW)    
  882.   
  883. 442         ts->yp += S3C_ADCDAT0_XPDATA_MASK - (data0 & S3C_ADCDAT0_XPDATA_MASK);    
  884.   
  885. 443         ts->xp += S3C_ADCDAT1_YPDATA_MASK - (data1 & S3C_ADCDAT1_YPDATA_MASK);    
  886.   
  887. 444 #else    
  888.   
  889. 445         ts->xp += data0 & S3C_ADCDAT0_XPDATA_MASK;    
  890.   
  891. 446         ts->yp += data1 & S3C_ADCDAT1_YPDATA_MASK;    
  892.   
  893. 447 #endif    
  894.   
  895. 448     }  // 转换结果累加    
  896.   
  897. 449      
  898.   
  899. 450     ts->count++;    
  900.   
  901. 451      
  902.   
  903. 452     if (ts->count < (1 << ts->shift)) { // 采样未完成,继续下一次采样 ,通过 ENABLE_START 启动 AD 转换,一次一个数据    
  904.   
  905. 453         writel(S3C_ADCTSC_PULL_UP_DISABLE | AUTOPST, ts_base + S3C_ADCTSC);    
  906.   
  907. 454         writel(readl(ts_base + S3C_ADCCON) | S3C_ADCCON_ENABLE_START, ts_base + S3C_ADCCON);    
  908.   
  909. 455     } else {  // 采样完毕,激活下半部处理程序touch_timer_fire,处理接收数据      
  910.   
  911. 456         mod_timer(&touch_timer, jiffies + 1);   //设置定时器超时的时间,目的是为了延时触发 touch_timer_fire 中断,如果在这段时间有抬起中断发生,则表示是抖动    
  912.   
  913. 457                                                  //jiffies变量记录了系统启动以来,系统定时器已经触发的次数。内核每秒钟将jiffies变量增加HZ次。    
  914.   
  915. 458                                                  //因此,对于HZ值为100的系统,1个jiffy等于10ms,而对于HZ为1000的系统,1个jiffy仅为1ms    
  916.   
  917. 459                                                      
  918.   
  919. 460         writel(WAIT4INT(1), ts_base + S3C_ADCTSC);   //设置为等待抬起中断    
  920.   
  921. 461     }    
  922.   
  923. 462      
  924.   
  925. 463     if (ts->s3c_adc_con == ADC_TYPE_2) {    
  926.   
  927. 464         /* Clear ADC and PEN Down/UP interrupt */   
  928.   
  929. 465         __raw_writel(0x0, ts_base + S3C_ADCCLRWK);    
  930.   
  931. 466         __raw_writel(0x0, ts_base + S3C_ADCCLRINT);    
  932.   
  933. 467     }    
  934.   
  935. 468      
  936.   
  937. 469     return IRQ_HANDLED;    
  938.   
  939. 470 }    
  940.   
  941. 471      
  942.   
  943. 472      
  944.   
  945. 473 #ifdef CONFIG_MINI6410_ADC    
  946.   
  947. 474 static unsigned int _adccon, _adctsc, _adcdly;    
  948.   
  949. 475      
  950.   
  951. 476 //其它模块要用ADC时,需要调用这个函数,来确定ADC是否可用,如果可用,则将它锁住,不让别的驱动用    
  952.   
  953. 477 int mini6410_adc_acquire_io(void) {    
  954.   
  955. 478     int ret;    
  956.   
  957. 479      
  958.   
  959. 480     ret = s3c_ts_adc_lock(LOCK_ADC); //锁住ADC,不让其它模块使用    
  960.   
  961. 481     if (!ret) {  //如果ADC没有被使用,则保存ADC寄存器的值    
  962.   
  963. 482         _adccon = readl(ts_base + S3C_ADCCON);    
  964.   
  965. 483         _adctsc = readl(ts_base + S3C_ADCTSC);    
  966.   
  967. 484         _adcdly = readl(ts_base + S3C_ADCDLY);    
  968.   
  969. 485     }    
  970.   
  971. 486      
  972.   
  973. 487     return ret;// 0:操作成功  1:操作失败    
  974.   
  975. 488 }    
  976.   
  977. 489 EXPORT_SYMBOL(mini6410_adc_acquire_io);  //声明为外部可用    
  978.   
  979. 490      
  980.   
  981. 491 //其它模块不要用ADC了,需要调用这个函数,来解锁ADC让别的驱动用    
  982.   
  983. 492 void mini6410_adc_release_io(void) {    
  984.   
  985. 493         //还原ADC寄存器的设置    
  986.   
  987. 494     writel(_adccon, ts_base + S3C_ADCCON);    
  988.   
  989. 495     writel(_adctsc, ts_base + S3C_ADCTSC);    
  990.   
  991. 496     writel(_adcdly, ts_base + S3C_ADCDLY);    
  992.   
  993. 497     writel(WAIT4INT(0), ts_base + S3C_ADCTSC);    
  994.   
  995. 498      
  996.   
  997. 499     s3c_ts_adc_unlock(); //释放ADC,其它模块可以使用    
  998.   
  999. 500 }    
  1000.   
  1001. 501      
  1002.   
  1003. 502 EXPORT_SYMBOL(mini6410_adc_release_io);    
  1004.   
  1005. 503 #endif    
  1006.   
  1007. 504      
  1008.   
  1009. 505 //获得触摸屏的配置信息==    
  1010.   
  1011. 506 static struct s3c_ts_mach_info *s3c_ts_get_platdata(struct device *dev)    
  1012.   
  1013. 507 {    
  1014.   
  1015. 508     if (dev->platform_data != NULL)    
  1016.   
  1017. 509         return (struct s3c_ts_mach_info *)dev->platform_data;  //优先使用 arch/arm/mach-s3c64xx中的定义    
  1018.   
  1019. 510      
  1020.   
  1021. 511     return &s3c_ts_default_cfg;   //如果前面没定义,则使用本函数的default定义    
  1022.   
  1023. 512 }    
  1024.   
  1025. 513 //--    
  1026.   
  1027. 514      
  1028.   
  1029. 515 /*   
  1030.  
  1031. 516  * The functions for inserting/removing us as a module.   
  1032.  
  1033. 517  */   
  1034.   
  1035. 518 static int __init s3c_ts_probe(struct platform_device *pdev)    
  1036.   
  1037. 519 {    
  1038.   
  1039. 520     struct resource *res;    
  1040.   
  1041. 521     struct device *dev;    
  1042.   
  1043. 522     struct s3c_ts_mach_info * s3c_ts_cfg;    
  1044.   
  1045. 523     int ret, size;    
  1046.   
  1047. 524      
  1048.   
  1049. 525     dev = &pdev->dev;    
  1050.   
  1051. 526      
  1052.   
  1053. 527     res = platform_get_resource(pdev, IORESOURCE_MEM, 0);    
  1054.   
  1055. 528     if (res == NULL) {    
  1056.   
  1057. 529         dev_err(dev,"no memory resource specified\n");    
  1058.   
  1059. 530         return -ENOENT;    
  1060.   
  1061. 531     }    
  1062.   
  1063. 532      
  1064.   
  1065. 533     size = (res->end - res->start) + 1;    
  1066.   
  1067. 534     ts_mem = request_mem_region(res->start, size, pdev->name);    
  1068.   
  1069. 535     if (ts_mem == NULL) {    
  1070.   
  1071. 536         dev_err(dev, "failed to get memory region\n");    
  1072.   
  1073. 537         ret = -ENOENT;    
  1074.   
  1075. 538         goto err_req;    
  1076.   
  1077. 539     }    
  1078.   
  1079. 540      
  1080.   
  1081. 541     ts_base = ioremap(res->start, size);    
  1082.   
  1083. 542     if (ts_base == NULL) {    
  1084.   
  1085. 543         dev_err(dev, "failed to ioremap() region\n");    
  1086.   
  1087. 544         ret = -EINVAL;    
  1088.   
  1089. 545         goto err_map;    
  1090.   
  1091. 546     }    
  1092.   
  1093. 547      
  1094.   
  1095. 548     ts_clock = clk_get(&pdev->dev, "adc");    
  1096.   
  1097. 549     if (IS_ERR(ts_clock)) {    
  1098.   
  1099. 550         dev_err(dev, "failed to find watchdog clock source\n");    
  1100.   
  1101. 551         ret = PTR_ERR(ts_clock);    
  1102.   
  1103. 552         goto err_clk;    
  1104.   
  1105. 553     }    
  1106.   
  1107. 554      
  1108.   
  1109. 555     clk_enable(ts_clock);    
  1110.   
  1111. 556      
  1112.   
  1113. 557     s3c_ts_cfg = s3c_ts_get_platdata(&pdev->dev);  //获取配置参数    
  1114.   
  1115. 558              
  1116.   
  1117. 559     //设置ADC分频    
  1118.   
  1119. 560     if ((s3c_ts_cfg->presc & 0xff) > 0)      
  1120.   
  1121. 561         writel(S3C_ADCCON_PRSCEN | S3C_ADCCON_PRSCVL(s3c_ts_cfg->presc & 0xff),    
  1122.   
  1123. 562                 ts_base + S3C_ADCCON);    
  1124.   
  1125. 563     else   
  1126.   
  1127. 564         writel(0, ts_base + S3C_ADCCON);    
  1128.   
  1129. 565      
  1130.   
  1131. 566     /* Initialise registers */   
  1132.   
  1133. 567     //设置转换延时    
  1134.   
  1135. 568     if ((s3c_ts_cfg->delay & 0xffff) > 0)    
  1136.   
  1137. 569         writel(s3c_ts_cfg->delay & 0xffff, ts_base + S3C_ADCDLY);    
  1138.   
  1139. 570      
  1140.   
  1141. 571     if (s3c_ts_cfg->resol_bit == 12) {    
  1142.   
  1143. 572         switch(s3c_ts_cfg->s3c_adc_con) {    
  1144.   
  1145. 573             case ADC_TYPE_2:    
  1146.   
  1147. 574                 writel(readl(ts_base + S3C_ADCCON) | S3C_ADCCON_RESSEL_12BIT,    
  1148.   
  1149. 575                         ts_base + S3C_ADCCON);    
  1150.   
  1151. 576                 break;    
  1152.   
  1153. 577      
  1154.   
  1155. 578             case ADC_TYPE_1:    
  1156.   
  1157. 579                 writel(readl(ts_base + S3C_ADCCON) | S3C_ADCCON_RESSEL_12BIT_1,    
  1158.   
  1159. 580                         ts_base + S3C_ADCCON);    
  1160.   
  1161. 581                 break;    
  1162.   
  1163. 582      
  1164.   
  1165. 583             default:    
  1166.   
  1167. 584                 dev_err(dev, "Touchscreen over this type of AP isn't supported !\n");    
  1168.   
  1169. 585                 break;    
  1170.   
  1171. 586         }    
  1172.   
  1173. 587     }    
  1174.   
  1175. 588      
  1176.   
  1177. 589     writel(WAIT4INT(0), ts_base + S3C_ADCTSC);    
  1178.   
  1179. 590      
  1180.   
  1181. 591     ts = kzalloc(sizeof(struct s3c_ts_info), GFP_KERNEL);    
  1182.   
  1183. 592      
  1184.   
  1185. 593     ts->shift = s3c_ts_cfg->oversampling_shift;    
  1186.   
  1187. 594     ts->resol_bit = s3c_ts_cfg->resol_bit;    
  1188.   
  1189. 595     ts->s3c_adc_con = s3c_ts_cfg->s3c_adc_con;    
  1190.   
  1191. 596      
  1192.   
  1193. 597     /* For IRQ_PENDUP */   
  1194.   
  1195. 598     ts_irq = platform_get_resource(pdev, IORESOURCE_IRQ, 0);    
  1196.   
  1197. 599     if (ts_irq == NULL) {    
  1198.   
  1199. 600         dev_err(dev, "no irq resource specified\n");    
  1200.   
  1201. 601         ret = -ENOENT;    
  1202.   
  1203. 602         goto err_irq;    
  1204.   
  1205. 603     }    
  1206.   
  1207. 604      
  1208.   
  1209. 605     ret = request_irq(ts_irq->start, stylus_updown, IRQF_SAMPLE_RANDOM, "s3c_updown", ts);    
  1210.   
  1211. 606     if (ret != 0) {    
  1212.   
  1213. 607         dev_err(dev,"s3c_ts.c: Could not allocate ts IRQ_PENDN !\n");    
  1214.   
  1215. 608         ret = -EIO;    
  1216.   
  1217. 609         goto err_irq;    
  1218.   
  1219. 610     }    
  1220.   
  1221. 611      
  1222.   
  1223. 612     /* For IRQ_ADC */   
  1224.   
  1225. 613     ts_irq = platform_get_resource(pdev, IORESOURCE_IRQ, 1);    
  1226.   
  1227. 614     if (ts_irq == NULL) {    
  1228.   
  1229. 615         dev_err(dev, "no irq resource specified\n");    
  1230.   
  1231. 616         ret = -ENOENT;    
  1232.   
  1233. 617         goto err_irq;    
  1234.   
  1235. 618     }    
  1236.   
  1237. 619      
  1238.   
  1239. 620     ret = request_irq(ts_irq->start, stylus_action, IRQF_SAMPLE_RANDOM | IRQF_SHARED,    
  1240.   
  1241. 621             "s3c_action", ts);    
  1242.   
  1243. 622     if (ret != 0) {    
  1244.   
  1245. 623         dev_err(dev, "s3c_ts.c: Could not allocate ts IRQ_ADC !\n");    
  1246.   
  1247. 624         ret = -EIO;    
  1248.   
  1249. 625         goto err_irq;    
  1250.   
  1251. 626     }    
  1252.   
  1253. 627      
  1254.   
  1255. 628     printk(KERN_INFO "%s got loaded successfully : %d bits\n", DEVICE_NAME, s3c_ts_cfg->resol_bit);    
  1256.   
  1257. 629      
  1258.   
  1259. 630     ret = misc_register(&misc);  //注册这混杂字符设备    
  1260.   
  1261. 631     if (ret) {    
  1262.   
  1263. 632         dev_err(dev, "s3c_ts.c: Could not register device(mini6410 touchscreen)!\n");    
  1264.   
  1265. 633         ret = -EIO;    
  1266.   
  1267. 634         goto fail;    
  1268.   
  1269. 635     }    
  1270.   
  1271. 636      
  1272.   
  1273. 637     return 0;    
  1274.   
  1275. 638      
  1276.   
  1277. 639 fail:    
  1278.   
  1279. 640     free_irq(ts_irq->start, ts->dev);    
  1280.   
  1281. 641     free_irq(ts_irq->end, ts->dev);    
  1282.   
  1283. 642      
  1284.   
  1285. 643 err_irq:    
  1286.   
  1287. 644     kfree(ts);    
  1288.   
  1289. 645      
  1290.   
  1291. 646     clk_disable(ts_clock);    
  1292.   
  1293. 647     clk_put(ts_clock);    
  1294.   
  1295. 648      
  1296.   
  1297. 649 err_clk:    
  1298.   
  1299. 650     iounmap(ts_base);    
  1300.   
  1301. 651      
  1302.   
  1303. 652 err_map:    
  1304.   
  1305. 653     release_resource(ts_mem);    
  1306.   
  1307. 654     kfree(ts_mem);    
  1308.   
  1309. 655      
  1310.   
  1311. 656 err_req:    
  1312.   
  1313. 657     return ret;    
  1314.   
  1315. 658 }    
  1316.   
  1317. 659      
  1318.   
  1319. 660 static int s3c_ts_remove(struct platform_device *dev)    
  1320.   
  1321. 661 {    
  1322.   
  1323. 662     printk(KERN_INFO "s3c_ts_remove() of TS called !\n");    
  1324.   
  1325. 663      
  1326.   
  1327. 664     disable_irq(IRQ_ADC);    
  1328.   
  1329. 665     disable_irq(IRQ_PENDN);    
  1330.   
  1331. 666      
  1332.   
  1333. 667     free_irq(IRQ_PENDN, ts->dev);    
  1334.   
  1335. 668     free_irq(IRQ_ADC, ts->dev);    
  1336.   
  1337. 669      
  1338.   
  1339. 670     if (ts_clock) {    
  1340.   
  1341. 671         clk_disable(ts_clock);    
  1342.   
  1343. 672         clk_put(ts_clock);    
  1344.   
  1345. 673         ts_clock = NULL;    
  1346.   
  1347. 674     }    
  1348.   
  1349. 675      
  1350.   
  1351. 676     misc_deregister(&misc);    
  1352.   
  1353. 677     iounmap(ts_base);    
  1354.   
  1355. 678      
  1356.   
  1357. 679     return 0;    
  1358.   
  1359. 680 }    
  1360.   
  1361. 681      
  1362.   
  1363. 682 #ifdef CONFIG_PM    
  1364.   
  1365. 683 static unsigned int adccon, adctsc, adcdly;    
  1366.   
  1367. 684      
  1368.   
  1369. 685 static int s3c_ts_suspend(struct platform_device *dev, pm_message_t state)    
  1370.   
  1371. 686 {    
  1372.   
  1373. 687     adccon = readl(ts_base + S3C_ADCCON);    
  1374.   
  1375. 688     adctsc = readl(ts_base + S3C_ADCTSC);    
  1376.   
  1377. 689     adcdly = readl(ts_base + S3C_ADCDLY);    
  1378.   
  1379. 690      
  1380.   
  1381. 691     disable_irq(IRQ_ADC);    
  1382.   
  1383. 692     disable_irq(IRQ_PENDN);    
  1384.   
  1385. 693      
  1386.   
  1387. 694     clk_disable(ts_clock);    
  1388.   
  1389. 695      
  1390.   
  1391. 696     return 0;    
  1392.   
  1393. 697 }    
  1394.   
  1395. 698      
  1396.   
  1397. 699 static int s3c_ts_resume(struct platform_device *pdev)    
  1398.   
  1399. 700 {    
  1400.   
  1401. 701     clk_enable(ts_clock);    
  1402.   
  1403. 702      
  1404.   
  1405. 703     writel(adccon, ts_base + S3C_ADCCON);    
  1406.   
  1407. 704     writel(adctsc, ts_base + S3C_ADCTSC);    
  1408.   
  1409. 705     writel(adcdly, ts_base + S3C_ADCDLY);    
  1410.   
  1411. 706     writel(WAIT4INT(0), ts_base + S3C_ADCTSC);    
  1412.   
  1413. 707      
  1414.   
  1415. 708     enable_irq(IRQ_ADC);    
  1416.   
  1417. 709     enable_irq(IRQ_PENDN);    
  1418.   
  1419. 710     return 0;    
  1420.   
  1421. 711 }    
  1422.   
  1423. 712 #else    
  1424.   
  1425. 713 #define s3c_ts_suspend  NULL    
  1426.   
  1427. 714 #define s3c_ts_resume   NULL    
  1428.   
  1429. 715 #endif    
  1430.   
  1431. 716      
  1432.   
  1433. 717 static struct platform_driver s3c_ts_driver = {    
  1434.   
  1435. 718     .probe          = s3c_ts_probe,    
  1436.   
  1437. 719     .remove         = s3c_ts_remove,    
  1438.   
  1439. 720     .suspend        = s3c_ts_suspend,    
  1440.   
  1441. 721     .resume         = s3c_ts_resume,    
  1442.   
  1443. 722     .driver         = {    
  1444.   
  1445. 723         .owner          = THIS_MODULE,    
  1446.   
  1447. 724         .name           = "s3c-ts",    
  1448.   
  1449. 725     },    
  1450.   
  1451. 726 };    
  1452.   
  1453. 727      
  1454.   
  1455. 728 static char banner[] __initdata = KERN_INFO "S3C Touchscreen driver, (c) 2010 FriendlyARM,\n";    
  1456.   
  1457. 729      
  1458.   
  1459. 730 static int __init s3c_ts_init(void)    
  1460.   
  1461. 731 {    
  1462.   
  1463. 732     printk(banner);    
  1464.   
  1465. 733     return platform_driver_register(&s3c_ts_driver);    
  1466.   
  1467. 734 }    
  1468.   
  1469. 735      
  1470.   
  1471. 736 static void __exit s3c_ts_exit(void)    
  1472.   
  1473. 737 {    
  1474.   
  1475. 738     platform_driver_unregister(&s3c_ts_driver);    
  1476.   
  1477. 739 }    
  1478.   
  1479. 740      
  1480.   
  1481. 741 module_init(s3c_ts_init);    
  1482.   
  1483. 742 module_exit(s3c_ts_exit);    
  1484.   
  1485. 743      
  1486.   
  1487. 744 MODULE_AUTHOR("FriendlyARM Inc.");    
  1488.   
  1489. 745 MODULE_LICENSE("GPL");    
  1490.   
  1491. 746      
  1492.   
  1493. 747      
  1494.   
  1495. 748 /*   
  1496.  
  1497. 749  * 驱动分析   
  1498.  
  1499. 750  * 1、内核是如何加载驱动的?   
  1500.  
  1501. 751  *    首先要提到两个结构体:设备用Platform_device表示,驱动用Platform_driver进行注册   
  1502.  
  1503. 752  *    Platform机制开发发底层驱动的大致流程为:  定义 platform_device  注册 platform_device 定义 platform_driver 注册 platform_driver   
  1504.  
  1505. 753  *    首先要确认的就是设备的资源信息platform_device,例如设备的地址,中断号等 该结构体定义在kernel\include\linux\platform_device.h   
  1506.  
  1507. 754  *    该结构一个重要的元素是resource,该元素存入了最为重要的设备资源信息,定义在kernel\include\linux\ioport.h中   
  1508.  
  1509. 755  *    下面我们以本例来进行说明:   
  1510.  
  1511. 756  *       arch/arm/mach-s3c64xx中dev-ts-mini6410.c中定义了platform_device s3c_device_ts   
  1512.  
  1513. 757  *       定义好了platform_device结构体后就可以调用函数platform_add_devices向系统中添加该设备了,之后可以调用platform_driver_register()进行设备注册。   
  1514.  
  1515. 758  *       要注意的是,这里的platform_device设备的注册过程必须在相应设备驱动加载之前被调用,即执行platform_driver_register之前,原因是因为驱动注册时需要   
  1516.  
  1517. 759  *       匹配内核中所以已注册的设备名。   
  1518.  
  1519. 760  *       platform_devicerr的注册是在arch/arm/mach-s3c64xx中mach-mini6410.c中的mini6410_machine_init函数实现的。   
  1520.  
  1521. 761  *       mini6410_machine_init是在启动后调用,它是在module_init之前;更具体的见MACHINE_START   
  1522.  
  1523. 762  *  MACHINE_START(MINI6410, "MINI6410")   
  1524.  
  1525. 763  *          
  1526.  
  1527. 764  *        .boot_params  = S3C64XX_PA_SDRAM + 0x100,  //.boot_params是bootloader向内核传递的参数的位置,这要和bootloader中参数的定义要一致。   
  1528.  
  1529. 765  *   
  1530.  
  1531. 766  *        .init_irq = s3c6410_init_irq,  //.init_irq在start_kernel() --> init_IRQ() --> init_arch_irq()中被调用   
  1532.  
  1533. 767  *        .map_io       = mini6410_map_io,   //.map_io 在 setup_arch() --> paging_init() --> devicemaps_init()中被调用   
  1534.  
  1535. 768  *        .init_machine = mini6410_machine_init,  //init_machine 在 arch/arm/kernel/setup.c 中被 customize_machine 调用,   
  1536.  
  1537. 769  *                          //放在 arch_initcall() 段里面,会自动按顺序被调用。   
  1538.  
  1539. 770  *        .timer        = &s3c24xx_timer,  //.timer是定义系统时钟,定义TIMER4为系统时钟,在arch/arm/plat-s3c/time.c中体现。   
  1540.  
  1541. 771  *                      //在start_kernel() --> time_init()中被调用。   
  1542.  
  1543. 772  *       MACHINE_END   
  1544.  
  1545. 773  *       再来看看platform_driver,这个定义在本文中,   
  1546.  
  1547. 774  *       在驱动初始化函数中调用函数platform_driver_register()注册platform_driver,需要注意的是s3c_device_ts结构中name元素和s3c_ts_driver结构中driver.name   
  1548.  
  1549. 775  *       必须是相同的,这样在platform_driver_register()注册时会对所有已注册的所有platform_device中的name和当前注册的platform_driver的driver.name进行比较,   
  1550.  
  1551. 776  *       只有找到相同的名称的platfomr_device才能注册成功,当注册成功时会调用platform_driver结构元素probe函数指针,这里就是s3c_ts_probe   
  1552.  
  1553. 777  *       参考资料:http://blogold.chinaunix.net/u2/60011/showart.php?id=1018502   
  1554.  
  1555. 778  *    
  1556.  
  1557. 779  * 2、timer在这里的作用   
  1558.  
  1559. 780  *     timer是用来防抖的,我们知道,触摸屏处理分为两个时间段,一个是由按下中断触发的四次AD转换的时间A,一个是4次AD转换完成后将AD数据存入FIFO的时间B,在时间A,没有打开抬起中断,   
  1560.  
  1561. 781  *     也就是说如果在这段时间有抬起事件,也不会触发中断,不会影响AD的转换。在时间B,打开抬起中断,打开定时器延时触发touch_timer_fire,如果在延时这段时间,有抬起事件发生   
  1562.  
  1563. 782  *      则touch_timer_fire不会将前面的数据存入到FIFO中,否则写入FIFO,表示值有效。   
  1564.  
  1565. 783  *    
  1566.  
  1567. 784  *    
  1568.  
  1569. 785  */   
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值