linux/drivers/power/power_supply_core.c
<<
>>
Prefs
   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/err.h>
  19#include <linux/power_supply.h>
  20#include <linux/thermal.h>
  21#include "power_supply.h"
  22
  23/* exported for the APM Power driver, APM emulation */
  24struct class *power_supply_class;
  25EXPORT_SYMBOL_GPL(power_supply_class);
  26
  27static struct device_type power_supply_dev_type;
  28
  29static bool __power_supply_is_supplied_by(struct power_supply *supplier,
  30                                         struct power_supply *supply)
  31{
  32        int i;
  33
  34        if (!supply->supplied_from && !supplier->supplied_to)
  35                return false;
  36
  37        /* Support both supplied_to and supplied_from modes */
  38        if (supply->supplied_from) {
  39                if (!supplier->name)
  40                        return false;
  41                for (i = 0; i < supply->num_supplies; i++)
  42                        if (!strcmp(supplier->name, supply->supplied_from[i]))
  43                                return true;
  44        } else {
  45                if (!supply->name)
  46                        return false;
  47                for (i = 0; i < supplier->num_supplicants; i++)
  48                        if (!strcmp(supplier->supplied_to[i], supply->name))
  49                                return true;
  50        }
  51
  52        return false;
  53}
  54
  55static int __power_supply_changed_work(struct device *dev, void *data)
  56{
  57        struct power_supply *psy = (struct power_supply *)data;
  58        struct power_supply *pst = dev_get_drvdata(dev);
  59
  60        if (__power_supply_is_supplied_by(psy, pst)) {
  61                if (pst->external_power_changed)
  62                        pst->external_power_changed(pst);
  63        }
  64
  65        return 0;
  66}
  67
  68static void power_supply_changed_work(struct work_struct *work)
  69{
  70        unsigned long flags;
  71        struct power_supply *psy = container_of(work, struct power_supply,
  72                                                changed_work);
  73
  74        dev_dbg(psy->dev, "%s\n", __func__);
  75
  76        spin_lock_irqsave(&psy->changed_lock, flags);
  77        if (psy->changed) {
  78                psy->changed = false;
  79                spin_unlock_irqrestore(&psy->changed_lock, flags);
  80                class_for_each_device(power_supply_class, NULL, psy,
  81                                      __power_supply_changed_work);
  82                power_supply_update_leds(psy);
  83                kobject_uevent(&psy->dev->kobj, KOBJ_CHANGE);
  84                spin_lock_irqsave(&psy->changed_lock, flags);
  85        }
  86        /*
  87         * Dependent power supplies (e.g. battery) may have changed state
  88         * as a result of this event, so poll again and hold the
  89         * wakeup_source until all events are processed.
  90         */
  91        if (!psy->changed)
  92                pm_relax(psy->dev);
  93        spin_unlock_irqrestore(&psy->changed_lock, flags);
  94}
  95
  96void power_supply_changed(struct power_supply *psy)
  97{
  98        unsigned long flags;
  99
 100        dev_dbg(psy->dev, "%s\n", __func__);
 101
 102        spin_lock_irqsave(&psy->changed_lock, flags);
 103        psy->changed = true;
 104        pm_stay_awake(psy->dev);
 105        spin_unlock_irqrestore(&psy->changed_lock, flags);
 106        schedule_work(&psy->changed_work);
 107}
 108EXPORT_SYMBOL_GPL(power_supply_changed);
 109
 110#ifdef CONFIG_OF
 111#include <linux/of.h>
 112
 113static int __power_supply_populate_supplied_from(struct device *dev,
 114                                                 void *data)
 115{
 116        struct power_supply *psy = (struct power_supply *)data;
 117        struct power_supply *epsy = dev_get_drvdata(dev);
 118        struct device_node *np;
 119        int i = 0;
 120
 121        do {
 122                np = of_parse_phandle(psy->of_node, "power-supplies", i++);
 123                if (!np)
 124                        continue;
 125
 126                if (np == epsy->of_node) {
 127                        dev_info(psy->dev, "%s: Found supply : %s\n",
 128                                psy->name, epsy->name);
 129                        psy->supplied_from[i-1] = (char *)epsy->name;
 130                        psy->num_supplies++;
 131                        break;
 132                }
 133        } while (np);
 134
 135        return 0;
 136}
 137
 138static int power_supply_populate_supplied_from(struct power_supply *psy)
 139{
 140        int error;
 141
 142        error = class_for_each_device(power_supply_class, NULL, psy,
 143                                      __power_supply_populate_supplied_from);
 144
 145        dev_dbg(psy->dev, "%s %d\n", __func__, error);
 146
 147        return error;
 148}
 149
 150static int  __power_supply_find_supply_from_node(struct device *dev,
 151                                                 void *data)
 152{
 153        struct device_node *np = (struct device_node *)data;
 154        struct power_supply *epsy = dev_get_drvdata(dev);
 155
 156        /* return error breaks out of class_for_each_device loop */
 157        if (epsy->of_node == np)
 158                return -EINVAL;
 159
 160        return 0;
 161}
 162
 163static int power_supply_find_supply_from_node(struct device_node *supply_node)
 164{
 165        int error;
 166        struct device *dev;
 167        struct class_dev_iter iter;
 168
 169        /*
 170         * Use iterator to see if any other device is registered.
 171         * This is required since class_for_each_device returns 0
 172         * if there are no devices registered.
 173         */
 174        class_dev_iter_init(&iter, power_supply_class, NULL, NULL);
 175        dev = class_dev_iter_next(&iter);
 176
 177        if (!dev)
 178                return -EPROBE_DEFER;
 179
 180        /*
 181         * We have to treat the return value as inverted, because if
 182         * we return error on not found, then it won't continue looking.
 183         * So we trick it by returning error on success to stop looking
 184         * once the matching device is found.
 185         */
 186        error = class_for_each_device(power_supply_class, NULL, supply_node,
 187                                       __power_supply_find_supply_from_node);
 188
 189        return error ? 0 : -EPROBE_DEFER;
 190}
 191
 192static int power_supply_check_supplies(struct power_supply *psy)
 193{
 194        struct device_node *np;
 195        int cnt = 0;
 196
 197        /* If there is already a list honor it */
 198        if (psy->supplied_from && psy->num_supplies > 0)
 199                return 0;
 200
 201        /* No device node found, nothing to do */
 202        if (!psy->of_node)
 203                return 0;
 204
 205        do {
 206                int ret;
 207
 208                np = of_parse_phandle(psy->of_node, "power-supplies", cnt++);
 209                if (!np)
 210                        continue;
 211
 212                ret = power_supply_find_supply_from_node(np);
 213                if (ret) {
 214                        dev_dbg(psy->dev, "Failed to find supply, defer!\n");
 215                        return -EPROBE_DEFER;
 216                }
 217        } while (np);
 218
 219        /* All supplies found, allocate char ** array for filling */
 220        psy->supplied_from = devm_kzalloc(psy->dev, sizeof(psy->supplied_from),
 221                                          GFP_KERNEL);
 222        if (!psy->supplied_from) {
 223                dev_err(psy->dev, "Couldn't allocate memory for supply list\n");
 224                return -ENOMEM;
 225        }
 226
 227        *psy->supplied_from = devm_kzalloc(psy->dev, sizeof(char *) * cnt,
 228                                           GFP_KERNEL);
 229        if (!*psy->supplied_from) {
 230                dev_err(psy->dev, "Couldn't allocate memory for supply list\n");
 231                return -ENOMEM;
 232        }
 233
 234        return power_supply_populate_supplied_from(psy);
 235}
 236#else
 237static inline int power_supply_check_supplies(struct power_supply *psy)
 238{
 239        return 0;
 240}
 241#endif
 242
 243static int __power_supply_am_i_supplied(struct device *dev, void *data)
 244{
 245        union power_supply_propval ret = {0,};
 246        struct power_supply *psy = (struct power_supply *)data;
 247        struct power_supply *epsy = dev_get_drvdata(dev);
 248
 249        if (__power_supply_is_supplied_by(epsy, psy))
 250                if (!epsy->get_property(epsy, POWER_SUPPLY_PROP_ONLINE, &ret)) {
 251                        if (ret.intval)
 252                                return ret.intval;
 253                }
 254
 255        return 0;
 256}
 257
 258int power_supply_am_i_supplied(struct power_supply *psy)
 259{
 260        int error;
 261
 262        error = class_for_each_device(power_supply_class, NULL, psy,
 263                                      __power_supply_am_i_supplied);
 264
 265        dev_dbg(psy->dev, "%s %d\n", __func__, error);
 266
 267        return error;
 268}
 269EXPORT_SYMBOL_GPL(power_supply_am_i_supplied);
 270
 271static int __power_supply_is_system_supplied(struct device *dev, void *data)
 272{
 273        union power_supply_propval ret = {0,};
 274        struct power_supply *psy = dev_get_drvdata(dev);
 275        unsigned int *count = data;
 276
 277        (*count)++;
 278        if (psy->type != POWER_SUPPLY_TYPE_BATTERY) {
 279                if (psy->get_property(psy, POWER_SUPPLY_PROP_ONLINE, &ret))
 280                        return 0;
 281                if (ret.intval)
 282                        return ret.intval;
 283        }
 284        return 0;
 285}
 286
 287int power_supply_is_system_supplied(void)
 288{
 289        int error;
 290        unsigned int count = 0;
 291
 292        error = class_for_each_device(power_supply_class, NULL, &count,
 293                                      __power_supply_is_system_supplied);
 294
 295        /*
 296         * If no power class device was found at all, most probably we are
 297         * running on a desktop system, so assume we are on mains power.
 298         */
 299        if (count == 0)
 300                return 1;
 301
 302        return error;
 303}
 304EXPORT_SYMBOL_GPL(power_supply_is_system_supplied);
 305
 306int power_supply_set_battery_charged(struct power_supply *psy)
 307{
 308        if (psy->type == POWER_SUPPLY_TYPE_BATTERY && psy->set_charged) {
 309                psy->set_charged(psy);
 310                return 0;
 311        }
 312
 313        return -EINVAL;
 314}
 315EXPORT_SYMBOL_GPL(power_supply_set_battery_charged);
 316
 317static int power_supply_match_device_by_name(struct device *dev, const void *data)
 318{
 319        const char *name = data;
 320        struct power_supply *psy = dev_get_drvdata(dev);
 321
 322        return strcmp(psy->name, name) == 0;
 323}
 324
 325struct power_supply *power_supply_get_by_name(const char *name)
 326{
 327        struct device *dev = class_find_device(power_supply_class, NULL, name,
 328                                        power_supply_match_device_by_name);
 329
 330        return dev ? dev_get_drvdata(dev) : NULL;
 331}
 332EXPORT_SYMBOL_GPL(power_supply_get_by_name);
 333
 334int power_supply_powers(struct power_supply *psy, struct device *dev)
 335{
 336        return sysfs_create_link(&psy->dev->kobj, &dev->kobj, "powers");
 337}
 338EXPORT_SYMBOL_GPL(power_supply_powers);
 339
 340static void power_supply_dev_release(struct device *dev)
 341{
 342        pr_debug("device: '%s': %s\n", dev_name(dev), __func__);
 343        kfree(dev);
 344}
 345
 346#ifdef CONFIG_THERMAL
 347static int power_supply_read_temp(struct thermal_zone_device *tzd,
 348                unsigned long *temp)
 349{
 350        struct power_supply *psy;
 351        union power_supply_propval val;
 352        int ret;
 353
 354        WARN_ON(tzd == NULL);
 355        psy = tzd->devdata;
 356        ret = psy->get_property(psy, POWER_SUPPLY_PROP_TEMP, &val);
 357
 358        /* Convert tenths of degree Celsius to milli degree Celsius. */
 359        if (!ret)
 360                *temp = val.intval * 100;
 361
 362        return ret;
 363}
 364
 365static struct thermal_zone_device_ops psy_tzd_ops = {
 366        .get_temp = power_supply_read_temp,
 367};
 368
 369static int psy_register_thermal(struct power_supply *psy)
 370{
 371        int i;
 372
 373        /* Register battery zone device psy reports temperature */
 374        for (i = 0; i < psy->num_properties; i++) {
 375                if (psy->properties[i] == POWER_SUPPLY_PROP_TEMP) {
 376                        psy->tzd = thermal_zone_device_register(psy->name, 0, 0,
 377                                        psy, &psy_tzd_ops, NULL, 0, 0);
 378                        if (IS_ERR(psy->tzd))
 379                                return PTR_ERR(psy->tzd);
 380                        break;
 381                }
 382        }
 383        return 0;
 384}
 385
 386static void psy_unregister_thermal(struct power_supply *psy)
 387{
 388        if (IS_ERR_OR_NULL(psy->tzd))
 389                return;
 390        thermal_zone_device_unregister(psy->tzd);
 391}
 392
 393/* thermal cooling device callbacks */
 394static int ps_get_max_charge_cntl_limit(struct thermal_cooling_device *tcd,
 395                                        unsigned long *state)
 396{
 397        struct power_supply *psy;
 398        union power_supply_propval val;
 399        int ret;
 400
 401        psy = tcd->devdata;
 402        ret = psy->get_property(psy,
 403                POWER_SUPPLY_PROP_CHARGE_CONTROL_LIMIT_MAX, &val);
 404        if (!ret)
 405                *state = val.intval;
 406
 407        return ret;
 408}
 409
 410static int ps_get_cur_chrage_cntl_limit(struct thermal_cooling_device *tcd,
 411                                        unsigned long *state)
 412{
 413        struct power_supply *psy;
 414        union power_supply_propval val;
 415        int ret;
 416
 417        psy = tcd->devdata;
 418        ret = psy->get_property(psy,
 419                POWER_SUPPLY_PROP_CHARGE_CONTROL_LIMIT, &val);
 420        if (!ret)
 421                *state = val.intval;
 422
 423        return ret;
 424}
 425
 426static int ps_set_cur_charge_cntl_limit(struct thermal_cooling_device *tcd,
 427                                        unsigned long state)
 428{
 429        struct power_supply *psy;
 430        union power_supply_propval val;
 431        int ret;
 432
 433        psy = tcd->devdata;
 434        val.intval = state;
 435        ret = psy->set_property(psy,
 436                POWER_SUPPLY_PROP_CHARGE_CONTROL_LIMIT, &val);
 437
 438        return ret;
 439}
 440
 441static struct thermal_cooling_device_ops psy_tcd_ops = {
 442        .get_max_state = ps_get_max_charge_cntl_limit,
 443        .get_cur_state = ps_get_cur_chrage_cntl_limit,
 444        .set_cur_state = ps_set_cur_charge_cntl_limit,
 445};
 446
 447static int psy_register_cooler(struct power_supply *psy)
 448{
 449        int i;
 450
 451        /* Register for cooling device if psy can control charging */
 452        for (i = 0; i < psy->num_properties; i++) {
 453                if (psy->properties[i] ==
 454                                POWER_SUPPLY_PROP_CHARGE_CONTROL_LIMIT) {
 455                        psy->tcd = thermal_cooling_device_register(
 456                                                        (char *)psy->name,
 457                                                        psy, &psy_tcd_ops);
 458                        if (IS_ERR(psy->tcd))
 459                                return PTR_ERR(psy->tcd);
 460                        break;
 461                }
 462        }
 463        return 0;
 464}
 465
 466static void psy_unregister_cooler(struct power_supply *psy)
 467{
 468        if (IS_ERR_OR_NULL(psy->tcd))
 469                return;
 470        thermal_cooling_device_unregister(psy->tcd);
 471}
 472#else
 473static int psy_register_thermal(struct power_supply *psy)
 474{
 475        return 0;
 476}
 477
 478static void psy_unregister_thermal(struct power_supply *psy)
 479{
 480}
 481
 482static int psy_register_cooler(struct power_supply *psy)
 483{
 484        return 0;
 485}
 486
 487static void psy_unregister_cooler(struct power_supply *psy)
 488{
 489}
 490#endif
 491
 492int __power_supply_register(struct device *parent, struct power_supply *psy, bool ws)
 493{
 494        struct device *dev;
 495        int rc;
 496
 497        dev = kzalloc(sizeof(*dev), GFP_KERNEL);
 498        if (!dev)
 499                return -ENOMEM;
 500
 501        device_initialize(dev);
 502
 503        dev->class = power_supply_class;
 504        dev->type = &power_supply_dev_type;
 505        dev->parent = parent;
 506        dev->release = power_supply_dev_release;
 507        dev_set_drvdata(dev, psy);
 508        psy->dev = dev;
 509
 510        INIT_WORK(&psy->changed_work, power_supply_changed_work);
 511
 512        rc = power_supply_check_supplies(psy);
 513        if (rc) {
 514                dev_info(dev, "Not all required supplies found, defer probe\n");
 515                goto check_supplies_failed;
 516        }
 517
 518        spin_lock_init(&psy->changed_lock);
 519        rc = device_init_wakeup(dev, ws);
 520        if (rc)
 521                goto wakeup_init_failed;
 522
 523        rc = kobject_set_name(&dev->kobj, "%s", psy->name);
 524        if (rc)
 525                goto kobject_set_name_failed;
 526
 527        rc = device_add(dev);
 528        if (rc)
 529                goto device_add_failed;
 530
 531        rc = psy_register_thermal(psy);
 532        if (rc)
 533                goto register_thermal_failed;
 534
 535        rc = psy_register_cooler(psy);
 536        if (rc)
 537                goto register_cooler_failed;
 538
 539        rc = power_supply_create_triggers(psy);
 540        if (rc)
 541                goto create_triggers_failed;
 542
 543        power_supply_changed(psy);
 544
 545        goto success;
 546
 547create_triggers_failed:
 548        psy_unregister_cooler(psy);
 549register_cooler_failed:
 550        psy_unregister_thermal(psy);
 551register_thermal_failed:
 552wakeup_init_failed:
 553        device_del(dev);
 554kobject_set_name_failed:
 555device_add_failed:
 556check_supplies_failed:
 557        put_device(dev);
 558success:
 559        return rc;
 560}
 561
 562int power_supply_register(struct device *parent, struct power_supply *psy)
 563{
 564        return __power_supply_register(parent, psy, true);
 565}
 566EXPORT_SYMBOL_GPL(power_supply_register);
 567
 568int power_supply_register_no_ws(struct device *parent, struct power_supply *psy)
 569{
 570        return __power_supply_register(parent, psy, false);
 571}
 572EXPORT_SYMBOL_GPL(power_supply_register_no_ws);
 573
 574static void devm_power_supply_release(struct device *dev, void *res)
 575{
 576        struct power_supply **psy = res;
 577
 578        power_supply_unregister(*psy);
 579}
 580
 581int devm_power_supply_register(struct device *parent, struct power_supply *psy)
 582{
 583        struct power_supply **ptr = devres_alloc(devm_power_supply_release,
 584                                                 sizeof(*ptr), GFP_KERNEL);
 585        int ret;
 586
 587        if (!ptr)
 588                return -ENOMEM;
 589        ret = __power_supply_register(parent, psy, true);
 590        if (ret < 0)
 591                devres_free(ptr);
 592        else {
 593                *ptr = psy;
 594                devres_add(parent, ptr);
 595        }
 596        return ret;
 597}
 598EXPORT_SYMBOL_GPL(devm_power_supply_register);
 599
 600int devm_power_supply_register_no_ws(struct device *parent, struct power_supply *psy)
 601{
 602        struct power_supply **ptr = devres_alloc(devm_power_supply_release,
 603                                                 sizeof(*ptr), GFP_KERNEL);
 604        int ret;
 605
 606        if (!ptr)
 607                return -ENOMEM;
 608        ret = __power_supply_register(parent, psy, false);
 609        if (ret < 0)
 610                devres_free(ptr);
 611        else {
 612                *ptr = psy;
 613                devres_add(parent, ptr);
 614        }
 615        return ret;
 616}
 617EXPORT_SYMBOL_GPL(devm_power_supply_register_no_ws);
 618
 619void power_supply_unregister(struct power_supply *psy)
 620{
 621        cancel_work_sync(&psy->changed_work);
 622        sysfs_remove_link(&psy->dev->kobj, "powers");
 623        power_supply_remove_triggers(psy);
 624        psy_unregister_cooler(psy);
 625        psy_unregister_thermal(psy);
 626        device_init_wakeup(psy->dev, false);
 627        device_unregister(psy->dev);
 628}
 629EXPORT_SYMBOL_GPL(power_supply_unregister);
 630
 631static int __init power_supply_class_init(void)
 632{
 633        power_supply_class = class_create(THIS_MODULE, "power_supply");
 634
 635        if (IS_ERR(power_supply_class))
 636                return PTR_ERR(power_supply_class);
 637
 638        power_supply_class->dev_uevent = power_supply_uevent;
 639        power_supply_init_attrs(&power_supply_dev_type);
 640
 641        return 0;
 642}
 643
 644static void __exit power_supply_class_exit(void)
 645{
 646        class_destroy(power_supply_class);
 647}
 648
 649subsys_initcall(power_supply_class_init);
 650module_exit(power_supply_class_exit);
 651
 652MODULE_DESCRIPTION("Universal power supply monitor class");
 653MODULE_AUTHOR("Ian Molton <spyro@f2s.com>, "
 654              "Szabolcs Gyurko, "
 655              "Anton Vorontsov <cbou@mail.ru>");
 656MODULE_LICENSE("GPL");
 657