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>
21 #include <linux/power_supply.h>
22 #include <linux/property.h>
23 #include <linux/thermal.h>
24 #include "power_supply.h"
26 /* exported for the APM Power driver, APM emulation */
27 struct class *power_supply_class;
28 EXPORT_SYMBOL_GPL(power_supply_class);
30 ATOMIC_NOTIFIER_HEAD(power_supply_notifier);
31 EXPORT_SYMBOL_GPL(power_supply_notifier);
33 static struct device_type power_supply_dev_type;
35 #define POWER_SUPPLY_DEFERRED_REGISTER_TIME msecs_to_jiffies(10)
37 static bool __power_supply_is_supplied_by(struct power_supply *supplier,
38 struct power_supply *supply)
42 if (!supply->supplied_from && !supplier->supplied_to)
45 /* Support both supplied_to and supplied_from modes */
46 if (supply->supplied_from) {
47 if (!supplier->desc->name)
49 for (i = 0; i < supply->num_supplies; i++)
50 if (!strcmp(supplier->desc->name, supply->supplied_from[i]))
53 if (!supply->desc->name)
55 for (i = 0; i < supplier->num_supplicants; i++)
56 if (!strcmp(supplier->supplied_to[i], supply->desc->name))
63 static int __power_supply_changed_work(struct device *dev, void *data)
65 struct power_supply *psy = data;
66 struct power_supply *pst = dev_get_drvdata(dev);
68 if (__power_supply_is_supplied_by(psy, pst)) {
69 if (pst->desc->external_power_changed)
70 pst->desc->external_power_changed(pst);
76 static void power_supply_changed_work(struct work_struct *work)
79 struct power_supply *psy = container_of(work, struct power_supply,
82 dev_dbg(&psy->dev, "%s\n", __func__);
84 spin_lock_irqsave(&psy->changed_lock, flags);
86 * Check 'changed' here to avoid issues due to race between
87 * power_supply_changed() and this routine. In worst case
88 * power_supply_changed() can be called again just before we take above
89 * lock. During the first call of this routine we will mark 'changed' as
90 * false and it will stay false for the next call as well.
92 if (likely(psy->changed)) {
94 spin_unlock_irqrestore(&psy->changed_lock, flags);
95 class_for_each_device(power_supply_class, NULL, psy,
96 __power_supply_changed_work);
97 power_supply_update_leds(psy);
98 atomic_notifier_call_chain(&power_supply_notifier,
99 PSY_EVENT_PROP_CHANGED, psy);
100 kobject_uevent(&psy->dev.kobj, KOBJ_CHANGE);
101 spin_lock_irqsave(&psy->changed_lock, flags);
105 * Hold the wakeup_source until all events are processed.
106 * power_supply_changed() might have called again and have set 'changed'
109 if (likely(!psy->changed))
111 spin_unlock_irqrestore(&psy->changed_lock, flags);
114 void power_supply_changed(struct power_supply *psy)
118 dev_dbg(&psy->dev, "%s\n", __func__);
120 spin_lock_irqsave(&psy->changed_lock, flags);
122 pm_stay_awake(&psy->dev);
123 spin_unlock_irqrestore(&psy->changed_lock, flags);
124 schedule_work(&psy->changed_work);
126 EXPORT_SYMBOL_GPL(power_supply_changed);
129 * Notify that power supply was registered after parent finished the probing.
131 * Often power supply is registered from driver's probe function. However
132 * calling power_supply_changed() directly from power_supply_register()
133 * would lead to execution of get_property() function provided by the driver
134 * too early - before the probe ends.
136 * Avoid that by waiting on parent's mutex.
138 static void power_supply_deferred_register_work(struct work_struct *work)
140 struct power_supply *psy = container_of(work, struct power_supply,
141 deferred_register_work.work);
144 mutex_lock(&psy->dev.parent->mutex);
146 power_supply_changed(psy);
149 mutex_unlock(&psy->dev.parent->mutex);
153 #include <linux/of.h>
155 static int __power_supply_populate_supplied_from(struct device *dev,
158 struct power_supply *psy = data;
159 struct power_supply *epsy = dev_get_drvdata(dev);
160 struct device_node *np;
164 np = of_parse_phandle(psy->of_node, "power-supplies", i++);
168 if (np == epsy->of_node) {
169 dev_info(&psy->dev, "%s: Found supply : %s\n",
170 psy->desc->name, epsy->desc->name);
171 psy->supplied_from[i-1] = (char *)epsy->desc->name;
182 static int power_supply_populate_supplied_from(struct power_supply *psy)
186 error = class_for_each_device(power_supply_class, NULL, psy,
187 __power_supply_populate_supplied_from);
189 dev_dbg(&psy->dev, "%s %d\n", __func__, error);
194 static int __power_supply_find_supply_from_node(struct device *dev,
197 struct device_node *np = data;
198 struct power_supply *epsy = dev_get_drvdata(dev);
200 /* returning non-zero breaks out of class_for_each_device loop */
201 if (epsy->of_node == np)
207 static int power_supply_find_supply_from_node(struct device_node *supply_node)
212 * class_for_each_device() either returns its own errors or values
213 * returned by __power_supply_find_supply_from_node().
215 * __power_supply_find_supply_from_node() will return 0 (no match)
218 * We return 0 if class_for_each_device() returned 1, -EPROBE_DEFER if
219 * it returned 0, or error as returned by it.
221 error = class_for_each_device(power_supply_class, NULL, supply_node,
222 __power_supply_find_supply_from_node);
224 return error ? (error == 1 ? 0 : error) : -EPROBE_DEFER;
227 static int power_supply_check_supplies(struct power_supply *psy)
229 struct device_node *np;
232 /* If there is already a list honor it */
233 if (psy->supplied_from && psy->num_supplies > 0)
236 /* No device node found, nothing to do */
243 np = of_parse_phandle(psy->of_node, "power-supplies", cnt++);
247 ret = power_supply_find_supply_from_node(np);
251 dev_dbg(&psy->dev, "Failed to find supply!\n");
256 /* Missing valid "power-supplies" entries */
260 /* All supplies found, allocate char ** array for filling */
261 psy->supplied_from = devm_kzalloc(&psy->dev, sizeof(psy->supplied_from),
263 if (!psy->supplied_from)
266 *psy->supplied_from = devm_kcalloc(&psy->dev,
267 cnt - 1, sizeof(char *),
269 if (!*psy->supplied_from)
272 return power_supply_populate_supplied_from(psy);
275 static int power_supply_check_supplies(struct power_supply *psy)
279 if (!psy->dev.parent)
282 nval = device_property_read_string_array(psy->dev.parent,
283 "supplied-from", NULL, 0);
287 psy->supplied_from = devm_kmalloc_array(&psy->dev, nval,
288 sizeof(char *), GFP_KERNEL);
289 if (!psy->supplied_from)
292 ret = device_property_read_string_array(psy->dev.parent,
293 "supplied-from", (const char **)psy->supplied_from, nval);
297 psy->num_supplies = nval;
303 struct psy_am_i_supplied_data {
304 struct power_supply *psy;
308 static int __power_supply_am_i_supplied(struct device *dev, void *_data)
310 union power_supply_propval ret = {0,};
311 struct power_supply *epsy = dev_get_drvdata(dev);
312 struct psy_am_i_supplied_data *data = _data;
314 if (__power_supply_is_supplied_by(epsy, data->psy)) {
316 if (!epsy->desc->get_property(epsy, POWER_SUPPLY_PROP_ONLINE,
324 int power_supply_am_i_supplied(struct power_supply *psy)
326 struct psy_am_i_supplied_data data = { psy, 0 };
329 error = class_for_each_device(power_supply_class, NULL, &data,
330 __power_supply_am_i_supplied);
332 dev_dbg(&psy->dev, "%s count %u err %d\n", __func__, data.count, error);
339 EXPORT_SYMBOL_GPL(power_supply_am_i_supplied);
341 static int __power_supply_is_system_supplied(struct device *dev, void *data)
343 union power_supply_propval ret = {0,};
344 struct power_supply *psy = dev_get_drvdata(dev);
345 unsigned int *count = data;
348 if (psy->desc->type != POWER_SUPPLY_TYPE_BATTERY)
349 if (!psy->desc->get_property(psy, POWER_SUPPLY_PROP_ONLINE,
356 int power_supply_is_system_supplied(void)
359 unsigned int count = 0;
361 error = class_for_each_device(power_supply_class, NULL, &count,
362 __power_supply_is_system_supplied);
365 * If no power class device was found at all, most probably we are
366 * running on a desktop system, so assume we are on mains power.
373 EXPORT_SYMBOL_GPL(power_supply_is_system_supplied);
375 static int __power_supply_get_supplier_max_current(struct device *dev,
378 union power_supply_propval ret = {0,};
379 struct power_supply *epsy = dev_get_drvdata(dev);
380 struct power_supply *psy = data;
382 if (__power_supply_is_supplied_by(epsy, psy))
383 if (!epsy->desc->get_property(epsy,
384 POWER_SUPPLY_PROP_CURRENT_MAX,
391 int power_supply_set_input_current_limit_from_supplier(struct power_supply *psy)
393 union power_supply_propval val = {0,};
396 if (!psy->desc->set_property)
400 * This function is not intended for use with a supply with multiple
401 * suppliers, we simply pick the first supply to report a non 0
404 curr = class_for_each_device(power_supply_class, NULL, psy,
405 __power_supply_get_supplier_max_current);
407 return (curr == 0) ? -ENODEV : curr;
411 return psy->desc->set_property(psy,
412 POWER_SUPPLY_PROP_INPUT_CURRENT_LIMIT, &val);
414 EXPORT_SYMBOL_GPL(power_supply_set_input_current_limit_from_supplier);
416 int power_supply_set_battery_charged(struct power_supply *psy)
418 if (atomic_read(&psy->use_cnt) >= 0 &&
419 psy->desc->type == POWER_SUPPLY_TYPE_BATTERY &&
420 psy->desc->set_charged) {
421 psy->desc->set_charged(psy);
427 EXPORT_SYMBOL_GPL(power_supply_set_battery_charged);
429 static int power_supply_match_device_by_name(struct device *dev, const void *data)
431 const char *name = data;
432 struct power_supply *psy = dev_get_drvdata(dev);
434 return strcmp(psy->desc->name, name) == 0;
438 * power_supply_get_by_name() - Search for a power supply and returns its ref
439 * @name: Power supply name to fetch
441 * If power supply was found, it increases reference count for the
442 * internal power supply's device. The user should power_supply_put()
445 * Return: On success returns a reference to a power supply with
446 * matching name equals to @name, a NULL otherwise.
448 struct power_supply *power_supply_get_by_name(const char *name)
450 struct power_supply *psy = NULL;
451 struct device *dev = class_find_device(power_supply_class, NULL, name,
452 power_supply_match_device_by_name);
455 psy = dev_get_drvdata(dev);
456 atomic_inc(&psy->use_cnt);
461 EXPORT_SYMBOL_GPL(power_supply_get_by_name);
464 * power_supply_put() - Drop reference obtained with power_supply_get_by_name
465 * @psy: Reference to put
467 * The reference to power supply should be put before unregistering
470 void power_supply_put(struct power_supply *psy)
474 atomic_dec(&psy->use_cnt);
475 put_device(&psy->dev);
477 EXPORT_SYMBOL_GPL(power_supply_put);
480 static int power_supply_match_device_node(struct device *dev, const void *data)
482 return dev->parent && dev->parent->of_node == data;
486 * power_supply_get_by_phandle() - Search for a power supply and returns its ref
487 * @np: Pointer to device node holding phandle property
488 * @property: Name of property holding a power supply name
490 * If power supply was found, it increases reference count for the
491 * internal power supply's device. The user should power_supply_put()
494 * Return: On success returns a reference to a power supply with
495 * matching name equals to value under @property, NULL or ERR_PTR otherwise.
497 struct power_supply *power_supply_get_by_phandle(struct device_node *np,
498 const char *property)
500 struct device_node *power_supply_np;
501 struct power_supply *psy = NULL;
504 power_supply_np = of_parse_phandle(np, property, 0);
505 if (!power_supply_np)
506 return ERR_PTR(-ENODEV);
508 dev = class_find_device(power_supply_class, NULL, power_supply_np,
509 power_supply_match_device_node);
511 of_node_put(power_supply_np);
514 psy = dev_get_drvdata(dev);
515 atomic_inc(&psy->use_cnt);
520 EXPORT_SYMBOL_GPL(power_supply_get_by_phandle);
522 static void devm_power_supply_put(struct device *dev, void *res)
524 struct power_supply **psy = res;
526 power_supply_put(*psy);
530 * devm_power_supply_get_by_phandle() - Resource managed version of
531 * power_supply_get_by_phandle()
532 * @dev: Pointer to device holding phandle property
533 * @property: Name of property holding a power supply phandle
535 * Return: On success returns a reference to a power supply with
536 * matching name equals to value under @property, NULL or ERR_PTR otherwise.
538 struct power_supply *devm_power_supply_get_by_phandle(struct device *dev,
539 const char *property)
541 struct power_supply **ptr, *psy;
544 return ERR_PTR(-ENODEV);
546 ptr = devres_alloc(devm_power_supply_put, sizeof(*ptr), GFP_KERNEL);
548 return ERR_PTR(-ENOMEM);
550 psy = power_supply_get_by_phandle(dev->of_node, property);
551 if (IS_ERR_OR_NULL(psy)) {
555 devres_add(dev, ptr);
559 EXPORT_SYMBOL_GPL(devm_power_supply_get_by_phandle);
560 #endif /* CONFIG_OF */
562 int power_supply_get_battery_info(struct power_supply *psy,
563 struct power_supply_battery_info *info)
565 struct device_node *battery_np;
569 info->energy_full_design_uwh = -EINVAL;
570 info->charge_full_design_uah = -EINVAL;
571 info->voltage_min_design_uv = -EINVAL;
572 info->precharge_current_ua = -EINVAL;
573 info->charge_term_current_ua = -EINVAL;
574 info->constant_charge_current_max_ua = -EINVAL;
575 info->constant_charge_voltage_max_uv = -EINVAL;
578 dev_warn(&psy->dev, "%s currently only supports devicetree\n",
583 battery_np = of_parse_phandle(psy->of_node, "monitored-battery", 0);
587 err = of_property_read_string(battery_np, "compatible", &value);
591 if (strcmp("simple-battery", value))
594 /* The property and field names below must correspond to elements
595 * in enum power_supply_property. For reasoning, see
596 * Documentation/power/power_supply_class.txt.
599 of_property_read_u32(battery_np, "energy-full-design-microwatt-hours",
600 &info->energy_full_design_uwh);
601 of_property_read_u32(battery_np, "charge-full-design-microamp-hours",
602 &info->charge_full_design_uah);
603 of_property_read_u32(battery_np, "voltage-min-design-microvolt",
604 &info->voltage_min_design_uv);
605 of_property_read_u32(battery_np, "precharge-current-microamp",
606 &info->precharge_current_ua);
607 of_property_read_u32(battery_np, "charge-term-current-microamp",
608 &info->charge_term_current_ua);
609 of_property_read_u32(battery_np, "constant_charge_current_max_microamp",
610 &info->constant_charge_current_max_ua);
611 of_property_read_u32(battery_np, "constant_charge_voltage_max_microvolt",
612 &info->constant_charge_voltage_max_uv);
616 EXPORT_SYMBOL_GPL(power_supply_get_battery_info);
618 int power_supply_get_property(struct power_supply *psy,
619 enum power_supply_property psp,
620 union power_supply_propval *val)
622 if (atomic_read(&psy->use_cnt) <= 0) {
623 if (!psy->initialized)
628 return psy->desc->get_property(psy, psp, val);
630 EXPORT_SYMBOL_GPL(power_supply_get_property);
632 int power_supply_set_property(struct power_supply *psy,
633 enum power_supply_property psp,
634 const union power_supply_propval *val)
636 if (atomic_read(&psy->use_cnt) <= 0 || !psy->desc->set_property)
639 return psy->desc->set_property(psy, psp, val);
641 EXPORT_SYMBOL_GPL(power_supply_set_property);
643 int power_supply_property_is_writeable(struct power_supply *psy,
644 enum power_supply_property psp)
646 if (atomic_read(&psy->use_cnt) <= 0 ||
647 !psy->desc->property_is_writeable)
650 return psy->desc->property_is_writeable(psy, psp);
652 EXPORT_SYMBOL_GPL(power_supply_property_is_writeable);
654 void power_supply_external_power_changed(struct power_supply *psy)
656 if (atomic_read(&psy->use_cnt) <= 0 ||
657 !psy->desc->external_power_changed)
660 psy->desc->external_power_changed(psy);
662 EXPORT_SYMBOL_GPL(power_supply_external_power_changed);
664 int power_supply_powers(struct power_supply *psy, struct device *dev)
666 return sysfs_create_link(&psy->dev.kobj, &dev->kobj, "powers");
668 EXPORT_SYMBOL_GPL(power_supply_powers);
670 static void power_supply_dev_release(struct device *dev)
672 struct power_supply *psy = to_power_supply(dev);
673 dev_dbg(dev, "%s\n", __func__);
677 int power_supply_reg_notifier(struct notifier_block *nb)
679 return atomic_notifier_chain_register(&power_supply_notifier, nb);
681 EXPORT_SYMBOL_GPL(power_supply_reg_notifier);
683 void power_supply_unreg_notifier(struct notifier_block *nb)
685 atomic_notifier_chain_unregister(&power_supply_notifier, nb);
687 EXPORT_SYMBOL_GPL(power_supply_unreg_notifier);
689 #ifdef CONFIG_THERMAL
690 static int power_supply_read_temp(struct thermal_zone_device *tzd,
693 struct power_supply *psy;
694 union power_supply_propval val;
697 WARN_ON(tzd == NULL);
699 ret = power_supply_get_property(psy, POWER_SUPPLY_PROP_TEMP, &val);
703 /* Convert tenths of degree Celsius to milli degree Celsius. */
704 *temp = val.intval * 100;
709 static struct thermal_zone_device_ops psy_tzd_ops = {
710 .get_temp = power_supply_read_temp,
713 static int psy_register_thermal(struct power_supply *psy)
717 if (psy->desc->no_thermal)
720 /* Register battery zone device psy reports temperature */
721 for (i = 0; i < psy->desc->num_properties; i++) {
722 if (psy->desc->properties[i] == POWER_SUPPLY_PROP_TEMP) {
723 psy->tzd = thermal_zone_device_register(psy->desc->name,
724 0, 0, psy, &psy_tzd_ops, NULL, 0, 0);
725 return PTR_ERR_OR_ZERO(psy->tzd);
731 static void psy_unregister_thermal(struct power_supply *psy)
733 if (IS_ERR_OR_NULL(psy->tzd))
735 thermal_zone_device_unregister(psy->tzd);
738 /* thermal cooling device callbacks */
739 static int ps_get_max_charge_cntl_limit(struct thermal_cooling_device *tcd,
740 unsigned long *state)
742 struct power_supply *psy;
743 union power_supply_propval val;
747 ret = power_supply_get_property(psy,
748 POWER_SUPPLY_PROP_CHARGE_CONTROL_LIMIT_MAX, &val);
757 static int ps_get_cur_chrage_cntl_limit(struct thermal_cooling_device *tcd,
758 unsigned long *state)
760 struct power_supply *psy;
761 union power_supply_propval val;
765 ret = power_supply_get_property(psy,
766 POWER_SUPPLY_PROP_CHARGE_CONTROL_LIMIT, &val);
775 static int ps_set_cur_charge_cntl_limit(struct thermal_cooling_device *tcd,
778 struct power_supply *psy;
779 union power_supply_propval val;
784 ret = psy->desc->set_property(psy,
785 POWER_SUPPLY_PROP_CHARGE_CONTROL_LIMIT, &val);
790 static const struct thermal_cooling_device_ops psy_tcd_ops = {
791 .get_max_state = ps_get_max_charge_cntl_limit,
792 .get_cur_state = ps_get_cur_chrage_cntl_limit,
793 .set_cur_state = ps_set_cur_charge_cntl_limit,
796 static int psy_register_cooler(struct power_supply *psy)
800 /* Register for cooling device if psy can control charging */
801 for (i = 0; i < psy->desc->num_properties; i++) {
802 if (psy->desc->properties[i] ==
803 POWER_SUPPLY_PROP_CHARGE_CONTROL_LIMIT) {
804 psy->tcd = thermal_cooling_device_register(
805 (char *)psy->desc->name,
807 return PTR_ERR_OR_ZERO(psy->tcd);
813 static void psy_unregister_cooler(struct power_supply *psy)
815 if (IS_ERR_OR_NULL(psy->tcd))
817 thermal_cooling_device_unregister(psy->tcd);
820 static int psy_register_thermal(struct power_supply *psy)
825 static void psy_unregister_thermal(struct power_supply *psy)
829 static int psy_register_cooler(struct power_supply *psy)
834 static void psy_unregister_cooler(struct power_supply *psy)
839 static struct power_supply *__must_check
840 __power_supply_register(struct device *parent,
841 const struct power_supply_desc *desc,
842 const struct power_supply_config *cfg,
846 struct power_supply *psy;
850 pr_warn("%s: Expected proper parent device for '%s'\n",
851 __func__, desc->name);
853 if (!desc || !desc->name || !desc->properties || !desc->num_properties)
854 return ERR_PTR(-EINVAL);
856 for (i = 0; i < desc->num_properties; ++i) {
857 if ((desc->properties[i] == POWER_SUPPLY_PROP_USB_TYPE) &&
858 (!desc->usb_types || !desc->num_usb_types))
859 return ERR_PTR(-EINVAL);
862 psy = kzalloc(sizeof(*psy), GFP_KERNEL);
864 return ERR_PTR(-ENOMEM);
868 device_initialize(dev);
870 dev->class = power_supply_class;
871 dev->type = &power_supply_dev_type;
872 dev->parent = parent;
873 dev->release = power_supply_dev_release;
874 dev_set_drvdata(dev, psy);
877 psy->drv_data = cfg->drv_data;
879 cfg->fwnode ? to_of_node(cfg->fwnode) : cfg->of_node;
880 psy->supplied_to = cfg->supplied_to;
881 psy->num_supplicants = cfg->num_supplicants;
884 rc = dev_set_name(dev, "%s", desc->name);
886 goto dev_set_name_failed;
888 INIT_WORK(&psy->changed_work, power_supply_changed_work);
889 INIT_DELAYED_WORK(&psy->deferred_register_work,
890 power_supply_deferred_register_work);
892 rc = power_supply_check_supplies(psy);
894 dev_info(dev, "Not all required supplies found, defer probe\n");
895 goto check_supplies_failed;
898 spin_lock_init(&psy->changed_lock);
899 rc = device_init_wakeup(dev, ws);
901 goto wakeup_init_failed;
903 rc = device_add(dev);
905 goto device_add_failed;
907 rc = psy_register_thermal(psy);
909 goto register_thermal_failed;
911 rc = psy_register_cooler(psy);
913 goto register_cooler_failed;
915 rc = power_supply_create_triggers(psy);
917 goto create_triggers_failed;
920 * Update use_cnt after any uevents (most notably from device_add()).
921 * We are here still during driver's probe but
922 * the power_supply_uevent() calls back driver's get_property
924 * 1. Driver did not assigned the returned struct power_supply,
925 * 2. Driver could not finish initialization (anything in its probe
926 * after calling power_supply_register()).
928 atomic_inc(&psy->use_cnt);
929 psy->initialized = true;
931 queue_delayed_work(system_power_efficient_wq,
932 &psy->deferred_register_work,
933 POWER_SUPPLY_DEFERRED_REGISTER_TIME);
937 create_triggers_failed:
938 psy_unregister_cooler(psy);
939 register_cooler_failed:
940 psy_unregister_thermal(psy);
941 register_thermal_failed:
945 check_supplies_failed:
952 * power_supply_register() - Register new power supply
953 * @parent: Device to be a parent of power supply's device, usually
954 * the device which probe function calls this
955 * @desc: Description of power supply, must be valid through whole
956 * lifetime of this power supply
957 * @cfg: Run-time specific configuration accessed during registering,
960 * Return: A pointer to newly allocated power_supply on success
961 * or ERR_PTR otherwise.
962 * Use power_supply_unregister() on returned power_supply pointer to release
965 struct power_supply *__must_check power_supply_register(struct device *parent,
966 const struct power_supply_desc *desc,
967 const struct power_supply_config *cfg)
969 return __power_supply_register(parent, desc, cfg, true);
971 EXPORT_SYMBOL_GPL(power_supply_register);
974 * power_supply_register_no_ws() - Register new non-waking-source power supply
975 * @parent: Device to be a parent of power supply's device, usually
976 * the device which probe function calls this
977 * @desc: Description of power supply, must be valid through whole
978 * lifetime of this power supply
979 * @cfg: Run-time specific configuration accessed during registering,
982 * Return: A pointer to newly allocated power_supply on success
983 * or ERR_PTR otherwise.
984 * Use power_supply_unregister() on returned power_supply pointer to release
987 struct power_supply *__must_check
988 power_supply_register_no_ws(struct device *parent,
989 const struct power_supply_desc *desc,
990 const struct power_supply_config *cfg)
992 return __power_supply_register(parent, desc, cfg, false);
994 EXPORT_SYMBOL_GPL(power_supply_register_no_ws);
996 static void devm_power_supply_release(struct device *dev, void *res)
998 struct power_supply **psy = res;
1000 power_supply_unregister(*psy);
1004 * devm_power_supply_register() - Register managed power supply
1005 * @parent: Device to be a parent of power supply's device, usually
1006 * the device which probe function calls this
1007 * @desc: Description of power supply, must be valid through whole
1008 * lifetime of this power supply
1009 * @cfg: Run-time specific configuration accessed during registering,
1012 * Return: A pointer to newly allocated power_supply on success
1013 * or ERR_PTR otherwise.
1014 * The returned power_supply pointer will be automatically unregistered
1017 struct power_supply *__must_check
1018 devm_power_supply_register(struct device *parent,
1019 const struct power_supply_desc *desc,
1020 const struct power_supply_config *cfg)
1022 struct power_supply **ptr, *psy;
1024 ptr = devres_alloc(devm_power_supply_release, sizeof(*ptr), GFP_KERNEL);
1027 return ERR_PTR(-ENOMEM);
1028 psy = __power_supply_register(parent, desc, cfg, true);
1033 devres_add(parent, ptr);
1037 EXPORT_SYMBOL_GPL(devm_power_supply_register);
1040 * devm_power_supply_register_no_ws() - Register managed non-waking-source power supply
1041 * @parent: Device to be a parent of power supply's device, usually
1042 * the device which probe function calls this
1043 * @desc: Description of power supply, must be valid through whole
1044 * lifetime of this power supply
1045 * @cfg: Run-time specific configuration accessed during registering,
1048 * Return: A pointer to newly allocated power_supply on success
1049 * or ERR_PTR otherwise.
1050 * The returned power_supply pointer will be automatically unregistered
1053 struct power_supply *__must_check
1054 devm_power_supply_register_no_ws(struct device *parent,
1055 const struct power_supply_desc *desc,
1056 const struct power_supply_config *cfg)
1058 struct power_supply **ptr, *psy;
1060 ptr = devres_alloc(devm_power_supply_release, sizeof(*ptr), GFP_KERNEL);
1063 return ERR_PTR(-ENOMEM);
1064 psy = __power_supply_register(parent, desc, cfg, false);
1069 devres_add(parent, ptr);
1073 EXPORT_SYMBOL_GPL(devm_power_supply_register_no_ws);
1076 * power_supply_unregister() - Remove this power supply from system
1077 * @psy: Pointer to power supply to unregister
1079 * Remove this power supply from the system. The resources of power supply
1080 * will be freed here or on last power_supply_put() call.
1082 void power_supply_unregister(struct power_supply *psy)
1084 WARN_ON(atomic_dec_return(&psy->use_cnt));
1085 cancel_work_sync(&psy->changed_work);
1086 cancel_delayed_work_sync(&psy->deferred_register_work);
1087 sysfs_remove_link(&psy->dev.kobj, "powers");
1088 power_supply_remove_triggers(psy);
1089 psy_unregister_cooler(psy);
1090 psy_unregister_thermal(psy);
1091 device_init_wakeup(&psy->dev, false);
1092 device_unregister(&psy->dev);
1094 EXPORT_SYMBOL_GPL(power_supply_unregister);
1096 void *power_supply_get_drvdata(struct power_supply *psy)
1098 return psy->drv_data;
1100 EXPORT_SYMBOL_GPL(power_supply_get_drvdata);
1102 static int __init power_supply_class_init(void)
1104 power_supply_class = class_create(THIS_MODULE, "power_supply");
1106 if (IS_ERR(power_supply_class))
1107 return PTR_ERR(power_supply_class);
1109 power_supply_class->dev_uevent = power_supply_uevent;
1110 power_supply_init_attrs(&power_supply_dev_type);
1115 static void __exit power_supply_class_exit(void)
1117 class_destroy(power_supply_class);
1120 subsys_initcall(power_supply_class_init);
1121 module_exit(power_supply_class_exit);
1123 MODULE_DESCRIPTION("Universal power supply monitor class");
1124 MODULE_AUTHOR("Ian Molton <spyro@f2s.com>, "
1126 "Anton Vorontsov <cbou@mail.ru>");
1127 MODULE_LICENSE("GPL");