aboutsummaryrefslogtreecommitdiffstats
path: root/drivers/input/mouse/cyapa_gen5.c
blob: 7f012bfa26583d5133c59b76b788b62b30f69172 (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
2201
2202
2203
2204
2205
2206
2207
2208
2209
2210
2211
2212
2213
2214
2215
2216
2217
2218
2219
2220
2221
2222
2223
2224
2225
2226
2227
2228
2229
2230
2231
2232
2233
2234
2235
2236
2237
2238
2239
2240
2241
2242
2243
2244
2245
2246
2247
2248
2249
2250
2251
2252
2253
2254
2255
2256
2257
2258
2259
2260
2261
2262
2263
2264
2265
2266
2267
2268
2269
2270
2271
2272
2273
2274
2275
2276
2277
2278
2279
2280
2281
2282
2283
2284
2285
2286
2287
2288
2289
2290
2291
2292
2293
2294
2295
2296
2297
2298
2299
2300
2301
2302
2303
2304
2305
2306
2307
2308
2309
2310
2311
2312
2313
2314
2315
2316
2317
2318
2319
2320
2321
2322
2323
2324
2325
2326
2327
2328
2329
2330
2331
2332
2333
2334
2335
2336
2337
2338
2339
2340
2341
2342
2343
2344
2345
2346
2347
2348
2349
2350
2351
2352
2353
2354
2355
2356
2357
2358
2359
2360
2361
2362
2363
2364
2365
2366
2367
2368
2369
2370
2371
2372
2373
2374
2375
2376
2377
2378
2379
2380
2381
2382
2383
2384
2385
2386
2387
2388
2389
2390
2391
2392
2393
2394
2395
2396
2397
2398
2399
2400
2401
2402
2403
2404
2405
2406
2407
2408
2409
2410
2411
2412
2413
2414
2415
2416
2417
2418
2419
2420
2421
2422
2423
2424
2425
2426
2427
2428
2429
2430
2431
2432
2433
2434
2435
2436
2437
2438
2439
2440
2441
2442
2443
2444
2445
2446
2447
2448
2449
2450
2451
2452
2453
2454
2455
2456
2457
2458
2459
2460
2461
2462
2463
2464
2465
2466
2467
2468
2469
2470
2471
2472
2473
2474
2475
2476
2477
2478
2479
2480
2481
2482
2483
2484
2485
2486
2487
2488
2489
2490
2491
2492
2493
2494
2495
2496
2497
2498
2499
2500
2501
2502
2503
2504
2505
2506
2507
2508
2509
2510
2511
2512
2513
2514
2515
2516
2517
2518
2519
2520
2521
2522
2523
2524
2525
2526
2527
2528
2529
2530
2531
2532
2533
2534
2535
2536
2537
2538
2539
2540
2541
2542
2543
2544
2545
2546
2547
2548
2549
2550
2551
2552
2553
2554
2555
2556
2557
2558
2559
2560
2561
2562
2563
2564
2565
2566
2567
2568
2569
2570
2571
2572
2573
2574
2575
2576
2577
2578
2579
2580
2581
2582
2583
2584
2585
2586
2587
2588
2589
2590
2591
2592
2593
2594
2595
2596
2597
2598
2599
2600
2601
2602
2603
2604
2605
2606
2607
2608
2609
2610
2611
2612
2613
2614
2615
2616
2617
2618
2619
2620
2621
2622
2623
2624
2625
2626
2627
2628
2629
2630
2631
2632
2633
2634
2635
2636
2637
2638
2639
2640
2641
2642
2643
2644
2645
2646
2647
2648
2649
2650
2651
2652
2653
2654
2655
2656
2657
2658
2659
2660
2661
2662
2663
2664
2665
2666
2667
2668
2669
2670
2671
2672
2673
2674
2675
2676
2677
2678
2679
2680
2681
2682
2683
2684
2685
2686
2687
2688
2689
2690
2691
2692
2693
2694
2695
2696
2697
2698
2699
2700
2701
2702
2703
2704
2705
2706
2707
2708
2709
2710
2711
2712
2713
2714
2715
2716
2717
2718
2719
2720
2721
2722
2723
2724
2725
2726
2727
2728
2729
2730
2731
2732
2733
2734
2735
2736
2737
2738
2739
2740
2741
2742
2743
2744
2745
2746
2747
2748
2749
2750
2751
2752
2753
2754
2755
2756
2757
2758
2759
2760
2761
2762
2763
2764
2765
2766
2767
2768
2769
2770
2771
2772
2773
2774
2775
2776
2777
2778
2779
2780
2781
2782
2783
2784
2785
2786
2787
2788
2789
2790
2791
2792
2793
2794
2795
2796
2797
2798
2799
2800
2801
2802
2803
2804
2805
2806
2807
2808
2809
2810
2811
2812
2813
2814
2815
2816
2817
2818
2819
2820
2821
2822
2823
2824
2825
2826
2827
2828
2829
2830
2831
2832
2833
2834
2835
2836
2837
2838
2839
2840
2841
2842
2843
2844
2845
2846
2847
2848
2849
2850
2851
2852
2853
2854
2855
2856
2857
2858
2859
2860
2861
2862
2863
2864
2865
2866
2867
2868
2869
2870
2871
2872
2873
2874
2875
2876
2877
2878
2879
2880
2881
2882
2883
2884
2885
2886
2887
2888
2889
2890
2891
2892
2893
2894
2895
2896
2897
2898
2899
2900
2901
2902
2903
2904
2905
2906
2907
2908
2909
/*
 * Cypress APA trackpad with I2C interface
 *
 * Author: Dudley Du <dudl@cypress.com>
 *
 * Copyright (C) 2014-2015 Cypress Semiconductor, Inc.
 *
 * This file is subject to the terms and conditions of the GNU General Public
 * License.  See the file COPYING in the main directory of this archive for
 * more details.
 */

#include <linux/delay.h>
#include <linux/i2c.h>
#include <linux/input.h>
#include <linux/input/mt.h>
#include <linux/mutex.h>
#include <linux/completion.h>
#include <linux/slab.h>
#include <asm/unaligned.h>
#include <linux/crc-itu-t.h>
#include <linux/pm_runtime.h>
#include "cyapa.h"


/* Macro of TSG firmware image */
#define CYAPA_TSG_FLASH_MAP_BLOCK_SIZE      0x80
#define CYAPA_TSG_IMG_FW_HDR_SIZE           13
#define CYAPA_TSG_FW_ROW_SIZE               (CYAPA_TSG_FLASH_MAP_BLOCK_SIZE)
#define CYAPA_TSG_IMG_START_ROW_NUM         0x002e
#define CYAPA_TSG_IMG_END_ROW_NUM           0x01fe
#define CYAPA_TSG_IMG_APP_INTEGRITY_ROW_NUM 0x01ff
#define CYAPA_TSG_IMG_MAX_RECORDS           (CYAPA_TSG_IMG_END_ROW_NUM - \
				CYAPA_TSG_IMG_START_ROW_NUM + 1 + 1)
#define CYAPA_TSG_IMG_READ_SIZE             (CYAPA_TSG_FLASH_MAP_BLOCK_SIZE / 2)
#define CYAPA_TSG_START_OF_APPLICATION      0x1700
#define CYAPA_TSG_APP_INTEGRITY_SIZE        60
#define CYAPA_TSG_FLASH_MAP_METADATA_SIZE   60
#define CYAPA_TSG_BL_KEY_SIZE               8

#define CYAPA_TSG_MAX_CMD_SIZE              256

/* Macro of PIP interface */
#define PIP_BL_INITIATE_RESP_LEN            11
#define PIP_BL_FAIL_EXIT_RESP_LEN           11
#define PIP_BL_FAIL_EXIT_STATUS_CODE        0x0c
#define PIP_BL_VERIFY_INTEGRITY_RESP_LEN    12
#define PIP_BL_INTEGRITY_CHEKC_PASS         0x00
#define PIP_BL_BLOCK_WRITE_RESP_LEN         11

#define PIP_TOUCH_REPORT_ID         0x01
#define PIP_BTN_REPORT_ID           0x03
#define PIP_WAKEUP_EVENT_REPORT_ID  0x04
#define PIP_PUSH_BTN_REPORT_ID      0x06
#define GEN5_OLD_PUSH_BTN_REPORT_ID 0x05  /* Special for old Gen5 TP. */
#define PIP_PROXIMITY_REPORT_ID     0x07

#define PIP_PROXIMITY_REPORT_SIZE	6
#define PIP_PROXIMITY_DISTANCE_OFFSET	0x05
#define PIP_PROXIMITY_DISTANCE_MASK	0x01

#define PIP_TOUCH_REPORT_HEAD_SIZE     7
#define PIP_TOUCH_REPORT_MAX_SIZE      127
#define PIP_BTN_REPORT_HEAD_SIZE       6
#define PIP_BTN_REPORT_MAX_SIZE        14
#define PIP_WAKEUP_EVENT_SIZE          4

#define PIP_NUMBER_OF_TOUCH_OFFSET  5
#define PIP_NUMBER_OF_TOUCH_MASK    0x1f
#define PIP_BUTTONS_OFFSET          5
#define PIP_BUTTONS_MASK            0x0f
#define PIP_GET_EVENT_ID(reg)       (((reg) >> 5) & 0x03)
#define PIP_GET_TOUCH_ID(reg)       ((reg) & 0x1f)
#define PIP_TOUCH_TYPE_FINGER	    0x00
#define PIP_TOUCH_TYPE_PROXIMITY    0x01
#define PIP_TOUCH_TYPE_HOVER	    0x02
#define PIP_GET_TOUCH_TYPE(reg)     ((reg) & 0x07)

#define RECORD_EVENT_NONE        0
#define RECORD_EVENT_TOUCHDOWN	 1
#define RECORD_EVENT_DISPLACE    2
#define RECORD_EVENT_LIFTOFF     3

#define PIP_SENSING_MODE_MUTUAL_CAP_FINE   0x00
#define PIP_SENSING_MODE_SELF_CAP          0x02

#define PIP_SET_PROXIMITY	0x49

/* Macro of Gen5 */
#define GEN5_BL_MAX_OUTPUT_LENGTH     0x0100
#define GEN5_APP_MAX_OUTPUT_LENGTH    0x00fe

#define GEN5_POWER_STATE_ACTIVE              0x01
#define GEN5_POWER_STATE_LOOK_FOR_TOUCH      0x02
#define GEN5_POWER_STATE_READY               0x03
#define GEN5_POWER_STATE_IDLE                0x04
#define GEN5_POWER_STATE_BTN_ONLY            0x05
#define GEN5_POWER_STATE_OFF                 0x06

#define GEN5_POWER_READY_MAX_INTRVL_TIME  50   /* Unit: ms */
#define GEN5_POWER_IDLE_MAX_INTRVL_TIME   250  /* Unit: ms */

#define GEN5_CMD_GET_PARAMETER		     0x05
#define GEN5_CMD_SET_PARAMETER		     0x06
#define GEN5_PARAMETER_ACT_INTERVL_ID        0x4d
#define GEN5_PARAMETER_ACT_INTERVL_SIZE      1
#define GEN5_PARAMETER_ACT_LFT_INTERVL_ID    0x4f
#define GEN5_PARAMETER_ACT_LFT_INTERVL_SIZE  2
#define GEN5_PARAMETER_LP_INTRVL_ID          0x4c
#define GEN5_PARAMETER_LP_INTRVL_SIZE        2

#define GEN5_PARAMETER_DISABLE_PIP_REPORT    0x08

#define GEN5_BL_REPORT_DESCRIPTOR_SIZE            0x1d
#define GEN5_BL_REPORT_DESCRIPTOR_ID              0xfe
#define GEN5_APP_REPORT_DESCRIPTOR_SIZE           0xee
#define GEN5_APP_CONTRACT_REPORT_DESCRIPTOR_SIZE  0xfa
#define GEN5_APP_REPORT_DESCRIPTOR_ID             0xf6

#define GEN5_RETRIEVE_MUTUAL_PWC_DATA        0x00
#define GEN5_RETRIEVE_SELF_CAP_PWC_DATA      0x01

#define GEN5_RETRIEVE_DATA_ELEMENT_SIZE_MASK 0x07

#define GEN5_CMD_EXECUTE_PANEL_SCAN          0x2a
#define GEN5_CMD_RETRIEVE_PANEL_SCAN         0x2b
#define GEN5_PANEL_SCAN_MUTUAL_RAW_DATA      0x00
#define GEN5_PANEL_SCAN_MUTUAL_BASELINE      0x01
#define GEN5_PANEL_SCAN_MUTUAL_DIFFCOUNT     0x02
#define GEN5_PANEL_SCAN_SELF_RAW_DATA        0x03
#define GEN5_PANEL_SCAN_SELF_BASELINE        0x04
#define GEN5_PANEL_SCAN_SELF_DIFFCOUNT       0x05

/* The offset only valid for retrieve PWC and panel scan commands */
#define GEN5_RESP_DATA_STRUCTURE_OFFSET      10
#define GEN5_PWC_DATA_ELEMENT_SIZE_MASK      0x07


struct cyapa_pip_touch_record {
	/*
	 * Bit 7 - 3: reserved
	 * Bit 2 - 0: touch type;
	 *            0 : standard finger;
	 *            1 : proximity (Start supported in Gen5 TP).
	 *            2 : finger hover (defined, but not used yet.)
	 *            3 - 15 : reserved.
	 */
	u8 touch_type;

	/*
	 * Bit 7: indicates touch liftoff status.
	 *		0 : touch is currently on the panel.
	 *		1 : touch record indicates a liftoff.
	 * Bit 6 - 5: indicates an event associated with this touch instance
	 *		0 : no event
	 *		1 : touchdown
	 *		2 : significant displacement (> active distance)
	 *		3 : liftoff (record reports last known coordinates)
	 * Bit 4 - 0: An arbitrary ID tag associated with a finger
	 *		to allow tracking a touch as it moves around the panel.
	 */
	u8 touch_tip_event_id;

	/* Bit 7 - 0 of X-axis coordinate of the touch in pixel. */
	u8 x_lo;

	/* Bit 15 - 8 of X-axis coordinate of the touch in pixel. */
	u8 x_hi;

	/* Bit 7 - 0 of Y-axis coordinate of the touch in pixel. */
	u8 y_lo;

	/* Bit 15 - 8 of Y-axis coordinate of the touch in pixel. */
	u8 y_hi;

	/*
	 * The meaning of this value is different when touch_type is different.
	 * For standard finger type:
	 *	Touch intensity in counts, pressure value.
	 * For proximity type (Start supported in Gen5 TP):
	 *	The distance, in surface units, between the contact and
	 *	the surface.
	 **/
	u8 z;

	/*
	 * The length of the major axis of the ellipse of contact between
	 * the finger and the panel (ABS_MT_TOUCH_MAJOR).
	 */
	u8 major_axis_len;

	/*
	 * The length of the minor axis of the ellipse of contact between
	 * the finger and the panel (ABS_MT_TOUCH_MINOR).
	 */
	u8 minor_axis_len;

	/*
	 * The length of the major axis of the approaching tool.
	 * (ABS_MT_WIDTH_MAJOR)
	 */
	u8 major_tool_len;

	/*
	 * The length of the minor axis of the approaching tool.
	 * (ABS_MT_WIDTH_MINOR)
	 */
	u8 minor_tool_len;

