aboutsummaryrefslogtreecommitdiffstats
path: root/target/linux/omap24xx/patches-2.6.37/900-n810-battery-management.patch
blob: 3f6262a357beea647349eca9491561513ab3db68 (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
---
 arch/arm/mach-omap2/board-n8x0.c |   13 +
 drivers/cbus/Kconfig             |   12 +
 drivers/cbus/Makefile            |    3 
 drivers/cbus/lipocharge.c        |   63 ++++++
 drivers/cbus/lipocharge.h        |   50 ++++
 drivers/cbus/n810bm_main.c       |  397 +++++++++++++++++++++++++++++++++++++++
 drivers/cbus/retu.c              |    4 
 drivers/cbus/retu.h              |    3 
 drivers/cbus/tahvo.h             |    6 
 9 files changed, 548 insertions(+), 3 deletions(-)

Index: linux-2.6.37/drivers/cbus/Kconfig
===================================================================
--- linux-2.6.37.orig/drivers/cbus/Kconfig	2011-01-28 22:33:39.703215389 +0100
+++ linux-2.6.37/drivers/cbus/Kconfig	2011-01-28 23:41:57.094298060 +0100
@@ -94,4 +94,12 @@
 	  to Retu/Vilma. Detection state and events are exposed through
 	  sysfs.
 
+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-2.6.37/drivers/cbus/Makefile
===================================================================
--- linux-2.6.37.orig/drivers/cbus/Makefile	2011-01-28 22:33:39.694216931 +0100
+++ linux-2.6.37/drivers/cbus/Makefile	2011-01-28 22:33:39.754206648 +0100
@@ -12,3 +12,6 @@
 obj-$(CONFIG_CBUS_TAHVO_USER)	+= tahvo-user.o
 obj-$(CONFIG_CBUS_RETU_USER)	+= retu-user.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-2.6.37/drivers/cbus/n810bm_main.c
===================================================================
--- /dev/null	1970-01-01 00:00:00.000000000 +0000
+++ linux-2.6.37/drivers/cbus/n810bm_main.c	2011-01-28 23:41:23.510064352 +0100
@@ -0,0 +1,562 @@
+/*
+ *   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.
+ */
+
+#include <linux/module.h>
+#include <linux/device.h>
+#include <linux/platform_device.h>
+#include <linux/slab.h>
+#include <linux/spinlock.h>
+#include <linux/timer.h>
+#include <linux/reboot.h>
+
+#include "retu.h"
+#include "tahvo.h"
+#include "lipocharge.h"
+
+
+#define N810BM_CHECK_INTERVAL		(HZ * 5)
+#define N810BM_MIN_VOLTAGE_THRES	3300 /* 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
+ */
+
+
+enum n810bm_capacity {
+	N810BM_CAP_UNKNOWN	= 0,
+	N810BM_CAP_1500MAH	= 1500,	/* 1500 mAh battery */
+};
+
+struct n810bm {
+	struct platform_device *pdev;
+
+	enum n810bm_capacity capacity;
+	struct timer_list check_timer;
+
+	struct lipocharge *charger;
+
+	spinlock_t lock;
+};
+
+
+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);
+	/* Force a hard shutdown. */
+	machine_power_off();
+	panic("n810bm: Failed to halt machine in emergency state\n");
+}
+
+#if 0
+static u16 retu_read(struct n810bm *bm, unsigned int reg)
+{
+	int ret;
+	unsigned long flags;
+
+	spin_lock_irqsave(&retu_lock, flags);
+	ret = retu_read_reg(reg);
+	spin_unlock_irqrestore(&retu_lock, flags);
+	if (ret < 0 || ret > 0xFFFF)
+		n810bm_emergency(bm, "retu_read");
+
+	return ret;
+}
+#endif
+
+static void retu_maskset(struct n810bm *bm, unsigned int reg, u16 mask, u16 set)
+{
+	int ret;
+	unsigned long flags;
+	u16 value;
+
+	spin_lock_irqsave(&retu_lock, flags);
+	if (~mask) {
+		ret = retu_read_reg(reg);
+		if (ret < 0 || ret > 0xFFFF)
+			goto fatal_unlock;
+		value = ret;
+	} else
+		value = 0;
+	value &= ~mask;
+	value |= set;
+	ret = retu_write_reg(reg, value);
+	if (ret)
+		goto fatal_unlock;
+	spin_unlock_irqrestore(&retu_lock, flags);
+
+	return;
+
+fatal_unlock:
+	spin_unlock_irqrestore(&retu_lock, flags);
+	n810bm_emergency(bm, "retu_maskset");
+}
+
+static inline void retu_write(struct n810bm *bm, unsigned int reg, u16 value)
+{
+	return retu_maskset(bm, reg, 0xFFFF, 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(chan);
+		if (ret < 0)
+			return ret;
+		value += ret;
+	}
+	value /= nr_passes;
+
+	return value;
+}
+
+static int adc_sanity_check(struct n810bm *bm, unsigned int channel)
+{
+	int value;
+
+	value = retu_read_adc(channel);
+	if (value < 0) {
+		dev_err(&bm->pdev->dev, "Failed to read GND ADC channel %u",
+			channel);
+		return -EIO;
+	}
+	dev_info(&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(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;
+
+	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_check_timer(unsigned long data)
+{
+	struct n810bm *bm = (struct n810bm *)data;
+	unsigned long flags;
+	int mv;
+
+	spin_lock_irqsave(&bm->lock, flags);
+
+	mv = n810bm_measure_batt_voltage(bm);
+	if (mv < 0)
+		n810bm_emergency(bm, "check timer: Failed to measure");
+	if (mv < N810BM_MIN_VOLTAGE_THRES)
+		n810bm_emergency(bm, "check timer: Minimum voltage threshold reached");
+
+	mod_timer(&bm->check_timer, round_jiffies(jiffies + N810BM_CHECK_INTERVAL));
+	spin_unlock_irqrestore(&bm->lock, flags);
+
+	return;
+}
+
+static void n810bm_adc_irq_handler(unsigned long data)
+{
+//	struct n810bm *bm = (struct n810bm *)data;
+
+	retu_ack_irq(RETU_INT_ADCS);
+	//TODO
+printk("n810bm: ADC timer triggered\n");
+}
+
+static ssize_t n810bm_attr_charge_show(struct device *dev,
+				       struct device_attribute *attr,
+				       char *buf)
+{
+	struct platform_device *pdev = to_platform_device(dev);
+	struct n810bm *bm = platform_get_drvdata(pdev);
+	int err = -ENODEV;
+	ssize_t count = 0;
+	int millivolt;
+
+	spin_lock_irq(&bm->lock);
+	millivolt = n810bm_measure_batt_voltage(bm);
+	if (millivolt >= 0) {
+		count = snprintf(buf, PAGE_SIZE, "%u\n",
+				 n810bm_mvolt2percent(millivolt));
+		err = 0;
+	}
+	spin_unlock_irq(&bm->lock);
+
+	return err ? err : count;
+}
+static DEVICE_ATTR(batt_charge, 0444, n810bm_attr_charge_show, NULL);
+
+static ssize_t n810bm_attr_capacity_show(struct device *dev,
+					 struct device_attribute *attr,
+					 char *buf)
+{
+	struct platform_device *pdev = to_platform_device(dev);
+	struct n810bm *bm = platform_get_drvdata(pdev);
+	ssize_t count;
+
+	spin_lock_irq(&bm->lock);
+	count = snprintf(buf, PAGE_SIZE, "%u\n",
+			 (unsigned int)bm->capacity);
+	spin_unlock_irq(&bm->lock);
+
+	return count;
+}
+static DEVICE_ATTR(batt_capacity, 0444, n810bm_attr_capacity_show, NULL);
+
+static ssize_t n810bm_attr_battemp_show(struct device *dev,
+					struct device_attribute *attr,
+					char *buf)
+{
+	struct platform_device *pdev = to_platform_device(dev);
+	struct n810bm *bm = platform_get_drvdata(pdev);
+	ssize_t count = 0;
+	int k, err = -ENODEV;
+
+	spin_lock_irq(&bm->lock);
+	k = n810bm_measure_batt_temp(bm);
+	if (k >= 0) {
+		count = snprintf(buf, PAGE_SIZE, "%d\n", k);
+		err = 0;
+	}
+	spin_unlock_irq(&bm->lock);
+
+	return err ? err : count;
+}
+static DEVICE_ATTR(batt_temp, 0444, n810bm_attr_battemp_show, NULL);
+
+static ssize_t n810bm_attr_charger_voltage(struct device *dev,
+					   struct device_attribute *attr,
+					   char *buf)
+{
+	struct platform_device *pdev = to_platform_device(dev);
+	struct n810bm *bm = platform_get_drvdata(pdev);
+	ssize_t count = 0;
+	int mv, err = -ENODEV;
+
+	spin_lock_irq(&bm->lock);
+	mv = n810bm_measure_charger_voltage(bm);
+	if (mv >= 0) {
+		count = snprintf(buf, PAGE_SIZE, "%d\n", mv);
+		err = 0;
+	}
+	spin_unlock_irq(&bm->lock);
+
+	return err ? err : count;
+}
+static DEVICE_ATTR(charger_voltage, 0444, n810bm_attr_charger_voltage, NULL);
+
+static ssize_t n810bm_attr_backup_batt_voltage(struct device *dev,
+					       struct device_attribute *attr,
+					       char *buf)
+{
+	struct platform_device *pdev = to_platform_device(dev);
+	struct n810bm *bm = platform_get_drvdata(pdev);
+	ssize_t count = 0;
+	int mv, err = -ENODEV;
+
+	spin_lock_irq(&bm->lock);
+	mv = n810bm_measure_backup_batt_voltage(bm);
+	if (mv >= 0) {
+		count = snprintf(buf, PAGE_SIZE, "%d\n", mv);
+		err = 0;
+	}
+	spin_unlock_irq(&bm->lock);
+
+	return err ? err : count;
+}
+static DEVICE_ATTR(backup_batt_voltage, 0444, n810bm_attr_backup_batt_voltage, NULL);
+
+static void n810bm_hw_exit(struct n810bm *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)
+		goto error;
+	bm->capacity = n810bm_read_batt_capacity(bm);
+	if (bm->capacity == N810BM_CAP_UNKNOWN) {
+		dev_err(&bm->pdev->dev, "Unknown battery detected");
+		err = -ENODEV;
+		goto error;
+	}
+	dev_info(&bm->pdev->dev, "Detected %u mAh battery\n",
+		 (unsigned int)bm->capacity);
+
+	return 0;
+
+error:
+	return err;
+}
+
+static int __devinit n810bm_probe(struct platform_device *pdev)
+{
+	struct n810bm *bm;
+	int err;
+
+	bm = kzalloc(sizeof(*bm), GFP_KERNEL);
+	if (!bm)
+		return -ENOMEM;
+	bm->pdev = pdev;
+	platform_set_drvdata(pdev, bm);
+	spin_lock_init(&bm->lock);
+	setup_timer(&bm->check_timer, n810bm_check_timer, (unsigned long)bm);
+
+	err = n810bm_hw_init(bm);
+	if (err)
+		goto err_free;
+	err = device_create_file(&pdev->dev, &dev_attr_batt_charge);
+	if (err)
+		goto err_exit;
+	err = device_create_file(&pdev->dev, &dev_attr_batt_capacity);
+	if (err)
+		goto err_rem_charge;
+	err = device_create_file(&pdev->dev, &dev_attr_batt_temp);
+	if (err)
+		goto err_rem_capa;
+	err = device_create_file(&pdev->dev, &dev_attr_charger_voltage);
+	if (err)
+		goto err_rem_temp;
+	err = device_create_file(&pdev->dev, &dev_attr_backup_batt_voltage);
+	if (err)
+		goto err_rem_chg;
+	err = retu_request_irq(RETU_INT_ADCS, n810bm_adc_irq_handler,
+			       (unsigned long)bm, "n810bm");
+	if (err)
+		goto err_rem_bkup;
+
+	mod_timer(&bm->check_timer, round_jiffies(jiffies + N810BM_CHECK_INTERVAL));
+
+	dev_info(&pdev->dev, "Battery management initialized");
+
+	return 0;
+
+err_rem_bkup:
+	device_remove_file(&pdev->dev, &dev_attr_backup_batt_voltage);
+err_rem_chg:
+	device_remove_file(&pdev->dev, &dev_attr_charger_voltage);
+err_rem_temp:
+	device_remove_file(&pdev->dev, &dev_attr_batt_temp);
+err_rem_capa:
+	device_remove_file(&pdev->dev, &dev_attr_batt_capacity);
+err_rem_charge:
+	device_remove_file(&pdev->dev, &dev_attr_batt_charge);
+err_exit:
+	n810bm_hw_exit(bm);
+err_free:
+	kfree(bm);
+	platform_set_drvdata(pdev, NULL);
+	return err;
+}
+
+static int __devexit n810bm_remove(struct platform_device *pdev)
+{
+	struct n810bm *bm = platform_get_drvdata(pdev);
+
+	retu_free_irq(RETU_INT_ADCS);
+	del_timer_sync(&bm->check_timer);
+	device_remove_file(&pdev->dev, &dev_attr_backup_batt_voltage);
+	device_remove_file(&pdev->dev, &dev_attr_charger_voltage);
+	device_remove_file(&pdev->dev, &dev_attr_batt_temp);
+	device_remove_file(&pdev->dev, &dev_attr_batt_capacity);
+	device_remove_file(&pdev->dev, &dev_attr_batt_charge);
+	n810bm_hw_exit(bm);
+
+	kfree(bm);
+	platform_set_drvdata(pdev, NULL);
+
+	return 0;
+}
+
+static struct platform_driver n810bm_driver = {
+	.remove		= __devexit_p(n810bm_remove),
+	.driver		= {
+		.name	= "n810bm",
+	}
+};
+
+static int __init n810bm_modinit(void)
+{
+	return platform_driver_probe(&n810bm_driver, n810bm_probe);
+}
+module_init(n810bm_modinit);
+
+static void __exit n810bm_modexit(void)
+{
+	platform_driver_unregister(&n810bm_driver);
+}
+module_exit(n810bm_modexit);
+
+MODULE_DESCRIPTION("Nokia n810 battery management");
+MODULE_LICENSE("GPL");
+MODULE_AUTHOR("Michael Buesch");
Index: linux-2.6.37/drivers/cbus/retu.c
===================================================================
--- linux-2.6.37.orig/drivers/cbus/retu.c	2011-01-28 22:33:39.695216760 +0100
+++ linux-2.6.37/drivers/cbus/retu.c	2011-01-28 22:33:39.754206648 +0100
@@ -85,10 +85,10 @@
  *
  * This function writes a value to the specified register
  */
-void retu_write_reg(int reg, u16 val)
+int retu_write_reg(int reg, u16 val)
 {
 	BUG_ON(!retu_initialized);
-	cbus_write_reg(cbus_host, RETU_ID, reg, val);
+	return cbus_write_reg(cbus_host, RETU_ID, reg, val);
 }
 
 void retu_set_clear_reg_bits(int reg, u16 set, u16 clear)
Index: linux-2.6.37/drivers/cbus/retu.h
===================================================================
--- linux-2.6.37.orig/drivers/cbus/retu.h	2011-01-28 22:33:39.695216760 +0100
+++ linux-2.6.37/drivers/cbus/retu.h	2011-01-28 22:40:55.380584650 +0100
@@ -39,6 +39,7 @@
 #define RETU_REG_CC2		0x0e	/* Common control register 2 */
 #define RETU_REG_CTRL_CLR	0x0f	/* Regulator clear register */
 #define RETU_REG_CTRL_SET	0x10	/* Regulator set register */
+#define RETU_REG_UNK1		0x14	/* 0x1000 is set when charger is plugged in */
 #define RETU_REG_STATUS		0x16	/* Status register */
 #define RETU_REG_WATCHDOG	0x17	/* Watchdog register */
 #define RETU_REG_AUDTXR		0x18	/* Audio Codec Tx register */
@@ -57,8 +58,25 @@
 
 #define	MAX_RETU_IRQ_HANDLERS	16
 
+/* ADC channels */
+#define RETU_ADC_GND		0x00 /* Ground */
+#define RETU_ADC_BSI		0x01 /* Battery Size Indicator */
+#define RETU_ADC_BATTEMP	0x02 /* Battery temperature */
+#define RETU_ADC_CHGVOLT	0x03 /* Charger voltage */
+#define RETU_ADC_HEADSET	0x04 /* Headset detection */
+#define RETU_ADC_HOOKDET	0x05 /* Hook detection */
+#define RETU_ADC_RFGP		0x06 /* RF GP */
+#define RETU_ADC_WBTX		0x07 /* Wideband Tx detection */
+#define RETU_ADC_BATTVOLT	0x08 /* Battery voltage measurement */
+#define RETU_ADC_GND2		0x09 /* Ground */
+#define RETU_ADC_LIGHTSENS	0x0A /* Light sensor */
+#define RETU_ADC_LIGHTTEMP	0x0B /* Light sensor temperature */
+#define RETU_ADC_BKUPVOLT	0x0C /* Backup battery voltage */
+#define RETU_ADC_TEMP		0x0D /* RETU temperature */
+
+
 int retu_read_reg(int reg);
-void retu_write_reg(int reg, u16 val);
+int retu_write_reg(int reg, u16 val);
 void retu_set_clear_reg_bits(int reg, u16 set, u16 clear);
 int retu_read_adc(int channel);
 int retu_request_irq(int id, void *irq_handler, unsigned long arg, char *name);
Index: linux-2.6.37/arch/arm/mach-omap2/board-n8x0.c
===================================================================
--- linux-2.6.37.orig/arch/arm/mach-omap2/board-n8x0.c	2011-01-28 22:33:39.679219500 +0100
+++ linux-2.6.37/arch/arm/mach-omap2/board-n8x0.c	2011-01-28 22:33:39.754206648 +0100
@@ -907,6 +907,17 @@
 				    ARRAY_SIZE(n8x0_gpio_switches));
 }
 
