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