aboutsummaryrefslogtreecommitdiff
path: root/drivers/input/touchscreen/mediatek/MZ_FT5346/focaltech_ex_fun.c
blob: 3a78071c1d65f57583b913db47895df66d20f910 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
1539
1540
1541
1542
1543
1544
1545
1546
1547
1548
1549
1550
1551
1552
1553
1554
1555
1556
1557
1558
1559
1560
1561
1562
1563
1564
1565
1566
1567
1568
1569
1570
1571
1572
1573
1574
1575
1576
1577
1578
1579
1580
1581
1582
1583
1584
1585
1586
1587
1588
1589
1590
1591
1592
1593
1594
1595
1596
1597
1598
1599
1600
1601
1602
1603
1604
1605
1606
1607
1608
1609
1610
1611
1612
1613
1614
1615
1616
1617
1618
1619
1620
1621
1622
1623
1624
1625
1626
1627
1628
1629
1630
1631
1632
1633
1634
1635
1636
1637
1638
1639
1640
1641
1642
1643
1644
1645
1646
1647
1648
1649
1650
1651
1652
1653
1654
1655
1656
1657
1658
1659
1660
1661
1662
1663
1664
1665
1666
1667
1668
1669
1670
1671
1672
1673
1674
1675
1676
1677
1678
1679
1680
1681
1682
1683
1684
1685
1686
1687
1688
1689
1690
1691
1692
1693
1694
1695
1696
1697
1698
1699
1700
1701
1702
1703
1704
1705
1706
1707
1708
1709
1710
1711
1712
1713
1714
1715
1716
1717
1718
1719
1720
1721
1722
1723
1724
1725
1726
1727
1728
1729
1730
1731
1732
1733
1734
1735
1736
1737
1738
1739
1740
1741
1742
1743
1744
1745
1746
1747
1748
1749
1750
1751
1752
1753
1754
1755
1756
1757
1758
1759
1760
1761
1762
1763
1764
1765
1766
1767
1768
1769
1770
1771
1772
1773
1774
1775
1776
1777
1778
1779
1780
1781
1782
1783
1784
1785
1786
1787
1788
1789
1790
1791
1792
1793
1794
1795
1796
1797
1798
1799
1800
1801
1802
1803
1804
1805
1806
1807
1808
1809
1810
1811
1812
1813
1814
1815
1816
1817
1818
1819
1820
1821
1822
1823
1824
1825
1826
1827
1828
1829
1830
1831
1832
1833
1834
1835
1836
1837
1838
1839
1840
1841
1842
1843
1844
1845
1846
1847
1848
1849
1850
1851
1852
1853
1854
1855
1856
1857
1858
1859
1860
1861
1862
1863
1864
1865
1866
1867
1868
1869
1870
1871
1872
1873
1874
1875
1876
1877
1878
1879
1880
1881
1882
1883
1884
1885
1886
1887
1888
1889
1890
1891
1892
1893
1894
1895
1896
1897
1898
1899
1900
1901
1902
1903
1904
1905
1906
1907
1908
1909
1910
1911
1912
1913
1914
1915
1916
1917
1918
1919
1920
1921
1922
1923
1924
1925
1926
1927
1928
1929
1930
1931
1932
1933
1934
1935
1936
1937
1938
1939
1940
1941
1942
1943
1944
1945
1946
1947
1948
1949
1950
1951
1952
1953
1954
1955
1956
1957
1958
1959
1960
1961
1962
1963
1964
1965
1966
1967
1968
1969
1970
1971
1972
1973
1974
1975
1976
1977
1978
1979
1980
1981
1982
1983
1984
1985
1986
1987
1988
1989
1990
1991
1992
1993
1994
1995
1996
1997
1998
1999
2000
2001
2002
2003
2004
2005
2006
2007
2008
2009
2010
2011
2012
2013
2014
2015
2016
2017
2018
2019
2020
2021
2022
2023
2024
2025
2026
2027
2028
2029
2030
2031
2032
2033
2034
2035
2036
2037
2038
2039
2040
2041
2042
2043
2044
2045
2046
2047
2048
2049
2050
2051
2052
2053
2054
2055
2056
2057
2058
2059
2060
2061
2062
2063
2064
2065
2066
2067
2068
2069
2070
2071
2072
2073
2074
2075
2076
2077
2078
2079
2080
2081
2082
2083
2084
2085
2086
2087
2088
2089
2090
2091
2092
2093
2094
2095
2096
2097
2098
2099
2100
2101
2102
2103
2104
2105
2106
2107
2108
2109
2110
2111
2112
2113
2114
2115
2116
2117
2118
2119
2120
2121
2122
2123
2124
2125
2126
2127
2128
2129
2130
2131
2132
2133
2134
2135
2136
2137
2138
2139
2140
2141
2142
2143
2144
2145
2146
2147
2148
2149
2150
2151
2152
2153
2154
2155
2156
2157
2158
2159
2160
2161
2162
2163
2164
2165
2166
2167
2168
2169
2170
2171
2172
2173
2174
2175
2176
2177
2178
2179
2180
2181
2182
2183
2184
2185
2186
2187
2188
2189
2190
2191
2192
2193
2194
2195
2196
2197
2198
2199
2200
/*
 *drivers/input/touchscreen/ft5x06_ex_fun.c
 *
 *FocalTech IC driver expand function for debug.
 *
 *Copyright (c) 2010  Focal tech Ltd.
 *
 *This software is licensed under the terms of the GNU General Public
 *License version 2, as published by the Free Software Foundation, and
 *may be copied, distributed, and modified under those terms.
 *
 *This program is distributed in the hope that it will be useful,
 *but WITHOUT ANY WARRANTY; without even the implied warranty of
 *MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *GNU General Public License for more details.
 *
 *Note:the error code of EIO is the general error in this file.
 */

#include "tpd.h"

#include "tpd_custom_fts.h"

#include "focaltech_ex_fun.h"

#include <linux/netdevice.h>
#include <linux/mount.h>
//#include <linux/netdevice.h>
#include <linux/proc_fs.h>

#ifdef FTS_MCAP_TEST
#include "self-test/mcap_5x46_test_lib.h"
#include "mcap_5x46_test_config.h"

extern struct tpd_device *tpd;
extern struct i2c_client *g_focalclient;
#endif
/*******************************************************************************/
#define VENDOR_ID_DEFAULT               0x79
#define VENDOR_ID_BROKEN                0x00
#define VENDOR_ID_OFILM                 0x51
#define LCM_ID_AUO_B			0x12
#define LCM_ID_AUO_W			0x11
#define LCM_ID_SHARP_B			0x22
#define LCM_ID_SHARP_W			0x21

/*auto upgrade*/
struct CtpInfo
{
	u8  vendor_id;
	u8  lcm_id;
	u8  fw_version;
};

struct CtpFwInfo
{
	u8  vendor_id;
	u8  lcm_id;
	u8  *fw_data;
	u32 fw_length;
};

static struct CtpInfo cur_tp_info;

//upgrade app
static unsigned char aucFW_APP_SHP_B[] =
{
#include "FT5346_V22_M81_OFILM_SHP_B_20150701_app.i"
};


static unsigned char aucFW_APP_AUO_B[] =
{
#include "FT5346_V27_M81_OFILM_AUO_B_20150831_app.i"
};

struct CtpFwInfo ctp_fw_app[] =
{
	/*sharp*/
	{0x51, 0x12, aucFW_APP_SHP_B, sizeof(aucFW_APP_SHP_B)},
	/*auo*/
	{0x51, 0x22, aucFW_APP_AUO_B, sizeof(aucFW_APP_AUO_B)},
};

/*******************************************************************************/
u8 *I2CDMABuf_va = NULL;
u64 I2CDMABuf_pa = NULL;

extern struct Upgrade_Info fts_updateinfo_curr;

/*******************************************************************************/
int fts_ctpm_fw_upgrade(struct i2c_client *client, u8 *pbt_buf, u32 dw_lenth);

extern void mt_eint_unmask(unsigned int line);
extern void mt_eint_mask(unsigned int line);


static DEFINE_MUTEX(g_device_mutex);

#ifdef TPD_AUTO_DOWNLOAD
int  fts_ctpm_fw_preupgrade_hwreset(struct i2c_client * client, u8* pbt_buf, u32 dw_lenth);
#endif

/*******************************************************************************/
int fts_hidi2c_to_stdi2c(struct i2c_client * client)
{
	u8 auc_i2c_write_buf[10] = {0};
	u8 reg_val[10] = {0};
	int iRet = 0;

	auc_i2c_write_buf[0] = 0xEB;
	auc_i2c_write_buf[1] = 0xAA;
	auc_i2c_write_buf[2] = 0x09;

	reg_val[0] = reg_val[1] =  reg_val[2] = 0x00;

	iRet = fts_i2c_Write(client, auc_i2c_write_buf, 3);

	msleep(10);
	iRet = fts_i2c_Read(client, auc_i2c_write_buf, 0, reg_val, 3);

	FTS_DBG("Change to STDI2cValue,REG1 = 0x%x,REG2 = 0x%x,REG3 = 0x%x, iRet=%d\n",
	    reg_val[0], reg_val[1], reg_val[2], iRet);

	if (reg_val[0] == 0xEB
	        && reg_val[1] == 0xAA
	        && reg_val[2] == 0x08)
	{
		pr_info("fts_hidi2c_to_stdi2c successful.\n");
		iRet = 1;
	}
	else
	{
		pr_info("fts_hidi2c_to_stdi2c error.\n");
		iRet = 0;
	}

	return iRet;
}

DEFINE_MUTEX(g_ft_i2c_lock);