+static struct platform_device n810_bm_device = {
+	.name		= "n810bm",
+	.id		= -1,
+};
+
+static void __init n810_bm_init(void)
+{
+	if (platform_device_register(&n810_bm_device))
+		BUG();
+}
+
 static void __init n8x0_init_machine(void)
 {
 	omap2420_mux_init(board_mux, OMAP_PACKAGE_ZAC);
@@ -933,6 +944,8 @@
 	n8x0_onenand_init();
 	n8x0_mmc_init();
 	n8x0_usb_init();
+
+	n810_bm_init();
 }
 
 MACHINE_START(NOKIA_N800, "Nokia N800")
Index: linux-2.6.37/drivers/cbus/lipocharge.c
===================================================================
--- /dev/null	1970-01-01 00:00:00.000000000 +0000
+++ linux-2.6.37/drivers/cbus/lipocharge.c	2011-01-28 22:33:39.755206476 +0100
@@ -0,0 +1,63 @@
+/*
+ *   Generic LIPO battery charger
+ *
+ *   Copyright (c) 2010 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.
+ */
+
+#include "lipocharge.h"
+
+#include <linux/slab.h>
+
+
+static void lipocharge_timer(unsigned long data)
+{
+	struct lipocharge *c = (struct lipocharge *)data;
+
+	spin_lock(&c->lock);
+	//TODO
+	spin_unlock(&c->lock);
+}
+
+struct lipocharge * lipocharge_alloc(gfp_t gfp)
+{
+	struct lipocharge *c;
+
+	c = kzalloc(sizeof(*c), gfp);
+	if (!c)
+		return NULL;
+	spin_lock_init(&c->lock);
+	setup_timer(&c->timer, lipocharge_timer, (unsigned long)c);
+
+	return c;
+}
+
+void lipocharge_free(struct lipocharge *c)
+{
+	kfree(c);
+}
+
+int lipocharge_start(struct lipocharge *c)
+{
+	if (!c->set_current || !c->get_voltage ||
+	    !c->finished || !c->emergency)
+		return -EINVAL;
+	if (!c->top_voltage || c->top_voltage > 4200)
+		return -EINVAL;
+	//TODO
+}
+
+void lipocharge_stop(struct lipocharge *c)
+{
+	del_timer_sync(&c->timer);
+	//TODO
+}
Index: linux-2.6.37/drivers/cbus/lipocharge.h
===================================================================
--- /dev/null	1970-01-01 00:00:00.000000000 +0000
+++ linux-2.6.37/drivers/cbus/lipocharge.h	2011-01-28 22:33:39.755206476 +0100
@@ -0,0 +1,50 @@
+#ifndef LIPOCHARGE_H_
+#define LIPOCHARGE_H_
+
+#include <linux/timer.h>
+#include <linux/spinlock.h>
+
+
+#define LIPORATE(a,b)	(((a) * 1000) + (b))
+#define LIPORATE_1C	LIPORATE(1,0)	/* 1C */
+#define LIPORATE_p8C	LIPORATE(0,8)	/* 0.8C */
+
+/** struct lipocharge - A generic LIPO charger
+ *
+ * @capacity: Battery capacity in mAh.
+ * @rate: Charge rate.
+ * @top_voltage: Fully charged voltage, in mV.
+ *
+ * @set_current: Set the charge current, in mA.
+ * @get_voltage: Get the battery voltage, in mV.
+ *
+ * @emergency: Something went wrong. Force shutdown.
+ *
+ * @priv: opaque pointer.
+ */
+struct lipocharge
+{
+	unsigned int capacity;
+	unsigned int rate;
+	unsigned int top_voltage;
+
+	int (*set_current)(struct lipocharge *c, unsigned int ma);
+	int (*get_voltage)(struct lipocharge *c, unsigned int *mv);
+
+	void (*finished)(struct lipocharge *c);
+	void (*emergency)(struct lipocharge *c);
+
+	void *priv;
+
+	/* internal */
+	spinlock_t lock;
+	struct timer_list timer;
+};
+
+struct lipocharge * lipocharge_alloc(gfp_t gfp);
+void lipocharge_free(struct lipocharge *c);
+
+int lipocharge_start(struct lipocharge *c);
+void lipocharge_stop(struct lipocharge *c);
+
+#endif /* LIPOCHARGE_H_ */
Index: linux-2.6.37/drivers/cbus/tahvo.h
===================================================================
--- linux-2.6.37.orig/drivers/cbus/tahvo.h	2011-01-28 22:33:39.696216589 +0100
+++ linux-2.6.37/drivers/cbus/tahvo.h	2011-01-28 22:33:39.755206476 +0100
@@ -30,8 +30,14 @@
 #define TAHVO_REG_IDR		0x01	/* Interrupt ID */
 #define TAHVO_REG_IDSR		0x02	/* Interrupt status */
 #define TAHVO_REG_IMR		0x03	/* Interrupt mask */
+#define TAHVO_REG_CHGCURR	0x04	/* Charge current control (8-bit) */
 #define TAHVO_REG_LEDPWMR	0x05	/* LED PWM */
 #define TAHVO_REG_USBR		0x06	/* USB control */
+#define TAHVO_REG_CHGCTL	0x08	/* Charge control register */
+#define  TAHVO_REG_CHGCTL_EN	0x0001	/* Global charge enable */
+#define TAHVO_REG_CHGCTL2	0x0c	/* Charge control register 2 */
+#define TAHVO_REG_BATCURR	0x0d	/* Battery (dis)charge current (signed 16-bit) */
+
 #define TAHVO_REG_MAX		0x0d
 
 /* Interrupt sources */