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
|
/*
* s2mu00x_battery.c - Example battery driver for S2MU00x series
*
* Copyright (C) 2017 Samsung Electronics Co.Ltd
*
* This program is free software; you can redistribute it and/or
* modify it under the terms of the GNU General Public License
* as published by the Free Software Foundation; either version 2
* of the License, or (at your option) any later version.
*
* 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/version.h>
#include <linux/printk.h>
#include <linux/platform_device.h>
#include <linux/init.h>
#include <linux/module.h>
#include <linux/of_gpio.h>
#include <linux/wakelock.h>
#include <linux/workqueue.h>
#include <linux/power_supply.h>
#include <linux/power/s2mu00x_battery.h>
#include <linux/muic/muic.h>
#include <linux/alarmtimer.h>
#if defined(CONFIG_MUIC_NOTIFIER)
#include <linux/muic/muic_notifier.h>
#endif /* CONFIG_MUIC_NOTIFIER */
#if defined(CONFIG_IFCONN_NOTIFIER)
#include <linux/ifconn/ifconn_notifier.h>
#include <linux/ifconn/ifconn_manager.h>
#include <linux/muic/muic_notifier.h>
#endif
#define FAKE_BAT_LEVEL 50
#define DEFAULT_ALARM_INTERVAL 10
#define SLEEP_ALARM_INTERVAL 30
static char *bat_status_str[] = {
"Unknown",
"Charging",
"Discharging",
"Not-charging",
"Full"
};
static char *health_str[] = {
"Unknown",
"Good",
"Overheat",
"Dead",
"OverVoltage",
"UnspecFailure",
"Cold",
"WatchdogTimerExpire",
"SafetyTimerExpire",
"UnderVoltage",
};
static enum power_supply_property s2mu00x_battery_props[] = {
POWER_SUPPLY_PROP_STATUS,
POWER_SUPPLY_PROP_HEALTH,
POWER_SUPPLY_PROP_ONLINE,
POWER_SUPPLY_PROP_PRESENT,
POWER_SUPPLY_PROP_VOLTAGE_NOW,
POWER_SUPPLY_PROP_VOLTAGE_AVG,
POWER_SUPPLY_PROP_TEMP,
POWER_SUPPLY_PROP_CHARGE_NOW,
POWER_SUPPLY_PROP_CAPACITY,
};
static enum power_supply_property s2mu00x_power_props[] = {
POWER_SUPPLY_PROP_ONLINE,
};
typedef struct s2mu00x_battery_platform_data {
s2mu00x_charging_current_t *charging_current;
char *charger_name;
char *fuelgauge_name;
int max_input_current;
int max_charging_current;
int temp_high;
int temp_high_recovery;
int temp_low;
int temp_low_recovery;
/* full check */
unsigned int full_check_count;
unsigned int chg_recharge_vcell;
unsigned int chg_full_vcell;
/* Initial maximum raw SOC */
unsigned int max_rawsoc;
/* battery */
char *vendor;
int technology;
int battery_type;
void *battery_data;
} s2mu00x_battery_platform_data_t;
struct s2mu00x_battery_info {
struct device *dev;
s2mu00x_battery_platform_data_t *pdata;
struct power_supply *psy_battery;
struct power_supply_desc psy_battery_desc;
struct power_supply *psy_usb;
struct power_supply_desc psy_usb_desc;
struct power_supply *psy_ac;
struct power_supply_desc psy_ac_desc;
struct mutex iolock;
struct wake_lock monitor_wake_lock;
struct workqueue_struct *monitor_wqueue;
struct delayed_work monitor_work;
struct wake_lock vbus_wake_lock;
struct alarm monitor_alarm;
unsigned int monitor_alarm_interval;
int input_current;
int max_input_current;
int charging_current;
int max_charging_current;
int topoff_current;
int cable_type;
unsigned int charging_mode;
#if defined(CONFIG_IFCONN_NOTIFIER)
struct notifier_block ifconn_nb;
#elif defined(CONFIG_MUIC_NOTIFIER)
struct notifier_block batt_nb;
#endif
int full_check_cnt;
/* charging */
bool is_recharging;
bool battery_valid;
int status;
int health;
int voltage_now;
int voltage_avg;
int voltage_ocv;
unsigned int capacity;
unsigned int max_rawsoc;
int current_now; /* current (mA) */
int current_avg; /* average current (mA) */
int current_max; /* input current limit (mA) */
#if defined(CONFIG_MUIC_NOTIFIER)
struct notifier_block cable_check;
#endif
/* temperature check */
int temperature; /* battery temperature(0.1 Celsius)*/
int temp_high;
int temp_high_recovery;
int temp_low;
int temp_low_recovery;
};
static char *s2mu00x_supplied_to[] = {
"s2mu00x-battery",
};
static void get_charging_current(struct s2mu00x_battery_info *battery,
int *input_current, int *charging_current)
{
int max_input_current = battery->max_input_current;
int max_charging_current = battery->max_charging_current;
if (*input_current > max_input_current) {
*input_current = max_input_current;
pr_info("%s: limit input current. (%d)\n", __func__, *input_current);
}
if (*charging_current > max_charging_current) {
*charging_current = max_charging_current;
pr_info("%s: limit charging current. (%d)\n", __func__, *charging_current);
}
}
static int set_charging_current(struct s2mu00x_battery_info *battery)
{
union power_supply_propval value;
int input_current =
battery->pdata->charging_current[battery->cable_type].input_current_limit,
charging_current =
battery->pdata->charging_current[battery->cable_type].fast_charging_current,
topoff_current =
battery->pdata->charging_current[battery->cable_type].full_check_current;
struct power_supply *psy;
int ret;
pr_info("%s: cable_type(%d), current(%d, %d, %d)\n", __func__,
battery->cable_type, input_current, charging_current, topoff_current);
mutex_lock(&battery->iolock);
/*Limit input & charging current according to the max current*/
get_charging_current(battery, &input_current, &charging_current);
/* set input current limit */
if (battery->input_current != input_current) {
value.intval = input_current;
psy = power_supply_get_by_name(battery->pdata->charger_name);
if (!psy)
return -EINVAL;
ret = power_supply_set_property(psy, POWER_SUPPLY_PROP_CURRENT_MAX, &value);
if (ret < 0)
pr_err("%s: Fail to execute property\n", __func__);
battery->input_current = input_current;
}
/* set fast charging current */
if (battery->charging_current != charging_current) {
value.intval = charging_current;
psy = power_supply_get_by_name(battery->pdata->charger_name);
if (!psy)
return -EINVAL;
ret = power_supply_set_property(psy, POWER_SUPPLY_PROP_CURRENT_NOW, &value);
if (ret < 0)
pr_err("%s: Fail to execute property\n", __func__);
battery->charging_current = charging_current;
}
/* set topoff current */
if (battery->topoff_current != topoff_current) {
value.intval = topoff_current;
psy = power_supply_get_by_name(battery->pdata->charger_name);
if (!psy)
return -EINVAL;
ret = power_supply_set_property(psy, POWER_SUPPLY_PROP_CURRENT_FULL, &value);
if (ret < 0)
pr_err("%s: Fail to execute property\n", __func__);
battery->topoff_current = topoff_current;
}
mutex_unlock(&battery->iolock);
return 0;
}
/*
* set_charger_mode(): charger_mode must have one of following values.
* 1. S2MU00X_BAT_CHG_MODE_CHARGING
* Charger on.
* Supply power to system & battery both.
* 2. S2MU00X_BAT_CHG_MODE_CHARGING_OFF
* Buck mode. Stop battery charging.
* But charger supplies system power.
* 3. S2MU00X_BAT_CHG_MODE_BUCK_OFF
* All off. Charger is completely off.
* Do not supply power to battery & system both.
*/
static int set_charger_mode(
struct s2mu00x_battery_info *battery,
int charger_mode)
{
union power_supply_propval val;
struct power_supply *psy;
int ret;
if (charger_mode != S2MU00X_BAT_CHG_MODE_CHARGING)
battery->full_check_cnt = 0;
val.intval = charger_mode;
psy = power_supply_get_by_name(battery->pdata->charger_name);
if (!psy)
return -EINVAL;
ret = power_supply_set_property(psy, POWER_SUPPLY_PROP_CHARGING_ENABLED, &val);
if (ret < 0)
pr_err("%s: Fail to execute property\n", __func__);
return 0;
}
static int set_battery_status(struct s2mu00x_battery_info *battery,
int status)
{
union power_supply_propval value;
struct power_supply *psy;
int ret;
pr_info("%s: current status = %d, new status = %d\n", __func__, battery->status, status);
if (battery->status == status)
return 0;
switch (status) {
case POWER_SUPPLY_STATUS_CHARGING:
/* notify charger cable type */
value.intval = battery->cable_type;
psy = power_supply_get_by_name(battery->pdata->charger_name);
if (!psy)
return -EINVAL;
ret = power_supply_set_property(psy, POWER_SUPPLY_PROP_ONLINE, &value);
if (ret < 0)
pr_err("%s: Fail to execute property\n", __func__);
set_charger_mode(battery, S2MU00X_BAT_CHG_MODE_CHARGING);
set_charging_current(battery);
break;
case POWER_SUPPLY_STATUS_DISCHARGING:
set_charging_current(battery);
/* notify charger cable type */
value.intval = battery->cable_type;
psy = power_supply_get_by_name(battery->pdata->charger_name);
if (!psy)
return -EINVAL;
ret = power_supply_set_property(psy, POWER_SUPPLY_PROP_ONLINE, &value);
if (ret < 0)
pr_err("%s: Fail to execute property\n", __func__);
set_charger_mode(battery, S2MU00X_BAT_CHG_MODE_CHARGING_OFF);
break;
case POWER_SUPPLY_STATUS_NOT_CHARGING:
set_charger_mode(battery, S2MU00X_BAT_CHG_MODE_BUCK_OFF);
/* to recover charger configuration when heath is recovered */
battery->input_current = 0;
battery->charging_current = 0;
battery->topoff_current = 0;
break;
case POWER_SUPPLY_STATUS_FULL:
set_charger_mode(battery, S2MU00X_BAT_CHG_MODE_CHARGING_OFF);
break;
}
/* battery status update */
battery->status = status;
value.intval = battery->status;
psy = power_supply_get_by_name(battery->pdata->charger_name);
if (!psy)
return -EINVAL;
ret = power_supply_set_property(psy, POWER_SUPPLY_PROP_STATUS, &value);
if (ret < 0)
pr_err("%s: Fail to execute property\n", __func__);
return 0;
}
static void set_bat_status_by_cable(struct s2mu00x_battery_info *battery)
{
if (battery->cable_type == POWER_SUPPLY_TYPE_BATTERY ||
battery->cable_type == POWER_SUPPLY_TYPE_UNKNOWN ||
battery->cable_type == POWER_SUPPLY_TYPE_OTG) {
battery->is_recharging = false;
set_battery_status(battery, POWER_SUPPLY_STATUS_DISCHARGING);
return;
}
if (battery->status != POWER_SUPPLY_STATUS_FULL) {
set_battery_status(battery, POWER_SUPPLY_STATUS_CHARGING);
return;
}
dev_info(battery->dev, "%s: abnormal cable_type or status", __func__);
}
static int s2mu00x_battery_get_property(struct power_supply *psy,
enum power_supply_property psp, union power_supply_propval *val)
{
struct s2mu00x_battery_info *battery = power_supply_get_drvdata(psy);
int ret = 0;
dev_dbg(battery->dev, "prop: %d\n", psp);
switch (psp) {
case POWER_SUPPLY_PROP_STATUS:
val->intval = battery->status;
break;
case POWER_SUPPLY_PROP_HEALTH:
val->intval = battery->health;
break;
case POWER_SUPPLY_PROP_ONLINE:
val->intval = battery->cable_type;
break;
case POWER_SUPPLY_PROP_PRESENT:
val->intval = battery->battery_valid;
break;
case POWER_SUPPLY_PROP_VOLTAGE_NOW:
if (!battery->battery_valid)
val->intval = FAKE_BAT_LEVEL;
else
val->intval = battery->voltage_now * 1000;
break;
case POWER_SUPPLY_PROP_VOLTAGE_AVG:
val->intval = battery->voltage_avg * 1000;
break;
case POWER_SUPPLY_PROP_TEMP:
val->intval = battery->temperature;
break;
case POWER_SUPPLY_PROP_CHARGE_NOW:
val->intval = battery->charging_mode;
break;
case POWER_SUPPLY_PROP_CAPACITY:
if (!battery->battery_valid)
val->intval = FAKE_BAT_LEVEL;
else {
if (battery->status == POWER_SUPPLY_STATUS_FULL)
val->intval = 100;
else
val->intval = battery->capacity;
}
break;
default:
ret = -ENODATA;
}
return ret;
}
static int s2mu00x_battery_set_property(struct power_supply *psy,
enum power_supply_property psp,
const union power_supply_propval *val)
{
struct s2mu00x_battery_info *battery = power_supply_get_drvdata(psy);
int ret = 0;
dev_dbg(battery->dev, "prop: %d\n", psp);
switch (psp) {
case POWER_SUPPLY_PROP_STATUS:
set_battery_status(battery, val->intval);
break;
case POWER_SUPPLY_PROP_HEALTH:
battery->health = val->intval;
break;
case POWER_SUPPLY_PROP_ONLINE:
battery->cable_type = val->intval;
break;
default:
ret = -EINVAL;
}
return ret;
}
static int s2mu00x_usb_get_property(struct power_supply *psy,
enum power_supply_property psp,
union power_supply_propval *val)
{
struct s2mu00x_battery_info *battery = power_supply_get_drvdata(psy);
if (psp != POWER_SUPPLY_PROP_ONLINE)
return -EINVAL;
/* Set enable=1 only if the USB charger is connected */
switch (battery->cable_type) {
case POWER_SUPPLY_TYPE_USB:
case POWER_SUPPLY_TYPE_USB_DCP:
case POWER_SUPPLY_TYPE_USB_CDP:
case POWER_SUPPLY_TYPE_USB_ACA:
val->intval = 1;
break;
default:
val->intval = 0;
break;
}
return 0;
}
/*
* AC charger operations
*/
static int s2mu00x_ac_get_property(struct power_supply *psy,
enum power_supply_property psp,
union power_supply_propval *val)
{
struct s2mu00x_battery_info *battery = power_supply_get_drvdata(psy);
if (psp != POWER_SUPPLY_PROP_ONLINE)
return -EINVAL;
/* Set enable=1 only if the AC charger is connected */
switch (battery->cable_type) {
case POWER_SUPPLY_TYPE_MAINS:
case POWER_SUPPLY_TYPE_UNKNOWN:
case POWER_SUPPLY_TYPE_PREPARE_TA:
case POWER_SUPPLY_TYPE_HV_MAINS:
val->intval = 1;
break;
default:
val->intval = 0;
break;
}
return 0;
}
#if defined(CONFIG_MUIC_NOTIFIER) || defined(CONFIG_IFCONN_NOTIFIER)
static int s2mu00x_bat_cable_check(struct s2mu00x_battery_info *battery,
muic_attached_dev_t attached_dev)
{
int current_cable_type = -1;
pr_info("[%s]ATTACHED(%d)\n", __func__, attached_dev);
switch (attached_dev) {
case ATTACHED_DEV_SMARTDOCK_MUIC:
case ATTACHED_DEV_DESKDOCK_MUIC:
current_cable_type = POWER_SUPPLY_TYPE_BATTERY;
break;
case ATTACHED_DEV_OTG_MUIC:
case ATTACHED_DEV_HMT_MUIC:
current_cable_type = POWER_SUPPLY_TYPE_OTG;
break;
case ATTACHED_DEV_USB_MUIC:
case ATTACHED_DEV_SMARTDOCK_USB_MUIC:
case ATTACHED_DEV_UNOFFICIAL_ID_USB_MUIC:
current_cable_type = POWER_SUPPLY_TYPE_USB;
break;
case ATTACHED_DEV_TA_MUIC:
case ATTACHED_DEV_CARDOCK_MUIC:
case ATTACHED_DEV_DESKDOCK_VB_MUIC:
case ATTACHED_DEV_SMARTDOCK_TA_MUIC:
case ATTACHED_DEV_AFC_CHARGER_5V_MUIC:
case ATTACHED_DEV_UNOFFICIAL_TA_MUIC:
case ATTACHED_DEV_UNOFFICIAL_ID_TA_MUIC:
case ATTACHED_DEV_UNOFFICIAL_ID_ANY_MUIC:
case ATTACHED_DEV_QC_CHARGER_5V_MUIC:
case ATTACHED_DEV_UNSUPPORTED_ID_VB_MUIC:
current_cable_type = POWER_SUPPLY_TYPE_MAINS;
break;
case ATTACHED_DEV_CDP_MUIC:
case ATTACHED_DEV_UNOFFICIAL_ID_CDP_MUIC:
current_cable_type = POWER_SUPPLY_TYPE_USB_CDP;
break;
case ATTACHED_DEV_AFC_CHARGER_9V_MUIC:
case ATTACHED_DEV_QC_CHARGER_9V_MUIC:
#if defined(CONFIG_HV_MUIC_S2MU004_PE)
case ATTACHED_DEV_PE_CHARGER_9V_MUIC:
#endif
current_cable_type = POWER_SUPPLY_TYPE_HV_MAINS;
break;
case ATTACHED_DEV_UNDEFINED_CHARGING_MUIC:
current_cable_type = POWER_SUPPLY_TYPE_UNKNOWN;
break;
#if defined(CONFIG_IFCONN_NOTIFIER)
case ATTACHED_DEV_UNDEFINED_RANGE_MUIC:
current_cable_type = POWER_SUPPLY_TYPE_BATTERY;
break;
case ATTACHED_DEV_TIMEOUT_OPEN_MUIC:
current_cable_type = POWER_SUPPLY_TYPE_USB;
break;
case ATTACHED_DEV_AFC_CHARGER_PREPARE_MUIC:
case ATTACHED_DEV_QC_CHARGER_PREPARE_MUIC:
current_cable_type = POWER_SUPPLY_TYPE_PREPARE_TA;
break;
case ATTACHED_DEV_AFC_CHARGER_9V_DUPLI_MUIC:
current_cable_type = POWER_SUPPLY_TYPE_HV_MAINS;
break;
case ATTACHED_DEV_VZW_INCOMPATIBLE_MUIC:
current_cable_type = POWER_SUPPLY_TYPE_UNKNOWN;
break;
#endif
default:
current_cable_type = POWER_SUPPLY_TYPE_BATTERY;
pr_err("%s: invalid type for charger:%d\n",
__func__, attached_dev);
}
return current_cable_type;
}
#endif
#if defined(CONFIG_MUIC_NOTIFIER)
static int s2mu00x_battery_handle_notification(struct notifier_block *nb,
unsigned long action, void *data)
{
muic_attached_dev_t attached_dev = *(muic_attached_dev_t *)data;
const char *cmd;
int cable_type;
union power_supply_propval value;
struct s2mu00x_battery_info *battery =
container_of(nb, struct s2mu00x_battery_info, batt_nb);
struct power_supply *psy;
int ret;
if (attached_dev == ATTACHED_DEV_MHL_MUIC)
return 0;
switch (action) {
case MUIC_NOTIFY_CMD_DETACH:
case MUIC_NOTIFY_CMD_LOGICALLY_DETACH:
cmd = "DETACH";
cable_type = POWER_SUPPLY_TYPE_BATTERY;
break;
case MUIC_NOTIFY_CMD_ATTACH:
case MUIC_NOTIFY_CMD_LOGICALLY_ATTACH:
cmd = "ATTACH";
cable_type = s2mu00x_bat_cable_check(battery, attached_dev);
break;
default:
cmd = "ERROR";
cable_type = -1;
break;
}
pr_info("%s: current_cable(%d) former cable_type(%d) battery_valid(%d)\n",
__func__, cable_type, battery->cable_type,
battery->battery_valid);
if (battery->battery_valid == false)
pr_info("%s: Battery is disconnected\n", __func__);
battery->cable_type = cable_type;
pr_info("%s: CMD=%s, attached_dev=%d battery_cable=%d\n",
__func__, cmd, attached_dev, battery->cable_type);
if (attached_dev == ATTACHED_DEV_OTG_MUIC) {
if (!strcmp(cmd, "ATTACH")) {
value.intval = true;
psy = power_supply_get_by_name(battery->pdata->charger_name);
if (!psy)
return -EINVAL;
ret = power_supply_set_property(psy, POWER_SUPPLY_PROP_CHARGE_OTG_CONTROL, &value);
if (ret < 0)
pr_err("%s: Fail to execute property\n", __func__);
pr_info("%s: OTG cable attached\n", __func__);
} else {
value.intval = false;
psy = power_supply_get_by_name(battery->pdata->charger_name);
if (!psy)
return -EINVAL;
ret = power_supply_set_property(psy, POWER_SUPPLY_PROP_CHARGE_OTG_CONTROL, &value);
if (ret < 0)
pr_err("%s: Fail to execute property\n", __func__);
pr_info("%s: OTG cable detached\n", __func__);
}
}
if (battery->cable_type == POWER_SUPPLY_TYPE_BATTERY ||
battery->cable_type == POWER_SUPPLY_TYPE_UNKNOWN) {
battery->is_recharging = false;
set_battery_status(battery, POWER_SUPPLY_STATUS_DISCHARGING);
} else {
if (battery->cable_type == POWER_SUPPLY_TYPE_OTG) {
set_battery_status(battery, POWER_SUPPLY_STATUS_DISCHARGING);
} else {
if (battery->status != POWER_SUPPLY_STATUS_FULL)
set_battery_status(battery, POWER_SUPPLY_STATUS_CHARGING);
}
}
pr_info(
"%s: Status(%s), Health(%s), Cable(%d), Recharging(%d)\n",
__func__,
bat_status_str[battery->status],
health_str[battery->health],
battery->cable_type,
battery->is_recharging
);
power_supply_changed(battery->psy_battery);
alarm_cancel(&battery->monitor_alarm);
wake_lock(&battery->monitor_wake_lock);
queue_delayed_work(battery->monitor_wqueue, &battery->monitor_work, 0);
return 0;
}
#endif
#if defined(CONFIG_IFCONN_NOTIFIER)
static int s2mu00x_ifconn_handle_notification(struct notifier_block *nb,
unsigned long action, void *data)
{
struct s2mu00x_battery_info *battery =
container_of(nb, struct s2mu00x_battery_info, ifconn_nb);
struct ifconn_notifier_template *ifconn_info = (struct ifconn_notifier_template *)data;
muic_attached_dev_t attached_dev = (muic_attached_dev_t)ifconn_info->event;
const char *cmd;
int cable_type;
union power_supply_propval value;
struct power_supply *psy;
int ret;
dev_info(battery->dev, "%s: action (%ld) dump(0x%01x, 0x%01x, 0x%02x, 0x%04x, 0x%04x, 0x%04x, 0x%04x)\n",
__func__, action, ifconn_info->src, ifconn_info->dest, ifconn_info->id,
ifconn_info->attach, ifconn_info->rprd, ifconn_info->cable_type, ifconn_info->event);
ifconn_info->cable_type = (muic_attached_dev_t)ifconn_info->event;
action = ifconn_info->id;
if (attached_dev == ATTACHED_DEV_MHL_MUIC)
return 0;
switch (action) {
case IFCONN_NOTIFY_ID_DETACH:
cmd = "DETACH";
cable_type = POWER_SUPPLY_TYPE_BATTERY;
break;
case IFCONN_NOTIFY_ID_ATTACH:
cmd = "ATTACH";
cable_type = s2mu00x_bat_cable_check(battery, attached_dev);
break;
default:
cmd = "ERROR";
cable_type = -1;
break;
}
pr_info("%s: CMD[%s] attached_dev(%d) current_cable(%d) former cable_type(%d) battery_valid(%d)\n",
__func__, cmd, attached_dev, cable_type,
battery->cable_type, battery->battery_valid);
if (battery->battery_valid == false)
pr_info("%s: Battery is disconnected\n", __func__);
battery->cable_type = cable_type;
if (attached_dev == ATTACHED_DEV_OTG_MUIC) {
if (!strcmp(cmd, "ATTACH")) {
value.intval = true;
psy = power_supply_get_by_name(battery->pdata->charger_name);
if (!psy)
return -EINVAL;
ret = power_supply_set_property(psy, POWER_SUPPLY_PROP_CHARGE_OTG_CONTROL, &value);
if (ret < 0)
pr_err("%s: Fail to execute property\n", __func__);
pr_info("%s: OTG cable attached\n", __func__);
} else {
value.intval = false;
psy = power_supply_get_by_name(battery->pdata->charger_name);
if (!psy)
return -EINVAL;
ret = power_supply_set_property(psy, POWER_SUPPLY_PROP_CHARGE_OTG_CONTROL, &value);
if (ret < 0)
pr_err("%s: Fail to execute property\n", __func__);
pr_info("%s: OTG cable detached\n", __func__);
}
}
set_bat_status_by_cable(battery);
pr_info("%s: Status(%s), Health(%s), Cable(%d), Recharging(%d)\n",
__func__, bat_status_str[battery->status], health_str[battery->health],
battery->cable_type, battery->is_recharging);
power_supply_changed(battery->psy_battery);
alarm_cancel(&battery->monitor_alarm);
wake_lock(&battery->monitor_wake_lock);
queue_delayed_work(battery->monitor_wqueue, &battery->monitor_work, 0);
return 0;
}
#endif
static void get_battery_capacity(struct s2mu00x_battery_info *battery)
{
union power_supply_propval value;
struct power_supply *psy;
int ret;
unsigned int raw_soc = 0;
psy = power_supply_get_by_name(battery->pdata->fuelgauge_name);
ret = power_supply_get_property(psy, POWER_SUPPLY_PROP_CAPACITY, &value);
if (ret < 0)
pr_err("%s: Fail to execute property\n", __func__);
raw_soc = value.intval;
if (battery->status == POWER_SUPPLY_STATUS_FULL)
battery->max_rawsoc = raw_soc;
battery->capacity = (raw_soc*100)/battery->max_rawsoc;
if (battery->capacity > 100)
battery->capacity = 100;
/* Display 50% SOC for SMDK. If fg Bootloader is not applied,
* this case is considered as surge case. This makes abnormal
* offset for surge W/A of f.g
* Please delete below line after f.g bootloader bring up
*/
battery->capacity = 50;
dev_info(battery->dev, "%s: SOC(%u), rawsoc(%d), max_rawsoc(%u).\n",
__func__, battery->capacity, raw_soc, battery->max_rawsoc);
}
static int get_battery_info(struct s2mu00x_battery_info *battery)
{
union power_supply_propval value;
struct power_supply *psy;
int ret;
/*Get fuelgauge psy*/
psy = power_supply_get_by_name(battery->pdata->fuelgauge_name);
if (!psy)
return -EINVAL;
/* Get voltage and current value */
ret = power_supply_get_property(psy, POWER_SUPPLY_PROP_VOLTAGE_NOW, &value);
if (ret < 0)
pr_err("%s: Fail to execute property\n", __func__);
battery->voltage_now = value.intval;
value.intval = S2MU00X_BATTERY_VOLTAGE_AVERAGE;
ret = power_supply_get_property(psy, POWER_SUPPLY_PROP_VOLTAGE_AVG, &value);
if (ret < 0)
pr_err("%s: Fail to execute property\n", __func__);
battery->voltage_avg = value.intval;
value.intval = S2MU00X_BATTERY_CURRENT_MA;
ret = power_supply_get_property(psy, POWER_SUPPLY_PROP_CURRENT_NOW, &value);
if (ret < 0)
pr_err("%s: Fail to execute property\n", __func__);
battery->current_now = value.intval;
value.intval = S2MU00X_BATTERY_CURRENT_MA;
ret = power_supply_get_property(psy, POWER_SUPPLY_PROP_CURRENT_AVG, &value);
if (ret < 0)
pr_err("%s: Fail to execute property\n", __func__);
battery->current_avg = value.intval;
/* Get temperature info */
ret = power_supply_get_property(psy, POWER_SUPPLY_PROP_TEMP, &value);
if (ret < 0)
pr_err("%s: Fail to execute property\n", __func__);
battery->temperature = value.intval;
get_battery_capacity(battery);
/*Get charger psy*/
psy = power_supply_get_by_name(battery->pdata->charger_name);
if (!psy)
return -EINVAL;
/* Get input current limit */
ret = power_supply_get_property(psy, POWER_SUPPLY_PROP_CURRENT_MAX, &value);
if (ret < 0)
pr_err("%s: Fail to execute property\n", __func__);
battery->current_max = value.intval;
/* Get charger status*/
ret = power_supply_get_property(psy, POWER_SUPPLY_PROP_STATUS, &value);
if (ret < 0)
pr_err("%s: Fail to execute property\n", __func__);
if (battery->status != value.intval)
pr_err("%s: battery status = %d, charger status = %d\n",
__func__, battery->status, value.intval);
dev_info(battery->dev,
"%s:Vnow(%dmV),Inow(%dmA),Imax(%dmA),SOC(%d%%),Tbat(%d)\n",
__func__,
battery->voltage_now, battery->current_now,
battery->current_max, battery->capacity,
battery->temperature
);
dev_dbg(battery->dev,
"%s,Vavg(%dmV),Vocv(%dmV),Iavg(%dmA)\n",
battery->battery_valid ? "Connected" : "Disconnected",
battery->voltage_avg, battery->voltage_ocv, battery->current_avg);
return 0;
}
static int get_battery_health(struct s2mu00x_battery_info *battery)
{
union power_supply_propval value;
int health = POWER_SUPPLY_HEALTH_UNKNOWN;
struct power_supply *psy;
int ret;
/* Get health status from charger */
psy = power_supply_get_by_name(battery->pdata->charger_name);
if (!psy)
return -EINVAL;
ret = power_supply_get_property(psy, POWER_SUPPLY_PROP_HEALTH, &value);
if (ret < 0)
pr_err("%s: Fail to execute property\n", __func__);
health = value.intval;
return health;
}
static int get_temperature_health(struct s2mu00x_battery_info *battery)
{
int health = POWER_SUPPLY_HEALTH_UNKNOWN;
switch (battery->health) {
case POWER_SUPPLY_HEALTH_OVERHEAT:
if (battery->temperature < battery->temp_high_recovery)
health = POWER_SUPPLY_HEALTH_GOOD;
else
health = POWER_SUPPLY_HEALTH_OVERHEAT;
break;
case POWER_SUPPLY_HEALTH_COLD:
if (battery->temperature > battery->temp_low_recovery)
health = POWER_SUPPLY_HEALTH_GOOD;
else
health = POWER_SUPPLY_HEALTH_COLD;
break;
case POWER_SUPPLY_HEALTH_GOOD:
default:
if (battery->temperature > battery->temp_high)
health = POWER_SUPPLY_HEALTH_OVERHEAT;
else if (battery->temperature < battery->temp_low)
health = POWER_SUPPLY_HEALTH_COLD;
else
health = POWER_SUPPLY_HEALTH_GOOD;
break;
}
/* For test, Temperature health is always good*/
health = POWER_SUPPLY_HEALTH_GOOD;
return health;
}
static void check_health(struct s2mu00x_battery_info *battery)
{
int battery_health = 0;
int temperature_health = 0;
battery_health = get_battery_health(battery);
temperature_health = get_temperature_health(battery);
pr_info("%s: T = %d, bat_health(%s), T_health(%s), Charging(%s)\n",
__func__, battery->temperature, health_str[battery_health],
health_str[temperature_health], bat_status_str[battery->status]);
/* If battery & temperature both are normal,
* set battery->health GOOD and recover battery->status
*/
if (battery_health == POWER_SUPPLY_HEALTH_GOOD &&
temperature_health == POWER_SUPPLY_HEALTH_GOOD) {
battery->health = POWER_SUPPLY_HEALTH_GOOD;
if (battery->status == POWER_SUPPLY_STATUS_NOT_CHARGING)
set_bat_status_by_cable(battery);
return;
}
switch (battery_health) {
case POWER_SUPPLY_HEALTH_OVERVOLTAGE:
case POWER_SUPPLY_HEALTH_UNDERVOLTAGE:
case POWER_SUPPLY_HEALTH_UNKNOWN:
battery->health = battery_health;
goto abnormal_health;
default:
break;
}
switch (temperature_health) {
case POWER_SUPPLY_HEALTH_OVERHEAT:
case POWER_SUPPLY_HEALTH_COLD:
case POWER_SUPPLY_HEALTH_UNKNOWN:
battery->health = temperature_health;
goto abnormal_health;
default:
break;
}
pr_err("%s: Abnormal case of temperature & battery health.\n", __func__);
return;
abnormal_health:
if (battery->status != POWER_SUPPLY_STATUS_NOT_CHARGING) {
battery->is_recharging = false;
/* Take the wakelock during 10 seconds
* when not_charging status is detected
*/
wake_lock_timeout(&battery->vbus_wake_lock, HZ * 10);
set_battery_status(battery, POWER_SUPPLY_STATUS_NOT_CHARGING);
}
}
static void check_charging_full(
struct s2mu00x_battery_info *battery)
{
pr_info("%s Start\n", __func__);
if ((battery->status == POWER_SUPPLY_STATUS_DISCHARGING) ||
(battery->status == POWER_SUPPLY_STATUS_NOT_CHARGING)) {
dev_dbg(battery->dev,
"%s: No Need to Check Full-Charged\n", __func__);
return;
}
/* 1. Recharging check */
if (battery->status == POWER_SUPPLY_STATUS_FULL &&
battery->voltage_now < battery->pdata->chg_recharge_vcell &&
!battery->is_recharging) {
pr_info("%s: Recharging start\n", __func__);
set_battery_status(battery, POWER_SUPPLY_STATUS_CHARGING);
battery->is_recharging = true;
}
/* 2. Full charged check */
if ((battery->current_now > 0 && battery->current_now <
battery->pdata->charging_current[
battery->cable_type].full_check_current) &&
(battery->voltage_avg > battery->pdata->chg_full_vcell)) {
battery->full_check_cnt++;
pr_info("%s: Full Check Cnt (%d)\n", __func__, battery->full_check_cnt);
} else if (battery->full_check_cnt != 0) {
/* Reset full check cnt when it is out of full condition */
battery->full_check_cnt = 0;
pr_info("%s: Reset Full Check Cnt\n", __func__);
}
/* 3. If full charged, turn off charging. */
if (battery->full_check_cnt >= battery->pdata->full_check_count) {
battery->full_check_cnt = 0;
battery->is_recharging = false;
set_battery_status(battery, POWER_SUPPLY_STATUS_FULL);
pr_info("%s: Full charged, charger off\n", __func__);
}
}
static void bat_monitor_work(struct work_struct *work)
{
struct s2mu00x_battery_info *battery =
container_of(work, struct s2mu00x_battery_info, monitor_work.work);
union power_supply_propval value;
struct power_supply *psy;
int ret;
pr_info("%s: start monitoring\n", __func__);
psy = power_supply_get_by_name(battery->pdata->charger_name);
if (!psy)
return;
ret = power_supply_get_property(psy, POWER_SUPPLY_PROP_PRESENT, &value);
if (ret < 0)
pr_err("%s: Fail to execute property\n", __func__);
if (!value.intval) {
battery->battery_valid = false;
pr_info("%s: There is no battery, skip monitoring.\n", __func__);
goto continue_monitor;
} else
battery->battery_valid = true;
get_battery_info(battery);
check_health(battery);
check_charging_full(battery);
power_supply_changed(battery->psy_battery);
continue_monitor:
pr_err(
"%s: Status(%s), Health(%s), Cable(%d), Recharging(%d)\n",
__func__,
bat_status_str[battery->status],
health_str[battery->health],
battery->cable_type,
battery->is_recharging
);
alarm_cancel(&battery->monitor_alarm);
alarm_start_relative(&battery->monitor_alarm, ktime_set(battery->monitor_alarm_interval, 0));
wake_unlock(&battery->monitor_wake_lock);
}
#ifdef CONFIG_OF
static int s2mu00x_battery_parse_dt(struct device *dev,
struct s2mu00x_battery_info *battery)
{
struct device_node *np = of_find_node_by_name(NULL, "battery");
s2mu00x_battery_platform_data_t *pdata = battery->pdata;
int ret = 0, len;
unsigned int i;
const u32 *p;
u32 temp;
u32 default_input_current, default_charging_current, default_full_check_current;
if (!np) {
pr_info("%s np NULL(battery)\n", __func__);
return -1;
}
ret = of_property_read_string(np,
"battery,vendor", (char const **)&pdata->vendor);
if (ret)
pr_info("%s: Vendor is empty\n", __func__);
ret = of_property_read_string(np,
"battery,charger_name", (char const **)&pdata->charger_name);
if (ret)
pr_info("%s: Charger name is empty\n", __func__);
ret = of_property_read_string(np,
"battery,fuelgauge_name", (char const **)&pdata->fuelgauge_name);
if (ret)
pr_info("%s: Fuelgauge name is empty\n", __func__);
ret = of_property_read_u32(np, "battery,technology",
&pdata->technology);
if (ret)
pr_info("%s : technology is empty\n", __func__);
p = of_get_property(np, "battery,input_current_limit", &len);
if (!p)
return 1;
len = len / sizeof(u32);
if (len < POWER_SUPPLY_TYPE_END)
len = POWER_SUPPLY_TYPE_END;
pdata->charging_current = kzalloc(sizeof(s2mu00x_charging_current_t) * len,
GFP_KERNEL);
ret = of_property_read_u32(np, "battery,default_input_current",
&default_input_current);
if (ret)
pr_info("%s : default_input_current is empty\n", __func__);
ret = of_property_read_u32(np, "battery,default_charging_current",
&default_charging_current);
if (ret)
pr_info("%s : default_charging_current is empty\n", __func__);
ret = of_property_read_u32(np, "battery,default_full_check_current",
&default_full_check_current);
if (ret)
pr_info("%s : default_full_check_current is empty\n", __func__);
for (i = 0; i < len; i++) {
ret = of_property_read_u32_index(np,
"battery,input_current_limit", i,
&pdata->charging_current[i].input_current_limit);
if (ret) {
pr_info("%s : Input_current_limit is empty\n",
__func__);
pdata->charging_current[i].input_current_limit = default_input_current;
}
ret = of_property_read_u32_index(np,
"battery,fast_charging_current", i,
&pdata->charging_current[i].fast_charging_current);
if (ret) {
pr_info("%s : Fast charging current is empty\n",
__func__);
pdata->charging_current[i].fast_charging_current = default_charging_current;
}
ret = of_property_read_u32_index(np,
"battery,full_check_current", i,
&pdata->charging_current[i].full_check_current);
if (ret) {
pr_info("%s : Full check current is empty\n",
__func__);
pdata->charging_current[i].full_check_current = default_full_check_current;
}
}
ret = of_property_read_u32(np, "battery,max_input_current",
&pdata->max_input_current);
if (ret)
pr_info("%s : max_input_current is empty\n", __func__);
ret = of_property_read_u32(np, "battery,max_charging_current",
&pdata->max_charging_current);
if (ret)
pr_info("%s : max_charging_current is empty\n", __func__);
ret = of_property_read_u32(np, "battery,temp_high", &temp);
if (ret) {
pr_info("%s : temp_high is empty\n", __func__);
pdata->temp_high = 500;
} else
pdata->temp_high = (int)temp;
ret = of_property_read_u32(np, "battery,temp_high_recovery", &temp);
if (ret) {
pr_info("%s : temp_high_recovery is empty\n", __func__);
pdata->temp_high_recovery = pdata->temp_high - 50;
} else
pdata->temp_high_recovery = (int)temp;
ret = of_property_read_u32(np, "battery,temp_low", &temp);
if (ret) {
pr_info("%s : temp_low is empty\n", __func__);
pdata->temp_low = 100;
} else
pdata->temp_low = (int)temp;
ret = of_property_read_u32(np, "battery,temp_low_recovery", &temp);
if (ret) {
pr_info("%s : temp_low_recovery is empty\n", __func__);
pdata->temp_low_recovery = pdata->temp_low + 50;
} else
pdata->temp_low_recovery = (int)temp;
pr_info("%s : temp_high(%d), temp_high_recovery(%d), temp_low(%d), temp_low_recovery(%d)\n",
__func__,
pdata->temp_high, pdata->temp_high_recovery,
pdata->temp_low, pdata->temp_low_recovery);
ret = of_property_read_u32(np, "battery,full_check_count",
&pdata->full_check_count);
if (ret)
pr_info("%s : full_check_count is empty\n", __func__);
ret = of_property_read_u32(np, "battery,chg_full_vcell",
&pdata->chg_full_vcell);
if (ret)
pr_info("%s : chg_full_vcell is empty\n", __func__);
ret = of_property_read_u32(np, "battery,chg_recharge_vcell",
&pdata->chg_recharge_vcell);
if (ret)
pr_info("%s : chg_recharge_vcell is empty\n", __func__);
ret = of_property_read_u32(np, "battery,max_rawsoc",
&pdata->max_rawsoc);
if (ret)
pr_info("%s : max_rawsoc is empty\n", __func__);
pr_info("%s:DT parsing is done, vendor : %s, technology : %d\n",
__func__, pdata->vendor, pdata->technology);
return ret;
}
#else
static int s2mu00x_battery_parse_dt(struct device *dev,
struct s2mu00x_battery_platform_data *pdata)
{
return pdev->dev.platform_data;
}
#endif
static const struct of_device_id s2mu00x_battery_match_table[] = {
{ .compatible = "samsung,s2mu00x-battery",},
{},
};
static enum alarmtimer_restart bat_monitor_alarm(
struct alarm *alarm, ktime_t now)
{
struct s2mu00x_battery_info *battery = container_of(alarm,
struct s2mu00x_battery_info, monitor_alarm);
wake_lock(&battery->monitor_wake_lock);
queue_delayed_work(battery->monitor_wqueue, &battery->monitor_work, 0);
return ALARMTIMER_NORESTART;
}
static int s2mu00x_battery_probe(struct platform_device *pdev)
{
struct s2mu00x_battery_info *battery;
struct power_supply_config psy_cfg = {};
union power_supply_propval value;
int ret = 0, temp = 0;
struct power_supply *psy;
#ifndef CONFIG_OF
int i;
#endif
pr_info("%s: S2MU00x battery driver loading\n", __func__);
/* Allocate necessary device data structures */
battery = kzalloc(sizeof(*battery), GFP_KERNEL);
if (!battery)
return -ENOMEM;
pr_info("%s: battery is allocated\n", __func__);
battery->pdata = devm_kzalloc(&pdev->dev, sizeof(*(battery->pdata)),
GFP_KERNEL);
if (!battery->pdata) {
ret = -ENOMEM;
goto err_bat_free;
}
pr_info("%s: pdata is allocated\n", __func__);
/* Get device/board dependent configuration data from DT */
temp = s2mu00x_battery_parse_dt(&pdev->dev, battery);
if (temp) {
pr_info("%s: s2mu00x_battery_parse_dt(&pdev->dev, battery) == %d\n", __func__, temp);
dev_err(&pdev->dev, "%s: Failed to get battery dt\n", __func__);
ret = -EINVAL;
goto err_parse_dt_nomem;
}
pr_info("%s: DT parsing is done\n", __func__);
/* Set driver data */
platform_set_drvdata(pdev, battery);
battery->dev = &pdev->dev;
mutex_init(&battery->iolock);
wake_lock_init(&battery->monitor_wake_lock, WAKE_LOCK_SUSPEND,
"sec-battery-monitor");
wake_lock_init(&battery->vbus_wake_lock, WAKE_LOCK_SUSPEND,
"sec-battery-vbus");
/* Inintialization of battery information */
battery->status = POWER_SUPPLY_STATUS_DISCHARGING;
battery->health = POWER_SUPPLY_HEALTH_GOOD;
battery->input_current = 0;
battery->charging_current = 0;
battery->topoff_current = 0;
battery->max_input_current = battery->pdata->max_input_current;
battery->max_charging_current = battery->pdata->max_charging_current;
battery->temp_high = battery->pdata->temp_high;
battery->temp_high_recovery = battery->pdata->temp_high_recovery;
battery->temp_low = battery->pdata->temp_low;
battery->temp_low_recovery = battery->pdata->temp_low_recovery;
battery->max_rawsoc = battery->pdata->max_rawsoc;
battery->is_recharging = false;
battery->cable_type = POWER_SUPPLY_TYPE_BATTERY;
psy = power_supply_get_by_name(battery->pdata->charger_name);
if (!psy)
return -EINVAL;
ret = power_supply_get_property(psy, POWER_SUPPLY_PROP_PRESENT, &value);
if (ret < 0)
pr_err("%s: Fail to execute property\n", __func__);
if (!value.intval)
battery->battery_valid = false;
else
battery->battery_valid = true;
/* Register battery as "POWER_SUPPLY_TYPE_BATTERY" */
battery->psy_battery_desc.name = "battery";
battery->psy_battery_desc.type = POWER_SUPPLY_TYPE_BATTERY;
battery->psy_battery_desc.get_property = s2mu00x_battery_get_property;
battery->psy_battery_desc.set_property = s2mu00x_battery_set_property;
battery->psy_battery_desc.properties = s2mu00x_battery_props;
battery->psy_battery_desc.num_properties = ARRAY_SIZE(s2mu00x_battery_props);
battery->psy_usb_desc.name = "usb";
battery->psy_usb_desc.type = POWER_SUPPLY_TYPE_USB;
battery->psy_usb_desc.get_property = s2mu00x_usb_get_property;
battery->psy_usb_desc.properties = s2mu00x_power_props;
battery->psy_usb_desc.num_properties = ARRAY_SIZE(s2mu00x_power_props);
battery->psy_ac_desc.name = "ac";
battery->psy_ac_desc.type = POWER_SUPPLY_TYPE_MAINS;
battery->psy_ac_desc.properties = s2mu00x_power_props;
battery->psy_ac_desc.num_properties = ARRAY_SIZE(s2mu00x_power_props);
battery->psy_ac_desc.get_property = s2mu00x_ac_get_property;
/* Initialize work queue for periodic polling thread */
battery->monitor_wqueue =
create_singlethread_workqueue(dev_name(&pdev->dev));
if (!battery->monitor_wqueue) {
dev_err(battery->dev,
"%s: Fail to Create Workqueue\n", __func__);
goto err_irr;
}
/* Init work & alarm for monitoring */
INIT_DELAYED_WORK(&battery->monitor_work, bat_monitor_work);
alarm_init(&battery->monitor_alarm, ALARM_BOOTTIME, bat_monitor_alarm);
battery->monitor_alarm_interval = DEFAULT_ALARM_INTERVAL;
/* Register power supply to framework */
psy_cfg.drv_data = battery;
psy_cfg.supplied_to = s2mu00x_supplied_to;
psy_cfg.num_supplicants = ARRAY_SIZE(s2mu00x_supplied_to);
battery->psy_battery = power_supply_register(&pdev->dev, &battery->psy_battery_desc, &psy_cfg);
if (IS_ERR(battery->psy_battery)) {
pr_err("%s: Failed to Register psy_battery\n", __func__);
ret = PTR_ERR(battery->psy_battery);
goto err_workqueue;
}
pr_info("%s: Registered battery as power supply\n", __func__);
battery->psy_usb = power_supply_register(&pdev->dev, &battery->psy_usb_desc, &psy_cfg);
if (IS_ERR(battery->psy_usb)) {
pr_err("%s: Failed to Register psy_usb\n", __func__);
ret = PTR_ERR(battery->psy_usb);
goto err_unreg_battery;
}
pr_info("%s: Registered USB as power supply\n", __func__);
battery->psy_ac = power_supply_register(&pdev->dev, &battery->psy_ac_desc, &psy_cfg);
if (IS_ERR(battery->psy_ac)) {
pr_err("%s: Failed to Register psy_ac\n", __func__);
ret = PTR_ERR(battery->psy_ac);
goto err_unreg_usb;
}
pr_info("%s: Registered AC as power supply\n", __func__);
/* Initialize battery level*/
value.intval = 0;
psy = power_supply_get_by_name(battery->pdata->fuelgauge_name);
if (!psy)
return -EINVAL;
ret = power_supply_get_property(psy, POWER_SUPPLY_PROP_CAPACITY, &value);
if (ret < 0)
pr_err("%s: Fail to execute property\n", __func__);
battery->capacity = value.intval;
#if defined(CONFIG_IFCONN_NOTIFIER)
ifconn_notifier_register(&battery->ifconn_nb,
s2mu00x_ifconn_handle_notification,
IFCONN_NOTIFY_BATTERY,
IFCONN_NOTIFY_MUIC);
#elif defined(CONFIG_MUIC_NOTIFIER)
pr_info("%s: Register MUIC notifier\n", __func__);
muic_notifier_register(&battery->batt_nb, s2mu00x_battery_handle_notification,
MUIC_NOTIFY_DEV_CHARGER);
#endif
/* Kick off monitoring thread */
pr_info("%s: start battery monitoring work\n", __func__);
queue_delayed_work(battery->monitor_wqueue, &battery->monitor_work, 5*HZ);
dev_info(battery->dev, "%s: Battery driver is loaded\n", __func__);
return 0;
err_unreg_usb:
power_supply_unregister(battery->psy_usb);
err_unreg_battery:
power_supply_unregister(battery->psy_battery);
err_workqueue:
destroy_workqueue(battery->monitor_wqueue);
err_irr:
wake_lock_destroy(&battery->monitor_wake_lock);
wake_lock_destroy(&battery->vbus_wake_lock);
mutex_destroy(&battery->iolock);
err_parse_dt_nomem:
kfree(battery->pdata);
err_bat_free:
kfree(battery);
return ret;
}
static int s2mu00x_battery_remove(struct platform_device *pdev)
{
return 0;
}
#if defined CONFIG_PM
static int s2mu00x_battery_prepare(struct device *dev)
{
struct s2mu00x_battery_info *battery = dev_get_drvdata(dev);
alarm_cancel(&battery->monitor_alarm);
wake_unlock(&battery->monitor_wake_lock);
/* If charger is connected, monitoring is required*/
if (battery->cable_type != POWER_SUPPLY_TYPE_BATTERY) {
battery->monitor_alarm_interval = SLEEP_ALARM_INTERVAL;
pr_info("%s: Increase battery monitoring interval -> %d\n",
__func__, battery->monitor_alarm_interval);
alarm_start_relative(&battery->monitor_alarm,
ktime_set(battery->monitor_alarm_interval, 0));
}
return 0;
}
static int s2mu00x_battery_suspend(struct device *dev)
{
return 0;
}
static int s2mu00x_battery_resume(struct device *dev)
{
return 0;
}
static void s2mu00x_battery_complete(struct device *dev)
{
struct s2mu00x_battery_info *battery = dev_get_drvdata(dev);
if (battery->monitor_alarm_interval != DEFAULT_ALARM_INTERVAL) {
battery->monitor_alarm_interval = DEFAULT_ALARM_INTERVAL;
pr_info("%s: Recover battery monitoring interval -> %d\n",
__func__, battery->monitor_alarm_interval);
}
alarm_cancel(&battery->monitor_alarm);
wake_lock(&battery->monitor_wake_lock);
queue_delayed_work(battery->monitor_wqueue, &battery->monitor_work, 0);
}
#else
#define s2mu00x_battery_prepare NULL
#define s2mu00x_battery_suspend NULL
#define s2mu00x_battery_resume NULL
#define s2mu00x_battery_complete NULL
#endif
static const struct dev_pm_ops s2mu00x_battery_pm_ops = {
.prepare = s2mu00x_battery_prepare,
.suspend = s2mu00x_battery_suspend,
.resume = s2mu00x_battery_resume,
.complete = s2mu00x_battery_complete,
};
static struct platform_driver s2mu00x_battery_driver = {
.driver = {
.name = "s2mu00x-battery",
.owner = THIS_MODULE,
.pm = &s2mu00x_battery_pm_ops,
.of_match_table = s2mu00x_battery_match_table,
},
.probe = s2mu00x_battery_probe,
.remove = s2mu00x_battery_remove,
};
static int __init s2mu00x_battery_init(void)
{
int ret = 0;
ret = platform_driver_register(&s2mu00x_battery_driver);
return ret;
}
late_initcall(s2mu00x_battery_init);
static void __exit s2mu00x_battery_exit(void)
{
platform_driver_unregister(&s2mu00x_battery_driver);
}
module_exit(s2mu00x_battery_exit);
MODULE_LICENSE("GPL");
MODULE_AUTHOR("Samsung Electronics");
MODULE_DESCRIPTION("Battery driver for S2MU00x");
|