1 --- a/drivers/cbus/Kconfig
2 +++ b/drivers/cbus/Kconfig
3 @@ -83,4 +83,12 @@ config CBUS_RETU_HEADSET
8 + depends on CBUS_RETU && CBUS_TAHVO
9 + tristate "Nokia n810 battery management"
11 + Nokia n810 device battery management.
16 --- a/drivers/cbus/Makefile
17 +++ b/drivers/cbus/Makefile
18 @@ -11,3 +11,6 @@ obj-$(CONFIG_CBUS_RETU_POWERBUTTON) += r
19 obj-$(CONFIG_CBUS_RETU_RTC) += retu-rtc.o
20 obj-$(CONFIG_CBUS_RETU_WDT) += retu-wdt.o
21 obj-$(CONFIG_CBUS_RETU_HEADSET) += retu-headset.o
22 +n810bm-y += n810bm_main.o
23 +n810bm-y += lipocharge.o
24 +obj-$(CONFIG_N810BM) += n810bm.o
26 +++ b/drivers/cbus/n810bm_main.c
29 + * Nokia n810 battery management
31 + * WARNING: This driver is based on unconfirmed documentation.
32 + * It is possibly dangerous to use this software.
33 + * Use this software at your own risk!
35 + * Copyright (c) 2010-2011 Michael Buesch <mb@bu3sch.de>
37 + * This program is free software; you can redistribute it and/or
38 + * modify it under the terms of the GNU General Public License
39 + * as published by the Free Software Foundation; either version 2
40 + * of the License, or (at your option) any later version.
42 + * This program is distributed in the hope that it will be useful,
43 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
44 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
45 + * GNU General Public License for more details.
50 +#include <linux/module.h>
51 +#include <linux/device.h>
52 +#include <linux/platform_device.h>
53 +#include <linux/slab.h>
54 +#include <linux/mutex.h>
55 +#include <linux/timer.h>
56 +#include <linux/firmware.h>
57 +#include <linux/bitops.h>
58 +#include <linux/workqueue.h>
59 +#include <linux/delay.h>
60 +#include <linux/interrupt.h>
65 +#include "lipocharge.h"
68 +#define N810BM_PMM_BLOCK_FILENAME "n810-cal-bme-pmm.fw"
69 +#define N810BM_PMM_BLOCK_SIZE 0x600
70 +#define N810BM_PMM_GROUP_SIZE 0x200
71 +#define N810BM_PMM_ELEM_SIZE 0x10
73 +#define N810BM_CHECK_INTERVAL (HZ * 2)
74 +#define N810BM_MIN_VOLTAGE_THRES 3200 /* Absolute minimum voltage threshold */
78 + * The battery size indicator ADC measures the resistance between
79 + * the battery BSI pin and ground. This is used to detect the battery
80 + * capacity, as the BSI resistor is related to capacity.
82 + * Manually measured lookup table.
83 + * Hard to measure, thus not very accurate.
85 + * Resistance | ADC value
86 + * ========================
94 + * Manually measured lookup table.
95 + * Hard to measure, thus not very accurate.
97 + * Voltage | ADC value
98 + * =====================
117 +/* PMM block ADC IDs */
118 +enum n810bm_pmm_adc_id {
119 + N810BM_PMM_ADC_BATVOLT = 0x01, /* Battery voltage */
120 + N810BM_PMM_ADC_CHGVOLT = 0x02, /* Charger voltage */
121 + N810BM_PMM_ADC_GND2 = 0x03, /* Ground 0V */
122 + N810BM_PMM_ADC_BSI = 0x04, /* Battery size indicator */
123 + N810BM_PMM_ADC_BATTEMP = 0x05, /* Battery temperature */
124 + N810BM_PMM_ADC_HEADSET = 0x06, /* Headset detection */
125 + N810BM_PMM_ADC_HOOKDET = 0x07, /* Hook detection */
126 + N810BM_PMM_ADC_LIGHTSENS = 0x08, /* Light sensor */
127 + N810BM_PMM_ADC_BATCURR = 0x0E, /* Battery current */
128 + N810BM_PMM_ADC_BKUPVOLT = 0x13, /* Backup battery voltage */
129 + N810BM_PMM_ADC_LIGHTTEMP = 0x14, /* Light sensor temperature */
130 + N810BM_PMM_ADC_RFGP = 0x15, /* RF GP */
131 + N810BM_PMM_ADC_WBTX = 0x16, /* Wideband TX detection */
132 + N810BM_PMM_ADC_RETUTEMP = 0x17, /* RETU chip temperature */
133 + N810BM_PMM_ADC_0xFE = 0xFE,
136 +struct n810bm_adc_calib {
137 + enum n810bm_pmm_adc_id id;
146 +struct n810bm_calib {
147 + struct n810bm_adc_calib adc[25];
150 +enum n810bm_capacity {
151 + N810BM_CAP_UNKNOWN = -1,
152 + N810BM_CAP_NONE = 0,
153 + N810BM_CAP_1500MAH = 1500, /* 1500 mAh battery */
156 +enum n810bm_notify_flags {
157 + N810BM_NOTIFY_charger_present,
158 + N810BM_NOTIFY_charger_state,
159 + N810BM_NOTIFY_charger_pwm,
164 + bool tahvo_irq_enabled;
166 + bool battery_present; /* A battery is inserted */
167 + bool charger_present; /* The charger is connected */
168 + enum n810bm_capacity capacity; /* The capacity of the inserted battery (if any) */
170 + bool charger_enabled; /* Want to charge? */
171 + struct lipocharge charger; /* Charger subsystem */
172 + unsigned int active_current_pwm; /* Active value of TAHVO_REG_CHGCURR */
173 + int current_measure_enabled; /* Current measure enable refcount */
175 + struct platform_device *pdev;
176 + struct n810bm_calib calib; /* Calibration data */
178 + bool verbose_charge_log; /* Verbose charge logging */
180 + unsigned long notify_flags;
181 + struct work_struct notify_work;
182 + struct delayed_work periodic_check_work;
184 + bool initialized; /* The hardware was initialized */
185 + struct mutex mutex;
188 +static void n810bm_notify_charger_present(struct n810bm *bm);
189 +static void n810bm_notify_charger_state(struct n810bm *bm);
190 +static void n810bm_notify_charger_pwm(struct n810bm *bm);
193 +static struct platform_device *n810bm_retu_device;
194 +static struct platform_device *n810bm_tahvo_device;
197 +static inline struct n810bm * device_to_n810bm(struct device *dev)
199 + struct platform_device *pdev = to_platform_device(dev);
200 + struct n810bm *bm = platform_get_drvdata(pdev);
205 +static inline bool n810bm_known_battery_present(struct n810bm *bm)
207 + return bm->battery_present &&
208 + bm->capacity != N810BM_CAP_UNKNOWN &&
209 + bm->capacity != N810BM_CAP_NONE;
212 +static NORET_TYPE void n810bm_emergency(struct n810bm *bm, const char *message) ATTRIB_NORET;
213 +static void n810bm_emergency(struct n810bm *bm, const char *message)
215 + printk(KERN_EMERG "n810 battery management fatal fault: %s\n", message);
219 +static u16 tahvo_read(struct n810bm *bm, unsigned int reg)
221 + return tahvo_read_reg(&n810bm_tahvo_device->dev, reg);
224 +static void tahvo_maskset(struct n810bm *bm, unsigned int reg, u16 mask, u16 set)
226 + tahvo_set_clear_reg_bits(&n810bm_tahvo_device->dev, reg, set, mask);
229 +static inline void tahvo_write(struct n810bm *bm, unsigned int reg, u16 value)
231 + tahvo_write_reg(&n810bm_tahvo_device->dev, reg, value);
234 +static inline void tahvo_set(struct n810bm *bm, unsigned int reg, u16 mask)
236 + tahvo_set_clear_reg_bits(&n810bm_tahvo_device->dev, reg, mask, mask);
239 +static inline void tahvo_clear(struct n810bm *bm, unsigned int reg, u16 mask)
241 + tahvo_set_clear_reg_bits(&n810bm_tahvo_device->dev, reg, 0, mask);
244 +static u16 retu_read(struct n810bm *bm, unsigned int reg)
246 + return retu_read_reg(&n810bm_retu_device->dev, reg);
249 +static void retu_maskset(struct n810bm *bm, unsigned int reg, u16 mask, u16 set)
251 + retu_set_clear_reg_bits(&n810bm_retu_device->dev, reg, set, mask);
254 +static inline void retu_write(struct n810bm *bm, unsigned int reg, u16 value)
256 + retu_write_reg(&n810bm_retu_device->dev, reg, value);
259 +static int retu_adc_average(struct n810bm *bm, unsigned int chan,
260 + unsigned int nr_passes)
262 + unsigned int i, value = 0;
265 + if (WARN_ON(!nr_passes))
267 + for (i = 0; i < nr_passes; i++) {
268 + ret = retu_read_adc(&n810bm_retu_device->dev, chan);
273 + value /= nr_passes;
278 +static struct n810bm_adc_calib * n810bm_get_adc_calib(struct n810bm *bm,
279 + enum n810bm_pmm_adc_id id)
281 + unsigned int index = 0;
282 + struct n810bm_adc_calib *cal;
284 + if (id != N810BM_PMM_ADC_0xFE)
285 + index = (unsigned int)id + 1;
286 + if (index >= ARRAY_SIZE(bm->calib.adc))
289 + cal = &bm->calib.adc[index];
290 + WARN_ON(cal->id && cal->id != id);
295 +static int pmm_record_get(struct n810bm *bm,
296 + const struct firmware *pmm_block,
297 + void *buffer, size_t length,
298 + unsigned int group, unsigned int element, unsigned int offset)
300 + const u8 *pmm_area = pmm_block->data;
301 + u8 active_group_mask;
303 + if (pmm_block->size != N810BM_PMM_BLOCK_SIZE)
305 + if (group >= N810BM_PMM_BLOCK_SIZE / N810BM_PMM_GROUP_SIZE)
307 + if (element >= N810BM_PMM_GROUP_SIZE / N810BM_PMM_ELEM_SIZE)
309 + if (offset >= N810BM_PMM_ELEM_SIZE || length > N810BM_PMM_ELEM_SIZE ||
310 + length + offset > N810BM_PMM_ELEM_SIZE)
313 + active_group_mask = pmm_area[16];
314 + if (!(active_group_mask & (1 << group))) {
315 + dev_dbg(&bm->pdev->dev, "pwm_record_get: Requested group %u, "
316 + "but group is not active", group);
321 + pmm_area + group * N810BM_PMM_GROUP_SIZE
322 + + element * N810BM_PMM_ELEM_SIZE
329 +/* PMM block group 1 element */
330 +struct group1_element {
339 +static int extract_group1_elem(struct n810bm *bm,
340 + const struct firmware *pmm_block,
341 + const enum n810bm_pmm_adc_id *pmm_adc_ids, size_t nr_pmm_adc_ids,
342 + u32 field1_mask, u32 field2_mask)
344 + struct group1_element elem;
346 + unsigned int i, element_nr;
347 + struct n810bm_adc_calib *adc_calib;
349 + for (i = 0; i < nr_pmm_adc_ids; i++) {
350 + element_nr = (unsigned int)(pmm_adc_ids[i]) + 3;
352 + err = pmm_record_get(bm, pmm_block, &elem, sizeof(elem),
356 + adc_calib = n810bm_get_adc_calib(bm, elem.id);
358 + dev_err(&bm->pdev->dev, "extract_group1_elem: "
359 + "Could not get calib element for 0x%02X",
364 + if (adc_calib->flags == elem.flags) {
365 + adc_calib->field1 = le32_to_cpu(elem.field1) & field1_mask;
366 + adc_calib->field2 = le32_to_cpu(elem.field2) & field2_mask;
368 + dev_dbg(&bm->pdev->dev, "extract_group1_elem: "
369 + "Not extracting fields due to flags mismatch: "
370 + "0x%02X vs 0x%02X",
371 + adc_calib->flags, elem.flags);
378 +static int n810bm_parse_pmm_group1(struct n810bm *bm,
379 + const struct firmware *pmm_block)
381 + struct n810bm_adc_calib *adc_calib;
382 + struct group1_element elem;
385 + static const enum n810bm_pmm_adc_id pmm_adc_ids_1[] = {
386 + N810BM_PMM_ADC_BATVOLT,
387 + N810BM_PMM_ADC_CHGVOLT,
388 + N810BM_PMM_ADC_BKUPVOLT,
389 + N810BM_PMM_ADC_BATCURR,
391 + static const enum n810bm_pmm_adc_id pmm_adc_ids_2[] = {
392 + N810BM_PMM_ADC_BSI,
394 + static const enum n810bm_pmm_adc_id pmm_adc_ids_3[] = {
395 + N810BM_PMM_ADC_BATTEMP,
398 + /* Parse element 2 */
399 + err = pmm_record_get(bm, pmm_block, &elem, sizeof(elem),
402 + dev_err(&bm->pdev->dev,
403 + "PMM: Failed to get group 1 / element 2");
406 + if (elem.id == N810BM_PMM_ADC_0xFE && elem.flags == 0x05) {
407 + adc_calib = n810bm_get_adc_calib(bm, elem.id);
409 + dev_err(&bm->pdev->dev,
410 + "calib extract: Failed to get 0xFE calib");
413 + adc_calib->id = elem.id;
414 + adc_calib->flags = elem.flags;
415 + adc_calib->field1 = le32_to_cpu(elem.field1);
416 + adc_calib->field2 = le32_to_cpu(elem.field2);
419 + err = extract_group1_elem(bm, pmm_block,
420 + pmm_adc_ids_1, ARRAY_SIZE(pmm_adc_ids_1),
421 + 0xFFFFFFFF, 0xFFFFFFFF);
424 + err = extract_group1_elem(bm, pmm_block,
425 + pmm_adc_ids_2, ARRAY_SIZE(pmm_adc_ids_2),
429 + err = extract_group1_elem(bm, pmm_block,
430 + pmm_adc_ids_3, ARRAY_SIZE(pmm_adc_ids_3),
431 + 0xFFFFFFFF, 0x0000FFFF);
438 +static int n810bm_parse_pmm_group2(struct n810bm *bm,
439 + const struct firmware *pmm_block)
441 + dev_err(&bm->pdev->dev, "TODO: CAL BME PMM group 2 parser not implemented, yet");
442 + return -EOPNOTSUPP;
445 +static void n810bm_adc_calib_set_defaults(struct n810bm *bm)
447 + struct n810bm_adc_calib *adc_calib;
450 + static const struct n810bm_adc_calib defaults[] = {
451 + /* ADC group-nr 0 */
453 + .id = N810BM_PMM_ADC_HEADSET,
457 + .id = N810BM_PMM_ADC_HOOKDET,
461 + .id = N810BM_PMM_ADC_RFGP,
465 + .id = N810BM_PMM_ADC_LIGHTSENS,
469 + .id = N810BM_PMM_ADC_WBTX,
473 + .id = N810BM_PMM_ADC_RETUTEMP,
477 + .id = N810BM_PMM_ADC_GND2,
481 + /* ADC group-nr 1 */
483 + .id = N810BM_PMM_ADC_0xFE,
489 + .id = N810BM_PMM_ADC_BATVOLT,
495 + .id = N810BM_PMM_ADC_CHGVOLT,
501 + .id = N810BM_PMM_ADC_BKUPVOLT,
507 + .id = N810BM_PMM_ADC_BATCURR,
513 + /* ADC group-nr 2 */
515 + .id = N810BM_PMM_ADC_BSI,
521 + /* ADC group-nr 3 */
523 + .id = N810BM_PMM_ADC_BATTEMP,
526 + .field1 = 265423000,
529 + /* ADC group-nr 4 */
531 + .id = N810BM_PMM_ADC_LIGHTTEMP,
534 + .field1 = 19533778,
535 + .field2 = 308019670,
541 + /* Clear the array */
542 + memset(&bm->calib.adc, 0, sizeof(bm->calib.adc));
543 + for (i = 0; i < ARRAY_SIZE(bm->calib.adc); i++)
544 + bm->calib.adc[i].flags = 0xFF;
546 + /* Copy the defaults */
547 + for (i = 0; i < ARRAY_SIZE(defaults); i++) {
548 + adc_calib = n810bm_get_adc_calib(bm, defaults[i].id);
549 + if (WARN_ON(!adc_calib))
551 + *adc_calib = defaults[i];
555 +static int n810bm_parse_pmm_block(struct n810bm *bm,
556 + const struct firmware *pmm_block)
560 + unsigned int i, count;
561 + struct n810bm_adc_calib *adc_calib;
563 + /* Initialize to defaults */
564 + n810bm_adc_calib_set_defaults(bm);
566 + /* Parse the PMM data */
567 + err = pmm_record_get(bm, pmm_block, &byte, sizeof(byte),
568 + 1, 0, 0); /* group 1 / element 0 */
569 + err |= (byte != 0x01);
570 + err |= pmm_record_get(bm, pmm_block, &byte, sizeof(byte),
571 + 1, 1, 0); /* group 1 / element 1 */
572 + err |= (byte != 0x01);
574 + err = n810bm_parse_pmm_group2(bm, pmm_block);
576 + err = n810bm_parse_pmm_group1(bm, pmm_block);
580 + /* Sanity checks */
581 + for (i = 0, count = 0; i < ARRAY_SIZE(bm->calib.adc); i++) {
582 + adc_calib = &bm->calib.adc[i];
583 + if (adc_calib->flags == 0xFF)
585 + switch (adc_calib->id) {
586 + case N810BM_PMM_ADC_BATVOLT:
587 + if (adc_calib->field1 < 2400 ||
588 + adc_calib->field1 > 2700)
589 + goto value_check_fail;
590 + if (adc_calib->field2 < 20000 ||
591 + adc_calib->field2 > 23000)
592 + goto value_check_fail;
595 + case N810BM_PMM_ADC_BSI:
596 + if (adc_calib->field1 < 1100 ||
597 + adc_calib->field1 > 1300)
598 + goto value_check_fail;
601 + case N810BM_PMM_ADC_BATCURR:
602 + if (adc_calib->field2 < 7000 ||
603 + adc_calib->field2 > 12000)
604 + goto value_check_fail;
607 + case N810BM_PMM_ADC_0xFE:
608 + if ((s32)adc_calib->field1 > 14 ||
609 + (s32)adc_calib->field1 < -14)
610 + goto value_check_fail;
611 + if (adc_calib->field2 < 13000 ||
612 + adc_calib->field2 > 13350)
613 + goto value_check_fail;
616 + case N810BM_PMM_ADC_CHGVOLT:
617 + case N810BM_PMM_ADC_BATTEMP:
618 + case N810BM_PMM_ADC_BKUPVOLT:
621 + case N810BM_PMM_ADC_GND2:
622 + case N810BM_PMM_ADC_HOOKDET:
623 + case N810BM_PMM_ADC_LIGHTSENS:
624 + case N810BM_PMM_ADC_HEADSET:
625 + case N810BM_PMM_ADC_LIGHTTEMP:
626 + case N810BM_PMM_ADC_RFGP:
627 + case N810BM_PMM_ADC_WBTX:
628 + case N810BM_PMM_ADC_RETUTEMP:
631 + dev_dbg(&bm->pdev->dev,
632 + "ADC 0x%02X calib: 0x%02X 0x%02X 0x%08X 0x%08X 0x%04X 0x%04X",
633 + adc_calib->id, adc_calib->flags, adc_calib->adc_groupnr,
634 + adc_calib->field1, adc_calib->field2,
635 + adc_calib->field3, adc_calib->field4);
638 + dev_err(&bm->pdev->dev, "PMM sanity check: Did not find "
639 + "all required values (count=%u)", count);
646 + dev_err(&bm->pdev->dev, "PMM image sanity check failed "
647 + "(id=%02X, field1=%08X, field2=%08X)",
648 + adc_calib->id, adc_calib->field1, adc_calib->field2);
653 +/* Set the current measure timer that triggers on Tahvo IRQ 7
654 + * An interval of zero disables the timer. */
655 +static void n810bm_set_current_measure_timer(struct n810bm *bm,
656 + u16 millisec_interval)
658 + u16 value = millisec_interval;
660 + if (value <= 0xF905) {
661 + value = ((u64)0x10624DD3 * (u64)(value + 0xF9)) >> 32;
666 + tahvo_write(bm, TAHVO_REG_BATCURRTIMER, value & 0xFF);
668 + tahvo_set(bm, TAHVO_REG_CHGCTL,
669 + TAHVO_REG_CHGCTL_CURTIMRST);
670 + tahvo_clear(bm, TAHVO_REG_CHGCTL,
671 + TAHVO_REG_CHGCTL_CURTIMRST);
673 + if (millisec_interval) {
674 + if (!bm->tahvo_irq_enabled) {
675 + bm->tahvo_irq_enabled = 1;
676 + enable_irq(bm->tahvo_irq);
679 + if (bm->tahvo_irq_enabled) {
680 + bm->tahvo_irq_enabled = 0;
681 + disable_irq_nosync(bm->tahvo_irq);
685 + //TODO also do a software timer for safety.
688 +static void n810bm_enable_current_measure(struct n810bm *bm)
690 + WARN_ON(bm->current_measure_enabled < 0);
691 + if (!bm->current_measure_enabled) {
692 + /* Enable the current measurement circuitry */
693 + tahvo_set(bm, TAHVO_REG_CHGCTL,
694 + TAHVO_REG_CHGCTL_CURMEAS);
695 + dev_dbg(&bm->pdev->dev,
696 + "Current measurement circuitry enabled");
698 + bm->current_measure_enabled++;
701 +static void n810bm_disable_current_measure(struct n810bm *bm)
703 + bm->current_measure_enabled--;
704 + WARN_ON(bm->current_measure_enabled < 0);
705 + if (!bm->current_measure_enabled) {
706 + /* Disable the current measurement circuitry */
707 + tahvo_clear(bm, TAHVO_REG_CHGCTL,
708 + TAHVO_REG_CHGCTL_CURMEAS);
709 + dev_dbg(&bm->pdev->dev,
710 + "Current measurement circuitry disabled");
714 +/* Measure the actual battery current. Returns a signed value in mA.
715 + * Does only work, if current measurement was enabled. */
716 +static int n810bm_measure_batt_current(struct n810bm *bm)
719 + int adc = 0, ma, i;
721 + if (WARN_ON(bm->current_measure_enabled <= 0))
723 + for (i = 0; i < 3; i++) {
724 + retval = tahvo_read(bm, TAHVO_REG_BATCURR);
725 + adc += (s16)retval; /* Value is signed */
729 + //TODO convert to mA
735 +/* Requires bm->mutex locked */
736 +static int n810bm_measure_batt_current_async(struct n810bm *bm)
739 + bool charging = lipocharge_is_charging(&bm->charger);
741 + n810bm_enable_current_measure(bm);
743 + WARN_ON(bm->active_current_pwm != 0);
744 + tahvo_maskset(bm, TAHVO_REG_CHGCTL,
745 + TAHVO_REG_CHGCTL_EN |
746 + TAHVO_REG_CHGCTL_PWMOVR |
747 + TAHVO_REG_CHGCTL_PWMOVRZERO,
748 + TAHVO_REG_CHGCTL_EN |
749 + TAHVO_REG_CHGCTL_PWMOVR |
750 + (charging ? 0 : TAHVO_REG_CHGCTL_PWMOVRZERO));
751 + ma = n810bm_measure_batt_current(bm);
752 + tahvo_maskset(bm, TAHVO_REG_CHGCTL,
753 + TAHVO_REG_CHGCTL_EN |
754 + TAHVO_REG_CHGCTL_PWMOVR |
755 + TAHVO_REG_CHGCTL_PWMOVRZERO,
756 + (charging ? TAHVO_REG_CHGCTL_EN : 0));
757 + n810bm_disable_current_measure(bm);
762 +static int adc_sanity_check(struct n810bm *bm, unsigned int channel)
766 + value = retu_read_adc(&n810bm_retu_device->dev, channel);
768 + dev_err(&bm->pdev->dev, "Failed to read GND ADC channel %u",
772 + dev_dbg(&bm->pdev->dev,
773 + "GND ADC channel %u sanity check got value: %d",
776 + n810bm_emergency(bm, "GND ADC sanity check failed");
783 +static int n810bm_check_adc_sanity(struct n810bm *bm)
787 + /* Discard one conversion */
788 + retu_write(bm, RETU_REG_ADCSCR, 0);
789 + retu_read_adc(&n810bm_retu_device->dev, RETU_ADC_GND2);
791 + err = adc_sanity_check(bm, RETU_ADC_GND2);
798 +/* Measure the battery voltage. Returns the value in mV (or negative value on error). */
799 +static int n810bm_measure_batt_voltage(struct n810bm *bm)
803 + const unsigned int scale = 1000;
805 + adc = retu_adc_average(bm, RETU_ADC_BATTVOLT, 5);
810 + mv = 2800 + ((adc - 0x37) * (((4200 - 2800) * scale) / (0x236 - 0x37))) / scale;
812 + //TODO compensate for power consumption
813 + //TODO honor calibration values
818 +/* Measure the charger voltage. Returns the value in mV (or negative value on error). */
819 +static int n810bm_measure_charger_voltage(struct n810bm *bm)
824 + adc = retu_adc_average(bm, RETU_ADC_CHGVOLT, 5);
827 + //TODO convert to mV
833 +/* Measure backup battery voltage. Returns the value in mV (or negative value on error). */
834 +static int n810bm_measure_backup_batt_voltage(struct n810bm *bm)
839 + adc = retu_adc_average(bm, RETU_ADC_BKUPVOLT, 3);
842 + //TODO convert to mV
848 +/* Measure the battery temperature. Returns the value in K (or negative value on error). */
849 +static int n810bm_measure_batt_temp(struct n810bm *bm)
854 + adc = retu_adc_average(bm, RETU_ADC_BATTEMP, 3);
857 + //TODO convert to K
863 +/* Read the battery capacity via BSI pin. */
864 +static enum n810bm_capacity n810bm_read_batt_capacity(struct n810bm *bm)
867 + const unsigned int hyst = 20;
869 + adc = retu_adc_average(bm, RETU_ADC_BSI, 5);
871 + dev_err(&bm->pdev->dev, "Failed to read BSI ADC");
872 + return N810BM_CAP_UNKNOWN;
875 + if (adc >= 0x3B5 - hyst && adc <= 0x3B5 + hyst)
876 + return N810BM_CAP_1500MAH;
878 + dev_err(&bm->pdev->dev, "Capacity indicator 0x%X unknown", adc);
880 + return N810BM_CAP_UNKNOWN;
883 +/* Convert a battery voltage (in mV) to percentage. */
884 +static unsigned int n810bm_mvolt2percent(unsigned int mv)
886 + const unsigned int minv = 3700;
887 + const unsigned int maxv = 4150;
888 + unsigned int percent;
890 + mv = clamp(mv, minv, maxv);
891 + percent = (mv - minv) * 100 / (maxv - minv);
896 +static void n810bm_start_charge(struct n810bm *bm)
900 + WARN_ON(!bm->battery_present);
901 + WARN_ON(!bm->charger_present);
903 + /* Set PWM to zero */
904 + bm->active_current_pwm = 0;
905 + tahvo_write(bm, TAHVO_REG_CHGCURR, bm->active_current_pwm);
907 + /* Charge global enable */
908 + tahvo_maskset(bm, TAHVO_REG_CHGCTL,
909 + TAHVO_REG_CHGCTL_EN |
910 + TAHVO_REG_CHGCTL_PWMOVR |
911 + TAHVO_REG_CHGCTL_PWMOVRZERO,
912 + TAHVO_REG_CHGCTL_EN);
914 + WARN_ON((int)bm->capacity <= 0);
915 + bm->charger.capacity = bm->capacity;
916 + err = lipocharge_start(&bm->charger);
919 + /* Initialize current measurement circuitry */
920 + n810bm_enable_current_measure(bm);
921 + n810bm_set_current_measure_timer(bm, 250);
923 + dev_info(&bm->pdev->dev, "Charging battery");
924 + n810bm_notify_charger_state(bm);
925 + n810bm_notify_charger_pwm(bm);
928 +static void n810bm_stop_charge(struct n810bm *bm)
930 + if (lipocharge_is_charging(&bm->charger)) {
931 + n810bm_set_current_measure_timer(bm, 0);
932 + n810bm_disable_current_measure(bm);
934 + lipocharge_stop(&bm->charger);
936 + /* Set PWM to zero */
937 + bm->active_current_pwm = 0;
938 + tahvo_write(bm, TAHVO_REG_CHGCURR, bm->active_current_pwm);
940 + /* Charge global disable */
941 + tahvo_maskset(bm, TAHVO_REG_CHGCTL,
942 + TAHVO_REG_CHGCTL_EN |
943 + TAHVO_REG_CHGCTL_PWMOVR |
944 + TAHVO_REG_CHGCTL_PWMOVRZERO,
947 + dev_info(&bm->pdev->dev, "Not charging battery");
948 + n810bm_notify_charger_state(bm);
949 + n810bm_notify_charger_pwm(bm);
952 +/* Periodic check */
953 +static void n810bm_periodic_check_work(struct work_struct *work)
955 + struct n810bm *bm = container_of(to_delayed_work(work),
956 + struct n810bm, periodic_check_work);
958 + bool battery_was_present, charger_was_present;
961 + mutex_lock(&bm->mutex);
963 + status = retu_read(bm, RETU_REG_STATUS);
964 + battery_was_present = bm->battery_present;
965 + charger_was_present = bm->charger_present;
966 + bm->battery_present = !!(status & RETU_REG_STATUS_BATAVAIL);
967 + bm->charger_present = !!(status & RETU_REG_STATUS_CHGPLUG);
969 + if (bm->battery_present != battery_was_present) {
970 + /* Battery state changed */
971 + if (bm->battery_present) {
972 + bm->capacity = n810bm_read_batt_capacity(bm);
973 + if (bm->capacity == N810BM_CAP_UNKNOWN) {
974 + dev_err(&bm->pdev->dev, "Unknown battery detected");
976 + dev_info(&bm->pdev->dev, "Detected %u mAh battery",
977 + (unsigned int)bm->capacity);
980 + bm->capacity = N810BM_CAP_NONE;
981 + dev_info(&bm->pdev->dev, "The main battery was removed");
982 + //TODO disable charging
986 + if (bm->charger_present != charger_was_present) {
987 + /* Charger state changed */
988 + dev_info(&bm->pdev->dev, "The charger was %s",
989 + bm->charger_present ? "plugged in" : "removed");
990 + n810bm_notify_charger_present(bm);
993 + if ((bm->battery_present && !bm->charger_present) ||
994 + !n810bm_known_battery_present(bm)){
995 + /* We're draining the battery */
996 + mv = n810bm_measure_batt_voltage(bm);
998 + n810bm_emergency(bm,
999 + "check: Failed to measure voltage");
1001 + if (mv < N810BM_MIN_VOLTAGE_THRES) {
1002 + n810bm_emergency(bm,
1003 + "check: Minimum voltage threshold reached");
1007 + if (bm->charger_present && n810bm_known_battery_present(bm)) {
1008 + /* Known battery and charger are connected */
1009 + if (bm->charger_enabled) {
1010 + /* Charger is enabled */
1011 + if (!lipocharge_is_charging(&bm->charger)) {
1012 + //TODO start charging, if battery is below some threshold
1013 + n810bm_start_charge(bm);
1018 + if (lipocharge_is_charging(&bm->charger) && !bm->charger_present) {
1019 + /* Charger was unplugged. */
1020 + n810bm_stop_charge(bm);
1023 + mutex_unlock(&bm->mutex);
1024 + schedule_delayed_work(&bm->periodic_check_work,
1025 + round_jiffies_relative(N810BM_CHECK_INTERVAL));
1029 +static void n810bm_adc_irq_handler(unsigned long data)
1031 + struct n810bm *bm = (struct n810bm *)data;
1033 + retu_ack_irq(RETU_INT_ADCS);
1035 +dev_info(&bm->pdev->dev, "ADC interrupt triggered\n");
1039 +static irqreturn_t n810bm_tahvo_current_measure_irq_handler(int irq, void *data)
1041 + struct n810bm *bm = data;
1042 + int res, ma, mv, temp;
1044 + mutex_lock(&bm->mutex);
1045 + if (!lipocharge_is_charging(&bm->charger))
1048 + tahvo_maskset(bm, TAHVO_REG_CHGCTL,
1049 + TAHVO_REG_CHGCTL_PWMOVR |
1050 + TAHVO_REG_CHGCTL_PWMOVRZERO,
1051 + TAHVO_REG_CHGCTL_PWMOVR);
1052 + ma = n810bm_measure_batt_current(bm);
1053 + tahvo_maskset(bm, TAHVO_REG_CHGCTL,
1054 + TAHVO_REG_CHGCTL_PWMOVR |
1055 + TAHVO_REG_CHGCTL_PWMOVRZERO,
1056 + TAHVO_REG_CHGCTL_PWMOVR |
1057 + TAHVO_REG_CHGCTL_PWMOVRZERO);
1059 + mv = n810bm_measure_batt_voltage(bm);
1060 + tahvo_maskset(bm, TAHVO_REG_CHGCTL,
1061 + TAHVO_REG_CHGCTL_PWMOVR |
1062 + TAHVO_REG_CHGCTL_PWMOVRZERO,
1064 + temp = n810bm_measure_batt_temp(bm);
1065 + if (WARN_ON(mv < 0))
1067 + if (WARN_ON(temp < 0))
1070 + if (bm->verbose_charge_log) {
1071 + dev_info(&bm->pdev->dev,
1072 + "Battery charge state: %d mV, %d mA (%s)",
1074 + (ma <= 0) ? "discharging" : "charging");
1076 + res = lipocharge_update_state(&bm->charger, mv, ma, temp);
1079 + dev_info(&bm->pdev->dev, "Battery fully charged");
1080 + n810bm_stop_charge(bm);
1083 + mutex_unlock(&bm->mutex);
1085 + return IRQ_HANDLED;
1088 +#define DEFINE_ATTR_NOTIFY(attr_name) \
1089 + void n810bm_notify_##attr_name(struct n810bm *bm) \
1091 + set_bit(N810BM_NOTIFY_##attr_name, &bm->notify_flags); \
1093 + schedule_work(&bm->notify_work); \
1096 +#define DEFINE_SHOW_INT_FUNC(name, member) \
1097 + static ssize_t n810bm_attr_##name##_show(struct device *dev, \
1098 + struct device_attribute *attr, \
1101 + struct n810bm *bm = device_to_n810bm(dev); \
1104 + mutex_lock(&bm->mutex); \
1105 + count = snprintf(buf, PAGE_SIZE, "%d\n", (int)(bm->member)); \
1106 + mutex_unlock(&bm->mutex); \
1111 +#define DEFINE_STORE_INT_FUNC(name, member) \
1112 + static ssize_t n810bm_attr_##name##_store(struct device *dev, \
1113 + struct device_attribute *attr,\
1114 + const char *buf, size_t count)\
1116 + struct n810bm *bm = device_to_n810bm(dev); \
1120 + mutex_lock(&bm->mutex); \
1121 + err = strict_strtol(buf, 0, &val); \
1123 + bm->member = (typeof(bm->member))val; \
1124 + mutex_unlock(&bm->mutex); \
1126 + return err ? err : count; \
1129 +#define DEFINE_ATTR_SHOW_INT(name, member) \
1130 + DEFINE_SHOW_INT_FUNC(name, member) \
1131 + static DEVICE_ATTR(name, S_IRUGO, \
1132 + n810bm_attr_##name##_show, NULL);
1134 +#define DEFINE_ATTR_SHOW_STORE_INT(name, member) \
1135 + DEFINE_SHOW_INT_FUNC(name, member) \
1136 + DEFINE_STORE_INT_FUNC(name, member) \
1137 + static DEVICE_ATTR(name, S_IRUGO | S_IWUSR, \
1138 + n810bm_attr_##name##_show, \
1139 + n810bm_attr_##name##_store);
1141 +DEFINE_ATTR_SHOW_INT(battery_present, battery_present);
1142 +DEFINE_ATTR_SHOW_INT(charger_present, charger_present);
1143 +static DEFINE_ATTR_NOTIFY(charger_present);
1144 +DEFINE_ATTR_SHOW_INT(charger_state, charger.state);
1145 +static DEFINE_ATTR_NOTIFY(charger_state);
1146 +DEFINE_ATTR_SHOW_INT(charger_pwm, active_current_pwm);
1147 +static DEFINE_ATTR_NOTIFY(charger_pwm);
1148 +DEFINE_ATTR_SHOW_STORE_INT(charger_enable, charger_enabled);
1149 +DEFINE_ATTR_SHOW_STORE_INT(charger_verbose, verbose_charge_log);
1151 +static ssize_t n810bm_attr_battery_level_show(struct device *dev,
1152 + struct device_attribute *attr,
1155 + struct n810bm *bm = device_to_n810bm(dev);
1156 + ssize_t count = -ENODEV;
1159 + mutex_lock(&bm->mutex);
1160 + if (!bm->battery_present || lipocharge_is_charging(&bm->charger))
1163 + millivolt = n810bm_measure_batt_voltage(bm);
1164 + if (millivolt >= 0) {
1165 + count = snprintf(buf, PAGE_SIZE, "%u\n",
1166 + n810bm_mvolt2percent(millivolt));
1168 + mutex_unlock(&bm->mutex);
1172 +static DEVICE_ATTR(battery_level, S_IRUGO,
1173 + n810bm_attr_battery_level_show, NULL);
1175 +static ssize_t n810bm_attr_battery_capacity_show(struct device *dev,
1176 + struct device_attribute *attr,
1179 + struct n810bm *bm = device_to_n810bm(dev);
1183 + mutex_lock(&bm->mutex);
1184 + if (n810bm_known_battery_present(bm))
1185 + capacity = (int)bm->capacity;
1186 + count = snprintf(buf, PAGE_SIZE, "%d\n", capacity);
1187 + mutex_unlock(&bm->mutex);
1191 +static DEVICE_ATTR(battery_capacity, S_IRUGO,
1192 + n810bm_attr_battery_capacity_show, NULL);
1194 +static ssize_t n810bm_attr_battery_temp_show(struct device *dev,
1195 + struct device_attribute *attr,
1198 + struct n810bm *bm = device_to_n810bm(dev);
1199 + ssize_t count = -ENODEV;
1202 + mutex_lock(&bm->mutex);
1203 + k = n810bm_measure_batt_temp(bm);
1205 + count = snprintf(buf, PAGE_SIZE, "%d\n", k);
1206 + mutex_unlock(&bm->mutex);
1210 +static DEVICE_ATTR(battery_temp, S_IRUGO,
1211 + n810bm_attr_battery_temp_show, NULL);
1213 +static ssize_t n810bm_attr_charger_voltage_show(struct device *dev,
1214 + struct device_attribute *attr,
1217 + struct n810bm *bm = device_to_n810bm(dev);
1218 + ssize_t count = -ENODEV;
1221 + mutex_lock(&bm->mutex);
1222 + if (bm->charger_present)
1223 + mv = n810bm_measure_charger_voltage(bm);
1225 + count = snprintf(buf, PAGE_SIZE, "%d\n", mv);
1226 + mutex_unlock(&bm->mutex);
1230 +static DEVICE_ATTR(charger_voltage, S_IRUGO,
1231 + n810bm_attr_charger_voltage_show, NULL);
1233 +static ssize_t n810bm_attr_backup_battery_voltage_show(struct device *dev,
1234 + struct device_attribute *attr,
1237 + struct n810bm *bm = device_to_n810bm(dev);
1238 + ssize_t count = -ENODEV;
1241 + mutex_lock(&bm->mutex);
1242 + mv = n810bm_measure_backup_batt_voltage(bm);
1244 + count = snprintf(buf, PAGE_SIZE, "%d\n", mv);
1245 + mutex_unlock(&bm->mutex);
1249 +static DEVICE_ATTR(backup_battery_voltage, S_IRUGO,
1250 + n810bm_attr_backup_battery_voltage_show, NULL);
1252 +static ssize_t n810bm_attr_battery_current_show(struct device *dev,
1253 + struct device_attribute *attr,
1256 + struct n810bm *bm = device_to_n810bm(dev);
1257 + ssize_t count = -ENODEV;
1260 + mutex_lock(&bm->mutex);
1261 + if (bm->battery_present)
1262 + ma = n810bm_measure_batt_current_async(bm);
1263 + count = snprintf(buf, PAGE_SIZE, "%d\n", ma);
1264 + mutex_unlock(&bm->mutex);
1268 +static DEVICE_ATTR(battery_current, S_IRUGO,
1269 + n810bm_attr_battery_current_show, NULL);
1271 +static const struct device_attribute *n810bm_attrs[] = {
1272 + &dev_attr_battery_present,
1273 + &dev_attr_battery_level,
1274 + &dev_attr_battery_current,
1275 + &dev_attr_battery_capacity,
1276 + &dev_attr_battery_temp,
1277 + &dev_attr_backup_battery_voltage,
1278 + &dev_attr_charger_present,
1279 + &dev_attr_charger_state,
1280 + &dev_attr_charger_verbose,
1281 + &dev_attr_charger_voltage,
1282 + &dev_attr_charger_enable,
1283 + &dev_attr_charger_pwm,
1286 +static void n810bm_notify_work(struct work_struct *work)
1288 + struct n810bm *bm = container_of(work, struct n810bm, notify_work);
1289 + unsigned long notify_flags;
1291 + notify_flags = xchg(&bm->notify_flags, 0);
1294 +#define do_notify(attr_name) \
1296 + if (notify_flags & (1 << N810BM_NOTIFY_##attr_name)) { \
1297 + sysfs_notify(&bm->pdev->dev.kobj, NULL, \
1298 + dev_attr_##attr_name.attr.name); \
1302 + do_notify(charger_present);
1303 + do_notify(charger_state);
1304 + do_notify(charger_pwm);
1307 +static int n810bm_charger_set_current_pwm(struct lipocharge *c,
1308 + unsigned int duty_cycle)
1310 + struct n810bm *bm = container_of(c, struct n810bm, charger);
1311 + int err = -EINVAL;
1313 + WARN_ON(!mutex_is_locked(&bm->mutex));
1314 + if (WARN_ON(duty_cycle > 0xFF))
1316 + if (WARN_ON(!bm->charger_enabled))
1318 + if (WARN_ON(!bm->battery_present || !bm->charger_present))
1321 + if (duty_cycle != bm->active_current_pwm) {
1322 + bm->active_current_pwm = duty_cycle;
1323 + tahvo_write(bm, TAHVO_REG_CHGCURR, duty_cycle);
1324 + n810bm_notify_charger_pwm(bm);
1333 +static void n810bm_charger_emergency(struct lipocharge *c)
1335 + struct n810bm *bm = container_of(c, struct n810bm, charger);
1337 + n810bm_emergency(bm, "Battery charger fault");
1340 +static void n810bm_hw_exit(struct n810bm *bm)
1342 + n810bm_stop_charge(bm);
1343 + retu_write(bm, RETU_REG_ADCSCR, 0);
1346 +static int n810bm_hw_init(struct n810bm *bm)
1350 + err = n810bm_check_adc_sanity(bm);
1354 + n810bm_stop_charge(bm);
1359 +static void n810bm_cancel_and_flush_work(struct n810bm *bm)
1361 + cancel_delayed_work_sync(&bm->periodic_check_work);
1362 + cancel_work_sync(&bm->notify_work);
1363 + flush_scheduled_work();
1366 +static int n810bm_device_init(struct n810bm *bm)
1371 + bm->charger.rate = LIPORATE_p6C;
1372 + bm->charger.top_voltage = 4100;
1373 + bm->charger.duty_cycle_max = 0xFF;
1374 + bm->charger.set_current_pwm = n810bm_charger_set_current_pwm;
1375 + bm->charger.emergency = n810bm_charger_emergency;
1376 + lipocharge_init(&bm->charger, &bm->pdev->dev);
1378 + err = n810bm_hw_init(bm);
1381 + for (attr_index = 0; attr_index < ARRAY_SIZE(n810bm_attrs); attr_index++) {
1382 + err = device_create_file(&bm->pdev->dev, n810bm_attrs[attr_index]);
1384 + goto err_unwind_attrs;
1387 + err = retu_request_irq(RETU_INT_ADCS,
1388 + n810bm_adc_irq_handler,
1389 + (unsigned long)bm, "n810bm");
1391 + goto err_unwind_attrs;
1393 + bm->tahvo_irq = platform_get_irq(n810bm_tahvo_device, 0);
1394 + err = request_threaded_irq(bm->tahvo_irq, NULL,
1395 + n810bm_tahvo_current_measure_irq_handler,
1396 + IRQF_ONESHOT, "tahvo-n810bm", bm);
1398 + goto err_free_retu_irq;
1399 + disable_irq_nosync(bm->tahvo_irq);
1400 + bm->tahvo_irq_enabled = 0;
1402 + schedule_delayed_work(&bm->periodic_check_work,
1403 + round_jiffies_relative(N810BM_CHECK_INTERVAL));
1405 + bm->initialized = 1;
1406 + dev_info(&bm->pdev->dev, "Battery management initialized");
1411 +//XXX retu_free_irq(RETU_INT_ADCS);
1413 + for (attr_index--; attr_index >= 0; attr_index--)
1414 + device_remove_file(&bm->pdev->dev, n810bm_attrs[attr_index]);
1416 + n810bm_hw_exit(bm);
1418 + n810bm_cancel_and_flush_work(bm);
1423 +static void n810bm_device_exit(struct n810bm *bm)
1427 + if (!bm->initialized)
1430 + lipocharge_exit(&bm->charger);
1431 + free_irq(bm->tahvo_irq, bm);
1432 +//XXX retu_free_irq(RETU_INT_ADCS);
1433 + for (i = 0; i < ARRAY_SIZE(n810bm_attrs); i++)
1434 + device_remove_file(&bm->pdev->dev, n810bm_attrs[i]);
1436 + n810bm_cancel_and_flush_work(bm);
1438 + n810bm_hw_exit(bm);
1440 + bm->initialized = 0;
1443 +static void n810bm_pmm_block_found(const struct firmware *fw, void *context)
1445 + struct n810bm *bm = context;
1449 + dev_err(&bm->pdev->dev,
1450 + "CAL PMM block image file not found");
1453 + if (fw->size != N810BM_PMM_BLOCK_SIZE ||
1454 + memcmp(fw->data, "BME-PMM-BLOCK01", 15) != 0) {
1455 + dev_err(&bm->pdev->dev,
1456 + "CAL PMM block image file has an invalid format");
1460 + err = n810bm_parse_pmm_block(bm, fw);
1463 + release_firmware(fw);
1465 + err = n810bm_device_init(bm);
1467 + dev_err(&bm->pdev->dev,
1468 + "Failed to initialized battery management (%d)", err);
1474 + release_firmware(fw);
1479 +static int __devinit n810bm_probe(void)
1481 + struct n810bm *bm;
1484 + if (!n810bm_retu_device || !n810bm_tahvo_device)
1487 + bm = kzalloc(sizeof(*bm), GFP_KERNEL);
1490 + bm->pdev = n810bm_retu_device;
1491 + platform_set_drvdata(n810bm_retu_device, bm);
1492 + platform_set_drvdata(n810bm_tahvo_device, bm);
1493 + mutex_init(&bm->mutex);
1494 + INIT_DELAYED_WORK(&bm->periodic_check_work, n810bm_periodic_check_work);
1495 + INIT_WORK(&bm->notify_work, n810bm_notify_work);
1497 + dev_info(&bm->pdev->dev, "Requesting CAL BME PMM block firmware file "
1498 + N810BM_PMM_BLOCK_FILENAME);
1499 + err = request_firmware_nowait(THIS_MODULE, 1,
1500 + N810BM_PMM_BLOCK_FILENAME,
1501 + &bm->pdev->dev, GFP_KERNEL,
1502 + bm, n810bm_pmm_block_found);
1504 + dev_err(&bm->pdev->dev,
1505 + "Failed to request CAL PMM block image file (%d)", err);
1517 +static void __devexit n810bm_remove(void)
1519 + struct n810bm *bm;
1521 + if (!n810bm_retu_device || !n810bm_tahvo_device)
1523 + bm = platform_get_drvdata(n810bm_retu_device);
1525 + n810bm_device_exit(bm);
1528 + platform_set_drvdata(n810bm_retu_device, NULL);
1529 + platform_set_drvdata(n810bm_tahvo_device, NULL);
1532 +static int __devinit n810bm_retu_probe(struct platform_device *pdev)
1534 + n810bm_retu_device = pdev;
1535 + return n810bm_probe();
1538 +static int __devexit n810bm_retu_remove(struct platform_device *pdev)
1541 + n810bm_retu_device = NULL;
1545 +static int __devinit n810bm_tahvo_probe(struct platform_device *pdev)
1547 + n810bm_tahvo_device = pdev;
1548 + return n810bm_probe();
1551 +static int __devexit n810bm_tahvo_remove(struct platform_device *pdev)
1554 + n810bm_tahvo_device = NULL;
1558 +static struct platform_driver n810bm_retu_driver = {
1559 + .remove = __devexit_p(n810bm_retu_remove),
1561 + .name = "retu-n810bm",
1565 +static struct platform_driver n810bm_tahvo_driver = {
1566 + .remove = __devexit_p(n810bm_tahvo_remove),
1568 + .name = "tahvo-n810bm",
1572 +static int __init n810bm_modinit(void)
1576 + err = platform_driver_probe(&n810bm_retu_driver, n810bm_retu_probe);
1579 + err = platform_driver_probe(&n810bm_tahvo_driver, n810bm_tahvo_probe);
1581 + platform_driver_unregister(&n810bm_retu_driver);
1587 +module_init(n810bm_modinit);
1589 +static void __exit n810bm_modexit(void)
1591 + platform_driver_unregister(&n810bm_tahvo_driver);
1592 + platform_driver_unregister(&n810bm_retu_driver);
1594 +module_exit(n810bm_modexit);
1596 +MODULE_DESCRIPTION("Nokia n810 battery management");
1597 +MODULE_FIRMWARE(N810BM_PMM_BLOCK_FILENAME);
1598 +MODULE_LICENSE("GPL");
1599 +MODULE_AUTHOR("Michael Buesch");
1601 +++ b/drivers/cbus/lipocharge.c
1604 + * Generic LIPO battery charger
1606 + * Copyright (c) 2010-2011 Michael Buesch <mb@bu3sch.de>
1608 + * This program is free software; you can redistribute it and/or
1609 + * modify it under the terms of the GNU General Public License
1610 + * as published by the Free Software Foundation; either version 2
1611 + * of the License, or (at your option) any later version.
1613 + * This program is distributed in the hope that it will be useful,
1614 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
1615 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
1616 + * GNU General Public License for more details.
1621 +#include "lipocharge.h"
1623 +#include <linux/slab.h>
1626 +/* Hysteresis constants */
1627 +#define CURRENT_HYST 30 /* mA */
1628 +#define VOLTAGE_HYST 10 /* mV */
1630 +/* Threshold constants */
1631 +#define FINISH_CURRENT_PERCENT 3
1634 +/* Returns the requested first-stage charge current in mA */
1635 +static inline unsigned int get_stage1_charge_current(struct lipocharge *c)
1637 + /* current = (capacity * C) */
1638 + return c->capacity * c->rate / 1000;
1641 +void lipocharge_init(struct lipocharge *c, struct device *dev)
1644 + c->state = LIPO_IDLE;
1647 +void lipocharge_exit(struct lipocharge *c)
1649 + c->state = LIPO_IDLE;
1652 +int lipocharge_start(struct lipocharge *c)
1656 + if (c->state != LIPO_IDLE)
1658 + if (!c->set_current_pwm || !c->emergency)
1660 + if (!c->top_voltage || c->top_voltage > 4200)
1663 + c->active_duty_cycle = 0;
1664 + err = c->set_current_pwm(c, c->active_duty_cycle);
1667 + c->state = LIPO_FIRST_STAGE;
1672 +void lipocharge_stop(struct lipocharge *c)
1674 + if (c->state == LIPO_IDLE)
1676 + c->state = LIPO_IDLE;
1679 +static int lipocharge_increase_current(struct lipocharge *c,
1680 + unsigned int inc_permille)
1682 + int old_pwm, new_pwm;
1684 + if (c->active_duty_cycle >= c->duty_cycle_max)
1687 + old_pwm = c->active_duty_cycle;
1688 + new_pwm = old_pwm + (c->duty_cycle_max * inc_permille / 1000);
1689 + new_pwm = min(new_pwm, (int)c->duty_cycle_max);
1690 + c->active_duty_cycle = new_pwm;
1692 + dev_dbg(c->dev, "lipo: Increasing duty_cycle by "
1693 + "%u permille (0x%02X -> 0x%02X)",
1694 + inc_permille, old_pwm, new_pwm);
1696 + return c->set_current_pwm(c, c->active_duty_cycle);
1699 +static int lipocharge_decrease_current(struct lipocharge *c,
1700 + unsigned int dec_permille)
1702 + int old_pwm, new_pwm;
1704 + if (c->active_duty_cycle <= 0)
1707 + old_pwm = c->active_duty_cycle;
1708 + new_pwm = old_pwm - (c->duty_cycle_max * dec_permille / 1000);
1709 + new_pwm = max(0, new_pwm);
1710 + c->active_duty_cycle = new_pwm;
1712 + dev_dbg(c->dev, "lipo: Decreasing duty_cycle by "
1713 + "%u permille (0x%02X -> 0x%02X)",
1714 + dec_permille, old_pwm, new_pwm);
1716 + return c->set_current_pwm(c, c->active_duty_cycle);
1719 +/** lipocharge_update_state - Update the charge state
1720 + * @c: The context.
1721 + * @voltage_mV: The measured battery voltage.
1722 + * @current_mA: The measured charge current.
1723 + * negative -> drain.
1724 + * positive -> charge.
1725 + * @temp_K: Battery temperature in K.
1727 + * Returns 0 on success, -1 on error.
1728 + * Returns 1, if the charging process is finished.
1730 +int lipocharge_update_state(struct lipocharge *c,
1731 + unsigned int voltage_mV,
1733 + unsigned int temp_K)
1735 + int requested_current, current_diff;
1737 + unsigned int permille;
1742 + switch (c->state) {
1744 + dev_err(c->dev, "%s: called while idle", __func__);
1746 + case LIPO_FIRST_STAGE: /* Constant current */
1747 +//printk("GOT %u %d %u\n", voltage_mV, current_mA, temp_K);
1748 + if (voltage_mV >= c->top_voltage) {
1749 + /* Float voltage reached.
1750 + * Switch charger mode to "constant current" */
1751 + c->state = LIPO_SECOND_STAGE;
1752 + dev_dbg(c->dev, "Switched to second charging stage.");
1755 + /* Float voltage not reached, yet.
1756 + * Try to get the requested constant current. */
1757 + requested_current = get_stage1_charge_current(c);
1758 + if (current_mA < 0)
1760 + current_diff = requested_current - current_mA;
1761 + if (abs(requested_current - current_mA) > CURRENT_HYST) {
1762 + if (current_diff > 0) {
1763 + /* Increase current */
1764 + permille = current_diff * 1000 / requested_current;
1766 + err = lipocharge_increase_current(c, permille);
1770 + /* Decrease current */
1771 + permille = (-current_diff) * 1000 / requested_current;
1773 + err = lipocharge_decrease_current(c, permille);
1779 + case LIPO_SECOND_STAGE: /* Constant voltage */
1787 +++ b/drivers/cbus/lipocharge.h
1789 +#ifndef LIPOCHARGE_H_
1790 +#define LIPOCHARGE_H_
1792 +#include <linux/types.h>
1793 +#include <linux/device.h>
1796 +#define LIPORATE(a,b) (((a) * 1000) + ((b) * 100))
1797 +#define LIPORATE_p6C LIPORATE(0,6) /* 0.6C */
1799 +enum lipocharge_state {
1800 + LIPO_IDLE = 0, /* Not charging */
1801 + LIPO_FIRST_STAGE, /* Charging: constant current */
1802 + LIPO_SECOND_STAGE, /* Charging: constant voltage */
1805 +/** struct lipocharge - A generic LIPO charger
1807 + * @capacity: Battery capacity in mAh.
1808 + * @rate: Charge rate.
1809 + * @top_voltage: Fully charged voltage, in mV.
1810 + * @duty_cycle_max: Max value for duty_cycle.
1812 + * @set_charge_current: Set the charge current PWM duty cycle.
1813 + * @emergency: Something went wrong. Force shutdown.
1815 +struct lipocharge {
1816 + unsigned int capacity;
1817 + unsigned int rate;
1818 + unsigned int top_voltage;
1819 + unsigned int duty_cycle_max;
1821 + int (*set_current_pwm)(struct lipocharge *c, unsigned int duty_cycle);
1822 + void (*emergency)(struct lipocharge *c);
1825 + struct device *dev;
1826 + enum lipocharge_state state;
1827 + unsigned int active_duty_cycle;
1829 + //TODO implement timer to cut power after maximum charge time.
1832 +void lipocharge_init(struct lipocharge *c, struct device *dev);
1833 +void lipocharge_exit(struct lipocharge *c);
1835 +int lipocharge_start(struct lipocharge *c);
1836 +void lipocharge_stop(struct lipocharge *c);
1838 +int lipocharge_update_state(struct lipocharge *c,
1839 + unsigned int voltage_mV,
1841 + unsigned int temp_K);
1843 +static inline bool lipocharge_is_charging(struct lipocharge *c)
1845 + return (c->state != LIPO_IDLE);
1848 +#endif /* LIPOCHARGE_H_ */
1849 --- a/drivers/cbus/cbus.c
1850 +++ b/drivers/cbus/cbus.c
1852 #include <linux/gpio.h>
1853 #include <linux/platform_device.h>
1854 #include <linux/platform_data/cbus.h>
1855 +#include <linux/reboot.h>
1859 @@ -324,6 +325,13 @@ static void __exit cbus_bus_exit(void)
1861 module_exit(cbus_bus_exit);
1863 +void cbus_emergency(void)
1865 + machine_power_off();
1866 + panic("cbus: Failed to halt machine in emergency state\n");
1868 +EXPORT_SYMBOL(cbus_emergency);
1870 MODULE_DESCRIPTION("CBUS serial protocol");
1871 MODULE_LICENSE("GPL");
1872 MODULE_AUTHOR("Juha Yrjölä");
1873 --- a/drivers/cbus/cbus.h
1874 +++ b/drivers/cbus/cbus.h
1875 @@ -27,4 +27,6 @@ extern int cbus_read_reg(struct device *
1876 extern int cbus_write_reg(struct device *, unsigned dev, unsigned reg,
1879 +NORET_TYPE void cbus_emergency(void) ATTRIB_NORET;
1881 #endif /* __DRIVERS_CBUS_CBUS_H */
1882 --- a/drivers/cbus/retu.c
1883 +++ b/drivers/cbus/retu.c
1884 @@ -399,6 +399,11 @@ static int retu_allocate_children(struct
1888 + child = retu_allocate_child("retu-n810bm", parent, irq_base,
1889 + RETU_INT_ADCS, -1, 1);
1896 --- a/drivers/cbus/tahvo.c
1897 +++ b/drivers/cbus/tahvo.c
1898 @@ -129,6 +129,7 @@ void tahvo_set_clear_reg_bits(struct dev
1899 __tahvo_write_reg(tahvo, reg, w);
1900 mutex_unlock(&tahvo->mutex);
1902 +EXPORT_SYMBOL(tahvo_set_clear_reg_bits);
1904 static irqreturn_t tahvo_irq_handler(int irq, void *_tahvo)
1906 @@ -292,6 +293,11 @@ static int tahvo_allocate_children(struc
1910 + child = tahvo_allocate_child("tahvo-n810bm", parent,
1911 + irq_base + TAHVO_INT_BATCURR);