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