linux/ar71xx: (2.6.36) add a missed patch
[openwrt.git] / target / linux / omap24xx / patches-2.6.35 / 900-n810-battery-management.patch
1 ---
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(-)
9
10 --- linux-2.6.35.4.orig/drivers/cbus/Kconfig
11 +++ linux-2.6.35.4/drivers/cbus/Kconfig
12 @@ -94,4 +94,16 @@ config CBUS_RETU_HEADSET
13 to Retu/Vilma. Detection state and events are exposed through
14 sysfs.
15
16 +config N810BM
17 + depends on CBUS_RETU && CBUS_TAHVO
18 + tristate "Nokia n810 battery management"
19 + ---help---
20 + Nokia n810 device battery management.
21 +
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!
25 +
26 + If unsure, say N.
27 +
28 endmenu
29 --- linux-2.6.35.4.orig/drivers/cbus/Makefile
30 +++ linux-2.6.35.4/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
36 --- /dev/null
37 +++ linux-2.6.35.4/drivers/cbus/n810bm.c
38 @@ -0,0 +1,396 @@
39 +/*
40 + * Nokia n810 battery management
41 + *
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!
45 + *
46 + * Copyright (c) 2010 Michael Buesch <mb@bu3sch.de>
47 + *
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.
52 + *
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.
57 + */
58 +
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>
66 +
67 +#include "retu.h"
68 +#include "tahvo.h"
69 +
70 +
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. */
76 +
77 +
78 +/* Battery related retu ADC channels */
79 +#define RETU_ADC_BSI 0x01 /* Battery Size Indicator */
80 +#define RETU_ADC_BATTVOLT 0x08 /* Battery voltage measurement */
81 +
82 +/* RETU_ADC_BSI
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.
86 + *
87 + * Manually measured lookup table.
88 + * Hard to measure, thus not very accurate.
89 + *
90 + * Resistance | ADC value
91 + * ========================
92 + * 120k | 0x3AC
93 + * 110k | 0x37C
94 + * 100k | 0x351
95 + * 90k | 0x329
96 + */
97 +
98 +/* RETU_ADC_BATTVOLT
99 + * Manually measured lookup table.
100 + * Hard to measure, thus not very accurate.
101 + *
102 + * Voltage | ADC value
103 + * =====================
104 + * 2.80V | 0x037
105 + * 2.90V | 0x05E
106 + * 3.00V | 0x090
107 + * 3.10V | 0x0A4
108 + * 3.20V | 0x0CC
109 + * 3.30V | 0x0EF
110 + * 3.40V | 0x115
111 + * 3.50V | 0x136
112 + * 3.60V | 0x15C
113 + * 3.70V | 0x187
114 + * 3.80V | 0x1A5
115 + * 3.90V | 0x1C9
116 + * 4.00V | 0x1ED
117 + * 4.10V | 0x212
118 + * 4.20V | 0x236
119 + */
120 +
121 +
122 +enum n810bm_capacity {
123 + N810BM_CAP_UNKNOWN = 0,
124 + N810BM_CAP_1500MAH = 1500, /* 1500 mAh battery */
125 +};
126 +
127 +struct n810bm {
128 + struct platform_device *pdev;
129 + enum n810bm_capacity capacity;
130 + struct timer_list check_timer;
131 + spinlock_t lock;
132 +};
133 +
134 +
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)
137 +{
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");
142 +}
143 +
144 +#if 0
145 +static u16 retu_read(struct n810bm *bm, unsigned int reg)
146 +{
147 + int ret;
148 + unsigned long flags;
149 +
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");
155 +
156 + return ret;
157 +}
158 +#endif
159 +
160 +static void retu_maskset(struct n810bm *bm, unsigned int reg, u16 mask, u16 set)
161 +{
162 + int ret;
163 + unsigned long flags;
164 + u16 value;
165 +
166 + spin_lock_irqsave(&retu_lock, flags);
167 + if (~mask) {
168 + ret = retu_read_reg(reg);
169 + if (ret < 0 || ret > 0xFFFF)
170 + goto fatal_unlock;
171 + value = ret;
172 + } else
173 + value = 0;
174 + value &= ~mask;
175 + value |= set;
176 + ret = retu_write_reg(reg, value);
177 + if (ret)
178 + goto fatal_unlock;
179 + spin_unlock_irqrestore(&retu_lock, flags);
180 +
181 + return;
182 +
183 +fatal_unlock:
184 + spin_unlock_irqrestore(&retu_lock, flags);
185 + n810bm_emergency(bm, "retu_maskset");
186 +}
187 +
188 +static inline void retu_write(struct n810bm *bm, unsigned int reg, u16 value)
189 +{
190 + return retu_maskset(bm, reg, 0xFFFF, value);
191 +}
192 +
193 +static int retu_adc_average(struct n810bm *bm, unsigned int chan,
194 + unsigned int nr_passes)
195 +{
196 + unsigned int i, value = 0;
197 + int ret;
198 +
199 + if (WARN_ON(!nr_passes))
200 + return 0;
201 + for (i = 0; i < nr_passes; i++) {
202 + ret = retu_read_adc(chan);
203 + if (ret < 0)
204 + return ret;
205 + value += ret;
206 + }
207 + value /= nr_passes;
208 +
209 + return value;
210 +}
211 +
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)
214 +{
215 + int adc;
216 + unsigned int mv;
217 + const unsigned int scale = 1000;
218 +
219 + adc = retu_adc_average(bm, RETU_ADC_BATTVOLT, 5);
220 + if (adc < 0)
221 + return adc;
222 + if (adc <= 0x37)
223 + return 2800;
224 + mv = 2800 + ((adc - 0x37) * (((4200 - 2800) * scale) / (0x236 - 0x37))) / scale;
225 +
226 + return mv;
227 +}
228 +
229 +/* Read the battery capacity via BSI pin. */
230 +static enum n810bm_capacity n810bm_read_batt_capacity(struct n810bm *bm)
231 +{
232 + int adc;
233 + const unsigned int hyst = 16;
234 +
235 + adc = retu_adc_average(bm, RETU_ADC_BSI, 5);
236 + if (adc < 0)
237 + return N810BM_CAP_UNKNOWN;
238 +
239 + if (adc >= 0x3AC - hyst && adc <= 0x3AC + hyst)
240 + return N810BM_CAP_1500MAH;
241 +
242 + return N810BM_CAP_UNKNOWN;
243 +}
244 +
245 +struct mv2p {
246 + unsigned int mv;
247 + unsigned int p;
248 +};
249 +
250 +/* Convert a battery voltage (in mV) to percentage. */
251 +static unsigned int n810bm_mvolt2percent(unsigned int mv)
252 +{
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, },
266 + };
267 + const struct mv2p *e;
268 + unsigned int i;
269 +
270 + for (i = 0; i < ARRAY_SIZE(table); i++) {
271 + e = &table[i];
272 + if (mv >= e->mv)
273 + return e->p;
274 + }
275 +
276 + return 0;
277 +}
278 +
279 +static void n810bm_check_timer(unsigned long data)
280 +{
281 + struct n810bm *bm = (struct n810bm *)data;
282 + unsigned long flags;
283 + int mv;
284 +
285 + spin_lock_irqsave(&bm->lock, flags);
286 +
287 + mv = n810bm_measure_batt_voltage(bm);
288 + if (mv < 0)
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");
292 +
293 + mod_timer(&bm->check_timer, round_jiffies(jiffies + N810BM_CHECK_INTERVAL));
294 + spin_unlock_irqrestore(&bm->lock, flags);
295 +
296 + return;
297 +}
298 +
299 +static ssize_t n810bm_attr_charge_show(struct device *dev,
300 + struct device_attribute *attr,
301 + char *buf)
302 +{
303 + struct platform_device *pdev = to_platform_device(dev);
304 + struct n810bm *bm = platform_get_drvdata(pdev);
305 + int err = -ENODEV;
306 + ssize_t count = 0;
307 + int millivolt;
308 +
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));
314 + err = 0;
315 + }
316 + spin_unlock_irq(&bm->lock);
317 +
318 + return err ? err : count;
319 +}
320 +static DEVICE_ATTR(batt_charge, 0444, n810bm_attr_charge_show, NULL);
321 +
322 +static ssize_t n810bm_attr_capacity_show(struct device *dev,
323 + struct device_attribute *attr,
324 + char *buf)
325 +{
326 + struct platform_device *pdev = to_platform_device(dev);
327 + struct n810bm *bm = platform_get_drvdata(pdev);
328 + ssize_t count;
329 +
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);
334 +
335 + return count;
336 +}
337 +static DEVICE_ATTR(batt_capacity, 0444, n810bm_attr_capacity_show, NULL);
338 +
339 +static void n810bm_hw_exit(struct n810bm *bm)
340 +{
341 + retu_write(bm, RETU_REG_ADCSCR, 0);
342 +}
343 +
344 +static int n810bm_hw_init(struct n810bm *bm)
345 +{
346 + retu_write(bm, RETU_REG_ADCSCR, 0);
347 +
348 + bm->capacity = n810bm_read_batt_capacity(bm);
349 + if (bm->capacity == N810BM_CAP_UNKNOWN) {
350 + dev_err(&bm->pdev->dev, "Unknown battery detected");
351 + return -ENODEV;
352 + }
353 + dev_info(&bm->pdev->dev, "Detected %u mAh battery\n",
354 + (unsigned int)bm->capacity);
355 +
356 + return 0;
357 +}
358 +
359 +static int __devinit n810bm_probe(struct platform_device *pdev)
360 +{
361 + struct n810bm *bm;
362 + int err;
363 +
364 + bm = kzalloc(sizeof(*bm), GFP_KERNEL);
365 + if (!bm)
366 + return -ENOMEM;
367 + bm->pdev = pdev;
368 + platform_set_drvdata(pdev, bm);
369 + spin_lock_init(&bm->lock);
370 + setup_timer(&bm->check_timer, n810bm_check_timer, (unsigned long)bm);
371 +
372 + err = n810bm_hw_init(bm);
373 + if (err)
374 + goto err_free;
375 + err = device_create_file(&pdev->dev, &dev_attr_batt_charge);
376 + if (err)
377 + goto err_exit;
378 + err = device_create_file(&pdev->dev, &dev_attr_batt_capacity);
379 + if (err)
380 + goto err_rem_charge;
381 +
382 + mod_timer(&bm->check_timer, round_jiffies(jiffies + N810BM_CHECK_INTERVAL));
383 +
384 + dev_info(&pdev->dev, "Battery management initialized");
385 +
386 + return 0;
387 +
388 +err_rem_charge:
389 + device_remove_file(&pdev->dev, &dev_attr_batt_charge);
390 +err_exit:
391 + n810bm_hw_exit(bm);
392 +err_free:
393 + kfree(bm);
394 + platform_set_drvdata(pdev, NULL);
395 + return err;
396 +}
397 +
398 +static int __devexit n810bm_remove(struct platform_device *pdev)
399 +{
400 + struct n810bm *bm = platform_get_drvdata(pdev);
401 +
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);
406 +
407 + kfree(bm);
408 + platform_set_drvdata(pdev, NULL);
409 +
410 + return 0;
411 +}
412 +
413 +static struct platform_driver n810bm_driver = {
414 + .remove = __devexit_p(n810bm_remove),
415 + .driver = {
416 + .name = "n810bm",
417 + }
418 +};
419 +
420 +static int __init n810bm_modinit(void)
421 +{
422 + return platform_driver_probe(&n810bm_driver, n810bm_probe);
423 +}
424 +module_init(n810bm_modinit);
425 +
426 +static void __exit n810bm_modexit(void)
427 +{
428 + platform_driver_unregister(&n810bm_driver);
429 +}
430 +module_exit(n810bm_modexit);
431 +
432 +MODULE_DESCRIPTION("Nokia n810 battery management");
433 +MODULE_LICENSE("GPL");
434 +MODULE_AUTHOR("Michael Buesch");
435 --- linux-2.6.35.4.orig/drivers/cbus/retu.c
436 +++ linux-2.6.35.4/drivers/cbus/retu.c
437 @@ -85,10 +85,10 @@ int retu_read_reg(int reg)
438 *
439 * This function writes a value to the specified register
440 */
441 -void retu_write_reg(int reg, u16 val)
442 +int retu_write_reg(int reg, u16 val)
443 {
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);
447 }
448
449 void retu_set_clear_reg_bits(int reg, u16 set, u16 clear)
450 --- linux-2.6.35.4.orig/drivers/cbus/retu.h
451 +++ linux-2.6.35.4/drivers/cbus/retu.h
452 @@ -58,7 +58,7 @@
453 #define MAX_RETU_IRQ_HANDLERS 16
454
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.4.orig/arch/arm/mach-omap2/board-n8x0.c
462 +++ linux-2.6.35.4/arch/arm/mach-omap2/board-n8x0.c
463 @@ -781,6 +781,17 @@ extern void n8x0_blizzard_init(void);
464
465 extern void n8x0_usb_init(void);
466
467 +static struct platform_device n810_bm_device = {
468 + .name = "n810bm",
469 + .id = -1,
470 +};
471 +
472 +static void __init n810_bm_init(void)
473 +{
474 + if (platform_device_register(&n810_bm_device))
475 + BUG();
476 +}
477 +
478 static void __init n8x0_init_machine(void)
479 {
480 platform_device_register(&n8x0_cbus_device);
481 @@ -807,6 +818,8 @@ static void __init n8x0_init_machine(voi
482 n8x0_onenand_init();
483 n8x0_mmc_init();
484 n8x0_usb_init();
485 +
486 + n810_bm_init();
487 }
488
489 MACHINE_START(NOKIA_N800, "Nokia N800")
This page took 0.080518 seconds and 5 git commands to generate.