Merge tag 'v4.4-rc6'
[firefly-linux-kernel-4.4.55.git] / drivers / input / touchscreen / ct360_ts.c
1 /*
2  * drivers/input/touchscreen/gt801_ts.c
3  *
4  * Copyright (C) 2010 ROCKCHIP, Inc.
5  *
6  * This software is licensed under the terms of the GNU General Public
7  * License version 2, as published by the Free Software Foundation, and
8  * may be copied, distributed, and modified under those terms.
9  *
10  * This program is distributed in the hope that it will be useful,
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13  * GNU General Public License for more details.
14  */
15
16 #include <linux/module.h>
17 #include <linux/delay.h>
18 #include <linux/earlysuspend.h>
19 #include <linux/hrtimer.h>
20 #include <linux/i2c.h>
21 #include <linux/input.h>
22 #include <linux/interrupt.h>
23 #include <linux/io.h>
24 #include <linux/gpio.h>
25 #include <mach/iomux.h>
26 #include <mach/board.h>
27 #include <linux/platform_device.h>
28 #include <linux/async.h>
29 #include <linux/workqueue.h>
30 #include <linux/slab.h>
31 #include <linux/input/mt.h>
32 #include <asm/mach/time.h>
33 #include "ct360_calib.h"
34 #include "ct360_ch.h"
35
36 #define CT360_DEBUG                     0
37 #define MYCT360_DEBUG                 0
38
39 #if CT360_DEBUG
40         #define ct360printk(msg...)     printk(msg);
41 #else
42         #define ct360printk(msg...)
43 #endif
44
45 #if 0
46         #define  yj_printk(msg...)  printk(msg);
47 #else
48         #define  yj_printk(msg...)
49 #endif
50
51 #if 1
52         #define  boot_printk(msg...)  printk(msg);
53 #else
54         #define  boot_printk(msg...)
55 #endif
56
57
58 #if MYCT360_DEBUG
59         #define myct360printk(msg...)   printk(msg);
60 #else
61         #define myct360printk(msg...)
62 #endif
63
64 static int touch_flag_up=0;
65 static int touch_flag_down = 0;
66 static int flag=0;
67 static int last_num_point=1;
68
69 static int last_x[2]={0,0};
70 static int last_y[2]={0,0};
71
72 #define ct360_TS_NAME "ct360_ts"
73 #define TOUCH_NUMBER 5
74 #define TOUCH_REG_NUM 4 
75 #define IOMUX_NAME_SIZE 48
76
77 enum regadd {
78         ptxh = 0, ptxl = 1, ptyh = 2, ptyl = 3, ptpressure = 4,
79 };
80 enum touchstate {
81         TOUCH_UP = 0, TOUCH_DOWN = 1,
82 };
83
84
85
86 const unsigned char GT801_RegData[]={   
87         0x0F,0x02,0x04,0x28,0x02,0x14,0x14,0x10,0x28,0xFA,0x03,0x20,0x05,0x00,0x01,
88         0x23,0x45,0x67,0x89,0xAB,0xCD,0xE1,0x00,0x00,0x35,0x2E,0x4D,0xC1,0x20,0x05,
89         0x00,0x80,0x50,0x3C,0x1E,0xB4,0x00,0x33,0x2C,0x01,0xEC,0x00,0x32,0x00,0x00,
90         0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x01
91 };
92
93 /*tochscreen private data*/
94 static int touch_state[TOUCH_NUMBER] = {TOUCH_UP,TOUCH_UP};
95 struct ct360_ts_data  *ct360;
96 #ifdef CONFIG_HAS_EARLYSUSPEND
97 static void ct360_ts_early_suspend(struct early_suspend *h);
98 static void ct360_ts_late_resume(struct early_suspend *h);
99 #endif
100
101
102 static int ct360_read_regs(struct i2c_client *client, u8 buf[], unsigned len)
103 {
104         int ret;
105         ret = i2c_master_normal_recv(client, buf, len, 400*1000);
106         if(ret < 0)
107                 printk("ct360_ts_work_func:i2c_transfer fail =%d\n",ret);
108         return ret;
109 }
110 /* set the ct360 registe,used i2c bus*/
111 static int ct360_write_regs(struct i2c_client *client, u8 const buf[], unsigned short len)
112 {
113         int ret;
114         ret = i2c_master_normal_send(client, buf, len, 100*1000);
115         if (ret < 0) {
116           printk("ct360_ts_work_func:i2c_transfer fail =%d\n",ret);
117     }
118         return ret;
119 }
120
121 extern char Binary_Data[16384]; 
122
123 //update firmware
124 #define CT36X_TS_I2C_SPEED 300*1000
125
126 static void ct36x_ts_reg_read(struct i2c_client *client, unsigned short addr, char *buf, int len, int rate)
127 {
128         struct i2c_msg msgs;
129
130         msgs.addr = addr;
131         msgs.flags = 0x01;  // 0x00: write 0x01:read 
132         msgs.len = len;
133         msgs.buf = buf;
134         msgs.scl_rate = rate;
135         i2c_transfer(client->adapter, &msgs, 1);
136 }
137
138 static void ct36x_ts_reg_write(struct i2c_client *client, unsigned short addr, char *buf, int len, int rate)
139 {
140         struct i2c_msg msgs;
141
142         msgs.addr = addr;
143         msgs.flags = 0x00;  // 0x00: write 0x01:read 
144         msgs.len = len;
145         msgs.buf = buf;
146         msgs.scl_rate = rate;
147         i2c_transfer(client->adapter, &msgs, 1);
148 }
149
150
151 int CT360_CTP_BootLoader(struct ct360_ts_data *ts)
152 {
153         int i = 0, j = 0;
154         unsigned int ver_chk_cnt = 0;
155         unsigned int flash_addr = 0;
156         unsigned char CheckSum[16];
157
158         //------------------------------
159         // Step1 --> initial BootLoader
160         // Note. 0x7F -> 0x00 -> 0xA5 ;
161         // MCU goto idle
162         //------------------------------
163         printk("%s() Set mcu to idle \n", __FUNCTION__);
164         ts->data.buf[0] = 0x00;
165         ts->data.buf[1] = 0xA5;
166         ct36x_ts_reg_write(ts->client, 0x7F, ts->data.buf, 2, CT36X_TS_I2C_SPEED);
167         mdelay(10);
168         
169         //------------------------------
170         // Reset I2C Offset address
171         // Note. 0x7F -> 0x00   
172         //------------------------------
173         printk(&"%s() Reset i2c offset address \n", __FUNCTION__);
174         ts->data.buf[0] = 0x00;
175         ct36x_ts_reg_write(ts->client, 0x7F, ts->data.buf, 1, CT36X_TS_I2C_SPEED);
176         mdelay(10);
177         
178         //------------------------------
179         // Read I2C Bus status
180         //------------------------------
181         printk("%s() Read i2c bus status \n", __FUNCTION__);
182         ct36x_ts_reg_read(ts->client, 0x7F, ts->data.buf, 1, CT36X_TS_I2C_SPEED);
183         mdelay(10);                                                                     // Delay 1 ms
184
185         // if return "AAH" then going next step
186         if (ts->data.buf[0] != 0xAA)
187         {
188                 printk("%s() i2c bus status: 0x%x \n", __FUNCTION__, ts->data.buf[0]);
189                 return -1;
190         }
191
192         //------------------------------
193         // Check incomplete flash erase
194         //------------------------------
195         printk("%s() Flash erase verify \n", __FUNCTION__);
196         ts->data.buf[0] = 0x00;
197         ts->data.buf[1] = 0x99;                 // Generate check sum command  -->read flash, set addr
198         ts->data.buf[2] = 0x00;                 // define a flash address for CT36x to generate check sum
199         ts->data.buf[3] = 0x00;                 //
200         ts->data.buf[4] = 0x08;                 // Define a data length for CT36x to generate check sum
201
202         // Write Genertate check sum command to CT36x
203         ct36x_ts_reg_write(ts->client, 0x7F, ts->data.buf, 5, CT36X_TS_I2C_SPEED);
204         mdelay(10);                                                             // Delay 10 ms
205
206         ct36x_ts_reg_read(ts->client, 0x7F, ts->data.buf, 13, CT36X_TS_I2C_SPEED);
207         mdelay(10);                                                             // Delay 10 ms 
208
209         CheckSum[0] = ts->data.buf[5];
210         CheckSum[1] = ts->data.buf[6];
211
212         ts->data.buf[0] = 0x00;
213         ts->data.buf[1] = 0x99;                 // Generate check sum command  -->read flash, set addr
214         ts->data.buf[2] = 0x3F;                 // define a flash address for CT36x to generate check sum
215         ts->data.buf[3] = 0xE0;                 //
216         ts->data.buf[4] = 0x08;                 // Define a data length for CT36x to generate check sum
217         // Write Genertate check sum command to CT36x
218         ct36x_ts_reg_write(ts->client, 0x7F, ts->data.buf, 5, CT36X_TS_I2C_SPEED);
219         mdelay(10);                                                             // Delay 10 ms
220
221         ct36x_ts_reg_read(ts->client, 0x7F, ts->data.buf, 13, CT36X_TS_I2C_SPEED);
222         mdelay(10);
223
224         CheckSum[2] = ts->data.buf[5];
225         CheckSum[3] = ts->data.buf[6];
226
227         if ( (CheckSum[0] ^ CheckSum[2]) == 0xFF && (CheckSum[1] ^ CheckSum[3]) == 0xFF )
228                 goto FLASH_ERASE;
229         
230         //------------------------------
231         // check valid Vendor ID
232         //------------------------------
233         printk("%s() Vendor ID Check \n", __FUNCTION__);
234         ts->data.buf[0] = 0x00;
235         ts->data.buf[1] = 0x99;                 // Generate check sum command  -->read flash, set addr
236         ts->data.buf[2] = 0x00;                 // define a flash address for CT365 to generate check sum
237         ts->data.buf[3] = 0x44;                 //
238         ts->data.buf[4] = 0x08;                 // Define a data length for CT365 to generate check sum
239
240         // Write Genertate check sum command to CT36x
241         ct36x_ts_reg_write(ts->client, 0x7F, ts->data.buf, 5, CT36X_TS_I2C_SPEED);
242         mdelay(10);                                                             // Delay 10 ms
243
244         ct36x_ts_reg_read(ts->client, 0x7F, ts->data.buf, 13, CT36X_TS_I2C_SPEED);
245         mdelay(10);                                                             // Delay 10 ms 
246         
247         // Read check sum and flash data from CT36x
248         if ( (ts->data.buf[5] != 'V') || (ts->data.buf[9] != 'T') )
249                 ver_chk_cnt++;
250
251         ts->data.buf[0] = 0x00;
252         ts->data.buf[1] = 0x99;                 // Generate check sum command  -->read flash,set addr
253         ts->data.buf[2] = 0x00;                 // define a flash address for CT365 to generate check sum       
254         ts->data.buf[3] = 0xA4;                 //
255         ts->data.buf[4] = 0x08;                 // Define a data length for CT365 to generate check sum 
256
257         // Write Genertate check sum command to CT365
258         ct36x_ts_reg_write(ts->client, 0x7F, ts->data.buf, 5, CT36X_TS_I2C_SPEED);
259         mdelay(10);                                                             // Delay 10 ms
260
261         ct36x_ts_reg_read(ts->client, 0x7F, ts->data.buf, 13, CT36X_TS_I2C_SPEED);
262         mdelay(10);                                                             // Delay 10 ms 
263         
264         if ((ts->data.buf[5] != 'V') || (ts->data.buf[9] != 'T'))
265                 ver_chk_cnt++;
266
267         if ( ver_chk_cnt >= 2 ) {
268                 printk("%s() Invalid FW Version \n", __FUNCTION__);
269                 return -1;
270         }
271
272 FLASH_ERASE:
273         //-----------------------------------------------------
274         // Step 2 : Erase 32K flash memory via Mass Erase (33H)  
275         // 0x7F --> 0x00 --> 0x33 --> 0x00;
276         //-----------------------------------------------------
277         printk("%s() Erase flash \n", __FUNCTION__);
278         for(i = 0; i < 8; i++ ) {
279                 ts->data.buf[0] = 0x00;                 // Offset address
280                 ts->data.buf[1] = 0x33;                 // Mass Erase command
281                 ts->data.buf[2] = 0x00 + (i * 8);  
282                 ct36x_ts_reg_write(ts->client, 0x7F, ts->data.buf, 3, CT36X_TS_I2C_SPEED);
283                 mdelay(120);                            // Delay 10 mS
284
285                 //------------------------------
286                 // Reset I2C Offset address
287                 // Note. 0x7F -> 0x00   
288                 //------------------------------
289                 ts->data.buf[0] = 0x00;
290                 ct36x_ts_reg_write(ts->client, 0x7F, ts->data.buf, 1, CT36X_TS_I2C_SPEED);
291                 mdelay(120);                            // Delay 10 mS
292
293                 //------------------------------
294                 // Read I2C Bus status
295                 //------------------------------
296                 ct36x_ts_reg_read(ts->client, 0x7F, ts->data.buf, 1, CT36X_TS_I2C_SPEED);
297                 mdelay(10);                                                     // Delay 1 ms 
298
299                 // if return "AAH" then going next step
300                 if( ts->data.buf[0] != 0xAA )
301                         return -1;
302         }
303
304         //----------------------------------------
305         // Step3. Host write 128 bytes to CT36x
306         // Step4. Host read checksum to verify ;
307         // Write/Read for 256 times ( 32k Bytes )
308         //----------------------------------------
309         printk("%s() flash FW \n", __FUNCTION__);
310         for ( flash_addr = 0; flash_addr < 0x3FFF; flash_addr+=8 ) {
311                 // Step 3 : write binary data to CT36x
312                 ts->data.buf[0] = 0x00;                                                         // Offset address 
313                 ts->data.buf[1] = 0x55;                                                         // Flash write command
314                 ts->data.buf[2] = (char)(flash_addr  >> 8);                     // Flash address [15:8]
315                 ts->data.buf[3] = (char)(flash_addr & 0xFF);                    // Flash address [7:0]
316                 ts->data.buf[4] = 0x08;                                                         // Data Length 
317
318                 if( flash_addr == 160 || flash_addr == 168 ) {
319                         ts->data.buf[6] = ~Binary_Data[flash_addr + 0]; // Binary data 1
320                         ts->data.buf[7] = ~Binary_Data[flash_addr + 1]; // Binary data 2
321                         ts->data.buf[8] = ~Binary_Data[flash_addr + 2]; // Binary data 3
322                         ts->data.buf[9] = ~Binary_Data[flash_addr + 3]; // Binary data 4
323                         ts->data.buf[10] = ~Binary_Data[flash_addr + 4];        // Binary data 5
324                         ts->data.buf[11] = ~Binary_Data[flash_addr + 5];        // Binary data 6
325                         ts->data.buf[12] = ~Binary_Data[flash_addr + 6];        // Binary data 7
326                         ts->data.buf[13] = ~Binary_Data[flash_addr + 7];        // Binary data 8
327                 } else {
328                         ts->data.buf[6] = Binary_Data[flash_addr + 0];                  // Binary data 1
329                         ts->data.buf[7] = Binary_Data[flash_addr + 1];                  // Binary data 2
330                         ts->data.buf[8] = Binary_Data[flash_addr + 2];                  // Binary data 3
331                         ts->data.buf[9] = Binary_Data[flash_addr + 3];                  // Binary data 4
332                         ts->data.buf[10] = Binary_Data[flash_addr + 4];                 // Binary data 5
333                         ts->data.buf[11] = Binary_Data[flash_addr + 5];                 // Binary data 6
334                         ts->data.buf[12] = Binary_Data[flash_addr + 6];                 // Binary data 7
335                         ts->data.buf[13] = Binary_Data[flash_addr + 7];                 // Binary data 8
336                 }
337                 // Calculate a check sum by Host controller. 
338                 // Checksum = / (FLASH_ADRH+FLASH_ADRL+LENGTH+
339                 // Binary_Data1+Binary_Data2+Binary_Data3+Binary_Data4+
340                 // Binary_Data5+Binary_Data6+Binary_Data7+Binary_Data8) + 1 
341                 CheckSum[0] = ~(ts->data.buf[2] + ts->data.buf[3] + ts->data.buf[4] + ts->data.buf[6] + ts->data.buf[7] + 
342                         ts->data.buf[8] + ts->data.buf[9] + ts->data.buf[10] + ts->data.buf[11] + ts->data.buf[12] +
343                         ts->data.buf[13]) + 1; 
344
345                 ts->data.buf[5] = CheckSum[0];                                          // Load check sum to I2C Buffer 
346
347                 ct36x_ts_reg_write(ts->client, 0x7F, ts->data.buf, 14, CT36X_TS_I2C_SPEED);                                                                     // Host write I2C_Buf[0?K12] to CT365. 
348                 mdelay(1);                                                                                                      // 8 Bytes program --> Need 1 ms delay time 
349
350                 // Step4. Verify process 
351                 printk("%s(flash_addr:0x%04x) Verify FW \n", __FUNCTION__, flash_addr);
352                 //Step 4 : Force CT365 generate check sum for host to compare data. 
353                 //Prepare get check sum from CT36x
354                 ts->data.buf[0] = 0x00;
355                 ts->data.buf[1] = 0x99;                                                         // Generate check sum command
356                 ts->data.buf[2] = (char)(flash_addr >> 8);                      // define a flash address for NT1100x to generate check sum 
357                 ts->data.buf[3] = (char)(flash_addr & 0xFF);            //
358                 ts->data.buf[4] = 0x08;                                                         // Define a data length for CT36x to generate check sum 
359
360                 ct36x_ts_reg_write(ts->client, 0x7F, ts->data.buf, 5, CT36X_TS_I2C_SPEED);                                                                      // Write Genertate check sum command to CT365
361                 mdelay(1);                                                                                                      // Delay 1 ms
362
363                 ct36x_ts_reg_read(ts->client, 0x7F, ts->data.buf, 13, CT36X_TS_I2C_SPEED);      // Read check sum and flash data from CT365
364
365                 // Compare host check sum with CT365 check sum(I2C_Buf[4])
366                 if ( ts->data.buf[4] != CheckSum[0] ) {
367                         return -1;
368                 }
369         }
370
371         return  0;
372 }
373
374
375 /*read the ct360 register ,used i2c bus*/
376
377 static int ct360_init_panel(struct ct360_ts_data *ts)
378 {
379     return 0;
380 }
381
382 static void report_value(int x,int y,struct ct360_ts_data *ts)
383 {
384         myct360printk("%s(%d,%d)\n", __FUNCTION__,x, y);
385         //if((x>ts->x_max)||(y>ts->y_max))
386         //      return;
387         myct360printk("x=%d,y=%d,swap=%d\n",x,y,ts->swap_xy);
388         if (ts->swap_xy){
389                 swap(x, y);
390         }
391         if((x==0)||(y==0))
392                 return;
393         input_report_abs(ts->input_dev, ABS_MT_TOUCH_MAJOR, 1); //Finger Size
394         input_report_abs(ts->input_dev, ABS_MT_POSITION_X, x);
395         input_report_abs(ts->input_dev, ABS_MT_POSITION_Y, y);
396         input_report_abs(ts->input_dev, ABS_MT_WIDTH_MAJOR, 1); //Touch Size
397         input_mt_sync(ts->input_dev);
398         
399 }
400
401 static void ct360_ts_work_func(struct work_struct *work)
402 {
403         //struct timespec now;
404     //now = current_kernel_time();//(&now);
405         //printk("now %lu:%lu\n",now.tv_sec,now.tv_nsec);
406         
407         unsigned short x = 0;
408         unsigned short y = 0;
409         int i,ret,syn_flag = 0;
410         char toatl_num = 0;
411         int bufLen = 0;
412         unsigned char buf[TOUCH_REG_NUM*TOUCH_NUMBER+1] = {0};
413         int point_status;
414         int point_id;
415         int touch_state_index=0;
416         int pendown = 0;
417         struct ct360_ts_data *ts = container_of(work, struct ct360_ts_data, work);
418         //printk("before read the gpio_get_value(ts->client->irq) is %d\n",gpio_get_value(ts->client->irq));
419         
420         ret= ct360_read_regs(ts->client, buf, TOUCH_REG_NUM*TOUCH_NUMBER);//only one data  represent the current touch num
421         if (ret < 0) {
422                 printk("%s:i2c_transfer fail =%d\n", __FUNCTION__, toatl_num);
423                 //enable_irq(ts->irq);
424                 return;
425         }
426         
427     for (i = 0; i < 20; i += TOUCH_REG_NUM)
428     {
429                 point_status = buf[i] & 0x0F;
430                 point_id = buf[i] >> 4;
431                 //printk("point_status:0x%02x, point_id:0x%02x  i = %d\n", point_status, point_id, i);
432                 //printk("buf: 0x%02x, 0x%02x, 0x%02x, 0x%02x\n", buf[i], buf[i+1], buf[i+2], buf[i+3]);
433                 //if (point_status != 0)
434                 {
435                         
436                         if((point_status == 1) || (point_status == 2)) {
437                                 x = (((s16)buf[i+1] << 4)|((s16)buf[i+3] >> 4));
438                                 y = (((s16)buf[i+2] << 4)|((s16)buf[i+3] & 0x0f));
439                                 //printk("x=%d,y=%d\n",x,y);
440                                 
441                                 input_mt_slot(ts->input_dev, point_id);
442                                 input_mt_report_slot_state(ts->input_dev, MT_TOOL_FINGER, true);
443                                 input_report_abs(ts->input_dev, ABS_MT_TRACKING_ID, point_id);
444                                 input_report_abs(ts->input_dev, ABS_MT_TOUCH_MAJOR, 100); //Finger Size
445                                 input_report_abs(ts->input_dev, ABS_MT_POSITION_X, x);
446                                 input_report_abs(ts->input_dev, ABS_MT_POSITION_Y, y);
447                                 input_report_abs(ts->input_dev, ABS_MT_WIDTH_MAJOR, 100); //Touch Size
448                         
449                                 syn_flag = 1;
450                         //      touch_state[touch_state_index] = TOUCH_DOWN;
451                                 //printk("TOUCH_DOWN\n");
452                         //      last_x[touch_state_index]=x;
453                         //      last_y[touch_state_index]=y;
454                         //   pendown = 1;
455                         
456                         } else if(point_status == 3 || point_status == 0) {
457                                 //if(touch_state[touch_state_index] == TOUCH_DOWN){
458                                         //ct360printk("%s:%d touch up\n",__FUNCTION__,i);
459                                         input_mt_slot(ts->input_dev, point_id);
460                                         input_mt_report_slot_state(ts->input_dev, MT_TOOL_FINGER, false);
461                                         input_report_abs(ts->input_dev, ABS_MT_TRACKING_ID, -1);
462                                         syn_flag = 1;
463                                         touch_state[touch_state_index] = TOUCH_UP;
464                                         //printk("TOUCH_UP:%d\n", point_id);
465                 //              }
466                         }
467                 }
468         }
469         if(syn_flag)
470                 input_sync(ts->input_dev);
471     
472 out:
473         //printk("the gpio_get_value(ts->client->irq) is %d\n",gpio_get_value(ts->client->irq));
474         //if(pendown==1)
475         //{
476         //      queue_delayed_work(ts->ct360_wq, &ts->work, msecs_to_jiffies(15));
477                 //pendown = 0;
478         //}
479         //else
480 //              enable_irq(ts->irq);
481         
482         return;
483 }
484
485 static irqreturn_t ct360_ts_irq_handler(int irq, void *dev_id)
486 {
487     struct ct360_ts_data *ts = dev_id;
488         //printk("the ts->irq is %d   ts->client->addr=%d\n",gpio_get_value(ts->irq),ts->client->addr);
489 //    disable_irq_nosync(ts->irq);
490         
491     queue_work(ts->ct360_wq, &ts->work);
492
493     return IRQ_HANDLED;
494 }
495
496 static int ct360_chip_Init(struct i2c_client *client)
497 {
498         int ret=0;
499         u8 start_reg=0x00;
500         unsigned char status;
501         printk("enter ct360_chip_Init!!!!\n");
502         u8 buf0[2];
503         buf0[0] = 0xA5;
504         client->addr = 0x01;
505         ret = i2c_master_reg8_send(client, start_reg, buf0, 1, 200*1000);
506         msleep(10);
507         u8 buf1[2];
508         buf1[0] = 0x00;
509         ret = i2c_master_reg8_send(client, start_reg, buf1, 1, 200*1000);
510         msleep(2);
511         if(ret<0){
512                 printk("\n--%s--Set Register values error !!!\n",__FUNCTION__);
513         }
514         i2c_master_reg8_recv(client,start_reg,&status,1,200*1000);
515         printk("the status is %x",status);
516         if(status != 0xAA)
517         {
518            printk("the status11 is %x",status);
519         }
520         
521         return ret;
522 }
523
524 static int ct360_ts_probe(struct i2c_client *client, const struct i2c_device_id *id)
525 {
526     struct ct360_ts_data *ts;
527         struct ct360_platform_data      *pdata = client->dev.platform_data;
528     int ret = 0;
529         char loader_buf[3] = {0xfF,0x0f,0x2A};
530         char boot_buf = 0;
531         char boot_loader[2] = {0};
532
533     ct360printk("%s \n",__FUNCTION__);
534         
535     if (!pdata) {
536                 dev_err(&client->dev, "empty platform_data\n");
537                 goto err_check_functionality_failed;
538     }
539     if (!i2c_check_functionality(client->adapter, I2C_FUNC_I2C)) {
540         printk(KERN_ERR "ct360_ts_probe: need I2C_FUNC_I2C\n");
541         ret = -ENODEV;
542         goto err_check_functionality_failed;
543     }
544         
545     ts = kzalloc(sizeof(*ts), GFP_KERNEL);
546     if (ts == NULL) {
547         ret = -ENOMEM;
548         goto err_alloc_data_failed;
549     }
550
551     ts->ct360_wq = create_singlethread_workqueue("ct360_wq");
552     if (!ts->ct360_wq){
553                 printk(KERN_ERR"%s: create workqueue failed\n", __func__);
554                 ret = -ENOMEM;
555                 goto err_input_dev_alloc_failed;
556         }
557
558     INIT_WORK(&ts->work, ct360_ts_work_func);
559     ts->client = client;
560     i2c_set_clientdata(client, ts);
561         
562         if(pdata->hw_init)
563                 pdata->hw_init();
564
565         if(pdata->shutdown){
566                 pdata->shutdown(1);
567                 mdelay(5);
568                 pdata->shutdown(0);
569                 mdelay(20);
570                 pdata->shutdown(1);
571                 mdelay(20);
572         }
573         
574 #if 1
575         mdelay(20);
576         mdelay(20);
577         ret=ct360_write_regs(client,loader_buf, 3);     
578         if(ret<0){
579                 printk("\n--%s--Set Register values error !!!\n",__FUNCTION__);
580         }
581
582         mdelay(1);
583         printk("%s...........%d\n",__FUNCTION__,boot_buf);
584         ret = i2c_master_normal_send(client,boot_loader,1,100*1000);
585         if(ret < 0)
586                 printk("ct360_ts_probe:sdf  i2c_transfer fail =%d\n",ret);
587         else
588                 printk("%s.............ok\n",__FUNCTION__);     
589
590         mdelay(2);
591         ret = ct360_read_regs(client,&boot_buf,1);
592         printk("%s....3......%x\n",__FUNCTION__,boot_buf);
593         
594         if(ret < 0)
595                 printk("ct360_ts_probe:i2c_transfer fail =%d\n",ret);
596         else
597                 printk("%s.............boot_buf=%d\n",__FUNCTION__,boot_buf);
598
599         if ((Binary_Data[16372] - boot_buf) >= 1)
600         {
601                 printk("start Bootloader ...........boot_Buf=%x.....%d......%x..........TP \n\n",boot_buf,(Binary_Data[16372]-boot_buf),Binary_Data[16372]);
602                 ret = CT360_CTP_BootLoader(ts);
603                 if (ret == 0)
604                         printk("TP Bootloader success\n");
605                 else
606                         printk("TP Bootloader failed  ret=%d\n",ret);
607                 printk("stop Bootloader.................................TP \n\n");
608         }
609         else
610         {
611                 printk("Don't need bootloader.skip it %x \n",Binary_Data[16372]);
612         }
613 #endif
614         ts->client->addr = 0x01;
615         if(pdata->shutdown){
616                 pdata->shutdown(1);
617                 mdelay(5);
618                 pdata->shutdown(0);
619                 mdelay(20);
620                 pdata->shutdown(1);
621                 mdelay(30);
622         }
623
624         ts->client->addr = 0x01;
625         //ret=ct360_chip_Init(ts->client);
626         //if(ret<0)
627         //{
628         //      printk("%s:chips init failed\n",__FUNCTION__);
629         //      goto err_input_dev_alloc_failed;
630         //}
631
632
633         
634     /* allocate input device */
635     ts->input_dev = input_allocate_device();
636     if (ts->input_dev == NULL) {
637         ret = -ENOMEM;
638         printk(KERN_ERR "%s: Failed to allocate input device\n",__FUNCTION__);
639         goto err_input_dev_alloc_failed;
640     }
641         
642         ts->x_max = pdata->x_max;
643         ts->y_max = pdata->y_max;
644         ts->swap_xy = 1;
645         ts->input_dev->name = ct360_TS_NAME;
646         ts->input_dev->dev.parent = &client->dev;
647
648     __set_bit(EV_ABS, ts->input_dev->evbit);
649     __set_bit(EV_KEY, ts->input_dev->evbit);
650     __set_bit(EV_REP,  ts->input_dev->evbit);
651     __set_bit(INPUT_PROP_DIRECT, ts->input_dev->propbit);
652     set_bit(ABS_MT_POSITION_X, ts->input_dev->absbit);
653     set_bit(ABS_MT_POSITION_Y, ts->input_dev->absbit);
654     set_bit(ABS_MT_TOUCH_MAJOR, ts->input_dev->absbit);
655     set_bit(ABS_MT_WIDTH_MAJOR, ts->input_dev->absbit);
656     
657     input_mt_init_slots(ts->input_dev, 5);    
658     input_set_abs_params(ts->input_dev,ABS_MT_POSITION_X, 0, ts->x_max, 0, 0);
659     input_set_abs_params(ts->input_dev,ABS_MT_POSITION_Y, 0, ts->y_max, 0, 0);
660     input_set_abs_params(ts->input_dev,ABS_MT_TOUCH_MAJOR, 0, 255, 0, 0);
661     input_set_abs_params(ts->input_dev,ABS_MT_WIDTH_MAJOR, 0, 200, 0, 0);
662
663     ret = input_register_device(ts->input_dev);
664     if (ret) {
665         printk(KERN_ERR "%s: Unable to register %s input device\n", __FUNCTION__,ts->input_dev->name);
666         goto err_input_register_device_failed;
667     }
668
669         ts->irq = gpio_to_irq(client->irq);
670         ret = request_irq(ts->irq, ct360_ts_irq_handler, IRQF_TRIGGER_FALLING, client->name, ts);
671         if (ret){
672                 printk("!!! ct360 request_irq failed\n");
673                 goto err_input_register_device_failed;
674         }
675         
676 #ifdef CONFIG_HAS_EARLYSUSPEND
677     ts->early_suspend.level = EARLY_SUSPEND_LEVEL_BLANK_SCREEN + 1;
678     ts->early_suspend.suspend = ct360_ts_early_suspend;
679     ts->early_suspend.resume = ct360_ts_late_resume;
680     register_early_suspend(&ts->early_suspend);
681 #endif
682     printk(KERN_INFO "%s: probe ok  ts->client->addr=%d!!\n", __FUNCTION__,ts->client->addr);
683
684     return 0;
685
686 err_input_register_device_failed:
687     input_free_device(ts->input_dev);
688 err_input_dev_alloc_failed:
689         kfree(ts);
690 err_alloc_data_failed:
691 err_check_functionality_failed:
692         
693     return ret;
694 }
695
696 static int ct360_ts_remove(struct i2c_client *client)
697 {
698     struct ct360_ts_data *ts = i2c_get_clientdata(client);
699     unregister_early_suspend(&ts->early_suspend);
700         free_irq(ts->irq, ts);
701     input_unregister_device(ts->input_dev);
702     if (ts->ct360_wq)
703         destroy_workqueue(ts->ct360_wq);
704
705     kfree(ts);
706     return 0;
707 }
708
709 static int ct360_ts_suspend(struct i2c_client *client, pm_message_t mesg)
710 {
711     struct ct360_ts_data *ts = i2c_get_clientdata(client);
712     struct ct360_platform_data  *pdata = client->dev.platform_data;
713         int ret,i;
714     printk("ct360 TS Suspend\n");
715         //if(pdata->shutdown)
716         //      pdata->shutdown(1);
717         
718     disable_irq(ts->irq);
719     cancel_work_sync(&ts->work);
720         
721         char buf[3] = {0xff,0x0f,0x2b};
722         char buf1[2]={0x00,0x00};
723         //for(i=0;i<3;i++)
724         //{
725                 ret = i2c_master_normal_send(client,buf,3,100*1000);
726                 if(ret<0)
727                 {
728                         printk("ct360_ts supend fail!\n");
729                 }
730         //}
731         mdelay(1);
732         ret = i2c_master_normal_send(client,buf1,2,100*1000);
733         if(ret<0)
734         {
735                 printk("ct360_ts supend fail!!!\n");
736         }
737
738         //printk("the buf1 is %x\n",buf[0]);
739         //gpio_set_value(ts->gpio_reset, ts->gpio_reset_active_low? GPIO_LOW:GPIO_HIGH);
740         
741     return 0;
742 }
743
744 static int ct360_ts_resume(struct i2c_client *client)
745 {
746     struct ct360_ts_data *ts = i2c_get_clientdata(client);
747     struct ct360_platform_data  *pdata = client->dev.platform_data;
748    // if(pdata->shutdown)
749         //      pdata->shutdown(0);
750         
751     //ct360_init_panel(ts);
752     
753     printk("ct360 TS Resume\n");
754         if(pdata->shutdown){
755                 pdata->shutdown(1);
756                 mdelay(5);
757                 pdata->shutdown(0);
758                 mdelay(20);
759                 pdata->shutdown(1);
760                 mdelay(5);
761         }
762 //      gpio_set_value(ts->gpio_reset, ts->gpio_reset_active_low? GPIO_HIGH:GPIO_LOW);
763 //      msleep(50);
764
765         //printk("enabling IRQ %d\n", ts->irq);
766         enable_irq(ts->irq);
767         msleep(50);
768     return 0;
769 }
770
771 #ifdef CONFIG_HAS_EARLYSUSPEND
772 static void ct360_ts_early_suspend(struct early_suspend *h)
773 {
774         //#if 1
775     struct ct360_ts_data *ts;
776    //printk("======%s======\n",__FUNCTION__);
777     ts = container_of(h, struct ct360_ts_data, early_suspend);
778     ct360_ts_suspend(ts->client, PMSG_SUSPEND);
779         //#endif
780 }
781
782 static void ct360_ts_late_resume(struct early_suspend *h)
783 {
784         #if 1
785     struct ct360_ts_data *ts;
786     ts = container_of(h, struct ct360_ts_data, early_suspend);
787     ct360_ts_resume(ts->client);
788         #endif
789 }
790 #endif
791
792 static const struct i2c_device_id ct360_ts_id[] = {
793     { ct360_TS_NAME, 0 },
794     { }
795 };
796
797 static struct i2c_driver ct360_ts_driver = {
798     .probe      = ct360_ts_probe,
799     .remove     = ct360_ts_remove,
800 #ifndef CONFIG_HAS_EARLYSUSPEND
801     .suspend    = ct360_ts_suspend,
802     .resume     = ct360_ts_resume,
803 #endif
804     .id_table   = ct360_ts_id,
805     .driver = {
806         .name   = ct360_TS_NAME,
807     },
808 };
809
810 static int __devinit ct360_ts_init(void)
811 {
812     printk("%s\n",__FUNCTION__);
813
814     return i2c_add_driver(&ct360_ts_driver);
815 }
816
817 static void __exit ct360_ts_exit(void)
818 {
819     printk("%s\n",__FUNCTION__);
820     i2c_del_driver(&ct360_ts_driver);
821 }
822
823 late_initcall_sync(ct360_ts_init);
824 module_exit(ct360_ts_exit);
825
826 MODULE_DESCRIPTION("ct360 Touchscreen Driver");
827 MODULE_LICENSE("GPL");