summaryrefslogtreecommitdiffstats
path: root/meta/packages/linux/linux-rp-2.6.23/pda-power.patch
diff options
context:
space:
mode:
Diffstat (limited to 'meta/packages/linux/linux-rp-2.6.23/pda-power.patch')
-rw-r--r--meta/packages/linux/linux-rp-2.6.23/pda-power.patch3373
1 files changed, 3373 insertions, 0 deletions
diff --git a/meta/packages/linux/linux-rp-2.6.23/pda-power.patch b/meta/packages/linux/linux-rp-2.6.23/pda-power.patch
new file mode 100644
index 0000000000..face2f4ef2
--- /dev/null
+++ b/meta/packages/linux/linux-rp-2.6.23/pda-power.patch
@@ -0,0 +1,3373 @@
1---
2 arch/arm/Kconfig | 2
3 drivers/Kconfig | 2
4 drivers/Makefile | 1
5 drivers/power/Kconfig | 70 +++++
6 drivers/power/Makefile | 28 ++
7 drivers/power/adc_battery.c | 278 +++++++++++++++++++++
8 drivers/power/apm_power.c | 247 +++++++++++++++++++
9 drivers/power/ds2760_battery.c | 475 +++++++++++++++++++++++++++++++++++++
10 drivers/power/micro_battery.c | 257 ++++++++++++++++++++
11 drivers/power/olpc_battery.c | 302 +++++++++++++++++++++++
12 drivers/power/pda_power.c | 263 ++++++++++++++++++++
13 drivers/power/pmu_battery.c | 215 ++++++++++++++++
14 drivers/power/power_supply.h | 42 +++
15 drivers/power/power_supply_core.c | 168 +++++++++++++
16 drivers/power/power_supply_leds.c | 188 ++++++++++++++
17 drivers/power/power_supply_sysfs.c | 289 ++++++++++++++++++++++
18 drivers/power/simpad-battery.c | 242 ++++++++++++++++++
19 include/linux/power_supply.h | 175 +++++++++++++
20 18 files changed, 3244 insertions(+)
21
22Index: linux-2.6.22/drivers/power/adc_battery.c
23===================================================================
24--- /dev/null 1970-01-01 00:00:00.000000000 +0000
25+++ linux-2.6.22/drivers/power/adc_battery.c 2007-08-23 12:26:28.000000000 +0200
26@@ -0,0 +1,278 @@
27+/*
28+ * Copyright (c) 2007 Paul Sokolovsky
29+ *
30+ * This program is free software; you can redistribute it and/or modify
31+ * it under the terms of the GNU General Public License as published by
32+ * the Free Software Foundation; either version 2 of the License, or
33+ * (at your option) any later version.
34+ *
35+ */
36+
37+//#define DEBUG
38+
39+#include <linux/module.h>
40+#include <linux/interrupt.h>
41+#include <linux/pm.h>
42+#include <linux/delay.h>
43+#include <linux/workqueue.h>
44+#include <linux/platform_device.h>
45+#include <linux/power_supply.h>
46+#include <linux/adc.h>
47+#include <linux/adc_battery.h>
48+
49+#include <asm/irq.h>
50+
51+#define PIN_NO_VOLT 0
52+#define PIN_NO_CURR 1
53+#define PIN_NO_TEMP 2
54+
55+struct battery_adc_priv {
56+ struct power_supply batt_cdev;
57+
58+ struct battery_adc_platform_data *pdata;
59+
60+ struct adc_request req;
61+ struct adc_sense pins[3];
62+ struct adc_sense last_good_pins[3];
63+
64+ struct workqueue_struct *wq;
65+ struct delayed_work work;
66+};
67+
68+/*
69+ * Battery properties
70+ */
71+
72+static int adc_battery_get_property(struct power_supply *psy,
73+ enum power_supply_property psp,
74+ union power_supply_propval *val)
75+{
76+ struct battery_adc_priv* drvdata = (struct battery_adc_priv*)psy;
77+ int voltage;
78+
79+ switch (psp) {
80+ case POWER_SUPPLY_PROP_STATUS:
81+ val->intval = drvdata->pdata->charge_status;
82+ break;
83+ case POWER_SUPPLY_PROP_VOLTAGE_MAX_DESIGN:
84+ val->intval = drvdata->pdata->battery_info.voltage_max_design;
85+ break;
86+ case POWER_SUPPLY_PROP_VOLTAGE_MIN_DESIGN:
87+ val->intval = drvdata->pdata->battery_info.voltage_min_design;
88+ break;
89+ case POWER_SUPPLY_PROP_CHARGE_FULL_DESIGN:
90+ val->intval = drvdata->pdata->battery_info.charge_full_design;
91+ break;
92+ case POWER_SUPPLY_PROP_CHARGE_EMPTY_DESIGN:
93+ val->intval = drvdata->pdata->battery_info.charge_empty_design;
94+ break;
95+ case POWER_SUPPLY_PROP_VOLTAGE_NOW:
96+ val->intval = drvdata->last_good_pins[PIN_NO_VOLT].value * drvdata->pdata->voltage_mult;
97+ break;
98+ case POWER_SUPPLY_PROP_CURRENT_NOW:
99+ val->intval = drvdata->last_good_pins[PIN_NO_CURR].value * drvdata->pdata->current_mult;
100+ break;
101+ case POWER_SUPPLY_PROP_CHARGE_NOW:
102+ /* We do calculations in mX, not uX, because todo it in uX we should use "long long"s,
103+ * which is a mess (need to use do_div) when you need divide operation). */
104+ voltage = drvdata->last_good_pins[PIN_NO_VOLT].value * drvdata->pdata->voltage_mult;
105+ val->intval = ((voltage/1000 - drvdata->pdata->battery_info.voltage_min_design/1000) *
106+ (drvdata->pdata->battery_info.charge_full_design/1000 -
107+ drvdata->pdata->battery_info.charge_empty_design/1000)) /
108+ (drvdata->pdata->battery_info.voltage_max_design/1000 -
109+ drvdata->pdata->battery_info.voltage_min_design/1000);
110+ val->intval *= 1000; /* convert final result to uX */
111+ break;
112+ case POWER_SUPPLY_PROP_TEMP:
113+ val->intval = drvdata->last_good_pins[PIN_NO_TEMP].value * drvdata->pdata->temperature_mult / 1000;
114+ break;
115+ default:
116+ return -EINVAL;
117+ };
118+ return 0;
119+}
120+
121+/*
122+ * Driver body
123+ */
124+
125+static void adc_battery_query(struct battery_adc_priv *drvdata)
126+{
127+ struct battery_adc_platform_data *pdata = drvdata->pdata;
128+ int powered, charging;
129+
130+ adc_request_sample(&drvdata->req);
131+
132+ powered = power_supply_am_i_supplied(&drvdata->batt_cdev);
133+ charging = pdata->is_charging ? pdata->is_charging() : -1;
134+
135+ if (powered && charging)
136+ pdata->charge_status = POWER_SUPPLY_STATUS_CHARGING;
137+ else if (powered && !charging && charging != -1)
138+ pdata->charge_status = POWER_SUPPLY_STATUS_FULL;
139+ else
140+ pdata->charge_status = POWER_SUPPLY_STATUS_DISCHARGING;
141+
142+ /* Throw away invalid samples, this may happen soon after resume for example. */
143+ if (drvdata->pins[PIN_NO_VOLT].value > 0) {
144+ memcpy(drvdata->last_good_pins, drvdata->pins, sizeof(drvdata->pins));
145+#ifdef DEBUG
146+ printk("%d %d %d\n", drvdata->pins[PIN_NO_VOLT].value,
147+ drvdata->pins[PIN_NO_CURR].value,
148+ drvdata->pins[PIN_NO_TEMP].value);
149+#endif
150+ }
151+}
152+
153+static void adc_battery_charge_power_changed(struct power_supply *bat)
154+{
155+ struct battery_adc_priv *drvdata = (struct battery_adc_priv*)bat;
156+ cancel_delayed_work(&drvdata->work);
157+ queue_delayed_work(drvdata->wq, &drvdata->work, 0);
158+}
159+
160+static void adc_battery_work_func(struct work_struct *work)
161+{
162+ struct delayed_work *delayed_work = container_of(work, struct delayed_work, work);
163+ struct battery_adc_priv *drvdata = container_of(delayed_work, struct battery_adc_priv, work);
164+
165+ adc_battery_query(drvdata);
166+ power_supply_changed(&drvdata->batt_cdev);
167+
168+ queue_delayed_work(drvdata->wq, &drvdata->work, (5000 * HZ) / 1000);
169+}
170+
171+static int adc_battery_probe(struct platform_device *pdev)
172+{
173+ int retval;
174+ struct battery_adc_platform_data *pdata = pdev->dev.platform_data;
175+ struct battery_adc_priv *drvdata;
176+ int i, j;
177+ enum power_supply_property props[] = {
178+ POWER_SUPPLY_PROP_STATUS,
179+ POWER_SUPPLY_PROP_VOLTAGE_MAX_DESIGN,
180+ POWER_SUPPLY_PROP_VOLTAGE_MIN_DESIGN,
181+ POWER_SUPPLY_PROP_VOLTAGE_NOW,
182+ POWER_SUPPLY_PROP_CURRENT_NOW,
183+ POWER_SUPPLY_PROP_CHARGE_NOW,
184+ POWER_SUPPLY_PROP_CHARGE_FULL_DESIGN,
185+ POWER_SUPPLY_PROP_CHARGE_EMPTY_DESIGN,
186+ POWER_SUPPLY_PROP_TEMP,
187+ };
188+
189+ // Initialize ts data structure.
190+ drvdata = kzalloc(sizeof(*drvdata), GFP_KERNEL);
191+ if (!drvdata)
192+ return -ENOMEM;
193+
194+ drvdata->batt_cdev.name = pdata->battery_info.name;
195+ drvdata->batt_cdev.use_for_apm = pdata->battery_info.use_for_apm;
196+ drvdata->batt_cdev.num_properties = ARRAY_SIZE(props);
197+ drvdata->batt_cdev.get_property = adc_battery_get_property;
198+ drvdata->batt_cdev.external_power_changed =
199+ adc_battery_charge_power_changed;
200+
201+ if (!pdata->voltage_pin) {
202+ drvdata->batt_cdev.num_properties--;
203+ props[3] = -1;
204+ }
205+ if (!pdata->current_pin) {
206+ drvdata->batt_cdev.num_properties--;
207+ props[4] = -1;
208+ }
209+ if (!pdata->temperature_pin) {
210+ drvdata->batt_cdev.num_properties--;
211+ props[8] = -1;
212+ }
213+
214+ drvdata->batt_cdev.properties = kmalloc(
215+ sizeof(*drvdata->batt_cdev.properties) *
216+ drvdata->batt_cdev.num_properties, GFP_KERNEL);
217+ if (!drvdata->batt_cdev.properties)
218+ return -ENOMEM;
219+
220+ j = 0;
221+ for (i = 0; i < ARRAY_SIZE(props); i++) {
222+ if (props[i] == -1)
223+ continue;
224+ drvdata->batt_cdev.properties[j++] = props[i];
225+ }
226+
227+ retval = power_supply_register(&pdev->dev, &drvdata->batt_cdev);
228+ if (retval) {
229+ printk("adc-battery: Error registering battery classdev");
230+ return retval;
231+ }
232+
233+ drvdata->req.senses = drvdata->pins;
234+ drvdata->req.num_senses = ARRAY_SIZE(drvdata->pins);
235+ drvdata->pins[PIN_NO_VOLT].name = pdata->voltage_pin;
236+ drvdata->pins[PIN_NO_CURR].name = pdata->current_pin;
237+ drvdata->pins[PIN_NO_TEMP].name = pdata->temperature_pin;
238+
239+ adc_request_register(&drvdata->req);
240+
241+ /* Here we assume raw values in mV */
242+ if (!pdata->voltage_mult)
243+ pdata->voltage_mult = 1000;
244+ /* Here we assume raw values in mA */
245+ if (!pdata->current_mult)
246+ pdata->current_mult = 1000;
247+ /* Here we assume raw values in 1/10 C */
248+ if (!pdata->temperature_mult)
249+ pdata->temperature_mult = 1000;
250+
251+ drvdata->pdata = pdata;
252+ pdata->drvdata = drvdata; /* Seems ugly, we need better solution */
253+
254+ platform_set_drvdata(pdev, drvdata);
255+
256+ // Load initial values ASAP
257+ adc_battery_query(drvdata);
258+
259+ // Still schedule next sampling soon
260+ INIT_DELAYED_WORK(&drvdata->work, adc_battery_work_func);
261+ drvdata->wq = create_workqueue(pdev->dev.bus_id);
262+ if (!drvdata->wq)
263+ return -ESRCH;
264+
265+ queue_delayed_work(drvdata->wq, &drvdata->work, (5000 * HZ) / 1000);
266+
267+ return retval;
268+}
269+
270+static int adc_battery_remove(struct platform_device *pdev)
271+{
272+ struct battery_adc_priv *drvdata = platform_get_drvdata(pdev);
273+ cancel_delayed_work(&drvdata->work);
274+ destroy_workqueue(drvdata->wq);
275+ power_supply_unregister(&drvdata->batt_cdev);
276+ adc_request_unregister(&drvdata->req);
277+ kfree(drvdata->batt_cdev.properties);
278+ return 0;
279+}
280+
281+static struct platform_driver adc_battery_driver = {
282+ .driver = {
283+ .name = "adc-battery",
284+ },
285+ .probe = adc_battery_probe,
286+ .remove = adc_battery_remove,
287+};
288+
289+static int __init adc_battery_init(void)
290+{
291+ return platform_driver_register(&adc_battery_driver);
292+}
293+
294+static void __exit adc_battery_exit(void)
295+{
296+ platform_driver_unregister(&adc_battery_driver);
297+}
298+
299+module_init(adc_battery_init)
300+module_exit(adc_battery_exit)
301+
302+MODULE_AUTHOR("Paul Sokolovsky");
303+MODULE_DESCRIPTION("Battery driver for ADC device");
304+MODULE_LICENSE("GPL");
305Index: linux-2.6.22/drivers/power/apm_power.c
306===================================================================
307--- /dev/null 1970-01-01 00:00:00.000000000 +0000
308+++ linux-2.6.22/drivers/power/apm_power.c 2007-08-23 12:13:52.000000000 +0200
309@@ -0,0 +1,247 @@
310+/*
311+ * Copyright (c) 2007 Anton Vorontsov <cbou@mail.ru>
312+ * Copyright (c) 2007 Eugeny Boger <eugenyboger@dgap.mipt.ru>
313+ *
314+ * Author: Eugeny Boger <eugenyboger@dgap.mipt.ru>
315+ *
316+ * Use consistent with the GNU GPL is permitted,
317+ * provided that this copyright notice is
318+ * preserved in its entirety in all copies and derived works.
319+ */
320+
321+#include <linux/module.h>
322+#include <linux/power_supply.h>
323+#include <linux/apm-emulation.h>
324+
325+#define PSY_PROP(psy, prop, val) psy->get_property(psy, \
326+ POWER_SUPPLY_PROP_##prop, val)
327+
328+#define _MPSY_PROP(prop, val) main_battery->get_property(main_battery, \
329+ prop, val)
330+
331+#define MPSY_PROP(prop, val) _MPSY_PROP(POWER_SUPPLY_PROP_##prop, val)
332+
333+static struct power_supply *main_battery;
334+
335+static void find_main_battery(void)
336+{
337+ struct device *dev;
338+ struct power_supply *bat, *batm;
339+ union power_supply_propval full;
340+ int max_charge = 0;
341+
342+ main_battery = NULL;
343+ batm = NULL;
344+ list_for_each_entry(dev, &power_supply_class->devices, node) {
345+ bat = dev_get_drvdata(dev);
346+ /* If none of battery devices cantains 'use_for_apm' flag,
347+ choice one with maximum design charge */
348+ if (!PSY_PROP(bat, CHARGE_FULL_DESIGN, &full)) {
349+ if (full.intval > max_charge) {
350+ batm = bat;
351+ max_charge = full.intval;
352+ }
353+ }
354+
355+ if (bat->use_for_apm)
356+ main_battery = bat;
357+ }
358+ if (!main_battery)
359+ main_battery = batm;
360+
361+ return;
362+}
363+
364+static int calculate_time(int status)
365+{
366+ union power_supply_propval charge_full, charge_empty;
367+ union power_supply_propval charge, I;
368+
369+ if (MPSY_PROP(CHARGE_FULL, &charge_full)) {
370+ /* if battery can't report this property, use design value */
371+ if (MPSY_PROP(CHARGE_FULL_DESIGN, &charge_full))
372+ return -1;
373+ }
374+
375+ if (MPSY_PROP(CHARGE_EMPTY, &charge_empty)) {
376+ /* if battery can't report this property, use design value */
377+ if (MPSY_PROP(CHARGE_EMPTY_DESIGN, &charge_empty))
378+ charge_empty.intval = 0;
379+ }
380+
381+ if (MPSY_PROP(CHARGE_AVG, &charge)) {
382+ /* if battery can't report average value, use momentary */
383+ if (MPSY_PROP(CHARGE_NOW, &charge))
384+ return -1;
385+ }
386+
387+ if (MPSY_PROP(CURRENT_AVG, &I)) {
388+ /* if battery can't report average value, use momentary */
389+ if (MPSY_PROP(CURRENT_NOW, &I))
390+ return -1;
391+ }
392+
393+ if (I.intval == 0)
394+ return 0;
395+ else if (status == POWER_SUPPLY_STATUS_CHARGING)
396+ return ((charge.intval - charge_full.intval) * 60L) /
397+ I.intval;
398+ else
399+ return -((charge.intval - charge_empty.intval) * 60L) /
400+ I.intval;
401+}
402+
403+static int calculate_capacity(int using_charge)
404+{
405+ enum power_supply_property full_prop, empty_prop;
406+ enum power_supply_property full_design_prop, empty_design_prop;
407+ enum power_supply_property now_prop, avg_prop;
408+ union power_supply_propval empty, full, cur;
409+ int ret;
410+
411+ if (using_charge) {
412+ full_prop = POWER_SUPPLY_PROP_CHARGE_FULL;
413+ empty_prop = POWER_SUPPLY_PROP_CHARGE_EMPTY;
414+ full_design_prop = POWER_SUPPLY_PROP_CHARGE_FULL_DESIGN;
415+ empty_design_prop = POWER_SUPPLY_PROP_CHARGE_EMPTY_DESIGN;
416+ now_prop = POWER_SUPPLY_PROP_CHARGE_NOW;
417+ avg_prop = POWER_SUPPLY_PROP_CHARGE_AVG;
418+ }
419+ else {
420+ full_prop = POWER_SUPPLY_PROP_ENERGY_FULL;
421+ empty_prop = POWER_SUPPLY_PROP_ENERGY_EMPTY;
422+ full_design_prop = POWER_SUPPLY_PROP_ENERGY_FULL_DESIGN;
423+ empty_design_prop = POWER_SUPPLY_PROP_ENERGY_EMPTY_DESIGN;
424+ now_prop = POWER_SUPPLY_PROP_ENERGY_NOW;
425+ avg_prop = POWER_SUPPLY_PROP_ENERGY_AVG;
426+ }
427+
428+ if (_MPSY_PROP(full_prop, &full)) {
429+ /* if battery can't report this property, use design value */
430+ if (_MPSY_PROP(full_design_prop, &full))
431+ return -1;
432+ }
433+
434+ if (_MPSY_PROP(avg_prop, &cur)) {
435+ /* if battery can't report average value, use momentary */
436+ if (_MPSY_PROP(now_prop, &cur))
437+ return -1;
438+ }
439+
440+ if (_MPSY_PROP(empty_prop, &empty)) {
441+ /* if battery can't report this property, use design value */
442+ if (_MPSY_PROP(empty_design_prop, &empty))
443+ empty.intval = 0;
444+ }
445+
446+ if (full.intval - empty.intval)
447+ ret = ((cur.intval - empty.intval) * 100L) /
448+ (full.intval - empty.intval);
449+ else
450+ return -1;
451+
452+ if (ret > 100)
453+ return 100;
454+ else if (ret < 0)
455+ return 0;
456+
457+ return ret;
458+}
459+
460+static void apm_battery_apm_get_power_status(struct apm_power_info *info)
461+{
462+ union power_supply_propval status;
463+ union power_supply_propval capacity, time_to_full, time_to_empty;
464+
465+ down(&power_supply_class->sem);
466+ find_main_battery();
467+ if (!main_battery) {
468+ up(&power_supply_class->sem);
469+ return;
470+ }
471+
472+ /* status */
473+
474+ if (MPSY_PROP(STATUS, &status))
475+ status.intval = POWER_SUPPLY_STATUS_UNKNOWN;
476+
477+ /* ac line status */
478+
479+ if ((status.intval == POWER_SUPPLY_STATUS_CHARGING) ||
480+ (status.intval == POWER_SUPPLY_STATUS_NOT_CHARGING) ||
481+ (status.intval == POWER_SUPPLY_STATUS_FULL))
482+ info->ac_line_status = APM_AC_ONLINE;
483+ else
484+ info->ac_line_status = APM_AC_OFFLINE;
485+
486+ /* battery life (i.e. capacity, in percents) */
487+
488+ if (MPSY_PROP(CAPACITY, &capacity) == 0)
489+ info->battery_life = capacity.intval;
490+ else {
491+ /* try calculate using energy */
492+ info->battery_life = calculate_capacity(0);
493+ /* if failed try calculate using charge instead */
494+ if (info->battery_life == -1)
495+ info->battery_life = calculate_capacity(1);
496+ }
497+
498+ /* charging status */
499+
500+ if (status.intval == POWER_SUPPLY_STATUS_CHARGING)
501+ info->battery_status = APM_BATTERY_STATUS_CHARGING;
502+ else {
503+ if (info->battery_life > 50)
504+ info->battery_status = APM_BATTERY_STATUS_HIGH;
505+ else if (info->battery_life > 5)
506+ info->battery_status = APM_BATTERY_STATUS_LOW;
507+ else
508+ info->battery_status = APM_BATTERY_STATUS_CRITICAL;
509+ }
510+ info->battery_flag = info->battery_status;
511+
512+ /* time */
513+
514+ info->units = APM_UNITS_MINS;
515+
516+ if (status.intval == POWER_SUPPLY_STATUS_CHARGING) {
517+ if (MPSY_PROP(TIME_TO_FULL_AVG, &time_to_full)) {
518+ if (MPSY_PROP(TIME_TO_FULL_NOW, &time_to_full))
519+ info->time = calculate_time(status.intval);
520+ else
521+ info->time = time_to_full.intval / 60;
522+ }
523+ }
524+ else {
525+ if (MPSY_PROP(TIME_TO_EMPTY_AVG, &time_to_empty)) {
526+ if (MPSY_PROP(TIME_TO_EMPTY_NOW, &time_to_empty))
527+ info->time = calculate_time(status.intval);
528+ else
529+ info->time = time_to_empty.intval / 60;
530+ }
531+ }
532+
533+ up(&power_supply_class->sem);
534+ return;
535+}
536+
537+static int __init apm_battery_init(void)
538+{
539+ printk(KERN_INFO "APM Battery Driver\n");
540+
541+ apm_get_power_status = apm_battery_apm_get_power_status;
542+ return 0;
543+}
544+
545+static void __exit apm_battery_exit(void)
546+{
547+ apm_get_power_status = NULL;
548+ return;
549+}
550+
551+module_init(apm_battery_init);
552+module_exit(apm_battery_exit);
553+
554+MODULE_AUTHOR("Eugeny Boger <eugenyboger@dgap.mipt.ru>");
555+MODULE_DESCRIPTION("APM emulation driver for battery monitoring class");
556+MODULE_LICENSE("GPL");
557Index: linux-2.6.22/drivers/power/ds2760_battery.c
558===================================================================
559--- /dev/null 1970-01-01 00:00:00.000000000 +0000
560+++ linux-2.6.22/drivers/power/ds2760_battery.c 2007-08-23 12:13:52.000000000 +0200
561@@ -0,0 +1,475 @@
562+/*
563+ * Driver for batteries with DS2760 chips inside.
564+ *
565+ * Copyright (c) 2007 Anton Vorontsov
566+ * 2004-2007 Matt Reimer
567+ * 2004 Szabolcs Gyurko
568+ *
569+ * Use consistent with the GNU GPL is permitted,
570+ * provided that this copyright notice is
571+ * preserved in its entirety in all copies and derived works.
572+ *
573+ * Author: Anton Vorontsov <cbou@mail.ru>
574+ * February 2007
575+ *
576+ * Matt Reimer <mreimer@vpop.net>
577+ * April 2004, 2005, 2007
578+ *
579+ * Szabolcs Gyurko <szabolcs.gyurko@tlt.hu>
580+ * September 2004
581+ */
582+
583+#include <linux/module.h>
584+#include <linux/param.h>
585+#include <linux/jiffies.h>
586+#include <linux/workqueue.h>
587+#include <linux/pm.h>
588+#include <linux/platform_device.h>
589+#include <linux/power_supply.h>
590+
591+#include "../w1/w1.h"
592+#include "../w1/slaves/w1_ds2760.h"
593+
594+struct ds2760_device_info {
595+ struct device *dev;
596+
597+ /* DS2760 data, valid after calling ds2760_battery_read_status() */
598+ unsigned long update_time; /* jiffies when data read */
599+ char raw[DS2760_DATA_SIZE]; /* raw DS2760 data */
600+ int voltage_raw; /* units of 4.88 mV */
601+ int voltage_uV; /* units of uV */
602+ int current_raw; /* units of 0.625 mA */
603+ int current_uA; /* units of uA */
604+ int accum_current_raw; /* units of 0.25 mAh */
605+ int accum_current_uAh; /* units of uAh */
606+ int temp_raw; /* units of 0.125 C */
607+ int temp_C; /* units of 0.1 C */
608+ int rated_capacity; /* units of uAh */
609+ int rem_capacity; /* percentage */
610+ int full_active_uAh; /* units of uAh */
611+ int empty_uAh; /* units of uAh */
612+ int life_sec; /* units of seconds */
613+ int charge_status; /* POWER_SUPPLY_STATUS_* */
614+
615+ int full_counter;
616+ struct power_supply bat;
617+ struct device *w1_dev;
618+ struct workqueue_struct *monitor_wqueue;
619+ struct delayed_work monitor_work;
620+};
621+
622+static unsigned int cache_time = 1000;
623+module_param(cache_time, uint, 0644);
624+MODULE_PARM_DESC(cache_time, "cache time in milliseconds");
625+
626+/* Some batteries have their rated capacity stored a N * 10 mAh, while
627+ * others use an index into this table. */
628+static int rated_capacities[] = {
629+ 0,
630+ 920, /* Samsung */
631+ 920, /* BYD */
632+ 920, /* Lishen */
633+ 920, /* NEC */
634+ 1440, /* Samsung */
635+ 1440, /* BYD */
636+ 1440, /* Lishen */
637+ 1440, /* NEC */
638+ 2880, /* Samsung */
639+ 2880, /* BYD */
640+ 2880, /* Lishen */
641+ 2880 /* NEC */
642+};
643+
644+/* array is level at temps 0C, 10C, 20C, 30C, 40C
645+ * temp is in Celsius */
646+static int battery_interpolate(int array[], int temp)
647+{
648+ int index, dt;
649+
650+ if (temp <= 0)
651+ return array[0];
652+ if (temp >= 40)
653+ return array[4];
654+
655+ index = temp / 10;
656+ dt = temp % 10;
657+
658+ return array[index] + (((array[index + 1] - array[index]) * dt) / 10);
659+}
660+
661+static int ds2760_battery_read_status(struct ds2760_device_info *di)
662+{
663+ int ret, i, start, count, scale[5];
664+
665+ if (di->update_time && time_before(jiffies, di->update_time +
666+ msecs_to_jiffies(cache_time)))
667+ return 0;
668+
669+ /* The first time we read the entire contents of SRAM/EEPROM,
670+ * but after that we just read the interesting bits that change. */
671+ if (di->update_time == 0) {
672+ start = 0;
673+ count = DS2760_DATA_SIZE;
674+ }
675+ else {
676+ start = DS2760_VOLTAGE_MSB;
677+ count = DS2760_TEMP_LSB - start + 1;
678+ }
679+
680+ ret = w1_ds2760_read(di->w1_dev, di->raw + start, start, count);
681+ if (ret != count) {
682+ dev_warn(di->dev, "call to w1_ds2760_read failed (0x%p)\n",
683+ di->w1_dev);
684+ return 1;
685+ }
686+
687+ di->update_time = jiffies;
688+
689+ /* DS2760 reports voltage in units of 4.88mV, but the battery class
690+ * reports in units of uV, so convert by multiplying by 4880. */
691+ di->voltage_raw = (di->raw[DS2760_VOLTAGE_MSB] << 3) |
692+ (di->raw[DS2760_VOLTAGE_LSB] >> 5);
693+ di->voltage_uV = di->voltage_raw * 4880;
694+
695+ /* DS2760 reports current in signed units of 0.625mA, but the battery
696+ * class reports in units of uA, so convert by multiplying by 625. */
697+ di->current_raw =
698+ (((signed char)di->raw[DS2760_CURRENT_MSB]) << 5) |
699+ (di->raw[DS2760_CURRENT_LSB] >> 3);
700+ di->current_uA = di->current_raw * 625;
701+
702+ /* DS2760 reports accumulated current in signed units of 0.25mAh. */
703+ di->accum_current_raw =
704+ (((signed char)di->raw[DS2760_CURRENT_ACCUM_MSB]) << 8) |
705+ di->raw[DS2760_CURRENT_ACCUM_LSB];
706+ di->accum_current_uAh = di->accum_current_raw * 250;
707+
708+ /* DS2760 reports temperature in signed units of 0.125C, but the
709+ * battery class reports in units of 1/10 C, so we convert by
710+ * multiplying by .125 * 10 = 1.25. */
711+ di->temp_raw = (((signed char)di->raw[DS2760_TEMP_MSB]) << 3) |
712+ (di->raw[DS2760_TEMP_LSB] >> 5);
713+ di->temp_C = di->temp_raw + (di->temp_raw / 4);
714+
715+ /* At least some battery monitors (e.g. HP iPAQ) store the battery's
716+ * maximum rated capacity. */
717+ if (di->raw[DS2760_RATED_CAPACITY] < ARRAY_SIZE(rated_capacities))
718+ di->rated_capacity = rated_capacities[
719+ (unsigned int)di->raw[DS2760_RATED_CAPACITY]];
720+ else
721+ di->rated_capacity = di->raw[DS2760_RATED_CAPACITY] * 10;
722+
723+ di->rated_capacity *= 1000; /* convert to uAh */
724+
725+ /* Calculate the full level at the present temperature. */
726+ di->full_active_uAh = di->raw[DS2760_ACTIVE_FULL] << 8 |
727+ di->raw[DS2760_ACTIVE_FULL + 1];
728+
729+ scale[0] = di->raw[DS2760_ACTIVE_FULL] << 8 |
730+ di->raw[DS2760_ACTIVE_FULL + 1];
731+ for (i = 1; i < 5; i++)
732+ scale[i] = scale[i - 1] + di->raw[DS2760_ACTIVE_FULL + 2 + i];
733+
734+ di->full_active_uAh = battery_interpolate(scale, di->temp_C / 10);
735+ di->full_active_uAh *= 1000; /* convert to uAh */
736+
737+ /* Calculate the empty level at the present temperature. */
738+ scale[4] = di->raw[DS2760_ACTIVE_EMPTY + 4];
739+ for (i = 3; i >= 0; i--)
740+ scale[i] = scale[i + 1] + di->raw[DS2760_ACTIVE_EMPTY + i];
741+
742+ di->empty_uAh = battery_interpolate(scale, di->temp_C / 10);
743+ di->empty_uAh *= 1000; /* convert to uAh */
744+
745+ /* From Maxim Application Note 131: remaining capacity =
746+ * ((ICA - Empty Value) / (Full Value - Empty Value)) x 100% */
747+ di->rem_capacity = ((di->accum_current_uAh - di->empty_uAh) * 100L) /
748+ (di->full_active_uAh - di->empty_uAh);
749+
750+ if (di->rem_capacity < 0)
751+ di->rem_capacity = 0;
752+ if (di->rem_capacity > 100)
753+ di->rem_capacity = 100;
754+
755+ if (di->current_uA)
756+ di->life_sec = -((di->accum_current_uAh - di->empty_uAh) *
757+ 3600L) / di->current_uA;
758+ else
759+ di->life_sec = 0;
760+
761+ return 0;
762+}
763+
764+static void ds2760_battery_update_status(struct ds2760_device_info *di)
765+{
766+ int old_charge_status = di->charge_status;
767+
768+ ds2760_battery_read_status(di);
769+
770+ if (di->charge_status == POWER_SUPPLY_STATUS_UNKNOWN)
771+ di->full_counter = 0;
772+
773+ if (power_supply_am_i_supplied(&di->bat)) {
774+ if (di->current_uA > 10000) {
775+ di->charge_status = POWER_SUPPLY_STATUS_CHARGING;
776+ di->full_counter = 0;
777+ }
778+ else if (di->current_uA < -5000) {
779+ if (di->charge_status != POWER_SUPPLY_STATUS_NOT_CHARGING)
780+ dev_notice(di->dev, "not enough power to "
781+ "charge\n");
782+ di->charge_status = POWER_SUPPLY_STATUS_NOT_CHARGING;
783+ di->full_counter = 0;
784+ }
785+ else if (di->current_uA < 10000 &&
786+ di->charge_status != POWER_SUPPLY_STATUS_FULL) {
787+
788+ /* Don't consider the battery to be full unless
789+ * we've seen the current < 10 mA at least two
790+ * consecutive times. */
791+
792+ di->full_counter++;
793+
794+ if (di->full_counter < 2)
795+ di->charge_status = POWER_SUPPLY_STATUS_CHARGING;
796+ else {
797+ unsigned char acr[2];
798+ int acr_val;
799+
800+ /* acr is in units of 0.25 mAh */
801+ acr_val = di->full_active_uAh * 4L / 1000;
802+
803+ acr[0] = acr_val >> 8;
804+ acr[1] = acr_val & 0xff;
805+
806+ if (w1_ds2760_write(di->w1_dev, acr,
807+ DS2760_CURRENT_ACCUM_MSB, 2) < 2)
808+ dev_warn(di->dev,
809+ "ACR reset failed\n");
810+
811+ di->charge_status = POWER_SUPPLY_STATUS_FULL;
812+ }
813+ }
814+ }
815+ else {
816+ di->charge_status = POWER_SUPPLY_STATUS_DISCHARGING;
817+ di->full_counter = 0;
818+ }
819+
820+ if (di->charge_status != old_charge_status)
821+ power_supply_changed(&di->bat);
822+
823+ return;
824+}
825+
826+static void ds2760_battery_work(struct work_struct *work)
827+{
828+ struct ds2760_device_info *di = container_of(work,
829+ struct ds2760_device_info, monitor_work.work);
830+ const int interval = HZ * 60;
831+
832+ dev_dbg(di->dev, "%s\n", __FUNCTION__);
833+
834+ ds2760_battery_update_status(di);
835+ queue_delayed_work(di->monitor_wqueue, &di->monitor_work, interval);
836+
837+ return;
838+}
839+
840+#define to_ds2760_device_info(x) container_of((x), struct ds2760_device_info, \
841+ bat);
842+
843+static void ds2760_battery_external_power_changed(struct power_supply *psy)
844+{
845+ struct ds2760_device_info *di = to_ds2760_device_info(psy);
846+
847+ dev_dbg(di->dev, "%s\n", __FUNCTION__);
848+
849+ cancel_delayed_work(&di->monitor_work);
850+ queue_delayed_work(di->monitor_wqueue, &di->monitor_work, HZ/10);
851+
852+ return;
853+}
854+
855+static int ds2760_battery_get_property(struct power_supply *psy,
856+ enum power_supply_property psp,
857+ union power_supply_propval *val)
858+{
859+ struct ds2760_device_info *di = to_ds2760_device_info(psy);
860+
861+ switch (psp) {
862+ case POWER_SUPPLY_PROP_STATUS:
863+ val->intval = di->charge_status;
864+ return 0;
865+ default:
866+ break;
867+ }
868+
869+ ds2760_battery_read_status(di);
870+
871+ switch (psp) {
872+ case POWER_SUPPLY_PROP_VOLTAGE_NOW:
873+ val->intval = di->voltage_uV;
874+ break;
875+ case POWER_SUPPLY_PROP_CURRENT_NOW:
876+ val->intval = di->current_uA;
877+ break;
878+ case POWER_SUPPLY_PROP_CHARGE_FULL_DESIGN:
879+ val->intval = di->rated_capacity;
880+ break;
881+ case POWER_SUPPLY_PROP_CHARGE_FULL:
882+ val->intval = di->full_active_uAh;
883+ break;
884+ case POWER_SUPPLY_PROP_CHARGE_EMPTY:
885+ val->intval = di->empty_uAh;
886+ break;
887+ case POWER_SUPPLY_PROP_CHARGE_NOW:
888+ val->intval = di->accum_current_uAh;
889+ break;
890+ case POWER_SUPPLY_PROP_TEMP:
891+ val->intval = di->temp_C;
892+ break;
893+ default:
894+ return -EINVAL;
895+ }
896+
897+ return 0;
898+}
899+
900+static enum power_supply_property ds2760_battery_props[] = {
901+ POWER_SUPPLY_PROP_STATUS,
902+ POWER_SUPPLY_PROP_VOLTAGE_NOW,
903+ POWER_SUPPLY_PROP_CURRENT_NOW,
904+ POWER_SUPPLY_PROP_CHARGE_FULL_DESIGN,
905+ POWER_SUPPLY_PROP_CHARGE_FULL,
906+ POWER_SUPPLY_PROP_CHARGE_EMPTY,
907+ POWER_SUPPLY_PROP_CHARGE_NOW,
908+ POWER_SUPPLY_PROP_TEMP,
909+};
910+
911+static int ds2760_battery_probe(struct platform_device *pdev)
912+{
913+ int retval = 0;
914+ struct ds2760_device_info *di;
915+ struct ds2760_platform_data *pdata;
916+
917+ di = kzalloc(sizeof(*di), GFP_KERNEL);
918+ if (!di) {
919+ retval = -ENOMEM;
920+ goto di_alloc_failed;
921+ }
922+
923+ platform_set_drvdata(pdev, di);
924+
925+ pdata = pdev->dev.platform_data;
926+ di->dev = &pdev->dev;
927+ di->w1_dev = pdev->dev.parent;
928+ di->bat.name = pdev->dev.bus_id;
929+ di->bat.type = POWER_SUPPLY_TYPE_BATTERY;
930+ di->bat.properties = ds2760_battery_props;
931+ di->bat.num_properties = ARRAY_SIZE(ds2760_battery_props);
932+ di->bat.get_property = ds2760_battery_get_property;
933+ di->bat.external_power_changed =
934+ ds2760_battery_external_power_changed;
935+ di->bat.use_for_apm = 1;
936+
937+ di->charge_status = POWER_SUPPLY_STATUS_UNKNOWN;
938+
939+ retval = power_supply_register(&pdev->dev, &di->bat);
940+ if (retval) {
941+ dev_err(di->dev, "failed to register battery");
942+ goto batt_failed;
943+ }
944+
945+ INIT_DELAYED_WORK(&di->monitor_work, ds2760_battery_work);
946+ di->monitor_wqueue = create_singlethread_workqueue(pdev->dev.bus_id);
947+ if (!di->monitor_wqueue) {
948+ retval = -ESRCH;
949+ goto workqueue_failed;
950+ }
951+ queue_delayed_work(di->monitor_wqueue, &di->monitor_work, HZ * 1);
952+
953+ goto success;
954+
955+workqueue_failed:
956+ power_supply_unregister(&di->bat);
957+batt_failed:
958+ kfree(di);
959+di_alloc_failed:
960+success:
961+ return retval;
962+}
963+
964+static int ds2760_battery_remove(struct platform_device *pdev)
965+{
966+ struct ds2760_device_info *di = platform_get_drvdata(pdev);
967+
968+ cancel_rearming_delayed_workqueue(di->monitor_wqueue,
969+ &di->monitor_work);
970+ destroy_workqueue(di->monitor_wqueue);
971+ power_supply_unregister(&di->bat);
972+
973+ return 0;
974+}
975+
976+#ifdef CONFIG_PM
977+
978+static int ds2760_battery_suspend(struct platform_device *pdev,
979+ pm_message_t state)
980+{
981+ struct ds2760_device_info *di = platform_get_drvdata(pdev);
982+
983+ di->charge_status = POWER_SUPPLY_STATUS_UNKNOWN;
984+
985+ return 0;
986+}
987+
988+static int ds2760_battery_resume(struct platform_device *pdev)
989+{
990+ struct ds2760_device_info *di = platform_get_drvdata(pdev);
991+
992+ di->charge_status = POWER_SUPPLY_STATUS_UNKNOWN;
993+ power_supply_changed(&di->bat);
994+
995+ cancel_delayed_work(&di->monitor_work);
996+ queue_delayed_work(di->monitor_wqueue, &di->monitor_work, HZ);
997+
998+ return 0;
999+}
1000+
1001+#else
1002+
1003+#define ds2760_battery_suspend NULL
1004+#define ds2760_battery_resume NULL
1005+
1006+#endif /* CONFIG_PM */
1007+
1008+static struct platform_driver ds2760_battery_driver = {
1009+ .driver = {
1010+ .name = "ds2760-battery",
1011+ },
1012+ .probe = ds2760_battery_probe,
1013+ .remove = ds2760_battery_remove,
1014+ .suspend = ds2760_battery_suspend,
1015+ .resume = ds2760_battery_resume,
1016+};
1017+
1018+static int __init ds2760_battery_init(void)
1019+{
1020+ return platform_driver_register(&ds2760_battery_driver);
1021+}
1022+
1023+static void __exit ds2760_battery_exit(void)
1024+{
1025+ platform_driver_unregister(&ds2760_battery_driver);
1026+ return;
1027+}
1028+
1029+module_init(ds2760_battery_init);
1030+module_exit(ds2760_battery_exit);
1031+
1032+MODULE_LICENSE("GPL");
1033+MODULE_AUTHOR("Szabolcs Gyurko <szabolcs.gyurko@tlt.hu>, "
1034+ "Matt Reimer <mreimer@vpop.net>, "
1035+ "Anton Vorontsov <cbou@mail.ru>");
1036+MODULE_DESCRIPTION("ds2760 battery driver");
1037Index: linux-2.6.22/drivers/power/Kconfig
1038===================================================================
1039--- /dev/null 1970-01-01 00:00:00.000000000 +0000
1040+++ linux-2.6.22/drivers/power/Kconfig 2007-08-23 12:13:52.000000000 +0200
1041@@ -0,0 +1,70 @@
1042+menuconfig POWER_SUPPLY
1043+ tristate "Power supply class support"
1044+ help
1045+ Say Y here to enable power supply class support. This allows
1046+ power supply (batteries, AC, USB) monitoring by userspace
1047+ via sysfs and uevent (if available) and/or APM kernel interface
1048+ (if selected below).
1049+
1050+if POWER_SUPPLY
1051+
1052+config POWER_SUPPLY_DEBUG
1053+ bool "Power supply debug"
1054+ help
1055+ Say Y here to enable debugging messages for power supply class
1056+ and drivers.
1057+
1058+config PDA_POWER
1059+ tristate "Generic PDA/phone power driver"
1060+ help
1061+ Say Y here to enable generic power driver for PDAs and phones with
1062+ one or two external power supplies (AC/USB) connected to main and
1063+ backup batteries, and optional builtin charger.
1064+
1065+config APM_POWER
1066+ tristate "APM emulation for class batteries"
1067+ depends on APM_EMULATION
1068+ help
1069+ Say Y here to enable support APM status emulation using
1070+ battery class devices.
1071+
1072+config BATTERY_DS2760
1073+ tristate "DS2760 battery driver (HP iPAQ & others)"
1074+ select W1
1075+ select W1_SLAVE_DS2760
1076+ help
1077+ Say Y here to enable support for batteries with ds2760 chip.
1078+
1079+config BATTERY_PMU
1080+ tristate "Apple PMU battery"
1081+ depends on ADB_PMU
1082+ help
1083+ Say Y here to expose battery information on Apple machines
1084+ through the generic battery class.
1085+
1086+config BATTERY_OLPC
1087+ tristate "One Laptop Per Child battery"
1088+ depends on X86_32
1089+ help
1090+ Say Y to enable support for the battery on the OLPC laptop.
1091+
1092+# drivers below are not in battery2-2.6 tree
1093+
1094+config ADC_BATTERY
1095+ tristate "Generic ADC battery driver"
1096+ depends on ADC && POWER_SUPPLY
1097+ help
1098+ Say Y here to enable support for battery monitoring using generic ADC device.
1099+
1100+config IPAQ_MICRO_BATTERY
1101+ tristate "HP iPAQ Micro ASIC battery driver"
1102+ depends on IPAQ_MICRO && POWER_SUPPLY
1103+ help
1104+ Choose this option if you want to monitor battery status on
1105+ Compaq/HP iPAQ h3100 h3600
1106+
1107+config MCP_UCB1x00_SIMPAD_BATTERY
1108+ tristate "SIMpad Battery Reading Support"
1109+ depends on MCP_UCB1x00 && POWER_SUPPLY
1110+
1111+endif # POWER_SUPPLY
1112Index: linux-2.6.22/drivers/power/Makefile
1113===================================================================
1114--- /dev/null 1970-01-01 00:00:00.000000000 +0000
1115+++ linux-2.6.22/drivers/power/Makefile 2007-08-23 12:13:52.000000000 +0200
1116@@ -0,0 +1,28 @@
1117+power_supply-objs := power_supply_core.o
1118+
1119+ifeq ($(CONFIG_SYSFS),y)
1120+power_supply-objs += power_supply_sysfs.o
1121+endif
1122+
1123+ifeq ($(CONFIG_LEDS_TRIGGERS),y)
1124+power_supply-objs += power_supply_leds.o
1125+endif
1126+
1127+ifeq ($(CONFIG_POWER_SUPPLY_DEBUG),y)
1128+EXTRA_CFLAGS += -DDEBUG
1129+endif
1130+
1131+obj-$(CONFIG_POWER_SUPPLY) += power_supply.o
1132+
1133+obj-$(CONFIG_PDA_POWER) += pda_power.o
1134+obj-$(CONFIG_APM_POWER) += apm_power.o
1135+
1136+obj-$(CONFIG_BATTERY_DS2760) += ds2760_battery.o
1137+obj-$(CONFIG_BATTERY_PMU) += pmu_battery.o
1138+obj-$(CONFIG_BATTERY_OLPC) += olpc_battery.o
1139+
1140+# drivers below are not in battery2-2.6 tree
1141+
1142+obj-$(CONFIG_ADC_BATTERY) += adc_battery.o
1143+obj-$(CONFIG_IPAQ_MICRO_BATTERY) += micro_battery.o
1144+obj-$(CONFIG_MCP_UCB1x00_SIMPAD_BATTERY) += simpad-battery.o
1145Index: linux-2.6.22/drivers/power/micro_battery.c
1146===================================================================
1147--- /dev/null 1970-01-01 00:00:00.000000000 +0000
1148+++ linux-2.6.22/drivers/power/micro_battery.c 2007-08-23 12:25:20.000000000 +0200
1149@@ -0,0 +1,257 @@
1150+/*
1151+ * This program is free software; you can redistribute it and/or modify
1152+ * it under the terms of the GNU General Public License version 2 as
1153+ * published by the Free Software Foundation.
1154+ *
1155+ * h3600 atmel micro companion support, battery subdevice
1156+ * based on previous kernel 2.4 version
1157+ * Author : Alessandro Gardich <gremlin@gremlin.it>
1158+ *
1159+ */
1160+
1161+
1162+#include <linux/module.h>
1163+#include <linux/version.h>
1164+
1165+#include <linux/init.h>
1166+#include <linux/fs.h>
1167+#include <linux/interrupt.h>
1168+#include <linux/sched.h>
1169+#include <linux/pm.h>
1170+#include <linux/sysctl.h>
1171+#include <linux/proc_fs.h>
1172+#include <linux/delay.h>
1173+#include <linux/device.h>
1174+#include <linux/power_supply.h>
1175+#include <linux/platform_device.h>
1176+#include <linux/timer.h>
1177+
1178+#include <asm/arch/hardware.h>
1179+
1180+#include <asm/arch/h3600.h>
1181+#include <asm/arch/SA-1100.h>
1182+
1183+#include <asm/hardware/micro.h>
1184+
1185+#define BATT_PERIOD 10*HZ
1186+
1187+#define H3600_BATT_STATUS_HIGH 0x01
1188+#define H3600_BATT_STATUS_LOW 0x02
1189+#define H3600_BATT_STATUS_CRITICAL 0x04
1190+#define H3600_BATT_STATUS_CHARGING 0x08
1191+#define H3600_BATT_STATUS_CHARGEMAIN 0x10
1192+#define H3600_BATT_STATUS_DEAD 0x20 /* Battery will not charge */
1193+#define H3600_BATT_STATUS_NOTINSTALLED 0x20 /* For expansion pack batteries */
1194+#define H3600_BATT_STATUS_FULL 0x40 /* Battery fully charged (and connected to AC) */
1195+#define H3600_BATT_STATUS_NOBATTERY 0x80
1196+#define H3600_BATT_STATUS_UNKNOWN 0xff
1197+
1198+
1199+//static struct power_supply_dev *micro_battery;
1200+
1201+static micro_private_t *p_micro;
1202+
1203+struct timer_list batt_timer;
1204+
1205+struct {
1206+ int ac;
1207+ int update_time;
1208+ int chemistry;
1209+ int voltage;
1210+ int temperature;
1211+ int flag;
1212+} micro_battery;
1213+
1214+static void micro_battery_receive (int len, unsigned char *data) {
1215+ if (0) {
1216+ printk(KERN_ERR "h3600_battery - AC = %02x\n", data[0]);
1217+ printk(KERN_ERR "h3600_battery - BAT1 chemistry = %02x\n", data[1]);
1218+ printk(KERN_ERR "h3600_battery - BAT1 voltage = %d %02x%02x\n", (data[3]<<8)+data[2], data[2], data[3]);
1219+ printk(KERN_ERR "h3600_battery - BAT1 status = %02x\n", data[4]);
1220+ }
1221+
1222+ micro_battery.ac = data[0];
1223+ micro_battery.chemistry = data[1];
1224+ micro_battery.voltage = ((((unsigned short)data[3]<<8)+data[2]) * 5000L ) * 1000 / 1024;
1225+ micro_battery.flag = data[4];
1226+
1227+ if (len == 9) {
1228+ if (0) {
1229+ printk(KERN_ERR "h3600_battery - BAT2 chemistry = %02x\n", data[5]);
1230+ printk(KERN_ERR "h3600_battery - BAT2 voltage = %d %02x%02x\n", (data[7]<<8)+data[6], data[6], data[7]);
1231+ printk(KERN_ERR "h3600_battery - BAT2 status = %02x\n", data[8]);
1232+ }
1233+ }
1234+}
1235+
1236+static void micro_temperature_receive (int len, unsigned char *data) {
1237+ micro_battery.temperature = ((unsigned short)data[1]<<8)+data[0];
1238+}
1239+
1240+void h3600_battery_read_status(unsigned long data) {
1241+
1242+ if (++data % 2)
1243+ h3600_micro_tx_msg(0x09,0,NULL);
1244+ else
1245+ h3600_micro_tx_msg(0x06,0,NULL);
1246+
1247+ batt_timer.expires += BATT_PERIOD;
1248+ batt_timer.data = data;
1249+
1250+ add_timer(&batt_timer);
1251+}
1252+
1253+int get_capacity(struct power_supply *b) {
1254+ switch (micro_battery.flag) {
1255+ case H3600_BATT_STATUS_HIGH : return 100; break;
1256+ case H3600_BATT_STATUS_LOW : return 50; break;
1257+ case H3600_BATT_STATUS_CRITICAL : return 5; break;
1258+ default: break;
1259+ }
1260+ return 0;
1261+}
1262+
1263+int get_status(struct power_supply *b) {
1264+
1265+ if (micro_battery.flag == H3600_BATT_STATUS_UNKNOWN)
1266+ return POWER_SUPPLY_STATUS_UNKNOWN;
1267+
1268+ if (micro_battery.flag & H3600_BATT_STATUS_FULL)
1269+ return POWER_SUPPLY_STATUS_FULL;
1270+
1271+ if ((micro_battery.flag & H3600_BATT_STATUS_CHARGING) ||
1272+ (micro_battery.flag & H3600_BATT_STATUS_CHARGEMAIN))
1273+ return POWER_SUPPLY_STATUS_CHARGING;
1274+
1275+ return POWER_SUPPLY_STATUS_DISCHARGING;
1276+}
1277+
1278+static int micro_batt_get_property(struct power_supply *b,
1279+ enum power_supply_property psp,
1280+ union power_supply_propval *val)
1281+{
1282+ switch (psp) {
1283+ case POWER_SUPPLY_PROP_STATUS:
1284+ val->intval = get_status(b);
1285+ break;
1286+ case POWER_SUPPLY_PROP_VOLTAGE_MAX_DESIGN:
1287+ val->intval = 4700000;
1288+ break;
1289+ case POWER_SUPPLY_PROP_CAPACITY:
1290+ val->intval = get_capacity(b);
1291+ break;
1292+ case POWER_SUPPLY_PROP_TEMP:
1293+ val->intval = micro_battery.temperature;
1294+ break;
1295+ case POWER_SUPPLY_PROP_VOLTAGE_NOW:
1296+ val->intval = micro_battery.voltage;
1297+ break;
1298+ default:
1299+ return -EINVAL;
1300+ };
1301+
1302+ return 0;
1303+}
1304+
1305+static enum power_supply_property micro_batt_props[] = {
1306+ POWER_SUPPLY_PROP_STATUS,
1307+ POWER_SUPPLY_PROP_VOLTAGE_MAX_DESIGN,
1308+ POWER_SUPPLY_PROP_CAPACITY,
1309+ POWER_SUPPLY_PROP_TEMP,
1310+ POWER_SUPPLY_PROP_VOLTAGE_NOW,
1311+};
1312+
1313+static struct power_supply h3600_battery = {
1314+ .name = "main-battery",
1315+ .properties = micro_batt_props,
1316+ .num_properties = ARRAY_SIZE(micro_batt_props),
1317+ .get_property = micro_batt_get_property,
1318+ .use_for_apm = 1,
1319+};
1320+
1321+static int micro_batt_probe (struct platform_device *pdev)
1322+{
1323+ if (1) printk(KERN_ERR "micro battery probe : begin\n");
1324+
1325+ power_supply_register(&pdev->dev, &h3600_battery);
1326+
1327+ { /*--- callback ---*/
1328+ p_micro = platform_get_drvdata(pdev);
1329+ spin_lock(p_micro->lock);
1330+ p_micro->h_batt = micro_battery_receive;
1331+ p_micro->h_temp = micro_temperature_receive;
1332+ spin_unlock(p_micro->lock);
1333+ }
1334+
1335+ { /*--- timer ---*/
1336+ init_timer(&batt_timer);
1337+ batt_timer.expires = jiffies + BATT_PERIOD;
1338+ batt_timer.data = 0;
1339+ batt_timer.function = h3600_battery_read_status;
1340+
1341+ add_timer(&batt_timer);
1342+ }
1343+
1344+ if (1) printk(KERN_ERR "micro battery probe : end\n");
1345+ return 0;
1346+}
1347+
1348+static int micro_batt_remove (struct platform_device *pdev)
1349+{
1350+ power_supply_unregister(&h3600_battery);
1351+ { /*--- callback ---*/
1352+ init_timer(&batt_timer);
1353+ p_micro->h_batt = NULL;
1354+ p_micro->h_temp = NULL;
1355+ spin_unlock(p_micro->lock);
1356+ }
1357+ { /*--- timer ---*/
1358+ del_timer_sync(&batt_timer);
1359+ }
1360+ return 0;
1361+}
1362+
1363+static int micro_batt_suspend ( struct platform_device *pdev, pm_message_t state)
1364+{
1365+ { /*--- timer ---*/
1366+ del_timer(&batt_timer);
1367+ }
1368+ return 0;
1369+}
1370+
1371+static int micro_batt_resume ( struct platform_device *pdev)
1372+{
1373+ { /*--- timer ---*/
1374+ add_timer(&batt_timer);
1375+ }
1376+ return 0;
1377+}
1378+
1379+struct platform_driver micro_batt_device_driver = {
1380+ .driver = {
1381+ .name = "h3600-micro-battery",
1382+ },
1383+ .probe = micro_batt_probe,
1384+ .remove = micro_batt_remove,
1385+ .suspend = micro_batt_suspend,
1386+ .resume = micro_batt_resume,
1387+};
1388+
1389+static int micro_batt_init (void)
1390+{
1391+ return platform_driver_register(&micro_batt_device_driver);
1392+}
1393+
1394+static void micro_batt_cleanup (void)
1395+{
1396+ platform_driver_unregister (&micro_batt_device_driver);
1397+}
1398+
1399+module_init (micro_batt_init);
1400+module_exit (micro_batt_cleanup);
1401+
1402+MODULE_LICENSE("GPL");
1403+MODULE_AUTHOR("gremlin.it");
1404+MODULE_DESCRIPTION("driver for iPAQ Atmel micro battery");
1405+
1406+
1407Index: linux-2.6.22/drivers/power/olpc_battery.c
1408===================================================================
1409--- /dev/null 1970-01-01 00:00:00.000000000 +0000
1410+++ linux-2.6.22/drivers/power/olpc_battery.c 2007-08-23 12:13:52.000000000 +0200
1411@@ -0,0 +1,302 @@
1412+/*
1413+ * Battery driver for One Laptop Per Child board.
1414+ *
1415+ * Copyright © 2006 David Woodhouse <dwmw2@infradead.org>
1416+ *
1417+ * This program is free software; you can redistribute it and/or modify
1418+ * it under the terms of the GNU General Public License version 2 as
1419+ * published by the Free Software Foundation.
1420+ */
1421+
1422+#include <linux/module.h>
1423+#include <linux/err.h>
1424+#include <linux/platform_device.h>
1425+#include <linux/power_supply.h>
1426+#include <linux/jiffies.h>
1427+#include <linux/sched.h>
1428+#include <asm/io.h>
1429+
1430+#define wBAT_VOLTAGE 0xf900 /* *9.76/32, mV */
1431+#define wBAT_CURRENT 0xf902 /* *15.625/120, mA */
1432+#define wBAT_TEMP 0xf906 /* *256/1000, °C */
1433+#define wAMB_TEMP 0xf908 /* *256/1000, °C */
1434+#define SOC 0xf910 /* percentage */
1435+#define sMBAT_STATUS 0xfaa4
1436+#define sBAT_PRESENT 1
1437+#define sBAT_FULL 2
1438+#define sBAT_DESTROY 4 /* what is this exactly? */
1439+#define sBAT_LOW 32
1440+#define sBAT_DISCHG 64
1441+#define sMCHARGE_STATUS 0xfaa5
1442+#define sBAT_CHARGE 1
1443+#define sBAT_OVERTEMP 4
1444+#define sBAT_NiMH 8
1445+#define sPOWER_FLAG 0xfa40
1446+#define ADAPTER_IN 1
1447+
1448+/*********************************************************************
1449+ * EC locking and access
1450+ *********************************************************************/
1451+
1452+static int lock_ec(void)
1453+{
1454+ unsigned long timeo = jiffies + HZ / 20;
1455+
1456+ while (1) {
1457+ unsigned char lock = inb(0x6c) & 0x80;
1458+ if (!lock)
1459+ return 0;
1460+ if (time_after(jiffies, timeo)) {
1461+ printk(KERN_ERR "olpc_battery: failed to lock EC for "
1462+ "battery access\n");
1463+ return 1;
1464+ }
1465+ yield();
1466+ }
1467+}
1468+
1469+static void unlock_ec(void)
1470+{
1471+ outb(0xff, 0x6c);
1472+ return;
1473+}
1474+
1475+static unsigned char read_ec_byte(unsigned short adr)
1476+{
1477+ outb(adr >> 8, 0x381);
1478+ outb(adr, 0x382);
1479+ return inb(0x383);
1480+}
1481+
1482+static unsigned short read_ec_word(unsigned short adr)
1483+{
1484+ return (read_ec_byte(adr) << 8) | read_ec_byte(adr + 1);
1485+}
1486+
1487+/*********************************************************************
1488+ * Power
1489+ *********************************************************************/
1490+
1491+static int olpc_ac_get_prop(struct power_supply *psy,
1492+ enum power_supply_property psp,
1493+ union power_supply_propval *val)
1494+{
1495+ int ret = 0;
1496+
1497+ if (lock_ec())
1498+ return -EIO;
1499+
1500+ switch (psp) {
1501+ case POWER_SUPPLY_PROP_ONLINE:
1502+ if (!(read_ec_byte(sMBAT_STATUS) & sBAT_PRESENT)) {
1503+ ret = -ENODEV;
1504+ goto out;
1505+ }
1506+ val->intval = !!(read_ec_byte(sPOWER_FLAG) & ADAPTER_IN);
1507+ break;
1508+ default:
1509+ ret = -EINVAL;
1510+ break;
1511+ }
1512+out:
1513+ unlock_ec();
1514+ return ret;
1515+}
1516+
1517+static enum power_supply_property olpc_ac_props[] = {
1518+ POWER_SUPPLY_PROP_ONLINE,
1519+};
1520+
1521+static struct power_supply olpc_ac = {
1522+ .name = "olpc-ac",
1523+ .type = POWER_SUPPLY_TYPE_MAINS,
1524+ .properties = olpc_ac_props,
1525+ .num_properties = ARRAY_SIZE(olpc_ac_props),
1526+ .get_property = olpc_ac_get_prop,
1527+};
1528+
1529+/*********************************************************************
1530+ * Battery properties
1531+ *********************************************************************/
1532+
1533+static int olpc_bat_get_property(struct power_supply *psy,
1534+ enum power_supply_property psp,
1535+ union power_supply_propval *val)
1536+{
1537+ int ret = 0;
1538+
1539+ if (lock_ec())
1540+ return -EIO;
1541+
1542+ switch (psp) {
1543+ case POWER_SUPPLY_PROP_STATUS:
1544+ {
1545+ int status = POWER_SUPPLY_STATUS_UNKNOWN;
1546+
1547+ val->intval = read_ec_byte(sMBAT_STATUS);
1548+
1549+ if (!(val->intval & sBAT_PRESENT)) {
1550+ ret = -ENODEV;
1551+ goto out;
1552+ }
1553+
1554+ if (val->intval & sBAT_DISCHG)
1555+ status = POWER_SUPPLY_STATUS_DISCHARGING;
1556+ else if (val->intval & sBAT_FULL)
1557+ status = POWER_SUPPLY_STATUS_FULL;
1558+
1559+ val->intval = read_ec_byte(sMCHARGE_STATUS);
1560+ if (val->intval & sBAT_CHARGE)
1561+ status = POWER_SUPPLY_STATUS_CHARGING;
1562+
1563+ val->intval = status;
1564+ break;
1565+ }
1566+ case POWER_SUPPLY_PROP_PRESENT:
1567+ val->intval = !!(read_ec_byte(sMBAT_STATUS) & sBAT_PRESENT);
1568+ break;
1569+ case POWER_SUPPLY_PROP_HEALTH:
1570+ val->intval = read_ec_byte(sMCHARGE_STATUS);
1571+ if (val->intval & sBAT_OVERTEMP)
1572+ val->intval = POWER_SUPPLY_HEALTH_OVERHEAT;
1573+ else
1574+ val->intval = POWER_SUPPLY_HEALTH_GOOD;
1575+ break;
1576+ case POWER_SUPPLY_PROP_TECHNOLOGY:
1577+ val->intval = read_ec_byte(sMCHARGE_STATUS);
1578+ if (val->intval & sBAT_NiMH)
1579+ val->intval = POWER_SUPPLY_TECHNOLOGY_NIMH;
1580+ else
1581+ val->intval = POWER_SUPPLY_TECHNOLOGY_UNKNOWN;
1582+ break;
1583+ case POWER_SUPPLY_PROP_VOLTAGE_AVG:
1584+ val->intval = read_ec_byte(wBAT_VOLTAGE) * 9760L / 32;
1585+ break;
1586+ case POWER_SUPPLY_PROP_CURRENT_AVG:
1587+ val->intval = read_ec_byte(wBAT_CURRENT) * 15625L / 120;
1588+ break;
1589+ case POWER_SUPPLY_PROP_CAPACITY:
1590+ val->intval = read_ec_byte(SOC);
1591+ break;
1592+ case POWER_SUPPLY_PROP_CAPACITY_LEVEL:
1593+ val->intval = read_ec_byte(sMBAT_STATUS);
1594+ if (val->intval & sBAT_FULL)
1595+ val->intval = POWER_SUPPLY_CAPACITY_LEVEL_FULL;
1596+ else if (val->intval & sBAT_LOW)
1597+ val->intval = POWER_SUPPLY_CAPACITY_LEVEL_LOW;
1598+ else
1599+ val->intval = POWER_SUPPLY_CAPACITY_LEVEL_NORMAL;
1600+ break;
1601+ case POWER_SUPPLY_PROP_TEMP:
1602+ val->intval = read_ec_byte(wBAT_TEMP) * 256 / 100;
1603+ break;
1604+ case POWER_SUPPLY_PROP_TEMP_AMBIENT:
1605+ val->intval = read_ec_byte(wAMB_TEMP) * 256 / 100;
1606+ break;
1607+ default:
1608+ ret = -EINVAL;
1609+ break;
1610+ }
1611+
1612+out:
1613+ unlock_ec();
1614+ return ret;
1615+}
1616+
1617+static enum power_supply_property olpc_bat_props[] = {
1618+ POWER_SUPPLY_PROP_STATUS,
1619+ POWER_SUPPLY_PROP_PRESENT,
1620+ POWER_SUPPLY_PROP_HEALTH,
1621+ POWER_SUPPLY_PROP_TECHNOLOGY,
1622+ POWER_SUPPLY_PROP_VOLTAGE_AVG,
1623+ POWER_SUPPLY_PROP_CURRENT_AVG,
1624+ POWER_SUPPLY_PROP_CAPACITY,
1625+ POWER_SUPPLY_PROP_CAPACITY_LEVEL,
1626+ POWER_SUPPLY_PROP_TEMP,
1627+ POWER_SUPPLY_PROP_TEMP_AMBIENT,
1628+};
1629+
1630+/*********************************************************************
1631+ * Initialisation
1632+ *********************************************************************/
1633+
1634+static struct platform_device *bat_pdev;
1635+
1636+static struct power_supply olpc_bat = {
1637+ .properties = olpc_bat_props,
1638+ .num_properties = ARRAY_SIZE(olpc_bat_props),
1639+ .get_property = olpc_bat_get_property,
1640+ .use_for_apm = 1,
1641+};
1642+
1643+static int __init olpc_bat_init(void)
1644+{
1645+ int ret = 0;
1646+ unsigned short tmp;
1647+
1648+ if (!request_region(0x380, 4, "olpc-battery")) {
1649+ ret = -EIO;
1650+ goto region_failed;
1651+ }
1652+
1653+ if (lock_ec()) {
1654+ ret = -EIO;
1655+ goto lock_failed;
1656+ }
1657+
1658+ tmp = read_ec_word(0xfe92);
1659+ unlock_ec();
1660+
1661+ if (tmp != 0x380) {
1662+ /* Doesn't look like OLPC EC */
1663+ ret = -ENODEV;
1664+ goto not_olpc_ec;
1665+ }
1666+
1667+ bat_pdev = platform_device_register_simple("olpc-battery", 0, NULL, 0);
1668+ if (IS_ERR(bat_pdev)) {
1669+ ret = PTR_ERR(bat_pdev);
1670+ goto pdev_failed;
1671+ }
1672+
1673+ ret = power_supply_register(&bat_pdev->dev, &olpc_ac);
1674+ if (ret)
1675+ goto ac_failed;
1676+
1677+ olpc_bat.name = bat_pdev->name;
1678+
1679+ ret = power_supply_register(&bat_pdev->dev, &olpc_bat);
1680+ if (ret)
1681+ goto battery_failed;
1682+
1683+ goto success;
1684+
1685+battery_failed:
1686+ power_supply_unregister(&olpc_ac);
1687+ac_failed:
1688+ platform_device_unregister(bat_pdev);
1689+pdev_failed:
1690+not_olpc_ec:
1691+lock_failed:
1692+ release_region(0x380, 4);
1693+region_failed:
1694+success:
1695+ return ret;
1696+}
1697+
1698+static void __exit olpc_bat_exit(void)
1699+{
1700+ power_supply_unregister(&olpc_bat);
1701+ power_supply_unregister(&olpc_ac);
1702+ platform_device_unregister(bat_pdev);
1703+ release_region(0x380, 4);
1704+ return;
1705+}
1706+
1707+module_init(olpc_bat_init);
1708+module_exit(olpc_bat_exit);
1709+
1710+MODULE_AUTHOR("David Woodhouse <dwmw2@infradead.org>");
1711+MODULE_LICENSE("GPL");
1712+MODULE_DESCRIPTION("Battery driver for One Laptop Per Child "
1713+ "($100 laptop) board.");
1714Index: linux-2.6.22/drivers/power/pda_power.c
1715===================================================================
1716--- /dev/null 1970-01-01 00:00:00.000000000 +0000
1717+++ linux-2.6.22/drivers/power/pda_power.c 2007-08-23 12:13:52.000000000 +0200
1718@@ -0,0 +1,263 @@
1719+/*
1720+ * Common power driver for PDAs and phones with one or two external
1721+ * power supplies (AC/USB) connected to main and backup batteries,
1722+ * and optional builtin charger.
1723+ *
1724+ * Copyright 2007 Anton Vorontsov <cbou@mail.ru>
1725+ *
1726+ * This program is free software; you can redistribute it and/or modify
1727+ * it under the terms of the GNU General Public License version 2 as
1728+ * published by the Free Software Foundation.
1729+ */
1730+
1731+#include <linux/module.h>
1732+#include <linux/platform_device.h>
1733+#include <linux/interrupt.h>
1734+#include <linux/power_supply.h>
1735+#include <linux/pda_power.h>
1736+#include <linux/timer.h>
1737+#include <linux/jiffies.h>
1738+
1739+static inline unsigned int get_irq_flags(struct resource *res)
1740+{
1741+ unsigned int flags = IRQF_DISABLED | IRQF_SHARED;
1742+
1743+ flags |= res->flags & IRQF_TRIGGER_MASK;
1744+
1745+ return flags;
1746+}
1747+
1748+static struct device *dev;
1749+static struct pda_power_pdata *pdata;
1750+static struct resource *ac_irq, *usb_irq;
1751+static struct timer_list charger_timer;
1752+static struct timer_list supply_timer;
1753+
1754+static int pda_power_get_property(struct power_supply *psy,
1755+ enum power_supply_property psp,
1756+ union power_supply_propval *val)
1757+{
1758+ switch (psp) {
1759+ case POWER_SUPPLY_PROP_ONLINE:
1760+ if (psy->type == POWER_SUPPLY_TYPE_MAINS)
1761+ val->intval = pdata->is_ac_online ?
1762+ pdata->is_ac_online() : 0;
1763+ else
1764+ val->intval = pdata->is_usb_online ?
1765+ pdata->is_usb_online() : 0;
1766+ break;
1767+ default:
1768+ return -EINVAL;
1769+ }
1770+ return 0;
1771+}
1772+
1773+static enum power_supply_property pda_power_props[] = {
1774+ POWER_SUPPLY_PROP_ONLINE,
1775+};
1776+
1777+static char *pda_power_supplied_to[] = {
1778+ "main-battery",
1779+ "backup-battery",
1780+};
1781+
1782+static struct power_supply pda_power_supplies[] = {
1783+ {
1784+ .name = "ac",
1785+ .type = POWER_SUPPLY_TYPE_MAINS,
1786+ .supplied_to = pda_power_supplied_to,
1787+ .num_supplicants = ARRAY_SIZE(pda_power_supplied_to),
1788+ .properties = pda_power_props,
1789+ .num_properties = ARRAY_SIZE(pda_power_props),
1790+ .get_property = pda_power_get_property,
1791+ },
1792+ {
1793+ .name = "usb",
1794+ .type = POWER_SUPPLY_TYPE_USB,
1795+ .supplied_to = pda_power_supplied_to,
1796+ .num_supplicants = ARRAY_SIZE(pda_power_supplied_to),
1797+ .properties = pda_power_props,
1798+ .num_properties = ARRAY_SIZE(pda_power_props),
1799+ .get_property = pda_power_get_property,
1800+ },
1801+};
1802+
1803+static void update_charger(void)
1804+{
1805+ if (!pdata->set_charge)
1806+ return;
1807+
1808+ if (pdata->is_ac_online && pdata->is_ac_online()) {
1809+ dev_dbg(dev, "charger on (AC)\n");
1810+ pdata->set_charge(PDA_POWER_CHARGE_AC);
1811+ }
1812+ else if (pdata->is_usb_online && pdata->is_usb_online()) {
1813+ dev_dbg(dev, "charger on (USB)\n");
1814+ pdata->set_charge(PDA_POWER_CHARGE_USB);
1815+ }
1816+ else {
1817+ dev_dbg(dev, "charger off\n");
1818+ pdata->set_charge(0);
1819+ }
1820+
1821+ return;
1822+}
1823+
1824+static void supply_timer_func(unsigned long irq)
1825+{
1826+ if (ac_irq && irq == ac_irq->start)
1827+ power_supply_changed(&pda_power_supplies[0]);
1828+ else if (usb_irq && irq == usb_irq->start)
1829+ power_supply_changed(&pda_power_supplies[1]);
1830+ return;
1831+}
1832+
1833+static void charger_timer_func(unsigned long irq)
1834+{
1835+ update_charger();
1836+
1837+ /* Okay, charger set. Now wait a bit before notifying supplicants,
1838+ * charge power should stabilize. */
1839+ supply_timer.data = irq;
1840+ mod_timer(&supply_timer,
1841+ jiffies + msecs_to_jiffies(pdata->wait_for_charger));
1842+ return;
1843+}
1844+
1845+static irqreturn_t power_changed_isr(int irq, void *unused)
1846+{
1847+ /* Wait a bit before reading ac/usb line status and setting charger,
1848+ * because ac/usb status readings may lag from irq. */
1849+ charger_timer.data = irq;
1850+ mod_timer(&charger_timer,
1851+ jiffies + msecs_to_jiffies(pdata->wait_for_status));
1852+ return IRQ_HANDLED;
1853+}
1854+
1855+static int pda_power_probe(struct platform_device *pdev)
1856+{
1857+ int ret = 0;
1858+
1859+ dev = &pdev->dev;
1860+
1861+ if (pdev->id != -1) {
1862+ dev_err(dev, "it's meaningless to register several "
1863+ "pda_powers, use id = -1\n");
1864+ ret = -EINVAL;
1865+ goto wrongid;
1866+ }
1867+
1868+ pdata = pdev->dev.platform_data;
1869+
1870+ update_charger();
1871+
1872+ if (!pdata->wait_for_status)
1873+ pdata->wait_for_status = 500;
1874+
1875+ if (!pdata->wait_for_charger)
1876+ pdata->wait_for_charger = 500;
1877+
1878+ setup_timer(&charger_timer, charger_timer_func, 0);
1879+ setup_timer(&supply_timer, supply_timer_func, 0);
1880+
1881+ ac_irq = platform_get_resource_byname(pdev, IORESOURCE_IRQ, "ac");
1882+ usb_irq = platform_get_resource_byname(pdev, IORESOURCE_IRQ, "usb");
1883+ if (!ac_irq && !usb_irq) {
1884+ dev_err(dev, "no ac/usb irq specified\n");
1885+ ret = -ENODEV;
1886+ goto noirqs;
1887+ }
1888+
1889+ if (pdata->supplied_to) {
1890+ pda_power_supplies[0].supplied_to = pdata->supplied_to;
1891+ pda_power_supplies[1].supplied_to = pdata->supplied_to;
1892+ pda_power_supplies[0].num_supplicants = pdata->num_supplicants;
1893+ pda_power_supplies[1].num_supplicants = pdata->num_supplicants;
1894+ }
1895+
1896+ ret = power_supply_register(&pdev->dev, &pda_power_supplies[0]);
1897+ if (ret) {
1898+ dev_err(dev, "failed to register %s power supply\n",
1899+ pda_power_supplies[0].name);
1900+ goto supply0_failed;
1901+ }
1902+
1903+ ret = power_supply_register(&pdev->dev, &pda_power_supplies[1]);
1904+ if (ret) {
1905+ dev_err(dev, "failed to register %s power supply\n",
1906+ pda_power_supplies[1].name);
1907+ goto supply1_failed;
1908+ }
1909+
1910+ if (ac_irq) {
1911+ ret = request_irq(ac_irq->start, power_changed_isr,
1912+ get_irq_flags(ac_irq), ac_irq->name,
1913+ &pda_power_supplies[0]);
1914+ if (ret) {
1915+ dev_err(dev, "request ac irq failed\n");
1916+ goto ac_irq_failed;
1917+ }
1918+ }
1919+
1920+ if (usb_irq) {
1921+ ret = request_irq(usb_irq->start, power_changed_isr,
1922+ get_irq_flags(usb_irq), usb_irq->name,
1923+ &pda_power_supplies[1]);
1924+ if (ret) {
1925+ dev_err(dev, "request usb irq failed\n");
1926+ goto usb_irq_failed;
1927+ }
1928+ }
1929+
1930+ goto success;
1931+
1932+usb_irq_failed:
1933+ if (ac_irq)
1934+ free_irq(ac_irq->start, &pda_power_supplies[0]);
1935+ac_irq_failed:
1936+ power_supply_unregister(&pda_power_supplies[1]);
1937+supply1_failed:
1938+ power_supply_unregister(&pda_power_supplies[0]);
1939+supply0_failed:
1940+noirqs:
1941+wrongid:
1942+success:
1943+ return ret;
1944+}
1945+
1946+static int pda_power_remove(struct platform_device *pdev)
1947+{
1948+ if (usb_irq)
1949+ free_irq(usb_irq->start, &pda_power_supplies[1]);
1950+ if (ac_irq)
1951+ free_irq(ac_irq->start, &pda_power_supplies[0]);
1952+ del_timer_sync(&charger_timer);
1953+ del_timer_sync(&supply_timer);
1954+ power_supply_unregister(&pda_power_supplies[1]);
1955+ power_supply_unregister(&pda_power_supplies[0]);
1956+ return 0;
1957+}
1958+
1959+static struct platform_driver pda_power_pdrv = {
1960+ .driver = {
1961+ .name = "pda-power",
1962+ },
1963+ .probe = pda_power_probe,
1964+ .remove = pda_power_remove,
1965+};
1966+
1967+static int __init pda_power_init(void)
1968+{
1969+ return platform_driver_register(&pda_power_pdrv);
1970+}
1971+
1972+static void __exit pda_power_exit(void)
1973+{
1974+ platform_driver_unregister(&pda_power_pdrv);
1975+ return;
1976+}
1977+
1978+module_init(pda_power_init);
1979+module_exit(pda_power_exit);
1980+MODULE_LICENSE("GPL");
1981+MODULE_AUTHOR("Anton Vorontsov <cbou@mail.ru>");
1982Index: linux-2.6.22/drivers/power/pmu_battery.c
1983===================================================================
1984--- /dev/null 1970-01-01 00:00:00.000000000 +0000
1985+++ linux-2.6.22/drivers/power/pmu_battery.c 2007-08-23 12:13:52.000000000 +0200
1986@@ -0,0 +1,215 @@
1987+/*
1988+ * Battery class driver for Apple PMU
1989+ *
1990+ * Copyright © 2006 David Woodhouse <dwmw2@infradead.org>
1991+ *
1992+ * This program is free software; you can redistribute it and/or modify
1993+ * it under the terms of the GNU General Public License version 2 as
1994+ * published by the Free Software Foundation.
1995+ */
1996+
1997+#include <linux/module.h>
1998+#include <linux/platform_device.h>
1999+#include <linux/err.h>
2000+#include <linux/power_supply.h>
2001+#include <linux/adb.h>
2002+#include <linux/pmu.h>
2003+
2004+static struct pmu_battery_dev {
2005+ struct power_supply bat;
2006+ struct pmu_battery_info *pbi;
2007+ char name[16];
2008+ int propval;
2009+} *pbats[PMU_MAX_BATTERIES];
2010+
2011+#define to_pmu_battery_dev(x) container_of(x, struct pmu_battery_dev, bat)
2012+
2013+/*********************************************************************
2014+ * Power
2015+ *********************************************************************/
2016+
2017+static int pmu_get_ac_prop(struct power_supply *psy,
2018+ enum power_supply_property psp,
2019+ union power_supply_propval *val)
2020+{
2021+ switch (psp) {
2022+ case POWER_SUPPLY_PROP_ONLINE:
2023+ val->intval = (!!(pmu_power_flags & PMU_PWR_AC_PRESENT)) ||
2024+ (pmu_battery_count == 0);
2025+ break;
2026+ default:
2027+ return -EINVAL;
2028+ }
2029+
2030+ return 0;
2031+}
2032+
2033+static enum power_supply_property pmu_ac_props[] = {
2034+ POWER_SUPPLY_PROP_ONLINE,
2035+};
2036+
2037+static struct power_supply pmu_ac = {
2038+ .name = "pmu-ac",
2039+ .type = POWER_SUPPLY_TYPE_MAINS,
2040+ .properties = pmu_ac_props,
2041+ .num_properties = ARRAY_SIZE(pmu_ac_props),
2042+ .get_property = pmu_get_ac_prop,
2043+};
2044+
2045+/*********************************************************************
2046+ * Battery properties
2047+ *********************************************************************/
2048+
2049+static char *pmu_batt_types[] = {
2050+ "Smart", "Comet", "Hooper", "Unknown"
2051+};
2052+
2053+static char *pmu_bat_get_model_name(struct pmu_battery_info *pbi)
2054+{
2055+ switch (pbi->flags & PMU_BATT_TYPE_MASK) {
2056+ case PMU_BATT_TYPE_SMART:
2057+ return pmu_batt_types[0];
2058+ case PMU_BATT_TYPE_COMET:
2059+ return pmu_batt_types[1];
2060+ case PMU_BATT_TYPE_HOOPER:
2061+ return pmu_batt_types[2];
2062+ default: break;
2063+ }
2064+ return pmu_batt_types[3];
2065+}
2066+
2067+static int pmu_bat_get_property(struct power_supply *psy,
2068+ enum power_supply_property psp,
2069+ union power_supply_propval *val)
2070+{
2071+ struct pmu_battery_dev *pbat = to_pmu_battery_dev(psy);
2072+ struct pmu_battery_info *pbi = pbat->pbi;
2073+
2074+ switch (psp) {
2075+ case POWER_SUPPLY_PROP_STATUS:
2076+ if (pbi->flags & PMU_BATT_CHARGING)
2077+ val->intval = POWER_SUPPLY_STATUS_CHARGING;
2078+ else
2079+ val->intval = POWER_SUPPLY_STATUS_DISCHARGING;
2080+ break;
2081+ case POWER_SUPPLY_PROP_PRESENT:
2082+ val->intval = !!(pbi->flags & PMU_BATT_PRESENT);
2083+ break;
2084+ case POWER_SUPPLY_PROP_MODEL_NAME:
2085+ val->strval = pmu_bat_get_model_name(pbi);
2086+ break;
2087+ case POWER_SUPPLY_PROP_ENERGY_AVG:
2088+ val->intval = pbi->charge * 1000; /* mWh -> µWh */
2089+ break;
2090+ case POWER_SUPPLY_PROP_ENERGY_FULL:
2091+ val->intval = pbi->max_charge * 1000; /* mWh -> µWh */
2092+ break;
2093+ case POWER_SUPPLY_PROP_CURRENT_AVG:
2094+ val->intval = pbi->amperage * 1000; /* mA -> µA */
2095+ break;
2096+ case POWER_SUPPLY_PROP_VOLTAGE_AVG:
2097+ val->intval = pbi->voltage * 1000; /* mV -> µV */
2098+ break;
2099+ case POWER_SUPPLY_PROP_TIME_TO_EMPTY_AVG:
2100+ val->intval = pbi->time_remaining;
2101+ break;
2102+ default:
2103+ return -EINVAL;
2104+ }
2105+
2106+ return 0;
2107+}
2108+
2109+static enum power_supply_property pmu_bat_props[] = {
2110+ POWER_SUPPLY_PROP_STATUS,
2111+ POWER_SUPPLY_PROP_PRESENT,
2112+ POWER_SUPPLY_PROP_MODEL_NAME,
2113+ POWER_SUPPLY_PROP_ENERGY_AVG,
2114+ POWER_SUPPLY_PROP_ENERGY_FULL,
2115+ POWER_SUPPLY_PROP_CURRENT_AVG,
2116+ POWER_SUPPLY_PROP_VOLTAGE_AVG,
2117+ POWER_SUPPLY_PROP_TIME_TO_EMPTY_AVG,
2118+};
2119+
2120+/*********************************************************************
2121+ * Initialisation
2122+ *********************************************************************/
2123+
2124+static struct platform_device *bat_pdev;
2125+
2126+static int __init pmu_bat_init(void)
2127+{
2128+ int ret;
2129+ int i;
2130+
2131+ bat_pdev = platform_device_register_simple("pmu-battery",
2132+ 0, NULL, 0);
2133+ if (IS_ERR(bat_pdev)) {
2134+ ret = PTR_ERR(bat_pdev);
2135+ goto pdev_register_failed;
2136+ }
2137+
2138+ ret = power_supply_register(&bat_pdev->dev, &pmu_ac);
2139+ if (ret)
2140+ goto ac_register_failed;
2141+
2142+ for (i = 0; i < pmu_battery_count; i++) {
2143+ struct pmu_battery_dev *pbat = kzalloc(sizeof(*pbat),
2144+ GFP_KERNEL);
2145+ if (!pbat)
2146+ break;
2147+
2148+ sprintf(pbat->name, "PMU battery %d", i);
2149+ pbat->bat.name = pbat->name;
2150+ pbat->bat.properties = pmu_bat_props;
2151+ pbat->bat.num_properties = ARRAY_SIZE(pmu_bat_props);
2152+ pbat->bat.get_property = pmu_bat_get_property;
2153+ pbat->pbi = &pmu_batteries[i];
2154+
2155+ ret = power_supply_register(&bat_pdev->dev, &pbat->bat);
2156+ if (ret) {
2157+ kfree(pbat);
2158+ goto battery_register_failed;
2159+ }
2160+ pbats[i] = pbat;
2161+ }
2162+
2163+ goto success;
2164+
2165+battery_register_failed:
2166+ while (i--) {
2167+ if (!pbats[i])
2168+ continue;
2169+ power_supply_unregister(&pbats[i]->bat);
2170+ kfree(pbats[i]);
2171+ }
2172+ power_supply_unregister(&pmu_ac);
2173+ac_register_failed:
2174+ platform_device_unregister(bat_pdev);
2175+pdev_register_failed:
2176+success:
2177+ return ret;
2178+}
2179+
2180+static void __exit pmu_bat_exit(void)
2181+{
2182+ int i;
2183+
2184+ for (i = 0; i < PMU_MAX_BATTERIES; i++) {
2185+ if (!pbats[i])
2186+ continue;
2187+ power_supply_unregister(&pbats[i]->bat);
2188+ kfree(pbats[i]);
2189+ }
2190+ power_supply_unregister(&pmu_ac);
2191+ platform_device_unregister(bat_pdev);
2192+
2193+ return;
2194+}
2195+
2196+module_init(pmu_bat_init);
2197+module_exit(pmu_bat_exit);
2198+
2199+MODULE_AUTHOR("David Woodhouse <dwmw2@infradead.org>");
2200+MODULE_LICENSE("GPL");
2201+MODULE_DESCRIPTION("PMU battery driver");
2202Index: linux-2.6.22/drivers/power/power_supply_core.c
2203===================================================================
2204--- /dev/null 1970-01-01 00:00:00.000000000 +0000
2205+++ linux-2.6.22/drivers/power/power_supply_core.c 2007-08-23 12:13:52.000000000 +0200
2206@@ -0,0 +1,168 @@
2207+/*
2208+ * Universal power supply monitor class
2209+ *
2210+ * Copyright (c) 2007 Anton Vorontsov <cbou@mail.ru>
2211+ * Copyright (c) 2004 Szabolcs Gyurko
2212+ * Copyright (c) 2003 Ian Molton <spyro@f2s.com>
2213+ *
2214+ * Modified: 2004, Oct Szabolcs Gyurko
2215+ *
2216+ * You may use this code as per GPL version 2
2217+ */
2218+
2219+#include <linux/module.h>
2220+#include <linux/types.h>
2221+#include <linux/init.h>
2222+#include <linux/device.h>
2223+#include <linux/err.h>
2224+#include <linux/power_supply.h>
2225+#include "power_supply.h"
2226+
2227+struct class *power_supply_class;
2228+
2229+static void power_supply_changed_work(struct work_struct *work)
2230+{
2231+ struct power_supply *psy = container_of(work, struct power_supply,
2232+ changed_work);
2233+ int i;
2234+
2235+ dev_dbg(psy->dev, "%s\n", __FUNCTION__);
2236+
2237+ for (i = 0; i < psy->num_supplicants; i++) {
2238+ struct device *dev;
2239+
2240+ down(&power_supply_class->sem);
2241+ list_for_each_entry(dev, &power_supply_class->devices, node) {
2242+ struct power_supply *pst = dev_get_drvdata(dev);
2243+
2244+ if (!strcmp(psy->supplied_to[i], pst->name)) {
2245+ if (pst->external_power_changed)
2246+ pst->external_power_changed(pst);
2247+ }
2248+ }
2249+ up(&power_supply_class->sem);
2250+ }
2251+
2252+ power_supply_update_leds(psy);
2253+
2254+ kobject_uevent(&psy->dev->kobj, KOBJ_CHANGE);
2255+
2256+ return;
2257+}
2258+
2259+void power_supply_changed(struct power_supply *psy)
2260+{
2261+ dev_dbg(psy->dev, "%s\n", __FUNCTION__);
2262+
2263+ schedule_work(&psy->changed_work);
2264+
2265+ return;
2266+}
2267+
2268+int power_supply_am_i_supplied(struct power_supply *psy)
2269+{
2270+ union power_supply_propval ret = {0,};
2271+ struct device *dev;
2272+
2273+ down(&power_supply_class->sem);
2274+ list_for_each_entry(dev, &power_supply_class->devices, node) {
2275+ struct power_supply *epsy = dev_get_drvdata(dev);
2276+ int i;
2277+
2278+ for (i = 0; i < epsy->num_supplicants; i++) {
2279+ if (!strcmp(epsy->supplied_to[i], psy->name)) {
2280+ if (epsy->get_property(epsy,
2281+ POWER_SUPPLY_PROP_ONLINE, &ret))
2282+ continue;
2283+ if (ret.intval)
2284+ goto out;
2285+ }
2286+ }
2287+ }
2288+out:
2289+ up(&power_supply_class->sem);
2290+
2291+ dev_dbg(psy->dev, "%s %d\n", __FUNCTION__, ret.intval);
2292+
2293+ return ret.intval;
2294+}
2295+
2296+int power_supply_register(struct device *parent, struct power_supply *psy)
2297+{
2298+ int rc = 0;
2299+
2300+ psy->dev = device_create(power_supply_class, parent, 0,
2301+ "%s", psy->name);
2302+ if (IS_ERR(psy->dev)) {
2303+ rc = PTR_ERR(psy->dev);
2304+ goto dev_create_failed;
2305+ }
2306+
2307+ dev_set_drvdata(psy->dev, psy);
2308+
2309+ INIT_WORK(&psy->changed_work, power_supply_changed_work);
2310+
2311+ rc = power_supply_create_attrs(psy);
2312+ if (rc)
2313+ goto create_attrs_failed;
2314+
2315+ rc = power_supply_create_triggers(psy);
2316+ if (rc)
2317+ goto create_triggers_failed;
2318+
2319+ power_supply_changed(psy);
2320+
2321+ goto success;
2322+
2323+create_triggers_failed:
2324+ power_supply_remove_attrs(psy);
2325+create_attrs_failed:
2326+ device_unregister(psy->dev);
2327+dev_create_failed:
2328+success:
2329+ return rc;
2330+}
2331+
2332+void power_supply_unregister(struct power_supply *psy)
2333+{
2334+ flush_scheduled_work();
2335+ power_supply_remove_triggers(psy);
2336+ power_supply_remove_attrs(psy);
2337+ device_unregister(psy->dev);
2338+ return;
2339+}
2340+
2341+static int __init power_supply_class_init(void)
2342+{
2343+ power_supply_class = class_create(THIS_MODULE, "power_supply");
2344+
2345+ if (IS_ERR(power_supply_class))
2346+ return PTR_ERR(power_supply_class);
2347+
2348+ power_supply_class->dev_uevent = power_supply_uevent;
2349+
2350+ return 0;
2351+}
2352+
2353+static void __exit power_supply_class_exit(void)
2354+{
2355+ class_destroy(power_supply_class);
2356+ return;
2357+}
2358+
2359+EXPORT_SYMBOL_GPL(power_supply_changed);
2360+EXPORT_SYMBOL_GPL(power_supply_am_i_supplied);
2361+EXPORT_SYMBOL_GPL(power_supply_register);
2362+EXPORT_SYMBOL_GPL(power_supply_unregister);
2363+
2364+/* exported for the APM Power driver, APM emulation */
2365+EXPORT_SYMBOL_GPL(power_supply_class);
2366+
2367+subsys_initcall(power_supply_class_init);
2368+module_exit(power_supply_class_exit);
2369+
2370+MODULE_DESCRIPTION("Universal power supply monitor class");
2371+MODULE_AUTHOR("Ian Molton <spyro@f2s.com>, "
2372+ "Szabolcs Gyurko, "
2373+ "Anton Vorontsov <cbou@mail.ru>");
2374+MODULE_LICENSE("GPL");
2375Index: linux-2.6.22/drivers/power/power_supply.h
2376===================================================================
2377--- /dev/null 1970-01-01 00:00:00.000000000 +0000
2378+++ linux-2.6.22/drivers/power/power_supply.h 2007-08-23 12:13:52.000000000 +0200
2379@@ -0,0 +1,42 @@
2380+/*
2381+ * Functions private to power supply class
2382+ *
2383+ * Copyright (c) 2007 Anton Vorontsov <cbou@mail.ru>
2384+ * Copyright (c) 2004 Szabolcs Gyurko
2385+ * Copyright (c) 2003 Ian Molton <spyro@f2s.com>
2386+ *
2387+ * Modified: 2004, Oct Szabolcs Gyurko
2388+ *
2389+ * You may use this code as per GPL version 2
2390+ */
2391+
2392+#ifdef CONFIG_SYSFS
2393+
2394+extern int power_supply_create_attrs(struct power_supply *psy);
2395+extern void power_supply_remove_attrs(struct power_supply *psy);
2396+extern int power_supply_uevent(struct device *dev, char **envp, int num_envp,
2397+ char *buffer, int buffer_size);
2398+
2399+#else
2400+
2401+static inline int power_supply_create_attrs(struct power_supply *psy)
2402+{ return 0; }
2403+static inline void power_supply_remove_attrs(struct power_supply *psy) {}
2404+#define power_supply_uevent NULL
2405+
2406+#endif /* CONFIG_SYSFS */
2407+
2408+#ifdef CONFIG_LEDS_TRIGGERS
2409+
2410+extern void power_supply_update_leds(struct power_supply *psy);
2411+extern int power_supply_create_triggers(struct power_supply *psy);
2412+extern void power_supply_remove_triggers(struct power_supply *psy);
2413+
2414+#else
2415+
2416+static inline void power_supply_update_leds(struct power_supply *psy) {}
2417+static inline int power_supply_create_triggers(struct power_supply *psy)
2418+{ return 0; }
2419+static inline void power_supply_remove_triggers(struct power_supply *psy) {}
2420+
2421+#endif /* CONFIG_LEDS_TRIGGERS */
2422Index: linux-2.6.22/drivers/power/power_supply_leds.c
2423===================================================================
2424--- /dev/null 1970-01-01 00:00:00.000000000 +0000
2425+++ linux-2.6.22/drivers/power/power_supply_leds.c 2007-08-23 12:13:52.000000000 +0200
2426@@ -0,0 +1,188 @@
2427+/*
2428+ * LEDs triggers for power supply class
2429+ *
2430+ * Copyright (c) 2007 Anton Vorontsov <cbou@mail.ru>
2431+ * Copyright (c) 2004 Szabolcs Gyurko
2432+ * Copyright (c) 2003 Ian Molton <spyro@f2s.com>
2433+ *
2434+ * Modified: 2004, Oct Szabolcs Gyurko
2435+ *
2436+ * You may use this code as per GPL version 2
2437+ */
2438+
2439+#include <linux/power_supply.h>
2440+
2441+/* If we have hwtimer trigger, then use it to blink charging LED */
2442+
2443+#if defined(CONFIG_LEDS_TRIGGER_HWTIMER) || \
2444+ (defined(CONFIG_BATTERY_MODULE) && \
2445+ defined(CONFIG_LEDS_TRIGGER_HWTIMER_MODULE))
2446+ #define led_trigger_register_charging led_trigger_register_hwtimer
2447+ #define led_trigger_unregister_charging led_trigger_unregister_hwtimer
2448+#else
2449+ #define led_trigger_register_charging led_trigger_register_simple
2450+ #define led_trigger_unregister_charging led_trigger_unregister_simple
2451+#endif
2452+
2453+/* Battery specific LEDs triggers. */
2454+
2455+static void power_supply_update_bat_leds(struct power_supply *psy)
2456+{
2457+ union power_supply_propval status;
2458+
2459+ if (psy->get_property(psy, POWER_SUPPLY_PROP_STATUS, &status))
2460+ return;
2461+
2462+ dev_dbg(psy->dev, "%s %d\n", __FUNCTION__, status.intval);
2463+
2464+ switch(status.intval) {
2465+ case POWER_SUPPLY_STATUS_FULL:
2466+ led_trigger_event(psy->charging_full_trig, LED_FULL);
2467+ led_trigger_event(psy->charging_trig, LED_OFF);
2468+ led_trigger_event(psy->full_trig, LED_FULL);
2469+ break;
2470+ case POWER_SUPPLY_STATUS_CHARGING:
2471+ led_trigger_event(psy->charging_full_trig, LED_FULL);
2472+ led_trigger_event(psy->charging_trig, LED_FULL);
2473+ led_trigger_event(psy->full_trig, LED_OFF);
2474+ break;
2475+ default:
2476+ led_trigger_event(psy->charging_full_trig, LED_OFF);
2477+ led_trigger_event(psy->charging_trig, LED_OFF);
2478+ led_trigger_event(psy->full_trig, LED_OFF);
2479+ break;
2480+ }
2481+
2482+ return;
2483+}
2484+
2485+static int power_supply_create_bat_triggers(struct power_supply *psy)
2486+{
2487+ int rc = 0;
2488+
2489+ psy->charging_full_trig_name = kmalloc(strlen(psy->name) +
2490+ sizeof("-charging-or-full"), GFP_KERNEL);
2491+ if (!psy->charging_full_trig_name)
2492+ goto charging_full_failed;
2493+
2494+ psy->charging_trig_name = kmalloc(strlen(psy->name) +
2495+ sizeof("-charging"), GFP_KERNEL);
2496+ if (!psy->charging_trig_name)
2497+ goto charging_failed;
2498+
2499+ psy->full_trig_name = kmalloc(strlen(psy->name) +
2500+ sizeof("-full"), GFP_KERNEL);
2501+ if (!psy->full_trig_name)
2502+ goto full_failed;
2503+
2504+ strcpy(psy->charging_full_trig_name, psy->name);
2505+ strcat(psy->charging_full_trig_name, "-charging-or-full");
2506+ strcpy(psy->charging_trig_name, psy->name);
2507+ strcat(psy->charging_trig_name, "-charging");
2508+ strcpy(psy->full_trig_name, psy->name);
2509+ strcat(psy->full_trig_name, "-full");
2510+
2511+ led_trigger_register_simple(psy->charging_full_trig_name,
2512+ &psy->charging_full_trig);
2513+ led_trigger_register_charging(psy->charging_trig_name,
2514+ &psy->charging_trig);
2515+ led_trigger_register_simple(psy->full_trig_name,
2516+ &psy->full_trig);
2517+
2518+ goto success;
2519+
2520+full_failed:
2521+ kfree(psy->charging_trig_name);
2522+charging_failed:
2523+ kfree(psy->charging_full_trig_name);
2524+charging_full_failed:
2525+ rc = -ENOMEM;
2526+success:
2527+ return rc;
2528+}
2529+
2530+static void power_supply_remove_bat_triggers(struct power_supply *psy)
2531+{
2532+ led_trigger_unregister_simple(psy->charging_full_trig);
2533+ led_trigger_unregister_charging(psy->charging_trig);
2534+ led_trigger_unregister_simple(psy->full_trig);
2535+ kfree(psy->full_trig_name);
2536+ kfree(psy->charging_trig_name);
2537+ kfree(psy->charging_full_trig_name);
2538+ return;
2539+}
2540+
2541+/* Generated power specific LEDs triggers. */
2542+
2543+static void power_supply_update_gen_leds(struct power_supply *psy)
2544+{
2545+ union power_supply_propval online;
2546+
2547+ if (psy->get_property(psy, POWER_SUPPLY_PROP_ONLINE, &online))
2548+ return;
2549+
2550+ dev_dbg(psy->dev, "%s %d\n", __FUNCTION__, online.intval);
2551+
2552+ if (online.intval)
2553+ led_trigger_event(psy->online_trig, LED_FULL);
2554+ else
2555+ led_trigger_event(psy->online_trig, LED_OFF);
2556+
2557+ return;
2558+}
2559+
2560+static int power_supply_create_gen_triggers(struct power_supply *psy)
2561+{
2562+ int rc = 0;
2563+
2564+ psy->online_trig_name = kmalloc(strlen(psy->name) + sizeof("-online"),
2565+ GFP_KERNEL);
2566+ if (!psy->online_trig_name)
2567+ goto online_failed;
2568+
2569+ strcpy(psy->online_trig_name, psy->name);
2570+ strcat(psy->online_trig_name, "-online");
2571+
2572+ led_trigger_register_simple(psy->online_trig_name, &psy->online_trig);
2573+
2574+ goto success;
2575+
2576+online_failed:
2577+ rc = -ENOMEM;
2578+success:
2579+ return rc;
2580+}
2581+
2582+static void power_supply_remove_gen_triggers(struct power_supply *psy)
2583+{
2584+ led_trigger_unregister_simple(psy->online_trig);
2585+ kfree(psy->online_trig_name);
2586+ return;
2587+}
2588+
2589+/* Choice what triggers to create&update. */
2590+
2591+void power_supply_update_leds(struct power_supply *psy)
2592+{
2593+ if (psy->type == POWER_SUPPLY_TYPE_BATTERY)
2594+ power_supply_update_bat_leds(psy);
2595+ else
2596+ power_supply_update_gen_leds(psy);
2597+ return;
2598+}
2599+
2600+int power_supply_create_triggers(struct power_supply *psy)
2601+{
2602+ if (psy->type == POWER_SUPPLY_TYPE_BATTERY)
2603+ return power_supply_create_bat_triggers(psy);
2604+ return power_supply_create_gen_triggers(psy);
2605+}
2606+
2607+void power_supply_remove_triggers(struct power_supply *psy)
2608+{
2609+ if (psy->type == POWER_SUPPLY_TYPE_BATTERY)
2610+ power_supply_remove_bat_triggers(psy);
2611+ else
2612+ power_supply_remove_gen_triggers(psy);
2613+ return;
2614+}
2615Index: linux-2.6.22/drivers/power/power_supply_sysfs.c
2616===================================================================
2617--- /dev/null 1970-01-01 00:00:00.000000000 +0000
2618+++ linux-2.6.22/drivers/power/power_supply_sysfs.c 2007-08-23 12:13:52.000000000 +0200
2619@@ -0,0 +1,289 @@
2620+/*
2621+ * Sysfs interface for the universal power supply monitor class
2622+ *
2623+ * Copyright © 2007 David Woodhouse <dwmw2@infradead.org>
2624+ * Copyright (c) 2007 Anton Vorontsov <cbou@mail.ru>
2625+ * Copyright (c) 2004 Szabolcs Gyurko
2626+ * Copyright (c) 2003 Ian Molton <spyro@f2s.com>
2627+ *
2628+ * Modified: 2004, Oct Szabolcs Gyurko
2629+ *
2630+ * You may use this code as per GPL version 2
2631+ */
2632+
2633+#include <linux/ctype.h>
2634+#include <linux/power_supply.h>
2635+
2636+/*
2637+ * This is because the name "current" breaks the device attr macro.
2638+ * The "current" word resolvs to "(get_current())" so instead of
2639+ * "current" "(get_current())" appears in the sysfs.
2640+ *
2641+ * The source of this definition is the device.h which calls __ATTR
2642+ * macro in sysfs.h which calls the __stringify macro.
2643+ *
2644+ * Only modification that the name is not tried to be resolved
2645+ * (as a macro let's say).
2646+ */
2647+
2648+#define POWER_SUPPLY_ATTR(_name) \
2649+{ \
2650+ .attr = { .name = #_name, .mode = 0444, .owner = THIS_MODULE }, \
2651+ .show = power_supply_show_property, \
2652+ .store = NULL, \
2653+}
2654+
2655+static struct device_attribute power_supply_attrs[];
2656+
2657+static ssize_t power_supply_show_property(struct device *dev,
2658+ struct device_attribute *attr,
2659+ char *buf) {
2660+ static char *status_text[] = {
2661+ "Unknown", "Charging", "Discharging", "Not charging", "Full"
2662+ };
2663+ static char *health_text[] = {
2664+ "Unknown", "Good", "Overheat", "Dead"
2665+ };
2666+ static char *technology_text[] = {
2667+ "Unknown", "NiMH", "Li-ion", "Li-poly"
2668+ };
2669+ static char *capacity_level_text[] = {
2670+ "Unknown", "Critical", "Low", "Normal", "High", "Full"
2671+ };
2672+ ssize_t ret;
2673+ struct power_supply *psy = dev_get_drvdata(dev);
2674+ const ptrdiff_t off = attr - power_supply_attrs;
2675+ union power_supply_propval value;
2676+
2677+ ret = psy->get_property(psy, off, &value);
2678+
2679+ if (ret < 0) {
2680+ dev_err(dev, "driver failed to report `%s' property\n",
2681+ attr->attr.name);
2682+ return ret;
2683+ }
2684+
2685+ if (off == POWER_SUPPLY_PROP_STATUS)
2686+ return sprintf(buf, "%s\n", status_text[value.intval]);
2687+ else if (off == POWER_SUPPLY_PROP_HEALTH)
2688+ return sprintf(buf, "%s\n", health_text[value.intval]);
2689+ else if (off == POWER_SUPPLY_PROP_TECHNOLOGY)
2690+ return sprintf(buf, "%s\n", technology_text[value.intval]);
2691+ else if (off == POWER_SUPPLY_PROP_CAPACITY_LEVEL)
2692+ return sprintf(buf, "%s\n",
2693+ capacity_level_text[value.intval]);
2694+ else if (off == POWER_SUPPLY_PROP_MODEL_NAME)
2695+ return sprintf(buf, "%s\n", value.strval);
2696+
2697+ return sprintf(buf, "%d\n", value.intval);
2698+}
2699+
2700+/* Must be in the same order as POWER_SUPPLY_PROP_* */
2701+static struct device_attribute power_supply_attrs[] = {
2702+ /* Properties of type `int' */
2703+ POWER_SUPPLY_ATTR(status),
2704+ POWER_SUPPLY_ATTR(health),
2705+ POWER_SUPPLY_ATTR(present),
2706+ POWER_SUPPLY_ATTR(online),
2707+ POWER_SUPPLY_ATTR(technology),
2708+ POWER_SUPPLY_ATTR(voltage_max_design),
2709+ POWER_SUPPLY_ATTR(voltage_min_design),
2710+ POWER_SUPPLY_ATTR(voltage_now),
2711+ POWER_SUPPLY_ATTR(voltage_avg),
2712+ POWER_SUPPLY_ATTR(current_now),
2713+ POWER_SUPPLY_ATTR(current_avg),
2714+ POWER_SUPPLY_ATTR(charge_full_design),
2715+ POWER_SUPPLY_ATTR(charge_empty_design),
2716+ POWER_SUPPLY_ATTR(charge_full),
2717+ POWER_SUPPLY_ATTR(charge_empty),
2718+ POWER_SUPPLY_ATTR(charge_now),
2719+ POWER_SUPPLY_ATTR(charge_avg),
2720+ POWER_SUPPLY_ATTR(energy_full_design),
2721+ POWER_SUPPLY_ATTR(energy_empty_design),
2722+ POWER_SUPPLY_ATTR(energy_full),
2723+ POWER_SUPPLY_ATTR(energy_empty),
2724+ POWER_SUPPLY_ATTR(energy_now),
2725+ POWER_SUPPLY_ATTR(energy_avg),
2726+ POWER_SUPPLY_ATTR(capacity),
2727+ POWER_SUPPLY_ATTR(capacity_level),
2728+ POWER_SUPPLY_ATTR(temp),
2729+ POWER_SUPPLY_ATTR(temp_ambient),
2730+ POWER_SUPPLY_ATTR(time_to_empty_now),
2731+ POWER_SUPPLY_ATTR(time_to_empty_avg),
2732+ POWER_SUPPLY_ATTR(time_to_full_now),
2733+ POWER_SUPPLY_ATTR(time_to_full_avg),
2734+ /* Properties of type `const char *' */
2735+ POWER_SUPPLY_ATTR(model_name),
2736+};
2737+
2738+static ssize_t power_supply_show_static_attrs(struct device *dev,
2739+ struct device_attribute *attr,
2740+ char *buf) {
2741+ static char *type_text[] = { "Battery", "UPS", "Mains", "USB" };
2742+ struct power_supply *psy = dev_get_drvdata(dev);
2743+
2744+ return sprintf(buf, "%s\n", type_text[psy->type]);
2745+}
2746+
2747+static struct device_attribute power_supply_static_attrs[] = {
2748+ __ATTR(type, 0444, power_supply_show_static_attrs, NULL),
2749+};
2750+
2751+int power_supply_create_attrs(struct power_supply *psy)
2752+{
2753+ int rc = 0;
2754+ int i, j;
2755+
2756+ for (i = 0; i < ARRAY_SIZE(power_supply_static_attrs); i++) {
2757+ rc = device_create_file(psy->dev,
2758+ &power_supply_static_attrs[i]);
2759+ if (rc)
2760+ goto statics_failed;
2761+ }
2762+
2763+ for (j = 0; j < psy->num_properties; j++) {
2764+ rc = device_create_file(psy->dev,
2765+ &power_supply_attrs[psy->properties[j]]);
2766+ if (rc)
2767+ goto dynamics_failed;
2768+ }
2769+
2770+ goto succeed;
2771+
2772+dynamics_failed:
2773+ while (j--)
2774+ device_remove_file(psy->dev,
2775+ &power_supply_attrs[psy->properties[j]]);
2776+statics_failed:
2777+ while (i--)
2778+ device_remove_file(psy->dev,
2779+ &power_supply_static_attrs[psy->properties[i]]);
2780+succeed:
2781+ return rc;
2782+}
2783+
2784+void power_supply_remove_attrs(struct power_supply *psy)
2785+{
2786+ int i;
2787+
2788+ for (i = 0; i < ARRAY_SIZE(power_supply_static_attrs); i++)
2789+ device_remove_file(psy->dev,
2790+ &power_supply_static_attrs[i]);
2791+
2792+ for (i = 0; i < psy->num_properties; i++)
2793+ device_remove_file(psy->dev,
2794+ &power_supply_attrs[psy->properties[i]]);
2795+
2796+ return;
2797+}
2798+
2799+static char *kstruprdup(const char *str, gfp_t gfp)
2800+{
2801+ char *ret, *ustr;
2802+
2803+ ustr = ret = kmalloc(strlen(str) + 1, gfp);
2804+
2805+ if (!ret)
2806+ return NULL;
2807+
2808+ while (*str)
2809+ *ustr++ = toupper(*str++);
2810+
2811+ *ustr = 0;
2812+
2813+ return ret;
2814+}
2815+
2816+int power_supply_uevent(struct device *dev, char **envp, int num_envp,
2817+ char *buffer, int buffer_size)
2818+{
2819+ struct power_supply *psy = dev_get_drvdata(dev);
2820+ int i = 0, length = 0, ret = 0, j;
2821+ char *prop_buf;
2822+ char *attrname;
2823+
2824+ dev_dbg(dev, "uevent\n");
2825+
2826+ if (!psy) {
2827+ dev_dbg(dev, "No power supply yet\n");
2828+ return ret;
2829+ }
2830+
2831+ dev_dbg(dev, "POWER_SUPPLY_NAME=%s\n", psy->name);
2832+
2833+ ret = add_uevent_var(envp, num_envp, &i, buffer, buffer_size,
2834+ &length, "POWER_SUPPLY_NAME=%s", psy->name);
2835+ if (ret)
2836+ return ret;
2837+
2838+ prop_buf = (char *)get_zeroed_page(GFP_KERNEL);
2839+ if (!prop_buf)
2840+ return -ENOMEM;
2841+
2842+ for (j = 0; j < ARRAY_SIZE(power_supply_static_attrs); j++) {
2843+ struct device_attribute *attr;
2844+ char *line;
2845+
2846+ attr = &power_supply_static_attrs[j];
2847+
2848+ ret = power_supply_show_static_attrs(dev, attr, prop_buf);
2849+ if (ret < 0)
2850+ goto out;
2851+
2852+ line = strchr(prop_buf, '\n');
2853+ if (line)
2854+ *line = 0;
2855+
2856+ attrname = kstruprdup(attr->attr.name, GFP_KERNEL);
2857+ if (!attrname) {
2858+ ret = -ENOMEM;
2859+ goto out;
2860+ }
2861+
2862+ dev_dbg(dev, "Static prop %s=%s\n", attrname, prop_buf);
2863+
2864+ ret = add_uevent_var(envp, num_envp, &i, buffer, buffer_size,
2865+ &length, "POWER_SUPPLY_%s=%s",
2866+ attrname, prop_buf);
2867+ kfree(attrname);
2868+ if (ret)
2869+ goto out;
2870+ }
2871+
2872+ dev_dbg(dev, "%zd dynamic props\n", psy->num_properties);
2873+
2874+ for (j = 0; j < psy->num_properties; j++) {
2875+ struct device_attribute *attr;
2876+ char *line;
2877+
2878+ attr = &power_supply_attrs[psy->properties[j]];
2879+
2880+ ret = power_supply_show_property(dev, attr, prop_buf);
2881+ if (ret < 0)
2882+ goto out;
2883+
2884+ line = strchr(prop_buf, '\n');
2885+ if (line)
2886+ *line = 0;
2887+
2888+ attrname = kstruprdup(attr->attr.name, GFP_KERNEL);
2889+ if (!attrname) {
2890+ ret = -ENOMEM;
2891+ goto out;
2892+ }
2893+
2894+ dev_dbg(dev, "prop %s=%s\n", attrname, prop_buf);
2895+
2896+ ret = add_uevent_var(envp, num_envp, &i, buffer, buffer_size,
2897+ &length, "POWER_SUPPLY_%s=%s",
2898+ attrname, prop_buf);
2899+ kfree(attrname);
2900+ if (ret)
2901+ goto out;
2902+ }
2903+
2904+out:
2905+ free_page((unsigned long)prop_buf);
2906+
2907+ return ret;
2908+}
2909Index: linux-2.6.22/drivers/power/simpad-battery.c
2910===================================================================
2911--- /dev/null 1970-01-01 00:00:00.000000000 +0000
2912+++ linux-2.6.22/drivers/power/simpad-battery.c 2007-08-23 12:13:52.000000000 +0200
2913@@ -0,0 +1,242 @@
2914+/*
2915+ * linux/drivers/misc/simpad-battery.c
2916+ *
2917+ * Copyright (C) 2005 Holger Hans Peter Freyther
2918+ * Copyright (C) 2001 Juergen Messerer
2919+ *
2920+ * This program is free software; you can redistribute it and/or modify
2921+ * it under the terms of the GNU General Public License as published by
2922+ * the Free Software Foundation; either version 2 of the License.
2923+ *
2924+ * Read the Battery Level through the UCB1x00 chip. T-Sinuspad is
2925+ * unsupported for now.
2926+ *
2927+ */
2928+
2929+#include <linux/battery.h>
2930+#include <asm/dma.h>
2931+#include "ucb1x00.h"
2932+
2933+
2934+/*
2935+ * Conversion from AD -> mV
2936+ * 7.5V = 1023 7.3313mV/Digit
2937+ *
2938+ * 400 Units == 9.7V
2939+ * a = ADC value
2940+ * 21 = ADC error
2941+ * 12600 = Divident to get 2*7.3242
2942+ * 860 = Divider to get 2*7.3242
2943+ * 170 = Voltagedrop over
2944+ */
2945+#define CALIBRATE_BATTERY(a) ((((a + 21)*12600)/860) + 170)
2946+
2947+/*
2948+ * We have two types of batteries a small and a large one
2949+ * To get the right value we to distinguish between those two
2950+ * 450 Units == 15 V
2951+ */
2952+#define CALIBRATE_SUPPLY(a) (((a) * 1500) / 45)
2953+#define MIN_SUPPLY 12000 /* Less then 12V means no powersupply */
2954+
2955+/*
2956+ * Charging Current
2957+ * if value is >= 50 then charging is on
2958+ */
2959+#define CALIBRATE_CHARGING(a) (((a)* 1000)/(152/4)))
2960+
2961+struct simpad_battery_t {
2962+ struct battery battery;
2963+ struct ucb1x00* ucb;
2964+
2965+ /*
2966+ * Variables for the values to one time support
2967+ * T-Sinuspad as well
2968+ */
2969+ int min_voltage;
2970+ int min_current;
2971+ int min_charge;
2972+
2973+ int max_voltage;
2974+ int max_current;
2975+ int max_charge;
2976+
2977+ int min_supply;
2978+ int charging_led_label;
2979+ int charging_max_label;
2980+ int batt_full;
2981+ int batt_low;
2982+ int batt_critical;
2983+ int batt_empty;
2984+};
2985+
2986+static int simpad_get_min_voltage(struct battery* _battery )
2987+{
2988+ struct simpad_battery_t* battery = (struct simpad_battery_t*)_battery;
2989+ return battery->min_voltage;
2990+}
2991+
2992+static int simpad_get_min_current(struct battery* _battery)
2993+{
2994+ struct simpad_battery_t* battery = (struct simpad_battery_t*)_battery;
2995+ return battery->min_current;
2996+}
2997+
2998+static int simpad_get_min_charge(struct battery* _battery)
2999+{
3000+ struct simpad_battery_t* battery = (struct simpad_battery_t*)_battery;
3001+ return battery->min_charge;
3002+}
3003+
3004+static int simpad_get_max_voltage(struct battery* _battery)
3005+{
3006+ struct simpad_battery_t* battery = (struct simpad_battery_t*)_battery;
3007+ return battery->max_voltage;
3008+}
3009+
3010+static int simpad_get_max_current(struct battery* _battery)
3011+{
3012+ struct simpad_battery_t* battery = (struct simpad_battery_t*)_battery;
3013+ return battery->max_current;
3014+}
3015+
3016+static int simpad_get_max_charge(struct battery* _battery)
3017+{
3018+ struct simpad_battery_t* battery = (struct simpad_battery_t*)_battery;
3019+ return battery->max_charge;
3020+}
3021+
3022+static int simpad_get_temp(struct battery* _battery)
3023+{
3024+ return 0;
3025+}
3026+
3027+static int simpad_get_voltage(struct battery* _battery)
3028+{
3029+ int val;
3030+ struct simpad_battery_t* battery = (struct simpad_battery_t*)_battery;
3031+
3032+
3033+ ucb1x00_adc_enable(battery->ucb);
3034+ val = ucb1x00_adc_read(battery->ucb, UCB_ADC_INP_AD1, UCB_NOSYNC);
3035+ ucb1x00_adc_disable(battery->ucb);
3036+
3037+ return CALIBRATE_BATTERY(val);
3038+}
3039+
3040+static int simpad_get_current(struct battery* _battery)
3041+{
3042+ int val;
3043+ struct simpad_battery_t* battery = (struct simpad_battery_t*)_battery;
3044+
3045+ ucb1x00_adc_enable(battery->ucb);
3046+ val = ucb1x00_adc_read(battery->ucb, UCB_ADC_INP_AD3, UCB_NOSYNC);
3047+ ucb1x00_adc_disable(battery->ucb);
3048+
3049+ return val;
3050+}
3051+
3052+static int simpad_get_charge(struct battery* _battery)
3053+{
3054+ int val;
3055+ struct simpad_battery_t* battery = (struct simpad_battery_t*)_battery;
3056+
3057+ ucb1x00_adc_enable(battery->ucb);
3058+ val = ucb1x00_adc_read(battery->ucb, UCB_ADC_INP_AD2, UCB_NOSYNC);
3059+ ucb1x00_adc_disable(battery->ucb);
3060+
3061+ return CALIBRATE_SUPPLY(val);
3062+
3063+}
3064+
3065+static int simpad_get_status(struct battery* _battery)
3066+{
3067+ struct simpad_battery_t* battery = (struct simpad_battery_t*)(_battery);
3068+ int vcharger = simpad_get_voltage(_battery);
3069+ int icharger = simpad_get_current(_battery);
3070+
3071+ int status = BATTERY_STATUS_UNKNOWN;
3072+ if(icharger > battery->charging_led_label)
3073+ status = BATTERY_STATUS_CHARGING;
3074+ else if(vcharger > battery->min_supply)
3075+ status = BATTERY_STATUS_NOT_CHARGING;
3076+ else
3077+ status = BATTERY_STATUS_DISCHARGING;
3078+
3079+ return status;
3080+}
3081+
3082+static struct simpad_battery_t simpad_battery = {
3083+ .battery = {
3084+ .get_min_voltage = simpad_get_min_voltage,
3085+ .get_min_current = simpad_get_min_current,
3086+ .get_min_charge = simpad_get_min_charge,
3087+ .get_max_voltage = simpad_get_max_voltage,
3088+ .get_max_current = simpad_get_max_current,
3089+ .get_max_charge = simpad_get_max_charge,
3090+ .get_temp = simpad_get_temp,
3091+ .get_voltage = simpad_get_voltage,
3092+ .get_current = simpad_get_current,
3093+ .get_charge = simpad_get_charge,
3094+ .get_status = simpad_get_status,
3095+ },
3096+ .min_voltage = 0,
3097+ .min_current = 0,
3098+ .min_charge = 0,
3099+ .max_voltage = 0,
3100+ .max_current = 0,
3101+ .max_charge = 0,
3102+
3103+ .min_supply = 1200,
3104+ .charging_led_label = 18,
3105+ .charging_max_label = 265,
3106+ .batt_full = 8300,
3107+ .batt_low = 7300,
3108+ .batt_critical = 6800,
3109+ .batt_empty = 6500,
3110+};
3111+
3112+
3113+
3114+/*
3115+ * UCB glue code
3116+ */
3117+static int ucb1x00_battery_add(struct class_device *dev)
3118+{
3119+ struct ucb1x00 *ucb = classdev_to_ucb1x00(dev);
3120+ simpad_battery.ucb = ucb;
3121+
3122+ battery_class_register(&simpad_battery.battery);
3123+
3124+ return 0;
3125+}
3126+
3127+static void ucb1x00_battery_remove(struct class_device *dev)
3128+{
3129+ return battery_class_unregister(&simpad_battery.battery);
3130+}
3131+
3132+
3133+static struct ucb1x00_class_interface ucb1x00_battery_interface = {
3134+ .interface = {
3135+ .add = ucb1x00_battery_add,
3136+ .remove = ucb1x00_battery_remove,
3137+ },
3138+};
3139+
3140+
3141+static int __init battery_register(void)
3142+{
3143+ return ucb1x00_register_interface(&ucb1x00_battery_interface);
3144+}
3145+
3146+static void __exit battery_unregister(void)
3147+{
3148+ ucb1x00_unregister_interface(&ucb1x00_battery_interface);
3149+}
3150+
3151+module_init(battery_register);
3152+module_exit(battery_unregister);
3153+
3154+MODULE_AUTHOR("Holger Hans Peter Freyther");
3155+MODULE_LICENSE("GPL");
3156Index: linux-2.6.22/arch/arm/Kconfig
3157===================================================================
3158--- linux-2.6.22.orig/arch/arm/Kconfig 2007-08-23 12:17:42.000000000 +0200
3159+++ linux-2.6.22/arch/arm/Kconfig 2007-08-23 12:22:28.000000000 +0200
3160@@ -1016,6 +1016,8 @@
3161
3162 source "drivers/w1/Kconfig"
3163
3164+source "drivers/power/Kconfig"
3165+
3166 source "drivers/hwmon/Kconfig"
3167
3168 #source "drivers/l3/Kconfig"
3169Index: linux-2.6.22/drivers/Kconfig
3170===================================================================
3171--- linux-2.6.22.orig/drivers/Kconfig 2007-08-23 12:21:27.000000000 +0200
3172+++ linux-2.6.22/drivers/Kconfig 2007-08-23 12:22:03.000000000 +0200
3173@@ -54,6 +54,8 @@
3174
3175 source "drivers/w1/Kconfig"
3176
3177+source "drivers/power/Kconfig"
3178+
3179 source "drivers/hwmon/Kconfig"
3180
3181 source "drivers/mfd/Kconfig"
3182Index: linux-2.6.22/drivers/Makefile
3183===================================================================
3184--- linux-2.6.22.orig/drivers/Makefile 2007-08-23 12:33:58.000000000 +0200
3185+++ linux-2.6.22/drivers/Makefile 2007-08-23 12:34:34.000000000 +0200
3186@@ -61,6 +61,7 @@
3187 obj-$(CONFIG_RTC_LIB) += rtc/
3188 obj-y += i2c/
3189 obj-$(CONFIG_W1) += w1/
3190+obj-$(CONFIG_POWER_SUPPLY) += power/
3191 obj-$(CONFIG_HWMON) += hwmon/
3192 obj-$(CONFIG_PHONE) += telephony/
3193 obj-$(CONFIG_MD) += md/
3194Index: linux-2.6.22/include/linux/power_supply.h
3195===================================================================
3196--- /dev/null 1970-01-01 00:00:00.000000000 +0000
3197+++ linux-2.6.22/include/linux/power_supply.h 2007-08-23 12:37:10.000000000 +0200
3198@@ -0,0 +1,175 @@
3199+/*
3200+ * Universal power supply monitor class
3201+ *
3202+ * Copyright (c) 2007 Anton Vorontsov <cbou@mail.ru>
3203+ * Copyright (c) 2004 Szabolcs Gyurko
3204+ * Copyright (c) 2003 Ian Molton <spyro@f2s.com>
3205+ *
3206+ * Modified: 2004, Oct Szabolcs Gyurko
3207+ *
3208+ * You may use this code as per GPL version 2
3209+ */
3210+
3211+#ifndef __LINUX_POWER_SUPPLY_H__
3212+#define __LINUX_POWER_SUPPLY_H__
3213+
3214+#include <linux/device.h>
3215+#include <linux/workqueue.h>
3216+#include <linux/leds.h>
3217+
3218+/*
3219+ * All voltages, currents, charges, energies, time and temperatures in uV,
3220+ * uA, uAh, uWh, seconds and tenths of degree Celsius unless otherwise
3221+ * stated. It's driver's job to convert its raw values to units in which
3222+ * this class operates.
3223+ */
3224+
3225+/*
3226+ * For systems where the charger determines the maximum battery capacity
3227+ * the min and max fields should be used to present these values to user
3228+ * space. Unused/unknown fields will not appear in sysfs.
3229+ */
3230+
3231+enum {
3232+ POWER_SUPPLY_STATUS_UNKNOWN = 0,
3233+ POWER_SUPPLY_STATUS_CHARGING,
3234+ POWER_SUPPLY_STATUS_DISCHARGING,
3235+ POWER_SUPPLY_STATUS_NOT_CHARGING,
3236+ POWER_SUPPLY_STATUS_FULL,
3237+};
3238+
3239+enum {
3240+ POWER_SUPPLY_HEALTH_UNKNOWN = 0,
3241+ POWER_SUPPLY_HEALTH_GOOD,
3242+ POWER_SUPPLY_HEALTH_OVERHEAT,
3243+ POWER_SUPPLY_HEALTH_DEAD,
3244+};
3245+
3246+enum {
3247+ POWER_SUPPLY_TECHNOLOGY_UNKNOWN = 0,
3248+ POWER_SUPPLY_TECHNOLOGY_NIMH,
3249+ POWER_SUPPLY_TECHNOLOGY_LION,
3250+ POWER_SUPPLY_TECHNOLOGY_LIPO,
3251+};
3252+
3253+enum {
3254+ POWER_SUPPLY_CAPACITY_LEVEL_UNKNOWN = 0,
3255+ POWER_SUPPLY_CAPACITY_LEVEL_CRITICAL,
3256+ POWER_SUPPLY_CAPACITY_LEVEL_LOW,
3257+ POWER_SUPPLY_CAPACITY_LEVEL_NORMAL,
3258+ POWER_SUPPLY_CAPACITY_LEVEL_HIGH,
3259+ POWER_SUPPLY_CAPACITY_LEVEL_FULL,
3260+};
3261+
3262+enum power_supply_property {
3263+ /* Properties of type `int' */
3264+ POWER_SUPPLY_PROP_STATUS = 0,
3265+ POWER_SUPPLY_PROP_HEALTH,
3266+ POWER_SUPPLY_PROP_PRESENT,
3267+ POWER_SUPPLY_PROP_ONLINE,
3268+ POWER_SUPPLY_PROP_TECHNOLOGY,
3269+ POWER_SUPPLY_PROP_VOLTAGE_MAX_DESIGN,
3270+ POWER_SUPPLY_PROP_VOLTAGE_MIN_DESIGN,
3271+ POWER_SUPPLY_PROP_VOLTAGE_NOW,
3272+ POWER_SUPPLY_PROP_VOLTAGE_AVG,
3273+ POWER_SUPPLY_PROP_CURRENT_NOW,
3274+ POWER_SUPPLY_PROP_CURRENT_AVG,
3275+ POWER_SUPPLY_PROP_CHARGE_FULL_DESIGN,
3276+ POWER_SUPPLY_PROP_CHARGE_EMPTY_DESIGN,
3277+ POWER_SUPPLY_PROP_CHARGE_FULL,
3278+ POWER_SUPPLY_PROP_CHARGE_EMPTY,
3279+ POWER_SUPPLY_PROP_CHARGE_NOW,
3280+ POWER_SUPPLY_PROP_CHARGE_AVG,
3281+ POWER_SUPPLY_PROP_ENERGY_FULL_DESIGN,
3282+ POWER_SUPPLY_PROP_ENERGY_EMPTY_DESIGN,
3283+ POWER_SUPPLY_PROP_ENERGY_FULL,
3284+ POWER_SUPPLY_PROP_ENERGY_EMPTY,
3285+ POWER_SUPPLY_PROP_ENERGY_NOW,
3286+ POWER_SUPPLY_PROP_ENERGY_AVG,
3287+ POWER_SUPPLY_PROP_CAPACITY, /* in percents! */
3288+ POWER_SUPPLY_PROP_CAPACITY_LEVEL,
3289+ POWER_SUPPLY_PROP_TEMP,
3290+ POWER_SUPPLY_PROP_TEMP_AMBIENT,
3291+ POWER_SUPPLY_PROP_TIME_TO_EMPTY_NOW,
3292+ POWER_SUPPLY_PROP_TIME_TO_EMPTY_AVG,
3293+ POWER_SUPPLY_PROP_TIME_TO_FULL_NOW,
3294+ POWER_SUPPLY_PROP_TIME_TO_FULL_AVG,
3295+ /* Properties of type `const char *' */
3296+ POWER_SUPPLY_PROP_MODEL_NAME,
3297+};
3298+
3299+enum power_supply_type {
3300+ POWER_SUPPLY_TYPE_BATTERY = 0,
3301+ POWER_SUPPLY_TYPE_UPS,
3302+ POWER_SUPPLY_TYPE_MAINS,
3303+ POWER_SUPPLY_TYPE_USB,
3304+};
3305+
3306+union power_supply_propval {
3307+ int intval;
3308+ const char *strval;
3309+};
3310+
3311+struct power_supply {
3312+ const char *name;
3313+ enum power_supply_type type;
3314+ enum power_supply_property *properties;
3315+ size_t num_properties;
3316+
3317+ char **supplied_to;
3318+ size_t num_supplicants;
3319+
3320+ int (*get_property)(struct power_supply *psy,
3321+ enum power_supply_property psp,
3322+ union power_supply_propval *val);
3323+ void (*external_power_changed)(struct power_supply *psy);
3324+
3325+ /* For APM emulation, think legacy userspace. */
3326+ int use_for_apm;
3327+
3328+ /* private */
3329+ struct device *dev;
3330+ struct work_struct changed_work;
3331+
3332+#ifdef CONFIG_LEDS_TRIGGERS
3333+ struct led_trigger *charging_full_trig;
3334+ char *charging_full_trig_name;
3335+ struct led_trigger *charging_trig;
3336+ char *charging_trig_name;
3337+ struct led_trigger *full_trig;
3338+ char *full_trig_name;
3339+ struct led_trigger *online_trig;
3340+ char *online_trig_name;
3341+#endif
3342+};
3343+
3344+/*
3345+ * This is recommended structure to specify static power supply parameters.
3346+ * Generic one, parametrizable for different power supplies. Power supply
3347+ * class itself does not use it, but that's what implementing most platform
3348+ * drivers, should try reuse for consistency.
3349+ */
3350+
3351+struct power_supply_info {
3352+ const char *name;
3353+ int technology;
3354+ int voltage_max_design;
3355+ int voltage_min_design;
3356+ int charge_full_design;
3357+ int charge_empty_design;
3358+ int energy_full_design;
3359+ int energy_empty_design;
3360+ int use_for_apm;
3361+};
3362+
3363+extern void power_supply_changed(struct power_supply *psy);
3364+extern int power_supply_am_i_supplied(struct power_supply *psy);
3365+
3366+extern int power_supply_register(struct device *parent,
3367+ struct power_supply *psy);
3368+extern void power_supply_unregister(struct power_supply *psy);
3369+
3370+/* For APM emulation, think legacy userspace. */
3371+extern struct class *power_supply_class;
3372+
3373+#endif /* __LINUX_POWER_SUPPLY_H__ */