2 arch/arm/mach-omap2/board-n8x0.c | 13 +
3 drivers/cbus/Kconfig | 12 +
4 drivers/cbus/Makefile | 3
5 drivers/cbus/lipocharge.c | 63 ++++++
6 drivers/cbus/lipocharge.h | 50 ++++
7 drivers/cbus/n810bm_main.c | 397 +++++++++++++++++++++++++++++++++++++++
8 drivers/cbus/retu.c | 4
9 drivers/cbus/retu.h | 3
10 drivers/cbus/tahvo.h | 6
11 9 files changed, 548 insertions(+), 3 deletions(-)
13 --- linux-2.6.36-rc7.orig/drivers/cbus/Kconfig
14 +++ linux-2.6.36-rc7/drivers/cbus/Kconfig
15 @@ -94,4 +94,16 @@ config CBUS_RETU_HEADSET
16 to Retu/Vilma. Detection state and events are exposed through
20 + depends on CBUS_RETU && CBUS_TAHVO
21 + tristate "Nokia n810 battery management"
23 + Nokia n810 device battery management.
25 + WARNING: This driver is based on reverse engineered information.
26 + It is possibly dangerous to use this software.
27 + Use this software at your own risk!
32 --- linux-2.6.36-rc7.orig/drivers/cbus/Makefile
33 +++ linux-2.6.36-rc7/drivers/cbus/Makefile
34 @@ -12,3 +12,6 @@ obj-$(CONFIG_CBUS_RETU_WDT) += retu-wdt.
35 obj-$(CONFIG_CBUS_TAHVO_USER) += tahvo-user.o
36 obj-$(CONFIG_CBUS_RETU_USER) += retu-user.o
37 obj-$(CONFIG_CBUS_RETU_HEADSET) += retu-headset.o
38 +n810bm-y += n810bm_main.o
39 +n810bm-y += lipocharge.o
40 +obj-$(CONFIG_N810BM) += n810bm.o
42 +++ linux-2.6.36-rc7/drivers/cbus/n810bm_main.c
45 + * Nokia n810 battery management
47 + * WARNING: This driver is based on reverse engineered information.
48 + * It is possibly dangerous to use this software.
49 + * Use this software at your own risk!
51 + * Copyright (c) 2010 Michael Buesch <mb@bu3sch.de>
53 + * This program is free software; you can redistribute it and/or
54 + * modify it under the terms of the GNU General Public License
55 + * as published by the Free Software Foundation; either version 2
56 + * of the License, or (at your option) any later version.
58 + * This program is distributed in the hope that it will be useful,
59 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
60 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
61 + * GNU General Public License for more details.
64 +#include <linux/module.h>
65 +#include <linux/device.h>
66 +#include <linux/platform_device.h>
67 +#include <linux/slab.h>
68 +#include <linux/spinlock.h>
69 +#include <linux/timer.h>
70 +#include <linux/reboot.h>
74 +#include "lipocharge.h"
77 +#define N810BM_CHECK_INTERVAL (HZ * 5)
78 +#define N810BM_MIN_VOLTAGE_THRES 3300 /* Absolute minimum voltage threshold */
81 +/* Battery related retu ADC channels */
82 +#define RETU_ADC_BSI 0x01 /* Battery Size Indicator */
83 +#define RETU_ADC_BATTVOLT 0x08 /* Battery voltage measurement */
86 + * The battery size indicator ADC measures the resistance between
87 + * the battery BSI pin and ground. This is used to detect the battery
88 + * capacity, as the BSI resistor is related to capacity.
90 + * Manually measured lookup table.
91 + * Hard to measure, thus not very accurate.
93 + * Resistance | ADC value
94 + * ========================
101 +/* RETU_ADC_BATTVOLT
102 + * Manually measured lookup table.
103 + * Hard to measure, thus not very accurate.
105 + * Voltage | ADC value
106 + * =====================
125 +enum n810bm_capacity {
126 + N810BM_CAP_UNKNOWN = 0,
127 + N810BM_CAP_1500MAH = 1500, /* 1500 mAh battery */
131 + struct platform_device *pdev;
133 + enum n810bm_capacity capacity;
134 + struct timer_list check_timer;
136 + struct lipocharge *charger;
142 +static NORET_TYPE void n810bm_emergency(struct n810bm *bm, const char *message) ATTRIB_NORET;
143 +static void n810bm_emergency(struct n810bm *bm, const char *message)
145 + printk(KERN_EMERG "n810 battery management fatal fault: %s\n", message);
146 + /* Force a hard shutdown. */
147 + machine_power_off();
148 + panic("n810bm: Failed to halt machine in emergency state\n");
152 +static u16 retu_read(struct n810bm *bm, unsigned int reg)
155 + unsigned long flags;
157 + spin_lock_irqsave(&retu_lock, flags);
158 + ret = retu_read_reg(reg);
159 + spin_unlock_irqrestore(&retu_lock, flags);
160 + if (ret < 0 || ret > 0xFFFF)
161 + n810bm_emergency(bm, "retu_read");
167 +static void retu_maskset(struct n810bm *bm, unsigned int reg, u16 mask, u16 set)
170 + unsigned long flags;
173 + spin_lock_irqsave(&retu_lock, flags);
175 + ret = retu_read_reg(reg);
176 + if (ret < 0 || ret > 0xFFFF)
183 + ret = retu_write_reg(reg, value);
186 + spin_unlock_irqrestore(&retu_lock, flags);
191 + spin_unlock_irqrestore(&retu_lock, flags);
192 + n810bm_emergency(bm, "retu_maskset");
195 +static inline void retu_write(struct n810bm *bm, unsigned int reg, u16 value)
197 + return retu_maskset(bm, reg, 0xFFFF, value);
200 +static int retu_adc_average(struct n810bm *bm, unsigned int chan,
201 + unsigned int nr_passes)
203 + unsigned int i, value = 0;
206 + if (WARN_ON(!nr_passes))
208 + for (i = 0; i < nr_passes; i++) {
209 + ret = retu_read_adc(chan);
214 + value /= nr_passes;
219 +/* Measure the battery voltage. Returns the value in mV (or negative value on error). */
220 +static int n810bm_measure_batt_voltage(struct n810bm *bm)
224 + const unsigned int scale = 1000;
226 + adc = retu_adc_average(bm, RETU_ADC_BATTVOLT, 5);
231 + mv = 2800 + ((adc - 0x37) * (((4200 - 2800) * scale) / (0x236 - 0x37))) / scale;
236 +/* Read the battery capacity via BSI pin. */
237 +static enum n810bm_capacity n810bm_read_batt_capacity(struct n810bm *bm)
240 + const unsigned int hyst = 20;
242 + adc = retu_adc_average(bm, RETU_ADC_BSI, 5);
244 + dev_err(&bm->pdev->dev, "Failed to read BSI ADC");
245 + return N810BM_CAP_UNKNOWN;
248 + if (adc >= 0x3B5 - hyst && adc <= 0x3B5 + hyst)
249 + return N810BM_CAP_1500MAH;
251 + dev_err(&bm->pdev->dev, "Capacity indicator 0x%X unknown", adc);
253 + return N810BM_CAP_UNKNOWN;
256 +/* Convert a battery voltage (in mV) to percentage. */
257 +static unsigned int n810bm_mvolt2percent(unsigned int mv)
259 + const unsigned int minv = 3700;
260 + const unsigned int maxv = 4150;
261 + unsigned int percent;
263 + mv = clamp(mv, minv, maxv);
264 + percent = (mv - minv) * 100 / (maxv - minv);
269 +static void n810bm_check_timer(unsigned long data)
271 + struct n810bm *bm = (struct n810bm *)data;
272 + unsigned long flags;
275 + spin_lock_irqsave(&bm->lock, flags);
277 + mv = n810bm_measure_batt_voltage(bm);
279 + n810bm_emergency(bm, "check timer: Failed to measure");
280 + if (mv < N810BM_MIN_VOLTAGE_THRES)
281 + n810bm_emergency(bm, "check timer: Minimum voltage threshold reached");
283 + mod_timer(&bm->check_timer, round_jiffies(jiffies + N810BM_CHECK_INTERVAL));
284 + spin_unlock_irqrestore(&bm->lock, flags);
289 +static void n810bm_adc_irq_handler(unsigned long data)
291 + struct n810bm *bm = (struct n810bm *)data;
293 + retu_ack_irq(RETU_INT_ADCS);
295 +printk("n810bm: ADC timer triggered\n");
298 +static ssize_t n810bm_attr_charge_show(struct device *dev,
299 + struct device_attribute *attr,
302 + struct platform_device *pdev = to_platform_device(dev);
303 + struct n810bm *bm = platform_get_drvdata(pdev);
308 + spin_lock_irq(&bm->lock);
309 + millivolt = n810bm_measure_batt_voltage(bm);
310 + if (millivolt >= 0) {
311 + count = snprintf(buf, PAGE_SIZE, "%u\n",
312 + n810bm_mvolt2percent(millivolt));
315 + spin_unlock_irq(&bm->lock);
317 + return err ? err : count;
319 +static DEVICE_ATTR(batt_charge, 0444, n810bm_attr_charge_show, NULL);
321 +static ssize_t n810bm_attr_capacity_show(struct device *dev,
322 + struct device_attribute *attr,
325 + struct platform_device *pdev = to_platform_device(dev);
326 + struct n810bm *bm = platform_get_drvdata(pdev);
329 + spin_lock_irq(&bm->lock);
330 + count = snprintf(buf, PAGE_SIZE, "%u\n",
331 + (unsigned int)bm->capacity);
332 + spin_unlock_irq(&bm->lock);
336 +static DEVICE_ATTR(batt_capacity, 0444, n810bm_attr_capacity_show, NULL);
338 +static void n810bm_hw_exit(struct n810bm *bm)
340 + retu_write(bm, RETU_REG_ADCSCR, 0);
343 +static int n810bm_hw_init(struct n810bm *bm)
345 + retu_write(bm, RETU_REG_ADCSCR, 0);
347 + bm->capacity = n810bm_read_batt_capacity(bm);
348 + if (bm->capacity == N810BM_CAP_UNKNOWN) {
349 + dev_err(&bm->pdev->dev, "Unknown battery detected");
352 + dev_info(&bm->pdev->dev, "Detected %u mAh battery\n",
353 + (unsigned int)bm->capacity);
358 +static int __devinit n810bm_probe(struct platform_device *pdev)
363 + bm = kzalloc(sizeof(*bm), GFP_KERNEL);
367 + platform_set_drvdata(pdev, bm);
368 + spin_lock_init(&bm->lock);
369 + setup_timer(&bm->check_timer, n810bm_check_timer, (unsigned long)bm);
371 + err = n810bm_hw_init(bm);
374 + err = device_create_file(&pdev->dev, &dev_attr_batt_charge);
377 + err = device_create_file(&pdev->dev, &dev_attr_batt_capacity);
379 + goto err_rem_charge;
380 + err = retu_request_irq(RETU_INT_ADCS, n810bm_adc_irq_handler,
381 + (unsigned long)bm, "n810bm");
385 + mod_timer(&bm->check_timer, round_jiffies(jiffies + N810BM_CHECK_INTERVAL));
387 + dev_info(&pdev->dev, "Battery management initialized");
392 + device_remove_file(&pdev->dev, &dev_attr_batt_capacity);
394 + device_remove_file(&pdev->dev, &dev_attr_batt_charge);
396 + n810bm_hw_exit(bm);
399 + platform_set_drvdata(pdev, NULL);
403 +static int __devexit n810bm_remove(struct platform_device *pdev)
405 + struct n810bm *bm = platform_get_drvdata(pdev);
407 + retu_free_irq(RETU_INT_ADCS);
408 + del_timer_sync(&bm->check_timer);
409 + device_remove_file(&pdev->dev, &dev_attr_batt_capacity);
410 + device_remove_file(&pdev->dev, &dev_attr_batt_charge);
411 + n810bm_hw_exit(bm);
414 + platform_set_drvdata(pdev, NULL);
419 +static struct platform_driver n810bm_driver = {
420 + .remove = __devexit_p(n810bm_remove),
426 +static int __init n810bm_modinit(void)
428 + return platform_driver_probe(&n810bm_driver, n810bm_probe);
430 +module_init(n810bm_modinit);
432 +static void __exit n810bm_modexit(void)
434 + platform_driver_unregister(&n810bm_driver);
436 +module_exit(n810bm_modexit);
438 +MODULE_DESCRIPTION("Nokia n810 battery management");
439 +MODULE_LICENSE("GPL");
440 +MODULE_AUTHOR("Michael Buesch");
441 --- linux-2.6.36-rc7.orig/drivers/cbus/retu.c
442 +++ linux-2.6.36-rc7/drivers/cbus/retu.c
443 @@ -85,10 +85,10 @@ int retu_read_reg(int reg)
445 * This function writes a value to the specified register
447 -void retu_write_reg(int reg, u16 val)
448 +int retu_write_reg(int reg, u16 val)
450 BUG_ON(!retu_initialized);
451 - cbus_write_reg(cbus_host, RETU_ID, reg, val);
452 + return cbus_write_reg(cbus_host, RETU_ID, reg, val);
455 void retu_set_clear_reg_bits(int reg, u16 set, u16 clear)
456 --- linux-2.6.36-rc7.orig/drivers/cbus/retu.h
457 +++ linux-2.6.36-rc7/drivers/cbus/retu.h
459 #define RETU_REG_CC2 0x0e /* Common control register 2 */
460 #define RETU_REG_CTRL_CLR 0x0f /* Regulator clear register */
461 #define RETU_REG_CTRL_SET 0x10 /* Regulator set register */
462 +#define RETU_REG_UNK1 0x14 /* 0x1000 is set when charger is plugged in */
463 #define RETU_REG_STATUS 0x16 /* Status register */
464 #define RETU_REG_WATCHDOG 0x17 /* Watchdog register */
465 #define RETU_REG_AUDTXR 0x18 /* Audio Codec Tx register */
467 #define MAX_RETU_IRQ_HANDLERS 16
469 int retu_read_reg(int reg);
470 -void retu_write_reg(int reg, u16 val);
471 +int retu_write_reg(int reg, u16 val);
472 void retu_set_clear_reg_bits(int reg, u16 set, u16 clear);
473 int retu_read_adc(int channel);
474 int retu_request_irq(int id, void *irq_handler, unsigned long arg, char *name);
475 --- linux-2.6.36-rc7.orig/arch/arm/mach-omap2/board-n8x0.c
476 +++ linux-2.6.36-rc7/arch/arm/mach-omap2/board-n8x0.c
477 @@ -920,6 +920,17 @@ static int __init n810_tlv320aic3x_regis
479 arch_initcall(n810_tlv320aic3x_register);
481 +static struct platform_device n810_bm_device = {
486 +static void __init n810_bm_init(void)
488 + if (platform_device_register(&n810_bm_device))
492 static void __init n8x0_init_machine(void)
494 omap2420_mux_init(board_mux, OMAP_PACKAGE_ZAC);
495 @@ -947,6 +958,8 @@ static void __init n8x0_init_machine(voi
503 MACHINE_START(NOKIA_N800, "Nokia N800")
505 +++ linux-2.6.36-rc7/drivers/cbus/lipocharge.c
508 + * Generic LIPO battery charger
510 + * Copyright (c) 2010 Michael Buesch <mb@bu3sch.de>
512 + * This program is free software; you can redistribute it and/or
513 + * modify it under the terms of the GNU General Public License
514 + * as published by the Free Software Foundation; either version 2
515 + * of the License, or (at your option) any later version.
517 + * This program is distributed in the hope that it will be useful,
518 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
519 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
520 + * GNU General Public License for more details.
523 +#include "lipocharge.h"
525 +#include <linux/slab.h>
528 +static void lipocharge_timer(unsigned long data)
530 + struct lipocharge *c = (struct lipocharge *)data;
532 + spin_lock(&c->lock);
534 + spin_unlock(&c->lock);
537 +struct lipocharge * lipocharge_alloc(gfp_t gfp)
539 + struct lipocharge *c;
541 + c = kzalloc(sizeof(*c), gfp);
544 + spin_lock_init(&c->lock);
545 + setup_timer(&c->timer, lipocharge_timer, (unsigned long)c);
550 +void lipocharge_free(struct lipocharge *c)
555 +int lipocharge_start(struct lipocharge *c)
557 + if (!c->set_current || !c->get_voltage ||
558 + !c->finished || !c->emergency)
560 + if (!c->top_voltage || c->top_voltage > 4200)
565 +void lipocharge_stop(struct lipocharge *c)
567 + del_timer_sync(&c->timer);
571 +++ linux-2.6.36-rc7/drivers/cbus/lipocharge.h
573 +#ifndef LIPOCHARGE_H_
574 +#define LIPOCHARGE_H_
576 +#include <linux/timer.h>
577 +#include <linux/spinlock.h>
580 +#define LIPORATE(a,b) (((a) * 1000) + (b))
581 +#define LIPORATE_1C LIPORATE(1,0) /* 1C */
582 +#define LIPORATE_p8C LIPORATE(0,8) /* 0.8C */
584 +/** struct lipocharge - A generic LIPO charger
586 + * @capacity: Battery capacity in mAh.
587 + * @rate: Charge rate.
588 + * @top_voltage: Fully charged voltage, in mV.
590 + * @set_current: Set the charge current, in mA.
591 + * @get_voltage: Get the battery voltage, in mV.
593 + * @emergency: Something went wrong. Force shutdown.
595 + * @priv: opaque pointer.
599 + unsigned int capacity;
601 + unsigned int top_voltage;
603 + int (*set_current)(struct lipocharge *c, unsigned int ma);
604 + int (*get_voltage)(struct lipocharge *c, unsigned int *mv);
606 + void (*finished)(struct lipocharge *c);
607 + void (*emergency)(struct lipocharge *c);
613 + struct timer_list timer;
616 +struct lipocharge * lipocharge_alloc(gfp_t gfp);
617 +void lipocharge_free(struct lipocharge *c);
619 +int lipocharge_start(struct lipocharge *c);
620 +void lipocharge_stop(struct lipocharge *c);
622 +#endif /* LIPOCHARGE_H_ */
623 --- linux-2.6.36-rc7.orig/drivers/cbus/tahvo.h
624 +++ linux-2.6.36-rc7/drivers/cbus/tahvo.h
626 #define TAHVO_REG_IDR 0x01 /* Interrupt ID */
627 #define TAHVO_REG_IDSR 0x02 /* Interrupt status */
628 #define TAHVO_REG_IMR 0x03 /* Interrupt mask */
629 +#define TAHVO_REG_CHGCURR 0x04 /* Charge current control (8-bit) */
630 #define TAHVO_REG_LEDPWMR 0x05 /* LED PWM */
631 #define TAHVO_REG_USBR 0x06 /* USB control */
632 +#define TAHVO_REG_CHGCTL 0x08 /* Charge control register */
633 +#define TAHVO_REG_CHGCTL_EN 0x0001 /* Global charge enable */
634 +#define TAHVO_REG_CHGCTL2 0x0c /* Charge control register 2 */
635 +#define TAHVO_REG_BATCURR 0x0d /* Battery (dis)charge current (signed 16-bit) */
637 #define TAHVO_REG_MAX 0x0d
639 /* Interrupt sources */