	/*
	 * The angle between the panel vertical axis and
	 * the major axis of the contact ellipse. This value is an 8-bit
	 * signed integer. The range is -127 to +127 (corresponding to
	 * -90 degree and +90 degree respectively).
	 * The positive direction is clockwise from the vertical axis.
	 * If the ellipse of contact degenerates into a circle,
	 * orientation is reported as 0.
	 */
	u8 orientation;
} __packed;

struct cyapa_pip_report_data {
	u8 report_head[PIP_TOUCH_REPORT_HEAD_SIZE];
	struct cyapa_pip_touch_record touch_records[10];
} __packed;

struct cyapa_tsg_bin_image_head {
	u8 head_size;  /* Unit: bytes, including itself. */
	u8 ttda_driver_major_version;  /* Reserved as 0. */
	u8 ttda_driver_minor_version;  /* Reserved as 0. */
	u8 fw_major_version;
	u8 fw_minor_version;
	u8 fw_revision_control_number[8];
	u8 silicon_id_hi;
	u8 silicon_id_lo;
	u8 chip_revision;
	u8 family_id;
	u8 bl_ver_maj;
	u8 bl_ver_min;
} __packed;

struct cyapa_tsg_bin_image_data_record {
	u8 flash_array_id;
	__be16 row_number;
	/* The number of bytes of flash data contained in this record. */
	__be16 record_len;
	/* The flash program data. */
	u8 record_data[CYAPA_TSG_FW_ROW_SIZE];
} __packed;

struct cyapa_tsg_bin_image {
	struct cyapa_tsg_bin_image_head image_head;
	struct cyapa_tsg_bin_image_data_record records[];
} __packed;

struct pip_bl_packet_start {
	u8 sop;  /* Start of packet, must be 01h */
	u8 cmd_code;
	__le16 data_length;  /* Size of data parameter start from data[0] */
} __packed;

struct pip_bl_packet_end {
	__le16 crc;
	u8 eop;  /* End of packet, must be 17h */
} __packed;

struct pip_bl_cmd_head {
	__le16 addr;   /* Output report register address, must be 0004h */
	/* Size of packet not including output report register address */
	__le16 length;
	u8 report_id;  /* Bootloader output report id, must be 40h */
	u8 rsvd;  /* Reserved, must be 0 */
	struct pip_bl_packet_start packet_start;
	u8 data[];  /* Command data variable based on commands */
} __packed;

/* Initiate bootload command data structure. */
struct pip_bl_initiate_cmd_data {
	/* Key must be "A5h 01h 02h 03h FFh FEh FDh 5Ah" */
	u8 key[CYAPA_TSG_BL_KEY_SIZE];
	u8 metadata_raw_parameter[CYAPA_TSG_FLASH_MAP_METADATA_SIZE];
	__le16 metadata_crc;
} __packed;

struct tsg_bl_metadata_row_params {
	__le16 size;
	__le16 maximum_size;
	__le32 app_start;
	__le16 app_len;
	__le16 app_crc;
	__le32 app_entry;
	__le32 upgrade_start;
	__le16 upgrade_len;
	__le16 entry_row_crc;
	u8 padding[36];  /* Padding data must be 0 */
	__le16 metadata_crc;  /* CRC starts at offset of 60 */
} __packed;

/* Bootload program and verify row command data structure */
struct tsg_bl_flash_row_head {
	u8 flash_array_id;
	__le16 flash_row_id;
	u8 flash_data[];
} __packed;

struct pip_app_cmd_head {
	__le16 addr;   /* Output report register address, must be 0004h */
	/* Size of packet not including output report register address */
	__le16 length;
	u8 report_id;  /* Application output report id, must be 2Fh */
	u8 rsvd;  /* Reserved, must be 0 */
	/*
	 * Bit 7: reserved, must be 0.
	 * Bit 6-0: command code.
	 */
	u8 cmd_code;
	u8 parameter_data[];  /* Parameter data variable based on cmd_code */
} __packed;

/* Application get/set parameter command data structure */
struct gen5_app_set_parameter_data {
	u8 parameter_id;
	u8 parameter_size;
	__le32 value;
} __packed;

struct gen5_app_get_parameter_data {
	u8 parameter_id;
} __packed;

struct gen5_retrieve_panel_scan_data {
	__le16 read_offset;
	__le16 read_elements;
	u8 data_id;
} __packed;

u8 pip_read_sys_info[] = { 0x04, 0x00, 0x05, 0x00, 0x2f, 0x00, 0x02 };
u8 pip_bl_read_app_info[] = { 0x04, 0x00, 0x0b, 0x00, 0x40, 0x00,
		0x01, 0x3c, 0x00, 0x00, 0xb0, 0x42, 0x17
	};

static u8 cyapa_pip_bl_cmd_key[] = { 0xa5, 0x01, 0x02, 0x03,
	0xff, 0xfe, 0xfd, 0x5a };

static int cyapa_pip_event_process(struct cyapa *cyapa,
				   struct cyapa_pip_report_data *report_data);

int cyapa_pip_cmd_state_initialize(struct cyapa *cyapa)
{
	struct cyapa_pip_cmd_states *pip = &cyapa->cmd_states.pip;

	init_completion(&pip->cmd_ready);
	atomic_set(&pip->cmd_issued, 0);
	mutex_init(&pip->cmd_lock);

	mutex_init(&pip->pm_stage_lock);
	pip->pm_stage = CYAPA_PM_DEACTIVE;

	pip->resp_sort_func = NULL;
	pip->in_progress_cmd = PIP_INVALID_CMD;
	pip->resp_data = NULL;
	pip->resp_len = NULL;

	cyapa->dev_pwr_mode = UNINIT_PWR_MODE;
	cyapa->dev_sleep_time = UNINIT_SLEEP_TIME;

	return 0;
}

/* Return negative errno, or else the number of bytes read. */
ssize_t cyapa_i2c_pip_read(struct cyapa *cyapa, u8 *buf, size_t size)
{
	int ret;

	if (size == 0)
		return 0;

	if (!buf || size > CYAPA_REG_MAP_SIZE)
		return -EINVAL;

	ret = i2c_master_recv(cyapa->client, buf, size);

	if (ret != size)
		return (ret < 0) ? ret : -EIO;
	return size;
}

/**
 * Return a negative errno code else zero on success.
 */
ssize_t cyapa_i2c_pip_write(struct cyapa *cyapa, u8 *buf, size_t size)
{
	int ret;

	if (!buf || !size)
		return -EINVAL;

	ret = i2c_master_send(cyapa->client, buf, size);

	if (ret != size)
		return (ret < 0) ? ret : -EIO;

	return 0;
}

static void cyapa_set_pip_pm_state(struct cyapa *cyapa,
				   enum cyapa_pm_stage pm_stage)
{
	struct cyapa_pip_cmd_states *pip = &cyapa->cmd_states.pip;

	mutex_lock(&pip->pm_stage_lock);
	pip->pm_stage = pm_stage;
	mutex_unlock(&pip->pm_stage_lock);
}

static void cyapa_reset_pip_pm_state(struct cyapa *cyapa)
{
	struct cyapa_pip_cmd_states *pip = &cyapa->cmd_states.pip;

	/* Indicates the pip->pm_stage is not valid. */
	mutex_lock(&pip->pm_stage_lock);
	pip->pm_stage = CYAPA_PM_DEACTIVE;
	mutex_unlock(&pip->pm_stage_lock);
}

static enum cyapa_pm_stage cyapa_get_pip_pm_state(struct cyapa *cyapa)
{
	struct cyapa_pip_cmd_states *pip = &cyapa->cmd_states.pip;
	enum cyapa_pm_stage pm_stage;

	mutex_lock(&pip->pm_stage_lock);
	pm_stage = pip->pm_stage;
	mutex_unlock(&pip->pm_stage_lock);

	return pm_stage;
}

/**
 * This function is aimed to dump all not read data in Gen5 trackpad
 * before send any command, otherwise, the interrupt line will be blocked.
 */
int cyapa_empty_pip_output_data(struct cyapa *cyapa,
		u8 *buf, int *len, cb_sort func)
{
	struct input_dev *input = cyapa->input;
	struct cyapa_pip_cmd_states *pip = &cyapa->cmd_states.pip;
	enum cyapa_pm_stage pm_stage = cyapa_get_pip_pm_state(cyapa);
	int length;
	int report_count;
	int empty_count;
	int buf_len;
	int error;

	buf_len = 0;
	if (len) {
		buf_len = (*len < CYAPA_REG_MAP_SIZE) ?
				*len : CYAPA_REG_MAP_SIZE;
		*len = 0;
	}

	report_count = 8;  /* max 7 pending data before command response data */
	empty_count = 0;
	do {
		/*
		 * Depending on testing in cyapa driver, there are max 5 "02 00"
		 * packets between two valid buffered data report in firmware.
		 * So in order to dump all buffered data out and
		 * make interrupt line release for reassert again,
		 * we must set the empty_count check value bigger than 5 to
		 * make it work. Otherwise, in some situation,
		 * the interrupt line may unable to reactive again,
		 * which will cause trackpad device unable to
		 * report data any more.
		 * for example, it may happen in EFT and ESD testing.
		 */
		if (empty_count > 5)
			return 0;

		error = cyapa_i2c_pip_read(cyapa, pip->empty_buf,
				PIP_RESP_LENGTH_SIZE);
		if (error < 0)
			return error;

		length = get_unaligned_le16(pip->empty_buf);
		if (length == PIP_RESP_LENGTH_SIZE) {
			empty_count++;
			continue;
		} else if (length > CYAPA_REG_MAP_SIZE) {
			/* Should not happen */
			return -EINVAL;
		} else if (length == 0) {
			/* Application or bootloader launch data polled out. */
			length = PIP_RESP_LENGTH_SIZE;
			if (buf && buf_len && func &&
				func(cyapa, pip->empty_buf, length)) {
				length = min(buf_len, length);
				memcpy(buf, pip->empty_buf, length);
				*len = length;
				/* Response found, success. */
				return 0;
			}
			continue;
		}

		error = cyapa_i2c_pip_read(cyapa, pip->empty_buf, length);
		if (error < 0)
			return error;

		report_count--;
		empty_count = 0;
		length = get_unaligned_le16(pip->empty_buf);
		if (length <= PIP_RESP_LENGTH_SIZE) {
			empty_count++;
		} else if (buf && buf_len && func &&
			func(cyapa, pip->empty_buf, length)) {
			length = min(buf_len, length);
			memcpy(buf, pip->empty_buf, length);
			*len = length;
			/* Response found, success. */
			return 0;
		} else if (cyapa->operational && input && input->users &&
			   (pm_stage == CYAPA_PM_RUNTIME_RESUME ||
			    pm_stage == CYAPA_PM_RUNTIME_SUSPEND)) {
			/* Parse the data and report it if it's valid. */
			cyapa_pip_event_process(cyapa,
			       (struct cyapa_pip_report_data *)pip->empty_buf);
		}

		error = -EINVAL;
	} while (report_count);

	return error;
}

static int cyapa_do_i2c_pip_cmd_irq_sync(
		struct cyapa *cyapa,
		u8 *cmd, size_t cmd_len,
		unsigned long timeout)
{
	struct cyapa_pip_cmd_states *pip = &cyapa->cmd_states.pip;
	int error;

	/* Wait for interrupt to set ready completion */
	init_completion(&pip->cmd_ready);

	atomic_inc(&pip->cmd_issued);
	error = cyapa_i2c_pip_write(cyapa, cmd, cmd_len);
	if (error) {
		atomic_dec(&pip->cmd_issued);
		return (error < 0) ? error : -EIO;
	}

	/* Wait for interrupt to indicate command is completed. */
	timeout = wait_for_completion_timeout(&pip->cmd_ready,
				msecs_to_jiffies(timeout));
	if (timeout == 0) {
		atomic_dec(&pip->cmd_issued);
		return -ETIMEDOUT;
	}

	return 0;
}

static int cyapa_do_i2c_pip_cmd_polling(
		struct cyapa *cyapa,
		u8 *cmd, size_t cmd_len,
		u8 *resp_data, int *resp_len,
		unsigned long timeout,
		cb_sort func)
{
	struct cyapa_pip_cmd_states *pip = &cyapa->cmd_states.pip;
	int tries;
	int length;
	int error;

	atomic_inc(&pip->cmd_issued);
	error = cyapa_i2c_pip_write(cyapa, cmd, cmd_len);
	if (error) {
		atomic_dec(&pip->cmd_issued);
		return error < 0 ? error : -EIO;
	}

	length = resp_len ? *resp_len : 0;
	if (resp_data && resp_len && length != 0 && func) {
		tries = timeout / 5;
		do {
			usleep_range(3000, 5000);
			*resp_len = length;
			error = cyapa_empty_pip_output_data(cyapa,
					resp_data, resp_len, func);
			if (error || *resp_len == 0)
				continue;
			else
				break;
		} while (--tries > 0);
		if ((error || *resp_len == 0) || tries <= 0)
			error = error ? error : -ETIMEDOUT;
	}

	atomic_dec(&pip->cmd_issued);
	return error;
}

int cyapa_i2c_pip_cmd_irq_sync(
		struct cyapa *cyapa,
		u8 *cmd, int cmd_len,
		u8 *resp_data, int *resp_len,
		unsigned long timeout,
		cb_sort func,
		bool irq_mode)
{
	struct cyapa_pip_cmd_states *pip = &cyapa->cmd_states.pip;
	int error;

	if (!cmd || !cmd_len)
		return -EINVAL;

	/* Commands must be serialized. */
	error = mutex_lock_interruptible(&pip->cmd_lock);
	if (error)
		return error;

	pip->resp_sort_func = func;
	pip->resp_data = resp_data;
	pip->resp_len = resp_len;

	if (cmd_len >= PIP_MIN_APP_CMD_LENGTH &&
			cmd[4] == PIP_APP_CMD_REPORT_ID) {
		/* Application command */
		pip->in_progress_cmd = cmd[6] & 0x7f;
	} else if (cmd_len >= PIP_MIN_BL_CMD_LENGTH &&
			cmd[4] == PIP_BL_CMD_REPORT_ID) {
		/* Bootloader command */
		pip->in_progress_cmd = cmd[7];
	}

	/* Send command data, wait and read output response data's length. */
	if (irq_mode) {
		pip->is_irq_mode = true;
		error = cyapa_do_i2c_pip_cmd_irq_sync(cyapa, cmd, cmd_len,
							timeout);
		if (error == -ETIMEDOUT && resp_data &&
				resp_len && *resp_len != 0 && func) {
			/*
			 * For some old version, there was no interrupt for
			 * the command response data, so need to poll here
			 * to try to get the response data.
			 */
			error = cyapa_empty_pip_output_data(cyapa,
					resp_data, resp_len, func);
			if (error || *resp_len == 0)
				error = error ? error : -ETIMEDOUT;
		}
	} else {
		pip->is_irq_mode = false;
		error = cyapa_do_i2c_pip_cmd_polling(cyapa, cmd, cmd_len,
				resp_data, resp_len, timeout, func);
	}

	pip->resp_sort_func = NULL;
	pip->resp_data = NULL;
	pip->resp_len = NULL;
	pip->in_progress_cmd = PIP_INVALID_CMD;

	mutex_unlock(&pip->cmd_lock);
	return error;
}

