USB: serial: ftdi_sio: make process-packet buffer unsigned
[linux/fpc-iii.git] / drivers / regulator / core.c
bloba17aebe0aa7a73f8bd10b5babdbed9fade34f496
1 // SPDX-License-Identifier: GPL-2.0-or-later
2 //
3 // core.c -- Voltage/Current Regulator framework.
4 //
5 // Copyright 2007, 2008 Wolfson Microelectronics PLC.
6 // Copyright 2008 SlimLogic Ltd.
7 //
8 // Author: Liam Girdwood <lrg@slimlogic.co.uk>
10 #include <linux/kernel.h>
11 #include <linux/init.h>
12 #include <linux/debugfs.h>
13 #include <linux/device.h>
14 #include <linux/slab.h>
15 #include <linux/async.h>
16 #include <linux/err.h>
17 #include <linux/mutex.h>
18 #include <linux/suspend.h>
19 #include <linux/delay.h>
20 #include <linux/gpio/consumer.h>
21 #include <linux/of.h>
22 #include <linux/regmap.h>
23 #include <linux/regulator/of_regulator.h>
24 #include <linux/regulator/consumer.h>
25 #include <linux/regulator/coupler.h>
26 #include <linux/regulator/driver.h>
27 #include <linux/regulator/machine.h>
28 #include <linux/module.h>
30 #define CREATE_TRACE_POINTS
31 #include <trace/events/regulator.h>
33 #include "dummy.h"
34 #include "internal.h"
36 #define rdev_crit(rdev, fmt, ...) \
37 pr_crit("%s: " fmt, rdev_get_name(rdev), ##__VA_ARGS__)
38 #define rdev_err(rdev, fmt, ...) \
39 pr_err("%s: " fmt, rdev_get_name(rdev), ##__VA_ARGS__)
40 #define rdev_warn(rdev, fmt, ...) \
41 pr_warn("%s: " fmt, rdev_get_name(rdev), ##__VA_ARGS__)
42 #define rdev_info(rdev, fmt, ...) \
43 pr_info("%s: " fmt, rdev_get_name(rdev), ##__VA_ARGS__)
44 #define rdev_dbg(rdev, fmt, ...) \
45 pr_debug("%s: " fmt, rdev_get_name(rdev), ##__VA_ARGS__)
47 static DEFINE_WW_CLASS(regulator_ww_class);
48 static DEFINE_MUTEX(regulator_nesting_mutex);
49 static DEFINE_MUTEX(regulator_list_mutex);
50 static LIST_HEAD(regulator_map_list);
51 static LIST_HEAD(regulator_ena_gpio_list);
52 static LIST_HEAD(regulator_supply_alias_list);
53 static LIST_HEAD(regulator_coupler_list);
54 static bool has_full_constraints;
56 static struct dentry *debugfs_root;
59 * struct regulator_map
61 * Used to provide symbolic supply names to devices.
63 struct regulator_map {
64 struct list_head list;
65 const char *dev_name; /* The dev_name() for the consumer */
66 const char *supply;
67 struct regulator_dev *regulator;
71 * struct regulator_enable_gpio
73 * Management for shared enable GPIO pin
75 struct regulator_enable_gpio {
76 struct list_head list;
77 struct gpio_desc *gpiod;
78 u32 enable_count; /* a number of enabled shared GPIO */
79 u32 request_count; /* a number of requested shared GPIO */
83 * struct regulator_supply_alias
85 * Used to map lookups for a supply onto an alternative device.
87 struct regulator_supply_alias {
88 struct list_head list;
89 struct device *src_dev;
90 const char *src_supply;
91 struct device *alias_dev;
92 const char *alias_supply;
95 static int _regulator_is_enabled(struct regulator_dev *rdev);
96 static int _regulator_disable(struct regulator *regulator);
97 static int _regulator_get_current_limit(struct regulator_dev *rdev);
98 static unsigned int _regulator_get_mode(struct regulator_dev *rdev);
99 static int _notifier_call_chain(struct regulator_dev *rdev,
100 unsigned long event, void *data);
101 static int _regulator_do_set_voltage(struct regulator_dev *rdev,
102 int min_uV, int max_uV);
103 static int regulator_balance_voltage(struct regulator_dev *rdev,
104 suspend_state_t state);
105 static struct regulator *create_regulator(struct regulator_dev *rdev,
106 struct device *dev,
107 const char *supply_name);
108 static void _regulator_put(struct regulator *regulator);
110 const char *rdev_get_name(struct regulator_dev *rdev)
112 if (rdev->constraints && rdev->constraints->name)
113 return rdev->constraints->name;
114 else if (rdev->desc->name)
115 return rdev->desc->name;
116 else
117 return "";
120 static bool have_full_constraints(void)
122 return has_full_constraints || of_have_populated_dt();
125 static bool regulator_ops_is_valid(struct regulator_dev *rdev, int ops)
127 if (!rdev->constraints) {
128 rdev_err(rdev, "no constraints\n");
129 return false;
132 if (rdev->constraints->valid_ops_mask & ops)
133 return true;
135 return false;
139 * regulator_lock_nested - lock a single regulator
140 * @rdev: regulator source
141 * @ww_ctx: w/w mutex acquire context
143 * This function can be called many times by one task on
144 * a single regulator and its mutex will be locked only
145 * once. If a task, which is calling this function is other
146 * than the one, which initially locked the mutex, it will
147 * wait on mutex.
149 static inline int regulator_lock_nested(struct regulator_dev *rdev,
150 struct ww_acquire_ctx *ww_ctx)
152 bool lock = false;
153 int ret = 0;
155 mutex_lock(&regulator_nesting_mutex);
157 if (ww_ctx || !ww_mutex_trylock(&rdev->mutex)) {
158 if (rdev->mutex_owner == current)
159 rdev->ref_cnt++;
160 else
161 lock = true;
163 if (lock) {
164 mutex_unlock(&regulator_nesting_mutex);
165 ret = ww_mutex_lock(&rdev->mutex, ww_ctx);
166 mutex_lock(&regulator_nesting_mutex);
168 } else {
169 lock = true;
172 if (lock && ret != -EDEADLK) {
173 rdev->ref_cnt++;
174 rdev->mutex_owner = current;
177 mutex_unlock(&regulator_nesting_mutex);
179 return ret;
183 * regulator_lock - lock a single regulator
184 * @rdev: regulator source
186 * This function can be called many times by one task on
187 * a single regulator and its mutex will be locked only
188 * once. If a task, which is calling this function is other
189 * than the one, which initially locked the mutex, it will
190 * wait on mutex.
192 void regulator_lock(struct regulator_dev *rdev)
194 regulator_lock_nested(rdev, NULL);
196 EXPORT_SYMBOL_GPL(regulator_lock);
199 * regulator_unlock - unlock a single regulator
200 * @rdev: regulator_source
202 * This function unlocks the mutex when the
203 * reference counter reaches 0.
205 void regulator_unlock(struct regulator_dev *rdev)
207 mutex_lock(&regulator_nesting_mutex);
209 if (--rdev->ref_cnt == 0) {
210 rdev->mutex_owner = NULL;
211 ww_mutex_unlock(&rdev->mutex);
214 WARN_ON_ONCE(rdev->ref_cnt < 0);
216 mutex_unlock(&regulator_nesting_mutex);
218 EXPORT_SYMBOL_GPL(regulator_unlock);
220 static bool regulator_supply_is_couple(struct regulator_dev *rdev)
222 struct regulator_dev *c_rdev;
223 int i;
225 for (i = 1; i < rdev->coupling_desc.n_coupled; i++) {
226 c_rdev = rdev->coupling_desc.coupled_rdevs[i];
228 if (rdev->supply->rdev == c_rdev)
229 return true;
232 return false;
235 static void regulator_unlock_recursive(struct regulator_dev *rdev,
236 unsigned int n_coupled)
238 struct regulator_dev *c_rdev;
239 int i;
241 for (i = n_coupled; i > 0; i--) {
242 c_rdev = rdev->coupling_desc.coupled_rdevs[i - 1];
244 if (!c_rdev)
245 continue;
247 if (c_rdev->supply && !regulator_supply_is_couple(c_rdev))
248 regulator_unlock_recursive(
249 c_rdev->supply->rdev,
250 c_rdev->coupling_desc.n_coupled);
252 regulator_unlock(c_rdev);
256 static int regulator_lock_recursive(struct regulator_dev *rdev,
257 struct regulator_dev **new_contended_rdev,
258 struct regulator_dev **old_contended_rdev,
259 struct ww_acquire_ctx *ww_ctx)
261 struct regulator_dev *c_rdev;
262 int i, err;
264 for (i = 0; i < rdev->coupling_desc.n_coupled; i++) {
265 c_rdev = rdev->coupling_desc.coupled_rdevs[i];
267 if (!c_rdev)
268 continue;
270 if (c_rdev != *old_contended_rdev) {
271 err = regulator_lock_nested(c_rdev, ww_ctx);
272 if (err) {
273 if (err == -EDEADLK) {
274 *new_contended_rdev = c_rdev;
275 goto err_unlock;
278 /* shouldn't happen */
279 WARN_ON_ONCE(err != -EALREADY);
281 } else {
282 *old_contended_rdev = NULL;
285 if (c_rdev->supply && !regulator_supply_is_couple(c_rdev)) {
286 err = regulator_lock_recursive(c_rdev->supply->rdev,
287 new_contended_rdev,
288 old_contended_rdev,
289 ww_ctx);
290 if (err) {
291 regulator_unlock(c_rdev);
292 goto err_unlock;
297 return 0;
299 err_unlock:
300 regulator_unlock_recursive(rdev, i);
302 return err;
306 * regulator_unlock_dependent - unlock regulator's suppliers and coupled
307 * regulators
308 * @rdev: regulator source
309 * @ww_ctx: w/w mutex acquire context
311 * Unlock all regulators related with rdev by coupling or supplying.
313 static void regulator_unlock_dependent(struct regulator_dev *rdev,
314 struct ww_acquire_ctx *ww_ctx)
316 regulator_unlock_recursive(rdev, rdev->coupling_desc.n_coupled);
317 ww_acquire_fini(ww_ctx);
321 * regulator_lock_dependent - lock regulator's suppliers and coupled regulators
322 * @rdev: regulator source
323 * @ww_ctx: w/w mutex acquire context
325 * This function as a wrapper on regulator_lock_recursive(), which locks
326 * all regulators related with rdev by coupling or supplying.
328 static void regulator_lock_dependent(struct regulator_dev *rdev,
329 struct ww_acquire_ctx *ww_ctx)
331 struct regulator_dev *new_contended_rdev = NULL;
332 struct regulator_dev *old_contended_rdev = NULL;
333 int err;
335 mutex_lock(&regulator_list_mutex);
337 ww_acquire_init(ww_ctx, &regulator_ww_class);
339 do {
340 if (new_contended_rdev) {
341 ww_mutex_lock_slow(&new_contended_rdev->mutex, ww_ctx);
342 old_contended_rdev = new_contended_rdev;
343 old_contended_rdev->ref_cnt++;
346 err = regulator_lock_recursive(rdev,
347 &new_contended_rdev,
348 &old_contended_rdev,
349 ww_ctx);
351 if (old_contended_rdev)
352 regulator_unlock(old_contended_rdev);
354 } while (err == -EDEADLK);
356 ww_acquire_done(ww_ctx);
358 mutex_unlock(&regulator_list_mutex);
362 * of_get_child_regulator - get a child regulator device node
363 * based on supply name
364 * @parent: Parent device node
365 * @prop_name: Combination regulator supply name and "-supply"
367 * Traverse all child nodes.
368 * Extract the child regulator device node corresponding to the supply name.
369 * returns the device node corresponding to the regulator if found, else
370 * returns NULL.
372 static struct device_node *of_get_child_regulator(struct device_node *parent,
373 const char *prop_name)
375 struct device_node *regnode = NULL;
376 struct device_node *child = NULL;
378 for_each_child_of_node(parent, child) {
379 regnode = of_parse_phandle(child, prop_name, 0);
381 if (!regnode) {
382 regnode = of_get_child_regulator(child, prop_name);
383 if (regnode)
384 goto err_node_put;
385 } else {
386 goto err_node_put;
389 return NULL;
391 err_node_put:
392 of_node_put(child);
393 return regnode;
397 * of_get_regulator - get a regulator device node based on supply name
398 * @dev: Device pointer for the consumer (of regulator) device
399 * @supply: regulator supply name
401 * Extract the regulator device node corresponding to the supply name.
402 * returns the device node corresponding to the regulator if found, else
403 * returns NULL.
405 static struct device_node *of_get_regulator(struct device *dev, const char *supply)
407 struct device_node *regnode = NULL;
408 char prop_name[32]; /* 32 is max size of property name */
410 dev_dbg(dev, "Looking up %s-supply from device tree\n", supply);
412 snprintf(prop_name, 32, "%s-supply", supply);
413 regnode = of_parse_phandle(dev->of_node, prop_name, 0);
415 if (!regnode) {
416 regnode = of_get_child_regulator(dev->of_node, prop_name);
417 if (regnode)
418 return regnode;
420 dev_dbg(dev, "Looking up %s property in node %pOF failed\n",
421 prop_name, dev->of_node);
422 return NULL;
424 return regnode;
427 /* Platform voltage constraint check */
428 int regulator_check_voltage(struct regulator_dev *rdev,
429 int *min_uV, int *max_uV)
431 BUG_ON(*min_uV > *max_uV);
433 if (!regulator_ops_is_valid(rdev, REGULATOR_CHANGE_VOLTAGE)) {
434 rdev_err(rdev, "voltage operation not allowed\n");
435 return -EPERM;
438 if (*max_uV > rdev->constraints->max_uV)
439 *max_uV = rdev->constraints->max_uV;
440 if (*min_uV < rdev->constraints->min_uV)
441 *min_uV = rdev->constraints->min_uV;
443 if (*min_uV > *max_uV) {
444 rdev_err(rdev, "unsupportable voltage range: %d-%duV\n",
445 *min_uV, *max_uV);
446 return -EINVAL;
449 return 0;
452 /* return 0 if the state is valid */
453 static int regulator_check_states(suspend_state_t state)
455 return (state > PM_SUSPEND_MAX || state == PM_SUSPEND_TO_IDLE);
458 /* Make sure we select a voltage that suits the needs of all
459 * regulator consumers
461 int regulator_check_consumers(struct regulator_dev *rdev,
462 int *min_uV, int *max_uV,
463 suspend_state_t state)
465 struct regulator *regulator;
466 struct regulator_voltage *voltage;
468 list_for_each_entry(regulator, &rdev->consumer_list, list) {
469 voltage = &regulator->voltage[state];
471 * Assume consumers that didn't say anything are OK
472 * with anything in the constraint range.
474 if (!voltage->min_uV && !voltage->max_uV)
475 continue;
477 if (*max_uV > voltage->max_uV)
478 *max_uV = voltage->max_uV;
479 if (*min_uV < voltage->min_uV)
480 *min_uV = voltage->min_uV;
483 if (*min_uV > *max_uV) {
484 rdev_err(rdev, "Restricting voltage, %u-%uuV\n",
485 *min_uV, *max_uV);
486 return -EINVAL;
489 return 0;
492 /* current constraint check */
493 static int regulator_check_current_limit(struct regulator_dev *rdev,
494 int *min_uA, int *max_uA)
496 BUG_ON(*min_uA > *max_uA);
498 if (!regulator_ops_is_valid(rdev, REGULATOR_CHANGE_CURRENT)) {
499 rdev_err(rdev, "current operation not allowed\n");
500 return -EPERM;
503 if (*max_uA > rdev->constraints->max_uA)
504 *max_uA = rdev->constraints->max_uA;
505 if (*min_uA < rdev->constraints->min_uA)
506 *min_uA = rdev->constraints->min_uA;
508 if (*min_uA > *max_uA) {
509 rdev_err(rdev, "unsupportable current range: %d-%duA\n",
510 *min_uA, *max_uA);
511 return -EINVAL;
514 return 0;
517 /* operating mode constraint check */
518 static int regulator_mode_constrain(struct regulator_dev *rdev,
519 unsigned int *mode)
521 switch (*mode) {
522 case REGULATOR_MODE_FAST:
523 case REGULATOR_MODE_NORMAL:
524 case REGULATOR_MODE_IDLE:
525 case REGULATOR_MODE_STANDBY:
526 break;
527 default:
528 rdev_err(rdev, "invalid mode %x specified\n", *mode);
529 return -EINVAL;
532 if (!regulator_ops_is_valid(rdev, REGULATOR_CHANGE_MODE)) {
533 rdev_err(rdev, "mode operation not allowed\n");
534 return -EPERM;
537 /* The modes are bitmasks, the most power hungry modes having
538 * the lowest values. If the requested mode isn't supported
539 * try higher modes. */
540 while (*mode) {
541 if (rdev->constraints->valid_modes_mask & *mode)
542 return 0;
543 *mode /= 2;
546 return -EINVAL;
549 static inline struct regulator_state *
550 regulator_get_suspend_state(struct regulator_dev *rdev, suspend_state_t state)
552 if (rdev->constraints == NULL)
553 return NULL;
555 switch (state) {
556 case PM_SUSPEND_STANDBY:
557 return &rdev->constraints->state_standby;
558 case PM_SUSPEND_MEM:
559 return &rdev->constraints->state_mem;
560 case PM_SUSPEND_MAX:
561 return &rdev->constraints->state_disk;
562 default:
563 return NULL;
567 static ssize_t regulator_uV_show(struct device *dev,
568 struct device_attribute *attr, char *buf)
570 struct regulator_dev *rdev = dev_get_drvdata(dev);
571 int uV;
573 regulator_lock(rdev);
574 uV = regulator_get_voltage_rdev(rdev);
575 regulator_unlock(rdev);
577 if (uV < 0)
578 return uV;
579 return sprintf(buf, "%d\n", uV);
581 static DEVICE_ATTR(microvolts, 0444, regulator_uV_show, NULL);
583 static ssize_t regulator_uA_show(struct device *dev,
584 struct device_attribute *attr, char *buf)
586 struct regulator_dev *rdev = dev_get_drvdata(dev);
588 return sprintf(buf, "%d\n", _regulator_get_current_limit(rdev));
590 static DEVICE_ATTR(microamps, 0444, regulator_uA_show, NULL);
592 static ssize_t name_show(struct device *dev, struct device_attribute *attr,
593 char *buf)
595 struct regulator_dev *rdev = dev_get_drvdata(dev);
597 return sprintf(buf, "%s\n", rdev_get_name(rdev));
599 static DEVICE_ATTR_RO(name);
601 static const char *regulator_opmode_to_str(int mode)
603 switch (mode) {
604 case REGULATOR_MODE_FAST:
605 return "fast";
606 case REGULATOR_MODE_NORMAL:
607 return "normal";
608 case REGULATOR_MODE_IDLE:
609 return "idle";
610 case REGULATOR_MODE_STANDBY:
611 return "standby";
613 return "unknown";
616 static ssize_t regulator_print_opmode(char *buf, int mode)
618 return sprintf(buf, "%s\n", regulator_opmode_to_str(mode));
621 static ssize_t regulator_opmode_show(struct device *dev,
622 struct device_attribute *attr, char *buf)
624 struct regulator_dev *rdev = dev_get_drvdata(dev);
626 return regulator_print_opmode(buf, _regulator_get_mode(rdev));
628 static DEVICE_ATTR(opmode, 0444, regulator_opmode_show, NULL);
630 static ssize_t regulator_print_state(char *buf, int state)
632 if (state > 0)
633 return sprintf(buf, "enabled\n");
634 else if (state == 0)
635 return sprintf(buf, "disabled\n");
636 else
637 return sprintf(buf, "unknown\n");
640 static ssize_t regulator_state_show(struct device *dev,
641 struct device_attribute *attr, char *buf)
643 struct regulator_dev *rdev = dev_get_drvdata(dev);
644 ssize_t ret;
646 regulator_lock(rdev);
647 ret = regulator_print_state(buf, _regulator_is_enabled(rdev));
648 regulator_unlock(rdev);
650 return ret;
652 static DEVICE_ATTR(state, 0444, regulator_state_show, NULL);
654 static ssize_t regulator_status_show(struct device *dev,
655 struct device_attribute *attr, char *buf)
657 struct regulator_dev *rdev = dev_get_drvdata(dev);
658 int status;
659 char *label;
661 status = rdev->desc->ops->get_status(rdev);
662 if (status < 0)
663 return status;
665 switch (status) {
666 case REGULATOR_STATUS_OFF:
667 label = "off";
668 break;
669 case REGULATOR_STATUS_ON:
670 label = "on";
671 break;
672 case REGULATOR_STATUS_ERROR:
673 label = "error";
674 break;
675 case REGULATOR_STATUS_FAST:
676 label = "fast";
677 break;
678 case REGULATOR_STATUS_NORMAL:
679 label = "normal";
680 break;
681 case REGULATOR_STATUS_IDLE:
682 label = "idle";
683 break;
684 case REGULATOR_STATUS_STANDBY:
685 label = "standby";
686 break;
687 case REGULATOR_STATUS_BYPASS:
688 label = "bypass";
689 break;
690 case REGULATOR_STATUS_UNDEFINED:
691 label = "undefined";
692 break;
693 default:
694 return -ERANGE;
697 return sprintf(buf, "%s\n", label);
699 static DEVICE_ATTR(status, 0444, regulator_status_show, NULL);
701 static ssize_t regulator_min_uA_show(struct device *dev,
702 struct device_attribute *attr, char *buf)
704 struct regulator_dev *rdev = dev_get_drvdata(dev);
706 if (!rdev->constraints)
707 return sprintf(buf, "constraint not defined\n");
709 return sprintf(buf, "%d\n", rdev->constraints->min_uA);
711 static DEVICE_ATTR(min_microamps, 0444, regulator_min_uA_show, NULL);
713 static ssize_t regulator_max_uA_show(struct device *dev,
714 struct device_attribute *attr, char *buf)
716 struct regulator_dev *rdev = dev_get_drvdata(dev);
718 if (!rdev->constraints)
719 return sprintf(buf, "constraint not defined\n");
721 return sprintf(buf, "%d\n", rdev->constraints->max_uA);
723 static DEVICE_ATTR(max_microamps, 0444, regulator_max_uA_show, NULL);
725 static ssize_t regulator_min_uV_show(struct device *dev,
726 struct device_attribute *attr, char *buf)
728 struct regulator_dev *rdev = dev_get_drvdata(dev);
730 if (!rdev->constraints)
731 return sprintf(buf, "constraint not defined\n");
733 return sprintf(buf, "%d\n", rdev->constraints->min_uV);
735 static DEVICE_ATTR(min_microvolts, 0444, regulator_min_uV_show, NULL);
737 static ssize_t regulator_max_uV_show(struct device *dev,
738 struct device_attribute *attr, char *buf)
740 struct regulator_dev *rdev = dev_get_drvdata(dev);
742 if (!rdev->constraints)
743 return sprintf(buf, "constraint not defined\n");
745 return sprintf(buf, "%d\n", rdev->constraints->max_uV);
747 static DEVICE_ATTR(max_microvolts, 0444, regulator_max_uV_show, NULL);
749 static ssize_t regulator_total_uA_show(struct device *dev,
750 struct device_attribute *attr, char *buf)
752 struct regulator_dev *rdev = dev_get_drvdata(dev);
753 struct regulator *regulator;
754 int uA = 0;
756 regulator_lock(rdev);
757 list_for_each_entry(regulator, &rdev->consumer_list, list) {
758 if (regulator->enable_count)
759 uA += regulator->uA_load;
761 regulator_unlock(rdev);
762 return sprintf(buf, "%d\n", uA);
764 static DEVICE_ATTR(requested_microamps, 0444, regulator_total_uA_show, NULL);
766 static ssize_t num_users_show(struct device *dev, struct device_attribute *attr,
767 char *buf)
769 struct regulator_dev *rdev = dev_get_drvdata(dev);
770 return sprintf(buf, "%d\n", rdev->use_count);
772 static DEVICE_ATTR_RO(num_users);
774 static ssize_t type_show(struct device *dev, struct device_attribute *attr,
775 char *buf)
777 struct regulator_dev *rdev = dev_get_drvdata(dev);
779 switch (rdev->desc->type) {
780 case REGULATOR_VOLTAGE:
781 return sprintf(buf, "voltage\n");
782 case REGULATOR_CURRENT:
783 return sprintf(buf, "current\n");
785 return sprintf(buf, "unknown\n");
787 static DEVICE_ATTR_RO(type);
789 static ssize_t regulator_suspend_mem_uV_show(struct device *dev,
790 struct device_attribute *attr, char *buf)
792 struct regulator_dev *rdev = dev_get_drvdata(dev);
794 return sprintf(buf, "%d\n", rdev->constraints->state_mem.uV);
796 static DEVICE_ATTR(suspend_mem_microvolts, 0444,
797 regulator_suspend_mem_uV_show, NULL);
799 static ssize_t regulator_suspend_disk_uV_show(struct device *dev,
800 struct device_attribute *attr, char *buf)
802 struct regulator_dev *rdev = dev_get_drvdata(dev);
804 return sprintf(buf, "%d\n", rdev->constraints->state_disk.uV);
806 static DEVICE_ATTR(suspend_disk_microvolts, 0444,
807 regulator_suspend_disk_uV_show, NULL);
809 static ssize_t regulator_suspend_standby_uV_show(struct device *dev,
810 struct device_attribute *attr, char *buf)
812 struct regulator_dev *rdev = dev_get_drvdata(dev);
814 return sprintf(buf, "%d\n", rdev->constraints->state_standby.uV);
816 static DEVICE_ATTR(suspend_standby_microvolts, 0444,
817 regulator_suspend_standby_uV_show, NULL);
819 static ssize_t regulator_suspend_mem_mode_show(struct device *dev,
820 struct device_attribute *attr, char *buf)
822 struct regulator_dev *rdev = dev_get_drvdata(dev);
824 return regulator_print_opmode(buf,
825 rdev->constraints->state_mem.mode);
827 static DEVICE_ATTR(suspend_mem_mode, 0444,
828 regulator_suspend_mem_mode_show, NULL);
830 static ssize_t regulator_suspend_disk_mode_show(struct device *dev,
831 struct device_attribute *attr, char *buf)
833 struct regulator_dev *rdev = dev_get_drvdata(dev);
835 return regulator_print_opmode(buf,
836 rdev->constraints->state_disk.mode);
838 static DEVICE_ATTR(suspend_disk_mode, 0444,
839 regulator_suspend_disk_mode_show, NULL);
841 static ssize_t regulator_suspend_standby_mode_show(struct device *dev,
842 struct device_attribute *attr, char *buf)
844 struct regulator_dev *rdev = dev_get_drvdata(dev);
846 return regulator_print_opmode(buf,
847 rdev->constraints->state_standby.mode);
849 static DEVICE_ATTR(suspend_standby_mode, 0444,
850 regulator_suspend_standby_mode_show, NULL);
852 static ssize_t regulator_suspend_mem_state_show(struct device *dev,
853 struct device_attribute *attr, char *buf)
855 struct regulator_dev *rdev = dev_get_drvdata(dev);
857 return regulator_print_state(buf,
858 rdev->constraints->state_mem.enabled);
860 static DEVICE_ATTR(suspend_mem_state, 0444,
861 regulator_suspend_mem_state_show, NULL);
863 static ssize_t regulator_suspend_disk_state_show(struct device *dev,
864 struct device_attribute *attr, char *buf)
866 struct regulator_dev *rdev = dev_get_drvdata(dev);
868 return regulator_print_state(buf,
869 rdev->constraints->state_disk.enabled);
871 static DEVICE_ATTR(suspend_disk_state, 0444,
872 regulator_suspend_disk_state_show, NULL);
874 static ssize_t regulator_suspend_standby_state_show(struct device *dev,
875 struct device_attribute *attr, char *buf)
877 struct regulator_dev *rdev = dev_get_drvdata(dev);
879 return regulator_print_state(buf,
880 rdev->constraints->state_standby.enabled);
882 static DEVICE_ATTR(suspend_standby_state, 0444,
883 regulator_suspend_standby_state_show, NULL);
885 static ssize_t regulator_bypass_show(struct device *dev,
886 struct device_attribute *attr, char *buf)
888 struct regulator_dev *rdev = dev_get_drvdata(dev);
889 const char *report;
890 bool bypass;
891 int ret;
893 ret = rdev->desc->ops->get_bypass(rdev, &bypass);
895 if (ret != 0)
896 report = "unknown";
897 else if (bypass)
898 report = "enabled";
899 else
900 report = "disabled";
902 return sprintf(buf, "%s\n", report);
904 static DEVICE_ATTR(bypass, 0444,
905 regulator_bypass_show, NULL);
907 /* Calculate the new optimum regulator operating mode based on the new total
908 * consumer load. All locks held by caller */
909 static int drms_uA_update(struct regulator_dev *rdev)
911 struct regulator *sibling;
912 int current_uA = 0, output_uV, input_uV, err;
913 unsigned int mode;
916 * first check to see if we can set modes at all, otherwise just
917 * tell the consumer everything is OK.
919 if (!regulator_ops_is_valid(rdev, REGULATOR_CHANGE_DRMS)) {
920 rdev_dbg(rdev, "DRMS operation not allowed\n");
921 return 0;
924 if (!rdev->desc->ops->get_optimum_mode &&
925 !rdev->desc->ops->set_load)
926 return 0;
928 if (!rdev->desc->ops->set_mode &&
929 !rdev->desc->ops->set_load)
930 return -EINVAL;
932 /* calc total requested load */
933 list_for_each_entry(sibling, &rdev->consumer_list, list) {
934 if (sibling->enable_count)
935 current_uA += sibling->uA_load;
938 current_uA += rdev->constraints->system_load;
940 if (rdev->desc->ops->set_load) {
941 /* set the optimum mode for our new total regulator load */
942 err = rdev->desc->ops->set_load(rdev, current_uA);
943 if (err < 0)
944 rdev_err(rdev, "failed to set load %d\n", current_uA);
945 } else {
946 /* get output voltage */
947 output_uV = regulator_get_voltage_rdev(rdev);
948 if (output_uV <= 0) {
949 rdev_err(rdev, "invalid output voltage found\n");
950 return -EINVAL;
953 /* get input voltage */
954 input_uV = 0;
955 if (rdev->supply)
956 input_uV = regulator_get_voltage(rdev->supply);
957 if (input_uV <= 0)
958 input_uV = rdev->constraints->input_uV;
959 if (input_uV <= 0) {
960 rdev_err(rdev, "invalid input voltage found\n");
961 return -EINVAL;
964 /* now get the optimum mode for our new total regulator load */
965 mode = rdev->desc->ops->get_optimum_mode(rdev, input_uV,
966 output_uV, current_uA);
968 /* check the new mode is allowed */
969 err = regulator_mode_constrain(rdev, &mode);
970 if (err < 0) {
971 rdev_err(rdev, "failed to get optimum mode @ %d uA %d -> %d uV\n",
972 current_uA, input_uV, output_uV);
973 return err;
976 err = rdev->desc->ops->set_mode(rdev, mode);
977 if (err < 0)
978 rdev_err(rdev, "failed to set optimum mode %x\n", mode);
981 return err;
984 static int suspend_set_state(struct regulator_dev *rdev,
985 suspend_state_t state)
987 int ret = 0;
988 struct regulator_state *rstate;
990 rstate = regulator_get_suspend_state(rdev, state);
991 if (rstate == NULL)
992 return 0;
994 /* If we have no suspend mode configuration don't set anything;
995 * only warn if the driver implements set_suspend_voltage or
996 * set_suspend_mode callback.
998 if (rstate->enabled != ENABLE_IN_SUSPEND &&
999 rstate->enabled != DISABLE_IN_SUSPEND) {
1000 if (rdev->desc->ops->set_suspend_voltage ||
1001 rdev->desc->ops->set_suspend_mode)
1002 rdev_warn(rdev, "No configuration\n");
1003 return 0;
1006 if (rstate->enabled == ENABLE_IN_SUSPEND &&
1007 rdev->desc->ops->set_suspend_enable)
1008 ret = rdev->desc->ops->set_suspend_enable(rdev);
1009 else if (rstate->enabled == DISABLE_IN_SUSPEND &&
1010 rdev->desc->ops->set_suspend_disable)
1011 ret = rdev->desc->ops->set_suspend_disable(rdev);
1012 else /* OK if set_suspend_enable or set_suspend_disable is NULL */
1013 ret = 0;
1015 if (ret < 0) {
1016 rdev_err(rdev, "failed to enabled/disable\n");
1017 return ret;
1020 if (rdev->desc->ops->set_suspend_voltage && rstate->uV > 0) {
1021 ret = rdev->desc->ops->set_suspend_voltage(rdev, rstate->uV);
1022 if (ret < 0) {
1023 rdev_err(rdev, "failed to set voltage\n");
1024 return ret;
1028 if (rdev->desc->ops->set_suspend_mode && rstate->mode > 0) {
1029 ret = rdev->desc->ops->set_suspend_mode(rdev, rstate->mode);
1030 if (ret < 0) {
1031 rdev_err(rdev, "failed to set mode\n");
1032 return ret;
1036 return ret;
1039 static void print_constraints(struct regulator_dev *rdev)
1041 struct regulation_constraints *constraints = rdev->constraints;
1042 char buf[160] = "";
1043 size_t len = sizeof(buf) - 1;
1044 int count = 0;
1045 int ret;
1047 if (constraints->min_uV && constraints->max_uV) {
1048 if (constraints->min_uV == constraints->max_uV)
1049 count += scnprintf(buf + count, len - count, "%d mV ",
1050 constraints->min_uV / 1000);
1051 else
1052 count += scnprintf(buf + count, len - count,
1053 "%d <--> %d mV ",
1054 constraints->min_uV / 1000,
1055 constraints->max_uV / 1000);
1058 if (!constraints->min_uV ||
1059 constraints->min_uV != constraints->max_uV) {
1060 ret = regulator_get_voltage_rdev(rdev);
1061 if (ret > 0)
1062 count += scnprintf(buf + count, len - count,
1063 "at %d mV ", ret / 1000);
1066 if (constraints->uV_offset)
1067 count += scnprintf(buf + count, len - count, "%dmV offset ",
1068 constraints->uV_offset / 1000);
1070 if (constraints->min_uA && constraints->max_uA) {
1071 if (constraints->min_uA == constraints->max_uA)
1072 count += scnprintf(buf + count, len - count, "%d mA ",
1073 constraints->min_uA / 1000);
1074 else
1075 count += scnprintf(buf + count, len - count,
1076 "%d <--> %d mA ",
1077 constraints->min_uA / 1000,
1078 constraints->max_uA / 1000);
1081 if (!constraints->min_uA ||
1082 constraints->min_uA != constraints->max_uA) {
1083 ret = _regulator_get_current_limit(rdev);
1084 if (ret > 0)
1085 count += scnprintf(buf + count, len - count,
1086 "at %d mA ", ret / 1000);
1089 if (constraints->valid_modes_mask & REGULATOR_MODE_FAST)
1090 count += scnprintf(buf + count, len - count, "fast ");
1091 if (constraints->valid_modes_mask & REGULATOR_MODE_NORMAL)
1092 count += scnprintf(buf + count, len - count, "normal ");
1093 if (constraints->valid_modes_mask & REGULATOR_MODE_IDLE)
1094 count += scnprintf(buf + count, len - count, "idle ");
1095 if (constraints->valid_modes_mask & REGULATOR_MODE_STANDBY)
1096 count += scnprintf(buf + count, len - count, "standby");
1098 if (!count)
1099 scnprintf(buf, len, "no parameters");
1101 rdev_dbg(rdev, "%s\n", buf);
1103 if ((constraints->min_uV != constraints->max_uV) &&
1104 !regulator_ops_is_valid(rdev, REGULATOR_CHANGE_VOLTAGE))
1105 rdev_warn(rdev,
1106 "Voltage range but no REGULATOR_CHANGE_VOLTAGE\n");
1109 static int machine_constraints_voltage(struct regulator_dev *rdev,
1110 struct regulation_constraints *constraints)
1112 const struct regulator_ops *ops = rdev->desc->ops;
1113 int ret;
1115 /* do we need to apply the constraint voltage */
1116 if (rdev->constraints->apply_uV &&
1117 rdev->constraints->min_uV && rdev->constraints->max_uV) {
1118 int target_min, target_max;
1119 int current_uV = regulator_get_voltage_rdev(rdev);
1121 if (current_uV == -ENOTRECOVERABLE) {
1122 /* This regulator can't be read and must be initialized */
1123 rdev_info(rdev, "Setting %d-%duV\n",
1124 rdev->constraints->min_uV,
1125 rdev->constraints->max_uV);
1126 _regulator_do_set_voltage(rdev,
1127 rdev->constraints->min_uV,
1128 rdev->constraints->max_uV);
1129 current_uV = regulator_get_voltage_rdev(rdev);
1132 if (current_uV < 0) {
1133 rdev_err(rdev,
1134 "failed to get the current voltage(%d)\n",
1135 current_uV);
1136 return current_uV;
1140 * If we're below the minimum voltage move up to the
1141 * minimum voltage, if we're above the maximum voltage
1142 * then move down to the maximum.
1144 target_min = current_uV;
1145 target_max = current_uV;
1147 if (current_uV < rdev->constraints->min_uV) {
1148 target_min = rdev->constraints->min_uV;
1149 target_max = rdev->constraints->min_uV;
1152 if (current_uV > rdev->constraints->max_uV) {
1153 target_min = rdev->constraints->max_uV;
1154 target_max = rdev->constraints->max_uV;
1157 if (target_min != current_uV || target_max != current_uV) {
1158 rdev_info(rdev, "Bringing %duV into %d-%duV\n",
1159 current_uV, target_min, target_max);
1160 ret = _regulator_do_set_voltage(
1161 rdev, target_min, target_max);
1162 if (ret < 0) {
1163 rdev_err(rdev,
1164 "failed to apply %d-%duV constraint(%d)\n",
1165 target_min, target_max, ret);
1166 return ret;
1171 /* constrain machine-level voltage specs to fit
1172 * the actual range supported by this regulator.
1174 if (ops->list_voltage && rdev->desc->n_voltages) {
1175 int count = rdev->desc->n_voltages;
1176 int i;
1177 int min_uV = INT_MAX;
1178 int max_uV = INT_MIN;
1179 int cmin = constraints->min_uV;
1180 int cmax = constraints->max_uV;
1182 /* it's safe to autoconfigure fixed-voltage supplies
1183 and the constraints are used by list_voltage. */
1184 if (count == 1 && !cmin) {
1185 cmin = 1;
1186 cmax = INT_MAX;
1187 constraints->min_uV = cmin;
1188 constraints->max_uV = cmax;
1191 /* voltage constraints are optional */
1192 if ((cmin == 0) && (cmax == 0))
1193 return 0;
1195 /* else require explicit machine-level constraints */
1196 if (cmin <= 0 || cmax <= 0 || cmax < cmin) {
1197 rdev_err(rdev, "invalid voltage constraints\n");
1198 return -EINVAL;
1201 /* initial: [cmin..cmax] valid, [min_uV..max_uV] not */
1202 for (i = 0; i < count; i++) {
1203 int value;
1205 value = ops->list_voltage(rdev, i);
1206 if (value <= 0)
1207 continue;
1209 /* maybe adjust [min_uV..max_uV] */
1210 if (value >= cmin && value < min_uV)
1211 min_uV = value;
1212 if (value <= cmax && value > max_uV)
1213 max_uV = value;
1216 /* final: [min_uV..max_uV] valid iff constraints valid */
1217 if (max_uV < min_uV) {
1218 rdev_err(rdev,
1219 "unsupportable voltage constraints %u-%uuV\n",
1220 min_uV, max_uV);
1221 return -EINVAL;
1224 /* use regulator's subset of machine constraints */
1225 if (constraints->min_uV < min_uV) {
1226 rdev_dbg(rdev, "override min_uV, %d -> %d\n",
1227 constraints->min_uV, min_uV);
1228 constraints->min_uV = min_uV;
1230 if (constraints->max_uV > max_uV) {
1231 rdev_dbg(rdev, "override max_uV, %d -> %d\n",
1232 constraints->max_uV, max_uV);
1233 constraints->max_uV = max_uV;
1237 return 0;
1240 static int machine_constraints_current(struct regulator_dev *rdev,
1241 struct regulation_constraints *constraints)
1243 const struct regulator_ops *ops = rdev->desc->ops;
1244 int ret;
1246 if (!constraints->min_uA && !constraints->max_uA)
1247 return 0;
1249 if (constraints->min_uA > constraints->max_uA) {
1250 rdev_err(rdev, "Invalid current constraints\n");
1251 return -EINVAL;
1254 if (!ops->set_current_limit || !ops->get_current_limit) {
1255 rdev_warn(rdev, "Operation of current configuration missing\n");
1256 return 0;
1259 /* Set regulator current in constraints range */
1260 ret = ops->set_current_limit(rdev, constraints->min_uA,
1261 constraints->max_uA);
1262 if (ret < 0) {
1263 rdev_err(rdev, "Failed to set current constraint, %d\n", ret);
1264 return ret;
1267 return 0;
1270 static int _regulator_do_enable(struct regulator_dev *rdev);
1273 * set_machine_constraints - sets regulator constraints
1274 * @rdev: regulator source
1275 * @constraints: constraints to apply
1277 * Allows platform initialisation code to define and constrain
1278 * regulator circuits e.g. valid voltage/current ranges, etc. NOTE:
1279 * Constraints *must* be set by platform code in order for some
1280 * regulator operations to proceed i.e. set_voltage, set_current_limit,
1281 * set_mode.
1283 static int set_machine_constraints(struct regulator_dev *rdev,
1284 const struct regulation_constraints *constraints)
1286 int ret = 0;
1287 const struct regulator_ops *ops = rdev->desc->ops;
1289 if (constraints)
1290 rdev->constraints = kmemdup(constraints, sizeof(*constraints),
1291 GFP_KERNEL);
1292 else
1293 rdev->constraints = kzalloc(sizeof(*constraints),
1294 GFP_KERNEL);
1295 if (!rdev->constraints)
1296 return -ENOMEM;
1298 ret = machine_constraints_voltage(rdev, rdev->constraints);
1299 if (ret != 0)
1300 return ret;
1302 ret = machine_constraints_current(rdev, rdev->constraints);
1303 if (ret != 0)
1304 return ret;
1306 if (rdev->constraints->ilim_uA && ops->set_input_current_limit) {
1307 ret = ops->set_input_current_limit(rdev,
1308 rdev->constraints->ilim_uA);
1309 if (ret < 0) {
1310 rdev_err(rdev, "failed to set input limit\n");
1311 return ret;
1315 /* do we need to setup our suspend state */
1316 if (rdev->constraints->initial_state) {
1317 ret = suspend_set_state(rdev, rdev->constraints->initial_state);
1318 if (ret < 0) {
1319 rdev_err(rdev, "failed to set suspend state\n");
1320 return ret;
1324 if (rdev->constraints->initial_mode) {
1325 if (!ops->set_mode) {
1326 rdev_err(rdev, "no set_mode operation\n");
1327 return -EINVAL;
1330 ret = ops->set_mode(rdev, rdev->constraints->initial_mode);
1331 if (ret < 0) {
1332 rdev_err(rdev, "failed to set initial mode: %d\n", ret);
1333 return ret;
1335 } else if (rdev->constraints->system_load) {
1337 * We'll only apply the initial system load if an
1338 * initial mode wasn't specified.
1340 drms_uA_update(rdev);
1343 if ((rdev->constraints->ramp_delay || rdev->constraints->ramp_disable)
1344 && ops->set_ramp_delay) {
1345 ret = ops->set_ramp_delay(rdev, rdev->constraints->ramp_delay);
1346 if (ret < 0) {
1347 rdev_err(rdev, "failed to set ramp_delay\n");
1348 return ret;
1352 if (rdev->constraints->pull_down && ops->set_pull_down) {
1353 ret = ops->set_pull_down(rdev);
1354 if (ret < 0) {
1355 rdev_err(rdev, "failed to set pull down\n");
1356 return ret;
1360 if (rdev->constraints->soft_start && ops->set_soft_start) {
1361 ret = ops->set_soft_start(rdev);
1362 if (ret < 0) {
1363 rdev_err(rdev, "failed to set soft start\n");
1364 return ret;
1368 if (rdev->constraints->over_current_protection
1369 && ops->set_over_current_protection) {
1370 ret = ops->set_over_current_protection(rdev);
1371 if (ret < 0) {
1372 rdev_err(rdev, "failed to set over current protection\n");
1373 return ret;
1377 if (rdev->constraints->active_discharge && ops->set_active_discharge) {
1378 bool ad_state = (rdev->constraints->active_discharge ==
1379 REGULATOR_ACTIVE_DISCHARGE_ENABLE) ? true : false;
1381 ret = ops->set_active_discharge(rdev, ad_state);
1382 if (ret < 0) {
1383 rdev_err(rdev, "failed to set active discharge\n");
1384 return ret;
1388 /* If the constraints say the regulator should be on at this point
1389 * and we have control then make sure it is enabled.
1391 if (rdev->constraints->always_on || rdev->constraints->boot_on) {
1392 if (rdev->supply) {
1393 ret = regulator_enable(rdev->supply);
1394 if (ret < 0) {
1395 _regulator_put(rdev->supply);
1396 rdev->supply = NULL;
1397 return ret;
1401 ret = _regulator_do_enable(rdev);
1402 if (ret < 0 && ret != -EINVAL) {
1403 rdev_err(rdev, "failed to enable\n");
1404 return ret;
1407 if (rdev->constraints->always_on)
1408 rdev->use_count++;
1411 print_constraints(rdev);
1412 return 0;
1416 * set_supply - set regulator supply regulator
1417 * @rdev: regulator name
1418 * @supply_rdev: supply regulator name
1420 * Called by platform initialisation code to set the supply regulator for this
1421 * regulator. This ensures that a regulators supply will also be enabled by the
1422 * core if it's child is enabled.
1424 static int set_supply(struct regulator_dev *rdev,
1425 struct regulator_dev *supply_rdev)
1427 int err;
1429 rdev_info(rdev, "supplied by %s\n", rdev_get_name(supply_rdev));
1431 if (!try_module_get(supply_rdev->owner))
1432 return -ENODEV;
1434 rdev->supply = create_regulator(supply_rdev, &rdev->dev, "SUPPLY");
1435 if (rdev->supply == NULL) {
1436 err = -ENOMEM;
1437 return err;
1439 supply_rdev->open_count++;
1441 return 0;
1445 * set_consumer_device_supply - Bind a regulator to a symbolic supply
1446 * @rdev: regulator source
1447 * @consumer_dev_name: dev_name() string for device supply applies to
1448 * @supply: symbolic name for supply
1450 * Allows platform initialisation code to map physical regulator
1451 * sources to symbolic names for supplies for use by devices. Devices
1452 * should use these symbolic names to request regulators, avoiding the
1453 * need to provide board-specific regulator names as platform data.
1455 static int set_consumer_device_supply(struct regulator_dev *rdev,
1456 const char *consumer_dev_name,
1457 const char *supply)
1459 struct regulator_map *node;
1460 int has_dev;
1462 if (supply == NULL)
1463 return -EINVAL;
1465 if (consumer_dev_name != NULL)
1466 has_dev = 1;
1467 else
1468 has_dev = 0;
1470 list_for_each_entry(node, &regulator_map_list, list) {
1471 if (node->dev_name && consumer_dev_name) {
1472 if (strcmp(node->dev_name, consumer_dev_name) != 0)
1473 continue;
1474 } else if (node->dev_name || consumer_dev_name) {
1475 continue;
1478 if (strcmp(node->supply, supply) != 0)
1479 continue;
1481 pr_debug("%s: %s/%s is '%s' supply; fail %s/%s\n",
1482 consumer_dev_name,
1483 dev_name(&node->regulator->dev),
1484 node->regulator->desc->name,
1485 supply,
1486 dev_name(&rdev->dev), rdev_get_name(rdev));
1487 return -EBUSY;
1490 node = kzalloc(sizeof(struct regulator_map), GFP_KERNEL);
1491 if (node == NULL)
1492 return -ENOMEM;
1494 node->regulator = rdev;
1495 node->supply = supply;
1497 if (has_dev) {
1498 node->dev_name = kstrdup(consumer_dev_name, GFP_KERNEL);
1499 if (node->dev_name == NULL) {
1500 kfree(node);
1501 return -ENOMEM;
1505 list_add(&node->list, &regulator_map_list);
1506 return 0;
1509 static void unset_regulator_supplies(struct regulator_dev *rdev)
1511 struct regulator_map *node, *n;
1513 list_for_each_entry_safe(node, n, &regulator_map_list, list) {
1514 if (rdev == node->regulator) {
1515 list_del(&node->list);
1516 kfree(node->dev_name);
1517 kfree(node);
1522 #ifdef CONFIG_DEBUG_FS
1523 static ssize_t constraint_flags_read_file(struct file *file,
1524 char __user *user_buf,
1525 size_t count, loff_t *ppos)
1527 const struct regulator *regulator = file->private_data;
1528 const struct regulation_constraints *c = regulator->rdev->constraints;
1529 char *buf;
1530 ssize_t ret;
1532 if (!c)
1533 return 0;
1535 buf = kmalloc(PAGE_SIZE, GFP_KERNEL);
1536 if (!buf)
1537 return -ENOMEM;
1539 ret = snprintf(buf, PAGE_SIZE,
1540 "always_on: %u\n"
1541 "boot_on: %u\n"
1542 "apply_uV: %u\n"
1543 "ramp_disable: %u\n"
1544 "soft_start: %u\n"
1545 "pull_down: %u\n"
1546 "over_current_protection: %u\n",
1547 c->always_on,
1548 c->boot_on,
1549 c->apply_uV,
1550 c->ramp_disable,
1551 c->soft_start,
1552 c->pull_down,
1553 c->over_current_protection);
1555 ret = simple_read_from_buffer(user_buf, count, ppos, buf, ret);
1556 kfree(buf);
1558 return ret;
1561 #endif
1563 static const struct file_operations constraint_flags_fops = {
1564 #ifdef CONFIG_DEBUG_FS
1565 .open = simple_open,
1566 .read = constraint_flags_read_file,
1567 .llseek = default_llseek,
1568 #endif
1571 #define REG_STR_SIZE 64
1573 static struct regulator *create_regulator(struct regulator_dev *rdev,
1574 struct device *dev,
1575 const char *supply_name)
1577 struct regulator *regulator;
1578 char buf[REG_STR_SIZE];
1579 int err, size;
1581 regulator = kzalloc(sizeof(*regulator), GFP_KERNEL);
1582 if (regulator == NULL)
1583 return NULL;
1585 regulator_lock(rdev);
1586 regulator->rdev = rdev;
1587 list_add(&regulator->list, &rdev->consumer_list);
1589 if (dev) {
1590 regulator->dev = dev;
1592 /* Add a link to the device sysfs entry */
1593 size = snprintf(buf, REG_STR_SIZE, "%s-%s",
1594 dev->kobj.name, supply_name);
1595 if (size >= REG_STR_SIZE)
1596 goto overflow_err;
1598 regulator->supply_name = kstrdup(buf, GFP_KERNEL);
1599 if (regulator->supply_name == NULL)
1600 goto overflow_err;
1602 err = sysfs_create_link_nowarn(&rdev->dev.kobj, &dev->kobj,
1603 buf);
1604 if (err) {
1605 rdev_dbg(rdev, "could not add device link %s err %d\n",
1606 dev->kobj.name, err);
1607 /* non-fatal */
1609 } else {
1610 regulator->supply_name = kstrdup_const(supply_name, GFP_KERNEL);
1611 if (regulator->supply_name == NULL)
1612 goto overflow_err;
1615 regulator->debugfs = debugfs_create_dir(regulator->supply_name,
1616 rdev->debugfs);
1617 if (!regulator->debugfs) {
1618 rdev_dbg(rdev, "Failed to create debugfs directory\n");
1619 } else {
1620 debugfs_create_u32("uA_load", 0444, regulator->debugfs,
1621 &regulator->uA_load);
1622 debugfs_create_u32("min_uV", 0444, regulator->debugfs,
1623 &regulator->voltage[PM_SUSPEND_ON].min_uV);
1624 debugfs_create_u32("max_uV", 0444, regulator->debugfs,
1625 &regulator->voltage[PM_SUSPEND_ON].max_uV);
1626 debugfs_create_file("constraint_flags", 0444,
1627 regulator->debugfs, regulator,
1628 &constraint_flags_fops);
1632 * Check now if the regulator is an always on regulator - if
1633 * it is then we don't need to do nearly so much work for
1634 * enable/disable calls.
1636 if (!regulator_ops_is_valid(rdev, REGULATOR_CHANGE_STATUS) &&
1637 _regulator_is_enabled(rdev))
1638 regulator->always_on = true;
1640 regulator_unlock(rdev);
1641 return regulator;
1642 overflow_err:
1643 list_del(&regulator->list);
1644 kfree(regulator);
1645 regulator_unlock(rdev);
1646 return NULL;
1649 static int _regulator_get_enable_time(struct regulator_dev *rdev)
1651 if (rdev->constraints && rdev->constraints->enable_time)
1652 return rdev->constraints->enable_time;
1653 if (rdev->desc->ops->enable_time)
1654 return rdev->desc->ops->enable_time(rdev);
1655 return rdev->desc->enable_time;
1658 static struct regulator_supply_alias *regulator_find_supply_alias(
1659 struct device *dev, const char *supply)
1661 struct regulator_supply_alias *map;
1663 list_for_each_entry(map, &regulator_supply_alias_list, list)
1664 if (map->src_dev == dev && strcmp(map->src_supply, supply) == 0)
1665 return map;
1667 return NULL;
1670 static void regulator_supply_alias(struct device **dev, const char **supply)
1672 struct regulator_supply_alias *map;
1674 map = regulator_find_supply_alias(*dev, *supply);
1675 if (map) {
1676 dev_dbg(*dev, "Mapping supply %s to %s,%s\n",
1677 *supply, map->alias_supply,
1678 dev_name(map->alias_dev));
1679 *dev = map->alias_dev;
1680 *supply = map->alias_supply;
1684 static int regulator_match(struct device *dev, const void *data)
1686 struct regulator_dev *r = dev_to_rdev(dev);
1688 return strcmp(rdev_get_name(r), data) == 0;
1691 static struct regulator_dev *regulator_lookup_by_name(const char *name)
1693 struct device *dev;
1695 dev = class_find_device(&regulator_class, NULL, name, regulator_match);
1697 return dev ? dev_to_rdev(dev) : NULL;
1701 * regulator_dev_lookup - lookup a regulator device.
1702 * @dev: device for regulator "consumer".
1703 * @supply: Supply name or regulator ID.
1705 * If successful, returns a struct regulator_dev that corresponds to the name
1706 * @supply and with the embedded struct device refcount incremented by one.
1707 * The refcount must be dropped by calling put_device().
1708 * On failure one of the following ERR-PTR-encoded values is returned:
1709 * -ENODEV if lookup fails permanently, -EPROBE_DEFER if lookup could succeed
1710 * in the future.
1712 static struct regulator_dev *regulator_dev_lookup(struct device *dev,
1713 const char *supply)
1715 struct regulator_dev *r = NULL;
1716 struct device_node *node;
1717 struct regulator_map *map;
1718 const char *devname = NULL;
1720 regulator_supply_alias(&dev, &supply);
1722 /* first do a dt based lookup */
1723 if (dev && dev->of_node) {
1724 node = of_get_regulator(dev, supply);
1725 if (node) {
1726 r = of_find_regulator_by_node(node);
1727 if (r)
1728 return r;
1731 * We have a node, but there is no device.
1732 * assume it has not registered yet.
1734 return ERR_PTR(-EPROBE_DEFER);
1738 /* if not found, try doing it non-dt way */
1739 if (dev)
1740 devname = dev_name(dev);
1742 mutex_lock(&regulator_list_mutex);
1743 list_for_each_entry(map, &regulator_map_list, list) {
1744 /* If the mapping has a device set up it must match */
1745 if (map->dev_name &&
1746 (!devname || strcmp(map->dev_name, devname)))
1747 continue;
1749 if (strcmp(map->supply, supply) == 0 &&
1750 get_device(&map->regulator->dev)) {
1751 r = map->regulator;
1752 break;
1755 mutex_unlock(&regulator_list_mutex);
1757 if (r)
1758 return r;
1760 r = regulator_lookup_by_name(supply);
1761 if (r)
1762 return r;
1764 return ERR_PTR(-ENODEV);
1767 static int regulator_resolve_supply(struct regulator_dev *rdev)
1769 struct regulator_dev *r;
1770 struct device *dev = rdev->dev.parent;
1771 int ret;
1773 /* No supply to resolve? */
1774 if (!rdev->supply_name)
1775 return 0;
1777 /* Supply already resolved? */
1778 if (rdev->supply)
1779 return 0;
1781 r = regulator_dev_lookup(dev, rdev->supply_name);
1782 if (IS_ERR(r)) {
1783 ret = PTR_ERR(r);
1785 /* Did the lookup explicitly defer for us? */
1786 if (ret == -EPROBE_DEFER)
1787 return ret;
1789 if (have_full_constraints()) {
1790 r = dummy_regulator_rdev;
1791 get_device(&r->dev);
1792 } else {
1793 dev_err(dev, "Failed to resolve %s-supply for %s\n",
1794 rdev->supply_name, rdev->desc->name);
1795 return -EPROBE_DEFER;
1800 * If the supply's parent device is not the same as the
1801 * regulator's parent device, then ensure the parent device
1802 * is bound before we resolve the supply, in case the parent
1803 * device get probe deferred and unregisters the supply.
1805 if (r->dev.parent && r->dev.parent != rdev->dev.parent) {
1806 if (!device_is_bound(r->dev.parent)) {
1807 put_device(&r->dev);
1808 return -EPROBE_DEFER;
1812 /* Recursively resolve the supply of the supply */
1813 ret = regulator_resolve_supply(r);
1814 if (ret < 0) {
1815 put_device(&r->dev);
1816 return ret;
1819 ret = set_supply(rdev, r);
1820 if (ret < 0) {
1821 put_device(&r->dev);
1822 return ret;
1826 * In set_machine_constraints() we may have turned this regulator on
1827 * but we couldn't propagate to the supply if it hadn't been resolved
1828 * yet. Do it now.
1830 if (rdev->use_count) {
1831 ret = regulator_enable(rdev->supply);
1832 if (ret < 0) {
1833 _regulator_put(rdev->supply);
1834 rdev->supply = NULL;
1835 return ret;
1839 return 0;
1842 /* Internal regulator request function */
1843 struct regulator *_regulator_get(struct device *dev, const char *id,
1844 enum regulator_get_type get_type)
1846 struct regulator_dev *rdev;
1847 struct regulator *regulator;
1848 const char *devname = dev ? dev_name(dev) : "deviceless";
1849 int ret;
1851 if (get_type >= MAX_GET_TYPE) {
1852 dev_err(dev, "invalid type %d in %s\n", get_type, __func__);
1853 return ERR_PTR(-EINVAL);
1856 if (id == NULL) {
1857 pr_err("get() with no identifier\n");
1858 return ERR_PTR(-EINVAL);
1861 rdev = regulator_dev_lookup(dev, id);
1862 if (IS_ERR(rdev)) {
1863 ret = PTR_ERR(rdev);
1866 * If regulator_dev_lookup() fails with error other
1867 * than -ENODEV our job here is done, we simply return it.
1869 if (ret != -ENODEV)
1870 return ERR_PTR(ret);
1872 if (!have_full_constraints()) {
1873 dev_warn(dev,
1874 "incomplete constraints, dummy supplies not allowed\n");
1875 return ERR_PTR(-ENODEV);
1878 switch (get_type) {
1879 case NORMAL_GET:
1881 * Assume that a regulator is physically present and
1882 * enabled, even if it isn't hooked up, and just
1883 * provide a dummy.
1885 dev_warn(dev,
1886 "%s supply %s not found, using dummy regulator\n",
1887 devname, id);
1888 rdev = dummy_regulator_rdev;
1889 get_device(&rdev->dev);
1890 break;
1892 case EXCLUSIVE_GET:
1893 dev_warn(dev,
1894 "dummy supplies not allowed for exclusive requests\n");
1895 /* fall through */
1897 default:
1898 return ERR_PTR(-ENODEV);
1902 if (rdev->exclusive) {
1903 regulator = ERR_PTR(-EPERM);
1904 put_device(&rdev->dev);
1905 return regulator;
1908 if (get_type == EXCLUSIVE_GET && rdev->open_count) {
1909 regulator = ERR_PTR(-EBUSY);
1910 put_device(&rdev->dev);
1911 return regulator;
1914 mutex_lock(&regulator_list_mutex);
1915 ret = (rdev->coupling_desc.n_resolved != rdev->coupling_desc.n_coupled);
1916 mutex_unlock(&regulator_list_mutex);
1918 if (ret != 0) {
1919 regulator = ERR_PTR(-EPROBE_DEFER);
1920 put_device(&rdev->dev);
1921 return regulator;
1924 ret = regulator_resolve_supply(rdev);
1925 if (ret < 0) {
1926 regulator = ERR_PTR(ret);
1927 put_device(&rdev->dev);
1928 return regulator;
1931 if (!try_module_get(rdev->owner)) {
1932 regulator = ERR_PTR(-EPROBE_DEFER);
1933 put_device(&rdev->dev);
1934 return regulator;
1937 regulator = create_regulator(rdev, dev, id);
1938 if (regulator == NULL) {
1939 regulator = ERR_PTR(-ENOMEM);
1940 module_put(rdev->owner);
1941 put_device(&rdev->dev);
1942 return regulator;
1945 rdev->open_count++;
1946 if (get_type == EXCLUSIVE_GET) {
1947 rdev->exclusive = 1;
1949 ret = _regulator_is_enabled(rdev);
1950 if (ret > 0)
1951 rdev->use_count = 1;
1952 else
1953 rdev->use_count = 0;
1956 device_link_add(dev, &rdev->dev, DL_FLAG_STATELESS);
1958 return regulator;
1962 * regulator_get - lookup and obtain a reference to a regulator.
1963 * @dev: device for regulator "consumer"
1964 * @id: Supply name or regulator ID.
1966 * Returns a struct regulator corresponding to the regulator producer,
1967 * or IS_ERR() condition containing errno.
1969 * Use of supply names configured via regulator_set_device_supply() is
1970 * strongly encouraged. It is recommended that the supply name used
1971 * should match the name used for the supply and/or the relevant
1972 * device pins in the datasheet.
1974 struct regulator *regulator_get(struct device *dev, const char *id)
1976 return _regulator_get(dev, id, NORMAL_GET);
1978 EXPORT_SYMBOL_GPL(regulator_get);
1981 * regulator_get_exclusive - obtain exclusive access to a regulator.
1982 * @dev: device for regulator "consumer"
1983 * @id: Supply name or regulator ID.
1985 * Returns a struct regulator corresponding to the regulator producer,
1986 * or IS_ERR() condition containing errno. Other consumers will be
1987 * unable to obtain this regulator while this reference is held and the
1988 * use count for the regulator will be initialised to reflect the current
1989 * state of the regulator.
1991 * This is intended for use by consumers which cannot tolerate shared
1992 * use of the regulator such as those which need to force the
1993 * regulator off for correct operation of the hardware they are
1994 * controlling.
1996 * Use of supply names configured via regulator_set_device_supply() is
1997 * strongly encouraged. It is recommended that the supply name used
1998 * should match the name used for the supply and/or the relevant
1999 * device pins in the datasheet.
2001 struct regulator *regulator_get_exclusive(struct device *dev, const char *id)
2003 return _regulator_get(dev, id, EXCLUSIVE_GET);
2005 EXPORT_SYMBOL_GPL(regulator_get_exclusive);
2008 * regulator_get_optional - obtain optional access to a regulator.
2009 * @dev: device for regulator "consumer"
2010 * @id: Supply name or regulator ID.
2012 * Returns a struct regulator corresponding to the regulator producer,
2013 * or IS_ERR() condition containing errno.
2015 * This is intended for use by consumers for devices which can have
2016 * some supplies unconnected in normal use, such as some MMC devices.
2017 * It can allow the regulator core to provide stub supplies for other
2018 * supplies requested using normal regulator_get() calls without
2019 * disrupting the operation of drivers that can handle absent
2020 * supplies.
2022 * Use of supply names configured via regulator_set_device_supply() is
2023 * strongly encouraged. It is recommended that the supply name used
2024 * should match the name used for the supply and/or the relevant
2025 * device pins in the datasheet.
2027 struct regulator *regulator_get_optional(struct device *dev, const char *id)
2029 return _regulator_get(dev, id, OPTIONAL_GET);
2031 EXPORT_SYMBOL_GPL(regulator_get_optional);
2033 /* regulator_list_mutex lock held by regulator_put() */
2034 static void _regulator_put(struct regulator *regulator)
2036 struct regulator_dev *rdev;
2038 if (IS_ERR_OR_NULL(regulator))
2039 return;
2041 lockdep_assert_held_once(&regulator_list_mutex);
2043 /* Docs say you must disable before calling regulator_put() */
2044 WARN_ON(regulator->enable_count);
2046 rdev = regulator->rdev;
2048 debugfs_remove_recursive(regulator->debugfs);
2050 if (regulator->dev) {
2051 device_link_remove(regulator->dev, &rdev->dev);
2053 /* remove any sysfs entries */
2054 sysfs_remove_link(&rdev->dev.kobj, regulator->supply_name);
2057 regulator_lock(rdev);
2058 list_del(&regulator->list);
2060 rdev->open_count--;
2061 rdev->exclusive = 0;
2062 regulator_unlock(rdev);
2064 kfree_const(regulator->supply_name);
2065 kfree(regulator);
2067 module_put(rdev->owner);
2068 put_device(&rdev->dev);
2072 * regulator_put - "free" the regulator source
2073 * @regulator: regulator source
2075 * Note: drivers must ensure that all regulator_enable calls made on this
2076 * regulator source are balanced by regulator_disable calls prior to calling
2077 * this function.
2079 void regulator_put(struct regulator *regulator)
2081 mutex_lock(&regulator_list_mutex);
2082 _regulator_put(regulator);
2083 mutex_unlock(&regulator_list_mutex);
2085 EXPORT_SYMBOL_GPL(regulator_put);
2088 * regulator_register_supply_alias - Provide device alias for supply lookup
2090 * @dev: device that will be given as the regulator "consumer"
2091 * @id: Supply name or regulator ID
2092 * @alias_dev: device that should be used to lookup the supply
2093 * @alias_id: Supply name or regulator ID that should be used to lookup the
2094 * supply
2096 * All lookups for id on dev will instead be conducted for alias_id on
2097 * alias_dev.
2099 int regulator_register_supply_alias(struct device *dev, const char *id,
2100 struct device *alias_dev,
2101 const char *alias_id)
2103 struct regulator_supply_alias *map;
2105 map = regulator_find_supply_alias(dev, id);
2106 if (map)
2107 return -EEXIST;
2109 map = kzalloc(sizeof(struct regulator_supply_alias), GFP_KERNEL);
2110 if (!map)
2111 return -ENOMEM;
2113 map->src_dev = dev;
2114 map->src_supply = id;
2115 map->alias_dev = alias_dev;
2116 map->alias_supply = alias_id;
2118 list_add(&map->list, &regulator_supply_alias_list);
2120 pr_info("Adding alias for supply %s,%s -> %s,%s\n",
2121 id, dev_name(dev), alias_id, dev_name(alias_dev));
2123 return 0;
2125 EXPORT_SYMBOL_GPL(regulator_register_supply_alias);
2128 * regulator_unregister_supply_alias - Remove device alias
2130 * @dev: device that will be given as the regulator "consumer"
2131 * @id: Supply name or regulator ID
2133 * Remove a lookup alias if one exists for id on dev.
2135 void regulator_unregister_supply_alias(struct device *dev, const char *id)
2137 struct regulator_supply_alias *map;
2139 map = regulator_find_supply_alias(dev, id);
2140 if (map) {
2141 list_del(&map->list);
2142 kfree(map);
2145 EXPORT_SYMBOL_GPL(regulator_unregister_supply_alias);
2148 * regulator_bulk_register_supply_alias - register multiple aliases
2150 * @dev: device that will be given as the regulator "consumer"
2151 * @id: List of supply names or regulator IDs
2152 * @alias_dev: device that should be used to lookup the supply
2153 * @alias_id: List of supply names or regulator IDs that should be used to
2154 * lookup the supply
2155 * @num_id: Number of aliases to register
2157 * @return 0 on success, an errno on failure.
2159 * This helper function allows drivers to register several supply
2160 * aliases in one operation. If any of the aliases cannot be
2161 * registered any aliases that were registered will be removed
2162 * before returning to the caller.
2164 int regulator_bulk_register_supply_alias(struct device *dev,
2165 const char *const *id,
2166 struct device *alias_dev,
2167 const char *const *alias_id,
2168 int num_id)
2170 int i;
2171 int ret;
2173 for (i = 0; i < num_id; ++i) {
2174 ret = regulator_register_supply_alias(dev, id[i], alias_dev,
2175 alias_id[i]);
2176 if (ret < 0)
2177 goto err;
2180 return 0;
2182 err:
2183 dev_err(dev,
2184 "Failed to create supply alias %s,%s -> %s,%s\n",
2185 id[i], dev_name(dev), alias_id[i], dev_name(alias_dev));
2187 while (--i >= 0)
2188 regulator_unregister_supply_alias(dev, id[i]);
2190 return ret;
2192 EXPORT_SYMBOL_GPL(regulator_bulk_register_supply_alias);
2195 * regulator_bulk_unregister_supply_alias - unregister multiple aliases
2197 * @dev: device that will be given as the regulator "consumer"
2198 * @id: List of supply names or regulator IDs
2199 * @num_id: Number of aliases to unregister
2201 * This helper function allows drivers to unregister several supply
2202 * aliases in one operation.
2204 void regulator_bulk_unregister_supply_alias(struct device *dev,
2205 const char *const *id,
2206 int num_id)
2208 int i;
2210 for (i = 0; i < num_id; ++i)
2211 regulator_unregister_supply_alias(dev, id[i]);
2213 EXPORT_SYMBOL_GPL(regulator_bulk_unregister_supply_alias);
2216 /* Manage enable GPIO list. Same GPIO pin can be shared among regulators */
2217 static int regulator_ena_gpio_request(struct regulator_dev *rdev,
2218 const struct regulator_config *config)
2220 struct regulator_enable_gpio *pin;
2221 struct gpio_desc *gpiod;
2223 gpiod = config->ena_gpiod;
2225 list_for_each_entry(pin, &regulator_ena_gpio_list, list) {
2226 if (pin->gpiod == gpiod) {
2227 rdev_dbg(rdev, "GPIO is already used\n");
2228 goto update_ena_gpio_to_rdev;
2232 pin = kzalloc(sizeof(struct regulator_enable_gpio), GFP_KERNEL);
2233 if (pin == NULL)
2234 return -ENOMEM;
2236 pin->gpiod = gpiod;
2237 list_add(&pin->list, &regulator_ena_gpio_list);
2239 update_ena_gpio_to_rdev:
2240 pin->request_count++;
2241 rdev->ena_pin = pin;
2242 return 0;
2245 static void regulator_ena_gpio_free(struct regulator_dev *rdev)
2247 struct regulator_enable_gpio *pin, *n;
2249 if (!rdev->ena_pin)
2250 return;
2252 /* Free the GPIO only in case of no use */
2253 list_for_each_entry_safe(pin, n, &regulator_ena_gpio_list, list) {
2254 if (pin->gpiod == rdev->ena_pin->gpiod) {
2255 if (pin->request_count <= 1) {
2256 pin->request_count = 0;
2257 gpiod_put(pin->gpiod);
2258 list_del(&pin->list);
2259 kfree(pin);
2260 rdev->ena_pin = NULL;
2261 return;
2262 } else {
2263 pin->request_count--;
2270 * regulator_ena_gpio_ctrl - balance enable_count of each GPIO and actual GPIO pin control
2271 * @rdev: regulator_dev structure
2272 * @enable: enable GPIO at initial use?
2274 * GPIO is enabled in case of initial use. (enable_count is 0)
2275 * GPIO is disabled when it is not shared any more. (enable_count <= 1)
2277 static int regulator_ena_gpio_ctrl(struct regulator_dev *rdev, bool enable)
2279 struct regulator_enable_gpio *pin = rdev->ena_pin;
2281 if (!pin)
2282 return -EINVAL;
2284 if (enable) {
2285 /* Enable GPIO at initial use */
2286 if (pin->enable_count == 0)
2287 gpiod_set_value_cansleep(pin->gpiod, 1);
2289 pin->enable_count++;
2290 } else {
2291 if (pin->enable_count > 1) {
2292 pin->enable_count--;
2293 return 0;
2296 /* Disable GPIO if not used */
2297 if (pin->enable_count <= 1) {
2298 gpiod_set_value_cansleep(pin->gpiod, 0);
2299 pin->enable_count = 0;
2303 return 0;
2307 * _regulator_enable_delay - a delay helper function
2308 * @delay: time to delay in microseconds
2310 * Delay for the requested amount of time as per the guidelines in:
2312 * Documentation/timers/timers-howto.rst
2314 * The assumption here is that regulators will never be enabled in
2315 * atomic context and therefore sleeping functions can be used.
2317 static void _regulator_enable_delay(unsigned int delay)
2319 unsigned int ms = delay / 1000;
2320 unsigned int us = delay % 1000;
2322 if (ms > 0) {
2324 * For small enough values, handle super-millisecond
2325 * delays in the usleep_range() call below.
2327 if (ms < 20)
2328 us += ms * 1000;
2329 else
2330 msleep(ms);
2334 * Give the scheduler some room to coalesce with any other
2335 * wakeup sources. For delays shorter than 10 us, don't even
2336 * bother setting up high-resolution timers and just busy-
2337 * loop.
2339 if (us >= 10)
2340 usleep_range(us, us + 100);
2341 else
2342 udelay(us);
2345 static int _regulator_do_enable(struct regulator_dev *rdev)
2347 int ret, delay;
2349 /* Query before enabling in case configuration dependent. */
2350 ret = _regulator_get_enable_time(rdev);
2351 if (ret >= 0) {
2352 delay = ret;
2353 } else {
2354 rdev_warn(rdev, "enable_time() failed: %d\n", ret);
2355 delay = 0;
2358 trace_regulator_enable(rdev_get_name(rdev));
2360 if (rdev->desc->off_on_delay) {
2361 /* if needed, keep a distance of off_on_delay from last time
2362 * this regulator was disabled.
2364 unsigned long start_jiffy = jiffies;
2365 unsigned long intended, max_delay, remaining;
2367 max_delay = usecs_to_jiffies(rdev->desc->off_on_delay);
2368 intended = rdev->last_off_jiffy + max_delay;
2370 if (time_before(start_jiffy, intended)) {
2371 /* calc remaining jiffies to deal with one-time
2372 * timer wrapping.
2373 * in case of multiple timer wrapping, either it can be
2374 * detected by out-of-range remaining, or it cannot be
2375 * detected and we get a penalty of
2376 * _regulator_enable_delay().
2378 remaining = intended - start_jiffy;
2379 if (remaining <= max_delay)
2380 _regulator_enable_delay(
2381 jiffies_to_usecs(remaining));
2385 if (rdev->ena_pin) {
2386 if (!rdev->ena_gpio_state) {
2387 ret = regulator_ena_gpio_ctrl(rdev, true);
2388 if (ret < 0)
2389 return ret;
2390 rdev->ena_gpio_state = 1;
2392 } else if (rdev->desc->ops->enable) {
2393 ret = rdev->desc->ops->enable(rdev);
2394 if (ret < 0)
2395 return ret;
2396 } else {
2397 return -EINVAL;
2400 /* Allow the regulator to ramp; it would be useful to extend
2401 * this for bulk operations so that the regulators can ramp
2402 * together. */
2403 trace_regulator_enable_delay(rdev_get_name(rdev));
2405 _regulator_enable_delay(delay);
2407 trace_regulator_enable_complete(rdev_get_name(rdev));
2409 return 0;
2413 * _regulator_handle_consumer_enable - handle that a consumer enabled
2414 * @regulator: regulator source
2416 * Some things on a regulator consumer (like the contribution towards total
2417 * load on the regulator) only have an effect when the consumer wants the
2418 * regulator enabled. Explained in example with two consumers of the same
2419 * regulator:
2420 * consumer A: set_load(100); => total load = 0
2421 * consumer A: regulator_enable(); => total load = 100
2422 * consumer B: set_load(1000); => total load = 100
2423 * consumer B: regulator_enable(); => total load = 1100
2424 * consumer A: regulator_disable(); => total_load = 1000
2426 * This function (together with _regulator_handle_consumer_disable) is
2427 * responsible for keeping track of the refcount for a given regulator consumer
2428 * and applying / unapplying these things.
2430 * Returns 0 upon no error; -error upon error.
2432 static int _regulator_handle_consumer_enable(struct regulator *regulator)
2434 struct regulator_dev *rdev = regulator->rdev;
2436 lockdep_assert_held_once(&rdev->mutex.base);
2438 regulator->enable_count++;
2439 if (regulator->uA_load && regulator->enable_count == 1)
2440 return drms_uA_update(rdev);
2442 return 0;
2446 * _regulator_handle_consumer_disable - handle that a consumer disabled
2447 * @regulator: regulator source
2449 * The opposite of _regulator_handle_consumer_enable().
2451 * Returns 0 upon no error; -error upon error.
2453 static int _regulator_handle_consumer_disable(struct regulator *regulator)
2455 struct regulator_dev *rdev = regulator->rdev;
2457 lockdep_assert_held_once(&rdev->mutex.base);
2459 if (!regulator->enable_count) {
2460 rdev_err(rdev, "Underflow of regulator enable count\n");
2461 return -EINVAL;
2464 regulator->enable_count--;
2465 if (regulator->uA_load && regulator->enable_count == 0)
2466 return drms_uA_update(rdev);
2468 return 0;
2471 /* locks held by regulator_enable() */
2472 static int _regulator_enable(struct regulator *regulator)
2474 struct regulator_dev *rdev = regulator->rdev;
2475 int ret;
2477 lockdep_assert_held_once(&rdev->mutex.base);
2479 if (rdev->use_count == 0 && rdev->supply) {
2480 ret = _regulator_enable(rdev->supply);
2481 if (ret < 0)
2482 return ret;
2485 /* balance only if there are regulators coupled */
2486 if (rdev->coupling_desc.n_coupled > 1) {
2487 ret = regulator_balance_voltage(rdev, PM_SUSPEND_ON);
2488 if (ret < 0)
2489 goto err_disable_supply;
2492 ret = _regulator_handle_consumer_enable(regulator);
2493 if (ret < 0)
2494 goto err_disable_supply;
2496 if (rdev->use_count == 0) {
2497 /* The regulator may on if it's not switchable or left on */
2498 ret = _regulator_is_enabled(rdev);
2499 if (ret == -EINVAL || ret == 0) {
2500 if (!regulator_ops_is_valid(rdev,
2501 REGULATOR_CHANGE_STATUS)) {
2502 ret = -EPERM;
2503 goto err_consumer_disable;
2506 ret = _regulator_do_enable(rdev);
2507 if (ret < 0)
2508 goto err_consumer_disable;
2510 _notifier_call_chain(rdev, REGULATOR_EVENT_ENABLE,
2511 NULL);
2512 } else if (ret < 0) {
2513 rdev_err(rdev, "is_enabled() failed: %d\n", ret);
2514 goto err_consumer_disable;
2516 /* Fallthrough on positive return values - already enabled */
2519 rdev->use_count++;
2521 return 0;
2523 err_consumer_disable:
2524 _regulator_handle_consumer_disable(regulator);
2526 err_disable_supply:
2527 if (rdev->use_count == 0 && rdev->supply)
2528 _regulator_disable(rdev->supply);
2530 return ret;
2534 * regulator_enable - enable regulator output
2535 * @regulator: regulator source
2537 * Request that the regulator be enabled with the regulator output at
2538 * the predefined voltage or current value. Calls to regulator_enable()
2539 * must be balanced with calls to regulator_disable().
2541 * NOTE: the output value can be set by other drivers, boot loader or may be
2542 * hardwired in the regulator.
2544 int regulator_enable(struct regulator *regulator)
2546 struct regulator_dev *rdev = regulator->rdev;
2547 struct ww_acquire_ctx ww_ctx;
2548 int ret;
2550 regulator_lock_dependent(rdev, &ww_ctx);
2551 ret = _regulator_enable(regulator);
2552 regulator_unlock_dependent(rdev, &ww_ctx);
2554 return ret;
2556 EXPORT_SYMBOL_GPL(regulator_enable);
2558 static int _regulator_do_disable(struct regulator_dev *rdev)
2560 int ret;
2562 trace_regulator_disable(rdev_get_name(rdev));
2564 if (rdev->ena_pin) {
2565 if (rdev->ena_gpio_state) {
2566 ret = regulator_ena_gpio_ctrl(rdev, false);
2567 if (ret < 0)
2568 return ret;
2569 rdev->ena_gpio_state = 0;
2572 } else if (rdev->desc->ops->disable) {
2573 ret = rdev->desc->ops->disable(rdev);
2574 if (ret != 0)
2575 return ret;
2578 /* cares about last_off_jiffy only if off_on_delay is required by
2579 * device.
2581 if (rdev->desc->off_on_delay)
2582 rdev->last_off_jiffy = jiffies;
2584 trace_regulator_disable_complete(rdev_get_name(rdev));
2586 return 0;
2589 /* locks held by regulator_disable() */
2590 static int _regulator_disable(struct regulator *regulator)
2592 struct regulator_dev *rdev = regulator->rdev;
2593 int ret = 0;
2595 lockdep_assert_held_once(&rdev->mutex.base);
2597 if (WARN(rdev->use_count <= 0,
2598 "unbalanced disables for %s\n", rdev_get_name(rdev)))
2599 return -EIO;
2601 /* are we the last user and permitted to disable ? */
2602 if (rdev->use_count == 1 &&
2603 (rdev->constraints && !rdev->constraints->always_on)) {
2605 /* we are last user */
2606 if (regulator_ops_is_valid(rdev, REGULATOR_CHANGE_STATUS)) {
2607 ret = _notifier_call_chain(rdev,
2608 REGULATOR_EVENT_PRE_DISABLE,
2609 NULL);
2610 if (ret & NOTIFY_STOP_MASK)
2611 return -EINVAL;
2613 ret = _regulator_do_disable(rdev);
2614 if (ret < 0) {
2615 rdev_err(rdev, "failed to disable\n");
2616 _notifier_call_chain(rdev,
2617 REGULATOR_EVENT_ABORT_DISABLE,
2618 NULL);
2619 return ret;
2621 _notifier_call_chain(rdev, REGULATOR_EVENT_DISABLE,
2622 NULL);
2625 rdev->use_count = 0;
2626 } else if (rdev->use_count > 1) {
2627 rdev->use_count--;
2630 if (ret == 0)
2631 ret = _regulator_handle_consumer_disable(regulator);
2633 if (ret == 0 && rdev->coupling_desc.n_coupled > 1)
2634 ret = regulator_balance_voltage(rdev, PM_SUSPEND_ON);
2636 if (ret == 0 && rdev->use_count == 0 && rdev->supply)
2637 ret = _regulator_disable(rdev->supply);
2639 return ret;
2643 * regulator_disable - disable regulator output
2644 * @regulator: regulator source
2646 * Disable the regulator output voltage or current. Calls to
2647 * regulator_enable() must be balanced with calls to
2648 * regulator_disable().
2650 * NOTE: this will only disable the regulator output if no other consumer
2651 * devices have it enabled, the regulator device supports disabling and
2652 * machine constraints permit this operation.
2654 int regulator_disable(struct regulator *regulator)
2656 struct regulator_dev *rdev = regulator->rdev;
2657 struct ww_acquire_ctx ww_ctx;
2658 int ret;
2660 regulator_lock_dependent(rdev, &ww_ctx);
2661 ret = _regulator_disable(regulator);
2662 regulator_unlock_dependent(rdev, &ww_ctx);
2664 return ret;
2666 EXPORT_SYMBOL_GPL(regulator_disable);
2668 /* locks held by regulator_force_disable() */
2669 static int _regulator_force_disable(struct regulator_dev *rdev)
2671 int ret = 0;
2673 lockdep_assert_held_once(&rdev->mutex.base);
2675 ret = _notifier_call_chain(rdev, REGULATOR_EVENT_FORCE_DISABLE |
2676 REGULATOR_EVENT_PRE_DISABLE, NULL);
2677 if (ret & NOTIFY_STOP_MASK)
2678 return -EINVAL;
2680 ret = _regulator_do_disable(rdev);
2681 if (ret < 0) {
2682 rdev_err(rdev, "failed to force disable\n");
2683 _notifier_call_chain(rdev, REGULATOR_EVENT_FORCE_DISABLE |
2684 REGULATOR_EVENT_ABORT_DISABLE, NULL);
2685 return ret;
2688 _notifier_call_chain(rdev, REGULATOR_EVENT_FORCE_DISABLE |
2689 REGULATOR_EVENT_DISABLE, NULL);
2691 return 0;
2695 * regulator_force_disable - force disable regulator output
2696 * @regulator: regulator source
2698 * Forcibly disable the regulator output voltage or current.
2699 * NOTE: this *will* disable the regulator output even if other consumer
2700 * devices have it enabled. This should be used for situations when device
2701 * damage will likely occur if the regulator is not disabled (e.g. over temp).
2703 int regulator_force_disable(struct regulator *regulator)
2705 struct regulator_dev *rdev = regulator->rdev;
2706 struct ww_acquire_ctx ww_ctx;
2707 int ret;
2709 regulator_lock_dependent(rdev, &ww_ctx);
2711 ret = _regulator_force_disable(regulator->rdev);
2713 if (rdev->coupling_desc.n_coupled > 1)
2714 regulator_balance_voltage(rdev, PM_SUSPEND_ON);
2716 if (regulator->uA_load) {
2717 regulator->uA_load = 0;
2718 ret = drms_uA_update(rdev);
2721 if (rdev->use_count != 0 && rdev->supply)
2722 _regulator_disable(rdev->supply);
2724 regulator_unlock_dependent(rdev, &ww_ctx);
2726 return ret;
2728 EXPORT_SYMBOL_GPL(regulator_force_disable);
2730 static void regulator_disable_work(struct work_struct *work)
2732 struct regulator_dev *rdev = container_of(work, struct regulator_dev,
2733 disable_work.work);
2734 struct ww_acquire_ctx ww_ctx;
2735 int count, i, ret;
2736 struct regulator *regulator;
2737 int total_count = 0;
2739 regulator_lock_dependent(rdev, &ww_ctx);
2742 * Workqueue functions queue the new work instance while the previous
2743 * work instance is being processed. Cancel the queued work instance
2744 * as the work instance under processing does the job of the queued
2745 * work instance.
2747 cancel_delayed_work(&rdev->disable_work);
2749 list_for_each_entry(regulator, &rdev->consumer_list, list) {
2750 count = regulator->deferred_disables;
2752 if (!count)
2753 continue;
2755 total_count += count;
2756 regulator->deferred_disables = 0;
2758 for (i = 0; i < count; i++) {
2759 ret = _regulator_disable(regulator);
2760 if (ret != 0)
2761 rdev_err(rdev, "Deferred disable failed: %d\n", ret);
2764 WARN_ON(!total_count);
2766 if (rdev->coupling_desc.n_coupled > 1)
2767 regulator_balance_voltage(rdev, PM_SUSPEND_ON);
2769 regulator_unlock_dependent(rdev, &ww_ctx);
2773 * regulator_disable_deferred - disable regulator output with delay
2774 * @regulator: regulator source
2775 * @ms: milliseconds until the regulator is disabled
2777 * Execute regulator_disable() on the regulator after a delay. This
2778 * is intended for use with devices that require some time to quiesce.
2780 * NOTE: this will only disable the regulator output if no other consumer
2781 * devices have it enabled, the regulator device supports disabling and
2782 * machine constraints permit this operation.
2784 int regulator_disable_deferred(struct regulator *regulator, int ms)
2786 struct regulator_dev *rdev = regulator->rdev;
2788 if (!ms)
2789 return regulator_disable(regulator);
2791 regulator_lock(rdev);
2792 regulator->deferred_disables++;
2793 mod_delayed_work(system_power_efficient_wq, &rdev->disable_work,
2794 msecs_to_jiffies(ms));
2795 regulator_unlock(rdev);
2797 return 0;
2799 EXPORT_SYMBOL_GPL(regulator_disable_deferred);
2801 static int _regulator_is_enabled(struct regulator_dev *rdev)
2803 /* A GPIO control always takes precedence */
2804 if (rdev->ena_pin)
2805 return rdev->ena_gpio_state;
2807 /* If we don't know then assume that the regulator is always on */
2808 if (!rdev->desc->ops->is_enabled)
2809 return 1;
2811 return rdev->desc->ops->is_enabled(rdev);
2814 static int _regulator_list_voltage(struct regulator_dev *rdev,
2815 unsigned selector, int lock)
2817 const struct regulator_ops *ops = rdev->desc->ops;
2818 int ret;
2820 if (rdev->desc->fixed_uV && rdev->desc->n_voltages == 1 && !selector)
2821 return rdev->desc->fixed_uV;
2823 if (ops->list_voltage) {
2824 if (selector >= rdev->desc->n_voltages)
2825 return -EINVAL;
2826 if (lock)
2827 regulator_lock(rdev);
2828 ret = ops->list_voltage(rdev, selector);
2829 if (lock)
2830 regulator_unlock(rdev);
2831 } else if (rdev->is_switch && rdev->supply) {
2832 ret = _regulator_list_voltage(rdev->supply->rdev,
2833 selector, lock);
2834 } else {
2835 return -EINVAL;
2838 if (ret > 0) {
2839 if (ret < rdev->constraints->min_uV)
2840 ret = 0;
2841 else if (ret > rdev->constraints->max_uV)
2842 ret = 0;
2845 return ret;
2849 * regulator_is_enabled - is the regulator output enabled
2850 * @regulator: regulator source
2852 * Returns positive if the regulator driver backing the source/client
2853 * has requested that the device be enabled, zero if it hasn't, else a
2854 * negative errno code.
2856 * Note that the device backing this regulator handle can have multiple
2857 * users, so it might be enabled even if regulator_enable() was never
2858 * called for this particular source.
2860 int regulator_is_enabled(struct regulator *regulator)
2862 int ret;
2864 if (regulator->always_on)
2865 return 1;
2867 regulator_lock(regulator->rdev);
2868 ret = _regulator_is_enabled(regulator->rdev);
2869 regulator_unlock(regulator->rdev);
2871 return ret;
2873 EXPORT_SYMBOL_GPL(regulator_is_enabled);
2876 * regulator_count_voltages - count regulator_list_voltage() selectors
2877 * @regulator: regulator source
2879 * Returns number of selectors, or negative errno. Selectors are
2880 * numbered starting at zero, and typically correspond to bitfields
2881 * in hardware registers.
2883 int regulator_count_voltages(struct regulator *regulator)
2885 struct regulator_dev *rdev = regulator->rdev;
2887 if (rdev->desc->n_voltages)
2888 return rdev->desc->n_voltages;
2890 if (!rdev->is_switch || !rdev->supply)
2891 return -EINVAL;
2893 return regulator_count_voltages(rdev->supply);
2895 EXPORT_SYMBOL_GPL(regulator_count_voltages);
2898 * regulator_list_voltage - enumerate supported voltages
2899 * @regulator: regulator source
2900 * @selector: identify voltage to list
2901 * Context: can sleep
2903 * Returns a voltage that can be passed to @regulator_set_voltage(),
2904 * zero if this selector code can't be used on this system, or a
2905 * negative errno.
2907 int regulator_list_voltage(struct regulator *regulator, unsigned selector)
2909 return _regulator_list_voltage(regulator->rdev, selector, 1);
2911 EXPORT_SYMBOL_GPL(regulator_list_voltage);
2914 * regulator_get_regmap - get the regulator's register map
2915 * @regulator: regulator source
2917 * Returns the register map for the given regulator, or an ERR_PTR value
2918 * if the regulator doesn't use regmap.
2920 struct regmap *regulator_get_regmap(struct regulator *regulator)
2922 struct regmap *map = regulator->rdev->regmap;
2924 return map ? map : ERR_PTR(-EOPNOTSUPP);
2928 * regulator_get_hardware_vsel_register - get the HW voltage selector register
2929 * @regulator: regulator source
2930 * @vsel_reg: voltage selector register, output parameter
2931 * @vsel_mask: mask for voltage selector bitfield, output parameter
2933 * Returns the hardware register offset and bitmask used for setting the
2934 * regulator voltage. This might be useful when configuring voltage-scaling
2935 * hardware or firmware that can make I2C requests behind the kernel's back,
2936 * for example.
2938 * On success, the output parameters @vsel_reg and @vsel_mask are filled in
2939 * and 0 is returned, otherwise a negative errno is returned.
2941 int regulator_get_hardware_vsel_register(struct regulator *regulator,
2942 unsigned *vsel_reg,
2943 unsigned *vsel_mask)
2945 struct regulator_dev *rdev = regulator->rdev;
2946 const struct regulator_ops *ops = rdev->desc->ops;
2948 if (ops->set_voltage_sel != regulator_set_voltage_sel_regmap)
2949 return -EOPNOTSUPP;
2951 *vsel_reg = rdev->desc->vsel_reg;
2952 *vsel_mask = rdev->desc->vsel_mask;
2954 return 0;
2956 EXPORT_SYMBOL_GPL(regulator_get_hardware_vsel_register);
2959 * regulator_list_hardware_vsel - get the HW-specific register value for a selector
2960 * @regulator: regulator source
2961 * @selector: identify voltage to list
2963 * Converts the selector to a hardware-specific voltage selector that can be
2964 * directly written to the regulator registers. The address of the voltage
2965 * register can be determined by calling @regulator_get_hardware_vsel_register.
2967 * On error a negative errno is returned.
2969 int regulator_list_hardware_vsel(struct regulator *regulator,
2970 unsigned selector)
2972 struct regulator_dev *rdev = regulator->rdev;
2973 const struct regulator_ops *ops = rdev->desc->ops;
2975 if (selector >= rdev->desc->n_voltages)
2976 return -EINVAL;
2977 if (ops->set_voltage_sel != regulator_set_voltage_sel_regmap)
2978 return -EOPNOTSUPP;
2980 return selector;
2982 EXPORT_SYMBOL_GPL(regulator_list_hardware_vsel);
2985 * regulator_get_linear_step - return the voltage step size between VSEL values
2986 * @regulator: regulator source
2988 * Returns the voltage step size between VSEL values for linear
2989 * regulators, or return 0 if the regulator isn't a linear regulator.
2991 unsigned int regulator_get_linear_step(struct regulator *regulator)
2993 struct regulator_dev *rdev = regulator->rdev;
2995 return rdev->desc->uV_step;
2997 EXPORT_SYMBOL_GPL(regulator_get_linear_step);
3000 * regulator_is_supported_voltage - check if a voltage range can be supported
3002 * @regulator: Regulator to check.
3003 * @min_uV: Minimum required voltage in uV.
3004 * @max_uV: Maximum required voltage in uV.
3006 * Returns a boolean.
3008 int regulator_is_supported_voltage(struct regulator *regulator,
3009 int min_uV, int max_uV)
3011 struct regulator_dev *rdev = regulator->rdev;
3012 int i, voltages, ret;
3014 /* If we can't change voltage check the current voltage */
3015 if (!regulator_ops_is_valid(rdev, REGULATOR_CHANGE_VOLTAGE)) {
3016 ret = regulator_get_voltage(regulator);
3017 if (ret >= 0)
3018 return min_uV <= ret && ret <= max_uV;
3019 else
3020 return ret;
3023 /* Any voltage within constrains range is fine? */
3024 if (rdev->desc->continuous_voltage_range)
3025 return min_uV >= rdev->constraints->min_uV &&
3026 max_uV <= rdev->constraints->max_uV;
3028 ret = regulator_count_voltages(regulator);
3029 if (ret < 0)
3030 return 0;
3031 voltages = ret;
3033 for (i = 0; i < voltages; i++) {
3034 ret = regulator_list_voltage(regulator, i);
3036 if (ret >= min_uV && ret <= max_uV)
3037 return 1;
3040 return 0;
3042 EXPORT_SYMBOL_GPL(regulator_is_supported_voltage);
3044 static int regulator_map_voltage(struct regulator_dev *rdev, int min_uV,
3045 int max_uV)
3047 const struct regulator_desc *desc = rdev->desc;
3049 if (desc->ops->map_voltage)
3050 return desc->ops->map_voltage(rdev, min_uV, max_uV);
3052 if (desc->ops->list_voltage == regulator_list_voltage_linear)
3053 return regulator_map_voltage_linear(rdev, min_uV, max_uV);
3055 if (desc->ops->list_voltage == regulator_list_voltage_linear_range)
3056 return regulator_map_voltage_linear_range(rdev, min_uV, max_uV);
3058 if (desc->ops->list_voltage ==
3059 regulator_list_voltage_pickable_linear_range)
3060 return regulator_map_voltage_pickable_linear_range(rdev,
3061 min_uV, max_uV);
3063 return regulator_map_voltage_iterate(rdev, min_uV, max_uV);
3066 static int _regulator_call_set_voltage(struct regulator_dev *rdev,
3067 int min_uV, int max_uV,
3068 unsigned *selector)
3070 struct pre_voltage_change_data data;
3071 int ret;
3073 data.old_uV = regulator_get_voltage_rdev(rdev);
3074 data.min_uV = min_uV;
3075 data.max_uV = max_uV;
3076 ret = _notifier_call_chain(rdev, REGULATOR_EVENT_PRE_VOLTAGE_CHANGE,
3077 &data);
3078 if (ret & NOTIFY_STOP_MASK)
3079 return -EINVAL;
3081 ret = rdev->desc->ops->set_voltage(rdev, min_uV, max_uV, selector);
3082 if (ret >= 0)
3083 return ret;
3085 _notifier_call_chain(rdev, REGULATOR_EVENT_ABORT_VOLTAGE_CHANGE,
3086 (void *)data.old_uV);
3088 return ret;
3091 static int _regulator_call_set_voltage_sel(struct regulator_dev *rdev,
3092 int uV, unsigned selector)
3094 struct pre_voltage_change_data data;
3095 int ret;
3097 data.old_uV = regulator_get_voltage_rdev(rdev);
3098 data.min_uV = uV;
3099 data.max_uV = uV;
3100 ret = _notifier_call_chain(rdev, REGULATOR_EVENT_PRE_VOLTAGE_CHANGE,
3101 &data);
3102 if (ret & NOTIFY_STOP_MASK)
3103 return -EINVAL;
3105 ret = rdev->desc->ops->set_voltage_sel(rdev, selector);
3106 if (ret >= 0)
3107 return ret;
3109 _notifier_call_chain(rdev, REGULATOR_EVENT_ABORT_VOLTAGE_CHANGE,
3110 (void *)data.old_uV);
3112 return ret;
3115 static int _regulator_set_voltage_sel_step(struct regulator_dev *rdev,
3116 int uV, int new_selector)
3118 const struct regulator_ops *ops = rdev->desc->ops;
3119 int diff, old_sel, curr_sel, ret;
3121 /* Stepping is only needed if the regulator is enabled. */
3122 if (!_regulator_is_enabled(rdev))
3123 goto final_set;
3125 if (!ops->get_voltage_sel)
3126 return -EINVAL;
3128 old_sel = ops->get_voltage_sel(rdev);
3129 if (old_sel < 0)
3130 return old_sel;
3132 diff = new_selector - old_sel;
3133 if (diff == 0)
3134 return 0; /* No change needed. */
3136 if (diff > 0) {
3137 /* Stepping up. */
3138 for (curr_sel = old_sel + rdev->desc->vsel_step;
3139 curr_sel < new_selector;
3140 curr_sel += rdev->desc->vsel_step) {
3142 * Call the callback directly instead of using
3143 * _regulator_call_set_voltage_sel() as we don't
3144 * want to notify anyone yet. Same in the branch
3145 * below.
3147 ret = ops->set_voltage_sel(rdev, curr_sel);
3148 if (ret)
3149 goto try_revert;
3151 } else {
3152 /* Stepping down. */
3153 for (curr_sel = old_sel - rdev->desc->vsel_step;
3154 curr_sel > new_selector;
3155 curr_sel -= rdev->desc->vsel_step) {
3156 ret = ops->set_voltage_sel(rdev, curr_sel);
3157 if (ret)
3158 goto try_revert;
3162 final_set:
3163 /* The final selector will trigger the notifiers. */
3164 return _regulator_call_set_voltage_sel(rdev, uV, new_selector);
3166 try_revert:
3168 * At least try to return to the previous voltage if setting a new
3169 * one failed.
3171 (void)ops->set_voltage_sel(rdev, old_sel);
3172 return ret;
3175 static int _regulator_set_voltage_time(struct regulator_dev *rdev,
3176 int old_uV, int new_uV)
3178 unsigned int ramp_delay = 0;
3180 if (rdev->constraints->ramp_delay)
3181 ramp_delay = rdev->constraints->ramp_delay;
3182 else if (rdev->desc->ramp_delay)
3183 ramp_delay = rdev->desc->ramp_delay;
3184 else if (rdev->constraints->settling_time)
3185 return rdev->constraints->settling_time;
3186 else if (rdev->constraints->settling_time_up &&
3187 (new_uV > old_uV))
3188 return rdev->constraints->settling_time_up;
3189 else if (rdev->constraints->settling_time_down &&
3190 (new_uV < old_uV))
3191 return rdev->constraints->settling_time_down;
3193 if (ramp_delay == 0) {
3194 rdev_dbg(rdev, "ramp_delay not set\n");
3195 return 0;
3198 return DIV_ROUND_UP(abs(new_uV - old_uV), ramp_delay);
3201 static int _regulator_do_set_voltage(struct regulator_dev *rdev,
3202 int min_uV, int max_uV)
3204 int ret;
3205 int delay = 0;
3206 int best_val = 0;
3207 unsigned int selector;
3208 int old_selector = -1;
3209 const struct regulator_ops *ops = rdev->desc->ops;
3210 int old_uV = regulator_get_voltage_rdev(rdev);
3212 trace_regulator_set_voltage(rdev_get_name(rdev), min_uV, max_uV);
3214 min_uV += rdev->constraints->uV_offset;
3215 max_uV += rdev->constraints->uV_offset;
3218 * If we can't obtain the old selector there is not enough
3219 * info to call set_voltage_time_sel().
3221 if (_regulator_is_enabled(rdev) &&
3222 ops->set_voltage_time_sel && ops->get_voltage_sel) {
3223 old_selector = ops->get_voltage_sel(rdev);
3224 if (old_selector < 0)
3225 return old_selector;
3228 if (ops->set_voltage) {
3229 ret = _regulator_call_set_voltage(rdev, min_uV, max_uV,
3230 &selector);
3232 if (ret >= 0) {
3233 if (ops->list_voltage)
3234 best_val = ops->list_voltage(rdev,
3235 selector);
3236 else
3237 best_val = regulator_get_voltage_rdev(rdev);
3240 } else if (ops->set_voltage_sel) {
3241 ret = regulator_map_voltage(rdev, min_uV, max_uV);
3242 if (ret >= 0) {
3243 best_val = ops->list_voltage(rdev, ret);
3244 if (min_uV <= best_val && max_uV >= best_val) {
3245 selector = ret;
3246 if (old_selector == selector)
3247 ret = 0;
3248 else if (rdev->desc->vsel_step)
3249 ret = _regulator_set_voltage_sel_step(
3250 rdev, best_val, selector);
3251 else
3252 ret = _regulator_call_set_voltage_sel(
3253 rdev, best_val, selector);
3254 } else {
3255 ret = -EINVAL;
3258 } else {
3259 ret = -EINVAL;
3262 if (ret)
3263 goto out;
3265 if (ops->set_voltage_time_sel) {
3267 * Call set_voltage_time_sel if successfully obtained
3268 * old_selector
3270 if (old_selector >= 0 && old_selector != selector)
3271 delay = ops->set_voltage_time_sel(rdev, old_selector,
3272 selector);
3273 } else {
3274 if (old_uV != best_val) {
3275 if (ops->set_voltage_time)
3276 delay = ops->set_voltage_time(rdev, old_uV,
3277 best_val);
3278 else
3279 delay = _regulator_set_voltage_time(rdev,
3280 old_uV,
3281 best_val);
3285 if (delay < 0) {
3286 rdev_warn(rdev, "failed to get delay: %d\n", delay);
3287 delay = 0;
3290 /* Insert any necessary delays */
3291 if (delay >= 1000) {
3292 mdelay(delay / 1000);
3293 udelay(delay % 1000);
3294 } else if (delay) {
3295 udelay(delay);
3298 if (best_val >= 0) {
3299 unsigned long data = best_val;
3301 _notifier_call_chain(rdev, REGULATOR_EVENT_VOLTAGE_CHANGE,
3302 (void *)data);
3305 out:
3306 trace_regulator_set_voltage_complete(rdev_get_name(rdev), best_val);
3308 return ret;
3311 static int _regulator_do_set_suspend_voltage(struct regulator_dev *rdev,
3312 int min_uV, int max_uV, suspend_state_t state)
3314 struct regulator_state *rstate;
3315 int uV, sel;
3317 rstate = regulator_get_suspend_state(rdev, state);
3318 if (rstate == NULL)
3319 return -EINVAL;
3321 if (min_uV < rstate->min_uV)
3322 min_uV = rstate->min_uV;
3323 if (max_uV > rstate->max_uV)
3324 max_uV = rstate->max_uV;
3326 sel = regulator_map_voltage(rdev, min_uV, max_uV);
3327 if (sel < 0)
3328 return sel;
3330 uV = rdev->desc->ops->list_voltage(rdev, sel);
3331 if (uV >= min_uV && uV <= max_uV)
3332 rstate->uV = uV;
3334 return 0;
3337 static int regulator_set_voltage_unlocked(struct regulator *regulator,
3338 int min_uV, int max_uV,
3339 suspend_state_t state)
3341 struct regulator_dev *rdev = regulator->rdev;
3342 struct regulator_voltage *voltage = &regulator->voltage[state];
3343 int ret = 0;
3344 int old_min_uV, old_max_uV;
3345 int current_uV;
3347 /* If we're setting the same range as last time the change
3348 * should be a noop (some cpufreq implementations use the same
3349 * voltage for multiple frequencies, for example).
3351 if (voltage->min_uV == min_uV && voltage->max_uV == max_uV)
3352 goto out;
3354 /* If we're trying to set a range that overlaps the current voltage,
3355 * return successfully even though the regulator does not support
3356 * changing the voltage.
3358 if (!regulator_ops_is_valid(rdev, REGULATOR_CHANGE_VOLTAGE)) {
3359 current_uV = regulator_get_voltage_rdev(rdev);
3360 if (min_uV <= current_uV && current_uV <= max_uV) {
3361 voltage->min_uV = min_uV;
3362 voltage->max_uV = max_uV;
3363 goto out;
3367 /* sanity check */
3368 if (!rdev->desc->ops->set_voltage &&
3369 !rdev->desc->ops->set_voltage_sel) {
3370 ret = -EINVAL;
3371 goto out;
3374 /* constraints check */
3375 ret = regulator_check_voltage(rdev, &min_uV, &max_uV);
3376 if (ret < 0)
3377 goto out;
3379 /* restore original values in case of error */
3380 old_min_uV = voltage->min_uV;
3381 old_max_uV = voltage->max_uV;
3382 voltage->min_uV = min_uV;
3383 voltage->max_uV = max_uV;
3385 /* for not coupled regulators this will just set the voltage */
3386 ret = regulator_balance_voltage(rdev, state);
3387 if (ret < 0) {
3388 voltage->min_uV = old_min_uV;
3389 voltage->max_uV = old_max_uV;
3392 out:
3393 return ret;
3396 int regulator_set_voltage_rdev(struct regulator_dev *rdev, int min_uV,
3397 int max_uV, suspend_state_t state)
3399 int best_supply_uV = 0;
3400 int supply_change_uV = 0;
3401 int ret;
3403 if (rdev->supply &&
3404 regulator_ops_is_valid(rdev->supply->rdev,
3405 REGULATOR_CHANGE_VOLTAGE) &&
3406 (rdev->desc->min_dropout_uV || !(rdev->desc->ops->get_voltage ||
3407 rdev->desc->ops->get_voltage_sel))) {
3408 int current_supply_uV;
3409 int selector;
3411 selector = regulator_map_voltage(rdev, min_uV, max_uV);
3412 if (selector < 0) {
3413 ret = selector;
3414 goto out;
3417 best_supply_uV = _regulator_list_voltage(rdev, selector, 0);
3418 if (best_supply_uV < 0) {
3419 ret = best_supply_uV;
3420 goto out;
3423 best_supply_uV += rdev->desc->min_dropout_uV;
3425 current_supply_uV = regulator_get_voltage_rdev(rdev->supply->rdev);
3426 if (current_supply_uV < 0) {
3427 ret = current_supply_uV;
3428 goto out;
3431 supply_change_uV = best_supply_uV - current_supply_uV;
3434 if (supply_change_uV > 0) {
3435 ret = regulator_set_voltage_unlocked(rdev->supply,
3436 best_supply_uV, INT_MAX, state);
3437 if (ret) {
3438 dev_err(&rdev->dev, "Failed to increase supply voltage: %d\n",
3439 ret);
3440 goto out;
3444 if (state == PM_SUSPEND_ON)
3445 ret = _regulator_do_set_voltage(rdev, min_uV, max_uV);
3446 else
3447 ret = _regulator_do_set_suspend_voltage(rdev, min_uV,
3448 max_uV, state);
3449 if (ret < 0)
3450 goto out;
3452 if (supply_change_uV < 0) {
3453 ret = regulator_set_voltage_unlocked(rdev->supply,
3454 best_supply_uV, INT_MAX, state);
3455 if (ret)
3456 dev_warn(&rdev->dev, "Failed to decrease supply voltage: %d\n",
3457 ret);
3458 /* No need to fail here */
3459 ret = 0;
3462 out:
3463 return ret;
3465 EXPORT_SYMBOL_GPL(regulator_set_voltage_rdev);
3467 static int regulator_limit_voltage_step(struct regulator_dev *rdev,
3468 int *current_uV, int *min_uV)
3470 struct regulation_constraints *constraints = rdev->constraints;
3472 /* Limit voltage change only if necessary */
3473 if (!constraints->max_uV_step || !_regulator_is_enabled(rdev))
3474 return 1;
3476 if (*current_uV < 0) {
3477 *current_uV = regulator_get_voltage_rdev(rdev);
3479 if (*current_uV < 0)
3480 return *current_uV;
3483 if (abs(*current_uV - *min_uV) <= constraints->max_uV_step)
3484 return 1;
3486 /* Clamp target voltage within the given step */
3487 if (*current_uV < *min_uV)
3488 *min_uV = min(*current_uV + constraints->max_uV_step,
3489 *min_uV);
3490 else
3491 *min_uV = max(*current_uV - constraints->max_uV_step,
3492 *min_uV);
3494 return 0;
3497 static int regulator_get_optimal_voltage(struct regulator_dev *rdev,
3498 int *current_uV,
3499 int *min_uV, int *max_uV,
3500 suspend_state_t state,
3501 int n_coupled)
3503 struct coupling_desc *c_desc = &rdev->coupling_desc;
3504 struct regulator_dev **c_rdevs = c_desc->coupled_rdevs;
3505 struct regulation_constraints *constraints = rdev->constraints;
3506 int desired_min_uV = 0, desired_max_uV = INT_MAX;
3507 int max_current_uV = 0, min_current_uV = INT_MAX;
3508 int highest_min_uV = 0, target_uV, possible_uV;
3509 int i, ret, max_spread;
3510 bool done;
3512 *current_uV = -1;
3515 * If there are no coupled regulators, simply set the voltage
3516 * demanded by consumers.
3518 if (n_coupled == 1) {
3520 * If consumers don't provide any demands, set voltage
3521 * to min_uV
3523 desired_min_uV = constraints->min_uV;
3524 desired_max_uV = constraints->max_uV;
3526 ret = regulator_check_consumers(rdev,
3527 &desired_min_uV,
3528 &desired_max_uV, state);
3529 if (ret < 0)
3530 return ret;
3532 possible_uV = desired_min_uV;
3533 done = true;
3535 goto finish;
3538 /* Find highest min desired voltage */
3539 for (i = 0; i < n_coupled; i++) {
3540 int tmp_min = 0;
3541 int tmp_max = INT_MAX;
3543 lockdep_assert_held_once(&c_rdevs[i]->mutex.base);
3545 ret = regulator_check_consumers(c_rdevs[i],
3546 &tmp_min,
3547 &tmp_max, state);
3548 if (ret < 0)
3549 return ret;
3551 ret = regulator_check_voltage(c_rdevs[i], &tmp_min, &tmp_max);
3552 if (ret < 0)
3553 return ret;
3555 highest_min_uV = max(highest_min_uV, tmp_min);
3557 if (i == 0) {
3558 desired_min_uV = tmp_min;
3559 desired_max_uV = tmp_max;
3563 max_spread = constraints->max_spread[0];
3566 * Let target_uV be equal to the desired one if possible.
3567 * If not, set it to minimum voltage, allowed by other coupled
3568 * regulators.
3570 target_uV = max(desired_min_uV, highest_min_uV - max_spread);
3573 * Find min and max voltages, which currently aren't violating
3574 * max_spread.
3576 for (i = 1; i < n_coupled; i++) {
3577 int tmp_act;
3579 if (!_regulator_is_enabled(c_rdevs[i]))
3580 continue;
3582 tmp_act = regulator_get_voltage_rdev(c_rdevs[i]);
3583 if (tmp_act < 0)
3584 return tmp_act;
3586 min_current_uV = min(tmp_act, min_current_uV);
3587 max_current_uV = max(tmp_act, max_current_uV);
3590 /* There aren't any other regulators enabled */
3591 if (max_current_uV == 0) {
3592 possible_uV = target_uV;
3593 } else {
3595 * Correct target voltage, so as it currently isn't
3596 * violating max_spread
3598 possible_uV = max(target_uV, max_current_uV - max_spread);
3599 possible_uV = min(possible_uV, min_current_uV + max_spread);
3602 if (possible_uV > desired_max_uV)
3603 return -EINVAL;
3605 done = (possible_uV == target_uV);
3606 desired_min_uV = possible_uV;
3608 finish:
3609 /* Apply max_uV_step constraint if necessary */
3610 if (state == PM_SUSPEND_ON) {
3611 ret = regulator_limit_voltage_step(rdev, current_uV,
3612 &desired_min_uV);
3613 if (ret < 0)
3614 return ret;
3616 if (ret == 0)
3617 done = false;
3620 /* Set current_uV if wasn't done earlier in the code and if necessary */
3621 if (n_coupled > 1 && *current_uV == -1) {
3623 if (_regulator_is_enabled(rdev)) {
3624 ret = regulator_get_voltage_rdev(rdev);
3625 if (ret < 0)
3626 return ret;
3628 *current_uV = ret;
3629 } else {
3630 *current_uV = desired_min_uV;
3634 *min_uV = desired_min_uV;
3635 *max_uV = desired_max_uV;
3637 return done;
3640 static int regulator_balance_voltage(struct regulator_dev *rdev,
3641 suspend_state_t state)
3643 struct regulator_dev **c_rdevs;
3644 struct regulator_dev *best_rdev;
3645 struct coupling_desc *c_desc = &rdev->coupling_desc;
3646 struct regulator_coupler *coupler = c_desc->coupler;
3647 int i, ret, n_coupled, best_min_uV, best_max_uV, best_c_rdev;
3648 unsigned int delta, best_delta;
3649 unsigned long c_rdev_done = 0;
3650 bool best_c_rdev_done;
3652 c_rdevs = c_desc->coupled_rdevs;
3653 n_coupled = c_desc->n_coupled;
3656 * If system is in a state other than PM_SUSPEND_ON, don't check
3657 * other coupled regulators.
3659 if (state != PM_SUSPEND_ON)
3660 n_coupled = 1;
3662 if (c_desc->n_resolved < n_coupled) {
3663 rdev_err(rdev, "Not all coupled regulators registered\n");
3664 return -EPERM;
3667 /* Invoke custom balancer for customized couplers */
3668 if (coupler && coupler->balance_voltage)
3669 return coupler->balance_voltage(coupler, rdev, state);
3672 * Find the best possible voltage change on each loop. Leave the loop
3673 * if there isn't any possible change.
3675 do {
3676 best_c_rdev_done = false;
3677 best_delta = 0;
3678 best_min_uV = 0;
3679 best_max_uV = 0;
3680 best_c_rdev = 0;
3681 best_rdev = NULL;
3684 * Find highest difference between optimal voltage
3685 * and current voltage.
3687 for (i = 0; i < n_coupled; i++) {
3689 * optimal_uV is the best voltage that can be set for
3690 * i-th regulator at the moment without violating
3691 * max_spread constraint in order to balance
3692 * the coupled voltages.
3694 int optimal_uV = 0, optimal_max_uV = 0, current_uV = 0;
3696 if (test_bit(i, &c_rdev_done))
3697 continue;
3699 ret = regulator_get_optimal_voltage(c_rdevs[i],
3700 &current_uV,
3701 &optimal_uV,
3702 &optimal_max_uV,
3703 state, n_coupled);
3704 if (ret < 0)
3705 goto out;
3707 delta = abs(optimal_uV - current_uV);
3709 if (delta && best_delta <= delta) {
3710 best_c_rdev_done = ret;
3711 best_delta = delta;
3712 best_rdev = c_rdevs[i];
3713 best_min_uV = optimal_uV;
3714 best_max_uV = optimal_max_uV;
3715 best_c_rdev = i;
3719 /* Nothing to change, return successfully */
3720 if (!best_rdev) {
3721 ret = 0;
3722 goto out;
3725 ret = regulator_set_voltage_rdev(best_rdev, best_min_uV,
3726 best_max_uV, state);
3728 if (ret < 0)
3729 goto out;
3731 if (best_c_rdev_done)
3732 set_bit(best_c_rdev, &c_rdev_done);
3734 } while (n_coupled > 1);
3736 out:
3737 return ret;
3741 * regulator_set_voltage - set regulator output voltage
3742 * @regulator: regulator source
3743 * @min_uV: Minimum required voltage in uV
3744 * @max_uV: Maximum acceptable voltage in uV
3746 * Sets a voltage regulator to the desired output voltage. This can be set
3747 * during any regulator state. IOW, regulator can be disabled or enabled.
3749 * If the regulator is enabled then the voltage will change to the new value
3750 * immediately otherwise if the regulator is disabled the regulator will
3751 * output at the new voltage when enabled.
3753 * NOTE: If the regulator is shared between several devices then the lowest
3754 * request voltage that meets the system constraints will be used.
3755 * Regulator system constraints must be set for this regulator before
3756 * calling this function otherwise this call will fail.
3758 int regulator_set_voltage(struct regulator *regulator, int min_uV, int max_uV)
3760 struct ww_acquire_ctx ww_ctx;
3761 int ret;
3763 regulator_lock_dependent(regulator->rdev, &ww_ctx);
3765 ret = regulator_set_voltage_unlocked(regulator, min_uV, max_uV,
3766 PM_SUSPEND_ON);
3768 regulator_unlock_dependent(regulator->rdev, &ww_ctx);
3770 return ret;
3772 EXPORT_SYMBOL_GPL(regulator_set_voltage);
3774 static inline int regulator_suspend_toggle(struct regulator_dev *rdev,
3775 suspend_state_t state, bool en)
3777 struct regulator_state *rstate;
3779 rstate = regulator_get_suspend_state(rdev, state);
3780 if (rstate == NULL)
3781 return -EINVAL;
3783 if (!rstate->changeable)
3784 return -EPERM;
3786 rstate->enabled = (en) ? ENABLE_IN_SUSPEND : DISABLE_IN_SUSPEND;
3788 return 0;
3791 int regulator_suspend_enable(struct regulator_dev *rdev,
3792 suspend_state_t state)
3794 return regulator_suspend_toggle(rdev, state, true);
3796 EXPORT_SYMBOL_GPL(regulator_suspend_enable);
3798 int regulator_suspend_disable(struct regulator_dev *rdev,
3799 suspend_state_t state)
3801 struct regulator *regulator;
3802 struct regulator_voltage *voltage;
3805 * if any consumer wants this regulator device keeping on in
3806 * suspend states, don't set it as disabled.
3808 list_for_each_entry(regulator, &rdev->consumer_list, list) {
3809 voltage = &regulator->voltage[state];
3810 if (voltage->min_uV || voltage->max_uV)
3811 return 0;
3814 return regulator_suspend_toggle(rdev, state, false);
3816 EXPORT_SYMBOL_GPL(regulator_suspend_disable);
3818 static int _regulator_set_suspend_voltage(struct regulator *regulator,
3819 int min_uV, int max_uV,
3820 suspend_state_t state)
3822 struct regulator_dev *rdev = regulator->rdev;
3823 struct regulator_state *rstate;
3825 rstate = regulator_get_suspend_state(rdev, state);
3826 if (rstate == NULL)
3827 return -EINVAL;
3829 if (rstate->min_uV == rstate->max_uV) {
3830 rdev_err(rdev, "The suspend voltage can't be changed!\n");
3831 return -EPERM;
3834 return regulator_set_voltage_unlocked(regulator, min_uV, max_uV, state);
3837 int regulator_set_suspend_voltage(struct regulator *regulator, int min_uV,
3838 int max_uV, suspend_state_t state)
3840 struct ww_acquire_ctx ww_ctx;
3841 int ret;
3843 /* PM_SUSPEND_ON is handled by regulator_set_voltage() */
3844 if (regulator_check_states(state) || state == PM_SUSPEND_ON)
3845 return -EINVAL;
3847 regulator_lock_dependent(regulator->rdev, &ww_ctx);
3849 ret = _regulator_set_suspend_voltage(regulator, min_uV,
3850 max_uV, state);
3852 regulator_unlock_dependent(regulator->rdev, &ww_ctx);
3854 return ret;
3856 EXPORT_SYMBOL_GPL(regulator_set_suspend_voltage);
3859 * regulator_set_voltage_time - get raise/fall time
3860 * @regulator: regulator source
3861 * @old_uV: starting voltage in microvolts
3862 * @new_uV: target voltage in microvolts
3864 * Provided with the starting and ending voltage, this function attempts to
3865 * calculate the time in microseconds required to rise or fall to this new
3866 * voltage.
3868 int regulator_set_voltage_time(struct regulator *regulator,
3869 int old_uV, int new_uV)
3871 struct regulator_dev *rdev = regulator->rdev;
3872 const struct regulator_ops *ops = rdev->desc->ops;
3873 int old_sel = -1;
3874 int new_sel = -1;
3875 int voltage;
3876 int i;
3878 if (ops->set_voltage_time)
3879 return ops->set_voltage_time(rdev, old_uV, new_uV);
3880 else if (!ops->set_voltage_time_sel)
3881 return _regulator_set_voltage_time(rdev, old_uV, new_uV);
3883 /* Currently requires operations to do this */
3884 if (!ops->list_voltage || !rdev->desc->n_voltages)
3885 return -EINVAL;
3887 for (i = 0; i < rdev->desc->n_voltages; i++) {
3888 /* We only look for exact voltage matches here */
3889 voltage = regulator_list_voltage(regulator, i);
3890 if (voltage < 0)
3891 return -EINVAL;
3892 if (voltage == 0)
3893 continue;
3894 if (voltage == old_uV)
3895 old_sel = i;
3896 if (voltage == new_uV)
3897 new_sel = i;
3900 if (old_sel < 0 || new_sel < 0)
3901 return -EINVAL;
3903 return ops->set_voltage_time_sel(rdev, old_sel, new_sel);
3905 EXPORT_SYMBOL_GPL(regulator_set_voltage_time);
3908 * regulator_set_voltage_time_sel - get raise/fall time
3909 * @rdev: regulator source device
3910 * @old_selector: selector for starting voltage
3911 * @new_selector: selector for target voltage
3913 * Provided with the starting and target voltage selectors, this function
3914 * returns time in microseconds required to rise or fall to this new voltage
3916 * Drivers providing ramp_delay in regulation_constraints can use this as their
3917 * set_voltage_time_sel() operation.
3919 int regulator_set_voltage_time_sel(struct regulator_dev *rdev,
3920 unsigned int old_selector,
3921 unsigned int new_selector)
3923 int old_volt, new_volt;
3925 /* sanity check */
3926 if (!rdev->desc->ops->list_voltage)
3927 return -EINVAL;
3929 old_volt = rdev->desc->ops->list_voltage(rdev, old_selector);
3930 new_volt = rdev->desc->ops->list_voltage(rdev, new_selector);
3932 if (rdev->desc->ops->set_voltage_time)
3933 return rdev->desc->ops->set_voltage_time(rdev, old_volt,
3934 new_volt);
3935 else
3936 return _regulator_set_voltage_time(rdev, old_volt, new_volt);
3938 EXPORT_SYMBOL_GPL(regulator_set_voltage_time_sel);
3941 * regulator_sync_voltage - re-apply last regulator output voltage
3942 * @regulator: regulator source
3944 * Re-apply the last configured voltage. This is intended to be used
3945 * where some external control source the consumer is cooperating with
3946 * has caused the configured voltage to change.
3948 int regulator_sync_voltage(struct regulator *regulator)
3950 struct regulator_dev *rdev = regulator->rdev;
3951 struct regulator_voltage *voltage = &regulator->voltage[PM_SUSPEND_ON];
3952 int ret, min_uV, max_uV;
3954 regulator_lock(rdev);
3956 if (!rdev->desc->ops->set_voltage &&
3957 !rdev->desc->ops->set_voltage_sel) {
3958 ret = -EINVAL;
3959 goto out;
3962 /* This is only going to work if we've had a voltage configured. */
3963 if (!voltage->min_uV && !voltage->max_uV) {
3964 ret = -EINVAL;
3965 goto out;
3968 min_uV = voltage->min_uV;
3969 max_uV = voltage->max_uV;
3971 /* This should be a paranoia check... */
3972 ret = regulator_check_voltage(rdev, &min_uV, &max_uV);
3973 if (ret < 0)
3974 goto out;
3976 ret = regulator_check_consumers(rdev, &min_uV, &max_uV, 0);
3977 if (ret < 0)
3978 goto out;
3980 ret = _regulator_do_set_voltage(rdev, min_uV, max_uV);
3982 out:
3983 regulator_unlock(rdev);
3984 return ret;
3986 EXPORT_SYMBOL_GPL(regulator_sync_voltage);
3988 int regulator_get_voltage_rdev(struct regulator_dev *rdev)
3990 int sel, ret;
3991 bool bypassed;
3993 if (rdev->desc->ops->get_bypass) {
3994 ret = rdev->desc->ops->get_bypass(rdev, &bypassed);
3995 if (ret < 0)
3996 return ret;
3997 if (bypassed) {
3998 /* if bypassed the regulator must have a supply */
3999 if (!rdev->supply) {
4000 rdev_err(rdev,
4001 "bypassed regulator has no supply!\n");
4002 return -EPROBE_DEFER;
4005 return regulator_get_voltage_rdev(rdev->supply->rdev);
4009 if (rdev->desc->ops->get_voltage_sel) {
4010 sel = rdev->desc->ops->get_voltage_sel(rdev);
4011 if (sel < 0)
4012 return sel;
4013 ret = rdev->desc->ops->list_voltage(rdev, sel);
4014 } else if (rdev->desc->ops->get_voltage) {
4015 ret = rdev->desc->ops->get_voltage(rdev);
4016 } else if (rdev->desc->ops->list_voltage) {
4017 ret = rdev->desc->ops->list_voltage(rdev, 0);
4018 } else if (rdev->desc->fixed_uV && (rdev->desc->n_voltages == 1)) {
4019 ret = rdev->desc->fixed_uV;
4020 } else if (rdev->supply) {
4021 ret = regulator_get_voltage_rdev(rdev->supply->rdev);
4022 } else {
4023 return -EINVAL;
4026 if (ret < 0)
4027 return ret;
4028 return ret - rdev->constraints->uV_offset;
4030 EXPORT_SYMBOL_GPL(regulator_get_voltage_rdev);
4033 * regulator_get_voltage - get regulator output voltage
4034 * @regulator: regulator source
4036 * This returns the current regulator voltage in uV.
4038 * NOTE: If the regulator is disabled it will return the voltage value. This
4039 * function should not be used to determine regulator state.
4041 int regulator_get_voltage(struct regulator *regulator)
4043 struct ww_acquire_ctx ww_ctx;
4044 int ret;
4046 regulator_lock_dependent(regulator->rdev, &ww_ctx);
4047 ret = regulator_get_voltage_rdev(regulator->rdev);
4048 regulator_unlock_dependent(regulator->rdev, &ww_ctx);
4050 return ret;
4052 EXPORT_SYMBOL_GPL(regulator_get_voltage);
4055 * regulator_set_current_limit - set regulator output current limit
4056 * @regulator: regulator source
4057 * @min_uA: Minimum supported current in uA
4058 * @max_uA: Maximum supported current in uA
4060 * Sets current sink to the desired output current. This can be set during
4061 * any regulator state. IOW, regulator can be disabled or enabled.
4063 * If the regulator is enabled then the current will change to the new value
4064 * immediately otherwise if the regulator is disabled the regulator will
4065 * output at the new current when enabled.
4067 * NOTE: Regulator system constraints must be set for this regulator before
4068 * calling this function otherwise this call will fail.
4070 int regulator_set_current_limit(struct regulator *regulator,
4071 int min_uA, int max_uA)
4073 struct regulator_dev *rdev = regulator->rdev;
4074 int ret;
4076 regulator_lock(rdev);
4078 /* sanity check */
4079 if (!rdev->desc->ops->set_current_limit) {
4080 ret = -EINVAL;
4081 goto out;
4084 /* constraints check */
4085 ret = regulator_check_current_limit(rdev, &min_uA, &max_uA);
4086 if (ret < 0)
4087 goto out;
4089 ret = rdev->desc->ops->set_current_limit(rdev, min_uA, max_uA);
4090 out:
4091 regulator_unlock(rdev);
4092 return ret;
4094 EXPORT_SYMBOL_GPL(regulator_set_current_limit);
4096 static int _regulator_get_current_limit_unlocked(struct regulator_dev *rdev)
4098 /* sanity check */
4099 if (!rdev->desc->ops->get_current_limit)
4100 return -EINVAL;
4102 return rdev->desc->ops->get_current_limit(rdev);
4105 static int _regulator_get_current_limit(struct regulator_dev *rdev)
4107 int ret;
4109 regulator_lock(rdev);
4110 ret = _regulator_get_current_limit_unlocked(rdev);
4111 regulator_unlock(rdev);
4113 return ret;
4117 * regulator_get_current_limit - get regulator output current
4118 * @regulator: regulator source
4120 * This returns the current supplied by the specified current sink in uA.
4122 * NOTE: If the regulator is disabled it will return the current value. This
4123 * function should not be used to determine regulator state.
4125 int regulator_get_current_limit(struct regulator *regulator)
4127 return _regulator_get_current_limit(regulator->rdev);
4129 EXPORT_SYMBOL_GPL(regulator_get_current_limit);
4132 * regulator_set_mode - set regulator operating mode
4133 * @regulator: regulator source
4134 * @mode: operating mode - one of the REGULATOR_MODE constants
4136 * Set regulator operating mode to increase regulator efficiency or improve
4137 * regulation performance.
4139 * NOTE: Regulator system constraints must be set for this regulator before
4140 * calling this function otherwise this call will fail.
4142 int regulator_set_mode(struct regulator *regulator, unsigned int mode)
4144 struct regulator_dev *rdev = regulator->rdev;
4145 int ret;
4146 int regulator_curr_mode;
4148 regulator_lock(rdev);
4150 /* sanity check */
4151 if (!rdev->desc->ops->set_mode) {
4152 ret = -EINVAL;
4153 goto out;
4156 /* return if the same mode is requested */
4157 if (rdev->desc->ops->get_mode) {
4158 regulator_curr_mode = rdev->desc->ops->get_mode(rdev);
4159 if (regulator_curr_mode == mode) {
4160 ret = 0;
4161 goto out;
4165 /* constraints check */
4166 ret = regulator_mode_constrain(rdev, &mode);
4167 if (ret < 0)
4168 goto out;
4170 ret = rdev->desc->ops->set_mode(rdev, mode);
4171 out:
4172 regulator_unlock(rdev);
4173 return ret;
4175 EXPORT_SYMBOL_GPL(regulator_set_mode);
4177 static unsigned int _regulator_get_mode_unlocked(struct regulator_dev *rdev)
4179 /* sanity check */
4180 if (!rdev->desc->ops->get_mode)
4181 return -EINVAL;
4183 return rdev->desc->ops->get_mode(rdev);
4186 static unsigned int _regulator_get_mode(struct regulator_dev *rdev)
4188 int ret;
4190 regulator_lock(rdev);
4191 ret = _regulator_get_mode_unlocked(rdev);
4192 regulator_unlock(rdev);
4194 return ret;
4198 * regulator_get_mode - get regulator operating mode
4199 * @regulator: regulator source
4201 * Get the current regulator operating mode.
4203 unsigned int regulator_get_mode(struct regulator *regulator)
4205 return _regulator_get_mode(regulator->rdev);
4207 EXPORT_SYMBOL_GPL(regulator_get_mode);
4209 static int _regulator_get_error_flags(struct regulator_dev *rdev,
4210 unsigned int *flags)
4212 int ret;
4214 regulator_lock(rdev);
4216 /* sanity check */
4217 if (!rdev->desc->ops->get_error_flags) {
4218 ret = -EINVAL;
4219 goto out;
4222 ret = rdev->desc->ops->get_error_flags(rdev, flags);
4223 out:
4224 regulator_unlock(rdev);
4225 return ret;
4229 * regulator_get_error_flags - get regulator error information
4230 * @regulator: regulator source
4231 * @flags: pointer to store error flags
4233 * Get the current regulator error information.
4235 int regulator_get_error_flags(struct regulator *regulator,
4236 unsigned int *flags)
4238 return _regulator_get_error_flags(regulator->rdev, flags);
4240 EXPORT_SYMBOL_GPL(regulator_get_error_flags);
4243 * regulator_set_load - set regulator load
4244 * @regulator: regulator source
4245 * @uA_load: load current
4247 * Notifies the regulator core of a new device load. This is then used by
4248 * DRMS (if enabled by constraints) to set the most efficient regulator
4249 * operating mode for the new regulator loading.
4251 * Consumer devices notify their supply regulator of the maximum power
4252 * they will require (can be taken from device datasheet in the power
4253 * consumption tables) when they change operational status and hence power
4254 * state. Examples of operational state changes that can affect power
4255 * consumption are :-
4257 * o Device is opened / closed.
4258 * o Device I/O is about to begin or has just finished.
4259 * o Device is idling in between work.
4261 * This information is also exported via sysfs to userspace.
4263 * DRMS will sum the total requested load on the regulator and change
4264 * to the most efficient operating mode if platform constraints allow.
4266 * NOTE: when a regulator consumer requests to have a regulator
4267 * disabled then any load that consumer requested no longer counts
4268 * toward the total requested load. If the regulator is re-enabled
4269 * then the previously requested load will start counting again.
4271 * If a regulator is an always-on regulator then an individual consumer's
4272 * load will still be removed if that consumer is fully disabled.
4274 * On error a negative errno is returned.
4276 int regulator_set_load(struct regulator *regulator, int uA_load)
4278 struct regulator_dev *rdev = regulator->rdev;
4279 int old_uA_load;
4280 int ret = 0;
4282 regulator_lock(rdev);
4283 old_uA_load = regulator->uA_load;
4284 regulator->uA_load = uA_load;
4285 if (regulator->enable_count && old_uA_load != uA_load) {
4286 ret = drms_uA_update(rdev);
4287 if (ret < 0)
4288 regulator->uA_load = old_uA_load;
4290 regulator_unlock(rdev);
4292 return ret;
4294 EXPORT_SYMBOL_GPL(regulator_set_load);
4297 * regulator_allow_bypass - allow the regulator to go into bypass mode
4299 * @regulator: Regulator to configure
4300 * @enable: enable or disable bypass mode
4302 * Allow the regulator to go into bypass mode if all other consumers
4303 * for the regulator also enable bypass mode and the machine
4304 * constraints allow this. Bypass mode means that the regulator is
4305 * simply passing the input directly to the output with no regulation.
4307 int regulator_allow_bypass(struct regulator *regulator, bool enable)
4309 struct regulator_dev *rdev = regulator->rdev;
4310 int ret = 0;
4312 if (!rdev->desc->ops->set_bypass)
4313 return 0;
4315 if (!regulator_ops_is_valid(rdev, REGULATOR_CHANGE_BYPASS))
4316 return 0;
4318 regulator_lock(rdev);
4320 if (enable && !regulator->bypass) {
4321 rdev->bypass_count++;
4323 if (rdev->bypass_count == rdev->open_count) {
4324 ret = rdev->desc->ops->set_bypass(rdev, enable);
4325 if (ret != 0)
4326 rdev->bypass_count--;
4329 } else if (!enable && regulator->bypass) {
4330 rdev->bypass_count--;
4332 if (rdev->bypass_count != rdev->open_count) {
4333 ret = rdev->desc->ops->set_bypass(rdev, enable);
4334 if (ret != 0)
4335 rdev->bypass_count++;
4339 if (ret == 0)
4340 regulator->bypass = enable;
4342 regulator_unlock(rdev);
4344 return ret;
4346 EXPORT_SYMBOL_GPL(regulator_allow_bypass);
4349 * regulator_register_notifier - register regulator event notifier
4350 * @regulator: regulator source
4351 * @nb: notifier block
4353 * Register notifier block to receive regulator events.
4355 int regulator_register_notifier(struct regulator *regulator,
4356 struct notifier_block *nb)
4358 return blocking_notifier_chain_register(&regulator->rdev->notifier,
4359 nb);
4361 EXPORT_SYMBOL_GPL(regulator_register_notifier);
4364 * regulator_unregister_notifier - unregister regulator event notifier
4365 * @regulator: regulator source
4366 * @nb: notifier block
4368 * Unregister regulator event notifier block.
4370 int regulator_unregister_notifier(struct regulator *regulator,
4371 struct notifier_block *nb)
4373 return blocking_notifier_chain_unregister(&regulator->rdev->notifier,
4374 nb);
4376 EXPORT_SYMBOL_GPL(regulator_unregister_notifier);
4378 /* notify regulator consumers and downstream regulator consumers.
4379 * Note mutex must be held by caller.
4381 static int _notifier_call_chain(struct regulator_dev *rdev,
4382 unsigned long event, void *data)
4384 /* call rdev chain first */
4385 return blocking_notifier_call_chain(&rdev->notifier, event, data);
4389 * regulator_bulk_get - get multiple regulator consumers
4391 * @dev: Device to supply
4392 * @num_consumers: Number of consumers to register
4393 * @consumers: Configuration of consumers; clients are stored here.
4395 * @return 0 on success, an errno on failure.
4397 * This helper function allows drivers to get several regulator
4398 * consumers in one operation. If any of the regulators cannot be
4399 * acquired then any regulators that were allocated will be freed
4400 * before returning to the caller.
4402 int regulator_bulk_get(struct device *dev, int num_consumers,
4403 struct regulator_bulk_data *consumers)
4405 int i;
4406 int ret;
4408 for (i = 0; i < num_consumers; i++)
4409 consumers[i].consumer = NULL;
4411 for (i = 0; i < num_consumers; i++) {
4412 consumers[i].consumer = regulator_get(dev,
4413 consumers[i].supply);
4414 if (IS_ERR(consumers[i].consumer)) {
4415 ret = PTR_ERR(consumers[i].consumer);
4416 consumers[i].consumer = NULL;
4417 goto err;
4421 return 0;
4423 err:
4424 if (ret != -EPROBE_DEFER)
4425 dev_err(dev, "Failed to get supply '%s': %d\n",
4426 consumers[i].supply, ret);
4427 else
4428 dev_dbg(dev, "Failed to get supply '%s', deferring\n",
4429 consumers[i].supply);
4431 while (--i >= 0)
4432 regulator_put(consumers[i].consumer);
4434 return ret;
4436 EXPORT_SYMBOL_GPL(regulator_bulk_get);
4438 static void regulator_bulk_enable_async(void *data, async_cookie_t cookie)
4440 struct regulator_bulk_data *bulk = data;
4442 bulk->ret = regulator_enable(bulk->consumer);
4446 * regulator_bulk_enable - enable multiple regulator consumers
4448 * @num_consumers: Number of consumers
4449 * @consumers: Consumer data; clients are stored here.
4450 * @return 0 on success, an errno on failure
4452 * This convenience API allows consumers to enable multiple regulator
4453 * clients in a single API call. If any consumers cannot be enabled
4454 * then any others that were enabled will be disabled again prior to
4455 * return.
4457 int regulator_bulk_enable(int num_consumers,
4458 struct regulator_bulk_data *consumers)
4460 ASYNC_DOMAIN_EXCLUSIVE(async_domain);
4461 int i;
4462 int ret = 0;
4464 for (i = 0; i < num_consumers; i++) {
4465 async_schedule_domain(regulator_bulk_enable_async,
4466 &consumers[i], &async_domain);
4469 async_synchronize_full_domain(&async_domain);
4471 /* If any consumer failed we need to unwind any that succeeded */
4472 for (i = 0; i < num_consumers; i++) {
4473 if (consumers[i].ret != 0) {
4474 ret = consumers[i].ret;
4475 goto err;
4479 return 0;
4481 err:
4482 for (i = 0; i < num_consumers; i++) {
4483 if (consumers[i].ret < 0)
4484 pr_err("Failed to enable %s: %d\n", consumers[i].supply,
4485 consumers[i].ret);
4486 else
4487 regulator_disable(consumers[i].consumer);
4490 return ret;
4492 EXPORT_SYMBOL_GPL(regulator_bulk_enable);
4495 * regulator_bulk_disable - disable multiple regulator consumers
4497 * @num_consumers: Number of consumers
4498 * @consumers: Consumer data; clients are stored here.
4499 * @return 0 on success, an errno on failure
4501 * This convenience API allows consumers to disable multiple regulator
4502 * clients in a single API call. If any consumers cannot be disabled
4503 * then any others that were disabled will be enabled again prior to
4504 * return.
4506 int regulator_bulk_disable(int num_consumers,
4507 struct regulator_bulk_data *consumers)
4509 int i;
4510 int ret, r;
4512 for (i = num_consumers - 1; i >= 0; --i) {
4513 ret = regulator_disable(consumers[i].consumer);
4514 if (ret != 0)
4515 goto err;
4518 return 0;
4520 err:
4521 pr_err("Failed to disable %s: %d\n", consumers[i].supply, ret);
4522 for (++i; i < num_consumers; ++i) {
4523 r = regulator_enable(consumers[i].consumer);
4524 if (r != 0)
4525 pr_err("Failed to re-enable %s: %d\n",
4526 consumers[i].supply, r);
4529 return ret;
4531 EXPORT_SYMBOL_GPL(regulator_bulk_disable);
4534 * regulator_bulk_force_disable - force disable multiple regulator consumers
4536 * @num_consumers: Number of consumers
4537 * @consumers: Consumer data; clients are stored here.
4538 * @return 0 on success, an errno on failure
4540 * This convenience API allows consumers to forcibly disable multiple regulator
4541 * clients in a single API call.
4542 * NOTE: This should be used for situations when device damage will
4543 * likely occur if the regulators are not disabled (e.g. over temp).
4544 * Although regulator_force_disable function call for some consumers can
4545 * return error numbers, the function is called for all consumers.
4547 int regulator_bulk_force_disable(int num_consumers,
4548 struct regulator_bulk_data *consumers)
4550 int i;
4551 int ret = 0;
4553 for (i = 0; i < num_consumers; i++) {
4554 consumers[i].ret =
4555 regulator_force_disable(consumers[i].consumer);
4557 /* Store first error for reporting */
4558 if (consumers[i].ret && !ret)
4559 ret = consumers[i].ret;
4562 return ret;
4564 EXPORT_SYMBOL_GPL(regulator_bulk_force_disable);
4567 * regulator_bulk_free - free multiple regulator consumers
4569 * @num_consumers: Number of consumers
4570 * @consumers: Consumer data; clients are stored here.
4572 * This convenience API allows consumers to free multiple regulator
4573 * clients in a single API call.
4575 void regulator_bulk_free(int num_consumers,
4576 struct regulator_bulk_data *consumers)
4578 int i;
4580 for (i = 0; i < num_consumers; i++) {
4581 regulator_put(consumers[i].consumer);
4582 consumers[i].consumer = NULL;
4585 EXPORT_SYMBOL_GPL(regulator_bulk_free);
4588 * regulator_notifier_call_chain - call regulator event notifier
4589 * @rdev: regulator source
4590 * @event: notifier block
4591 * @data: callback-specific data.
4593 * Called by regulator drivers to notify clients a regulator event has
4594 * occurred. We also notify regulator clients downstream.
4595 * Note lock must be held by caller.
4597 int regulator_notifier_call_chain(struct regulator_dev *rdev,
4598 unsigned long event, void *data)
4600 lockdep_assert_held_once(&rdev->mutex.base);
4602 _notifier_call_chain(rdev, event, data);
4603 return NOTIFY_DONE;
4606 EXPORT_SYMBOL_GPL(regulator_notifier_call_chain);
4609 * regulator_mode_to_status - convert a regulator mode into a status
4611 * @mode: Mode to convert
4613 * Convert a regulator mode into a status.
4615 int regulator_mode_to_status(unsigned int mode)
4617 switch (mode) {
4618 case REGULATOR_MODE_FAST:
4619 return REGULATOR_STATUS_FAST;
4620 case REGULATOR_MODE_NORMAL:
4621 return REGULATOR_STATUS_NORMAL;
4622 case REGULATOR_MODE_IDLE:
4623 return REGULATOR_STATUS_IDLE;
4624 case REGULATOR_MODE_STANDBY:
4625 return REGULATOR_STATUS_STANDBY;
4626 default:
4627 return REGULATOR_STATUS_UNDEFINED;
4630 EXPORT_SYMBOL_GPL(regulator_mode_to_status);
4632 static struct attribute *regulator_dev_attrs[] = {
4633 &dev_attr_name.attr,
4634 &dev_attr_num_users.attr,
4635 &dev_attr_type.attr,
4636 &dev_attr_microvolts.attr,
4637 &dev_attr_microamps.attr,
4638 &dev_attr_opmode.attr,
4639 &dev_attr_state.attr,
4640 &dev_attr_status.attr,
4641 &dev_attr_bypass.attr,
4642 &dev_attr_requested_microamps.attr,
4643 &dev_attr_min_microvolts.attr,
4644 &dev_attr_max_microvolts.attr,
4645 &dev_attr_min_microamps.attr,
4646 &dev_attr_max_microamps.attr,
4647 &dev_attr_suspend_standby_state.attr,
4648 &dev_attr_suspend_mem_state.attr,
4649 &dev_attr_suspend_disk_state.attr,
4650 &dev_attr_suspend_standby_microvolts.attr,
4651 &dev_attr_suspend_mem_microvolts.attr,
4652 &dev_attr_suspend_disk_microvolts.attr,
4653 &dev_attr_suspend_standby_mode.attr,
4654 &dev_attr_suspend_mem_mode.attr,
4655 &dev_attr_suspend_disk_mode.attr,
4656 NULL
4660 * To avoid cluttering sysfs (and memory) with useless state, only
4661 * create attributes that can be meaningfully displayed.
4663 static umode_t regulator_attr_is_visible(struct kobject *kobj,
4664 struct attribute *attr, int idx)
4666 struct device *dev = kobj_to_dev(kobj);
4667 struct regulator_dev *rdev = dev_to_rdev(dev);
4668 const struct regulator_ops *ops = rdev->desc->ops;
4669 umode_t mode = attr->mode;
4671 /* these three are always present */
4672 if (attr == &dev_attr_name.attr ||
4673 attr == &dev_attr_num_users.attr ||
4674 attr == &dev_attr_type.attr)
4675 return mode;
4677 /* some attributes need specific methods to be displayed */
4678 if (attr == &dev_attr_microvolts.attr) {
4679 if ((ops->get_voltage && ops->get_voltage(rdev) >= 0) ||
4680 (ops->get_voltage_sel && ops->get_voltage_sel(rdev) >= 0) ||
4681 (ops->list_voltage && ops->list_voltage(rdev, 0) >= 0) ||
4682 (rdev->desc->fixed_uV && rdev->desc->n_voltages == 1))
4683 return mode;
4684 return 0;
4687 if (attr == &dev_attr_microamps.attr)
4688 return ops->get_current_limit ? mode : 0;
4690 if (attr == &dev_attr_opmode.attr)
4691 return ops->get_mode ? mode : 0;
4693 if (attr == &dev_attr_state.attr)
4694 return (rdev->ena_pin || ops->is_enabled) ? mode : 0;
4696 if (attr == &dev_attr_status.attr)
4697 return ops->get_status ? mode : 0;
4699 if (attr == &dev_attr_bypass.attr)
4700 return ops->get_bypass ? mode : 0;
4702 /* constraints need specific supporting methods */
4703 if (attr == &dev_attr_min_microvolts.attr ||
4704 attr == &dev_attr_max_microvolts.attr)
4705 return (ops->set_voltage || ops->set_voltage_sel) ? mode : 0;
4707 if (attr == &dev_attr_min_microamps.attr ||
4708 attr == &dev_attr_max_microamps.attr)
4709 return ops->set_current_limit ? mode : 0;
4711 if (attr == &dev_attr_suspend_standby_state.attr ||
4712 attr == &dev_attr_suspend_mem_state.attr ||
4713 attr == &dev_attr_suspend_disk_state.attr)
4714 return mode;
4716 if (attr == &dev_attr_suspend_standby_microvolts.attr ||
4717 attr == &dev_attr_suspend_mem_microvolts.attr ||
4718 attr == &dev_attr_suspend_disk_microvolts.attr)
4719 return ops->set_suspend_voltage ? mode : 0;
4721 if (attr == &dev_attr_suspend_standby_mode.attr ||
4722 attr == &dev_attr_suspend_mem_mode.attr ||
4723 attr == &dev_attr_suspend_disk_mode.attr)
4724 return ops->set_suspend_mode ? mode : 0;
4726 return mode;
4729 static const struct attribute_group regulator_dev_group = {
4730 .attrs = regulator_dev_attrs,
4731 .is_visible = regulator_attr_is_visible,
4734 static const struct attribute_group *regulator_dev_groups[] = {
4735 &regulator_dev_group,
4736 NULL
4739 static void regulator_dev_release(struct device *dev)
4741 struct regulator_dev *rdev = dev_get_drvdata(dev);
4743 kfree(rdev->constraints);
4744 of_node_put(rdev->dev.of_node);
4745 kfree(rdev);
4748 static void rdev_init_debugfs(struct regulator_dev *rdev)
4750 struct device *parent = rdev->dev.parent;
4751 const char *rname = rdev_get_name(rdev);
4752 char name[NAME_MAX];
4754 /* Avoid duplicate debugfs directory names */
4755 if (parent && rname == rdev->desc->name) {
4756 snprintf(name, sizeof(name), "%s-%s", dev_name(parent),
4757 rname);
4758 rname = name;
4761 rdev->debugfs = debugfs_create_dir(rname, debugfs_root);
4762 if (!rdev->debugfs) {
4763 rdev_warn(rdev, "Failed to create debugfs directory\n");
4764 return;
4767 debugfs_create_u32("use_count", 0444, rdev->debugfs,
4768 &rdev->use_count);
4769 debugfs_create_u32("open_count", 0444, rdev->debugfs,
4770 &rdev->open_count);
4771 debugfs_create_u32("bypass_count", 0444, rdev->debugfs,
4772 &rdev->bypass_count);
4775 static int regulator_register_resolve_supply(struct device *dev, void *data)
4777 struct regulator_dev *rdev = dev_to_rdev(dev);
4779 if (regulator_resolve_supply(rdev))
4780 rdev_dbg(rdev, "unable to resolve supply\n");
4782 return 0;
4785 int regulator_coupler_register(struct regulator_coupler *coupler)
4787 mutex_lock(&regulator_list_mutex);
4788 list_add_tail(&coupler->list, &regulator_coupler_list);
4789 mutex_unlock(&regulator_list_mutex);
4791 return 0;
4794 static struct regulator_coupler *
4795 regulator_find_coupler(struct regulator_dev *rdev)
4797 struct regulator_coupler *coupler;
4798 int err;
4801 * Note that regulators are appended to the list and the generic
4802 * coupler is registered first, hence it will be attached at last
4803 * if nobody cared.
4805 list_for_each_entry_reverse(coupler, &regulator_coupler_list, list) {
4806 err = coupler->attach_regulator(coupler, rdev);
4807 if (!err) {
4808 if (!coupler->balance_voltage &&
4809 rdev->coupling_desc.n_coupled > 2)
4810 goto err_unsupported;
4812 return coupler;
4815 if (err < 0)
4816 return ERR_PTR(err);
4818 if (err == 1)
4819 continue;
4821 break;
4824 return ERR_PTR(-EINVAL);
4826 err_unsupported:
4827 if (coupler->detach_regulator)
4828 coupler->detach_regulator(coupler, rdev);
4830 rdev_err(rdev,
4831 "Voltage balancing for multiple regulator couples is unimplemented\n");
4833 return ERR_PTR(-EPERM);
4836 static void regulator_resolve_coupling(struct regulator_dev *rdev)
4838 struct regulator_coupler *coupler = rdev->coupling_desc.coupler;
4839 struct coupling_desc *c_desc = &rdev->coupling_desc;
4840 int n_coupled = c_desc->n_coupled;
4841 struct regulator_dev *c_rdev;
4842 int i;
4844 for (i = 1; i < n_coupled; i++) {
4845 /* already resolved */
4846 if (c_desc->coupled_rdevs[i])
4847 continue;
4849 c_rdev = of_parse_coupled_regulator(rdev, i - 1);
4851 if (!c_rdev)
4852 continue;
4854 if (c_rdev->coupling_desc.coupler != coupler) {
4855 rdev_err(rdev, "coupler mismatch with %s\n",
4856 rdev_get_name(c_rdev));
4857 return;
4860 regulator_lock(c_rdev);
4862 c_desc->coupled_rdevs[i] = c_rdev;
4863 c_desc->n_resolved++;
4865 regulator_unlock(c_rdev);
4867 regulator_resolve_coupling(c_rdev);
4871 static void regulator_remove_coupling(struct regulator_dev *rdev)
4873 struct regulator_coupler *coupler = rdev->coupling_desc.coupler;
4874 struct coupling_desc *__c_desc, *c_desc = &rdev->coupling_desc;
4875 struct regulator_dev *__c_rdev, *c_rdev;
4876 unsigned int __n_coupled, n_coupled;
4877 int i, k;
4878 int err;
4880 n_coupled = c_desc->n_coupled;
4882 for (i = 1; i < n_coupled; i++) {
4883 c_rdev = c_desc->coupled_rdevs[i];
4885 if (!c_rdev)
4886 continue;
4888 regulator_lock(c_rdev);
4890 __c_desc = &c_rdev->coupling_desc;
4891 __n_coupled = __c_desc->n_coupled;
4893 for (k = 1; k < __n_coupled; k++) {
4894 __c_rdev = __c_desc->coupled_rdevs[k];
4896 if (__c_rdev == rdev) {
4897 __c_desc->coupled_rdevs[k] = NULL;
4898 __c_desc->n_resolved--;
4899 break;
4903 regulator_unlock(c_rdev);
4905 c_desc->coupled_rdevs[i] = NULL;
4906 c_desc->n_resolved--;
4909 if (coupler && coupler->detach_regulator) {
4910 err = coupler->detach_regulator(coupler, rdev);
4911 if (err)
4912 rdev_err(rdev, "failed to detach from coupler: %d\n",
4913 err);
4916 kfree(rdev->coupling_desc.coupled_rdevs);
4917 rdev->coupling_desc.coupled_rdevs = NULL;
4920 static int regulator_init_coupling(struct regulator_dev *rdev)
4922 int err, n_phandles;
4923 size_t alloc_size;
4925 if (!IS_ENABLED(CONFIG_OF))
4926 n_phandles = 0;
4927 else
4928 n_phandles = of_get_n_coupled(rdev);
4930 alloc_size = sizeof(*rdev) * (n_phandles + 1);
4932 rdev->coupling_desc.coupled_rdevs = kzalloc(alloc_size, GFP_KERNEL);
4933 if (!rdev->coupling_desc.coupled_rdevs)
4934 return -ENOMEM;
4937 * Every regulator should always have coupling descriptor filled with
4938 * at least pointer to itself.
4940 rdev->coupling_desc.coupled_rdevs[0] = rdev;
4941 rdev->coupling_desc.n_coupled = n_phandles + 1;
4942 rdev->coupling_desc.n_resolved++;
4944 /* regulator isn't coupled */
4945 if (n_phandles == 0)
4946 return 0;
4948 if (!of_check_coupling_data(rdev))
4949 return -EPERM;
4951 rdev->coupling_desc.coupler = regulator_find_coupler(rdev);
4952 if (IS_ERR(rdev->coupling_desc.coupler)) {
4953 err = PTR_ERR(rdev->coupling_desc.coupler);
4954 rdev_err(rdev, "failed to get coupler: %d\n", err);
4955 return err;
4958 return 0;
4961 static int generic_coupler_attach(struct regulator_coupler *coupler,
4962 struct regulator_dev *rdev)
4964 if (rdev->coupling_desc.n_coupled > 2) {
4965 rdev_err(rdev,
4966 "Voltage balancing for multiple regulator couples is unimplemented\n");
4967 return -EPERM;
4970 return 0;
4973 static struct regulator_coupler generic_regulator_coupler = {
4974 .attach_regulator = generic_coupler_attach,
4978 * regulator_register - register regulator
4979 * @regulator_desc: regulator to register
4980 * @cfg: runtime configuration for regulator
4982 * Called by regulator drivers to register a regulator.
4983 * Returns a valid pointer to struct regulator_dev on success
4984 * or an ERR_PTR() on error.
4986 struct regulator_dev *
4987 regulator_register(const struct regulator_desc *regulator_desc,
4988 const struct regulator_config *cfg)
4990 const struct regulation_constraints *constraints = NULL;
4991 const struct regulator_init_data *init_data;
4992 struct regulator_config *config = NULL;
4993 static atomic_t regulator_no = ATOMIC_INIT(-1);
4994 struct regulator_dev *rdev;
4995 bool dangling_cfg_gpiod = false;
4996 bool dangling_of_gpiod = false;
4997 struct device *dev;
4998 int ret, i;
5000 if (cfg == NULL)
5001 return ERR_PTR(-EINVAL);
5002 if (cfg->ena_gpiod)
5003 dangling_cfg_gpiod = true;
5004 if (regulator_desc == NULL) {
5005 ret = -EINVAL;
5006 goto rinse;
5009 dev = cfg->dev;
5010 WARN_ON(!dev);
5012 if (regulator_desc->name == NULL || regulator_desc->ops == NULL) {
5013 ret = -EINVAL;
5014 goto rinse;
5017 if (regulator_desc->type != REGULATOR_VOLTAGE &&
5018 regulator_desc->type != REGULATOR_CURRENT) {
5019 ret = -EINVAL;
5020 goto rinse;
5023 /* Only one of each should be implemented */
5024 WARN_ON(regulator_desc->ops->get_voltage &&
5025 regulator_desc->ops->get_voltage_sel);
5026 WARN_ON(regulator_desc->ops->set_voltage &&
5027 regulator_desc->ops->set_voltage_sel);
5029 /* If we're using selectors we must implement list_voltage. */
5030 if (regulator_desc->ops->get_voltage_sel &&
5031 !regulator_desc->ops->list_voltage) {
5032 ret = -EINVAL;
5033 goto rinse;
5035 if (regulator_desc->ops->set_voltage_sel &&
5036 !regulator_desc->ops->list_voltage) {
5037 ret = -EINVAL;
5038 goto rinse;
5041 rdev = kzalloc(sizeof(struct regulator_dev), GFP_KERNEL);
5042 if (rdev == NULL) {
5043 ret = -ENOMEM;
5044 goto rinse;
5048 * Duplicate the config so the driver could override it after
5049 * parsing init data.
5051 config = kmemdup(cfg, sizeof(*cfg), GFP_KERNEL);
5052 if (config == NULL) {
5053 kfree(rdev);
5054 ret = -ENOMEM;
5055 goto rinse;
5058 init_data = regulator_of_get_init_data(dev, regulator_desc, config,
5059 &rdev->dev.of_node);
5062 * Sometimes not all resources are probed already so we need to take
5063 * that into account. This happens most the time if the ena_gpiod comes
5064 * from a gpio extender or something else.
5066 if (PTR_ERR(init_data) == -EPROBE_DEFER) {
5067 kfree(config);
5068 kfree(rdev);
5069 ret = -EPROBE_DEFER;
5070 goto rinse;
5074 * We need to keep track of any GPIO descriptor coming from the
5075 * device tree until we have handled it over to the core. If the
5076 * config that was passed in to this function DOES NOT contain
5077 * a descriptor, and the config after this call DOES contain
5078 * a descriptor, we definitely got one from parsing the device
5079 * tree.
5081 if (!cfg->ena_gpiod && config->ena_gpiod)
5082 dangling_of_gpiod = true;
5083 if (!init_data) {
5084 init_data = config->init_data;
5085 rdev->dev.of_node = of_node_get(config->of_node);
5088 ww_mutex_init(&rdev->mutex, &regulator_ww_class);
5089 rdev->reg_data = config->driver_data;
5090 rdev->owner = regulator_desc->owner;
5091 rdev->desc = regulator_desc;
5092 if (config->regmap)
5093 rdev->regmap = config->regmap;
5094 else if (dev_get_regmap(dev, NULL))
5095 rdev->regmap = dev_get_regmap(dev, NULL);
5096 else if (dev->parent)
5097 rdev->regmap = dev_get_regmap(dev->parent, NULL);
5098 INIT_LIST_HEAD(&rdev->consumer_list);
5099 INIT_LIST_HEAD(&rdev->list);
5100 BLOCKING_INIT_NOTIFIER_HEAD(&rdev->notifier);
5101 INIT_DELAYED_WORK(&rdev->disable_work, regulator_disable_work);
5103 /* preform any regulator specific init */
5104 if (init_data && init_data->regulator_init) {
5105 ret = init_data->regulator_init(rdev->reg_data);
5106 if (ret < 0)
5107 goto clean;
5110 if (config->ena_gpiod) {
5111 mutex_lock(&regulator_list_mutex);
5112 ret = regulator_ena_gpio_request(rdev, config);
5113 mutex_unlock(&regulator_list_mutex);
5114 if (ret != 0) {
5115 rdev_err(rdev, "Failed to request enable GPIO: %d\n",
5116 ret);
5117 goto clean;
5119 /* The regulator core took over the GPIO descriptor */
5120 dangling_cfg_gpiod = false;
5121 dangling_of_gpiod = false;
5124 /* register with sysfs */
5125 device_initialize(&rdev->dev);
5126 rdev->dev.class = &regulator_class;
5127 rdev->dev.parent = dev;
5128 dev_set_name(&rdev->dev, "regulator.%lu",
5129 (unsigned long) atomic_inc_return(&regulator_no));
5130 dev_set_drvdata(&rdev->dev, rdev);
5132 /* set regulator constraints */
5133 if (init_data)
5134 constraints = &init_data->constraints;
5136 if (init_data && init_data->supply_regulator)
5137 rdev->supply_name = init_data->supply_regulator;
5138 else if (regulator_desc->supply_name)
5139 rdev->supply_name = regulator_desc->supply_name;
5142 * Attempt to resolve the regulator supply, if specified,
5143 * but don't return an error if we fail because we will try
5144 * to resolve it again later as more regulators are added.
5146 if (regulator_resolve_supply(rdev))
5147 rdev_dbg(rdev, "unable to resolve supply\n");
5149 ret = set_machine_constraints(rdev, constraints);
5150 if (ret < 0)
5151 goto wash;
5153 mutex_lock(&regulator_list_mutex);
5154 ret = regulator_init_coupling(rdev);
5155 mutex_unlock(&regulator_list_mutex);
5156 if (ret < 0)
5157 goto wash;
5159 /* add consumers devices */
5160 if (init_data) {
5161 mutex_lock(&regulator_list_mutex);
5162 for (i = 0; i < init_data->num_consumer_supplies; i++) {
5163 ret = set_consumer_device_supply(rdev,
5164 init_data->consumer_supplies[i].dev_name,
5165 init_data->consumer_supplies[i].supply);
5166 if (ret < 0) {
5167 mutex_unlock(&regulator_list_mutex);
5168 dev_err(dev, "Failed to set supply %s\n",
5169 init_data->consumer_supplies[i].supply);
5170 goto unset_supplies;
5173 mutex_unlock(&regulator_list_mutex);
5176 if (!rdev->desc->ops->get_voltage &&
5177 !rdev->desc->ops->list_voltage &&
5178 !rdev->desc->fixed_uV)
5179 rdev->is_switch = true;
5181 ret = device_add(&rdev->dev);
5182 if (ret != 0)
5183 goto unset_supplies;
5185 rdev_init_debugfs(rdev);
5187 /* try to resolve regulators coupling since a new one was registered */
5188 mutex_lock(&regulator_list_mutex);
5189 regulator_resolve_coupling(rdev);
5190 mutex_unlock(&regulator_list_mutex);
5192 /* try to resolve regulators supply since a new one was registered */
5193 class_for_each_device(&regulator_class, NULL, NULL,
5194 regulator_register_resolve_supply);
5195 kfree(config);
5196 return rdev;
5198 unset_supplies:
5199 mutex_lock(&regulator_list_mutex);
5200 unset_regulator_supplies(rdev);
5201 regulator_remove_coupling(rdev);
5202 mutex_unlock(&regulator_list_mutex);
5203 wash:
5204 kfree(rdev->coupling_desc.coupled_rdevs);
5205 mutex_lock(&regulator_list_mutex);
5206 regulator_ena_gpio_free(rdev);
5207 mutex_unlock(&regulator_list_mutex);
5208 put_device(&rdev->dev);
5209 rdev = NULL;
5210 clean:
5211 if (dangling_of_gpiod)
5212 gpiod_put(config->ena_gpiod);
5213 kfree(rdev);
5214 kfree(config);
5215 rinse:
5216 if (dangling_cfg_gpiod)
5217 gpiod_put(cfg->ena_gpiod);
5218 return ERR_PTR(ret);
5220 EXPORT_SYMBOL_GPL(regulator_register);
5223 * regulator_unregister - unregister regulator
5224 * @rdev: regulator to unregister
5226 * Called by regulator drivers to unregister a regulator.
5228 void regulator_unregister(struct regulator_dev *rdev)
5230 if (rdev == NULL)
5231 return;
5233 if (rdev->supply) {
5234 while (rdev->use_count--)
5235 regulator_disable(rdev->supply);
5236 regulator_put(rdev->supply);
5239 flush_work(&rdev->disable_work.work);
5241 mutex_lock(&regulator_list_mutex);
5243 debugfs_remove_recursive(rdev->debugfs);
5244 WARN_ON(rdev->open_count);
5245 regulator_remove_coupling(rdev);
5246 unset_regulator_supplies(rdev);
5247 list_del(&rdev->list);
5248 regulator_ena_gpio_free(rdev);
5249 device_unregister(&rdev->dev);
5251 mutex_unlock(&regulator_list_mutex);
5253 EXPORT_SYMBOL_GPL(regulator_unregister);
5255 #ifdef CONFIG_SUSPEND
5257 * regulator_suspend - prepare regulators for system wide suspend
5258 * @dev: ``&struct device`` pointer that is passed to _regulator_suspend()
5260 * Configure each regulator with it's suspend operating parameters for state.
5262 static int regulator_suspend(struct device *dev)
5264 struct regulator_dev *rdev = dev_to_rdev(dev);
5265 suspend_state_t state = pm_suspend_target_state;
5266 int ret;
5268 regulator_lock(rdev);
5269 ret = suspend_set_state(rdev, state);
5270 regulator_unlock(rdev);
5272 return ret;
5275 static int regulator_resume(struct device *dev)
5277 suspend_state_t state = pm_suspend_target_state;
5278 struct regulator_dev *rdev = dev_to_rdev(dev);
5279 struct regulator_state *rstate;
5280 int ret = 0;
5282 rstate = regulator_get_suspend_state(rdev, state);
5283 if (rstate == NULL)
5284 return 0;
5286 regulator_lock(rdev);
5288 if (rdev->desc->ops->resume &&
5289 (rstate->enabled == ENABLE_IN_SUSPEND ||
5290 rstate->enabled == DISABLE_IN_SUSPEND))
5291 ret = rdev->desc->ops->resume(rdev);
5293 regulator_unlock(rdev);
5295 return ret;
5297 #else /* !CONFIG_SUSPEND */
5299 #define regulator_suspend NULL
5300 #define regulator_resume NULL
5302 #endif /* !CONFIG_SUSPEND */
5304 #ifdef CONFIG_PM
5305 static const struct dev_pm_ops __maybe_unused regulator_pm_ops = {
5306 .suspend = regulator_suspend,
5307 .resume = regulator_resume,
5309 #endif
5311 struct class regulator_class = {
5312 .name = "regulator",
5313 .dev_release = regulator_dev_release,
5314 .dev_groups = regulator_dev_groups,
5315 #ifdef CONFIG_PM
5316 .pm = &regulator_pm_ops,
5317 #endif
5320 * regulator_has_full_constraints - the system has fully specified constraints
5322 * Calling this function will cause the regulator API to disable all
5323 * regulators which have a zero use count and don't have an always_on
5324 * constraint in a late_initcall.
5326 * The intention is that this will become the default behaviour in a
5327 * future kernel release so users are encouraged to use this facility
5328 * now.
5330 void regulator_has_full_constraints(void)
5332 has_full_constraints = 1;
5334 EXPORT_SYMBOL_GPL(regulator_has_full_constraints);
5337 * rdev_get_drvdata - get rdev regulator driver data
5338 * @rdev: regulator
5340 * Get rdev regulator driver private data. This call can be used in the
5341 * regulator driver context.
5343 void *rdev_get_drvdata(struct regulator_dev *rdev)
5345 return rdev->reg_data;
5347 EXPORT_SYMBOL_GPL(rdev_get_drvdata);
5350 * regulator_get_drvdata - get regulator driver data
5351 * @regulator: regulator
5353 * Get regulator driver private data. This call can be used in the consumer
5354 * driver context when non API regulator specific functions need to be called.
5356 void *regulator_get_drvdata(struct regulator *regulator)
5358 return regulator->rdev->reg_data;
5360 EXPORT_SYMBOL_GPL(regulator_get_drvdata);
5363 * regulator_set_drvdata - set regulator driver data
5364 * @regulator: regulator
5365 * @data: data
5367 void regulator_set_drvdata(struct regulator *regulator, void *data)
5369 regulator->rdev->reg_data = data;
5371 EXPORT_SYMBOL_GPL(regulator_set_drvdata);
5374 * regulator_get_id - get regulator ID
5375 * @rdev: regulator
5377 int rdev_get_id(struct regulator_dev *rdev)
5379 return rdev->desc->id;
5381 EXPORT_SYMBOL_GPL(rdev_get_id);
5383 struct device *rdev_get_dev(struct regulator_dev *rdev)
5385 return &rdev->dev;
5387 EXPORT_SYMBOL_GPL(rdev_get_dev);
5389 struct regmap *rdev_get_regmap(struct regulator_dev *rdev)
5391 return rdev->regmap;
5393 EXPORT_SYMBOL_GPL(rdev_get_regmap);
5395 void *regulator_get_init_drvdata(struct regulator_init_data *reg_init_data)
5397 return reg_init_data->driver_data;
5399 EXPORT_SYMBOL_GPL(regulator_get_init_drvdata);
5401 #ifdef CONFIG_DEBUG_FS
5402 static int supply_map_show(struct seq_file *sf, void *data)
5404 struct regulator_map *map;
5406 list_for_each_entry(map, &regulator_map_list, list) {
5407 seq_printf(sf, "%s -> %s.%s\n",
5408 rdev_get_name(map->regulator), map->dev_name,
5409 map->supply);
5412 return 0;
5414 DEFINE_SHOW_ATTRIBUTE(supply_map);
5416 struct summary_data {
5417 struct seq_file *s;
5418 struct regulator_dev *parent;
5419 int level;
5422 static void regulator_summary_show_subtree(struct seq_file *s,
5423 struct regulator_dev *rdev,
5424 int level);
5426 static int regulator_summary_show_children(struct device *dev, void *data)
5428 struct regulator_dev *rdev = dev_to_rdev(dev);
5429 struct summary_data *summary_data = data;
5431 if (rdev->supply && rdev->supply->rdev == summary_data->parent)
5432 regulator_summary_show_subtree(summary_data->s, rdev,
5433 summary_data->level + 1);
5435 return 0;
5438 static void regulator_summary_show_subtree(struct seq_file *s,
5439 struct regulator_dev *rdev,
5440 int level)
5442 struct regulation_constraints *c;
5443 struct regulator *consumer;
5444 struct summary_data summary_data;
5445 unsigned int opmode;
5447 if (!rdev)
5448 return;
5450 opmode = _regulator_get_mode_unlocked(rdev);
5451 seq_printf(s, "%*s%-*s %3d %4d %6d %7s ",
5452 level * 3 + 1, "",
5453 30 - level * 3, rdev_get_name(rdev),
5454 rdev->use_count, rdev->open_count, rdev->bypass_count,
5455 regulator_opmode_to_str(opmode));
5457 seq_printf(s, "%5dmV ", regulator_get_voltage_rdev(rdev) / 1000);
5458 seq_printf(s, "%5dmA ",
5459 _regulator_get_current_limit_unlocked(rdev) / 1000);
5461 c = rdev->constraints;
5462 if (c) {
5463 switch (rdev->desc->type) {
5464 case REGULATOR_VOLTAGE:
5465 seq_printf(s, "%5dmV %5dmV ",
5466 c->min_uV / 1000, c->max_uV / 1000);
5467 break;
5468 case REGULATOR_CURRENT:
5469 seq_printf(s, "%5dmA %5dmA ",
5470 c->min_uA / 1000, c->max_uA / 1000);
5471 break;
5475 seq_puts(s, "\n");
5477 list_for_each_entry(consumer, &rdev->consumer_list, list) {
5478 if (consumer->dev && consumer->dev->class == &regulator_class)
5479 continue;
5481 seq_printf(s, "%*s%-*s ",
5482 (level + 1) * 3 + 1, "",
5483 30 - (level + 1) * 3,
5484 consumer->dev ? dev_name(consumer->dev) : "deviceless");
5486 switch (rdev->desc->type) {
5487 case REGULATOR_VOLTAGE:
5488 seq_printf(s, "%3d %33dmA%c%5dmV %5dmV",
5489 consumer->enable_count,
5490 consumer->uA_load / 1000,
5491 consumer->uA_load && !consumer->enable_count ?
5492 '*' : ' ',
5493 consumer->voltage[PM_SUSPEND_ON].min_uV / 1000,
5494 consumer->voltage[PM_SUSPEND_ON].max_uV / 1000);
5495 break;
5496 case REGULATOR_CURRENT:
5497 break;
5500 seq_puts(s, "\n");
5503 summary_data.s = s;
5504 summary_data.level = level;
5505 summary_data.parent = rdev;
5507 class_for_each_device(&regulator_class, NULL, &summary_data,
5508 regulator_summary_show_children);
5511 struct summary_lock_data {
5512 struct ww_acquire_ctx *ww_ctx;
5513 struct regulator_dev **new_contended_rdev;
5514 struct regulator_dev **old_contended_rdev;
5517 static int regulator_summary_lock_one(struct device *dev, void *data)
5519 struct regulator_dev *rdev = dev_to_rdev(dev);
5520 struct summary_lock_data *lock_data = data;
5521 int ret = 0;
5523 if (rdev != *lock_data->old_contended_rdev) {
5524 ret = regulator_lock_nested(rdev, lock_data->ww_ctx);
5526 if (ret == -EDEADLK)
5527 *lock_data->new_contended_rdev = rdev;
5528 else
5529 WARN_ON_ONCE(ret);
5530 } else {
5531 *lock_data->old_contended_rdev = NULL;
5534 return ret;
5537 static int regulator_summary_unlock_one(struct device *dev, void *data)
5539 struct regulator_dev *rdev = dev_to_rdev(dev);
5540 struct summary_lock_data *lock_data = data;
5542 if (lock_data) {
5543 if (rdev == *lock_data->new_contended_rdev)
5544 return -EDEADLK;
5547 regulator_unlock(rdev);
5549 return 0;
5552 static int regulator_summary_lock_all(struct ww_acquire_ctx *ww_ctx,
5553 struct regulator_dev **new_contended_rdev,
5554 struct regulator_dev **old_contended_rdev)
5556 struct summary_lock_data lock_data;
5557 int ret;
5559 lock_data.ww_ctx = ww_ctx;
5560 lock_data.new_contended_rdev = new_contended_rdev;
5561 lock_data.old_contended_rdev = old_contended_rdev;
5563 ret = class_for_each_device(&regulator_class, NULL, &lock_data,
5564 regulator_summary_lock_one);
5565 if (ret)
5566 class_for_each_device(&regulator_class, NULL, &lock_data,
5567 regulator_summary_unlock_one);
5569 return ret;
5572 static void regulator_summary_lock(struct ww_acquire_ctx *ww_ctx)
5574 struct regulator_dev *new_contended_rdev = NULL;
5575 struct regulator_dev *old_contended_rdev = NULL;
5576 int err;
5578 mutex_lock(&regulator_list_mutex);
5580 ww_acquire_init(ww_ctx, &regulator_ww_class);
5582 do {
5583 if (new_contended_rdev) {
5584 ww_mutex_lock_slow(&new_contended_rdev->mutex, ww_ctx);
5585 old_contended_rdev = new_contended_rdev;
5586 old_contended_rdev->ref_cnt++;
5589 err = regulator_summary_lock_all(ww_ctx,
5590 &new_contended_rdev,
5591 &old_contended_rdev);
5593 if (old_contended_rdev)
5594 regulator_unlock(old_contended_rdev);
5596 } while (err == -EDEADLK);
5598 ww_acquire_done(ww_ctx);
5601 static void regulator_summary_unlock(struct ww_acquire_ctx *ww_ctx)
5603 class_for_each_device(&regulator_class, NULL, NULL,
5604 regulator_summary_unlock_one);
5605 ww_acquire_fini(ww_ctx);
5607 mutex_unlock(&regulator_list_mutex);
5610 static int regulator_summary_show_roots(struct device *dev, void *data)
5612 struct regulator_dev *rdev = dev_to_rdev(dev);
5613 struct seq_file *s = data;
5615 if (!rdev->supply)
5616 regulator_summary_show_subtree(s, rdev, 0);
5618 return 0;
5621 static int regulator_summary_show(struct seq_file *s, void *data)
5623 struct ww_acquire_ctx ww_ctx;
5625 seq_puts(s, " regulator use open bypass opmode voltage current min max\n");
5626 seq_puts(s, "---------------------------------------------------------------------------------------\n");
5628 regulator_summary_lock(&ww_ctx);
5630 class_for_each_device(&regulator_class, NULL, s,
5631 regulator_summary_show_roots);
5633 regulator_summary_unlock(&ww_ctx);
5635 return 0;
5637 DEFINE_SHOW_ATTRIBUTE(regulator_summary);
5638 #endif /* CONFIG_DEBUG_FS */
5640 static int __init regulator_init(void)
5642 int ret;
5644 ret = class_register(&regulator_class);
5646 debugfs_root = debugfs_create_dir("regulator", NULL);
5647 if (!debugfs_root)
5648 pr_warn("regulator: Failed to create debugfs directory\n");
5650 #ifdef CONFIG_DEBUG_FS
5651 debugfs_create_file("supply_map", 0444, debugfs_root, NULL,
5652 &supply_map_fops);
5654 debugfs_create_file("regulator_summary", 0444, debugfs_root,
5655 NULL, &regulator_summary_fops);
5656 #endif
5657 regulator_dummy_init();
5659 regulator_coupler_register(&generic_regulator_coupler);
5661 return ret;
5664 /* init early to allow our consumers to complete system booting */
5665 core_initcall(regulator_init);
5667 static int regulator_late_cleanup(struct device *dev, void *data)
5669 struct regulator_dev *rdev = dev_to_rdev(dev);
5670 const struct regulator_ops *ops = rdev->desc->ops;
5671 struct regulation_constraints *c = rdev->constraints;
5672 int enabled, ret;
5674 if (c && c->always_on)
5675 return 0;
5677 if (!regulator_ops_is_valid(rdev, REGULATOR_CHANGE_STATUS))
5678 return 0;
5680 regulator_lock(rdev);
5682 if (rdev->use_count)
5683 goto unlock;
5685 /* If we can't read the status assume it's on. */
5686 if (ops->is_enabled)
5687 enabled = ops->is_enabled(rdev);
5688 else
5689 enabled = 1;
5691 if (!enabled)
5692 goto unlock;
5694 if (have_full_constraints()) {
5695 /* We log since this may kill the system if it goes
5696 * wrong. */
5697 rdev_info(rdev, "disabling\n");
5698 ret = _regulator_do_disable(rdev);
5699 if (ret != 0)
5700 rdev_err(rdev, "couldn't disable: %d\n", ret);
5701 } else {
5702 /* The intention is that in future we will
5703 * assume that full constraints are provided
5704 * so warn even if we aren't going to do
5705 * anything here.
5707 rdev_warn(rdev, "incomplete constraints, leaving on\n");
5710 unlock:
5711 regulator_unlock(rdev);
5713 return 0;
5716 static void regulator_init_complete_work_function(struct work_struct *work)
5719 * Regulators may had failed to resolve their input supplies
5720 * when were registered, either because the input supply was
5721 * not registered yet or because its parent device was not
5722 * bound yet. So attempt to resolve the input supplies for
5723 * pending regulators before trying to disable unused ones.
5725 class_for_each_device(&regulator_class, NULL, NULL,
5726 regulator_register_resolve_supply);
5728 /* If we have a full configuration then disable any regulators
5729 * we have permission to change the status for and which are
5730 * not in use or always_on. This is effectively the default
5731 * for DT and ACPI as they have full constraints.
5733 class_for_each_device(&regulator_class, NULL, NULL,
5734 regulator_late_cleanup);
5737 static DECLARE_DELAYED_WORK(regulator_init_complete_work,
5738 regulator_init_complete_work_function);
5740 static int __init regulator_init_complete(void)
5743 * Since DT doesn't provide an idiomatic mechanism for
5744 * enabling full constraints and since it's much more natural
5745 * with DT to provide them just assume that a DT enabled
5746 * system has full constraints.
5748 if (of_have_populated_dt())
5749 has_full_constraints = true;
5752 * We punt completion for an arbitrary amount of time since
5753 * systems like distros will load many drivers from userspace
5754 * so consumers might not always be ready yet, this is
5755 * particularly an issue with laptops where this might bounce
5756 * the display off then on. Ideally we'd get a notification
5757 * from userspace when this happens but we don't so just wait
5758 * a bit and hope we waited long enough. It'd be better if
5759 * we'd only do this on systems that need it, and a kernel
5760 * command line option might be useful.
5762 schedule_delayed_work(&regulator_init_complete_work,
5763 msecs_to_jiffies(30000));
5765 return 0;
5767 late_initcall_sync(regulator_init_complete);