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