/*
*fts_i2c_Read-read data and write data by i2c
*@client: handle of i2c
*@writebuf: Data that will be written to the slave
*@writelen: How many bytes to write
*@readbuf: Where to store data read from slave
*@readlen: How many bytes to read
*
*Returns negative errno, else the number of messages executed
*
*
*/
int fts_i2c_Read(struct i2c_client *client, u8 *writebuf,
                 int writelen, u8 *readbuf, int readlen)
{
	int ret,i;

	mutex_lock(&g_ft_i2c_lock);
	if(writelen > 0)
	{
		//DMA Write
		{
			for(i = 0 ; i < writelen; i++)
			{
				I2CDMABuf_va[i] = writebuf[i];
			}

			client->addr = client->addr & I2C_MASK_FLAG | I2C_DMA_FLAG;

			if((ret=i2c_master_send(client, I2CDMABuf_pa, writelen))!=writelen)
				FTS_DBG( "###%s i2c write len=%x\n", __func__,ret);
			//MSE_ERR("Sensor dma timing is %x!\r\n", this_client->timing);
			//return ret;
			client->addr = client->addr & I2C_MASK_FLAG &(~ I2C_DMA_FLAG);

		}
	}
	//DMA Read
	if(readlen > 0)
	{
		{

			client->addr = client->addr & I2C_MASK_FLAG | I2C_DMA_FLAG;
			ret = i2c_master_recv(client, I2CDMABuf_pa, readlen);

			for(i = 0; i < readlen; i++)
			{
				readbuf[i] = I2CDMABuf_va[i];
			}
			client->addr = client->addr & I2C_MASK_FLAG &(~ I2C_DMA_FLAG);

		}
	}
	mutex_unlock(&g_ft_i2c_lock);
	return ret;
}
/*write data by i2c*/

int fts_i2c_Write(struct i2c_client *client, u8 *writebuf, int writelen)
{
	int ret;
	int i = 0;

	mutex_lock(&g_ft_i2c_lock);
	if (writelen > 0)
	{
		for(i = 0 ; i < writelen; i++)
		{
			I2CDMABuf_va[i] = writebuf[i];
		}

		client->addr = client->addr & I2C_MASK_FLAG | I2C_DMA_FLAG;

		if((ret=i2c_master_send(client, I2CDMABuf_pa, writelen))!=writelen)
		{
			FTS_DBG( "###%s i2c write len=%x\n", __func__,ret);
		}
		//MSE_ERR("Sensor dma timing is %x!\r\n", this_client->timing);
		client->addr = client->addr & I2C_MASK_FLAG &(~ I2C_DMA_FLAG);

	}
	mutex_unlock(&g_ft_i2c_lock);
	return ret;

}

void fts_tp_power_reset(void)
{
#ifdef TPD_POWER_SOURCE_CUSTOM
	hwPowerDown(TPD_POWER_SOURCE_CUSTOM, "TP");
#else
	hwPowerDown(TPD_POWER_SOURCE, "TP");
#endif

	mdelay(5);
#ifdef TPD_POWER_SOURCE_CUSTOM
	hwPowerOn(TPD_POWER_SOURCE_CUSTOM, VOL_2800, "TP");
#else
	hwPowerOn(TPD_POWER_SOURCE, VOL_3300, "TP");
#endif

	mdelay(300);
}

#ifdef FTS_MCAP_TEST
int focal_i2c_Read(unsigned char *writebuf,
		    int writelen, unsigned char *readbuf, int readlen)
{
	int ret;
	int i = 0;

	mutex_lock(&g_ft_i2c_lock);
	if (writelen > 0) {
		{
			for(i = 0 ; i < writelen; i++)
			{
				I2CDMABuf_va[i] = writebuf[i];
			}

			g_focalclient->addr = g_focalclient->addr & I2C_MASK_FLAG | I2C_DMA_FLAG;

			if((ret=i2c_master_send(g_focalclient, I2CDMABuf_pa, writelen))!=writelen)
				FTS_DBG( "###%s i2c write len=%x\n", __func__,ret);
			//MSE_ERR("Sensor dma timing is %x!\r\n", this_client->timing);
			//return ret;
			g_focalclient->addr = g_focalclient->addr & I2C_MASK_FLAG &(~ I2C_DMA_FLAG);

		}
			
	} 
	
	if(readlen > 0){
		{

			g_focalclient->addr = g_focalclient->addr & I2C_MASK_FLAG | I2C_DMA_FLAG;
			ret = i2c_master_recv(g_focalclient, I2CDMABuf_pa, readlen);

			for(i = 0; i < readlen; i++)
			{
				readbuf[i] = I2CDMABuf_va[i];
			}
			g_focalclient->addr = g_focalclient->addr & I2C_MASK_FLAG &(~ I2C_DMA_FLAG);

		}
	}
	mutex_unlock(&g_ft_i2c_lock);
	return ret;
}
/*write data by i2c*/
int focal_i2c_Write(unsigned char *writebuf, int writelen)
{
	int ret;
	int i = 0;

	mutex_lock(&g_ft_i2c_lock);
	if (writelen > 0)
	{
		for(i = 0 ; i < writelen; i++)
		{
			I2CDMABuf_va[i] = writebuf[i];
		}

		g_focalclient->addr = g_focalclient->addr & I2C_MASK_FLAG | I2C_DMA_FLAG;

		if((ret=i2c_master_send(g_focalclient, I2CDMABuf_pa, writelen))!=writelen)
		{
			FTS_DBG( "###%s i2c write err\n", __func__);
		}
		//MSE_ERR("Sensor dma timing is %x!\r\n", this_client->timing);
		g_focalclient->addr = g_focalclient->addr & I2C_MASK_FLAG &(~ I2C_DMA_FLAG);

	}
	mutex_unlock(&g_ft_i2c_lock);
	return ret;
}
#endif

int fts_write_reg(struct i2c_client *client, u8 regaddr, u8 regvalue)
{
	unsigned char buf[2] = {0};
	buf[0] = regaddr;
	buf[1] = regvalue;

	return fts_i2c_Write(client, buf, sizeof(buf));
}


int fts_read_reg(struct i2c_client *client, u8 regaddr, u8 *regvalue)
{
	return fts_i2c_Read(client, &regaddr, 1, regvalue, 1);
}

void fts_ctpm_hw_reset(void)
{
	mt_set_gpio_mode(GPIO_CTP_RST_PIN, GPIO_CTP_RST_PIN_M_GPIO);
	mt_set_gpio_dir(GPIO_CTP_RST_PIN, GPIO_DIR_OUT);
	mt_set_gpio_out(GPIO_CTP_RST_PIN, GPIO_OUT_ONE);
	mdelay(20);
	mt_set_gpio_out(GPIO_CTP_RST_PIN, GPIO_OUT_ZERO);
	mdelay(20);
	mt_set_gpio_out(GPIO_CTP_RST_PIN, GPIO_OUT_ONE);
}

int fts_ctpm_sw_reset(struct i2c_client * client)
{
	int i_ret;

	i_ret = fts_write_reg(client, 0xfc, FT_UPGRADE_AA);

	if (i_ret > 0)
	{
		msleep(fts_updateinfo_curr.delay_aa);

		i_ret = fts_write_reg(client, 0xfc, FT_UPGRADE_55);
	}
	
	return i_ret;
}

int fts_ctpm_auto_clb(struct i2c_client *client)
{
	unsigned char uc_temp = 0x00;
	unsigned char i = 0;

	/*start auto CLB */
	msleep(200);

	fts_write_reg(client, 0, FTS_FACTORYMODE_VALUE);
	/*make sure already enter factory mode */
	msleep(100);
	/*write command to start calibration */
	fts_write_reg(client, 2, 0x4);
	msleep(300);
	if ((fts_updateinfo_curr.CHIP_ID == 0x11)
	        || (fts_updateinfo_curr.CHIP_ID == 0x12)
	        || (fts_updateinfo_curr.CHIP_ID == 0x13)
	        || (fts_updateinfo_curr.CHIP_ID == 0x14)) //5x36,5x36i
	{
		for(i=0; i<100; i++)
		{
			fts_read_reg(client, 0x02, &uc_temp);
			if (0x02 == uc_temp ||
			        0xFF == uc_temp)
			{
				/*if 0x02, then auto clb ok, else 0xff, auto clb failure*/
				break;
			}
			msleep(20);
		}
	}
	else
	{
		for(i=0; i<100; i++)
		{
			fts_read_reg(client, 0, &uc_temp);
			if (0x0 == ((uc_temp&0x70)>>4))  /*return to normal mode, calibration finish*/
			{
				break;
			}
			msleep(20);
		}
	}
	/*calibration OK*/
	fts_write_reg(client, 0, 0x40);  /*goto factory mode for store*/
	msleep(200);   /*make sure already enter factory mode*/
	fts_write_reg(client, 2, 0x5);  /*store CLB result*/
	msleep(300);
	fts_write_reg(client, 0, FTS_WORKMODE_VALUE);	/*return to normal mode */
	msleep(300);

	/*store CLB result OK */
	return 0;
}

/*
get app.i fw version
*/
u8 fts_ctpm_get_fw_version(u8 vendor_id, u8 lcm_id)
{
	int i = 0;
	int fw_cnt = sizeof(ctp_fw_app)/sizeof(struct CtpFwInfo);

	for (i = 0; i < fw_cnt; i++)
	{
		if ((vendor_id == ctp_fw_app[i].vendor_id)
			&& (lcm_id == ctp_fw_app[i].lcm_id)
		    && (NULL != ctp_fw_app[i].fw_data)
		    && (2 < ctp_fw_app[i].fw_length))
		{
			return ctp_fw_app[i].fw_data[ctp_fw_app[i].fw_length - 2];
		}
	}

	return 0x00;	/*default value */
}

