input: touchscreen: add touch screen of gslx680 for rk3399-firefly-edp
[firefly-linux-kernel-4.4.55.git] / drivers / input / touchscreen / nas_ts.c
1 /****************************************************************************************
2  * driver/input/touchscreen/hannstar_nas.c
3  *Copyright     :ROCKCHIP  Inc
4  *Author        :       sfm
5  *Date          :  2010.2.5
6  *This driver use for rk28 chip extern touchscreen. Use i2c IF ,the chip is Hannstar
7  *description??
8  ********************************************************************************************/
9
10 #include <linux/module.h>
11 #include <linux/delay.h>
12 #include <linux/earlysuspend.h>
13 #include <linux/hrtimer.h>
14 #include <linux/i2c.h>
15 #include <linux/input.h>
16 #include <linux/interrupt.h>
17 #include <linux/io.h>
18 #include <linux/platform_device.h>
19 #include <linux/async.h>
20 #include <mach/gpio.h>
21 #include <linux/irq.h>
22 #include <mach/board.h>
23 #include <linux/proc_fs.h>
24 #include <asm/uaccess.h>
25
26 #include <linux/earlysuspend.h>
27 #define MAX_SUPPORT_POINT       2// //  4
28 #define PACKGE_BUFLEN           10
29
30 #define CHECK_STATUS    1
31 //#define       TP_ERROR_RESTART_POWER  1
32
33 //#define Singltouch_Mode
34 //#define       NAS_TP_DEBUG    0
35 #define SAKURA_DBG                  0
36 #if SAKURA_DBG 
37 #define sakura_dbg_msg(fmt,...)       do {                                      \
38                                    printk("sakura dbg msg------>"                       \
39                                           " (func-->%s ; line-->%d) " fmt, __func__, __LINE__ , ##__VA_ARGS__); \
40                                   } while(0)
41 #define sakura_dbg_report_key_msg(fmt,...)      do{                                                     \
42                                                     printk("sakura report " fmt,##__VA_ARGS__);          \
43                                                 }while(0)
44 #else
45 #define sakura_dbg_msg(fmt,...)       do {} while(0)
46 #define sakura_dbg_report_key_msg(fmt,...)      do{}while(0)
47 #endif
48
49 #define SWAP_Y_POS
50 #define TOUCH_REPORT_X_MAX        1280//(1255)  //(1280 - 1)//(0xfff)
51 #define TOUCH_REPORT_Y_MAX        768//(700)    //(768 - 1)//(0xfff)
52
53 #define RECORD_PREVIOUS_VALUES
54 #define FILTER_SAME_POINTS
55
56 #define NASTECH_ZERO_TOUCH        0     
57 #define NASTECH_ONE_TOUCH         1
58 #define NASTECH_TWO_TOUCH         2
59 #define NASTECH_PENUP_RECHECK     3
60 #define NASTECH_DeltaX            100
61 #define NASTECH_DeltaY            80
62 #define NASTECH_R_Threshold       4000
63
64 /*delay used to check the touch panel current status.*/
65 #define TS_RELEASE_CHECK_DELAY          (100)
66 #define TS_CUR_STATUS_PUSH                      1
67 #define TS_CUR_STATUS_RELEASE           0
68 #define TS_POLL_DELAY_DOWN        (2)  /* ms delay before the first sample */
69 #define TS_POLL_DELAY_UP          (10)  /* ms delay after pen up, to double check the pen up event */
70 #define TS_POLL_PERIOD            (30)  /* ms delay between samples */
71 #define PENUP_DETECT_MAX          (4)   /* How many times we should detect for a penup event. */
72 #define INT_TEST_TIMES            40
73
74 struct touch_point {
75         int count;
76         int curX;
77         int curY;
78         int unCalX;
79         int unCalY;
80         int prevX;
81         int prevY;
82 #ifdef RECORD_PREVIOUS_VALUES
83         int sample_time;
84         int last_finger;
85         int x[2];
86         int y[2];
87 #endif
88 };
89
90 struct point_data {     
91         short status;   
92         short x;        
93         short y;
94     short z;
95 };
96
97 struct multitouch_event{
98         struct point_data point_data[MAX_SUPPORT_POINT];
99         int contactid;
100     int validtouch;
101 };
102
103 struct ts_nas {
104         struct input_dev        *input;
105         char                    phys[32];
106         struct delayed_work     work;
107         struct workqueue_struct *wq;
108
109         struct i2c_client       *client;
110     struct multitouch_event mt_event;
111         u16                     model;
112
113         bool            pendown;
114         bool            status;
115         int                     irq;
116         int             has_relative_report;
117 //add from nas
118
119         bool                sleepstatus;
120         bool                pendown_sent;          /* Whether we've sent out a pendown event */
121         bool                pendown_ignore;        /* Sample two points and send out only one point */
122         int                 penup_recheck;         /* Pen up re-check times, when it reaches PENUP_DETECTE_MAX, pen up is reported. */
123         int                 release_check_time; /* Debounse time for pen down, ms */
124         int                 pendown_debounce_time; /* Debounse time for pen down, ms */
125         int                 penup_debounce_time;   /* Debounce time for pen up, ms */
126         int                 repeat_time;           /* Time between samples, ms */
127         struct touch_point  point;
128         struct timer_list       status_check_timer;
129         int                 reported_finger_count;
130         int                             touch_cur_status;
131
132         int                     (*get_pendown_state)(void);
133         void            (*clear_penirq)(void);
134 #ifdef  CHECK_STATUS    
135         struct delayed_work     work1;
136         struct workqueue_struct *wq1;
137 #endif  
138         
139 };
140
141 //mg gamma
142 unsigned short y_gamma[]={
143  0 ,   0 ,   1 ,   1 ,       
144  2 ,   3 ,   3 ,   4 ,   5 ,   6 ,   6 ,   7 ,       
145  8 ,   9 ,   10 ,   10 ,   11 ,   12 ,   13 ,   14 ,       
146  15 ,   15 ,   16 ,   17 ,   18 ,   19 ,   20 ,   21 ,       
147  21 ,   22 ,   23 ,   24 ,   25 ,   26 ,   27 ,   28 ,       
148  28 ,   29 ,   30 ,   31 ,   32 ,   33 ,   34 ,   35 ,       
149  36 ,   37 ,   38 ,   38 ,   39 ,   40 ,   41 ,   42 ,       
150  43 ,   44 ,   45 ,   46 ,   47 ,   48 ,   49 ,   50 ,       
151  51 ,   52 ,   52 ,   53 ,   54 ,   55 ,   56 ,   57 ,       
152  58 ,   59 ,   60 ,   61 ,   62 ,   63 ,   64 ,   65 ,       
153  66 ,   67 ,   68 ,   69 ,   70 ,   71 ,   72 ,   73 ,       
154  74 ,   75 ,   76 ,   77 ,   78 ,   79 ,   80 ,   81 ,       
155  82 ,   83 ,   84 ,   85 ,   86 ,   87 ,   88 ,   89 ,       
156  90 ,   91 ,   92 ,   93 ,   94 ,   95 ,   96 ,   97 ,       
157  98 ,   99 ,   100 ,   101 ,   102 ,   103 ,   104 ,   105 ,       
158  106 ,   107 ,   108 ,   109 ,   110 ,   111 ,   112 ,   113 ,       
159  114 ,   115 ,   116 ,   117 ,   118 ,   119 ,   120 ,   121 ,       
160  122 ,   123 ,   124 ,   125 ,   126 ,   127 ,   128 ,   129 ,       
161  130 ,   132 ,   133 ,   134 ,   135 ,   136 ,   137 ,   138 ,       
162  139 ,   140 ,   141 ,   142 ,   143 ,   144 ,   145 ,   146 ,       
163  147 ,   148 ,   149 ,   150 ,   151 ,   152 ,   154 ,   155 ,       
164  156 ,   157 ,   158 ,   159 ,   160 ,   161 ,   162 ,   163 ,       
165  164 ,   165 ,   166 ,   167 ,   168 ,   169 ,   171 ,   172 ,       
166  173 ,   174 ,   175 ,   176 ,   177 ,   178 ,   179 ,   180 ,       
167  181 ,   182 ,   183 ,   184 ,   186 ,   187 ,   188 ,   189 ,       
168  190 ,   191 ,   192 ,   193 ,   194 ,   195 ,   196 ,   197 ,       
169  199 ,   200 ,   201 ,   202 ,   203 ,   204 ,   205 ,   206 ,       
170  207 ,   208 ,   209 ,   210 ,   212 ,   213 ,   214 ,   215 ,       
171  216 ,   217 ,   218 ,   219 ,   220 ,   221 ,   222 ,   224 ,       
172  225 ,   226 ,   227 ,   228 ,   229 ,   230 ,   231 ,   232 ,       
173  233 ,   235 ,   236 ,   237 };
174
175 unsigned short y_gamma900[]={
176 0 ,   0 ,   1 ,   2 ,       
177  3 ,   4 ,   4 ,   5 ,   6 ,   7 ,   8 ,   9 ,       
178  10 ,   11 ,   12 ,   12 ,   13 ,   14 ,   15 ,   16 ,       
179  17 ,   18 ,   19 ,   20 ,   21 ,   22 ,   23 ,   23 ,       
180  24 ,   25 ,   26 ,   27 ,   28 ,   29 ,   30 ,   31 ,       
181  32 ,   33 ,   34 ,   35 ,   36 ,   37 ,   38 ,   39 ,       
182  40 ,   41 ,   42 ,   42 ,   43 ,   44 ,   45 ,   46 ,       
183  47 ,   48 ,   49 ,   50 ,   51 ,   52 ,   53 ,   54 ,       
184  55 ,   56 ,   57 ,   58 ,   59 ,   60 ,   61 ,   62 ,       
185  63 ,   64 ,   65 ,   66 ,   67 ,   68 ,   69 ,   70 ,       
186  71 ,   72 ,   73 ,   74 ,   75 ,   76 ,   77 ,   78 ,       
187  79 ,   80 ,   81 ,   82 ,   83 ,   84 ,   85 ,   86 ,       
188  87 ,   88 ,   89 ,   90 ,   91 ,   92 ,   93 ,   94 ,       
189  95 ,   96 ,   97 ,   98 ,   99 ,   100 ,   101 ,   102 ,       
190  103 ,   104 ,   105 ,   106 ,   107 ,   108 ,   109 ,   110 ,       
191  111 ,   112 ,   113 ,   114 ,   115 ,   116 ,   117 ,   118 ,       
192  119 ,   120 ,   121 ,   122 ,   123 ,   124 ,   125 ,   126 ,       
193  127 ,   128 ,   129 ,   130 ,   131 ,   132 ,   133 ,   134 ,       
194  135 ,   136 ,   137 ,   138 ,   139 ,   140 ,   141 ,   142 ,       
195  143 ,   144 ,   145 ,   146 ,   147 ,   148 ,   149 ,   150 ,       
196  152 ,   153 ,   154 ,   155 ,   156 ,   157 ,   158 ,   159 ,       
197  160 ,   161 ,   162 ,   163 ,   164 ,   165 ,   166 ,   167 ,       
198  168 ,   169 ,   170 ,   171 ,   172 ,   173 ,   174 ,   175 ,       
199  176 ,   177 ,   178 ,   179 ,   180 ,   181 ,   182 ,   183 ,       
200  185 ,   186 ,   187 ,   188 ,   189 ,   190 ,   191 ,   192 ,       
201  193 ,   194 ,   195 ,   196 ,   197 ,   198 ,   199 ,   200 ,       
202  201 ,   202 ,   203 ,   204 ,   205 ,   206 ,   207 ,   208 ,       
203  209 ,   210 ,   212 ,   213 ,   214 ,   215 ,   216 ,   217 ,       
204  218 ,   219 ,   220 ,   221 ,   222 ,   223 ,   224 ,   225 ,       
205  226 ,   227 ,   228 ,   229 ,   230 ,   231 ,   232 ,   233 ,       
206  235 ,   236 ,   237 ,   238  };  
207
208
209 unsigned short y_gamma800[]={
210   0 ,   0 ,   0 ,   0 ,   1 ,   1 ,       
211  2 ,   2 ,   3 ,   3 ,   4 ,   5 ,   5 ,   6 ,       
212  6 ,   7 ,   8 ,   8 ,   9 ,   9 ,   10 ,   11 ,       
213  11 ,   12 ,   13 ,   13 ,   14 ,   15 ,   16 ,   16 ,       
214  17 ,   18 ,   19 ,   19 ,   20 ,   21 ,   22 ,   22 ,       
215  23 ,   24 ,   25 ,   25 ,   26 ,   27 ,   28 ,   29 ,       
216  29 ,   30 ,   31 ,   32 ,   33 ,   34 ,   34 ,   35 ,       
217  36 ,   37 ,   38 ,   39 ,   40 ,   40 ,   41 ,   42 ,       
218  43 ,   44 ,   45 ,   46 ,   47 ,   47 ,   48 ,   49 ,       
219  50 ,   51 ,   52 ,   53 ,   54 ,   55 ,   56 ,   57 ,       
220  58 ,   58 ,   59 ,   60 ,   61 ,   62 ,   63 ,   64 ,       
221  65 ,   66 ,   67 ,   68 ,   69 ,   70 ,   71 ,   72 ,       
222  73 ,   74 ,   75 ,   76 ,   77 ,   78 ,   79 ,   80 ,       
223  81 ,   82 ,   83 ,   84 ,   85 ,   86 ,   87 ,   88 ,       
224  89 ,   90 ,   91 ,   92 ,   93 ,   94 ,   95 ,   96 ,       
225  97 ,   98 ,   99 ,   100 ,   101 ,   102 ,   103 ,   104 ,       
226  105 ,   106 ,   107 ,   108 ,   109 ,   110 ,   111 ,   113 ,       
227  114 ,   115 ,   116 ,   117 ,   118 ,   119 ,   120 ,   121 ,       
228  122 ,   123 ,   124 ,   125 ,   127 ,   128 ,   129 ,   130 ,       
229  131 ,   132 ,   133 ,   134 ,   135 ,   136 ,   137 ,   139 ,       
230  140 ,   141 ,   142 ,   143 ,   144 ,   145 ,   146 ,   147 ,       
231  149 ,   150 ,   151 ,   152 ,   153 ,   154 ,   155 ,   157 ,       
232  158 ,   159 ,   160 ,   161 ,   162 ,   163 ,   164 ,   166 ,       
233  167 ,   168 ,   169 ,   170 ,   171 ,   173 ,   174 ,   175 ,       
234  176 ,   177 ,   178 ,   180 ,   181 ,   182 ,   183 ,   184 ,       
235  185 ,   187 ,   188 ,   189 ,   190 ,   191 ,   192 ,   194 ,       
236  195 ,   196 ,   197 ,   198 ,   200 ,   201 ,   202 ,   203 ,       
237  204 ,   206 ,   207 ,   208 ,   209 ,   210 ,   212 ,   213 ,       
238  214 ,   215 ,   216 ,   218 ,   219 ,   220 ,   221 ,   222 ,       
239  224 ,   225 ,   226 ,   227 ,   229 ,   230 ,   231 ,   232 ,       
240  233 ,   235 ,   236 ,   237 ,   238 ,   240 ,   241 ,   242 ,       
241  243 ,   245 ,   246 ,   247 ,   248 ,   250 ,   251 ,   252 ,       
242  253 ,   255 ,   256 ,   257 ,   258 ,   260 ,   261 ,   262  
243 };
244
245 static struct proc_dir_entry *nas_tp_debug_ctl_entry;
246 static char nas_tp_debug_flag = 0;
247
248 extern void nas_reset(void);
249 static void nas_early_resume(struct early_suspend *h)
250 {
251         //nas_reset();
252 }
253 static struct early_suspend nastech_early_suspend =
254 {
255         .resume = nas_early_resume
256 };
257 #ifdef  CHECK_STATUS
258 static void nas_status_check_timer(unsigned long data)
259 {
260         unsigned char buf[4]={0};
261         struct ts_nas *ts_dev = (struct ts_nas *)data;
262         int ret=0;
263         //printk("nas_status_check_timer ...\n");
264                 queue_delayed_work(ts_dev->wq1, &ts_dev->work1, 0);
265         ts_dev->status_check_timer.expires  = jiffies + msecs_to_jiffies(1000);
266         add_timer(&ts_dev->status_check_timer);
267 }
268
269 #define FB_DISPLAY_ON_PIN RK29_PIN6_PD0
270 extern void rk29_lcd_reset(void);
271 static void nas_status_check_work(struct work_struct *work1)
272 {
273         unsigned char buf[4]={0};
274         struct ts_nas *ts_dev =
275                 container_of(to_delayed_work(work1), struct ts_nas, work1);
276         int ret=0;
277
278         if (GPIO_LOW == gpio_get_value(FB_DISPLAY_ON_PIN))
279                 return;
280
281         //printk("nas_status_check_work ...");
282         ret = i2c_master_reg8_recv( ts_dev->client, 0xFC, buf, 1, 200*1000);
283         //printk(" ret = %d\n", ret);
284         if( ret <0)
285         {
286                 printk("nas status error, ret=%d \nnas_reset()\n", ret);
287 #ifdef  TP_ERROR_RESTART_POWER          
288                 rk29_lcd_reset();
289                 msleep(100);
290 #endif          
291                 nas_reset();
292         }
293 }       
294 #endif
295 /** Get point from touch pannel */
296 static int nastech_ts_get_point(struct ts_nas *ts_dev, unsigned short *pX, unsigned short *pY)
297 {
298         unsigned short xpos, ypos;
299         unsigned short xpos0, ypos0;
300         unsigned char event;
301         unsigned char Finger, hwFinger;
302         unsigned char buf[26];
303         //unsigned char buf[13]; // for 2 finger touch, only used the first 13 bytes
304         int ret;
305         int nTouch = 0;
306         struct i2c_client *client = ts_dev->client;
307         struct input_dev *input = ts_dev->input;
308
309         memset(buf, 0xff, sizeof(buf));
310         //ret = nastech_read_regs(client, NASTECH_READ_POS, buf, sizeof(buf));
311         ret = i2c_master_reg8_recv( ts_dev->client, 0xF9, buf, 26, 200*1000);
312
313         hwFinger=buf[3];
314
315         //-------------------------------------------------------------------------------------------
316         //xpos=(unsigned short)(buf[7]*0x100);
317         xpos=(unsigned short)((buf[7]&0x0f)<<8 );
318         xpos=xpos|buf[8];
319         //ypos=(unsigned short)(buf[5]*0x100);
320         ypos=(unsigned short)((buf[5]&0x0f)<<8 );
321         ypos=ypos|buf[6];
322
323         //xpos0=(unsigned short)(buf[11]*0x100);
324         xpos0=(unsigned short)((buf[11]&0x0f)<<8 );
325         xpos0=xpos0|buf[12];
326         //ypos0=(unsigned short)(buf[9]*0x100);
327         ypos0=(unsigned short)((buf[9]&0x0f)<<8 );
328         ypos0=ypos0|buf[10];
329         //-------------------------------------------------------------------------------------------
330         
331 //#ifdef NAS_TP_DEBUG
332         if (nas_tp_debug_flag == 1)
333                 printk(KERN_INFO "read from TP: (%d,%d), (%d,%d)\n", xpos, ypos, xpos0, ypos0);
334 //#endif
335 unsigned short   ypos_pre,ratio;
336
337
338
339         *pX = xpos;
340         *pY = ypos;
341
342 #ifdef TOUCH_REPORT_ONLY_ONE_POINT
343         if(xpos==0x0FFF || ypos==0x0FFF)
344         {
345                 Finger = NASTECH_ZERO_TOUCH;
346         }
347         else
348         {
349                 Finger = NASTECH_ONE_TOUCH;
350         }
351 #else
352
353         if(xpos==0x0FFF || ypos==0x0FFF)
354         {
355                 Finger = NASTECH_ZERO_TOUCH;
356         }
357         else
358         {
359                 Finger = NASTECH_ONE_TOUCH;
360         }
361         
362         if(xpos0!=0x0FFF && ypos0!=0x0FFF)
363         {
364                 Finger = NASTECH_TWO_TOUCH;
365         }
366
367 #ifdef SWAP_Y_POS
368 ypos = TOUCH_REPORT_Y_MAX - ypos;
369         if(ypos < 0)
370                 ypos = 0;
371                 
372          if (ypos<262)
373 {
374    ratio=ypos;
375  
376                 ypos= y_gamma800[ratio];
377             
378 }
379  if (ypos>506)
380 {
381     
382    ratio= 767-ypos ;
383          ypos=767-y_gamma800[ratio];
384                
385             
386 }
387
388
389         if (xpos<262)
390 {
391    ratio=xpos;
392  
393                 xpos= y_gamma800[ratio];
394             
395 }
396 if (xpos>1017)
397 {
398    ratio=1279-xpos;
399  
400                 xpos= 1279-y_gamma800[ratio];
401             
402 }
403
404
405         //ypos=report_y_correction(y_pre); //mg for coorection  
406         
407         ypos0 = TOUCH_REPORT_Y_MAX - ypos0;
408         if(ypos0 < 0)
409                 ypos0 = 0;
410                 
411          if (ypos0<262)
412 {
413    ratio=ypos0;
414  
415                 ypos0= y_gamma800[ratio];
416             
417 }
418  if (ypos0>506)
419 {
420     
421    ratio= 767-ypos0 ;
422          ypos0=767-y_gamma800[ratio];
423                
424             
425 }
426
427
428         if (xpos0<262)
429 {
430    ratio=xpos0;
431  
432                 xpos0= y_gamma800[ratio];
433             
434 }
435 if (xpos>1017)
436 {
437    ratio=1279-xpos0;
438  
439                 xpos0= 1279-y_gamma800[ratio];
440             
441 }        
442 #endif
443 //#ifdef NAS_TP_DEBUG
444         if (nas_tp_debug_flag == 1) {
445                 if (NASTECH_TWO_TOUCH == Finger) {
446                         printk(KERN_INFO "original: (%d,%d), (%d,%d)\n", xpos, ypos, xpos0, ypos0);
447                 } else if (NASTECH_ONE_TOUCH == Finger) {
448                         printk(KERN_INFO "original: (%d,%d)\n", xpos, ypos);
449                 }
450         }
451 //#endif
452         //printk("nastech_ts_get_point: Finger = %d!\n",Finger);
453 #ifdef RECORD_PREVIOUS_VALUES
454         if (ts_dev->point.last_finger == Finger)
455         {
456                 switch(Finger)
457                 {
458                 case NASTECH_ZERO_TOUCH:
459                         break;
460                 case NASTECH_ONE_TOUCH:
461                         if (ts_dev->point.x[0] == xpos && ts_dev->point.y[0] == ypos)
462                                 return Finger;
463                         break;
464                 case NASTECH_TWO_TOUCH:
465                         if ((ts_dev->point.x[0] == xpos && ts_dev->point.y[0] == ypos)
466                                 && (ts_dev->point.x[1] == xpos0 && ts_dev->point.y[1] == ypos0))
467                                 return Finger;
468                         break;
469                 }
470                 ts_dev->point.x[0] = xpos;
471                 ts_dev->point.y[0] = ypos;
472                 ts_dev->point.x[1] = xpos0;
473                 ts_dev->point.y[1] = ypos0;
474         }
475         else
476         {
477                 ts_dev->point.last_finger = Finger;
478                 ts_dev->point.x[0] = xpos;
479                 ts_dev->point.y[0] = ypos;
480                 ts_dev->point.x[1] = xpos0;
481                 ts_dev->point.y[1] = ypos0;
482         }
483 #endif
484         {
485                 int z,w;
486                 if(!Finger)
487                 {
488                         z=0;
489                         w=0;
490                 }
491                 else
492                 {
493                         z=255;
494                         w=15;
495                 }
496
497                 ts_dev->touch_cur_status = TS_CUR_STATUS_PUSH;
498                 
499                 if(Finger>1)
500                 {
501                         input_report_abs(input, ABS_MT_TOUCH_MAJOR, z);
502                         input_report_abs(input, ABS_MT_WIDTH_MAJOR, w);
503                         input_report_abs(input, ABS_MT_POSITION_X, xpos);
504                         input_report_abs(input, ABS_MT_POSITION_Y, ypos);
505                         input_report_key(ts_dev->input, BTN_TOUCH, 1);
506                         input_mt_sync(input);
507                         
508                         input_report_abs(input, ABS_MT_TOUCH_MAJOR, z);
509                         input_report_abs(input, ABS_MT_WIDTH_MAJOR, w);
510                         input_report_abs(input, ABS_MT_POSITION_X, xpos0);
511                         input_report_abs(input, ABS_MT_POSITION_Y, ypos0);
512                         input_report_key(ts_dev->input, BTN_2, 1);
513                         input_mt_sync(input);
514                 }
515                 else
516                 {
517                         if (1 == Finger)
518                         {
519                                 input_report_abs(input, ABS_MT_POSITION_X, xpos);
520                                 input_report_abs(input, ABS_MT_POSITION_Y, ypos);
521                                 input_report_abs(input, ABS_MT_TOUCH_MAJOR, z);
522                                 input_report_abs(input, ABS_MT_WIDTH_MAJOR, w);
523                                 input_report_key(ts_dev->input, BTN_TOUCH, 1);
524                                 input_mt_sync(input);
525                                 //printk("pen press (x,y):(%d,%d)\n",xpos,ypos);
526                         }
527                         else
528                         {
529                         //      printk("nastech_ts_get_point: released......!\n");
530                                 input_report_abs(input, ABS_MT_POSITION_X, xpos);
531                                 input_report_abs(input, ABS_MT_POSITION_Y, ypos);
532                                 input_report_abs(input, ABS_MT_TOUCH_MAJOR, z);
533                                 input_report_abs(input, ABS_MT_WIDTH_MAJOR, w);
534                                 input_report_key(ts_dev->input, BTN_TOUCH, 0);
535                                 input_mt_sync(input);
536                                 ts_dev->touch_cur_status = TS_CUR_STATUS_RELEASE;
537                         }
538                         
539                         if(ts_dev->reported_finger_count > 1)
540                         {
541                                 input_report_abs(input, ABS_MT_POSITION_X, xpos);
542                                 input_report_abs(input, ABS_MT_POSITION_Y, ypos);
543                                 input_report_abs(input, ABS_MT_TOUCH_MAJOR, 0);
544                                 input_report_abs(input, ABS_MT_WIDTH_MAJOR, 0);
545                                 input_report_key(ts_dev->input, BTN_2, 0);
546                                 input_mt_sync(input);
547                         }
548                 }
549                 ts_dev->reported_finger_count = Finger;
550         }
551         input_sync(input);
552 #endif
553         return (int)Finger;
554 }
555
556 static int nastech_sample_new_point(struct ts_nas *ts_dev)
557 {
558         unsigned short  x, y;
559         unsigned int nTouch = 0;
560         
561         struct i2c_client *client = ts_dev->client;
562         struct touch_point *point = &ts_dev->point;
563         
564         nTouch = nastech_ts_get_point(ts_dev, &x, &y);
565         point->unCalX = x;
566         point->unCalY = y;
567
568         if (unlikely(NASTECH_ZERO_TOUCH == nTouch)) {
569                 //printk(KERN_INFO "<======Pen released at (%d, %d)\n", point->prevX, point->prevY);
570                 point->curX = point->curY = 0;
571                 point->count = 0;
572         } else {
573                 point->curX = x;
574                 point->curY = y;
575 #ifdef TOUCHPANEL_SWAP_XY
576                 {
577                         int temp = point->curX;
578                         point->curX = point->curY;
579                         point->curY = temp;
580                 }
581 #endif
582                 if(nTouch == NASTECH_ONE_TOUCH) {
583                         if( !ts_dev->pendown_sent || (abs(point->curX-point->prevX) < NASTECH_DeltaX)
584                                 && (abs(point->curY - point->prevY) < NASTECH_DeltaY) ){ // is it a movement ? 
585                                 // update pX, pY and send new point
586                                 point->prevX = point->curX;
587                                 point->prevY = point->curY;
588                         } else {
589                                 printk(KERN_INFO "use the old touch: prev=(%d,%d), cur=(%d,%d)\n",
590                                                 point->prevX, point->prevY, point->curX, point->curY);
591                                 // use old one
592                                 point->curX = point->prevX;
593                                 point->curY = point->prevY;
594                         }
595                 }
596         }
597         return nTouch;
598 }
599 int nas_get_pendown_state(void)
600 {
601         return 0;
602 }
603
604 static void nas_report_event(struct ts_nas *ts,struct multitouch_event *tc)
605 {
606         struct input_dev *input = ts->input;
607     int i,pandown = 0;
608         dev_dbg(&ts->client->dev, "UP\n");
609                 
610     for(i=0; i<MAX_SUPPORT_POINT;i++){                  
611         if(tc->point_data[i].status >= 0){
612             pandown |= tc->point_data[i].status;
613             input_report_abs(input, ABS_MT_TRACKING_ID, i);                                                     
614             input_report_abs(input, ABS_MT_TOUCH_MAJOR, tc->point_data[i].status);                              
615             input_report_abs(input, ABS_MT_WIDTH_MAJOR, 0);     
616             input_report_abs(input, ABS_MT_POSITION_X, tc->point_data[i].x);                            
617             input_report_abs(input, ABS_MT_POSITION_Y, tc->point_data[i].y);                            
618             input_mt_sync(input);       
619
620             sakura_dbg_report_key_msg("ABS_MT_TRACKING_ID = %x, ABS_MT_TOUCH_MAJOR = %x\n ABS_MT_POSITION_X = %x, ABS_MT_POSITION_Y = %x\n",i,tc->point_data[i].status,tc->point_data[i].x,tc->point_data[i].y);
621 #if defined(CONFIG_HANNSTAR_DEBUG)
622                         printk("hannstar nas Px = [%d],Py = [%d] \n",tc->point_data[i].x,tc->point_data[i].y);
623 #endif
624
625             if(tc->point_data[i].status == 0)                                   
626                 tc->point_data[i].status--;                     
627         }
628         
629     }   
630
631     ts->pendown = pandown;
632     input_sync(input);
633 }
634
635
636 static inline int nas_check_firmwork(struct ts_nas *ts)
637 {
638     int data;
639     int len = 10;
640     char buf[10] = {0x03 , 0x03 , 0x0a , 0x01 , 'D' , 0x00 , 0x00 , 0x00 , 0x00 , 0x00};
641         int i;
642     short contactid=0;
643
644     data = i2c_master_normal_send(ts->client, buf,len, 200*1000);
645
646         if(data < 0){
647                 dev_err(&ts->client->dev, "i2c io error %d \n", data);
648                 return data;
649         }
650
651         data = i2c_master_normal_recv(ts->client, buf,len, 200*1000);
652
653         if(data < 0){
654                 dev_err(&ts->client->dev, "i2c io error %d \n", data);
655                 return data;
656         }
657
658         printk("nas reg[5] = %c ,reg[6] = %c, reg[7] = %c, reg[8] = %c\n" , buf[5],buf[6],buf[7],buf[8]);
659         printk("nas reg[5] = %x ,reg[6] = %x, reg[7] = %x, reg[8] = %x\n" , buf[5],buf[6],buf[7],buf[8]);
660     return data;
661 }
662
663
664 static inline int nas_read_values(struct ts_nas *ts, struct multitouch_event *tc)
665 {
666     int data, j;
667     int len = 10;
668         char buff[26];
669         int ret;
670         int i;
671         unsigned short xpos, ypos, xpos0, ypos0;
672         memset(buff, 0xff, sizeof(buff));
673         ret = i2c_master_reg8_recv( ts->client, 0xF9, buff, 26, 200*1000);
674 /*
675         printk("ret = %d, buff[]=\n", ret);
676         for(i=0; i<26; i++)
677         {
678                 if(i %8 == 0)
679                         printk("\n");
680                 printk("0x%02x ", buff[i]);
681         }
682 */
683         //-------------------------------------------------------------------------------------------
684         //xpos=(unsigned short)(buf[7]*0x100);
685         xpos=(unsigned short)((buff[7]&0x0f)<<8 );
686         xpos=xpos|buff[8];
687         //ypos=(unsigned short)(buf[5]*0x100);
688         ypos=(unsigned short)((buff[5]&0x0f)<<8 );
689         ypos=ypos|buff[6];
690
691         //xpos0=(unsigned short)(buf[11]*0x100);
692         xpos0=(unsigned short)((buff[11]&0x0f)<<8 );
693         xpos0=xpos0|buff[12];
694         //ypos0=(unsigned short)(buf[9]*0x100);
695         ypos0=(unsigned short)((buff[9]&0x0f)<<8 );
696         ypos0=ypos0|buff[10];
697         //-------------------------------------------------------------------------------------------
698         
699 //      printk("read from TP: (%d,%d), (%d,%d)\n", xpos, ypos, xpos0, ypos0);
700     return 10;
701 }
702
703
704 static void nas_work(struct work_struct *work)
705 {
706         struct ts_nas *ts =
707                 container_of(to_delayed_work(work), struct ts_nas, work);
708         struct multitouch_event *tc = &ts->mt_event;
709         int rt;
710 #if     0    
711         rt = nas_read_values(ts,tc);
712     
713     if(rt < 0)
714         goto out;
715         
716 #if defined (Singltouch_Mode)
717     nas_report_single_event(ts,tc);
718 #else
719     nas_report_event(ts,tc);
720 #endif
721 #endif
722
723 //      if (ts->sleepstatus == 1)
724 //              return;
725         
726         switch(nastech_sample_new_point(ts)) {
727         case NASTECH_ZERO_TOUCH:
728                 break;
729         case NASTECH_ONE_TOUCH:
730         case NASTECH_TWO_TOUCH:
731 #if     0
732                 {
733                         /* periodly sample the next points */
734                         del_timer(&ts->repeat_timer);
735                         ts->repeat_timer.expires = jiffies + msecs_to_jiffies(ts->repeat_time);
736                         add_timer(&ts->repeat_timer);
737                 }
738 #endif
739                 break;  
740         default:
741                 break;
742         }
743 out:               
744 #if     1
745                 enable_irq(ts->irq);
746 #else
747         if (ts->pendown){
748                 queue_delayed_work(ts->wq, &ts->work, msecs_to_jiffies(10));
749                 ts->pendown = 0;
750         }
751         else{
752                 enable_irq(ts->irq);
753         }
754 #endif
755 }
756
757 static irqreturn_t nas_irq(int irq, void *handle)
758 {
759         struct ts_nas *ts = handle;
760         //printk("enter nas_irq()\n");
761 #if 1
762         if (!ts->get_pendown_state || likely(ts->get_pendown_state())) {
763                 //printk("disable_irq_nosync()\n");
764                 disable_irq_nosync(ts->irq);
765                 queue_delayed_work(ts->wq, &ts->work, 0);
766         }
767
768 #endif
769         return IRQ_HANDLED;
770 }
771
772 static void nas_free_irq(struct ts_nas *ts)
773 {
774         free_irq(ts->irq, ts);
775         if (cancel_delayed_work_sync(&ts->work)) {
776                 /*
777                  * Work was pending, therefore we need to enable
778                  * IRQ here to balance the disable_irq() done in the
779                  * interrupt handler.
780                  */
781                 enable_irq(ts->irq);
782         }
783 }
784
785
786 static ssize_t nas_tp_debug_ctl(struct file *file, const char __user *buffer, size_t count, loff_t *data)
787 {
788         char val;
789
790         if (copy_from_user(&val, buffer, 1))
791                 return -EFAULT;
792
793         if (val != '0' && val != '1')
794                 return -EINVAL;
795
796         nas_tp_debug_flag = val - '0';
797
798         return count;
799 }
800
801 static int __devinit nas_probe(struct i2c_client *client,
802                                    const struct i2c_device_id *id)
803 {
804         struct ts_nas *ts;
805         struct nas_platform_data *pdata = pdata = client->dev.platform_data;
806         struct input_dev *input_dev;
807         int err;
808         
809         if (!pdata) {
810                 dev_err(&client->dev, "platform data is required!\n");
811                 return -EINVAL;
812         }
813
814         if (!i2c_check_functionality(client->adapter, I2C_FUNC_I2C))
815                 return -EIO;
816
817         ts = kzalloc(sizeof(struct ts_nas), GFP_KERNEL);
818         input_dev = input_allocate_device();
819         if (!ts || !input_dev) {
820                 err = -ENOMEM;
821                 goto err_free_mem;
822         }
823
824         ts->client = client;
825         ts->irq = client->irq;
826         ts->input = input_dev;
827         ts->status =0 ;// fjp add by 2010-9-30
828         ts->pendown = 0; // fjp add by 2010-10-06
829
830         ts->wq = create_rt_workqueue("nas_wq");
831         INIT_DELAYED_WORK(&ts->work, nas_work);
832         
833 #ifdef  CHECK_STATUS
834         ts->wq1 = create_rt_workqueue("nas_wq1");
835         INIT_DELAYED_WORK(&ts->work1, nas_status_check_work);
836 #endif  
837         ts->model             = pdata->model;
838
839         snprintf(ts->phys, sizeof(ts->phys),
840                  "%s/input0", dev_name(&client->dev));
841
842         input_dev->name = "nas Touchscreen";
843         input_dev->phys = ts->phys;
844         input_dev->id.bustype = BUS_I2C;
845
846         ts->has_relative_report = 0;
847         input_dev->evbit[0] = BIT_MASK(EV_ABS)|BIT_MASK(EV_KEY)|BIT_MASK(EV_SYN);
848         input_dev->keybit[BIT_WORD(BTN_TOUCH)] = BIT_MASK(BTN_TOUCH);
849         input_dev->keybit[BIT_WORD(BTN_2)] = BIT_MASK(BTN_2); //jaocbchen for dual
850 #if     0
851         input_set_abs_params(input_dev, ABS_X, 0, CONFIG_HANNSTAR_MAX_X, 0, 0);
852         input_set_abs_params(input_dev, ABS_Y, 0, CONFIG_HANNSTAR_MAX_Y, 0, 0);
853         input_set_abs_params(input_dev, ABS_PRESSURE, 0, 255, 0, 0);
854         input_set_abs_params(input_dev, ABS_TOOL_WIDTH, 0, 15, 0, 0);
855         input_set_abs_params(input_dev, ABS_HAT0X, 0, CONFIG_HANNSTAR_MAX_X, 0, 0);
856         input_set_abs_params(input_dev, ABS_HAT0Y, 0, CONFIG_HANNSTAR_MAX_Y, 0, 0);
857         input_set_abs_params(input_dev, ABS_MT_POSITION_X,0, CONFIG_HANNSTAR_MAX_X, 0, 0);
858         input_set_abs_params(input_dev, ABS_MT_POSITION_Y, 0, CONFIG_HANNSTAR_MAX_Y, 0, 0);
859         input_set_abs_params(input_dev, ABS_MT_TOUCH_MAJOR, 0, 255, 0, 0);
860         input_set_abs_params(input_dev, ABS_MT_WIDTH_MAJOR, 0, 255, 0, 0);
861         input_set_abs_params(input_dev, ABS_MT_TRACKING_ID, 0, 10, 0, 0);   
862 #else
863         input_set_abs_params(input_dev, ABS_X, 0, TOUCH_REPORT_X_MAX, 0, 0);
864         input_set_abs_params(input_dev, ABS_Y, 0, TOUCH_REPORT_Y_MAX, 0, 0);
865         input_set_abs_params(input_dev, ABS_PRESSURE, 0, 1, 0, 0);
866         input_set_abs_params(input_dev, ABS_HAT0X, 0, TOUCH_REPORT_X_MAX, 0, 0);
867         input_set_abs_params(input_dev, ABS_HAT0Y, 0, TOUCH_REPORT_Y_MAX, 0, 0);
868         input_set_abs_params(input_dev, ABS_MT_POSITION_X, 0, TOUCH_REPORT_X_MAX, 0, 0);
869         input_set_abs_params(input_dev, ABS_MT_POSITION_Y, 0, TOUCH_REPORT_Y_MAX, 0, 0);
870         input_set_abs_params(input_dev, ABS_MT_TOUCH_MAJOR, 0, 255, 0, 0);
871         input_set_abs_params(input_dev, ABS_MT_WIDTH_MAJOR, 0, 15, 0, 0);
872 #endif
873         if (pdata->init_platform_hw)
874                 pdata->init_platform_hw();
875
876         if (!ts->irq) {
877                 dev_dbg(&ts->client->dev, "no IRQ?\n");
878                 return -ENODEV;
879         }else{
880                 ts->irq = gpio_to_irq(ts->irq);
881         }
882 //miaozh modify
883         err = request_irq(ts->irq, nas_irq, GPIOEdgelFalling,
884                         client->dev.driver->name, ts);
885 //      err = request_irq(ts->irq, nas_irq, 0,
886 //                      client->dev.driver->name, ts);
887         
888         if (err < 0) {
889                 dev_err(&client->dev, "irq %d busy?\n", ts->irq);
890                 goto err_free_mem;
891         }
892         
893         if (err < 0)
894                 goto err_free_irq;
895 #if 0
896         err = set_irq_type(ts->irq,IRQ_TYPE_LEVEL_LOW);
897         if (err < 0) {
898                 dev_err(&client->dev, "irq %d busy?\n", ts->irq);
899                 goto err_free_mem;
900         }
901         if (err < 0)
902                 goto err_free_irq;
903 #endif
904         err = input_register_device(input_dev);
905         if (err)
906                 goto err_free_irq;
907
908         i2c_set_clientdata(client, ts);
909
910         nas_check_firmwork(ts);
911         
912         nas_tp_debug_ctl_entry = create_proc_entry("nas_tp_debug_ctl", 0644, NULL);
913         if (nas_tp_debug_ctl_entry) {
914                 nas_tp_debug_ctl_entry->read_proc = NULL;
915                 nas_tp_debug_ctl_entry->write_proc = nas_tp_debug_ctl;
916         }
917 #ifdef CONFIG_HAS_EARLYSUSPEND
918         register_early_suspend(&nastech_early_suspend);
919 #endif
920
921 #ifdef  CHECK_STATUS
922         setup_timer(&ts->status_check_timer, nas_status_check_timer, (unsigned long)ts);
923         ts->status_check_timer.expires  = jiffies + msecs_to_jiffies(1000);
924         add_timer(&ts->status_check_timer);
925 #endif  
926         return 0;
927
928  err_free_irq:
929         nas_free_irq(ts);
930         if (pdata->exit_platform_hw)
931                 pdata->exit_platform_hw();
932  err_free_mem:
933         input_free_device(input_dev);
934         kfree(ts);
935         return err;
936 }
937
938 static int __devexit nas_remove(struct i2c_client *client)
939 {
940         struct ts_nas *ts = i2c_get_clientdata(client);
941         struct nas_platform_data *pdata = client->dev.platform_data;
942
943         nas_free_irq(ts);
944
945         if (pdata->exit_platform_hw)
946                 pdata->exit_platform_hw();
947
948         input_unregister_device(ts->input);
949         kfree(ts);
950         
951         if (nas_tp_debug_ctl_entry)
952                 remove_proc_entry("nas_tp_debug_ctl", NULL);
953
954         return 0;
955 }
956
957 static struct i2c_device_id nas_idtable[] = {
958         { "nas_touch", 0 },
959         { }
960 };
961
962 MODULE_DEVICE_TABLE(i2c, nas_idtable);
963
964 static struct i2c_driver nas_driver = {
965         .driver = {
966                 .owner  = THIS_MODULE,
967                 .name   = "nas_touch"
968         },
969         .id_table       = nas_idtable,
970         .probe          = nas_probe,
971         .remove         = __devexit_p(nas_remove),
972 };
973
974 static void __init nas_init_async(void *unused, async_cookie_t cookie)
975 {
976         printk("--------> %s <-------------\n",__func__);
977         i2c_add_driver(&nas_driver);
978 }
979
980 static int __init nas_init(void)
981 {
982         async_schedule(nas_init_async, NULL);
983         return 0;
984 }
985
986 static void __exit nas_exit(void)
987 {
988         return i2c_del_driver(&nas_driver);
989 }
990 module_init(nas_init);
991 module_exit(nas_exit);
992 MODULE_LICENSE("GPL");
993