2 * Universal power supply monitor class
4 * Copyright © 2007 Anton Vorontsov <cbou@mail.ru>
5 * Copyright © 2004 Szabolcs Gyurko
6 * Copyright © 2003 Ian Molton <spyro@f2s.com>
8 * Modified: 2004, Oct Szabolcs Gyurko
10 * You may use this code as per GPL version 2
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"
21 /* exported for the APM Power driver, APM emulation */
22 struct class *power_supply_class;
23 EXPORT_SYMBOL_GPL(power_supply_class);
25 static int __power_supply_changed_work(struct device *dev, void *data)
27 struct power_supply *psy = (struct power_supply *)data;
28 struct power_supply *pst = dev_get_drvdata(dev);
31 for (i = 0; i < psy->num_supplicants; i++)
32 if (!strcmp(psy->supplied_to[i], pst->name)) {
33 if (pst->external_power_changed)
34 pst->external_power_changed(pst);
39 static void power_supply_changed_work(struct work_struct *work)
42 struct power_supply *psy = container_of(work, struct power_supply,
45 dev_dbg(psy->dev, "%s\n", __func__);
47 spin_lock_irqsave(&psy->changed_lock, flags);
50 spin_unlock_irqrestore(&psy->changed_lock, flags);
52 class_for_each_device(power_supply_class, NULL, psy,
53 __power_supply_changed_work);
55 power_supply_update_leds(psy);
57 kobject_uevent(&psy->dev->kobj, KOBJ_CHANGE);
58 spin_lock_irqsave(&psy->changed_lock, flags);
61 wake_unlock(&psy->work_wake_lock);
62 spin_unlock_irqrestore(&psy->changed_lock, flags);
65 void power_supply_changed(struct power_supply *psy)
69 dev_dbg(psy->dev, "%s\n", __func__);
71 spin_lock_irqsave(&psy->changed_lock, flags);
73 wake_lock(&psy->work_wake_lock);
74 spin_unlock_irqrestore(&psy->changed_lock, flags);
75 schedule_work(&psy->changed_work);
77 EXPORT_SYMBOL_GPL(power_supply_changed);
79 static int __power_supply_am_i_supplied(struct device *dev, void *data)
81 union power_supply_propval ret = {0,};
82 struct power_supply *psy = (struct power_supply *)data;
83 struct power_supply *epsy = dev_get_drvdata(dev);
86 for (i = 0; i < epsy->num_supplicants; i++) {
87 if (!strcmp(epsy->supplied_to[i], psy->name)) {
88 if (epsy->get_property(epsy,
89 POWER_SUPPLY_PROP_ONLINE, &ret))
98 int power_supply_am_i_supplied(struct power_supply *psy)
102 error = class_for_each_device(power_supply_class, NULL, psy,
103 __power_supply_am_i_supplied);
105 dev_dbg(psy->dev, "%s %d\n", __func__, error);
109 EXPORT_SYMBOL_GPL(power_supply_am_i_supplied);
111 static int __power_supply_is_system_supplied(struct device *dev, void *data)
113 union power_supply_propval ret = {0,};
114 struct power_supply *psy = dev_get_drvdata(dev);
116 if (psy->type != POWER_SUPPLY_TYPE_BATTERY) {
117 if (psy->get_property(psy, POWER_SUPPLY_PROP_ONLINE, &ret))
125 int power_supply_is_system_supplied(void)
129 error = class_for_each_device(power_supply_class, NULL, NULL,
130 __power_supply_is_system_supplied);
134 EXPORT_SYMBOL_GPL(power_supply_is_system_supplied);
136 int power_supply_set_battery_charged(struct power_supply *psy)
138 if (psy->type == POWER_SUPPLY_TYPE_BATTERY && psy->set_charged) {
139 psy->set_charged(psy);
145 EXPORT_SYMBOL_GPL(power_supply_set_battery_charged);
147 static int power_supply_match_device_by_name(struct device *dev, void *data)
149 const char *name = data;
150 struct power_supply *psy = dev_get_drvdata(dev);
152 return strcmp(psy->name, name) == 0;
155 struct power_supply *power_supply_get_by_name(char *name)
157 struct device *dev = class_find_device(power_supply_class, NULL, name,
158 power_supply_match_device_by_name);
160 return dev ? dev_get_drvdata(dev) : NULL;
162 EXPORT_SYMBOL_GPL(power_supply_get_by_name);
164 int power_supply_register(struct device *parent, struct power_supply *psy)
168 psy->dev = device_create(power_supply_class, parent, 0, psy,
170 if (IS_ERR(psy->dev)) {
171 rc = PTR_ERR(psy->dev);
172 goto dev_create_failed;
175 INIT_WORK(&psy->changed_work, power_supply_changed_work);
176 spin_lock_init(&psy->changed_lock);
177 wake_lock_init(&psy->work_wake_lock, WAKE_LOCK_SUSPEND, "power-supply");
179 rc = power_supply_create_attrs(psy);
181 goto create_attrs_failed;
183 rc = power_supply_create_triggers(psy);
185 goto create_triggers_failed;
187 power_supply_changed(psy);
191 create_triggers_failed:
192 power_supply_remove_attrs(psy);
194 wake_lock_destroy(&psy->work_wake_lock);
195 device_unregister(psy->dev);
200 EXPORT_SYMBOL_GPL(power_supply_register);
202 void power_supply_unregister(struct power_supply *psy)
204 flush_scheduled_work();
205 power_supply_remove_triggers(psy);
206 power_supply_remove_attrs(psy);
207 wake_lock_destroy(&psy->work_wake_lock);
208 device_unregister(psy->dev);
210 EXPORT_SYMBOL_GPL(power_supply_unregister);
212 static int __init power_supply_class_init(void)
214 power_supply_class = class_create(THIS_MODULE, "power_supply");
216 if (IS_ERR(power_supply_class))
217 return PTR_ERR(power_supply_class);
219 power_supply_class->dev_uevent = power_supply_uevent;
224 static void __exit power_supply_class_exit(void)
226 class_destroy(power_supply_class);
229 subsys_initcall(power_supply_class_init);
230 module_exit(power_supply_class_exit);
232 MODULE_DESCRIPTION("Universal power supply monitor class");
233 MODULE_AUTHOR("Ian Molton <spyro@f2s.com>, "
235 "Anton Vorontsov <cbou@mail.ru>");
236 MODULE_LICENSE("GPL");