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
) {
45 for (i
= 0; i
< supply
->num_supplies
; i
++)
46 if (!strcmp(supplier
->name
, supply
->supplied_from
[i
]))
51 for (i
= 0; i
< supplier
->num_supplicants
; i
++)
52 if (!strcmp(supplier
->supplied_to
[i
], supply
->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
->external_power_changed
)
66 pst
->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
->name
, epsy
->name
);
143 psy
->supplied_from
[i
-1] = (char *)epsy
->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
, sizeof(char *) * (cnt
- 1),
242 if (!*psy
->supplied_from
) {
243 dev_err(psy
->dev
, "Couldn't allocate memory for supply list\n");
247 return power_supply_populate_supplied_from(psy
);
250 static inline int power_supply_check_supplies(struct power_supply
*psy
)
256 static int __power_supply_am_i_supplied(struct device
*dev
, void *data
)
258 union power_supply_propval ret
= {0,};
259 struct power_supply
*psy
= data
;
260 struct power_supply
*epsy
= dev_get_drvdata(dev
);
262 if (__power_supply_is_supplied_by(epsy
, psy
))
263 if (!epsy
->get_property(epsy
, POWER_SUPPLY_PROP_ONLINE
, &ret
))
269 int power_supply_am_i_supplied(struct power_supply
*psy
)
273 error
= class_for_each_device(power_supply_class
, NULL
, psy
,
274 __power_supply_am_i_supplied
);
276 dev_dbg(psy
->dev
, "%s %d\n", __func__
, error
);
280 EXPORT_SYMBOL_GPL(power_supply_am_i_supplied
);
282 static int __power_supply_is_system_supplied(struct device
*dev
, void *data
)
284 union power_supply_propval ret
= {0,};
285 struct power_supply
*psy
= dev_get_drvdata(dev
);
286 unsigned int *count
= data
;
289 if (psy
->type
!= POWER_SUPPLY_TYPE_BATTERY
)
290 if (!psy
->get_property(psy
, POWER_SUPPLY_PROP_ONLINE
, &ret
))
296 int power_supply_is_system_supplied(void)
299 unsigned int count
= 0;
301 error
= class_for_each_device(power_supply_class
, NULL
, &count
,
302 __power_supply_is_system_supplied
);
305 * If no power class device was found at all, most probably we are
306 * running on a desktop system, so assume we are on mains power.
313 EXPORT_SYMBOL_GPL(power_supply_is_system_supplied
);
315 int power_supply_set_battery_charged(struct power_supply
*psy
)
317 if (psy
->type
== POWER_SUPPLY_TYPE_BATTERY
&& psy
->set_charged
) {
318 psy
->set_charged(psy
);
324 EXPORT_SYMBOL_GPL(power_supply_set_battery_charged
);
326 static int power_supply_match_device_by_name(struct device
*dev
, const void *data
)
328 const char *name
= data
;
329 struct power_supply
*psy
= dev_get_drvdata(dev
);
331 return strcmp(psy
->name
, name
) == 0;
334 struct power_supply
*power_supply_get_by_name(const char *name
)
336 struct device
*dev
= class_find_device(power_supply_class
, NULL
, name
,
337 power_supply_match_device_by_name
);
339 return dev
? dev_get_drvdata(dev
) : NULL
;
341 EXPORT_SYMBOL_GPL(power_supply_get_by_name
);
344 static int power_supply_match_device_node(struct device
*dev
, const void *data
)
346 return dev
->parent
&& dev
->parent
->of_node
== data
;
349 struct power_supply
*power_supply_get_by_phandle(struct device_node
*np
,
350 const char *property
)
352 struct device_node
*power_supply_np
;
355 power_supply_np
= of_parse_phandle(np
, property
, 0);
356 if (!power_supply_np
)
357 return ERR_PTR(-ENODEV
);
359 dev
= class_find_device(power_supply_class
, NULL
, power_supply_np
,
360 power_supply_match_device_node
);
362 of_node_put(power_supply_np
);
364 return dev
? dev_get_drvdata(dev
) : NULL
;
366 EXPORT_SYMBOL_GPL(power_supply_get_by_phandle
);
367 #endif /* CONFIG_OF */
369 int power_supply_powers(struct power_supply
*psy
, struct device
*dev
)
371 return sysfs_create_link(&psy
->dev
->kobj
, &dev
->kobj
, "powers");
373 EXPORT_SYMBOL_GPL(power_supply_powers
);
375 static void power_supply_dev_release(struct device
*dev
)
377 pr_debug("device: '%s': %s\n", dev_name(dev
), __func__
);
381 int power_supply_reg_notifier(struct notifier_block
*nb
)
383 return atomic_notifier_chain_register(&power_supply_notifier
, nb
);
385 EXPORT_SYMBOL_GPL(power_supply_reg_notifier
);
387 void power_supply_unreg_notifier(struct notifier_block
*nb
)
389 atomic_notifier_chain_unregister(&power_supply_notifier
, nb
);
391 EXPORT_SYMBOL_GPL(power_supply_unreg_notifier
);
393 #ifdef CONFIG_THERMAL
394 static int power_supply_read_temp(struct thermal_zone_device
*tzd
,
397 struct power_supply
*psy
;
398 union power_supply_propval val
;
401 WARN_ON(tzd
== NULL
);
403 ret
= psy
->get_property(psy
, POWER_SUPPLY_PROP_TEMP
, &val
);
405 /* Convert tenths of degree Celsius to milli degree Celsius. */
407 *temp
= val
.intval
* 100;
412 static struct thermal_zone_device_ops psy_tzd_ops
= {
413 .get_temp
= power_supply_read_temp
,
416 static int psy_register_thermal(struct power_supply
*psy
)
423 /* Register battery zone device psy reports temperature */
424 for (i
= 0; i
< psy
->num_properties
; i
++) {
425 if (psy
->properties
[i
] == POWER_SUPPLY_PROP_TEMP
) {
426 psy
->tzd
= thermal_zone_device_register(psy
->name
, 0, 0,
427 psy
, &psy_tzd_ops
, NULL
, 0, 0);
428 return PTR_ERR_OR_ZERO(psy
->tzd
);
434 static void psy_unregister_thermal(struct power_supply
*psy
)
436 if (IS_ERR_OR_NULL(psy
->tzd
))
438 thermal_zone_device_unregister(psy
->tzd
);
441 /* thermal cooling device callbacks */
442 static int ps_get_max_charge_cntl_limit(struct thermal_cooling_device
*tcd
,
443 unsigned long *state
)
445 struct power_supply
*psy
;
446 union power_supply_propval val
;
450 ret
= psy
->get_property(psy
,
451 POWER_SUPPLY_PROP_CHARGE_CONTROL_LIMIT_MAX
, &val
);
458 static int ps_get_cur_chrage_cntl_limit(struct thermal_cooling_device
*tcd
,
459 unsigned long *state
)
461 struct power_supply
*psy
;
462 union power_supply_propval val
;
466 ret
= psy
->get_property(psy
,
467 POWER_SUPPLY_PROP_CHARGE_CONTROL_LIMIT
, &val
);
474 static int ps_set_cur_charge_cntl_limit(struct thermal_cooling_device
*tcd
,
477 struct power_supply
*psy
;
478 union power_supply_propval val
;
483 ret
= psy
->set_property(psy
,
484 POWER_SUPPLY_PROP_CHARGE_CONTROL_LIMIT
, &val
);
489 static struct thermal_cooling_device_ops psy_tcd_ops
= {
490 .get_max_state
= ps_get_max_charge_cntl_limit
,
491 .get_cur_state
= ps_get_cur_chrage_cntl_limit
,
492 .set_cur_state
= ps_set_cur_charge_cntl_limit
,
495 static int psy_register_cooler(struct power_supply
*psy
)
499 /* Register for cooling device if psy can control charging */
500 for (i
= 0; i
< psy
->num_properties
; i
++) {
501 if (psy
->properties
[i
] ==
502 POWER_SUPPLY_PROP_CHARGE_CONTROL_LIMIT
) {
503 psy
->tcd
= thermal_cooling_device_register(
506 return PTR_ERR_OR_ZERO(psy
->tcd
);
512 static void psy_unregister_cooler(struct power_supply
*psy
)
514 if (IS_ERR_OR_NULL(psy
->tcd
))
516 thermal_cooling_device_unregister(psy
->tcd
);
519 static int psy_register_thermal(struct power_supply
*psy
)
524 static void psy_unregister_thermal(struct power_supply
*psy
)
528 static int psy_register_cooler(struct power_supply
*psy
)
533 static void psy_unregister_cooler(struct power_supply
*psy
)
538 static int __power_supply_register(struct device
*parent
,
539 struct power_supply
*psy
, bool ws
)
544 dev
= kzalloc(sizeof(*dev
), GFP_KERNEL
);
548 device_initialize(dev
);
550 dev
->class = power_supply_class
;
551 dev
->type
= &power_supply_dev_type
;
552 dev
->parent
= parent
;
553 dev
->release
= power_supply_dev_release
;
554 dev_set_drvdata(dev
, psy
);
557 rc
= dev_set_name(dev
, "%s", psy
->name
);
559 goto dev_set_name_failed
;
561 INIT_WORK(&psy
->changed_work
, power_supply_changed_work
);
563 rc
= power_supply_check_supplies(psy
);
565 dev_info(dev
, "Not all required supplies found, defer probe\n");
566 goto check_supplies_failed
;
569 spin_lock_init(&psy
->changed_lock
);
570 rc
= device_init_wakeup(dev
, ws
);
572 goto wakeup_init_failed
;
574 rc
= device_add(dev
);
576 goto device_add_failed
;
578 rc
= psy_register_thermal(psy
);
580 goto register_thermal_failed
;
582 rc
= psy_register_cooler(psy
);
584 goto register_cooler_failed
;
586 rc
= power_supply_create_triggers(psy
);
588 goto create_triggers_failed
;
590 power_supply_changed(psy
);
594 create_triggers_failed
:
595 psy_unregister_cooler(psy
);
596 register_cooler_failed
:
597 psy_unregister_thermal(psy
);
598 register_thermal_failed
:
602 check_supplies_failed
:
608 int power_supply_register(struct device
*parent
, struct power_supply
*psy
)
610 return __power_supply_register(parent
, psy
, true);
612 EXPORT_SYMBOL_GPL(power_supply_register
);
614 int power_supply_register_no_ws(struct device
*parent
, struct power_supply
*psy
)
616 return __power_supply_register(parent
, psy
, false);
618 EXPORT_SYMBOL_GPL(power_supply_register_no_ws
);
620 void power_supply_unregister(struct power_supply
*psy
)
622 cancel_work_sync(&psy
->changed_work
);
623 sysfs_remove_link(&psy
->dev
->kobj
, "powers");
624 power_supply_remove_triggers(psy
);
625 psy_unregister_cooler(psy
);
626 psy_unregister_thermal(psy
);
627 device_init_wakeup(psy
->dev
, false);
628 device_unregister(psy
->dev
);
630 EXPORT_SYMBOL_GPL(power_supply_unregister
);
632 static int __init
power_supply_class_init(void)
634 power_supply_class
= class_create(THIS_MODULE
, "power_supply");
636 if (IS_ERR(power_supply_class
))
637 return PTR_ERR(power_supply_class
);
639 power_supply_class
->dev_uevent
= power_supply_uevent
;
640 power_supply_init_attrs(&power_supply_dev_type
);
645 static void __exit
power_supply_class_exit(void)
647 class_destroy(power_supply_class
);
650 subsys_initcall(power_supply_class_init
);
651 module_exit(power_supply_class_exit
);
653 MODULE_DESCRIPTION("Universal power supply monitor class");
654 MODULE_AUTHOR("Ian Molton <spyro@f2s.com>, "
656 "Anton Vorontsov <cbou@mail.ru>");
657 MODULE_LICENSE("GPL");