bool cyapa_sort_tsg_pip_bl_resp_data(struct cyapa *cyapa,
		u8 *data, int len)
{
	if (!data || len < PIP_MIN_BL_RESP_LENGTH)
		return false;

	/* Bootloader input report id 30h */
	if (data[PIP_RESP_REPORT_ID_OFFSET] == PIP_BL_RESP_REPORT_ID &&
			data[PIP_RESP_RSVD_OFFSET] == PIP_RESP_RSVD_KEY &&
			data[PIP_RESP_BL_SOP_OFFSET] == PIP_SOP_KEY)
		return true;

	return false;
}

bool cyapa_sort_tsg_pip_app_resp_data(struct cyapa *cyapa,
		u8 *data, int len)
{
	struct cyapa_pip_cmd_states *pip = &cyapa->cmd_states.pip;
	int resp_len;

	if (!data || len < PIP_MIN_APP_RESP_LENGTH)
		return false;

	if (data[PIP_RESP_REPORT_ID_OFFSET] == PIP_APP_RESP_REPORT_ID &&
			data[PIP_RESP_RSVD_OFFSET] == PIP_RESP_RSVD_KEY) {
		resp_len = get_unaligned_le16(&data[PIP_RESP_LENGTH_OFFSET]);
		if (GET_PIP_CMD_CODE(data[PIP_RESP_APP_CMD_OFFSET]) == 0x00 &&
			resp_len == PIP_UNSUPPORTED_CMD_RESP_LENGTH &&
			data[5] == pip->in_progress_cmd) {
			/* Unsupported command code */
			return false;
		} else if (GET_PIP_CMD_CODE(data[PIP_RESP_APP_CMD_OFFSET]) ==
				pip->in_progress_cmd) {
			/* Correct command response received */
			return true;
		}
	}

	return false;
}

static bool cyapa_sort_pip_application_launch_data(struct cyapa *cyapa,
		u8 *buf, int len)
{
	if (buf == NULL || len < PIP_RESP_LENGTH_SIZE)
		return false;

	/*
	 * After reset or power on, trackpad device always sets to 0x00 0x00
	 * to indicate a reset or power on event.
	 */
	if (buf[0] == 0 && buf[1] == 0)
		return true;

	return false;
}

static bool cyapa_sort_gen5_hid_descriptor_data(struct cyapa *cyapa,
		u8 *buf, int len)
{
	int resp_len;
	int max_output_len;

	/* Check hid descriptor. */
	if (len != PIP_HID_DESCRIPTOR_SIZE)
		return false;

	resp_len = get_unaligned_le16(&buf[PIP_RESP_LENGTH_OFFSET]);
	max_output_len = get_unaligned_le16(&buf[16]);
	if (resp_len == PIP_HID_DESCRIPTOR_SIZE) {
		if (buf[PIP_RESP_REPORT_ID_OFFSET] == PIP_HID_BL_REPORT_ID &&
				max_output_len == GEN5_BL_MAX_OUTPUT_LENGTH) {
			/* BL mode HID Descriptor */
			return true;
		} else if ((buf[PIP_RESP_REPORT_ID_OFFSET] ==
				PIP_HID_APP_REPORT_ID) &&
				max_output_len == GEN5_APP_MAX_OUTPUT_LENGTH) {
			/* APP mode HID Descriptor */
			return true;
		}
	}

	return false;
}

static bool cyapa_sort_pip_deep_sleep_data(struct cyapa *cyapa,
		u8 *buf, int len)
{
	if (len == PIP_DEEP_SLEEP_RESP_LENGTH &&
		buf[PIP_RESP_REPORT_ID_OFFSET] ==
			PIP_APP_DEEP_SLEEP_REPORT_ID &&
		(buf[4] & PIP_DEEP_SLEEP_OPCODE_MASK) ==
			PIP_DEEP_SLEEP_OPCODE)
		return true;
	return false;
}

static int gen5_idle_state_parse(struct cyapa *cyapa)
{
	u8 resp_data[PIP_HID_DESCRIPTOR_SIZE];
	int max_output_len;
	int length;
	u8 cmd[2];
	int ret;
	int error;

	/*
	 * Dump all buffered data firstly for the situation
	 * when the trackpad is just power on the cyapa go here.
	 */
	cyapa_empty_pip_output_data(cyapa, NULL, NULL, NULL);

	memset(resp_data, 0, sizeof(resp_data));
	ret = cyapa_i2c_pip_read(cyapa, resp_data, 3);
	if (ret != 3)
		return ret < 0 ? ret : -EIO;

	length = get_unaligned_le16(&resp_data[PIP_RESP_LENGTH_OFFSET]);
	if (length == PIP_RESP_LENGTH_SIZE) {
		/* Normal state of Gen5 with no data to response */
		cyapa->gen = CYAPA_GEN5;

		cyapa_empty_pip_output_data(cyapa, NULL, NULL, NULL);

		/* Read description from trackpad device */
		cmd[0] = 0x01;
		cmd[1] = 0x00;
		length = PIP_HID_DESCRIPTOR_SIZE;
		error = cyapa_i2c_pip_cmd_irq_sync(cyapa,
				cmd, PIP_RESP_LENGTH_SIZE,
				resp_data, &length,
				300,
				cyapa_sort_gen5_hid_descriptor_data,
				false);
		if (error)
			return error;

		length = get_unaligned_le16(
				&resp_data[PIP_RESP_LENGTH_OFFSET]);
		max_output_len = get_unaligned_le16(&resp_data[16]);
		if ((length == PIP_HID_DESCRIPTOR_SIZE ||
				length == PIP_RESP_LENGTH_SIZE) &&
			(resp_data[PIP_RESP_REPORT_ID_OFFSET] ==
				PIP_HID_BL_REPORT_ID) &&
			max_output_len == GEN5_BL_MAX_OUTPUT_LENGTH) {
			/* BL mode HID Description read */
			cyapa->state = CYAPA_STATE_GEN5_BL;
		} else if ((length == PIP_HID_DESCRIPTOR_SIZE ||
				length == PIP_RESP_LENGTH_SIZE) &&
			(resp_data[PIP_RESP_REPORT_ID_OFFSET] ==
				PIP_HID_APP_REPORT_ID) &&
			max_output_len == GEN5_APP_MAX_OUTPUT_LENGTH) {
			/* APP mode HID Description read */
			cyapa->state = CYAPA_STATE_GEN5_APP;
		} else {
			/* Should not happen!!! */
			cyapa->state = CYAPA_STATE_NO_DEVICE;
		}
	}

	return 0;
}

static int gen5_hid_description_header_parse(struct cyapa *cyapa, u8 *reg_data)
{
	int length;
	u8 resp_data[32];
	int max_output_len;
	int ret;

	/* 0x20 0x00 0xF7 is Gen5 Application HID Description Header;
	 * 0x20 0x00 0xFF is Gen5 Bootloader HID Description Header.
	 *
	 * Must read HID Description content through out,
	 * otherwise Gen5 trackpad cannot response next command
	 * or report any touch or button data.
	 */
	ret = cyapa_i2c_pip_read(cyapa, resp_data,
			PIP_HID_DESCRIPTOR_SIZE);
	if (ret != PIP_HID_DESCRIPTOR_SIZE)
		return ret < 0 ? ret : -EIO;
	length = get_unaligned_le16(&resp_data[PIP_RESP_LENGTH_OFFSET]);
	max_output_len = get_unaligned_le16(&resp_data[16]);
	if (length == PIP_RESP_LENGTH_SIZE) {
		if (reg_data[PIP_RESP_REPORT_ID_OFFSET] ==
				PIP_HID_BL_REPORT_ID) {
			/*
			 * BL mode HID Description has been previously
			 * read out.
			 */
			cyapa->gen = CYAPA_GEN5;
			cyapa->state = CYAPA_STATE_GEN5_BL;
		} else {
			/*
			 * APP mode HID Description has been previously
			 * read out.
			 */
			cyapa->gen = CYAPA_GEN5;
			cyapa->state = CYAPA_STATE_GEN5_APP;
		}
	} else if (length == PIP_HID_DESCRIPTOR_SIZE &&
			resp_data[2] == PIP_HID_BL_REPORT_ID &&
			max_output_len == GEN5_BL_MAX_OUTPUT_LENGTH) {
		/* BL mode HID Description read. */
		cyapa->gen = CYAPA_GEN5;
		cyapa->state = CYAPA_STATE_GEN5_BL;
	} else if (length == PIP_HID_DESCRIPTOR_SIZE &&
			(resp_data[PIP_RESP_REPORT_ID_OFFSET] ==
				PIP_HID_APP_REPORT_ID) &&
			max_output_len == GEN5_APP_MAX_OUTPUT_LENGTH) {
		/* APP mode HID Description read. */
		cyapa->gen = CYAPA_GEN5;
		cyapa->state = CYAPA_STATE_GEN5_APP;
	} else {
		/* Should not happen!!! */
		cyapa->state = CYAPA_STATE_NO_DEVICE;
	}

	return 0;
}

static int gen5_report_data_header_parse(struct cyapa *cyapa, u8 *reg_data)
{
	int length;

	length = get_unaligned_le16(&reg_data[PIP_RESP_LENGTH_OFFSET]);
	switch (reg_data[PIP_RESP_REPORT_ID_OFFSET]) {
	case PIP_TOUCH_REPORT_ID:
		if (length < PIP_TOUCH_REPORT_HEAD_SIZE ||
			length > PIP_TOUCH_REPORT_MAX_SIZE)
			return -EINVAL;
		break;
	case PIP_BTN_REPORT_ID:
	case GEN5_OLD_PUSH_BTN_REPORT_ID:
	case PIP_PUSH_BTN_REPORT_ID:
		if (length < PIP_BTN_REPORT_HEAD_SIZE ||
			length > PIP_BTN_REPORT_MAX_SIZE)
			return -EINVAL;
		break;
	case PIP_WAKEUP_EVENT_REPORT_ID:
		if (length != PIP_WAKEUP_EVENT_SIZE)
			return -EINVAL;
		break;
	default:
		return -EINVAL;
	}

	cyapa->gen = CYAPA_GEN5;
	cyapa->state = CYAPA_STATE_GEN5_APP;
	return 0;
}

static int gen5_cmd_resp_header_parse(struct cyapa *cyapa, u8 *reg_data)
{
	struct cyapa_pip_cmd_states *pip = &cyapa->cmd_states.pip;
	int length;
	int ret;

	/*
	 * Must read report data through out,
	 * otherwise Gen5 trackpad cannot response next command
	 * or report any touch or button data.
	 */
	length = get_unaligned_le16(&reg_data[PIP_RESP_LENGTH_OFFSET]);
	ret = cyapa_i2c_pip_read(cyapa, pip->empty_buf, length);
	if (ret != length)
		return ret < 0 ? ret : -EIO;

	if (length == PIP_RESP_LENGTH_SIZE) {
		/* Previous command has read the data through out. */
		if (reg_data[PIP_RESP_REPORT_ID_OFFSET] ==
				PIP_BL_RESP_REPORT_ID) {
			/* Gen5 BL command response data detected */
			cyapa->gen = CYAPA_GEN5;
			cyapa->state = CYAPA_STATE_GEN5_BL;
		} else {
			/* Gen5 APP command response data detected */
			cyapa->gen = CYAPA_GEN5;
			cyapa->state = CYAPA_STATE_GEN5_APP;
		}
	} else if ((pip->empty_buf[PIP_RESP_REPORT_ID_OFFSET] ==
				PIP_BL_RESP_REPORT_ID) &&
			(pip->empty_buf[PIP_RESP_RSVD_OFFSET] ==
				PIP_RESP_RSVD_KEY) &&
			(pip->empty_buf[PIP_RESP_BL_SOP_OFFSET] ==
				PIP_SOP_KEY) &&
			(pip->empty_buf[length - 1] ==
				PIP_EOP_KEY)) {
		/* Gen5 BL command response data detected */
		cyapa->gen = CYAPA_GEN5;
		cyapa->state = CYAPA_STATE_GEN5_BL;
	} else if (pip->empty_buf[PIP_RESP_REPORT_ID_OFFSET] ==
				PIP_APP_RESP_REPORT_ID &&
			pip->empty_buf[PIP_RESP_RSVD_OFFSET] ==
				PIP_RESP_RSVD_KEY) {
		/* Gen5 APP command response data detected */
		cyapa->gen = CYAPA_GEN5;
		cyapa->state = CYAPA_STATE_GEN5_APP;
	} else {
		/* Should not happen!!! */
		cyapa->state = CYAPA_STATE_NO_DEVICE;
	}

	return 0;
}

static int cyapa_gen5_state_parse(struct cyapa *cyapa, u8 *reg_data, int len)
{
	int length;

	if (!reg_data || len < 3)
		return -EINVAL;

	cyapa->state = CYAPA_STATE_NO_DEVICE;

	/* Parse based on Gen5 characteristic registers and bits */
	length = get_unaligned_le16(&reg_data[PIP_RESP_LENGTH_OFFSET]);
	if (length == 0 || length == PIP_RESP_LENGTH_SIZE) {
		gen5_idle_state_parse(cyapa);
	} else if (length == PIP_HID_DESCRIPTOR_SIZE &&
			(reg_data[2] == PIP_HID_BL_REPORT_ID ||
				reg_data[2] == PIP_HID_APP_REPORT_ID)) {
		gen5_hid_description_header_parse(cyapa, reg_data);
	} else if ((length == GEN5_APP_REPORT_DESCRIPTOR_SIZE ||
			length == GEN5_APP_CONTRACT_REPORT_DESCRIPTOR_SIZE) &&
			reg_data[2] == GEN5_APP_REPORT_DESCRIPTOR_ID) {
		/* 0xEE 0x00 0xF6 is Gen5 APP report description header. */
		cyapa->gen = CYAPA_GEN5;
		cyapa->state = CYAPA_STATE_GEN5_APP;
	} else if (length == GEN5_BL_REPORT_DESCRIPTOR_SIZE &&
			reg_data[2] == GEN5_BL_REPORT_DESCRIPTOR_ID) {
		/* 0x1D 0x00 0xFE is Gen5 BL report descriptor header. */
		cyapa->gen = CYAPA_GEN5;
		cyapa->state = CYAPA_STATE_GEN5_BL;
	} else if (reg_data[2] == PIP_TOUCH_REPORT_ID ||
			reg_data[2] == PIP_BTN_REPORT_ID ||
			reg_data[2] == GEN5_OLD_PUSH_BTN_REPORT_ID ||
			reg_data[2] == PIP_PUSH_BTN_REPORT_ID ||
			reg_data[2] == PIP_WAKEUP_EVENT_REPORT_ID) {
		gen5_report_data_header_parse(cyapa, reg_data);
	} else if (reg_data[2] == PIP_BL_RESP_REPORT_ID ||
			reg_data[2] == PIP_APP_RESP_REPORT_ID) {
		gen5_cmd_resp_header_parse(cyapa, reg_data);
	}

	if (cyapa->gen == CYAPA_GEN5) {
		/*
		 * Must read the content (e.g.: report description and so on)
		 * from trackpad device throughout. Otherwise,
		 * Gen5 trackpad cannot response to next command or
		 * report any touch or button data later.
		 */
		cyapa_empty_pip_output_data(cyapa, NULL, NULL, NULL);

		if (cyapa->state == CYAPA_STATE_GEN5_APP ||
			cyapa->state == CYAPA_STATE_GEN5_BL)
			return 0;
	}

	return -EAGAIN;
}