int fts_ctpm_get_fw_data(u8 vendor_id, u8 lcm_id, u8 **fw_data, u32 *fw_length)
{
	int i = 0;
	int fw_cnt = sizeof(ctp_fw_app)/sizeof(struct CtpFwInfo);

	if ((fw_data == NULL) || (fw_length == NULL))
	{
		return -EIO;
	}

	for (i = 0; i < fw_cnt; i++)
	{
		if ((vendor_id == ctp_fw_app[i].vendor_id)
			&& (lcm_id == ctp_fw_app[i].lcm_id))
		{
			*fw_data = ctp_fw_app[i].fw_data;
			*fw_length = ctp_fw_app[i].fw_length;

			return 0;
		}
	}

	return -EIO;
}

int fts_ctpm_read_ctp_info_from_app(struct i2c_client *client,
		struct CtpInfo *ctp_info)

{
	int i_ret = 0;

	i_ret = fts_read_reg(client, FT_REG_FW_VER, &(ctp_info->fw_version));
	if (i_ret < 0)
		pr_err("rad FT_REG_FW_VER failed, ret: %d value: %x\n",
				i_ret, ctp_info->fw_version);

	i_ret = fts_read_reg(client, FT_REG_VENDOR_ID, &(ctp_info->vendor_id));
	if (i_ret < 0)
		pr_err("rad FT_REG_VENDOR_ID failed, ret: %d value: %x\n",
				i_ret, ctp_info->vendor_id);

	i_ret = fts_read_reg(client, FT_REG_LCM_ID, &(ctp_info->lcm_id));
	if (i_ret < 0)
		pr_err("read FT_REG_LCM_ID failed, ret: %d value: %x\n",
				i_ret, ctp_info->lcm_id);

	return i_ret;
}

int  fts_ctpm_read_ctp_info_from_flash(struct i2c_client * client,
		struct CtpInfo* ctp_info)
{
	u8 reg_val[4] = {0};
	u8 auc_i2c_write_buf[10] = {0};
	int i = 0, i_ret = 0, is_need_reset = 0;

	do
	{
			
		if (ctp_info == NULL)
		{
			return -EIO;
		}

		i_ret = fts_hidi2c_to_stdi2c(client);

		for (i = 0; i < FTS_UPGRADE_LOOP; i++)
		{
			/*********Step 1:Reset  CTPM *****/
			FTS_DBG("[FTS] Step 1:Reset  CTPM\n");
			
#if 0//def TPD_HW_REST
			fts_ctpm_hw_reset();
#else
			/*write 0xaa to register 0xfc */
			fts_write_reg(client, 0xfc, FT_UPGRADE_AA);
			msleep(fts_updateinfo_curr.delay_aa);

			/*write 0x55 to register 0xfc */
			fts_write_reg(client, 0xfc, FT_UPGRADE_55);
#endif

			if (i<=15)
			{
				msleep(fts_updateinfo_curr.delay_55+i*3);
			}
			else
			{
				msleep(fts_updateinfo_curr.delay_55-(i-15)*2);
			}

			/*********Step 2:Enter upgrade mode *****/
			FTS_DBG("[FTS] Step 2:Enter upgrade mode \n");

			i_ret = fts_hidi2c_to_stdi2c(client);
			msleep(5);

			auc_i2c_write_buf[0] = FT_UPGRADE_55;
			fts_i2c_Write(client, auc_i2c_write_buf, 1);
			msleep(5);
			auc_i2c_write_buf[0] = FT_UPGRADE_AA;
			fts_i2c_Write(client, auc_i2c_write_buf, 1);

			/*********Step 3:check READ-ID***********************/
			msleep(fts_updateinfo_curr.delay_readid);
			auc_i2c_write_buf[0] = 0x90;
			auc_i2c_write_buf[1] = auc_i2c_write_buf[2] = auc_i2c_write_buf[3] =0x00;
			fts_i2c_Read(client, auc_i2c_write_buf, 4, reg_val, 2);

			pr_warn("[FTS] Step 3: CTPM ID,ID1 = 0x%x,ID2 = 0x%x\n",reg_val[0], reg_val[1]);
			if (reg_val[0] == fts_updateinfo_curr.upgrade_id_1
			        && reg_val[1] != 0)
			{
				FTS_DBG("[FTS] Step 3: CTPM ID,ID1 = 0x%x,ID2 = 0x%x\n",
				    reg_val[0], reg_val[1]);
				break;
			}
			else
			{
				FTS_DBG( "[FTS] Step 3: CTPM ID,ID1 = 0x%x,ID2 = 0x%x\n",
				         reg_val[0], reg_val[1]);

				continue;
			}
		}

		if (i >= FTS_UPGRADE_LOOP )
		{
			if ((reg_val[0] != 0x00) || (reg_val[1] != 0x00))
			{
				FTS_DBG( "[FTS] Step 3: CTPM ID error, break.\n");
				is_need_reset = 1;
			}
			break;
		}

		/*********Step 4: read vendor id from app param area***********************/
		FTS_DBG("[FTS] Step 4: read vendor id from flash\n");
		msleep(10);

		auc_i2c_write_buf[0] = 0x03;
		auc_i2c_write_buf[1] = 0x00;

		for (i = 0; i < FTS_UPGRADE_LOOP; i++)
		{	
			auc_i2c_write_buf[2] = 0xd7;
			auc_i2c_write_buf[3] = 0x86;
			reg_val[0] = reg_val[1] = 0x00;
			
			i_ret = fts_i2c_Write(client, auc_i2c_write_buf, 4);
			if (i_ret < 0)
			{
				pr_err( "[FTS] Step 4: read lcm id from flash error when i2c write, i_ret = %d\n", i_ret);
				continue;
			}
			
			msleep(5);
			
			i_ret = fts_i2c_Read(client, auc_i2c_write_buf, 0, reg_val, 2);
			if (i_ret < 0)
			{
				pr_err( "[FTS] Step 4: read lcm id from flash error when i2c read, i_ret = %d\n", i_ret);
				continue;
			}

			ctp_info->lcm_id = reg_val[0];
			pr_warn("[FTS] Step 4: lcm id = 0x%x\n", ctp_info->lcm_id);

			auc_i2c_write_buf[2] = 0xd7;
			auc_i2c_write_buf[3] = 0x84;
			reg_val[0] = reg_val[1] = 0x00;
			
			i_ret = fts_i2c_Write(client, auc_i2c_write_buf, 4);
			if (i_ret < 0)
			{
				pr_err( "[FTS] Step 4: read vendor id from flash error when i2c write, i_ret = %d\n", i_ret);
				continue;
			}
			
			msleep(5);
			
			i_ret = fts_i2c_Read(client, auc_i2c_write_buf, 0, reg_val, 2);
			if (i_ret < 0)
			{
				pr_err( "[FTS] Step 4: read vendor id from flash error when i2c read, i_ret = %d\n", i_ret);
				continue;
			}
			
			ctp_info->vendor_id = reg_val[0];

			pr_warn("[FTS] Step 4: vendor id = 0x%x\n", ctp_info->vendor_id);
			break;
		}
		
		msleep(50);

		is_need_reset = 1;
		
	}while(0);

	/*********Step 5: reset the new FW***********************/
	if (is_need_reset)
	{
		pr_warn("Step 5: reset the FW\n");
		fts_ctpm_hw_reset();
		msleep(200);   //make sure CTP startup normally

		i_ret = fts_hidi2c_to_stdi2c(client);//Android to Std i2c.
		if(i_ret == 0)
		{
			FTS_DBG("HidI2c change to StdI2c i_ret = %d ! \n", i_ret);
		}

		msleep(10);
	}
	
	if (i == FTS_UPGRADE_LOOP)
	{
		return -EIO;
	}

	return 0;
}

