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/thermal.h>
23 #include "power_supply.h"
25 /* exported for the APM Power driver, APM emulation */
26 struct class *power_supply_class;
27 EXPORT_SYMBOL_GPL(power_supply_class);
29 ATOMIC_NOTIFIER_HEAD(power_supply_notifier);
30 EXPORT_SYMBOL_GPL(power_supply_notifier);
32 static struct device_type power_supply_dev_type;
34 #define POWER_SUPPLY_DEFERRED_REGISTER_TIME msecs_to_jiffies(10)
36 static bool __power_supply_is_supplied_by(struct power_supply *supplier,
37 struct power_supply *supply)
41 if (!supply->supplied_from && !supplier->supplied_to)
44 /* Support both supplied_to and supplied_from modes */
45 if (supply->supplied_from) {
46 if (!supplier->desc->name)
48 for (i = 0; i < supply->num_supplies; i++)
49 if (!strcmp(supplier->desc->name, supply->supplied_from[i]))
52 if (!supply->desc->name)
54 for (i = 0; i < supplier->num_supplicants; i++)
55 if (!strcmp(supplier->supplied_to[i], supply->desc->name))
62 static int __power_supply_changed_work(struct device *dev, void *data)
64 struct power_supply *psy = data;
65 struct power_supply *pst = dev_get_drvdata(dev);
67 if (__power_supply_is_supplied_by(psy, pst)) {
68 if (pst->desc->external_power_changed)
69 pst->desc->external_power_changed(pst);
75 static void power_supply_changed_work(struct work_struct *work)
78 struct power_supply *psy = container_of(work, struct power_supply,
81 dev_dbg(&psy->dev, "%s\n", __func__);
83 spin_lock_irqsave(&psy->changed_lock, flags);
85 * Check 'changed' here to avoid issues due to race between
86 * power_supply_changed() and this routine. In worst case
87 * power_supply_changed() can be called again just before we take above
88 * lock. During the first call of this routine we will mark 'changed' as
89 * false and it will stay false for the next call as well.
91 if (likely(psy->changed)) {
93 spin_unlock_irqrestore(&psy->changed_lock, flags);
94 class_for_each_device(power_supply_class, NULL, psy,
95 __power_supply_changed_work);
96 power_supply_update_leds(psy);
97 atomic_notifier_call_chain(&power_supply_notifier,
98 PSY_EVENT_PROP_CHANGED, psy);
99 kobject_uevent(&psy->dev.kobj, KOBJ_CHANGE);
100 spin_lock_irqsave(&psy->changed_lock, flags);
104 * Hold the wakeup_source until all events are processed.
105 * power_supply_changed() might have called again and have set 'changed'
108 if (likely(!psy->changed))
110 spin_unlock_irqrestore(&psy->changed_lock, flags);
113 void power_supply_changed(struct power_supply *psy)
117 dev_dbg(&psy->dev, "%s\n", __func__);
119 spin_lock_irqsave(&psy->changed_lock, flags);
121 pm_stay_awake(&psy->dev);
122 spin_unlock_irqrestore(&psy->changed_lock, flags);
123 schedule_work(&psy->changed_work);
125 EXPORT_SYMBOL_GPL(power_supply_changed);
128 * Notify that power supply was registered after parent finished the probing.
130 * Often power supply is registered from driver's probe function. However
131 * calling power_supply_changed() directly from power_supply_register()
132 * would lead to execution of get_property() function provided by the driver
133 * too early - before the probe ends.
135 * Avoid that by waiting on parent's mutex.
137 static void power_supply_deferred_register_work(struct work_struct *work)
139 struct power_supply *psy = container_of(work, struct power_supply,
140 deferred_register_work.work);
143 mutex_lock(&psy->dev.parent->mutex);
145 power_supply_changed(psy);
148 mutex_unlock(&psy->dev.parent->mutex);
152 #include <linux/of.h>
154 static int __power_supply_populate_supplied_from(struct device *dev,
157 struct power_supply *psy = data;
158 struct power_supply *epsy = dev_get_drvdata(dev);
159 struct device_node *np;
163 np = of_parse_phandle(psy->of_node, "power-supplies", i++);
167 if (np == epsy->of_node) {
168 dev_info(&psy->dev, "%s: Found supply : %s\n",
169 psy->desc->name, epsy->desc->name);
170 psy->supplied_from[i-1] = (char *)epsy->desc->name;
181 static int power_supply_populate_supplied_from(struct power_supply *psy)
185 error = class_for_each_device(power_supply_class, NULL, psy,
186 __power_supply_populate_supplied_from);
188 dev_dbg(&psy->dev, "%s %d\n", __func__, error);
193 static int __power_supply_find_supply_from_node(struct device *dev,
196 struct device_node *np = data;
197 struct power_supply *epsy = dev_get_drvdata(dev);
199 /* returning non-zero breaks out of class_for_each_device loop */
200 if (epsy->of_node == np)
206 static int power_supply_find_supply_from_node(struct device_node *supply_node)
211 * class_for_each_device() either returns its own errors or values
212 * returned by __power_supply_find_supply_from_node().
214 * __power_supply_find_supply_from_node() will return 0 (no match)
217 * We return 0 if class_for_each_device() returned 1, -EPROBE_DEFER if
218 * it returned 0, or error as returned by it.
220 error = class_for_each_device(power_supply_class, NULL, supply_node,
221 __power_supply_find_supply_from_node);
223 return error ? (error == 1 ? 0 : error) : -EPROBE_DEFER;
226 static int power_supply_check_supplies(struct power_supply *psy)
228 struct device_node *np;
231 /* If there is already a list honor it */
232 if (psy->supplied_from && psy->num_supplies > 0)
235 /* No device node found, nothing to do */
242 np = of_parse_phandle(psy->of_node, "power-supplies", cnt++);
246 ret = power_supply_find_supply_from_node(np);
250 dev_dbg(&psy->dev, "Failed to find supply!\n");
255 /* Missing valid "power-supplies" entries */
259 /* All supplies found, allocate char ** array for filling */
260 psy->supplied_from = devm_kzalloc(&psy->dev, sizeof(psy->supplied_from),
262 if (!psy->supplied_from) {
263 dev_err(&psy->dev, "Couldn't allocate memory for supply list\n");
267 *psy->supplied_from = devm_kzalloc(&psy->dev,
268 sizeof(char *) * (cnt - 1),
270 if (!*psy->supplied_from) {
271 dev_err(&psy->dev, "Couldn't allocate memory for supply list\n");
275 return power_supply_populate_supplied_from(psy);
278 static int power_supply_check_supplies(struct power_supply *psy)
282 if (!psy->dev.parent)
285 nval = device_property_read_string_array(psy->dev.parent,
286 "supplied-from", NULL, 0);
290 psy->supplied_from = devm_kmalloc_array(&psy->dev, nval,
291 sizeof(char *), GFP_KERNEL);
292 if (!psy->supplied_from)
295 ret = device_property_read_string_array(psy->dev.parent,
296 "supplied-from", (const char **)psy->supplied_from, nval);
300 psy->num_supplies = nval;
306 struct psy_am_i_supplied_data {
307 struct power_supply *psy;
311 static int __power_supply_am_i_supplied(struct device *dev, void *_data)
313 union power_supply_propval ret = {0,};
314 struct power_supply *epsy = dev_get_drvdata(dev);
315 struct psy_am_i_supplied_data *data = _data;
318 if (__power_supply_is_supplied_by(epsy, data->psy))
319 if (!epsy->desc->get_property(epsy, POWER_SUPPLY_PROP_ONLINE,
326 int power_supply_am_i_supplied(struct power_supply *psy)
328 struct psy_am_i_supplied_data data = { psy, 0 };
331 error = class_for_each_device(power_supply_class, NULL, &data,
332 __power_supply_am_i_supplied);
334 dev_dbg(&psy->dev, "%s count %u err %d\n", __func__, data.count, error);
341 EXPORT_SYMBOL_GPL(power_supply_am_i_supplied);
343 static int __power_supply_is_system_supplied(struct device *dev, void *data)
345 union power_supply_propval ret = {0,};
346 struct power_supply *psy = dev_get_drvdata(dev);
347 unsigned int *count = data;
350 if (psy->desc->type != POWER_SUPPLY_TYPE_BATTERY)
351 if (!psy->desc->get_property(psy, POWER_SUPPLY_PROP_ONLINE,
358 int power_supply_is_system_supplied(void)
361 unsigned int count = 0;
363 error = class_for_each_device(power_supply_class, NULL, &count,
364 __power_supply_is_system_supplied);
367 * If no power class device was found at all, most probably we are
368 * running on a desktop system, so assume we are on mains power.
375 EXPORT_SYMBOL_GPL(power_supply_is_system_supplied);
377 int power_supply_set_battery_charged(struct power_supply *psy)
379 if (atomic_read(&psy->use_cnt) >= 0 &&
380 psy->desc->type == POWER_SUPPLY_TYPE_BATTERY &&
381 psy->desc->set_charged) {
382 psy->desc->set_charged(psy);
388 EXPORT_SYMBOL_GPL(power_supply_set_battery_charged);
390 static int power_supply_match_device_by_name(struct device *dev, const void *data)
392 const char *name = data;
393 struct power_supply *psy = dev_get_drvdata(dev);
395 return strcmp(psy->desc->name, name) == 0;
399 * power_supply_get_by_name() - Search for a power supply and returns its ref
400 * @name: Power supply name to fetch
402 * If power supply was found, it increases reference count for the
403 * internal power supply's device. The user should power_supply_put()
406 * Return: On success returns a reference to a power supply with
407 * matching name equals to @name, a NULL otherwise.
409 struct power_supply *power_supply_get_by_name(const char *name)
411 struct power_supply *psy = NULL;
412 struct device *dev = class_find_device(power_supply_class, NULL, name,
413 power_supply_match_device_by_name);
416 psy = dev_get_drvdata(dev);
417 atomic_inc(&psy->use_cnt);
422 EXPORT_SYMBOL_GPL(power_supply_get_by_name);
425 * power_supply_put() - Drop reference obtained with power_supply_get_by_name
426 * @psy: Reference to put
428 * The reference to power supply should be put before unregistering
431 void power_supply_put(struct power_supply *psy)
435 atomic_dec(&psy->use_cnt);
436 put_device(&psy->dev);
438 EXPORT_SYMBOL_GPL(power_supply_put);
441 static int power_supply_match_device_node(struct device *dev, const void *data)
443 return dev->parent && dev->parent->of_node == data;
447 * power_supply_get_by_phandle() - Search for a power supply and returns its ref
448 * @np: Pointer to device node holding phandle property
449 * @property: Name of property holding a power supply name
451 * If power supply was found, it increases reference count for the
452 * internal power supply's device. The user should power_supply_put()
455 * Return: On success returns a reference to a power supply with
456 * matching name equals to value under @property, NULL or ERR_PTR otherwise.
458 struct power_supply *power_supply_get_by_phandle(struct device_node *np,
459 const char *property)
461 struct device_node *power_supply_np;
462 struct power_supply *psy = NULL;
465 power_supply_np = of_parse_phandle(np, property, 0);
466 if (!power_supply_np)
467 return ERR_PTR(-ENODEV);
469 dev = class_find_device(power_supply_class, NULL, power_supply_np,
470 power_supply_match_device_node);
472 of_node_put(power_supply_np);
475 psy = dev_get_drvdata(dev);
476 atomic_inc(&psy->use_cnt);
481 EXPORT_SYMBOL_GPL(power_supply_get_by_phandle);
483 static void devm_power_supply_put(struct device *dev, void *res)
485 struct power_supply **psy = res;
487 power_supply_put(*psy);
491 * devm_power_supply_get_by_phandle() - Resource managed version of
492 * power_supply_get_by_phandle()
493 * @dev: Pointer to device holding phandle property
494 * @property: Name of property holding a power supply phandle
496 * Return: On success returns a reference to a power supply with
497 * matching name equals to value under @property, NULL or ERR_PTR otherwise.
499 struct power_supply *devm_power_supply_get_by_phandle(struct device *dev,
500 const char *property)
502 struct power_supply **ptr, *psy;
505 return ERR_PTR(-ENODEV);
507 ptr = devres_alloc(devm_power_supply_put, sizeof(*ptr), GFP_KERNEL);
509 return ERR_PTR(-ENOMEM);
511 psy = power_supply_get_by_phandle(dev->of_node, property);
512 if (IS_ERR_OR_NULL(psy)) {
516 devres_add(dev, ptr);
520 EXPORT_SYMBOL_GPL(devm_power_supply_get_by_phandle);
521 #endif /* CONFIG_OF */
523 int power_supply_get_battery_info(struct power_supply *psy,
524 struct power_supply_battery_info *info)
526 struct device_node *battery_np;
530 info->energy_full_design_uwh = -EINVAL;
531 info->charge_full_design_uah = -EINVAL;
532 info->voltage_min_design_uv = -EINVAL;
533 info->precharge_current_ua = -EINVAL;
534 info->charge_term_current_ua = -EINVAL;
535 info->constant_charge_current_max_ua = -EINVAL;
536 info->constant_charge_voltage_max_uv = -EINVAL;
539 dev_warn(&psy->dev, "%s currently only supports devicetree\n",
544 battery_np = of_parse_phandle(psy->of_node, "monitored-battery", 0);
548 err = of_property_read_string(battery_np, "compatible", &value);
552 if (strcmp("simple-battery", value))
555 /* The property and field names below must correspond to elements
556 * in enum power_supply_property. For reasoning, see
557 * Documentation/power/power_supply_class.txt.
560 of_property_read_u32(battery_np, "energy-full-design-microwatt-hours",
561 &info->energy_full_design_uwh);
562 of_property_read_u32(battery_np, "charge-full-design-microamp-hours",
563 &info->charge_full_design_uah);
564 of_property_read_u32(battery_np, "voltage-min-design-microvolt",
565 &info->voltage_min_design_uv);
566 of_property_read_u32(battery_np, "precharge-current-microamp",
567 &info->precharge_current_ua);
568 of_property_read_u32(battery_np, "charge-term-current-microamp",
569 &info->charge_term_current_ua);
570 of_property_read_u32(battery_np, "constant_charge_current_max_microamp",
571 &info->constant_charge_current_max_ua);
572 of_property_read_u32(battery_np, "constant_charge_voltage_max_microvolt",
573 &info->constant_charge_voltage_max_uv);
577 EXPORT_SYMBOL_GPL(power_supply_get_battery_info);
579 int power_supply_get_property(struct power_supply *psy,
580 enum power_supply_property psp,
581 union power_supply_propval *val)
583 if (atomic_read(&psy->use_cnt) <= 0) {
584 if (!psy->initialized)
589 return psy->desc->get_property(psy, psp, val);
591 EXPORT_SYMBOL_GPL(power_supply_get_property);
593 int power_supply_set_property(struct power_supply *psy,
594 enum power_supply_property psp,
595 const union power_supply_propval *val)
597 if (atomic_read(&psy->use_cnt) <= 0 || !psy->desc->set_property)
600 return psy->desc->set_property(psy, psp, val);
602 EXPORT_SYMBOL_GPL(power_supply_set_property);
604 int power_supply_property_is_writeable(struct power_supply *psy,
605 enum power_supply_property psp)
607 if (atomic_read(&psy->use_cnt) <= 0 ||
608 !psy->desc->property_is_writeable)
611 return psy->desc->property_is_writeable(psy, psp);
613 EXPORT_SYMBOL_GPL(power_supply_property_is_writeable);
615 void power_supply_external_power_changed(struct power_supply *psy)
617 if (atomic_read(&psy->use_cnt) <= 0 ||
618 !psy->desc->external_power_changed)
621 psy->desc->external_power_changed(psy);
623 EXPORT_SYMBOL_GPL(power_supply_external_power_changed);
625 int power_supply_powers(struct power_supply *psy, struct device *dev)
627 return sysfs_create_link(&psy->dev.kobj, &dev->kobj, "powers");
629 EXPORT_SYMBOL_GPL(power_supply_powers);
631 static void power_supply_dev_release(struct device *dev)
633 struct power_supply *psy = container_of(dev, struct power_supply, dev);
634 pr_debug("device: '%s': %s\n", dev_name(dev), __func__);
638 int power_supply_reg_notifier(struct notifier_block *nb)
640 return atomic_notifier_chain_register(&power_supply_notifier, nb);
642 EXPORT_SYMBOL_GPL(power_supply_reg_notifier);
644 void power_supply_unreg_notifier(struct notifier_block *nb)
646 atomic_notifier_chain_unregister(&power_supply_notifier, nb);
648 EXPORT_SYMBOL_GPL(power_supply_unreg_notifier);
650 #ifdef CONFIG_THERMAL
651 static int power_supply_read_temp(struct thermal_zone_device *tzd,
654 struct power_supply *psy;
655 union power_supply_propval val;
658 WARN_ON(tzd == NULL);
660 ret = power_supply_get_property(psy, POWER_SUPPLY_PROP_TEMP, &val);
664 /* Convert tenths of degree Celsius to milli degree Celsius. */
665 *temp = val.intval * 100;
670 static struct thermal_zone_device_ops psy_tzd_ops = {
671 .get_temp = power_supply_read_temp,
674 static int psy_register_thermal(struct power_supply *psy)
678 if (psy->desc->no_thermal)
681 /* Register battery zone device psy reports temperature */
682 for (i = 0; i < psy->desc->num_properties; i++) {
683 if (psy->desc->properties[i] == POWER_SUPPLY_PROP_TEMP) {
684 psy->tzd = thermal_zone_device_register(psy->desc->name,
685 0, 0, psy, &psy_tzd_ops, NULL, 0, 0);
686 return PTR_ERR_OR_ZERO(psy->tzd);
692 static void psy_unregister_thermal(struct power_supply *psy)
694 if (IS_ERR_OR_NULL(psy->tzd))
696 thermal_zone_device_unregister(psy->tzd);
699 /* thermal cooling device callbacks */
700 static int ps_get_max_charge_cntl_limit(struct thermal_cooling_device *tcd,
701 unsigned long *state)
703 struct power_supply *psy;
704 union power_supply_propval val;
708 ret = power_supply_get_property(psy,
709 POWER_SUPPLY_PROP_CHARGE_CONTROL_LIMIT_MAX, &val);
718 static int ps_get_cur_chrage_cntl_limit(struct thermal_cooling_device *tcd,
719 unsigned long *state)
721 struct power_supply *psy;
722 union power_supply_propval val;
726 ret = power_supply_get_property(psy,
727 POWER_SUPPLY_PROP_CHARGE_CONTROL_LIMIT, &val);
736 static int ps_set_cur_charge_cntl_limit(struct thermal_cooling_device *tcd,
739 struct power_supply *psy;
740 union power_supply_propval val;
745 ret = psy->desc->set_property(psy,
746 POWER_SUPPLY_PROP_CHARGE_CONTROL_LIMIT, &val);
751 static const struct thermal_cooling_device_ops psy_tcd_ops = {
752 .get_max_state = ps_get_max_charge_cntl_limit,
753 .get_cur_state = ps_get_cur_chrage_cntl_limit,
754 .set_cur_state = ps_set_cur_charge_cntl_limit,
757 static int psy_register_cooler(struct power_supply *psy)
761 /* Register for cooling device if psy can control charging */
762 for (i = 0; i < psy->desc->num_properties; i++) {
763 if (psy->desc->properties[i] ==
764 POWER_SUPPLY_PROP_CHARGE_CONTROL_LIMIT) {
765 psy->tcd = thermal_cooling_device_register(
766 (char *)psy->desc->name,
768 return PTR_ERR_OR_ZERO(psy->tcd);
774 static void psy_unregister_cooler(struct power_supply *psy)
776 if (IS_ERR_OR_NULL(psy->tcd))
778 thermal_cooling_device_unregister(psy->tcd);
781 static int psy_register_thermal(struct power_supply *psy)
786 static void psy_unregister_thermal(struct power_supply *psy)
790 static int psy_register_cooler(struct power_supply *psy)
795 static void psy_unregister_cooler(struct power_supply *psy)
800 static struct power_supply *__must_check
801 __power_supply_register(struct device *parent,
802 const struct power_supply_desc *desc,
803 const struct power_supply_config *cfg,
807 struct power_supply *psy;
811 pr_warn("%s: Expected proper parent device for '%s'\n",
812 __func__, desc->name);
814 psy = kzalloc(sizeof(*psy), GFP_KERNEL);
816 return ERR_PTR(-ENOMEM);
820 device_initialize(dev);
822 dev->class = power_supply_class;
823 dev->type = &power_supply_dev_type;
824 dev->parent = parent;
825 dev->release = power_supply_dev_release;
826 dev_set_drvdata(dev, psy);
829 psy->drv_data = cfg->drv_data;
830 psy->of_node = cfg->of_node;
831 psy->supplied_to = cfg->supplied_to;
832 psy->num_supplicants = cfg->num_supplicants;
835 rc = dev_set_name(dev, "%s", desc->name);
837 goto dev_set_name_failed;
839 INIT_WORK(&psy->changed_work, power_supply_changed_work);
840 INIT_DELAYED_WORK(&psy->deferred_register_work,
841 power_supply_deferred_register_work);
843 rc = power_supply_check_supplies(psy);
845 dev_info(dev, "Not all required supplies found, defer probe\n");
846 goto check_supplies_failed;
849 spin_lock_init(&psy->changed_lock);
850 rc = device_init_wakeup(dev, ws);
852 goto wakeup_init_failed;
854 rc = device_add(dev);
856 goto device_add_failed;
858 rc = psy_register_thermal(psy);
860 goto register_thermal_failed;
862 rc = psy_register_cooler(psy);
864 goto register_cooler_failed;
866 rc = power_supply_create_triggers(psy);
868 goto create_triggers_failed;
871 * Update use_cnt after any uevents (most notably from device_add()).
872 * We are here still during driver's probe but
873 * the power_supply_uevent() calls back driver's get_property
875 * 1. Driver did not assigned the returned struct power_supply,
876 * 2. Driver could not finish initialization (anything in its probe
877 * after calling power_supply_register()).
879 atomic_inc(&psy->use_cnt);
880 psy->initialized = true;
882 queue_delayed_work(system_power_efficient_wq,
883 &psy->deferred_register_work,
884 POWER_SUPPLY_DEFERRED_REGISTER_TIME);
888 create_triggers_failed:
889 psy_unregister_cooler(psy);
890 register_cooler_failed:
891 psy_unregister_thermal(psy);
892 register_thermal_failed:
896 check_supplies_failed:
903 * power_supply_register() - Register new power supply
904 * @parent: Device to be a parent of power supply's device, usually
905 * the device which probe function calls this
906 * @desc: Description of power supply, must be valid through whole
907 * lifetime of this power supply
908 * @cfg: Run-time specific configuration accessed during registering,
911 * Return: A pointer to newly allocated power_supply on success
912 * or ERR_PTR otherwise.
913 * Use power_supply_unregister() on returned power_supply pointer to release
916 struct power_supply *__must_check power_supply_register(struct device *parent,
917 const struct power_supply_desc *desc,
918 const struct power_supply_config *cfg)
920 return __power_supply_register(parent, desc, cfg, true);
922 EXPORT_SYMBOL_GPL(power_supply_register);
925 * power_supply_register_no_ws() - Register new non-waking-source power supply
926 * @parent: Device to be a parent of power supply's device, usually
927 * the device which probe function calls this
928 * @desc: Description of power supply, must be valid through whole
929 * lifetime of this power supply
930 * @cfg: Run-time specific configuration accessed during registering,
933 * Return: A pointer to newly allocated power_supply on success
934 * or ERR_PTR otherwise.
935 * Use power_supply_unregister() on returned power_supply pointer to release
938 struct power_supply *__must_check
939 power_supply_register_no_ws(struct device *parent,
940 const struct power_supply_desc *desc,
941 const struct power_supply_config *cfg)
943 return __power_supply_register(parent, desc, cfg, false);
945 EXPORT_SYMBOL_GPL(power_supply_register_no_ws);
947 static void devm_power_supply_release(struct device *dev, void *res)
949 struct power_supply **psy = res;
951 power_supply_unregister(*psy);
955 * devm_power_supply_register() - Register managed power supply
956 * @parent: Device to be a parent of power supply's device, usually
957 * the device which probe function calls this
958 * @desc: Description of power supply, must be valid through whole
959 * lifetime of this power supply
960 * @cfg: Run-time specific configuration accessed during registering,
963 * Return: A pointer to newly allocated power_supply on success
964 * or ERR_PTR otherwise.
965 * The returned power_supply pointer will be automatically unregistered
968 struct power_supply *__must_check
969 devm_power_supply_register(struct device *parent,
970 const struct power_supply_desc *desc,
971 const struct power_supply_config *cfg)
973 struct power_supply **ptr, *psy;
975 ptr = devres_alloc(devm_power_supply_release, sizeof(*ptr), GFP_KERNEL);
978 return ERR_PTR(-ENOMEM);
979 psy = __power_supply_register(parent, desc, cfg, true);
984 devres_add(parent, ptr);
988 EXPORT_SYMBOL_GPL(devm_power_supply_register);
991 * devm_power_supply_register_no_ws() - Register managed non-waking-source power supply
992 * @parent: Device to be a parent of power supply's device, usually
993 * the device which probe function calls this
994 * @desc: Description of power supply, must be valid through whole
995 * lifetime of this power supply
996 * @cfg: Run-time specific configuration accessed during registering,
999 * Return: A pointer to newly allocated power_supply on success
1000 * or ERR_PTR otherwise.
1001 * The returned power_supply pointer will be automatically unregistered
1004 struct power_supply *__must_check
1005 devm_power_supply_register_no_ws(struct device *parent,
1006 const struct power_supply_desc *desc,
1007 const struct power_supply_config *cfg)
1009 struct power_supply **ptr, *psy;
1011 ptr = devres_alloc(devm_power_supply_release, sizeof(*ptr), GFP_KERNEL);
1014 return ERR_PTR(-ENOMEM);
1015 psy = __power_supply_register(parent, desc, cfg, false);
1020 devres_add(parent, ptr);
1024 EXPORT_SYMBOL_GPL(devm_power_supply_register_no_ws);
1027 * power_supply_unregister() - Remove this power supply from system
1028 * @psy: Pointer to power supply to unregister
1030 * Remove this power supply from the system. The resources of power supply
1031 * will be freed here or on last power_supply_put() call.
1033 void power_supply_unregister(struct power_supply *psy)
1035 WARN_ON(atomic_dec_return(&psy->use_cnt));
1036 cancel_work_sync(&psy->changed_work);
1037 cancel_delayed_work_sync(&psy->deferred_register_work);
1038 sysfs_remove_link(&psy->dev.kobj, "powers");
1039 power_supply_remove_triggers(psy);
1040 psy_unregister_cooler(psy);
1041 psy_unregister_thermal(psy);
1042 device_init_wakeup(&psy->dev, false);
1043 device_unregister(&psy->dev);
1045 EXPORT_SYMBOL_GPL(power_supply_unregister);
1047 void *power_supply_get_drvdata(struct power_supply *psy)
1049 return psy->drv_data;
1051 EXPORT_SYMBOL_GPL(power_supply_get_drvdata);
1053 static int __init power_supply_class_init(void)
1055 power_supply_class = class_create(THIS_MODULE, "power_supply");
1057 if (IS_ERR(power_supply_class))
1058 return PTR_ERR(power_supply_class);
1060 power_supply_class->dev_uevent = power_supply_uevent;
1061 power_supply_init_attrs(&power_supply_dev_type);
1066 static void __exit power_supply_class_exit(void)
1068 class_destroy(power_supply_class);
1071 subsys_initcall(power_supply_class_init);
1072 module_exit(power_supply_class_exit);
1074 MODULE_DESCRIPTION("Universal power supply monitor class");
1075 MODULE_AUTHOR("Ian Molton <spyro@f2s.com>, "
1077 "Anton Vorontsov <cbou@mail.ru>");
1078 MODULE_LICENSE("GPL");