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