int fts_ctpm_fw_upgrade(struct i2c_client *client, u8 *pbt_buf, u32 dw_lenth)
{
	u8 reg_val[4] = {0};
	u32 i = 0;
	u8 is_5336_new_bootloader = 0;
	u8 is_5336_fwsize_30 = 0;
	u32 packet_number;
	u32 j=0;
	u32 temp;
	u32 lenght;
	u8 packet_buf[FTS_PACKET_LENGTH + 6];
	u8 auc_i2c_write_buf[10];
	u8 bt_ecc;
	int i_ret;
	int reflash_flag = 0;
	u8 uc_tp_fm_ver;

ft_fw_reflash_1:
	i_ret = fts_hidi2c_to_stdi2c(client);

	for (i = 0; i < FTS_UPGRADE_LOOP; i++)
	{
		msleep(100);

		pr_warn("[FTS] Upgrade Step 1:Reset  CTPM\n");

		/*********Step 1:Reset  CTPM *****/
		/*write 0xaa to register 0xfc */
		i_ret = fts_write_reg(client, 0xfc, FT_UPGRADE_AA);
		msleep(fts_updateinfo_curr.delay_aa);

		/*write 0x55 to register 0xfc */
		i_ret |= fts_write_reg(client, 0xfc, FT_UPGRADE_55);

		if(i_ret < 0) {
			pr_err("[FTS] Upgrade Step 1 reset i2c transfer error\n");
		}

		if (i<=FTS_UPGRADE_LOOP/2)
		{
			msleep(fts_updateinfo_curr.delay_55+i*3);
		}
		else
		{
			msleep(fts_updateinfo_curr.delay_55-(i-15)*2);
		}

		/*********Step 2:Enter upgrade mode *****/
		pr_warn("[FTS] Upgrade Step 2:Enter upgrade mode \n");

		i_ret = fts_hidi2c_to_stdi2c(client);


		auc_i2c_write_buf[0] = FT_UPGRADE_55;
		i_ret = fts_i2c_Write(client, auc_i2c_write_buf, 1);
		msleep(5);
		auc_i2c_write_buf[0] = FT_UPGRADE_AA;
		i_ret = fts_i2c_Write(client, auc_i2c_write_buf, 1);

		if(i_ret < 0) {
			pr_err("[FTS] Upgrade Step 1 reset i2c transfer error\n");
		}

		/*********Step 3:check READ-ID***********************/
		msleep(fts_updateinfo_curr.delay_readid);
		auc_i2c_write_buf[0] = 0x90;
		auc_i2c_write_buf[1] = auc_i2c_write_buf[2] = auc_i2c_write_buf[3] =0x00;
		fts_i2c_Read(client, auc_i2c_write_buf, 4, reg_val, 2);

		pr_warn("[FTS] Upgrade Step 3: CTPM ID,ID1 = 0x%x,ID2 = 0x%x\n",reg_val[0], reg_val[1]);
		if (reg_val[0] == fts_updateinfo_curr.upgrade_id_1
		        && reg_val[1] == fts_updateinfo_curr.upgrade_id_2)
		{
			pr_err("[FTS] Step 3: CTPM ID,ID1 = 0x%x,ID2 = 0x%x\n",
			    reg_val[0], reg_val[1]);
			break;
		}
		else
		{
			pr_err("[FTS] Step 3: CTPM ID,ID1 = 0x%x,ID2 = 0x%x\n",
			        reg_val[0], reg_val[1]);
			continue;
		}
	}

	if (i >= FTS_UPGRADE_LOOP) {
		return -EIO;
	}

ft_fw_reflash_4:
	pr_warn("[FTS] Step 4:erase app and panel paramenter area\n");
	/*Step 4:erase app and panel paramenter area*/
	pr_warn("Step 4:erase app and panel paramenter area\n");
	auc_i2c_write_buf[0] = 0x61;
	i_ret = fts_i2c_Write(client, auc_i2c_write_buf, 1);	/*erase app area */
	msleep(fts_updateinfo_curr.delay_earse_flash);
	/*erase panel parameter area */

	for(i = 0; i < 15; i++)
	{
		auc_i2c_write_buf[0] = 0x6a;
		reg_val[0] = reg_val[1] = 0x00;
		i_ret |= fts_i2c_Read(client, auc_i2c_write_buf, 1, reg_val, 2);

		if(0xF0==reg_val[0] && 0xAA==reg_val[1])
		{
			break;
		}
		msleep(50);
	}

	//write bin file length to FW bootloader.
	auc_i2c_write_buf[0] = 0xB0;
	auc_i2c_write_buf[1] = (u8) ((dw_lenth >> 16) & 0xFF);
	auc_i2c_write_buf[2] = (u8) ((dw_lenth >> 8) & 0xFF);
	auc_i2c_write_buf[3] = (u8) (dw_lenth & 0xFF);

	i_ret |= fts_i2c_Write(client, auc_i2c_write_buf, 4);

	if(i_ret < 0) {
		pr_err("[FTS] Upgrade Step 4 i2c transfer error\n");
	}

	pr_warn("[FTS] Step 5:write firmware(FW) to ctpm flash\n");
	/*********Step 5:write firmware(FW) to ctpm flash*********/
	bt_ecc = 0;
	pr_warn("Step 5:write firmware(FW) to ctpm flash\n");

	//dw_lenth = dw_lenth - 8;
	temp = 0;
	packet_number = (dw_lenth) / FTS_PACKET_LENGTH;
	packet_buf[0] = 0xbf;
	packet_buf[1] = 0x00;
	i_ret = 0;

	for (j = 0; j < packet_number; j++)
	{
		temp = j * FTS_PACKET_LENGTH;
		packet_buf[2] = (u8) (temp >> 8);
		packet_buf[3] = (u8) temp;
		lenght = FTS_PACKET_LENGTH;
		packet_buf[4] = (u8) (lenght >> 8);
		packet_buf[5] = (u8) lenght;

		for (i = 0; i < FTS_PACKET_LENGTH; i++)
		{
			packet_buf[6 + i] = pbt_buf[j * FTS_PACKET_LENGTH + i];
			bt_ecc ^= packet_buf[6 + i];
		}
		i_ret |= fts_i2c_Write(client, packet_buf, FTS_PACKET_LENGTH + 6);
		//msleep(10);

		for(i = 0; i < 30; i++)
		{
			auc_i2c_write_buf[0] = 0x6a;
			reg_val[0] = reg_val[1] = 0x00;
			i_ret |= fts_i2c_Read(client, auc_i2c_write_buf, 1, reg_val, 2);

			if ((j + 0x1000) == (((reg_val[0]) << 8) | reg_val[1]))
			{
				break;
			}
			msleep(1);

		}
	}

	if ((dw_lenth) % FTS_PACKET_LENGTH > 0)
	{
		temp = packet_number * FTS_PACKET_LENGTH;
		packet_buf[2] = (u8) (temp >> 8);
		packet_buf[3] = (u8) temp;
		temp = (dw_lenth) % FTS_PACKET_LENGTH;
		packet_buf[4] = (u8) (temp >> 8);
		packet_buf[5] = (u8) temp;

		for (i = 0; i < temp; i++)
		{
			packet_buf[6 + i] = pbt_buf[packet_number * FTS_PACKET_LENGTH + i];
			bt_ecc ^= packet_buf[6 + i];
		}
		i_ret |= fts_i2c_Write(client, packet_buf, temp + 6);

		for(i = 0; i < 30; i++)
		{
			auc_i2c_write_buf[0] = 0x6a;
			reg_val[0] = reg_val[1] = 0x00;
			i_ret |= fts_i2c_Read(client, auc_i2c_write_buf, 1, reg_val, 2);

			if ((j + 0x1000) == (((reg_val[0]) << 8) | reg_val[1]))
			{
				break;
			}
			msleep(1);

		}
	}

	if(i_ret < 0) {
		pr_err("[FTS] Upgrade Step 5 i2c transfer error\n");
	}
	msleep(50);


	pr_warn("[FTS] Upgrade Step 6: read out checksum\n");
	/*********Step 6: read out checksum***********************/
	/*send the opration head */
	auc_i2c_write_buf[0] = 0x64;
	fts_i2c_Write(client, auc_i2c_write_buf, 1);
	msleep(300);

	temp = 0;
	auc_i2c_write_buf[0] = 0x65;
	auc_i2c_write_buf[1] = (u8)(temp >> 16);
	auc_i2c_write_buf[2] = (u8)(temp >> 8);
	auc_i2c_write_buf[3] = (u8)(temp);
	temp = dw_lenth;
	auc_i2c_write_buf[4] = (u8)(temp >> 8);
	auc_i2c_write_buf[5] = (u8)(temp);
	i_ret = fts_i2c_Write(client, auc_i2c_write_buf, 6);
	msleep(dw_lenth/256);

	for(i = 0; i < 100; i++)
	{
		auc_i2c_write_buf[0] = 0x6a;
		reg_val[0] = reg_val[1] = 0x00;
		fts_i2c_Read(client, auc_i2c_write_buf, 1, reg_val, 2);

		if (0xF0==reg_val[0] && 0x55==reg_val[1])
		{
			break;
		}
		msleep(1);

	}

	auc_i2c_write_buf[0] = 0x66;
	fts_i2c_Read(client, auc_i2c_write_buf, 1, reg_val, 1);
	if (reg_val[0] != bt_ecc)
	{
		pr_err("[FTS] Upgrade Step 6: ecc error! FW=%02x bt_ecc=%02x\n",
		        reg_val[0],
		        bt_ecc);
		if (reflash_flag) {
			pr_err("[FTS] check ecc failed in twice, abort\n");
			return -EIO;
		} else {
			reflash_flag++;
			pr_err("[FTS] check ecc error, reflash again\n");
			goto ft_fw_reflash_4;
		}
	}


	pr_warn("[FTS] Upgrade Step 7: reset the new FW\n");
	/*********Step 7: reset the new FW***********************/
	auc_i2c_write_buf[0] = 0x07;
	fts_i2c_Write(client, auc_i2c_write_buf, 1);
	msleep(300);	/*make sure CTP startup normally */

	//fts_tp_power_reset();
	fts_read_reg(client, FT_REG_FW_VER, &uc_tp_fm_ver);

	if (pbt_buf[dw_lenth -2] != uc_tp_fm_ver) {
		pr_err("[FTS] Upgrade Step 7 reset failed, %d\n", reflash_flag);
		if(reflash_flag < 4) {
			reflash_flag++;
			goto ft_fw_reflash_1;
		} else {
			pr_err("[FTS] reset error, abort\n");
			return -EIO;
		}
	}

	i_ret = fts_hidi2c_to_stdi2c(client);//Android to Std i2c.

	return 0;
}

int fts_ctpm_fw_upgrade_with_i_file(struct i2c_client *client,
                                    u8 *fw_data, u32 fw_len)
{
	int i_ret;

	/*judge the fw that will be upgraded
	* if illegal, then stop upgrade and return.
	*/
	do
	{
		if (fw_data == NULL)
		{
			pr_err( "%s:FW data = NULL error\n", __func__);
			i_ret = -EIO;
			break;
		}

		if (fw_len < FTS_MIN_FW_LENGTH || fw_len > FTS_APP_FW_LENGTH)
		{
			pr_err( "%s:FW length error\n", __func__);
			i_ret = -EIO;
			break;
		}
		
		/*call the upgrade function */
		i_ret = fts_ctpm_fw_upgrade(client, fw_data, fw_len);

		if (i_ret != 0)
		{
			pr_err( "%s:upgrade failed. err.\n",
			         __func__);

			break;
		}
		else if(fts_updateinfo_curr.AUTO_CLB == AUTO_CLB_NEED)
		{
			fts_ctpm_auto_clb(client);	/*start auto CLB */
		}
		
	} while(0);

	return i_ret;
}


