summaryrefslogtreecommitdiff
path: root/drivers/external_drivers/drivers/power/bq25898_charger.c
blob: 78c60378ebf7615b0fd3f5281ea175f793ca95c0 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
1539
1540
1541
1542
1543
1544
1545
1546
1547
1548
1549
1550
1551
1552
1553
1554
1555
1556
1557
1558
1559
1560
1561
1562
1563
1564
1565
1566
1567
1568
1569
1570
1571
1572
1573
1574
1575
1576
1577
1578
1579
1580
1581
1582
1583
1584
1585
1586
1587
1588
1589
1590
1591
1592
1593
1594
1595
1596
1597
1598
1599
1600
1601
1602
1603
1604
1605
1606
1607
1608
1609
1610
1611
1612
1613
1614
1615
1616
1617
1618
1619
1620
1621
1622
1623
1624
1625
1626
1627
1628
1629
1630
1631
1632
1633
1634
1635
1636
1637
1638
1639
1640
1641
1642
1643
1644
1645
1646
1647
1648
1649
1650
1651
1652
1653
1654
1655
1656
1657
1658
1659
1660
1661
1662
1663
1664
1665
1666
1667
1668
1669
1670
1671
1672
1673
1674
1675
1676
1677
1678
1679
1680
1681
1682
1683
1684
1685
1686
1687
1688
1689
1690
1691
1692
1693
1694
1695
1696
1697
1698
1699
1700
1701
1702
1703
1704
1705
1706
1707
1708
1709
1710
1711
1712
1713
1714
1715
1716
1717
1718
1719
1720
1721
1722
1723
1724
1725
1726
1727
1728
1729
1730
1731
1732
1733
1734
1735
1736
1737
1738
1739
1740
1741
1742
1743
1744
1745
1746
1747
1748
1749
1750
1751
1752
1753
1754
1755
1756
1757
1758
1759
1760
1761
1762
1763
1764
1765
1766
1767
1768
1769
1770
1771
1772
1773
1774
1775
1776
1777
1778
1779
1780
1781
1782
1783
1784
1785
1786
1787
1788
1789
1790
1791
1792
1793
1794
1795
1796
1797
1798
1799
1800
1801
1802
1803
1804
1805
1806
1807
1808
1809
1810
1811
1812
1813
1814
1815
1816
1817
1818
1819
1820
1821
1822
1823
1824
1825
1826
1827
1828
1829
1830
1831
1832
1833
1834
1835
1836
1837
1838
1839
1840
1841
1842
1843
1844
1845
1846
1847
1848
1849
1850
1851
1852
1853
1854
1855
1856
1857
1858
1859
1860
1861
1862
1863
1864
1865
1866
1867
1868
1869
1870
1871
1872
1873
1874
1875
1876
1877
1878
1879
1880
1881
1882
1883
1884
1885
1886
1887
1888
1889
1890
1891
1892
1893
1894
1895
1896
1897
1898
1899
1900
1901
1902
1903
1904
1905
1906
1907
1908
1909
1910
1911
1912
1913
1914
1915
1916
1917
1918
1919
1920
1921
1922
1923
1924
1925
1926
1927
1928
1929
1930
1931
1932
1933
1934
1935
1936
1937
1938
1939
1940
1941
1942
1943
1944
1945
1946
1947
1948
1949
1950
1951
1952
1953
1954
1955
1956
1957
1958
1959
1960
1961
1962
1963
1964
1965
1966
1967
1968
1969
1970
1971
1972
1973
1974
1975
1976
1977
1978
1979
1980
1981
1982
1983
1984
1985
1986
1987
1988
1989
1990
1991
1992
1993
1994
1995
1996
1997
1998
1999
2000
2001
2002
2003
2004
2005
2006
2007
2008
2009
2010
2011
2012
2013
2014
2015
2016
2017
2018
2019
2020
2021
2022
2023
2024
2025
2026
2027
2028
2029
2030
2031
2032
2033
2034
2035
2036
2037
2038
2039
2040
2041
2042
2043
2044
2045
2046
2047
2048
2049
2050
2051
2052
2053
2054
2055
2056
2057
2058
2059
2060
2061
2062
2063
2064
2065
2066
2067
2068
2069
2070
2071
2072
2073
2074
2075
2076
2077
2078
2079
2080
2081
2082
2083
2084
2085
2086
2087
2088
2089
2090
2091
2092
2093
2094
2095
2096
2097
2098
2099
2100
2101
2102
2103
2104
2105
2106
2107
2108
2109
2110
2111
2112
2113
2114
2115
2116
2117
2118
2119
2120
2121
2122
2123
2124
2125
2126
2127
2128
2129
2130
2131
2132
2133
2134
2135
2136
2137
2138
2139
2140
2141
2142
2143
2144
2145
2146
2147
2148
2149
2150
2151
2152
2153
2154
2155
2156
2157
2158
2159
2160
2161
2162
2163
2164
2165
2166
2167
2168
2169
2170
2171
2172
2173
2174
2175
2176
2177
2178
2179
2180
2181
2182
2183
2184
2185
2186
2187
2188
2189
2190
2191
2192
2193
2194
2195
2196
2197
2198
2199
2200
2201
2202
2203
2204
2205
2206
2207
2208
2209
2210
2211
2212
2213
2214
2215
2216
2217
2218
2219
2220
2221
2222
2223
2224
2225
2226
2227
2228
2229
2230
2231
2232
2233
2234
2235
2236
2237
2238
2239
2240
2241
2242
2243
2244
2245
2246
2247
2248
2249
2250
2251
2252
2253
2254
2255
2256
2257
2258
2259
2260
2261
2262
2263
2264
2265
2266
2267
2268
2269
2270
2271
2272
2273
2274
2275
2276
2277
2278
2279
2280
2281
2282
2283
2284
2285
2286
2287
2288
2289
2290
2291
2292
2293
2294
2295
2296
2297
2298
2299
2300
2301
2302
2303
2304
2305
2306
2307
2308
2309
2310
2311
2312
2313
2314
2315
2316
2317
2318
2319
2320
2321
2322
2323
2324
2325
2326
2327
2328
2329
2330
2331
2332
2333
2334
2335
2336
2337
2338
2339
2340
2341
2342
2343
2344
2345
2346
2347
2348
2349
2350
2351
2352
2353
2354
2355
2356
2357
2358
2359
2360
2361
2362
2363
2364
2365
2366
2367
2368
2369
2370
2371
2372
2373
2374
2375
2376
2377
2378
2379
2380
2381
2382
2383
2384
2385
2386
2387
2388
2389
2390
2391
2392
2393
2394
2395
2396
2397
2398
2399
2400
2401
2402
2403
2404
2405
2406
2407
2408
2409
2410
2411
2412
2413
2414
2415
2416
2417
2418
2419
2420
2421
2422
2423
2424
2425
2426
2427
2428
2429
2430
2431
2432
2433
2434
2435
2436
2437
2438
2439
2440
2441
2442
2443
2444
2445
2446
2447
2448
2449
2450
2451
2452
2453
2454
2455
2456
2457
2458
2459
2460
2461
2462
2463
2464
2465
2466
2467
2468
2469
2470
2471
2472
2473
2474
2475
2476
2477
2478
2479
2480
2481
2482
2483
2484
2485
2486
2487
2488
2489
2490
2491
2492
2493
2494
2495
2496
2497
2498
2499
2500
2501
2502
2503
2504
2505
2506
2507
2508
2509
2510
2511
2512
2513
2514
2515
2516
2517
2518
2519
2520
2521
2522
2523
2524
2525
2526
2527
2528
2529
2530
2531
2532
2533
2534
2535
2536
2537
2538
2539
2540
2541
2542
2543
2544
2545
2546
2547
2548
2549
2550
2551
2552
2553
2554
2555
2556
2557
2558
2559
2560
2561
2562
2563
2564
2565
2566
2567
2568
2569
2570
2571
2572
2573
2574
2575
2576
2577
2578
2579
2580
2581
2582
2583
2584
2585
2586
2587
2588
2589
2590
2591
2592
2593
2594
2595
2596
2597
2598
2599
2600
2601
2602
2603
2604
2605
2606
2607
2608
2609
2610
2611
2612
2613
2614
2615
2616
2617
2618
2619
2620
2621
2622
2623
2624
2625
2626
2627
2628
2629
2630
2631
2632
2633
2634
2635
2636
2637
2638
2639
2640
2641
2642
2643
2644
2645
2646
2647
2648
2649
2650
2651
2652
2653
2654
2655
2656
2657
2658
2659
2660
2661
2662
2663
2664
2665
2666
2667
2668
2669
2670
2671
2672
2673
2674
2675
2676
2677
2678
2679
2680
2681
2682
2683
2684
2685
2686
2687
2688
2689
2690
2691
2692
2693
2694
2695
2696
2697
2698
2699
2700
2701
2702
2703
2704
2705
2706
2707
2708
2709
2710
2711
2712
2713
2714
2715
2716
2717
2718
2719
2720
2721
2722
2723
2724
2725
2726
2727
2728
2729
2730
2731
2732
2733
2734
2735
2736
2737
2738
2739
2740
2741
2742
2743
2744
2745
2746
2747
2748
2749
2750
2751
2752
2753
2754
2755
2756
2757
2758
2759
2760
2761
2762
2763
2764
2765
2766
2767
2768
2769
2770
2771
2772
2773
2774
2775
2776
2777
2778
2779
2780
2781
2782
2783
2784
2785
2786
2787
2788
2789
2790
2791
2792
2793
2794
2795
2796
2797
2798
2799
2800
2801
2802
2803
2804
2805
2806
2807
2808
2809
2810
2811
2812
2813
2814
2815
2816
2817
2818
2819
2820
2821
2822
2823
2824
2825
2826
2827
2828
2829
2830
2831
2832
2833
2834
2835
2836
2837
2838
2839
2840
2841
2842
2843
2844
2845
2846
2847
2848
2849
2850
2851
2852
2853
2854
2855
2856
2857
2858
2859
2860
2861
2862
2863
2864
2865
2866
2867
2868
2869
2870
2871
2872
2873
2874
2875
2876
2877
2878
2879
2880
2881
2882
2883
2884
2885
2886
2887
2888
2889
2890
2891
2892
2893
2894
2895
2896
2897
2898
2899
2900
2901
2902
2903
2904
2905
2906
2907
2908
2909
2910
2911
2912
2913
2914
2915
2916
2917
2918
2919
2920
2921
2922
2923
2924
2925
2926
2927
2928
2929
2930
2931
2932
2933
2934
2935
2936
2937
2938
2939
2940
2941
2942
2943
2944
2945
2946
2947
2948
2949
2950
2951
2952
2953
2954
2955
2956
2957
2958
2959
2960
2961
2962
2963
2964
2965
2966
2967
2968
2969
2970
2971
2972
2973
2974
2975
2976
2977
2978
2979
2980
2981
2982
2983
2984
2985
2986
2987
2988
2989
2990
2991
2992
2993
2994
2995
2996
2997
2998
2999
3000
3001
3002
3003
3004
3005
3006
3007
3008
3009
3010
3011
3012
3013
3014
3015
3016
3017
3018
3019
3020
3021
3022
3023
3024
3025
3026
3027
3028
3029
3030
3031
3032
3033
3034
3035
3036
3037
3038
3039
3040
3041
3042
3043
3044
3045
3046
3047
3048
3049
3050
3051
3052
3053
3054
3055
3056
3057
3058
3059
3060
3061
3062
3063
3064
3065
3066
3067
3068
3069
3070
3071
3072
3073
3074
3075
3076
3077
3078
3079
3080
3081
3082
3083
3084
3085
3086
3087
3088
3089
3090
3091
3092
3093
3094
3095
3096
3097
3098
3099
3100
3101
3102
3103
3104
3105
3106
3107
3108
3109
3110
3111
3112
3113
/*
 * bq25898_charger.c - BQ25898 Charger I2C client driver
 *
 * Copyright (C) 2016 Intel Corporation
 *
 * ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
 *
 * 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; version 2 of the License.
 *
 * 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 PARTICLAR PURPOSE.	See the GNU
 * General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License along
 * with this program; if not, write to the Free Software Foundation, Inc.,
 * 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA.
 *
 * ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
 *
 */
/*
 *  To enable dev_dbg, add this to the cmdline:
 *	dyndbg = "module bq25898_charger +p"
 *  or at compile time:
 *	#define DEBUG
 */
#include <linux/version.h>
#include <linux/kernel.h>
#include <linux/module.h>
#include <linux/err.h>
#include <linux/init.h>
#include <linux/interrupt.h>
#include <linux/slab.h>
#include <linux/device.h>
#include <linux/i2c.h>
#include <linux/power_supply.h>
#include <linux/pm_runtime.h>
#include <linux/io.h>
#include <linux/sched.h>
#include <linux/delay.h>
#include <linux/usb/otg.h>
#include <linux/power/bq25898_charger.h>
#include <linux/seq_file.h>
#include <linux/debugfs.h>
#include <linux/wakelock.h>
#include <linux/reboot.h>
#include <linux/gpio.h>
#include <linux/time.h>
#include <linux/wakelock.h>

#include <asm/intel_scu_ipc.h>
#include <asm/pmic_pdata.h>

/* for test purpose only */
#define DEBUG_DUMP_REG			0

#define NR_RETRY_CNT			3

#define BUFSIZ_COPY_FROM_USER		20

#define ADC_CONVERSION_TIMEOUT_MS	300
#define TIME_SLEEP_MS			30

#define DEV_NAME			"bq25898_charger"
#define DEV_MANUFACTURER		"TI"
#define MODEL_NAME			"BQ25898"
#define MODEL_NAME_SIZE			8
#define DEV_MANUFACTURER_NAME_SIZE	4

#define BQ25898_POSTCHARGE_DEFAULT_DURATION_MN	30
#define ONE_MINUTE                              (60 * HZ)
#define BQ25898_BAT_MONITOR_DELAY               ONE_MINUTE
#define BQ25898_CURR_CHECK_INTERVAL_DEFAULT     ONE_MINUTE
#define BQ25898_CURR_EOC_LIMIT_DEFAULT          20000  /* uA */

#define BQ25898_I2C_SLAVE_ADDR			0x6B

/*
 * Input source control register (REG00)
 */
#define BQ25898_INPUT_SRC_CTRL_REG		0x00

/* Set Bit7 to enable HIZ , default unset (HIZ disabled) */
#define INPUT_SRC_CNTL_ENABLE_HIZ		(1 << 7)

/*
 * Bits 0 to 5 to set the IINLIM (Input Current Limit)
 * IINLIM
 * Default: b001000 (500mA), offset of 100mA
 */
#define INPUT_SRC_CUR_IINLIM5		(1 << 5)	/* 1600mA */
#define INPUT_SRC_CUR_IINLIM4		(1 << 4)	/* 800mA */
#define INPUT_SRC_CUR_IINLIM3		(1 << 3)	/* 400mA */
#define INPUT_SRC_CUR_IINLIM2		(1 << 2)	/* 200mA */
#define INPUT_SRC_CUR_IINLIM1		(1 << 1)	/* 100mA */
#define INPUT_SRC_CUR_IINLIM0		(1 << 0)	/* 50mA */

/*
 * Input voltage control register (REG01)
 */
#define BQ25898_INPUT_VOLTAGE_CTRL_REG		0x01
/* EN_12V -- 0: disable 12V for maxcharge, 1: enable 12V for maxcharge */
#define INPUT_SRC_VOLTAGE_EN_12V		(1 << 1)
/* VINDPM_OS --  0: 400mA offset, 1: 600mA offset */
#define INPUT_SRC_VOLTAGE_VINDPM0		(1 << 0)

/*
 * ADC converter control register (REG02)
 */