static struct cyapa_tsg_bin_image_data_record *
cyapa_get_image_record_data_num(const struct firmware *fw,
		int *record_num)
{
	int head_size;

	head_size = fw->data[0] + 1;
	*record_num = (fw->size - head_size) /
			sizeof(struct cyapa_tsg_bin_image_data_record);
	return (struct cyapa_tsg_bin_image_data_record *)&fw->data[head_size];
}

int cyapa_pip_bl_initiate(struct cyapa *cyapa, const struct firmware *fw)
{
	struct cyapa_tsg_bin_image_data_record *image_records;
	struct pip_bl_cmd_head *bl_cmd_head;
	struct pip_bl_packet_start *bl_packet_start;
	struct pip_bl_initiate_cmd_data *cmd_data;
	struct pip_bl_packet_end *bl_packet_end;
	u8 cmd[CYAPA_TSG_MAX_CMD_SIZE];
	int cmd_len;
	u16 cmd_data_len;
	u16 cmd_crc = 0;
	u16 meta_data_crc = 0;
	u8 resp_data[11];
	int resp_len;
	int records_num;
	u8 *data;
	int error;

	/* Try to dump all buffered report data before any send command. */
	cyapa_empty_pip_output_data(cyapa, NULL, NULL, NULL);

	memset(cmd, 0, CYAPA_TSG_MAX_CMD_SIZE);
	bl_cmd_head = (struct pip_bl_cmd_head *)cmd;
	cmd_data_len = CYAPA_TSG_BL_KEY_SIZE + CYAPA_TSG_FLASH_MAP_BLOCK_SIZE;
	cmd_len = sizeof(struct pip_bl_cmd_head) + cmd_data_len +
		  sizeof(struct pip_bl_packet_end);

	put_unaligned_le16(PIP_OUTPUT_REPORT_ADDR, &bl_cmd_head->addr);
	put_unaligned_le16(cmd_len - 2, &bl_cmd_head->length);
	bl_cmd_head->report_id = PIP_BL_CMD_REPORT_ID;

	bl_packet_start = &bl_cmd_head->packet_start;
	bl_packet_start->sop = PIP_SOP_KEY;
	bl_packet_start->cmd_code = PIP_BL_CMD_INITIATE_BL;
	/* 8 key bytes and 128 bytes block size */
	put_unaligned_le16(cmd_data_len, &bl_packet_start->data_length);

	cmd_data = (struct pip_bl_initiate_cmd_data *)bl_cmd_head->data;
	memcpy(cmd_data->key, cyapa_pip_bl_cmd_key, CYAPA_TSG_BL_KEY_SIZE);

	image_records = cyapa_get_image_record_data_num(fw, &records_num);

	/* APP_INTEGRITY row is always the last row block */
	data = image_records[records_num - 1].record_data;
	memcpy(cmd_data->metadata_raw_parameter, data,
		CYAPA_TSG_FLASH_MAP_METADATA_SIZE);

	meta_data_crc = crc_itu_t(0xffff, cmd_data->metadata_raw_parameter,
				CYAPA_TSG_FLASH_MAP_METADATA_SIZE);
	put_unaligned_le16(meta_data_crc, &cmd_data->metadata_crc);

	bl_packet_end = (struct pip_bl_packet_end *)(bl_cmd_head->data +
				cmd_data_len);
	cmd_crc = crc_itu_t(0xffff, (u8 *)bl_packet_start,
		sizeof(struct pip_bl_packet_start) + cmd_data_len);
	put_unaligned_le16(cmd_crc, &bl_packet_end->crc);
	bl_packet_end->eop = PIP_EOP_KEY;

	resp_len = sizeof(resp_data);
	error = cyapa_i2c_pip_cmd_irq_sync(cyapa,
			cmd, cmd_len,
			resp_data, &resp_len, 12000,
			cyapa_sort_tsg_pip_bl_resp_data, true);
	if (error || resp_len != PIP_BL_INITIATE_RESP_LEN ||
			resp_data[2] != PIP_BL_RESP_REPORT_ID ||
			!PIP_CMD_COMPLETE_SUCCESS(resp_data))
		return error ? error : -EAGAIN;

	return 0;
}

static bool cyapa_sort_pip_bl_exit_data(struct cyapa *cyapa, u8 *buf, int len)
{
	if (buf == NULL || len < PIP_RESP_LENGTH_SIZE)
		return false;

	if (buf[0] == 0 && buf[1] == 0)
		return true;

	/* Exit bootloader failed for some reason. */
	if (len == PIP_BL_FAIL_EXIT_RESP_LEN &&
			buf[PIP_RESP_REPORT_ID_OFFSET] ==
				PIP_BL_RESP_REPORT_ID &&
			buf[PIP_RESP_RSVD_OFFSET] == PIP_RESP_RSVD_KEY &&
			buf[PIP_RESP_BL_SOP_OFFSET] == PIP_SOP_KEY &&
			buf[10] == PIP_EOP_KEY)
		return true;

	return false;
}

int cyapa_pip_bl_exit(struct cyapa *cyapa)
{

	u8 bl_gen5_bl_exit[] = { 0x04, 0x00,
		0x0B, 0x00, 0x40, 0x00, 0x01, 0x3b, 0x00, 0x00,
		0x20, 0xc7, 0x17
	};
	u8 resp_data[11];
	int resp_len;
	int error;

	resp_len = sizeof(resp_data);
	error = cyapa_i2c_pip_cmd_irq_sync(cyapa,
			bl_gen5_bl_exit, sizeof(bl_gen5_bl_exit),
			resp_data, &resp_len,
			5000, cyapa_sort_pip_bl_exit_data, false);
	if (error)
		return error;

	if (resp_len == PIP_BL_FAIL_EXIT_RESP_LEN ||
			resp_data[PIP_RESP_REPORT_ID_OFFSET] ==
				PIP_BL_RESP_REPORT_ID)
		return -EAGAIN;

	if (resp_data[0] == 0x00 && resp_data[1] == 0x00)
		return 0;

	return -ENODEV;
}

int cyapa_pip_bl_enter(struct cyapa *cyapa)
{
	u8 cmd[] = { 0x04, 0x00, 0x05, 0x00, 0x2F, 0x00, 0x01 };
	u8 resp_data[2];
	int resp_len;
	int error;

	error = cyapa_poll_state(cyapa, 500);
	if (error < 0)
		return error;

	/* Already in bootloader mode, Skipping exit. */
	if (cyapa_is_pip_bl_mode(cyapa))
		return 0;
	else if (!cyapa_is_pip_app_mode(cyapa))
		return -EINVAL;

	/* Try to dump all buffered report data before any send command. */
	cyapa_empty_pip_output_data(cyapa, NULL, NULL, NULL);

	/*
	 * Send bootloader enter command to trackpad device,
	 * after enter bootloader, the response data is two bytes of 0x00 0x00.
	 */
	resp_len = sizeof(resp_data);
	memset(resp_data, 0, resp_len);
	error = cyapa_i2c_pip_cmd_irq_sync(cyapa,
			cmd, sizeof(cmd),
			resp_data, &resp_len,
			5000, cyapa_sort_pip_application_launch_data,
			true);
	if (error || resp_data[0] != 0x00 || resp_data[1] != 0x00)
		return error < 0 ? error : -EAGAIN;

	cyapa->operational = false;
	if (cyapa->gen == CYAPA_GEN5)
		cyapa->state = CYAPA_STATE_GEN5_BL;
	else if (cyapa->gen == CYAPA_GEN6)
		cyapa->state = CYAPA_STATE_GEN6_BL;
	return 0;
}

static int cyapa_pip_fw_head_check(struct cyapa *cyapa,
		struct cyapa_tsg_bin_image_head *image_head)
{
	if (image_head->head_size != 0x0C && image_head->head_size != 0x12)
		return -EINVAL;

	switch (cyapa->gen) {
	case CYAPA_GEN6:
		if (image_head->family_id != 0x9B ||
		    image_head->silicon_id_hi != 0x0B)
			return -EINVAL;
		break;
	case CYAPA_GEN5:
		/* Gen5 without proximity support. */
		if (cyapa->platform_ver < 2) {
			if (image_head->head_size == 0x0C)
				break;
			return -EINVAL;
		}

		if (image_head->family_id != 0x91 ||
		    image_head->silicon_id_hi != 0x02)
			return -EINVAL;
		break;
	default:
		return -EINVAL;
	}

	return 0;
}

int cyapa_pip_check_fw(struct cyapa *cyapa, const struct firmware *fw)
{
	struct device *dev = &cyapa->client->dev;
	struct cyapa_tsg_bin_image_data_record *image_records;
	const struct cyapa_tsg_bin_image_data_record *app_integrity;
	const struct tsg_bl_metadata_row_params *metadata;
	int flash_records_count;
	u32 fw_app_start, fw_upgrade_start;
	u16 fw_app_len, fw_upgrade_len;
	u16 app_crc;
	u16 app_integrity_crc;
	int i;

	/* Verify the firmware image not miss-used for Gen5 and Gen6. */
	if (cyapa_pip_fw_head_check(cyapa,
		(struct cyapa_tsg_bin_image_head *)fw->data)) {
		dev_err(dev, "%s: firmware image not match TP device.\n",
			     __func__);
		return -EINVAL;
	}

	image_records =
		cyapa_get_image_record_data_num(fw, &flash_records_count);

	/*
	 * APP_INTEGRITY row is always the last row block,
	 * and the row id must be 0x01ff.
	 */
	app_integrity = &image_records[flash_records_count - 1];

	if (app_integrity->flash_array_id != 0x00 ||
	    get_unaligned_be16(&app_integrity->row_number) != 0x01ff) {
		dev_err(dev, "%s: invalid app_integrity data.\n", __func__);
		return -EINVAL;
	}

	metadata = (const void *)app_integrity->record_data;

	/* Verify app_integrity crc */
	app_integrity_crc = crc_itu_t(0xffff, app_integrity->record_data,
				      CYAPA_TSG_APP_INTEGRITY_SIZE);
	if (app_integrity_crc != get_unaligned_le16(&metadata->metadata_crc)) {
		dev_err(dev, "%s: invalid app_integrity crc.\n", __func__);
		return -EINVAL;
	}

	fw_app_start = get_unaligned_le32(&metadata->app_start);
	fw_app_len = get_unaligned_le16(&metadata->app_len);
	fw_upgrade_start = get_unaligned_le32(&metadata->upgrade_start);
	fw_upgrade_len = get_unaligned_le16(&metadata->upgrade_len);

	if (fw_app_start % CYAPA_TSG_FW_ROW_SIZE ||
	    fw_app_len % CYAPA_TSG_FW_ROW_SIZE ||
	    fw_upgrade_start % CYAPA_TSG_FW_ROW_SIZE ||
	    fw_upgrade_len % CYAPA_TSG_FW_ROW_SIZE) {
		dev_err(dev, "%s: invalid image alignment.\n", __func__);
		return -EINVAL;
	}

	/* Verify application image CRC. */
	app_crc = 0xffffU;
	for (i = 0; i < fw_app_len / CYAPA_TSG_FW_ROW_SIZE; i++) {
		const u8 *data = image_records[i].record_data;

		app_crc = crc_itu_t(app_crc, data, CYAPA_TSG_FW_ROW_SIZE);
	}

	if (app_crc != get_unaligned_le16(&metadata->app_crc)) {
		dev_err(dev, "%s: invalid firmware app crc check.\n", __func__);
		return -EINVAL;
	}

	return 0;
}

static int cyapa_pip_write_fw_block(struct cyapa *cyapa,
		struct cyapa_tsg_bin_image_data_record *flash_record)
{
	struct pip_bl_cmd_head *bl_cmd_head;
	struct pip_bl_packet_start *bl_packet_start;
	struct tsg_bl_flash_row_head *flash_row_head;
	struct pip_bl_packet_end *bl_packet_end;
	u8 cmd[CYAPA_TSG_MAX_CMD_SIZE];
	u16 cmd_len;
	u8 flash_array_id;
	u16 flash_row_id;
	u16 record_len;
	u8 *record_data;
	u16 data_len;
	u16 crc;
	u8 resp_data[11];
	int resp_len;
	int error;

	flash_array_id = flash_record->flash_array_id;
	flash_row_id = get_unaligned_be16(&flash_record->row_number);
	record_len = get_unaligned_be16(&flash_record->record_len);
	record_data = flash_record->record_data;

	memset(cmd, 0, CYAPA_TSG_MAX_CMD_SIZE);
	bl_cmd_head = (struct pip_bl_cmd_head *)cmd;
	bl_packet_start = &bl_cmd_head->packet_start;
	cmd_len = sizeof(struct pip_bl_cmd_head) +
		  sizeof(struct tsg_bl_flash_row_head) +
		  CYAPA_TSG_FLASH_MAP_BLOCK_SIZE +
		  sizeof(struct pip_bl_packet_end);

	put_unaligned_le16(PIP_OUTPUT_REPORT_ADDR, &bl_cmd_head->addr);
	/* Don't include 2 bytes register address */
	put_unaligned_le16(cmd_len - 2, &bl_cmd_head->length);
	bl_cmd_head->report_id = PIP_BL_CMD_REPORT_ID;
	bl_packet_start->sop = PIP_SOP_KEY;
	bl_packet_start->cmd_code = PIP_BL_CMD_PROGRAM_VERIFY_ROW;

	/* 1 (Flash Array ID) + 2 (Flash Row ID) + 128 (flash data) */
	data_len = sizeof(struct tsg_bl_flash_row_head) + record_len;
	put_unaligned_le16(data_len, &bl_packet_start->data_length);

	flash_row_head = (struct tsg_bl_flash_row_head *)bl_cmd_head->data;
	flash_row_head->flash_array_id = flash_array_id;
	put_unaligned_le16(flash_row_id, &flash_row_head->flash_row_id);
	memcpy(flash_row_head->flash_data, record_data, record_len);

	bl_packet_end = (struct pip_bl_packet_end *)(bl_cmd_head->data +
						      data_len);
	crc = crc_itu_t(0xffff, (u8 *)bl_packet_start,
		sizeof(struct pip_bl_packet_start) + data_len);
	put_unaligned_le16(crc, &bl_packet_end->crc);
	bl_packet_end->eop = PIP_EOP_KEY;

	resp_len = sizeof(resp_data);
	error = cyapa_i2c_pip_cmd_irq_sync(cyapa, cmd, cmd_len,
			resp_data, &resp_len,
			500, cyapa_sort_tsg_pip_bl_resp_data, true);
	if (error || resp_len != PIP_BL_BLOCK_WRITE_RESP_LEN ||
			resp_data[2] != PIP_BL_RESP_REPORT_ID ||
			!PIP_CMD_COMPLETE_SUCCESS(resp_data))
		return error < 0 ? error : -EAGAIN;

	return 0;
}

