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