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/delay.h>
18 #include <linux/device.h>
19 #include <linux/notifier.h>
20 #include <linux/err.h>
22 #include <linux/power_supply.h>
23 #include <linux/property.h>
24 #include <linux/thermal.h>
25 #include "power_supply.h"
27 /* exported for the APM Power driver, APM emulation */
28 struct class *power_supply_class
;
29 EXPORT_SYMBOL_GPL(power_supply_class
);
31 ATOMIC_NOTIFIER_HEAD(power_supply_notifier
);
32 EXPORT_SYMBOL_GPL(power_supply_notifier
);
34 static struct device_type power_supply_dev_type
;
36 #define POWER_SUPPLY_DEFERRED_REGISTER_TIME msecs_to_jiffies(10)
38 static bool __power_supply_is_supplied_by(struct power_supply
*supplier
,
39 struct power_supply
*supply
)
43 if (!supply
->supplied_from
&& !supplier
->supplied_to
)
46 /* Support both supplied_to and supplied_from modes */
47 if (supply
->supplied_from
) {
48 if (!supplier
->desc
->name
)
50 for (i
= 0; i
< supply
->num_supplies
; i
++)
51 if (!strcmp(supplier
->desc
->name
, supply
->supplied_from
[i
]))
54 if (!supply
->desc
->name
)
56 for (i
= 0; i
< supplier
->num_supplicants
; i
++)
57 if (!strcmp(supplier
->supplied_to
[i
], supply
->desc
->name
))
64 static int __power_supply_changed_work(struct device
*dev
, void *data
)
66 struct power_supply
*psy
= data
;
67 struct power_supply
*pst
= dev_get_drvdata(dev
);
69 if (__power_supply_is_supplied_by(psy
, pst
)) {
70 if (pst
->desc
->external_power_changed
)
71 pst
->desc
->external_power_changed(pst
);
77 static void power_supply_changed_work(struct work_struct
*work
)
80 struct power_supply
*psy
= container_of(work
, struct power_supply
,
83 dev_dbg(&psy
->dev
, "%s\n", __func__
);
85 spin_lock_irqsave(&psy
->changed_lock
, flags
);
87 * Check 'changed' here to avoid issues due to race between
88 * power_supply_changed() and this routine. In worst case
89 * power_supply_changed() can be called again just before we take above
90 * lock. During the first call of this routine we will mark 'changed' as
91 * false and it will stay false for the next call as well.
93 if (likely(psy
->changed
)) {
95 spin_unlock_irqrestore(&psy
->changed_lock
, flags
);
96 class_for_each_device(power_supply_class
, NULL
, psy
,
97 __power_supply_changed_work
);
98 power_supply_update_leds(psy
);
99 atomic_notifier_call_chain(&power_supply_notifier
,
100 PSY_EVENT_PROP_CHANGED
, psy
);
101 kobject_uevent(&psy
->dev
.kobj
, KOBJ_CHANGE
);
102 spin_lock_irqsave(&psy
->changed_lock
, flags
);
106 * Hold the wakeup_source until all events are processed.
107 * power_supply_changed() might have called again and have set 'changed'
110 if (likely(!psy
->changed
))
112 spin_unlock_irqrestore(&psy
->changed_lock
, flags
);
115 void power_supply_changed(struct power_supply
*psy
)
119 dev_dbg(&psy
->dev
, "%s\n", __func__
);
121 spin_lock_irqsave(&psy
->changed_lock
, flags
);
123 pm_stay_awake(&psy
->dev
);
124 spin_unlock_irqrestore(&psy
->changed_lock
, flags
);
125 schedule_work(&psy
->changed_work
);
127 EXPORT_SYMBOL_GPL(power_supply_changed
);
130 * Notify that power supply was registered after parent finished the probing.
132 * Often power supply is registered from driver's probe function. However
133 * calling power_supply_changed() directly from power_supply_register()
134 * would lead to execution of get_property() function provided by the driver
135 * too early - before the probe ends.
137 * Avoid that by waiting on parent's mutex.
139 static void power_supply_deferred_register_work(struct work_struct
*work
)
141 struct power_supply
*psy
= container_of(work
, struct power_supply
,
142 deferred_register_work
.work
);
144 if (psy
->dev
.parent
) {
145 while (!mutex_trylock(&psy
->dev
.parent
->mutex
)) {
152 power_supply_changed(psy
);
155 mutex_unlock(&psy
->dev
.parent
->mutex
);
159 #include <linux/of.h>
161 static int __power_supply_populate_supplied_from(struct device
*dev
,
164 struct power_supply
*psy
= data
;
165 struct power_supply
*epsy
= dev_get_drvdata(dev
);
166 struct device_node
*np
;
170 np
= of_parse_phandle(psy
->of_node
, "power-supplies", i
++);
174 if (np
== epsy
->of_node
) {
175 dev_info(&psy
->dev
, "%s: Found supply : %s\n",
176 psy
->desc
->name
, epsy
->desc
->name
);
177 psy
->supplied_from
[i
-1] = (char *)epsy
->desc
->name
;
188 static int power_supply_populate_supplied_from(struct power_supply
*psy
)
192 error
= class_for_each_device(power_supply_class
, NULL
, psy
,
193 __power_supply_populate_supplied_from
);
195 dev_dbg(&psy
->dev
, "%s %d\n", __func__
, error
);
200 static int __power_supply_find_supply_from_node(struct device
*dev
,
203 struct device_node
*np
= data
;
204 struct power_supply
*epsy
= dev_get_drvdata(dev
);
206 /* returning non-zero breaks out of class_for_each_device loop */
207 if (epsy
->of_node
== np
)
213 static int power_supply_find_supply_from_node(struct device_node
*supply_node
)
218 * class_for_each_device() either returns its own errors or values
219 * returned by __power_supply_find_supply_from_node().
221 * __power_supply_find_supply_from_node() will return 0 (no match)
224 * We return 0 if class_for_each_device() returned 1, -EPROBE_DEFER if
225 * it returned 0, or error as returned by it.
227 error
= class_for_each_device(power_supply_class
, NULL
, supply_node
,
228 __power_supply_find_supply_from_node
);
230 return error
? (error
== 1 ? 0 : error
) : -EPROBE_DEFER
;
233 static int power_supply_check_supplies(struct power_supply
*psy
)
235 struct device_node
*np
;
238 /* If there is already a list honor it */
239 if (psy
->supplied_from
&& psy
->num_supplies
> 0)
242 /* No device node found, nothing to do */
249 np
= of_parse_phandle(psy
->of_node
, "power-supplies", cnt
++);
253 ret
= power_supply_find_supply_from_node(np
);
257 dev_dbg(&psy
->dev
, "Failed to find supply!\n");
262 /* Missing valid "power-supplies" entries */
266 /* All supplies found, allocate char ** array for filling */
267 psy
->supplied_from
= devm_kzalloc(&psy
->dev
, sizeof(psy
->supplied_from
),
269 if (!psy
->supplied_from
)
272 *psy
->supplied_from
= devm_kcalloc(&psy
->dev
,
273 cnt
- 1, sizeof(char *),
275 if (!*psy
->supplied_from
)
278 return power_supply_populate_supplied_from(psy
);
281 static int power_supply_check_supplies(struct power_supply
*psy
)
285 if (!psy
->dev
.parent
)
288 nval
= device_property_read_string_array(psy
->dev
.parent
,
289 "supplied-from", NULL
, 0);
293 psy
->supplied_from
= devm_kmalloc_array(&psy
->dev
, nval
,
294 sizeof(char *), GFP_KERNEL
);
295 if (!psy
->supplied_from
)
298 ret
= device_property_read_string_array(psy
->dev
.parent
,
299 "supplied-from", (const char **)psy
->supplied_from
, nval
);
303 psy
->num_supplies
= nval
;
309 struct psy_am_i_supplied_data
{
310 struct power_supply
*psy
;
314 static int __power_supply_am_i_supplied(struct device
*dev
, void *_data
)
316 union power_supply_propval ret
= {0,};
317 struct power_supply
*epsy
= dev_get_drvdata(dev
);
318 struct psy_am_i_supplied_data
*data
= _data
;
320 if (__power_supply_is_supplied_by(epsy
, data
->psy
)) {
322 if (!epsy
->desc
->get_property(epsy
, POWER_SUPPLY_PROP_ONLINE
,
330 int power_supply_am_i_supplied(struct power_supply
*psy
)
332 struct psy_am_i_supplied_data data
= { psy
, 0 };
335 error
= class_for_each_device(power_supply_class
, NULL
, &data
,
336 __power_supply_am_i_supplied
);
338 dev_dbg(&psy
->dev
, "%s count %u err %d\n", __func__
, data
.count
, error
);
345 EXPORT_SYMBOL_GPL(power_supply_am_i_supplied
);
347 static int __power_supply_is_system_supplied(struct device
*dev
, void *data
)
349 union power_supply_propval ret
= {0,};
350 struct power_supply
*psy
= dev_get_drvdata(dev
);
351 unsigned int *count
= data
;
354 if (psy
->desc
->type
!= POWER_SUPPLY_TYPE_BATTERY
)
355 if (!psy
->desc
->get_property(psy
, POWER_SUPPLY_PROP_ONLINE
,
362 int power_supply_is_system_supplied(void)
365 unsigned int count
= 0;
367 error
= class_for_each_device(power_supply_class
, NULL
, &count
,
368 __power_supply_is_system_supplied
);
371 * If no power class device was found at all, most probably we are
372 * running on a desktop system, so assume we are on mains power.
379 EXPORT_SYMBOL_GPL(power_supply_is_system_supplied
);
381 static int __power_supply_get_supplier_max_current(struct device
*dev
,
384 union power_supply_propval ret
= {0,};
385 struct power_supply
*epsy
= dev_get_drvdata(dev
);
386 struct power_supply
*psy
= data
;
388 if (__power_supply_is_supplied_by(epsy
, psy
))
389 if (!epsy
->desc
->get_property(epsy
,
390 POWER_SUPPLY_PROP_CURRENT_MAX
,
397 int power_supply_set_input_current_limit_from_supplier(struct power_supply
*psy
)
399 union power_supply_propval val
= {0,};
402 if (!psy
->desc
->set_property
)
406 * This function is not intended for use with a supply with multiple
407 * suppliers, we simply pick the first supply to report a non 0
410 curr
= class_for_each_device(power_supply_class
, NULL
, psy
,
411 __power_supply_get_supplier_max_current
);
413 return (curr
== 0) ? -ENODEV
: curr
;
417 return psy
->desc
->set_property(psy
,
418 POWER_SUPPLY_PROP_INPUT_CURRENT_LIMIT
, &val
);
420 EXPORT_SYMBOL_GPL(power_supply_set_input_current_limit_from_supplier
);
422 int power_supply_set_battery_charged(struct power_supply
*psy
)
424 if (atomic_read(&psy
->use_cnt
) >= 0 &&
425 psy
->desc
->type
== POWER_SUPPLY_TYPE_BATTERY
&&
426 psy
->desc
->set_charged
) {
427 psy
->desc
->set_charged(psy
);
433 EXPORT_SYMBOL_GPL(power_supply_set_battery_charged
);
435 static int power_supply_match_device_by_name(struct device
*dev
, const void *data
)
437 const char *name
= data
;
438 struct power_supply
*psy
= dev_get_drvdata(dev
);
440 return strcmp(psy
->desc
->name
, name
) == 0;
444 * power_supply_get_by_name() - Search for a power supply and returns its ref
445 * @name: Power supply name to fetch
447 * If power supply was found, it increases reference count for the
448 * internal power supply's device. The user should power_supply_put()
451 * Return: On success returns a reference to a power supply with
452 * matching name equals to @name, a NULL otherwise.
454 struct power_supply
*power_supply_get_by_name(const char *name
)
456 struct power_supply
*psy
= NULL
;
457 struct device
*dev
= class_find_device(power_supply_class
, NULL
, name
,
458 power_supply_match_device_by_name
);
461 psy
= dev_get_drvdata(dev
);
462 atomic_inc(&psy
->use_cnt
);
467 EXPORT_SYMBOL_GPL(power_supply_get_by_name
);
470 * power_supply_put() - Drop reference obtained with power_supply_get_by_name
471 * @psy: Reference to put
473 * The reference to power supply should be put before unregistering
476 void power_supply_put(struct power_supply
*psy
)
480 atomic_dec(&psy
->use_cnt
);
481 put_device(&psy
->dev
);
483 EXPORT_SYMBOL_GPL(power_supply_put
);
486 static int power_supply_match_device_node(struct device
*dev
, const void *data
)
488 return dev
->parent
&& dev
->parent
->of_node
== data
;
492 * power_supply_get_by_phandle() - Search for a power supply and returns its ref
493 * @np: Pointer to device node holding phandle property
494 * @property: Name of property holding a power supply name
496 * If power supply was found, it increases reference count for the
497 * internal power supply's device. The user should power_supply_put()
500 * Return: On success returns a reference to a power supply with
501 * matching name equals to value under @property, NULL or ERR_PTR otherwise.
503 struct power_supply
*power_supply_get_by_phandle(struct device_node
*np
,
504 const char *property
)
506 struct device_node
*power_supply_np
;
507 struct power_supply
*psy
= NULL
;
510 power_supply_np
= of_parse_phandle(np
, property
, 0);
511 if (!power_supply_np
)
512 return ERR_PTR(-ENODEV
);
514 dev
= class_find_device(power_supply_class
, NULL
, power_supply_np
,
515 power_supply_match_device_node
);
517 of_node_put(power_supply_np
);
520 psy
= dev_get_drvdata(dev
);
521 atomic_inc(&psy
->use_cnt
);
526 EXPORT_SYMBOL_GPL(power_supply_get_by_phandle
);
528 static void devm_power_supply_put(struct device
*dev
, void *res
)
530 struct power_supply
**psy
= res
;
532 power_supply_put(*psy
);
536 * devm_power_supply_get_by_phandle() - Resource managed version of
537 * power_supply_get_by_phandle()
538 * @dev: Pointer to device holding phandle property
539 * @property: Name of property holding a power supply phandle
541 * Return: On success returns a reference to a power supply with
542 * matching name equals to value under @property, NULL or ERR_PTR otherwise.
544 struct power_supply
*devm_power_supply_get_by_phandle(struct device
*dev
,
545 const char *property
)
547 struct power_supply
**ptr
, *psy
;
550 return ERR_PTR(-ENODEV
);
552 ptr
= devres_alloc(devm_power_supply_put
, sizeof(*ptr
), GFP_KERNEL
);
554 return ERR_PTR(-ENOMEM
);
556 psy
= power_supply_get_by_phandle(dev
->of_node
, property
);
557 if (IS_ERR_OR_NULL(psy
)) {
561 devres_add(dev
, ptr
);
565 EXPORT_SYMBOL_GPL(devm_power_supply_get_by_phandle
);
566 #endif /* CONFIG_OF */
568 int power_supply_get_battery_info(struct power_supply
*psy
,
569 struct power_supply_battery_info
*info
)
571 struct device_node
*battery_np
;
575 info
->energy_full_design_uwh
= -EINVAL
;
576 info
->charge_full_design_uah
= -EINVAL
;
577 info
->voltage_min_design_uv
= -EINVAL
;
578 info
->precharge_current_ua
= -EINVAL
;
579 info
->charge_term_current_ua
= -EINVAL
;
580 info
->constant_charge_current_max_ua
= -EINVAL
;
581 info
->constant_charge_voltage_max_uv
= -EINVAL
;
582 info
->factory_internal_resistance_uohm
= -EINVAL
;
584 for (index
= 0; index
< POWER_SUPPLY_OCV_TEMP_MAX
; index
++) {
585 info
->ocv_table
[index
] = NULL
;
586 info
->ocv_temp
[index
] = -EINVAL
;
587 info
->ocv_table_size
[index
] = -EINVAL
;
591 dev_warn(&psy
->dev
, "%s currently only supports devicetree\n",
596 battery_np
= of_parse_phandle(psy
->of_node
, "monitored-battery", 0);
600 err
= of_property_read_string(battery_np
, "compatible", &value
);
604 if (strcmp("simple-battery", value
))
607 /* The property and field names below must correspond to elements
608 * in enum power_supply_property. For reasoning, see
609 * Documentation/power/power_supply_class.txt.
612 of_property_read_u32(battery_np
, "energy-full-design-microwatt-hours",
613 &info
->energy_full_design_uwh
);
614 of_property_read_u32(battery_np
, "charge-full-design-microamp-hours",
615 &info
->charge_full_design_uah
);
616 of_property_read_u32(battery_np
, "voltage-min-design-microvolt",
617 &info
->voltage_min_design_uv
);
618 of_property_read_u32(battery_np
, "precharge-current-microamp",
619 &info
->precharge_current_ua
);
620 of_property_read_u32(battery_np
, "charge-term-current-microamp",
621 &info
->charge_term_current_ua
);
622 of_property_read_u32(battery_np
, "constant_charge_current_max_microamp",
623 &info
->constant_charge_current_max_ua
);
624 of_property_read_u32(battery_np
, "constant_charge_voltage_max_microvolt",
625 &info
->constant_charge_voltage_max_uv
);
626 of_property_read_u32(battery_np
, "factory-internal-resistance-micro-ohms",
627 &info
->factory_internal_resistance_uohm
);
629 len
= of_property_count_u32_elems(battery_np
, "ocv-capacity-celsius");
630 if (len
< 0 && len
!= -EINVAL
) {
632 } else if (len
> POWER_SUPPLY_OCV_TEMP_MAX
) {
633 dev_err(&psy
->dev
, "Too many temperature values\n");
635 } else if (len
> 0) {
636 of_property_read_u32_array(battery_np
, "ocv-capacity-celsius",
637 info
->ocv_temp
, len
);
640 for (index
= 0; index
< len
; index
++) {
641 struct power_supply_battery_ocv_table
*table
;
644 int i
, tab_len
, size
;
646 propname
= kasprintf(GFP_KERNEL
, "ocv-capacity-table-%d", index
);
647 list
= of_get_property(battery_np
, propname
, &size
);
648 if (!list
|| !size
) {
649 dev_err(&psy
->dev
, "failed to get %s\n", propname
);
651 power_supply_put_battery_info(psy
, info
);
656 tab_len
= size
/ (2 * sizeof(__be32
));
657 info
->ocv_table_size
[index
] = tab_len
;
659 table
= info
->ocv_table
[index
] =
660 devm_kcalloc(&psy
->dev
, tab_len
, sizeof(*table
), GFP_KERNEL
);
661 if (!info
->ocv_table
[index
]) {
662 power_supply_put_battery_info(psy
, info
);
666 for (i
= 0; i
< tab_len
; i
++) {
667 table
[i
].ocv
= be32_to_cpu(*list
++);
668 table
[i
].capacity
= be32_to_cpu(*list
++);
674 EXPORT_SYMBOL_GPL(power_supply_get_battery_info
);
676 void power_supply_put_battery_info(struct power_supply
*psy
,
677 struct power_supply_battery_info
*info
)
681 for (i
= 0; i
< POWER_SUPPLY_OCV_TEMP_MAX
; i
++) {
682 if (info
->ocv_table
[i
])
683 devm_kfree(&psy
->dev
, info
->ocv_table
[i
]);
686 EXPORT_SYMBOL_GPL(power_supply_put_battery_info
);
689 * power_supply_ocv2cap_simple() - find the battery capacity
690 * @table: Pointer to battery OCV lookup table
691 * @table_len: OCV table length
692 * @ocv: Current OCV value
694 * This helper function is used to look up battery capacity according to
695 * current OCV value from one OCV table, and the OCV table must be ordered
698 * Return: the battery capacity.
700 int power_supply_ocv2cap_simple(struct power_supply_battery_ocv_table
*table
,
701 int table_len
, int ocv
)
705 for (i
= 0; i
< table_len
; i
++)
706 if (ocv
> table
[i
].ocv
)
709 if (i
> 0 && i
< table_len
) {
710 tmp
= (table
[i
- 1].capacity
- table
[i
].capacity
) *
711 (ocv
- table
[i
].ocv
);
712 tmp
/= table
[i
- 1].ocv
- table
[i
].ocv
;
713 cap
= tmp
+ table
[i
].capacity
;
715 cap
= table
[0].capacity
;
717 cap
= table
[table_len
- 1].capacity
;
722 EXPORT_SYMBOL_GPL(power_supply_ocv2cap_simple
);
724 struct power_supply_battery_ocv_table
*
725 power_supply_find_ocv2cap_table(struct power_supply_battery_info
*info
,
726 int temp
, int *table_len
)
728 int best_temp_diff
= INT_MAX
, temp_diff
;
729 u8 i
, best_index
= 0;
731 if (!info
->ocv_table
[0])
734 for (i
= 0; i
< POWER_SUPPLY_OCV_TEMP_MAX
; i
++) {
735 temp_diff
= abs(info
->ocv_temp
[i
] - temp
);
737 if (temp_diff
< best_temp_diff
) {
738 best_temp_diff
= temp_diff
;
743 *table_len
= info
->ocv_table_size
[best_index
];
744 return info
->ocv_table
[best_index
];
746 EXPORT_SYMBOL_GPL(power_supply_find_ocv2cap_table
);
748 int power_supply_batinfo_ocv2cap(struct power_supply_battery_info
*info
,
751 struct power_supply_battery_ocv_table
*table
;
754 table
= power_supply_find_ocv2cap_table(info
, temp
, &table_len
);
758 return power_supply_ocv2cap_simple(table
, table_len
, ocv
);
760 EXPORT_SYMBOL_GPL(power_supply_batinfo_ocv2cap
);
762 int power_supply_get_property(struct power_supply
*psy
,
763 enum power_supply_property psp
,
764 union power_supply_propval
*val
)
766 if (atomic_read(&psy
->use_cnt
) <= 0) {
767 if (!psy
->initialized
)
772 return psy
->desc
->get_property(psy
, psp
, val
);
774 EXPORT_SYMBOL_GPL(power_supply_get_property
);
776 int power_supply_set_property(struct power_supply
*psy
,
777 enum power_supply_property psp
,
778 const union power_supply_propval
*val
)
780 if (atomic_read(&psy
->use_cnt
) <= 0 || !psy
->desc
->set_property
)
783 return psy
->desc
->set_property(psy
, psp
, val
);
785 EXPORT_SYMBOL_GPL(power_supply_set_property
);
787 int power_supply_property_is_writeable(struct power_supply
*psy
,
788 enum power_supply_property psp
)
790 if (atomic_read(&psy
->use_cnt
) <= 0 ||
791 !psy
->desc
->property_is_writeable
)
794 return psy
->desc
->property_is_writeable(psy
, psp
);
796 EXPORT_SYMBOL_GPL(power_supply_property_is_writeable
);
798 void power_supply_external_power_changed(struct power_supply
*psy
)
800 if (atomic_read(&psy
->use_cnt
) <= 0 ||
801 !psy
->desc
->external_power_changed
)
804 psy
->desc
->external_power_changed(psy
);
806 EXPORT_SYMBOL_GPL(power_supply_external_power_changed
);
808 int power_supply_powers(struct power_supply
*psy
, struct device
*dev
)
810 return sysfs_create_link(&psy
->dev
.kobj
, &dev
->kobj
, "powers");
812 EXPORT_SYMBOL_GPL(power_supply_powers
);
814 static void power_supply_dev_release(struct device
*dev
)
816 struct power_supply
*psy
= to_power_supply(dev
);
817 dev_dbg(dev
, "%s\n", __func__
);
821 int power_supply_reg_notifier(struct notifier_block
*nb
)
823 return atomic_notifier_chain_register(&power_supply_notifier
, nb
);
825 EXPORT_SYMBOL_GPL(power_supply_reg_notifier
);
827 void power_supply_unreg_notifier(struct notifier_block
*nb
)
829 atomic_notifier_chain_unregister(&power_supply_notifier
, nb
);
831 EXPORT_SYMBOL_GPL(power_supply_unreg_notifier
);
833 #ifdef CONFIG_THERMAL
834 static int power_supply_read_temp(struct thermal_zone_device
*tzd
,
837 struct power_supply
*psy
;
838 union power_supply_propval val
;
841 WARN_ON(tzd
== NULL
);
843 ret
= power_supply_get_property(psy
, POWER_SUPPLY_PROP_TEMP
, &val
);
847 /* Convert tenths of degree Celsius to milli degree Celsius. */
848 *temp
= val
.intval
* 100;
853 static struct thermal_zone_device_ops psy_tzd_ops
= {
854 .get_temp
= power_supply_read_temp
,
857 static int psy_register_thermal(struct power_supply
*psy
)
861 if (psy
->desc
->no_thermal
)
864 /* Register battery zone device psy reports temperature */
865 for (i
= 0; i
< psy
->desc
->num_properties
; i
++) {
866 if (psy
->desc
->properties
[i
] == POWER_SUPPLY_PROP_TEMP
) {
867 psy
->tzd
= thermal_zone_device_register(psy
->desc
->name
,
868 0, 0, psy
, &psy_tzd_ops
, NULL
, 0, 0);
869 return PTR_ERR_OR_ZERO(psy
->tzd
);
875 static void psy_unregister_thermal(struct power_supply
*psy
)
877 if (IS_ERR_OR_NULL(psy
->tzd
))
879 thermal_zone_device_unregister(psy
->tzd
);
882 /* thermal cooling device callbacks */
883 static int ps_get_max_charge_cntl_limit(struct thermal_cooling_device
*tcd
,
884 unsigned long *state
)
886 struct power_supply
*psy
;
887 union power_supply_propval val
;
891 ret
= power_supply_get_property(psy
,
892 POWER_SUPPLY_PROP_CHARGE_CONTROL_LIMIT_MAX
, &val
);
901 static int ps_get_cur_chrage_cntl_limit(struct thermal_cooling_device
*tcd
,
902 unsigned long *state
)
904 struct power_supply
*psy
;
905 union power_supply_propval val
;
909 ret
= power_supply_get_property(psy
,
910 POWER_SUPPLY_PROP_CHARGE_CONTROL_LIMIT
, &val
);
919 static int ps_set_cur_charge_cntl_limit(struct thermal_cooling_device
*tcd
,
922 struct power_supply
*psy
;
923 union power_supply_propval val
;
928 ret
= psy
->desc
->set_property(psy
,
929 POWER_SUPPLY_PROP_CHARGE_CONTROL_LIMIT
, &val
);
934 static const struct thermal_cooling_device_ops psy_tcd_ops
= {
935 .get_max_state
= ps_get_max_charge_cntl_limit
,
936 .get_cur_state
= ps_get_cur_chrage_cntl_limit
,
937 .set_cur_state
= ps_set_cur_charge_cntl_limit
,
940 static int psy_register_cooler(struct power_supply
*psy
)
944 /* Register for cooling device if psy can control charging */
945 for (i
= 0; i
< psy
->desc
->num_properties
; i
++) {
946 if (psy
->desc
->properties
[i
] ==
947 POWER_SUPPLY_PROP_CHARGE_CONTROL_LIMIT
) {
948 psy
->tcd
= thermal_cooling_device_register(
949 (char *)psy
->desc
->name
,
951 return PTR_ERR_OR_ZERO(psy
->tcd
);
957 static void psy_unregister_cooler(struct power_supply
*psy
)
959 if (IS_ERR_OR_NULL(psy
->tcd
))
961 thermal_cooling_device_unregister(psy
->tcd
);
964 static int psy_register_thermal(struct power_supply
*psy
)
969 static void psy_unregister_thermal(struct power_supply
*psy
)
973 static int psy_register_cooler(struct power_supply
*psy
)
978 static void psy_unregister_cooler(struct power_supply
*psy
)
983 static struct power_supply
*__must_check
984 __power_supply_register(struct device
*parent
,
985 const struct power_supply_desc
*desc
,
986 const struct power_supply_config
*cfg
,
990 struct power_supply
*psy
;
994 pr_warn("%s: Expected proper parent device for '%s'\n",
995 __func__
, desc
->name
);
997 if (!desc
|| !desc
->name
|| !desc
->properties
|| !desc
->num_properties
)
998 return ERR_PTR(-EINVAL
);
1000 for (i
= 0; i
< desc
->num_properties
; ++i
) {
1001 if ((desc
->properties
[i
] == POWER_SUPPLY_PROP_USB_TYPE
) &&
1002 (!desc
->usb_types
|| !desc
->num_usb_types
))
1003 return ERR_PTR(-EINVAL
);
1006 psy
= kzalloc(sizeof(*psy
), GFP_KERNEL
);
1008 return ERR_PTR(-ENOMEM
);
1012 device_initialize(dev
);
1014 dev
->class = power_supply_class
;
1015 dev
->type
= &power_supply_dev_type
;
1016 dev
->parent
= parent
;
1017 dev
->release
= power_supply_dev_release
;
1018 dev_set_drvdata(dev
, psy
);
1021 dev
->groups
= cfg
->attr_grp
;
1022 psy
->drv_data
= cfg
->drv_data
;
1024 cfg
->fwnode
? to_of_node(cfg
->fwnode
) : cfg
->of_node
;
1025 psy
->supplied_to
= cfg
->supplied_to
;
1026 psy
->num_supplicants
= cfg
->num_supplicants
;
1029 rc
= dev_set_name(dev
, "%s", desc
->name
);
1031 goto dev_set_name_failed
;
1033 INIT_WORK(&psy
->changed_work
, power_supply_changed_work
);
1034 INIT_DELAYED_WORK(&psy
->deferred_register_work
,
1035 power_supply_deferred_register_work
);
1037 rc
= power_supply_check_supplies(psy
);
1039 dev_info(dev
, "Not all required supplies found, defer probe\n");
1040 goto check_supplies_failed
;
1043 spin_lock_init(&psy
->changed_lock
);
1044 rc
= device_init_wakeup(dev
, ws
);
1046 goto wakeup_init_failed
;
1048 rc
= device_add(dev
);
1050 goto device_add_failed
;
1052 rc
= psy_register_thermal(psy
);
1054 goto register_thermal_failed
;
1056 rc
= psy_register_cooler(psy
);
1058 goto register_cooler_failed
;
1060 rc
= power_supply_create_triggers(psy
);
1062 goto create_triggers_failed
;
1065 * Update use_cnt after any uevents (most notably from device_add()).
1066 * We are here still during driver's probe but
1067 * the power_supply_uevent() calls back driver's get_property
1069 * 1. Driver did not assigned the returned struct power_supply,
1070 * 2. Driver could not finish initialization (anything in its probe
1071 * after calling power_supply_register()).
1073 atomic_inc(&psy
->use_cnt
);
1074 psy
->initialized
= true;
1076 queue_delayed_work(system_power_efficient_wq
,
1077 &psy
->deferred_register_work
,
1078 POWER_SUPPLY_DEFERRED_REGISTER_TIME
);
1082 create_triggers_failed
:
1083 psy_unregister_cooler(psy
);
1084 register_cooler_failed
:
1085 psy_unregister_thermal(psy
);
1086 register_thermal_failed
:
1090 check_supplies_failed
:
1091 dev_set_name_failed
:
1097 * power_supply_register() - Register new power supply
1098 * @parent: Device to be a parent of power supply's device, usually
1099 * the device which probe function calls this
1100 * @desc: Description of power supply, must be valid through whole
1101 * lifetime of this power supply
1102 * @cfg: Run-time specific configuration accessed during registering,
1105 * Return: A pointer to newly allocated power_supply on success
1106 * or ERR_PTR otherwise.
1107 * Use power_supply_unregister() on returned power_supply pointer to release
1110 struct power_supply
*__must_check
power_supply_register(struct device
*parent
,
1111 const struct power_supply_desc
*desc
,
1112 const struct power_supply_config
*cfg
)
1114 return __power_supply_register(parent
, desc
, cfg
, true);
1116 EXPORT_SYMBOL_GPL(power_supply_register
);
1119 * power_supply_register_no_ws() - Register new non-waking-source power supply
1120 * @parent: Device to be a parent of power supply's device, usually
1121 * the device which probe function calls this
1122 * @desc: Description of power supply, must be valid through whole
1123 * lifetime of this power supply
1124 * @cfg: Run-time specific configuration accessed during registering,
1127 * Return: A pointer to newly allocated power_supply on success
1128 * or ERR_PTR otherwise.
1129 * Use power_supply_unregister() on returned power_supply pointer to release
1132 struct power_supply
*__must_check
1133 power_supply_register_no_ws(struct device
*parent
,
1134 const struct power_supply_desc
*desc
,
1135 const struct power_supply_config
*cfg
)
1137 return __power_supply_register(parent
, desc
, cfg
, false);
1139 EXPORT_SYMBOL_GPL(power_supply_register_no_ws
);
1141 static void devm_power_supply_release(struct device
*dev
, void *res
)
1143 struct power_supply
**psy
= res
;
1145 power_supply_unregister(*psy
);
1149 * devm_power_supply_register() - Register managed power supply
1150 * @parent: Device to be a parent of power supply's device, usually
1151 * the device which probe function calls this
1152 * @desc: Description of power supply, must be valid through whole
1153 * lifetime of this power supply
1154 * @cfg: Run-time specific configuration accessed during registering,
1157 * Return: A pointer to newly allocated power_supply on success
1158 * or ERR_PTR otherwise.
1159 * The returned power_supply pointer will be automatically unregistered
1162 struct power_supply
*__must_check
1163 devm_power_supply_register(struct device
*parent
,
1164 const struct power_supply_desc
*desc
,
1165 const struct power_supply_config
*cfg
)
1167 struct power_supply
**ptr
, *psy
;
1169 ptr
= devres_alloc(devm_power_supply_release
, sizeof(*ptr
), GFP_KERNEL
);
1172 return ERR_PTR(-ENOMEM
);
1173 psy
= __power_supply_register(parent
, desc
, cfg
, true);
1178 devres_add(parent
, ptr
);
1182 EXPORT_SYMBOL_GPL(devm_power_supply_register
);
1185 * devm_power_supply_register_no_ws() - Register managed non-waking-source power supply
1186 * @parent: Device to be a parent of power supply's device, usually
1187 * the device which probe function calls this
1188 * @desc: Description of power supply, must be valid through whole
1189 * lifetime of this power supply
1190 * @cfg: Run-time specific configuration accessed during registering,
1193 * Return: A pointer to newly allocated power_supply on success
1194 * or ERR_PTR otherwise.
1195 * The returned power_supply pointer will be automatically unregistered
1198 struct power_supply
*__must_check
1199 devm_power_supply_register_no_ws(struct device
*parent
,
1200 const struct power_supply_desc
*desc
,
1201 const struct power_supply_config
*cfg
)
1203 struct power_supply
**ptr
, *psy
;
1205 ptr
= devres_alloc(devm_power_supply_release
, sizeof(*ptr
), GFP_KERNEL
);
1208 return ERR_PTR(-ENOMEM
);
1209 psy
= __power_supply_register(parent
, desc
, cfg
, false);
1214 devres_add(parent
, ptr
);
1218 EXPORT_SYMBOL_GPL(devm_power_supply_register_no_ws
);
1221 * power_supply_unregister() - Remove this power supply from system
1222 * @psy: Pointer to power supply to unregister
1224 * Remove this power supply from the system. The resources of power supply
1225 * will be freed here or on last power_supply_put() call.
1227 void power_supply_unregister(struct power_supply
*psy
)
1229 WARN_ON(atomic_dec_return(&psy
->use_cnt
));
1230 psy
->removing
= true;
1231 cancel_work_sync(&psy
->changed_work
);
1232 cancel_delayed_work_sync(&psy
->deferred_register_work
);
1233 sysfs_remove_link(&psy
->dev
.kobj
, "powers");
1234 power_supply_remove_triggers(psy
);
1235 psy_unregister_cooler(psy
);
1236 psy_unregister_thermal(psy
);
1237 device_init_wakeup(&psy
->dev
, false);
1238 device_unregister(&psy
->dev
);
1240 EXPORT_SYMBOL_GPL(power_supply_unregister
);
1242 void *power_supply_get_drvdata(struct power_supply
*psy
)
1244 return psy
->drv_data
;
1246 EXPORT_SYMBOL_GPL(power_supply_get_drvdata
);
1248 static int __init
power_supply_class_init(void)
1250 power_supply_class
= class_create(THIS_MODULE
, "power_supply");
1252 if (IS_ERR(power_supply_class
))
1253 return PTR_ERR(power_supply_class
);
1255 power_supply_class
->dev_uevent
= power_supply_uevent
;
1256 power_supply_init_attrs(&power_supply_dev_type
);
1261 static void __exit
power_supply_class_exit(void)
1263 class_destroy(power_supply_class
);
1266 subsys_initcall(power_supply_class_init
);
1267 module_exit(power_supply_class_exit
);
1269 MODULE_DESCRIPTION("Universal power supply monitor class");
1270 MODULE_AUTHOR("Ian Molton <spyro@f2s.com>, "
1272 "Anton Vorontsov <cbou@mail.ru>");
1273 MODULE_LICENSE("GPL");