int cyapa_pip_do_fw_update(struct cyapa *cyapa,
		const struct firmware *fw)
{
	struct device *dev = &cyapa->client->dev;
	struct cyapa_tsg_bin_image_data_record *image_records;
	int flash_records_count;
	int i;
	int error;

	cyapa_empty_pip_output_data(cyapa, NULL, NULL, NULL);

	image_records =
		cyapa_get_image_record_data_num(fw, &flash_records_count);

	/*
	 * The last flash row 0x01ff has been written through bl_initiate
	 * command, so DO NOT write flash 0x01ff to trackpad device.
	 */
	for (i = 0; i < (flash_records_count - 1); i++) {
		error = cyapa_pip_write_fw_block(cyapa, &image_records[i]);
		if (error) {
			dev_err(dev, "%s: Gen5 FW update aborted: %d\n",
				__func__, error);
			return error;
		}
	}

	return 0;
}

static int cyapa_gen5_change_power_state(struct cyapa *cyapa, u8 power_state)
{
	u8 cmd[8] = { 0x04, 0x00, 0x06, 0x00, 0x2f, 0x00, 0x08, 0x01 };
	u8 resp_data[6];
	int resp_len;
	int error;

	cmd[7] = power_state;
	resp_len = sizeof(resp_data);
	error = cyapa_i2c_pip_cmd_irq_sync(cyapa, cmd, sizeof(cmd),
			resp_data, &resp_len,
			500, cyapa_sort_tsg_pip_app_resp_data, false);
	if (error || !VALID_CMD_RESP_HEADER(resp_data, 0x08) ||
			!PIP_CMD_COMPLETE_SUCCESS(resp_data))
		return error < 0 ? error : -EINVAL;

	return 0;
}

static int cyapa_gen5_set_interval_time(struct cyapa *cyapa,
		u8 parameter_id, u16 interval_time)
{
	struct pip_app_cmd_head *app_cmd_head;
	struct gen5_app_set_parameter_data *parameter_data;
	u8 cmd[CYAPA_TSG_MAX_CMD_SIZE];
	int cmd_len;
	u8 resp_data[7];
	int resp_len;
	u8 parameter_size;
	int error;

	memset(cmd, 0, CYAPA_TSG_MAX_CMD_SIZE);
	app_cmd_head = (struct pip_app_cmd_head *)cmd;
	parameter_data = (struct gen5_app_set_parameter_data *)
			 app_cmd_head->parameter_data;
	cmd_len = sizeof(struct pip_app_cmd_head) +
		  sizeof(struct gen5_app_set_parameter_data);

	switch (parameter_id) {
	case GEN5_PARAMETER_ACT_INTERVL_ID:
		parameter_size = GEN5_PARAMETER_ACT_INTERVL_SIZE;
		break;
	case GEN5_PARAMETER_ACT_LFT_INTERVL_ID:
		parameter_size = GEN5_PARAMETER_ACT_LFT_INTERVL_SIZE;
		break;
	case GEN5_PARAMETER_LP_INTRVL_ID:
		parameter_size = GEN5_PARAMETER_LP_INTRVL_SIZE;
		break;
	default:
		return -EINVAL;
	}

	put_unaligned_le16(PIP_OUTPUT_REPORT_ADDR, &app_cmd_head->addr);
	/*
	 * Don't include unused parameter value bytes and
	 * 2 bytes register address.
	 */
	put_unaligned_le16(cmd_len - (4 - parameter_size) - 2,
			   &app_cmd_head->length);
	app_cmd_head->report_id = PIP_APP_CMD_REPORT_ID;
	app_cmd_head->cmd_code = GEN5_CMD_SET_PARAMETER;
	parameter_data->parameter_id = parameter_id;
	parameter_data->parameter_size = parameter_size;
	put_unaligned_le32((u32)interval_time, &parameter_data->value);
	resp_len = sizeof(resp_data);
	error = cyapa_i2c_pip_cmd_irq_sync(cyapa, cmd, cmd_len,
			resp_data, &resp_len,
			500, cyapa_sort_tsg_pip_app_resp_data, false);
	if (error || resp_data[5] != parameter_id ||
		resp_data[6] != parameter_size ||
		!VALID_CMD_RESP_HEADER(resp_data, GEN5_CMD_SET_PARAMETER))
		return error < 0 ? error : -EINVAL;

	return 0;
}

static int cyapa_gen5_get_interval_time(struct cyapa *cyapa,
		u8 parameter_id, u16 *interval_time)
{
	struct pip_app_cmd_head *app_cmd_head;
	struct gen5_app_get_parameter_data *parameter_data;
	u8 cmd[CYAPA_TSG_MAX_CMD_SIZE];
	int cmd_len;
	u8 resp_data[11];
	int resp_len;
	u8 parameter_size;
	u16 mask, i;
	int error;

	memset(cmd, 0, CYAPA_TSG_MAX_CMD_SIZE);
	app_cmd_head = (struct pip_app_cmd_head *)cmd;
	parameter_data = (struct gen5_app_get_parameter_data *)
			 app_cmd_head->parameter_data;
	cmd_len = sizeof(struct pip_app_cmd_head) +
		  sizeof(struct gen5_app_get_parameter_data);

	*interval_time = 0;
	switch (parameter_id) {
	case GEN5_PARAMETER_ACT_INTERVL_ID:
		parameter_size = GEN5_PARAMETER_ACT_INTERVL_SIZE;
		break;
	case GEN5_PARAMETER_ACT_LFT_INTERVL_ID:
		parameter_size = GEN5_PARAMETER_ACT_LFT_INTERVL_SIZE;
		break;
	case GEN5_PARAMETER_LP_INTRVL_ID:
		parameter_size = GEN5_PARAMETER_LP_INTRVL_SIZE;
		break;
	default:
		return -EINVAL;
	}

	put_unaligned_le16(PIP_OUTPUT_REPORT_ADDR, &app_cmd_head->addr);
	/* Don't include 2 bytes register address */
	put_unaligned_le16(cmd_len - 2, &app_cmd_head->length);
	app_cmd_head->report_id = PIP_APP_CMD_REPORT_ID;
	app_cmd_head->cmd_code = GEN5_CMD_GET_PARAMETER;
	parameter_data->parameter_id = parameter_id;

	resp_len = sizeof(resp_data);
	error = cyapa_i2c_pip_cmd_irq_sync(cyapa, cmd, cmd_len,
			resp_data, &resp_len,
			500, cyapa_sort_tsg_pip_app_resp_data, false);
	if (error || resp_data[5] != parameter_id || resp_data[6] == 0 ||
		!VALID_CMD_RESP_HEADER(resp_data, GEN5_CMD_GET_PARAMETER))
		return error < 0 ? error : -EINVAL;

	mask = 0;
	for (i = 0; i < parameter_size; i++)
		mask |= (0xff << (i * 8));
	*interval_time = get_unaligned_le16(&resp_data[7]) & mask;

	return 0;
}

static int cyapa_gen5_disable_pip_report(struct cyapa *cyapa)
{
	struct pip_app_cmd_head *app_cmd_head;
	u8 cmd[10];
	u8 resp_data[7];
	int resp_len;
	int error;

	memset(cmd, 0, sizeof(cmd));
	app_cmd_head = (struct pip_app_cmd_head *)cmd;

	put_unaligned_le16(PIP_OUTPUT_REPORT_ADDR, &app_cmd_head->addr);
	put_unaligned_le16(sizeof(cmd) - 2, &app_cmd_head->length);
	app_cmd_head->report_id = PIP_APP_CMD_REPORT_ID;
	app_cmd_head->cmd_code = GEN5_CMD_SET_PARAMETER;
	app_cmd_head->parameter_data[0] = GEN5_PARAMETER_DISABLE_PIP_REPORT;
	app_cmd_head->parameter_data[1] = 0x01;
	app_cmd_head->parameter_data[2] = 0x01;
	resp_len = sizeof(resp_data);
	error = cyapa_i2c_pip_cmd_irq_sync(cyapa, cmd, sizeof(cmd),
			resp_data, &resp_len,
			500, cyapa_sort_tsg_pip_app_resp_data, false);
	if (error || resp_data[5] != GEN5_PARAMETER_DISABLE_PIP_REPORT ||
		!VALID_CMD_RESP_HEADER(resp_data, GEN5_CMD_SET_PARAMETER) ||
		resp_data[6] != 0x01)
		return error < 0 ? error : -EINVAL;

	return 0;
}

int cyapa_pip_set_proximity(struct cyapa *cyapa, bool enable)
{
	u8 cmd[] = { 0x04, 0x00, 0x06, 0x00, 0x2f, 0x00, PIP_SET_PROXIMITY,
		     (u8)!!enable
	};
	u8 resp_data[6];
	int resp_len;
	int error;

	resp_len = sizeof(resp_data);
	error = cyapa_i2c_pip_cmd_irq_sync(cyapa, cmd, sizeof(cmd),
			resp_data, &resp_len,
			500, cyapa_sort_tsg_pip_app_resp_data, false);
	if (error || !VALID_CMD_RESP_HEADER(resp_data, PIP_SET_PROXIMITY) ||
			!PIP_CMD_COMPLETE_SUCCESS(resp_data)) {
		error = (error == -ETIMEDOUT) ? -EOPNOTSUPP : error;
		return error < 0 ? error : -EINVAL;
	}

	return 0;
}

int cyapa_pip_deep_sleep(struct cyapa *cyapa, u8 state)
{
	u8 cmd[] = { 0x05, 0x00, 0x00, 0x08};
	u8 resp_data[5];
	int resp_len;
	int error;

	cmd[2] = state & PIP_DEEP_SLEEP_STATE_MASK;
	resp_len = sizeof(resp_data);
	error = cyapa_i2c_pip_cmd_irq_sync(cyapa, cmd, sizeof(cmd),
			resp_data, &resp_len,
			500, cyapa_sort_pip_deep_sleep_data, false);
	if (error || ((resp_data[3] & PIP_DEEP_SLEEP_STATE_MASK) != state))
		return -EINVAL;

	return 0;
}

static int cyapa_gen5_set_power_mode(struct cyapa *cyapa,
		u8 power_mode, u16 sleep_time, enum cyapa_pm_stage pm_stage)
{
	struct device *dev = &cyapa->client->dev;
	u8 power_state;
	int error = 0;

	if (cyapa->state != CYAPA_STATE_GEN5_APP)
		return 0;

	cyapa_set_pip_pm_state(cyapa, pm_stage);

	if (PIP_DEV_GET_PWR_STATE(cyapa) == UNINIT_PWR_MODE) {
		/*
		 * Assume TP in deep sleep mode when driver is loaded,
		 * avoid driver unload and reload command IO issue caused by TP
		 * has been set into deep sleep mode when unloading.
		 */
		PIP_DEV_SET_PWR_STATE(cyapa, PWR_MODE_OFF);
	}

	if (PIP_DEV_UNINIT_SLEEP_TIME(cyapa) &&
			PIP_DEV_GET_PWR_STATE(cyapa) != PWR_MODE_OFF)
		if (cyapa_gen5_get_interval_time(cyapa,
				GEN5_PARAMETER_LP_INTRVL_ID,
				&cyapa->dev_sleep_time) != 0)
			PIP_DEV_SET_SLEEP_TIME(cyapa, UNINIT_SLEEP_TIME);

	if (PIP_DEV_GET_PWR_STATE(cyapa) == power_mode) {
		if (power_mode == PWR_MODE_OFF ||
			power_mode == PWR_MODE_FULL_ACTIVE ||
			power_mode == PWR_MODE_BTN_ONLY ||
			PIP_DEV_GET_SLEEP_TIME(cyapa) == sleep_time) {
			/* Has in correct power mode state, early return. */
			goto out;
		}
	}

	if (power_mode == PWR_MODE_OFF) {
		error = cyapa_pip_deep_sleep(cyapa, PIP_DEEP_SLEEP_STATE_OFF);
		if (error) {
			dev_err(dev, "enter deep sleep fail: %d\n", error);
			goto out;
		}

		PIP_DEV_SET_PWR_STATE(cyapa, PWR_MODE_OFF);
		goto out;
	}

	/*
	 * When trackpad in power off mode, it cannot change to other power
	 * state directly, must be wake up from sleep firstly, then
	 * continue to do next power sate change.
	 */
	if (PIP_DEV_GET_PWR_STATE(cyapa) == PWR_MODE_OFF) {
		error = cyapa_pip_deep_sleep(cyapa, PIP_DEEP_SLEEP_STATE_ON);
		if (error) {
			dev_err(dev, "deep sleep wake fail: %d\n", error);
			goto out;
		}
	}

	if (power_mode == PWR_MODE_FULL_ACTIVE) {
		error = cyapa_gen5_change_power_state(cyapa,
				GEN5_POWER_STATE_ACTIVE);
		if (error) {
			dev_err(dev, "change to active fail: %d\n", error);
			goto out;
		}

		PIP_DEV_SET_PWR_STATE(cyapa, PWR_MODE_FULL_ACTIVE);
	} else if (power_mode == PWR_MODE_BTN_ONLY) {
		error = cyapa_gen5_change_power_state(cyapa,
				GEN5_POWER_STATE_BTN_ONLY);
		if (error) {
			dev_err(dev, "fail to button only mode: %d\n", error);
			goto out;
		}

		PIP_DEV_SET_PWR_STATE(cyapa, PWR_MODE_BTN_ONLY);
	} else {
		/*
		 * Continue to change power mode even failed to set
		 * interval time, it won't affect the power mode change.
		 * except the sleep interval time is not correct.
		 */
		if (PIP_DEV_UNINIT_SLEEP_TIME(cyapa) ||
				sleep_time != PIP_DEV_GET_SLEEP_TIME(cyapa))
			if (cyapa_gen5_set_interval_time(cyapa,
					GEN5_PARAMETER_LP_INTRVL_ID,
					sleep_time) == 0)
				PIP_DEV_SET_SLEEP_TIME(cyapa, sleep_time);

		if (sleep_time <= GEN5_POWER_READY_MAX_INTRVL_TIME)
			power_state = GEN5_POWER_STATE_READY;
		else
			power_state = GEN5_POWER_STATE_IDLE;
		error = cyapa_gen5_change_power_state(cyapa, power_state);
		if (error) {
			dev_err(dev, "set power state to 0x%02x failed: %d\n",
				power_state, error);
			goto out;
		}

		/*
		 * Disable pip report for a little time, firmware will
		 * re-enable it automatically. It's used to fix the issue
		 * that trackpad unable to report signal to wake system up
		 * in the special situation that system is in suspending, and
		 * at the same time, user touch trackpad to wake system up.
		 * This function can avoid the data to be buffered when system
		 * is suspending which may cause interrupt line unable to be
		 * asserted again.
		 */
		if (pm_stage == CYAPA_PM_SUSPEND)
			cyapa_gen5_disable_pip_report(cyapa);

		PIP_DEV_SET_PWR_STATE(cyapa,
			cyapa_sleep_time_to_pwr_cmd(sleep_time));
	}

out:
	cyapa_reset_pip_pm_state(cyapa);
	return error;
}

int cyapa_pip_resume_scanning(struct cyapa *cyapa)
{
	u8 cmd[] = { 0x04, 0x00, 0x05, 0x00, 0x2f, 0x00, 0x04 };
	u8 resp_data[6];
	int resp_len;
	int error;

	/* Try to dump all buffered data before doing command. */
	cyapa_empty_pip_output_data(cyapa, NULL, NULL, NULL);

	resp_len = sizeof(resp_data);
	error = cyapa_i2c_pip_cmd_irq_sync(cyapa,
			cmd, sizeof(cmd),
			resp_data, &resp_len,
			500, cyapa_sort_tsg_pip_app_resp_data, true);
	if (error || !VALID_CMD_RESP_HEADER(resp_data, 0x04))
		return -EINVAL;

	/* Try to dump all buffered data when resuming scanning. */
	cyapa_empty_pip_output_data(cyapa, NULL, NULL, NULL);

	return 0;
}

