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 static bool __power_supply_is_supplied_by(struct power_supply
*supplier
,
34 struct power_supply
*supply
)
38 if (!supply
->supplied_from
&& !supplier
->supplied_to
)
41 /* Support both supplied_to and supplied_from modes */
42 if (supply
->supplied_from
) {
43 if (!supplier
->desc
->name
)
45 for (i
= 0; i
< supply
->num_supplies
; i
++)
46 if (!strcmp(supplier
->desc
->name
, supply
->supplied_from
[i
]))
49 if (!supply
->desc
->name
)
51 for (i
= 0; i
< supplier
->num_supplicants
; i
++)
52 if (!strcmp(supplier
->supplied_to
[i
], supply
->desc
->name
))
59 static int __power_supply_changed_work(struct device
*dev
, void *data
)
61 struct power_supply
*psy
= data
;
62 struct power_supply
*pst
= dev_get_drvdata(dev
);
64 if (__power_supply_is_supplied_by(psy
, pst
)) {
65 if (pst
->desc
->external_power_changed
)
66 pst
->desc
->external_power_changed(pst
);
72 static void power_supply_changed_work(struct work_struct
*work
)
75 struct power_supply
*psy
= container_of(work
, struct power_supply
,
78 dev_dbg(&psy
->dev
, "%s\n", __func__
);
80 spin_lock_irqsave(&psy
->changed_lock
, flags
);
82 * Check 'changed' here to avoid issues due to race between
83 * power_supply_changed() and this routine. In worst case
84 * power_supply_changed() can be called again just before we take above
85 * lock. During the first call of this routine we will mark 'changed' as
86 * false and it will stay false for the next call as well.
88 if (likely(psy
->changed
)) {
90 spin_unlock_irqrestore(&psy
->changed_lock
, flags
);
91 class_for_each_device(power_supply_class
, NULL
, psy
,
92 __power_supply_changed_work
);
93 power_supply_update_leds(psy
);
94 atomic_notifier_call_chain(&power_supply_notifier
,
95 PSY_EVENT_PROP_CHANGED
, psy
);
96 kobject_uevent(&psy
->dev
.kobj
, KOBJ_CHANGE
);
97 spin_lock_irqsave(&psy
->changed_lock
, flags
);
101 * Hold the wakeup_source until all events are processed.
102 * power_supply_changed() might have called again and have set 'changed'
105 if (likely(!psy
->changed
))
107 spin_unlock_irqrestore(&psy
->changed_lock
, flags
);
110 void power_supply_changed(struct power_supply
*psy
)
114 dev_dbg(&psy
->dev
, "%s\n", __func__
);
116 spin_lock_irqsave(&psy
->changed_lock
, flags
);
118 pm_stay_awake(&psy
->dev
);
119 spin_unlock_irqrestore(&psy
->changed_lock
, flags
);
120 schedule_work(&psy
->changed_work
);
122 EXPORT_SYMBOL_GPL(power_supply_changed
);
125 #include <linux/of.h>
127 static int __power_supply_populate_supplied_from(struct device
*dev
,
130 struct power_supply
*psy
= data
;
131 struct power_supply
*epsy
= dev_get_drvdata(dev
);
132 struct device_node
*np
;
136 np
= of_parse_phandle(psy
->of_node
, "power-supplies", i
++);
140 if (np
== epsy
->of_node
) {
141 dev_info(&psy
->dev
, "%s: Found supply : %s\n",
142 psy
->desc
->name
, epsy
->desc
->name
);
143 psy
->supplied_from
[i
-1] = (char *)epsy
->desc
->name
;
154 static int power_supply_populate_supplied_from(struct power_supply
*psy
)
158 error
= class_for_each_device(power_supply_class
, NULL
, psy
,
159 __power_supply_populate_supplied_from
);
161 dev_dbg(&psy
->dev
, "%s %d\n", __func__
, error
);
166 static int __power_supply_find_supply_from_node(struct device
*dev
,
169 struct device_node
*np
= data
;
170 struct power_supply
*epsy
= dev_get_drvdata(dev
);
172 /* returning non-zero breaks out of class_for_each_device loop */
173 if (epsy
->of_node
== np
)
179 static int power_supply_find_supply_from_node(struct device_node
*supply_node
)
184 * class_for_each_device() either returns its own errors or values
185 * returned by __power_supply_find_supply_from_node().
187 * __power_supply_find_supply_from_node() will return 0 (no match)
190 * We return 0 if class_for_each_device() returned 1, -EPROBE_DEFER if
191 * it returned 0, or error as returned by it.
193 error
= class_for_each_device(power_supply_class
, NULL
, supply_node
,
194 __power_supply_find_supply_from_node
);
196 return error
? (error
== 1 ? 0 : error
) : -EPROBE_DEFER
;
199 static int power_supply_check_supplies(struct power_supply
*psy
)
201 struct device_node
*np
;
204 /* If there is already a list honor it */
205 if (psy
->supplied_from
&& psy
->num_supplies
> 0)
208 /* No device node found, nothing to do */
215 np
= of_parse_phandle(psy
->of_node
, "power-supplies", cnt
++);
219 ret
= power_supply_find_supply_from_node(np
);
223 dev_dbg(&psy
->dev
, "Failed to find supply!\n");
228 /* Missing valid "power-supplies" entries */
232 /* All supplies found, allocate char ** array for filling */
233 psy
->supplied_from
= devm_kzalloc(&psy
->dev
, sizeof(psy
->supplied_from
),
235 if (!psy
->supplied_from
) {
236 dev_err(&psy
->dev
, "Couldn't allocate memory for supply list\n");
240 *psy
->supplied_from
= devm_kzalloc(&psy
->dev
,
241 sizeof(char *) * (cnt
- 1),
243 if (!*psy
->supplied_from
) {
244 dev_err(&psy
->dev
, "Couldn't allocate memory for supply list\n");
248 return power_supply_populate_supplied_from(psy
);
251 static inline int power_supply_check_supplies(struct power_supply
*psy
)
257 static int __power_supply_am_i_supplied(struct device
*dev
, void *data
)
259 union power_supply_propval ret
= {0,};
260 struct power_supply
*psy
= data
;
261 struct power_supply
*epsy
= dev_get_drvdata(dev
);
263 if (__power_supply_is_supplied_by(epsy
, psy
))
264 if (!epsy
->desc
->get_property(epsy
, POWER_SUPPLY_PROP_ONLINE
,
271 int power_supply_am_i_supplied(struct power_supply
*psy
)
275 error
= class_for_each_device(power_supply_class
, NULL
, psy
,
276 __power_supply_am_i_supplied
);
278 dev_dbg(&psy
->dev
, "%s %d\n", __func__
, error
);
282 EXPORT_SYMBOL_GPL(power_supply_am_i_supplied
);
284 static int __power_supply_is_system_supplied(struct device
*dev
, void *data
)
286 union power_supply_propval ret
= {0,};
287 struct power_supply
*psy
= dev_get_drvdata(dev
);
288 unsigned int *count
= data
;
291 if (psy
->desc
->type
!= POWER_SUPPLY_TYPE_BATTERY
)
292 if (!psy
->desc
->get_property(psy
, POWER_SUPPLY_PROP_ONLINE
,
299 int power_supply_is_system_supplied(void)
302 unsigned int count
= 0;
304 error
= class_for_each_device(power_supply_class
, NULL
, &count
,
305 __power_supply_is_system_supplied
);
308 * If no power class device was found at all, most probably we are
309 * running on a desktop system, so assume we are on mains power.
316 EXPORT_SYMBOL_GPL(power_supply_is_system_supplied
);
318 int power_supply_set_battery_charged(struct power_supply
*psy
)
320 if (atomic_read(&psy
->use_cnt
) >= 0 &&
321 psy
->desc
->type
== POWER_SUPPLY_TYPE_BATTERY
&&
322 psy
->desc
->set_charged
) {
323 psy
->desc
->set_charged(psy
);
329 EXPORT_SYMBOL_GPL(power_supply_set_battery_charged
);
331 static int power_supply_match_device_by_name(struct device
*dev
, const void *data
)
333 const char *name
= data
;
334 struct power_supply
*psy
= dev_get_drvdata(dev
);
336 return strcmp(psy
->desc
->name
, name
) == 0;
340 * power_supply_get_by_name() - Search for a power supply and returns its ref
341 * @name: Power supply name to fetch
343 * If power supply was found, it increases reference count for the
344 * internal power supply's device. The user should power_supply_put()
347 * Return: On success returns a reference to a power supply with
348 * matching name equals to @name, a NULL otherwise.
350 struct power_supply
*power_supply_get_by_name(const char *name
)
352 struct device
*dev
= class_find_device(power_supply_class
, NULL
, name
,
353 power_supply_match_device_by_name
);
355 return dev
? dev_get_drvdata(dev
) : NULL
;
357 EXPORT_SYMBOL_GPL(power_supply_get_by_name
);
360 * power_supply_put() - Drop reference obtained with power_supply_get_by_name
361 * @psy: Reference to put
363 * The reference to power supply should be put before unregistering
366 void power_supply_put(struct power_supply
*psy
)
370 put_device(&psy
->dev
);
372 EXPORT_SYMBOL_GPL(power_supply_put
);
375 static int power_supply_match_device_node(struct device
*dev
, const void *data
)
377 return dev
->parent
&& dev
->parent
->of_node
== data
;
381 * power_supply_get_by_phandle() - Search for a power supply and returns its ref
382 * @np: Pointer to device node holding phandle property
383 * @phandle_name: Name of property holding a power supply name
385 * If power supply was found, it increases reference count for the
386 * internal power supply's device. The user should power_supply_put()
389 * Return: On success returns a reference to a power supply with
390 * matching name equals to value under @property, NULL or ERR_PTR otherwise.
392 struct power_supply
*power_supply_get_by_phandle(struct device_node
*np
,
393 const char *property
)
395 struct device_node
*power_supply_np
;
398 power_supply_np
= of_parse_phandle(np
, property
, 0);
399 if (!power_supply_np
)
400 return ERR_PTR(-ENODEV
);
402 dev
= class_find_device(power_supply_class
, NULL
, power_supply_np
,
403 power_supply_match_device_node
);
405 of_node_put(power_supply_np
);
407 return dev
? dev_get_drvdata(dev
) : NULL
;
409 EXPORT_SYMBOL_GPL(power_supply_get_by_phandle
);
410 #endif /* CONFIG_OF */
412 int power_supply_get_property(struct power_supply
*psy
,
413 enum power_supply_property psp
,
414 union power_supply_propval
*val
)
416 if (atomic_read(&psy
->use_cnt
) <= 0)
419 return psy
->desc
->get_property(psy
, psp
, val
);
421 EXPORT_SYMBOL_GPL(power_supply_get_property
);
423 int power_supply_set_property(struct power_supply
*psy
,
424 enum power_supply_property psp
,
425 const union power_supply_propval
*val
)
427 if (atomic_read(&psy
->use_cnt
) <= 0 || !psy
->desc
->set_property
)
430 return psy
->desc
->set_property(psy
, psp
, val
);
432 EXPORT_SYMBOL_GPL(power_supply_set_property
);
434 int power_supply_property_is_writeable(struct power_supply
*psy
,
435 enum power_supply_property psp
)
437 if (atomic_read(&psy
->use_cnt
) <= 0 ||
438 !psy
->desc
->property_is_writeable
)
441 return psy
->desc
->property_is_writeable(psy
, psp
);
443 EXPORT_SYMBOL_GPL(power_supply_property_is_writeable
);
445 void power_supply_external_power_changed(struct power_supply
*psy
)
447 if (atomic_read(&psy
->use_cnt
) <= 0 ||
448 !psy
->desc
->external_power_changed
)
451 psy
->desc
->external_power_changed(psy
);
453 EXPORT_SYMBOL_GPL(power_supply_external_power_changed
);
455 int power_supply_powers(struct power_supply
*psy
, struct device
*dev
)
457 return sysfs_create_link(&psy
->dev
.kobj
, &dev
->kobj
, "powers");
459 EXPORT_SYMBOL_GPL(power_supply_powers
);
461 static void power_supply_dev_release(struct device
*dev
)
463 struct power_supply
*psy
= container_of(dev
, struct power_supply
, dev
);
464 pr_debug("device: '%s': %s\n", dev_name(dev
), __func__
);
468 int power_supply_reg_notifier(struct notifier_block
*nb
)
470 return atomic_notifier_chain_register(&power_supply_notifier
, nb
);
472 EXPORT_SYMBOL_GPL(power_supply_reg_notifier
);
474 void power_supply_unreg_notifier(struct notifier_block
*nb
)
476 atomic_notifier_chain_unregister(&power_supply_notifier
, nb
);
478 EXPORT_SYMBOL_GPL(power_supply_unreg_notifier
);
480 #ifdef CONFIG_THERMAL
481 static int power_supply_read_temp(struct thermal_zone_device
*tzd
,
484 struct power_supply
*psy
;
485 union power_supply_propval val
;
488 WARN_ON(tzd
== NULL
);
490 ret
= psy
->desc
->get_property(psy
, POWER_SUPPLY_PROP_TEMP
, &val
);
492 /* Convert tenths of degree Celsius to milli degree Celsius. */
494 *temp
= val
.intval
* 100;
499 static struct thermal_zone_device_ops psy_tzd_ops
= {
500 .get_temp
= power_supply_read_temp
,
503 static int psy_register_thermal(struct power_supply
*psy
)
507 if (psy
->desc
->no_thermal
)
510 /* Register battery zone device psy reports temperature */
511 for (i
= 0; i
< psy
->desc
->num_properties
; i
++) {
512 if (psy
->desc
->properties
[i
] == POWER_SUPPLY_PROP_TEMP
) {
513 psy
->tzd
= thermal_zone_device_register(psy
->desc
->name
,
514 0, 0, psy
, &psy_tzd_ops
, NULL
, 0, 0);
515 return PTR_ERR_OR_ZERO(psy
->tzd
);
521 static void psy_unregister_thermal(struct power_supply
*psy
)
523 if (IS_ERR_OR_NULL(psy
->tzd
))
525 thermal_zone_device_unregister(psy
->tzd
);
528 /* thermal cooling device callbacks */
529 static int ps_get_max_charge_cntl_limit(struct thermal_cooling_device
*tcd
,
530 unsigned long *state
)
532 struct power_supply
*psy
;
533 union power_supply_propval val
;
537 ret
= psy
->desc
->get_property(psy
,
538 POWER_SUPPLY_PROP_CHARGE_CONTROL_LIMIT_MAX
, &val
);
545 static int ps_get_cur_chrage_cntl_limit(struct thermal_cooling_device
*tcd
,
546 unsigned long *state
)
548 struct power_supply
*psy
;
549 union power_supply_propval val
;
553 ret
= psy
->desc
->get_property(psy
,
554 POWER_SUPPLY_PROP_CHARGE_CONTROL_LIMIT
, &val
);
561 static int ps_set_cur_charge_cntl_limit(struct thermal_cooling_device
*tcd
,
564 struct power_supply
*psy
;
565 union power_supply_propval val
;
570 ret
= psy
->desc
->set_property(psy
,
571 POWER_SUPPLY_PROP_CHARGE_CONTROL_LIMIT
, &val
);
576 static struct thermal_cooling_device_ops psy_tcd_ops
= {
577 .get_max_state
= ps_get_max_charge_cntl_limit
,
578 .get_cur_state
= ps_get_cur_chrage_cntl_limit
,
579 .set_cur_state
= ps_set_cur_charge_cntl_limit
,
582 static int psy_register_cooler(struct power_supply
*psy
)
586 /* Register for cooling device if psy can control charging */
587 for (i
= 0; i
< psy
->desc
->num_properties
; i
++) {
588 if (psy
->desc
->properties
[i
] ==
589 POWER_SUPPLY_PROP_CHARGE_CONTROL_LIMIT
) {
590 psy
->tcd
= thermal_cooling_device_register(
591 (char *)psy
->desc
->name
,
593 return PTR_ERR_OR_ZERO(psy
->tcd
);
599 static void psy_unregister_cooler(struct power_supply
*psy
)
601 if (IS_ERR_OR_NULL(psy
->tcd
))
603 thermal_cooling_device_unregister(psy
->tcd
);
606 static int psy_register_thermal(struct power_supply
*psy
)
611 static void psy_unregister_thermal(struct power_supply
*psy
)
615 static int psy_register_cooler(struct power_supply
*psy
)
620 static void psy_unregister_cooler(struct power_supply
*psy
)
625 static struct power_supply
*__must_check
626 __power_supply_register(struct device
*parent
,
627 const struct power_supply_desc
*desc
,
628 const struct power_supply_config
*cfg
,
632 struct power_supply
*psy
;
635 psy
= kzalloc(sizeof(*psy
), GFP_KERNEL
);
637 return ERR_PTR(-ENOMEM
);
641 device_initialize(dev
);
643 dev
->class = power_supply_class
;
644 dev
->type
= &power_supply_dev_type
;
645 dev
->parent
= parent
;
646 dev
->release
= power_supply_dev_release
;
647 dev_set_drvdata(dev
, psy
);
649 atomic_inc(&psy
->use_cnt
);
651 psy
->drv_data
= cfg
->drv_data
;
652 psy
->of_node
= cfg
->of_node
;
653 psy
->supplied_to
= cfg
->supplied_to
;
654 psy
->num_supplicants
= cfg
->num_supplicants
;
657 rc
= dev_set_name(dev
, "%s", desc
->name
);
659 goto dev_set_name_failed
;
661 INIT_WORK(&psy
->changed_work
, power_supply_changed_work
);
663 rc
= power_supply_check_supplies(psy
);
665 dev_info(dev
, "Not all required supplies found, defer probe\n");
666 goto check_supplies_failed
;
669 spin_lock_init(&psy
->changed_lock
);
670 rc
= device_init_wakeup(dev
, ws
);
672 goto wakeup_init_failed
;
674 rc
= device_add(dev
);
676 goto device_add_failed
;
678 rc
= psy_register_thermal(psy
);
680 goto register_thermal_failed
;
682 rc
= psy_register_cooler(psy
);
684 goto register_cooler_failed
;
686 rc
= power_supply_create_triggers(psy
);
688 goto create_triggers_failed
;
690 power_supply_changed(psy
);
694 create_triggers_failed
:
695 psy_unregister_cooler(psy
);
696 register_cooler_failed
:
697 psy_unregister_thermal(psy
);
698 register_thermal_failed
:
702 check_supplies_failed
:
709 * power_supply_register() - Register new power supply
710 * @parent: Device to be a parent of power supply's device
711 * @desc: Description of power supply, must be valid through whole
712 * lifetime of this power supply
713 * @cfg: Run-time specific configuration accessed during registering,
716 * Return: A pointer to newly allocated power_supply on success
717 * or ERR_PTR otherwise.
718 * Use power_supply_unregister() on returned power_supply pointer to release
721 struct power_supply
*__must_check
power_supply_register(struct device
*parent
,
722 const struct power_supply_desc
*desc
,
723 const struct power_supply_config
*cfg
)
725 return __power_supply_register(parent
, desc
, cfg
, true);
727 EXPORT_SYMBOL_GPL(power_supply_register
);
730 * power_supply_register() - Register new non-waking-source power supply
731 * @parent: Device to be a parent of power supply's device
732 * @desc: Description of power supply, must be valid through whole
733 * lifetime of this power supply
734 * @cfg: Run-time specific configuration accessed during registering,
737 * Return: A pointer to newly allocated power_supply on success
738 * or ERR_PTR otherwise.
739 * Use power_supply_unregister() on returned power_supply pointer to release
742 struct power_supply
*__must_check
743 power_supply_register_no_ws(struct device
*parent
,
744 const struct power_supply_desc
*desc
,
745 const struct power_supply_config
*cfg
)
747 return __power_supply_register(parent
, desc
, cfg
, false);
749 EXPORT_SYMBOL_GPL(power_supply_register_no_ws
);
751 static void devm_power_supply_release(struct device
*dev
, void *res
)
753 struct power_supply
**psy
= res
;
755 power_supply_unregister(*psy
);
759 * power_supply_register() - Register managed power supply
760 * @parent: Device to be a parent of power supply's device
761 * @desc: Description of power supply, must be valid through whole
762 * lifetime of this power supply
763 * @cfg: Run-time specific configuration accessed during registering,
766 * Return: A pointer to newly allocated power_supply on success
767 * or ERR_PTR otherwise.
768 * The returned power_supply pointer will be automatically unregistered
771 struct power_supply
*__must_check
772 devm_power_supply_register(struct device
*parent
,
773 const struct power_supply_desc
*desc
,
774 const struct power_supply_config
*cfg
)
776 struct power_supply
**ptr
, *psy
;
778 ptr
= devres_alloc(devm_power_supply_release
, sizeof(*ptr
), GFP_KERNEL
);
781 return ERR_PTR(-ENOMEM
);
782 psy
= __power_supply_register(parent
, desc
, cfg
, true);
787 devres_add(parent
, ptr
);
791 EXPORT_SYMBOL_GPL(devm_power_supply_register
);
794 * power_supply_register() - Register managed non-waking-source power supply
795 * @parent: Device to be a parent of power supply's device
796 * @desc: Description of power supply, must be valid through whole
797 * lifetime of this power supply
798 * @cfg: Run-time specific configuration accessed during registering,
801 * Return: A pointer to newly allocated power_supply on success
802 * or ERR_PTR otherwise.
803 * The returned power_supply pointer will be automatically unregistered
806 struct power_supply
*__must_check
807 devm_power_supply_register_no_ws(struct device
*parent
,
808 const struct power_supply_desc
*desc
,
809 const struct power_supply_config
*cfg
)
811 struct power_supply
**ptr
, *psy
;
813 ptr
= devres_alloc(devm_power_supply_release
, sizeof(*ptr
), GFP_KERNEL
);
816 return ERR_PTR(-ENOMEM
);
817 psy
= __power_supply_register(parent
, desc
, cfg
, false);
822 devres_add(parent
, ptr
);
826 EXPORT_SYMBOL_GPL(devm_power_supply_register_no_ws
);
829 * power_supply_unregister() - Remove this power supply from system
830 * @psy: Pointer to power supply to unregister
832 * Remove this power supply from the system. The resources of power supply
833 * will be freed here or on last power_supply_put() call.
835 void power_supply_unregister(struct power_supply
*psy
)
837 WARN_ON(atomic_dec_return(&psy
->use_cnt
));
838 cancel_work_sync(&psy
->changed_work
);
839 sysfs_remove_link(&psy
->dev
.kobj
, "powers");
840 power_supply_remove_triggers(psy
);
841 psy_unregister_cooler(psy
);
842 psy_unregister_thermal(psy
);
843 device_init_wakeup(&psy
->dev
, false);
844 device_unregister(&psy
->dev
);
846 EXPORT_SYMBOL_GPL(power_supply_unregister
);
848 void *power_supply_get_drvdata(struct power_supply
*psy
)
850 return psy
->drv_data
;
852 EXPORT_SYMBOL_GPL(power_supply_get_drvdata
);
854 static int __init
power_supply_class_init(void)
856 power_supply_class
= class_create(THIS_MODULE
, "power_supply");
858 if (IS_ERR(power_supply_class
))
859 return PTR_ERR(power_supply_class
);
861 power_supply_class
->dev_uevent
= power_supply_uevent
;
862 power_supply_init_attrs(&power_supply_dev_type
);
867 static void __exit
power_supply_class_exit(void)
869 class_destroy(power_supply_class
);
872 subsys_initcall(power_supply_class_init
);
873 module_exit(power_supply_class_exit
);
875 MODULE_DESCRIPTION("Universal power supply monitor class");
876 MODULE_AUTHOR("Ian Molton <spyro@f2s.com>, "
878 "Anton Vorontsov <cbou@mail.ru>");
879 MODULE_LICENSE("GPL");