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