int cyapa_pip_suspend_scanning(struct cyapa *cyapa)
{
	u8 cmd[] = { 0x04, 0x00, 0x05, 0x00, 0x2f, 0x00, 0x03 };
	u8 resp_data[6];
	int resp_len;
	int error;

	/* Try to dump all buffered data before doing command. */
	cyapa_empty_pip_output_data(cyapa, NULL, NULL, NULL);

	resp_len = sizeof(resp_data);
	error = cyapa_i2c_pip_cmd_irq_sync(cyapa,
			cmd, sizeof(cmd),
			resp_data, &resp_len,
			500, cyapa_sort_tsg_pip_app_resp_data, true);
	if (error || !VALID_CMD_RESP_HEADER(resp_data, 0x03))
		return -EINVAL;

	/* Try to dump all buffered data when suspending scanning. */
	cyapa_empty_pip_output_data(cyapa, NULL, NULL, NULL);

	return 0;
}

static int cyapa_pip_calibrate_pwcs(struct cyapa *cyapa,
		u8 calibrate_sensing_mode_type)
{
	struct pip_app_cmd_head *app_cmd_head;
	u8 cmd[8];
	u8 resp_data[6];
	int resp_len;
	int error;

	/* Try to dump all buffered data before doing command. */
	cyapa_empty_pip_output_data(cyapa, NULL, NULL, NULL);

	memset(cmd, 0, sizeof(cmd));
	app_cmd_head = (struct pip_app_cmd_head *)cmd;
	put_unaligned_le16(PIP_OUTPUT_REPORT_ADDR, &app_cmd_head->addr);
	put_unaligned_le16(sizeof(cmd) - 2, &app_cmd_head->length);
	app_cmd_head->report_id = PIP_APP_CMD_REPORT_ID;
	app_cmd_head->cmd_code = PIP_CMD_CALIBRATE;
	app_cmd_head->parameter_data[0] = calibrate_sensing_mode_type;
	resp_len = sizeof(resp_data);
	error = cyapa_i2c_pip_cmd_irq_sync(cyapa,
			cmd, sizeof(cmd),
			resp_data, &resp_len,
			5000, cyapa_sort_tsg_pip_app_resp_data, true);
	if (error || !VALID_CMD_RESP_HEADER(resp_data, PIP_CMD_CALIBRATE) ||
			!PIP_CMD_COMPLETE_SUCCESS(resp_data))
		return error < 0 ? error : -EAGAIN;

	return 0;
}

ssize_t cyapa_pip_do_calibrate(struct device *dev,
				     struct device_attribute *attr,
				     const char *buf, size_t count)
{
	struct cyapa *cyapa = dev_get_drvdata(dev);
	int error, calibrate_error;

	/* 1. Suspend Scanning*/
	error = cyapa_pip_suspend_scanning(cyapa);
	if (error)
		return error;

	/* 2. Do mutual capacitance fine calibrate. */
	calibrate_error = cyapa_pip_calibrate_pwcs(cyapa,
				PIP_SENSING_MODE_MUTUAL_CAP_FINE);
	if (calibrate_error)
		goto resume_scanning;

	/* 3. Do self capacitance calibrate. */
	calibrate_error = cyapa_pip_calibrate_pwcs(cyapa,
				PIP_SENSING_MODE_SELF_CAP);
	if (calibrate_error)
		goto resume_scanning;

resume_scanning:
	/* 4. Resume Scanning*/
	error = cyapa_pip_resume_scanning(cyapa);
	if (error || calibrate_error)
		return error ? error : calibrate_error;

	return count;
}

static s32 twos_complement_to_s32(s32 value, int num_bits)
{
	if (value >> (num_bits - 1))
		value |=  -1 << num_bits;
	return value;
}

static s32 cyapa_parse_structure_data(u8 data_format, u8 *buf, int buf_len)
{
	int data_size;
	bool big_endian;
	bool unsigned_type;
	s32 value;

	data_size = (data_format & 0x07);
	big_endian = ((data_format & 0x10) == 0x00);
	unsigned_type = ((data_format & 0x20) == 0x00);

	if (buf_len < data_size)
		return 0;

	switch (data_size) {
	case 1:
		value  = buf[0];
		break;
	case 2:
		if (big_endian)
			value = get_unaligned_be16(buf);
		else
			value = get_unaligned_le16(buf);
		break;
	case 4:
		if (big_endian)
			value = get_unaligned_be32(buf);
		else
			value = get_unaligned_le32(buf);
		break;
	default:
		/* Should not happen, just as default case here. */
		value = 0;
		break;
	}

	if (!unsigned_type)
		value = twos_complement_to_s32(value, data_size * 8);

	return value;
}

static void cyapa_gen5_guess_electrodes(struct cyapa *cyapa,
		int *electrodes_rx, int *electrodes_tx)
{
	if (cyapa->electrodes_rx != 0) {
		*electrodes_rx = cyapa->electrodes_rx;
		*electrodes_tx = (cyapa->electrodes_x == *electrodes_rx) ?
				cyapa->electrodes_y : cyapa->electrodes_x;
	} else {
		*electrodes_tx = min(cyapa->electrodes_x, cyapa->electrodes_y);
		*electrodes_rx = max(cyapa->electrodes_x, cyapa->electrodes_y);
	}
}

/*
 * Read all the global mutual or self idac data or mutual or self local PWC
 * data based on the @idac_data_type.
 * If the input value of @data_size is 0, then means read global mutual or
 * self idac data. For read global mutual idac data, @idac_max, @idac_min and
 * @idac_ave are in order used to return the max value of global mutual idac
 * data, the min value of global mutual idac and the average value of the
 * global mutual idac data. For read global self idac data, @idac_max is used
 * to return the global self cap idac data in Rx direction, @idac_min is used
 * to return the global self cap idac data in Tx direction. @idac_ave is not
 * used.
 * If the input value of @data_size is not 0, than means read the mutual or
 * self local PWC data. The @idac_max, @idac_min and @idac_ave are used to
 * return the max, min and average value of the mutual or self local PWC data.
 * Note, in order to read mutual local PWC data, must read invoke this function
 * to read the mutual global idac data firstly to set the correct Rx number
 * value, otherwise, the read mutual idac and PWC data may not correct.
 */
static int cyapa_gen5_read_idac_data(struct cyapa *cyapa,
		u8 cmd_code, u8 idac_data_type, int *data_size,
		int *idac_max, int *idac_min, int *idac_ave)
{
	struct pip_app_cmd_head *cmd_head;
	u8 cmd[12];
	u8 resp_data[256];
	int resp_len;
	int read_len;
	int value;
	u16 offset;
	int read_elements;
	bool read_global_idac;
	int sum, count, max_element_cnt;
	int tmp_max, tmp_min, tmp_ave, tmp_sum, tmp_count;
	int electrodes_rx, electrodes_tx;
	int i;
	int error;

	if (cmd_code != PIP_RETRIEVE_DATA_STRUCTURE ||
		(idac_data_type != GEN5_RETRIEVE_MUTUAL_PWC_DATA &&
		idac_data_type != GEN5_RETRIEVE_SELF_CAP_PWC_DATA) ||
		!data_size || !idac_max || !idac_min || !idac_ave)
		return -EINVAL;

	*idac_max = INT_MIN;
	*idac_min = INT_MAX;
	sum = count = tmp_count = 0;
	electrodes_rx = electrodes_tx = 0;
	if (*data_size == 0) {
		/*
		 * Read global idac values firstly.
		 * Currently, no idac data exceed 4 bytes.
		 */
		read_global_idac = true;
		offset = 0;
		*data_size = 4;
		tmp_max = INT_MIN;
		tmp_min = INT_MAX;
		tmp_ave = tmp_sum = tmp_count = 0;

		if (idac_data_type == GEN5_RETRIEVE_MUTUAL_PWC_DATA) {
			if (cyapa->aligned_electrodes_rx == 0) {
				cyapa_gen5_guess_electrodes(cyapa,
					&electrodes_rx, &electrodes_tx);
				cyapa->aligned_electrodes_rx =
					(electrodes_rx + 3) & ~3u;
			}
			max_element_cnt =
				(cyapa->aligned_electrodes_rx + 7) & ~7u;
		} else {
			max_element_cnt = 2;
		}
	} else {
		read_global_idac = false;
		if (*data_size > 4)
			*data_size = 4;
		/* Calculate the start offset in bytes of local PWC data. */
		if (idac_data_type == GEN5_RETRIEVE_MUTUAL_PWC_DATA) {
			offset = cyapa->aligned_electrodes_rx * (*data_size);
			if (cyapa->electrodes_rx == cyapa->electrodes_x)
				electrodes_tx = cyapa->electrodes_y;
			else
				electrodes_tx = cyapa->electrodes_x;
			max_element_cnt = ((cyapa->aligned_electrodes_rx + 7) &
						~7u) * electrodes_tx;
		} else {
			offset = 2;
			max_element_cnt = cyapa->electrodes_x +
						cyapa->electrodes_y;
			max_element_cnt = (max_element_cnt + 3) & ~3u;
		}
	}

	memset(cmd, 0, sizeof(cmd));
	cmd_head = (struct pip_app_cmd_head *)cmd;
	put_unaligned_le16(PIP_OUTPUT_REPORT_ADDR, &cmd_head->addr);
	put_unaligned_le16(sizeof(cmd) - 2, &cmd_head->length);
	cmd_head->report_id = PIP_APP_CMD_REPORT_ID;
	cmd_head->cmd_code = cmd_code;
	do {
		read_elements = (256 - GEN5_RESP_DATA_STRUCTURE_OFFSET) /
				(*data_size);
		read_elements = min(read_elements, max_element_cnt - count);
		read_len = read_elements * (*data_size);

		put_unaligned_le16(offset, &cmd_head->parameter_data[0]);
		put_unaligned_le16(read_len, &cmd_head->parameter_data[2]);
		cmd_head->parameter_data[4] = idac_data_type;
		resp_len = GEN5_RESP_DATA_STRUCTURE_OFFSET + read_len;
		error = cyapa_i2c_pip_cmd_irq_sync(cyapa,
				cmd, sizeof(cmd),
				resp_data, &resp_len,
				500, cyapa_sort_tsg_pip_app_resp_data,
				true);
		if (error || resp_len < GEN5_RESP_DATA_STRUCTURE_OFFSET ||
				!VALID_CMD_RESP_HEADER(resp_data, cmd_code) ||
				!PIP_CMD_COMPLETE_SUCCESS(resp_data) ||
				resp_data[6] != idac_data_type)
			return (error < 0) ? error : -EAGAIN;
		read_len = get_unaligned_le16(&resp_data[7]);
		if (read_len == 0)
			break;

		*data_size = (resp_data[9] & GEN5_PWC_DATA_ELEMENT_SIZE_MASK);
		if (read_len < *data_size)
			return -EINVAL;

		if (read_global_idac &&
			idac_data_type == GEN5_RETRIEVE_SELF_CAP_PWC_DATA) {
			/* Rx's self global idac data. */
			*idac_max = cyapa_parse_structure_data(
				resp_data[9],
				&resp_data[GEN5_RESP_DATA_STRUCTURE_OFFSET],
				*data_size);
			/* Tx's self global idac data. */
			*idac_min = cyapa_parse_structure_data(
				resp_data[9],
				&resp_data[GEN5_RESP_DATA_STRUCTURE_OFFSET +
					   *data_size],
				*data_size);
			break;
		}

		/* Read mutual global idac or local mutual/self PWC data. */
		offset += read_len;
		for (i = 10; i < (read_len + GEN5_RESP_DATA_STRUCTURE_OFFSET);
				i += *data_size) {
			value = cyapa_parse_structure_data(resp_data[9],
					&resp_data[i], *data_size);
			*idac_min = min(value, *idac_min);
			*idac_max = max(value, *idac_max);

			if (idac_data_type == GEN5_RETRIEVE_MUTUAL_PWC_DATA &&
				tmp_count < cyapa->aligned_electrodes_rx &&
				read_global_idac) {
				/*
				 * The value gap between global and local mutual
				 * idac data must bigger than 50%.
				 * Normally, global value bigger than 50,
				 * local values less than 10.
				 */
				if (!tmp_ave || value > tmp_ave / 2) {
					tmp_min = min(value, tmp_min);
					tmp_max = max(value, tmp_max);
					tmp_sum += value;
					tmp_count++;

					tmp_ave = tmp_sum / tmp_count;
				}
			}

			sum += value;
			count++;

			if (count >= max_element_cnt)
				goto out;
		}
	} while (true);

out:
	*idac_ave = count ? (sum / count) : 0;

	if (read_global_idac &&
		idac_data_type == GEN5_RETRIEVE_MUTUAL_PWC_DATA) {
		if (tmp_count == 0)
			return 0;

		if (tmp_count == cyapa->aligned_electrodes_rx) {
			cyapa->electrodes_rx = cyapa->electrodes_rx ?
				cyapa->electrodes_rx : electrodes_rx;
		} else if (tmp_count == electrodes_rx) {
			cyapa->electrodes_rx = cyapa->electrodes_rx ?
				cyapa->electrodes_rx : electrodes_rx;
			cyapa->aligned_electrodes_rx = electrodes_rx;
		} else {
			cyapa->electrodes_rx = cyapa->electrodes_rx ?
				cyapa->electrodes_rx : electrodes_tx;
			cyapa->aligned_electrodes_rx = tmp_count;
		}

		*idac_min = tmp_min;
		*idac_max = tmp_max;
		*idac_ave = tmp_ave;
	}

	return 0;
}

static int cyapa_gen5_read_mutual_idac_data(struct cyapa *cyapa,
	int *gidac_mutual_max, int *gidac_mutual_min, int *gidac_mutual_ave,
	int *lidac_mutual_max, int *lidac_mutual_min, int *lidac_mutual_ave)
{
	int data_size;
	int error;

	*gidac_mutual_max = *gidac_mutual_min = *gidac_mutual_ave = 0;
	*lidac_mutual_max = *lidac_mutual_min = *lidac_mutual_ave = 0;

	data_size = 0;
	error = cyapa_gen5_read_idac_data(cyapa,
		PIP_RETRIEVE_DATA_STRUCTURE,
		GEN5_RETRIEVE_MUTUAL_PWC_DATA,
		&data_size,
		gidac_mutual_max, gidac_mutual_min, gidac_mutual_ave);
	if (error)
		return error;

	error = cyapa_gen5_read_idac_data(cyapa,
		PIP_RETRIEVE_DATA_STRUCTURE,
		GEN5_RETRIEVE_MUTUAL_PWC_DATA,
		&data_size,
		lidac_mutual_max, lidac_mutual_min, lidac_mutual_ave);
	return error;
}

