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