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