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/delay.h>
18 #include <linux/device.h>
19 #include <linux/notifier.h>
20 #include <linux/err.h>
22 #include <linux/power_supply.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);
143 if (psy->dev.parent) {
144 while (!mutex_trylock(&psy->dev.parent->mutex)) {
151 power_supply_changed(psy);
154 mutex_unlock(&psy->dev.parent->mutex);
158 #include <linux/of.h>
160 static int __power_supply_populate_supplied_from(struct device *dev,
163 struct power_supply *psy = data;
164 struct power_supply *epsy = dev_get_drvdata(dev);
165 struct device_node *np;
169 np = of_parse_phandle(psy->of_node, "power-supplies", i++);
173 if (np == epsy->of_node) {
174 dev_info(&psy->dev, "%s: Found supply : %s\n",
175 psy->desc->name, epsy->desc->name);
176 psy->supplied_from[i-1] = (char *)epsy->desc->name;
187 static int power_supply_populate_supplied_from(struct power_supply *psy)
191 error = class_for_each_device(power_supply_class, NULL, psy,
192 __power_supply_populate_supplied_from);
194 dev_dbg(&psy->dev, "%s %d\n", __func__, error);
199 static int __power_supply_find_supply_from_node(struct device *dev,
202 struct device_node *np = data;
203 struct power_supply *epsy = dev_get_drvdata(dev);
205 /* returning non-zero breaks out of class_for_each_device loop */
206 if (epsy->of_node == np)
212 static int power_supply_find_supply_from_node(struct device_node *supply_node)
217 * class_for_each_device() either returns its own errors or values
218 * returned by __power_supply_find_supply_from_node().
220 * __power_supply_find_supply_from_node() will return 0 (no match)
223 * We return 0 if class_for_each_device() returned 1, -EPROBE_DEFER if
224 * it returned 0, or error as returned by it.
226 error = class_for_each_device(power_supply_class, NULL, supply_node,
227 __power_supply_find_supply_from_node);
229 return error ? (error == 1 ? 0 : error) : -EPROBE_DEFER;
232 static int power_supply_check_supplies(struct power_supply *psy)
234 struct device_node *np;
237 /* If there is already a list honor it */
238 if (psy->supplied_from && psy->num_supplies > 0)
241 /* No device node found, nothing to do */
248 np = of_parse_phandle(psy->of_node, "power-supplies", cnt++);
252 ret = power_supply_find_supply_from_node(np);
256 dev_dbg(&psy->dev, "Failed to find supply!\n");
261 /* Missing valid "power-supplies" entries */
265 /* All supplies found, allocate char ** array for filling */
266 psy->supplied_from = devm_kzalloc(&psy->dev, sizeof(psy->supplied_from),
268 if (!psy->supplied_from)
271 *psy->supplied_from = devm_kzalloc(&psy->dev,
272 sizeof(char *) * (cnt - 1),
274 if (!*psy->supplied_from)
277 return power_supply_populate_supplied_from(psy);
280 static int power_supply_check_supplies(struct power_supply *psy)
284 if (!psy->dev.parent)
287 nval = device_property_read_string_array(psy->dev.parent,
288 "supplied-from", NULL, 0);
292 psy->supplied_from = devm_kmalloc_array(&psy->dev, nval,
293 sizeof(char *), GFP_KERNEL);
294 if (!psy->supplied_from)
297 ret = device_property_read_string_array(psy->dev.parent,
298 "supplied-from", (const char **)psy->supplied_from, nval);
302 psy->num_supplies = nval;
308 struct psy_am_i_supplied_data {
309 struct power_supply *psy;
313 static int __power_supply_am_i_supplied(struct device *dev, void *_data)
315 union power_supply_propval ret = {0,};
316 struct power_supply *epsy = dev_get_drvdata(dev);
317 struct psy_am_i_supplied_data *data = _data;
319 if (__power_supply_is_supplied_by(epsy, data->psy)) {
321 if (!epsy->desc->get_property(epsy, POWER_SUPPLY_PROP_ONLINE,
329 int power_supply_am_i_supplied(struct power_supply *psy)
331 struct psy_am_i_supplied_data data = { psy, 0 };
334 error = class_for_each_device(power_supply_class, NULL, &data,
335 __power_supply_am_i_supplied);
337 dev_dbg(&psy->dev, "%s count %u err %d\n", __func__, data.count, error);
344 EXPORT_SYMBOL_GPL(power_supply_am_i_supplied);
346 static int __power_supply_is_system_supplied(struct device *dev, void *data)
348 union power_supply_propval ret = {0,};
349 struct power_supply *psy = dev_get_drvdata(dev);
350 unsigned int *count = data;
353 if (psy->desc->type != POWER_SUPPLY_TYPE_BATTERY)
354 if (!psy->desc->get_property(psy, POWER_SUPPLY_PROP_ONLINE,
361 int power_supply_is_system_supplied(void)
364 unsigned int count = 0;
366 error = class_for_each_device(power_supply_class, NULL, &count,
367 __power_supply_is_system_supplied);
370 * If no power class device was found at all, most probably we are
371 * running on a desktop system, so assume we are on mains power.
378 EXPORT_SYMBOL_GPL(power_supply_is_system_supplied);
380 static int __power_supply_get_supplier_max_current(struct device *dev,
383 union power_supply_propval ret = {0,};
384 struct power_supply *epsy = dev_get_drvdata(dev);
385 struct power_supply *psy = data;
387 if (__power_supply_is_supplied_by(epsy, psy))
388 if (!epsy->desc->get_property(epsy,
389 POWER_SUPPLY_PROP_CURRENT_MAX,
396 int power_supply_set_input_current_limit_from_supplier(struct power_supply *psy)
398 union power_supply_propval val = {0,};
401 if (!psy->desc->set_property)
405 * This function is not intended for use with a supply with multiple
406 * suppliers, we simply pick the first supply to report a non 0
409 curr = class_for_each_device(power_supply_class, NULL, psy,
410 __power_supply_get_supplier_max_current);
412 return (curr == 0) ? -ENODEV : curr;
416 return psy->desc->set_property(psy,
417 POWER_SUPPLY_PROP_INPUT_CURRENT_LIMIT, &val);
419 EXPORT_SYMBOL_GPL(power_supply_set_input_current_limit_from_supplier);
421 int power_supply_set_battery_charged(struct power_supply *psy)
423 if (atomic_read(&psy->use_cnt) >= 0 &&
424 psy->desc->type == POWER_SUPPLY_TYPE_BATTERY &&
425 psy->desc->set_charged) {
426 psy->desc->set_charged(psy);
432 EXPORT_SYMBOL_GPL(power_supply_set_battery_charged);
434 static int power_supply_match_device_by_name(struct device *dev, const void *data)
436 const char *name = data;
437 struct power_supply *psy = dev_get_drvdata(dev);
439 return strcmp(psy->desc->name, name) == 0;
443 * power_supply_get_by_name() - Search for a power supply and returns its ref
444 * @name: Power supply name to fetch
446 * If power supply was found, it increases reference count for the
447 * internal power supply's device. The user should power_supply_put()
450 * Return: On success returns a reference to a power supply with
451 * matching name equals to @name, a NULL otherwise.
453 struct power_supply *power_supply_get_by_name(const char *name)
455 struct power_supply *psy = NULL;
456 struct device *dev = class_find_device(power_supply_class, NULL, name,
457 power_supply_match_device_by_name);
460 psy = dev_get_drvdata(dev);
461 atomic_inc(&psy->use_cnt);
466 EXPORT_SYMBOL_GPL(power_supply_get_by_name);
469 * power_supply_put() - Drop reference obtained with power_supply_get_by_name
470 * @psy: Reference to put
472 * The reference to power supply should be put before unregistering
475 void power_supply_put(struct power_supply *psy)
479 atomic_dec(&psy->use_cnt);
480 put_device(&psy->dev);
482 EXPORT_SYMBOL_GPL(power_supply_put);
485 static int power_supply_match_device_node(struct device *dev, const void *data)
487 return dev->parent && dev->parent->of_node == data;
491 * power_supply_get_by_phandle() - Search for a power supply and returns its ref
492 * @np: Pointer to device node holding phandle property
493 * @property: Name of property holding a power supply name
495 * If power supply was found, it increases reference count for the
496 * internal power supply's device. The user should power_supply_put()
499 * Return: On success returns a reference to a power supply with
500 * matching name equals to value under @property, NULL or ERR_PTR otherwise.
502 struct power_supply *power_supply_get_by_phandle(struct device_node *np,
503 const char *property)
505 struct device_node *power_supply_np;
506 struct power_supply *psy = NULL;
509 power_supply_np = of_parse_phandle(np, property, 0);
510 if (!power_supply_np)
511 return ERR_PTR(-ENODEV);
513 dev = class_find_device(power_supply_class, NULL, power_supply_np,
514 power_supply_match_device_node);
516 of_node_put(power_supply_np);
519 psy = dev_get_drvdata(dev);
520 atomic_inc(&psy->use_cnt);
525 EXPORT_SYMBOL_GPL(power_supply_get_by_phandle);
527 static void devm_power_supply_put(struct device *dev, void *res)
529 struct power_supply **psy = res;
531 power_supply_put(*psy);
535 * devm_power_supply_get_by_phandle() - Resource managed version of
536 * power_supply_get_by_phandle()
537 * @dev: Pointer to device holding phandle property
538 * @property: Name of property holding a power supply phandle
540 * Return: On success returns a reference to a power supply with
541 * matching name equals to value under @property, NULL or ERR_PTR otherwise.
543 struct power_supply *devm_power_supply_get_by_phandle(struct device *dev,
544 const char *property)
546 struct power_supply **ptr, *psy;
549 return ERR_PTR(-ENODEV);
551 ptr = devres_alloc(devm_power_supply_put, sizeof(*ptr), GFP_KERNEL);
553 return ERR_PTR(-ENOMEM);
555 psy = power_supply_get_by_phandle(dev->of_node, property);
556 if (IS_ERR_OR_NULL(psy)) {
560 devres_add(dev, ptr);
564 EXPORT_SYMBOL_GPL(devm_power_supply_get_by_phandle);
565 #endif /* CONFIG_OF */
567 int power_supply_get_battery_info(struct power_supply *psy,
568 struct power_supply_battery_info *info)
570 struct device_node *battery_np;
574 info->energy_full_design_uwh = -EINVAL;
575 info->charge_full_design_uah = -EINVAL;
576 info->voltage_min_design_uv = -EINVAL;
577 info->precharge_current_ua = -EINVAL;
578 info->charge_term_current_ua = -EINVAL;
579 info->constant_charge_current_max_ua = -EINVAL;
580 info->constant_charge_voltage_max_uv = -EINVAL;
583 dev_warn(&psy->dev, "%s currently only supports devicetree\n",
588 battery_np = of_parse_phandle(psy->of_node, "monitored-battery", 0);
592 err = of_property_read_string(battery_np, "compatible", &value);
596 if (strcmp("simple-battery", value))
599 /* The property and field names below must correspond to elements
600 * in enum power_supply_property. For reasoning, see
601 * Documentation/power/power_supply_class.txt.
604 of_property_read_u32(battery_np, "energy-full-design-microwatt-hours",
605 &info->energy_full_design_uwh);
606 of_property_read_u32(battery_np, "charge-full-design-microamp-hours",
607 &info->charge_full_design_uah);
608 of_property_read_u32(battery_np, "voltage-min-design-microvolt",
609 &info->voltage_min_design_uv);
610 of_property_read_u32(battery_np, "precharge-current-microamp",
611 &info->precharge_current_ua);
612 of_property_read_u32(battery_np, "charge-term-current-microamp",
613 &info->charge_term_current_ua);
614 of_property_read_u32(battery_np, "constant_charge_current_max_microamp",
615 &info->constant_charge_current_max_ua);
616 of_property_read_u32(battery_np, "constant_charge_voltage_max_microvolt",
617 &info->constant_charge_voltage_max_uv);
621 EXPORT_SYMBOL_GPL(power_supply_get_battery_info);
623 int power_supply_get_property(struct power_supply *psy,
624 enum power_supply_property psp,
625 union power_supply_propval *val)
627 if (atomic_read(&psy->use_cnt) <= 0) {
628 if (!psy->initialized)
633 return psy->desc->get_property(psy, psp, val);
635 EXPORT_SYMBOL_GPL(power_supply_get_property);
637 int power_supply_set_property(struct power_supply *psy,
638 enum power_supply_property psp,
639 const union power_supply_propval *val)
641 if (atomic_read(&psy->use_cnt) <= 0 || !psy->desc->set_property)
644 return psy->desc->set_property(psy, psp, val);
646 EXPORT_SYMBOL_GPL(power_supply_set_property);
648 int power_supply_property_is_writeable(struct power_supply *psy,
649 enum power_supply_property psp)
651 if (atomic_read(&psy->use_cnt) <= 0 ||
652 !psy->desc->property_is_writeable)
655 return psy->desc->property_is_writeable(psy, psp);
657 EXPORT_SYMBOL_GPL(power_supply_property_is_writeable);
659 void power_supply_external_power_changed(struct power_supply *psy)
661 if (atomic_read(&psy->use_cnt) <= 0 ||
662 !psy->desc->external_power_changed)
665 psy->desc->external_power_changed(psy);
667 EXPORT_SYMBOL_GPL(power_supply_external_power_changed);
669 int power_supply_powers(struct power_supply *psy, struct device *dev)
671 return sysfs_create_link(&psy->dev.kobj, &dev->kobj, "powers");
673 EXPORT_SYMBOL_GPL(power_supply_powers);
675 static void power_supply_dev_release(struct device *dev)
677 struct power_supply *psy = container_of(dev, struct power_supply, dev);
678 pr_debug("device: '%s': %s\n", dev_name(dev), __func__);
682 int power_supply_reg_notifier(struct notifier_block *nb)
684 return atomic_notifier_chain_register(&power_supply_notifier, nb);
686 EXPORT_SYMBOL_GPL(power_supply_reg_notifier);
688 void power_supply_unreg_notifier(struct notifier_block *nb)
690 atomic_notifier_chain_unregister(&power_supply_notifier, nb);
692 EXPORT_SYMBOL_GPL(power_supply_unreg_notifier);
694 #ifdef CONFIG_THERMAL
695 static int power_supply_read_temp(struct thermal_zone_device *tzd,
698 struct power_supply *psy;
699 union power_supply_propval val;
702 WARN_ON(tzd == NULL);
704 ret = power_supply_get_property(psy, POWER_SUPPLY_PROP_TEMP, &val);
708 /* Convert tenths of degree Celsius to milli degree Celsius. */
709 *temp = val.intval * 100;
714 static struct thermal_zone_device_ops psy_tzd_ops = {
715 .get_temp = power_supply_read_temp,
718 static int psy_register_thermal(struct power_supply *psy)
722 if (psy->desc->no_thermal)
725 /* Register battery zone device psy reports temperature */
726 for (i = 0; i < psy->desc->num_properties; i++) {
727 if (psy->desc->properties[i] == POWER_SUPPLY_PROP_TEMP) {
728 psy->tzd = thermal_zone_device_register(psy->desc->name,
729 0, 0, psy, &psy_tzd_ops, NULL, 0, 0);
730 return PTR_ERR_OR_ZERO(psy->tzd);
736 static void psy_unregister_thermal(struct power_supply *psy)
738 if (IS_ERR_OR_NULL(psy->tzd))
740 thermal_zone_device_unregister(psy->tzd);
743 /* thermal cooling device callbacks */
744 static int ps_get_max_charge_cntl_limit(struct thermal_cooling_device *tcd,
745 unsigned long *state)
747 struct power_supply *psy;
748 union power_supply_propval val;
752 ret = power_supply_get_property(psy,
753 POWER_SUPPLY_PROP_CHARGE_CONTROL_LIMIT_MAX, &val);
762 static int ps_get_cur_chrage_cntl_limit(struct thermal_cooling_device *tcd,
763 unsigned long *state)
765 struct power_supply *psy;
766 union power_supply_propval val;
770 ret = power_supply_get_property(psy,
771 POWER_SUPPLY_PROP_CHARGE_CONTROL_LIMIT, &val);
780 static int ps_set_cur_charge_cntl_limit(struct thermal_cooling_device *tcd,
783 struct power_supply *psy;
784 union power_supply_propval val;
789 ret = psy->desc->set_property(psy,
790 POWER_SUPPLY_PROP_CHARGE_CONTROL_LIMIT, &val);
795 static const struct thermal_cooling_device_ops psy_tcd_ops = {
796 .get_max_state = ps_get_max_charge_cntl_limit,
797 .get_cur_state = ps_get_cur_chrage_cntl_limit,
798 .set_cur_state = ps_set_cur_charge_cntl_limit,
801 static int psy_register_cooler(struct power_supply *psy)
805 /* Register for cooling device if psy can control charging */
806 for (i = 0; i < psy->desc->num_properties; i++) {
807 if (psy->desc->properties[i] ==
808 POWER_SUPPLY_PROP_CHARGE_CONTROL_LIMIT) {
809 psy->tcd = thermal_cooling_device_register(
810 (char *)psy->desc->name,
812 return PTR_ERR_OR_ZERO(psy->tcd);
818 static void psy_unregister_cooler(struct power_supply *psy)
820 if (IS_ERR_OR_NULL(psy->tcd))
822 thermal_cooling_device_unregister(psy->tcd);
825 static int psy_register_thermal(struct power_supply *psy)
830 static void psy_unregister_thermal(struct power_supply *psy)
834 static int psy_register_cooler(struct power_supply *psy)
839 static void psy_unregister_cooler(struct power_supply *psy)
844 static struct power_supply *__must_check
845 __power_supply_register(struct device *parent,
846 const struct power_supply_desc *desc,
847 const struct power_supply_config *cfg,
851 struct power_supply *psy;
855 pr_warn("%s: Expected proper parent device for '%s'\n",
856 __func__, desc->name);
858 psy = kzalloc(sizeof(*psy), GFP_KERNEL);
860 return ERR_PTR(-ENOMEM);
864 device_initialize(dev);
866 dev->class = power_supply_class;
867 dev->type = &power_supply_dev_type;
868 dev->parent = parent;
869 dev->release = power_supply_dev_release;
870 dev_set_drvdata(dev, psy);
873 psy->drv_data = cfg->drv_data;
874 psy->of_node = cfg->of_node;
875 psy->supplied_to = cfg->supplied_to;
876 psy->num_supplicants = cfg->num_supplicants;
879 rc = dev_set_name(dev, "%s", desc->name);
881 goto dev_set_name_failed;
883 INIT_WORK(&psy->changed_work, power_supply_changed_work);
884 INIT_DELAYED_WORK(&psy->deferred_register_work,
885 power_supply_deferred_register_work);
887 rc = power_supply_check_supplies(psy);
889 dev_info(dev, "Not all required supplies found, defer probe\n");
890 goto check_supplies_failed;
893 spin_lock_init(&psy->changed_lock);
894 rc = device_add(dev);
896 goto device_add_failed;
898 rc = device_init_wakeup(dev, ws);
900 goto wakeup_init_failed;
902 rc = psy_register_thermal(psy);
904 goto register_thermal_failed;
906 rc = psy_register_cooler(psy);
908 goto register_cooler_failed;
910 rc = power_supply_create_triggers(psy);
912 goto create_triggers_failed;
915 * Update use_cnt after any uevents (most notably from device_add()).
916 * We are here still during driver's probe but
917 * the power_supply_uevent() calls back driver's get_property
919 * 1. Driver did not assigned the returned struct power_supply,
920 * 2. Driver could not finish initialization (anything in its probe
921 * after calling power_supply_register()).
923 atomic_inc(&psy->use_cnt);
924 psy->initialized = true;
926 queue_delayed_work(system_power_efficient_wq,
927 &psy->deferred_register_work,
928 POWER_SUPPLY_DEFERRED_REGISTER_TIME);
932 create_triggers_failed:
933 psy_unregister_cooler(psy);
934 register_cooler_failed:
935 psy_unregister_thermal(psy);
936 register_thermal_failed:
940 check_supplies_failed:
947 * power_supply_register() - Register new power supply
948 * @parent: Device to be a parent of power supply's device, usually
949 * the device which probe function calls this
950 * @desc: Description of power supply, must be valid through whole
951 * lifetime of this power supply
952 * @cfg: Run-time specific configuration accessed during registering,
955 * Return: A pointer to newly allocated power_supply on success
956 * or ERR_PTR otherwise.
957 * Use power_supply_unregister() on returned power_supply pointer to release
960 struct power_supply *__must_check power_supply_register(struct device *parent,
961 const struct power_supply_desc *desc,
962 const struct power_supply_config *cfg)
964 return __power_supply_register(parent, desc, cfg, true);
966 EXPORT_SYMBOL_GPL(power_supply_register);
969 * power_supply_register_no_ws() - Register new non-waking-source power supply
970 * @parent: Device to be a parent of power supply's device, usually
971 * the device which probe function calls this
972 * @desc: Description of power supply, must be valid through whole
973 * lifetime of this power supply
974 * @cfg: Run-time specific configuration accessed during registering,
977 * Return: A pointer to newly allocated power_supply on success
978 * or ERR_PTR otherwise.
979 * Use power_supply_unregister() on returned power_supply pointer to release
982 struct power_supply *__must_check
983 power_supply_register_no_ws(struct device *parent,
984 const struct power_supply_desc *desc,
985 const struct power_supply_config *cfg)
987 return __power_supply_register(parent, desc, cfg, false);
989 EXPORT_SYMBOL_GPL(power_supply_register_no_ws);
991 static void devm_power_supply_release(struct device *dev, void *res)
993 struct power_supply **psy = res;
995 power_supply_unregister(*psy);
999 * devm_power_supply_register() - Register managed power supply
1000 * @parent: Device to be a parent of power supply's device, usually
1001 * the device which probe function calls this
1002 * @desc: Description of power supply, must be valid through whole
1003 * lifetime of this power supply
1004 * @cfg: Run-time specific configuration accessed during registering,
1007 * Return: A pointer to newly allocated power_supply on success
1008 * or ERR_PTR otherwise.
1009 * The returned power_supply pointer will be automatically unregistered
1012 struct power_supply *__must_check
1013 devm_power_supply_register(struct device *parent,
1014 const struct power_supply_desc *desc,
1015 const struct power_supply_config *cfg)
1017 struct power_supply **ptr, *psy;
1019 ptr = devres_alloc(devm_power_supply_release, sizeof(*ptr), GFP_KERNEL);
1022 return ERR_PTR(-ENOMEM);
1023 psy = __power_supply_register(parent, desc, cfg, true);
1028 devres_add(parent, ptr);
1032 EXPORT_SYMBOL_GPL(devm_power_supply_register);
1035 * devm_power_supply_register_no_ws() - Register managed non-waking-source power supply
1036 * @parent: Device to be a parent of power supply's device, usually
1037 * the device which probe function calls this
1038 * @desc: Description of power supply, must be valid through whole
1039 * lifetime of this power supply
1040 * @cfg: Run-time specific configuration accessed during registering,
1043 * Return: A pointer to newly allocated power_supply on success
1044 * or ERR_PTR otherwise.
1045 * The returned power_supply pointer will be automatically unregistered
1048 struct power_supply *__must_check
1049 devm_power_supply_register_no_ws(struct device *parent,
1050 const struct power_supply_desc *desc,
1051 const struct power_supply_config *cfg)
1053 struct power_supply **ptr, *psy;
1055 ptr = devres_alloc(devm_power_supply_release, sizeof(*ptr), GFP_KERNEL);
1058 return ERR_PTR(-ENOMEM);
1059 psy = __power_supply_register(parent, desc, cfg, false);
1064 devres_add(parent, ptr);
1068 EXPORT_SYMBOL_GPL(devm_power_supply_register_no_ws);
1071 * power_supply_unregister() - Remove this power supply from system
1072 * @psy: Pointer to power supply to unregister
1074 * Remove this power supply from the system. The resources of power supply
1075 * will be freed here or on last power_supply_put() call.
1077 void power_supply_unregister(struct power_supply *psy)
1079 WARN_ON(atomic_dec_return(&psy->use_cnt));
1080 psy->removing = true;
1081 cancel_work_sync(&psy->changed_work);
1082 cancel_delayed_work_sync(&psy->deferred_register_work);
1083 sysfs_remove_link(&psy->dev.kobj, "powers");
1084 power_supply_remove_triggers(psy);
1085 psy_unregister_cooler(psy);
1086 psy_unregister_thermal(psy);
1087 device_init_wakeup(&psy->dev, false);
1088 device_unregister(&psy->dev);
1090 EXPORT_SYMBOL_GPL(power_supply_unregister);
1092 void *power_supply_get_drvdata(struct power_supply *psy)
1094 return psy->drv_data;
1096 EXPORT_SYMBOL_GPL(power_supply_get_drvdata);
1098 static int __init power_supply_class_init(void)
1100 power_supply_class = class_create(THIS_MODULE, "power_supply");
1102 if (IS_ERR(power_supply_class))
1103 return PTR_ERR(power_supply_class);
1105 power_supply_class->dev_uevent = power_supply_uevent;
1106 power_supply_init_attrs(&power_supply_dev_type);
1111 static void __exit power_supply_class_exit(void)
1113 class_destroy(power_supply_class);
1116 subsys_initcall(power_supply_class_init);
1117 module_exit(power_supply_class_exit);
1119 MODULE_DESCRIPTION("Universal power supply monitor class");
1120 MODULE_AUTHOR("Ian Molton <spyro@f2s.com>, "
1122 "Anton Vorontsov <cbou@mail.ru>");
1123 MODULE_LICENSE("GPL");