#define BQ25898_ADC_CTRL_REG			0x02
/* CONV_START -- 0: ADC not active, 1: Start ADC conversion */
#define ADC_CONV_START				(1 << 7)
/* CONV_RATE --  0: One short ADC conversion, 1: Start 1s Continuous Conversion */
#define ADC_CONV_RATE				(1 << 6)
/* BOOST_FREQ -- 0: 1.5Mhz 1: 500kHz*/
#define BOOST_FREQ				(1 << 5)
/* ICO_EN -- 0: ICO Disabled, 1: ICO Enabled */
#define ICO_EN					(1 << 4)
/* HVDCP_EN -- 0: HVDCP_EN Disabled, 1: HVDCP_EN Enabled */
#define HVDCP_EN				(1 << 3)
/* MAXC_EN -- 0: MAXC_EN Disabled, 1: MAXC_EN Enabled */
#define MAXC_EN					(1 << 2)
/* FORCE_DPDM -- 0: Not in PSEL detection, 1: Force PSEL detection */
#define ADC_FORCE_DPDM				(1 << 1)
/* AUTO_DPDM_EN -- 0: Disable PSEL detection, 1: Enable PSEL detection */
#define ADC_AUTO_DPDM_ENABLE			(1 << 0)

/*
 * Charge/Timer control register ( REG03)
 */
#define BQ25898_CHARGE_CTRL_REG			0x03
/* VOK_OTG_EN -- 0: VOK_OTG_EN disabled, 1: VOK_OTG_EN enabled) */
#define VOK_OTG_EN				(1 << 7)
/* WD_RST -- 0: Normal, 1: reset (back to 0 after timer reset) */
#define WD_TIMER_RESET				(1 << 6)
/* OTG_CONFIG -- 0: OTG_CONFIG disabled,1: OTG_CONFIG enabled */
#define OTG_CONFIG				(1 << 5)
/* CHG_CONFIG -- 0: Charge disable,1: Charge enable */
#define CHG_CONFIG				(1 << 4)
/* SYS_MIN
 * Default: b101, offset 3.0V
 */
#define SYS_MIN2				(1 << 3)	/* 0.4v */
#define SYS_MIN1				(1 << 2)	/* 0.2v */
#define SYS_MIN0				(1 << 1)	/* 0.1v */

/*
 *  Fast Charge control register ( REG04)
 */
#define BQ25898_FAST_CHARGE_CTRL_REG			0x04
/* EN_PUMPX -- 0: Current pulse control disabled, 1: Current pulse control enabled */
#define EN_PUMPX					(1 << 7)
/* ICHG  Fast Charge current limit
 * Default b000000, 0mv offset
 */
#define FAST_CHARGE_ICHG6			(1 << 6) /* 4096mA */
#define FAST_CHARGE_ICHG5			(1 << 5) /* 2048mA */
#define FAST_CHARGE_ICHG4			(1 << 4) /* 1024mA */
#define FAST_CHARGE_ICHG3			(1 << 3) /* 512mA */
#define FAST_CHARGE_ICHG2			(1 << 2) /* 256mA */
#define FAST_CHARGE_ICHG1			(1 << 1) /* 128mA */
#define FAST_CHARGE_ICHG0			(1 << 0) /* 64mA */

/*
 * Pre charge and Termination current limit (REG05)
 */
#define BQ25898_CUR_LIMIT_CTRL_REG		0x05
/*
 * IPRECHG Pre charge current limit
 * Default: b0001 (128mA), 64mA offset
 */
#define PRECHARGE_CUR_LIMIT3			(1 << 7)	/* 512mA */
#define PRECHARGE_CUR_LIMIT2			(1 << 6)	/* 256mA */
#define PRECHARGE_CUR_LIMIT1			(1 << 5)	/* 128Ma */
#define PRECHARGE_CUR_LIMIT0			(1 << 4)	/* 64mA  */
/*
 * ITERM  Termination current limit
 * Default: b0011 (256mA), 64mA offset
 */
#define TERM_CUR_LIMIT3				(1 << 3)	/* 512mA */
#define TERM_CUR_LIMIT2				(1 << 2)	/* 256mA */
#define TERM_CUR_LIMIT1				(1 << 1)	/* 128mA */
#define TERM_CUR_LIMIT0				(1 << 0)	/* 64 mA */

/*
 * Charge voltage limit register
 * Bat precharge/recharge threshold reg (REG06)
 */
#define BQ25898_VLIM_CHRG_CTRL_REG		0x06
/*
 * VREG Charge Voltage Limit
 * Default: b010111, 3.840V offset, default: 4.208V
 */
#define CHARGE_VOLT_LIMIT5			(1 << 7)	/* 512mV */
#define CHARGE_VOLT_LIMIT4			(1 << 6)	/* 256mV */
#define CHARGE_VOLT_LIMIT3			(1 << 5)	/* 128mV */
#define CHARGE_VOLT_LIMIT2			(1 << 4)	/* 64mV  */
#define CHARGE_VOLT_LIMIT1			(1 << 3)	/* 32mV  */
#define CHARGE_VOLT_LIMIT0			(1 << 2)	/* 16mV  */

/* Battery Precharge to Fast Charge Threshold -- 0: 2.8V, 1: 3.0V, default 3.0V */
#define BAT_PRE2FAST_CHARGE_THRESHOLD		(1 << 1)

/* Battery Recharge Threshold -- 0: 100mV, 1: 200mV, default: 100mV */
#define BAT_RECHARGE_THRESHOLD			(1 << 0)

/*
 * Charge termination, termination indicator
 * I2C Watchdog Timer register
 * Safety Timer  (REG07)
 */
#define BQ25898_TERM_WDT_SFTY_CTRL_REG		0x07
/* */
#define CHARGE_TERM_ENABLE			(1 << 7)	/* 0: disable, 1: enable */
#define STAT_PIN_ENABLE				(1 << 6)	/* 0: enable STAT pin, 1: disable STAT pin */
/* WATCHDOG */
#define I2C_WDT_TIMER_SETTINGS1			(1 << 5)	/* b00 Disable Timer, b01:40s, b10:80s*/
#define I2C_WDT_TIMER_SETTINGS0			(1 << 4)	/* b11: 160s, default b01 */
/* EN_TIMER Enable safety timer*/
#define CHARGING_SAFETY_TIMER_ENABLE		(1 << 3)	/* 0: disable, 1: enable */
/* CHG_TIMER Fast charge safety timer*/
#define CHARGING_SAFETY_TIMER_SETTING1		(1 << 2)	/* b00: 5hrs, b01:8Hrs, b10: 12Hrs */
#define CHARGING_SAFETY_TIMER_SETTING0		(1 << 1)	/* b11: 20Hrs, default b10 */
/* JEITA_ISET low temp setting */
#define JEITA_ISET				(1 << 0)	/* 0: 50%, 1: 20% */

/*
 * Thermal regulation threshold register (REG08)
 */
#define BQ25898_THERM_REGULATION_CTRL_REG	0x08
/* BAT_COMP */
#define BAT_COMP2				(1 << 7)	/* 80m Ohm */
#define BAT_COMP1				(1 << 6)	/* 40m Ohm */
#define BAT_COMP0				(1 << 5)	/* 20m Ohm */
/* VCLAMP */
#define VCLAMP2					(1 << 4)	/* 128mV */
#define VCLAMP1					(1 << 3)	/* 64mV */
#define VCLAMP0					(1 << 2)	/* 32mV */
/* TREG */
#define THERMAL_REGUL_THRESOLD1			(1 << 1)	/* b00: 60C, b01:80C, b10: 100C */
#define THERMAL_REGUL_THRESOLD0			(1 << 0)	/* b11: 120C, default b11 */

/*
 *  safety timer control register (REG09)
 */
#define BQ25898_SAFETY_TIMER_CTRL_REG		0x09
/* FORCE_ICO -- 0: do not force input current optimizer , 1: force input current optimizer*/
#define FORCE_ICO				(1 << 7)
/* TMR2X_EN -- 0: Safety timer not slowed by 2X, 1: slowed by 2X*/
#define SAFETY_TIMER_SLOW_ENABLE		(1 << 6)
/* BATFET_Disable -- 0: Allow Q4 turn on, 1: turn off Q4*/
#define BATFET_DISABLE				(1 << 5)
/* JEITA_VSET -- 0: set charge voltage to VREG-200mv, 1: set charge voltage to VREG*/
#define JEITA_VSET				(1 << 4)
/* BATFET_DLY -- 0: Turn off BATFET immediatly when BATFET_DISABLE is set,
 1: Turn off BATFET after Tbatfet_dly (10s) when BATFET_DISABLE is set */
#define BATFET_DLY				(1 << 3)

/* BATFET_RST_EN -- 0: disable BATFET reset function, 1: enable BATFET reset function */
#define BATFET_RST_EN				(1 << 2)
/* PUMPX_UP */
#define PUMPX_UP				(1 << 1)	/* 0: , 1: */
/* PUMPX_DN */
#define PUMPX_DN				(1 << 0)	/* 0: , 1: */

/*
 * Boost register (REG0A)
 */
#define BQ25898_BOOST_CTRL_REG			0x0A
/* BOOSTV offset: 4.55V , default b0111 4.998V*/
#define BOOSTV3					(1 << 7)	/* 512mV */
#define BOOSTV2					(1 << 6)	/* 256mV */
#define BOOSTV1					(1 << 5)	/* 128mV */
#define BOOSTV0					(1 << 4)	/* 64mV	 */
/* PFM_OTG_DIS */
#define PFM_OTG_DIS				(1 << 3)	/* 0: enable , 1: disable */
/* BOOST_LIM */
#define BOOST_LIM2				(1 << 2)	/* b000: 0.5A b001: 0.8A b010: 1.0A   */
#define BOOST_LIM1				(1 << 1)	/* b011: 1.2A b100: 1.5A b101 1.8A  */
#define BOOST_LIM0				(1 << 0)	/* b110: 2.1A b111: 2.4A  */

/*
 *  Status register (REG0B)
 */
#define BQ25898_STATUS_REG			0x0B
/* VBUS_STAT */
#define VBUS_STATUS2				(1 << 7)	/* b000: No input, b001: USB Host SDP */
#define VBUS_STATUS1				(1 << 6)	/* b010: Adapter (3.25A), b111: NA */
#define VBUS_STATUS0				(1 << 5)
#define VBUS_STATUS_MASK			(7 << 5)
/* CHRG_STAT */
#define CHARGER_STATUS1				(1 << 4)	/* b00: Not charging, b01: Precharge */
#define CHARGER_STATUS0				(1 << 3)	/* b10: Fast charge, b11: Charge termination done */
#define CHARGER_STATUS_MASK			(3 << 3)
/* PG_STAT -- 0: Not power good, 1: Power good */
#define PG_STAT					(1 << 2)
#define PG_STAT_MASK			(1 << 2)
/* VSYS_STAT -- 0: Not in VSYSmin, 1: in Vsys min regulation (BAT < VSYSMIN) */
#define VSYS_STAT				(1 << 0)
#define BQ25898_STATUS_REG_DEFAULT		0x02

/*
 *  Fault Register (REG0C)
 */
#define BQ25898_FAULT_REG			0x0C

#define NTC_FAULT_MASK			(0x07)
#define BAT_FAULT_MASK			(0x08)
#define CHARGER_FAULT_MASK		(0x30)
#define BOOST_FAULT_MASK		(0x40)
#define WATCHDOG_FAULT_MASK		(0x80)

#define CHARGER_FAULT_OVERHEAT            0x01
#define CHARGER_FAULT_SAFETY_TIMER_EXPIRE 0x03
#define NTC_FAULT_COLD                    0x05
#define NTC_FAULT_OVERHEAT                0x06

const char * const NTC_fault_to_human[] = {
		[0x0] = "Normal\n",
		[0x2] = "TS Warm (Buck mode)\n",
		[0x3] = "TS Cool (Buck mode)\n",
		[0x5] = "TS Cold\n",
		[0x6] = "TS Hot\n"
};

const char * const CHARGER_fault_to_human[] = {
	[0x0] = "Normal\n",
	[0x1] = "Input fault\n",
	[0x2] = "Thermal shutdown\n",
	[0x3] = "Charge safety timer expired\n"
};

enum {
	BAT_FAULT_OFF = 0,
	BOOST_FAULT_OFF,
	WATCHDOG_FAULT_OFF,
	NTC_FAULT_OFF,
	CHARGER_FAULT_OFF = NTC_FAULT_OFF + ARRAY_SIZE(NTC_fault_to_human),
	FAULT_OFF_SIZE = CHARGER_FAULT_OFF + ARRAY_SIZE(CHARGER_fault_to_human)
};

u16 fault_count[FAULT_OFF_SIZE];

/*
 * Voltage IN control register (REG0D),
 * all bits will be reset when adaptor plug in
 * default: b00010010
 */
#define BQ25898_VIN_CTRL_REG			0x0D
/* FORCE_VINDPM -- 0: run VINDPM threshold algo, 1: force VINDMP; bypass algo */
#define FORCE_VINDPM				(1 << 7)
/* VINDPM
 * Absolute VINDPM threshold offset: 2.6V, default b0010010; 4.4V
 */
#define VINDPM6					(1 << 6)	/* 6400mV */
#define VINDPM5					(1 << 5)	/* 3200mV */
#define VINDPM4					(1 << 4)	/* 1600mV */
#define VINDPM3					(1 << 3)	/* 800mV */
#define VINDPM2					(1 << 2)	/* 400mV */
#define VINDPM1					(1 << 1)	/* 200mV */
#define VINDPM0					(1 << 0)	/* 100mV */

/*
 * Battery voltage register	(REG0E), readonly
 */
#define BQ25898_BAT_VOLT_REG			0x0E
/* THERM_STAT -- 0: normal, 1:in thermal regulation */
#define THERM_STAT				(1 << 7)
/* BATV - Adc conversion Bat voltage
 * Offset 2.304V, default b000000*/
#define ADC_CONV_BATV6				(1 << 6)	/* 1280mV */
#define ADC_CONV_BATV5				(1 << 5)	/* 640mV */
#define ADC_CONV_BATV4				(1 << 4)	/* 320mV */
#define ADC_CONV_BATV3				(1 << 3)	/* 160mV */
#define ADC_CONV_BATV2				(1 << 2)	/* 80mV */
#define ADC_CONV_BATV1				(1 << 1)	/* 40mV */
#define ADC_CONV_BATV0				(1 << 0)	/* 20mV */

/*
 * System voltage register	(REG0F), readonly
 */
#define BQ25898_SYS_VOLTAGE_REG			0x0F
/* SYSV
 * ADC Conversion of system Voltage
 * Default: b0000000, offsert 2.304V
 */
#define ADC_CONV_SYSV6				(1 << 6)	/* 1280mV */
#define ADC_CONV_SYSV5				(1 << 5)	/* 640mV */
#define ADC_CONV_SYSV4				(1 << 4)	/* 320mV */
#define ADC_CONV_SYSV3				(1 << 3)	/* 160mV */
#define ADC_CONV_SYSV2				(1 << 2)	/* 80mV */
#define ADC_CONV_SYSV1				(1 << 1)	/* 40mV */
#define ADC_CONV_SYSV0				(1 << 0)	/* 20mV */

/*
 * ADC Conversion of TS Voltage (REG10), readonly
 * TSPCPT
 */
#define BQ25898_ADC_TSPCT_REG			0x10
/* TSPCT  Adc Conversion of TS voltage
 * offset 21%, default b0000000
 */
#define TSPCT6					(1 << 6)	/* 29.76% */
#define TSPCT5					(1 << 5)	/* 14.88% */
#define TSPCT4					(1 << 4)	/* 7.44% */
#define TSPCT3					(1 << 3)	/* 3.72% */
#define TSPCT2					(1 << 2)	/* 1.86% */
#define TSPCT1					(1 << 1)	/* 0.93% */
#define TSPCT0					(1 << 0)	/* 0.465% */

