(xxm) FIH:update touch screen atmel_mxt224.c
[firefly-linux-kernel-4.4.55.git] / drivers / input / touchscreen / atmel_mxt224.c
1 /****************************************************************************************
2  * driver/input/touchscreen/atmel_mxt224.c
3  *Copyright     :ROCKCHIP  Inc
4  *Author        :       dqz
5  *Date          :  2011.2.28
6  *
7  *description£º
8  ********************************************************************************************/
9 #include <linux/module.h>
10 #include <linux/delay.h>
11 #include <linux/earlysuspend.h>
12 #include <linux/hrtimer.h>
13 #include <linux/i2c.h>
14 #include <linux/input.h>
15 #include <linux/interrupt.h>
16 #include <linux/io.h>
17 #include <linux/platform_device.h>
18 #include <linux/async.h>
19 #include <mach/gpio.h>
20 #include <linux/irq.h>
21 #include <mach/board.h>
22
23 #define FEATURE_CFG_DUMP
24
25 static int mxt224_i2c_write(struct i2c_client *client, u16 offset,void *buf,int size);
26 static int mxt224_i2c_read(struct i2c_client *client, u16 offset,void *buf,int size);
27 static int mXT224_probe(struct i2c_client *client, const struct i2c_device_id *id);
28 static int mXT224_remove(struct i2c_client *client);
29
30 #ifdef FEATURE_CFG_DUMP
31 static int total_size = 0;
32 static u8 *cfg_dmup;
33 #endif
34
35 #define local_debug //printk
36
37 #define ID_INFORMATION_SIZE        0x7
38 #define OBJECT_TABLE_ELEMENT_SIZE  0x6
39 #define CRC_SIZE                   0x3
40 #define TABLE_SIZE_ADDR            ID_INFORMATION_SIZE-1
41 #define MESSAGE_T5                 0x5
42
43 #define MXT224_REPORTID_T9_OFFSET 9
44 #define MXT224_MT_SCAN_POINTS 2
45
46 struct mxt224_id_info{
47     u8   family_id;
48     u8   variant_id;
49     u8   version;
50     u8   build;
51     u8   matrix_x_size;
52     u8   matrix_y_size;
53     u8   object_num;
54 }__packed;
55
56 struct mxt224_table_info{
57     u8   obj_type;
58     u8   start_addr_lsb;
59     u8   start_addr_msb;
60     u8   size;
61     u8   instance;
62     u8   report_id;
63 }__packed;
64
65 #define CFGERR_MASK (0x01<<3)
66
67 union msg_body{
68     u8 msg[7];
69     struct{
70         u8  status;
71         u32 checksum:24;
72     }t6;
73     struct{
74         u8  status;
75         u8  x_msb;
76         u8  y_msb;
77         u8  xy_poslisb;
78         u8  area;
79         u8  tchamplitude;
80         u8  tchvector;
81     }t9;
82 }__packed;
83
84  
85 struct message_t5{
86     u8 report_id;
87     union msg_body body;
88     u8 checksum;
89 }__packed;
90
91 struct mxt224_obj{
92     struct mxt224_id_info    *id_info;
93     u8                       *table_info_byte;
94     struct mxt224_table_info *table_info;
95     u8                        table_size;
96     u16                       msg_t5_addr;
97     u32                       info_crc;
98 };
99
100 struct mXT224_info {
101     int int_gpio;
102     int reset_gpio;
103     int cfg_delay;
104         int last_key_index;
105     u16 last_read_addr;
106
107         char phys[32];
108     //struct hrtimer timer;
109     struct i2c_client *client;
110     struct input_dev *input_dev;
111         struct delayed_work     work;
112         struct workqueue_struct *mxt224_wq;
113
114     int (*power)(int on);
115     /* Object table relation */
116     struct mxt224_obj obj;
117 };
118
119 static struct mXT224_info ts_data = {
120     .int_gpio = RK29_PIN0_PA2,
121     .reset_gpio = RK29_PIN6_PC3,
122     .cfg_delay = 0,
123     .last_key_index = 0,
124     .last_read_addr = 0,
125 };
126
127 struct report_id_table{
128     u8 report_start;
129     u8 report_end;
130     u8 obj_type;
131 };
132
133 static struct report_id_table* id_table = NULL;
134 static u8 T9_cfg[31] = {0};
135 struct mxt224_cfg{
136     u8 type;
137     const u8* data;
138     int size;
139 };
140
141
142 struct mxt224_key_info{
143     u32  start;
144     u32  end;
145     u32  code;
146 };
147 const struct mxt224_key_info key_info[] = {
148         {0, 0, KEY_BACK},
149     {0, 0, KEY_MENU},
150     {0, 0, KEY_HOME},
151     {0, 0, KEY_SEARCH},
152 };
153
154 #if 1
155 const u8 T7[] =  {0xff, 0xff, 0x32};
156 const u8 T8[] =  {0x08, 0x05, 0x14, 0x14, 0x00, 0x00, 0x0a, 0x0f};
157 const u8 T9[] =  {0x83, 0x00, 0x00, 0x0D, 0x0A, 0x00, 0x11, 0x28,
158                   0x02, 0x01, 0x00, 0x01, 0x01, 0x00, 0x0A, 0x0A,
159                   0x0A, 0x0A, 0x01, 0x5A, 0x00, 0xEF, 0x00, 0x00,
160                   0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
161                   ///////////////////////////////////////
162                   //0x83, 0x0D, 0x0A, 0x03, 0x03, 0x00, 0x11, 0x28,
163                   //0x02, 0x03, 0x00, 0x01, 0x01, 0x00, 0x0A, 0x0A,
164                   //0x0A, 0x0A, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
165                   //0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 
166                   };
167 const u8 T15[] = {0x00, 0x00, 0x00, 0x01, 0x01, 0x00, 0x41, 0x1E,
168                   0x02, 0x00, 0x00 };
169 const u8 T18[] = {0x00, 0x00 };
170 const u8 T19[] = {0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
171                   0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00};
172 const u8 T20[] = {0x00, 0x64, 0x64, 0x64, 0x64, 0x00, 0x00, 0x00,
173                   0x00, 0x00, 0x00, 0x00 };
174 const u8 T22[] = {0x15, 0x00, 0x00, 0x00, 0x19, 0xFF, 0xE7, 0x04,
175                   0x32, 0x00, 0x01, 0x0A, 0x0F, 0x14, 0x19, 0x1E,
176                   0x04,
177                   /////////////////////////////////////
178                   //0x15, 0x00, 0x00, 0x00, 0x19, 0xff, 0xe7, 0x04,
179                   //0x32, 0x00, 0x01, 0x0a, 0x0f, 0x14, 0x19, 0x1e,
180                   //0x04,
181                   };
182 const u8 T23[] = {0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
183                   0x00, 0x00, 0x00, 0x00, 0x00 };
184 const u8 T24[] = {0x03, 0x04, 0x03, 0xFF, 0x00, 0x64, 0x64, 0x01,
185                   0x0A, 0x14, 0x28, 0x00, 0x4B, 0x00, 0x02, 0x00,
186                   0x64, 0x00, 0x19 };
187 const u8 T25[] = {0x00, 0x00, 0x2E, 0xE0, 0x1B, 0x58, 0x36, 0xB0,
188                   0x01, 0xF4, 0x00, 0x00, 0x00, 0x00 };
189 const u8 T27[] = {0x03, 0x02, 0x00, 0xE0, 0x03, 0x00, 0x23};
190 const u8 T28[] = {0x00, 0x00, 0x00, 0x04, 0x08, 0xF6 };
191 const u8 T38[] = {0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00};
192
193 const struct mxt224_cfg cfg_table[] = {
194     {7,  T7,  sizeof(T7)},
195     {8,  T8,  sizeof(T8)},
196     {9,  T9,  sizeof(T9)},
197     {15, T15, sizeof(T15)},
198     {18, T18, sizeof(T18)},
199     {19, T19, sizeof(T19)},
200     {20, T20, sizeof(T20)},
201     {22, T22, sizeof(T22)},
202     {23, T23, sizeof(T23)},
203     {24, T24, sizeof(T24)},
204     {25, T25, sizeof(T25)},
205     {27, T27, sizeof(T27)},
206     {28, T28, sizeof(T28)},
207     {38, T38, sizeof(T38)}
208 };
209 #endif
210
211
212 enum mXT224_type{
213     MSG_T9_MT_1 = 1,
214     MSG_T9_MT_2,
215     MSG_T9_MT_3,
216     MSG_T9_MT_4,
217     MSG_T9_MT_5,
218     MSG_T9_MT_6,
219     MSG_T9_MT_7,
220     MSG_T9_MT_8,
221     MSG_T9_MT_9,
222     MSG_T9_KEY_PRESS,
223
224     MSG_T6,
225 };
226
227 enum mXT224_touch_status{
228     STATUS_RELEASE = 0,
229     STATUS_PRESS,
230 };
231
232
233 u32 static crc24(u32 crc, u8 firstbyte, u8 secondbyte)
234 {
235     const u32 crcpoly = 0x80001b;
236     u32 result;
237     u16 data_word;
238
239     data_word = (u16)((u16)(secondbyte<<8u)|firstbyte);
240     result = ((crc<<1u)^(u32)data_word);
241     if(result & 0x1000000){
242         result ^= crcpoly;
243     }
244     return result;
245 }
246
247 u32 static get_crc24(const u8* src, int cnt)
248 {
249     int index = 0;
250     u32 crc = 0;
251
252     while(index < (cnt-1)){
253         crc = crc24(crc, *(src+index), *(src+index+1));
254         index += 2;
255     }
256     //1 TODO:
257     if(index != cnt){
258         crc = crc24(crc, *(src+index), 0);
259     }
260     crc = (crc & 0x00ffffff);
261     return crc;
262 }
263
264 static u32 mXT224_cfg_crc(void)
265 {
266     int index;
267     int sub_index = 0;
268     u32 crc = 0;
269
270     /* Remove T38 */
271     for(index=0; index<((sizeof(cfg_table)/sizeof(cfg_table[0]))-1); index++){
272         const u8* data = cfg_table[index].data;
273         while(sub_index <(cfg_table[index].size-1)){
274             crc = crc24(crc, *(data+sub_index), *(data+sub_index+1));
275             sub_index += 2;
276         }
277         if(sub_index != cfg_table[index].size){
278             if(index == ((sizeof(cfg_table)/sizeof(cfg_table[0]))-1)){
279                 crc = crc24(crc, *(data+sub_index), 0);
280             }else{
281                 const u8* next_data = cfg_table[index+1].data;
282                 crc = crc24(crc, *(data+sub_index), *(next_data));
283                 crc = (crc & 0x00ffffff);
284                 sub_index = 1;
285                 continue;
286             }            
287         }
288         sub_index = 0;
289         crc = (crc & 0x00ffffff);
290     }
291     //1 TODO:
292     //crc = crc24(crc, 0, 0);
293     crc = (crc & 0x00ffffff);
294     return crc;
295 }
296
297 static void mxt224_mem_dbg(const void *src, int cnt)
298 {
299 #if 0
300     int index;
301     const u8* disp = (u8*)src;
302     local_debug(KERN_INFO "%s: start...\n", __func__);
303     for(index=0; index < cnt; index++){
304         local_debug(KERN_INFO "0x%2x  ", disp[index]);
305     }
306     local_debug(KERN_INFO "\n%s: ...end\n", __func__);
307 #endif
308 }
309
310
311
312 static irqreturn_t mXT224_ts_interrupt(int irq, void *handle)
313 {
314         struct mXT224_info *ts = handle;
315         local_debug(KERN_INFO "%s\n", __func__);
316
317         disable_irq_nosync(ts->int_gpio);
318         queue_delayed_work(ts->mxt224_wq, &ts->work, 0);
319
320         return IRQ_HANDLED;
321 }
322
323
324 static u16 mXT224_get_obj_addr(u8 type, struct mxt224_table_info *info, u8 info_size)
325 {
326     int index;
327     u16 addr = 0;
328
329     for(index=0; index < info_size; index++){
330                 if(type == info[index].obj_type){
331             addr = info[index].start_addr_msb;
332             addr <<= 8;
333             addr |= info[index].start_addr_lsb;
334             return addr;
335         }
336     }
337     return addr;
338 }
339
340
341 static struct report_id_table* mXT224_build_report_id_table(
342         struct mxt224_table_info *info, u8 info_size)
343 {
344     int index;
345     int write_index;
346     u8  offset = 0;
347     id_table = (struct report_id_table*)kzalloc(info_size*sizeof(struct report_id_table), GFP_KERNEL);
348     
349     if(!id_table){
350         local_debug(KERN_INFO "%s: Can't get memory!\n", __func__);
351         return NULL;
352     }
353     
354     write_index = 0;
355     
356     for(index = 0; index < info_size; index++){     
357 #ifdef FEATURE_CFG_DUMP
358         total_size += ((info[index].size+1)*(info[index].instance+1));
359 #endif
360         if(info[index].obj_type == 0x5)
361             continue;
362         if(info[index].report_id == 0x00)
363             continue;
364         
365         id_table[write_index].obj_type = info[index].obj_type;
366         id_table[write_index].report_start = (offset+1);
367         id_table[write_index].report_end = id_table[write_index].report_start+
368                                                 info[index].report_id*(info[index].instance+1)-1;
369         
370         offset = id_table[write_index].report_end;
371         write_index++;
372     }
373     
374 #ifdef FEATURE_CFG_DUMP
375     for(index = 0; index < info_size; index++){
376                 local_debug(KERN_INFO "%s: Object type:%d, size:[%d]\n", __func__, 
377                             info[index].obj_type, info[index].size+1);
378     }
379 #endif
380     
381     return id_table;
382 }
383
384
385 static u8 mXT224_get_obj_type(u8 id, struct report_id_table* table, u8 table_size)
386 {
387     int index;
388
389     for(index=0; index < table_size; index++){    
390                 local_debug(KERN_INFO "%s: ID:%d, start:[%d], end:[%d], type:[%d]\n", __func__, 
391                          id, table[index].report_start, table[index].report_end, 
392                 table[index].obj_type);
393         if(id>=table[index].report_start && id<=table[index].report_end){
394             break;
395         }
396     }
397     
398     switch(table[index].obj_type){
399         case 6:
400             return MSG_T6;
401             
402         case 9:
403         {
404             int t9_offset = id-table[index].report_start;
405             if(t9_offset < MXT224_REPORTID_T9_OFFSET){
406                 return MSG_T9_MT_1+t9_offset;
407             }else{
408                 return 0;
409             }
410         }   
411         case 15:
412                 {
413                         return MSG_T9_KEY_PRESS;
414                 }
415         default:
416             return 0;
417     }
418 }
419
420
421 #define TS_POLL_PERIOD 10000*1000
422
423  
424
425 static void mXT224_load_cfg(void)
426 {
427     int index;
428     u8  buf[6] = {0};
429     u16 addr;
430     int rc;
431     
432     local_debug(KERN_INFO "%s\n", __func__);
433
434     if(ts_data.cfg_delay){
435         return;
436     }
437     ts_data.cfg_delay = 1;
438     
439    // hrtimer_start(&ts_data.timer, ktime_set(0, TS_POLL_PERIOD), HRTIMER_MODE_REL);
440     
441     for(index=0; index<(sizeof(cfg_table)/sizeof(cfg_table[0])); index++){
442
443         const u8* data = cfg_table[index].data;
444         u16 addr = mXT224_get_obj_addr(cfg_table[index].type, ts_data.obj.table_info,
445                                                               ts_data.obj.table_size);
446         rc = mxt224_i2c_write(ts_data.client, addr, data, cfg_table[index].size);
447         if(rc){
448             local_debug(KERN_INFO "%s: Load mXT224 config failed, addr: 0x%x!\n", __func__, addr);
449         }
450     }
451     
452     addr = mXT224_get_obj_addr(6, ts_data.obj.table_info, ts_data.obj.table_size);
453     
454     //buf[0] = 0x05;
455     buf[1] = 0x55;
456     
457     rc = mxt224_i2c_write(ts_data.client, addr, buf, 6);
458     
459     if(rc){
460         local_debug(KERN_INFO "%s: Back up NV failed!\n", __func__);
461     }
462     
463     /* Reset mXT224 */
464     msleep(5);
465     
466     gpio_set_value(ts_data.reset_gpio, 0);
467     msleep(1);
468     
469     gpio_set_value(ts_data.reset_gpio, 1);
470     msleep(50);
471     
472 }
473
474
475
476 #define DETECT_MASK     (0x01<<7)
477 #define RELEASE_MASK    (0x01<<5)
478 #define MOVE_MASK       (0x01<<4)
479
480
481 static u32 cfg_crc;
482
483
484 static int mXT224_process_msg(u8 id, u8 *msg)
485 {
486
487     switch(id){
488         case MSG_T6:
489         {
490                         local_debug(KERN_INFO "%s: Process mXT224 msg MSG_T6!\n", __func__);
491             u32 checksum = ((union msg_body*)msg)->t6.checksum;
492             
493             u8  status = ((union msg_body*)msg)->t6.status;
494             
495             if(status & CFGERR_MASK){
496                 local_debug(KERN_INFO "%s: Process mXT224 cfg error!\n", __func__);
497                // mXT224_load_cfg();
498             }
499             /*
500             if(checksum!=cfg_crc){
501                 local_debug(KERN_INFO "%s: Process mXT224 cfg CRC error!\n", __func__);
502                 local_debug(KERN_INFO "%s: Read CRC:[0x%x], Our CRC:[0x%x]\n", __func__, checksum, cfg_crc);
503                 mXT224_load_cfg();
504             }
505             */
506             break;
507         }
508         
509         case MSG_T9_MT_1:
510           
511         case MSG_T9_MT_2:
512           
513         case MSG_T9_MT_3:
514                 case MSG_T9_MT_4:
515                 case MSG_T9_MT_5:
516         {
517                         local_debug(KERN_INFO "%s: Process mXT224 msg MSG_T9_MT!\n", __func__);
518             u32 x, y;
519             int tcStatus = 0;
520             x = ((union msg_body*)msg)->t9.x_msb;
521             x <<= 4;
522             x |= (((union msg_body*)msg)->t9.xy_poslisb>>4);
523
524             y = ((union msg_body*)msg)->t9.y_msb;
525             y <<= 4;
526             ((union msg_body*)msg)->t9.xy_poslisb &= 0x0f;
527             
528             y |= ((union msg_body*)msg)->t9.xy_poslisb;
529             
530             local_debug(KERN_INFO "%s: X[%d], Y[%d]\n", __func__, x, y);
531
532                         if(((union msg_body*)msg)->t9.status & DETECT_MASK)
533                         {
534                                 tcStatus = STATUS_PRESS;
535                         }else if(((union msg_body*)msg)->t9.status & RELEASE_MASK)
536                         {
537                                 tcStatus = STATUS_RELEASE;
538                         }
539                         
540             input_report_abs(ts_data.input_dev, ABS_MT_TRACKING_ID, id - 1);                                                    
541             input_report_abs(ts_data.input_dev, ABS_MT_TOUCH_MAJOR, tcStatus);                          
542             input_report_abs(ts_data.input_dev, ABS_MT_WIDTH_MAJOR, 0); 
543             input_report_abs(ts_data.input_dev, ABS_MT_POSITION_X, x);                          
544             input_report_abs(ts_data.input_dev, ABS_MT_POSITION_Y, y);                          
545             input_mt_sync(ts_data.input_dev);   
546                         local_debug(KERN_INFO "%s,input_report_abs x is %d,y is %d. status is [%d].\n", __func__, x, y, tcStatus);
547             break;
548         }       
549         case MSG_T9_KEY_PRESS:
550         {
551             int keyStatus, keyIndex;
552                         
553             keyStatus = ((union msg_body*)msg)->t9.status >> 7;
554                         if(keyStatus) //press.
555             {
556                 keyIndex = ((union msg_body*)msg)->t9.x_msb;
557                                 ts_data.last_key_index = keyIndex;
558                         }else{                                                  
559                             keyIndex = ts_data.last_key_index;
560                         }
561                     switch(keyIndex){
562                         case 1:
563                             {
564                                                 keyIndex = 0;
565                                                 break;
566                             }
567                                 case 2:
568                                         {
569                                                 keyIndex = 1;
570                                                 break;
571                                     }
572                                 case 4:
573                                         {
574                                                 keyIndex = 2;
575                                                 break;
576                                         }
577                                 case 8:
578                                         {
579                                                 keyIndex = 3;
580                                                 break;
581                                         }
582                     default:
583                 local_debug(KERN_INFO "%s: Default keyIndex [0x%x]\n", __func__, keyIndex);        
584             break;
585                     }
586
587             local_debug(KERN_INFO "%s: Touch KEY code is [%d], keyStatus is [%d]\n", __func__, key_info[keyIndex].code, keyStatus);
588             
589             input_report_key(ts_data.input_dev, key_info[keyIndex].code, keyStatus);
590             
591             break;
592         }
593
594
595         default:
596             local_debug(KERN_INFO "%s: Default id[0x%x]\n", __func__, id);
597             
598             break;
599     }
600     
601     return 0;
602 }
603
604  
605
606 static void mXT224_work_func(struct work_struct *work)
607 {
608     u8 track[MXT224_MT_SCAN_POINTS];
609     int index, ret, read_points;
610     struct message_t5 msg_t5_array[MXT224_MT_SCAN_POINTS];
611         
612     local_debug(KERN_INFO "%s\n", __func__);
613     
614     
615     ret = mxt224_i2c_read(ts_data.client, ts_data.obj.msg_t5_addr, 
616                         (u8*)&msg_t5_array[0], sizeof(struct message_t5) * MXT224_MT_SCAN_POINTS);
617         read_points = ret / sizeof(struct message_t5);
618
619         local_debug(KERN_INFO "%s, this time read_points is %d\n", __func__, read_points);
620     mxt224_mem_dbg((u8*)&msg_t5_array[0], sizeof(struct message_t5) * read_points);
621
622         for(index = 0; index < read_points; index++)
623         {
624                 if(msg_t5_array[index].report_id == 0xFF) // dirty message, don't process.
625                 {
626                         track[index] = 0xFF;
627                         continue;
628                 }
629                 track[index] = mXT224_get_obj_type(msg_t5_array[index].report_id, id_table, ts_data.obj.table_size);
630
631                 if(track[index] == 0){
632                         local_debug(KERN_INFO "%s: Get object type failed!, report id[0x%x]\n", __func__, msg_t5_array[index].report_id);
633                         goto end;
634                 }
635                 
636                 local_debug(KERN_INFO "%s,object's msg type is %d.\n", __func__, track[index]);
637         }
638
639         for(index = 0; index < read_points; index++)
640         {
641                 if(track[index] == 0xFF)
642                         continue;
643             mXT224_process_msg(track[index], (u8*)&msg_t5_array[index].body);
644                 if(track[index] == track[read_points - 1] || track[read_points - 1] == 0xFF)
645                 {
646                     input_sync(ts_data.input_dev);
647                         local_debug(KERN_INFO "%s,input_sync ts_data.input_dev.\n", __func__);
648                 }
649         }
650         
651 end:
652         enable_irq(ts_data.int_gpio);
653 }
654
655
656 static int mxt224_i2c_write(struct i2c_client *client, u16 offset,void *buf,int size)
657 {
658         unsigned char objectAddr[2+size];
659         int retlen;
660
661         objectAddr[0] = offset & 0x00FF;
662         objectAddr[1] = offset >> 8;
663         memcpy(&objectAddr[2], (char *)buf, size);
664         retlen = i2c_master_normal_send(client, objectAddr,2 + size, 200*1000);
665         
666     return retlen;
667 }
668
669 static int mxt224_i2c_read(struct i2c_client *client, u16 offset,void *buf,int size)
670 {
671         unsigned char objectAddr[2];
672         int retlen;
673
674         if(ts_data.last_read_addr != offset)
675         {
676         ts_data.last_read_addr = offset;
677         objectAddr[0] = offset & 0x00FF;
678         objectAddr[1] = offset >> 8;
679         retlen = i2c_master_normal_send(client, objectAddr,2, 200*1000);
680         if(retlen <= 0)
681                 return retlen;
682         }
683         retlen = i2c_master_normal_recv(client, (char *)buf, size, 200*1000);
684         
685         return retlen;
686 }
687
688 static int mXT224_probe(struct i2c_client *client, const struct i2c_device_id *id)
689 {
690     int     rc;
691     int     index;
692     int     info_size;
693     u32     crc;
694     u16     object_addr;
695         
696         struct message_t5 msg_t5;
697
698          if (!i2c_check_functionality(client->adapter, I2C_FUNC_I2C))
699                  return -EIO;
700          /* Try get GPIO */
701          rc = gpio_request(ts_data.int_gpio, "Touch_int");
702          
703          if(rc)
704          {
705                  local_debug(KERN_INFO "%s: Request GPIO failed!\n", __func__);
706                  goto failed;
707          }
708
709          rc = gpio_request(ts_data.reset_gpio, "Touch_reset");
710          
711          if(rc)
712          {
713                  local_debug(KERN_INFO "%s: Request mXT224 reset GPIO failed!\n", __func__);
714                  goto failed;
715          }
716
717      /* store the value */
718     i2c_set_clientdata(client, &ts_data);
719     ts_data.client = client;
720         ts_data.int_gpio= client->irq;
721     //client->driver = &mXT224_driver;  
722
723         ts_data.mxt224_wq = create_rt_workqueue("mxt224_wq");
724         INIT_DELAYED_WORK(&ts_data.work, mXT224_work_func);     
725         
726     /* Reset mXT224 */
727         gpio_pull_updown(ts_data.int_gpio, 1);
728         
729         gpio_direction_output(ts_data.reset_gpio, 0);
730     gpio_set_value(ts_data.reset_gpio, GPIO_LOW);
731     msleep(10);
732     gpio_set_value(ts_data.reset_gpio, GPIO_HIGH);
733     msleep(500);
734
735
736     /* Try get mXT224 table size */
737     
738      rc = mxt224_i2c_read(client, TABLE_SIZE_ADDR, &ts_data.obj.table_size, 1);
739     
740     if(rc <= 0)
741     {
742         local_debug(KERN_INFO "%s: Get table size failed!\n", __func__);
743         goto failed;
744     }
745
746
747     
748     /* Try get mXT224 device info */
749     info_size = CRC_SIZE+ID_INFORMATION_SIZE+ts_data.obj.table_size*OBJECT_TABLE_ELEMENT_SIZE;
750     
751     ts_data.obj.table_info_byte = (u8*)kzalloc(info_size, GFP_KERNEL);
752     
753     if(!ts_data.obj.table_info_byte)
754     {
755         local_debug(KERN_INFO "%s: Can't get memory!\n", __func__);
756         rc = -1;
757         goto failed;
758     }
759     
760     rc = mxt224_i2c_read(client, 0, ts_data.obj.table_info_byte, info_size);
761     
762     if(rc <= 0)
763     {
764         local_debug(KERN_INFO "%s: Get mXT224 info failed!\n", __func__);
765         goto get_info_failed;
766     }
767     
768     ts_data.obj.table_info = (struct mxt224_table_info*)(ts_data.obj.table_info_byte+ID_INFORMATION_SIZE);
769     mxt224_mem_dbg(ts_data.obj.table_info_byte, info_size);
770
771  
772
773 #if 0
774     /* Try get and check CRC */
775     ts_data.obj.info_crc = (ts_data.obj.table_info_byte[info_size-3])|
776                            (ts_data.obj.table_info_byte[info_size-2]<<8)|
777                            (ts_data.obj.table_info_byte[info_size-1]<<16);
778     crc = get_crc24(ts_data.obj.table_info_byte, info_size-CRC_SIZE);
779     
780     if(ts_data.obj.info_crc != crc)
781     {
782         //1 TODO: Need set config table
783         
784         local_debug(KERN_INFO "%s:CRC failed, read CRC:[0x%x], get CRC:[0x%x]\n", __func__, ts_data.obj.info_crc, crc);
785         
786         mXT224_load_cfg();
787     }
788
789
790     /* Build cfg CRC */
791     cfg_crc = mXT224_cfg_crc();
792 #endif 
793
794  
795
796     /* Build report id table */
797     mXT224_build_report_id_table(ts_data.obj.table_info, ts_data.obj.table_size);
798
799  
800
801     /* Dump mXT224 config setting */
802 #ifdef FEATURE_CFG_DUMP
803     
804     local_debug(KERN_INFO "%s: Config size: %d\n", __func__, total_size);
805     
806     cfg_dmup = (u8*)kzalloc(info_size+total_size, GFP_KERNEL);
807     
808     if(!cfg_dmup)
809     {
810         local_debug(KERN_INFO "%s: Cannot get memory!\n", __func__);
811         goto failed;
812     }
813     
814     mxt224_i2c_read(client, 0, cfg_dmup, info_size+total_size);
815     mxt224_mem_dbg(cfg_dmup, info_size+total_size);
816 #endif
817
818  
819
820     /* Try get message T5 info */
821     if(gpio_get_value(ts_data.int_gpio))
822     {
823         //1 TODO: Need check touch interrput pin
824         
825         local_debug(KERN_INFO "%s: GPIO status error!\n", __func__);
826         
827         rc = -1;
828         goto failed;
829     }
830
831     ts_data.obj.msg_t5_addr = mXT224_get_obj_addr(0x5, ts_data.obj.table_info, ts_data.obj.table_size);
832     
833     rc = mxt224_i2c_read(client, ts_data.obj.msg_t5_addr, (u8*)&msg_t5, sizeof(struct message_t5));
834     
835     if(rc <= 0)
836     {
837         local_debug(KERN_INFO "%s:Can't get message T5!\n", __func__);
838         goto failed;
839     }
840     
841     mxt224_mem_dbg((u8*)&msg_t5, sizeof(struct message_t5));
842     mXT224_process_msg(mXT224_get_obj_type(msg_t5.report_id, id_table, ts_data.obj.table_size), (u8*)&msg_t5.body);
843
844     object_addr = mXT224_get_obj_addr(0x9, ts_data.obj.table_info, ts_data.obj.table_size);
845     
846     rc = mxt224_i2c_read(client, object_addr, (u8*)&T9_cfg[0], 31);
847     
848     if(rc <= 0)
849     {
850         local_debug(KERN_INFO "%s:Can't get message T9!\n", __func__);
851         goto failed;
852     }
853     
854     mxt224_mem_dbg((u8*)&T9_cfg[0], 31);
855
856     local_debug(KERN_INFO "%s:Change T9 orient to [0]!\n", __func__);
857         
858         T9_cfg[9] = 0;
859         rc = mxt224_i2c_write(client, object_addr, (u8*)&T9_cfg[0], 31);
860     if(rc <= 0)
861     {
862         local_debug(KERN_INFO "%s:Can't write message T9!\n", __func__);
863         goto failed;
864     }
865
866     rc = mxt224_i2c_read(client, object_addr, (u8*)&T9_cfg[0], 31);
867     
868     if(rc <= 0)
869     {
870         local_debug(KERN_INFO "%s:Can't get message T9!\n", __func__);
871         goto failed;
872     }
873         mxt224_mem_dbg((u8*)&T9_cfg[0], 31);
874         
875     //local_debug(KERN_INFO "%s:Find obj report: 0x%x\n", __func__, 
876     //            mXT224_get_obj_type(msg_t5.report_id, id_table, obj_table_size));
877     msleep(15);
878     
879     if(gpio_get_value(ts_data.int_gpio))
880     {
881         local_debug(KERN_INFO "%s: GPIO value is high\n", __func__);
882     }
883    
884
885     ts_data.input_dev = input_allocate_device();
886     if (!ts_data.input_dev)
887     {
888         rc = -ENOMEM;
889         goto failed;
890     }
891     
892     input_set_drvdata(ts_data.input_dev, &ts_data);
893
894         snprintf(ts_data.phys, sizeof(ts_data.phys),
895                  "%s/input0", dev_name(&client->dev));
896
897     ts_data.input_dev->name = "mXT224_touch";
898     ts_data.input_dev->id.bustype = BUS_I2C;
899         ts_data.input_dev->phys = ts_data.phys;
900
901 #if 0    
902     set_bit(EV_SYN, ts_data.input_dev->evbit);
903     set_bit(EV_KEY, ts_data.input_dev->evbit);
904     set_bit(BTN_TOUCH, ts_data.input_dev->keybit);
905     set_bit(BTN_2, ts_data.input_dev->keybit);
906     set_bit(EV_ABS, ts_data.input_dev->evbit);
907     
908     input_set_abs_params(ts_data.input_dev, ABS_X, 0, 240, 0, 0);
909     input_set_abs_params(ts_data.input_dev, ABS_Y, 0, 320, 0, 0);
910     
911     for(index=0; index<(sizeof(key_info)/sizeof(key_info[0])); index++)
912      {
913         input_set_capability(ts_data.input_dev, EV_KEY, key_info[index].code);
914     }
915 #else
916         ts_data.input_dev->evbit[0] = BIT_MASK(EV_ABS)|BIT_MASK(EV_KEY)|BIT_MASK(EV_SYN);
917         ts_data.input_dev->keybit[BIT_WORD(BTN_TOUCH)] = BIT_MASK(BTN_TOUCH);
918         //ts_data.input_dev->keybit[BIT_WORD(BTN_2)] = BIT_MASK(BTN_2); 
919
920     for(index=0; index<(sizeof(key_info)/sizeof(key_info[0])); index++)
921      {
922         input_set_capability(ts_data.input_dev, EV_KEY, key_info[index].code);
923     }
924         input_set_abs_params(ts_data.input_dev, ABS_X, 0, CONFIG_MXT224_MAX_X, 0, 0);
925         input_set_abs_params(ts_data.input_dev, ABS_Y, 0, CONFIG_MXT224_MAX_Y, 0, 0);
926         input_set_abs_params(ts_data.input_dev, ABS_PRESSURE, 0, 255, 0, 0);
927         input_set_abs_params(ts_data.input_dev, ABS_TOOL_WIDTH, 0, 15, 0, 0);
928         input_set_abs_params(ts_data.input_dev, ABS_HAT0X, 0, CONFIG_MXT224_MAX_X, 0, 0);
929         input_set_abs_params(ts_data.input_dev, ABS_HAT0Y, 0, CONFIG_MXT224_MAX_Y, 0, 0);
930         input_set_abs_params(ts_data.input_dev, ABS_MT_POSITION_X,0, CONFIG_MXT224_MAX_X, 0, 0);
931         input_set_abs_params(ts_data.input_dev, ABS_MT_POSITION_Y, 0, CONFIG_MXT224_MAX_Y, 0, 0);
932         input_set_abs_params(ts_data.input_dev, ABS_MT_TOUCH_MAJOR, 0, 255, 0, 0);
933         input_set_abs_params(ts_data.input_dev, ABS_MT_WIDTH_MAJOR, 0, 255, 0, 0);
934         input_set_abs_params(ts_data.input_dev, ABS_MT_TRACKING_ID, 0, 10, 0, 0);  
935
936 #endif
937     rc = input_register_device(ts_data.input_dev);
938     
939     if (rc) 
940     {
941         dev_err(&client->dev, "mXT224: input_register_device rc=%d\n", rc);
942         goto failed;
943     }
944         
945         ts_data.int_gpio = gpio_to_irq(ts_data.int_gpio);
946
947     rc = request_irq(ts_data.int_gpio, mXT224_ts_interrupt,
948                      IRQF_TRIGGER_FALLING, "mXT224_touch", &ts_data);
949         if(rc)
950         {
951                 local_debug(KERN_INFO "mXT224 request interrput failed!\n");
952         }else{
953                 local_debug(KERN_INFO "mXT224 request interrput successed!\n");
954         }
955
956     
957     return 0;
958
959 get_info_failed:
960     /* Free mXT224 info */
961 failed:
962
963         if(ts_data.input_dev != NULL)
964         input_free_device(ts_data.input_dev);
965
966 #ifdef FEATURE_CFG_DUMP
967         kfree(cfg_dmup);
968 #endif
969         kfree(ts_data.obj.table_info_byte);
970
971         if(id_table != NULL)
972         {
973                 kfree(id_table);
974                 id_table = NULL;
975         }
976
977     return rc;
978 }
979
980
981 static int __devexit mXT224_remove(struct i2c_client *client)
982 {
983         struct mXT224_info *ts = i2c_get_clientdata(client);
984         
985         free_irq(ts->int_gpio, ts);
986         if (cancel_delayed_work_sync(&ts->work)) {
987                 /*
988                  * Work was pending, therefore we need to enable
989                  * IRQ here to balance the disable_irq() done in the
990                  * interrupt handler.
991                  */
992                 enable_irq(ts->int_gpio);
993         }
994
995         input_unregister_device(ts->input_dev);
996         
997         if(id_table != NULL)
998         {
999                 kfree(id_table);
1000                 id_table = NULL;
1001         }
1002         return 0;
1003 }
1004
1005 static const struct i2c_device_id mXT224_ts_id[] = {
1006     { "mXT224_touch", 0 },
1007     { }
1008 };
1009
1010 MODULE_DEVICE_TABLE(i2c, mXT224_ts_id);
1011
1012 static struct i2c_driver mXT224_driver = {
1013         .driver = {
1014                 .owner  = THIS_MODULE,
1015                 .name   = "mXT224_touch"
1016         },
1017         .id_table       = mXT224_ts_id,
1018         .probe          = mXT224_probe,
1019         .remove         = __devexit_p(mXT224_remove),
1020 };
1021
1022 static void __init mXT_init_async(void *unused, async_cookie_t cookie)
1023 {
1024         local_debug("--------> %s <-------------\n",__func__);
1025         i2c_add_driver(&mXT224_driver);
1026 }
1027
1028
1029 static int __init mXT_init(void)
1030 {
1031         async_schedule(mXT_init_async, NULL);
1032         return 0;
1033
1034 }
1035
1036 static void __exit mXT_exit(void)
1037 {
1038  i2c_del_driver(&mXT224_driver);
1039 }
1040
1041
1042 module_init(mXT_init);
1043 module_exit(mXT_exit);
1044
1045 MODULE_LICENSE("GPL");