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
3541
3542
3543
3544
3545
3546
3547
3548
3549
3550
3551
3552
3553
3554
3555
3556
3557
3558
3559
3560
3561
3562
3563
3564
3565
3566
3567
3568
3569
3570
3571
3572
3573
3574
3575
3576
3577
3578
3579
3580
3581
3582
3583
3584
3585
3586
3587
3588
3589
3590
3591
3592
3593
3594
3595
3596
3597
3598
3599
3600
3601
3602
3603
3604
3605
3606
3607
3608
3609
3610
3611
3612
3613
3614
3615
3616
3617
3618
3619
3620
3621
3622
3623
3624
3625
3626
3627
3628
3629
3630
3631
3632
3633
3634
3635
3636
3637
3638
3639
3640
3641
3642
3643
3644
3645
3646
3647
3648
3649
3650
3651
3652
3653
3654
3655
3656
3657
3658
3659
3660
3661
3662
3663
3664
3665
3666
3667
3668
3669
3670
3671
3672
3673
3674
3675
3676
3677
3678
3679
3680
3681
3682
3683
3684
3685
3686
3687
3688
3689
3690
3691
3692
3693
3694
3695
3696
3697
3698
3699
3700
3701
3702
3703
3704
3705
3706
3707
3708
3709
3710
3711
3712
3713
3714
3715
3716
3717
3718
3719
3720
3721
3722
3723
3724
3725
3726
3727
3728
3729
3730
3731
3732
3733
3734
3735
3736
3737
3738
3739
3740
3741
3742
3743
3744
3745
3746
3747
3748
3749
3750
3751
3752
3753
3754
3755
3756
3757
3758
3759
3760
3761
3762
3763
3764
3765
3766
3767
3768
3769
3770
3771
3772
3773
3774
3775
3776
3777
3778
3779
3780
3781
3782
3783
3784
3785
3786
3787
3788
3789
3790
3791
3792
3793
3794
3795
3796
3797
3798
3799
3800
3801
3802
3803
3804
3805
3806
3807
3808
3809
3810
3811
3812
3813
3814
3815
3816
3817
3818
3819
3820
3821
3822
3823
3824
3825
3826
3827
3828
3829
3830
3831
3832
3833
3834
3835
3836
3837
3838
3839
3840
3841
3842
3843
3844
3845
3846
3847
3848
3849
3850
3851
3852
3853
3854
3855
3856
3857
3858
3859
3860
3861
3862
3863
3864
3865
3866
3867
3868
3869
3870
3871
3872
3873
3874
3875
3876
3877
3878
3879
3880
3881
3882
3883
3884
3885
3886
3887
3888
3889
3890
3891
3892
3893
3894
3895
3896
3897
3898
3899
3900
3901
3902
3903
3904
3905
3906
3907
3908
3909
3910
3911
3912
3913
3914
3915
3916
3917
3918
3919
3920
3921
3922
3923
3924
3925
3926
3927
3928
3929
3930
3931
3932
3933
3934
3935
3936
3937
3938
3939
3940
3941
3942
3943
3944
3945
3946
3947
3948
3949
3950
3951
3952
3953
3954
3955
3956
3957
3958
3959
3960
3961
3962
3963
3964
3965
3966
3967
3968
3969
3970
3971
3972
3973
3974
3975
3976
3977
3978
3979
3980
3981
3982
3983
3984
3985
3986
3987
3988
3989
3990
3991
3992
3993
3994
3995
3996
3997
3998
3999
4000
4001
4002
4003
4004
4005
4006
4007
4008
4009
4010
4011
4012
4013
4014
4015
4016
4017
4018
4019
4020
4021
4022
4023
4024
4025
4026
4027
4028
4029
4030
4031
4032
4033
4034
4035
4036
4037
4038
4039
4040
4041
4042
4043
4044
4045
4046
4047
4048
4049
4050
4051
4052
4053
4054
4055
4056
4057
4058
4059
4060
4061
4062
4063
4064
4065
4066
4067
4068
4069
4070
4071
4072
4073
4074
4075
4076
4077
4078
4079
4080
4081
4082
4083
4084
4085
4086
4087
4088
4089
4090
4091
4092
4093
4094
4095
4096
4097
4098
4099
4100
4101
4102
4103
4104
4105
4106
4107
4108
4109
4110
4111
4112
4113
4114
4115
4116
4117
4118
4119
4120
4121
4122
4123
4124
4125
4126
4127
4128
4129
4130
4131
4132
4133
4134
4135
4136
4137
4138
4139
4140
4141
4142
4143
4144
4145
4146
4147
4148
4149
4150
4151
4152
4153
4154
4155
4156
4157
4158
4159
4160
4161
4162
4163
4164
4165
4166
4167
4168
4169
4170
4171
4172
4173
4174
4175
4176
4177
4178
4179
4180
4181
4182
4183
4184
4185
4186
4187
4188
4189
4190
4191
4192
4193
4194
4195
4196
4197
4198
4199
4200
4201
4202
4203
4204
4205
4206
4207
4208
4209
4210
4211
4212
4213
4214
4215
4216
4217
4218
4219
4220
4221
4222
4223
4224
4225
4226
4227
4228
4229
4230
4231
4232
4233
4234
4235
4236
4237
4238
4239
4240
4241
4242
4243
4244
4245
4246
4247
4248
4249
4250
4251
4252
4253
4254
4255
4256
4257
4258
4259
4260
4261
4262
4263
4264
4265
4266
4267
4268
4269
4270
4271
4272
4273
4274
4275
4276
4277
4278
4279
4280
4281
4282
4283
4284
4285
4286
4287
4288
4289
4290
4291
4292
4293
4294
4295
4296
4297
4298
4299
4300
4301
4302
4303
4304
4305
4306
4307
4308
4309
4310
4311
4312
4313
4314
4315
4316
4317
4318
4319
4320
4321
4322
4323
4324
4325
4326
4327
4328
4329
4330
4331
4332
4333
4334
4335
4336
4337
4338
4339
4340
4341
4342
4343
4344
4345
4346
4347
4348
4349
4350
4351
4352
4353
4354
4355
4356
4357
4358
4359
4360
4361
4362
4363
4364
4365
4366
4367
4368
4369
4370
4371
4372
4373
4374
4375
4376
4377
4378
4379
4380
4381
4382
4383
4384
4385
4386
4387
4388
4389
4390
4391
4392
4393
4394
4395
4396
4397
4398
4399
4400
4401
4402
4403
4404
4405
4406
4407
4408
4409
4410
4411
4412
4413
4414
4415
4416
4417
4418
4419
4420
4421
4422
4423
4424
4425
4426
4427
4428
4429
4430
4431
4432
4433
4434
4435
4436
4437
4438
4439
4440
4441
4442
4443
4444
4445
4446
4447
4448
4449
4450
4451
4452
4453
4454
4455
4456
4457
4458
4459
4460
4461
4462
4463
4464
4465
4466
4467
4468
4469
4470
4471
4472
4473
4474
4475
4476
4477
4478
4479
4480
4481
4482
4483
4484
4485
4486
4487
4488
4489
4490
4491
4492
4493
4494
4495
4496
4497
4498
4499
4500
4501
4502
4503
4504
4505
4506
4507
4508
4509
4510
4511
4512
4513
4514
4515
4516
4517
4518
4519
4520
4521
4522
4523
4524
4525
4526
4527
4528
4529
4530
4531
4532
4533
4534
4535
4536
4537
4538
4539
4540
4541
4542
4543
4544
4545
4546
4547
4548
4549
4550
4551
4552
4553
4554
4555
4556
4557
4558
4559
4560
4561
4562
4563
4564
4565
4566
4567
4568
4569
4570
4571
4572
4573
4574
4575
4576
4577
4578
4579
4580
4581
4582
4583
4584
4585
4586
4587
4588
4589
4590
4591
4592
4593
4594
4595
4596
4597
4598
4599
4600
4601
4602
4603
4604
4605
4606
4607
4608
4609
4610
4611
4612
4613
4614
4615
4616
4617
4618
4619
4620
4621
4622
4623
4624
4625
4626
4627
4628
4629
4630
4631
4632
4633
4634
4635
4636
4637
4638
4639
4640
4641
4642
4643
4644
4645
4646
4647
4648
4649
4650
4651
4652
4653
4654
4655
4656
4657
4658
4659
4660
4661
4662
4663
4664
4665
4666
4667
4668
4669
4670
4671
4672
4673
4674
4675
4676
4677
4678
4679
4680
4681
4682
4683
4684
4685
4686
4687
4688
4689
4690
4691
4692
4693
4694
4695
4696
4697
4698
4699
4700
4701
4702
4703
4704
4705
4706
4707
4708
4709
4710
4711
4712
4713
4714
4715
4716
4717
4718
4719
4720
4721
4722
4723
4724
4725
4726
4727
4728
4729
4730
4731
4732
4733
4734
4735
4736
4737
4738
4739
4740
4741
4742
4743
4744
4745
4746
4747
4748
4749
4750
4751
4752
4753
4754
4755
4756
4757
4758
4759
4760
4761
4762
4763
4764
4765
4766
4767
4768
4769
4770
4771
4772
4773
4774
4775
4776
4777
4778
4779
4780
4781
4782
4783
4784
4785
4786
4787
4788
4789
4790
4791
4792
4793
4794
4795
4796
4797
4798
4799
4800
4801
4802
4803
4804
4805
4806
4807
4808
4809
4810
4811
4812
4813
4814
4815
4816
4817
4818
4819
4820
4821
4822
4823
4824
4825
4826
4827
4828
4829
4830
4831
4832
4833
4834
4835
4836
4837
4838
4839
4840
4841
4842
4843
4844
4845
4846
4847
4848
4849
4850
4851
4852
4853
4854
4855
4856
4857
4858
4859
4860
4861
4862
4863
4864
4865
4866
4867
4868
4869
4870
4871
4872
4873
4874
4875
4876
4877
4878
4879
4880
4881
4882
4883
4884
4885
4886
4887
4888
4889
4890
4891
4892
4893
4894
4895
4896
4897
4898
4899
4900
4901
4902
4903
4904
4905
4906
4907
4908
4909
4910
4911
4912
4913
4914
4915
4916
4917
4918
4919
4920
4921
4922
4923
4924
4925
4926
4927
4928
4929
4930
4931
4932
4933
4934
4935
4936
4937
4938
4939
4940
4941
4942
4943
4944
4945
4946
4947
4948
4949
4950
4951
4952
4953
4954
4955
4956
4957
4958
4959
4960
4961
4962
4963
4964
4965
4966
4967
4968
4969
4970
4971
4972
4973
4974
4975
4976
4977
4978
4979
4980
4981
4982
4983
4984
4985
4986
4987
4988
4989
4990
4991
4992
4993
4994
4995
4996
4997
4998
4999
5000
5001
5002
5003
5004
5005
5006
5007
5008
5009
5010
5011
5012
5013
5014
5015
5016
5017
5018
5019
5020
5021
5022
5023
5024
5025
5026
5027
5028
5029
5030
5031
5032
5033
5034
5035
5036
5037
5038
5039
5040
5041
5042
5043
5044
5045
5046
5047
5048
5049
5050
5051
5052
5053
5054
5055
5056
5057
5058
5059
5060
5061
5062
5063
5064
5065
5066
5067
5068
5069
5070
5071
5072
5073
5074
5075
5076
5077
5078
5079
5080
5081
5082
5083
5084
5085
5086
5087
5088
5089
5090
5091
|
/* Copyright (c) 2016-2018 The Linux Foundation. All rights reserved.
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License version 2 and
* only version 2 as published by the Free Software Foundation.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*/
#include <linux/device.h>
#include <linux/regmap.h>
#include <linux/delay.h>
#include <linux/iio/consumer.h>
#include <linux/power_supply.h>
#include <linux/regulator/driver.h>
#include <linux/qpnp/qpnp-revid.h>
#include <linux/input/qpnp-power-on.h>
#include <linux/irq.h>
#include <linux/pmic-voter.h>
#include "smb-lib.h"
#include "smb-reg.h"
#include "battery.h"
#include "step-chg-jeita.h"
#include "storm-watch.h"
#define smblib_err(chg, fmt, ...) \
pr_err("%s: %s: " fmt, chg->name, \
__func__, ##__VA_ARGS__) \
#define smblib_dbg(chg, reason, fmt, ...) \
do { \
if (*chg->debug_mask & (reason)) \
pr_info("%s: %s: " fmt, chg->name, \
__func__, ##__VA_ARGS__); \
else \
pr_debug("%s: %s: " fmt, chg->name, \
__func__, ##__VA_ARGS__); \
} while (0)
static bool is_secure(struct smb_charger *chg, int addr)
{
if (addr == SHIP_MODE_REG || addr == FREQ_CLK_DIV_REG)
return true;
/* assume everything above 0xA0 is secure */
return (bool)((addr & 0xFF) >= 0xA0);
}
int smblib_read(struct smb_charger *chg, u16 addr, u8 *val)
{
unsigned int temp;
int rc = 0;
rc = regmap_read(chg->regmap, addr, &temp);
if (rc >= 0)
*val = (u8)temp;
return rc;
}
int smblib_multibyte_read(struct smb_charger *chg, u16 addr, u8 *val,
int count)
{
return regmap_bulk_read(chg->regmap, addr, val, count);
}
int smblib_masked_write(struct smb_charger *chg, u16 addr, u8 mask, u8 val)
{
int rc = 0;
mutex_lock(&chg->write_lock);
if (is_secure(chg, addr)) {
rc = regmap_write(chg->regmap, (addr & 0xFF00) | 0xD0, 0xA5);
if (rc < 0)
goto unlock;
}
rc = regmap_update_bits(chg->regmap, addr, mask, val);
unlock:
mutex_unlock(&chg->write_lock);
return rc;
}
int smblib_write(struct smb_charger *chg, u16 addr, u8 val)
{
int rc = 0;
mutex_lock(&chg->write_lock);
if (is_secure(chg, addr)) {
rc = regmap_write(chg->regmap, (addr & ~(0xFF)) | 0xD0, 0xA5);
if (rc < 0)
goto unlock;
}
rc = regmap_write(chg->regmap, addr, val);
unlock:
mutex_unlock(&chg->write_lock);
return rc;
}
static int smblib_get_jeita_cc_delta(struct smb_charger *chg, int *cc_delta_ua)
{
int rc, cc_minus_ua;
u8 stat;
rc = smblib_read(chg, BATTERY_CHARGER_STATUS_2_REG, &stat);
if (rc < 0) {
smblib_err(chg, "Couldn't read BATTERY_CHARGER_STATUS_2 rc=%d\n",
rc);
return rc;
}
if (!(stat & BAT_TEMP_STATUS_SOFT_LIMIT_MASK)) {
*cc_delta_ua = 0;
return 0;
}
rc = smblib_get_charge_param(chg, &chg->param.jeita_cc_comp,
&cc_minus_ua);
if (rc < 0) {
smblib_err(chg, "Couldn't get jeita cc minus rc=%d\n", rc);
return rc;
}
*cc_delta_ua = -cc_minus_ua;
return 0;
}
int smblib_icl_override(struct smb_charger *chg, bool override)
{
int rc;
rc = smblib_masked_write(chg, USBIN_LOAD_CFG_REG,
ICL_OVERRIDE_AFTER_APSD_BIT,
override ? ICL_OVERRIDE_AFTER_APSD_BIT : 0);
if (rc < 0)
smblib_err(chg, "Couldn't override ICL rc=%d\n", rc);
return rc;
}
/********************
* REGISTER GETTERS *
********************/
int smblib_get_charge_param(struct smb_charger *chg,
struct smb_chg_param *param, int *val_u)
{
int rc = 0;
u8 val_raw;
rc = smblib_read(chg, param->reg, &val_raw);
if (rc < 0) {
smblib_err(chg, "%s: Couldn't read from 0x%04x rc=%d\n",
param->name, param->reg, rc);
return rc;
}
if (param->get_proc)
*val_u = param->get_proc(param, val_raw);
else
*val_u = val_raw * param->step_u + param->min_u;
smblib_dbg(chg, PR_REGISTER, "%s = %d (0x%02x)\n",
param->name, *val_u, val_raw);
return rc;
}
int smblib_get_usb_suspend(struct smb_charger *chg, int *suspend)
{
int rc = 0;
u8 temp;
rc = smblib_read(chg, USBIN_CMD_IL_REG, &temp);
if (rc < 0) {
smblib_err(chg, "Couldn't read USBIN_CMD_IL rc=%d\n", rc);
return rc;
}
*suspend = temp & USBIN_SUSPEND_BIT;
return rc;
}
struct apsd_result {
const char * const name;
const u8 bit;
const enum power_supply_type pst;
};
enum {
UNKNOWN,
SDP,
CDP,
DCP,
OCP,
FLOAT,
HVDCP2,
HVDCP3,
MAX_TYPES
};
static const struct apsd_result const smblib_apsd_results[] = {
[UNKNOWN] = {
.name = "UNKNOWN",
.bit = 0,
.pst = POWER_SUPPLY_TYPE_UNKNOWN
},
[SDP] = {
.name = "SDP",
.bit = SDP_CHARGER_BIT,
.pst = POWER_SUPPLY_TYPE_USB
},
[CDP] = {
.name = "CDP",
.bit = CDP_CHARGER_BIT,
.pst = POWER_SUPPLY_TYPE_USB_CDP
},
[DCP] = {
.name = "DCP",
.bit = DCP_CHARGER_BIT,
.pst = POWER_SUPPLY_TYPE_USB_DCP
},
[OCP] = {
.name = "OCP",
.bit = OCP_CHARGER_BIT,
.pst = POWER_SUPPLY_TYPE_USB_DCP
},
[FLOAT] = {
.name = "FLOAT",
.bit = FLOAT_CHARGER_BIT,
.pst = POWER_SUPPLY_TYPE_USB_FLOAT
},
[HVDCP2] = {
.name = "HVDCP2",
.bit = DCP_CHARGER_BIT | QC_2P0_BIT,
.pst = POWER_SUPPLY_TYPE_USB_HVDCP
},
[HVDCP3] = {
.name = "HVDCP3",
.bit = DCP_CHARGER_BIT | QC_3P0_BIT,
.pst = POWER_SUPPLY_TYPE_USB_HVDCP_3,
},
};
static const struct apsd_result *smblib_get_apsd_result(struct smb_charger *chg)
{
int rc, i;
u8 apsd_stat, stat;
const struct apsd_result *result = &smblib_apsd_results[UNKNOWN];
rc = smblib_read(chg, APSD_STATUS_REG, &apsd_stat);
if (rc < 0) {
smblib_err(chg, "Couldn't read APSD_STATUS rc=%d\n", rc);
return result;
}
smblib_dbg(chg, PR_REGISTER, "APSD_STATUS = 0x%02x\n", apsd_stat);
if (!(apsd_stat & APSD_DTC_STATUS_DONE_BIT))
return result;
rc = smblib_read(chg, APSD_RESULT_STATUS_REG, &stat);
if (rc < 0) {
smblib_err(chg, "Couldn't read APSD_RESULT_STATUS rc=%d\n",
rc);
return result;
}
stat &= APSD_RESULT_STATUS_MASK;
for (i = 0; i < ARRAY_SIZE(smblib_apsd_results); i++) {
if (smblib_apsd_results[i].bit == stat)
result = &smblib_apsd_results[i];
}
if (apsd_stat & QC_CHARGER_BIT) {
/* since its a qc_charger, either return HVDCP3 or HVDCP2 */
if (result != &smblib_apsd_results[HVDCP3])
result = &smblib_apsd_results[HVDCP2];
}
return result;
}
/********************
* REGISTER SETTERS *
********************/
static int chg_freq_list[] = {
9600, 9600, 6400, 4800, 3800, 3200, 2700, 2400, 2100, 1900, 1700,
1600, 1500, 1400, 1300, 1200,
};
int smblib_set_chg_freq(struct smb_chg_param *param,
int val_u, u8 *val_raw)
{
u8 i;
if (val_u > param->max_u || val_u < param->min_u)
return -EINVAL;
/* Charger FSW is the configured freqency / 2 */
val_u *= 2;
for (i = 0; i < ARRAY_SIZE(chg_freq_list); i++) {
if (chg_freq_list[i] == val_u)
break;
}
if (i == ARRAY_SIZE(chg_freq_list)) {
pr_err("Invalid frequency %d Hz\n", val_u / 2);
return -EINVAL;
}
*val_raw = i;
return 0;
}
static int smblib_set_opt_freq_buck(struct smb_charger *chg, int fsw_khz)
{
union power_supply_propval pval = {0, };
int rc = 0;
rc = smblib_set_charge_param(chg, &chg->param.freq_buck, fsw_khz);
if (rc < 0)
dev_err(chg->dev, "Error in setting freq_buck rc=%d\n", rc);
if (chg->mode == PARALLEL_MASTER && chg->pl.psy) {
pval.intval = fsw_khz;
/*
* Some parallel charging implementations may not have
* PROP_BUCK_FREQ property - they could be running
* with a fixed frequency
*/
power_supply_set_property(chg->pl.psy,
POWER_SUPPLY_PROP_BUCK_FREQ, &pval);
}
return rc;
}
int smblib_set_charge_param(struct smb_charger *chg,
struct smb_chg_param *param, int val_u)
{
int rc = 0;
u8 val_raw;
if (param->set_proc) {
rc = param->set_proc(param, val_u, &val_raw);
if (rc < 0)
return -EINVAL;
} else {
if (val_u > param->max_u || val_u < param->min_u) {
smblib_err(chg, "%s: %d is out of range [%d, %d]\n",
param->name, val_u, param->min_u, param->max_u);
return -EINVAL;
}
val_raw = (val_u - param->min_u) / param->step_u;
}
rc = smblib_write(chg, param->reg, val_raw);
if (rc < 0) {
smblib_err(chg, "%s: Couldn't write 0x%02x to 0x%04x rc=%d\n",
param->name, val_raw, param->reg, rc);
return rc;
}
smblib_dbg(chg, PR_REGISTER, "%s = %d (0x%02x)\n",
param->name, val_u, val_raw);
return rc;
}
int smblib_set_usb_suspend(struct smb_charger *chg, bool suspend)
{
int rc = 0;
int irq = chg->irq_info[USBIN_ICL_CHANGE_IRQ].irq;
if (suspend && irq) {
if (chg->usb_icl_change_irq_enabled) {
disable_irq_nosync(irq);
chg->usb_icl_change_irq_enabled = false;
}
}
rc = smblib_masked_write(chg, USBIN_CMD_IL_REG, USBIN_SUSPEND_BIT,
suspend ? USBIN_SUSPEND_BIT : 0);
if (rc < 0)
smblib_err(chg, "Couldn't write %s to USBIN_SUSPEND_BIT rc=%d\n",
suspend ? "suspend" : "resume", rc);
if (!suspend && irq) {
if (!chg->usb_icl_change_irq_enabled) {
enable_irq(irq);
chg->usb_icl_change_irq_enabled = true;
}
}
return rc;
}
int smblib_set_dc_suspend(struct smb_charger *chg, bool suspend)
{
int rc = 0;
rc = smblib_masked_write(chg, DCIN_CMD_IL_REG, DCIN_SUSPEND_BIT,
suspend ? DCIN_SUSPEND_BIT : 0);
if (rc < 0)
smblib_err(chg, "Couldn't write %s to DCIN_SUSPEND_BIT rc=%d\n",
suspend ? "suspend" : "resume", rc);
return rc;
}
static int smblib_set_adapter_allowance(struct smb_charger *chg,
u8 allowed_voltage)
{
int rc = 0;
/* PM660 only support max. 9V */
if (chg->smb_version == PM660_SUBTYPE) {
switch (allowed_voltage) {
case USBIN_ADAPTER_ALLOW_12V:
case USBIN_ADAPTER_ALLOW_9V_TO_12V:
allowed_voltage = USBIN_ADAPTER_ALLOW_9V;
break;
case USBIN_ADAPTER_ALLOW_5V_OR_12V:
case USBIN_ADAPTER_ALLOW_5V_OR_9V_TO_12V:
allowed_voltage = USBIN_ADAPTER_ALLOW_5V_OR_9V;
break;
case USBIN_ADAPTER_ALLOW_5V_TO_12V:
allowed_voltage = USBIN_ADAPTER_ALLOW_5V_TO_9V;
break;
}
}
rc = smblib_write(chg, USBIN_ADAPTER_ALLOW_CFG_REG, allowed_voltage);
if (rc < 0) {
smblib_err(chg, "Couldn't write 0x%02x to USBIN_ADAPTER_ALLOW_CFG rc=%d\n",
allowed_voltage, rc);
return rc;
}
return rc;
}
#define MICRO_5V 5000000
#define MICRO_9V 9000000
#define MICRO_12V 12000000
static int smblib_set_usb_pd_allowed_voltage(struct smb_charger *chg,
int min_allowed_uv, int max_allowed_uv)
{
int rc;
u8 allowed_voltage;
if (min_allowed_uv == MICRO_5V && max_allowed_uv == MICRO_5V) {
allowed_voltage = USBIN_ADAPTER_ALLOW_5V;
smblib_set_opt_freq_buck(chg, chg->chg_freq.freq_5V);
} else if (min_allowed_uv == MICRO_9V && max_allowed_uv == MICRO_9V) {
allowed_voltage = USBIN_ADAPTER_ALLOW_9V;
smblib_set_opt_freq_buck(chg, chg->chg_freq.freq_9V);
} else if (min_allowed_uv == MICRO_12V && max_allowed_uv == MICRO_12V) {
allowed_voltage = USBIN_ADAPTER_ALLOW_12V;
smblib_set_opt_freq_buck(chg, chg->chg_freq.freq_12V);
} else if (min_allowed_uv < MICRO_9V && max_allowed_uv <= MICRO_9V) {
allowed_voltage = USBIN_ADAPTER_ALLOW_5V_TO_9V;
} else if (min_allowed_uv < MICRO_9V && max_allowed_uv <= MICRO_12V) {
allowed_voltage = USBIN_ADAPTER_ALLOW_5V_TO_12V;
} else if (min_allowed_uv < MICRO_12V && max_allowed_uv <= MICRO_12V) {
allowed_voltage = USBIN_ADAPTER_ALLOW_9V_TO_12V;
} else {
smblib_err(chg, "invalid allowed voltage [%d, %d]\n",
min_allowed_uv, max_allowed_uv);
return -EINVAL;
}
rc = smblib_set_adapter_allowance(chg, allowed_voltage);
if (rc < 0) {
smblib_err(chg, "Couldn't configure adapter allowance rc=%d\n",
rc);
return rc;
}
return rc;
}
/********************
* HELPER FUNCTIONS *
********************/
static int smblib_request_dpdm(struct smb_charger *chg, bool enable)
{
int rc = 0;
/* fetch the DPDM regulator */
if (!chg->dpdm_reg && of_get_property(chg->dev->of_node,
"dpdm-supply", NULL)) {
chg->dpdm_reg = devm_regulator_get(chg->dev, "dpdm");
if (IS_ERR(chg->dpdm_reg)) {
rc = PTR_ERR(chg->dpdm_reg);
smblib_err(chg, "Couldn't get dpdm regulator rc=%d\n",
rc);
chg->dpdm_reg = NULL;
return rc;
}
}
if (enable) {
if (chg->dpdm_reg && !regulator_is_enabled(chg->dpdm_reg)) {
smblib_dbg(chg, PR_MISC, "enabling DPDM regulator\n");
rc = regulator_enable(chg->dpdm_reg);
if (rc < 0)
smblib_err(chg,
"Couldn't enable dpdm regulator rc=%d\n",
rc);
}
} else {
if (chg->dpdm_reg && regulator_is_enabled(chg->dpdm_reg)) {
smblib_dbg(chg, PR_MISC, "disabling DPDM regulator\n");
rc = regulator_disable(chg->dpdm_reg);
if (rc < 0)
smblib_err(chg,
"Couldn't disable dpdm regulator rc=%d\n",
rc);
}
}
return rc;
}
static void smblib_rerun_apsd(struct smb_charger *chg)
{
int rc;
smblib_dbg(chg, PR_MISC, "re-running APSD\n");
if (chg->wa_flags & QC_AUTH_INTERRUPT_WA_BIT) {
rc = smblib_masked_write(chg,
USBIN_SOURCE_CHANGE_INTRPT_ENB_REG,
AUTH_IRQ_EN_CFG_BIT, AUTH_IRQ_EN_CFG_BIT);
if (rc < 0)
smblib_err(chg, "Couldn't enable HVDCP auth IRQ rc=%d\n",
rc);
}
rc = smblib_masked_write(chg, CMD_APSD_REG,
APSD_RERUN_BIT, APSD_RERUN_BIT);
if (rc < 0)
smblib_err(chg, "Couldn't re-run APSD rc=%d\n", rc);
}
static const struct apsd_result *smblib_update_usb_type(struct smb_charger *chg)
{
const struct apsd_result *apsd_result = smblib_get_apsd_result(chg);
/* if PD is active, APSD is disabled so won't have a valid result */
if (chg->pd_active) {
chg->real_charger_type = POWER_SUPPLY_TYPE_USB_PD;
} else {
/*
* Update real charger type only if its not FLOAT
* detected as as SDP
*/
if (!(apsd_result->pst == POWER_SUPPLY_TYPE_USB_FLOAT &&
chg->real_charger_type == POWER_SUPPLY_TYPE_USB))
chg->real_charger_type = apsd_result->pst;
}
smblib_dbg(chg, PR_MISC, "APSD=%s PD=%d\n",
apsd_result->name, chg->pd_active);
return apsd_result;
}
static int smblib_notifier_call(struct notifier_block *nb,
unsigned long ev, void *v)
{
struct power_supply *psy = v;
struct smb_charger *chg = container_of(nb, struct smb_charger, nb);
if (!strcmp(psy->desc->name, "bms")) {
if (!chg->bms_psy)
chg->bms_psy = psy;
if (ev == PSY_EVENT_PROP_CHANGED)
schedule_work(&chg->bms_update_work);
}
if (!chg->pl.psy && !strcmp(psy->desc->name, "parallel"))
chg->pl.psy = psy;
return NOTIFY_OK;
}
static int smblib_register_notifier(struct smb_charger *chg)
{
int rc;
chg->nb.notifier_call = smblib_notifier_call;
rc = power_supply_reg_notifier(&chg->nb);
if (rc < 0) {
smblib_err(chg, "Couldn't register psy notifier rc = %d\n", rc);
return rc;
}
return 0;
}
int smblib_mapping_soc_from_field_value(struct smb_chg_param *param,
int val_u, u8 *val_raw)
{
if (val_u > param->max_u || val_u < param->min_u)
return -EINVAL;
*val_raw = val_u << 1;
return 0;
}
int smblib_mapping_cc_delta_to_field_value(struct smb_chg_param *param,
u8 val_raw)
{
int val_u = val_raw * param->step_u + param->min_u;
if (val_u > param->max_u)
val_u -= param->max_u * 2;
return val_u;
}
int smblib_mapping_cc_delta_from_field_value(struct smb_chg_param *param,
int val_u, u8 *val_raw)
{
if (val_u > param->max_u || val_u < param->min_u - param->max_u)
return -EINVAL;
val_u += param->max_u * 2 - param->min_u;
val_u %= param->max_u * 2;
*val_raw = val_u / param->step_u;
return 0;
}
static void smblib_uusb_removal(struct smb_charger *chg)
{
int rc;
struct smb_irq_data *data;
struct storm_watch *wdata;
cancel_delayed_work_sync(&chg->pl_enable_work);
rc = smblib_request_dpdm(chg, false);
if (rc < 0)
smblib_err(chg, "Couldn't to disable DPDM rc=%d\n", rc);
if (chg->wa_flags & BOOST_BACK_WA) {
data = chg->irq_info[SWITCH_POWER_OK_IRQ].irq_data;
if (data) {
wdata = &data->storm_data;
update_storm_count(wdata, WEAK_CHG_STORM_COUNT);
vote(chg->usb_icl_votable, BOOST_BACK_VOTER, false, 0);
vote(chg->usb_icl_votable, WEAK_CHARGER_VOTER,
false, 0);
}
}
vote(chg->pl_disable_votable, PL_DELAY_VOTER, true, 0);
vote(chg->awake_votable, PL_DELAY_VOTER, false, 0);
/* reset both usbin current and voltage votes */
vote(chg->pl_enable_votable_indirect, USBIN_I_VOTER, false, 0);
vote(chg->pl_enable_votable_indirect, USBIN_V_VOTER, false, 0);
vote(chg->usb_icl_votable, SW_QC3_VOTER, false, 0);
vote(chg->usb_icl_votable, USBIN_USBIN_BOOST_VOTER, false, 0);
vote(chg->hvdcp_hw_inov_dis_votable, OV_VOTER, false, 0);
cancel_delayed_work_sync(&chg->hvdcp_detect_work);
if (chg->wa_flags & QC_AUTH_INTERRUPT_WA_BIT) {
/* re-enable AUTH_IRQ_EN_CFG_BIT */
rc = smblib_masked_write(chg,
USBIN_SOURCE_CHANGE_INTRPT_ENB_REG,
AUTH_IRQ_EN_CFG_BIT, AUTH_IRQ_EN_CFG_BIT);
if (rc < 0)
smblib_err(chg,
"Couldn't enable QC auth setting rc=%d\n", rc);
}
/* reconfigure allowed voltage for HVDCP */
rc = smblib_set_adapter_allowance(chg,
USBIN_ADAPTER_ALLOW_5V_OR_9V_TO_12V);
if (rc < 0)
smblib_err(chg, "Couldn't set USBIN_ADAPTER_ALLOW_5V_OR_9V_TO_12V rc=%d\n",
rc);
chg->voltage_min_uv = MICRO_5V;
chg->voltage_max_uv = MICRO_5V;
chg->usb_icl_delta_ua = 0;
chg->pulse_cnt = 0;
chg->uusb_apsd_rerun_done = false;
/* clear USB ICL vote for USB_PSY_VOTER */
rc = vote(chg->usb_icl_votable, USB_PSY_VOTER, false, 0);
if (rc < 0)
smblib_err(chg, "Couldn't un-vote for USB ICL rc=%d\n", rc);
/* clear USB ICL vote for DCP_VOTER */
rc = vote(chg->usb_icl_votable, DCP_VOTER, false, 0);
if (rc < 0)
smblib_err(chg,
"Couldn't un-vote DCP from USB ICL rc=%d\n", rc);
}
void smblib_suspend_on_debug_battery(struct smb_charger *chg)
{
int rc;
union power_supply_propval val;
if (!chg->suspend_input_on_debug_batt)
return;
rc = power_supply_get_property(chg->bms_psy,
POWER_SUPPLY_PROP_DEBUG_BATTERY, &val);
if (rc < 0) {
smblib_err(chg, "Couldn't get debug battery prop rc=%d\n", rc);
return;
}
vote(chg->usb_icl_votable, DEBUG_BOARD_VOTER, val.intval, 0);
vote(chg->dc_suspend_votable, DEBUG_BOARD_VOTER, val.intval, 0);
if (val.intval)
pr_info("Input suspended: Fake battery\n");
}
int smblib_rerun_apsd_if_required(struct smb_charger *chg)
{
union power_supply_propval val;
int rc;
rc = smblib_get_prop_usb_present(chg, &val);
if (rc < 0) {
smblib_err(chg, "Couldn't get usb present rc = %d\n", rc);
return rc;
}
if (!val.intval)
return 0;
rc = smblib_request_dpdm(chg, true);
if (rc < 0)
smblib_err(chg, "Couldn't to enable DPDM rc=%d\n", rc);
chg->uusb_apsd_rerun_done = true;
smblib_rerun_apsd(chg);
return 0;
}
static int smblib_get_hw_pulse_cnt(struct smb_charger *chg, int *count)
{
int rc;
u8 val[2];
switch (chg->smb_version) {
case PMI8998_SUBTYPE:
rc = smblib_read(chg, QC_PULSE_COUNT_STATUS_REG, val);
if (rc) {
pr_err("failed to read QC_PULSE_COUNT_STATUS_REG rc=%d\n",
rc);
return rc;
}
*count = val[0] & QC_PULSE_COUNT_MASK;
break;
case PM660_SUBTYPE:
rc = smblib_multibyte_read(chg,
QC_PULSE_COUNT_STATUS_1_REG, val, 2);
if (rc) {
pr_err("failed to read QC_PULSE_COUNT_STATUS_1_REG rc=%d\n",
rc);
return rc;
}
*count = (val[1] << 8) | val[0];
break;
default:
smblib_dbg(chg, PR_PARALLEL, "unknown SMB chip %d\n",
chg->smb_version);
return -EINVAL;
}
return 0;
}
static int smblib_get_pulse_cnt(struct smb_charger *chg, int *count)
{
int rc;
/* Use software based pulse count if HW INOV is disabled */
if (get_effective_result(chg->hvdcp_hw_inov_dis_votable) > 0) {
*count = chg->pulse_cnt;
return 0;
}
/* Use h/w pulse count if autonomous mode is enabled */
rc = smblib_get_hw_pulse_cnt(chg, count);
if (rc < 0)
smblib_err(chg, "failed to read h/w pulse count rc=%d\n", rc);
return rc;
}
#define USBIN_25MA 25000
#define USBIN_100MA 100000
#define USBIN_150MA 150000
#define USBIN_500MA 500000
#define USBIN_900MA 900000
static int set_sdp_current(struct smb_charger *chg, int icl_ua)
{
int rc;
u8 icl_options;
const struct apsd_result *apsd_result = smblib_get_apsd_result(chg);
/* power source is SDP */
switch (icl_ua) {
case USBIN_100MA:
/* USB 2.0 100mA */
icl_options = 0;
break;
case USBIN_150MA:
/* USB 3.0 150mA */
icl_options = CFG_USB3P0_SEL_BIT;
break;
case USBIN_500MA:
/* USB 2.0 500mA */
icl_options = USB51_MODE_BIT;
break;
case USBIN_900MA:
/* USB 3.0 900mA */
icl_options = CFG_USB3P0_SEL_BIT | USB51_MODE_BIT;
break;
default:
smblib_err(chg, "ICL %duA isn't supported for SDP\n", icl_ua);
return -EINVAL;
}
if (chg->real_charger_type == POWER_SUPPLY_TYPE_USB &&
apsd_result->pst == POWER_SUPPLY_TYPE_USB_FLOAT) {
/*
* change the float charger configuration to SDP, if this
* is the case of SDP being detected as FLOAT
*/
rc = smblib_masked_write(chg, USBIN_OPTIONS_2_CFG_REG,
FORCE_FLOAT_SDP_CFG_BIT, FORCE_FLOAT_SDP_CFG_BIT);
if (rc < 0) {
smblib_err(chg, "Couldn't set float ICL options rc=%d\n",
rc);
return rc;
}
}
rc = smblib_masked_write(chg, USBIN_ICL_OPTIONS_REG,
CFG_USB3P0_SEL_BIT | USB51_MODE_BIT, icl_options);
if (rc < 0) {
smblib_err(chg, "Couldn't set ICL options rc=%d\n", rc);
return rc;
}
return rc;
}
static int get_sdp_current(struct smb_charger *chg, int *icl_ua)
{
int rc;
u8 icl_options;
bool usb3 = false;
rc = smblib_read(chg, USBIN_ICL_OPTIONS_REG, &icl_options);
if (rc < 0) {
smblib_err(chg, "Couldn't get ICL options rc=%d\n", rc);
return rc;
}
usb3 = (icl_options & CFG_USB3P0_SEL_BIT);
if (icl_options & USB51_MODE_BIT)
*icl_ua = usb3 ? USBIN_900MA : USBIN_500MA;
else
*icl_ua = usb3 ? USBIN_150MA : USBIN_100MA;
return rc;
}
int smblib_set_icl_current(struct smb_charger *chg, int icl_ua)
{
int rc = 0;
bool override;
/* suspend and return if 25mA or less is requested */
if (icl_ua < USBIN_25MA)
return smblib_set_usb_suspend(chg, true);
if (icl_ua == INT_MAX)
goto override_suspend_config;
/* configure current */
if (chg->typec_mode == POWER_SUPPLY_TYPEC_SOURCE_DEFAULT
&& (chg->real_charger_type == POWER_SUPPLY_TYPE_USB)) {
rc = set_sdp_current(chg, icl_ua);
if (rc < 0) {
smblib_err(chg, "Couldn't set SDP ICL rc=%d\n", rc);
goto enable_icl_changed_interrupt;
}
} else {
set_sdp_current(chg, 100000);
rc = smblib_set_charge_param(chg, &chg->param.usb_icl, icl_ua);
if (rc < 0) {
smblib_err(chg, "Couldn't set HC ICL rc=%d\n", rc);
goto enable_icl_changed_interrupt;
}
}
override_suspend_config:
/* determine if override needs to be enforced */
override = true;
if (icl_ua == INT_MAX) {
/* remove override if no voters - hw defaults is desired */
override = false;
} else if (chg->typec_mode == POWER_SUPPLY_TYPEC_SOURCE_DEFAULT) {
if (chg->real_charger_type == POWER_SUPPLY_TYPE_USB)
/* For std cable with type = SDP never override */
override = false;
else if (chg->real_charger_type == POWER_SUPPLY_TYPE_USB_CDP
&& icl_ua == 1500000)
/*
* For std cable with type = CDP override only if
* current is not 1500mA
*/
override = false;
}
/* enforce override */
rc = smblib_masked_write(chg, USBIN_ICL_OPTIONS_REG,
USBIN_MODE_CHG_BIT, override ? USBIN_MODE_CHG_BIT : 0);
rc = smblib_icl_override(chg, override);
if (rc < 0) {
smblib_err(chg, "Couldn't set ICL override rc=%d\n", rc);
goto enable_icl_changed_interrupt;
}
/* unsuspend after configuring current and override */
rc = smblib_set_usb_suspend(chg, false);
if (rc < 0) {
smblib_err(chg, "Couldn't resume input rc=%d\n", rc);
goto enable_icl_changed_interrupt;
}
enable_icl_changed_interrupt:
return rc;
}
int smblib_get_icl_current(struct smb_charger *chg, int *icl_ua)
{
int rc = 0;
u8 load_cfg;
bool override;
if ((chg->typec_mode == POWER_SUPPLY_TYPEC_SOURCE_DEFAULT
|| chg->micro_usb_mode)
&& (chg->usb_psy_desc.type == POWER_SUPPLY_TYPE_USB)) {
rc = get_sdp_current(chg, icl_ua);
if (rc < 0) {
smblib_err(chg, "Couldn't get SDP ICL rc=%d\n", rc);
return rc;
}
} else {
rc = smblib_read(chg, USBIN_LOAD_CFG_REG, &load_cfg);
if (rc < 0) {
smblib_err(chg, "Couldn't get load cfg rc=%d\n", rc);
return rc;
}
override = load_cfg & ICL_OVERRIDE_AFTER_APSD_BIT;
if (!override)
return INT_MAX;
/* override is set */
rc = smblib_get_charge_param(chg, &chg->param.usb_icl, icl_ua);
if (rc < 0) {
smblib_err(chg, "Couldn't get HC ICL rc=%d\n", rc);
return rc;
}
}
return 0;
}
/*********************
* VOTABLE CALLBACKS *
*********************/
static int smblib_dc_suspend_vote_callback(struct votable *votable, void *data,
int suspend, const char *client)
{
struct smb_charger *chg = data;
/* resume input if suspend is invalid */
if (suspend < 0)
suspend = 0;
return smblib_set_dc_suspend(chg, (bool)suspend);
}
static int smblib_dc_icl_vote_callback(struct votable *votable, void *data,
int icl_ua, const char *client)
{
struct smb_charger *chg = data;
int rc = 0;
bool suspend;
if (icl_ua < 0) {
smblib_dbg(chg, PR_MISC, "No Voter hence suspending\n");
icl_ua = 0;
}
suspend = (icl_ua < USBIN_25MA);
if (suspend)
goto suspend;
rc = smblib_set_charge_param(chg, &chg->param.dc_icl, icl_ua);
if (rc < 0) {
smblib_err(chg, "Couldn't set DC input current limit rc=%d\n",
rc);
return rc;
}
suspend:
rc = vote(chg->dc_suspend_votable, USER_VOTER, suspend, 0);
if (rc < 0) {
smblib_err(chg, "Couldn't vote to %s DC rc=%d\n",
suspend ? "suspend" : "resume", rc);
return rc;
}
return rc;
}
static int smblib_pd_disallowed_votable_indirect_callback(
struct votable *votable, void *data, int disallowed, const char *client)
{
struct smb_charger *chg = data;
int rc;
rc = vote(chg->pd_allowed_votable, PD_DISALLOWED_INDIRECT_VOTER,
!disallowed, 0);
return rc;
}
static int smblib_awake_vote_callback(struct votable *votable, void *data,
int awake, const char *client)
{
struct smb_charger *chg = data;
if (awake)
pm_stay_awake(chg->dev);
else
pm_relax(chg->dev);
return 0;
}
static int smblib_chg_disable_vote_callback(struct votable *votable, void *data,
int chg_disable, const char *client)
{
struct smb_charger *chg = data;
int rc;
rc = smblib_masked_write(chg, CHARGING_ENABLE_CMD_REG,
CHARGING_ENABLE_CMD_BIT,
chg_disable ? 0 : CHARGING_ENABLE_CMD_BIT);
if (rc < 0) {
smblib_err(chg, "Couldn't %s charging rc=%d\n",
chg_disable ? "disable" : "enable", rc);
return rc;
}
return 0;
}
static int smblib_hvdcp_enable_vote_callback(struct votable *votable,
void *data,
int hvdcp_enable, const char *client)
{
struct smb_charger *chg = data;
int rc;
u8 val = HVDCP_AUTH_ALG_EN_CFG_BIT | HVDCP_EN_BIT;
u8 stat;
/* vote to enable/disable HW autonomous INOV */
vote(chg->hvdcp_hw_inov_dis_votable, client, !hvdcp_enable, 0);
/*
* Disable the autonomous bit and auth bit for disabling hvdcp.
* This ensures only qc 2.0 detection runs but no vbus
* negotiation happens.
*/
if (!hvdcp_enable)
val = HVDCP_EN_BIT;
rc = smblib_masked_write(chg, USBIN_OPTIONS_1_CFG_REG,
HVDCP_EN_BIT | HVDCP_AUTH_ALG_EN_CFG_BIT,
val);
if (rc < 0) {
smblib_err(chg, "Couldn't %s hvdcp rc=%d\n",
hvdcp_enable ? "enable" : "disable", rc);
return rc;
}
rc = smblib_read(chg, APSD_STATUS_REG, &stat);
if (rc < 0) {
smblib_err(chg, "Couldn't read APSD status rc=%d\n", rc);
return rc;
}
/* re-run APSD if HVDCP was detected */
if (stat & QC_CHARGER_BIT)
smblib_rerun_apsd(chg);
return 0;
}
static int smblib_hvdcp_disable_indirect_vote_callback(struct votable *votable,
void *data, int hvdcp_disable, const char *client)
{
struct smb_charger *chg = data;
vote(chg->hvdcp_enable_votable, HVDCP_INDIRECT_VOTER,
!hvdcp_disable, 0);
return 0;
}
static int smblib_apsd_disable_vote_callback(struct votable *votable,
void *data,
int apsd_disable, const char *client)
{
struct smb_charger *chg = data;
int rc;
if (apsd_disable) {
rc = smblib_masked_write(chg, USBIN_OPTIONS_1_CFG_REG,
AUTO_SRC_DETECT_BIT,
0);
if (rc < 0) {
smblib_err(chg, "Couldn't disable APSD rc=%d\n", rc);
return rc;
}
} else {
rc = smblib_masked_write(chg, USBIN_OPTIONS_1_CFG_REG,
AUTO_SRC_DETECT_BIT,
AUTO_SRC_DETECT_BIT);
if (rc < 0) {
smblib_err(chg, "Couldn't enable APSD rc=%d\n", rc);
return rc;
}
}
return 0;
}
static int smblib_hvdcp_hw_inov_dis_vote_callback(struct votable *votable,
void *data, int disable, const char *client)
{
struct smb_charger *chg = data;
int rc;
if (disable) {
/*
* the pulse count register get zeroed when autonomous mode is
* disabled. Track that in variables before disabling
*/
rc = smblib_get_hw_pulse_cnt(chg, &chg->pulse_cnt);
if (rc < 0) {
pr_err("failed to read QC_PULSE_COUNT_STATUS_REG rc=%d\n",
rc);
return rc;
}
}
rc = smblib_masked_write(chg, USBIN_OPTIONS_1_CFG_REG,
HVDCP_AUTONOMOUS_MODE_EN_CFG_BIT,
disable ? 0 : HVDCP_AUTONOMOUS_MODE_EN_CFG_BIT);
if (rc < 0) {
smblib_err(chg, "Couldn't %s hvdcp rc=%d\n",
disable ? "disable" : "enable", rc);
return rc;
}
return rc;
}
static int smblib_usb_irq_enable_vote_callback(struct votable *votable,
void *data, int enable, const char *client)
{
struct smb_charger *chg = data;
if (!chg->irq_info[INPUT_CURRENT_LIMIT_IRQ].irq ||
!chg->irq_info[HIGH_DUTY_CYCLE_IRQ].irq)
return 0;
if (enable) {
enable_irq(chg->irq_info[INPUT_CURRENT_LIMIT_IRQ].irq);
enable_irq(chg->irq_info[HIGH_DUTY_CYCLE_IRQ].irq);
} else {
disable_irq(chg->irq_info[INPUT_CURRENT_LIMIT_IRQ].irq);
disable_irq(chg->irq_info[HIGH_DUTY_CYCLE_IRQ].irq);
}
return 0;
}
static int smblib_typec_irq_disable_vote_callback(struct votable *votable,
void *data, int disable, const char *client)
{
struct smb_charger *chg = data;
if (!chg->irq_info[TYPE_C_CHANGE_IRQ].irq)
return 0;
if (disable)
disable_irq_nosync(chg->irq_info[TYPE_C_CHANGE_IRQ].irq);
else
enable_irq(chg->irq_info[TYPE_C_CHANGE_IRQ].irq);
return 0;
}
/*******************
* VCONN REGULATOR *
* *****************/
#define MAX_OTG_SS_TRIES 2
static int _smblib_vconn_regulator_enable(struct regulator_dev *rdev)
{
struct smb_charger *chg = rdev_get_drvdata(rdev);
int rc = 0;
u8 val;
/*
* When enabling VCONN using the command register the CC pin must be
* selected. VCONN should be supplied to the inactive CC pin hence using
* the opposite of the CC_ORIENTATION_BIT.
*/
smblib_dbg(chg, PR_OTG, "enabling VCONN\n");
val = chg->typec_status[3] &
CC_ORIENTATION_BIT ? 0 : VCONN_EN_ORIENTATION_BIT;
rc = smblib_masked_write(chg, TYPE_C_INTRPT_ENB_SOFTWARE_CTRL_REG,
VCONN_EN_VALUE_BIT | VCONN_EN_ORIENTATION_BIT,
VCONN_EN_VALUE_BIT | val);
if (rc < 0) {
smblib_err(chg, "Couldn't enable vconn setting rc=%d\n", rc);
return rc;
}
return rc;
}
int smblib_vconn_regulator_enable(struct regulator_dev *rdev)
{
struct smb_charger *chg = rdev_get_drvdata(rdev);
int rc = 0;
mutex_lock(&chg->vconn_oc_lock);
if (chg->vconn_en)
goto unlock;
rc = _smblib_vconn_regulator_enable(rdev);
if (rc >= 0)
chg->vconn_en = true;
unlock:
mutex_unlock(&chg->vconn_oc_lock);
return rc;
}
static int _smblib_vconn_regulator_disable(struct regulator_dev *rdev)
{
struct smb_charger *chg = rdev_get_drvdata(rdev);
int rc = 0;
smblib_dbg(chg, PR_OTG, "disabling VCONN\n");
rc = smblib_masked_write(chg, TYPE_C_INTRPT_ENB_SOFTWARE_CTRL_REG,
VCONN_EN_VALUE_BIT, 0);
if (rc < 0)
smblib_err(chg, "Couldn't disable vconn regulator rc=%d\n", rc);
return rc;
}
int smblib_vconn_regulator_disable(struct regulator_dev *rdev)
{
struct smb_charger *chg = rdev_get_drvdata(rdev);
int rc = 0;
mutex_lock(&chg->vconn_oc_lock);
if (!chg->vconn_en)
goto unlock;
rc = _smblib_vconn_regulator_disable(rdev);
if (rc >= 0)
chg->vconn_en = false;
unlock:
mutex_unlock(&chg->vconn_oc_lock);
return rc;
}
int smblib_vconn_regulator_is_enabled(struct regulator_dev *rdev)
{
struct smb_charger *chg = rdev_get_drvdata(rdev);
int ret;
mutex_lock(&chg->vconn_oc_lock);
ret = chg->vconn_en;
mutex_unlock(&chg->vconn_oc_lock);
return ret;
}
/*****************
* OTG REGULATOR *
*****************/
#define MAX_RETRY 15
#define MIN_DELAY_US 2000
#define MAX_DELAY_US 9000
static int otg_current[] = {250000, 500000, 1000000, 1500000};
static int smblib_enable_otg_wa(struct smb_charger *chg)
{
u8 stat;
int rc, i, retry_count = 0, min_delay = MIN_DELAY_US;
for (i = 0; i < ARRAY_SIZE(otg_current); i++) {
smblib_dbg(chg, PR_OTG, "enabling OTG with %duA\n",
otg_current[i]);
rc = smblib_set_charge_param(chg, &chg->param.otg_cl,
otg_current[i]);
if (rc < 0) {
smblib_err(chg, "Couldn't set otg limit rc=%d\n", rc);
return rc;
}
rc = smblib_write(chg, CMD_OTG_REG, OTG_EN_BIT);
if (rc < 0) {
smblib_err(chg, "Couldn't enable OTG rc=%d\n", rc);
return rc;
}
retry_count = 0;
min_delay = MIN_DELAY_US;
do {
usleep_range(min_delay, min_delay + 100);
rc = smblib_read(chg, OTG_STATUS_REG, &stat);
if (rc < 0) {
smblib_err(chg, "Couldn't read OTG status rc=%d\n",
rc);
goto out;
}
if (stat & BOOST_SOFTSTART_DONE_BIT) {
rc = smblib_set_charge_param(chg,
&chg->param.otg_cl, chg->otg_cl_ua);
if (rc < 0) {
smblib_err(chg, "Couldn't set otg limit rc=%d\n",
rc);
goto out;
}
break;
}
/* increase the delay for following iterations */
if (retry_count > 5)
min_delay = MAX_DELAY_US;
} while (retry_count++ < MAX_RETRY);
if (retry_count >= MAX_RETRY) {
smblib_dbg(chg, PR_OTG, "OTG enable failed with %duA\n",
otg_current[i]);
rc = smblib_write(chg, CMD_OTG_REG, 0);
if (rc < 0) {
smblib_err(chg, "disable OTG rc=%d\n", rc);
goto out;
}
} else {
smblib_dbg(chg, PR_OTG, "OTG enabled\n");
return 0;
}
}
if (i == ARRAY_SIZE(otg_current)) {
rc = -EINVAL;
goto out;
}
return 0;
out:
smblib_write(chg, CMD_OTG_REG, 0);
return rc;
}
static int _smblib_vbus_regulator_enable(struct regulator_dev *rdev)
{
struct smb_charger *chg = rdev_get_drvdata(rdev);
int rc;
smblib_dbg(chg, PR_OTG, "halt 1 in 8 mode\n");
rc = smblib_masked_write(chg, OTG_ENG_OTG_CFG_REG,
ENG_BUCKBOOST_HALT1_8_MODE_BIT,
ENG_BUCKBOOST_HALT1_8_MODE_BIT);
if (rc < 0) {
smblib_err(chg, "Couldn't set OTG_ENG_OTG_CFG_REG rc=%d\n",
rc);
return rc;
}
smblib_dbg(chg, PR_OTG, "enabling OTG\n");
if (chg->wa_flags & OTG_WA) {
rc = smblib_enable_otg_wa(chg);
if (rc < 0)
smblib_err(chg, "Couldn't enable OTG rc=%d\n", rc);
} else {
rc = smblib_write(chg, CMD_OTG_REG, OTG_EN_BIT);
if (rc < 0)
smblib_err(chg, "Couldn't enable OTG rc=%d\n", rc);
}
return rc;
}
int smblib_vbus_regulator_enable(struct regulator_dev *rdev)
{
struct smb_charger *chg = rdev_get_drvdata(rdev);
int rc = 0;
mutex_lock(&chg->otg_oc_lock);
if (chg->otg_en)
goto unlock;
if (!chg->usb_icl_votable) {
chg->usb_icl_votable = find_votable("USB_ICL");
if (!chg->usb_icl_votable)
return -EINVAL;
}
vote(chg->usb_icl_votable, USBIN_USBIN_BOOST_VOTER, true, 0);
rc = _smblib_vbus_regulator_enable(rdev);
if (rc >= 0)
chg->otg_en = true;
else
vote(chg->usb_icl_votable, USBIN_USBIN_BOOST_VOTER, false, 0);
unlock:
mutex_unlock(&chg->otg_oc_lock);
return rc;
}
static int _smblib_vbus_regulator_disable(struct regulator_dev *rdev)
{
struct smb_charger *chg = rdev_get_drvdata(rdev);
int rc;
if (chg->wa_flags & OTG_WA) {
/* set OTG current limit to minimum value */
rc = smblib_set_charge_param(chg, &chg->param.otg_cl,
chg->param.otg_cl.min_u);
if (rc < 0) {
smblib_err(chg,
"Couldn't set otg current limit rc=%d\n", rc);
return rc;
}
}
smblib_dbg(chg, PR_OTG, "disabling OTG\n");
rc = smblib_write(chg, CMD_OTG_REG, 0);
if (rc < 0) {
smblib_err(chg, "Couldn't disable OTG regulator rc=%d\n", rc);
return rc;
}
smblib_dbg(chg, PR_OTG, "start 1 in 8 mode\n");
rc = smblib_masked_write(chg, OTG_ENG_OTG_CFG_REG,
ENG_BUCKBOOST_HALT1_8_MODE_BIT, 0);
if (rc < 0) {
smblib_err(chg, "Couldn't set OTG_ENG_OTG_CFG_REG rc=%d\n", rc);
return rc;
}
return 0;
}
int smblib_vbus_regulator_disable(struct regulator_dev *rdev)
{
struct smb_charger *chg = rdev_get_drvdata(rdev);
int rc = 0;
mutex_lock(&chg->otg_oc_lock);
if (!chg->otg_en)
goto unlock;
rc = _smblib_vbus_regulator_disable(rdev);
if (rc >= 0)
chg->otg_en = false;
if (chg->usb_icl_votable)
vote(chg->usb_icl_votable, USBIN_USBIN_BOOST_VOTER, false, 0);
unlock:
mutex_unlock(&chg->otg_oc_lock);
return rc;
}
int smblib_vbus_regulator_is_enabled(struct regulator_dev *rdev)
{
struct smb_charger *chg = rdev_get_drvdata(rdev);
int ret;
mutex_lock(&chg->otg_oc_lock);
ret = chg->otg_en;
mutex_unlock(&chg->otg_oc_lock);
return ret;
}
/********************
* BATT PSY GETTERS *
********************/
int smblib_get_prop_input_suspend(struct smb_charger *chg,
union power_supply_propval *val)
{
val->intval
= (get_client_vote(chg->usb_icl_votable, USER_VOTER) == 0)
&& get_client_vote(chg->dc_suspend_votable, USER_VOTER);
return 0;
}
int smblib_get_prop_batt_present(struct smb_charger *chg,
union power_supply_propval *val)
{
int rc;
u8 stat;
rc = smblib_read(chg, BATIF_BASE + INT_RT_STS_OFFSET, &stat);
if (rc < 0) {
smblib_err(chg, "Couldn't read BATIF_INT_RT_STS rc=%d\n", rc);
return rc;
}
val->intval = !(stat & (BAT_THERM_OR_ID_MISSING_RT_STS_BIT
| BAT_TERMINAL_MISSING_RT_STS_BIT));
return rc;
}
int smblib_get_prop_batt_capacity(struct smb_charger *chg,
union power_supply_propval *val)
{
int rc = -EINVAL;
if (chg->fake_capacity >= 0) {
val->intval = chg->fake_capacity;
return 0;
}
if (chg->bms_psy)
rc = power_supply_get_property(chg->bms_psy,
POWER_SUPPLY_PROP_CAPACITY, val);
return rc;
}
int smblib_get_prop_batt_status(struct smb_charger *chg,
union power_supply_propval *val)
{
union power_supply_propval pval = {0, };
bool usb_online, dc_online, qnovo_en;
u8 stat, pt_en_cmd;
int rc;
rc = smblib_get_prop_usb_online(chg, &pval);
if (rc < 0) {
smblib_err(chg, "Couldn't get usb online property rc=%d\n",
rc);
return rc;
}
usb_online = (bool)pval.intval;
rc = smblib_get_prop_dc_online(chg, &pval);
if (rc < 0) {
smblib_err(chg, "Couldn't get dc online property rc=%d\n",
rc);
return rc;
}
dc_online = (bool)pval.intval;
rc = smblib_read(chg, BATTERY_CHARGER_STATUS_1_REG, &stat);
if (rc < 0) {
smblib_err(chg, "Couldn't read BATTERY_CHARGER_STATUS_1 rc=%d\n",
rc);
return rc;
}
stat = stat & BATTERY_CHARGER_STATUS_MASK;
if (!usb_online && !dc_online) {
switch (stat) {
case TERMINATE_CHARGE:
case INHIBIT_CHARGE:
val->intval = POWER_SUPPLY_STATUS_FULL;
break;
default:
val->intval = POWER_SUPPLY_STATUS_DISCHARGING;
break;
}
return rc;
}
switch (stat) {
case TRICKLE_CHARGE:
case PRE_CHARGE:
case FAST_CHARGE:
case FULLON_CHARGE:
case TAPER_CHARGE:
val->intval = POWER_SUPPLY_STATUS_CHARGING;
break;
case TERMINATE_CHARGE:
case INHIBIT_CHARGE:
val->intval = POWER_SUPPLY_STATUS_FULL;
break;
case DISABLE_CHARGE:
val->intval = POWER_SUPPLY_STATUS_NOT_CHARGING;
break;
default:
val->intval = POWER_SUPPLY_STATUS_UNKNOWN;
break;
}
if (val->intval != POWER_SUPPLY_STATUS_CHARGING)
return 0;
rc = smblib_read(chg, BATTERY_CHARGER_STATUS_7_REG, &stat);
if (rc < 0) {
smblib_err(chg, "Couldn't read BATTERY_CHARGER_STATUS_2 rc=%d\n",
rc);
return rc;
}
stat &= ENABLE_TRICKLE_BIT | ENABLE_PRE_CHARGING_BIT |
ENABLE_FAST_CHARGING_BIT | ENABLE_FULLON_MODE_BIT;
rc = smblib_read(chg, QNOVO_PT_ENABLE_CMD_REG, &pt_en_cmd);
if (rc < 0) {
smblib_err(chg, "Couldn't read QNOVO_PT_ENABLE_CMD_REG rc=%d\n",
rc);
return rc;
}
qnovo_en = (bool)(pt_en_cmd & QNOVO_PT_ENABLE_CMD_BIT);
/* ignore stat7 when qnovo is enabled */
if (!qnovo_en && !stat)
val->intval = POWER_SUPPLY_STATUS_NOT_CHARGING;
return 0;
}
int smblib_get_prop_batt_charge_type(struct smb_charger *chg,
union power_supply_propval *val)
{
int rc;
u8 stat;
rc = smblib_read(chg, BATTERY_CHARGER_STATUS_1_REG, &stat);
if (rc < 0) {
smblib_err(chg, "Couldn't read BATTERY_CHARGER_STATUS_1 rc=%d\n",
rc);
return rc;
}
switch (stat & BATTERY_CHARGER_STATUS_MASK) {
case TRICKLE_CHARGE:
case PRE_CHARGE:
val->intval = POWER_SUPPLY_CHARGE_TYPE_TRICKLE;
break;
case FAST_CHARGE:
case FULLON_CHARGE:
val->intval = POWER_SUPPLY_CHARGE_TYPE_FAST;
break;
case TAPER_CHARGE:
val->intval = POWER_SUPPLY_CHARGE_TYPE_TAPER;
break;
default:
val->intval = POWER_SUPPLY_CHARGE_TYPE_NONE;
}
return rc;
}
int smblib_get_prop_batt_health(struct smb_charger *chg,
union power_supply_propval *val)
{
union power_supply_propval pval;
int rc;
int effective_fv_uv;
u8 stat;
rc = smblib_read(chg, BATTERY_CHARGER_STATUS_2_REG, &stat);
if (rc < 0) {
smblib_err(chg, "Couldn't read BATTERY_CHARGER_STATUS_2 rc=%d\n",
rc);
return rc;
}
smblib_dbg(chg, PR_REGISTER, "BATTERY_CHARGER_STATUS_2 = 0x%02x\n",
stat);
if (stat & CHARGER_ERROR_STATUS_BAT_OV_BIT) {
rc = smblib_get_prop_from_bms(chg,
POWER_SUPPLY_PROP_VOLTAGE_NOW, &pval);
if (!rc) {
/*
* If Vbatt is within 40mV above Vfloat, then don't
* treat it as overvoltage.
*/
effective_fv_uv = get_effective_result(chg->fv_votable);
if (pval.intval >= effective_fv_uv + 40000) {
val->intval = POWER_SUPPLY_HEALTH_OVERVOLTAGE;
smblib_err(chg, "battery over-voltage vbat_fg = %duV, fv = %duV\n",
pval.intval, effective_fv_uv);
goto done;
}
}
}
if (stat & BAT_TEMP_STATUS_TOO_COLD_BIT)
val->intval = POWER_SUPPLY_HEALTH_COLD;
else if (stat & BAT_TEMP_STATUS_TOO_HOT_BIT)
val->intval = POWER_SUPPLY_HEALTH_OVERHEAT;
else if (stat & BAT_TEMP_STATUS_COLD_SOFT_LIMIT_BIT)
val->intval = POWER_SUPPLY_HEALTH_COOL;
else if (stat & BAT_TEMP_STATUS_HOT_SOFT_LIMIT_BIT)
val->intval = POWER_SUPPLY_HEALTH_WARM;
else
val->intval = POWER_SUPPLY_HEALTH_GOOD;
done:
return rc;
}
int smblib_get_prop_system_temp_level(struct smb_charger *chg,
union power_supply_propval *val)
{
val->intval = chg->system_temp_level;
return 0;
}
int smblib_get_prop_input_current_limited(struct smb_charger *chg,
union power_supply_propval *val)
{
u8 stat;
int rc;
if (chg->fake_input_current_limited >= 0) {
val->intval = chg->fake_input_current_limited;
return 0;
}
rc = smblib_read(chg, AICL_STATUS_REG, &stat);
if (rc < 0) {
smblib_err(chg, "Couldn't read AICL_STATUS rc=%d\n", rc);
return rc;
}
val->intval = (stat & SOFT_ILIMIT_BIT) || chg->is_hdc;
return 0;
}
int smblib_get_prop_batt_charge_done(struct smb_charger *chg,
union power_supply_propval *val)
{
int rc;
u8 stat;
rc = smblib_read(chg, BATTERY_CHARGER_STATUS_1_REG, &stat);
if (rc < 0) {
smblib_err(chg, "Couldn't read BATTERY_CHARGER_STATUS_1 rc=%d\n",
rc);
return rc;
}
stat = stat & BATTERY_CHARGER_STATUS_MASK;
val->intval = (stat == TERMINATE_CHARGE);
return 0;
}
int smblib_get_prop_charge_qnovo_enable(struct smb_charger *chg,
union power_supply_propval *val)
{
int rc;
u8 stat;
rc = smblib_read(chg, QNOVO_PT_ENABLE_CMD_REG, &stat);
if (rc < 0) {
smblib_err(chg, "Couldn't read QNOVO_PT_ENABLE_CMD rc=%d\n",
rc);
return rc;
}
val->intval = (bool)(stat & QNOVO_PT_ENABLE_CMD_BIT);
return 0;
}
int smblib_get_prop_from_bms(struct smb_charger *chg,
enum power_supply_property psp,
union power_supply_propval *val)
{
int rc;
if (!chg->bms_psy)
return -EINVAL;
rc = power_supply_get_property(chg->bms_psy, psp, val);
return rc;
}
/***********************
* BATTERY PSY SETTERS *
***********************/
int smblib_set_prop_input_suspend(struct smb_charger *chg,
const union power_supply_propval *val)
{
int rc;
/* vote 0mA when suspended */
rc = vote(chg->usb_icl_votable, USER_VOTER, (bool)val->intval, 0);
if (rc < 0) {
smblib_err(chg, "Couldn't vote to %s USB rc=%d\n",
(bool)val->intval ? "suspend" : "resume", rc);
return rc;
}
rc = vote(chg->dc_suspend_votable, USER_VOTER, (bool)val->intval, 0);
if (rc < 0) {
smblib_err(chg, "Couldn't vote to %s DC rc=%d\n",
(bool)val->intval ? "suspend" : "resume", rc);
return rc;
}
power_supply_changed(chg->batt_psy);
return rc;
}
int smblib_set_prop_batt_capacity(struct smb_charger *chg,
const union power_supply_propval *val)
{
chg->fake_capacity = val->intval;
power_supply_changed(chg->batt_psy);
return 0;
}
int smblib_set_prop_system_temp_level(struct smb_charger *chg,
const union power_supply_propval *val)
{
if (val->intval < 0)
return -EINVAL;
if (chg->thermal_levels <= 0)
return -EINVAL;
if (val->intval > chg->thermal_levels)
return -EINVAL;
chg->system_temp_level = val->intval;
/* disable parallel charge in case of system temp level */
vote(chg->pl_disable_votable, THERMAL_DAEMON_VOTER,
chg->system_temp_level ? true : false, 0);
if (chg->system_temp_level == chg->thermal_levels)
return vote(chg->chg_disable_votable,
THERMAL_DAEMON_VOTER, true, 0);
vote(chg->chg_disable_votable, THERMAL_DAEMON_VOTER, false, 0);
if (chg->system_temp_level == 0)
return vote(chg->fcc_votable, THERMAL_DAEMON_VOTER, false, 0);
vote(chg->fcc_votable, THERMAL_DAEMON_VOTER, true,
chg->thermal_mitigation[chg->system_temp_level]);
return 0;
}
int smblib_set_prop_charge_qnovo_enable(struct smb_charger *chg,
const union power_supply_propval *val)
{
int rc = 0;
rc = smblib_masked_write(chg, QNOVO_PT_ENABLE_CMD_REG,
QNOVO_PT_ENABLE_CMD_BIT,
val->intval ? QNOVO_PT_ENABLE_CMD_BIT : 0);
if (rc < 0) {
dev_err(chg->dev, "Couldn't enable qnovo rc=%d\n", rc);
return rc;
}
return rc;
}
int smblib_set_prop_input_current_limited(struct smb_charger *chg,
const union power_supply_propval *val)
{
chg->fake_input_current_limited = val->intval;
return 0;
}
int smblib_rerun_aicl(struct smb_charger *chg)
{
int rc, settled_icl_ua;
u8 stat;
rc = smblib_read(chg, POWER_PATH_STATUS_REG, &stat);
if (rc < 0) {
smblib_err(chg, "Couldn't read POWER_PATH_STATUS rc=%d\n",
rc);
return rc;
}
/* USB is suspended so skip re-running AICL */
if (stat & USBIN_SUSPEND_STS_BIT)
return rc;
smblib_dbg(chg, PR_MISC, "re-running AICL\n");
rc = smblib_get_charge_param(chg, &chg->param.icl_stat,
&settled_icl_ua);
if (rc < 0) {
smblib_err(chg, "Couldn't get settled ICL rc=%d\n", rc);
return rc;
}
vote(chg->usb_icl_votable, AICL_RERUN_VOTER, true,
max(settled_icl_ua - chg->param.usb_icl.step_u,
chg->param.usb_icl.step_u));
vote(chg->usb_icl_votable, AICL_RERUN_VOTER, false, 0);
return 0;
}
static int smblib_dp_pulse(struct smb_charger *chg)
{
int rc;
/* QC 3.0 increment */
rc = smblib_masked_write(chg, CMD_HVDCP_2_REG, SINGLE_INCREMENT_BIT,
SINGLE_INCREMENT_BIT);
if (rc < 0)
smblib_err(chg, "Couldn't write to CMD_HVDCP_2_REG rc=%d\n",
rc);
return rc;
}
static int smblib_dm_pulse(struct smb_charger *chg)
{
int rc;
/* QC 3.0 decrement */
rc = smblib_masked_write(chg, CMD_HVDCP_2_REG, SINGLE_DECREMENT_BIT,
SINGLE_DECREMENT_BIT);
if (rc < 0)
smblib_err(chg, "Couldn't write to CMD_HVDCP_2_REG rc=%d\n",
rc);
return rc;
}
static int smblib_force_vbus_voltage(struct smb_charger *chg, u8 val)
{
int rc;
rc = smblib_masked_write(chg, CMD_HVDCP_2_REG, val, val);
if (rc < 0)
smblib_err(chg, "Couldn't write to CMD_HVDCP_2_REG rc=%d\n",
rc);
return rc;
}
int smblib_dp_dm(struct smb_charger *chg, int val)
{
int target_icl_ua, rc = 0;
union power_supply_propval pval;
switch (val) {
case POWER_SUPPLY_DP_DM_DP_PULSE:
rc = smblib_dp_pulse(chg);
if (!rc)
chg->pulse_cnt++;
smblib_dbg(chg, PR_PARALLEL, "DP_DM_DP_PULSE rc=%d cnt=%d\n",
rc, chg->pulse_cnt);
break;
case POWER_SUPPLY_DP_DM_DM_PULSE:
rc = smblib_dm_pulse(chg);
if (!rc && chg->pulse_cnt)
chg->pulse_cnt--;
smblib_dbg(chg, PR_PARALLEL, "DP_DM_DM_PULSE rc=%d cnt=%d\n",
rc, chg->pulse_cnt);
break;
case POWER_SUPPLY_DP_DM_ICL_DOWN:
target_icl_ua = get_effective_result(chg->usb_icl_votable);
if (target_icl_ua < 0) {
/* no client vote, get the ICL from charger */
rc = power_supply_get_property(chg->usb_psy,
POWER_SUPPLY_PROP_HW_CURRENT_MAX,
&pval);
if (rc < 0) {
smblib_err(chg,
"Couldn't get max current rc=%d\n",
rc);
return rc;
}
target_icl_ua = pval.intval;
}
/*
* Check if any other voter voted on USB_ICL in case of
* voter other than SW_QC3_VOTER reset and restart reduction
* again.
*/
if (target_icl_ua != get_client_vote(chg->usb_icl_votable,
SW_QC3_VOTER))
chg->usb_icl_delta_ua = 0;
chg->usb_icl_delta_ua += 100000;
vote(chg->usb_icl_votable, SW_QC3_VOTER, true,
target_icl_ua - 100000);
smblib_dbg(chg, PR_PARALLEL, "ICL DOWN ICL=%d reduction=%d\n",
target_icl_ua, chg->usb_icl_delta_ua);
break;
case POWER_SUPPLY_DP_DM_FORCE_5V:
rc = smblib_force_vbus_voltage(chg, FORCE_5V_BIT);
if (rc < 0)
pr_err("Failed to force 5V\n");
break;
case POWER_SUPPLY_DP_DM_FORCE_9V:
rc = smblib_force_vbus_voltage(chg, FORCE_9V_BIT);
if (rc < 0)
pr_err("Failed to force 9V\n");
break;
case POWER_SUPPLY_DP_DM_FORCE_12V:
rc = smblib_force_vbus_voltage(chg, FORCE_12V_BIT);
if (rc < 0)
pr_err("Failed to force 12V\n");
break;
case POWER_SUPPLY_DP_DM_ICL_UP:
default:
break;
}
return rc;
}
int smblib_disable_hw_jeita(struct smb_charger *chg, bool disable)
{
int rc;
u8 mask;
/*
* Disable h/w base JEITA compensation if s/w JEITA is enabled
*/
mask = JEITA_EN_COLD_SL_FCV_BIT
| JEITA_EN_HOT_SL_FCV_BIT
| JEITA_EN_HOT_SL_CCC_BIT
| JEITA_EN_COLD_SL_CCC_BIT,
rc = smblib_masked_write(chg, JEITA_EN_CFG_REG, mask,
disable ? 0 : mask);
if (rc < 0) {
dev_err(chg->dev,
"Couldn't configure s/w jeita rc=%d\n",
rc);
return rc;
}
return 0;
}
/*******************
* DC PSY GETTERS *
*******************/
int smblib_get_prop_dc_present(struct smb_charger *chg,
union power_supply_propval *val)
{
int rc;
u8 stat;
rc = smblib_read(chg, DCIN_BASE + INT_RT_STS_OFFSET, &stat);
if (rc < 0) {
smblib_err(chg, "Couldn't read DCIN_RT_STS rc=%d\n", rc);
return rc;
}
val->intval = (bool)(stat & DCIN_PLUGIN_RT_STS_BIT);
return 0;
}
int smblib_get_prop_dc_online(struct smb_charger *chg,
union power_supply_propval *val)
{
int rc = 0;
u8 stat;
if (get_client_vote(chg->dc_suspend_votable, USER_VOTER)) {
val->intval = false;
return rc;
}
rc = smblib_read(chg, POWER_PATH_STATUS_REG, &stat);
if (rc < 0) {
smblib_err(chg, "Couldn't read POWER_PATH_STATUS rc=%d\n",
rc);
return rc;
}
smblib_dbg(chg, PR_REGISTER, "POWER_PATH_STATUS = 0x%02x\n",
stat);
val->intval = (stat & USE_DCIN_BIT) &&
(stat & VALID_INPUT_POWER_SOURCE_STS_BIT);
return rc;
}
int smblib_get_prop_dc_current_max(struct smb_charger *chg,
union power_supply_propval *val)
{
val->intval = get_effective_result_locked(chg->dc_icl_votable);
return 0;
}
/*******************
* DC PSY SETTERS *
* *****************/
int smblib_set_prop_dc_current_max(struct smb_charger *chg,
const union power_supply_propval *val)
{
int rc;
rc = vote(chg->dc_icl_votable, USER_VOTER, true, val->intval);
return rc;
}
/*******************
* USB PSY GETTERS *
*******************/
int smblib_get_prop_usb_present(struct smb_charger *chg,
union power_supply_propval *val)
{
int rc;
u8 stat;
rc = smblib_read(chg, USBIN_BASE + INT_RT_STS_OFFSET, &stat);
if (rc < 0) {
smblib_err(chg, "Couldn't read USBIN_RT_STS rc=%d\n", rc);
return rc;
}
val->intval = (bool)(stat & USBIN_PLUGIN_RT_STS_BIT);
return 0;
}
int smblib_get_prop_usb_online(struct smb_charger *chg,
union power_supply_propval *val)
{
int rc = 0;
u8 stat;
if (get_client_vote_locked(chg->usb_icl_votable, USER_VOTER) == 0) {
val->intval = false;
return rc;
}
rc = smblib_read(chg, POWER_PATH_STATUS_REG, &stat);
if (rc < 0) {
smblib_err(chg, "Couldn't read POWER_PATH_STATUS rc=%d\n",
rc);
return rc;
}
smblib_dbg(chg, PR_REGISTER, "POWER_PATH_STATUS = 0x%02x\n",
stat);
val->intval = (stat & USE_USBIN_BIT) &&
(stat & VALID_INPUT_POWER_SOURCE_STS_BIT);
return rc;
}
int smblib_get_prop_usb_voltage_max(struct smb_charger *chg,
union power_supply_propval *val)
{
switch (chg->real_charger_type) {
case POWER_SUPPLY_TYPE_USB_HVDCP:
case POWER_SUPPLY_TYPE_USB_HVDCP_3:
case POWER_SUPPLY_TYPE_USB_PD:
if (chg->smb_version == PM660_SUBTYPE)
val->intval = MICRO_9V;
else
val->intval = MICRO_12V;
break;
default:
val->intval = MICRO_5V;
break;
}
return 0;
}
int smblib_get_prop_usb_voltage_now(struct smb_charger *chg,
union power_supply_propval *val)
{
if (!chg->iio.usbin_v_chan ||
PTR_ERR(chg->iio.usbin_v_chan) == -EPROBE_DEFER)
chg->iio.usbin_v_chan = iio_channel_get(chg->dev, "usbin_v");
if (IS_ERR(chg->iio.usbin_v_chan))
return PTR_ERR(chg->iio.usbin_v_chan);
return iio_read_channel_processed(chg->iio.usbin_v_chan, &val->intval);
}
int smblib_get_prop_usb_current_now(struct smb_charger *chg,
union power_supply_propval *val)
{
int rc = 0;
rc = smblib_get_prop_usb_present(chg, val);
if (rc < 0 || !val->intval)
return rc;
if (!chg->iio.usbin_i_chan ||
PTR_ERR(chg->iio.usbin_i_chan) == -EPROBE_DEFER)
chg->iio.usbin_i_chan = iio_channel_get(chg->dev, "usbin_i");
if (IS_ERR(chg->iio.usbin_i_chan))
return PTR_ERR(chg->iio.usbin_i_chan);
return iio_read_channel_processed(chg->iio.usbin_i_chan, &val->intval);
}
int smblib_get_prop_charger_temp(struct smb_charger *chg,
union power_supply_propval *val)
{
int rc;
if (!chg->iio.temp_chan ||
PTR_ERR(chg->iio.temp_chan) == -EPROBE_DEFER)
chg->iio.temp_chan = iio_channel_get(chg->dev, "charger_temp");
if (IS_ERR(chg->iio.temp_chan))
return PTR_ERR(chg->iio.temp_chan);
rc = iio_read_channel_processed(chg->iio.temp_chan, &val->intval);
val->intval /= 100;
return rc;
}
int smblib_get_prop_charger_temp_max(struct smb_charger *chg,
union power_supply_propval *val)
{
int rc;
if (!chg->iio.temp_max_chan ||
PTR_ERR(chg->iio.temp_max_chan) == -EPROBE_DEFER)
chg->iio.temp_max_chan = iio_channel_get(chg->dev,
"charger_temp_max");
if (IS_ERR(chg->iio.temp_max_chan))
return PTR_ERR(chg->iio.temp_max_chan);
rc = iio_read_channel_processed(chg->iio.temp_max_chan, &val->intval);
val->intval /= 100;
return rc;
}
int smblib_get_prop_typec_cc_orientation(struct smb_charger *chg,
union power_supply_propval *val)
{
if (chg->typec_status[3] & CC_ATTACHED_BIT)
val->intval =
(bool)(chg->typec_status[3] & CC_ORIENTATION_BIT) + 1;
else
val->intval = 0;
return 0;
}
static const char * const smblib_typec_mode_name[] = {
[POWER_SUPPLY_TYPEC_NONE] = "NONE",
[POWER_SUPPLY_TYPEC_SOURCE_DEFAULT] = "SOURCE_DEFAULT",
[POWER_SUPPLY_TYPEC_SOURCE_MEDIUM] = "SOURCE_MEDIUM",
[POWER_SUPPLY_TYPEC_SOURCE_HIGH] = "SOURCE_HIGH",
[POWER_SUPPLY_TYPEC_NON_COMPLIANT] = "NON_COMPLIANT",
[POWER_SUPPLY_TYPEC_SINK] = "SINK",
[POWER_SUPPLY_TYPEC_SINK_POWERED_CABLE] = "SINK_POWERED_CABLE",
[POWER_SUPPLY_TYPEC_SINK_DEBUG_ACCESSORY] = "SINK_DEBUG_ACCESSORY",
[POWER_SUPPLY_TYPEC_SINK_AUDIO_ADAPTER] = "SINK_AUDIO_ADAPTER",
[POWER_SUPPLY_TYPEC_POWERED_CABLE_ONLY] = "POWERED_CABLE_ONLY",
};
static int smblib_get_prop_ufp_mode(struct smb_charger *chg)
{
switch (chg->typec_status[0]) {
case UFP_TYPEC_RDSTD_BIT:
return POWER_SUPPLY_TYPEC_SOURCE_DEFAULT;
case UFP_TYPEC_RD1P5_BIT:
return POWER_SUPPLY_TYPEC_SOURCE_MEDIUM;
case UFP_TYPEC_RD3P0_BIT:
return POWER_SUPPLY_TYPEC_SOURCE_HIGH;
default:
break;
}
return POWER_SUPPLY_TYPEC_NONE;
}
static int smblib_get_prop_dfp_mode(struct smb_charger *chg)
{
switch (chg->typec_status[1] & DFP_TYPEC_MASK) {
case DFP_RA_RA_BIT:
return POWER_SUPPLY_TYPEC_SINK_AUDIO_ADAPTER;
case DFP_RD_RD_BIT:
return POWER_SUPPLY_TYPEC_SINK_DEBUG_ACCESSORY;
case DFP_RD_RA_VCONN_BIT:
return POWER_SUPPLY_TYPEC_SINK_POWERED_CABLE;
case DFP_RD_OPEN_BIT:
return POWER_SUPPLY_TYPEC_SINK;
default:
break;
}
return POWER_SUPPLY_TYPEC_NONE;
}
static int smblib_get_prop_typec_mode(struct smb_charger *chg)
{
if (chg->typec_status[3] & UFP_DFP_MODE_STATUS_BIT)
return smblib_get_prop_dfp_mode(chg);
else
return smblib_get_prop_ufp_mode(chg);
}
int smblib_get_prop_typec_power_role(struct smb_charger *chg,
union power_supply_propval *val)
{
int rc = 0;
u8 ctrl;
rc = smblib_read(chg, TYPE_C_INTRPT_ENB_SOFTWARE_CTRL_REG, &ctrl);
if (rc < 0) {
smblib_err(chg, "Couldn't read TYPE_C_INTRPT_ENB_SOFTWARE_CTRL rc=%d\n",
rc);
return rc;
}
smblib_dbg(chg, PR_REGISTER, "TYPE_C_INTRPT_ENB_SOFTWARE_CTRL = 0x%02x\n",
ctrl);
if (ctrl & TYPEC_DISABLE_CMD_BIT) {
val->intval = POWER_SUPPLY_TYPEC_PR_NONE;
return rc;
}
switch (ctrl & (DFP_EN_CMD_BIT | UFP_EN_CMD_BIT)) {
case 0:
val->intval = POWER_SUPPLY_TYPEC_PR_DUAL;
break;
case DFP_EN_CMD_BIT:
val->intval = POWER_SUPPLY_TYPEC_PR_SOURCE;
break;
case UFP_EN_CMD_BIT:
val->intval = POWER_SUPPLY_TYPEC_PR_SINK;
break;
default:
val->intval = POWER_SUPPLY_TYPEC_PR_NONE;
smblib_err(chg, "unsupported power role 0x%02lx\n",
ctrl & (DFP_EN_CMD_BIT | UFP_EN_CMD_BIT));
return -EINVAL;
}
return rc;
}
int smblib_get_prop_pd_allowed(struct smb_charger *chg,
union power_supply_propval *val)
{
val->intval = get_effective_result(chg->pd_allowed_votable);
return 0;
}
int smblib_get_prop_input_current_settled(struct smb_charger *chg,
union power_supply_propval *val)
{
return smblib_get_charge_param(chg, &chg->param.icl_stat, &val->intval);
}
#define HVDCP3_STEP_UV 200000
int smblib_get_prop_input_voltage_settled(struct smb_charger *chg,
union power_supply_propval *val)
{
int rc, pulses;
switch (chg->real_charger_type) {
case POWER_SUPPLY_TYPE_USB_HVDCP_3:
rc = smblib_get_pulse_cnt(chg, &pulses);
if (rc < 0) {
smblib_err(chg,
"Couldn't read QC_PULSE_COUNT rc=%d\n", rc);
return 0;
}
val->intval = MICRO_5V + HVDCP3_STEP_UV * pulses;
break;
case POWER_SUPPLY_TYPE_USB_PD:
val->intval = chg->voltage_min_uv;
break;
default:
val->intval = MICRO_5V;
break;
}
return 0;
}
int smblib_get_prop_pd_in_hard_reset(struct smb_charger *chg,
union power_supply_propval *val)
{
val->intval = chg->pd_hard_reset;
return 0;
}
int smblib_get_pe_start(struct smb_charger *chg,
union power_supply_propval *val)
{
/*
* hvdcp timeout voter is the last one to allow pd. Use its vote
* to indicate start of pe engine
*/
val->intval
= !get_client_vote_locked(chg->pd_disallowed_votable_indirect,
HVDCP_TIMEOUT_VOTER);
return 0;
}
int smblib_get_prop_die_health(struct smb_charger *chg,
union power_supply_propval *val)
{
int rc;
u8 stat;
rc = smblib_read(chg, TEMP_RANGE_STATUS_REG, &stat);
if (rc < 0) {
smblib_err(chg, "Couldn't read TEMP_RANGE_STATUS_REG rc=%d\n",
rc);
return rc;
}
/* TEMP_RANGE bits are mutually exclusive */
switch (stat & TEMP_RANGE_MASK) {
case TEMP_BELOW_RANGE_BIT:
val->intval = POWER_SUPPLY_HEALTH_COOL;
break;
case TEMP_WITHIN_RANGE_BIT:
val->intval = POWER_SUPPLY_HEALTH_WARM;
break;
case TEMP_ABOVE_RANGE_BIT:
val->intval = POWER_SUPPLY_HEALTH_HOT;
break;
case ALERT_LEVEL_BIT:
val->intval = POWER_SUPPLY_HEALTH_OVERHEAT;
break;
default:
val->intval = POWER_SUPPLY_HEALTH_UNKNOWN;
}
return 0;
}
#define SDP_CURRENT_UA 500000
#define CDP_CURRENT_UA 1500000
#define DCP_CURRENT_UA 1500000
#define HVDCP_CURRENT_UA 3000000
#define TYPEC_DEFAULT_CURRENT_UA 900000
#define TYPEC_MEDIUM_CURRENT_UA 1500000
#define TYPEC_HIGH_CURRENT_UA 3000000
static int get_rp_based_dcp_current(struct smb_charger *chg, int typec_mode)
{
int rp_ua;
switch (typec_mode) {
case POWER_SUPPLY_TYPEC_SOURCE_HIGH:
rp_ua = TYPEC_HIGH_CURRENT_UA;
break;
case POWER_SUPPLY_TYPEC_SOURCE_MEDIUM:
case POWER_SUPPLY_TYPEC_SOURCE_DEFAULT:
/* fall through */
default:
rp_ua = DCP_CURRENT_UA;
}
return rp_ua;
}
/*******************
* USB PSY SETTERS *
* *****************/
int smblib_set_prop_pd_current_max(struct smb_charger *chg,
const union power_supply_propval *val)
{
int rc;
if (chg->pd_active)
rc = vote(chg->usb_icl_votable, PD_VOTER, true, val->intval);
else
rc = -EPERM;
return rc;
}
static int smblib_handle_usb_current(struct smb_charger *chg,
int usb_current)
{
int rc = 0, rp_ua, typec_mode;
if (chg->real_charger_type == POWER_SUPPLY_TYPE_USB_FLOAT) {
if (usb_current == -ETIMEDOUT) {
/*
* Valid FLOAT charger, report the current based
* of Rp
*/
typec_mode = smblib_get_prop_typec_mode(chg);
rp_ua = get_rp_based_dcp_current(chg, typec_mode);
rc = vote(chg->usb_icl_votable, LEGACY_UNKNOWN_VOTER,
true, rp_ua);
if (rc < 0)
return rc;
} else {
/*
* FLOAT charger detected as SDP by USB driver,
* charge with the requested current and update the
* real_charger_type
*/
chg->real_charger_type = POWER_SUPPLY_TYPE_USB;
rc = vote(chg->usb_icl_votable, USB_PSY_VOTER,
true, usb_current);
if (rc < 0)
return rc;
rc = vote(chg->usb_icl_votable, LEGACY_UNKNOWN_VOTER,
false, 0);
if (rc < 0)
return rc;
}
} else {
rc = vote(chg->usb_icl_votable, USB_PSY_VOTER,
true, usb_current);
}
return rc;
}
int smblib_set_prop_sdp_current_max(struct smb_charger *chg,
const union power_supply_propval *val)
{
int rc = 0;
if (!chg->pd_active) {
rc = smblib_handle_usb_current(chg, val->intval);
} else if (chg->system_suspend_supported) {
if (val->intval <= USBIN_25MA)
rc = vote(chg->usb_icl_votable,
PD_SUSPEND_SUPPORTED_VOTER, true, val->intval);
else
rc = vote(chg->usb_icl_votable,
PD_SUSPEND_SUPPORTED_VOTER, false, 0);
}
return rc;
}
int smblib_set_prop_boost_current(struct smb_charger *chg,
const union power_supply_propval *val)
{
int rc = 0;
rc = smblib_set_charge_param(chg, &chg->param.freq_boost,
val->intval <= chg->boost_threshold_ua ?
chg->chg_freq.freq_below_otg_threshold :
chg->chg_freq.freq_above_otg_threshold);
if (rc < 0) {
dev_err(chg->dev, "Error in setting freq_boost rc=%d\n", rc);
return rc;
}
chg->boost_current_ua = val->intval;
return rc;
}
int smblib_set_prop_typec_power_role(struct smb_charger *chg,
const union power_supply_propval *val)
{
int rc = 0;
u8 power_role;
switch (val->intval) {
case POWER_SUPPLY_TYPEC_PR_NONE:
power_role = TYPEC_DISABLE_CMD_BIT;
break;
case POWER_SUPPLY_TYPEC_PR_DUAL:
power_role = 0;
break;
case POWER_SUPPLY_TYPEC_PR_SINK:
power_role = UFP_EN_CMD_BIT;
break;
case POWER_SUPPLY_TYPEC_PR_SOURCE:
power_role = DFP_EN_CMD_BIT;
break;
default:
smblib_err(chg, "power role %d not supported\n", val->intval);
return -EINVAL;
}
if (chg->wa_flags & TYPEC_PBS_WA_BIT) {
if (power_role == UFP_EN_CMD_BIT) {
/* disable PBS workaround when forcing sink mode */
rc = smblib_write(chg, TM_IO_DTEST4_SEL, 0x0);
if (rc < 0) {
smblib_err(chg, "Couldn't write to TM_IO_DTEST4_SEL rc=%d\n",
rc);
}
} else {
/* restore it back to 0xA5 */
rc = smblib_write(chg, TM_IO_DTEST4_SEL, 0xA5);
if (rc < 0) {
smblib_err(chg, "Couldn't write to TM_IO_DTEST4_SEL rc=%d\n",
rc);
}
}
}
rc = smblib_masked_write(chg, TYPE_C_INTRPT_ENB_SOFTWARE_CTRL_REG,
TYPEC_POWER_ROLE_CMD_MASK, power_role);
if (rc < 0) {
smblib_err(chg, "Couldn't write 0x%02x to TYPE_C_INTRPT_ENB_SOFTWARE_CTRL rc=%d\n",
power_role, rc);
return rc;
}
return rc;
}
int smblib_set_prop_pd_voltage_min(struct smb_charger *chg,
const union power_supply_propval *val)
{
int rc, min_uv;
min_uv = min(val->intval, chg->voltage_max_uv);
rc = smblib_set_usb_pd_allowed_voltage(chg, min_uv,
chg->voltage_max_uv);
if (rc < 0) {
smblib_err(chg, "invalid max voltage %duV rc=%d\n",
val->intval, rc);
return rc;
}
chg->voltage_min_uv = min_uv;
power_supply_changed(chg->usb_main_psy);
return rc;
}
int smblib_set_prop_pd_voltage_max(struct smb_charger *chg,
const union power_supply_propval *val)
{
int rc, max_uv;
max_uv = max(val->intval, chg->voltage_min_uv);
rc = smblib_set_usb_pd_allowed_voltage(chg, chg->voltage_min_uv,
max_uv);
if (rc < 0) {
smblib_err(chg, "invalid min voltage %duV rc=%d\n",
val->intval, rc);
return rc;
}
chg->voltage_max_uv = max_uv;
return rc;
}
static int __smblib_set_prop_pd_active(struct smb_charger *chg, bool pd_active)
{
int rc;
bool orientation, sink_attached, hvdcp;
u8 stat;
chg->pd_active = pd_active;
if (chg->pd_active) {
vote(chg->apsd_disable_votable, PD_VOTER, true, 0);
vote(chg->pd_allowed_votable, PD_VOTER, true, 0);
vote(chg->usb_irq_enable_votable, PD_VOTER, true, 0);
/*
* VCONN_EN_ORIENTATION_BIT controls whether to use CC1 or CC2
* line when TYPEC_SPARE_CFG_BIT (CC pin selection s/w override)
* is set or when VCONN_EN_VALUE_BIT is set.
*/
orientation = chg->typec_status[3] & CC_ORIENTATION_BIT;
rc = smblib_masked_write(chg,
TYPE_C_INTRPT_ENB_SOFTWARE_CTRL_REG,
VCONN_EN_ORIENTATION_BIT,
orientation ? 0 : VCONN_EN_ORIENTATION_BIT);
if (rc < 0)
smblib_err(chg,
"Couldn't enable vconn on CC line rc=%d\n", rc);
/* SW controlled CC_OUT */
rc = smblib_masked_write(chg, TAPER_TIMER_SEL_CFG_REG,
TYPEC_SPARE_CFG_BIT, TYPEC_SPARE_CFG_BIT);
if (rc < 0)
smblib_err(chg, "Couldn't enable SW cc_out rc=%d\n",
rc);
/*
* Enforce 500mA for PD until the real vote comes in later.
* It is guaranteed that pd_active is set prior to
* pd_current_max
*/
rc = vote(chg->usb_icl_votable, PD_VOTER, true, USBIN_500MA);
if (rc < 0)
smblib_err(chg, "Couldn't vote for USB ICL rc=%d\n",
rc);
/* since PD was found the cable must be non-legacy */
vote(chg->usb_icl_votable, LEGACY_UNKNOWN_VOTER, false, 0);
/* clear USB ICL vote for DCP_VOTER */
rc = vote(chg->usb_icl_votable, DCP_VOTER, false, 0);
if (rc < 0)
smblib_err(chg, "Couldn't un-vote DCP from USB ICL rc=%d\n",
rc);
/* remove USB_PSY_VOTER */
rc = vote(chg->usb_icl_votable, USB_PSY_VOTER, false, 0);
if (rc < 0)
smblib_err(chg, "Couldn't unvote USB_PSY rc=%d\n", rc);
} else {
rc = smblib_read(chg, APSD_STATUS_REG, &stat);
if (rc < 0) {
smblib_err(chg, "Couldn't read APSD status rc=%d\n",
rc);
return rc;
}
hvdcp = stat & QC_CHARGER_BIT;
vote(chg->apsd_disable_votable, PD_VOTER, false, 0);
vote(chg->pd_allowed_votable, PD_VOTER, true, 0);
vote(chg->usb_irq_enable_votable, PD_VOTER, false, 0);
vote(chg->hvdcp_disable_votable_indirect, PD_INACTIVE_VOTER,
false, 0);
/* HW controlled CC_OUT */
rc = smblib_masked_write(chg, TAPER_TIMER_SEL_CFG_REG,
TYPEC_SPARE_CFG_BIT, 0);
if (rc < 0)
smblib_err(chg, "Couldn't enable HW cc_out rc=%d\n",
rc);
/*
* This WA should only run for HVDCP. Non-legacy SDP/CDP could
* draw more, but this WA will remove Rd causing VBUS to drop,
* and data could be interrupted. Non-legacy DCP could also draw
* more, but it may impact compliance.
*/
sink_attached = chg->typec_status[3] & UFP_DFP_MODE_STATUS_BIT;
if (!chg->typec_legacy_valid && !sink_attached && hvdcp)
schedule_work(&chg->legacy_detection_work);
}
smblib_update_usb_type(chg);
power_supply_changed(chg->usb_psy);
return rc;
}
int smblib_set_prop_pd_active(struct smb_charger *chg,
const union power_supply_propval *val)
{
if (!get_effective_result(chg->pd_allowed_votable))
return -EINVAL;
return __smblib_set_prop_pd_active(chg, val->intval);
}
int smblib_set_prop_ship_mode(struct smb_charger *chg,
const union power_supply_propval *val)
{
int rc;
smblib_dbg(chg, PR_MISC, "Set ship mode: %d!!\n", !!val->intval);
rc = smblib_masked_write(chg, SHIP_MODE_REG, SHIP_MODE_EN_BIT,
!!val->intval ? SHIP_MODE_EN_BIT : 0);
if (rc < 0)
dev_err(chg->dev, "Couldn't %s ship mode, rc=%d\n",
!!val->intval ? "enable" : "disable", rc);
return rc;
}
int smblib_reg_block_update(struct smb_charger *chg,
struct reg_info *entry)
{
int rc = 0;
while (entry && entry->reg) {
rc = smblib_read(chg, entry->reg, &entry->bak);
if (rc < 0) {
dev_err(chg->dev, "Error in reading %s rc=%d\n",
entry->desc, rc);
break;
}
entry->bak &= entry->mask;
rc = smblib_masked_write(chg, entry->reg,
entry->mask, entry->val);
if (rc < 0) {
dev_err(chg->dev, "Error in writing %s rc=%d\n",
entry->desc, rc);
break;
}
entry++;
}
return rc;
}
int smblib_reg_block_restore(struct smb_charger *chg,
struct reg_info *entry)
{
int rc = 0;
while (entry && entry->reg) {
rc = smblib_masked_write(chg, entry->reg,
entry->mask, entry->bak);
if (rc < 0) {
dev_err(chg->dev, "Error in writing %s rc=%d\n",
entry->desc, rc);
break;
}
entry++;
}
return rc;
}
static struct reg_info cc2_detach_settings[] = {
{
.reg = TYPE_C_CFG_2_REG,
.mask = TYPE_C_UFP_MODE_BIT | EN_TRY_SOURCE_MODE_BIT,
.val = TYPE_C_UFP_MODE_BIT,
.desc = "TYPE_C_CFG_2_REG",
},
{
.reg = TYPE_C_CFG_3_REG,
.mask = EN_TRYSINK_MODE_BIT,
.val = 0,
.desc = "TYPE_C_CFG_3_REG",
},
{
.reg = TAPER_TIMER_SEL_CFG_REG,
.mask = TYPEC_SPARE_CFG_BIT,
.val = TYPEC_SPARE_CFG_BIT,
.desc = "TAPER_TIMER_SEL_CFG_REG",
},
{
.reg = TYPE_C_INTRPT_ENB_SOFTWARE_CTRL_REG,
.mask = VCONN_EN_ORIENTATION_BIT,
.val = 0,
.desc = "TYPE_C_INTRPT_ENB_SOFTWARE_CTRL_REG",
},
{
.reg = MISC_CFG_REG,
.mask = TCC_DEBOUNCE_20MS_BIT,
.val = TCC_DEBOUNCE_20MS_BIT,
.desc = "Tccdebounce time"
},
{
},
};
static int smblib_cc2_sink_removal_enter(struct smb_charger *chg)
{
int rc, ccout, ufp_mode;
u8 stat;
if ((chg->wa_flags & TYPEC_CC2_REMOVAL_WA_BIT) == 0)
return 0;
if (chg->cc2_detach_wa_active)
return 0;
rc = smblib_read(chg, TYPE_C_STATUS_4_REG, &stat);
if (rc < 0) {
smblib_err(chg, "Couldn't read TYPE_C_STATUS_4 rc=%d\n", rc);
return rc;
}
ccout = (stat & CC_ATTACHED_BIT) ?
(!!(stat & CC_ORIENTATION_BIT) + 1) : 0;
ufp_mode = (stat & TYPEC_DEBOUNCE_DONE_STATUS_BIT) ?
!(stat & UFP_DFP_MODE_STATUS_BIT) : 0;
if (ccout != 2)
return 0;
if (!ufp_mode)
return 0;
chg->cc2_detach_wa_active = true;
/* The CC2 removal WA will cause a type-c-change IRQ storm */
smblib_reg_block_update(chg, cc2_detach_settings);
schedule_work(&chg->rdstd_cc2_detach_work);
return rc;
}
static int smblib_cc2_sink_removal_exit(struct smb_charger *chg)
{
if ((chg->wa_flags & TYPEC_CC2_REMOVAL_WA_BIT) == 0)
return 0;
if (!chg->cc2_detach_wa_active)
return 0;
chg->cc2_detach_wa_active = false;
cancel_work_sync(&chg->rdstd_cc2_detach_work);
smblib_reg_block_restore(chg, cc2_detach_settings);
return 0;
}
int smblib_set_prop_pd_in_hard_reset(struct smb_charger *chg,
const union power_supply_propval *val)
{
int rc = 0;
if (chg->pd_hard_reset == val->intval)
return rc;
chg->pd_hard_reset = val->intval;
rc = smblib_masked_write(chg, TYPE_C_INTRPT_ENB_SOFTWARE_CTRL_REG,
EXIT_SNK_BASED_ON_CC_BIT,
(chg->pd_hard_reset) ? EXIT_SNK_BASED_ON_CC_BIT : 0);
if (rc < 0)
smblib_err(chg, "Couldn't set EXIT_SNK_BASED_ON_CC rc=%d\n",
rc);
vote(chg->apsd_disable_votable, PD_HARD_RESET_VOTER,
chg->pd_hard_reset, 0);
return rc;
}
static int smblib_recover_from_soft_jeita(struct smb_charger *chg)
{
u8 stat_1, stat_2;
int rc;
rc = smblib_read(chg, BATTERY_CHARGER_STATUS_1_REG, &stat_1);
if (rc < 0) {
smblib_err(chg, "Couldn't read BATTERY_CHARGER_STATUS_1 rc=%d\n",
rc);
return rc;
}
rc = smblib_read(chg, BATTERY_CHARGER_STATUS_2_REG, &stat_2);
if (rc < 0) {
smblib_err(chg, "Couldn't read BATTERY_CHARGER_STATUS_2 rc=%d\n",
rc);
return rc;
}
if ((chg->jeita_status && !(stat_2 & BAT_TEMP_STATUS_SOFT_LIMIT_MASK) &&
((stat_1 & BATTERY_CHARGER_STATUS_MASK) == TERMINATE_CHARGE))) {
/*
* We are moving from JEITA soft -> Normal and charging
* is terminated
*/
rc = smblib_write(chg, CHARGING_ENABLE_CMD_REG, 0);
if (rc < 0) {
smblib_err(chg, "Couldn't disable charging rc=%d\n",
rc);
return rc;
}
rc = smblib_write(chg, CHARGING_ENABLE_CMD_REG,
CHARGING_ENABLE_CMD_BIT);
if (rc < 0) {
smblib_err(chg, "Couldn't enable charging rc=%d\n",
rc);
return rc;
}
}
chg->jeita_status = stat_2 & BAT_TEMP_STATUS_SOFT_LIMIT_MASK;
return 0;
}
/***********************
* USB MAIN PSY GETTERS *
*************************/
int smblib_get_prop_fcc_delta(struct smb_charger *chg,
union power_supply_propval *val)
{
int rc, jeita_cc_delta_ua = 0;
rc = smblib_get_jeita_cc_delta(chg, &jeita_cc_delta_ua);
if (rc < 0) {
smblib_err(chg, "Couldn't get jeita cc delta rc=%d\n", rc);
jeita_cc_delta_ua = 0;
}
val->intval = jeita_cc_delta_ua;
return 0;
}
/***********************
* USB MAIN PSY SETTERS *
*************************/
int smblib_get_charge_current(struct smb_charger *chg,
int *total_current_ua)
{
const struct apsd_result *apsd_result = smblib_get_apsd_result(chg);
union power_supply_propval val = {0, };
int rc = 0, typec_source_rd, current_ua;
bool non_compliant;
u8 stat5;
if (chg->pd_active) {
*total_current_ua =
get_client_vote_locked(chg->usb_icl_votable, PD_VOTER);
return rc;
}
rc = smblib_read(chg, TYPE_C_STATUS_5_REG, &stat5);
if (rc < 0) {
smblib_err(chg, "Couldn't read TYPE_C_STATUS_5 rc=%d\n", rc);
return rc;
}
non_compliant = stat5 & TYPEC_NONCOMP_LEGACY_CABLE_STATUS_BIT;
/* get settled ICL */
rc = smblib_get_prop_input_current_settled(chg, &val);
if (rc < 0) {
smblib_err(chg, "Couldn't get settled ICL rc=%d\n", rc);
return rc;
}
typec_source_rd = smblib_get_prop_ufp_mode(chg);
/* QC 2.0/3.0 adapter */
if (apsd_result->bit & (QC_3P0_BIT | QC_2P0_BIT)) {
*total_current_ua = HVDCP_CURRENT_UA;
return 0;
}
if (non_compliant) {
switch (apsd_result->bit) {
case CDP_CHARGER_BIT:
current_ua = CDP_CURRENT_UA;
break;
case DCP_CHARGER_BIT:
case OCP_CHARGER_BIT:
case FLOAT_CHARGER_BIT:
current_ua = DCP_CURRENT_UA;
break;
default:
current_ua = 0;
break;
}
*total_current_ua = max(current_ua, val.intval);
return 0;
}
switch (typec_source_rd) {
case POWER_SUPPLY_TYPEC_SOURCE_DEFAULT:
switch (apsd_result->bit) {
case CDP_CHARGER_BIT:
current_ua = CDP_CURRENT_UA;
break;
case DCP_CHARGER_BIT:
case OCP_CHARGER_BIT:
case FLOAT_CHARGER_BIT:
current_ua = chg->default_icl_ua;
break;
default:
current_ua = 0;
break;
}
break;
case POWER_SUPPLY_TYPEC_SOURCE_MEDIUM:
current_ua = TYPEC_MEDIUM_CURRENT_UA;
break;
case POWER_SUPPLY_TYPEC_SOURCE_HIGH:
current_ua = TYPEC_HIGH_CURRENT_UA;
break;
case POWER_SUPPLY_TYPEC_NON_COMPLIANT:
case POWER_SUPPLY_TYPEC_NONE:
default:
current_ua = 0;
break;
}
*total_current_ua = max(current_ua, val.intval);
return 0;
}
/************************
* PARALLEL PSY GETTERS *
************************/
int smblib_get_prop_slave_current_now(struct smb_charger *chg,
union power_supply_propval *pval)
{
if (IS_ERR_OR_NULL(chg->iio.batt_i_chan))
chg->iio.batt_i_chan = iio_channel_get(chg->dev, "batt_i");
if (IS_ERR(chg->iio.batt_i_chan))
return PTR_ERR(chg->iio.batt_i_chan);
return iio_read_channel_processed(chg->iio.batt_i_chan, &pval->intval);
}
/**********************
* INTERRUPT HANDLERS *
**********************/
irqreturn_t smblib_handle_debug(int irq, void *data)
{
struct smb_irq_data *irq_data = data;
struct smb_charger *chg = irq_data->parent_data;
smblib_dbg(chg, PR_INTERRUPT, "IRQ: %s\n", irq_data->name);
return IRQ_HANDLED;
}
irqreturn_t smblib_handle_otg_overcurrent(int irq, void *data)
{
struct smb_irq_data *irq_data = data;
struct smb_charger *chg = irq_data->parent_data;
int rc;
u8 stat;
rc = smblib_read(chg, OTG_BASE + INT_RT_STS_OFFSET, &stat);
if (rc < 0) {
dev_err(chg->dev, "Couldn't read OTG_INT_RT_STS rc=%d\n", rc);
return IRQ_HANDLED;
}
if (chg->wa_flags & OTG_WA) {
if (stat & OTG_OC_DIS_SW_STS_RT_STS_BIT)
smblib_err(chg, "OTG disabled by hw\n");
/* not handling software based hiccups for PM660 */
return IRQ_HANDLED;
}
if (stat & OTG_OVERCURRENT_RT_STS_BIT)
schedule_work(&chg->otg_oc_work);
return IRQ_HANDLED;
}
irqreturn_t smblib_handle_chg_state_change(int irq, void *data)
{
struct smb_irq_data *irq_data = data;
struct smb_charger *chg = irq_data->parent_data;
u8 stat;
int rc;
smblib_dbg(chg, PR_INTERRUPT, "IRQ: %s\n", irq_data->name);
rc = smblib_read(chg, BATTERY_CHARGER_STATUS_1_REG, &stat);
if (rc < 0) {
smblib_err(chg, "Couldn't read BATTERY_CHARGER_STATUS_1 rc=%d\n",
rc);
return IRQ_HANDLED;
}
stat = stat & BATTERY_CHARGER_STATUS_MASK;
power_supply_changed(chg->batt_psy);
return IRQ_HANDLED;
}
irqreturn_t smblib_handle_batt_temp_changed(int irq, void *data)
{
struct smb_irq_data *irq_data = data;
struct smb_charger *chg = irq_data->parent_data;
int rc;
rc = smblib_recover_from_soft_jeita(chg);
if (rc < 0) {
smblib_err(chg, "Couldn't recover chg from soft jeita rc=%d\n",
rc);
return IRQ_HANDLED;
}
rerun_election(chg->fcc_votable);
power_supply_changed(chg->batt_psy);
return IRQ_HANDLED;
}
irqreturn_t smblib_handle_batt_psy_changed(int irq, void *data)
{
struct smb_irq_data *irq_data = data;
struct smb_charger *chg = irq_data->parent_data;
smblib_dbg(chg, PR_INTERRUPT, "IRQ: %s\n", irq_data->name);
power_supply_changed(chg->batt_psy);
return IRQ_HANDLED;
}
irqreturn_t smblib_handle_usb_psy_changed(int irq, void *data)
{
struct smb_irq_data *irq_data = data;
struct smb_charger *chg = irq_data->parent_data;
smblib_dbg(chg, PR_INTERRUPT, "IRQ: %s\n", irq_data->name);
power_supply_changed(chg->usb_psy);
return IRQ_HANDLED;
}
irqreturn_t smblib_handle_usbin_uv(int irq, void *data)
{
struct smb_irq_data *irq_data = data;
struct smb_charger *chg = irq_data->parent_data;
struct storm_watch *wdata;
smblib_dbg(chg, PR_INTERRUPT, "IRQ: %s\n", irq_data->name);
if (!chg->irq_info[SWITCH_POWER_OK_IRQ].irq_data)
return IRQ_HANDLED;
wdata = &chg->irq_info[SWITCH_POWER_OK_IRQ].irq_data->storm_data;
reset_storm_count(wdata);
return IRQ_HANDLED;
}
static void smblib_micro_usb_plugin(struct smb_charger *chg, bool vbus_rising)
{
if (vbus_rising) {
/* use the typec flag even though its not typec */
chg->typec_present = 1;
} else {
chg->typec_present = 0;
smblib_update_usb_type(chg);
extcon_set_cable_state_(chg->extcon, EXTCON_USB, false);
smblib_uusb_removal(chg);
}
}
void smblib_usb_plugin_hard_reset_locked(struct smb_charger *chg)
{
int rc;
u8 stat;
bool vbus_rising;
struct smb_irq_data *data;
struct storm_watch *wdata;
rc = smblib_read(chg, USBIN_BASE + INT_RT_STS_OFFSET, &stat);
if (rc < 0) {
smblib_err(chg, "Couldn't read USB_INT_RT_STS rc=%d\n", rc);
return;
}
vbus_rising = (bool)(stat & USBIN_PLUGIN_RT_STS_BIT);
if (vbus_rising) {
smblib_cc2_sink_removal_exit(chg);
} else {
smblib_cc2_sink_removal_enter(chg);
if (chg->wa_flags & BOOST_BACK_WA) {
data = chg->irq_info[SWITCH_POWER_OK_IRQ].irq_data;
if (data) {
wdata = &data->storm_data;
update_storm_count(wdata,
WEAK_CHG_STORM_COUNT);
vote(chg->usb_icl_votable, BOOST_BACK_VOTER,
false, 0);
vote(chg->usb_icl_votable, WEAK_CHARGER_VOTER,
false, 0);
}
}
}
power_supply_changed(chg->usb_psy);
smblib_dbg(chg, PR_INTERRUPT, "IRQ: usbin-plugin %s\n",
vbus_rising ? "attached" : "detached");
}
#define PL_DELAY_MS 30000
void smblib_usb_plugin_locked(struct smb_charger *chg)
{
int rc;
u8 stat;
bool vbus_rising;
struct smb_irq_data *data;
struct storm_watch *wdata;
rc = smblib_read(chg, USBIN_BASE + INT_RT_STS_OFFSET, &stat);
if (rc < 0) {
smblib_err(chg, "Couldn't read USB_INT_RT_STS rc=%d\n", rc);
return;
}
vbus_rising = (bool)(stat & USBIN_PLUGIN_RT_STS_BIT);
smblib_set_opt_freq_buck(chg, vbus_rising ? chg->chg_freq.freq_5V :
chg->chg_freq.freq_removal);
if (vbus_rising) {
rc = smblib_request_dpdm(chg, true);
if (rc < 0)
smblib_err(chg, "Couldn't to enable DPDM rc=%d\n", rc);
if (chg->fcc_stepper_mode)
vote(chg->fcc_votable, FCC_STEPPER_VOTER, false, 0);
/* Schedule work to enable parallel charger */
vote(chg->awake_votable, PL_DELAY_VOTER, true, 0);
schedule_delayed_work(&chg->pl_enable_work,
msecs_to_jiffies(PL_DELAY_MS));
} else {
if (chg->wa_flags & BOOST_BACK_WA) {
data = chg->irq_info[SWITCH_POWER_OK_IRQ].irq_data;
if (data) {
wdata = &data->storm_data;
update_storm_count(wdata,
WEAK_CHG_STORM_COUNT);
vote(chg->usb_icl_votable, BOOST_BACK_VOTER,
false, 0);
vote(chg->usb_icl_votable, WEAK_CHARGER_VOTER,
false, 0);
}
}
/* Force 1500mA FCC on removal */
if (chg->fcc_stepper_mode)
vote(chg->fcc_votable, FCC_STEPPER_VOTER,
true, 1500000);
rc = smblib_request_dpdm(chg, false);
if (rc < 0)
smblib_err(chg, "Couldn't disable DPDM rc=%d\n", rc);
}
if (chg->micro_usb_mode)
smblib_micro_usb_plugin(chg, vbus_rising);
power_supply_changed(chg->usb_psy);
smblib_dbg(chg, PR_INTERRUPT, "IRQ: usbin-plugin %s\n",
vbus_rising ? "attached" : "detached");
}
irqreturn_t smblib_handle_usb_plugin(int irq, void *data)
{
struct smb_irq_data *irq_data = data;
struct smb_charger *chg = irq_data->parent_data;
mutex_lock(&chg->lock);
if (chg->pd_hard_reset)
smblib_usb_plugin_hard_reset_locked(chg);
else
smblib_usb_plugin_locked(chg);
mutex_unlock(&chg->lock);
return IRQ_HANDLED;
}
#define USB_WEAK_INPUT_UA 1400000
#define ICL_CHANGE_DELAY_MS 1000
irqreturn_t smblib_handle_icl_change(int irq, void *data)
{
u8 stat;
int rc, settled_ua, delay = ICL_CHANGE_DELAY_MS;
struct smb_irq_data *irq_data = data;
struct smb_charger *chg = irq_data->parent_data;
if (chg->mode == PARALLEL_MASTER) {
rc = smblib_read(chg, AICL_STATUS_REG, &stat);
if (rc < 0) {
smblib_err(chg, "Couldn't read AICL_STATUS rc=%d\n",
rc);
return IRQ_HANDLED;
}
rc = smblib_get_charge_param(chg, &chg->param.icl_stat,
&settled_ua);
if (rc < 0) {
smblib_err(chg, "Couldn't get ICL status rc=%d\n", rc);
return IRQ_HANDLED;
}
/* If AICL settled then schedule work now */
if ((settled_ua == get_effective_result(chg->usb_icl_votable))
|| (stat & AICL_DONE_BIT))
delay = 0;
cancel_delayed_work_sync(&chg->icl_change_work);
schedule_delayed_work(&chg->icl_change_work,
msecs_to_jiffies(delay));
}
return IRQ_HANDLED;
}
static void smblib_handle_slow_plugin_timeout(struct smb_charger *chg,
bool rising)
{
smblib_dbg(chg, PR_INTERRUPT, "IRQ: slow-plugin-timeout %s\n",
rising ? "rising" : "falling");
}
static void smblib_handle_sdp_enumeration_done(struct smb_charger *chg,
bool rising)
{
smblib_dbg(chg, PR_INTERRUPT, "IRQ: sdp-enumeration-done %s\n",
rising ? "rising" : "falling");
}
#define MICRO_10P3V 10300000
static void smblib_check_ov_condition(struct smb_charger *chg)
{
union power_supply_propval pval = {0, };
int rc;
if (chg->wa_flags & OV_IRQ_WA_BIT) {
rc = power_supply_get_property(chg->usb_psy,
POWER_SUPPLY_PROP_VOLTAGE_NOW, &pval);
if (rc < 0) {
smblib_err(chg, "Couldn't get current voltage, rc=%d\n",
rc);
return;
}
if (pval.intval > MICRO_10P3V) {
smblib_err(chg, "USBIN OV detected\n");
vote(chg->hvdcp_hw_inov_dis_votable, OV_VOTER, true,
0);
pval.intval = POWER_SUPPLY_DP_DM_FORCE_5V;
rc = power_supply_set_property(chg->batt_psy,
POWER_SUPPLY_PROP_DP_DM, &pval);
return;
}
}
}
#define QC3_PULSES_FOR_6V 5
#define QC3_PULSES_FOR_9V 20
#define QC3_PULSES_FOR_12V 35
static void smblib_hvdcp_adaptive_voltage_change(struct smb_charger *chg)
{
int rc;
u8 stat;
int pulses;
smblib_check_ov_condition(chg);
power_supply_changed(chg->usb_main_psy);
if (chg->real_charger_type == POWER_SUPPLY_TYPE_USB_HVDCP) {
rc = smblib_read(chg, QC_CHANGE_STATUS_REG, &stat);
if (rc < 0) {
smblib_err(chg,
"Couldn't read QC_CHANGE_STATUS rc=%d\n", rc);
return;
}
switch (stat & QC_2P0_STATUS_MASK) {
case QC_5V_BIT:
smblib_set_opt_freq_buck(chg,
chg->chg_freq.freq_5V);
break;
case QC_9V_BIT:
smblib_set_opt_freq_buck(chg,
chg->chg_freq.freq_9V);
break;
case QC_12V_BIT:
smblib_set_opt_freq_buck(chg,
chg->chg_freq.freq_12V);
break;
default:
smblib_set_opt_freq_buck(chg,
chg->chg_freq.freq_removal);
break;
}
}
if (chg->real_charger_type == POWER_SUPPLY_TYPE_USB_HVDCP_3) {
rc = smblib_get_pulse_cnt(chg, &pulses);
if (rc < 0) {
smblib_err(chg,
"Couldn't read QC_PULSE_COUNT rc=%d\n", rc);
return;
}
if (pulses < QC3_PULSES_FOR_6V)
smblib_set_opt_freq_buck(chg,
chg->chg_freq.freq_5V);
else if (pulses < QC3_PULSES_FOR_9V)
smblib_set_opt_freq_buck(chg,
chg->chg_freq.freq_6V_8V);
else if (pulses < QC3_PULSES_FOR_12V)
smblib_set_opt_freq_buck(chg,
chg->chg_freq.freq_9V);
else
smblib_set_opt_freq_buck(chg,
chg->chg_freq.freq_12V);
}
}
/* triggers when HVDCP 3.0 authentication has finished */
static void smblib_handle_hvdcp_3p0_auth_done(struct smb_charger *chg,
bool rising)
{
const struct apsd_result *apsd_result;
int rc;
if (!rising)
return;
if (chg->wa_flags & QC_AUTH_INTERRUPT_WA_BIT) {
/*
* Disable AUTH_IRQ_EN_CFG_BIT to receive adapter voltage
* change interrupt.
*/
rc = smblib_masked_write(chg,
USBIN_SOURCE_CHANGE_INTRPT_ENB_REG,
AUTH_IRQ_EN_CFG_BIT, 0);
if (rc < 0)
smblib_err(chg,
"Couldn't enable QC auth setting rc=%d\n", rc);
}
if (chg->mode == PARALLEL_MASTER)
vote(chg->pl_enable_votable_indirect, USBIN_V_VOTER, true, 0);
/* the APSD done handler will set the USB supply type */
apsd_result = smblib_get_apsd_result(chg);
if (get_effective_result(chg->hvdcp_hw_inov_dis_votable)) {
if (apsd_result->pst == POWER_SUPPLY_TYPE_USB_HVDCP) {
/* force HVDCP2 to 9V if INOV is disabled */
rc = smblib_masked_write(chg, CMD_HVDCP_2_REG,
FORCE_9V_BIT, FORCE_9V_BIT);
if (rc < 0)
smblib_err(chg,
"Couldn't force 9V HVDCP rc=%d\n", rc);
}
}
smblib_dbg(chg, PR_INTERRUPT, "IRQ: hvdcp-3p0-auth-done rising; %s detected\n",
apsd_result->name);
}
static void smblib_handle_hvdcp_check_timeout(struct smb_charger *chg,
bool rising, bool qc_charger)
{
const struct apsd_result *apsd_result = smblib_get_apsd_result(chg);
/* Hold off PD only until hvdcp 2.0 detection timeout */
if (rising) {
vote(chg->pd_disallowed_votable_indirect, HVDCP_TIMEOUT_VOTER,
false, 0);
/* enable HDC and ICL irq for QC2/3 charger */
if (qc_charger)
vote(chg->usb_irq_enable_votable, QC_VOTER, true, 0);
/*
* HVDCP detection timeout done
* If adapter is not QC2.0/QC3.0 - it is a plain old DCP.
*/
if (!qc_charger && (apsd_result->bit & DCP_CHARGER_BIT))
/* enforce DCP ICL if specified */
vote(chg->usb_icl_votable, DCP_VOTER,
chg->dcp_icl_ua != -EINVAL, chg->dcp_icl_ua);
/*
* if pd is not allowed, then set pd_active = false right here,
* so that it starts the hvdcp engine
*/
if (!get_effective_result(chg->pd_allowed_votable) &&
!chg->micro_usb_mode)
__smblib_set_prop_pd_active(chg, 0);
}
smblib_dbg(chg, PR_INTERRUPT, "IRQ: smblib_handle_hvdcp_check_timeout %s\n",
rising ? "rising" : "falling");
}
/* triggers when HVDCP is detected */
static void smblib_handle_hvdcp_detect_done(struct smb_charger *chg,
bool rising)
{
if (!rising)
return;
/* the APSD done handler will set the USB supply type */
cancel_delayed_work_sync(&chg->hvdcp_detect_work);
smblib_dbg(chg, PR_INTERRUPT, "IRQ: hvdcp-detect-done %s\n",
rising ? "rising" : "falling");
}
static void smblib_force_legacy_icl(struct smb_charger *chg, int pst)
{
int typec_mode;
int rp_ua;
/* while PD is active it should have complete ICL control */
if (chg->pd_active)
return;
switch (pst) {
case POWER_SUPPLY_TYPE_USB:
/*
* USB_PSY will vote to increase the current to 500/900mA once
* enumeration is done. Ensure that USB_PSY has at least voted
* for 100mA before releasing the LEGACY_UNKNOWN vote
*/
if (!is_client_vote_enabled(chg->usb_icl_votable,
USB_PSY_VOTER))
vote(chg->usb_icl_votable, USB_PSY_VOTER, true, 100000);
vote(chg->usb_icl_votable, LEGACY_UNKNOWN_VOTER, false, 0);
break;
case POWER_SUPPLY_TYPE_USB_CDP:
vote(chg->usb_icl_votable, LEGACY_UNKNOWN_VOTER, true, 1500000);
break;
case POWER_SUPPLY_TYPE_USB_DCP:
typec_mode = smblib_get_prop_typec_mode(chg);
rp_ua = get_rp_based_dcp_current(chg, typec_mode);
vote(chg->usb_icl_votable, LEGACY_UNKNOWN_VOTER, true, rp_ua);
break;
case POWER_SUPPLY_TYPE_USB_FLOAT:
/*
* limit ICL to 100mA, the USB driver will enumerate to check
* if this is a SDP and appropriately set the current
*/
vote(chg->usb_icl_votable, LEGACY_UNKNOWN_VOTER, true, 100000);
break;
case POWER_SUPPLY_TYPE_USB_HVDCP:
case POWER_SUPPLY_TYPE_USB_HVDCP_3:
vote(chg->usb_icl_votable, LEGACY_UNKNOWN_VOTER, true, 3000000);
break;
default:
smblib_err(chg, "Unknown APSD %d; forcing 500mA\n", pst);
vote(chg->usb_icl_votable, LEGACY_UNKNOWN_VOTER, true, 500000);
break;
}
}
static void smblib_notify_extcon_props(struct smb_charger *chg)
{
union power_supply_propval val;
smblib_get_prop_typec_cc_orientation(chg, &val);
extcon_set_cable_state_(chg->extcon, EXTCON_USB_CC,
(val.intval == 2) ? 1 : 0);
extcon_set_cable_state_(chg->extcon, EXTCON_USB_SPEED, true);
}
static void smblib_notify_device_mode(struct smb_charger *chg, bool enable)
{
if (enable)
smblib_notify_extcon_props(chg);
extcon_set_cable_state_(chg->extcon, EXTCON_USB, enable);
}
static void smblib_notify_usb_host(struct smb_charger *chg, bool enable)
{
if (enable)
smblib_notify_extcon_props(chg);
extcon_set_cable_state_(chg->extcon, EXTCON_USB_HOST, enable);
}
#define HVDCP_DET_MS 2500
static void smblib_handle_apsd_done(struct smb_charger *chg, bool rising)
{
const struct apsd_result *apsd_result;
if (!rising)
return;
apsd_result = smblib_update_usb_type(chg);
if (!chg->typec_legacy_valid)
smblib_force_legacy_icl(chg, apsd_result->pst);
switch (apsd_result->bit) {
case SDP_CHARGER_BIT:
case CDP_CHARGER_BIT:
if (chg->micro_usb_mode)
extcon_set_cable_state_(chg->extcon, EXTCON_USB,
true);
if (chg->use_extcon)
smblib_notify_device_mode(chg, true);
case OCP_CHARGER_BIT:
case FLOAT_CHARGER_BIT:
/* if not DCP then no hvdcp timeout happens, Enable pd here. */
vote(chg->pd_disallowed_votable_indirect, HVDCP_TIMEOUT_VOTER,
false, 0);
break;
case DCP_CHARGER_BIT:
if (chg->wa_flags & QC_CHARGER_DETECTION_WA_BIT)
schedule_delayed_work(&chg->hvdcp_detect_work,
msecs_to_jiffies(HVDCP_DET_MS));
break;
default:
break;
}
smblib_dbg(chg, PR_INTERRUPT, "IRQ: apsd-done rising; %s detected\n",
apsd_result->name);
}
irqreturn_t smblib_handle_usb_source_change(int irq, void *data)
{
struct smb_irq_data *irq_data = data;
struct smb_charger *chg = irq_data->parent_data;
int rc = 0;
u8 stat;
rc = smblib_read(chg, APSD_STATUS_REG, &stat);
if (rc < 0) {
smblib_err(chg, "Couldn't read APSD_STATUS rc=%d\n", rc);
return IRQ_HANDLED;
}
smblib_dbg(chg, PR_REGISTER, "APSD_STATUS = 0x%02x\n", stat);
if (chg->micro_usb_mode && (stat & APSD_DTC_STATUS_DONE_BIT)
&& !chg->uusb_apsd_rerun_done) {
/*
* Force re-run APSD to handle slow insertion related
* charger-mis-detection.
*/
chg->uusb_apsd_rerun_done = true;
smblib_rerun_apsd(chg);
return IRQ_HANDLED;
}
smblib_handle_apsd_done(chg,
(bool)(stat & APSD_DTC_STATUS_DONE_BIT));
smblib_handle_hvdcp_detect_done(chg,
(bool)(stat & QC_CHARGER_BIT));
smblib_handle_hvdcp_check_timeout(chg,
(bool)(stat & HVDCP_CHECK_TIMEOUT_BIT),
(bool)(stat & QC_CHARGER_BIT));
smblib_handle_hvdcp_3p0_auth_done(chg,
(bool)(stat & QC_AUTH_DONE_STATUS_BIT));
smblib_handle_sdp_enumeration_done(chg,
(bool)(stat & ENUMERATION_DONE_BIT));
smblib_handle_slow_plugin_timeout(chg,
(bool)(stat & SLOW_PLUGIN_TIMEOUT_BIT));
smblib_hvdcp_adaptive_voltage_change(chg);
power_supply_changed(chg->usb_psy);
rc = smblib_read(chg, APSD_STATUS_REG, &stat);
if (rc < 0) {
smblib_err(chg, "Couldn't read APSD_STATUS rc=%d\n", rc);
return IRQ_HANDLED;
}
smblib_dbg(chg, PR_REGISTER, "APSD_STATUS = 0x%02x\n", stat);
return IRQ_HANDLED;
}
static int typec_try_sink(struct smb_charger *chg)
{
union power_supply_propval val;
bool debounce_done, vbus_detected, sink;
u8 stat;
int exit_mode = ATTACHED_SRC, rc;
/* ignore typec interrupt while try.snk WIP */
chg->try_sink_active = true;
/* force SNK mode */
val.intval = POWER_SUPPLY_TYPEC_PR_SINK;
rc = smblib_set_prop_typec_power_role(chg, &val);
if (rc < 0) {
smblib_err(chg, "Couldn't set UFP mode rc=%d\n", rc);
goto try_sink_exit;
}
/* reduce Tccdebounce time to ~20ms */
rc = smblib_masked_write(chg, MISC_CFG_REG,
TCC_DEBOUNCE_20MS_BIT, TCC_DEBOUNCE_20MS_BIT);
if (rc < 0) {
smblib_err(chg, "Couldn't set MISC_CFG_REG rc=%d\n", rc);
goto try_sink_exit;
}
/*
* give opportunity to the other side to be a SRC,
* for tDRPTRY + Tccdebounce time
*/
msleep(120);
rc = smblib_read(chg, TYPE_C_STATUS_4_REG, &stat);
if (rc < 0) {
smblib_err(chg, "Couldn't read TYPE_C_STATUS_4 rc=%d\n",
rc);
goto try_sink_exit;
}
debounce_done = stat & TYPEC_DEBOUNCE_DONE_STATUS_BIT;
if (!debounce_done)
/*
* The other side didn't switch to source, either it
* is an adamant sink or is removed go back to showing Rp
*/
goto try_wait_src;
/*
* We are in force sink mode and the other side has switched to
* showing Rp. Config DRP in case the other side removes Rp so we
* can quickly (20ms) switch to showing our Rp. Note that the spec
* needs us to show Rp for 80mS while the drp DFP residency is just
* 54mS. But 54mS is plenty time for us to react and force Rp for
* the remaining 26mS.
*/
val.intval = POWER_SUPPLY_TYPEC_PR_DUAL;
rc = smblib_set_prop_typec_power_role(chg, &val);
if (rc < 0) {
smblib_err(chg, "Couldn't set DFP mode rc=%d\n",
rc);
goto try_sink_exit;
}
/*
* while other side is Rp, wait for VBUS from it; exit if other side
* removes Rp
*/
do {
rc = smblib_read(chg, TYPE_C_STATUS_4_REG, &stat);
if (rc < 0) {
smblib_err(chg, "Couldn't read TYPE_C_STATUS_4 rc=%d\n",
rc);
goto try_sink_exit;
}
debounce_done = stat & TYPEC_DEBOUNCE_DONE_STATUS_BIT;
vbus_detected = stat & TYPEC_VBUS_STATUS_BIT;
/* Successfully transitioned to ATTACHED.SNK */
if (vbus_detected && debounce_done) {
exit_mode = ATTACHED_SINK;
goto try_sink_exit;
}
/*
* Ensure sink since drp may put us in source if other
* side switches back to Rd
*/
sink = !(stat & UFP_DFP_MODE_STATUS_BIT);
usleep_range(1000, 2000);
} while (debounce_done && sink);
try_wait_src:
/*
* Transition to trywait.SRC state. check if other side still wants
* to be SNK or has been removed.
*/
val.intval = POWER_SUPPLY_TYPEC_PR_SOURCE;
rc = smblib_set_prop_typec_power_role(chg, &val);
if (rc < 0) {
smblib_err(chg, "Couldn't set UFP mode rc=%d\n", rc);
goto try_sink_exit;
}
/* Need to be in this state for tDRPTRY time, 75ms~150ms */
msleep(80);
rc = smblib_read(chg, TYPE_C_STATUS_4_REG, &stat);
if (rc < 0) {
smblib_err(chg, "Couldn't read TYPE_C_STATUS_4 rc=%d\n", rc);
goto try_sink_exit;
}
debounce_done = stat & TYPEC_DEBOUNCE_DONE_STATUS_BIT;
if (debounce_done)
/* the other side wants to be a sink */
exit_mode = ATTACHED_SRC;
else
/* the other side is detached */
exit_mode = UNATTACHED_SINK;
try_sink_exit:
/* release forcing of SRC/SNK mode */
val.intval = POWER_SUPPLY_TYPEC_PR_DUAL;
rc = smblib_set_prop_typec_power_role(chg, &val);
if (rc < 0)
smblib_err(chg, "Couldn't set DFP mode rc=%d\n", rc);
/* revert Tccdebounce time back to ~120ms */
rc = smblib_masked_write(chg, MISC_CFG_REG, TCC_DEBOUNCE_20MS_BIT, 0);
if (rc < 0)
smblib_err(chg, "Couldn't set MISC_CFG_REG rc=%d\n", rc);
chg->try_sink_active = false;
return exit_mode;
}
static void typec_sink_insertion(struct smb_charger *chg)
{
int exit_mode;
/*
* Try.SNK entry status - ATTACHWAIT.SRC state and detected Rd-open
* or RD-Ra for TccDebounce time.
*/
if (*chg->try_sink_enabled) {
exit_mode = typec_try_sink(chg);
if (exit_mode != ATTACHED_SRC) {
smblib_usb_typec_change(chg);
return;
}
}
/* when a sink is inserted we should not wait on hvdcp timeout to
* enable pd
*/
vote(chg->pd_disallowed_votable_indirect, HVDCP_TIMEOUT_VOTER,
false, 0);
if (chg->use_extcon) {
smblib_notify_usb_host(chg, true);
chg->otg_present = true;
}
}
static void typec_sink_removal(struct smb_charger *chg)
{
smblib_set_charge_param(chg, &chg->param.freq_boost,
chg->chg_freq.freq_above_otg_threshold);
chg->boost_current_ua = 0;
}
static void smblib_handle_typec_removal(struct smb_charger *chg)
{
int rc;
struct smb_irq_data *data;
struct storm_watch *wdata;
chg->cc2_detach_wa_active = false;
rc = smblib_request_dpdm(chg, false);
if (rc < 0)
smblib_err(chg, "Couldn't disable DPDM rc=%d\n", rc);
if (chg->wa_flags & BOOST_BACK_WA) {
data = chg->irq_info[SWITCH_POWER_OK_IRQ].irq_data;
if (data) {
wdata = &data->storm_data;
update_storm_count(wdata, WEAK_CHG_STORM_COUNT);
vote(chg->usb_icl_votable, BOOST_BACK_VOTER, false, 0);
vote(chg->usb_icl_votable, WEAK_CHARGER_VOTER,
false, 0);
}
}
/* reset APSD voters */
vote(chg->apsd_disable_votable, PD_HARD_RESET_VOTER, false, 0);
vote(chg->apsd_disable_votable, PD_VOTER, false, 0);
cancel_delayed_work_sync(&chg->pl_enable_work);
cancel_delayed_work_sync(&chg->hvdcp_detect_work);
/* reset input current limit voters */
vote(chg->usb_icl_votable, LEGACY_UNKNOWN_VOTER, true, 100000);
vote(chg->usb_icl_votable, PD_VOTER, false, 0);
vote(chg->usb_icl_votable, USB_PSY_VOTER, false, 0);
vote(chg->usb_icl_votable, DCP_VOTER, false, 0);
vote(chg->usb_icl_votable, PL_USBIN_USBIN_VOTER, false, 0);
vote(chg->usb_icl_votable, SW_QC3_VOTER, false, 0);
/* reset hvdcp voters */
vote(chg->hvdcp_disable_votable_indirect, VBUS_CC_SHORT_VOTER, true, 0);
vote(chg->hvdcp_disable_votable_indirect, PD_INACTIVE_VOTER, true, 0);
vote(chg->hvdcp_hw_inov_dis_votable, OV_VOTER, false, 0);
/* reset power delivery voters */
vote(chg->pd_allowed_votable, PD_VOTER, false, 0);
vote(chg->pd_disallowed_votable_indirect, CC_DETACHED_VOTER, true, 0);
vote(chg->pd_disallowed_votable_indirect, HVDCP_TIMEOUT_VOTER, true, 0);
/* reset usb irq voters */
vote(chg->usb_irq_enable_votable, PD_VOTER, false, 0);
vote(chg->usb_irq_enable_votable, QC_VOTER, false, 0);
/* reset parallel voters */
vote(chg->pl_disable_votable, PL_DELAY_VOTER, true, 0);
vote(chg->pl_enable_votable_indirect, USBIN_I_VOTER, false, 0);
vote(chg->pl_enable_votable_indirect, USBIN_V_VOTER, false, 0);
vote(chg->awake_votable, PL_DELAY_VOTER, false, 0);
vote(chg->usb_icl_votable, USBIN_USBIN_BOOST_VOTER, false, 0);
chg->vconn_attempts = 0;
chg->otg_attempts = 0;
chg->pulse_cnt = 0;
chg->usb_icl_delta_ua = 0;
chg->voltage_min_uv = MICRO_5V;
chg->voltage_max_uv = MICRO_5V;
chg->pd_active = 0;
chg->pd_hard_reset = 0;
chg->typec_legacy_valid = false;
/* write back the default FLOAT charger configuration */
rc = smblib_masked_write(chg, USBIN_OPTIONS_2_CFG_REG,
(u8)FLOAT_OPTIONS_MASK, chg->float_cfg);
if (rc < 0)
smblib_err(chg, "Couldn't write float charger options rc=%d\n",
rc);
/* reset back to 120mS tCC debounce */
rc = smblib_masked_write(chg, MISC_CFG_REG, TCC_DEBOUNCE_20MS_BIT, 0);
if (rc < 0)
smblib_err(chg, "Couldn't set 120mS tCC debounce rc=%d\n", rc);
/* enable APSD CC trigger for next insertion */
rc = smblib_masked_write(chg, TYPE_C_CFG_REG,
APSD_START_ON_CC_BIT, APSD_START_ON_CC_BIT);
if (rc < 0)
smblib_err(chg, "Couldn't enable APSD_START_ON_CC rc=%d\n", rc);
if (chg->wa_flags & QC_AUTH_INTERRUPT_WA_BIT) {
/* re-enable AUTH_IRQ_EN_CFG_BIT */
rc = smblib_masked_write(chg,
USBIN_SOURCE_CHANGE_INTRPT_ENB_REG,
AUTH_IRQ_EN_CFG_BIT, AUTH_IRQ_EN_CFG_BIT);
if (rc < 0)
smblib_err(chg,
"Couldn't enable QC auth setting rc=%d\n", rc);
}
/* reconfigure allowed voltage for HVDCP */
rc = smblib_set_adapter_allowance(chg,
USBIN_ADAPTER_ALLOW_5V_OR_9V_TO_12V);
if (rc < 0)
smblib_err(chg, "Couldn't set USBIN_ADAPTER_ALLOW_5V_OR_9V_TO_12V rc=%d\n",
rc);
/* enable DRP */
rc = smblib_masked_write(chg, TYPE_C_INTRPT_ENB_SOFTWARE_CTRL_REG,
TYPEC_POWER_ROLE_CMD_MASK, 0);
if (rc < 0)
smblib_err(chg, "Couldn't enable DRP rc=%d\n", rc);
/* HW controlled CC_OUT */
rc = smblib_masked_write(chg, TAPER_TIMER_SEL_CFG_REG,
TYPEC_SPARE_CFG_BIT, 0);
if (rc < 0)
smblib_err(chg, "Couldn't enable HW cc_out rc=%d\n", rc);
/* restore crude sensor if PM660/PMI8998 */
if (chg->wa_flags & TYPEC_PBS_WA_BIT) {
rc = smblib_write(chg, TM_IO_DTEST4_SEL, 0xA5);
if (rc < 0)
smblib_err(chg, "Couldn't restore crude sensor rc=%d\n",
rc);
}
mutex_lock(&chg->vconn_oc_lock);
if (!chg->vconn_en)
goto unlock;
smblib_masked_write(chg, TYPE_C_INTRPT_ENB_SOFTWARE_CTRL_REG,
VCONN_EN_VALUE_BIT, 0);
chg->vconn_en = false;
unlock:
mutex_unlock(&chg->vconn_oc_lock);
/* clear exit sink based on cc */
rc = smblib_masked_write(chg, TYPE_C_INTRPT_ENB_SOFTWARE_CTRL_REG,
EXIT_SNK_BASED_ON_CC_BIT, 0);
if (rc < 0)
smblib_err(chg, "Couldn't clear exit_sink_based_on_cc rc=%d\n",
rc);
typec_sink_removal(chg);
smblib_update_usb_type(chg);
if (chg->use_extcon) {
if (chg->otg_present)
smblib_notify_usb_host(chg, false);
else
smblib_notify_device_mode(chg, false);
}
chg->otg_present = false;
}
static void smblib_handle_typec_insertion(struct smb_charger *chg)
{
int rc;
vote(chg->pd_disallowed_votable_indirect, CC_DETACHED_VOTER, false, 0);
/* disable APSD CC trigger since CC is attached */
rc = smblib_masked_write(chg, TYPE_C_CFG_REG, APSD_START_ON_CC_BIT, 0);
if (rc < 0)
smblib_err(chg, "Couldn't disable APSD_START_ON_CC rc=%d\n",
rc);
if (chg->typec_status[3] & UFP_DFP_MODE_STATUS_BIT) {
typec_sink_insertion(chg);
} else {
rc = smblib_request_dpdm(chg, true);
if (rc < 0)
smblib_err(chg, "Couldn't to enable DPDM rc=%d\n", rc);
typec_sink_removal(chg);
}
}
static void smblib_handle_rp_change(struct smb_charger *chg, int typec_mode)
{
int rp_ua;
const struct apsd_result *apsd = smblib_get_apsd_result(chg);
if ((apsd->pst != POWER_SUPPLY_TYPE_USB_DCP)
&& (apsd->pst != POWER_SUPPLY_TYPE_USB_FLOAT))
return;
/*
* if APSD indicates FLOAT and the USB stack had detected SDP,
* do not respond to Rp changes as we do not confirm that its
* a legacy cable
*/
if (chg->real_charger_type == POWER_SUPPLY_TYPE_USB)
return;
/*
* We want the ICL vote @ 100mA for a FLOAT charger
* until the detection by the USB stack is complete.
* Ignore the Rp changes unless there is a
* pre-existing valid vote.
*/
if (apsd->pst == POWER_SUPPLY_TYPE_USB_FLOAT &&
get_client_vote(chg->usb_icl_votable,
LEGACY_UNKNOWN_VOTER) <= 100000)
return;
/*
* handle Rp change for DCP/FLOAT/OCP.
* Update the current only if the Rp is different from
* the last Rp value.
*/
smblib_dbg(chg, PR_MISC, "CC change old_mode=%d new_mode=%d\n",
chg->typec_mode, typec_mode);
rp_ua = get_rp_based_dcp_current(chg, typec_mode);
vote(chg->usb_icl_votable, LEGACY_UNKNOWN_VOTER, true, rp_ua);
}
static void smblib_handle_typec_cc_state_change(struct smb_charger *chg)
{
int typec_mode;
if (chg->pr_swap_in_progress)
return;
typec_mode = smblib_get_prop_typec_mode(chg);
if (chg->typec_present && (typec_mode != chg->typec_mode))
smblib_handle_rp_change(chg, typec_mode);
chg->typec_mode = typec_mode;
if (!chg->typec_present && chg->typec_mode != POWER_SUPPLY_TYPEC_NONE) {
chg->typec_present = true;
smblib_dbg(chg, PR_MISC, "TypeC %s insertion\n",
smblib_typec_mode_name[chg->typec_mode]);
smblib_handle_typec_insertion(chg);
} else if (chg->typec_present &&
chg->typec_mode == POWER_SUPPLY_TYPEC_NONE) {
chg->typec_present = false;
smblib_dbg(chg, PR_MISC, "TypeC removal\n");
smblib_handle_typec_removal(chg);
}
smblib_dbg(chg, PR_INTERRUPT, "IRQ: cc-state-change; Type-C %s detected\n",
smblib_typec_mode_name[chg->typec_mode]);
}
void smblib_usb_typec_change(struct smb_charger *chg)
{
int rc;
rc = smblib_multibyte_read(chg, TYPE_C_STATUS_1_REG,
chg->typec_status, 5);
if (rc < 0) {
smblib_err(chg, "Couldn't cache USB Type-C status rc=%d\n", rc);
return;
}
smblib_handle_typec_cc_state_change(chg);
if (chg->typec_status[3] & TYPEC_VBUS_ERROR_STATUS_BIT)
smblib_dbg(chg, PR_INTERRUPT, "IRQ: vbus-error\n");
if (chg->typec_status[3] & TYPEC_VCONN_OVERCURR_STATUS_BIT)
schedule_work(&chg->vconn_oc_work);
power_supply_changed(chg->usb_psy);
}
irqreturn_t smblib_handle_usb_typec_change(int irq, void *data)
{
struct smb_irq_data *irq_data = data;
struct smb_charger *chg = irq_data->parent_data;
if (chg->micro_usb_mode) {
cancel_delayed_work_sync(&chg->uusb_otg_work);
vote(chg->awake_votable, OTG_DELAY_VOTER, true, 0);
smblib_dbg(chg, PR_INTERRUPT, "Scheduling OTG work\n");
schedule_delayed_work(&chg->uusb_otg_work,
msecs_to_jiffies(chg->otg_delay_ms));
return IRQ_HANDLED;
}
if (chg->cc2_detach_wa_active || chg->typec_en_dis_active ||
chg->try_sink_active) {
smblib_dbg(chg, PR_MISC | PR_INTERRUPT, "Ignoring since %s active\n",
chg->cc2_detach_wa_active ?
"cc2_detach_wa" : "typec_en_dis");
return IRQ_HANDLED;
}
mutex_lock(&chg->lock);
smblib_usb_typec_change(chg);
mutex_unlock(&chg->lock);
return IRQ_HANDLED;
}
irqreturn_t smblib_handle_dc_plugin(int irq, void *data)
{
struct smb_irq_data *irq_data = data;
struct smb_charger *chg = irq_data->parent_data;
power_supply_changed(chg->dc_psy);
return IRQ_HANDLED;
}
irqreturn_t smblib_handle_high_duty_cycle(int irq, void *data)
{
struct smb_irq_data *irq_data = data;
struct smb_charger *chg = irq_data->parent_data;
chg->is_hdc = true;
/*
* Disable usb IRQs after the flag set and re-enable IRQs after
* the flag cleared in the delayed work queue, to avoid any IRQ
* storming during the delays
*/
if (chg->irq_info[HIGH_DUTY_CYCLE_IRQ].irq)
disable_irq_nosync(chg->irq_info[HIGH_DUTY_CYCLE_IRQ].irq);
schedule_delayed_work(&chg->clear_hdc_work, msecs_to_jiffies(60));
return IRQ_HANDLED;
}
static void smblib_bb_removal_work(struct work_struct *work)
{
struct smb_charger *chg = container_of(work, struct smb_charger,
bb_removal_work.work);
vote(chg->usb_icl_votable, BOOST_BACK_VOTER, false, 0);
vote(chg->awake_votable, BOOST_BACK_VOTER, false, 0);
}
#define BOOST_BACK_UNVOTE_DELAY_MS 750
#define BOOST_BACK_STORM_COUNT 3
#define WEAK_CHG_STORM_COUNT 8
irqreturn_t smblib_handle_switcher_power_ok(int irq, void *data)
{
struct smb_irq_data *irq_data = data;
struct smb_charger *chg = irq_data->parent_data;
struct storm_watch *wdata = &irq_data->storm_data;
int rc, usb_icl;
u8 stat;
if (!(chg->wa_flags & BOOST_BACK_WA))
return IRQ_HANDLED;
rc = smblib_read(chg, POWER_PATH_STATUS_REG, &stat);
if (rc < 0) {
smblib_err(chg, "Couldn't read POWER_PATH_STATUS rc=%d\n", rc);
return IRQ_HANDLED;
}
/* skip suspending input if its already suspended by some other voter */
usb_icl = get_effective_result(chg->usb_icl_votable);
if ((stat & USE_USBIN_BIT) && usb_icl >= 0 && usb_icl < USBIN_25MA)
return IRQ_HANDLED;
if (stat & USE_DCIN_BIT)
return IRQ_HANDLED;
if (is_storming(&irq_data->storm_data)) {
/* This could be a weak charger reduce ICL */
if (!is_client_vote_enabled(chg->usb_icl_votable,
WEAK_CHARGER_VOTER)) {
smblib_err(chg,
"Weak charger detected: voting %dmA ICL\n",
*chg->weak_chg_icl_ua / 1000);
vote(chg->usb_icl_votable, WEAK_CHARGER_VOTER,
true, *chg->weak_chg_icl_ua);
/*
* reset storm data and set the storm threshold
* to 3 for reverse boost detection.
*/
update_storm_count(wdata, BOOST_BACK_STORM_COUNT);
} else {
smblib_err(chg,
"Reverse boost detected: voting 0mA to suspend input\n");
vote(chg->usb_icl_votable, BOOST_BACK_VOTER, true, 0);
vote(chg->awake_votable, BOOST_BACK_VOTER, true, 0);
/*
* Remove the boost-back vote after a delay, to avoid
* permanently suspending the input if the boost-back
* condition is unintentionally hit.
*/
schedule_delayed_work(&chg->bb_removal_work,
msecs_to_jiffies(BOOST_BACK_UNVOTE_DELAY_MS));
}
}
return IRQ_HANDLED;
}
irqreturn_t smblib_handle_wdog_bark(int irq, void *data)
{
struct smb_irq_data *irq_data = data;
struct smb_charger *chg = irq_data->parent_data;
int rc;
smblib_dbg(chg, PR_INTERRUPT, "IRQ: %s\n", irq_data->name);
rc = smblib_write(chg, BARK_BITE_WDOG_PET_REG, BARK_BITE_WDOG_PET_BIT);
if (rc < 0)
smblib_err(chg, "Couldn't pet the dog rc=%d\n", rc);
if (chg->step_chg_enabled || chg->sw_jeita_enabled)
power_supply_changed(chg->batt_psy);
return IRQ_HANDLED;
}
/**************
* Additional USB PSY getters/setters
* that call interrupt functions
***************/
int smblib_get_prop_pr_swap_in_progress(struct smb_charger *chg,
union power_supply_propval *val)
{
val->intval = chg->pr_swap_in_progress;
return 0;
}
int smblib_set_prop_pr_swap_in_progress(struct smb_charger *chg,
const union power_supply_propval *val)
{
int rc;
chg->pr_swap_in_progress = val->intval;
/*
* call the cc changed irq to handle real removals while
* PR_SWAP was in progress
*/
smblib_usb_typec_change(chg);
rc = smblib_masked_write(chg, MISC_CFG_REG, TCC_DEBOUNCE_20MS_BIT,
val->intval ? TCC_DEBOUNCE_20MS_BIT : 0);
if (rc < 0)
smblib_err(chg, "Couldn't set tCC debounce rc=%d\n", rc);
return 0;
}
/***************
* Work Queues *
***************/
static void smblib_uusb_otg_work(struct work_struct *work)
{
struct smb_charger *chg = container_of(work, struct smb_charger,
uusb_otg_work.work);
int rc;
u8 stat;
bool otg;
rc = smblib_read(chg, TYPE_C_STATUS_3_REG, &stat);
if (rc < 0) {
smblib_err(chg, "Couldn't read TYPE_C_STATUS_3 rc=%d\n", rc);
goto out;
}
otg = !!(stat & (U_USB_GND_NOVBUS_BIT | U_USB_GND_BIT));
extcon_set_cable_state_(chg->extcon, EXTCON_USB_HOST, otg);
smblib_dbg(chg, PR_REGISTER, "TYPE_C_STATUS_3 = 0x%02x OTG=%d\n",
stat, otg);
power_supply_changed(chg->usb_psy);
out:
vote(chg->awake_votable, OTG_DELAY_VOTER, false, 0);
}
static void smblib_hvdcp_detect_work(struct work_struct *work)
{
struct smb_charger *chg = container_of(work, struct smb_charger,
hvdcp_detect_work.work);
vote(chg->pd_disallowed_votable_indirect, HVDCP_TIMEOUT_VOTER,
false, 0);
power_supply_changed(chg->usb_psy);
}
static void bms_update_work(struct work_struct *work)
{
struct smb_charger *chg = container_of(work, struct smb_charger,
bms_update_work);
smblib_suspend_on_debug_battery(chg);
if (chg->batt_psy)
power_supply_changed(chg->batt_psy);
}
static void clear_hdc_work(struct work_struct *work)
{
struct smb_charger *chg = container_of(work, struct smb_charger,
clear_hdc_work.work);
chg->is_hdc = 0;
if (chg->irq_info[HIGH_DUTY_CYCLE_IRQ].irq)
enable_irq(chg->irq_info[HIGH_DUTY_CYCLE_IRQ].irq);
}
static void rdstd_cc2_detach_work(struct work_struct *work)
{
int rc;
u8 stat4, stat5;
bool lock = false;
struct smb_charger *chg = container_of(work, struct smb_charger,
rdstd_cc2_detach_work);
if (!chg->cc2_detach_wa_active)
return;
/*
* WA steps -
* 1. Enable both UFP and DFP, wait for 10ms.
* 2. Disable DFP, wait for 30ms.
* 3. Removal detected if both TYPEC_DEBOUNCE_DONE_STATUS
* and TIMER_STAGE bits are gone, otherwise repeat all by
* work rescheduling.
* Note, work will be cancelled when USB_PLUGIN rises.
*/
rc = smblib_masked_write(chg, TYPE_C_INTRPT_ENB_SOFTWARE_CTRL_REG,
UFP_EN_CMD_BIT | DFP_EN_CMD_BIT,
UFP_EN_CMD_BIT | DFP_EN_CMD_BIT);
if (rc < 0) {
smblib_err(chg, "Couldn't write TYPE_C_CTRL_REG rc=%d\n", rc);
return;
}
usleep_range(10000, 11000);
rc = smblib_masked_write(chg, TYPE_C_INTRPT_ENB_SOFTWARE_CTRL_REG,
UFP_EN_CMD_BIT | DFP_EN_CMD_BIT,
UFP_EN_CMD_BIT);
if (rc < 0) {
smblib_err(chg, "Couldn't write TYPE_C_CTRL_REG rc=%d\n", rc);
return;
}
usleep_range(30000, 31000);
rc = smblib_read(chg, TYPE_C_STATUS_4_REG, &stat4);
if (rc < 0) {
smblib_err(chg, "Couldn't read TYPE_C_STATUS_4 rc=%d\n", rc);
return;
}
rc = smblib_read(chg, TYPE_C_STATUS_5_REG, &stat5);
if (rc < 0) {
smblib_err(chg,
"Couldn't read TYPE_C_STATUS_5_REG rc=%d\n", rc);
return;
}
if ((stat4 & TYPEC_DEBOUNCE_DONE_STATUS_BIT)
|| (stat5 & TIMER_STAGE_2_BIT)) {
smblib_dbg(chg, PR_MISC, "rerunning DD=%d TS2BIT=%d\n",
(int)(stat4 & TYPEC_DEBOUNCE_DONE_STATUS_BIT),
(int)(stat5 & TIMER_STAGE_2_BIT));
goto rerun;
}
smblib_dbg(chg, PR_MISC, "Bingo CC2 Removal detected\n");
chg->cc2_detach_wa_active = false;
rc = smblib_masked_write(chg, TYPE_C_INTRPT_ENB_SOFTWARE_CTRL_REG,
EXIT_SNK_BASED_ON_CC_BIT, 0);
smblib_reg_block_restore(chg, cc2_detach_settings);
/*
* Mutex acquisition deadlock can happen while cancelling this work
* during pd_hard_reset from the function smblib_cc2_sink_removal_exit
* which is called in the same lock context that we try to acquire in
* this work routine.
* Check if this work is running during pd_hard_reset and use trylock
* instead of mutex_lock to prevent any deadlock if mutext is already
* held.
*/
if (chg->pd_hard_reset) {
if (mutex_trylock(&chg->lock))
lock = true;
} else {
mutex_lock(&chg->lock);
lock = true;
}
smblib_usb_typec_change(chg);
if (lock)
mutex_unlock(&chg->lock);
return;
rerun:
schedule_work(&chg->rdstd_cc2_detach_work);
}
static void smblib_otg_oc_exit(struct smb_charger *chg, bool success)
{
int rc;
chg->otg_attempts = 0;
if (!success) {
smblib_err(chg, "OTG soft start failed\n");
chg->otg_en = false;
}
smblib_dbg(chg, PR_OTG, "enabling VBUS < 1V check\n");
rc = smblib_masked_write(chg, OTG_CFG_REG,
QUICKSTART_OTG_FASTROLESWAP_BIT, 0);
if (rc < 0)
smblib_err(chg, "Couldn't enable VBUS < 1V check rc=%d\n", rc);
}
#define MAX_OC_FALLING_TRIES 10
static void smblib_otg_oc_work(struct work_struct *work)
{
struct smb_charger *chg = container_of(work, struct smb_charger,
otg_oc_work);
int rc, i;
u8 stat;
if (!chg->vbus_vreg || !chg->vbus_vreg->rdev)
return;
smblib_err(chg, "over-current detected on VBUS\n");
mutex_lock(&chg->otg_oc_lock);
if (!chg->otg_en)
goto unlock;
smblib_dbg(chg, PR_OTG, "disabling VBUS < 1V check\n");
smblib_masked_write(chg, OTG_CFG_REG,
QUICKSTART_OTG_FASTROLESWAP_BIT,
QUICKSTART_OTG_FASTROLESWAP_BIT);
/*
* If 500ms has passed and another over-current interrupt has not
* triggered then it is likely that the software based soft start was
* successful and the VBUS < 1V restriction should be re-enabled.
*/
schedule_delayed_work(&chg->otg_ss_done_work, msecs_to_jiffies(500));
rc = _smblib_vbus_regulator_disable(chg->vbus_vreg->rdev);
if (rc < 0) {
smblib_err(chg, "Couldn't disable VBUS rc=%d\n", rc);
goto unlock;
}
if (++chg->otg_attempts > OTG_MAX_ATTEMPTS) {
cancel_delayed_work_sync(&chg->otg_ss_done_work);
smblib_err(chg, "OTG failed to enable after %d attempts\n",
chg->otg_attempts - 1);
smblib_otg_oc_exit(chg, false);
goto unlock;
}
/*
* The real time status should go low within 10ms. Poll every 1-2ms to
* minimize the delay when re-enabling OTG.
*/
for (i = 0; i < MAX_OC_FALLING_TRIES; ++i) {
usleep_range(1000, 2000);
rc = smblib_read(chg, OTG_BASE + INT_RT_STS_OFFSET, &stat);
if (rc >= 0 && !(stat & OTG_OVERCURRENT_RT_STS_BIT))
break;
}
if (i >= MAX_OC_FALLING_TRIES) {
cancel_delayed_work_sync(&chg->otg_ss_done_work);
smblib_err(chg, "OTG OC did not fall after %dms\n",
2 * MAX_OC_FALLING_TRIES);
smblib_otg_oc_exit(chg, false);
goto unlock;
}
smblib_dbg(chg, PR_OTG, "OTG OC fell after %dms\n", 2 * i + 1);
rc = _smblib_vbus_regulator_enable(chg->vbus_vreg->rdev);
if (rc < 0) {
smblib_err(chg, "Couldn't enable VBUS rc=%d\n", rc);
goto unlock;
}
unlock:
mutex_unlock(&chg->otg_oc_lock);
}
static void smblib_vconn_oc_work(struct work_struct *work)
{
struct smb_charger *chg = container_of(work, struct smb_charger,
vconn_oc_work);
int rc, i;
u8 stat;
if (chg->micro_usb_mode)
return;
smblib_err(chg, "over-current detected on VCONN\n");
if (!chg->vconn_vreg || !chg->vconn_vreg->rdev)
return;
mutex_lock(&chg->vconn_oc_lock);
rc = _smblib_vconn_regulator_disable(chg->vconn_vreg->rdev);
if (rc < 0) {
smblib_err(chg, "Couldn't disable VCONN rc=%d\n", rc);
goto unlock;
}
if (++chg->vconn_attempts > VCONN_MAX_ATTEMPTS) {
smblib_err(chg, "VCONN failed to enable after %d attempts\n",
chg->otg_attempts - 1);
chg->vconn_en = false;
chg->vconn_attempts = 0;
goto unlock;
}
/*
* The real time status should go low within 10ms. Poll every 1-2ms to
* minimize the delay when re-enabling OTG.
*/
for (i = 0; i < MAX_OC_FALLING_TRIES; ++i) {
usleep_range(1000, 2000);
rc = smblib_read(chg, TYPE_C_STATUS_4_REG, &stat);
if (rc >= 0 && !(stat & TYPEC_VCONN_OVERCURR_STATUS_BIT))
break;
}
if (i >= MAX_OC_FALLING_TRIES) {
smblib_err(chg, "VCONN OC did not fall after %dms\n",
2 * MAX_OC_FALLING_TRIES);
chg->vconn_en = false;
chg->vconn_attempts = 0;
goto unlock;
}
smblib_dbg(chg, PR_OTG, "VCONN OC fell after %dms\n", 2 * i + 1);
if (++chg->vconn_attempts > VCONN_MAX_ATTEMPTS) {
smblib_err(chg, "VCONN failed to enable after %d attempts\n",
chg->vconn_attempts - 1);
chg->vconn_en = false;
goto unlock;
}
rc = _smblib_vconn_regulator_enable(chg->vconn_vreg->rdev);
if (rc < 0) {
smblib_err(chg, "Couldn't enable VCONN rc=%d\n", rc);
goto unlock;
}
unlock:
mutex_unlock(&chg->vconn_oc_lock);
}
static void smblib_otg_ss_done_work(struct work_struct *work)
{
struct smb_charger *chg = container_of(work, struct smb_charger,
otg_ss_done_work.work);
int rc;
bool success = false;
u8 stat;
mutex_lock(&chg->otg_oc_lock);
rc = smblib_read(chg, OTG_STATUS_REG, &stat);
if (rc < 0)
smblib_err(chg, "Couldn't read OTG status rc=%d\n", rc);
else if (stat & BOOST_SOFTSTART_DONE_BIT)
success = true;
smblib_otg_oc_exit(chg, success);
mutex_unlock(&chg->otg_oc_lock);
}
static void smblib_icl_change_work(struct work_struct *work)
{
struct smb_charger *chg = container_of(work, struct smb_charger,
icl_change_work.work);
int rc, settled_ua;
rc = smblib_get_charge_param(chg, &chg->param.icl_stat, &settled_ua);
if (rc < 0) {
smblib_err(chg, "Couldn't get ICL status rc=%d\n", rc);
return;
}
power_supply_changed(chg->usb_main_psy);
smblib_dbg(chg, PR_INTERRUPT, "icl_settled=%d\n", settled_ua);
}
static void smblib_pl_enable_work(struct work_struct *work)
{
struct smb_charger *chg = container_of(work, struct smb_charger,
pl_enable_work.work);
smblib_dbg(chg, PR_PARALLEL, "timer expired, enabling parallel\n");
vote(chg->pl_disable_votable, PL_DELAY_VOTER, false, 0);
vote(chg->awake_votable, PL_DELAY_VOTER, false, 0);
}
static void smblib_legacy_detection_work(struct work_struct *work)
{
struct smb_charger *chg = container_of(work, struct smb_charger,
legacy_detection_work);
int rc;
u8 stat;
bool legacy, rp_high;
mutex_lock(&chg->lock);
chg->typec_en_dis_active = 1;
smblib_dbg(chg, PR_MISC, "running legacy unknown workaround\n");
rc = smblib_masked_write(chg,
TYPE_C_INTRPT_ENB_SOFTWARE_CTRL_REG,
TYPEC_DISABLE_CMD_BIT,
TYPEC_DISABLE_CMD_BIT);
if (rc < 0)
smblib_err(chg, "Couldn't disable type-c rc=%d\n", rc);
/* wait for the adapter to turn off VBUS */
msleep(1000);
smblib_dbg(chg, PR_MISC, "legacy workaround enabling typec\n");
rc = smblib_masked_write(chg,
TYPE_C_INTRPT_ENB_SOFTWARE_CTRL_REG,
TYPEC_DISABLE_CMD_BIT, 0);
if (rc < 0)
smblib_err(chg, "Couldn't enable type-c rc=%d\n", rc);
/* wait for type-c detection to complete */
msleep(400);
rc = smblib_read(chg, TYPE_C_STATUS_5_REG, &stat);
if (rc < 0) {
smblib_err(chg, "Couldn't read typec stat5 rc = %d\n", rc);
goto unlock;
}
chg->typec_legacy_valid = true;
vote(chg->usb_icl_votable, LEGACY_UNKNOWN_VOTER, false, 0);
legacy = stat & TYPEC_LEGACY_CABLE_STATUS_BIT;
rp_high = chg->typec_mode == POWER_SUPPLY_TYPEC_SOURCE_HIGH;
smblib_dbg(chg, PR_MISC, "legacy workaround done legacy = %d rp_high = %d\n",
legacy, rp_high);
if (!legacy || !rp_high)
vote(chg->hvdcp_disable_votable_indirect, VBUS_CC_SHORT_VOTER,
false, 0);
unlock:
chg->typec_en_dis_active = 0;
smblib_usb_typec_change(chg);
mutex_unlock(&chg->lock);
}
static int smblib_create_votables(struct smb_charger *chg)
{
int rc = 0;
chg->fcc_votable = find_votable("FCC");
if (chg->fcc_votable == NULL) {
rc = -EINVAL;
smblib_err(chg, "Couldn't find FCC votable rc=%d\n", rc);
return rc;
}
chg->fv_votable = find_votable("FV");
if (chg->fv_votable == NULL) {
rc = -EINVAL;
smblib_err(chg, "Couldn't find FV votable rc=%d\n", rc);
return rc;
}
chg->usb_icl_votable = find_votable("USB_ICL");
if (!chg->usb_icl_votable) {
rc = -EINVAL;
smblib_err(chg, "Couldn't find USB_ICL votable rc=%d\n", rc);
return rc;
}
chg->pl_disable_votable = find_votable("PL_DISABLE");
if (chg->pl_disable_votable == NULL) {
rc = -EINVAL;
smblib_err(chg, "Couldn't find votable PL_DISABLE rc=%d\n", rc);
return rc;
}
chg->pl_enable_votable_indirect = find_votable("PL_ENABLE_INDIRECT");
if (chg->pl_enable_votable_indirect == NULL) {
rc = -EINVAL;
smblib_err(chg,
"Couldn't find votable PL_ENABLE_INDIRECT rc=%d\n",
rc);
return rc;
}
vote(chg->pl_disable_votable, PL_DELAY_VOTER, true, 0);
chg->dc_suspend_votable = create_votable("DC_SUSPEND", VOTE_SET_ANY,
smblib_dc_suspend_vote_callback,
chg);
if (IS_ERR(chg->dc_suspend_votable)) {
rc = PTR_ERR(chg->dc_suspend_votable);
return rc;
}
chg->dc_icl_votable = create_votable("DC_ICL", VOTE_MIN,
smblib_dc_icl_vote_callback,
chg);
if (IS_ERR(chg->dc_icl_votable)) {
rc = PTR_ERR(chg->dc_icl_votable);
return rc;
}
chg->pd_disallowed_votable_indirect
= create_votable("PD_DISALLOWED_INDIRECT", VOTE_SET_ANY,
smblib_pd_disallowed_votable_indirect_callback, chg);
if (IS_ERR(chg->pd_disallowed_votable_indirect)) {
rc = PTR_ERR(chg->pd_disallowed_votable_indirect);
return rc;
}
chg->pd_allowed_votable = create_votable("PD_ALLOWED",
VOTE_SET_ANY, NULL, NULL);
if (IS_ERR(chg->pd_allowed_votable)) {
rc = PTR_ERR(chg->pd_allowed_votable);
return rc;
}
chg->awake_votable = create_votable("AWAKE", VOTE_SET_ANY,
smblib_awake_vote_callback,
chg);
if (IS_ERR(chg->awake_votable)) {
rc = PTR_ERR(chg->awake_votable);
return rc;
}
chg->chg_disable_votable = create_votable("CHG_DISABLE", VOTE_SET_ANY,
smblib_chg_disable_vote_callback,
chg);
if (IS_ERR(chg->chg_disable_votable)) {
rc = PTR_ERR(chg->chg_disable_votable);
return rc;
}
chg->hvdcp_disable_votable_indirect = create_votable(
"HVDCP_DISABLE_INDIRECT",
VOTE_SET_ANY,
smblib_hvdcp_disable_indirect_vote_callback,
chg);
if (IS_ERR(chg->hvdcp_disable_votable_indirect)) {
rc = PTR_ERR(chg->hvdcp_disable_votable_indirect);
return rc;
}
chg->hvdcp_enable_votable = create_votable("HVDCP_ENABLE",
VOTE_SET_ANY,
smblib_hvdcp_enable_vote_callback,
chg);
if (IS_ERR(chg->hvdcp_enable_votable)) {
rc = PTR_ERR(chg->hvdcp_enable_votable);
return rc;
}
chg->apsd_disable_votable = create_votable("APSD_DISABLE",
VOTE_SET_ANY,
smblib_apsd_disable_vote_callback,
chg);
if (IS_ERR(chg->apsd_disable_votable)) {
rc = PTR_ERR(chg->apsd_disable_votable);
return rc;
}
chg->hvdcp_hw_inov_dis_votable = create_votable("HVDCP_HW_INOV_DIS",
VOTE_SET_ANY,
smblib_hvdcp_hw_inov_dis_vote_callback,
chg);
if (IS_ERR(chg->hvdcp_hw_inov_dis_votable)) {
rc = PTR_ERR(chg->hvdcp_hw_inov_dis_votable);
return rc;
}
chg->usb_irq_enable_votable = create_votable("USB_IRQ_DISABLE",
VOTE_SET_ANY,
smblib_usb_irq_enable_vote_callback,
chg);
if (IS_ERR(chg->usb_irq_enable_votable)) {
rc = PTR_ERR(chg->usb_irq_enable_votable);
return rc;
}
chg->typec_irq_disable_votable = create_votable("TYPEC_IRQ_DISABLE",
VOTE_SET_ANY,
smblib_typec_irq_disable_vote_callback,
chg);
if (IS_ERR(chg->typec_irq_disable_votable)) {
rc = PTR_ERR(chg->typec_irq_disable_votable);
return rc;
}
return rc;
}
static void smblib_destroy_votables(struct smb_charger *chg)
{
if (chg->dc_suspend_votable)
destroy_votable(chg->dc_suspend_votable);
if (chg->usb_icl_votable)
destroy_votable(chg->usb_icl_votable);
if (chg->dc_icl_votable)
destroy_votable(chg->dc_icl_votable);
if (chg->pd_disallowed_votable_indirect)
destroy_votable(chg->pd_disallowed_votable_indirect);
if (chg->pd_allowed_votable)
destroy_votable(chg->pd_allowed_votable);
if (chg->awake_votable)
destroy_votable(chg->awake_votable);
if (chg->chg_disable_votable)
destroy_votable(chg->chg_disable_votable);
if (chg->apsd_disable_votable)
destroy_votable(chg->apsd_disable_votable);
if (chg->hvdcp_hw_inov_dis_votable)
destroy_votable(chg->hvdcp_hw_inov_dis_votable);
if (chg->typec_irq_disable_votable)
destroy_votable(chg->typec_irq_disable_votable);
}
static void smblib_iio_deinit(struct smb_charger *chg)
{
if (!IS_ERR_OR_NULL(chg->iio.temp_chan))
iio_channel_release(chg->iio.temp_chan);
if (!IS_ERR_OR_NULL(chg->iio.temp_max_chan))
iio_channel_release(chg->iio.temp_max_chan);
if (!IS_ERR_OR_NULL(chg->iio.usbin_i_chan))
iio_channel_release(chg->iio.usbin_i_chan);
if (!IS_ERR_OR_NULL(chg->iio.usbin_v_chan))
iio_channel_release(chg->iio.usbin_v_chan);
if (!IS_ERR_OR_NULL(chg->iio.batt_i_chan))
iio_channel_release(chg->iio.batt_i_chan);
}
int smblib_init(struct smb_charger *chg)
{
int rc = 0;
mutex_init(&chg->lock);
mutex_init(&chg->write_lock);
mutex_init(&chg->otg_oc_lock);
mutex_init(&chg->vconn_oc_lock);
INIT_WORK(&chg->bms_update_work, bms_update_work);
INIT_WORK(&chg->rdstd_cc2_detach_work, rdstd_cc2_detach_work);
INIT_DELAYED_WORK(&chg->hvdcp_detect_work, smblib_hvdcp_detect_work);
INIT_DELAYED_WORK(&chg->clear_hdc_work, clear_hdc_work);
INIT_WORK(&chg->otg_oc_work, smblib_otg_oc_work);
INIT_WORK(&chg->vconn_oc_work, smblib_vconn_oc_work);
INIT_DELAYED_WORK(&chg->otg_ss_done_work, smblib_otg_ss_done_work);
INIT_DELAYED_WORK(&chg->icl_change_work, smblib_icl_change_work);
INIT_DELAYED_WORK(&chg->pl_enable_work, smblib_pl_enable_work);
INIT_WORK(&chg->legacy_detection_work, smblib_legacy_detection_work);
INIT_DELAYED_WORK(&chg->uusb_otg_work, smblib_uusb_otg_work);
INIT_DELAYED_WORK(&chg->bb_removal_work, smblib_bb_removal_work);
chg->fake_capacity = -EINVAL;
chg->fake_input_current_limited = -EINVAL;
switch (chg->mode) {
case PARALLEL_MASTER:
rc = qcom_batt_init();
if (rc < 0) {
smblib_err(chg, "Couldn't init qcom_batt_init rc=%d\n",
rc);
return rc;
}
rc = qcom_step_chg_init(chg->step_chg_enabled,
chg->sw_jeita_enabled);
if (rc < 0) {
smblib_err(chg, "Couldn't init qcom_step_chg_init rc=%d\n",
rc);
return rc;
}
rc = smblib_create_votables(chg);
if (rc < 0) {
smblib_err(chg, "Couldn't create votables rc=%d\n",
rc);
return rc;
}
rc = smblib_register_notifier(chg);
if (rc < 0) {
smblib_err(chg,
"Couldn't register notifier rc=%d\n", rc);
return rc;
}
chg->bms_psy = power_supply_get_by_name("bms");
chg->pl.psy = power_supply_get_by_name("parallel");
break;
case PARALLEL_SLAVE:
break;
default:
smblib_err(chg, "Unsupported mode %d\n", chg->mode);
return -EINVAL;
}
return rc;
}
int smblib_deinit(struct smb_charger *chg)
{
switch (chg->mode) {
case PARALLEL_MASTER:
cancel_work_sync(&chg->bms_update_work);
cancel_work_sync(&chg->rdstd_cc2_detach_work);
cancel_delayed_work_sync(&chg->hvdcp_detect_work);
cancel_delayed_work_sync(&chg->clear_hdc_work);
cancel_work_sync(&chg->otg_oc_work);
cancel_work_sync(&chg->vconn_oc_work);
cancel_delayed_work_sync(&chg->otg_ss_done_work);
cancel_delayed_work_sync(&chg->icl_change_work);
cancel_delayed_work_sync(&chg->pl_enable_work);
cancel_work_sync(&chg->legacy_detection_work);
cancel_delayed_work_sync(&chg->uusb_otg_work);
cancel_delayed_work_sync(&chg->bb_removal_work);
power_supply_unreg_notifier(&chg->nb);
smblib_destroy_votables(chg);
qcom_step_chg_deinit();
qcom_batt_deinit();
break;
case PARALLEL_SLAVE:
break;
default:
smblib_err(chg, "Unsupported mode %d\n", chg->mode);
return -EINVAL;
}
smblib_iio_deinit(chg);
return 0;
}
|