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