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 struct class *power_supply_class;
23 static void power_supply_changed_work(struct work_struct *work)
25 struct power_supply *psy = container_of(work, struct power_supply,
29 dev_dbg(psy->dev, "%s\n", __FUNCTION__);
31 for (i = 0; i < psy->num_supplicants; i++) {
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);
38 if (!strcmp(psy->supplied_to[i], pst->name)) {
39 if (pst->external_power_changed)
40 pst->external_power_changed(pst);
43 up(&power_supply_class->sem);
46 power_supply_update_leds(psy);
48 kobject_uevent(&psy->dev->kobj, KOBJ_CHANGE);
51 void power_supply_changed(struct power_supply *psy)
53 dev_dbg(psy->dev, "%s\n", __FUNCTION__);
55 schedule_work(&psy->changed_work);
58 int power_supply_am_i_supplied(struct power_supply *psy)
60 union power_supply_propval ret = {0,};
63 down(&power_supply_class->sem);
64 list_for_each_entry(dev, &power_supply_class->devices, node) {
65 struct power_supply *epsy = dev_get_drvdata(dev);
68 for (i = 0; i < epsy->num_supplicants; i++) {
69 if (!strcmp(epsy->supplied_to[i], psy->name)) {
70 if (epsy->get_property(epsy,
71 POWER_SUPPLY_PROP_ONLINE, &ret))
79 up(&power_supply_class->sem);
81 dev_dbg(psy->dev, "%s %d\n", __FUNCTION__, ret.intval);
86 int power_supply_register(struct device *parent, struct power_supply *psy)
90 psy->dev = device_create(power_supply_class, parent, 0,
92 if (IS_ERR(psy->dev)) {
93 rc = PTR_ERR(psy->dev);
94 goto dev_create_failed;
97 dev_set_drvdata(psy->dev, psy);
99 INIT_WORK(&psy->changed_work, power_supply_changed_work);
101 rc = power_supply_create_attrs(psy);
103 goto create_attrs_failed;
105 rc = power_supply_create_triggers(psy);
107 goto create_triggers_failed;
109 power_supply_changed(psy);
113 create_triggers_failed:
114 power_supply_remove_attrs(psy);
116 device_unregister(psy->dev);
122 void power_supply_unregister(struct power_supply *psy)
124 flush_scheduled_work();
125 power_supply_remove_triggers(psy);
126 power_supply_remove_attrs(psy);
127 device_unregister(psy->dev);
130 static int __init power_supply_class_init(void)
132 power_supply_class = class_create(THIS_MODULE, "power_supply");
134 if (IS_ERR(power_supply_class))
135 return PTR_ERR(power_supply_class);
137 power_supply_class->dev_uevent = power_supply_uevent;
142 static void __exit power_supply_class_exit(void)
144 class_destroy(power_supply_class);
147 EXPORT_SYMBOL_GPL(power_supply_changed);
148 EXPORT_SYMBOL_GPL(power_supply_am_i_supplied);
149 EXPORT_SYMBOL_GPL(power_supply_register);
150 EXPORT_SYMBOL_GPL(power_supply_unregister);
152 /* exported for the APM Power driver, APM emulation */
153 EXPORT_SYMBOL_GPL(power_supply_class);
155 subsys_initcall(power_supply_class_init);
156 module_exit(power_supply_class_exit);
158 MODULE_DESCRIPTION("Universal power supply monitor class");
159 MODULE_AUTHOR("Ian Molton <spyro@f2s.com>, "
161 "Anton Vorontsov <cbou@mail.ru>");
162 MODULE_LICENSE("GPL");