rk3066 add phone pad modem support
[firefly-linux-kernel-4.4.55.git] / drivers / input / touchscreen / ct36x_ts / ct365.c
1
2 #include <linux/i2c.h>
3 #include <linux/delay.h>
4
5 #include "tscore.h"
6 #include "platform.h"
7 #include "ct365.h"
8
9 /*
10 * Private functions
11 */
12 #define CT36X_CHIP_FLASH_SECTOR_NUM     256
13 #define CT36X_CHIP_FLASH_SECTOR_SIZE    128
14 #define CT36X_CHIP_FLASH_SOURCE_SIZE    8
15
16 static unsigned char binary_data[] = {
17 //#include "CT365Five3020D_V42120523A.dat"
18 //#include "CT365_THSD_40X28_V05_120827_I2C0X01.dat"
19 };
20
21
22 static void ct36x_chip_set_idle(struct i2c_client *client, unsigned char *buf)
23 {
24         if ( CT36X_TS_CHIP_DEBUG )
25         printk(">>>>> %s() called <<<<< \n", __FUNCTION__);
26
27         buf[0] = 0x00;
28         buf[1] = 0xA5;
29         ct36x_ts_reg_write(client, 0x7F, buf, 2);
30         mdelay(10);
31 }
32
33 static void ct36x_chip_rst_offset(struct i2c_client *client, unsigned char *buf)
34 {
35         if ( CT36X_TS_CHIP_DEBUG )
36         printk(">>>>> %s() called <<<<< \n", __FUNCTION__);
37
38         buf[0] = 0x00;
39         ct36x_ts_reg_write(client, 0x7F, buf, 1);
40         mdelay(10);
41 }
42
43 static int ct36x_chip_get_busstatus(struct i2c_client *client, unsigned char *buf)
44 {
45         if ( CT36X_TS_CHIP_DEBUG )
46         printk(">>>>> %s() called <<<<< \n", __FUNCTION__);
47
48         ct36x_ts_reg_read(client, 0x7F, buf, 1);
49         mdelay(10);
50
51         return buf[0];
52 }
53
54 static int ct36x_chip_erase_flash(struct i2c_client *client, unsigned char *buf)
55 {
56         int ret = -1;
57
58         if ( CT36X_TS_CHIP_DEBUG )
59         printk(">>>>> %s() called <<<<< \n", __FUNCTION__);
60
61         // Erase 32k flash
62         buf[0] = 0x00;
63         buf[1] = 0x33;
64         buf[2] = 0x00;
65         ct36x_ts_reg_write(client, 0x7F, buf, 3);
66         mdelay(10);
67
68         // Reset I2C offset address
69         ct36x_chip_rst_offset(client, buf);
70
71         // Read I2C bus status
72         ret = ct36x_chip_get_busstatus(client, buf);
73         if ( ret != 0xAA ) {
74                 return -1;
75         }
76
77         return 0;
78 }
79
80 /*
81 ** Prepare code segment
82 */
83 static int ct36x_chip_set_code(unsigned int flash_addr, unsigned char *buf)
84 {
85         unsigned char cod_chksum;
86
87         //if ( CT36X_TS_CHIP_DEBUG )
88         //printk(">>>>> %s() called <<<<< \n", __FUNCTION__);
89
90         // Flash address
91         // data length
92         buf[2] = (char)(flash_addr >> 8);
93         buf[3] = (char)(flash_addr & 0xFF);
94         buf[4] = 0x08;
95
96         // Fill firmware source data
97         //if ( (sec == 1 && cod == 4) || (sec == 1 && cod == 5) ) {
98         //if ( flash_addr == (CT36X_CHIP_FLASH_SECTOR_SIZE + 32) || 
99         //flash_addr == (CT36X_CHIP_FLASH_SECTOR_SIZE + 40) ) {
100         if ( flash_addr == (160) || flash_addr == (168) ) {
101                 buf[6] = ~binary_data[flash_addr + 0];
102                 buf[7] = ~binary_data[flash_addr + 1];
103                 buf[8] = ~binary_data[flash_addr + 2];
104                 buf[9] = ~binary_data[flash_addr + 3];
105                 buf[10] = ~binary_data[flash_addr + 4];
106                 buf[11] = ~binary_data[flash_addr + 5];
107                 buf[12] = ~binary_data[flash_addr + 6];
108                 buf[13] = ~binary_data[flash_addr + 7];
109         } else {
110                 buf[6] = binary_data[flash_addr + 0];
111                 buf[7] = binary_data[flash_addr + 1];
112                 buf[8] = binary_data[flash_addr + 2];
113                 buf[9] = binary_data[flash_addr + 3];
114                 buf[10] = binary_data[flash_addr + 4];
115                 buf[11] = binary_data[flash_addr + 5];
116                 buf[12] = binary_data[flash_addr + 6];
117                 buf[13] = binary_data[flash_addr + 7];
118         }
119                         
120         /* Calculate a checksum by Host controller. 
121         ** Checksum =  ~(FLASH_ADRH+FLASH_ADRL+LENGTH+
122         ** Binary_Data1+Binary_Data2+Binary_Data3+Binary_Data4+
123         ** Binary_Data5+Binary_Data6+Binary_Data7+Binary_Data8) + 1
124         */
125         cod_chksum = ~(buf[2]+buf[3]+buf[4]+
126                                 buf[6]+buf[7]+buf[8]+buf[9]+
127                                 buf[10]+buf[11]+buf[12]+buf[13]) + 1;
128         buf[5] = cod_chksum;
129
130         return cod_chksum;
131 }
132
133 static int ct36x_chip_write_firmware(struct i2c_client *client, unsigned char *buf)
134 {
135         int ret = -1;
136         int sec, cod;
137         unsigned char cod_chksum;
138         unsigned int fin_chksum;
139         unsigned int flash_addr;
140
141         if ( CT36X_TS_CHIP_DEBUG )
142         printk(">>>>> %s() called <<<<< \n", __FUNCTION__);
143
144         // Code checksum, final checksum
145         cod_chksum = 0x00; fin_chksum = 0x00;
146
147         // Flash write command
148         buf[0] = 0x00;
149         buf[1] = 0x55;
150
151         // 256 sectors, 128 bytes per sectors
152         for ( sec = 0; sec < CT36X_CHIP_FLASH_SECTOR_NUM; sec++ ) {
153                 flash_addr = sec * CT36X_CHIP_FLASH_SECTOR_SIZE;
154                 // 16 segments, 8 bytes per segment
155                 for ( cod = 0; cod < (CT36X_CHIP_FLASH_SECTOR_SIZE/CT36X_CHIP_FLASH_SOURCE_SIZE); cod++ ) {
156                         // Fill binary data
157                         cod_chksum = ct36x_chip_set_code(flash_addr, buf);
158                         fin_chksum += cod_chksum;
159
160                         // Write firmware source data
161                         ct36x_ts_reg_write(client, 0x7F, buf, 14);
162
163                         // 
164                         mdelay(1);
165
166                         // Increase flash address 8bytes for each write command
167                         flash_addr += CT36X_CHIP_FLASH_SOURCE_SIZE;
168                 }
169                 //
170                 mdelay(20);
171         }
172
173         return 0;
174 }
175
176 static int ct36x_chip_read_infoblk(struct i2c_client *client, unsigned char *buf)
177 {
178         if ( CT36X_TS_CHIP_DEBUG )
179         printk(">>>>> %s() called <<<<< \n", __FUNCTION__);
180
181         return 0;
182 }
183
184 static int ct36x_chip_erase_infoblk(struct i2c_client *client, unsigned char *buf)
185 {
186         int ret = -1;
187
188         if ( CT36X_TS_CHIP_DEBUG )
189         printk(">>>>> %s() called <<<<< \n", __FUNCTION__);
190
191         // info block erase command
192         buf[0] = 0x00;
193         buf[1] = 0x60;
194         buf[2] = 0x00;
195         ct36x_ts_reg_write(client, 0x7F, buf, 3);
196         mdelay(10);
197
198         // Reset I2C offset address
199         ct36x_chip_rst_offset(client, buf);
200
201         // Read I2C bus status
202         ret = ct36x_chip_get_busstatus(client, buf);
203         if ( ret != 0xAA ) {
204                 printk("trim data erase error!!! \n");
205                 return -1;
206         }
207
208         return 0;
209 }
210
211 static int ct36x_chip_write_infoblk(struct i2c_client *client, unsigned char *buf)
212 {
213         //int ret = -1;
214         int sec, cod;
215         unsigned int flash_addr;
216
217         if ( CT36X_TS_CHIP_DEBUG )
218         printk(">>>>> %s() called <<<<< \n", __FUNCTION__);
219
220         flash_addr = 0x00;
221
222         // write info block 0
223         buf[0] = 0x00;
224         buf[1] = 0x61;
225
226         for ( cod = 0; cod < 16; cod++ ) {
227         // Flash address
228         // data length
229         buf[2] = (char)(flash_addr >> 8);
230         buf[3] = (char)(flash_addr & 0xFF);
231         buf[4] = 0x08;
232         if ( flash_addr == 0x0000 )
233         buf[6] = 0x17;
234         else
235         buf[6] = 0x00;
236         
237         buf[7] = 0x00;
238         buf[8] = 0x00;
239         buf[9] = 0x00;
240         buf[10] = 0x00;
241         buf[11] = 0x00;
242         buf[12] = 0x00;
243         buf[13] = 0x00;
244
245         buf[5] = (~(buf[2]+buf[3]+buf[4]+buf[6]+buf[7]+buf[8]+buf[9]+buf[10]+buf[11]+buf[12]+buf[13]))+1;
246                 
247         ct36x_ts_reg_write(client, 0x7F, buf, 14);
248         mdelay(10);
249
250         flash_addr += 8;
251         }
252
253         return 0;
254 }
255
256 int ct36x_chip_get_binchksum(unsigned char *buf)
257 {
258         int sec, cod;
259         unsigned char cod_chksum;
260         unsigned int fin_chksum = 0;
261         unsigned int flash_addr;
262
263         if ( CT36X_TS_CHIP_DEBUG )
264         printk(">>>>> %s() called <<<<< \n", __FUNCTION__);
265
266         // 256 sectors, 128 bytes per sectors
267         for ( sec = 0; sec < CT36X_CHIP_FLASH_SECTOR_NUM; sec++ ) {
268                 flash_addr = sec * CT36X_CHIP_FLASH_SECTOR_SIZE;
269                 // 16 segments, 8 bytes per segment
270                 for ( cod = 0; cod < (CT36X_CHIP_FLASH_SECTOR_SIZE/CT36X_CHIP_FLASH_SOURCE_SIZE); cod++ ) {
271                         // Fill binary data
272                         cod_chksum = ct36x_chip_set_code(flash_addr, buf);
273                         fin_chksum += cod_chksum;
274
275                         // Increase flash address 8bytes for each write command
276                         flash_addr += CT36X_CHIP_FLASH_SOURCE_SIZE;
277                 }
278         }
279
280         return (unsigned short)fin_chksum;
281 }
282
283 int ct36x_chip_get_fwchksum(struct i2c_client *client, unsigned char *buf)
284 {
285         int fwchksum = 0x00;
286
287         if ( CT36X_TS_CHIP_DEBUG )
288         printk(">>>>> %s() called <<<<< \n", __FUNCTION__);
289
290         buf[0] = 0xFF;
291         buf[1] = 0x8F;
292         buf[2] = 0xFF;
293         ct36x_ts_reg_write(client, client->addr, buf, 3);
294         mdelay(20);
295
296         buf[0] = 0x00;
297         buf[1] = 0xE1;
298         ct36x_ts_reg_write(client, client->addr, buf, 2);
299         mdelay(500);
300
301         buf[0] = 0xFF;
302         buf[1] = 0x8E;
303         buf[2] = 0x0E;
304         ct36x_ts_reg_write(client, client->addr, buf, 3);
305         mdelay(20);
306
307         ct36x_chip_rst_offset(client, buf);
308
309         ct36x_ts_reg_read(client, client->addr, buf, 3);
310         mdelay(20);
311
312         fwchksum = ((buf[0]<<8) | buf[1]);
313
314         return fwchksum;
315 }
316
317 int ct36x_chip_get_ver(struct i2c_client *client, unsigned char *buf)
318 {
319         if ( CT36X_TS_CHIP_DEBUG )
320         printk(">>>>> %s() called <<<<< \n", __FUNCTION__);
321
322         // Read version command
323         buf[0] = 0xFF;
324         buf[1] = 0x3F;
325         buf[2] = 0xFF;
326
327         ct36x_ts_reg_write(client, client->addr, buf, 3);
328         mdelay(10);
329
330         buf[0] = 0x00;
331         ct36x_ts_reg_write(client, client->addr, buf, 1);
332         mdelay(10);
333
334         // do read version
335         ct36x_ts_reg_read(client, client->addr, buf, 1);
336         mdelay(10);
337
338         return buf[0];
339 }
340
341 int ct36x_chip_get_vendor(struct i2c_client *client, unsigned char *buf)
342 {
343         if ( CT36X_TS_CHIP_DEBUG )
344         printk(">>>>> %s() called <<<<< \n", __FUNCTION__);
345
346         return 0;
347 }
348
349 void ct36x_chip_go_sleep(struct i2c_client *client, unsigned char *buf)
350 {
351         if ( CT36X_TS_CHIP_DEBUG )
352         printk(">>>>> %s() called <<<<< \n", __FUNCTION__);
353
354         buf[0] = 0xFF;
355         buf[1] = 0x8F;
356         buf[2] = 0xFF;
357         ct36x_ts_reg_write(client, client->addr, buf, 3);
358         mdelay(3);
359
360         buf[0] = 0x00;
361         buf[1] = 0xAF;
362         ct36x_ts_reg_write(client, client->addr, buf, 2);
363         mdelay(3);
364
365         //mdelay(50);
366 }
367
368 int ct36x_chip_go_bootloader(struct i2c_client *client, unsigned char *buf)
369 {
370         int ret = -1;
371
372         if ( CT36X_TS_CHIP_DEBUG )
373         printk(">>>>> %s() called <<<<< \n", __FUNCTION__);
374
375         // Init bootloader
376         ct36x_chip_set_idle(client, buf);
377
378         // Reset I2C offset address
379         ct36x_chip_rst_offset(client, buf);
380
381         // Get I2C bus status
382         ret = ct36x_chip_get_busstatus(client, buf);
383         if ( ret != 0xAA ) {
384                 printk("I2C bus status: 0x%x.\n", ret);
385                 return -1;
386         }
387
388         // trim adc
389         ct36x_chip_read_infoblk(client, buf);
390         ct36x_chip_erase_infoblk(client, buf);
391         ct36x_chip_write_infoblk(client, buf);
392
393         // Erase flash
394         //ret = ct36x_chip_erase_flash(client, buf);
395         //if ( ret ) {
396         //      printk("Erase flash failed.\n");
397         //      return -1;
398         //}
399
400         // Write source data
401         //ct36x_chip_write_firmware(client, buf);
402         
403         return 0;
404 }
405
406 void ct36x_chip_set_adapter_on(struct i2c_client *client, unsigned char *buf)
407 {
408         if ( CT36X_TS_CHIP_DEBUG )
409         printk(">>>>> %s() called <<<<< \n", __FUNCTION__);
410
411         buf[0] = 0xFF;
412         buf[1] = 0x0F;
413         buf[2] = 0xFF;
414         ct36x_ts_reg_write(client, client->addr, buf, 3);
415         mdelay(3);
416
417         buf[0] = 0x00;
418         buf[1] = 0xE3;
419         ct36x_ts_reg_write(client, client->addr, buf, 2);
420         mdelay(3);
421 }
422
423 void ct36x_chip_set_adapter_off(struct i2c_client *client, unsigned char *buf)
424 {
425         if ( CT36X_TS_CHIP_DEBUG )
426         printk(">>>>> %s() called <<<<< \n", __FUNCTION__);
427
428         buf[0] = 0xFF;
429         buf[1] = 0x0F;
430         buf[2] = 0xFF;
431         ct36x_ts_reg_write(client, client->addr, buf, 3);
432         mdelay(3);
433
434         buf[0] = 0x00;
435         buf[1] = 0xE2;
436         ct36x_ts_reg_write(client, client->addr, buf, 2);
437         mdelay(3);
438 }
439