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