1 // SPDX-License-Identifier: GPL-2.0-only
3 * Universal power supply monitor class
5 * Copyright © 2007 Anton Vorontsov <cbou@mail.ru>
6 * Copyright © 2004 Szabolcs Gyurko
7 * Copyright © 2003 Ian Molton <spyro@f2s.com>
9 * Modified: 2004, Oct Szabolcs Gyurko
12 #include <linux/module.h>
13 #include <linux/types.h>
14 #include <linux/init.h>
15 #include <linux/slab.h>
16 #include <linux/delay.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/property.h>
23 #include <linux/thermal.h>
24 #include "power_supply.h"
26 /* exported for the APM Power driver, APM emulation */
27 struct class *power_supply_class;
28 EXPORT_SYMBOL_GPL(power_supply_class);
30 ATOMIC_NOTIFIER_HEAD(power_supply_notifier);
31 EXPORT_SYMBOL_GPL(power_supply_notifier);
33 static struct device_type power_supply_dev_type;
35 #define POWER_SUPPLY_DEFERRED_REGISTER_TIME msecs_to_jiffies(10)
37 static bool __power_supply_is_supplied_by(struct power_supply *supplier,
38 struct power_supply *supply)
42 if (!supply->supplied_from && !supplier->supplied_to)
45 /* Support both supplied_to and supplied_from modes */
46 if (supply->supplied_from) {
47 if (!supplier->desc->name)
49 for (i = 0; i < supply->num_supplies; i++)
50 if (!strcmp(supplier->desc->name, supply->supplied_from[i]))
53 if (!supply->desc->name)
55 for (i = 0; i < supplier->num_supplicants; i++)
56 if (!strcmp(supplier->supplied_to[i], supply->desc->name))
63 static int __power_supply_changed_work(struct device *dev, void *data)
65 struct power_supply *psy = data;
66 struct power_supply *pst = dev_get_drvdata(dev);
68 if (__power_supply_is_supplied_by(psy, pst)) {
69 if (pst->desc->external_power_changed)
70 pst->desc->external_power_changed(pst);
76 static void power_supply_changed_work(struct work_struct *work)
79 struct power_supply *psy = container_of(work, struct power_supply,
82 dev_dbg(&psy->dev, "%s\n", __func__);
84 spin_lock_irqsave(&psy->changed_lock, flags);
86 * Check 'changed' here to avoid issues due to race between
87 * power_supply_changed() and this routine. In worst case
88 * power_supply_changed() can be called again just before we take above
89 * lock. During the first call of this routine we will mark 'changed' as
90 * false and it will stay false for the next call as well.
92 if (likely(psy->changed)) {
94 spin_unlock_irqrestore(&psy->changed_lock, flags);
95 class_for_each_device(power_supply_class, NULL, psy,
96 __power_supply_changed_work);
97 power_supply_update_leds(psy);
98 atomic_notifier_call_chain(&power_supply_notifier,
99 PSY_EVENT_PROP_CHANGED, psy);
100 kobject_uevent(&psy->dev.kobj, KOBJ_CHANGE);
101 spin_lock_irqsave(&psy->changed_lock, flags);
105 * Hold the wakeup_source until all events are processed.
106 * power_supply_changed() might have called again and have set 'changed'
109 if (likely(!psy->changed))
111 spin_unlock_irqrestore(&psy->changed_lock, flags);
114 void power_supply_changed(struct power_supply *psy)
118 dev_dbg(&psy->dev, "%s\n", __func__);
120 spin_lock_irqsave(&psy->changed_lock, flags);
122 pm_stay_awake(&psy->dev);
123 spin_unlock_irqrestore(&psy->changed_lock, flags);
124 schedule_work(&psy->changed_work);
126 EXPORT_SYMBOL_GPL(power_supply_changed);
129 * Notify that power supply was registered after parent finished the probing.
131 * Often power supply is registered from driver's probe function. However
132 * calling power_supply_changed() directly from power_supply_register()
133 * would lead to execution of get_property() function provided by the driver
134 * too early - before the probe ends.
136 * Avoid that by waiting on parent's mutex.
138 static void power_supply_deferred_register_work(struct work_struct *work)
140 struct power_supply *psy = container_of(work, struct power_supply,
141 deferred_register_work.work);
143 if (psy->dev.parent) {
144 while (!mutex_trylock(&psy->dev.parent->mutex)) {
151 power_supply_changed(psy);
154 mutex_unlock(&psy->dev.parent->mutex);
158 static int __power_supply_populate_supplied_from(struct device *dev,
161 struct power_supply *psy = data;
162 struct power_supply *epsy = dev_get_drvdata(dev);
163 struct device_node *np;
167 np = of_parse_phandle(psy->of_node, "power-supplies", i++);
171 if (np == epsy->of_node) {
172 dev_info(&psy->dev, "%s: Found supply : %s\n",
173 psy->desc->name, epsy->desc->name);
174 psy->supplied_from[i-1] = (char *)epsy->desc->name;
185 static int power_supply_populate_supplied_from(struct power_supply *psy)
189 error = class_for_each_device(power_supply_class, NULL, psy,
190 __power_supply_populate_supplied_from);
192 dev_dbg(&psy->dev, "%s %d\n", __func__, error);
197 static int __power_supply_find_supply_from_node(struct device *dev,
200 struct device_node *np = data;
201 struct power_supply *epsy = dev_get_drvdata(dev);
203 /* returning non-zero breaks out of class_for_each_device loop */
204 if (epsy->of_node == np)
210 static int power_supply_find_supply_from_node(struct device_node *supply_node)
215 * class_for_each_device() either returns its own errors or values
216 * returned by __power_supply_find_supply_from_node().
218 * __power_supply_find_supply_from_node() will return 0 (no match)
221 * We return 0 if class_for_each_device() returned 1, -EPROBE_DEFER if
222 * it returned 0, or error as returned by it.
224 error = class_for_each_device(power_supply_class, NULL, supply_node,
225 __power_supply_find_supply_from_node);
227 return error ? (error == 1 ? 0 : error) : -EPROBE_DEFER;
230 static int power_supply_check_supplies(struct power_supply *psy)
232 struct device_node *np;
235 /* If there is already a list honor it */
236 if (psy->supplied_from && psy->num_supplies > 0)
239 /* No device node found, nothing to do */
246 np = of_parse_phandle(psy->of_node, "power-supplies", cnt++);
250 ret = power_supply_find_supply_from_node(np);
254 dev_dbg(&psy->dev, "Failed to find supply!\n");
259 /* Missing valid "power-supplies" entries */
263 /* All supplies found, allocate char ** array for filling */
264 psy->supplied_from = devm_kzalloc(&psy->dev, sizeof(psy->supplied_from),
266 if (!psy->supplied_from)
269 *psy->supplied_from = devm_kcalloc(&psy->dev,
270 cnt - 1, sizeof(char *),
272 if (!*psy->supplied_from)
275 return power_supply_populate_supplied_from(psy);
278 static int power_supply_check_supplies(struct power_supply *psy)
282 if (!psy->dev.parent)
285 nval = device_property_read_string_array(psy->dev.parent,
286 "supplied-from", NULL, 0);
290 psy->supplied_from = devm_kmalloc_array(&psy->dev, nval,
291 sizeof(char *), GFP_KERNEL);
292 if (!psy->supplied_from)
295 ret = device_property_read_string_array(psy->dev.parent,
296 "supplied-from", (const char **)psy->supplied_from, nval);
300 psy->num_supplies = nval;
306 struct psy_am_i_supplied_data {
307 struct power_supply *psy;
311 static int __power_supply_am_i_supplied(struct device *dev, void *_data)
313 union power_supply_propval ret = {0,};
314 struct power_supply *epsy = dev_get_drvdata(dev);
315 struct psy_am_i_supplied_data *data = _data;
317 if (__power_supply_is_supplied_by(epsy, data->psy)) {
319 if (!epsy->desc->get_property(epsy, POWER_SUPPLY_PROP_ONLINE,
327 int power_supply_am_i_supplied(struct power_supply *psy)
329 struct psy_am_i_supplied_data data = { psy, 0 };
332 error = class_for_each_device(power_supply_class, NULL, &data,
333 __power_supply_am_i_supplied);
335 dev_dbg(&psy->dev, "%s count %u err %d\n", __func__, data.count, error);
342 EXPORT_SYMBOL_GPL(power_supply_am_i_supplied);
344 static int __power_supply_is_system_supplied(struct device *dev, void *data)
346 union power_supply_propval ret = {0,};
347 struct power_supply *psy = dev_get_drvdata(dev);
348 unsigned int *count = data;
351 if (psy->desc->type != POWER_SUPPLY_TYPE_BATTERY)
352 if (!psy->desc->get_property(psy, POWER_SUPPLY_PROP_ONLINE,
359 int power_supply_is_system_supplied(void)
362 unsigned int count = 0;
364 error = class_for_each_device(power_supply_class, NULL, &count,
365 __power_supply_is_system_supplied);
368 * If no power class device was found at all, most probably we are
369 * running on a desktop system, so assume we are on mains power.
376 EXPORT_SYMBOL_GPL(power_supply_is_system_supplied);
378 static int __power_supply_get_supplier_max_current(struct device *dev,
381 union power_supply_propval ret = {0,};
382 struct power_supply *epsy = dev_get_drvdata(dev);
383 struct power_supply *psy = data;
385 if (__power_supply_is_supplied_by(epsy, psy))
386 if (!epsy->desc->get_property(epsy,
387 POWER_SUPPLY_PROP_CURRENT_MAX,
394 int power_supply_set_input_current_limit_from_supplier(struct power_supply *psy)
396 union power_supply_propval val = {0,};
399 if (!psy->desc->set_property)
403 * This function is not intended for use with a supply with multiple
404 * suppliers, we simply pick the first supply to report a non 0
407 curr = class_for_each_device(power_supply_class, NULL, psy,
408 __power_supply_get_supplier_max_current);
410 return (curr == 0) ? -ENODEV : curr;
414 return psy->desc->set_property(psy,
415 POWER_SUPPLY_PROP_INPUT_CURRENT_LIMIT, &val);
417 EXPORT_SYMBOL_GPL(power_supply_set_input_current_limit_from_supplier);
419 int power_supply_set_battery_charged(struct power_supply *psy)
421 if (atomic_read(&psy->use_cnt) >= 0 &&
422 psy->desc->type == POWER_SUPPLY_TYPE_BATTERY &&
423 psy->desc->set_charged) {
424 psy->desc->set_charged(psy);
430 EXPORT_SYMBOL_GPL(power_supply_set_battery_charged);
432 static int power_supply_match_device_by_name(struct device *dev, const void *data)
434 const char *name = data;
435 struct power_supply *psy = dev_get_drvdata(dev);
437 return strcmp(psy->desc->name, name) == 0;
441 * power_supply_get_by_name() - Search for a power supply and returns its ref
442 * @name: Power supply name to fetch
444 * If power supply was found, it increases reference count for the
445 * internal power supply's device. The user should power_supply_put()
448 * Return: On success returns a reference to a power supply with
449 * matching name equals to @name, a NULL otherwise.
451 struct power_supply *power_supply_get_by_name(const char *name)
453 struct power_supply *psy = NULL;
454 struct device *dev = class_find_device(power_supply_class, NULL, name,
455 power_supply_match_device_by_name);
458 psy = dev_get_drvdata(dev);
459 atomic_inc(&psy->use_cnt);
464 EXPORT_SYMBOL_GPL(power_supply_get_by_name);
467 * power_supply_put() - Drop reference obtained with power_supply_get_by_name
468 * @psy: Reference to put
470 * The reference to power supply should be put before unregistering
473 void power_supply_put(struct power_supply *psy)
477 atomic_dec(&psy->use_cnt);
478 put_device(&psy->dev);
480 EXPORT_SYMBOL_GPL(power_supply_put);
483 static int power_supply_match_device_node(struct device *dev, const void *data)
485 return dev->parent && dev->parent->of_node == data;
489 * power_supply_get_by_phandle() - Search for a power supply and returns its ref
490 * @np: Pointer to device node holding phandle property
491 * @property: Name of property holding a power supply name
493 * If power supply was found, it increases reference count for the
494 * internal power supply's device. The user should power_supply_put()
497 * Return: On success returns a reference to a power supply with
498 * matching name equals to value under @property, NULL or ERR_PTR otherwise.
500 struct power_supply *power_supply_get_by_phandle(struct device_node *np,
501 const char *property)
503 struct device_node *power_supply_np;
504 struct power_supply *psy = NULL;
507 power_supply_np = of_parse_phandle(np, property, 0);
508 if (!power_supply_np)
509 return ERR_PTR(-ENODEV);
511 dev = class_find_device(power_supply_class, NULL, power_supply_np,
512 power_supply_match_device_node);
514 of_node_put(power_supply_np);
517 psy = dev_get_drvdata(dev);
518 atomic_inc(&psy->use_cnt);
523 EXPORT_SYMBOL_GPL(power_supply_get_by_phandle);
525 static void devm_power_supply_put(struct device *dev, void *res)
527 struct power_supply **psy = res;
529 power_supply_put(*psy);
533 * devm_power_supply_get_by_phandle() - Resource managed version of
534 * power_supply_get_by_phandle()
535 * @dev: Pointer to device holding phandle property
536 * @property: Name of property holding a power supply phandle
538 * Return: On success returns a reference to a power supply with
539 * matching name equals to value under @property, NULL or ERR_PTR otherwise.
541 struct power_supply *devm_power_supply_get_by_phandle(struct device *dev,
542 const char *property)
544 struct power_supply **ptr, *psy;
547 return ERR_PTR(-ENODEV);
549 ptr = devres_alloc(devm_power_supply_put, sizeof(*ptr), GFP_KERNEL);
551 return ERR_PTR(-ENOMEM);
553 psy = power_supply_get_by_phandle(dev->of_node, property);
554 if (IS_ERR_OR_NULL(psy)) {
558 devres_add(dev, ptr);
562 EXPORT_SYMBOL_GPL(devm_power_supply_get_by_phandle);
563 #endif /* CONFIG_OF */
565 int power_supply_get_battery_info(struct power_supply *psy,
566 struct power_supply_battery_info *info)
568 struct power_supply_resistance_temp_table *resist_table;
569 struct device_node *battery_np;
574 info->energy_full_design_uwh = -EINVAL;
575 info->charge_full_design_uah = -EINVAL;
576 info->voltage_min_design_uv = -EINVAL;
577 info->voltage_max_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;
583 info->resist_table = NULL;
585 for (index = 0; index < POWER_SUPPLY_OCV_TEMP_MAX; index++) {
586 info->ocv_table[index] = NULL;
587 info->ocv_temp[index] = -EINVAL;
588 info->ocv_table_size[index] = -EINVAL;
592 dev_warn(&psy->dev, "%s currently only supports devicetree\n",
597 battery_np = of_parse_phandle(psy->of_node, "monitored-battery", 0);
601 err = of_property_read_string(battery_np, "compatible", &value);
605 if (strcmp("simple-battery", value)) {
610 /* The property and field names below must correspond to elements
611 * in enum power_supply_property. For reasoning, see
612 * Documentation/power/power_supply_class.rst.
615 of_property_read_u32(battery_np, "energy-full-design-microwatt-hours",
616 &info->energy_full_design_uwh);
617 of_property_read_u32(battery_np, "charge-full-design-microamp-hours",
618 &info->charge_full_design_uah);
619 of_property_read_u32(battery_np, "voltage-min-design-microvolt",
620 &info->voltage_min_design_uv);
621 of_property_read_u32(battery_np, "voltage-max-design-microvolt",
622 &info->voltage_max_design_uv);
623 of_property_read_u32(battery_np, "trickle-charge-current-microamp",
624 &info->tricklecharge_current_ua);
625 of_property_read_u32(battery_np, "precharge-current-microamp",
626 &info->precharge_current_ua);
627 of_property_read_u32(battery_np, "precharge-upper-limit-microvolt",
628 &info->precharge_voltage_max_uv);
629 of_property_read_u32(battery_np, "charge-term-current-microamp",
630 &info->charge_term_current_ua);
631 of_property_read_u32(battery_np, "re-charge-voltage-microvolt",
632 &info->charge_restart_voltage_uv);
633 of_property_read_u32(battery_np, "over-voltage-threshold-microvolt",
634 &info->overvoltage_limit_uv);
635 of_property_read_u32(battery_np, "constant-charge-current-max-microamp",
636 &info->constant_charge_current_max_ua);
637 of_property_read_u32(battery_np, "constant-charge-voltage-max-microvolt",
638 &info->constant_charge_voltage_max_uv);
639 of_property_read_u32(battery_np, "factory-internal-resistance-micro-ohms",
640 &info->factory_internal_resistance_uohm);
642 len = of_property_count_u32_elems(battery_np, "ocv-capacity-celsius");
643 if (len < 0 && len != -EINVAL) {
646 } else if (len > POWER_SUPPLY_OCV_TEMP_MAX) {
647 dev_err(&psy->dev, "Too many temperature values\n");
650 } else if (len > 0) {
651 of_property_read_u32_array(battery_np, "ocv-capacity-celsius",
652 info->ocv_temp, len);
655 for (index = 0; index < len; index++) {
656 struct power_supply_battery_ocv_table *table;
658 int i, tab_len, size;
660 propname = kasprintf(GFP_KERNEL, "ocv-capacity-table-%d", index);
661 list = of_get_property(battery_np, propname, &size);
662 if (!list || !size) {
663 dev_err(&psy->dev, "failed to get %s\n", propname);
665 power_supply_put_battery_info(psy, info);
671 tab_len = size / (2 * sizeof(__be32));
672 info->ocv_table_size[index] = tab_len;
674 table = info->ocv_table[index] =
675 devm_kcalloc(&psy->dev, tab_len, sizeof(*table), GFP_KERNEL);
676 if (!info->ocv_table[index]) {
677 power_supply_put_battery_info(psy, info);
682 for (i = 0; i < tab_len; i++) {
683 table[i].ocv = be32_to_cpu(*list);
685 table[i].capacity = be32_to_cpu(*list);
690 list = of_get_property(battery_np, "resistance-temp-table", &len);
694 info->resist_table_size = len / (2 * sizeof(__be32));
695 resist_table = info->resist_table = devm_kcalloc(&psy->dev,
696 info->resist_table_size,
697 sizeof(*resist_table),
699 if (!info->resist_table) {
700 power_supply_put_battery_info(psy, info);
705 for (index = 0; index < info->resist_table_size; index++) {
706 resist_table[index].temp = be32_to_cpu(*list++);
707 resist_table[index].resistance = be32_to_cpu(*list++);
711 of_node_put(battery_np);
714 EXPORT_SYMBOL_GPL(power_supply_get_battery_info);
716 void power_supply_put_battery_info(struct power_supply *psy,
717 struct power_supply_battery_info *info)
721 for (i = 0; i < POWER_SUPPLY_OCV_TEMP_MAX; i++) {
722 if (info->ocv_table[i])
723 devm_kfree(&psy->dev, info->ocv_table[i]);
726 if (info->resist_table)
727 devm_kfree(&psy->dev, info->resist_table);
729 EXPORT_SYMBOL_GPL(power_supply_put_battery_info);
732 * power_supply_temp2resist_simple() - find the battery internal resistance
734 * @table: Pointer to battery resistance temperature table
735 * @table_len: The table length
736 * @temp: Current temperature
738 * This helper function is used to look up battery internal resistance percent
739 * according to current temperature value from the resistance temperature table,
740 * and the table must be ordered descending. Then the actual battery internal
741 * resistance = the ideal battery internal resistance * percent / 100.
743 * Return: the battery internal resistance percent
745 int power_supply_temp2resist_simple(struct power_supply_resistance_temp_table *table,
746 int table_len, int temp)
750 for (i = 0; i < table_len; i++)
751 if (temp > table[i].temp)
754 if (i > 0 && i < table_len) {
757 tmp = (table[i - 1].resistance - table[i].resistance) *
758 (temp - table[i].temp);
759 tmp /= table[i - 1].temp - table[i].temp;
760 resist = tmp + table[i].resistance;
762 resist = table[0].resistance;
764 resist = table[table_len - 1].resistance;
769 EXPORT_SYMBOL_GPL(power_supply_temp2resist_simple);
772 * power_supply_ocv2cap_simple() - find the battery capacity
773 * @table: Pointer to battery OCV lookup table
774 * @table_len: OCV table length
775 * @ocv: Current OCV value
777 * This helper function is used to look up battery capacity according to
778 * current OCV value from one OCV table, and the OCV table must be ordered
781 * Return: the battery capacity.
783 int power_supply_ocv2cap_simple(struct power_supply_battery_ocv_table *table,
784 int table_len, int ocv)
788 for (i = 0; i < table_len; i++)
789 if (ocv > table[i].ocv)
792 if (i > 0 && i < table_len) {
793 tmp = (table[i - 1].capacity - table[i].capacity) *
794 (ocv - table[i].ocv);
795 tmp /= table[i - 1].ocv - table[i].ocv;
796 cap = tmp + table[i].capacity;
798 cap = table[0].capacity;
800 cap = table[table_len - 1].capacity;
805 EXPORT_SYMBOL_GPL(power_supply_ocv2cap_simple);
807 struct power_supply_battery_ocv_table *
808 power_supply_find_ocv2cap_table(struct power_supply_battery_info *info,
809 int temp, int *table_len)
811 int best_temp_diff = INT_MAX, temp_diff;
812 u8 i, best_index = 0;
814 if (!info->ocv_table[0])
817 for (i = 0; i < POWER_SUPPLY_OCV_TEMP_MAX; i++) {
818 temp_diff = abs(info->ocv_temp[i] - temp);
820 if (temp_diff < best_temp_diff) {
821 best_temp_diff = temp_diff;
826 *table_len = info->ocv_table_size[best_index];
827 return info->ocv_table[best_index];
829 EXPORT_SYMBOL_GPL(power_supply_find_ocv2cap_table);
831 int power_supply_batinfo_ocv2cap(struct power_supply_battery_info *info,
834 struct power_supply_battery_ocv_table *table;
837 table = power_supply_find_ocv2cap_table(info, temp, &table_len);
841 return power_supply_ocv2cap_simple(table, table_len, ocv);
843 EXPORT_SYMBOL_GPL(power_supply_batinfo_ocv2cap);
845 int power_supply_get_property(struct power_supply *psy,
846 enum power_supply_property psp,
847 union power_supply_propval *val)
849 if (atomic_read(&psy->use_cnt) <= 0) {
850 if (!psy->initialized)
855 return psy->desc->get_property(psy, psp, val);
857 EXPORT_SYMBOL_GPL(power_supply_get_property);
859 int power_supply_set_property(struct power_supply *psy,
860 enum power_supply_property psp,
861 const union power_supply_propval *val)
863 if (atomic_read(&psy->use_cnt) <= 0 || !psy->desc->set_property)
866 return psy->desc->set_property(psy, psp, val);
868 EXPORT_SYMBOL_GPL(power_supply_set_property);
870 int power_supply_property_is_writeable(struct power_supply *psy,
871 enum power_supply_property psp)
873 if (atomic_read(&psy->use_cnt) <= 0 ||
874 !psy->desc->property_is_writeable)
877 return psy->desc->property_is_writeable(psy, psp);
879 EXPORT_SYMBOL_GPL(power_supply_property_is_writeable);
881 void power_supply_external_power_changed(struct power_supply *psy)
883 if (atomic_read(&psy->use_cnt) <= 0 ||
884 !psy->desc->external_power_changed)
887 psy->desc->external_power_changed(psy);
889 EXPORT_SYMBOL_GPL(power_supply_external_power_changed);
891 int power_supply_powers(struct power_supply *psy, struct device *dev)
893 return sysfs_create_link(&psy->dev.kobj, &dev->kobj, "powers");
895 EXPORT_SYMBOL_GPL(power_supply_powers);
897 static void power_supply_dev_release(struct device *dev)
899 struct power_supply *psy = to_power_supply(dev);
900 dev_dbg(dev, "%s\n", __func__);
904 int power_supply_reg_notifier(struct notifier_block *nb)
906 return atomic_notifier_chain_register(&power_supply_notifier, nb);
908 EXPORT_SYMBOL_GPL(power_supply_reg_notifier);
910 void power_supply_unreg_notifier(struct notifier_block *nb)
912 atomic_notifier_chain_unregister(&power_supply_notifier, nb);
914 EXPORT_SYMBOL_GPL(power_supply_unreg_notifier);
916 #ifdef CONFIG_THERMAL
917 static int power_supply_read_temp(struct thermal_zone_device *tzd,
920 struct power_supply *psy;
921 union power_supply_propval val;
924 WARN_ON(tzd == NULL);
926 ret = power_supply_get_property(psy, POWER_SUPPLY_PROP_TEMP, &val);
930 /* Convert tenths of degree Celsius to milli degree Celsius. */
931 *temp = val.intval * 100;
936 static struct thermal_zone_device_ops psy_tzd_ops = {
937 .get_temp = power_supply_read_temp,
940 static int psy_register_thermal(struct power_supply *psy)
944 if (psy->desc->no_thermal)
947 /* Register battery zone device psy reports temperature */
948 for (i = 0; i < psy->desc->num_properties; i++) {
949 if (psy->desc->properties[i] == POWER_SUPPLY_PROP_TEMP) {
950 psy->tzd = thermal_zone_device_register(psy->desc->name,
951 0, 0, psy, &psy_tzd_ops, NULL, 0, 0);
952 if (IS_ERR(psy->tzd))
953 return PTR_ERR(psy->tzd);
954 ret = thermal_zone_device_enable(psy->tzd);
956 thermal_zone_device_unregister(psy->tzd);
963 static void psy_unregister_thermal(struct power_supply *psy)
965 if (IS_ERR_OR_NULL(psy->tzd))
967 thermal_zone_device_unregister(psy->tzd);
970 /* thermal cooling device callbacks */
971 static int ps_get_max_charge_cntl_limit(struct thermal_cooling_device *tcd,
972 unsigned long *state)
974 struct power_supply *psy;
975 union power_supply_propval val;
979 ret = power_supply_get_property(psy,
980 POWER_SUPPLY_PROP_CHARGE_CONTROL_LIMIT_MAX, &val);
989 static int ps_get_cur_charge_cntl_limit(struct thermal_cooling_device *tcd,
990 unsigned long *state)
992 struct power_supply *psy;
993 union power_supply_propval val;
997 ret = power_supply_get_property(psy,
998 POWER_SUPPLY_PROP_CHARGE_CONTROL_LIMIT, &val);
1002 *state = val.intval;
1007 static int ps_set_cur_charge_cntl_limit(struct thermal_cooling_device *tcd,
1008 unsigned long state)
1010 struct power_supply *psy;
1011 union power_supply_propval val;
1016 ret = psy->desc->set_property(psy,
1017 POWER_SUPPLY_PROP_CHARGE_CONTROL_LIMIT, &val);
1022 static const struct thermal_cooling_device_ops psy_tcd_ops = {
1023 .get_max_state = ps_get_max_charge_cntl_limit,
1024 .get_cur_state = ps_get_cur_charge_cntl_limit,
1025 .set_cur_state = ps_set_cur_charge_cntl_limit,
1028 static int psy_register_cooler(struct power_supply *psy)
1032 /* Register for cooling device if psy can control charging */
1033 for (i = 0; i < psy->desc->num_properties; i++) {
1034 if (psy->desc->properties[i] ==
1035 POWER_SUPPLY_PROP_CHARGE_CONTROL_LIMIT) {
1036 psy->tcd = thermal_cooling_device_register(
1037 (char *)psy->desc->name,
1039 return PTR_ERR_OR_ZERO(psy->tcd);
1045 static void psy_unregister_cooler(struct power_supply *psy)
1047 if (IS_ERR_OR_NULL(psy->tcd))
1049 thermal_cooling_device_unregister(psy->tcd);
1052 static int psy_register_thermal(struct power_supply *psy)
1057 static void psy_unregister_thermal(struct power_supply *psy)
1061 static int psy_register_cooler(struct power_supply *psy)
1066 static void psy_unregister_cooler(struct power_supply *psy)
1071 static struct power_supply *__must_check
1072 __power_supply_register(struct device *parent,
1073 const struct power_supply_desc *desc,
1074 const struct power_supply_config *cfg,
1078 struct power_supply *psy;
1082 pr_warn("%s: Expected proper parent device for '%s'\n",
1083 __func__, desc->name);
1085 if (!desc || !desc->name || !desc->properties || !desc->num_properties)
1086 return ERR_PTR(-EINVAL);
1088 for (i = 0; i < desc->num_properties; ++i) {
1089 if ((desc->properties[i] == POWER_SUPPLY_PROP_USB_TYPE) &&
1090 (!desc->usb_types || !desc->num_usb_types))
1091 return ERR_PTR(-EINVAL);
1094 psy = kzalloc(sizeof(*psy), GFP_KERNEL);
1096 return ERR_PTR(-ENOMEM);
1100 device_initialize(dev);
1102 dev->class = power_supply_class;
1103 dev->type = &power_supply_dev_type;
1104 dev->parent = parent;
1105 dev->release = power_supply_dev_release;
1106 dev_set_drvdata(dev, psy);
1109 dev->groups = cfg->attr_grp;
1110 psy->drv_data = cfg->drv_data;
1112 cfg->fwnode ? to_of_node(cfg->fwnode) : cfg->of_node;
1113 psy->supplied_to = cfg->supplied_to;
1114 psy->num_supplicants = cfg->num_supplicants;
1117 rc = dev_set_name(dev, "%s", desc->name);
1119 goto dev_set_name_failed;
1121 INIT_WORK(&psy->changed_work, power_supply_changed_work);
1122 INIT_DELAYED_WORK(&psy->deferred_register_work,
1123 power_supply_deferred_register_work);
1125 rc = power_supply_check_supplies(psy);
1127 dev_info(dev, "Not all required supplies found, defer probe\n");
1128 goto check_supplies_failed;
1131 spin_lock_init(&psy->changed_lock);
1132 rc = device_add(dev);
1134 goto device_add_failed;
1136 rc = device_init_wakeup(dev, ws);
1138 goto wakeup_init_failed;
1140 rc = psy_register_thermal(psy);
1142 goto register_thermal_failed;
1144 rc = psy_register_cooler(psy);
1146 goto register_cooler_failed;
1148 rc = power_supply_create_triggers(psy);
1150 goto create_triggers_failed;
1152 rc = power_supply_add_hwmon_sysfs(psy);
1154 goto add_hwmon_sysfs_failed;
1157 * Update use_cnt after any uevents (most notably from device_add()).
1158 * We are here still during driver's probe but
1159 * the power_supply_uevent() calls back driver's get_property
1161 * 1. Driver did not assigned the returned struct power_supply,
1162 * 2. Driver could not finish initialization (anything in its probe
1163 * after calling power_supply_register()).
1165 atomic_inc(&psy->use_cnt);
1166 psy->initialized = true;
1168 queue_delayed_work(system_power_efficient_wq,
1169 &psy->deferred_register_work,
1170 POWER_SUPPLY_DEFERRED_REGISTER_TIME);
1174 add_hwmon_sysfs_failed:
1175 power_supply_remove_triggers(psy);
1176 create_triggers_failed:
1177 psy_unregister_cooler(psy);
1178 register_cooler_failed:
1179 psy_unregister_thermal(psy);
1180 register_thermal_failed:
1184 check_supplies_failed:
1185 dev_set_name_failed:
1191 * power_supply_register() - Register new power supply
1192 * @parent: Device to be a parent of power supply's device, usually
1193 * the device which probe function calls this
1194 * @desc: Description of power supply, must be valid through whole
1195 * lifetime of this power supply
1196 * @cfg: Run-time specific configuration accessed during registering,
1199 * Return: A pointer to newly allocated power_supply on success
1200 * or ERR_PTR otherwise.
1201 * Use power_supply_unregister() on returned power_supply pointer to release
1204 struct power_supply *__must_check power_supply_register(struct device *parent,
1205 const struct power_supply_desc *desc,
1206 const struct power_supply_config *cfg)
1208 return __power_supply_register(parent, desc, cfg, true);
1210 EXPORT_SYMBOL_GPL(power_supply_register);
1213 * power_supply_register_no_ws() - Register new non-waking-source power supply
1214 * @parent: Device to be a parent of power supply's device, usually
1215 * the device which probe function calls this
1216 * @desc: Description of power supply, must be valid through whole
1217 * lifetime of this power supply
1218 * @cfg: Run-time specific configuration accessed during registering,
1221 * Return: A pointer to newly allocated power_supply on success
1222 * or ERR_PTR otherwise.
1223 * Use power_supply_unregister() on returned power_supply pointer to release
1226 struct power_supply *__must_check
1227 power_supply_register_no_ws(struct device *parent,
1228 const struct power_supply_desc *desc,
1229 const struct power_supply_config *cfg)
1231 return __power_supply_register(parent, desc, cfg, false);
1233 EXPORT_SYMBOL_GPL(power_supply_register_no_ws);
1235 static void devm_power_supply_release(struct device *dev, void *res)
1237 struct power_supply **psy = res;
1239 power_supply_unregister(*psy);
1243 * devm_power_supply_register() - Register managed power supply
1244 * @parent: Device to be a parent of power supply's device, usually
1245 * the device which probe function calls this
1246 * @desc: Description of power supply, must be valid through whole
1247 * lifetime of this power supply
1248 * @cfg: Run-time specific configuration accessed during registering,
1251 * Return: A pointer to newly allocated power_supply on success
1252 * or ERR_PTR otherwise.
1253 * The returned power_supply pointer will be automatically unregistered
1256 struct power_supply *__must_check
1257 devm_power_supply_register(struct device *parent,
1258 const struct power_supply_desc *desc,
1259 const struct power_supply_config *cfg)
1261 struct power_supply **ptr, *psy;
1263 ptr = devres_alloc(devm_power_supply_release, sizeof(*ptr), GFP_KERNEL);
1266 return ERR_PTR(-ENOMEM);
1267 psy = __power_supply_register(parent, desc, cfg, true);
1272 devres_add(parent, ptr);
1276 EXPORT_SYMBOL_GPL(devm_power_supply_register);
1279 * devm_power_supply_register_no_ws() - Register managed non-waking-source power supply
1280 * @parent: Device to be a parent of power supply's device, usually
1281 * the device which probe function calls this
1282 * @desc: Description of power supply, must be valid through whole
1283 * lifetime of this power supply
1284 * @cfg: Run-time specific configuration accessed during registering,
1287 * Return: A pointer to newly allocated power_supply on success
1288 * or ERR_PTR otherwise.
1289 * The returned power_supply pointer will be automatically unregistered
1292 struct power_supply *__must_check
1293 devm_power_supply_register_no_ws(struct device *parent,
1294 const struct power_supply_desc *desc,
1295 const struct power_supply_config *cfg)
1297 struct power_supply **ptr, *psy;
1299 ptr = devres_alloc(devm_power_supply_release, sizeof(*ptr), GFP_KERNEL);
1302 return ERR_PTR(-ENOMEM);
1303 psy = __power_supply_register(parent, desc, cfg, false);
1308 devres_add(parent, ptr);
1312 EXPORT_SYMBOL_GPL(devm_power_supply_register_no_ws);
1315 * power_supply_unregister() - Remove this power supply from system
1316 * @psy: Pointer to power supply to unregister
1318 * Remove this power supply from the system. The resources of power supply
1319 * will be freed here or on last power_supply_put() call.
1321 void power_supply_unregister(struct power_supply *psy)
1323 WARN_ON(atomic_dec_return(&psy->use_cnt));
1324 psy->removing = true;
1325 cancel_work_sync(&psy->changed_work);
1326 cancel_delayed_work_sync(&psy->deferred_register_work);
1327 sysfs_remove_link(&psy->dev.kobj, "powers");
1328 power_supply_remove_hwmon_sysfs(psy);
1329 power_supply_remove_triggers(psy);
1330 psy_unregister_cooler(psy);
1331 psy_unregister_thermal(psy);
1332 device_init_wakeup(&psy->dev, false);
1333 device_unregister(&psy->dev);
1335 EXPORT_SYMBOL_GPL(power_supply_unregister);
1337 void *power_supply_get_drvdata(struct power_supply *psy)
1339 return psy->drv_data;
1341 EXPORT_SYMBOL_GPL(power_supply_get_drvdata);
1343 static int __init power_supply_class_init(void)
1345 power_supply_class = class_create(THIS_MODULE, "power_supply");
1347 if (IS_ERR(power_supply_class))
1348 return PTR_ERR(power_supply_class);
1350 power_supply_class->dev_uevent = power_supply_uevent;
1351 power_supply_init_attrs(&power_supply_dev_type);
1356 static void __exit power_supply_class_exit(void)
1358 class_destroy(power_supply_class);
1361 subsys_initcall(power_supply_class_init);
1362 module_exit(power_supply_class_exit);
1364 MODULE_DESCRIPTION("Universal power supply monitor class");
1365 MODULE_AUTHOR("Ian Molton <spyro@f2s.com>, "
1367 "Anton Vorontsov <cbou@mail.ru>");
1368 MODULE_LICENSE("GPL");