1631 lines · c
1// SPDX-License-Identifier: GPL-2.0-only2/*3 * Universal power supply monitor class4 *5 * Copyright © 2007 Anton Vorontsov <cbou@mail.ru>6 * Copyright © 2004 Szabolcs Gyurko7 * Copyright © 2003 Ian Molton <spyro@f2s.com>8 *9 * Modified: 2004, Oct Szabolcs Gyurko10 */11 12#include <linux/cleanup.h>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>21#include <linux/of.h>22#include <linux/power_supply.h>23#include <linux/property.h>24#include <linux/thermal.h>25#include <linux/fixp-arith.h>26#include "power_supply.h"27#include "samsung-sdi-battery.h"28 29static const struct class power_supply_class = {30 .name = "power_supply",31 .dev_uevent = power_supply_uevent,32};33 34static BLOCKING_NOTIFIER_HEAD(power_supply_notifier);35 36static const struct device_type power_supply_dev_type = {37 .name = "power_supply",38 .groups = power_supply_attr_groups,39};40 41#define POWER_SUPPLY_DEFERRED_REGISTER_TIME msecs_to_jiffies(10)42 43static bool __power_supply_is_supplied_by(struct power_supply *supplier,44 struct power_supply *supply)45{46 int i;47 48 if (!supply->supplied_from && !supplier->supplied_to)49 return false;50 51 /* Support both supplied_to and supplied_from modes */52 if (supply->supplied_from) {53 if (!supplier->desc->name)54 return false;55 for (i = 0; i < supply->num_supplies; i++)56 if (!strcmp(supplier->desc->name, supply->supplied_from[i]))57 return true;58 } else {59 if (!supply->desc->name)60 return false;61 for (i = 0; i < supplier->num_supplicants; i++)62 if (!strcmp(supplier->supplied_to[i], supply->desc->name))63 return true;64 }65 66 return false;67}68 69static int __power_supply_changed_work(struct device *dev, void *data)70{71 struct power_supply *psy = data;72 struct power_supply *pst = dev_get_drvdata(dev);73 74 if (__power_supply_is_supplied_by(psy, pst)) {75 if (pst->desc->external_power_changed)76 pst->desc->external_power_changed(pst);77 }78 79 return 0;80}81 82static void power_supply_changed_work(struct work_struct *work)83{84 unsigned long flags;85 struct power_supply *psy = container_of(work, struct power_supply,86 changed_work);87 88 dev_dbg(&psy->dev, "%s\n", __func__);89 90 spin_lock_irqsave(&psy->changed_lock, flags);91 /*92 * Check 'changed' here to avoid issues due to race between93 * power_supply_changed() and this routine. In worst case94 * power_supply_changed() can be called again just before we take above95 * lock. During the first call of this routine we will mark 'changed' as96 * false and it will stay false for the next call as well.97 */98 if (likely(psy->changed)) {99 psy->changed = false;100 spin_unlock_irqrestore(&psy->changed_lock, flags);101 power_supply_for_each_device(psy, __power_supply_changed_work);102 power_supply_update_leds(psy);103 blocking_notifier_call_chain(&power_supply_notifier,104 PSY_EVENT_PROP_CHANGED, psy);105 kobject_uevent(&psy->dev.kobj, KOBJ_CHANGE);106 spin_lock_irqsave(&psy->changed_lock, flags);107 }108 109 /*110 * Hold the wakeup_source until all events are processed.111 * power_supply_changed() might have called again and have set 'changed'112 * to true.113 */114 if (likely(!psy->changed))115 pm_relax(&psy->dev);116 spin_unlock_irqrestore(&psy->changed_lock, flags);117}118 119int power_supply_for_each_device(void *data, int (*fn)(struct device *dev, void *data))120{121 return class_for_each_device(&power_supply_class, NULL, data, fn);122}123EXPORT_SYMBOL_GPL(power_supply_for_each_device);124 125void power_supply_changed(struct power_supply *psy)126{127 unsigned long flags;128 129 dev_dbg(&psy->dev, "%s\n", __func__);130 131 spin_lock_irqsave(&psy->changed_lock, flags);132 psy->changed = true;133 pm_stay_awake(&psy->dev);134 spin_unlock_irqrestore(&psy->changed_lock, flags);135 schedule_work(&psy->changed_work);136}137EXPORT_SYMBOL_GPL(power_supply_changed);138 139/*140 * Notify that power supply was registered after parent finished the probing.141 *142 * Often power supply is registered from driver's probe function. However143 * calling power_supply_changed() directly from power_supply_register()144 * would lead to execution of get_property() function provided by the driver145 * too early - before the probe ends.146 *147 * Avoid that by waiting on parent's mutex.148 */149static void power_supply_deferred_register_work(struct work_struct *work)150{151 struct power_supply *psy = container_of(work, struct power_supply,152 deferred_register_work.work);153 154 if (psy->dev.parent) {155 while (!mutex_trylock(&psy->dev.parent->mutex)) {156 if (psy->removing)157 return;158 msleep(10);159 }160 }161 162 power_supply_changed(psy);163 164 if (psy->dev.parent)165 mutex_unlock(&psy->dev.parent->mutex);166}167 168#ifdef CONFIG_OF169static int __power_supply_populate_supplied_from(struct device *dev,170 void *data)171{172 struct power_supply *psy = data;173 struct power_supply *epsy = dev_get_drvdata(dev);174 struct device_node *np;175 int i = 0;176 177 do {178 np = of_parse_phandle(psy->of_node, "power-supplies", i++);179 if (!np)180 break;181 182 if (np == epsy->of_node) {183 dev_dbg(&psy->dev, "%s: Found supply : %s\n",184 psy->desc->name, epsy->desc->name);185 psy->supplied_from[i-1] = (char *)epsy->desc->name;186 psy->num_supplies++;187 of_node_put(np);188 break;189 }190 of_node_put(np);191 } while (np);192 193 return 0;194}195 196static int power_supply_populate_supplied_from(struct power_supply *psy)197{198 int error;199 200 error = power_supply_for_each_device(psy, __power_supply_populate_supplied_from);201 202 dev_dbg(&psy->dev, "%s %d\n", __func__, error);203 204 return error;205}206 207static int __power_supply_find_supply_from_node(struct device *dev,208 void *data)209{210 struct device_node *np = data;211 struct power_supply *epsy = dev_get_drvdata(dev);212 213 /* returning non-zero breaks out of power_supply_for_each_device loop */214 if (epsy->of_node == np)215 return 1;216 217 return 0;218}219 220static int power_supply_find_supply_from_node(struct device_node *supply_node)221{222 int error;223 224 /*225 * power_supply_for_each_device() either returns its own errors or values226 * returned by __power_supply_find_supply_from_node().227 *228 * __power_supply_find_supply_from_node() will return 0 (no match)229 * or 1 (match).230 *231 * We return 0 if power_supply_for_each_device() returned 1, -EPROBE_DEFER if232 * it returned 0, or error as returned by it.233 */234 error = power_supply_for_each_device(supply_node, __power_supply_find_supply_from_node);235 236 return error ? (error == 1 ? 0 : error) : -EPROBE_DEFER;237}238 239static int power_supply_check_supplies(struct power_supply *psy)240{241 struct device_node *np;242 int cnt = 0;243 244 /* If there is already a list honor it */245 if (psy->supplied_from && psy->num_supplies > 0)246 return 0;247 248 /* No device node found, nothing to do */249 if (!psy->of_node)250 return 0;251 252 do {253 int ret;254 255 np = of_parse_phandle(psy->of_node, "power-supplies", cnt++);256 if (!np)257 break;258 259 ret = power_supply_find_supply_from_node(np);260 of_node_put(np);261 262 if (ret) {263 dev_dbg(&psy->dev, "Failed to find supply!\n");264 return ret;265 }266 } while (np);267 268 /* Missing valid "power-supplies" entries */269 if (cnt == 1)270 return 0;271 272 /* All supplies found, allocate char ** array for filling */273 psy->supplied_from = devm_kzalloc(&psy->dev, sizeof(*psy->supplied_from),274 GFP_KERNEL);275 if (!psy->supplied_from)276 return -ENOMEM;277 278 *psy->supplied_from = devm_kcalloc(&psy->dev,279 cnt - 1, sizeof(**psy->supplied_from),280 GFP_KERNEL);281 if (!*psy->supplied_from)282 return -ENOMEM;283 284 return power_supply_populate_supplied_from(psy);285}286#else287static int power_supply_check_supplies(struct power_supply *psy)288{289 int nval, ret;290 291 if (!psy->dev.parent)292 return 0;293 294 nval = device_property_string_array_count(psy->dev.parent, "supplied-from");295 if (nval <= 0)296 return 0;297 298 psy->supplied_from = devm_kmalloc_array(&psy->dev, nval,299 sizeof(char *), GFP_KERNEL);300 if (!psy->supplied_from)301 return -ENOMEM;302 303 ret = device_property_read_string_array(psy->dev.parent,304 "supplied-from", (const char **)psy->supplied_from, nval);305 if (ret < 0)306 return ret;307 308 psy->num_supplies = nval;309 310 return 0;311}312#endif313 314struct psy_am_i_supplied_data {315 struct power_supply *psy;316 unsigned int count;317};318 319static int __power_supply_am_i_supplied(struct device *dev, void *_data)320{321 union power_supply_propval ret = {0,};322 struct power_supply *epsy = dev_get_drvdata(dev);323 struct psy_am_i_supplied_data *data = _data;324 325 if (__power_supply_is_supplied_by(epsy, data->psy)) {326 data->count++;327 if (!epsy->desc->get_property(epsy, POWER_SUPPLY_PROP_ONLINE,328 &ret))329 return ret.intval;330 }331 332 return 0;333}334 335int power_supply_am_i_supplied(struct power_supply *psy)336{337 struct psy_am_i_supplied_data data = { psy, 0 };338 int error;339 340 error = power_supply_for_each_device(&data, __power_supply_am_i_supplied);341 342 dev_dbg(&psy->dev, "%s count %u err %d\n", __func__, data.count, error);343 344 if (data.count == 0)345 return -ENODEV;346 347 return error;348}349EXPORT_SYMBOL_GPL(power_supply_am_i_supplied);350 351static int __power_supply_is_system_supplied(struct device *dev, void *data)352{353 union power_supply_propval ret = {0,};354 struct power_supply *psy = dev_get_drvdata(dev);355 unsigned int *count = data;356 357 if (!psy->desc->get_property(psy, POWER_SUPPLY_PROP_SCOPE, &ret))358 if (ret.intval == POWER_SUPPLY_SCOPE_DEVICE)359 return 0;360 361 (*count)++;362 if (psy->desc->type != POWER_SUPPLY_TYPE_BATTERY)363 if (!psy->desc->get_property(psy, POWER_SUPPLY_PROP_ONLINE,364 &ret))365 return ret.intval;366 367 return 0;368}369 370int power_supply_is_system_supplied(void)371{372 int error;373 unsigned int count = 0;374 375 error = power_supply_for_each_device(&count, __power_supply_is_system_supplied);376 377 /*378 * If no system scope power class device was found at all, most probably we379 * are running on a desktop system, so assume we are on mains power.380 */381 if (count == 0)382 return 1;383 384 return error;385}386EXPORT_SYMBOL_GPL(power_supply_is_system_supplied);387 388struct psy_get_supplier_prop_data {389 struct power_supply *psy;390 enum power_supply_property psp;391 union power_supply_propval *val;392};393 394static int __power_supply_get_supplier_property(struct device *dev, void *_data)395{396 struct power_supply *epsy = dev_get_drvdata(dev);397 struct psy_get_supplier_prop_data *data = _data;398 399 if (__power_supply_is_supplied_by(epsy, data->psy))400 if (!power_supply_get_property(epsy, data->psp, data->val))401 return 1; /* Success */402 403 return 0; /* Continue iterating */404}405 406int power_supply_get_property_from_supplier(struct power_supply *psy,407 enum power_supply_property psp,408 union power_supply_propval *val)409{410 struct psy_get_supplier_prop_data data = {411 .psy = psy,412 .psp = psp,413 .val = val,414 };415 int ret;416 417 /*418 * This function is not intended for use with a supply with multiple419 * suppliers, we simply pick the first supply to report the psp.420 */421 ret = power_supply_for_each_device(&data, __power_supply_get_supplier_property);422 if (ret < 0)423 return ret;424 if (ret == 0)425 return -ENODEV;426 427 return 0;428}429EXPORT_SYMBOL_GPL(power_supply_get_property_from_supplier);430 431int power_supply_set_battery_charged(struct power_supply *psy)432{433 if (atomic_read(&psy->use_cnt) >= 0 &&434 psy->desc->type == POWER_SUPPLY_TYPE_BATTERY &&435 psy->desc->set_charged) {436 psy->desc->set_charged(psy);437 return 0;438 }439 440 return -EINVAL;441}442EXPORT_SYMBOL_GPL(power_supply_set_battery_charged);443 444static int power_supply_match_device_by_name(struct device *dev, const void *data)445{446 const char *name = data;447 struct power_supply *psy = dev_get_drvdata(dev);448 449 return strcmp(psy->desc->name, name) == 0;450}451 452/**453 * power_supply_get_by_name() - Search for a power supply and returns its ref454 * @name: Power supply name to fetch455 *456 * If power supply was found, it increases reference count for the457 * internal power supply's device. The user should power_supply_put()458 * after usage.459 *460 * Return: On success returns a reference to a power supply with461 * matching name equals to @name, a NULL otherwise.462 */463struct power_supply *power_supply_get_by_name(const char *name)464{465 struct power_supply *psy = NULL;466 struct device *dev = class_find_device(&power_supply_class, NULL, name,467 power_supply_match_device_by_name);468 469 if (dev) {470 psy = dev_get_drvdata(dev);471 atomic_inc(&psy->use_cnt);472 }473 474 return psy;475}476EXPORT_SYMBOL_GPL(power_supply_get_by_name);477 478/**479 * power_supply_put() - Drop reference obtained with power_supply_get_by_name480 * @psy: Reference to put481 *482 * The reference to power supply should be put before unregistering483 * the power supply.484 */485void power_supply_put(struct power_supply *psy)486{487 might_sleep();488 489 atomic_dec(&psy->use_cnt);490 put_device(&psy->dev);491}492EXPORT_SYMBOL_GPL(power_supply_put);493 494#ifdef CONFIG_OF495static int power_supply_match_device_node(struct device *dev, const void *data)496{497 return dev->parent && dev->parent->of_node == data;498}499 500/**501 * power_supply_get_by_phandle() - Search for a power supply and returns its ref502 * @np: Pointer to device node holding phandle property503 * @property: Name of property holding a power supply name504 *505 * If power supply was found, it increases reference count for the506 * internal power supply's device. The user should power_supply_put()507 * after usage.508 *509 * Return: On success returns a reference to a power supply with510 * matching name equals to value under @property, NULL or ERR_PTR otherwise.511 */512struct power_supply *power_supply_get_by_phandle(struct device_node *np,513 const char *property)514{515 struct device_node *power_supply_np;516 struct power_supply *psy = NULL;517 struct device *dev;518 519 power_supply_np = of_parse_phandle(np, property, 0);520 if (!power_supply_np)521 return ERR_PTR(-ENODEV);522 523 dev = class_find_device(&power_supply_class, NULL, power_supply_np,524 power_supply_match_device_node);525 526 of_node_put(power_supply_np);527 528 if (dev) {529 psy = dev_get_drvdata(dev);530 atomic_inc(&psy->use_cnt);531 }532 533 return psy;534}535EXPORT_SYMBOL_GPL(power_supply_get_by_phandle);536 537static void devm_power_supply_put(struct device *dev, void *res)538{539 struct power_supply **psy = res;540 541 power_supply_put(*psy);542}543 544/**545 * devm_power_supply_get_by_phandle() - Resource managed version of546 * power_supply_get_by_phandle()547 * @dev: Pointer to device holding phandle property548 * @property: Name of property holding a power supply phandle549 *550 * Return: On success returns a reference to a power supply with551 * matching name equals to value under @property, NULL or ERR_PTR otherwise.552 */553struct power_supply *devm_power_supply_get_by_phandle(struct device *dev,554 const char *property)555{556 struct power_supply **ptr, *psy;557 558 if (!dev->of_node)559 return ERR_PTR(-ENODEV);560 561 ptr = devres_alloc(devm_power_supply_put, sizeof(*ptr), GFP_KERNEL);562 if (!ptr)563 return ERR_PTR(-ENOMEM);564 565 psy = power_supply_get_by_phandle(dev->of_node, property);566 if (IS_ERR_OR_NULL(psy)) {567 devres_free(ptr);568 } else {569 *ptr = psy;570 devres_add(dev, ptr);571 }572 return psy;573}574EXPORT_SYMBOL_GPL(devm_power_supply_get_by_phandle);575#endif /* CONFIG_OF */576 577int power_supply_get_battery_info(struct power_supply *psy,578 struct power_supply_battery_info **info_out)579{580 struct power_supply_resistance_temp_table *resist_table;581 struct power_supply_battery_info *info;582 struct device_node *battery_np = NULL;583 struct fwnode_reference_args args;584 struct fwnode_handle *fwnode = NULL;585 const char *value;586 int err, len, index;587 const __be32 *list;588 u32 min_max[2];589 590 if (psy->of_node) {591 battery_np = of_parse_phandle(psy->of_node, "monitored-battery", 0);592 if (!battery_np)593 return -ENODEV;594 595 fwnode = fwnode_handle_get(of_fwnode_handle(battery_np));596 } else if (psy->dev.parent) {597 err = fwnode_property_get_reference_args(598 dev_fwnode(psy->dev.parent),599 "monitored-battery", NULL, 0, 0, &args);600 if (err)601 return err;602 603 fwnode = args.fwnode;604 }605 606 if (!fwnode)607 return -ENOENT;608 609 err = fwnode_property_read_string(fwnode, "compatible", &value);610 if (err)611 goto out_put_node;612 613 614 /* Try static batteries first */615 err = samsung_sdi_battery_get_info(&psy->dev, value, &info);616 if (!err)617 goto out_ret_pointer;618 else if (err == -ENODEV)619 /*620 * Device does not have a static battery.621 * Proceed to look for a simple battery.622 */623 err = 0;624 625 if (strcmp("simple-battery", value)) {626 err = -ENODEV;627 goto out_put_node;628 }629 630 info = devm_kzalloc(&psy->dev, sizeof(*info), GFP_KERNEL);631 if (!info) {632 err = -ENOMEM;633 goto out_put_node;634 }635 636 info->technology = POWER_SUPPLY_TECHNOLOGY_UNKNOWN;637 info->energy_full_design_uwh = -EINVAL;638 info->charge_full_design_uah = -EINVAL;639 info->voltage_min_design_uv = -EINVAL;640 info->voltage_max_design_uv = -EINVAL;641 info->precharge_current_ua = -EINVAL;642 info->charge_term_current_ua = -EINVAL;643 info->constant_charge_current_max_ua = -EINVAL;644 info->constant_charge_voltage_max_uv = -EINVAL;645 info->tricklecharge_current_ua = -EINVAL;646 info->precharge_voltage_max_uv = -EINVAL;647 info->charge_restart_voltage_uv = -EINVAL;648 info->overvoltage_limit_uv = -EINVAL;649 info->maintenance_charge = NULL;650 info->alert_low_temp_charge_current_ua = -EINVAL;651 info->alert_low_temp_charge_voltage_uv = -EINVAL;652 info->alert_high_temp_charge_current_ua = -EINVAL;653 info->alert_high_temp_charge_voltage_uv = -EINVAL;654 info->temp_ambient_alert_min = INT_MIN;655 info->temp_ambient_alert_max = INT_MAX;656 info->temp_alert_min = INT_MIN;657 info->temp_alert_max = INT_MAX;658 info->temp_min = INT_MIN;659 info->temp_max = INT_MAX;660 info->factory_internal_resistance_uohm = -EINVAL;661 info->resist_table = NULL;662 info->bti_resistance_ohm = -EINVAL;663 info->bti_resistance_tolerance = -EINVAL;664 665 for (index = 0; index < POWER_SUPPLY_OCV_TEMP_MAX; index++) {666 info->ocv_table[index] = NULL;667 info->ocv_temp[index] = -EINVAL;668 info->ocv_table_size[index] = -EINVAL;669 }670 671 /* The property and field names below must correspond to elements672 * in enum power_supply_property. For reasoning, see673 * Documentation/power/power_supply_class.rst.674 */675 676 if (!fwnode_property_read_string(fwnode, "device-chemistry", &value)) {677 if (!strcmp("nickel-cadmium", value))678 info->technology = POWER_SUPPLY_TECHNOLOGY_NiCd;679 else if (!strcmp("nickel-metal-hydride", value))680 info->technology = POWER_SUPPLY_TECHNOLOGY_NiMH;681 else if (!strcmp("lithium-ion", value))682 /* Imprecise lithium-ion type */683 info->technology = POWER_SUPPLY_TECHNOLOGY_LION;684 else if (!strcmp("lithium-ion-polymer", value))685 info->technology = POWER_SUPPLY_TECHNOLOGY_LIPO;686 else if (!strcmp("lithium-ion-iron-phosphate", value))687 info->technology = POWER_SUPPLY_TECHNOLOGY_LiFe;688 else if (!strcmp("lithium-ion-manganese-oxide", value))689 info->technology = POWER_SUPPLY_TECHNOLOGY_LiMn;690 else691 dev_warn(&psy->dev, "%s unknown battery type\n", value);692 }693 694 fwnode_property_read_u32(fwnode, "energy-full-design-microwatt-hours",695 &info->energy_full_design_uwh);696 fwnode_property_read_u32(fwnode, "charge-full-design-microamp-hours",697 &info->charge_full_design_uah);698 fwnode_property_read_u32(fwnode, "voltage-min-design-microvolt",699 &info->voltage_min_design_uv);700 fwnode_property_read_u32(fwnode, "voltage-max-design-microvolt",701 &info->voltage_max_design_uv);702 fwnode_property_read_u32(fwnode, "trickle-charge-current-microamp",703 &info->tricklecharge_current_ua);704 fwnode_property_read_u32(fwnode, "precharge-current-microamp",705 &info->precharge_current_ua);706 fwnode_property_read_u32(fwnode, "precharge-upper-limit-microvolt",707 &info->precharge_voltage_max_uv);708 fwnode_property_read_u32(fwnode, "charge-term-current-microamp",709 &info->charge_term_current_ua);710 fwnode_property_read_u32(fwnode, "re-charge-voltage-microvolt",711 &info->charge_restart_voltage_uv);712 fwnode_property_read_u32(fwnode, "over-voltage-threshold-microvolt",713 &info->overvoltage_limit_uv);714 fwnode_property_read_u32(fwnode, "constant-charge-current-max-microamp",715 &info->constant_charge_current_max_ua);716 fwnode_property_read_u32(fwnode, "constant-charge-voltage-max-microvolt",717 &info->constant_charge_voltage_max_uv);718 fwnode_property_read_u32(fwnode, "factory-internal-resistance-micro-ohms",719 &info->factory_internal_resistance_uohm);720 721 if (!fwnode_property_read_u32_array(fwnode, "ambient-celsius",722 min_max, ARRAY_SIZE(min_max))) {723 info->temp_ambient_alert_min = min_max[0];724 info->temp_ambient_alert_max = min_max[1];725 }726 if (!fwnode_property_read_u32_array(fwnode, "alert-celsius",727 min_max, ARRAY_SIZE(min_max))) {728 info->temp_alert_min = min_max[0];729 info->temp_alert_max = min_max[1];730 }731 if (!fwnode_property_read_u32_array(fwnode, "operating-range-celsius",732 min_max, ARRAY_SIZE(min_max))) {733 info->temp_min = min_max[0];734 info->temp_max = min_max[1];735 }736 737 /*738 * The below code uses raw of-data parsing to parse739 * /schemas/types.yaml#/definitions/uint32-matrix740 * data, so for now this is only support with of.741 */742 if (!battery_np)743 goto out_ret_pointer;744 745 len = of_property_count_u32_elems(battery_np, "ocv-capacity-celsius");746 if (len < 0 && len != -EINVAL) {747 err = len;748 goto out_put_node;749 } else if (len > POWER_SUPPLY_OCV_TEMP_MAX) {750 dev_err(&psy->dev, "Too many temperature values\n");751 err = -EINVAL;752 goto out_put_node;753 } else if (len > 0) {754 of_property_read_u32_array(battery_np, "ocv-capacity-celsius",755 info->ocv_temp, len);756 }757 758 for (index = 0; index < len; index++) {759 struct power_supply_battery_ocv_table *table;760 int i, tab_len, size;761 762 char *propname __free(kfree) = kasprintf(GFP_KERNEL, "ocv-capacity-table-%d",763 index);764 if (!propname) {765 power_supply_put_battery_info(psy, info);766 err = -ENOMEM;767 goto out_put_node;768 }769 list = of_get_property(battery_np, propname, &size);770 if (!list || !size) {771 dev_err(&psy->dev, "failed to get %s\n", propname);772 power_supply_put_battery_info(psy, info);773 err = -EINVAL;774 goto out_put_node;775 }776 777 tab_len = size / (2 * sizeof(__be32));778 info->ocv_table_size[index] = tab_len;779 780 table = info->ocv_table[index] =781 devm_kcalloc(&psy->dev, tab_len, sizeof(*table), GFP_KERNEL);782 if (!info->ocv_table[index]) {783 power_supply_put_battery_info(psy, info);784 err = -ENOMEM;785 goto out_put_node;786 }787 788 for (i = 0; i < tab_len; i++) {789 table[i].ocv = be32_to_cpu(*list);790 list++;791 table[i].capacity = be32_to_cpu(*list);792 list++;793 }794 }795 796 list = of_get_property(battery_np, "resistance-temp-table", &len);797 if (!list || !len)798 goto out_ret_pointer;799 800 info->resist_table_size = len / (2 * sizeof(__be32));801 resist_table = info->resist_table = devm_kcalloc(&psy->dev,802 info->resist_table_size,803 sizeof(*resist_table),804 GFP_KERNEL);805 if (!info->resist_table) {806 power_supply_put_battery_info(psy, info);807 err = -ENOMEM;808 goto out_put_node;809 }810 811 for (index = 0; index < info->resist_table_size; index++) {812 resist_table[index].temp = be32_to_cpu(*list++);813 resist_table[index].resistance = be32_to_cpu(*list++);814 }815 816out_ret_pointer:817 /* Finally return the whole thing */818 *info_out = info;819 820out_put_node:821 fwnode_handle_put(fwnode);822 of_node_put(battery_np);823 return err;824}825EXPORT_SYMBOL_GPL(power_supply_get_battery_info);826 827void power_supply_put_battery_info(struct power_supply *psy,828 struct power_supply_battery_info *info)829{830 int i;831 832 for (i = 0; i < POWER_SUPPLY_OCV_TEMP_MAX; i++) {833 if (info->ocv_table[i])834 devm_kfree(&psy->dev, info->ocv_table[i]);835 }836 837 if (info->resist_table)838 devm_kfree(&psy->dev, info->resist_table);839 840 devm_kfree(&psy->dev, info);841}842EXPORT_SYMBOL_GPL(power_supply_put_battery_info);843 844const enum power_supply_property power_supply_battery_info_properties[] = {845 POWER_SUPPLY_PROP_TECHNOLOGY,846 POWER_SUPPLY_PROP_ENERGY_FULL_DESIGN,847 POWER_SUPPLY_PROP_CHARGE_FULL_DESIGN,848 POWER_SUPPLY_PROP_VOLTAGE_MIN_DESIGN,849 POWER_SUPPLY_PROP_VOLTAGE_MAX_DESIGN,850 POWER_SUPPLY_PROP_PRECHARGE_CURRENT,851 POWER_SUPPLY_PROP_CHARGE_TERM_CURRENT,852 POWER_SUPPLY_PROP_CONSTANT_CHARGE_CURRENT_MAX,853 POWER_SUPPLY_PROP_CONSTANT_CHARGE_VOLTAGE_MAX,854 POWER_SUPPLY_PROP_TEMP_AMBIENT_ALERT_MIN,855 POWER_SUPPLY_PROP_TEMP_AMBIENT_ALERT_MAX,856 POWER_SUPPLY_PROP_TEMP_ALERT_MIN,857 POWER_SUPPLY_PROP_TEMP_ALERT_MAX,858 POWER_SUPPLY_PROP_TEMP_MIN,859 POWER_SUPPLY_PROP_TEMP_MAX,860};861EXPORT_SYMBOL_GPL(power_supply_battery_info_properties);862 863const size_t power_supply_battery_info_properties_size = ARRAY_SIZE(power_supply_battery_info_properties);864EXPORT_SYMBOL_GPL(power_supply_battery_info_properties_size);865 866bool power_supply_battery_info_has_prop(struct power_supply_battery_info *info,867 enum power_supply_property psp)868{869 if (!info)870 return false;871 872 switch (psp) {873 case POWER_SUPPLY_PROP_TECHNOLOGY:874 return info->technology != POWER_SUPPLY_TECHNOLOGY_UNKNOWN;875 case POWER_SUPPLY_PROP_ENERGY_FULL_DESIGN:876 return info->energy_full_design_uwh >= 0;877 case POWER_SUPPLY_PROP_CHARGE_FULL_DESIGN:878 return info->charge_full_design_uah >= 0;879 case POWER_SUPPLY_PROP_VOLTAGE_MIN_DESIGN:880 return info->voltage_min_design_uv >= 0;881 case POWER_SUPPLY_PROP_VOLTAGE_MAX_DESIGN:882 return info->voltage_max_design_uv >= 0;883 case POWER_SUPPLY_PROP_PRECHARGE_CURRENT:884 return info->precharge_current_ua >= 0;885 case POWER_SUPPLY_PROP_CHARGE_TERM_CURRENT:886 return info->charge_term_current_ua >= 0;887 case POWER_SUPPLY_PROP_CONSTANT_CHARGE_CURRENT_MAX:888 return info->constant_charge_current_max_ua >= 0;889 case POWER_SUPPLY_PROP_CONSTANT_CHARGE_VOLTAGE_MAX:890 return info->constant_charge_voltage_max_uv >= 0;891 case POWER_SUPPLY_PROP_TEMP_AMBIENT_ALERT_MIN:892 return info->temp_ambient_alert_min > INT_MIN;893 case POWER_SUPPLY_PROP_TEMP_AMBIENT_ALERT_MAX:894 return info->temp_ambient_alert_max < INT_MAX;895 case POWER_SUPPLY_PROP_TEMP_ALERT_MIN:896 return info->temp_alert_min > INT_MIN;897 case POWER_SUPPLY_PROP_TEMP_ALERT_MAX:898 return info->temp_alert_max < INT_MAX;899 case POWER_SUPPLY_PROP_TEMP_MIN:900 return info->temp_min > INT_MIN;901 case POWER_SUPPLY_PROP_TEMP_MAX:902 return info->temp_max < INT_MAX;903 default:904 return false;905 }906}907EXPORT_SYMBOL_GPL(power_supply_battery_info_has_prop);908 909int power_supply_battery_info_get_prop(struct power_supply_battery_info *info,910 enum power_supply_property psp,911 union power_supply_propval *val)912{913 if (!info)914 return -EINVAL;915 916 if (!power_supply_battery_info_has_prop(info, psp))917 return -EINVAL;918 919 switch (psp) {920 case POWER_SUPPLY_PROP_TECHNOLOGY:921 val->intval = info->technology;922 return 0;923 case POWER_SUPPLY_PROP_ENERGY_FULL_DESIGN:924 val->intval = info->energy_full_design_uwh;925 return 0;926 case POWER_SUPPLY_PROP_CHARGE_FULL_DESIGN:927 val->intval = info->charge_full_design_uah;928 return 0;929 case POWER_SUPPLY_PROP_VOLTAGE_MIN_DESIGN:930 val->intval = info->voltage_min_design_uv;931 return 0;932 case POWER_SUPPLY_PROP_VOLTAGE_MAX_DESIGN:933 val->intval = info->voltage_max_design_uv;934 return 0;935 case POWER_SUPPLY_PROP_PRECHARGE_CURRENT:936 val->intval = info->precharge_current_ua;937 return 0;938 case POWER_SUPPLY_PROP_CHARGE_TERM_CURRENT:939 val->intval = info->charge_term_current_ua;940 return 0;941 case POWER_SUPPLY_PROP_CONSTANT_CHARGE_CURRENT_MAX:942 val->intval = info->constant_charge_current_max_ua;943 return 0;944 case POWER_SUPPLY_PROP_CONSTANT_CHARGE_VOLTAGE_MAX:945 val->intval = info->constant_charge_voltage_max_uv;946 return 0;947 case POWER_SUPPLY_PROP_TEMP_AMBIENT_ALERT_MIN:948 val->intval = info->temp_ambient_alert_min;949 return 0;950 case POWER_SUPPLY_PROP_TEMP_AMBIENT_ALERT_MAX:951 val->intval = info->temp_ambient_alert_max;952 return 0;953 case POWER_SUPPLY_PROP_TEMP_ALERT_MIN:954 val->intval = info->temp_alert_min;955 return 0;956 case POWER_SUPPLY_PROP_TEMP_ALERT_MAX:957 val->intval = info->temp_alert_max;958 return 0;959 case POWER_SUPPLY_PROP_TEMP_MIN:960 val->intval = info->temp_min;961 return 0;962 case POWER_SUPPLY_PROP_TEMP_MAX:963 val->intval = info->temp_max;964 return 0;965 default:966 return -EINVAL;967 }968}969EXPORT_SYMBOL_GPL(power_supply_battery_info_get_prop);970 971/**972 * power_supply_temp2resist_simple() - find the battery internal resistance973 * percent from temperature974 * @table: Pointer to battery resistance temperature table975 * @table_len: The table length976 * @temp: Current temperature977 *978 * This helper function is used to look up battery internal resistance percent979 * according to current temperature value from the resistance temperature table,980 * and the table must be ordered descending. Then the actual battery internal981 * resistance = the ideal battery internal resistance * percent / 100.982 *983 * Return: the battery internal resistance percent984 */985int power_supply_temp2resist_simple(struct power_supply_resistance_temp_table *table,986 int table_len, int temp)987{988 int i, high, low;989 990 for (i = 0; i < table_len; i++)991 if (temp > table[i].temp)992 break;993 994 /* The library function will deal with high == low */995 if (i == 0)996 high = low = i;997 else if (i == table_len)998 high = low = i - 1;999 else1000 high = (low = i) - 1;1001 1002 return fixp_linear_interpolate(table[low].temp,1003 table[low].resistance,1004 table[high].temp,1005 table[high].resistance,1006 temp);1007}1008EXPORT_SYMBOL_GPL(power_supply_temp2resist_simple);1009 1010/**1011 * power_supply_vbat2ri() - find the battery internal resistance1012 * from the battery voltage1013 * @info: The battery information container1014 * @vbat_uv: The battery voltage in microvolt1015 * @charging: If we are charging (true) or not (false)1016 *1017 * This helper function is used to look up battery internal resistance1018 * according to current battery voltage. Depending on whether the battery1019 * is currently charging or not, different resistance will be returned.1020 *1021 * Returns the internal resistance in microohm or negative error code.1022 */1023int power_supply_vbat2ri(struct power_supply_battery_info *info,1024 int vbat_uv, bool charging)1025{1026 const struct power_supply_vbat_ri_table *vbat2ri;1027 int table_len;1028 int i, high, low;1029 1030 /*1031 * If we are charging, and the battery supplies a separate table1032 * for this state, we use that in order to compensate for the1033 * charging voltage. Otherwise we use the main table.1034 */1035 if (charging && info->vbat2ri_charging) {1036 vbat2ri = info->vbat2ri_charging;1037 table_len = info->vbat2ri_charging_size;1038 } else {1039 vbat2ri = info->vbat2ri_discharging;1040 table_len = info->vbat2ri_discharging_size;1041 }1042 1043 /*1044 * If no tables are specified, or if we are above the highest voltage in1045 * the voltage table, just return the factory specified internal resistance.1046 */1047 if (!vbat2ri || (table_len <= 0) || (vbat_uv > vbat2ri[0].vbat_uv)) {1048 if (charging && (info->factory_internal_resistance_charging_uohm > 0))1049 return info->factory_internal_resistance_charging_uohm;1050 else1051 return info->factory_internal_resistance_uohm;1052 }1053 1054 /* Break loop at table_len - 1 because that is the highest index */1055 for (i = 0; i < table_len - 1; i++)1056 if (vbat_uv > vbat2ri[i].vbat_uv)1057 break;1058 1059 /* The library function will deal with high == low */1060 if ((i == 0) || (i == (table_len - 1)))1061 high = i;1062 else1063 high = i - 1;1064 low = i;1065 1066 return fixp_linear_interpolate(vbat2ri[low].vbat_uv,1067 vbat2ri[low].ri_uohm,1068 vbat2ri[high].vbat_uv,1069 vbat2ri[high].ri_uohm,1070 vbat_uv);1071}1072EXPORT_SYMBOL_GPL(power_supply_vbat2ri);1073 1074const struct power_supply_maintenance_charge_table *1075power_supply_get_maintenance_charging_setting(struct power_supply_battery_info *info,1076 int index)1077{1078 if (index >= info->maintenance_charge_size)1079 return NULL;1080 return &info->maintenance_charge[index];1081}1082EXPORT_SYMBOL_GPL(power_supply_get_maintenance_charging_setting);1083 1084/**1085 * power_supply_ocv2cap_simple() - find the battery capacity1086 * @table: Pointer to battery OCV lookup table1087 * @table_len: OCV table length1088 * @ocv: Current OCV value1089 *1090 * This helper function is used to look up battery capacity according to1091 * current OCV value from one OCV table, and the OCV table must be ordered1092 * descending.1093 *1094 * Return: the battery capacity.1095 */1096int power_supply_ocv2cap_simple(struct power_supply_battery_ocv_table *table,1097 int table_len, int ocv)1098{1099 int i, high, low;1100 1101 for (i = 0; i < table_len; i++)1102 if (ocv > table[i].ocv)1103 break;1104 1105 /* The library function will deal with high == low */1106 if (i == 0)1107 high = low = i;1108 else if (i == table_len)1109 high = low = i - 1;1110 else1111 high = (low = i) - 1;1112 1113 return fixp_linear_interpolate(table[low].ocv,1114 table[low].capacity,1115 table[high].ocv,1116 table[high].capacity,1117 ocv);1118}1119EXPORT_SYMBOL_GPL(power_supply_ocv2cap_simple);1120 1121struct power_supply_battery_ocv_table *1122power_supply_find_ocv2cap_table(struct power_supply_battery_info *info,1123 int temp, int *table_len)1124{1125 int best_temp_diff = INT_MAX, temp_diff;1126 u8 i, best_index = 0;1127 1128 if (!info->ocv_table[0])1129 return NULL;1130 1131 for (i = 0; i < POWER_SUPPLY_OCV_TEMP_MAX; i++) {1132 /* Out of capacity tables */1133 if (!info->ocv_table[i])1134 break;1135 1136 temp_diff = abs(info->ocv_temp[i] - temp);1137 1138 if (temp_diff < best_temp_diff) {1139 best_temp_diff = temp_diff;1140 best_index = i;1141 }1142 }1143 1144 *table_len = info->ocv_table_size[best_index];1145 return info->ocv_table[best_index];1146}1147EXPORT_SYMBOL_GPL(power_supply_find_ocv2cap_table);1148 1149int power_supply_batinfo_ocv2cap(struct power_supply_battery_info *info,1150 int ocv, int temp)1151{1152 struct power_supply_battery_ocv_table *table;1153 int table_len;1154 1155 table = power_supply_find_ocv2cap_table(info, temp, &table_len);1156 if (!table)1157 return -EINVAL;1158 1159 return power_supply_ocv2cap_simple(table, table_len, ocv);1160}1161EXPORT_SYMBOL_GPL(power_supply_batinfo_ocv2cap);1162 1163bool power_supply_battery_bti_in_range(struct power_supply_battery_info *info,1164 int resistance)1165{1166 int low, high;1167 1168 /* Nothing like this can be checked */1169 if (info->bti_resistance_ohm <= 0)1170 return false;1171 1172 /* This will be extremely strict and unlikely to work */1173 if (info->bti_resistance_tolerance <= 0)1174 return (info->bti_resistance_ohm == resistance);1175 1176 low = info->bti_resistance_ohm -1177 (info->bti_resistance_ohm * info->bti_resistance_tolerance) / 100;1178 high = info->bti_resistance_ohm +1179 (info->bti_resistance_ohm * info->bti_resistance_tolerance) / 100;1180 1181 return ((resistance >= low) && (resistance <= high));1182}1183EXPORT_SYMBOL_GPL(power_supply_battery_bti_in_range);1184 1185static bool psy_has_property(const struct power_supply_desc *psy_desc,1186 enum power_supply_property psp)1187{1188 bool found = false;1189 int i;1190 1191 for (i = 0; i < psy_desc->num_properties; i++) {1192 if (psy_desc->properties[i] == psp) {1193 found = true;1194 break;1195 }1196 }1197 1198 return found;1199}1200 1201int power_supply_get_property(struct power_supply *psy,1202 enum power_supply_property psp,1203 union power_supply_propval *val)1204{1205 if (atomic_read(&psy->use_cnt) <= 0) {1206 if (!psy->initialized)1207 return -EAGAIN;1208 return -ENODEV;1209 }1210 1211 if (psy_has_property(psy->desc, psp))1212 return psy->desc->get_property(psy, psp, val);1213 else if (power_supply_battery_info_has_prop(psy->battery_info, psp))1214 return power_supply_battery_info_get_prop(psy->battery_info, psp, val);1215 else1216 return -EINVAL;1217}1218EXPORT_SYMBOL_GPL(power_supply_get_property);1219 1220int power_supply_set_property(struct power_supply *psy,1221 enum power_supply_property psp,1222 const union power_supply_propval *val)1223{1224 if (atomic_read(&psy->use_cnt) <= 0 || !psy->desc->set_property)1225 return -ENODEV;1226 1227 return psy->desc->set_property(psy, psp, val);1228}1229EXPORT_SYMBOL_GPL(power_supply_set_property);1230 1231int power_supply_property_is_writeable(struct power_supply *psy,1232 enum power_supply_property psp)1233{1234 return psy->desc->property_is_writeable && psy->desc->property_is_writeable(psy, psp);1235}1236EXPORT_SYMBOL_GPL(power_supply_property_is_writeable);1237 1238void power_supply_external_power_changed(struct power_supply *psy)1239{1240 if (atomic_read(&psy->use_cnt) <= 0 ||1241 !psy->desc->external_power_changed)1242 return;1243 1244 psy->desc->external_power_changed(psy);1245}1246EXPORT_SYMBOL_GPL(power_supply_external_power_changed);1247 1248int power_supply_powers(struct power_supply *psy, struct device *dev)1249{1250 return sysfs_create_link(&psy->dev.kobj, &dev->kobj, "powers");1251}1252EXPORT_SYMBOL_GPL(power_supply_powers);1253 1254static void power_supply_dev_release(struct device *dev)1255{1256 struct power_supply *psy = to_power_supply(dev);1257 1258 dev_dbg(dev, "%s\n", __func__);1259 kfree(psy);1260}1261 1262int power_supply_reg_notifier(struct notifier_block *nb)1263{1264 return blocking_notifier_chain_register(&power_supply_notifier, nb);1265}1266EXPORT_SYMBOL_GPL(power_supply_reg_notifier);1267 1268void power_supply_unreg_notifier(struct notifier_block *nb)1269{1270 blocking_notifier_chain_unregister(&power_supply_notifier, nb);1271}1272EXPORT_SYMBOL_GPL(power_supply_unreg_notifier);1273 1274#ifdef CONFIG_THERMAL1275static int power_supply_read_temp(struct thermal_zone_device *tzd,1276 int *temp)1277{1278 struct power_supply *psy;1279 union power_supply_propval val;1280 int ret;1281 1282 WARN_ON(tzd == NULL);1283 psy = thermal_zone_device_priv(tzd);1284 ret = power_supply_get_property(psy, POWER_SUPPLY_PROP_TEMP, &val);1285 if (ret)1286 return ret;1287 1288 /* Convert tenths of degree Celsius to milli degree Celsius. */1289 *temp = val.intval * 100;1290 1291 return ret;1292}1293 1294static const struct thermal_zone_device_ops psy_tzd_ops = {1295 .get_temp = power_supply_read_temp,1296};1297 1298static int psy_register_thermal(struct power_supply *psy)1299{1300 int ret;1301 1302 if (psy->desc->no_thermal)1303 return 0;1304 1305 /* Register battery zone device psy reports temperature */1306 if (psy_has_property(psy->desc, POWER_SUPPLY_PROP_TEMP)) {1307 /* Prefer our hwmon device and avoid duplicates */1308 struct thermal_zone_params tzp = {1309 .no_hwmon = IS_ENABLED(CONFIG_POWER_SUPPLY_HWMON)1310 };1311 psy->tzd = thermal_tripless_zone_device_register(psy->desc->name,1312 psy, &psy_tzd_ops, &tzp);1313 if (IS_ERR(psy->tzd))1314 return PTR_ERR(psy->tzd);1315 ret = thermal_zone_device_enable(psy->tzd);1316 if (ret)1317 thermal_zone_device_unregister(psy->tzd);1318 return ret;1319 }1320 1321 return 0;1322}1323 1324static void psy_unregister_thermal(struct power_supply *psy)1325{1326 if (IS_ERR_OR_NULL(psy->tzd))1327 return;1328 thermal_zone_device_unregister(psy->tzd);1329}1330 1331#else1332static int psy_register_thermal(struct power_supply *psy)1333{1334 return 0;1335}1336 1337static void psy_unregister_thermal(struct power_supply *psy)1338{1339}1340#endif1341 1342static struct power_supply *__must_check1343__power_supply_register(struct device *parent,1344 const struct power_supply_desc *desc,1345 const struct power_supply_config *cfg,1346 bool ws)1347{1348 struct device *dev;1349 struct power_supply *psy;1350 int rc;1351 1352 if (!desc || !desc->name || !desc->properties || !desc->num_properties)1353 return ERR_PTR(-EINVAL);1354 1355 if (!parent)1356 pr_warn("%s: Expected proper parent device for '%s'\n",1357 __func__, desc->name);1358 1359 psy = kzalloc(sizeof(*psy), GFP_KERNEL);1360 if (!psy)1361 return ERR_PTR(-ENOMEM);1362 1363 dev = &psy->dev;1364 1365 device_initialize(dev);1366 1367 dev->class = &power_supply_class;1368 dev->type = &power_supply_dev_type;1369 dev->parent = parent;1370 dev->release = power_supply_dev_release;1371 dev_set_drvdata(dev, psy);1372 psy->desc = desc;1373 if (cfg) {1374 dev->groups = cfg->attr_grp;1375 psy->drv_data = cfg->drv_data;1376 psy->of_node =1377 cfg->fwnode ? to_of_node(cfg->fwnode) : cfg->of_node;1378 dev->of_node = psy->of_node;1379 psy->supplied_to = cfg->supplied_to;1380 psy->num_supplicants = cfg->num_supplicants;1381 }1382 1383 rc = dev_set_name(dev, "%s", desc->name);1384 if (rc)1385 goto dev_set_name_failed;1386 1387 INIT_WORK(&psy->changed_work, power_supply_changed_work);1388 INIT_DELAYED_WORK(&psy->deferred_register_work,1389 power_supply_deferred_register_work);1390 1391 rc = power_supply_check_supplies(psy);1392 if (rc) {1393 dev_dbg(dev, "Not all required supplies found, defer probe\n");1394 goto check_supplies_failed;1395 }1396 1397 /*1398 * Expose constant battery info, if it is available. While there are1399 * some chargers accessing constant battery data, we only want to1400 * expose battery data to userspace for battery devices.1401 */1402 if (desc->type == POWER_SUPPLY_TYPE_BATTERY) {1403 rc = power_supply_get_battery_info(psy, &psy->battery_info);1404 if (rc && rc != -ENODEV && rc != -ENOENT)1405 goto check_supplies_failed;1406 }1407 1408 spin_lock_init(&psy->changed_lock);1409 rc = device_add(dev);1410 if (rc)1411 goto device_add_failed;1412 1413 rc = device_init_wakeup(dev, ws);1414 if (rc)1415 goto wakeup_init_failed;1416 1417 rc = psy_register_thermal(psy);1418 if (rc)1419 goto register_thermal_failed;1420 1421 rc = power_supply_create_triggers(psy);1422 if (rc)1423 goto create_triggers_failed;1424 1425 rc = power_supply_add_hwmon_sysfs(psy);1426 if (rc)1427 goto add_hwmon_sysfs_failed;1428 1429 /*1430 * Update use_cnt after any uevents (most notably from device_add()).1431 * We are here still during driver's probe but1432 * the power_supply_uevent() calls back driver's get_property1433 * method so:1434 * 1. Driver did not assigned the returned struct power_supply,1435 * 2. Driver could not finish initialization (anything in its probe1436 * after calling power_supply_register()).1437 */1438 atomic_inc(&psy->use_cnt);1439 psy->initialized = true;1440 1441 queue_delayed_work(system_power_efficient_wq,1442 &psy->deferred_register_work,1443 POWER_SUPPLY_DEFERRED_REGISTER_TIME);1444 1445 return psy;1446 1447add_hwmon_sysfs_failed:1448 power_supply_remove_triggers(psy);1449create_triggers_failed:1450 psy_unregister_thermal(psy);1451register_thermal_failed:1452wakeup_init_failed:1453 device_del(dev);1454device_add_failed:1455check_supplies_failed:1456dev_set_name_failed:1457 put_device(dev);1458 return ERR_PTR(rc);1459}1460 1461/**1462 * power_supply_register() - Register new power supply1463 * @parent: Device to be a parent of power supply's device, usually1464 * the device which probe function calls this1465 * @desc: Description of power supply, must be valid through whole1466 * lifetime of this power supply1467 * @cfg: Run-time specific configuration accessed during registering,1468 * may be NULL1469 *1470 * Return: A pointer to newly allocated power_supply on success1471 * or ERR_PTR otherwise.1472 * Use power_supply_unregister() on returned power_supply pointer to release1473 * resources.1474 */1475struct power_supply *__must_check power_supply_register(struct device *parent,1476 const struct power_supply_desc *desc,1477 const struct power_supply_config *cfg)1478{1479 return __power_supply_register(parent, desc, cfg, true);1480}1481EXPORT_SYMBOL_GPL(power_supply_register);1482 1483/**1484 * power_supply_register_no_ws() - Register new non-waking-source power supply1485 * @parent: Device to be a parent of power supply's device, usually1486 * the device which probe function calls this1487 * @desc: Description of power supply, must be valid through whole1488 * lifetime of this power supply1489 * @cfg: Run-time specific configuration accessed during registering,1490 * may be NULL1491 *1492 * Return: A pointer to newly allocated power_supply on success1493 * or ERR_PTR otherwise.1494 * Use power_supply_unregister() on returned power_supply pointer to release1495 * resources.1496 */1497struct power_supply *__must_check1498power_supply_register_no_ws(struct device *parent,1499 const struct power_supply_desc *desc,1500 const struct power_supply_config *cfg)1501{1502 return __power_supply_register(parent, desc, cfg, false);1503}1504EXPORT_SYMBOL_GPL(power_supply_register_no_ws);1505 1506static void devm_power_supply_release(struct device *dev, void *res)1507{1508 struct power_supply **psy = res;1509 1510 power_supply_unregister(*psy);1511}1512 1513/**1514 * devm_power_supply_register() - Register managed power supply1515 * @parent: Device to be a parent of power supply's device, usually1516 * the device which probe function calls this1517 * @desc: Description of power supply, must be valid through whole1518 * lifetime of this power supply1519 * @cfg: Run-time specific configuration accessed during registering,1520 * may be NULL1521 *1522 * Return: A pointer to newly allocated power_supply on success1523 * or ERR_PTR otherwise.1524 * The returned power_supply pointer will be automatically unregistered1525 * on driver detach.1526 */1527struct power_supply *__must_check1528devm_power_supply_register(struct device *parent,1529 const struct power_supply_desc *desc,1530 const struct power_supply_config *cfg)1531{1532 struct power_supply **ptr, *psy;1533 1534 ptr = devres_alloc(devm_power_supply_release, sizeof(*ptr), GFP_KERNEL);1535 1536 if (!ptr)1537 return ERR_PTR(-ENOMEM);1538 psy = __power_supply_register(parent, desc, cfg, true);1539 if (IS_ERR(psy)) {1540 devres_free(ptr);1541 } else {1542 *ptr = psy;1543 devres_add(parent, ptr);1544 }1545 return psy;1546}1547EXPORT_SYMBOL_GPL(devm_power_supply_register);1548 1549/**1550 * devm_power_supply_register_no_ws() - Register managed non-waking-source power supply1551 * @parent: Device to be a parent of power supply's device, usually1552 * the device which probe function calls this1553 * @desc: Description of power supply, must be valid through whole1554 * lifetime of this power supply1555 * @cfg: Run-time specific configuration accessed during registering,1556 * may be NULL1557 *1558 * Return: A pointer to newly allocated power_supply on success1559 * or ERR_PTR otherwise.1560 * The returned power_supply pointer will be automatically unregistered1561 * on driver detach.1562 */1563struct power_supply *__must_check1564devm_power_supply_register_no_ws(struct device *parent,1565 const struct power_supply_desc *desc,1566 const struct power_supply_config *cfg)1567{1568 struct power_supply **ptr, *psy;1569 1570 ptr = devres_alloc(devm_power_supply_release, sizeof(*ptr), GFP_KERNEL);1571 1572 if (!ptr)1573 return ERR_PTR(-ENOMEM);1574 psy = __power_supply_register(parent, desc, cfg, false);1575 if (IS_ERR(psy)) {1576 devres_free(ptr);1577 } else {1578 *ptr = psy;1579 devres_add(parent, ptr);1580 }1581 return psy;1582}1583EXPORT_SYMBOL_GPL(devm_power_supply_register_no_ws);1584 1585/**1586 * power_supply_unregister() - Remove this power supply from system1587 * @psy: Pointer to power supply to unregister1588 *1589 * Remove this power supply from the system. The resources of power supply1590 * will be freed here or on last power_supply_put() call.1591 */1592void power_supply_unregister(struct power_supply *psy)1593{1594 WARN_ON(atomic_dec_return(&psy->use_cnt));1595 psy->removing = true;1596 cancel_work_sync(&psy->changed_work);1597 cancel_delayed_work_sync(&psy->deferred_register_work);1598 sysfs_remove_link(&psy->dev.kobj, "powers");1599 power_supply_remove_hwmon_sysfs(psy);1600 power_supply_remove_triggers(psy);1601 psy_unregister_thermal(psy);1602 device_init_wakeup(&psy->dev, false);1603 device_unregister(&psy->dev);1604}1605EXPORT_SYMBOL_GPL(power_supply_unregister);1606 1607void *power_supply_get_drvdata(struct power_supply *psy)1608{1609 return psy->drv_data;1610}1611EXPORT_SYMBOL_GPL(power_supply_get_drvdata);1612 1613static int __init power_supply_class_init(void)1614{1615 power_supply_init_attrs();1616 return class_register(&power_supply_class);1617}1618 1619static void __exit power_supply_class_exit(void)1620{1621 class_unregister(&power_supply_class);1622}1623 1624subsys_initcall(power_supply_class_init);1625module_exit(power_supply_class_exit);1626 1627MODULE_DESCRIPTION("Universal power supply monitor class");1628MODULE_AUTHOR("Ian Molton <spyro@f2s.com>");1629MODULE_AUTHOR("Szabolcs Gyurko");1630MODULE_AUTHOR("Anton Vorontsov <cbou@mail.ru>");1631