diff options
author | Anton Vorontsov <cbou@mail.ru> | 2007-05-03 16:27:45 -0400 |
---|---|---|
committer | David Woodhouse <dwmw2@infradead.org> | 2007-07-10 06:25:44 -0400 |
commit | 4a11b59d8283662193a9c6a9c14c58d1b9bf0617 (patch) | |
tree | 492bf022c806ec6c554ca5f8e7c05ea6db138137 /drivers/power/power_supply_core.c | |
parent | 7dcca30a32aadb0520417521b0c44f42d09fe05c (diff) |
[BATTERY] Universal power supply class (was: battery class)
This class is result of "external power" and "battery" classes merge,
as suggested by David Woodhouse. He also implemented uevent support.
Here how userspace seeing it now:
# ls /sys/class/power\ supply/
ac main-battery usb
# cat /sys/class/power\ supply/ac/type
AC
# cat /sys/class/power\ supply/usb/type
USB
# cat /sys/class/power\ supply/main-battery/type
Battery
# cat /sys/class/power\ supply/ac/online
1
# cat /sys/class/power\ supply/usb/online
0
# cat /sys/class/power\ supply/main-battery/status
Charging
# cat /sys/class/leds/h5400\:red-left/trigger
none h5400-radio timer hwtimer ac-online usb-online
main-battery-charging-or-full [main-battery-charging]
main-battery-full
Signed-off-by: Anton Vorontsov <cbou@mail.ru>
Signed-off-by: David Woodhouse <dwmw2@infradead.org>
Signed-off-by: Andrew Morton <akpm@linux-foundation.org>
Diffstat (limited to 'drivers/power/power_supply_core.c')
-rw-r--r-- | drivers/power/power_supply_core.c | 168 |
1 files changed, 168 insertions, 0 deletions
diff --git a/drivers/power/power_supply_core.c b/drivers/power/power_supply_core.c new file mode 100644 index 000000000000..e87ea5156755 --- /dev/null +++ b/drivers/power/power_supply_core.c | |||
@@ -0,0 +1,168 @@ | |||
1 | /* | ||
2 | * Universal power supply monitor class | ||
3 | * | ||
4 | * Copyright © 2007 Anton Vorontsov <cbou@mail.ru> | ||
5 | * Copyright © 2004 Szabolcs Gyurko | ||
6 | * Copyright © 2003 Ian Molton <spyro@f2s.com> | ||
7 | * | ||
8 | * Modified: 2004, Oct Szabolcs Gyurko | ||
9 | * | ||
10 | * You may use this code as per GPL version 2 | ||
11 | */ | ||
12 | |||
13 | #include <linux/module.h> | ||
14 | #include <linux/types.h> | ||
15 | #include <linux/init.h> | ||
16 | #include <linux/device.h> | ||
17 | #include <linux/err.h> | ||
18 | #include <linux/power_supply.h> | ||
19 | #include "power_supply.h" | ||
20 | |||
21 | struct class *power_supply_class; | ||
22 | |||
23 | static void power_supply_changed_work(struct work_struct *work) | ||
24 | { | ||
25 | struct power_supply *psy = container_of(work, struct power_supply, | ||
26 | changed_work); | ||
27 | int i; | ||
28 | |||
29 | dev_dbg(psy->dev, "%s\n", __FUNCTION__); | ||
30 | |||
31 | for (i = 0; i < psy->num_supplicants; i++) { | ||
32 | struct device *dev; | ||
33 | |||
34 | down(&power_supply_class->sem); | ||
35 | list_for_each_entry(dev, &power_supply_class->devices, node) { | ||
36 | struct power_supply *pst = dev_get_drvdata(dev); | ||
37 | |||
38 | if (!strcmp(psy->supplied_to[i], pst->name)) { | ||
39 | if (pst->external_power_changed) | ||
40 | pst->external_power_changed(pst); | ||
41 | } | ||
42 | } | ||
43 | up(&power_supply_class->sem); | ||
44 | } | ||
45 | |||
46 | power_supply_update_leds(psy); | ||
47 | |||
48 | kobject_uevent(&psy->dev->kobj, KOBJ_CHANGE); | ||
49 | |||
50 | return; | ||
51 | } | ||
52 | |||
53 | void power_supply_changed(struct power_supply *psy) | ||
54 | { | ||
55 | dev_dbg(psy->dev, "%s\n", __FUNCTION__); | ||
56 | |||
57 | schedule_work(&psy->changed_work); | ||
58 | |||
59 | return; | ||
60 | } | ||
61 | |||
62 | int power_supply_am_i_supplied(struct power_supply *psy) | ||
63 | { | ||
64 | union power_supply_propval ret = {0,}; | ||
65 | struct device *dev; | ||
66 | |||
67 | down(&power_supply_class->sem); | ||
68 | list_for_each_entry(dev, &power_supply_class->devices, node) { | ||
69 | struct power_supply *epsy = dev_get_drvdata(dev); | ||
70 | int i; | ||
71 | |||
72 | for (i = 0; i < epsy->num_supplicants; i++) { | ||
73 | if (!strcmp(epsy->supplied_to[i], psy->name)) { | ||
74 | if (epsy->get_property(epsy, | ||
75 | POWER_SUPPLY_PROP_ONLINE, &ret)) | ||
76 | continue; | ||
77 | if (ret.intval) | ||
78 | goto out; | ||
79 | } | ||
80 | } | ||
81 | } | ||
82 | out: | ||
83 | up(&power_supply_class->sem); | ||
84 | |||
85 | dev_dbg(psy->dev, "%s %d\n", __FUNCTION__, ret.intval); | ||
86 | |||
87 | return ret.intval; | ||
88 | } | ||
89 | |||
90 | int power_supply_register(struct device *parent, struct power_supply *psy) | ||
91 | { | ||
92 | int rc = 0; | ||
93 | |||
94 | psy->dev = device_create(power_supply_class, parent, 0, | ||
95 | "%s", psy->name); | ||
96 | if (IS_ERR(psy->dev)) { | ||
97 | rc = PTR_ERR(psy->dev); | ||
98 | goto dev_create_failed; | ||
99 | } | ||
100 | |||
101 | dev_set_drvdata(psy->dev, psy); | ||
102 | |||
103 | INIT_WORK(&psy->changed_work, power_supply_changed_work); | ||
104 | |||
105 | rc = power_supply_create_attrs(psy); | ||
106 | if (rc) | ||
107 | goto create_attrs_failed; | ||
108 | |||
109 | rc = power_supply_create_triggers(psy); | ||
110 | if (rc) | ||
111 | goto create_triggers_failed; | ||
112 | |||
113 | power_supply_changed(psy); | ||
114 | |||
115 | goto success; | ||
116 | |||
117 | create_triggers_failed: | ||
118 | power_supply_remove_attrs(psy); | ||
119 | create_attrs_failed: | ||
120 | device_unregister(psy->dev); | ||
121 | dev_create_failed: | ||
122 | success: | ||
123 | return rc; | ||
124 | } | ||
125 | |||
126 | void power_supply_unregister(struct power_supply *psy) | ||
127 | { | ||
128 | flush_scheduled_work(); | ||
129 | power_supply_remove_triggers(psy); | ||
130 | power_supply_remove_attrs(psy); | ||
131 | device_unregister(psy->dev); | ||
132 | return; | ||
133 | } | ||
134 | |||
135 | static int __init power_supply_class_init(void) | ||
136 | { | ||
137 | power_supply_class = class_create(THIS_MODULE, "power_supply"); | ||
138 | |||
139 | if (IS_ERR(power_supply_class)) | ||
140 | return PTR_ERR(power_supply_class); | ||
141 | |||
142 | power_supply_class->dev_uevent = power_supply_uevent; | ||
143 | |||
144 | return 0; | ||
145 | } | ||
146 | |||
147 | static void __exit power_supply_class_exit(void) | ||
148 | { | ||
149 | class_destroy(power_supply_class); | ||
150 | return; | ||
151 | } | ||
152 | |||
153 | EXPORT_SYMBOL_GPL(power_supply_changed); | ||
154 | EXPORT_SYMBOL_GPL(power_supply_am_i_supplied); | ||
155 | EXPORT_SYMBOL_GPL(power_supply_register); | ||
156 | EXPORT_SYMBOL_GPL(power_supply_unregister); | ||
157 | |||
158 | /* exported for the APM Power driver, APM emulation */ | ||
159 | EXPORT_SYMBOL_GPL(power_supply_class); | ||
160 | |||
161 | subsys_initcall(power_supply_class_init); | ||
162 | module_exit(power_supply_class_exit); | ||
163 | |||
164 | MODULE_DESCRIPTION("Universal power supply monitor class"); | ||
165 | MODULE_AUTHOR("Ian Molton <spyro@f2s.com>, " | ||
166 | "Szabolcs Gyurko, " | ||
167 | "Anton Vorontsov <cbou@mail.ru>"); | ||
168 | MODULE_LICENSE("GPL"); | ||