/*
 * VBUS ADC conversion register (REG11), readonly
 */
#define BQ25898_VBUS_ADC_REG			0x11
/* VBUS_GD */
#define VBUS_ATTACHED				(1 << 7)	/* 0: No VBUS attached, 1: VBUS attached */
/* VBUSV  Adc Conversion of VBUS voltage
 * Default b0000000, Offset 2.6V
 */
#define VBUSV_ADC_VOLT6				(1 << 6)	/* 6400mV */
#define VBUSV_ADC_VOLT5				(1 << 5)	/* 3200mV */
#define VBUSV_ADC_VOLT4				(1 << 4)	/* 1600mV */
#define VBUSV_ADC_VOLT3				(1 << 3)	/* 800mV */
#define VBUSV_ADC_VOLT2				(1 << 2)	/* 400mV */
#define VBUSV_ADC_VOLT1				(1 << 1)	/* 200mV */
#define VBUSV_ADC_VOLT0				(1 << 0)	/* 100mV */

/*
 *  ADC Charge Current conversion register (REG12), readonly
 */
#define BQ25898_CC_ADC_REG			0x12
/* ICHGR  Adc Conversion of charge current
 * Default b0000000, Offset 0mA
 */
#define ADC_CHARGE_CURRENT6			(1 << 6)	/* 3200mA */
#define ADC_CHARGE_CURRENT5			(1 << 5)	/* 1600mA */
#define ADC_CHARGE_CURRENT4			(1 << 4)	/* 800mA */
#define ADC_CHARGE_CURRENT3			(1 << 3)	/* 400mA */
#define ADC_CHARGE_CURRENT2			(1 << 2)	/* 200mA */
#define ADC_CHARGE_CURRENT1			(1 << 1)	/* 100mA */
#define ADC_CHARGE_CURRENT0			(1 << 0)	/* 50mA */

/*
 *  VINDPM Current Limit register (REG13), readonly
 */
#define BQ25898_VINDPM_LIM_REG			0x13
/* VDPM_STAT */
#define VDPM_STAT				(1 << 7)	/* 0: not in VINDPM, 1: VINDPM */
/* IDPM_STAT */
#define IDPM_STAT				(1 << 6)	/* 0: not in IINDPM, 1: IINDPM */
/* IDMP_LIM  Input current DPM limit
 * Default b0000000, Offset 100mA
 */
#define IDPM_CURRENT_LIM5			(1 << 5)	/* 1600mA */
#define IDPM_CURRENT_LIM4			(1 << 4)	/* 800mA */
#define IDPM_CURRENT_LIM3			(1 << 3)	/* 400mA */
#define IDPM_CURRENT_LIM2			(1 << 2)	/* 200mA */
#define IDPM_CURRENT_LIM1			(1 << 1)	/* 100mA */
#define IDPM_CURRENT_LIM0			(1 << 0)	/* 50mA */

/*
 * Device Register control register (REG14)
 */
#define BQ25898_DEVREG_CTRL_REG			0x14
/* REG_RST Register reset -- 0: keep current register setting, 1: reset to default */
#define REG_RST					(1 << 7)
/* ICO_OPTIMIZED -- 0: optimization is in progress, 1: Maximum input current detected */
#define ICO_OPTIMIZED				(1 << 6)
/* PN Device configuration */
#define PN2					(1 << 5)
#define PN1					(1 << 4)
#define PN0					(1 << 3)
/* DEV_REV default:b01 */
#define DEV_REV1				(1 << 1)
#define DEV_REV0				(1 << 0)

#define BQ25898_FIRST_REGISTER			0x00
#define BQ25898_LAST_REGISTER			0x14

#define BQ25898_ALL_REGISTER			0x15

#define BQ25898_INT_COUNTER "bq25898_irq_counter"

#define DEFAULT_SHIP_MODE_DELAY_10S     1
#define MAX_POSTCHARGE_DURATION         120    /* max postcharge duration in minutes : 120 => 2 hours */
#define MAX_CURR_EOC_LIMIT              64000
#define MAX_CURR_CHECK_INTERVAL         120

static bool ship_mode_delay_10s = DEFAULT_SHIP_MODE_DELAY_10S;

enum bq25898_wdt_timing {
	BQ25898_WDT_TIMER_DISABLE,
	BQ25898_WDT_TIMER_40S,
	BQ25898_WDT_TIMER_80S,
	BQ25898_WDT_TIMER_160S
};

struct bq25898_debugfs_file {
	struct bq25898_charger *parent;
	u8 regnr;
};

enum bq25898_wdt_state {
	WDT_UNKNOWN,
	WDT_ENABLED,
	WDT_DISABLED
};

struct bq25898_charger {
	struct mutex stat_lock;
	struct mutex sysfs_lock;
	struct mutex ship_mode_lock;
	struct mutex charge_config_lock;
	struct i2c_client *client;
	struct bq25898_plat_data *pdata;
	struct power_supply psy_usb;
	struct delayed_work sw_term_work;
	struct delayed_work wdt_work;
	struct delayed_work batmon_work;
	struct work_struct sw_config_work;
	struct work_struct charge_status_work;
	struct notifier_block otg_usb_change;
	struct notifier_block reboot_notifier;
	struct notifier_block pmic_notifier;
	struct usb_phy *transceiver;
	struct dentry *dbgfs_dir;
	struct bq25898_debugfs_file dbgfs_file[BQ25898_ALL_REGISTER+1];
	int revision;
	char model_name[MODEL_NAME_SIZE];
	char manufacturer[DEV_MANUFACTURER_NAME_SIZE];
	int current_now;		/* provided by healthd */
	unsigned int postcharge_duration_mn;	/* duration in mn after charge termination interrupt */
	u32 irq_counter;
	bool ship_mode_scheduled;
	bool is_charge_complete;	/* charge stopped after termination done */
	enum bq25898_wdt_state watchdog_state;
	int irq;
	unsigned long postcharge_start_time_sec;
	unsigned int curr_check_interval;
	unsigned int curr_eoc_limit;
	int status_reg_oldvalue;
	struct wake_lock charger_wlock; /* wakelock */
};

static int bq25898_charger_configure(struct i2c_client *client);
static irqreturn_t bq25898_handler(int irq, void *data);
static irqreturn_t bq25898_thread_handler(int id, void *data);
static int bq25898_force_charging(struct bq25898_charger *chip);

static enum power_supply_property bq25898_battery_properties[] = {
	POWER_SUPPLY_PROP_STATUS,		/* charging status */
	POWER_SUPPLY_PROP_HEALTH,		/* battery health */
	POWER_SUPPLY_PROP_PRESENT,		/* battery present */
	POWER_SUPPLY_PROP_ONLINE,		/* power supply online */
	POWER_SUPPLY_PROP_TEMP,			/* battery temperature */
	POWER_SUPPLY_PROP_TECHNOLOGY,		/* battery technology */
	POWER_SUPPLY_PROP_CURRENT_NOW,		/* battery current */
	POWER_SUPPLY_PROP_VOLTAGE_NOW		/* battery voltage */
};

static int bq25898_usb_change_notifier(struct notifier_block *self, unsigned long action,
				void *param)
{
	struct power_supply_cable_props *caps = param;
	struct bq25898_charger *chip = container_of(self, struct bq25898_charger,
						otg_usb_change);

	if (!chip || !caps)
		return NOTIFY_DONE;

	switch (action) {
	case USB_EVENT_CHARGER:

		dev_info(&chip->client->dev, "%s cable (type: %s)\n",
			((caps->chrg_evt == POWER_SUPPLY_CHARGER_EVENT_CONNECT) ? "Connect" :
			((caps->chrg_evt == POWER_SUPPLY_CHARGER_EVENT_DISCONNECT) ? "Disconnect" : "Other charger event")),
			(caps->chrg_type == POWER_SUPPLY_CHARGER_TYPE_USB_SDP) ? "SDP" :
			((caps->chrg_type == POWER_SUPPLY_CHARGER_TYPE_USB_DCP) ? "DCP" :
			((caps->chrg_type == POWER_SUPPLY_CHARGER_TYPE_USB_CDP) ? "CDP" : "Other")));

		switch (caps->chrg_evt) {
		case POWER_SUPPLY_CHARGER_EVENT_CONNECT:
			/* schedule battery monitoring */
			schedule_delayed_work(&chip->batmon_work, BQ25898_BAT_MONITOR_DELAY);

			/* wakelock : lock (suspend disabled), when not already locked, and on DCP (wall charger) detection */
			if (!wake_lock_active(&(chip->charger_wlock)) && caps->chrg_type == POWER_SUPPLY_CHARGER_TYPE_USB_DCP) {
				dev_info(&chip->client->dev, "locking wakelock\n");
				wake_lock(&(chip->charger_wlock));
			}
			break;
		case POWER_SUPPLY_CHARGER_EVENT_DISCONNECT:
			/* cancel battery monitor */
			cancel_delayed_work(&chip->batmon_work);
			/* ensure charge termination is enabled and if needed watchdog disabled */
			cancel_delayed_work(&chip->sw_term_work);
			queue_work(system_nrt_wq, &chip->sw_config_work);

			/* wakelock : unlock (suspend allowed) */
			if (wake_lock_active(&(chip->charger_wlock))) {
				dev_info(&chip->client->dev, "unlocking wakelock\n");
				wake_unlock(&(chip->charger_wlock));
			}
			break;
		default:
			break;
		}
		break;
	default:
		break;
	}
	return NOTIFY_OK;
}


static int bq25898_read_reg(struct i2c_client *client, u8 reg)
{
	int ret, i;

	for (i = 0; i < NR_RETRY_CNT; i++) {
		ret = pm_runtime_get_sync(&client->dev);
		if (ret < 0) {
			dev_info(&client->dev, "pm_runtime_get_sync failed:%d",ret);
			continue;
		}
		ret = i2c_smbus_read_byte_data(client, reg);
		pm_runtime_put_sync(&client->dev);
		if (ret < 0)
			continue;
		else
			break;
	}

	if (ret < 0)
		dev_err(&client->dev, "error in reading reg %d:%d\n", reg, ret);

	return ret;
}

static int bq25898_write_reg(struct i2c_client *client, u8 reg, u8 data)
{
	int ret, i;

	for (i = 0; i < NR_RETRY_CNT; i++) {
		ret = pm_runtime_get_sync(&client->dev);
		if (ret < 0) {
			dev_info(&client->dev, "pm_runtime_get_sync failed:%d\n",ret);
			continue;
		}
		ret = i2c_smbus_write_byte_data(client, reg, data);
		pm_runtime_put_sync(&client->dev);
		if (ret < 0)
			continue;
		else
			break;
	}

	if (ret < 0)
		dev_err(&client->dev, "error in writing %d to reg %d:%d\n",
			data, reg, ret);

	return ret;
}

static int bq25898_read_modify_reg(struct i2c_client *client, u8 reg,
					  u8 mask, u8 val)
{
	int ret;

	ret = bq25898_read_reg(client, reg);
	if (ret < 0)
		return ret;

	dev_dbg(&client->dev, "bq read mod reg: 0x%02x\n", ret);

	ret = (ret & ~mask)|(mask & val);

	dev_dbg(&client->dev, "bq read mod new reg: 0x%02x\n", ret);

	return bq25898_write_reg(client, reg, ret);
}

#if DEBUG_DUMP_REG
static void bq25898_dump_regs(struct i2c_client *client)
{
	int i;
	int ret;
	struct bq25898_charger *chip;
	char buf[160] = {0};
	int used = 0;

	if (!client)
		return;

	chip = i2c_get_clientdata(client);

	dev_info(&client->dev, "BQ25898 Register dump: ");

	for (i = BQ25898_FIRST_REGISTER; i <= BQ25898_LAST_REGISTER; ++i) {
		ret = bq25898_read_reg(client, i);
		if (ret < 0)
			dev_err(&client->dev,
				"Error in reading REG %X:%d\n", i, ret);
		else
			used += snprintf(buf + used, sizeof(buf) - used,
					"%02X:0x%X ", i, ret);
	}
	dev_info(&client->dev, "%s\n", buf);

}
#endif

#ifdef CONFIG_DEBUG_FS
static void bq25898_reg0_to_human(struct seq_file *seq, u8 reg, u8 val)
{
	int res = 0;

	if (val & INPUT_SRC_CNTL_ENABLE_HIZ)
		seq_puts(seq, "HIZ enabled\n");
	else
		seq_puts(seq, "HIZ disabled\n");

	res = 100;
	if (val & INPUT_SRC_CUR_IINLIM5)
		res += 1600;
	if (val & INPUT_SRC_CUR_IINLIM4)
		res += 800;
	if (val & INPUT_SRC_CUR_IINLIM3)
		res += 400;
	if (val & INPUT_SRC_CUR_IINLIM2)
		res += 200;
	if (val & INPUT_SRC_CUR_IINLIM1)
		res += 100;
	if (val & INPUT_SRC_CUR_IINLIM0)
		res += 50;

	seq_printf(seq, "IINLIM %dmA\n", res);
}

static void bq25898_reg1_to_human(struct seq_file *seq, u8 reg, u8 val)
{

	if (val & INPUT_SRC_VOLTAGE_EN_12V)
		seq_puts(seq, "12V enabled for maxcharge\n");
	else
		seq_puts(seq, "12V disabled for maxcharge\n");

	if (val & INPUT_SRC_VOLTAGE_VINDPM0)
		seq_puts(seq, "VINDPM 600mA offset\n");
	else
		seq_puts(seq, "VINDPM 400mA offset\n");

}

static void bq25898_reg2_to_human(struct seq_file *seq, u8 reg, u8 val)
{

	if (val & ADC_CONV_START)
		seq_puts(seq, "Start ADC conversion\n");
	else
		seq_puts(seq, "ADC not active\n");

	if (val & ADC_CONV_RATE)
		seq_puts(seq, "Start 1s Continuous conversion\n");
	else
		seq_puts(seq, "One shot ADC conversion\n");

	if (val & BOOST_FREQ)
		seq_puts(seq, "BOOST_FREQ 500kHz\n");
	else
		seq_puts(seq, "BOOST_FREQ 1.5Mhz\n");

	if (val & ICO_EN)
		seq_puts(seq, "ICO_EN enabled\n");
	else
		seq_puts(seq, "ICO_EN disabled\n");

	if (val & HVDCP_EN)
		seq_puts(seq, "HVDCP_EN enabled\n");
	else
		seq_puts(seq, "HVDCP_EN disabled\n");

	if (val & MAXC_EN)
		seq_puts(seq, "MAXC_EN enabled\n");
	else
		seq_puts(seq, "MAXC_EN disabled\n");

	if (val & ADC_FORCE_DPDM)
		seq_puts(seq, "Force D+/D- detection\n");
	else
		seq_puts(seq, "Not in D+/D- or PSEL detection\n");

	if (val & ADC_AUTO_DPDM_ENABLE)
		seq_puts(seq, "Enable auto D+/D- or PSEL detection\n");
	else
		seq_puts(seq, "Disable auto D+/D- or PSEL detection\n");
}

static void bq25898_reg3_to_human(struct seq_file *seq, u8 reg, u8 val)
{
	int res = 0;

	if (val & VOK_OTG_EN)
		seq_puts(seq, "VOK_OTG_EN enabled\n");
	else
		seq_puts(seq, "VOK_OTG_EN disabled\n");

	if (val & WD_TIMER_RESET)
		seq_puts(seq, "WD_RST Reset\n");
	else
		seq_puts(seq, "WD_RST Normal\n");

	if (val & OTG_CONFIG)
		seq_puts(seq, "OTG_CONFIG enabled\n");
	else
		seq_puts(seq, "OTG_CONFIG disabled\n");

	if (val & CHG_CONFIG)
		seq_puts(seq, "CHG_CONFIG enabled\n");
	else
		seq_puts(seq, "CHG_CONFIG disabled\n");

	res = 3000;
	if (val & SYS_MIN2)
		res += 400;
	if (val & SYS_MIN1)
		res += 200;
	if (val & SYS_MIN0)
		res += 100;

	seq_printf(seq, "SYS_MIN %dmV\n", res);
}