int  fts_ctpm_fw_preupgrade_hwreset(struct i2c_client * client, u8* pbt_buf, u32 dw_lenth)
{
	u8 reg_val[4] = {0};
	u32 i = 0;
	u32 packet_number;
	u32 j;
	u32 temp;
	u32 lenght;
	u8 packet_buf[FTS_PACKET_LENGTH + 6];
	u8 auc_i2c_write_buf[10];
	u8 bt_ecc;
	int i_ret;
	
	for (i = 0; i < FTS_UPGRADE_LOOP; i++) 
	{
		/*********Step 1:Reset  CTPM *****/
		fts_ctpm_hw_reset();
		
		if (i<=15)
		{
			msleep(fts_updateinfo_curr.delay_55+i*3);
		}
		else
		{
			msleep(fts_updateinfo_curr.delay_55-(i-15)*2);
		}
		
		/*********Step 2:Enter upgrade mode *****/
		auc_i2c_write_buf[0] = FT_UPGRADE_55;
		i_ret = fts_i2c_Write(client, auc_i2c_write_buf, 1);
		if(i_ret < 0)
		{
			pr_err("[FTS] failed writing  0x55 ! \n");
			continue;
		}
		
		auc_i2c_write_buf[0] = FT_UPGRADE_AA;
		i_ret = fts_i2c_Write(client, auc_i2c_write_buf, 1);
		if(i_ret < 0)
		{
			pr_err("[FTS] failed writing  0xaa ! \n");
			continue;
		}

		/*********Step 3:check READ-ID***********************/
		msleep(1);
		auc_i2c_write_buf[0] = 0x90;
		auc_i2c_write_buf[1] = auc_i2c_write_buf[2] = auc_i2c_write_buf[3] =
			0x00;
		reg_val[0] = reg_val[1] = 0x00;
		
		fts_i2c_Read(client, auc_i2c_write_buf, 4, reg_val, 2);

		if (reg_val[0] == 0x54
			&& reg_val[1] == 0x22) 
		{
			i_ret = 0x00;
			fts_read_reg(client, 0xd0, &i_ret);

			if(i_ret == 0)
			{
				pr_err("[FTS] Step 3: READ State fail \n");
				continue;
			}

			pr_warn("[FTS] Step 3: i_ret = %d \n", i_ret);
			
			
			pr_warn("[FTS] Step 3: READ CTPM ID OK,ID1 = 0x%x,ID2 = 0x%x\n",
				reg_val[0], reg_val[1]);
			
			break;
		} 
		else 
		{
			dev_err(&client->dev, "[FTS] Step 3: CTPM ID,ID1 = 0x%x,ID2 = 0x%x\n",
				reg_val[0], reg_val[1]);
			
			continue;
		}
	}

	if (i >= FTS_UPGRADE_LOOP )
		return -EIO;

	/*********Step 4:write firmware(FW) to ctpm flash*********/
	bt_ecc = 0;
	pr_warn("Step 5:write firmware(FW) to ctpm flash\n");

	dw_lenth = dw_lenth - 8;
	temp = 0;
	packet_number = (dw_lenth) / FTS_PACKET_LENGTH;
	packet_buf[0] = 0xae;
	packet_buf[1] = 0x00;

	pr_warn("packet_number: %d\n", packet_number);
	for (j = 0; j < packet_number; j++) {
		temp = j * FTS_PACKET_LENGTH;
		packet_buf[2] = (u8) (temp >> 8);
		packet_buf[3] = (u8) temp;
		lenght = FTS_PACKET_LENGTH;
		packet_buf[4] = (u8) (lenght >> 8);
		packet_buf[5] = (u8) lenght;

		for (i = 0; i < FTS_PACKET_LENGTH; i++) {
			packet_buf[6 + i] = pbt_buf[j * FTS_PACKET_LENGTH + i];
			bt_ecc ^= packet_buf[6 + i];
		}
		fts_i2c_Write(client, packet_buf, FTS_PACKET_LENGTH + 6);
	}

	if ((dw_lenth) % FTS_PACKET_LENGTH > 0) {
		temp = packet_number * FTS_PACKET_LENGTH;
		packet_buf[2] = (u8) (temp >> 8);
		packet_buf[3] = (u8) temp;
		temp = (dw_lenth) % FTS_PACKET_LENGTH;
		packet_buf[4] = (u8) (temp >> 8);
		packet_buf[5] = (u8) temp;

		for (i = 0; i < temp; i++) {
			packet_buf[6 + i] = pbt_buf[packet_number * FTS_PACKET_LENGTH + i];
			bt_ecc ^= packet_buf[6 + i];
		}		
		fts_i2c_Write(client, packet_buf, temp + 6);
	}

	temp = FT_APP_INFO_ADDR;
	packet_buf[2] = (u8) (temp >> 8);
	packet_buf[3] = (u8) temp;
	temp = 8;
	packet_buf[4] = (u8) (temp >> 8);
	packet_buf[5] = (u8) temp;
	for (i = 0; i < 8; i++) 
	{
		packet_buf[6+i] = pbt_buf[dw_lenth + i];
		bt_ecc ^= packet_buf[6+i];
	}	
	fts_i2c_Write(client, packet_buf, 6+8);
	
	/*********Step 5: read out checksum***********************/
	/*send the opration head */
	pr_warn("Step 6: read out checksum\n");
	auc_i2c_write_buf[0] = 0xcc;
	//msleep(2);
	fts_i2c_Read(client, auc_i2c_write_buf, 1, reg_val, 1);
	if (reg_val[0] != bt_ecc) {
		dev_err(&client->dev, "[FTS]--ecc error! FW=%02x bt_ecc=%02x\n",
					reg_val[0],
					bt_ecc);	
		return -EIO;
	}
	pr_warn("checksum %X %X \n",reg_val[0],bt_ecc);
	pr_warn("Read flash and compare\n");
		
	msleep(50);

	/*********Step 6: start app***********************/
	pr_warn("Step 6: start app\n");
	auc_i2c_write_buf[0] = 0x08;
	fts_i2c_Write(client, auc_i2c_write_buf, 1);
	msleep(20);

	return 0;
}

