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 static DEFINE_WW_CLASS(regulator_ww_class);
37 static DEFINE_MUTEX(regulator_nesting_mutex);
38 static DEFINE_MUTEX(regulator_list_mutex);
39 static LIST_HEAD(regulator_map_list);
40 static LIST_HEAD(regulator_ena_gpio_list);
41 static LIST_HEAD(regulator_supply_alias_list);
42 static LIST_HEAD(regulator_coupler_list);
43 static bool has_full_constraints;
45 static struct dentry *debugfs_root;
48 * struct regulator_map
50 * Used to provide symbolic supply names to devices.
52 struct regulator_map {
53 struct list_head list;
54 const char *dev_name; /* The dev_name() for the consumer */
56 struct regulator_dev *regulator;
60 * struct regulator_enable_gpio
62 * Management for shared enable GPIO pin
64 struct regulator_enable_gpio {
65 struct list_head list;
66 struct gpio_desc *gpiod;
67 u32 enable_count; /* a number of enabled shared GPIO */
68 u32 request_count; /* a number of requested shared GPIO */
72 * struct regulator_supply_alias
74 * Used to map lookups for a supply onto an alternative device.
76 struct regulator_supply_alias {
77 struct list_head list;
78 struct device *src_dev;
79 const char *src_supply;
80 struct device *alias_dev;
81 const char *alias_supply;
84 static int _regulator_is_enabled(struct regulator_dev *rdev);
85 static int _regulator_disable(struct regulator *regulator);
86 static int _regulator_get_error_flags(struct regulator_dev *rdev, unsigned int *flags);
87 static int _regulator_get_current_limit(struct regulator_dev *rdev);
88 static unsigned int _regulator_get_mode(struct regulator_dev *rdev);
89 static int _notifier_call_chain(struct regulator_dev *rdev,
90 unsigned long event, void *data);
91 static int _regulator_do_set_voltage(struct regulator_dev *rdev,
92 int min_uV, int max_uV);
93 static int regulator_balance_voltage(struct regulator_dev *rdev,
94 suspend_state_t state);
95 static struct regulator *create_regulator(struct regulator_dev *rdev,
97 const char *supply_name);
98 static void destroy_regulator(struct regulator *regulator);
99 static void _regulator_put(struct regulator *regulator);
101 const char *rdev_get_name(struct regulator_dev *rdev)
103 if (rdev->constraints && rdev->constraints->name)
104 return rdev->constraints->name;
105 else if (rdev->desc->name)
106 return rdev->desc->name;
110 EXPORT_SYMBOL_GPL(rdev_get_name);
112 static bool have_full_constraints(void)
114 return has_full_constraints || of_have_populated_dt();
117 static bool regulator_ops_is_valid(struct regulator_dev *rdev, int ops)
119 if (!rdev->constraints) {
120 rdev_err(rdev, "no constraints\n");
124 if (rdev->constraints->valid_ops_mask & ops)
131 * regulator_lock_nested - lock a single regulator
132 * @rdev: regulator source
133 * @ww_ctx: w/w mutex acquire context
135 * This function can be called many times by one task on
136 * a single regulator and its mutex will be locked only
137 * once. If a task, which is calling this function is other
138 * than the one, which initially locked the mutex, it will
141 static inline int regulator_lock_nested(struct regulator_dev *rdev,
142 struct ww_acquire_ctx *ww_ctx)
147 mutex_lock(®ulator_nesting_mutex);
149 if (!ww_mutex_trylock(&rdev->mutex, ww_ctx)) {
150 if (rdev->mutex_owner == current)
156 mutex_unlock(®ulator_nesting_mutex);
157 ret = ww_mutex_lock(&rdev->mutex, ww_ctx);
158 mutex_lock(®ulator_nesting_mutex);
164 if (lock && ret != -EDEADLK) {
166 rdev->mutex_owner = current;
169 mutex_unlock(®ulator_nesting_mutex);
175 * regulator_lock - lock a single regulator
176 * @rdev: regulator source
178 * This function can be called many times by one task on
179 * a single regulator and its mutex will be locked only
180 * once. If a task, which is calling this function is other
181 * than the one, which initially locked the mutex, it will
184 static void regulator_lock(struct regulator_dev *rdev)
186 regulator_lock_nested(rdev, NULL);
190 * regulator_unlock - unlock a single regulator
191 * @rdev: regulator_source
193 * This function unlocks the mutex when the
194 * reference counter reaches 0.
196 static void regulator_unlock(struct regulator_dev *rdev)
198 mutex_lock(®ulator_nesting_mutex);
200 if (--rdev->ref_cnt == 0) {
201 rdev->mutex_owner = NULL;
202 ww_mutex_unlock(&rdev->mutex);
205 WARN_ON_ONCE(rdev->ref_cnt < 0);
207 mutex_unlock(®ulator_nesting_mutex);
211 * regulator_lock_two - lock two regulators
212 * @rdev1: first regulator
213 * @rdev2: second regulator
214 * @ww_ctx: w/w mutex acquire context
216 * Locks both rdevs using the regulator_ww_class.
218 static void regulator_lock_two(struct regulator_dev *rdev1,
219 struct regulator_dev *rdev2,
220 struct ww_acquire_ctx *ww_ctx)
222 struct regulator_dev *held, *contended;
225 ww_acquire_init(ww_ctx, ®ulator_ww_class);
227 /* Try to just grab both of them */
228 ret = regulator_lock_nested(rdev1, ww_ctx);
230 ret = regulator_lock_nested(rdev2, ww_ctx);
231 if (ret != -EDEADLOCK) {
239 regulator_unlock(held);
241 ww_mutex_lock_slow(&contended->mutex, ww_ctx);
242 contended->ref_cnt++;
243 contended->mutex_owner = current;
244 swap(held, contended);
245 ret = regulator_lock_nested(contended, ww_ctx);
247 if (ret != -EDEADLOCK) {
254 ww_acquire_done(ww_ctx);
258 * regulator_unlock_two - unlock two regulators
259 * @rdev1: first regulator
260 * @rdev2: second regulator
261 * @ww_ctx: w/w mutex acquire context
263 * The inverse of regulator_lock_two().
266 static void regulator_unlock_two(struct regulator_dev *rdev1,
267 struct regulator_dev *rdev2,
268 struct ww_acquire_ctx *ww_ctx)
270 regulator_unlock(rdev2);
271 regulator_unlock(rdev1);
272 ww_acquire_fini(ww_ctx);
275 static bool regulator_supply_is_couple(struct regulator_dev *rdev)
277 struct regulator_dev *c_rdev;
280 for (i = 1; i < rdev->coupling_desc.n_coupled; i++) {
281 c_rdev = rdev->coupling_desc.coupled_rdevs[i];
283 if (rdev->supply->rdev == c_rdev)
290 static void regulator_unlock_recursive(struct regulator_dev *rdev,
291 unsigned int n_coupled)
293 struct regulator_dev *c_rdev, *supply_rdev;
294 int i, supply_n_coupled;
296 for (i = n_coupled; i > 0; i--) {
297 c_rdev = rdev->coupling_desc.coupled_rdevs[i - 1];
302 if (c_rdev->supply && !regulator_supply_is_couple(c_rdev)) {
303 supply_rdev = c_rdev->supply->rdev;
304 supply_n_coupled = supply_rdev->coupling_desc.n_coupled;
306 regulator_unlock_recursive(supply_rdev,
310 regulator_unlock(c_rdev);
314 static int regulator_lock_recursive(struct regulator_dev *rdev,
315 struct regulator_dev **new_contended_rdev,
316 struct regulator_dev **old_contended_rdev,
317 struct ww_acquire_ctx *ww_ctx)
319 struct regulator_dev *c_rdev;
322 for (i = 0; i < rdev->coupling_desc.n_coupled; i++) {
323 c_rdev = rdev->coupling_desc.coupled_rdevs[i];
328 if (c_rdev != *old_contended_rdev) {
329 err = regulator_lock_nested(c_rdev, ww_ctx);
331 if (err == -EDEADLK) {
332 *new_contended_rdev = c_rdev;
336 /* shouldn't happen */
337 WARN_ON_ONCE(err != -EALREADY);
340 *old_contended_rdev = NULL;
343 if (c_rdev->supply && !regulator_supply_is_couple(c_rdev)) {
344 err = regulator_lock_recursive(c_rdev->supply->rdev,
349 regulator_unlock(c_rdev);
358 regulator_unlock_recursive(rdev, i);
364 * regulator_unlock_dependent - unlock regulator's suppliers and coupled
366 * @rdev: regulator source
367 * @ww_ctx: w/w mutex acquire context
369 * Unlock all regulators related with rdev by coupling or supplying.
371 static void regulator_unlock_dependent(struct regulator_dev *rdev,
372 struct ww_acquire_ctx *ww_ctx)
374 regulator_unlock_recursive(rdev, rdev->coupling_desc.n_coupled);
375 ww_acquire_fini(ww_ctx);
379 * regulator_lock_dependent - lock regulator's suppliers and coupled regulators
380 * @rdev: regulator source
381 * @ww_ctx: w/w mutex acquire context
383 * This function as a wrapper on regulator_lock_recursive(), which locks
384 * all regulators related with rdev by coupling or supplying.
386 static void regulator_lock_dependent(struct regulator_dev *rdev,
387 struct ww_acquire_ctx *ww_ctx)
389 struct regulator_dev *new_contended_rdev = NULL;
390 struct regulator_dev *old_contended_rdev = NULL;
393 mutex_lock(®ulator_list_mutex);
395 ww_acquire_init(ww_ctx, ®ulator_ww_class);
398 if (new_contended_rdev) {
399 ww_mutex_lock_slow(&new_contended_rdev->mutex, ww_ctx);
400 old_contended_rdev = new_contended_rdev;
401 old_contended_rdev->ref_cnt++;
402 old_contended_rdev->mutex_owner = current;
405 err = regulator_lock_recursive(rdev,
410 if (old_contended_rdev)
411 regulator_unlock(old_contended_rdev);
413 } while (err == -EDEADLK);
415 ww_acquire_done(ww_ctx);
417 mutex_unlock(®ulator_list_mutex);
421 * of_get_child_regulator - get a child regulator device node
422 * based on supply name
423 * @parent: Parent device node
424 * @prop_name: Combination regulator supply name and "-supply"
426 * Traverse all child nodes.
427 * Extract the child regulator device node corresponding to the supply name.
428 * returns the device node corresponding to the regulator if found, else
431 static struct device_node *of_get_child_regulator(struct device_node *parent,
432 const char *prop_name)
434 struct device_node *regnode = NULL;
435 struct device_node *child = NULL;
437 for_each_child_of_node(parent, child) {
438 regnode = of_parse_phandle(child, prop_name, 0);
441 regnode = of_get_child_regulator(child, prop_name);
456 * of_get_regulator - get a regulator device node based on supply name
457 * @dev: Device pointer for the consumer (of regulator) device
458 * @supply: regulator supply name
460 * Extract the regulator device node corresponding to the supply name.
461 * returns the device node corresponding to the regulator if found, else
464 static struct device_node *of_get_regulator(struct device *dev, const char *supply)
466 struct device_node *regnode = NULL;
467 char prop_name[64]; /* 64 is max size of property name */
469 dev_dbg(dev, "Looking up %s-supply from device tree\n", supply);
471 snprintf(prop_name, 64, "%s-supply", supply);
472 regnode = of_parse_phandle(dev->of_node, prop_name, 0);
475 regnode = of_get_child_regulator(dev->of_node, prop_name);
479 dev_dbg(dev, "Looking up %s property in node %pOF failed\n",
480 prop_name, dev->of_node);
486 /* Platform voltage constraint check */
487 int regulator_check_voltage(struct regulator_dev *rdev,
488 int *min_uV, int *max_uV)
490 BUG_ON(*min_uV > *max_uV);
492 if (!regulator_ops_is_valid(rdev, REGULATOR_CHANGE_VOLTAGE)) {
493 rdev_err(rdev, "voltage operation not allowed\n");
497 if (*max_uV > rdev->constraints->max_uV)
498 *max_uV = rdev->constraints->max_uV;
499 if (*min_uV < rdev->constraints->min_uV)
500 *min_uV = rdev->constraints->min_uV;
502 if (*min_uV > *max_uV) {
503 rdev_err(rdev, "unsupportable voltage range: %d-%duV\n",
511 /* return 0 if the state is valid */
512 static int regulator_check_states(suspend_state_t state)
514 return (state > PM_SUSPEND_MAX || state == PM_SUSPEND_TO_IDLE);
517 /* Make sure we select a voltage that suits the needs of all
518 * regulator consumers
520 int regulator_check_consumers(struct regulator_dev *rdev,
521 int *min_uV, int *max_uV,
522 suspend_state_t state)
524 struct regulator *regulator;
525 struct regulator_voltage *voltage;
527 list_for_each_entry(regulator, &rdev->consumer_list, list) {
528 voltage = ®ulator->voltage[state];
530 * Assume consumers that didn't say anything are OK
531 * with anything in the constraint range.
533 if (!voltage->min_uV && !voltage->max_uV)
536 if (*max_uV > voltage->max_uV)
537 *max_uV = voltage->max_uV;
538 if (*min_uV < voltage->min_uV)
539 *min_uV = voltage->min_uV;
542 if (*min_uV > *max_uV) {
543 rdev_err(rdev, "Restricting voltage, %u-%uuV\n",
551 /* current constraint check */
552 static int regulator_check_current_limit(struct regulator_dev *rdev,
553 int *min_uA, int *max_uA)
555 BUG_ON(*min_uA > *max_uA);
557 if (!regulator_ops_is_valid(rdev, REGULATOR_CHANGE_CURRENT)) {
558 rdev_err(rdev, "current operation not allowed\n");
562 if (*max_uA > rdev->constraints->max_uA)
563 *max_uA = rdev->constraints->max_uA;
564 if (*min_uA < rdev->constraints->min_uA)
565 *min_uA = rdev->constraints->min_uA;
567 if (*min_uA > *max_uA) {
568 rdev_err(rdev, "unsupportable current range: %d-%duA\n",
576 /* operating mode constraint check */
577 static int regulator_mode_constrain(struct regulator_dev *rdev,
581 case REGULATOR_MODE_FAST:
582 case REGULATOR_MODE_NORMAL:
583 case REGULATOR_MODE_IDLE:
584 case REGULATOR_MODE_STANDBY:
587 rdev_err(rdev, "invalid mode %x specified\n", *mode);
591 if (!regulator_ops_is_valid(rdev, REGULATOR_CHANGE_MODE)) {
592 rdev_err(rdev, "mode operation not allowed\n");
596 /* The modes are bitmasks, the most power hungry modes having
597 * the lowest values. If the requested mode isn't supported
601 if (rdev->constraints->valid_modes_mask & *mode)
609 static inline struct regulator_state *
610 regulator_get_suspend_state(struct regulator_dev *rdev, suspend_state_t state)
612 if (rdev->constraints == NULL)
616 case PM_SUSPEND_STANDBY:
617 return &rdev->constraints->state_standby;
619 return &rdev->constraints->state_mem;
621 return &rdev->constraints->state_disk;
627 static const struct regulator_state *
628 regulator_get_suspend_state_check(struct regulator_dev *rdev, suspend_state_t state)
630 const struct regulator_state *rstate;
632 rstate = regulator_get_suspend_state(rdev, state);
636 /* If we have no suspend mode configuration don't set anything;
637 * only warn if the driver implements set_suspend_voltage or
638 * set_suspend_mode callback.
640 if (rstate->enabled != ENABLE_IN_SUSPEND &&
641 rstate->enabled != DISABLE_IN_SUSPEND) {
642 if (rdev->desc->ops->set_suspend_voltage ||
643 rdev->desc->ops->set_suspend_mode)
644 rdev_warn(rdev, "No configuration\n");
651 static ssize_t microvolts_show(struct device *dev,
652 struct device_attribute *attr, char *buf)
654 struct regulator_dev *rdev = dev_get_drvdata(dev);
657 regulator_lock(rdev);
658 uV = regulator_get_voltage_rdev(rdev);
659 regulator_unlock(rdev);
663 return sprintf(buf, "%d\n", uV);
665 static DEVICE_ATTR_RO(microvolts);
667 static ssize_t microamps_show(struct device *dev,
668 struct device_attribute *attr, char *buf)
670 struct regulator_dev *rdev = dev_get_drvdata(dev);
672 return sprintf(buf, "%d\n", _regulator_get_current_limit(rdev));
674 static DEVICE_ATTR_RO(microamps);
676 static ssize_t name_show(struct device *dev, struct device_attribute *attr,
679 struct regulator_dev *rdev = dev_get_drvdata(dev);
681 return sprintf(buf, "%s\n", rdev_get_name(rdev));
683 static DEVICE_ATTR_RO(name);
685 static const char *regulator_opmode_to_str(int mode)
688 case REGULATOR_MODE_FAST:
690 case REGULATOR_MODE_NORMAL:
692 case REGULATOR_MODE_IDLE:
694 case REGULATOR_MODE_STANDBY:
700 static ssize_t regulator_print_opmode(char *buf, int mode)
702 return sprintf(buf, "%s\n", regulator_opmode_to_str(mode));
705 static ssize_t opmode_show(struct device *dev,
706 struct device_attribute *attr, char *buf)
708 struct regulator_dev *rdev = dev_get_drvdata(dev);
710 return regulator_print_opmode(buf, _regulator_get_mode(rdev));
712 static DEVICE_ATTR_RO(opmode);
714 static ssize_t regulator_print_state(char *buf, int state)
717 return sprintf(buf, "enabled\n");
719 return sprintf(buf, "disabled\n");
721 return sprintf(buf, "unknown\n");
724 static ssize_t state_show(struct device *dev,
725 struct device_attribute *attr, char *buf)
727 struct regulator_dev *rdev = dev_get_drvdata(dev);
730 regulator_lock(rdev);
731 ret = regulator_print_state(buf, _regulator_is_enabled(rdev));
732 regulator_unlock(rdev);
736 static DEVICE_ATTR_RO(state);
738 static ssize_t status_show(struct device *dev,
739 struct device_attribute *attr, char *buf)
741 struct regulator_dev *rdev = dev_get_drvdata(dev);
745 status = rdev->desc->ops->get_status(rdev);
750 case REGULATOR_STATUS_OFF:
753 case REGULATOR_STATUS_ON:
756 case REGULATOR_STATUS_ERROR:
759 case REGULATOR_STATUS_FAST:
762 case REGULATOR_STATUS_NORMAL:
765 case REGULATOR_STATUS_IDLE:
768 case REGULATOR_STATUS_STANDBY:
771 case REGULATOR_STATUS_BYPASS:
774 case REGULATOR_STATUS_UNDEFINED:
781 return sprintf(buf, "%s\n", label);
783 static DEVICE_ATTR_RO(status);
785 static ssize_t min_microamps_show(struct device *dev,
786 struct device_attribute *attr, char *buf)
788 struct regulator_dev *rdev = dev_get_drvdata(dev);
790 if (!rdev->constraints)
791 return sprintf(buf, "constraint not defined\n");
793 return sprintf(buf, "%d\n", rdev->constraints->min_uA);
795 static DEVICE_ATTR_RO(min_microamps);
797 static ssize_t max_microamps_show(struct device *dev,
798 struct device_attribute *attr, char *buf)
800 struct regulator_dev *rdev = dev_get_drvdata(dev);
802 if (!rdev->constraints)
803 return sprintf(buf, "constraint not defined\n");
805 return sprintf(buf, "%d\n", rdev->constraints->max_uA);
807 static DEVICE_ATTR_RO(max_microamps);
809 static ssize_t min_microvolts_show(struct device *dev,
810 struct device_attribute *attr, char *buf)
812 struct regulator_dev *rdev = dev_get_drvdata(dev);
814 if (!rdev->constraints)
815 return sprintf(buf, "constraint not defined\n");
817 return sprintf(buf, "%d\n", rdev->constraints->min_uV);
819 static DEVICE_ATTR_RO(min_microvolts);
821 static ssize_t max_microvolts_show(struct device *dev,
822 struct device_attribute *attr, char *buf)
824 struct regulator_dev *rdev = dev_get_drvdata(dev);
826 if (!rdev->constraints)
827 return sprintf(buf, "constraint not defined\n");
829 return sprintf(buf, "%d\n", rdev->constraints->max_uV);
831 static DEVICE_ATTR_RO(max_microvolts);
833 static ssize_t requested_microamps_show(struct device *dev,
834 struct device_attribute *attr, char *buf)
836 struct regulator_dev *rdev = dev_get_drvdata(dev);
837 struct regulator *regulator;
840 regulator_lock(rdev);
841 list_for_each_entry(regulator, &rdev->consumer_list, list) {
842 if (regulator->enable_count)
843 uA += regulator->uA_load;
845 regulator_unlock(rdev);
846 return sprintf(buf, "%d\n", uA);
848 static DEVICE_ATTR_RO(requested_microamps);
850 static ssize_t num_users_show(struct device *dev, struct device_attribute *attr,
853 struct regulator_dev *rdev = dev_get_drvdata(dev);
854 return sprintf(buf, "%d\n", rdev->use_count);
856 static DEVICE_ATTR_RO(num_users);
858 static ssize_t type_show(struct device *dev, struct device_attribute *attr,
861 struct regulator_dev *rdev = dev_get_drvdata(dev);
863 switch (rdev->desc->type) {
864 case REGULATOR_VOLTAGE:
865 return sprintf(buf, "voltage\n");
866 case REGULATOR_CURRENT:
867 return sprintf(buf, "current\n");
869 return sprintf(buf, "unknown\n");
871 static DEVICE_ATTR_RO(type);
873 static ssize_t suspend_mem_microvolts_show(struct device *dev,
874 struct device_attribute *attr, char *buf)
876 struct regulator_dev *rdev = dev_get_drvdata(dev);
878 return sprintf(buf, "%d\n", rdev->constraints->state_mem.uV);
880 static DEVICE_ATTR_RO(suspend_mem_microvolts);
882 static ssize_t suspend_disk_microvolts_show(struct device *dev,
883 struct device_attribute *attr, char *buf)
885 struct regulator_dev *rdev = dev_get_drvdata(dev);
887 return sprintf(buf, "%d\n", rdev->constraints->state_disk.uV);
889 static DEVICE_ATTR_RO(suspend_disk_microvolts);
891 static ssize_t suspend_standby_microvolts_show(struct device *dev,
892 struct device_attribute *attr, char *buf)
894 struct regulator_dev *rdev = dev_get_drvdata(dev);
896 return sprintf(buf, "%d\n", rdev->constraints->state_standby.uV);
898 static DEVICE_ATTR_RO(suspend_standby_microvolts);
900 static ssize_t suspend_mem_mode_show(struct device *dev,
901 struct device_attribute *attr, char *buf)
903 struct regulator_dev *rdev = dev_get_drvdata(dev);
905 return regulator_print_opmode(buf,
906 rdev->constraints->state_mem.mode);
908 static DEVICE_ATTR_RO(suspend_mem_mode);
910 static ssize_t suspend_disk_mode_show(struct device *dev,
911 struct device_attribute *attr, char *buf)
913 struct regulator_dev *rdev = dev_get_drvdata(dev);
915 return regulator_print_opmode(buf,
916 rdev->constraints->state_disk.mode);
918 static DEVICE_ATTR_RO(suspend_disk_mode);
920 static ssize_t suspend_standby_mode_show(struct device *dev,
921 struct device_attribute *attr, char *buf)
923 struct regulator_dev *rdev = dev_get_drvdata(dev);
925 return regulator_print_opmode(buf,
926 rdev->constraints->state_standby.mode);
928 static DEVICE_ATTR_RO(suspend_standby_mode);
930 static ssize_t suspend_mem_state_show(struct device *dev,
931 struct device_attribute *attr, char *buf)
933 struct regulator_dev *rdev = dev_get_drvdata(dev);
935 return regulator_print_state(buf,
936 rdev->constraints->state_mem.enabled);
938 static DEVICE_ATTR_RO(suspend_mem_state);
940 static ssize_t suspend_disk_state_show(struct device *dev,
941 struct device_attribute *attr, char *buf)
943 struct regulator_dev *rdev = dev_get_drvdata(dev);
945 return regulator_print_state(buf,
946 rdev->constraints->state_disk.enabled);
948 static DEVICE_ATTR_RO(suspend_disk_state);
950 static ssize_t suspend_standby_state_show(struct device *dev,
951 struct device_attribute *attr, char *buf)
953 struct regulator_dev *rdev = dev_get_drvdata(dev);
955 return regulator_print_state(buf,
956 rdev->constraints->state_standby.enabled);
958 static DEVICE_ATTR_RO(suspend_standby_state);
960 static ssize_t bypass_show(struct device *dev,
961 struct device_attribute *attr, char *buf)
963 struct regulator_dev *rdev = dev_get_drvdata(dev);
968 ret = rdev->desc->ops->get_bypass(rdev, &bypass);
977 return sprintf(buf, "%s\n", report);
979 static DEVICE_ATTR_RO(bypass);
981 #define REGULATOR_ERROR_ATTR(name, bit) \
982 static ssize_t name##_show(struct device *dev, struct device_attribute *attr, \
986 unsigned int flags; \
987 struct regulator_dev *rdev = dev_get_drvdata(dev); \
988 ret = _regulator_get_error_flags(rdev, &flags); \
991 return sysfs_emit(buf, "%d\n", !!(flags & (bit))); \
993 static DEVICE_ATTR_RO(name)
995 REGULATOR_ERROR_ATTR(under_voltage, REGULATOR_ERROR_UNDER_VOLTAGE);
996 REGULATOR_ERROR_ATTR(over_current, REGULATOR_ERROR_OVER_CURRENT);
997 REGULATOR_ERROR_ATTR(regulation_out, REGULATOR_ERROR_REGULATION_OUT);
998 REGULATOR_ERROR_ATTR(fail, REGULATOR_ERROR_FAIL);
999 REGULATOR_ERROR_ATTR(over_temp, REGULATOR_ERROR_OVER_TEMP);
1000 REGULATOR_ERROR_ATTR(under_voltage_warn, REGULATOR_ERROR_UNDER_VOLTAGE_WARN);
1001 REGULATOR_ERROR_ATTR(over_current_warn, REGULATOR_ERROR_OVER_CURRENT_WARN);
1002 REGULATOR_ERROR_ATTR(over_voltage_warn, REGULATOR_ERROR_OVER_VOLTAGE_WARN);
1003 REGULATOR_ERROR_ATTR(over_temp_warn, REGULATOR_ERROR_OVER_TEMP_WARN);
1005 /* Calculate the new optimum regulator operating mode based on the new total
1006 * consumer load. All locks held by caller
1008 static int drms_uA_update(struct regulator_dev *rdev)
1010 struct regulator *sibling;
1011 int current_uA = 0, output_uV, input_uV, err;
1015 * first check to see if we can set modes at all, otherwise just
1016 * tell the consumer everything is OK.
1018 if (!regulator_ops_is_valid(rdev, REGULATOR_CHANGE_DRMS)) {
1019 rdev_dbg(rdev, "DRMS operation not allowed\n");
1023 if (!rdev->desc->ops->get_optimum_mode &&
1024 !rdev->desc->ops->set_load)
1027 if (!rdev->desc->ops->set_mode &&
1028 !rdev->desc->ops->set_load)
1031 /* calc total requested load */
1032 list_for_each_entry(sibling, &rdev->consumer_list, list) {
1033 if (sibling->enable_count)
1034 current_uA += sibling->uA_load;
1037 current_uA += rdev->constraints->system_load;
1039 if (rdev->desc->ops->set_load) {
1040 /* set the optimum mode for our new total regulator load */
1041 err = rdev->desc->ops->set_load(rdev, current_uA);
1043 rdev_err(rdev, "failed to set load %d: %pe\n",
1044 current_uA, ERR_PTR(err));
1047 * Unfortunately in some cases the constraints->valid_ops has
1048 * REGULATOR_CHANGE_DRMS but there are no valid modes listed.
1049 * That's not really legit but we won't consider it a fatal
1050 * error here. We'll treat it as if REGULATOR_CHANGE_DRMS
1053 if (!rdev->constraints->valid_modes_mask) {
1054 rdev_dbg(rdev, "Can change modes; but no valid mode\n");
1058 /* get output voltage */
1059 output_uV = regulator_get_voltage_rdev(rdev);
1062 * Don't return an error; if regulator driver cares about
1063 * output_uV then it's up to the driver to validate.
1066 rdev_dbg(rdev, "invalid output voltage found\n");
1068 /* get input voltage */
1071 input_uV = regulator_get_voltage_rdev(rdev->supply->rdev);
1073 input_uV = rdev->constraints->input_uV;
1076 * Don't return an error; if regulator driver cares about
1077 * input_uV then it's up to the driver to validate.
1080 rdev_dbg(rdev, "invalid input voltage found\n");
1082 /* now get the optimum mode for our new total regulator load */
1083 mode = rdev->desc->ops->get_optimum_mode(rdev, input_uV,
1084 output_uV, current_uA);
1086 /* check the new mode is allowed */
1087 err = regulator_mode_constrain(rdev, &mode);
1089 rdev_err(rdev, "failed to get optimum mode @ %d uA %d -> %d uV: %pe\n",
1090 current_uA, input_uV, output_uV, ERR_PTR(err));
1094 err = rdev->desc->ops->set_mode(rdev, mode);
1096 rdev_err(rdev, "failed to set optimum mode %x: %pe\n",
1097 mode, ERR_PTR(err));
1103 static int __suspend_set_state(struct regulator_dev *rdev,
1104 const struct regulator_state *rstate)
1108 if (rstate->enabled == ENABLE_IN_SUSPEND &&
1109 rdev->desc->ops->set_suspend_enable)
1110 ret = rdev->desc->ops->set_suspend_enable(rdev);
1111 else if (rstate->enabled == DISABLE_IN_SUSPEND &&
1112 rdev->desc->ops->set_suspend_disable)
1113 ret = rdev->desc->ops->set_suspend_disable(rdev);
1114 else /* OK if set_suspend_enable or set_suspend_disable is NULL */
1118 rdev_err(rdev, "failed to enabled/disable: %pe\n", ERR_PTR(ret));
1122 if (rdev->desc->ops->set_suspend_voltage && rstate->uV > 0) {
1123 ret = rdev->desc->ops->set_suspend_voltage(rdev, rstate->uV);
1125 rdev_err(rdev, "failed to set voltage: %pe\n", ERR_PTR(ret));
1130 if (rdev->desc->ops->set_suspend_mode && rstate->mode > 0) {
1131 ret = rdev->desc->ops->set_suspend_mode(rdev, rstate->mode);
1133 rdev_err(rdev, "failed to set mode: %pe\n", ERR_PTR(ret));
1141 static int suspend_set_initial_state(struct regulator_dev *rdev)
1143 const struct regulator_state *rstate;
1145 rstate = regulator_get_suspend_state_check(rdev,
1146 rdev->constraints->initial_state);
1150 return __suspend_set_state(rdev, rstate);
1153 #if defined(DEBUG) || defined(CONFIG_DYNAMIC_DEBUG)
1154 static void print_constraints_debug(struct regulator_dev *rdev)
1156 struct regulation_constraints *constraints = rdev->constraints;
1158 size_t len = sizeof(buf) - 1;
1162 if (constraints->min_uV && constraints->max_uV) {
1163 if (constraints->min_uV == constraints->max_uV)
1164 count += scnprintf(buf + count, len - count, "%d mV ",
1165 constraints->min_uV / 1000);
1167 count += scnprintf(buf + count, len - count,
1169 constraints->min_uV / 1000,
1170 constraints->max_uV / 1000);
1173 if (!constraints->min_uV ||
1174 constraints->min_uV != constraints->max_uV) {
1175 ret = regulator_get_voltage_rdev(rdev);
1177 count += scnprintf(buf + count, len - count,
1178 "at %d mV ", ret / 1000);
1181 if (constraints->uV_offset)
1182 count += scnprintf(buf + count, len - count, "%dmV offset ",
1183 constraints->uV_offset / 1000);
1185 if (constraints->min_uA && constraints->max_uA) {
1186 if (constraints->min_uA == constraints->max_uA)
1187 count += scnprintf(buf + count, len - count, "%d mA ",
1188 constraints->min_uA / 1000);
1190 count += scnprintf(buf + count, len - count,
1192 constraints->min_uA / 1000,
1193 constraints->max_uA / 1000);
1196 if (!constraints->min_uA ||
1197 constraints->min_uA != constraints->max_uA) {
1198 ret = _regulator_get_current_limit(rdev);
1200 count += scnprintf(buf + count, len - count,
1201 "at %d mA ", ret / 1000);
1204 if (constraints->valid_modes_mask & REGULATOR_MODE_FAST)
1205 count += scnprintf(buf + count, len - count, "fast ");
1206 if (constraints->valid_modes_mask & REGULATOR_MODE_NORMAL)
1207 count += scnprintf(buf + count, len - count, "normal ");
1208 if (constraints->valid_modes_mask & REGULATOR_MODE_IDLE)
1209 count += scnprintf(buf + count, len - count, "idle ");
1210 if (constraints->valid_modes_mask & REGULATOR_MODE_STANDBY)
1211 count += scnprintf(buf + count, len - count, "standby ");
1214 count = scnprintf(buf, len, "no parameters");
1218 count += scnprintf(buf + count, len - count, ", %s",
1219 _regulator_is_enabled(rdev) ? "enabled" : "disabled");
1221 rdev_dbg(rdev, "%s\n", buf);
1223 #else /* !DEBUG && !CONFIG_DYNAMIC_DEBUG */
1224 static inline void print_constraints_debug(struct regulator_dev *rdev) {}
1225 #endif /* !DEBUG && !CONFIG_DYNAMIC_DEBUG */
1227 static void print_constraints(struct regulator_dev *rdev)
1229 struct regulation_constraints *constraints = rdev->constraints;
1231 print_constraints_debug(rdev);
1233 if ((constraints->min_uV != constraints->max_uV) &&
1234 !regulator_ops_is_valid(rdev, REGULATOR_CHANGE_VOLTAGE))
1236 "Voltage range but no REGULATOR_CHANGE_VOLTAGE\n");
1239 static int machine_constraints_voltage(struct regulator_dev *rdev,
1240 struct regulation_constraints *constraints)
1242 const struct regulator_ops *ops = rdev->desc->ops;
1245 /* do we need to apply the constraint voltage */
1246 if (rdev->constraints->apply_uV &&
1247 rdev->constraints->min_uV && rdev->constraints->max_uV) {
1248 int target_min, target_max;
1249 int current_uV = regulator_get_voltage_rdev(rdev);
1251 if (current_uV == -ENOTRECOVERABLE) {
1252 /* This regulator can't be read and must be initialized */
1253 rdev_info(rdev, "Setting %d-%duV\n",
1254 rdev->constraints->min_uV,
1255 rdev->constraints->max_uV);
1256 _regulator_do_set_voltage(rdev,
1257 rdev->constraints->min_uV,
1258 rdev->constraints->max_uV);
1259 current_uV = regulator_get_voltage_rdev(rdev);
1262 if (current_uV < 0) {
1263 if (current_uV != -EPROBE_DEFER)
1265 "failed to get the current voltage: %pe\n",
1266 ERR_PTR(current_uV));
1271 * If we're below the minimum voltage move up to the
1272 * minimum voltage, if we're above the maximum voltage
1273 * then move down to the maximum.
1275 target_min = current_uV;
1276 target_max = current_uV;
1278 if (current_uV < rdev->constraints->min_uV) {
1279 target_min = rdev->constraints->min_uV;
1280 target_max = rdev->constraints->min_uV;
1283 if (current_uV > rdev->constraints->max_uV) {
1284 target_min = rdev->constraints->max_uV;
1285 target_max = rdev->constraints->max_uV;
1288 if (target_min != current_uV || target_max != current_uV) {
1289 rdev_info(rdev, "Bringing %duV into %d-%duV\n",
1290 current_uV, target_min, target_max);
1291 ret = _regulator_do_set_voltage(
1292 rdev, target_min, target_max);
1295 "failed to apply %d-%duV constraint: %pe\n",
1296 target_min, target_max, ERR_PTR(ret));
1302 /* constrain machine-level voltage specs to fit
1303 * the actual range supported by this regulator.
1305 if (ops->list_voltage && rdev->desc->n_voltages) {
1306 int count = rdev->desc->n_voltages;
1308 int min_uV = INT_MAX;
1309 int max_uV = INT_MIN;
1310 int cmin = constraints->min_uV;
1311 int cmax = constraints->max_uV;
1313 /* it's safe to autoconfigure fixed-voltage supplies
1314 * and the constraints are used by list_voltage.
1316 if (count == 1 && !cmin) {
1319 constraints->min_uV = cmin;
1320 constraints->max_uV = cmax;
1323 /* voltage constraints are optional */
1324 if ((cmin == 0) && (cmax == 0))
1327 /* else require explicit machine-level constraints */
1328 if (cmin <= 0 || cmax <= 0 || cmax < cmin) {
1329 rdev_err(rdev, "invalid voltage constraints\n");
1333 /* no need to loop voltages if range is continuous */
1334 if (rdev->desc->continuous_voltage_range)
1337 /* initial: [cmin..cmax] valid, [min_uV..max_uV] not */
1338 for (i = 0; i < count; i++) {
1341 value = ops->list_voltage(rdev, i);
1345 /* maybe adjust [min_uV..max_uV] */
1346 if (value >= cmin && value < min_uV)
1348 if (value <= cmax && value > max_uV)
1352 /* final: [min_uV..max_uV] valid iff constraints valid */
1353 if (max_uV < min_uV) {
1355 "unsupportable voltage constraints %u-%uuV\n",
1360 /* use regulator's subset of machine constraints */
1361 if (constraints->min_uV < min_uV) {
1362 rdev_dbg(rdev, "override min_uV, %d -> %d\n",
1363 constraints->min_uV, min_uV);
1364 constraints->min_uV = min_uV;
1366 if (constraints->max_uV > max_uV) {
1367 rdev_dbg(rdev, "override max_uV, %d -> %d\n",
1368 constraints->max_uV, max_uV);
1369 constraints->max_uV = max_uV;
1376 static int machine_constraints_current(struct regulator_dev *rdev,
1377 struct regulation_constraints *constraints)
1379 const struct regulator_ops *ops = rdev->desc->ops;
1382 if (!constraints->min_uA && !constraints->max_uA)
1385 if (constraints->min_uA > constraints->max_uA) {
1386 rdev_err(rdev, "Invalid current constraints\n");
1390 if (!ops->set_current_limit || !ops->get_current_limit) {
1391 rdev_warn(rdev, "Operation of current configuration missing\n");
1395 /* Set regulator current in constraints range */
1396 ret = ops->set_current_limit(rdev, constraints->min_uA,
1397 constraints->max_uA);
1399 rdev_err(rdev, "Failed to set current constraint, %d\n", ret);
1406 static int _regulator_do_enable(struct regulator_dev *rdev);
1408 static int notif_set_limit(struct regulator_dev *rdev,
1409 int (*set)(struct regulator_dev *, int, int, bool),
1410 int limit, int severity)
1414 if (limit == REGULATOR_NOTIF_LIMIT_DISABLE) {
1421 if (limit == REGULATOR_NOTIF_LIMIT_ENABLE)
1424 return set(rdev, limit, severity, enable);
1427 static int handle_notify_limits(struct regulator_dev *rdev,
1428 int (*set)(struct regulator_dev *, int, int, bool),
1429 struct notification_limit *limits)
1437 ret = notif_set_limit(rdev, set, limits->prot,
1438 REGULATOR_SEVERITY_PROT);
1443 ret = notif_set_limit(rdev, set, limits->err,
1444 REGULATOR_SEVERITY_ERR);
1449 ret = notif_set_limit(rdev, set, limits->warn,
1450 REGULATOR_SEVERITY_WARN);
1455 * set_machine_constraints - sets regulator constraints
1456 * @rdev: regulator source
1458 * Allows platform initialisation code to define and constrain
1459 * regulator circuits e.g. valid voltage/current ranges, etc. NOTE:
1460 * Constraints *must* be set by platform code in order for some
1461 * regulator operations to proceed i.e. set_voltage, set_current_limit,
1464 static int set_machine_constraints(struct regulator_dev *rdev)
1467 const struct regulator_ops *ops = rdev->desc->ops;
1469 ret = machine_constraints_voltage(rdev, rdev->constraints);
1473 ret = machine_constraints_current(rdev, rdev->constraints);
1477 if (rdev->constraints->ilim_uA && ops->set_input_current_limit) {
1478 ret = ops->set_input_current_limit(rdev,
1479 rdev->constraints->ilim_uA);
1481 rdev_err(rdev, "failed to set input limit: %pe\n", ERR_PTR(ret));
1486 /* do we need to setup our suspend state */
1487 if (rdev->constraints->initial_state) {
1488 ret = suspend_set_initial_state(rdev);
1490 rdev_err(rdev, "failed to set suspend state: %pe\n", ERR_PTR(ret));
1495 if (rdev->constraints->initial_mode) {
1496 if (!ops->set_mode) {
1497 rdev_err(rdev, "no set_mode operation\n");
1501 ret = ops->set_mode(rdev, rdev->constraints->initial_mode);
1503 rdev_err(rdev, "failed to set initial mode: %pe\n", ERR_PTR(ret));
1506 } else if (rdev->constraints->system_load) {
1508 * We'll only apply the initial system load if an
1509 * initial mode wasn't specified.
1511 drms_uA_update(rdev);
1514 if ((rdev->constraints->ramp_delay || rdev->constraints->ramp_disable)
1515 && ops->set_ramp_delay) {
1516 ret = ops->set_ramp_delay(rdev, rdev->constraints->ramp_delay);
1518 rdev_err(rdev, "failed to set ramp_delay: %pe\n", ERR_PTR(ret));
1523 if (rdev->constraints->pull_down && ops->set_pull_down) {
1524 ret = ops->set_pull_down(rdev);
1526 rdev_err(rdev, "failed to set pull down: %pe\n", ERR_PTR(ret));
1531 if (rdev->constraints->soft_start && ops->set_soft_start) {
1532 ret = ops->set_soft_start(rdev);
1534 rdev_err(rdev, "failed to set soft start: %pe\n", ERR_PTR(ret));
1540 * Existing logic does not warn if over_current_protection is given as
1541 * a constraint but driver does not support that. I think we should
1542 * warn about this type of issues as it is possible someone changes
1543 * PMIC on board to another type - and the another PMIC's driver does
1544 * not support setting protection. Board composer may happily believe
1545 * the DT limits are respected - especially if the new PMIC HW also
1546 * supports protection but the driver does not. I won't change the logic
1547 * without hearing more experienced opinion on this though.
1549 * If warning is seen as a good idea then we can merge handling the
1550 * over-curret protection and detection and get rid of this special
1553 if (rdev->constraints->over_current_protection
1554 && ops->set_over_current_protection) {
1555 int lim = rdev->constraints->over_curr_limits.prot;
1557 ret = ops->set_over_current_protection(rdev, lim,
1558 REGULATOR_SEVERITY_PROT,
1561 rdev_err(rdev, "failed to set over current protection: %pe\n",
1567 if (rdev->constraints->over_current_detection)
1568 ret = handle_notify_limits(rdev,
1569 ops->set_over_current_protection,
1570 &rdev->constraints->over_curr_limits);
1572 if (ret != -EOPNOTSUPP) {
1573 rdev_err(rdev, "failed to set over current limits: %pe\n",
1578 "IC does not support requested over-current limits\n");
1581 if (rdev->constraints->over_voltage_detection)
1582 ret = handle_notify_limits(rdev,
1583 ops->set_over_voltage_protection,
1584 &rdev->constraints->over_voltage_limits);
1586 if (ret != -EOPNOTSUPP) {
1587 rdev_err(rdev, "failed to set over voltage limits %pe\n",
1592 "IC does not support requested over voltage limits\n");
1595 if (rdev->constraints->under_voltage_detection)
1596 ret = handle_notify_limits(rdev,
1597 ops->set_under_voltage_protection,
1598 &rdev->constraints->under_voltage_limits);
1600 if (ret != -EOPNOTSUPP) {
1601 rdev_err(rdev, "failed to set under voltage limits %pe\n",
1606 "IC does not support requested under voltage limits\n");
1609 if (rdev->constraints->over_temp_detection)
1610 ret = handle_notify_limits(rdev,
1611 ops->set_thermal_protection,
1612 &rdev->constraints->temp_limits);
1614 if (ret != -EOPNOTSUPP) {
1615 rdev_err(rdev, "failed to set temperature limits %pe\n",
1620 "IC does not support requested temperature limits\n");
1623 if (rdev->constraints->active_discharge && ops->set_active_discharge) {
1624 bool ad_state = (rdev->constraints->active_discharge ==
1625 REGULATOR_ACTIVE_DISCHARGE_ENABLE) ? true : false;
1627 ret = ops->set_active_discharge(rdev, ad_state);
1629 rdev_err(rdev, "failed to set active discharge: %pe\n", ERR_PTR(ret));
1635 * If there is no mechanism for controlling the regulator then
1636 * flag it as always_on so we don't end up duplicating checks
1637 * for this so much. Note that we could control the state of
1638 * a supply to control the output on a regulator that has no
1641 if (!rdev->ena_pin && !ops->enable) {
1642 if (rdev->supply_name && !rdev->supply)
1643 return -EPROBE_DEFER;
1646 rdev->constraints->always_on =
1647 rdev->supply->rdev->constraints->always_on;
1649 rdev->constraints->always_on = true;
1652 /* If the constraints say the regulator should be on at this point
1653 * and we have control then make sure it is enabled.
1655 if (rdev->constraints->always_on || rdev->constraints->boot_on) {
1656 /* If we want to enable this regulator, make sure that we know
1657 * the supplying regulator.
1659 if (rdev->supply_name && !rdev->supply)
1660 return -EPROBE_DEFER;
1662 /* If supplying regulator has already been enabled,
1663 * it's not intended to have use_count increment
1664 * when rdev is only boot-on.
1667 (rdev->constraints->always_on ||
1668 !regulator_is_enabled(rdev->supply))) {
1669 ret = regulator_enable(rdev->supply);
1671 _regulator_put(rdev->supply);
1672 rdev->supply = NULL;
1677 ret = _regulator_do_enable(rdev);
1678 if (ret < 0 && ret != -EINVAL) {
1679 rdev_err(rdev, "failed to enable: %pe\n", ERR_PTR(ret));
1683 if (rdev->constraints->always_on)
1685 } else if (rdev->desc->off_on_delay) {
1686 rdev->last_off = ktime_get();
1689 print_constraints(rdev);
1694 * set_supply - set regulator supply regulator
1695 * @rdev: regulator (locked)
1696 * @supply_rdev: supply regulator (locked))
1698 * Called by platform initialisation code to set the supply regulator for this
1699 * regulator. This ensures that a regulators supply will also be enabled by the
1700 * core if it's child is enabled.
1702 static int set_supply(struct regulator_dev *rdev,
1703 struct regulator_dev *supply_rdev)
1707 rdev_dbg(rdev, "supplied by %s\n", rdev_get_name(supply_rdev));
1709 if (!try_module_get(supply_rdev->owner))
1712 rdev->supply = create_regulator(supply_rdev, &rdev->dev, "SUPPLY");
1713 if (rdev->supply == NULL) {
1714 module_put(supply_rdev->owner);
1718 supply_rdev->open_count++;
1724 * set_consumer_device_supply - Bind a regulator to a symbolic supply
1725 * @rdev: regulator source
1726 * @consumer_dev_name: dev_name() string for device supply applies to
1727 * @supply: symbolic name for supply
1729 * Allows platform initialisation code to map physical regulator
1730 * sources to symbolic names for supplies for use by devices. Devices
1731 * should use these symbolic names to request regulators, avoiding the
1732 * need to provide board-specific regulator names as platform data.
1734 static int set_consumer_device_supply(struct regulator_dev *rdev,
1735 const char *consumer_dev_name,
1738 struct regulator_map *node, *new_node;
1744 if (consumer_dev_name != NULL)
1749 new_node = kzalloc(sizeof(struct regulator_map), GFP_KERNEL);
1750 if (new_node == NULL)
1753 new_node->regulator = rdev;
1754 new_node->supply = supply;
1757 new_node->dev_name = kstrdup(consumer_dev_name, GFP_KERNEL);
1758 if (new_node->dev_name == NULL) {
1764 mutex_lock(®ulator_list_mutex);
1765 list_for_each_entry(node, ®ulator_map_list, list) {
1766 if (node->dev_name && consumer_dev_name) {
1767 if (strcmp(node->dev_name, consumer_dev_name) != 0)
1769 } else if (node->dev_name || consumer_dev_name) {
1773 if (strcmp(node->supply, supply) != 0)
1776 pr_debug("%s: %s/%s is '%s' supply; fail %s/%s\n",
1778 dev_name(&node->regulator->dev),
1779 node->regulator->desc->name,
1781 dev_name(&rdev->dev), rdev_get_name(rdev));
1785 list_add(&new_node->list, ®ulator_map_list);
1786 mutex_unlock(®ulator_list_mutex);
1791 mutex_unlock(®ulator_list_mutex);
1792 kfree(new_node->dev_name);
1797 static void unset_regulator_supplies(struct regulator_dev *rdev)
1799 struct regulator_map *node, *n;
1801 list_for_each_entry_safe(node, n, ®ulator_map_list, list) {
1802 if (rdev == node->regulator) {
1803 list_del(&node->list);
1804 kfree(node->dev_name);
1810 #ifdef CONFIG_DEBUG_FS
1811 static ssize_t constraint_flags_read_file(struct file *file,
1812 char __user *user_buf,
1813 size_t count, loff_t *ppos)
1815 const struct regulator *regulator = file->private_data;
1816 const struct regulation_constraints *c = regulator->rdev->constraints;
1823 buf = kmalloc(PAGE_SIZE, GFP_KERNEL);
1827 ret = snprintf(buf, PAGE_SIZE,
1831 "ramp_disable: %u\n"
1834 "over_current_protection: %u\n",
1841 c->over_current_protection);
1843 ret = simple_read_from_buffer(user_buf, count, ppos, buf, ret);
1851 static const struct file_operations constraint_flags_fops = {
1852 #ifdef CONFIG_DEBUG_FS
1853 .open = simple_open,
1854 .read = constraint_flags_read_file,
1855 .llseek = default_llseek,
1859 #define REG_STR_SIZE 64
1861 static struct regulator *create_regulator(struct regulator_dev *rdev,
1863 const char *supply_name)
1865 struct regulator *regulator;
1868 lockdep_assert_held_once(&rdev->mutex.base);
1871 char buf[REG_STR_SIZE];
1874 size = snprintf(buf, REG_STR_SIZE, "%s-%s",
1875 dev->kobj.name, supply_name);
1876 if (size >= REG_STR_SIZE)
1879 supply_name = kstrdup(buf, GFP_KERNEL);
1880 if (supply_name == NULL)
1883 supply_name = kstrdup_const(supply_name, GFP_KERNEL);
1884 if (supply_name == NULL)
1888 regulator = kzalloc(sizeof(*regulator), GFP_KERNEL);
1889 if (regulator == NULL) {
1890 kfree_const(supply_name);
1894 regulator->rdev = rdev;
1895 regulator->supply_name = supply_name;
1897 list_add(®ulator->list, &rdev->consumer_list);
1900 regulator->dev = dev;
1902 /* Add a link to the device sysfs entry */
1903 err = sysfs_create_link_nowarn(&rdev->dev.kobj, &dev->kobj,
1906 rdev_dbg(rdev, "could not add device link %s: %pe\n",
1907 dev->kobj.name, ERR_PTR(err));
1913 regulator->debugfs = debugfs_create_dir(supply_name, rdev->debugfs);
1914 if (IS_ERR(regulator->debugfs))
1915 rdev_dbg(rdev, "Failed to create debugfs directory\n");
1917 debugfs_create_u32("uA_load", 0444, regulator->debugfs,
1918 ®ulator->uA_load);
1919 debugfs_create_u32("min_uV", 0444, regulator->debugfs,
1920 ®ulator->voltage[PM_SUSPEND_ON].min_uV);
1921 debugfs_create_u32("max_uV", 0444, regulator->debugfs,
1922 ®ulator->voltage[PM_SUSPEND_ON].max_uV);
1923 debugfs_create_file("constraint_flags", 0444, regulator->debugfs,
1924 regulator, &constraint_flags_fops);
1927 * Check now if the regulator is an always on regulator - if
1928 * it is then we don't need to do nearly so much work for
1929 * enable/disable calls.
1931 if (!regulator_ops_is_valid(rdev, REGULATOR_CHANGE_STATUS) &&
1932 _regulator_is_enabled(rdev))
1933 regulator->always_on = true;
1938 static int _regulator_get_enable_time(struct regulator_dev *rdev)
1940 if (rdev->constraints && rdev->constraints->enable_time)
1941 return rdev->constraints->enable_time;
1942 if (rdev->desc->ops->enable_time)
1943 return rdev->desc->ops->enable_time(rdev);
1944 return rdev->desc->enable_time;
1947 static struct regulator_supply_alias *regulator_find_supply_alias(
1948 struct device *dev, const char *supply)
1950 struct regulator_supply_alias *map;
1952 list_for_each_entry(map, ®ulator_supply_alias_list, list)
1953 if (map->src_dev == dev && strcmp(map->src_supply, supply) == 0)
1959 static void regulator_supply_alias(struct device **dev, const char **supply)
1961 struct regulator_supply_alias *map;
1963 map = regulator_find_supply_alias(*dev, *supply);
1965 dev_dbg(*dev, "Mapping supply %s to %s,%s\n",
1966 *supply, map->alias_supply,
1967 dev_name(map->alias_dev));
1968 *dev = map->alias_dev;
1969 *supply = map->alias_supply;
1973 static int regulator_match(struct device *dev, const void *data)
1975 struct regulator_dev *r = dev_to_rdev(dev);
1977 return strcmp(rdev_get_name(r), data) == 0;
1980 static struct regulator_dev *regulator_lookup_by_name(const char *name)
1984 dev = class_find_device(®ulator_class, NULL, name, regulator_match);
1986 return dev ? dev_to_rdev(dev) : NULL;
1990 * regulator_dev_lookup - lookup a regulator device.
1991 * @dev: device for regulator "consumer".
1992 * @supply: Supply name or regulator ID.
1994 * If successful, returns a struct regulator_dev that corresponds to the name
1995 * @supply and with the embedded struct device refcount incremented by one.
1996 * The refcount must be dropped by calling put_device().
1997 * On failure one of the following ERR-PTR-encoded values is returned:
1998 * -ENODEV if lookup fails permanently, -EPROBE_DEFER if lookup could succeed
2001 static struct regulator_dev *regulator_dev_lookup(struct device *dev,
2004 struct regulator_dev *r = NULL;
2005 struct device_node *node;
2006 struct regulator_map *map;
2007 const char *devname = NULL;
2009 regulator_supply_alias(&dev, &supply);
2011 /* first do a dt based lookup */
2012 if (dev && dev->of_node) {
2013 node = of_get_regulator(dev, supply);
2015 r = of_find_regulator_by_node(node);
2021 * We have a node, but there is no device.
2022 * assume it has not registered yet.
2024 return ERR_PTR(-EPROBE_DEFER);
2028 /* if not found, try doing it non-dt way */
2030 devname = dev_name(dev);
2032 mutex_lock(®ulator_list_mutex);
2033 list_for_each_entry(map, ®ulator_map_list, list) {
2034 /* If the mapping has a device set up it must match */
2035 if (map->dev_name &&
2036 (!devname || strcmp(map->dev_name, devname)))
2039 if (strcmp(map->supply, supply) == 0 &&
2040 get_device(&map->regulator->dev)) {
2045 mutex_unlock(®ulator_list_mutex);
2050 r = regulator_lookup_by_name(supply);
2054 return ERR_PTR(-ENODEV);
2057 static int regulator_resolve_supply(struct regulator_dev *rdev)
2059 struct regulator_dev *r;
2060 struct device *dev = rdev->dev.parent;
2061 struct ww_acquire_ctx ww_ctx;
2064 /* No supply to resolve? */
2065 if (!rdev->supply_name)
2068 /* Supply already resolved? (fast-path without locking contention) */
2072 r = regulator_dev_lookup(dev, rdev->supply_name);
2076 /* Did the lookup explicitly defer for us? */
2077 if (ret == -EPROBE_DEFER)
2080 if (have_full_constraints()) {
2081 r = dummy_regulator_rdev;
2082 get_device(&r->dev);
2084 dev_err(dev, "Failed to resolve %s-supply for %s\n",
2085 rdev->supply_name, rdev->desc->name);
2086 ret = -EPROBE_DEFER;
2092 dev_err(dev, "Supply for %s (%s) resolved to itself\n",
2093 rdev->desc->name, rdev->supply_name);
2094 if (!have_full_constraints()) {
2098 r = dummy_regulator_rdev;
2099 get_device(&r->dev);
2103 * If the supply's parent device is not the same as the
2104 * regulator's parent device, then ensure the parent device
2105 * is bound before we resolve the supply, in case the parent
2106 * device get probe deferred and unregisters the supply.
2108 if (r->dev.parent && r->dev.parent != rdev->dev.parent) {
2109 if (!device_is_bound(r->dev.parent)) {
2110 put_device(&r->dev);
2111 ret = -EPROBE_DEFER;
2116 /* Recursively resolve the supply of the supply */
2117 ret = regulator_resolve_supply(r);
2119 put_device(&r->dev);
2124 * Recheck rdev->supply with rdev->mutex lock held to avoid a race
2125 * between rdev->supply null check and setting rdev->supply in
2126 * set_supply() from concurrent tasks.
2128 regulator_lock_two(rdev, r, &ww_ctx);
2130 /* Supply just resolved by a concurrent task? */
2132 regulator_unlock_two(rdev, r, &ww_ctx);
2133 put_device(&r->dev);
2137 ret = set_supply(rdev, r);
2139 regulator_unlock_two(rdev, r, &ww_ctx);
2140 put_device(&r->dev);
2144 regulator_unlock_two(rdev, r, &ww_ctx);
2147 * In set_machine_constraints() we may have turned this regulator on
2148 * but we couldn't propagate to the supply if it hadn't been resolved
2151 if (rdev->use_count) {
2152 ret = regulator_enable(rdev->supply);
2154 _regulator_put(rdev->supply);
2155 rdev->supply = NULL;
2164 /* Internal regulator request function */
2165 struct regulator *_regulator_get(struct device *dev, const char *id,
2166 enum regulator_get_type get_type)
2168 struct regulator_dev *rdev;
2169 struct regulator *regulator;
2170 struct device_link *link;
2173 if (get_type >= MAX_GET_TYPE) {
2174 dev_err(dev, "invalid type %d in %s\n", get_type, __func__);
2175 return ERR_PTR(-EINVAL);
2179 pr_err("get() with no identifier\n");
2180 return ERR_PTR(-EINVAL);
2183 rdev = regulator_dev_lookup(dev, id);
2185 ret = PTR_ERR(rdev);
2188 * If regulator_dev_lookup() fails with error other
2189 * than -ENODEV our job here is done, we simply return it.
2192 return ERR_PTR(ret);
2194 if (!have_full_constraints()) {
2196 "incomplete constraints, dummy supplies not allowed\n");
2197 return ERR_PTR(-ENODEV);
2203 * Assume that a regulator is physically present and
2204 * enabled, even if it isn't hooked up, and just
2207 dev_warn(dev, "supply %s not found, using dummy regulator\n", id);
2208 rdev = dummy_regulator_rdev;
2209 get_device(&rdev->dev);
2214 "dummy supplies not allowed for exclusive requests\n");
2218 return ERR_PTR(-ENODEV);
2222 if (rdev->exclusive) {
2223 regulator = ERR_PTR(-EPERM);
2224 put_device(&rdev->dev);
2228 if (get_type == EXCLUSIVE_GET && rdev->open_count) {
2229 regulator = ERR_PTR(-EBUSY);
2230 put_device(&rdev->dev);
2234 mutex_lock(®ulator_list_mutex);
2235 ret = (rdev->coupling_desc.n_resolved != rdev->coupling_desc.n_coupled);
2236 mutex_unlock(®ulator_list_mutex);
2239 regulator = ERR_PTR(-EPROBE_DEFER);
2240 put_device(&rdev->dev);
2244 ret = regulator_resolve_supply(rdev);
2246 regulator = ERR_PTR(ret);
2247 put_device(&rdev->dev);
2251 if (!try_module_get(rdev->owner)) {
2252 regulator = ERR_PTR(-EPROBE_DEFER);
2253 put_device(&rdev->dev);
2257 regulator_lock(rdev);
2258 regulator = create_regulator(rdev, dev, id);
2259 regulator_unlock(rdev);
2260 if (regulator == NULL) {
2261 regulator = ERR_PTR(-ENOMEM);
2262 module_put(rdev->owner);
2263 put_device(&rdev->dev);
2268 if (get_type == EXCLUSIVE_GET) {
2269 rdev->exclusive = 1;
2271 ret = _regulator_is_enabled(rdev);
2273 rdev->use_count = 1;
2274 regulator->enable_count = 1;
2276 rdev->use_count = 0;
2277 regulator->enable_count = 0;
2281 link = device_link_add(dev, &rdev->dev, DL_FLAG_STATELESS);
2282 if (!IS_ERR_OR_NULL(link))
2283 regulator->device_link = true;
2289 * regulator_get - lookup and obtain a reference to a regulator.
2290 * @dev: device for regulator "consumer"
2291 * @id: Supply name or regulator ID.
2293 * Returns a struct regulator corresponding to the regulator producer,
2294 * or IS_ERR() condition containing errno.
2296 * Use of supply names configured via set_consumer_device_supply() is
2297 * strongly encouraged. It is recommended that the supply name used
2298 * should match the name used for the supply and/or the relevant
2299 * device pins in the datasheet.
2301 struct regulator *regulator_get(struct device *dev, const char *id)
2303 return _regulator_get(dev, id, NORMAL_GET);
2305 EXPORT_SYMBOL_GPL(regulator_get);
2308 * regulator_get_exclusive - obtain exclusive access to a regulator.
2309 * @dev: device for regulator "consumer"
2310 * @id: Supply name or regulator ID.
2312 * Returns a struct regulator corresponding to the regulator producer,
2313 * or IS_ERR() condition containing errno. Other consumers will be
2314 * unable to obtain this regulator while this reference is held and the
2315 * use count for the regulator will be initialised to reflect the current
2316 * state of the regulator.
2318 * This is intended for use by consumers which cannot tolerate shared
2319 * use of the regulator such as those which need to force the
2320 * regulator off for correct operation of the hardware they are
2323 * Use of supply names configured via set_consumer_device_supply() is
2324 * strongly encouraged. It is recommended that the supply name used
2325 * should match the name used for the supply and/or the relevant
2326 * device pins in the datasheet.
2328 struct regulator *regulator_get_exclusive(struct device *dev, const char *id)
2330 return _regulator_get(dev, id, EXCLUSIVE_GET);
2332 EXPORT_SYMBOL_GPL(regulator_get_exclusive);
2335 * regulator_get_optional - obtain optional access to a regulator.
2336 * @dev: device for regulator "consumer"
2337 * @id: Supply name or regulator ID.
2339 * Returns a struct regulator corresponding to the regulator producer,
2340 * or IS_ERR() condition containing errno.
2342 * This is intended for use by consumers for devices which can have
2343 * some supplies unconnected in normal use, such as some MMC devices.
2344 * It can allow the regulator core to provide stub supplies for other
2345 * supplies requested using normal regulator_get() calls without
2346 * disrupting the operation of drivers that can handle absent
2349 * Use of supply names configured via set_consumer_device_supply() is
2350 * strongly encouraged. It is recommended that the supply name used
2351 * should match the name used for the supply and/or the relevant
2352 * device pins in the datasheet.
2354 struct regulator *regulator_get_optional(struct device *dev, const char *id)
2356 return _regulator_get(dev, id, OPTIONAL_GET);
2358 EXPORT_SYMBOL_GPL(regulator_get_optional);
2360 static void destroy_regulator(struct regulator *regulator)
2362 struct regulator_dev *rdev = regulator->rdev;
2364 debugfs_remove_recursive(regulator->debugfs);
2366 if (regulator->dev) {
2367 if (regulator->device_link)
2368 device_link_remove(regulator->dev, &rdev->dev);
2370 /* remove any sysfs entries */
2371 sysfs_remove_link(&rdev->dev.kobj, regulator->supply_name);
2374 regulator_lock(rdev);
2375 list_del(®ulator->list);
2378 rdev->exclusive = 0;
2379 regulator_unlock(rdev);
2381 kfree_const(regulator->supply_name);
2385 /* regulator_list_mutex lock held by regulator_put() */
2386 static void _regulator_put(struct regulator *regulator)
2388 struct regulator_dev *rdev;
2390 if (IS_ERR_OR_NULL(regulator))
2393 lockdep_assert_held_once(®ulator_list_mutex);
2395 /* Docs say you must disable before calling regulator_put() */
2396 WARN_ON(regulator->enable_count);
2398 rdev = regulator->rdev;
2400 destroy_regulator(regulator);
2402 module_put(rdev->owner);
2403 put_device(&rdev->dev);
2407 * regulator_put - "free" the regulator source
2408 * @regulator: regulator source
2410 * Note: drivers must ensure that all regulator_enable calls made on this
2411 * regulator source are balanced by regulator_disable calls prior to calling
2414 void regulator_put(struct regulator *regulator)
2416 mutex_lock(®ulator_list_mutex);
2417 _regulator_put(regulator);
2418 mutex_unlock(®ulator_list_mutex);
2420 EXPORT_SYMBOL_GPL(regulator_put);
2423 * regulator_register_supply_alias - Provide device alias for supply lookup
2425 * @dev: device that will be given as the regulator "consumer"
2426 * @id: Supply name or regulator ID
2427 * @alias_dev: device that should be used to lookup the supply
2428 * @alias_id: Supply name or regulator ID that should be used to lookup the
2431 * All lookups for id on dev will instead be conducted for alias_id on
2434 int regulator_register_supply_alias(struct device *dev, const char *id,
2435 struct device *alias_dev,
2436 const char *alias_id)
2438 struct regulator_supply_alias *map;
2440 map = regulator_find_supply_alias(dev, id);
2444 map = kzalloc(sizeof(struct regulator_supply_alias), GFP_KERNEL);
2449 map->src_supply = id;
2450 map->alias_dev = alias_dev;
2451 map->alias_supply = alias_id;
2453 list_add(&map->list, ®ulator_supply_alias_list);
2455 pr_info("Adding alias for supply %s,%s -> %s,%s\n",
2456 id, dev_name(dev), alias_id, dev_name(alias_dev));
2460 EXPORT_SYMBOL_GPL(regulator_register_supply_alias);
2463 * regulator_unregister_supply_alias - Remove device alias
2465 * @dev: device that will be given as the regulator "consumer"
2466 * @id: Supply name or regulator ID
2468 * Remove a lookup alias if one exists for id on dev.
2470 void regulator_unregister_supply_alias(struct device *dev, const char *id)
2472 struct regulator_supply_alias *map;
2474 map = regulator_find_supply_alias(dev, id);
2476 list_del(&map->list);
2480 EXPORT_SYMBOL_GPL(regulator_unregister_supply_alias);
2483 * regulator_bulk_register_supply_alias - register multiple aliases
2485 * @dev: device that will be given as the regulator "consumer"
2486 * @id: List of supply names or regulator IDs
2487 * @alias_dev: device that should be used to lookup the supply
2488 * @alias_id: List of supply names or regulator IDs that should be used to
2490 * @num_id: Number of aliases to register
2492 * @return 0 on success, an errno on failure.
2494 * This helper function allows drivers to register several supply
2495 * aliases in one operation. If any of the aliases cannot be
2496 * registered any aliases that were registered will be removed
2497 * before returning to the caller.
2499 int regulator_bulk_register_supply_alias(struct device *dev,
2500 const char *const *id,
2501 struct device *alias_dev,
2502 const char *const *alias_id,
2508 for (i = 0; i < num_id; ++i) {
2509 ret = regulator_register_supply_alias(dev, id[i], alias_dev,
2519 "Failed to create supply alias %s,%s -> %s,%s\n",
2520 id[i], dev_name(dev), alias_id[i], dev_name(alias_dev));
2523 regulator_unregister_supply_alias(dev, id[i]);
2527 EXPORT_SYMBOL_GPL(regulator_bulk_register_supply_alias);
2530 * regulator_bulk_unregister_supply_alias - unregister multiple aliases
2532 * @dev: device that will be given as the regulator "consumer"
2533 * @id: List of supply names or regulator IDs
2534 * @num_id: Number of aliases to unregister
2536 * This helper function allows drivers to unregister several supply
2537 * aliases in one operation.
2539 void regulator_bulk_unregister_supply_alias(struct device *dev,
2540 const char *const *id,
2545 for (i = 0; i < num_id; ++i)
2546 regulator_unregister_supply_alias(dev, id[i]);
2548 EXPORT_SYMBOL_GPL(regulator_bulk_unregister_supply_alias);
2551 /* Manage enable GPIO list. Same GPIO pin can be shared among regulators */
2552 static int regulator_ena_gpio_request(struct regulator_dev *rdev,
2553 const struct regulator_config *config)
2555 struct regulator_enable_gpio *pin, *new_pin;
2556 struct gpio_desc *gpiod;
2558 gpiod = config->ena_gpiod;
2559 new_pin = kzalloc(sizeof(*new_pin), GFP_KERNEL);
2561 mutex_lock(®ulator_list_mutex);
2563 list_for_each_entry(pin, ®ulator_ena_gpio_list, list) {
2564 if (pin->gpiod == gpiod) {
2565 rdev_dbg(rdev, "GPIO is already used\n");
2566 goto update_ena_gpio_to_rdev;
2570 if (new_pin == NULL) {
2571 mutex_unlock(®ulator_list_mutex);
2579 list_add(&pin->list, ®ulator_ena_gpio_list);
2581 update_ena_gpio_to_rdev:
2582 pin->request_count++;
2583 rdev->ena_pin = pin;
2585 mutex_unlock(®ulator_list_mutex);
2591 static void regulator_ena_gpio_free(struct regulator_dev *rdev)
2593 struct regulator_enable_gpio *pin, *n;
2598 /* Free the GPIO only in case of no use */
2599 list_for_each_entry_safe(pin, n, ®ulator_ena_gpio_list, list) {
2600 if (pin != rdev->ena_pin)
2603 if (--pin->request_count)
2606 gpiod_put(pin->gpiod);
2607 list_del(&pin->list);
2612 rdev->ena_pin = NULL;
2616 * regulator_ena_gpio_ctrl - balance enable_count of each GPIO and actual GPIO pin control
2617 * @rdev: regulator_dev structure
2618 * @enable: enable GPIO at initial use?
2620 * GPIO is enabled in case of initial use. (enable_count is 0)
2621 * GPIO is disabled when it is not shared any more. (enable_count <= 1)
2623 static int regulator_ena_gpio_ctrl(struct regulator_dev *rdev, bool enable)
2625 struct regulator_enable_gpio *pin = rdev->ena_pin;
2631 /* Enable GPIO at initial use */
2632 if (pin->enable_count == 0)
2633 gpiod_set_value_cansleep(pin->gpiod, 1);
2635 pin->enable_count++;
2637 if (pin->enable_count > 1) {
2638 pin->enable_count--;
2642 /* Disable GPIO if not used */
2643 if (pin->enable_count <= 1) {
2644 gpiod_set_value_cansleep(pin->gpiod, 0);
2645 pin->enable_count = 0;
2653 * _regulator_delay_helper - a delay helper function
2654 * @delay: time to delay in microseconds
2656 * Delay for the requested amount of time as per the guidelines in:
2658 * Documentation/timers/timers-howto.rst
2660 * The assumption here is that these regulator operations will never used in
2661 * atomic context and therefore sleeping functions can be used.
2663 static void _regulator_delay_helper(unsigned int delay)
2665 unsigned int ms = delay / 1000;
2666 unsigned int us = delay % 1000;
2670 * For small enough values, handle super-millisecond
2671 * delays in the usleep_range() call below.
2680 * Give the scheduler some room to coalesce with any other
2681 * wakeup sources. For delays shorter than 10 us, don't even
2682 * bother setting up high-resolution timers and just busy-
2686 usleep_range(us, us + 100);
2692 * _regulator_check_status_enabled
2694 * A helper function to check if the regulator status can be interpreted
2695 * as 'regulator is enabled'.
2696 * @rdev: the regulator device to check
2699 * * 1 - if status shows regulator is in enabled state
2700 * * 0 - if not enabled state
2701 * * Error Value - as received from ops->get_status()
2703 static inline int _regulator_check_status_enabled(struct regulator_dev *rdev)
2705 int ret = rdev->desc->ops->get_status(rdev);
2708 rdev_info(rdev, "get_status returned error: %d\n", ret);
2713 case REGULATOR_STATUS_OFF:
2714 case REGULATOR_STATUS_ERROR:
2715 case REGULATOR_STATUS_UNDEFINED:
2722 static int _regulator_do_enable(struct regulator_dev *rdev)
2726 /* Query before enabling in case configuration dependent. */
2727 ret = _regulator_get_enable_time(rdev);
2731 rdev_warn(rdev, "enable_time() failed: %pe\n", ERR_PTR(ret));
2735 trace_regulator_enable(rdev_get_name(rdev));
2737 if (rdev->desc->off_on_delay) {
2738 /* if needed, keep a distance of off_on_delay from last time
2739 * this regulator was disabled.
2741 ktime_t end = ktime_add_us(rdev->last_off, rdev->desc->off_on_delay);
2742 s64 remaining = ktime_us_delta(end, ktime_get_boottime());
2745 _regulator_delay_helper(remaining);
2748 if (rdev->ena_pin) {
2749 if (!rdev->ena_gpio_state) {
2750 ret = regulator_ena_gpio_ctrl(rdev, true);
2753 rdev->ena_gpio_state = 1;
2755 } else if (rdev->desc->ops->enable) {
2756 ret = rdev->desc->ops->enable(rdev);
2763 /* Allow the regulator to ramp; it would be useful to extend
2764 * this for bulk operations so that the regulators can ramp
2767 trace_regulator_enable_delay(rdev_get_name(rdev));
2769 /* If poll_enabled_time is set, poll upto the delay calculated
2770 * above, delaying poll_enabled_time uS to check if the regulator
2771 * actually got enabled.
2772 * If the regulator isn't enabled after our delay helper has expired,
2773 * return -ETIMEDOUT.
2775 if (rdev->desc->poll_enabled_time) {
2776 int time_remaining = delay;
2778 while (time_remaining > 0) {
2779 _regulator_delay_helper(rdev->desc->poll_enabled_time);
2781 if (rdev->desc->ops->get_status) {
2782 ret = _regulator_check_status_enabled(rdev);
2787 } else if (rdev->desc->ops->is_enabled(rdev))
2790 time_remaining -= rdev->desc->poll_enabled_time;
2793 if (time_remaining <= 0) {
2794 rdev_err(rdev, "Enabled check timed out\n");
2798 _regulator_delay_helper(delay);
2801 trace_regulator_enable_complete(rdev_get_name(rdev));
2807 * _regulator_handle_consumer_enable - handle that a consumer enabled
2808 * @regulator: regulator source
2810 * Some things on a regulator consumer (like the contribution towards total
2811 * load on the regulator) only have an effect when the consumer wants the
2812 * regulator enabled. Explained in example with two consumers of the same
2814 * consumer A: set_load(100); => total load = 0
2815 * consumer A: regulator_enable(); => total load = 100
2816 * consumer B: set_load(1000); => total load = 100
2817 * consumer B: regulator_enable(); => total load = 1100
2818 * consumer A: regulator_disable(); => total_load = 1000
2820 * This function (together with _regulator_handle_consumer_disable) is
2821 * responsible for keeping track of the refcount for a given regulator consumer
2822 * and applying / unapplying these things.
2824 * Returns 0 upon no error; -error upon error.
2826 static int _regulator_handle_consumer_enable(struct regulator *regulator)
2829 struct regulator_dev *rdev = regulator->rdev;
2831 lockdep_assert_held_once(&rdev->mutex.base);
2833 regulator->enable_count++;
2834 if (regulator->uA_load && regulator->enable_count == 1) {
2835 ret = drms_uA_update(rdev);
2837 regulator->enable_count--;
2845 * _regulator_handle_consumer_disable - handle that a consumer disabled
2846 * @regulator: regulator source
2848 * The opposite of _regulator_handle_consumer_enable().
2850 * Returns 0 upon no error; -error upon error.
2852 static int _regulator_handle_consumer_disable(struct regulator *regulator)
2854 struct regulator_dev *rdev = regulator->rdev;
2856 lockdep_assert_held_once(&rdev->mutex.base);
2858 if (!regulator->enable_count) {
2859 rdev_err(rdev, "Underflow of regulator enable count\n");
2863 regulator->enable_count--;
2864 if (regulator->uA_load && regulator->enable_count == 0)
2865 return drms_uA_update(rdev);
2870 /* locks held by regulator_enable() */
2871 static int _regulator_enable(struct regulator *regulator)
2873 struct regulator_dev *rdev = regulator->rdev;
2876 lockdep_assert_held_once(&rdev->mutex.base);
2878 if (rdev->use_count == 0 && rdev->supply) {
2879 ret = _regulator_enable(rdev->supply);
2884 /* balance only if there are regulators coupled */
2885 if (rdev->coupling_desc.n_coupled > 1) {
2886 ret = regulator_balance_voltage(rdev, PM_SUSPEND_ON);
2888 goto err_disable_supply;
2891 ret = _regulator_handle_consumer_enable(regulator);
2893 goto err_disable_supply;
2895 if (rdev->use_count == 0) {
2897 * The regulator may already be enabled if it's not switchable
2900 ret = _regulator_is_enabled(rdev);
2901 if (ret == -EINVAL || ret == 0) {
2902 if (!regulator_ops_is_valid(rdev,
2903 REGULATOR_CHANGE_STATUS)) {
2905 goto err_consumer_disable;
2908 ret = _regulator_do_enable(rdev);
2910 goto err_consumer_disable;
2912 _notifier_call_chain(rdev, REGULATOR_EVENT_ENABLE,
2914 } else if (ret < 0) {
2915 rdev_err(rdev, "is_enabled() failed: %pe\n", ERR_PTR(ret));
2916 goto err_consumer_disable;
2918 /* Fallthrough on positive return values - already enabled */
2921 if (regulator->enable_count == 1)
2926 err_consumer_disable:
2927 _regulator_handle_consumer_disable(regulator);
2930 if (rdev->use_count == 0 && rdev->supply)
2931 _regulator_disable(rdev->supply);
2937 * regulator_enable - enable regulator output
2938 * @regulator: regulator source
2940 * Request that the regulator be enabled with the regulator output at
2941 * the predefined voltage or current value. Calls to regulator_enable()
2942 * must be balanced with calls to regulator_disable().
2944 * NOTE: the output value can be set by other drivers, boot loader or may be
2945 * hardwired in the regulator.
2947 int regulator_enable(struct regulator *regulator)
2949 struct regulator_dev *rdev = regulator->rdev;
2950 struct ww_acquire_ctx ww_ctx;
2953 regulator_lock_dependent(rdev, &ww_ctx);
2954 ret = _regulator_enable(regulator);
2955 regulator_unlock_dependent(rdev, &ww_ctx);
2959 EXPORT_SYMBOL_GPL(regulator_enable);
2961 static int _regulator_do_disable(struct regulator_dev *rdev)
2965 trace_regulator_disable(rdev_get_name(rdev));
2967 if (rdev->ena_pin) {
2968 if (rdev->ena_gpio_state) {
2969 ret = regulator_ena_gpio_ctrl(rdev, false);
2972 rdev->ena_gpio_state = 0;
2975 } else if (rdev->desc->ops->disable) {
2976 ret = rdev->desc->ops->disable(rdev);
2981 if (rdev->desc->off_on_delay)
2982 rdev->last_off = ktime_get_boottime();
2984 trace_regulator_disable_complete(rdev_get_name(rdev));
2989 /* locks held by regulator_disable() */
2990 static int _regulator_disable(struct regulator *regulator)
2992 struct regulator_dev *rdev = regulator->rdev;
2995 lockdep_assert_held_once(&rdev->mutex.base);
2997 if (WARN(regulator->enable_count == 0,
2998 "unbalanced disables for %s\n", rdev_get_name(rdev)))
3001 if (regulator->enable_count == 1) {
3002 /* disabling last enable_count from this regulator */
3003 /* are we the last user and permitted to disable ? */
3004 if (rdev->use_count == 1 &&
3005 (rdev->constraints && !rdev->constraints->always_on)) {
3007 /* we are last user */
3008 if (regulator_ops_is_valid(rdev, REGULATOR_CHANGE_STATUS)) {
3009 ret = _notifier_call_chain(rdev,
3010 REGULATOR_EVENT_PRE_DISABLE,
3012 if (ret & NOTIFY_STOP_MASK)
3015 ret = _regulator_do_disable(rdev);
3017 rdev_err(rdev, "failed to disable: %pe\n", ERR_PTR(ret));
3018 _notifier_call_chain(rdev,
3019 REGULATOR_EVENT_ABORT_DISABLE,
3023 _notifier_call_chain(rdev, REGULATOR_EVENT_DISABLE,
3027 rdev->use_count = 0;
3028 } else if (rdev->use_count > 1) {
3034 ret = _regulator_handle_consumer_disable(regulator);
3036 if (ret == 0 && rdev->coupling_desc.n_coupled > 1)
3037 ret = regulator_balance_voltage(rdev, PM_SUSPEND_ON);
3039 if (ret == 0 && rdev->use_count == 0 && rdev->supply)
3040 ret = _regulator_disable(rdev->supply);
3046 * regulator_disable - disable regulator output
3047 * @regulator: regulator source
3049 * Disable the regulator output voltage or current. Calls to
3050 * regulator_enable() must be balanced with calls to
3051 * regulator_disable().
3053 * NOTE: this will only disable the regulator output if no other consumer
3054 * devices have it enabled, the regulator device supports disabling and
3055 * machine constraints permit this operation.
3057 int regulator_disable(struct regulator *regulator)
3059 struct regulator_dev *rdev = regulator->rdev;
3060 struct ww_acquire_ctx ww_ctx;
3063 regulator_lock_dependent(rdev, &ww_ctx);
3064 ret = _regulator_disable(regulator);
3065 regulator_unlock_dependent(rdev, &ww_ctx);
3069 EXPORT_SYMBOL_GPL(regulator_disable);
3071 /* locks held by regulator_force_disable() */
3072 static int _regulator_force_disable(struct regulator_dev *rdev)
3076 lockdep_assert_held_once(&rdev->mutex.base);
3078 ret = _notifier_call_chain(rdev, REGULATOR_EVENT_FORCE_DISABLE |
3079 REGULATOR_EVENT_PRE_DISABLE, NULL);
3080 if (ret & NOTIFY_STOP_MASK)
3083 ret = _regulator_do_disable(rdev);
3085 rdev_err(rdev, "failed to force disable: %pe\n", ERR_PTR(ret));
3086 _notifier_call_chain(rdev, REGULATOR_EVENT_FORCE_DISABLE |
3087 REGULATOR_EVENT_ABORT_DISABLE, NULL);
3091 _notifier_call_chain(rdev, REGULATOR_EVENT_FORCE_DISABLE |
3092 REGULATOR_EVENT_DISABLE, NULL);
3098 * regulator_force_disable - force disable regulator output
3099 * @regulator: regulator source
3101 * Forcibly disable the regulator output voltage or current.
3102 * NOTE: this *will* disable the regulator output even if other consumer
3103 * devices have it enabled. This should be used for situations when device
3104 * damage will likely occur if the regulator is not disabled (e.g. over temp).
3106 int regulator_force_disable(struct regulator *regulator)
3108 struct regulator_dev *rdev = regulator->rdev;
3109 struct ww_acquire_ctx ww_ctx;
3112 regulator_lock_dependent(rdev, &ww_ctx);
3114 ret = _regulator_force_disable(regulator->rdev);
3116 if (rdev->coupling_desc.n_coupled > 1)
3117 regulator_balance_voltage(rdev, PM_SUSPEND_ON);
3119 if (regulator->uA_load) {
3120 regulator->uA_load = 0;
3121 ret = drms_uA_update(rdev);
3124 if (rdev->use_count != 0 && rdev->supply)
3125 _regulator_disable(rdev->supply);
3127 regulator_unlock_dependent(rdev, &ww_ctx);
3131 EXPORT_SYMBOL_GPL(regulator_force_disable);
3133 static void regulator_disable_work(struct work_struct *work)
3135 struct regulator_dev *rdev = container_of(work, struct regulator_dev,
3137 struct ww_acquire_ctx ww_ctx;
3139 struct regulator *regulator;
3140 int total_count = 0;
3142 regulator_lock_dependent(rdev, &ww_ctx);
3145 * Workqueue functions queue the new work instance while the previous
3146 * work instance is being processed. Cancel the queued work instance
3147 * as the work instance under processing does the job of the queued
3150 cancel_delayed_work(&rdev->disable_work);
3152 list_for_each_entry(regulator, &rdev->consumer_list, list) {
3153 count = regulator->deferred_disables;
3158 total_count += count;
3159 regulator->deferred_disables = 0;
3161 for (i = 0; i < count; i++) {
3162 ret = _regulator_disable(regulator);
3164 rdev_err(rdev, "Deferred disable failed: %pe\n",
3168 WARN_ON(!total_count);
3170 if (rdev->coupling_desc.n_coupled > 1)
3171 regulator_balance_voltage(rdev, PM_SUSPEND_ON);
3173 regulator_unlock_dependent(rdev, &ww_ctx);
3177 * regulator_disable_deferred - disable regulator output with delay
3178 * @regulator: regulator source
3179 * @ms: milliseconds until the regulator is disabled
3181 * Execute regulator_disable() on the regulator after a delay. This
3182 * is intended for use with devices that require some time to quiesce.
3184 * NOTE: this will only disable the regulator output if no other consumer
3185 * devices have it enabled, the regulator device supports disabling and
3186 * machine constraints permit this operation.
3188 int regulator_disable_deferred(struct regulator *regulator, int ms)
3190 struct regulator_dev *rdev = regulator->rdev;
3193 return regulator_disable(regulator);
3195 regulator_lock(rdev);
3196 regulator->deferred_disables++;
3197 mod_delayed_work(system_power_efficient_wq, &rdev->disable_work,
3198 msecs_to_jiffies(ms));
3199 regulator_unlock(rdev);
3203 EXPORT_SYMBOL_GPL(regulator_disable_deferred);
3205 static int _regulator_is_enabled(struct regulator_dev *rdev)
3207 /* A GPIO control always takes precedence */
3209 return rdev->ena_gpio_state;
3211 /* If we don't know then assume that the regulator is always on */
3212 if (!rdev->desc->ops->is_enabled)
3215 return rdev->desc->ops->is_enabled(rdev);
3218 static int _regulator_list_voltage(struct regulator_dev *rdev,
3219 unsigned selector, int lock)
3221 const struct regulator_ops *ops = rdev->desc->ops;
3224 if (rdev->desc->fixed_uV && rdev->desc->n_voltages == 1 && !selector)
3225 return rdev->desc->fixed_uV;
3227 if (ops->list_voltage) {
3228 if (selector >= rdev->desc->n_voltages)
3230 if (selector < rdev->desc->linear_min_sel)
3233 regulator_lock(rdev);
3234 ret = ops->list_voltage(rdev, selector);
3236 regulator_unlock(rdev);
3237 } else if (rdev->is_switch && rdev->supply) {
3238 ret = _regulator_list_voltage(rdev->supply->rdev,
3245 if (ret < rdev->constraints->min_uV)
3247 else if (ret > rdev->constraints->max_uV)
3255 * regulator_is_enabled - is the regulator output enabled
3256 * @regulator: regulator source
3258 * Returns positive if the regulator driver backing the source/client
3259 * has requested that the device be enabled, zero if it hasn't, else a
3260 * negative errno code.
3262 * Note that the device backing this regulator handle can have multiple
3263 * users, so it might be enabled even if regulator_enable() was never
3264 * called for this particular source.
3266 int regulator_is_enabled(struct regulator *regulator)
3270 if (regulator->always_on)
3273 regulator_lock(regulator->rdev);
3274 ret = _regulator_is_enabled(regulator->rdev);
3275 regulator_unlock(regulator->rdev);
3279 EXPORT_SYMBOL_GPL(regulator_is_enabled);
3282 * regulator_count_voltages - count regulator_list_voltage() selectors
3283 * @regulator: regulator source
3285 * Returns number of selectors, or negative errno. Selectors are
3286 * numbered starting at zero, and typically correspond to bitfields
3287 * in hardware registers.
3289 int regulator_count_voltages(struct regulator *regulator)
3291 struct regulator_dev *rdev = regulator->rdev;
3293 if (rdev->desc->n_voltages)
3294 return rdev->desc->n_voltages;
3296 if (!rdev->is_switch || !rdev->supply)
3299 return regulator_count_voltages(rdev->supply);
3301 EXPORT_SYMBOL_GPL(regulator_count_voltages);
3304 * regulator_list_voltage - enumerate supported voltages
3305 * @regulator: regulator source
3306 * @selector: identify voltage to list
3307 * Context: can sleep
3309 * Returns a voltage that can be passed to @regulator_set_voltage(),
3310 * zero if this selector code can't be used on this system, or a
3313 int regulator_list_voltage(struct regulator *regulator, unsigned selector)
3315 return _regulator_list_voltage(regulator->rdev, selector, 1);
3317 EXPORT_SYMBOL_GPL(regulator_list_voltage);
3320 * regulator_get_regmap - get the regulator's register map
3321 * @regulator: regulator source
3323 * Returns the register map for the given regulator, or an ERR_PTR value
3324 * if the regulator doesn't use regmap.
3326 struct regmap *regulator_get_regmap(struct regulator *regulator)
3328 struct regmap *map = regulator->rdev->regmap;
3330 return map ? map : ERR_PTR(-EOPNOTSUPP);
3334 * regulator_get_hardware_vsel_register - get the HW voltage selector register
3335 * @regulator: regulator source
3336 * @vsel_reg: voltage selector register, output parameter
3337 * @vsel_mask: mask for voltage selector bitfield, output parameter
3339 * Returns the hardware register offset and bitmask used for setting the
3340 * regulator voltage. This might be useful when configuring voltage-scaling
3341 * hardware or firmware that can make I2C requests behind the kernel's back,
3344 * On success, the output parameters @vsel_reg and @vsel_mask are filled in
3345 * and 0 is returned, otherwise a negative errno is returned.
3347 int regulator_get_hardware_vsel_register(struct regulator *regulator,
3349 unsigned *vsel_mask)
3351 struct regulator_dev *rdev = regulator->rdev;
3352 const struct regulator_ops *ops = rdev->desc->ops;
3354 if (ops->set_voltage_sel != regulator_set_voltage_sel_regmap)
3357 *vsel_reg = rdev->desc->vsel_reg;
3358 *vsel_mask = rdev->desc->vsel_mask;
3362 EXPORT_SYMBOL_GPL(regulator_get_hardware_vsel_register);
3365 * regulator_list_hardware_vsel - get the HW-specific register value for a selector
3366 * @regulator: regulator source
3367 * @selector: identify voltage to list
3369 * Converts the selector to a hardware-specific voltage selector that can be
3370 * directly written to the regulator registers. The address of the voltage
3371 * register can be determined by calling @regulator_get_hardware_vsel_register.
3373 * On error a negative errno is returned.
3375 int regulator_list_hardware_vsel(struct regulator *regulator,
3378 struct regulator_dev *rdev = regulator->rdev;
3379 const struct regulator_ops *ops = rdev->desc->ops;
3381 if (selector >= rdev->desc->n_voltages)
3383 if (selector < rdev->desc->linear_min_sel)
3385 if (ops->set_voltage_sel != regulator_set_voltage_sel_regmap)
3390 EXPORT_SYMBOL_GPL(regulator_list_hardware_vsel);
3393 * regulator_get_linear_step - return the voltage step size between VSEL values
3394 * @regulator: regulator source
3396 * Returns the voltage step size between VSEL values for linear
3397 * regulators, or return 0 if the regulator isn't a linear regulator.
3399 unsigned int regulator_get_linear_step(struct regulator *regulator)
3401 struct regulator_dev *rdev = regulator->rdev;
3403 return rdev->desc->uV_step;
3405 EXPORT_SYMBOL_GPL(regulator_get_linear_step);
3408 * regulator_is_supported_voltage - check if a voltage range can be supported
3410 * @regulator: Regulator to check.
3411 * @min_uV: Minimum required voltage in uV.
3412 * @max_uV: Maximum required voltage in uV.
3414 * Returns a boolean.
3416 int regulator_is_supported_voltage(struct regulator *regulator,
3417 int min_uV, int max_uV)
3419 struct regulator_dev *rdev = regulator->rdev;
3420 int i, voltages, ret;
3422 /* If we can't change voltage check the current voltage */
3423 if (!regulator_ops_is_valid(rdev, REGULATOR_CHANGE_VOLTAGE)) {
3424 ret = regulator_get_voltage(regulator);
3426 return min_uV <= ret && ret <= max_uV;
3431 /* Any voltage within constrains range is fine? */
3432 if (rdev->desc->continuous_voltage_range)
3433 return min_uV >= rdev->constraints->min_uV &&
3434 max_uV <= rdev->constraints->max_uV;
3436 ret = regulator_count_voltages(regulator);
3441 for (i = 0; i < voltages; i++) {
3442 ret = regulator_list_voltage(regulator, i);
3444 if (ret >= min_uV && ret <= max_uV)
3450 EXPORT_SYMBOL_GPL(regulator_is_supported_voltage);
3452 static int regulator_map_voltage(struct regulator_dev *rdev, int min_uV,
3455 const struct regulator_desc *desc = rdev->desc;
3457 if (desc->ops->map_voltage)
3458 return desc->ops->map_voltage(rdev, min_uV, max_uV);
3460 if (desc->ops->list_voltage == regulator_list_voltage_linear)
3461 return regulator_map_voltage_linear(rdev, min_uV, max_uV);
3463 if (desc->ops->list_voltage == regulator_list_voltage_linear_range)
3464 return regulator_map_voltage_linear_range(rdev, min_uV, max_uV);
3466 if (desc->ops->list_voltage ==
3467 regulator_list_voltage_pickable_linear_range)
3468 return regulator_map_voltage_pickable_linear_range(rdev,
3471 return regulator_map_voltage_iterate(rdev, min_uV, max_uV);
3474 static int _regulator_call_set_voltage(struct regulator_dev *rdev,
3475 int min_uV, int max_uV,
3478 struct pre_voltage_change_data data;
3481 data.old_uV = regulator_get_voltage_rdev(rdev);
3482 data.min_uV = min_uV;
3483 data.max_uV = max_uV;
3484 ret = _notifier_call_chain(rdev, REGULATOR_EVENT_PRE_VOLTAGE_CHANGE,
3486 if (ret & NOTIFY_STOP_MASK)
3489 ret = rdev->desc->ops->set_voltage(rdev, min_uV, max_uV, selector);
3493 _notifier_call_chain(rdev, REGULATOR_EVENT_ABORT_VOLTAGE_CHANGE,
3494 (void *)data.old_uV);
3499 static int _regulator_call_set_voltage_sel(struct regulator_dev *rdev,
3500 int uV, unsigned selector)
3502 struct pre_voltage_change_data data;
3505 data.old_uV = regulator_get_voltage_rdev(rdev);
3508 ret = _notifier_call_chain(rdev, REGULATOR_EVENT_PRE_VOLTAGE_CHANGE,
3510 if (ret & NOTIFY_STOP_MASK)
3513 ret = rdev->desc->ops->set_voltage_sel(rdev, selector);
3517 _notifier_call_chain(rdev, REGULATOR_EVENT_ABORT_VOLTAGE_CHANGE,
3518 (void *)data.old_uV);
3523 static int _regulator_set_voltage_sel_step(struct regulator_dev *rdev,
3524 int uV, int new_selector)
3526 const struct regulator_ops *ops = rdev->desc->ops;
3527 int diff, old_sel, curr_sel, ret;
3529 /* Stepping is only needed if the regulator is enabled. */
3530 if (!_regulator_is_enabled(rdev))
3533 if (!ops->get_voltage_sel)
3536 old_sel = ops->get_voltage_sel(rdev);
3540 diff = new_selector - old_sel;
3542 return 0; /* No change needed. */
3546 for (curr_sel = old_sel + rdev->desc->vsel_step;
3547 curr_sel < new_selector;
3548 curr_sel += rdev->desc->vsel_step) {
3550 * Call the callback directly instead of using
3551 * _regulator_call_set_voltage_sel() as we don't
3552 * want to notify anyone yet. Same in the branch
3555 ret = ops->set_voltage_sel(rdev, curr_sel);
3560 /* Stepping down. */
3561 for (curr_sel = old_sel - rdev->desc->vsel_step;
3562 curr_sel > new_selector;
3563 curr_sel -= rdev->desc->vsel_step) {
3564 ret = ops->set_voltage_sel(rdev, curr_sel);
3571 /* The final selector will trigger the notifiers. */
3572 return _regulator_call_set_voltage_sel(rdev, uV, new_selector);
3576 * At least try to return to the previous voltage if setting a new
3579 (void)ops->set_voltage_sel(rdev, old_sel);
3583 static int _regulator_set_voltage_time(struct regulator_dev *rdev,
3584 int old_uV, int new_uV)
3586 unsigned int ramp_delay = 0;
3588 if (rdev->constraints->ramp_delay)
3589 ramp_delay = rdev->constraints->ramp_delay;
3590 else if (rdev->desc->ramp_delay)
3591 ramp_delay = rdev->desc->ramp_delay;
3592 else if (rdev->constraints->settling_time)
3593 return rdev->constraints->settling_time;
3594 else if (rdev->constraints->settling_time_up &&
3596 return rdev->constraints->settling_time_up;
3597 else if (rdev->constraints->settling_time_down &&
3599 return rdev->constraints->settling_time_down;
3601 if (ramp_delay == 0)
3604 return DIV_ROUND_UP(abs(new_uV - old_uV), ramp_delay);
3607 static int _regulator_do_set_voltage(struct regulator_dev *rdev,
3608 int min_uV, int max_uV)
3613 unsigned int selector;
3614 int old_selector = -1;
3615 const struct regulator_ops *ops = rdev->desc->ops;
3616 int old_uV = regulator_get_voltage_rdev(rdev);
3618 trace_regulator_set_voltage(rdev_get_name(rdev), min_uV, max_uV);
3620 min_uV += rdev->constraints->uV_offset;
3621 max_uV += rdev->constraints->uV_offset;
3624 * If we can't obtain the old selector there is not enough
3625 * info to call set_voltage_time_sel().
3627 if (_regulator_is_enabled(rdev) &&
3628 ops->set_voltage_time_sel && ops->get_voltage_sel) {
3629 old_selector = ops->get_voltage_sel(rdev);
3630 if (old_selector < 0)
3631 return old_selector;
3634 if (ops->set_voltage) {
3635 ret = _regulator_call_set_voltage(rdev, min_uV, max_uV,
3639 if (ops->list_voltage)
3640 best_val = ops->list_voltage(rdev,
3643 best_val = regulator_get_voltage_rdev(rdev);
3646 } else if (ops->set_voltage_sel) {
3647 ret = regulator_map_voltage(rdev, min_uV, max_uV);
3649 best_val = ops->list_voltage(rdev, ret);
3650 if (min_uV <= best_val && max_uV >= best_val) {
3652 if (old_selector == selector)
3654 else if (rdev->desc->vsel_step)
3655 ret = _regulator_set_voltage_sel_step(
3656 rdev, best_val, selector);
3658 ret = _regulator_call_set_voltage_sel(
3659 rdev, best_val, selector);
3671 if (ops->set_voltage_time_sel) {
3673 * Call set_voltage_time_sel if successfully obtained
3676 if (old_selector >= 0 && old_selector != selector)
3677 delay = ops->set_voltage_time_sel(rdev, old_selector,
3680 if (old_uV != best_val) {
3681 if (ops->set_voltage_time)
3682 delay = ops->set_voltage_time(rdev, old_uV,
3685 delay = _regulator_set_voltage_time(rdev,
3692 rdev_warn(rdev, "failed to get delay: %pe\n", ERR_PTR(delay));
3696 /* Insert any necessary delays */
3697 _regulator_delay_helper(delay);
3699 if (best_val >= 0) {
3700 unsigned long data = best_val;
3702 _notifier_call_chain(rdev, REGULATOR_EVENT_VOLTAGE_CHANGE,
3707 trace_regulator_set_voltage_complete(rdev_get_name(rdev), best_val);
3712 static int _regulator_do_set_suspend_voltage(struct regulator_dev *rdev,
3713 int min_uV, int max_uV, suspend_state_t state)
3715 struct regulator_state *rstate;
3718 rstate = regulator_get_suspend_state(rdev, state);
3722 if (min_uV < rstate->min_uV)
3723 min_uV = rstate->min_uV;
3724 if (max_uV > rstate->max_uV)
3725 max_uV = rstate->max_uV;
3727 sel = regulator_map_voltage(rdev, min_uV, max_uV);
3731 uV = rdev->desc->ops->list_voltage(rdev, sel);
3732 if (uV >= min_uV && uV <= max_uV)
3738 static int regulator_set_voltage_unlocked(struct regulator *regulator,
3739 int min_uV, int max_uV,
3740 suspend_state_t state)
3742 struct regulator_dev *rdev = regulator->rdev;
3743 struct regulator_voltage *voltage = ®ulator->voltage[state];
3745 int old_min_uV, old_max_uV;
3748 /* If we're setting the same range as last time the change
3749 * should be a noop (some cpufreq implementations use the same
3750 * voltage for multiple frequencies, for example).
3752 if (voltage->min_uV == min_uV && voltage->max_uV == max_uV)
3755 /* If we're trying to set a range that overlaps the current voltage,
3756 * return successfully even though the regulator does not support
3757 * changing the voltage.
3759 if (!regulator_ops_is_valid(rdev, REGULATOR_CHANGE_VOLTAGE)) {
3760 current_uV = regulator_get_voltage_rdev(rdev);
3761 if (min_uV <= current_uV && current_uV <= max_uV) {
3762 voltage->min_uV = min_uV;
3763 voltage->max_uV = max_uV;
3769 if (!rdev->desc->ops->set_voltage &&
3770 !rdev->desc->ops->set_voltage_sel) {
3775 /* constraints check */
3776 ret = regulator_check_voltage(rdev, &min_uV, &max_uV);
3780 /* restore original values in case of error */
3781 old_min_uV = voltage->min_uV;
3782 old_max_uV = voltage->max_uV;
3783 voltage->min_uV = min_uV;
3784 voltage->max_uV = max_uV;
3786 /* for not coupled regulators this will just set the voltage */
3787 ret = regulator_balance_voltage(rdev, state);
3789 voltage->min_uV = old_min_uV;
3790 voltage->max_uV = old_max_uV;
3797 int regulator_set_voltage_rdev(struct regulator_dev *rdev, int min_uV,
3798 int max_uV, suspend_state_t state)
3800 int best_supply_uV = 0;
3801 int supply_change_uV = 0;
3805 regulator_ops_is_valid(rdev->supply->rdev,
3806 REGULATOR_CHANGE_VOLTAGE) &&
3807 (rdev->desc->min_dropout_uV || !(rdev->desc->ops->get_voltage ||
3808 rdev->desc->ops->get_voltage_sel))) {
3809 int current_supply_uV;
3812 selector = regulator_map_voltage(rdev, min_uV, max_uV);
3818 best_supply_uV = _regulator_list_voltage(rdev, selector, 0);
3819 if (best_supply_uV < 0) {
3820 ret = best_supply_uV;
3824 best_supply_uV += rdev->desc->min_dropout_uV;
3826 current_supply_uV = regulator_get_voltage_rdev(rdev->supply->rdev);
3827 if (current_supply_uV < 0) {
3828 ret = current_supply_uV;
3832 supply_change_uV = best_supply_uV - current_supply_uV;
3835 if (supply_change_uV > 0) {
3836 ret = regulator_set_voltage_unlocked(rdev->supply,
3837 best_supply_uV, INT_MAX, state);
3839 dev_err(&rdev->dev, "Failed to increase supply voltage: %pe\n",
3845 if (state == PM_SUSPEND_ON)
3846 ret = _regulator_do_set_voltage(rdev, min_uV, max_uV);
3848 ret = _regulator_do_set_suspend_voltage(rdev, min_uV,
3853 if (supply_change_uV < 0) {
3854 ret = regulator_set_voltage_unlocked(rdev->supply,
3855 best_supply_uV, INT_MAX, state);
3857 dev_warn(&rdev->dev, "Failed to decrease supply voltage: %pe\n",
3859 /* No need to fail here */
3866 EXPORT_SYMBOL_GPL(regulator_set_voltage_rdev);
3868 static int regulator_limit_voltage_step(struct regulator_dev *rdev,
3869 int *current_uV, int *min_uV)
3871 struct regulation_constraints *constraints = rdev->constraints;
3873 /* Limit voltage change only if necessary */
3874 if (!constraints->max_uV_step || !_regulator_is_enabled(rdev))
3877 if (*current_uV < 0) {
3878 *current_uV = regulator_get_voltage_rdev(rdev);
3880 if (*current_uV < 0)
3884 if (abs(*current_uV - *min_uV) <= constraints->max_uV_step)
3887 /* Clamp target voltage within the given step */
3888 if (*current_uV < *min_uV)
3889 *min_uV = min(*current_uV + constraints->max_uV_step,
3892 *min_uV = max(*current_uV - constraints->max_uV_step,
3898 static int regulator_get_optimal_voltage(struct regulator_dev *rdev,
3900 int *min_uV, int *max_uV,
3901 suspend_state_t state,
3904 struct coupling_desc *c_desc = &rdev->coupling_desc;
3905 struct regulator_dev **c_rdevs = c_desc->coupled_rdevs;
3906 struct regulation_constraints *constraints = rdev->constraints;
3907 int desired_min_uV = 0, desired_max_uV = INT_MAX;
3908 int max_current_uV = 0, min_current_uV = INT_MAX;
3909 int highest_min_uV = 0, target_uV, possible_uV;
3910 int i, ret, max_spread;
3916 * If there are no coupled regulators, simply set the voltage
3917 * demanded by consumers.
3919 if (n_coupled == 1) {
3921 * If consumers don't provide any demands, set voltage
3924 desired_min_uV = constraints->min_uV;
3925 desired_max_uV = constraints->max_uV;
3927 ret = regulator_check_consumers(rdev,
3929 &desired_max_uV, state);
3933 possible_uV = desired_min_uV;
3939 /* Find highest min desired voltage */
3940 for (i = 0; i < n_coupled; i++) {
3942 int tmp_max = INT_MAX;
3944 lockdep_assert_held_once(&c_rdevs[i]->mutex.base);
3946 ret = regulator_check_consumers(c_rdevs[i],
3952 ret = regulator_check_voltage(c_rdevs[i], &tmp_min, &tmp_max);
3956 highest_min_uV = max(highest_min_uV, tmp_min);
3959 desired_min_uV = tmp_min;
3960 desired_max_uV = tmp_max;
3964 max_spread = constraints->max_spread[0];
3967 * Let target_uV be equal to the desired one if possible.
3968 * If not, set it to minimum voltage, allowed by other coupled
3971 target_uV = max(desired_min_uV, highest_min_uV - max_spread);
3974 * Find min and max voltages, which currently aren't violating
3977 for (i = 1; i < n_coupled; i++) {
3980 if (!_regulator_is_enabled(c_rdevs[i]))
3983 tmp_act = regulator_get_voltage_rdev(c_rdevs[i]);
3987 min_current_uV = min(tmp_act, min_current_uV);
3988 max_current_uV = max(tmp_act, max_current_uV);
3991 /* There aren't any other regulators enabled */
3992 if (max_current_uV == 0) {
3993 possible_uV = target_uV;
3996 * Correct target voltage, so as it currently isn't
3997 * violating max_spread
3999 possible_uV = max(target_uV, max_current_uV - max_spread);
4000 possible_uV = min(possible_uV, min_current_uV + max_spread);
4003 if (possible_uV > desired_max_uV)
4006 done = (possible_uV == target_uV);
4007 desired_min_uV = possible_uV;
4010 /* Apply max_uV_step constraint if necessary */
4011 if (state == PM_SUSPEND_ON) {
4012 ret = regulator_limit_voltage_step(rdev, current_uV,
4021 /* Set current_uV if wasn't done earlier in the code and if necessary */
4022 if (n_coupled > 1 && *current_uV == -1) {
4024 if (_regulator_is_enabled(rdev)) {
4025 ret = regulator_get_voltage_rdev(rdev);
4031 *current_uV = desired_min_uV;
4035 *min_uV = desired_min_uV;
4036 *max_uV = desired_max_uV;
4041 int regulator_do_balance_voltage(struct regulator_dev *rdev,
4042 suspend_state_t state, bool skip_coupled)
4044 struct regulator_dev **c_rdevs;
4045 struct regulator_dev *best_rdev;
4046 struct coupling_desc *c_desc = &rdev->coupling_desc;
4047 int i, ret, n_coupled, best_min_uV, best_max_uV, best_c_rdev;
4048 unsigned int delta, best_delta;
4049 unsigned long c_rdev_done = 0;
4050 bool best_c_rdev_done;
4052 c_rdevs = c_desc->coupled_rdevs;
4053 n_coupled = skip_coupled ? 1 : c_desc->n_coupled;
4056 * Find the best possible voltage change on each loop. Leave the loop
4057 * if there isn't any possible change.
4060 best_c_rdev_done = false;
4068 * Find highest difference between optimal voltage
4069 * and current voltage.
4071 for (i = 0; i < n_coupled; i++) {
4073 * optimal_uV is the best voltage that can be set for
4074 * i-th regulator at the moment without violating
4075 * max_spread constraint in order to balance
4076 * the coupled voltages.
4078 int optimal_uV = 0, optimal_max_uV = 0, current_uV = 0;
4080 if (test_bit(i, &c_rdev_done))
4083 ret = regulator_get_optimal_voltage(c_rdevs[i],
4091 delta = abs(optimal_uV - current_uV);
4093 if (delta && best_delta <= delta) {
4094 best_c_rdev_done = ret;
4096 best_rdev = c_rdevs[i];
4097 best_min_uV = optimal_uV;
4098 best_max_uV = optimal_max_uV;
4103 /* Nothing to change, return successfully */
4109 ret = regulator_set_voltage_rdev(best_rdev, best_min_uV,
4110 best_max_uV, state);
4115 if (best_c_rdev_done)
4116 set_bit(best_c_rdev, &c_rdev_done);
4118 } while (n_coupled > 1);
4124 static int regulator_balance_voltage(struct regulator_dev *rdev,
4125 suspend_state_t state)
4127 struct coupling_desc *c_desc = &rdev->coupling_desc;
4128 struct regulator_coupler *coupler = c_desc->coupler;
4129 bool skip_coupled = false;
4132 * If system is in a state other than PM_SUSPEND_ON, don't check
4133 * other coupled regulators.
4135 if (state != PM_SUSPEND_ON)
4136 skip_coupled = true;
4138 if (c_desc->n_resolved < c_desc->n_coupled) {
4139 rdev_err(rdev, "Not all coupled regulators registered\n");
4143 /* Invoke custom balancer for customized couplers */
4144 if (coupler && coupler->balance_voltage)
4145 return coupler->balance_voltage(coupler, rdev, state);
4147 return regulator_do_balance_voltage(rdev, state, skip_coupled);
4151 * regulator_set_voltage - set regulator output voltage
4152 * @regulator: regulator source
4153 * @min_uV: Minimum required voltage in uV
4154 * @max_uV: Maximum acceptable voltage in uV
4156 * Sets a voltage regulator to the desired output voltage. This can be set
4157 * during any regulator state. IOW, regulator can be disabled or enabled.
4159 * If the regulator is enabled then the voltage will change to the new value
4160 * immediately otherwise if the regulator is disabled the regulator will
4161 * output at the new voltage when enabled.
4163 * NOTE: If the regulator is shared between several devices then the lowest
4164 * request voltage that meets the system constraints will be used.
4165 * Regulator system constraints must be set for this regulator before
4166 * calling this function otherwise this call will fail.
4168 int regulator_set_voltage(struct regulator *regulator, int min_uV, int max_uV)
4170 struct ww_acquire_ctx ww_ctx;
4173 regulator_lock_dependent(regulator->rdev, &ww_ctx);
4175 ret = regulator_set_voltage_unlocked(regulator, min_uV, max_uV,
4178 regulator_unlock_dependent(regulator->rdev, &ww_ctx);
4182 EXPORT_SYMBOL_GPL(regulator_set_voltage);
4184 static inline int regulator_suspend_toggle(struct regulator_dev *rdev,
4185 suspend_state_t state, bool en)
4187 struct regulator_state *rstate;
4189 rstate = regulator_get_suspend_state(rdev, state);
4193 if (!rstate->changeable)
4196 rstate->enabled = (en) ? ENABLE_IN_SUSPEND : DISABLE_IN_SUSPEND;
4201 int regulator_suspend_enable(struct regulator_dev *rdev,
4202 suspend_state_t state)
4204 return regulator_suspend_toggle(rdev, state, true);
4206 EXPORT_SYMBOL_GPL(regulator_suspend_enable);
4208 int regulator_suspend_disable(struct regulator_dev *rdev,
4209 suspend_state_t state)
4211 struct regulator *regulator;
4212 struct regulator_voltage *voltage;
4215 * if any consumer wants this regulator device keeping on in
4216 * suspend states, don't set it as disabled.
4218 list_for_each_entry(regulator, &rdev->consumer_list, list) {
4219 voltage = ®ulator->voltage[state];
4220 if (voltage->min_uV || voltage->max_uV)
4224 return regulator_suspend_toggle(rdev, state, false);
4226 EXPORT_SYMBOL_GPL(regulator_suspend_disable);
4228 static int _regulator_set_suspend_voltage(struct regulator *regulator,
4229 int min_uV, int max_uV,
4230 suspend_state_t state)
4232 struct regulator_dev *rdev = regulator->rdev;
4233 struct regulator_state *rstate;
4235 rstate = regulator_get_suspend_state(rdev, state);
4239 if (rstate->min_uV == rstate->max_uV) {
4240 rdev_err(rdev, "The suspend voltage can't be changed!\n");
4244 return regulator_set_voltage_unlocked(regulator, min_uV, max_uV, state);
4247 int regulator_set_suspend_voltage(struct regulator *regulator, int min_uV,
4248 int max_uV, suspend_state_t state)
4250 struct ww_acquire_ctx ww_ctx;
4253 /* PM_SUSPEND_ON is handled by regulator_set_voltage() */
4254 if (regulator_check_states(state) || state == PM_SUSPEND_ON)
4257 regulator_lock_dependent(regulator->rdev, &ww_ctx);
4259 ret = _regulator_set_suspend_voltage(regulator, min_uV,
4262 regulator_unlock_dependent(regulator->rdev, &ww_ctx);
4266 EXPORT_SYMBOL_GPL(regulator_set_suspend_voltage);
4269 * regulator_set_voltage_time - get raise/fall time
4270 * @regulator: regulator source
4271 * @old_uV: starting voltage in microvolts
4272 * @new_uV: target voltage in microvolts
4274 * Provided with the starting and ending voltage, this function attempts to
4275 * calculate the time in microseconds required to rise or fall to this new
4278 int regulator_set_voltage_time(struct regulator *regulator,
4279 int old_uV, int new_uV)
4281 struct regulator_dev *rdev = regulator->rdev;
4282 const struct regulator_ops *ops = rdev->desc->ops;
4288 if (ops->set_voltage_time)
4289 return ops->set_voltage_time(rdev, old_uV, new_uV);
4290 else if (!ops->set_voltage_time_sel)
4291 return _regulator_set_voltage_time(rdev, old_uV, new_uV);
4293 /* Currently requires operations to do this */
4294 if (!ops->list_voltage || !rdev->desc->n_voltages)
4297 for (i = 0; i < rdev->desc->n_voltages; i++) {
4298 /* We only look for exact voltage matches here */
4299 if (i < rdev->desc->linear_min_sel)
4302 if (old_sel >= 0 && new_sel >= 0)
4305 voltage = regulator_list_voltage(regulator, i);
4310 if (voltage == old_uV)
4312 if (voltage == new_uV)
4316 if (old_sel < 0 || new_sel < 0)
4319 return ops->set_voltage_time_sel(rdev, old_sel, new_sel);
4321 EXPORT_SYMBOL_GPL(regulator_set_voltage_time);
4324 * regulator_set_voltage_time_sel - get raise/fall time
4325 * @rdev: regulator source device
4326 * @old_selector: selector for starting voltage
4327 * @new_selector: selector for target voltage
4329 * Provided with the starting and target voltage selectors, this function
4330 * returns time in microseconds required to rise or fall to this new voltage
4332 * Drivers providing ramp_delay in regulation_constraints can use this as their
4333 * set_voltage_time_sel() operation.
4335 int regulator_set_voltage_time_sel(struct regulator_dev *rdev,
4336 unsigned int old_selector,
4337 unsigned int new_selector)
4339 int old_volt, new_volt;
4342 if (!rdev->desc->ops->list_voltage)
4345 old_volt = rdev->desc->ops->list_voltage(rdev, old_selector);
4346 new_volt = rdev->desc->ops->list_voltage(rdev, new_selector);
4348 if (rdev->desc->ops->set_voltage_time)
4349 return rdev->desc->ops->set_voltage_time(rdev, old_volt,
4352 return _regulator_set_voltage_time(rdev, old_volt, new_volt);
4354 EXPORT_SYMBOL_GPL(regulator_set_voltage_time_sel);
4356 int regulator_sync_voltage_rdev(struct regulator_dev *rdev)
4360 regulator_lock(rdev);
4362 if (!rdev->desc->ops->set_voltage &&
4363 !rdev->desc->ops->set_voltage_sel) {
4368 /* balance only, if regulator is coupled */
4369 if (rdev->coupling_desc.n_coupled > 1)
4370 ret = regulator_balance_voltage(rdev, PM_SUSPEND_ON);
4375 regulator_unlock(rdev);
4380 * regulator_sync_voltage - re-apply last regulator output voltage
4381 * @regulator: regulator source
4383 * Re-apply the last configured voltage. This is intended to be used
4384 * where some external control source the consumer is cooperating with
4385 * has caused the configured voltage to change.
4387 int regulator_sync_voltage(struct regulator *regulator)
4389 struct regulator_dev *rdev = regulator->rdev;
4390 struct regulator_voltage *voltage = ®ulator->voltage[PM_SUSPEND_ON];
4391 int ret, min_uV, max_uV;
4393 if (!regulator_ops_is_valid(rdev, REGULATOR_CHANGE_VOLTAGE))
4396 regulator_lock(rdev);
4398 if (!rdev->desc->ops->set_voltage &&
4399 !rdev->desc->ops->set_voltage_sel) {
4404 /* This is only going to work if we've had a voltage configured. */
4405 if (!voltage->min_uV && !voltage->max_uV) {
4410 min_uV = voltage->min_uV;
4411 max_uV = voltage->max_uV;
4413 /* This should be a paranoia check... */
4414 ret = regulator_check_voltage(rdev, &min_uV, &max_uV);
4418 ret = regulator_check_consumers(rdev, &min_uV, &max_uV, 0);
4422 /* balance only, if regulator is coupled */
4423 if (rdev->coupling_desc.n_coupled > 1)
4424 ret = regulator_balance_voltage(rdev, PM_SUSPEND_ON);
4426 ret = _regulator_do_set_voltage(rdev, min_uV, max_uV);
4429 regulator_unlock(rdev);
4432 EXPORT_SYMBOL_GPL(regulator_sync_voltage);
4434 int regulator_get_voltage_rdev(struct regulator_dev *rdev)
4439 if (rdev->desc->ops->get_bypass) {
4440 ret = rdev->desc->ops->get_bypass(rdev, &bypassed);
4444 /* if bypassed the regulator must have a supply */
4445 if (!rdev->supply) {
4447 "bypassed regulator has no supply!\n");
4448 return -EPROBE_DEFER;
4451 return regulator_get_voltage_rdev(rdev->supply->rdev);
4455 if (rdev->desc->ops->get_voltage_sel) {
4456 sel = rdev->desc->ops->get_voltage_sel(rdev);
4459 ret = rdev->desc->ops->list_voltage(rdev, sel);
4460 } else if (rdev->desc->ops->get_voltage) {
4461 ret = rdev->desc->ops->get_voltage(rdev);
4462 } else if (rdev->desc->ops->list_voltage) {
4463 ret = rdev->desc->ops->list_voltage(rdev, 0);
4464 } else if (rdev->desc->fixed_uV && (rdev->desc->n_voltages == 1)) {
4465 ret = rdev->desc->fixed_uV;
4466 } else if (rdev->supply) {
4467 ret = regulator_get_voltage_rdev(rdev->supply->rdev);
4468 } else if (rdev->supply_name) {
4469 return -EPROBE_DEFER;
4476 return ret - rdev->constraints->uV_offset;
4478 EXPORT_SYMBOL_GPL(regulator_get_voltage_rdev);
4481 * regulator_get_voltage - get regulator output voltage
4482 * @regulator: regulator source
4484 * This returns the current regulator voltage in uV.
4486 * NOTE: If the regulator is disabled it will return the voltage value. This
4487 * function should not be used to determine regulator state.
4489 int regulator_get_voltage(struct regulator *regulator)
4491 struct ww_acquire_ctx ww_ctx;
4494 regulator_lock_dependent(regulator->rdev, &ww_ctx);
4495 ret = regulator_get_voltage_rdev(regulator->rdev);
4496 regulator_unlock_dependent(regulator->rdev, &ww_ctx);
4500 EXPORT_SYMBOL_GPL(regulator_get_voltage);
4503 * regulator_set_current_limit - set regulator output current limit
4504 * @regulator: regulator source
4505 * @min_uA: Minimum supported current in uA
4506 * @max_uA: Maximum supported current in uA
4508 * Sets current sink to the desired output current. This can be set during
4509 * any regulator state. IOW, regulator can be disabled or enabled.
4511 * If the regulator is enabled then the current will change to the new value
4512 * immediately otherwise if the regulator is disabled the regulator will
4513 * output at the new current when enabled.
4515 * NOTE: Regulator system constraints must be set for this regulator before
4516 * calling this function otherwise this call will fail.
4518 int regulator_set_current_limit(struct regulator *regulator,
4519 int min_uA, int max_uA)
4521 struct regulator_dev *rdev = regulator->rdev;
4524 regulator_lock(rdev);
4527 if (!rdev->desc->ops->set_current_limit) {
4532 /* constraints check */
4533 ret = regulator_check_current_limit(rdev, &min_uA, &max_uA);
4537 ret = rdev->desc->ops->set_current_limit(rdev, min_uA, max_uA);
4539 regulator_unlock(rdev);
4542 EXPORT_SYMBOL_GPL(regulator_set_current_limit);
4544 static int _regulator_get_current_limit_unlocked(struct regulator_dev *rdev)
4547 if (!rdev->desc->ops->get_current_limit)
4550 return rdev->desc->ops->get_current_limit(rdev);
4553 static int _regulator_get_current_limit(struct regulator_dev *rdev)
4557 regulator_lock(rdev);
4558 ret = _regulator_get_current_limit_unlocked(rdev);
4559 regulator_unlock(rdev);
4565 * regulator_get_current_limit - get regulator output current
4566 * @regulator: regulator source
4568 * This returns the current supplied by the specified current sink in uA.
4570 * NOTE: If the regulator is disabled it will return the current value. This
4571 * function should not be used to determine regulator state.
4573 int regulator_get_current_limit(struct regulator *regulator)
4575 return _regulator_get_current_limit(regulator->rdev);
4577 EXPORT_SYMBOL_GPL(regulator_get_current_limit);
4580 * regulator_set_mode - set regulator operating mode
4581 * @regulator: regulator source
4582 * @mode: operating mode - one of the REGULATOR_MODE constants
4584 * Set regulator operating mode to increase regulator efficiency or improve
4585 * regulation performance.
4587 * NOTE: Regulator system constraints must be set for this regulator before
4588 * calling this function otherwise this call will fail.
4590 int regulator_set_mode(struct regulator *regulator, unsigned int mode)
4592 struct regulator_dev *rdev = regulator->rdev;
4594 int regulator_curr_mode;
4596 regulator_lock(rdev);
4599 if (!rdev->desc->ops->set_mode) {
4604 /* return if the same mode is requested */
4605 if (rdev->desc->ops->get_mode) {
4606 regulator_curr_mode = rdev->desc->ops->get_mode(rdev);
4607 if (regulator_curr_mode == mode) {
4613 /* constraints check */
4614 ret = regulator_mode_constrain(rdev, &mode);
4618 ret = rdev->desc->ops->set_mode(rdev, mode);
4620 regulator_unlock(rdev);
4623 EXPORT_SYMBOL_GPL(regulator_set_mode);
4625 static unsigned int _regulator_get_mode_unlocked(struct regulator_dev *rdev)
4628 if (!rdev->desc->ops->get_mode)
4631 return rdev->desc->ops->get_mode(rdev);
4634 static unsigned int _regulator_get_mode(struct regulator_dev *rdev)
4638 regulator_lock(rdev);
4639 ret = _regulator_get_mode_unlocked(rdev);
4640 regulator_unlock(rdev);
4646 * regulator_get_mode - get regulator operating mode
4647 * @regulator: regulator source
4649 * Get the current regulator operating mode.
4651 unsigned int regulator_get_mode(struct regulator *regulator)
4653 return _regulator_get_mode(regulator->rdev);
4655 EXPORT_SYMBOL_GPL(regulator_get_mode);
4657 static int rdev_get_cached_err_flags(struct regulator_dev *rdev)
4661 if (rdev->use_cached_err) {
4662 spin_lock(&rdev->err_lock);
4663 ret = rdev->cached_err;
4664 spin_unlock(&rdev->err_lock);
4669 static int _regulator_get_error_flags(struct regulator_dev *rdev,
4670 unsigned int *flags)
4672 int cached_flags, ret = 0;
4674 regulator_lock(rdev);
4676 cached_flags = rdev_get_cached_err_flags(rdev);
4678 if (rdev->desc->ops->get_error_flags)
4679 ret = rdev->desc->ops->get_error_flags(rdev, flags);
4680 else if (!rdev->use_cached_err)
4683 *flags |= cached_flags;
4685 regulator_unlock(rdev);
4691 * regulator_get_error_flags - get regulator error information
4692 * @regulator: regulator source
4693 * @flags: pointer to store error flags
4695 * Get the current regulator error information.
4697 int regulator_get_error_flags(struct regulator *regulator,
4698 unsigned int *flags)
4700 return _regulator_get_error_flags(regulator->rdev, flags);
4702 EXPORT_SYMBOL_GPL(regulator_get_error_flags);
4705 * regulator_set_load - set regulator load
4706 * @regulator: regulator source
4707 * @uA_load: load current
4709 * Notifies the regulator core of a new device load. This is then used by
4710 * DRMS (if enabled by constraints) to set the most efficient regulator
4711 * operating mode for the new regulator loading.
4713 * Consumer devices notify their supply regulator of the maximum power
4714 * they will require (can be taken from device datasheet in the power
4715 * consumption tables) when they change operational status and hence power
4716 * state. Examples of operational state changes that can affect power
4717 * consumption are :-
4719 * o Device is opened / closed.
4720 * o Device I/O is about to begin or has just finished.
4721 * o Device is idling in between work.
4723 * This information is also exported via sysfs to userspace.
4725 * DRMS will sum the total requested load on the regulator and change
4726 * to the most efficient operating mode if platform constraints allow.
4728 * NOTE: when a regulator consumer requests to have a regulator
4729 * disabled then any load that consumer requested no longer counts
4730 * toward the total requested load. If the regulator is re-enabled
4731 * then the previously requested load will start counting again.
4733 * If a regulator is an always-on regulator then an individual consumer's
4734 * load will still be removed if that consumer is fully disabled.
4736 * On error a negative errno is returned.
4738 int regulator_set_load(struct regulator *regulator, int uA_load)
4740 struct regulator_dev *rdev = regulator->rdev;
4744 regulator_lock(rdev);
4745 old_uA_load = regulator->uA_load;
4746 regulator->uA_load = uA_load;
4747 if (regulator->enable_count && old_uA_load != uA_load) {
4748 ret = drms_uA_update(rdev);
4750 regulator->uA_load = old_uA_load;
4752 regulator_unlock(rdev);
4756 EXPORT_SYMBOL_GPL(regulator_set_load);
4759 * regulator_allow_bypass - allow the regulator to go into bypass mode
4761 * @regulator: Regulator to configure
4762 * @enable: enable or disable bypass mode
4764 * Allow the regulator to go into bypass mode if all other consumers
4765 * for the regulator also enable bypass mode and the machine
4766 * constraints allow this. Bypass mode means that the regulator is
4767 * simply passing the input directly to the output with no regulation.
4769 int regulator_allow_bypass(struct regulator *regulator, bool enable)
4771 struct regulator_dev *rdev = regulator->rdev;
4772 const char *name = rdev_get_name(rdev);
4775 if (!rdev->desc->ops->set_bypass)
4778 if (!regulator_ops_is_valid(rdev, REGULATOR_CHANGE_BYPASS))
4781 regulator_lock(rdev);
4783 if (enable && !regulator->bypass) {
4784 rdev->bypass_count++;
4786 if (rdev->bypass_count == rdev->open_count) {
4787 trace_regulator_bypass_enable(name);
4789 ret = rdev->desc->ops->set_bypass(rdev, enable);
4791 rdev->bypass_count--;
4793 trace_regulator_bypass_enable_complete(name);
4796 } else if (!enable && regulator->bypass) {
4797 rdev->bypass_count--;
4799 if (rdev->bypass_count != rdev->open_count) {
4800 trace_regulator_bypass_disable(name);
4802 ret = rdev->desc->ops->set_bypass(rdev, enable);
4804 rdev->bypass_count++;
4806 trace_regulator_bypass_disable_complete(name);
4811 regulator->bypass = enable;
4813 regulator_unlock(rdev);
4817 EXPORT_SYMBOL_GPL(regulator_allow_bypass);
4820 * regulator_register_notifier - register regulator event notifier
4821 * @regulator: regulator source
4822 * @nb: notifier block
4824 * Register notifier block to receive regulator events.
4826 int regulator_register_notifier(struct regulator *regulator,
4827 struct notifier_block *nb)
4829 return blocking_notifier_chain_register(®ulator->rdev->notifier,
4832 EXPORT_SYMBOL_GPL(regulator_register_notifier);
4835 * regulator_unregister_notifier - unregister regulator event notifier
4836 * @regulator: regulator source
4837 * @nb: notifier block
4839 * Unregister regulator event notifier block.
4841 int regulator_unregister_notifier(struct regulator *regulator,
4842 struct notifier_block *nb)
4844 return blocking_notifier_chain_unregister(®ulator->rdev->notifier,
4847 EXPORT_SYMBOL_GPL(regulator_unregister_notifier);
4849 /* notify regulator consumers and downstream regulator consumers.
4850 * Note mutex must be held by caller.
4852 static int _notifier_call_chain(struct regulator_dev *rdev,
4853 unsigned long event, void *data)
4855 /* call rdev chain first */
4856 return blocking_notifier_call_chain(&rdev->notifier, event, data);
4859 int _regulator_bulk_get(struct device *dev, int num_consumers,
4860 struct regulator_bulk_data *consumers, enum regulator_get_type get_type)
4865 for (i = 0; i < num_consumers; i++)
4866 consumers[i].consumer = NULL;
4868 for (i = 0; i < num_consumers; i++) {
4869 consumers[i].consumer = _regulator_get(dev,
4870 consumers[i].supply, get_type);
4871 if (IS_ERR(consumers[i].consumer)) {
4872 ret = dev_err_probe(dev, PTR_ERR(consumers[i].consumer),
4873 "Failed to get supply '%s'",
4874 consumers[i].supply);
4875 consumers[i].consumer = NULL;
4879 if (consumers[i].init_load_uA > 0) {
4880 ret = regulator_set_load(consumers[i].consumer,
4881 consumers[i].init_load_uA);
4893 regulator_put(consumers[i].consumer);
4899 * regulator_bulk_get - get multiple regulator consumers
4901 * @dev: Device to supply
4902 * @num_consumers: Number of consumers to register
4903 * @consumers: Configuration of consumers; clients are stored here.
4905 * @return 0 on success, an errno on failure.
4907 * This helper function allows drivers to get several regulator
4908 * consumers in one operation. If any of the regulators cannot be
4909 * acquired then any regulators that were allocated will be freed
4910 * before returning to the caller.
4912 int regulator_bulk_get(struct device *dev, int num_consumers,
4913 struct regulator_bulk_data *consumers)
4915 return _regulator_bulk_get(dev, num_consumers, consumers, NORMAL_GET);
4917 EXPORT_SYMBOL_GPL(regulator_bulk_get);
4919 static void regulator_bulk_enable_async(void *data, async_cookie_t cookie)
4921 struct regulator_bulk_data *bulk = data;
4923 bulk->ret = regulator_enable(bulk->consumer);
4927 * regulator_bulk_enable - enable multiple regulator consumers
4929 * @num_consumers: Number of consumers
4930 * @consumers: Consumer data; clients are stored here.
4931 * @return 0 on success, an errno on failure
4933 * This convenience API allows consumers to enable multiple regulator
4934 * clients in a single API call. If any consumers cannot be enabled
4935 * then any others that were enabled will be disabled again prior to
4938 int regulator_bulk_enable(int num_consumers,
4939 struct regulator_bulk_data *consumers)
4941 ASYNC_DOMAIN_EXCLUSIVE(async_domain);
4945 for (i = 0; i < num_consumers; i++) {
4946 async_schedule_domain(regulator_bulk_enable_async,
4947 &consumers[i], &async_domain);
4950 async_synchronize_full_domain(&async_domain);
4952 /* If any consumer failed we need to unwind any that succeeded */
4953 for (i = 0; i < num_consumers; i++) {
4954 if (consumers[i].ret != 0) {
4955 ret = consumers[i].ret;
4963 for (i = 0; i < num_consumers; i++) {
4964 if (consumers[i].ret < 0)
4965 pr_err("Failed to enable %s: %pe\n", consumers[i].supply,
4966 ERR_PTR(consumers[i].ret));
4968 regulator_disable(consumers[i].consumer);
4973 EXPORT_SYMBOL_GPL(regulator_bulk_enable);
4976 * regulator_bulk_disable - disable multiple regulator consumers
4978 * @num_consumers: Number of consumers
4979 * @consumers: Consumer data; clients are stored here.
4980 * @return 0 on success, an errno on failure
4982 * This convenience API allows consumers to disable multiple regulator
4983 * clients in a single API call. If any consumers cannot be disabled
4984 * then any others that were disabled will be enabled again prior to
4987 int regulator_bulk_disable(int num_consumers,
4988 struct regulator_bulk_data *consumers)
4993 for (i = num_consumers - 1; i >= 0; --i) {
4994 ret = regulator_disable(consumers[i].consumer);
5002 pr_err("Failed to disable %s: %pe\n", consumers[i].supply, ERR_PTR(ret));
5003 for (++i; i < num_consumers; ++i) {
5004 r = regulator_enable(consumers[i].consumer);
5006 pr_err("Failed to re-enable %s: %pe\n",
5007 consumers[i].supply, ERR_PTR(r));
5012 EXPORT_SYMBOL_GPL(regulator_bulk_disable);
5015 * regulator_bulk_force_disable - force disable multiple regulator consumers
5017 * @num_consumers: Number of consumers
5018 * @consumers: Consumer data; clients are stored here.
5019 * @return 0 on success, an errno on failure
5021 * This convenience API allows consumers to forcibly disable multiple regulator
5022 * clients in a single API call.
5023 * NOTE: This should be used for situations when device damage will
5024 * likely occur if the regulators are not disabled (e.g. over temp).
5025 * Although regulator_force_disable function call for some consumers can
5026 * return error numbers, the function is called for all consumers.
5028 int regulator_bulk_force_disable(int num_consumers,
5029 struct regulator_bulk_data *consumers)
5034 for (i = 0; i < num_consumers; i++) {
5036 regulator_force_disable(consumers[i].consumer);
5038 /* Store first error for reporting */
5039 if (consumers[i].ret && !ret)
5040 ret = consumers[i].ret;
5045 EXPORT_SYMBOL_GPL(regulator_bulk_force_disable);
5048 * regulator_bulk_free - free multiple regulator consumers
5050 * @num_consumers: Number of consumers
5051 * @consumers: Consumer data; clients are stored here.
5053 * This convenience API allows consumers to free multiple regulator
5054 * clients in a single API call.
5056 void regulator_bulk_free(int num_consumers,
5057 struct regulator_bulk_data *consumers)
5061 for (i = 0; i < num_consumers; i++) {
5062 regulator_put(consumers[i].consumer);
5063 consumers[i].consumer = NULL;
5066 EXPORT_SYMBOL_GPL(regulator_bulk_free);
5069 * regulator_notifier_call_chain - call regulator event notifier
5070 * @rdev: regulator source
5071 * @event: notifier block
5072 * @data: callback-specific data.
5074 * Called by regulator drivers to notify clients a regulator event has
5077 int regulator_notifier_call_chain(struct regulator_dev *rdev,
5078 unsigned long event, void *data)
5080 _notifier_call_chain(rdev, event, data);
5084 EXPORT_SYMBOL_GPL(regulator_notifier_call_chain);
5087 * regulator_mode_to_status - convert a regulator mode into a status
5089 * @mode: Mode to convert
5091 * Convert a regulator mode into a status.
5093 int regulator_mode_to_status(unsigned int mode)
5096 case REGULATOR_MODE_FAST:
5097 return REGULATOR_STATUS_FAST;
5098 case REGULATOR_MODE_NORMAL:
5099 return REGULATOR_STATUS_NORMAL;
5100 case REGULATOR_MODE_IDLE:
5101 return REGULATOR_STATUS_IDLE;
5102 case REGULATOR_MODE_STANDBY:
5103 return REGULATOR_STATUS_STANDBY;
5105 return REGULATOR_STATUS_UNDEFINED;
5108 EXPORT_SYMBOL_GPL(regulator_mode_to_status);
5110 static struct attribute *regulator_dev_attrs[] = {
5111 &dev_attr_name.attr,
5112 &dev_attr_num_users.attr,
5113 &dev_attr_type.attr,
5114 &dev_attr_microvolts.attr,
5115 &dev_attr_microamps.attr,
5116 &dev_attr_opmode.attr,
5117 &dev_attr_state.attr,
5118 &dev_attr_status.attr,
5119 &dev_attr_bypass.attr,
5120 &dev_attr_requested_microamps.attr,
5121 &dev_attr_min_microvolts.attr,
5122 &dev_attr_max_microvolts.attr,
5123 &dev_attr_min_microamps.attr,
5124 &dev_attr_max_microamps.attr,
5125 &dev_attr_under_voltage.attr,
5126 &dev_attr_over_current.attr,
5127 &dev_attr_regulation_out.attr,
5128 &dev_attr_fail.attr,
5129 &dev_attr_over_temp.attr,
5130 &dev_attr_under_voltage_warn.attr,
5131 &dev_attr_over_current_warn.attr,
5132 &dev_attr_over_voltage_warn.attr,
5133 &dev_attr_over_temp_warn.attr,
5134 &dev_attr_suspend_standby_state.attr,
5135 &dev_attr_suspend_mem_state.attr,
5136 &dev_attr_suspend_disk_state.attr,
5137 &dev_attr_suspend_standby_microvolts.attr,
5138 &dev_attr_suspend_mem_microvolts.attr,
5139 &dev_attr_suspend_disk_microvolts.attr,
5140 &dev_attr_suspend_standby_mode.attr,
5141 &dev_attr_suspend_mem_mode.attr,
5142 &dev_attr_suspend_disk_mode.attr,
5147 * To avoid cluttering sysfs (and memory) with useless state, only
5148 * create attributes that can be meaningfully displayed.
5150 static umode_t regulator_attr_is_visible(struct kobject *kobj,
5151 struct attribute *attr, int idx)
5153 struct device *dev = kobj_to_dev(kobj);
5154 struct regulator_dev *rdev = dev_to_rdev(dev);
5155 const struct regulator_ops *ops = rdev->desc->ops;
5156 umode_t mode = attr->mode;
5158 /* these three are always present */
5159 if (attr == &dev_attr_name.attr ||
5160 attr == &dev_attr_num_users.attr ||
5161 attr == &dev_attr_type.attr)
5164 /* some attributes need specific methods to be displayed */
5165 if (attr == &dev_attr_microvolts.attr) {
5166 if ((ops->get_voltage && ops->get_voltage(rdev) >= 0) ||
5167 (ops->get_voltage_sel && ops->get_voltage_sel(rdev) >= 0) ||
5168 (ops->list_voltage && ops->list_voltage(rdev, 0) >= 0) ||
5169 (rdev->desc->fixed_uV && rdev->desc->n_voltages == 1))
5174 if (attr == &dev_attr_microamps.attr)
5175 return ops->get_current_limit ? mode : 0;
5177 if (attr == &dev_attr_opmode.attr)
5178 return ops->get_mode ? mode : 0;
5180 if (attr == &dev_attr_state.attr)
5181 return (rdev->ena_pin || ops->is_enabled) ? mode : 0;
5183 if (attr == &dev_attr_status.attr)
5184 return ops->get_status ? mode : 0;
5186 if (attr == &dev_attr_bypass.attr)
5187 return ops->get_bypass ? mode : 0;
5189 if (attr == &dev_attr_under_voltage.attr ||
5190 attr == &dev_attr_over_current.attr ||
5191 attr == &dev_attr_regulation_out.attr ||
5192 attr == &dev_attr_fail.attr ||
5193 attr == &dev_attr_over_temp.attr ||
5194 attr == &dev_attr_under_voltage_warn.attr ||
5195 attr == &dev_attr_over_current_warn.attr ||
5196 attr == &dev_attr_over_voltage_warn.attr ||
5197 attr == &dev_attr_over_temp_warn.attr)
5198 return ops->get_error_flags ? mode : 0;
5200 /* constraints need specific supporting methods */
5201 if (attr == &dev_attr_min_microvolts.attr ||
5202 attr == &dev_attr_max_microvolts.attr)
5203 return (ops->set_voltage || ops->set_voltage_sel) ? mode : 0;
5205 if (attr == &dev_attr_min_microamps.attr ||
5206 attr == &dev_attr_max_microamps.attr)
5207 return ops->set_current_limit ? mode : 0;
5209 if (attr == &dev_attr_suspend_standby_state.attr ||
5210 attr == &dev_attr_suspend_mem_state.attr ||
5211 attr == &dev_attr_suspend_disk_state.attr)
5214 if (attr == &dev_attr_suspend_standby_microvolts.attr ||
5215 attr == &dev_attr_suspend_mem_microvolts.attr ||
5216 attr == &dev_attr_suspend_disk_microvolts.attr)
5217 return ops->set_suspend_voltage ? mode : 0;
5219 if (attr == &dev_attr_suspend_standby_mode.attr ||
5220 attr == &dev_attr_suspend_mem_mode.attr ||
5221 attr == &dev_attr_suspend_disk_mode.attr)
5222 return ops->set_suspend_mode ? mode : 0;
5227 static const struct attribute_group regulator_dev_group = {
5228 .attrs = regulator_dev_attrs,
5229 .is_visible = regulator_attr_is_visible,
5232 static const struct attribute_group *regulator_dev_groups[] = {
5233 ®ulator_dev_group,
5237 static void regulator_dev_release(struct device *dev)
5239 struct regulator_dev *rdev = dev_get_drvdata(dev);
5241 debugfs_remove_recursive(rdev->debugfs);
5242 kfree(rdev->constraints);
5243 of_node_put(rdev->dev.of_node);
5247 static void rdev_init_debugfs(struct regulator_dev *rdev)
5249 struct device *parent = rdev->dev.parent;
5250 const char *rname = rdev_get_name(rdev);
5251 char name[NAME_MAX];
5253 /* Avoid duplicate debugfs directory names */
5254 if (parent && rname == rdev->desc->name) {
5255 snprintf(name, sizeof(name), "%s-%s", dev_name(parent),
5260 rdev->debugfs = debugfs_create_dir(rname, debugfs_root);
5261 if (IS_ERR(rdev->debugfs))
5262 rdev_dbg(rdev, "Failed to create debugfs directory\n");
5264 debugfs_create_u32("use_count", 0444, rdev->debugfs,
5266 debugfs_create_u32("open_count", 0444, rdev->debugfs,
5268 debugfs_create_u32("bypass_count", 0444, rdev->debugfs,
5269 &rdev->bypass_count);
5272 static int regulator_register_resolve_supply(struct device *dev, void *data)
5274 struct regulator_dev *rdev = dev_to_rdev(dev);
5276 if (regulator_resolve_supply(rdev))
5277 rdev_dbg(rdev, "unable to resolve supply\n");
5282 int regulator_coupler_register(struct regulator_coupler *coupler)
5284 mutex_lock(®ulator_list_mutex);
5285 list_add_tail(&coupler->list, ®ulator_coupler_list);
5286 mutex_unlock(®ulator_list_mutex);
5291 static struct regulator_coupler *
5292 regulator_find_coupler(struct regulator_dev *rdev)
5294 struct regulator_coupler *coupler;
5298 * Note that regulators are appended to the list and the generic
5299 * coupler is registered first, hence it will be attached at last
5302 list_for_each_entry_reverse(coupler, ®ulator_coupler_list, list) {
5303 err = coupler->attach_regulator(coupler, rdev);
5305 if (!coupler->balance_voltage &&
5306 rdev->coupling_desc.n_coupled > 2)
5307 goto err_unsupported;
5313 return ERR_PTR(err);
5321 return ERR_PTR(-EINVAL);
5324 if (coupler->detach_regulator)
5325 coupler->detach_regulator(coupler, rdev);
5328 "Voltage balancing for multiple regulator couples is unimplemented\n");
5330 return ERR_PTR(-EPERM);
5333 static void regulator_resolve_coupling(struct regulator_dev *rdev)
5335 struct regulator_coupler *coupler = rdev->coupling_desc.coupler;
5336 struct coupling_desc *c_desc = &rdev->coupling_desc;
5337 int n_coupled = c_desc->n_coupled;
5338 struct regulator_dev *c_rdev;
5341 for (i = 1; i < n_coupled; i++) {
5342 /* already resolved */
5343 if (c_desc->coupled_rdevs[i])
5346 c_rdev = of_parse_coupled_regulator(rdev, i - 1);
5351 if (c_rdev->coupling_desc.coupler != coupler) {
5352 rdev_err(rdev, "coupler mismatch with %s\n",
5353 rdev_get_name(c_rdev));
5357 c_desc->coupled_rdevs[i] = c_rdev;
5358 c_desc->n_resolved++;
5360 regulator_resolve_coupling(c_rdev);
5364 static void regulator_remove_coupling(struct regulator_dev *rdev)
5366 struct regulator_coupler *coupler = rdev->coupling_desc.coupler;
5367 struct coupling_desc *__c_desc, *c_desc = &rdev->coupling_desc;
5368 struct regulator_dev *__c_rdev, *c_rdev;
5369 unsigned int __n_coupled, n_coupled;
5373 n_coupled = c_desc->n_coupled;
5375 for (i = 1; i < n_coupled; i++) {
5376 c_rdev = c_desc->coupled_rdevs[i];
5381 regulator_lock(c_rdev);
5383 __c_desc = &c_rdev->coupling_desc;
5384 __n_coupled = __c_desc->n_coupled;
5386 for (k = 1; k < __n_coupled; k++) {
5387 __c_rdev = __c_desc->coupled_rdevs[k];
5389 if (__c_rdev == rdev) {
5390 __c_desc->coupled_rdevs[k] = NULL;
5391 __c_desc->n_resolved--;
5396 regulator_unlock(c_rdev);
5398 c_desc->coupled_rdevs[i] = NULL;
5399 c_desc->n_resolved--;
5402 if (coupler && coupler->detach_regulator) {
5403 err = coupler->detach_regulator(coupler, rdev);
5405 rdev_err(rdev, "failed to detach from coupler: %pe\n",
5409 kfree(rdev->coupling_desc.coupled_rdevs);
5410 rdev->coupling_desc.coupled_rdevs = NULL;
5413 static int regulator_init_coupling(struct regulator_dev *rdev)
5415 struct regulator_dev **coupled;
5416 int err, n_phandles;
5418 if (!IS_ENABLED(CONFIG_OF))
5421 n_phandles = of_get_n_coupled(rdev);
5423 coupled = kcalloc(n_phandles + 1, sizeof(*coupled), GFP_KERNEL);
5427 rdev->coupling_desc.coupled_rdevs = coupled;
5430 * Every regulator should always have coupling descriptor filled with
5431 * at least pointer to itself.
5433 rdev->coupling_desc.coupled_rdevs[0] = rdev;
5434 rdev->coupling_desc.n_coupled = n_phandles + 1;
5435 rdev->coupling_desc.n_resolved++;
5437 /* regulator isn't coupled */
5438 if (n_phandles == 0)
5441 if (!of_check_coupling_data(rdev))
5444 mutex_lock(®ulator_list_mutex);
5445 rdev->coupling_desc.coupler = regulator_find_coupler(rdev);
5446 mutex_unlock(®ulator_list_mutex);
5448 if (IS_ERR(rdev->coupling_desc.coupler)) {
5449 err = PTR_ERR(rdev->coupling_desc.coupler);
5450 rdev_err(rdev, "failed to get coupler: %pe\n", ERR_PTR(err));
5457 static int generic_coupler_attach(struct regulator_coupler *coupler,
5458 struct regulator_dev *rdev)
5460 if (rdev->coupling_desc.n_coupled > 2) {
5462 "Voltage balancing for multiple regulator couples is unimplemented\n");
5466 if (!rdev->constraints->always_on) {
5468 "Coupling of a non always-on regulator is unimplemented\n");
5475 static struct regulator_coupler generic_regulator_coupler = {
5476 .attach_regulator = generic_coupler_attach,
5480 * regulator_register - register regulator
5481 * @dev: the device that drive the regulator
5482 * @regulator_desc: regulator to register
5483 * @cfg: runtime configuration for regulator
5485 * Called by regulator drivers to register a regulator.
5486 * Returns a valid pointer to struct regulator_dev on success
5487 * or an ERR_PTR() on error.
5489 struct regulator_dev *
5490 regulator_register(struct device *dev,
5491 const struct regulator_desc *regulator_desc,
5492 const struct regulator_config *cfg)
5494 const struct regulator_init_data *init_data;
5495 struct regulator_config *config = NULL;
5496 static atomic_t regulator_no = ATOMIC_INIT(-1);
5497 struct regulator_dev *rdev;
5498 bool dangling_cfg_gpiod = false;
5499 bool dangling_of_gpiod = false;
5501 bool resolved_early = false;
5504 return ERR_PTR(-EINVAL);
5506 dangling_cfg_gpiod = true;
5507 if (regulator_desc == NULL) {
5512 WARN_ON(!dev || !cfg->dev);
5514 if (regulator_desc->name == NULL || regulator_desc->ops == NULL) {
5519 if (regulator_desc->type != REGULATOR_VOLTAGE &&
5520 regulator_desc->type != REGULATOR_CURRENT) {
5525 /* Only one of each should be implemented */
5526 WARN_ON(regulator_desc->ops->get_voltage &&
5527 regulator_desc->ops->get_voltage_sel);
5528 WARN_ON(regulator_desc->ops->set_voltage &&
5529 regulator_desc->ops->set_voltage_sel);
5531 /* If we're using selectors we must implement list_voltage. */
5532 if (regulator_desc->ops->get_voltage_sel &&
5533 !regulator_desc->ops->list_voltage) {
5537 if (regulator_desc->ops->set_voltage_sel &&
5538 !regulator_desc->ops->list_voltage) {
5543 rdev = kzalloc(sizeof(struct regulator_dev), GFP_KERNEL);
5548 device_initialize(&rdev->dev);
5549 dev_set_drvdata(&rdev->dev, rdev);
5550 rdev->dev.class = ®ulator_class;
5551 spin_lock_init(&rdev->err_lock);
5554 * Duplicate the config so the driver could override it after
5555 * parsing init data.
5557 config = kmemdup(cfg, sizeof(*cfg), GFP_KERNEL);
5558 if (config == NULL) {
5563 init_data = regulator_of_get_init_data(dev, regulator_desc, config,
5564 &rdev->dev.of_node);
5567 * Sometimes not all resources are probed already so we need to take
5568 * that into account. This happens most the time if the ena_gpiod comes
5569 * from a gpio extender or something else.
5571 if (PTR_ERR(init_data) == -EPROBE_DEFER) {
5572 ret = -EPROBE_DEFER;
5577 * We need to keep track of any GPIO descriptor coming from the
5578 * device tree until we have handled it over to the core. If the
5579 * config that was passed in to this function DOES NOT contain
5580 * a descriptor, and the config after this call DOES contain
5581 * a descriptor, we definitely got one from parsing the device
5584 if (!cfg->ena_gpiod && config->ena_gpiod)
5585 dangling_of_gpiod = true;
5587 init_data = config->init_data;
5588 rdev->dev.of_node = of_node_get(config->of_node);
5591 ww_mutex_init(&rdev->mutex, ®ulator_ww_class);
5592 rdev->reg_data = config->driver_data;
5593 rdev->owner = regulator_desc->owner;
5594 rdev->desc = regulator_desc;
5596 rdev->regmap = config->regmap;
5597 else if (dev_get_regmap(dev, NULL))
5598 rdev->regmap = dev_get_regmap(dev, NULL);
5599 else if (dev->parent)
5600 rdev->regmap = dev_get_regmap(dev->parent, NULL);
5601 INIT_LIST_HEAD(&rdev->consumer_list);
5602 INIT_LIST_HEAD(&rdev->list);
5603 BLOCKING_INIT_NOTIFIER_HEAD(&rdev->notifier);
5604 INIT_DELAYED_WORK(&rdev->disable_work, regulator_disable_work);
5606 if (init_data && init_data->supply_regulator)
5607 rdev->supply_name = init_data->supply_regulator;
5608 else if (regulator_desc->supply_name)
5609 rdev->supply_name = regulator_desc->supply_name;
5611 /* register with sysfs */
5612 rdev->dev.parent = config->dev;
5613 dev_set_name(&rdev->dev, "regulator.%lu",
5614 (unsigned long) atomic_inc_return(®ulator_no));
5616 /* set regulator constraints */
5618 rdev->constraints = kmemdup(&init_data->constraints,
5619 sizeof(*rdev->constraints),
5622 rdev->constraints = kzalloc(sizeof(*rdev->constraints),
5624 if (!rdev->constraints) {
5629 if ((rdev->supply_name && !rdev->supply) &&
5630 (rdev->constraints->always_on ||
5631 rdev->constraints->boot_on)) {
5632 ret = regulator_resolve_supply(rdev);
5634 rdev_dbg(rdev, "unable to resolve supply early: %pe\n",
5637 resolved_early = true;
5640 /* perform any regulator specific init */
5641 if (init_data && init_data->regulator_init) {
5642 ret = init_data->regulator_init(rdev->reg_data);
5647 if (config->ena_gpiod) {
5648 ret = regulator_ena_gpio_request(rdev, config);
5650 rdev_err(rdev, "Failed to request enable GPIO: %pe\n",
5654 /* The regulator core took over the GPIO descriptor */
5655 dangling_cfg_gpiod = false;
5656 dangling_of_gpiod = false;
5659 ret = set_machine_constraints(rdev);
5660 if (ret == -EPROBE_DEFER && !resolved_early) {
5661 /* Regulator might be in bypass mode and so needs its supply
5662 * to set the constraints
5664 /* FIXME: this currently triggers a chicken-and-egg problem
5665 * when creating -SUPPLY symlink in sysfs to a regulator
5666 * that is just being created
5668 rdev_dbg(rdev, "will resolve supply early: %s\n",
5670 ret = regulator_resolve_supply(rdev);
5672 ret = set_machine_constraints(rdev);
5674 rdev_dbg(rdev, "unable to resolve supply early: %pe\n",
5680 ret = regulator_init_coupling(rdev);
5684 /* add consumers devices */
5686 for (i = 0; i < init_data->num_consumer_supplies; i++) {
5687 ret = set_consumer_device_supply(rdev,
5688 init_data->consumer_supplies[i].dev_name,
5689 init_data->consumer_supplies[i].supply);
5691 dev_err(dev, "Failed to set supply %s\n",
5692 init_data->consumer_supplies[i].supply);
5693 goto unset_supplies;
5698 if (!rdev->desc->ops->get_voltage &&
5699 !rdev->desc->ops->list_voltage &&
5700 !rdev->desc->fixed_uV)
5701 rdev->is_switch = true;
5703 ret = device_add(&rdev->dev);
5705 goto unset_supplies;
5707 rdev_init_debugfs(rdev);
5709 /* try to resolve regulators coupling since a new one was registered */
5710 mutex_lock(®ulator_list_mutex);
5711 regulator_resolve_coupling(rdev);
5712 mutex_unlock(®ulator_list_mutex);
5714 /* try to resolve regulators supply since a new one was registered */
5715 class_for_each_device(®ulator_class, NULL, NULL,
5716 regulator_register_resolve_supply);
5721 mutex_lock(®ulator_list_mutex);
5722 unset_regulator_supplies(rdev);
5723 regulator_remove_coupling(rdev);
5724 mutex_unlock(®ulator_list_mutex);
5726 regulator_put(rdev->supply);
5727 kfree(rdev->coupling_desc.coupled_rdevs);
5728 mutex_lock(®ulator_list_mutex);
5729 regulator_ena_gpio_free(rdev);
5730 mutex_unlock(®ulator_list_mutex);
5732 if (dangling_of_gpiod)
5733 gpiod_put(config->ena_gpiod);
5735 put_device(&rdev->dev);
5737 if (dangling_cfg_gpiod)
5738 gpiod_put(cfg->ena_gpiod);
5739 return ERR_PTR(ret);
5741 EXPORT_SYMBOL_GPL(regulator_register);
5744 * regulator_unregister - unregister regulator
5745 * @rdev: regulator to unregister
5747 * Called by regulator drivers to unregister a regulator.
5749 void regulator_unregister(struct regulator_dev *rdev)
5755 while (rdev->use_count--)
5756 regulator_disable(rdev->supply);
5757 regulator_put(rdev->supply);
5760 flush_work(&rdev->disable_work.work);
5762 mutex_lock(®ulator_list_mutex);
5764 WARN_ON(rdev->open_count);
5765 regulator_remove_coupling(rdev);
5766 unset_regulator_supplies(rdev);
5767 list_del(&rdev->list);
5768 regulator_ena_gpio_free(rdev);
5769 device_unregister(&rdev->dev);
5771 mutex_unlock(®ulator_list_mutex);
5773 EXPORT_SYMBOL_GPL(regulator_unregister);
5775 #ifdef CONFIG_SUSPEND
5777 * regulator_suspend - prepare regulators for system wide suspend
5778 * @dev: ``&struct device`` pointer that is passed to _regulator_suspend()
5780 * Configure each regulator with it's suspend operating parameters for state.
5782 static int regulator_suspend(struct device *dev)
5784 struct regulator_dev *rdev = dev_to_rdev(dev);
5785 suspend_state_t state = pm_suspend_target_state;
5787 const struct regulator_state *rstate;
5789 rstate = regulator_get_suspend_state_check(rdev, state);
5793 regulator_lock(rdev);
5794 ret = __suspend_set_state(rdev, rstate);
5795 regulator_unlock(rdev);
5800 static int regulator_resume(struct device *dev)
5802 suspend_state_t state = pm_suspend_target_state;
5803 struct regulator_dev *rdev = dev_to_rdev(dev);
5804 struct regulator_state *rstate;
5807 rstate = regulator_get_suspend_state(rdev, state);
5811 /* Avoid grabbing the lock if we don't need to */
5812 if (!rdev->desc->ops->resume)
5815 regulator_lock(rdev);
5817 if (rstate->enabled == ENABLE_IN_SUSPEND ||
5818 rstate->enabled == DISABLE_IN_SUSPEND)
5819 ret = rdev->desc->ops->resume(rdev);
5821 regulator_unlock(rdev);
5825 #else /* !CONFIG_SUSPEND */
5827 #define regulator_suspend NULL
5828 #define regulator_resume NULL
5830 #endif /* !CONFIG_SUSPEND */
5833 static const struct dev_pm_ops __maybe_unused regulator_pm_ops = {
5834 .suspend = regulator_suspend,
5835 .resume = regulator_resume,
5839 struct class regulator_class = {
5840 .name = "regulator",
5841 .dev_release = regulator_dev_release,
5842 .dev_groups = regulator_dev_groups,
5844 .pm = ®ulator_pm_ops,
5848 * regulator_has_full_constraints - the system has fully specified constraints
5850 * Calling this function will cause the regulator API to disable all
5851 * regulators which have a zero use count and don't have an always_on
5852 * constraint in a late_initcall.
5854 * The intention is that this will become the default behaviour in a
5855 * future kernel release so users are encouraged to use this facility
5858 void regulator_has_full_constraints(void)
5860 has_full_constraints = 1;
5862 EXPORT_SYMBOL_GPL(regulator_has_full_constraints);
5865 * rdev_get_drvdata - get rdev regulator driver data
5868 * Get rdev regulator driver private data. This call can be used in the
5869 * regulator driver context.
5871 void *rdev_get_drvdata(struct regulator_dev *rdev)
5873 return rdev->reg_data;
5875 EXPORT_SYMBOL_GPL(rdev_get_drvdata);
5878 * regulator_get_drvdata - get regulator driver data
5879 * @regulator: regulator
5881 * Get regulator driver private data. This call can be used in the consumer
5882 * driver context when non API regulator specific functions need to be called.
5884 void *regulator_get_drvdata(struct regulator *regulator)
5886 return regulator->rdev->reg_data;
5888 EXPORT_SYMBOL_GPL(regulator_get_drvdata);
5891 * regulator_set_drvdata - set regulator driver data
5892 * @regulator: regulator
5895 void regulator_set_drvdata(struct regulator *regulator, void *data)
5897 regulator->rdev->reg_data = data;
5899 EXPORT_SYMBOL_GPL(regulator_set_drvdata);
5902 * rdev_get_id - get regulator ID
5905 int rdev_get_id(struct regulator_dev *rdev)
5907 return rdev->desc->id;
5909 EXPORT_SYMBOL_GPL(rdev_get_id);
5911 struct device *rdev_get_dev(struct regulator_dev *rdev)
5915 EXPORT_SYMBOL_GPL(rdev_get_dev);
5917 struct regmap *rdev_get_regmap(struct regulator_dev *rdev)
5919 return rdev->regmap;
5921 EXPORT_SYMBOL_GPL(rdev_get_regmap);
5923 void *regulator_get_init_drvdata(struct regulator_init_data *reg_init_data)
5925 return reg_init_data->driver_data;
5927 EXPORT_SYMBOL_GPL(regulator_get_init_drvdata);
5929 #ifdef CONFIG_DEBUG_FS
5930 static int supply_map_show(struct seq_file *sf, void *data)
5932 struct regulator_map *map;
5934 list_for_each_entry(map, ®ulator_map_list, list) {
5935 seq_printf(sf, "%s -> %s.%s\n",
5936 rdev_get_name(map->regulator), map->dev_name,
5942 DEFINE_SHOW_ATTRIBUTE(supply_map);
5944 struct summary_data {
5946 struct regulator_dev *parent;
5950 static void regulator_summary_show_subtree(struct seq_file *s,
5951 struct regulator_dev *rdev,
5954 static int regulator_summary_show_children(struct device *dev, void *data)
5956 struct regulator_dev *rdev = dev_to_rdev(dev);
5957 struct summary_data *summary_data = data;
5959 if (rdev->supply && rdev->supply->rdev == summary_data->parent)
5960 regulator_summary_show_subtree(summary_data->s, rdev,
5961 summary_data->level + 1);
5966 static void regulator_summary_show_subtree(struct seq_file *s,
5967 struct regulator_dev *rdev,
5970 struct regulation_constraints *c;
5971 struct regulator *consumer;
5972 struct summary_data summary_data;
5973 unsigned int opmode;
5978 opmode = _regulator_get_mode_unlocked(rdev);
5979 seq_printf(s, "%*s%-*s %3d %4d %6d %7s ",
5981 30 - level * 3, rdev_get_name(rdev),
5982 rdev->use_count, rdev->open_count, rdev->bypass_count,
5983 regulator_opmode_to_str(opmode));
5985 seq_printf(s, "%5dmV ", regulator_get_voltage_rdev(rdev) / 1000);
5986 seq_printf(s, "%5dmA ",
5987 _regulator_get_current_limit_unlocked(rdev) / 1000);
5989 c = rdev->constraints;
5991 switch (rdev->desc->type) {
5992 case REGULATOR_VOLTAGE:
5993 seq_printf(s, "%5dmV %5dmV ",
5994 c->min_uV / 1000, c->max_uV / 1000);
5996 case REGULATOR_CURRENT:
5997 seq_printf(s, "%5dmA %5dmA ",
5998 c->min_uA / 1000, c->max_uA / 1000);
6005 list_for_each_entry(consumer, &rdev->consumer_list, list) {
6006 if (consumer->dev && consumer->dev->class == ®ulator_class)
6009 seq_printf(s, "%*s%-*s ",
6010 (level + 1) * 3 + 1, "",
6011 30 - (level + 1) * 3,
6012 consumer->supply_name ? consumer->supply_name :
6013 consumer->dev ? dev_name(consumer->dev) : "deviceless");
6015 switch (rdev->desc->type) {
6016 case REGULATOR_VOLTAGE:
6017 seq_printf(s, "%3d %33dmA%c%5dmV %5dmV",
6018 consumer->enable_count,
6019 consumer->uA_load / 1000,
6020 consumer->uA_load && !consumer->enable_count ?
6022 consumer->voltage[PM_SUSPEND_ON].min_uV / 1000,
6023 consumer->voltage[PM_SUSPEND_ON].max_uV / 1000);
6025 case REGULATOR_CURRENT:
6033 summary_data.level = level;
6034 summary_data.parent = rdev;
6036 class_for_each_device(®ulator_class, NULL, &summary_data,
6037 regulator_summary_show_children);
6040 struct summary_lock_data {
6041 struct ww_acquire_ctx *ww_ctx;
6042 struct regulator_dev **new_contended_rdev;
6043 struct regulator_dev **old_contended_rdev;
6046 static int regulator_summary_lock_one(struct device *dev, void *data)
6048 struct regulator_dev *rdev = dev_to_rdev(dev);
6049 struct summary_lock_data *lock_data = data;
6052 if (rdev != *lock_data->old_contended_rdev) {
6053 ret = regulator_lock_nested(rdev, lock_data->ww_ctx);
6055 if (ret == -EDEADLK)
6056 *lock_data->new_contended_rdev = rdev;
6060 *lock_data->old_contended_rdev = NULL;
6066 static int regulator_summary_unlock_one(struct device *dev, void *data)
6068 struct regulator_dev *rdev = dev_to_rdev(dev);
6069 struct summary_lock_data *lock_data = data;
6072 if (rdev == *lock_data->new_contended_rdev)
6076 regulator_unlock(rdev);
6081 static int regulator_summary_lock_all(struct ww_acquire_ctx *ww_ctx,
6082 struct regulator_dev **new_contended_rdev,
6083 struct regulator_dev **old_contended_rdev)
6085 struct summary_lock_data lock_data;
6088 lock_data.ww_ctx = ww_ctx;
6089 lock_data.new_contended_rdev = new_contended_rdev;
6090 lock_data.old_contended_rdev = old_contended_rdev;
6092 ret = class_for_each_device(®ulator_class, NULL, &lock_data,
6093 regulator_summary_lock_one);
6095 class_for_each_device(®ulator_class, NULL, &lock_data,
6096 regulator_summary_unlock_one);
6101 static void regulator_summary_lock(struct ww_acquire_ctx *ww_ctx)
6103 struct regulator_dev *new_contended_rdev = NULL;
6104 struct regulator_dev *old_contended_rdev = NULL;
6107 mutex_lock(®ulator_list_mutex);
6109 ww_acquire_init(ww_ctx, ®ulator_ww_class);
6112 if (new_contended_rdev) {
6113 ww_mutex_lock_slow(&new_contended_rdev->mutex, ww_ctx);
6114 old_contended_rdev = new_contended_rdev;
6115 old_contended_rdev->ref_cnt++;
6116 old_contended_rdev->mutex_owner = current;
6119 err = regulator_summary_lock_all(ww_ctx,
6120 &new_contended_rdev,
6121 &old_contended_rdev);
6123 if (old_contended_rdev)
6124 regulator_unlock(old_contended_rdev);
6126 } while (err == -EDEADLK);
6128 ww_acquire_done(ww_ctx);
6131 static void regulator_summary_unlock(struct ww_acquire_ctx *ww_ctx)
6133 class_for_each_device(®ulator_class, NULL, NULL,
6134 regulator_summary_unlock_one);
6135 ww_acquire_fini(ww_ctx);
6137 mutex_unlock(®ulator_list_mutex);
6140 static int regulator_summary_show_roots(struct device *dev, void *data)
6142 struct regulator_dev *rdev = dev_to_rdev(dev);
6143 struct seq_file *s = data;
6146 regulator_summary_show_subtree(s, rdev, 0);
6151 static int regulator_summary_show(struct seq_file *s, void *data)
6153 struct ww_acquire_ctx ww_ctx;
6155 seq_puts(s, " regulator use open bypass opmode voltage current min max\n");
6156 seq_puts(s, "---------------------------------------------------------------------------------------\n");
6158 regulator_summary_lock(&ww_ctx);
6160 class_for_each_device(®ulator_class, NULL, s,
6161 regulator_summary_show_roots);
6163 regulator_summary_unlock(&ww_ctx);
6167 DEFINE_SHOW_ATTRIBUTE(regulator_summary);
6168 #endif /* CONFIG_DEBUG_FS */
6170 static int __init regulator_init(void)
6174 ret = class_register(®ulator_class);
6176 debugfs_root = debugfs_create_dir("regulator", NULL);
6177 if (IS_ERR(debugfs_root))
6178 pr_debug("regulator: Failed to create debugfs directory\n");
6180 #ifdef CONFIG_DEBUG_FS
6181 debugfs_create_file("supply_map", 0444, debugfs_root, NULL,
6184 debugfs_create_file("regulator_summary", 0444, debugfs_root,
6185 NULL, ®ulator_summary_fops);
6187 regulator_dummy_init();
6189 regulator_coupler_register(&generic_regulator_coupler);
6194 /* init early to allow our consumers to complete system booting */
6195 core_initcall(regulator_init);
6197 static int regulator_late_cleanup(struct device *dev, void *data)
6199 struct regulator_dev *rdev = dev_to_rdev(dev);
6200 struct regulation_constraints *c = rdev->constraints;
6203 if (c && c->always_on)
6206 if (!regulator_ops_is_valid(rdev, REGULATOR_CHANGE_STATUS))
6209 regulator_lock(rdev);
6211 if (rdev->use_count)
6214 /* If reading the status failed, assume that it's off. */
6215 if (_regulator_is_enabled(rdev) <= 0)
6218 if (have_full_constraints()) {
6219 /* We log since this may kill the system if it goes
6222 rdev_info(rdev, "disabling\n");
6223 ret = _regulator_do_disable(rdev);
6225 rdev_err(rdev, "couldn't disable: %pe\n", ERR_PTR(ret));
6227 /* The intention is that in future we will
6228 * assume that full constraints are provided
6229 * so warn even if we aren't going to do
6232 rdev_warn(rdev, "incomplete constraints, leaving on\n");
6236 regulator_unlock(rdev);
6241 static void regulator_init_complete_work_function(struct work_struct *work)
6244 * Regulators may had failed to resolve their input supplies
6245 * when were registered, either because the input supply was
6246 * not registered yet or because its parent device was not
6247 * bound yet. So attempt to resolve the input supplies for
6248 * pending regulators before trying to disable unused ones.
6250 class_for_each_device(®ulator_class, NULL, NULL,
6251 regulator_register_resolve_supply);
6253 /* If we have a full configuration then disable any regulators
6254 * we have permission to change the status for and which are
6255 * not in use or always_on. This is effectively the default
6256 * for DT and ACPI as they have full constraints.
6258 class_for_each_device(®ulator_class, NULL, NULL,
6259 regulator_late_cleanup);
6262 static DECLARE_DELAYED_WORK(regulator_init_complete_work,
6263 regulator_init_complete_work_function);
6265 static int __init regulator_init_complete(void)
6268 * Since DT doesn't provide an idiomatic mechanism for
6269 * enabling full constraints and since it's much more natural
6270 * with DT to provide them just assume that a DT enabled
6271 * system has full constraints.
6273 if (of_have_populated_dt())
6274 has_full_constraints = true;
6277 * We punt completion for an arbitrary amount of time since
6278 * systems like distros will load many drivers from userspace
6279 * so consumers might not always be ready yet, this is
6280 * particularly an issue with laptops where this might bounce
6281 * the display off then on. Ideally we'd get a notification
6282 * from userspace when this happens but we don't so just wait
6283 * a bit and hope we waited long enough. It'd be better if
6284 * we'd only do this on systems that need it, and a kernel
6285 * command line option might be useful.
6287 schedule_delayed_work(®ulator_init_complete_work,
6288 msecs_to_jiffies(30000));
6292 late_initcall_sync(regulator_init_complete);