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
|
// SPDX-License-Identifier: GPL-2.0-only
/*
* Copyright (c) 2019-2020, The Linux Foundation. All rights reserved.
*/
#define pr_fmt(fmt) "BATTERY_CHG: %s: " fmt, __func__
#include <linux/debugfs.h>
#include <linux/delay.h>
#include <linux/device.h>
#include <linux/firmware.h>
#include <linux/module.h>
#include <linux/of.h>
#include <linux/platform_device.h>
#include <linux/reboot.h>
#include <linux/rpmsg.h>
#include <linux/mutex.h>
#include <linux/pm_wakeup.h>
#include <linux/power_supply.h>
#include <linux/soc/qcom/pmic_glink.h>
#include <linux/soc/qcom/battery_charger.h>
#define MSG_OWNER_BC 32778
#define MSG_TYPE_REQ_RESP 1
#define MSG_TYPE_NOTIFY 2
/* opcode for battery charger */
#define BC_SET_NOTIFY_REQ 0x04
#define BC_NOTIFY_IND 0x07
#define BC_BATTERY_STATUS_GET 0x30
#define BC_BATTERY_STATUS_SET 0x31
#define BC_USB_STATUS_GET 0x32
#define BC_USB_STATUS_SET 0x33
#define BC_WLS_STATUS_GET 0x34
#define BC_WLS_STATUS_SET 0x35
#define BC_SHIP_MODE_REQ_SET 0x36
#define BC_WLS_FW_CHECK_UPDATE 0x40
#define BC_WLS_FW_PUSH_BUF_REQ 0x41
#define BC_WLS_FW_UPDATE_STATUS_RESP 0x42
#define BC_WLS_FW_PUSH_BUF_RESP 0x43
#define BC_WLS_FW_GET_VERSION 0x44
#define BC_GENERIC_NOTIFY 0x80
/* Generic definitions */
#define MAX_STR_LEN 128
#define BC_WAIT_TIME_MS 1000
#define WLS_FW_PREPARE_TIME_MS 300
#define WLS_FW_WAIT_TIME_MS 500
#define WLS_FW_UPDATE_TIME_MS 1000
#define WLS_FW_BUF_SIZE 128
#define DEFAULT_RESTRICT_FCC_UA 1000000
enum psy_type {
PSY_TYPE_BATTERY,
PSY_TYPE_USB,
PSY_TYPE_WLS,
PSY_TYPE_MAX,
};
enum ship_mode_type {
SHIP_MODE_PMIC,
SHIP_MODE_PACK_SIDE,
};
/* property ids */
enum battery_property_id {
BATT_STATUS,
BATT_HEALTH,
BATT_PRESENT,
BATT_CHG_TYPE,
BATT_CAPACITY,
BATT_SOH,
BATT_VOLT_OCV,
BATT_VOLT_NOW,
BATT_VOLT_MAX,
BATT_CURR_NOW,
BATT_CHG_CTRL_LIM,
BATT_CHG_CTRL_LIM_MAX,
BATT_TEMP,
BATT_TECHNOLOGY,
BATT_CHG_COUNTER,
BATT_CYCLE_COUNT,
BATT_CHG_FULL_DESIGN,
BATT_CHG_FULL,
BATT_MODEL_NAME,
BATT_TTF_AVG,
BATT_TTE_AVG,
BATT_RESISTANCE,
BATT_POWER_NOW,
BATT_POWER_AVG,
BATT_PROP_MAX,
};
enum usb_property_id {
USB_ONLINE,
USB_VOLT_NOW,
USB_VOLT_MAX,
USB_CURR_NOW,
USB_CURR_MAX,
USB_INPUT_CURR_LIMIT,
USB_TYPE,
USB_ADAP_TYPE,
USB_MOISTURE_DET_EN,
USB_MOISTURE_DET_STS,
USB_TEMP,
USB_REAL_TYPE,
USB_TYPEC_COMPLIANT,
USB_PROP_MAX,
};
enum wireless_property_id {
WLS_ONLINE,
WLS_VOLT_NOW,
WLS_VOLT_MAX,
WLS_CURR_NOW,
WLS_CURR_MAX,
WLS_TYPE,
WLS_BOOST_EN,
WLS_PROP_MAX,
};
enum {
QTI_POWER_SUPPLY_USB_TYPE_HVDCP = 0x80,
QTI_POWER_SUPPLY_USB_TYPE_HVDCP_3,
QTI_POWER_SUPPLY_USB_TYPE_HVDCP_3P5,
};
struct battery_charger_set_notify_msg {
struct pmic_glink_hdr hdr;
u32 battery_id;
u32 power_state;
u32 low_capacity;
u32 high_capacity;
};
struct battery_charger_notify_msg {
struct pmic_glink_hdr hdr;
u32 notification;
};
struct battery_charger_req_msg {
struct pmic_glink_hdr hdr;
u32 battery_id;
u32 property_id;
u32 value;
};
struct battery_charger_resp_msg {
struct pmic_glink_hdr hdr;
u32 property_id;
u32 value;
u32 ret_code;
};
struct battery_model_resp_msg {
struct pmic_glink_hdr hdr;
u32 property_id;
char model[MAX_STR_LEN];
};
struct wireless_fw_check_req {
struct pmic_glink_hdr hdr;
u32 fw_version;
u32 fw_size;
u32 fw_crc;
};
struct wireless_fw_check_resp {
struct pmic_glink_hdr hdr;
u32 ret_code;
};
struct wireless_fw_push_buf_req {
struct pmic_glink_hdr hdr;
u8 buf[WLS_FW_BUF_SIZE];
u32 fw_chunk_id;
};
struct wireless_fw_push_buf_resp {
struct pmic_glink_hdr hdr;
u32 fw_update_status;
};
struct wireless_fw_update_status {
struct pmic_glink_hdr hdr;
u32 fw_update_done;
};
struct wireless_fw_get_version_req {
struct pmic_glink_hdr hdr;
};
struct wireless_fw_get_version_resp {
struct pmic_glink_hdr hdr;
u32 fw_version;
};
struct battery_charger_ship_mode_req_msg {
struct pmic_glink_hdr hdr;
u32 ship_mode_type;
};
struct psy_state {
struct power_supply *psy;
char *model;
const int *map;
u32 *prop;
u32 prop_count;
u32 opcode_get;
u32 opcode_set;
};
struct battery_chg_dev {
struct device *dev;
struct class battery_class;
struct pmic_glink_client *client;
struct mutex rw_lock;
struct completion ack;
struct completion fw_buf_ack;
struct completion fw_update_ack;
struct psy_state psy_list[PSY_TYPE_MAX];
struct dentry *debugfs_dir;
u32 *thermal_levels;
const char *wls_fw_name;
int curr_thermal_level;
int num_thermal_levels;
atomic_t state;
struct work_struct subsys_up_work;
struct work_struct usb_type_work;
int fake_soc;
bool block_tx;
bool ship_mode_en;
bool debug_battery_detected;
bool wls_fw_update_reqd;
u32 wls_fw_version;
u16 wls_fw_crc;
struct notifier_block reboot_notifier;
u32 thermal_fcc_ua;
u32 restrict_fcc_ua;
u32 last_fcc_ua;
u32 usb_icl_ua;
bool restrict_chg_en;
/* To track the driver initialization status */
bool initialized;
};
static const int battery_prop_map[BATT_PROP_MAX] = {
[BATT_STATUS] = POWER_SUPPLY_PROP_STATUS,
[BATT_HEALTH] = POWER_SUPPLY_PROP_HEALTH,
[BATT_PRESENT] = POWER_SUPPLY_PROP_PRESENT,
[BATT_CHG_TYPE] = POWER_SUPPLY_PROP_CHARGE_TYPE,
[BATT_CAPACITY] = POWER_SUPPLY_PROP_CAPACITY,
[BATT_VOLT_OCV] = POWER_SUPPLY_PROP_VOLTAGE_OCV,
[BATT_VOLT_NOW] = POWER_SUPPLY_PROP_VOLTAGE_NOW,
[BATT_VOLT_MAX] = POWER_SUPPLY_PROP_VOLTAGE_MAX,
[BATT_CURR_NOW] = POWER_SUPPLY_PROP_CURRENT_NOW,
[BATT_CHG_CTRL_LIM] = POWER_SUPPLY_PROP_CHARGE_CONTROL_LIMIT,
[BATT_CHG_CTRL_LIM_MAX] = POWER_SUPPLY_PROP_CHARGE_CONTROL_LIMIT_MAX,
[BATT_TEMP] = POWER_SUPPLY_PROP_TEMP,
[BATT_TECHNOLOGY] = POWER_SUPPLY_PROP_TECHNOLOGY,
[BATT_CHG_COUNTER] = POWER_SUPPLY_PROP_CHARGE_COUNTER,
[BATT_CYCLE_COUNT] = POWER_SUPPLY_PROP_CYCLE_COUNT,
[BATT_CHG_FULL_DESIGN] = POWER_SUPPLY_PROP_CHARGE_FULL_DESIGN,
[BATT_CHG_FULL] = POWER_SUPPLY_PROP_CHARGE_FULL,
[BATT_MODEL_NAME] = POWER_SUPPLY_PROP_MODEL_NAME,
[BATT_TTF_AVG] = POWER_SUPPLY_PROP_TIME_TO_FULL_AVG,
[BATT_TTE_AVG] = POWER_SUPPLY_PROP_TIME_TO_EMPTY_AVG,
[BATT_POWER_NOW] = POWER_SUPPLY_PROP_POWER_NOW,
[BATT_POWER_AVG] = POWER_SUPPLY_PROP_POWER_AVG,
};
static const int usb_prop_map[USB_PROP_MAX] = {
[USB_ONLINE] = POWER_SUPPLY_PROP_ONLINE,
[USB_VOLT_NOW] = POWER_SUPPLY_PROP_VOLTAGE_NOW,
[USB_VOLT_MAX] = POWER_SUPPLY_PROP_VOLTAGE_MAX,
[USB_CURR_NOW] = POWER_SUPPLY_PROP_CURRENT_NOW,
[USB_CURR_MAX] = POWER_SUPPLY_PROP_CURRENT_MAX,
[USB_INPUT_CURR_LIMIT] = POWER_SUPPLY_PROP_INPUT_CURRENT_LIMIT,
[USB_ADAP_TYPE] = POWER_SUPPLY_PROP_USB_TYPE,
[USB_TEMP] = POWER_SUPPLY_PROP_TEMP,
};
static const int wls_prop_map[WLS_PROP_MAX] = {
[WLS_ONLINE] = POWER_SUPPLY_PROP_ONLINE,
[WLS_VOLT_NOW] = POWER_SUPPLY_PROP_VOLTAGE_NOW,
[WLS_VOLT_MAX] = POWER_SUPPLY_PROP_VOLTAGE_MAX,
[WLS_CURR_NOW] = POWER_SUPPLY_PROP_CURRENT_NOW,
[WLS_CURR_MAX] = POWER_SUPPLY_PROP_CURRENT_MAX,
};
/* Standard usb_type definitions similar to power_supply_sysfs.c */
static const char * const power_supply_usb_type_text[] = {
"Unknown", "SDP", "DCP", "CDP", "ACA", "C",
"PD", "PD_DRP", "PD_PPS", "BrickID"
};
/* Custom usb_type definitions */
static const char * const qc_power_supply_usb_type_text[] = {
"HVDCP", "HVDCP_3", "HVDCP_3P5"
};
static int battery_chg_fw_write(struct battery_chg_dev *bcdev, void *data,
int len)
{
int rc;
if (atomic_read(&bcdev->state) == PMIC_GLINK_STATE_DOWN) {
pr_debug("glink state is down\n");
return -ENOTCONN;
}
reinit_completion(&bcdev->fw_buf_ack);
rc = pmic_glink_write(bcdev->client, data, len);
if (!rc) {
rc = wait_for_completion_timeout(&bcdev->fw_buf_ack,
msecs_to_jiffies(WLS_FW_WAIT_TIME_MS));
if (!rc) {
pr_err("Error, timed out sending message\n");
return -ETIMEDOUT;
}
rc = 0;
}
return rc;
}
static int battery_chg_write(struct battery_chg_dev *bcdev, void *data,
int len)
{
int rc;
/*
* When the subsystem goes down, it's better to return the last
* known values until it comes back up. Hence, return 0 so that
* pmic_glink_write() is not attempted until pmic glink is up.
*/
if (atomic_read(&bcdev->state) == PMIC_GLINK_STATE_DOWN) {
pr_debug("glink state is down\n");
return 0;
}
if (bcdev->debug_battery_detected && bcdev->block_tx)
return 0;
mutex_lock(&bcdev->rw_lock);
reinit_completion(&bcdev->ack);
rc = pmic_glink_write(bcdev->client, data, len);
if (!rc) {
rc = wait_for_completion_timeout(&bcdev->ack,
msecs_to_jiffies(BC_WAIT_TIME_MS));
if (!rc) {
pr_err("Error, timed out sending message\n");
mutex_unlock(&bcdev->rw_lock);
return -ETIMEDOUT;
}
rc = 0;
}
mutex_unlock(&bcdev->rw_lock);
return rc;
}
static int write_property_id(struct battery_chg_dev *bcdev,
struct psy_state *pst, u32 prop_id, u32 val)
{
struct battery_charger_req_msg req_msg = { { 0 } };
req_msg.property_id = prop_id;
req_msg.battery_id = 0;
req_msg.value = val;
req_msg.hdr.owner = MSG_OWNER_BC;
req_msg.hdr.type = MSG_TYPE_REQ_RESP;
req_msg.hdr.opcode = pst->opcode_set;
pr_debug("psy: %s prop_id: %u val: %u\n", pst->psy->desc->name,
req_msg.property_id, val);
return battery_chg_write(bcdev, &req_msg, sizeof(req_msg));
}
static int read_property_id(struct battery_chg_dev *bcdev,
struct psy_state *pst, u32 prop_id)
{
struct battery_charger_req_msg req_msg = { { 0 } };
req_msg.property_id = prop_id;
req_msg.battery_id = 0;
req_msg.value = 0;
req_msg.hdr.owner = MSG_OWNER_BC;
req_msg.hdr.type = MSG_TYPE_REQ_RESP;
req_msg.hdr.opcode = pst->opcode_get;
pr_debug("psy: %s prop_id: %u\n", pst->psy->desc->name,
req_msg.property_id);
return battery_chg_write(bcdev, &req_msg, sizeof(req_msg));
}
static int get_property_id(struct psy_state *pst,
enum power_supply_property prop)
{
u32 i;
for (i = 0; i < pst->prop_count; i++)
if (pst->map[i] == prop)
return i;
pr_err("No property id for property %d in psy %s\n", prop,
pst->psy->desc->name);
return -ENOENT;
}
static void battery_chg_notify_enable(struct battery_chg_dev *bcdev)
{
struct battery_charger_set_notify_msg req_msg = { { 0 } };
int rc;
/* Send request to enable notification */
req_msg.hdr.owner = MSG_OWNER_BC;
req_msg.hdr.type = MSG_TYPE_NOTIFY;
req_msg.hdr.opcode = BC_SET_NOTIFY_REQ;
rc = battery_chg_write(bcdev, &req_msg, sizeof(req_msg));
if (rc < 0)
pr_err("Failed to enable notification rc=%d\n", rc);
}
static void battery_chg_state_cb(void *priv, enum pmic_glink_state state)
{
struct battery_chg_dev *bcdev = priv;
pr_debug("state: %d\n", state);
atomic_set(&bcdev->state, state);
if (state == PMIC_GLINK_STATE_UP)
schedule_work(&bcdev->subsys_up_work);
}
/**
* qti_battery_charger_get_prop() - Gets the property being requested
*
* @name: Power supply name
* @prop_id: Property id to be read
* @val: Pointer to value that needs to be updated
*
* Return: 0 if success, negative on error.
*/
int qti_battery_charger_get_prop(const char *name,
enum battery_charger_prop prop_id, int *val)
{
struct power_supply *psy;
struct battery_chg_dev *bcdev;
struct psy_state *pst;
int rc = 0;
if (prop_id >= BATTERY_CHARGER_PROP_MAX)
return -EINVAL;
if (strcmp(name, "battery") && strcmp(name, "usb") &&
strcmp(name, "wireless"))
return -EINVAL;
psy = power_supply_get_by_name(name);
if (!psy)
return -ENODEV;
bcdev = power_supply_get_drvdata(psy);
if (!bcdev)
return -ENODEV;
power_supply_put(psy);
switch (prop_id) {
case BATTERY_RESISTANCE:
pst = &bcdev->psy_list[PSY_TYPE_BATTERY];
rc = read_property_id(bcdev, pst, BATT_RESISTANCE);
if (!rc)
*val = pst->prop[BATT_RESISTANCE];
break;
default:
break;
}
return rc;
}
EXPORT_SYMBOL(qti_battery_charger_get_prop);
static bool validate_message(struct battery_charger_resp_msg *resp_msg,
size_t len)
{
if (len != sizeof(*resp_msg)) {
pr_err("Incorrect response length %zu for opcode %#x\n", len,
resp_msg->hdr.opcode);
return false;
}
if (resp_msg->ret_code) {
pr_err("Error in response for opcode %#x prop_id %u, rc=%d\n",
resp_msg->hdr.opcode, resp_msg->property_id,
(int)resp_msg->ret_code);
return false;
}
return true;
}
#define MODEL_DEBUG_BOARD "Debug_Board"
static void handle_message(struct battery_chg_dev *bcdev, void *data,
size_t len)
{
struct battery_charger_resp_msg *resp_msg = data;
struct battery_model_resp_msg *model_resp_msg = data;
struct wireless_fw_check_resp *fw_check_msg;
struct wireless_fw_push_buf_resp *fw_resp_msg;
struct wireless_fw_update_status *fw_update_msg;
struct wireless_fw_get_version_resp *fw_ver_msg;
struct psy_state *pst;
bool ack_set = false;
switch (resp_msg->hdr.opcode) {
case BC_BATTERY_STATUS_GET:
pst = &bcdev->psy_list[PSY_TYPE_BATTERY];
/* Handle model response uniquely as it's a string */
if (pst->model && len == sizeof(*model_resp_msg)) {
memcpy(pst->model, model_resp_msg->model, MAX_STR_LEN);
ack_set = true;
bcdev->debug_battery_detected = !strcmp(pst->model,
MODEL_DEBUG_BOARD);
break;
}
/* Other response should be of same type as they've u32 value */
if (validate_message(resp_msg, len) &&
resp_msg->property_id < pst->prop_count) {
pst->prop[resp_msg->property_id] = resp_msg->value;
ack_set = true;
}
break;
case BC_USB_STATUS_GET:
pst = &bcdev->psy_list[PSY_TYPE_USB];
if (validate_message(resp_msg, len) &&
resp_msg->property_id < pst->prop_count) {
pst->prop[resp_msg->property_id] = resp_msg->value;
ack_set = true;
}
break;
case BC_WLS_STATUS_GET:
pst = &bcdev->psy_list[PSY_TYPE_WLS];
if (validate_message(resp_msg, len) &&
resp_msg->property_id < pst->prop_count) {
pst->prop[resp_msg->property_id] = resp_msg->value;
ack_set = true;
}
break;
case BC_BATTERY_STATUS_SET:
case BC_USB_STATUS_SET:
case BC_WLS_STATUS_SET:
if (validate_message(data, len))
ack_set = true;
break;
case BC_SET_NOTIFY_REQ:
/* Always ACK response for notify request */
ack_set = true;
break;
case BC_WLS_FW_CHECK_UPDATE:
if (len == sizeof(*fw_check_msg)) {
fw_check_msg = data;
if (fw_check_msg->ret_code == 1)
bcdev->wls_fw_update_reqd = true;
ack_set = true;
} else {
pr_err("Incorrect response length %zu for wls_fw_check_update\n",
len);
}
break;
case BC_WLS_FW_PUSH_BUF_RESP:
if (len == sizeof(*fw_resp_msg)) {
fw_resp_msg = data;
if (fw_resp_msg->fw_update_status == 1)
complete(&bcdev->fw_buf_ack);
} else {
pr_err("Incorrect response length %zu for wls_fw_push_buf_resp\n",
len);
}
break;
case BC_WLS_FW_UPDATE_STATUS_RESP:
if (len == sizeof(*fw_update_msg)) {
fw_update_msg = data;
if (fw_update_msg->fw_update_done == 1)
complete(&bcdev->fw_update_ack);
else
pr_err("Wireless FW update not done %d\n",
(int)fw_update_msg->fw_update_done);
} else {
pr_err("Incorrect response length %zu for wls_fw_update_status_resp\n",
len);
}
break;
case BC_WLS_FW_GET_VERSION:
if (len == sizeof(*fw_ver_msg)) {
fw_ver_msg = data;
bcdev->wls_fw_version = fw_ver_msg->fw_version;
ack_set = true;
} else {
pr_err("Incorrect response length %zu for wls_fw_get_version\n",
len);
}
break;
default:
pr_err("Unknown opcode: %u\n", resp_msg->hdr.opcode);
break;
}
if (ack_set)
complete(&bcdev->ack);
}
static struct power_supply_desc usb_psy_desc;
static void battery_chg_update_usb_type_work(struct work_struct *work)
{
struct battery_chg_dev *bcdev = container_of(work,
struct battery_chg_dev, usb_type_work);
struct psy_state *pst = &bcdev->psy_list[PSY_TYPE_USB];
int rc;
rc = read_property_id(bcdev, pst, USB_ADAP_TYPE);
if (rc < 0) {
pr_err("Failed to read USB_ADAP_TYPE rc=%d\n", rc);
return;
}
/* Reset usb_icl_ua whenever USB adapter type changes */
if (pst->prop[USB_ADAP_TYPE] != POWER_SUPPLY_USB_TYPE_SDP &&
pst->prop[USB_ADAP_TYPE] != POWER_SUPPLY_USB_TYPE_PD)
bcdev->usb_icl_ua = 0;
pr_debug("usb_adap_type: %u\n", pst->prop[USB_ADAP_TYPE]);
switch (pst->prop[USB_ADAP_TYPE]) {
case POWER_SUPPLY_USB_TYPE_SDP:
usb_psy_desc.type = POWER_SUPPLY_TYPE_USB;
break;
case POWER_SUPPLY_USB_TYPE_DCP:
case POWER_SUPPLY_USB_TYPE_APPLE_BRICK_ID:
case QTI_POWER_SUPPLY_USB_TYPE_HVDCP:
case QTI_POWER_SUPPLY_USB_TYPE_HVDCP_3:
case QTI_POWER_SUPPLY_USB_TYPE_HVDCP_3P5:
usb_psy_desc.type = POWER_SUPPLY_TYPE_USB_DCP;
break;
case POWER_SUPPLY_USB_TYPE_CDP:
usb_psy_desc.type = POWER_SUPPLY_TYPE_USB_CDP;
break;
case POWER_SUPPLY_USB_TYPE_ACA:
usb_psy_desc.type = POWER_SUPPLY_TYPE_USB_ACA;
break;
case POWER_SUPPLY_USB_TYPE_C:
usb_psy_desc.type = POWER_SUPPLY_TYPE_USB_TYPE_C;
break;
case POWER_SUPPLY_USB_TYPE_PD:
case POWER_SUPPLY_USB_TYPE_PD_DRP:
case POWER_SUPPLY_USB_TYPE_PD_PPS:
usb_psy_desc.type = POWER_SUPPLY_TYPE_USB_PD;
break;
default:
usb_psy_desc.type = POWER_SUPPLY_TYPE_USB;
break;
}
}
static void handle_notification(struct battery_chg_dev *bcdev, void *data,
size_t len)
{
struct battery_charger_notify_msg *notify_msg = data;
struct psy_state *pst = NULL;
if (len != sizeof(*notify_msg)) {
pr_err("Incorrect response length %zu\n", len);
return;
}
pr_debug("notification: %#x\n", notify_msg->notification);
switch (notify_msg->notification) {
case BC_BATTERY_STATUS_GET:
case BC_GENERIC_NOTIFY:
pst = &bcdev->psy_list[PSY_TYPE_BATTERY];
break;
case BC_USB_STATUS_GET:
pst = &bcdev->psy_list[PSY_TYPE_USB];
schedule_work(&bcdev->usb_type_work);
break;
case BC_WLS_STATUS_GET:
pst = &bcdev->psy_list[PSY_TYPE_WLS];
break;
default:
break;
}
if (pst && pst->psy) {
/*
* For charger mode, keep the device awake at least for 50 ms
* so that device won't enter suspend when a non-SDP charger
* is removed. This would allow the userspace process like
* "charger" to be able to read power supply uevents to take
* appropriate actions (e.g. shutting down when the charger is
* unplugged).
*/
power_supply_changed(pst->psy);
pm_wakeup_dev_event(bcdev->dev, 50, true);
}
}
static int battery_chg_callback(void *priv, void *data, size_t len)
{
struct pmic_glink_hdr *hdr = data;
struct battery_chg_dev *bcdev = priv;
pr_debug("owner: %u type: %u opcode: %#x len: %zu\n", hdr->owner,
hdr->type, hdr->opcode, len);
if (!bcdev->initialized) {
pr_debug("Driver initialization failed: Dropping glink callback message: state %d\n",
bcdev->state);
return 0;
}
if (hdr->opcode == BC_NOTIFY_IND)
handle_notification(bcdev, data, len);
else
handle_message(bcdev, data, len);
return 0;
}
static int wls_psy_get_prop(struct power_supply *psy,
enum power_supply_property prop,
union power_supply_propval *pval)
{
struct battery_chg_dev *bcdev = power_supply_get_drvdata(psy);
struct psy_state *pst = &bcdev->psy_list[PSY_TYPE_WLS];
int prop_id, rc;
pval->intval = -ENODATA;
prop_id = get_property_id(pst, prop);
if (prop_id < 0)
return prop_id;
rc = read_property_id(bcdev, pst, prop_id);
if (rc < 0)
return rc;
pval->intval = pst->prop[prop_id];
return 0;
}
static int wls_psy_set_prop(struct power_supply *psy,
enum power_supply_property prop,
const union power_supply_propval *pval)
{
return 0;
}
static int wls_psy_prop_is_writeable(struct power_supply *psy,
enum power_supply_property prop)
{
return 0;
}
static enum power_supply_property wls_props[] = {
POWER_SUPPLY_PROP_ONLINE,
POWER_SUPPLY_PROP_VOLTAGE_NOW,
POWER_SUPPLY_PROP_VOLTAGE_MAX,
POWER_SUPPLY_PROP_CURRENT_NOW,
POWER_SUPPLY_PROP_CURRENT_MAX,
};
static const struct power_supply_desc wls_psy_desc = {
.name = "wireless",
.type = POWER_SUPPLY_TYPE_WIRELESS,
.properties = wls_props,
.num_properties = ARRAY_SIZE(wls_props),
.get_property = wls_psy_get_prop,
.set_property = wls_psy_set_prop,
.property_is_writeable = wls_psy_prop_is_writeable,
};
static const char *get_usb_type_name(u32 usb_type)
{
u32 i;
if (usb_type >= QTI_POWER_SUPPLY_USB_TYPE_HVDCP &&
usb_type <= QTI_POWER_SUPPLY_USB_TYPE_HVDCP_3P5) {
for (i = 0; i < ARRAY_SIZE(qc_power_supply_usb_type_text);
i++) {
if (i == (usb_type - QTI_POWER_SUPPLY_USB_TYPE_HVDCP))
return qc_power_supply_usb_type_text[i];
}
return "Unknown";
}
for (i = 0; i < ARRAY_SIZE(power_supply_usb_type_text); i++) {
if (i == usb_type)
return power_supply_usb_type_text[i];
}
return "Unknown";
}
static int usb_psy_set_icl(struct battery_chg_dev *bcdev, u32 prop_id, int val)
{
struct psy_state *pst = &bcdev->psy_list[PSY_TYPE_USB];
u32 temp;
int rc;
rc = read_property_id(bcdev, pst, USB_ADAP_TYPE);
if (rc < 0) {
pr_err("Failed to read prop USB_ADAP_TYPE, rc=%d\n", rc);
return rc;
}
/* Allow this only for SDP or USB_PD and not for other charger types */
if (pst->prop[USB_ADAP_TYPE] != POWER_SUPPLY_USB_TYPE_SDP &&
pst->prop[USB_ADAP_TYPE] != POWER_SUPPLY_USB_TYPE_PD)
return -EINVAL;
/*
* Input current limit (ICL) can be set by different clients. E.g. USB
* driver can request for a current of 500/900 mA depending on the
* port type. Also, clients like EUD driver can pass 0 or -22 to
* suspend or unsuspend the input for its use case.
*/
temp = val;
if (val < 0)
temp = UINT_MAX;
rc = write_property_id(bcdev, pst, prop_id, temp);
if (rc < 0) {
pr_err("Failed to set ICL (%u uA) rc=%d\n", temp, rc);
} else {
pr_debug("Set ICL to %u\n", temp);
bcdev->usb_icl_ua = temp;
}
return rc;
}
static int usb_psy_get_prop(struct power_supply *psy,
enum power_supply_property prop,
union power_supply_propval *pval)
{
struct battery_chg_dev *bcdev = power_supply_get_drvdata(psy);
struct psy_state *pst = &bcdev->psy_list[PSY_TYPE_USB];
int prop_id, rc;
pval->intval = -ENODATA;
prop_id = get_property_id(pst, prop);
if (prop_id < 0)
return prop_id;
rc = read_property_id(bcdev, pst, prop_id);
if (rc < 0)
return rc;
pval->intval = pst->prop[prop_id];
if (prop == POWER_SUPPLY_PROP_TEMP)
pval->intval = DIV_ROUND_CLOSEST((int)pval->intval, 10);
return 0;
}
static int usb_psy_set_prop(struct power_supply *psy,
enum power_supply_property prop,
const union power_supply_propval *pval)
{
struct battery_chg_dev *bcdev = power_supply_get_drvdata(psy);
struct psy_state *pst = &bcdev->psy_list[PSY_TYPE_USB];
int prop_id, rc = 0;
prop_id = get_property_id(pst, prop);
if (prop_id < 0)
return prop_id;
switch (prop) {
case POWER_SUPPLY_PROP_INPUT_CURRENT_LIMIT:
rc = usb_psy_set_icl(bcdev, prop_id, pval->intval);
break;
default:
break;
}
return rc;
}
static int usb_psy_prop_is_writeable(struct power_supply *psy,
enum power_supply_property prop)
{
switch (prop) {
case POWER_SUPPLY_PROP_INPUT_CURRENT_LIMIT:
return 1;
default:
break;
}
return 0;
}
static enum power_supply_property usb_props[] = {
POWER_SUPPLY_PROP_ONLINE,
POWER_SUPPLY_PROP_VOLTAGE_NOW,
POWER_SUPPLY_PROP_VOLTAGE_MAX,
POWER_SUPPLY_PROP_CURRENT_NOW,
POWER_SUPPLY_PROP_CURRENT_MAX,
POWER_SUPPLY_PROP_INPUT_CURRENT_LIMIT,
POWER_SUPPLY_PROP_USB_TYPE,
POWER_SUPPLY_PROP_TEMP,
};
static enum power_supply_usb_type usb_psy_supported_types[] = {
POWER_SUPPLY_USB_TYPE_UNKNOWN,
POWER_SUPPLY_USB_TYPE_SDP,
POWER_SUPPLY_USB_TYPE_DCP,
POWER_SUPPLY_USB_TYPE_CDP,
POWER_SUPPLY_USB_TYPE_ACA,
POWER_SUPPLY_USB_TYPE_C,
POWER_SUPPLY_USB_TYPE_PD,
POWER_SUPPLY_USB_TYPE_PD_DRP,
POWER_SUPPLY_USB_TYPE_PD_PPS,
POWER_SUPPLY_USB_TYPE_APPLE_BRICK_ID,
};
static struct power_supply_desc usb_psy_desc = {
.name = "usb",
.type = POWER_SUPPLY_TYPE_USB,
.properties = usb_props,
.num_properties = ARRAY_SIZE(usb_props),
.get_property = usb_psy_get_prop,
.set_property = usb_psy_set_prop,
.usb_types = usb_psy_supported_types,
.num_usb_types = ARRAY_SIZE(usb_psy_supported_types),
.property_is_writeable = usb_psy_prop_is_writeable,
};
static int __battery_psy_set_charge_current(struct battery_chg_dev *bcdev,
u32 fcc_ua)
{
int rc;
if (bcdev->restrict_chg_en) {
fcc_ua = min_t(u32, fcc_ua, bcdev->restrict_fcc_ua);
fcc_ua = min_t(u32, fcc_ua, bcdev->thermal_fcc_ua);
}
rc = write_property_id(bcdev, &bcdev->psy_list[PSY_TYPE_BATTERY],
BATT_CHG_CTRL_LIM, fcc_ua);
if (rc < 0) {
pr_err("Failed to set FCC %u, rc=%d\n", fcc_ua, rc);
} else {
pr_debug("Set FCC to %u uA\n", fcc_ua);
bcdev->last_fcc_ua = fcc_ua;
}
return rc;
}
static int battery_psy_set_charge_current(struct battery_chg_dev *bcdev,
int val)
{
int rc;
u32 fcc_ua, prev_fcc_ua;
if (!bcdev->num_thermal_levels)
return 0;
if (bcdev->num_thermal_levels < 0) {
pr_err("Incorrect num_thermal_levels\n");
return -EINVAL;
}
if (val < 0 || val > bcdev->num_thermal_levels)
return -EINVAL;
fcc_ua = bcdev->thermal_levels[val];
prev_fcc_ua = bcdev->thermal_fcc_ua;
bcdev->thermal_fcc_ua = fcc_ua;
rc = __battery_psy_set_charge_current(bcdev, fcc_ua);
if (!rc)
bcdev->curr_thermal_level = val;
else
bcdev->thermal_fcc_ua = prev_fcc_ua;
return rc;
}
static int battery_psy_get_prop(struct power_supply *psy,
enum power_supply_property prop,
union power_supply_propval *pval)
{
struct battery_chg_dev *bcdev = power_supply_get_drvdata(psy);
struct psy_state *pst = &bcdev->psy_list[PSY_TYPE_BATTERY];
int prop_id, rc;
pval->intval = -ENODATA;
prop_id = get_property_id(pst, prop);
if (prop_id < 0)
return prop_id;
rc = read_property_id(bcdev, pst, prop_id);
if (rc < 0)
return rc;
switch (prop) {
case POWER_SUPPLY_PROP_MODEL_NAME:
pval->strval = pst->model;
break;
case POWER_SUPPLY_PROP_CAPACITY:
pval->intval = DIV_ROUND_CLOSEST(pst->prop[prop_id], 100);
if (IS_ENABLED(CONFIG_QTI_PMIC_GLINK_CLIENT_DEBUG) &&
(bcdev->fake_soc >= 0 && bcdev->fake_soc <= 100))
pval->intval = bcdev->fake_soc;
break;
case POWER_SUPPLY_PROP_TEMP:
pval->intval = DIV_ROUND_CLOSEST((int)pst->prop[prop_id], 10);
break;
case POWER_SUPPLY_PROP_CHARGE_CONTROL_LIMIT:
pval->intval = bcdev->curr_thermal_level;
break;
case POWER_SUPPLY_PROP_CHARGE_CONTROL_LIMIT_MAX:
pval->intval = bcdev->num_thermal_levels;
break;
default:
pval->intval = pst->prop[prop_id];
break;
}
return rc;
}
static int battery_psy_set_prop(struct power_supply *psy,
enum power_supply_property prop,
const union power_supply_propval *pval)
{
struct battery_chg_dev *bcdev = power_supply_get_drvdata(psy);
switch (prop) {
case POWER_SUPPLY_PROP_CHARGE_CONTROL_LIMIT:
return battery_psy_set_charge_current(bcdev, pval->intval);
default:
return -EINVAL;
}
return 0;
}
static int battery_psy_prop_is_writeable(struct power_supply *psy,
enum power_supply_property prop)
{
switch (prop) {
case POWER_SUPPLY_PROP_CHARGE_CONTROL_LIMIT:
return 1;
default:
break;
}
return 0;
}
static enum power_supply_property battery_props[] = {
POWER_SUPPLY_PROP_STATUS,
POWER_SUPPLY_PROP_HEALTH,
POWER_SUPPLY_PROP_PRESENT,
POWER_SUPPLY_PROP_CHARGE_TYPE,
POWER_SUPPLY_PROP_CAPACITY,
POWER_SUPPLY_PROP_VOLTAGE_OCV,
POWER_SUPPLY_PROP_VOLTAGE_NOW,
POWER_SUPPLY_PROP_VOLTAGE_MAX,
POWER_SUPPLY_PROP_CURRENT_NOW,
POWER_SUPPLY_PROP_CHARGE_CONTROL_LIMIT,
POWER_SUPPLY_PROP_CHARGE_CONTROL_LIMIT_MAX,
POWER_SUPPLY_PROP_TEMP,
POWER_SUPPLY_PROP_TECHNOLOGY,
POWER_SUPPLY_PROP_CHARGE_COUNTER,
POWER_SUPPLY_PROP_CYCLE_COUNT,
POWER_SUPPLY_PROP_CHARGE_FULL_DESIGN,
POWER_SUPPLY_PROP_CHARGE_FULL,
POWER_SUPPLY_PROP_MODEL_NAME,
POWER_SUPPLY_PROP_TIME_TO_FULL_AVG,
POWER_SUPPLY_PROP_TIME_TO_EMPTY_AVG,
POWER_SUPPLY_PROP_POWER_NOW,
POWER_SUPPLY_PROP_POWER_AVG,
};
static const struct power_supply_desc batt_psy_desc = {
.name = "battery",
.type = POWER_SUPPLY_TYPE_BATTERY,
.properties = battery_props,
.num_properties = ARRAY_SIZE(battery_props),
.get_property = battery_psy_get_prop,
.set_property = battery_psy_set_prop,
.property_is_writeable = battery_psy_prop_is_writeable,
};
static int battery_chg_init_psy(struct battery_chg_dev *bcdev)
{
struct power_supply_config psy_cfg = {};
int rc;
psy_cfg.drv_data = bcdev;
psy_cfg.of_node = bcdev->dev->of_node;
bcdev->psy_list[PSY_TYPE_BATTERY].psy =
devm_power_supply_register(bcdev->dev, &batt_psy_desc,
&psy_cfg);
if (IS_ERR(bcdev->psy_list[PSY_TYPE_BATTERY].psy)) {
rc = PTR_ERR(bcdev->psy_list[PSY_TYPE_BATTERY].psy);
pr_err("Failed to register battery power supply, rc=%d\n", rc);
return rc;
}
bcdev->psy_list[PSY_TYPE_USB].psy =
devm_power_supply_register(bcdev->dev, &usb_psy_desc, &psy_cfg);
if (IS_ERR(bcdev->psy_list[PSY_TYPE_USB].psy)) {
rc = PTR_ERR(bcdev->psy_list[PSY_TYPE_USB].psy);
pr_err("Failed to register USB power supply, rc=%d\n", rc);
return rc;
}
bcdev->psy_list[PSY_TYPE_WLS].psy =
devm_power_supply_register(bcdev->dev, &wls_psy_desc, &psy_cfg);
if (IS_ERR(bcdev->psy_list[PSY_TYPE_WLS].psy)) {
rc = PTR_ERR(bcdev->psy_list[PSY_TYPE_WLS].psy);
pr_err("Failed to register wireless power supply, rc=%d\n", rc);
return rc;
}
return 0;
}
static void battery_chg_subsys_up_work(struct work_struct *work)
{
struct battery_chg_dev *bcdev = container_of(work,
struct battery_chg_dev, subsys_up_work);
int rc;
battery_chg_notify_enable(bcdev);
/*
* Give some time after enabling notification so that USB adapter type
* information can be obtained properly which is essential for setting
* USB ICL.
*/
msleep(200);
if (bcdev->last_fcc_ua) {
rc = __battery_psy_set_charge_current(bcdev,
bcdev->last_fcc_ua);
if (rc < 0)
pr_err("Failed to set FCC (%u uA), rc=%d\n",
bcdev->last_fcc_ua, rc);
}
if (bcdev->usb_icl_ua) {
rc = usb_psy_set_icl(bcdev, USB_INPUT_CURR_LIMIT,
bcdev->usb_icl_ua);
if (rc < 0)
pr_err("Failed to set ICL(%u uA), rc=%d\n",
bcdev->usb_icl_ua, rc);
}
}
static int wireless_fw_send_firmware(struct battery_chg_dev *bcdev,
const struct firmware *fw)
{
struct wireless_fw_push_buf_req msg = {};
const u8 *ptr;
u32 i, num_chunks, partial_chunk_size;
int rc;
num_chunks = fw->size / WLS_FW_BUF_SIZE;
partial_chunk_size = fw->size % WLS_FW_BUF_SIZE;
if (!num_chunks)
return -EINVAL;
pr_debug("Updating FW...\n");
ptr = fw->data;
msg.hdr.owner = MSG_OWNER_BC;
msg.hdr.type = MSG_TYPE_REQ_RESP;
msg.hdr.opcode = BC_WLS_FW_PUSH_BUF_REQ;
for (i = 0; i < num_chunks; i++, ptr += WLS_FW_BUF_SIZE) {
msg.fw_chunk_id = i + 1;
memcpy(msg.buf, ptr, WLS_FW_BUF_SIZE);
pr_debug("sending FW chunk %u\n", i + 1);
rc = battery_chg_fw_write(bcdev, &msg, sizeof(msg));
if (rc < 0)
return rc;
}
if (partial_chunk_size) {
msg.fw_chunk_id = i + 1;
memset(msg.buf, 0, WLS_FW_BUF_SIZE);
memcpy(msg.buf, ptr, partial_chunk_size);
pr_debug("sending partial FW chunk %u\n", i + 1);
rc = battery_chg_fw_write(bcdev, &msg, sizeof(msg));
if (rc < 0)
return rc;
}
return 0;
}
static int wireless_fw_check_for_update(struct battery_chg_dev *bcdev,
u32 version, size_t size)
{
struct wireless_fw_check_req req_msg = {};
bcdev->wls_fw_update_reqd = false;
req_msg.hdr.owner = MSG_OWNER_BC;
req_msg.hdr.type = MSG_TYPE_REQ_RESP;
req_msg.hdr.opcode = BC_WLS_FW_CHECK_UPDATE;
req_msg.fw_version = version;
req_msg.fw_size = size;
req_msg.fw_crc = bcdev->wls_fw_crc;
return battery_chg_write(bcdev, &req_msg, sizeof(req_msg));
}
#define IDT_FW_MAJOR_VER_OFFSET 0x94
#define IDT_FW_MINOR_VER_OFFSET 0x96
static int wireless_fw_update(struct battery_chg_dev *bcdev, bool force)
{
const struct firmware *fw;
struct psy_state *pst;
u32 version;
u16 maj_ver, min_ver;
int rc;
pm_stay_awake(bcdev->dev);
/*
* Check for USB presence. If nothing is connected, check whether
* battery SOC is at least 50% before allowing FW update.
*/
pst = &bcdev->psy_list[PSY_TYPE_USB];
rc = read_property_id(bcdev, pst, USB_ONLINE);
if (rc < 0)
goto out;
if (!pst->prop[USB_ONLINE]) {
pst = &bcdev->psy_list[PSY_TYPE_BATTERY];
rc = read_property_id(bcdev, pst, BATT_CAPACITY);
if (rc < 0)
goto out;
if ((pst->prop[BATT_CAPACITY] / 100) < 50) {
pr_err("Battery SOC should be at least 50%% or connect charger\n");
rc = -EINVAL;
goto out;
}
}
rc = firmware_request_nowarn(&fw, bcdev->wls_fw_name, bcdev->dev);
if (rc) {
pr_err("Couldn't get firmware rc=%d\n", rc);
goto out;
}
if (!fw || !fw->data || !fw->size) {
pr_err("Invalid firmware\n");
rc = -EINVAL;
goto release_fw;
}
if (fw->size < SZ_16K) {
pr_err("Invalid firmware size %zu\n", fw->size);
rc = -EINVAL;
goto release_fw;
}
maj_ver = le16_to_cpu(*(__le16 *)(fw->data + IDT_FW_MAJOR_VER_OFFSET));
min_ver = le16_to_cpu(*(__le16 *)(fw->data + IDT_FW_MINOR_VER_OFFSET));
version = maj_ver << 16 | min_ver;
if (force)
version = UINT_MAX;
pr_debug("FW size: %zu version: %#x\n", fw->size, version);
rc = wireless_fw_check_for_update(bcdev, version, fw->size);
if (rc < 0) {
pr_err("Wireless FW update not needed, rc=%d\n", rc);
goto release_fw;
}
if (!bcdev->wls_fw_update_reqd) {
pr_warn("Wireless FW update not required\n");
goto release_fw;
}
/* Wait for IDT to be setup by charger firmware */
msleep(WLS_FW_PREPARE_TIME_MS);
reinit_completion(&bcdev->fw_update_ack);
rc = wireless_fw_send_firmware(bcdev, fw);
if (rc < 0) {
pr_err("Failed to send FW chunk, rc=%d\n", rc);
goto release_fw;
}
rc = wait_for_completion_timeout(&bcdev->fw_update_ack,
msecs_to_jiffies(WLS_FW_UPDATE_TIME_MS));
if (!rc) {
pr_err("Error, timed out updating firmware\n");
rc = -ETIMEDOUT;
goto release_fw;
} else {
rc = 0;
}
pr_info("Wireless FW update done\n");
release_fw:
bcdev->wls_fw_crc = 0;
release_firmware(fw);
out:
pm_relax(bcdev->dev);
return rc;
}
static ssize_t wireless_fw_crc_store(struct class *c,
struct class_attribute *attr,
const char *buf, size_t count)
{
struct battery_chg_dev *bcdev = container_of(c, struct battery_chg_dev,
battery_class);
u16 val;
if (kstrtou16(buf, 0, &val) || !val)
return -EINVAL;
bcdev->wls_fw_crc = val;
return count;
}
static CLASS_ATTR_WO(wireless_fw_crc);
static ssize_t wireless_fw_version_show(struct class *c,
struct class_attribute *attr,
char *buf)
{
struct battery_chg_dev *bcdev = container_of(c, struct battery_chg_dev,
battery_class);
struct wireless_fw_get_version_req req_msg = {};
int rc;
req_msg.hdr.owner = MSG_OWNER_BC;
req_msg.hdr.type = MSG_TYPE_REQ_RESP;
req_msg.hdr.opcode = BC_WLS_FW_GET_VERSION;
rc = battery_chg_write(bcdev, &req_msg, sizeof(req_msg));
if (rc < 0) {
pr_err("Failed to get FW version rc=%d\n", rc);
return rc;
}
return scnprintf(buf, PAGE_SIZE, "%#x\n", bcdev->wls_fw_version);
}
static CLASS_ATTR_RO(wireless_fw_version);
static ssize_t wireless_fw_force_update_store(struct class *c,
struct class_attribute *attr,
const char *buf, size_t count)
{
struct battery_chg_dev *bcdev = container_of(c, struct battery_chg_dev,
battery_class);
bool val;
int rc;
if (kstrtobool(buf, &val) || !val)
return -EINVAL;
rc = wireless_fw_update(bcdev, true);
if (rc < 0)
return rc;
return count;
}
static CLASS_ATTR_WO(wireless_fw_force_update);
static ssize_t wireless_fw_update_store(struct class *c,
struct class_attribute *attr,
const char *buf, size_t count)
{
struct battery_chg_dev *bcdev = container_of(c, struct battery_chg_dev,
battery_class);
bool val;
int rc;
if (kstrtobool(buf, &val) || !val)
return -EINVAL;
rc = wireless_fw_update(bcdev, false);
if (rc < 0)
return rc;
return count;
}
static CLASS_ATTR_WO(wireless_fw_update);
static ssize_t usb_typec_compliant_show(struct class *c,
struct class_attribute *attr, char *buf)
{
struct battery_chg_dev *bcdev = container_of(c, struct battery_chg_dev,
battery_class);
struct psy_state *pst = &bcdev->psy_list[PSY_TYPE_USB];
int rc;
rc = read_property_id(bcdev, pst, USB_TYPEC_COMPLIANT);
if (rc < 0)
return rc;
return scnprintf(buf, PAGE_SIZE, "%d\n",
(int)pst->prop[USB_TYPEC_COMPLIANT]);
}
static CLASS_ATTR_RO(usb_typec_compliant);
static ssize_t usb_real_type_show(struct class *c,
struct class_attribute *attr, char *buf)
{
struct battery_chg_dev *bcdev = container_of(c, struct battery_chg_dev,
battery_class);
struct psy_state *pst = &bcdev->psy_list[PSY_TYPE_USB];
int rc;
rc = read_property_id(bcdev, pst, USB_REAL_TYPE);
if (rc < 0)
return rc;
return scnprintf(buf, PAGE_SIZE, "%s\n",
get_usb_type_name(pst->prop[USB_REAL_TYPE]));
}
static CLASS_ATTR_RO(usb_real_type);
static ssize_t restrict_cur_store(struct class *c, struct class_attribute *attr,
const char *buf, size_t count)
{
struct battery_chg_dev *bcdev = container_of(c, struct battery_chg_dev,
battery_class);
int rc;
u32 fcc_ua, prev_fcc_ua;
if (kstrtou32(buf, 0, &fcc_ua) || fcc_ua > bcdev->thermal_fcc_ua)
return -EINVAL;
prev_fcc_ua = bcdev->restrict_fcc_ua;
bcdev->restrict_fcc_ua = fcc_ua;
if (bcdev->restrict_chg_en) {
rc = __battery_psy_set_charge_current(bcdev, fcc_ua);
if (rc < 0) {
bcdev->restrict_fcc_ua = prev_fcc_ua;
return rc;
}
}
return count;
}
static ssize_t restrict_cur_show(struct class *c, struct class_attribute *attr,
char *buf)
{
struct battery_chg_dev *bcdev = container_of(c, struct battery_chg_dev,
battery_class);
return scnprintf(buf, PAGE_SIZE, "%u\n", bcdev->restrict_fcc_ua);
}
static CLASS_ATTR_RW(restrict_cur);
static ssize_t restrict_chg_store(struct class *c, struct class_attribute *attr,
const char *buf, size_t count)
{
struct battery_chg_dev *bcdev = container_of(c, struct battery_chg_dev,
battery_class);
int rc;
bool val;
if (kstrtobool(buf, &val))
return -EINVAL;
bcdev->restrict_chg_en = val;
rc = __battery_psy_set_charge_current(bcdev, bcdev->restrict_chg_en ?
bcdev->restrict_fcc_ua : bcdev->thermal_fcc_ua);
if (rc < 0)
return rc;
return count;
}
static ssize_t restrict_chg_show(struct class *c, struct class_attribute *attr,
char *buf)
{
struct battery_chg_dev *bcdev = container_of(c, struct battery_chg_dev,
battery_class);
return scnprintf(buf, PAGE_SIZE, "%d\n", bcdev->restrict_chg_en);
}
static CLASS_ATTR_RW(restrict_chg);
static ssize_t fake_soc_store(struct class *c, struct class_attribute *attr,
const char *buf, size_t count)
{
struct battery_chg_dev *bcdev = container_of(c, struct battery_chg_dev,
battery_class);
struct psy_state *pst = &bcdev->psy_list[PSY_TYPE_BATTERY];
int val;
if (kstrtoint(buf, 0, &val))
return -EINVAL;
bcdev->fake_soc = val;
pr_debug("Set fake soc to %d\n", val);
if (IS_ENABLED(CONFIG_QTI_PMIC_GLINK_CLIENT_DEBUG) && pst->psy)
power_supply_changed(pst->psy);
return count;
}
static ssize_t fake_soc_show(struct class *c, struct class_attribute *attr,
char *buf)
{
struct battery_chg_dev *bcdev = container_of(c, struct battery_chg_dev,
battery_class);
return scnprintf(buf, PAGE_SIZE, "%d\n", bcdev->fake_soc);
}
static CLASS_ATTR_RW(fake_soc);
static ssize_t wireless_boost_en_store(struct class *c,
struct class_attribute *attr,
const char *buf, size_t count)
{
struct battery_chg_dev *bcdev = container_of(c, struct battery_chg_dev,
battery_class);
int rc;
bool val;
if (kstrtobool(buf, &val))
return -EINVAL;
rc = write_property_id(bcdev, &bcdev->psy_list[PSY_TYPE_WLS],
WLS_BOOST_EN, val);
if (rc < 0)
return rc;
return count;
}
static ssize_t wireless_boost_en_show(struct class *c,
struct class_attribute *attr, char *buf)
{
struct battery_chg_dev *bcdev = container_of(c, struct battery_chg_dev,
battery_class);
struct psy_state *pst = &bcdev->psy_list[PSY_TYPE_WLS];
int rc;
rc = read_property_id(bcdev, pst, WLS_BOOST_EN);
if (rc < 0)
return rc;
return scnprintf(buf, PAGE_SIZE, "%d\n", pst->prop[WLS_BOOST_EN]);
}
static CLASS_ATTR_RW(wireless_boost_en);
static ssize_t moisture_detection_en_store(struct class *c,
struct class_attribute *attr,
const char *buf, size_t count)
{
struct battery_chg_dev *bcdev = container_of(c, struct battery_chg_dev,
battery_class);
int rc;
bool val;
if (kstrtobool(buf, &val))
return -EINVAL;
rc = write_property_id(bcdev, &bcdev->psy_list[PSY_TYPE_USB],
USB_MOISTURE_DET_EN, val);
if (rc < 0)
return rc;
return count;
}
static ssize_t moisture_detection_en_show(struct class *c,
struct class_attribute *attr, char *buf)
{
struct battery_chg_dev *bcdev = container_of(c, struct battery_chg_dev,
battery_class);
struct psy_state *pst = &bcdev->psy_list[PSY_TYPE_USB];
int rc;
rc = read_property_id(bcdev, pst, USB_MOISTURE_DET_EN);
if (rc < 0)
return rc;
return scnprintf(buf, PAGE_SIZE, "%d\n",
pst->prop[USB_MOISTURE_DET_EN]);
}
static CLASS_ATTR_RW(moisture_detection_en);
static ssize_t moisture_detection_status_show(struct class *c,
struct class_attribute *attr, char *buf)
{
struct battery_chg_dev *bcdev = container_of(c, struct battery_chg_dev,
battery_class);
struct psy_state *pst = &bcdev->psy_list[PSY_TYPE_USB];
int rc;
rc = read_property_id(bcdev, pst, USB_MOISTURE_DET_STS);
if (rc < 0)
return rc;
return scnprintf(buf, PAGE_SIZE, "%d\n",
pst->prop[USB_MOISTURE_DET_STS]);
}
static CLASS_ATTR_RO(moisture_detection_status);
static ssize_t resistance_show(struct class *c,
struct class_attribute *attr, char *buf)
{
struct battery_chg_dev *bcdev = container_of(c, struct battery_chg_dev,
battery_class);
struct psy_state *pst = &bcdev->psy_list[PSY_TYPE_BATTERY];
int rc;
rc = read_property_id(bcdev, pst, BATT_RESISTANCE);
if (rc < 0)
return rc;
return scnprintf(buf, PAGE_SIZE, "%u\n", pst->prop[BATT_RESISTANCE]);
}
static CLASS_ATTR_RO(resistance);
static ssize_t soh_show(struct class *c, struct class_attribute *attr,
char *buf)
{
struct battery_chg_dev *bcdev = container_of(c, struct battery_chg_dev,
battery_class);
struct psy_state *pst = &bcdev->psy_list[PSY_TYPE_BATTERY];
int rc;
rc = read_property_id(bcdev, pst, BATT_SOH);
if (rc < 0)
return rc;
return scnprintf(buf, PAGE_SIZE, "%d\n", pst->prop[BATT_SOH]);
}
static CLASS_ATTR_RO(soh);
static ssize_t ship_mode_en_store(struct class *c, struct class_attribute *attr,
const char *buf, size_t count)
{
struct battery_chg_dev *bcdev = container_of(c, struct battery_chg_dev,
battery_class);
if (kstrtobool(buf, &bcdev->ship_mode_en))
return -EINVAL;
return count;
}
static ssize_t ship_mode_en_show(struct class *c, struct class_attribute *attr,
char *buf)
{
struct battery_chg_dev *bcdev = container_of(c, struct battery_chg_dev,
battery_class);
return scnprintf(buf, PAGE_SIZE, "%d\n", bcdev->ship_mode_en);
}
static CLASS_ATTR_RW(ship_mode_en);
static struct attribute *battery_class_attrs[] = {
&class_attr_soh.attr,
&class_attr_resistance.attr,
&class_attr_moisture_detection_status.attr,
&class_attr_moisture_detection_en.attr,
&class_attr_wireless_boost_en.attr,
&class_attr_fake_soc.attr,
&class_attr_wireless_fw_update.attr,
&class_attr_wireless_fw_force_update.attr,
&class_attr_wireless_fw_version.attr,
&class_attr_wireless_fw_crc.attr,
&class_attr_ship_mode_en.attr,
&class_attr_restrict_chg.attr,
&class_attr_restrict_cur.attr,
&class_attr_usb_real_type.attr,
&class_attr_usb_typec_compliant.attr,
NULL,
};
ATTRIBUTE_GROUPS(battery_class);
#ifdef CONFIG_DEBUG_FS
static void battery_chg_add_debugfs(struct battery_chg_dev *bcdev)
{
int rc;
struct dentry *dir, *file;
dir = debugfs_create_dir("battery_charger", NULL);
if (IS_ERR(dir)) {
rc = PTR_ERR(dir);
pr_err("Failed to create charger debugfs directory, rc=%d\n",
rc);
return;
}
file = debugfs_create_bool("block_tx", 0600, dir, &bcdev->block_tx);
if (IS_ERR(file)) {
rc = PTR_ERR(file);
pr_err("Failed to create block_tx debugfs file, rc=%d\n",
rc);
goto error;
}
bcdev->debugfs_dir = dir;
return;
error:
debugfs_remove_recursive(dir);
}
#else
static void battery_chg_add_debugfs(struct battery_chg_dev *bcdev) { }
#endif
static int battery_chg_parse_dt(struct battery_chg_dev *bcdev)
{
struct device_node *node = bcdev->dev->of_node;
struct psy_state *pst = &bcdev->psy_list[PSY_TYPE_BATTERY];
int i, rc, len;
u32 prev, val;
of_property_read_string(node, "qcom,wireless-fw-name",
&bcdev->wls_fw_name);
rc = of_property_count_elems_of_size(node, "qcom,thermal-mitigation",
sizeof(u32));
if (rc <= 0)
return 0;
len = rc;
rc = read_property_id(bcdev, pst, BATT_CHG_CTRL_LIM_MAX);
if (rc < 0) {
pr_err("Failed to read prop BATT_CHG_CTRL_LIM_MAX, rc=%d\n",
rc);
return rc;
}
prev = pst->prop[BATT_CHG_CTRL_LIM_MAX];
for (i = 0; i < len; i++) {
rc = of_property_read_u32_index(node, "qcom,thermal-mitigation",
i, &val);
if (rc < 0)
return rc;
if (val > prev) {
pr_err("Thermal levels should be in descending order\n");
bcdev->num_thermal_levels = -EINVAL;
return 0;
}
prev = val;
}
bcdev->thermal_levels = devm_kcalloc(bcdev->dev, len + 1,
sizeof(*bcdev->thermal_levels),
GFP_KERNEL);
if (!bcdev->thermal_levels)
return -ENOMEM;
/*
* Element 0 is for normal charging current. Elements from index 1
* onwards is for thermal mitigation charging currents.
*/
bcdev->thermal_levels[0] = pst->prop[BATT_CHG_CTRL_LIM_MAX];
rc = of_property_read_u32_array(node, "qcom,thermal-mitigation",
&bcdev->thermal_levels[1], len);
if (rc < 0) {
pr_err("Error in reading qcom,thermal-mitigation, rc=%d\n", rc);
return rc;
}
bcdev->num_thermal_levels = len;
bcdev->thermal_fcc_ua = pst->prop[BATT_CHG_CTRL_LIM_MAX];
return 0;
}
static int battery_chg_ship_mode(struct notifier_block *nb, unsigned long code,
void *unused)
{
struct battery_charger_ship_mode_req_msg msg = { { 0 } };
struct battery_chg_dev *bcdev = container_of(nb, struct battery_chg_dev,
reboot_notifier);
int rc;
if (!bcdev->ship_mode_en)
return NOTIFY_DONE;
msg.hdr.owner = MSG_OWNER_BC;
msg.hdr.type = MSG_TYPE_REQ_RESP;
msg.hdr.opcode = BC_SHIP_MODE_REQ_SET;
msg.ship_mode_type = SHIP_MODE_PMIC;
if (code == SYS_POWER_OFF) {
rc = battery_chg_write(bcdev, &msg, sizeof(msg));
if (rc < 0)
pr_emerg("Failed to write ship mode: %d\n", rc);
}
return NOTIFY_DONE;
}
static int battery_chg_probe(struct platform_device *pdev)
{
struct battery_chg_dev *bcdev;
struct device *dev = &pdev->dev;
struct pmic_glink_client_data client_data = { };
int rc, i;
bcdev = devm_kzalloc(&pdev->dev, sizeof(*bcdev), GFP_KERNEL);
if (!bcdev)
return -ENOMEM;
bcdev->psy_list[PSY_TYPE_BATTERY].map = battery_prop_map;
bcdev->psy_list[PSY_TYPE_BATTERY].prop_count = BATT_PROP_MAX;
bcdev->psy_list[PSY_TYPE_BATTERY].opcode_get = BC_BATTERY_STATUS_GET;
bcdev->psy_list[PSY_TYPE_BATTERY].opcode_set = BC_BATTERY_STATUS_SET;
bcdev->psy_list[PSY_TYPE_USB].map = usb_prop_map;
bcdev->psy_list[PSY_TYPE_USB].prop_count = USB_PROP_MAX;
bcdev->psy_list[PSY_TYPE_USB].opcode_get = BC_USB_STATUS_GET;
bcdev->psy_list[PSY_TYPE_USB].opcode_set = BC_USB_STATUS_SET;
bcdev->psy_list[PSY_TYPE_WLS].map = wls_prop_map;
bcdev->psy_list[PSY_TYPE_WLS].prop_count = WLS_PROP_MAX;
bcdev->psy_list[PSY_TYPE_WLS].opcode_get = BC_WLS_STATUS_GET;
bcdev->psy_list[PSY_TYPE_WLS].opcode_set = BC_WLS_STATUS_SET;
for (i = 0; i < PSY_TYPE_MAX; i++) {
bcdev->psy_list[i].prop =
devm_kcalloc(&pdev->dev, bcdev->psy_list[i].prop_count,
sizeof(u32), GFP_KERNEL);
if (!bcdev->psy_list[i].prop)
return -ENOMEM;
}
bcdev->psy_list[PSY_TYPE_BATTERY].model =
devm_kzalloc(&pdev->dev, MAX_STR_LEN, GFP_KERNEL);
if (!bcdev->psy_list[PSY_TYPE_BATTERY].model)
return -ENOMEM;
mutex_init(&bcdev->rw_lock);
init_completion(&bcdev->ack);
init_completion(&bcdev->fw_buf_ack);
init_completion(&bcdev->fw_update_ack);
INIT_WORK(&bcdev->subsys_up_work, battery_chg_subsys_up_work);
INIT_WORK(&bcdev->usb_type_work, battery_chg_update_usb_type_work);
atomic_set(&bcdev->state, PMIC_GLINK_STATE_UP);
bcdev->dev = dev;
client_data.id = MSG_OWNER_BC;
client_data.name = "battery_charger";
client_data.msg_cb = battery_chg_callback;
client_data.priv = bcdev;
client_data.state_cb = battery_chg_state_cb;
bcdev->client = pmic_glink_register_client(dev, &client_data);
if (IS_ERR(bcdev->client)) {
rc = PTR_ERR(bcdev->client);
if (rc != -EPROBE_DEFER)
dev_err(dev, "Error in registering with pmic_glink %d\n",
rc);
return rc;
}
bcdev->initialized = true;
bcdev->reboot_notifier.notifier_call = battery_chg_ship_mode;
bcdev->reboot_notifier.priority = 255;
register_reboot_notifier(&bcdev->reboot_notifier);
rc = battery_chg_parse_dt(bcdev);
if (rc < 0) {
dev_err(dev, "Failed to parse dt rc=%d\n", rc);
goto error;
}
bcdev->restrict_fcc_ua = DEFAULT_RESTRICT_FCC_UA;
platform_set_drvdata(pdev, bcdev);
bcdev->fake_soc = -EINVAL;
rc = battery_chg_init_psy(bcdev);
if (rc < 0)
goto error;
bcdev->battery_class.name = "qcom-battery";
bcdev->battery_class.class_groups = battery_class_groups;
rc = class_register(&bcdev->battery_class);
if (rc < 0) {
dev_err(dev, "Failed to create battery_class rc=%d\n", rc);
goto error;
}
battery_chg_add_debugfs(bcdev);
battery_chg_notify_enable(bcdev);
device_init_wakeup(bcdev->dev, true);
return 0;
error:
bcdev->initialized = false;
complete(&bcdev->ack);
pmic_glink_unregister_client(bcdev->client);
unregister_reboot_notifier(&bcdev->reboot_notifier);
return rc;
}
static int battery_chg_remove(struct platform_device *pdev)
{
struct battery_chg_dev *bcdev = platform_get_drvdata(pdev);
int rc;
device_init_wakeup(bcdev->dev, false);
debugfs_remove_recursive(bcdev->debugfs_dir);
class_unregister(&bcdev->battery_class);
unregister_reboot_notifier(&bcdev->reboot_notifier);
rc = pmic_glink_unregister_client(bcdev->client);
if (rc < 0) {
pr_err("Error unregistering from pmic_glink, rc=%d\n", rc);
return rc;
}
return 0;
}
static const struct of_device_id battery_chg_match_table[] = {
{ .compatible = "qcom,battery-charger" },
{},
};
static struct platform_driver battery_chg_driver = {
.driver = {
.name = "qti_battery_charger",
.of_match_table = battery_chg_match_table,
},
.probe = battery_chg_probe,
.remove = battery_chg_remove,
};
module_platform_driver(battery_chg_driver);
MODULE_DESCRIPTION("QTI Glink battery charger driver");
MODULE_LICENSE("GPL v2");
|