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