1 /* 2 * Universal power supply monitor class 3 * 4 * Copyright © 2007 Anton Vorontsov <cbou@mail.ru> 5 * Copyright © 2004 Szabolcs Gyurko 6 * Copyright © 2003 Ian Molton <spyro@f2s.com> 7 * 8 * Modified: 2004, Oct Szabolcs Gyurko 9 * 10 * You may use this code as per GPL version 2 11 */ 12 13 #include <linux/module.h> 14 #include <linux/types.h> 15 #include <linux/init.h> 16 #include <linux/slab.h> 17 #include <linux/delay.h> 18 #include <linux/device.h> 19 #include <linux/notifier.h> 20 #include <linux/err.h> 21 #include <linux/of.h> 22 #include <linux/power_supply.h> 23 #include <linux/property.h> 24 #include <linux/thermal.h> 25 #include "power_supply.h" 26 27 /* exported for the APM Power driver, APM emulation */ 28 struct class *power_supply_class; 29 EXPORT_SYMBOL_GPL(power_supply_class); 30 31 ATOMIC_NOTIFIER_HEAD(power_supply_notifier); 32 EXPORT_SYMBOL_GPL(power_supply_notifier); 33 34 static struct device_type power_supply_dev_type; 35 36 #define POWER_SUPPLY_DEFERRED_REGISTER_TIME msecs_to_jiffies(10) 37 38 static bool __power_supply_is_supplied_by(struct power_supply *supplier, 39 struct power_supply *supply) 40 { 41 int i; 42 43 if (!supply->supplied_from && !supplier->supplied_to) 44 return false; 45 46 /* Support both supplied_to and supplied_from modes */ 47 if (supply->supplied_from) { 48 if (!supplier->desc->name) 49 return false; 50 for (i = 0; i < supply->num_supplies; i++) 51 if (!strcmp(supplier->desc->name, supply->supplied_from[i])) 52 return true; 53 } else { 54 if (!supply->desc->name) 55 return false; 56 for (i = 0; i < supplier->num_supplicants; i++) 57 if (!strcmp(supplier->supplied_to[i], supply->desc->name)) 58 return true; 59 } 60 61 return false; 62 } 63 64 static int __power_supply_changed_work(struct device *dev, void *data) 65 { 66 struct power_supply *psy = data; 67 struct power_supply *pst = dev_get_drvdata(dev); 68 69 if (__power_supply_is_supplied_by(psy, pst)) { 70 if (pst->desc->external_power_changed) 71 pst->desc->external_power_changed(pst); 72 } 73 74 return 0; 75 } 76 77 static void power_supply_changed_work(struct work_struct *work) 78 { 79 unsigned long flags; 80 struct power_supply *psy = container_of(work, struct power_supply, 81 changed_work); 82 83 dev_dbg(&psy->dev, "%s\n", __func__); 84 85 spin_lock_irqsave(&psy->changed_lock, flags); 86 /* 87 * Check 'changed' here to avoid issues due to race between 88 * power_supply_changed() and this routine. In worst case 89 * power_supply_changed() can be called again just before we take above 90 * lock. During the first call of this routine we will mark 'changed' as 91 * false and it will stay false for the next call as well. 92 */ 93 if (likely(psy->changed)) { 94 psy->changed = false; 95 spin_unlock_irqrestore(&psy->changed_lock, flags); 96 class_for_each_device(power_supply_class, NULL, psy, 97 __power_supply_changed_work); 98 power_supply_update_leds(psy); 99 atomic_notifier_call_chain(&power_supply_notifier, 100 PSY_EVENT_PROP_CHANGED, psy); 101 kobject_uevent(&psy->dev.kobj, KOBJ_CHANGE); 102 spin_lock_irqsave(&psy->changed_lock, flags); 103 } 104 105 /* 106 * Hold the wakeup_source until all events are processed. 107 * power_supply_changed() might have called again and have set 'changed' 108 * to true. 109 */ 110 if (likely(!psy->changed)) 111 pm_relax(&psy->dev); 112 spin_unlock_irqrestore(&psy->changed_lock, flags); 113 } 114 115 void power_supply_changed(struct power_supply *psy) 116 { 117 unsigned long flags; 118 119 dev_dbg(&psy->dev, "%s\n", __func__); 120 121 spin_lock_irqsave(&psy->changed_lock, flags); 122 psy->changed = true; 123 pm_stay_awake(&psy->dev); 124 spin_unlock_irqrestore(&psy->changed_lock, flags); 125 schedule_work(&psy->changed_work); 126 } 127 EXPORT_SYMBOL_GPL(power_supply_changed); 128 129 /* 130 * Notify that power supply was registered after parent finished the probing. 131 * 132 * Often power supply is registered from driver's probe function. However 133 * calling power_supply_changed() directly from power_supply_register() 134 * would lead to execution of get_property() function provided by the driver 135 * too early - before the probe ends. 136 * 137 * Avoid that by waiting on parent's mutex. 138 */ 139 static void power_supply_deferred_register_work(struct work_struct *work) 140 { 141 struct power_supply *psy = container_of(work, struct power_supply, 142 deferred_register_work.work); 143 144 if (psy->dev.parent) { 145 while (!mutex_trylock(&psy->dev.parent->mutex)) { 146 if (psy->removing) 147 return; 148 msleep(10); 149 } 150 } 151 152 power_supply_changed(psy); 153 154 if (psy->dev.parent) 155 mutex_unlock(&psy->dev.parent->mutex); 156 } 157 158 #ifdef CONFIG_OF 159 static int __power_supply_populate_supplied_from(struct device *dev, 160 void *data) 161 { 162 struct power_supply *psy = data; 163 struct power_supply *epsy = dev_get_drvdata(dev); 164 struct device_node *np; 165 int i = 0; 166 167 do { 168 np = of_parse_phandle(psy->of_node, "power-supplies", i++); 169 if (!np) 170 break; 171 172 if (np == epsy->of_node) { 173 dev_info(&psy->dev, "%s: Found supply : %s\n", 174 psy->desc->name, epsy->desc->name); 175 psy->supplied_from[i-1] = (char *)epsy->desc->name; 176 psy->num_supplies++; 177 of_node_put(np); 178 break; 179 } 180 of_node_put(np); 181 } while (np); 182 183 return 0; 184 } 185 186 static int power_supply_populate_supplied_from(struct power_supply *psy) 187 { 188 int error; 189 190 error = class_for_each_device(power_supply_class, NULL, psy, 191 __power_supply_populate_supplied_from); 192 193 dev_dbg(&psy->dev, "%s %d\n", __func__, error); 194 195 return error; 196 } 197 198 static int __power_supply_find_supply_from_node(struct device *dev, 199 void *data) 200 { 201 struct device_node *np = data; 202 struct power_supply *epsy = dev_get_drvdata(dev); 203 204 /* returning non-zero breaks out of class_for_each_device loop */ 205 if (epsy->of_node == np) 206 return 1; 207 208 return 0; 209 } 210 211 static int power_supply_find_supply_from_node(struct device_node *supply_node) 212 { 213 int error; 214 215 /* 216 * class_for_each_device() either returns its own errors or values 217 * returned by __power_supply_find_supply_from_node(). 218 * 219 * __power_supply_find_supply_from_node() will return 0 (no match) 220 * or 1 (match). 221 * 222 * We return 0 if class_for_each_device() returned 1, -EPROBE_DEFER if 223 * it returned 0, or error as returned by it. 224 */ 225 error = class_for_each_device(power_supply_class, NULL, supply_node, 226 __power_supply_find_supply_from_node); 227 228 return error ? (error == 1 ? 0 : error) : -EPROBE_DEFER; 229 } 230 231 static int power_supply_check_supplies(struct power_supply *psy) 232 { 233 struct device_node *np; 234 int cnt = 0; 235 236 /* If there is already a list honor it */ 237 if (psy->supplied_from && psy->num_supplies > 0) 238 return 0; 239 240 /* No device node found, nothing to do */ 241 if (!psy->of_node) 242 return 0; 243 244 do { 245 int ret; 246 247 np = of_parse_phandle(psy->of_node, "power-supplies", cnt++); 248 if (!np) 249 break; 250 251 ret = power_supply_find_supply_from_node(np); 252 of_node_put(np); 253 254 if (ret) { 255 dev_dbg(&psy->dev, "Failed to find supply!\n"); 256 return ret; 257 } 258 } while (np); 259 260 /* Missing valid "power-supplies" entries */ 261 if (cnt == 1) 262 return 0; 263 264 /* All supplies found, allocate char ** array for filling */ 265 psy->supplied_from = devm_kzalloc(&psy->dev, sizeof(psy->supplied_from), 266 GFP_KERNEL); 267 if (!psy->supplied_from) 268 return -ENOMEM; 269 270 *psy->supplied_from = devm_kcalloc(&psy->dev, 271 cnt - 1, sizeof(char *), 272 GFP_KERNEL); 273 if (!*psy->supplied_from) 274 return -ENOMEM; 275 276 return power_supply_populate_supplied_from(psy); 277 } 278 #else 279 static int power_supply_check_supplies(struct power_supply *psy) 280 { 281 int nval, ret; 282 283 if (!psy->dev.parent) 284 return 0; 285 286 nval = device_property_read_string_array(psy->dev.parent, 287 "supplied-from", NULL, 0); 288 if (nval <= 0) 289 return 0; 290 291 psy->supplied_from = devm_kmalloc_array(&psy->dev, nval, 292 sizeof(char *), GFP_KERNEL); 293 if (!psy->supplied_from) 294 return -ENOMEM; 295 296 ret = device_property_read_string_array(psy->dev.parent, 297 "supplied-from", (const char **)psy->supplied_from, nval); 298 if (ret < 0) 299 return ret; 300 301 psy->num_supplies = nval; 302 303 return 0; 304 } 305 #endif 306 307 struct psy_am_i_supplied_data { 308 struct power_supply *psy; 309 unsigned int count; 310 }; 311 312 static int __power_supply_am_i_supplied(struct device *dev, void *_data) 313 { 314 union power_supply_propval ret = {0,}; 315 struct power_supply *epsy = dev_get_drvdata(dev); 316 struct psy_am_i_supplied_data *data = _data; 317 318 if (__power_supply_is_supplied_by(epsy, data->psy)) { 319 data->count++; 320 if (!epsy->desc->get_property(epsy, POWER_SUPPLY_PROP_ONLINE, 321 &ret)) 322 return ret.intval; 323 } 324 325 return 0; 326 } 327 328 int power_supply_am_i_supplied(struct power_supply *psy) 329 { 330 struct psy_am_i_supplied_data data = { psy, 0 }; 331 int error; 332 333 error = class_for_each_device(power_supply_class, NULL, &data, 334 __power_supply_am_i_supplied); 335 336 dev_dbg(&psy->dev, "%s count %u err %d\n", __func__, data.count, error); 337 338 if (data.count == 0) 339 return -ENODEV; 340 341 return error; 342 } 343 EXPORT_SYMBOL_GPL(power_supply_am_i_supplied); 344 345 static int __power_supply_is_system_supplied(struct device *dev, void *data) 346 { 347 union power_supply_propval ret = {0,}; 348 struct power_supply *psy = dev_get_drvdata(dev); 349 unsigned int *count = data; 350 351 (*count)++; 352 if (psy->desc->type != POWER_SUPPLY_TYPE_BATTERY) 353 if (!psy->desc->get_property(psy, POWER_SUPPLY_PROP_ONLINE, 354 &ret)) 355 return ret.intval; 356 357 return 0; 358 } 359 360 int power_supply_is_system_supplied(void) 361 { 362 int error; 363 unsigned int count = 0; 364 365 error = class_for_each_device(power_supply_class, NULL, &count, 366 __power_supply_is_system_supplied); 367 368 /* 369 * If no power class device was found at all, most probably we are 370 * running on a desktop system, so assume we are on mains power. 371 */ 372 if (count == 0) 373 return 1; 374 375 return error; 376 } 377 EXPORT_SYMBOL_GPL(power_supply_is_system_supplied); 378 379 static int __power_supply_get_supplier_max_current(struct device *dev, 380 void *data) 381 { 382 union power_supply_propval ret = {0,}; 383 struct power_supply *epsy = dev_get_drvdata(dev); 384 struct power_supply *psy = data; 385 386 if (__power_supply_is_supplied_by(epsy, psy)) 387 if (!epsy->desc->get_property(epsy, 388 POWER_SUPPLY_PROP_CURRENT_MAX, 389 &ret)) 390 return ret.intval; 391 392 return 0; 393 } 394 395 int power_supply_set_input_current_limit_from_supplier(struct power_supply *psy) 396 { 397 union power_supply_propval val = {0,}; 398 int curr; 399 400 if (!psy->desc->set_property) 401 return -EINVAL; 402 403 /* 404 * This function is not intended for use with a supply with multiple 405 * suppliers, we simply pick the first supply to report a non 0 406 * max-current. 407 */ 408 curr = class_for_each_device(power_supply_class, NULL, psy, 409 __power_supply_get_supplier_max_current); 410 if (curr <= 0) 411 return (curr == 0) ? -ENODEV : curr; 412 413 val.intval = curr; 414 415 return psy->desc->set_property(psy, 416 POWER_SUPPLY_PROP_INPUT_CURRENT_LIMIT, &val); 417 } 418 EXPORT_SYMBOL_GPL(power_supply_set_input_current_limit_from_supplier); 419 420 int power_supply_set_battery_charged(struct power_supply *psy) 421 { 422 if (atomic_read(&psy->use_cnt) >= 0 && 423 psy->desc->type == POWER_SUPPLY_TYPE_BATTERY && 424 psy->desc->set_charged) { 425 psy->desc->set_charged(psy); 426 return 0; 427 } 428 429 return -EINVAL; 430 } 431 EXPORT_SYMBOL_GPL(power_supply_set_battery_charged); 432 433 static int power_supply_match_device_by_name(struct device *dev, const void *data) 434 { 435 const char *name = data; 436 struct power_supply *psy = dev_get_drvdata(dev); 437 438 return strcmp(psy->desc->name, name) == 0; 439 } 440 441 /** 442 * power_supply_get_by_name() - Search for a power supply and returns its ref 443 * @name: Power supply name to fetch 444 * 445 * If power supply was found, it increases reference count for the 446 * internal power supply's device. The user should power_supply_put() 447 * after usage. 448 * 449 * Return: On success returns a reference to a power supply with 450 * matching name equals to @name, a NULL otherwise. 451 */ 452 struct power_supply *power_supply_get_by_name(const char *name) 453 { 454 struct power_supply *psy = NULL; 455 struct device *dev = class_find_device(power_supply_class, NULL, name, 456 power_supply_match_device_by_name); 457 458 if (dev) { 459 psy = dev_get_drvdata(dev); 460 atomic_inc(&psy->use_cnt); 461 } 462 463 return psy; 464 } 465 EXPORT_SYMBOL_GPL(power_supply_get_by_name); 466 467 /** 468 * power_supply_put() - Drop reference obtained with power_supply_get_by_name 469 * @psy: Reference to put 470 * 471 * The reference to power supply should be put before unregistering 472 * the power supply. 473 */ 474 void power_supply_put(struct power_supply *psy) 475 { 476 might_sleep(); 477 478 atomic_dec(&psy->use_cnt); 479 put_device(&psy->dev); 480 } 481 EXPORT_SYMBOL_GPL(power_supply_put); 482 483 #ifdef CONFIG_OF 484 static int power_supply_match_device_node(struct device *dev, const void *data) 485 { 486 return dev->parent && dev->parent->of_node == data; 487 } 488 489 /** 490 * power_supply_get_by_phandle() - Search for a power supply and returns its ref 491 * @np: Pointer to device node holding phandle property 492 * @property: Name of property holding a power supply name 493 * 494 * If power supply was found, it increases reference count for the 495 * internal power supply's device. The user should power_supply_put() 496 * after usage. 497 * 498 * Return: On success returns a reference to a power supply with 499 * matching name equals to value under @property, NULL or ERR_PTR otherwise. 500 */ 501 struct power_supply *power_supply_get_by_phandle(struct device_node *np, 502 const char *property) 503 { 504 struct device_node *power_supply_np; 505 struct power_supply *psy = NULL; 506 struct device *dev; 507 508 power_supply_np = of_parse_phandle(np, property, 0); 509 if (!power_supply_np) 510 return ERR_PTR(-ENODEV); 511 512 dev = class_find_device(power_supply_class, NULL, power_supply_np, 513 power_supply_match_device_node); 514 515 of_node_put(power_supply_np); 516 517 if (dev) { 518 psy = dev_get_drvdata(dev); 519 atomic_inc(&psy->use_cnt); 520 } 521 522 return psy; 523 } 524 EXPORT_SYMBOL_GPL(power_supply_get_by_phandle); 525 526 static void devm_power_supply_put(struct device *dev, void *res) 527 { 528 struct power_supply **psy = res; 529 530 power_supply_put(*psy); 531 } 532 533 /** 534 * devm_power_supply_get_by_phandle() - Resource managed version of 535 * power_supply_get_by_phandle() 536 * @dev: Pointer to device holding phandle property 537 * @property: Name of property holding a power supply phandle 538 * 539 * Return: On success returns a reference to a power supply with 540 * matching name equals to value under @property, NULL or ERR_PTR otherwise. 541 */ 542 struct power_supply *devm_power_supply_get_by_phandle(struct device *dev, 543 const char *property) 544 { 545 struct power_supply **ptr, *psy; 546 547 if (!dev->of_node) 548 return ERR_PTR(-ENODEV); 549 550 ptr = devres_alloc(devm_power_supply_put, sizeof(*ptr), GFP_KERNEL); 551 if (!ptr) 552 return ERR_PTR(-ENOMEM); 553 554 psy = power_supply_get_by_phandle(dev->of_node, property); 555 if (IS_ERR_OR_NULL(psy)) { 556 devres_free(ptr); 557 } else { 558 *ptr = psy; 559 devres_add(dev, ptr); 560 } 561 return psy; 562 } 563 EXPORT_SYMBOL_GPL(devm_power_supply_get_by_phandle); 564 #endif /* CONFIG_OF */ 565 566 int power_supply_get_battery_info(struct power_supply *psy, 567 struct power_supply_battery_info *info) 568 { 569 struct device_node *battery_np; 570 const char *value; 571 int err, len, index; 572 573 info->energy_full_design_uwh = -EINVAL; 574 info->charge_full_design_uah = -EINVAL; 575 info->voltage_min_design_uv = -EINVAL; 576 info->voltage_max_design_uv = -EINVAL; 577 info->precharge_current_ua = -EINVAL; 578 info->charge_term_current_ua = -EINVAL; 579 info->constant_charge_current_max_ua = -EINVAL; 580 info->constant_charge_voltage_max_uv = -EINVAL; 581 info->factory_internal_resistance_uohm = -EINVAL; 582 583 for (index = 0; index < POWER_SUPPLY_OCV_TEMP_MAX; index++) { 584 info->ocv_table[index] = NULL; 585 info->ocv_temp[index] = -EINVAL; 586 info->ocv_table_size[index] = -EINVAL; 587 } 588 589 if (!psy->of_node) { 590 dev_warn(&psy->dev, "%s currently only supports devicetree\n", 591 __func__); 592 return -ENXIO; 593 } 594 595 battery_np = of_parse_phandle(psy->of_node, "monitored-battery", 0); 596 if (!battery_np) 597 return -ENODEV; 598 599 err = of_property_read_string(battery_np, "compatible", &value); 600 if (err) 601 goto out_put_node; 602 603 if (strcmp("simple-battery", value)) { 604 err = -ENODEV; 605 goto out_put_node; 606 } 607 608 /* The property and field names below must correspond to elements 609 * in enum power_supply_property. For reasoning, see 610 * Documentation/power/power_supply_class.txt. 611 */ 612 613 of_property_read_u32(battery_np, "energy-full-design-microwatt-hours", 614 &info->energy_full_design_uwh); 615 of_property_read_u32(battery_np, "charge-full-design-microamp-hours", 616 &info->charge_full_design_uah); 617 of_property_read_u32(battery_np, "voltage-min-design-microvolt", 618 &info->voltage_min_design_uv); 619 of_property_read_u32(battery_np, "voltage-max-design-microvolt", 620 &info->voltage_max_design_uv); 621 of_property_read_u32(battery_np, "precharge-current-microamp", 622 &info->precharge_current_ua); 623 of_property_read_u32(battery_np, "charge-term-current-microamp", 624 &info->charge_term_current_ua); 625 of_property_read_u32(battery_np, "constant-charge-current-max-microamp", 626 &info->constant_charge_current_max_ua); 627 of_property_read_u32(battery_np, "constant-charge-voltage-max-microvolt", 628 &info->constant_charge_voltage_max_uv); 629 of_property_read_u32(battery_np, "factory-internal-resistance-micro-ohms", 630 &info->factory_internal_resistance_uohm); 631 632 len = of_property_count_u32_elems(battery_np, "ocv-capacity-celsius"); 633 if (len < 0 && len != -EINVAL) { 634 err = len; 635 goto out_put_node; 636 } else if (len > POWER_SUPPLY_OCV_TEMP_MAX) { 637 dev_err(&psy->dev, "Too many temperature values\n"); 638 err = -EINVAL; 639 goto out_put_node; 640 } else if (len > 0) { 641 of_property_read_u32_array(battery_np, "ocv-capacity-celsius", 642 info->ocv_temp, len); 643 } 644 645 for (index = 0; index < len; index++) { 646 struct power_supply_battery_ocv_table *table; 647 char *propname; 648 const __be32 *list; 649 int i, tab_len, size; 650 651 propname = kasprintf(GFP_KERNEL, "ocv-capacity-table-%d", index); 652 list = of_get_property(battery_np, propname, &size); 653 if (!list || !size) { 654 dev_err(&psy->dev, "failed to get %s\n", propname); 655 kfree(propname); 656 power_supply_put_battery_info(psy, info); 657 err = -EINVAL; 658 goto out_put_node; 659 } 660 661 kfree(propname); 662 tab_len = size / (2 * sizeof(__be32)); 663 info->ocv_table_size[index] = tab_len; 664 665 table = info->ocv_table[index] = 666 devm_kcalloc(&psy->dev, tab_len, sizeof(*table), GFP_KERNEL); 667 if (!info->ocv_table[index]) { 668 power_supply_put_battery_info(psy, info); 669 err = -ENOMEM; 670 goto out_put_node; 671 } 672 673 for (i = 0; i < tab_len; i++) { 674 table[i].ocv = be32_to_cpu(*list); 675 list++; 676 table[i].capacity = be32_to_cpu(*list); 677 list++; 678 } 679 } 680 681 out_put_node: 682 of_node_put(battery_np); 683 return err; 684 } 685 EXPORT_SYMBOL_GPL(power_supply_get_battery_info); 686 687 void power_supply_put_battery_info(struct power_supply *psy, 688 struct power_supply_battery_info *info) 689 { 690 int i; 691 692 for (i = 0; i < POWER_SUPPLY_OCV_TEMP_MAX; i++) { 693 if (info->ocv_table[i]) 694 devm_kfree(&psy->dev, info->ocv_table[i]); 695 } 696 } 697 EXPORT_SYMBOL_GPL(power_supply_put_battery_info); 698 699 /** 700 * power_supply_ocv2cap_simple() - find the battery capacity 701 * @table: Pointer to battery OCV lookup table 702 * @table_len: OCV table length 703 * @ocv: Current OCV value 704 * 705 * This helper function is used to look up battery capacity according to 706 * current OCV value from one OCV table, and the OCV table must be ordered 707 * descending. 708 * 709 * Return: the battery capacity. 710 */ 711 int power_supply_ocv2cap_simple(struct power_supply_battery_ocv_table *table, 712 int table_len, int ocv) 713 { 714 int i, cap, tmp; 715 716 for (i = 0; i < table_len; i++) 717 if (ocv > table[i].ocv) 718 break; 719 720 if (i > 0 && i < table_len) { 721 tmp = (table[i - 1].capacity - table[i].capacity) * 722 (ocv - table[i].ocv); 723 tmp /= table[i - 1].ocv - table[i].ocv; 724 cap = tmp + table[i].capacity; 725 } else if (i == 0) { 726 cap = table[0].capacity; 727 } else { 728 cap = table[table_len - 1].capacity; 729 } 730 731 return cap; 732 } 733 EXPORT_SYMBOL_GPL(power_supply_ocv2cap_simple); 734 735 struct power_supply_battery_ocv_table * 736 power_supply_find_ocv2cap_table(struct power_supply_battery_info *info, 737 int temp, int *table_len) 738 { 739 int best_temp_diff = INT_MAX, temp_diff; 740 u8 i, best_index = 0; 741 742 if (!info->ocv_table[0]) 743 return NULL; 744 745 for (i = 0; i < POWER_SUPPLY_OCV_TEMP_MAX; i++) { 746 temp_diff = abs(info->ocv_temp[i] - temp); 747 748 if (temp_diff < best_temp_diff) { 749 best_temp_diff = temp_diff; 750 best_index = i; 751 } 752 } 753 754 *table_len = info->ocv_table_size[best_index]; 755 return info->ocv_table[best_index]; 756 } 757 EXPORT_SYMBOL_GPL(power_supply_find_ocv2cap_table); 758 759 int power_supply_batinfo_ocv2cap(struct power_supply_battery_info *info, 760 int ocv, int temp) 761 { 762 struct power_supply_battery_ocv_table *table; 763 int table_len; 764 765 table = power_supply_find_ocv2cap_table(info, temp, &table_len); 766 if (!table) 767 return -EINVAL; 768 769 return power_supply_ocv2cap_simple(table, table_len, ocv); 770 } 771 EXPORT_SYMBOL_GPL(power_supply_batinfo_ocv2cap); 772 773 int power_supply_get_property(struct power_supply *psy, 774 enum power_supply_property psp, 775 union power_supply_propval *val) 776 { 777 if (atomic_read(&psy->use_cnt) <= 0) { 778 if (!psy->initialized) 779 return -EAGAIN; 780 return -ENODEV; 781 } 782 783 return psy->desc->get_property(psy, psp, val); 784 } 785 EXPORT_SYMBOL_GPL(power_supply_get_property); 786 787 int power_supply_set_property(struct power_supply *psy, 788 enum power_supply_property psp, 789 const union power_supply_propval *val) 790 { 791 if (atomic_read(&psy->use_cnt) <= 0 || !psy->desc->set_property) 792 return -ENODEV; 793 794 return psy->desc->set_property(psy, psp, val); 795 } 796 EXPORT_SYMBOL_GPL(power_supply_set_property); 797 798 int power_supply_property_is_writeable(struct power_supply *psy, 799 enum power_supply_property psp) 800 { 801 if (atomic_read(&psy->use_cnt) <= 0 || 802 !psy->desc->property_is_writeable) 803 return -ENODEV; 804 805 return psy->desc->property_is_writeable(psy, psp); 806 } 807 EXPORT_SYMBOL_GPL(power_supply_property_is_writeable); 808 809 void power_supply_external_power_changed(struct power_supply *psy) 810 { 811 if (atomic_read(&psy->use_cnt) <= 0 || 812 !psy->desc->external_power_changed) 813 return; 814 815 psy->desc->external_power_changed(psy); 816 } 817 EXPORT_SYMBOL_GPL(power_supply_external_power_changed); 818 819 int power_supply_powers(struct power_supply *psy, struct device *dev) 820 { 821 return sysfs_create_link(&psy->dev.kobj, &dev->kobj, "powers"); 822 } 823 EXPORT_SYMBOL_GPL(power_supply_powers); 824 825 static void power_supply_dev_release(struct device *dev) 826 { 827 struct power_supply *psy = to_power_supply(dev); 828 dev_dbg(dev, "%s\n", __func__); 829 kfree(psy); 830 } 831 832 int power_supply_reg_notifier(struct notifier_block *nb) 833 { 834 return atomic_notifier_chain_register(&power_supply_notifier, nb); 835 } 836 EXPORT_SYMBOL_GPL(power_supply_reg_notifier); 837 838 void power_supply_unreg_notifier(struct notifier_block *nb) 839 { 840 atomic_notifier_chain_unregister(&power_supply_notifier, nb); 841 } 842 EXPORT_SYMBOL_GPL(power_supply_unreg_notifier); 843 844 #ifdef CONFIG_THERMAL 845 static int power_supply_read_temp(struct thermal_zone_device *tzd, 846 int *temp) 847 { 848 struct power_supply *psy; 849 union power_supply_propval val; 850 int ret; 851 852 WARN_ON(tzd == NULL); 853 psy = tzd->devdata; 854 ret = power_supply_get_property(psy, POWER_SUPPLY_PROP_TEMP, &val); 855 if (ret) 856 return ret; 857 858 /* Convert tenths of degree Celsius to milli degree Celsius. */ 859 *temp = val.intval * 100; 860 861 return ret; 862 } 863 864 static struct thermal_zone_device_ops psy_tzd_ops = { 865 .get_temp = power_supply_read_temp, 866 }; 867 868 static int psy_register_thermal(struct power_supply *psy) 869 { 870 int i; 871 872 if (psy->desc->no_thermal) 873 return 0; 874 875 /* Register battery zone device psy reports temperature */ 876 for (i = 0; i < psy->desc->num_properties; i++) { 877 if (psy->desc->properties[i] == POWER_SUPPLY_PROP_TEMP) { 878 psy->tzd = thermal_zone_device_register(psy->desc->name, 879 0, 0, psy, &psy_tzd_ops, NULL, 0, 0); 880 return PTR_ERR_OR_ZERO(psy->tzd); 881 } 882 } 883 return 0; 884 } 885 886 static void psy_unregister_thermal(struct power_supply *psy) 887 { 888 if (IS_ERR_OR_NULL(psy->tzd)) 889 return; 890 thermal_zone_device_unregister(psy->tzd); 891 } 892 893 /* thermal cooling device callbacks */ 894 static int ps_get_max_charge_cntl_limit(struct thermal_cooling_device *tcd, 895 unsigned long *state) 896 { 897 struct power_supply *psy; 898 union power_supply_propval val; 899 int ret; 900 901 psy = tcd->devdata; 902 ret = power_supply_get_property(psy, 903 POWER_SUPPLY_PROP_CHARGE_CONTROL_LIMIT_MAX, &val); 904 if (ret) 905 return ret; 906 907 *state = val.intval; 908 909 return ret; 910 } 911 912 static int ps_get_cur_charge_cntl_limit(struct thermal_cooling_device *tcd, 913 unsigned long *state) 914 { 915 struct power_supply *psy; 916 union power_supply_propval val; 917 int ret; 918 919 psy = tcd->devdata; 920 ret = power_supply_get_property(psy, 921 POWER_SUPPLY_PROP_CHARGE_CONTROL_LIMIT, &val); 922 if (ret) 923 return ret; 924 925 *state = val.intval; 926 927 return ret; 928 } 929 930 static int ps_set_cur_charge_cntl_limit(struct thermal_cooling_device *tcd, 931 unsigned long state) 932 { 933 struct power_supply *psy; 934 union power_supply_propval val; 935 int ret; 936 937 psy = tcd->devdata; 938 val.intval = state; 939 ret = psy->desc->set_property(psy, 940 POWER_SUPPLY_PROP_CHARGE_CONTROL_LIMIT, &val); 941 942 return ret; 943 } 944 945 static const struct thermal_cooling_device_ops psy_tcd_ops = { 946 .get_max_state = ps_get_max_charge_cntl_limit, 947 .get_cur_state = ps_get_cur_charge_cntl_limit, 948 .set_cur_state = ps_set_cur_charge_cntl_limit, 949 }; 950 951 static int psy_register_cooler(struct power_supply *psy) 952 { 953 int i; 954 955 /* Register for cooling device if psy can control charging */ 956 for (i = 0; i < psy->desc->num_properties; i++) { 957 if (psy->desc->properties[i] == 958 POWER_SUPPLY_PROP_CHARGE_CONTROL_LIMIT) { 959 psy->tcd = thermal_cooling_device_register( 960 (char *)psy->desc->name, 961 psy, &psy_tcd_ops); 962 return PTR_ERR_OR_ZERO(psy->tcd); 963 } 964 } 965 return 0; 966 } 967 968 static void psy_unregister_cooler(struct power_supply *psy) 969 { 970 if (IS_ERR_OR_NULL(psy->tcd)) 971 return; 972 thermal_cooling_device_unregister(psy->tcd); 973 } 974 #else 975 static int psy_register_thermal(struct power_supply *psy) 976 { 977 return 0; 978 } 979 980 static void psy_unregister_thermal(struct power_supply *psy) 981 { 982 } 983 984 static int psy_register_cooler(struct power_supply *psy) 985 { 986 return 0; 987 } 988 989 static void psy_unregister_cooler(struct power_supply *psy) 990 { 991 } 992 #endif 993 994 static struct power_supply *__must_check 995 __power_supply_register(struct device *parent, 996 const struct power_supply_desc *desc, 997 const struct power_supply_config *cfg, 998 bool ws) 999 { 1000 struct device *dev; 1001 struct power_supply *psy; 1002 int i, rc; 1003 1004 if (!parent) 1005 pr_warn("%s: Expected proper parent device for '%s'\n", 1006 __func__, desc->name); 1007 1008 if (!desc || !desc->name || !desc->properties || !desc->num_properties) 1009 return ERR_PTR(-EINVAL); 1010 1011 for (i = 0; i < desc->num_properties; ++i) { 1012 if ((desc->properties[i] == POWER_SUPPLY_PROP_USB_TYPE) && 1013 (!desc->usb_types || !desc->num_usb_types)) 1014 return ERR_PTR(-EINVAL); 1015 } 1016 1017 psy = kzalloc(sizeof(*psy), GFP_KERNEL); 1018 if (!psy) 1019 return ERR_PTR(-ENOMEM); 1020 1021 dev = &psy->dev; 1022 1023 device_initialize(dev); 1024 1025 dev->class = power_supply_class; 1026 dev->type = &power_supply_dev_type; 1027 dev->parent = parent; 1028 dev->release = power_supply_dev_release; 1029 dev_set_drvdata(dev, psy); 1030 psy->desc = desc; 1031 if (cfg) { 1032 dev->groups = cfg->attr_grp; 1033 psy->drv_data = cfg->drv_data; 1034 psy->of_node = 1035 cfg->fwnode ? to_of_node(cfg->fwnode) : cfg->of_node; 1036 psy->supplied_to = cfg->supplied_to; 1037 psy->num_supplicants = cfg->num_supplicants; 1038 } 1039 1040 rc = dev_set_name(dev, "%s", desc->name); 1041 if (rc) 1042 goto dev_set_name_failed; 1043 1044 INIT_WORK(&psy->changed_work, power_supply_changed_work); 1045 INIT_DELAYED_WORK(&psy->deferred_register_work, 1046 power_supply_deferred_register_work); 1047 1048 rc = power_supply_check_supplies(psy); 1049 if (rc) { 1050 dev_info(dev, "Not all required supplies found, defer probe\n"); 1051 goto check_supplies_failed; 1052 } 1053 1054 spin_lock_init(&psy->changed_lock); 1055 rc = device_init_wakeup(dev, ws); 1056 if (rc) 1057 goto wakeup_init_failed; 1058 1059 rc = device_add(dev); 1060 if (rc) 1061 goto device_add_failed; 1062 1063 rc = psy_register_thermal(psy); 1064 if (rc) 1065 goto register_thermal_failed; 1066 1067 rc = psy_register_cooler(psy); 1068 if (rc) 1069 goto register_cooler_failed; 1070 1071 rc = power_supply_create_triggers(psy); 1072 if (rc) 1073 goto create_triggers_failed; 1074 1075 /* 1076 * Update use_cnt after any uevents (most notably from device_add()). 1077 * We are here still during driver's probe but 1078 * the power_supply_uevent() calls back driver's get_property 1079 * method so: 1080 * 1. Driver did not assigned the returned struct power_supply, 1081 * 2. Driver could not finish initialization (anything in its probe 1082 * after calling power_supply_register()). 1083 */ 1084 atomic_inc(&psy->use_cnt); 1085 psy->initialized = true; 1086 1087 queue_delayed_work(system_power_efficient_wq, 1088 &psy->deferred_register_work, 1089 POWER_SUPPLY_DEFERRED_REGISTER_TIME); 1090 1091 return psy; 1092 1093 create_triggers_failed: 1094 psy_unregister_cooler(psy); 1095 register_cooler_failed: 1096 psy_unregister_thermal(psy); 1097 register_thermal_failed: 1098 device_del(dev); 1099 device_add_failed: 1100 wakeup_init_failed: 1101 check_supplies_failed: 1102 dev_set_name_failed: 1103 put_device(dev); 1104 return ERR_PTR(rc); 1105 } 1106 1107 /** 1108 * power_supply_register() - Register new power supply 1109 * @parent: Device to be a parent of power supply's device, usually 1110 * the device which probe function calls this 1111 * @desc: Description of power supply, must be valid through whole 1112 * lifetime of this power supply 1113 * @cfg: Run-time specific configuration accessed during registering, 1114 * may be NULL 1115 * 1116 * Return: A pointer to newly allocated power_supply on success 1117 * or ERR_PTR otherwise. 1118 * Use power_supply_unregister() on returned power_supply pointer to release 1119 * resources. 1120 */ 1121 struct power_supply *__must_check power_supply_register(struct device *parent, 1122 const struct power_supply_desc *desc, 1123 const struct power_supply_config *cfg) 1124 { 1125 return __power_supply_register(parent, desc, cfg, true); 1126 } 1127 EXPORT_SYMBOL_GPL(power_supply_register); 1128 1129 /** 1130 * power_supply_register_no_ws() - Register new non-waking-source power supply 1131 * @parent: Device to be a parent of power supply's device, usually 1132 * the device which probe function calls this 1133 * @desc: Description of power supply, must be valid through whole 1134 * lifetime of this power supply 1135 * @cfg: Run-time specific configuration accessed during registering, 1136 * may be NULL 1137 * 1138 * Return: A pointer to newly allocated power_supply on success 1139 * or ERR_PTR otherwise. 1140 * Use power_supply_unregister() on returned power_supply pointer to release 1141 * resources. 1142 */ 1143 struct power_supply *__must_check 1144 power_supply_register_no_ws(struct device *parent, 1145 const struct power_supply_desc *desc, 1146 const struct power_supply_config *cfg) 1147 { 1148 return __power_supply_register(parent, desc, cfg, false); 1149 } 1150 EXPORT_SYMBOL_GPL(power_supply_register_no_ws); 1151 1152 static void devm_power_supply_release(struct device *dev, void *res) 1153 { 1154 struct power_supply **psy = res; 1155 1156 power_supply_unregister(*psy); 1157 } 1158 1159 /** 1160 * devm_power_supply_register() - Register managed power supply 1161 * @parent: Device to be a parent of power supply's device, usually 1162 * the device which probe function calls this 1163 * @desc: Description of power supply, must be valid through whole 1164 * lifetime of this power supply 1165 * @cfg: Run-time specific configuration accessed during registering, 1166 * may be NULL 1167 * 1168 * Return: A pointer to newly allocated power_supply on success 1169 * or ERR_PTR otherwise. 1170 * The returned power_supply pointer will be automatically unregistered 1171 * on driver detach. 1172 */ 1173 struct power_supply *__must_check 1174 devm_power_supply_register(struct device *parent, 1175 const struct power_supply_desc *desc, 1176 const struct power_supply_config *cfg) 1177 { 1178 struct power_supply **ptr, *psy; 1179 1180 ptr = devres_alloc(devm_power_supply_release, sizeof(*ptr), GFP_KERNEL); 1181 1182 if (!ptr) 1183 return ERR_PTR(-ENOMEM); 1184 psy = __power_supply_register(parent, desc, cfg, true); 1185 if (IS_ERR(psy)) { 1186 devres_free(ptr); 1187 } else { 1188 *ptr = psy; 1189 devres_add(parent, ptr); 1190 } 1191 return psy; 1192 } 1193 EXPORT_SYMBOL_GPL(devm_power_supply_register); 1194 1195 /** 1196 * devm_power_supply_register_no_ws() - Register managed non-waking-source power supply 1197 * @parent: Device to be a parent of power supply's device, usually 1198 * the device which probe function calls this 1199 * @desc: Description of power supply, must be valid through whole 1200 * lifetime of this power supply 1201 * @cfg: Run-time specific configuration accessed during registering, 1202 * may be NULL 1203 * 1204 * Return: A pointer to newly allocated power_supply on success 1205 * or ERR_PTR otherwise. 1206 * The returned power_supply pointer will be automatically unregistered 1207 * on driver detach. 1208 */ 1209 struct power_supply *__must_check 1210 devm_power_supply_register_no_ws(struct device *parent, 1211 const struct power_supply_desc *desc, 1212 const struct power_supply_config *cfg) 1213 { 1214 struct power_supply **ptr, *psy; 1215 1216 ptr = devres_alloc(devm_power_supply_release, sizeof(*ptr), GFP_KERNEL); 1217 1218 if (!ptr) 1219 return ERR_PTR(-ENOMEM); 1220 psy = __power_supply_register(parent, desc, cfg, false); 1221 if (IS_ERR(psy)) { 1222 devres_free(ptr); 1223 } else { 1224 *ptr = psy; 1225 devres_add(parent, ptr); 1226 } 1227 return psy; 1228 } 1229 EXPORT_SYMBOL_GPL(devm_power_supply_register_no_ws); 1230 1231 /** 1232 * power_supply_unregister() - Remove this power supply from system 1233 * @psy: Pointer to power supply to unregister 1234 * 1235 * Remove this power supply from the system. The resources of power supply 1236 * will be freed here or on last power_supply_put() call. 1237 */ 1238 void power_supply_unregister(struct power_supply *psy) 1239 { 1240 WARN_ON(atomic_dec_return(&psy->use_cnt)); 1241 psy->removing = true; 1242 cancel_work_sync(&psy->changed_work); 1243 cancel_delayed_work_sync(&psy->deferred_register_work); 1244 sysfs_remove_link(&psy->dev.kobj, "powers"); 1245 power_supply_remove_triggers(psy); 1246 psy_unregister_cooler(psy); 1247 psy_unregister_thermal(psy); 1248 device_init_wakeup(&psy->dev, false); 1249 device_unregister(&psy->dev); 1250 } 1251 EXPORT_SYMBOL_GPL(power_supply_unregister); 1252 1253 void *power_supply_get_drvdata(struct power_supply *psy) 1254 { 1255 return psy->drv_data; 1256 } 1257 EXPORT_SYMBOL_GPL(power_supply_get_drvdata); 1258 1259 static int __init power_supply_class_init(void) 1260 { 1261 power_supply_class = class_create(THIS_MODULE, "power_supply"); 1262 1263 if (IS_ERR(power_supply_class)) 1264 return PTR_ERR(power_supply_class); 1265 1266 power_supply_class->dev_uevent = power_supply_uevent; 1267 power_supply_init_attrs(&power_supply_dev_type); 1268 1269 return 0; 1270 } 1271 1272 static void __exit power_supply_class_exit(void) 1273 { 1274 class_destroy(power_supply_class); 1275 } 1276 1277 subsys_initcall(power_supply_class_init); 1278 module_exit(power_supply_class_exit); 1279 1280 MODULE_DESCRIPTION("Universal power supply monitor class"); 1281 MODULE_AUTHOR("Ian Molton <spyro@f2s.com>, " 1282 "Szabolcs Gyurko, " 1283 "Anton Vorontsov <cbou@mail.ru>"); 1284 MODULE_LICENSE("GPL"); 1285