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