#ifdef TPD_AUTO_DOWNLOAD
int fts_ctpm_fw_download(struct i2c_client *client, u8 *pbt_buf, u32 dw_lenth)
{
	u8 reg_val[4] = {0};
	u32 i = 0;
	u8 is_5336_new_bootloader = 0;
	u8 is_5336_fwsize_30 = 0;
	u32 packet_number;
	u32 j=0;
	u32 temp;
	u32 lenght;
	u8 packet_buf[FTS_PACKET_LENGTH + 6];
	u8 auc_i2c_write_buf[10];
	u8 bt_ecc;
	int i_ret;

	for (i = 0; i < FTS_UPGRADE_LOOP; i++)
	{
		msleep(100);

		pr_warn("[FTS] Download Step 1:Reset  CTPM\n");
		/*********Step 1:Reset  CTPM *****/

		/*write 0xaa to register 0xfc */
		fts_write_reg(client, 0xfc, FT_UPGRADE_AA);
		msleep(fts_updateinfo_curr.delay_aa);

		/*write 0x55 to register 0xfc */
		fts_write_reg(client, 0xfc, FT_UPGRADE_55);

		if (i<=15)
		{
			msleep(fts_updateinfo_curr.delay_55+i*3);
		}
		else
		{
			msleep(fts_updateinfo_curr.delay_55-(i-15)*2);
		}

		pr_warn("[FTS] Download Step 2:Enter upgrade mode \n");
		/*********Step 2:Enter upgrade mode *****/

		auc_i2c_write_buf[0] = FT_UPGRADE_55;
		fts_i2c_Write(client, auc_i2c_write_buf, 1);
		msleep(5);
		auc_i2c_write_buf[0] = FT_UPGRADE_AA;
		fts_i2c_Write(client, auc_i2c_write_buf, 1);

		/*********Step 3:check READ-ID***********************/
		msleep(fts_updateinfo_curr.delay_readid);
		auc_i2c_write_buf[0] = 0x90;
		auc_i2c_write_buf[1] = auc_i2c_write_buf[2] = auc_i2c_write_buf[3] =0x00;
		fts_i2c_Read(client, auc_i2c_write_buf, 4, reg_val, 2);

		pr_warn("[FTS] Download Step 3: CTPM ID,ID1 = 0x%x,ID2 = 0x%x\n",reg_val[0], reg_val[1]);
		if (reg_val[0] == fts_updateinfo_curr.download_id_1
		        && reg_val[1] == fts_updateinfo_curr.download_id_2)
		{
			pr_err("[FTS] Download Step 3: CTPM ID,ID1 = 0x%x,ID2 = 0x%x\n",
			    reg_val[0], reg_val[1]);
			break;
		}
		else
		{
			pr_err("[FTS] Download Step 3: CTPM ID,ID1 = 0x%x,ID2 = 0x%x\n",
			        reg_val[0], reg_val[1]);

			continue;
		}
	}

	if (i >= FTS_UPGRADE_LOOP)
	{
		if (reg_val[0] != 0x00 || reg_val[1] != 0x00)
		{
			fts_ctpm_hw_reset();
			msleep(300);
		}
		return -EIO;
	}
 
	pr_warn("[FTS] Download Step 4:change to write flash mode\n");
	fts_write_reg(client, 0x09, 0x0a);

	/*Step 4:erase app and panel paramenter area*/
	pr_warn("[FTS] Download Step 4:erase app and panel paramenter area\n");
	auc_i2c_write_buf[0] = 0x61;
	fts_i2c_Write(client, auc_i2c_write_buf, 1);	/*erase app area */
	msleep(fts_updateinfo_curr.delay_earse_flash);
	/*erase panel parameter area */

	for(i = 0; i < 15; i++)
	{
		auc_i2c_write_buf[0] = 0x6a;
		reg_val[0] = reg_val[1] = 0x00;
		fts_i2c_Read(client, auc_i2c_write_buf, 1, reg_val, 2);

		if(0xF0==reg_val[0] && 0xAA==reg_val[1])
		{
			break;
		}
		msleep(50);
	}

	pr_warn("[FTS] Download Step 5:write firmware(FW) to ctpm flash\n");
	/*********Step 5:write firmware(FW) to ctpm flash*********/
	bt_ecc = 0;

	//dw_lenth = dw_lenth - 8;
	temp = 0;
	packet_number = (dw_lenth) / FTS_PACKET_LENGTH;
	packet_buf[0] = 0xbf;
	packet_buf[1] = 0x00;

	pr_warn("[FTS] Download Step 5:packet_number: %d\n", packet_number);
	for (j = 0; j < packet_number; j++)
	{
		temp = j * FTS_PACKET_LENGTH;
		packet_buf[2] = (u8) (temp >> 8);
		packet_buf[3] = (u8) temp;
		lenght = FTS_PACKET_LENGTH;
		packet_buf[4] = (u8) (lenght >> 8);
		packet_buf[5] = (u8) lenght;

		for (i = 0; i < FTS_PACKET_LENGTH; i++)
		{
			packet_buf[6 + i] = pbt_buf[j * FTS_PACKET_LENGTH + i];
			bt_ecc ^= packet_buf[6 + i];
		}
		fts_i2c_Write(client, packet_buf, FTS_PACKET_LENGTH + 6);
		//msleep(10);

		for(i = 0; i < 30; i++)
		{
			auc_i2c_write_buf[0] = 0x6a;
			reg_val[0] = reg_val[1] = 0x00;
			fts_i2c_Read(client, auc_i2c_write_buf, 1, reg_val, 2);

			if ((j + 0x1000) == (((reg_val[0]) << 8) | reg_val[1]))
			{
				break;
			}
			//msleep(1);

		}
	}

	if ((dw_lenth) % FTS_PACKET_LENGTH > 0)
	{
		temp = packet_number * FTS_PACKET_LENGTH;
		packet_buf[2] = (u8) (temp >> 8);
		packet_buf[3] = (u8) temp;
		temp = (dw_lenth) % FTS_PACKET_LENGTH;
		packet_buf[4] = (u8) (temp >> 8);
		packet_buf[5] = (u8) temp;

		for (i = 0; i < temp; i++)
		{
			packet_buf[6 + i] = pbt_buf[packet_number * FTS_PACKET_LENGTH + i];
			bt_ecc ^= packet_buf[6 + i];
		}
		fts_i2c_Write(client, packet_buf, temp + 6);

		for(i = 0; i < 30; i++)
		{
			auc_i2c_write_buf[0] = 0x6a;
			reg_val[0] = reg_val[1] = 0x00;
			fts_i2c_Read(client, auc_i2c_write_buf, 1, reg_val, 2);

			if ((j + 0x1000) == (((reg_val[0]) << 8) | reg_val[1]))
			{
				break;
			}
			msleep(1);

		}
	}

	msleep(50);


	pr_warn("[FTS] Download Step 6: read out checksum\n");
	/*********Step 6: read out checksum***********************/
	auc_i2c_write_buf[0] = 0x64;
	fts_i2c_Write(client, auc_i2c_write_buf, 1);
	msleep(300);

	temp = 0;
	auc_i2c_write_buf[0] = 0x65;
	auc_i2c_write_buf[1] = (u8)(temp >> 16);
	auc_i2c_write_buf[2] = (u8)(temp >> 8);
	auc_i2c_write_buf[3] = (u8)(temp);
	temp = dw_lenth;
	auc_i2c_write_buf[4] = (u8)(temp >> 8);
	auc_i2c_write_buf[5] = (u8)(temp);
	i_ret = fts_i2c_Write(client, auc_i2c_write_buf, 6);
	msleep(dw_lenth/256);

	for(i = 0; i < 100; i++)
	{
		auc_i2c_write_buf[0] = 0x6a;
		reg_val[0] = reg_val[1] = 0x00;
		fts_i2c_Read(client, auc_i2c_write_buf, 1, reg_val, 2);

		if (0xF0==reg_val[0] && 0x55==reg_val[1])
		{
			break;
		}
		msleep(1);

	}

	auc_i2c_write_buf[0] = 0x66;
	fts_i2c_Read(client, auc_i2c_write_buf, 1, reg_val, 1);
	if (reg_val[0] != bt_ecc)
	{
		pr_err("[FTS] Download Step 6: ecc error! FW=%02x bt_ecc=%02x\n",
		        reg_val[0],
		        bt_ecc);

		return -EIO;
	}


	pr_warn("[FTS] Download Step 7: reset the new FW\n");
	/*********Step 7: reset the new FW***********************/

	fts_tp_power_reset();

	i_ret = fts_hidi2c_to_stdi2c(client);//Android to Std i2c.

	if(i_ret == 0)
	{
		pr_err("HidI2c change to StdI2c i_ret = %d ! \n", i_ret);
	}

	return 0;
}

int fts_ctpm_fw_download_with_i_file(struct i2c_client *client,
                                    u8 *fw_data, u32 fw_len)
{
	int i_ret;

	/*judge the fw that will be upgraded
	* if illegal, then stop upgrade and return.
	*/
	do
	{
		if (fw_data == NULL)
		{
			pr_err( "%s:FW data = NULL error\n", __func__);
			i_ret = -EIO;
			break;
		}

		if (fw_len < FTS_MIN_FW_LENGTH || fw_len > FTS_ALL_FW_LENGTH)
		{
			pr_err( "%s:FW length error\n", __func__);
			i_ret = -EIO;
			break;
		}

	    i_ret = fts_ctpm_fw_preupgrade_hwreset(client, aucFW_PRAM_BOOT, sizeof(aucFW_PRAM_BOOT));

		if (i_ret != 0)
		{
			pr_err( "%s:write pram failed. err.\n",
			         __func__);
			
			break;
		}
		
		/*call the upgrade function */
		i_ret = fts_ctpm_fw_download(client, fw_data, fw_len);

		if (i_ret != 0)
		{
			pr_err( "%s:upgrade failed. err.\n",
			         __func__);
		}
		
	} while(0);

	return i_ret;
}
#endif

// get lcm id from LCD
extern int primary_display_get_lcm_index(void);

int fts_ctpm_auto_upgrade(struct i2c_client *client)
{
	u8 *fw_data = NULL;
	u32 fw_length = 0;
	int i, i_ret;
	struct CtpInfo ctp_info = {0};
	u8 auc_i2c_write_buf;
	static bool version_check = true;
	int upgrade_ret = MZ_FAILED;
	int lcm_id;

	pr_warn("********************Enter CTP Auto Upgrade********************\n");

	for(i = 0; i < 5; ++i) {
		/*
		if(i < 3)
			i_ret = fts_ctpm_read_ctp_info_from_app(client,
					&ctp_info);
		else {
		*/
			i_ret = fts_ctpm_read_ctp_info_from_flash(client,
					&ctp_info);
		//	version_check = false;
		//}

		// there no version info in flash area, read it in app
		fts_read_reg(client, FT_REG_FW_VER, &(ctp_info.fw_version));
		pr_warn("[FTS] %d: lcm_id = 0x%x, fm_ver = 0x%x, vendor_id = 0x%x\n",
			    i, ctp_info.lcm_id,
			    ctp_info.fw_version, ctp_info.vendor_id);

/*
		if(ctp_info.vendor_id == 0x79) {
			pr_warn("[FTS] ignore old fw %02x upgrade",
					ctp_info.vendor_id);
			goto fw_exit;
		}
*/

		// get fw according to vendor_id and lcm_id
		i_ret = fts_ctpm_get_fw_data(ctp_info.vendor_id,
				ctp_info.lcm_id, &fw_data, &fw_length);
		if (i_ret != 0) {
			pr_err("can not find firmware using vendor_id %02x lcm_id %02x\n",
					ctp_info.vendor_id, ctp_info.lcm_id);
		} else
			break;
	}

	if(i >= 5) {
		pr_err("!!! firmware not match vendor_id %02x lcm_id %02x, exit\n",
				ctp_info.vendor_id, ctp_info.lcm_id);
		/*
		 * read from LCD, if lcm_id is not exist
		 * 0 -- sharp
		 * 1 -- auo
		 */
		lcm_id = primary_display_get_lcm_index();
		if(lcm_id >= 0) {
			pr_err("lcm_id from LCD %d\n", lcm_id);
			// ctp_fw_app index is same as lcm_id, direct reference
			fw_data = ctp_fw_app[lcm_id].fw_data;
			fw_length = ctp_fw_app[lcm_id].fw_length;
			version_check = false;
		} else {
			pr_err("!!! get wrong lcm_id from LCD %d, abort\n", lcm_id);
			goto fw_exit;
		}
	}

	//version check
	if(version_check && (ctp_info.fw_version >= fw_data[fw_length - 2])) {
		pr_warn("firmware is up-to-date ic %02x driver %02x, abort\n",
				ctp_info.fw_version, fw_data[fw_length - 2]);
		version_check = false;
		upgrade_ret = MZ_NO_ACTION;
		goto fw_exit;
	}

	//start upgrade
	i_ret = fts_ctpm_fw_upgrade(client, fw_data, fw_length);
	if(i_ret < 0) {
		pr_err("firmware upgrade failed, ret %d\n", i_ret);
	} else
		upgrade_ret = MZ_SUCCESS;

fw_exit:
	// read current tp info
	fts_ctpm_read_ctp_info_from_app(client, &cur_tp_info);
	return upgrade_ret;
}

/*sysfs debug*/