static void bq25898_reg4_to_human(struct seq_file *seq, u8 reg, u8 val)
{
	int res = 0;

	if (val & EN_PUMPX)
		seq_puts(seq, "EN_PUMPX current pulse control enabled\n");
	else
		seq_puts(seq, "EN_PUMPX current pulse control disabled\n");

	if (val & FAST_CHARGE_ICHG6)
		res += 4096;
	if (val & FAST_CHARGE_ICHG5)
		res += 2048;
	if (val & FAST_CHARGE_ICHG4)
		res += 1024;
	if (val & FAST_CHARGE_ICHG3)
		res += 512;
	if (val & FAST_CHARGE_ICHG2)
		res += 256;
	if (val & FAST_CHARGE_ICHG1)
		res += 128;
	if (val & FAST_CHARGE_ICHG0)
		res += 64;

	seq_printf(seq, "ICHG Fast charge %dmA\n", res);
}

static void bq25898_reg5_to_human(struct seq_file *seq, u8 reg, u8 val)
{
	int res = 64;

	if (val & PRECHARGE_CUR_LIMIT3)
		res += 512;
	if (val & PRECHARGE_CUR_LIMIT2)
		res += 256;
	if (val & PRECHARGE_CUR_LIMIT1)
		res += 128;
	if (val & PRECHARGE_CUR_LIMIT0)
		res += 64;

	seq_printf(seq, "IPRECHG %dmA\n", res);

	res = 64;
	if (val & TERM_CUR_LIMIT3)
		res += 512;
	if (val & TERM_CUR_LIMIT2)
		res += 256;
	if (val & TERM_CUR_LIMIT1)
		res += 128;
	if (val & TERM_CUR_LIMIT0)
		res += 64;

	seq_printf(seq, "ITERM %dmA\n", res);
}

static void bq25898_reg6_to_human(struct seq_file *seq, u8 reg, u8 val)
{
	int res = 3840;

	if (val & CHARGE_VOLT_LIMIT5)
		res += 512;
	if (val & CHARGE_VOLT_LIMIT4)
		res += 256;
	if (val & CHARGE_VOLT_LIMIT3)
		res += 128;
	if (val & CHARGE_VOLT_LIMIT2)
		res += 64;
	if (val & CHARGE_VOLT_LIMIT1)
		res += 32;
	if (val & CHARGE_VOLT_LIMIT0)
		res += 16;

	seq_printf(seq, "Charge Voltage Limit %dmV\n", res);

	if (val & BAT_PRE2FAST_CHARGE_THRESHOLD)
		seq_puts(seq, "Precharge2Fast threshold 3.0V\n");
	else
		seq_puts(seq, "Precharge2Fast threshold 2.8V\n");

	if (val & BAT_RECHARGE_THRESHOLD)
		seq_printf(seq, "Recharge threshold VREG-200mV (%dmV)\n", res-200);
	else
		seq_printf(seq, "Recharge threshold VREG-100mV (%dmV)\n", res-100);
}

static void bq25898_reg7_to_human(struct seq_file *seq, u8 reg, u8 val)
{

	if (val & CHARGE_TERM_ENABLE)
		seq_puts(seq, "Charge Term enabled\n");
	else
		seq_puts(seq, "Charge Term disabled\n");

	if (!(val & I2C_WDT_TIMER_SETTINGS1) && !(val & I2C_WDT_TIMER_SETTINGS0))
		seq_puts(seq, "WD timer disabled\n");
	else if (!(val & I2C_WDT_TIMER_SETTINGS1) && (val & I2C_WDT_TIMER_SETTINGS0))
		seq_puts(seq, "WD timer 40s\n");
	else if ((val & I2C_WDT_TIMER_SETTINGS1) && !(val & I2C_WDT_TIMER_SETTINGS0))
		seq_puts(seq, "WD timer 80s\n");
	else if ((val & I2C_WDT_TIMER_SETTINGS1) && (val & I2C_WDT_TIMER_SETTINGS0))
		seq_puts(seq, "WD timer 160s\n");

	if (val & CHARGING_SAFETY_TIMER_ENABLE)
		seq_puts(seq, "Charging safety timer enabled\n");
	else
		seq_puts(seq, "Charging safety timer disabled\n");

	if (!(val & CHARGING_SAFETY_TIMER_SETTING1) && !(val & CHARGING_SAFETY_TIMER_SETTING0))
		seq_puts(seq, "Charging safety timer 5hrs\n");
	else if (!(val & CHARGING_SAFETY_TIMER_SETTING1) && (val & CHARGING_SAFETY_TIMER_SETTING0))
		seq_puts(seq, "Charging safety timer 8hrs\n");
	else if ((val & CHARGING_SAFETY_TIMER_SETTING1) && !(val & CHARGING_SAFETY_TIMER_SETTING0))
		seq_puts(seq, "Charging safety timer 12hrs\n");
	else if ((val & CHARGING_SAFETY_TIMER_SETTING1) && (val & CHARGING_SAFETY_TIMER_SETTING0))
		seq_puts(seq, "Charging safety timer 20hrs\n");

	if (val & JEITA_ISET)
		seq_printf(seq, "JEITA_ISET low temperature set to 20%% of ICHG\n");
	else
		seq_printf(seq, "JEITA_ISET low temperature set to 50%% of ICHG\n");

}

static void bq25898_reg8_to_human(struct seq_file *seq, u8 reg, u8 val)
{

	int res = 0;

	if (val & BAT_COMP2)
		res += 80;
	if (val & BAT_COMP1)
		res += 40;
	if (val & BAT_COMP0)
		res += 20;

	seq_printf(seq, "BAT_COMP IR compensation %d Ohm\n", res);

	res = 0;

	if (val & VCLAMP2)
		res += 128;
	if (val & VCLAMP1)
		res += 64;
	if (val & VCLAMP0)
		res += 32;

	seq_printf(seq, "VCLAMP IRcompensation voltage clamp %dmV\n", res);

	if (!(val & THERMAL_REGUL_THRESOLD1) && !(val & THERMAL_REGUL_THRESOLD0))
		seq_puts(seq, "Thermal threshold 60C\n");
	else if (!(val & THERMAL_REGUL_THRESOLD1) && (val & THERMAL_REGUL_THRESOLD0))
		seq_puts(seq, "Thermal threshold 80C\n");
	else if ((val & THERMAL_REGUL_THRESOLD1) && !(val & THERMAL_REGUL_THRESOLD0))
		seq_puts(seq, "Thermal threshold 100C\n");
	else if ((val & THERMAL_REGUL_THRESOLD1) && (val & THERMAL_REGUL_THRESOLD0))
		seq_puts(seq, "Thermal threshold 120C\n");
}

static void bq25898_reg9_to_human(struct seq_file *seq, u8 reg, u8 val)
{

	if (val & FORCE_ICO)
		seq_puts(seq, "FORCE_ICO input current optimizer forced\n");
	else
		seq_puts(seq, "FORCE_ICO input current optimizer NOT forced\n");

	if (val & SAFETY_TIMER_SLOW_ENABLE)
		seq_puts(seq, "Safety timer slowed x2\n");
	else
		seq_puts(seq, "Safety timer normal\n");

	if (val & BATFET_DISABLE)
		seq_puts(seq, "BATFET_DISABLE Q4 turned off\n");
	else
		seq_puts(seq, "BATFET_DISABLE Q4 turned on\n");

	if (val & JEITA_VSET)
		seq_puts(seq, "JEITA_VSET charge voltage set to VREG\n");
	else
		seq_puts(seq, "JEITA_VSET charge voltage set to VREG-200mV\n");

	if (val & BATFET_DLY)
		seq_puts(seq, "BATFET_DLY Turn off BATFET after 10s if BATFET_DISABLE set\n");
	else
		seq_puts(seq, "BATFET_DLY Turn off BATFET immedialety if BATFET_DISABLE set\n");

	if (val & BATFET_RST_EN)
		seq_puts(seq, "BATFET_RST_EN is enabled\n");
	else
		seq_puts(seq, "BATFET_RST_EN is disabled\n");

	if (val & PUMPX_UP)
		seq_puts(seq, "PUMPX_UP pump express voltage up is enabled\n");
	else
		seq_puts(seq, "PUMPX_UP pump express voltage up is disabled\n");

	if (val & PUMPX_DN)
		seq_puts(seq, "PUMPX_DN pump express voltage down is enabled\n");
	else
		seq_puts(seq, "PUMPX_DN pump express voltage down is disabled\n");
}

static void bq25898_rega_to_human(struct seq_file *seq, u8 reg, u8 val)
{
	int res = 4550;

	if (val & BOOSTV3)
		res += 512;
	if (val & BOOSTV2)
		res += 256;
	if (val & BOOSTV1)
		res += 128;
	if (val & BOOSTV0)
		res += 64;

	seq_printf(seq, "Boost Voltage %dmV\n", res);

	if (val & PFM_OTG_DIS)
		seq_puts(seq, "PFM_OTG_DIS disabled\n");
	else
		seq_puts(seq, "PFM_OTG_DIS enabled\n");

	seq_puts(seq, "BOOST_LIM ");
	if (!(val & BOOST_LIM2) && !(val & BOOST_LIM1) && !(val & BOOST_LIM0))
		seq_puts(seq, "0.5A\n");
	else if (!(val & BOOST_LIM2) && !(val & BOOST_LIM1) && (val & BOOST_LIM0))
		seq_puts(seq, "0.8A\n");
	else if (!(val & BOOST_LIM2) && (val & BOOST_LIM1) && !(val & BOOST_LIM0))
		seq_puts(seq, "1.0A\n");
	else if (!(val & BOOST_LIM2) && (val & BOOST_LIM1) && (val & BOOST_LIM0))
		seq_puts(seq, "1.2A\n");
	else if ((val & BOOST_LIM2) && !(val & BOOST_LIM1) && !(val & BOOST_LIM0))
		seq_puts(seq, "1.5A\n");
	else if ((val & BOOST_LIM2) && !(val & BOOST_LIM1) && (val & BOOST_LIM0))
		seq_puts(seq, "1.8A\n");
	else if ((val & BOOST_LIM2) && (val & BOOST_LIM1) && !(val & BOOST_LIM0))
		seq_puts(seq, "2.1A\n");
	else if ((val & BOOST_LIM2) && (val & BOOST_LIM1) && (val & BOOST_LIM0))
		seq_puts(seq, "2.4A\n");

}

static void bq25898_regb_to_human(struct seq_file *seq, u8 reg, u8 val)
{
	seq_puts(seq, "VBUS_STATUS ");
	if (!(val & VBUS_STATUS2) && !(val & VBUS_STATUS1) && !(val & VBUS_STATUS0))
		seq_puts(seq, "no input\n");
	else if (!(val & VBUS_STATUS2) && !(val & VBUS_STATUS1) && (val & VBUS_STATUS0))
		seq_puts(seq, "USB host SDP\n");
	else if (!(val & VBUS_STATUS2) && (val & VBUS_STATUS1) && !(val & VBUS_STATUS0))
		seq_puts(seq, "adapter 3.25A\n");


	seq_puts(seq, "CHARGER_STATUS ");
	if (!(val & CHARGER_STATUS1) && !(val & CHARGER_STATUS0))
		seq_puts(seq, "Not charging\n");
	else if (!(val & CHARGER_STATUS1) && (val & CHARGER_STATUS0))
		seq_puts(seq, "Precharge\n");
	else if ((val & CHARGER_STATUS1) && !(val & CHARGER_STATUS0))
		seq_puts(seq, "Fast charging\n");
	else if ((val & CHARGER_STATUS1) && (val & CHARGER_STATUS0))
		seq_puts(seq, "Charge termination done\n");

	if (val & PG_STAT)
		seq_puts(seq, "PG_STAT Power good\n");
	else
		seq_puts(seq, "PG_STAT Not power good\n");

	seq_puts(seq, "VSYS_STAT ");
	if (val & VSYS_STAT)
		seq_puts(seq, "In VSYSMIN regulation (BAT < VSYSMIN)\n");
	else
		seq_puts(seq, "Not in VSYSMIN regulation\n");

}

static void bq25898_regc_to_human(struct seq_file *seq, u8 reg, u8 val)
{

	seq_puts(seq, "WATCHDOG_FAULT ");
	if (val & WATCHDOG_FAULT_MASK)
		seq_puts(seq, "WD Timer expiration\n");
	else
		seq_puts(seq, "WD Normal\n");

	seq_puts(seq, "BOOST_FAULT ");
	if (val & BOOST_FAULT_MASK)
		seq_puts(seq, "VBUS overloaded in OTG, or VBUS OVP or battery too low\n");
	else
		seq_puts(seq, "Normal\n");

	seq_puts(seq, "CHARGER_FAULT ");
	seq_puts(seq, CHARGER_fault_to_human[(val & CHARGER_FAULT_MASK) >> 4]);
	seq_puts(seq, "\n");

	seq_puts(seq, "BAT_FAULT ");
	if (val & BAT_FAULT_MASK)
		seq_puts(seq, "BATOVP!!\n");
	else
		seq_puts(seq, "Normal\n");

	seq_puts(seq, "NTC_FAULT ");
	seq_puts(seq, NTC_fault_to_human[val & NTC_FAULT_MASK]);
	seq_puts(seq, "\n");
}

static void bq25898_regd_to_human(struct seq_file *seq, u8 reg, u8 val)
{
	int res = 0;

	if (val & FORCE_VINDPM)
		seq_puts(seq, "Run Absolute VINDPM Threshold\n");
	else
		seq_puts(seq, "Run Relative VINDPM Threshold\n");

	res = 2600;
	if (val & VINDPM6)
		res += 6400;
	if (val & VINDPM5)
		res += 3200;
	if (val & VINDPM4)
		res += 1600;
	if (val & VINDPM3)
		res += 800;
	if (val & VINDPM2)
		res += 400;
	if (val & VINDPM1)
		res += 200;
	if (val & VINDPM0)
		res += 100;

	seq_printf(seq, "VINDPM Absolute threshold %dmV\n", res);
}

/* Convert register to uV value. Ignores THERM_STAT bit */
static inline int bq25898_rege_convert_uv(u8 val)
{
	int res = 2304000;

	if (val & ADC_CONV_BATV6)
		res += 1280000;
	if (val & ADC_CONV_BATV5)
		res += 640000;
	if (val & ADC_CONV_BATV4)
		res += 320000;
	if (val & ADC_CONV_BATV3)
		res += 160000;
	if (val & ADC_CONV_BATV2)
		res += 80000;
	if (val & ADC_CONV_BATV1)
		res += 40000;
	if (val & ADC_CONV_BATV0)
		res += 20000;

	return res;
}

static void bq25898_rege_to_human(struct seq_file *seq, u8 reg, u8 val)
{
	seq_puts(seq, "THERM_STAT ");
	if (val & THERM_STAT)
		seq_puts(seq, "in thermal regulation\n");
	else
		seq_puts(seq, "normal thermal\n");

	seq_printf(seq, "ADC Conversion BAT voltage %dmV\n",
		   bq25898_rege_convert_uv(val)/1000);

}

