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 <linux/fixp-arith.h>
25 #include "power_supply.h"
26 #include "samsung-sdi-battery.h"
28 /* exported for the APM Power driver, APM emulation */
29 struct class *power_supply_class;
30 EXPORT_SYMBOL_GPL(power_supply_class);
32 ATOMIC_NOTIFIER_HEAD(power_supply_notifier);
33 EXPORT_SYMBOL_GPL(power_supply_notifier);
35 static struct device_type power_supply_dev_type;
37 #define POWER_SUPPLY_DEFERRED_REGISTER_TIME msecs_to_jiffies(10)
39 static bool __power_supply_is_supplied_by(struct power_supply *supplier,
40 struct power_supply *supply)
44 if (!supply->supplied_from && !supplier->supplied_to)
47 /* Support both supplied_to and supplied_from modes */
48 if (supply->supplied_from) {
49 if (!supplier->desc->name)
51 for (i = 0; i < supply->num_supplies; i++)
52 if (!strcmp(supplier->desc->name, supply->supplied_from[i]))
55 if (!supply->desc->name)
57 for (i = 0; i < supplier->num_supplicants; i++)
58 if (!strcmp(supplier->supplied_to[i], supply->desc->name))
65 static int __power_supply_changed_work(struct device *dev, void *data)
67 struct power_supply *psy = data;
68 struct power_supply *pst = dev_get_drvdata(dev);
70 if (__power_supply_is_supplied_by(psy, pst)) {
71 if (pst->desc->external_power_changed)
72 pst->desc->external_power_changed(pst);
78 static void power_supply_changed_work(struct work_struct *work)
81 struct power_supply *psy = container_of(work, struct power_supply,
84 dev_dbg(&psy->dev, "%s\n", __func__);
86 spin_lock_irqsave(&psy->changed_lock, flags);
88 * Check 'changed' here to avoid issues due to race between
89 * power_supply_changed() and this routine. In worst case
90 * power_supply_changed() can be called again just before we take above
91 * lock. During the first call of this routine we will mark 'changed' as
92 * false and it will stay false for the next call as well.
94 if (likely(psy->changed)) {
96 spin_unlock_irqrestore(&psy->changed_lock, flags);
97 class_for_each_device(power_supply_class, NULL, psy,
98 __power_supply_changed_work);
99 power_supply_update_leds(psy);
100 atomic_notifier_call_chain(&power_supply_notifier,
101 PSY_EVENT_PROP_CHANGED, psy);
102 kobject_uevent(&psy->dev.kobj, KOBJ_CHANGE);
103 spin_lock_irqsave(&psy->changed_lock, flags);
107 * Hold the wakeup_source until all events are processed.
108 * power_supply_changed() might have called again and have set 'changed'
111 if (likely(!psy->changed))
113 spin_unlock_irqrestore(&psy->changed_lock, flags);
116 void power_supply_changed(struct power_supply *psy)
120 dev_dbg(&psy->dev, "%s\n", __func__);
122 spin_lock_irqsave(&psy->changed_lock, flags);
124 pm_stay_awake(&psy->dev);
125 spin_unlock_irqrestore(&psy->changed_lock, flags);
126 schedule_work(&psy->changed_work);
128 EXPORT_SYMBOL_GPL(power_supply_changed);
131 * Notify that power supply was registered after parent finished the probing.
133 * Often power supply is registered from driver's probe function. However
134 * calling power_supply_changed() directly from power_supply_register()
135 * would lead to execution of get_property() function provided by the driver
136 * too early - before the probe ends.
138 * Avoid that by waiting on parent's mutex.
140 static void power_supply_deferred_register_work(struct work_struct *work)
142 struct power_supply *psy = container_of(work, struct power_supply,
143 deferred_register_work.work);
145 if (psy->dev.parent) {
146 while (!mutex_trylock(&psy->dev.parent->mutex)) {
153 power_supply_changed(psy);
156 mutex_unlock(&psy->dev.parent->mutex);
160 static int __power_supply_populate_supplied_from(struct device *dev,
163 struct power_supply *psy = data;
164 struct power_supply *epsy = dev_get_drvdata(dev);
165 struct device_node *np;
169 np = of_parse_phandle(psy->of_node, "power-supplies", i++);
173 if (np == epsy->of_node) {
174 dev_dbg(&psy->dev, "%s: Found supply : %s\n",
175 psy->desc->name, epsy->desc->name);
176 psy->supplied_from[i-1] = (char *)epsy->desc->name;
187 static int power_supply_populate_supplied_from(struct power_supply *psy)
191 error = class_for_each_device(power_supply_class, NULL, psy,
192 __power_supply_populate_supplied_from);
194 dev_dbg(&psy->dev, "%s %d\n", __func__, error);
199 static int __power_supply_find_supply_from_node(struct device *dev,
202 struct device_node *np = data;
203 struct power_supply *epsy = dev_get_drvdata(dev);
205 /* returning non-zero breaks out of class_for_each_device loop */
206 if (epsy->of_node == np)
212 static int power_supply_find_supply_from_node(struct device_node *supply_node)
217 * class_for_each_device() either returns its own errors or values
218 * returned by __power_supply_find_supply_from_node().
220 * __power_supply_find_supply_from_node() will return 0 (no match)
223 * We return 0 if class_for_each_device() returned 1, -EPROBE_DEFER if
224 * it returned 0, or error as returned by it.
226 error = class_for_each_device(power_supply_class, NULL, supply_node,
227 __power_supply_find_supply_from_node);
229 return error ? (error == 1 ? 0 : error) : -EPROBE_DEFER;
232 static int power_supply_check_supplies(struct power_supply *psy)
234 struct device_node *np;
237 /* If there is already a list honor it */
238 if (psy->supplied_from && psy->num_supplies > 0)
241 /* No device node found, nothing to do */
248 np = of_parse_phandle(psy->of_node, "power-supplies", cnt++);
252 ret = power_supply_find_supply_from_node(np);
256 dev_dbg(&psy->dev, "Failed to find supply!\n");
261 /* Missing valid "power-supplies" entries */
265 /* All supplies found, allocate char ** array for filling */
266 psy->supplied_from = devm_kzalloc(&psy->dev, sizeof(psy->supplied_from),
268 if (!psy->supplied_from)
271 *psy->supplied_from = devm_kcalloc(&psy->dev,
272 cnt - 1, sizeof(char *),
274 if (!*psy->supplied_from)
277 return power_supply_populate_supplied_from(psy);
280 static int power_supply_check_supplies(struct power_supply *psy)
284 if (!psy->dev.parent)
287 nval = device_property_string_array_count(psy->dev.parent, "supplied-from");
291 psy->supplied_from = devm_kmalloc_array(&psy->dev, nval,
292 sizeof(char *), GFP_KERNEL);
293 if (!psy->supplied_from)
296 ret = device_property_read_string_array(psy->dev.parent,
297 "supplied-from", (const char **)psy->supplied_from, nval);
301 psy->num_supplies = nval;
307 struct psy_am_i_supplied_data {
308 struct power_supply *psy;
312 static int __power_supply_am_i_supplied(struct device *dev, void *_data)
314 union power_supply_propval ret = {0,};
315 struct power_supply *epsy = dev_get_drvdata(dev);
316 struct psy_am_i_supplied_data *data = _data;
318 if (__power_supply_is_supplied_by(epsy, data->psy)) {
320 if (!epsy->desc->get_property(epsy, POWER_SUPPLY_PROP_ONLINE,
328 int power_supply_am_i_supplied(struct power_supply *psy)
330 struct psy_am_i_supplied_data data = { psy, 0 };
333 error = class_for_each_device(power_supply_class, NULL, &data,
334 __power_supply_am_i_supplied);
336 dev_dbg(&psy->dev, "%s count %u err %d\n", __func__, data.count, error);
343 EXPORT_SYMBOL_GPL(power_supply_am_i_supplied);
345 static int __power_supply_is_system_supplied(struct device *dev, void *data)
347 union power_supply_propval ret = {0,};
348 struct power_supply *psy = dev_get_drvdata(dev);
349 unsigned int *count = data;
352 if (psy->desc->type != POWER_SUPPLY_TYPE_BATTERY)
353 if (!psy->desc->get_property(psy, POWER_SUPPLY_PROP_ONLINE,
360 int power_supply_is_system_supplied(void)
363 unsigned int count = 0;
365 error = class_for_each_device(power_supply_class, NULL, &count,
366 __power_supply_is_system_supplied);
369 * If no power class device was found at all, most probably we are
370 * running on a desktop system, so assume we are on mains power.
377 EXPORT_SYMBOL_GPL(power_supply_is_system_supplied);
379 struct psy_get_supplier_prop_data {
380 struct power_supply *psy;
381 enum power_supply_property psp;
382 union power_supply_propval *val;
385 static int __power_supply_get_supplier_property(struct device *dev, void *_data)
387 struct power_supply *epsy = dev_get_drvdata(dev);
388 struct psy_get_supplier_prop_data *data = _data;
390 if (__power_supply_is_supplied_by(epsy, data->psy))
391 if (!epsy->desc->get_property(epsy, data->psp, data->val))
392 return 1; /* Success */
394 return 0; /* Continue iterating */
397 int power_supply_get_property_from_supplier(struct power_supply *psy,
398 enum power_supply_property psp,
399 union power_supply_propval *val)
401 struct psy_get_supplier_prop_data data = {
409 * This function is not intended for use with a supply with multiple
410 * suppliers, we simply pick the first supply to report the psp.
412 ret = class_for_each_device(power_supply_class, NULL, &data,
413 __power_supply_get_supplier_property);
421 EXPORT_SYMBOL_GPL(power_supply_get_property_from_supplier);
423 int power_supply_set_battery_charged(struct power_supply *psy)
425 if (atomic_read(&psy->use_cnt) >= 0 &&
426 psy->desc->type == POWER_SUPPLY_TYPE_BATTERY &&
427 psy->desc->set_charged) {
428 psy->desc->set_charged(psy);
434 EXPORT_SYMBOL_GPL(power_supply_set_battery_charged);
436 static int power_supply_match_device_by_name(struct device *dev, const void *data)
438 const char *name = data;
439 struct power_supply *psy = dev_get_drvdata(dev);
441 return strcmp(psy->desc->name, name) == 0;
445 * power_supply_get_by_name() - Search for a power supply and returns its ref
446 * @name: Power supply name to fetch
448 * If power supply was found, it increases reference count for the
449 * internal power supply's device. The user should power_supply_put()
452 * Return: On success returns a reference to a power supply with
453 * matching name equals to @name, a NULL otherwise.
455 struct power_supply *power_supply_get_by_name(const char *name)
457 struct power_supply *psy = NULL;
458 struct device *dev = class_find_device(power_supply_class, NULL, name,
459 power_supply_match_device_by_name);
462 psy = dev_get_drvdata(dev);
463 atomic_inc(&psy->use_cnt);
468 EXPORT_SYMBOL_GPL(power_supply_get_by_name);
471 * power_supply_put() - Drop reference obtained with power_supply_get_by_name
472 * @psy: Reference to put
474 * The reference to power supply should be put before unregistering
477 void power_supply_put(struct power_supply *psy)
481 atomic_dec(&psy->use_cnt);
482 put_device(&psy->dev);
484 EXPORT_SYMBOL_GPL(power_supply_put);
487 static int power_supply_match_device_node(struct device *dev, const void *data)
489 return dev->parent && dev->parent->of_node == data;
493 * power_supply_get_by_phandle() - Search for a power supply and returns its ref
494 * @np: Pointer to device node holding phandle property
495 * @property: Name of property holding a power supply name
497 * If power supply was found, it increases reference count for the
498 * internal power supply's device. The user should power_supply_put()
501 * Return: On success returns a reference to a power supply with
502 * matching name equals to value under @property, NULL or ERR_PTR otherwise.
504 struct power_supply *power_supply_get_by_phandle(struct device_node *np,
505 const char *property)
507 struct device_node *power_supply_np;
508 struct power_supply *psy = NULL;
511 power_supply_np = of_parse_phandle(np, property, 0);
512 if (!power_supply_np)
513 return ERR_PTR(-ENODEV);
515 dev = class_find_device(power_supply_class, NULL, power_supply_np,
516 power_supply_match_device_node);
518 of_node_put(power_supply_np);
521 psy = dev_get_drvdata(dev);
522 atomic_inc(&psy->use_cnt);
527 EXPORT_SYMBOL_GPL(power_supply_get_by_phandle);
529 static void devm_power_supply_put(struct device *dev, void *res)
531 struct power_supply **psy = res;
533 power_supply_put(*psy);
537 * devm_power_supply_get_by_phandle() - Resource managed version of
538 * power_supply_get_by_phandle()
539 * @dev: Pointer to device holding phandle property
540 * @property: Name of property holding a power supply phandle
542 * Return: On success returns a reference to a power supply with
543 * matching name equals to value under @property, NULL or ERR_PTR otherwise.
545 struct power_supply *devm_power_supply_get_by_phandle(struct device *dev,
546 const char *property)
548 struct power_supply **ptr, *psy;
551 return ERR_PTR(-ENODEV);
553 ptr = devres_alloc(devm_power_supply_put, sizeof(*ptr), GFP_KERNEL);
555 return ERR_PTR(-ENOMEM);
557 psy = power_supply_get_by_phandle(dev->of_node, property);
558 if (IS_ERR_OR_NULL(psy)) {
562 devres_add(dev, ptr);
566 EXPORT_SYMBOL_GPL(devm_power_supply_get_by_phandle);
567 #endif /* CONFIG_OF */
569 int power_supply_get_battery_info(struct power_supply *psy,
570 struct power_supply_battery_info **info_out)
572 struct power_supply_resistance_temp_table *resist_table;
573 struct power_supply_battery_info *info;
574 struct device_node *battery_np = NULL;
575 struct fwnode_reference_args args;
576 struct fwnode_handle *fwnode;
583 battery_np = of_parse_phandle(psy->of_node, "monitored-battery", 0);
587 fwnode = fwnode_handle_get(of_fwnode_handle(battery_np));
589 err = fwnode_property_get_reference_args(
590 dev_fwnode(psy->dev.parent),
591 "monitored-battery", NULL, 0, 0, &args);
595 fwnode = args.fwnode;
598 err = fwnode_property_read_string(fwnode, "compatible", &value);
603 /* Try static batteries first */
604 err = samsung_sdi_battery_get_info(&psy->dev, value, &info);
606 goto out_ret_pointer;
607 else if (err == -ENODEV)
609 * Device does not have a static battery.
610 * Proceed to look for a simple battery.
614 if (strcmp("simple-battery", value)) {
619 info = devm_kzalloc(&psy->dev, sizeof(*info), GFP_KERNEL);
625 info->technology = POWER_SUPPLY_TECHNOLOGY_UNKNOWN;
626 info->energy_full_design_uwh = -EINVAL;
627 info->charge_full_design_uah = -EINVAL;
628 info->voltage_min_design_uv = -EINVAL;
629 info->voltage_max_design_uv = -EINVAL;
630 info->precharge_current_ua = -EINVAL;
631 info->charge_term_current_ua = -EINVAL;
632 info->constant_charge_current_max_ua = -EINVAL;
633 info->constant_charge_voltage_max_uv = -EINVAL;
634 info->tricklecharge_current_ua = -EINVAL;
635 info->precharge_voltage_max_uv = -EINVAL;
636 info->charge_restart_voltage_uv = -EINVAL;
637 info->overvoltage_limit_uv = -EINVAL;
638 info->maintenance_charge = NULL;
639 info->alert_low_temp_charge_current_ua = -EINVAL;
640 info->alert_low_temp_charge_voltage_uv = -EINVAL;
641 info->alert_high_temp_charge_current_ua = -EINVAL;
642 info->alert_high_temp_charge_voltage_uv = -EINVAL;
643 info->temp_ambient_alert_min = INT_MIN;
644 info->temp_ambient_alert_max = INT_MAX;
645 info->temp_alert_min = INT_MIN;
646 info->temp_alert_max = INT_MAX;
647 info->temp_min = INT_MIN;
648 info->temp_max = INT_MAX;
649 info->factory_internal_resistance_uohm = -EINVAL;
650 info->resist_table = NULL;
651 info->bti_resistance_ohm = -EINVAL;
652 info->bti_resistance_tolerance = -EINVAL;
654 for (index = 0; index < POWER_SUPPLY_OCV_TEMP_MAX; index++) {
655 info->ocv_table[index] = NULL;
656 info->ocv_temp[index] = -EINVAL;
657 info->ocv_table_size[index] = -EINVAL;
660 /* The property and field names below must correspond to elements
661 * in enum power_supply_property. For reasoning, see
662 * Documentation/power/power_supply_class.rst.
665 if (!fwnode_property_read_string(fwnode, "device-chemistry", &value)) {
666 if (!strcmp("nickel-cadmium", value))
667 info->technology = POWER_SUPPLY_TECHNOLOGY_NiCd;
668 else if (!strcmp("nickel-metal-hydride", value))
669 info->technology = POWER_SUPPLY_TECHNOLOGY_NiMH;
670 else if (!strcmp("lithium-ion", value))
671 /* Imprecise lithium-ion type */
672 info->technology = POWER_SUPPLY_TECHNOLOGY_LION;
673 else if (!strcmp("lithium-ion-polymer", value))
674 info->technology = POWER_SUPPLY_TECHNOLOGY_LIPO;
675 else if (!strcmp("lithium-ion-iron-phosphate", value))
676 info->technology = POWER_SUPPLY_TECHNOLOGY_LiFe;
677 else if (!strcmp("lithium-ion-manganese-oxide", value))
678 info->technology = POWER_SUPPLY_TECHNOLOGY_LiMn;
680 dev_warn(&psy->dev, "%s unknown battery type\n", value);
683 fwnode_property_read_u32(fwnode, "energy-full-design-microwatt-hours",
684 &info->energy_full_design_uwh);
685 fwnode_property_read_u32(fwnode, "charge-full-design-microamp-hours",
686 &info->charge_full_design_uah);
687 fwnode_property_read_u32(fwnode, "voltage-min-design-microvolt",
688 &info->voltage_min_design_uv);
689 fwnode_property_read_u32(fwnode, "voltage-max-design-microvolt",
690 &info->voltage_max_design_uv);
691 fwnode_property_read_u32(fwnode, "trickle-charge-current-microamp",
692 &info->tricklecharge_current_ua);
693 fwnode_property_read_u32(fwnode, "precharge-current-microamp",
694 &info->precharge_current_ua);
695 fwnode_property_read_u32(fwnode, "precharge-upper-limit-microvolt",
696 &info->precharge_voltage_max_uv);
697 fwnode_property_read_u32(fwnode, "charge-term-current-microamp",
698 &info->charge_term_current_ua);
699 fwnode_property_read_u32(fwnode, "re-charge-voltage-microvolt",
700 &info->charge_restart_voltage_uv);
701 fwnode_property_read_u32(fwnode, "over-voltage-threshold-microvolt",
702 &info->overvoltage_limit_uv);
703 fwnode_property_read_u32(fwnode, "constant-charge-current-max-microamp",
704 &info->constant_charge_current_max_ua);
705 fwnode_property_read_u32(fwnode, "constant-charge-voltage-max-microvolt",
706 &info->constant_charge_voltage_max_uv);
707 fwnode_property_read_u32(fwnode, "factory-internal-resistance-micro-ohms",
708 &info->factory_internal_resistance_uohm);
710 if (!fwnode_property_read_u32_array(fwnode, "ambient-celsius",
711 min_max, ARRAY_SIZE(min_max))) {
712 info->temp_ambient_alert_min = min_max[0];
713 info->temp_ambient_alert_max = min_max[1];
715 if (!fwnode_property_read_u32_array(fwnode, "alert-celsius",
716 min_max, ARRAY_SIZE(min_max))) {
717 info->temp_alert_min = min_max[0];
718 info->temp_alert_max = min_max[1];
720 if (!fwnode_property_read_u32_array(fwnode, "operating-range-celsius",
721 min_max, ARRAY_SIZE(min_max))) {
722 info->temp_min = min_max[0];
723 info->temp_max = min_max[1];
727 * The below code uses raw of-data parsing to parse
728 * /schemas/types.yaml#/definitions/uint32-matrix
729 * data, so for now this is only support with of.
732 goto out_ret_pointer;
734 len = of_property_count_u32_elems(battery_np, "ocv-capacity-celsius");
735 if (len < 0 && len != -EINVAL) {
738 } else if (len > POWER_SUPPLY_OCV_TEMP_MAX) {
739 dev_err(&psy->dev, "Too many temperature values\n");
742 } else if (len > 0) {
743 of_property_read_u32_array(battery_np, "ocv-capacity-celsius",
744 info->ocv_temp, len);
747 for (index = 0; index < len; index++) {
748 struct power_supply_battery_ocv_table *table;
750 int i, tab_len, size;
752 propname = kasprintf(GFP_KERNEL, "ocv-capacity-table-%d", index);
753 list = of_get_property(battery_np, propname, &size);
754 if (!list || !size) {
755 dev_err(&psy->dev, "failed to get %s\n", propname);
757 power_supply_put_battery_info(psy, info);
763 tab_len = size / (2 * sizeof(__be32));
764 info->ocv_table_size[index] = tab_len;
766 table = info->ocv_table[index] =
767 devm_kcalloc(&psy->dev, tab_len, sizeof(*table), GFP_KERNEL);
768 if (!info->ocv_table[index]) {
769 power_supply_put_battery_info(psy, info);
774 for (i = 0; i < tab_len; i++) {
775 table[i].ocv = be32_to_cpu(*list);
777 table[i].capacity = be32_to_cpu(*list);
782 list = of_get_property(battery_np, "resistance-temp-table", &len);
784 goto out_ret_pointer;
786 info->resist_table_size = len / (2 * sizeof(__be32));
787 resist_table = info->resist_table = devm_kcalloc(&psy->dev,
788 info->resist_table_size,
789 sizeof(*resist_table),
791 if (!info->resist_table) {
792 power_supply_put_battery_info(psy, info);
797 for (index = 0; index < info->resist_table_size; index++) {
798 resist_table[index].temp = be32_to_cpu(*list++);
799 resist_table[index].resistance = be32_to_cpu(*list++);
803 /* Finally return the whole thing */
807 fwnode_handle_put(fwnode);
808 of_node_put(battery_np);
811 EXPORT_SYMBOL_GPL(power_supply_get_battery_info);
813 void power_supply_put_battery_info(struct power_supply *psy,
814 struct power_supply_battery_info *info)
818 for (i = 0; i < POWER_SUPPLY_OCV_TEMP_MAX; i++) {
819 if (info->ocv_table[i])
820 devm_kfree(&psy->dev, info->ocv_table[i]);
823 if (info->resist_table)
824 devm_kfree(&psy->dev, info->resist_table);
826 devm_kfree(&psy->dev, info);
828 EXPORT_SYMBOL_GPL(power_supply_put_battery_info);
831 * power_supply_temp2resist_simple() - find the battery internal resistance
832 * percent from temperature
833 * @table: Pointer to battery resistance temperature table
834 * @table_len: The table length
835 * @temp: Current temperature
837 * This helper function is used to look up battery internal resistance percent
838 * according to current temperature value from the resistance temperature table,
839 * and the table must be ordered descending. Then the actual battery internal
840 * resistance = the ideal battery internal resistance * percent / 100.
842 * Return: the battery internal resistance percent
844 int power_supply_temp2resist_simple(struct power_supply_resistance_temp_table *table,
845 int table_len, int temp)
849 for (i = 0; i < table_len; i++)
850 if (temp > table[i].temp)
853 /* The library function will deal with high == low */
856 else if (i == table_len)
859 high = (low = i) - 1;
861 return fixp_linear_interpolate(table[low].temp,
862 table[low].resistance,
864 table[high].resistance,
867 EXPORT_SYMBOL_GPL(power_supply_temp2resist_simple);
870 * power_supply_vbat2ri() - find the battery internal resistance
871 * from the battery voltage
872 * @info: The battery information container
873 * @table: Pointer to battery resistance temperature table
874 * @vbat_uv: The battery voltage in microvolt
875 * @charging: If we are charging (true) or not (false)
877 * This helper function is used to look up battery internal resistance
878 * according to current battery voltage. Depending on whether the battery
879 * is currently charging or not, different resistance will be returned.
881 * Returns the internal resistance in microohm or negative error code.
883 int power_supply_vbat2ri(struct power_supply_battery_info *info,
884 int vbat_uv, bool charging)
886 struct power_supply_vbat_ri_table *vbat2ri;
891 * If we are charging, and the battery supplies a separate table
892 * for this state, we use that in order to compensate for the
893 * charging voltage. Otherwise we use the main table.
895 if (charging && info->vbat2ri_charging) {
896 vbat2ri = info->vbat2ri_charging;
897 table_len = info->vbat2ri_charging_size;
899 vbat2ri = info->vbat2ri_discharging;
900 table_len = info->vbat2ri_discharging_size;
904 * If no tables are specified, or if we are above the highest voltage in
905 * the voltage table, just return the factory specified internal resistance.
907 if (!vbat2ri || (table_len <= 0) || (vbat_uv > vbat2ri[0].vbat_uv)) {
908 if (charging && (info->factory_internal_resistance_charging_uohm > 0))
909 return info->factory_internal_resistance_charging_uohm;
911 return info->factory_internal_resistance_uohm;
914 /* Break loop at table_len - 1 because that is the highest index */
915 for (i = 0; i < table_len - 1; i++)
916 if (vbat_uv > vbat2ri[i].vbat_uv)
919 /* The library function will deal with high == low */
920 if ((i == 0) || (i == (table_len - 1)))
926 return fixp_linear_interpolate(vbat2ri[low].vbat_uv,
927 vbat2ri[low].ri_uohm,
928 vbat2ri[high].vbat_uv,
929 vbat2ri[high].ri_uohm,
932 EXPORT_SYMBOL_GPL(power_supply_vbat2ri);
934 struct power_supply_maintenance_charge_table *
935 power_supply_get_maintenance_charging_setting(struct power_supply_battery_info *info,
938 if (index >= info->maintenance_charge_size)
940 return &info->maintenance_charge[index];
942 EXPORT_SYMBOL_GPL(power_supply_get_maintenance_charging_setting);
945 * power_supply_ocv2cap_simple() - find the battery capacity
946 * @table: Pointer to battery OCV lookup table
947 * @table_len: OCV table length
948 * @ocv: Current OCV value
950 * This helper function is used to look up battery capacity according to
951 * current OCV value from one OCV table, and the OCV table must be ordered
954 * Return: the battery capacity.
956 int power_supply_ocv2cap_simple(struct power_supply_battery_ocv_table *table,
957 int table_len, int ocv)
961 for (i = 0; i < table_len; i++)
962 if (ocv > table[i].ocv)
965 /* The library function will deal with high == low */
968 else if (i == table_len)
971 high = (low = i) - 1;
973 return fixp_linear_interpolate(table[low].ocv,
976 table[high].capacity,
979 EXPORT_SYMBOL_GPL(power_supply_ocv2cap_simple);
981 struct power_supply_battery_ocv_table *
982 power_supply_find_ocv2cap_table(struct power_supply_battery_info *info,
983 int temp, int *table_len)
985 int best_temp_diff = INT_MAX, temp_diff;
986 u8 i, best_index = 0;
988 if (!info->ocv_table[0])
991 for (i = 0; i < POWER_SUPPLY_OCV_TEMP_MAX; i++) {
992 /* Out of capacity tables */
993 if (!info->ocv_table[i])
996 temp_diff = abs(info->ocv_temp[i] - temp);
998 if (temp_diff < best_temp_diff) {
999 best_temp_diff = temp_diff;
1004 *table_len = info->ocv_table_size[best_index];
1005 return info->ocv_table[best_index];
1007 EXPORT_SYMBOL_GPL(power_supply_find_ocv2cap_table);
1009 int power_supply_batinfo_ocv2cap(struct power_supply_battery_info *info,
1012 struct power_supply_battery_ocv_table *table;
1015 table = power_supply_find_ocv2cap_table(info, temp, &table_len);
1019 return power_supply_ocv2cap_simple(table, table_len, ocv);
1021 EXPORT_SYMBOL_GPL(power_supply_batinfo_ocv2cap);
1023 bool power_supply_battery_bti_in_range(struct power_supply_battery_info *info,
1028 /* Nothing like this can be checked */
1029 if (info->bti_resistance_ohm <= 0)
1032 /* This will be extremely strict and unlikely to work */
1033 if (info->bti_resistance_tolerance <= 0)
1034 return (info->bti_resistance_ohm == resistance);
1036 low = info->bti_resistance_ohm -
1037 (info->bti_resistance_ohm * info->bti_resistance_tolerance) / 100;
1038 high = info->bti_resistance_ohm +
1039 (info->bti_resistance_ohm * info->bti_resistance_tolerance) / 100;
1041 return ((resistance >= low) && (resistance <= high));
1043 EXPORT_SYMBOL_GPL(power_supply_battery_bti_in_range);
1045 int power_supply_get_property(struct power_supply *psy,
1046 enum power_supply_property psp,
1047 union power_supply_propval *val)
1049 if (atomic_read(&psy->use_cnt) <= 0) {
1050 if (!psy->initialized)
1055 return psy->desc->get_property(psy, psp, val);
1057 EXPORT_SYMBOL_GPL(power_supply_get_property);
1059 int power_supply_set_property(struct power_supply *psy,
1060 enum power_supply_property psp,
1061 const union power_supply_propval *val)
1063 if (atomic_read(&psy->use_cnt) <= 0 || !psy->desc->set_property)
1066 return psy->desc->set_property(psy, psp, val);
1068 EXPORT_SYMBOL_GPL(power_supply_set_property);
1070 int power_supply_property_is_writeable(struct power_supply *psy,
1071 enum power_supply_property psp)
1073 if (atomic_read(&psy->use_cnt) <= 0 ||
1074 !psy->desc->property_is_writeable)
1077 return psy->desc->property_is_writeable(psy, psp);
1079 EXPORT_SYMBOL_GPL(power_supply_property_is_writeable);
1081 void power_supply_external_power_changed(struct power_supply *psy)
1083 if (atomic_read(&psy->use_cnt) <= 0 ||
1084 !psy->desc->external_power_changed)
1087 psy->desc->external_power_changed(psy);
1089 EXPORT_SYMBOL_GPL(power_supply_external_power_changed);
1091 int power_supply_powers(struct power_supply *psy, struct device *dev)
1093 return sysfs_create_link(&psy->dev.kobj, &dev->kobj, "powers");
1095 EXPORT_SYMBOL_GPL(power_supply_powers);
1097 static void power_supply_dev_release(struct device *dev)
1099 struct power_supply *psy = to_power_supply(dev);
1100 dev_dbg(dev, "%s\n", __func__);
1104 int power_supply_reg_notifier(struct notifier_block *nb)
1106 return atomic_notifier_chain_register(&power_supply_notifier, nb);
1108 EXPORT_SYMBOL_GPL(power_supply_reg_notifier);
1110 void power_supply_unreg_notifier(struct notifier_block *nb)
1112 atomic_notifier_chain_unregister(&power_supply_notifier, nb);
1114 EXPORT_SYMBOL_GPL(power_supply_unreg_notifier);
1116 static bool psy_has_property(const struct power_supply_desc *psy_desc,
1117 enum power_supply_property psp)
1122 for (i = 0; i < psy_desc->num_properties; i++) {
1123 if (psy_desc->properties[i] == psp) {
1132 #ifdef CONFIG_THERMAL
1133 static int power_supply_read_temp(struct thermal_zone_device *tzd,
1136 struct power_supply *psy;
1137 union power_supply_propval val;
1140 WARN_ON(tzd == NULL);
1142 ret = power_supply_get_property(psy, POWER_SUPPLY_PROP_TEMP, &val);
1146 /* Convert tenths of degree Celsius to milli degree Celsius. */
1147 *temp = val.intval * 100;
1152 static struct thermal_zone_device_ops psy_tzd_ops = {
1153 .get_temp = power_supply_read_temp,
1156 static int psy_register_thermal(struct power_supply *psy)
1160 if (psy->desc->no_thermal)
1163 /* Register battery zone device psy reports temperature */
1164 if (psy_has_property(psy->desc, POWER_SUPPLY_PROP_TEMP)) {
1165 psy->tzd = thermal_zone_device_register(psy->desc->name,
1166 0, 0, psy, &psy_tzd_ops, NULL, 0, 0);
1167 if (IS_ERR(psy->tzd))
1168 return PTR_ERR(psy->tzd);
1169 ret = thermal_zone_device_enable(psy->tzd);
1171 thermal_zone_device_unregister(psy->tzd);
1178 static void psy_unregister_thermal(struct power_supply *psy)
1180 if (IS_ERR_OR_NULL(psy->tzd))
1182 thermal_zone_device_unregister(psy->tzd);
1185 /* thermal cooling device callbacks */
1186 static int ps_get_max_charge_cntl_limit(struct thermal_cooling_device *tcd,
1187 unsigned long *state)
1189 struct power_supply *psy;
1190 union power_supply_propval val;
1194 ret = power_supply_get_property(psy,
1195 POWER_SUPPLY_PROP_CHARGE_CONTROL_LIMIT_MAX, &val);
1199 *state = val.intval;
1204 static int ps_get_cur_charge_cntl_limit(struct thermal_cooling_device *tcd,
1205 unsigned long *state)
1207 struct power_supply *psy;
1208 union power_supply_propval val;
1212 ret = power_supply_get_property(psy,
1213 POWER_SUPPLY_PROP_CHARGE_CONTROL_LIMIT, &val);
1217 *state = val.intval;
1222 static int ps_set_cur_charge_cntl_limit(struct thermal_cooling_device *tcd,
1223 unsigned long state)
1225 struct power_supply *psy;
1226 union power_supply_propval val;
1231 ret = psy->desc->set_property(psy,
1232 POWER_SUPPLY_PROP_CHARGE_CONTROL_LIMIT, &val);
1237 static const struct thermal_cooling_device_ops psy_tcd_ops = {
1238 .get_max_state = ps_get_max_charge_cntl_limit,
1239 .get_cur_state = ps_get_cur_charge_cntl_limit,
1240 .set_cur_state = ps_set_cur_charge_cntl_limit,
1243 static int psy_register_cooler(struct power_supply *psy)
1245 /* Register for cooling device if psy can control charging */
1246 if (psy_has_property(psy->desc, POWER_SUPPLY_PROP_CHARGE_CONTROL_LIMIT)) {
1247 psy->tcd = thermal_cooling_device_register(
1248 (char *)psy->desc->name,
1250 return PTR_ERR_OR_ZERO(psy->tcd);
1256 static void psy_unregister_cooler(struct power_supply *psy)
1258 if (IS_ERR_OR_NULL(psy->tcd))
1260 thermal_cooling_device_unregister(psy->tcd);
1263 static int psy_register_thermal(struct power_supply *psy)
1268 static void psy_unregister_thermal(struct power_supply *psy)
1272 static int psy_register_cooler(struct power_supply *psy)
1277 static void psy_unregister_cooler(struct power_supply *psy)
1282 static struct power_supply *__must_check
1283 __power_supply_register(struct device *parent,
1284 const struct power_supply_desc *desc,
1285 const struct power_supply_config *cfg,
1289 struct power_supply *psy;
1293 pr_warn("%s: Expected proper parent device for '%s'\n",
1294 __func__, desc->name);
1296 if (!desc || !desc->name || !desc->properties || !desc->num_properties)
1297 return ERR_PTR(-EINVAL);
1299 if (psy_has_property(desc, POWER_SUPPLY_PROP_USB_TYPE) &&
1300 (!desc->usb_types || !desc->num_usb_types))
1301 return ERR_PTR(-EINVAL);
1303 psy = kzalloc(sizeof(*psy), GFP_KERNEL);
1305 return ERR_PTR(-ENOMEM);
1309 device_initialize(dev);
1311 dev->class = power_supply_class;
1312 dev->type = &power_supply_dev_type;
1313 dev->parent = parent;
1314 dev->release = power_supply_dev_release;
1315 dev_set_drvdata(dev, psy);
1318 dev->groups = cfg->attr_grp;
1319 psy->drv_data = cfg->drv_data;
1321 cfg->fwnode ? to_of_node(cfg->fwnode) : cfg->of_node;
1322 psy->supplied_to = cfg->supplied_to;
1323 psy->num_supplicants = cfg->num_supplicants;
1326 rc = dev_set_name(dev, "%s", desc->name);
1328 goto dev_set_name_failed;
1330 INIT_WORK(&psy->changed_work, power_supply_changed_work);
1331 INIT_DELAYED_WORK(&psy->deferred_register_work,
1332 power_supply_deferred_register_work);
1334 rc = power_supply_check_supplies(psy);
1336 dev_dbg(dev, "Not all required supplies found, defer probe\n");
1337 goto check_supplies_failed;
1340 spin_lock_init(&psy->changed_lock);
1341 rc = device_add(dev);
1343 goto device_add_failed;
1345 rc = device_init_wakeup(dev, ws);
1347 goto wakeup_init_failed;
1349 rc = psy_register_thermal(psy);
1351 goto register_thermal_failed;
1353 rc = psy_register_cooler(psy);
1355 goto register_cooler_failed;
1357 rc = power_supply_create_triggers(psy);
1359 goto create_triggers_failed;
1361 rc = power_supply_add_hwmon_sysfs(psy);
1363 goto add_hwmon_sysfs_failed;
1366 * Update use_cnt after any uevents (most notably from device_add()).
1367 * We are here still during driver's probe but
1368 * the power_supply_uevent() calls back driver's get_property
1370 * 1. Driver did not assigned the returned struct power_supply,
1371 * 2. Driver could not finish initialization (anything in its probe
1372 * after calling power_supply_register()).
1374 atomic_inc(&psy->use_cnt);
1375 psy->initialized = true;
1377 queue_delayed_work(system_power_efficient_wq,
1378 &psy->deferred_register_work,
1379 POWER_SUPPLY_DEFERRED_REGISTER_TIME);
1383 add_hwmon_sysfs_failed:
1384 power_supply_remove_triggers(psy);
1385 create_triggers_failed:
1386 psy_unregister_cooler(psy);
1387 register_cooler_failed:
1388 psy_unregister_thermal(psy);
1389 register_thermal_failed:
1393 check_supplies_failed:
1394 dev_set_name_failed:
1400 * power_supply_register() - Register new power supply
1401 * @parent: Device to be a parent of power supply's device, usually
1402 * the device which probe function calls this
1403 * @desc: Description of power supply, must be valid through whole
1404 * lifetime of this power supply
1405 * @cfg: Run-time specific configuration accessed during registering,
1408 * Return: A pointer to newly allocated power_supply on success
1409 * or ERR_PTR otherwise.
1410 * Use power_supply_unregister() on returned power_supply pointer to release
1413 struct power_supply *__must_check power_supply_register(struct device *parent,
1414 const struct power_supply_desc *desc,
1415 const struct power_supply_config *cfg)
1417 return __power_supply_register(parent, desc, cfg, true);
1419 EXPORT_SYMBOL_GPL(power_supply_register);
1422 * power_supply_register_no_ws() - Register new non-waking-source power supply
1423 * @parent: Device to be a parent of power supply's device, usually
1424 * the device which probe function calls this
1425 * @desc: Description of power supply, must be valid through whole
1426 * lifetime of this power supply
1427 * @cfg: Run-time specific configuration accessed during registering,
1430 * Return: A pointer to newly allocated power_supply on success
1431 * or ERR_PTR otherwise.
1432 * Use power_supply_unregister() on returned power_supply pointer to release
1435 struct power_supply *__must_check
1436 power_supply_register_no_ws(struct device *parent,
1437 const struct power_supply_desc *desc,
1438 const struct power_supply_config *cfg)
1440 return __power_supply_register(parent, desc, cfg, false);
1442 EXPORT_SYMBOL_GPL(power_supply_register_no_ws);
1444 static void devm_power_supply_release(struct device *dev, void *res)
1446 struct power_supply **psy = res;
1448 power_supply_unregister(*psy);
1452 * devm_power_supply_register() - Register managed power supply
1453 * @parent: Device to be a parent of power supply's device, usually
1454 * the device which probe function calls this
1455 * @desc: Description of power supply, must be valid through whole
1456 * lifetime of this power supply
1457 * @cfg: Run-time specific configuration accessed during registering,
1460 * Return: A pointer to newly allocated power_supply on success
1461 * or ERR_PTR otherwise.
1462 * The returned power_supply pointer will be automatically unregistered
1465 struct power_supply *__must_check
1466 devm_power_supply_register(struct device *parent,
1467 const struct power_supply_desc *desc,
1468 const struct power_supply_config *cfg)
1470 struct power_supply **ptr, *psy;
1472 ptr = devres_alloc(devm_power_supply_release, sizeof(*ptr), GFP_KERNEL);
1475 return ERR_PTR(-ENOMEM);
1476 psy = __power_supply_register(parent, desc, cfg, true);
1481 devres_add(parent, ptr);
1485 EXPORT_SYMBOL_GPL(devm_power_supply_register);
1488 * devm_power_supply_register_no_ws() - Register managed non-waking-source power supply
1489 * @parent: Device to be a parent of power supply's device, usually
1490 * the device which probe function calls this
1491 * @desc: Description of power supply, must be valid through whole
1492 * lifetime of this power supply
1493 * @cfg: Run-time specific configuration accessed during registering,
1496 * Return: A pointer to newly allocated power_supply on success
1497 * or ERR_PTR otherwise.
1498 * The returned power_supply pointer will be automatically unregistered
1501 struct power_supply *__must_check
1502 devm_power_supply_register_no_ws(struct device *parent,
1503 const struct power_supply_desc *desc,
1504 const struct power_supply_config *cfg)
1506 struct power_supply **ptr, *psy;
1508 ptr = devres_alloc(devm_power_supply_release, sizeof(*ptr), GFP_KERNEL);
1511 return ERR_PTR(-ENOMEM);
1512 psy = __power_supply_register(parent, desc, cfg, false);
1517 devres_add(parent, ptr);
1521 EXPORT_SYMBOL_GPL(devm_power_supply_register_no_ws);
1524 * power_supply_unregister() - Remove this power supply from system
1525 * @psy: Pointer to power supply to unregister
1527 * Remove this power supply from the system. The resources of power supply
1528 * will be freed here or on last power_supply_put() call.
1530 void power_supply_unregister(struct power_supply *psy)
1532 WARN_ON(atomic_dec_return(&psy->use_cnt));
1533 psy->removing = true;
1534 cancel_work_sync(&psy->changed_work);
1535 cancel_delayed_work_sync(&psy->deferred_register_work);
1536 sysfs_remove_link(&psy->dev.kobj, "powers");
1537 power_supply_remove_hwmon_sysfs(psy);
1538 power_supply_remove_triggers(psy);
1539 psy_unregister_cooler(psy);
1540 psy_unregister_thermal(psy);
1541 device_init_wakeup(&psy->dev, false);
1542 device_unregister(&psy->dev);
1544 EXPORT_SYMBOL_GPL(power_supply_unregister);
1546 void *power_supply_get_drvdata(struct power_supply *psy)
1548 return psy->drv_data;
1550 EXPORT_SYMBOL_GPL(power_supply_get_drvdata);
1552 static int __init power_supply_class_init(void)
1554 power_supply_class = class_create(THIS_MODULE, "power_supply");
1556 if (IS_ERR(power_supply_class))
1557 return PTR_ERR(power_supply_class);
1559 power_supply_class->dev_uevent = power_supply_uevent;
1560 power_supply_init_attrs(&power_supply_dev_type);
1565 static void __exit power_supply_class_exit(void)
1567 class_destroy(power_supply_class);
1570 subsys_initcall(power_supply_class_init);
1571 module_exit(power_supply_class_exit);
1573 MODULE_DESCRIPTION("Universal power supply monitor class");
1574 MODULE_AUTHOR("Ian Molton <spyro@f2s.com>, "
1576 "Anton Vorontsov <cbou@mail.ru>");
1577 MODULE_LICENSE("GPL");