1 Index: linux-3.1/drivers/cbus/Kconfig
2 ===================================================================
3 --- linux-3.1.orig/drivers/cbus/Kconfig 2011-11-05 17:03:39.578846146 +0100
4 +++ linux-3.1/drivers/cbus/Kconfig 2011-11-05 17:06:51.589348749 +0100
5 @@ -83,4 +83,12 @@ config CBUS_RETU_HEADSET
10 + depends on CBUS_RETU && CBUS_TAHVO
11 + tristate "Nokia n810 battery management"
13 + Nokia n810 device battery management.
18 Index: linux-3.1/drivers/cbus/Makefile
19 ===================================================================
20 --- linux-3.1.orig/drivers/cbus/Makefile 2011-11-05 17:03:39.618846091 +0100
21 +++ linux-3.1/drivers/cbus/Makefile 2011-11-05 17:06:51.589348749 +0100
22 @@ -11,3 +11,6 @@ obj-$(CONFIG_CBUS_RETU_POWERBUTTON) += r
23 obj-$(CONFIG_CBUS_RETU_RTC) += retu-rtc.o
24 obj-$(CONFIG_CBUS_RETU_WDT) += retu-wdt.o
25 obj-$(CONFIG_CBUS_RETU_HEADSET) += retu-headset.o
26 +n810bm-y += n810bm_main.o
27 +n810bm-y += lipocharge.o
28 +obj-$(CONFIG_N810BM) += n810bm.o
29 Index: linux-3.1/drivers/cbus/n810bm_main.c
30 ===================================================================
31 --- /dev/null 1970-01-01 00:00:00.000000000 +0000
32 +++ linux-3.1/drivers/cbus/n810bm_main.c 2011-11-05 17:06:51.589348749 +0100
35 + * Nokia n810 battery management
37 + * WARNING: This driver is based on unconfirmed documentation.
38 + * It is possibly dangerous to use this software.
39 + * Use this software at your own risk!
41 + * Copyright (c) 2010-2011 Michael Buesch <mb@bu3sch.de>
43 + * This program is free software; you can redistribute it and/or
44 + * modify it under the terms of the GNU General Public License
45 + * as published by the Free Software Foundation; either version 2
46 + * of the License, or (at your option) any later version.
48 + * This program is distributed in the hope that it will be useful,
49 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
50 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
51 + * GNU General Public License for more details.
56 +#include <linux/module.h>
57 +#include <linux/device.h>
58 +#include <linux/platform_device.h>
59 +#include <linux/slab.h>
60 +#include <linux/mutex.h>
61 +#include <linux/timer.h>
62 +#include <linux/firmware.h>
63 +#include <linux/bitops.h>
64 +#include <linux/workqueue.h>
65 +#include <linux/delay.h>
66 +#include <linux/interrupt.h>
71 +#include "lipocharge.h"
74 +#define N810BM_PMM_BLOCK_FILENAME "n810-cal-bme-pmm.fw"
75 +#define N810BM_PMM_BLOCK_SIZE 0x600
76 +#define N810BM_PMM_GROUP_SIZE 0x200
77 +#define N810BM_PMM_ELEM_SIZE 0x10
79 +#define N810BM_CHECK_INTERVAL (HZ * 2)
80 +#define N810BM_MIN_VOLTAGE_THRES 3200 /* Absolute minimum voltage threshold */
84 + * The battery size indicator ADC measures the resistance between
85 + * the battery BSI pin and ground. This is used to detect the battery
86 + * capacity, as the BSI resistor is related to capacity.
88 + * Manually measured lookup table.
89 + * Hard to measure, thus not very accurate.
91 + * Resistance | ADC value
92 + * ========================
100 + * Manually measured lookup table.
101 + * Hard to measure, thus not very accurate.
103 + * Voltage | ADC value
104 + * =====================
123 +/* PMM block ADC IDs */
124 +enum n810bm_pmm_adc_id {
125 + N810BM_PMM_ADC_BATVOLT = 0x01, /* Battery voltage */
126 + N810BM_PMM_ADC_CHGVOLT = 0x02, /* Charger voltage */
127 + N810BM_PMM_ADC_GND2 = 0x03, /* Ground 0V */
128 + N810BM_PMM_ADC_BSI = 0x04, /* Battery size indicator */
129 + N810BM_PMM_ADC_BATTEMP = 0x05, /* Battery temperature */
130 + N810BM_PMM_ADC_HEADSET = 0x06, /* Headset detection */
131 + N810BM_PMM_ADC_HOOKDET = 0x07, /* Hook detection */
132 + N810BM_PMM_ADC_LIGHTSENS = 0x08, /* Light sensor */
133 + N810BM_PMM_ADC_BATCURR = 0x0E, /* Battery current */
134 + N810BM_PMM_ADC_BKUPVOLT = 0x13, /* Backup battery voltage */
135 + N810BM_PMM_ADC_LIGHTTEMP = 0x14, /* Light sensor temperature */
136 + N810BM_PMM_ADC_RFGP = 0x15, /* RF GP */
137 + N810BM_PMM_ADC_WBTX = 0x16, /* Wideband TX detection */
138 + N810BM_PMM_ADC_RETUTEMP = 0x17, /* RETU chip temperature */
139 + N810BM_PMM_ADC_0xFE = 0xFE,
142 +struct n810bm_adc_calib {
143 + enum n810bm_pmm_adc_id id;
152 +struct n810bm_calib {
153 + struct n810bm_adc_calib adc[25];
156 +enum n810bm_capacity {
157 + N810BM_CAP_UNKNOWN = -1,
158 + N810BM_CAP_NONE = 0,
159 + N810BM_CAP_1500MAH = 1500, /* 1500 mAh battery */
162 +enum n810bm_notify_flags {
163 + N810BM_NOTIFY_charger_present,
164 + N810BM_NOTIFY_charger_state,
165 + N810BM_NOTIFY_charger_pwm,
170 + bool tahvo_irq_enabled;
172 + bool battery_present; /* A battery is inserted */
173 + bool charger_present; /* The charger is connected */
174 + enum n810bm_capacity capacity; /* The capacity of the inserted battery (if any) */
176 + bool charger_enabled; /* Want to charge? */
177 + struct lipocharge charger; /* Charger subsystem */
178 + unsigned int active_current_pwm; /* Active value of TAHVO_REG_CHGCURR */
179 + int current_measure_enabled; /* Current measure enable refcount */
181 + struct platform_device *pdev;
182 + struct n810bm_calib calib; /* Calibration data */
184 + bool verbose_charge_log; /* Verbose charge logging */
186 + unsigned long notify_flags;
187 + struct work_struct notify_work;
188 + struct delayed_work periodic_check_work;
190 + bool initialized; /* The hardware was initialized */
191 + struct mutex mutex;
194 +static void n810bm_notify_charger_present(struct n810bm *bm);
195 +static void n810bm_notify_charger_state(struct n810bm *bm);
196 +static void n810bm_notify_charger_pwm(struct n810bm *bm);
199 +static struct platform_device *n810bm_retu_device;
200 +static struct platform_device *n810bm_tahvo_device;
203 +static inline struct n810bm * device_to_n810bm(struct device *dev)
205 + struct platform_device *pdev = to_platform_device(dev);
206 + struct n810bm *bm = platform_get_drvdata(pdev);
211 +static inline bool n810bm_known_battery_present(struct n810bm *bm)
213 + return bm->battery_present &&
214 + bm->capacity != N810BM_CAP_UNKNOWN &&
215 + bm->capacity != N810BM_CAP_NONE;
218 +static NORET_TYPE void n810bm_emergency(struct n810bm *bm, const char *message) ATTRIB_NORET;
219 +static void n810bm_emergency(struct n810bm *bm, const char *message)
221 + printk(KERN_EMERG "n810 battery management fatal fault: %s\n", message);
225 +static u16 tahvo_read(struct n810bm *bm, unsigned int reg)
227 + return tahvo_read_reg(&n810bm_tahvo_device->dev, reg);
230 +static void tahvo_maskset(struct n810bm *bm, unsigned int reg, u16 mask, u16 set)
232 + tahvo_set_clear_reg_bits(&n810bm_tahvo_device->dev, reg, set, mask);
235 +static inline void tahvo_write(struct n810bm *bm, unsigned int reg, u16 value)
237 + tahvo_write_reg(&n810bm_tahvo_device->dev, reg, value);
240 +static inline void tahvo_set(struct n810bm *bm, unsigned int reg, u16 mask)
242 + tahvo_set_clear_reg_bits(&n810bm_tahvo_device->dev, reg, mask, mask);
245 +static inline void tahvo_clear(struct n810bm *bm, unsigned int reg, u16 mask)
247 + tahvo_set_clear_reg_bits(&n810bm_tahvo_device->dev, reg, 0, mask);
250 +static u16 retu_read(struct n810bm *bm, unsigned int reg)
252 + return retu_read_reg(&n810bm_retu_device->dev, reg);
255 +static void retu_maskset(struct n810bm *bm, unsigned int reg, u16 mask, u16 set)
257 + retu_set_clear_reg_bits(&n810bm_retu_device->dev, reg, set, mask);
260 +static inline void retu_write(struct n810bm *bm, unsigned int reg, u16 value)
262 + retu_write_reg(&n810bm_retu_device->dev, reg, value);
265 +static int retu_adc_average(struct n810bm *bm, unsigned int chan,
266 + unsigned int nr_passes)
268 + unsigned int i, value = 0;
271 + if (WARN_ON(!nr_passes))
273 + for (i = 0; i < nr_passes; i++) {
274 + ret = retu_read_adc(&n810bm_retu_device->dev, chan);
279 + value /= nr_passes;
284 +static struct n810bm_adc_calib * n810bm_get_adc_calib(struct n810bm *bm,
285 + enum n810bm_pmm_adc_id id)
287 + unsigned int index = 0;
288 + struct n810bm_adc_calib *cal;
290 + if (id != N810BM_PMM_ADC_0xFE)
291 + index = (unsigned int)id + 1;
292 + if (index >= ARRAY_SIZE(bm->calib.adc))
295 + cal = &bm->calib.adc[index];
296 + WARN_ON(cal->id && cal->id != id);
301 +static int pmm_record_get(struct n810bm *bm,
302 + const struct firmware *pmm_block,
303 + void *buffer, size_t length,
304 + unsigned int group, unsigned int element, unsigned int offset)
306 + const u8 *pmm_area = pmm_block->data;
307 + u8 active_group_mask;
309 + if (pmm_block->size != N810BM_PMM_BLOCK_SIZE)
311 + if (group >= N810BM_PMM_BLOCK_SIZE / N810BM_PMM_GROUP_SIZE)
313 + if (element >= N810BM_PMM_GROUP_SIZE / N810BM_PMM_ELEM_SIZE)
315 + if (offset >= N810BM_PMM_ELEM_SIZE || length > N810BM_PMM_ELEM_SIZE ||
316 + length + offset > N810BM_PMM_ELEM_SIZE)
319 + active_group_mask = pmm_area[16];
320 + if (!(active_group_mask & (1 << group))) {
321 + dev_dbg(&bm->pdev->dev, "pwm_record_get: Requested group %u, "
322 + "but group is not active", group);
327 + pmm_area + group * N810BM_PMM_GROUP_SIZE
328 + + element * N810BM_PMM_ELEM_SIZE
335 +/* PMM block group 1 element */
336 +struct group1_element {
345 +static int extract_group1_elem(struct n810bm *bm,
346 + const struct firmware *pmm_block,
347 + const enum n810bm_pmm_adc_id *pmm_adc_ids, size_t nr_pmm_adc_ids,
348 + u32 field1_mask, u32 field2_mask)
350 + struct group1_element elem;
352 + unsigned int i, element_nr;
353 + struct n810bm_adc_calib *adc_calib;
355 + for (i = 0; i < nr_pmm_adc_ids; i++) {
356 + element_nr = (unsigned int)(pmm_adc_ids[i]) + 3;
358 + err = pmm_record_get(bm, pmm_block, &elem, sizeof(elem),
362 + adc_calib = n810bm_get_adc_calib(bm, elem.id);
364 + dev_err(&bm->pdev->dev, "extract_group1_elem: "
365 + "Could not get calib element for 0x%02X",
370 + if (adc_calib->flags == elem.flags) {
371 + adc_calib->field1 = le32_to_cpu(elem.field1) & field1_mask;
372 + adc_calib->field2 = le32_to_cpu(elem.field2) & field2_mask;
374 + dev_dbg(&bm->pdev->dev, "extract_group1_elem: "
375 + "Not extracting fields due to flags mismatch: "
376 + "0x%02X vs 0x%02X",
377 + adc_calib->flags, elem.flags);
384 +static int n810bm_parse_pmm_group1(struct n810bm *bm,
385 + const struct firmware *pmm_block)
387 + struct n810bm_adc_calib *adc_calib;
388 + struct group1_element elem;
391 + static const enum n810bm_pmm_adc_id pmm_adc_ids_1[] = {
392 + N810BM_PMM_ADC_BATVOLT,
393 + N810BM_PMM_ADC_CHGVOLT,
394 + N810BM_PMM_ADC_BKUPVOLT,
395 + N810BM_PMM_ADC_BATCURR,
397 + static const enum n810bm_pmm_adc_id pmm_adc_ids_2[] = {
398 + N810BM_PMM_ADC_BSI,
400 + static const enum n810bm_pmm_adc_id pmm_adc_ids_3[] = {
401 + N810BM_PMM_ADC_BATTEMP,
404 + /* Parse element 2 */
405 + err = pmm_record_get(bm, pmm_block, &elem, sizeof(elem),
408 + dev_err(&bm->pdev->dev,
409 + "PMM: Failed to get group 1 / element 2");
412 + if (elem.id == N810BM_PMM_ADC_0xFE && elem.flags == 0x05) {
413 + adc_calib = n810bm_get_adc_calib(bm, elem.id);
415 + dev_err(&bm->pdev->dev,
416 + "calib extract: Failed to get 0xFE calib");
419 + adc_calib->id = elem.id;
420 + adc_calib->flags = elem.flags;
421 + adc_calib->field1 = le32_to_cpu(elem.field1);
422 + adc_calib->field2 = le32_to_cpu(elem.field2);
425 + err = extract_group1_elem(bm, pmm_block,
426 + pmm_adc_ids_1, ARRAY_SIZE(pmm_adc_ids_1),
427 + 0xFFFFFFFF, 0xFFFFFFFF);
430 + err = extract_group1_elem(bm, pmm_block,
431 + pmm_adc_ids_2, ARRAY_SIZE(pmm_adc_ids_2),
435 + err = extract_group1_elem(bm, pmm_block,
436 + pmm_adc_ids_3, ARRAY_SIZE(pmm_adc_ids_3),
437 + 0xFFFFFFFF, 0x0000FFFF);
444 +static int n810bm_parse_pmm_group2(struct n810bm *bm,
445 + const struct firmware *pmm_block)
447 + dev_err(&bm->pdev->dev, "TODO: CAL BME PMM group 2 parser not implemented, yet");
448 + return -EOPNOTSUPP;
451 +static void n810bm_adc_calib_set_defaults(struct n810bm *bm)
453 + struct n810bm_adc_calib *adc_calib;
456 + static const struct n810bm_adc_calib defaults[] = {
457 + /* ADC group-nr 0 */
459 + .id = N810BM_PMM_ADC_HEADSET,
463 + .id = N810BM_PMM_ADC_HOOKDET,
467 + .id = N810BM_PMM_ADC_RFGP,
471 + .id = N810BM_PMM_ADC_LIGHTSENS,
475 + .id = N810BM_PMM_ADC_WBTX,
479 + .id = N810BM_PMM_ADC_RETUTEMP,
483 + .id = N810BM_PMM_ADC_GND2,
487 + /* ADC group-nr 1 */
489 + .id = N810BM_PMM_ADC_0xFE,
495 + .id = N810BM_PMM_ADC_BATVOLT,
501 + .id = N810BM_PMM_ADC_CHGVOLT,
507 + .id = N810BM_PMM_ADC_BKUPVOLT,
513 + .id = N810BM_PMM_ADC_BATCURR,
519 + /* ADC group-nr 2 */
521 + .id = N810BM_PMM_ADC_BSI,
527 + /* ADC group-nr 3 */
529 + .id = N810BM_PMM_ADC_BATTEMP,
532 + .field1 = 265423000,
535 + /* ADC group-nr 4 */
537 + .id = N810BM_PMM_ADC_LIGHTTEMP,
540 + .field1 = 19533778,
541 + .field2 = 308019670,
547 + /* Clear the array */
548 + memset(&bm->calib.adc, 0, sizeof(bm->calib.adc));
549 + for (i = 0; i < ARRAY_SIZE(bm->calib.adc); i++)
550 + bm->calib.adc[i].flags = 0xFF;
552 + /* Copy the defaults */
553 + for (i = 0; i < ARRAY_SIZE(defaults); i++) {
554 + adc_calib = n810bm_get_adc_calib(bm, defaults[i].id);
555 + if (WARN_ON(!adc_calib))
557 + *adc_calib = defaults[i];
561 +static int n810bm_parse_pmm_block(struct n810bm *bm,
562 + const struct firmware *pmm_block)
566 + unsigned int i, count;
567 + struct n810bm_adc_calib *adc_calib;
569 + /* Initialize to defaults */
570 + n810bm_adc_calib_set_defaults(bm);
572 + /* Parse the PMM data */
573 + err = pmm_record_get(bm, pmm_block, &byte, sizeof(byte),
574 + 1, 0, 0); /* group 1 / element 0 */
575 + err |= (byte != 0x01);
576 + err |= pmm_record_get(bm, pmm_block, &byte, sizeof(byte),
577 + 1, 1, 0); /* group 1 / element 1 */
578 + err |= (byte != 0x01);
580 + err = n810bm_parse_pmm_group2(bm, pmm_block);
582 + err = n810bm_parse_pmm_group1(bm, pmm_block);
586 + /* Sanity checks */
587 + for (i = 0, count = 0; i < ARRAY_SIZE(bm->calib.adc); i++) {
588 + adc_calib = &bm->calib.adc[i];
589 + if (adc_calib->flags == 0xFF)
591 + switch (adc_calib->id) {
592 + case N810BM_PMM_ADC_BATVOLT:
593 + if (adc_calib->field1 < 2400 ||
594 + adc_calib->field1 > 2700)
595 + goto value_check_fail;
596 + if (adc_calib->field2 < 20000 ||
597 + adc_calib->field2 > 23000)
598 + goto value_check_fail;
601 + case N810BM_PMM_ADC_BSI:
602 + if (adc_calib->field1 < 1100 ||
603 + adc_calib->field1 > 1300)
604 + goto value_check_fail;
607 + case N810BM_PMM_ADC_BATCURR:
608 + if (adc_calib->field2 < 7000 ||
609 + adc_calib->field2 > 12000)
610 + goto value_check_fail;
613 + case N810BM_PMM_ADC_0xFE:
614 + if ((s32)adc_calib->field1 > 14 ||
615 + (s32)adc_calib->field1 < -14)
616 + goto value_check_fail;
617 + if (adc_calib->field2 < 13000 ||
618 + adc_calib->field2 > 13350)
619 + goto value_check_fail;
622 + case N810BM_PMM_ADC_CHGVOLT:
623 + case N810BM_PMM_ADC_BATTEMP:
624 + case N810BM_PMM_ADC_BKUPVOLT:
627 + case N810BM_PMM_ADC_GND2:
628 + case N810BM_PMM_ADC_HOOKDET:
629 + case N810BM_PMM_ADC_LIGHTSENS:
630 + case N810BM_PMM_ADC_HEADSET:
631 + case N810BM_PMM_ADC_LIGHTTEMP:
632 + case N810BM_PMM_ADC_RFGP:
633 + case N810BM_PMM_ADC_WBTX:
634 + case N810BM_PMM_ADC_RETUTEMP:
637 + dev_dbg(&bm->pdev->dev,
638 + "ADC 0x%02X calib: 0x%02X 0x%02X 0x%08X 0x%08X 0x%04X 0x%04X",
639 + adc_calib->id, adc_calib->flags, adc_calib->adc_groupnr,
640 + adc_calib->field1, adc_calib->field2,
641 + adc_calib->field3, adc_calib->field4);
644 + dev_err(&bm->pdev->dev, "PMM sanity check: Did not find "
645 + "all required values (count=%u)", count);
652 + dev_err(&bm->pdev->dev, "PMM image sanity check failed "
653 + "(id=%02X, field1=%08X, field2=%08X)",
654 + adc_calib->id, adc_calib->field1, adc_calib->field2);
659 +/* Set the current measure timer that triggers on Tahvo IRQ 7
660 + * An interval of zero disables the timer. */
661 +static void n810bm_set_current_measure_timer(struct n810bm *bm,
662 + u16 millisec_interval)
664 + u16 value = millisec_interval;
666 + if (value <= 0xF905) {
667 + value = ((u64)0x10624DD3 * (u64)(value + 0xF9)) >> 32;
672 + tahvo_write(bm, TAHVO_REG_BATCURRTIMER, value & 0xFF);
674 + tahvo_set(bm, TAHVO_REG_CHGCTL,
675 + TAHVO_REG_CHGCTL_CURTIMRST);
676 + tahvo_clear(bm, TAHVO_REG_CHGCTL,
677 + TAHVO_REG_CHGCTL_CURTIMRST);
679 + if (millisec_interval) {
680 + if (!bm->tahvo_irq_enabled) {
681 + bm->tahvo_irq_enabled = 1;
682 + enable_irq(bm->tahvo_irq);
685 + if (bm->tahvo_irq_enabled) {
686 + bm->tahvo_irq_enabled = 0;
687 + disable_irq_nosync(bm->tahvo_irq);
691 + //TODO also do a software timer for safety.
694 +static void n810bm_enable_current_measure(struct n810bm *bm)
696 + WARN_ON(bm->current_measure_enabled < 0);
697 + if (!bm->current_measure_enabled) {
698 + /* Enable the current measurement circuitry */
699 + tahvo_set(bm, TAHVO_REG_CHGCTL,
700 + TAHVO_REG_CHGCTL_CURMEAS);
701 + dev_dbg(&bm->pdev->dev,
702 + "Current measurement circuitry enabled");
704 + bm->current_measure_enabled++;
707 +static void n810bm_disable_current_measure(struct n810bm *bm)
709 + bm->current_measure_enabled--;
710 + WARN_ON(bm->current_measure_enabled < 0);
711 + if (!bm->current_measure_enabled) {
712 + /* Disable the current measurement circuitry */
713 + tahvo_clear(bm, TAHVO_REG_CHGCTL,
714 + TAHVO_REG_CHGCTL_CURMEAS);
715 + dev_dbg(&bm->pdev->dev,
716 + "Current measurement circuitry disabled");
720 +/* Measure the actual battery current. Returns a signed value in mA.
721 + * Does only work, if current measurement was enabled. */
722 +static int n810bm_measure_batt_current(struct n810bm *bm)
725 + int adc = 0, ma, i;
727 + if (WARN_ON(bm->current_measure_enabled <= 0))
729 + for (i = 0; i < 3; i++) {
730 + retval = tahvo_read(bm, TAHVO_REG_BATCURR);
731 + adc += (s16)retval; /* Value is signed */
735 + //TODO convert to mA
741 +/* Requires bm->mutex locked */
742 +static int n810bm_measure_batt_current_async(struct n810bm *bm)
745 + bool charging = lipocharge_is_charging(&bm->charger);
747 + n810bm_enable_current_measure(bm);
749 + WARN_ON(bm->active_current_pwm != 0);
750 + tahvo_maskset(bm, TAHVO_REG_CHGCTL,
751 + TAHVO_REG_CHGCTL_EN |
752 + TAHVO_REG_CHGCTL_PWMOVR |
753 + TAHVO_REG_CHGCTL_PWMOVRZERO,
754 + TAHVO_REG_CHGCTL_EN |
755 + TAHVO_REG_CHGCTL_PWMOVR |
756 + (charging ? 0 : TAHVO_REG_CHGCTL_PWMOVRZERO));
757 + ma = n810bm_measure_batt_current(bm);
758 + tahvo_maskset(bm, TAHVO_REG_CHGCTL,
759 + TAHVO_REG_CHGCTL_EN |
760 + TAHVO_REG_CHGCTL_PWMOVR |
761 + TAHVO_REG_CHGCTL_PWMOVRZERO,
762 + (charging ? TAHVO_REG_CHGCTL_EN : 0));
763 + n810bm_disable_current_measure(bm);
768 +static int adc_sanity_check(struct n810bm *bm, unsigned int channel)
772 + value = retu_read_adc(&n810bm_retu_device->dev, channel);
774 + dev_err(&bm->pdev->dev, "Failed to read GND ADC channel %u",
778 + dev_dbg(&bm->pdev->dev,
779 + "GND ADC channel %u sanity check got value: %d",
782 + n810bm_emergency(bm, "GND ADC sanity check failed");
789 +static int n810bm_check_adc_sanity(struct n810bm *bm)
793 + /* Discard one conversion */
794 + retu_write(bm, RETU_REG_ADCSCR, 0);
795 + retu_read_adc(&n810bm_retu_device->dev, RETU_ADC_GND2);
797 + err = adc_sanity_check(bm, RETU_ADC_GND2);
804 +/* Measure the battery voltage. Returns the value in mV (or negative value on error). */
805 +static int n810bm_measure_batt_voltage(struct n810bm *bm)
809 + const unsigned int scale = 1000;
811 + adc = retu_adc_average(bm, RETU_ADC_BATTVOLT, 5);
816 + mv = 2800 + ((adc - 0x37) * (((4200 - 2800) * scale) / (0x236 - 0x37))) / scale;
818 + //TODO compensate for power consumption
819 + //TODO honor calibration values
824 +/* Measure the charger voltage. Returns the value in mV (or negative value on error). */
825 +static int n810bm_measure_charger_voltage(struct n810bm *bm)
830 + adc = retu_adc_average(bm, RETU_ADC_CHGVOLT, 5);
833 + //TODO convert to mV
839 +/* Measure backup battery voltage. Returns the value in mV (or negative value on error). */
840 +static int n810bm_measure_backup_batt_voltage(struct n810bm *bm)
845 + adc = retu_adc_average(bm, RETU_ADC_BKUPVOLT, 3);
848 + //TODO convert to mV
854 +/* Measure the battery temperature. Returns the value in K (or negative value on error). */
855 +static int n810bm_measure_batt_temp(struct n810bm *bm)
860 + adc = retu_adc_average(bm, RETU_ADC_BATTEMP, 3);
863 + //TODO convert to K
869 +/* Read the battery capacity via BSI pin. */
870 +static enum n810bm_capacity n810bm_read_batt_capacity(struct n810bm *bm)
873 + const unsigned int hyst = 20;
875 + adc = retu_adc_average(bm, RETU_ADC_BSI, 5);
877 + dev_err(&bm->pdev->dev, "Failed to read BSI ADC");
878 + return N810BM_CAP_UNKNOWN;
881 + if (adc >= 0x3B5 - hyst && adc <= 0x3B5 + hyst)
882 + return N810BM_CAP_1500MAH;
884 + dev_err(&bm->pdev->dev, "Capacity indicator 0x%X unknown", adc);
886 + return N810BM_CAP_UNKNOWN;
889 +/* Convert a battery voltage (in mV) to percentage. */
890 +static unsigned int n810bm_mvolt2percent(unsigned int mv)
892 + const unsigned int minv = 3700;
893 + const unsigned int maxv = 4150;
894 + unsigned int percent;
896 + mv = clamp(mv, minv, maxv);
897 + percent = (mv - minv) * 100 / (maxv - minv);
902 +static void n810bm_start_charge(struct n810bm *bm)
906 + WARN_ON(!bm->battery_present);
907 + WARN_ON(!bm->charger_present);
909 + /* Set PWM to zero */
910 + bm->active_current_pwm = 0;
911 + tahvo_write(bm, TAHVO_REG_CHGCURR, bm->active_current_pwm);
913 + /* Charge global enable */
914 + tahvo_maskset(bm, TAHVO_REG_CHGCTL,
915 + TAHVO_REG_CHGCTL_EN |
916 + TAHVO_REG_CHGCTL_PWMOVR |
917 + TAHVO_REG_CHGCTL_PWMOVRZERO,
918 + TAHVO_REG_CHGCTL_EN);
920 + WARN_ON((int)bm->capacity <= 0);
921 + bm->charger.capacity = bm->capacity;
922 + err = lipocharge_start(&bm->charger);
925 + /* Initialize current measurement circuitry */
926 + n810bm_enable_current_measure(bm);
927 + n810bm_set_current_measure_timer(bm, 250);
929 + dev_info(&bm->pdev->dev, "Charging battery");
930 + n810bm_notify_charger_state(bm);
931 + n810bm_notify_charger_pwm(bm);
934 +static void n810bm_stop_charge(struct n810bm *bm)
936 + if (lipocharge_is_charging(&bm->charger)) {
937 + n810bm_set_current_measure_timer(bm, 0);
938 + n810bm_disable_current_measure(bm);
940 + lipocharge_stop(&bm->charger);
942 + /* Set PWM to zero */
943 + bm->active_current_pwm = 0;
944 + tahvo_write(bm, TAHVO_REG_CHGCURR, bm->active_current_pwm);
946 + /* Charge global disable */
947 + tahvo_maskset(bm, TAHVO_REG_CHGCTL,
948 + TAHVO_REG_CHGCTL_EN |
949 + TAHVO_REG_CHGCTL_PWMOVR |
950 + TAHVO_REG_CHGCTL_PWMOVRZERO,
953 + dev_info(&bm->pdev->dev, "Not charging battery");
954 + n810bm_notify_charger_state(bm);
955 + n810bm_notify_charger_pwm(bm);
958 +/* Periodic check */
959 +static void n810bm_periodic_check_work(struct work_struct *work)
961 + struct n810bm *bm = container_of(to_delayed_work(work),
962 + struct n810bm, periodic_check_work);
964 + bool battery_was_present, charger_was_present;
967 + mutex_lock(&bm->mutex);
969 + status = retu_read(bm, RETU_REG_STATUS);
970 + battery_was_present = bm->battery_present;
971 + charger_was_present = bm->charger_present;
972 + bm->battery_present = !!(status & RETU_REG_STATUS_BATAVAIL);
973 + bm->charger_present = !!(status & RETU_REG_STATUS_CHGPLUG);
975 + if (bm->battery_present != battery_was_present) {
976 + /* Battery state changed */
977 + if (bm->battery_present) {
978 + bm->capacity = n810bm_read_batt_capacity(bm);
979 + if (bm->capacity == N810BM_CAP_UNKNOWN) {
980 + dev_err(&bm->pdev->dev, "Unknown battery detected");
982 + dev_info(&bm->pdev->dev, "Detected %u mAh battery",
983 + (unsigned int)bm->capacity);
986 + bm->capacity = N810BM_CAP_NONE;
987 + dev_info(&bm->pdev->dev, "The main battery was removed");
988 + //TODO disable charging
992 + if (bm->charger_present != charger_was_present) {
993 + /* Charger state changed */
994 + dev_info(&bm->pdev->dev, "The charger was %s",
995 + bm->charger_present ? "plugged in" : "removed");
996 + n810bm_notify_charger_present(bm);
999 + if ((bm->battery_present && !bm->charger_present) ||
1000 + !n810bm_known_battery_present(bm)){
1001 + /* We're draining the battery */
1002 + mv = n810bm_measure_batt_voltage(bm);
1004 + n810bm_emergency(bm,
1005 + "check: Failed to measure voltage");
1007 + if (mv < N810BM_MIN_VOLTAGE_THRES) {
1008 + n810bm_emergency(bm,
1009 + "check: Minimum voltage threshold reached");
1013 + if (bm->charger_present && n810bm_known_battery_present(bm)) {
1014 + /* Known battery and charger are connected */
1015 + if (bm->charger_enabled) {
1016 + /* Charger is enabled */
1017 + if (!lipocharge_is_charging(&bm->charger)) {
1018 + //TODO start charging, if battery is below some threshold
1019 + n810bm_start_charge(bm);
1024 + if (lipocharge_is_charging(&bm->charger) && !bm->charger_present) {
1025 + /* Charger was unplugged. */
1026 + n810bm_stop_charge(bm);
1029 + mutex_unlock(&bm->mutex);
1030 + schedule_delayed_work(&bm->periodic_check_work,
1031 + round_jiffies_relative(N810BM_CHECK_INTERVAL));
1035 +static void n810bm_adc_irq_handler(unsigned long data)
1037 + struct n810bm *bm = (struct n810bm *)data;
1039 + retu_ack_irq(RETU_INT_ADCS);
1041 +dev_info(&bm->pdev->dev, "ADC interrupt triggered\n");
1045 +static irqreturn_t n810bm_tahvo_current_measure_irq_handler(int irq, void *data)
1047 + struct n810bm *bm = data;
1048 + int res, ma, mv, temp;
1050 + mutex_lock(&bm->mutex);
1051 + if (!lipocharge_is_charging(&bm->charger))
1054 + tahvo_maskset(bm, TAHVO_REG_CHGCTL,
1055 + TAHVO_REG_CHGCTL_PWMOVR |
1056 + TAHVO_REG_CHGCTL_PWMOVRZERO,
1057 + TAHVO_REG_CHGCTL_PWMOVR);
1058 + ma = n810bm_measure_batt_current(bm);
1059 + tahvo_maskset(bm, TAHVO_REG_CHGCTL,
1060 + TAHVO_REG_CHGCTL_PWMOVR |
1061 + TAHVO_REG_CHGCTL_PWMOVRZERO,
1062 + TAHVO_REG_CHGCTL_PWMOVR |
1063 + TAHVO_REG_CHGCTL_PWMOVRZERO);
1065 + mv = n810bm_measure_batt_voltage(bm);
1066 + tahvo_maskset(bm, TAHVO_REG_CHGCTL,
1067 + TAHVO_REG_CHGCTL_PWMOVR |
1068 + TAHVO_REG_CHGCTL_PWMOVRZERO,
1070 + temp = n810bm_measure_batt_temp(bm);
1071 + if (WARN_ON(mv < 0))
1073 + if (WARN_ON(temp < 0))
1076 + if (bm->verbose_charge_log) {
1077 + dev_info(&bm->pdev->dev,
1078 + "Battery charge state: %d mV, %d mA (%s)",
1080 + (ma <= 0) ? "discharging" : "charging");
1082 + res = lipocharge_update_state(&bm->charger, mv, ma, temp);
1085 + dev_info(&bm->pdev->dev, "Battery fully charged");
1086 + n810bm_stop_charge(bm);
1089 + mutex_unlock(&bm->mutex);
1091 + return IRQ_HANDLED;
1094 +#define DEFINE_ATTR_NOTIFY(attr_name) \
1095 + void n810bm_notify_##attr_name(struct n810bm *bm) \
1097 + set_bit(N810BM_NOTIFY_##attr_name, &bm->notify_flags); \
1099 + schedule_work(&bm->notify_work); \
1102 +#define DEFINE_SHOW_INT_FUNC(name, member) \
1103 + static ssize_t n810bm_attr_##name##_show(struct device *dev, \
1104 + struct device_attribute *attr, \
1107 + struct n810bm *bm = device_to_n810bm(dev); \
1110 + mutex_lock(&bm->mutex); \
1111 + count = snprintf(buf, PAGE_SIZE, "%d\n", (int)(bm->member)); \
1112 + mutex_unlock(&bm->mutex); \
1117 +#define DEFINE_STORE_INT_FUNC(name, member) \
1118 + static ssize_t n810bm_attr_##name##_store(struct device *dev, \
1119 + struct device_attribute *attr,\
1120 + const char *buf, size_t count)\
1122 + struct n810bm *bm = device_to_n810bm(dev); \
1126 + mutex_lock(&bm->mutex); \
1127 + err = strict_strtol(buf, 0, &val); \
1129 + bm->member = (typeof(bm->member))val; \
1130 + mutex_unlock(&bm->mutex); \
1132 + return err ? err : count; \
1135 +#define DEFINE_ATTR_SHOW_INT(name, member) \
1136 + DEFINE_SHOW_INT_FUNC(name, member) \
1137 + static DEVICE_ATTR(name, S_IRUGO, \
1138 + n810bm_attr_##name##_show, NULL);
1140 +#define DEFINE_ATTR_SHOW_STORE_INT(name, member) \
1141 + DEFINE_SHOW_INT_FUNC(name, member) \
1142 + DEFINE_STORE_INT_FUNC(name, member) \
1143 + static DEVICE_ATTR(name, S_IRUGO | S_IWUSR, \
1144 + n810bm_attr_##name##_show, \
1145 + n810bm_attr_##name##_store);
1147 +DEFINE_ATTR_SHOW_INT(battery_present, battery_present);
1148 +DEFINE_ATTR_SHOW_INT(charger_present, charger_present);
1149 +static DEFINE_ATTR_NOTIFY(charger_present);
1150 +DEFINE_ATTR_SHOW_INT(charger_state, charger.state);
1151 +static DEFINE_ATTR_NOTIFY(charger_state);
1152 +DEFINE_ATTR_SHOW_INT(charger_pwm, active_current_pwm);
1153 +static DEFINE_ATTR_NOTIFY(charger_pwm);
1154 +DEFINE_ATTR_SHOW_STORE_INT(charger_enable, charger_enabled);
1155 +DEFINE_ATTR_SHOW_STORE_INT(charger_verbose, verbose_charge_log);
1157 +static ssize_t n810bm_attr_battery_level_show(struct device *dev,
1158 + struct device_attribute *attr,
1161 + struct n810bm *bm = device_to_n810bm(dev);
1162 + ssize_t count = -ENODEV;
1165 + mutex_lock(&bm->mutex);
1166 + if (!bm->battery_present || lipocharge_is_charging(&bm->charger))
1169 + millivolt = n810bm_measure_batt_voltage(bm);
1170 + if (millivolt >= 0) {
1171 + count = snprintf(buf, PAGE_SIZE, "%u\n",
1172 + n810bm_mvolt2percent(millivolt));
1174 + mutex_unlock(&bm->mutex);
1178 +static DEVICE_ATTR(battery_level, S_IRUGO,
1179 + n810bm_attr_battery_level_show, NULL);
1181 +static ssize_t n810bm_attr_battery_capacity_show(struct device *dev,
1182 + struct device_attribute *attr,
1185 + struct n810bm *bm = device_to_n810bm(dev);
1189 + mutex_lock(&bm->mutex);
1190 + if (n810bm_known_battery_present(bm))
1191 + capacity = (int)bm->capacity;
1192 + count = snprintf(buf, PAGE_SIZE, "%d\n", capacity);
1193 + mutex_unlock(&bm->mutex);
1197 +static DEVICE_ATTR(battery_capacity, S_IRUGO,
1198 + n810bm_attr_battery_capacity_show, NULL);
1200 +static ssize_t n810bm_attr_battery_temp_show(struct device *dev,
1201 + struct device_attribute *attr,
1204 + struct n810bm *bm = device_to_n810bm(dev);
1205 + ssize_t count = -ENODEV;
1208 + mutex_lock(&bm->mutex);
1209 + k = n810bm_measure_batt_temp(bm);
1211 + count = snprintf(buf, PAGE_SIZE, "%d\n", k);
1212 + mutex_unlock(&bm->mutex);
1216 +static DEVICE_ATTR(battery_temp, S_IRUGO,
1217 + n810bm_attr_battery_temp_show, NULL);
1219 +static ssize_t n810bm_attr_charger_voltage_show(struct device *dev,
1220 + struct device_attribute *attr,
1223 + struct n810bm *bm = device_to_n810bm(dev);
1224 + ssize_t count = -ENODEV;
1227 + mutex_lock(&bm->mutex);
1228 + if (bm->charger_present)
1229 + mv = n810bm_measure_charger_voltage(bm);
1231 + count = snprintf(buf, PAGE_SIZE, "%d\n", mv);
1232 + mutex_unlock(&bm->mutex);
1236 +static DEVICE_ATTR(charger_voltage, S_IRUGO,
1237 + n810bm_attr_charger_voltage_show, NULL);
1239 +static ssize_t n810bm_attr_backup_battery_voltage_show(struct device *dev,
1240 + struct device_attribute *attr,
1243 + struct n810bm *bm = device_to_n810bm(dev);
1244 + ssize_t count = -ENODEV;
1247 + mutex_lock(&bm->mutex);
1248 + mv = n810bm_measure_backup_batt_voltage(bm);
1250 + count = snprintf(buf, PAGE_SIZE, "%d\n", mv);
1251 + mutex_unlock(&bm->mutex);
1255 +static DEVICE_ATTR(backup_battery_voltage, S_IRUGO,
1256 + n810bm_attr_backup_battery_voltage_show, NULL);
1258 +static ssize_t n810bm_attr_battery_current_show(struct device *dev,
1259 + struct device_attribute *attr,
1262 + struct n810bm *bm = device_to_n810bm(dev);
1263 + ssize_t count = -ENODEV;
1266 + mutex_lock(&bm->mutex);
1267 + if (bm->battery_present)
1268 + ma = n810bm_measure_batt_current_async(bm);
1269 + count = snprintf(buf, PAGE_SIZE, "%d\n", ma);
1270 + mutex_unlock(&bm->mutex);
1274 +static DEVICE_ATTR(battery_current, S_IRUGO,
1275 + n810bm_attr_battery_current_show, NULL);
1277 +static const struct device_attribute *n810bm_attrs[] = {
1278 + &dev_attr_battery_present,
1279 + &dev_attr_battery_level,
1280 + &dev_attr_battery_current,
1281 + &dev_attr_battery_capacity,
1282 + &dev_attr_battery_temp,
1283 + &dev_attr_backup_battery_voltage,
1284 + &dev_attr_charger_present,
1285 + &dev_attr_charger_state,
1286 + &dev_attr_charger_verbose,
1287 + &dev_attr_charger_voltage,
1288 + &dev_attr_charger_enable,
1289 + &dev_attr_charger_pwm,
1292 +static void n810bm_notify_work(struct work_struct *work)
1294 + struct n810bm *bm = container_of(work, struct n810bm, notify_work);
1295 + unsigned long notify_flags;
1297 + notify_flags = xchg(&bm->notify_flags, 0);
1300 +#define do_notify(attr_name) \
1302 + if (notify_flags & (1 << N810BM_NOTIFY_##attr_name)) { \
1303 + sysfs_notify(&bm->pdev->dev.kobj, NULL, \
1304 + dev_attr_##attr_name.attr.name); \
1308 + do_notify(charger_present);
1309 + do_notify(charger_state);
1310 + do_notify(charger_pwm);
1313 +static int n810bm_charger_set_current_pwm(struct lipocharge *c,
1314 + unsigned int duty_cycle)
1316 + struct n810bm *bm = container_of(c, struct n810bm, charger);
1317 + int err = -EINVAL;
1319 + WARN_ON(!mutex_is_locked(&bm->mutex));
1320 + if (WARN_ON(duty_cycle > 0xFF))
1322 + if (WARN_ON(!bm->charger_enabled))
1324 + if (WARN_ON(!bm->battery_present || !bm->charger_present))
1327 + if (duty_cycle != bm->active_current_pwm) {
1328 + bm->active_current_pwm = duty_cycle;
1329 + tahvo_write(bm, TAHVO_REG_CHGCURR, duty_cycle);
1330 + n810bm_notify_charger_pwm(bm);
1339 +static void n810bm_charger_emergency(struct lipocharge *c)
1341 + struct n810bm *bm = container_of(c, struct n810bm, charger);
1343 + n810bm_emergency(bm, "Battery charger fault");
1346 +static void n810bm_hw_exit(struct n810bm *bm)
1348 + n810bm_stop_charge(bm);
1349 + retu_write(bm, RETU_REG_ADCSCR, 0);
1352 +static int n810bm_hw_init(struct n810bm *bm)
1356 + err = n810bm_check_adc_sanity(bm);
1360 + n810bm_stop_charge(bm);
1365 +static void n810bm_cancel_and_flush_work(struct n810bm *bm)
1367 + cancel_delayed_work_sync(&bm->periodic_check_work);
1368 + cancel_work_sync(&bm->notify_work);
1369 + flush_scheduled_work();
1372 +static int n810bm_device_init(struct n810bm *bm)
1377 + bm->charger.rate = LIPORATE_p6C;
1378 + bm->charger.top_voltage = 4100;
1379 + bm->charger.duty_cycle_max = 0xFF;
1380 + bm->charger.set_current_pwm = n810bm_charger_set_current_pwm;
1381 + bm->charger.emergency = n810bm_charger_emergency;
1382 + lipocharge_init(&bm->charger, &bm->pdev->dev);
1384 + err = n810bm_hw_init(bm);
1387 + for (attr_index = 0; attr_index < ARRAY_SIZE(n810bm_attrs); attr_index++) {
1388 + err = device_create_file(&bm->pdev->dev, n810bm_attrs[attr_index]);
1390 + goto err_unwind_attrs;
1393 + err = retu_request_irq(RETU_INT_ADCS,
1394 + n810bm_adc_irq_handler,
1395 + (unsigned long)bm, "n810bm");
1397 + goto err_unwind_attrs;
1399 + bm->tahvo_irq = platform_get_irq(n810bm_tahvo_device, 0);
1400 + err = request_threaded_irq(bm->tahvo_irq, NULL,
1401 + n810bm_tahvo_current_measure_irq_handler,
1402 + IRQF_ONESHOT, "tahvo-n810bm", bm);
1404 + goto err_free_retu_irq;
1405 + disable_irq_nosync(bm->tahvo_irq);
1406 + bm->tahvo_irq_enabled = 0;
1408 + schedule_delayed_work(&bm->periodic_check_work,
1409 + round_jiffies_relative(N810BM_CHECK_INTERVAL));
1411 + bm->initialized = 1;
1412 + dev_info(&bm->pdev->dev, "Battery management initialized");
1417 +//XXX retu_free_irq(RETU_INT_ADCS);
1419 + for (attr_index--; attr_index >= 0; attr_index--)
1420 + device_remove_file(&bm->pdev->dev, n810bm_attrs[attr_index]);
1422 + n810bm_hw_exit(bm);
1424 + n810bm_cancel_and_flush_work(bm);
1429 +static void n810bm_device_exit(struct n810bm *bm)
1433 + if (!bm->initialized)
1436 + lipocharge_exit(&bm->charger);
1437 + free_irq(bm->tahvo_irq, bm);
1438 +//XXX retu_free_irq(RETU_INT_ADCS);
1439 + for (i = 0; i < ARRAY_SIZE(n810bm_attrs); i++)
1440 + device_remove_file(&bm->pdev->dev, n810bm_attrs[i]);
1442 + n810bm_cancel_and_flush_work(bm);
1444 + n810bm_hw_exit(bm);
1446 + bm->initialized = 0;
1449 +static void n810bm_pmm_block_found(const struct firmware *fw, void *context)
1451 + struct n810bm *bm = context;
1455 + dev_err(&bm->pdev->dev,
1456 + "CAL PMM block image file not found");
1459 + if (fw->size != N810BM_PMM_BLOCK_SIZE ||
1460 + memcmp(fw->data, "BME-PMM-BLOCK01", 15) != 0) {
1461 + dev_err(&bm->pdev->dev,
1462 + "CAL PMM block image file has an invalid format");
1466 + err = n810bm_parse_pmm_block(bm, fw);
1469 + release_firmware(fw);
1471 + err = n810bm_device_init(bm);
1473 + dev_err(&bm->pdev->dev,
1474 + "Failed to initialized battery management (%d)", err);
1480 + release_firmware(fw);
1485 +static int __devinit n810bm_probe(void)
1487 + struct n810bm *bm;
1490 + if (!n810bm_retu_device || !n810bm_tahvo_device)
1493 + bm = kzalloc(sizeof(*bm), GFP_KERNEL);
1496 + bm->pdev = n810bm_retu_device;
1497 + platform_set_drvdata(n810bm_retu_device, bm);
1498 + platform_set_drvdata(n810bm_tahvo_device, bm);
1499 + mutex_init(&bm->mutex);
1500 + INIT_DELAYED_WORK(&bm->periodic_check_work, n810bm_periodic_check_work);
1501 + INIT_WORK(&bm->notify_work, n810bm_notify_work);
1503 + dev_info(&bm->pdev->dev, "Requesting CAL BME PMM block firmware file "
1504 + N810BM_PMM_BLOCK_FILENAME);
1505 + err = request_firmware_nowait(THIS_MODULE, 1,
1506 + N810BM_PMM_BLOCK_FILENAME,
1507 + &bm->pdev->dev, GFP_KERNEL,
1508 + bm, n810bm_pmm_block_found);
1510 + dev_err(&bm->pdev->dev,
1511 + "Failed to request CAL PMM block image file (%d)", err);
1523 +static void __devexit n810bm_remove(void)
1525 + struct n810bm *bm;
1527 + if (!n810bm_retu_device || !n810bm_tahvo_device)
1529 + bm = platform_get_drvdata(n810bm_retu_device);
1531 + n810bm_device_exit(bm);
1534 + platform_set_drvdata(n810bm_retu_device, NULL);
1535 + platform_set_drvdata(n810bm_tahvo_device, NULL);
1538 +static int __devinit n810bm_retu_probe(struct platform_device *pdev)
1540 + n810bm_retu_device = pdev;
1541 + return n810bm_probe();
1544 +static int __devexit n810bm_retu_remove(struct platform_device *pdev)
1547 + n810bm_retu_device = NULL;
1551 +static int __devinit n810bm_tahvo_probe(struct platform_device *pdev)
1553 + n810bm_tahvo_device = pdev;
1554 + return n810bm_probe();
1557 +static int __devexit n810bm_tahvo_remove(struct platform_device *pdev)
1560 + n810bm_tahvo_device = NULL;
1564 +static struct platform_driver n810bm_retu_driver = {
1565 + .remove = __devexit_p(n810bm_retu_remove),
1567 + .name = "retu-n810bm",
1571 +static struct platform_driver n810bm_tahvo_driver = {
1572 + .remove = __devexit_p(n810bm_tahvo_remove),
1574 + .name = "tahvo-n810bm",
1578 +static int __init n810bm_modinit(void)
1582 + err = platform_driver_probe(&n810bm_retu_driver, n810bm_retu_probe);
1585 + err = platform_driver_probe(&n810bm_tahvo_driver, n810bm_tahvo_probe);
1587 + platform_driver_unregister(&n810bm_retu_driver);
1593 +module_init(n810bm_modinit);
1595 +static void __exit n810bm_modexit(void)
1597 + platform_driver_unregister(&n810bm_tahvo_driver);
1598 + platform_driver_unregister(&n810bm_retu_driver);
1600 +module_exit(n810bm_modexit);
1602 +MODULE_DESCRIPTION("Nokia n810 battery management");
1603 +MODULE_FIRMWARE(N810BM_PMM_BLOCK_FILENAME);
1604 +MODULE_LICENSE("GPL");
1605 +MODULE_AUTHOR("Michael Buesch");
1606 Index: linux-3.1/drivers/cbus/lipocharge.c
1607 ===================================================================
1608 --- /dev/null 1970-01-01 00:00:00.000000000 +0000
1609 +++ linux-3.1/drivers/cbus/lipocharge.c 2011-11-05 17:06:51.589348749 +0100
1612 + * Generic LIPO battery charger
1614 + * Copyright (c) 2010-2011 Michael Buesch <mb@bu3sch.de>
1616 + * This program is free software; you can redistribute it and/or
1617 + * modify it under the terms of the GNU General Public License
1618 + * as published by the Free Software Foundation; either version 2
1619 + * of the License, or (at your option) any later version.
1621 + * This program is distributed in the hope that it will be useful,
1622 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
1623 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
1624 + * GNU General Public License for more details.
1629 +#include "lipocharge.h"
1631 +#include <linux/slab.h>
1634 +/* Hysteresis constants */
1635 +#define CURRENT_HYST 30 /* mA */
1636 +#define VOLTAGE_HYST 10 /* mV */
1638 +/* Threshold constants */
1639 +#define FINISH_CURRENT_PERCENT 3
1642 +/* Returns the requested first-stage charge current in mA */
1643 +static inline unsigned int get_stage1_charge_current(struct lipocharge *c)
1645 + /* current = (capacity * C) */
1646 + return c->capacity * c->rate / 1000;
1649 +void lipocharge_init(struct lipocharge *c, struct device *dev)
1652 + c->state = LIPO_IDLE;
1655 +void lipocharge_exit(struct lipocharge *c)
1657 + c->state = LIPO_IDLE;
1660 +int lipocharge_start(struct lipocharge *c)
1664 + if (c->state != LIPO_IDLE)
1666 + if (!c->set_current_pwm || !c->emergency)
1668 + if (!c->top_voltage || c->top_voltage > 4200)
1671 + c->active_duty_cycle = 0;
1672 + err = c->set_current_pwm(c, c->active_duty_cycle);
1675 + c->state = LIPO_FIRST_STAGE;
1680 +void lipocharge_stop(struct lipocharge *c)
1682 + if (c->state == LIPO_IDLE)
1684 + c->state = LIPO_IDLE;
1687 +static int lipocharge_increase_current(struct lipocharge *c,
1688 + unsigned int inc_permille)
1690 + int old_pwm, new_pwm;
1692 + if (c->active_duty_cycle >= c->duty_cycle_max)
1695 + old_pwm = c->active_duty_cycle;
1696 + new_pwm = old_pwm + (c->duty_cycle_max * inc_permille / 1000);
1697 + new_pwm = min(new_pwm, (int)c->duty_cycle_max);
1698 + c->active_duty_cycle = new_pwm;
1700 + dev_dbg(c->dev, "lipo: Increasing duty_cycle by "
1701 + "%u permille (0x%02X -> 0x%02X)",
1702 + inc_permille, old_pwm, new_pwm);
1704 + return c->set_current_pwm(c, c->active_duty_cycle);
1707 +static int lipocharge_decrease_current(struct lipocharge *c,
1708 + unsigned int dec_permille)
1710 + int old_pwm, new_pwm;
1712 + if (c->active_duty_cycle <= 0)
1715 + old_pwm = c->active_duty_cycle;
1716 + new_pwm = old_pwm - (c->duty_cycle_max * dec_permille / 1000);
1717 + new_pwm = max(0, new_pwm);
1718 + c->active_duty_cycle = new_pwm;
1720 + dev_dbg(c->dev, "lipo: Decreasing duty_cycle by "
1721 + "%u permille (0x%02X -> 0x%02X)",
1722 + dec_permille, old_pwm, new_pwm);
1724 + return c->set_current_pwm(c, c->active_duty_cycle);
1727 +/** lipocharge_update_state - Update the charge state
1728 + * @c: The context.
1729 + * @voltage_mV: The measured battery voltage.
1730 + * @current_mA: The measured charge current.
1731 + * negative -> drain.
1732 + * positive -> charge.
1733 + * @temp_K: Battery temperature in K.
1735 + * Returns 0 on success, -1 on error.
1736 + * Returns 1, if the charging process is finished.
1738 +int lipocharge_update_state(struct lipocharge *c,
1739 + unsigned int voltage_mV,
1741 + unsigned int temp_K)
1743 + int requested_current, current_diff;
1745 + unsigned int permille;
1750 + switch (c->state) {
1752 + dev_err(c->dev, "%s: called while idle", __func__);
1754 + case LIPO_FIRST_STAGE: /* Constant current */
1755 +//printk("GOT %u %d %u\n", voltage_mV, current_mA, temp_K);
1756 + if (voltage_mV >= c->top_voltage) {
1757 + /* Float voltage reached.
1758 + * Switch charger mode to "constant current" */
1759 + c->state = LIPO_SECOND_STAGE;
1760 + dev_dbg(c->dev, "Switched to second charging stage.");
1763 + /* Float voltage not reached, yet.
1764 + * Try to get the requested constant current. */
1765 + requested_current = get_stage1_charge_current(c);
1766 + if (current_mA < 0)
1768 + current_diff = requested_current - current_mA;
1769 + if (abs(requested_current - current_mA) > CURRENT_HYST) {
1770 + if (current_diff > 0) {
1771 + /* Increase current */
1772 + permille = current_diff * 1000 / requested_current;
1774 + err = lipocharge_increase_current(c, permille);
1778 + /* Decrease current */
1779 + permille = (-current_diff) * 1000 / requested_current;
1781 + err = lipocharge_decrease_current(c, permille);
1787 + case LIPO_SECOND_STAGE: /* Constant voltage */
1794 Index: linux-3.1/drivers/cbus/lipocharge.h
1795 ===================================================================
1796 --- /dev/null 1970-01-01 00:00:00.000000000 +0000
1797 +++ linux-3.1/drivers/cbus/lipocharge.h 2011-11-05 17:06:51.589348749 +0100
1799 +#ifndef LIPOCHARGE_H_
1800 +#define LIPOCHARGE_H_
1802 +#include <linux/types.h>
1803 +#include <linux/device.h>
1806 +#define LIPORATE(a,b) (((a) * 1000) + ((b) * 100))
1807 +#define LIPORATE_p6C LIPORATE(0,6) /* 0.6C */
1809 +enum lipocharge_state {
1810 + LIPO_IDLE = 0, /* Not charging */
1811 + LIPO_FIRST_STAGE, /* Charging: constant current */
1812 + LIPO_SECOND_STAGE, /* Charging: constant voltage */
1815 +/** struct lipocharge - A generic LIPO charger
1817 + * @capacity: Battery capacity in mAh.
1818 + * @rate: Charge rate.
1819 + * @top_voltage: Fully charged voltage, in mV.
1820 + * @duty_cycle_max: Max value for duty_cycle.
1822 + * @set_charge_current: Set the charge current PWM duty cycle.
1823 + * @emergency: Something went wrong. Force shutdown.
1825 +struct lipocharge {
1826 + unsigned int capacity;
1827 + unsigned int rate;
1828 + unsigned int top_voltage;
1829 + unsigned int duty_cycle_max;
1831 + int (*set_current_pwm)(struct lipocharge *c, unsigned int duty_cycle);
1832 + void (*emergency)(struct lipocharge *c);
1835 + struct device *dev;
1836 + enum lipocharge_state state;
1837 + unsigned int active_duty_cycle;
1839 + //TODO implement timer to cut power after maximum charge time.
1842 +void lipocharge_init(struct lipocharge *c, struct device *dev);
1843 +void lipocharge_exit(struct lipocharge *c);
1845 +int lipocharge_start(struct lipocharge *c);
1846 +void lipocharge_stop(struct lipocharge *c);
1848 +int lipocharge_update_state(struct lipocharge *c,
1849 + unsigned int voltage_mV,
1851 + unsigned int temp_K);
1853 +static inline bool lipocharge_is_charging(struct lipocharge *c)
1855 + return (c->state != LIPO_IDLE);
1858 +#endif /* LIPOCHARGE_H_ */
1859 Index: linux-3.1/drivers/cbus/cbus.c
1860 ===================================================================
1861 --- linux-3.1.orig/drivers/cbus/cbus.c 2011-11-05 17:03:39.610846102 +0100
1862 +++ linux-3.1/drivers/cbus/cbus.c 2011-11-05 17:06:51.589348749 +0100
1864 #include <linux/gpio.h>
1865 #include <linux/platform_device.h>
1866 #include <linux/platform_data/cbus.h>
1867 +#include <linux/reboot.h>
1871 @@ -324,6 +325,13 @@ static void __exit cbus_bus_exit(void)
1873 module_exit(cbus_bus_exit);
1875 +void cbus_emergency(void)
1877 + machine_power_off();
1878 + panic("cbus: Failed to halt machine in emergency state\n");
1880 +EXPORT_SYMBOL(cbus_emergency);
1882 MODULE_DESCRIPTION("CBUS serial protocol");
1883 MODULE_LICENSE("GPL");
1884 MODULE_AUTHOR("Juha Yrjölä");
1885 Index: linux-3.1/drivers/cbus/cbus.h
1886 ===================================================================
1887 --- linux-3.1.orig/drivers/cbus/cbus.h 2011-11-05 17:03:39.590846131 +0100
1888 +++ linux-3.1/drivers/cbus/cbus.h 2011-11-05 17:06:51.589348749 +0100
1889 @@ -27,4 +27,6 @@ extern int cbus_read_reg(struct device *
1890 extern int cbus_write_reg(struct device *, unsigned dev, unsigned reg,
1893 +NORET_TYPE void cbus_emergency(void) ATTRIB_NORET;
1895 #endif /* __DRIVERS_CBUS_CBUS_H */
1896 Index: linux-3.1/drivers/cbus/retu.c
1897 ===================================================================
1898 --- linux-3.1.orig/drivers/cbus/retu.c 2011-11-05 17:03:39.606846106 +0100
1899 +++ linux-3.1/drivers/cbus/retu.c 2011-11-05 17:06:51.589348749 +0100
1900 @@ -417,6 +417,11 @@ static int retu_allocate_children(struct
1904 + child = retu_allocate_child("retu-n810bm", parent, irq_base,
1905 + RETU_INT_ADCS, -1, 1);
1912 Index: linux-3.1/drivers/cbus/tahvo.c
1913 ===================================================================
1914 --- linux-3.1.orig/drivers/cbus/tahvo.c 2011-11-05 17:04:36.274768324 +0100
1915 +++ linux-3.1/drivers/cbus/tahvo.c 2011-11-05 17:06:51.589348749 +0100
1916 @@ -131,6 +131,7 @@ void tahvo_set_clear_reg_bits(struct dev
1917 __tahvo_write_reg(tahvo, reg, w);
1918 mutex_unlock(&tahvo->mutex);
1920 +EXPORT_SYMBOL(tahvo_set_clear_reg_bits);
1922 static irqreturn_t tahvo_irq_handler(int irq, void *_tahvo)
1924 @@ -307,6 +308,11 @@ static int tahvo_allocate_children(struc
1928 + child = tahvo_allocate_child("tahvo-n810bm", parent,
1929 + irq_base + TAHVO_INT_BATCURR);