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
|
/* Copyright 2018 The Chromium OS Authors. All rights reserved.
* Use of this source code is governed by a BSD-style license that can be
* found in the LICENSE file.
*/
#include "atomic.h"
#include "board.h"
#include "common.h"
#include "console.h"
#include "gpio.h"
#include "hwtimer.h"
#include "hooks.h"
#include "i2c.h"
#include "registers.h"
#include "spi.h"
#include "task.h"
#include "tablet_mode.h"
#include "timer.h"
#include "touchpad.h"
#include "touchpad_st.h"
#include "update_fw.h"
#include "usb_api.h"
#include "usb_hid_touchpad.h"
#include "usb_isochronous.h"
#include "util.h"
#include "watchdog.h"
/* Console output macros */
#define CC_TOUCHPAD CC_USB
#define CPUTS(outstr) cputs(CC_TOUCHPAD, outstr)
#define CPRINTF(format, args...) cprintf(CC_TOUCHPAD, format, ## args)
#define CPRINTS(format, args...) cprints(CC_TOUCHPAD, format, ## args)
#define TASK_EVENT_POWER TASK_EVENT_CUSTOM(1)
#define TASK_EVENT_TP_UPDATED TASK_EVENT_CUSTOM(2)
#define SPI (&(spi_devices[SPI_ST_TP_DEVICE_ID]))
BUILD_ASSERT(sizeof(struct st_tp_event_t) == 8);
BUILD_ASSERT(BYTES_PER_PIXEL == 1);
/* Function prototypes */
static int st_tp_panel_init(int full);
static int st_tp_read_all_events(int show_error);
static int st_tp_read_host_buffer_header(void);
static int st_tp_send_ack(void);
static int st_tp_start_scan(void);
static int st_tp_stop_scan(void);
static int st_tp_update_system_state(int new_state, int mask);
static void touchpad_power_control(void);
/* Global variables */
/*
* Current system state, meaning of each bit is defined below.
*/
static int system_state;
#define SYSTEM_STATE_DEBUG_MODE BIT(0)
#define SYSTEM_STATE_ENABLE_HEAT_MAP BIT(1)
#define SYSTEM_STATE_ENABLE_DOME_SWITCH BIT(2)
#define SYSTEM_STATE_ACTIVE_MODE BIT(3)
#define SYSTEM_STATE_DOME_SWITCH_LEVEL BIT(4)
#define SYSTEM_STATE_READY BIT(5)
/*
* Pending action for touchpad.
*/
static int tp_control;
#define TP_CONTROL_SHALL_HALT BIT(0)
#define TP_CONTROL_SHALL_RESET BIT(1)
#define TP_CONTROL_SHALL_INIT BIT(2)
#define TP_CONTROL_SHALL_INIT_FULL BIT(3)
#define TP_CONTROL_SHALL_DUMP_ERROR BIT(4)
#define TP_CONTROL_RESETTING BIT(5)
#define TP_CONTROL_INIT BIT(6)
#define TP_CONTROL_INIT_FULL BIT(7)
/*
* Number of times we have reset the touchpad because of errors.
*/
static int tp_reset_retry_count;
#define MAX_TP_RESET_RETRY_COUNT 3
static int dump_memory_on_error;
/*
* Bitmap to track if a finger exists.
*/
static int touch_slot;
/*
* Timestamp of last interrupt (32 bits are enough as we divide the value by 100
* and then put it in a 16-bit field).
*/
static uint32_t irq_ts;
/*
* Cached system info.
*/
static struct st_tp_system_info_t system_info;
static struct {
#if ST_TP_DUMMY_BYTE == 1
uint8_t dummy;
#endif
union {
uint8_t bytes[512];
struct st_tp_host_buffer_header_t buffer_header;
struct st_tp_host_buffer_heat_map_t heat_map;
struct st_tp_host_data_header_t data_header;
struct st_tp_event_t events[32];
uint32_t dump_info[32];
} /* anonymous */;
} __packed rx_buf;
#ifdef CONFIG_USB_ISOCHRONOUS
#define USB_ISO_PACKET_SIZE 256
/*
* Header of each USB pacaket.
*/
struct packet_header_t {
uint8_t index;
#define HEADER_FLAGS_NEW_FRAME BIT(0)
uint8_t flags;
} __packed;
BUILD_ASSERT(sizeof(struct packet_header_t) < USB_ISO_PACKET_SIZE);
static struct packet_header_t packet_header;
/* What will be sent to USB interface. */
struct st_tp_usb_packet_t {
#define USB_FRAME_FLAGS_BUTTON BIT(0)
/*
* This will be true if user clicked on touchpad.
* TODO(b/70482333): add corresponding code for button signal.
*/
uint8_t flags;
/*
* This will be `st_tp_host_buffer_heat_map_t.frame` but each pixel
* will be scaled to 8 bits value.
*/
uint8_t frame[ST_TOUCH_ROWS * ST_TOUCH_COLS];
} __packed;
/* Next buffer index SPI will write to. */
static volatile uint32_t spi_buffer_index;
/* Next buffer index USB will read from. */
static volatile uint32_t usb_buffer_index;
static struct st_tp_usb_packet_t usb_packet[2]; /* double buffering */
/* How many bytes we have transmitted. */
static size_t transmit_report_offset;
/* Function prototypes */
static int get_heat_map_addr(void);
static void print_frame(void);
static void st_tp_disable_heat_map(void);
static void st_tp_enable_heat_map(void);
static int st_tp_read_frame(void);
static void st_tp_interrupt_send(void);
DECLARE_DEFERRED(st_tp_interrupt_send);
#endif
/* Function implementations */
static void set_bits(int *lvalue, int rvalue, int mask)
{
*lvalue &= ~mask;
*lvalue |= rvalue & mask;
}
/*
* Parse a finger report from ST event and save it to (report)->finger.
*
* @param report: pointer to a USB HID touchpad report.
* @param event: a pointer event from ST.
* @param i: array index for next finger.
*
* @return array index of next finger (i.e. (i + 1) if a finger is added).
*/
static int st_tp_parse_finger(struct usb_hid_touchpad_report *report,
struct st_tp_event_t *event,
int i)
{
const int id = event->finger.touch_id;
/* This is not a finger */
if (event->finger.touch_type == ST_TP_TOUCH_TYPE_INVALID)
return i;
if (event->evt_id == ST_TP_EVENT_ID_ENTER_POINTER)
touch_slot |= 1 << id;
else if (event->evt_id == ST_TP_EVENT_ID_LEAVE_POINTER)
touch_slot &= ~BIT(id);
/* We cannot report more fingers */
if (i >= ARRAY_SIZE(report->finger)) {
CPRINTS("WARN: ST reports more than %d fingers", i);
return i;
}
switch (event->evt_id) {
case ST_TP_EVENT_ID_ENTER_POINTER:
case ST_TP_EVENT_ID_MOTION_POINTER:
/* Pressure == 255 is a palm. */
report->finger[i].confidence = (event->finger.z < 255);
report->finger[i].tip = 1;
report->finger[i].inrange = 1;
report->finger[i].id = id;
report->finger[i].pressure = event->finger.z;
report->finger[i].width = (event->finger.minor |
(event->minor_high << 4)) << 5;
report->finger[i].height = (event->finger.major |
(event->major_high << 4)) << 5;
report->finger[i].x = (CONFIG_USB_HID_TOUCHPAD_LOGICAL_MAX_X -
event->finger.x);
report->finger[i].y = (CONFIG_USB_HID_TOUCHPAD_LOGICAL_MAX_Y -
event->finger.y);
break;
case ST_TP_EVENT_ID_LEAVE_POINTER:
report->finger[i].id = id;
/* When a finger is leaving, it's not a palm */
report->finger[i].confidence = 1;
break;
}
return i + 1;
}
/*
* Read domeswitch level from touchpad, and save in `system_state`.
*
* After calling this function, use
* `system_state & SYSTEM_STATE_DOME_SWITCH_LEVEL`
* to get current value.
*
* @return error code on failure.
*/
static int st_tp_check_domeswitch_state(void)
{
int ret = st_tp_read_host_buffer_header();
if (ret)
return ret;
ret = rx_buf.buffer_header.flags & ST_TP_BUFFER_HEADER_DOMESWITCH_LVL;
/*
* Domeswitch level from device is inverted.
* That is, 0 => pressed, 1 => released.
*/
set_bits(&system_state,
ret ? 0 : SYSTEM_STATE_DOME_SWITCH_LEVEL,
SYSTEM_STATE_DOME_SWITCH_LEVEL);
return 0;
}
static int st_tp_write_hid_report(void)
{
int ret, i, num_finger, num_events;
const int old_system_state = system_state;
int domeswitch_changed;
struct usb_hid_touchpad_report report;
ret = st_tp_check_domeswitch_state();
if (ret)
return ret;
domeswitch_changed = ((old_system_state ^ system_state) &
SYSTEM_STATE_DOME_SWITCH_LEVEL);
num_events = st_tp_read_all_events(1);
if (tp_control)
return 1;
memset(&report, 0, sizeof(report));
report.id = REPORT_ID_TOUCHPAD;
num_finger = 0;
for (i = 0; i < num_events; i++) {
struct st_tp_event_t *e = &rx_buf.events[i];
switch (e->evt_id) {
case ST_TP_EVENT_ID_ENTER_POINTER:
case ST_TP_EVENT_ID_MOTION_POINTER:
case ST_TP_EVENT_ID_LEAVE_POINTER:
num_finger = st_tp_parse_finger(&report, e, num_finger);
break;
default:
break;
}
}
if (!num_finger && !domeswitch_changed) /* nothing changed */
return 0;
/* Don't report 0 finger click. */
if (num_finger && (system_state & SYSTEM_STATE_DOME_SWITCH_LEVEL))
report.button = 1;
report.count = num_finger;
report.timestamp = irq_ts / USB_HID_TOUCHPAD_TIMESTAMP_UNIT;
set_touchpad_report(&report);
return 0;
}
static int st_tp_read_report(void)
{
if (system_state & SYSTEM_STATE_ENABLE_HEAT_MAP) {
#ifdef CONFIG_USB_ISOCHRONOUS
/*
* Because we are using double buffering, so, if
* usb_buffer_index = N
*
* 1. spi_buffer_index == N => ok, both slots are empty
* 2. spi_buffer_index == N + 1 => ok, second slot is empty
* 3. spi_buffer_index == N + 2 => not ok, need to wait for USB
*/
if (spi_buffer_index - usb_buffer_index <= 1) {
if (st_tp_read_frame() == EC_SUCCESS) {
spi_buffer_index++;
if (system_state & SYSTEM_STATE_DEBUG_MODE) {
print_frame();
usb_buffer_index++;
}
}
}
if (spi_buffer_index > usb_buffer_index)
hook_call_deferred(&st_tp_interrupt_send_data, 0);
#endif
} else {
st_tp_write_hid_report();
}
return st_tp_send_ack();
}
static int st_tp_read_host_buffer_header(void)
{
const uint8_t tx_buf[] = { ST_TP_CMD_READ_SPI_HOST_BUFFER, 0x00, 0x00 };
int rx_len = ST_TP_DUMMY_BYTE + sizeof(rx_buf.buffer_header);
return spi_transaction(SPI, tx_buf, sizeof(tx_buf),
(uint8_t *)&rx_buf, rx_len);
}
static int st_tp_send_ack(void)
{
uint8_t tx_buf[] = { ST_TP_CMD_SPI_HOST_BUFFER_ACK };
return spi_transaction(SPI, tx_buf, sizeof(tx_buf), NULL, 0);
}
static int st_tp_update_system_state(int new_state, int mask)
{
int ret = EC_SUCCESS;
int need_locked_scan_mode = 0;
/* copy reserved bits */
set_bits(&new_state, system_state, ~mask);
mask = SYSTEM_STATE_DEBUG_MODE;
if ((new_state & mask) != (system_state & mask))
set_bits(&system_state, new_state, mask);
mask = SYSTEM_STATE_ENABLE_HEAT_MAP | SYSTEM_STATE_ENABLE_DOME_SWITCH;
if ((new_state & mask) != (system_state & mask)) {
uint8_t tx_buf[] = {
ST_TP_CMD_WRITE_FEATURE_SELECT,
0x05,
0
};
if (new_state & SYSTEM_STATE_ENABLE_HEAT_MAP) {
CPRINTS("Heatmap enabled");
tx_buf[2] |= BIT(0);
need_locked_scan_mode = 1;
} else {
CPRINTS("Heatmap disabled");
}
if (new_state & SYSTEM_STATE_ENABLE_DOME_SWITCH)
tx_buf[2] |= BIT(1);
ret = spi_transaction(SPI, tx_buf, sizeof(tx_buf), NULL, 0);
if (ret)
return ret;
set_bits(&system_state, new_state, mask);
}
mask = SYSTEM_STATE_ACTIVE_MODE;
if ((new_state & mask) != (system_state & mask)) {
uint8_t tx_buf[] = {
ST_TP_CMD_WRITE_SCAN_MODE_SELECT,
ST_TP_SCAN_MODE_ACTIVE,
!!(new_state & SYSTEM_STATE_ACTIVE_MODE),
};
CPRINTS("Enable Multi-Touch: %d", tx_buf[2]);
ret = spi_transaction(SPI, tx_buf, sizeof(tx_buf), NULL, 0);
if (ret)
return ret;
set_bits(&system_state, new_state, mask);
}
/*
* We need to lock scan mode to prevent scan rate drop when heat map
* mode is enabled.
*/
if (need_locked_scan_mode) {
uint8_t tx_buf[] = {
ST_TP_CMD_WRITE_SCAN_MODE_SELECT,
ST_TP_SCAN_MODE_LOCKED,
0x0,
};
ret = spi_transaction(SPI, tx_buf, sizeof(tx_buf), NULL, 0);
if (ret)
return ret;
}
return ret;
}
static void st_tp_enable_interrupt(int enable)
{
uint8_t tx_buf[] = {
ST_TP_CMD_WRITE_SYSTEM_COMMAND, 0x01, enable ? 1 : 0};
if (enable)
gpio_enable_interrupt(GPIO_TOUCHPAD_INT);
spi_transaction(SPI, tx_buf, sizeof(tx_buf), NULL, 0);
if (!enable)
gpio_disable_interrupt(GPIO_TOUCHPAD_INT);
}
static int st_tp_start_scan(void)
{
int new_state = (SYSTEM_STATE_ACTIVE_MODE |
SYSTEM_STATE_ENABLE_DOME_SWITCH);
int mask = new_state;
int ret;
CPRINTS("ST: Start scanning");
ret = st_tp_update_system_state(new_state, mask);
if (ret)
return ret;
st_tp_send_ack();
st_tp_enable_interrupt(1);
return ret;
}
static int st_tp_read_host_data_memory(uint16_t addr, void *rx_buf, int len)
{
uint8_t tx_buf[] = {
ST_TP_CMD_READ_HOST_DATA_MEMORY, addr >> 8, addr & 0xFF
};
return spi_transaction(SPI, tx_buf, sizeof(tx_buf), rx_buf, len);
}
static int st_tp_stop_scan(void)
{
int new_state = 0;
int mask = SYSTEM_STATE_ACTIVE_MODE;
int ret;
CPRINTS("ST: Stop scanning");
ret = st_tp_update_system_state(new_state, mask);
st_tp_enable_interrupt(0);
return ret;
}
static int st_tp_load_host_data(uint8_t mem_id)
{
uint8_t tx_buf[] = {
ST_TP_CMD_WRITE_SYSTEM_COMMAND, 0x06, mem_id
};
int retry, ret;
uint16_t count;
struct st_tp_host_data_header_t *header = &rx_buf.data_header;
int rx_len = sizeof(*header) + ST_TP_DUMMY_BYTE;
st_tp_read_host_data_memory(0x0000, &rx_buf, rx_len);
if (header->host_data_mem_id == mem_id)
return EC_SUCCESS; /* already loaded no need to reload */
count = header->count;
ret = spi_transaction(SPI, tx_buf, sizeof(tx_buf), NULL, 0);
if (ret)
return ret;
ret = EC_ERROR_TIMEOUT;
retry = 5;
while (retry--) {
st_tp_read_host_data_memory(0x0000, &rx_buf, rx_len);
if (header->magic == ST_TP_HEADER_MAGIC &&
header->host_data_mem_id == mem_id &&
header->count != count) {
ret = EC_SUCCESS;
break;
}
msleep(10);
}
return ret;
}
/*
* Read System Info from Host Data Memory.
*
* @param reload: true to force reloading system info into host data memory
* before reading.
*/
static int st_tp_read_system_info(int reload)
{
int ret = EC_SUCCESS;
int rx_len = ST_TP_DUMMY_BYTE + ST_TP_SYSTEM_INFO_LEN;
uint8_t *ptr = rx_buf.bytes;
if (reload)
ret = st_tp_load_host_data(ST_TP_MEM_ID_SYSTEM_INFO);
if (ret)
return ret;
ret = st_tp_read_host_data_memory(0x0000, &rx_buf, rx_len);
if (ret)
return ret;
/* Parse the content */
memcpy(&system_info, ptr, ST_TP_SYSTEM_INFO_PART_1_SIZE);
/* Check header */
if (system_info.header.magic != ST_TP_HEADER_MAGIC ||
system_info.header.host_data_mem_id != ST_TP_MEM_ID_SYSTEM_INFO)
return EC_ERROR_UNKNOWN;
ptr += ST_TP_SYSTEM_INFO_PART_1_SIZE;
ptr += ST_TP_SYSTEM_INFO_PART_1_RESERVED;
memcpy(&system_info.scr_res_x, ptr, ST_TP_SYSTEM_INFO_PART_2_SIZE);
#define ST_TP_SHOW(attr) CPRINTS(#attr ": %04x", system_info.attr)
ST_TP_SHOW(chip0_id[0]);
ST_TP_SHOW(chip0_id[1]);
ST_TP_SHOW(chip0_ver);
ST_TP_SHOW(scr_tx_len);
ST_TP_SHOW(scr_rx_len);
ST_TP_SHOW(release_info);
#undef ST_TP_SHOW
return ret;
}
/*
* Enable / disable deep sleep on memory and bus.
*
* Before calling dump_error() and dump_error(), deep sleep should be disabled,
* otherwise response data might be garbage.
*/
static void enable_deep_sleep(int enable)
{
uint8_t cmd[] = {0xFA, 0x20, 0x00, 0x00, 0x68, enable ? 0x0B : 0x08};
spi_transaction(SPI, cmd, sizeof(cmd), NULL, 0);
}
static void dump_error(void)
{
uint8_t tx_buf[] = {0xFB, 0x20, 0x01, 0xEF, 0x80};
int rx_len = sizeof(rx_buf.dump_info) + ST_TP_DUMMY_BYTE;
int i;
spi_transaction(SPI, tx_buf, sizeof(tx_buf),
(uint8_t *)&rx_buf, rx_len);
for (i = 0; i < ARRAY_SIZE(rx_buf.dump_info); i += 4)
CPRINTS("%08x %08x %08x %08x",
rx_buf.dump_info[i + 0], rx_buf.dump_info[i + 1],
rx_buf.dump_info[i + 2], rx_buf.dump_info[i + 3]);
msleep(8);
}
/*
* Dump entire 64K memory on touchpad.
*
* This is very time consuming. For now, let's disable this in production
* build.
*/
static void dump_memory(void)
{
uint32_t size = 0x10000, rx_len = 512 + ST_TP_DUMMY_BYTE;
uint32_t offset, i;
uint8_t cmd[] = {0xFB, 0x00, 0x10, 0x00, 0x00};
if (!dump_memory_on_error)
return;
for (offset = 0; offset < size; offset += 512) {
cmd[3] = (offset >> 8) & 0xFF;
cmd[4] = (offset >> 0) & 0xFF;
spi_transaction(SPI, cmd, sizeof(cmd),
(uint8_t *)&rx_buf, rx_len);
for (i = 0; i < rx_len - ST_TP_DUMMY_BYTE; i += 32) {
CPRINTF("%.4h %.4h %.4h %.4h %.4h %.4h %.4h %.4h\n",
rx_buf.bytes + i + 4 * 0,
rx_buf.bytes + i + 4 * 1,
rx_buf.bytes + i + 4 * 2,
rx_buf.bytes + i + 4 * 3,
rx_buf.bytes + i + 4 * 4,
rx_buf.bytes + i + 4 * 5,
rx_buf.bytes + i + 4 * 6,
rx_buf.bytes + i + 4 * 7);
msleep(8);
}
}
CPRINTF("===============================\n");
msleep(8);
}
/*
* Set `tp_control` if there are any actions should be taken.
*/
static void st_tp_handle_error(uint8_t error_type)
{
tp_control |= TP_CONTROL_SHALL_DUMP_ERROR;
/*
* Suggest action: memory dump and power cycle.
*/
if (error_type <= 0x06 ||
error_type == 0xF1 ||
error_type == 0xF2 ||
error_type == 0xF3 ||
(error_type >= 0x47 && error_type <= 0x4E)) {
tp_control |= TP_CONTROL_SHALL_RESET;
return;
}
/*
* Suggest action: FW shall halt, consult ST.
*/
if ((error_type >= 0x20 && error_type <= 0x23) ||
error_type == 0x25 ||
(error_type >= 0x2E && error_type <= 0x46)) {
CPRINTS("tp shall halt");
tp_control |= TP_CONTROL_SHALL_HALT;
return;
}
/*
* Corrupted panel configuration, a panel init should fix it.
*/
if (error_type >= 0x28 && error_type <= 0x29) {
tp_control |= TP_CONTROL_SHALL_INIT;
return;
}
/*
* Corrupted CX section, a full panel init should fix it.
*/
if (error_type >= 0xA0 && error_type <= 0xA6) {
tp_control |= TP_CONTROL_SHALL_INIT_FULL;
return;
}
/*
* When 0xFF is received, it's very likely ST touchpad is down.
* Try if touchpad can be recovered by reset.
*/
if (error_type == 0xFF) {
if (tp_reset_retry_count < MAX_TP_RESET_RETRY_COUNT) {
tp_control |= TP_CONTROL_SHALL_RESET;
tp_reset_retry_count++;
} else {
tp_control |= TP_CONTROL_SHALL_HALT;
}
return;
}
}
/*
* Handles error reports.
*/
static void st_tp_handle_error_report(struct st_tp_event_t *e)
{
uint8_t error_type = e->report.report_type;
CPRINTS("Touchpad error: %x %x", error_type,
((e->report.info[0] << 0) | (e->report.info[1] << 8) |
(e->report.info[2] << 16) | (e->report.info[3] << 24)));
st_tp_handle_error(error_type);
}
static void st_tp_handle_status_report(struct st_tp_event_t *e)
{
static uint32_t prev_idle_count;
uint32_t info = ((e->report.info[0] << 0) |
(e->report.info[1] << 8) |
(e->report.info[2] << 16) |
(e->report.info[3] << 24));
if (e->report.report_type == ST_TP_STATUS_FCAL ||
e->report.report_type == ST_TP_STATUS_FRAME_DROP)
CPRINTS("TP STATUS REPORT: %02x %08x",
e->report.report_type, info);
/*
* Idle count might not change if ST FW is busy (for example, when the
* user puts a big palm on touchpad). Therefore if idle count doesn't
* change, we need to double check with touch count.
*
* If touch count is 0, and idle count doesn't change, it means that:
*
* 1) ST doesn't think there are any fingers.
* 2) ST is busy on something, can't get into idle mode, and this
* might cause (1).
*
* Resetting touchpad should be the correct action.
*/
if (e->report.report_type == ST_TP_STATUS_BEACON) {
#if 0
const uint8_t touch_count = e->report.reserved;
CPRINTS("BEACON: idle count=%08x", info);
CPRINTS(" touch count=%d touch slot=%04x",
touch_count, touch_slot);
#endif
if (prev_idle_count == info && touch_slot == 0) {
CPRINTS(" idle count=%08x not changed", info);
tp_control |= TP_CONTROL_SHALL_RESET;
return;
}
prev_idle_count = info;
}
}
/*
* Read all events, and handle errors.
*
* When there are error events, suggested action will be saved in `tp_control`.
*
* @param show_error: weather EC should read and dump error or not.
* ***If this is true, rx_buf.events[] will be cleared.***
*
* @return number of events available
*/
static int st_tp_read_all_events(int show_error)
{
uint8_t cmd = ST_TP_CMD_READ_ALL_EVENTS;
int rx_len = sizeof(rx_buf.events) + ST_TP_DUMMY_BYTE;
int i;
if (spi_transaction(SPI, &cmd, 1, (uint8_t *)&rx_buf, rx_len))
return 0;
for (i = 0; i < ARRAY_SIZE(rx_buf.events); i++) {
struct st_tp_event_t *e = &rx_buf.events[i];
if (e->magic != ST_TP_EVENT_MAGIC)
break;
switch (e->evt_id) {
case ST_TP_EVENT_ID_ERROR_REPORT:
st_tp_handle_error_report(e);
break;
case ST_TP_EVENT_ID_STATUS_REPORT:
st_tp_handle_status_report(e);
break;
}
}
if (show_error && (tp_control & TP_CONTROL_SHALL_DUMP_ERROR)) {
enable_deep_sleep(0);
dump_error();
dump_memory();
enable_deep_sleep(1);
/* rx_buf.events[] is invalid now */
i = 0;
}
tp_control &= ~TP_CONTROL_SHALL_DUMP_ERROR;
return i;
}
/*
* Reset touchpad. This function will wait for "controller ready" event after
* the touchpad is reset.
*/
static int st_tp_reset(void)
{
int i, num_events, retry = 100;
board_touchpad_reset();
while (retry--) {
num_events = st_tp_read_all_events(0);
/*
* We are not doing full panel initialization, and error code
* suggest us to reset or halt.
*/
if (!(tp_control & (TP_CONTROL_INIT | TP_CONTROL_INIT_FULL)) &&
(tp_control & (TP_CONTROL_SHALL_HALT |
TP_CONTROL_SHALL_RESET)))
break;
for (i = 0; i < num_events; i++) {
struct st_tp_event_t *e = &rx_buf.events[i];
if (e->evt_id == ST_TP_EVENT_ID_CONTROLLER_READY) {
CPRINTS("Touchpad ready");
tp_reset_retry_count = 0;
return 0;
}
}
msleep(10);
}
CPRINTS("Timeout waiting for controller ready.");
return EC_ERROR_TIMEOUT;
}
/* Initialize the controller ICs after reset */
static void st_tp_init(void)
{
tp_control = 0;
system_state = 0;
if (st_tp_reset())
return;
if (tp_control) {
CPRINTS("tp_control = %x", tp_control);
return;
}
/*
* On boot, ST firmware will load system info to host data memory,
* So we don't need to reload it.
*/
st_tp_read_system_info(0);
system_state = SYSTEM_STATE_READY;
touch_slot = 0;
touchpad_power_control();
}
DECLARE_DEFERRED(st_tp_init);
#ifdef CONFIG_USB_UPDATE
int touchpad_get_info(struct touchpad_info *tp)
{
if (st_tp_read_system_info(1)) {
tp->status = EC_RES_SUCCESS;
tp->vendor = ST_VENDOR_ID;
/*
* failed to get system info, FW corrupted, return some default
* values.
*/
tp->st.id = 0x3936;
tp->st.fw_version = 0;
tp->st.fw_checksum = 0;
return sizeof(*tp);
}
tp->status = EC_RES_SUCCESS;
tp->vendor = ST_VENDOR_ID;
tp->st.id = (system_info.chip0_id[0] << 8) | system_info.chip0_id[1];
tp->st.fw_version = system_info.release_info;
tp->st.fw_checksum = system_info.fw_crc;
return sizeof(*tp);
}
/*
* Helper functions for firmware update
*
* There is no documentation about ST_TP_CMD_WRITE_HW_REG (0xFA).
* All implementations below are based on sample code from ST.
*/
static int write_hwreg_cmd32(uint32_t address, uint32_t data)
{
uint8_t tx_buf[] = {
ST_TP_CMD_WRITE_HW_REG,
(address >> 24) & 0xFF,
(address >> 16) & 0xFF,
(address >> 8) & 0xFF,
(address >> 0) & 0xFF,
(data >> 24) & 0xFF,
(data >> 16) & 0xFF,
(data >> 8) & 0xFF,
(data >> 0) & 0xFF,
};
return spi_transaction(SPI, tx_buf, sizeof(tx_buf), NULL, 0);
}
static int write_hwreg_cmd8(uint32_t address, uint8_t data)
{
uint8_t tx_buf[] = {
ST_TP_CMD_WRITE_HW_REG,
(address >> 24) & 0xFF,
(address >> 16) & 0xFF,
(address >> 8) & 0xFF,
(address >> 0) & 0xFF,
data,
};
return spi_transaction(SPI, tx_buf, sizeof(tx_buf), NULL, 0);
}
static int wait_for_flash_ready(uint8_t type)
{
uint8_t tx_buf[] = {
ST_TP_CMD_READ_HW_REG,
0x20, 0x00, 0x00, type,
};
int ret = EC_SUCCESS, retry = 200;
while (retry--) {
ret = spi_transaction(SPI, tx_buf, sizeof(tx_buf),
(uint8_t *)&rx_buf, 1 + ST_TP_DUMMY_BYTE);
if (ret == EC_SUCCESS && !(rx_buf.bytes[0] & 0x80))
break;
msleep(50);
}
return retry >= 0 ? ret : EC_ERROR_TIMEOUT;
}
static int erase_flash(int full_init_required)
{
int ret;
if (full_init_required)
ret = write_hwreg_cmd32(0x20000128, 0xFFFFFFFF);
else
/* Erase everything, except CX */
ret = write_hwreg_cmd32(0x20000128, 0xFFFFFF83);
if (ret)
return ret;
ret = write_hwreg_cmd8(0x2000006B, 0x00);
if (ret)
return ret;
ret = write_hwreg_cmd8(0x2000006A, 0xA0);
if (ret)
return ret;
return wait_for_flash_ready(0x6A);
}
static int st_tp_prepare_for_update(int full_init_required)
{
/* hold m3 */
write_hwreg_cmd8(0x20000024, 0x01);
/* unlock flash */
write_hwreg_cmd8(0x20000025, 0x20);
/* unlock flash erase */
write_hwreg_cmd8(0x200000DE, 0x03);
erase_flash(full_init_required);
return EC_SUCCESS;
}
static int st_tp_start_flash_dma(void)
{
int ret;
ret = write_hwreg_cmd8(0x20000071, 0xC0);
if (ret)
return ret;
ret = wait_for_flash_ready(0x71);
return ret;
}
static int st_tp_write_one_chunk(const uint8_t *head,
uint32_t addr, uint32_t chunk_size)
{
uint8_t tx_buf[ST_TP_DMA_CHUNK_SIZE + 5];
uint32_t index = 0;
int ret;
index = 0;
tx_buf[index++] = ST_TP_CMD_WRITE_HW_REG;
tx_buf[index++] = (addr >> 24) & 0xFF;
tx_buf[index++] = (addr >> 16) & 0xFF;
tx_buf[index++] = (addr >> 8) & 0xFF;
tx_buf[index++] = (addr >> 0) & 0xFF;
memcpy(tx_buf + index, head, chunk_size);
ret = spi_transaction(SPI, tx_buf, chunk_size + 5, NULL, 0);
return ret;
}
/*
* @param offset: offset in memory to copy the data (in bytes).
* @param size: length of data (in bytes).
* @param data: pointer to data bytes.
*/
static int st_tp_write_flash(int offset, int size, const uint8_t *data)
{
uint8_t tx_buf[12] = {0};
const uint8_t *head = data, *tail = data + size;
uint32_t addr, index, chunk_size;
uint32_t flash_buffer_size;
int ret;
offset >>= 2; /* offset should be count in words */
/*
* To write to flash, the data has to be separated into several chunks.
* Each chunk will be no more than `ST_TP_DMA_CHUNK_SIZE` bytes.
* The chunks will first be saved into a buffer, the buffer can only
* holds `ST_TP_FLASH_BUFFER_SIZE` bytes. We have to flush the buffer
* when the capacity is reached.
*/
while (head < tail) {
addr = 0x00100000;
flash_buffer_size = 0;
while (flash_buffer_size < ST_TP_FLASH_BUFFER_SIZE) {
chunk_size = MIN(ST_TP_DMA_CHUNK_SIZE, tail - head);
ret = st_tp_write_one_chunk(head, addr, chunk_size);
if (ret)
return ret;
flash_buffer_size += chunk_size;
addr += chunk_size;
head += chunk_size;
if (head >= tail)
break;
}
/* configuring the DMA */
flash_buffer_size = flash_buffer_size / 4 - 1;
index = 0;
tx_buf[index++] = ST_TP_CMD_WRITE_HW_REG;
tx_buf[index++] = 0x20;
tx_buf[index++] = 0x00;
tx_buf[index++] = 0x00;
tx_buf[index++] = 0x72; /* flash DMA config */
tx_buf[index++] = 0x00;
tx_buf[index++] = 0x00;
tx_buf[index++] = offset & 0xFF;
tx_buf[index++] = (offset >> 8) & 0xFF;
tx_buf[index++] = flash_buffer_size & 0xFF;
tx_buf[index++] = (flash_buffer_size >> 8) & 0xFF;
tx_buf[index++] = 0x00;
ret = spi_transaction(SPI, tx_buf, index, NULL, 0);
if (ret)
return ret;
ret = st_tp_start_flash_dma();
if (ret)
return ret;
offset += ST_TP_FLASH_BUFFER_SIZE / 4;
}
return EC_SUCCESS;
}
static int st_tp_check_command_echo(const uint8_t *cmd, const size_t len)
{
int num_events, i;
num_events = st_tp_read_all_events(0);
for (i = 0; i < num_events; i++) {
struct st_tp_event_t *e = &rx_buf.events[i];
if (e->evt_id == ST_TP_EVENT_ID_STATUS_REPORT &&
e->report.report_type == ST_TP_STATUS_CMD_ECHO &&
memcmp(e->report.info, cmd, MIN(4, len)) == 0)
return EC_SUCCESS;
}
return EC_ERROR_BUSY;
}
static uint8_t get_cx_version(uint8_t tp_version)
{
/*
* CX version is tracked by ST release note: go/whiskers-st-release-note
*/
if (tp_version >= 32)
return 3;
if (tp_version >= 20)
return 2;
if (tp_version >= 18)
return 1;
return 0;
}
/*
* Perform panel initialization.
*
* This function will wait until the initialization is done, or 10 second
* timeout is reached.
*
* @param full: 1 => force "full" panel initialization. Otherwise, tp_control
* will be checked to decide if full panel initialization is
* required.
*
* @return EC_SUCCESS or error code.
*/
static int st_tp_panel_init(int full)
{
uint8_t tx_buf[] = {
ST_TP_CMD_WRITE_SYSTEM_COMMAND, 0x00, 0x02
};
int ret, retry;
if (tp_control & (TP_CONTROL_INIT | TP_CONTROL_INIT_FULL))
return EC_ERROR_BUSY;
st_tp_stop_scan();
ret = st_tp_reset();
/*
* TODO(b:118312397): Figure out how to handle st_tp_reset errors (if
* needed at all).
*/
CPRINTS("st_tp_reset ret=%d", ret);
full |= tp_control & TP_CONTROL_SHALL_INIT_FULL;
if (full) {
/* should perform full panel initialization */
tx_buf[2] = 0x3;
tp_control = TP_CONTROL_INIT_FULL;
} else {
tp_control = TP_CONTROL_INIT;
}
CPRINTS("Start panel initialization (full=%d)", full);
spi_transaction(SPI, tx_buf, sizeof(tx_buf), NULL, 0);
retry = 100;
while (retry--) {
watchdog_reload();
msleep(100);
ret = st_tp_check_command_echo(tx_buf, sizeof(tx_buf));
if (ret == EC_SUCCESS) {
CPRINTS("Panel initialization completed.");
tp_control &= ~(TP_CONTROL_INIT | TP_CONTROL_INIT_FULL);
st_tp_init();
return EC_SUCCESS;
} else if (ret == EC_ERROR_BUSY) {
CPRINTS("Panel initialization on going...");
} else if (tp_control & ~(TP_CONTROL_INIT |
TP_CONTROL_INIT_FULL)) {
/* there are other kind of errors. */
CPRINTS("Panel initialization failed, tp_control: %x",
tp_control);
return EC_ERROR_UNKNOWN;
}
}
return EC_ERROR_TIMEOUT;
}
/*
* @param offset: should be address between 0 to 1M, aligned with
* ST_TP_DMA_CHUNK_SIZE.
* @param size: length of `data` array.
* @param data: content of new touchpad firmware.
*/
int touchpad_update_write(int offset, int size, const uint8_t *data)
{
static int full_init_required;
int ret, flash_offset;
CPRINTS("%s %08x %d", __func__, offset, size);
if (offset == 0) {
const struct st_tp_fw_header_t *header;
uint8_t old_cx_version;
uint8_t new_cx_version;
int retry;
header = (const struct st_tp_fw_header_t *)data;
if (header->signature != 0xAA55AA55)
return EC_ERROR_INVAL;
for (retry = 50; retry > 0; retry--) {
watchdog_reload();
if (system_state & SYSTEM_STATE_READY)
break;
if (retry % 10 == 0)
CPRINTS("TP not ready for update, "
"will check again");
msleep(100);
}
old_cx_version = get_cx_version(system_info.release_info);
new_cx_version = get_cx_version(header->release_info);
full_init_required = old_cx_version != new_cx_version;
/* stop scanning, interrupt, etc... */
st_tp_stop_scan();
ret = st_tp_prepare_for_update(full_init_required);
if (ret)
return ret;
return EC_SUCCESS;
}
flash_offset = offset - CONFIG_UPDATE_PDU_SIZE;
if (flash_offset % ST_TP_DMA_CHUNK_SIZE)
return EC_ERROR_INVAL;
if (flash_offset >= ST_TP_FLASH_OFFSET_PANEL_CFG &&
flash_offset < ST_TP_FLASH_OFFSET_CONFIG)
/* don't update CX section && panel config section */
return EC_SUCCESS;
ret = st_tp_write_flash(flash_offset, size, data);
if (ret)
return ret;
if (offset + size == CONFIG_TOUCHPAD_VIRTUAL_SIZE) {
CPRINTS("%s: End update, wait for reset.", __func__);
ret = st_tp_panel_init(full_init_required);
task_set_event(TASK_ID_TOUCHPAD, TASK_EVENT_TP_UPDATED, 0);
return ret;
}
return EC_SUCCESS;
}
int touchpad_debug(const uint8_t *param, unsigned int param_size,
uint8_t **data, unsigned int *data_size)
{
static uint8_t buf[8];
int num_events;
if (param_size != 1)
return EC_RES_INVALID_PARAM;
switch (*param) {
case ST_TP_DEBUG_CMD_RESET_TOUCHPAD:
*data = NULL;
*data_size = 0;
st_tp_stop_scan();
hook_call_deferred(&st_tp_init_data, 100 * MSEC);
return EC_SUCCESS;
case ST_TP_DEBUG_CMD_CALIBRATE:
/* no return value */
*data = NULL;
*data_size = 0;
st_tp_panel_init(1);
return EC_SUCCESS;
case ST_TP_DEBUG_CMD_START_SCAN:
*data = NULL;
*data_size = 0;
st_tp_start_scan();
return EC_SUCCESS;
case ST_TP_DEBUG_CMD_STOP_SCAN:
*data = NULL;
*data_size = 0;
st_tp_stop_scan();
return EC_SUCCESS;
case ST_TP_DEBUG_CMD_READ_BUF_HEADER:
*data = buf;
*data_size = 8;
st_tp_read_host_buffer_header();
memcpy(buf, rx_buf.bytes, *data_size);
CPRINTS("header: %.*h", *data_size, buf);
return EC_SUCCESS;
case ST_TP_DEBUG_CMD_READ_EVENTS:
num_events = st_tp_read_all_events(0);
if (num_events) {
int i;
for (i = 0; i < num_events; i++) {
CPRINTS("event[%d]: id=%d, type=%d",
i, rx_buf.events[i].evt_id,
rx_buf.events[i].report.report_type);
}
}
*data = buf;
*data_size = 1;
*data[0] = num_events;
st_tp_send_ack();
return EC_SUCCESS;
}
return EC_RES_INVALID_PARAM;
}
#endif
void touchpad_interrupt(enum gpio_signal signal)
{
irq_ts = __hw_clock_source_read();
task_wake(TASK_ID_TOUCHPAD);
}
static int touchpad_should_enable(void)
{
/* touchpad is not ready. */
if (tp_control)
return 0;
#ifdef CONFIG_USB_SUSPEND
if (usb_is_suspended() && !usb_is_remote_wakeup_enabled())
return 0;
#endif
#ifdef CONFIG_TABLET_MODE
if (tablet_get_mode())
return 0;
#endif
return 1;
}
/* Make a decision on touchpad power, based on USB and tablet mode status. */
static void touchpad_power_control(void)
{
const int enabled = !!(system_state & SYSTEM_STATE_ACTIVE_MODE);
int enable = touchpad_should_enable();
if (enabled == enable)
return;
if (enable)
st_tp_start_scan();
else
st_tp_stop_scan();
}
static void touchpad_read_idle_count(void)
{
static uint32_t prev_count;
uint32_t count;
int ret;
int rx_len = 2 + ST_TP_DUMMY_BYTE;
uint8_t cmd_read_counter[] = {
0xFB, 0x00, 0x10, 0xff, 0xff
};
/* Find address of idle count. */
ret = st_tp_load_host_data(ST_TP_MEM_ID_SYSTEM_INFO);
if (ret)
return;
st_tp_read_host_data_memory(0x0082, &rx_buf, rx_len);
/* Fill in address of idle count, the byte order is reversed. */
cmd_read_counter[3] = rx_buf.bytes[1];
cmd_read_counter[4] = rx_buf.bytes[0];
/* Read idle count */
spi_transaction(SPI, cmd_read_counter, sizeof(cmd_read_counter),
(uint8_t *)&rx_buf, 4 + ST_TP_DUMMY_BYTE);
count = rx_buf.dump_info[0];
CPRINTS("idle_count = %08x", count);
if (count == prev_count)
CPRINTS("counter doesn't change...");
else
prev_count = count;
}
/*
* Try to collect symptoms of type B error.
*
* There are three possible symptoms:
* 1. error dump section is corrupted / contains error.
* 2. memory stack is corrupted (not 0xCC).
* 3. idle count is not changing.
*/
static void touchpad_collect_error(void)
{
const uint8_t tx_dump_error[] = {
0xFB, 0x20, 0x01, 0xEF, 0x80
};
uint32_t dump_info[2];
const uint8_t tx_dump_memory[] = {
0xFB, 0x00, 0x10, 0x00, 0x00
};
uint32_t dump_memory[16];
int i;
enable_deep_sleep(0);
spi_transaction(SPI, tx_dump_error, sizeof(tx_dump_error),
(uint8_t *)&rx_buf,
sizeof(dump_info) + ST_TP_DUMMY_BYTE);
memcpy(dump_info, rx_buf.bytes, sizeof(dump_info));
spi_transaction(SPI, tx_dump_memory, sizeof(tx_dump_memory),
(uint8_t *)&rx_buf,
sizeof(dump_memory) + ST_TP_DUMMY_BYTE);
memcpy(dump_memory, rx_buf.bytes, sizeof(dump_memory));
CPRINTS("check error dump: %08x %08x", dump_info[0], dump_info[1]);
CPRINTS("check memory dump:");
for (i = 0; i < ARRAY_SIZE(dump_memory); i += 8) {
CPRINTF("%08x %08x %08x %08x %08x %08x %08x %08x\n",
dump_memory[i + 0],
dump_memory[i + 1],
dump_memory[i + 2],
dump_memory[i + 3],
dump_memory[i + 4],
dump_memory[i + 5],
dump_memory[i + 6],
dump_memory[i + 7]);
}
for (i = 0; i < 3; i++)
touchpad_read_idle_count();
enable_deep_sleep(1);
tp_control |= TP_CONTROL_SHALL_RESET;
}
void touchpad_task(void *u)
{
uint32_t event;
while (1) {
uint32_t retry;
for (retry = 0; retry < 3; retry++) {
CPRINTS("st_tp_init: trial %d", retry + 1);
st_tp_init();
if (system_state & SYSTEM_STATE_READY)
break;
/*
* React on touchpad errors.
*/
if (tp_control & TP_CONTROL_SHALL_INIT_FULL) {
/* suppress other handlers */
tp_control = TP_CONTROL_SHALL_INIT_FULL;
st_tp_panel_init(1);
} else if (tp_control & TP_CONTROL_SHALL_INIT) {
/* suppress other handlers */
tp_control = TP_CONTROL_SHALL_INIT;
st_tp_panel_init(0);
} else if (tp_control & TP_CONTROL_SHALL_RESET) {
/* suppress other handlers */
tp_control = TP_CONTROL_SHALL_RESET;
} else if (tp_control & TP_CONTROL_SHALL_HALT) {
CPRINTS("shall halt");
tp_control = 0;
break;
}
}
if (system_state & SYSTEM_STATE_READY)
break;
/* failed to init, mark it as ready to allow upgrade */
system_state = SYSTEM_STATE_READY;
/* wait for upgrade complete */
task_wait_event_mask(TASK_EVENT_TP_UPDATED, -1);
}
touchpad_power_control();
while (1) {
/* wait for at most 3 seconds */
event = task_wait_event(3 * 1000 * 1000);
if ((event & TASK_EVENT_TIMER) &&
(system_state & SYSTEM_STATE_ACTIVE_MODE))
/*
* Haven't received anything for 3 seconds, and we are
* supposed to be in active mode. This is not normal,
* check for errors and reset.
*/
touchpad_collect_error();
if (event & TASK_EVENT_WAKE)
while (!tp_control &&
!gpio_get_level(GPIO_TOUCHPAD_INT))
st_tp_read_report();
/*
* React on touchpad errors.
*/
if (tp_control & TP_CONTROL_SHALL_INIT_FULL) {
/* suppress other handlers */
tp_control = TP_CONTROL_SHALL_INIT_FULL;
st_tp_panel_init(1);
} else if (tp_control & TP_CONTROL_SHALL_INIT) {
/* suppress other handlers */
tp_control = TP_CONTROL_SHALL_INIT;
st_tp_panel_init(0);
} else if (tp_control & TP_CONTROL_SHALL_RESET) {
/* suppress other handlers */
tp_control = TP_CONTROL_SHALL_RESET;
st_tp_init();
} else if (tp_control & TP_CONTROL_SHALL_HALT) {
tp_control = 0;
st_tp_stop_scan();
}
if (event & TASK_EVENT_POWER)
touchpad_power_control();
}
}
/*
* When USB PM status changes, or tablet mode changes, call in the main task to
* decide whether to turn touchpad on or off.
*/
#if defined(CONFIG_USB_SUSPEND) || defined(CONFIG_TABLET_MODE)
static void touchpad_power_change(void)
{
task_set_event(TASK_ID_TOUCHPAD, TASK_EVENT_POWER, 0);
}
#endif
#ifdef CONFIG_USB_SUSPEND
DECLARE_HOOK(HOOK_USB_PM_CHANGE, touchpad_power_change, HOOK_PRIO_DEFAULT);
#endif
#ifdef CONFIG_TABLET_MODE
DECLARE_HOOK(HOOK_TABLET_MODE_CHANGE, touchpad_power_change, HOOK_PRIO_DEFAULT);
#endif
#ifdef CONFIG_USB_ISOCHRONOUS
static void st_tp_enable_heat_map(void)
{
int new_state = (SYSTEM_STATE_ENABLE_HEAT_MAP |
SYSTEM_STATE_ENABLE_DOME_SWITCH |
SYSTEM_STATE_ACTIVE_MODE);
int mask = new_state;
st_tp_update_system_state(new_state, mask);
}
DECLARE_DEFERRED(st_tp_enable_heat_map);
static void st_tp_disable_heat_map(void)
{
int new_state = 0;
int mask = SYSTEM_STATE_ENABLE_HEAT_MAP;
st_tp_update_system_state(new_state, mask);
}
DECLARE_DEFERRED(st_tp_disable_heat_map);
static void print_frame(void)
{
char debug_line[ST_TOUCH_COLS + 5];
int i, j, index;
int v;
struct st_tp_usb_packet_t *packet = &usb_packet[usb_buffer_index & 1];
if (usb_buffer_index == spi_buffer_index)
/* buffer is empty. */
return;
/* We will have ~150 FPS, let's print ~4 frames per second */
if (usb_buffer_index % 37 == 0) {
/* move cursor back to top left corner */
CPRINTF("\x1b[H");
CPUTS("==============\n");
for (i = 0; i < ST_TOUCH_ROWS; i++) {
for (j = 0; j < ST_TOUCH_COLS; j++) {
index = i * ST_TOUCH_COLS;
index += (ST_TOUCH_COLS - j - 1); // flip X
v = packet->frame[index];
if (v > 0)
debug_line[j] = '0' + v * 10 / 256;
else
debug_line[j] = ' ';
}
debug_line[j++] = '\n';
debug_line[j++] = '\0';
CPRINTF(debug_line);
}
CPUTS("==============\n");
}
}
static int st_tp_read_frame(void)
{
int ret = EC_SUCCESS;
int rx_len = ST_TOUCH_FRAME_SIZE + ST_TP_DUMMY_BYTE;
int heat_map_addr = get_heat_map_addr();
uint8_t tx_buf[] = {
ST_TP_CMD_READ_SPI_HOST_BUFFER,
(heat_map_addr >> 8) & 0xFF,
(heat_map_addr >> 0) & 0xFF,
};
/*
* Since usb_packet.frame is already ane uint8_t byte array, we can just
* make it the RX buffer for SPI transaction.
*
* When there is a dummy byte, since we know that flags is a one byte
* value, and we will override it later, it's okay for SPI transaction
* to write the dummy byte to flags address.
*/
#if ST_TP_DUMMY_BYTE == 1
BUILD_ASSERT(sizeof(usb_packet[0].flags) == 1);
uint8_t *rx_buf = &usb_packet[spi_buffer_index & 1].flags;
#else
uint8_t *rx_buf = usb_packet[spi_buffer_index & 1].frame;
#endif
st_tp_read_all_events(1);
if (tp_control) {
ret = EC_ERROR_UNKNOWN;
goto failed;
}
if (heat_map_addr < 0)
goto failed;
ret = st_tp_check_domeswitch_state();
if (ret)
goto failed;
/*
* Theoretically, we should read host buffer header to check if data is
* valid, but the data should always be ready when interrupt pin is low.
* Let's skip this check for now.
*/
ret = spi_transaction(SPI, tx_buf, sizeof(tx_buf),
(uint8_t *)rx_buf, rx_len);
if (ret == EC_SUCCESS) {
int i;
uint8_t *dest = usb_packet[spi_buffer_index & 1].frame;
uint8_t max_value = 0;
for (i = 0; i < ST_TOUCH_COLS * ST_TOUCH_ROWS; i++)
max_value |= dest[i];
if (max_value == 0) // empty frame
return -1;
usb_packet[spi_buffer_index & 1].flags = 0;
if (system_state & SYSTEM_STATE_DOME_SWITCH_LEVEL)
usb_packet[spi_buffer_index & 1].flags |=
USB_FRAME_FLAGS_BUTTON;
}
failed:
return ret;
}
/* Define USB interface for heat_map */
/* function prototypes */
static int st_tp_usb_set_interface(usb_uint alternate_setting,
usb_uint interface);
static int heatmap_send_packet(struct usb_isochronous_config const *config);
static void st_tp_usb_tx_callback(struct usb_isochronous_config const *config);
/* USB descriptors */
USB_ISOCHRONOUS_CONFIG_FULL(usb_st_tp_heatmap_config,
USB_IFACE_ST_TOUCHPAD,
USB_CLASS_VENDOR_SPEC,
USB_SUBCLASS_GOOGLE_HEATMAP,
USB_PROTOCOL_GOOGLE_HEATMAP,
USB_STR_HEATMAP_NAME, /* interface name */
USB_EP_ST_TOUCHPAD,
USB_ISO_PACKET_SIZE,
st_tp_usb_tx_callback,
st_tp_usb_set_interface,
1 /* 1 extra EP for interrupts */)
/* ***This function will be executed in interrupt context*** */
void st_tp_usb_tx_callback(struct usb_isochronous_config const *config)
{
task_wake(TASK_ID_HEATMAP);
}
void heatmap_task(void *unused)
{
struct usb_isochronous_config const *config;
config = &usb_st_tp_heatmap_config;
while (1) {
/* waiting st_tp_usb_tx_callback() */
task_wait_event(-1);
if (system_state & SYSTEM_STATE_DEBUG_MODE)
continue;
if (usb_buffer_index == spi_buffer_index)
/* buffer is empty */
continue;
while (heatmap_send_packet(config))
/* We failed to write a packet, try again later. */
task_wait_event(100);
}
}
/* USB interface has completed TX, it's asking for more data */
static int heatmap_send_packet(struct usb_isochronous_config const *config)
{
size_t num_byte_available;
size_t offset = 0;
int ret, buffer_id = -1;
struct st_tp_usb_packet_t *packet = &usb_packet[usb_buffer_index & 1];
packet_header.flags = 0;
num_byte_available = sizeof(*packet) - transmit_report_offset;
if (num_byte_available > 0) {
if (transmit_report_offset == 0)
packet_header.flags |= HEADER_FLAGS_NEW_FRAME;
ret = usb_isochronous_write_buffer(
config,
(uint8_t *)&packet_header,
sizeof(packet_header),
offset,
&buffer_id,
0);
/*
* Since USB_ISO_PACKET_SIZE > sizeof(packet_header), this must
* be true.
*/
if (ret != sizeof(packet_header))
return -1;
offset += ret;
packet_header.index++;
ret = usb_isochronous_write_buffer(
config,
(uint8_t *)packet + transmit_report_offset,
num_byte_available,
offset,
&buffer_id,
1);
if (ret < 0) {
/*
* TODO(b/70482333): handle this error, it might be:
* 1. timeout (buffer_id changed)
* 2. invalid offset
*
* For now, let's just return an error and try again.
*/
CPRINTS("%s %d: %d", __func__, __LINE__, -ret);
return ret;
}
/* We should have sent some bytes, update offset */
transmit_report_offset += ret;
if (transmit_report_offset == sizeof(*packet)) {
transmit_report_offset = 0;
usb_buffer_index++;
}
}
return 0;
}
static int st_tp_usb_set_interface(usb_uint alternate_setting,
usb_uint interface)
{
if (alternate_setting == 1) {
if ((system_info.release_info & 0xFF) <
ST_TP_MIN_HEATMAP_VERSION) {
CPRINTS("release version %04x doesn't support heatmap",
system_info.release_info);
/* Heatmap mode is not supported in this version. */
return -1;
}
hook_call_deferred(&st_tp_enable_heat_map_data, 0);
return 0;
} else if (alternate_setting == 0) {
hook_call_deferred(&st_tp_disable_heat_map_data, 0);
return 0;
} else /* we only have two settings. */
return -1;
}
static int get_heat_map_addr(void)
{
/*
* TODO(stimim): drop this when we are sure all trackpads are having the
* same config (e.g. after EVT).
*/
if (system_info.release_info >= 0x3)
return 0x0120;
else if (system_info.release_info == 0x1)
return 0x20;
else
return -1; /* Unknown version */
}
struct st_tp_interrupt_t {
#define ST_TP_INT_FRAME_AVAILABLE BIT(0)
uint8_t flags;
} __packed;
static usb_uint st_tp_usb_int_buffer[
DIV_ROUND_UP(sizeof(struct st_tp_interrupt_t), 2)] __usb_ram;
const struct usb_endpoint_descriptor USB_EP_DESC(USB_IFACE_ST_TOUCHPAD, 81) = {
.bLength = USB_DT_ENDPOINT_SIZE,
.bDescriptorType = USB_DT_ENDPOINT,
.bEndpointAddress = 0x80 | USB_EP_ST_TOUCHPAD_INT,
.bmAttributes = 0x03 /* Interrupt endpoint */,
.wMaxPacketSize = sizeof(struct st_tp_interrupt_t),
.bInterval = 1 /* ms */,
};
static void st_tp_interrupt_send(void)
{
struct st_tp_interrupt_t report;
memset(&report, 0, sizeof(report));
if (usb_buffer_index < spi_buffer_index)
report.flags |= ST_TP_INT_FRAME_AVAILABLE;
memcpy_to_usbram((void *)usb_sram_addr(st_tp_usb_int_buffer),
&report, sizeof(report));
/* enable TX */
STM32_TOGGLE_EP(USB_EP_ST_TOUCHPAD_INT, EP_TX_MASK, EP_TX_VALID, 0);
usb_wake();
}
static void st_tp_interrupt_tx(void)
{
STM32_USB_EP(USB_EP_ST_TOUCHPAD_INT) &= EP_MASK;
if (usb_buffer_index < spi_buffer_index)
/* pending frames */
hook_call_deferred(&st_tp_interrupt_send_data, 0);
}
static void st_tp_interrupt_event(enum usb_ep_event evt)
{
int ep = USB_EP_ST_TOUCHPAD_INT;
if (evt == USB_EVENT_RESET) {
btable_ep[ep].tx_addr = usb_sram_addr(st_tp_usb_int_buffer);
btable_ep[ep].tx_count = sizeof(struct st_tp_interrupt_t);
STM32_USB_EP(ep) = ((ep << 0) |
EP_TX_VALID |
(3 << 9) /* interrupt EP */ |
EP_RX_DISAB);
}
}
USB_DECLARE_EP(USB_EP_ST_TOUCHPAD_INT, st_tp_interrupt_tx, st_tp_interrupt_tx,
st_tp_interrupt_event);
#endif
/* Debugging commands */
static int command_touchpad_st(int argc, char **argv)
{
if (argc < 2)
return EC_ERROR_PARAM_COUNT;
if (strcasecmp(argv[1], "version") == 0) {
st_tp_read_system_info(1);
return EC_SUCCESS;
} else if (strcasecmp(argv[1], "calibrate") == 0) {
st_tp_panel_init(1);
return EC_SUCCESS;
} else if (strcasecmp(argv[1], "enable") == 0) {
#ifdef CONFIG_USB_ISOCHRONOUS
set_bits(&system_state, SYSTEM_STATE_DEBUG_MODE,
SYSTEM_STATE_DEBUG_MODE);
hook_call_deferred(&st_tp_enable_heat_map_data, 0);
return 0;
#else
return EC_ERROR_NOT_HANDLED;
#endif
} else if (strcasecmp(argv[1], "disable") == 0) {
#ifdef CONFIG_USB_ISOCHRONOUS
set_bits(&system_state, 0, SYSTEM_STATE_DEBUG_MODE);
hook_call_deferred(&st_tp_disable_heat_map_data, 0);
return 0;
#else
return EC_ERROR_NOT_HANDLED;
#endif
} else if (strcasecmp(argv[1], "dump") == 0) {
enable_deep_sleep(0);
dump_error();
dump_memory();
enable_deep_sleep(1);
return EC_SUCCESS;
} else if (strcasecmp(argv[1], "memory_dump") == 0) {
if (argc == 3 && !parse_bool(argv[2], &dump_memory_on_error))
return EC_ERROR_PARAM2;
ccprintf("memory_dump: %d\n", dump_memory_on_error);
return EC_SUCCESS;
} else {
return EC_ERROR_PARAM1;
}
}
DECLARE_CONSOLE_COMMAND(touchpad_st, command_touchpad_st,
"<enable | disable | version | calibrate | dump | "
"memory_dump <enable|disable>>",
"Read write spi. id is spi_devices array index");
|