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/slab.h>
17 #include <linux/device.h>
18 #include <linux/err.h>
19 #include <linux/power_supply.h>
20 #include "power_supply.h"
22 #ifdef CONFIG_POWER_ON_CHARGER_DISPLAY
23 extern struct list_head rk_psy_head;
25 /* exported for the APM Power driver, APM emulation */
26 struct class *power_supply_class;
27 EXPORT_SYMBOL_GPL(power_supply_class);
29 static struct device_type power_supply_dev_type;
31 static int __power_supply_changed_work(struct device *dev, void *data)
33 struct power_supply *psy = (struct power_supply *)data;
34 struct power_supply *pst = dev_get_drvdata(dev);
37 for (i = 0; i < psy->num_supplicants; i++)
38 if (!strcmp(psy->supplied_to[i], pst->name)) {
39 if (pst->external_power_changed)
40 pst->external_power_changed(pst);
45 static void power_supply_changed_work(struct work_struct *work)
48 struct power_supply *psy = container_of(work, struct power_supply,
51 dev_dbg(psy->dev, "%s\n", __func__);
53 spin_lock_irqsave(&psy->changed_lock, flags);
56 spin_unlock_irqrestore(&psy->changed_lock, flags);
58 class_for_each_device(power_supply_class, NULL, psy,
59 __power_supply_changed_work);
61 power_supply_update_leds(psy);
63 kobject_uevent(&psy->dev->kobj, KOBJ_CHANGE);
64 spin_lock_irqsave(&psy->changed_lock, flags);
67 wake_unlock(&psy->work_wake_lock);
68 spin_unlock_irqrestore(&psy->changed_lock, flags);
71 void power_supply_changed(struct power_supply *psy)
75 dev_dbg(psy->dev, "%s\n", __func__);
77 spin_lock_irqsave(&psy->changed_lock, flags);
79 wake_lock(&psy->work_wake_lock);
80 spin_unlock_irqrestore(&psy->changed_lock, flags);
81 schedule_work(&psy->changed_work);
83 EXPORT_SYMBOL_GPL(power_supply_changed);
85 static int __power_supply_am_i_supplied(struct device *dev, void *data)
87 union power_supply_propval ret = {0,};
88 struct power_supply *psy = (struct power_supply *)data;
89 struct power_supply *epsy = dev_get_drvdata(dev);
92 for (i = 0; i < epsy->num_supplicants; i++) {
93 if (!strcmp(epsy->supplied_to[i], psy->name)) {
94 if (epsy->get_property(epsy,
95 POWER_SUPPLY_PROP_ONLINE, &ret))
104 int power_supply_am_i_supplied(struct power_supply *psy)
108 error = class_for_each_device(power_supply_class, NULL, psy,
109 __power_supply_am_i_supplied);
111 dev_dbg(psy->dev, "%s %d\n", __func__, error);
115 EXPORT_SYMBOL_GPL(power_supply_am_i_supplied);
117 static int __power_supply_is_system_supplied(struct device *dev, void *data)
119 union power_supply_propval ret = {0,};
120 struct power_supply *psy = dev_get_drvdata(dev);
122 if (psy->type != POWER_SUPPLY_TYPE_BATTERY) {
123 if (psy->get_property(psy, POWER_SUPPLY_PROP_ONLINE, &ret))
131 int power_supply_is_system_supplied(void)
135 error = class_for_each_device(power_supply_class, NULL, NULL,
136 __power_supply_is_system_supplied);
140 EXPORT_SYMBOL_GPL(power_supply_is_system_supplied);
142 int power_supply_set_battery_charged(struct power_supply *psy)
144 if (psy->type == POWER_SUPPLY_TYPE_BATTERY && psy->set_charged) {
145 psy->set_charged(psy);
151 EXPORT_SYMBOL_GPL(power_supply_set_battery_charged);
153 static int power_supply_match_device_by_name(struct device *dev, void *data)
155 const char *name = data;
156 struct power_supply *psy = dev_get_drvdata(dev);
158 return strcmp(psy->name, name) == 0;
161 struct power_supply *power_supply_get_by_name(char *name)
163 struct device *dev = class_find_device(power_supply_class, NULL, name,
164 power_supply_match_device_by_name);
166 return dev ? dev_get_drvdata(dev) : NULL;
168 EXPORT_SYMBOL_GPL(power_supply_get_by_name);
170 static void power_supply_dev_release(struct device *dev)
172 pr_debug("device: '%s': %s\n", dev_name(dev), __func__);
176 int power_supply_register(struct device *parent, struct power_supply *psy)
181 dev = kzalloc(sizeof(*dev), GFP_KERNEL);
185 device_initialize(dev);
187 #ifdef CONFIG_POWER_ON_CHARGER_DISPLAY
188 list_add(&psy->rk_psy_node, &rk_psy_head);
191 dev->class = power_supply_class;
192 dev->type = &power_supply_dev_type;
193 dev->parent = parent;
194 dev->release = power_supply_dev_release;
195 dev_set_drvdata(dev, psy);
198 INIT_WORK(&psy->changed_work, power_supply_changed_work);
200 rc = kobject_set_name(&dev->kobj, "%s", psy->name);
202 goto kobject_set_name_failed;
204 rc = device_add(dev);
206 goto device_add_failed;
208 spin_lock_init(&psy->changed_lock);
209 wake_lock_init(&psy->work_wake_lock, WAKE_LOCK_SUSPEND, "power-supply");
211 rc = power_supply_create_triggers(psy);
213 goto create_triggers_failed;
215 power_supply_changed(psy);
219 create_triggers_failed:
220 wake_lock_destroy(&psy->work_wake_lock);
222 kobject_set_name_failed:
228 EXPORT_SYMBOL_GPL(power_supply_register);
230 void power_supply_unregister(struct power_supply *psy)
232 cancel_work_sync(&psy->changed_work);
233 power_supply_remove_triggers(psy);
234 wake_lock_destroy(&psy->work_wake_lock);
235 device_unregister(psy->dev);
237 EXPORT_SYMBOL_GPL(power_supply_unregister);
239 static int __init power_supply_class_init(void)
241 power_supply_class = class_create(THIS_MODULE, "power_supply");
243 if (IS_ERR(power_supply_class))
244 return PTR_ERR(power_supply_class);
246 power_supply_class->dev_uevent = power_supply_uevent;
247 power_supply_init_attrs(&power_supply_dev_type);
252 static void __exit power_supply_class_exit(void)
254 class_destroy(power_supply_class);
257 subsys_initcall(power_supply_class_init);
258 module_exit(power_supply_class_exit);
260 MODULE_DESCRIPTION("Universal power supply monitor class");
261 MODULE_AUTHOR("Ian Molton <spyro@f2s.com>, "
263 "Anton Vorontsov <cbou@mail.ru>");
264 MODULE_LICENSE("GPL");