static void bq25898_regf_to_human(struct seq_file *seq, u8 reg, u8 val)
{
	int res = 2304;

	if (val & ADC_CONV_SYSV6)
		res += 1280;
	if (val & ADC_CONV_SYSV5)
		res += 640;
	if (val & ADC_CONV_SYSV4)
		res += 320;
	if (val & ADC_CONV_SYSV3)
		res += 160;
	if (val & ADC_CONV_SYSV2)
		res += 80;
	if (val & ADC_CONV_SYSV1)
		res += 40;
	if (val & ADC_CONV_SYSV0)
		res += 20;

	seq_printf(seq, "ADC Conversion SYS voltage %dmV\n", res);
}

/* Return value in 10th of degree Celsius */
static inline int bq25898_reg10_convert_c(u8 val)
{
	static const int temp_lookup[] = {
		848, 837, 826, 816,
		806, 796, 786, 776,
		767, 757, 748, 739,
		730, 721, 713, 704,
		696, 687, 679, 671,
		663, 655, 647, 639,
		632, 624, 616, 609,
		602, 594, 587, 580,
		573, 566, 558, 551,
		545, 538, 531, 524,
		517, 511, 504, 497,
		491, 484, 477, 471,
		464, 458, 451, 445,
		439, 432, 426, 419,
		413, 407, 400, 394,
		388, 381, 375, 369,
		362, 356, 350, 343,
		337, 331, 324, 318,
		312, 305, 299, 292,
		286, 280, 273, 266,
		260, 253, 247, 240,
		233, 227, 220, 213,
		206, 199, 192, 185,
		178, 171, 164, 156,
		149, 141, 134, 126,
		118, 110, 102,  94,
		 86,  78,  69,  60,
		 51,  42,  33,  24,
		 14,   4,  -5, -16,
		-27, -38, -49, -61,
		-74, -86, -100, -114,
		-129, -144, -161, -179,
	};

	return temp_lookup[val];
}

static void bq25898_reg10_to_human(struct seq_file *seq, u8 reg, u8 val)
{
	int res = 2100;

	if (val & TSPCT6)
		res += 2976;
	if (val & TSPCT5)
		res += 1488;
	if (val & TSPCT4)
		res += 744;
	if (val & TSPCT3)
		res += 372;
	if (val & TSPCT2)
		res += 186;
	if (val & TSPCT1)
		res += 93;
	if (val & TSPCT0)
		res += 46;

	seq_printf(seq, "TSPCT ADC Conversion of TS Voltage ~%d/100 %% of REGN\n", res);
}

static void bq25898_reg11_to_human(struct seq_file *seq, u8 reg, u8 val)
{
	int res = 0;

	if (val & VBUS_ATTACHED)
		seq_puts(seq, "VBUS attached\n");
	else
		seq_puts(seq, "VBUS NOT attached\n");

	res = 2600;
	if (val & VBUSV_ADC_VOLT6)
		res += 6400;
	if (val & VBUSV_ADC_VOLT5)
		res += 3200;
	if (val & VBUSV_ADC_VOLT4)
		res += 1600;
	if (val & VBUSV_ADC_VOLT3)
		res += 800;
	if (val & VBUSV_ADC_VOLT2)
		res += 400;
	if (val & VBUSV_ADC_VOLT1)
		res += 200;
	if (val & VBUSV_ADC_VOLT0)
		res += 100;

	seq_printf(seq, "VBUSV ADC conversion of Vbus voltage %dmV\n", res);

}

static void bq25898_reg12_to_human(struct seq_file *seq, u8 reg, u8 val)
{
	int res = 0;
	if (val & ADC_CHARGE_CURRENT6)
		res += 3200;
	if (val & ADC_CHARGE_CURRENT5)
		res += 1600;
	if (val & ADC_CHARGE_CURRENT4)
		res += 800;
	if (val & ADC_CHARGE_CURRENT3)
		res += 400;
	if (val & ADC_CHARGE_CURRENT2)
		res += 200;
	if (val & ADC_CHARGE_CURRENT1)
		res += 100;
	if (val & ADC_CHARGE_CURRENT0)
		res += 50;

	seq_printf(seq, "ADC_CHARGE_CURRENT ADC conversion of charge current %dmA\n", res);
}

static void bq25898_reg13_to_human(struct seq_file *seq, u8 reg, u8 val)
{
	int res = 100;

	seq_puts(seq, "VDPM_STAT ");
	if (val & VDPM_STAT)
		seq_puts(seq, "in VINDPM\n");
	else
		seq_puts(seq, "not in VINDPM\n");

	seq_puts(seq, "IDPM_STAT ");
	if (val & IDPM_STAT)
		seq_puts(seq, "in IINDPM\n");
	else
		seq_puts(seq, "not in IINDPM\n");


	if (val & IDPM_CURRENT_LIM5)
		res += 1600;
	if (val & IDPM_CURRENT_LIM4)
		res += 800;
	if (val & IDPM_CURRENT_LIM3)
		res += 400;
	if (val & IDPM_CURRENT_LIM2)
		res += 200;
	if (val & IDPM_CURRENT_LIM1)
		res += 100;
	if (val & IDPM_CURRENT_LIM0)
		res += 50;

	seq_printf(seq, "IDPM Current Limit %dmA\n", res);
}

static void bq25898_reg14_to_human(struct seq_file *seq, u8 reg, u8 val)
{

	seq_puts(seq, "REG_RST ");
	if (val & REG_RST)
		seq_puts(seq, "reset to default\n");
	else
		seq_puts(seq, "keep current register setting\n");

	seq_puts(seq, "ICO_OPTIMIZED ");
	if (val & ICO_OPTIMIZED)
		seq_puts(seq, "maximum input current detected\n");
	else
		seq_puts(seq, "optimization is in progress\n");

	seq_printf(seq, "Revision: %d\n", (val & (DEV_REV1 | DEV_REV0)));
}

/* invoked by reading content of register >= BQ25898_ALL_REGISTER
 * basically, any unknown register */
static void bq25898_regall_to_human(struct i2c_client *client, struct seq_file *seq)
{
	u8 i;
	u8 reg = 0;
	u8 val = 0;

	if (!client || !seq)
		return;

	seq_printf(seq, "REG %02X:\n", reg);
	val = bq25898_read_reg(client, reg++);
	if (val >= 0)
		bq25898_reg0_to_human(seq, reg, val);

	seq_printf(seq, "REG %02X:\n", reg);
	val = bq25898_read_reg(client, reg++);
	if (val >= 0)
		bq25898_reg1_to_human(seq, reg, val);

	seq_printf(seq, "REG %02X:\n", reg);
	val = bq25898_read_reg(client, reg++);
	if (val >= 0)
		bq25898_reg2_to_human(seq, reg, val);

	seq_printf(seq, "REG %02X:\n", reg);
	val = bq25898_read_reg(client, reg++);
	if (val >= 0)
		bq25898_reg3_to_human(seq, reg, val);

	seq_printf(seq, "REG %02X:\n", reg);
	val = bq25898_read_reg(client, reg++);
	if (val >= 0)
		bq25898_reg4_to_human(seq, reg, val);

	seq_printf(seq, "REG %02X:\n", reg);
	val = bq25898_read_reg(client, reg++);
	if (val >= 0)
		bq25898_reg5_to_human(seq, reg, val);

	seq_printf(seq, "REG %02X:\n", reg);
	val = bq25898_read_reg(client, reg++);
	if (val >= 0)
		bq25898_reg6_to_human(seq, reg, val);

	seq_printf(seq, "REG %02X:\n", reg);
	val = bq25898_read_reg(client, reg++);
	if (val >= 0)
		bq25898_reg7_to_human(seq, reg, val);

	seq_printf(seq, "REG %02X:\n", reg);
	val = bq25898_read_reg(client, reg++);
	if (val >= 0)
		bq25898_reg8_to_human(seq, reg, val);

	seq_printf(seq, "REG %02X:\n", reg);
	val = bq25898_read_reg(client, reg++);
	if (val >= 0)
		bq25898_reg9_to_human(seq, reg, val);

	seq_printf(seq, "REG %02X:\n", reg);
	val = bq25898_read_reg(client, reg++);
	if (val >= 0)
		bq25898_rega_to_human(seq, reg, val);

	seq_printf(seq, "REG %02X:\n", reg);
	val = bq25898_read_reg(client, reg++);
	if (val >= 0)
		bq25898_regb_to_human(seq, reg, val);

	seq_printf(seq, "REG %02X:\n", reg);
	val = bq25898_read_reg(client, reg++);
	if (val >= 0)
		bq25898_regc_to_human(seq, reg, val);

	seq_printf(seq, "REG %02X:\n", reg);
	val = bq25898_read_reg(client, reg++);
	if (val >= 0)
		bq25898_regd_to_human(seq, reg, val);

	seq_printf(seq, "REG %02X:\n", reg);
	val = bq25898_read_reg(client, reg++);
	if (val >= 0)
		bq25898_rege_to_human(seq, reg, val);

	seq_printf(seq, "REG %02X:\n", reg);
	val = bq25898_read_reg(client, reg++);
	if (val >= 0)
		bq25898_regf_to_human(seq, reg, val);

	seq_printf(seq, "REG %02X:\n", reg);
	val = bq25898_read_reg(client, reg++);
	if (val >= 0)
		bq25898_reg10_to_human(seq, reg, val);

	seq_printf(seq, "REG %02X:\n", reg);
	val = bq25898_read_reg(client, reg++);
	if (val >= 0)
		bq25898_reg11_to_human(seq, reg, val);

	seq_printf(seq, "REG %02X:\n", reg);
	val = bq25898_read_reg(client, reg++);
	if (val >= 0)
		bq25898_reg12_to_human(seq, reg, val);

	seq_printf(seq, "REG %02X:\n", reg);
	val = bq25898_read_reg(client, reg++);
	if (val >= 0)
		bq25898_reg13_to_human(seq, reg, val);

	seq_printf(seq, "REG %02X:\n", reg);
	val = bq25898_read_reg(client, reg++);
	if (val >= 0)
		bq25898_reg14_to_human(seq, reg, val);

	for (i=0; i<ARRAY_SIZE(fault_count); i++) {
		seq_printf(seq, "fault count for %d:%d\n", i, fault_count[i]);
	}
}


static int bq25898_reg_show(struct seq_file *seq, void *unused)
{
	int val;
	u8 reg;
	struct bq25898_debugfs_file *dbgfs_file;

	if (!seq)
		return -EINVAL;

	dbgfs_file = (struct bq25898_debugfs_file *) seq->private;

	reg = (u8) (dbgfs_file->regnr);
	/* if we want to have a human output -dump all register */
	if (reg >= BQ25898_ALL_REGISTER) {
		bq25898_regall_to_human(dbgfs_file->parent->client, seq);
	} else {
		val = bq25898_read_reg(dbgfs_file->parent->client, reg);
		seq_printf(seq, "0x%02x\n", val);
	}

	return 0;
}

static ssize_t bq25898_dbgfs_write(struct file *pfile, const char __user *user_data,
				size_t count, loff_t *ppos)
{
	u8 val, reg;
	struct bq25898_debugfs_file *dbgfs_file;
	struct seq_file *seq = pfile->private_data;
	int ret;
	char locbuf[BUFSIZ_COPY_FROM_USER+1] = {0};

	if (!seq)
		return -EINVAL;

	dbgfs_file = (struct bq25898_debugfs_file*) seq->private;

	if (!dbgfs_file)
		return -EINVAL;

	if (count > BUFSIZ_COPY_FROM_USER)
		count = BUFSIZ_COPY_FROM_USER;

	if (copy_from_user(locbuf, user_data, count))
		return -EINVAL;

	ret = kstrtou8(locbuf, 10, &val);
	if (ret < 0) {
		/* try base 16 */
		ret = kstrtou8(locbuf, 16, &val);
		if (ret < 0)
			return ret;
	}

	reg = (u8) dbgfs_file->regnr;

	if (reg < BQ25898_FIRST_REGISTER || reg > BQ25898_LAST_REGISTER)
		return -EINVAL;

	ret = bq25898_write_reg(dbgfs_file->parent->client, reg, val);

	if (ret < 0) {
		dev_err(&dbgfs_file->parent->client->dev, "Error writing val 0x%02X:%d\n", val, ret);
		return ret;
	}

	return count;
}

static int bq25898_dbgfs_open(struct inode *inode, struct file *file)
{
	return single_open(file, bq25898_reg_show, inode->i_private);
}

static const struct file_operations bq25898_dbg_fops = {
	.open = bq25898_dbgfs_open,
	.read = seq_read,
	.llseek = seq_lseek,
	.release = single_release,
	.write = bq25898_dbgfs_write,
};

static void bq25898_debugfs_init(struct bq25898_charger *chip)
{
	struct dentry *fentry;
	u32 i;
	char name[6] = {0};

	if (!chip)
		return;

	chip->dbgfs_dir = debugfs_create_dir(DEV_NAME, NULL);
	if (chip->dbgfs_dir == NULL)
		goto debugfs_root_exit;

	for (i = 0; i <= BQ25898_ALL_REGISTER; i++) {
		snprintf(name, 6, "%02x", i);
		chip->dbgfs_file[i].parent = chip;
		chip->dbgfs_file[i].regnr = i;
		fentry = debugfs_create_file(name, S_IRUGO,
						chip->dbgfs_dir,
						&chip->dbgfs_file[i],
						&bq25898_dbg_fops);

		if (fentry == NULL)
			goto debugfs_err_exit;
	}

	fentry = debugfs_create_u32(BQ25898_INT_COUNTER, S_IRUGO,
			chip->dbgfs_dir, &chip->irq_counter);

	if (fentry == NULL)
		goto debugfs_err_exit;

	return;

debugfs_err_exit:
	debugfs_remove_recursive(chip->dbgfs_dir);
debugfs_root_exit:
	dev_err(&chip->client->dev, "Error Creating debugfs!!\n");
	return;
}

static void bq25898_debugfs_exit(struct bq25898_charger *chip)
{

	if (!chip)
		return;

	debugfs_remove_recursive(chip->dbgfs_dir);

	return;
}
#else /* !CONFIG_DEBUG_FS */

static int bq25898_debugfs_init(struct bq25898_charger *chip)
{
	(void) chip;
	return 0;
}

static int bq25898_debugfs_exit(struct bq25898_charger *chip)
{
	(void) chip;
	return;
}
#endif /* !CONFIG_DEBUG_FS */

static int enable_ship_mode(struct i2c_client *bq25898_client)
{
	int ret = 0;

	/* Enable I2C Ship mode */

	ret = bq25898_read_modify_reg(bq25898_client, BQ25898_SAFETY_TIMER_CTRL_REG,
			BATFET_DISABLE, 0x20);
	if (ret < 0) {
		dev_err(&bq25898_client->dev,
			"I2C ship mode enable write failed: %d", ret);
		return ret;
	}

	dev_info(&bq25898_client->dev, "Ship mode enabled.");

	return ret;
}

static int disable_ship_mode(struct i2c_client *bq25898_client)
{
	int ret = 0;

	/* Disable I2C Ship mode */

	ret = bq25898_read_modify_reg(bq25898_client, BQ25898_SAFETY_TIMER_CTRL_REG,
			BATFET_DISABLE, 0);
	if (ret < 0) {
		dev_err(&bq25898_client->dev,
			"I2C ship mode disable write failed: %d", ret);
		return ret;
	}

	dev_info(&bq25898_client->dev, "Ship mode disabled.");

	return ret;
}

/**
 * get_ship_mode - get function for sysfs ship_mode
 * Parameters as defined by sysfs interface
 */
