1 --- a/drivers/cbus/Kconfig
2 +++ b/drivers/cbus/Kconfig
3 @@ -72,4 +72,12 @@ config CBUS_RETU_HEADSET
4 to Retu/Vilma. Detection state and events are exposed through
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>
64 +#include "lipocharge.h"
67 +#define N810BM_PMM_BLOCK_FILENAME "n810-cal-bme-pmm.fw"
68 +#define N810BM_PMM_BLOCK_SIZE 0x600
69 +#define N810BM_PMM_GROUP_SIZE 0x200
70 +#define N810BM_PMM_ELEM_SIZE 0x10
72 +#define N810BM_CHECK_INTERVAL (HZ * 2)
73 +#define N810BM_MIN_VOLTAGE_THRES 3200 /* Absolute minimum voltage threshold */
77 + * The battery size indicator ADC measures the resistance between
78 + * the battery BSI pin and ground. This is used to detect the battery
79 + * capacity, as the BSI resistor is related to capacity.
81 + * Manually measured lookup table.
82 + * Hard to measure, thus not very accurate.
84 + * Resistance | ADC value
85 + * ========================
93 + * Manually measured lookup table.
94 + * Hard to measure, thus not very accurate.
96 + * Voltage | ADC value
97 + * =====================
116 +/* PMM block ADC IDs */
117 +enum n810bm_pmm_adc_id {
118 + N810BM_PMM_ADC_BATVOLT = 0x01, /* Battery voltage */
119 + N810BM_PMM_ADC_CHGVOLT = 0x02, /* Charger voltage */
120 + N810BM_PMM_ADC_GND2 = 0x03, /* Ground 0V */
121 + N810BM_PMM_ADC_BSI = 0x04, /* Battery size indicator */
122 + N810BM_PMM_ADC_BATTEMP = 0x05, /* Battery temperature */
123 + N810BM_PMM_ADC_HEADSET = 0x06, /* Headset detection */
124 + N810BM_PMM_ADC_HOOKDET = 0x07, /* Hook detection */
125 + N810BM_PMM_ADC_LIGHTSENS = 0x08, /* Light sensor */
126 + N810BM_PMM_ADC_BATCURR = 0x0E, /* Battery current */
127 + N810BM_PMM_ADC_BKUPVOLT = 0x13, /* Backup battery voltage */
128 + N810BM_PMM_ADC_LIGHTTEMP = 0x14, /* Light sensor temperature */
129 + N810BM_PMM_ADC_RFGP = 0x15, /* RF GP */
130 + N810BM_PMM_ADC_WBTX = 0x16, /* Wideband TX detection */
131 + N810BM_PMM_ADC_RETUTEMP = 0x17, /* RETU chip temperature */
132 + N810BM_PMM_ADC_0xFE = 0xFE,
135 +struct n810bm_adc_calib {
136 + enum n810bm_pmm_adc_id id;
145 +struct n810bm_calib {
146 + struct n810bm_adc_calib adc[25];
149 +enum n810bm_capacity {
150 + N810BM_CAP_UNKNOWN = -1,
151 + N810BM_CAP_NONE = 0,
152 + N810BM_CAP_1500MAH = 1500, /* 1500 mAh battery */
155 +enum n810bm_notify_flags {
156 + N810BM_NOTIFY_charger_present,
157 + N810BM_NOTIFY_charger_state,
158 + N810BM_NOTIFY_charger_pwm,
162 + bool battery_present; /* A battery is inserted */
163 + bool charger_present; /* The charger is connected */
164 + enum n810bm_capacity capacity; /* The capacity of the inserted battery (if any) */
166 + bool charger_enabled; /* Want to charge? */
167 + struct lipocharge charger; /* Charger subsystem */
168 + unsigned int active_current_pwm; /* Active value of TAHVO_REG_CHGCURR */
169 + int current_measure_enabled; /* Current measure enable refcount */
171 + struct platform_device *pdev;
172 + struct n810bm_calib calib; /* Calibration data */
174 + bool verbose_charge_log; /* Verbose charge logging */
176 + unsigned long notify_flags;
177 + struct work_struct notify_work;
178 + struct work_struct currmeas_irq_work;
179 + struct delayed_work periodic_check_work;
181 + bool initialized; /* The hardware was initialized */
182 + struct mutex mutex;
185 +static void n810bm_notify_charger_present(struct n810bm *bm);
186 +static void n810bm_notify_charger_state(struct n810bm *bm);
187 +static void n810bm_notify_charger_pwm(struct n810bm *bm);
190 +static struct platform_device *n810bm_retu_device;
191 +static struct platform_device *n810bm_tahvo_device;
194 +static inline struct n810bm * device_to_n810bm(struct device *dev)
196 + struct platform_device *pdev = to_platform_device(dev);
197 + struct n810bm *bm = platform_get_drvdata(pdev);
202 +static inline bool n810bm_known_battery_present(struct n810bm *bm)
204 + return bm->battery_present &&
205 + bm->capacity != N810BM_CAP_UNKNOWN &&
206 + bm->capacity != N810BM_CAP_NONE;
209 +static NORET_TYPE void n810bm_emergency(struct n810bm *bm, const char *message) ATTRIB_NORET;
210 +static void n810bm_emergency(struct n810bm *bm, const char *message)
212 + printk(KERN_EMERG "n810 battery management fatal fault: %s\n", message);
216 +static u16 tahvo_read(struct n810bm *bm, unsigned int reg)
218 + return tahvo_read_reg(reg);
221 +static void tahvo_maskset(struct n810bm *bm, unsigned int reg, u16 mask, u16 set)
223 + tahvo_set_clear_reg_bits(reg, set, mask);
226 +static inline void tahvo_write(struct n810bm *bm, unsigned int reg, u16 value)
228 + unsigned long flags;
230 + spin_lock_irqsave(&tahvo_lock, flags);
231 + tahvo_write_reg(reg, value);
232 + spin_unlock_irqrestore(&tahvo_lock, flags);
235 +static inline void tahvo_set(struct n810bm *bm, unsigned int reg, u16 mask)
237 + tahvo_set_clear_reg_bits(reg, mask, mask);
240 +static inline void tahvo_clear(struct n810bm *bm, unsigned int reg, u16 mask)
242 + tahvo_set_clear_reg_bits(reg, 0, mask);
245 +static u16 retu_read(struct n810bm *bm, unsigned int reg)
247 + return retu_read_reg(&n810bm_retu_device->dev, reg);
250 +static void retu_maskset(struct n810bm *bm, unsigned int reg, u16 mask, u16 set)
252 + retu_set_clear_reg_bits(&n810bm_retu_device->dev, reg, set, mask);
255 +static inline void retu_write(struct n810bm *bm, unsigned int reg, u16 value)
257 + retu_write_reg(&n810bm_retu_device->dev, reg, value);
260 +static int retu_adc_average(struct n810bm *bm, unsigned int chan,
261 + unsigned int nr_passes)
263 + unsigned int i, value = 0;
266 + if (WARN_ON(!nr_passes))
268 + for (i = 0; i < nr_passes; i++) {
269 + ret = retu_read_adc(&n810bm_retu_device->dev, chan);
274 + value /= nr_passes;
279 +static struct n810bm_adc_calib * n810bm_get_adc_calib(struct n810bm *bm,
280 + enum n810bm_pmm_adc_id id)
282 + unsigned int index = 0;
283 + struct n810bm_adc_calib *cal;
285 + if (id != N810BM_PMM_ADC_0xFE)
286 + index = (unsigned int)id + 1;
287 + if (index >= ARRAY_SIZE(bm->calib.adc))
290 + cal = &bm->calib.adc[index];
291 + WARN_ON(cal->id && cal->id != id);
296 +static int pmm_record_get(struct n810bm *bm,
297 + const struct firmware *pmm_block,
298 + void *buffer, size_t length,
299 + unsigned int group, unsigned int element, unsigned int offset)
301 + const u8 *pmm_area = pmm_block->data;
302 + u8 active_group_mask;
304 + if (pmm_block->size != N810BM_PMM_BLOCK_SIZE)
306 + if (group >= N810BM_PMM_BLOCK_SIZE / N810BM_PMM_GROUP_SIZE)
308 + if (element >= N810BM_PMM_GROUP_SIZE / N810BM_PMM_ELEM_SIZE)
310 + if (offset >= N810BM_PMM_ELEM_SIZE || length > N810BM_PMM_ELEM_SIZE ||
311 + length + offset > N810BM_PMM_ELEM_SIZE)
314 + active_group_mask = pmm_area[16];
315 + if (!(active_group_mask & (1 << group))) {
316 + dev_dbg(&bm->pdev->dev, "pwm_record_get: Requested group %u, "
317 + "but group is not active", group);
322 + pmm_area + group * N810BM_PMM_GROUP_SIZE
323 + + element * N810BM_PMM_ELEM_SIZE
330 +/* PMM block group 1 element */
331 +struct group1_element {
340 +static int extract_group1_elem(struct n810bm *bm,
341 + const struct firmware *pmm_block,
342 + const enum n810bm_pmm_adc_id *pmm_adc_ids, size_t nr_pmm_adc_ids,
343 + u32 field1_mask, u32 field2_mask)
345 + struct group1_element elem;
347 + unsigned int i, element_nr;
348 + struct n810bm_adc_calib *adc_calib;
350 + for (i = 0; i < nr_pmm_adc_ids; i++) {
351 + element_nr = (unsigned int)(pmm_adc_ids[i]) + 3;
353 + err = pmm_record_get(bm, pmm_block, &elem, sizeof(elem),
357 + adc_calib = n810bm_get_adc_calib(bm, elem.id);
359 + dev_err(&bm->pdev->dev, "extract_group1_elem: "
360 + "Could not get calib element for 0x%02X",
365 + if (adc_calib->flags == elem.flags) {
366 + adc_calib->field1 = le32_to_cpu(elem.field1) & field1_mask;
367 + adc_calib->field2 = le32_to_cpu(elem.field2) & field2_mask;
369 + dev_dbg(&bm->pdev->dev, "extract_group1_elem: "
370 + "Not extracting fields due to flags mismatch: "
371 + "0x%02X vs 0x%02X",
372 + adc_calib->flags, elem.flags);
379 +static int n810bm_parse_pmm_group1(struct n810bm *bm,
380 + const struct firmware *pmm_block)
382 + struct n810bm_adc_calib *adc_calib;
383 + struct group1_element elem;
386 + static const enum n810bm_pmm_adc_id pmm_adc_ids_1[] = {
387 + N810BM_PMM_ADC_BATVOLT,
388 + N810BM_PMM_ADC_CHGVOLT,
389 + N810BM_PMM_ADC_BKUPVOLT,
390 + N810BM_PMM_ADC_BATCURR,
392 + static const enum n810bm_pmm_adc_id pmm_adc_ids_2[] = {
393 + N810BM_PMM_ADC_BSI,
395 + static const enum n810bm_pmm_adc_id pmm_adc_ids_3[] = {
396 + N810BM_PMM_ADC_BATTEMP,
399 + /* Parse element 2 */
400 + err = pmm_record_get(bm, pmm_block, &elem, sizeof(elem),
403 + dev_err(&bm->pdev->dev,
404 + "PMM: Failed to get group 1 / element 2");
407 + if (elem.id == N810BM_PMM_ADC_0xFE && elem.flags == 0x05) {
408 + adc_calib = n810bm_get_adc_calib(bm, elem.id);
410 + dev_err(&bm->pdev->dev,
411 + "calib extract: Failed to get 0xFE calib");
414 + adc_calib->id = elem.id;
415 + adc_calib->flags = elem.flags;
416 + adc_calib->field1 = le32_to_cpu(elem.field1);
417 + adc_calib->field2 = le32_to_cpu(elem.field2);
420 + err = extract_group1_elem(bm, pmm_block,
421 + pmm_adc_ids_1, ARRAY_SIZE(pmm_adc_ids_1),
422 + 0xFFFFFFFF, 0xFFFFFFFF);
425 + err = extract_group1_elem(bm, pmm_block,
426 + pmm_adc_ids_2, ARRAY_SIZE(pmm_adc_ids_2),
430 + err = extract_group1_elem(bm, pmm_block,
431 + pmm_adc_ids_3, ARRAY_SIZE(pmm_adc_ids_3),
432 + 0xFFFFFFFF, 0x0000FFFF);
439 +static int n810bm_parse_pmm_group2(struct n810bm *bm,
440 + const struct firmware *pmm_block)
442 + dev_err(&bm->pdev->dev, "TODO: CAL BME PMM group 2 parser not implemented, yet");
443 + return -EOPNOTSUPP;
446 +static void n810bm_adc_calib_set_defaults(struct n810bm *bm)
448 + struct n810bm_adc_calib *adc_calib;
451 + static const struct n810bm_adc_calib defaults[] = {
452 + /* ADC group-nr 0 */
454 + .id = N810BM_PMM_ADC_HEADSET,
458 + .id = N810BM_PMM_ADC_HOOKDET,
462 + .id = N810BM_PMM_ADC_RFGP,
466 + .id = N810BM_PMM_ADC_LIGHTSENS,
470 + .id = N810BM_PMM_ADC_WBTX,
474 + .id = N810BM_PMM_ADC_RETUTEMP,
478 + .id = N810BM_PMM_ADC_GND2,
482 + /* ADC group-nr 1 */
484 + .id = N810BM_PMM_ADC_0xFE,
490 + .id = N810BM_PMM_ADC_BATVOLT,
496 + .id = N810BM_PMM_ADC_CHGVOLT,
502 + .id = N810BM_PMM_ADC_BKUPVOLT,
508 + .id = N810BM_PMM_ADC_BATCURR,
514 + /* ADC group-nr 2 */
516 + .id = N810BM_PMM_ADC_BSI,
522 + /* ADC group-nr 3 */
524 + .id = N810BM_PMM_ADC_BATTEMP,
527 + .field1 = 265423000,
530 + /* ADC group-nr 4 */
532 + .id = N810BM_PMM_ADC_LIGHTTEMP,
535 + .field1 = 19533778,
536 + .field2 = 308019670,
542 + /* Clear the array */
543 + memset(&bm->calib.adc, 0, sizeof(bm->calib.adc));
544 + for (i = 0; i < ARRAY_SIZE(bm->calib.adc); i++)
545 + bm->calib.adc[i].flags = 0xFF;
547 + /* Copy the defaults */
548 + for (i = 0; i < ARRAY_SIZE(defaults); i++) {
549 + adc_calib = n810bm_get_adc_calib(bm, defaults[i].id);
550 + if (WARN_ON(!adc_calib))
552 + *adc_calib = defaults[i];
556 +static int n810bm_parse_pmm_block(struct n810bm *bm,
557 + const struct firmware *pmm_block)
561 + unsigned int i, count;
562 + struct n810bm_adc_calib *adc_calib;
564 + /* Initialize to defaults */
565 + n810bm_adc_calib_set_defaults(bm);
567 + /* Parse the PMM data */
568 + err = pmm_record_get(bm, pmm_block, &byte, sizeof(byte),
569 + 1, 0, 0); /* group 1 / element 0 */
570 + err |= (byte != 0x01);
571 + err |= pmm_record_get(bm, pmm_block, &byte, sizeof(byte),
572 + 1, 1, 0); /* group 1 / element 1 */
573 + err |= (byte != 0x01);
575 + err = n810bm_parse_pmm_group2(bm, pmm_block);
577 + err = n810bm_parse_pmm_group1(bm, pmm_block);
581 + /* Sanity checks */
582 + for (i = 0, count = 0; i < ARRAY_SIZE(bm->calib.adc); i++) {
583 + adc_calib = &bm->calib.adc[i];
584 + if (adc_calib->flags == 0xFF)
586 + switch (adc_calib->id) {
587 + case N810BM_PMM_ADC_BATVOLT:
588 + if (adc_calib->field1 < 2400 ||
589 + adc_calib->field1 > 2700)
590 + goto value_check_fail;
591 + if (adc_calib->field2 < 20000 ||
592 + adc_calib->field2 > 23000)
593 + goto value_check_fail;
596 + case N810BM_PMM_ADC_BSI:
597 + if (adc_calib->field1 < 1100 ||
598 + adc_calib->field1 > 1300)
599 + goto value_check_fail;
602 + case N810BM_PMM_ADC_BATCURR:
603 + if (adc_calib->field2 < 7000 ||
604 + adc_calib->field2 > 12000)
605 + goto value_check_fail;
608 + case N810BM_PMM_ADC_0xFE:
609 + if ((s32)adc_calib->field1 > 14 ||
610 + (s32)adc_calib->field1 < -14)
611 + goto value_check_fail;
612 + if (adc_calib->field2 < 13000 ||
613 + adc_calib->field2 > 13350)
614 + goto value_check_fail;
617 + case N810BM_PMM_ADC_CHGVOLT:
618 + case N810BM_PMM_ADC_BATTEMP:
619 + case N810BM_PMM_ADC_BKUPVOLT:
622 + case N810BM_PMM_ADC_GND2:
623 + case N810BM_PMM_ADC_HOOKDET:
624 + case N810BM_PMM_ADC_LIGHTSENS:
625 + case N810BM_PMM_ADC_HEADSET:
626 + case N810BM_PMM_ADC_LIGHTTEMP:
627 + case N810BM_PMM_ADC_RFGP:
628 + case N810BM_PMM_ADC_WBTX:
629 + case N810BM_PMM_ADC_RETUTEMP:
632 + dev_dbg(&bm->pdev->dev,
633 + "ADC 0x%02X calib: 0x%02X 0x%02X 0x%08X 0x%08X 0x%04X 0x%04X",
634 + adc_calib->id, adc_calib->flags, adc_calib->adc_groupnr,
635 + adc_calib->field1, adc_calib->field2,
636 + adc_calib->field3, adc_calib->field4);
639 + dev_err(&bm->pdev->dev, "PMM sanity check: Did not find "
640 + "all required values (count=%u)", count);
647 + dev_err(&bm->pdev->dev, "PMM image sanity check failed "
648 + "(id=%02X, field1=%08X, field2=%08X)",
649 + adc_calib->id, adc_calib->field1, adc_calib->field2);
654 +/* Set the current measure timer that triggers on Tahvo IRQ 7
655 + * An interval of zero disables the timer. */
656 +static void n810bm_set_current_measure_timer(struct n810bm *bm,
657 + u16 millisec_interval)
659 + u16 value = millisec_interval;
661 + if (value <= 0xF905) {
662 + value = ((u64)0x10624DD3 * (u64)(value + 0xF9)) >> 32;
667 + tahvo_write(bm, TAHVO_REG_BATCURRTIMER, value & 0xFF);
669 + tahvo_set(bm, TAHVO_REG_CHGCTL,
670 + TAHVO_REG_CHGCTL_CURTIMRST);
671 + tahvo_clear(bm, TAHVO_REG_CHGCTL,
672 + TAHVO_REG_CHGCTL_CURTIMRST);
674 + if (millisec_interval)
675 + tahvo_enable_irq(TAHVO_INT_BATCURR);
677 + tahvo_disable_irq(TAHVO_INT_BATCURR);
679 + //TODO also do a software timer for safety.
682 +static void n810bm_enable_current_measure(struct n810bm *bm)
684 + WARN_ON(bm->current_measure_enabled < 0);
685 + if (!bm->current_measure_enabled) {
686 + /* Enable the current measurement circuitry */
687 + tahvo_set(bm, TAHVO_REG_CHGCTL,
688 + TAHVO_REG_CHGCTL_CURMEAS);
689 + dev_dbg(&bm->pdev->dev,
690 + "Current measurement circuitry enabled");
692 + bm->current_measure_enabled++;
695 +static void n810bm_disable_current_measure(struct n810bm *bm)
697 + bm->current_measure_enabled--;
698 + WARN_ON(bm->current_measure_enabled < 0);
699 + if (!bm->current_measure_enabled) {
700 + /* Disable the current measurement circuitry */
701 + tahvo_clear(bm, TAHVO_REG_CHGCTL,
702 + TAHVO_REG_CHGCTL_CURMEAS);
703 + dev_dbg(&bm->pdev->dev,
704 + "Current measurement circuitry disabled");
708 +/* Measure the actual battery current. Returns a signed value in mA.
709 + * Does only work, if current measurement was enabled. */
710 +static int n810bm_measure_batt_current(struct n810bm *bm)
713 + int adc = 0, ma, i;
715 + if (WARN_ON(bm->current_measure_enabled <= 0))
717 + for (i = 0; i < 3; i++) {
718 + retval = tahvo_read(bm, TAHVO_REG_BATCURR);
719 + adc += (s16)retval; /* Value is signed */
723 + //TODO convert to mA
729 +/* Requires bm->mutex locked */
730 +static int n810bm_measure_batt_current_async(struct n810bm *bm)
733 + bool charging = lipocharge_is_charging(&bm->charger);
735 + n810bm_enable_current_measure(bm);
737 + WARN_ON(bm->active_current_pwm != 0);
738 + tahvo_maskset(bm, TAHVO_REG_CHGCTL,
739 + TAHVO_REG_CHGCTL_EN |
740 + TAHVO_REG_CHGCTL_PWMOVR |
741 + TAHVO_REG_CHGCTL_PWMOVRZERO,
742 + TAHVO_REG_CHGCTL_EN |
743 + TAHVO_REG_CHGCTL_PWMOVR |
744 + (charging ? 0 : TAHVO_REG_CHGCTL_PWMOVRZERO));
745 + ma = n810bm_measure_batt_current(bm);
746 + tahvo_maskset(bm, TAHVO_REG_CHGCTL,
747 + TAHVO_REG_CHGCTL_EN |
748 + TAHVO_REG_CHGCTL_PWMOVR |
749 + TAHVO_REG_CHGCTL_PWMOVRZERO,
750 + (charging ? TAHVO_REG_CHGCTL_EN : 0));
751 + n810bm_disable_current_measure(bm);
756 +static int adc_sanity_check(struct n810bm *bm, unsigned int channel)
760 + value = retu_read_adc(&n810bm_retu_device->dev, channel);
762 + dev_err(&bm->pdev->dev, "Failed to read GND ADC channel %u",
766 + dev_dbg(&bm->pdev->dev,
767 + "GND ADC channel %u sanity check got value: %d",
770 + n810bm_emergency(bm, "GND ADC sanity check failed");
777 +static int n810bm_check_adc_sanity(struct n810bm *bm)
781 + /* Discard one conversion */
782 + retu_write(bm, RETU_REG_ADCSCR, 0);
783 + retu_read_adc(&n810bm_retu_device->dev, RETU_ADC_GND2);
785 + err = adc_sanity_check(bm, RETU_ADC_GND2);
792 +/* Measure the battery voltage. Returns the value in mV (or negative value on error). */
793 +static int n810bm_measure_batt_voltage(struct n810bm *bm)
797 + const unsigned int scale = 1000;
799 + adc = retu_adc_average(bm, RETU_ADC_BATTVOLT, 5);
804 + mv = 2800 + ((adc - 0x37) * (((4200 - 2800) * scale) / (0x236 - 0x37))) / scale;
806 + //TODO compensate for power consumption
807 + //TODO honor calibration values
812 +/* Measure the charger voltage. Returns the value in mV (or negative value on error). */
813 +static int n810bm_measure_charger_voltage(struct n810bm *bm)
818 + adc = retu_adc_average(bm, RETU_ADC_CHGVOLT, 5);
821 + //TODO convert to mV
827 +/* Measure backup battery voltage. Returns the value in mV (or negative value on error). */
828 +static int n810bm_measure_backup_batt_voltage(struct n810bm *bm)
833 + adc = retu_adc_average(bm, RETU_ADC_BKUPVOLT, 3);
836 + //TODO convert to mV
842 +/* Measure the battery temperature. Returns the value in K (or negative value on error). */
843 +static int n810bm_measure_batt_temp(struct n810bm *bm)
848 + adc = retu_adc_average(bm, RETU_ADC_BATTEMP, 3);
851 + //TODO convert to K
857 +/* Read the battery capacity via BSI pin. */
858 +static enum n810bm_capacity n810bm_read_batt_capacity(struct n810bm *bm)
861 + const unsigned int hyst = 20;
863 + adc = retu_adc_average(bm, RETU_ADC_BSI, 5);
865 + dev_err(&bm->pdev->dev, "Failed to read BSI ADC");
866 + return N810BM_CAP_UNKNOWN;
869 + if (adc >= 0x3B5 - hyst && adc <= 0x3B5 + hyst)
870 + return N810BM_CAP_1500MAH;
872 + dev_err(&bm->pdev->dev, "Capacity indicator 0x%X unknown", adc);
874 + return N810BM_CAP_UNKNOWN;
877 +/* Convert a battery voltage (in mV) to percentage. */
878 +static unsigned int n810bm_mvolt2percent(unsigned int mv)
880 + const unsigned int minv = 3700;
881 + const unsigned int maxv = 4150;
882 + unsigned int percent;
884 + mv = clamp(mv, minv, maxv);
885 + percent = (mv - minv) * 100 / (maxv - minv);
890 +static void n810bm_start_charge(struct n810bm *bm)
894 + WARN_ON(!bm->battery_present);
895 + WARN_ON(!bm->charger_present);
897 + /* Set PWM to zero */
898 + bm->active_current_pwm = 0;
899 + tahvo_write(bm, TAHVO_REG_CHGCURR, bm->active_current_pwm);
901 + /* Charge global enable */
902 + tahvo_maskset(bm, TAHVO_REG_CHGCTL,
903 + TAHVO_REG_CHGCTL_EN |
904 + TAHVO_REG_CHGCTL_PWMOVR |
905 + TAHVO_REG_CHGCTL_PWMOVRZERO,
906 + TAHVO_REG_CHGCTL_EN);
908 + WARN_ON((int)bm->capacity <= 0);
909 + bm->charger.capacity = bm->capacity;
910 + err = lipocharge_start(&bm->charger);
913 + /* Initialize current measurement circuitry */
914 + n810bm_enable_current_measure(bm);
915 + n810bm_set_current_measure_timer(bm, 250);
917 + dev_info(&bm->pdev->dev, "Charging battery");
918 + n810bm_notify_charger_state(bm);
919 + n810bm_notify_charger_pwm(bm);
922 +static void n810bm_stop_charge(struct n810bm *bm)
924 + if (lipocharge_is_charging(&bm->charger)) {
925 + n810bm_set_current_measure_timer(bm, 0);
926 + n810bm_disable_current_measure(bm);
928 + lipocharge_stop(&bm->charger);
930 + /* Set PWM to zero */
931 + bm->active_current_pwm = 0;
932 + tahvo_write(bm, TAHVO_REG_CHGCURR, bm->active_current_pwm);
934 + /* Charge global disable */
935 + tahvo_maskset(bm, TAHVO_REG_CHGCTL,
936 + TAHVO_REG_CHGCTL_EN |
937 + TAHVO_REG_CHGCTL_PWMOVR |
938 + TAHVO_REG_CHGCTL_PWMOVRZERO,
941 + dev_info(&bm->pdev->dev, "Not charging battery");
942 + n810bm_notify_charger_state(bm);
943 + n810bm_notify_charger_pwm(bm);
946 +/* Periodic check */
947 +static void n810bm_periodic_check_work(struct work_struct *work)
949 + struct n810bm *bm = container_of(to_delayed_work(work),
950 + struct n810bm, periodic_check_work);
952 + bool battery_was_present, charger_was_present;
955 + mutex_lock(&bm->mutex);
957 + status = retu_read(bm, RETU_REG_STATUS);
958 + battery_was_present = bm->battery_present;
959 + charger_was_present = bm->charger_present;
960 + bm->battery_present = !!(status & RETU_REG_STATUS_BATAVAIL);
961 + bm->charger_present = !!(status & RETU_REG_STATUS_CHGPLUG);
963 + if (bm->battery_present != battery_was_present) {
964 + /* Battery state changed */
965 + if (bm->battery_present) {
966 + bm->capacity = n810bm_read_batt_capacity(bm);
967 + if (bm->capacity == N810BM_CAP_UNKNOWN) {
968 + dev_err(&bm->pdev->dev, "Unknown battery detected");
970 + dev_info(&bm->pdev->dev, "Detected %u mAh battery",
971 + (unsigned int)bm->capacity);
974 + bm->capacity = N810BM_CAP_NONE;
975 + dev_info(&bm->pdev->dev, "The main battery was removed");
976 + //TODO disable charging
980 + if (bm->charger_present != charger_was_present) {
981 + /* Charger state changed */
982 + dev_info(&bm->pdev->dev, "The charger was %s",
983 + bm->charger_present ? "plugged in" : "removed");
984 + n810bm_notify_charger_present(bm);
987 + if ((bm->battery_present && !bm->charger_present) ||
988 + !n810bm_known_battery_present(bm)){
989 + /* We're draining the battery */
990 + mv = n810bm_measure_batt_voltage(bm);
992 + n810bm_emergency(bm,
993 + "check: Failed to measure voltage");
995 + if (mv < N810BM_MIN_VOLTAGE_THRES) {
996 + n810bm_emergency(bm,
997 + "check: Minimum voltage threshold reached");
1001 + if (bm->charger_present && n810bm_known_battery_present(bm)) {
1002 + /* Known battery and charger are connected */
1003 + if (bm->charger_enabled) {
1004 + /* Charger is enabled */
1005 + if (!lipocharge_is_charging(&bm->charger)) {
1006 + //TODO start charging, if battery is below some threshold
1007 + n810bm_start_charge(bm);
1012 + if (lipocharge_is_charging(&bm->charger) && !bm->charger_present) {
1013 + /* Charger was unplugged. */
1014 + n810bm_stop_charge(bm);
1017 + mutex_unlock(&bm->mutex);
1018 + schedule_delayed_work(&bm->periodic_check_work,
1019 + round_jiffies_relative(N810BM_CHECK_INTERVAL));
1023 +static void n810bm_adc_irq_handler(unsigned long data)
1025 + struct n810bm *bm = (struct n810bm *)data;
1027 + retu_ack_irq(RETU_INT_ADCS);
1029 +dev_info(&bm->pdev->dev, "ADC interrupt triggered\n");
1033 +static void n810bm_tahvo_current_measure_work(struct work_struct *work)
1035 + struct n810bm *bm = container_of(work, struct n810bm, currmeas_irq_work);
1036 + int res, ma, mv, temp;
1038 + mutex_lock(&bm->mutex);
1039 + if (!lipocharge_is_charging(&bm->charger))
1042 + tahvo_maskset(bm, TAHVO_REG_CHGCTL,
1043 + TAHVO_REG_CHGCTL_PWMOVR |
1044 + TAHVO_REG_CHGCTL_PWMOVRZERO,
1045 + TAHVO_REG_CHGCTL_PWMOVR);
1046 + ma = n810bm_measure_batt_current(bm);
1047 + tahvo_maskset(bm, TAHVO_REG_CHGCTL,
1048 + TAHVO_REG_CHGCTL_PWMOVR |
1049 + TAHVO_REG_CHGCTL_PWMOVRZERO,
1050 + TAHVO_REG_CHGCTL_PWMOVR |
1051 + TAHVO_REG_CHGCTL_PWMOVRZERO);
1053 + mv = n810bm_measure_batt_voltage(bm);
1054 + tahvo_maskset(bm, TAHVO_REG_CHGCTL,
1055 + TAHVO_REG_CHGCTL_PWMOVR |
1056 + TAHVO_REG_CHGCTL_PWMOVRZERO,
1058 + temp = n810bm_measure_batt_temp(bm);
1059 + if (WARN_ON(mv < 0))
1061 + if (WARN_ON(temp < 0))
1064 + if (bm->verbose_charge_log) {
1065 + dev_info(&bm->pdev->dev,
1066 + "Battery charge state: %d mV, %d mA (%s)",
1068 + (ma <= 0) ? "discharging" : "charging");
1070 + res = lipocharge_update_state(&bm->charger, mv, ma, temp);
1073 + dev_info(&bm->pdev->dev, "Battery fully charged");
1074 + n810bm_stop_charge(bm);
1077 + mutex_unlock(&bm->mutex);
1080 +static void n810bm_tahvo_current_measure_irq_handler(unsigned long data)
1082 + struct n810bm *bm = (struct n810bm *)data;
1084 + tahvo_ack_irq(TAHVO_INT_BATCURR);
1085 + schedule_work(&bm->currmeas_irq_work);
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 + cancel_work_sync(&bm->currmeas_irq_work);
1364 + flush_scheduled_work();
1367 +static int n810bm_device_init(struct n810bm *bm)
1372 + bm->charger.rate = LIPORATE_p6C;
1373 + bm->charger.top_voltage = 4100;
1374 + bm->charger.duty_cycle_max = 0xFF;
1375 + bm->charger.set_current_pwm = n810bm_charger_set_current_pwm;
1376 + bm->charger.emergency = n810bm_charger_emergency;
1377 + lipocharge_init(&bm->charger, &bm->pdev->dev);
1379 + err = n810bm_hw_init(bm);
1382 + for (attr_index = 0; attr_index < ARRAY_SIZE(n810bm_attrs); attr_index++) {
1383 + err = device_create_file(&bm->pdev->dev, n810bm_attrs[attr_index]);
1385 + goto err_unwind_attrs;
1388 + err = retu_request_irq(RETU_INT_ADCS,
1389 + n810bm_adc_irq_handler,
1390 + (unsigned long)bm, "n810bm");
1392 + goto err_unwind_attrs;
1394 + err = tahvo_request_irq(TAHVO_INT_BATCURR,
1395 + n810bm_tahvo_current_measure_irq_handler,
1396 + (unsigned long)bm, "n810bm");
1398 + goto err_free_retu_irq;
1399 + tahvo_disable_irq(TAHVO_INT_BATCURR);
1401 + schedule_delayed_work(&bm->periodic_check_work,
1402 + round_jiffies_relative(N810BM_CHECK_INTERVAL));
1404 + bm->initialized = 1;
1405 + dev_info(&bm->pdev->dev, "Battery management initialized");
1410 +//XXX retu_free_irq(RETU_INT_ADCS);
1412 + for (attr_index--; attr_index >= 0; attr_index--)
1413 + device_remove_file(&bm->pdev->dev, n810bm_attrs[attr_index]);
1415 + n810bm_hw_exit(bm);
1417 + n810bm_cancel_and_flush_work(bm);
1422 +static void n810bm_device_exit(struct n810bm *bm)
1426 + if (!bm->initialized)
1429 + lipocharge_exit(&bm->charger);
1430 + tahvo_free_irq(TAHVO_INT_BATCURR);
1431 +//XXX retu_free_irq(RETU_INT_ADCS);
1432 + for (i = 0; i < ARRAY_SIZE(n810bm_attrs); i++)
1433 + device_remove_file(&bm->pdev->dev, n810bm_attrs[i]);
1435 + n810bm_cancel_and_flush_work(bm);
1437 + n810bm_hw_exit(bm);
1439 + bm->initialized = 0;
1442 +static void n810bm_pmm_block_found(const struct firmware *fw, void *context)
1444 + struct n810bm *bm = context;
1448 + dev_err(&bm->pdev->dev,
1449 + "CAL PMM block image file not found");
1452 + if (fw->size != N810BM_PMM_BLOCK_SIZE ||
1453 + memcmp(fw->data, "BME-PMM-BLOCK01", 15) != 0) {
1454 + dev_err(&bm->pdev->dev,
1455 + "CAL PMM block image file has an invalid format");
1459 + err = n810bm_parse_pmm_block(bm, fw);
1462 + release_firmware(fw);
1464 + err = n810bm_device_init(bm);
1466 + dev_err(&bm->pdev->dev,
1467 + "Failed to initialized battery management (%d)", err);
1473 + release_firmware(fw);
1478 +static int __devinit n810bm_probe(void)
1480 + struct n810bm *bm;
1483 + if (!n810bm_retu_device || !n810bm_tahvo_device)
1486 + bm = kzalloc(sizeof(*bm), GFP_KERNEL);
1489 + bm->pdev = n810bm_retu_device;
1490 + platform_set_drvdata(n810bm_retu_device, bm);
1491 + platform_set_drvdata(n810bm_tahvo_device, bm);
1492 + mutex_init(&bm->mutex);
1493 + INIT_DELAYED_WORK(&bm->periodic_check_work, n810bm_periodic_check_work);
1494 + INIT_WORK(&bm->notify_work, n810bm_notify_work);
1495 + INIT_WORK(&bm->currmeas_irq_work, n810bm_tahvo_current_measure_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 +/* FIXME: for now alloc the device here... */
1573 +static struct platform_device n810bm_tahvo_dev = {
1574 + .name = "tahvo-n810bm",
1578 +static int __init n810bm_modinit(void)
1583 + err = platform_device_register(&n810bm_tahvo_dev);
1587 + err = platform_driver_probe(&n810bm_retu_driver, n810bm_retu_probe);
1590 + err = platform_driver_probe(&n810bm_tahvo_driver, n810bm_tahvo_probe);
1592 + platform_driver_unregister(&n810bm_retu_driver);
1598 +module_init(n810bm_modinit);
1600 +static void __exit n810bm_modexit(void)
1603 + platform_device_unregister(&n810bm_tahvo_dev);
1605 + platform_driver_unregister(&n810bm_tahvo_driver);
1606 + platform_driver_unregister(&n810bm_retu_driver);
1608 +module_exit(n810bm_modexit);
1610 +MODULE_DESCRIPTION("Nokia n810 battery management");
1611 +MODULE_FIRMWARE(N810BM_PMM_BLOCK_FILENAME);
1612 +MODULE_LICENSE("GPL");
1613 +MODULE_AUTHOR("Michael Buesch");
1615 +++ b/drivers/cbus/lipocharge.c
1618 + * Generic LIPO battery charger
1620 + * Copyright (c) 2010-2011 Michael Buesch <mb@bu3sch.de>
1622 + * This program is free software; you can redistribute it and/or
1623 + * modify it under the terms of the GNU General Public License
1624 + * as published by the Free Software Foundation; either version 2
1625 + * of the License, or (at your option) any later version.
1627 + * This program is distributed in the hope that it will be useful,
1628 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
1629 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
1630 + * GNU General Public License for more details.
1635 +#include "lipocharge.h"
1637 +#include <linux/slab.h>
1640 +/* Hysteresis constants */
1641 +#define CURRENT_HYST 30 /* mA */
1642 +#define VOLTAGE_HYST 10 /* mV */
1644 +/* Threshold constants */
1645 +#define FINISH_CURRENT_PERCENT 3
1648 +/* Returns the requested first-stage charge current in mA */
1649 +static inline unsigned int get_stage1_charge_current(struct lipocharge *c)
1651 + /* current = (capacity * C) */
1652 + return c->capacity * c->rate / 1000;
1655 +void lipocharge_init(struct lipocharge *c, struct device *dev)
1658 + c->state = LIPO_IDLE;
1661 +void lipocharge_exit(struct lipocharge *c)
1663 + c->state = LIPO_IDLE;
1666 +int lipocharge_start(struct lipocharge *c)
1670 + if (c->state != LIPO_IDLE)
1672 + if (!c->set_current_pwm || !c->emergency)
1674 + if (!c->top_voltage || c->top_voltage > 4200)
1677 + c->active_duty_cycle = 0;
1678 + err = c->set_current_pwm(c, c->active_duty_cycle);
1681 + c->state = LIPO_FIRST_STAGE;
1686 +void lipocharge_stop(struct lipocharge *c)
1688 + if (c->state == LIPO_IDLE)
1690 + c->state = LIPO_IDLE;
1693 +static int lipocharge_increase_current(struct lipocharge *c,
1694 + unsigned int inc_permille)
1696 + int old_pwm, new_pwm;
1698 + if (c->active_duty_cycle >= c->duty_cycle_max)
1701 + old_pwm = c->active_duty_cycle;
1702 + new_pwm = old_pwm + (c->duty_cycle_max * inc_permille / 1000);
1703 + new_pwm = min(new_pwm, (int)c->duty_cycle_max);
1704 + c->active_duty_cycle = new_pwm;
1706 + dev_dbg(c->dev, "lipo: Increasing duty_cycle by "
1707 + "%u permille (0x%02X -> 0x%02X)",
1708 + inc_permille, old_pwm, new_pwm);
1710 + return c->set_current_pwm(c, c->active_duty_cycle);
1713 +static int lipocharge_decrease_current(struct lipocharge *c,
1714 + unsigned int dec_permille)
1716 + int old_pwm, new_pwm;
1718 + if (c->active_duty_cycle <= 0)
1721 + old_pwm = c->active_duty_cycle;
1722 + new_pwm = old_pwm - (c->duty_cycle_max * dec_permille / 1000);
1723 + new_pwm = max(0, new_pwm);
1724 + c->active_duty_cycle = new_pwm;
1726 + dev_dbg(c->dev, "lipo: Decreasing duty_cycle by "
1727 + "%u permille (0x%02X -> 0x%02X)",
1728 + dec_permille, old_pwm, new_pwm);
1730 + return c->set_current_pwm(c, c->active_duty_cycle);
1733 +/** lipocharge_update_state - Update the charge state
1734 + * @c: The context.
1735 + * @voltage_mV: The measured battery voltage.
1736 + * @current_mA: The measured charge current.
1737 + * negative -> drain.
1738 + * positive -> charge.
1739 + * @temp_K: Battery temperature in K.
1741 + * Returns 0 on success, -1 on error.
1742 + * Returns 1, if the charging process is finished.
1744 +int lipocharge_update_state(struct lipocharge *c,
1745 + unsigned int voltage_mV,
1747 + unsigned int temp_K)
1749 + int requested_current, current_diff;
1751 + unsigned int permille;
1756 + switch (c->state) {
1758 + dev_err(c->dev, "%s: called while idle", __func__);
1760 + case LIPO_FIRST_STAGE: /* Constant current */
1761 +//printk("GOT %u %d %u\n", voltage_mV, current_mA, temp_K);
1762 + if (voltage_mV >= c->top_voltage) {
1763 + /* Float voltage reached.
1764 + * Switch charger mode to "constant current" */
1765 + c->state = LIPO_SECOND_STAGE;
1766 + dev_dbg(c->dev, "Switched to second charging stage.");
1769 + /* Float voltage not reached, yet.
1770 + * Try to get the requested constant current. */
1771 + requested_current = get_stage1_charge_current(c);
1772 + if (current_mA < 0)
1774 + current_diff = requested_current - current_mA;
1775 + if (abs(requested_current - current_mA) > CURRENT_HYST) {
1776 + if (current_diff > 0) {
1777 + /* Increase current */
1778 + permille = current_diff * 1000 / requested_current;
1780 + err = lipocharge_increase_current(c, permille);
1784 + /* Decrease current */
1785 + permille = (-current_diff) * 1000 / requested_current;
1787 + err = lipocharge_decrease_current(c, permille);
1793 + case LIPO_SECOND_STAGE: /* Constant voltage */
1801 +++ b/drivers/cbus/lipocharge.h
1803 +#ifndef LIPOCHARGE_H_
1804 +#define LIPOCHARGE_H_
1806 +#include <linux/types.h>
1807 +#include <linux/device.h>
1810 +#define LIPORATE(a,b) (((a) * 1000) + ((b) * 100))
1811 +#define LIPORATE_p6C LIPORATE(0,6) /* 0.6C */
1813 +enum lipocharge_state {
1814 + LIPO_IDLE = 0, /* Not charging */
1815 + LIPO_FIRST_STAGE, /* Charging: constant current */
1816 + LIPO_SECOND_STAGE, /* Charging: constant voltage */
1819 +/** struct lipocharge - A generic LIPO charger
1821 + * @capacity: Battery capacity in mAh.
1822 + * @rate: Charge rate.
1823 + * @top_voltage: Fully charged voltage, in mV.
1824 + * @duty_cycle_max: Max value for duty_cycle.
1826 + * @set_charge_current: Set the charge current PWM duty cycle.
1827 + * @emergency: Something went wrong. Force shutdown.
1829 +struct lipocharge {
1830 + unsigned int capacity;
1831 + unsigned int rate;
1832 + unsigned int top_voltage;
1833 + unsigned int duty_cycle_max;
1835 + int (*set_current_pwm)(struct lipocharge *c, unsigned int duty_cycle);
1836 + void (*emergency)(struct lipocharge *c);
1839 + struct device *dev;
1840 + enum lipocharge_state state;
1841 + unsigned int active_duty_cycle;
1843 + //TODO implement timer to cut power after maximum charge time.
1846 +void lipocharge_init(struct lipocharge *c, struct device *dev);
1847 +void lipocharge_exit(struct lipocharge *c);
1849 +int lipocharge_start(struct lipocharge *c);
1850 +void lipocharge_stop(struct lipocharge *c);
1852 +int lipocharge_update_state(struct lipocharge *c,
1853 + unsigned int voltage_mV,
1855 + unsigned int temp_K);
1857 +static inline bool lipocharge_is_charging(struct lipocharge *c)
1859 + return (c->state != LIPO_IDLE);
1862 +#endif /* LIPOCHARGE_H_ */
1863 --- a/drivers/cbus/cbus.c
1864 +++ b/drivers/cbus/cbus.c
1866 #include <linux/platform_device.h>
1868 #include <plat/cbus.h>
1869 +#include <linux/reboot.h>
1873 @@ -323,6 +324,13 @@ static void __exit cbus_bus_exit(void)
1875 module_exit(cbus_bus_exit);
1877 +void cbus_emergency(void)
1879 + machine_power_off();
1880 + panic("cbus: Failed to halt machine in emergency state\n");
1882 +EXPORT_SYMBOL(cbus_emergency);
1884 MODULE_DESCRIPTION("CBUS serial protocol");
1885 MODULE_LICENSE("GPL");
1886 MODULE_AUTHOR("Juha Yrjölä");
1887 --- a/drivers/cbus/cbus.h
1888 +++ b/drivers/cbus/cbus.h
1890 extern int cbus_read_reg(unsigned dev, unsigned reg);
1891 extern int cbus_write_reg(unsigned dev, unsigned reg, unsigned val);
1893 +NORET_TYPE void cbus_emergency(void) ATTRIB_NORET;
1895 #endif /* __DRIVERS_CBUS_CBUS_H */
1896 --- a/drivers/cbus/retu.c
1897 +++ b/drivers/cbus/retu.c
1898 @@ -425,6 +425,11 @@ static int retu_allocate_children(struct
1902 + child = retu_allocate_child("retu-n810bm", parent, irq_base,
1903 + RETU_INT_ADCS, -1, 1);
1910 --- a/drivers/cbus/tahvo.c
1911 +++ b/drivers/cbus/tahvo.c
1912 @@ -54,6 +54,7 @@ static int tahvo_is_betty;
1914 static struct tasklet_struct tahvo_tasklet;
1915 spinlock_t tahvo_lock = SPIN_LOCK_UNLOCKED;
1916 +EXPORT_SYMBOL(tahvo_lock);
1918 struct tahvo_irq_handler_desc {
1919 int (*func)(unsigned long);
1920 @@ -115,6 +116,7 @@ void tahvo_set_clear_reg_bits(unsigned r
1921 tahvo_write_reg(reg, w);
1922 spin_unlock_irqrestore(&tahvo_lock, flags);
1924 +EXPORT_SYMBOL(tahvo_set_clear_reg_bits);
1927 * Disable given TAHVO interrupt