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