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