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