static int cyapa_gen5_read_self_idac_data(struct cyapa *cyapa,
		int *gidac_self_rx, int *gidac_self_tx,
		int *lidac_self_max, int *lidac_self_min, int *lidac_self_ave)
{
	int data_size;
	int error;

	*gidac_self_rx = *gidac_self_tx = 0;
	*lidac_self_max = *lidac_self_min = *lidac_self_ave = 0;

	data_size = 0;
	error = cyapa_gen5_read_idac_data(cyapa,
		PIP_RETRIEVE_DATA_STRUCTURE,
		GEN5_RETRIEVE_SELF_CAP_PWC_DATA,
		&data_size,
		lidac_self_max, lidac_self_min, lidac_self_ave);
	if (error)
		return error;
	*gidac_self_rx = *lidac_self_max;
	*gidac_self_tx = *lidac_self_min;

	error = cyapa_gen5_read_idac_data(cyapa,
		PIP_RETRIEVE_DATA_STRUCTURE,
		GEN5_RETRIEVE_SELF_CAP_PWC_DATA,
		&data_size,
		lidac_self_max, lidac_self_min, lidac_self_ave);
	return error;
}

static ssize_t cyapa_gen5_execute_panel_scan(struct cyapa *cyapa)
{
	struct pip_app_cmd_head *app_cmd_head;
	u8 cmd[7];
	u8 resp_data[6];
	int resp_len;
	int error;

	memset(cmd, 0, sizeof(cmd));
	app_cmd_head = (struct pip_app_cmd_head *)cmd;
	put_unaligned_le16(PIP_OUTPUT_REPORT_ADDR, &app_cmd_head->addr);
	put_unaligned_le16(sizeof(cmd) - 2, &app_cmd_head->length);
	app_cmd_head->report_id = PIP_APP_CMD_REPORT_ID;
	app_cmd_head->cmd_code = GEN5_CMD_EXECUTE_PANEL_SCAN;
	resp_len = sizeof(resp_data);
	error = cyapa_i2c_pip_cmd_irq_sync(cyapa,
			cmd, sizeof(cmd),
			resp_data, &resp_len,
			500, cyapa_sort_tsg_pip_app_resp_data, true);
	if (error || resp_len != sizeof(resp_data) ||
			!VALID_CMD_RESP_HEADER(resp_data,
				GEN5_CMD_EXECUTE_PANEL_SCAN) ||
			!PIP_CMD_COMPLETE_SUCCESS(resp_data))
		return error ? error : -EAGAIN;

	return 0;
}

static int cyapa_gen5_read_panel_scan_raw_data(struct cyapa *cyapa,
		u8 cmd_code, u8 raw_data_type, int raw_data_max_num,
		int *raw_data_max, int *raw_data_min, int *raw_data_ave,
		u8 *buffer)
{
	struct pip_app_cmd_head *app_cmd_head;
	struct gen5_retrieve_panel_scan_data *panel_sacn_data;
	u8 cmd[12];
	u8 resp_data[256];  /* Max bytes can transfer one time. */
	int resp_len;
	int read_elements;
	int read_len;
	u16 offset;
	s32 value;
	int sum, count;
	int data_size;
	s32 *intp;
	int i;
	int error;

	if (cmd_code != GEN5_CMD_RETRIEVE_PANEL_SCAN ||
		(raw_data_type > GEN5_PANEL_SCAN_SELF_DIFFCOUNT) ||
		!raw_data_max || !raw_data_min || !raw_data_ave)
		return -EINVAL;

	intp = (s32 *)buffer;
	*raw_data_max = INT_MIN;
	*raw_data_min = INT_MAX;
	sum = count = 0;
	offset = 0;
	/* Assume max element size is 4 currently. */
	read_elements = (256 - GEN5_RESP_DATA_STRUCTURE_OFFSET) / 4;
	read_len = read_elements * 4;
	app_cmd_head = (struct pip_app_cmd_head *)cmd;
	put_unaligned_le16(PIP_OUTPUT_REPORT_ADDR, &app_cmd_head->addr);
	put_unaligned_le16(sizeof(cmd) - 2, &app_cmd_head->length);
	app_cmd_head->report_id = PIP_APP_CMD_REPORT_ID;
	app_cmd_head->cmd_code = cmd_code;
	panel_sacn_data = (struct gen5_retrieve_panel_scan_data *)
			app_cmd_head->parameter_data;
	do {
		put_unaligned_le16(offset, &panel_sacn_data->read_offset);
		put_unaligned_le16(read_elements,
			&panel_sacn_data->read_elements);
		panel_sacn_data->data_id = raw_data_type;

		resp_len = GEN5_RESP_DATA_STRUCTURE_OFFSET + read_len;
		error = cyapa_i2c_pip_cmd_irq_sync(cyapa,
			cmd, sizeof(cmd),
			resp_data, &resp_len,
			500, cyapa_sort_tsg_pip_app_resp_data, true);
		if (error || resp_len < GEN5_RESP_DATA_STRUCTURE_OFFSET ||
				!VALID_CMD_RESP_HEADER(resp_data, cmd_code) ||
				!PIP_CMD_COMPLETE_SUCCESS(resp_data) ||
				resp_data[6] != raw_data_type)
			return error ? error : -EAGAIN;

		read_elements = get_unaligned_le16(&resp_data[7]);
		if (read_elements == 0)
			break;

		data_size = (resp_data[9] & GEN5_PWC_DATA_ELEMENT_SIZE_MASK);
		offset += read_elements;
		if (read_elements) {
			for (i = GEN5_RESP_DATA_STRUCTURE_OFFSET;
			     i < (read_elements * data_size +
					GEN5_RESP_DATA_STRUCTURE_OFFSET);
			     i += data_size) {
				value = cyapa_parse_structure_data(resp_data[9],
						&resp_data[i], data_size);
				*raw_data_min = min(value, *raw_data_min);
				*raw_data_max = max(value, *raw_data_max);

				if (intp)
					put_unaligned_le32(value, &intp[count]);

				sum += value;
				count++;

			}
		}

		if (count >= raw_data_max_num)
			break;

		read_elements = (sizeof(resp_data) -
				GEN5_RESP_DATA_STRUCTURE_OFFSET) / data_size;
		read_len = read_elements * data_size;
	} while (true);

	*raw_data_ave = count ? (sum / count) : 0;

	return 0;
}

static ssize_t cyapa_gen5_show_baseline(struct device *dev,
				   struct device_attribute *attr, char *buf)
{
	struct cyapa *cyapa = dev_get_drvdata(dev);
	int gidac_mutual_max, gidac_mutual_min, gidac_mutual_ave;
	int lidac_mutual_max, lidac_mutual_min, lidac_mutual_ave;
	int gidac_self_rx, gidac_self_tx;
	int lidac_self_max, lidac_self_min, lidac_self_ave;
	int raw_cap_mutual_max, raw_cap_mutual_min, raw_cap_mutual_ave;
	int raw_cap_self_max, raw_cap_self_min, raw_cap_self_ave;
	int mutual_diffdata_max, mutual_diffdata_min, mutual_diffdata_ave;
	int self_diffdata_max, self_diffdata_min, self_diffdata_ave;
	int mutual_baseline_max, mutual_baseline_min, mutual_baseline_ave;
	int self_baseline_max, self_baseline_min, self_baseline_ave;
	int error, resume_error;
	int size;

	if (!cyapa_is_pip_app_mode(cyapa))
		return -EBUSY;

	/* 1. Suspend Scanning*/
	error = cyapa_pip_suspend_scanning(cyapa);
	if (error)
		return error;

	/* 2.  Read global and local mutual IDAC data. */
	gidac_self_rx = gidac_self_tx = 0;
	error = cyapa_gen5_read_mutual_idac_data(cyapa,
				&gidac_mutual_max, &gidac_mutual_min,
				&gidac_mutual_ave, &lidac_mutual_max,
				&lidac_mutual_min, &lidac_mutual_ave);
	if (error)
		goto resume_scanning;

	/* 3.  Read global and local self IDAC data. */
	error = cyapa_gen5_read_self_idac_data(cyapa,
				&gidac_self_rx, &gidac_self_tx,
				&lidac_self_max, &lidac_self_min,
				&lidac_self_ave);
	if (error)
		goto resume_scanning;

	/* 4. Execute panel scan. It must be executed before read data. */
	error = cyapa_gen5_execute_panel_scan(cyapa);
	if (error)
		goto resume_scanning;

	/* 5. Retrieve panel scan, mutual cap raw data. */
	error = cyapa_gen5_read_panel_scan_raw_data(cyapa,
				GEN5_CMD_RETRIEVE_PANEL_SCAN,
				GEN5_PANEL_SCAN_MUTUAL_RAW_DATA,
				cyapa->electrodes_x * cyapa->electrodes_y,
				&raw_cap_mutual_max, &raw_cap_mutual_min,
				&raw_cap_mutual_ave,
				NULL);
	if (error)
		goto resume_scanning;

	/* 6. Retrieve panel scan, self cap raw data. */
	error = cyapa_gen5_read_panel_scan_raw_data(cyapa,
				GEN5_CMD_RETRIEVE_PANEL_SCAN,
				GEN5_PANEL_SCAN_SELF_RAW_DATA,
				cyapa->electrodes_x + cyapa->electrodes_y,
				&raw_cap_self_max, &raw_cap_self_min,
				&raw_cap_self_ave,
				NULL);
	if (error)
		goto resume_scanning;

	/* 7. Retrieve panel scan, mutual cap diffcount raw data. */
	error = cyapa_gen5_read_panel_scan_raw_data(cyapa,
				GEN5_CMD_RETRIEVE_PANEL_SCAN,
				GEN5_PANEL_SCAN_MUTUAL_DIFFCOUNT,
				cyapa->electrodes_x * cyapa->electrodes_y,
				&mutual_diffdata_max, &mutual_diffdata_min,
				&mutual_diffdata_ave,
				NULL);
	if (error)
		goto resume_scanning;

	/* 8. Retrieve panel scan, self cap diffcount raw data. */
	error = cyapa_gen5_read_panel_scan_raw_data(cyapa,
				GEN5_CMD_RETRIEVE_PANEL_SCAN,
				GEN5_PANEL_SCAN_SELF_DIFFCOUNT,
				cyapa->electrodes_x + cyapa->electrodes_y,
				&self_diffdata_max, &self_diffdata_min,
				&self_diffdata_ave,
				NULL);
	if (error)
		goto resume_scanning;

	/* 9. Retrieve panel scan, mutual cap baseline raw data. */
	error = cyapa_gen5_read_panel_scan_raw_data(cyapa,
				GEN5_CMD_RETRIEVE_PANEL_SCAN,
				GEN5_PANEL_SCAN_MUTUAL_BASELINE,
				cyapa->electrodes_x * cyapa->electrodes_y,
				&mutual_baseline_max, &mutual_baseline_min,
				&mutual_baseline_ave,
				NULL);
	if (error)
		goto resume_scanning;

	/* 10. Retrieve panel scan, self cap baseline raw data. */
	error = cyapa_gen5_read_panel_scan_raw_data(cyapa,
				GEN5_CMD_RETRIEVE_PANEL_SCAN,
				GEN5_PANEL_SCAN_SELF_BASELINE,
				cyapa->electrodes_x + cyapa->electrodes_y,
				&self_baseline_max, &self_baseline_min,
				&self_baseline_ave,
				NULL);
	if (error)
		goto resume_scanning;

resume_scanning:
	/* 11. Resume Scanning*/
	resume_error = cyapa_pip_resume_scanning(cyapa);
	if (resume_error || error)
		return resume_error ? resume_error : error;

	/* 12. Output data strings */
	size = scnprintf(buf, PAGE_SIZE, "%d %d %d %d %d %d %d %d %d %d %d ",
		gidac_mutual_min, gidac_mutual_max, gidac_mutual_ave,
		lidac_mutual_min, lidac_mutual_max, lidac_mutual_ave,
		gidac_self_rx, gidac_self_tx,
		lidac_self_min, lidac_self_max, lidac_self_ave);
	size += scnprintf(buf + size, PAGE_SIZE - size,
		"%d %d %d %d %d %d %d %d %d %d %d %d %d %d %d %d %d %d\n",
		raw_cap_mutual_min, raw_cap_mutual_max, raw_cap_mutual_ave,
		raw_cap_self_min, raw_cap_self_max, raw_cap_self_ave,
		mutual_diffdata_min, mutual_diffdata_max, mutual_diffdata_ave,
		self_diffdata_min, self_diffdata_max, self_diffdata_ave,
		mutual_baseline_min, mutual_baseline_max, mutual_baseline_ave,
		self_baseline_min, self_baseline_max, self_baseline_ave);
	return size;
}

bool cyapa_pip_sort_system_info_data(struct cyapa *cyapa,
		u8 *buf, int len)
{
	/* Check the report id and command code */
	if (VALID_CMD_RESP_HEADER(buf, 0x02))
		return true;

	return false;
}

static int cyapa_gen5_bl_query_data(struct cyapa *cyapa)
{
	u8 resp_data[PIP_BL_APP_INFO_RESP_LENGTH];
	int resp_len;
	int error;

	resp_len = sizeof(resp_data);
	error = cyapa_i2c_pip_cmd_irq_sync(cyapa,
			pip_bl_read_app_info, PIP_BL_READ_APP_INFO_CMD_LENGTH,
			resp_data, &resp_len,
			500, cyapa_sort_tsg_pip_bl_resp_data, false);
	if (error || resp_len < PIP_BL_APP_INFO_RESP_LENGTH ||
		!PIP_CMD_COMPLETE_SUCCESS(resp_data))
		return error ? error : -EIO;

	memcpy(&cyapa->product_id[0], &resp_data[8], 5);
	cyapa->product_id[5] = '-';
	memcpy(&cyapa->product_id[6], &resp_data[13], 6);
	cyapa->product_id[12] = '-';
	memcpy(&cyapa->product_id[13], &resp_data[19], 2);
	cyapa->product_id[15] = '\0';

	cyapa->fw_maj_ver = resp_data[22];
	cyapa->fw_min_ver = resp_data[23];

	cyapa->platform_ver = (resp_data[26] >> PIP_BL_PLATFORM_VER_SHIFT) &
			      PIP_BL_PLATFORM_VER_MASK;

	return 0;
}

