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