2 arch/arm/mach-omap2/board-n8x0.c | 13 +
3 drivers/cbus/Kconfig | 12 +
4 drivers/cbus/Makefile | 3
5 drivers/cbus/lipocharge.c | 63 ++++++
6 drivers/cbus/lipocharge.h | 50 ++++
7 drivers/cbus/n810bm_main.c | 397 +++++++++++++++++++++++++++++++++++++++
8 drivers/cbus/retu.c | 4
9 drivers/cbus/retu.h | 3
10 drivers/cbus/tahvo.h | 6
11 9 files changed, 548 insertions(+), 3 deletions(-)
13 Index: linux-2.6.37.1/drivers/cbus/Kconfig
14 ===================================================================
15 --- linux-2.6.37.1.orig/drivers/cbus/Kconfig 2011-02-19 20:26:01.282338290 +0100
16 +++ linux-2.6.37.1/drivers/cbus/Kconfig 2011-02-19 20:26:01.526318768 +0100
18 to Retu/Vilma. Detection state and events are exposed through
22 + depends on CBUS_RETU && CBUS_TAHVO
23 + tristate "Nokia n810 battery management"
25 + Nokia n810 device battery management.
30 Index: linux-2.6.37.1/drivers/cbus/Makefile
31 ===================================================================
32 --- linux-2.6.37.1.orig/drivers/cbus/Makefile 2011-02-19 20:26:01.250340850 +0100
33 +++ linux-2.6.37.1/drivers/cbus/Makefile 2011-02-19 20:26:01.526318768 +0100
35 obj-$(CONFIG_CBUS_TAHVO_USER) += tahvo-user.o
36 obj-$(CONFIG_CBUS_RETU_USER) += retu-user.o
37 obj-$(CONFIG_CBUS_RETU_HEADSET) += retu-headset.o
38 +n810bm-y += n810bm_main.o
39 +n810bm-y += lipocharge.o
40 +obj-$(CONFIG_N810BM) += n810bm.o
41 Index: linux-2.6.37.1/drivers/cbus/n810bm_main.c
42 ===================================================================
43 --- /dev/null 1970-01-01 00:00:00.000000000 +0000
44 +++ linux-2.6.37.1/drivers/cbus/n810bm_main.c 2011-02-20 15:11:55.764356685 +0100
47 + * Nokia n810 battery management
49 + * WARNING: This driver is based on unconfirmed documentation.
50 + * It is possibly dangerous to use this software.
51 + * Use this software at your own risk!
53 + * Copyright (c) 2010-2011 Michael Buesch <mb@bu3sch.de>
55 + * This program is free software; you can redistribute it and/or
56 + * modify it under the terms of the GNU General Public License
57 + * as published by the Free Software Foundation; either version 2
58 + * of the License, or (at your option) any later version.
60 + * This program is distributed in the hope that it will be useful,
61 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
62 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
63 + * GNU General Public License for more details.
68 +#include <linux/module.h>
69 +#include <linux/device.h>
70 +#include <linux/platform_device.h>
71 +#include <linux/slab.h>
72 +#include <linux/mutex.h>
73 +#include <linux/timer.h>
74 +#include <linux/firmware.h>
75 +#include <linux/bitops.h>
76 +#include <linux/workqueue.h>
77 +#include <linux/delay.h>
82 +#include "lipocharge.h"
85 +#define N810BM_PMM_BLOCK_FILENAME "n810-cal-bme-pmm.fw"
86 +#define N810BM_PMM_BLOCK_SIZE 0x600
87 +#define N810BM_PMM_GROUP_SIZE 0x200
88 +#define N810BM_PMM_ELEM_SIZE 0x10
90 +#define N810BM_CHECK_INTERVAL (HZ * 2)
91 +#define N810BM_MIN_VOLTAGE_THRES 3200 /* Absolute minimum voltage threshold */
95 + * The battery size indicator ADC measures the resistance between
96 + * the battery BSI pin and ground. This is used to detect the battery
97 + * capacity, as the BSI resistor is related to capacity.
99 + * Manually measured lookup table.
100 + * Hard to measure, thus not very accurate.
102 + * Resistance | ADC value
103 + * ========================
110 +/* RETU_ADC_BATTVOLT
111 + * Manually measured lookup table.
112 + * Hard to measure, thus not very accurate.
114 + * Voltage | ADC value
115 + * =====================
134 +enum n810bm_pmm_adc_id {
135 + N810BM_PMM_ADC_0x01 = 0x01,
136 + N810BM_PMM_ADC_0x02 = 0x02,
137 + N810BM_PMM_ADC_0x03 = 0x03,
138 + N810BM_PMM_ADC_0x04 = 0x04,
139 + N810BM_PMM_ADC_BATTEMP = 0x05,
140 + N810BM_PMM_ADC_0x06 = 0x06,
141 + N810BM_PMM_ADC_0x07 = 0x07,
142 + N810BM_PMM_ADC_0x08 = 0x08,
143 + N810BM_PMM_ADC_0x0E = 0x0E,
144 + N810BM_PMM_ADC_0x13 = 0x13,
145 + N810BM_PMM_ADC_0x14 = 0x14,
146 + N810BM_PMM_ADC_0x15 = 0x15,
147 + N810BM_PMM_ADC_0x16 = 0x16,
148 + N810BM_PMM_ADC_0x17 = 0x17,
149 + N810BM_PMM_ADC_0xFE = 0xFE,
152 +struct n810bm_adc_calib {
153 + enum n810bm_pmm_adc_id id;
162 +struct n810bm_calib {
163 + struct n810bm_adc_calib adc[25];
166 +enum n810bm_capacity {
167 + N810BM_CAP_UNKNOWN = -1,
168 + N810BM_CAP_NONE = 0,
169 + N810BM_CAP_1500MAH = 1500, /* 1500 mAh battery */
172 +enum n810bm_notify_flags {
173 + N810BM_NOTIFY_battery_charging,
174 + N810BM_NOTIFY_charger_pwm,
178 + bool battery_present; /* A battery is inserted */
179 + bool charger_present; /* The charger is connected */
180 + enum n810bm_capacity capacity; /* The capacity of the inserted battery (if any) */
182 + bool charger_enabled; /* Want to charge? */
183 + struct lipocharge charger; /* Charger subsystem */
184 + unsigned int active_current_pwm; /* Active value of TAHVO_REG_CHGCURR */
185 + int current_measure_enabled; /* Current measure enable refcount */
187 + struct platform_device *pdev;
188 + struct n810bm_calib calib; /* Calibration data */
190 + bool verbose_charge_log; /* Verbose charge logging */
192 + unsigned long notify_flags;
193 + struct work_struct notify_work;
194 + struct work_struct currmeas_irq_work;
195 + struct delayed_work periodic_check_work;
197 + bool initialized; /* The hardware was initialized */
198 + struct mutex mutex;
201 +static void n810bm_notify_battery_charging(struct n810bm *bm);
202 +static void n810bm_notify_charger_pwm(struct n810bm *bm);
205 +static inline struct n810bm * device_to_n810bm(struct device *dev)
207 + struct platform_device *pdev = to_platform_device(dev);
208 + struct n810bm *bm = platform_get_drvdata(pdev);
213 +static inline bool n810bm_known_battery_present(struct n810bm *bm)
215 + return bm->battery_present &&
216 + bm->capacity != N810BM_CAP_UNKNOWN &&
217 + bm->capacity != N810BM_CAP_NONE;
220 +static NORET_TYPE void n810bm_emergency(struct n810bm *bm, const char *message) ATTRIB_NORET;
221 +static void n810bm_emergency(struct n810bm *bm, const char *message)
223 + printk(KERN_EMERG "n810 battery management fatal fault: %s\n", message);
227 +static u16 tahvo_read(struct n810bm *bm, unsigned int reg)
230 + unsigned long flags;
232 + spin_lock_irqsave(&tahvo_lock, flags);
233 + ret = tahvo_read_reg(reg);
234 + spin_unlock_irqrestore(&tahvo_lock, flags);
235 + if (ret < 0 || ret > 0xFFFF)
236 + n810bm_emergency(bm, "tahvo_read");
241 +static void tahvo_maskset(struct n810bm *bm, unsigned int reg, u16 mask, u16 set)
244 + unsigned long flags;
247 + spin_lock_irqsave(&tahvo_lock, flags);
249 + ret = tahvo_read_reg(reg);
250 + if (ret < 0 || ret > 0xFFFF)
257 + ret = tahvo_write_reg(reg, value);
260 + spin_unlock_irqrestore(&tahvo_lock, flags);
265 + spin_unlock_irqrestore(&tahvo_lock, flags);
266 + n810bm_emergency(bm, "tahvo_maskset");
269 +static inline void tahvo_write(struct n810bm *bm, unsigned int reg, u16 value)
271 + tahvo_maskset(bm, reg, 0xFFFF, value);
274 +static inline void tahvo_set(struct n810bm *bm, unsigned int reg, u16 mask)
276 + tahvo_maskset(bm, reg, mask, mask);
279 +static inline void tahvo_clear(struct n810bm *bm, unsigned int reg, u16 mask)
281 + tahvo_maskset(bm, reg, mask, 0);
284 +static u16 retu_read(struct n810bm *bm, unsigned int reg)
287 + unsigned long flags;
289 + spin_lock_irqsave(&retu_lock, flags);
290 + ret = retu_read_reg(reg);
291 + spin_unlock_irqrestore(&retu_lock, flags);
292 + if (ret < 0 || ret > 0xFFFF)
293 + n810bm_emergency(bm, "retu_read");
298 +static void retu_maskset(struct n810bm *bm, unsigned int reg, u16 mask, u16 set)
301 + unsigned long flags;
304 + spin_lock_irqsave(&retu_lock, flags);
306 + ret = retu_read_reg(reg);
307 + if (ret < 0 || ret > 0xFFFF)
314 + ret = retu_write_reg(reg, value);
317 + spin_unlock_irqrestore(&retu_lock, flags);
322 + spin_unlock_irqrestore(&retu_lock, flags);
323 + n810bm_emergency(bm, "retu_maskset");
326 +static inline void retu_write(struct n810bm *bm, unsigned int reg, u16 value)
328 + retu_maskset(bm, reg, 0xFFFF, value);
331 +static int retu_adc_average(struct n810bm *bm, unsigned int chan,
332 + unsigned int nr_passes)
334 + unsigned int i, value = 0;
337 + if (WARN_ON(!nr_passes))
339 + for (i = 0; i < nr_passes; i++) {
340 + ret = retu_read_adc(chan);
345 + value /= nr_passes;
350 +static int pmm_record_get(struct n810bm *bm,
351 + const struct firmware *pmm_block,
352 + void *buffer, size_t length,
353 + unsigned int group, unsigned int element, unsigned int offset)
355 + const u8 *pmm_area = pmm_block->data;
356 + u8 active_group_mask;
358 + if (pmm_block->size != N810BM_PMM_BLOCK_SIZE)
360 + if (group >= N810BM_PMM_BLOCK_SIZE / N810BM_PMM_GROUP_SIZE)
362 + if (element >= N810BM_PMM_GROUP_SIZE / N810BM_PMM_ELEM_SIZE)
364 + if (offset >= N810BM_PMM_ELEM_SIZE || length > N810BM_PMM_ELEM_SIZE ||
365 + length + offset > N810BM_PMM_ELEM_SIZE)
368 + active_group_mask = pmm_area[16];
369 + if (!(active_group_mask & (1 << group))) {
370 + dev_dbg(&bm->pdev->dev, "pwm_record_get: Requested group %u, "
371 + "but group is not active", group);
376 + pmm_area + group * N810BM_PMM_GROUP_SIZE
377 + + element * N810BM_PMM_ELEM_SIZE
384 +/* PMM block group 1 element */
385 +struct group1_element {
394 +static void extract_group1_elem(struct n810bm *bm,
395 + const struct firmware *pmm_block,
396 + const u8 *pmm_adc_ids, size_t nr_pmm_adc_ids,
397 + u32 field1_mask, u32 field2_mask)
399 + struct group1_element elem;
401 + unsigned int i, element_nr;
402 + struct n810bm_adc_calib *adc_calib;
404 + for (i = 0; i < nr_pmm_adc_ids; i++) {
405 + element_nr = pmm_adc_ids[i] + 3;
407 + err = pmm_record_get(bm, pmm_block, &elem, sizeof(elem),
411 + WARN_ON(element_nr - 3 + 1 >= ARRAY_SIZE(bm->calib.adc));
412 + adc_calib = &bm->calib.adc[element_nr - 3 + 1];
414 + if (adc_calib->flags == elem.flags) {
415 + WARN_ON(adc_calib->id != elem.id);
416 + adc_calib->field1 = le32_to_cpu(elem.field1) & field1_mask;
417 + adc_calib->field2 = le32_to_cpu(elem.field2) & field2_mask;
422 +static int n810bm_parse_pmm_group1(struct n810bm *bm,
423 + const struct firmware *pmm_block)
425 + struct group1_element elem;
428 + static const u8 pmm_adc_ids_0[] = {
429 + N810BM_PMM_ADC_0x01,
430 + N810BM_PMM_ADC_0x02,
431 + N810BM_PMM_ADC_0x13,
432 + N810BM_PMM_ADC_0x0E,
434 + static const u8 pmm_adc_ids_1[] = {
435 + N810BM_PMM_ADC_0x04,
437 + static const u8 pmm_adc_ids_2[] = {
438 + N810BM_PMM_ADC_BATTEMP,
441 + /* Parse element 2 */
442 + err = pmm_record_get(bm, pmm_block, &elem, sizeof(elem),
445 + dev_err(&bm->pdev->dev,
446 + "PMM: Failed to get group 1 / element 2");
449 + if (elem.id == N810BM_PMM_ADC_0xFE && elem.flags == 0x05) {
450 + bm->calib.adc[0].id = elem.id;
451 + bm->calib.adc[0].flags = elem.flags;
452 + bm->calib.adc[0].field1 = le32_to_cpu(elem.field1);
453 + bm->calib.adc[0].field2 = le32_to_cpu(elem.field2);
456 + extract_group1_elem(bm, pmm_block,
457 + pmm_adc_ids_0, ARRAY_SIZE(pmm_adc_ids_0),
458 + 0xFFFFFFFF, 0xFFFFFFFF);
459 + extract_group1_elem(bm, pmm_block,
460 + pmm_adc_ids_1, ARRAY_SIZE(pmm_adc_ids_1),
462 + extract_group1_elem(bm, pmm_block,
463 + pmm_adc_ids_2, ARRAY_SIZE(pmm_adc_ids_2),
464 + 0xFFFFFFFF, 0x0000FFFF);
469 +static int n810bm_parse_pmm_group2(struct n810bm *bm,
470 + const struct firmware *pmm_block)
472 + dev_err(&bm->pdev->dev, "TODO: CAL BME PMM group 2 parser not implemented, yet");
473 + return -EOPNOTSUPP;
476 +static void n810bm_adc_calib_set_defaults(struct n810bm *bm)
478 + struct n810bm_adc_calib *adc_calib;
479 + const struct n810bm_adc_calib *def;
480 + unsigned int i, index;
482 + static const struct n810bm_adc_calib defaults[] = {
483 + /* ADC group-nr 0 */
485 + .id = N810BM_PMM_ADC_0x06,
489 + .id = N810BM_PMM_ADC_0x07,
493 + .id = N810BM_PMM_ADC_0x15,
497 + .id = N810BM_PMM_ADC_0x08,
501 + .id = N810BM_PMM_ADC_0x16,
505 + .id = N810BM_PMM_ADC_0x17,
509 + .id = N810BM_PMM_ADC_0x03,
513 + /* ADC group-nr 1 */
515 + .id = N810BM_PMM_ADC_0xFE,
521 + .id = N810BM_PMM_ADC_0x01,
527 + .id = N810BM_PMM_ADC_0x02,
533 + .id = N810BM_PMM_ADC_0x13,
539 + .id = N810BM_PMM_ADC_0x0E,
545 + /* ADC group-nr 2 */
547 + .id = N810BM_PMM_ADC_0x04,
553 + /* ADC group-nr 3 */
555 + .id = N810BM_PMM_ADC_BATTEMP,
558 + .field1 = 265423000,
561 + /* ADC group-nr 4 */
563 + .id = N810BM_PMM_ADC_0x14,
566 + .field1 = 19533778,
567 + .field2 = 308019670,
573 + /* Clear the array */
574 + memset(&bm->calib.adc, 0, sizeof(bm->calib.adc));
575 + for (i = 0; i < ARRAY_SIZE(bm->calib.adc); i++)
576 + bm->calib.adc[i].flags = 0xFF;
578 + /* Copy the defaults */
579 + for (i = 0; i < ARRAY_SIZE(defaults); i++) {
580 + def = &defaults[i];
583 + if (def->id != N810BM_PMM_ADC_0xFE)
584 + index = def->id + 1;
585 + WARN_ON(index >= ARRAY_SIZE(bm->calib.adc));
587 + adc_calib = &bm->calib.adc[index];
592 +static const struct n810bm_adc_calib * n810bm_get_adc_calib(struct n810bm *bm,
593 + enum n810bm_pmm_adc_id id)
595 + unsigned int index = 0;
597 + if (id != N810BM_PMM_ADC_0xFE)
598 + index = (unsigned int)id + 1;
599 + WARN_ON(index >= ARRAY_SIZE(bm->calib.adc));
601 + return &bm->calib.adc[index];
604 +static int n810bm_parse_pmm_block(struct n810bm *bm,
605 + const struct firmware *pmm_block)
609 + unsigned int i, count;
610 + struct n810bm_adc_calib *adc_calib;
612 + /* Initialize to defaults */
613 + n810bm_adc_calib_set_defaults(bm);
615 + /* Parse the PMM data */
616 + err = pmm_record_get(bm, pmm_block, &byte, sizeof(byte),
617 + 1, 0, 0); /* group 1 / element 0 */
618 + err |= (byte != 0x01);
619 + err |= pmm_record_get(bm, pmm_block, &byte, sizeof(byte),
620 + 1, 1, 0); /* group 1 / element 1 */
621 + err |= (byte != 0x01);
623 + err = n810bm_parse_pmm_group2(bm, pmm_block);
625 + err = n810bm_parse_pmm_group1(bm, pmm_block);
629 + /* Sanity checks */
630 + for (i = 0, count = 0; i < ARRAY_SIZE(bm->calib.adc); i++) {
631 + adc_calib = &bm->calib.adc[i];
632 + if (adc_calib->flags == 0xFF)
634 + switch (adc_calib->id) {
635 + case N810BM_PMM_ADC_0x01:
636 + if (adc_calib->field1 < 2400 ||
637 + adc_calib->field1 > 2700)
638 + goto value_check_fail;
639 + if (adc_calib->field2 < 20000 ||
640 + adc_calib->field2 > 23000)
641 + goto value_check_fail;
644 + case N810BM_PMM_ADC_0x04:
645 + if (adc_calib->field1 < 1100 ||
646 + adc_calib->field1 > 1300)
647 + goto value_check_fail;
650 + case N810BM_PMM_ADC_0x0E:
651 + if (adc_calib->field2 < 7000 ||
652 + adc_calib->field2 > 12000)
653 + goto value_check_fail;
656 + case N810BM_PMM_ADC_0xFE:
657 + if ((s32)adc_calib->field1 > 14 ||
658 + (s32)adc_calib->field1 < -14)
659 + goto value_check_fail;
660 + if (adc_calib->field2 < 13000 ||
661 + adc_calib->field2 > 13350)
662 + goto value_check_fail;
665 + case N810BM_PMM_ADC_0x02:
666 + case N810BM_PMM_ADC_BATTEMP:
667 + case N810BM_PMM_ADC_0x13:
670 + case N810BM_PMM_ADC_0x03:
671 + case N810BM_PMM_ADC_0x07:
672 + case N810BM_PMM_ADC_0x08:
673 + case N810BM_PMM_ADC_0x06:
674 + case N810BM_PMM_ADC_0x14:
675 + case N810BM_PMM_ADC_0x15:
676 + case N810BM_PMM_ADC_0x16:
677 + case N810BM_PMM_ADC_0x17:
680 + dev_dbg(&bm->pdev->dev,
681 + "ADC 0x%02X calib: 0x%02X 0x%02X 0x%08X 0x%08X 0x%04X 0x%04X",
682 + adc_calib->id, adc_calib->flags, adc_calib->adc_groupnr,
683 + adc_calib->field1, adc_calib->field2,
684 + adc_calib->field3, adc_calib->field4);
687 + dev_err(&bm->pdev->dev, "PMM sanity check: Did not find "
688 + "all required values (count=%u)", count);
695 + dev_err(&bm->pdev->dev, "PMM image sanity check failed "
696 + "(id=%02X, field1=%08X, field2=%08X)",
697 + adc_calib->id, adc_calib->field1, adc_calib->field2);
702 +/* Set the current measure timer that triggers on Tahvo IRQ 7
703 + * An interval of zero disables the timer. */
704 +static void n810bm_set_current_measure_timer(struct n810bm *bm,
705 + u16 millisec_interval)
707 + u16 value = millisec_interval;
709 + if (value <= 0xF905) {
710 + value = ((u64)0x10624DD3 * (u64)(value + 0xF9)) >> 32;
715 + tahvo_write(bm, TAHVO_REG_BATCURRTIMER, value & 0xFF);
717 + tahvo_set(bm, TAHVO_REG_CHGCTL,
718 + TAHVO_REG_CHGCTL_CURTIMRST);
719 + tahvo_clear(bm, TAHVO_REG_CHGCTL,
720 + TAHVO_REG_CHGCTL_CURTIMRST);
722 + if (millisec_interval)
723 + tahvo_enable_irq(TAHVO_INT_BATCURR);
725 + tahvo_disable_irq(TAHVO_INT_BATCURR);
727 + //TODO also do a software timer for safety.
730 +static void n810bm_enable_current_measure(struct n810bm *bm)
732 + WARN_ON(bm->current_measure_enabled < 0);
733 + if (!bm->current_measure_enabled) {
734 + /* Enable the current measurement circuitry */
735 + tahvo_set(bm, TAHVO_REG_CHGCTL,
736 + TAHVO_REG_CHGCTL_CURMEAS);
737 + dev_dbg(&bm->pdev->dev,
738 + "Current measurement circuitry enabled");
740 + bm->current_measure_enabled++;
743 +static void n810bm_disable_current_measure(struct n810bm *bm)
745 + bm->current_measure_enabled--;
746 + WARN_ON(bm->current_measure_enabled < 0);
747 + if (!bm->current_measure_enabled) {
748 + /* Disable the current measurement circuitry */
749 + tahvo_clear(bm, TAHVO_REG_CHGCTL,
750 + TAHVO_REG_CHGCTL_CURMEAS);
751 + dev_dbg(&bm->pdev->dev,
752 + "Current measurement circuitry disabled");
756 +/* Measure the actual battery current. Returns a signed value in mA.
757 + * Does only work, if current measurement was enabled. */
758 +static int n810bm_measure_batt_current(struct n810bm *bm)
761 + int adc = 0, ma, i;
763 + if (WARN_ON(bm->current_measure_enabled <= 0))
765 + for (i = 0; i < 3; i++) {
766 + retval = tahvo_read(bm, TAHVO_REG_BATCURR);
767 + adc += (s16)retval; /* Value is signed */
771 + //TODO convert to mA
777 +/* Requires bm->mutex locked */
778 +static int n810bm_measure_batt_current_async(struct n810bm *bm)
781 + bool charging = lipocharge_is_charging(&bm->charger);
783 + n810bm_enable_current_measure(bm);
785 + WARN_ON(bm->active_current_pwm != 0);
786 + tahvo_maskset(bm, TAHVO_REG_CHGCTL,
787 + TAHVO_REG_CHGCTL_EN |
788 + TAHVO_REG_CHGCTL_PWMOVR |
789 + TAHVO_REG_CHGCTL_PWMOVRZERO,
790 + TAHVO_REG_CHGCTL_EN |
791 + TAHVO_REG_CHGCTL_PWMOVR |
792 + (charging ? 0 : TAHVO_REG_CHGCTL_PWMOVRZERO));
793 + ma = n810bm_measure_batt_current(bm);
794 + tahvo_maskset(bm, TAHVO_REG_CHGCTL,
795 + TAHVO_REG_CHGCTL_EN |
796 + TAHVO_REG_CHGCTL_PWMOVR |
797 + TAHVO_REG_CHGCTL_PWMOVRZERO,
798 + (charging ? TAHVO_REG_CHGCTL_EN : 0));
799 + n810bm_disable_current_measure(bm);
804 +static int adc_sanity_check(struct n810bm *bm, unsigned int channel)
808 + value = retu_read_adc(channel);
810 + dev_err(&bm->pdev->dev, "Failed to read GND ADC channel %u",
814 + dev_dbg(&bm->pdev->dev,
815 + "GND ADC channel %u sanity check got value: %d",
818 + n810bm_emergency(bm, "GND ADC sanity check failed");
825 +static int n810bm_check_adc_sanity(struct n810bm *bm)
829 + /* Discard one conversion */
830 + retu_write(bm, RETU_REG_ADCSCR, 0);
831 + retu_read_adc(RETU_ADC_GND2);
833 + err = adc_sanity_check(bm, RETU_ADC_GND2);
840 +/* Measure the battery voltage. Returns the value in mV (or negative value on error). */
841 +static int n810bm_measure_batt_voltage(struct n810bm *bm)
845 + const unsigned int scale = 1000;
847 + adc = retu_adc_average(bm, RETU_ADC_BATTVOLT, 5);
852 + mv = 2800 + ((adc - 0x37) * (((4200 - 2800) * scale) / (0x236 - 0x37))) / scale;
854 + //TODO compensate for power consumption
855 + //TODO honor calibration values
860 +/* Measure the charger voltage. Returns the value in mV (or negative value on error). */
861 +static int n810bm_measure_charger_voltage(struct n810bm *bm)
866 + adc = retu_adc_average(bm, RETU_ADC_CHGVOLT, 5);
869 + //TODO convert to mV
875 +/* Measure backup battery voltage. Returns the value in mV (or negative value on error). */
876 +static int n810bm_measure_backup_batt_voltage(struct n810bm *bm)
881 + adc = retu_adc_average(bm, RETU_ADC_BKUPVOLT, 3);
884 + //TODO convert to mV
890 +/* Measure the battery temperature. Returns the value in K (or negative value on error). */
891 +static int n810bm_measure_batt_temp(struct n810bm *bm)
896 + adc = retu_adc_average(bm, RETU_ADC_BATTEMP, 3);
899 + //TODO convert to K
905 +/* Read the battery capacity via BSI pin. */
906 +static enum n810bm_capacity n810bm_read_batt_capacity(struct n810bm *bm)
909 + const unsigned int hyst = 20;
911 + adc = retu_adc_average(bm, RETU_ADC_BSI, 5);
913 + dev_err(&bm->pdev->dev, "Failed to read BSI ADC");
914 + return N810BM_CAP_UNKNOWN;
917 + if (adc >= 0x3B5 - hyst && adc <= 0x3B5 + hyst)
918 + return N810BM_CAP_1500MAH;
920 + dev_err(&bm->pdev->dev, "Capacity indicator 0x%X unknown", adc);
922 + return N810BM_CAP_UNKNOWN;
925 +/* Convert a battery voltage (in mV) to percentage. */
926 +static unsigned int n810bm_mvolt2percent(unsigned int mv)
928 + const unsigned int minv = 3700;
929 + const unsigned int maxv = 4150;
930 + unsigned int percent;
932 + mv = clamp(mv, minv, maxv);
933 + percent = (mv - minv) * 100 / (maxv - minv);
938 +static void n810bm_start_charge(struct n810bm *bm)
942 + WARN_ON(!bm->battery_present);
943 + WARN_ON(!bm->charger_present);
945 + /* Set PWM to zero */
946 + bm->active_current_pwm = 0;
947 + tahvo_write(bm, TAHVO_REG_CHGCURR, bm->active_current_pwm);
949 + /* Charge global enable */
950 + tahvo_maskset(bm, TAHVO_REG_CHGCTL,
951 + TAHVO_REG_CHGCTL_EN |
952 + TAHVO_REG_CHGCTL_PWMOVR |
953 + TAHVO_REG_CHGCTL_PWMOVRZERO,
954 + TAHVO_REG_CHGCTL_EN);
956 + WARN_ON((int)bm->capacity <= 0);
957 + bm->charger.capacity = bm->capacity;
958 + err = lipocharge_start(&bm->charger);
961 + /* Initialize current measurement circuitry */
962 + n810bm_enable_current_measure(bm);
963 + n810bm_set_current_measure_timer(bm, 250);
965 + dev_info(&bm->pdev->dev, "Charging battery");
966 + n810bm_notify_charger_pwm(bm);
967 + n810bm_notify_battery_charging(bm);
970 +static void n810bm_stop_charge(struct n810bm *bm)
972 + if (lipocharge_is_charging(&bm->charger)) {
973 + n810bm_set_current_measure_timer(bm, 0);
974 + n810bm_disable_current_measure(bm);
976 + lipocharge_stop(&bm->charger);
978 + /* Set PWM to zero */
979 + bm->active_current_pwm = 0;
980 + tahvo_write(bm, TAHVO_REG_CHGCURR, bm->active_current_pwm);
982 + /* Charge global disable */
983 + tahvo_maskset(bm, TAHVO_REG_CHGCTL,
984 + TAHVO_REG_CHGCTL_EN |
985 + TAHVO_REG_CHGCTL_PWMOVR |
986 + TAHVO_REG_CHGCTL_PWMOVRZERO,
989 + dev_info(&bm->pdev->dev, "Not charging battery");
990 + n810bm_notify_charger_pwm(bm);
991 + n810bm_notify_battery_charging(bm);
994 +/* Periodic check */
995 +static void n810bm_periodic_check_work(struct work_struct *work)
997 + struct n810bm *bm = container_of(to_delayed_work(work),
998 + struct n810bm, periodic_check_work);
1000 + bool battery_was_present, charger_was_present;
1003 + mutex_lock(&bm->mutex);
1005 + status = retu_read(bm, RETU_REG_STATUS);
1006 + battery_was_present = bm->battery_present;
1007 + charger_was_present = bm->charger_present;
1008 + bm->battery_present = !!(status & RETU_REG_STATUS_BATAVAIL);
1009 + bm->charger_present = !!(status & RETU_REG_STATUS_CHGPLUG);
1011 + if (bm->battery_present != battery_was_present) {
1012 + /* Battery state changed */
1013 + if (bm->battery_present) {
1014 + bm->capacity = n810bm_read_batt_capacity(bm);
1015 + if (bm->capacity == N810BM_CAP_UNKNOWN) {
1016 + dev_err(&bm->pdev->dev, "Unknown battery detected");
1018 + dev_info(&bm->pdev->dev, "Detected %u mAh battery",
1019 + (unsigned int)bm->capacity);
1022 + bm->capacity = N810BM_CAP_NONE;
1023 + dev_info(&bm->pdev->dev, "The main battery was removed");
1024 + //TODO disable charging
1028 + if (bm->charger_present != charger_was_present) {
1029 + /* Charger state changed */
1030 + dev_info(&bm->pdev->dev, "The charger was %s",
1031 + bm->charger_present ? "plugged in" : "removed");
1034 + if ((bm->battery_present && !bm->charger_present) ||
1035 + !n810bm_known_battery_present(bm)){
1036 + /* We're draining the battery */
1037 + mv = n810bm_measure_batt_voltage(bm);
1039 + n810bm_emergency(bm,
1040 + "check: Failed to measure voltage");
1042 + if (mv < N810BM_MIN_VOLTAGE_THRES) {
1043 + n810bm_emergency(bm,
1044 + "check: Minimum voltage threshold reached");
1048 + if (bm->charger_present && n810bm_known_battery_present(bm)) {
1049 + /* Known battery and charger are connected */
1050 + if (bm->charger_enabled) {
1051 + /* Charger is enabled */
1052 + if (!lipocharge_is_charging(&bm->charger)) {
1053 + //TODO start charging, if battery is below some threshold
1054 + n810bm_start_charge(bm);
1059 + if (lipocharge_is_charging(&bm->charger) && !bm->charger_present) {
1060 + /* Charger was unplugged. */
1061 + n810bm_stop_charge(bm);
1064 + mutex_unlock(&bm->mutex);
1065 + schedule_delayed_work(&bm->periodic_check_work,
1066 + round_jiffies_relative(N810BM_CHECK_INTERVAL));
1069 +static void n810bm_adc_irq_handler(unsigned long data)
1071 + struct n810bm *bm = (struct n810bm *)data;
1073 + retu_ack_irq(RETU_INT_ADCS);
1075 +dev_info(&bm->pdev->dev, "ADC interrupt triggered\n");
1078 +static void n810bm_tahvo_current_measure_work(struct work_struct *work)
1080 + struct n810bm *bm = container_of(work, struct n810bm, currmeas_irq_work);
1081 + int res, ma, mv, temp;
1083 + mutex_lock(&bm->mutex);
1084 + if (!lipocharge_is_charging(&bm->charger))
1087 + tahvo_maskset(bm, TAHVO_REG_CHGCTL,
1088 + TAHVO_REG_CHGCTL_PWMOVR |
1089 + TAHVO_REG_CHGCTL_PWMOVRZERO,
1090 + TAHVO_REG_CHGCTL_PWMOVR);
1091 + ma = n810bm_measure_batt_current(bm);
1092 + tahvo_maskset(bm, TAHVO_REG_CHGCTL,
1093 + TAHVO_REG_CHGCTL_PWMOVR |
1094 + TAHVO_REG_CHGCTL_PWMOVRZERO,
1095 + TAHVO_REG_CHGCTL_PWMOVR |
1096 + TAHVO_REG_CHGCTL_PWMOVRZERO);
1098 + mv = n810bm_measure_batt_voltage(bm);
1099 + tahvo_maskset(bm, TAHVO_REG_CHGCTL,
1100 + TAHVO_REG_CHGCTL_PWMOVR |
1101 + TAHVO_REG_CHGCTL_PWMOVRZERO,
1103 + temp = n810bm_measure_batt_temp(bm);
1104 + if (WARN_ON(mv < 0))
1106 + if (WARN_ON(temp < 0))
1109 + if (bm->verbose_charge_log) {
1110 + dev_info(&bm->pdev->dev,
1111 + "Battery charge state: %d mV, %d mA (%s)",
1113 + (ma <= 0) ? "discharging" : "charging");
1115 + res = lipocharge_update_state(&bm->charger, mv, ma, temp);
1118 + dev_info(&bm->pdev->dev, "Battery fully charged");
1119 + n810bm_stop_charge(bm);
1122 + mutex_unlock(&bm->mutex);
1125 +static void n810bm_tahvo_current_measure_irq_handler(unsigned long data)
1127 + struct n810bm *bm = (struct n810bm *)data;
1129 + tahvo_ack_irq(TAHVO_INT_BATCURR);
1130 + schedule_work(&bm->currmeas_irq_work);
1133 +#define DEFINE_ATTR_NOTIFY(attr_name) \
1134 + void n810bm_notify_##attr_name(struct n810bm *bm) \
1136 + set_bit(N810BM_NOTIFY_##attr_name, &bm->notify_flags); \
1138 + schedule_work(&bm->notify_work); \
1141 +#define DEFINE_SHOW_INT_FUNC(name, member) \
1142 + static ssize_t n810bm_attr_##name##_show(struct device *dev, \
1143 + struct device_attribute *attr, \
1146 + struct n810bm *bm = device_to_n810bm(dev); \
1149 + mutex_lock(&bm->mutex); \
1150 + count = snprintf(buf, PAGE_SIZE, "%d\n", (int)(bm->member)); \
1151 + mutex_unlock(&bm->mutex); \
1156 +#define DEFINE_STORE_INT_FUNC(name, member) \
1157 + static ssize_t n810bm_attr_##name##_store(struct device *dev, \
1158 + struct device_attribute *attr,\
1159 + const char *buf, size_t count)\
1161 + struct n810bm *bm = device_to_n810bm(dev); \
1165 + mutex_lock(&bm->mutex); \
1166 + err = strict_strtol(buf, 0, &val); \
1168 + bm->member = (typeof(bm->member))val; \
1169 + mutex_unlock(&bm->mutex); \
1171 + return err ? err : count; \
1174 +#define DEFINE_ATTR_SHOW_INT(name, member) \
1175 + DEFINE_SHOW_INT_FUNC(name, member) \
1176 + static DEVICE_ATTR(name, S_IRUGO, \
1177 + n810bm_attr_##name##_show, NULL);
1179 +#define DEFINE_ATTR_SHOW_STORE_INT(name, member) \
1180 + DEFINE_SHOW_INT_FUNC(name, member) \
1181 + DEFINE_STORE_INT_FUNC(name, member) \
1182 + static DEVICE_ATTR(name, S_IRUGO | S_IWUSR, \
1183 + n810bm_attr_##name##_show, \
1184 + n810bm_attr_##name##_store);
1186 +DEFINE_ATTR_SHOW_INT(battery_present, battery_present);
1187 +DEFINE_ATTR_SHOW_INT(charger_present, charger_present);
1188 +DEFINE_ATTR_SHOW_INT(charger_pwm, active_current_pwm);
1189 +static DEFINE_ATTR_NOTIFY(charger_pwm);
1190 +DEFINE_ATTR_SHOW_STORE_INT(charger_enable, charger_enabled);
1191 +DEFINE_ATTR_SHOW_STORE_INT(charger_verbose, verbose_charge_log);
1193 +static ssize_t n810bm_attr_battery_charging(struct device *dev,
1194 + struct device_attribute *attr,
1197 + struct n810bm *bm = device_to_n810bm(dev);
1200 + mutex_lock(&bm->mutex);
1201 + count = snprintf(buf, PAGE_SIZE, "%d\n",
1202 + (int)lipocharge_is_charging(&bm->charger));
1203 + mutex_unlock(&bm->mutex);
1207 +static DEVICE_ATTR(battery_charging, S_IRUGO,
1208 + n810bm_attr_battery_charging, NULL);
1209 +static DEFINE_ATTR_NOTIFY(battery_charging);
1211 +static ssize_t n810bm_attr_battery_level_show(struct device *dev,
1212 + struct device_attribute *attr,
1215 + struct n810bm *bm = device_to_n810bm(dev);
1216 + ssize_t count = -ENODEV;
1219 + mutex_lock(&bm->mutex);
1220 + if (!bm->battery_present || lipocharge_is_charging(&bm->charger))
1223 + millivolt = n810bm_measure_batt_voltage(bm);
1224 + if (millivolt >= 0) {
1225 + count = snprintf(buf, PAGE_SIZE, "%u\n",
1226 + n810bm_mvolt2percent(millivolt));
1228 + mutex_unlock(&bm->mutex);
1232 +static DEVICE_ATTR(battery_level, S_IRUGO,
1233 + n810bm_attr_battery_level_show, NULL);
1235 +static ssize_t n810bm_attr_battery_capacity_show(struct device *dev,
1236 + struct device_attribute *attr,
1239 + struct n810bm *bm = device_to_n810bm(dev);
1243 + mutex_lock(&bm->mutex);
1244 + if (n810bm_known_battery_present(bm))
1245 + capacity = (int)bm->capacity;
1246 + count = snprintf(buf, PAGE_SIZE, "%d\n", capacity);
1247 + mutex_unlock(&bm->mutex);
1251 +static DEVICE_ATTR(battery_capacity, S_IRUGO,
1252 + n810bm_attr_battery_capacity_show, NULL);
1254 +static ssize_t n810bm_attr_battery_temp_show(struct device *dev,
1255 + struct device_attribute *attr,
1258 + struct n810bm *bm = device_to_n810bm(dev);
1259 + ssize_t count = -ENODEV;
1262 + mutex_lock(&bm->mutex);
1263 + k = n810bm_measure_batt_temp(bm);
1265 + count = snprintf(buf, PAGE_SIZE, "%d\n", k);
1266 + mutex_unlock(&bm->mutex);
1270 +static DEVICE_ATTR(battery_temp, S_IRUGO,
1271 + n810bm_attr_battery_temp_show, NULL);
1273 +static ssize_t n810bm_attr_charger_voltage_show(struct device *dev,
1274 + struct device_attribute *attr,
1277 + struct n810bm *bm = device_to_n810bm(dev);
1278 + ssize_t count = -ENODEV;
1281 + mutex_lock(&bm->mutex);
1282 + if (bm->charger_present)
1283 + mv = n810bm_measure_charger_voltage(bm);
1285 + count = snprintf(buf, PAGE_SIZE, "%d\n", mv);
1286 + mutex_unlock(&bm->mutex);
1290 +static DEVICE_ATTR(charger_voltage, S_IRUGO,
1291 + n810bm_attr_charger_voltage_show, NULL);
1293 +static ssize_t n810bm_attr_backup_battery_voltage_show(struct device *dev,
1294 + struct device_attribute *attr,
1297 + struct n810bm *bm = device_to_n810bm(dev);
1298 + ssize_t count = -ENODEV;
1301 + mutex_lock(&bm->mutex);
1302 + mv = n810bm_measure_backup_batt_voltage(bm);
1304 + count = snprintf(buf, PAGE_SIZE, "%d\n", mv);
1305 + mutex_unlock(&bm->mutex);
1309 +static DEVICE_ATTR(backup_battery_voltage, S_IRUGO,
1310 + n810bm_attr_backup_battery_voltage_show, NULL);
1312 +static ssize_t n810bm_attr_battery_current_show(struct device *dev,
1313 + struct device_attribute *attr,
1316 + struct n810bm *bm = device_to_n810bm(dev);
1317 + ssize_t count = -ENODEV;
1320 + mutex_lock(&bm->mutex);
1321 + if (bm->battery_present)
1322 + ma = n810bm_measure_batt_current_async(bm);
1323 + count = snprintf(buf, PAGE_SIZE, "%d\n", ma);
1324 + mutex_unlock(&bm->mutex);
1328 +static DEVICE_ATTR(battery_current, S_IRUGO,
1329 + n810bm_attr_battery_current_show, NULL);
1331 +static const struct device_attribute *n810bm_attrs[] = {
1332 + &dev_attr_battery_present,
1333 + &dev_attr_battery_level,
1334 + &dev_attr_battery_charging,
1335 + &dev_attr_battery_current,
1336 + &dev_attr_battery_capacity,
1337 + &dev_attr_battery_temp,
1338 + &dev_attr_backup_battery_voltage,
1339 + &dev_attr_charger_present,
1340 + &dev_attr_charger_verbose,
1341 + &dev_attr_charger_voltage,
1342 + &dev_attr_charger_enable,
1343 + &dev_attr_charger_pwm,
1346 +static void n810bm_notify_work(struct work_struct *work)
1348 + struct n810bm *bm = container_of(work, struct n810bm, notify_work);
1349 + unsigned long notify_flags;
1351 + notify_flags = xchg(&bm->notify_flags, 0);
1354 +#define do_notify(attr_name) \
1356 + if (notify_flags & (1 << N810BM_NOTIFY_##attr_name)) { \
1357 + sysfs_notify(&bm->pdev->dev.kobj, NULL, \
1358 + dev_attr_##attr_name.attr.name); \
1362 + do_notify(battery_charging);
1363 + do_notify(charger_pwm);
1366 +static int n810bm_charger_set_current_pwm(struct lipocharge *c,
1367 + unsigned int duty_cycle)
1369 + struct n810bm *bm = container_of(c, struct n810bm, charger);
1370 + int err = -EINVAL;
1372 + WARN_ON(!mutex_is_locked(&bm->mutex));
1373 + if (WARN_ON(duty_cycle > 0xFF))
1375 + if (WARN_ON(!bm->charger_enabled))
1377 + if (WARN_ON(!bm->battery_present || !bm->charger_present))
1380 + if (duty_cycle != bm->active_current_pwm) {
1381 + bm->active_current_pwm = duty_cycle;
1382 + tahvo_write(bm, TAHVO_REG_CHGCURR, duty_cycle);
1383 + n810bm_notify_charger_pwm(bm);
1392 +static void n810bm_charger_emergency(struct lipocharge *c)
1394 + struct n810bm *bm = container_of(c, struct n810bm, charger);
1396 + n810bm_emergency(bm, "Battery charger fault");
1399 +static void n810bm_hw_exit(struct n810bm *bm)
1401 + n810bm_stop_charge(bm);
1402 + retu_write(bm, RETU_REG_ADCSCR, 0);
1405 +static int n810bm_hw_init(struct n810bm *bm)
1409 + err = n810bm_check_adc_sanity(bm);
1413 + n810bm_stop_charge(bm);
1418 +static void n810bm_cancel_and_flush_work(struct n810bm *bm)
1420 + cancel_delayed_work_sync(&bm->periodic_check_work);
1421 + cancel_work_sync(&bm->notify_work);
1422 + cancel_work_sync(&bm->currmeas_irq_work);
1423 + flush_scheduled_work();
1426 +static int n810bm_device_init(struct n810bm *bm)
1431 + bm->charger.rate = LIPORATE_p6C;
1432 + bm->charger.top_voltage = 4100;
1433 + bm->charger.duty_cycle_max = 0xFF;
1434 + bm->charger.set_current_pwm = n810bm_charger_set_current_pwm;
1435 + bm->charger.emergency = n810bm_charger_emergency;
1436 + lipocharge_init(&bm->charger, &bm->pdev->dev);
1438 + err = n810bm_hw_init(bm);
1441 + for (attr_index = 0; attr_index < ARRAY_SIZE(n810bm_attrs); attr_index++) {
1442 + err = device_create_file(&bm->pdev->dev, n810bm_attrs[attr_index]);
1444 + goto err_unwind_attrs;
1446 + err = retu_request_irq(RETU_INT_ADCS,
1447 + n810bm_adc_irq_handler,
1448 + (unsigned long)bm, "n810bm");
1450 + goto err_unwind_attrs;
1451 + err = tahvo_request_irq(TAHVO_INT_BATCURR,
1452 + n810bm_tahvo_current_measure_irq_handler,
1453 + (unsigned long)bm, "n810bm");
1455 + goto err_free_retu_irq;
1456 + tahvo_disable_irq(TAHVO_INT_BATCURR);
1458 + schedule_delayed_work(&bm->periodic_check_work,
1459 + round_jiffies_relative(N810BM_CHECK_INTERVAL));
1461 + bm->initialized = 1;
1462 + dev_info(&bm->pdev->dev, "Battery management initialized");
1467 + retu_free_irq(RETU_INT_ADCS);
1469 + for (attr_index--; attr_index >= 0; attr_index--)
1470 + device_remove_file(&bm->pdev->dev, n810bm_attrs[attr_index]);
1472 + n810bm_hw_exit(bm);
1474 + n810bm_cancel_and_flush_work(bm);
1479 +static void n810bm_device_exit(struct n810bm *bm)
1483 + if (!bm->initialized)
1486 + lipocharge_exit(&bm->charger);
1487 + tahvo_free_irq(TAHVO_INT_BATCURR);
1488 + retu_free_irq(RETU_INT_ADCS);
1489 + for (i = 0; i < ARRAY_SIZE(n810bm_attrs); i++)
1490 + device_remove_file(&bm->pdev->dev, n810bm_attrs[i]);
1492 + n810bm_cancel_and_flush_work(bm);
1494 + n810bm_hw_exit(bm);
1496 + bm->initialized = 0;
1499 +static void n810bm_pmm_block_found(const struct firmware *fw, void *context)
1501 + struct n810bm *bm = context;
1505 + dev_err(&bm->pdev->dev,
1506 + "CAL PMM block image file not found");
1509 + if (fw->size != N810BM_PMM_BLOCK_SIZE ||
1510 + memcmp(fw->data, "BME-PMM-BLOCK01", 15) != 0) {
1511 + dev_err(&bm->pdev->dev,
1512 + "CAL PMM block image file has an invalid format");
1516 + err = n810bm_parse_pmm_block(bm, fw);
1519 + release_firmware(fw);
1521 + err = n810bm_device_init(bm);
1523 + dev_err(&bm->pdev->dev,
1524 + "Failed to initialized battery management (%d)", err);
1530 + release_firmware(fw);
1535 +static int __devinit n810bm_probe(struct platform_device *pdev)
1537 + struct n810bm *bm;
1540 + bm = kzalloc(sizeof(*bm), GFP_KERNEL);
1544 + platform_set_drvdata(pdev, bm);
1545 + mutex_init(&bm->mutex);
1546 + INIT_DELAYED_WORK(&bm->periodic_check_work, n810bm_periodic_check_work);
1547 + INIT_WORK(&bm->notify_work, n810bm_notify_work);
1548 + INIT_WORK(&bm->currmeas_irq_work, n810bm_tahvo_current_measure_work);
1550 + dev_info(&bm->pdev->dev, "Requesting CAL BME PMM block firmware file "
1551 + N810BM_PMM_BLOCK_FILENAME);
1552 + err = request_firmware_nowait(THIS_MODULE, 1,
1553 + N810BM_PMM_BLOCK_FILENAME,
1554 + &bm->pdev->dev, GFP_KERNEL,
1555 + bm, n810bm_pmm_block_found);
1557 + dev_err(&bm->pdev->dev,
1558 + "Failed to request CAL PMM block image file (%d)", err);
1570 +static int __devexit n810bm_remove(struct platform_device *pdev)
1572 + struct n810bm *bm = platform_get_drvdata(pdev);
1574 + n810bm_device_exit(bm);
1577 + platform_set_drvdata(pdev, NULL);
1582 +static struct platform_driver n810bm_driver = {
1583 + .remove = __devexit_p(n810bm_remove),
1589 +static int __init n810bm_modinit(void)
1591 + return platform_driver_probe(&n810bm_driver, n810bm_probe);
1593 +module_init(n810bm_modinit);
1595 +static void __exit n810bm_modexit(void)
1597 + platform_driver_unregister(&n810bm_driver);
1599 +module_exit(n810bm_modexit);
1601 +MODULE_DESCRIPTION("Nokia n810 battery management");
1602 +MODULE_FIRMWARE(N810BM_PMM_BLOCK_FILENAME);
1603 +MODULE_LICENSE("GPL");
1604 +MODULE_AUTHOR("Michael Buesch");
1605 Index: linux-2.6.37.1/drivers/cbus/retu.c
1606 ===================================================================
1607 --- linux-2.6.37.1.orig/drivers/cbus/retu.c 2011-02-19 20:26:01.251340770 +0100
1608 +++ linux-2.6.37.1/drivers/cbus/retu.c 2011-02-19 20:26:01.529318528 +0100
1611 * This function writes a value to the specified register
1613 -void retu_write_reg(int reg, u16 val)
1614 +int retu_write_reg(int reg, u16 val)
1616 BUG_ON(!retu_initialized);
1617 - cbus_write_reg(cbus_host, RETU_ID, reg, val);
1618 + return cbus_write_reg(cbus_host, RETU_ID, reg, val);
1621 void retu_set_clear_reg_bits(int reg, u16 set, u16 clear)
1623 EXPORT_SYMBOL(retu_ack_irq);
1624 EXPORT_SYMBOL(retu_read_reg);
1625 EXPORT_SYMBOL(retu_write_reg);
1626 +EXPORT_SYMBOL(retu_read_adc);
1628 subsys_initcall(retu_init);
1629 module_exit(retu_exit);
1630 Index: linux-2.6.37.1/drivers/cbus/retu.h
1631 ===================================================================
1632 --- linux-2.6.37.1.orig/drivers/cbus/retu.h 2011-02-19 20:26:01.252340690 +0100
1633 +++ linux-2.6.37.1/drivers/cbus/retu.h 2011-02-19 20:26:01.530318448 +0100
1635 #define RETU_REG_CTRL_CLR 0x0f /* Regulator clear register */
1636 #define RETU_REG_CTRL_SET 0x10 /* Regulator set register */
1637 #define RETU_REG_STATUS 0x16 /* Status register */
1638 +#define RETU_REG_STATUS_BATAVAIL 0x0100 /* Battery available */
1639 +#define RETU_REG_STATUS_CHGPLUG 0x1000 /* Charger is plugged in */
1640 #define RETU_REG_WATCHDOG 0x17 /* Watchdog register */
1641 #define RETU_REG_AUDTXR 0x18 /* Audio Codec Tx register */
1642 #define RETU_REG_MAX 0x1f
1645 #define MAX_RETU_IRQ_HANDLERS 16
1648 +#define RETU_ADC_GND 0x00 /* Ground */
1649 +#define RETU_ADC_BSI 0x01 /* Battery Size Indicator */
1650 +#define RETU_ADC_BATTEMP 0x02 /* Battery temperature */
1651 +#define RETU_ADC_CHGVOLT 0x03 /* Charger voltage */
1652 +#define RETU_ADC_HEADSET 0x04 /* Headset detection */
1653 +#define RETU_ADC_HOOKDET 0x05 /* Hook detection */
1654 +#define RETU_ADC_RFGP 0x06 /* RF GP */
1655 +#define RETU_ADC_WBTX 0x07 /* Wideband Tx detection */
1656 +#define RETU_ADC_BATTVOLT 0x08 /* Battery voltage measurement */
1657 +#define RETU_ADC_GND2 0x09 /* Ground */
1658 +#define RETU_ADC_LIGHTSENS 0x0A /* Light sensor */
1659 +#define RETU_ADC_LIGHTTEMP 0x0B /* Light sensor temperature */
1660 +#define RETU_ADC_BKUPVOLT 0x0C /* Backup battery voltage */
1661 +#define RETU_ADC_TEMP 0x0D /* RETU temperature */
1664 int retu_read_reg(int reg);
1665 -void retu_write_reg(int reg, u16 val);
1666 +int retu_write_reg(int reg, u16 val);
1667 void retu_set_clear_reg_bits(int reg, u16 set, u16 clear);
1668 int retu_read_adc(int channel);
1669 int retu_request_irq(int id, void *irq_handler, unsigned long arg, char *name);
1670 Index: linux-2.6.37.1/arch/arm/mach-omap2/board-n8x0.c
1671 ===================================================================
1672 --- linux-2.6.37.1.orig/arch/arm/mach-omap2/board-n8x0.c 2011-02-19 20:26:01.201344770 +0100
1673 +++ linux-2.6.37.1/arch/arm/mach-omap2/board-n8x0.c 2011-02-19 20:26:01.531318368 +0100
1674 @@ -907,6 +907,17 @@
1675 ARRAY_SIZE(n8x0_gpio_switches));
1678 +static struct platform_device n810_bm_device = {
1683 +static void __init n810_bm_init(void)
1685 + if (platform_device_register(&n810_bm_device))
1689 static void __init n8x0_init_machine(void)
1691 omap2420_mux_init(board_mux, OMAP_PACKAGE_ZAC);
1693 n8x0_onenand_init();
1700 MACHINE_START(NOKIA_N800, "Nokia N800")
1701 Index: linux-2.6.37.1/drivers/cbus/lipocharge.c
1702 ===================================================================
1703 --- /dev/null 1970-01-01 00:00:00.000000000 +0000
1704 +++ linux-2.6.37.1/drivers/cbus/lipocharge.c 2011-02-19 20:26:01.531318368 +0100
1707 + * Generic LIPO battery charger
1709 + * Copyright (c) 2010-2011 Michael Buesch <mb@bu3sch.de>
1711 + * This program is free software; you can redistribute it and/or
1712 + * modify it under the terms of the GNU General Public License
1713 + * as published by the Free Software Foundation; either version 2
1714 + * of the License, or (at your option) any later version.
1716 + * This program is distributed in the hope that it will be useful,
1717 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
1718 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
1719 + * GNU General Public License for more details.
1724 +#include "lipocharge.h"
1726 +#include <linux/slab.h>
1729 +/* Hysteresis constants */
1730 +#define CURRENT_HYST 30 /* mA */
1731 +#define VOLTAGE_HYST 10 /* mV */
1733 +/* Threshold constants */
1734 +#define FINISH_CURRENT_PERCENT 3
1737 +/* Returns the requested first-stage charge current in mA */
1738 +static inline unsigned int get_stage1_charge_current(struct lipocharge *c)
1740 + /* current = (capacity * C) */
1741 + return c->capacity * c->rate / 1000;
1744 +void lipocharge_init(struct lipocharge *c, struct device *dev)
1747 + c->state = LIPO_IDLE;
1750 +void lipocharge_exit(struct lipocharge *c)
1752 + c->state = LIPO_IDLE;
1755 +int lipocharge_start(struct lipocharge *c)
1759 + if (c->state != LIPO_IDLE)
1761 + if (!c->set_current_pwm || !c->emergency)
1763 + if (!c->top_voltage || c->top_voltage > 4200)
1766 + c->active_duty_cycle = 0;
1767 + err = c->set_current_pwm(c, c->active_duty_cycle);
1770 + c->state = LIPO_FIRST_STAGE;
1775 +void lipocharge_stop(struct lipocharge *c)
1777 + if (c->state == LIPO_IDLE)
1779 + c->state = LIPO_IDLE;
1782 +static int lipocharge_increase_current(struct lipocharge *c,
1783 + unsigned int inc_permille)
1785 + int old_pwm, new_pwm;
1787 + if (c->active_duty_cycle >= c->duty_cycle_max)
1790 + old_pwm = c->active_duty_cycle;
1791 + new_pwm = old_pwm + (c->duty_cycle_max * inc_permille / 1000);
1792 + new_pwm = min(new_pwm, (int)c->duty_cycle_max);
1793 + c->active_duty_cycle = new_pwm;
1795 + dev_dbg(c->dev, "lipo: Increasing duty_cycle by "
1796 + "%u permille (0x%02X -> 0x%02X)",
1797 + inc_permille, old_pwm, new_pwm);
1799 + return c->set_current_pwm(c, c->active_duty_cycle);
1802 +static int lipocharge_decrease_current(struct lipocharge *c,
1803 + unsigned int dec_permille)
1805 + int old_pwm, new_pwm;
1807 + if (c->active_duty_cycle <= 0)
1810 + old_pwm = c->active_duty_cycle;
1811 + new_pwm = old_pwm - (c->duty_cycle_max * dec_permille / 1000);
1812 + new_pwm = max(0, new_pwm);
1813 + c->active_duty_cycle = new_pwm;
1815 + dev_dbg(c->dev, "lipo: Decreasing duty_cycle by "
1816 + "%u permille (0x%02X -> 0x%02X)",
1817 + dec_permille, old_pwm, new_pwm);
1819 + return c->set_current_pwm(c, c->active_duty_cycle);
1822 +/** lipocharge_update_state - Update the charge state
1823 + * @c: The context.
1824 + * @voltage_mV: The measured battery voltage.
1825 + * @current_mA: The measured charge current.
1826 + * negative -> drain.
1827 + * positive -> charge.
1828 + * @temp_K: Battery temperature in K.
1830 + * Returns 0 on success, -1 on error.
1831 + * Returns 1, if the charging process is finished.
1833 +int lipocharge_update_state(struct lipocharge *c,
1834 + unsigned int voltage_mV,
1836 + unsigned int temp_K)
1838 + int requested_current, current_diff;
1840 + unsigned int permille;
1845 + switch (c->state) {
1847 + dev_err(c->dev, "%s: called while idle", __func__);
1849 + case LIPO_FIRST_STAGE: /* Constant current */
1850 +//printk("GOT %u %d %u\n", voltage_mV, current_mA, temp_K);
1851 + if (voltage_mV >= c->top_voltage) {
1852 + /* Float voltage reached.
1853 + * Switch charger mode to "constant current" */
1854 + c->state = LIPO_SECOND_STAGE;
1855 + dev_dbg(c->dev, "Switched to second charging stage.");
1858 + /* Float voltage not reached, yet.
1859 + * Try to get the requested constant current. */
1860 + requested_current = get_stage1_charge_current(c);
1861 + if (current_mA < 0)
1863 + current_diff = requested_current - current_mA;
1864 + if (abs(requested_current - current_mA) > CURRENT_HYST) {
1865 + if (current_diff > 0) {
1866 + /* Increase current */
1867 + permille = current_diff * 1000 / requested_current;
1869 + err = lipocharge_increase_current(c, permille);
1873 + /* Decrease current */
1874 + permille = (-current_diff) * 1000 / requested_current;
1876 + err = lipocharge_decrease_current(c, permille);
1882 + case LIPO_SECOND_STAGE: /* Constant voltage */
1889 Index: linux-2.6.37.1/drivers/cbus/lipocharge.h
1890 ===================================================================
1891 --- /dev/null 1970-01-01 00:00:00.000000000 +0000
1892 +++ linux-2.6.37.1/drivers/cbus/lipocharge.h 2011-02-19 20:26:01.531318368 +0100
1894 +#ifndef LIPOCHARGE_H_
1895 +#define LIPOCHARGE_H_
1897 +#include <linux/types.h>
1898 +#include <linux/device.h>
1901 +#define LIPORATE(a,b) (((a) * 1000) + ((b) * 100))
1902 +#define LIPORATE_p6C LIPORATE(0,6) /* 0.6C */
1904 +enum lipocharge_state {
1905 + LIPO_IDLE, /* Not charging */
1906 + LIPO_FIRST_STAGE, /* Charging: constant current */
1907 + LIPO_SECOND_STAGE, /* Charging: constant voltage */
1910 +/** struct lipocharge - A generic LIPO charger
1912 + * @capacity: Battery capacity in mAh.
1913 + * @rate: Charge rate.
1914 + * @top_voltage: Fully charged voltage, in mV.
1915 + * @duty_cycle_max: Max value for duty_cycle.
1917 + * @set_charge_current: Set the charge current PWM duty cycle.
1918 + * @emergency: Something went wrong. Force shutdown.
1920 +struct lipocharge {
1921 + unsigned int capacity;
1922 + unsigned int rate;
1923 + unsigned int top_voltage;
1924 + unsigned int duty_cycle_max;
1926 + int (*set_current_pwm)(struct lipocharge *c, unsigned int duty_cycle);
1927 + void (*emergency)(struct lipocharge *c);
1930 + struct device *dev;
1931 + enum lipocharge_state state;
1932 + unsigned int active_duty_cycle;
1934 + //TODO implement timer to cut power after maximum charge time.
1937 +void lipocharge_init(struct lipocharge *c, struct device *dev);
1938 +void lipocharge_exit(struct lipocharge *c);
1940 +int lipocharge_start(struct lipocharge *c);
1941 +void lipocharge_stop(struct lipocharge *c);
1943 +int lipocharge_update_state(struct lipocharge *c,
1944 + unsigned int voltage_mV,
1946 + unsigned int temp_K);
1948 +static inline bool lipocharge_is_charging(struct lipocharge *c)
1950 + return (c->state != LIPO_IDLE);
1953 +#endif /* LIPOCHARGE_H_ */
1954 Index: linux-2.6.37.1/drivers/cbus/tahvo.h
1955 ===================================================================
1956 --- linux-2.6.37.1.orig/drivers/cbus/tahvo.h 2011-02-19 20:26:01.256340370 +0100
1957 +++ linux-2.6.37.1/drivers/cbus/tahvo.h 2011-02-19 20:26:01.532318288 +0100
1959 #define TAHVO_REG_IDR 0x01 /* Interrupt ID */
1960 #define TAHVO_REG_IDSR 0x02 /* Interrupt status */
1961 #define TAHVO_REG_IMR 0x03 /* Interrupt mask */
1962 +#define TAHVO_REG_CHGCURR 0x04 /* Charge current control PWM (8-bit) */
1963 #define TAHVO_REG_LEDPWMR 0x05 /* LED PWM */
1964 #define TAHVO_REG_USBR 0x06 /* USB control */
1965 +#define TAHVO_REG_CHGCTL 0x08 /* Charge control register */
1966 +#define TAHVO_REG_CHGCTL_EN 0x0001 /* Global charge enable */
1967 +#define TAHVO_REG_CHGCTL_PWMOVR 0x0004 /* PWM override. Force charge PWM to 0%/100% duty cycle. */
1968 +#define TAHVO_REG_CHGCTL_PWMOVRZERO 0x0008 /* If set, PWM override is 0% (If unset -> 100%) */
1969 +#define TAHVO_REG_CHGCTL_CURMEAS 0x0040 /* Enable battery current measurement. */
1970 +#define TAHVO_REG_CHGCTL_CURTIMRST 0x0080 /* Current measure timer reset. */
1971 +#define TAHVO_REG_BATCURRTIMER 0x0c /* Battery current measure timer (8-bit) */
1972 +#define TAHVO_REG_BATCURR 0x0d /* Battery (dis)charge current (signed 16-bit) */
1974 #define TAHVO_REG_MAX 0x0d
1976 /* Interrupt sources */
1977 #define TAHVO_INT_VBUSON 0
1978 +#define TAHVO_INT_BATCURR 7 /* Battery current measure timer */
1980 #define MAX_TAHVO_IRQ_HANDLERS 8
1982 int tahvo_read_reg(int reg);
1983 -void tahvo_write_reg(int reg, u16 val);
1984 +int tahvo_write_reg(int reg, u16 val);
1985 void tahvo_set_clear_reg_bits(int reg, u16 set, u16 clear);
1986 int tahvo_request_irq(int id, void *irq_handler, unsigned long arg, char *name);
1987 void tahvo_free_irq(int id);
1988 Index: linux-2.6.37.1/drivers/cbus/tahvo.c
1989 ===================================================================
1990 --- linux-2.6.37.1.orig/drivers/cbus/tahvo.c 2011-02-19 20:26:01.256340370 +0100
1991 +++ linux-2.6.37.1/drivers/cbus/tahvo.c 2011-02-19 20:26:01.532318288 +0100
1994 * This function writes a value to the specified register
1996 -void tahvo_write_reg(int reg, u16 val)
1997 +int tahvo_write_reg(int reg, u16 val)
1999 BUG_ON(!tahvo_initialized);
2000 - cbus_write_reg(cbus_host, TAHVO_ID, reg, val);
2001 + return cbus_write_reg(cbus_host, TAHVO_ID, reg, val);
2005 Index: linux-2.6.37.1/drivers/cbus/cbus.c
2006 ===================================================================
2007 --- linux-2.6.37.1.orig/drivers/cbus/cbus.c 2011-02-19 20:26:01.249340930 +0100
2008 +++ linux-2.6.37.1/drivers/cbus/cbus.c 2011-02-19 20:26:01.533318208 +0100
2010 #include <linux/gpio.h>
2011 #include <linux/platform_device.h>
2012 #include <linux/slab.h>
2013 +#include <linux/reboot.h>
2016 #include <asm/mach-types.h>
2017 @@ -301,6 +302,13 @@
2019 module_exit(cbus_bus_exit);
2021 +void cbus_emergency(void)
2023 + machine_power_off();
2024 + panic("cbus: Failed to halt machine in emergency state\n");
2026 +EXPORT_SYMBOL(cbus_emergency);
2028 MODULE_DESCRIPTION("CBUS serial protocol");
2029 MODULE_LICENSE("GPL");
2030 MODULE_AUTHOR("Juha Yrjölä");
2031 Index: linux-2.6.37.1/drivers/cbus/cbus.h
2032 ===================================================================
2033 --- linux-2.6.37.1.orig/drivers/cbus/cbus.h 2011-02-19 20:26:01.250340850 +0100
2034 +++ linux-2.6.37.1/drivers/cbus/cbus.h 2011-02-19 20:26:01.533318208 +0100
2036 extern int cbus_read_reg(struct cbus_host *host, int dev, int reg);
2037 extern int cbus_write_reg(struct cbus_host *host, int dev, int reg, u16 val);
2039 +NORET_TYPE void cbus_emergency(void) ATTRIB_NORET;
2041 #endif /* __DRIVERS_CBUS_CBUS_H */