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