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