LCOV - code coverage report
Current view: top level - drivers/regulator - core.c (source / functions) Hit Total Coverage
Test: Real Lines: 256 1882 13.6 %
Date: 2020-10-17 15:46:16 Functions: 0 170 0.0 %
Legend: Neither, QEMU, Real, Both Branches: 0 0 -

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

Generated by: LCOV version 1.14