static ssize_t get_ship_mode(struct device *dev,
				struct device_attribute *attr, char *buf)
{
	struct i2c_client *bq25898_client = container_of(dev, struct i2c_client, dev);
	struct bq25898_charger *chip = i2c_get_clientdata(bq25898_client);

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

/**
 * get_postcharge_duration - get function for sysfs postcharge_duration
 * Parameters as defined by sysfs interface
 */
static ssize_t get_postcharge_duration(struct device *dev,
				struct device_attribute *attr, char *buf)
{
	struct i2c_client *bq25898_client = container_of(dev, struct i2c_client, dev);
	struct bq25898_charger *chip = i2c_get_clientdata(bq25898_client);

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

/**
 * get_curr_check_interval - get function for sysfs curr_check_interval
 * Parameters as defined by sysfs interface
 */
static ssize_t get_curr_check_interval(struct device *dev,
				struct device_attribute *attr, char *buf)
{
	struct i2c_client *bq25898_client = container_of(dev, struct i2c_client, dev);
	struct bq25898_charger *chip = i2c_get_clientdata(bq25898_client);

	return scnprintf(buf, 8, "%d\n", chip->curr_check_interval / ONE_MINUTE);
}

/**
 * get_curr_eoc_limit - get function for sysfs curr_eoc_limit
 * Parameters as defined by sysfs interface
 */
static ssize_t get_curr_eoc_limit(struct device *dev,
				struct device_attribute *attr, char *buf)
{
	struct i2c_client *bq25898_client = container_of(dev, struct i2c_client, dev);
	struct bq25898_charger *chip = i2c_get_clientdata(bq25898_client);

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

/**
 * set_ship_mode - set function for sysfs ship_mode
 * Parameters as defined by sysfs interface
 * ship_mode can take the values 0 and 1
 */
static ssize_t set_ship_mode(struct device *dev,
				struct device_attribute *attr, const char *buf,
				size_t count)
{
	struct i2c_client *bq25898_client = container_of(dev, struct i2c_client, dev);
	struct bq25898_charger *chip = i2c_get_clientdata(bq25898_client);
	unsigned long value;

	if (kstrtoul(buf, 10, &value))
		return -EINVAL;

	/* allow only 0 or 1 */
	if (value > 1)
		return -EINVAL;

	mutex_lock(&chip->ship_mode_lock);
	if (value)
		/* ship_mode is enabled after a reboot notification */
		chip->ship_mode_scheduled = true;
	else
		chip->ship_mode_scheduled = false;
	mutex_unlock(&chip->ship_mode_lock);

	return count;
}

/**
 * set_postcharge_duration - set function for sysfs postcharge_duration
 * Parameters as defined by sysfs interface
 */
static ssize_t set_postcharge_duration(struct device *dev,
				struct device_attribute *attr, const char *buf,
				size_t count)
{
	struct i2c_client *bq25898_client = container_of(dev, struct i2c_client, dev);
	struct bq25898_charger *chip = i2c_get_clientdata(bq25898_client);
	unsigned long value;

	if (kstrtoul(buf, 10, &value) ||
		(value > MAX_POSTCHARGE_DURATION))
		return -EINVAL;

	mutex_lock(&chip->sysfs_lock);
	chip->postcharge_duration_mn = value;
	mutex_unlock(&chip->sysfs_lock);

	return count;
}

/**
 * set_curr_check_interval - set function for sysfs curr_check_interval
 * Parameters as defined by sysfs interface
 */
static ssize_t set_curr_check_interval(struct device *dev,
				struct device_attribute *attr, const char *buf,
				size_t count)
{
	struct i2c_client *bq25898_client = container_of(dev, struct i2c_client, dev);
	struct bq25898_charger *chip = i2c_get_clientdata(bq25898_client);
	unsigned long value;

	if (kstrtoul(buf, 10, &value) ||
		(value > MAX_CURR_CHECK_INTERVAL) || (value == 0))
		return -EINVAL;

	mutex_lock(&chip->sysfs_lock);
	chip->curr_check_interval = value * ONE_MINUTE;
	mutex_unlock(&chip->sysfs_lock);

	return count;
}

/**
 * set_curr_eoc_limit - set function for sysfs curr_eoc_limit
 * Parameters as defined by sysfs interface
 */
static ssize_t set_curr_eoc_limit(struct device *dev,
				struct device_attribute *attr, const char *buf,
				size_t count)
{
	struct i2c_client *bq25898_client = container_of(dev, struct i2c_client, dev);
	struct bq25898_charger *chip = i2c_get_clientdata(bq25898_client);
	unsigned long value;

	if (kstrtoul(buf, 10, &value) ||
		(value > MAX_CURR_EOC_LIMIT))
		return -EINVAL;

	mutex_lock(&chip->sysfs_lock);
	chip->curr_eoc_limit = value;
	mutex_unlock(&chip->sysfs_lock);

	return count;
}

#ifdef CONFIG_SYSFS
static DEVICE_ATTR(ship_mode, S_IRUGO | S_IWUSR,
	get_ship_mode, set_ship_mode);
static DEVICE_ATTR(postcharge_duration_mn, S_IRUGO | S_IWUSR,
	get_postcharge_duration, set_postcharge_duration);
static DEVICE_ATTR(curr_check_interval_mn, S_IRUGO | S_IWUSR,
	get_curr_check_interval, set_curr_check_interval);
static DEVICE_ATTR(curr_eoc_limit, S_IRUGO | S_IWUSR,
	get_curr_eoc_limit, set_curr_eoc_limit);

static struct attribute *sysfs_attrs_ctrl[] = {
	&dev_attr_ship_mode.attr,
	&dev_attr_postcharge_duration_mn.attr,
	&dev_attr_curr_check_interval_mn.attr,
	&dev_attr_curr_eoc_limit.attr,
	NULL
};

static struct attribute_group bq25898_attribute_group = {
	.attrs = sysfs_attrs_ctrl,
};

#endif /* !CONFIG_SYSFS */

static int bq25898_wdt_configure(struct i2c_client *client, enum bq25898_wdt_timing val)
{
	int reg;

	if (!client)
		return -EINVAL;

	reg = bq25898_read_reg(client, BQ25898_TERM_WDT_SFTY_CTRL_REG);
	if (reg < 0)
		return reg;

	dev_info(&client->dev, "current wdt reg: 0x%02x\n", reg);

	switch (val) {
	case BQ25898_WDT_TIMER_DISABLE:
		reg &= ~(I2C_WDT_TIMER_SETTINGS1 | I2C_WDT_TIMER_SETTINGS0);
		break;
	case BQ25898_WDT_TIMER_40S:
		reg &= ~I2C_WDT_TIMER_SETTINGS1;
		reg |= I2C_WDT_TIMER_SETTINGS0;
		break;
	case BQ25898_WDT_TIMER_80S:
		reg &= ~I2C_WDT_TIMER_SETTINGS0;
		reg |= I2C_WDT_TIMER_SETTINGS1;
		break;
	case BQ25898_WDT_TIMER_160S:
		reg |= (I2C_WDT_TIMER_SETTINGS1 | I2C_WDT_TIMER_SETTINGS0);
		break;
	}

	dev_info(&client->dev, "wdt new value: 0x%02x\n", reg);

	reg = bq25898_write_reg(client, BQ25898_TERM_WDT_SFTY_CTRL_REG, reg);
	dev_info(&client->dev, "wdt ret: 0x%02x\n", reg);

	return reg;
}

/* Don't call this function directly from interrupt context! */
static int bq25898_enable_charging(struct i2c_client *client)
{
	int ret = 0;
	struct bq25898_charger *chip;

	if (!client)
		return -EINVAL;

	chip = i2c_get_clientdata(client);
	if (!chip)
		return -EINVAL;

	/* disable charging */
	ret = bq25898_read_modify_reg(client, BQ25898_CHARGE_CTRL_REG,
				CHG_CONFIG, 0);

	if (ret < 0)
		dev_err(&client->dev, "error disabling charging %d\n", ret);

	dev_info(&client->dev, "enabling charge termination\n");

	/* enable charge termination */
	ret = bq25898_read_modify_reg(client, BQ25898_TERM_WDT_SFTY_CTRL_REG,
				CHARGE_TERM_ENABLE, 0x80);

	if (ret < 0)
		dev_err(&client->dev, "error enabling charge termination %d\n", ret);

	dev_info(&client->dev, "enabling charging\n");

	/* enable charging */
	ret = bq25898_read_modify_reg(client, BQ25898_CHARGE_CTRL_REG,
				CHG_CONFIG, 0x10);

	if (ret < 0)
		dev_err(&client->dev, "error enabling charging %d\n", ret);

	return ret;
}

static int bq25898_ship_mode_configure(struct i2c_client *client)
{
	int ret = 0;
	struct bq25898_charger *chip = i2c_get_clientdata(client);

	if (!chip)
		return -EINVAL;

	dev_info(&client->dev, "Configure charger, setting default values for ship_mode\n");

	/* disable ship_mode */
	mutex_lock(&chip->ship_mode_lock);
	ret = disable_ship_mode(client);
	mutex_unlock(&chip->ship_mode_lock);
	if (ret < 0) {
		dev_err(&client->dev, "error disabling ship mode\n");
		return ret;
	}
	/* set ship mode delay to 10s */
	if (ship_mode_delay_10s) {
		ret = bq25898_read_modify_reg(client, BQ25898_SAFETY_TIMER_CTRL_REG,
				BATFET_DLY, 0x8);
		if (ret < 0)
			dev_err(&client->dev, "error setting ship mode delay to 10s\n");
	}

	return ret;
}

static int bq25898_check_expected_register(struct i2c_client *client, u8 address, u8 expected_val)
{
	int ret = 0, val;
	struct bq25898_charger *chip = i2c_get_clientdata(client);

	val = bq25898_read_reg(client, address);
	if (val < 0)
		return val;
	if (val != expected_val) {
		dev_warn(&client->dev,
			"wrong value for register (0x%02x):0x%02x  fixing it ...\n",
			address, val);
		ret = bq25898_write_reg(client, address, expected_val);
		if (ret < 0)
			return ret;
	}

	return ret;
}

static int bq25898_check_configuration(struct i2c_client *client)
{
	int ret = 0;
	struct bq25898_charger *chip = i2c_get_clientdata(client);

	dev_info(&client->dev, "checking registers configuration\n");
	/* check register 0x00 */
	ret = bq25898_check_expected_register(client, BQ25898_INPUT_SRC_CTRL_REG,
			chip->pdata->reg_config.reg00);
	if (ret < 0)
		return ret;
	/* check register 0x04 */
	ret = bq25898_check_expected_register(client, BQ25898_FAST_CHARGE_CTRL_REG,
			chip->pdata->reg_config.reg04);
	if (ret < 0)
		return ret;
	/* check register 0x05 */
	ret = bq25898_check_expected_register(client, BQ25898_CUR_LIMIT_CTRL_REG,
			chip->pdata->reg_config.reg05);
	if (ret < 0)
		return ret;
	/* check register 0x06 */
	ret = bq25898_check_expected_register(client, BQ25898_VLIM_CHRG_CTRL_REG,
			chip->pdata->reg_config.reg06);

	return ret;
}

/* Don't call this function directly from interrupt context! */
static int bq25898_charger_configure(struct i2c_client *client)
{
	int ret = 0;

	if (!client)
		return -EINVAL;

	ret = bq25898_check_configuration(client);
	if (ret < 0)
		return ret;
	ret = bq25898_ship_mode_configure(client);
	if (ret < 0)
		return ret;
	ret = bq25898_enable_charging(client);

	return ret;
}

static int bq25898_wdt_kick(struct bq25898_charger *chip)
{
	int ret = 0;

	if (!chip)
		return -EINVAL;

	ret = bq25898_read_modify_reg(chip->client,
				BQ25898_CHARGE_CTRL_REG,
				WD_TIMER_RESET, WD_TIMER_RESET);
	if (ret < 0)
		dev_err(&chip->client->dev, "fail to kick watchdog:%d\n", ret);

	return ret;
}

static int bq25898_restore_configuration(struct bq25898_charger *chip)
{
	int ret = 0;

	if (!chip->client)
		return -EINVAL;

	/* Restore correct value for reg 0x00*/
	ret = bq25898_write_reg(chip->client, BQ25898_INPUT_SRC_CTRL_REG, chip->pdata->reg_config.reg00);
	if (ret < 0) {
		dev_err(&chip->client->dev, "error restoring reg00 value\n");
		return ret;
	}
	/* Restore correct value for reg 0x04*/
	ret = bq25898_write_reg(chip->client, BQ25898_FAST_CHARGE_CTRL_REG, chip->pdata->reg_config.reg04);
	if (ret < 0) {
		dev_err(&chip->client->dev, "error restoring reg04 value\n");
		return ret;
	}
	/* Restore correct value for reg 0x05*/
	ret = bq25898_write_reg(chip->client, BQ25898_CUR_LIMIT_CTRL_REG, chip->pdata->reg_config.reg05);
	if (ret < 0) {
		dev_err(&chip->client->dev, "error restoring reg05 value\n");
		return ret;
	}
	/* Restore correct value for reg 0x06*/
	ret = bq25898_write_reg(chip->client, BQ25898_VLIM_CHRG_CTRL_REG, chip->pdata->reg_config.reg06);
	if (ret < 0) {
		dev_err(&chip->client->dev, "error restoring reg06 value\n");
		return ret;
	}

	ret = bq25898_ship_mode_configure(chip->client);
	if (ret < 0)
		return ret;

	/* Enable Watchdog Timer */
	ret = bq25898_wdt_configure(chip->client, BQ25898_WDT_TIMER_160S);
	if (ret < 0) {
		dev_err(&chip->client->dev, "error enabling watchdog\n");
		return ret;
	} else {
		chip->watchdog_state = WDT_ENABLED;
	}

	return ret;
}

static int bq25898_charger_status_reg_handler(struct bq25898_charger *chip)
{
	int ret = 0, val;

	val = bq25898_read_reg(chip->client, BQ25898_STATUS_REG);
	if (val < 0) {
		dev_err(&chip->client->dev, "Error reading charger reg %d:%d\n",
			BQ25898_STATUS_REG, val);
		return val;
	}

	dev_info(&chip->client->dev, "Charger_status (0x%02x):0x%02x\n", BQ25898_STATUS_REG, val);

	chip->irq_counter++;

	/*
	 * if charge termination, and if postcharging feature is enabled
	 * then force charging for a maximum of 30 mn.
	 */
	if ((val & CHARGER_STATUS1) && (val & CHARGER_STATUS0)
		&& ((chip->status_reg_oldvalue & CHARGER_STATUS_MASK) != CHARGER_STATUS_MASK)) {
		if (chip->pdata->enable_postcharge) {
			if (!(chip->is_charge_complete)) {
				mutex_lock(&chip->charge_config_lock);
				ret = bq25898_force_charging(chip);
				mutex_unlock(&chip->charge_config_lock);
				if (ret < 0)
					return ret;
				/* update current status variable as charging status
				 * has been modified by bq25898_force_charging
				 */
				val = bq25898_read_reg(chip->client, BQ25898_STATUS_REG);
				if (val < 0) {
					dev_err(&chip->client->dev, "Error reading charger reg %d:%d\n",
						BQ25898_STATUS_REG, val);
					return val;
				}
			} else {
				chip->is_charge_complete = false;
			}
		} else {
			dev_info(&chip->client->dev, "Received interrupt for End of charge\n");
		}
	} else if ((val & PG_STAT) && ((chip->status_reg_oldvalue & PG_STAT_MASK) != PG_STAT_MASK)) {
		dev_info(&chip->client->dev, "Received interrupt for PG_STAT\n");
	} else if ((val & VBUS_STATUS_MASK) != (chip->status_reg_oldvalue & VBUS_STATUS_MASK)) {
		dev_info(&chip->client->dev, "Received interrupt for VBUS_STATUS\n");
	} else {
		dev_info(&chip->client->dev,
			"Discarding received charger interrupt\n");
	}

	chip->status_reg_oldvalue = val;
	return ret;
}

static int bq25898_charger_fault_reg_handler(struct bq25898_charger *chip)
{
	int ret = 0, val;

	/* Read this register two times to get the current value
	 * as specified by the vendor */
	val = bq25898_read_reg(chip->client, BQ25898_FAULT_REG);
	if (val < 0)
		return val;
	val = bq25898_read_reg(chip->client, BQ25898_FAULT_REG);
	if (val < 0)
		return val;

	dev_info(&chip->client->dev,
		"Charger_Fault_reg (0x%02x):0x%02x\n", BQ25898_FAULT_REG, val);

	if (val & WATCHDOG_FAULT_MASK) {
		dev_warn(&chip->client->dev,
			"Watchdog Timer has expired... Starting recovery\n");
		mutex_lock(&chip->charge_config_lock);
		ret = bq25898_restore_configuration(chip);
		mutex_unlock(&chip->charge_config_lock);
		if (ret < 0)
			dev_err(&chip->client->dev,
				"Unable to restore charger's register configuration");
		fault_count[WATCHDOG_FAULT_OFF]++;
	}

	if (val & BOOST_FAULT_MASK) {
		fault_count[BOOST_FAULT_OFF]++;
		dev_info(&chip->client->dev, "Boost fault\n");
	}
	if (val & BAT_FAULT_MASK) {
		fault_count[BAT_FAULT_OFF]++;
		dev_info(&chip->client->dev, "Battery fault\n");
	}

	if (val & NTC_FAULT_MASK) {
		fault_count[NTC_FAULT_OFF+(val & NTC_FAULT_MASK)]++;
		dev_info(&chip->client->dev, "NTC fault: %s\n", NTC_fault_to_human[val & NTC_FAULT_MASK]);
	}

	if (val & CHARGER_FAULT_MASK) {
		fault_count[CHARGER_FAULT_OFF+((val & CHARGER_FAULT_MASK) >> 4)]++;
		dev_info(&chip->client->dev, "Charger fault: %s\n", CHARGER_fault_to_human[(val & CHARGER_FAULT_MASK) >> 4]);
	}


	return ret;
}

static void bq25898_handle_charging_worker(struct work_struct *work)
{
	int ret;
	struct bq25898_charger *chip = container_of(work, struct bq25898_charger,
						charge_status_work);

	if (!chip)
		return;

	dev_info(&chip->client->dev, "Received charger interrupt\n");

	ret = bq25898_charger_status_reg_handler(chip);
	if (ret < 0)
		dev_err(&chip->client->dev, "Error while checking status_reg\n");
	ret = bq25898_charger_fault_reg_handler(chip);
	if (ret < 0)
		dev_err(&chip->client->dev, "Error while checking fault_reg\n");

	return;
}

static int bq25898_notify_charge_status_change(struct notifier_block *self,
					unsigned long action, void *param)
{
	struct bq25898_charger *chip = container_of(self, struct bq25898_charger,
						pmic_notifier);

	if (!chip)
		return NOTIFY_DONE;

	dev_info(&chip->client->dev, "Received PMIC notification action:%lu\n", action);

	switch (action)	{
	case PMIC_ACTION_CHARGING_STATUS:
		queue_work(system_nrt_wq, &chip->charge_status_work);
		return NOTIFY_OK;
	case PMIC_ACTION_BATTERY_ZONE_CHANGED:
	case PMIC_ACTION_OVERHEAT:
	default:
		return NOTIFY_DONE;
	}
}

static int bq25898_notify_reboot(struct notifier_block *self,
					unsigned long action, void *param)
{
	int ret = 0;
	struct bq25898_charger *chip = container_of(self, struct bq25898_charger,
						reboot_notifier);

	if (!chip)
		return NOTIFY_BAD;

	if (chip->ship_mode_scheduled) {
		ret = enable_ship_mode(chip->client);
		if (ret < 0)
			dev_err(&chip->client->dev, "Error enabling ship_mode\n");
	}

	return NOTIFY_DONE;
}

static void bq25898_sw_config_worker(struct work_struct *work)
{
	int ret = 0;
	struct bq25898_charger *chip = container_of(work, struct bq25898_charger,
						sw_config_work);

	if (!chip)
		return;

	mutex_lock(&chip->charge_config_lock);
	ret = bq25898_enable_charging(chip->client);
	mutex_unlock(&chip->charge_config_lock);
	dev_info(&chip->client->dev, "charging enabled: 0x%x\n", ret);

	if (chip->watchdog_state != WDT_DISABLED) {
		dev_info(&chip->client->dev, "disabling watchdog: 0x%02x\n", ret);
		ret = bq25898_wdt_configure(chip->client, BQ25898_WDT_TIMER_DISABLE);
		if (ret < 0)
			dev_err(&chip->client->dev, "error disabling watchdog %d\n", ret);
		else
			chip->watchdog_state = WDT_DISABLED;
	}
}

static void bq25898_sw_charge_term_worker(struct work_struct *work)
{
	int ret = 0;
	struct bq25898_charger *chip = container_of(work, struct bq25898_charger,
						sw_term_work.work);

	if (!chip)
		return;

	dev_info(&chip->client->dev, "Postcharging phase started at : %lu, current_time : %lu, current_now value %d",
		chip->postcharge_start_time_sec, CURRENT_TIME.tv_sec, chip->current_now);
	if ((chip->current_now < chip->curr_eoc_limit) ||
		(((CURRENT_TIME.tv_sec - chip->postcharge_start_time_sec) / 60) >= chip->postcharge_duration_mn)) {
		/* Mark battery as full */
		chip->is_charge_complete = true;

		dev_info(&chip->client->dev, "Enabling maintenance mode\n");
		/* Disable charging then enable charging and charging termination in order to activate
		 * maintenance mode. Charging will stop immediately */
		mutex_lock(&chip->charge_config_lock);
		ret = bq25898_enable_charging(chip->client);
		mutex_unlock(&chip->charge_config_lock);
		if (ret < 0) {
			dev_err(&chip->client->dev, "error enabling maintenance mode\n");
			return;
		}
	} else {
		schedule_delayed_work(&chip->sw_term_work, chip->curr_check_interval);
	}
}

static void bq25898_sw_batmon_worker(struct work_struct *work)
{
	struct bq25898_charger *chip = container_of(work, struct bq25898_charger,
						batmon_work.work);
	int ret;

	if (!chip)
		return;

	dev_info(&chip->client->dev, "running battery monitor\n");

	/* ensure watchdog is enabled */
	if (chip->watchdog_state != WDT_ENABLED) {
		ret = bq25898_wdt_configure(chip->client, BQ25898_WDT_TIMER_160S);
		if (ret < 0)
			dev_err(&chip->client->dev, "error enabling watchdog %d\n", ret);
		else
			chip->watchdog_state = WDT_ENABLED;
	}

	/* kick watchdog */
	mutex_lock(&chip->sysfs_lock);
	ret = bq25898_wdt_kick(chip);
	mutex_unlock(&chip->sysfs_lock);
	if (ret < 0)
		dev_err(&chip->client->dev, "failure to kick watchdog: %d\n", ret);
	else
		dev_info(&chip->client->dev, "watchdog kicked\n");


	/*
	 * This is the current battery profile handling
	 * done by the bq25898 (HW handling)
	 * Temperature:
	 * below  0 deg : stop charging
	 *  0 to 10 deg : charging at 20% ICHG  (64mA) and VREG       (4.352V)
	 * 10 to 45 deg : charging at     ICHG (320mA) and VREG       (4.352V)
	 * 45 to 60 deg : charging at     ICHG (320mA) and VREG-200mV (4.152V)
	 * above 60 deg : stop charging
	 */

	/* reschedule ourself */
	schedule_delayed_work(&chip->batmon_work, BQ25898_BAT_MONITOR_DELAY);
	return;
}

static int bq25898_suspend(struct device *dev)
{
	struct bq25898_charger *chip;

	chip = dev_get_drvdata(dev);
	if (chip->irq && !chip->pdata->is_pmic_notifier) {
		disable_irq(chip->irq);
		enable_irq_wake(chip->irq);
	}
	dev_info(&chip->client->dev, "suspend\n");
	return 0;
}

static int bq25898_resume(struct device *dev)
{
	struct bq25898_charger *chip;

	chip = dev_get_drvdata(dev);

	if (chip->irq && !chip->pdata->is_pmic_notifier) {
		disable_irq_wake(chip->irq);
		enable_irq(chip->irq);
	}
	dev_info(&chip->client->dev, "%s\n", __func__);
	return 0;
}

static int bq25898_runtime_suspend(struct device *dev)
{
	struct bq25898_charger *chip;

	chip = dev_get_drvdata(dev);

	dev_dbg(&chip->client->dev, "%s\n", __func__);
	return 0;
}

static int bq25898_runtime_resume(struct device *dev)
{
	struct bq25898_charger *chip;

	chip = dev_get_drvdata(dev);

	dev_dbg(&chip->client->dev, "%s\n", __func__);
	return 0;
}

static int bq25898_runtime_idle(struct device *dev)
{
	struct bq25898_charger *chip;

	chip = dev_get_drvdata(dev);

	dev_dbg(&chip->client->dev, "%s\n", __func__);
	return 0;
}

static int register_otg_notification(struct bq25898_charger *chip)
{
	int retval;

	chip->otg_usb_change.notifier_call = bq25898_usb_change_notifier;
	chip->otg_usb_change.priority = 1;

	/*
	 * Get the USB transceiver instance
	 */
	chip->transceiver = usb_get_phy(USB_PHY_TYPE_USB2);
	if (chip->transceiver < 0) {
		dev_err(&chip->client->dev, "failed to get the USB transceiver\n");
		return -ENODEV;
	}

	retval = usb_register_notifier(chip->transceiver, &chip->otg_usb_change);
	if (retval) {
		dev_err(&chip->client->dev, "failed to register otg notifier:%d\n",
			retval);
		return -EINVAL;
	}

	return 0;
}

static int register_pmic_notification(struct bq25898_charger *chip)
{
	int ret;

	chip->pmic_notifier.notifier_call = bq25898_notify_charge_status_change;

	ret = register_pmic_notifier(&chip->pmic_notifier);
	if (ret) {
		dev_err(&chip->client->dev, "failed to register pmic notifier:%d\n",
			ret);
		return -EINVAL;
	}

	return 0;
}

static int register_reboot_notification(struct bq25898_charger *chip)
{
	chip->reboot_notifier.notifier_call = bq25898_notify_reboot;
	return register_reboot_notifier(&chip->reboot_notifier);
}

static int bq25898_get_prop_health(struct bq25898_charger *chip)
{
	int val;

	if (!chip)
		return POWER_SUPPLY_HEALTH_UNKNOWN;

	/* Read this register two times to get the current value
	 * as specified by the vendor */
	val = bq25898_read_reg(chip->client, BQ25898_FAULT_REG);
	if (val < 0)
		return val;
	val = bq25898_read_reg(chip->client, BQ25898_FAULT_REG);
	if (val < 0)
		return val;

	/*No fault*/
	if (!val)
		return POWER_SUPPLY_HEALTH_GOOD;

	/*Watchdog fault*/
	if (val & WATCHDOG_FAULT_MASK)
		return POWER_SUPPLY_HEALTH_WATCHDOG_TIMER_EXPIRE;

	/*Charger fault*/
	if (val & CHARGER_FAULT_MASK) {
		if (((val & CHARGER_FAULT_MASK) >> 4) == CHARGER_FAULT_OVERHEAT)
			return POWER_SUPPLY_HEALTH_OVERHEAT;
		if (((val & CHARGER_FAULT_MASK) >> 4) == CHARGER_FAULT_SAFETY_TIMER_EXPIRE)
			return POWER_SUPPLY_HEALTH_SAFETY_TIMER_EXPIRE;
	}

	/*Battery fault*/
	if (val & BAT_FAULT_MASK)
		return POWER_SUPPLY_HEALTH_OVERVOLTAGE;

	/*Thermal fault*/
	if (val & NTC_FAULT_MASK) {
		if ((val & NTC_FAULT_MASK) == NTC_FAULT_COLD)
			return POWER_SUPPLY_HEALTH_COLD;
		if ((val & NTC_FAULT_MASK) == NTC_FAULT_OVERHEAT)
			return POWER_SUPPLY_HEALTH_OVERHEAT;
	}

	return POWER_SUPPLY_HEALTH_UNKNOWN;
}

static int bq25898_get_prop_status(struct bq25898_charger *chip)
{
	int val;

	if (!chip)
		return POWER_SUPPLY_STATUS_UNKNOWN;

	val = bq25898_read_reg(chip->client, BQ25898_STATUS_REG);
	if (val < 0)
		return val;

	/* Pre-charge or fast charge */
	if ((!(val & CHARGER_STATUS1) && (val & CHARGER_STATUS0)) ||
	    ((val & CHARGER_STATUS1) && !(val & CHARGER_STATUS0)))
		return POWER_SUPPLY_STATUS_CHARGING;
	/* Charge termination done */
	else if ((val & CHARGER_STATUS1) && (val & CHARGER_STATUS0))
		return POWER_SUPPLY_STATUS_FULL;
	/* Discharging or Not charging */
	else if (!(val & CHARGER_STATUS1) && !(val & CHARGER_STATUS0)) {
		if (!(val & PG_STAT))
			return POWER_SUPPLY_STATUS_DISCHARGING;
		else
			return POWER_SUPPLY_STATUS_NOT_CHARGING;
	}

	return POWER_SUPPLY_STATUS_UNKNOWN;
}

static int bq25898_get_prop_online(struct bq25898_charger *chip)
{
	int val;

	val = bq25898_read_reg(chip->client, BQ25898_STATUS_REG);
	if (val < 0)
		return val;

	if (val & PG_STAT)
		return 1;
	else
		return 0;
}

/* Launch an ADC conversion and wait for its completion */
static int bq25898_adc_convert(struct i2c_client *client)
{
	int ret;
	int t;

	/* Start one-shot adc conversion */
	ret = bq25898_read_modify_reg(client, BQ25898_ADC_CTRL_REG,
				      ADC_CONV_START, ADC_CONV_START);
	if (ret < 0) {
		dev_err(&client->dev,
			"ADC start failed: %d\n", ret);
		return ret;
	}

	/* Conversion takes usually 80ms */
	for (t = 0; t < ADC_CONVERSION_TIMEOUT_MS; t += TIME_SLEEP_MS) {
		if (bq25898_read_reg(client, BQ25898_ADC_CTRL_REG) & ADC_CONV_START)
			msleep(TIME_SLEEP_MS);
		else
			return 0;
	}

	dev_err(&client->dev, "ADC conversion timed out\n");
	return -EIO;
}

static int bq25898_get_prop_voltage_now(struct bq25898_charger *chip)
{
	int val;

	val = bq25898_adc_convert(chip->client);
	if (val < 0)
		return val;

	val = bq25898_read_reg(chip->client, BQ25898_BAT_VOLT_REG);
	if (val < 0)
		return val;

	return bq25898_rege_convert_uv(val);
}

static int bq25898_get_prop_temp(struct bq25898_charger *chip, int *temp)
{
	int val;

	val = bq25898_adc_convert(chip->client);
	if (val < 0)
		return val;

	val = bq25898_read_reg(chip->client, BQ25898_ADC_TSPCT_REG);
	if (val < 0)
		return val;

	/* 7-bit ADC */
	if (val >= 128)
		return -ERANGE;

	*temp = bq25898_reg10_convert_c(val);

	return 0;
}

static int bq25898_get_property(struct power_supply *psy,
				enum power_supply_property psp,
				union power_supply_propval *val)
{
	int ret;
	struct bq25898_charger *chip = container_of(psy,
						struct bq25898_charger,
						psy_usb);

