summaryrefslogtreecommitdiff
path: root/drivers/usb/typec/tcpm/google/tcpci_max77759.c
blob: 7037d1deb311bebcc44f1023a0013c92a9c6f266 (plain)
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
2910
2911
2912
2913
2914
2915
2916
2917
2918
2919
2920
2921
2922
2923
2924
2925
2926
2927
2928
2929
2930
2931
2932
2933
2934
2935
2936
2937
2938
2939
2940
2941
2942
2943
2944
2945
2946
2947
2948
2949
2950
2951
2952
2953
2954
2955
2956
2957
2958
2959
2960
2961
2962
2963
2964
2965
2966
2967
2968
2969
2970
2971
2972
2973
2974
2975
2976
2977
2978
2979
2980
2981
2982
2983
2984
2985
2986
2987
2988
2989
2990
2991
2992
2993
2994
2995
2996
2997
2998
2999
3000
3001
3002
3003
3004
3005
3006
3007
3008
3009
3010
3011
3012
3013
3014
3015
3016
3017
3018
3019
3020
3021
3022
3023
3024
3025
3026
3027
3028
3029
3030
3031
3032
3033
3034
3035
3036
3037
3038
3039
3040
3041
3042
3043
3044
3045
3046
3047
3048
3049
3050
3051
3052
3053
3054
3055
3056
3057
3058
3059
3060
3061
3062
3063
3064
3065
3066
3067
3068
3069
3070
3071
3072
3073
3074
3075
3076
3077
3078
3079
3080
3081
3082
3083
3084
3085
3086
3087
3088
3089
3090
3091
3092
3093
3094
3095
3096
3097
3098
3099
3100
3101
3102
3103
3104
3105
3106
3107
3108
3109
3110
3111
3112
3113
3114
3115
3116
3117
3118
3119
3120
3121
3122
3123
3124
3125
3126
3127
3128
3129
3130
3131
3132
3133
3134
3135
3136
3137
3138
3139
3140
3141
3142
3143
3144
3145
3146
3147
3148
3149
3150
3151
3152
3153
3154
3155
3156
3157
3158
3159
3160
3161
3162
3163
3164
3165
3166
3167
3168
3169
3170
3171
3172
3173
3174
3175
3176
3177
3178
3179
3180
3181
3182
3183
3184
3185
3186
3187
3188
3189
3190
3191
3192
3193
3194
3195
3196
3197
3198
3199
3200
3201
3202
3203
3204
3205
3206
3207
3208
3209
3210
3211
3212
3213
3214
3215
3216
3217
3218
3219
3220
3221
3222
3223
3224
3225
3226
3227
3228
3229
3230
3231
3232
3233
3234
3235
3236
3237
3238
3239
3240
3241
3242
3243
3244
3245
3246
3247
3248
3249
3250
3251
3252
3253
3254
3255
3256
3257
3258
3259
3260
3261
3262
3263
3264
3265
3266
3267
3268
3269
3270
3271
3272
3273
3274
3275
3276
3277
3278
3279
3280
3281
3282
3283
3284
3285
3286
3287
3288
3289
3290
3291
3292
3293
3294
3295
3296
3297
3298
3299
3300
3301
3302
3303
3304
3305
3306
3307
3308
3309
3310
3311
3312
3313
3314
3315
3316
3317
3318
3319
3320
3321
3322
3323
3324
3325
3326
3327
3328
3329
3330
3331
3332
3333
3334
3335
3336
3337
3338
3339
3340
3341
3342
3343
3344
3345
3346
3347
3348
3349
3350
3351
3352
3353
3354
3355
3356
3357
3358
3359
3360
3361
3362
3363
3364
3365
3366
3367
3368
3369
3370
3371
3372
3373
3374
3375
3376
3377
3378
3379
3380
3381
3382
3383
3384
3385
3386
3387
3388
3389
3390
3391
3392
3393
3394
3395
3396
3397
3398
3399
3400
3401
3402
3403
3404
3405
3406
3407
3408
3409
3410
3411
3412
3413
3414
3415
3416
3417
3418
3419
3420
3421
3422
3423
3424
3425
3426
3427
3428
3429
3430
3431
3432
3433
3434
3435
3436
3437
3438
3439
3440
3441
3442
3443
3444
3445
3446
3447
3448
3449
3450
3451
3452
3453
3454
3455
3456
3457
3458
3459
3460
3461
3462
3463
3464
3465
3466
3467
3468
3469
3470
3471
3472
3473
3474
3475
3476
3477
3478
3479
3480
3481
3482
3483
3484
3485
3486
3487
3488
3489
3490
3491
3492
3493
3494
3495
3496
3497
3498
3499
3500
3501
3502
3503
3504
3505
3506
3507
3508
3509
3510
3511
3512
3513
3514
3515
3516
3517
3518
3519
3520
3521
3522
3523
3524
3525
3526
3527
3528
3529
3530
3531
3532
3533
3534
3535
3536
3537
3538
3539
3540
// SPDX-License-Identifier: GPL-2.0-only
/*
 * Copyright (C) 2019, Google LLC
 *
 * MAX77759 TCPCI driver
 */

#include <linux/debugfs.h>
#include <linux/extcon.h>
#include <linux/extcon-provider.h>
#include <linux/gpio.h>
#include <linux/gpio/consumer.h>
#include <linux/interrupt.h>
#include <linux/i2c.h>
#include <linux/kernel.h>
#include <linux/module.h>
#include <linux/of_gpio.h>
#include <linux/power_supply.h>
#include <linux/regmap.h>
#include <linux/regulator/consumer.h>
#include <linux/spinlock.h>
#include <linux/usb/pd.h>
#include <linux/usb/pd_vdo.h>
#include <linux/usb/tcpm.h>
#include <linux/usb/typec.h>
#include <linux/usb/typec_dp.h>
#include <linux/usb/typec_mux.h>
#include <misc/logbuffer.h>
#include <trace/hooks/typec.h>

#include "bc_max77759.h"
#include <linux/usb/max77759_export.h>
#include "max77759_helper.h"
/* This header comes from the GKI kernel tree */
#include <linux/usb/tcpci.h>
#include "tcpci_max77759.h"
#include "tcpci_max77759_vendor_reg.h"
#include "usb_icl_voter.h"
#include "usb_psy.h"
#include "usb_thermal_voter.h"

#define TCPCI_MODE_VOTER	"TCPCI"
#define LIMIT_SINK_VOTER	"LIMIT_SINK_CURRENT_VOTER"
#define LIMIT_ACCESSORY_VOTER	"LIMIT_ACCESSORY_CURRENT_VOTER"

#define AICL_ACTIVE_EL "AICL_ACTIVE_EL"

#define TCPC_RECEIVE_BUFFER_COUNT_OFFSET                0
#define TCPC_RECEIVE_BUFFER_FRAME_TYPE_OFFSET           1
#define TCPC_RECEIVE_BUFFER_RX_BYTE_BUF_OFFSET          2

#define TCPCI_HI_Z_CC		0xf
/*
 * LongMessage not supported, hence 32 bytes for buf to be read from RECEIVE_BUFFER.
 * DEVICE_CAPABILITIES_2.LongMessage = 0, the value in READABLE_BYTE_COUNT reg shall be
 * less than or equal to 31. Since, RECEIVE_BUFFER len = 31 + 1(READABLE_BYTE_COUNT).
 */
#define TCPC_RECEIVE_BUFFER_LEN                         32

#define PD_ACTIVITY_TIMEOUT_MS				10000
#define IO_ERROR_RETRY_MS				3000
#define VSAFE0V_DEBOUNCE_MS				15
#define VBUS_RAMPUP_TIMEOUT_MS				250
#define VBUS_RAMPUP_MAX_RETRY				8

#define GBMS_MODE_VOTABLE "CHARGER_MODE"

#define MAX77759_DEVICE_ID_A1				0x2

#define MAX77759_DISABLE_TOGGLE				1
#define MAX77759_ENABLE_TOGGLE				0
/* Vote value doesn't matter. Only status matters. */
#define MAX77759_DISABLE_TOGGLE_VOTE			1

/* Longer delay to accommodate additional Rp update latency during boot. */
#define MAX77759_FIRST_RP_MISSING_TIMEOUT_MS           5000
#define MAX77759_RP_MISSING_TIMEOUT_MS                 2000

#define AICL_CHECK_MS				       10000

/* system use cases */
enum gbms_charger_modes {
	GBMS_USB_BUCK_ON	= 0x30,
	GBMS_USB_OTG_ON		= 0x31,
	GBMS_USB_OTG_FRS_ON	= 0x32,
};

#define CONTAMINANT_DETECT_DISABLE	0
#define CONTAMINANT_DETECT_AP		1
#define CONTAMINANT_DETECT_MAXQ		2

#define TCPM_RESTART_TOGGLING		0
#define CONTAMINANT_HANDLES_TOGGLING	1

#define VOLTAGE_ALARM_HI_EN_MV		3000
#define VOLTAGE_ALARM_HI_DIS_MV		21000
#define VOLTAGE_ALARM_LOW_EN_MV		1500
#define VOLTAGE_ALARM_LOW_DIS_MV	0
#define VBUS_PRESENT_THRESHOLD_MV	4000

#define TCPC_ALERT_VENDOR		BIT(15)

#define FLOATING_CABLE_OR_SINK_INSTANCE_THRESHOLD	10
#define AUTO_ULTRA_LOW_POWER_MODE_REENABLE_MS		600000

#define REGMAP_REG_MAX_ADDR			0x95
#define REGMAP_REG_COUNT			(REGMAP_REG_MAX_ADDR + 1)

