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