summaryrefslogtreecommitdiff
path: root/target/linux/omap24xx/patches-3.1/900-n810-battery-management.patch
blob: 3b1f296d4b99781fbf188c6568b0d9c240748f82 (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
Index: linux-3.1/drivers/cbus/Kconfig
===================================================================
--- linux-3.1.orig/drivers/cbus/Kconfig	2011-11-05 17:03:39.578846146 +0100
+++ linux-3.1/drivers/cbus/Kconfig	2011-11-05 17:06:51.589348749 +0100
@@ -83,4 +83,12 @@ config CBUS_RETU_HEADSET
 
 endif # CBUS_RETU
 
+config N810BM
+	depends on CBUS_RETU && CBUS_TAHVO
+	tristate "Nokia n810 battery management"
+	---help---
+	  Nokia n810 device battery management.
+
+	  If unsure, say N.
+
 endmenu
Index: linux-3.1/drivers/cbus/Makefile
===================================================================
--- linux-3.1.orig/drivers/cbus/Makefile	2011-11-05 17:03:39.618846091 +0100
+++ linux-3.1/drivers/cbus/Makefile	2011-11-05 17:06:51.589348749 +0100
@@ -11,3 +11,6 @@ obj-$(CONFIG_CBUS_RETU_POWERBUTTON) += r
 obj-$(CONFIG_CBUS_RETU_RTC)	+= retu-rtc.o
 obj-$(CONFIG_CBUS_RETU_WDT)	+= retu-wdt.o
 obj-$(CONFIG_CBUS_RETU_HEADSET)	+= retu-headset.o
+n810bm-y			+= n810bm_main.o
+n810bm-y			+= lipocharge.o
+obj-$(CONFIG_N810BM)		+= n810bm.o
Index: linux-3.1/drivers/cbus/n810bm_main.c
===================================================================
--- /dev/null	1970-01-01 00:00:00.000000000 +0000
+++ linux-3.1/drivers/cbus/n810bm_main.c	2011-11-05 17:06:51.589348749 +0100
@@ -0,0 +1,1572 @@
+/*
+ *   Nokia n810 battery management
+ *
+ *   WARNING: This driver is based on unconfirmed documentation.
+ *            It is possibly dangerous to use this software.
+ *            Use this software at your own risk!
+ *
+ *   Copyright (c) 2010-2011 Michael Buesch <mb@bu3sch.de>
+ *
+ *   This program is free software; you can redistribute it and/or
+ *   modify it under the terms of the GNU General Public License
+ *   as published by the Free Software Foundation; either version 2
+ *   of the License, or (at your option) any later version.
+ *
+ *   This program is distributed in the hope that it will be useful,
+ *   but WITHOUT ANY WARRANTY; without even the implied warranty of
+ *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+ *   GNU General Public License for more details.
+ */
+
+#define DEBUG
+
+#include <linux/module.h>
+#include <linux/device.h>
+#include <linux/platform_device.h>
+#include <linux/slab.h>
+#include <linux/mutex.h>
+#include <linux/timer.h>
+#include <linux/firmware.h>
+#include <linux/bitops.h>
+#include <linux/workqueue.h>
+#include <linux/delay.h>
+#include <linux/interrupt.h>
+
+#include "cbus.h"
+#include "retu.h"
+#include "tahvo.h"
+#include "lipocharge.h"
+
+
+#define N810BM_PMM_BLOCK_FILENAME	"n810-cal-bme-pmm.fw"
+#define N810BM_PMM_BLOCK_SIZE		0x600
+#define N810BM_PMM_GROUP_SIZE		0x200
+#define N810BM_PMM_ELEM_SIZE		0x10
+
+#define N810BM_CHECK_INTERVAL		(HZ * 2)
+#define N810BM_MIN_VOLTAGE_THRES	3200 /* Absolute minimum voltage threshold */
+
+
+/* RETU_ADC_BSI
+ * The battery size indicator ADC measures the resistance between
+ * the battery BSI pin and ground. This is used to detect the battery
+ * capacity, as the BSI resistor is related to capacity.
+ *
+ * Manually measured lookup table.
+ * Hard to measure, thus not very accurate.
+ *
+ * Resistance  |  ADC value
+ * ========================
+ * 120k        |  0x3AC
+ * 110k        |  0x37C
+ * 100k        |  0x351
+ *  90k        |  0x329
+ */
+
+/* RETU_ADC_BATTVOLT
+ * Manually measured lookup table.
+ * Hard to measure, thus not very accurate.
+ *
+ * Voltage  |  ADC value
+ * =====================
+ * 2.80V    |  0x037
+ * 2.90V    |  0x05E
+ * 3.00V    |  0x090
+ * 3.10V    |  0x0A4
+ * 3.20V    |  0x0CC
+ * 3.30V    |  0x0EF
+ * 3.40V    |  0x115
+ * 3.50V    |  0x136
+ * 3.60V    |  0x15C
+ * 3.70V    |  0x187
+ * 3.80V    |  0x1A5
+ * 3.90V    |  0x1C9
+ * 4.00V    |  0x1ED
+ * 4.10V    |  0x212
+ * 4.20V    |  0x236
+ */
+
+
+/* PMM block ADC IDs */
+enum n810bm_pmm_adc_id {
+	N810BM_PMM_ADC_BATVOLT		= 0x01,	/* Battery voltage */
+	N810BM_PMM_ADC_CHGVOLT		= 0x02,	/* Charger voltage */
+	N810BM_PMM_ADC_GND2		= 0x03,	/* Ground 0V */
+	N810BM_PMM_ADC_BSI		= 0x04,	/* Battery size indicator */
+	N810BM_PMM_ADC_BATTEMP		= 0x05,	/* Battery temperature */
+	N810BM_PMM_ADC_HEADSET		= 0x06,	/* Headset detection */
+	N810BM_PMM_ADC_HOOKDET		= 0x07,	/* Hook detection */
+	N810BM_PMM_ADC_LIGHTSENS	= 0x08,	/* Light sensor */
+	N810BM_PMM_ADC_BATCURR		= 0x0E,	/* Battery current */
+	N810BM_PMM_ADC_BKUPVOLT		= 0x13,	/* Backup battery voltage */
+	N810BM_PMM_ADC_LIGHTTEMP	= 0x14,	/* Light sensor temperature */
+	N810BM_PMM_ADC_RFGP		= 0x15,	/* RF GP */
+	N810BM_PMM_ADC_WBTX		= 0x16,	/* Wideband TX detection */
+	N810BM_PMM_ADC_RETUTEMP		= 0x17,	/* RETU chip temperature */
+	N810BM_PMM_ADC_0xFE		= 0xFE,
+};
+
+struct n810bm_adc_calib {
+	enum n810bm_pmm_adc_id id;
+	u8 flags;
+	u8 adc_groupnr;
+	u32 field1;
+	u32 field2;
+	u16 field3;
+	u16 field4;
+};
+
+struct n810bm_calib {
+	struct n810bm_adc_calib adc[25];
+};
+
+enum n810bm_capacity {
+	N810BM_CAP_UNKNOWN	= -1,
+	N810BM_CAP_NONE		= 0,
+	N810BM_CAP_1500MAH	= 1500,	/* 1500 mAh battery */
+};
+
+enum n810bm_notify_flags {
+	N810BM_NOTIFY_charger_present,
+	N810BM_NOTIFY_charger_state,
+	N810BM_NOTIFY_charger_pwm,
+};
+
+struct n810bm {
+	int tahvo_irq;
+	bool tahvo_irq_enabled;
+
+	bool battery_present;			/* A battery is inserted */
+	bool charger_present;			/* The charger is connected */
+	enum n810bm_capacity capacity;		/* The capacity of the inserted battery (if any) */
+
+	bool charger_enabled;			/* Want to charge? */
+	struct lipocharge charger;		/* Charger subsystem */
+	unsigned int active_current_pwm;	/* Active value of TAHVO_REG_CHGCURR */
+	int current_measure_enabled;		/* Current measure enable refcount */
+
+	struct platform_device *pdev;
+	struct n810bm_calib calib;		/* Calibration data */
+
+	bool verbose_charge_log;		/* Verbose charge logging */
+
+	unsigned long notify_flags;
+	struct work_struct notify_work;
+	struct delayed_work periodic_check_work;
+
+	bool initialized;			/* The hardware was initialized */
+	struct mutex mutex;
+};
+
+static void n810bm_notify_charger_present(struct n810bm *bm);
+static void n810bm_notify_charger_state(struct n810bm *bm);
+static void n810bm_notify_charger_pwm(struct n810bm *bm);
+
+
+static struct platform_device *n810bm_retu_device;
+static struct platform_device *n810bm_tahvo_device;
+
+
+static inline struct n810bm * device_to_n810bm(struct device *dev)
+{
+	struct platform_device *pdev = to_platform_device(dev);
+	struct n810bm *bm = platform_get_drvdata(pdev);
+
+	return bm;
+}
+
+static inline bool n810bm_known_battery_present(struct n810bm *bm)
+{
+	return bm->battery_present &&
+	       bm->capacity != N810BM_CAP_UNKNOWN &&
+	       bm->capacity != N810BM_CAP_NONE;
+}
+
+static NORET_TYPE void n810bm_emergency(struct n810bm *bm, const char *message) ATTRIB_NORET;
+static void n810bm_emergency(struct n810bm *bm, const char *message)
+{
+	printk(KERN_EMERG "n810 battery management fatal fault: %s\n", message);
+	cbus_emergency();
+}
+
+static u16 tahvo_read(struct n810bm *bm, unsigned int reg)
+{
+	return tahvo_read_reg(&n810bm_tahvo_device->dev, reg);
+}
+
+static void tahvo_maskset(struct n810bm *bm, unsigned int reg, u16 mask, u16 set)
+{
+	tahvo_set_clear_reg_bits(&n810bm_tahvo_device->dev, reg, set, mask);
+}
+
+static inline void tahvo_write(struct n810bm *bm, unsigned int reg, u16 value)
+{
+	tahvo_write_reg(&n810bm_tahvo_device->dev, reg, value);
+}
+
+static inline void tahvo_set(struct n810bm *bm, unsigned int reg, u16 mask)
+{
+	tahvo_set_clear_reg_bits(&n810bm_tahvo_device->dev, reg, mask, mask);
+}
+
+static inline void tahvo_clear(struct n810bm *bm, unsigned int reg, u16 mask)
+{
+	tahvo_set_clear_reg_bits(&n810bm_tahvo_device->dev, reg, 0, mask);
+}
+
+static u16 retu_read(struct n810bm *bm, unsigned int reg)
+{
+	return retu_read_reg(&n810bm_retu_device->dev, reg);
+}
+
+static void retu_maskset(struct n810bm *bm, unsigned int reg, u16 mask, u16 set)
+{
+	retu_set_clear_reg_bits(&n810bm_retu_device->dev, reg, set, mask);
+}
+
+static inline void retu_write(struct n810bm *bm, unsigned int reg, u16 value)
+{
+	retu_write_reg(&n810bm_retu_device->dev, reg, value);
+}
+
+static int retu_adc_average(struct n810bm *bm, unsigned int chan,
+			    unsigned int nr_passes)
+{
+	unsigned int i, value = 0;
+	int ret;
+
+	if (WARN_ON(!nr_passes))
+		return 0;
+	for (i = 0; i < nr_passes; i++) {
+		ret = retu_read_adc(&n810bm_retu_device->dev, chan);
+		if (ret < 0)
+			return ret;
+		value += ret;
+	}
+	value /= nr_passes;
+
+	return value;
+}
+
+static struct n810bm_adc_calib * n810bm_get_adc_calib(struct n810bm *bm,
+						enum n810bm_pmm_adc_id id)
+{
+	unsigned int index = 0;
+	struct n810bm_adc_calib *cal;
+
+	if (id != N810BM_PMM_ADC_0xFE)
+		index = (unsigned int)id + 1;
+	if (index >= ARRAY_SIZE(bm->calib.adc))
+		return NULL;
+
+	cal = &bm->calib.adc[index];
+	WARN_ON(cal->id && cal->id != id);
+
+	return cal;
+}
+
+static int pmm_record_get(struct n810bm *bm,
+			  const struct firmware *pmm_block,
+			  void *buffer, size_t length,
+			  unsigned int group, unsigned int element, unsigned int offset)
+{
+	const u8 *pmm_area = pmm_block->data;
+	u8 active_group_mask;
+
+	if (pmm_block->size != N810BM_PMM_BLOCK_SIZE)
+		return -EINVAL;
+	if (group >= N810BM_PMM_BLOCK_SIZE / N810BM_PMM_GROUP_SIZE)
+		return -EINVAL;
+	if (element >= N810BM_PMM_GROUP_SIZE / N810BM_PMM_ELEM_SIZE)
+		return -EINVAL;
+	if (offset >= N810BM_PMM_ELEM_SIZE || length > N810BM_PMM_ELEM_SIZE ||
+	    length + offset > N810BM_PMM_ELEM_SIZE)
+		return -EINVAL;
+
+	active_group_mask = pmm_area[16];
+	if (!(active_group_mask & (1 << group))) {
+		dev_dbg(&bm->pdev->dev, "pwm_record_get: Requested group %u, "
+			"but group is not active", group);
+		return -ENOENT;
+	}
+
+	memcpy(buffer,
+	       pmm_area + group * N810BM_PMM_GROUP_SIZE
+			+ element * N810BM_PMM_ELEM_SIZE
+			+ offset,
+	       length);
+
+	return 0;
+}
+
+/* PMM block group 1 element */
+struct group1_element {
+	u8 id;
+	u8 flags;
+	u8 adc_groupnr;
+	u8 _padding;
+	__le32 field1;
+	__le32 field2;
+} __packed;
+
+static int extract_group1_elem(struct n810bm *bm,
+			       const struct firmware *pmm_block,
+			       const enum n810bm_pmm_adc_id *pmm_adc_ids, size_t nr_pmm_adc_ids,
+			       u32 field1_mask, u32 field2_mask)
+{
+	struct group1_element elem;
+	int err;
+	unsigned int i, element_nr;
+	struct n810bm_adc_calib *adc_calib;
+
+	for (i = 0; i < nr_pmm_adc_ids; i++) {
+		element_nr = (unsigned int)(pmm_adc_ids[i]) + 3;
+
+		err = pmm_record_get(bm, pmm_block, &elem, sizeof(elem),
+				     1, element_nr, 0);
+		if (err)
+			continue;
+		adc_calib = n810bm_get_adc_calib(bm, elem.id);
+		if (!adc_calib) {
+			dev_err(&bm->pdev->dev, "extract_group1_elem: "
+				"Could not get calib element for 0x%02X",
+				elem.id);
+			return -EINVAL;
+		}
+
+		if (adc_calib->flags == elem.flags) {
+			adc_calib->field1 = le32_to_cpu(elem.field1) & field1_mask;
+			adc_calib->field2 = le32_to_cpu(elem.field2) & field2_mask;
+		} else {
+			dev_dbg(&bm->pdev->dev, "extract_group1_elem: "
+				"Not extracting fields due to flags mismatch: "
+				"0x%02X vs 0x%02X",
+				adc_calib->flags, elem.flags);
+		}
+	}
+
+	return 0;
+}
+
+static int n810bm_parse_pmm_group1(struct n810bm *bm,
+				   const struct firmware *pmm_block)
+{
+	struct n810bm_adc_calib *adc_calib;
+	struct group1_element elem;
+	int err;
+
+	static const enum n810bm_pmm_adc_id pmm_adc_ids_1[] = {
+		N810BM_PMM_ADC_BATVOLT,
+		N810BM_PMM_ADC_CHGVOLT,
+		N810BM_PMM_ADC_BKUPVOLT,
+		N810BM_PMM_ADC_BATCURR,
+	};
+	static const enum n810bm_pmm_adc_id pmm_adc_ids_2[] = {
+		N810BM_PMM_ADC_BSI,
+	};
+	static const enum n810bm_pmm_adc_id pmm_adc_ids_3[] = {
+		N810BM_PMM_ADC_BATTEMP,
+	};
+
+	/* Parse element 2 */
+	err = pmm_record_get(bm, pmm_block, &elem, sizeof(elem),
+			     1, 2, 0);
+	if (err) {
+		dev_err(&bm->pdev->dev,
+			"PMM: Failed to get group 1 / element 2");
+		return err;
+	}
+	if (elem.id == N810BM_PMM_ADC_0xFE && elem.flags == 0x05) {
+		adc_calib = n810bm_get_adc_calib(bm, elem.id);
+		if (!adc_calib) {
+			dev_err(&bm->pdev->dev,
+				"calib extract: Failed to get 0xFE calib");
+			return -EINVAL;
+		}
+		adc_calib->id = elem.id;
+		adc_calib->flags = elem.flags;
+		adc_calib->field1 = le32_to_cpu(elem.field1);
+		adc_calib->field2 = le32_to_cpu(elem.field2);
+	}
+
+	err = extract_group1_elem(bm, pmm_block,
+				  pmm_adc_ids_1, ARRAY_SIZE(pmm_adc_ids_1),
+				  0xFFFFFFFF, 0xFFFFFFFF);
+	if (err)
+		return err;
+	err = extract_group1_elem(bm, pmm_block,
+				  pmm_adc_ids_2, ARRAY_SIZE(pmm_adc_ids_2),
+				  0xFFFFFFFF, 0);
+	if (err)
+		return err;
+	err = extract_group1_elem(bm, pmm_block,
+				  pmm_adc_ids_3, ARRAY_SIZE(pmm_adc_ids_3),
+				  0xFFFFFFFF, 0x0000FFFF);
+	if (err)
+		return err;
+
+	return 0;
+}
+
+static int n810bm_parse_pmm_group2(struct n810bm *bm,
+				   const struct firmware *pmm_block)
+{
+	dev_err(&bm->pdev->dev, "TODO: CAL BME PMM group 2 parser not implemented, yet");
+	return -EOPNOTSUPP;
+}
+
+static void n810bm_adc_calib_set_defaults(struct n810bm *bm)
+{
+	struct n810bm_adc_calib *adc_calib;
+	unsigned int i;
+
+	static const struct n810bm_adc_calib defaults[] = {
+		/* ADC group-nr 0 */
+		{
+			.id		= N810BM_PMM_ADC_HEADSET,
+			.flags		= 0x00,
+			.adc_groupnr	= 0,
+		}, {
+			.id		= N810BM_PMM_ADC_HOOKDET,
+			.flags		= 0x00,
+			.adc_groupnr	= 0,
+		}, {
+			.id		= N810BM_PMM_ADC_RFGP,
+			.flags		= 0x00,
+			.adc_groupnr	= 0,
+		}, {
+			.id		= N810BM_PMM_ADC_LIGHTSENS,
+			.flags		= 0x00,
+			.adc_groupnr	= 0,
+		}, {
+			.id		= N810BM_PMM_ADC_WBTX,
+			.flags		= 0x00,
+			.adc_groupnr	= 0,
+		}, {
+			.id		= N810BM_PMM_ADC_RETUTEMP,
+			.flags		= 0x00,
+			.adc_groupnr	= 0,
+		}, {
+			.id		= N810BM_PMM_ADC_GND2,
+			.flags		= 0x00,
+			.adc_groupnr	= 0,
+		},
+		/* ADC group-nr 1 */
+		{
+			.id		= N810BM_PMM_ADC_0xFE,
+			.flags		= 0x05,
+			.adc_groupnr	= 1,
+			.field1		= (u32)-2,
+			.field2		= 13189,
+		}, {
+			.id		= N810BM_PMM_ADC_BATVOLT,
+			.flags		= 0x01,
+			.adc_groupnr	= 1,
+			.field1		= 2527,
+			.field2		= 21373,
+		}, {
+			.id		= N810BM_PMM_ADC_CHGVOLT,
+			.flags		= 0x01,
+			.adc_groupnr	= 1,
+			.field1		= 0,
+			.field2		= 129848,
+		}, {
+			.id		= N810BM_PMM_ADC_BKUPVOLT,
+			.flags		= 0x01,
+			.adc_groupnr	= 1,
+			.field1		= 0,
+			.field2		= 20000,
+		}, {
+			.id		= N810BM_PMM_ADC_BATCURR,
+			.flags		= 0x06,
+			.adc_groupnr	= 1,
+			.field1		= 0,
+			.field2		= 9660,
+		},
+		/* ADC group-nr 2 */
+		{
+			.id		= N810BM_PMM_ADC_BSI,
+			.flags		= 0x02,
+			.adc_groupnr	= 2,
+			.field1		= 1169,
+			.field2		= 0,
+		},
+		/* ADC group-nr 3 */
+		{
+			.id		= N810BM_PMM_ADC_BATTEMP,
+			.flags		= 0x03,
+			.adc_groupnr	= 3,
+			.field1		= 265423000,
+			.field2		= 298,
+		},
+		/* ADC group-nr 4 */
+		{
+			.id		= N810BM_PMM_ADC_LIGHTTEMP,
+			.flags		= 0x04,
+			.adc_groupnr	= 4,
+			.field1		= 19533778,
+			.field2		= 308019670,
+			.field3		= 4700,
+			.field4		= 2500,
+		},
+	};
+
+	/* Clear the array */
+	memset(&bm->calib.adc, 0, sizeof(bm->calib.adc));
+	for (i = 0; i < ARRAY_SIZE(bm->calib.adc); i++)
+		bm->calib.adc[i].flags = 0xFF;
+
+	/* Copy the defaults */
+	for (i = 0; i < ARRAY_SIZE(defaults); i++) {
+		adc_calib = n810bm_get_adc_calib(bm, defaults[i].id);
+		if (WARN_ON(!adc_calib))
+			continue;
+		*adc_calib = defaults[i];
+	}
+}
+
+static int n810bm_parse_pmm_block(struct n810bm *bm,
+				  const struct firmware *pmm_block)
+{
+	u8 byte;
+	int err;
+	unsigned int i, count;
+	struct n810bm_adc_calib *adc_calib;
+
+	/* Initialize to defaults */
+	n810bm_adc_calib_set_defaults(bm);
+
+	/* Parse the PMM data */
+	err = pmm_record_get(bm, pmm_block, &byte, sizeof(byte),
+			     1, 0, 0); /* group 1 / element 0 */
+	err |= (byte != 0x01);
+	err |= pmm_record_get(bm, pmm_block, &byte, sizeof(byte),
+			      1, 1, 0); /* group 1 / element 1 */
+	err |= (byte != 0x01);
+	if (err)
+		err = n810bm_parse_pmm_group2(bm, pmm_block);
+	else
+		err = n810bm_parse_pmm_group1(bm, pmm_block);
+	if (err)
+		return err;
+
+	/* Sanity checks */
+	for (i = 0, count = 0; i < ARRAY_SIZE(bm->calib.adc); i++) {
+		adc_calib = &bm->calib.adc[i];
+		if (adc_calib->flags == 0xFF)
+			continue;
+		switch (adc_calib->id) {
+		case N810BM_PMM_ADC_BATVOLT:
+			if (adc_calib->field1 < 2400 ||
+			    adc_calib->field1 > 2700)
+				goto value_check_fail;
+			if (adc_calib->field2 < 20000 ||
+			    adc_calib->field2 > 23000)
+				goto value_check_fail;
+			count++;
+			break;
+		case N810BM_PMM_ADC_BSI:
+			if (adc_calib->field1 < 1100 ||
+			    adc_calib->field1 > 1300)
+				goto value_check_fail;
+			count++;
+			break;
+		case N810BM_PMM_ADC_BATCURR:
+			if (adc_calib->field2 < 7000 ||
+			    adc_calib->field2 > 12000)
+				goto value_check_fail;
+			count++;
+			break;
+		case N810BM_PMM_ADC_0xFE:
+			if ((s32)adc_calib->field1 > 14 ||
+			    (s32)adc_calib->field1 < -14)
+				goto value_check_fail;
+			if (adc_calib->field2 < 13000 ||
+			    adc_calib->field2 > 13350)
+				goto value_check_fail;
+			count++;
+			break;
+		case N810BM_PMM_ADC_CHGVOLT:
+		case N810BM_PMM_ADC_BATTEMP:
+		case N810BM_PMM_ADC_BKUPVOLT:
+			count++;
+			break;
+		case N810BM_PMM_ADC_GND2:
+		case N810BM_PMM_ADC_HOOKDET:
+		case N810BM_PMM_ADC_LIGHTSENS:
+		case N810BM_PMM_ADC_HEADSET:
+		case N810BM_PMM_ADC_LIGHTTEMP:
+		case N810BM_PMM_ADC_RFGP:
+		case N810BM_PMM_ADC_WBTX:
+		case N810BM_PMM_ADC_RETUTEMP:
+			break;
+		}
+		dev_dbg(&bm->pdev->dev,
+			"ADC 0x%02X calib: 0x%02X 0x%02X 0x%08X 0x%08X 0x%04X 0x%04X",
+			adc_calib->id, adc_calib->flags, adc_calib->adc_groupnr,
+			adc_calib->field1, adc_calib->field2,
+			adc_calib->field3, adc_calib->field4);
+	}
+	if (count != 7) {
+		dev_err(&bm->pdev->dev, "PMM sanity check: Did not find "
+			"all required values (count=%u)", count);
+		goto check_fail;
+	}
+
+	return 0;
+
+value_check_fail:
+	dev_err(&bm->pdev->dev, "PMM image sanity check failed "
+		"(id=%02X, field1=%08X, field2=%08X)",
+		adc_calib->id, adc_calib->field1, adc_calib->field2);
+check_fail:
+	return -EILSEQ;
+}
+
+/* Set the current measure timer that triggers on Tahvo IRQ 7
+ * An interval of zero disables the timer. */
+static void n810bm_set_current_measure_timer(struct n810bm *bm,
+					     u16 millisec_interval)
+{
+	u16 value = millisec_interval;
+
+	if (value <= 0xF905) {
+		value = ((u64)0x10624DD3 * (u64)(value + 0xF9)) >> 32;
+		value /= 16;
+	} else
+		value = 0xFF;
+
+	tahvo_write(bm, TAHVO_REG_BATCURRTIMER, value & 0xFF);
+
+	tahvo_set(bm, TAHVO_REG_CHGCTL,
+		  TAHVO_REG_CHGCTL_CURTIMRST);
+	tahvo_clear(bm, TAHVO_REG_CHGCTL,
+		    TAHVO_REG_CHGCTL_CURTIMRST);
+
+	if (millisec_interval) {
+		if (!bm->tahvo_irq_enabled) {
+			bm->tahvo_irq_enabled = 1;
+			enable_irq(bm->tahvo_irq);
+		}
+	} else {
+		if (bm->tahvo_irq_enabled) {
+			bm->tahvo_irq_enabled = 0;
+			disable_irq_nosync(bm->tahvo_irq);
+		}
+	}
+
+	//TODO also do a software timer for safety.
+}
+
+static void n810bm_enable_current_measure(struct n810bm *bm)
+{
+	WARN_ON(bm->current_measure_enabled < 0);
+	if (!bm->current_measure_enabled) {
+		/* Enable the current measurement circuitry */
+		tahvo_set(bm, TAHVO_REG_CHGCTL,
+			  TAHVO_REG_CHGCTL_CURMEAS);
+		dev_dbg(&bm->pdev->dev,
+			"Current measurement circuitry enabled");
+	}
+	bm->current_measure_enabled++;
+}
+
+static void n810bm_disable_current_measure(struct n810bm *bm)
+{
+	bm->current_measure_enabled--;
+	WARN_ON(bm->current_measure_enabled < 0);
+	if (!bm->current_measure_enabled) {
+		/* Disable the current measurement circuitry */
+		tahvo_clear(bm, TAHVO_REG_CHGCTL,
+			    TAHVO_REG_CHGCTL_CURMEAS);
+		dev_dbg(&bm->pdev->dev,
+			"Current measurement circuitry disabled");
+	}
+}
+
+/* Measure the actual battery current. Returns a signed value in mA.
+ * Does only work, if current measurement was enabled. */
+static int n810bm_measure_batt_current(struct n810bm *bm)
+{
+	u16 retval;
+	int adc = 0, ma, i;
+
+	if (WARN_ON(bm->current_measure_enabled <= 0))
+		return 0;
+	for (i = 0; i < 3; i++) {
+		retval = tahvo_read(bm, TAHVO_REG_BATCURR);
+		adc += (s16)retval; /* Value is signed */
+	}
+	adc /= 3;
+
+	//TODO convert to mA
+	ma = adc;
+
+	return ma;
+}
+
+/* Requires bm->mutex locked */
+static int n810bm_measure_batt_current_async(struct n810bm *bm)
+{
+	int ma;
+	bool charging = lipocharge_is_charging(&bm->charger);
+
+	n810bm_enable_current_measure(bm);
+	if (!charging)
+		WARN_ON(bm->active_current_pwm != 0);
+	tahvo_maskset(bm, TAHVO_REG_CHGCTL,
+		      TAHVO_REG_CHGCTL_EN |
+		      TAHVO_REG_CHGCTL_PWMOVR |
+		      TAHVO_REG_CHGCTL_PWMOVRZERO,
+		      TAHVO_REG_CHGCTL_EN |
+		      TAHVO_REG_CHGCTL_PWMOVR |
+		      (charging ? 0 : TAHVO_REG_CHGCTL_PWMOVRZERO));
+	ma = n810bm_measure_batt_current(bm);
+	tahvo_maskset(bm, TAHVO_REG_CHGCTL,
+		      TAHVO_REG_CHGCTL_EN |
+		      TAHVO_REG_CHGCTL_PWMOVR |
+		      TAHVO_REG_CHGCTL_PWMOVRZERO,
+		      (charging ? TAHVO_REG_CHGCTL_EN : 0));
+	n810bm_disable_current_measure(bm);
+
+	return ma;
+}
+
+static int adc_sanity_check(struct n810bm *bm, unsigned int channel)
+{
+	int value;
+
+	value = retu_read_adc(&n810bm_retu_device->dev, channel);
+	if (value < 0) {
+		dev_err(&bm->pdev->dev, "Failed to read GND ADC channel %u",
+			channel);
+		return -EIO;
+	}
+	dev_dbg(&bm->pdev->dev,
+		"GND ADC channel %u sanity check got value: %d",
+		channel, value);
+	if (value > 5) {
+		n810bm_emergency(bm, "GND ADC sanity check failed");
+		return -EIO;
+	}
+
+	return 0;
+}
+
+static int n810bm_check_adc_sanity(struct n810bm *bm)
+{
+	int err;
+
+	/* Discard one conversion */
+	retu_write(bm, RETU_REG_ADCSCR, 0);
+	retu_read_adc(&n810bm_retu_device->dev, RETU_ADC_GND2);
+
+	err = adc_sanity_check(bm, RETU_ADC_GND2);
+	if (err)
+		return err;
+
+	return 0;
+}
+
+/* Measure the battery voltage. Returns the value in mV (or negative value on error). */
+static int n810bm_measure_batt_voltage(struct n810bm *bm)
+{
+	int adc;
+	unsigned int mv;
+	const unsigned int scale = 1000;
+
+	adc = retu_adc_average(bm, RETU_ADC_BATTVOLT, 5);
+	if (adc < 0)
+		return adc;
+	if (adc <= 0x37)
+		return 2800;
+	mv = 2800 + ((adc - 0x37) * (((4200 - 2800) * scale) / (0x236 - 0x37))) / scale;
+
+	//TODO compensate for power consumption
+	//TODO honor calibration values
+
+	return mv;
+}
+
+/* Measure the charger voltage. Returns the value in mV (or negative value on error). */
+static int n810bm_measure_charger_voltage(struct n810bm *bm)
+{
+	int adc;
+	unsigned int mv;
+
+	adc = retu_adc_average(bm, RETU_ADC_CHGVOLT, 5);
+	if (adc < 0)
+		return adc;
+	//TODO convert to mV
+	mv = adc;
+
+	return mv;
+}
+
+/* Measure backup battery voltage. Returns the value in mV (or negative value on error). */
+static int n810bm_measure_backup_batt_voltage(struct n810bm *bm)
+{
+	int adc;
+	unsigned int mv;
+
+	adc = retu_adc_average(bm, RETU_ADC_BKUPVOLT, 3);
+	if (adc < 0)
+		return adc;
+	//TODO convert to mV
+	mv = adc;
+
+	return mv;
+}
+
+/* Measure the battery temperature. Returns the value in K (or negative value on error). */
+static int n810bm_measure_batt_temp(struct n810bm *bm)
+{
+	int adc;
+	unsigned int k;
+
+	adc = retu_adc_average(bm, RETU_ADC_BATTEMP, 3);
+	if (adc < 0)
+		return adc;
+	//TODO convert to K
+	k = adc;
+
+	return k;
+}
+
+/* Read the battery capacity via BSI pin. */
+static enum n810bm_capacity n810bm_read_batt_capacity(struct n810bm *bm)
+{
+	int adc;
+	const unsigned int hyst = 20;
+
+	adc = retu_adc_average(bm, RETU_ADC_BSI, 5);
+	if (adc < 0) {
+		dev_err(&bm->pdev->dev, "Failed to read BSI ADC");
+		return N810BM_CAP_UNKNOWN;
+	}
+
+	if (adc >= 0x3B5 - hyst && adc <= 0x3B5 + hyst)
+		return N810BM_CAP_1500MAH;
+
+	dev_err(&bm->pdev->dev, "Capacity indicator 0x%X unknown", adc);
+
+	return N810BM_CAP_UNKNOWN;
+}
+
+/* Convert a battery voltage (in mV) to percentage. */
+static unsigned int n810bm_mvolt2percent(unsigned int mv)
+{
+	const unsigned int minv = 3700;
+	const unsigned int maxv = 4150;
+	unsigned int percent;
+
+	mv = clamp(mv, minv, maxv);
+	percent = (mv - minv) * 100 / (maxv - minv);
+
+	return percent;
+}
+
+static void n810bm_start_charge(struct n810bm *bm)
+{
+	int err;
+
+	WARN_ON(!bm->battery_present);
+	WARN_ON(!bm->charger_present);
+
+	/* Set PWM to zero */
+	bm->active_current_pwm = 0;
+	tahvo_write(bm, TAHVO_REG_CHGCURR, bm->active_current_pwm);
+
+	/* Charge global enable */
+	tahvo_maskset(bm, TAHVO_REG_CHGCTL,
+		      TAHVO_REG_CHGCTL_EN |
+		      TAHVO_REG_CHGCTL_PWMOVR |
+		      TAHVO_REG_CHGCTL_PWMOVRZERO,
+		      TAHVO_REG_CHGCTL_EN);
+
+	WARN_ON((int)bm->capacity <= 0);
+	bm->charger.capacity = bm->capacity;
+	err = lipocharge_start(&bm->charger);
+	WARN_ON(err);
+
+	/* Initialize current measurement circuitry */
+	n810bm_enable_current_measure(bm);
+	n810bm_set_current_measure_timer(bm, 250);
+
+	dev_info(&bm->pdev->dev, "Charging battery");
+	n810bm_notify_charger_state(bm);
+	n810bm_notify_charger_pwm(bm);
+}
+
+static void n810bm_stop_charge(struct n810bm *bm)
+{
+	if (lipocharge_is_charging(&bm->charger)) {
+		n810bm_set_current_measure_timer(bm, 0);
+		n810bm_disable_current_measure(bm);
+	}
+	lipocharge_stop(&bm->charger);
+
+	/* Set PWM to zero */
+	bm->active_current_pwm = 0;
+	tahvo_write(bm, TAHVO_REG_CHGCURR, bm->active_current_pwm);
+
+	/* Charge global disable */
+	tahvo_maskset(bm, TAHVO_REG_CHGCTL,
+		      TAHVO_REG_CHGCTL_EN |
+		      TAHVO_REG_CHGCTL_PWMOVR |
+		      TAHVO_REG_CHGCTL_PWMOVRZERO,
+		      0);
+
+	dev_info(&bm->pdev->dev, "Not charging battery");
+	n810bm_notify_charger_state(bm);
+	n810bm_notify_charger_pwm(bm);
+}
+
+/* Periodic check */
+static void n810bm_periodic_check_work(struct work_struct *work)
+{
+	struct n810bm *bm = container_of(to_delayed_work(work),
+					 struct n810bm, periodic_check_work);
+	u16 status;
+	bool battery_was_present, charger_was_present;
+	int mv;
+
+	mutex_lock(&bm->mutex);
+
+	status = retu_read(bm, RETU_REG_STATUS);
+	battery_was_present = bm->battery_present;
+	charger_was_present = bm->charger_present;
+	bm->battery_present = !!(status & RETU_REG_STATUS_BATAVAIL);
+	bm->charger_present = !!(status & RETU_REG_STATUS_CHGPLUG);
+
+	if (bm->battery_present != battery_was_present) {
+		/* Battery state changed */
+		if (bm->battery_present) {
+			bm->capacity = n810bm_read_batt_capacity(bm);
+			if (bm->capacity == N810BM_CAP_UNKNOWN) {
+				dev_err(&bm->pdev->dev, "Unknown battery detected");
+			} else {
+				dev_info(&bm->pdev->dev, "Detected %u mAh battery",
+					 (unsigned int)bm->capacity);
+			}
+		} else {
+			bm->capacity = N810BM_CAP_NONE;
+			dev_info(&bm->pdev->dev, "The main battery was removed");
+			//TODO disable charging
+		}
+	}
+
+	if (bm->charger_present != charger_was_present) {
+		/* Charger state changed */
+		dev_info(&bm->pdev->dev, "The charger was %s",
+			 bm->charger_present ? "plugged in" : "removed");
+		n810bm_notify_charger_present(bm);
+	}
+
+	if ((bm->battery_present && !bm->charger_present) ||
+	    !n810bm_known_battery_present(bm)){
+		/* We're draining the battery */
+		mv = n810bm_measure_batt_voltage(bm);
+		if (mv < 0) {
+			n810bm_emergency(bm,
+				"check: Failed to measure voltage");
+		}
+		if (mv < N810BM_MIN_VOLTAGE_THRES) {
+			n810bm_emergency(bm,
+				"check: Minimum voltage threshold reached");
+		}
+	}
+
+	if (bm->charger_present && n810bm_known_battery_present(bm)) {
+		/* Known battery and charger are connected */
+		if (bm->charger_enabled) {
+			/* Charger is enabled */
+			if (!lipocharge_is_charging(&bm->charger)) {
+				//TODO start charging, if battery is below some threshold
+				n810bm_start_charge(bm);
+			}
+		}
+	}
+
+	if (lipocharge_is_charging(&bm->charger) && !bm->charger_present) {
+		/* Charger was unplugged. */
+		n810bm_stop_charge(bm);
+	}
+
+	mutex_unlock(&bm->mutex);
+	schedule_delayed_work(&bm->periodic_check_work,
+			      round_jiffies_relative(N810BM_CHECK_INTERVAL));
+}
+
+/*XXX
+static void n810bm_adc_irq_handler(unsigned long data)
+{
+	struct n810bm *bm = (struct n810bm *)data;
+
+	retu_ack_irq(RETU_INT_ADCS);
+	//TODO
+dev_info(&bm->pdev->dev, "ADC interrupt triggered\n");
+}
+*/
+
+static irqreturn_t n810bm_tahvo_current_measure_irq_handler(int irq, void *data)
+{
+	struct n810bm *bm = data;
+	int res, ma, mv, temp;
+
+	mutex_lock(&bm->mutex);
+	if (!lipocharge_is_charging(&bm->charger))
+		goto out_unlock;
+
+	tahvo_maskset(bm, TAHVO_REG_CHGCTL,
+		      TAHVO_REG_CHGCTL_PWMOVR |
+		      TAHVO_REG_CHGCTL_PWMOVRZERO,
+		      TAHVO_REG_CHGCTL_PWMOVR);
+	ma = n810bm_measure_batt_current(bm);
+	tahvo_maskset(bm, TAHVO_REG_CHGCTL,
+		      TAHVO_REG_CHGCTL_PWMOVR |
+		      TAHVO_REG_CHGCTL_PWMOVRZERO,
+		      TAHVO_REG_CHGCTL_PWMOVR |
+		      TAHVO_REG_CHGCTL_PWMOVRZERO);
+	msleep(10);
+	mv = n810bm_measure_batt_voltage(bm);
+	tahvo_maskset(bm, TAHVO_REG_CHGCTL,
+		      TAHVO_REG_CHGCTL_PWMOVR |
+		      TAHVO_REG_CHGCTL_PWMOVRZERO,
+		      0);
+	temp = n810bm_measure_batt_temp(bm);
+	if (WARN_ON(mv < 0))
+		goto out_unlock;
+	if (WARN_ON(temp < 0))
+		goto out_unlock;
+
+	if (bm->verbose_charge_log) {
+		dev_info(&bm->pdev->dev,
+			 "Battery charge state: %d mV, %d mA (%s)",
+			 mv, ma,
+			 (ma <= 0) ? "discharging" : "charging");
+	}
+	res = lipocharge_update_state(&bm->charger, mv, ma, temp);
+	if (res) {
+		if (res > 0)
+			dev_info(&bm->pdev->dev, "Battery fully charged");
+		n810bm_stop_charge(bm);
+	}
+out_unlock:
+	mutex_unlock(&bm->mutex);
+
+	return IRQ_HANDLED;
+}
+
+#define DEFINE_ATTR_NOTIFY(attr_name)						\
+	void n810bm_notify_##attr_name(struct n810bm *bm)			\
+	{									\
+		set_bit(N810BM_NOTIFY_##attr_name, &bm->notify_flags);		\
+		wmb();								\
+		schedule_work(&bm->notify_work);				\
+	}
+
+#define DEFINE_SHOW_INT_FUNC(name, member)					\
+	static ssize_t n810bm_attr_##name##_show(struct device *dev,		\
+						 struct device_attribute *attr,	\
+						 char *buf)			\
+	{									\
+		struct n810bm *bm = device_to_n810bm(dev);			\
+		ssize_t count;							\
+										\
+		mutex_lock(&bm->mutex);						\
+		count = snprintf(buf, PAGE_SIZE, "%d\n", (int)(bm->member));	\
+		mutex_unlock(&bm->mutex);					\
+										\
+		return count;							\
+	}
+
+#define DEFINE_STORE_INT_FUNC(name, member)					\
+	static ssize_t n810bm_attr_##name##_store(struct device *dev,		\
+						  struct device_attribute *attr,\
+						  const char *buf, size_t count)\
+	{									\
+		struct n810bm *bm = device_to_n810bm(dev);			\
+		long val;							\
+		int err;							\
+										\
+		mutex_lock(&bm->mutex);						\
+		err = strict_strtol(buf, 0, &val);				\
+		if (!err)							\
+			bm->member = (typeof(bm->member))val;			\
+		mutex_unlock(&bm->mutex);					\
+										\
+		return err ? err : count;					\
+	}
+
+#define DEFINE_ATTR_SHOW_INT(name, member)					\
+	DEFINE_SHOW_INT_FUNC(name, member)					\
+	static DEVICE_ATTR(name, S_IRUGO,					\
+			   n810bm_attr_##name##_show, NULL);
+
+#define DEFINE_ATTR_SHOW_STORE_INT(name, member)				\
+	DEFINE_SHOW_INT_FUNC(name, member)					\
+	DEFINE_STORE_INT_FUNC(name, member)					\
+	static DEVICE_ATTR(name, S_IRUGO | S_IWUSR,				\
+			   n810bm_attr_##name##_show,				\
+			   n810bm_attr_##name##_store);
+
+DEFINE_ATTR_SHOW_INT(battery_present, battery_present);
+DEFINE_ATTR_SHOW_INT(charger_present, charger_present);
+static DEFINE_ATTR_NOTIFY(charger_present);
+DEFINE_ATTR_SHOW_INT(charger_state, charger.state);
+static DEFINE_ATTR_NOTIFY(charger_state);
+DEFINE_ATTR_SHOW_INT(charger_pwm, active_current_pwm);
+static DEFINE_ATTR_NOTIFY(charger_pwm);
+DEFINE_ATTR_SHOW_STORE_INT(charger_enable, charger_enabled);
+DEFINE_ATTR_SHOW_STORE_INT(charger_verbose, verbose_charge_log);
+
+static ssize_t n810bm_attr_battery_level_show(struct device *dev,
+					      struct device_attribute *attr,
+					      char *buf)
+{
+	struct n810bm *bm = device_to_n810bm(dev);
+	ssize_t count = -ENODEV;
+	int millivolt;
+
+	mutex_lock(&bm->mutex);
+	if (!bm->battery_present || lipocharge_is_charging(&bm->charger))
+		millivolt = 0;
+	else
+		millivolt = n810bm_measure_batt_voltage(bm);
+	if (millivolt >= 0) {
+		count = snprintf(buf, PAGE_SIZE, "%u\n",
+				 n810bm_mvolt2percent(millivolt));
+	}
+	mutex_unlock(&bm->mutex);
+
+	return count;
+}
+static DEVICE_ATTR(battery_level, S_IRUGO,
+		   n810bm_attr_battery_level_show, NULL);
+
+static ssize_t n810bm_attr_battery_capacity_show(struct device *dev,
+						 struct device_attribute *attr,
+						 char *buf)
+{
+	struct n810bm *bm = device_to_n810bm(dev);
+	ssize_t count;
+	int capacity = 0;
+
+	mutex_lock(&bm->mutex);
+	if (n810bm_known_battery_present(bm))
+		capacity = (int)bm->capacity;
+	count = snprintf(buf, PAGE_SIZE, "%d\n", capacity);
+	mutex_unlock(&bm->mutex);
+
+	return count;
+}
+static DEVICE_ATTR(battery_capacity, S_IRUGO,
+		   n810bm_attr_battery_capacity_show, NULL);
+
+static ssize_t n810bm_attr_battery_temp_show(struct device *dev,
+					     struct device_attribute *attr,
+					     char *buf)
+{
+	struct n810bm *bm = device_to_n810bm(dev);
+	ssize_t count = -ENODEV;
+	int k;
+
+	mutex_lock(&bm->mutex);
+	k = n810bm_measure_batt_temp(bm);
+	if (k >= 0)
+		count = snprintf(buf, PAGE_SIZE, "%d\n", k);
+	mutex_unlock(&bm->mutex);
+
+	return count;
+}
+static DEVICE_ATTR(battery_temp, S_IRUGO,
+		   n810bm_attr_battery_temp_show, NULL);
+
+static ssize_t n810bm_attr_charger_voltage_show(struct device *dev,
+						struct device_attribute *attr,
+						char *buf)
+{
+	struct n810bm *bm = device_to_n810bm(dev);
+	ssize_t count = -ENODEV;
+	int mv = 0;
+
+	mutex_lock(&bm->mutex);
+	if (bm->charger_present)
+		mv = n810bm_measure_charger_voltage(bm);
+	if (mv >= 0)
+		count = snprintf(buf, PAGE_SIZE, "%d\n", mv);
+	mutex_unlock(&bm->mutex);
+
+	return count;
+}
+static DEVICE_ATTR(charger_voltage, S_IRUGO,
+		   n810bm_attr_charger_voltage_show, NULL);
+
+static ssize_t n810bm_attr_backup_battery_voltage_show(struct device *dev,
+						       struct device_attribute *attr,
+						       char *buf)
+{
+	struct n810bm *bm = device_to_n810bm(dev);
+	ssize_t count = -ENODEV;
+	int mv;
+
+	mutex_lock(&bm->mutex);
+	mv = n810bm_measure_backup_batt_voltage(bm);
+	if (mv >= 0)
+		count = snprintf(buf, PAGE_SIZE, "%d\n", mv);
+	mutex_unlock(&bm->mutex);
+
+	return count;
+}
+static DEVICE_ATTR(backup_battery_voltage, S_IRUGO,
+		   n810bm_attr_backup_battery_voltage_show, NULL);
+
+static ssize_t n810bm_attr_battery_current_show(struct device *dev,
+						struct device_attribute *attr,
+						char *buf)
+{
+	struct n810bm *bm = device_to_n810bm(dev);
+	ssize_t count = -ENODEV;
+	int ma = 0;
+
+	mutex_lock(&bm->mutex);
+	if (bm->battery_present)
+		ma = n810bm_measure_batt_current_async(bm);
+	count = snprintf(buf, PAGE_SIZE, "%d\n", ma);
+	mutex_unlock(&bm->mutex);
+
+	return count;
+}
+static DEVICE_ATTR(battery_current, S_IRUGO,
+		   n810bm_attr_battery_current_show, NULL);
+
+static const struct device_attribute *n810bm_attrs[] = {
+	&dev_attr_battery_present,
+	&dev_attr_battery_level,
+	&dev_attr_battery_current,
+	&dev_attr_battery_capacity,
+	&dev_attr_battery_temp,
+	&dev_attr_backup_battery_voltage,
+	&dev_attr_charger_present,
+	&dev_attr_charger_state,
+	&dev_attr_charger_verbose,
+	&dev_attr_charger_voltage,
+	&dev_attr_charger_enable,
+	&dev_attr_charger_pwm,
+};
+
+static void n810bm_notify_work(struct work_struct *work)
+{
+	struct n810bm *bm = container_of(work, struct n810bm, notify_work);
+	unsigned long notify_flags;
+
+	notify_flags = xchg(&bm->notify_flags, 0);
+	mb();
+
+#define do_notify(attr_name)						\
+	do {								\
+		if (notify_flags & (1 << N810BM_NOTIFY_##attr_name)) {	\
+			sysfs_notify(&bm->pdev->dev.kobj, NULL,		\
+				     dev_attr_##attr_name.attr.name);	\
+		}							\
+	} while (0)
+
+	do_notify(charger_present);
+	do_notify(charger_state);
+	do_notify(charger_pwm);
+}
+
+static int n810bm_charger_set_current_pwm(struct lipocharge *c,
+					  unsigned int duty_cycle)
+{
+	struct n810bm *bm = container_of(c, struct n810bm, charger);
+	int err = -EINVAL;
+
+	WARN_ON(!mutex_is_locked(&bm->mutex));
+	if (WARN_ON(duty_cycle > 0xFF))
+		goto out;
+	if (WARN_ON(!bm->charger_enabled))
+		goto out;
+	if (WARN_ON(!bm->battery_present || !bm->charger_present))
+		goto out;
+
+	if (duty_cycle != bm->active_current_pwm) {
+		bm->active_current_pwm = duty_cycle;
+		tahvo_write(bm, TAHVO_REG_CHGCURR, duty_cycle);
+		n810bm_notify_charger_pwm(bm);
+	}
+
+	err = 0;
+out:
+
+	return err;
+}
+
+static void n810bm_charger_emergency(struct lipocharge *c)
+{
+	struct n810bm *bm = container_of(c, struct n810bm, charger);
+
+	n810bm_emergency(bm, "Battery charger fault");
+}
+
+static void n810bm_hw_exit(struct n810bm *bm)
+{
+	n810bm_stop_charge(bm);
+	retu_write(bm, RETU_REG_ADCSCR, 0);
+}
+
+static int n810bm_hw_init(struct n810bm *bm)
+{
+	int err;
+
+	err = n810bm_check_adc_sanity(bm);
+	if (err)
+		return err;
+
+	n810bm_stop_charge(bm);
+
+	return 0;
+}
+
+static void n810bm_cancel_and_flush_work(struct n810bm *bm)
+{
+	cancel_delayed_work_sync(&bm->periodic_check_work);
+	cancel_work_sync(&bm->notify_work);
+	flush_scheduled_work();
+}
+
+static int n810bm_device_init(struct n810bm *bm)
+{
+	int attr_index;
+	int err;
+
+	bm->charger.rate = LIPORATE_p6C;
+	bm->charger.top_voltage = 4100;
+	bm->charger.duty_cycle_max = 0xFF;
+	bm->charger.set_current_pwm = n810bm_charger_set_current_pwm;
+	bm->charger.emergency = n810bm_charger_emergency;
+	lipocharge_init(&bm->charger, &bm->pdev->dev);
+
+	err = n810bm_hw_init(bm);
+	if (err)
+		goto error;
+	for (attr_index = 0; attr_index < ARRAY_SIZE(n810bm_attrs); attr_index++) {
+		err = device_create_file(&bm->pdev->dev, n810bm_attrs[attr_index]);
+		if (err)
+			goto err_unwind_attrs;
+	}
+/*XXX
+	err = retu_request_irq(RETU_INT_ADCS,
+			       n810bm_adc_irq_handler,
+			       (unsigned long)bm, "n810bm");
+	if (err)
+		goto err_unwind_attrs;
+*/
+	bm->tahvo_irq = platform_get_irq(n810bm_tahvo_device, 0);
+	err = request_threaded_irq(bm->tahvo_irq, NULL,
+				   n810bm_tahvo_current_measure_irq_handler,
+				   IRQF_ONESHOT, "tahvo-n810bm", bm);
+	if (err)
+		goto err_free_retu_irq;
+	disable_irq_nosync(bm->tahvo_irq);
+	bm->tahvo_irq_enabled = 0;
+
+	schedule_delayed_work(&bm->periodic_check_work,
+			      round_jiffies_relative(N810BM_CHECK_INTERVAL));
+
+	bm->initialized = 1;
+	dev_info(&bm->pdev->dev, "Battery management initialized");
+
+	return 0;
+
+err_free_retu_irq:
+//XXX	retu_free_irq(RETU_INT_ADCS);
+err_unwind_attrs:
+	for (attr_index--; attr_index >= 0; attr_index--)
+		device_remove_file(&bm->pdev->dev, n810bm_attrs[attr_index]);
+/*err_exit:*/
+	n810bm_hw_exit(bm);
+error:
+	n810bm_cancel_and_flush_work(bm);
+
+	return err;
+}
+
+static void n810bm_device_exit(struct n810bm *bm)
+{
+	int i;
+
+	if (!bm->initialized)
+		return;
+
+	lipocharge_exit(&bm->charger);
+	free_irq(bm->tahvo_irq, bm);
+//XXX	retu_free_irq(RETU_INT_ADCS);
+	for (i = 0; i < ARRAY_SIZE(n810bm_attrs); i++)
+		device_remove_file(&bm->pdev->dev, n810bm_attrs[i]);
+
+	n810bm_cancel_and_flush_work(bm);
+
+	n810bm_hw_exit(bm);
+
+	bm->initialized = 0;
+}
+
+static void n810bm_pmm_block_found(const struct firmware *fw, void *context)
+{
+	struct n810bm *bm = context;
+	int err;
+
+	if (!fw) {
+		dev_err(&bm->pdev->dev,
+			"CAL PMM block image file not found");
+		goto err_release;
+	}
+	if (fw->size != N810BM_PMM_BLOCK_SIZE ||
+	    memcmp(fw->data, "BME-PMM-BLOCK01", 15) != 0) {
+		dev_err(&bm->pdev->dev,
+			"CAL PMM block image file has an invalid format");
+		goto err_release;
+	}
+
+	err = n810bm_parse_pmm_block(bm, fw);
+	if (err)
+		goto err_release;
+	release_firmware(fw);
+
+	err = n810bm_device_init(bm);
+	if (err) {
+		dev_err(&bm->pdev->dev,
+			"Failed to initialized battery management (%d)", err);
+		goto error;
+	}
+
+	return;
+err_release:
+	release_firmware(fw);
+error:
+	return;
+}
+
+static int __devinit n810bm_probe(void)
+{
+	struct n810bm *bm;
+	int err;
+
+	if (!n810bm_retu_device || !n810bm_tahvo_device)
+		return 0;
+
+	bm = kzalloc(sizeof(*bm), GFP_KERNEL);
+	if (!bm)
+		return -ENOMEM;
+	bm->pdev = n810bm_retu_device;
+	platform_set_drvdata(n810bm_retu_device, bm);
+	platform_set_drvdata(n810bm_tahvo_device, bm);
+	mutex_init(&bm->mutex);
+	INIT_DELAYED_WORK(&bm->periodic_check_work, n810bm_periodic_check_work);
+	INIT_WORK(&bm->notify_work, n810bm_notify_work);
+
+	dev_info(&bm->pdev->dev, "Requesting CAL BME PMM block firmware file "
+		 N810BM_PMM_BLOCK_FILENAME);
+	err = request_firmware_nowait(THIS_MODULE, 1,
+				      N810BM_PMM_BLOCK_FILENAME,
+				      &bm->pdev->dev, GFP_KERNEL,
+				      bm, n810bm_pmm_block_found);
+	if (err) {
+		dev_err(&bm->pdev->dev,
+			"Failed to request CAL PMM block image file (%d)", err);
+		goto err_free;
+	}
+
+	return 0;
+
+err_free:
+	kfree(bm);
+
+	return err;
+}
+
+static void __devexit n810bm_remove(void)
+{
+	struct n810bm *bm;
+
+	if (!n810bm_retu_device || !n810bm_tahvo_device)
+		return;
+	bm = platform_get_drvdata(n810bm_retu_device);
+
+	n810bm_device_exit(bm);
+
+	kfree(bm);
+	platform_set_drvdata(n810bm_retu_device, NULL);
+	platform_set_drvdata(n810bm_tahvo_device, NULL);
+}
+
+static int __devinit n810bm_retu_probe(struct platform_device *pdev)
+{
+	n810bm_retu_device = pdev;
+	return n810bm_probe();
+}
+
+static int __devexit n810bm_retu_remove(struct platform_device *pdev)
+{
+	n810bm_remove();
+	n810bm_retu_device = NULL;
+	return 0;
+}
+
+static int __devinit n810bm_tahvo_probe(struct platform_device *pdev)
+{
+	n810bm_tahvo_device = pdev;
+	return n810bm_probe();
+}
+
+static int __devexit n810bm_tahvo_remove(struct platform_device *pdev)
+{
+	n810bm_remove();
+	n810bm_tahvo_device = NULL;
+	return 0;
+}
+
+static struct platform_driver n810bm_retu_driver = {
+	.remove		= __devexit_p(n810bm_retu_remove),
+	.driver		= {
+		.name	= "retu-n810bm",
+	}
+};
+
+static struct platform_driver n810bm_tahvo_driver = {
+	.remove		= __devexit_p(n810bm_tahvo_remove),
+	.driver		= {
+		.name	= "tahvo-n810bm",
+	}
+};
+
+static int __init n810bm_modinit(void)
+{
+	int err;
+
+	err = platform_driver_probe(&n810bm_retu_driver, n810bm_retu_probe);
+	if (err)
+		return err;
+	err = platform_driver_probe(&n810bm_tahvo_driver, n810bm_tahvo_probe);
+	if (err) {
+		platform_driver_unregister(&n810bm_retu_driver);
+		return err;
+	}
+
+	return 0;
+}
+module_init(n810bm_modinit);
+
+static void __exit n810bm_modexit(void)
+{
+	platform_driver_unregister(&n810bm_tahvo_driver);
+	platform_driver_unregister(&n810bm_retu_driver);
+}
+module_exit(n810bm_modexit);
+
+MODULE_DESCRIPTION("Nokia n810 battery management");
+MODULE_FIRMWARE(N810BM_PMM_BLOCK_FILENAME);
+MODULE_LICENSE("GPL");
+MODULE_AUTHOR("Michael Buesch");
Index: linux-3.1/drivers/cbus/lipocharge.c
===================================================================
--- /dev/null	1970-01-01 00:00:00.000000000 +0000
+++ linux-3.1/drivers/cbus/lipocharge.c	2011-11-05 17:06:51.589348749 +0100
@@ -0,0 +1,183 @@
+/*
+ *   Generic LIPO battery charger
+ *
+ *   Copyright (c) 2010-2011 Michael Buesch <mb@bu3sch.de>
+ *
+ *   This program is free software; you can redistribute it and/or
+ *   modify it under the terms of the GNU General Public License
+ *   as published by the Free Software Foundation; either version 2
+ *   of the License, or (at your option) any later version.
+ *
+ *   This program is distributed in the hope that it will be useful,
+ *   but WITHOUT ANY WARRANTY; without even the implied warranty of
+ *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+ *   GNU General Public License for more details.
+ */
+
+#define DEBUG
+
+#include "lipocharge.h"
+
+#include <linux/slab.h>
+
+
+/* Hysteresis constants */
+#define CURRENT_HYST		30 /* mA */
+#define VOLTAGE_HYST		10 /* mV */
+
+/* Threshold constants */
+#define FINISH_CURRENT_PERCENT	3
+
+
+/* Returns the requested first-stage charge current in mA */
+static inline unsigned int get_stage1_charge_current(struct lipocharge *c)
+{
+	/* current = (capacity * C) */
+	return c->capacity * c->rate / 1000;
+}
+
+void lipocharge_init(struct lipocharge *c, struct device *dev)
+{
+	c->dev = dev;
+	c->state = LIPO_IDLE;
+}
+
+void lipocharge_exit(struct lipocharge *c)
+{
+	c->state = LIPO_IDLE;
+}
+
+int lipocharge_start(struct lipocharge *c)
+{
+	int err;
+
+	if (c->state != LIPO_IDLE)
+		return -EBUSY;
+	if (!c->set_current_pwm || !c->emergency)
+		return -EINVAL;
+	if (!c->top_voltage || c->top_voltage > 4200)
+		return -EINVAL;
+
+	c->active_duty_cycle = 0;
+	err = c->set_current_pwm(c, c->active_duty_cycle);
+	if (err)
+		return err;
+	c->state = LIPO_FIRST_STAGE;
+
+	return 0;
+}
+
+void lipocharge_stop(struct lipocharge *c)
+{
+	if (c->state == LIPO_IDLE)
+		return;
+	c->state = LIPO_IDLE;
+}
+
+static int lipocharge_increase_current(struct lipocharge *c,
+				       unsigned int inc_permille)
+{
+	int old_pwm, new_pwm;
+
+	if (c->active_duty_cycle >= c->duty_cycle_max)
+		return 0;
+
+	old_pwm = c->active_duty_cycle;
+	new_pwm = old_pwm + (c->duty_cycle_max * inc_permille / 1000);
+	new_pwm = min(new_pwm, (int)c->duty_cycle_max);
+	c->active_duty_cycle = new_pwm;
+
+	dev_dbg(c->dev, "lipo: Increasing duty_cycle by "
+		"%u permille (0x%02X -> 0x%02X)",
+		inc_permille, old_pwm, new_pwm);
+
+	return c->set_current_pwm(c, c->active_duty_cycle);
+}
+
+static int lipocharge_decrease_current(struct lipocharge *c,
+				       unsigned int dec_permille)
+{
+	int old_pwm, new_pwm;
+
+	if (c->active_duty_cycle <= 0)
+		return 0;
+
+	old_pwm = c->active_duty_cycle;
+	new_pwm = old_pwm - (c->duty_cycle_max * dec_permille / 1000);
+	new_pwm = max(0, new_pwm);
+	c->active_duty_cycle = new_pwm;
+
+	dev_dbg(c->dev, "lipo: Decreasing duty_cycle by "
+		"%u permille (0x%02X -> 0x%02X)",
+		dec_permille, old_pwm, new_pwm);
+
+	return c->set_current_pwm(c, c->active_duty_cycle);
+}
+
+/** lipocharge_update_state - Update the charge state
+ * @c: The context.
+ * @voltage_mV: The measured battery voltage.
+ * @current_mA: The measured charge current.
+ *		negative -> drain.
+ *		positive -> charge.
+ * @temp_K: Battery temperature in K.
+ *
+ * Returns 0 on success, -1 on error.
+ * Returns 1, if the charging process is finished.
+ */
+int lipocharge_update_state(struct lipocharge *c,
+			    unsigned int voltage_mV,
+			    int current_mA,
+			    unsigned int temp_K)
+{
+	int requested_current, current_diff;
+	int err;
+	unsigned int permille;
+
+	//TODO temp
+
+restart:
+	switch (c->state) {
+	case LIPO_IDLE:
+		dev_err(c->dev, "%s: called while idle", __func__);
+		return -EINVAL;
+	case LIPO_FIRST_STAGE:	/* Constant current */
+//printk("GOT %u %d %u\n", voltage_mV, current_mA, temp_K);
+		if (voltage_mV >= c->top_voltage) {
+			/* Float voltage reached.
+			 * Switch charger mode to "constant current" */
+			c->state = LIPO_SECOND_STAGE;
+			dev_dbg(c->dev, "Switched to second charging stage.");
+			goto restart;
+		}
+		/* Float voltage not reached, yet.
+		 * Try to get the requested constant current. */
+		requested_current = get_stage1_charge_current(c);
+		if (current_mA < 0)
+			current_mA = 0;
+		current_diff = requested_current - current_mA;
+		if (abs(requested_current - current_mA) > CURRENT_HYST) {
+			if (current_diff > 0) {
+				/* Increase current */
+				permille = current_diff * 1000 / requested_current;
+				permille /= 2;
+				err = lipocharge_increase_current(c, permille);
+				if (err)
+					return err;
+			} else {
+				/* Decrease current */
+				permille = (-current_diff) * 1000 / requested_current;
+				permille /= 2;
+				err = lipocharge_decrease_current(c, permille);
+				if (err)
+					return err;
+			}
+		}
+		break;
+	case LIPO_SECOND_STAGE:	/* Constant voltage */
+		//TODO
+		break;
+	}
+
+	return 0;
+}
Index: linux-3.1/drivers/cbus/lipocharge.h
===================================================================
--- /dev/null	1970-01-01 00:00:00.000000000 +0000
+++ linux-3.1/drivers/cbus/lipocharge.h	2011-11-05 17:06:51.589348749 +0100
@@ -0,0 +1,60 @@
+#ifndef LIPOCHARGE_H_
+#define LIPOCHARGE_H_
+
+#include <linux/types.h>
+#include <linux/device.h>
+
+
+#define LIPORATE(a,b)	(((a) * 1000) + ((b) * 100))
+#define LIPORATE_p6C	LIPORATE(0,6)	/* 0.6C */
+
+enum lipocharge_state {
+	LIPO_IDLE = 0,		/* Not charging */
+	LIPO_FIRST_STAGE,	/* Charging: constant current */
+	LIPO_SECOND_STAGE,	/* Charging: constant voltage */
+};
+
+/** struct lipocharge - A generic LIPO charger
+ *
+ * @capacity: Battery capacity in mAh.
+ * @rate: Charge rate.
+ * @top_voltage: Fully charged voltage, in mV.
+ * @duty_cycle_max: Max value for duty_cycle.
+ *
+ * @set_charge_current: Set the charge current PWM duty cycle.
+ * @emergency: Something went wrong. Force shutdown.
+ */
+struct lipocharge {
+	unsigned int capacity;
+	unsigned int rate;
+	unsigned int top_voltage;
+	unsigned int duty_cycle_max;
+
+	int (*set_current_pwm)(struct lipocharge *c, unsigned int duty_cycle);
+	void (*emergency)(struct lipocharge *c);
+
+	/* internal */
+	struct device *dev;
+	enum lipocharge_state state;
+	unsigned int active_duty_cycle;
+
+	//TODO implement timer to cut power after maximum charge time.
+};
+
+void lipocharge_init(struct lipocharge *c, struct device *dev);
+void lipocharge_exit(struct lipocharge *c);
+
+int lipocharge_start(struct lipocharge *c);
+void lipocharge_stop(struct lipocharge *c);
+
+int lipocharge_update_state(struct lipocharge *c,
+			    unsigned int voltage_mV,
+			    int current_mA,
+			    unsigned int temp_K);
+
+static inline bool lipocharge_is_charging(struct lipocharge *c)
+{
+	return (c->state != LIPO_IDLE);
+}
+
+#endif /* LIPOCHARGE_H_ */
Index: linux-3.1/drivers/cbus/cbus.c
===================================================================
--- linux-3.1.orig/drivers/cbus/cbus.c	2011-11-05 17:03:39.610846102 +0100
+++ linux-3.1/drivers/cbus/cbus.c	2011-11-05 17:06:51.589348749 +0100
@@ -34,6 +34,7 @@
 #include <linux/gpio.h>
 #include <linux/platform_device.h>
 #include <linux/platform_data/cbus.h>
+#include <linux/reboot.h>
 
 #include "cbus.h"
 
@@ -324,6 +325,13 @@ static void __exit cbus_bus_exit(void)
 }
 module_exit(cbus_bus_exit);
 
+void cbus_emergency(void)
+{
+	machine_power_off();
+	panic("cbus: Failed to halt machine in emergency state\n");
+}
+EXPORT_SYMBOL(cbus_emergency);
+
 MODULE_DESCRIPTION("CBUS serial protocol");
 MODULE_LICENSE("GPL");
 MODULE_AUTHOR("Juha Yrjölä");
Index: linux-3.1/drivers/cbus/cbus.h
===================================================================
--- linux-3.1.orig/drivers/cbus/cbus.h	2011-11-05 17:03:39.590846131 +0100
+++ linux-3.1/drivers/cbus/cbus.h	2011-11-05 17:06:51.589348749 +0100
@@ -27,4 +27,6 @@ extern int cbus_read_reg(struct device *
 extern int cbus_write_reg(struct device *, unsigned dev, unsigned reg,
 		unsigned val);
 
+NORET_TYPE void cbus_emergency(void) ATTRIB_NORET;
+
 #endif /* __DRIVERS_CBUS_CBUS_H */
Index: linux-3.1/drivers/cbus/retu.c
===================================================================
--- linux-3.1.orig/drivers/cbus/retu.c	2011-11-05 17:03:39.606846106 +0100
+++ linux-3.1/drivers/cbus/retu.c	2011-11-05 17:06:51.589348749 +0100
@@ -417,6 +417,11 @@ static int retu_allocate_children(struct
 	if (!child)
 		return -ENOMEM;
 
+	child = retu_allocate_child("retu-n810bm", parent, irq_base,
+			RETU_INT_ADCS, -1, 1);
+	if (!child)
+		return -ENOMEM;
+
 	return 0;
 }
 
Index: linux-3.1/drivers/cbus/tahvo.c
===================================================================
--- linux-3.1.orig/drivers/cbus/tahvo.c	2011-11-05 17:04:36.274768324 +0100
+++ linux-3.1/drivers/cbus/tahvo.c	2011-11-05 17:06:51.589348749 +0100
@@ -131,6 +131,7 @@ void tahvo_set_clear_reg_bits(struct dev
 	__tahvo_write_reg(tahvo, reg, w);
 	mutex_unlock(&tahvo->mutex);
 }
+EXPORT_SYMBOL(tahvo_set_clear_reg_bits);
 
 static irqreturn_t tahvo_irq_handler(int irq, void *_tahvo)
 {
@@ -307,6 +308,11 @@ static int tahvo_allocate_children(struc
 	if (!child)
 		return -ENOMEM;
 
+	child = tahvo_allocate_child("tahvo-n810bm", parent,
+				     irq_base + TAHVO_INT_BATCURR);
+	if (!child)
+		return -ENOMEM;
+
 	return 0;
 }