2 arch/arm/mach-omap2/board-n8x0.c | 13 +
3 drivers/cbus/Kconfig | 12 +
4 drivers/cbus/Makefile | 1
5 drivers/cbus/n810bm.c | 396 +++++++++++++++++++++++++++++++++++++++
6 drivers/cbus/retu.c | 4
7 drivers/cbus/retu.h | 2
8 6 files changed, 425 insertions(+), 3 deletions(-)
10 --- linux-2.6.35.3.orig/drivers/cbus/Kconfig
11 +++ linux-2.6.35.3/drivers/cbus/Kconfig
12 @@ -94,4 +94,16 @@ config CBUS_RETU_HEADSET
13 to Retu/Vilma. Detection state and events are exposed through
17 + depends on CBUS_RETU && CBUS_TAHVO
18 + tristate "Nokia n810 battery management"
20 + Nokia n810 device battery management.
22 + WARNING: This driver is based on reverse engineered information.
23 + It is possibly dangerous to use this software.
24 + Use this software at your own risk!
29 --- linux-2.6.35.3.orig/drivers/cbus/Makefile
30 +++ linux-2.6.35.3/drivers/cbus/Makefile
31 @@ -12,3 +12,4 @@ obj-$(CONFIG_CBUS_RETU_WDT) += retu-wdt.
32 obj-$(CONFIG_CBUS_TAHVO_USER) += tahvo-user.o
33 obj-$(CONFIG_CBUS_RETU_USER) += retu-user.o
34 obj-$(CONFIG_CBUS_RETU_HEADSET) += retu-headset.o
35 +obj-$(CONFIG_N810BM) += n810bm.o
37 +++ linux-2.6.35.3/drivers/cbus/n810bm.c
40 + * Nokia n810 battery management
42 + * WARNING: This driver is based on reverse engineered information.
43 + * It is possibly dangerous to use this software.
44 + * Use this software at your own risk!
46 + * Copyright (c) 2010 Michael Buesch <mb@bu3sch.de>
48 + * This program is free software; you can redistribute it and/or
49 + * modify it under the terms of the GNU General Public License
50 + * as published by the Free Software Foundation; either version 2
51 + * of the License, or (at your option) any later version.
53 + * This program is distributed in the hope that it will be useful,
54 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
55 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
56 + * GNU General Public License for more details.
59 +#include <linux/module.h>
60 +#include <linux/device.h>
61 +#include <linux/platform_device.h>
62 +#include <linux/slab.h>
63 +#include <linux/spinlock.h>
64 +#include <linux/timer.h>
65 +#include <linux/reboot.h>
71 +#define N810BM_CHECK_INTERVAL (HZ * 5)
72 +#define N810BM_MIN_VOLTAGE_THRES 3400 /* Absolute minimum voltage threshold */
73 +/* FIXME: Not sure about the actual value of the low threshold, yet.
74 + * We must give userspace a chance to detect undervoltage and perform
75 + * a clean shutdown, before we flip the big switch. */
78 +/* Battery related retu ADC channels */
79 +#define RETU_ADC_BSI 0x01 /* Battery Size Indicator */
80 +#define RETU_ADC_BATTVOLT 0x08 /* Battery voltage measurement */
83 + * The battery size indicator ADC measures the resistance between
84 + * the battery BSI pin and ground. This is used to detect the battery
85 + * capacity, as the BSI resistor is related to capacity.
87 + * Manually measured lookup table.
88 + * Hard to measure, thus not very accurate.
90 + * Resistance | ADC value
91 + * ========================
99 + * Manually measured lookup table.
100 + * Hard to measure, thus not very accurate.
102 + * Voltage | ADC value
103 + * =====================
122 +enum n810bm_capacity {
123 + N810BM_CAP_UNKNOWN = 0,
124 + N810BM_CAP_1500MAH = 1500, /* 1500 mAh battery */
128 + struct platform_device *pdev;
129 + enum n810bm_capacity capacity;
130 + struct timer_list check_timer;
135 +static NORET_TYPE void n810bm_emergency(struct n810bm *bm, const char *message) ATTRIB_NORET;
136 +static void n810bm_emergency(struct n810bm *bm, const char *message)
138 + printk(KERN_EMERG "Nokia n810 battery management fatal fault: %s\n", message);
139 + /* Force a hard shutdown. */
140 + machine_power_off();
141 + panic("n810bm: Failed to halt machine in emergency state\n");
145 +static u16 retu_read(struct n810bm *bm, unsigned int reg)
148 + unsigned long flags;
150 + spin_lock_irqsave(&retu_lock, flags);
151 + ret = retu_read_reg(reg);
152 + spin_unlock_irqrestore(&retu_lock, flags);
153 + if (ret < 0 || ret > 0xFFFF)
154 + n810bm_emergency(bm, "retu_read");
160 +static void retu_maskset(struct n810bm *bm, unsigned int reg, u16 mask, u16 set)
163 + unsigned long flags;
166 + spin_lock_irqsave(&retu_lock, flags);
168 + ret = retu_read_reg(reg);
169 + if (ret < 0 || ret > 0xFFFF)
176 + ret = retu_write_reg(reg, value);
179 + spin_unlock_irqrestore(&retu_lock, flags);
184 + spin_unlock_irqrestore(&retu_lock, flags);
185 + n810bm_emergency(bm, "retu_maskset");
188 +static inline void retu_write(struct n810bm *bm, unsigned int reg, u16 value)
190 + return retu_maskset(bm, reg, 0xFFFF, value);
193 +static int retu_adc_average(struct n810bm *bm, unsigned int chan,
194 + unsigned int nr_passes)
196 + unsigned int i, value = 0;
199 + if (WARN_ON(!nr_passes))
201 + for (i = 0; i < nr_passes; i++) {
202 + ret = retu_read_adc(chan);
207 + value /= nr_passes;
212 +/* Measure the battery voltage. Returns the value in mV (or negative value on error). */
213 +static int n810bm_measure_batt_voltage(struct n810bm *bm)
217 + const unsigned int scale = 1000;
219 + adc = retu_adc_average(bm, RETU_ADC_BATTVOLT, 5);
224 + mv = 2800 + ((adc - 0x37) * (((4200 - 2800) * scale) / (0x236 - 0x37))) / scale;
229 +/* Read the battery capacity via BSI pin. */
230 +static enum n810bm_capacity n810bm_read_batt_capacity(struct n810bm *bm)
233 + const unsigned int hyst = 16;
235 + adc = retu_adc_average(bm, RETU_ADC_BSI, 5);
237 + return N810BM_CAP_UNKNOWN;
239 + if (adc >= 0x3AC - hyst && adc <= 0x3AC + hyst)
240 + return N810BM_CAP_1500MAH;
242 + return N810BM_CAP_UNKNOWN;
250 +/* Convert a battery voltage (in mV) to percentage. */
251 +static unsigned int n810bm_mvolt2percent(unsigned int mv)
253 + /* FIXME: Create a correct table. */
254 + static const struct mv2p table[] = {
255 + { .mv = 4200, .p = 100, },
256 + { .mv = 4150, .p = 90, },
257 + { .mv = 4100, .p = 80, },
258 + { .mv = 4050, .p = 70, },
259 + { .mv = 4000, .p = 60, },
260 + { .mv = 3950, .p = 50, },
261 + { .mv = 3900, .p = 40, },
262 + { .mv = 3850, .p = 30, },
263 + { .mv = 3800, .p = 20, },
264 + { .mv = 3750, .p = 10, },
265 + { .mv = 3700, .p = 0, },
267 + const struct mv2p *e;
270 + for (i = 0; i < ARRAY_SIZE(table); i++) {
279 +static void n810bm_check_timer(unsigned long data)
281 + struct n810bm *bm = (struct n810bm *)data;
282 + unsigned long flags;
285 + spin_lock_irqsave(&bm->lock, flags);
287 + mv = n810bm_measure_batt_voltage(bm);
289 + n810bm_emergency(bm, "check timer: Failed to measure");
290 + if (mv < N810BM_MIN_VOLTAGE_THRES)
291 + n810bm_emergency(bm, "check timer: Minimum voltage threshold reached");
293 + mod_timer(&bm->check_timer, round_jiffies(jiffies + N810BM_CHECK_INTERVAL));
294 + spin_unlock_irqrestore(&bm->lock, flags);
299 +static ssize_t n810bm_attr_charge_show(struct device *dev,
300 + struct device_attribute *attr,
303 + struct platform_device *pdev = to_platform_device(dev);
304 + struct n810bm *bm = platform_get_drvdata(pdev);
309 + spin_lock_irq(&bm->lock);
310 + millivolt = n810bm_measure_batt_voltage(bm);
311 + if (millivolt >= 0) {
312 + count = snprintf(buf, PAGE_SIZE, "%u\n",
313 + n810bm_mvolt2percent(millivolt));
316 + spin_unlock_irq(&bm->lock);
318 + return err ? err : count;
320 +static DEVICE_ATTR(batt_charge, 0444, n810bm_attr_charge_show, NULL);
322 +static ssize_t n810bm_attr_capacity_show(struct device *dev,
323 + struct device_attribute *attr,
326 + struct platform_device *pdev = to_platform_device(dev);
327 + struct n810bm *bm = platform_get_drvdata(pdev);
330 + spin_lock_irq(&bm->lock);
331 + count = snprintf(buf, PAGE_SIZE, "%u\n",
332 + (unsigned int)bm->capacity);
333 + spin_unlock_irq(&bm->lock);
337 +static DEVICE_ATTR(batt_capacity, 0444, n810bm_attr_capacity_show, NULL);
339 +static void n810bm_hw_exit(struct n810bm *bm)
341 + retu_write(bm, RETU_REG_ADCSCR, 0);
344 +static int n810bm_hw_init(struct n810bm *bm)
346 + retu_write(bm, RETU_REG_ADCSCR, 0);
348 + bm->capacity = n810bm_read_batt_capacity(bm);
349 + if (bm->capacity == N810BM_CAP_UNKNOWN) {
350 + dev_err(&bm->pdev->dev, "Unknown battery detected");
353 + dev_info(&bm->pdev->dev, "Detected %u mAh battery\n",
354 + (unsigned int)bm->capacity);
359 +static int __devinit n810bm_probe(struct platform_device *pdev)
364 + bm = kzalloc(sizeof(*bm), GFP_KERNEL);
368 + platform_set_drvdata(pdev, bm);
369 + spin_lock_init(&bm->lock);
370 + setup_timer(&bm->check_timer, n810bm_check_timer, (unsigned long)bm);
372 + err = n810bm_hw_init(bm);
375 + err = device_create_file(&pdev->dev, &dev_attr_batt_charge);
378 + err = device_create_file(&pdev->dev, &dev_attr_batt_capacity);
380 + goto err_rem_charge;
382 + mod_timer(&bm->check_timer, round_jiffies(jiffies + N810BM_CHECK_INTERVAL));
384 + dev_info(&pdev->dev, "Battery management initialized");
389 + device_remove_file(&pdev->dev, &dev_attr_batt_charge);
391 + n810bm_hw_exit(bm);
394 + platform_set_drvdata(pdev, NULL);
398 +static int __devexit n810bm_remove(struct platform_device *pdev)
400 + struct n810bm *bm = platform_get_drvdata(pdev);
402 + del_timer_sync(&bm->check_timer);
403 + device_remove_file(&pdev->dev, &dev_attr_batt_capacity);
404 + device_remove_file(&pdev->dev, &dev_attr_batt_charge);
405 + n810bm_hw_exit(bm);
408 + platform_set_drvdata(pdev, NULL);
413 +static struct platform_driver n810bm_driver = {
414 + .remove = __devexit_p(n810bm_remove),
420 +static int __init n810bm_modinit(void)
422 + return platform_driver_probe(&n810bm_driver, n810bm_probe);
424 +module_init(n810bm_modinit);
426 +static void __exit n810bm_modexit(void)
428 + platform_driver_unregister(&n810bm_driver);
430 +module_exit(n810bm_modexit);
432 +MODULE_DESCRIPTION("Nokia n810 battery management");
433 +MODULE_LICENSE("GPL");
434 +MODULE_AUTHOR("Michael Buesch");
435 --- linux-2.6.35.3.orig/drivers/cbus/retu.c
436 +++ linux-2.6.35.3/drivers/cbus/retu.c
437 @@ -85,10 +85,10 @@ int retu_read_reg(int reg)
439 * This function writes a value to the specified register
441 -void retu_write_reg(int reg, u16 val)
442 +int retu_write_reg(int reg, u16 val)
444 BUG_ON(!retu_initialized);
445 - cbus_write_reg(cbus_host, RETU_ID, reg, val);
446 + return cbus_write_reg(cbus_host, RETU_ID, reg, val);
449 void retu_set_clear_reg_bits(int reg, u16 set, u16 clear)
450 --- linux-2.6.35.3.orig/drivers/cbus/retu.h
451 +++ linux-2.6.35.3/drivers/cbus/retu.h
453 #define MAX_RETU_IRQ_HANDLERS 16
455 int retu_read_reg(int reg);
456 -void retu_write_reg(int reg, u16 val);
457 +int retu_write_reg(int reg, u16 val);
458 void retu_set_clear_reg_bits(int reg, u16 set, u16 clear);
459 int retu_read_adc(int channel);
460 int retu_request_irq(int id, void *irq_handler, unsigned long arg, char *name);
461 --- linux-2.6.35.3.orig/arch/arm/mach-omap2/board-n8x0.c
462 +++ linux-2.6.35.3/arch/arm/mach-omap2/board-n8x0.c
463 @@ -875,6 +875,17 @@ extern void n8x0_blizzard_init(void);
464 #define n8x0_blizzard_init() 0
467 +static struct platform_device n810_bm_device = {
472 +static void __init n810_bm_init(void)
474 + if (platform_device_register(&n810_bm_device))
478 static void __init n8x0_init_machine(void)
480 platform_device_register(&n8x0_cbus_device);
481 @@ -901,6 +912,8 @@ static void __init n8x0_init_machine(voi
489 MACHINE_START(NOKIA_N800, "Nokia N800")