HID: hiddev: Fix slab-out-of-bounds write in hiddev_ioctl_usage()
[linux/fpc-iii.git] / drivers / power / power_supply_core.c
blob9281e42c9ed56808731bfd12f7558351340a752d
1 /*
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 #define POWER_SUPPLY_DEFERRED_REGISTER_TIME msecs_to_jiffies(10)
35 static bool __power_supply_is_supplied_by(struct power_supply *supplier,
36 struct power_supply *supply)
38 int i;
40 if (!supply->supplied_from && !supplier->supplied_to)
41 return false;
43 /* Support both supplied_to and supplied_from modes */
44 if (supply->supplied_from) {
45 if (!supplier->desc->name)
46 return false;
47 for (i = 0; i < supply->num_supplies; i++)
48 if (!strcmp(supplier->desc->name, supply->supplied_from[i]))
49 return true;
50 } else {
51 if (!supply->desc->name)
52 return false;
53 for (i = 0; i < supplier->num_supplicants; i++)
54 if (!strcmp(supplier->supplied_to[i], supply->desc->name))
55 return true;
58 return false;
61 static int __power_supply_changed_work(struct device *dev, void *data)
63 struct power_supply *psy = data;
64 struct power_supply *pst = dev_get_drvdata(dev);
66 if (__power_supply_is_supplied_by(psy, pst)) {
67 if (pst->desc->external_power_changed)
68 pst->desc->external_power_changed(pst);
71 return 0;
74 static void power_supply_changed_work(struct work_struct *work)
76 unsigned long flags;
77 struct power_supply *psy = container_of(work, struct power_supply,
78 changed_work);
80 dev_dbg(&psy->dev, "%s\n", __func__);
82 spin_lock_irqsave(&psy->changed_lock, flags);
84 * Check 'changed' here to avoid issues due to race between
85 * power_supply_changed() and this routine. In worst case
86 * power_supply_changed() can be called again just before we take above
87 * lock. During the first call of this routine we will mark 'changed' as
88 * false and it will stay false for the next call as well.
90 if (likely(psy->changed)) {
91 psy->changed = false;
92 spin_unlock_irqrestore(&psy->changed_lock, flags);
93 class_for_each_device(power_supply_class, NULL, psy,
94 __power_supply_changed_work);
95 power_supply_update_leds(psy);
96 atomic_notifier_call_chain(&power_supply_notifier,
97 PSY_EVENT_PROP_CHANGED, psy);
98 kobject_uevent(&psy->dev.kobj, KOBJ_CHANGE);
99 spin_lock_irqsave(&psy->changed_lock, flags);
103 * Hold the wakeup_source until all events are processed.
104 * power_supply_changed() might have called again and have set 'changed'
105 * to true.
107 if (likely(!psy->changed))
108 pm_relax(&psy->dev);
109 spin_unlock_irqrestore(&psy->changed_lock, flags);
112 void power_supply_changed(struct power_supply *psy)
114 unsigned long flags;
116 dev_dbg(&psy->dev, "%s\n", __func__);
118 spin_lock_irqsave(&psy->changed_lock, flags);
119 psy->changed = true;
120 pm_stay_awake(&psy->dev);
121 spin_unlock_irqrestore(&psy->changed_lock, flags);
122 schedule_work(&psy->changed_work);
124 EXPORT_SYMBOL_GPL(power_supply_changed);
127 * Notify that power supply was registered after parent finished the probing.
129 * Often power supply is registered from driver's probe function. However
130 * calling power_supply_changed() directly from power_supply_register()
131 * would lead to execution of get_property() function provided by the driver
132 * too early - before the probe ends.
134 * Avoid that by waiting on parent's mutex.
136 static void power_supply_deferred_register_work(struct work_struct *work)
138 struct power_supply *psy = container_of(work, struct power_supply,
139 deferred_register_work.work);
141 if (psy->dev.parent)
142 mutex_lock(&psy->dev.parent->mutex);
144 power_supply_changed(psy);
146 if (psy->dev.parent)
147 mutex_unlock(&psy->dev.parent->mutex);
150 #ifdef CONFIG_OF
151 #include <linux/of.h>
153 static int __power_supply_populate_supplied_from(struct device *dev,
154 void *data)
156 struct power_supply *psy = data;
157 struct power_supply *epsy = dev_get_drvdata(dev);
158 struct device_node *np;
159 int i = 0;
161 do {
162 np = of_parse_phandle(psy->of_node, "power-supplies", i++);
163 if (!np)
164 break;
166 if (np == epsy->of_node) {
167 dev_info(&psy->dev, "%s: Found supply : %s\n",
168 psy->desc->name, epsy->desc->name);
169 psy->supplied_from[i-1] = (char *)epsy->desc->name;
170 psy->num_supplies++;
171 of_node_put(np);
172 break;
174 of_node_put(np);
175 } while (np);
177 return 0;
180 static int power_supply_populate_supplied_from(struct power_supply *psy)
182 int error;
184 error = class_for_each_device(power_supply_class, NULL, psy,
185 __power_supply_populate_supplied_from);
187 dev_dbg(&psy->dev, "%s %d\n", __func__, error);
189 return error;
192 static int __power_supply_find_supply_from_node(struct device *dev,
193 void *data)
195 struct device_node *np = data;
196 struct power_supply *epsy = dev_get_drvdata(dev);
198 /* returning non-zero breaks out of class_for_each_device loop */
199 if (epsy->of_node == np)
200 return 1;
202 return 0;
205 static int power_supply_find_supply_from_node(struct device_node *supply_node)
207 int error;
210 * class_for_each_device() either returns its own errors or values
211 * returned by __power_supply_find_supply_from_node().
213 * __power_supply_find_supply_from_node() will return 0 (no match)
214 * or 1 (match).
216 * We return 0 if class_for_each_device() returned 1, -EPROBE_DEFER if
217 * it returned 0, or error as returned by it.
219 error = class_for_each_device(power_supply_class, NULL, supply_node,
220 __power_supply_find_supply_from_node);
222 return error ? (error == 1 ? 0 : error) : -EPROBE_DEFER;
225 static int power_supply_check_supplies(struct power_supply *psy)
227 struct device_node *np;
228 int cnt = 0;
230 /* If there is already a list honor it */
231 if (psy->supplied_from && psy->num_supplies > 0)
232 return 0;
234 /* No device node found, nothing to do */
235 if (!psy->of_node)
236 return 0;
238 do {
239 int ret;
241 np = of_parse_phandle(psy->of_node, "power-supplies", cnt++);
242 if (!np)
243 break;
245 ret = power_supply_find_supply_from_node(np);
246 of_node_put(np);
248 if (ret) {
249 dev_dbg(&psy->dev, "Failed to find supply!\n");
250 return ret;
252 } while (np);
254 /* Missing valid "power-supplies" entries */
255 if (cnt == 1)
256 return 0;
258 /* All supplies found, allocate char ** array for filling */
259 psy->supplied_from = devm_kzalloc(&psy->dev, sizeof(psy->supplied_from),
260 GFP_KERNEL);
261 if (!psy->supplied_from) {
262 dev_err(&psy->dev, "Couldn't allocate memory for supply list\n");
263 return -ENOMEM;
266 *psy->supplied_from = devm_kzalloc(&psy->dev,
267 sizeof(char *) * (cnt - 1),
268 GFP_KERNEL);
269 if (!*psy->supplied_from) {
270 dev_err(&psy->dev, "Couldn't allocate memory for supply list\n");
271 return -ENOMEM;
274 return power_supply_populate_supplied_from(psy);
276 #else
277 static inline int power_supply_check_supplies(struct power_supply *psy)
279 return 0;
281 #endif
283 static int __power_supply_am_i_supplied(struct device *dev, void *data)
285 union power_supply_propval ret = {0,};
286 struct power_supply *psy = data;
287 struct power_supply *epsy = dev_get_drvdata(dev);
289 if (__power_supply_is_supplied_by(epsy, psy))
290 if (!epsy->desc->get_property(epsy, POWER_SUPPLY_PROP_ONLINE,
291 &ret))
292 return ret.intval;
294 return 0;
297 int power_supply_am_i_supplied(struct power_supply *psy)
299 int error;
301 error = class_for_each_device(power_supply_class, NULL, psy,
302 __power_supply_am_i_supplied);
304 dev_dbg(&psy->dev, "%s %d\n", __func__, error);
306 return error;
308 EXPORT_SYMBOL_GPL(power_supply_am_i_supplied);
310 static int __power_supply_is_system_supplied(struct device *dev, void *data)
312 union power_supply_propval ret = {0,};
313 struct power_supply *psy = dev_get_drvdata(dev);
314 unsigned int *count = data;
316 (*count)++;
317 if (psy->desc->type != POWER_SUPPLY_TYPE_BATTERY)
318 if (!psy->desc->get_property(psy, POWER_SUPPLY_PROP_ONLINE,
319 &ret))
320 return ret.intval;
322 return 0;
325 int power_supply_is_system_supplied(void)
327 int error;
328 unsigned int count = 0;
330 error = class_for_each_device(power_supply_class, NULL, &count,
331 __power_supply_is_system_supplied);
334 * If no power class device was found at all, most probably we are
335 * running on a desktop system, so assume we are on mains power.
337 if (count == 0)
338 return 1;
340 return error;
342 EXPORT_SYMBOL_GPL(power_supply_is_system_supplied);
344 int power_supply_set_battery_charged(struct power_supply *psy)
346 if (atomic_read(&psy->use_cnt) >= 0 &&
347 psy->desc->type == POWER_SUPPLY_TYPE_BATTERY &&
348 psy->desc->set_charged) {
349 psy->desc->set_charged(psy);
350 return 0;
353 return -EINVAL;
355 EXPORT_SYMBOL_GPL(power_supply_set_battery_charged);
357 static int power_supply_match_device_by_name(struct device *dev, const void *data)
359 const char *name = data;
360 struct power_supply *psy = dev_get_drvdata(dev);
362 return strcmp(psy->desc->name, name) == 0;
366 * power_supply_get_by_name() - Search for a power supply and returns its ref
367 * @name: Power supply name to fetch
369 * If power supply was found, it increases reference count for the
370 * internal power supply's device. The user should power_supply_put()
371 * after usage.
373 * Return: On success returns a reference to a power supply with
374 * matching name equals to @name, a NULL otherwise.
376 struct power_supply *power_supply_get_by_name(const char *name)
378 struct power_supply *psy = NULL;
379 struct device *dev = class_find_device(power_supply_class, NULL, name,
380 power_supply_match_device_by_name);
382 if (dev) {
383 psy = dev_get_drvdata(dev);
384 atomic_inc(&psy->use_cnt);
387 return psy;
389 EXPORT_SYMBOL_GPL(power_supply_get_by_name);
392 * power_supply_put() - Drop reference obtained with power_supply_get_by_name
393 * @psy: Reference to put
395 * The reference to power supply should be put before unregistering
396 * the power supply.
398 void power_supply_put(struct power_supply *psy)
400 might_sleep();
402 atomic_dec(&psy->use_cnt);
403 put_device(&psy->dev);
405 EXPORT_SYMBOL_GPL(power_supply_put);
407 #ifdef CONFIG_OF
408 static int power_supply_match_device_node(struct device *dev, const void *data)
410 return dev->parent && dev->parent->of_node == data;
414 * power_supply_get_by_phandle() - Search for a power supply and returns its ref
415 * @np: Pointer to device node holding phandle property
416 * @phandle_name: Name of property holding a power supply name
418 * If power supply was found, it increases reference count for the
419 * internal power supply's device. The user should power_supply_put()
420 * after usage.
422 * Return: On success returns a reference to a power supply with
423 * matching name equals to value under @property, NULL or ERR_PTR otherwise.
425 struct power_supply *power_supply_get_by_phandle(struct device_node *np,
426 const char *property)
428 struct device_node *power_supply_np;
429 struct power_supply *psy = NULL;
430 struct device *dev;
432 power_supply_np = of_parse_phandle(np, property, 0);
433 if (!power_supply_np)
434 return ERR_PTR(-ENODEV);
436 dev = class_find_device(power_supply_class, NULL, power_supply_np,
437 power_supply_match_device_node);
439 of_node_put(power_supply_np);
441 if (dev) {
442 psy = dev_get_drvdata(dev);
443 atomic_inc(&psy->use_cnt);
446 return psy;
448 EXPORT_SYMBOL_GPL(power_supply_get_by_phandle);
450 static void devm_power_supply_put(struct device *dev, void *res)
452 struct power_supply **psy = res;
454 power_supply_put(*psy);
458 * devm_power_supply_get_by_phandle() - Resource managed version of
459 * power_supply_get_by_phandle()
460 * @dev: Pointer to device holding phandle property
461 * @phandle_name: Name of property holding a power supply phandle
463 * Return: On success returns a reference to a power supply with
464 * matching name equals to value under @property, NULL or ERR_PTR otherwise.
466 struct power_supply *devm_power_supply_get_by_phandle(struct device *dev,
467 const char *property)
469 struct power_supply **ptr, *psy;
471 if (!dev->of_node)
472 return ERR_PTR(-ENODEV);
474 ptr = devres_alloc(devm_power_supply_put, sizeof(*ptr), GFP_KERNEL);
475 if (!ptr)
476 return ERR_PTR(-ENOMEM);
478 psy = power_supply_get_by_phandle(dev->of_node, property);
479 if (IS_ERR_OR_NULL(psy)) {
480 devres_free(ptr);
481 } else {
482 *ptr = psy;
483 devres_add(dev, ptr);
485 return psy;
487 EXPORT_SYMBOL_GPL(devm_power_supply_get_by_phandle);
488 #endif /* CONFIG_OF */
490 int power_supply_get_property(struct power_supply *psy,
491 enum power_supply_property psp,
492 union power_supply_propval *val)
494 if (atomic_read(&psy->use_cnt) <= 0)
495 return -ENODEV;
497 return psy->desc->get_property(psy, psp, val);
499 EXPORT_SYMBOL_GPL(power_supply_get_property);
501 int power_supply_set_property(struct power_supply *psy,
502 enum power_supply_property psp,
503 const union power_supply_propval *val)
505 if (atomic_read(&psy->use_cnt) <= 0 || !psy->desc->set_property)
506 return -ENODEV;
508 return psy->desc->set_property(psy, psp, val);
510 EXPORT_SYMBOL_GPL(power_supply_set_property);
512 int power_supply_property_is_writeable(struct power_supply *psy,
513 enum power_supply_property psp)
515 if (atomic_read(&psy->use_cnt) <= 0 ||
516 !psy->desc->property_is_writeable)
517 return -ENODEV;
519 return psy->desc->property_is_writeable(psy, psp);
521 EXPORT_SYMBOL_GPL(power_supply_property_is_writeable);
523 void power_supply_external_power_changed(struct power_supply *psy)
525 if (atomic_read(&psy->use_cnt) <= 0 ||
526 !psy->desc->external_power_changed)
527 return;
529 psy->desc->external_power_changed(psy);
531 EXPORT_SYMBOL_GPL(power_supply_external_power_changed);
533 int power_supply_powers(struct power_supply *psy, struct device *dev)
535 return sysfs_create_link(&psy->dev.kobj, &dev->kobj, "powers");
537 EXPORT_SYMBOL_GPL(power_supply_powers);
539 static void power_supply_dev_release(struct device *dev)
541 struct power_supply *psy = container_of(dev, struct power_supply, dev);
542 pr_debug("device: '%s': %s\n", dev_name(dev), __func__);
543 kfree(psy);
546 int power_supply_reg_notifier(struct notifier_block *nb)
548 return atomic_notifier_chain_register(&power_supply_notifier, nb);
550 EXPORT_SYMBOL_GPL(power_supply_reg_notifier);
552 void power_supply_unreg_notifier(struct notifier_block *nb)
554 atomic_notifier_chain_unregister(&power_supply_notifier, nb);
556 EXPORT_SYMBOL_GPL(power_supply_unreg_notifier);
558 #ifdef CONFIG_THERMAL
559 static int power_supply_read_temp(struct thermal_zone_device *tzd,
560 int *temp)
562 struct power_supply *psy;
563 union power_supply_propval val;
564 int ret;
566 WARN_ON(tzd == NULL);
567 psy = tzd->devdata;
568 ret = power_supply_get_property(psy, POWER_SUPPLY_PROP_TEMP, &val);
569 if (ret)
570 return ret;
572 /* Convert tenths of degree Celsius to milli degree Celsius. */
573 *temp = val.intval * 100;
575 return ret;
578 static struct thermal_zone_device_ops psy_tzd_ops = {
579 .get_temp = power_supply_read_temp,
582 static int psy_register_thermal(struct power_supply *psy)
584 int i;
586 if (psy->desc->no_thermal)
587 return 0;
589 /* Register battery zone device psy reports temperature */
590 for (i = 0; i < psy->desc->num_properties; i++) {
591 if (psy->desc->properties[i] == POWER_SUPPLY_PROP_TEMP) {
592 psy->tzd = thermal_zone_device_register(psy->desc->name,
593 0, 0, psy, &psy_tzd_ops, NULL, 0, 0);
594 return PTR_ERR_OR_ZERO(psy->tzd);
597 return 0;
600 static void psy_unregister_thermal(struct power_supply *psy)
602 if (IS_ERR_OR_NULL(psy->tzd))
603 return;
604 thermal_zone_device_unregister(psy->tzd);
607 /* thermal cooling device callbacks */
608 static int ps_get_max_charge_cntl_limit(struct thermal_cooling_device *tcd,
609 unsigned long *state)
611 struct power_supply *psy;
612 union power_supply_propval val;
613 int ret;
615 psy = tcd->devdata;
616 ret = power_supply_get_property(psy,
617 POWER_SUPPLY_PROP_CHARGE_CONTROL_LIMIT_MAX, &val);
618 if (ret)
619 return ret;
621 *state = val.intval;
623 return ret;
626 static int ps_get_cur_chrage_cntl_limit(struct thermal_cooling_device *tcd,
627 unsigned long *state)
629 struct power_supply *psy;
630 union power_supply_propval val;
631 int ret;
633 psy = tcd->devdata;
634 ret = power_supply_get_property(psy,
635 POWER_SUPPLY_PROP_CHARGE_CONTROL_LIMIT, &val);
636 if (ret)
637 return ret;
639 *state = val.intval;
641 return ret;
644 static int ps_set_cur_charge_cntl_limit(struct thermal_cooling_device *tcd,
645 unsigned long state)
647 struct power_supply *psy;
648 union power_supply_propval val;
649 int ret;
651 psy = tcd->devdata;
652 val.intval = state;
653 ret = psy->desc->set_property(psy,
654 POWER_SUPPLY_PROP_CHARGE_CONTROL_LIMIT, &val);
656 return ret;
659 static struct thermal_cooling_device_ops psy_tcd_ops = {
660 .get_max_state = ps_get_max_charge_cntl_limit,
661 .get_cur_state = ps_get_cur_chrage_cntl_limit,
662 .set_cur_state = ps_set_cur_charge_cntl_limit,
665 static int psy_register_cooler(struct power_supply *psy)
667 int i;
669 /* Register for cooling device if psy can control charging */
670 for (i = 0; i < psy->desc->num_properties; i++) {
671 if (psy->desc->properties[i] ==
672 POWER_SUPPLY_PROP_CHARGE_CONTROL_LIMIT) {
673 psy->tcd = thermal_cooling_device_register(
674 (char *)psy->desc->name,
675 psy, &psy_tcd_ops);
676 return PTR_ERR_OR_ZERO(psy->tcd);
679 return 0;
682 static void psy_unregister_cooler(struct power_supply *psy)
684 if (IS_ERR_OR_NULL(psy->tcd))
685 return;
686 thermal_cooling_device_unregister(psy->tcd);
688 #else
689 static int psy_register_thermal(struct power_supply *psy)
691 return 0;
694 static void psy_unregister_thermal(struct power_supply *psy)
698 static int psy_register_cooler(struct power_supply *psy)
700 return 0;
703 static void psy_unregister_cooler(struct power_supply *psy)
706 #endif
708 static struct power_supply *__must_check
709 __power_supply_register(struct device *parent,
710 const struct power_supply_desc *desc,
711 const struct power_supply_config *cfg,
712 bool ws)
714 struct device *dev;
715 struct power_supply *psy;
716 int rc;
718 if (!parent)
719 pr_warn("%s: Expected proper parent device for '%s'\n",
720 __func__, desc->name);
722 psy = kzalloc(sizeof(*psy), GFP_KERNEL);
723 if (!psy)
724 return ERR_PTR(-ENOMEM);
726 dev = &psy->dev;
728 device_initialize(dev);
730 dev->class = power_supply_class;
731 dev->type = &power_supply_dev_type;
732 dev->parent = parent;
733 dev->release = power_supply_dev_release;
734 dev_set_drvdata(dev, psy);
735 psy->desc = desc;
736 if (cfg) {
737 psy->drv_data = cfg->drv_data;
738 psy->of_node = cfg->of_node;
739 psy->supplied_to = cfg->supplied_to;
740 psy->num_supplicants = cfg->num_supplicants;
743 rc = dev_set_name(dev, "%s", desc->name);
744 if (rc)
745 goto dev_set_name_failed;
747 INIT_WORK(&psy->changed_work, power_supply_changed_work);
748 INIT_DELAYED_WORK(&psy->deferred_register_work,
749 power_supply_deferred_register_work);
751 rc = power_supply_check_supplies(psy);
752 if (rc) {
753 dev_info(dev, "Not all required supplies found, defer probe\n");
754 goto check_supplies_failed;
757 spin_lock_init(&psy->changed_lock);
758 rc = device_add(dev);
759 if (rc)
760 goto device_add_failed;
762 rc = device_init_wakeup(dev, ws);
763 if (rc)
764 goto wakeup_init_failed;
766 rc = psy_register_thermal(psy);
767 if (rc)
768 goto register_thermal_failed;
770 rc = psy_register_cooler(psy);
771 if (rc)
772 goto register_cooler_failed;
774 rc = power_supply_create_triggers(psy);
775 if (rc)
776 goto create_triggers_failed;
779 * Update use_cnt after any uevents (most notably from device_add()).
780 * We are here still during driver's probe but
781 * the power_supply_uevent() calls back driver's get_property
782 * method so:
783 * 1. Driver did not assigned the returned struct power_supply,
784 * 2. Driver could not finish initialization (anything in its probe
785 * after calling power_supply_register()).
787 atomic_inc(&psy->use_cnt);
789 queue_delayed_work(system_power_efficient_wq,
790 &psy->deferred_register_work,
791 POWER_SUPPLY_DEFERRED_REGISTER_TIME);
793 return psy;
795 create_triggers_failed:
796 psy_unregister_cooler(psy);
797 register_cooler_failed:
798 psy_unregister_thermal(psy);
799 register_thermal_failed:
800 device_del(dev);
801 wakeup_init_failed:
802 device_add_failed:
803 check_supplies_failed:
804 dev_set_name_failed:
805 put_device(dev);
806 return ERR_PTR(rc);
810 * power_supply_register() - Register new power supply
811 * @parent: Device to be a parent of power supply's device, usually
812 * the device which probe function calls this
813 * @desc: Description of power supply, must be valid through whole
814 * lifetime of this power supply
815 * @cfg: Run-time specific configuration accessed during registering,
816 * may be NULL
818 * Return: A pointer to newly allocated power_supply on success
819 * or ERR_PTR otherwise.
820 * Use power_supply_unregister() on returned power_supply pointer to release
821 * resources.
823 struct power_supply *__must_check power_supply_register(struct device *parent,
824 const struct power_supply_desc *desc,
825 const struct power_supply_config *cfg)
827 return __power_supply_register(parent, desc, cfg, true);
829 EXPORT_SYMBOL_GPL(power_supply_register);
832 * power_supply_register_no_ws() - Register new non-waking-source power supply
833 * @parent: Device to be a parent of power supply's device, usually
834 * the device which probe function calls this
835 * @desc: Description of power supply, must be valid through whole
836 * lifetime of this power supply
837 * @cfg: Run-time specific configuration accessed during registering,
838 * may be NULL
840 * Return: A pointer to newly allocated power_supply on success
841 * or ERR_PTR otherwise.
842 * Use power_supply_unregister() on returned power_supply pointer to release
843 * resources.
845 struct power_supply *__must_check
846 power_supply_register_no_ws(struct device *parent,
847 const struct power_supply_desc *desc,
848 const struct power_supply_config *cfg)
850 return __power_supply_register(parent, desc, cfg, false);
852 EXPORT_SYMBOL_GPL(power_supply_register_no_ws);
854 static void devm_power_supply_release(struct device *dev, void *res)
856 struct power_supply **psy = res;
858 power_supply_unregister(*psy);
862 * devm_power_supply_register() - Register managed power supply
863 * @parent: Device to be a parent of power supply's device, usually
864 * the device which probe function calls this
865 * @desc: Description of power supply, must be valid through whole
866 * lifetime of this power supply
867 * @cfg: Run-time specific configuration accessed during registering,
868 * may be NULL
870 * Return: A pointer to newly allocated power_supply on success
871 * or ERR_PTR otherwise.
872 * The returned power_supply pointer will be automatically unregistered
873 * on driver detach.
875 struct power_supply *__must_check
876 devm_power_supply_register(struct device *parent,
877 const struct power_supply_desc *desc,
878 const struct power_supply_config *cfg)
880 struct power_supply **ptr, *psy;
882 ptr = devres_alloc(devm_power_supply_release, sizeof(*ptr), GFP_KERNEL);
884 if (!ptr)
885 return ERR_PTR(-ENOMEM);
886 psy = __power_supply_register(parent, desc, cfg, true);
887 if (IS_ERR(psy)) {
888 devres_free(ptr);
889 } else {
890 *ptr = psy;
891 devres_add(parent, ptr);
893 return psy;
895 EXPORT_SYMBOL_GPL(devm_power_supply_register);
898 * devm_power_supply_register_no_ws() - Register managed non-waking-source power supply
899 * @parent: Device to be a parent of power supply's device, usually
900 * the device which probe function calls this
901 * @desc: Description of power supply, must be valid through whole
902 * lifetime of this power supply
903 * @cfg: Run-time specific configuration accessed during registering,
904 * may be NULL
906 * Return: A pointer to newly allocated power_supply on success
907 * or ERR_PTR otherwise.
908 * The returned power_supply pointer will be automatically unregistered
909 * on driver detach.
911 struct power_supply *__must_check
912 devm_power_supply_register_no_ws(struct device *parent,
913 const struct power_supply_desc *desc,
914 const struct power_supply_config *cfg)
916 struct power_supply **ptr, *psy;
918 ptr = devres_alloc(devm_power_supply_release, sizeof(*ptr), GFP_KERNEL);
920 if (!ptr)
921 return ERR_PTR(-ENOMEM);
922 psy = __power_supply_register(parent, desc, cfg, false);
923 if (IS_ERR(psy)) {
924 devres_free(ptr);
925 } else {
926 *ptr = psy;
927 devres_add(parent, ptr);
929 return psy;
931 EXPORT_SYMBOL_GPL(devm_power_supply_register_no_ws);
934 * power_supply_unregister() - Remove this power supply from system
935 * @psy: Pointer to power supply to unregister
937 * Remove this power supply from the system. The resources of power supply
938 * will be freed here or on last power_supply_put() call.
940 void power_supply_unregister(struct power_supply *psy)
942 WARN_ON(atomic_dec_return(&psy->use_cnt));
943 cancel_work_sync(&psy->changed_work);
944 cancel_delayed_work_sync(&psy->deferred_register_work);
945 sysfs_remove_link(&psy->dev.kobj, "powers");
946 power_supply_remove_triggers(psy);
947 psy_unregister_cooler(psy);
948 psy_unregister_thermal(psy);
949 device_init_wakeup(&psy->dev, false);
950 device_unregister(&psy->dev);
952 EXPORT_SYMBOL_GPL(power_supply_unregister);
954 void *power_supply_get_drvdata(struct power_supply *psy)
956 return psy->drv_data;
958 EXPORT_SYMBOL_GPL(power_supply_get_drvdata);
960 static int __init power_supply_class_init(void)
962 power_supply_class = class_create(THIS_MODULE, "power_supply");
964 if (IS_ERR(power_supply_class))
965 return PTR_ERR(power_supply_class);
967 power_supply_class->dev_uevent = power_supply_uevent;
968 power_supply_init_attrs(&power_supply_dev_type);
970 return 0;
973 static void __exit power_supply_class_exit(void)
975 class_destroy(power_supply_class);
978 subsys_initcall(power_supply_class_init);
979 module_exit(power_supply_class_exit);
981 MODULE_DESCRIPTION("Universal power supply monitor class");
982 MODULE_AUTHOR("Ian Molton <spyro@f2s.com>, "
983 "Szabolcs Gyurko, "
984 "Anton Vorontsov <cbou@mail.ru>");
985 MODULE_LICENSE("GPL");