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