linux/drivers/regulator/core.c
<<
>>
Prefs
   1// SPDX-License-Identifier: GPL-2.0-or-later
   2//
   3// core.c  --  Voltage/Current Regulator framework.
   4//
   5// Copyright 2007, 2008 Wolfson Microelectronics PLC.
   6// Copyright 2008 SlimLogic Ltd.
   7//
   8// Author: Liam Girdwood <lrg@slimlogic.co.uk>
   9
  10#include <linux/kernel.h>
  11#include <linux/init.h>
  12#include <linux/debugfs.h>
  13#include <linux/device.h>
  14#include <linux/slab.h>
  15#include <linux/async.h>
  16#include <linux/err.h>
  17#include <linux/mutex.h>
  18#include <linux/suspend.h>
  19#include <linux/delay.h>
  20#include <linux/gpio/consumer.h>
  21#include <linux/of.h>
  22#include <linux/regmap.h>
  23#include <linux/regulator/of_regulator.h>
  24#include <linux/regulator/consumer.h>
  25#include <linux/regulator/coupler.h>
  26#include <linux/regulator/driver.h>
  27#include <linux/regulator/machine.h>
  28#include <linux/module.h>
  29
  30#define CREATE_TRACE_POINTS
  31#include <trace/events/regulator.h>
  32
  33#include "dummy.h"
  34#include "internal.h"
  35
  36#define rdev_crit(rdev, fmt, ...)                                       \
  37        pr_crit("%s: " fmt, rdev_get_name(rdev), ##__VA_ARGS__)
  38#define rdev_err(rdev, fmt, ...)                                        \
  39        pr_err("%s: " fmt, rdev_get_name(rdev), ##__VA_ARGS__)
  40#define rdev_warn(rdev, fmt, ...)                                       \
  41        pr_warn("%s: " fmt, rdev_get_name(rdev), ##__VA_ARGS__)
  42#define rdev_info(rdev, fmt, ...)                                       \
  43        pr_info("%s: " fmt, rdev_get_name(rdev), ##__VA_ARGS__)
  44#define rdev_dbg(rdev, fmt, ...)                                        \
  45        pr_debug("%s: " fmt, rdev_get_name(rdev), ##__VA_ARGS__)
  46
  47static DEFINE_WW_CLASS(regulator_ww_class);
  48static DEFINE_MUTEX(regulator_nesting_mutex);
  49static DEFINE_MUTEX(regulator_list_mutex);
  50static LIST_HEAD(regulator_map_list);
  51static LIST_HEAD(regulator_ena_gpio_list);
  52static LIST_HEAD(regulator_supply_alias_list);
  53static LIST_HEAD(regulator_coupler_list);
  54static bool has_full_constraints;
  55
  56static struct dentry *debugfs_root;
  57
  58/*
  59 * struct regulator_map
  60 *
  61 * Used to provide symbolic supply names to devices.
  62 */
  63struct regulator_map {
  64        struct list_head list;
  65        const char *dev_name;   /* The dev_name() for the consumer */
  66        const char *supply;
  67        struct regulator_dev *regulator;
  68};
  69
  70/*
  71 * struct regulator_enable_gpio
  72 *
  73 * Management for shared enable GPIO pin
  74 */
  75struct regulator_enable_gpio {
  76        struct list_head list;
  77        struct gpio_desc *gpiod;
  78        u32 enable_count;       /* a number of enabled shared GPIO */
  79        u32 request_count;      /* a number of requested shared GPIO */
  80};
  81
  82/*
  83 * struct regulator_supply_alias
  84 *
  85 * Used to map lookups for a supply onto an alternative device.
  86 */
  87struct regulator_supply_alias {
  88        struct list_head list;
  89        struct device *src_dev;
  90        const char *src_supply;
  91        struct device *alias_dev;
  92        const char *alias_supply;
  93};
  94
  95static int _regulator_is_enabled(struct regulator_dev *rdev);
  96static int _regulator_disable(struct regulator *regulator);
  97static int _regulator_get_current_limit(struct regulator_dev *rdev);
  98static unsigned int _regulator_get_mode(struct regulator_dev *rdev);
  99static int _notifier_call_chain(struct regulator_dev *rdev,
 100                                  unsigned long event, void *data);
 101static int _regulator_do_set_voltage(struct regulator_dev *rdev,
 102                                     int min_uV, int max_uV);
 103static int regulator_balance_voltage(struct regulator_dev *rdev,
 104                                     suspend_state_t state);
 105static struct regulator *create_regulator(struct regulator_dev *rdev,
 106                                          struct device *dev,
 107                                          const char *supply_name);
 108static void _regulator_put(struct regulator *regulator);
 109
 110const char *rdev_get_name(struct regulator_dev *rdev)
 111{
 112        if (rdev->constraints && rdev->constraints->name)
 113                return rdev->constraints->name;
 114        else if (rdev->desc->name)
 115                return rdev->desc->name;
 116        else
 117                return "";
 118}
 119
 120static bool have_full_constraints(void)
 121{
 122        return has_full_constraints || of_have_populated_dt();
 123}
 124
 125static bool regulator_ops_is_valid(struct regulator_dev *rdev, int ops)
 126{
 127        if (!rdev->constraints) {
 128                rdev_err(rdev, "no constraints\n");
 129                return false;
 130        }
 131
 132        if (rdev->constraints->valid_ops_mask & ops)
 133                return true;
 134
 135        return false;
 136}
 137
 138/**
 139 * regulator_lock_nested - lock a single regulator
 140 * @rdev:               regulator source
 141 * @ww_ctx:             w/w mutex acquire context
 142 *
 143 * This function can be called many times by one task on
 144 * a single regulator and its mutex will be locked only
 145 * once. If a task, which is calling this function is other
 146 * than the one, which initially locked the mutex, it will
 147 * wait on mutex.
 148 */
 149static inline int regulator_lock_nested(struct regulator_dev *rdev,
 150                                        struct ww_acquire_ctx *ww_ctx)
 151{
 152        bool lock = false;
 153        int ret = 0;
 154
 155        mutex_lock(&regulator_nesting_mutex);
 156
 157        if (ww_ctx || !ww_mutex_trylock(&rdev->mutex)) {
 158                if (rdev->mutex_owner == current)
 159                        rdev->ref_cnt++;
 160                else
 161                        lock = true;
 162
 163                if (lock) {
 164                        mutex_unlock(&regulator_nesting_mutex);
 165                        ret = ww_mutex_lock(&rdev->mutex, ww_ctx);
 166                        mutex_lock(&regulator_nesting_mutex);
 167                }
 168        } else {
 169                lock = true;
 170        }
 171
 172        if (lock && ret != -EDEADLK) {
 173                rdev->ref_cnt++;
 174                rdev->mutex_owner = current;
 175        }
 176
 177        mutex_unlock(&regulator_nesting_mutex);
 178
 179        return ret;
 180}
 181
 182/**
 183 * regulator_lock - lock a single regulator
 184 * @rdev:               regulator source
 185 *
 186 * This function can be called many times by one task on
 187 * a single regulator and its mutex will be locked only
 188 * once. If a task, which is calling this function is other
 189 * than the one, which initially locked the mutex, it will
 190 * wait on mutex.
 191 */
 192void regulator_lock(struct regulator_dev *rdev)
 193{
 194        regulator_lock_nested(rdev, NULL);
 195}
 196EXPORT_SYMBOL_GPL(regulator_lock);
 197
 198/**
 199 * regulator_unlock - unlock a single regulator
 200 * @rdev:               regulator_source
 201 *
 202 * This function unlocks the mutex when the
 203 * reference counter reaches 0.
 204 */
 205void regulator_unlock(struct regulator_dev *rdev)
 206{
 207        mutex_lock(&regulator_nesting_mutex);
 208
 209        if (--rdev->ref_cnt == 0) {
 210                rdev->mutex_owner = NULL;
 211                ww_mutex_unlock(&rdev->mutex);
 212        }
 213
 214        WARN_ON_ONCE(rdev->ref_cnt < 0);
 215
 216        mutex_unlock(&regulator_nesting_mutex);
 217}
 218EXPORT_SYMBOL_GPL(regulator_unlock);
 219
 220static bool regulator_supply_is_couple(struct regulator_dev *rdev)
 221{
 222        struct regulator_dev *c_rdev;
 223        int i;
 224
 225        for (i = 1; i < rdev->coupling_desc.n_coupled; i++) {
 226                c_rdev = rdev->coupling_desc.coupled_rdevs[i];
 227
 228                if (rdev->supply->rdev == c_rdev)
 229                        return true;
 230        }
 231
 232        return false;
 233}
 234
 235static void regulator_unlock_recursive(struct regulator_dev *rdev,
 236                                       unsigned int n_coupled)
 237{
 238        struct regulator_dev *c_rdev;
 239        int i;
 240
 241        for (i = n_coupled; i > 0; i--) {
 242                c_rdev = rdev->coupling_desc.coupled_rdevs[i - 1];
 243
 244                if (!c_rdev)
 245                        continue;
 246
 247                if (c_rdev->supply && !regulator_supply_is_couple(c_rdev))
 248                        regulator_unlock_recursive(
 249                                        c_rdev->supply->rdev,
 250                                        c_rdev->coupling_desc.n_coupled);
 251
 252                regulator_unlock(c_rdev);
 253        }
 254}
 255
 256static int regulator_lock_recursive(struct regulator_dev *rdev,
 257                                    struct regulator_dev **new_contended_rdev,
 258                                    struct regulator_dev **old_contended_rdev,
 259                                    struct ww_acquire_ctx *ww_ctx)
 260{
 261        struct regulator_dev *c_rdev;
 262        int i, err;
 263
 264        for (i = 0; i < rdev->coupling_desc.n_coupled; i++) {
 265                c_rdev = rdev->coupling_desc.coupled_rdevs[i];
 266
 267                if (!c_rdev)
 268                        continue;
 269
 270                if (c_rdev != *old_contended_rdev) {
 271                        err = regulator_lock_nested(c_rdev, ww_ctx);
 272                        if (err) {
 273                                if (err == -EDEADLK) {
 274                                        *new_contended_rdev = c_rdev;
 275                                        goto err_unlock;
 276                                }
 277
 278                                /* shouldn't happen */
 279                                WARN_ON_ONCE(err != -EALREADY);
 280                        }
 281                } else {
 282                        *old_contended_rdev = NULL;
 283                }
 284
 285                if (c_rdev->supply && !regulator_supply_is_couple(c_rdev)) {
 286                        err = regulator_lock_recursive(c_rdev->supply->rdev,
 287                                                       new_contended_rdev,
 288                                                       old_contended_rdev,
 289                                                       ww_ctx);
 290                        if (err) {
 291                                regulator_unlock(c_rdev);
 292                                goto err_unlock;
 293                        }
 294                }
 295        }
 296
 297        return 0;
 298
 299err_unlock:
 300        regulator_unlock_recursive(rdev, i);
 301
 302        return err;
 303}
 304
 305/**
 306 * regulator_unlock_dependent - unlock regulator's suppliers and coupled
 307 *                              regulators
 308 * @rdev:                       regulator source
 309 * @ww_ctx:                     w/w mutex acquire context
 310 *
 311 * Unlock all regulators related with rdev by coupling or supplying.
 312 */
 313static void regulator_unlock_dependent(struct regulator_dev *rdev,
 314                                       struct ww_acquire_ctx *ww_ctx)
 315{
 316        regulator_unlock_recursive(rdev, rdev->coupling_desc.n_coupled);
 317        ww_acquire_fini(ww_ctx);
 318}
 319
 320/**
 321 * regulator_lock_dependent - lock regulator's suppliers and coupled regulators
 322 * @rdev:                       regulator source
 323 * @ww_ctx:                     w/w mutex acquire context
 324 *
 325 * This function as a wrapper on regulator_lock_recursive(), which locks
 326 * all regulators related with rdev by coupling or supplying.
 327 */
 328static void regulator_lock_dependent(struct regulator_dev *rdev,
 329                                     struct ww_acquire_ctx *ww_ctx)
 330{
 331        struct regulator_dev *new_contended_rdev = NULL;
 332        struct regulator_dev *old_contended_rdev = NULL;
 333        int err;
 334
 335        mutex_lock(&regulator_list_mutex);
 336
 337        ww_acquire_init(ww_ctx, &regulator_ww_class);
 338
 339        do {
 340                if (new_contended_rdev) {
 341                        ww_mutex_lock_slow(&new_contended_rdev->mutex, ww_ctx);
 342                        old_contended_rdev = new_contended_rdev;
 343                        old_contended_rdev->ref_cnt++;
 344                }
 345
 346                err = regulator_lock_recursive(rdev,
 347                                               &new_contended_rdev,
 348                                               &old_contended_rdev,
 349                                               ww_ctx);
 350
 351                if (old_contended_rdev)
 352                        regulator_unlock(old_contended_rdev);
 353
 354        } while (err == -EDEADLK);
 355
 356        ww_acquire_done(ww_ctx);
 357
 358        mutex_unlock(&regulator_list_mutex);
 359}
 360
 361/**
 362 * of_get_child_regulator - get a child regulator device node
 363 * based on supply name
 364 * @parent: Parent device node
 365 * @prop_name: Combination regulator supply name and "-supply"
 366 *
 367 * Traverse all child nodes.
 368 * Extract the child regulator device node corresponding to the supply name.
 369 * returns the device node corresponding to the regulator if found, else
 370 * returns NULL.
 371 */
 372static struct device_node *of_get_child_regulator(struct device_node *parent,
 373                                                  const char *prop_name)
 374{
 375        struct device_node *regnode = NULL;
 376        struct device_node *child = NULL;
 377
 378        for_each_child_of_node(parent, child) {
 379                regnode = of_parse_phandle(child, prop_name, 0);
 380
 381                if (!regnode) {
 382                        regnode = of_get_child_regulator(child, prop_name);
 383                        if (regnode)
 384                                return regnode;
 385                } else {
 386                        return regnode;
 387                }
 388        }
 389        return NULL;
 390}
 391
 392/**
 393 * of_get_regulator - get a regulator device node based on supply name
 394 * @dev: Device pointer for the consumer (of regulator) device
 395 * @supply: regulator supply name
 396 *
 397 * Extract the regulator device node corresponding to the supply name.
 398 * returns the device node corresponding to the regulator if found, else
 399 * returns NULL.
 400 */
 401static struct device_node *of_get_regulator(struct device *dev, const char *supply)
 402{
 403        struct device_node *regnode = NULL;
 404        char prop_name[32]; /* 32 is max size of property name */
 405
 406        dev_dbg(dev, "Looking up %s-supply from device tree\n", supply);
 407
 408        snprintf(prop_name, 32, "%s-supply", supply);
 409        regnode = of_parse_phandle(dev->of_node, prop_name, 0);
 410
 411        if (!regnode) {
 412                regnode = of_get_child_regulator(dev->of_node, prop_name);
 413                if (regnode)
 414                        return regnode;
 415
 416                dev_dbg(dev, "Looking up %s property in node %pOF failed\n",
 417                                prop_name, dev->of_node);
 418                return NULL;
 419        }
 420        return regnode;
 421}
 422
 423/* Platform voltage constraint check */
 424int regulator_check_voltage(struct regulator_dev *rdev,
 425                            int *min_uV, int *max_uV)
 426{
 427        BUG_ON(*min_uV > *max_uV);
 428
 429        if (!regulator_ops_is_valid(rdev, REGULATOR_CHANGE_VOLTAGE)) {
 430                rdev_err(rdev, "voltage operation not allowed\n");
 431                return -EPERM;
 432        }
 433
 434        if (*max_uV > rdev->constraints->max_uV)
 435                *max_uV = rdev->constraints->max_uV;
 436        if (*min_uV < rdev->constraints->min_uV)
 437                *min_uV = rdev->constraints->min_uV;
 438
 439        if (*min_uV > *max_uV) {
 440                rdev_err(rdev, "unsupportable voltage range: %d-%duV\n",
 441                         *min_uV, *max_uV);
 442                return -EINVAL;
 443        }
 444
 445        return 0;
 446}
 447
 448/* return 0 if the state is valid */
 449static int regulator_check_states(suspend_state_t state)
 450{
 451        return (state > PM_SUSPEND_MAX || state == PM_SUSPEND_TO_IDLE);
 452}
 453
 454/* Make sure we select a voltage that suits the needs of all
 455 * regulator consumers
 456 */
 457int regulator_check_consumers(struct regulator_dev *rdev,
 458                              int *min_uV, int *max_uV,
 459                              suspend_state_t state)
 460{
 461        struct regulator *regulator;
 462        struct regulator_voltage *voltage;
 463
 464        list_for_each_entry(regulator, &rdev->consumer_list, list) {
 465                voltage = &regulator->voltage[state];
 466                /*
 467                 * Assume consumers that didn't say anything are OK
 468                 * with anything in the constraint range.
 469                 */
 470                if (!voltage->min_uV && !voltage->max_uV)
 471                        continue;
 472
 473                if (*max_uV > voltage->max_uV)
 474                        *max_uV = voltage->max_uV;
 475                if (*min_uV < voltage->min_uV)
 476                        *min_uV = voltage->min_uV;
 477        }
 478
 479        if (*min_uV > *max_uV) {
 480                rdev_err(rdev, "Restricting voltage, %u-%uuV\n",
 481                        *min_uV, *max_uV);
 482                return -EINVAL;
 483        }
 484
 485        return 0;
 486}
 487
 488/* current constraint check */
 489static int regulator_check_current_limit(struct regulator_dev *rdev,
 490                                        int *min_uA, int *max_uA)
 491{
 492        BUG_ON(*min_uA > *max_uA);
 493
 494        if (!regulator_ops_is_valid(rdev, REGULATOR_CHANGE_CURRENT)) {
 495                rdev_err(rdev, "current operation not allowed\n");
 496                return -EPERM;
 497        }
 498
 499        if (*max_uA > rdev->constraints->max_uA)
 500                *max_uA = rdev->constraints->max_uA;
 501        if (*min_uA < rdev->constraints->min_uA)
 502                *min_uA = rdev->constraints->min_uA;
 503
 504        if (*min_uA > *max_uA) {
 505                rdev_err(rdev, "unsupportable current range: %d-%duA\n",
 506                         *min_uA, *max_uA);
 507                return -EINVAL;
 508        }
 509
 510        return 0;
 511}
 512
 513/* operating mode constraint check */
 514static int regulator_mode_constrain(struct regulator_dev *rdev,
 515                                    unsigned int *mode)
 516{
 517        switch (*mode) {
 518        case REGULATOR_MODE_FAST:
 519        case REGULATOR_MODE_NORMAL:
 520        case REGULATOR_MODE_IDLE:
 521        case REGULATOR_MODE_STANDBY:
 522                break;
 523        default:
 524                rdev_err(rdev, "invalid mode %x specified\n", *mode);
 525                return -EINVAL;
 526        }
 527
 528        if (!regulator_ops_is_valid(rdev, REGULATOR_CHANGE_MODE)) {
 529                rdev_err(rdev, "mode operation not allowed\n");
 530                return -EPERM;
 531        }
 532
 533        /* The modes are bitmasks, the most power hungry modes having
 534         * the lowest values. If the requested mode isn't supported
 535         * try higher modes. */
 536        while (*mode) {
 537                if (rdev->constraints->valid_modes_mask & *mode)
 538                        return 0;
 539                *mode /= 2;
 540        }
 541
 542        return -EINVAL;
 543}
 544
 545static inline struct regulator_state *
 546regulator_get_suspend_state(struct regulator_dev *rdev, suspend_state_t state)
 547{
 548        if (rdev->constraints == NULL)
 549                return NULL;
 550
 551        switch (state) {
 552        case PM_SUSPEND_STANDBY:
 553                return &rdev->constraints->state_standby;
 554        case PM_SUSPEND_MEM:
 555                return &rdev->constraints->state_mem;
 556        case PM_SUSPEND_MAX:
 557                return &rdev->constraints->state_disk;
 558        default:
 559                return NULL;
 560        }
 561}
 562
 563static ssize_t regulator_uV_show(struct device *dev,
 564                                struct device_attribute *attr, char *buf)
 565{
 566        struct regulator_dev *rdev = dev_get_drvdata(dev);
 567        ssize_t ret;
 568
 569        regulator_lock(rdev);
 570        ret = sprintf(buf, "%d\n", regulator_get_voltage_rdev(rdev));
 571        regulator_unlock(rdev);
 572
 573        return ret;
 574}
 575static DEVICE_ATTR(microvolts, 0444, regulator_uV_show, NULL);
 576
 577static ssize_t regulator_uA_show(struct device *dev,
 578                                struct device_attribute *attr, char *buf)
 579{
 580        struct regulator_dev *rdev = dev_get_drvdata(dev);
 581
 582        return sprintf(buf, "%d\n", _regulator_get_current_limit(rdev));
 583}
 584static DEVICE_ATTR(microamps, 0444, regulator_uA_show, NULL);
 585
 586static ssize_t name_show(struct device *dev, struct device_attribute *attr,
 587                         char *buf)
 588{
 589        struct regulator_dev *rdev = dev_get_drvdata(dev);
 590
 591        return sprintf(buf, "%s\n", rdev_get_name(rdev));
 592}
 593static DEVICE_ATTR_RO(name);
 594
 595static const char *regulator_opmode_to_str(int mode)
 596{
 597        switch (mode) {
 598        case REGULATOR_MODE_FAST:
 599                return "fast";
 600        case REGULATOR_MODE_NORMAL:
 601                return "normal";
 602        case REGULATOR_MODE_IDLE:
 603                return "idle";
 604        case REGULATOR_MODE_STANDBY:
 605                return "standby";
 606        }
 607        return "unknown";
 608}
 609
 610static ssize_t regulator_print_opmode(char *buf, int mode)
 611{
 612        return sprintf(buf, "%s\n", regulator_opmode_to_str(mode));
 613}
 614
 615static ssize_t regulator_opmode_show(struct device *dev,
 616                                    struct device_attribute *attr, char *buf)
 617{
 618        struct regulator_dev *rdev = dev_get_drvdata(dev);
 619
 620        return regulator_print_opmode(buf, _regulator_get_mode(rdev));
 621}
 622static DEVICE_ATTR(opmode, 0444, regulator_opmode_show, NULL);
 623
 624static ssize_t regulator_print_state(char *buf, int state)
 625{
 626        if (state > 0)
 627                return sprintf(buf, "enabled\n");
 628        else if (state == 0)
 629                return sprintf(buf, "disabled\n");
 630        else
 631                return sprintf(buf, "unknown\n");
 632}
 633
 634static ssize_t regulator_state_show(struct device *dev,
 635                                   struct device_attribute *attr, char *buf)
 636{
 637        struct regulator_dev *rdev = dev_get_drvdata(dev);
 638        ssize_t ret;
 639
 640        regulator_lock(rdev);
 641        ret = regulator_print_state(buf, _regulator_is_enabled(rdev));
 642        regulator_unlock(rdev);
 643
 644        return ret;
 645}
 646static DEVICE_ATTR(state, 0444, regulator_state_show, NULL);
 647
 648static ssize_t regulator_status_show(struct device *dev,
 649                                   struct device_attribute *attr, char *buf)
 650{
 651        struct regulator_dev *rdev = dev_get_drvdata(dev);
 652        int status;
 653        char *label;
 654
 655        status = rdev->desc->ops->get_status(rdev);
 656        if (status < 0)
 657                return status;
 658
 659        switch (status) {
 660        case REGULATOR_STATUS_OFF:
 661                label = "off";
 662                break;
 663        case REGULATOR_STATUS_ON:
 664                label = "on";
 665                break;
 666        case REGULATOR_STATUS_ERROR:
 667                label = "error";
 668                break;
 669        case REGULATOR_STATUS_FAST:
 670                label = "fast";
 671                break;
 672        case REGULATOR_STATUS_NORMAL:
 673                label = "normal";
 674                break;
 675        case REGULATOR_STATUS_IDLE:
 676                label = "idle";
 677                break;
 678        case REGULATOR_STATUS_STANDBY:
 679                label = "standby";
 680                break;
 681        case REGULATOR_STATUS_BYPASS:
 682                label = "bypass";
 683                break;
 684        case REGULATOR_STATUS_UNDEFINED:
 685                label = "undefined";
 686                break;
 687        default:
 688                return -ERANGE;
 689        }
 690
 691        return sprintf(buf, "%s\n", label);
 692}
 693static DEVICE_ATTR(status, 0444, regulator_status_show, NULL);
 694
 695static ssize_t regulator_min_uA_show(struct device *dev,
 696                                    struct device_attribute *attr, char *buf)
 697{
 698        struct regulator_dev *rdev = dev_get_drvdata(dev);
 699
 700        if (!rdev->constraints)
 701                return sprintf(buf, "constraint not defined\n");
 702
 703        return sprintf(buf, "%d\n", rdev->constraints->min_uA);
 704}
 705static DEVICE_ATTR(min_microamps, 0444, regulator_min_uA_show, NULL);
 706
 707static ssize_t regulator_max_uA_show(struct device *dev,
 708                                    struct device_attribute *attr, char *buf)
 709{
 710        struct regulator_dev *rdev = dev_get_drvdata(dev);
 711
 712        if (!rdev->constraints)
 713                return sprintf(buf, "constraint not defined\n");
 714
 715        return sprintf(buf, "%d\n", rdev->constraints->max_uA);
 716}
 717static DEVICE_ATTR(max_microamps, 0444, regulator_max_uA_show, NULL);
 718
 719static ssize_t regulator_min_uV_show(struct device *dev,
 720                                    struct device_attribute *attr, char *buf)
 721{
 722        struct regulator_dev *rdev = dev_get_drvdata(dev);
 723
 724        if (!rdev->constraints)
 725                return sprintf(buf, "constraint not defined\n");
 726
 727        return sprintf(buf, "%d\n", rdev->constraints->min_uV);
 728}
 729static DEVICE_ATTR(min_microvolts, 0444, regulator_min_uV_show, NULL);
 730
 731static ssize_t regulator_max_uV_show(struct device *dev,
 732                                    struct device_attribute *attr, char *buf)
 733{
 734        struct regulator_dev *rdev = dev_get_drvdata(dev);
 735
 736        if (!rdev->constraints)
 737                return sprintf(buf, "constraint not defined\n");
 738
 739        return sprintf(buf, "%d\n", rdev->constraints->max_uV);
 740}
 741static DEVICE_ATTR(max_microvolts, 0444, regulator_max_uV_show, NULL);
 742
 743static ssize_t regulator_total_uA_show(struct device *dev,
 744                                      struct device_attribute *attr, char *buf)
 745{
 746        struct regulator_dev *rdev = dev_get_drvdata(dev);
 747        struct regulator *regulator;
 748        int uA = 0;
 749
 750        regulator_lock(rdev);
 751        list_for_each_entry(regulator, &rdev->consumer_list, list) {
 752                if (regulator->enable_count)
 753                        uA += regulator->uA_load;
 754        }
 755        regulator_unlock(rdev);
 756        return sprintf(buf, "%d\n", uA);
 757}
 758static DEVICE_ATTR(requested_microamps, 0444, regulator_total_uA_show, NULL);
 759
 760static ssize_t num_users_show(struct device *dev, struct device_attribute *attr,
 761                              char *buf)
 762{
 763        struct regulator_dev *rdev = dev_get_drvdata(dev);
 764        return sprintf(buf, "%d\n", rdev->use_count);
 765}
 766static DEVICE_ATTR_RO(num_users);
 767
 768static ssize_t type_show(struct device *dev, struct device_attribute *attr,
 769                         char *buf)
 770{
 771        struct regulator_dev *rdev = dev_get_drvdata(dev);
 772
 773        switch (rdev->desc->type) {
 774        case REGULATOR_VOLTAGE:
 775                return sprintf(buf, "voltage\n");
 776        case REGULATOR_CURRENT:
 777                return sprintf(buf, "current\n");
 778        }
 779        return sprintf(buf, "unknown\n");
 780}
 781static DEVICE_ATTR_RO(type);
 782
 783static ssize_t regulator_suspend_mem_uV_show(struct device *dev,
 784                                struct device_attribute *attr, char *buf)
 785{
 786        struct regulator_dev *rdev = dev_get_drvdata(dev);
 787
 788        return sprintf(buf, "%d\n", rdev->constraints->state_mem.uV);
 789}
 790static DEVICE_ATTR(suspend_mem_microvolts, 0444,
 791                regulator_suspend_mem_uV_show, NULL);
 792
 793static ssize_t regulator_suspend_disk_uV_show(struct device *dev,
 794                                struct device_attribute *attr, char *buf)
 795{
 796        struct regulator_dev *rdev = dev_get_drvdata(dev);
 797
 798        return sprintf(buf, "%d\n", rdev->constraints->state_disk.uV);
 799}
 800static DEVICE_ATTR(suspend_disk_microvolts, 0444,
 801                regulator_suspend_disk_uV_show, NULL);
 802
 803static ssize_t regulator_suspend_standby_uV_show(struct device *dev,
 804                                struct device_attribute *attr, char *buf)
 805{
 806        struct regulator_dev *rdev = dev_get_drvdata(dev);
 807
 808        return sprintf(buf, "%d\n", rdev->constraints->state_standby.uV);
 809}
 810static DEVICE_ATTR(suspend_standby_microvolts, 0444,
 811                regulator_suspend_standby_uV_show, NULL);
 812
 813static ssize_t regulator_suspend_mem_mode_show(struct device *dev,
 814                                struct device_attribute *attr, char *buf)
 815{
 816        struct regulator_dev *rdev = dev_get_drvdata(dev);
 817
 818        return regulator_print_opmode(buf,
 819                rdev->constraints->state_mem.mode);
 820}
 821static DEVICE_ATTR(suspend_mem_mode, 0444,
 822                regulator_suspend_mem_mode_show, NULL);
 823
 824static ssize_t regulator_suspend_disk_mode_show(struct device *dev,
 825                                struct device_attribute *attr, char *buf)
 826{
 827        struct regulator_dev *rdev = dev_get_drvdata(dev);
 828
 829        return regulator_print_opmode(buf,
 830                rdev->constraints->state_disk.mode);
 831}
 832static DEVICE_ATTR(suspend_disk_mode, 0444,
 833                regulator_suspend_disk_mode_show, NULL);
 834
 835static ssize_t regulator_suspend_standby_mode_show(struct device *dev,
 836                                struct device_attribute *attr, char *buf)
 837{
 838        struct regulator_dev *rdev = dev_get_drvdata(dev);
 839
 840        return regulator_print_opmode(buf,
 841                rdev->constraints->state_standby.mode);
 842}
 843static DEVICE_ATTR(suspend_standby_mode, 0444,
 844                regulator_suspend_standby_mode_show, NULL);
 845
 846static ssize_t regulator_suspend_mem_state_show(struct device *dev,
 847                                   struct device_attribute *attr, char *buf)
 848{
 849        struct regulator_dev *rdev = dev_get_drvdata(dev);
 850
 851        return regulator_print_state(buf,
 852                        rdev->constraints->state_mem.enabled);
 853}
 854static DEVICE_ATTR(suspend_mem_state, 0444,
 855                regulator_suspend_mem_state_show, NULL);
 856
 857static ssize_t regulator_suspend_disk_state_show(struct device *dev,
 858                                   struct device_attribute *attr, char *buf)
 859{
 860        struct regulator_dev *rdev = dev_get_drvdata(dev);
 861
 862        return regulator_print_state(buf,
 863                        rdev->constraints->state_disk.enabled);
 864}
 865static DEVICE_ATTR(suspend_disk_state, 0444,
 866                regulator_suspend_disk_state_show, NULL);
 867
 868static ssize_t regulator_suspend_standby_state_show(struct device *dev,
 869                                   struct device_attribute *attr, char *buf)
 870{
 871        struct regulator_dev *rdev = dev_get_drvdata(dev);
 872
 873        return regulator_print_state(buf,
 874                        rdev->constraints->state_standby.enabled);
 875}
 876static DEVICE_ATTR(suspend_standby_state, 0444,
 877                regulator_suspend_standby_state_show, NULL);
 878
 879static ssize_t regulator_bypass_show(struct device *dev,
 880                                     struct device_attribute *attr, char *buf)
 881{
 882        struct regulator_dev *rdev = dev_get_drvdata(dev);
 883        const char *report;
 884        bool bypass;
 885        int ret;
 886
 887        ret = rdev->desc->ops->get_bypass(rdev, &bypass);
 888
 889        if (ret != 0)
 890                report = "unknown";
 891        else if (bypass)
 892                report = "enabled";
 893        else
 894                report = "disabled";
 895
 896        return sprintf(buf, "%s\n", report);
 897}
 898static DEVICE_ATTR(bypass, 0444,
 899                   regulator_bypass_show, NULL);
 900
 901/* Calculate the new optimum regulator operating mode based on the new total
 902 * consumer load. All locks held by caller */
 903static int drms_uA_update(struct regulator_dev *rdev)
 904{
 905        struct regulator *sibling;
 906        int current_uA = 0, output_uV, input_uV, err;
 907        unsigned int mode;
 908
 909        /*
 910         * first check to see if we can set modes at all, otherwise just
 911         * tell the consumer everything is OK.
 912         */
 913        if (!regulator_ops_is_valid(rdev, REGULATOR_CHANGE_DRMS)) {
 914                rdev_dbg(rdev, "DRMS operation not allowed\n");
 915                return 0;
 916        }
 917
 918        if (!rdev->desc->ops->get_optimum_mode &&
 919            !rdev->desc->ops->set_load)
 920                return 0;
 921
 922        if (!rdev->desc->ops->set_mode &&
 923            !rdev->desc->ops->set_load)
 924                return -EINVAL;
 925
 926        /* calc total requested load */
 927        list_for_each_entry(sibling, &rdev->consumer_list, list) {
 928                if (sibling->enable_count)
 929                        current_uA += sibling->uA_load;
 930        }
 931
 932        current_uA += rdev->constraints->system_load;
 933
 934        if (rdev->desc->ops->set_load) {
 935                /* set the optimum mode for our new total regulator load */
 936                err = rdev->desc->ops->set_load(rdev, current_uA);
 937                if (err < 0)
 938                        rdev_err(rdev, "failed to set load %d\n", current_uA);
 939        } else {
 940                /* get output voltage */
 941                output_uV = regulator_get_voltage_rdev(rdev);
 942                if (output_uV <= 0) {
 943                        rdev_err(rdev, "invalid output voltage found\n");
 944                        return -EINVAL;
 945                }
 946
 947                /* get input voltage */
 948                input_uV = 0;
 949                if (rdev->supply)
 950                        input_uV = regulator_get_voltage(rdev->supply);
 951                if (input_uV <= 0)
 952                        input_uV = rdev->constraints->input_uV;
 953                if (input_uV <= 0) {
 954                        rdev_err(rdev, "invalid input voltage found\n");
 955                        return -EINVAL;
 956                }
 957
 958                /* now get the optimum mode for our new total regulator load */
 959                mode = rdev->desc->ops->get_optimum_mode(rdev, input_uV,
 960                                                         output_uV, current_uA);
 961
 962                /* check the new mode is allowed */
 963                err = regulator_mode_constrain(rdev, &mode);
 964                if (err < 0) {
 965                        rdev_err(rdev, "failed to get optimum mode @ %d uA %d -> %d uV\n",
 966                                 current_uA, input_uV, output_uV);
 967                        return err;
 968                }
 969
 970                err = rdev->desc->ops->set_mode(rdev, mode);
 971                if (err < 0)
 972                        rdev_err(rdev, "failed to set optimum mode %x\n", mode);
 973        }
 974
 975        return err;
 976}
 977
 978static int suspend_set_state(struct regulator_dev *rdev,
 979                                    suspend_state_t state)
 980{
 981        int ret = 0;
 982        struct regulator_state *rstate;
 983
 984        rstate = regulator_get_suspend_state(rdev, state);
 985        if (rstate == NULL)
 986                return 0;
 987
 988        /* If we have no suspend mode configuration don't set anything;
 989         * only warn if the driver implements set_suspend_voltage or
 990         * set_suspend_mode callback.
 991         */
 992        if (rstate->enabled != ENABLE_IN_SUSPEND &&
 993            rstate->enabled != DISABLE_IN_SUSPEND) {
 994                if (rdev->desc->ops->set_suspend_voltage ||
 995                    rdev->desc->ops->set_suspend_mode)
 996                        rdev_warn(rdev, "No configuration\n");
 997                return 0;
 998        }
 999
1000        if (rstate->enabled == ENABLE_IN_SUSPEND &&
1001                rdev->desc->ops->set_suspend_enable)
1002                ret = rdev->desc->ops->set_suspend_enable(rdev);
1003        else if (rstate->enabled == DISABLE_IN_SUSPEND &&
1004                rdev->desc->ops->set_suspend_disable)
1005                ret = rdev->desc->ops->set_suspend_disable(rdev);
1006        else /* OK if set_suspend_enable or set_suspend_disable is NULL */
1007                ret = 0;
1008
1009        if (ret < 0) {
1010                rdev_err(rdev, "failed to enabled/disable\n");
1011                return ret;
1012        }
1013
1014        if (rdev->desc->ops->set_suspend_voltage && rstate->uV > 0) {
1015                ret = rdev->desc->ops->set_suspend_voltage(rdev, rstate->uV);
1016                if (ret < 0) {
1017                        rdev_err(rdev, "failed to set voltage\n");
1018                        return ret;
1019                }
1020        }
1021
1022        if (rdev->desc->ops->set_suspend_mode && rstate->mode > 0) {
1023                ret = rdev->desc->ops->set_suspend_mode(rdev, rstate->mode);
1024                if (ret < 0) {
1025                        rdev_err(rdev, "failed to set mode\n");
1026                        return ret;
1027                }
1028        }
1029
1030        return ret;
1031}
1032
1033static void print_constraints(struct regulator_dev *rdev)
1034{
1035        struct regulation_constraints *constraints = rdev->constraints;
1036        char buf[160] = "";
1037        size_t len = sizeof(buf) - 1;
1038        int count = 0;
1039        int ret;
1040
1041        if (constraints->min_uV && constraints->max_uV) {
1042                if (constraints->min_uV == constraints->max_uV)
1043                        count += scnprintf(buf + count, len - count, "%d mV ",
1044                                           constraints->min_uV / 1000);
1045                else
1046                        count += scnprintf(buf + count, len - count,
1047                                           "%d <--> %d mV ",
1048                                           constraints->min_uV / 1000,
1049                                           constraints->max_uV / 1000);
1050        }
1051
1052        if (!constraints->min_uV ||
1053            constraints->min_uV != constraints->max_uV) {
1054                ret = regulator_get_voltage_rdev(rdev);
1055                if (ret > 0)
1056                        count += scnprintf(buf + count, len - count,
1057                                           "at %d mV ", ret / 1000);
1058        }
1059
1060        if (constraints->uV_offset)
1061                count += scnprintf(buf + count, len - count, "%dmV offset ",
1062                                   constraints->uV_offset / 1000);
1063
1064        if (constraints->min_uA && constraints->max_uA) {
1065                if (constraints->min_uA == constraints->max_uA)
1066                        count += scnprintf(buf + count, len - count, "%d mA ",
1067                                           constraints->min_uA / 1000);
1068                else
1069                        count += scnprintf(buf + count, len - count,
1070                                           "%d <--> %d mA ",
1071                                           constraints->min_uA / 1000,
1072                                           constraints->max_uA / 1000);
1073        }
1074
1075        if (!constraints->min_uA ||
1076            constraints->min_uA != constraints->max_uA) {
1077                ret = _regulator_get_current_limit(rdev);
1078                if (ret > 0)
1079                        count += scnprintf(buf + count, len - count,
1080                                           "at %d mA ", ret / 1000);
1081        }
1082
1083        if (constraints->valid_modes_mask & REGULATOR_MODE_FAST)
1084                count += scnprintf(buf + count, len - count, "fast ");
1085        if (constraints->valid_modes_mask & REGULATOR_MODE_NORMAL)
1086                count += scnprintf(buf + count, len - count, "normal ");
1087        if (constraints->valid_modes_mask & REGULATOR_MODE_IDLE)
1088                count += scnprintf(buf + count, len - count, "idle ");
1089        if (constraints->valid_modes_mask & REGULATOR_MODE_STANDBY)
1090                count += scnprintf(buf + count, len - count, "standby");
1091
1092        if (!count)
1093                scnprintf(buf, len, "no parameters");
1094
1095        rdev_dbg(rdev, "%s\n", buf);
1096
1097        if ((constraints->min_uV != constraints->max_uV) &&
1098            !regulator_ops_is_valid(rdev, REGULATOR_CHANGE_VOLTAGE))
1099                rdev_warn(rdev,
1100                          "Voltage range but no REGULATOR_CHANGE_VOLTAGE\n");
1101}
1102
1103static int machine_constraints_voltage(struct regulator_dev *rdev,
1104        struct regulation_constraints *constraints)
1105{
1106        const struct regulator_ops *ops = rdev->desc->ops;
1107        int ret;
1108
1109        /* do we need to apply the constraint voltage */
1110        if (rdev->constraints->apply_uV &&
1111            rdev->constraints->min_uV && rdev->constraints->max_uV) {
1112                int target_min, target_max;
1113                int current_uV = regulator_get_voltage_rdev(rdev);
1114
1115                if (current_uV == -ENOTRECOVERABLE) {
1116                        /* This regulator can't be read and must be initialized */
1117                        rdev_info(rdev, "Setting %d-%duV\n",
1118                                  rdev->constraints->min_uV,
1119                                  rdev->constraints->max_uV);
1120                        _regulator_do_set_voltage(rdev,
1121                                                  rdev->constraints->min_uV,
1122                                                  rdev->constraints->max_uV);
1123                        current_uV = regulator_get_voltage_rdev(rdev);
1124                }
1125
1126                if (current_uV < 0) {
1127                        rdev_err(rdev,
1128                                 "failed to get the current voltage(%d)\n",
1129                                 current_uV);
1130                        return current_uV;
1131                }
1132
1133                /*
1134                 * If we're below the minimum voltage move up to the
1135                 * minimum voltage, if we're above the maximum voltage
1136                 * then move down to the maximum.
1137                 */
1138                target_min = current_uV;
1139                target_max = current_uV;
1140
1141                if (current_uV < rdev->constraints->min_uV) {
1142                        target_min = rdev->constraints->min_uV;
1143                        target_max = rdev->constraints->min_uV;
1144                }
1145
1146                if (current_uV > rdev->constraints->max_uV) {
1147                        target_min = rdev->constraints->max_uV;
1148                        target_max = rdev->constraints->max_uV;
1149                }
1150
1151                if (target_min != current_uV || target_max != current_uV) {
1152                        rdev_info(rdev, "Bringing %duV into %d-%duV\n",
1153                                  current_uV, target_min, target_max);
1154                        ret = _regulator_do_set_voltage(
1155                                rdev, target_min, target_max);
1156                        if (ret < 0) {
1157                                rdev_err(rdev,
1158                                        "failed to apply %d-%duV constraint(%d)\n",
1159                                        target_min, target_max, ret);
1160                                return ret;
1161                        }
1162                }
1163        }
1164
1165        /* constrain machine-level voltage specs to fit
1166         * the actual range supported by this regulator.
1167         */
1168        if (ops->list_voltage && rdev->desc->n_voltages) {
1169                int     count = rdev->desc->n_voltages;
1170                int     i;
1171                int     min_uV = INT_MAX;
1172                int     max_uV = INT_MIN;
1173                int     cmin = constraints->min_uV;
1174                int     cmax = constraints->max_uV;
1175
1176                /* it's safe to autoconfigure fixed-voltage supplies
1177                   and the constraints are used by list_voltage. */
1178                if (count == 1 && !cmin) {
1179                        cmin = 1;
1180                        cmax = INT_MAX;
1181                        constraints->min_uV = cmin;
1182                        constraints->max_uV = cmax;
1183                }
1184
1185                /* voltage constraints are optional */
1186                if ((cmin == 0) && (cmax == 0))
1187                        return 0;
1188
1189                /* else require explicit machine-level constraints */
1190                if (cmin <= 0 || cmax <= 0 || cmax < cmin) {
1191                        rdev_err(rdev, "invalid voltage constraints\n");
1192                        return -EINVAL;
1193                }
1194
1195                /* initial: [cmin..cmax] valid, [min_uV..max_uV] not */
1196                for (i = 0; i < count; i++) {
1197                        int     value;
1198
1199                        value = ops->list_voltage(rdev, i);
1200                        if (value <= 0)
1201                                continue;
1202
1203                        /* maybe adjust [min_uV..max_uV] */
1204                        if (value >= cmin && value < min_uV)
1205                                min_uV = value;
1206                        if (value <= cmax && value > max_uV)
1207                                max_uV = value;
1208                }
1209
1210                /* final: [min_uV..max_uV] valid iff constraints valid */
1211                if (max_uV < min_uV) {
1212                        rdev_err(rdev,
1213                                 "unsupportable voltage constraints %u-%uuV\n",
1214                                 min_uV, max_uV);
1215                        return -EINVAL;
1216                }
1217
1218                /* use regulator's subset of machine constraints */
1219                if (constraints->min_uV < min_uV) {
1220                        rdev_dbg(rdev, "override min_uV, %d -> %d\n",
1221                                 constraints->min_uV, min_uV);
1222                        constraints->min_uV = min_uV;
1223                }
1224                if (constraints->max_uV > max_uV) {
1225                        rdev_dbg(rdev, "override max_uV, %d -> %d\n",
1226                                 constraints->max_uV, max_uV);
1227                        constraints->max_uV = max_uV;
1228                }
1229        }
1230
1231        return 0;
1232}
1233
1234static int machine_constraints_current(struct regulator_dev *rdev,
1235        struct regulation_constraints *constraints)
1236{
1237        const struct regulator_ops *ops = rdev->desc->ops;
1238        int ret;
1239
1240        if (!constraints->min_uA && !constraints->max_uA)
1241                return 0;
1242
1243        if (constraints->min_uA > constraints->max_uA) {
1244                rdev_err(rdev, "Invalid current constraints\n");
1245                return -EINVAL;
1246        }
1247
1248        if (!ops->set_current_limit || !ops->get_current_limit) {
1249                rdev_warn(rdev, "Operation of current configuration missing\n");
1250                return 0;
1251        }
1252
1253        /* Set regulator current in constraints range */
1254        ret = ops->set_current_limit(rdev, constraints->min_uA,
1255                        constraints->max_uA);
1256        if (ret < 0) {
1257                rdev_err(rdev, "Failed to set current constraint, %d\n", ret);
1258                return ret;
1259        }
1260
1261        return 0;
1262}
1263
1264static int _regulator_do_enable(struct regulator_dev *rdev);
1265
1266/**
1267 * set_machine_constraints - sets regulator constraints
1268 * @rdev: regulator source
1269 * @constraints: constraints to apply
1270 *
1271 * Allows platform initialisation code to define and constrain
1272 * regulator circuits e.g. valid voltage/current ranges, etc.  NOTE:
1273 * Constraints *must* be set by platform code in order for some
1274 * regulator operations to proceed i.e. set_voltage, set_current_limit,
1275 * set_mode.
1276 */
1277static int set_machine_constraints(struct regulator_dev *rdev,
1278        const struct regulation_constraints *constraints)
1279{
1280        int ret = 0;
1281        const struct regulator_ops *ops = rdev->desc->ops;
1282
1283        if (constraints)
1284                rdev->constraints = kmemdup(constraints, sizeof(*constraints),
1285                                            GFP_KERNEL);
1286        else
1287                rdev->constraints = kzalloc(sizeof(*constraints),
1288                                            GFP_KERNEL);
1289        if (!rdev->constraints)
1290                return -ENOMEM;
1291
1292        ret = machine_constraints_voltage(rdev, rdev->constraints);
1293        if (ret != 0)
1294                return ret;
1295
1296        ret = machine_constraints_current(rdev, rdev->constraints);
1297        if (ret != 0)
1298                return ret;
1299
1300        if (rdev->constraints->ilim_uA && ops->set_input_current_limit) {
1301                ret = ops->set_input_current_limit(rdev,
1302                                                   rdev->constraints->ilim_uA);
1303                if (ret < 0) {
1304                        rdev_err(rdev, "failed to set input limit\n");
1305                        return ret;
1306                }
1307        }
1308
1309        /* do we need to setup our suspend state */
1310        if (rdev->constraints->initial_state) {
1311                ret = suspend_set_state(rdev, rdev->constraints->initial_state);
1312                if (ret < 0) {
1313                        rdev_err(rdev, "failed to set suspend state\n");
1314                        return ret;
1315                }
1316        }
1317
1318        if (rdev->constraints->initial_mode) {
1319                if (!ops->set_mode) {
1320                        rdev_err(rdev, "no set_mode operation\n");
1321                        return -EINVAL;
1322                }
1323
1324                ret = ops->set_mode(rdev, rdev->constraints->initial_mode);
1325                if (ret < 0) {
1326                        rdev_err(rdev, "failed to set initial mode: %d\n", ret);
1327                        return ret;
1328                }
1329        } else if (rdev->constraints->system_load) {
1330                /*
1331                 * We'll only apply the initial system load if an
1332                 * initial mode wasn't specified.
1333                 */
1334                drms_uA_update(rdev);
1335        }
1336
1337        if ((rdev->constraints->ramp_delay || rdev->constraints->ramp_disable)
1338                && ops->set_ramp_delay) {
1339                ret = ops->set_ramp_delay(rdev, rdev->constraints->ramp_delay);
1340                if (ret < 0) {
1341                        rdev_err(rdev, "failed to set ramp_delay\n");
1342                        return ret;
1343                }
1344        }
1345
1346        if (rdev->constraints->pull_down && ops->set_pull_down) {
1347                ret = ops->set_pull_down(rdev);
1348                if (ret < 0) {
1349                        rdev_err(rdev, "failed to set pull down\n");
1350                        return ret;
1351                }
1352        }
1353
1354        if (rdev->constraints->soft_start && ops->set_soft_start) {
1355                ret = ops->set_soft_start(rdev);
1356                if (ret < 0) {
1357                        rdev_err(rdev, "failed to set soft start\n");
1358                        return ret;
1359                }
1360        }
1361
1362        if (rdev->constraints->over_current_protection
1363                && ops->set_over_current_protection) {
1364                ret = ops->set_over_current_protection(rdev);
1365                if (ret < 0) {
1366                        rdev_err(rdev, "failed to set over current protection\n");
1367                        return ret;
1368                }
1369        }
1370
1371        if (rdev->constraints->active_discharge && ops->set_active_discharge) {
1372                bool ad_state = (rdev->constraints->active_discharge ==
1373                              REGULATOR_ACTIVE_DISCHARGE_ENABLE) ? true : false;
1374
1375                ret = ops->set_active_discharge(rdev, ad_state);
1376                if (ret < 0) {
1377                        rdev_err(rdev, "failed to set active discharge\n");
1378                        return ret;
1379                }
1380        }
1381
1382        /* If the constraints say the regulator should be on at this point
1383         * and we have control then make sure it is enabled.
1384         */
1385        if (rdev->constraints->always_on || rdev->constraints->boot_on) {
1386                if (rdev->supply) {
1387                        ret = regulator_enable(rdev->supply);
1388                        if (ret < 0) {
1389                                _regulator_put(rdev->supply);
1390                                rdev->supply = NULL;
1391                                return ret;
1392                        }
1393                }
1394
1395                ret = _regulator_do_enable(rdev);
1396                if (ret < 0 && ret != -EINVAL) {
1397                        rdev_err(rdev, "failed to enable\n");
1398                        return ret;
1399                }
1400                rdev->use_count++;
1401        }
1402
1403        print_constraints(rdev);
1404        return 0;
1405}
1406
1407/**
1408 * set_supply - set regulator supply regulator
1409 * @rdev: regulator name
1410 * @supply_rdev: supply regulator name
1411 *
1412 * Called by platform initialisation code to set the supply regulator for this
1413 * regulator. This ensures that a regulators supply will also be enabled by the
1414 * core if it's child is enabled.
1415 */
1416static int set_supply(struct regulator_dev *rdev,
1417                      struct regulator_dev *supply_rdev)
1418{
1419        int err;
1420
1421        rdev_info(rdev, "supplied by %s\n", rdev_get_name(supply_rdev));
1422
1423        if (!try_module_get(supply_rdev->owner))
1424                return -ENODEV;
1425
1426        rdev->supply = create_regulator(supply_rdev, &rdev->dev, "SUPPLY");
1427        if (rdev->supply == NULL) {
1428                err = -ENOMEM;
1429                return err;
1430        }
1431        supply_rdev->open_count++;
1432
1433        return 0;
1434}
1435
1436/**
1437 * set_consumer_device_supply - Bind a regulator to a symbolic supply
1438 * @rdev:         regulator source
1439 * @consumer_dev_name: dev_name() string for device supply applies to
1440 * @supply:       symbolic name for supply
1441 *
1442 * Allows platform initialisation code to map physical regulator
1443 * sources to symbolic names for supplies for use by devices.  Devices
1444 * should use these symbolic names to request regulators, avoiding the
1445 * need to provide board-specific regulator names as platform data.
1446 */
1447static int set_consumer_device_supply(struct regulator_dev *rdev,
1448                                      const char *consumer_dev_name,
1449                                      const char *supply)
1450{
1451        struct regulator_map *node;
1452        int has_dev;
1453
1454        if (supply == NULL)
1455                return -EINVAL;
1456
1457        if (consumer_dev_name != NULL)
1458                has_dev = 1;
1459        else
1460                has_dev = 0;
1461
1462        list_for_each_entry(node, &regulator_map_list, list) {
1463                if (node->dev_name && consumer_dev_name) {
1464                        if (strcmp(node->dev_name, consumer_dev_name) != 0)
1465                                continue;
1466                } else if (node->dev_name || consumer_dev_name) {
1467                        continue;
1468                }
1469
1470                if (strcmp(node->supply, supply) != 0)
1471                        continue;
1472
1473                pr_debug("%s: %s/%s is '%s' supply; fail %s/%s\n",
1474                         consumer_dev_name,
1475                         dev_name(&node->regulator->dev),
1476                         node->regulator->desc->name,
1477                         supply,
1478                         dev_name(&rdev->dev), rdev_get_name(rdev));
1479                return -EBUSY;
1480        }
1481
1482        node = kzalloc(sizeof(struct regulator_map), GFP_KERNEL);
1483        if (node == NULL)
1484                return -ENOMEM;
1485
1486        node->regulator = rdev;
1487        node->supply = supply;
1488
1489        if (has_dev) {
1490                node->dev_name = kstrdup(consumer_dev_name, GFP_KERNEL);
1491                if (node->dev_name == NULL) {
1492                        kfree(node);
1493                        return -ENOMEM;
1494                }
1495        }
1496
1497        list_add(&node->list, &regulator_map_list);
1498        return 0;
1499}
1500
1501static void unset_regulator_supplies(struct regulator_dev *rdev)
1502{
1503        struct regulator_map *node, *n;
1504
1505        list_for_each_entry_safe(node, n, &regulator_map_list, list) {
1506                if (rdev == node->regulator) {
1507                        list_del(&node->list);
1508                        kfree(node->dev_name);
1509                        kfree(node);
1510                }
1511        }
1512}
1513
1514#ifdef CONFIG_DEBUG_FS
1515static ssize_t constraint_flags_read_file(struct file *file,
1516                                          char __user *user_buf,
1517                                          size_t count, loff_t *ppos)
1518{
1519        const struct regulator *regulator = file->private_data;
1520        const struct regulation_constraints *c = regulator->rdev->constraints;
1521        char *buf;
1522        ssize_t ret;
1523
1524        if (!c)
1525                return 0;
1526
1527        buf = kmalloc(PAGE_SIZE, GFP_KERNEL);
1528        if (!buf)
1529                return -ENOMEM;
1530
1531        ret = snprintf(buf, PAGE_SIZE,
1532                        "always_on: %u\n"
1533                        "boot_on: %u\n"
1534                        "apply_uV: %u\n"
1535                        "ramp_disable: %u\n"
1536                        "soft_start: %u\n"
1537                        "pull_down: %u\n"
1538                        "over_current_protection: %u\n",
1539                        c->always_on,
1540                        c->boot_on,
1541                        c->apply_uV,
1542                        c->ramp_disable,
1543                        c->soft_start,
1544                        c->pull_down,
1545                        c->over_current_protection);
1546
1547        ret = simple_read_from_buffer(user_buf, count, ppos, buf, ret);
1548        kfree(buf);
1549
1550        return ret;
1551}
1552
1553#endif
1554
1555static const struct file_operations constraint_flags_fops = {
1556#ifdef CONFIG_DEBUG_FS
1557        .open = simple_open,
1558        .read = constraint_flags_read_file,
1559        .llseek = default_llseek,
1560#endif
1561};
1562
1563#define REG_STR_SIZE    64
1564
1565static struct regulator *create_regulator(struct regulator_dev *rdev,
1566                                          struct device *dev,
1567                                          const char *supply_name)
1568{
1569        struct regulator *regulator;
1570        char buf[REG_STR_SIZE];
1571        int err, size;
1572
1573        regulator = kzalloc(sizeof(*regulator), GFP_KERNEL);
1574        if (regulator == NULL)
1575                return NULL;
1576
1577        regulator_lock(rdev);
1578        regulator->rdev = rdev;
1579        list_add(&regulator->list, &rdev->consumer_list);
1580
1581        if (dev) {
1582                regulator->dev = dev;
1583
1584                /* Add a link to the device sysfs entry */
1585                size = snprintf(buf, REG_STR_SIZE, "%s-%s",
1586                                dev->kobj.name, supply_name);
1587                if (size >= REG_STR_SIZE)
1588                        goto overflow_err;
1589
1590                regulator->supply_name = kstrdup(buf, GFP_KERNEL);
1591                if (regulator->supply_name == NULL)
1592                        goto overflow_err;
1593
1594                err = sysfs_create_link_nowarn(&rdev->dev.kobj, &dev->kobj,
1595                                        buf);
1596                if (err) {
1597                        rdev_dbg(rdev, "could not add device link %s err %d\n",
1598                                  dev->kobj.name, err);
1599                        /* non-fatal */
1600                }
1601        } else {
1602                regulator->supply_name = kstrdup_const(supply_name, GFP_KERNEL);
1603                if (regulator->supply_name == NULL)
1604                        goto overflow_err;
1605        }
1606
1607        regulator->debugfs = debugfs_create_dir(regulator->supply_name,
1608                                                rdev->debugfs);
1609        if (!regulator->debugfs) {
1610                rdev_dbg(rdev, "Failed to create debugfs directory\n");
1611        } else {
1612                debugfs_create_u32("uA_load", 0444, regulator->debugfs,
1613                                   &regulator->uA_load);
1614                debugfs_create_u32("min_uV", 0444, regulator->debugfs,
1615                                   &regulator->voltage[PM_SUSPEND_ON].min_uV);
1616                debugfs_create_u32("max_uV", 0444, regulator->debugfs,
1617                                   &regulator->voltage[PM_SUSPEND_ON].max_uV);
1618                debugfs_create_file("constraint_flags", 0444,
1619                                    regulator->debugfs, regulator,
1620                                    &constraint_flags_fops);
1621        }
1622
1623        /*
1624         * Check now if the regulator is an always on regulator - if
1625         * it is then we don't need to do nearly so much work for
1626         * enable/disable calls.
1627         */
1628        if (!regulator_ops_is_valid(rdev, REGULATOR_CHANGE_STATUS) &&
1629            _regulator_is_enabled(rdev))
1630                regulator->always_on = true;
1631
1632        regulator_unlock(rdev);
1633        return regulator;
1634overflow_err:
1635        list_del(&regulator->list);
1636        kfree(regulator);
1637        regulator_unlock(rdev);
1638        return NULL;
1639}
1640
1641static int _regulator_get_enable_time(struct regulator_dev *rdev)
1642{
1643        if (rdev->constraints && rdev->constraints->enable_time)
1644                return rdev->constraints->enable_time;
1645        if (rdev->desc->ops->enable_time)
1646                return rdev->desc->ops->enable_time(rdev);
1647        return rdev->desc->enable_time;
1648}
1649
1650static struct regulator_supply_alias *regulator_find_supply_alias(
1651                struct device *dev, const char *supply)
1652{
1653        struct regulator_supply_alias *map;
1654
1655        list_for_each_entry(map, &regulator_supply_alias_list, list)
1656                if (map->src_dev == dev && strcmp(map->src_supply, supply) == 0)
1657                        return map;
1658
1659        return NULL;
1660}
1661
1662static void regulator_supply_alias(struct device **dev, const char **supply)
1663{
1664        struct regulator_supply_alias *map;
1665
1666        map = regulator_find_supply_alias(*dev, *supply);
1667        if (map) {
1668                dev_dbg(*dev, "Mapping supply %s to %s,%s\n",
1669                                *supply, map->alias_supply,
1670                                dev_name(map->alias_dev));
1671                *dev = map->alias_dev;
1672                *supply = map->alias_supply;
1673        }
1674}
1675
1676static int regulator_match(struct device *dev, const void *data)
1677{
1678        struct regulator_dev *r = dev_to_rdev(dev);
1679
1680        return strcmp(rdev_get_name(r), data) == 0;
1681}
1682
1683static struct regulator_dev *regulator_lookup_by_name(const char *name)
1684{
1685        struct device *dev;
1686
1687        dev = class_find_device(&regulator_class, NULL, name, regulator_match);
1688
1689        return dev ? dev_to_rdev(dev) : NULL;
1690}
1691
1692/**
1693 * regulator_dev_lookup - lookup a regulator device.
1694 * @dev: device for regulator "consumer".
1695 * @supply: Supply name or regulator ID.
1696 *
1697 * If successful, returns a struct regulator_dev that corresponds to the name
1698 * @supply and with the embedded struct device refcount incremented by one.
1699 * The refcount must be dropped by calling put_device().
1700 * On failure one of the following ERR-PTR-encoded values is returned:
1701 * -ENODEV if lookup fails permanently, -EPROBE_DEFER if lookup could succeed
1702 * in the future.
1703 */
1704static struct regulator_dev *regulator_dev_lookup(struct device *dev,
1705                                                  const char *supply)
1706{
1707        struct regulator_dev *r = NULL;
1708        struct device_node *node;
1709        struct regulator_map *map;
1710        const char *devname = NULL;
1711
1712        regulator_supply_alias(&dev, &supply);
1713
1714        /* first do a dt based lookup */
1715        if (dev && dev->of_node) {
1716                node = of_get_regulator(dev, supply);
1717                if (node) {
1718                        r = of_find_regulator_by_node(node);
1719                        if (r)
1720                                return r;
1721
1722                        /*
1723                         * We have a node, but there is no device.
1724                         * assume it has not registered yet.
1725                         */
1726                        return ERR_PTR(-EPROBE_DEFER);
1727                }
1728        }
1729
1730        /* if not found, try doing it non-dt way */
1731        if (dev)
1732                devname = dev_name(dev);
1733
1734        mutex_lock(&regulator_list_mutex);
1735        list_for_each_entry(map, &regulator_map_list, list) {
1736                /* If the mapping has a device set up it must match */
1737                if (map->dev_name &&
1738                    (!devname || strcmp(map->dev_name, devname)))
1739                        continue;
1740
1741                if (strcmp(map->supply, supply) == 0 &&
1742                    get_device(&map->regulator->dev)) {
1743                        r = map->regulator;
1744                        break;
1745                }
1746        }
1747        mutex_unlock(&regulator_list_mutex);
1748
1749        if (r)
1750                return r;
1751
1752        r = regulator_lookup_by_name(supply);
1753        if (r)
1754                return r;
1755
1756        return ERR_PTR(-ENODEV);
1757}
1758
1759static int regulator_resolve_supply(struct regulator_dev *rdev)
1760{
1761        struct regulator_dev *r;
1762        struct device *dev = rdev->dev.parent;
1763        int ret;
1764
1765        /* No supply to resolve? */
1766        if (!rdev->supply_name)
1767                return 0;
1768
1769        /* Supply already resolved? */
1770        if (rdev->supply)
1771                return 0;
1772
1773        r = regulator_dev_lookup(dev, rdev->supply_name);
1774        if (IS_ERR(r)) {
1775                ret = PTR_ERR(r);
1776
1777                /* Did the lookup explicitly defer for us? */
1778                if (ret == -EPROBE_DEFER)
1779                        return ret;
1780
1781                if (have_full_constraints()) {
1782                        r = dummy_regulator_rdev;
1783                        get_device(&r->dev);
1784                } else {
1785                        dev_err(dev, "Failed to resolve %s-supply for %s\n",
1786                                rdev->supply_name, rdev->desc->name);
1787                        return -EPROBE_DEFER;
1788                }
1789        }
1790
1791        /*
1792         * If the supply's parent device is not the same as the
1793         * regulator's parent device, then ensure the parent device
1794         * is bound before we resolve the supply, in case the parent
1795         * device get probe deferred and unregisters the supply.
1796         */
1797        if (r->dev.parent && r->dev.parent != rdev->dev.parent) {
1798                if (!device_is_bound(r->dev.parent)) {
1799                        put_device(&r->dev);
1800                        return -EPROBE_DEFER;
1801                }
1802        }
1803
1804        /* Recursively resolve the supply of the supply */
1805        ret = regulator_resolve_supply(r);
1806        if (ret < 0) {
1807                put_device(&r->dev);
1808                return ret;
1809        }
1810
1811        ret = set_supply(rdev, r);
1812        if (ret < 0) {
1813                put_device(&r->dev);
1814                return ret;
1815        }
1816
1817        /*
1818         * In set_machine_constraints() we may have turned this regulator on
1819         * but we couldn't propagate to the supply if it hadn't been resolved
1820         * yet.  Do it now.
1821         */
1822        if (rdev->use_count) {
1823                ret = regulator_enable(rdev->supply);
1824                if (ret < 0) {
1825                        _regulator_put(rdev->supply);
1826                        rdev->supply = NULL;
1827                        return ret;
1828                }
1829        }
1830
1831        return 0;
1832}
1833
1834/* Internal regulator request function */
1835struct regulator *_regulator_get(struct device *dev, const char *id,
1836                                 enum regulator_get_type get_type)
1837{
1838        struct regulator_dev *rdev;
1839        struct regulator *regulator;
1840        const char *devname = dev ? dev_name(dev) : "deviceless";
1841        int ret;
1842
1843        if (get_type >= MAX_GET_TYPE) {
1844                dev_err(dev, "invalid type %d in %s\n", get_type, __func__);
1845                return ERR_PTR(-EINVAL);
1846        }
1847
1848        if (id == NULL) {
1849                pr_err("get() with no identifier\n");
1850                return ERR_PTR(-EINVAL);
1851        }
1852
1853        rdev = regulator_dev_lookup(dev, id);
1854        if (IS_ERR(rdev)) {
1855                ret = PTR_ERR(rdev);
1856
1857                /*
1858                 * If regulator_dev_lookup() fails with error other
1859                 * than -ENODEV our job here is done, we simply return it.
1860                 */
1861                if (ret != -ENODEV)
1862                        return ERR_PTR(ret);
1863
1864                if (!have_full_constraints()) {
1865                        dev_warn(dev,
1866                                 "incomplete constraints, dummy supplies not allowed\n");
1867                        return ERR_PTR(-ENODEV);
1868                }
1869
1870                switch (get_type) {
1871                case NORMAL_GET:
1872                        /*
1873                         * Assume that a regulator is physically present and
1874                         * enabled, even if it isn't hooked up, and just
1875                         * provide a dummy.
1876                         */
1877                        dev_warn(dev,
1878                                 "%s supply %s not found, using dummy regulator\n",
1879                                 devname, id);
1880                        rdev = dummy_regulator_rdev;
1881                        get_device(&rdev->dev);
1882                        break;
1883
1884                case EXCLUSIVE_GET:
1885                        dev_warn(dev,
1886                                 "dummy supplies not allowed for exclusive requests\n");
1887                        /* fall through */
1888
1889                default:
1890                        return ERR_PTR(-ENODEV);
1891                }
1892        }
1893
1894        if (rdev->exclusive) {
1895                regulator = ERR_PTR(-EPERM);
1896                put_device(&rdev->dev);
1897                return regulator;
1898        }
1899
1900        if (get_type == EXCLUSIVE_GET && rdev->open_count) {
1901                regulator = ERR_PTR(-EBUSY);
1902                put_device(&rdev->dev);
1903                return regulator;
1904        }
1905
1906        mutex_lock(&regulator_list_mutex);
1907        ret = (rdev->coupling_desc.n_resolved != rdev->coupling_desc.n_coupled);
1908        mutex_unlock(&regulator_list_mutex);
1909
1910        if (ret != 0) {
1911                regulator = ERR_PTR(-EPROBE_DEFER);
1912                put_device(&rdev->dev);
1913                return regulator;
1914        }
1915
1916        ret = regulator_resolve_supply(rdev);
1917        if (ret < 0) {
1918                regulator = ERR_PTR(ret);
1919                put_device(&rdev->dev);
1920                return regulator;
1921        }
1922
1923        if (!try_module_get(rdev->owner)) {
1924                regulator = ERR_PTR(-EPROBE_DEFER);
1925                put_device(&rdev->dev);
1926                return regulator;
1927        }
1928
1929        regulator = create_regulator(rdev, dev, id);
1930        if (regulator == NULL) {
1931                regulator = ERR_PTR(-ENOMEM);
1932                put_device(&rdev->dev);
1933                module_put(rdev->owner);
1934                return regulator;
1935        }
1936
1937        rdev->open_count++;
1938        if (get_type == EXCLUSIVE_GET) {
1939                rdev->exclusive = 1;
1940
1941                ret = _regulator_is_enabled(rdev);
1942                if (ret > 0)
1943                        rdev->use_count = 1;
1944                else
1945                        rdev->use_count = 0;
1946        }
1947
1948        device_link_add(dev, &rdev->dev, DL_FLAG_STATELESS);
1949
1950        return regulator;
1951}
1952
1953/**
1954 * regulator_get - lookup and obtain a reference to a regulator.
1955 * @dev: device for regulator "consumer"
1956 * @id: Supply name or regulator ID.
1957 *
1958 * Returns a struct regulator corresponding to the regulator producer,
1959 * or IS_ERR() condition containing errno.
1960 *
1961 * Use of supply names configured via regulator_set_device_supply() is
1962 * strongly encouraged.  It is recommended that the supply name used
1963 * should match the name used for the supply and/or the relevant
1964 * device pins in the datasheet.
1965 */
1966struct regulator *regulator_get(struct device *dev, const char *id)
1967{
1968        return _regulator_get(dev, id, NORMAL_GET);
1969}
1970EXPORT_SYMBOL_GPL(regulator_get);
1971
1972/**
1973 * regulator_get_exclusive - obtain exclusive access to a regulator.
1974 * @dev: device for regulator "consumer"
1975 * @id: Supply name or regulator ID.
1976 *
1977 * Returns a struct regulator corresponding to the regulator producer,
1978 * or IS_ERR() condition containing errno.  Other consumers will be
1979 * unable to obtain this regulator while this reference is held and the
1980 * use count for the regulator will be initialised to reflect the current
1981 * state of the regulator.
1982 *
1983 * This is intended for use by consumers which cannot tolerate shared
1984 * use of the regulator such as those which need to force the
1985 * regulator off for correct operation of the hardware they are
1986 * controlling.
1987 *
1988 * Use of supply names configured via regulator_set_device_supply() is
1989 * strongly encouraged.  It is recommended that the supply name used
1990 * should match the name used for the supply and/or the relevant
1991 * device pins in the datasheet.
1992 */
1993struct regulator *regulator_get_exclusive(struct device *dev, const char *id)
1994{
1995        return _regulator_get(dev, id, EXCLUSIVE_GET);
1996}
1997EXPORT_SYMBOL_GPL(regulator_get_exclusive);
1998
1999/**
2000 * regulator_get_optional - obtain optional access to a regulator.
2001 * @dev: device for regulator "consumer"
2002 * @id: Supply name or regulator ID.
2003 *
2004 * Returns a struct regulator corresponding to the regulator producer,
2005 * or IS_ERR() condition containing errno.
2006 *
2007 * This is intended for use by consumers for devices which can have
2008 * some supplies unconnected in normal use, such as some MMC devices.
2009 * It can allow the regulator core to provide stub supplies for other
2010 * supplies requested using normal regulator_get() calls without
2011 * disrupting the operation of drivers that can handle absent
2012 * supplies.
2013 *
2014 * Use of supply names configured via regulator_set_device_supply() is
2015 * strongly encouraged.  It is recommended that the supply name used
2016 * should match the name used for the supply and/or the relevant
2017 * device pins in the datasheet.
2018 */
2019struct regulator *regulator_get_optional(struct device *dev, const char *id)
2020{
2021        return _regulator_get(dev, id, OPTIONAL_GET);
2022}
2023EXPORT_SYMBOL_GPL(regulator_get_optional);
2024
2025/* regulator_list_mutex lock held by regulator_put() */
2026static void _regulator_put(struct regulator *regulator)
2027{
2028        struct regulator_dev *rdev;
2029
2030        if (IS_ERR_OR_NULL(regulator))
2031                return;
2032
2033        lockdep_assert_held_once(&regulator_list_mutex);
2034
2035        /* Docs say you must disable before calling regulator_put() */
2036        WARN_ON(regulator->enable_count);
2037
2038        rdev = regulator->rdev;
2039
2040        debugfs_remove_recursive(regulator->debugfs);
2041
2042        if (regulator->dev) {
2043                device_link_remove(regulator->dev, &rdev->dev);
2044
2045                /* remove any sysfs entries */
2046                sysfs_remove_link(&rdev->dev.kobj, regulator->supply_name);
2047        }
2048
2049        regulator_lock(rdev);
2050        list_del(&regulator->list);
2051
2052        rdev->open_count--;
2053        rdev->exclusive = 0;
2054        put_device(&rdev->dev);
2055        regulator_unlock(rdev);
2056
2057        kfree_const(regulator->supply_name);
2058        kfree(regulator);
2059
2060        module_put(rdev->owner);
2061}
2062
2063/**
2064 * regulator_put - "free" the regulator source
2065 * @regulator: regulator source
2066 *
2067 * Note: drivers must ensure that all regulator_enable calls made on this
2068 * regulator source are balanced by regulator_disable calls prior to calling
2069 * this function.
2070 */
2071void regulator_put(struct regulator *regulator)
2072{
2073        mutex_lock(&regulator_list_mutex);
2074        _regulator_put(regulator);
2075        mutex_unlock(&regulator_list_mutex);
2076}
2077EXPORT_SYMBOL_GPL(regulator_put);
2078
2079/**
2080 * regulator_register_supply_alias - Provide device alias for supply lookup
2081 *
2082 * @dev: device that will be given as the regulator "consumer"
2083 * @id: Supply name or regulator ID
2084 * @alias_dev: device that should be used to lookup the supply
2085 * @alias_id: Supply name or regulator ID that should be used to lookup the
2086 * supply
2087 *
2088 * All lookups for id on dev will instead be conducted for alias_id on
2089 * alias_dev.
2090 */
2091int regulator_register_supply_alias(struct device *dev, const char *id,
2092                                    struct device *alias_dev,
2093                                    const char *alias_id)
2094{
2095        struct regulator_supply_alias *map;
2096
2097        map = regulator_find_supply_alias(dev, id);
2098        if (map)
2099                return -EEXIST;
2100
2101        map = kzalloc(sizeof(struct regulator_supply_alias), GFP_KERNEL);
2102        if (!map)
2103                return -ENOMEM;
2104
2105        map->src_dev = dev;
2106        map->src_supply = id;
2107        map->alias_dev = alias_dev;
2108        map->alias_supply = alias_id;
2109
2110        list_add(&map->list, &regulator_supply_alias_list);
2111
2112        pr_info("Adding alias for supply %s,%s -> %s,%s\n",
2113                id, dev_name(dev), alias_id, dev_name(alias_dev));
2114
2115        return 0;
2116}
2117EXPORT_SYMBOL_GPL(regulator_register_supply_alias);
2118
2119/**
2120 * regulator_unregister_supply_alias - Remove device alias
2121 *
2122 * @dev: device that will be given as the regulator "consumer"
2123 * @id: Supply name or regulator ID
2124 *
2125 * Remove a lookup alias if one exists for id on dev.
2126 */
2127void regulator_unregister_supply_alias(struct device *dev, const char *id)
2128{
2129        struct regulator_supply_alias *map;
2130
2131        map = regulator_find_supply_alias(dev, id);
2132        if (map) {
2133                list_del(&map->list);
2134                kfree(map);
2135        }
2136}
2137EXPORT_SYMBOL_GPL(regulator_unregister_supply_alias);
2138
2139/**
2140 * regulator_bulk_register_supply_alias - register multiple aliases
2141 *
2142 * @dev: device that will be given as the regulator "consumer"
2143 * @id: List of supply names or regulator IDs
2144 * @alias_dev: device that should be used to lookup the supply
2145 * @alias_id: List of supply names or regulator IDs that should be used to
2146 * lookup the supply
2147 * @num_id: Number of aliases to register
2148 *
2149 * @return 0 on success, an errno on failure.
2150 *
2151 * This helper function allows drivers to register several supply
2152 * aliases in one operation.  If any of the aliases cannot be
2153 * registered any aliases that were registered will be removed
2154 * before returning to the caller.
2155 */
2156int regulator_bulk_register_supply_alias(struct device *dev,
2157                                         const char *const *id,
2158                                         struct device *alias_dev,
2159                                         const char *const *alias_id,
2160                                         int num_id)
2161{
2162        int i;
2163        int ret;
2164
2165        for (i = 0; i < num_id; ++i) {
2166                ret = regulator_register_supply_alias(dev, id[i], alias_dev,
2167                                                      alias_id[i]);
2168                if (ret < 0)
2169                        goto err;
2170        }
2171
2172        return 0;
2173
2174err:
2175        dev_err(dev,
2176                "Failed to create supply alias %s,%s -> %s,%s\n",
2177                id[i], dev_name(dev), alias_id[i], dev_name(alias_dev));
2178
2179        while (--i >= 0)
2180                regulator_unregister_supply_alias(dev, id[i]);
2181
2182        return ret;
2183}
2184EXPORT_SYMBOL_GPL(regulator_bulk_register_supply_alias);
2185
2186/**
2187 * regulator_bulk_unregister_supply_alias - unregister multiple aliases
2188 *
2189 * @dev: device that will be given as the regulator "consumer"
2190 * @id: List of supply names or regulator IDs
2191 * @num_id: Number of aliases to unregister
2192 *
2193 * This helper function allows drivers to unregister several supply
2194 * aliases in one operation.
2195 */
2196void regulator_bulk_unregister_supply_alias(struct device *dev,
2197                                            const char *const *id,
2198                                            int num_id)
2199{
2200        int i;
2201
2202        for (i = 0; i < num_id; ++i)
2203                regulator_unregister_supply_alias(dev, id[i]);
2204}
2205EXPORT_SYMBOL_GPL(regulator_bulk_unregister_supply_alias);
2206
2207
2208/* Manage enable GPIO list. Same GPIO pin can be shared among regulators */
2209static int regulator_ena_gpio_request(struct regulator_dev *rdev,
2210                                const struct regulator_config *config)
2211{
2212        struct regulator_enable_gpio *pin;
2213        struct gpio_desc *gpiod;
2214
2215        gpiod = config->ena_gpiod;
2216
2217        list_for_each_entry(pin, &regulator_ena_gpio_list, list) {
2218                if (pin->gpiod == gpiod) {
2219                        rdev_dbg(rdev, "GPIO is already used\n");
2220                        goto update_ena_gpio_to_rdev;
2221                }
2222        }
2223
2224        pin = kzalloc(sizeof(struct regulator_enable_gpio), GFP_KERNEL);
2225        if (pin == NULL)
2226                return -ENOMEM;
2227
2228        pin->gpiod = gpiod;
2229        list_add(&pin->list, &regulator_ena_gpio_list);
2230
2231update_ena_gpio_to_rdev:
2232        pin->request_count++;
2233        rdev->ena_pin = pin;
2234        return 0;
2235}
2236
2237static void regulator_ena_gpio_free(struct regulator_dev *rdev)
2238{
2239        struct regulator_enable_gpio *pin, *n;
2240
2241        if (!rdev->ena_pin)
2242                return;
2243
2244        /* Free the GPIO only in case of no use */
2245        list_for_each_entry_safe(pin, n, &regulator_ena_gpio_list, list) {
2246                if (pin->gpiod == rdev->ena_pin->gpiod) {
2247                        if (pin->request_count <= 1) {
2248                                pin->request_count = 0;
2249                                gpiod_put(pin->gpiod);
2250                                list_del(&pin->list);
2251                                kfree(pin);
2252                                rdev->ena_pin = NULL;
2253                                return;
2254                        } else {
2255                                pin->request_count--;
2256                        }
2257                }
2258        }
2259}
2260
2261/**
2262 * regulator_ena_gpio_ctrl - balance enable_count of each GPIO and actual GPIO pin control
2263 * @rdev: regulator_dev structure
2264 * @enable: enable GPIO at initial use?
2265 *
2266 * GPIO is enabled in case of initial use. (enable_count is 0)
2267 * GPIO is disabled when it is not shared any more. (enable_count <= 1)
2268 */
2269static int regulator_ena_gpio_ctrl(struct regulator_dev *rdev, bool enable)
2270{
2271        struct regulator_enable_gpio *pin = rdev->ena_pin;
2272
2273        if (!pin)
2274                return -EINVAL;
2275
2276        if (enable) {
2277                /* Enable GPIO at initial use */
2278                if (pin->enable_count == 0)
2279                        gpiod_set_value_cansleep(pin->gpiod, 1);
2280
2281                pin->enable_count++;
2282        } else {
2283                if (pin->enable_count > 1) {
2284                        pin->enable_count--;
2285                        return 0;
2286                }
2287
2288                /* Disable GPIO if not used */
2289                if (pin->enable_count <= 1) {
2290                        gpiod_set_value_cansleep(pin->gpiod, 0);
2291                        pin->enable_count = 0;
2292                }
2293        }
2294
2295        return 0;
2296}
2297
2298/**
2299 * _regulator_enable_delay - a delay helper function
2300 * @delay: time to delay in microseconds
2301 *
2302 * Delay for the requested amount of time as per the guidelines in:
2303 *
2304 *     Documentation/timers/timers-howto.rst
2305 *
2306 * The assumption here is that regulators will never be enabled in
2307 * atomic context and therefore sleeping functions can be used.
2308 */
2309static void _regulator_enable_delay(unsigned int delay)
2310{
2311        unsigned int ms = delay / 1000;
2312        unsigned int us = delay % 1000;
2313
2314        if (ms > 0) {
2315                /*
2316                 * For small enough values, handle super-millisecond
2317                 * delays in the usleep_range() call below.
2318                 */
2319                if (ms < 20)
2320                        us += ms * 1000;
2321                else
2322                        msleep(ms);
2323        }
2324
2325        /*
2326         * Give the scheduler some room to coalesce with any other
2327         * wakeup sources. For delays shorter than 10 us, don't even
2328         * bother setting up high-resolution timers and just busy-
2329         * loop.
2330         */
2331        if (us >= 10)
2332                usleep_range(us, us + 100);
2333        else
2334                udelay(us);
2335}
2336
2337static int _regulator_do_enable(struct regulator_dev *rdev)
2338{
2339        int ret, delay;
2340
2341        /* Query before enabling in case configuration dependent.  */
2342        ret = _regulator_get_enable_time(rdev);
2343        if (ret >= 0) {
2344                delay = ret;
2345        } else {
2346                rdev_warn(rdev, "enable_time() failed: %d\n", ret);
2347                delay = 0;
2348        }
2349
2350        trace_regulator_enable(rdev_get_name(rdev));
2351
2352        if (rdev->desc->off_on_delay) {
2353                /* if needed, keep a distance of off_on_delay from last time
2354                 * this regulator was disabled.
2355                 */
2356                unsigned long start_jiffy = jiffies;
2357                unsigned long intended, max_delay, remaining;
2358
2359                max_delay = usecs_to_jiffies(rdev->desc->off_on_delay);
2360                intended = rdev->last_off_jiffy + max_delay;
2361
2362                if (time_before(start_jiffy, intended)) {
2363                        /* calc remaining jiffies to deal with one-time
2364                         * timer wrapping.
2365                         * in case of multiple timer wrapping, either it can be
2366                         * detected by out-of-range remaining, or it cannot be
2367                         * detected and we get a penalty of
2368                         * _regulator_enable_delay().
2369                         */
2370                        remaining = intended - start_jiffy;
2371                        if (remaining <= max_delay)
2372                                _regulator_enable_delay(
2373                                                jiffies_to_usecs(remaining));
2374                }
2375        }
2376
2377        if (rdev->ena_pin) {
2378                if (!rdev->ena_gpio_state) {
2379                        ret = regulator_ena_gpio_ctrl(rdev, true);
2380                        if (ret < 0)
2381                                return ret;
2382                        rdev->ena_gpio_state = 1;
2383                }
2384        } else if (rdev->desc->ops->enable) {
2385                ret = rdev->desc->ops->enable(rdev);
2386                if (ret < 0)
2387                        return ret;
2388        } else {
2389                return -EINVAL;
2390        }
2391
2392        /* Allow the regulator to ramp; it would be useful to extend
2393         * this for bulk operations so that the regulators can ramp
2394         * together.  */
2395        trace_regulator_enable_delay(rdev_get_name(rdev));
2396
2397        _regulator_enable_delay(delay);
2398
2399        trace_regulator_enable_complete(rdev_get_name(rdev));
2400
2401        return 0;
2402}
2403
2404/**
2405 * _regulator_handle_consumer_enable - handle that a consumer enabled
2406 * @regulator: regulator source
2407 *
2408 * Some things on a regulator consumer (like the contribution towards total
2409 * load on the regulator) only have an effect when the consumer wants the
2410 * regulator enabled.  Explained in example with two consumers of the same
2411 * regulator:
2412 *   consumer A: set_load(100);       => total load = 0
2413 *   consumer A: regulator_enable();  => total load = 100
2414 *   consumer B: set_load(1000);      => total load = 100
2415 *   consumer B: regulator_enable();  => total load = 1100
2416 *   consumer A: regulator_disable(); => total_load = 1000
2417 *
2418 * This function (together with _regulator_handle_consumer_disable) is
2419 * responsible for keeping track of the refcount for a given regulator consumer
2420 * and applying / unapplying these things.
2421 *
2422 * Returns 0 upon no error; -error upon error.
2423 */
2424static int _regulator_handle_consumer_enable(struct regulator *regulator)
2425{
2426        struct regulator_dev *rdev = regulator->rdev;
2427
2428        lockdep_assert_held_once(&rdev->mutex.base);
2429
2430        regulator->enable_count++;
2431        if (regulator->uA_load && regulator->enable_count == 1)
2432                return drms_uA_update(rdev);
2433
2434        return 0;
2435}
2436
2437/**
2438 * _regulator_handle_consumer_disable - handle that a consumer disabled
2439 * @regulator: regulator source
2440 *
2441 * The opposite of _regulator_handle_consumer_enable().
2442 *
2443 * Returns 0 upon no error; -error upon error.
2444 */
2445static int _regulator_handle_consumer_disable(struct regulator *regulator)
2446{
2447        struct regulator_dev *rdev = regulator->rdev;
2448
2449        lockdep_assert_held_once(&rdev->mutex.base);
2450
2451        if (!regulator->enable_count) {
2452                rdev_err(rdev, "Underflow of regulator enable count\n");
2453                return -EINVAL;
2454        }
2455
2456        regulator->enable_count--;
2457        if (regulator->uA_load && regulator->enable_count == 0)
2458                return drms_uA_update(rdev);
2459
2460        return 0;
2461}
2462
2463/* locks held by regulator_enable() */
2464static int _regulator_enable(struct regulator *regulator)
2465{
2466        struct regulator_dev *rdev = regulator->rdev;
2467        int ret;
2468
2469        lockdep_assert_held_once(&rdev->mutex.base);
2470
2471        if (rdev->use_count == 0 && rdev->supply) {
2472                ret = _regulator_enable(rdev->supply);
2473                if (ret < 0)
2474                        return ret;
2475        }
2476
2477        /* balance only if there are regulators coupled */
2478        if (rdev->coupling_desc.n_coupled > 1) {
2479                ret = regulator_balance_voltage(rdev, PM_SUSPEND_ON);
2480                if (ret < 0)
2481                        goto err_disable_supply;
2482        }
2483
2484        ret = _regulator_handle_consumer_enable(regulator);
2485        if (ret < 0)
2486                goto err_disable_supply;
2487
2488        if (rdev->use_count == 0) {
2489                /* The regulator may on if it's not switchable or left on */
2490                ret = _regulator_is_enabled(rdev);
2491                if (ret == -EINVAL || ret == 0) {
2492                        if (!regulator_ops_is_valid(rdev,
2493                                        REGULATOR_CHANGE_STATUS)) {
2494                                ret = -EPERM;
2495                                goto err_consumer_disable;
2496                        }
2497
2498                        ret = _regulator_do_enable(rdev);
2499                        if (ret < 0)
2500                                goto err_consumer_disable;
2501
2502                        _notifier_call_chain(rdev, REGULATOR_EVENT_ENABLE,
2503                                             NULL);
2504                } else if (ret < 0) {
2505                        rdev_err(rdev, "is_enabled() failed: %d\n", ret);
2506                        goto err_consumer_disable;
2507                }
2508                /* Fallthrough on positive return values - already enabled */
2509        }
2510
2511        rdev->use_count++;
2512
2513        return 0;
2514
2515err_consumer_disable:
2516        _regulator_handle_consumer_disable(regulator);
2517
2518err_disable_supply:
2519        if (rdev->use_count == 0 && rdev->supply)
2520                _regulator_disable(rdev->supply);
2521
2522        return ret;
2523}
2524
2525/**
2526 * regulator_enable - enable regulator output
2527 * @regulator: regulator source
2528 *
2529 * Request that the regulator be enabled with the regulator output at
2530 * the predefined voltage or current value.  Calls to regulator_enable()
2531 * must be balanced with calls to regulator_disable().
2532 *
2533 * NOTE: the output value can be set by other drivers, boot loader or may be
2534 * hardwired in the regulator.
2535 */
2536int regulator_enable(struct regulator *regulator)
2537{
2538        struct regulator_dev *rdev = regulator->rdev;
2539        struct ww_acquire_ctx ww_ctx;
2540        int ret;
2541
2542        regulator_lock_dependent(rdev, &ww_ctx);
2543        ret = _regulator_enable(regulator);
2544        regulator_unlock_dependent(rdev, &ww_ctx);
2545
2546        return ret;
2547}
2548EXPORT_SYMBOL_GPL(regulator_enable);
2549
2550static int _regulator_do_disable(struct regulator_dev *rdev)
2551{
2552        int ret;
2553
2554        trace_regulator_disable(rdev_get_name(rdev));
2555
2556        if (rdev->ena_pin) {
2557                if (rdev->ena_gpio_state) {
2558                        ret = regulator_ena_gpio_ctrl(rdev, false);
2559                        if (ret < 0)
2560                                return ret;
2561                        rdev->ena_gpio_state = 0;
2562                }
2563
2564        } else if (rdev->desc->ops->disable) {
2565                ret = rdev->desc->ops->disable(rdev);
2566                if (ret != 0)
2567                        return ret;
2568        }
2569
2570        /* cares about last_off_jiffy only if off_on_delay is required by
2571         * device.
2572         */
2573        if (rdev->desc->off_on_delay)
2574                rdev->last_off_jiffy = jiffies;
2575
2576        trace_regulator_disable_complete(rdev_get_name(rdev));
2577
2578        return 0;
2579}
2580
2581/* locks held by regulator_disable() */
2582static int _regulator_disable(struct regulator *regulator)
2583{
2584        struct regulator_dev *rdev = regulator->rdev;
2585        int ret = 0;
2586
2587        lockdep_assert_held_once(&rdev->mutex.base);
2588
2589        if (WARN(rdev->use_count <= 0,
2590                 "unbalanced disables for %s\n", rdev_get_name(rdev)))
2591                return -EIO;
2592
2593        /* are we the last user and permitted to disable ? */
2594        if (rdev->use_count == 1 &&
2595            (rdev->constraints && !rdev->constraints->always_on)) {
2596
2597                /* we are last user */
2598                if (regulator_ops_is_valid(rdev, REGULATOR_CHANGE_STATUS)) {
2599                        ret = _notifier_call_chain(rdev,
2600                                                   REGULATOR_EVENT_PRE_DISABLE,
2601                                                   NULL);
2602                        if (ret & NOTIFY_STOP_MASK)
2603                                return -EINVAL;
2604
2605                        ret = _regulator_do_disable(rdev);
2606                        if (ret < 0) {
2607                                rdev_err(rdev, "failed to disable\n");
2608                                _notifier_call_chain(rdev,
2609                                                REGULATOR_EVENT_ABORT_DISABLE,
2610                                                NULL);
2611                                return ret;
2612                        }
2613                        _notifier_call_chain(rdev, REGULATOR_EVENT_DISABLE,
2614                                        NULL);
2615                }
2616
2617                rdev->use_count = 0;
2618        } else if (rdev->use_count > 1) {
2619                rdev->use_count--;
2620        }
2621
2622        if (ret == 0)
2623                ret = _regulator_handle_consumer_disable(regulator);
2624
2625        if (ret == 0 && rdev->coupling_desc.n_coupled > 1)
2626                ret = regulator_balance_voltage(rdev, PM_SUSPEND_ON);
2627
2628        if (ret == 0 && rdev->use_count == 0 && rdev->supply)
2629                ret = _regulator_disable(rdev->supply);
2630
2631        return ret;
2632}
2633
2634/**
2635 * regulator_disable - disable regulator output
2636 * @regulator: regulator source
2637 *
2638 * Disable the regulator output voltage or current.  Calls to
2639 * regulator_enable() must be balanced with calls to
2640 * regulator_disable().
2641 *
2642 * NOTE: this will only disable the regulator output if no other consumer
2643 * devices have it enabled, the regulator device supports disabling and
2644 * machine constraints permit this operation.
2645 */
2646int regulator_disable(struct regulator *regulator)
2647{
2648        struct regulator_dev *rdev = regulator->rdev;
2649        struct ww_acquire_ctx ww_ctx;
2650        int ret;
2651
2652        regulator_lock_dependent(rdev, &ww_ctx);
2653        ret = _regulator_disable(regulator);
2654        regulator_unlock_dependent(rdev, &ww_ctx);
2655
2656        return ret;
2657}
2658EXPORT_SYMBOL_GPL(regulator_disable);
2659
2660/* locks held by regulator_force_disable() */
2661static int _regulator_force_disable(struct regulator_dev *rdev)
2662{
2663        int ret = 0;
2664
2665        lockdep_assert_held_once(&rdev->mutex.base);
2666
2667        ret = _notifier_call_chain(rdev, REGULATOR_EVENT_FORCE_DISABLE |
2668                        REGULATOR_EVENT_PRE_DISABLE, NULL);
2669        if (ret & NOTIFY_STOP_MASK)
2670                return -EINVAL;
2671
2672        ret = _regulator_do_disable(rdev);
2673        if (ret < 0) {
2674                rdev_err(rdev, "failed to force disable\n");
2675                _notifier_call_chain(rdev, REGULATOR_EVENT_FORCE_DISABLE |
2676                                REGULATOR_EVENT_ABORT_DISABLE, NULL);
2677                return ret;
2678        }
2679
2680        _notifier_call_chain(rdev, REGULATOR_EVENT_FORCE_DISABLE |
2681                        REGULATOR_EVENT_DISABLE, NULL);
2682
2683        return 0;
2684}
2685
2686/**
2687 * regulator_force_disable - force disable regulator output
2688 * @regulator: regulator source
2689 *
2690 * Forcibly disable the regulator output voltage or current.
2691 * NOTE: this *will* disable the regulator output even if other consumer
2692 * devices have it enabled. This should be used for situations when device
2693 * damage will likely occur if the regulator is not disabled (e.g. over temp).
2694 */
2695int regulator_force_disable(struct regulator *regulator)
2696{
2697        struct regulator_dev *rdev = regulator->rdev;
2698        struct ww_acquire_ctx ww_ctx;
2699        int ret;
2700
2701        regulator_lock_dependent(rdev, &ww_ctx);
2702
2703        ret = _regulator_force_disable(regulator->rdev);
2704
2705        if (rdev->coupling_desc.n_coupled > 1)
2706                regulator_balance_voltage(rdev, PM_SUSPEND_ON);
2707
2708        if (regulator->uA_load) {
2709                regulator->uA_load = 0;
2710                ret = drms_uA_update(rdev);
2711        }
2712
2713        if (rdev->use_count != 0 && rdev->supply)
2714                _regulator_disable(rdev->supply);
2715
2716        regulator_unlock_dependent(rdev, &ww_ctx);
2717
2718        return ret;
2719}
2720EXPORT_SYMBOL_GPL(regulator_force_disable);
2721
2722static void regulator_disable_work(struct work_struct *work)
2723{
2724        struct regulator_dev *rdev = container_of(work, struct regulator_dev,
2725                                                  disable_work.work);
2726        struct ww_acquire_ctx ww_ctx;
2727        int count, i, ret;
2728        struct regulator *regulator;
2729        int total_count = 0;
2730
2731        regulator_lock_dependent(rdev, &ww_ctx);
2732
2733        /*
2734         * Workqueue functions queue the new work instance while the previous
2735         * work instance is being processed. Cancel the queued work instance
2736         * as the work instance under processing does the job of the queued
2737         * work instance.
2738         */
2739        cancel_delayed_work(&rdev->disable_work);
2740
2741        list_for_each_entry(regulator, &rdev->consumer_list, list) {
2742                count = regulator->deferred_disables;
2743
2744                if (!count)
2745                        continue;
2746
2747                total_count += count;
2748                regulator->deferred_disables = 0;
2749
2750                for (i = 0; i < count; i++) {
2751                        ret = _regulator_disable(regulator);
2752                        if (ret != 0)
2753                                rdev_err(rdev, "Deferred disable failed: %d\n", ret);
2754                }
2755        }
2756        WARN_ON(!total_count);
2757
2758        if (rdev->coupling_desc.n_coupled > 1)
2759                regulator_balance_voltage(rdev, PM_SUSPEND_ON);
2760
2761        regulator_unlock_dependent(rdev, &ww_ctx);
2762}
2763
2764/**
2765 * regulator_disable_deferred - disable regulator output with delay
2766 * @regulator: regulator source
2767 * @ms: milliseconds until the regulator is disabled
2768 *
2769 * Execute regulator_disable() on the regulator after a delay.  This
2770 * is intended for use with devices that require some time to quiesce.
2771 *
2772 * NOTE: this will only disable the regulator output if no other consumer
2773 * devices have it enabled, the regulator device supports disabling and
2774 * machine constraints permit this operation.
2775 */
2776int regulator_disable_deferred(struct regulator *regulator, int ms)
2777{
2778        struct regulator_dev *rdev = regulator->rdev;
2779
2780        if (!ms)
2781                return regulator_disable(regulator);
2782
2783        regulator_lock(rdev);
2784        regulator->deferred_disables++;
2785        mod_delayed_work(system_power_efficient_wq, &rdev->disable_work,
2786                         msecs_to_jiffies(ms));
2787        regulator_unlock(rdev);
2788
2789        return 0;
2790}
2791EXPORT_SYMBOL_GPL(regulator_disable_deferred);
2792
2793static int _regulator_is_enabled(struct regulator_dev *rdev)
2794{
2795        /* A GPIO control always takes precedence */
2796        if (rdev->ena_pin)
2797                return rdev->ena_gpio_state;
2798
2799        /* If we don't know then assume that the regulator is always on */
2800        if (!rdev->desc->ops->is_enabled)
2801                return 1;
2802
2803        return rdev->desc->ops->is_enabled(rdev);
2804}
2805
2806static int _regulator_list_voltage(struct regulator_dev *rdev,
2807                                   unsigned selector, int lock)
2808{
2809        const struct regulator_ops *ops = rdev->desc->ops;
2810        int ret;
2811
2812        if (rdev->desc->fixed_uV && rdev->desc->n_voltages == 1 && !selector)
2813                return rdev->desc->fixed_uV;
2814
2815        if (ops->list_voltage) {
2816                if (selector >= rdev->desc->n_voltages)
2817                        return -EINVAL;
2818                if (lock)
2819                        regulator_lock(rdev);
2820                ret = ops->list_voltage(rdev, selector);
2821                if (lock)
2822                        regulator_unlock(rdev);
2823        } else if (rdev->is_switch && rdev->supply) {
2824                ret = _regulator_list_voltage(rdev->supply->rdev,
2825                                              selector, lock);
2826        } else {
2827                return -EINVAL;
2828        }
2829
2830        if (ret > 0) {
2831                if (ret < rdev->constraints->min_uV)
2832                        ret = 0;
2833                else if (ret > rdev->constraints->max_uV)
2834                        ret = 0;
2835        }
2836
2837        return ret;
2838}
2839
2840/**
2841 * regulator_is_enabled - is the regulator output enabled
2842 * @regulator: regulator source
2843 *
2844 * Returns positive if the regulator driver backing the source/client
2845 * has requested that the device be enabled, zero if it hasn't, else a
2846 * negative errno code.
2847 *
2848 * Note that the device backing this regulator handle can have multiple
2849 * users, so it might be enabled even if regulator_enable() was never
2850 * called for this particular source.
2851 */
2852int regulator_is_enabled(struct regulator *regulator)
2853{
2854        int ret;
2855
2856        if (regulator->always_on)
2857                return 1;
2858
2859        regulator_lock(regulator->rdev);
2860        ret = _regulator_is_enabled(regulator->rdev);
2861        regulator_unlock(regulator->rdev);
2862
2863        return ret;
2864}
2865EXPORT_SYMBOL_GPL(regulator_is_enabled);
2866
2867/**
2868 * regulator_count_voltages - count regulator_list_voltage() selectors
2869 * @regulator: regulator source
2870 *
2871 * Returns number of selectors, or negative errno.  Selectors are
2872 * numbered starting at zero, and typically correspond to bitfields
2873 * in hardware registers.
2874 */
2875int regulator_count_voltages(struct regulator *regulator)
2876{
2877        struct regulator_dev    *rdev = regulator->rdev;
2878
2879        if (rdev->desc->n_voltages)
2880                return rdev->desc->n_voltages;
2881
2882        if (!rdev->is_switch || !rdev->supply)
2883                return -EINVAL;
2884
2885        return regulator_count_voltages(rdev->supply);
2886}
2887EXPORT_SYMBOL_GPL(regulator_count_voltages);
2888
2889/**
2890 * regulator_list_voltage - enumerate supported voltages
2891 * @regulator: regulator source
2892 * @selector: identify voltage to list
2893 * Context: can sleep
2894 *
2895 * Returns a voltage that can be passed to @regulator_set_voltage(),
2896 * zero if this selector code can't be used on this system, or a
2897 * negative errno.
2898 */
2899int regulator_list_voltage(struct regulator *regulator, unsigned selector)
2900{
2901        return _regulator_list_voltage(regulator->rdev, selector, 1);
2902}
2903EXPORT_SYMBOL_GPL(regulator_list_voltage);
2904
2905/**
2906 * regulator_get_regmap - get the regulator's register map
2907 * @regulator: regulator source
2908 *
2909 * Returns the register map for the given regulator, or an ERR_PTR value
2910 * if the regulator doesn't use regmap.
2911 */
2912struct regmap *regulator_get_regmap(struct regulator *regulator)
2913{
2914        struct regmap *map = regulator->rdev->regmap;
2915
2916        return map ? map : ERR_PTR(-EOPNOTSUPP);
2917}
2918
2919/**
2920 * regulator_get_hardware_vsel_register - get the HW voltage selector register
2921 * @regulator: regulator source
2922 * @vsel_reg: voltage selector register, output parameter
2923 * @vsel_mask: mask for voltage selector bitfield, output parameter
2924 *
2925 * Returns the hardware register offset and bitmask used for setting the
2926 * regulator voltage. This might be useful when configuring voltage-scaling
2927 * hardware or firmware that can make I2C requests behind the kernel's back,
2928 * for example.
2929 *
2930 * On success, the output parameters @vsel_reg and @vsel_mask are filled in
2931 * and 0 is returned, otherwise a negative errno is returned.
2932 */
2933int regulator_get_hardware_vsel_register(struct regulator *regulator,
2934                                         unsigned *vsel_reg,
2935                                         unsigned *vsel_mask)
2936{
2937        struct regulator_dev *rdev = regulator->rdev;
2938        const struct regulator_ops *ops = rdev->desc->ops;
2939
2940        if (ops->set_voltage_sel != regulator_set_voltage_sel_regmap)
2941                return -EOPNOTSUPP;
2942
2943        *vsel_reg = rdev->desc->vsel_reg;
2944        *vsel_mask = rdev->desc->vsel_mask;
2945
2946         return 0;
2947}
2948EXPORT_SYMBOL_GPL(regulator_get_hardware_vsel_register);
2949
2950/**
2951 * regulator_list_hardware_vsel - get the HW-specific register value for a selector
2952 * @regulator: regulator source
2953 * @selector: identify voltage to list
2954 *
2955 * Converts the selector to a hardware-specific voltage selector that can be
2956 * directly written to the regulator registers. The address of the voltage
2957 * register can be determined by calling @regulator_get_hardware_vsel_register.
2958 *
2959 * On error a negative errno is returned.
2960 */
2961int regulator_list_hardware_vsel(struct regulator *regulator,
2962                                 unsigned selector)
2963{
2964        struct regulator_dev *rdev = regulator->rdev;
2965        const struct regulator_ops *ops = rdev->desc->ops;
2966
2967        if (selector >= rdev->desc->n_voltages)
2968                return -EINVAL;
2969        if (ops->set_voltage_sel != regulator_set_voltage_sel_regmap)
2970                return -EOPNOTSUPP;
2971
2972        return selector;
2973}
2974EXPORT_SYMBOL_GPL(regulator_list_hardware_vsel);
2975
2976/**
2977 * regulator_get_linear_step - return the voltage step size between VSEL values
2978 * @regulator: regulator source
2979 *
2980 * Returns the voltage step size between VSEL values for linear
2981 * regulators, or return 0 if the regulator isn't a linear regulator.
2982 */
2983unsigned int regulator_get_linear_step(struct regulator *regulator)
2984{
2985        struct regulator_dev *rdev = regulator->rdev;
2986
2987        return rdev->desc->uV_step;
2988}
2989EXPORT_SYMBOL_GPL(regulator_get_linear_step);
2990
2991/**
2992 * regulator_is_supported_voltage - check if a voltage range can be supported
2993 *
2994 * @regulator: Regulator to check.
2995 * @min_uV: Minimum required voltage in uV.
2996 * @max_uV: Maximum required voltage in uV.
2997 *
2998 * Returns a boolean.
2999 */
3000int regulator_is_supported_voltage(struct regulator *regulator,
3001                                   int min_uV, int max_uV)
3002{
3003        struct regulator_dev *rdev = regulator->rdev;
3004        int i, voltages, ret;
3005
3006        /* If we can't change voltage check the current voltage */
3007        if (!regulator_ops_is_valid(rdev, REGULATOR_CHANGE_VOLTAGE)) {
3008                ret = regulator_get_voltage(regulator);
3009                if (ret >= 0)
3010                        return min_uV <= ret && ret <= max_uV;
3011                else
3012                        return ret;
3013        }
3014
3015        /* Any voltage within constrains range is fine? */
3016        if (rdev->desc->continuous_voltage_range)
3017                return min_uV >= rdev->constraints->min_uV &&
3018                                max_uV <= rdev->constraints->max_uV;
3019
3020        ret = regulator_count_voltages(regulator);
3021        if (ret < 0)
3022                return 0;
3023        voltages = ret;
3024
3025        for (i = 0; i < voltages; i++) {
3026                ret = regulator_list_voltage(regulator, i);
3027
3028                if (ret >= min_uV && ret <= max_uV)
3029                        return 1;
3030        }
3031
3032        return 0;
3033}
3034EXPORT_SYMBOL_GPL(regulator_is_supported_voltage);
3035
3036static int regulator_map_voltage(struct regulator_dev *rdev, int min_uV,
3037                                 int max_uV)
3038{
3039        const struct regulator_desc *desc = rdev->desc;
3040
3041        if (desc->ops->map_voltage)
3042                return desc->ops->map_voltage(rdev, min_uV, max_uV);
3043
3044        if (desc->ops->list_voltage == regulator_list_voltage_linear)
3045                return regulator_map_voltage_linear(rdev, min_uV, max_uV);
3046
3047        if (desc->ops->list_voltage == regulator_list_voltage_linear_range)
3048                return regulator_map_voltage_linear_range(rdev, min_uV, max_uV);
3049
3050        if (desc->ops->list_voltage ==
3051                regulator_list_voltage_pickable_linear_range)
3052                return regulator_map_voltage_pickable_linear_range(rdev,
3053                                                        min_uV, max_uV);
3054
3055        return regulator_map_voltage_iterate(rdev, min_uV, max_uV);
3056}
3057
3058static int _regulator_call_set_voltage(struct regulator_dev *rdev,
3059                                       int min_uV, int max_uV,
3060                                       unsigned *selector)
3061{
3062        struct pre_voltage_change_data data;
3063        int ret;
3064
3065        data.old_uV = regulator_get_voltage_rdev(rdev);
3066        data.min_uV = min_uV;
3067        data.max_uV = max_uV;
3068        ret = _notifier_call_chain(rdev, REGULATOR_EVENT_PRE_VOLTAGE_CHANGE,
3069                                   &data);
3070        if (ret & NOTIFY_STOP_MASK)
3071                return -EINVAL;
3072
3073        ret = rdev->desc->ops->set_voltage(rdev, min_uV, max_uV, selector);
3074        if (ret >= 0)
3075                return ret;
3076
3077        _notifier_call_chain(rdev, REGULATOR_EVENT_ABORT_VOLTAGE_CHANGE,
3078                             (void *)data.old_uV);
3079
3080        return ret;
3081}
3082
3083static int _regulator_call_set_voltage_sel(struct regulator_dev *rdev,
3084                                           int uV, unsigned selector)
3085{
3086        struct pre_voltage_change_data data;
3087        int ret;
3088
3089        data.old_uV = regulator_get_voltage_rdev(rdev);
3090        data.min_uV = uV;
3091        data.max_uV = uV;
3092        ret = _notifier_call_chain(rdev, REGULATOR_EVENT_PRE_VOLTAGE_CHANGE,
3093                                   &data);
3094        if (ret & NOTIFY_STOP_MASK)
3095                return -EINVAL;
3096
3097        ret = rdev->desc->ops->set_voltage_sel(rdev, selector);
3098        if (ret >= 0)
3099                return ret;
3100
3101        _notifier_call_chain(rdev, REGULATOR_EVENT_ABORT_VOLTAGE_CHANGE,
3102                             (void *)data.old_uV);
3103
3104        return ret;
3105}
3106
3107static int _regulator_set_voltage_sel_step(struct regulator_dev *rdev,
3108                                           int uV, int new_selector)
3109{
3110        const struct regulator_ops *ops = rdev->desc->ops;
3111        int diff, old_sel, curr_sel, ret;
3112
3113        /* Stepping is only needed if the regulator is enabled. */
3114        if (!_regulator_is_enabled(rdev))
3115                goto final_set;
3116
3117        if (!ops->get_voltage_sel)
3118                return -EINVAL;
3119
3120        old_sel = ops->get_voltage_sel(rdev);
3121        if (old_sel < 0)
3122                return old_sel;
3123
3124        diff = new_selector - old_sel;
3125        if (diff == 0)
3126                return 0; /* No change needed. */
3127
3128        if (diff > 0) {
3129                /* Stepping up. */
3130                for (curr_sel = old_sel + rdev->desc->vsel_step;
3131                     curr_sel < new_selector;
3132                     curr_sel += rdev->desc->vsel_step) {
3133                        /*
3134                         * Call the callback directly instead of using
3135                         * _regulator_call_set_voltage_sel() as we don't
3136                         * want to notify anyone yet. Same in the branch
3137                         * below.
3138                         */
3139                        ret = ops->set_voltage_sel(rdev, curr_sel);
3140                        if (ret)
3141                                goto try_revert;
3142                }
3143        } else {
3144                /* Stepping down. */
3145                for (curr_sel = old_sel - rdev->desc->vsel_step;
3146                     curr_sel > new_selector;
3147                     curr_sel -= rdev->desc->vsel_step) {
3148                        ret = ops->set_voltage_sel(rdev, curr_sel);
3149                        if (ret)
3150                                goto try_revert;
3151                }
3152        }
3153
3154final_set:
3155        /* The final selector will trigger the notifiers. */
3156        return _regulator_call_set_voltage_sel(rdev, uV, new_selector);
3157
3158try_revert:
3159        /*
3160         * At least try to return to the previous voltage if setting a new
3161         * one failed.
3162         */
3163        (void)ops->set_voltage_sel(rdev, old_sel);
3164        return ret;
3165}
3166
3167static int _regulator_set_voltage_time(struct regulator_dev *rdev,
3168                                       int old_uV, int new_uV)
3169{
3170        unsigned int ramp_delay = 0;
3171
3172        if (rdev->constraints->ramp_delay)
3173                ramp_delay = rdev->constraints->ramp_delay;
3174        else if (rdev->desc->ramp_delay)
3175                ramp_delay = rdev->desc->ramp_delay;
3176        else if (rdev->constraints->settling_time)
3177                return rdev->constraints->settling_time;
3178        else if (rdev->constraints->settling_time_up &&
3179                 (new_uV > old_uV))
3180                return rdev->constraints->settling_time_up;
3181        else if (rdev->constraints->settling_time_down &&
3182                 (new_uV < old_uV))
3183                return rdev->constraints->settling_time_down;
3184
3185        if (ramp_delay == 0) {
3186                rdev_dbg(rdev, "ramp_delay not set\n");
3187                return 0;
3188        }
3189
3190        return DIV_ROUND_UP(abs(new_uV - old_uV), ramp_delay);
3191}
3192
3193static int _regulator_do_set_voltage(struct regulator_dev *rdev,
3194                                     int min_uV, int max_uV)
3195{
3196        int ret;
3197        int delay = 0;
3198        int best_val = 0;
3199        unsigned int selector;
3200        int old_selector = -1;
3201        const struct regulator_ops *ops = rdev->desc->ops;
3202        int old_uV = regulator_get_voltage_rdev(rdev);
3203
3204        trace_regulator_set_voltage(rdev_get_name(rdev), min_uV, max_uV);
3205
3206        min_uV += rdev->constraints->uV_offset;
3207        max_uV += rdev->constraints->uV_offset;
3208
3209        /*
3210         * If we can't obtain the old selector there is not enough
3211         * info to call set_voltage_time_sel().
3212         */
3213        if (_regulator_is_enabled(rdev) &&
3214            ops->set_voltage_time_sel && ops->get_voltage_sel) {
3215                old_selector = ops->get_voltage_sel(rdev);
3216                if (old_selector < 0)
3217                        return old_selector;
3218        }
3219
3220        if (ops->set_voltage) {
3221                ret = _regulator_call_set_voltage(rdev, min_uV, max_uV,
3222                                                  &selector);
3223
3224                if (ret >= 0) {
3225                        if (ops->list_voltage)
3226                                best_val = ops->list_voltage(rdev,
3227                                                             selector);
3228                        else
3229                                best_val = regulator_get_voltage_rdev(rdev);
3230                }
3231
3232        } else if (ops->set_voltage_sel) {
3233                ret = regulator_map_voltage(rdev, min_uV, max_uV);
3234                if (ret >= 0) {
3235                        best_val = ops->list_voltage(rdev, ret);
3236                        if (min_uV <= best_val && max_uV >= best_val) {
3237                                selector = ret;
3238                                if (old_selector == selector)
3239                                        ret = 0;
3240                                else if (rdev->desc->vsel_step)
3241                                        ret = _regulator_set_voltage_sel_step(
3242                                                rdev, best_val, selector);
3243                                else
3244                                        ret = _regulator_call_set_voltage_sel(
3245                                                rdev, best_val, selector);
3246                        } else {
3247                                ret = -EINVAL;
3248                        }
3249                }
3250        } else {
3251                ret = -EINVAL;
3252        }
3253
3254        if (ret)
3255                goto out;
3256
3257        if (ops->set_voltage_time_sel) {
3258                /*
3259                 * Call set_voltage_time_sel if successfully obtained
3260                 * old_selector
3261                 */
3262                if (old_selector >= 0 && old_selector != selector)
3263                        delay = ops->set_voltage_time_sel(rdev, old_selector,
3264                                                          selector);
3265        } else {
3266                if (old_uV != best_val) {
3267                        if (ops->set_voltage_time)
3268                                delay = ops->set_voltage_time(rdev, old_uV,
3269                                                              best_val);
3270                        else
3271                                delay = _regulator_set_voltage_time(rdev,
3272                                                                    old_uV,
3273                                                                    best_val);
3274                }
3275        }
3276
3277        if (delay < 0) {
3278                rdev_warn(rdev, "failed to get delay: %d\n", delay);
3279                delay = 0;
3280        }
3281
3282        /* Insert any necessary delays */
3283        if (delay >= 1000) {
3284                mdelay(delay / 1000);
3285                udelay(delay % 1000);
3286        } else if (delay) {
3287                udelay(delay);
3288        }
3289
3290        if (best_val >= 0) {
3291                unsigned long data = best_val;
3292
3293                _notifier_call_chain(rdev, REGULATOR_EVENT_VOLTAGE_CHANGE,
3294                                     (void *)data);
3295        }
3296
3297out:
3298        trace_regulator_set_voltage_complete(rdev_get_name(rdev), best_val);
3299
3300        return ret;
3301}
3302
3303static int _regulator_do_set_suspend_voltage(struct regulator_dev *rdev,
3304                                  int min_uV, int max_uV, suspend_state_t state)
3305{
3306        struct regulator_state *rstate;
3307        int uV, sel;
3308
3309        rstate = regulator_get_suspend_state(rdev, state);
3310        if (rstate == NULL)
3311                return -EINVAL;
3312
3313        if (min_uV < rstate->min_uV)
3314                min_uV = rstate->min_uV;
3315        if (max_uV > rstate->max_uV)
3316                max_uV = rstate->max_uV;
3317
3318        sel = regulator_map_voltage(rdev, min_uV, max_uV);
3319        if (sel < 0)
3320                return sel;
3321
3322        uV = rdev->desc->ops->list_voltage(rdev, sel);
3323        if (uV >= min_uV && uV <= max_uV)
3324                rstate->uV = uV;
3325
3326        return 0;
3327}
3328
3329static int regulator_set_voltage_unlocked(struct regulator *regulator,
3330                                          int min_uV, int max_uV,
3331                                          suspend_state_t state)
3332{
3333        struct regulator_dev *rdev = regulator->rdev;
3334        struct regulator_voltage *voltage = &regulator->voltage[state];
3335        int ret = 0;
3336        int old_min_uV, old_max_uV;
3337        int current_uV;
3338
3339        /* If we're setting the same range as last time the change
3340         * should be a noop (some cpufreq implementations use the same
3341         * voltage for multiple frequencies, for example).
3342         */
3343        if (voltage->min_uV == min_uV && voltage->max_uV == max_uV)
3344                goto out;
3345
3346        /* If we're trying to set a range that overlaps the current voltage,
3347         * return successfully even though the regulator does not support
3348         * changing the voltage.
3349         */
3350        if (!regulator_ops_is_valid(rdev, REGULATOR_CHANGE_VOLTAGE)) {
3351                current_uV = regulator_get_voltage_rdev(rdev);
3352                if (min_uV <= current_uV && current_uV <= max_uV) {
3353                        voltage->min_uV = min_uV;
3354                        voltage->max_uV = max_uV;
3355                        goto out;
3356                }
3357        }
3358
3359        /* sanity check */
3360        if (!rdev->desc->ops->set_voltage &&
3361            !rdev->desc->ops->set_voltage_sel) {
3362                ret = -EINVAL;
3363                goto out;
3364        }
3365
3366        /* constraints check */
3367        ret = regulator_check_voltage(rdev, &min_uV, &max_uV);
3368        if (ret < 0)
3369                goto out;
3370
3371        /* restore original values in case of error */
3372        old_min_uV = voltage->min_uV;
3373        old_max_uV = voltage->max_uV;
3374        voltage->min_uV = min_uV;
3375        voltage->max_uV = max_uV;
3376
3377        /* for not coupled regulators this will just set the voltage */
3378        ret = regulator_balance_voltage(rdev, state);
3379        if (ret < 0) {
3380                voltage->min_uV = old_min_uV;
3381                voltage->max_uV = old_max_uV;
3382        }
3383
3384out:
3385        return ret;
3386}
3387
3388int regulator_set_voltage_rdev(struct regulator_dev *rdev, int min_uV,
3389                               int max_uV, suspend_state_t state)
3390{
3391        int best_supply_uV = 0;
3392        int supply_change_uV = 0;
3393        int ret;
3394
3395        if (rdev->supply &&
3396            regulator_ops_is_valid(rdev->supply->rdev,
3397                                   REGULATOR_CHANGE_VOLTAGE) &&
3398            (rdev->desc->min_dropout_uV || !(rdev->desc->ops->get_voltage ||
3399                                           rdev->desc->ops->get_voltage_sel))) {
3400                int current_supply_uV;
3401                int selector;
3402
3403                selector = regulator_map_voltage(rdev, min_uV, max_uV);
3404                if (selector < 0) {
3405                        ret = selector;
3406                        goto out;
3407                }
3408
3409                best_supply_uV = _regulator_list_voltage(rdev, selector, 0);
3410                if (best_supply_uV < 0) {
3411                        ret = best_supply_uV;
3412                        goto out;
3413                }
3414
3415                best_supply_uV += rdev->desc->min_dropout_uV;
3416
3417                current_supply_uV = regulator_get_voltage_rdev(rdev->supply->rdev);
3418                if (current_supply_uV < 0) {
3419                        ret = current_supply_uV;
3420                        goto out;
3421                }
3422
3423                supply_change_uV = best_supply_uV - current_supply_uV;
3424        }
3425
3426        if (supply_change_uV > 0) {
3427                ret = regulator_set_voltage_unlocked(rdev->supply,
3428                                best_supply_uV, INT_MAX, state);
3429                if (ret) {
3430                        dev_err(&rdev->dev, "Failed to increase supply voltage: %d\n",
3431                                        ret);
3432                        goto out;
3433                }
3434        }
3435
3436        if (state == PM_SUSPEND_ON)
3437                ret = _regulator_do_set_voltage(rdev, min_uV, max_uV);
3438        else
3439                ret = _regulator_do_set_suspend_voltage(rdev, min_uV,
3440                                                        max_uV, state);
3441        if (ret < 0)
3442                goto out;
3443
3444        if (supply_change_uV < 0) {
3445                ret = regulator_set_voltage_unlocked(rdev->supply,
3446                                best_supply_uV, INT_MAX, state);
3447                if (ret)
3448                        dev_warn(&rdev->dev, "Failed to decrease supply voltage: %d\n",
3449                                        ret);
3450                /* No need to fail here */
3451                ret = 0;
3452        }
3453
3454out:
3455        return ret;
3456}
3457
3458static int regulator_limit_voltage_step(struct regulator_dev *rdev,
3459                                        int *current_uV, int *min_uV)
3460{
3461        struct regulation_constraints *constraints = rdev->constraints;
3462
3463        /* Limit voltage change only if necessary */
3464        if (!constraints->max_uV_step || !_regulator_is_enabled(rdev))
3465                return 1;
3466
3467        if (*current_uV < 0) {
3468                *current_uV = regulator_get_voltage_rdev(rdev);
3469
3470                if (*current_uV < 0)
3471                        return *current_uV;
3472        }
3473
3474        if (abs(*current_uV - *min_uV) <= constraints->max_uV_step)
3475                return 1;
3476
3477        /* Clamp target voltage within the given step */
3478        if (*current_uV < *min_uV)
3479                *min_uV = min(*current_uV + constraints->max_uV_step,
3480                              *min_uV);
3481        else
3482                *min_uV = max(*current_uV - constraints->max_uV_step,
3483                              *min_uV);
3484
3485        return 0;
3486}
3487
3488static int regulator_get_optimal_voltage(struct regulator_dev *rdev,
3489                                         int *current_uV,
3490                                         int *min_uV, int *max_uV,
3491                                         suspend_state_t state,
3492                                         int n_coupled)
3493{
3494        struct coupling_desc *c_desc = &rdev->coupling_desc;
3495        struct regulator_dev **c_rdevs = c_desc->coupled_rdevs;
3496        struct regulation_constraints *constraints = rdev->constraints;
3497        int desired_min_uV = 0, desired_max_uV = INT_MAX;
3498        int max_current_uV = 0, min_current_uV = INT_MAX;
3499        int highest_min_uV = 0, target_uV, possible_uV;
3500        int i, ret, max_spread;
3501        bool done;
3502
3503        *current_uV = -1;
3504
3505        /*
3506         * If there are no coupled regulators, simply set the voltage
3507         * demanded by consumers.
3508         */
3509        if (n_coupled == 1) {
3510                /*
3511                 * If consumers don't provide any demands, set voltage
3512                 * to min_uV
3513                 */
3514                desired_min_uV = constraints->min_uV;
3515                desired_max_uV = constraints->max_uV;
3516
3517                ret = regulator_check_consumers(rdev,
3518                                                &desired_min_uV,
3519                                                &desired_max_uV, state);
3520                if (ret < 0)
3521                        return ret;
3522
3523                possible_uV = desired_min_uV;
3524                done = true;
3525
3526                goto finish;
3527        }
3528
3529        /* Find highest min desired voltage */
3530        for (i = 0; i < n_coupled; i++) {
3531                int tmp_min = 0;
3532                int tmp_max = INT_MAX;
3533
3534                lockdep_assert_held_once(&c_rdevs[i]->mutex.base);
3535
3536                ret = regulator_check_consumers(c_rdevs[i],
3537                                                &tmp_min,
3538                                                &tmp_max, state);
3539                if (ret < 0)
3540                        return ret;
3541
3542                ret = regulator_check_voltage(c_rdevs[i], &tmp_min, &tmp_max);
3543                if (ret < 0)
3544                        return ret;
3545
3546                highest_min_uV = max(highest_min_uV, tmp_min);
3547
3548                if (i == 0) {
3549                        desired_min_uV = tmp_min;
3550                        desired_max_uV = tmp_max;
3551                }
3552        }
3553
3554        max_spread = constraints->max_spread[0];
3555
3556        /*
3557         * Let target_uV be equal to the desired one if possible.
3558         * If not, set it to minimum voltage, allowed by other coupled
3559         * regulators.
3560         */
3561        target_uV = max(desired_min_uV, highest_min_uV - max_spread);
3562
3563        /*
3564         * Find min and max voltages, which currently aren't violating
3565         * max_spread.
3566         */
3567        for (i = 1; i < n_coupled; i++) {
3568                int tmp_act;
3569
3570                if (!_regulator_is_enabled(c_rdevs[i]))
3571                        continue;
3572
3573                tmp_act = regulator_get_voltage_rdev(c_rdevs[i]);
3574                if (tmp_act < 0)
3575                        return tmp_act;
3576
3577                min_current_uV = min(tmp_act, min_current_uV);
3578                max_current_uV = max(tmp_act, max_current_uV);
3579        }
3580
3581        /* There aren't any other regulators enabled */
3582        if (max_current_uV == 0) {
3583                possible_uV = target_uV;
3584        } else {
3585                /*
3586                 * Correct target voltage, so as it currently isn't
3587                 * violating max_spread
3588                 */
3589                possible_uV = max(target_uV, max_current_uV - max_spread);
3590                possible_uV = min(possible_uV, min_current_uV + max_spread);
3591        }
3592
3593        if (possible_uV > desired_max_uV)
3594                return -EINVAL;
3595
3596        done = (possible_uV == target_uV);
3597        desired_min_uV = possible_uV;
3598
3599finish:
3600        /* Apply max_uV_step constraint if necessary */
3601        if (state == PM_SUSPEND_ON) {
3602                ret = regulator_limit_voltage_step(rdev, current_uV,
3603                                                   &desired_min_uV);
3604                if (ret < 0)
3605                        return ret;
3606
3607                if (ret == 0)
3608                        done = false;
3609        }
3610
3611        /* Set current_uV if wasn't done earlier in the code and if necessary */
3612        if (n_coupled > 1 && *current_uV == -1) {
3613
3614                if (_regulator_is_enabled(rdev)) {
3615                        ret = regulator_get_voltage_rdev(rdev);
3616                        if (ret < 0)
3617                                return ret;
3618
3619                        *current_uV = ret;
3620                } else {
3621                        *current_uV = desired_min_uV;
3622                }
3623        }
3624
3625        *min_uV = desired_min_uV;
3626        *max_uV = desired_max_uV;
3627
3628        return done;
3629}
3630
3631static int regulator_balance_voltage(struct regulator_dev *rdev,
3632                                     suspend_state_t state)
3633{
3634        struct regulator_dev **c_rdevs;
3635        struct regulator_dev *best_rdev;
3636        struct coupling_desc *c_desc = &rdev->coupling_desc;
3637        struct regulator_coupler *coupler = c_desc->coupler;
3638        int i, ret, n_coupled, best_min_uV, best_max_uV, best_c_rdev;
3639        unsigned int delta, best_delta;
3640        unsigned long c_rdev_done = 0;
3641        bool best_c_rdev_done;
3642
3643        c_rdevs = c_desc->coupled_rdevs;
3644        n_coupled = c_desc->n_coupled;
3645
3646        /*
3647         * If system is in a state other than PM_SUSPEND_ON, don't check
3648         * other coupled regulators.
3649         */
3650        if (state != PM_SUSPEND_ON)
3651                n_coupled = 1;
3652
3653        if (c_desc->n_resolved < n_coupled) {
3654                rdev_err(rdev, "Not all coupled regulators registered\n");
3655                return -EPERM;
3656        }
3657
3658        /* Invoke custom balancer for customized couplers */
3659        if (coupler && coupler->balance_voltage)
3660                return coupler->balance_voltage(coupler, rdev, state);
3661
3662        /*
3663         * Find the best possible voltage change on each loop. Leave the loop
3664         * if there isn't any possible change.
3665         */
3666        do {
3667                best_c_rdev_done = false;
3668                best_delta = 0;
3669                best_min_uV = 0;
3670                best_max_uV = 0;
3671                best_c_rdev = 0;
3672                best_rdev = NULL;
3673
3674                /*
3675                 * Find highest difference between optimal voltage
3676                 * and current voltage.
3677                 */
3678                for (i = 0; i < n_coupled; i++) {
3679                        /*
3680                         * optimal_uV is the best voltage that can be set for
3681                         * i-th regulator at the moment without violating
3682                         * max_spread constraint in order to balance
3683                         * the coupled voltages.
3684                         */
3685                        int optimal_uV = 0, optimal_max_uV = 0, current_uV = 0;
3686
3687                        if (test_bit(i, &c_rdev_done))
3688                                continue;
3689
3690                        ret = regulator_get_optimal_voltage(c_rdevs[i],
3691                                                            &current_uV,
3692                                                            &optimal_uV,
3693                                                            &optimal_max_uV,
3694                                                            state, n_coupled);
3695                        if (ret < 0)
3696                                goto out;
3697
3698                        delta = abs(optimal_uV - current_uV);
3699
3700                        if (delta && best_delta <= delta) {
3701                                best_c_rdev_done = ret;
3702                                best_delta = delta;
3703                                best_rdev = c_rdevs[i];
3704                                best_min_uV = optimal_uV;
3705                                best_max_uV = optimal_max_uV;
3706                                best_c_rdev = i;
3707                        }
3708                }
3709
3710                /* Nothing to change, return successfully */
3711                if (!best_rdev) {
3712                        ret = 0;
3713                        goto out;
3714                }
3715
3716                ret = regulator_set_voltage_rdev(best_rdev, best_min_uV,
3717                                                 best_max_uV, state);
3718
3719                if (ret < 0)
3720                        goto out;
3721
3722                if (best_c_rdev_done)
3723                        set_bit(best_c_rdev, &c_rdev_done);
3724
3725        } while (n_coupled > 1);
3726
3727out:
3728        return ret;
3729}
3730
3731/**
3732 * regulator_set_voltage - set regulator output voltage
3733 * @regulator: regulator source
3734 * @min_uV: Minimum required voltage in uV
3735 * @max_uV: Maximum acceptable voltage in uV
3736 *
3737 * Sets a voltage regulator to the desired output voltage. This can be set
3738 * during any regulator state. IOW, regulator can be disabled or enabled.
3739 *
3740 * If the regulator is enabled then the voltage will change to the new value
3741 * immediately otherwise if the regulator is disabled the regulator will
3742 * output at the new voltage when enabled.
3743 *
3744 * NOTE: If the regulator is shared between several devices then the lowest
3745 * request voltage that meets the system constraints will be used.
3746 * Regulator system constraints must be set for this regulator before
3747 * calling this function otherwise this call will fail.
3748 */
3749int regulator_set_voltage(struct regulator *regulator, int min_uV, int max_uV)
3750{
3751        struct ww_acquire_ctx ww_ctx;
3752        int ret;
3753
3754        regulator_lock_dependent(regulator->rdev, &ww_ctx);
3755
3756        ret = regulator_set_voltage_unlocked(regulator, min_uV, max_uV,
3757                                             PM_SUSPEND_ON);
3758
3759        regulator_unlock_dependent(regulator->rdev, &ww_ctx);
3760
3761        return ret;
3762}
3763EXPORT_SYMBOL_GPL(regulator_set_voltage);
3764
3765static inline int regulator_suspend_toggle(struct regulator_dev *rdev,
3766                                           suspend_state_t state, bool en)
3767{
3768        struct regulator_state *rstate;
3769
3770        rstate = regulator_get_suspend_state(rdev, state);
3771        if (rstate == NULL)
3772                return -EINVAL;
3773
3774        if (!rstate->changeable)
3775                return -EPERM;
3776
3777        rstate->enabled = (en) ? ENABLE_IN_SUSPEND : DISABLE_IN_SUSPEND;
3778
3779        return 0;
3780}
3781
3782int regulator_suspend_enable(struct regulator_dev *rdev,
3783                                    suspend_state_t state)
3784{
3785        return regulator_suspend_toggle(rdev, state, true);
3786}
3787EXPORT_SYMBOL_GPL(regulator_suspend_enable);
3788
3789int regulator_suspend_disable(struct regulator_dev *rdev,
3790                                     suspend_state_t state)
3791{
3792        struct regulator *regulator;
3793        struct regulator_voltage *voltage;
3794
3795        /*
3796         * if any consumer wants this regulator device keeping on in
3797         * suspend states, don't set it as disabled.
3798         */
3799        list_for_each_entry(regulator, &rdev->consumer_list, list) {
3800                voltage = &regulator->voltage[state];
3801                if (voltage->min_uV || voltage->max_uV)
3802                        return 0;
3803        }
3804
3805        return regulator_suspend_toggle(rdev, state, false);
3806}
3807EXPORT_SYMBOL_GPL(regulator_suspend_disable);
3808
3809static int _regulator_set_suspend_voltage(struct regulator *regulator,
3810                                          int min_uV, int max_uV,
3811                                          suspend_state_t state)
3812{
3813        struct regulator_dev *rdev = regulator->rdev;
3814        struct regulator_state *rstate;
3815
3816        rstate = regulator_get_suspend_state(rdev, state);
3817        if (rstate == NULL)
3818                return -EINVAL;
3819
3820        if (rstate->min_uV == rstate->max_uV) {
3821                rdev_err(rdev, "The suspend voltage can't be changed!\n");
3822                return -EPERM;
3823        }
3824
3825        return regulator_set_voltage_unlocked(regulator, min_uV, max_uV, state);
3826}
3827
3828int regulator_set_suspend_voltage(struct regulator *regulator, int min_uV,
3829                                  int max_uV, suspend_state_t state)
3830{
3831        struct ww_acquire_ctx ww_ctx;
3832        int ret;
3833
3834        /* PM_SUSPEND_ON is handled by regulator_set_voltage() */
3835        if (regulator_check_states(state) || state == PM_SUSPEND_ON)
3836                return -EINVAL;
3837
3838        regulator_lock_dependent(regulator->rdev, &ww_ctx);
3839
3840        ret = _regulator_set_suspend_voltage(regulator, min_uV,
3841                                             max_uV, state);
3842
3843        regulator_unlock_dependent(regulator->rdev, &ww_ctx);
3844
3845        return ret;
3846}
3847EXPORT_SYMBOL_GPL(regulator_set_suspend_voltage);
3848
3849/**
3850 * regulator_set_voltage_time - get raise/fall time
3851 * @regulator: regulator source
3852 * @old_uV: starting voltage in microvolts
3853 * @new_uV: target voltage in microvolts
3854 *
3855 * Provided with the starting and ending voltage, this function attempts to
3856 * calculate the time in microseconds required to rise or fall to this new
3857 * voltage.
3858 */
3859int regulator_set_voltage_time(struct regulator *regulator,
3860                               int old_uV, int new_uV)
3861{
3862        struct regulator_dev *rdev = regulator->rdev;
3863        const struct regulator_ops *ops = rdev->desc->ops;
3864        int old_sel = -1;
3865        int new_sel = -1;
3866        int voltage;
3867        int i;
3868
3869        if (ops->set_voltage_time)
3870                return ops->set_voltage_time(rdev, old_uV, new_uV);
3871        else if (!ops->set_voltage_time_sel)
3872                return _regulator_set_voltage_time(rdev, old_uV, new_uV);
3873
3874        /* Currently requires operations to do this */
3875        if (!ops->list_voltage || !rdev->desc->n_voltages)
3876                return -EINVAL;
3877
3878        for (i = 0; i < rdev->desc->n_voltages; i++) {
3879                /* We only look for exact voltage matches here */
3880                voltage = regulator_list_voltage(regulator, i);
3881                if (voltage < 0)
3882                        return -EINVAL;
3883                if (voltage == 0)
3884                        continue;
3885                if (voltage == old_uV)
3886                        old_sel = i;
3887                if (voltage == new_uV)
3888                        new_sel = i;
3889        }
3890
3891        if (old_sel < 0 || new_sel < 0)
3892                return -EINVAL;
3893
3894        return ops->set_voltage_time_sel(rdev, old_sel, new_sel);
3895}
3896EXPORT_SYMBOL_GPL(regulator_set_voltage_time);
3897
3898/**
3899 * regulator_set_voltage_time_sel - get raise/fall time
3900 * @rdev: regulator source device
3901 * @old_selector: selector for starting voltage
3902 * @new_selector: selector for target voltage
3903 *
3904 * Provided with the starting and target voltage selectors, this function
3905 * returns time in microseconds required to rise or fall to this new voltage
3906 *
3907 * Drivers providing ramp_delay in regulation_constraints can use this as their
3908 * set_voltage_time_sel() operation.
3909 */
3910int regulator_set_voltage_time_sel(struct regulator_dev *rdev,
3911                                   unsigned int old_selector,
3912                                   unsigned int new_selector)
3913{
3914        int old_volt, new_volt;
3915
3916        /* sanity check */
3917        if (!rdev->desc->ops->list_voltage)
3918                return -EINVAL;
3919
3920        old_volt = rdev->desc->ops->list_voltage(rdev, old_selector);
3921        new_volt = rdev->desc->ops->list_voltage(rdev, new_selector);
3922
3923        if (rdev->desc->ops->set_voltage_time)
3924                return rdev->desc->ops->set_voltage_time(rdev, old_volt,
3925                                                         new_volt);
3926        else
3927                return _regulator_set_voltage_time(rdev, old_volt, new_volt);
3928}
3929EXPORT_SYMBOL_GPL(regulator_set_voltage_time_sel);
3930
3931/**
3932 * regulator_sync_voltage - re-apply last regulator output voltage
3933 * @regulator: regulator source
3934 *
3935 * Re-apply the last configured voltage.  This is intended to be used
3936 * where some external control source the consumer is cooperating with
3937 * has caused the configured voltage to change.
3938 */
3939int regulator_sync_voltage(struct regulator *regulator)
3940{
3941        struct regulator_dev *rdev = regulator->rdev;
3942        struct regulator_voltage *voltage = &regulator->voltage[PM_SUSPEND_ON];
3943        int ret, min_uV, max_uV;
3944
3945        regulator_lock(rdev);
3946
3947        if (!rdev->desc->ops->set_voltage &&
3948            !rdev->desc->ops->set_voltage_sel) {
3949                ret = -EINVAL;
3950                goto out;
3951        }
3952
3953        /* This is only going to work if we've had a voltage configured. */
3954        if (!voltage->min_uV && !voltage->max_uV) {
3955                ret = -EINVAL;
3956                goto out;
3957        }
3958
3959        min_uV = voltage->min_uV;
3960        max_uV = voltage->max_uV;
3961
3962        /* This should be a paranoia check... */
3963        ret = regulator_check_voltage(rdev, &min_uV, &max_uV);
3964        if (ret < 0)
3965                goto out;
3966
3967        ret = regulator_check_consumers(rdev, &min_uV, &max_uV, 0);
3968        if (ret < 0)
3969                goto out;
3970
3971        ret = _regulator_do_set_voltage(rdev, min_uV, max_uV);
3972
3973out:
3974        regulator_unlock(rdev);
3975        return ret;
3976}
3977EXPORT_SYMBOL_GPL(regulator_sync_voltage);
3978
3979int regulator_get_voltage_rdev(struct regulator_dev *rdev)
3980{
3981        int sel, ret;
3982        bool bypassed;
3983
3984        if (rdev->desc->ops->get_bypass) {
3985                ret = rdev->desc->ops->get_bypass(rdev, &bypassed);
3986                if (ret < 0)
3987                        return ret;
3988                if (bypassed) {
3989                        /* if bypassed the regulator must have a supply */
3990                        if (!rdev->supply) {
3991                                rdev_err(rdev,
3992                                         "bypassed regulator has no supply!\n");
3993                                return -EPROBE_DEFER;
3994                        }
3995
3996                        return regulator_get_voltage_rdev(rdev->supply->rdev);
3997                }
3998        }
3999
4000        if (rdev->desc->ops->get_voltage_sel) {
4001                sel = rdev->desc->ops->get_voltage_sel(rdev);
4002                if (sel < 0)
4003                        return sel;
4004                ret = rdev->desc->ops->list_voltage(rdev, sel);
4005        } else if (rdev->desc->ops->get_voltage) {
4006                ret = rdev->desc->ops->get_voltage(rdev);
4007        } else if (rdev->desc->ops->list_voltage) {
4008                ret = rdev->desc->ops->list_voltage(rdev, 0);
4009        } else if (rdev->desc->fixed_uV && (rdev->desc->n_voltages == 1)) {
4010                ret = rdev->desc->fixed_uV;
4011        } else if (rdev->supply) {
4012                ret = regulator_get_voltage_rdev(rdev->supply->rdev);
4013        } else {
4014                return -EINVAL;
4015        }
4016
4017        if (ret < 0)
4018                return ret;
4019        return ret - rdev->constraints->uV_offset;
4020}
4021
4022/**
4023 * regulator_get_voltage - get regulator output voltage
4024 * @regulator: regulator source
4025 *
4026 * This returns the current regulator voltage in uV.
4027 *
4028 * NOTE: If the regulator is disabled it will return the voltage value. This
4029 * function should not be used to determine regulator state.
4030 */
4031int regulator_get_voltage(struct regulator *regulator)
4032{
4033        struct ww_acquire_ctx ww_ctx;
4034        int ret;
4035
4036        regulator_lock_dependent(regulator->rdev, &ww_ctx);
4037        ret = regulator_get_voltage_rdev(regulator->rdev);
4038        regulator_unlock_dependent(regulator->rdev, &ww_ctx);
4039
4040        return ret;
4041}
4042EXPORT_SYMBOL_GPL(regulator_get_voltage);
4043
4044/**
4045 * regulator_set_current_limit - set regulator output current limit
4046 * @regulator: regulator source
4047 * @min_uA: Minimum supported current in uA
4048 * @max_uA: Maximum supported current in uA
4049 *
4050 * Sets current sink to the desired output current. This can be set during
4051 * any regulator state. IOW, regulator can be disabled or enabled.
4052 *
4053 * If the regulator is enabled then the current will change to the new value
4054 * immediately otherwise if the regulator is disabled the regulator will
4055 * output at the new current when enabled.
4056 *
4057 * NOTE: Regulator system constraints must be set for this regulator before
4058 * calling this function otherwise this call will fail.
4059 */
4060int regulator_set_current_limit(struct regulator *regulator,
4061                               int min_uA, int max_uA)
4062{
4063        struct regulator_dev *rdev = regulator->rdev;
4064        int ret;
4065
4066        regulator_lock(rdev);
4067
4068        /* sanity check */
4069        if (!rdev->desc->ops->set_current_limit) {
4070                ret = -EINVAL;
4071                goto out;
4072        }
4073
4074        /* constraints check */
4075        ret = regulator_check_current_limit(rdev, &min_uA, &max_uA);
4076        if (ret < 0)
4077                goto out;
4078
4079        ret = rdev->desc->ops->set_current_limit(rdev, min_uA, max_uA);
4080out:
4081        regulator_unlock(rdev);
4082        return ret;
4083}
4084EXPORT_SYMBOL_GPL(regulator_set_current_limit);
4085
4086static int _regulator_get_current_limit_unlocked(struct regulator_dev *rdev)
4087{
4088        /* sanity check */
4089        if (!rdev->desc->ops->get_current_limit)
4090                return -EINVAL;
4091
4092        return rdev->desc->ops->get_current_limit(rdev);
4093}
4094
4095static int _regulator_get_current_limit(struct regulator_dev *rdev)
4096{
4097        int ret;
4098
4099        regulator_lock(rdev);
4100        ret = _regulator_get_current_limit_unlocked(rdev);
4101        regulator_unlock(rdev);
4102
4103        return ret;
4104}
4105
4106/**
4107 * regulator_get_current_limit - get regulator output current
4108 * @regulator: regulator source
4109 *
4110 * This returns the current supplied by the specified current sink in uA.
4111 *
4112 * NOTE: If the regulator is disabled it will return the current value. This
4113 * function should not be used to determine regulator state.
4114 */
4115int regulator_get_current_limit(struct regulator *regulator)
4116{
4117        return _regulator_get_current_limit(regulator->rdev);
4118}
4119EXPORT_SYMBOL_GPL(regulator_get_current_limit);
4120
4121/**
4122 * regulator_set_mode - set regulator operating mode
4123 * @regulator: regulator source
4124 * @mode: operating mode - one of the REGULATOR_MODE constants
4125 *
4126 * Set regulator operating mode to increase regulator efficiency or improve
4127 * regulation performance.
4128 *
4129 * NOTE: Regulator system constraints must be set for this regulator before
4130 * calling this function otherwise this call will fail.
4131 */
4132int regulator_set_mode(struct regulator *regulator, unsigned int mode)
4133{
4134        struct regulator_dev *rdev = regulator->rdev;
4135        int ret;
4136        int regulator_curr_mode;
4137
4138        regulator_lock(rdev);
4139
4140        /* sanity check */
4141        if (!rdev->desc->ops->set_mode) {
4142                ret = -EINVAL;
4143                goto out;
4144        }
4145
4146        /* return if the same mode is requested */
4147        if (rdev->desc->ops->get_mode) {
4148                regulator_curr_mode = rdev->desc->ops->get_mode(rdev);
4149                if (regulator_curr_mode == mode) {
4150                        ret = 0;
4151                        goto out;
4152                }
4153        }
4154
4155        /* constraints check */
4156        ret = regulator_mode_constrain(rdev, &mode);
4157        if (ret < 0)
4158                goto out;
4159
4160        ret = rdev->desc->ops->set_mode(rdev, mode);
4161out:
4162        regulator_unlock(rdev);
4163        return ret;
4164}
4165EXPORT_SYMBOL_GPL(regulator_set_mode);
4166
4167static unsigned int _regulator_get_mode_unlocked(struct regulator_dev *rdev)
4168{
4169        /* sanity check */
4170        if (!rdev->desc->ops->get_mode)
4171                return -EINVAL;
4172
4173        return rdev->desc->ops->get_mode(rdev);
4174}
4175
4176static unsigned int _regulator_get_mode(struct regulator_dev *rdev)
4177{
4178        int ret;
4179
4180        regulator_lock(rdev);
4181        ret = _regulator_get_mode_unlocked(rdev);
4182        regulator_unlock(rdev);
4183
4184        return ret;
4185}
4186
4187/**
4188 * regulator_get_mode - get regulator operating mode
4189 * @regulator: regulator source
4190 *
4191 * Get the current regulator operating mode.
4192 */
4193unsigned int regulator_get_mode(struct regulator *regulator)
4194{
4195        return _regulator_get_mode(regulator->rdev);
4196}
4197EXPORT_SYMBOL_GPL(regulator_get_mode);
4198
4199static int _regulator_get_error_flags(struct regulator_dev *rdev,
4200                                        unsigned int *flags)
4201{
4202        int ret;
4203
4204        regulator_lock(rdev);
4205
4206        /* sanity check */
4207        if (!rdev->desc->ops->get_error_flags) {
4208                ret = -EINVAL;
4209                goto out;
4210        }
4211
4212        ret = rdev->desc->ops->get_error_flags(rdev, flags);
4213out:
4214        regulator_unlock(rdev);
4215        return ret;
4216}
4217
4218/**
4219 * regulator_get_error_flags - get regulator error information
4220 * @regulator: regulator source
4221 * @flags: pointer to store error flags
4222 *
4223 * Get the current regulator error information.
4224 */
4225int regulator_get_error_flags(struct regulator *regulator,
4226                                unsigned int *flags)
4227{
4228        return _regulator_get_error_flags(regulator->rdev, flags);
4229}
4230EXPORT_SYMBOL_GPL(regulator_get_error_flags);
4231
4232/**
4233 * regulator_set_load - set regulator load
4234 * @regulator: regulator source
4235 * @uA_load: load current
4236 *
4237 * Notifies the regulator core of a new device load. This is then used by
4238 * DRMS (if enabled by constraints) to set the most efficient regulator
4239 * operating mode for the new regulator loading.
4240 *
4241 * Consumer devices notify their supply regulator of the maximum power
4242 * they will require (can be taken from device datasheet in the power
4243 * consumption tables) when they change operational status and hence power
4244 * state. Examples of operational state changes that can affect power
4245 * consumption are :-
4246 *
4247 *    o Device is opened / closed.
4248 *    o Device I/O is about to begin or has just finished.
4249 *    o Device is idling in between work.
4250 *
4251 * This information is also exported via sysfs to userspace.
4252 *
4253 * DRMS will sum the total requested load on the regulator and change
4254 * to the most efficient operating mode if platform constraints allow.
4255 *
4256 * NOTE: when a regulator consumer requests to have a regulator
4257 * disabled then any load that consumer requested no longer counts
4258 * toward the total requested load.  If the regulator is re-enabled
4259 * then the previously requested load will start counting again.
4260 *
4261 * If a regulator is an always-on regulator then an individual consumer's
4262 * load will still be removed if that consumer is fully disabled.
4263 *
4264 * On error a negative errno is returned.
4265 */
4266int regulator_set_load(struct regulator *regulator, int uA_load)
4267{
4268        struct regulator_dev *rdev = regulator->rdev;
4269        int old_uA_load;
4270        int ret = 0;
4271
4272        regulator_lock(rdev);
4273        old_uA_load = regulator->uA_load;
4274        regulator->uA_load = uA_load;
4275        if (regulator->enable_count && old_uA_load != uA_load) {
4276                ret = drms_uA_update(rdev);
4277                if (ret < 0)
4278                        regulator->uA_load = old_uA_load;
4279        }
4280        regulator_unlock(rdev);
4281
4282        return ret;
4283}
4284EXPORT_SYMBOL_GPL(regulator_set_load);
4285
4286/**
4287 * regulator_allow_bypass - allow the regulator to go into bypass mode
4288 *
4289 * @regulator: Regulator to configure
4290 * @enable: enable or disable bypass mode
4291 *
4292 * Allow the regulator to go into bypass mode if all other consumers
4293 * for the regulator also enable bypass mode and the machine
4294 * constraints allow this.  Bypass mode means that the regulator is
4295 * simply passing the input directly to the output with no regulation.
4296 */
4297int regulator_allow_bypass(struct regulator *regulator, bool enable)
4298{
4299        struct regulator_dev *rdev = regulator->rdev;
4300        int ret = 0;
4301
4302        if (!rdev->desc->ops->set_bypass)
4303                return 0;
4304
4305        if (!regulator_ops_is_valid(rdev, REGULATOR_CHANGE_BYPASS))
4306                return 0;
4307
4308        regulator_lock(rdev);
4309
4310        if (enable && !regulator->bypass) {
4311                rdev->bypass_count++;
4312
4313                if (rdev->bypass_count == rdev->open_count) {
4314                        ret = rdev->desc->ops->set_bypass(rdev, enable);
4315                        if (ret != 0)
4316                                rdev->bypass_count--;
4317                }
4318
4319        } else if (!enable && regulator->bypass) {
4320                rdev->bypass_count--;
4321
4322                if (rdev->bypass_count != rdev->open_count) {
4323                        ret = rdev->desc->ops->set_bypass(rdev, enable);
4324                        if (ret != 0)
4325                                rdev->bypass_count++;
4326                }
4327        }
4328
4329        if (ret == 0)
4330                regulator->bypass = enable;
4331
4332        regulator_unlock(rdev);
4333
4334        return ret;
4335}
4336EXPORT_SYMBOL_GPL(regulator_allow_bypass);
4337
4338/**
4339 * regulator_register_notifier - register regulator event notifier
4340 * @regulator: regulator source
4341 * @nb: notifier block
4342 *
4343 * Register notifier block to receive regulator events.
4344 */
4345int regulator_register_notifier(struct regulator *regulator,
4346                              struct notifier_block *nb)
4347{
4348        return blocking_notifier_chain_register(&regulator->rdev->notifier,
4349                                                nb);
4350}
4351EXPORT_SYMBOL_GPL(regulator_register_notifier);
4352
4353/**
4354 * regulator_unregister_notifier - unregister regulator event notifier
4355 * @regulator: regulator source
4356 * @nb: notifier block
4357 *
4358 * Unregister regulator event notifier block.
4359 */
4360int regulator_unregister_notifier(struct regulator *regulator,
4361                                struct notifier_block *nb)
4362{
4363        return blocking_notifier_chain_unregister(&regulator->rdev->notifier,
4364                                                  nb);
4365}
4366EXPORT_SYMBOL_GPL(regulator_unregister_notifier);
4367
4368/* notify regulator consumers and downstream regulator consumers.
4369 * Note mutex must be held by caller.
4370 */
4371static int _notifier_call_chain(struct regulator_dev *rdev,
4372                                  unsigned long event, void *data)
4373{
4374        /* call rdev chain first */
4375        return blocking_notifier_call_chain(&rdev->notifier, event, data);
4376}
4377
4378/**
4379 * regulator_bulk_get - get multiple regulator consumers
4380 *
4381 * @dev:           Device to supply
4382 * @num_consumers: Number of consumers to register
4383 * @consumers:     Configuration of consumers; clients are stored here.
4384 *
4385 * @return 0 on success, an errno on failure.
4386 *
4387 * This helper function allows drivers to get several regulator
4388 * consumers in one operation.  If any of the regulators cannot be
4389 * acquired then any regulators that were allocated will be freed
4390 * before returning to the caller.
4391 */
4392int regulator_bulk_get(struct device *dev, int num_consumers,
4393                       struct regulator_bulk_data *consumers)
4394{
4395        int i;
4396        int ret;
4397
4398        for (i = 0; i < num_consumers; i++)
4399                consumers[i].consumer = NULL;
4400
4401        for (i = 0; i < num_consumers; i++) {
4402                consumers[i].consumer = regulator_get(dev,
4403                                                      consumers[i].supply);
4404                if (IS_ERR(consumers[i].consumer)) {
4405                        ret = PTR_ERR(consumers[i].consumer);
4406                        consumers[i].consumer = NULL;
4407                        goto err;
4408                }
4409        }
4410
4411        return 0;
4412
4413err:
4414        if (ret != -EPROBE_DEFER)
4415                dev_err(dev, "Failed to get supply '%s': %d\n",
4416                        consumers[i].supply, ret);
4417        else
4418                dev_dbg(dev, "Failed to get supply '%s', deferring\n",
4419                        consumers[i].supply);
4420
4421        while (--i >= 0)
4422                regulator_put(consumers[i].consumer);
4423
4424        return ret;
4425}
4426EXPORT_SYMBOL_GPL(regulator_bulk_get);
4427
4428static void regulator_bulk_enable_async(void *data, async_cookie_t cookie)
4429{
4430        struct regulator_bulk_data *bulk = data;
4431
4432        bulk->ret = regulator_enable(bulk->consumer);
4433}
4434
4435/**
4436 * regulator_bulk_enable - enable multiple regulator consumers
4437 *
4438 * @num_consumers: Number of consumers
4439 * @consumers:     Consumer data; clients are stored here.
4440 * @return         0 on success, an errno on failure
4441 *
4442 * This convenience API allows consumers to enable multiple regulator
4443 * clients in a single API call.  If any consumers cannot be enabled
4444 * then any others that were enabled will be disabled again prior to
4445 * return.
4446 */
4447int regulator_bulk_enable(int num_consumers,
4448                          struct regulator_bulk_data *consumers)
4449{
4450        ASYNC_DOMAIN_EXCLUSIVE(async_domain);
4451        int i;
4452        int ret = 0;
4453
4454        for (i = 0; i < num_consumers; i++) {
4455                async_schedule_domain(regulator_bulk_enable_async,
4456                                      &consumers[i], &async_domain);
4457        }
4458
4459        async_synchronize_full_domain(&async_domain);
4460
4461        /* If any consumer failed we need to unwind any that succeeded */
4462        for (i = 0; i < num_consumers; i++) {
4463                if (consumers[i].ret != 0) {
4464                        ret = consumers[i].ret;
4465                        goto err;
4466                }
4467        }
4468
4469        return 0;
4470
4471err:
4472        for (i = 0; i < num_consumers; i++) {
4473                if (consumers[i].ret < 0)
4474                        pr_err("Failed to enable %s: %d\n", consumers[i].supply,
4475                               consumers[i].ret);
4476                else
4477                        regulator_disable(consumers[i].consumer);
4478        }
4479
4480        return ret;
4481}
4482EXPORT_SYMBOL_GPL(regulator_bulk_enable);
4483
4484/**
4485 * regulator_bulk_disable - disable multiple regulator consumers
4486 *
4487 * @num_consumers: Number of consumers
4488 * @consumers:     Consumer data; clients are stored here.
4489 * @return         0 on success, an errno on failure
4490 *
4491 * This convenience API allows consumers to disable multiple regulator
4492 * clients in a single API call.  If any consumers cannot be disabled
4493 * then any others that were disabled will be enabled again prior to
4494 * return.
4495 */
4496int regulator_bulk_disable(int num_consumers,
4497                           struct regulator_bulk_data *consumers)
4498{
4499        int i;
4500        int ret, r;
4501
4502        for (i = num_consumers - 1; i >= 0; --i) {
4503                ret = regulator_disable(consumers[i].consumer);
4504                if (ret != 0)
4505                        goto err;
4506        }
4507
4508        return 0;
4509
4510err:
4511        pr_err("Failed to disable %s: %d\n", consumers[i].supply, ret);
4512        for (++i; i < num_consumers; ++i) {
4513                r = regulator_enable(consumers[i].consumer);
4514                if (r != 0)
4515                        pr_err("Failed to re-enable %s: %d\n",
4516                               consumers[i].supply, r);
4517        }
4518
4519        return ret;
4520}
4521EXPORT_SYMBOL_GPL(regulator_bulk_disable);
4522
4523/**
4524 * regulator_bulk_force_disable - force disable multiple regulator consumers
4525 *
4526 * @num_consumers: Number of consumers
4527 * @consumers:     Consumer data; clients are stored here.
4528 * @return         0 on success, an errno on failure
4529 *
4530 * This convenience API allows consumers to forcibly disable multiple regulator
4531 * clients in a single API call.
4532 * NOTE: This should be used for situations when device damage will
4533 * likely occur if the regulators are not disabled (e.g. over temp).
4534 * Although regulator_force_disable function call for some consumers can
4535 * return error numbers, the function is called for all consumers.
4536 */
4537int regulator_bulk_force_disable(int num_consumers,
4538                           struct regulator_bulk_data *consumers)
4539{
4540        int i;
4541        int ret = 0;
4542
4543        for (i = 0; i < num_consumers; i++) {
4544                consumers[i].ret =
4545                            regulator_force_disable(consumers[i].consumer);
4546
4547                /* Store first error for reporting */
4548                if (consumers[i].ret && !ret)
4549                        ret = consumers[i].ret;
4550        }
4551
4552        return ret;
4553}
4554EXPORT_SYMBOL_GPL(regulator_bulk_force_disable);
4555
4556/**
4557 * regulator_bulk_free - free multiple regulator consumers
4558 *
4559 * @num_consumers: Number of consumers
4560 * @consumers:     Consumer data; clients are stored here.
4561 *
4562 * This convenience API allows consumers to free multiple regulator
4563 * clients in a single API call.
4564 */
4565void regulator_bulk_free(int num_consumers,
4566                         struct regulator_bulk_data *consumers)
4567{
4568        int i;
4569
4570        for (i = 0; i < num_consumers; i++) {
4571                regulator_put(consumers[i].consumer);
4572                consumers[i].consumer = NULL;
4573        }
4574}
4575EXPORT_SYMBOL_GPL(regulator_bulk_free);
4576
4577/**
4578 * regulator_notifier_call_chain - call regulator event notifier
4579 * @rdev: regulator source
4580 * @event: notifier block
4581 * @data: callback-specific data.
4582 *
4583 * Called by regulator drivers to notify clients a regulator event has
4584 * occurred. We also notify regulator clients downstream.
4585 * Note lock must be held by caller.
4586 */
4587int regulator_notifier_call_chain(struct regulator_dev *rdev,
4588                                  unsigned long event, void *data)
4589{
4590        lockdep_assert_held_once(&rdev->mutex.base);
4591
4592        _notifier_call_chain(rdev, event, data);
4593        return NOTIFY_DONE;
4594
4595}
4596EXPORT_SYMBOL_GPL(regulator_notifier_call_chain);
4597
4598/**
4599 * regulator_mode_to_status - convert a regulator mode into a status
4600 *
4601 * @mode: Mode to convert
4602 *
4603 * Convert a regulator mode into a status.
4604 */
4605int regulator_mode_to_status(unsigned int mode)
4606{
4607        switch (mode) {
4608        case REGULATOR_MODE_FAST:
4609                return REGULATOR_STATUS_FAST;
4610        case REGULATOR_MODE_NORMAL:
4611                return REGULATOR_STATUS_NORMAL;
4612        case REGULATOR_MODE_IDLE:
4613                return REGULATOR_STATUS_IDLE;
4614        case REGULATOR_MODE_STANDBY:
4615                return REGULATOR_STATUS_STANDBY;
4616        default:
4617                return REGULATOR_STATUS_UNDEFINED;
4618        }
4619}
4620EXPORT_SYMBOL_GPL(regulator_mode_to_status);
4621
4622static struct attribute *regulator_dev_attrs[] = {
4623        &dev_attr_name.attr,
4624        &dev_attr_num_users.attr,
4625        &dev_attr_type.attr,
4626        &dev_attr_microvolts.attr,
4627        &dev_attr_microamps.attr,
4628        &dev_attr_opmode.attr,
4629        &dev_attr_state.attr,
4630        &dev_attr_status.attr,
4631        &dev_attr_bypass.attr,
4632        &dev_attr_requested_microamps.attr,
4633        &dev_attr_min_microvolts.attr,
4634        &dev_attr_max_microvolts.attr,
4635        &dev_attr_min_microamps.attr,
4636        &dev_attr_max_microamps.attr,
4637        &dev_attr_suspend_standby_state.attr,
4638        &dev_attr_suspend_mem_state.attr,
4639        &dev_attr_suspend_disk_state.attr,
4640        &dev_attr_suspend_standby_microvolts.attr,
4641        &dev_attr_suspend_mem_microvolts.attr,
4642        &dev_attr_suspend_disk_microvolts.attr,
4643        &dev_attr_suspend_standby_mode.attr,
4644        &dev_attr_suspend_mem_mode.attr,
4645        &dev_attr_suspend_disk_mode.attr,
4646        NULL
4647};
4648
4649/*
4650 * To avoid cluttering sysfs (and memory) with useless state, only
4651 * create attributes that can be meaningfully displayed.
4652 */
4653static umode_t regulator_attr_is_visible(struct kobject *kobj,
4654                                         struct attribute *attr, int idx)
4655{
4656        struct device *dev = kobj_to_dev(kobj);
4657        struct regulator_dev *rdev = dev_to_rdev(dev);
4658        const struct regulator_ops *ops = rdev->desc->ops;
4659        umode_t mode = attr->mode;
4660
4661        /* these three are always present */
4662        if (attr == &dev_attr_name.attr ||
4663            attr == &dev_attr_num_users.attr ||
4664            attr == &dev_attr_type.attr)
4665                return mode;
4666
4667        /* some attributes need specific methods to be displayed */
4668        if (attr == &dev_attr_microvolts.attr) {
4669                if ((ops->get_voltage && ops->get_voltage(rdev) >= 0) ||
4670                    (ops->get_voltage_sel && ops->get_voltage_sel(rdev) >= 0) ||
4671                    (ops->list_voltage && ops->list_voltage(rdev, 0) >= 0) ||
4672                    (rdev->desc->fixed_uV && rdev->desc->n_voltages == 1))
4673                        return mode;
4674                return 0;
4675        }
4676
4677        if (attr == &dev_attr_microamps.attr)
4678                return ops->get_current_limit ? mode : 0;
4679
4680        if (attr == &dev_attr_opmode.attr)
4681                return ops->get_mode ? mode : 0;
4682
4683        if (attr == &dev_attr_state.attr)
4684                return (rdev->ena_pin || ops->is_enabled) ? mode : 0;
4685
4686        if (attr == &dev_attr_status.attr)
4687                return ops->get_status ? mode : 0;
4688
4689        if (attr == &dev_attr_bypass.attr)
4690                return ops->get_bypass ? mode : 0;
4691
4692        /* constraints need specific supporting methods */
4693        if (attr == &dev_attr_min_microvolts.attr ||
4694            attr == &dev_attr_max_microvolts.attr)
4695                return (ops->set_voltage || ops->set_voltage_sel) ? mode : 0;
4696
4697        if (attr == &dev_attr_min_microamps.attr ||
4698            attr == &dev_attr_max_microamps.attr)
4699                return ops->set_current_limit ? mode : 0;
4700
4701        if (attr == &dev_attr_suspend_standby_state.attr ||
4702            attr == &dev_attr_suspend_mem_state.attr ||
4703            attr == &dev_attr_suspend_disk_state.attr)
4704                return mode;
4705
4706        if (attr == &dev_attr_suspend_standby_microvolts.attr ||
4707            attr == &dev_attr_suspend_mem_microvolts.attr ||
4708            attr == &dev_attr_suspend_disk_microvolts.attr)
4709                return ops->set_suspend_voltage ? mode : 0;
4710
4711        if (attr == &dev_attr_suspend_standby_mode.attr ||
4712            attr == &dev_attr_suspend_mem_mode.attr ||
4713            attr == &dev_attr_suspend_disk_mode.attr)
4714                return ops->set_suspend_mode ? mode : 0;
4715
4716        return mode;
4717}
4718
4719static const struct attribute_group regulator_dev_group = {
4720        .attrs = regulator_dev_attrs,
4721        .is_visible = regulator_attr_is_visible,
4722};
4723
4724static const struct attribute_group *regulator_dev_groups[] = {
4725        &regulator_dev_group,
4726        NULL
4727};
4728
4729static void regulator_dev_release(struct device *dev)
4730{
4731        struct regulator_dev *rdev = dev_get_drvdata(dev);
4732
4733        kfree(rdev->constraints);
4734        of_node_put(rdev->dev.of_node);
4735        kfree(rdev);
4736}
4737
4738static void rdev_init_debugfs(struct regulator_dev *rdev)
4739{
4740        struct device *parent = rdev->dev.parent;
4741        const char *rname = rdev_get_name(rdev);
4742        char name[NAME_MAX];
4743
4744        /* Avoid duplicate debugfs directory names */
4745        if (parent && rname == rdev->desc->name) {
4746                snprintf(name, sizeof(name), "%s-%s", dev_name(parent),
4747                         rname);
4748                rname = name;
4749        }
4750
4751        rdev->debugfs = debugfs_create_dir(rname, debugfs_root);
4752        if (!rdev->debugfs) {
4753                rdev_warn(rdev, "Failed to create debugfs directory\n");
4754                return;
4755        }
4756
4757        debugfs_create_u32("use_count", 0444, rdev->debugfs,
4758                           &rdev->use_count);
4759        debugfs_create_u32("open_count", 0444, rdev->debugfs,
4760                           &rdev->open_count);
4761        debugfs_create_u32("bypass_count", 0444, rdev->debugfs,
4762                           &rdev->bypass_count);
4763}
4764
4765static int regulator_register_resolve_supply(struct device *dev, void *data)
4766{
4767        struct regulator_dev *rdev = dev_to_rdev(dev);
4768
4769        if (regulator_resolve_supply(rdev))
4770                rdev_dbg(rdev, "unable to resolve supply\n");
4771
4772        return 0;
4773}
4774
4775int regulator_coupler_register(struct regulator_coupler *coupler)
4776{
4777        mutex_lock(&regulator_list_mutex);
4778        list_add_tail(&coupler->list, &regulator_coupler_list);
4779        mutex_unlock(&regulator_list_mutex);
4780
4781        return 0;
4782}
4783
4784static struct regulator_coupler *
4785regulator_find_coupler(struct regulator_dev *rdev)
4786{
4787        struct regulator_coupler *coupler;
4788        int err;
4789
4790        /*
4791         * Note that regulators are appended to the list and the generic
4792         * coupler is registered first, hence it will be attached at last
4793         * if nobody cared.
4794         */
4795        list_for_each_entry_reverse(coupler, &regulator_coupler_list, list) {
4796                err = coupler->attach_regulator(coupler, rdev);
4797                if (!err) {
4798                        if (!coupler->balance_voltage &&
4799                            rdev->coupling_desc.n_coupled > 2)
4800                                goto err_unsupported;
4801
4802                        return coupler;
4803                }
4804
4805                if (err < 0)
4806                        return ERR_PTR(err);
4807
4808                if (err == 1)
4809                        continue;
4810
4811                break;
4812        }
4813
4814        return ERR_PTR(-EINVAL);
4815
4816err_unsupported:
4817        if (coupler->detach_regulator)
4818                coupler->detach_regulator(coupler, rdev);
4819
4820        rdev_err(rdev,
4821                "Voltage balancing for multiple regulator couples is unimplemented\n");
4822
4823        return ERR_PTR(-EPERM);
4824}
4825
4826static void regulator_resolve_coupling(struct regulator_dev *rdev)
4827{
4828        struct regulator_coupler *coupler = rdev->coupling_desc.coupler;
4829        struct coupling_desc *c_desc = &rdev->coupling_desc;
4830        int n_coupled = c_desc->n_coupled;
4831        struct regulator_dev *c_rdev;
4832        int i;
4833
4834        for (i = 1; i < n_coupled; i++) {
4835                /* already resolved */
4836                if (c_desc->coupled_rdevs[i])
4837                        continue;
4838
4839                c_rdev = of_parse_coupled_regulator(rdev, i - 1);
4840
4841                if (!c_rdev)
4842                        continue;
4843
4844                if (c_rdev->coupling_desc.coupler != coupler) {
4845                        rdev_err(rdev, "coupler mismatch with %s\n",
4846                                 rdev_get_name(c_rdev));
4847                        return;
4848                }
4849
4850                regulator_lock(c_rdev);
4851
4852                c_desc->coupled_rdevs[i] = c_rdev;
4853                c_desc->n_resolved++;
4854
4855                regulator_unlock(c_rdev);
4856
4857                regulator_resolve_coupling(c_rdev);
4858        }
4859}
4860
4861static void regulator_remove_coupling(struct regulator_dev *rdev)
4862{
4863        struct regulator_coupler *coupler = rdev->coupling_desc.coupler;
4864        struct coupling_desc *__c_desc, *c_desc = &rdev->coupling_desc;
4865        struct regulator_dev *__c_rdev, *c_rdev;
4866        unsigned int __n_coupled, n_coupled;
4867        int i, k;
4868        int err;
4869
4870        n_coupled = c_desc->n_coupled;
4871
4872        for (i = 1; i < n_coupled; i++) {
4873                c_rdev = c_desc->coupled_rdevs[i];
4874
4875                if (!c_rdev)
4876                        continue;
4877
4878                regulator_lock(c_rdev);
4879
4880                __c_desc = &c_rdev->coupling_desc;
4881                __n_coupled = __c_desc->n_coupled;
4882
4883                for (k = 1; k < __n_coupled; k++) {
4884                        __c_rdev = __c_desc->coupled_rdevs[k];
4885
4886                        if (__c_rdev == rdev) {
4887                                __c_desc->coupled_rdevs[k] = NULL;
4888                                __c_desc->n_resolved--;
4889                                break;
4890                        }
4891                }
4892
4893                regulator_unlock(c_rdev);
4894
4895                c_desc->coupled_rdevs[i] = NULL;
4896                c_desc->n_resolved--;
4897        }
4898
4899        if (coupler && coupler->detach_regulator) {
4900                err = coupler->detach_regulator(coupler, rdev);
4901                if (err)
4902                        rdev_err(rdev, "failed to detach from coupler: %d\n",
4903                                 err);
4904        }
4905
4906        kfree(rdev->coupling_desc.coupled_rdevs);
4907        rdev->coupling_desc.coupled_rdevs = NULL;
4908}
4909
4910static int regulator_init_coupling(struct regulator_dev *rdev)
4911{
4912        int err, n_phandles;
4913        size_t alloc_size;
4914
4915        if (!IS_ENABLED(CONFIG_OF))
4916                n_phandles = 0;
4917        else
4918                n_phandles = of_get_n_coupled(rdev);
4919
4920        alloc_size = sizeof(*rdev) * (n_phandles + 1);
4921
4922        rdev->coupling_desc.coupled_rdevs = kzalloc(alloc_size, GFP_KERNEL);
4923        if (!rdev->coupling_desc.coupled_rdevs)
4924                return -ENOMEM;
4925
4926        /*
4927         * Every regulator should always have coupling descriptor filled with
4928         * at least pointer to itself.
4929         */
4930        rdev->coupling_desc.coupled_rdevs[0] = rdev;
4931        rdev->coupling_desc.n_coupled = n_phandles + 1;
4932        rdev->coupling_desc.n_resolved++;
4933
4934        /* regulator isn't coupled */
4935        if (n_phandles == 0)
4936                return 0;
4937
4938        if (!of_check_coupling_data(rdev))
4939                return -EPERM;
4940
4941        rdev->coupling_desc.coupler = regulator_find_coupler(rdev);
4942        if (IS_ERR(rdev->coupling_desc.coupler)) {
4943                err = PTR_ERR(rdev->coupling_desc.coupler);
4944                rdev_err(rdev, "failed to get coupler: %d\n", err);
4945                return err;
4946        }
4947
4948        return 0;
4949}
4950
4951static int generic_coupler_attach(struct regulator_coupler *coupler,
4952                                  struct regulator_dev *rdev)
4953{
4954        if (rdev->coupling_desc.n_coupled > 2) {
4955                rdev_err(rdev,
4956                         "Voltage balancing for multiple regulator couples is unimplemented\n");
4957                return -EPERM;
4958        }
4959
4960        return 0;
4961}
4962
4963static struct regulator_coupler generic_regulator_coupler = {
4964        .attach_regulator = generic_coupler_attach,
4965};
4966
4967/**
4968 * regulator_register - register regulator
4969 * @regulator_desc: regulator to register
4970 * @cfg: runtime configuration for regulator
4971 *
4972 * Called by regulator drivers to register a regulator.
4973 * Returns a valid pointer to struct regulator_dev on success
4974 * or an ERR_PTR() on error.
4975 */
4976struct regulator_dev *
4977regulator_register(const struct regulator_desc *regulator_desc,
4978                   const struct regulator_config *cfg)
4979{
4980        const struct regulation_constraints *constraints = NULL;
4981        const struct regulator_init_data *init_data;
4982        struct regulator_config *config = NULL;
4983        static atomic_t regulator_no = ATOMIC_INIT(-1);
4984        struct regulator_dev *rdev;
4985        bool dangling_cfg_gpiod = false;
4986        bool dangling_of_gpiod = false;
4987        struct device *dev;
4988        int ret, i;
4989
4990        if (cfg == NULL)
4991                return ERR_PTR(-EINVAL);
4992        if (cfg->ena_gpiod)
4993                dangling_cfg_gpiod = true;
4994        if (regulator_desc == NULL) {
4995                ret = -EINVAL;
4996                goto rinse;
4997        }
4998
4999        dev = cfg->dev;
5000        WARN_ON(!dev);
5001
5002        if (regulator_desc->name == NULL || regulator_desc->ops == NULL) {
5003                ret = -EINVAL;
5004                goto rinse;
5005        }
5006
5007        if (regulator_desc->type != REGULATOR_VOLTAGE &&
5008            regulator_desc->type != REGULATOR_CURRENT) {
5009                ret = -EINVAL;
5010                goto rinse;
5011        }
5012
5013        /* Only one of each should be implemented */
5014        WARN_ON(regulator_desc->ops->get_voltage &&
5015                regulator_desc->ops->get_voltage_sel);
5016        WARN_ON(regulator_desc->ops->set_voltage &&
5017                regulator_desc->ops->set_voltage_sel);
5018
5019        /* If we're using selectors we must implement list_voltage. */
5020        if (regulator_desc->ops->get_voltage_sel &&
5021            !regulator_desc->ops->list_voltage) {
5022                ret = -EINVAL;
5023                goto rinse;
5024        }
5025        if (regulator_desc->ops->set_voltage_sel &&
5026            !regulator_desc->ops->list_voltage) {
5027                ret = -EINVAL;
5028                goto rinse;
5029        }
5030
5031        rdev = kzalloc(sizeof(struct regulator_dev), GFP_KERNEL);
5032        if (rdev == NULL) {
5033                ret = -ENOMEM;
5034                goto rinse;
5035        }
5036
5037        /*
5038         * Duplicate the config so the driver could override it after
5039         * parsing init data.
5040         */
5041        config = kmemdup(cfg, sizeof(*cfg), GFP_KERNEL);
5042        if (config == NULL) {
5043                kfree(rdev);
5044                ret = -ENOMEM;
5045                goto rinse;
5046        }
5047
5048        init_data = regulator_of_get_init_data(dev, regulator_desc, config,
5049                                               &rdev->dev.of_node);
5050        /*
5051         * We need to keep track of any GPIO descriptor coming from the
5052         * device tree until we have handled it over to the core. If the
5053         * config that was passed in to this function DOES NOT contain
5054         * a descriptor, and the config after this call DOES contain
5055         * a descriptor, we definitely got one from parsing the device
5056         * tree.
5057         */
5058        if (!cfg->ena_gpiod && config->ena_gpiod)
5059                dangling_of_gpiod = true;
5060        if (!init_data) {
5061                init_data = config->init_data;
5062                rdev->dev.of_node = of_node_get(config->of_node);
5063        }
5064
5065        ww_mutex_init(&rdev->mutex, &regulator_ww_class);
5066        rdev->reg_data = config->driver_data;
5067        rdev->owner = regulator_desc->owner;
5068        rdev->desc = regulator_desc;
5069        if (config->regmap)
5070                rdev->regmap = config->regmap;
5071        else if (dev_get_regmap(dev, NULL))
5072                rdev->regmap = dev_get_regmap(dev, NULL);
5073        else if (dev->parent)
5074                rdev->regmap = dev_get_regmap(dev->parent, NULL);
5075        INIT_LIST_HEAD(&rdev->consumer_list);
5076        INIT_LIST_HEAD(&rdev->list);
5077        BLOCKING_INIT_NOTIFIER_HEAD(&rdev->notifier);
5078        INIT_DELAYED_WORK(&rdev->disable_work, regulator_disable_work);
5079
5080        /* preform any regulator specific init */
5081        if (init_data && init_data->regulator_init) {
5082                ret = init_data->regulator_init(rdev->reg_data);
5083                if (ret < 0)
5084                        goto clean;
5085        }
5086
5087        if (config->ena_gpiod) {
5088                mutex_lock(&regulator_list_mutex);
5089                ret = regulator_ena_gpio_request(rdev, config);
5090                mutex_unlock(&regulator_list_mutex);
5091                if (ret != 0) {
5092                        rdev_err(rdev, "Failed to request enable GPIO: %d\n",
5093                                 ret);
5094                        goto clean;
5095                }
5096                /* The regulator core took over the GPIO descriptor */
5097                dangling_cfg_gpiod = false;
5098                dangling_of_gpiod = false;
5099        }
5100
5101        /* register with sysfs */
5102        rdev->dev.class = &regulator_class;
5103        rdev->dev.parent = dev;
5104        dev_set_name(&rdev->dev, "regulator.%lu",
5105                    (unsigned long) atomic_inc_return(&regulator_no));
5106
5107        /* set regulator constraints */
5108        if (init_data)
5109                constraints = &init_data->constraints;
5110
5111        if (init_data && init_data->supply_regulator)
5112                rdev->supply_name = init_data->supply_regulator;
5113        else if (regulator_desc->supply_name)
5114                rdev->supply_name = regulator_desc->supply_name;
5115
5116        /*
5117         * Attempt to resolve the regulator supply, if specified,
5118         * but don't return an error if we fail because we will try
5119         * to resolve it again later as more regulators are added.
5120         */
5121        if (regulator_resolve_supply(rdev))
5122                rdev_dbg(rdev, "unable to resolve supply\n");
5123
5124        ret = set_machine_constraints(rdev, constraints);
5125        if (ret < 0)
5126                goto wash;
5127
5128        mutex_lock(&regulator_list_mutex);
5129        ret = regulator_init_coupling(rdev);
5130        mutex_unlock(&regulator_list_mutex);
5131        if (ret < 0)
5132                goto wash;
5133
5134        /* add consumers devices */
5135        if (init_data) {
5136                mutex_lock(&regulator_list_mutex);
5137                for (i = 0; i < init_data->num_consumer_supplies; i++) {
5138                        ret = set_consumer_device_supply(rdev,
5139                                init_data->consumer_supplies[i].dev_name,
5140                                init_data->consumer_supplies[i].supply);
5141                        if (ret < 0) {
5142                                mutex_unlock(&regulator_list_mutex);
5143                                dev_err(dev, "Failed to set supply %s\n",
5144                                        init_data->consumer_supplies[i].supply);
5145                                goto unset_supplies;
5146                        }
5147                }
5148                mutex_unlock(&regulator_list_mutex);
5149        }
5150
5151        if (!rdev->desc->ops->get_voltage &&
5152            !rdev->desc->ops->list_voltage &&
5153            !rdev->desc->fixed_uV)
5154                rdev->is_switch = true;
5155
5156        dev_set_drvdata(&rdev->dev, rdev);
5157        ret = device_register(&rdev->dev);
5158        if (ret != 0) {
5159                put_device(&rdev->dev);
5160                goto unset_supplies;
5161        }
5162
5163        rdev_init_debugfs(rdev);
5164
5165        /* try to resolve regulators coupling since a new one was registered */
5166        mutex_lock(&regulator_list_mutex);
5167        regulator_resolve_coupling(rdev);
5168        mutex_unlock(&regulator_list_mutex);
5169
5170        /* try to resolve regulators supply since a new one was registered */
5171        class_for_each_device(&regulator_class, NULL, NULL,
5172                              regulator_register_resolve_supply);
5173        kfree(config);
5174        return rdev;
5175
5176unset_supplies:
5177        mutex_lock(&regulator_list_mutex);
5178        unset_regulator_supplies(rdev);
5179        regulator_remove_coupling(rdev);
5180        mutex_unlock(&regulator_list_mutex);
5181wash:
5182        kfree(rdev->constraints);
5183        mutex_lock(&regulator_list_mutex);
5184        regulator_ena_gpio_free(rdev);
5185        mutex_unlock(&regulator_list_mutex);
5186clean:
5187        if (dangling_of_gpiod)
5188                gpiod_put(config->ena_gpiod);
5189        kfree(rdev);
5190        kfree(config);
5191rinse:
5192        if (dangling_cfg_gpiod)
5193                gpiod_put(cfg->ena_gpiod);
5194        return ERR_PTR(ret);
5195}
5196EXPORT_SYMBOL_GPL(regulator_register);
5197
5198/**
5199 * regulator_unregister - unregister regulator
5200 * @rdev: regulator to unregister
5201 *
5202 * Called by regulator drivers to unregister a regulator.
5203 */
5204void regulator_unregister(struct regulator_dev *rdev)
5205{
5206        if (rdev == NULL)
5207                return;
5208
5209        if (rdev->supply) {
5210                while (rdev->use_count--)
5211                        regulator_disable(rdev->supply);
5212                regulator_put(rdev->supply);
5213        }
5214
5215        flush_work(&rdev->disable_work.work);
5216
5217        mutex_lock(&regulator_list_mutex);
5218
5219        debugfs_remove_recursive(rdev->debugfs);
5220        WARN_ON(rdev->open_count);
5221        regulator_remove_coupling(rdev);
5222        unset_regulator_supplies(rdev);
5223        list_del(&rdev->list);
5224        regulator_ena_gpio_free(rdev);
5225        device_unregister(&rdev->dev);
5226
5227        mutex_unlock(&regulator_list_mutex);
5228}
5229EXPORT_SYMBOL_GPL(regulator_unregister);
5230
5231#ifdef CONFIG_SUSPEND
5232/**
5233 * regulator_suspend - prepare regulators for system wide suspend
5234 * @dev: ``&struct device`` pointer that is passed to _regulator_suspend()
5235 *
5236 * Configure each regulator with it's suspend operating parameters for state.
5237 */
5238static int regulator_suspend(struct device *dev)
5239{
5240        struct regulator_dev *rdev = dev_to_rdev(dev);
5241        suspend_state_t state = pm_suspend_target_state;
5242        int ret;
5243
5244        regulator_lock(rdev);
5245        ret = suspend_set_state(rdev, state);
5246        regulator_unlock(rdev);
5247
5248        return ret;
5249}
5250
5251static int regulator_resume(struct device *dev)
5252{
5253        suspend_state_t state = pm_suspend_target_state;
5254        struct regulator_dev *rdev = dev_to_rdev(dev);
5255        struct regulator_state *rstate;
5256        int ret = 0;
5257
5258        rstate = regulator_get_suspend_state(rdev, state);
5259        if (rstate == NULL)
5260                return 0;
5261
5262        regulator_lock(rdev);
5263
5264        if (rdev->desc->ops->resume &&
5265            (rstate->enabled == ENABLE_IN_SUSPEND ||
5266             rstate->enabled == DISABLE_IN_SUSPEND))
5267                ret = rdev->desc->ops->resume(rdev);
5268
5269        regulator_unlock(rdev);
5270
5271        return ret;
5272}
5273#else /* !CONFIG_SUSPEND */
5274
5275#define regulator_suspend       NULL
5276#define regulator_resume        NULL
5277
5278#endif /* !CONFIG_SUSPEND */
5279
5280#ifdef CONFIG_PM
5281static const struct dev_pm_ops __maybe_unused regulator_pm_ops = {
5282        .suspend        = regulator_suspend,
5283        .resume         = regulator_resume,
5284};
5285#endif
5286
5287struct class regulator_class = {
5288        .name = "regulator",
5289        .dev_release = regulator_dev_release,
5290        .dev_groups = regulator_dev_groups,
5291#ifdef CONFIG_PM
5292        .pm = &regulator_pm_ops,
5293#endif
5294};
5295/**
5296 * regulator_has_full_constraints - the system has fully specified constraints
5297 *
5298 * Calling this function will cause the regulator API to disable all
5299 * regulators which have a zero use count and don't have an always_on
5300 * constraint in a late_initcall.
5301 *
5302 * The intention is that this will become the default behaviour in a
5303 * future kernel release so users are encouraged to use this facility
5304 * now.
5305 */
5306void regulator_has_full_constraints(void)
5307{
5308        has_full_constraints = 1;
5309}
5310EXPORT_SYMBOL_GPL(regulator_has_full_constraints);
5311
5312/**
5313 * rdev_get_drvdata - get rdev regulator driver data
5314 * @rdev: regulator
5315 *
5316 * Get rdev regulator driver private data. This call can be used in the
5317 * regulator driver context.
5318 */
5319void *rdev_get_drvdata(struct regulator_dev *rdev)
5320{
5321        return rdev->reg_data;
5322}
5323EXPORT_SYMBOL_GPL(rdev_get_drvdata);
5324
5325/**
5326 * regulator_get_drvdata - get regulator driver data
5327 * @regulator: regulator
5328 *
5329 * Get regulator driver private data. This call can be used in the consumer
5330 * driver context when non API regulator specific functions need to be called.
5331 */
5332void *regulator_get_drvdata(struct regulator *regulator)
5333{
5334        return regulator->rdev->reg_data;
5335}
5336EXPORT_SYMBOL_GPL(regulator_get_drvdata);
5337
5338/**
5339 * regulator_set_drvdata - set regulator driver data
5340 * @regulator: regulator
5341 * @data: data
5342 */
5343void regulator_set_drvdata(struct regulator *regulator, void *data)
5344{
5345        regulator->rdev->reg_data = data;
5346}
5347EXPORT_SYMBOL_GPL(regulator_set_drvdata);
5348
5349/**
5350 * regulator_get_id - get regulator ID
5351 * @rdev: regulator
5352 */
5353int rdev_get_id(struct regulator_dev *rdev)
5354{
5355        return rdev->desc->id;
5356}
5357EXPORT_SYMBOL_GPL(rdev_get_id);
5358
5359struct device *rdev_get_dev(struct regulator_dev *rdev)
5360{
5361        return &rdev->dev;
5362}
5363EXPORT_SYMBOL_GPL(rdev_get_dev);
5364
5365struct regmap *rdev_get_regmap(struct regulator_dev *rdev)
5366{
5367        return rdev->regmap;
5368}
5369EXPORT_SYMBOL_GPL(rdev_get_regmap);
5370
5371void *regulator_get_init_drvdata(struct regulator_init_data *reg_init_data)
5372{
5373        return reg_init_data->driver_data;
5374}
5375EXPORT_SYMBOL_GPL(regulator_get_init_drvdata);
5376
5377#ifdef CONFIG_DEBUG_FS
5378static int supply_map_show(struct seq_file *sf, void *data)
5379{
5380        struct regulator_map *map;
5381
5382        list_for_each_entry(map, &regulator_map_list, list) {
5383                seq_printf(sf, "%s -> %s.%s\n",
5384                                rdev_get_name(map->regulator), map->dev_name,
5385                                map->supply);
5386        }
5387
5388        return 0;
5389}
5390DEFINE_SHOW_ATTRIBUTE(supply_map);
5391
5392struct summary_data {
5393        struct seq_file *s;
5394        struct regulator_dev *parent;
5395        int level;
5396};
5397
5398static void regulator_summary_show_subtree(struct seq_file *s,
5399                                           struct regulator_dev *rdev,
5400                                           int level);
5401
5402static int regulator_summary_show_children(struct device *dev, void *data)
5403{
5404        struct regulator_dev *rdev = dev_to_rdev(dev);
5405        struct summary_data *summary_data = data;
5406
5407        if (rdev->supply && rdev->supply->rdev == summary_data->parent)
5408                regulator_summary_show_subtree(summary_data->s, rdev,
5409                                               summary_data->level + 1);
5410
5411        return 0;
5412}
5413
5414static void regulator_summary_show_subtree(struct seq_file *s,
5415                                           struct regulator_dev *rdev,
5416                                           int level)
5417{
5418        struct regulation_constraints *c;
5419        struct regulator *consumer;
5420        struct summary_data summary_data;
5421        unsigned int opmode;
5422
5423        if (!rdev)
5424                return;
5425
5426        opmode = _regulator_get_mode_unlocked(rdev);
5427        seq_printf(s, "%*s%-*s %3d %4d %6d %7s ",
5428                   level * 3 + 1, "",
5429                   30 - level * 3, rdev_get_name(rdev),
5430                   rdev->use_count, rdev->open_count, rdev->bypass_count,
5431                   regulator_opmode_to_str(opmode));
5432
5433        seq_printf(s, "%5dmV ", regulator_get_voltage_rdev(rdev) / 1000);
5434        seq_printf(s, "%5dmA ",
5435                   _regulator_get_current_limit_unlocked(rdev) / 1000);
5436
5437        c = rdev->constraints;
5438        if (c) {
5439                switch (rdev->desc->type) {
5440                case REGULATOR_VOLTAGE:
5441                        seq_printf(s, "%5dmV %5dmV ",
5442                                   c->min_uV / 1000, c->max_uV / 1000);
5443                        break;
5444                case REGULATOR_CURRENT:
5445                        seq_printf(s, "%5dmA %5dmA ",
5446                                   c->min_uA / 1000, c->max_uA / 1000);
5447                        break;
5448                }
5449        }
5450
5451        seq_puts(s, "\n");
5452
5453        list_for_each_entry(consumer, &rdev->consumer_list, list) {
5454                if (consumer->dev && consumer->dev->class == &regulator_class)
5455                        continue;
5456
5457                seq_printf(s, "%*s%-*s ",
5458                           (level + 1) * 3 + 1, "",
5459                           30 - (level + 1) * 3,
5460                           consumer->dev ? dev_name(consumer->dev) : "deviceless");
5461
5462                switch (rdev->desc->type) {
5463                case REGULATOR_VOLTAGE:
5464                        seq_printf(s, "%3d %33dmA%c%5dmV %5dmV",
5465                                   consumer->enable_count,
5466                                   consumer->uA_load / 1000,
5467                                   consumer->uA_load && !consumer->enable_count ?
5468                                   '*' : ' ',
5469                                   consumer->voltage[PM_SUSPEND_ON].min_uV / 1000,
5470                                   consumer->voltage[PM_SUSPEND_ON].max_uV / 1000);
5471                        break;
5472                case REGULATOR_CURRENT:
5473                        break;
5474                }
5475
5476                seq_puts(s, "\n");
5477        }
5478
5479        summary_data.s = s;
5480        summary_data.level = level;
5481        summary_data.parent = rdev;
5482
5483        class_for_each_device(&regulator_class, NULL, &summary_data,
5484                              regulator_summary_show_children);
5485}
5486
5487struct summary_lock_data {
5488        struct ww_acquire_ctx *ww_ctx;
5489        struct regulator_dev **new_contended_rdev;
5490        struct regulator_dev **old_contended_rdev;
5491};
5492
5493static int regulator_summary_lock_one(struct device *dev, void *data)
5494{
5495        struct regulator_dev *rdev = dev_to_rdev(dev);
5496        struct summary_lock_data *lock_data = data;
5497        int ret = 0;
5498
5499        if (rdev != *lock_data->old_contended_rdev) {
5500                ret = regulator_lock_nested(rdev, lock_data->ww_ctx);
5501
5502                if (ret == -EDEADLK)
5503                        *lock_data->new_contended_rdev = rdev;
5504                else
5505                        WARN_ON_ONCE(ret);
5506        } else {
5507                *lock_data->old_contended_rdev = NULL;
5508        }
5509
5510        return ret;
5511}
5512
5513static int regulator_summary_unlock_one(struct device *dev, void *data)
5514{
5515        struct regulator_dev *rdev = dev_to_rdev(dev);
5516        struct summary_lock_data *lock_data = data;
5517
5518        if (lock_data) {
5519                if (rdev == *lock_data->new_contended_rdev)
5520                        return -EDEADLK;
5521        }
5522
5523        regulator_unlock(rdev);
5524
5525        return 0;
5526}
5527
5528static int regulator_summary_lock_all(struct ww_acquire_ctx *ww_ctx,
5529                                      struct regulator_dev **new_contended_rdev,
5530                                      struct regulator_dev **old_contended_rdev)
5531{
5532        struct summary_lock_data lock_data;
5533        int ret;
5534
5535        lock_data.ww_ctx = ww_ctx;
5536        lock_data.new_contended_rdev = new_contended_rdev;
5537        lock_data.old_contended_rdev = old_contended_rdev;
5538
5539        ret = class_for_each_device(&regulator_class, NULL, &lock_data,
5540                                    regulator_summary_lock_one);
5541        if (ret)
5542                class_for_each_device(&regulator_class, NULL, &lock_data,
5543                                      regulator_summary_unlock_one);
5544
5545        return ret;
5546}
5547
5548static void regulator_summary_lock(struct ww_acquire_ctx *ww_ctx)
5549{
5550        struct regulator_dev *new_contended_rdev = NULL;
5551        struct regulator_dev *old_contended_rdev = NULL;
5552        int err;
5553
5554        mutex_lock(&regulator_list_mutex);
5555
5556        ww_acquire_init(ww_ctx, &regulator_ww_class);
5557
5558        do {
5559                if (new_contended_rdev) {
5560                        ww_mutex_lock_slow(&new_contended_rdev->mutex, ww_ctx);
5561                        old_contended_rdev = new_contended_rdev;
5562                        old_contended_rdev->ref_cnt++;
5563                }
5564
5565                err = regulator_summary_lock_all(ww_ctx,
5566                                                 &new_contended_rdev,
5567                                                 &old_contended_rdev);
5568
5569                if (old_contended_rdev)
5570                        regulator_unlock(old_contended_rdev);
5571
5572        } while (err == -EDEADLK);
5573
5574        ww_acquire_done(ww_ctx);
5575}
5576
5577static void regulator_summary_unlock(struct ww_acquire_ctx *ww_ctx)
5578{
5579        class_for_each_device(&regulator_class, NULL, NULL,
5580                              regulator_summary_unlock_one);
5581        ww_acquire_fini(ww_ctx);
5582
5583        mutex_unlock(&regulator_list_mutex);
5584}
5585
5586static int regulator_summary_show_roots(struct device *dev, void *data)
5587{
5588        struct regulator_dev *rdev = dev_to_rdev(dev);
5589        struct seq_file *s = data;
5590
5591        if (!rdev->supply)
5592                regulator_summary_show_subtree(s, rdev, 0);
5593
5594        return 0;
5595}
5596
5597static int regulator_summary_show(struct seq_file *s, void *data)
5598{
5599        struct ww_acquire_ctx ww_ctx;
5600
5601        seq_puts(s, " regulator                      use open bypass  opmode voltage current     min     max\n");
5602        seq_puts(s, "---------------------------------------------------------------------------------------\n");
5603
5604        regulator_summary_lock(&ww_ctx);
5605
5606        class_for_each_device(&regulator_class, NULL, s,
5607                              regulator_summary_show_roots);
5608
5609        regulator_summary_unlock(&ww_ctx);
5610
5611        return 0;
5612}
5613DEFINE_SHOW_ATTRIBUTE(regulator_summary);
5614#endif /* CONFIG_DEBUG_FS */
5615
5616static int __init regulator_init(void)
5617{
5618        int ret;
5619
5620        ret = class_register(&regulator_class);
5621
5622        debugfs_root = debugfs_create_dir("regulator", NULL);
5623        if (!debugfs_root)
5624                pr_warn("regulator: Failed to create debugfs directory\n");
5625
5626#ifdef CONFIG_DEBUG_FS
5627        debugfs_create_file("supply_map", 0444, debugfs_root, NULL,
5628                            &supply_map_fops);
5629
5630        debugfs_create_file("regulator_summary", 0444, debugfs_root,
5631                            NULL, &regulator_summary_fops);
5632#endif
5633        regulator_dummy_init();
5634
5635        regulator_coupler_register(&generic_regulator_coupler);
5636
5637        return ret;
5638}
5639
5640/* init early to allow our consumers to complete system booting */
5641core_initcall(regulator_init);
5642
5643static int __init regulator_late_cleanup(struct device *dev, void *data)
5644{
5645        struct regulator_dev *rdev = dev_to_rdev(dev);
5646        const struct regulator_ops *ops = rdev->desc->ops;
5647        struct regulation_constraints *c = rdev->constraints;
5648        int enabled, ret;
5649
5650        if (c && c->always_on)
5651                return 0;
5652
5653        if (!regulator_ops_is_valid(rdev, REGULATOR_CHANGE_STATUS))
5654                return 0;
5655
5656        regulator_lock(rdev);
5657
5658        if (rdev->use_count)
5659                goto unlock;
5660
5661        /* If we can't read the status assume it's on. */
5662        if (ops->is_enabled)
5663                enabled = ops->is_enabled(rdev);
5664        else
5665                enabled = 1;
5666
5667        if (!enabled)
5668                goto unlock;
5669
5670        if (have_full_constraints()) {
5671                /* We log since this may kill the system if it goes
5672                 * wrong. */
5673                rdev_info(rdev, "disabling\n");
5674                ret = _regulator_do_disable(rdev);
5675                if (ret != 0)
5676                        rdev_err(rdev, "couldn't disable: %d\n", ret);
5677        } else {
5678                /* The intention is that in future we will
5679                 * assume that full constraints are provided
5680                 * so warn even if we aren't going to do
5681                 * anything here.
5682                 */
5683                rdev_warn(rdev, "incomplete constraints, leaving on\n");
5684        }
5685
5686unlock:
5687        regulator_unlock(rdev);
5688
5689        return 0;
5690}
5691
5692static int __init regulator_init_complete(void)
5693{
5694        /*
5695         * Since DT doesn't provide an idiomatic mechanism for
5696         * enabling full constraints and since it's much more natural
5697         * with DT to provide them just assume that a DT enabled
5698         * system has full constraints.
5699         */
5700        if (of_have_populated_dt())
5701                has_full_constraints = true;
5702
5703        /*
5704         * Regulators may had failed to resolve their input supplies
5705         * when were registered, either because the input supply was
5706         * not registered yet or because its parent device was not
5707         * bound yet. So attempt to resolve the input supplies for
5708         * pending regulators before trying to disable unused ones.
5709         */
5710        class_for_each_device(&regulator_class, NULL, NULL,
5711                              regulator_register_resolve_supply);
5712
5713        /* If we have a full configuration then disable any regulators
5714         * we have permission to change the status for and which are
5715         * not in use or always_on.  This is effectively the default
5716         * for DT and ACPI as they have full constraints.
5717         */
5718        class_for_each_device(&regulator_class, NULL, NULL,
5719                              regulator_late_cleanup);
5720
5721        return 0;
5722}
5723late_initcall_sync(regulator_init_complete);
5724