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 /* returning non-zero 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 * class_for_each_device() either returns its own errors or values
190 * returned by __power_supply_find_supply_from_node().
192 * __power_supply_find_supply_from_node() will return 0 (no match)
195 * We return 0 if class_for_each_device() returned 1, -EPROBE_DEFER if
196 * it returned 0, or error as returned by it.
198 error = class_for_each_device(power_supply_class, NULL, supply_node,
199 __power_supply_find_supply_from_node);
201 return error ? (error == 1 ? 0 : error) : -EPROBE_DEFER;
204 static int power_supply_check_supplies(struct power_supply *psy)
206 struct device_node *np;
209 /* If there is already a list honor it */
210 if (psy->supplied_from && psy->num_supplies > 0)
213 /* No device node found, nothing to do */
220 np = of_parse_phandle(psy->of_node, "power-supplies", cnt++);
224 ret = power_supply_find_supply_from_node(np);
228 dev_dbg(psy->dev, "Failed to find supply!\n");
233 /* Missing valid "power-supplies" entries */
237 /* All supplies found, allocate char ** array for filling */
238 psy->supplied_from = devm_kzalloc(psy->dev, sizeof(psy->supplied_from),
240 if (!psy->supplied_from) {
241 dev_err(psy->dev, "Couldn't allocate memory for supply list\n");
245 *psy->supplied_from = devm_kzalloc(psy->dev, sizeof(char *) * (cnt - 1),
247 if (!*psy->supplied_from) {
248 dev_err(psy->dev, "Couldn't allocate memory for supply list\n");
252 return power_supply_populate_supplied_from(psy);
255 static inline int power_supply_check_supplies(struct power_supply *psy)
261 static int __power_supply_am_i_supplied(struct device *dev, void *data)
263 union power_supply_propval ret = {0,};
264 struct power_supply *psy = data;
265 struct power_supply *epsy = dev_get_drvdata(dev);
267 if (__power_supply_is_supplied_by(epsy, psy))
268 if (!epsy->get_property(epsy, POWER_SUPPLY_PROP_ONLINE, &ret)) {
276 int power_supply_am_i_supplied(struct power_supply *psy)
280 error = class_for_each_device(power_supply_class, NULL, psy,
281 __power_supply_am_i_supplied);
283 dev_dbg(psy->dev, "%s %d\n", __func__, error);
287 EXPORT_SYMBOL_GPL(power_supply_am_i_supplied);
289 static int __power_supply_is_system_supplied(struct device *dev, void *data)
291 union power_supply_propval ret = {0,};
292 struct power_supply *psy = dev_get_drvdata(dev);
293 unsigned int *count = data;
296 if (psy->type != POWER_SUPPLY_TYPE_BATTERY) {
297 if (psy->get_property(psy, POWER_SUPPLY_PROP_ONLINE, &ret))
305 int power_supply_is_system_supplied(void)
308 unsigned int count = 0;
310 error = class_for_each_device(power_supply_class, NULL, &count,
311 __power_supply_is_system_supplied);
314 * If no power class device was found at all, most probably we are
315 * running on a desktop system, so assume we are on mains power.
322 EXPORT_SYMBOL_GPL(power_supply_is_system_supplied);
324 int power_supply_set_battery_charged(struct power_supply *psy)
326 if (psy->type == POWER_SUPPLY_TYPE_BATTERY && psy->set_charged) {
327 psy->set_charged(psy);
333 EXPORT_SYMBOL_GPL(power_supply_set_battery_charged);
335 static int power_supply_match_device_by_name(struct device *dev, const void *data)
337 const char *name = data;
338 struct power_supply *psy = dev_get_drvdata(dev);
340 return strcmp(psy->name, name) == 0;
343 struct power_supply *power_supply_get_by_name(const char *name)
345 struct device *dev = class_find_device(power_supply_class, NULL, name,
346 power_supply_match_device_by_name);
348 return dev ? dev_get_drvdata(dev) : NULL;
350 EXPORT_SYMBOL_GPL(power_supply_get_by_name);
353 static int power_supply_match_device_node(struct device *dev, const void *data)
355 return dev->parent && dev->parent->of_node == data;
358 struct power_supply *power_supply_get_by_phandle(struct device_node *np,
359 const char *property)
361 struct device_node *power_supply_np;
364 power_supply_np = of_parse_phandle(np, property, 0);
365 if (!power_supply_np)
366 return ERR_PTR(-ENODEV);
368 dev = class_find_device(power_supply_class, NULL, power_supply_np,
369 power_supply_match_device_node);
371 of_node_put(power_supply_np);
373 return dev ? dev_get_drvdata(dev) : NULL;
375 EXPORT_SYMBOL_GPL(power_supply_get_by_phandle);
376 #endif /* CONFIG_OF */
378 int power_supply_powers(struct power_supply *psy, struct device *dev)
380 return sysfs_create_link(&psy->dev->kobj, &dev->kobj, "powers");
382 EXPORT_SYMBOL_GPL(power_supply_powers);
384 static void power_supply_dev_release(struct device *dev)
386 pr_debug("device: '%s': %s\n", dev_name(dev), __func__);
390 int power_supply_reg_notifier(struct notifier_block *nb)
392 return atomic_notifier_chain_register(&power_supply_notifier, nb);
394 EXPORT_SYMBOL_GPL(power_supply_reg_notifier);
396 void power_supply_unreg_notifier(struct notifier_block *nb)
398 atomic_notifier_chain_unregister(&power_supply_notifier, nb);
400 EXPORT_SYMBOL_GPL(power_supply_unreg_notifier);
402 #ifdef CONFIG_THERMAL
403 static int power_supply_read_temp(struct thermal_zone_device *tzd,
406 struct power_supply *psy;
407 union power_supply_propval val;
410 WARN_ON(tzd == NULL);
412 ret = psy->get_property(psy, POWER_SUPPLY_PROP_TEMP, &val);
414 /* Convert tenths of degree Celsius to milli degree Celsius. */
416 *temp = val.intval * 100;
421 static struct thermal_zone_device_ops psy_tzd_ops = {
422 .get_temp = power_supply_read_temp,
425 static int psy_register_thermal(struct power_supply *psy)
429 /* Register battery zone device psy reports temperature */
430 for (i = 0; i < psy->num_properties; i++) {
431 if (psy->properties[i] == POWER_SUPPLY_PROP_TEMP) {
432 psy->tzd = thermal_zone_device_register(psy->name, 0, 0,
433 psy, &psy_tzd_ops, NULL, 0, 0);
434 if (IS_ERR(psy->tzd))
435 return PTR_ERR(psy->tzd);
442 static void psy_unregister_thermal(struct power_supply *psy)
444 if (IS_ERR_OR_NULL(psy->tzd))
446 thermal_zone_device_unregister(psy->tzd);
449 /* thermal cooling device callbacks */
450 static int ps_get_max_charge_cntl_limit(struct thermal_cooling_device *tcd,
451 unsigned long *state)
453 struct power_supply *psy;
454 union power_supply_propval val;
458 ret = psy->get_property(psy,
459 POWER_SUPPLY_PROP_CHARGE_CONTROL_LIMIT_MAX, &val);
466 static int ps_get_cur_chrage_cntl_limit(struct thermal_cooling_device *tcd,
467 unsigned long *state)
469 struct power_supply *psy;
470 union power_supply_propval val;
474 ret = psy->get_property(psy,
475 POWER_SUPPLY_PROP_CHARGE_CONTROL_LIMIT, &val);
482 static int ps_set_cur_charge_cntl_limit(struct thermal_cooling_device *tcd,
485 struct power_supply *psy;
486 union power_supply_propval val;
491 ret = psy->set_property(psy,
492 POWER_SUPPLY_PROP_CHARGE_CONTROL_LIMIT, &val);
497 static struct thermal_cooling_device_ops psy_tcd_ops = {
498 .get_max_state = ps_get_max_charge_cntl_limit,
499 .get_cur_state = ps_get_cur_chrage_cntl_limit,
500 .set_cur_state = ps_set_cur_charge_cntl_limit,
503 static int psy_register_cooler(struct power_supply *psy)
507 /* Register for cooling device if psy can control charging */
508 for (i = 0; i < psy->num_properties; i++) {
509 if (psy->properties[i] ==
510 POWER_SUPPLY_PROP_CHARGE_CONTROL_LIMIT) {
511 psy->tcd = thermal_cooling_device_register(
514 if (IS_ERR(psy->tcd))
515 return PTR_ERR(psy->tcd);
522 static void psy_unregister_cooler(struct power_supply *psy)
524 if (IS_ERR_OR_NULL(psy->tcd))
526 thermal_cooling_device_unregister(psy->tcd);
529 static int psy_register_thermal(struct power_supply *psy)
534 static void psy_unregister_thermal(struct power_supply *psy)
538 static int psy_register_cooler(struct power_supply *psy)
543 static void psy_unregister_cooler(struct power_supply *psy)
548 static int __power_supply_register(struct device *parent,
549 struct power_supply *psy, bool ws)
554 dev = kzalloc(sizeof(*dev), GFP_KERNEL);
558 device_initialize(dev);
560 dev->class = power_supply_class;
561 dev->type = &power_supply_dev_type;
562 dev->parent = parent;
563 dev->release = power_supply_dev_release;
564 dev_set_drvdata(dev, psy);
567 rc = dev_set_name(dev, "%s", psy->name);
569 goto dev_set_name_failed;
571 INIT_WORK(&psy->changed_work, power_supply_changed_work);
573 rc = power_supply_check_supplies(psy);
575 dev_info(dev, "Not all required supplies found, defer probe\n");
576 goto check_supplies_failed;
579 spin_lock_init(&psy->changed_lock);
580 rc = device_init_wakeup(dev, ws);
582 goto wakeup_init_failed;
584 rc = device_add(dev);
586 goto device_add_failed;
588 rc = psy_register_thermal(psy);
590 goto register_thermal_failed;
592 rc = psy_register_cooler(psy);
594 goto register_cooler_failed;
596 rc = power_supply_create_triggers(psy);
598 goto create_triggers_failed;
600 power_supply_changed(psy);
604 create_triggers_failed:
605 psy_unregister_cooler(psy);
606 register_cooler_failed:
607 psy_unregister_thermal(psy);
608 register_thermal_failed:
612 check_supplies_failed:
619 int power_supply_register(struct device *parent, struct power_supply *psy)
621 return __power_supply_register(parent, psy, true);
623 EXPORT_SYMBOL_GPL(power_supply_register);
625 int power_supply_register_no_ws(struct device *parent, struct power_supply *psy)
627 return __power_supply_register(parent, psy, false);
629 EXPORT_SYMBOL_GPL(power_supply_register_no_ws);
631 void power_supply_unregister(struct power_supply *psy)
633 cancel_work_sync(&psy->changed_work);
634 sysfs_remove_link(&psy->dev->kobj, "powers");
635 power_supply_remove_triggers(psy);
636 psy_unregister_cooler(psy);
637 psy_unregister_thermal(psy);
638 device_init_wakeup(psy->dev, false);
639 device_unregister(psy->dev);
641 EXPORT_SYMBOL_GPL(power_supply_unregister);
643 static int __init power_supply_class_init(void)
645 power_supply_class = class_create(THIS_MODULE, "power_supply");
647 if (IS_ERR(power_supply_class))
648 return PTR_ERR(power_supply_class);
650 power_supply_class->dev_uevent = power_supply_uevent;
651 power_supply_init_attrs(&power_supply_dev_type);
656 static void __exit power_supply_class_exit(void)
658 class_destroy(power_supply_class);
661 subsys_initcall(power_supply_class_init);
662 module_exit(power_supply_class_exit);
664 MODULE_DESCRIPTION("Universal power supply monitor class");
665 MODULE_AUTHOR("Ian Molton <spyro@f2s.com>, "
667 "Anton Vorontsov <cbou@mail.ru>");
668 MODULE_LICENSE("GPL");