static int cyapa_gen5_get_query_data(struct cyapa *cyapa)
{
	u8 resp_data[PIP_READ_SYS_INFO_RESP_LENGTH];
	int resp_len;
	u16 product_family;
	int error;

	resp_len = sizeof(resp_data);
	error = cyapa_i2c_pip_cmd_irq_sync(cyapa,
			pip_read_sys_info, PIP_READ_SYS_INFO_CMD_LENGTH,
			resp_data, &resp_len,
			2000, cyapa_pip_sort_system_info_data, false);
	if (error || resp_len < sizeof(resp_data))
		return error ? error : -EIO;

	product_family = get_unaligned_le16(&resp_data[7]);
	if ((product_family & PIP_PRODUCT_FAMILY_MASK) !=
		PIP_PRODUCT_FAMILY_TRACKPAD)
		return -EINVAL;

	cyapa->platform_ver = (resp_data[49] >> PIP_BL_PLATFORM_VER_SHIFT) &
			      PIP_BL_PLATFORM_VER_MASK;
	if (cyapa->gen == CYAPA_GEN5 && cyapa->platform_ver < 2) {
		/* Gen5 firmware that does not support proximity. */
		cyapa->fw_maj_ver = resp_data[15];
		cyapa->fw_min_ver = resp_data[16];
	} else {
		cyapa->fw_maj_ver = resp_data[9];
		cyapa->fw_min_ver = resp_data[10];
	}

	cyapa->electrodes_x = resp_data[52];
	cyapa->electrodes_y = resp_data[53];

	cyapa->physical_size_x =  get_unaligned_le16(&resp_data[54]) / 100;
	cyapa->physical_size_y = get_unaligned_le16(&resp_data[56]) / 100;

	cyapa->max_abs_x = get_unaligned_le16(&resp_data[58]);
	cyapa->max_abs_y = get_unaligned_le16(&resp_data[60]);

	cyapa->max_z = get_unaligned_le16(&resp_data[62]);

	cyapa->x_origin = resp_data[64] & 0x01;
	cyapa->y_origin = resp_data[65] & 0x01;

	cyapa->btn_capability = (resp_data[70] << 3) & CAPABILITY_BTN_MASK;

	memcpy(&cyapa->product_id[0], &resp_data[33], 5);
	cyapa->product_id[5] = '-';
	memcpy(&cyapa->product_id[6], &resp_data[38], 6);
	cyapa->product_id[12] = '-';
	memcpy(&cyapa->product_id[13], &resp_data[44], 2);
	cyapa->product_id[15] = '\0';

	if (!cyapa->electrodes_x || !cyapa->electrodes_y ||
		!cyapa->physical_size_x || !cyapa->physical_size_y ||
		!cyapa->max_abs_x || !cyapa->max_abs_y || !cyapa->max_z)
		return -EINVAL;

	return 0;
}

static int cyapa_gen5_do_operational_check(struct cyapa *cyapa)
{
	struct device *dev = &cyapa->client->dev;
	int error;

	if (cyapa->gen != CYAPA_GEN5)
		return -ENODEV;

	switch (cyapa->state) {
	case CYAPA_STATE_GEN5_BL:
		error = cyapa_pip_bl_exit(cyapa);
		if (error) {
			/* Try to update trackpad product information. */
			cyapa_gen5_bl_query_data(cyapa);
			goto out;
		}

		cyapa->state = CYAPA_STATE_GEN5_APP;
		/* fall through */

	case CYAPA_STATE_GEN5_APP:
		/*
		 * If trackpad device in deep sleep mode,
		 * the app command will fail.
		 * So always try to reset trackpad device to full active when
		 * the device state is required.
		 */
		error = cyapa_gen5_set_power_mode(cyapa,
				PWR_MODE_FULL_ACTIVE, 0, CYAPA_PM_ACTIVE);
		if (error)
			dev_warn(dev, "%s: failed to set power active mode.\n",
				__func__);

		/* By default, the trackpad proximity function is enabled. */
		if (cyapa->platform_ver >= 2) {
			error = cyapa_pip_set_proximity(cyapa, true);
			if (error)
				dev_warn(dev,
					"%s: failed to enable proximity.\n",
					__func__);
		}

		/* Get trackpad product information. */
		error = cyapa_gen5_get_query_data(cyapa);
		if (error)
			goto out;
		/* Only support product ID starting with CYTRA */
		if (memcmp(cyapa->product_id, product_id,
				strlen(product_id)) != 0) {
			dev_err(dev, "%s: unknown product ID (%s)\n",
				__func__, cyapa->product_id);
			error = -EINVAL;
		}
		break;
	default:
		error = -EINVAL;
	}

out:
	return error;
}

/*
 * Return false, do not continue process
 * Return true, continue process.
 */
bool cyapa_pip_irq_cmd_handler(struct cyapa *cyapa)
{
	struct cyapa_pip_cmd_states *pip = &cyapa->cmd_states.pip;
	int length;

	if (atomic_read(&pip->cmd_issued)) {
		/* Polling command response data. */
		if (pip->is_irq_mode == false)
			return false;

		/*
		 * Read out all none command response data.
		 * these output data may caused by user put finger on
		 * trackpad when host waiting the command response.
		 */
		cyapa_i2c_pip_read(cyapa, pip->irq_cmd_buf,
			PIP_RESP_LENGTH_SIZE);
		length = get_unaligned_le16(pip->irq_cmd_buf);
		length = (length <= PIP_RESP_LENGTH_SIZE) ?
				PIP_RESP_LENGTH_SIZE : length;
		if (length > PIP_RESP_LENGTH_SIZE)
			cyapa_i2c_pip_read(cyapa,
				pip->irq_cmd_buf, length);
		if (!(pip->resp_sort_func &&
			pip->resp_sort_func(cyapa,
				pip->irq_cmd_buf, length))) {
			/*
			 * Cover the Gen5 V1 firmware issue.
			 * The issue is no interrupt would be asserted from
			 * trackpad device to host for the command response
			 * ready event. Because when there was a finger touch
			 * on trackpad device, and the firmware output queue
			 * won't be empty (always with touch report data), so
			 * the interrupt signal won't be asserted again until
			 * the output queue was previous emptied.
			 * This issue would happen in the scenario that
			 * user always has his/her fingers touched on the
			 * trackpad device during system booting/rebooting.
			 */
			length = 0;
			if (pip->resp_len)
				length = *pip->resp_len;
			cyapa_empty_pip_output_data(cyapa,
					pip->resp_data,
					&length,
					pip->resp_sort_func);
			if (pip->resp_len && length != 0) {
				*pip->resp_len = length;
				atomic_dec(&pip->cmd_issued);
				complete(&pip->cmd_ready);
			}
			return false;
		}

		if (pip->resp_data && pip->resp_len) {
			*pip->resp_len = (*pip->resp_len < length) ?
				*pip->resp_len : length;
			memcpy(pip->resp_data, pip->irq_cmd_buf,
				*pip->resp_len);
		}
		atomic_dec(&pip->cmd_issued);
		complete(&pip->cmd_ready);
		return false;
	}

	return true;
}

static void cyapa_pip_report_buttons(struct cyapa *cyapa,
		const struct cyapa_pip_report_data *report_data)
{
	struct input_dev *input = cyapa->input;
	u8 buttons = report_data->report_head[PIP_BUTTONS_OFFSET];

	buttons = (buttons << CAPABILITY_BTN_SHIFT) & CAPABILITY_BTN_MASK;

	if (cyapa->btn_capability & CAPABILITY_LEFT_BTN_MASK) {
		input_report_key(input, BTN_LEFT,
			!!(buttons & CAPABILITY_LEFT_BTN_MASK));
	}
	if (cyapa->btn_capability & CAPABILITY_MIDDLE_BTN_MASK) {
		input_report_key(input, BTN_MIDDLE,
			!!(buttons & CAPABILITY_MIDDLE_BTN_MASK));
	}
	if (cyapa->btn_capability & CAPABILITY_RIGHT_BTN_MASK) {
		input_report_key(input, BTN_RIGHT,
			!!(buttons & CAPABILITY_RIGHT_BTN_MASK));
	}

	input_sync(input);
}

static void cyapa_pip_report_proximity(struct cyapa *cyapa,
		const struct cyapa_pip_report_data *report_data)
{
	struct input_dev *input = cyapa->input;
	u8 distance = report_data->report_head[PIP_PROXIMITY_DISTANCE_OFFSET] &
			PIP_PROXIMITY_DISTANCE_MASK;

	input_report_abs(input, ABS_DISTANCE, distance);
	input_sync(input);
}

static void cyapa_pip_report_slot_data(struct cyapa *cyapa,
		const struct cyapa_pip_touch_record *touch)
{
	struct input_dev *input = cyapa->input;
	u8 event_id = PIP_GET_EVENT_ID(touch->touch_tip_event_id);
	int slot = PIP_GET_TOUCH_ID(touch->touch_tip_event_id);
	int x, y;

	if (event_id == RECORD_EVENT_LIFTOFF)
		return;

	input_mt_slot(input, slot);
	input_mt_report_slot_state(input, MT_TOOL_FINGER, true);
	x = (touch->x_hi << 8) | touch->x_lo;
	if (cyapa->x_origin)
		x = cyapa->max_abs_x - x;
	y = (touch->y_hi << 8) | touch->y_lo;
	if (cyapa->y_origin)
		y = cyapa->max_abs_y - y;
	input_report_abs(input, ABS_MT_POSITION_X, x);
	input_report_abs(input, ABS_MT_POSITION_Y, y);
	input_report_abs(input, ABS_DISTANCE, 0);
	input_report_abs(input, ABS_MT_PRESSURE,
		touch->z);
	input_report_abs(input, ABS_MT_TOUCH_MAJOR,
		touch->major_axis_len);
	input_report_abs(input, ABS_MT_TOUCH_MINOR,
		touch->minor_axis_len);

	input_report_abs(input, ABS_MT_WIDTH_MAJOR,
		touch->major_tool_len);
	input_report_abs(input, ABS_MT_WIDTH_MINOR,
		touch->minor_tool_len);

	input_report_abs(input, ABS_MT_ORIENTATION,
		touch->orientation);
}

static void cyapa_pip_report_touches(struct cyapa *cyapa,
		const struct cyapa_pip_report_data *report_data)
{
	struct input_dev *input = cyapa->input;
	unsigned int touch_num;
	int i;

	touch_num = report_data->report_head[PIP_NUMBER_OF_TOUCH_OFFSET] &
			PIP_NUMBER_OF_TOUCH_MASK;

	for (i = 0; i < touch_num; i++)
		cyapa_pip_report_slot_data(cyapa,
			&report_data->touch_records[i]);

	input_mt_sync_frame(input);
	input_sync(input);
}

int cyapa_pip_irq_handler(struct cyapa *cyapa)
{
	struct device *dev = &cyapa->client->dev;
	struct cyapa_pip_report_data report_data;
	unsigned int report_len;
	int ret;

	if (!cyapa_is_pip_app_mode(cyapa)) {
		dev_err(dev, "invalid device state, gen=%d, state=0x%02x\n",
			cyapa->gen, cyapa->state);
		return -EINVAL;
	}

	ret = cyapa_i2c_pip_read(cyapa, (u8 *)&report_data,
			PIP_RESP_LENGTH_SIZE);
	if (ret != PIP_RESP_LENGTH_SIZE) {
		dev_err(dev, "failed to read length bytes, (%d)\n", ret);
		return -EINVAL;
	}

	report_len = get_unaligned_le16(
			&report_data.report_head[PIP_RESP_LENGTH_OFFSET]);
	if (report_len < PIP_RESP_LENGTH_SIZE) {
		/* Invalid length or internal reset happened. */
		dev_err(dev, "invalid report_len=%d. bytes: %02x %02x\n",
			report_len, report_data.report_head[0],
			report_data.report_head[1]);
		return -EINVAL;
	}

	/* Idle, no data for report. */
	if (report_len == PIP_RESP_LENGTH_SIZE)
		return 0;

	ret = cyapa_i2c_pip_read(cyapa, (u8 *)&report_data, report_len);
	if (ret != report_len) {
		dev_err(dev, "failed to read %d bytes report data, (%d)\n",
			report_len, ret);
		return -EINVAL;
	}

	return cyapa_pip_event_process(cyapa, &report_data);
}

static int cyapa_pip_event_process(struct cyapa *cyapa,
				   struct cyapa_pip_report_data *report_data)
{
	struct device *dev = &cyapa->client->dev;
	unsigned int report_len;
	u8 report_id;

	report_len = get_unaligned_le16(
			&report_data->report_head[PIP_RESP_LENGTH_OFFSET]);
	/* Idle, no data for report. */
	if (report_len == PIP_RESP_LENGTH_SIZE)
		return 0;

	report_id = report_data->report_head[PIP_RESP_REPORT_ID_OFFSET];
	if (report_id == PIP_WAKEUP_EVENT_REPORT_ID &&
			report_len == PIP_WAKEUP_EVENT_SIZE) {
		/*
		 * Device wake event from deep sleep mode for touch.
		 * This interrupt event is used to wake system up.
		 *
		 * Note:
		 * It will introduce about 20~40 ms additional delay
		 * time in receiving for first valid touch report data.
		 * The time is used to execute device runtime resume
		 * process.
		 */
		pm_runtime_get_sync(dev);
		pm_runtime_mark_last_busy(dev);
		pm_runtime_put_sync_autosuspend(dev);
		return 0;
	} else if (report_id != PIP_TOUCH_REPORT_ID &&
			report_id != PIP_BTN_REPORT_ID &&
			report_id != GEN5_OLD_PUSH_BTN_REPORT_ID &&
			report_id != PIP_PUSH_BTN_REPORT_ID &&
			report_id != PIP_PROXIMITY_REPORT_ID) {
		/* Running in BL mode or unknown response data read. */
		dev_err(dev, "invalid report_id=0x%02x\n", report_id);
		return -EINVAL;
	}

	if (report_id == PIP_TOUCH_REPORT_ID &&
		(report_len < PIP_TOUCH_REPORT_HEAD_SIZE ||
			report_len > PIP_TOUCH_REPORT_MAX_SIZE)) {
		/* Invalid report data length for finger packet. */
		dev_err(dev, "invalid touch packet length=%d\n", report_len);
		return 0;
	}

	if ((report_id == PIP_BTN_REPORT_ID ||
			report_id == GEN5_OLD_PUSH_BTN_REPORT_ID ||
			report_id == PIP_PUSH_BTN_REPORT_ID) &&
		(report_len < PIP_BTN_REPORT_HEAD_SIZE ||
			report_len > PIP_BTN_REPORT_MAX_SIZE)) {
		/* Invalid report data length of button packet. */
		dev_err(dev, "invalid button packet length=%d\n", report_len);
		return 0;
	}

	if (report_id == PIP_PROXIMITY_REPORT_ID &&
			report_len != PIP_PROXIMITY_REPORT_SIZE) {
		/* Invalid report data length of proximity packet. */
		dev_err(dev, "invalid proximity data, length=%d\n", report_len);
		return 0;
	}

	if (report_id == PIP_TOUCH_REPORT_ID)
		cyapa_pip_report_touches(cyapa, report_data);
	else if (report_id == PIP_PROXIMITY_REPORT_ID)
		cyapa_pip_report_proximity(cyapa, report_data);
	else
		cyapa_pip_report_buttons(cyapa, report_data);

	return 0;
}

int cyapa_pip_bl_activate(struct cyapa *cyapa) { return 0; }
int cyapa_pip_bl_deactivate(struct cyapa *cyapa) { return 0; }


const struct cyapa_dev_ops cyapa_gen5_ops = {
	.check_fw = cyapa_pip_check_fw,
	.bl_enter = cyapa_pip_bl_enter,
	.bl_initiate = cyapa_pip_bl_initiate,
	.update_fw = cyapa_pip_do_fw_update,
	.bl_activate = cyapa_pip_bl_activate,
	.bl_deactivate = cyapa_pip_bl_deactivate,

	.show_baseline = cyapa_gen5_show_baseline,
	.calibrate_store = cyapa_pip_do_calibrate,

	.initialize = cyapa_pip_cmd_state_initialize,

	.state_parse = cyapa_gen5_state_parse,
	.operational_check = cyapa_gen5_do_operational_check,

	.irq_handler = cyapa_pip_irq_handler,
	.irq_cmd_handler = cyapa_pip_irq_cmd_handler,
	.sort_empty_output_data = cyapa_empty_pip_output_data,
	.set_power_mode = cyapa_gen5_set_power_mode,

	.set_proximity = cyapa_pip_set_proximity,
};