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