	if (!val || !chip) {
		dev_err(&chip->client->dev, "%s power_supply_propval:%p, chip: %p\n", __func__, val, chip);
		return -EINVAL;
	}

	mutex_lock(&chip->sysfs_lock);

	switch (psp) {
	case POWER_SUPPLY_PROP_STATUS:
		ret = bq25898_get_prop_status(chip);
		if (ret < 0)
			goto error;

		val->intval = ret;
		dev_dbg(&chip->client->dev, "%s prop status:%d\n", __func__, val->intval);
		break;
	case POWER_SUPPLY_PROP_HEALTH:
		ret = bq25898_get_prop_health(chip);
		if (ret < 0)
			goto error;

		val->intval = ret;
		dev_dbg(&chip->client->dev, "%s health:%d\n", __func__, val->intval);
		break;
	case POWER_SUPPLY_PROP_ONLINE:
		ret = bq25898_get_prop_online(chip);
		if (ret < 0)
			goto error;

		val->intval = ret;
		dev_dbg(&chip->client->dev, "%s online:%d\n", __func__, val->intval);
		break;
	case POWER_SUPPLY_PROP_PRESENT:
		val->intval = 1;
		break;
	case POWER_SUPPLY_PROP_TEMP:
		ret = bq25898_get_prop_online(chip);
		if (ret < 0)
			goto error;

		/* USB must be connected for the temperature to be measured */
		if (ret == 0) {
			dev_dbg(&chip->client->dev, "%s temperature:(not available)\n", __func__);
			ret = -ENODATA;
			goto error;
		}
		ret = bq25898_get_prop_temp(chip, &val->intval);
		if (ret < 0)
			goto error;

		dev_dbg(&chip->client->dev, "%s temperature:%d\n", __func__, val->intval);
		break;
	case POWER_SUPPLY_PROP_TECHNOLOGY:
		val->intval = POWER_SUPPLY_TECHNOLOGY_LION;
		break;
	case POWER_SUPPLY_PROP_CURRENT_NOW:
		val->intval = chip->current_now;
		break;
	case POWER_SUPPLY_PROP_VOLTAGE_NOW:
		ret = bq25898_get_prop_voltage_now(chip);
		if (ret < 0)
			goto error;

		val->intval = ret;
		dev_dbg(&chip->client->dev, "%s voltage_now:%d\n", __func__, val->intval);
		break;
	default:
		break;
	}


error:
	mutex_unlock(&chip->sysfs_lock);

