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 return err; 602 603 if (strcmp("simple-battery", value)) 604 return -ENODEV; 605 606 /* The property and field names below must correspond to elements 607 * in enum power_supply_property. For reasoning, see 608 * Documentation/power/power_supply_class.txt. 609 */ 610 611 of_property_read_u32(battery_np, "energy-full-design-microwatt-hours", 612 &info->energy_full_design_uwh); 613 of_property_read_u32(battery_np, "charge-full-design-microamp-hours", 614 &info->charge_full_design_uah); 615 of_property_read_u32(battery_np, "voltage-min-design-microvolt", 616 &info->voltage_min_design_uv); 617 of_property_read_u32(battery_np, "voltage-max-design-microvolt", 618 &info->voltage_max_design_uv); 619 of_property_read_u32(battery_np, "precharge-current-microamp", 620 &info->precharge_current_ua); 621 of_property_read_u32(battery_np, "charge-term-current-microamp", 622 &info->charge_term_current_ua); 623 of_property_read_u32(battery_np, "constant_charge_current_max_microamp", 624 &info->constant_charge_current_max_ua); 625 of_property_read_u32(battery_np, "constant_charge_voltage_max_microvolt", 626 &info->constant_charge_voltage_max_uv); 627 of_property_read_u32(battery_np, "factory-internal-resistance-micro-ohms", 628 &info->factory_internal_resistance_uohm); 629 630 len = of_property_count_u32_elems(battery_np, "ocv-capacity-celsius"); 631 if (len < 0 && len != -EINVAL) { 632 return len; 633 } else if (len > POWER_SUPPLY_OCV_TEMP_MAX) { 634 dev_err(&psy->dev, "Too many temperature values\n"); 635 return -EINVAL; 636 } else if (len > 0) { 637 of_property_read_u32_array(battery_np, "ocv-capacity-celsius", 638 info->ocv_temp, len); 639 } 640 641 for (index = 0; index < len; index++) { 642 struct power_supply_battery_ocv_table *table; 643 char *propname; 644 const __be32 *list; 645 int i, tab_len, size; 646 647 propname = kasprintf(GFP_KERNEL, "ocv-capacity-table-%d", index); 648 list = of_get_property(battery_np, propname, &size); 649 if (!list || !size) { 650 dev_err(&psy->dev, "failed to get %s\n", propname); 651 kfree(propname); 652 power_supply_put_battery_info(psy, info); 653 return -EINVAL; 654 } 655 656 kfree(propname); 657 tab_len = size / (2 * sizeof(__be32)); 658 info->ocv_table_size[index] = tab_len; 659 660 table = info->ocv_table[index] = 661 devm_kcalloc(&psy->dev, tab_len, sizeof(*table), GFP_KERNEL); 662 if (!info->ocv_table[index]) { 663 power_supply_put_battery_info(psy, info); 664 return -ENOMEM; 665 } 666 667 for (i = 0; i < tab_len; i++) { 668 table[i].ocv = be32_to_cpu(*list++); 669 table[i].capacity = be32_to_cpu(*list++); 670 } 671 } 672 673 return 0; 674 } 675 EXPORT_SYMBOL_GPL(power_supply_get_battery_info); 676 677 void power_supply_put_battery_info(struct power_supply *psy, 678 struct power_supply_battery_info *info) 679 { 680 int i; 681 682 for (i = 0; i < POWER_SUPPLY_OCV_TEMP_MAX; i++) { 683 if (info->ocv_table[i]) 684 devm_kfree(&psy->dev, info->ocv_table[i]); 685 } 686 } 687 EXPORT_SYMBOL_GPL(power_supply_put_battery_info); 688 689 /** 690 * power_supply_ocv2cap_simple() - find the battery capacity 691 * @table: Pointer to battery OCV lookup table 692 * @table_len: OCV table length 693 * @ocv: Current OCV value 694 * 695 * This helper function is used to look up battery capacity according to 696 * current OCV value from one OCV table, and the OCV table must be ordered 697 * descending. 698 * 699 * Return: the battery capacity. 700 */ 701 int power_supply_ocv2cap_simple(struct power_supply_battery_ocv_table *table, 702 int table_len, int ocv) 703 { 704 int i, cap, tmp; 705 706 for (i = 0; i < table_len; i++) 707 if (ocv > table[i].ocv) 708 break; 709 710 if (i > 0 && i < table_len) { 711 tmp = (table[i - 1].capacity - table[i].capacity) * 712 (ocv - table[i].ocv); 713 tmp /= table[i - 1].ocv - table[i].ocv; 714 cap = tmp + table[i].capacity; 715 } else if (i == 0) { 716 cap = table[0].capacity; 717 } else { 718 cap = table[table_len - 1].capacity; 719 } 720 721 return cap; 722 } 723 EXPORT_SYMBOL_GPL(power_supply_ocv2cap_simple); 724 725 struct power_supply_battery_ocv_table * 726 power_supply_find_ocv2cap_table(struct power_supply_battery_info *info, 727 int temp, int *table_len) 728 { 729 int best_temp_diff = INT_MAX, temp_diff; 730 u8 i, best_index = 0; 731 732 if (!info->ocv_table[0]) 733 return NULL; 734 735 for (i = 0; i < POWER_SUPPLY_OCV_TEMP_MAX; i++) { 736 temp_diff = abs(info->ocv_temp[i] - temp); 737 738 if (temp_diff < best_temp_diff) { 739 best_temp_diff = temp_diff; 740 best_index = i; 741 } 742 } 743 744 *table_len = info->ocv_table_size[best_index]; 745 return info->ocv_table[best_index]; 746 } 747 EXPORT_SYMBOL_GPL(power_supply_find_ocv2cap_table); 748 749 int power_supply_batinfo_ocv2cap(struct power_supply_battery_info *info, 750 int ocv, int temp) 751 { 752 struct power_supply_battery_ocv_table *table; 753 int table_len; 754 755 table = power_supply_find_ocv2cap_table(info, temp, &table_len); 756 if (!table) 757 return -EINVAL; 758 759 return power_supply_ocv2cap_simple(table, table_len, ocv); 760 } 761 EXPORT_SYMBOL_GPL(power_supply_batinfo_ocv2cap); 762 763 int power_supply_get_property(struct power_supply *psy, 764 enum power_supply_property psp, 765 union power_supply_propval *val) 766 { 767 if (atomic_read(&psy->use_cnt) <= 0) { 768 if (!psy->initialized) 769 return -EAGAIN; 770 return -ENODEV; 771 } 772 773 return psy->desc->get_property(psy, psp, val); 774 } 775 EXPORT_SYMBOL_GPL(power_supply_get_property); 776 777 int power_supply_set_property(struct power_supply *psy, 778 enum power_supply_property psp, 779 const union power_supply_propval *val) 780 { 781 if (atomic_read(&psy->use_cnt) <= 0 || !psy->desc->set_property) 782 return -ENODEV; 783 784 return psy->desc->set_property(psy, psp, val); 785 } 786 EXPORT_SYMBOL_GPL(power_supply_set_property); 787 788 int power_supply_property_is_writeable(struct power_supply *psy, 789 enum power_supply_property psp) 790 { 791 if (atomic_read(&psy->use_cnt) <= 0 || 792 !psy->desc->property_is_writeable) 793 return -ENODEV; 794 795 return psy->desc->property_is_writeable(psy, psp); 796 } 797 EXPORT_SYMBOL_GPL(power_supply_property_is_writeable); 798 799 void power_supply_external_power_changed(struct power_supply *psy) 800 { 801 if (atomic_read(&psy->use_cnt) <= 0 || 802 !psy->desc->external_power_changed) 803 return; 804 805 psy->desc->external_power_changed(psy); 806 } 807 EXPORT_SYMBOL_GPL(power_supply_external_power_changed); 808 809 int power_supply_powers(struct power_supply *psy, struct device *dev) 810 { 811 return sysfs_create_link(&psy->dev.kobj, &dev->kobj, "powers"); 812 } 813 EXPORT_SYMBOL_GPL(power_supply_powers); 814 815 static void power_supply_dev_release(struct device *dev) 816 { 817 struct power_supply *psy = to_power_supply(dev); 818 dev_dbg(dev, "%s\n", __func__); 819 kfree(psy); 820 } 821 822 int power_supply_reg_notifier(struct notifier_block *nb) 823 { 824 return atomic_notifier_chain_register(&power_supply_notifier, nb); 825 } 826 EXPORT_SYMBOL_GPL(power_supply_reg_notifier); 827 828 void power_supply_unreg_notifier(struct notifier_block *nb) 829 { 830 atomic_notifier_chain_unregister(&power_supply_notifier, nb); 831 } 832 EXPORT_SYMBOL_GPL(power_supply_unreg_notifier); 833 834 #ifdef CONFIG_THERMAL 835 static int power_supply_read_temp(struct thermal_zone_device *tzd, 836 int *temp) 837 { 838 struct power_supply *psy; 839 union power_supply_propval val; 840 int ret; 841 842 WARN_ON(tzd == NULL); 843 psy = tzd->devdata; 844 ret = power_supply_get_property(psy, POWER_SUPPLY_PROP_TEMP, &val); 845 if (ret) 846 return ret; 847 848 /* Convert tenths of degree Celsius to milli degree Celsius. */ 849 *temp = val.intval * 100; 850 851 return ret; 852 } 853 854 static struct thermal_zone_device_ops psy_tzd_ops = { 855 .get_temp = power_supply_read_temp, 856 }; 857 858 static int psy_register_thermal(struct power_supply *psy) 859 { 860 int i; 861 862 if (psy->desc->no_thermal) 863 return 0; 864 865 /* Register battery zone device psy reports temperature */ 866 for (i = 0; i < psy->desc->num_properties; i++) { 867 if (psy->desc->properties[i] == POWER_SUPPLY_PROP_TEMP) { 868 psy->tzd = thermal_zone_device_register(psy->desc->name, 869 0, 0, psy, &psy_tzd_ops, NULL, 0, 0); 870 return PTR_ERR_OR_ZERO(psy->tzd); 871 } 872 } 873 return 0; 874 } 875 876 static void psy_unregister_thermal(struct power_supply *psy) 877 { 878 if (IS_ERR_OR_NULL(psy->tzd)) 879 return; 880 thermal_zone_device_unregister(psy->tzd); 881 } 882 883 /* thermal cooling device callbacks */ 884 static int ps_get_max_charge_cntl_limit(struct thermal_cooling_device *tcd, 885 unsigned long *state) 886 { 887 struct power_supply *psy; 888 union power_supply_propval val; 889 int ret; 890 891 psy = tcd->devdata; 892 ret = power_supply_get_property(psy, 893 POWER_SUPPLY_PROP_CHARGE_CONTROL_LIMIT_MAX, &val); 894 if (ret) 895 return ret; 896 897 *state = val.intval; 898 899 return ret; 900 } 901 902 static int ps_get_cur_chrage_cntl_limit(struct thermal_cooling_device *tcd, 903 unsigned long *state) 904 { 905 struct power_supply *psy; 906 union power_supply_propval val; 907 int ret; 908 909 psy = tcd->devdata; 910 ret = power_supply_get_property(psy, 911 POWER_SUPPLY_PROP_CHARGE_CONTROL_LIMIT, &val); 912 if (ret) 913 return ret; 914 915 *state = val.intval; 916 917 return ret; 918 } 919 920 static int ps_set_cur_charge_cntl_limit(struct thermal_cooling_device *tcd, 921 unsigned long state) 922 { 923 struct power_supply *psy; 924 union power_supply_propval val; 925 int ret; 926 927 psy = tcd->devdata; 928 val.intval = state; 929 ret = psy->desc->set_property(psy, 930 POWER_SUPPLY_PROP_CHARGE_CONTROL_LIMIT, &val); 931 932 return ret; 933 } 934 935 static const struct thermal_cooling_device_ops psy_tcd_ops = { 936 .get_max_state = ps_get_max_charge_cntl_limit, 937 .get_cur_state = ps_get_cur_chrage_cntl_limit, 938 .set_cur_state = ps_set_cur_charge_cntl_limit, 939 }; 940 941 static int psy_register_cooler(struct power_supply *psy) 942 { 943 int i; 944 945 /* Register for cooling device if psy can control charging */ 946 for (i = 0; i < psy->desc->num_properties; i++) { 947 if (psy->desc->properties[i] == 948 POWER_SUPPLY_PROP_CHARGE_CONTROL_LIMIT) { 949 psy->tcd = thermal_cooling_device_register( 950 (char *)psy->desc->name, 951 psy, &psy_tcd_ops); 952 return PTR_ERR_OR_ZERO(psy->tcd); 953 } 954 } 955 return 0; 956 } 957 958 static void psy_unregister_cooler(struct power_supply *psy) 959 { 960 if (IS_ERR_OR_NULL(psy->tcd)) 961 return; 962 thermal_cooling_device_unregister(psy->tcd); 963 } 964 #else 965 static int psy_register_thermal(struct power_supply *psy) 966 { 967 return 0; 968 } 969 970 static void psy_unregister_thermal(struct power_supply *psy) 971 { 972 } 973 974 static int psy_register_cooler(struct power_supply *psy) 975 { 976 return 0; 977 } 978 979 static void psy_unregister_cooler(struct power_supply *psy) 980 { 981 } 982 #endif 983 984 static struct power_supply *__must_check 985 __power_supply_register(struct device *parent, 986 const struct power_supply_desc *desc, 987 const struct power_supply_config *cfg, 988 bool ws) 989 { 990 struct device *dev; 991 struct power_supply *psy; 992 int i, rc; 993 994 if (!parent) 995 pr_warn("%s: Expected proper parent device for '%s'\n", 996 __func__, desc->name); 997 998 if (!desc || !desc->name || !desc->properties || !desc->num_properties) 999 return ERR_PTR(-EINVAL); 1000 1001 for (i = 0; i < desc->num_properties; ++i) { 1002 if ((desc->properties[i] == POWER_SUPPLY_PROP_USB_TYPE) && 1003 (!desc->usb_types || !desc->num_usb_types)) 1004 return ERR_PTR(-EINVAL); 1005 } 1006 1007 psy = kzalloc(sizeof(*psy), GFP_KERNEL); 1008 if (!psy) 1009 return ERR_PTR(-ENOMEM); 1010 1011 dev = &psy->dev; 1012 1013 device_initialize(dev); 1014 1015 dev->class = power_supply_class; 1016 dev->type = &power_supply_dev_type; 1017 dev->parent = parent; 1018 dev->release = power_supply_dev_release; 1019 dev_set_drvdata(dev, psy); 1020 psy->desc = desc; 1021 if (cfg) { 1022 dev->groups = cfg->attr_grp; 1023 psy->drv_data = cfg->drv_data; 1024 psy->of_node = 1025 cfg->fwnode ? to_of_node(cfg->fwnode) : cfg->of_node; 1026 psy->supplied_to = cfg->supplied_to; 1027 psy->num_supplicants = cfg->num_supplicants; 1028 } 1029 1030 rc = dev_set_name(dev, "%s", desc->name); 1031 if (rc) 1032 goto dev_set_name_failed; 1033 1034 INIT_WORK(&psy->changed_work, power_supply_changed_work); 1035 INIT_DELAYED_WORK(&psy->deferred_register_work, 1036 power_supply_deferred_register_work); 1037 1038 rc = power_supply_check_supplies(psy); 1039 if (rc) { 1040 dev_info(dev, "Not all required supplies found, defer probe\n"); 1041 goto check_supplies_failed; 1042 } 1043 1044 spin_lock_init(&psy->changed_lock); 1045 rc = device_init_wakeup(dev, ws); 1046 if (rc) 1047 goto wakeup_init_failed; 1048 1049 rc = device_add(dev); 1050 if (rc) 1051 goto device_add_failed; 1052 1053 rc = psy_register_thermal(psy); 1054 if (rc) 1055 goto register_thermal_failed; 1056 1057 rc = psy_register_cooler(psy); 1058 if (rc) 1059 goto register_cooler_failed; 1060 1061 rc = power_supply_create_triggers(psy); 1062 if (rc) 1063 goto create_triggers_failed; 1064 1065 /* 1066 * Update use_cnt after any uevents (most notably from device_add()). 1067 * We are here still during driver's probe but 1068 * the power_supply_uevent() calls back driver's get_property 1069 * method so: 1070 * 1. Driver did not assigned the returned struct power_supply, 1071 * 2. Driver could not finish initialization (anything in its probe 1072 * after calling power_supply_register()). 1073 */ 1074 atomic_inc(&psy->use_cnt); 1075 psy->initialized = true; 1076 1077 queue_delayed_work(system_power_efficient_wq, 1078 &psy->deferred_register_work, 1079 POWER_SUPPLY_DEFERRED_REGISTER_TIME); 1080 1081 return psy; 1082 1083 create_triggers_failed: 1084 psy_unregister_cooler(psy); 1085 register_cooler_failed: 1086 psy_unregister_thermal(psy); 1087 register_thermal_failed: 1088 device_del(dev); 1089 device_add_failed: 1090 wakeup_init_failed: 1091 check_supplies_failed: 1092 dev_set_name_failed: 1093 put_device(dev); 1094 return ERR_PTR(rc); 1095 } 1096 1097 /** 1098 * power_supply_register() - Register new power supply 1099 * @parent: Device to be a parent of power supply's device, usually 1100 * the device which probe function calls this 1101 * @desc: Description of power supply, must be valid through whole 1102 * lifetime of this power supply 1103 * @cfg: Run-time specific configuration accessed during registering, 1104 * may be NULL 1105 * 1106 * Return: A pointer to newly allocated power_supply on success 1107 * or ERR_PTR otherwise. 1108 * Use power_supply_unregister() on returned power_supply pointer to release 1109 * resources. 1110 */ 1111 struct power_supply *__must_check power_supply_register(struct device *parent, 1112 const struct power_supply_desc *desc, 1113 const struct power_supply_config *cfg) 1114 { 1115 return __power_supply_register(parent, desc, cfg, true); 1116 } 1117 EXPORT_SYMBOL_GPL(power_supply_register); 1118 1119 /** 1120 * power_supply_register_no_ws() - Register new non-waking-source power supply 1121 * @parent: Device to be a parent of power supply's device, usually 1122 * the device which probe function calls this 1123 * @desc: Description of power supply, must be valid through whole 1124 * lifetime of this power supply 1125 * @cfg: Run-time specific configuration accessed during registering, 1126 * may be NULL 1127 * 1128 * Return: A pointer to newly allocated power_supply on success 1129 * or ERR_PTR otherwise. 1130 * Use power_supply_unregister() on returned power_supply pointer to release 1131 * resources. 1132 */ 1133 struct power_supply *__must_check 1134 power_supply_register_no_ws(struct device *parent, 1135 const struct power_supply_desc *desc, 1136 const struct power_supply_config *cfg) 1137 { 1138 return __power_supply_register(parent, desc, cfg, false); 1139 } 1140 EXPORT_SYMBOL_GPL(power_supply_register_no_ws); 1141 1142 static void devm_power_supply_release(struct device *dev, void *res) 1143 { 1144 struct power_supply **psy = res; 1145 1146 power_supply_unregister(*psy); 1147 } 1148 1149 /** 1150 * devm_power_supply_register() - Register managed power supply 1151 * @parent: Device to be a parent of power supply's device, usually 1152 * the device which probe function calls this 1153 * @desc: Description of power supply, must be valid through whole 1154 * lifetime of this power supply 1155 * @cfg: Run-time specific configuration accessed during registering, 1156 * may be NULL 1157 * 1158 * Return: A pointer to newly allocated power_supply on success 1159 * or ERR_PTR otherwise. 1160 * The returned power_supply pointer will be automatically unregistered 1161 * on driver detach. 1162 */ 1163 struct power_supply *__must_check 1164 devm_power_supply_register(struct device *parent, 1165 const struct power_supply_desc *desc, 1166 const struct power_supply_config *cfg) 1167 { 1168 struct power_supply **ptr, *psy; 1169 1170 ptr = devres_alloc(devm_power_supply_release, sizeof(*ptr), GFP_KERNEL); 1171 1172 if (!ptr) 1173 return ERR_PTR(-ENOMEM); 1174 psy = __power_supply_register(parent, desc, cfg, true); 1175 if (IS_ERR(psy)) { 1176 devres_free(ptr); 1177 } else { 1178 *ptr = psy; 1179 devres_add(parent, ptr); 1180 } 1181 return psy; 1182 } 1183 EXPORT_SYMBOL_GPL(devm_power_supply_register); 1184 1185 /** 1186 * devm_power_supply_register_no_ws() - Register managed non-waking-source power supply 1187 * @parent: Device to be a parent of power supply's device, usually 1188 * the device which probe function calls this 1189 * @desc: Description of power supply, must be valid through whole 1190 * lifetime of this power supply 1191 * @cfg: Run-time specific configuration accessed during registering, 1192 * may be NULL 1193 * 1194 * Return: A pointer to newly allocated power_supply on success 1195 * or ERR_PTR otherwise. 1196 * The returned power_supply pointer will be automatically unregistered 1197 * on driver detach. 1198 */ 1199 struct power_supply *__must_check 1200 devm_power_supply_register_no_ws(struct device *parent, 1201 const struct power_supply_desc *desc, 1202 const struct power_supply_config *cfg) 1203 { 1204 struct power_supply **ptr, *psy; 1205 1206 ptr = devres_alloc(devm_power_supply_release, sizeof(*ptr), GFP_KERNEL); 1207 1208 if (!ptr) 1209 return ERR_PTR(-ENOMEM); 1210 psy = __power_supply_register(parent, desc, cfg, false); 1211 if (IS_ERR(psy)) { 1212 devres_free(ptr); 1213 } else { 1214 *ptr = psy; 1215 devres_add(parent, ptr); 1216 } 1217 return psy; 1218 } 1219 EXPORT_SYMBOL_GPL(devm_power_supply_register_no_ws); 1220 1221 /** 1222 * power_supply_unregister() - Remove this power supply from system 1223 * @psy: Pointer to power supply to unregister 1224 * 1225 * Remove this power supply from the system. The resources of power supply 1226 * will be freed here or on last power_supply_put() call. 1227 */ 1228 void power_supply_unregister(struct power_supply *psy) 1229 { 1230 WARN_ON(atomic_dec_return(&psy->use_cnt)); 1231 psy->removing = true; 1232 cancel_work_sync(&psy->changed_work); 1233 cancel_delayed_work_sync(&psy->deferred_register_work); 1234 sysfs_remove_link(&psy->dev.kobj, "powers"); 1235 power_supply_remove_triggers(psy); 1236 psy_unregister_cooler(psy); 1237 psy_unregister_thermal(psy); 1238 device_init_wakeup(&psy->dev, false); 1239 device_unregister(&psy->dev); 1240 } 1241 EXPORT_SYMBOL_GPL(power_supply_unregister); 1242 1243 void *power_supply_get_drvdata(struct power_supply *psy) 1244 { 1245 return psy->drv_data; 1246 } 1247 EXPORT_SYMBOL_GPL(power_supply_get_drvdata); 1248 1249 static int __init power_supply_class_init(void) 1250 { 1251 power_supply_class = class_create(THIS_MODULE, "power_supply"); 1252 1253 if (IS_ERR(power_supply_class)) 1254 return PTR_ERR(power_supply_class); 1255 1256 power_supply_class->dev_uevent = power_supply_uevent; 1257 power_supply_init_attrs(&power_supply_dev_type); 1258 1259 return 0; 1260 } 1261 1262 static void __exit power_supply_class_exit(void) 1263 { 1264 class_destroy(power_supply_class); 1265 } 1266 1267 subsys_initcall(power_supply_class_init); 1268 module_exit(power_supply_class_exit); 1269 1270 MODULE_DESCRIPTION("Universal power supply monitor class"); 1271 MODULE_AUTHOR("Ian Molton <spyro@f2s.com>, " 1272 "Szabolcs Gyurko, " 1273 "Anton Vorontsov <cbou@mail.ru>"); 1274 MODULE_LICENSE("GPL"); 1275