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