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
)
265 *psy
->supplied_from
= devm_kzalloc(&psy
->dev
,
266 sizeof(char *) * (cnt
- 1),
268 if (!*psy
->supplied_from
)
271 return power_supply_populate_supplied_from(psy
);
274 static int power_supply_check_supplies(struct power_supply
*psy
)
278 if (!psy
->dev
.parent
)
281 nval
= device_property_read_string_array(psy
->dev
.parent
,
282 "supplied-from", NULL
, 0);
286 psy
->supplied_from
= devm_kmalloc_array(&psy
->dev
, nval
,
287 sizeof(char *), GFP_KERNEL
);
288 if (!psy
->supplied_from
)
291 ret
= device_property_read_string_array(psy
->dev
.parent
,
292 "supplied-from", (const char **)psy
->supplied_from
, nval
);
296 psy
->num_supplies
= nval
;
302 struct psy_am_i_supplied_data
{
303 struct power_supply
*psy
;
307 static int __power_supply_am_i_supplied(struct device
*dev
, void *_data
)
309 union power_supply_propval ret
= {0,};
310 struct power_supply
*epsy
= dev_get_drvdata(dev
);
311 struct psy_am_i_supplied_data
*data
= _data
;
313 if (__power_supply_is_supplied_by(epsy
, data
->psy
)) {
315 if (!epsy
->desc
->get_property(epsy
, POWER_SUPPLY_PROP_ONLINE
,
323 int power_supply_am_i_supplied(struct power_supply
*psy
)
325 struct psy_am_i_supplied_data data
= { psy
, 0 };
328 error
= class_for_each_device(power_supply_class
, NULL
, &data
,
329 __power_supply_am_i_supplied
);
331 dev_dbg(&psy
->dev
, "%s count %u err %d\n", __func__
, data
.count
, error
);
338 EXPORT_SYMBOL_GPL(power_supply_am_i_supplied
);
340 static int __power_supply_is_system_supplied(struct device
*dev
, void *data
)
342 union power_supply_propval ret
= {0,};
343 struct power_supply
*psy
= dev_get_drvdata(dev
);
344 unsigned int *count
= data
;
347 if (psy
->desc
->type
!= POWER_SUPPLY_TYPE_BATTERY
)
348 if (!psy
->desc
->get_property(psy
, POWER_SUPPLY_PROP_ONLINE
,
355 int power_supply_is_system_supplied(void)
358 unsigned int count
= 0;
360 error
= class_for_each_device(power_supply_class
, NULL
, &count
,
361 __power_supply_is_system_supplied
);
364 * If no power class device was found at all, most probably we are
365 * running on a desktop system, so assume we are on mains power.
372 EXPORT_SYMBOL_GPL(power_supply_is_system_supplied
);
374 static int __power_supply_get_supplier_max_current(struct device
*dev
,
377 union power_supply_propval ret
= {0,};
378 struct power_supply
*epsy
= dev_get_drvdata(dev
);
379 struct power_supply
*psy
= data
;
381 if (__power_supply_is_supplied_by(epsy
, psy
))
382 if (!epsy
->desc
->get_property(epsy
,
383 POWER_SUPPLY_PROP_CURRENT_MAX
,
390 int power_supply_set_input_current_limit_from_supplier(struct power_supply
*psy
)
392 union power_supply_propval val
= {0,};
395 if (!psy
->desc
->set_property
)
399 * This function is not intended for use with a supply with multiple
400 * suppliers, we simply pick the first supply to report a non 0
403 curr
= class_for_each_device(power_supply_class
, NULL
, psy
,
404 __power_supply_get_supplier_max_current
);
406 return (curr
== 0) ? -ENODEV
: curr
;
410 return psy
->desc
->set_property(psy
,
411 POWER_SUPPLY_PROP_INPUT_CURRENT_LIMIT
, &val
);
413 EXPORT_SYMBOL_GPL(power_supply_set_input_current_limit_from_supplier
);
415 int power_supply_set_battery_charged(struct power_supply
*psy
)
417 if (atomic_read(&psy
->use_cnt
) >= 0 &&
418 psy
->desc
->type
== POWER_SUPPLY_TYPE_BATTERY
&&
419 psy
->desc
->set_charged
) {
420 psy
->desc
->set_charged(psy
);
426 EXPORT_SYMBOL_GPL(power_supply_set_battery_charged
);
428 static int power_supply_match_device_by_name(struct device
*dev
, const void *data
)
430 const char *name
= data
;
431 struct power_supply
*psy
= dev_get_drvdata(dev
);
433 return strcmp(psy
->desc
->name
, name
) == 0;
437 * power_supply_get_by_name() - Search for a power supply and returns its ref
438 * @name: Power supply name to fetch
440 * If power supply was found, it increases reference count for the
441 * internal power supply's device. The user should power_supply_put()
444 * Return: On success returns a reference to a power supply with
445 * matching name equals to @name, a NULL otherwise.
447 struct power_supply
*power_supply_get_by_name(const char *name
)
449 struct power_supply
*psy
= NULL
;
450 struct device
*dev
= class_find_device(power_supply_class
, NULL
, name
,
451 power_supply_match_device_by_name
);
454 psy
= dev_get_drvdata(dev
);
455 atomic_inc(&psy
->use_cnt
);
460 EXPORT_SYMBOL_GPL(power_supply_get_by_name
);
463 * power_supply_put() - Drop reference obtained with power_supply_get_by_name
464 * @psy: Reference to put
466 * The reference to power supply should be put before unregistering
469 void power_supply_put(struct power_supply
*psy
)
473 atomic_dec(&psy
->use_cnt
);
474 put_device(&psy
->dev
);
476 EXPORT_SYMBOL_GPL(power_supply_put
);
479 static int power_supply_match_device_node(struct device
*dev
, const void *data
)
481 return dev
->parent
&& dev
->parent
->of_node
== data
;
485 * power_supply_get_by_phandle() - Search for a power supply and returns its ref
486 * @np: Pointer to device node holding phandle property
487 * @property: Name of property holding a power supply name
489 * If power supply was found, it increases reference count for the
490 * internal power supply's device. The user should power_supply_put()
493 * Return: On success returns a reference to a power supply with
494 * matching name equals to value under @property, NULL or ERR_PTR otherwise.
496 struct power_supply
*power_supply_get_by_phandle(struct device_node
*np
,
497 const char *property
)
499 struct device_node
*power_supply_np
;
500 struct power_supply
*psy
= NULL
;
503 power_supply_np
= of_parse_phandle(np
, property
, 0);
504 if (!power_supply_np
)
505 return ERR_PTR(-ENODEV
);
507 dev
= class_find_device(power_supply_class
, NULL
, power_supply_np
,
508 power_supply_match_device_node
);
510 of_node_put(power_supply_np
);
513 psy
= dev_get_drvdata(dev
);
514 atomic_inc(&psy
->use_cnt
);
519 EXPORT_SYMBOL_GPL(power_supply_get_by_phandle
);
521 static void devm_power_supply_put(struct device
*dev
, void *res
)
523 struct power_supply
**psy
= res
;
525 power_supply_put(*psy
);
529 * devm_power_supply_get_by_phandle() - Resource managed version of
530 * power_supply_get_by_phandle()
531 * @dev: Pointer to device holding phandle property
532 * @property: Name of property holding a power supply phandle
534 * Return: On success returns a reference to a power supply with
535 * matching name equals to value under @property, NULL or ERR_PTR otherwise.
537 struct power_supply
*devm_power_supply_get_by_phandle(struct device
*dev
,
538 const char *property
)
540 struct power_supply
**ptr
, *psy
;
543 return ERR_PTR(-ENODEV
);
545 ptr
= devres_alloc(devm_power_supply_put
, sizeof(*ptr
), GFP_KERNEL
);
547 return ERR_PTR(-ENOMEM
);
549 psy
= power_supply_get_by_phandle(dev
->of_node
, property
);
550 if (IS_ERR_OR_NULL(psy
)) {
554 devres_add(dev
, ptr
);
558 EXPORT_SYMBOL_GPL(devm_power_supply_get_by_phandle
);
559 #endif /* CONFIG_OF */
561 int power_supply_get_battery_info(struct power_supply
*psy
,
562 struct power_supply_battery_info
*info
)
564 struct device_node
*battery_np
;
568 info
->energy_full_design_uwh
= -EINVAL
;
569 info
->charge_full_design_uah
= -EINVAL
;
570 info
->voltage_min_design_uv
= -EINVAL
;
571 info
->precharge_current_ua
= -EINVAL
;
572 info
->charge_term_current_ua
= -EINVAL
;
573 info
->constant_charge_current_max_ua
= -EINVAL
;
574 info
->constant_charge_voltage_max_uv
= -EINVAL
;
577 dev_warn(&psy
->dev
, "%s currently only supports devicetree\n",
582 battery_np
= of_parse_phandle(psy
->of_node
, "monitored-battery", 0);
586 err
= of_property_read_string(battery_np
, "compatible", &value
);
590 if (strcmp("simple-battery", value
))
593 /* The property and field names below must correspond to elements
594 * in enum power_supply_property. For reasoning, see
595 * Documentation/power/power_supply_class.txt.
598 of_property_read_u32(battery_np
, "energy-full-design-microwatt-hours",
599 &info
->energy_full_design_uwh
);
600 of_property_read_u32(battery_np
, "charge-full-design-microamp-hours",
601 &info
->charge_full_design_uah
);
602 of_property_read_u32(battery_np
, "voltage-min-design-microvolt",
603 &info
->voltage_min_design_uv
);
604 of_property_read_u32(battery_np
, "precharge-current-microamp",
605 &info
->precharge_current_ua
);
606 of_property_read_u32(battery_np
, "charge-term-current-microamp",
607 &info
->charge_term_current_ua
);
608 of_property_read_u32(battery_np
, "constant_charge_current_max_microamp",
609 &info
->constant_charge_current_max_ua
);
610 of_property_read_u32(battery_np
, "constant_charge_voltage_max_microvolt",
611 &info
->constant_charge_voltage_max_uv
);
615 EXPORT_SYMBOL_GPL(power_supply_get_battery_info
);
617 int power_supply_get_property(struct power_supply
*psy
,
618 enum power_supply_property psp
,
619 union power_supply_propval
*val
)
621 if (atomic_read(&psy
->use_cnt
) <= 0) {
622 if (!psy
->initialized
)
627 return psy
->desc
->get_property(psy
, psp
, val
);
629 EXPORT_SYMBOL_GPL(power_supply_get_property
);
631 int power_supply_set_property(struct power_supply
*psy
,
632 enum power_supply_property psp
,
633 const union power_supply_propval
*val
)
635 if (atomic_read(&psy
->use_cnt
) <= 0 || !psy
->desc
->set_property
)
638 return psy
->desc
->set_property(psy
, psp
, val
);
640 EXPORT_SYMBOL_GPL(power_supply_set_property
);
642 int power_supply_property_is_writeable(struct power_supply
*psy
,
643 enum power_supply_property psp
)
645 if (atomic_read(&psy
->use_cnt
) <= 0 ||
646 !psy
->desc
->property_is_writeable
)
649 return psy
->desc
->property_is_writeable(psy
, psp
);
651 EXPORT_SYMBOL_GPL(power_supply_property_is_writeable
);
653 void power_supply_external_power_changed(struct power_supply
*psy
)
655 if (atomic_read(&psy
->use_cnt
) <= 0 ||
656 !psy
->desc
->external_power_changed
)
659 psy
->desc
->external_power_changed(psy
);
661 EXPORT_SYMBOL_GPL(power_supply_external_power_changed
);
663 int power_supply_powers(struct power_supply
*psy
, struct device
*dev
)
665 return sysfs_create_link(&psy
->dev
.kobj
, &dev
->kobj
, "powers");
667 EXPORT_SYMBOL_GPL(power_supply_powers
);
669 static void power_supply_dev_release(struct device
*dev
)
671 struct power_supply
*psy
= container_of(dev
, struct power_supply
, dev
);
672 dev_dbg(dev
, "%s\n", __func__
);
676 int power_supply_reg_notifier(struct notifier_block
*nb
)
678 return atomic_notifier_chain_register(&power_supply_notifier
, nb
);
680 EXPORT_SYMBOL_GPL(power_supply_reg_notifier
);
682 void power_supply_unreg_notifier(struct notifier_block
*nb
)
684 atomic_notifier_chain_unregister(&power_supply_notifier
, nb
);
686 EXPORT_SYMBOL_GPL(power_supply_unreg_notifier
);
688 #ifdef CONFIG_THERMAL
689 static int power_supply_read_temp(struct thermal_zone_device
*tzd
,
692 struct power_supply
*psy
;
693 union power_supply_propval val
;
696 WARN_ON(tzd
== NULL
);
698 ret
= power_supply_get_property(psy
, POWER_SUPPLY_PROP_TEMP
, &val
);
702 /* Convert tenths of degree Celsius to milli degree Celsius. */
703 *temp
= val
.intval
* 100;
708 static struct thermal_zone_device_ops psy_tzd_ops
= {
709 .get_temp
= power_supply_read_temp
,
712 static int psy_register_thermal(struct power_supply
*psy
)
716 if (psy
->desc
->no_thermal
)
719 /* Register battery zone device psy reports temperature */
720 for (i
= 0; i
< psy
->desc
->num_properties
; i
++) {
721 if (psy
->desc
->properties
[i
] == POWER_SUPPLY_PROP_TEMP
) {
722 psy
->tzd
= thermal_zone_device_register(psy
->desc
->name
,
723 0, 0, psy
, &psy_tzd_ops
, NULL
, 0, 0);
724 return PTR_ERR_OR_ZERO(psy
->tzd
);
730 static void psy_unregister_thermal(struct power_supply
*psy
)
732 if (IS_ERR_OR_NULL(psy
->tzd
))
734 thermal_zone_device_unregister(psy
->tzd
);
737 /* thermal cooling device callbacks */
738 static int ps_get_max_charge_cntl_limit(struct thermal_cooling_device
*tcd
,
739 unsigned long *state
)
741 struct power_supply
*psy
;
742 union power_supply_propval val
;
746 ret
= power_supply_get_property(psy
,
747 POWER_SUPPLY_PROP_CHARGE_CONTROL_LIMIT_MAX
, &val
);
756 static int ps_get_cur_chrage_cntl_limit(struct thermal_cooling_device
*tcd
,
757 unsigned long *state
)
759 struct power_supply
*psy
;
760 union power_supply_propval val
;
764 ret
= power_supply_get_property(psy
,
765 POWER_SUPPLY_PROP_CHARGE_CONTROL_LIMIT
, &val
);
774 static int ps_set_cur_charge_cntl_limit(struct thermal_cooling_device
*tcd
,
777 struct power_supply
*psy
;
778 union power_supply_propval val
;
783 ret
= psy
->desc
->set_property(psy
,
784 POWER_SUPPLY_PROP_CHARGE_CONTROL_LIMIT
, &val
);
789 static const struct thermal_cooling_device_ops psy_tcd_ops
= {
790 .get_max_state
= ps_get_max_charge_cntl_limit
,
791 .get_cur_state
= ps_get_cur_chrage_cntl_limit
,
792 .set_cur_state
= ps_set_cur_charge_cntl_limit
,
795 static int psy_register_cooler(struct power_supply
*psy
)
799 /* Register for cooling device if psy can control charging */
800 for (i
= 0; i
< psy
->desc
->num_properties
; i
++) {
801 if (psy
->desc
->properties
[i
] ==
802 POWER_SUPPLY_PROP_CHARGE_CONTROL_LIMIT
) {
803 psy
->tcd
= thermal_cooling_device_register(
804 (char *)psy
->desc
->name
,
806 return PTR_ERR_OR_ZERO(psy
->tcd
);
812 static void psy_unregister_cooler(struct power_supply
*psy
)
814 if (IS_ERR_OR_NULL(psy
->tcd
))
816 thermal_cooling_device_unregister(psy
->tcd
);
819 static int psy_register_thermal(struct power_supply
*psy
)
824 static void psy_unregister_thermal(struct power_supply
*psy
)
828 static int psy_register_cooler(struct power_supply
*psy
)
833 static void psy_unregister_cooler(struct power_supply
*psy
)
838 static struct power_supply
*__must_check
839 __power_supply_register(struct device
*parent
,
840 const struct power_supply_desc
*desc
,
841 const struct power_supply_config
*cfg
,
845 struct power_supply
*psy
;
849 pr_warn("%s: Expected proper parent device for '%s'\n",
850 __func__
, desc
->name
);
852 psy
= kzalloc(sizeof(*psy
), GFP_KERNEL
);
854 return ERR_PTR(-ENOMEM
);
858 device_initialize(dev
);
860 dev
->class = power_supply_class
;
861 dev
->type
= &power_supply_dev_type
;
862 dev
->parent
= parent
;
863 dev
->release
= power_supply_dev_release
;
864 dev_set_drvdata(dev
, psy
);
867 psy
->drv_data
= cfg
->drv_data
;
868 psy
->of_node
= cfg
->of_node
;
869 psy
->supplied_to
= cfg
->supplied_to
;
870 psy
->num_supplicants
= cfg
->num_supplicants
;
873 rc
= dev_set_name(dev
, "%s", desc
->name
);
875 goto dev_set_name_failed
;
877 INIT_WORK(&psy
->changed_work
, power_supply_changed_work
);
878 INIT_DELAYED_WORK(&psy
->deferred_register_work
,
879 power_supply_deferred_register_work
);
881 rc
= power_supply_check_supplies(psy
);
883 dev_info(dev
, "Not all required supplies found, defer probe\n");
884 goto check_supplies_failed
;
887 spin_lock_init(&psy
->changed_lock
);
888 rc
= device_init_wakeup(dev
, ws
);
890 goto wakeup_init_failed
;
892 rc
= device_add(dev
);
894 goto device_add_failed
;
896 rc
= psy_register_thermal(psy
);
898 goto register_thermal_failed
;
900 rc
= psy_register_cooler(psy
);
902 goto register_cooler_failed
;
904 rc
= power_supply_create_triggers(psy
);
906 goto create_triggers_failed
;
909 * Update use_cnt after any uevents (most notably from device_add()).
910 * We are here still during driver's probe but
911 * the power_supply_uevent() calls back driver's get_property
913 * 1. Driver did not assigned the returned struct power_supply,
914 * 2. Driver could not finish initialization (anything in its probe
915 * after calling power_supply_register()).
917 atomic_inc(&psy
->use_cnt
);
918 psy
->initialized
= true;
920 queue_delayed_work(system_power_efficient_wq
,
921 &psy
->deferred_register_work
,
922 POWER_SUPPLY_DEFERRED_REGISTER_TIME
);
926 create_triggers_failed
:
927 psy_unregister_cooler(psy
);
928 register_cooler_failed
:
929 psy_unregister_thermal(psy
);
930 register_thermal_failed
:
934 check_supplies_failed
:
941 * power_supply_register() - Register new power supply
942 * @parent: Device to be a parent of power supply's device, usually
943 * the device which probe function calls this
944 * @desc: Description of power supply, must be valid through whole
945 * lifetime of this power supply
946 * @cfg: Run-time specific configuration accessed during registering,
949 * Return: A pointer to newly allocated power_supply on success
950 * or ERR_PTR otherwise.
951 * Use power_supply_unregister() on returned power_supply pointer to release
954 struct power_supply
*__must_check
power_supply_register(struct device
*parent
,
955 const struct power_supply_desc
*desc
,
956 const struct power_supply_config
*cfg
)
958 return __power_supply_register(parent
, desc
, cfg
, true);
960 EXPORT_SYMBOL_GPL(power_supply_register
);
963 * power_supply_register_no_ws() - Register new non-waking-source power supply
964 * @parent: Device to be a parent of power supply's device, usually
965 * the device which probe function calls this
966 * @desc: Description of power supply, must be valid through whole
967 * lifetime of this power supply
968 * @cfg: Run-time specific configuration accessed during registering,
971 * Return: A pointer to newly allocated power_supply on success
972 * or ERR_PTR otherwise.
973 * Use power_supply_unregister() on returned power_supply pointer to release
976 struct power_supply
*__must_check
977 power_supply_register_no_ws(struct device
*parent
,
978 const struct power_supply_desc
*desc
,
979 const struct power_supply_config
*cfg
)
981 return __power_supply_register(parent
, desc
, cfg
, false);
983 EXPORT_SYMBOL_GPL(power_supply_register_no_ws
);
985 static void devm_power_supply_release(struct device
*dev
, void *res
)
987 struct power_supply
**psy
= res
;
989 power_supply_unregister(*psy
);
993 * devm_power_supply_register() - Register managed power supply
994 * @parent: Device to be a parent of power supply's device, usually
995 * the device which probe function calls this
996 * @desc: Description of power supply, must be valid through whole
997 * lifetime of this power supply
998 * @cfg: Run-time specific configuration accessed during registering,
1001 * Return: A pointer to newly allocated power_supply on success
1002 * or ERR_PTR otherwise.
1003 * The returned power_supply pointer will be automatically unregistered
1006 struct power_supply
*__must_check
1007 devm_power_supply_register(struct device
*parent
,
1008 const struct power_supply_desc
*desc
,
1009 const struct power_supply_config
*cfg
)
1011 struct power_supply
**ptr
, *psy
;
1013 ptr
= devres_alloc(devm_power_supply_release
, sizeof(*ptr
), GFP_KERNEL
);
1016 return ERR_PTR(-ENOMEM
);
1017 psy
= __power_supply_register(parent
, desc
, cfg
, true);
1022 devres_add(parent
, ptr
);
1026 EXPORT_SYMBOL_GPL(devm_power_supply_register
);
1029 * devm_power_supply_register_no_ws() - Register managed non-waking-source power supply
1030 * @parent: Device to be a parent of power supply's device, usually
1031 * the device which probe function calls this
1032 * @desc: Description of power supply, must be valid through whole
1033 * lifetime of this power supply
1034 * @cfg: Run-time specific configuration accessed during registering,
1037 * Return: A pointer to newly allocated power_supply on success
1038 * or ERR_PTR otherwise.
1039 * The returned power_supply pointer will be automatically unregistered
1042 struct power_supply
*__must_check
1043 devm_power_supply_register_no_ws(struct device
*parent
,
1044 const struct power_supply_desc
*desc
,
1045 const struct power_supply_config
*cfg
)
1047 struct power_supply
**ptr
, *psy
;
1049 ptr
= devres_alloc(devm_power_supply_release
, sizeof(*ptr
), GFP_KERNEL
);
1052 return ERR_PTR(-ENOMEM
);
1053 psy
= __power_supply_register(parent
, desc
, cfg
, false);
1058 devres_add(parent
, ptr
);
1062 EXPORT_SYMBOL_GPL(devm_power_supply_register_no_ws
);
1065 * power_supply_unregister() - Remove this power supply from system
1066 * @psy: Pointer to power supply to unregister
1068 * Remove this power supply from the system. The resources of power supply
1069 * will be freed here or on last power_supply_put() call.
1071 void power_supply_unregister(struct power_supply
*psy
)
1073 WARN_ON(atomic_dec_return(&psy
->use_cnt
));
1074 cancel_work_sync(&psy
->changed_work
);
1075 cancel_delayed_work_sync(&psy
->deferred_register_work
);
1076 sysfs_remove_link(&psy
->dev
.kobj
, "powers");
1077 power_supply_remove_triggers(psy
);
1078 psy_unregister_cooler(psy
);
1079 psy_unregister_thermal(psy
);
1080 device_init_wakeup(&psy
->dev
, false);
1081 device_unregister(&psy
->dev
);
1083 EXPORT_SYMBOL_GPL(power_supply_unregister
);
1085 void *power_supply_get_drvdata(struct power_supply
*psy
)
1087 return psy
->drv_data
;
1089 EXPORT_SYMBOL_GPL(power_supply_get_drvdata
);
1091 static int __init
power_supply_class_init(void)
1093 power_supply_class
= class_create(THIS_MODULE
, "power_supply");
1095 if (IS_ERR(power_supply_class
))
1096 return PTR_ERR(power_supply_class
);
1098 power_supply_class
->dev_uevent
= power_supply_uevent
;
1099 power_supply_init_attrs(&power_supply_dev_type
);
1104 static void __exit
power_supply_class_exit(void)
1106 class_destroy(power_supply_class
);
1109 subsys_initcall(power_supply_class_init
);
1110 module_exit(power_supply_class_exit
);
1112 MODULE_DESCRIPTION("Universal power supply monitor class");
1113 MODULE_AUTHOR("Ian Molton <spyro@f2s.com>, "
1115 "Anton Vorontsov <cbou@mail.ru>");
1116 MODULE_LICENSE("GPL");