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