	return ( ret < 0 ? ret : 0 );
}

static int bq25898_set_property(struct power_supply *psy,
				enum power_supply_property psp,
				const union power_supply_propval *val)
{
	struct bq25898_charger *chip = container_of(psy,
						struct bq25898_charger,
						psy_usb);

	if (!val || !chip)
		return -EINVAL;

	mutex_lock(&chip->sysfs_lock);

	switch (psp) {
	case POWER_SUPPLY_PROP_CURRENT_NOW:
		dev_info(&chip->client->dev, "%s prop current:%d\n", __func__, val->intval);
		chip->current_now = val->intval;
		break;
	default:
		break;
	}
	mutex_unlock(&chip->sysfs_lock);

	return 0;
}

static int bq25898_property_is_writeable(struct power_supply *psy,
					enum power_supply_property psp)
{
	switch (psp) {
	case POWER_SUPPLY_PROP_CURRENT_NOW:	/* provided by Healthd from Fuel Gauge */
		return 1;
	default:
		return -EPERM;
	}
	return 0;
}

static int bq25898_force_charging(struct bq25898_charger *chip)
{
	int ret;

	dev_info(&chip->client->dev, "Charge terminated, disabling charging\n");
	/* disable charging */
	ret = bq25898_read_modify_reg(chip->client, BQ25898_CHARGE_CTRL_REG,
				CHG_CONFIG, 0);
	if (ret < 0) {
		dev_err(&chip->client->dev, "error disabling charging %d\n", ret);
		return ret;
	}

	dev_info(&chip->client->dev, "disabling charge termination\n");
	/* disable charge termination */
	ret = bq25898_read_modify_reg(chip->client, BQ25898_TERM_WDT_SFTY_CTRL_REG,
				CHARGE_TERM_ENABLE, 0);
	if (ret < 0) {
		dev_err(&chip->client->dev, "error disabling charge termination %d\n", ret);
		return ret;
	}

	dev_info(&chip->client->dev, "enabling charging\n");
	/* enable charging */
	ret = bq25898_read_modify_reg(chip->client, BQ25898_CHARGE_CTRL_REG,
				CHG_CONFIG, 0x10);
	if (ret < 0) {
		dev_err(&chip->client->dev, "error enabling charging %d\n", ret);
		return ret;
	}

	dev_info(&chip->client->dev, "charge restarted for a maximum of %d mn\n", chip->postcharge_duration_mn);

	/* the sw_term_work will disable the forced charging when "chip->current_now" < BQ25898_CURR_TERM_LIMIT
	 * or when "chip->postcharge_duration" minutes has been elapsed */
	cancel_work_sync(&chip->sw_config_work);
	chip->postcharge_start_time_sec = CURRENT_TIME.tv_sec;
	schedule_delayed_work(&chip->sw_term_work, chip->curr_check_interval);

	return 0;
}

static irqreturn_t bq25898_handler(int irq, void *data)
{
	struct bq25898_charger *chip = (struct bq25898_charger *)data;

	dev_info(&chip->client->dev, "%s", __func__);

	return IRQ_WAKE_THREAD;
}

static irqreturn_t bq25898_thread_handler(int id, void *data)
{
	struct bq25898_charger *chip = (struct bq25898_charger *)data;
	struct i2c_client *client = chip->client;
	int ret;

	if ((!chip) || (!client))
		return IRQ_NONE;

	dev_info(&client->dev, "%s", __func__);
	dev_info(&client->dev, "Received charger interrupt\n");

	ret = bq25898_charger_status_reg_handler(chip);
	if (ret < 0) {
		dev_err(&chip->client->dev, "Error while checking status_reg\n");
		goto thread_handler_end;
	}

	ret = bq25898_charger_fault_reg_handler(chip);
	if (ret < 0) {
		dev_err(&chip->client->dev, "Error while checking fault_reg\n");
		goto thread_handler_end;
	}
	power_supply_changed(&chip->psy_usb);

thread_handler_end:
	return IRQ_HANDLED;
}

static int bq25898_probe(struct i2c_client *client,
			 const struct i2c_device_id *id)
{
	struct i2c_adapter *adapter;
	struct bq25898_charger *chip;
	int ret, irq;

	dev_info(&client->dev, ">probe");

	adapter = to_i2c_adapter(client->dev.parent);

	if (!client->dev.platform_data) {
		dev_err(&client->dev, "platform data is null\n");
		return -EFAULT;
	}

	if (!i2c_check_functionality(adapter, I2C_FUNC_SMBUS_BYTE_DATA)) {
		dev_err(&client->dev,
			"I2C adapter %s doesn't support SMBUS_BYTE_DATA\n",
			adapter->name);
		return -EIO;
	}

	chip = devm_kzalloc(&client->dev, sizeof(*chip), GFP_KERNEL);
	if (!chip) {
		dev_err(&client->dev, "mem alloc failed\n");
		return -ENOMEM;
	}

	chip->client = client;
	chip->pdata = client->dev.platform_data;
	chip->psy_usb.name = DEV_NAME;
	chip->psy_usb.type = POWER_SUPPLY_TYPE_USB;
	chip->psy_usb.properties = bq25898_battery_properties;
	chip->psy_usb.num_properties = ARRAY_SIZE(bq25898_battery_properties);
	chip->psy_usb.set_property = bq25898_set_property;
	chip->psy_usb.get_property = bq25898_get_property;
	chip->psy_usb.property_is_writeable = bq25898_property_is_writeable;
	chip->postcharge_duration_mn = BQ25898_POSTCHARGE_DEFAULT_DURATION_MN;
	chip->current_now = 0;
	chip->irq_counter = 0;
	chip->ship_mode_scheduled = false;
	chip->is_charge_complete = false;
	chip->watchdog_state = WDT_DISABLED;
	chip->postcharge_start_time_sec = 0;
	chip->curr_check_interval = BQ25898_CURR_CHECK_INTERVAL_DEFAULT;
	chip->curr_eoc_limit = BQ25898_CURR_EOC_LIMIT_DEFAULT;
	chip->status_reg_oldvalue = BQ25898_STATUS_REG_DEFAULT;

	strncpy(chip->model_name,
		MODEL_NAME,
		MODEL_NAME_SIZE);
	strncpy(chip->manufacturer, DEV_MANUFACTURER,
		DEV_MANUFACTURER_NAME_SIZE);

	i2c_set_clientdata(client, chip);

	pm_runtime_enable(&client->dev);

	ret = chip->revision = bq25898_read_reg(client, BQ25898_DEVREG_CTRL_REG);
	if (ret < 0) {
		dev_err(&client->dev,
			"error in reading ctrl reg %02x:%d\n", BQ25898_DEVREG_CTRL_REG, ret);
		goto error0;
	}

	/* Disable watchdog */
	ret = bq25898_wdt_configure(client, BQ25898_WDT_TIMER_DISABLE);
	dev_info(&client->dev, "disabling watchdog: 0x%02x\n", ret);
	if (ret < 0) {
		dev_err(&client->dev, "error disabling watchdog %d\n", ret);
		goto error0;
	}

	mutex_init(&chip->stat_lock);
	mutex_init(&chip->sysfs_lock);
	mutex_init(&chip->ship_mode_lock);
	mutex_init(&chip->charge_config_lock);

	INIT_DELAYED_WORK(&chip->sw_term_work, bq25898_sw_charge_term_worker);
	/* monitor battery status and react accordingly */
	INIT_DELAYED_WORK(&chip->batmon_work, bq25898_sw_batmon_worker);
	INIT_WORK(&chip->sw_config_work, bq25898_sw_config_worker);
	INIT_WORK(&chip->charge_status_work, bq25898_handle_charging_worker);

	/* default configuration */
	ret = bq25898_charger_configure(client);
	if (ret < 0) {
		dev_err(&client->dev, "error configuring charger: %d\n", ret);
		goto error0;
	}

#ifdef CONFIG_SYSFS
	/* create sysfs attribute group */
	ret = sysfs_create_group(&client->dev.kobj, &bq25898_attribute_group);
	if (ret < 0) {
		dev_err(&client->dev, "cannot create sysfs attribute group\n");
		goto error0;
	}
#endif /* !CONFIG_SYSFS */

	bq25898_debugfs_init(chip);

	/* Wakelock init */
	dev_info(&client->dev, "init bq25898 wakelock\n");
	wake_lock_init(&(chip->charger_wlock), WAKE_LOCK_SUSPEND, "bq25898_wakelock");

	/* register for usb change */
	ret = register_otg_notification(chip);
	if (ret < 0) {
		dev_err(&client->dev, "error registering to OTG notification: %d\n", ret);
		goto error1;
	}

	/* register for reboot notification */
	ret = register_reboot_notification(chip);
	if (ret < 0) {
		dev_err(&client->dev, "error registering to REBOOT notification: %d\n", ret);
		goto error2;
	}

	ret = power_supply_register(&chip->client->dev, &chip->psy_usb);
	if (ret < 0) {
		dev_err(&client->dev, "error registering power supply: %d\n", ret);
		goto error3;
	}

	if (!chip->pdata->is_pmic_notifier) {
		if (!gpio_is_valid(chip->pdata->gpio_charger_int_n)) {
			dev_err(&client->dev, "Invalid gpio gpio_charger_int_n pin\n");
			ret = -EINVAL;
			goto error4;
		}
		ret = gpio_request(chip->pdata->gpio_charger_int_n, DEV_NAME);
		if (ret) {
			dev_err(&client->dev, "Failed to request gpio pin gpio_charger_int_n: %d\n", ret);
			goto error4;
		}
		ret = gpio_direction_input(chip->pdata->gpio_charger_int_n);
		if (ret) {
			dev_err(&client->dev, "Failed to set gpio gpio_charger_int_n to input: %d\n", ret);
			goto error5;
		}

		irq = gpio_to_irq(chip->pdata->gpio_charger_int_n);
		if (irq < 0) {
			dev_err(&client->dev, "gpio_to_irq fails: %d\n", ret);
			goto error5;
		}
		ret = request_threaded_irq(irq, bq25898_handler,
			bq25898_thread_handler, IRQF_SHARED | IRQF_TRIGGER_FALLING,
			DEV_NAME, chip);
		if (ret < 0) {
			dev_err(&client->dev, "Failed to request irq: %d\n", ret);
			goto error5;
		}
		chip->irq = irq;
	} else {
		/* register for pmic notifications */
		ret = register_pmic_notification(chip);
		if (ret < 0) {
			dev_err(&client->dev, "error registering to PMIC notification: %d\n", ret);
			goto error4;
		}
	}

	dev_info(&client->dev, "<probe");

	return ret;

error5:
	if (!chip->pdata->is_pmic_notifier)
		gpio_free(chip->pdata->gpio_charger_int_n);
error4:
	power_supply_unregister(&chip->psy_usb);
error3:
	unregister_reboot_notifier(&chip->reboot_notifier);
error2:
	usb_unregister_notifier(chip->transceiver, &chip->otg_usb_change);
error1:
	bq25898_debugfs_exit(chip);
#ifdef CONFIG_SYSFS
	sysfs_remove_group(&client->dev.kobj, &bq25898_attribute_group);
#endif /* !CONFIG_SYSFS */
error0:
	pm_runtime_disable(&client->dev);

	return ret;
}

static int bq25898_remove(struct i2c_client *client)
{
	struct bq25898_charger *chip;

	if (!client)
		return -EINVAL;

	chip = i2c_get_clientdata(client);

	if (!chip)
		return -EINVAL;

	flush_scheduled_work();
	flush_work(&chip->sw_config_work);
	flush_work(&chip->charge_status_work);

	if (chip->transceiver)
		usb_unregister_notifier(chip->transceiver, &chip->otg_usb_change);

	unregister_reboot_notifier(&chip->reboot_notifier);
	power_supply_unregister(&chip->psy_usb);

	bq25898_debugfs_exit(chip);

#ifdef CONFIG_SYSFS
	sysfs_remove_group(&client->dev.kobj, &bq25898_attribute_group);
#endif /* !CONFIG_SYSFS */

	pm_runtime_disable(&client->dev);
	if (chip->pdata->is_pmic_notifier) {
		unregister_pmic_notifier(&chip->pmic_notifier);
	} else if (chip->irq) {
		free_irq(chip->irq, chip);
		gpio_free(chip->pdata->gpio_charger_int_n);
	}

	return 0;
}

static const struct dev_pm_ops bq25898_pm_ops = {
	.suspend = bq25898_suspend,
	.resume = bq25898_resume,
	.runtime_suspend = bq25898_runtime_suspend,
	.runtime_resume = bq25898_runtime_resume,
	.runtime_idle = bq25898_runtime_idle,
};

static const struct i2c_device_id bq25898_id[] = {
	{DEV_NAME, 0},
	{},
};

MODULE_DEVICE_TABLE(i2c, bq25898_id);

static struct i2c_driver bq25898_driver = {
	.driver = {
		   .name = DEV_NAME,
		   .pm = &bq25898_pm_ops,
		   },
	.probe = bq25898_probe,
	.remove = bq25898_remove,
	.id_table = bq25898_id,
};

module_i2c_driver(bq25898_driver);

MODULE_AUTHOR("Kamel Slimani <kamel.slimani@intel.com>");
MODULE_AUTHOR("Marc Blassin <marc.blassin@intel.com>");
MODULE_DESCRIPTION("BQ25898 Charger Driver");

module_param(ship_mode_delay_10s, bool, 0);
MODULE_PARM_DESC(ship_mode_delay_10s,
                 "Set to 0 if an immediate poweroff of the system is \
                 needed during reboot when ship_mode is enabled, default "
                 __MODULE_STRING(DEFAULT_SHIP_MODE_DELAY_10S));
MODULE_LICENSE("GPL");