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