1 // SPDX-License-Identifier: GPL-2.0-or-later
3 // core.c -- Voltage/Current Regulator framework.
5 // Copyright 2007, 2008 Wolfson Microelectronics PLC.
6 // Copyright 2008 SlimLogic Ltd.
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>
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>
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 */
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,
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;
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");
132 if (rdev->constraints->valid_ops_mask & ops)
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
149 static inline int regulator_lock_nested(struct regulator_dev *rdev,
150 struct ww_acquire_ctx *ww_ctx)
155 mutex_lock(®ulator_nesting_mutex);
157 if (ww_ctx || !ww_mutex_trylock(&rdev->mutex)) {
158 if (rdev->mutex_owner == current)
164 mutex_unlock(®ulator_nesting_mutex);
165 ret = ww_mutex_lock(&rdev->mutex, ww_ctx);
166 mutex_lock(®ulator_nesting_mutex);
172 if (lock && ret != -EDEADLK) {
174 rdev->mutex_owner = current;
177 mutex_unlock(®ulator_nesting_mutex);
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
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(®ulator_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(®ulator_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;
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)
235 static void regulator_unlock_recursive(struct regulator_dev *rdev,
236 unsigned int n_coupled)
238 struct regulator_dev *c_rdev, *supply_rdev;
239 int i, supply_n_coupled;
241 for (i = n_coupled; i > 0; i--) {
242 c_rdev = rdev->coupling_desc.coupled_rdevs[i - 1];
247 if (c_rdev->supply && !regulator_supply_is_couple(c_rdev)) {
248 supply_rdev = c_rdev->supply->rdev;
249 supply_n_coupled = supply_rdev->coupling_desc.n_coupled;
251 regulator_unlock_recursive(supply_rdev,
255 regulator_unlock(c_rdev);
259 static int regulator_lock_recursive(struct regulator_dev *rdev,
260 struct regulator_dev **new_contended_rdev,
261 struct regulator_dev **old_contended_rdev,
262 struct ww_acquire_ctx *ww_ctx)
264 struct regulator_dev *c_rdev;
267 for (i = 0; i < rdev->coupling_desc.n_coupled; i++) {
268 c_rdev = rdev->coupling_desc.coupled_rdevs[i];
273 if (c_rdev != *old_contended_rdev) {
274 err = regulator_lock_nested(c_rdev, ww_ctx);
276 if (err == -EDEADLK) {
277 *new_contended_rdev = c_rdev;
281 /* shouldn't happen */
282 WARN_ON_ONCE(err != -EALREADY);
285 *old_contended_rdev = NULL;
288 if (c_rdev->supply && !regulator_supply_is_couple(c_rdev)) {
289 err = regulator_lock_recursive(c_rdev->supply->rdev,
294 regulator_unlock(c_rdev);
303 regulator_unlock_recursive(rdev, i);
309 * regulator_unlock_dependent - unlock regulator's suppliers and coupled
311 * @rdev: regulator source
312 * @ww_ctx: w/w mutex acquire context
314 * Unlock all regulators related with rdev by coupling or supplying.
316 static void regulator_unlock_dependent(struct regulator_dev *rdev,
317 struct ww_acquire_ctx *ww_ctx)
319 regulator_unlock_recursive(rdev, rdev->coupling_desc.n_coupled);
320 ww_acquire_fini(ww_ctx);
324 * regulator_lock_dependent - lock regulator's suppliers and coupled regulators
325 * @rdev: regulator source
326 * @ww_ctx: w/w mutex acquire context
328 * This function as a wrapper on regulator_lock_recursive(), which locks
329 * all regulators related with rdev by coupling or supplying.
331 static void regulator_lock_dependent(struct regulator_dev *rdev,
332 struct ww_acquire_ctx *ww_ctx)
334 struct regulator_dev *new_contended_rdev = NULL;
335 struct regulator_dev *old_contended_rdev = NULL;
338 mutex_lock(®ulator_list_mutex);
340 ww_acquire_init(ww_ctx, ®ulator_ww_class);
343 if (new_contended_rdev) {
344 ww_mutex_lock_slow(&new_contended_rdev->mutex, ww_ctx);
345 old_contended_rdev = new_contended_rdev;
346 old_contended_rdev->ref_cnt++;
349 err = regulator_lock_recursive(rdev,
354 if (old_contended_rdev)
355 regulator_unlock(old_contended_rdev);
357 } while (err == -EDEADLK);
359 ww_acquire_done(ww_ctx);
361 mutex_unlock(®ulator_list_mutex);
365 * of_get_child_regulator - get a child regulator device node
366 * based on supply name
367 * @parent: Parent device node
368 * @prop_name: Combination regulator supply name and "-supply"
370 * Traverse all child nodes.
371 * Extract the child regulator device node corresponding to the supply name.
372 * returns the device node corresponding to the regulator if found, else
375 static struct device_node *of_get_child_regulator(struct device_node *parent,
376 const char *prop_name)
378 struct device_node *regnode = NULL;
379 struct device_node *child = NULL;
381 for_each_child_of_node(parent, child) {
382 regnode = of_parse_phandle(child, prop_name, 0);
385 regnode = of_get_child_regulator(child, prop_name);
400 * of_get_regulator - get a regulator device node based on supply name
401 * @dev: Device pointer for the consumer (of regulator) device
402 * @supply: regulator supply name
404 * Extract the regulator device node corresponding to the supply name.
405 * returns the device node corresponding to the regulator if found, else
408 static struct device_node *of_get_regulator(struct device *dev, const char *supply)
410 struct device_node *regnode = NULL;
411 char prop_name[32]; /* 32 is max size of property name */
413 dev_dbg(dev, "Looking up %s-supply from device tree\n", supply);
415 snprintf(prop_name, 32, "%s-supply", supply);
416 regnode = of_parse_phandle(dev->of_node, prop_name, 0);
419 regnode = of_get_child_regulator(dev->of_node, prop_name);
423 dev_dbg(dev, "Looking up %s property in node %pOF failed\n",
424 prop_name, dev->of_node);
430 /* Platform voltage constraint check */
431 int regulator_check_voltage(struct regulator_dev *rdev,
432 int *min_uV, int *max_uV)
434 BUG_ON(*min_uV > *max_uV);
436 if (!regulator_ops_is_valid(rdev, REGULATOR_CHANGE_VOLTAGE)) {
437 rdev_err(rdev, "voltage operation not allowed\n");
441 if (*max_uV > rdev->constraints->max_uV)
442 *max_uV = rdev->constraints->max_uV;
443 if (*min_uV < rdev->constraints->min_uV)
444 *min_uV = rdev->constraints->min_uV;
446 if (*min_uV > *max_uV) {
447 rdev_err(rdev, "unsupportable voltage range: %d-%duV\n",
455 /* return 0 if the state is valid */
456 static int regulator_check_states(suspend_state_t state)
458 return (state > PM_SUSPEND_MAX || state == PM_SUSPEND_TO_IDLE);
461 /* Make sure we select a voltage that suits the needs of all
462 * regulator consumers
464 int regulator_check_consumers(struct regulator_dev *rdev,
465 int *min_uV, int *max_uV,
466 suspend_state_t state)
468 struct regulator *regulator;
469 struct regulator_voltage *voltage;
471 list_for_each_entry(regulator, &rdev->consumer_list, list) {
472 voltage = ®ulator->voltage[state];
474 * Assume consumers that didn't say anything are OK
475 * with anything in the constraint range.
477 if (!voltage->min_uV && !voltage->max_uV)
480 if (*max_uV > voltage->max_uV)
481 *max_uV = voltage->max_uV;
482 if (*min_uV < voltage->min_uV)
483 *min_uV = voltage->min_uV;
486 if (*min_uV > *max_uV) {
487 rdev_err(rdev, "Restricting voltage, %u-%uuV\n",
495 /* current constraint check */
496 static int regulator_check_current_limit(struct regulator_dev *rdev,
497 int *min_uA, int *max_uA)
499 BUG_ON(*min_uA > *max_uA);
501 if (!regulator_ops_is_valid(rdev, REGULATOR_CHANGE_CURRENT)) {
502 rdev_err(rdev, "current operation not allowed\n");
506 if (*max_uA > rdev->constraints->max_uA)
507 *max_uA = rdev->constraints->max_uA;
508 if (*min_uA < rdev->constraints->min_uA)
509 *min_uA = rdev->constraints->min_uA;
511 if (*min_uA > *max_uA) {
512 rdev_err(rdev, "unsupportable current range: %d-%duA\n",
520 /* operating mode constraint check */
521 static int regulator_mode_constrain(struct regulator_dev *rdev,
525 case REGULATOR_MODE_FAST:
526 case REGULATOR_MODE_NORMAL:
527 case REGULATOR_MODE_IDLE:
528 case REGULATOR_MODE_STANDBY:
531 rdev_err(rdev, "invalid mode %x specified\n", *mode);
535 if (!regulator_ops_is_valid(rdev, REGULATOR_CHANGE_MODE)) {
536 rdev_err(rdev, "mode operation not allowed\n");
540 /* The modes are bitmasks, the most power hungry modes having
541 * the lowest values. If the requested mode isn't supported
542 * try higher modes. */
544 if (rdev->constraints->valid_modes_mask & *mode)
552 static inline struct regulator_state *
553 regulator_get_suspend_state(struct regulator_dev *rdev, suspend_state_t state)
555 if (rdev->constraints == NULL)
559 case PM_SUSPEND_STANDBY:
560 return &rdev->constraints->state_standby;
562 return &rdev->constraints->state_mem;
564 return &rdev->constraints->state_disk;
570 static ssize_t regulator_uV_show(struct device *dev,
571 struct device_attribute *attr, char *buf)
573 struct regulator_dev *rdev = dev_get_drvdata(dev);
576 regulator_lock(rdev);
577 uV = regulator_get_voltage_rdev(rdev);
578 regulator_unlock(rdev);
582 return sprintf(buf, "%d\n", uV);
584 static DEVICE_ATTR(microvolts, 0444, regulator_uV_show, NULL);
586 static ssize_t regulator_uA_show(struct device *dev,
587 struct device_attribute *attr, char *buf)
589 struct regulator_dev *rdev = dev_get_drvdata(dev);
591 return sprintf(buf, "%d\n", _regulator_get_current_limit(rdev));
593 static DEVICE_ATTR(microamps, 0444, regulator_uA_show, NULL);
595 static ssize_t name_show(struct device *dev, struct device_attribute *attr,
598 struct regulator_dev *rdev = dev_get_drvdata(dev);
600 return sprintf(buf, "%s\n", rdev_get_name(rdev));
602 static DEVICE_ATTR_RO(name);
604 static const char *regulator_opmode_to_str(int mode)
607 case REGULATOR_MODE_FAST:
609 case REGULATOR_MODE_NORMAL:
611 case REGULATOR_MODE_IDLE:
613 case REGULATOR_MODE_STANDBY:
619 static ssize_t regulator_print_opmode(char *buf, int mode)
621 return sprintf(buf, "%s\n", regulator_opmode_to_str(mode));
624 static ssize_t regulator_opmode_show(struct device *dev,
625 struct device_attribute *attr, char *buf)
627 struct regulator_dev *rdev = dev_get_drvdata(dev);
629 return regulator_print_opmode(buf, _regulator_get_mode(rdev));
631 static DEVICE_ATTR(opmode, 0444, regulator_opmode_show, NULL);
633 static ssize_t regulator_print_state(char *buf, int state)
636 return sprintf(buf, "enabled\n");
638 return sprintf(buf, "disabled\n");
640 return sprintf(buf, "unknown\n");
643 static ssize_t regulator_state_show(struct device *dev,
644 struct device_attribute *attr, char *buf)
646 struct regulator_dev *rdev = dev_get_drvdata(dev);
649 regulator_lock(rdev);
650 ret = regulator_print_state(buf, _regulator_is_enabled(rdev));
651 regulator_unlock(rdev);
655 static DEVICE_ATTR(state, 0444, regulator_state_show, NULL);
657 static ssize_t regulator_status_show(struct device *dev,
658 struct device_attribute *attr, char *buf)
660 struct regulator_dev *rdev = dev_get_drvdata(dev);
664 status = rdev->desc->ops->get_status(rdev);
669 case REGULATOR_STATUS_OFF:
672 case REGULATOR_STATUS_ON:
675 case REGULATOR_STATUS_ERROR:
678 case REGULATOR_STATUS_FAST:
681 case REGULATOR_STATUS_NORMAL:
684 case REGULATOR_STATUS_IDLE:
687 case REGULATOR_STATUS_STANDBY:
690 case REGULATOR_STATUS_BYPASS:
693 case REGULATOR_STATUS_UNDEFINED:
700 return sprintf(buf, "%s\n", label);
702 static DEVICE_ATTR(status, 0444, regulator_status_show, NULL);
704 static ssize_t regulator_min_uA_show(struct device *dev,
705 struct device_attribute *attr, char *buf)
707 struct regulator_dev *rdev = dev_get_drvdata(dev);
709 if (!rdev->constraints)
710 return sprintf(buf, "constraint not defined\n");
712 return sprintf(buf, "%d\n", rdev->constraints->min_uA);
714 static DEVICE_ATTR(min_microamps, 0444, regulator_min_uA_show, NULL);
716 static ssize_t regulator_max_uA_show(struct device *dev,
717 struct device_attribute *attr, char *buf)
719 struct regulator_dev *rdev = dev_get_drvdata(dev);
721 if (!rdev->constraints)
722 return sprintf(buf, "constraint not defined\n");
724 return sprintf(buf, "%d\n", rdev->constraints->max_uA);
726 static DEVICE_ATTR(max_microamps, 0444, regulator_max_uA_show, NULL);
728 static ssize_t regulator_min_uV_show(struct device *dev,
729 struct device_attribute *attr, char *buf)
731 struct regulator_dev *rdev = dev_get_drvdata(dev);
733 if (!rdev->constraints)
734 return sprintf(buf, "constraint not defined\n");
736 return sprintf(buf, "%d\n", rdev->constraints->min_uV);
738 static DEVICE_ATTR(min_microvolts, 0444, regulator_min_uV_show, NULL);
740 static ssize_t regulator_max_uV_show(struct device *dev,
741 struct device_attribute *attr, char *buf)
743 struct regulator_dev *rdev = dev_get_drvdata(dev);
745 if (!rdev->constraints)
746 return sprintf(buf, "constraint not defined\n");
748 return sprintf(buf, "%d\n", rdev->constraints->max_uV);
750 static DEVICE_ATTR(max_microvolts, 0444, regulator_max_uV_show, NULL);
752 static ssize_t regulator_total_uA_show(struct device *dev,
753 struct device_attribute *attr, char *buf)
755 struct regulator_dev *rdev = dev_get_drvdata(dev);
756 struct regulator *regulator;
759 regulator_lock(rdev);
760 list_for_each_entry(regulator, &rdev->consumer_list, list) {
761 if (regulator->enable_count)
762 uA += regulator->uA_load;
764 regulator_unlock(rdev);
765 return sprintf(buf, "%d\n", uA);
767 static DEVICE_ATTR(requested_microamps, 0444, regulator_total_uA_show, NULL);
769 static ssize_t num_users_show(struct device *dev, struct device_attribute *attr,
772 struct regulator_dev *rdev = dev_get_drvdata(dev);
773 return sprintf(buf, "%d\n", rdev->use_count);
775 static DEVICE_ATTR_RO(num_users);
777 static ssize_t type_show(struct device *dev, struct device_attribute *attr,
780 struct regulator_dev *rdev = dev_get_drvdata(dev);
782 switch (rdev->desc->type) {
783 case REGULATOR_VOLTAGE:
784 return sprintf(buf, "voltage\n");
785 case REGULATOR_CURRENT:
786 return sprintf(buf, "current\n");
788 return sprintf(buf, "unknown\n");
790 static DEVICE_ATTR_RO(type);
792 static ssize_t regulator_suspend_mem_uV_show(struct device *dev,
793 struct device_attribute *attr, char *buf)
795 struct regulator_dev *rdev = dev_get_drvdata(dev);
797 return sprintf(buf, "%d\n", rdev->constraints->state_mem.uV);
799 static DEVICE_ATTR(suspend_mem_microvolts, 0444,
800 regulator_suspend_mem_uV_show, NULL);
802 static ssize_t regulator_suspend_disk_uV_show(struct device *dev,
803 struct device_attribute *attr, char *buf)
805 struct regulator_dev *rdev = dev_get_drvdata(dev);
807 return sprintf(buf, "%d\n", rdev->constraints->state_disk.uV);
809 static DEVICE_ATTR(suspend_disk_microvolts, 0444,
810 regulator_suspend_disk_uV_show, NULL);
812 static ssize_t regulator_suspend_standby_uV_show(struct device *dev,
813 struct device_attribute *attr, char *buf)
815 struct regulator_dev *rdev = dev_get_drvdata(dev);
817 return sprintf(buf, "%d\n", rdev->constraints->state_standby.uV);
819 static DEVICE_ATTR(suspend_standby_microvolts, 0444,
820 regulator_suspend_standby_uV_show, NULL);
822 static ssize_t regulator_suspend_mem_mode_show(struct device *dev,
823 struct device_attribute *attr, char *buf)
825 struct regulator_dev *rdev = dev_get_drvdata(dev);
827 return regulator_print_opmode(buf,
828 rdev->constraints->state_mem.mode);
830 static DEVICE_ATTR(suspend_mem_mode, 0444,
831 regulator_suspend_mem_mode_show, NULL);
833 static ssize_t regulator_suspend_disk_mode_show(struct device *dev,
834 struct device_attribute *attr, char *buf)
836 struct regulator_dev *rdev = dev_get_drvdata(dev);
838 return regulator_print_opmode(buf,
839 rdev->constraints->state_disk.mode);
841 static DEVICE_ATTR(suspend_disk_mode, 0444,
842 regulator_suspend_disk_mode_show, NULL);
844 static ssize_t regulator_suspend_standby_mode_show(struct device *dev,
845 struct device_attribute *attr, char *buf)
847 struct regulator_dev *rdev = dev_get_drvdata(dev);
849 return regulator_print_opmode(buf,
850 rdev->constraints->state_standby.mode);
852 static DEVICE_ATTR(suspend_standby_mode, 0444,
853 regulator_suspend_standby_mode_show, NULL);
855 static ssize_t regulator_suspend_mem_state_show(struct device *dev,
856 struct device_attribute *attr, char *buf)
858 struct regulator_dev *rdev = dev_get_drvdata(dev);
860 return regulator_print_state(buf,
861 rdev->constraints->state_mem.enabled);
863 static DEVICE_ATTR(suspend_mem_state, 0444,
864 regulator_suspend_mem_state_show, NULL);
866 static ssize_t regulator_suspend_disk_state_show(struct device *dev,
867 struct device_attribute *attr, char *buf)
869 struct regulator_dev *rdev = dev_get_drvdata(dev);
871 return regulator_print_state(buf,
872 rdev->constraints->state_disk.enabled);
874 static DEVICE_ATTR(suspend_disk_state, 0444,
875 regulator_suspend_disk_state_show, NULL);
877 static ssize_t regulator_suspend_standby_state_show(struct device *dev,
878 struct device_attribute *attr, char *buf)
880 struct regulator_dev *rdev = dev_get_drvdata(dev);
882 return regulator_print_state(buf,
883 rdev->constraints->state_standby.enabled);
885 static DEVICE_ATTR(suspend_standby_state, 0444,
886 regulator_suspend_standby_state_show, NULL);
888 static ssize_t regulator_bypass_show(struct device *dev,
889 struct device_attribute *attr, char *buf)
891 struct regulator_dev *rdev = dev_get_drvdata(dev);
896 ret = rdev->desc->ops->get_bypass(rdev, &bypass);
905 return sprintf(buf, "%s\n", report);
907 static DEVICE_ATTR(bypass, 0444,
908 regulator_bypass_show, NULL);
910 /* Calculate the new optimum regulator operating mode based on the new total
911 * consumer load. All locks held by caller */
912 static int drms_uA_update(struct regulator_dev *rdev)
914 struct regulator *sibling;
915 int current_uA = 0, output_uV, input_uV, err;
919 * first check to see if we can set modes at all, otherwise just
920 * tell the consumer everything is OK.
922 if (!regulator_ops_is_valid(rdev, REGULATOR_CHANGE_DRMS)) {
923 rdev_dbg(rdev, "DRMS operation not allowed\n");
927 if (!rdev->desc->ops->get_optimum_mode &&
928 !rdev->desc->ops->set_load)
931 if (!rdev->desc->ops->set_mode &&
932 !rdev->desc->ops->set_load)
935 /* calc total requested load */
936 list_for_each_entry(sibling, &rdev->consumer_list, list) {
937 if (sibling->enable_count)
938 current_uA += sibling->uA_load;
941 current_uA += rdev->constraints->system_load;
943 if (rdev->desc->ops->set_load) {
944 /* set the optimum mode for our new total regulator load */
945 err = rdev->desc->ops->set_load(rdev, current_uA);
947 rdev_err(rdev, "failed to set load %d\n", current_uA);
949 /* get output voltage */
950 output_uV = regulator_get_voltage_rdev(rdev);
951 if (output_uV <= 0) {
952 rdev_err(rdev, "invalid output voltage found\n");
956 /* get input voltage */
959 input_uV = regulator_get_voltage_rdev(rdev->supply->rdev);
961 input_uV = rdev->constraints->input_uV;
963 rdev_err(rdev, "invalid input voltage found\n");
967 /* now get the optimum mode for our new total regulator load */
968 mode = rdev->desc->ops->get_optimum_mode(rdev, input_uV,
969 output_uV, current_uA);
971 /* check the new mode is allowed */
972 err = regulator_mode_constrain(rdev, &mode);
974 rdev_err(rdev, "failed to get optimum mode @ %d uA %d -> %d uV\n",
975 current_uA, input_uV, output_uV);
979 err = rdev->desc->ops->set_mode(rdev, mode);
981 rdev_err(rdev, "failed to set optimum mode %x\n", mode);
987 static int suspend_set_state(struct regulator_dev *rdev,
988 suspend_state_t state)
991 struct regulator_state *rstate;
993 rstate = regulator_get_suspend_state(rdev, state);
997 /* If we have no suspend mode configuration don't set anything;
998 * only warn if the driver implements set_suspend_voltage or
999 * set_suspend_mode callback.
1001 if (rstate->enabled != ENABLE_IN_SUSPEND &&
1002 rstate->enabled != DISABLE_IN_SUSPEND) {
1003 if (rdev->desc->ops->set_suspend_voltage ||
1004 rdev->desc->ops->set_suspend_mode)
1005 rdev_warn(rdev, "No configuration\n");
1009 if (rstate->enabled == ENABLE_IN_SUSPEND &&
1010 rdev->desc->ops->set_suspend_enable)
1011 ret = rdev->desc->ops->set_suspend_enable(rdev);
1012 else if (rstate->enabled == DISABLE_IN_SUSPEND &&
1013 rdev->desc->ops->set_suspend_disable)
1014 ret = rdev->desc->ops->set_suspend_disable(rdev);
1015 else /* OK if set_suspend_enable or set_suspend_disable is NULL */
1019 rdev_err(rdev, "failed to enabled/disable\n");
1023 if (rdev->desc->ops->set_suspend_voltage && rstate->uV > 0) {
1024 ret = rdev->desc->ops->set_suspend_voltage(rdev, rstate->uV);
1026 rdev_err(rdev, "failed to set voltage\n");
1031 if (rdev->desc->ops->set_suspend_mode && rstate->mode > 0) {
1032 ret = rdev->desc->ops->set_suspend_mode(rdev, rstate->mode);
1034 rdev_err(rdev, "failed to set mode\n");
1042 static void print_constraints(struct regulator_dev *rdev)
1044 struct regulation_constraints *constraints = rdev->constraints;
1046 size_t len = sizeof(buf) - 1;
1050 if (constraints->min_uV && constraints->max_uV) {
1051 if (constraints->min_uV == constraints->max_uV)
1052 count += scnprintf(buf + count, len - count, "%d mV ",
1053 constraints->min_uV / 1000);
1055 count += scnprintf(buf + count, len - count,
1057 constraints->min_uV / 1000,
1058 constraints->max_uV / 1000);
1061 if (!constraints->min_uV ||
1062 constraints->min_uV != constraints->max_uV) {
1063 ret = regulator_get_voltage_rdev(rdev);
1065 count += scnprintf(buf + count, len - count,
1066 "at %d mV ", ret / 1000);
1069 if (constraints->uV_offset)
1070 count += scnprintf(buf + count, len - count, "%dmV offset ",
1071 constraints->uV_offset / 1000);
1073 if (constraints->min_uA && constraints->max_uA) {
1074 if (constraints->min_uA == constraints->max_uA)
1075 count += scnprintf(buf + count, len - count, "%d mA ",
1076 constraints->min_uA / 1000);
1078 count += scnprintf(buf + count, len - count,
1080 constraints->min_uA / 1000,
1081 constraints->max_uA / 1000);
1084 if (!constraints->min_uA ||
1085 constraints->min_uA != constraints->max_uA) {
1086 ret = _regulator_get_current_limit(rdev);
1088 count += scnprintf(buf + count, len - count,
1089 "at %d mA ", ret / 1000);
1092 if (constraints->valid_modes_mask & REGULATOR_MODE_FAST)
1093 count += scnprintf(buf + count, len - count, "fast ");
1094 if (constraints->valid_modes_mask & REGULATOR_MODE_NORMAL)
1095 count += scnprintf(buf + count, len - count, "normal ");
1096 if (constraints->valid_modes_mask & REGULATOR_MODE_IDLE)
1097 count += scnprintf(buf + count, len - count, "idle ");
1098 if (constraints->valid_modes_mask & REGULATOR_MODE_STANDBY)
1099 count += scnprintf(buf + count, len - count, "standby");
1102 scnprintf(buf, len, "no parameters");
1104 rdev_dbg(rdev, "%s\n", buf);
1106 if ((constraints->min_uV != constraints->max_uV) &&
1107 !regulator_ops_is_valid(rdev, REGULATOR_CHANGE_VOLTAGE))
1109 "Voltage range but no REGULATOR_CHANGE_VOLTAGE\n");
1112 static int machine_constraints_voltage(struct regulator_dev *rdev,
1113 struct regulation_constraints *constraints)
1115 const struct regulator_ops *ops = rdev->desc->ops;
1118 /* do we need to apply the constraint voltage */
1119 if (rdev->constraints->apply_uV &&
1120 rdev->constraints->min_uV && rdev->constraints->max_uV) {
1121 int target_min, target_max;
1122 int current_uV = regulator_get_voltage_rdev(rdev);
1124 if (current_uV == -ENOTRECOVERABLE) {
1125 /* This regulator can't be read and must be initialized */
1126 rdev_info(rdev, "Setting %d-%duV\n",
1127 rdev->constraints->min_uV,
1128 rdev->constraints->max_uV);
1129 _regulator_do_set_voltage(rdev,
1130 rdev->constraints->min_uV,
1131 rdev->constraints->max_uV);
1132 current_uV = regulator_get_voltage_rdev(rdev);
1135 if (current_uV < 0) {
1137 "failed to get the current voltage(%d)\n",
1143 * If we're below the minimum voltage move up to the
1144 * minimum voltage, if we're above the maximum voltage
1145 * then move down to the maximum.
1147 target_min = current_uV;
1148 target_max = current_uV;
1150 if (current_uV < rdev->constraints->min_uV) {
1151 target_min = rdev->constraints->min_uV;
1152 target_max = rdev->constraints->min_uV;
1155 if (current_uV > rdev->constraints->max_uV) {
1156 target_min = rdev->constraints->max_uV;
1157 target_max = rdev->constraints->max_uV;
1160 if (target_min != current_uV || target_max != current_uV) {
1161 rdev_info(rdev, "Bringing %duV into %d-%duV\n",
1162 current_uV, target_min, target_max);
1163 ret = _regulator_do_set_voltage(
1164 rdev, target_min, target_max);
1167 "failed to apply %d-%duV constraint(%d)\n",
1168 target_min, target_max, ret);
1174 /* constrain machine-level voltage specs to fit
1175 * the actual range supported by this regulator.
1177 if (ops->list_voltage && rdev->desc->n_voltages) {
1178 int count = rdev->desc->n_voltages;
1180 int min_uV = INT_MAX;
1181 int max_uV = INT_MIN;
1182 int cmin = constraints->min_uV;
1183 int cmax = constraints->max_uV;
1185 /* it's safe to autoconfigure fixed-voltage supplies
1186 and the constraints are used by list_voltage. */
1187 if (count == 1 && !cmin) {
1190 constraints->min_uV = cmin;
1191 constraints->max_uV = cmax;
1194 /* voltage constraints are optional */
1195 if ((cmin == 0) && (cmax == 0))
1198 /* else require explicit machine-level constraints */
1199 if (cmin <= 0 || cmax <= 0 || cmax < cmin) {
1200 rdev_err(rdev, "invalid voltage constraints\n");
1204 /* initial: [cmin..cmax] valid, [min_uV..max_uV] not */
1205 for (i = 0; i < count; i++) {
1208 value = ops->list_voltage(rdev, i);
1212 /* maybe adjust [min_uV..max_uV] */
1213 if (value >= cmin && value < min_uV)
1215 if (value <= cmax && value > max_uV)
1219 /* final: [min_uV..max_uV] valid iff constraints valid */
1220 if (max_uV < min_uV) {
1222 "unsupportable voltage constraints %u-%uuV\n",
1227 /* use regulator's subset of machine constraints */
1228 if (constraints->min_uV < min_uV) {
1229 rdev_dbg(rdev, "override min_uV, %d -> %d\n",
1230 constraints->min_uV, min_uV);
1231 constraints->min_uV = min_uV;
1233 if (constraints->max_uV > max_uV) {
1234 rdev_dbg(rdev, "override max_uV, %d -> %d\n",
1235 constraints->max_uV, max_uV);
1236 constraints->max_uV = max_uV;
1243 static int machine_constraints_current(struct regulator_dev *rdev,
1244 struct regulation_constraints *constraints)
1246 const struct regulator_ops *ops = rdev->desc->ops;
1249 if (!constraints->min_uA && !constraints->max_uA)
1252 if (constraints->min_uA > constraints->max_uA) {
1253 rdev_err(rdev, "Invalid current constraints\n");
1257 if (!ops->set_current_limit || !ops->get_current_limit) {
1258 rdev_warn(rdev, "Operation of current configuration missing\n");
1262 /* Set regulator current in constraints range */
1263 ret = ops->set_current_limit(rdev, constraints->min_uA,
1264 constraints->max_uA);
1266 rdev_err(rdev, "Failed to set current constraint, %d\n", ret);
1273 static int _regulator_do_enable(struct regulator_dev *rdev);
1276 * set_machine_constraints - sets regulator constraints
1277 * @rdev: regulator source
1279 * Allows platform initialisation code to define and constrain
1280 * regulator circuits e.g. valid voltage/current ranges, etc. NOTE:
1281 * Constraints *must* be set by platform code in order for some
1282 * regulator operations to proceed i.e. set_voltage, set_current_limit,
1285 static int set_machine_constraints(struct regulator_dev *rdev)
1288 const struct regulator_ops *ops = rdev->desc->ops;
1290 ret = machine_constraints_voltage(rdev, rdev->constraints);
1294 ret = machine_constraints_current(rdev, rdev->constraints);
1298 if (rdev->constraints->ilim_uA && ops->set_input_current_limit) {
1299 ret = ops->set_input_current_limit(rdev,
1300 rdev->constraints->ilim_uA);
1302 rdev_err(rdev, "failed to set input limit\n");
1307 /* do we need to setup our suspend state */
1308 if (rdev->constraints->initial_state) {
1309 ret = suspend_set_state(rdev, rdev->constraints->initial_state);
1311 rdev_err(rdev, "failed to set suspend state\n");
1316 if (rdev->constraints->initial_mode) {
1317 if (!ops->set_mode) {
1318 rdev_err(rdev, "no set_mode operation\n");
1322 ret = ops->set_mode(rdev, rdev->constraints->initial_mode);
1324 rdev_err(rdev, "failed to set initial mode: %d\n", ret);
1327 } else if (rdev->constraints->system_load) {
1329 * We'll only apply the initial system load if an
1330 * initial mode wasn't specified.
1332 drms_uA_update(rdev);
1335 if ((rdev->constraints->ramp_delay || rdev->constraints->ramp_disable)
1336 && ops->set_ramp_delay) {
1337 ret = ops->set_ramp_delay(rdev, rdev->constraints->ramp_delay);
1339 rdev_err(rdev, "failed to set ramp_delay\n");
1344 if (rdev->constraints->pull_down && ops->set_pull_down) {
1345 ret = ops->set_pull_down(rdev);
1347 rdev_err(rdev, "failed to set pull down\n");
1352 if (rdev->constraints->soft_start && ops->set_soft_start) {
1353 ret = ops->set_soft_start(rdev);
1355 rdev_err(rdev, "failed to set soft start\n");
1360 if (rdev->constraints->over_current_protection
1361 && ops->set_over_current_protection) {
1362 ret = ops->set_over_current_protection(rdev);
1364 rdev_err(rdev, "failed to set over current protection\n");
1369 if (rdev->constraints->active_discharge && ops->set_active_discharge) {
1370 bool ad_state = (rdev->constraints->active_discharge ==
1371 REGULATOR_ACTIVE_DISCHARGE_ENABLE) ? true : false;
1373 ret = ops->set_active_discharge(rdev, ad_state);
1375 rdev_err(rdev, "failed to set active discharge\n");
1380 /* If the constraints say the regulator should be on at this point
1381 * and we have control then make sure it is enabled.
1383 if (rdev->constraints->always_on || rdev->constraints->boot_on) {
1384 /* If we want to enable this regulator, make sure that we know
1385 * the supplying regulator.
1387 if (rdev->supply_name && !rdev->supply)
1388 return -EPROBE_DEFER;
1390 /* If supplying regulator has already been enabled,
1391 * it's not intended to have use_count increment
1392 * when rdev is only boot-on.
1395 (rdev->constraints->always_on ||
1396 !regulator_is_enabled(rdev->supply))) {
1397 ret = regulator_enable(rdev->supply);
1399 _regulator_put(rdev->supply);
1400 rdev->supply = NULL;
1405 ret = _regulator_do_enable(rdev);
1406 if (ret < 0 && ret != -EINVAL) {
1407 rdev_err(rdev, "failed to enable\n");
1411 if (rdev->constraints->always_on)
1415 print_constraints(rdev);
1420 * set_supply - set regulator supply regulator
1421 * @rdev: regulator name
1422 * @supply_rdev: supply regulator name
1424 * Called by platform initialisation code to set the supply regulator for this
1425 * regulator. This ensures that a regulators supply will also be enabled by the
1426 * core if it's child is enabled.
1428 static int set_supply(struct regulator_dev *rdev,
1429 struct regulator_dev *supply_rdev)
1433 rdev_info(rdev, "supplied by %s\n", rdev_get_name(supply_rdev));
1435 if (!try_module_get(supply_rdev->owner))
1438 rdev->supply = create_regulator(supply_rdev, &rdev->dev, "SUPPLY");
1439 if (rdev->supply == NULL) {
1440 module_put(supply_rdev->owner);
1444 supply_rdev->open_count++;
1450 * set_consumer_device_supply - Bind a regulator to a symbolic supply
1451 * @rdev: regulator source
1452 * @consumer_dev_name: dev_name() string for device supply applies to
1453 * @supply: symbolic name for supply
1455 * Allows platform initialisation code to map physical regulator
1456 * sources to symbolic names for supplies for use by devices. Devices
1457 * should use these symbolic names to request regulators, avoiding the
1458 * need to provide board-specific regulator names as platform data.
1460 static int set_consumer_device_supply(struct regulator_dev *rdev,
1461 const char *consumer_dev_name,
1464 struct regulator_map *node, *new_node;
1470 if (consumer_dev_name != NULL)
1475 new_node = kzalloc(sizeof(struct regulator_map), GFP_KERNEL);
1476 if (new_node == NULL)
1479 new_node->regulator = rdev;
1480 new_node->supply = supply;
1483 new_node->dev_name = kstrdup(consumer_dev_name, GFP_KERNEL);
1484 if (new_node->dev_name == NULL) {
1490 mutex_lock(®ulator_list_mutex);
1491 list_for_each_entry(node, ®ulator_map_list, list) {
1492 if (node->dev_name && consumer_dev_name) {
1493 if (strcmp(node->dev_name, consumer_dev_name) != 0)
1495 } else if (node->dev_name || consumer_dev_name) {
1499 if (strcmp(node->supply, supply) != 0)
1502 pr_debug("%s: %s/%s is '%s' supply; fail %s/%s\n",
1504 dev_name(&node->regulator->dev),
1505 node->regulator->desc->name,
1507 dev_name(&rdev->dev), rdev_get_name(rdev));
1511 list_add(&new_node->list, ®ulator_map_list);
1512 mutex_unlock(®ulator_list_mutex);
1517 mutex_unlock(®ulator_list_mutex);
1518 kfree(new_node->dev_name);
1523 static void unset_regulator_supplies(struct regulator_dev *rdev)
1525 struct regulator_map *node, *n;
1527 list_for_each_entry_safe(node, n, ®ulator_map_list, list) {
1528 if (rdev == node->regulator) {
1529 list_del(&node->list);
1530 kfree(node->dev_name);
1536 #ifdef CONFIG_DEBUG_FS
1537 static ssize_t constraint_flags_read_file(struct file *file,
1538 char __user *user_buf,
1539 size_t count, loff_t *ppos)
1541 const struct regulator *regulator = file->private_data;
1542 const struct regulation_constraints *c = regulator->rdev->constraints;
1549 buf = kmalloc(PAGE_SIZE, GFP_KERNEL);
1553 ret = snprintf(buf, PAGE_SIZE,
1557 "ramp_disable: %u\n"
1560 "over_current_protection: %u\n",
1567 c->over_current_protection);
1569 ret = simple_read_from_buffer(user_buf, count, ppos, buf, ret);
1577 static const struct file_operations constraint_flags_fops = {
1578 #ifdef CONFIG_DEBUG_FS
1579 .open = simple_open,
1580 .read = constraint_flags_read_file,
1581 .llseek = default_llseek,
1585 #define REG_STR_SIZE 64
1587 static struct regulator *create_regulator(struct regulator_dev *rdev,
1589 const char *supply_name)
1591 struct regulator *regulator;
1595 char buf[REG_STR_SIZE];
1598 size = snprintf(buf, REG_STR_SIZE, "%s-%s",
1599 dev->kobj.name, supply_name);
1600 if (size >= REG_STR_SIZE)
1603 supply_name = kstrdup(buf, GFP_KERNEL);
1604 if (supply_name == NULL)
1607 supply_name = kstrdup_const(supply_name, GFP_KERNEL);
1608 if (supply_name == NULL)
1612 regulator = kzalloc(sizeof(*regulator), GFP_KERNEL);
1613 if (regulator == NULL) {
1614 kfree_const(supply_name);
1618 regulator->rdev = rdev;
1619 regulator->supply_name = supply_name;
1621 regulator_lock(rdev);
1622 list_add(®ulator->list, &rdev->consumer_list);
1623 regulator_unlock(rdev);
1626 regulator->dev = dev;
1628 /* Add a link to the device sysfs entry */
1629 err = sysfs_create_link_nowarn(&rdev->dev.kobj, &dev->kobj,
1632 rdev_dbg(rdev, "could not add device link %s err %d\n",
1633 dev->kobj.name, err);
1639 regulator->debugfs = debugfs_create_dir(supply_name, rdev->debugfs);
1640 if (!regulator->debugfs) {
1641 rdev_dbg(rdev, "Failed to create debugfs directory\n");
1643 debugfs_create_u32("uA_load", 0444, regulator->debugfs,
1644 ®ulator->uA_load);
1645 debugfs_create_u32("min_uV", 0444, regulator->debugfs,
1646 ®ulator->voltage[PM_SUSPEND_ON].min_uV);
1647 debugfs_create_u32("max_uV", 0444, regulator->debugfs,
1648 ®ulator->voltage[PM_SUSPEND_ON].max_uV);
1649 debugfs_create_file("constraint_flags", 0444,
1650 regulator->debugfs, regulator,
1651 &constraint_flags_fops);
1655 * Check now if the regulator is an always on regulator - if
1656 * it is then we don't need to do nearly so much work for
1657 * enable/disable calls.
1659 if (!regulator_ops_is_valid(rdev, REGULATOR_CHANGE_STATUS) &&
1660 _regulator_is_enabled(rdev))
1661 regulator->always_on = true;
1666 static int _regulator_get_enable_time(struct regulator_dev *rdev)
1668 if (rdev->constraints && rdev->constraints->enable_time)
1669 return rdev->constraints->enable_time;
1670 if (rdev->desc->ops->enable_time)
1671 return rdev->desc->ops->enable_time(rdev);
1672 return rdev->desc->enable_time;
1675 static struct regulator_supply_alias *regulator_find_supply_alias(
1676 struct device *dev, const char *supply)
1678 struct regulator_supply_alias *map;
1680 list_for_each_entry(map, ®ulator_supply_alias_list, list)
1681 if (map->src_dev == dev && strcmp(map->src_supply, supply) == 0)
1687 static void regulator_supply_alias(struct device **dev, const char **supply)
1689 struct regulator_supply_alias *map;
1691 map = regulator_find_supply_alias(*dev, *supply);
1693 dev_dbg(*dev, "Mapping supply %s to %s,%s\n",
1694 *supply, map->alias_supply,
1695 dev_name(map->alias_dev));
1696 *dev = map->alias_dev;
1697 *supply = map->alias_supply;
1701 static int regulator_match(struct device *dev, const void *data)
1703 struct regulator_dev *r = dev_to_rdev(dev);
1705 return strcmp(rdev_get_name(r), data) == 0;
1708 static struct regulator_dev *regulator_lookup_by_name(const char *name)
1712 dev = class_find_device(®ulator_class, NULL, name, regulator_match);
1714 return dev ? dev_to_rdev(dev) : NULL;
1718 * regulator_dev_lookup - lookup a regulator device.
1719 * @dev: device for regulator "consumer".
1720 * @supply: Supply name or regulator ID.
1722 * If successful, returns a struct regulator_dev that corresponds to the name
1723 * @supply and with the embedded struct device refcount incremented by one.
1724 * The refcount must be dropped by calling put_device().
1725 * On failure one of the following ERR-PTR-encoded values is returned:
1726 * -ENODEV if lookup fails permanently, -EPROBE_DEFER if lookup could succeed
1729 static struct regulator_dev *regulator_dev_lookup(struct device *dev,
1732 struct regulator_dev *r = NULL;
1733 struct device_node *node;
1734 struct regulator_map *map;
1735 const char *devname = NULL;
1737 regulator_supply_alias(&dev, &supply);
1739 /* first do a dt based lookup */
1740 if (dev && dev->of_node) {
1741 node = of_get_regulator(dev, supply);
1743 r = of_find_regulator_by_node(node);
1749 * We have a node, but there is no device.
1750 * assume it has not registered yet.
1752 return ERR_PTR(-EPROBE_DEFER);
1756 /* if not found, try doing it non-dt way */
1758 devname = dev_name(dev);
1760 mutex_lock(®ulator_list_mutex);
1761 list_for_each_entry(map, ®ulator_map_list, list) {
1762 /* If the mapping has a device set up it must match */
1763 if (map->dev_name &&
1764 (!devname || strcmp(map->dev_name, devname)))
1767 if (strcmp(map->supply, supply) == 0 &&
1768 get_device(&map->regulator->dev)) {
1773 mutex_unlock(®ulator_list_mutex);
1778 r = regulator_lookup_by_name(supply);
1782 return ERR_PTR(-ENODEV);
1785 static int regulator_resolve_supply(struct regulator_dev *rdev)
1787 struct regulator_dev *r;
1788 struct device *dev = rdev->dev.parent;
1791 /* No supply to resolve? */
1792 if (!rdev->supply_name)
1795 /* Supply already resolved? (fast-path without locking contention) */
1799 r = regulator_dev_lookup(dev, rdev->supply_name);
1803 /* Did the lookup explicitly defer for us? */
1804 if (ret == -EPROBE_DEFER)
1807 if (have_full_constraints()) {
1808 r = dummy_regulator_rdev;
1809 get_device(&r->dev);
1811 dev_err(dev, "Failed to resolve %s-supply for %s\n",
1812 rdev->supply_name, rdev->desc->name);
1813 ret = -EPROBE_DEFER;
1819 dev_err(dev, "Supply for %s (%s) resolved to itself\n",
1820 rdev->desc->name, rdev->supply_name);
1821 if (!have_full_constraints()) {
1825 r = dummy_regulator_rdev;
1826 get_device(&r->dev);
1830 * If the supply's parent device is not the same as the
1831 * regulator's parent device, then ensure the parent device
1832 * is bound before we resolve the supply, in case the parent
1833 * device get probe deferred and unregisters the supply.
1835 if (r->dev.parent && r->dev.parent != rdev->dev.parent) {
1836 if (!device_is_bound(r->dev.parent)) {
1837 put_device(&r->dev);
1838 ret = -EPROBE_DEFER;
1843 /* Recursively resolve the supply of the supply */
1844 ret = regulator_resolve_supply(r);
1846 put_device(&r->dev);
1851 * Recheck rdev->supply with rdev->mutex lock held to avoid a race
1852 * between rdev->supply null check and setting rdev->supply in
1853 * set_supply() from concurrent tasks.
1855 regulator_lock(rdev);
1857 /* Supply just resolved by a concurrent task? */
1859 regulator_unlock(rdev);
1860 put_device(&r->dev);
1864 ret = set_supply(rdev, r);
1866 regulator_unlock(rdev);
1867 put_device(&r->dev);
1871 regulator_unlock(rdev);
1874 * In set_machine_constraints() we may have turned this regulator on
1875 * but we couldn't propagate to the supply if it hadn't been resolved
1878 if (rdev->use_count) {
1879 ret = regulator_enable(rdev->supply);
1881 _regulator_put(rdev->supply);
1882 rdev->supply = NULL;
1891 /* Internal regulator request function */
1892 struct regulator *_regulator_get(struct device *dev, const char *id,
1893 enum regulator_get_type get_type)
1895 struct regulator_dev *rdev;
1896 struct regulator *regulator;
1897 const char *devname = dev ? dev_name(dev) : "deviceless";
1900 if (get_type >= MAX_GET_TYPE) {
1901 dev_err(dev, "invalid type %d in %s\n", get_type, __func__);
1902 return ERR_PTR(-EINVAL);
1906 pr_err("get() with no identifier\n");
1907 return ERR_PTR(-EINVAL);
1910 rdev = regulator_dev_lookup(dev, id);
1912 ret = PTR_ERR(rdev);
1915 * If regulator_dev_lookup() fails with error other
1916 * than -ENODEV our job here is done, we simply return it.
1919 return ERR_PTR(ret);
1921 if (!have_full_constraints()) {
1923 "incomplete constraints, dummy supplies not allowed\n");
1924 return ERR_PTR(-ENODEV);
1930 * Assume that a regulator is physically present and
1931 * enabled, even if it isn't hooked up, and just
1935 "%s supply %s not found, using dummy regulator\n",
1937 rdev = dummy_regulator_rdev;
1938 get_device(&rdev->dev);
1943 "dummy supplies not allowed for exclusive requests\n");
1947 return ERR_PTR(-ENODEV);
1951 if (rdev->exclusive) {
1952 regulator = ERR_PTR(-EPERM);
1953 put_device(&rdev->dev);
1957 if (get_type == EXCLUSIVE_GET && rdev->open_count) {
1958 regulator = ERR_PTR(-EBUSY);
1959 put_device(&rdev->dev);
1963 mutex_lock(®ulator_list_mutex);
1964 ret = (rdev->coupling_desc.n_resolved != rdev->coupling_desc.n_coupled);
1965 mutex_unlock(®ulator_list_mutex);
1968 regulator = ERR_PTR(-EPROBE_DEFER);
1969 put_device(&rdev->dev);
1973 ret = regulator_resolve_supply(rdev);
1975 regulator = ERR_PTR(ret);
1976 put_device(&rdev->dev);
1980 if (!try_module_get(rdev->owner)) {
1981 regulator = ERR_PTR(-EPROBE_DEFER);
1982 put_device(&rdev->dev);
1986 regulator = create_regulator(rdev, dev, id);
1987 if (regulator == NULL) {
1988 regulator = ERR_PTR(-ENOMEM);
1989 module_put(rdev->owner);
1990 put_device(&rdev->dev);
1995 if (get_type == EXCLUSIVE_GET) {
1996 rdev->exclusive = 1;
1998 ret = _regulator_is_enabled(rdev);
2000 rdev->use_count = 1;
2001 regulator->enable_count = 1;
2003 rdev->use_count = 0;
2004 regulator->enable_count = 0;
2008 device_link_add(dev, &rdev->dev, DL_FLAG_STATELESS);
2014 * regulator_get - lookup and obtain a reference to a regulator.
2015 * @dev: device for regulator "consumer"
2016 * @id: Supply name or regulator ID.
2018 * Returns a struct regulator corresponding to the regulator producer,
2019 * or IS_ERR() condition containing errno.
2021 * Use of supply names configured via regulator_set_device_supply() is
2022 * strongly encouraged. It is recommended that the supply name used
2023 * should match the name used for the supply and/or the relevant
2024 * device pins in the datasheet.
2026 struct regulator *regulator_get(struct device *dev, const char *id)
2028 return _regulator_get(dev, id, NORMAL_GET);
2030 EXPORT_SYMBOL_GPL(regulator_get);
2033 * regulator_get_exclusive - obtain exclusive access to a regulator.
2034 * @dev: device for regulator "consumer"
2035 * @id: Supply name or regulator ID.
2037 * Returns a struct regulator corresponding to the regulator producer,
2038 * or IS_ERR() condition containing errno. Other consumers will be
2039 * unable to obtain this regulator while this reference is held and the
2040 * use count for the regulator will be initialised to reflect the current
2041 * state of the regulator.
2043 * This is intended for use by consumers which cannot tolerate shared
2044 * use of the regulator such as those which need to force the
2045 * regulator off for correct operation of the hardware they are
2048 * Use of supply names configured via regulator_set_device_supply() is
2049 * strongly encouraged. It is recommended that the supply name used
2050 * should match the name used for the supply and/or the relevant
2051 * device pins in the datasheet.
2053 struct regulator *regulator_get_exclusive(struct device *dev, const char *id)
2055 return _regulator_get(dev, id, EXCLUSIVE_GET);
2057 EXPORT_SYMBOL_GPL(regulator_get_exclusive);
2060 * regulator_get_optional - obtain optional access to a regulator.
2061 * @dev: device for regulator "consumer"
2062 * @id: Supply name or regulator ID.
2064 * Returns a struct regulator corresponding to the regulator producer,
2065 * or IS_ERR() condition containing errno.
2067 * This is intended for use by consumers for devices which can have
2068 * some supplies unconnected in normal use, such as some MMC devices.
2069 * It can allow the regulator core to provide stub supplies for other
2070 * supplies requested using normal regulator_get() calls without
2071 * disrupting the operation of drivers that can handle absent
2074 * Use of supply names configured via regulator_set_device_supply() is
2075 * strongly encouraged. It is recommended that the supply name used
2076 * should match the name used for the supply and/or the relevant
2077 * device pins in the datasheet.
2079 struct regulator *regulator_get_optional(struct device *dev, const char *id)
2081 return _regulator_get(dev, id, OPTIONAL_GET);
2083 EXPORT_SYMBOL_GPL(regulator_get_optional);
2085 /* regulator_list_mutex lock held by regulator_put() */
2086 static void _regulator_put(struct regulator *regulator)
2088 struct regulator_dev *rdev;
2090 if (IS_ERR_OR_NULL(regulator))
2093 lockdep_assert_held_once(®ulator_list_mutex);
2095 /* Docs say you must disable before calling regulator_put() */
2096 WARN_ON(regulator->enable_count);
2098 rdev = regulator->rdev;
2100 debugfs_remove_recursive(regulator->debugfs);
2102 if (regulator->dev) {
2103 device_link_remove(regulator->dev, &rdev->dev);
2105 /* remove any sysfs entries */
2106 sysfs_remove_link(&rdev->dev.kobj, regulator->supply_name);
2109 regulator_lock(rdev);
2110 list_del(®ulator->list);
2113 rdev->exclusive = 0;
2114 regulator_unlock(rdev);
2116 kfree_const(regulator->supply_name);
2119 module_put(rdev->owner);
2120 put_device(&rdev->dev);
2124 * regulator_put - "free" the regulator source
2125 * @regulator: regulator source
2127 * Note: drivers must ensure that all regulator_enable calls made on this
2128 * regulator source are balanced by regulator_disable calls prior to calling
2131 void regulator_put(struct regulator *regulator)
2133 mutex_lock(®ulator_list_mutex);
2134 _regulator_put(regulator);
2135 mutex_unlock(®ulator_list_mutex);
2137 EXPORT_SYMBOL_GPL(regulator_put);
2140 * regulator_register_supply_alias - Provide device alias for supply lookup
2142 * @dev: device that will be given as the regulator "consumer"
2143 * @id: Supply name or regulator ID
2144 * @alias_dev: device that should be used to lookup the supply
2145 * @alias_id: Supply name or regulator ID that should be used to lookup the
2148 * All lookups for id on dev will instead be conducted for alias_id on
2151 int regulator_register_supply_alias(struct device *dev, const char *id,
2152 struct device *alias_dev,
2153 const char *alias_id)
2155 struct regulator_supply_alias *map;
2157 map = regulator_find_supply_alias(dev, id);
2161 map = kzalloc(sizeof(struct regulator_supply_alias), GFP_KERNEL);
2166 map->src_supply = id;
2167 map->alias_dev = alias_dev;
2168 map->alias_supply = alias_id;
2170 list_add(&map->list, ®ulator_supply_alias_list);
2172 pr_info("Adding alias for supply %s,%s -> %s,%s\n",
2173 id, dev_name(dev), alias_id, dev_name(alias_dev));
2177 EXPORT_SYMBOL_GPL(regulator_register_supply_alias);
2180 * regulator_unregister_supply_alias - Remove device alias
2182 * @dev: device that will be given as the regulator "consumer"
2183 * @id: Supply name or regulator ID
2185 * Remove a lookup alias if one exists for id on dev.
2187 void regulator_unregister_supply_alias(struct device *dev, const char *id)
2189 struct regulator_supply_alias *map;
2191 map = regulator_find_supply_alias(dev, id);
2193 list_del(&map->list);
2197 EXPORT_SYMBOL_GPL(regulator_unregister_supply_alias);
2200 * regulator_bulk_register_supply_alias - register multiple aliases
2202 * @dev: device that will be given as the regulator "consumer"
2203 * @id: List of supply names or regulator IDs
2204 * @alias_dev: device that should be used to lookup the supply
2205 * @alias_id: List of supply names or regulator IDs that should be used to
2207 * @num_id: Number of aliases to register
2209 * @return 0 on success, an errno on failure.
2211 * This helper function allows drivers to register several supply
2212 * aliases in one operation. If any of the aliases cannot be
2213 * registered any aliases that were registered will be removed
2214 * before returning to the caller.
2216 int regulator_bulk_register_supply_alias(struct device *dev,
2217 const char *const *id,
2218 struct device *alias_dev,
2219 const char *const *alias_id,
2225 for (i = 0; i < num_id; ++i) {
2226 ret = regulator_register_supply_alias(dev, id[i], alias_dev,
2236 "Failed to create supply alias %s,%s -> %s,%s\n",
2237 id[i], dev_name(dev), alias_id[i], dev_name(alias_dev));
2240 regulator_unregister_supply_alias(dev, id[i]);
2244 EXPORT_SYMBOL_GPL(regulator_bulk_register_supply_alias);
2247 * regulator_bulk_unregister_supply_alias - unregister multiple aliases
2249 * @dev: device that will be given as the regulator "consumer"
2250 * @id: List of supply names or regulator IDs
2251 * @num_id: Number of aliases to unregister
2253 * This helper function allows drivers to unregister several supply
2254 * aliases in one operation.
2256 void regulator_bulk_unregister_supply_alias(struct device *dev,
2257 const char *const *id,
2262 for (i = 0; i < num_id; ++i)
2263 regulator_unregister_supply_alias(dev, id[i]);
2265 EXPORT_SYMBOL_GPL(regulator_bulk_unregister_supply_alias);
2268 /* Manage enable GPIO list. Same GPIO pin can be shared among regulators */
2269 static int regulator_ena_gpio_request(struct regulator_dev *rdev,
2270 const struct regulator_config *config)
2272 struct regulator_enable_gpio *pin, *new_pin;
2273 struct gpio_desc *gpiod;
2275 gpiod = config->ena_gpiod;
2276 new_pin = kzalloc(sizeof(*new_pin), GFP_KERNEL);
2278 mutex_lock(®ulator_list_mutex);
2280 list_for_each_entry(pin, ®ulator_ena_gpio_list, list) {
2281 if (pin->gpiod == gpiod) {
2282 rdev_dbg(rdev, "GPIO is already used\n");
2283 goto update_ena_gpio_to_rdev;
2287 if (new_pin == NULL) {
2288 mutex_unlock(®ulator_list_mutex);
2296 list_add(&pin->list, ®ulator_ena_gpio_list);
2298 update_ena_gpio_to_rdev:
2299 pin->request_count++;
2300 rdev->ena_pin = pin;
2302 mutex_unlock(®ulator_list_mutex);
2308 static void regulator_ena_gpio_free(struct regulator_dev *rdev)
2310 struct regulator_enable_gpio *pin, *n;
2315 /* Free the GPIO only in case of no use */
2316 list_for_each_entry_safe(pin, n, ®ulator_ena_gpio_list, list) {
2317 if (pin->gpiod == rdev->ena_pin->gpiod) {
2318 if (pin->request_count <= 1) {
2319 pin->request_count = 0;
2320 gpiod_put(pin->gpiod);
2321 list_del(&pin->list);
2323 rdev->ena_pin = NULL;
2326 pin->request_count--;
2333 * regulator_ena_gpio_ctrl - balance enable_count of each GPIO and actual GPIO pin control
2334 * @rdev: regulator_dev structure
2335 * @enable: enable GPIO at initial use?
2337 * GPIO is enabled in case of initial use. (enable_count is 0)
2338 * GPIO is disabled when it is not shared any more. (enable_count <= 1)
2340 static int regulator_ena_gpio_ctrl(struct regulator_dev *rdev, bool enable)
2342 struct regulator_enable_gpio *pin = rdev->ena_pin;
2348 /* Enable GPIO at initial use */
2349 if (pin->enable_count == 0)
2350 gpiod_set_value_cansleep(pin->gpiod, 1);
2352 pin->enable_count++;
2354 if (pin->enable_count > 1) {
2355 pin->enable_count--;
2359 /* Disable GPIO if not used */
2360 if (pin->enable_count <= 1) {
2361 gpiod_set_value_cansleep(pin->gpiod, 0);
2362 pin->enable_count = 0;
2370 * _regulator_enable_delay - a delay helper function
2371 * @delay: time to delay in microseconds
2373 * Delay for the requested amount of time as per the guidelines in:
2375 * Documentation/timers/timers-howto.rst
2377 * The assumption here is that regulators will never be enabled in
2378 * atomic context and therefore sleeping functions can be used.
2380 static void _regulator_enable_delay(unsigned int delay)
2382 unsigned int ms = delay / 1000;
2383 unsigned int us = delay % 1000;
2387 * For small enough values, handle super-millisecond
2388 * delays in the usleep_range() call below.
2397 * Give the scheduler some room to coalesce with any other
2398 * wakeup sources. For delays shorter than 10 us, don't even
2399 * bother setting up high-resolution timers and just busy-
2403 usleep_range(us, us + 100);
2408 static int _regulator_do_enable(struct regulator_dev *rdev)
2412 /* Query before enabling in case configuration dependent. */
2413 ret = _regulator_get_enable_time(rdev);
2417 rdev_warn(rdev, "enable_time() failed: %d\n", ret);
2421 trace_regulator_enable(rdev_get_name(rdev));
2423 if (rdev->desc->off_on_delay) {
2424 /* if needed, keep a distance of off_on_delay from last time
2425 * this regulator was disabled.
2427 unsigned long start_jiffy = jiffies;
2428 unsigned long intended, max_delay, remaining;
2430 max_delay = usecs_to_jiffies(rdev->desc->off_on_delay);
2431 intended = rdev->last_off_jiffy + max_delay;
2433 if (time_before(start_jiffy, intended)) {
2434 /* calc remaining jiffies to deal with one-time
2436 * in case of multiple timer wrapping, either it can be
2437 * detected by out-of-range remaining, or it cannot be
2438 * detected and we get a penalty of
2439 * _regulator_enable_delay().
2441 remaining = intended - start_jiffy;
2442 if (remaining <= max_delay)
2443 _regulator_enable_delay(
2444 jiffies_to_usecs(remaining));
2448 if (rdev->ena_pin) {
2449 if (!rdev->ena_gpio_state) {
2450 ret = regulator_ena_gpio_ctrl(rdev, true);
2453 rdev->ena_gpio_state = 1;
2455 } else if (rdev->desc->ops->enable) {
2456 ret = rdev->desc->ops->enable(rdev);
2463 /* Allow the regulator to ramp; it would be useful to extend
2464 * this for bulk operations so that the regulators can ramp
2466 trace_regulator_enable_delay(rdev_get_name(rdev));
2468 _regulator_enable_delay(delay);
2470 trace_regulator_enable_complete(rdev_get_name(rdev));
2476 * _regulator_handle_consumer_enable - handle that a consumer enabled
2477 * @regulator: regulator source
2479 * Some things on a regulator consumer (like the contribution towards total
2480 * load on the regulator) only have an effect when the consumer wants the
2481 * regulator enabled. Explained in example with two consumers of the same
2483 * consumer A: set_load(100); => total load = 0
2484 * consumer A: regulator_enable(); => total load = 100
2485 * consumer B: set_load(1000); => total load = 100
2486 * consumer B: regulator_enable(); => total load = 1100
2487 * consumer A: regulator_disable(); => total_load = 1000
2489 * This function (together with _regulator_handle_consumer_disable) is
2490 * responsible for keeping track of the refcount for a given regulator consumer
2491 * and applying / unapplying these things.
2493 * Returns 0 upon no error; -error upon error.
2495 static int _regulator_handle_consumer_enable(struct regulator *regulator)
2498 struct regulator_dev *rdev = regulator->rdev;
2500 lockdep_assert_held_once(&rdev->mutex.base);
2502 regulator->enable_count++;
2503 if (regulator->uA_load && regulator->enable_count == 1) {
2504 ret = drms_uA_update(rdev);
2506 regulator->enable_count--;
2514 * _regulator_handle_consumer_disable - handle that a consumer disabled
2515 * @regulator: regulator source
2517 * The opposite of _regulator_handle_consumer_enable().
2519 * Returns 0 upon no error; -error upon error.
2521 static int _regulator_handle_consumer_disable(struct regulator *regulator)
2523 struct regulator_dev *rdev = regulator->rdev;
2525 lockdep_assert_held_once(&rdev->mutex.base);
2527 if (!regulator->enable_count) {
2528 rdev_err(rdev, "Underflow of regulator enable count\n");
2532 regulator->enable_count--;
2533 if (regulator->uA_load && regulator->enable_count == 0)
2534 return drms_uA_update(rdev);
2539 /* locks held by regulator_enable() */
2540 static int _regulator_enable(struct regulator *regulator)
2542 struct regulator_dev *rdev = regulator->rdev;
2545 lockdep_assert_held_once(&rdev->mutex.base);
2547 if (rdev->use_count == 0 && rdev->supply) {
2548 ret = _regulator_enable(rdev->supply);
2553 /* balance only if there are regulators coupled */
2554 if (rdev->coupling_desc.n_coupled > 1) {
2555 ret = regulator_balance_voltage(rdev, PM_SUSPEND_ON);
2557 goto err_disable_supply;
2560 ret = _regulator_handle_consumer_enable(regulator);
2562 goto err_disable_supply;
2564 if (rdev->use_count == 0) {
2565 /* The regulator may on if it's not switchable or left on */
2566 ret = _regulator_is_enabled(rdev);
2567 if (ret == -EINVAL || ret == 0) {
2568 if (!regulator_ops_is_valid(rdev,
2569 REGULATOR_CHANGE_STATUS)) {
2571 goto err_consumer_disable;
2574 ret = _regulator_do_enable(rdev);
2576 goto err_consumer_disable;
2578 _notifier_call_chain(rdev, REGULATOR_EVENT_ENABLE,
2580 } else if (ret < 0) {
2581 rdev_err(rdev, "is_enabled() failed: %d\n", ret);
2582 goto err_consumer_disable;
2584 /* Fallthrough on positive return values - already enabled */
2591 err_consumer_disable:
2592 _regulator_handle_consumer_disable(regulator);
2595 if (rdev->use_count == 0 && rdev->supply)
2596 _regulator_disable(rdev->supply);
2602 * regulator_enable - enable regulator output
2603 * @regulator: regulator source
2605 * Request that the regulator be enabled with the regulator output at
2606 * the predefined voltage or current value. Calls to regulator_enable()
2607 * must be balanced with calls to regulator_disable().
2609 * NOTE: the output value can be set by other drivers, boot loader or may be
2610 * hardwired in the regulator.
2612 int regulator_enable(struct regulator *regulator)
2614 struct regulator_dev *rdev = regulator->rdev;
2615 struct ww_acquire_ctx ww_ctx;
2618 regulator_lock_dependent(rdev, &ww_ctx);
2619 ret = _regulator_enable(regulator);
2620 regulator_unlock_dependent(rdev, &ww_ctx);
2624 EXPORT_SYMBOL_GPL(regulator_enable);
2626 static int _regulator_do_disable(struct regulator_dev *rdev)
2630 trace_regulator_disable(rdev_get_name(rdev));
2632 if (rdev->ena_pin) {
2633 if (rdev->ena_gpio_state) {
2634 ret = regulator_ena_gpio_ctrl(rdev, false);
2637 rdev->ena_gpio_state = 0;
2640 } else if (rdev->desc->ops->disable) {
2641 ret = rdev->desc->ops->disable(rdev);
2646 /* cares about last_off_jiffy only if off_on_delay is required by
2649 if (rdev->desc->off_on_delay)
2650 rdev->last_off_jiffy = jiffies;
2652 trace_regulator_disable_complete(rdev_get_name(rdev));
2657 /* locks held by regulator_disable() */
2658 static int _regulator_disable(struct regulator *regulator)
2660 struct regulator_dev *rdev = regulator->rdev;
2663 lockdep_assert_held_once(&rdev->mutex.base);
2665 if (WARN(rdev->use_count <= 0,
2666 "unbalanced disables for %s\n", rdev_get_name(rdev)))
2669 /* are we the last user and permitted to disable ? */
2670 if (rdev->use_count == 1 &&
2671 (rdev->constraints && !rdev->constraints->always_on)) {
2673 /* we are last user */
2674 if (regulator_ops_is_valid(rdev, REGULATOR_CHANGE_STATUS)) {
2675 ret = _notifier_call_chain(rdev,
2676 REGULATOR_EVENT_PRE_DISABLE,
2678 if (ret & NOTIFY_STOP_MASK)
2681 ret = _regulator_do_disable(rdev);
2683 rdev_err(rdev, "failed to disable\n");
2684 _notifier_call_chain(rdev,
2685 REGULATOR_EVENT_ABORT_DISABLE,
2689 _notifier_call_chain(rdev, REGULATOR_EVENT_DISABLE,
2693 rdev->use_count = 0;
2694 } else if (rdev->use_count > 1) {
2699 ret = _regulator_handle_consumer_disable(regulator);
2701 if (ret == 0 && rdev->coupling_desc.n_coupled > 1)
2702 ret = regulator_balance_voltage(rdev, PM_SUSPEND_ON);
2704 if (ret == 0 && rdev->use_count == 0 && rdev->supply)
2705 ret = _regulator_disable(rdev->supply);
2711 * regulator_disable - disable regulator output
2712 * @regulator: regulator source
2714 * Disable the regulator output voltage or current. Calls to
2715 * regulator_enable() must be balanced with calls to
2716 * regulator_disable().
2718 * NOTE: this will only disable the regulator output if no other consumer
2719 * devices have it enabled, the regulator device supports disabling and
2720 * machine constraints permit this operation.
2722 int regulator_disable(struct regulator *regulator)
2724 struct regulator_dev *rdev = regulator->rdev;
2725 struct ww_acquire_ctx ww_ctx;
2728 regulator_lock_dependent(rdev, &ww_ctx);
2729 ret = _regulator_disable(regulator);
2730 regulator_unlock_dependent(rdev, &ww_ctx);
2734 EXPORT_SYMBOL_GPL(regulator_disable);
2736 /* locks held by regulator_force_disable() */
2737 static int _regulator_force_disable(struct regulator_dev *rdev)
2741 lockdep_assert_held_once(&rdev->mutex.base);
2743 ret = _notifier_call_chain(rdev, REGULATOR_EVENT_FORCE_DISABLE |
2744 REGULATOR_EVENT_PRE_DISABLE, NULL);
2745 if (ret & NOTIFY_STOP_MASK)
2748 ret = _regulator_do_disable(rdev);
2750 rdev_err(rdev, "failed to force disable\n");
2751 _notifier_call_chain(rdev, REGULATOR_EVENT_FORCE_DISABLE |
2752 REGULATOR_EVENT_ABORT_DISABLE, NULL);
2756 _notifier_call_chain(rdev, REGULATOR_EVENT_FORCE_DISABLE |
2757 REGULATOR_EVENT_DISABLE, NULL);
2763 * regulator_force_disable - force disable regulator output
2764 * @regulator: regulator source
2766 * Forcibly disable the regulator output voltage or current.
2767 * NOTE: this *will* disable the regulator output even if other consumer
2768 * devices have it enabled. This should be used for situations when device
2769 * damage will likely occur if the regulator is not disabled (e.g. over temp).
2771 int regulator_force_disable(struct regulator *regulator)
2773 struct regulator_dev *rdev = regulator->rdev;
2774 struct ww_acquire_ctx ww_ctx;
2777 regulator_lock_dependent(rdev, &ww_ctx);
2779 ret = _regulator_force_disable(regulator->rdev);
2781 if (rdev->coupling_desc.n_coupled > 1)
2782 regulator_balance_voltage(rdev, PM_SUSPEND_ON);
2784 if (regulator->uA_load) {
2785 regulator->uA_load = 0;
2786 ret = drms_uA_update(rdev);
2789 if (rdev->use_count != 0 && rdev->supply)
2790 _regulator_disable(rdev->supply);
2792 regulator_unlock_dependent(rdev, &ww_ctx);
2796 EXPORT_SYMBOL_GPL(regulator_force_disable);
2798 static void regulator_disable_work(struct work_struct *work)
2800 struct regulator_dev *rdev = container_of(work, struct regulator_dev,
2802 struct ww_acquire_ctx ww_ctx;
2804 struct regulator *regulator;
2805 int total_count = 0;
2807 regulator_lock_dependent(rdev, &ww_ctx);
2810 * Workqueue functions queue the new work instance while the previous
2811 * work instance is being processed. Cancel the queued work instance
2812 * as the work instance under processing does the job of the queued
2815 cancel_delayed_work(&rdev->disable_work);
2817 list_for_each_entry(regulator, &rdev->consumer_list, list) {
2818 count = regulator->deferred_disables;
2823 total_count += count;
2824 regulator->deferred_disables = 0;
2826 for (i = 0; i < count; i++) {
2827 ret = _regulator_disable(regulator);
2829 rdev_err(rdev, "Deferred disable failed: %d\n", ret);
2832 WARN_ON(!total_count);
2834 if (rdev->coupling_desc.n_coupled > 1)
2835 regulator_balance_voltage(rdev, PM_SUSPEND_ON);
2837 regulator_unlock_dependent(rdev, &ww_ctx);
2841 * regulator_disable_deferred - disable regulator output with delay
2842 * @regulator: regulator source
2843 * @ms: milliseconds until the regulator is disabled
2845 * Execute regulator_disable() on the regulator after a delay. This
2846 * is intended for use with devices that require some time to quiesce.
2848 * NOTE: this will only disable the regulator output if no other consumer
2849 * devices have it enabled, the regulator device supports disabling and
2850 * machine constraints permit this operation.
2852 int regulator_disable_deferred(struct regulator *regulator, int ms)
2854 struct regulator_dev *rdev = regulator->rdev;
2857 return regulator_disable(regulator);
2859 regulator_lock(rdev);
2860 regulator->deferred_disables++;
2861 mod_delayed_work(system_power_efficient_wq, &rdev->disable_work,
2862 msecs_to_jiffies(ms));
2863 regulator_unlock(rdev);
2867 EXPORT_SYMBOL_GPL(regulator_disable_deferred);
2869 static int _regulator_is_enabled(struct regulator_dev *rdev)
2871 /* A GPIO control always takes precedence */
2873 return rdev->ena_gpio_state;
2875 /* If we don't know then assume that the regulator is always on */
2876 if (!rdev->desc->ops->is_enabled)
2879 return rdev->desc->ops->is_enabled(rdev);
2882 static int _regulator_list_voltage(struct regulator_dev *rdev,
2883 unsigned selector, int lock)
2885 const struct regulator_ops *ops = rdev->desc->ops;
2888 if (rdev->desc->fixed_uV && rdev->desc->n_voltages == 1 && !selector)
2889 return rdev->desc->fixed_uV;
2891 if (ops->list_voltage) {
2892 if (selector >= rdev->desc->n_voltages)
2895 regulator_lock(rdev);
2896 ret = ops->list_voltage(rdev, selector);
2898 regulator_unlock(rdev);
2899 } else if (rdev->is_switch && rdev->supply) {
2900 ret = _regulator_list_voltage(rdev->supply->rdev,
2907 if (ret < rdev->constraints->min_uV)
2909 else if (ret > rdev->constraints->max_uV)
2917 * regulator_is_enabled - is the regulator output enabled
2918 * @regulator: regulator source
2920 * Returns positive if the regulator driver backing the source/client
2921 * has requested that the device be enabled, zero if it hasn't, else a
2922 * negative errno code.
2924 * Note that the device backing this regulator handle can have multiple
2925 * users, so it might be enabled even if regulator_enable() was never
2926 * called for this particular source.
2928 int regulator_is_enabled(struct regulator *regulator)
2932 if (regulator->always_on)
2935 regulator_lock(regulator->rdev);
2936 ret = _regulator_is_enabled(regulator->rdev);
2937 regulator_unlock(regulator->rdev);
2941 EXPORT_SYMBOL_GPL(regulator_is_enabled);
2944 * regulator_count_voltages - count regulator_list_voltage() selectors
2945 * @regulator: regulator source
2947 * Returns number of selectors, or negative errno. Selectors are
2948 * numbered starting at zero, and typically correspond to bitfields
2949 * in hardware registers.
2951 int regulator_count_voltages(struct regulator *regulator)
2953 struct regulator_dev *rdev = regulator->rdev;
2955 if (rdev->desc->n_voltages)
2956 return rdev->desc->n_voltages;
2958 if (!rdev->is_switch || !rdev->supply)
2961 return regulator_count_voltages(rdev->supply);
2963 EXPORT_SYMBOL_GPL(regulator_count_voltages);
2966 * regulator_list_voltage - enumerate supported voltages
2967 * @regulator: regulator source
2968 * @selector: identify voltage to list
2969 * Context: can sleep
2971 * Returns a voltage that can be passed to @regulator_set_voltage(),
2972 * zero if this selector code can't be used on this system, or a
2975 int regulator_list_voltage(struct regulator *regulator, unsigned selector)
2977 return _regulator_list_voltage(regulator->rdev, selector, 1);
2979 EXPORT_SYMBOL_GPL(regulator_list_voltage);
2982 * regulator_get_regmap - get the regulator's register map
2983 * @regulator: regulator source
2985 * Returns the register map for the given regulator, or an ERR_PTR value
2986 * if the regulator doesn't use regmap.
2988 struct regmap *regulator_get_regmap(struct regulator *regulator)
2990 struct regmap *map = regulator->rdev->regmap;
2992 return map ? map : ERR_PTR(-EOPNOTSUPP);
2996 * regulator_get_hardware_vsel_register - get the HW voltage selector register
2997 * @regulator: regulator source
2998 * @vsel_reg: voltage selector register, output parameter
2999 * @vsel_mask: mask for voltage selector bitfield, output parameter
3001 * Returns the hardware register offset and bitmask used for setting the
3002 * regulator voltage. This might be useful when configuring voltage-scaling
3003 * hardware or firmware that can make I2C requests behind the kernel's back,
3006 * On success, the output parameters @vsel_reg and @vsel_mask are filled in
3007 * and 0 is returned, otherwise a negative errno is returned.
3009 int regulator_get_hardware_vsel_register(struct regulator *regulator,
3011 unsigned *vsel_mask)
3013 struct regulator_dev *rdev = regulator->rdev;
3014 const struct regulator_ops *ops = rdev->desc->ops;
3016 if (ops->set_voltage_sel != regulator_set_voltage_sel_regmap)
3019 *vsel_reg = rdev->desc->vsel_reg;
3020 *vsel_mask = rdev->desc->vsel_mask;
3024 EXPORT_SYMBOL_GPL(regulator_get_hardware_vsel_register);
3027 * regulator_list_hardware_vsel - get the HW-specific register value for a selector
3028 * @regulator: regulator source
3029 * @selector: identify voltage to list
3031 * Converts the selector to a hardware-specific voltage selector that can be
3032 * directly written to the regulator registers. The address of the voltage
3033 * register can be determined by calling @regulator_get_hardware_vsel_register.
3035 * On error a negative errno is returned.
3037 int regulator_list_hardware_vsel(struct regulator *regulator,
3040 struct regulator_dev *rdev = regulator->rdev;
3041 const struct regulator_ops *ops = rdev->desc->ops;
3043 if (selector >= rdev->desc->n_voltages)
3045 if (ops->set_voltage_sel != regulator_set_voltage_sel_regmap)
3050 EXPORT_SYMBOL_GPL(regulator_list_hardware_vsel);
3053 * regulator_get_linear_step - return the voltage step size between VSEL values
3054 * @regulator: regulator source
3056 * Returns the voltage step size between VSEL values for linear
3057 * regulators, or return 0 if the regulator isn't a linear regulator.
3059 unsigned int regulator_get_linear_step(struct regulator *regulator)
3061 struct regulator_dev *rdev = regulator->rdev;
3063 return rdev->desc->uV_step;
3065 EXPORT_SYMBOL_GPL(regulator_get_linear_step);
3068 * regulator_is_supported_voltage - check if a voltage range can be supported
3070 * @regulator: Regulator to check.
3071 * @min_uV: Minimum required voltage in uV.
3072 * @max_uV: Maximum required voltage in uV.
3074 * Returns a boolean.
3076 int regulator_is_supported_voltage(struct regulator *regulator,
3077 int min_uV, int max_uV)
3079 struct regulator_dev *rdev = regulator->rdev;
3080 int i, voltages, ret;
3082 /* If we can't change voltage check the current voltage */
3083 if (!regulator_ops_is_valid(rdev, REGULATOR_CHANGE_VOLTAGE)) {
3084 ret = regulator_get_voltage(regulator);
3086 return min_uV <= ret && ret <= max_uV;
3091 /* Any voltage within constrains range is fine? */
3092 if (rdev->desc->continuous_voltage_range)
3093 return min_uV >= rdev->constraints->min_uV &&
3094 max_uV <= rdev->constraints->max_uV;
3096 ret = regulator_count_voltages(regulator);
3101 for (i = 0; i < voltages; i++) {
3102 ret = regulator_list_voltage(regulator, i);
3104 if (ret >= min_uV && ret <= max_uV)
3110 EXPORT_SYMBOL_GPL(regulator_is_supported_voltage);
3112 static int regulator_map_voltage(struct regulator_dev *rdev, int min_uV,
3115 const struct regulator_desc *desc = rdev->desc;
3117 if (desc->ops->map_voltage)
3118 return desc->ops->map_voltage(rdev, min_uV, max_uV);
3120 if (desc->ops->list_voltage == regulator_list_voltage_linear)
3121 return regulator_map_voltage_linear(rdev, min_uV, max_uV);
3123 if (desc->ops->list_voltage == regulator_list_voltage_linear_range)
3124 return regulator_map_voltage_linear_range(rdev, min_uV, max_uV);
3126 if (desc->ops->list_voltage ==
3127 regulator_list_voltage_pickable_linear_range)
3128 return regulator_map_voltage_pickable_linear_range(rdev,
3131 return regulator_map_voltage_iterate(rdev, min_uV, max_uV);
3134 static int _regulator_call_set_voltage(struct regulator_dev *rdev,
3135 int min_uV, int max_uV,
3138 struct pre_voltage_change_data data;
3141 data.old_uV = regulator_get_voltage_rdev(rdev);
3142 data.min_uV = min_uV;
3143 data.max_uV = max_uV;
3144 ret = _notifier_call_chain(rdev, REGULATOR_EVENT_PRE_VOLTAGE_CHANGE,
3146 if (ret & NOTIFY_STOP_MASK)
3149 ret = rdev->desc->ops->set_voltage(rdev, min_uV, max_uV, selector);
3153 _notifier_call_chain(rdev, REGULATOR_EVENT_ABORT_VOLTAGE_CHANGE,
3154 (void *)data.old_uV);
3159 static int _regulator_call_set_voltage_sel(struct regulator_dev *rdev,
3160 int uV, unsigned selector)
3162 struct pre_voltage_change_data data;
3165 data.old_uV = regulator_get_voltage_rdev(rdev);
3168 ret = _notifier_call_chain(rdev, REGULATOR_EVENT_PRE_VOLTAGE_CHANGE,
3170 if (ret & NOTIFY_STOP_MASK)
3173 ret = rdev->desc->ops->set_voltage_sel(rdev, selector);
3177 _notifier_call_chain(rdev, REGULATOR_EVENT_ABORT_VOLTAGE_CHANGE,
3178 (void *)data.old_uV);
3183 static int _regulator_set_voltage_sel_step(struct regulator_dev *rdev,
3184 int uV, int new_selector)
3186 const struct regulator_ops *ops = rdev->desc->ops;
3187 int diff, old_sel, curr_sel, ret;
3189 /* Stepping is only needed if the regulator is enabled. */
3190 if (!_regulator_is_enabled(rdev))
3193 if (!ops->get_voltage_sel)
3196 old_sel = ops->get_voltage_sel(rdev);
3200 diff = new_selector - old_sel;
3202 return 0; /* No change needed. */
3206 for (curr_sel = old_sel + rdev->desc->vsel_step;
3207 curr_sel < new_selector;
3208 curr_sel += rdev->desc->vsel_step) {
3210 * Call the callback directly instead of using
3211 * _regulator_call_set_voltage_sel() as we don't
3212 * want to notify anyone yet. Same in the branch
3215 ret = ops->set_voltage_sel(rdev, curr_sel);
3220 /* Stepping down. */
3221 for (curr_sel = old_sel - rdev->desc->vsel_step;
3222 curr_sel > new_selector;
3223 curr_sel -= rdev->desc->vsel_step) {
3224 ret = ops->set_voltage_sel(rdev, curr_sel);
3231 /* The final selector will trigger the notifiers. */
3232 return _regulator_call_set_voltage_sel(rdev, uV, new_selector);
3236 * At least try to return to the previous voltage if setting a new
3239 (void)ops->set_voltage_sel(rdev, old_sel);
3243 static int _regulator_set_voltage_time(struct regulator_dev *rdev,
3244 int old_uV, int new_uV)
3246 unsigned int ramp_delay = 0;
3248 if (rdev->constraints->ramp_delay)
3249 ramp_delay = rdev->constraints->ramp_delay;
3250 else if (rdev->desc->ramp_delay)
3251 ramp_delay = rdev->desc->ramp_delay;
3252 else if (rdev->constraints->settling_time)
3253 return rdev->constraints->settling_time;
3254 else if (rdev->constraints->settling_time_up &&
3256 return rdev->constraints->settling_time_up;
3257 else if (rdev->constraints->settling_time_down &&
3259 return rdev->constraints->settling_time_down;
3261 if (ramp_delay == 0) {
3262 rdev_dbg(rdev, "ramp_delay not set\n");
3266 return DIV_ROUND_UP(abs(new_uV - old_uV), ramp_delay);
3269 static int _regulator_do_set_voltage(struct regulator_dev *rdev,
3270 int min_uV, int max_uV)
3275 unsigned int selector;
3276 int old_selector = -1;
3277 const struct regulator_ops *ops = rdev->desc->ops;
3278 int old_uV = regulator_get_voltage_rdev(rdev);
3280 trace_regulator_set_voltage(rdev_get_name(rdev), min_uV, max_uV);
3282 min_uV += rdev->constraints->uV_offset;
3283 max_uV += rdev->constraints->uV_offset;
3286 * If we can't obtain the old selector there is not enough
3287 * info to call set_voltage_time_sel().
3289 if (_regulator_is_enabled(rdev) &&
3290 ops->set_voltage_time_sel && ops->get_voltage_sel) {
3291 old_selector = ops->get_voltage_sel(rdev);
3292 if (old_selector < 0)
3293 return old_selector;
3296 if (ops->set_voltage) {
3297 ret = _regulator_call_set_voltage(rdev, min_uV, max_uV,
3301 if (ops->list_voltage)
3302 best_val = ops->list_voltage(rdev,
3305 best_val = regulator_get_voltage_rdev(rdev);
3308 } else if (ops->set_voltage_sel) {
3309 ret = regulator_map_voltage(rdev, min_uV, max_uV);
3311 best_val = ops->list_voltage(rdev, ret);
3312 if (min_uV <= best_val && max_uV >= best_val) {
3314 if (old_selector == selector)
3316 else if (rdev->desc->vsel_step)
3317 ret = _regulator_set_voltage_sel_step(
3318 rdev, best_val, selector);
3320 ret = _regulator_call_set_voltage_sel(
3321 rdev, best_val, selector);
3333 if (ops->set_voltage_time_sel) {
3335 * Call set_voltage_time_sel if successfully obtained
3338 if (old_selector >= 0 && old_selector != selector)
3339 delay = ops->set_voltage_time_sel(rdev, old_selector,
3342 if (old_uV != best_val) {
3343 if (ops->set_voltage_time)
3344 delay = ops->set_voltage_time(rdev, old_uV,
3347 delay = _regulator_set_voltage_time(rdev,
3354 rdev_warn(rdev, "failed to get delay: %d\n", delay);
3358 /* Insert any necessary delays */
3359 if (delay >= 1000) {
3360 mdelay(delay / 1000);
3361 udelay(delay % 1000);
3366 if (best_val >= 0) {
3367 unsigned long data = best_val;
3369 _notifier_call_chain(rdev, REGULATOR_EVENT_VOLTAGE_CHANGE,
3374 trace_regulator_set_voltage_complete(rdev_get_name(rdev), best_val);
3379 static int _regulator_do_set_suspend_voltage(struct regulator_dev *rdev,
3380 int min_uV, int max_uV, suspend_state_t state)
3382 struct regulator_state *rstate;
3385 rstate = regulator_get_suspend_state(rdev, state);
3389 if (min_uV < rstate->min_uV)
3390 min_uV = rstate->min_uV;
3391 if (max_uV > rstate->max_uV)
3392 max_uV = rstate->max_uV;
3394 sel = regulator_map_voltage(rdev, min_uV, max_uV);
3398 uV = rdev->desc->ops->list_voltage(rdev, sel);
3399 if (uV >= min_uV && uV <= max_uV)
3405 static int regulator_set_voltage_unlocked(struct regulator *regulator,
3406 int min_uV, int max_uV,
3407 suspend_state_t state)
3409 struct regulator_dev *rdev = regulator->rdev;
3410 struct regulator_voltage *voltage = ®ulator->voltage[state];
3412 int old_min_uV, old_max_uV;
3415 /* If we're setting the same range as last time the change
3416 * should be a noop (some cpufreq implementations use the same
3417 * voltage for multiple frequencies, for example).
3419 if (voltage->min_uV == min_uV && voltage->max_uV == max_uV)
3422 /* If we're trying to set a range that overlaps the current voltage,
3423 * return successfully even though the regulator does not support
3424 * changing the voltage.
3426 if (!regulator_ops_is_valid(rdev, REGULATOR_CHANGE_VOLTAGE)) {
3427 current_uV = regulator_get_voltage_rdev(rdev);
3428 if (min_uV <= current_uV && current_uV <= max_uV) {
3429 voltage->min_uV = min_uV;
3430 voltage->max_uV = max_uV;
3436 if (!rdev->desc->ops->set_voltage &&
3437 !rdev->desc->ops->set_voltage_sel) {
3442 /* constraints check */
3443 ret = regulator_check_voltage(rdev, &min_uV, &max_uV);
3447 /* restore original values in case of error */
3448 old_min_uV = voltage->min_uV;
3449 old_max_uV = voltage->max_uV;
3450 voltage->min_uV = min_uV;
3451 voltage->max_uV = max_uV;
3453 /* for not coupled regulators this will just set the voltage */
3454 ret = regulator_balance_voltage(rdev, state);
3456 voltage->min_uV = old_min_uV;
3457 voltage->max_uV = old_max_uV;
3464 int regulator_set_voltage_rdev(struct regulator_dev *rdev, int min_uV,
3465 int max_uV, suspend_state_t state)
3467 int best_supply_uV = 0;
3468 int supply_change_uV = 0;
3472 regulator_ops_is_valid(rdev->supply->rdev,
3473 REGULATOR_CHANGE_VOLTAGE) &&
3474 (rdev->desc->min_dropout_uV || !(rdev->desc->ops->get_voltage ||
3475 rdev->desc->ops->get_voltage_sel))) {
3476 int current_supply_uV;
3479 selector = regulator_map_voltage(rdev, min_uV, max_uV);
3485 best_supply_uV = _regulator_list_voltage(rdev, selector, 0);
3486 if (best_supply_uV < 0) {
3487 ret = best_supply_uV;
3491 best_supply_uV += rdev->desc->min_dropout_uV;
3493 current_supply_uV = regulator_get_voltage_rdev(rdev->supply->rdev);
3494 if (current_supply_uV < 0) {
3495 ret = current_supply_uV;
3499 supply_change_uV = best_supply_uV - current_supply_uV;
3502 if (supply_change_uV > 0) {
3503 ret = regulator_set_voltage_unlocked(rdev->supply,
3504 best_supply_uV, INT_MAX, state);
3506 dev_err(&rdev->dev, "Failed to increase supply voltage: %d\n",
3512 if (state == PM_SUSPEND_ON)
3513 ret = _regulator_do_set_voltage(rdev, min_uV, max_uV);
3515 ret = _regulator_do_set_suspend_voltage(rdev, min_uV,
3520 if (supply_change_uV < 0) {
3521 ret = regulator_set_voltage_unlocked(rdev->supply,
3522 best_supply_uV, INT_MAX, state);
3524 dev_warn(&rdev->dev, "Failed to decrease supply voltage: %d\n",
3526 /* No need to fail here */
3533 EXPORT_SYMBOL_GPL(regulator_set_voltage_rdev);
3535 static int regulator_limit_voltage_step(struct regulator_dev *rdev,
3536 int *current_uV, int *min_uV)
3538 struct regulation_constraints *constraints = rdev->constraints;
3540 /* Limit voltage change only if necessary */
3541 if (!constraints->max_uV_step || !_regulator_is_enabled(rdev))
3544 if (*current_uV < 0) {
3545 *current_uV = regulator_get_voltage_rdev(rdev);
3547 if (*current_uV < 0)
3551 if (abs(*current_uV - *min_uV) <= constraints->max_uV_step)
3554 /* Clamp target voltage within the given step */
3555 if (*current_uV < *min_uV)
3556 *min_uV = min(*current_uV + constraints->max_uV_step,
3559 *min_uV = max(*current_uV - constraints->max_uV_step,
3565 static int regulator_get_optimal_voltage(struct regulator_dev *rdev,
3567 int *min_uV, int *max_uV,
3568 suspend_state_t state,
3571 struct coupling_desc *c_desc = &rdev->coupling_desc;
3572 struct regulator_dev **c_rdevs = c_desc->coupled_rdevs;
3573 struct regulation_constraints *constraints = rdev->constraints;
3574 int desired_min_uV = 0, desired_max_uV = INT_MAX;
3575 int max_current_uV = 0, min_current_uV = INT_MAX;
3576 int highest_min_uV = 0, target_uV, possible_uV;
3577 int i, ret, max_spread;
3583 * If there are no coupled regulators, simply set the voltage
3584 * demanded by consumers.
3586 if (n_coupled == 1) {
3588 * If consumers don't provide any demands, set voltage
3591 desired_min_uV = constraints->min_uV;
3592 desired_max_uV = constraints->max_uV;
3594 ret = regulator_check_consumers(rdev,
3596 &desired_max_uV, state);
3600 possible_uV = desired_min_uV;
3606 /* Find highest min desired voltage */
3607 for (i = 0; i < n_coupled; i++) {
3609 int tmp_max = INT_MAX;
3611 lockdep_assert_held_once(&c_rdevs[i]->mutex.base);
3613 ret = regulator_check_consumers(c_rdevs[i],
3619 ret = regulator_check_voltage(c_rdevs[i], &tmp_min, &tmp_max);
3623 highest_min_uV = max(highest_min_uV, tmp_min);
3626 desired_min_uV = tmp_min;
3627 desired_max_uV = tmp_max;
3631 max_spread = constraints->max_spread[0];
3634 * Let target_uV be equal to the desired one if possible.
3635 * If not, set it to minimum voltage, allowed by other coupled
3638 target_uV = max(desired_min_uV, highest_min_uV - max_spread);
3641 * Find min and max voltages, which currently aren't violating
3644 for (i = 1; i < n_coupled; i++) {
3647 if (!_regulator_is_enabled(c_rdevs[i]))
3650 tmp_act = regulator_get_voltage_rdev(c_rdevs[i]);
3654 min_current_uV = min(tmp_act, min_current_uV);
3655 max_current_uV = max(tmp_act, max_current_uV);
3658 /* There aren't any other regulators enabled */
3659 if (max_current_uV == 0) {
3660 possible_uV = target_uV;
3663 * Correct target voltage, so as it currently isn't
3664 * violating max_spread
3666 possible_uV = max(target_uV, max_current_uV - max_spread);
3667 possible_uV = min(possible_uV, min_current_uV + max_spread);
3670 if (possible_uV > desired_max_uV)
3673 done = (possible_uV == target_uV);
3674 desired_min_uV = possible_uV;
3677 /* Apply max_uV_step constraint if necessary */
3678 if (state == PM_SUSPEND_ON) {
3679 ret = regulator_limit_voltage_step(rdev, current_uV,
3688 /* Set current_uV if wasn't done earlier in the code and if necessary */
3689 if (n_coupled > 1 && *current_uV == -1) {
3691 if (_regulator_is_enabled(rdev)) {
3692 ret = regulator_get_voltage_rdev(rdev);
3698 *current_uV = desired_min_uV;
3702 *min_uV = desired_min_uV;
3703 *max_uV = desired_max_uV;
3708 static int regulator_balance_voltage(struct regulator_dev *rdev,
3709 suspend_state_t state)
3711 struct regulator_dev **c_rdevs;
3712 struct regulator_dev *best_rdev;
3713 struct coupling_desc *c_desc = &rdev->coupling_desc;
3714 struct regulator_coupler *coupler = c_desc->coupler;
3715 int i, ret, n_coupled, best_min_uV, best_max_uV, best_c_rdev;
3716 unsigned int delta, best_delta;
3717 unsigned long c_rdev_done = 0;
3718 bool best_c_rdev_done;
3720 c_rdevs = c_desc->coupled_rdevs;
3721 n_coupled = c_desc->n_coupled;
3724 * If system is in a state other than PM_SUSPEND_ON, don't check
3725 * other coupled regulators.
3727 if (state != PM_SUSPEND_ON)
3730 if (c_desc->n_resolved < n_coupled) {
3731 rdev_err(rdev, "Not all coupled regulators registered\n");
3735 /* Invoke custom balancer for customized couplers */
3736 if (coupler && coupler->balance_voltage)
3737 return coupler->balance_voltage(coupler, rdev, state);
3740 * Find the best possible voltage change on each loop. Leave the loop
3741 * if there isn't any possible change.
3744 best_c_rdev_done = false;
3752 * Find highest difference between optimal voltage
3753 * and current voltage.
3755 for (i = 0; i < n_coupled; i++) {
3757 * optimal_uV is the best voltage that can be set for
3758 * i-th regulator at the moment without violating
3759 * max_spread constraint in order to balance
3760 * the coupled voltages.
3762 int optimal_uV = 0, optimal_max_uV = 0, current_uV = 0;
3764 if (test_bit(i, &c_rdev_done))
3767 ret = regulator_get_optimal_voltage(c_rdevs[i],
3775 delta = abs(optimal_uV - current_uV);
3777 if (delta && best_delta <= delta) {
3778 best_c_rdev_done = ret;
3780 best_rdev = c_rdevs[i];
3781 best_min_uV = optimal_uV;
3782 best_max_uV = optimal_max_uV;
3787 /* Nothing to change, return successfully */
3793 ret = regulator_set_voltage_rdev(best_rdev, best_min_uV,
3794 best_max_uV, state);
3799 if (best_c_rdev_done)
3800 set_bit(best_c_rdev, &c_rdev_done);
3802 } while (n_coupled > 1);
3809 * regulator_set_voltage - set regulator output voltage
3810 * @regulator: regulator source
3811 * @min_uV: Minimum required voltage in uV
3812 * @max_uV: Maximum acceptable voltage in uV
3814 * Sets a voltage regulator to the desired output voltage. This can be set
3815 * during any regulator state. IOW, regulator can be disabled or enabled.
3817 * If the regulator is enabled then the voltage will change to the new value
3818 * immediately otherwise if the regulator is disabled the regulator will
3819 * output at the new voltage when enabled.
3821 * NOTE: If the regulator is shared between several devices then the lowest
3822 * request voltage that meets the system constraints will be used.
3823 * Regulator system constraints must be set for this regulator before
3824 * calling this function otherwise this call will fail.
3826 int regulator_set_voltage(struct regulator *regulator, int min_uV, int max_uV)
3828 struct ww_acquire_ctx ww_ctx;
3831 regulator_lock_dependent(regulator->rdev, &ww_ctx);
3833 ret = regulator_set_voltage_unlocked(regulator, min_uV, max_uV,
3836 regulator_unlock_dependent(regulator->rdev, &ww_ctx);
3840 EXPORT_SYMBOL_GPL(regulator_set_voltage);
3842 static inline int regulator_suspend_toggle(struct regulator_dev *rdev,
3843 suspend_state_t state, bool en)
3845 struct regulator_state *rstate;
3847 rstate = regulator_get_suspend_state(rdev, state);
3851 if (!rstate->changeable)
3854 rstate->enabled = (en) ? ENABLE_IN_SUSPEND : DISABLE_IN_SUSPEND;
3859 int regulator_suspend_enable(struct regulator_dev *rdev,
3860 suspend_state_t state)
3862 return regulator_suspend_toggle(rdev, state, true);
3864 EXPORT_SYMBOL_GPL(regulator_suspend_enable);
3866 int regulator_suspend_disable(struct regulator_dev *rdev,
3867 suspend_state_t state)
3869 struct regulator *regulator;
3870 struct regulator_voltage *voltage;
3873 * if any consumer wants this regulator device keeping on in
3874 * suspend states, don't set it as disabled.
3876 list_for_each_entry(regulator, &rdev->consumer_list, list) {
3877 voltage = ®ulator->voltage[state];
3878 if (voltage->min_uV || voltage->max_uV)
3882 return regulator_suspend_toggle(rdev, state, false);
3884 EXPORT_SYMBOL_GPL(regulator_suspend_disable);
3886 static int _regulator_set_suspend_voltage(struct regulator *regulator,
3887 int min_uV, int max_uV,
3888 suspend_state_t state)
3890 struct regulator_dev *rdev = regulator->rdev;
3891 struct regulator_state *rstate;
3893 rstate = regulator_get_suspend_state(rdev, state);
3897 if (rstate->min_uV == rstate->max_uV) {
3898 rdev_err(rdev, "The suspend voltage can't be changed!\n");
3902 return regulator_set_voltage_unlocked(regulator, min_uV, max_uV, state);
3905 int regulator_set_suspend_voltage(struct regulator *regulator, int min_uV,
3906 int max_uV, suspend_state_t state)
3908 struct ww_acquire_ctx ww_ctx;
3911 /* PM_SUSPEND_ON is handled by regulator_set_voltage() */
3912 if (regulator_check_states(state) || state == PM_SUSPEND_ON)
3915 regulator_lock_dependent(regulator->rdev, &ww_ctx);
3917 ret = _regulator_set_suspend_voltage(regulator, min_uV,
3920 regulator_unlock_dependent(regulator->rdev, &ww_ctx);
3924 EXPORT_SYMBOL_GPL(regulator_set_suspend_voltage);
3927 * regulator_set_voltage_time - get raise/fall time
3928 * @regulator: regulator source
3929 * @old_uV: starting voltage in microvolts
3930 * @new_uV: target voltage in microvolts
3932 * Provided with the starting and ending voltage, this function attempts to
3933 * calculate the time in microseconds required to rise or fall to this new
3936 int regulator_set_voltage_time(struct regulator *regulator,
3937 int old_uV, int new_uV)
3939 struct regulator_dev *rdev = regulator->rdev;
3940 const struct regulator_ops *ops = rdev->desc->ops;
3946 if (ops->set_voltage_time)
3947 return ops->set_voltage_time(rdev, old_uV, new_uV);
3948 else if (!ops->set_voltage_time_sel)
3949 return _regulator_set_voltage_time(rdev, old_uV, new_uV);
3951 /* Currently requires operations to do this */
3952 if (!ops->list_voltage || !rdev->desc->n_voltages)
3955 for (i = 0; i < rdev->desc->n_voltages; i++) {
3956 /* We only look for exact voltage matches here */
3957 voltage = regulator_list_voltage(regulator, i);
3962 if (voltage == old_uV)
3964 if (voltage == new_uV)
3968 if (old_sel < 0 || new_sel < 0)
3971 return ops->set_voltage_time_sel(rdev, old_sel, new_sel);
3973 EXPORT_SYMBOL_GPL(regulator_set_voltage_time);
3976 * regulator_set_voltage_time_sel - get raise/fall time
3977 * @rdev: regulator source device
3978 * @old_selector: selector for starting voltage
3979 * @new_selector: selector for target voltage
3981 * Provided with the starting and target voltage selectors, this function
3982 * returns time in microseconds required to rise or fall to this new voltage
3984 * Drivers providing ramp_delay in regulation_constraints can use this as their
3985 * set_voltage_time_sel() operation.
3987 int regulator_set_voltage_time_sel(struct regulator_dev *rdev,
3988 unsigned int old_selector,
3989 unsigned int new_selector)
3991 int old_volt, new_volt;
3994 if (!rdev->desc->ops->list_voltage)
3997 old_volt = rdev->desc->ops->list_voltage(rdev, old_selector);
3998 new_volt = rdev->desc->ops->list_voltage(rdev, new_selector);
4000 if (rdev->desc->ops->set_voltage_time)
4001 return rdev->desc->ops->set_voltage_time(rdev, old_volt,
4004 return _regulator_set_voltage_time(rdev, old_volt, new_volt);
4006 EXPORT_SYMBOL_GPL(regulator_set_voltage_time_sel);
4009 * regulator_sync_voltage - re-apply last regulator output voltage
4010 * @regulator: regulator source
4012 * Re-apply the last configured voltage. This is intended to be used
4013 * where some external control source the consumer is cooperating with
4014 * has caused the configured voltage to change.
4016 int regulator_sync_voltage(struct regulator *regulator)
4018 struct regulator_dev *rdev = regulator->rdev;
4019 struct regulator_voltage *voltage = ®ulator->voltage[PM_SUSPEND_ON];
4020 int ret, min_uV, max_uV;
4022 regulator_lock(rdev);
4024 if (!rdev->desc->ops->set_voltage &&
4025 !rdev->desc->ops->set_voltage_sel) {
4030 /* This is only going to work if we've had a voltage configured. */
4031 if (!voltage->min_uV && !voltage->max_uV) {
4036 min_uV = voltage->min_uV;
4037 max_uV = voltage->max_uV;
4039 /* This should be a paranoia check... */
4040 ret = regulator_check_voltage(rdev, &min_uV, &max_uV);
4044 ret = regulator_check_consumers(rdev, &min_uV, &max_uV, 0);
4048 ret = _regulator_do_set_voltage(rdev, min_uV, max_uV);
4051 regulator_unlock(rdev);
4054 EXPORT_SYMBOL_GPL(regulator_sync_voltage);
4056 int regulator_get_voltage_rdev(struct regulator_dev *rdev)
4061 if (rdev->desc->ops->get_bypass) {
4062 ret = rdev->desc->ops->get_bypass(rdev, &bypassed);
4066 /* if bypassed the regulator must have a supply */
4067 if (!rdev->supply) {
4069 "bypassed regulator has no supply!\n");
4070 return -EPROBE_DEFER;
4073 return regulator_get_voltage_rdev(rdev->supply->rdev);
4077 if (rdev->desc->ops->get_voltage_sel) {
4078 sel = rdev->desc->ops->get_voltage_sel(rdev);
4081 ret = rdev->desc->ops->list_voltage(rdev, sel);
4082 } else if (rdev->desc->ops->get_voltage) {
4083 ret = rdev->desc->ops->get_voltage(rdev);
4084 } else if (rdev->desc->ops->list_voltage) {
4085 ret = rdev->desc->ops->list_voltage(rdev, 0);
4086 } else if (rdev->desc->fixed_uV && (rdev->desc->n_voltages == 1)) {
4087 ret = rdev->desc->fixed_uV;
4088 } else if (rdev->supply) {
4089 ret = regulator_get_voltage_rdev(rdev->supply->rdev);
4090 } else if (rdev->supply_name) {
4091 return -EPROBE_DEFER;
4098 return ret - rdev->constraints->uV_offset;
4100 EXPORT_SYMBOL_GPL(regulator_get_voltage_rdev);
4103 * regulator_get_voltage - get regulator output voltage
4104 * @regulator: regulator source
4106 * This returns the current regulator voltage in uV.
4108 * NOTE: If the regulator is disabled it will return the voltage value. This
4109 * function should not be used to determine regulator state.
4111 int regulator_get_voltage(struct regulator *regulator)
4113 struct ww_acquire_ctx ww_ctx;
4116 regulator_lock_dependent(regulator->rdev, &ww_ctx);
4117 ret = regulator_get_voltage_rdev(regulator->rdev);
4118 regulator_unlock_dependent(regulator->rdev, &ww_ctx);
4122 EXPORT_SYMBOL_GPL(regulator_get_voltage);
4125 * regulator_set_current_limit - set regulator output current limit
4126 * @regulator: regulator source
4127 * @min_uA: Minimum supported current in uA
4128 * @max_uA: Maximum supported current in uA
4130 * Sets current sink to the desired output current. This can be set during
4131 * any regulator state. IOW, regulator can be disabled or enabled.
4133 * If the regulator is enabled then the current will change to the new value
4134 * immediately otherwise if the regulator is disabled the regulator will
4135 * output at the new current when enabled.
4137 * NOTE: Regulator system constraints must be set for this regulator before
4138 * calling this function otherwise this call will fail.
4140 int regulator_set_current_limit(struct regulator *regulator,
4141 int min_uA, int max_uA)
4143 struct regulator_dev *rdev = regulator->rdev;
4146 regulator_lock(rdev);
4149 if (!rdev->desc->ops->set_current_limit) {
4154 /* constraints check */
4155 ret = regulator_check_current_limit(rdev, &min_uA, &max_uA);
4159 ret = rdev->desc->ops->set_current_limit(rdev, min_uA, max_uA);
4161 regulator_unlock(rdev);
4164 EXPORT_SYMBOL_GPL(regulator_set_current_limit);
4166 static int _regulator_get_current_limit_unlocked(struct regulator_dev *rdev)
4169 if (!rdev->desc->ops->get_current_limit)
4172 return rdev->desc->ops->get_current_limit(rdev);
4175 static int _regulator_get_current_limit(struct regulator_dev *rdev)
4179 regulator_lock(rdev);
4180 ret = _regulator_get_current_limit_unlocked(rdev);
4181 regulator_unlock(rdev);
4187 * regulator_get_current_limit - get regulator output current
4188 * @regulator: regulator source
4190 * This returns the current supplied by the specified current sink in uA.
4192 * NOTE: If the regulator is disabled it will return the current value. This
4193 * function should not be used to determine regulator state.
4195 int regulator_get_current_limit(struct regulator *regulator)
4197 return _regulator_get_current_limit(regulator->rdev);
4199 EXPORT_SYMBOL_GPL(regulator_get_current_limit);
4202 * regulator_set_mode - set regulator operating mode
4203 * @regulator: regulator source
4204 * @mode: operating mode - one of the REGULATOR_MODE constants
4206 * Set regulator operating mode to increase regulator efficiency or improve
4207 * regulation performance.
4209 * NOTE: Regulator system constraints must be set for this regulator before
4210 * calling this function otherwise this call will fail.
4212 int regulator_set_mode(struct regulator *regulator, unsigned int mode)
4214 struct regulator_dev *rdev = regulator->rdev;
4216 int regulator_curr_mode;
4218 regulator_lock(rdev);
4221 if (!rdev->desc->ops->set_mode) {
4226 /* return if the same mode is requested */
4227 if (rdev->desc->ops->get_mode) {
4228 regulator_curr_mode = rdev->desc->ops->get_mode(rdev);
4229 if (regulator_curr_mode == mode) {
4235 /* constraints check */
4236 ret = regulator_mode_constrain(rdev, &mode);
4240 ret = rdev->desc->ops->set_mode(rdev, mode);
4242 regulator_unlock(rdev);
4245 EXPORT_SYMBOL_GPL(regulator_set_mode);
4247 static unsigned int _regulator_get_mode_unlocked(struct regulator_dev *rdev)
4250 if (!rdev->desc->ops->get_mode)
4253 return rdev->desc->ops->get_mode(rdev);
4256 static unsigned int _regulator_get_mode(struct regulator_dev *rdev)
4260 regulator_lock(rdev);
4261 ret = _regulator_get_mode_unlocked(rdev);
4262 regulator_unlock(rdev);
4268 * regulator_get_mode - get regulator operating mode
4269 * @regulator: regulator source
4271 * Get the current regulator operating mode.
4273 unsigned int regulator_get_mode(struct regulator *regulator)
4275 return _regulator_get_mode(regulator->rdev);
4277 EXPORT_SYMBOL_GPL(regulator_get_mode);
4279 static int _regulator_get_error_flags(struct regulator_dev *rdev,
4280 unsigned int *flags)
4284 regulator_lock(rdev);
4287 if (!rdev->desc->ops->get_error_flags) {
4292 ret = rdev->desc->ops->get_error_flags(rdev, flags);
4294 regulator_unlock(rdev);
4299 * regulator_get_error_flags - get regulator error information
4300 * @regulator: regulator source
4301 * @flags: pointer to store error flags
4303 * Get the current regulator error information.
4305 int regulator_get_error_flags(struct regulator *regulator,
4306 unsigned int *flags)
4308 return _regulator_get_error_flags(regulator->rdev, flags);
4310 EXPORT_SYMBOL_GPL(regulator_get_error_flags);
4313 * regulator_set_load - set regulator load
4314 * @regulator: regulator source
4315 * @uA_load: load current
4317 * Notifies the regulator core of a new device load. This is then used by
4318 * DRMS (if enabled by constraints) to set the most efficient regulator
4319 * operating mode for the new regulator loading.
4321 * Consumer devices notify their supply regulator of the maximum power
4322 * they will require (can be taken from device datasheet in the power
4323 * consumption tables) when they change operational status and hence power
4324 * state. Examples of operational state changes that can affect power
4325 * consumption are :-
4327 * o Device is opened / closed.
4328 * o Device I/O is about to begin or has just finished.
4329 * o Device is idling in between work.
4331 * This information is also exported via sysfs to userspace.
4333 * DRMS will sum the total requested load on the regulator and change
4334 * to the most efficient operating mode if platform constraints allow.
4336 * NOTE: when a regulator consumer requests to have a regulator
4337 * disabled then any load that consumer requested no longer counts
4338 * toward the total requested load. If the regulator is re-enabled
4339 * then the previously requested load will start counting again.
4341 * If a regulator is an always-on regulator then an individual consumer's
4342 * load will still be removed if that consumer is fully disabled.
4344 * On error a negative errno is returned.
4346 int regulator_set_load(struct regulator *regulator, int uA_load)
4348 struct regulator_dev *rdev = regulator->rdev;
4352 regulator_lock(rdev);
4353 old_uA_load = regulator->uA_load;
4354 regulator->uA_load = uA_load;
4355 if (regulator->enable_count && old_uA_load != uA_load) {
4356 ret = drms_uA_update(rdev);
4358 regulator->uA_load = old_uA_load;
4360 regulator_unlock(rdev);
4364 EXPORT_SYMBOL_GPL(regulator_set_load);
4367 * regulator_allow_bypass - allow the regulator to go into bypass mode
4369 * @regulator: Regulator to configure
4370 * @enable: enable or disable bypass mode
4372 * Allow the regulator to go into bypass mode if all other consumers
4373 * for the regulator also enable bypass mode and the machine
4374 * constraints allow this. Bypass mode means that the regulator is
4375 * simply passing the input directly to the output with no regulation.
4377 int regulator_allow_bypass(struct regulator *regulator, bool enable)
4379 struct regulator_dev *rdev = regulator->rdev;
4382 if (!rdev->desc->ops->set_bypass)
4385 if (!regulator_ops_is_valid(rdev, REGULATOR_CHANGE_BYPASS))
4388 regulator_lock(rdev);
4390 if (enable && !regulator->bypass) {
4391 rdev->bypass_count++;
4393 if (rdev->bypass_count == rdev->open_count) {
4394 ret = rdev->desc->ops->set_bypass(rdev, enable);
4396 rdev->bypass_count--;
4399 } else if (!enable && regulator->bypass) {
4400 rdev->bypass_count--;
4402 if (rdev->bypass_count != rdev->open_count) {
4403 ret = rdev->desc->ops->set_bypass(rdev, enable);
4405 rdev->bypass_count++;
4410 regulator->bypass = enable;
4412 regulator_unlock(rdev);
4416 EXPORT_SYMBOL_GPL(regulator_allow_bypass);
4419 * regulator_register_notifier - register regulator event notifier
4420 * @regulator: regulator source
4421 * @nb: notifier block
4423 * Register notifier block to receive regulator events.
4425 int regulator_register_notifier(struct regulator *regulator,
4426 struct notifier_block *nb)
4428 return blocking_notifier_chain_register(®ulator->rdev->notifier,
4431 EXPORT_SYMBOL_GPL(regulator_register_notifier);
4434 * regulator_unregister_notifier - unregister regulator event notifier
4435 * @regulator: regulator source
4436 * @nb: notifier block
4438 * Unregister regulator event notifier block.
4440 int regulator_unregister_notifier(struct regulator *regulator,
4441 struct notifier_block *nb)
4443 return blocking_notifier_chain_unregister(®ulator->rdev->notifier,
4446 EXPORT_SYMBOL_GPL(regulator_unregister_notifier);
4448 /* notify regulator consumers and downstream regulator consumers.
4449 * Note mutex must be held by caller.
4451 static int _notifier_call_chain(struct regulator_dev *rdev,
4452 unsigned long event, void *data)
4454 /* call rdev chain first */
4455 return blocking_notifier_call_chain(&rdev->notifier, event, data);
4459 * regulator_bulk_get - get multiple regulator consumers
4461 * @dev: Device to supply
4462 * @num_consumers: Number of consumers to register
4463 * @consumers: Configuration of consumers; clients are stored here.
4465 * @return 0 on success, an errno on failure.
4467 * This helper function allows drivers to get several regulator
4468 * consumers in one operation. If any of the regulators cannot be
4469 * acquired then any regulators that were allocated will be freed
4470 * before returning to the caller.
4472 int regulator_bulk_get(struct device *dev, int num_consumers,
4473 struct regulator_bulk_data *consumers)
4478 for (i = 0; i < num_consumers; i++)
4479 consumers[i].consumer = NULL;
4481 for (i = 0; i < num_consumers; i++) {
4482 consumers[i].consumer = regulator_get(dev,
4483 consumers[i].supply);
4484 if (IS_ERR(consumers[i].consumer)) {
4485 ret = PTR_ERR(consumers[i].consumer);
4486 consumers[i].consumer = NULL;
4494 if (ret != -EPROBE_DEFER)
4495 dev_err(dev, "Failed to get supply '%s': %d\n",
4496 consumers[i].supply, ret);
4498 dev_dbg(dev, "Failed to get supply '%s', deferring\n",
4499 consumers[i].supply);
4502 regulator_put(consumers[i].consumer);
4506 EXPORT_SYMBOL_GPL(regulator_bulk_get);
4508 static void regulator_bulk_enable_async(void *data, async_cookie_t cookie)
4510 struct regulator_bulk_data *bulk = data;
4512 bulk->ret = regulator_enable(bulk->consumer);
4516 * regulator_bulk_enable - enable multiple regulator consumers
4518 * @num_consumers: Number of consumers
4519 * @consumers: Consumer data; clients are stored here.
4520 * @return 0 on success, an errno on failure
4522 * This convenience API allows consumers to enable multiple regulator
4523 * clients in a single API call. If any consumers cannot be enabled
4524 * then any others that were enabled will be disabled again prior to
4527 int regulator_bulk_enable(int num_consumers,
4528 struct regulator_bulk_data *consumers)
4530 ASYNC_DOMAIN_EXCLUSIVE(async_domain);
4534 for (i = 0; i < num_consumers; i++) {
4535 async_schedule_domain(regulator_bulk_enable_async,
4536 &consumers[i], &async_domain);
4539 async_synchronize_full_domain(&async_domain);
4541 /* If any consumer failed we need to unwind any that succeeded */
4542 for (i = 0; i < num_consumers; i++) {
4543 if (consumers[i].ret != 0) {
4544 ret = consumers[i].ret;
4552 for (i = 0; i < num_consumers; i++) {
4553 if (consumers[i].ret < 0)
4554 pr_err("Failed to enable %s: %d\n", consumers[i].supply,
4557 regulator_disable(consumers[i].consumer);
4562 EXPORT_SYMBOL_GPL(regulator_bulk_enable);
4565 * regulator_bulk_disable - disable multiple regulator consumers
4567 * @num_consumers: Number of consumers
4568 * @consumers: Consumer data; clients are stored here.
4569 * @return 0 on success, an errno on failure
4571 * This convenience API allows consumers to disable multiple regulator
4572 * clients in a single API call. If any consumers cannot be disabled
4573 * then any others that were disabled will be enabled again prior to
4576 int regulator_bulk_disable(int num_consumers,
4577 struct regulator_bulk_data *consumers)
4582 for (i = num_consumers - 1; i >= 0; --i) {
4583 ret = regulator_disable(consumers[i].consumer);
4591 pr_err("Failed to disable %s: %d\n", consumers[i].supply, ret);
4592 for (++i; i < num_consumers; ++i) {
4593 r = regulator_enable(consumers[i].consumer);
4595 pr_err("Failed to re-enable %s: %d\n",
4596 consumers[i].supply, r);
4601 EXPORT_SYMBOL_GPL(regulator_bulk_disable);
4604 * regulator_bulk_force_disable - force disable multiple regulator consumers
4606 * @num_consumers: Number of consumers
4607 * @consumers: Consumer data; clients are stored here.
4608 * @return 0 on success, an errno on failure
4610 * This convenience API allows consumers to forcibly disable multiple regulator
4611 * clients in a single API call.
4612 * NOTE: This should be used for situations when device damage will
4613 * likely occur if the regulators are not disabled (e.g. over temp).
4614 * Although regulator_force_disable function call for some consumers can
4615 * return error numbers, the function is called for all consumers.
4617 int regulator_bulk_force_disable(int num_consumers,
4618 struct regulator_bulk_data *consumers)
4623 for (i = 0; i < num_consumers; i++) {
4625 regulator_force_disable(consumers[i].consumer);
4627 /* Store first error for reporting */
4628 if (consumers[i].ret && !ret)
4629 ret = consumers[i].ret;
4634 EXPORT_SYMBOL_GPL(regulator_bulk_force_disable);
4637 * regulator_bulk_free - free multiple regulator consumers
4639 * @num_consumers: Number of consumers
4640 * @consumers: Consumer data; clients are stored here.
4642 * This convenience API allows consumers to free multiple regulator
4643 * clients in a single API call.
4645 void regulator_bulk_free(int num_consumers,
4646 struct regulator_bulk_data *consumers)
4650 for (i = 0; i < num_consumers; i++) {
4651 regulator_put(consumers[i].consumer);
4652 consumers[i].consumer = NULL;
4655 EXPORT_SYMBOL_GPL(regulator_bulk_free);
4658 * regulator_notifier_call_chain - call regulator event notifier
4659 * @rdev: regulator source
4660 * @event: notifier block
4661 * @data: callback-specific data.
4663 * Called by regulator drivers to notify clients a regulator event has
4664 * occurred. We also notify regulator clients downstream.
4665 * Note lock must be held by caller.
4667 int regulator_notifier_call_chain(struct regulator_dev *rdev,
4668 unsigned long event, void *data)
4670 lockdep_assert_held_once(&rdev->mutex.base);
4672 _notifier_call_chain(rdev, event, data);
4676 EXPORT_SYMBOL_GPL(regulator_notifier_call_chain);
4679 * regulator_mode_to_status - convert a regulator mode into a status
4681 * @mode: Mode to convert
4683 * Convert a regulator mode into a status.
4685 int regulator_mode_to_status(unsigned int mode)
4688 case REGULATOR_MODE_FAST:
4689 return REGULATOR_STATUS_FAST;
4690 case REGULATOR_MODE_NORMAL:
4691 return REGULATOR_STATUS_NORMAL;
4692 case REGULATOR_MODE_IDLE:
4693 return REGULATOR_STATUS_IDLE;
4694 case REGULATOR_MODE_STANDBY:
4695 return REGULATOR_STATUS_STANDBY;
4697 return REGULATOR_STATUS_UNDEFINED;
4700 EXPORT_SYMBOL_GPL(regulator_mode_to_status);
4702 static struct attribute *regulator_dev_attrs[] = {
4703 &dev_attr_name.attr,
4704 &dev_attr_num_users.attr,
4705 &dev_attr_type.attr,
4706 &dev_attr_microvolts.attr,
4707 &dev_attr_microamps.attr,
4708 &dev_attr_opmode.attr,
4709 &dev_attr_state.attr,
4710 &dev_attr_status.attr,
4711 &dev_attr_bypass.attr,
4712 &dev_attr_requested_microamps.attr,
4713 &dev_attr_min_microvolts.attr,
4714 &dev_attr_max_microvolts.attr,
4715 &dev_attr_min_microamps.attr,
4716 &dev_attr_max_microamps.attr,
4717 &dev_attr_suspend_standby_state.attr,
4718 &dev_attr_suspend_mem_state.attr,
4719 &dev_attr_suspend_disk_state.attr,
4720 &dev_attr_suspend_standby_microvolts.attr,
4721 &dev_attr_suspend_mem_microvolts.attr,
4722 &dev_attr_suspend_disk_microvolts.attr,
4723 &dev_attr_suspend_standby_mode.attr,
4724 &dev_attr_suspend_mem_mode.attr,
4725 &dev_attr_suspend_disk_mode.attr,
4730 * To avoid cluttering sysfs (and memory) with useless state, only
4731 * create attributes that can be meaningfully displayed.
4733 static umode_t regulator_attr_is_visible(struct kobject *kobj,
4734 struct attribute *attr, int idx)
4736 struct device *dev = kobj_to_dev(kobj);
4737 struct regulator_dev *rdev = dev_to_rdev(dev);
4738 const struct regulator_ops *ops = rdev->desc->ops;
4739 umode_t mode = attr->mode;
4741 /* these three are always present */
4742 if (attr == &dev_attr_name.attr ||
4743 attr == &dev_attr_num_users.attr ||
4744 attr == &dev_attr_type.attr)
4747 /* some attributes need specific methods to be displayed */
4748 if (attr == &dev_attr_microvolts.attr) {
4749 if ((ops->get_voltage && ops->get_voltage(rdev) >= 0) ||
4750 (ops->get_voltage_sel && ops->get_voltage_sel(rdev) >= 0) ||
4751 (ops->list_voltage && ops->list_voltage(rdev, 0) >= 0) ||
4752 (rdev->desc->fixed_uV && rdev->desc->n_voltages == 1))
4757 if (attr == &dev_attr_microamps.attr)
4758 return ops->get_current_limit ? mode : 0;
4760 if (attr == &dev_attr_opmode.attr)
4761 return ops->get_mode ? mode : 0;
4763 if (attr == &dev_attr_state.attr)
4764 return (rdev->ena_pin || ops->is_enabled) ? mode : 0;
4766 if (attr == &dev_attr_status.attr)
4767 return ops->get_status ? mode : 0;
4769 if (attr == &dev_attr_bypass.attr)
4770 return ops->get_bypass ? mode : 0;
4772 /* constraints need specific supporting methods */
4773 if (attr == &dev_attr_min_microvolts.attr ||
4774 attr == &dev_attr_max_microvolts.attr)
4775 return (ops->set_voltage || ops->set_voltage_sel) ? mode : 0;
4777 if (attr == &dev_attr_min_microamps.attr ||
4778 attr == &dev_attr_max_microamps.attr)
4779 return ops->set_current_limit ? mode : 0;
4781 if (attr == &dev_attr_suspend_standby_state.attr ||
4782 attr == &dev_attr_suspend_mem_state.attr ||
4783 attr == &dev_attr_suspend_disk_state.attr)
4786 if (attr == &dev_attr_suspend_standby_microvolts.attr ||
4787 attr == &dev_attr_suspend_mem_microvolts.attr ||
4788 attr == &dev_attr_suspend_disk_microvolts.attr)
4789 return ops->set_suspend_voltage ? mode : 0;
4791 if (attr == &dev_attr_suspend_standby_mode.attr ||
4792 attr == &dev_attr_suspend_mem_mode.attr ||
4793 attr == &dev_attr_suspend_disk_mode.attr)
4794 return ops->set_suspend_mode ? mode : 0;
4799 static const struct attribute_group regulator_dev_group = {
4800 .attrs = regulator_dev_attrs,
4801 .is_visible = regulator_attr_is_visible,
4804 static const struct attribute_group *regulator_dev_groups[] = {
4805 ®ulator_dev_group,
4809 static void regulator_dev_release(struct device *dev)
4811 struct regulator_dev *rdev = dev_get_drvdata(dev);
4813 debugfs_remove_recursive(rdev->debugfs);
4814 kfree(rdev->constraints);
4815 of_node_put(rdev->dev.of_node);
4819 static void rdev_init_debugfs(struct regulator_dev *rdev)
4821 struct device *parent = rdev->dev.parent;
4822 const char *rname = rdev_get_name(rdev);
4823 char name[NAME_MAX];
4825 /* Avoid duplicate debugfs directory names */
4826 if (parent && rname == rdev->desc->name) {
4827 snprintf(name, sizeof(name), "%s-%s", dev_name(parent),
4832 rdev->debugfs = debugfs_create_dir(rname, debugfs_root);
4833 if (!rdev->debugfs) {
4834 rdev_warn(rdev, "Failed to create debugfs directory\n");
4838 debugfs_create_u32("use_count", 0444, rdev->debugfs,
4840 debugfs_create_u32("open_count", 0444, rdev->debugfs,
4842 debugfs_create_u32("bypass_count", 0444, rdev->debugfs,
4843 &rdev->bypass_count);
4846 static int regulator_register_resolve_supply(struct device *dev, void *data)
4848 struct regulator_dev *rdev = dev_to_rdev(dev);
4850 if (regulator_resolve_supply(rdev))
4851 rdev_dbg(rdev, "unable to resolve supply\n");
4856 int regulator_coupler_register(struct regulator_coupler *coupler)
4858 mutex_lock(®ulator_list_mutex);
4859 list_add_tail(&coupler->list, ®ulator_coupler_list);
4860 mutex_unlock(®ulator_list_mutex);
4865 static struct regulator_coupler *
4866 regulator_find_coupler(struct regulator_dev *rdev)
4868 struct regulator_coupler *coupler;
4872 * Note that regulators are appended to the list and the generic
4873 * coupler is registered first, hence it will be attached at last
4876 list_for_each_entry_reverse(coupler, ®ulator_coupler_list, list) {
4877 err = coupler->attach_regulator(coupler, rdev);
4879 if (!coupler->balance_voltage &&
4880 rdev->coupling_desc.n_coupled > 2)
4881 goto err_unsupported;
4887 return ERR_PTR(err);
4895 return ERR_PTR(-EINVAL);
4898 if (coupler->detach_regulator)
4899 coupler->detach_regulator(coupler, rdev);
4902 "Voltage balancing for multiple regulator couples is unimplemented\n");
4904 return ERR_PTR(-EPERM);
4907 static void regulator_resolve_coupling(struct regulator_dev *rdev)
4909 struct regulator_coupler *coupler = rdev->coupling_desc.coupler;
4910 struct coupling_desc *c_desc = &rdev->coupling_desc;
4911 int n_coupled = c_desc->n_coupled;
4912 struct regulator_dev *c_rdev;
4915 for (i = 1; i < n_coupled; i++) {
4916 /* already resolved */
4917 if (c_desc->coupled_rdevs[i])
4920 c_rdev = of_parse_coupled_regulator(rdev, i - 1);
4925 if (c_rdev->coupling_desc.coupler != coupler) {
4926 rdev_err(rdev, "coupler mismatch with %s\n",
4927 rdev_get_name(c_rdev));
4931 c_desc->coupled_rdevs[i] = c_rdev;
4932 c_desc->n_resolved++;
4934 regulator_resolve_coupling(c_rdev);
4938 static void regulator_remove_coupling(struct regulator_dev *rdev)
4940 struct regulator_coupler *coupler = rdev->coupling_desc.coupler;
4941 struct coupling_desc *__c_desc, *c_desc = &rdev->coupling_desc;
4942 struct regulator_dev *__c_rdev, *c_rdev;
4943 unsigned int __n_coupled, n_coupled;
4947 n_coupled = c_desc->n_coupled;
4949 for (i = 1; i < n_coupled; i++) {
4950 c_rdev = c_desc->coupled_rdevs[i];
4955 regulator_lock(c_rdev);
4957 __c_desc = &c_rdev->coupling_desc;
4958 __n_coupled = __c_desc->n_coupled;
4960 for (k = 1; k < __n_coupled; k++) {
4961 __c_rdev = __c_desc->coupled_rdevs[k];
4963 if (__c_rdev == rdev) {
4964 __c_desc->coupled_rdevs[k] = NULL;
4965 __c_desc->n_resolved--;
4970 regulator_unlock(c_rdev);
4972 c_desc->coupled_rdevs[i] = NULL;
4973 c_desc->n_resolved--;
4976 if (coupler && coupler->detach_regulator) {
4977 err = coupler->detach_regulator(coupler, rdev);
4979 rdev_err(rdev, "failed to detach from coupler: %d\n",
4983 kfree(rdev->coupling_desc.coupled_rdevs);
4984 rdev->coupling_desc.coupled_rdevs = NULL;
4987 static int regulator_init_coupling(struct regulator_dev *rdev)
4989 int err, n_phandles;
4992 if (!IS_ENABLED(CONFIG_OF))
4995 n_phandles = of_get_n_coupled(rdev);
4997 alloc_size = sizeof(*rdev) * (n_phandles + 1);
4999 rdev->coupling_desc.coupled_rdevs = kzalloc(alloc_size, GFP_KERNEL);
5000 if (!rdev->coupling_desc.coupled_rdevs)
5004 * Every regulator should always have coupling descriptor filled with
5005 * at least pointer to itself.
5007 rdev->coupling_desc.coupled_rdevs[0] = rdev;
5008 rdev->coupling_desc.n_coupled = n_phandles + 1;
5009 rdev->coupling_desc.n_resolved++;
5011 /* regulator isn't coupled */
5012 if (n_phandles == 0)
5015 if (!of_check_coupling_data(rdev))
5018 mutex_lock(®ulator_list_mutex);
5019 rdev->coupling_desc.coupler = regulator_find_coupler(rdev);
5020 mutex_unlock(®ulator_list_mutex);
5022 if (IS_ERR(rdev->coupling_desc.coupler)) {
5023 err = PTR_ERR(rdev->coupling_desc.coupler);
5024 rdev_err(rdev, "failed to get coupler: %d\n", err);
5031 static int generic_coupler_attach(struct regulator_coupler *coupler,
5032 struct regulator_dev *rdev)
5034 if (rdev->coupling_desc.n_coupled > 2) {
5036 "Voltage balancing for multiple regulator couples is unimplemented\n");
5043 static struct regulator_coupler generic_regulator_coupler = {
5044 .attach_regulator = generic_coupler_attach,
5048 * regulator_register - register regulator
5049 * @regulator_desc: regulator to register
5050 * @cfg: runtime configuration for regulator
5052 * Called by regulator drivers to register a regulator.
5053 * Returns a valid pointer to struct regulator_dev on success
5054 * or an ERR_PTR() on error.
5056 struct regulator_dev *
5057 regulator_register(const struct regulator_desc *regulator_desc,
5058 const struct regulator_config *cfg)
5060 const struct regulator_init_data *init_data;
5061 struct regulator_config *config = NULL;
5062 static atomic_t regulator_no = ATOMIC_INIT(-1);
5063 struct regulator_dev *rdev;
5064 bool dangling_cfg_gpiod = false;
5065 bool dangling_of_gpiod = false;
5070 return ERR_PTR(-EINVAL);
5072 dangling_cfg_gpiod = true;
5073 if (regulator_desc == NULL) {
5081 if (regulator_desc->name == NULL || regulator_desc->ops == NULL) {
5086 if (regulator_desc->type != REGULATOR_VOLTAGE &&
5087 regulator_desc->type != REGULATOR_CURRENT) {
5092 /* Only one of each should be implemented */
5093 WARN_ON(regulator_desc->ops->get_voltage &&
5094 regulator_desc->ops->get_voltage_sel);
5095 WARN_ON(regulator_desc->ops->set_voltage &&
5096 regulator_desc->ops->set_voltage_sel);
5098 /* If we're using selectors we must implement list_voltage. */
5099 if (regulator_desc->ops->get_voltage_sel &&
5100 !regulator_desc->ops->list_voltage) {
5104 if (regulator_desc->ops->set_voltage_sel &&
5105 !regulator_desc->ops->list_voltage) {
5110 rdev = kzalloc(sizeof(struct regulator_dev), GFP_KERNEL);
5115 device_initialize(&rdev->dev);
5118 * Duplicate the config so the driver could override it after
5119 * parsing init data.
5121 config = kmemdup(cfg, sizeof(*cfg), GFP_KERNEL);
5122 if (config == NULL) {
5127 init_data = regulator_of_get_init_data(dev, regulator_desc, config,
5128 &rdev->dev.of_node);
5131 * Sometimes not all resources are probed already so we need to take
5132 * that into account. This happens most the time if the ena_gpiod comes
5133 * from a gpio extender or something else.
5135 if (PTR_ERR(init_data) == -EPROBE_DEFER) {
5136 ret = -EPROBE_DEFER;
5141 * We need to keep track of any GPIO descriptor coming from the
5142 * device tree until we have handled it over to the core. If the
5143 * config that was passed in to this function DOES NOT contain
5144 * a descriptor, and the config after this call DOES contain
5145 * a descriptor, we definitely got one from parsing the device
5148 if (!cfg->ena_gpiod && config->ena_gpiod)
5149 dangling_of_gpiod = true;
5151 init_data = config->init_data;
5152 rdev->dev.of_node = of_node_get(config->of_node);
5155 ww_mutex_init(&rdev->mutex, ®ulator_ww_class);
5156 rdev->reg_data = config->driver_data;
5157 rdev->owner = regulator_desc->owner;
5158 rdev->desc = regulator_desc;
5160 rdev->regmap = config->regmap;
5161 else if (dev_get_regmap(dev, NULL))
5162 rdev->regmap = dev_get_regmap(dev, NULL);
5163 else if (dev->parent)
5164 rdev->regmap = dev_get_regmap(dev->parent, NULL);
5165 INIT_LIST_HEAD(&rdev->consumer_list);
5166 INIT_LIST_HEAD(&rdev->list);
5167 BLOCKING_INIT_NOTIFIER_HEAD(&rdev->notifier);
5168 INIT_DELAYED_WORK(&rdev->disable_work, regulator_disable_work);
5170 /* preform any regulator specific init */
5171 if (init_data && init_data->regulator_init) {
5172 ret = init_data->regulator_init(rdev->reg_data);
5177 if (config->ena_gpiod) {
5178 ret = regulator_ena_gpio_request(rdev, config);
5180 rdev_err(rdev, "Failed to request enable GPIO: %d\n",
5184 /* The regulator core took over the GPIO descriptor */
5185 dangling_cfg_gpiod = false;
5186 dangling_of_gpiod = false;
5189 /* register with sysfs */
5190 rdev->dev.class = ®ulator_class;
5191 rdev->dev.parent = dev;
5192 dev_set_name(&rdev->dev, "regulator.%lu",
5193 (unsigned long) atomic_inc_return(®ulator_no));
5194 dev_set_drvdata(&rdev->dev, rdev);
5196 /* set regulator constraints */
5198 rdev->constraints = kmemdup(&init_data->constraints,
5199 sizeof(*rdev->constraints),
5202 rdev->constraints = kzalloc(sizeof(*rdev->constraints),
5204 if (!rdev->constraints) {
5209 if (init_data && init_data->supply_regulator)
5210 rdev->supply_name = init_data->supply_regulator;
5211 else if (regulator_desc->supply_name)
5212 rdev->supply_name = regulator_desc->supply_name;
5214 ret = set_machine_constraints(rdev);
5215 if (ret == -EPROBE_DEFER) {
5216 /* Regulator might be in bypass mode and so needs its supply
5217 * to set the constraints */
5218 /* FIXME: this currently triggers a chicken-and-egg problem
5219 * when creating -SUPPLY symlink in sysfs to a regulator
5220 * that is just being created */
5221 ret = regulator_resolve_supply(rdev);
5223 ret = set_machine_constraints(rdev);
5225 rdev_dbg(rdev, "unable to resolve supply early: %pe\n",
5231 ret = regulator_init_coupling(rdev);
5235 /* add consumers devices */
5237 for (i = 0; i < init_data->num_consumer_supplies; i++) {
5238 ret = set_consumer_device_supply(rdev,
5239 init_data->consumer_supplies[i].dev_name,
5240 init_data->consumer_supplies[i].supply);
5242 dev_err(dev, "Failed to set supply %s\n",
5243 init_data->consumer_supplies[i].supply);
5244 goto unset_supplies;
5249 if (!rdev->desc->ops->get_voltage &&
5250 !rdev->desc->ops->list_voltage &&
5251 !rdev->desc->fixed_uV)
5252 rdev->is_switch = true;
5254 ret = device_add(&rdev->dev);
5256 goto unset_supplies;
5258 rdev_init_debugfs(rdev);
5260 /* try to resolve regulators coupling since a new one was registered */
5261 mutex_lock(®ulator_list_mutex);
5262 regulator_resolve_coupling(rdev);
5263 mutex_unlock(®ulator_list_mutex);
5265 /* try to resolve regulators supply since a new one was registered */
5266 class_for_each_device(®ulator_class, NULL, NULL,
5267 regulator_register_resolve_supply);
5272 mutex_lock(®ulator_list_mutex);
5273 unset_regulator_supplies(rdev);
5274 regulator_remove_coupling(rdev);
5275 mutex_unlock(®ulator_list_mutex);
5277 regulator_put(rdev->supply);
5278 kfree(rdev->coupling_desc.coupled_rdevs);
5279 mutex_lock(®ulator_list_mutex);
5280 regulator_ena_gpio_free(rdev);
5281 mutex_unlock(®ulator_list_mutex);
5282 put_device(&rdev->dev);
5285 if (dangling_of_gpiod)
5286 gpiod_put(config->ena_gpiod);
5287 if (rdev && rdev->dev.of_node)
5288 of_node_put(rdev->dev.of_node);
5292 if (dangling_cfg_gpiod)
5293 gpiod_put(cfg->ena_gpiod);
5294 return ERR_PTR(ret);
5296 EXPORT_SYMBOL_GPL(regulator_register);
5299 * regulator_unregister - unregister regulator
5300 * @rdev: regulator to unregister
5302 * Called by regulator drivers to unregister a regulator.
5304 void regulator_unregister(struct regulator_dev *rdev)
5310 while (rdev->use_count--)
5311 regulator_disable(rdev->supply);
5312 regulator_put(rdev->supply);
5315 flush_work(&rdev->disable_work.work);
5317 mutex_lock(®ulator_list_mutex);
5319 WARN_ON(rdev->open_count);
5320 regulator_remove_coupling(rdev);
5321 unset_regulator_supplies(rdev);
5322 list_del(&rdev->list);
5323 regulator_ena_gpio_free(rdev);
5324 device_unregister(&rdev->dev);
5326 mutex_unlock(®ulator_list_mutex);
5328 EXPORT_SYMBOL_GPL(regulator_unregister);
5330 #ifdef CONFIG_SUSPEND
5332 * regulator_suspend - prepare regulators for system wide suspend
5333 * @dev: ``&struct device`` pointer that is passed to _regulator_suspend()
5335 * Configure each regulator with it's suspend operating parameters for state.
5337 static int regulator_suspend(struct device *dev)
5339 struct regulator_dev *rdev = dev_to_rdev(dev);
5340 suspend_state_t state = pm_suspend_target_state;
5343 regulator_lock(rdev);
5344 ret = suspend_set_state(rdev, state);
5345 regulator_unlock(rdev);
5350 static int regulator_resume(struct device *dev)
5352 suspend_state_t state = pm_suspend_target_state;
5353 struct regulator_dev *rdev = dev_to_rdev(dev);
5354 struct regulator_state *rstate;
5357 rstate = regulator_get_suspend_state(rdev, state);
5361 regulator_lock(rdev);
5363 if (rdev->desc->ops->resume &&
5364 (rstate->enabled == ENABLE_IN_SUSPEND ||
5365 rstate->enabled == DISABLE_IN_SUSPEND))
5366 ret = rdev->desc->ops->resume(rdev);
5368 regulator_unlock(rdev);
5372 #else /* !CONFIG_SUSPEND */
5374 #define regulator_suspend NULL
5375 #define regulator_resume NULL
5377 #endif /* !CONFIG_SUSPEND */
5380 static const struct dev_pm_ops __maybe_unused regulator_pm_ops = {
5381 .suspend = regulator_suspend,
5382 .resume = regulator_resume,
5386 struct class regulator_class = {
5387 .name = "regulator",
5388 .dev_release = regulator_dev_release,
5389 .dev_groups = regulator_dev_groups,
5391 .pm = ®ulator_pm_ops,
5395 * regulator_has_full_constraints - the system has fully specified constraints
5397 * Calling this function will cause the regulator API to disable all
5398 * regulators which have a zero use count and don't have an always_on
5399 * constraint in a late_initcall.
5401 * The intention is that this will become the default behaviour in a
5402 * future kernel release so users are encouraged to use this facility
5405 void regulator_has_full_constraints(void)
5407 has_full_constraints = 1;
5409 EXPORT_SYMBOL_GPL(regulator_has_full_constraints);
5412 * rdev_get_drvdata - get rdev regulator driver data
5415 * Get rdev regulator driver private data. This call can be used in the
5416 * regulator driver context.
5418 void *rdev_get_drvdata(struct regulator_dev *rdev)
5420 return rdev->reg_data;
5422 EXPORT_SYMBOL_GPL(rdev_get_drvdata);
5425 * regulator_get_drvdata - get regulator driver data
5426 * @regulator: regulator
5428 * Get regulator driver private data. This call can be used in the consumer
5429 * driver context when non API regulator specific functions need to be called.
5431 void *regulator_get_drvdata(struct regulator *regulator)
5433 return regulator->rdev->reg_data;
5435 EXPORT_SYMBOL_GPL(regulator_get_drvdata);
5438 * regulator_set_drvdata - set regulator driver data
5439 * @regulator: regulator
5442 void regulator_set_drvdata(struct regulator *regulator, void *data)
5444 regulator->rdev->reg_data = data;
5446 EXPORT_SYMBOL_GPL(regulator_set_drvdata);
5449 * regulator_get_id - get regulator ID
5452 int rdev_get_id(struct regulator_dev *rdev)
5454 return rdev->desc->id;
5456 EXPORT_SYMBOL_GPL(rdev_get_id);
5458 struct device *rdev_get_dev(struct regulator_dev *rdev)
5462 EXPORT_SYMBOL_GPL(rdev_get_dev);
5464 struct regmap *rdev_get_regmap(struct regulator_dev *rdev)
5466 return rdev->regmap;
5468 EXPORT_SYMBOL_GPL(rdev_get_regmap);
5470 void *regulator_get_init_drvdata(struct regulator_init_data *reg_init_data)
5472 return reg_init_data->driver_data;
5474 EXPORT_SYMBOL_GPL(regulator_get_init_drvdata);
5476 #ifdef CONFIG_DEBUG_FS
5477 static int supply_map_show(struct seq_file *sf, void *data)
5479 struct regulator_map *map;
5481 list_for_each_entry(map, ®ulator_map_list, list) {
5482 seq_printf(sf, "%s -> %s.%s\n",
5483 rdev_get_name(map->regulator), map->dev_name,
5489 DEFINE_SHOW_ATTRIBUTE(supply_map);
5491 struct summary_data {
5493 struct regulator_dev *parent;
5497 static void regulator_summary_show_subtree(struct seq_file *s,
5498 struct regulator_dev *rdev,
5501 static int regulator_summary_show_children(struct device *dev, void *data)
5503 struct regulator_dev *rdev = dev_to_rdev(dev);
5504 struct summary_data *summary_data = data;
5506 if (rdev->supply && rdev->supply->rdev == summary_data->parent)
5507 regulator_summary_show_subtree(summary_data->s, rdev,
5508 summary_data->level + 1);
5513 static void regulator_summary_show_subtree(struct seq_file *s,
5514 struct regulator_dev *rdev,
5517 struct regulation_constraints *c;
5518 struct regulator *consumer;
5519 struct summary_data summary_data;
5520 unsigned int opmode;
5525 opmode = _regulator_get_mode_unlocked(rdev);
5526 seq_printf(s, "%*s%-*s %3d %4d %6d %7s ",
5528 30 - level * 3, rdev_get_name(rdev),
5529 rdev->use_count, rdev->open_count, rdev->bypass_count,
5530 regulator_opmode_to_str(opmode));
5532 seq_printf(s, "%5dmV ", regulator_get_voltage_rdev(rdev) / 1000);
5533 seq_printf(s, "%5dmA ",
5534 _regulator_get_current_limit_unlocked(rdev) / 1000);
5536 c = rdev->constraints;
5538 switch (rdev->desc->type) {
5539 case REGULATOR_VOLTAGE:
5540 seq_printf(s, "%5dmV %5dmV ",
5541 c->min_uV / 1000, c->max_uV / 1000);
5543 case REGULATOR_CURRENT:
5544 seq_printf(s, "%5dmA %5dmA ",
5545 c->min_uA / 1000, c->max_uA / 1000);
5552 list_for_each_entry(consumer, &rdev->consumer_list, list) {
5553 if (consumer->dev && consumer->dev->class == ®ulator_class)
5556 seq_printf(s, "%*s%-*s ",
5557 (level + 1) * 3 + 1, "",
5558 30 - (level + 1) * 3,
5559 consumer->dev ? dev_name(consumer->dev) : "deviceless");
5561 switch (rdev->desc->type) {
5562 case REGULATOR_VOLTAGE:
5563 seq_printf(s, "%3d %33dmA%c%5dmV %5dmV",
5564 consumer->enable_count,
5565 consumer->uA_load / 1000,
5566 consumer->uA_load && !consumer->enable_count ?
5568 consumer->voltage[PM_SUSPEND_ON].min_uV / 1000,
5569 consumer->voltage[PM_SUSPEND_ON].max_uV / 1000);
5571 case REGULATOR_CURRENT:
5579 summary_data.level = level;
5580 summary_data.parent = rdev;
5582 class_for_each_device(®ulator_class, NULL, &summary_data,
5583 regulator_summary_show_children);
5586 struct summary_lock_data {
5587 struct ww_acquire_ctx *ww_ctx;
5588 struct regulator_dev **new_contended_rdev;
5589 struct regulator_dev **old_contended_rdev;
5592 static int regulator_summary_lock_one(struct device *dev, void *data)
5594 struct regulator_dev *rdev = dev_to_rdev(dev);
5595 struct summary_lock_data *lock_data = data;
5598 if (rdev != *lock_data->old_contended_rdev) {
5599 ret = regulator_lock_nested(rdev, lock_data->ww_ctx);
5601 if (ret == -EDEADLK)
5602 *lock_data->new_contended_rdev = rdev;
5606 *lock_data->old_contended_rdev = NULL;
5612 static int regulator_summary_unlock_one(struct device *dev, void *data)
5614 struct regulator_dev *rdev = dev_to_rdev(dev);
5615 struct summary_lock_data *lock_data = data;
5618 if (rdev == *lock_data->new_contended_rdev)
5622 regulator_unlock(rdev);
5627 static int regulator_summary_lock_all(struct ww_acquire_ctx *ww_ctx,
5628 struct regulator_dev **new_contended_rdev,
5629 struct regulator_dev **old_contended_rdev)
5631 struct summary_lock_data lock_data;
5634 lock_data.ww_ctx = ww_ctx;
5635 lock_data.new_contended_rdev = new_contended_rdev;
5636 lock_data.old_contended_rdev = old_contended_rdev;
5638 ret = class_for_each_device(®ulator_class, NULL, &lock_data,
5639 regulator_summary_lock_one);
5641 class_for_each_device(®ulator_class, NULL, &lock_data,
5642 regulator_summary_unlock_one);
5647 static void regulator_summary_lock(struct ww_acquire_ctx *ww_ctx)
5649 struct regulator_dev *new_contended_rdev = NULL;
5650 struct regulator_dev *old_contended_rdev = NULL;
5653 mutex_lock(®ulator_list_mutex);
5655 ww_acquire_init(ww_ctx, ®ulator_ww_class);
5658 if (new_contended_rdev) {
5659 ww_mutex_lock_slow(&new_contended_rdev->mutex, ww_ctx);
5660 old_contended_rdev = new_contended_rdev;
5661 old_contended_rdev->ref_cnt++;
5664 err = regulator_summary_lock_all(ww_ctx,
5665 &new_contended_rdev,
5666 &old_contended_rdev);
5668 if (old_contended_rdev)
5669 regulator_unlock(old_contended_rdev);
5671 } while (err == -EDEADLK);
5673 ww_acquire_done(ww_ctx);
5676 static void regulator_summary_unlock(struct ww_acquire_ctx *ww_ctx)
5678 class_for_each_device(®ulator_class, NULL, NULL,
5679 regulator_summary_unlock_one);
5680 ww_acquire_fini(ww_ctx);
5682 mutex_unlock(®ulator_list_mutex);
5685 static int regulator_summary_show_roots(struct device *dev, void *data)
5687 struct regulator_dev *rdev = dev_to_rdev(dev);
5688 struct seq_file *s = data;
5691 regulator_summary_show_subtree(s, rdev, 0);
5696 static int regulator_summary_show(struct seq_file *s, void *data)
5698 struct ww_acquire_ctx ww_ctx;
5700 seq_puts(s, " regulator use open bypass opmode voltage current min max\n");
5701 seq_puts(s, "---------------------------------------------------------------------------------------\n");
5703 regulator_summary_lock(&ww_ctx);
5705 class_for_each_device(®ulator_class, NULL, s,
5706 regulator_summary_show_roots);
5708 regulator_summary_unlock(&ww_ctx);
5712 DEFINE_SHOW_ATTRIBUTE(regulator_summary);
5713 #endif /* CONFIG_DEBUG_FS */
5715 static int __init regulator_init(void)
5719 ret = class_register(®ulator_class);
5721 debugfs_root = debugfs_create_dir("regulator", NULL);
5723 pr_warn("regulator: Failed to create debugfs directory\n");
5725 #ifdef CONFIG_DEBUG_FS
5726 debugfs_create_file("supply_map", 0444, debugfs_root, NULL,
5729 debugfs_create_file("regulator_summary", 0444, debugfs_root,
5730 NULL, ®ulator_summary_fops);
5732 regulator_dummy_init();
5734 regulator_coupler_register(&generic_regulator_coupler);
5739 /* init early to allow our consumers to complete system booting */
5740 core_initcall(regulator_init);
5742 static int regulator_late_cleanup(struct device *dev, void *data)
5744 struct regulator_dev *rdev = dev_to_rdev(dev);
5745 const struct regulator_ops *ops = rdev->desc->ops;
5746 struct regulation_constraints *c = rdev->constraints;
5749 if (c && c->always_on)
5752 if (!regulator_ops_is_valid(rdev, REGULATOR_CHANGE_STATUS))
5755 regulator_lock(rdev);
5757 if (rdev->use_count)
5760 /* If we can't read the status assume it's on. */
5761 if (ops->is_enabled)
5762 enabled = ops->is_enabled(rdev);
5769 if (have_full_constraints()) {
5770 /* We log since this may kill the system if it goes
5772 rdev_info(rdev, "disabling\n");
5773 ret = _regulator_do_disable(rdev);
5775 rdev_err(rdev, "couldn't disable: %d\n", ret);
5777 /* The intention is that in future we will
5778 * assume that full constraints are provided
5779 * so warn even if we aren't going to do
5782 rdev_warn(rdev, "incomplete constraints, leaving on\n");
5786 regulator_unlock(rdev);
5791 static void regulator_init_complete_work_function(struct work_struct *work)
5794 * Regulators may had failed to resolve their input supplies
5795 * when were registered, either because the input supply was
5796 * not registered yet or because its parent device was not
5797 * bound yet. So attempt to resolve the input supplies for
5798 * pending regulators before trying to disable unused ones.
5800 class_for_each_device(®ulator_class, NULL, NULL,
5801 regulator_register_resolve_supply);
5803 /* If we have a full configuration then disable any regulators
5804 * we have permission to change the status for and which are
5805 * not in use or always_on. This is effectively the default
5806 * for DT and ACPI as they have full constraints.
5808 class_for_each_device(®ulator_class, NULL, NULL,
5809 regulator_late_cleanup);
5812 static DECLARE_DELAYED_WORK(regulator_init_complete_work,
5813 regulator_init_complete_work_function);
5815 static int __init regulator_init_complete(void)
5818 * Since DT doesn't provide an idiomatic mechanism for
5819 * enabling full constraints and since it's much more natural
5820 * with DT to provide them just assume that a DT enabled
5821 * system has full constraints.
5823 if (of_have_populated_dt())
5824 has_full_constraints = true;
5827 * We punt completion for an arbitrary amount of time since
5828 * systems like distros will load many drivers from userspace
5829 * so consumers might not always be ready yet, this is
5830 * particularly an issue with laptops where this might bounce
5831 * the display off then on. Ideally we'd get a notification
5832 * from userspace when this happens but we don't so just wait
5833 * a bit and hope we waited long enough. It'd be better if
5834 * we'd only do this on systems that need it, and a kernel
5835 * command line option might be useful.
5837 schedule_delayed_work(®ulator_init_complete_work,
5838 msecs_to_jiffies(30000));
5842 late_initcall_sync(regulator_init_complete);