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 int __power_supply_changed_work(struct device *dev, void *data)
25 struct power_supply *psy = (struct power_supply *)data;
26 struct power_supply *pst = dev_get_drvdata(dev);
29 for (i = 0; i < psy->num_supplicants; i++)
30 if (!strcmp(psy->supplied_to[i], pst->name)) {
31 if (pst->external_power_changed)
32 pst->external_power_changed(pst);
37 static void power_supply_changed_work(struct work_struct *work)
39 struct power_supply *psy = container_of(work, struct power_supply,
42 dev_dbg(psy->dev, "%s\n", __func__);
44 class_for_each_device(power_supply_class, NULL, psy,
45 __power_supply_changed_work);
47 power_supply_update_leds(psy);
49 kobject_uevent(&psy->dev->kobj, KOBJ_CHANGE);
52 void power_supply_changed(struct power_supply *psy)
54 dev_dbg(psy->dev, "%s\n", __func__);
56 schedule_work(&psy->changed_work);
59 static int __power_supply_am_i_supplied(struct device *dev, void *data)
61 union power_supply_propval ret = {0,};
62 struct power_supply *psy = (struct power_supply *)data;
63 struct power_supply *epsy = dev_get_drvdata(dev);
66 for (i = 0; i < epsy->num_supplicants; i++) {
67 if (!strcmp(epsy->supplied_to[i], psy->name)) {
68 if (epsy->get_property(epsy,
69 POWER_SUPPLY_PROP_ONLINE, &ret))
78 int power_supply_am_i_supplied(struct power_supply *psy)
82 error = class_for_each_device(power_supply_class, NULL, psy,
83 __power_supply_am_i_supplied);
85 dev_dbg(psy->dev, "%s %d\n", __func__, error);
90 static int __power_supply_is_system_supplied(struct device *dev, void *data)
92 union power_supply_propval ret = {0,};
93 struct power_supply *psy = dev_get_drvdata(dev);
95 if (psy->type != POWER_SUPPLY_TYPE_BATTERY) {
96 if (psy->get_property(psy, POWER_SUPPLY_PROP_ONLINE, &ret))
104 int power_supply_is_system_supplied(void)
108 error = class_for_each_device(power_supply_class, NULL, NULL,
109 __power_supply_is_system_supplied);
114 int power_supply_register(struct device *parent, struct power_supply *psy)
118 psy->dev = device_create(power_supply_class, parent, 0, psy,
120 if (IS_ERR(psy->dev)) {
121 rc = PTR_ERR(psy->dev);
122 goto dev_create_failed;
125 INIT_WORK(&psy->changed_work, power_supply_changed_work);
127 rc = power_supply_create_attrs(psy);
129 goto create_attrs_failed;
131 rc = power_supply_create_triggers(psy);
133 goto create_triggers_failed;
135 power_supply_changed(psy);
139 create_triggers_failed:
140 power_supply_remove_attrs(psy);
142 device_unregister(psy->dev);
148 void power_supply_unregister(struct power_supply *psy)
150 flush_scheduled_work();
151 power_supply_remove_triggers(psy);
152 power_supply_remove_attrs(psy);
153 device_unregister(psy->dev);
156 static int __init power_supply_class_init(void)
158 power_supply_class = class_create(THIS_MODULE, "power_supply");
160 if (IS_ERR(power_supply_class))
161 return PTR_ERR(power_supply_class);
163 power_supply_class->dev_uevent = power_supply_uevent;
168 static void __exit power_supply_class_exit(void)
170 class_destroy(power_supply_class);
173 EXPORT_SYMBOL_GPL(power_supply_changed);
174 EXPORT_SYMBOL_GPL(power_supply_am_i_supplied);
175 EXPORT_SYMBOL_GPL(power_supply_is_system_supplied);
176 EXPORT_SYMBOL_GPL(power_supply_register);
177 EXPORT_SYMBOL_GPL(power_supply_unregister);
179 /* exported for the APM Power driver, APM emulation */
180 EXPORT_SYMBOL_GPL(power_supply_class);
182 subsys_initcall(power_supply_class_init);
183 module_exit(power_supply_class_exit);
185 MODULE_DESCRIPTION("Universal power supply monitor class");
186 MODULE_AUTHOR("Ian Molton <spyro@f2s.com>, "
188 "Anton Vorontsov <cbou@mail.ru>");
189 MODULE_LICENSE("GPL");