/*
*get firmware size

@firmware_name:firmware name
*note:the firmware default path is sdcard.
	if you want to change the dir, please modify by yourself.
*/
static int fts_GetFirmwareSize(char *firmware_name)
{
	struct file *pfile = NULL;
	struct inode *inode;
	unsigned long magic;
	off_t fsize = 0;
	char filepath[128];
	memset(filepath, 0, sizeof(filepath));

	sprintf(filepath, "%s", firmware_name);

	if (NULL == pfile)
		pfile = filp_open(filepath, O_RDONLY, 0);

	if (IS_ERR(pfile))
	{
		pr_err("error occured while opening file %s.\n", filepath);
		return -EIO;
	}

	inode = pfile->f_dentry->d_inode;
	magic = inode->i_sb->s_magic;
	fsize = inode->i_size;
	filp_close(pfile, NULL);
	return fsize;
}



/*
*read firmware buf for .bin file.

@firmware_name: fireware name
@firmware_buf: data buf of fireware

note:the firmware default path is sdcard.
	if you want to change the dir, please modify by yourself.
*/
static int fts_ReadFirmware(char *firmware_name,
                            unsigned char *firmware_buf)
{
	struct file *pfile = NULL;
	struct inode *inode;
	unsigned long magic;
	off_t fsize;
	char filepath[128];
	loff_t pos;
	mm_segment_t old_fs;

	memset(filepath, 0, sizeof(filepath));
	sprintf(filepath, "%s", firmware_name);
	if (NULL == pfile)
		pfile = filp_open(filepath, O_RDONLY, 0);
	if (IS_ERR(pfile))
	{
		pr_err("error occured while opening file %s.\n", filepath);
		return -EIO;
	}

	inode = pfile->f_dentry->d_inode;
	magic = inode->i_sb->s_magic;
	fsize = inode->i_size;
	old_fs = get_fs();
	set_fs(KERNEL_DS);
	pos = 0;
	vfs_read(pfile, firmware_buf, fsize, &pos);
	filp_close(pfile, NULL);
	set_fs(old_fs);

	return 0;
}



/*
upgrade with *.bin file
*/

int fts_ctpm_fw_upgrade_with_app_file(struct i2c_client *client,
                                      char *firmware_name)
{
	u8 *pbt_buf = NULL;
	int i_ret=0;
	int fwsize = fts_GetFirmwareSize(firmware_name);

	if (fwsize <= 0)
	{
		FTS_DBG( "%s ERROR:Get firmware size failed\n",
		         __func__);
		return -EIO;
	}

	if (fwsize < FTS_MIN_FW_LENGTH || fwsize > FTS_ALL_FW_LENGTH)
	{
		FTS_DBG( "%s:FW length error\n", __func__);
		return -EIO;
	}


	/*=========FW upgrade========================*/
	pbt_buf = kmalloc(fwsize + 1, GFP_ATOMIC);

	if (fts_ReadFirmware(firmware_name, pbt_buf))
	{
		FTS_DBG( "%s() - ERROR: request_firmware failed\n",
		         __func__);
		kfree(pbt_buf);
		//return -EIO;
		i_ret = -EIO;
		goto err_ret;
	}

	/*call the upgrade function */
	i_ret = fts_ctpm_fw_upgrade(client, pbt_buf, fwsize);
	if (i_ret != 0)
		FTS_DBG( "%s() - ERROR:[FTS] upgrade failed..\n",
		         __func__);
	else if(fts_updateinfo_curr.AUTO_CLB==AUTO_CLB_NEED)
		fts_ctpm_auto_clb(client);

err_ret:



	kfree(pbt_buf);

	return i_ret;
}

static u8 reg_dump_address = 0;
static u8 reg_dump_value = 0;

static ssize_t fts_tprwreg_show(struct device *dev,
                                struct device_attribute *attr,
                                char *buf)
{
	return sprintf(buf, "%02x: %02x\n", reg_dump_address, reg_dump_value);
}

static ssize_t fts_tprwreg_store(struct device *dev,
                                 struct device_attribute *attr,
                                 const char *buf, size_t count)
{
	struct i2c_client *client = container_of(dev, struct i2c_client, dev);
	ssize_t num_read_chars = 0;
	int retval;
	long unsigned int wmreg = 0;
	u8 regaddr = 0xff, regvalue = 0xff;
	u8 valbuf[5] = {0};

	memset(valbuf, 0, sizeof(valbuf));
	mutex_lock(&g_device_mutex);
	num_read_chars = count - 1;

	if (num_read_chars != 2)
	{
		if (num_read_chars != 4)
		{
			pr_info("please input 2 or 4 character\n");
			goto error_return;
		}
	}

	memcpy(valbuf, buf, num_read_chars);
	retval = strict_strtoul(valbuf, 16, &wmreg);

	if (0 != retval)
	{
		FTS_DBG( "%s() - ERROR: Could not convert the "\
		         "given input to a number." \
		         "The given input was: \"%s\"\n",
		         __func__, buf);
		goto error_return;
	}

	if (2 == num_read_chars)
	{
		/*read register*/
		regaddr = wmreg;
		if (fts_read_reg(client, regaddr, &regvalue) < 0)
			FTS_DBG( "Could not read the register(0x%02x)\n",
			         regaddr);
		else {
			reg_dump_address = regaddr;
			reg_dump_value = regvalue;
		}
	}
	else
	{
		regaddr = wmreg >> 8;
		regvalue = wmreg;
		if (fts_write_reg(client, regaddr, regvalue) < 0)
			FTS_DBG( "Could not write the register(0x%02x)\n",
			         regaddr);
		else
			FTS_DBG( "Write 0x%02x into register(0x%02x) successful\n",
			         regvalue, regaddr);
	}

error_return:
	mutex_unlock(&g_device_mutex);

	return count;
}

#ifdef FTS_MCAP_TEST
static ssize_t ft5x46_appid_show(struct device *dev,
				struct device_attribute *attr, char *buf)
{
	char tmp[30];
	int count = 0;

	count += sprintf(tmp + count, "81");

	if((cur_tp_info.lcm_id & 0xf0) == 0x10)
		count += sprintf(tmp + count, "SHP");
	else
		count += sprintf(tmp + count, "AUO");

	count += sprintf(tmp + count, "FOGS");

	if((cur_tp_info.lcm_id & 0x0f) == 0x01)
		count += sprintf(tmp + count, "W");
	else
		count += sprintf(tmp + count, "B");

	count += sprintf(tmp + count, ":V%02X:FT5X46:%02X\n",
			cur_tp_info.fw_version, cur_tp_info.lcm_id);

	memcpy(buf, tmp, count);
	return count;
}

static ssize_t ft5x46_appid_store(struct device *dev,
		struct device_attribute *attr, const char *buf, size_t count)
{
	return -EPERM;
}

static ssize_t ft5x46_ctptest_show(struct device *dev,
				struct device_attribute *attr, char *buf)
{
	int count = 0;
	char *errlog = NULL;
	int ret_c = 0;
	struct i2c_client *client = container_of(dev, struct i2c_client, dev);

	mutex_lock(&g_device_mutex);

	fts_ctpm_hw_reset();
	mdelay(300);

	Init_I2C_Write_Func(focal_i2c_Write);
	Init_I2C_Read_Func(focal_i2c_Read);

	SetParamData(ft5x46_test_config_default);

	if(true == StartTestTP()) {
		count += sprintf(buf + count, "0\n");
	} else {
		count += sprintf(buf + count, "1\n");
		count += sprintf(buf + count, "\n[FTS] tp test failure\n");
		errlog = kmalloc((2 * 1024), GFP_KERNEL);
		memset(errlog, 0, (2 * 1024));
		ret_c = focal_save_error_data(errlog, (2 * 1024));
		if (errlog != NULL) {
			memcpy(buf + count, errlog, ret_c);
			count += ret_c;
			kfree(errlog);
		}
	}

	mutex_unlock(&g_device_mutex);

	return count;
}

static ssize_t ft5x46_ctptest_store(struct device *dev,
		struct device_attribute *attr, const char *buf, size_t count)
{
	return -EPERM;
}
#endif

#ifdef MZ_HALL_MODE
int ft5x46_hall_mode_state = 0;
int ft5x46_hall_mode_switch(int state)
{
	int ret = 0;
	int _state = !!state;

#define COVER_MODE_EN_REG	0xC1
#define HOST_EVENT_MSG_REG	0xC3

	if(_state) { // enter hall mode
		ret = fts_write_reg(g_focalclient, COVER_MODE_EN_REG, _state);
		ret |= fts_write_reg(g_focalclient, HOST_EVENT_MSG_REG, 2);
	} else { // exit hall mode
		ret = fts_write_reg(g_focalclient, COVER_MODE_EN_REG, _state);
		ret |= fts_write_reg(g_focalclient, HOST_EVENT_MSG_REG, 0);
	}

	return ret;
}

static ssize_t ft5x46_hall_mode_store(struct device *dev,
		struct device_attribute *attr, const char *buf, size_t count)
{
	struct i2c_client *client = container_of(dev, struct i2c_client, dev);
	int state = simple_strtol(buf, NULL, 10);

	ft5x46_hall_mode_state = !!state;

	ft5x46_hall_mode_switch(ft5x46_hall_mode_state);

	return count;
}

static ssize_t ft5x46_hall_mode_show(struct device *dev,
				struct device_attribute *attr, char *buf)
{
	return sprintf(buf, "%d\n", ft5x46_hall_mode_state);
}

static DEVICE_ATTR(hall_mode, S_IRUGO | S_IWUSR, ft5x46_hall_mode_show,
                   ft5x46_hall_mode_store);
#endif

/*read and write register
*read example: echo 88 > ftstprwreg ---read register 0x88
*write example:echo 8807 > ftstprwreg ---write 0x07 into register 0x88
*
*note:the number of input must be 2 or 4.if it not enough,please fill in the 0.
*/
static DEVICE_ATTR(ftstprwreg, S_IRUGO | S_IWUSR, fts_tprwreg_show,
                   fts_tprwreg_store);

#ifdef FTS_MCAP_TEST
static DEVICE_ATTR(ctptest, S_IRUGO|S_IWUSR, ft5x46_ctptest_show, ft5x46_ctptest_store);
static DEVICE_ATTR(appid, S_IRUGO|S_IWUSR, ft5x46_appid_show, ft5x46_appid_store);
#endif

