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