#define tcpc_presenting_rd(reg, cc) \
	(!(TCPC_ROLE_CTRL_DRP & (reg)) && \
	 (((reg) & (TCPC_ROLE_CTRL_## cc ##_MASK << TCPC_ROLE_CTRL_## cc ##_SHIFT)) == \
	  (TCPC_ROLE_CTRL_CC_RD << TCPC_ROLE_CTRL_## cc ##_SHIFT)))

#define cc_open_or_toggling(cc1, cc2) \
	(((cc1) == TYPEC_CC_OPEN) && ((cc2) == TYPEC_CC_OPEN))

#define rp_3a_detected(cc1, cc2) \
	((((cc1) == TYPEC_CC_RP_3_0) && ((cc2) == TYPEC_CC_OPEN)) || \
	 (((cc1) == TYPEC_CC_OPEN) && ((cc2) == TYPEC_CC_RP_3_0)))

#define rp_1a5_detected(cc1, cc2) \
	((((cc1) == TYPEC_CC_RP_1_5) && ((cc2) == TYPEC_CC_OPEN)) || \
	 (((cc1) == TYPEC_CC_OPEN) && ((cc2) == TYPEC_CC_RP_1_5)))

#define rp_def_detected(cc1, cc2) \
	((((cc1) == TYPEC_CC_RP_DEF) && ((cc2) == TYPEC_CC_OPEN)) || \
	 (((cc1) == TYPEC_CC_OPEN) && ((cc2) == TYPEC_CC_RP_DEF)))

#define port_is_sink(cc1, cc2) \
	(rp_def_detected(cc1, cc2) || rp_1a5_detected(cc1, cc2) || rp_3a_detected(cc1, cc2))

#define is_debug_accessory_detected(cc1, cc2) \
	((((cc1) == TYPEC_CC_RP_DEF) || ((cc1) == TYPEC_CC_RP_1_5) || ((cc1) == TYPEC_CC_RP_3_0)) && \
	 (((cc1) == TYPEC_CC_RP_DEF) || ((cc1) == TYPEC_CC_RP_1_5) || ((cc1) == TYPEC_CC_RP_3_0)))

#define FLOATING_CABLE_INSTANCE_THRESHOLD	5
#define AUTO_ULTRA_LOW_POWER_MODE_REENABLE_MS	600000

#define VOLTAGE_DP_AUX_DEFAULT_UV	3300000

#define SRC_CURRENT_LIMIT_MA		0

#define DISCONNECT_DEBOUNCE_MS		1200

#define LOG_LVL_DEBUG				1
#define LOG_LVL_INFO				2

/*
 * Set CURRENT_LOG_LEVEL to 0 in order to disable all logging activity, else set it to desired
 * value to increase or decrease verbosity.
 */
#define CURRENT_LOG_LEVEL			LOG_LVL_DEBUG

#define LOG(LOG_LEVEL, LOG, FMT, ...)		\
do {						\
	if (LOG_LEVEL <= CURRENT_LOG_LEVEL)	\
		logbuffer_log(LOG, FMT __VA_OPT__(,) __VA_ARGS__); \
} while (0)

#define OVP_OP_RETRY	3

enum ovp_operation {
	OVP_RESET,
	OVP_ON,
	OVP_OFF
};

static struct logbuffer *tcpm_log;

static bool modparam_conf_sbu;
module_param_named(conf_sbu, modparam_conf_sbu, bool, 0644);
MODULE_PARM_DESC(conf_sbu, "Configure sbu pins");

static char boot_mode_string[64];
module_param_string(mode, boot_mode_string, sizeof(boot_mode_string), 0440);
MODULE_PARM_DESC(mode, "Android bootmode");

static u32 partner_src_caps[PDO_MAX_OBJECTS];
static unsigned int nr_partner_src_caps;
static bool port_src_pdo_updated;
static bool limit_src_cap_enable;
static u32 orig_src_current;
static unsigned int nr_orig_src_pdo;
spinlock_t g_caps_lock;

static unsigned int sink_discovery_delay_ms;

/* Callback for data_active changes */
void (*data_active_callback)(void *data_active_payload);
void *data_active_payload;

static bool hooks_installed;

struct tcpci {
	struct device *dev;
	struct tcpm_port *port;
	struct regmap *regmap;
	bool controls_vbus;

	struct tcpc_dev tcpc;
	struct tcpci_data *data;
};

struct dp_notification_event {
	struct max77759_plat *chip;
	unsigned long mode;
	struct kthread_work dp_notification_work;
};

static const struct regmap_range max77759_tcpci_range[] = {
	regmap_reg_range(0x00, REGMAP_REG_MAX_ADDR)
};

const struct regmap_access_table max77759_tcpci_write_table = {
	.yes_ranges = max77759_tcpci_range,
	.n_yes_ranges = ARRAY_SIZE(max77759_tcpci_range),
};

static const struct regmap_config max77759_regmap_config = {
	.reg_bits = 8,
	.val_bits = 8,
	.max_register = REGMAP_REG_MAX_ADDR,
	.wr_table = &max77759_tcpci_write_table,
};

static ssize_t frs_show(struct device *dev, struct device_attribute *attr, char *buf)
{
	struct max77759_plat *chip = i2c_get_clientdata(to_i2c_client(dev));

	return scnprintf(buf, PAGE_SIZE, "%d\n", chip->frs);
};
static DEVICE_ATTR_RO(frs);

static ssize_t auto_discharge_show(struct device *dev, struct device_attribute *attr, char *buf)
{
	struct max77759_plat *chip = i2c_get_clientdata(to_i2c_client(dev));

	return scnprintf(buf, PAGE_SIZE, "%d\n", chip->data.auto_discharge_disconnect ? 1 : 0);
};
static DEVICE_ATTR_RO(auto_discharge);

static ssize_t bc12_enabled_show(struct device *dev, struct device_attribute *attr, char *buf)
{
	struct max77759_plat *chip = i2c_get_clientdata(to_i2c_client(dev));

	return scnprintf(buf, PAGE_SIZE, "%d\n", bc12_get_status(chip->bc12) ? 1 : 0);
};
static DEVICE_ATTR_RO(bc12_enabled);

/* Debugfs disabled in user builds. */
static ssize_t registers_show(struct device *dev, struct device_attribute *attr, char *buf)
{
	struct max77759_plat *chip = i2c_get_clientdata(to_i2c_client(dev));
	struct regmap *regmap = chip->data.regmap;
	u8 dump[REGMAP_REG_COUNT];
	int ret, offset = 0, addr;

	ret = regmap_bulk_read(regmap, 0, dump, REGMAP_REG_COUNT);
	if (ret < 0) {
		dev_err(chip->dev, "[%s]: Failed to dump ret:%d\n", __func__, ret);
		return 0;
	}

	for (addr = 0; addr < REGMAP_REG_COUNT; addr++) {
		ret = sysfs_emit_at(buf, offset, "%x: %x\n", addr, dump[addr]);
		if (!ret) {
			dev_err(chip->dev, "[%s]: Not all registers printed. last:%x\n", __func__,
				addr - 1);
			break;
		}
		offset += ret;
	}

	return offset;
};
static DEVICE_ATTR_RO(registers);

static ssize_t contaminant_detection_show(struct device *dev, struct device_attribute *attr,
					  char *buf)
{
	struct max77759_plat *chip = i2c_get_clientdata(to_i2c_client(dev));

	return scnprintf(buf, PAGE_SIZE, "%d\n", chip->contaminant_detection_userspace);
};

static void update_contaminant_detection_locked(struct max77759_plat *chip, int val)
{

	chip->contaminant_detection = val;

	if (chip->contaminant_detection)
		enable_contaminant_detection(chip, chip->contaminant_detection ==
					     CONTAMINANT_DETECT_MAXQ);
	else
		disable_contaminant_detection(chip);

	LOG(LOG_LVL_DEBUG, chip->log, "[%s]: %d", __func__, chip->contaminant_detection);
}

static ssize_t contaminant_detection_store(struct device *dev, struct device_attribute *attr,
					   const char *buf, size_t count)
{
	struct max77759_plat *chip = i2c_get_clientdata(to_i2c_client(dev));
	int val;

	if (kstrtoint(buf, 10, &val) < 0)
		return -EINVAL;

	mutex_lock(&chip->rc_lock);
	chip->contaminant_detection_userspace = val;
	update_contaminant_detection_locked(chip, val);
	mutex_unlock(&chip->rc_lock);
	return count;
}
static DEVICE_ATTR_RW(contaminant_detection);

static ssize_t cc_toggle_enable_show(struct device *dev, struct device_attribute *attr,
				     char *buf)
{
	struct max77759_plat *chip = i2c_get_clientdata(to_i2c_client(dev));

	return scnprintf(buf, PAGE_SIZE, "%d\n", chip->toggle_disable_status ? 0 : 1);
};

static ssize_t cc_toggle_enable_store(struct device *dev, struct device_attribute *attr,
				      const char *buf, size_t count)
{
	struct max77759_plat *chip = i2c_get_clientdata(to_i2c_client(dev));
	int val, ret;

	if (kstrtoint(buf, 10, &val) < 0)
		return -EINVAL;

	ret = gvotable_cast_vote(chip->toggle_disable_votable, "USER_VOTE",
				 (void *)MAX77759_DISABLE_TOGGLE_VOTE, val ?
				 MAX77759_ENABLE_TOGGLE : MAX77759_DISABLE_TOGGLE);
	if (ret < 0)
		dev_err(chip->dev, "Cannot set TOGGLE DISABLE=%d (%d)\n", val, ret);

	return count;
}
static DEVICE_ATTR_RW(cc_toggle_enable);

static ssize_t non_compliant_reasons_show(struct device *dev, struct device_attribute *attr,
					  char *buf)
{
	struct max77759_plat *chip = i2c_get_clientdata(to_i2c_client(dev));

	return compliance_warnings_to_buffer(chip->compliance_warnings, buf);
};

static DEVICE_ATTR_RO(non_compliant_reasons);

static ssize_t contaminant_detection_status_show(struct device *dev, struct device_attribute *attr,
						 char *buf)
{
	struct max77759_plat *chip = i2c_get_clientdata(to_i2c_client(dev));
	struct max77759_contaminant *contaminant;

	if (!chip)
		return -EAGAIN;

	contaminant = chip->contaminant;

	if (!contaminant)
		return -EAGAIN;

	return scnprintf(buf, PAGE_SIZE, "%d\n", is_contaminant_detected(chip));
}
static DEVICE_ATTR_RO(contaminant_detection_status);

static ssize_t usb_limit_sink_enable_show(struct device *dev, struct device_attribute *attr,
					  char *buf)
{
	struct max77759_plat *chip = i2c_get_clientdata(to_i2c_client(dev));

	return sysfs_emit(buf, "%u\n", chip->limit_sink_enable);
};

/* usb_limit_sink_current has to be set before usb_limit_sink_enable is invoked */
static ssize_t usb_limit_sink_enable_store(struct device *dev, struct device_attribute *attr,
					   const char *buf, size_t count)
{
	struct max77759_plat *chip = i2c_get_clientdata(to_i2c_client(dev));
	bool enable;
	int ret;

	if (kstrtobool(buf, &enable) < 0)
		return -EINVAL;

	if (enable) {
		ret = gvotable_cast_vote(chip->usb_icl_el, LIMIT_SINK_VOTER,
					 (void *)(long)chip->limit_sink_current, true);
		if (ret < 0) {
			dev_err(chip->dev, "Cannot set sink current %d uA (%d)\n",
				chip->limit_sink_current, ret);
			goto exit;
		}
	} else {
		ret = gvotable_cast_vote(chip->usb_icl_el, LIMIT_SINK_VOTER, 0, false);
		if (ret < 0) {
			dev_err(chip->dev, "Cannot unvote for sink current (%d)\n", ret);
			goto exit;
		}
	}

	chip->limit_sink_enable = enable;

exit:
	return count;
}
static DEVICE_ATTR_RW(usb_limit_sink_enable);

static ssize_t usb_limit_sink_current_show(struct device *dev, struct device_attribute *attr,
					   char *buf)
{
	struct max77759_plat *chip = i2c_get_clientdata(to_i2c_client(dev));

	return sysfs_emit(buf, "%u\n", chip->limit_sink_current);
};

/* limit_sink_current will not be updated if limit_sink_enable is already enabled */
static ssize_t usb_limit_sink_current_store(struct device *dev, struct device_attribute *attr,
					    const char *buf, size_t count)
{
	struct max77759_plat *chip = i2c_get_clientdata(to_i2c_client(dev));
	unsigned int val;

	if (kstrtouint(buf, 0, &val) < 0)
		return -EINVAL;

	/* Never accept current over 3A */
	if (val > 3000000)
		return -EINVAL;

	chip->limit_sink_current = val;

	return count;
}
static DEVICE_ATTR_RW(usb_limit_sink_current);

static ssize_t usb_limit_accessory_enable_show(struct device *dev, struct device_attribute *attr,
					  char *buf)
{
	struct max77759_plat *chip = i2c_get_clientdata(to_i2c_client(dev));

	return sysfs_emit(buf, "%u\n", chip->limit_accessory_enable);
};

/* usb_limit_accessory_current has to be set before usb_limit_accessory_enable is invoked */
static ssize_t usb_limit_accessory_enable_store(struct device *dev, struct device_attribute *attr,
						const char *buf, size_t count)
{
	struct max77759_plat *chip = i2c_get_clientdata(to_i2c_client(dev));
	bool enable;
	int ret;

	if (kstrtobool(buf, &enable) < 0)
		return -EINVAL;

	if (enable) {
		ret = gvotable_cast_vote(chip->usb_icl_el, LIMIT_ACCESSORY_VOTER,
					 (void *)(long)chip->limit_accessory_current, true);
		if (ret < 0) {
			dev_err(chip->dev, "Cannot set accessory current %d uA (%d)\n",
				chip->limit_accessory_current, ret);
			goto exit;
		}
	} else {
		ret = gvotable_cast_vote(chip->usb_icl_el, LIMIT_ACCESSORY_VOTER, 0, false);
		if (ret < 0) {
			dev_err(chip->dev, "Cannot unvote for accessory current (%d)\n", ret);
			goto exit;
		}
	}

	chip->limit_accessory_enable = enable;

exit:
	return count;
}
static DEVICE_ATTR_RW(usb_limit_accessory_enable);

static ssize_t usb_limit_accessory_current_show(struct device *dev, struct device_attribute *attr,
						char *buf)
{
	struct max77759_plat *chip = i2c_get_clientdata(to_i2c_client(dev));

	return sysfs_emit(buf, "%u\n", chip->limit_accessory_current);
};

static ssize_t usb_limit_accessory_current_store(struct device *dev, struct device_attribute *attr,
						 const char *buf, size_t count)
{
	struct max77759_plat *chip = i2c_get_clientdata(to_i2c_client(dev));
	unsigned int val;

	if (kstrtouint(buf, 0, &val) < 0)
		return -EINVAL;

	/* Never accept current over 3A */
	if (val > 3000000)
		return -EINVAL;

	chip->limit_accessory_current = val;

	return count;
}
static DEVICE_ATTR_RW(usb_limit_accessory_current);

static ssize_t sbu_pullup_show(struct device *dev, struct device_attribute *attr, char *buf)
{
	struct max77759_plat *chip = i2c_get_clientdata(to_i2c_client(dev));

	return sysfs_emit(buf, "%u\n", chip->current_sbu_state);
}

static ssize_t sbu_pullup_store(struct device *dev, struct device_attribute *attr, const char *buf,
				size_t count)
{
	struct max77759_plat *chip = i2c_get_clientdata(to_i2c_client(dev));
	int val, ret = 0;
	bool enable = false;

	if (kstrtoint(buf, 0, &val) < 0)
		return -EINVAL;

	switch (val) {
	case 0:
		if (chip->sbu_mux_en_gpio >= 0)
			gpio_set_value_cansleep(chip->sbu_mux_en_gpio, 0);
		gpio_set_value_cansleep(chip->sbu_mux_sel_gpio, 0);
		enable = false;
		break;
	case 1:
		if (chip->sbu_mux_en_gpio >= 0)
			gpio_set_value_cansleep(chip->sbu_mux_en_gpio, 0);
		gpio_set_value_cansleep(chip->sbu_mux_sel_gpio, 1);
		enable = false;
		break;
	case 2:
		if (chip->sbu_mux_en_gpio >= 0)
			gpio_set_value_cansleep(chip->sbu_mux_en_gpio, 1);
		gpio_set_value_cansleep(chip->sbu_mux_sel_gpio, 0);
		enable = true;
		break;
	case 3:
		if (chip->sbu_mux_en_gpio >= 0)
			gpio_set_value_cansleep(chip->sbu_mux_en_gpio, 1);
		gpio_set_value_cansleep(chip->sbu_mux_sel_gpio, 1);
		enable = true;
		break;
	default:
		goto set_sbu_state;
	}

	if ((enable && !chip->dp_regulator_enabled) || (!enable && chip->dp_regulator_enabled)) {
		ret = enable ? regulator_enable(chip->dp_regulator) : \
			regulator_disable(chip->dp_regulator);
		if (ret >= 0)
			chip->dp_regulator_enabled = enable;
		dev_info(chip->dev, "dp regulator_%s %s ret:%d", enable ? "enable" : "disable",
				ret < 0 ? "fail" : "success", ret);
		ret = enable ? regulator_set_voltage(chip->dp_regulator, VOLTAGE_DP_AUX_DEFAULT_UV,
							VOLTAGE_DP_AUX_DEFAULT_UV) : \
				regulator_set_voltage(chip->dp_regulator, chip->dp_regulator_min_uv,
							chip->dp_regulator_max_uv);
		dev_info(chip->dev, "dp regulator_set_voltage %s ret:%d",
				ret < 0 ? "fail" : "success", ret);
	}

	ret = max77759_write8(chip->data.regmap, TCPC_VENDOR_SBUSW_CTRL, enable ? SBUSW_PATH_1 :
			      (modparam_conf_sbu ? SBUSW_SERIAL_UART : 0));
	logbuffer_logk(chip->log, LOGLEVEL_INFO, "SBU dp switch %s %s ret:%d",
		       enable ? "enable" : "disable", ret < 0 ? "fail" : "success", ret);

set_sbu_state:
	dev_info(chip->dev, "dp_debug: sbu_pullup_store: val:%d \n", val);
	if (!ret)
		chip->current_sbu_state = val;

	return count;
}
static DEVICE_ATTR_RW(sbu_pullup);

static ssize_t irq_hpd_count_show(struct device *dev, struct device_attribute *attr, char *buf)
{
	struct max77759_plat *chip = i2c_get_clientdata(to_i2c_client(dev));

	return sysfs_emit(buf, "%u\n", chip->irq_hpd_count);
};
static DEVICE_ATTR_RO(irq_hpd_count);

static ssize_t usb_limit_source_enable_show(struct device *dev, struct device_attribute *attr,
					    char *buf)
{
	return sysfs_emit(buf, "%u\n", limit_src_cap_enable);
}

static ssize_t usb_limit_source_enable_store(struct device *dev, struct device_attribute *attr,
					     const char *buf, size_t count)
{
	struct max77759_plat *chip = i2c_get_clientdata(to_i2c_client(dev));
	bool enable;

	if (kstrtobool(buf, &enable) < 0)
		return -EINVAL;

	spin_lock(&g_caps_lock);
	port_src_pdo_updated = false;
	limit_src_cap_enable = enable;
	spin_unlock(&g_caps_lock);

	tcpm_cc_change(chip->tcpci->port);

	return count;
}
static DEVICE_ATTR_RW(usb_limit_source_enable);

static struct device_attribute *max77759_device_attrs[] = {
	&dev_attr_frs,
	&dev_attr_bc12_enabled,
	&dev_attr_registers,
	&dev_attr_auto_discharge,
	&dev_attr_contaminant_detection,
	&dev_attr_contaminant_detection_status,
	&dev_attr_cc_toggle_enable,
	&dev_attr_non_compliant_reasons,
	&dev_attr_usb_limit_sink_enable,
	&dev_attr_usb_limit_sink_current,
	&dev_attr_usb_limit_accessory_enable,
	&dev_attr_usb_limit_accessory_current,
	&dev_attr_sbu_pullup,
	&dev_attr_usb_limit_source_enable,
	&dev_attr_irq_hpd_count,
	NULL
};

void register_data_active_callback(void (*callback)(void *data_active_payload), void *data)
{
	data_active_callback = callback;
	data_active_payload = data;
}
EXPORT_SYMBOL_GPL(register_data_active_callback);

static void ovp_operation(struct max77759_plat *chip, int operation);
#ifdef CONFIG_GPIOLIB
static int ext_bst_en_gpio_get_direction(struct gpio_chip *chip, unsigned int offset)
{
	return GPIOF_DIR_OUT;
}

static int ext_bst_en_gpio_get(struct gpio_chip *gpio, unsigned int offset)
{
	int ret;
	u8 val;
	struct max77759_plat *chip = gpiochip_get_data(gpio);
	struct regmap *regmap = chip->data.regmap;

	ret = max77759_read8(regmap, TCPC_VENDOR_EXTBST_CTRL, &val);
	LOG(LOG_LVL_DEBUG, chip->log, "%s: ret:%d", __func__, ret);

	return val & EXT_BST_EN;
}

static void ext_bst_en_gpio_set(struct gpio_chip *gpio, unsigned int offset, int value)
{
	struct max77759_plat *chip = gpiochip_get_data(gpio);
	struct regmap *regmap = chip->data.regmap;
	bool vsafe0v, toggle_ovp = false;
	int ret;
	u8 raw;

	ret = max77759_read8(regmap, TCPC_EXTENDED_STATUS, &raw);
	if (ret < 0)
		vsafe0v = chip->vsafe0v;
	else
		vsafe0v = !!(raw & TCPC_EXTENDED_STATUS_VSAFE0V);

	/* b/309900468 toggle ovp to make sure that Vbus is vSafe0V when setting EXT_BST_EN. */
	if (chip->in_switch_gpio >= 0 && value && !vsafe0v)
		toggle_ovp = true;

	if (toggle_ovp)
		ovp_operation(chip, OVP_OFF);

	ret = max77759_write8(regmap, TCPC_VENDOR_EXTBST_CTRL, value ? EXT_BST_EN : 0);
	LOG(LOG_LVL_DEBUG, chip->log, "%s: TCPC_VENDOR_EXTBST_CTRL value:%d ret:%d", __func__,
	    value, ret);

	if (toggle_ovp) {
		mdelay(10);

		ovp_operation(chip, OVP_ON);
	}
}

static int ext_bst_en_gpio_init(struct max77759_plat *chip)
{
	int ret;

	/* Setup GPIO controller */
	chip->gpio.owner = THIS_MODULE;
	chip->gpio.parent = chip->dev;
	chip->gpio.label = "max77759_tcpc_gpio";
	chip->gpio.get_direction = ext_bst_en_gpio_get_direction;
	chip->gpio.get = ext_bst_en_gpio_get;
	chip->gpio.set = ext_bst_en_gpio_set;
	chip->gpio.base = -1;
	chip->gpio.ngpio = 1;
	chip->gpio.can_sleep = true;
	chip->gpio.of_node = of_find_node_by_name(chip->dev->of_node, chip->gpio.label);

	if (!chip->gpio.of_node)
		dev_err(chip->dev, "Failed to find %s DT node\n", chip->gpio.label);

	ret = devm_gpiochip_add_data(chip->dev, &chip->gpio, chip);
	if (ret)
		dev_err(chip->dev, "Failed to initialize gpio chip\n");

	return ret;
}
#endif

static struct max77759_plat *tdata_to_max77759(struct tcpci_data *tdata)
{
	return container_of(tdata, struct max77759_plat, data);
}

static void max77759_init_regs(struct regmap *regmap, struct logbuffer *log)
{
	u16 alert_mask = 0;
	int ret;

	ret = max77759_write16(regmap, TCPC_ALERT, 0xffff);
	if (ret < 0)
		return;

	ret = max77759_write16(regmap, TCPC_VENDOR_ALERT, 0xffff);
	if (ret < 0)
		return;

	ret = regmap_write(regmap, TCPC_EXTENDED_STATUS_MASK,
			   TCPC_EXTENDED_STATUS_VSAFE0V);
	if (ret < 0) {
		LOG(LOG_LVL_DEBUG, log, "Error writing TCPC_EXTENDED_STATUS_MASK ret:%d", ret);
		return;
	}

	LOG(LOG_LVL_DEBUG, log, "[%s] Init EXTENDED_STATUS_MASK: VSAFE0V", __func__);

	ret = max77759_write8(regmap, TCPC_ALERT_EXTENDED, 0xff);
	if (ret < 0) {
		LOG(LOG_LVL_DEBUG, log, "Unable to clear TCPC_ALERT_EXTENDED ret:%d\n", ret);
		return;
	}

	alert_mask = TCPC_ALERT_TX_SUCCESS | TCPC_ALERT_TX_DISCARDED |
		TCPC_ALERT_TX_FAILED | TCPC_ALERT_RX_HARD_RST |
		TCPC_ALERT_RX_STATUS | TCPC_ALERT_VENDOR | TCPC_ALERT_CC_STATUS |
		TCPC_ALERT_VBUS_DISCNCT | TCPC_ALERT_RX_BUF_OVF |
		TCPC_ALERT_EXTENDED_STATUS | TCPC_ALERT_POWER_STATUS |
		/* Enable Extended alert for detecting Fast Role Swap Signal */
		TCPC_ALERT_EXTND;

	ret = max77759_write16(regmap, TCPC_ALERT_MASK, alert_mask);
	if (ret < 0)
		return;
	LOG(LOG_LVL_DEBUG, log, "[%s] Init ALERT_MASK: %u", __func__, alert_mask);

	max77759_read16(regmap, TCPC_ALERT_MASK, &alert_mask);
	LOG(LOG_LVL_DEBUG, log, "[%s] Init ALERT_MASK read : %u", __func__, alert_mask);

	/* Enable vbus voltage monitoring, voltage alerts, bleed discharge */
	ret = max77759_update_bits8(regmap, TCPC_POWER_CTRL, TCPC_POWER_CTRL_VBUS_VOLT_MON |
				    TCPC_DIS_VOLT_ALRM | TCPC_POWER_CTRL_BLEED_DISCHARGE,
				    TCPC_POWER_CTRL_BLEED_DISCHARGE);
	if (ret < 0)
		return;
	LOG(LOG_LVL_DEBUG, log,
	    "TCPC_POWER_CTRL: Enable voltage monitoring, alarm, bleed discharge");

	ret = max77759_write8(regmap, TCPC_ALERT_EXTENDED_MASK, TCPC_SINK_FAST_ROLE_SWAP);
	if (ret < 0) {
		LOG(LOG_LVL_DEBUG, log, "Unable to unmask FAST_ROLE_SWAP interrupt");
		return;
	}

	ret = max77759_update_bits8(regmap, TCPC_VENDOR_VCON_CTRL, VCNILIM_MASK, VCNILIM_300_MA);
	if (ret < 0)
		LOG(LOG_LVL_DEBUG, log, "TCPC_VENDOR_VCON_CTRL: update vcnilim to 300mA failed");
}

static int post_process_pd_message(struct max77759_plat *chip, struct pd_message msg)
{
	enum pd_ctrl_msg_type pd_type = pd_header_type_le(msg.header);

	if (pd_type == PD_DATA_VENDOR_DEF) {
		u32 payload[2];
		int i;

		for (i = 0; i < 2; i++) {
			payload[i] = le32_to_cpu(msg.payload[i]);
			if ((PD_VDO_VID(payload[0]) == USB_TYPEC_DP_SID))
				LOG(LOG_LVL_DEBUG, chip->log, "DP VDO[%d] 0x%x", i, payload[i]);
		}

		if (PD_VDO_SVDM(payload[0]) && (PD_VDO_VID(payload[0]) == USB_TYPEC_DP_SID) &&
		    ((PD_VDO_CMD(payload[0]) == CMD_ATTENTION) ||
		    (PD_VDO_CMD(payload[0]) == DP_CMD_STATUS_UPDATE)) &&
		    (payload[1] & DP_STATUS_IRQ_HPD)) {
			chip->irq_hpd_count++;
			LOG(LOG_LVL_DEBUG, chip->log, "DP IRQ_HPD:%d count:%u",
			    (payload[1] & DP_STATUS_IRQ_HPD), chip->irq_hpd_count);
			// sysfs_notify(&chip->dev->kobj, NULL, "irq_hpd_count");
			kobject_uevent(&chip->dev->kobj, KOBJ_CHANGE);
		}
	}

	return 0;
}

static int process_rx(struct max77759_plat *chip, u16 status)
{
	struct pd_message msg;
	u8 count, frame_type, rx_buf[TCPC_RECEIVE_BUFFER_LEN];
	int ret, payload_index;
	u8 *rx_buf_ptr;
	enum pd_ctrl_msg_type pd_type;

	/*
	 * READABLE_BYTE_COUNT: Indicates the number of bytes in the RX_BUF_BYTE_x registers
	 * plus one (for the RX_BUF_FRAME_TYPE) Table 4-36.
	 * Read the count and frame type.
	 */
	LOG(LOG_LVL_INFO, chip->log, "%d", __LINE__);
	ret = regmap_raw_read(chip->data.regmap, TCPC_RX_BYTE_CNT, rx_buf, 2);
	LOG(LOG_LVL_INFO, chip->log, "%d", __LINE__);
	if (ret < 0) {
		dev_err(chip->dev, "TCPC_RX_BYTE_CNT read failed ret:%d", ret);
		return -EIO;
	}

	count = rx_buf[TCPC_RECEIVE_BUFFER_COUNT_OFFSET];
	frame_type = rx_buf[TCPC_RECEIVE_BUFFER_FRAME_TYPE_OFFSET];

	if (count == 0 || frame_type != TCPC_RX_BUF_FRAME_TYPE_SOP) {
		ret = max77759_write16(chip->data.regmap, TCPC_ALERT, TCPC_ALERT_RX_STATUS);
		dev_err(chip->dev, "%s", count ==  0 ? "error: count is 0" :
			"error frame_type is not SOP");
		if (ret < 0)
			return -EIO;
	}

	/*
	 * 1. struct pd_message does not have RX_BUF_FRAME_TYPE.
	 * 2. READABLE_BYTE_COUNT is exclusive of itself.
	 */
	if (count > sizeof(struct pd_message) + 1 || count + 1 > TCPC_RECEIVE_BUFFER_LEN) {
		dev_err(chip->dev, "Invalid TCPC_RX_BYTE_CNT %d", count);
		return 0;
	}

	/*
	 * Read count + 1 as RX_BUF_BYTE_x is hidden and can only be read through
	 * TCPC_RX_BYTE_CNT
	 */
	count += 1;
	ret = regmap_raw_read(chip->data.regmap, TCPC_RX_BYTE_CNT, rx_buf, count);
	LOG(LOG_LVL_INFO, chip->log, "%d", __LINE__);
	if (ret < 0) {
		dev_err(chip->dev, "Error: TCPC_RX_BYTE_CNT read failed: %d", ret);
		return -EIO;
	}

	rx_buf_ptr = rx_buf + TCPC_RECEIVE_BUFFER_RX_BYTE_BUF_OFFSET;
	msg.header = cpu_to_le16(*(u16 *)rx_buf_ptr);
	rx_buf_ptr = rx_buf_ptr + sizeof(msg.header);
	for (payload_index = 0; payload_index < pd_header_cnt_le(msg.header); payload_index++,
	     rx_buf_ptr += sizeof(msg.payload[0]))
		msg.payload[payload_index] = cpu_to_le32(*(u32 *)rx_buf_ptr);

	LOG(LOG_LVL_INFO, chip->log, "%d", __LINE__);

	/*
	 * Read complete, clear RX status alert bit.
	 * Clear overflow as well if set.
	 */
	ret = max77759_write16(chip->data.regmap, TCPC_ALERT, status & TCPC_ALERT_RX_BUF_OVF ?
			       TCPC_ALERT_RX_STATUS | TCPC_ALERT_RX_BUF_OVF :
			       TCPC_ALERT_RX_STATUS);
	if (ret < 0)
		return -EIO;

	LOG(LOG_LVL_DEBUG, chip->log, "rx clear");
	pd_type = pd_header_type_le(msg.header);
	if (pd_type == PD_CTRL_PR_SWAP) {
		LOG(LOG_LVL_DEBUG, chip->log, "PD_CTRL_PR_SWAP");
		/* To prevent disconnect during PR_SWAP. */
		ret = max77759_write16(chip->data.regmap, TCPC_VBUS_SINK_DISCONNECT_THRESH, 0);
		/* TODO: tcpci->pr_swap = true; */
		if (ret < 0)
			return -EIO;
	}

	tcpm_pd_receive(chip->port, &msg);

	ret = post_process_pd_message(chip, msg);
	if (ret < 0)
		return ret;

	return 0;
}

struct max77759_compliance_warnings *init_compliance_warnings(struct max77759_plat *chip)
{
	struct max77759_compliance_warnings *compliance_warnings;

	compliance_warnings = devm_kzalloc(chip->dev, sizeof(*compliance_warnings), GFP_KERNEL);
	if (!compliance_warnings)
		return ERR_PTR(-ENOMEM);

	compliance_warnings->chip = chip;

	return compliance_warnings;
}

ssize_t compliance_warnings_to_buffer(struct max77759_compliance_warnings *compliance_warnings,
				      char *buf)
{
	memset(buf, 0, PAGE_SIZE);
	strncat(buf, "[", strlen("["));
	if (compliance_warnings->other)
		strncat(buf, "other, ", strlen("other, "));
	if (compliance_warnings->debug_accessory)
		strncat(buf, "debug-accessory, ", strlen("debug-accessory, "));
	if (compliance_warnings->bc12)
		strncat(buf, "bc12, ", strlen("bc12, "));
	if (compliance_warnings->missing_rp)
		strncat(buf, "missing_rp, ", strlen("missing_rp, "));
	if (compliance_warnings->input_power_limited)
		strncat(buf, "input_power_limited, ", strlen("input_power_limited, "));
	strncat(buf, "]", strlen("]"));
	return strnlen(buf, PAGE_SIZE);
}

void update_compliance_warnings(struct max77759_plat *chip, int warning, bool value)
{
	bool compliance_warnings_changed = false;

	switch (warning) {
	case COMPLIANCE_WARNING_OTHER:
		compliance_warnings_changed = (chip->compliance_warnings->other != value);
		chip->compliance_warnings->other = value;
		break;
	case COMPLIANCE_WARNING_DEBUG_ACCESSORY:
		compliance_warnings_changed = (chip->compliance_warnings->debug_accessory != value);
		chip->compliance_warnings->debug_accessory = value;
		break;
	case COMPLIANCE_WARNING_BC12:
		compliance_warnings_changed = (chip->compliance_warnings->bc12 != value);
		chip->compliance_warnings->bc12 = value;
		break;
	case COMPLIANCE_WARNING_MISSING_RP:
		compliance_warnings_changed = (chip->compliance_warnings->missing_rp != value);
		chip->compliance_warnings->missing_rp = value;
		break;
	case COMPLIANCE_WARNING_INPUT_POWER_LIMITED:
		compliance_warnings_changed =
				(chip->compliance_warnings->input_power_limited != value);
		chip->compliance_warnings->input_power_limited = value;
		break;
	}

	if (compliance_warnings_changed) {
		kobject_uevent(&chip->dev->kobj, KOBJ_CHANGE);
		LOG(LOG_LVL_DEBUG, chip->log,
		    "compliance warning %d changed, new value: %d", warning, value);
	}
}

static void max77759_non_compliant_bc12_callback(void *data, bool status)
{
	struct max77759_plat *chip = data;

	/* Exclude Rp-1.5 or higher power sources */
	if ((status && !(chip->cc1 == TYPEC_CC_RP_3_0 || chip->cc1 == TYPEC_CC_RP_1_5 ||
			chip->cc2 == TYPEC_CC_RP_3_0 || chip->cc2 == TYPEC_CC_RP_1_5)) || !status)
		update_compliance_warnings(chip, COMPLIANCE_WARNING_BC12, status);
}

static void enable_dp_pulse(struct max77759_plat *chip)
{
	struct regmap *regmap = chip->data.regmap;
	int ret;

	ret = max77759_update_bits8(regmap, VENDOR_BC_CTRL2, DPDNMAN | DPDRV,
				    DPDNMAN | DPDRV_3V0 << DPDRV_SHIFT);
	if (ret < 0)
		LOG(LOG_LVL_DEBUG, chip->log, "%s failed to set dpDnMan and dpDrv", __func__);

	mdelay(100);

	ret = max77759_update_bits8(regmap, VENDOR_BC_CTRL2, DPDNMAN | DPDRV,
				    DPDRV_OPEN << DPDRV_SHIFT);
	if (ret < 0)
		LOG(LOG_LVL_DEBUG, chip->log, "%s failed to disable dpDnMan and dpDrv", __func__);
}

void enable_data_path_locked(struct max77759_plat *chip)
{
	int ret;
	bool enable_data = false;
	struct regmap *regmap = chip->data.regmap;

	if (chip->force_device_mode_on) {
		LOG(LOG_LVL_DEBUG, chip->log,
		    "%s skipping as force_device_mode_on is set", __func__);
		return;
	}

	if (chip->alt_path_active) {
		LOG(LOG_LVL_DEBUG, chip->log, "%s skipping as alt path is active", __func__);
		return;
	}

	logbuffer_logk(chip->log, LOGLEVEL_INFO,
		       "%s pd_data_capable:%u no_bc_12:%u bc12_data_capable:%u attached:%u debug_acc_conn:%u bc12_running:%u",
		       __func__, chip->pd_data_capable ? 1 : 0, chip->no_bc_12 ? 1 : 0,
		       chip->bc12_data_capable ? 1 : 0, chip->attached ? 1 : 0,
		       chip->debug_acc_connected, chip->bc12_running ? 1 : 0);

	enable_data = ((chip->pd_data_capable || chip->no_bc_12 || chip->bc12_data_capable ||
		       chip->debug_acc_connected) && !chip->bc12_running) ||
		       chip->data_role == TYPEC_HOST;

	if (chip->attached && enable_data && !chip->data_active) {
		/* Disable BC1.2 to prevent BC1.2 detection during PR_SWAP */
		bc12_enable(chip->bc12, false);
		/*
		 * Clear running flag here as PD might have configured data
		 * before BC12 started to run.
		 */
		chip->bc12_running = false;

		/*
		 * b/188614064: While swapping from host to device switches will not be configured
		 * by HW. So always enable the switches here.
		 */
		ret = max77759_write8(regmap, TCPC_VENDOR_USBSW_CTRL, USBSW_CONNECT);
		LOG(LOG_LVL_DEBUG, chip->log,
		    "Turning on dp switches %s", ret < 0 ? "fail" : "success");

		if (get_usb_type(chip->bc12) == POWER_SUPPLY_USB_TYPE_CDP &&
		    !chip->pd_data_capable) {
			LOG(LOG_LVL_DEBUG, chip->log, "CDP detected, gen dp pulse");
				enable_dp_pulse(chip);
		}

		ret = extcon_set_state_sync(chip->extcon, chip->data_role == TYPEC_HOST ?
					    EXTCON_USB_HOST : EXTCON_USB, 1);
		logbuffer_logk(chip->log, LOGLEVEL_INFO, "%s turning on %s",
			       ret < 0 ? "Failed" : "Succeeded",
			       chip->data_role == TYPEC_HOST ? "Host" : "Device");
		chip->data_active = true;
		if (data_active_callback)
			(*data_active_callback)(data_active_payload);
		chip->active_data_role = chip->data_role;
	} else if (chip->data_active && (!chip->attached || !enable_data)) {
		ret = extcon_set_state_sync(chip->extcon, chip->active_data_role == TYPEC_HOST ?
					    EXTCON_USB_HOST : EXTCON_USB, 0);
		logbuffer_logk(chip->log, LOGLEVEL_INFO, "%s turning off %s",
			       ret < 0 ? "Failed" : "Succeeded",
			       chip->active_data_role == TYPEC_HOST ? "Host" : "Device");
		chip->data_active = false;
		if (data_active_callback)
			(*data_active_callback)(data_active_payload);
		if  (chip->active_data_role == TYPEC_HOST) {
			ret = max77759_write8(regmap, TCPC_VENDOR_USBSW_CTRL, USBSW_DISCONNECT);
			LOG(LOG_LVL_DEBUG, chip->log,
			    "Turning off dp switches %s", ret < 0 ? "fail" : "success");
		}
	}
}
EXPORT_SYMBOL_GPL(enable_data_path_locked);

void data_alt_path_active(struct max77759_plat *chip, bool active)
{
	chip->alt_path_active = active;
}
EXPORT_SYMBOL_GPL(data_alt_path_active);

static void enable_vbus_work(struct kthread_work *work)
{
	struct max77759_plat *chip  =
		container_of(container_of(work, struct kthread_delayed_work, work),
			     struct max77759_plat, enable_vbus_work);
	int ret;

	LOG(LOG_LVL_DEBUG, chip->log, "%s", __func__);
	if (IS_ERR_OR_NULL(chip->charger_mode_votable)) {
		chip->charger_mode_votable = gvotable_election_get_handle(GBMS_MODE_VOTABLE);
		if (IS_ERR_OR_NULL(chip->charger_mode_votable)) {
			LOG(LOG_LVL_DEBUG, chip->log,
			    "ERR: GBMS_MODE_VOTABLE lazy get failed with error %ld",
			    PTR_ERR(chip->charger_mode_votable));
			return;
		}
	}

	ret = gvotable_cast_vote(chip->charger_mode_votable, TCPCI_MODE_VOTER,
				 chip->no_external_boost ? (void *)GBMS_USB_OTG_FRS_ON :
				 (void *)GBMS_USB_OTG_ON, true);

	LOG(LOG_LVL_DEBUG, chip->log, "%s: GBMS_MODE_VOTABLE voting source ret:%d",
	    ret < 0 ? "Error" : "Success", ret);

	if (ret < 0)
		return;

	if (!chip->sourcing_vbus)
		chip->sourcing_vbus = 1;
}

static int max77759_set_vbus(struct tcpci *tcpci, struct tcpci_data *tdata, bool source, bool sink)
{
	struct max77759_plat *chip = tdata_to_max77759(tdata);
	int ret;

	if (source && sink) {
		LOG(LOG_LVL_DEBUG, chip->log, "ERR: both source and sink set. Not voting");
		return -EINVAL;
	}

	if (IS_ERR_OR_NULL(chip->charger_mode_votable)) {
		chip->charger_mode_votable = gvotable_election_get_handle(GBMS_MODE_VOTABLE);
		if (IS_ERR_OR_NULL(chip->charger_mode_votable)) {
			LOG(LOG_LVL_DEBUG, chip->log,
			    "ERR: GBMS_MODE_VOTABLE lazy get failed with error %ld",
			    PTR_ERR(chip->charger_mode_votable));
			return 0;
		}
	}

	kthread_flush_work(&chip->enable_vbus_work.work);

	if (source && !sink) {
		kthread_mod_delayed_work(chip->wq, &chip->enable_vbus_work, 0);
		return 0;
	} else if (sink && !source) {
		ret = gvotable_cast_vote(chip->charger_mode_votable, TCPCI_MODE_VOTER,
					 (void *)GBMS_USB_BUCK_ON, true);
	} else {
		/* just one will do */
		ret = gvotable_cast_vote(chip->charger_mode_votable, TCPCI_MODE_VOTER,
					 (void *)GBMS_USB_BUCK_ON, false);
	}

	LOG(LOG_LVL_DEBUG, chip->log, "%s: GBMS_MODE_VOTABLE voting source:%c sink:%c ret:%d",
	    ret < 0 ? "Error" : "Success", source ? 'y' : 'n', sink ? 'y' : 'n', ret);

	if (ret < 0)
		return ret;

	if (!source && chip->sourcing_vbus) {
		chip->sourcing_vbus = 0;
		chip->vbus_present = 0;
		LOG(LOG_LVL_DEBUG, chip->log,
		    "[%s]: vbus_present %d", __func__, chip->vbus_present);
		tcpm_vbus_change(tcpci->port);
	}

	return 0;
}

static void max77759_frs_sourcing_vbus(struct tcpci *tcpci, struct tcpci_data *tdata)
{
	struct max77759_plat *chip = tdata_to_max77759(tdata);
	int ret;

	kthread_flush_work(&chip->enable_vbus_work.work);

	if (IS_ERR_OR_NULL(chip->charger_mode_votable)) {
		chip->charger_mode_votable = gvotable_election_get_handle(GBMS_MODE_VOTABLE);
		if (IS_ERR_OR_NULL(chip->charger_mode_votable)) {
			LOG(LOG_LVL_DEBUG, chip->log,
			    "ERR: GBMS_MODE_VOTABLE lazy get failed with error %ld",
			    PTR_ERR(chip->charger_mode_votable));
			return;
		}
	}

	ret = gvotable_cast_vote(chip->charger_mode_votable, TCPCI_MODE_VOTER,
				 (void *)GBMS_USB_OTG_FRS_ON, true);
	LOG(LOG_LVL_DEBUG, chip->log, "%s: GBMS_MODE_VOTABLE ret:%d", __func__, ret);

	if (!ret)
		chip->sourcing_vbus = 1;

	/*
	 * TODO: move this line to max77759_set_vbus after the change in TCPM gets upstreamed and
	 * cherry-picked to Pixel codebase.
	 * Be sure to ensure that this will only be called during FR_SWAP.
	 */
	usb_psy_set_sink_state(chip->usb_psy_data, false);
}

static void vsafe0v_debounce_work(struct kthread_work *work)
{
	struct max77759_plat *chip  =
		container_of(container_of(work, struct kthread_delayed_work, work),
			     struct max77759_plat, vsafe0v_work);
	struct tcpci *tcpci = chip->tcpci;

	/* update to TCPM only if it is still Vsafe0V */
	if (!chip->vsafe0v)
		return;

	chip->vbus_present = 0;
	LOG(LOG_LVL_DEBUG, chip->log, "[%s]: vsafe0v debounced, vbus_present 0", __func__);
	tcpm_vbus_change(tcpci->port);
}

void disconnect_missing_rp_partner(struct max77759_plat *chip)
{
	union power_supply_propval val;
	int ret;

	LOG(LOG_LVL_DEBUG, chip->log, "Disconnect missing Rp partner");
	val.intval = POWER_SUPPLY_USB_TYPE_UNKNOWN;
	max77759_set_vbus(chip->tcpci, chip->tcpci->data, false, false);
	update_compliance_warnings(chip, COMPLIANCE_WARNING_MISSING_RP, false);
	/*
	 * clear AICL warning for missing rp as detach will not be signalled for
	 * MISSING_RP + INPUT_POWER_LIMITED(AICL)
	 */
	update_compliance_warnings(chip, COMPLIANCE_WARNING_INPUT_POWER_LIMITED, false);
	chip->vbus_mv = 0;
	/* val.intval does not matter */
	ret = power_supply_set_property(chip->usb_psy, POWER_SUPPLY_PROP_VOLTAGE_MAX, &val);
	if (ret < 0)
		LOG(LOG_LVL_DEBUG, chip->log,
		    "unable to set max voltage to %d, ret=%d", chip->vbus_mv, ret);
	if (power_supply_set_property(chip->usb_psy, POWER_SUPPLY_PROP_USB_TYPE, &val))
		LOG(LOG_LVL_DEBUG, chip->log, "missing_rp: usb_psy set unknown failed");
	usb_psy_set_sink_state(chip->usb_psy_data, false);
}

static void check_missing_rp_work(struct kthread_work *work)
{
	struct max77759_plat *chip  =
		container_of(container_of(work, struct kthread_delayed_work, work),
			     struct max77759_plat, check_missing_rp_work);
	union power_supply_propval val;
	unsigned int pwr_status;
	int ret;

	ret = regmap_read(chip->data.regmap, TCPC_POWER_STATUS, &pwr_status);
	if (ret < 0) {
		LOG(LOG_LVL_DEBUG, chip->log, "Abort %s; TCPC_POWER_STATUS read error", __func__);
		return;
	}

	if (!!(pwr_status & TCPC_POWER_STATUS_VBUS_PRES) &&
	    (cc_open_or_toggling(chip->cc1, chip->cc2) ||
	     (chip->cc1 == TYPEC_CC_RP_DEF && chip->cc2 == TYPEC_CC_RP_DEF)) &&
	    !chip->compliance_warnings->missing_rp) {
		LOG(LOG_LVL_DEBUG, chip->log,
		    "%s: Missing or incorrect Rp partner detected. Enable WAR", __func__);
		/* Assume DCP for missing Rp non-compliant power source */
		val.intval = POWER_SUPPLY_USB_TYPE_DCP;
		max77759_set_vbus(chip->tcpci, chip->tcpci->data, false, true);
		if (power_supply_set_property(chip->usb_psy, POWER_SUPPLY_PROP_USB_TYPE, &val))
			LOG(LOG_LVL_DEBUG, chip->log, "%s: usb_psy set dcp failed", __func__);
		chip->vbus_mv = 5000;
		/* val.intval does not matter */
		ret = power_supply_set_property(chip->usb_psy, POWER_SUPPLY_PROP_VOLTAGE_MAX, &val);
		if (ret < 0)
			LOG(LOG_LVL_DEBUG, chip->log, "%s: unable to set max voltage to %d, ret=%d",
			    chip->vbus_mv, ret, __func__);
		update_compliance_warnings(chip, COMPLIANCE_WARNING_MISSING_RP, true);
		usb_psy_set_sink_state(chip->usb_psy_data, true);
	} else if (chip->compliance_warnings->missing_rp) {
		if (!(pwr_status & TCPC_POWER_STATUS_VBUS_PRES))
			disconnect_missing_rp_partner(chip);
	}
}

static void check_missing_rp(struct max77759_plat *chip, bool vbus_present,
			     enum typec_cc_status cc1, enum typec_cc_status cc2)
{
	unsigned int pwr_status;
	int ret;

	ret = regmap_read(chip->data.regmap, TCPC_POWER_STATUS, &pwr_status);
	if (ret < 0) {
		LOG(LOG_LVL_DEBUG, chip->log, "Abort %s; TCPC_POWER_STATUS read error", __func__);
		return;
	}

	if (!!(pwr_status & TCPC_POWER_STATUS_VBUS_PRES) && cc_open_or_toggling(cc1, cc2)) {
		kthread_mod_delayed_work(chip->wq, &chip->check_missing_rp_work,
					 msecs_to_jiffies(chip->first_rp_missing_timeout ?
					 MAX77759_FIRST_RP_MISSING_TIMEOUT_MS :
					 MAX77759_RP_MISSING_TIMEOUT_MS));
		chip->first_rp_missing_timeout = false;
	} else if (chip->compliance_warnings->missing_rp) {
		kthread_cancel_delayed_work_sync(&chip->check_missing_rp_work);
		if (!(pwr_status & TCPC_POWER_STATUS_VBUS_PRES))
			disconnect_missing_rp_partner(chip);
	}
}

static void process_power_status(struct max77759_plat *chip)
{
	struct tcpci *tcpci = chip->tcpci;
	struct logbuffer *log = chip->log;
	unsigned int pwr_status;
	int ret;

	ret = regmap_read(tcpci->regmap, TCPC_POWER_STATUS, &pwr_status);
	LOG(LOG_LVL_DEBUG, log, "TCPC_ALERT_POWER_STATUS status:0x%x", pwr_status);
	if (ret < 0)
		return;

	if (pwr_status == 0xff) {
		max77759_init_regs(tcpci->regmap, log);
		return;
	}

	if (pwr_status & TCPC_POWER_STATUS_SOURCING_VBUS) {
		if (!(pwr_status & TCPC_POWER_STATUS_VBUS_PRES)) {
			/*
			 * Sourcing vbus might be set before vbus present is
			 * set. This implies vbus has not reached VSAFE5V yet
			 * (or) TCPC_POWER_STATUS_VBUS_PRES is arriving late.
			 * Hold back signalling sourcing vbus here.
			 */
			LOG(LOG_LVL_DEBUG, log, "Discard sourcing vbus. Vbus present not set");
		} else {
			chip->sourcing_vbus = 1;
			tcpm_sourcing_vbus(tcpci->port);
			chip->in_frs = false;
		}
	}

	if (chip->in_frs) {
		chip->in_frs = false;
		/*
		 * While in FRS transition consider vbus present as a signal for
		 * sourcing vbus as controller would have reversed the direction
		 * here. This signal could arrive before or after
		 * TCPC_POWER_STATUS_SOURCING_VBUS
		 */
		if (pwr_status & TCPC_POWER_STATUS_VBUS_PRES) {
			chip->sourcing_vbus = 1;
			tcpm_sourcing_vbus(tcpci->port);
		}
	}

	if (pwr_status & TCPC_POWER_STATUS_VBUS_PRES)
		chip->vbus_present = 1;
	else if (!chip->data.auto_discharge_disconnect && !(pwr_status &
							    TCPC_POWER_STATUS_VBUS_PRES))
		chip->vbus_present = 0;
	LOG(LOG_LVL_DEBUG, chip->log, "[%s]: vbus_present %d", __func__, chip->vbus_present);
	tcpm_vbus_change(tcpci->port);
	/*
	 * Check for missing-rp non compliant power source.
	 * Skip when usb is throttled due to overheat.
	 */
	if (!chip->usb_throttled)
		check_missing_rp(chip, !!(pwr_status & TCPC_POWER_STATUS_VBUS_PRES), chip->cc1,
				 chip->cc2);

	if (chip->quick_ramp_vbus_ovp && chip->vbus_present) {
		kthread_cancel_delayed_work_sync(&chip->reset_ovp_work);
		chip->reset_ovp_retry = 0;
	}

	/* TODO: remove this cc event b/211341677 */
	if (!strncmp(boot_mode_string, "charger", strlen("charger")) && chip->vbus_present) {
		dev_info(chip->dev, "WA: trigger cc event in charger mode");
		tcpm_cc_change(tcpci->port);
	}

	/*
	 * Enable data path when TCPC signals sink debug accesssory connected
	 * and disable when disconnected.
	 */
	if ((!chip->debug_acc_connected && (pwr_status & TCPC_POWER_STATUS_DBG_ACC_CON)) ||
	    (chip->debug_acc_connected && !(pwr_status & TCPC_POWER_STATUS_DBG_ACC_CON))) {
		mutex_lock(&chip->data_path_lock);
		chip->debug_acc_connected = pwr_status & TCPC_POWER_STATUS_DBG_ACC_CON ? 1 : 0;
		chip->data_role = TYPEC_DEVICE;
		/*
		 * Renable BC1.2 upon disconnect if disabled. Needed for
		 * sink-only mode such as fastbootd/Recovery.
		 */
		if (chip->attached && !chip->debug_acc_connected && !bc12_get_status(chip->bc12))
			bc12_enable(chip->bc12, true);
		chip->attached = chip->debug_acc_connected;
		enable_data_path_locked(chip);
		mutex_unlock(&chip->data_path_lock);

		/* Log Debug Accessory to Device Compliance Warnings, or Remove from List. */
		update_compliance_warnings(chip, COMPLIANCE_WARNING_DEBUG_ACCESSORY,
					   chip->debug_acc_connected);

		LOG(LOG_LVL_DEBUG, log,
		    "Debug accessory %s", chip->debug_acc_connected ? "connected" : "disconnected");
		if (!chip->debug_acc_connected && modparam_conf_sbu) {
			ret = max77759_write8(tcpci->regmap, TCPC_VENDOR_SBUSW_CTRL,
					      SBUSW_SERIAL_UART);
			LOG(LOG_LVL_DEBUG, log,
			    "SBU switch enable %s", ret < 0 ? "fail" : "success");
		}
		usb_psy_set_attached_state(chip->usb_psy_data, chip->attached);
	}
}

static void process_tx(struct tcpci *tcpci, u16 status, struct logbuffer *log)
{
	if (status & TCPC_ALERT_TX_SUCCESS) {
		LOG(LOG_LVL_DEBUG, log, "TCPC_ALERT_TX_SUCCESS");
		tcpm_pd_transmit_complete(tcpci->port, TCPC_TX_SUCCESS);
	} else if (status & TCPC_ALERT_TX_DISCARDED) {
		LOG(LOG_LVL_DEBUG, log, "TCPC_ALERT_TX_DISCARDED");
		tcpm_pd_transmit_complete(tcpci->port, TCPC_TX_DISCARDED);
	} else if (status & TCPC_ALERT_TX_FAILED) {
		LOG(LOG_LVL_DEBUG, log, "TCPC_ALERT_TX_FAILED");
		tcpm_pd_transmit_complete(tcpci->port, TCPC_TX_FAILED);
	}

	/* Reinit regs as Hard reset sets them to default value */
	if ((status & TCPC_ALERT_TX_SUCCESS) && (status &
						 TCPC_ALERT_TX_FAILED))
		max77759_init_regs(tcpci->regmap, log);
}

static int max77759_enable_voltage_alarm(struct max77759_plat *chip, bool enable, bool high)
{
	int ret;

	if (!enable) {
		ret = max77759_update_bits8(chip->tcpci->regmap, TCPC_POWER_CTRL,
					    TCPC_DIS_VOLT_ALRM, TCPC_DIS_VOLT_ALRM);
		if (ret < 0)
			LOG(LOG_LVL_DEBUG, chip->log,
			    "Unable to disable voltage alarm, ret = %d", ret);
		return ret;
	}

	/* Set voltage alarm */
	ret = max77759_update_bits16(chip->tcpci->regmap, TCPC_VBUS_VOLTAGE_ALARM_HI_CFG,
				     TCPC_VBUS_VOLTAGE_MASK,
				     (high ? VOLTAGE_ALARM_HI_EN_MV : VOLTAGE_ALARM_HI_DIS_MV) /
				     TCPC_VBUS_VOLTAGE_LSB_MV);
	if (ret < 0) {
		LOG(LOG_LVL_DEBUG, chip->log,
		    "Unable to config VOLTAGE_ALARM_HI_CFG, ret = %d", ret);
		return ret;
	}

	ret = max77759_update_bits16(chip->tcpci->regmap, TCPC_VBUS_VOLTAGE_ALARM_LO_CFG,
				     TCPC_VBUS_VOLTAGE_MASK,
				     (!high ? VOLTAGE_ALARM_LOW_EN_MV : VOLTAGE_ALARM_LOW_DIS_MV) /
				     TCPC_VBUS_VOLTAGE_LSB_MV);
	if (ret < 0) {
		LOG(LOG_LVL_DEBUG, chip->log,
		    "Unable to config VOLTAGE_ALARM_LO_CFG, ret = %d", ret);
		return ret;
	}

	ret = max77759_update_bits8(chip->tcpci->regmap, TCPC_POWER_CTRL, TCPC_DIS_VOLT_ALRM, 0);
	if (ret < 0) {
		LOG(LOG_LVL_DEBUG, chip->log, "Unable to enable voltage alarm, ret = %d", ret);
		return ret;
	}

	ret = max77759_update_bits16(chip->tcpci->regmap, TCPC_ALERT_MASK,
				     TCPC_ALERT_V_ALARM_LO | TCPC_ALERT_V_ALARM_HI,
				     high ? TCPC_ALERT_V_ALARM_HI : TCPC_ALERT_V_ALARM_LO);
	if (ret < 0)
		LOG(LOG_LVL_DEBUG, chip->log,
		    "Unable to unmask voltage alarm interrupt, ret = %d", ret);

	return ret;
}

static int max77759_get_vbus_voltage_mv(struct i2c_client *tcpc_client)
{
	u16 raw;
	int ret;
	struct max77759_plat *chip = i2c_get_clientdata(tcpc_client);

	if (!chip || !chip->tcpci || !chip->tcpci->regmap)
		return -EAGAIN;

	/* TCPC_POWER_CTRL_VBUS_VOLT_MON enabled in init_regs */
	ret = max77759_read16(chip->tcpci->regmap, TCPC_VBUS_VOLTAGE, &raw);

	return ret ? 0 : ((raw & TCPC_VBUS_VOLTAGE_MASK) * TCPC_VBUS_VOLTAGE_LSB_MV);
}

/* Acquire rc lock before calling */
static void floating_cable_sink_detected_handler_locked(struct max77759_plat *chip)
{
	chip->floating_cable_or_sink_detected++;
	LOG(LOG_LVL_DEBUG, chip->log,
	    "floating_cable_or_sink_detected count: %d", chip->floating_cable_or_sink_detected);
	if (chip->floating_cable_or_sink_detected >= FLOATING_CABLE_OR_SINK_INSTANCE_THRESHOLD) {
		disable_auto_ultra_low_power_mode(chip, true);
		alarm_start_relative(&chip->reenable_auto_ultra_low_power_mode_alarm,
				     ms_to_ktime(AUTO_ULTRA_LOW_POWER_MODE_REENABLE_MS));
	}
}

static void ovp_operation(struct max77759_plat *chip, int operation)
{
	int gpio_val, retry = 0;

	mutex_lock(&chip->ovp_lock);
	if (operation == OVP_RESET || operation == OVP_OFF) {
		do {
			gpio_set_value_cansleep(chip->in_switch_gpio,
						!chip->in_switch_gpio_active_high);
			gpio_val = gpio_get_value(chip->in_switch_gpio);
			LOG(LOG_LVL_DEBUG, chip->log,
				      "%s: OVP disable gpio_val:%d in_switch_gpio_active_high:%d retry:%d",
				       __func__, gpio_val, chip->in_switch_gpio_active_high, retry++);
		} while ((gpio_val != !chip->in_switch_gpio_active_high) && (retry < OVP_OP_RETRY));
	}

	if (operation == OVP_RESET)
		mdelay(10);

	if (operation == OVP_RESET || operation == OVP_ON) {
		retry = 0;
		do {
			gpio_set_value_cansleep(chip->in_switch_gpio,
						chip->in_switch_gpio_active_high);
			gpio_val = gpio_get_value(chip->in_switch_gpio);
			LOG(LOG_LVL_DEBUG, chip->log,
				      "%s: OVP enable gpio_val:%d in_switch_gpio_active_high:%d retry:%d",
				      __func__, gpio_val, chip->in_switch_gpio_active_high, retry++);
		} while ((gpio_val != chip->in_switch_gpio_active_high) && (retry < OVP_OP_RETRY));
	}
	mutex_unlock(&chip->ovp_lock);
}

static void reset_ovp_work(struct kthread_work *work)
{
	struct max77759_plat *chip  =
		container_of(container_of(work, struct kthread_delayed_work, work),
			     struct max77759_plat, reset_ovp_work);
	u16 vbus_mv = max77759_get_vbus_voltage_mv(chip->client);

	LOG(LOG_LVL_DEBUG, chip->log, "%s: vbus %u mv", __func__, vbus_mv);

	if (vbus_mv > VBUS_PRESENT_THRESHOLD_MV)
		return;

	ovp_operation(chip, OVP_RESET);
	chip->reset_ovp_retry++;

	LOG(LOG_LVL_DEBUG, chip->log, "ovp reset done [%d]", chip->reset_ovp_retry);

	if (chip->reset_ovp_retry < VBUS_RAMPUP_MAX_RETRY)
		kthread_mod_delayed_work(chip->wq, &chip->reset_ovp_work,
					 msecs_to_jiffies(VBUS_RAMPUP_TIMEOUT_MS));
	else
		chip->reset_ovp_retry = 0;

}

static enum typec_cc_status tcpci_to_typec_cc(unsigned int cc, bool sink)
{
	switch (cc) {
	case 0x1:
		return sink ? TYPEC_CC_RP_DEF : TYPEC_CC_RA;
	case 0x2:
		return sink ? TYPEC_CC_RP_1_5 : TYPEC_CC_RD;
	case 0x3:
		if (sink)
			return TYPEC_CC_RP_3_0;
		fallthrough;
	case 0x0:
	default:
		return TYPEC_CC_OPEN;
	}
}

static void max77759_get_cc(struct max77759_plat *chip, enum typec_cc_status *cc1,
			    enum typec_cc_status *cc2)
{
	struct tcpci *tcpci = chip->tcpci;
	u8 reg, role_control;
	int ret;

	ret = max77759_read8(tcpci->regmap, TCPC_ROLE_CTRL, &role_control);
	if (ret < 0)
		return;

	ret = max77759_read8(tcpci->regmap, TCPC_CC_STATUS, &reg);
	if (ret < 0)
		return;

	*cc1 = tcpci_to_typec_cc((reg >> TCPC_CC_STATUS_CC1_SHIFT) &
				TCPC_CC_STATUS_CC1_MASK,
				reg & TCPC_CC_STATUS_TERM ||
				tcpc_presenting_rd(role_control, CC1));
	*cc2 = tcpci_to_typec_cc((reg >> TCPC_CC_STATUS_CC2_SHIFT) &
				TCPC_CC_STATUS_CC2_MASK,
				reg & TCPC_CC_STATUS_TERM ||
				tcpc_presenting_rd(role_control, CC2));
}

static void max77759_cache_cc(struct max77759_plat *chip)
{
	enum typec_cc_status cc1, cc2;

	max77759_get_cc(chip, &cc1, &cc2);
	/*
	 * If the Vbus OVP is restricted to quick ramp-up time for incoming Vbus to work properly,
	 * queue a delayed work to check the Vbus status later. Cancel the delayed work once the CC
	 * is back to Open as we won't expect that Vbus is coming.
	 */
	if (chip->quick_ramp_vbus_ovp) {
		if (cc_open_or_toggling(chip->cc1, chip->cc2) && port_is_sink(cc1, cc2)) {
			kthread_mod_delayed_work(chip->wq, &chip->reset_ovp_work,
						 msecs_to_jiffies(VBUS_RAMPUP_TIMEOUT_MS));
		} else if (cc_open_or_toggling(cc1, cc2)) {
			kthread_cancel_delayed_work_sync(&chip->reset_ovp_work);
			chip->reset_ovp_retry = 0;
		}
	}

	LOG(LOG_LVL_DEBUG, chip->log,
	    "cc1: %u -> %u cc2: %u -> %u", chip->cc1, cc1, chip->cc2, cc2);
	chip->cc1 = cc1;
	chip->cc2 = cc2;
}

/* hold irq_status_lock before calling */
static irqreturn_t _max77759_irq_locked(struct max77759_plat *chip, u16 status,
					struct logbuffer *log)
{
	u16 vendor_status = 0, vendor_status2 = 0, raw;
	struct tcpci *tcpci = chip->tcpci;
	int ret;
	const u16 mask = status & TCPC_ALERT_RX_BUF_OVF ? status &
		~(TCPC_ALERT_RX_STATUS | TCPC_ALERT_RX_BUF_OVF) :
		status & ~TCPC_ALERT_RX_STATUS;
	u8 reg_status;
	bool contaminant_cc_update_handled = false, invoke_tcpm_for_cc_update = false,
		port_clean = false;
	unsigned int pwr_status;

	pm_wakeup_event(chip->dev, PD_ACTIVITY_TIMEOUT_MS);
	LOG(LOG_LVL_DEBUG, log, "TCPC_ALERT status: %#x", status);
	/**
	 * Clear alert status for everything except RX_STATUS, which shouldn't
	 * be cleared until we have successfully retrieved message.
	 **/
	if (status & ~TCPC_ALERT_RX_STATUS) {
		ret = max77759_write16(tcpci->regmap, TCPC_ALERT, mask);
		if (ret < 0)
			goto reschedule;
	}

	if (status & TCPC_ALERT_RX_BUF_OVF && !(status &
						TCPC_ALERT_RX_STATUS)) {
		LOG(LOG_LVL_DEBUG, log, "TCPC_ALERT_RX_BUF_OVF");
		ret = max77759_write16(tcpci->regmap, TCPC_ALERT,
				       (TCPC_ALERT_RX_STATUS |
					TCPC_ALERT_RX_BUF_OVF));
		if (ret < 0)
			goto reschedule;
	}

	if (status & TCPC_ALERT_EXTND) {
		ret = max77759_read8(tcpci->regmap, TCPC_ALERT_EXTENDED, &reg_status);
		if (ret < 0)
			goto reschedule;

		ret = max77759_write8(tcpci->regmap, TCPC_ALERT_EXTENDED, reg_status);
		if (ret < 0)
			goto reschedule;

		if (reg_status & TCPC_SINK_FAST_ROLE_SWAP) {
			LOG(LOG_LVL_DEBUG, log, "FRS Signal");
			chip->in_frs = true;
			tcpm_sink_frs(tcpci->port);
		}
	}

	if (status & TCPC_ALERT_RX_STATUS) {
		LOG(LOG_LVL_DEBUG, log, "Enter process rx");
		ret = process_rx(chip, status);
		if (ret == -EIO)
			goto reschedule;
	}

	if (status & TCPC_ALERT_TX_DISCARDED)
		LOG(LOG_LVL_DEBUG, log, "TX_DISCARDED");

	if (status & TCPC_ALERT_VENDOR) {
		LOG(LOG_LVL_DEBUG, log, "TCPC_VENDOR_ALERT Mask");
		ret = max77759_write8(tcpci->regmap, TCPC_VENDOR_ALERT_MASK
				      , 0x0);
		if (ret < 0)
			goto reschedule;

		ret = max77759_write8(tcpci->regmap,
				      TCPC_VENDOR_ALERT_MASK2, 0x0);
		if (ret < 0)
			goto reschedule;

		/* Clear VENDOR_ALERT*/
		ret = max77759_read16(tcpci->regmap, TCPC_VENDOR_ALERT,
				      &vendor_status);
		if (ret < 0)
			goto reschedule;
		LOG(LOG_LVL_DEBUG, log, "TCPC_VENDOR_ALERT 0x%x", vendor_status);

		process_bc12_alert(chip->bc12, vendor_status);
		ret = max77759_write16(tcpci->regmap, TCPC_VENDOR_ALERT,
				       vendor_status);

		ret = max77759_read16(tcpci->regmap, TCPC_VENDOR_ALERT2, &vendor_status2);
		if (ret < 0)
			goto reschedule;
		LOG(LOG_LVL_DEBUG, log, "TCPC_VENDOR_ALERT2 0x%x", vendor_status2);

		ret = max77759_write16(tcpci->regmap, TCPC_VENDOR_ALERT2, vendor_status2);
		if (ret < 0)
			goto reschedule;
	}

	if (status & TCPC_ALERT_VBUS_DISCNCT) {
		LOG(LOG_LVL_DEBUG, log, "TCPC_ALERT_VBUS_DISCNCT");
		chip->vbus_present = 0;
		LOG(LOG_LVL_DEBUG, chip->log,
		    "[%s]: vbus_present %d", __func__, chip->vbus_present);
		tcpm_vbus_change(tcpci->port);
		if (chip->force_device_mode_on) {
			ret = max77759_write8(tcpci->regmap, TCPC_VENDOR_USBSW_CTRL, USBSW_CONNECT);
			LOG(LOG_LVL_DEBUG, chip->log,
			    "Forcing on dp switches %s", ret < 0 ? "fail" : "success");
			if (ret < 0)
				goto reschedule;
		}
	}

	if (status & TCPC_ALERT_CC_STATUS) {
		/**
		 * Process generic CC updates if it doesn't belong to
		 * contaminant detection.
		 */
		mutex_lock(&chip->rc_lock);
		LOG(LOG_LVL_DEBUG, chip->log, "Servicing TCPC_ALERT_CC_STATUS");
		if (!chip->usb_throttled && chip->contaminant_detection &&
		    tcpm_port_is_toggling(tcpci->port)) {
			LOG(LOG_LVL_DEBUG, chip->log, "Invoking process_contaminant_alert");
			ret = process_contaminant_alert(chip->contaminant, false, true,
							&contaminant_cc_update_handled,
							&port_clean);
			if (ret < 0) {
				mutex_unlock(&chip->rc_lock);
				goto reschedule;
			} else if (chip->check_contaminant) {
				/*
				 * Taken in debounce path when the port is dry.
				 * Move TCPM back to TOGGLING.
				 */
				if (port_clean) {
					chip->check_contaminant = false;
					tcpm_port_clean(chip->port);
				}
				/* tcpm_cc_change does not have to be invoked. */
				invoke_tcpm_for_cc_update = false;
			} else {
				/*
				 * Invoke TCPM when CC update not related to contaminant detection.
				 */
				invoke_tcpm_for_cc_update = !contaminant_cc_update_handled;
				/*
				 * CC status change handled by contaminant algorithm.
				 * Handle floating cable if detected.
				 */
				if (contaminant_cc_update_handled) {
					LOG(LOG_LVL_DEBUG, log,
					    "CC update: Contaminant algorithm responded");
					if (is_floating_cable_or_sink_detected(chip)) {
						floating_cable_sink_detected_handler_locked(chip);
						LOG(LOG_LVL_DEBUG, chip->log,
						    "Floating cable detected");
					} else {
						chip->floating_cable_or_sink_detected = 0;
						LOG(LOG_LVL_DEBUG, chip->log,
						    "Floating cable counter cleared");
					}
				}
			}
		} else {
			invoke_tcpm_for_cc_update = true;
		}

		if (invoke_tcpm_for_cc_update) {
			LOG(LOG_LVL_DEBUG, chip->log, "invoke_tcpm_for_cc_update");
			tcpm_cc_change(tcpci->port);
			max77759_cache_cc(chip);
			/* Check for missing-rp non compliant power source */
			if (!regmap_read(tcpci->regmap, TCPC_POWER_STATUS, &pwr_status) &&
			    !chip->usb_throttled)
				check_missing_rp(chip, !!(pwr_status & TCPC_POWER_STATUS_VBUS_PRES),
						 chip->cc1, chip->cc2);
			/* TCPM has detected valid CC terminations */
			if (!tcpm_port_is_toggling(tcpci->port)) {
				chip->floating_cable_or_sink_detected = 0;
				/*
				 * Only re-enable auto ultra low power mode only
				 * when contaminant detection is enabled.
				 */
				if (chip->contaminant_detection_userspace !=
					CONTAMINANT_DETECT_DISABLE)
					disable_auto_ultra_low_power_mode(chip, false);
			} else if (!chip->usb_throttled && chip->contaminant_detection) {
				/*
				 * TCPM has not detected valid CC terminations
				 * and neither the comparators nor ADC
				 * readings indicate sink or floating cable.
				 * Mitigate AP wakeups here.
				 *
				 * The counter will also incremented when
				 * transitioning from *_READY states to
				 * TOGGLING state. This shouldn't have adverse
				 * effect as the FLOATING_CABLE_OR_SINK_INSTANCE_THRESHOLD
				 * is now doubled.
				 */
				LOG(LOG_LVL_DEBUG, chip->log, "Treating as floating cable");
				floating_cable_sink_detected_handler_locked(chip);
			}
		}
		mutex_unlock(&chip->rc_lock);
	}

	if (status & TCPC_ALERT_POWER_STATUS)
		process_power_status(chip);

	if (status & TCPC_ALERT_V_ALARM_LO) {
		ret = max77759_read16(tcpci->regmap, TCPC_VBUS_VOLTAGE_ALARM_LO_CFG, &raw);
		if (ret < 0)
			goto reschedule;

		LOG(LOG_LVL_DEBUG, log, "VBUS LOW ALARM triggered: thresh:%umv vbus:%umv",
		    (raw & TCPC_VBUS_VOLTAGE_MASK) * TCPC_VBUS_VOLTAGE_LSB_MV,
		    max77759_get_vbus_voltage_mv(chip->client));
		max77759_enable_voltage_alarm(chip, true, true);

		ret = extcon_set_state_sync(chip->extcon, EXTCON_MECHANICAL, 0);
		LOG(LOG_LVL_DEBUG, chip->log, "%s turning off connected, ret=%d",
		    __func__, ret < 0 ? "Failed" : "Succeeded", ret);
	}

	if (status & TCPC_ALERT_V_ALARM_HI) {
		ret = max77759_read16(tcpci->regmap, TCPC_VBUS_VOLTAGE_ALARM_HI_CFG, &raw);
		if (ret < 0)
			goto reschedule;

		LOG(LOG_LVL_DEBUG, log, "VBUS HIGH ALARM triggered: thresh:%umv vbus:%umv",
		    (raw & TCPC_VBUS_VOLTAGE_MASK) * TCPC_VBUS_VOLTAGE_LSB_MV,
		    max77759_get_vbus_voltage_mv(chip->client));
		max77759_enable_voltage_alarm(chip, true, false);

		ret = extcon_set_state_sync(chip->extcon, EXTCON_MECHANICAL, 1);
		LOG(LOG_LVL_DEBUG, chip->log, "%s: %s turning on connected, ret=%d",
		    __func__, ret < 0 ? "Failed" : "Succeeded", ret);
	}

	if (status & TCPC_ALERT_RX_HARD_RST) {
		LOG(LOG_LVL_DEBUG, log, "TCPC_ALERT_RX_HARD_RST");
		/* To prevent disconnect during hardreset. */
		ret = max77759_write16(tcpci->regmap,
				       TCPC_VBUS_SINK_DISCONNECT_THRESH,
				       0);
		if (ret < 0)
			goto reschedule;

		tcpm_pd_hard_reset(tcpci->port);
		max77759_init_regs(tcpci->regmap, log);
	}

	if (status & TCPC_ALERT_TX_SUCCESS || status &
	    TCPC_ALERT_TX_DISCARDED || status & TCPC_ALERT_TX_FAILED)
		process_tx(tcpci, status, log);

	if (status & TCPC_ALERT_VENDOR) {
		LOG(LOG_LVL_DEBUG, log, "Exit TCPC_VENDOR_ALERT Unmask");
		ret = max77759_write8(tcpci->regmap, TCPC_VENDOR_ALERT_MASK
				      , 0xff);
		if (ret < 0)
			goto reschedule;
		ret = max77759_write8(tcpci->regmap,
				      TCPC_VENDOR_ALERT_MASK2, 0xff);
		if (ret < 0)
			goto reschedule;
	}

	if (status & TCPC_ALERT_EXTENDED_STATUS) {
		bool vsafe0v;
		ret = max77759_read8(tcpci->regmap, TCPC_EXTENDED_STATUS,
				     (u8 *)&raw);
		if (ret < 0)
			goto reschedule;

		vsafe0v = raw & TCPC_EXTENDED_STATUS_VSAFE0V;
		LOG(LOG_LVL_DEBUG, log, "VSAFE0V (runtime): %c -> %c",
		    chip->vsafe0v ? 'Y' : 'N', vsafe0v ? 'Y' : 'N');

		/*
		 * b/199991513 For some OVP chips, when the incoming Vbus ramps up from 0, there is
		 * a chance that an induced voltage (over Vsafe0V) behind the OVP would appear for a
		 * short time and then drop to 0 (Vsafe0V), and ramp up to some HIGH voltage
		 * (e.g Vsafe5V). To ignore the unwanted Vsafe0V event, queue a delayed work and
		 * re-check the voltage after VSAFE0V_DEBOUNCE_MS.
		 *
		 * The OVP which is restricted to quick ramp-up Vbus is the same as the one
		 * mentioned above. Thus re-use the same flag chip->quick_ramp_vbus_ovp.
		 */
		if (chip->quick_ramp_vbus_ovp) {
			if (!chip->vsafe0v && vsafe0v)
				kthread_mod_delayed_work(chip->wq, &chip->vsafe0v_work,
							 msecs_to_jiffies(VSAFE0V_DEBOUNCE_MS));
		} else if (vsafe0v) {
			chip->vbus_present = 0;
			LOG(LOG_LVL_DEBUG, chip->log,
			    "[%s]: vbus_present %d", __func__, chip->vbus_present);
			tcpm_vbus_change(tcpci->port);
		}

		chip->vsafe0v = vsafe0v;
	}

	LOG(LOG_LVL_DEBUG, log, "TCPC_ALERT status done: %#x", status);

	return IRQ_HANDLED;
reschedule:
	chip->irq_status = status;
	LOG(LOG_LVL_DEBUG, log, "TCPC_ALERT IO error occurred. status: %#x", status);
	kthread_mod_delayed_work(chip->wq, &chip->max77759_io_error_work,
				 msecs_to_jiffies(IO_ERROR_RETRY_MS));
	pm_wakeup_event(chip->dev, PD_ACTIVITY_TIMEOUT_MS + IO_ERROR_RETRY_MS);
	return IRQ_HANDLED;
}

static irqreturn_t max77759_irq(int irq, void *dev_id)
{
	struct max77759_plat *chip = dev_id;
	u16 status;
	irqreturn_t irq_return;
	int ret;

	LOG(LOG_LVL_DEBUG, chip->log, "TCPC_ALERT threaded irq running ");
	if (!chip->tcpci)
		return IRQ_HANDLED;

	ret = max77759_read16(chip->tcpci->regmap, TCPC_ALERT, &status);
	if (ret < 0)
		return ret;
	mutex_lock(&chip->irq_status_lock);
	while (status) {
		irq_return = _max77759_irq_locked(chip, status, chip->log);
		/* Do not return if the ALERT is already set. */
		LOG(LOG_LVL_DEBUG, chip->log, "TCPC_ALERT read alert status");
		ret = max77759_read16(chip->tcpci->regmap, TCPC_ALERT, &status);
		if (ret < 0)
			break;
		LOG(LOG_LVL_DEBUG, chip->log, "TCPC_ALERT status pending: %#x", status);
	}
	mutex_unlock(&chip->irq_status_lock);

	return irq_return;
}

static irqreturn_t max77759_isr(int irq, void *dev_id)
{
	struct max77759_plat *chip = dev_id;

	LOG(LOG_LVL_DEBUG, chip->log, "TCPC_ALERT triggered ");
	pm_wakeup_event(chip->dev, PD_ACTIVITY_TIMEOUT_MS);

	if (!chip->tcpci)
		return IRQ_HANDLED;

	return IRQ_WAKE_THREAD;
}

static void max77759_io_error_work(struct kthread_work *work)
{
	struct max77759_plat *chip =
		container_of(container_of(work, struct kthread_delayed_work, work),
			     struct max77759_plat, max77759_io_error_work);
	pm_wakeup_event(chip->dev, PD_ACTIVITY_TIMEOUT_MS);
	mutex_lock(&chip->irq_status_lock);
	LOG(LOG_LVL_DEBUG, chip->log, "IO error retry. status: %#x", chip->irq_status);
	_max77759_irq_locked(chip, chip->irq_status, chip->log);
	mutex_unlock(&chip->irq_status_lock);
}

static int max77759_init_alert(struct max77759_plat *chip,
			       struct i2c_client *client)
{
	int ret, irq_gpio;

	irq_gpio = of_get_named_gpio(client->dev.of_node, "usbpd,usbpd_int", 0);
	client->irq = gpio_to_irq(irq_gpio);
	if (!client->irq)
		return -ENODEV;

	ret = devm_request_threaded_irq(chip->dev, client->irq, max77759_isr,
					max77759_irq,
					(IRQF_TRIGGER_LOW | IRQF_ONESHOT),
					dev_name(chip->dev), chip);

	if (ret < 0)
		return ret;

	enable_irq_wake(client->irq);
	return 0;
}

/* Called while holding rc_lock */
static void max77759_enable_toggling_locked(struct max77759_plat *chip, bool enable)
{
	int ret;

	if (!enable) {
		ret = max77759_write8(chip->data.regmap, TCPC_ROLE_CTRL, TCPCI_HI_Z_CC);
		LOG(LOG_LVL_DEBUG, chip->log, "%s: HI-Z ret:%d", __func__, ret);
		return;
	}

	ret = max77759_write8(chip->data.regmap, TCPC_ROLE_CTRL, chip->role_ctrl_cache);
	if (ret < 0) {
		LOG(LOG_LVL_DEBUG, chip->log, "%s: update ROLE_CTRL failed ret:%d", __func__, ret);
		return;
	}

	ret = max77759_update_bits8(chip->data.regmap, TCPC_TCPC_CTRL,
				    TCPC_TCPC_CTRL_EN_LK4CONN_ALRT,
				    TCPC_TCPC_CTRL_EN_LK4CONN_ALRT);
	if (ret < 0) {
		LOG(LOG_LVL_DEBUG, chip->log,
		    "%s: Enable LK4CONN alert failed ret:%d", __func__, ret);
		return;
	}

	ret = regmap_write(chip->data.regmap, TCPC_COMMAND, TCPC_CMD_LOOK4CONNECTION);
	if (ret < 0)
		LOG(LOG_LVL_DEBUG, chip->log, "%s: Enable LK4CONN failed ret:%d", __func__, ret);
}
static int max77759_start_toggling(struct tcpci *tcpci,
				   struct tcpci_data *tdata,
				   enum typec_cc_status cc)
{
	struct max77759_plat *chip = tdata_to_max77759(tdata);
	u8 reg = TCPC_ROLE_CTRL_DRP, pwr_ctrl;
	int ret;
	enum typec_cc_status cc1, cc2;

	max77759_get_cc(chip, &cc1, &cc2);

	switch (cc) {
	case TYPEC_CC_RP_DEF:
		reg |= (TCPC_ROLE_CTRL_RP_VAL_DEF <<
			TCPC_ROLE_CTRL_RP_VAL_SHIFT);
		break;
	case TYPEC_CC_RP_1_5:
		reg |= (TCPC_ROLE_CTRL_RP_VAL_1_5 <<
			TCPC_ROLE_CTRL_RP_VAL_SHIFT);
		break;
	case TYPEC_CC_RP_3_0:
		reg |= (TCPC_ROLE_CTRL_RP_VAL_3_0 <<
			TCPC_ROLE_CTRL_RP_VAL_SHIFT);
		break;
	default:
		break;
	}

	if (cc == TYPEC_CC_RD)
		reg |= (TCPC_ROLE_CTRL_CC_RD << TCPC_ROLE_CTRL_CC1_SHIFT) |
			(TCPC_ROLE_CTRL_CC_RD << TCPC_ROLE_CTRL_CC2_SHIFT);
	else
		reg |= (TCPC_ROLE_CTRL_CC_RP << TCPC_ROLE_CTRL_CC1_SHIFT) |
			(TCPC_ROLE_CTRL_CC_RP << TCPC_ROLE_CTRL_CC2_SHIFT);

	mutex_lock(&chip->toggle_lock);
	max77759_init_regs(chip->tcpci->regmap, chip->log);
	mutex_unlock(&chip->toggle_lock);

	chip->role_ctrl_cache = reg;
	mutex_lock(&chip->rc_lock);
	if (chip->toggle_disable_status)
		goto unlock;

	/* Kick debug accessory state machine when enabling toggling for the first time */
	if (chip->first_toggle) {
		if ((chip->in_switch_gpio >= 0) && is_debug_accessory_detected(cc1, cc2)) {
			LOG(LOG_LVL_DEBUG, chip->log, "[%s]: Kick Debug accessory FSM", __func__);
			ovp_operation(chip, OVP_RESET);
		}
		chip->first_toggle = false;
	}

	/* Renable BC1.2*/
	if (!bc12_get_status(chip->bc12))
		bc12_enable(chip->bc12, true);

	/* Re-enable retry */
	bc12_reset_retry(chip->bc12);

	/* Disable Auto disacharge before enabling toggling */
	ret = max77759_read8(tcpci->regmap, TCPC_POWER_CTRL, &pwr_ctrl);
	LOG(LOG_LVL_DEBUG, chip->log, "TCPC_POWER_CTRL:0x%x ret:%d", pwr_ctrl, ret);
	if (pwr_ctrl & TCPC_POWER_CTRL_AUTO_DISCHARGE) {
		LOG(LOG_LVL_DEBUG, chip->log, "TCPC_POWER_CTRL_AUTO_DISCHARGE not cleared");
		ret = regmap_update_bits(tcpci->regmap, TCPC_POWER_CTRL,
					 TCPC_POWER_CTRL_AUTO_DISCHARGE, 0);
		if (ret < 0)
			LOG(LOG_LVL_DEBUG, chip->log,
			    "[%s]: Disabling auto discharge failed", __func__);
	}

	/* b/223078393: Disable ext bst upon toggling */
	ret = max77759_write8(tcpci->regmap, TCPC_VENDOR_EXTBST_CTRL, 0);
	LOG(LOG_LVL_DEBUG, chip->log, "%s: clear TCPC_VENDOR_EXTBST_CTRL ret:%d", __func__, ret);

	if (chip->contaminant_detection)
		update_contaminant_detection_locked(chip, chip->contaminant_detection);
	else
		max77759_enable_toggling_locked(chip, true);

unlock:
	mutex_unlock(&chip->rc_lock);

	return 0;
}

static void max77759_set_partner_usb_comm_capable(struct tcpci *tcpci, struct tcpci_data *data,
						  bool capable)
{
	struct max77759_plat *chip = tdata_to_max77759(data);

	mutex_lock(&chip->data_path_lock);
	chip->pd_data_capable = capable;
	enable_data_path_locked(chip);
	mutex_unlock(&chip->data_path_lock);
}

static int max77759_usb_set_orientation(struct typec_switch *sw, enum typec_orientation orientation)
{
	struct max77759_plat *chip = typec_switch_get_drvdata(sw);
	enum typec_cc_polarity polarity = orientation == TYPEC_ORIENTATION_REVERSE ?
		TYPEC_POLARITY_CC2 : TYPEC_POLARITY_CC1;
	int ret;

	chip->orientation = orientation;
	ret = extcon_set_property(chip->extcon, EXTCON_USB, EXTCON_PROP_USB_TYPEC_POLARITY,
				  (union extcon_property_value)(int)polarity);
	logbuffer_logk(chip->log, LOGLEVEL_INFO,
		       "%s setting polarity USB %d", ret < 0 ? "Failed" : "Succeeded", polarity);

	ret = extcon_set_property(chip->extcon, EXTCON_USB_HOST, EXTCON_PROP_USB_TYPEC_POLARITY,
				  (union extcon_property_value)(int)polarity);
	logbuffer_logk(chip->log, LOGLEVEL_INFO, "%s setting polarity USB_HOST %d",
		       ret < 0 ? "Failed" : "Succeeded", polarity);
	return ret;
}

static int max77759_vote_icl(struct max77759_plat *chip, u32 max_ua)
{
	int ret = 0;
	struct usb_vote vote;

	/*
	 * TCPM sets max_ua to zero for Rp-default which needs to be
	 * ignored. PPS values reflect the requested ones not the max.
	 */
	mutex_lock(&chip->icl_proto_el_lock);
	if ((chip->usb_type != POWER_SUPPLY_USB_TYPE_PD && max_ua == 0 && chip->online) ||
	    chip->online == TCPM_PSY_PROG_ONLINE)
		goto exit;

	init_vote(&vote, proto_voter_reason[USB_ICL_PD], USB_ICL_PD, max_ua);
	ret = gvotable_cast_vote(chip->usb_icl_proto_el,
				 proto_voter_reason[USB_ICL_PD], &vote,
				 chip->online);

	LOG(LOG_LVL_DEBUG, chip->log, "%s: %s:%d voting enabled:%s usb proto_el: %d by %s",
	    __func__, ret < 0 ? "error" : "success", ret, chip->online ? "enabled" : "disabled",
	    vote.val, proto_voter_reason[USB_ICL_PD]);

exit:
	mutex_unlock(&chip->icl_proto_el_lock);
	return ret;
}

static void icl_work_item(struct kthread_work *work)
{
	struct max77759_plat *chip  =
		container_of(container_of(work, struct kthread_delayed_work, work),
			     struct max77759_plat, icl_work);
	union power_supply_propval current_max = {0}, voltage_max = {0}, online = {0},
	      usb_type = {0}, val = {0};
	int ret;

	power_supply_get_property(chip->tcpm_psy, POWER_SUPPLY_PROP_CURRENT_MAX, &current_max);
	power_supply_get_property(chip->tcpm_psy, POWER_SUPPLY_PROP_VOLTAGE_MAX, &voltage_max);
	power_supply_get_property(chip->tcpm_psy, POWER_SUPPLY_PROP_ONLINE, &online);
	power_supply_get_property(chip->tcpm_psy, POWER_SUPPLY_PROP_USB_TYPE, &usb_type);
	logbuffer_logk(chip->log, LOGLEVEL_INFO,
		      "%s: ONLINE:%d USB_TYPE:%d CURRENT_MAX:%d VOLTAGE_MAX:%d",
		      __func__, online.intval, usb_type.intval, current_max.intval, voltage_max.intval);

	/* Debounce disconnect for power adapters that can source at least 1.5A */
	if (chip->debounce_adapter_disconnect && chip->online && !online.intval &&
	    chip->typec_current_max >= 1500000) {
		logbuffer_log(chip->log, "Debouncing disconnect\n");
		/* Reduce current limit 500mA during debounce */
		max77759_vote_icl(chip, 500000);
		chip->debounce_adapter_disconnect = false;
		kthread_mod_delayed_work(chip->wq, &chip->icl_work,
					 msecs_to_jiffies(DISCONNECT_DEBOUNCE_MS));
		return;
	}

	chip->vbus_mv = voltage_max.intval / 1000;
	ret = power_supply_set_property(chip->usb_psy, POWER_SUPPLY_PROP_VOLTAGE_MAX, &val);
	if (ret < 0)
		LOG(LOG_LVL_DEBUG, chip->log,
		    "unable to set max voltage to %d, ret=%d", chip->vbus_mv, ret);

	chip->online = online.intval;
	chip->usb_type = usb_type.intval;
	chip->typec_current_max = current_max.intval;
	usb_psy_set_sink_state(chip->usb_psy_data, chip->online);
	max77759_vote_icl(chip, chip->typec_current_max);
}

static int psy_changed(struct notifier_block *nb, unsigned long evt, void *ptr)
{
	struct max77759_plat *chip = container_of(nb, struct max77759_plat, psy_notifier);
	struct power_supply *psy = ptr;
	union power_supply_propval online = {0}, usb_type = {0};

	if (!strstr(psy->desc->name, "tcpm-source") || evt != PSY_EVENT_PROP_CHANGED)
		return NOTIFY_OK;

	power_supply_get_property(psy, POWER_SUPPLY_PROP_ONLINE, &online);
	power_supply_get_property(psy, POWER_SUPPLY_PROP_USB_TYPE, &usb_type);
	logbuffer_logk(chip->log, LOGLEVEL_INFO, "ONLINE:%d USB_TYPE:%d", online.intval,
		       usb_type.intval);
	chip->tcpm_psy = psy;

	if (chip->online && !online.intval && chip->typec_current_max >= 1500000)
		chip->debounce_adapter_disconnect = true;
	else
		chip->debounce_adapter_disconnect = false;

	/* Notifier is atomic, hence offloading */
	kthread_mod_delayed_work(chip->wq, &chip->icl_work, 0);
	return NOTIFY_OK;
}

static int max77759_get_vbus_voltage_max_mv(struct i2c_client *tcpc_client)
{
	struct max77759_plat *chip = i2c_get_clientdata(tcpc_client);

	return chip ? chip->vbus_mv : 0;
}

static int max77759_set_vbus_voltage_max_mv(struct i2c_client *tcpc_client,
					    unsigned int mv)
{
	struct max77759_plat *chip = i2c_get_clientdata(tcpc_client);

	if (chip)
		chip->vbus_mv = mv;

	return 0;
}

static void max77759_get_vbus(void *unused, struct tcpci *tcpci, struct tcpci_data *data, int *vbus,
			      int *bypass)
{
	struct max77759_plat *chip = tdata_to_max77759(data);
	u8 pwr_status;
	int ret;

	ret = max77759_read8(tcpci->regmap, TCPC_POWER_STATUS, &pwr_status);
	if (!ret && !chip->vbus_present && (pwr_status & TCPC_POWER_STATUS_VBUS_PRES)) {
		LOG(LOG_LVL_DEBUG, chip->log, "[%s]: syncing vbus_present", __func__);
		chip->vbus_present = 1;
	}

	LOG(LOG_LVL_DEBUG, chip->log, "[%s]: vbus_present %d", __func__, chip->vbus_present);
	*vbus = chip->vbus_present;
	*bypass = 1;
}

static int max77759_usb_set_role(struct usb_role_switch *sw, enum usb_role role)
{
	struct max77759_plat *chip = usb_role_switch_get_drvdata(sw);
	enum typec_data_role typec_data_role = TYPEC_DEVICE;
	bool attached = role != USB_ROLE_NONE, enable_data;
	int ret;

	if (role == USB_ROLE_HOST)
		typec_data_role = TYPEC_HOST;

	mutex_lock(&chip->data_path_lock);

	enable_data = chip->pd_data_capable || chip->no_bc_12 || chip->bc12_data_capable ||
		chip->data_role == TYPEC_HOST || chip->debug_acc_connected;

	if (!chip->force_device_mode_on && chip->data_active &&
	    (chip->active_data_role != typec_data_role || !attached || !enable_data)) {
		ret = extcon_set_state_sync(chip->extcon,
					    chip->active_data_role ==
					    TYPEC_HOST ? EXTCON_USB_HOST :
					    EXTCON_USB, 0);

		LOG(LOG_LVL_DEBUG, chip->log, "%s turning off %s", ret < 0 ? "Failed" : "Succeeded",
		    chip->active_data_role == TYPEC_HOST ? "Host" : "Device");
		chip->data_active = false;
		if (data_active_callback)
			(*data_active_callback)(data_active_payload);

		if  (chip->active_data_role == TYPEC_HOST) {
			ret = max77759_write8(chip->data.regmap, TCPC_VENDOR_USBSW_CTRL,
					      USBSW_DISCONNECT);
			LOG(LOG_LVL_DEBUG, chip->log,
			    "Turning off dp switches %s", ret < 0 ? "fail" : "success");
		}
	}

	/* Renable BC1.2 */
	if (chip->attached && !attached && !bc12_get_status(chip->bc12))
		bc12_enable(chip->bc12, true);
	/*
	 * To prevent data stack enumeration failure, previously there
	 * was a 300msec delay here
	 */

	chip->attached = attached;
	chip->data_role = typec_data_role;
	enable_data_path_locked(chip);
	mutex_unlock(&chip->data_path_lock);
	usb_psy_set_attached_state(chip->usb_psy_data, chip->attached);

	/*
	 * Renable BC1.2 upon disconnect if disabled. Needed for sink-only mode such as
	 * fastbootd/Recovery.
	 */
	if (chip->attached && !attached && !bc12_get_status(chip->bc12))
		bc12_enable(chip->bc12, true);

	/*
	 * Clear COMPLIANCE_WARNING_INPUT_POWER_LIMITED which tracks AICL_ACTIVE only upon
	 * disconnect. This prevents the incommpatible charging notification to not change status
	 * during the charging session. AICL active is system/battery load dependent and
	 * hence can change status during a charge session.
	 */
	if (!attached) {
		update_compliance_warnings(chip, COMPLIANCE_WARNING_INPUT_POWER_LIMITED, false);
		/* Clear BC12 as fallback when hardware does not clear it on disconnect. */
		update_compliance_warnings(chip, COMPLIANCE_WARNING_BC12, false);
	}

	return 0;
}

static void max77759_store_partner_src_caps(void *unused,
					    unsigned int *nr_source_caps,
					    u32 (*source_caps)[PDO_MAX_OBJECTS])
{
	int i;

	spin_lock(&g_caps_lock);

	nr_partner_src_caps = *nr_source_caps > PDO_MAX_OBJECTS ?
			      PDO_MAX_OBJECTS : *nr_source_caps;

	for (i = 0; i < nr_partner_src_caps; i++)
		partner_src_caps[i] = (*source_caps)[i];

	spin_unlock(&g_caps_lock);
}

/*
 * Don't call this function in interrupt context. Caller needs to free the
 * memory by calling tcpm_put_partner_src_caps.
 */
int tcpm_get_partner_src_caps(struct tcpm_port *port, u32 **src_pdo)
{
	int i, ret;

	*src_pdo = kcalloc(PDO_MAX_OBJECTS, sizeof(u32), GFP_KERNEL);
	if (!src_pdo)
		return -ENOMEM;

	spin_lock(&g_caps_lock);

	if (!nr_partner_src_caps) {
		ret = -ENODATA;
		goto cleanup;
	}

	for (i = 0, ret = nr_partner_src_caps; i < nr_partner_src_caps; i++)
		(*src_pdo)[i] = partner_src_caps[i];

	goto unlock;

cleanup:
	kfree(*src_pdo);
	*src_pdo = NULL;
unlock:
	spin_unlock(&g_caps_lock);
	return ret;
}
EXPORT_SYMBOL_GPL(tcpm_get_partner_src_caps);

void tcpm_put_partner_src_caps(u32 **src_pdo)
{
	kfree(*src_pdo);
	*src_pdo = NULL;
}
EXPORT_SYMBOL_GPL(tcpm_put_partner_src_caps);

void max77759_bc12_is_running(struct max77759_plat *chip, bool running)
{
	if (chip) {
		mutex_lock(&chip->data_path_lock);
		chip->bc12_running = running;
		if (!running)
			enable_data_path_locked(chip);
		mutex_unlock(&chip->data_path_lock);
	}
}

static void max77759_set_port_data_capable(struct i2c_client *tcpc_client,
					   enum power_supply_usb_type
					   usb_type)
{
	struct max77759_plat *chip = i2c_get_clientdata(tcpc_client);

	switch (usb_type) {
	case POWER_SUPPLY_USB_TYPE_SDP:
	case POWER_SUPPLY_USB_TYPE_CDP:
		mutex_lock(&chip->data_path_lock);
		chip->bc12_data_capable = true;
		enable_data_path_locked(chip);
		mutex_unlock(&chip->data_path_lock);
		break;
	case POWER_SUPPLY_USB_TYPE_DCP:
	case POWER_SUPPLY_USB_TYPE_UNKNOWN:
		mutex_lock(&chip->data_path_lock);
		chip->bc12_data_capable = false;
		enable_data_path_locked(chip);
		mutex_unlock(&chip->data_path_lock);
		break;
	default:
		chip->bc12_data_capable = false;
		break;
	}
}

static const unsigned int usbpd_extcon_cable[] = {
	EXTCON_USB,
	EXTCON_USB_HOST,
	EXTCON_MECHANICAL,
	EXTCON_NONE,
};

static int tcpci_init(struct tcpci *tcpci, struct tcpci_data *data)
{
	/*
	 * Generic TCPCI overwrites the regs once this driver initializes
	 * them. Prevent this by returning -1.
	 */
	return -1;
}

static int usb_throttle_votable_callback(struct gvotable_election *el,
					 const char *reason, void *value)
{
	struct max77759_plat *chip = gvotable_get_data(el);
	int throttled = (long)value ? USB_SUSPENDED : USB_RESUMED;

	mutex_lock(&chip->rc_lock);
	chip->usb_throttled =  throttled;
	LOG(LOG_LVL_DEBUG, chip->log, "%s: reason %s value %ld\n", __func__, reason, (long)value);
	mutex_unlock(&chip->rc_lock);

	return 0;
}

static int max77759_toggle_disable_votable_callback(struct gvotable_election *el,
						    const char *reason, void *value)
{
	struct max77759_plat *chip = gvotable_get_data(el);
	int disable = (long)value ? MAX77759_DISABLE_TOGGLE : MAX77759_ENABLE_TOGGLE;

	mutex_lock(&chip->rc_lock);
	if (chip->toggle_disable_status == disable) {
		mutex_unlock(&chip->rc_lock);
		return 0;
	}

	chip->toggle_disable_status = disable;
	if (chip->toggle_disable_status) {
		update_contaminant_detection_locked(chip, CONTAMINANT_DETECT_DISABLE);
		disable_contaminant_detection(chip);
		max77759_enable_toggling_locked(chip, false);
		if (chip->in_switch_gpio >= 0) {
			ovp_operation(chip, OVP_OFF);
			LOG(LOG_LVL_DEBUG, chip->log, "[%s]: Disable in-switch set %s / active %s",
			    __func__, !chip->in_switch_gpio_active_high ? "high" : "low",
			    chip->in_switch_gpio_active_high ? "high" : "low");
		}
	} else {
		if (chip->contaminant_detection_userspace)
			update_contaminant_detection_locked(chip,
							    chip->contaminant_detection_userspace);
		else
			max77759_enable_toggling_locked(chip, true);
		if (chip->in_switch_gpio >= 0) {
			ovp_operation(chip, OVP_ON);
			LOG(LOG_LVL_DEBUG, chip->log, "[%s]: Enable in-switch set %s / active %s",
			    __func__, chip->in_switch_gpio_active_high ? "high" : "low",
			    chip->in_switch_gpio_active_high ? "high" : "low");
		}
	}
	mutex_unlock(&chip->rc_lock);
	LOG(LOG_LVL_DEBUG, chip->log, "%s: reason %s value %ld\n", __func__, reason, (long)value);
	return 0;
}

#ifdef CONFIG_DEBUG_FS
static ssize_t force_device_mode_on_write(struct file *file, const char __user *ubuf, size_t count,
					  loff_t *ppos)
{
	struct max77759_plat *chip = file->private_data;
	long result, ret;

	ret = kstrtol_from_user(ubuf, count, 10, &result);
	if (ret)
		return ret;

	if (result == chip->force_device_mode_on)
		return count;

	mutex_lock(&chip->data_path_lock);
	chip->force_device_mode_on = result;
	/* Tear down previous data role if needed */
	if (((result && chip->active_data_role != TYPEC_DEVICE) ||
	    (!result && chip->active_data_role != chip->data_role)) && chip->data_active) {
		ret = extcon_set_state_sync(chip->extcon,
					    chip->active_data_role == TYPEC_HOST ?
					    EXTCON_USB_HOST : EXTCON_USB, 0);

		LOG(LOG_LVL_DEBUG, chip->log, "%s: %s turning off %s",
		    __func__, ret < 0 ? "Failed" : "Succeeded",
		    chip->active_data_role == TYPEC_HOST ? "Host" : "Device");
		chip->data_active = false;
		if (data_active_callback)
			(*data_active_callback)(data_active_payload);
	}

	if (result && !chip->data_active) {
		ret = extcon_set_state_sync(chip->extcon, EXTCON_USB, 1);
		LOG(LOG_LVL_DEBUG, chip->log,
		    "%s: %s turning on device", __func__, ret < 0 ? "Failed" : "Succeeded");
		chip->data_active = !ret;
		if (data_active_callback)
			(*data_active_callback)(data_active_payload);
		chip->active_data_role = TYPEC_DEVICE;

	} else if (!result) {
		enable_data_path_locked(chip);
	}

	mutex_unlock(&chip->data_path_lock);
	return count;
}

static ssize_t force_device_mode_on_read(struct file *file, char __user *userbuf, size_t count,
					 loff_t *ppos)
{
	struct max77759_plat *chip = file->private_data;
	char buf[16];
	int ret;

	ret = snprintf(buf, sizeof(buf) - 1, "%d\n", chip->force_device_mode_on);

	return simple_read_from_buffer(userbuf, count, ppos, buf, ret);
}

static const struct file_operations force_device_mode_on_fops = {
	.read	= force_device_mode_on_read,
	.write	= force_device_mode_on_write,
	.open	= simple_open,
	.llseek = default_llseek,
};
#endif

static void max77759_typec_tcpci_override_toggling(void *unused, struct tcpci *tcpci,
						   struct tcpci_data *data,
						   int *override_toggling)
{
	*override_toggling = 1;
}

static void max77759_get_timer_value(void *unused, const char *state, enum typec_timer timer,
				     unsigned int *val)
{
	switch (timer) {
	case SINK_DISCOVERY_BC12:
		*val = sink_discovery_delay_ms;
		break;
	case SINK_WAIT_CAP:
		*val = 450;
		break;
	case SOURCE_OFF:
		*val = 870;
		break;
	case CC_DEBOUNCE:
		*val = 170;
		break;
	default:
		break;
	}
}

static void max77759_tcpm_log(void *unused, const char *log, bool *bypass)
{
	if (tcpm_log)
		LOG(LOG_LVL_DEBUG, tcpm_log, "%s", log);

	*bypass = true;
}

static void max77759_modify_src_caps(void *unused, unsigned int *nr_src_pdo,
				     u32 (*src_pdo)[PDO_MAX_OBJECTS], bool *modified)
{
	spin_lock(&g_caps_lock);

	if (port_src_pdo_updated) {
		spin_unlock(&g_caps_lock);
		return;
	}

	if (limit_src_cap_enable) {
		(*src_pdo)[0] &= ~(PDO_CURR_MASK << PDO_FIXED_CURR_SHIFT);
		(*src_pdo)[0] |= PDO_FIXED_CURR(SRC_CURRENT_LIMIT_MA);
		*nr_src_pdo = 1;
	} else {
		(*src_pdo)[0] |= PDO_FIXED_CURR(orig_src_current);
		*nr_src_pdo = nr_orig_src_pdo;
	}

	port_src_pdo_updated = true;
	*modified = true;

	spin_unlock(&g_caps_lock);
}

static int max77759_register_vendor_hooks(struct i2c_client *client)
{
	int ret;

	if (hooks_installed)
		return 0;

	ret = register_trace_android_vh_typec_tcpci_override_toggling(
			max77759_typec_tcpci_override_toggling, NULL);

	if (ret) {
		dev_err(&client->dev,
			"register_trace_android_vh_typec_tcpci_override_toggling failed ret:%d",
			ret);
		return ret;
	}

	ret = register_trace_android_rvh_typec_tcpci_get_vbus(max77759_get_vbus, NULL);
	if (ret) {
		dev_err(&client->dev,
			"register_trace_android_rvh_typec_tcpci_get_vbus failed ret:%d\n", ret);
		return ret;
	}

	ret = register_trace_android_vh_typec_store_partner_src_caps(
			max77759_store_partner_src_caps, NULL);
	if (ret) {
		dev_err(&client->dev,
			"register_trace_android_vh_typec_store_partner_src_caps failed ret:%d\n",
			ret);
		return ret;
	}

	ret = register_trace_android_vh_typec_tcpm_get_timer(max77759_get_timer_value, NULL);
	if (ret) {
		dev_err(&client->dev,
			"register_trace_android_vh_typec_tcpm_get_timer failed ret:%d\n", ret);
		return ret;
	}

	ret = register_trace_android_vh_typec_tcpm_log(max77759_tcpm_log, NULL);
	if (ret) {
		dev_err(&client->dev,
			"register_trace_android_vh_typec_tcpm_log failed ret:%d\n", ret);
		return ret;
	}

	port_src_pdo_updated = true;
	ret = register_trace_android_vh_typec_tcpm_modify_src_caps(max77759_modify_src_caps, NULL);
	if (ret) {
		dev_err(&client->dev,
			"register_trace_android_vh_typec_tcpm_modify_src_caps failed ret:%d\n",
			ret);
		return ret;
	}

	hooks_installed = true;

	return ret;
}

static void reenable_auto_ultra_low_power_mode_work_item(struct kthread_work *work)
{
	struct max77759_plat *chip = container_of(work, struct max77759_plat,
						  reenable_auto_ultra_low_power_mode_work);

	chip->floating_cable_or_sink_detected = 0;
	disable_auto_ultra_low_power_mode(chip, false);
}

static enum alarmtimer_restart reenable_auto_ultra_low_power_mode_alarm_handler(struct alarm *alarm,
										ktime_t time)
{
	struct max77759_plat *chip = container_of(alarm, struct max77759_plat,
						  reenable_auto_ultra_low_power_mode_alarm);

	LOG(LOG_LVL_DEBUG, chip->log, "timer fired: enable_auto_ultra_low_power_mode");
	if (is_contaminant_detected(chip)) {
		LOG(LOG_LVL_DEBUG, chip->log,
		    "Skipping enable_auto_ultra_low_power_mode. Dry detection in progress");
		goto exit;
	}
	kthread_queue_work(chip->wq, &chip->reenable_auto_ultra_low_power_mode_work);
	pm_wakeup_event(chip->dev, PD_ACTIVITY_TIMEOUT_MS);

exit:
	return ALARMTIMER_NORESTART;
}

static void max_tcpci_check_contaminant(struct tcpci *tcpci, struct tcpci_data *tdata)
{
	struct max77759_plat *chip = tdata_to_max77759(tdata);
	bool contaminant_cc_status_handled = false, port_clean = false;
	int ret = 0;

	mutex_lock(&chip->rc_lock);
	LOG(LOG_LVL_DEBUG, chip->log, "max_tcpci_check_contaminant");
	if (chip->usb_throttled) {
		LOG(LOG_LVL_DEBUG, chip->log, "usb throttled; port clean");
		tcpm_port_clean(chip->port);
		mutex_unlock(&chip->rc_lock);
		return;
	}
	if (chip->contaminant_detection) {
		ret = process_contaminant_alert(chip->contaminant, true, false,
						&contaminant_cc_status_handled,
						&port_clean);
		if (ret < 0) {
			logbuffer_logk(chip->log, LOGLEVEL_ERR, "I/O error in %s", __func__);
			/* Assume clean port */
			tcpm_port_clean(chip->port);
		} else if (port_clean) {
			LOG(LOG_LVL_DEBUG, chip->log, "port clean");
			tcpm_port_clean(chip->port);
		} else {
			LOG(LOG_LVL_DEBUG, chip->log, "port dirty");
			chip->check_contaminant = true;
		}
	} else {
		LOG(LOG_LVL_DEBUG, chip->log, "port clean; Contaminant detection not enabled");
		tcpm_port_clean(chip->port);
	}
	mutex_unlock(&chip->rc_lock);
}

static void dp_notification_work_item(struct kthread_work *work)
{
	struct dp_notification_event *evt = container_of(work, struct dp_notification_event,
							 dp_notification_work);
	struct max77759_plat *chip = evt->chip;
	int dp, ret;

	logbuffer_logk(chip->log, LOGLEVEL_INFO, "dp wq %s: %lu", __func__, evt->mode);

	switch (evt->mode) {
	case TYPEC_DP_STATE_A:
	case TYPEC_DP_STATE_C:
	case TYPEC_DP_STATE_E:
		dp = 1;
		chip->lanes = 4;
		if (chip->sbu_mux_en_gpio >= 0)
			gpio_set_value_cansleep(chip->sbu_mux_en_gpio, 1);
		gpio_set_value_cansleep(chip->sbu_mux_sel_gpio,
					chip->orientation == TYPEC_ORIENTATION_NORMAL ?
					0 : 1);
		break;
	case TYPEC_DP_STATE_B:
	case TYPEC_DP_STATE_D:
	case TYPEC_DP_STATE_F:
		dp = 1;
		chip->lanes = 2;
		if (chip->sbu_mux_en_gpio >= 0)
			gpio_set_value_cansleep(chip->sbu_mux_en_gpio, 1);
		gpio_set_value_cansleep(chip->sbu_mux_sel_gpio,
					chip->orientation == TYPEC_ORIENTATION_NORMAL ?
					0 : 1);
		break;
	default:
		dp = 0;
	}

	if ((dp && !chip->dp_regulator_enabled) || (!dp && chip->dp_regulator_enabled)) {
		ret = dp ? regulator_enable(chip->dp_regulator) : \
			   regulator_disable(chip->dp_regulator);
		if (ret >= 0)
			chip->dp_regulator_enabled = dp;
		LOG(LOG_LVL_DEBUG, chip->log, "dp regulator_%s %s ret:%d",
		    dp ? "enable" : "disable", ret < 0 ? "fail" : "success", ret);
		ret = dp ? regulator_set_voltage(chip->dp_regulator, VOLTAGE_DP_AUX_DEFAULT_UV,
						 VOLTAGE_DP_AUX_DEFAULT_UV) : \
			   regulator_set_voltage(chip->dp_regulator, chip->dp_regulator_min_uv,
						 chip->dp_regulator_max_uv);
		LOG(LOG_LVL_DEBUG, chip->log,
		    "dp regulator_set_voltage %s ret:%d", ret < 0 ? "fail" : "success", ret);
	}

	ret = max77759_write8(chip->data.regmap, TCPC_VENDOR_SBUSW_CTRL, dp ? SBUSW_PATH_1 : 0);
	LOG(LOG_LVL_DEBUG, chip->log, "SBU dp switch %s %s ret:%d", dp ? "enable" : "disable",
	    ret < 0 ? "fail" : "success", ret);

	LOG(LOG_LVL_DEBUG, chip->log, "%s Signaling dp altmode: %s ret:%d",
	    ret < 0 ? "Failed" : "Succeeded", dp ? "on" : "off", ret);
	logbuffer_logk(chip->log, LOGLEVEL_INFO, "dp altmode orientation:%d lanes:%d dp:%d",
		      (int)chip->orientation, chip->lanes, dp);

	devm_kfree(chip->dev, evt);
}

static int max77759_usb_set_mode(struct typec_mux *mux, struct typec_mux_state *state)
{
	struct max77759_plat *chip = typec_mux_get_drvdata(mux);
	struct dp_notification_event *evt;

	evt = devm_kzalloc(chip->dev, sizeof(*evt), GFP_KERNEL);
	if (!evt) {
		LOG(LOG_LVL_DEBUG, chip->log, "dp notification: Dropping event");
		return 0;
	}
	kthread_init_work(&evt->dp_notification_work, dp_notification_work_item);
	evt->chip = chip;
	evt->mode = state->mode;
	kthread_queue_work(chip->dp_notification_wq, &evt->dp_notification_work);
	pm_wakeup_event(chip->dev, PD_ACTIVITY_TIMEOUT_MS);
	return 0;
}

static int max77759_setup_data_notifier(struct max77759_plat *chip)
{
	struct usb_role_switch_desc desc = { };
	struct typec_switch_desc sw_desc = { };
	struct typec_mux_desc mux_desc = {};
	u32 conn_handle;
	int ret;

	chip->extcon = devm_extcon_dev_allocate(chip->dev, usbpd_extcon_cable);
	if (IS_ERR(chip->extcon)) {
		dev_err(chip->dev, "Error allocating extcon: %ld\n",
			PTR_ERR(chip->extcon));
		return PTR_ERR(chip->extcon);
	}

	ret = devm_extcon_dev_register(chip->dev, chip->extcon);
	if (ret < 0) {
		dev_err(chip->dev, "failed to register extcon device:%d\n", ret);
		return ret;
	}

	extcon_set_property_capability(chip->extcon, EXTCON_USB,
				       EXTCON_PROP_USB_TYPEC_POLARITY);
	extcon_set_property_capability(chip->extcon, EXTCON_USB_HOST,
				       EXTCON_PROP_USB_TYPEC_POLARITY);

	of_property_read_u32(dev_of_node(chip->dev), "conn", &conn_handle);
	desc.fwnode = &of_find_node_by_phandle(conn_handle)->fwnode;
	desc.driver_data = chip;
	desc.name = fwnode_get_name(dev_fwnode(chip->dev));
	desc.set = max77759_usb_set_role;

	chip->usb_sw = usb_role_switch_register(chip->dev, &desc);
	if (IS_ERR(chip->usb_sw)) {
		ret = PTR_ERR(chip->usb_sw);
		dev_err(chip->dev, "Error while registering role switch:%d\n", ret);
		return ret;
	}

	sw_desc.fwnode = dev_fwnode(chip->dev);
	sw_desc.drvdata = chip;
	sw_desc.name = fwnode_get_name(dev_fwnode(chip->dev));
	sw_desc.set = max77759_usb_set_orientation;

	chip->typec_sw = typec_switch_register(chip->dev, &sw_desc);
	if (IS_ERR(chip->typec_sw)) {
		ret = PTR_ERR(chip->typec_sw);
		dev_err(chip->dev, "Error while registering orientation switch:%d\n", ret);
		goto usb_sw_free;
	}

	mux_desc.fwnode = dev_fwnode(chip->dev);
	mux_desc.drvdata = chip;
	mux_desc.name = fwnode_get_name(dev_fwnode(chip->dev));
	mux_desc.set = max77759_usb_set_mode;

	chip->mode_mux = typec_mux_register(chip->dev, &mux_desc);
	if (IS_ERR(chip->mode_mux)) {
		ret = PTR_ERR(chip->mode_mux);
		dev_err(chip->dev, "Error while registering mode mux:%d\n", ret);
		goto usb_sw_free;
	}

	return 0;

usb_sw_free:
	usb_role_switch_unregister(chip->usb_sw);
	return ret;
}

static void max77759_teardown_data_notifier(struct max77759_plat *chip)
{
	if (!IS_ERR_OR_NULL(chip->typec_sw))
		typec_switch_unregister(chip->typec_sw);
	if (!IS_ERR_OR_NULL(chip->usb_sw))
		usb_role_switch_unregister(chip->usb_sw);
}

static bool is_aicl_limited(struct max77759_plat *chip)
{
	unsigned int vbus_present, snk_vbus, pwr_status;
	union power_supply_propval current_now = {0};
	int ret;
	bool default_power, is_dcp;

	ret = regmap_read(chip->data.regmap, TCPC_POWER_STATUS, &pwr_status);
	if (ret < 0) {
		LOG(LOG_LVL_DEBUG, chip->log, "Abort %s; TCPC_POWER_STATUS read error", __func__);
		return false;
	}

	vbus_present = pwr_status & TCPC_POWER_STATUS_VBUS_PRES;
	snk_vbus = pwr_status & TCPC_POWER_STATUS_SINKING_VBUS;
	power_supply_get_property(chip->usb_psy, POWER_SUPPLY_PROP_CURRENT_NOW, &current_now);
	default_power = !(chip->cc1 == TYPEC_CC_RP_3_0 || chip->cc1 == TYPEC_CC_RP_1_5 ||
			  chip->cc2 == TYPEC_CC_RP_3_0 || chip->cc2 == TYPEC_CC_RP_1_5);
	is_dcp = (get_usb_type(chip->bc12) == POWER_SUPPLY_USB_TYPE_DCP);

	LOG(LOG_LVL_DEBUG, chip->log,
	    "AICL %s active vbus_present:%c snk_vbus:%c current_now:%d default_power:%c DCP:%c",
	    chip->aicl_active ? "" : "not", vbus_present ? 'y' : 'n', snk_vbus ? 'y' : 'n',
	    current_now.intval, default_power ? 'y' : 'n', is_dcp ? 'y' : 'n');
	/*
	 * AICL_ACTIVE + Charging over USB + USB input current less than 500mA and charging from
	 * default power sources.
	 *
	 * USB input current could be reported as 0 in scenarios such as charge full.
	 * Exclude these cases as input current should not be 0 esp. when input current is limited.
	 */
	if (!current_now.intval)
		return false;
	else if (chip->aicl_active && vbus_present && snk_vbus && current_now.intval < 500000 &&
		 default_power && is_dcp)
		return true;

	return false;
}

static void aicl_check_alarm_work_item(struct kthread_work *work)
{
	struct max77759_plat *chip = container_of(work, struct max77759_plat,
						  aicl_check_alarm_work);

	/*
	 * Set here and clear COMPLIANCE_WARNING_INPUT_POWER_LIMITED which tracks AICL_ACTIVE only
	 * upon disconnect. This prevents the incommpatible charging notification to not change
	 * status during the charging session. AICL active is system/battery load dependent and
	 * hence can change status during a charge session.
	 */
	if (is_aicl_limited(chip))
		update_compliance_warnings(chip, COMPLIANCE_WARNING_INPUT_POWER_LIMITED, true);
}

static enum alarmtimer_restart aicl_check_alarm_handler(struct alarm *alarm, ktime_t time)
{
	struct max77759_plat *chip = container_of(alarm, struct max77759_plat, aicl_check_alarm);

	LOG(LOG_LVL_DEBUG, chip->log, "timer fired: %s", __func__);
	kthread_queue_work(chip->wq, &chip->aicl_check_alarm_work);
	pm_wakeup_event(chip->dev, AICL_CHECK_MS);

	return ALARMTIMER_NORESTART;
}

static int max77759_aicl_active_cb(struct gvotable_election *el, const char *reason, void *value)
{
	struct max77759_plat *chip = gvotable_get_data(el);
	bool aicl_active = !!(long)value;

	chip->aicl_active = aicl_active;

	if (is_aicl_limited(chip)) {
		/* Recheck after AICL_CHECK_MS */
		alarm_start_relative(&chip->aicl_check_alarm, ms_to_ktime(AICL_CHECK_MS));
	} else {
		alarm_cancel(&chip->aicl_check_alarm);
		kthread_cancel_work_sync(&chip->aicl_check_alarm_work);
	}

	return 0;
}

static int max77759_probe(struct i2c_client *client,
			  const struct i2c_device_id *i2c_id)
{
	int ret, i;
	struct max77759_plat *chip;
	char *usb_psy_name;
	struct device_node *dn, *ovp_dn, *regulator_dn, *conn;
	u8 power_status;
	u16 device_id;
	u32 ovp_handle, regulator_handle;
	const char *ovp_status;
	enum of_gpio_flags flags;
	u32 first_src_pdo = 0;

	ret = max77759_register_vendor_hooks(client);
	if (ret)
		return ret;

	chip = devm_kzalloc(&client->dev, sizeof(*chip), GFP_KERNEL);
	if (!chip)
		return -ENOMEM;

	chip->client = client;
	chip->data.regmap = devm_regmap_init_i2c(client,
						 &max77759_regmap_config);
	if (IS_ERR(chip->data.regmap)) {
		dev_err(&client->dev, "Regmap init failed\n");
		return PTR_ERR(chip->data.regmap);
	}

	chip->charger_mode_votable = gvotable_election_get_handle(GBMS_MODE_VOTABLE);
	if (IS_ERR_OR_NULL(chip->charger_mode_votable)) {
		dev_err(&client->dev, "TCPCI: GBMS_MODE_VOTABLE get failed",
			PTR_ERR(chip->charger_mode_votable));
		if (!of_property_read_bool(dn, "gvotable-lazy-probe"))
			return -EPROBE_DEFER;
	}

	kthread_init_work(&chip->reenable_auto_ultra_low_power_mode_work,
			  reenable_auto_ultra_low_power_mode_work_item);
	alarm_init(&chip->reenable_auto_ultra_low_power_mode_alarm, ALARM_BOOTTIME,
		   reenable_auto_ultra_low_power_mode_alarm_handler);
	kthread_init_work(&chip->aicl_check_alarm_work, aicl_check_alarm_work_item);
	alarm_init(&chip->aicl_check_alarm, ALARM_BOOTTIME, aicl_check_alarm_handler);

	dn = dev_of_node(&client->dev);
	if (!dn) {
		dev_err(&client->dev, "of node not found\n");
		return -EINVAL;
	}

	chip->in_switch_gpio = -EINVAL;
	if (of_property_read_bool(dn, "ovp-present")) {
		chip->in_switch_gpio = of_get_named_gpio_flags(dn, "in-switch-gpio", 0, &flags);
		if (chip->in_switch_gpio < 0) {
			dev_err(&client->dev, "in-switch-gpio not found\n");
			return -EPROBE_DEFER;
		}
		chip->in_switch_gpio_active_high = (flags & OF_GPIO_ACTIVE_LOW) ? 0 : 1;
	} else if (!of_property_read_u32(dn, "max20339,ovp", &ovp_handle)) {
		ovp_dn = of_find_node_by_phandle(ovp_handle);
		if (!IS_ERR_OR_NULL(ovp_dn) &&
		    !of_property_read_string(ovp_dn, "status", &ovp_status) &&
		    strncmp(ovp_status, "disabled", strlen("disabled"))) {
			chip->in_switch_gpio = of_get_named_gpio_flags(dn, "in-switch-gpio", 0,
								       &flags);
			if (chip->in_switch_gpio < 0) {
				dev_err(&client->dev, "in-switch-gpio not found\n");
				return -EPROBE_DEFER;
			}
			chip->in_switch_gpio_active_high = (flags & OF_GPIO_ACTIVE_LOW) ? 0 : 1;
		}
	}

	chip->sbu_mux_en_gpio = of_get_named_gpio_flags(dn, "sbu-mux-en-gpio", 0, &flags);
	if (chip->sbu_mux_en_gpio < 0) {
		dev_err(&client->dev, "sbu-mux-en-gpio not found\n");
	}
	chip->sbu_mux_sel_gpio = of_get_named_gpio_flags(dn, "sbu-mux-sel-gpio", 0, &flags);
	if (chip->sbu_mux_sel_gpio < 0) {
		dev_err(&client->dev, "sbu-mux-sel-gpio not found\n");
	}
	chip->dev = &client->dev;
	i2c_set_clientdata(client, chip);
	mutex_init(&chip->icl_proto_el_lock);
	mutex_init(&chip->data_path_lock);
	mutex_init(&chip->rc_lock);
	mutex_init(&chip->irq_status_lock);
	mutex_init(&chip->toggle_lock);
	mutex_init(&chip->ovp_lock);
	spin_lock_init(&g_caps_lock);
	chip->first_toggle = true;
	chip->first_rp_missing_timeout = true;

	ret = max77759_read8(chip->data.regmap, TCPC_POWER_STATUS,
			     &power_status);
	if (ret < 0)
		return ret;

	if (power_status & TCPC_POWER_STATUS_UNINIT) {
		dev_err(&client->dev, "TCPC not ready!");
		return -EPROBE_DEFER;
	}

	chip->toggle_disable_votable =
		gvotable_create_bool_election(NULL, max77759_toggle_disable_votable_callback, chip);
	if (IS_ERR_OR_NULL(chip->toggle_disable_votable)) {
		ret = PTR_ERR(chip->toggle_disable_votable);
		dev_err(chip->dev, "no toggle_disable votable (%d)\n", ret);
		return ret;
	}
	gvotable_set_vote2str(chip->toggle_disable_votable, gvotable_v2s_int);
	gvotable_election_set_name(chip->toggle_disable_votable, "TOGGLE_DISABLE");

	chip->usb_throttle_votable =
		gvotable_create_bool_election(NULL, usb_throttle_votable_callback, chip);
	if (IS_ERR_OR_NULL(chip->usb_throttle_votable)) {
		ret = PTR_ERR(chip->usb_throttle_votable);
		dev_err(chip->dev, "USB throttle votable (%d) failed to create\n", ret);
		return ret;
	}
	gvotable_set_vote2str(chip->usb_throttle_votable, gvotable_v2s_int);
	gvotable_election_set_name(chip->usb_throttle_votable, USB_THROTTLE_VOTABLE);

	/* Chip level tcpci callbacks */
	chip->data.set_vbus = max77759_set_vbus;
	chip->data.start_drp_toggling = max77759_start_toggling;
	chip->data.TX_BUF_BYTE_x_hidden = 1;
	chip->data.vbus_vsafe0v = true;
	chip->data.set_partner_usb_comm_capable = max77759_set_partner_usb_comm_capable;
	chip->data.init = tcpci_init;
	chip->data.frs_sourcing_vbus = max77759_frs_sourcing_vbus;
	chip->data.check_contaminant = max_tcpci_check_contaminant;

	chip->compliance_warnings = init_compliance_warnings(chip);
	if (IS_ERR_OR_NULL(chip->compliance_warnings)) {
		ret = PTR_ERR(chip->compliance_warnings);
		dev_err(&client->dev, "init_compliance_warnings failed, ptr: %ld", ret);
		return ret;
	}

	chip->log = logbuffer_register("usbpd");
	if (IS_ERR_OR_NULL(chip->log)) {
		dev_err(&client->dev, "logbuffer get failed");
		chip->log = NULL;
	}

	chip->psy_ops.tcpc_get_vbus_voltage_mv =
		max77759_get_vbus_voltage_mv;
	chip->psy_ops.tcpc_get_vbus_voltage_max_mv =
		max77759_get_vbus_voltage_max_mv;
	chip->psy_ops.tcpc_set_vbus_voltage_max_mv =
		max77759_set_vbus_voltage_max_mv;
	chip->psy_ops.tcpc_set_port_data_capable =
		max77759_set_port_data_capable;
	chip->usb_psy_data = usb_psy_setup(client, chip->log, &chip->psy_ops, chip,
					   &max77759_non_compliant_bc12_callback);
	if (IS_ERR_OR_NULL(chip->usb_psy_data)) {
		dev_err(&client->dev, "USB psy failed to initialize");
		ret = PTR_ERR(chip->usb_psy_data);
		goto logbuffer_unreg;
	}

	/* Defered probe returned until usb power supply showup.*/
	chip->bc12 = bc12_init(chip, max77759_bc12_is_running);
	if (IS_ERR_OR_NULL(chip->bc12)) {
		ret = PTR_ERR(chip->bc12);
		goto unreg_psy;
	}

	usb_psy_name = (char *)of_get_property(dn, "usb-psy-name", NULL);
	if (!usb_psy_name) {
		dev_err(&client->dev, "usb-psy-name not set\n");
		ret = -EINVAL;
		goto teardown_bc12;
	}

	chip->no_bc_12 = of_property_read_bool(dn, "no-bc-12");
	chip->no_external_boost = of_property_read_bool(dn, "no-external-boost");
	of_property_read_u32(dn, "sink-discovery-delay-ms", &sink_discovery_delay_ms);

	conn = of_get_child_by_name(dn, "connector");
	if (!conn) {
		dev_err(&client->dev, "connector node not present\n");
		ret = -ENODEV;
		goto teardown_bc12;
	}

	/* DRP is expected and "source-pdos" should be present in device tree */
	nr_orig_src_pdo = of_property_count_u32_elems(conn, "source-pdos");
	if (nr_orig_src_pdo < 0) {
		dev_err(&client->dev, "failed to count elems in source-pdos\n");
		of_node_put(conn);
		ret = nr_orig_src_pdo;
		goto teardown_bc12;
	}

	ret = of_property_read_u32_index(conn, "source-pdos", 0, &first_src_pdo);
	of_node_put(conn);
	if (ret < 0) {
		dev_err(&client->dev, "failed to read the first source-pdo\n");
		goto teardown_bc12;
	}
	orig_src_current = ((first_src_pdo >> PDO_FIXED_CURR_SHIFT) & PDO_CURR_MASK) * 10;

	chip->usb_psy = power_supply_get_by_name(usb_psy_name);
	if (IS_ERR_OR_NULL(chip->usb_psy) || !chip->usb_psy) {
		dev_err(&client->dev, "usb psy not up\n");
		ret = -EPROBE_DEFER;
		goto teardown_bc12;
	}

	chip->dp_regulator = devm_regulator_get(chip->dev, "pullup");
	if (IS_ERR_OR_NULL(chip->dp_regulator) ) {
		dev_err(&client->dev, "devm_regulator_get failed\n");
		goto psy_put;
	}
	if (!of_property_read_u32(dn, "pullup-supply", &regulator_handle)) {
		regulator_dn = of_find_node_by_phandle(regulator_handle);
		if (!IS_ERR_OR_NULL(regulator_dn)) {
			if (of_property_read_u32(regulator_dn, "regulator-min-microvolt",
						 &chip->dp_regulator_min_uv)) {
				dev_err(&client->dev, "failed to read regulator-min-microvolt\n");
				goto psy_put;
			}
			if (of_property_read_u32(regulator_dn, "regulator-max-microvolt",
						 &chip->dp_regulator_max_uv)) {
				dev_err(&client->dev, "failed to read regulator-max-microvolt\n");
				goto psy_put;
			}
		}
	}

	ret = max77759_read16(chip->data.regmap, TCPC_BCD_DEV, &device_id);
	if (ret < 0)
		goto dp_regulator_put;

	LOG(LOG_LVL_DEBUG, chip->log, "TCPC DEVICE id:%d", device_id);
	/* Default enable on A1 or higher */
	chip->contaminant_detection = device_id >= MAX77759_DEVICE_ID_A1;
	chip->contaminant_detection_userspace = chip->contaminant_detection;
	if (chip->contaminant_detection) {
		LOG(LOG_LVL_DEBUG, chip->log, "Contaminant detection enabled");
		chip->data.check_contaminant = max_tcpci_check_contaminant;
		chip->contaminant = max77759_contaminant_init(chip, chip->contaminant_detection);
	}

	ret = max77759_setup_data_notifier(chip);
	if (ret < 0)
		goto dp_regulator_put;
	max77759_init_regs(chip->data.regmap, chip->log);

	/* Default enable on A1 or higher */
	if (device_id >= MAX77759_DEVICE_ID_A1) {
		chip->data.auto_discharge_disconnect = true;
		chip->frs = true;
	}

	chip->wq = kthread_create_worker(0, "wq-tcpm-tcpc");
	if (IS_ERR_OR_NULL(chip->wq)) {
		ret = PTR_ERR(chip->wq);
		goto teardown_data;
	}

	chip->dp_notification_wq = kthread_create_worker(0, "wq-tcpc-dp-notification");
	if (IS_ERR_OR_NULL(chip->dp_notification_wq)) {
		ret = PTR_ERR(chip->dp_notification_wq);
		goto destroy_worker;
	}

	kthread_init_delayed_work(&chip->icl_work, icl_work_item);
	kthread_init_delayed_work(&chip->enable_vbus_work, enable_vbus_work);
	kthread_init_delayed_work(&chip->vsafe0v_work, vsafe0v_debounce_work);
	kthread_init_delayed_work(&chip->max77759_io_error_work, max77759_io_error_work);
	kthread_init_delayed_work(&chip->check_missing_rp_work, check_missing_rp_work);

	/*
	 * b/218797880 Some OVP chips are restricted to quick Vin ramp-up time which means that if
	 * the ramp-up time is longer than a certain value, the OVP will keep being disabled if the
	 * status of the ON pin has been already set to active.
	 */
	chip->quick_ramp_vbus_ovp = of_property_read_bool(dn, "quick-ramp-vbus-ovp");
	if (chip->quick_ramp_vbus_ovp)
		kthread_init_delayed_work(&chip->reset_ovp_work, reset_ovp_work);

	chip->psy_notifier.notifier_call = psy_changed;
	ret = power_supply_reg_notifier(&chip->psy_notifier);
	if (ret < 0) {
		dev_err(&client->dev, "failed to register power supply callback\n");
		goto destroy_dp_worker;
	}

	chip->usb_icl_proto_el = gvotable_election_get_handle(USB_ICL_PROTO_EL);
	if (IS_ERR_OR_NULL(chip->usb_icl_proto_el)) {
		dev_err(&client->dev, "TCPCI: USB ICL PROTO EL get failed:%ld",
			PTR_ERR(chip->usb_icl_proto_el));
		ret = -ENODEV;
		goto unreg_notifier;
	}

	chip->usb_icl_el = gvotable_election_get_handle(USB_ICL_EL);
	if (IS_ERR_OR_NULL(chip->usb_icl_el)) {
		dev_err(&client->dev, "TCPCI: USB ICL EL get failed:%ld",
			PTR_ERR(chip->usb_icl_el));
		ret = -ENODEV;
		goto unreg_notifier;
	}

	chip->aicl_active_el =
		gvotable_create_bool_election(AICL_ACTIVE_EL, max77759_aicl_active_cb, chip);
	if (IS_ERR_OR_NULL(chip->aicl_active_el)) {
		ret = PTR_ERR(chip->aicl_active_el);
		dev_err(chip->dev, "Unable to create aicl_active_el(%d)\n", ret);
		goto unreg_notifier;
	}
	gvotable_set_vote2str(chip->aicl_active_el, gvotable_v2s_int);

	mutex_lock(&chip->toggle_lock);
	chip->tcpci = tcpci_register_port(chip->dev, &chip->data);
	mutex_unlock(&chip->toggle_lock);
	if (IS_ERR_OR_NULL(chip->tcpci)) {
		dev_err(&client->dev, "TCPCI port registration failed");
		ret = PTR_ERR(chip->tcpci);
		goto unreg_aicl_el;
	}
	chip->port = tcpci_get_tcpm_port(chip->tcpci);

	max77759_enable_voltage_alarm(chip, true, true);

	ret = max77759_init_alert(chip, client);
	if (ret < 0)
		goto unreg_port;

	device_init_wakeup(chip->dev, true);

	for (i = 0; max77759_device_attrs[i]; i++) {
		ret = device_create_file(&client->dev, max77759_device_attrs[i]);
		if (ret < 0)
			dev_err(&client->dev, "TCPCI: Unable to create device attr[%d] ret:%d:", i,
				ret);
	}

	if (!modparam_conf_sbu) {
		ret = max77759_write8(chip->data.regmap, TCPC_VENDOR_SBUSW_CTRL, 0);
		LOG(LOG_LVL_DEBUG, chip->log,
		    "SBU switch disable %s", ret < 0 ? "fail" : "success");
	}

#ifdef CONFIG_DEBUG_FS
	chip->dentry = debugfs_create_dir("tcpci_max77759", NULL);
	if (IS_ERR(chip->dentry)) {
		dev_err(&client->dev, "TCPCI: debugfs dentry failed: %ld", PTR_ERR(chip->dentry));
	} else {
		debugfs_create_file("force_device_mode_on", 0644, chip->dentry, chip,
				    &force_device_mode_on_fops);
	}
#endif

#ifdef CONFIG_GPIOLIB
	ret = ext_bst_en_gpio_init(chip);
	if (ret)
		goto remove_files;
#endif
	return 0;

remove_files:
#ifdef CONFIG_DEBUG_FS
	debugfs_remove_recursive(chip->dentry);
#endif
	for (i = 0; max77759_device_attrs[i]; i++)
		device_remove_file(&client->dev, max77759_device_attrs[i]);
unreg_port:
	tcpci_unregister_port(chip->tcpci);
unreg_aicl_el:
	gvotable_destroy_election(chip->aicl_active_el);
unreg_notifier:
	power_supply_unreg_notifier(&chip->psy_notifier);
destroy_dp_worker:
	kthread_destroy_worker(chip->dp_notification_wq);
destroy_worker:
	kthread_destroy_worker(chip->wq);
teardown_data:
	max77759_teardown_data_notifier(chip);
dp_regulator_put:
	devm_regulator_put(chip->dp_regulator);
psy_put:
	power_supply_put(chip->usb_psy);
teardown_bc12:
	bc12_teardown(chip->bc12);
unreg_psy:
	usb_psy_teardown(chip->usb_psy_data);
logbuffer_unreg:
	logbuffer_unregister(chip->log);

	return ret;
}

static int max77759_remove(struct i2c_client *client)
{
	struct max77759_plat *chip = i2c_get_clientdata(client);
	int i;

#ifdef CONFIG_DEBUG_FS
	debugfs_remove_recursive(chip->dentry);
#endif
	for (i = 0; max77759_device_attrs[i]; i++)
		device_remove_file(&client->dev, max77759_device_attrs[i]);
	if (!IS_ERR_OR_NULL(chip->tcpci))
		tcpci_unregister_port(chip->tcpci);
	if (!IS_ERR_OR_NULL(chip->dp_regulator))
		devm_regulator_put(chip->dp_regulator);
	if (!IS_ERR_OR_NULL(chip->aicl_active_el))
		gvotable_destroy_election(chip->aicl_active_el);
	if (!IS_ERR_OR_NULL(chip->usb_psy))
		power_supply_put(chip->usb_psy);
	if (!IS_ERR_OR_NULL(chip->usb_psy_data))
		usb_psy_teardown(chip->usb_psy_data);
	if (!IS_ERR_OR_NULL(chip->bc12))
		bc12_teardown(chip->bc12);
	if (!IS_ERR_OR_NULL(chip->log))
		logbuffer_unregister(chip->log);
	if (!IS_ERR_OR_NULL(chip->dp_notification_wq))
		kthread_destroy_worker(chip->dp_notification_wq);
	if (!IS_ERR_OR_NULL(chip->wq))
		kthread_destroy_worker(chip->wq);
	power_supply_unreg_notifier(&chip->psy_notifier);
	max77759_teardown_data_notifier(chip);

	return 0;
}

static void max77759_shutdown(struct i2c_client *client)
{
	struct max77759_plat *chip = i2c_get_clientdata(client);
	int ret;

	dev_info(&client->dev, "disabling Type-C upon shutdown\n");
	kthread_cancel_delayed_work_sync(&chip->check_missing_rp_work);
	kthread_cancel_delayed_work_sync(&chip->icl_work);
	/* Set current limit to 0. Will eventually happen after hi-Z as well */
	max77759_vote_icl(chip, 0);
	/* Prevent re-enabling toggling */
	/* Hi-z CC pins to trigger disconnection */
	ret = gvotable_cast_vote(chip->toggle_disable_votable, "SHUTDOWN_VOTE",
				 (void *)MAX77759_DISABLE_TOGGLE_VOTE, MAX77759_DISABLE_TOGGLE);
	if (ret < 0)
		dev_err(chip->dev, "Cannot set TOGGLE DISABLE (%d)\n", ret);
}

static const struct i2c_device_id max77759_id[] = {
	{ "max77759tcpc", 0 },
	{ }
};
MODULE_DEVICE_TABLE(i2c, max77759_id);

#ifdef CONFIG_OF
static const struct of_device_id max77759_of_match[] = {
	{ .compatible = "max77759tcpc", },
	{},
};
MODULE_DEVICE_TABLE(of, max77759_of_match);
#endif

static struct i2c_driver max77759_i2c_driver = {
	.driver = {
		.name = "max77759tcpc",
		.of_match_table = of_match_ptr(max77759_of_match),
	},
	.probe = max77759_probe,
	.remove = max77759_remove,
	.id_table = max77759_id,
	.shutdown = max77759_shutdown,
};

static int __init max77759_i2c_driver_init(void)
{
	tcpm_log = logbuffer_register("tcpm");
	if (IS_ERR_OR_NULL(tcpm_log))
		return -EAGAIN;

	return i2c_add_driver(&max77759_i2c_driver);
}
module_init(max77759_i2c_driver_init);

static void __exit max77759_i2c_driver_exit(void)
{
	i2c_del_driver(&max77759_i2c_driver);
}
module_exit(max77759_i2c_driver_exit);

MODULE_AUTHOR("Badhri Jagan Sridharan <badhri@google.com>");
MODULE_DESCRIPTION("MAX77759 USB Type-C Port Controller Interface Driver");
MODULE_LICENSE("GPL");