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                        of_node_put(np);
 132                        break;
 133                }
 134                of_node_put(np);
 135        } while (np);
 136
 137        return 0;
 138}
 139
 140static int power_supply_populate_supplied_from(struct power_supply *psy)
 141{
 142        int error;
 143
 144        error = class_for_each_device(power_supply_class, NULL, psy,
 145                                      __power_supply_populate_supplied_from);
 146
 147        dev_dbg(psy->dev, "%s %d\n", __func__, error);
 148
 149        return error;
 150}
 151
 152static int  __power_supply_find_supply_from_node(struct device *dev,
 153                                                 void *data)
 154{
 155        struct device_node *np = (struct device_node *)data;
 156        struct power_supply *epsy = dev_get_drvdata(dev);
 157
 158        /* return error breaks out of class_for_each_device loop */
 159        if (epsy->of_node == np)
 160                return -EINVAL;
 161
 162        return 0;
 163}
 164
 165static int power_supply_find_supply_from_node(struct device_node *supply_node)
 166{
 167        int error;
 168        struct device *dev;
 169        struct class_dev_iter iter;
 170
 171        /*
 172         * Use iterator to see if any other device is registered.
 173         * This is required since class_for_each_device returns 0
 174         * if there are no devices registered.
 175         */
 176        class_dev_iter_init(&iter, power_supply_class, NULL, NULL);
 177        dev = class_dev_iter_next(&iter);
 178
 179        if (!dev)
 180                return -EPROBE_DEFER;
 181
 182        /*
 183         * We have to treat the return value as inverted, because if
 184         * we return error on not found, then it won't continue looking.
 185         * So we trick it by returning error on success to stop looking
 186         * once the matching device is found.
 187         */
 188        error = class_for_each_device(power_supply_class, NULL, supply_node,
 189                                       __power_supply_find_supply_from_node);
 190
 191        return error ? 0 : -EPROBE_DEFER;
 192}
 193
 194static int power_supply_check_supplies(struct power_supply *psy)
 195{
 196        struct device_node *np;
 197        int cnt = 0;
 198
 199        /* If there is already a list honor it */
 200        if (psy->supplied_from && psy->num_supplies > 0)
 201                return 0;
 202
 203        /* No device node found, nothing to do */
 204        if (!psy->of_node)
 205                return 0;
 206
 207        do {
 208                int ret;
 209
 210                np = of_parse_phandle(psy->of_node, "power-supplies", cnt++);
 211                if (!np)
 212                        continue;
 213
 214                ret = power_supply_find_supply_from_node(np);
 215                if (ret) {
 216                        dev_dbg(psy->dev, "Failed to find supply, defer!\n");
 217                        of_node_put(np);
 218                        return -EPROBE_DEFER;
 219                }
 220                of_node_put(np);
 221        } while (np);
 222
 223        /* All supplies found, allocate char ** array for filling */
 224        psy->supplied_from = devm_kzalloc(psy->dev, sizeof(psy->supplied_from),
 225                                          GFP_KERNEL);
 226        if (!psy->supplied_from) {
 227                dev_err(psy->dev, "Couldn't allocate memory for supply list\n");
 228                return -ENOMEM;
 229        }
 230
 231        *psy->supplied_from = devm_kzalloc(psy->dev, sizeof(char *) * cnt,
 232                                           GFP_KERNEL);
 233        if (!*psy->supplied_from) {
 234                dev_err(psy->dev, "Couldn't allocate memory for supply list\n");
 235                return -ENOMEM;
 236        }
 237
 238        return power_supply_populate_supplied_from(psy);
 239}
 240#else
 241static inline int power_supply_check_supplies(struct power_supply *psy)
 242{
 243        return 0;
 244}
 245#endif
 246
 247static int __power_supply_am_i_supplied(struct device *dev, void *data)
 248{
 249        union power_supply_propval ret = {0,};
 250        struct power_supply *psy = (struct power_supply *)data;
 251        struct power_supply *epsy = dev_get_drvdata(dev);
 252
 253        if (__power_supply_is_supplied_by(epsy, psy))
 254                if (!epsy->get_property(epsy, POWER_SUPPLY_PROP_ONLINE, &ret)) {
 255                        if (ret.intval)
 256                                return ret.intval;
 257                }
 258
 259        return 0;
 260}
 261
 262int power_supply_am_i_supplied(struct power_supply *psy)
 263{
 264        int error;
 265
 266        error = class_for_each_device(power_supply_class, NULL, psy,
 267                                      __power_supply_am_i_supplied);
 268
 269        dev_dbg(psy->dev, "%s %d\n", __func__, error);
 270
 271        return error;
 272}
 273EXPORT_SYMBOL_GPL(power_supply_am_i_supplied);
 274
 275static int __power_supply_is_system_supplied(struct device *dev, void *data)
 276{
 277        union power_supply_propval ret = {0,};
 278        struct power_supply *psy = dev_get_drvdata(dev);
 279        unsigned int *count = data;
 280
 281        (*count)++;
 282        if (psy->type != POWER_SUPPLY_TYPE_BATTERY) {
 283                if (psy->get_property(psy, POWER_SUPPLY_PROP_ONLINE, &ret))
 284                        return 0;
 285                if (ret.intval)
 286                        return ret.intval;
 287        }
 288        return 0;
 289}
 290
 291int power_supply_is_system_supplied(void)
 292{
 293        int error;
 294        unsigned int count = 0;
 295
 296        error = class_for_each_device(power_supply_class, NULL, &count,
 297                                      __power_supply_is_system_supplied);
 298
 299        /*
 300         * If no power class device was found at all, most probably we are
 301         * running on a desktop system, so assume we are on mains power.
 302         */
 303        if (count == 0)
 304                return 1;
 305
 306        return error;
 307}
 308EXPORT_SYMBOL_GPL(power_supply_is_system_supplied);
 309
 310int power_supply_set_battery_charged(struct power_supply *psy)
 311{
 312        if (psy->type == POWER_SUPPLY_TYPE_BATTERY && psy->set_charged) {
 313                psy->set_charged(psy);
 314                return 0;
 315        }
 316
 317        return -EINVAL;
 318}
 319EXPORT_SYMBOL_GPL(power_supply_set_battery_charged);
 320
 321static int power_supply_match_device_by_name(struct device *dev, const void *data)
 322{
 323        const char *name = data;
 324        struct power_supply *psy = dev_get_drvdata(dev);
 325
 326        return strcmp(psy->name, name) == 0;
 327}
 328
 329struct power_supply *power_supply_get_by_name(const char *name)
 330{
 331        struct device *dev = class_find_device(power_supply_class, NULL, name,
 332                                        power_supply_match_device_by_name);
 333
 334        return dev ? dev_get_drvdata(dev) : NULL;
 335}
 336EXPORT_SYMBOL_GPL(power_supply_get_by_name);
 337
 338int power_supply_powers(struct power_supply *psy, struct device *dev)
 339{
 340        return sysfs_create_link(&psy->dev->kobj, &dev->kobj, "powers");
 341}
 342EXPORT_SYMBOL_GPL(power_supply_powers);
 343
 344static void power_supply_dev_release(struct device *dev)
 345{
 346        pr_debug("device: '%s': %s\n", dev_name(dev), __func__);
 347        kfree(dev);
 348}
 349
 350#ifdef CONFIG_THERMAL
 351static int power_supply_read_temp(struct thermal_zone_device *tzd,
 352                unsigned long *temp)
 353{
 354        struct power_supply *psy;
 355        union power_supply_propval val;
 356        int ret;
 357
 358        WARN_ON(tzd == NULL);
 359        psy = tzd->devdata;
 360        ret = psy->get_property(psy, POWER_SUPPLY_PROP_TEMP, &val);
 361
 362        /* Convert tenths of degree Celsius to milli degree Celsius. */
 363        if (!ret)
 364                *temp = val.intval * 100;
 365
 366        return ret;
 367}
 368
 369static struct thermal_zone_device_ops psy_tzd_ops = {
 370        .get_temp = power_supply_read_temp,
 371};
 372
 373static int psy_register_thermal(struct power_supply *psy)
 374{
 375        int i;
 376
 377        /* Register battery zone device psy reports temperature */
 378        for (i = 0; i < psy->num_properties; i++) {
 379                if (psy->properties[i] == POWER_SUPPLY_PROP_TEMP) {
 380                        psy->tzd = thermal_zone_device_register(psy->name, 0, 0,
 381                                        psy, &psy_tzd_ops, NULL, 0, 0);
 382                        if (IS_ERR(psy->tzd))
 383                                return PTR_ERR(psy->tzd);
 384                        break;
 385                }
 386        }
 387        return 0;
 388}
 389
 390static void psy_unregister_thermal(struct power_supply *psy)
 391{
 392        if (IS_ERR_OR_NULL(psy->tzd))
 393                return;
 394        thermal_zone_device_unregister(psy->tzd);
 395}
 396
 397/* thermal cooling device callbacks */
 398static int ps_get_max_charge_cntl_limit(struct thermal_cooling_device *tcd,
 399                                        unsigned long *state)
 400{
 401        struct power_supply *psy;
 402        union power_supply_propval val;
 403        int ret;
 404
 405        psy = tcd->devdata;
 406        ret = psy->get_property(psy,
 407                POWER_SUPPLY_PROP_CHARGE_CONTROL_LIMIT_MAX, &val);
 408        if (!ret)
 409                *state = val.intval;
 410
 411        return ret;
 412}
 413
 414static int ps_get_cur_chrage_cntl_limit(struct thermal_cooling_device *tcd,
 415                                        unsigned long *state)
 416{
 417        struct power_supply *psy;
 418        union power_supply_propval val;
 419        int ret;
 420
 421        psy = tcd->devdata;
 422        ret = psy->get_property(psy,
 423                POWER_SUPPLY_PROP_CHARGE_CONTROL_LIMIT, &val);
 424        if (!ret)
 425                *state = val.intval;
 426
 427        return ret;
 428}
 429
 430static int ps_set_cur_charge_cntl_limit(struct thermal_cooling_device *tcd,
 431                                        unsigned long state)
 432{
 433        struct power_supply *psy;
 434        union power_supply_propval val;
 435        int ret;
 436
 437        psy = tcd->devdata;
 438        val.intval = state;
 439        ret = psy->set_property(psy,
 440                POWER_SUPPLY_PROP_CHARGE_CONTROL_LIMIT, &val);
 441
 442        return ret;
 443}
 444
 445static struct thermal_cooling_device_ops psy_tcd_ops = {
 446        .get_max_state = ps_get_max_charge_cntl_limit,
 447        .get_cur_state = ps_get_cur_chrage_cntl_limit,
 448        .set_cur_state = ps_set_cur_charge_cntl_limit,
 449};
 450
 451static int psy_register_cooler(struct power_supply *psy)
 452{
 453        int i;
 454
 455        /* Register for cooling device if psy can control charging */
 456        for (i = 0; i < psy->num_properties; i++) {
 457                if (psy->properties[i] ==
 458                                POWER_SUPPLY_PROP_CHARGE_CONTROL_LIMIT) {
 459                        psy->tcd = thermal_cooling_device_register(
 460                                                        (char *)psy->name,
 461                                                        psy, &psy_tcd_ops);
 462                        if (IS_ERR(psy->tcd))
 463                                return PTR_ERR(psy->tcd);
 464                        break;
 465                }
 466        }
 467        return 0;
 468}
 469
 470static void psy_unregister_cooler(struct power_supply *psy)
 471{
 472        if (IS_ERR_OR_NULL(psy->tcd))
 473                return;
 474        thermal_cooling_device_unregister(psy->tcd);
 475}
 476#else
 477static int psy_register_thermal(struct power_supply *psy)
 478{
 479        return 0;
 480}
 481
 482static void psy_unregister_thermal(struct power_supply *psy)
 483{
 484}
 485
 486static int psy_register_cooler(struct power_supply *psy)
 487{
 488        return 0;
 489}
 490
 491static void psy_unregister_cooler(struct power_supply *psy)
 492{
 493}
 494#endif
 495
 496int power_supply_register(struct device *parent, struct power_supply *psy)
 497{
 498        struct device *dev;
 499        int rc;
 500
 501        dev = kzalloc(sizeof(*dev), GFP_KERNEL);
 502        if (!dev)
 503                return -ENOMEM;
 504
 505        device_initialize(dev);
 506
 507        dev->class = power_supply_class;
 508        dev->type = &power_supply_dev_type;
 509        dev->parent = parent;
 510        dev->release = power_supply_dev_release;
 511        dev_set_drvdata(dev, psy);
 512        psy->dev = dev;
 513
 514        INIT_WORK(&psy->changed_work, power_supply_changed_work);
 515
 516        rc = power_supply_check_supplies(psy);
 517        if (rc) {
 518                dev_info(dev, "Not all required supplies found, defer probe\n");
 519                goto check_supplies_failed;
 520        }
 521
 522        spin_lock_init(&psy->changed_lock);
 523        rc = device_init_wakeup(dev, true);
 524        if (rc)
 525                goto wakeup_init_failed;
 526
 527        rc = kobject_set_name(&dev->kobj, "%s", psy->name);
 528        if (rc)
 529                goto kobject_set_name_failed;
 530
 531        rc = device_add(dev);
 532        if (rc)
 533                goto device_add_failed;
 534
 535        rc = psy_register_thermal(psy);
 536        if (rc)
 537                goto register_thermal_failed;
 538
 539        rc = psy_register_cooler(psy);
 540        if (rc)
 541                goto register_cooler_failed;
 542
 543        rc = power_supply_create_triggers(psy);
 544        if (rc)
 545                goto create_triggers_failed;
 546
 547        power_supply_changed(psy);
 548
 549        goto success;
 550
 551create_triggers_failed:
 552        psy_unregister_cooler(psy);
 553register_cooler_failed:
 554        psy_unregister_thermal(psy);
 555register_thermal_failed:
 556wakeup_init_failed:
 557        device_del(dev);
 558kobject_set_name_failed:
 559device_add_failed:
 560check_supplies_failed:
 561        put_device(dev);
 562success:
 563        return rc;
 564}
 565EXPORT_SYMBOL_GPL(power_supply_register);
 566
 567void power_supply_unregister(struct power_supply *psy)
 568{
 569        cancel_work_sync(&psy->changed_work);
 570        sysfs_remove_link(&psy->dev->kobj, "powers");
 571        power_supply_remove_triggers(psy);
 572        psy_unregister_cooler(psy);
 573        psy_unregister_thermal(psy);
 574        device_init_wakeup(psy->dev, false);
 575        device_unregister(psy->dev);
 576}
 577EXPORT_SYMBOL_GPL(power_supply_unregister);
 578
 579static int __init power_supply_class_init(void)
 580{
 581        power_supply_class = class_create(THIS_MODULE, "power_supply");
 582
 583        if (IS_ERR(power_supply_class))
 584                return PTR_ERR(power_supply_class);
 585
 586        power_supply_class->dev_uevent = power_supply_uevent;
 587        power_supply_init_attrs(&power_supply_dev_type);
 588
 589        return 0;
 590}
 591
 592static void __exit power_supply_class_exit(void)
 593{
 594        class_destroy(power_supply_class);
 595}
 596
 597subsys_initcall(power_supply_class_init);
 598module_exit(power_supply_class_exit);
 599
 600MODULE_DESCRIPTION("Universal power supply monitor class");
 601MODULE_AUTHOR("Ian Molton <spyro@f2s.com>, "
 602              "Szabolcs Gyurko, "
 603              "Anton Vorontsov <cbou@mail.ru>");
 604MODULE_LICENSE("GPL");
 605
lxr.linux.no kindly hosted by Redpill Linpro AS, provider of Linux consulting and operations services since 1995.