static int mz_tp_mode = MZ_UNKNOWN;
static int mz_tp_state = MZ_NO_ACTION;

void mz_tp_set_mode(int mode)
{
	mz_tp_mode = mode;
}

static ssize_t ft5x46_mode_show(struct device *dev,
				struct device_attribute *attr, char *buf)
{
	return sprintf(buf, "%d\n", mz_tp_mode);
}

static ssize_t ft5x46_fwupdate_show(struct device *dev,
				struct device_attribute *attr, char *buf)
{
	return sprintf(buf, "%d\n", mz_tp_state);
}

extern int tpd_firmware_upgrade_thread(void *unused);
static ssize_t ft5x46_fwupdate_store(struct device *dev,
		struct device_attribute *attr, const char *buf, size_t count)
{
	mz_tp_state = tpd_firmware_upgrade_thread(NULL);

	return count;
}

static DEVICE_ATTR(mode, S_IRUGO, ft5x46_mode_show, NULL);
static DEVICE_ATTR(fwupdate, S_IRUGO|S_IWUSR, ft5x46_fwupdate_show, ft5x46_fwupdate_store);

/*add your attr in here*/
static struct attribute *fts_attributes[] =
{
	&dev_attr_ftstprwreg.attr,
	&dev_attr_mode.attr,
	&dev_attr_fwupdate.attr,
#ifdef MZ_HALL_MODE
	&dev_attr_hall_mode.attr,
#endif
#ifdef FTS_MCAP_TEST
	&dev_attr_ctptest.attr,
	&dev_attr_appid.attr,
#endif
	NULL
};

static struct attribute_group fts_attribute_group =
{
	.attrs = fts_attributes
};

/*create sysfs for debug*/
int fts_dma_buffer_init(void)
{
	I2CDMABuf_va = (u8 *)dma_alloc_coherent(&tpd->dev->dev, FTS_DMA_BUF_SIZE, &I2CDMABuf_pa, GFP_KERNEL);

	if (!I2CDMABuf_va)
	{
		FTS_DBG("[FTS] %s Allocate DMA I2C Buffer failed!\n", __func__);
		return -EIO;
	}
	return 0;
}

int fts_dma_buffer_deinit(void)
{
	if (I2CDMABuf_va)
	{
		dma_free_coherent(&tpd->dev->dev, FTS_DMA_BUF_SIZE, I2CDMABuf_va, I2CDMABuf_pa);
		I2CDMABuf_va = NULL;
		I2CDMABuf_pa = 0;
		FTS_DBG("[FTS] %s free DMA I2C Buffer...\r\n", __func__);
	}
	return 0;
}

int fts_create_sysfs(struct i2c_client *client)
{
	int err;
	err = sysfs_create_group(&client->dev.kobj, &fts_attribute_group);
	if (0 != err)
	{
		FTS_DBG( "%s() - ERROR: sysfs_create_group() failed.\n",	 __func__);
		sysfs_remove_group(&client->dev.kobj, &fts_attribute_group);
		return -EIO;
	}
	else
	{
		mutex_init(&g_device_mutex);
		pr_info("ft6x06:%s() - sysfs_create_group() succeeded.\n",
		        __func__);
	}

	fts_hidi2c_to_stdi2c(client);

	return err;
}

void fts_release_sysfs(struct i2c_client *client)
{
	sysfs_remove_group(&client->dev.kobj, &fts_attribute_group);
	mutex_destroy(&g_device_mutex);
}
/*create apk debug channel*/

#define PROC_UPGRADE			0
#define PROC_READ_REGISTER		1
#define PROC_WRITE_REGISTER  	2
#define PROC_RAWDATA			3
#define PROC_AUTOCLB			4
#define PROC_UPGRADE_INFO		5
#define PROC_WRITE_DATA			6
#define PROC_READ_DATA			7

#define PROC_NAME	"ft5x0x-debug"
static unsigned char proc_operate_mode = PROC_RAWDATA;
static struct proc_dir_entry *ft5x0x_proc_entry;

/*interface of write proc*/
static int ft5x0x_debug_write(struct file *filp, const char __user *buf,
		size_t count, loff_t *offp)
{
	struct i2c_client *client = PDE_DATA(file_inode(filp));
	unsigned char writebuf[FTS_PACKET_LENGTH];
	int buflen = count;
	int writelen = 0;
	int ret = 0;

	if (copy_from_user(&writebuf, buf, buflen))
	{
		FTS_DBG( "%s:copy from user error\n", __func__);
		return -EFAULT;
	}
	proc_operate_mode = writebuf[0];

	switch (proc_operate_mode)
	{
	case PROC_UPGRADE:
	{
		char upgrade_file_path[128];
		memset(upgrade_file_path, 0, sizeof(upgrade_file_path));
		sprintf(upgrade_file_path, "%s", writebuf + 1);
		upgrade_file_path[buflen-1] = '\0';
		FTS_DBG("%s\n", upgrade_file_path);
		disable_irq(client->irq);

		ret = fts_ctpm_fw_upgrade_with_app_file(client, upgrade_file_path);

		enable_irq(client->irq);
		if (ret < 0)
		{
			FTS_DBG( "%s:upgrade failed.\n", __func__);
			return ret;
		}
	}
	break;
	case PROC_READ_REGISTER:
		writelen = 1;
		FTS_DBG("%s:register addr=0x%02x\n", __func__, writebuf[1]);
		ret = fts_i2c_Write(client, writebuf + 1, writelen);
		if (ret < 0)
		{
			FTS_DBG( "%s:write iic error\n", __func__);
			return ret;
		}
		break;
	case PROC_WRITE_REGISTER:
		writelen = 2;
		ret = fts_i2c_Write(client, writebuf + 1, writelen);
		if (ret < 0)
		{
			FTS_DBG( "%s:write iic error\n", __func__);
			return ret;
		}
		break;
	case PROC_RAWDATA:
		break;
	case PROC_AUTOCLB:
		fts_ctpm_auto_clb(client);
		break;
	case PROC_READ_DATA:
	case PROC_WRITE_DATA:
		writelen = count - 1;
		ret = fts_i2c_Write(client, writebuf + 1, writelen);
		if (ret < 0)
		{
			FTS_DBG( "%s:write iic error\n", __func__);
			return ret;
		}
		break;

	default:
		break;
	}


	return count;
}

/*interface of read proc*/
static int ft5x0x_debug_read(struct file *filp,char __user *buf,size_t count,
				loff_t *offp)
{
	struct i2c_client *client = PDE_DATA(file_inode(filp));
	int ret = 0, err = 0;
	u8 tx = 0, rx = 0;
	int i, j;
	unsigned char tmp[PAGE_SIZE];
	int num_read_chars = 0;
	int readlen = 0;
	u8 regvalue = 0x00, regaddr = 0x00;
	switch (proc_operate_mode)
	{
	case PROC_UPGRADE:
		/*after calling ft5x0x_debug_write to upgrade*/
		regaddr = 0xA6;
		ret = fts_read_reg(client, regaddr, &regvalue);
		if (ret < 0)
			num_read_chars = sprintf(tmp, "%s", "get fw version failed.\n");
		else
			num_read_chars = sprintf(tmp, "current fw version:0x%02x\n", regvalue);
		break;
	case PROC_READ_REGISTER:
		readlen = 1;
		ret = fts_i2c_Read(client, NULL, 0, tmp, readlen);
		if (ret < 0)
		{
			FTS_DBG( "%s:read iic error\n", __func__);
			return ret;
		}
		else
			FTS_DBG("%s:value=0x%02x\n", __func__, tmp[0]);
		num_read_chars = 1;
		break;
	case PROC_RAWDATA:
		break;
	case PROC_READ_DATA:
		readlen = count;
		ret = fts_i2c_Read(client, NULL, 0, tmp, readlen);
		if (ret < 0) {
			FTS_DBG( "%s:read iic error\n", __func__);
			return ret;
		}

		num_read_chars = readlen;
		break;
	case PROC_WRITE_DATA:
		break;
	default:
		break;
	}

	copy_to_user(buf, tmp, num_read_chars);
	//memcpy(page, buf, num_read_chars);

	return num_read_chars;
}

#ifdef FTS_ESD_CHECKER
extern int apk_debug_flag;
#endif

int ft5x0x_debug_open(struct inode * node, struct file *filp)
{
#ifdef FTS_ESD_CHECKER
	apk_debug_flag++;
#endif

	return 0;
}

int ft5x0x_debug_release(struct inode *node, struct file *filp)
{
#ifdef FTS_ESD_CHECKER
	apk_debug_flag--;
#endif

	return 0;
}

static struct file_operations ft5x0x_proc_fops = {
	.open = ft5x0x_debug_open,
	.read = ft5x0x_debug_read,
	.write = ft5x0x_debug_write,
	.release = ft5x0x_debug_release,
};

int ft5x0x_create_apk_debug_channel(struct i2c_client * client)
{
	ft5x0x_proc_entry = proc_create_data(PROC_NAME, 0777, NULL,
			&ft5x0x_proc_fops, client);
	if (NULL == ft5x0x_proc_entry)
	{
		FTS_DBG( "Couldn't create proc entry!\n");
		return -ENOMEM;
	}
	/*
	else
	{
		dev_info(&client->dev, "Create proc entry success!\n");
		ft5x0x_proc_entry->data = client;
		ft5x0x_proc_entry->write_proc = ft5x0x_debug_write;
		ft5x0x_proc_entry->read_proc = ft5x0x_debug_read;
	}
	*/
	return 0;
}

void ft5x0x_release_apk_debug_channel(void)
{
	if (ft5x0x_proc_entry)
		remove_proc_entry(PROC_NAME, NULL);
}