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