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/notifier.h>
19 #include <linux/err.h>
20 #include <linux/power_supply.h>
21 #include <linux/thermal.h>
22 #include "power_supply.h"
24 /* exported for the APM Power driver, APM emulation */
25 struct class *power_supply_class;
26 EXPORT_SYMBOL_GPL(power_supply_class);
28 ATOMIC_NOTIFIER_HEAD(power_supply_notifier);
29 EXPORT_SYMBOL_GPL(power_supply_notifier);
31 static struct device_type power_supply_dev_type;
33 static bool __power_supply_is_supplied_by(struct power_supply *supplier,
34 struct power_supply *supply)
38 if (!supply->supplied_from && !supplier->supplied_to)
41 /* Support both supplied_to and supplied_from modes */
42 if (supply->supplied_from) {
45 for (i = 0; i < supply->num_supplies; i++)
46 if (!strcmp(supplier->name, supply->supplied_from[i]))
51 for (i = 0; i < supplier->num_supplicants; i++)
52 if (!strcmp(supplier->supplied_to[i], supply->name))
59 static int __power_supply_changed_work(struct device *dev, void *data)
61 struct power_supply *psy = data;
62 struct power_supply *pst = dev_get_drvdata(dev);
64 if (__power_supply_is_supplied_by(psy, pst)) {
65 if (pst->external_power_changed)
66 pst->external_power_changed(pst);
72 static void power_supply_changed_work(struct work_struct *work)
75 struct power_supply *psy = container_of(work, struct power_supply,
78 dev_dbg(psy->dev, "%s\n", __func__);
80 spin_lock_irqsave(&psy->changed_lock, flags);
83 spin_unlock_irqrestore(&psy->changed_lock, flags);
84 class_for_each_device(power_supply_class, NULL, psy,
85 __power_supply_changed_work);
86 power_supply_update_leds(psy);
87 atomic_notifier_call_chain(&power_supply_notifier,
88 PSY_EVENT_PROP_CHANGED, psy);
89 kobject_uevent(&psy->dev->kobj, KOBJ_CHANGE);
90 spin_lock_irqsave(&psy->changed_lock, flags);
93 * Dependent power supplies (e.g. battery) may have changed state
94 * as a result of this event, so poll again and hold the
95 * wakeup_source until all events are processed.
99 spin_unlock_irqrestore(&psy->changed_lock, flags);
102 void power_supply_changed(struct power_supply *psy)
106 dev_dbg(psy->dev, "%s\n", __func__);
108 spin_lock_irqsave(&psy->changed_lock, flags);
110 pm_stay_awake(psy->dev);
111 spin_unlock_irqrestore(&psy->changed_lock, flags);
112 schedule_work(&psy->changed_work);
114 EXPORT_SYMBOL_GPL(power_supply_changed);
117 #include <linux/of.h>
119 static int __power_supply_populate_supplied_from(struct device *dev,
122 struct power_supply *psy = data;
123 struct power_supply *epsy = dev_get_drvdata(dev);
124 struct device_node *np;
128 np = of_parse_phandle(psy->of_node, "power-supplies", i++);
132 if (np == epsy->of_node) {
133 dev_info(psy->dev, "%s: Found supply : %s\n",
134 psy->name, epsy->name);
135 psy->supplied_from[i-1] = (char *)epsy->name;
146 static int power_supply_populate_supplied_from(struct power_supply *psy)
150 error = class_for_each_device(power_supply_class, NULL, psy,
151 __power_supply_populate_supplied_from);
153 dev_dbg(psy->dev, "%s %d\n", __func__, error);
158 static int __power_supply_find_supply_from_node(struct device *dev,
161 struct device_node *np = data;
162 struct power_supply *epsy = dev_get_drvdata(dev);
164 /* return error breaks out of class_for_each_device loop */
165 if (epsy->of_node == np)
171 static int power_supply_find_supply_from_node(struct device_node *supply_node)
175 struct class_dev_iter iter;
178 * Use iterator to see if any other device is registered.
179 * This is required since class_for_each_device returns 0
180 * if there are no devices registered.
182 class_dev_iter_init(&iter, power_supply_class, NULL, NULL);
183 dev = class_dev_iter_next(&iter);
186 return -EPROBE_DEFER;
189 * We have to treat the return value as inverted, because if
190 * we return error on not found, then it won't continue looking.
191 * So we trick it by returning error on success to stop looking
192 * once the matching device is found.
194 error = class_for_each_device(power_supply_class, NULL, supply_node,
195 __power_supply_find_supply_from_node);
197 return error ? 0 : -EPROBE_DEFER;
200 static int power_supply_check_supplies(struct power_supply *psy)
202 struct device_node *np;
205 /* If there is already a list honor it */
206 if (psy->supplied_from && psy->num_supplies > 0)
209 /* No device node found, nothing to do */
216 np = of_parse_phandle(psy->of_node, "power-supplies", cnt++);
220 ret = power_supply_find_supply_from_node(np);
222 dev_dbg(psy->dev, "Failed to find supply, defer!\n");
224 return -EPROBE_DEFER;
229 /* Missing valid "power-supplies" entries */
233 /* All supplies found, allocate char ** array for filling */
234 psy->supplied_from = devm_kzalloc(psy->dev, sizeof(psy->supplied_from),
236 if (!psy->supplied_from) {
237 dev_err(psy->dev, "Couldn't allocate memory for supply list\n");
241 *psy->supplied_from = devm_kzalloc(psy->dev, sizeof(char *) * (cnt - 1),
243 if (!*psy->supplied_from) {
244 dev_err(psy->dev, "Couldn't allocate memory for supply list\n");
248 return power_supply_populate_supplied_from(psy);
251 static inline int power_supply_check_supplies(struct power_supply *psy)
257 static int __power_supply_am_i_supplied(struct device *dev, void *data)
259 union power_supply_propval ret = {0,};
260 struct power_supply *psy = data;
261 struct power_supply *epsy = dev_get_drvdata(dev);
263 if (__power_supply_is_supplied_by(epsy, psy))
264 if (!epsy->get_property(epsy, POWER_SUPPLY_PROP_ONLINE, &ret)) {
272 int power_supply_am_i_supplied(struct power_supply *psy)
276 error = class_for_each_device(power_supply_class, NULL, psy,
277 __power_supply_am_i_supplied);
279 dev_dbg(psy->dev, "%s %d\n", __func__, error);
283 EXPORT_SYMBOL_GPL(power_supply_am_i_supplied);
285 static int __power_supply_is_system_supplied(struct device *dev, void *data)
287 union power_supply_propval ret = {0,};
288 struct power_supply *psy = dev_get_drvdata(dev);
289 unsigned int *count = data;
292 if (psy->type != POWER_SUPPLY_TYPE_BATTERY) {
293 if (psy->get_property(psy, POWER_SUPPLY_PROP_ONLINE, &ret))
301 int power_supply_is_system_supplied(void)
304 unsigned int count = 0;
306 error = class_for_each_device(power_supply_class, NULL, &count,
307 __power_supply_is_system_supplied);
310 * If no power class device was found at all, most probably we are
311 * running on a desktop system, so assume we are on mains power.
318 EXPORT_SYMBOL_GPL(power_supply_is_system_supplied);
320 int power_supply_set_battery_charged(struct power_supply *psy)
322 if (psy->type == POWER_SUPPLY_TYPE_BATTERY && psy->set_charged) {
323 psy->set_charged(psy);
329 EXPORT_SYMBOL_GPL(power_supply_set_battery_charged);
331 static int power_supply_match_device_by_name(struct device *dev, const void *data)
333 const char *name = data;
334 struct power_supply *psy = dev_get_drvdata(dev);
336 return strcmp(psy->name, name) == 0;
339 struct power_supply *power_supply_get_by_name(const char *name)
341 struct device *dev = class_find_device(power_supply_class, NULL, name,
342 power_supply_match_device_by_name);
344 return dev ? dev_get_drvdata(dev) : NULL;
346 EXPORT_SYMBOL_GPL(power_supply_get_by_name);
349 static int power_supply_match_device_node(struct device *dev, const void *data)
351 return dev->parent && dev->parent->of_node == data;
354 struct power_supply *power_supply_get_by_phandle(struct device_node *np,
355 const char *property)
357 struct device_node *power_supply_np;
360 power_supply_np = of_parse_phandle(np, property, 0);
361 if (!power_supply_np)
362 return ERR_PTR(-ENODEV);
364 dev = class_find_device(power_supply_class, NULL, power_supply_np,
365 power_supply_match_device_node);
367 of_node_put(power_supply_np);
369 return dev ? dev_get_drvdata(dev) : NULL;
371 EXPORT_SYMBOL_GPL(power_supply_get_by_phandle);
372 #endif /* CONFIG_OF */
374 int power_supply_powers(struct power_supply *psy, struct device *dev)
376 return sysfs_create_link(&psy->dev->kobj, &dev->kobj, "powers");
378 EXPORT_SYMBOL_GPL(power_supply_powers);
380 static void power_supply_dev_release(struct device *dev)
382 pr_debug("device: '%s': %s\n", dev_name(dev), __func__);
386 int power_supply_reg_notifier(struct notifier_block *nb)
388 return atomic_notifier_chain_register(&power_supply_notifier, nb);
390 EXPORT_SYMBOL_GPL(power_supply_reg_notifier);
392 void power_supply_unreg_notifier(struct notifier_block *nb)
394 atomic_notifier_chain_unregister(&power_supply_notifier, nb);
396 EXPORT_SYMBOL_GPL(power_supply_unreg_notifier);
398 #ifdef CONFIG_THERMAL
399 static int power_supply_read_temp(struct thermal_zone_device *tzd,
402 struct power_supply *psy;
403 union power_supply_propval val;
406 WARN_ON(tzd == NULL);
408 ret = psy->get_property(psy, POWER_SUPPLY_PROP_TEMP, &val);
410 /* Convert tenths of degree Celsius to milli degree Celsius. */
412 *temp = val.intval * 100;
417 static struct thermal_zone_device_ops psy_tzd_ops = {
418 .get_temp = power_supply_read_temp,
421 static int psy_register_thermal(struct power_supply *psy)
425 /* Register battery zone device psy reports temperature */
426 for (i = 0; i < psy->num_properties; i++) {
427 if (psy->properties[i] == POWER_SUPPLY_PROP_TEMP) {
428 psy->tzd = thermal_zone_device_register(psy->name, 0, 0,
429 psy, &psy_tzd_ops, NULL, 0, 0);
430 if (IS_ERR(psy->tzd))
431 return PTR_ERR(psy->tzd);
438 static void psy_unregister_thermal(struct power_supply *psy)
440 if (IS_ERR_OR_NULL(psy->tzd))
442 thermal_zone_device_unregister(psy->tzd);
445 /* thermal cooling device callbacks */
446 static int ps_get_max_charge_cntl_limit(struct thermal_cooling_device *tcd,
447 unsigned long *state)
449 struct power_supply *psy;
450 union power_supply_propval val;
454 ret = psy->get_property(psy,
455 POWER_SUPPLY_PROP_CHARGE_CONTROL_LIMIT_MAX, &val);
462 static int ps_get_cur_chrage_cntl_limit(struct thermal_cooling_device *tcd,
463 unsigned long *state)
465 struct power_supply *psy;
466 union power_supply_propval val;
470 ret = psy->get_property(psy,
471 POWER_SUPPLY_PROP_CHARGE_CONTROL_LIMIT, &val);
478 static int ps_set_cur_charge_cntl_limit(struct thermal_cooling_device *tcd,
481 struct power_supply *psy;
482 union power_supply_propval val;
487 ret = psy->set_property(psy,
488 POWER_SUPPLY_PROP_CHARGE_CONTROL_LIMIT, &val);
493 static struct thermal_cooling_device_ops psy_tcd_ops = {
494 .get_max_state = ps_get_max_charge_cntl_limit,
495 .get_cur_state = ps_get_cur_chrage_cntl_limit,
496 .set_cur_state = ps_set_cur_charge_cntl_limit,
499 static int psy_register_cooler(struct power_supply *psy)
503 /* Register for cooling device if psy can control charging */
504 for (i = 0; i < psy->num_properties; i++) {
505 if (psy->properties[i] ==
506 POWER_SUPPLY_PROP_CHARGE_CONTROL_LIMIT) {
507 psy->tcd = thermal_cooling_device_register(
510 if (IS_ERR(psy->tcd))
511 return PTR_ERR(psy->tcd);
518 static void psy_unregister_cooler(struct power_supply *psy)
520 if (IS_ERR_OR_NULL(psy->tcd))
522 thermal_cooling_device_unregister(psy->tcd);
525 static int psy_register_thermal(struct power_supply *psy)
530 static void psy_unregister_thermal(struct power_supply *psy)
534 static int psy_register_cooler(struct power_supply *psy)
539 static void psy_unregister_cooler(struct power_supply *psy)
544 static int __power_supply_register(struct device *parent,
545 struct power_supply *psy, bool ws)
550 dev = kzalloc(sizeof(*dev), GFP_KERNEL);
554 device_initialize(dev);
556 dev->class = power_supply_class;
557 dev->type = &power_supply_dev_type;
558 dev->parent = parent;
559 dev->release = power_supply_dev_release;
560 dev_set_drvdata(dev, psy);
563 rc = dev_set_name(dev, "%s", psy->name);
565 goto dev_set_name_failed;
567 INIT_WORK(&psy->changed_work, power_supply_changed_work);
569 rc = power_supply_check_supplies(psy);
571 dev_info(dev, "Not all required supplies found, defer probe\n");
572 goto check_supplies_failed;
575 spin_lock_init(&psy->changed_lock);
576 rc = device_init_wakeup(dev, ws);
578 goto wakeup_init_failed;
580 rc = device_add(dev);
582 goto device_add_failed;
584 rc = psy_register_thermal(psy);
586 goto register_thermal_failed;
588 rc = psy_register_cooler(psy);
590 goto register_cooler_failed;
592 rc = power_supply_create_triggers(psy);
594 goto create_triggers_failed;
596 power_supply_changed(psy);
600 create_triggers_failed:
601 psy_unregister_cooler(psy);
602 register_cooler_failed:
603 psy_unregister_thermal(psy);
604 register_thermal_failed:
608 check_supplies_failed:
615 int power_supply_register(struct device *parent, struct power_supply *psy)
617 return __power_supply_register(parent, psy, true);
619 EXPORT_SYMBOL_GPL(power_supply_register);
621 int power_supply_register_no_ws(struct device *parent, struct power_supply *psy)
623 return __power_supply_register(parent, psy, false);
625 EXPORT_SYMBOL_GPL(power_supply_register_no_ws);
627 void power_supply_unregister(struct power_supply *psy)
629 cancel_work_sync(&psy->changed_work);
630 sysfs_remove_link(&psy->dev->kobj, "powers");
631 power_supply_remove_triggers(psy);
632 psy_unregister_cooler(psy);
633 psy_unregister_thermal(psy);
634 device_init_wakeup(psy->dev, false);
635 device_unregister(psy->dev);
637 EXPORT_SYMBOL_GPL(power_supply_unregister);
639 static int __init power_supply_class_init(void)
641 power_supply_class = class_create(THIS_MODULE, "power_supply");
643 if (IS_ERR(power_supply_class))
644 return PTR_ERR(power_supply_class);
646 power_supply_class->dev_uevent = power_supply_uevent;
647 power_supply_init_attrs(&power_supply_dev_type);
652 static void __exit power_supply_class_exit(void)
654 class_destroy(power_supply_class);
657 subsys_initcall(power_supply_class_init);
658 module_exit(power_supply_class_exit);
660 MODULE_DESCRIPTION("Universal power supply monitor class");
661 MODULE_AUTHOR("Ian Molton <spyro@f2s.com>, "
663 "Anton Vorontsov <cbou@mail.ru>");
664 MODULE_LICENSE("GPL");