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