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 *tmp;
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) {
238 * Start of loop: rdev1 was locked and rdev2 was contended.
239 * Need to unlock rdev1, slowly lock rdev2, then try rdev1
242 regulator_unlock(rdev1);
244 ww_mutex_lock_slow(&rdev2->mutex, ww_ctx);
246 rdev2->mutex_owner = current;
247 ret = regulator_lock_nested(rdev1, ww_ctx);
249 if (ret == -EDEADLOCK) {
250 /* More contention; swap which needs to be slow */
261 ww_acquire_done(ww_ctx);
265 * regulator_unlock_two - unlock two regulators
266 * @rdev1: first regulator
267 * @rdev2: second regulator
268 * @ww_ctx: w/w mutex acquire context
270 * The inverse of regulator_lock_two().
273 static void regulator_unlock_two(struct regulator_dev *rdev1,
274 struct regulator_dev *rdev2,
275 struct ww_acquire_ctx *ww_ctx)
277 regulator_unlock(rdev2);
278 regulator_unlock(rdev1);
279 ww_acquire_fini(ww_ctx);
282 static bool regulator_supply_is_couple(struct regulator_dev *rdev)
284 struct regulator_dev *c_rdev;
287 for (i = 1; i < rdev->coupling_desc.n_coupled; i++) {
288 c_rdev = rdev->coupling_desc.coupled_rdevs[i];
290 if (rdev->supply->rdev == c_rdev)
297 static void regulator_unlock_recursive(struct regulator_dev *rdev,
298 unsigned int n_coupled)
300 struct regulator_dev *c_rdev, *supply_rdev;
301 int i, supply_n_coupled;
303 for (i = n_coupled; i > 0; i--) {
304 c_rdev = rdev->coupling_desc.coupled_rdevs[i - 1];
309 if (c_rdev->supply && !regulator_supply_is_couple(c_rdev)) {
310 supply_rdev = c_rdev->supply->rdev;
311 supply_n_coupled = supply_rdev->coupling_desc.n_coupled;
313 regulator_unlock_recursive(supply_rdev,
317 regulator_unlock(c_rdev);
321 static int regulator_lock_recursive(struct regulator_dev *rdev,
322 struct regulator_dev **new_contended_rdev,
323 struct regulator_dev **old_contended_rdev,
324 struct ww_acquire_ctx *ww_ctx)
326 struct regulator_dev *c_rdev;
329 for (i = 0; i < rdev->coupling_desc.n_coupled; i++) {
330 c_rdev = rdev->coupling_desc.coupled_rdevs[i];
335 if (c_rdev != *old_contended_rdev) {
336 err = regulator_lock_nested(c_rdev, ww_ctx);
338 if (err == -EDEADLK) {
339 *new_contended_rdev = c_rdev;
343 /* shouldn't happen */
344 WARN_ON_ONCE(err != -EALREADY);
347 *old_contended_rdev = NULL;
350 if (c_rdev->supply && !regulator_supply_is_couple(c_rdev)) {
351 err = regulator_lock_recursive(c_rdev->supply->rdev,
356 regulator_unlock(c_rdev);
365 regulator_unlock_recursive(rdev, i);
371 * regulator_unlock_dependent - unlock regulator's suppliers and coupled
373 * @rdev: regulator source
374 * @ww_ctx: w/w mutex acquire context
376 * Unlock all regulators related with rdev by coupling or supplying.
378 static void regulator_unlock_dependent(struct regulator_dev *rdev,
379 struct ww_acquire_ctx *ww_ctx)
381 regulator_unlock_recursive(rdev, rdev->coupling_desc.n_coupled);
382 ww_acquire_fini(ww_ctx);
386 * regulator_lock_dependent - lock regulator's suppliers and coupled regulators
387 * @rdev: regulator source
388 * @ww_ctx: w/w mutex acquire context
390 * This function as a wrapper on regulator_lock_recursive(), which locks
391 * all regulators related with rdev by coupling or supplying.
393 static void regulator_lock_dependent(struct regulator_dev *rdev,
394 struct ww_acquire_ctx *ww_ctx)
396 struct regulator_dev *new_contended_rdev = NULL;
397 struct regulator_dev *old_contended_rdev = NULL;
400 mutex_lock(®ulator_list_mutex);
402 ww_acquire_init(ww_ctx, ®ulator_ww_class);
405 if (new_contended_rdev) {
406 ww_mutex_lock_slow(&new_contended_rdev->mutex, ww_ctx);
407 old_contended_rdev = new_contended_rdev;
408 old_contended_rdev->ref_cnt++;
409 old_contended_rdev->mutex_owner = current;
412 err = regulator_lock_recursive(rdev,
417 if (old_contended_rdev)
418 regulator_unlock(old_contended_rdev);
420 } while (err == -EDEADLK);
422 ww_acquire_done(ww_ctx);
424 mutex_unlock(®ulator_list_mutex);
428 * of_get_child_regulator - get a child regulator device node
429 * based on supply name
430 * @parent: Parent device node
431 * @prop_name: Combination regulator supply name and "-supply"
433 * Traverse all child nodes.
434 * Extract the child regulator device node corresponding to the supply name.
435 * returns the device node corresponding to the regulator if found, else
438 static struct device_node *of_get_child_regulator(struct device_node *parent,
439 const char *prop_name)
441 struct device_node *regnode = NULL;
442 struct device_node *child = NULL;
444 for_each_child_of_node(parent, child) {
445 regnode = of_parse_phandle(child, prop_name, 0);
448 regnode = of_get_child_regulator(child, prop_name);
463 * of_get_regulator - get a regulator device node based on supply name
464 * @dev: Device pointer for the consumer (of regulator) device
465 * @supply: regulator supply name
467 * Extract the regulator device node corresponding to the supply name.
468 * returns the device node corresponding to the regulator if found, else
471 static struct device_node *of_get_regulator(struct device *dev, const char *supply)
473 struct device_node *regnode = NULL;
474 char prop_name[64]; /* 64 is max size of property name */
476 dev_dbg(dev, "Looking up %s-supply from device tree\n", supply);
478 snprintf(prop_name, 64, "%s-supply", supply);
479 regnode = of_parse_phandle(dev->of_node, prop_name, 0);
482 regnode = of_get_child_regulator(dev->of_node, prop_name);
486 dev_dbg(dev, "Looking up %s property in node %pOF failed\n",
487 prop_name, dev->of_node);
493 /* Platform voltage constraint check */
494 int regulator_check_voltage(struct regulator_dev *rdev,
495 int *min_uV, int *max_uV)
497 BUG_ON(*min_uV > *max_uV);
499 if (!regulator_ops_is_valid(rdev, REGULATOR_CHANGE_VOLTAGE)) {
500 rdev_err(rdev, "voltage operation not allowed\n");
504 if (*max_uV > rdev->constraints->max_uV)
505 *max_uV = rdev->constraints->max_uV;
506 if (*min_uV < rdev->constraints->min_uV)
507 *min_uV = rdev->constraints->min_uV;
509 if (*min_uV > *max_uV) {
510 rdev_err(rdev, "unsupportable voltage range: %d-%duV\n",
518 /* return 0 if the state is valid */
519 static int regulator_check_states(suspend_state_t state)
521 return (state > PM_SUSPEND_MAX || state == PM_SUSPEND_TO_IDLE);
524 /* Make sure we select a voltage that suits the needs of all
525 * regulator consumers
527 int regulator_check_consumers(struct regulator_dev *rdev,
528 int *min_uV, int *max_uV,
529 suspend_state_t state)
531 struct regulator *regulator;
532 struct regulator_voltage *voltage;
534 list_for_each_entry(regulator, &rdev->consumer_list, list) {
535 voltage = ®ulator->voltage[state];
537 * Assume consumers that didn't say anything are OK
538 * with anything in the constraint range.
540 if (!voltage->min_uV && !voltage->max_uV)
543 if (*max_uV > voltage->max_uV)
544 *max_uV = voltage->max_uV;
545 if (*min_uV < voltage->min_uV)
546 *min_uV = voltage->min_uV;
549 if (*min_uV > *max_uV) {
550 rdev_err(rdev, "Restricting voltage, %u-%uuV\n",
558 /* current constraint check */
559 static int regulator_check_current_limit(struct regulator_dev *rdev,
560 int *min_uA, int *max_uA)
562 BUG_ON(*min_uA > *max_uA);
564 if (!regulator_ops_is_valid(rdev, REGULATOR_CHANGE_CURRENT)) {
565 rdev_err(rdev, "current operation not allowed\n");
569 if (*max_uA > rdev->constraints->max_uA)
570 *max_uA = rdev->constraints->max_uA;
571 if (*min_uA < rdev->constraints->min_uA)
572 *min_uA = rdev->constraints->min_uA;
574 if (*min_uA > *max_uA) {
575 rdev_err(rdev, "unsupportable current range: %d-%duA\n",
583 /* operating mode constraint check */
584 static int regulator_mode_constrain(struct regulator_dev *rdev,
588 case REGULATOR_MODE_FAST:
589 case REGULATOR_MODE_NORMAL:
590 case REGULATOR_MODE_IDLE:
591 case REGULATOR_MODE_STANDBY:
594 rdev_err(rdev, "invalid mode %x specified\n", *mode);
598 if (!regulator_ops_is_valid(rdev, REGULATOR_CHANGE_MODE)) {
599 rdev_err(rdev, "mode operation not allowed\n");
603 /* The modes are bitmasks, the most power hungry modes having
604 * the lowest values. If the requested mode isn't supported
608 if (rdev->constraints->valid_modes_mask & *mode)
616 static inline struct regulator_state *
617 regulator_get_suspend_state(struct regulator_dev *rdev, suspend_state_t state)
619 if (rdev->constraints == NULL)
623 case PM_SUSPEND_STANDBY:
624 return &rdev->constraints->state_standby;
626 return &rdev->constraints->state_mem;
628 return &rdev->constraints->state_disk;
634 static const struct regulator_state *
635 regulator_get_suspend_state_check(struct regulator_dev *rdev, suspend_state_t state)
637 const struct regulator_state *rstate;
639 rstate = regulator_get_suspend_state(rdev, state);
643 /* If we have no suspend mode configuration don't set anything;
644 * only warn if the driver implements set_suspend_voltage or
645 * set_suspend_mode callback.
647 if (rstate->enabled != ENABLE_IN_SUSPEND &&
648 rstate->enabled != DISABLE_IN_SUSPEND) {
649 if (rdev->desc->ops->set_suspend_voltage ||
650 rdev->desc->ops->set_suspend_mode)
651 rdev_warn(rdev, "No configuration\n");
658 static ssize_t microvolts_show(struct device *dev,
659 struct device_attribute *attr, char *buf)
661 struct regulator_dev *rdev = dev_get_drvdata(dev);
664 regulator_lock(rdev);
665 uV = regulator_get_voltage_rdev(rdev);
666 regulator_unlock(rdev);
670 return sprintf(buf, "%d\n", uV);
672 static DEVICE_ATTR_RO(microvolts);
674 static ssize_t microamps_show(struct device *dev,
675 struct device_attribute *attr, char *buf)
677 struct regulator_dev *rdev = dev_get_drvdata(dev);
679 return sprintf(buf, "%d\n", _regulator_get_current_limit(rdev));
681 static DEVICE_ATTR_RO(microamps);
683 static ssize_t name_show(struct device *dev, struct device_attribute *attr,
686 struct regulator_dev *rdev = dev_get_drvdata(dev);
688 return sprintf(buf, "%s\n", rdev_get_name(rdev));
690 static DEVICE_ATTR_RO(name);
692 static const char *regulator_opmode_to_str(int mode)
695 case REGULATOR_MODE_FAST:
697 case REGULATOR_MODE_NORMAL:
699 case REGULATOR_MODE_IDLE:
701 case REGULATOR_MODE_STANDBY:
707 static ssize_t regulator_print_opmode(char *buf, int mode)
709 return sprintf(buf, "%s\n", regulator_opmode_to_str(mode));
712 static ssize_t opmode_show(struct device *dev,
713 struct device_attribute *attr, char *buf)
715 struct regulator_dev *rdev = dev_get_drvdata(dev);
717 return regulator_print_opmode(buf, _regulator_get_mode(rdev));
719 static DEVICE_ATTR_RO(opmode);
721 static ssize_t regulator_print_state(char *buf, int state)
724 return sprintf(buf, "enabled\n");
726 return sprintf(buf, "disabled\n");
728 return sprintf(buf, "unknown\n");
731 static ssize_t state_show(struct device *dev,
732 struct device_attribute *attr, char *buf)
734 struct regulator_dev *rdev = dev_get_drvdata(dev);
737 regulator_lock(rdev);
738 ret = regulator_print_state(buf, _regulator_is_enabled(rdev));
739 regulator_unlock(rdev);
743 static DEVICE_ATTR_RO(state);
745 static ssize_t status_show(struct device *dev,
746 struct device_attribute *attr, char *buf)
748 struct regulator_dev *rdev = dev_get_drvdata(dev);
752 status = rdev->desc->ops->get_status(rdev);
757 case REGULATOR_STATUS_OFF:
760 case REGULATOR_STATUS_ON:
763 case REGULATOR_STATUS_ERROR:
766 case REGULATOR_STATUS_FAST:
769 case REGULATOR_STATUS_NORMAL:
772 case REGULATOR_STATUS_IDLE:
775 case REGULATOR_STATUS_STANDBY:
778 case REGULATOR_STATUS_BYPASS:
781 case REGULATOR_STATUS_UNDEFINED:
788 return sprintf(buf, "%s\n", label);
790 static DEVICE_ATTR_RO(status);
792 static ssize_t min_microamps_show(struct device *dev,
793 struct device_attribute *attr, char *buf)
795 struct regulator_dev *rdev = dev_get_drvdata(dev);
797 if (!rdev->constraints)
798 return sprintf(buf, "constraint not defined\n");
800 return sprintf(buf, "%d\n", rdev->constraints->min_uA);
802 static DEVICE_ATTR_RO(min_microamps);
804 static ssize_t max_microamps_show(struct device *dev,
805 struct device_attribute *attr, char *buf)
807 struct regulator_dev *rdev = dev_get_drvdata(dev);
809 if (!rdev->constraints)
810 return sprintf(buf, "constraint not defined\n");
812 return sprintf(buf, "%d\n", rdev->constraints->max_uA);
814 static DEVICE_ATTR_RO(max_microamps);
816 static ssize_t min_microvolts_show(struct device *dev,
817 struct device_attribute *attr, char *buf)
819 struct regulator_dev *rdev = dev_get_drvdata(dev);
821 if (!rdev->constraints)
822 return sprintf(buf, "constraint not defined\n");
824 return sprintf(buf, "%d\n", rdev->constraints->min_uV);
826 static DEVICE_ATTR_RO(min_microvolts);
828 static ssize_t max_microvolts_show(struct device *dev,
829 struct device_attribute *attr, char *buf)
831 struct regulator_dev *rdev = dev_get_drvdata(dev);
833 if (!rdev->constraints)
834 return sprintf(buf, "constraint not defined\n");
836 return sprintf(buf, "%d\n", rdev->constraints->max_uV);
838 static DEVICE_ATTR_RO(max_microvolts);
840 static ssize_t requested_microamps_show(struct device *dev,
841 struct device_attribute *attr, char *buf)
843 struct regulator_dev *rdev = dev_get_drvdata(dev);
844 struct regulator *regulator;
847 regulator_lock(rdev);
848 list_for_each_entry(regulator, &rdev->consumer_list, list) {
849 if (regulator->enable_count)
850 uA += regulator->uA_load;
852 regulator_unlock(rdev);
853 return sprintf(buf, "%d\n", uA);
855 static DEVICE_ATTR_RO(requested_microamps);
857 static ssize_t num_users_show(struct device *dev, struct device_attribute *attr,
860 struct regulator_dev *rdev = dev_get_drvdata(dev);
861 return sprintf(buf, "%d\n", rdev->use_count);
863 static DEVICE_ATTR_RO(num_users);
865 static ssize_t type_show(struct device *dev, struct device_attribute *attr,
868 struct regulator_dev *rdev = dev_get_drvdata(dev);
870 switch (rdev->desc->type) {
871 case REGULATOR_VOLTAGE:
872 return sprintf(buf, "voltage\n");
873 case REGULATOR_CURRENT:
874 return sprintf(buf, "current\n");
876 return sprintf(buf, "unknown\n");
878 static DEVICE_ATTR_RO(type);
880 static ssize_t suspend_mem_microvolts_show(struct device *dev,
881 struct device_attribute *attr, char *buf)
883 struct regulator_dev *rdev = dev_get_drvdata(dev);
885 return sprintf(buf, "%d\n", rdev->constraints->state_mem.uV);
887 static DEVICE_ATTR_RO(suspend_mem_microvolts);
889 static ssize_t suspend_disk_microvolts_show(struct device *dev,
890 struct device_attribute *attr, char *buf)
892 struct regulator_dev *rdev = dev_get_drvdata(dev);
894 return sprintf(buf, "%d\n", rdev->constraints->state_disk.uV);
896 static DEVICE_ATTR_RO(suspend_disk_microvolts);
898 static ssize_t suspend_standby_microvolts_show(struct device *dev,
899 struct device_attribute *attr, char *buf)
901 struct regulator_dev *rdev = dev_get_drvdata(dev);
903 return sprintf(buf, "%d\n", rdev->constraints->state_standby.uV);
905 static DEVICE_ATTR_RO(suspend_standby_microvolts);
907 static ssize_t suspend_mem_mode_show(struct device *dev,
908 struct device_attribute *attr, char *buf)
910 struct regulator_dev *rdev = dev_get_drvdata(dev);
912 return regulator_print_opmode(buf,
913 rdev->constraints->state_mem.mode);
915 static DEVICE_ATTR_RO(suspend_mem_mode);
917 static ssize_t suspend_disk_mode_show(struct device *dev,
918 struct device_attribute *attr, char *buf)
920 struct regulator_dev *rdev = dev_get_drvdata(dev);
922 return regulator_print_opmode(buf,
923 rdev->constraints->state_disk.mode);
925 static DEVICE_ATTR_RO(suspend_disk_mode);
927 static ssize_t suspend_standby_mode_show(struct device *dev,
928 struct device_attribute *attr, char *buf)
930 struct regulator_dev *rdev = dev_get_drvdata(dev);
932 return regulator_print_opmode(buf,
933 rdev->constraints->state_standby.mode);
935 static DEVICE_ATTR_RO(suspend_standby_mode);
937 static ssize_t suspend_mem_state_show(struct device *dev,
938 struct device_attribute *attr, char *buf)
940 struct regulator_dev *rdev = dev_get_drvdata(dev);
942 return regulator_print_state(buf,
943 rdev->constraints->state_mem.enabled);
945 static DEVICE_ATTR_RO(suspend_mem_state);
947 static ssize_t suspend_disk_state_show(struct device *dev,
948 struct device_attribute *attr, char *buf)
950 struct regulator_dev *rdev = dev_get_drvdata(dev);
952 return regulator_print_state(buf,
953 rdev->constraints->state_disk.enabled);
955 static DEVICE_ATTR_RO(suspend_disk_state);
957 static ssize_t suspend_standby_state_show(struct device *dev,
958 struct device_attribute *attr, char *buf)
960 struct regulator_dev *rdev = dev_get_drvdata(dev);
962 return regulator_print_state(buf,
963 rdev->constraints->state_standby.enabled);
965 static DEVICE_ATTR_RO(suspend_standby_state);
967 static ssize_t bypass_show(struct device *dev,
968 struct device_attribute *attr, char *buf)
970 struct regulator_dev *rdev = dev_get_drvdata(dev);
975 ret = rdev->desc->ops->get_bypass(rdev, &bypass);
984 return sprintf(buf, "%s\n", report);
986 static DEVICE_ATTR_RO(bypass);
988 #define REGULATOR_ERROR_ATTR(name, bit) \
989 static ssize_t name##_show(struct device *dev, struct device_attribute *attr, \
993 unsigned int flags; \
994 struct regulator_dev *rdev = dev_get_drvdata(dev); \
995 ret = _regulator_get_error_flags(rdev, &flags); \
998 return sysfs_emit(buf, "%d\n", !!(flags & (bit))); \
1000 static DEVICE_ATTR_RO(name)
1002 REGULATOR_ERROR_ATTR(under_voltage, REGULATOR_ERROR_UNDER_VOLTAGE);
1003 REGULATOR_ERROR_ATTR(over_current, REGULATOR_ERROR_OVER_CURRENT);
1004 REGULATOR_ERROR_ATTR(regulation_out, REGULATOR_ERROR_REGULATION_OUT);
1005 REGULATOR_ERROR_ATTR(fail, REGULATOR_ERROR_FAIL);
1006 REGULATOR_ERROR_ATTR(over_temp, REGULATOR_ERROR_OVER_TEMP);
1007 REGULATOR_ERROR_ATTR(under_voltage_warn, REGULATOR_ERROR_UNDER_VOLTAGE_WARN);
1008 REGULATOR_ERROR_ATTR(over_current_warn, REGULATOR_ERROR_OVER_CURRENT_WARN);
1009 REGULATOR_ERROR_ATTR(over_voltage_warn, REGULATOR_ERROR_OVER_VOLTAGE_WARN);
1010 REGULATOR_ERROR_ATTR(over_temp_warn, REGULATOR_ERROR_OVER_TEMP_WARN);
1012 /* Calculate the new optimum regulator operating mode based on the new total
1013 * consumer load. All locks held by caller
1015 static int drms_uA_update(struct regulator_dev *rdev)
1017 struct regulator *sibling;
1018 int current_uA = 0, output_uV, input_uV, err;
1022 * first check to see if we can set modes at all, otherwise just
1023 * tell the consumer everything is OK.
1025 if (!regulator_ops_is_valid(rdev, REGULATOR_CHANGE_DRMS)) {
1026 rdev_dbg(rdev, "DRMS operation not allowed\n");
1030 if (!rdev->desc->ops->get_optimum_mode &&
1031 !rdev->desc->ops->set_load)
1034 if (!rdev->desc->ops->set_mode &&
1035 !rdev->desc->ops->set_load)
1038 /* calc total requested load */
1039 list_for_each_entry(sibling, &rdev->consumer_list, list) {
1040 if (sibling->enable_count)
1041 current_uA += sibling->uA_load;
1044 current_uA += rdev->constraints->system_load;
1046 if (rdev->desc->ops->set_load) {
1047 /* set the optimum mode for our new total regulator load */
1048 err = rdev->desc->ops->set_load(rdev, current_uA);
1050 rdev_err(rdev, "failed to set load %d: %pe\n",
1051 current_uA, ERR_PTR(err));
1054 * Unfortunately in some cases the constraints->valid_ops has
1055 * REGULATOR_CHANGE_DRMS but there are no valid modes listed.
1056 * That's not really legit but we won't consider it a fatal
1057 * error here. We'll treat it as if REGULATOR_CHANGE_DRMS
1060 if (!rdev->constraints->valid_modes_mask) {
1061 rdev_dbg(rdev, "Can change modes; but no valid mode\n");
1065 /* get output voltage */
1066 output_uV = regulator_get_voltage_rdev(rdev);
1069 * Don't return an error; if regulator driver cares about
1070 * output_uV then it's up to the driver to validate.
1073 rdev_dbg(rdev, "invalid output voltage found\n");
1075 /* get input voltage */
1078 input_uV = regulator_get_voltage_rdev(rdev->supply->rdev);
1080 input_uV = rdev->constraints->input_uV;
1083 * Don't return an error; if regulator driver cares about
1084 * input_uV then it's up to the driver to validate.
1087 rdev_dbg(rdev, "invalid input voltage found\n");
1089 /* now get the optimum mode for our new total regulator load */
1090 mode = rdev->desc->ops->get_optimum_mode(rdev, input_uV,
1091 output_uV, current_uA);
1093 /* check the new mode is allowed */
1094 err = regulator_mode_constrain(rdev, &mode);
1096 rdev_err(rdev, "failed to get optimum mode @ %d uA %d -> %d uV: %pe\n",
1097 current_uA, input_uV, output_uV, ERR_PTR(err));
1101 err = rdev->desc->ops->set_mode(rdev, mode);
1103 rdev_err(rdev, "failed to set optimum mode %x: %pe\n",
1104 mode, ERR_PTR(err));
1110 static int __suspend_set_state(struct regulator_dev *rdev,
1111 const struct regulator_state *rstate)
1115 if (rstate->enabled == ENABLE_IN_SUSPEND &&
1116 rdev->desc->ops->set_suspend_enable)
1117 ret = rdev->desc->ops->set_suspend_enable(rdev);
1118 else if (rstate->enabled == DISABLE_IN_SUSPEND &&
1119 rdev->desc->ops->set_suspend_disable)
1120 ret = rdev->desc->ops->set_suspend_disable(rdev);
1121 else /* OK if set_suspend_enable or set_suspend_disable is NULL */
1125 rdev_err(rdev, "failed to enabled/disable: %pe\n", ERR_PTR(ret));
1129 if (rdev->desc->ops->set_suspend_voltage && rstate->uV > 0) {
1130 ret = rdev->desc->ops->set_suspend_voltage(rdev, rstate->uV);
1132 rdev_err(rdev, "failed to set voltage: %pe\n", ERR_PTR(ret));
1137 if (rdev->desc->ops->set_suspend_mode && rstate->mode > 0) {
1138 ret = rdev->desc->ops->set_suspend_mode(rdev, rstate->mode);
1140 rdev_err(rdev, "failed to set mode: %pe\n", ERR_PTR(ret));
1148 static int suspend_set_initial_state(struct regulator_dev *rdev)
1150 const struct regulator_state *rstate;
1152 rstate = regulator_get_suspend_state_check(rdev,
1153 rdev->constraints->initial_state);
1157 return __suspend_set_state(rdev, rstate);
1160 #if defined(DEBUG) || defined(CONFIG_DYNAMIC_DEBUG)
1161 static void print_constraints_debug(struct regulator_dev *rdev)
1163 struct regulation_constraints *constraints = rdev->constraints;
1165 size_t len = sizeof(buf) - 1;
1169 if (constraints->min_uV && constraints->max_uV) {
1170 if (constraints->min_uV == constraints->max_uV)
1171 count += scnprintf(buf + count, len - count, "%d mV ",
1172 constraints->min_uV / 1000);
1174 count += scnprintf(buf + count, len - count,
1176 constraints->min_uV / 1000,
1177 constraints->max_uV / 1000);
1180 if (!constraints->min_uV ||
1181 constraints->min_uV != constraints->max_uV) {
1182 ret = regulator_get_voltage_rdev(rdev);
1184 count += scnprintf(buf + count, len - count,
1185 "at %d mV ", ret / 1000);
1188 if (constraints->uV_offset)
1189 count += scnprintf(buf + count, len - count, "%dmV offset ",
1190 constraints->uV_offset / 1000);
1192 if (constraints->min_uA && constraints->max_uA) {
1193 if (constraints->min_uA == constraints->max_uA)
1194 count += scnprintf(buf + count, len - count, "%d mA ",
1195 constraints->min_uA / 1000);
1197 count += scnprintf(buf + count, len - count,
1199 constraints->min_uA / 1000,
1200 constraints->max_uA / 1000);
1203 if (!constraints->min_uA ||
1204 constraints->min_uA != constraints->max_uA) {
1205 ret = _regulator_get_current_limit(rdev);
1207 count += scnprintf(buf + count, len - count,
1208 "at %d mA ", ret / 1000);
1211 if (constraints->valid_modes_mask & REGULATOR_MODE_FAST)
1212 count += scnprintf(buf + count, len - count, "fast ");
1213 if (constraints->valid_modes_mask & REGULATOR_MODE_NORMAL)
1214 count += scnprintf(buf + count, len - count, "normal ");
1215 if (constraints->valid_modes_mask & REGULATOR_MODE_IDLE)
1216 count += scnprintf(buf + count, len - count, "idle ");
1217 if (constraints->valid_modes_mask & REGULATOR_MODE_STANDBY)
1218 count += scnprintf(buf + count, len - count, "standby ");
1221 count = scnprintf(buf, len, "no parameters");
1225 count += scnprintf(buf + count, len - count, ", %s",
1226 _regulator_is_enabled(rdev) ? "enabled" : "disabled");
1228 rdev_dbg(rdev, "%s\n", buf);
1230 #else /* !DEBUG && !CONFIG_DYNAMIC_DEBUG */
1231 static inline void print_constraints_debug(struct regulator_dev *rdev) {}
1232 #endif /* !DEBUG && !CONFIG_DYNAMIC_DEBUG */
1234 static void print_constraints(struct regulator_dev *rdev)
1236 struct regulation_constraints *constraints = rdev->constraints;
1238 print_constraints_debug(rdev);
1240 if ((constraints->min_uV != constraints->max_uV) &&
1241 !regulator_ops_is_valid(rdev, REGULATOR_CHANGE_VOLTAGE))
1243 "Voltage range but no REGULATOR_CHANGE_VOLTAGE\n");
1246 static int machine_constraints_voltage(struct regulator_dev *rdev,
1247 struct regulation_constraints *constraints)
1249 const struct regulator_ops *ops = rdev->desc->ops;
1252 /* do we need to apply the constraint voltage */
1253 if (rdev->constraints->apply_uV &&
1254 rdev->constraints->min_uV && rdev->constraints->max_uV) {
1255 int target_min, target_max;
1256 int current_uV = regulator_get_voltage_rdev(rdev);
1258 if (current_uV == -ENOTRECOVERABLE) {
1259 /* This regulator can't be read and must be initialized */
1260 rdev_info(rdev, "Setting %d-%duV\n",
1261 rdev->constraints->min_uV,
1262 rdev->constraints->max_uV);
1263 _regulator_do_set_voltage(rdev,
1264 rdev->constraints->min_uV,
1265 rdev->constraints->max_uV);
1266 current_uV = regulator_get_voltage_rdev(rdev);
1269 if (current_uV < 0) {
1270 if (current_uV != -EPROBE_DEFER)
1272 "failed to get the current voltage: %pe\n",
1273 ERR_PTR(current_uV));
1278 * If we're below the minimum voltage move up to the
1279 * minimum voltage, if we're above the maximum voltage
1280 * then move down to the maximum.
1282 target_min = current_uV;
1283 target_max = current_uV;
1285 if (current_uV < rdev->constraints->min_uV) {
1286 target_min = rdev->constraints->min_uV;
1287 target_max = rdev->constraints->min_uV;
1290 if (current_uV > rdev->constraints->max_uV) {
1291 target_min = rdev->constraints->max_uV;
1292 target_max = rdev->constraints->max_uV;
1295 if (target_min != current_uV || target_max != current_uV) {
1296 rdev_info(rdev, "Bringing %duV into %d-%duV\n",
1297 current_uV, target_min, target_max);
1298 ret = _regulator_do_set_voltage(
1299 rdev, target_min, target_max);
1302 "failed to apply %d-%duV constraint: %pe\n",
1303 target_min, target_max, ERR_PTR(ret));
1309 /* constrain machine-level voltage specs to fit
1310 * the actual range supported by this regulator.
1312 if (ops->list_voltage && rdev->desc->n_voltages) {
1313 int count = rdev->desc->n_voltages;
1315 int min_uV = INT_MAX;
1316 int max_uV = INT_MIN;
1317 int cmin = constraints->min_uV;
1318 int cmax = constraints->max_uV;
1320 /* it's safe to autoconfigure fixed-voltage supplies
1321 * and the constraints are used by list_voltage.
1323 if (count == 1 && !cmin) {
1326 constraints->min_uV = cmin;
1327 constraints->max_uV = cmax;
1330 /* voltage constraints are optional */
1331 if ((cmin == 0) && (cmax == 0))
1334 /* else require explicit machine-level constraints */
1335 if (cmin <= 0 || cmax <= 0 || cmax < cmin) {
1336 rdev_err(rdev, "invalid voltage constraints\n");
1340 /* no need to loop voltages if range is continuous */
1341 if (rdev->desc->continuous_voltage_range)
1344 /* initial: [cmin..cmax] valid, [min_uV..max_uV] not */
1345 for (i = 0; i < count; i++) {
1348 value = ops->list_voltage(rdev, i);
1352 /* maybe adjust [min_uV..max_uV] */
1353 if (value >= cmin && value < min_uV)
1355 if (value <= cmax && value > max_uV)
1359 /* final: [min_uV..max_uV] valid iff constraints valid */
1360 if (max_uV < min_uV) {
1362 "unsupportable voltage constraints %u-%uuV\n",
1367 /* use regulator's subset of machine constraints */
1368 if (constraints->min_uV < min_uV) {
1369 rdev_dbg(rdev, "override min_uV, %d -> %d\n",
1370 constraints->min_uV, min_uV);
1371 constraints->min_uV = min_uV;
1373 if (constraints->max_uV > max_uV) {
1374 rdev_dbg(rdev, "override max_uV, %d -> %d\n",
1375 constraints->max_uV, max_uV);
1376 constraints->max_uV = max_uV;
1383 static int machine_constraints_current(struct regulator_dev *rdev,
1384 struct regulation_constraints *constraints)
1386 const struct regulator_ops *ops = rdev->desc->ops;
1389 if (!constraints->min_uA && !constraints->max_uA)
1392 if (constraints->min_uA > constraints->max_uA) {
1393 rdev_err(rdev, "Invalid current constraints\n");
1397 if (!ops->set_current_limit || !ops->get_current_limit) {
1398 rdev_warn(rdev, "Operation of current configuration missing\n");
1402 /* Set regulator current in constraints range */
1403 ret = ops->set_current_limit(rdev, constraints->min_uA,
1404 constraints->max_uA);
1406 rdev_err(rdev, "Failed to set current constraint, %d\n", ret);
1413 static int _regulator_do_enable(struct regulator_dev *rdev);
1415 static int notif_set_limit(struct regulator_dev *rdev,
1416 int (*set)(struct regulator_dev *, int, int, bool),
1417 int limit, int severity)
1421 if (limit == REGULATOR_NOTIF_LIMIT_DISABLE) {
1428 if (limit == REGULATOR_NOTIF_LIMIT_ENABLE)
1431 return set(rdev, limit, severity, enable);
1434 static int handle_notify_limits(struct regulator_dev *rdev,
1435 int (*set)(struct regulator_dev *, int, int, bool),
1436 struct notification_limit *limits)
1444 ret = notif_set_limit(rdev, set, limits->prot,
1445 REGULATOR_SEVERITY_PROT);
1450 ret = notif_set_limit(rdev, set, limits->err,
1451 REGULATOR_SEVERITY_ERR);
1456 ret = notif_set_limit(rdev, set, limits->warn,
1457 REGULATOR_SEVERITY_WARN);
1462 * set_machine_constraints - sets regulator constraints
1463 * @rdev: regulator source
1465 * Allows platform initialisation code to define and constrain
1466 * regulator circuits e.g. valid voltage/current ranges, etc. NOTE:
1467 * Constraints *must* be set by platform code in order for some
1468 * regulator operations to proceed i.e. set_voltage, set_current_limit,
1471 static int set_machine_constraints(struct regulator_dev *rdev)
1474 const struct regulator_ops *ops = rdev->desc->ops;
1476 ret = machine_constraints_voltage(rdev, rdev->constraints);
1480 ret = machine_constraints_current(rdev, rdev->constraints);
1484 if (rdev->constraints->ilim_uA && ops->set_input_current_limit) {
1485 ret = ops->set_input_current_limit(rdev,
1486 rdev->constraints->ilim_uA);
1488 rdev_err(rdev, "failed to set input limit: %pe\n", ERR_PTR(ret));
1493 /* do we need to setup our suspend state */
1494 if (rdev->constraints->initial_state) {
1495 ret = suspend_set_initial_state(rdev);
1497 rdev_err(rdev, "failed to set suspend state: %pe\n", ERR_PTR(ret));
1502 if (rdev->constraints->initial_mode) {
1503 if (!ops->set_mode) {
1504 rdev_err(rdev, "no set_mode operation\n");
1508 ret = ops->set_mode(rdev, rdev->constraints->initial_mode);
1510 rdev_err(rdev, "failed to set initial mode: %pe\n", ERR_PTR(ret));
1513 } else if (rdev->constraints->system_load) {
1515 * We'll only apply the initial system load if an
1516 * initial mode wasn't specified.
1518 drms_uA_update(rdev);
1521 if ((rdev->constraints->ramp_delay || rdev->constraints->ramp_disable)
1522 && ops->set_ramp_delay) {
1523 ret = ops->set_ramp_delay(rdev, rdev->constraints->ramp_delay);
1525 rdev_err(rdev, "failed to set ramp_delay: %pe\n", ERR_PTR(ret));
1530 if (rdev->constraints->pull_down && ops->set_pull_down) {
1531 ret = ops->set_pull_down(rdev);
1533 rdev_err(rdev, "failed to set pull down: %pe\n", ERR_PTR(ret));
1538 if (rdev->constraints->soft_start && ops->set_soft_start) {
1539 ret = ops->set_soft_start(rdev);
1541 rdev_err(rdev, "failed to set soft start: %pe\n", ERR_PTR(ret));
1547 * Existing logic does not warn if over_current_protection is given as
1548 * a constraint but driver does not support that. I think we should
1549 * warn about this type of issues as it is possible someone changes
1550 * PMIC on board to another type - and the another PMIC's driver does
1551 * not support setting protection. Board composer may happily believe
1552 * the DT limits are respected - especially if the new PMIC HW also
1553 * supports protection but the driver does not. I won't change the logic
1554 * without hearing more experienced opinion on this though.
1556 * If warning is seen as a good idea then we can merge handling the
1557 * over-curret protection and detection and get rid of this special
1560 if (rdev->constraints->over_current_protection
1561 && ops->set_over_current_protection) {
1562 int lim = rdev->constraints->over_curr_limits.prot;
1564 ret = ops->set_over_current_protection(rdev, lim,
1565 REGULATOR_SEVERITY_PROT,
1568 rdev_err(rdev, "failed to set over current protection: %pe\n",
1574 if (rdev->constraints->over_current_detection)
1575 ret = handle_notify_limits(rdev,
1576 ops->set_over_current_protection,
1577 &rdev->constraints->over_curr_limits);
1579 if (ret != -EOPNOTSUPP) {
1580 rdev_err(rdev, "failed to set over current limits: %pe\n",
1585 "IC does not support requested over-current limits\n");
1588 if (rdev->constraints->over_voltage_detection)
1589 ret = handle_notify_limits(rdev,
1590 ops->set_over_voltage_protection,
1591 &rdev->constraints->over_voltage_limits);
1593 if (ret != -EOPNOTSUPP) {
1594 rdev_err(rdev, "failed to set over voltage limits %pe\n",
1599 "IC does not support requested over voltage limits\n");
1602 if (rdev->constraints->under_voltage_detection)
1603 ret = handle_notify_limits(rdev,
1604 ops->set_under_voltage_protection,
1605 &rdev->constraints->under_voltage_limits);
1607 if (ret != -EOPNOTSUPP) {
1608 rdev_err(rdev, "failed to set under voltage limits %pe\n",
1613 "IC does not support requested under voltage limits\n");
1616 if (rdev->constraints->over_temp_detection)
1617 ret = handle_notify_limits(rdev,
1618 ops->set_thermal_protection,
1619 &rdev->constraints->temp_limits);
1621 if (ret != -EOPNOTSUPP) {
1622 rdev_err(rdev, "failed to set temperature limits %pe\n",
1627 "IC does not support requested temperature limits\n");
1630 if (rdev->constraints->active_discharge && ops->set_active_discharge) {
1631 bool ad_state = (rdev->constraints->active_discharge ==
1632 REGULATOR_ACTIVE_DISCHARGE_ENABLE) ? true : false;
1634 ret = ops->set_active_discharge(rdev, ad_state);
1636 rdev_err(rdev, "failed to set active discharge: %pe\n", ERR_PTR(ret));
1642 * If there is no mechanism for controlling the regulator then
1643 * flag it as always_on so we don't end up duplicating checks
1644 * for this so much. Note that we could control the state of
1645 * a supply to control the output on a regulator that has no
1648 if (!rdev->ena_pin && !ops->enable) {
1649 if (rdev->supply_name && !rdev->supply)
1650 return -EPROBE_DEFER;
1653 rdev->constraints->always_on =
1654 rdev->supply->rdev->constraints->always_on;
1656 rdev->constraints->always_on = true;
1659 /* If the constraints say the regulator should be on at this point
1660 * and we have control then make sure it is enabled.
1662 if (rdev->constraints->always_on || rdev->constraints->boot_on) {
1663 /* If we want to enable this regulator, make sure that we know
1664 * the supplying regulator.
1666 if (rdev->supply_name && !rdev->supply)
1667 return -EPROBE_DEFER;
1669 /* If supplying regulator has already been enabled,
1670 * it's not intended to have use_count increment
1671 * when rdev is only boot-on.
1674 (rdev->constraints->always_on ||
1675 !regulator_is_enabled(rdev->supply))) {
1676 ret = regulator_enable(rdev->supply);
1678 _regulator_put(rdev->supply);
1679 rdev->supply = NULL;
1684 ret = _regulator_do_enable(rdev);
1685 if (ret < 0 && ret != -EINVAL) {
1686 rdev_err(rdev, "failed to enable: %pe\n", ERR_PTR(ret));
1690 if (rdev->constraints->always_on)
1692 } else if (rdev->desc->off_on_delay) {
1693 rdev->last_off = ktime_get();
1696 print_constraints(rdev);
1701 * set_supply - set regulator supply regulator
1702 * @rdev: regulator (locked)
1703 * @supply_rdev: supply regulator (locked))
1705 * Called by platform initialisation code to set the supply regulator for this
1706 * regulator. This ensures that a regulators supply will also be enabled by the
1707 * core if it's child is enabled.
1709 static int set_supply(struct regulator_dev *rdev,
1710 struct regulator_dev *supply_rdev)
1714 rdev_dbg(rdev, "supplied by %s\n", rdev_get_name(supply_rdev));
1716 if (!try_module_get(supply_rdev->owner))
1719 rdev->supply = create_regulator(supply_rdev, &rdev->dev, "SUPPLY");
1720 if (rdev->supply == NULL) {
1721 module_put(supply_rdev->owner);
1725 supply_rdev->open_count++;
1731 * set_consumer_device_supply - Bind a regulator to a symbolic supply
1732 * @rdev: regulator source
1733 * @consumer_dev_name: dev_name() string for device supply applies to
1734 * @supply: symbolic name for supply
1736 * Allows platform initialisation code to map physical regulator
1737 * sources to symbolic names for supplies for use by devices. Devices
1738 * should use these symbolic names to request regulators, avoiding the
1739 * need to provide board-specific regulator names as platform data.
1741 static int set_consumer_device_supply(struct regulator_dev *rdev,
1742 const char *consumer_dev_name,
1745 struct regulator_map *node, *new_node;
1751 if (consumer_dev_name != NULL)
1756 new_node = kzalloc(sizeof(struct regulator_map), GFP_KERNEL);
1757 if (new_node == NULL)
1760 new_node->regulator = rdev;
1761 new_node->supply = supply;
1764 new_node->dev_name = kstrdup(consumer_dev_name, GFP_KERNEL);
1765 if (new_node->dev_name == NULL) {
1771 mutex_lock(®ulator_list_mutex);
1772 list_for_each_entry(node, ®ulator_map_list, list) {
1773 if (node->dev_name && consumer_dev_name) {
1774 if (strcmp(node->dev_name, consumer_dev_name) != 0)
1776 } else if (node->dev_name || consumer_dev_name) {
1780 if (strcmp(node->supply, supply) != 0)
1783 pr_debug("%s: %s/%s is '%s' supply; fail %s/%s\n",
1785 dev_name(&node->regulator->dev),
1786 node->regulator->desc->name,
1788 dev_name(&rdev->dev), rdev_get_name(rdev));
1792 list_add(&new_node->list, ®ulator_map_list);
1793 mutex_unlock(®ulator_list_mutex);
1798 mutex_unlock(®ulator_list_mutex);
1799 kfree(new_node->dev_name);
1804 static void unset_regulator_supplies(struct regulator_dev *rdev)
1806 struct regulator_map *node, *n;
1808 list_for_each_entry_safe(node, n, ®ulator_map_list, list) {
1809 if (rdev == node->regulator) {
1810 list_del(&node->list);
1811 kfree(node->dev_name);
1817 #ifdef CONFIG_DEBUG_FS
1818 static ssize_t constraint_flags_read_file(struct file *file,
1819 char __user *user_buf,
1820 size_t count, loff_t *ppos)
1822 const struct regulator *regulator = file->private_data;
1823 const struct regulation_constraints *c = regulator->rdev->constraints;
1830 buf = kmalloc(PAGE_SIZE, GFP_KERNEL);
1834 ret = snprintf(buf, PAGE_SIZE,
1838 "ramp_disable: %u\n"
1841 "over_current_protection: %u\n",
1848 c->over_current_protection);
1850 ret = simple_read_from_buffer(user_buf, count, ppos, buf, ret);
1858 static const struct file_operations constraint_flags_fops = {
1859 #ifdef CONFIG_DEBUG_FS
1860 .open = simple_open,
1861 .read = constraint_flags_read_file,
1862 .llseek = default_llseek,
1866 #define REG_STR_SIZE 64
1868 static struct regulator *create_regulator(struct regulator_dev *rdev,
1870 const char *supply_name)
1872 struct regulator *regulator;
1875 lockdep_assert_held_once(&rdev->mutex.base);
1878 char buf[REG_STR_SIZE];
1881 size = snprintf(buf, REG_STR_SIZE, "%s-%s",
1882 dev->kobj.name, supply_name);
1883 if (size >= REG_STR_SIZE)
1886 supply_name = kstrdup(buf, GFP_KERNEL);
1887 if (supply_name == NULL)
1890 supply_name = kstrdup_const(supply_name, GFP_KERNEL);
1891 if (supply_name == NULL)
1895 regulator = kzalloc(sizeof(*regulator), GFP_KERNEL);
1896 if (regulator == NULL) {
1897 kfree_const(supply_name);
1901 regulator->rdev = rdev;
1902 regulator->supply_name = supply_name;
1904 list_add(®ulator->list, &rdev->consumer_list);
1907 regulator->dev = dev;
1909 /* Add a link to the device sysfs entry */
1910 err = sysfs_create_link_nowarn(&rdev->dev.kobj, &dev->kobj,
1913 rdev_dbg(rdev, "could not add device link %s: %pe\n",
1914 dev->kobj.name, ERR_PTR(err));
1920 regulator->debugfs = debugfs_create_dir(supply_name, rdev->debugfs);
1921 if (IS_ERR(regulator->debugfs))
1922 rdev_dbg(rdev, "Failed to create debugfs directory\n");
1924 debugfs_create_u32("uA_load", 0444, regulator->debugfs,
1925 ®ulator->uA_load);
1926 debugfs_create_u32("min_uV", 0444, regulator->debugfs,
1927 ®ulator->voltage[PM_SUSPEND_ON].min_uV);
1928 debugfs_create_u32("max_uV", 0444, regulator->debugfs,
1929 ®ulator->voltage[PM_SUSPEND_ON].max_uV);
1930 debugfs_create_file("constraint_flags", 0444, regulator->debugfs,
1931 regulator, &constraint_flags_fops);
1934 * Check now if the regulator is an always on regulator - if
1935 * it is then we don't need to do nearly so much work for
1936 * enable/disable calls.
1938 if (!regulator_ops_is_valid(rdev, REGULATOR_CHANGE_STATUS) &&
1939 _regulator_is_enabled(rdev))
1940 regulator->always_on = true;
1945 static int _regulator_get_enable_time(struct regulator_dev *rdev)
1947 if (rdev->constraints && rdev->constraints->enable_time)
1948 return rdev->constraints->enable_time;
1949 if (rdev->desc->ops->enable_time)
1950 return rdev->desc->ops->enable_time(rdev);
1951 return rdev->desc->enable_time;
1954 static struct regulator_supply_alias *regulator_find_supply_alias(
1955 struct device *dev, const char *supply)
1957 struct regulator_supply_alias *map;
1959 list_for_each_entry(map, ®ulator_supply_alias_list, list)
1960 if (map->src_dev == dev && strcmp(map->src_supply, supply) == 0)
1966 static void regulator_supply_alias(struct device **dev, const char **supply)
1968 struct regulator_supply_alias *map;
1970 map = regulator_find_supply_alias(*dev, *supply);
1972 dev_dbg(*dev, "Mapping supply %s to %s,%s\n",
1973 *supply, map->alias_supply,
1974 dev_name(map->alias_dev));
1975 *dev = map->alias_dev;
1976 *supply = map->alias_supply;
1980 static int regulator_match(struct device *dev, const void *data)
1982 struct regulator_dev *r = dev_to_rdev(dev);
1984 return strcmp(rdev_get_name(r), data) == 0;
1987 static struct regulator_dev *regulator_lookup_by_name(const char *name)
1991 dev = class_find_device(®ulator_class, NULL, name, regulator_match);
1993 return dev ? dev_to_rdev(dev) : NULL;
1997 * regulator_dev_lookup - lookup a regulator device.
1998 * @dev: device for regulator "consumer".
1999 * @supply: Supply name or regulator ID.
2001 * If successful, returns a struct regulator_dev that corresponds to the name
2002 * @supply and with the embedded struct device refcount incremented by one.
2003 * The refcount must be dropped by calling put_device().
2004 * On failure one of the following ERR-PTR-encoded values is returned:
2005 * -ENODEV if lookup fails permanently, -EPROBE_DEFER if lookup could succeed
2008 static struct regulator_dev *regulator_dev_lookup(struct device *dev,
2011 struct regulator_dev *r = NULL;
2012 struct device_node *node;
2013 struct regulator_map *map;
2014 const char *devname = NULL;
2016 regulator_supply_alias(&dev, &supply);
2018 /* first do a dt based lookup */
2019 if (dev && dev->of_node) {
2020 node = of_get_regulator(dev, supply);
2022 r = of_find_regulator_by_node(node);
2028 * We have a node, but there is no device.
2029 * assume it has not registered yet.
2031 return ERR_PTR(-EPROBE_DEFER);
2035 /* if not found, try doing it non-dt way */
2037 devname = dev_name(dev);
2039 mutex_lock(®ulator_list_mutex);
2040 list_for_each_entry(map, ®ulator_map_list, list) {
2041 /* If the mapping has a device set up it must match */
2042 if (map->dev_name &&
2043 (!devname || strcmp(map->dev_name, devname)))
2046 if (strcmp(map->supply, supply) == 0 &&
2047 get_device(&map->regulator->dev)) {
2052 mutex_unlock(®ulator_list_mutex);
2057 r = regulator_lookup_by_name(supply);
2061 return ERR_PTR(-ENODEV);
2064 static int regulator_resolve_supply(struct regulator_dev *rdev)
2066 struct regulator_dev *r;
2067 struct device *dev = rdev->dev.parent;
2068 struct ww_acquire_ctx ww_ctx;
2071 /* No supply to resolve? */
2072 if (!rdev->supply_name)
2075 /* Supply already resolved? (fast-path without locking contention) */
2079 r = regulator_dev_lookup(dev, rdev->supply_name);
2083 /* Did the lookup explicitly defer for us? */
2084 if (ret == -EPROBE_DEFER)
2087 if (have_full_constraints()) {
2088 r = dummy_regulator_rdev;
2089 get_device(&r->dev);
2091 dev_err(dev, "Failed to resolve %s-supply for %s\n",
2092 rdev->supply_name, rdev->desc->name);
2093 ret = -EPROBE_DEFER;
2099 dev_err(dev, "Supply for %s (%s) resolved to itself\n",
2100 rdev->desc->name, rdev->supply_name);
2101 if (!have_full_constraints()) {
2105 r = dummy_regulator_rdev;
2106 get_device(&r->dev);
2110 * If the supply's parent device is not the same as the
2111 * regulator's parent device, then ensure the parent device
2112 * is bound before we resolve the supply, in case the parent
2113 * device get probe deferred and unregisters the supply.
2115 if (r->dev.parent && r->dev.parent != rdev->dev.parent) {
2116 if (!device_is_bound(r->dev.parent)) {
2117 put_device(&r->dev);
2118 ret = -EPROBE_DEFER;
2123 /* Recursively resolve the supply of the supply */
2124 ret = regulator_resolve_supply(r);
2126 put_device(&r->dev);
2131 * Recheck rdev->supply with rdev->mutex lock held to avoid a race
2132 * between rdev->supply null check and setting rdev->supply in
2133 * set_supply() from concurrent tasks.
2135 regulator_lock_two(rdev, r, &ww_ctx);
2137 /* Supply just resolved by a concurrent task? */
2139 regulator_unlock_two(rdev, r, &ww_ctx);
2140 put_device(&r->dev);
2144 ret = set_supply(rdev, r);
2146 regulator_unlock_two(rdev, r, &ww_ctx);
2147 put_device(&r->dev);
2151 regulator_unlock_two(rdev, r, &ww_ctx);
2154 * In set_machine_constraints() we may have turned this regulator on
2155 * but we couldn't propagate to the supply if it hadn't been resolved
2158 if (rdev->use_count) {
2159 ret = regulator_enable(rdev->supply);
2161 _regulator_put(rdev->supply);
2162 rdev->supply = NULL;
2171 /* Internal regulator request function */
2172 struct regulator *_regulator_get(struct device *dev, const char *id,
2173 enum regulator_get_type get_type)
2175 struct regulator_dev *rdev;
2176 struct regulator *regulator;
2177 struct device_link *link;
2180 if (get_type >= MAX_GET_TYPE) {
2181 dev_err(dev, "invalid type %d in %s\n", get_type, __func__);
2182 return ERR_PTR(-EINVAL);
2186 pr_err("get() with no identifier\n");
2187 return ERR_PTR(-EINVAL);
2190 rdev = regulator_dev_lookup(dev, id);
2192 ret = PTR_ERR(rdev);
2195 * If regulator_dev_lookup() fails with error other
2196 * than -ENODEV our job here is done, we simply return it.
2199 return ERR_PTR(ret);
2201 if (!have_full_constraints()) {
2203 "incomplete constraints, dummy supplies not allowed\n");
2204 return ERR_PTR(-ENODEV);
2210 * Assume that a regulator is physically present and
2211 * enabled, even if it isn't hooked up, and just
2214 dev_warn(dev, "supply %s not found, using dummy regulator\n", id);
2215 rdev = dummy_regulator_rdev;
2216 get_device(&rdev->dev);
2221 "dummy supplies not allowed for exclusive requests\n");
2225 return ERR_PTR(-ENODEV);
2229 if (rdev->exclusive) {
2230 regulator = ERR_PTR(-EPERM);
2231 put_device(&rdev->dev);
2235 if (get_type == EXCLUSIVE_GET && rdev->open_count) {
2236 regulator = ERR_PTR(-EBUSY);
2237 put_device(&rdev->dev);
2241 mutex_lock(®ulator_list_mutex);
2242 ret = (rdev->coupling_desc.n_resolved != rdev->coupling_desc.n_coupled);
2243 mutex_unlock(®ulator_list_mutex);
2246 regulator = ERR_PTR(-EPROBE_DEFER);
2247 put_device(&rdev->dev);
2251 ret = regulator_resolve_supply(rdev);
2253 regulator = ERR_PTR(ret);
2254 put_device(&rdev->dev);
2258 if (!try_module_get(rdev->owner)) {
2259 regulator = ERR_PTR(-EPROBE_DEFER);
2260 put_device(&rdev->dev);
2264 regulator_lock(rdev);
2265 regulator = create_regulator(rdev, dev, id);
2266 regulator_unlock(rdev);
2267 if (regulator == NULL) {
2268 regulator = ERR_PTR(-ENOMEM);
2269 module_put(rdev->owner);
2270 put_device(&rdev->dev);
2275 if (get_type == EXCLUSIVE_GET) {
2276 rdev->exclusive = 1;
2278 ret = _regulator_is_enabled(rdev);
2280 rdev->use_count = 1;
2281 regulator->enable_count = 1;
2283 rdev->use_count = 0;
2284 regulator->enable_count = 0;
2288 link = device_link_add(dev, &rdev->dev, DL_FLAG_STATELESS);
2289 if (!IS_ERR_OR_NULL(link))
2290 regulator->device_link = true;
2296 * regulator_get - lookup and obtain a reference to a regulator.
2297 * @dev: device for regulator "consumer"
2298 * @id: Supply name or regulator ID.
2300 * Returns a struct regulator corresponding to the regulator producer,
2301 * or IS_ERR() condition containing errno.
2303 * Use of supply names configured via set_consumer_device_supply() is
2304 * strongly encouraged. It is recommended that the supply name used
2305 * should match the name used for the supply and/or the relevant
2306 * device pins in the datasheet.
2308 struct regulator *regulator_get(struct device *dev, const char *id)
2310 return _regulator_get(dev, id, NORMAL_GET);
2312 EXPORT_SYMBOL_GPL(regulator_get);
2315 * regulator_get_exclusive - obtain exclusive access to a regulator.
2316 * @dev: device for regulator "consumer"
2317 * @id: Supply name or regulator ID.
2319 * Returns a struct regulator corresponding to the regulator producer,
2320 * or IS_ERR() condition containing errno. Other consumers will be
2321 * unable to obtain this regulator while this reference is held and the
2322 * use count for the regulator will be initialised to reflect the current
2323 * state of the regulator.
2325 * This is intended for use by consumers which cannot tolerate shared
2326 * use of the regulator such as those which need to force the
2327 * regulator off for correct operation of the hardware they are
2330 * Use of supply names configured via set_consumer_device_supply() is
2331 * strongly encouraged. It is recommended that the supply name used
2332 * should match the name used for the supply and/or the relevant
2333 * device pins in the datasheet.
2335 struct regulator *regulator_get_exclusive(struct device *dev, const char *id)
2337 return _regulator_get(dev, id, EXCLUSIVE_GET);
2339 EXPORT_SYMBOL_GPL(regulator_get_exclusive);
2342 * regulator_get_optional - obtain optional access to a regulator.
2343 * @dev: device for regulator "consumer"
2344 * @id: Supply name or regulator ID.
2346 * Returns a struct regulator corresponding to the regulator producer,
2347 * or IS_ERR() condition containing errno.
2349 * This is intended for use by consumers for devices which can have
2350 * some supplies unconnected in normal use, such as some MMC devices.
2351 * It can allow the regulator core to provide stub supplies for other
2352 * supplies requested using normal regulator_get() calls without
2353 * disrupting the operation of drivers that can handle absent
2356 * Use of supply names configured via set_consumer_device_supply() is
2357 * strongly encouraged. It is recommended that the supply name used
2358 * should match the name used for the supply and/or the relevant
2359 * device pins in the datasheet.
2361 struct regulator *regulator_get_optional(struct device *dev, const char *id)
2363 return _regulator_get(dev, id, OPTIONAL_GET);
2365 EXPORT_SYMBOL_GPL(regulator_get_optional);
2367 static void destroy_regulator(struct regulator *regulator)
2369 struct regulator_dev *rdev = regulator->rdev;
2371 debugfs_remove_recursive(regulator->debugfs);
2373 if (regulator->dev) {
2374 if (regulator->device_link)
2375 device_link_remove(regulator->dev, &rdev->dev);
2377 /* remove any sysfs entries */
2378 sysfs_remove_link(&rdev->dev.kobj, regulator->supply_name);
2381 regulator_lock(rdev);
2382 list_del(®ulator->list);
2385 rdev->exclusive = 0;
2386 regulator_unlock(rdev);
2388 kfree_const(regulator->supply_name);
2392 /* regulator_list_mutex lock held by regulator_put() */
2393 static void _regulator_put(struct regulator *regulator)
2395 struct regulator_dev *rdev;
2397 if (IS_ERR_OR_NULL(regulator))
2400 lockdep_assert_held_once(®ulator_list_mutex);
2402 /* Docs say you must disable before calling regulator_put() */
2403 WARN_ON(regulator->enable_count);
2405 rdev = regulator->rdev;
2407 destroy_regulator(regulator);
2409 module_put(rdev->owner);
2410 put_device(&rdev->dev);
2414 * regulator_put - "free" the regulator source
2415 * @regulator: regulator source
2417 * Note: drivers must ensure that all regulator_enable calls made on this
2418 * regulator source are balanced by regulator_disable calls prior to calling
2421 void regulator_put(struct regulator *regulator)
2423 mutex_lock(®ulator_list_mutex);
2424 _regulator_put(regulator);
2425 mutex_unlock(®ulator_list_mutex);
2427 EXPORT_SYMBOL_GPL(regulator_put);
2430 * regulator_register_supply_alias - Provide device alias for supply lookup
2432 * @dev: device that will be given as the regulator "consumer"
2433 * @id: Supply name or regulator ID
2434 * @alias_dev: device that should be used to lookup the supply
2435 * @alias_id: Supply name or regulator ID that should be used to lookup the
2438 * All lookups for id on dev will instead be conducted for alias_id on
2441 int regulator_register_supply_alias(struct device *dev, const char *id,
2442 struct device *alias_dev,
2443 const char *alias_id)
2445 struct regulator_supply_alias *map;
2447 map = regulator_find_supply_alias(dev, id);
2451 map = kzalloc(sizeof(struct regulator_supply_alias), GFP_KERNEL);
2456 map->src_supply = id;
2457 map->alias_dev = alias_dev;
2458 map->alias_supply = alias_id;
2460 list_add(&map->list, ®ulator_supply_alias_list);
2462 pr_info("Adding alias for supply %s,%s -> %s,%s\n",
2463 id, dev_name(dev), alias_id, dev_name(alias_dev));
2467 EXPORT_SYMBOL_GPL(regulator_register_supply_alias);
2470 * regulator_unregister_supply_alias - Remove device alias
2472 * @dev: device that will be given as the regulator "consumer"
2473 * @id: Supply name or regulator ID
2475 * Remove a lookup alias if one exists for id on dev.
2477 void regulator_unregister_supply_alias(struct device *dev, const char *id)
2479 struct regulator_supply_alias *map;
2481 map = regulator_find_supply_alias(dev, id);
2483 list_del(&map->list);
2487 EXPORT_SYMBOL_GPL(regulator_unregister_supply_alias);
2490 * regulator_bulk_register_supply_alias - register multiple aliases
2492 * @dev: device that will be given as the regulator "consumer"
2493 * @id: List of supply names or regulator IDs
2494 * @alias_dev: device that should be used to lookup the supply
2495 * @alias_id: List of supply names or regulator IDs that should be used to
2497 * @num_id: Number of aliases to register
2499 * @return 0 on success, an errno on failure.
2501 * This helper function allows drivers to register several supply
2502 * aliases in one operation. If any of the aliases cannot be
2503 * registered any aliases that were registered will be removed
2504 * before returning to the caller.
2506 int regulator_bulk_register_supply_alias(struct device *dev,
2507 const char *const *id,
2508 struct device *alias_dev,
2509 const char *const *alias_id,
2515 for (i = 0; i < num_id; ++i) {
2516 ret = regulator_register_supply_alias(dev, id[i], alias_dev,
2526 "Failed to create supply alias %s,%s -> %s,%s\n",
2527 id[i], dev_name(dev), alias_id[i], dev_name(alias_dev));
2530 regulator_unregister_supply_alias(dev, id[i]);
2534 EXPORT_SYMBOL_GPL(regulator_bulk_register_supply_alias);
2537 * regulator_bulk_unregister_supply_alias - unregister multiple aliases
2539 * @dev: device that will be given as the regulator "consumer"
2540 * @id: List of supply names or regulator IDs
2541 * @num_id: Number of aliases to unregister
2543 * This helper function allows drivers to unregister several supply
2544 * aliases in one operation.
2546 void regulator_bulk_unregister_supply_alias(struct device *dev,
2547 const char *const *id,
2552 for (i = 0; i < num_id; ++i)
2553 regulator_unregister_supply_alias(dev, id[i]);
2555 EXPORT_SYMBOL_GPL(regulator_bulk_unregister_supply_alias);
2558 /* Manage enable GPIO list. Same GPIO pin can be shared among regulators */
2559 static int regulator_ena_gpio_request(struct regulator_dev *rdev,
2560 const struct regulator_config *config)
2562 struct regulator_enable_gpio *pin, *new_pin;
2563 struct gpio_desc *gpiod;
2565 gpiod = config->ena_gpiod;
2566 new_pin = kzalloc(sizeof(*new_pin), GFP_KERNEL);
2568 mutex_lock(®ulator_list_mutex);
2570 list_for_each_entry(pin, ®ulator_ena_gpio_list, list) {
2571 if (pin->gpiod == gpiod) {
2572 rdev_dbg(rdev, "GPIO is already used\n");
2573 goto update_ena_gpio_to_rdev;
2577 if (new_pin == NULL) {
2578 mutex_unlock(®ulator_list_mutex);
2586 list_add(&pin->list, ®ulator_ena_gpio_list);
2588 update_ena_gpio_to_rdev:
2589 pin->request_count++;
2590 rdev->ena_pin = pin;
2592 mutex_unlock(®ulator_list_mutex);
2598 static void regulator_ena_gpio_free(struct regulator_dev *rdev)
2600 struct regulator_enable_gpio *pin, *n;
2605 /* Free the GPIO only in case of no use */
2606 list_for_each_entry_safe(pin, n, ®ulator_ena_gpio_list, list) {
2607 if (pin != rdev->ena_pin)
2610 if (--pin->request_count)
2613 gpiod_put(pin->gpiod);
2614 list_del(&pin->list);
2619 rdev->ena_pin = NULL;
2623 * regulator_ena_gpio_ctrl - balance enable_count of each GPIO and actual GPIO pin control
2624 * @rdev: regulator_dev structure
2625 * @enable: enable GPIO at initial use?
2627 * GPIO is enabled in case of initial use. (enable_count is 0)
2628 * GPIO is disabled when it is not shared any more. (enable_count <= 1)
2630 static int regulator_ena_gpio_ctrl(struct regulator_dev *rdev, bool enable)
2632 struct regulator_enable_gpio *pin = rdev->ena_pin;
2638 /* Enable GPIO at initial use */
2639 if (pin->enable_count == 0)
2640 gpiod_set_value_cansleep(pin->gpiod, 1);
2642 pin->enable_count++;
2644 if (pin->enable_count > 1) {
2645 pin->enable_count--;
2649 /* Disable GPIO if not used */
2650 if (pin->enable_count <= 1) {
2651 gpiod_set_value_cansleep(pin->gpiod, 0);
2652 pin->enable_count = 0;
2660 * _regulator_delay_helper - a delay helper function
2661 * @delay: time to delay in microseconds
2663 * Delay for the requested amount of time as per the guidelines in:
2665 * Documentation/timers/timers-howto.rst
2667 * The assumption here is that these regulator operations will never used in
2668 * atomic context and therefore sleeping functions can be used.
2670 static void _regulator_delay_helper(unsigned int delay)
2672 unsigned int ms = delay / 1000;
2673 unsigned int us = delay % 1000;
2677 * For small enough values, handle super-millisecond
2678 * delays in the usleep_range() call below.
2687 * Give the scheduler some room to coalesce with any other
2688 * wakeup sources. For delays shorter than 10 us, don't even
2689 * bother setting up high-resolution timers and just busy-
2693 usleep_range(us, us + 100);
2699 * _regulator_check_status_enabled
2701 * A helper function to check if the regulator status can be interpreted
2702 * as 'regulator is enabled'.
2703 * @rdev: the regulator device to check
2706 * * 1 - if status shows regulator is in enabled state
2707 * * 0 - if not enabled state
2708 * * Error Value - as received from ops->get_status()
2710 static inline int _regulator_check_status_enabled(struct regulator_dev *rdev)
2712 int ret = rdev->desc->ops->get_status(rdev);
2715 rdev_info(rdev, "get_status returned error: %d\n", ret);
2720 case REGULATOR_STATUS_OFF:
2721 case REGULATOR_STATUS_ERROR:
2722 case REGULATOR_STATUS_UNDEFINED:
2729 static int _regulator_do_enable(struct regulator_dev *rdev)
2733 /* Query before enabling in case configuration dependent. */
2734 ret = _regulator_get_enable_time(rdev);
2738 rdev_warn(rdev, "enable_time() failed: %pe\n", ERR_PTR(ret));
2742 trace_regulator_enable(rdev_get_name(rdev));
2744 if (rdev->desc->off_on_delay) {
2745 /* if needed, keep a distance of off_on_delay from last time
2746 * this regulator was disabled.
2748 ktime_t end = ktime_add_us(rdev->last_off, rdev->desc->off_on_delay);
2749 s64 remaining = ktime_us_delta(end, ktime_get_boottime());
2752 _regulator_delay_helper(remaining);
2755 if (rdev->ena_pin) {
2756 if (!rdev->ena_gpio_state) {
2757 ret = regulator_ena_gpio_ctrl(rdev, true);
2760 rdev->ena_gpio_state = 1;
2762 } else if (rdev->desc->ops->enable) {
2763 ret = rdev->desc->ops->enable(rdev);
2770 /* Allow the regulator to ramp; it would be useful to extend
2771 * this for bulk operations so that the regulators can ramp
2774 trace_regulator_enable_delay(rdev_get_name(rdev));
2776 /* If poll_enabled_time is set, poll upto the delay calculated
2777 * above, delaying poll_enabled_time uS to check if the regulator
2778 * actually got enabled.
2779 * If the regulator isn't enabled after our delay helper has expired,
2780 * return -ETIMEDOUT.
2782 if (rdev->desc->poll_enabled_time) {
2783 int time_remaining = delay;
2785 while (time_remaining > 0) {
2786 _regulator_delay_helper(rdev->desc->poll_enabled_time);
2788 if (rdev->desc->ops->get_status) {
2789 ret = _regulator_check_status_enabled(rdev);
2794 } else if (rdev->desc->ops->is_enabled(rdev))
2797 time_remaining -= rdev->desc->poll_enabled_time;
2800 if (time_remaining <= 0) {
2801 rdev_err(rdev, "Enabled check timed out\n");
2805 _regulator_delay_helper(delay);
2808 trace_regulator_enable_complete(rdev_get_name(rdev));
2814 * _regulator_handle_consumer_enable - handle that a consumer enabled
2815 * @regulator: regulator source
2817 * Some things on a regulator consumer (like the contribution towards total
2818 * load on the regulator) only have an effect when the consumer wants the
2819 * regulator enabled. Explained in example with two consumers of the same
2821 * consumer A: set_load(100); => total load = 0
2822 * consumer A: regulator_enable(); => total load = 100
2823 * consumer B: set_load(1000); => total load = 100
2824 * consumer B: regulator_enable(); => total load = 1100
2825 * consumer A: regulator_disable(); => total_load = 1000
2827 * This function (together with _regulator_handle_consumer_disable) is
2828 * responsible for keeping track of the refcount for a given regulator consumer
2829 * and applying / unapplying these things.
2831 * Returns 0 upon no error; -error upon error.
2833 static int _regulator_handle_consumer_enable(struct regulator *regulator)
2836 struct regulator_dev *rdev = regulator->rdev;
2838 lockdep_assert_held_once(&rdev->mutex.base);
2840 regulator->enable_count++;
2841 if (regulator->uA_load && regulator->enable_count == 1) {
2842 ret = drms_uA_update(rdev);
2844 regulator->enable_count--;
2852 * _regulator_handle_consumer_disable - handle that a consumer disabled
2853 * @regulator: regulator source
2855 * The opposite of _regulator_handle_consumer_enable().
2857 * Returns 0 upon no error; -error upon error.
2859 static int _regulator_handle_consumer_disable(struct regulator *regulator)
2861 struct regulator_dev *rdev = regulator->rdev;
2863 lockdep_assert_held_once(&rdev->mutex.base);
2865 if (!regulator->enable_count) {
2866 rdev_err(rdev, "Underflow of regulator enable count\n");
2870 regulator->enable_count--;
2871 if (regulator->uA_load && regulator->enable_count == 0)
2872 return drms_uA_update(rdev);
2877 /* locks held by regulator_enable() */
2878 static int _regulator_enable(struct regulator *regulator)
2880 struct regulator_dev *rdev = regulator->rdev;
2883 lockdep_assert_held_once(&rdev->mutex.base);
2885 if (rdev->use_count == 0 && rdev->supply) {
2886 ret = _regulator_enable(rdev->supply);
2891 /* balance only if there are regulators coupled */
2892 if (rdev->coupling_desc.n_coupled > 1) {
2893 ret = regulator_balance_voltage(rdev, PM_SUSPEND_ON);
2895 goto err_disable_supply;
2898 ret = _regulator_handle_consumer_enable(regulator);
2900 goto err_disable_supply;
2902 if (rdev->use_count == 0) {
2904 * The regulator may already be enabled if it's not switchable
2907 ret = _regulator_is_enabled(rdev);
2908 if (ret == -EINVAL || ret == 0) {
2909 if (!regulator_ops_is_valid(rdev,
2910 REGULATOR_CHANGE_STATUS)) {
2912 goto err_consumer_disable;
2915 ret = _regulator_do_enable(rdev);
2917 goto err_consumer_disable;
2919 _notifier_call_chain(rdev, REGULATOR_EVENT_ENABLE,
2921 } else if (ret < 0) {
2922 rdev_err(rdev, "is_enabled() failed: %pe\n", ERR_PTR(ret));
2923 goto err_consumer_disable;
2925 /* Fallthrough on positive return values - already enabled */
2932 err_consumer_disable:
2933 _regulator_handle_consumer_disable(regulator);
2936 if (rdev->use_count == 0 && rdev->supply)
2937 _regulator_disable(rdev->supply);
2943 * regulator_enable - enable regulator output
2944 * @regulator: regulator source
2946 * Request that the regulator be enabled with the regulator output at
2947 * the predefined voltage or current value. Calls to regulator_enable()
2948 * must be balanced with calls to regulator_disable().
2950 * NOTE: the output value can be set by other drivers, boot loader or may be
2951 * hardwired in the regulator.
2953 int regulator_enable(struct regulator *regulator)
2955 struct regulator_dev *rdev = regulator->rdev;
2956 struct ww_acquire_ctx ww_ctx;
2959 regulator_lock_dependent(rdev, &ww_ctx);
2960 ret = _regulator_enable(regulator);
2961 regulator_unlock_dependent(rdev, &ww_ctx);
2965 EXPORT_SYMBOL_GPL(regulator_enable);
2967 static int _regulator_do_disable(struct regulator_dev *rdev)
2971 trace_regulator_disable(rdev_get_name(rdev));
2973 if (rdev->ena_pin) {
2974 if (rdev->ena_gpio_state) {
2975 ret = regulator_ena_gpio_ctrl(rdev, false);
2978 rdev->ena_gpio_state = 0;
2981 } else if (rdev->desc->ops->disable) {
2982 ret = rdev->desc->ops->disable(rdev);
2987 if (rdev->desc->off_on_delay)
2988 rdev->last_off = ktime_get_boottime();
2990 trace_regulator_disable_complete(rdev_get_name(rdev));
2995 /* locks held by regulator_disable() */
2996 static int _regulator_disable(struct regulator *regulator)
2998 struct regulator_dev *rdev = regulator->rdev;
3001 lockdep_assert_held_once(&rdev->mutex.base);
3003 if (WARN(rdev->use_count <= 0,
3004 "unbalanced disables for %s\n", rdev_get_name(rdev)))
3007 /* are we the last user and permitted to disable ? */
3008 if (rdev->use_count == 1 &&
3009 (rdev->constraints && !rdev->constraints->always_on)) {
3011 /* we are last user */
3012 if (regulator_ops_is_valid(rdev, REGULATOR_CHANGE_STATUS)) {
3013 ret = _notifier_call_chain(rdev,
3014 REGULATOR_EVENT_PRE_DISABLE,
3016 if (ret & NOTIFY_STOP_MASK)
3019 ret = _regulator_do_disable(rdev);
3021 rdev_err(rdev, "failed to disable: %pe\n", ERR_PTR(ret));
3022 _notifier_call_chain(rdev,
3023 REGULATOR_EVENT_ABORT_DISABLE,
3027 _notifier_call_chain(rdev, REGULATOR_EVENT_DISABLE,
3031 rdev->use_count = 0;
3032 } else if (rdev->use_count > 1) {
3037 ret = _regulator_handle_consumer_disable(regulator);
3039 if (ret == 0 && rdev->coupling_desc.n_coupled > 1)
3040 ret = regulator_balance_voltage(rdev, PM_SUSPEND_ON);
3042 if (ret == 0 && rdev->use_count == 0 && rdev->supply)
3043 ret = _regulator_disable(rdev->supply);
3049 * regulator_disable - disable regulator output
3050 * @regulator: regulator source
3052 * Disable the regulator output voltage or current. Calls to
3053 * regulator_enable() must be balanced with calls to
3054 * regulator_disable().
3056 * NOTE: this will only disable the regulator output if no other consumer
3057 * devices have it enabled, the regulator device supports disabling and
3058 * machine constraints permit this operation.
3060 int regulator_disable(struct regulator *regulator)
3062 struct regulator_dev *rdev = regulator->rdev;
3063 struct ww_acquire_ctx ww_ctx;
3066 regulator_lock_dependent(rdev, &ww_ctx);
3067 ret = _regulator_disable(regulator);
3068 regulator_unlock_dependent(rdev, &ww_ctx);
3072 EXPORT_SYMBOL_GPL(regulator_disable);
3074 /* locks held by regulator_force_disable() */
3075 static int _regulator_force_disable(struct regulator_dev *rdev)
3079 lockdep_assert_held_once(&rdev->mutex.base);
3081 ret = _notifier_call_chain(rdev, REGULATOR_EVENT_FORCE_DISABLE |
3082 REGULATOR_EVENT_PRE_DISABLE, NULL);
3083 if (ret & NOTIFY_STOP_MASK)
3086 ret = _regulator_do_disable(rdev);
3088 rdev_err(rdev, "failed to force disable: %pe\n", ERR_PTR(ret));
3089 _notifier_call_chain(rdev, REGULATOR_EVENT_FORCE_DISABLE |
3090 REGULATOR_EVENT_ABORT_DISABLE, NULL);
3094 _notifier_call_chain(rdev, REGULATOR_EVENT_FORCE_DISABLE |
3095 REGULATOR_EVENT_DISABLE, NULL);
3101 * regulator_force_disable - force disable regulator output
3102 * @regulator: regulator source
3104 * Forcibly disable the regulator output voltage or current.
3105 * NOTE: this *will* disable the regulator output even if other consumer
3106 * devices have it enabled. This should be used for situations when device
3107 * damage will likely occur if the regulator is not disabled (e.g. over temp).
3109 int regulator_force_disable(struct regulator *regulator)
3111 struct regulator_dev *rdev = regulator->rdev;
3112 struct ww_acquire_ctx ww_ctx;
3115 regulator_lock_dependent(rdev, &ww_ctx);
3117 ret = _regulator_force_disable(regulator->rdev);
3119 if (rdev->coupling_desc.n_coupled > 1)
3120 regulator_balance_voltage(rdev, PM_SUSPEND_ON);
3122 if (regulator->uA_load) {
3123 regulator->uA_load = 0;
3124 ret = drms_uA_update(rdev);
3127 if (rdev->use_count != 0 && rdev->supply)
3128 _regulator_disable(rdev->supply);
3130 regulator_unlock_dependent(rdev, &ww_ctx);
3134 EXPORT_SYMBOL_GPL(regulator_force_disable);
3136 static void regulator_disable_work(struct work_struct *work)
3138 struct regulator_dev *rdev = container_of(work, struct regulator_dev,
3140 struct ww_acquire_ctx ww_ctx;
3142 struct regulator *regulator;
3143 int total_count = 0;
3145 regulator_lock_dependent(rdev, &ww_ctx);
3148 * Workqueue functions queue the new work instance while the previous
3149 * work instance is being processed. Cancel the queued work instance
3150 * as the work instance under processing does the job of the queued
3153 cancel_delayed_work(&rdev->disable_work);
3155 list_for_each_entry(regulator, &rdev->consumer_list, list) {
3156 count = regulator->deferred_disables;
3161 total_count += count;
3162 regulator->deferred_disables = 0;
3164 for (i = 0; i < count; i++) {
3165 ret = _regulator_disable(regulator);
3167 rdev_err(rdev, "Deferred disable failed: %pe\n",
3171 WARN_ON(!total_count);
3173 if (rdev->coupling_desc.n_coupled > 1)
3174 regulator_balance_voltage(rdev, PM_SUSPEND_ON);
3176 regulator_unlock_dependent(rdev, &ww_ctx);
3180 * regulator_disable_deferred - disable regulator output with delay
3181 * @regulator: regulator source
3182 * @ms: milliseconds until the regulator is disabled
3184 * Execute regulator_disable() on the regulator after a delay. This
3185 * is intended for use with devices that require some time to quiesce.
3187 * NOTE: this will only disable the regulator output if no other consumer
3188 * devices have it enabled, the regulator device supports disabling and
3189 * machine constraints permit this operation.
3191 int regulator_disable_deferred(struct regulator *regulator, int ms)
3193 struct regulator_dev *rdev = regulator->rdev;
3196 return regulator_disable(regulator);
3198 regulator_lock(rdev);
3199 regulator->deferred_disables++;
3200 mod_delayed_work(system_power_efficient_wq, &rdev->disable_work,
3201 msecs_to_jiffies(ms));
3202 regulator_unlock(rdev);
3206 EXPORT_SYMBOL_GPL(regulator_disable_deferred);
3208 static int _regulator_is_enabled(struct regulator_dev *rdev)
3210 /* A GPIO control always takes precedence */
3212 return rdev->ena_gpio_state;
3214 /* If we don't know then assume that the regulator is always on */
3215 if (!rdev->desc->ops->is_enabled)
3218 return rdev->desc->ops->is_enabled(rdev);
3221 static int _regulator_list_voltage(struct regulator_dev *rdev,
3222 unsigned selector, int lock)
3224 const struct regulator_ops *ops = rdev->desc->ops;
3227 if (rdev->desc->fixed_uV && rdev->desc->n_voltages == 1 && !selector)
3228 return rdev->desc->fixed_uV;
3230 if (ops->list_voltage) {
3231 if (selector >= rdev->desc->n_voltages)
3233 if (selector < rdev->desc->linear_min_sel)
3236 regulator_lock(rdev);
3237 ret = ops->list_voltage(rdev, selector);
3239 regulator_unlock(rdev);
3240 } else if (rdev->is_switch && rdev->supply) {
3241 ret = _regulator_list_voltage(rdev->supply->rdev,
3248 if (ret < rdev->constraints->min_uV)
3250 else if (ret > rdev->constraints->max_uV)
3258 * regulator_is_enabled - is the regulator output enabled
3259 * @regulator: regulator source
3261 * Returns positive if the regulator driver backing the source/client
3262 * has requested that the device be enabled, zero if it hasn't, else a
3263 * negative errno code.
3265 * Note that the device backing this regulator handle can have multiple
3266 * users, so it might be enabled even if regulator_enable() was never
3267 * called for this particular source.
3269 int regulator_is_enabled(struct regulator *regulator)
3273 if (regulator->always_on)
3276 regulator_lock(regulator->rdev);
3277 ret = _regulator_is_enabled(regulator->rdev);
3278 regulator_unlock(regulator->rdev);
3282 EXPORT_SYMBOL_GPL(regulator_is_enabled);
3285 * regulator_count_voltages - count regulator_list_voltage() selectors
3286 * @regulator: regulator source
3288 * Returns number of selectors, or negative errno. Selectors are
3289 * numbered starting at zero, and typically correspond to bitfields
3290 * in hardware registers.
3292 int regulator_count_voltages(struct regulator *regulator)
3294 struct regulator_dev *rdev = regulator->rdev;
3296 if (rdev->desc->n_voltages)
3297 return rdev->desc->n_voltages;
3299 if (!rdev->is_switch || !rdev->supply)
3302 return regulator_count_voltages(rdev->supply);
3304 EXPORT_SYMBOL_GPL(regulator_count_voltages);
3307 * regulator_list_voltage - enumerate supported voltages
3308 * @regulator: regulator source
3309 * @selector: identify voltage to list
3310 * Context: can sleep
3312 * Returns a voltage that can be passed to @regulator_set_voltage(),
3313 * zero if this selector code can't be used on this system, or a
3316 int regulator_list_voltage(struct regulator *regulator, unsigned selector)
3318 return _regulator_list_voltage(regulator->rdev, selector, 1);
3320 EXPORT_SYMBOL_GPL(regulator_list_voltage);
3323 * regulator_get_regmap - get the regulator's register map
3324 * @regulator: regulator source
3326 * Returns the register map for the given regulator, or an ERR_PTR value
3327 * if the regulator doesn't use regmap.
3329 struct regmap *regulator_get_regmap(struct regulator *regulator)
3331 struct regmap *map = regulator->rdev->regmap;
3333 return map ? map : ERR_PTR(-EOPNOTSUPP);
3337 * regulator_get_hardware_vsel_register - get the HW voltage selector register
3338 * @regulator: regulator source
3339 * @vsel_reg: voltage selector register, output parameter
3340 * @vsel_mask: mask for voltage selector bitfield, output parameter
3342 * Returns the hardware register offset and bitmask used for setting the
3343 * regulator voltage. This might be useful when configuring voltage-scaling
3344 * hardware or firmware that can make I2C requests behind the kernel's back,
3347 * On success, the output parameters @vsel_reg and @vsel_mask are filled in
3348 * and 0 is returned, otherwise a negative errno is returned.
3350 int regulator_get_hardware_vsel_register(struct regulator *regulator,
3352 unsigned *vsel_mask)
3354 struct regulator_dev *rdev = regulator->rdev;
3355 const struct regulator_ops *ops = rdev->desc->ops;
3357 if (ops->set_voltage_sel != regulator_set_voltage_sel_regmap)
3360 *vsel_reg = rdev->desc->vsel_reg;
3361 *vsel_mask = rdev->desc->vsel_mask;
3365 EXPORT_SYMBOL_GPL(regulator_get_hardware_vsel_register);
3368 * regulator_list_hardware_vsel - get the HW-specific register value for a selector
3369 * @regulator: regulator source
3370 * @selector: identify voltage to list
3372 * Converts the selector to a hardware-specific voltage selector that can be
3373 * directly written to the regulator registers. The address of the voltage
3374 * register can be determined by calling @regulator_get_hardware_vsel_register.
3376 * On error a negative errno is returned.
3378 int regulator_list_hardware_vsel(struct regulator *regulator,
3381 struct regulator_dev *rdev = regulator->rdev;
3382 const struct regulator_ops *ops = rdev->desc->ops;
3384 if (selector >= rdev->desc->n_voltages)
3386 if (selector < rdev->desc->linear_min_sel)
3388 if (ops->set_voltage_sel != regulator_set_voltage_sel_regmap)
3393 EXPORT_SYMBOL_GPL(regulator_list_hardware_vsel);
3396 * regulator_get_linear_step - return the voltage step size between VSEL values
3397 * @regulator: regulator source
3399 * Returns the voltage step size between VSEL values for linear
3400 * regulators, or return 0 if the regulator isn't a linear regulator.
3402 unsigned int regulator_get_linear_step(struct regulator *regulator)
3404 struct regulator_dev *rdev = regulator->rdev;
3406 return rdev->desc->uV_step;
3408 EXPORT_SYMBOL_GPL(regulator_get_linear_step);
3411 * regulator_is_supported_voltage - check if a voltage range can be supported
3413 * @regulator: Regulator to check.
3414 * @min_uV: Minimum required voltage in uV.
3415 * @max_uV: Maximum required voltage in uV.
3417 * Returns a boolean.
3419 int regulator_is_supported_voltage(struct regulator *regulator,
3420 int min_uV, int max_uV)
3422 struct regulator_dev *rdev = regulator->rdev;
3423 int i, voltages, ret;
3425 /* If we can't change voltage check the current voltage */
3426 if (!regulator_ops_is_valid(rdev, REGULATOR_CHANGE_VOLTAGE)) {
3427 ret = regulator_get_voltage(regulator);
3429 return min_uV <= ret && ret <= max_uV;
3434 /* Any voltage within constrains range is fine? */
3435 if (rdev->desc->continuous_voltage_range)
3436 return min_uV >= rdev->constraints->min_uV &&
3437 max_uV <= rdev->constraints->max_uV;
3439 ret = regulator_count_voltages(regulator);
3444 for (i = 0; i < voltages; i++) {
3445 ret = regulator_list_voltage(regulator, i);
3447 if (ret >= min_uV && ret <= max_uV)
3453 EXPORT_SYMBOL_GPL(regulator_is_supported_voltage);
3455 static int regulator_map_voltage(struct regulator_dev *rdev, int min_uV,
3458 const struct regulator_desc *desc = rdev->desc;
3460 if (desc->ops->map_voltage)
3461 return desc->ops->map_voltage(rdev, min_uV, max_uV);
3463 if (desc->ops->list_voltage == regulator_list_voltage_linear)
3464 return regulator_map_voltage_linear(rdev, min_uV, max_uV);
3466 if (desc->ops->list_voltage == regulator_list_voltage_linear_range)
3467 return regulator_map_voltage_linear_range(rdev, min_uV, max_uV);
3469 if (desc->ops->list_voltage ==
3470 regulator_list_voltage_pickable_linear_range)
3471 return regulator_map_voltage_pickable_linear_range(rdev,
3474 return regulator_map_voltage_iterate(rdev, min_uV, max_uV);
3477 static int _regulator_call_set_voltage(struct regulator_dev *rdev,
3478 int min_uV, int max_uV,
3481 struct pre_voltage_change_data data;
3484 data.old_uV = regulator_get_voltage_rdev(rdev);
3485 data.min_uV = min_uV;
3486 data.max_uV = max_uV;
3487 ret = _notifier_call_chain(rdev, REGULATOR_EVENT_PRE_VOLTAGE_CHANGE,
3489 if (ret & NOTIFY_STOP_MASK)
3492 ret = rdev->desc->ops->set_voltage(rdev, min_uV, max_uV, selector);
3496 _notifier_call_chain(rdev, REGULATOR_EVENT_ABORT_VOLTAGE_CHANGE,
3497 (void *)data.old_uV);
3502 static int _regulator_call_set_voltage_sel(struct regulator_dev *rdev,
3503 int uV, unsigned selector)
3505 struct pre_voltage_change_data data;
3508 data.old_uV = regulator_get_voltage_rdev(rdev);
3511 ret = _notifier_call_chain(rdev, REGULATOR_EVENT_PRE_VOLTAGE_CHANGE,
3513 if (ret & NOTIFY_STOP_MASK)
3516 ret = rdev->desc->ops->set_voltage_sel(rdev, selector);
3520 _notifier_call_chain(rdev, REGULATOR_EVENT_ABORT_VOLTAGE_CHANGE,
3521 (void *)data.old_uV);
3526 static int _regulator_set_voltage_sel_step(struct regulator_dev *rdev,
3527 int uV, int new_selector)
3529 const struct regulator_ops *ops = rdev->desc->ops;
3530 int diff, old_sel, curr_sel, ret;
3532 /* Stepping is only needed if the regulator is enabled. */
3533 if (!_regulator_is_enabled(rdev))
3536 if (!ops->get_voltage_sel)
3539 old_sel = ops->get_voltage_sel(rdev);
3543 diff = new_selector - old_sel;
3545 return 0; /* No change needed. */
3549 for (curr_sel = old_sel + rdev->desc->vsel_step;
3550 curr_sel < new_selector;
3551 curr_sel += rdev->desc->vsel_step) {
3553 * Call the callback directly instead of using
3554 * _regulator_call_set_voltage_sel() as we don't
3555 * want to notify anyone yet. Same in the branch
3558 ret = ops->set_voltage_sel(rdev, curr_sel);
3563 /* Stepping down. */
3564 for (curr_sel = old_sel - rdev->desc->vsel_step;
3565 curr_sel > new_selector;
3566 curr_sel -= rdev->desc->vsel_step) {
3567 ret = ops->set_voltage_sel(rdev, curr_sel);
3574 /* The final selector will trigger the notifiers. */
3575 return _regulator_call_set_voltage_sel(rdev, uV, new_selector);
3579 * At least try to return to the previous voltage if setting a new
3582 (void)ops->set_voltage_sel(rdev, old_sel);
3586 static int _regulator_set_voltage_time(struct regulator_dev *rdev,
3587 int old_uV, int new_uV)
3589 unsigned int ramp_delay = 0;
3591 if (rdev->constraints->ramp_delay)
3592 ramp_delay = rdev->constraints->ramp_delay;
3593 else if (rdev->desc->ramp_delay)
3594 ramp_delay = rdev->desc->ramp_delay;
3595 else if (rdev->constraints->settling_time)
3596 return rdev->constraints->settling_time;
3597 else if (rdev->constraints->settling_time_up &&
3599 return rdev->constraints->settling_time_up;
3600 else if (rdev->constraints->settling_time_down &&
3602 return rdev->constraints->settling_time_down;
3604 if (ramp_delay == 0)
3607 return DIV_ROUND_UP(abs(new_uV - old_uV), ramp_delay);
3610 static int _regulator_do_set_voltage(struct regulator_dev *rdev,
3611 int min_uV, int max_uV)
3616 unsigned int selector;
3617 int old_selector = -1;
3618 const struct regulator_ops *ops = rdev->desc->ops;
3619 int old_uV = regulator_get_voltage_rdev(rdev);
3621 trace_regulator_set_voltage(rdev_get_name(rdev), min_uV, max_uV);
3623 min_uV += rdev->constraints->uV_offset;
3624 max_uV += rdev->constraints->uV_offset;
3627 * If we can't obtain the old selector there is not enough
3628 * info to call set_voltage_time_sel().
3630 if (_regulator_is_enabled(rdev) &&
3631 ops->set_voltage_time_sel && ops->get_voltage_sel) {
3632 old_selector = ops->get_voltage_sel(rdev);
3633 if (old_selector < 0)
3634 return old_selector;
3637 if (ops->set_voltage) {
3638 ret = _regulator_call_set_voltage(rdev, min_uV, max_uV,
3642 if (ops->list_voltage)
3643 best_val = ops->list_voltage(rdev,
3646 best_val = regulator_get_voltage_rdev(rdev);
3649 } else if (ops->set_voltage_sel) {
3650 ret = regulator_map_voltage(rdev, min_uV, max_uV);
3652 best_val = ops->list_voltage(rdev, ret);
3653 if (min_uV <= best_val && max_uV >= best_val) {
3655 if (old_selector == selector)
3657 else if (rdev->desc->vsel_step)
3658 ret = _regulator_set_voltage_sel_step(
3659 rdev, best_val, selector);
3661 ret = _regulator_call_set_voltage_sel(
3662 rdev, best_val, selector);
3674 if (ops->set_voltage_time_sel) {
3676 * Call set_voltage_time_sel if successfully obtained
3679 if (old_selector >= 0 && old_selector != selector)
3680 delay = ops->set_voltage_time_sel(rdev, old_selector,
3683 if (old_uV != best_val) {
3684 if (ops->set_voltage_time)
3685 delay = ops->set_voltage_time(rdev, old_uV,
3688 delay = _regulator_set_voltage_time(rdev,
3695 rdev_warn(rdev, "failed to get delay: %pe\n", ERR_PTR(delay));
3699 /* Insert any necessary delays */
3700 _regulator_delay_helper(delay);
3702 if (best_val >= 0) {
3703 unsigned long data = best_val;
3705 _notifier_call_chain(rdev, REGULATOR_EVENT_VOLTAGE_CHANGE,
3710 trace_regulator_set_voltage_complete(rdev_get_name(rdev), best_val);
3715 static int _regulator_do_set_suspend_voltage(struct regulator_dev *rdev,
3716 int min_uV, int max_uV, suspend_state_t state)
3718 struct regulator_state *rstate;
3721 rstate = regulator_get_suspend_state(rdev, state);
3725 if (min_uV < rstate->min_uV)
3726 min_uV = rstate->min_uV;
3727 if (max_uV > rstate->max_uV)
3728 max_uV = rstate->max_uV;
3730 sel = regulator_map_voltage(rdev, min_uV, max_uV);
3734 uV = rdev->desc->ops->list_voltage(rdev, sel);
3735 if (uV >= min_uV && uV <= max_uV)
3741 static int regulator_set_voltage_unlocked(struct regulator *regulator,
3742 int min_uV, int max_uV,
3743 suspend_state_t state)
3745 struct regulator_dev *rdev = regulator->rdev;
3746 struct regulator_voltage *voltage = ®ulator->voltage[state];
3748 int old_min_uV, old_max_uV;
3751 /* If we're setting the same range as last time the change
3752 * should be a noop (some cpufreq implementations use the same
3753 * voltage for multiple frequencies, for example).
3755 if (voltage->min_uV == min_uV && voltage->max_uV == max_uV)
3758 /* If we're trying to set a range that overlaps the current voltage,
3759 * return successfully even though the regulator does not support
3760 * changing the voltage.
3762 if (!regulator_ops_is_valid(rdev, REGULATOR_CHANGE_VOLTAGE)) {
3763 current_uV = regulator_get_voltage_rdev(rdev);
3764 if (min_uV <= current_uV && current_uV <= max_uV) {
3765 voltage->min_uV = min_uV;
3766 voltage->max_uV = max_uV;
3772 if (!rdev->desc->ops->set_voltage &&
3773 !rdev->desc->ops->set_voltage_sel) {
3778 /* constraints check */
3779 ret = regulator_check_voltage(rdev, &min_uV, &max_uV);
3783 /* restore original values in case of error */
3784 old_min_uV = voltage->min_uV;
3785 old_max_uV = voltage->max_uV;
3786 voltage->min_uV = min_uV;
3787 voltage->max_uV = max_uV;
3789 /* for not coupled regulators this will just set the voltage */
3790 ret = regulator_balance_voltage(rdev, state);
3792 voltage->min_uV = old_min_uV;
3793 voltage->max_uV = old_max_uV;
3800 int regulator_set_voltage_rdev(struct regulator_dev *rdev, int min_uV,
3801 int max_uV, suspend_state_t state)
3803 int best_supply_uV = 0;
3804 int supply_change_uV = 0;
3808 regulator_ops_is_valid(rdev->supply->rdev,
3809 REGULATOR_CHANGE_VOLTAGE) &&
3810 (rdev->desc->min_dropout_uV || !(rdev->desc->ops->get_voltage ||
3811 rdev->desc->ops->get_voltage_sel))) {
3812 int current_supply_uV;
3815 selector = regulator_map_voltage(rdev, min_uV, max_uV);
3821 best_supply_uV = _regulator_list_voltage(rdev, selector, 0);
3822 if (best_supply_uV < 0) {
3823 ret = best_supply_uV;
3827 best_supply_uV += rdev->desc->min_dropout_uV;
3829 current_supply_uV = regulator_get_voltage_rdev(rdev->supply->rdev);
3830 if (current_supply_uV < 0) {
3831 ret = current_supply_uV;
3835 supply_change_uV = best_supply_uV - current_supply_uV;
3838 if (supply_change_uV > 0) {
3839 ret = regulator_set_voltage_unlocked(rdev->supply,
3840 best_supply_uV, INT_MAX, state);
3842 dev_err(&rdev->dev, "Failed to increase supply voltage: %pe\n",
3848 if (state == PM_SUSPEND_ON)
3849 ret = _regulator_do_set_voltage(rdev, min_uV, max_uV);
3851 ret = _regulator_do_set_suspend_voltage(rdev, min_uV,
3856 if (supply_change_uV < 0) {
3857 ret = regulator_set_voltage_unlocked(rdev->supply,
3858 best_supply_uV, INT_MAX, state);
3860 dev_warn(&rdev->dev, "Failed to decrease supply voltage: %pe\n",
3862 /* No need to fail here */
3869 EXPORT_SYMBOL_GPL(regulator_set_voltage_rdev);
3871 static int regulator_limit_voltage_step(struct regulator_dev *rdev,
3872 int *current_uV, int *min_uV)
3874 struct regulation_constraints *constraints = rdev->constraints;
3876 /* Limit voltage change only if necessary */
3877 if (!constraints->max_uV_step || !_regulator_is_enabled(rdev))
3880 if (*current_uV < 0) {
3881 *current_uV = regulator_get_voltage_rdev(rdev);
3883 if (*current_uV < 0)
3887 if (abs(*current_uV - *min_uV) <= constraints->max_uV_step)
3890 /* Clamp target voltage within the given step */
3891 if (*current_uV < *min_uV)
3892 *min_uV = min(*current_uV + constraints->max_uV_step,
3895 *min_uV = max(*current_uV - constraints->max_uV_step,
3901 static int regulator_get_optimal_voltage(struct regulator_dev *rdev,
3903 int *min_uV, int *max_uV,
3904 suspend_state_t state,
3907 struct coupling_desc *c_desc = &rdev->coupling_desc;
3908 struct regulator_dev **c_rdevs = c_desc->coupled_rdevs;
3909 struct regulation_constraints *constraints = rdev->constraints;
3910 int desired_min_uV = 0, desired_max_uV = INT_MAX;
3911 int max_current_uV = 0, min_current_uV = INT_MAX;
3912 int highest_min_uV = 0, target_uV, possible_uV;
3913 int i, ret, max_spread;
3919 * If there are no coupled regulators, simply set the voltage
3920 * demanded by consumers.
3922 if (n_coupled == 1) {
3924 * If consumers don't provide any demands, set voltage
3927 desired_min_uV = constraints->min_uV;
3928 desired_max_uV = constraints->max_uV;
3930 ret = regulator_check_consumers(rdev,
3932 &desired_max_uV, state);
3936 possible_uV = desired_min_uV;
3942 /* Find highest min desired voltage */
3943 for (i = 0; i < n_coupled; i++) {
3945 int tmp_max = INT_MAX;
3947 lockdep_assert_held_once(&c_rdevs[i]->mutex.base);
3949 ret = regulator_check_consumers(c_rdevs[i],
3955 ret = regulator_check_voltage(c_rdevs[i], &tmp_min, &tmp_max);
3959 highest_min_uV = max(highest_min_uV, tmp_min);
3962 desired_min_uV = tmp_min;
3963 desired_max_uV = tmp_max;
3967 max_spread = constraints->max_spread[0];
3970 * Let target_uV be equal to the desired one if possible.
3971 * If not, set it to minimum voltage, allowed by other coupled
3974 target_uV = max(desired_min_uV, highest_min_uV - max_spread);
3977 * Find min and max voltages, which currently aren't violating
3980 for (i = 1; i < n_coupled; i++) {
3983 if (!_regulator_is_enabled(c_rdevs[i]))
3986 tmp_act = regulator_get_voltage_rdev(c_rdevs[i]);
3990 min_current_uV = min(tmp_act, min_current_uV);
3991 max_current_uV = max(tmp_act, max_current_uV);
3994 /* There aren't any other regulators enabled */
3995 if (max_current_uV == 0) {
3996 possible_uV = target_uV;
3999 * Correct target voltage, so as it currently isn't
4000 * violating max_spread
4002 possible_uV = max(target_uV, max_current_uV - max_spread);
4003 possible_uV = min(possible_uV, min_current_uV + max_spread);
4006 if (possible_uV > desired_max_uV)
4009 done = (possible_uV == target_uV);
4010 desired_min_uV = possible_uV;
4013 /* Apply max_uV_step constraint if necessary */
4014 if (state == PM_SUSPEND_ON) {
4015 ret = regulator_limit_voltage_step(rdev, current_uV,
4024 /* Set current_uV if wasn't done earlier in the code and if necessary */
4025 if (n_coupled > 1 && *current_uV == -1) {
4027 if (_regulator_is_enabled(rdev)) {
4028 ret = regulator_get_voltage_rdev(rdev);
4034 *current_uV = desired_min_uV;
4038 *min_uV = desired_min_uV;
4039 *max_uV = desired_max_uV;
4044 int regulator_do_balance_voltage(struct regulator_dev *rdev,
4045 suspend_state_t state, bool skip_coupled)
4047 struct regulator_dev **c_rdevs;
4048 struct regulator_dev *best_rdev;
4049 struct coupling_desc *c_desc = &rdev->coupling_desc;
4050 int i, ret, n_coupled, best_min_uV, best_max_uV, best_c_rdev;
4051 unsigned int delta, best_delta;
4052 unsigned long c_rdev_done = 0;
4053 bool best_c_rdev_done;
4055 c_rdevs = c_desc->coupled_rdevs;
4056 n_coupled = skip_coupled ? 1 : c_desc->n_coupled;
4059 * Find the best possible voltage change on each loop. Leave the loop
4060 * if there isn't any possible change.
4063 best_c_rdev_done = false;
4071 * Find highest difference between optimal voltage
4072 * and current voltage.
4074 for (i = 0; i < n_coupled; i++) {
4076 * optimal_uV is the best voltage that can be set for
4077 * i-th regulator at the moment without violating
4078 * max_spread constraint in order to balance
4079 * the coupled voltages.
4081 int optimal_uV = 0, optimal_max_uV = 0, current_uV = 0;
4083 if (test_bit(i, &c_rdev_done))
4086 ret = regulator_get_optimal_voltage(c_rdevs[i],
4094 delta = abs(optimal_uV - current_uV);
4096 if (delta && best_delta <= delta) {
4097 best_c_rdev_done = ret;
4099 best_rdev = c_rdevs[i];
4100 best_min_uV = optimal_uV;
4101 best_max_uV = optimal_max_uV;
4106 /* Nothing to change, return successfully */
4112 ret = regulator_set_voltage_rdev(best_rdev, best_min_uV,
4113 best_max_uV, state);
4118 if (best_c_rdev_done)
4119 set_bit(best_c_rdev, &c_rdev_done);
4121 } while (n_coupled > 1);
4127 static int regulator_balance_voltage(struct regulator_dev *rdev,
4128 suspend_state_t state)
4130 struct coupling_desc *c_desc = &rdev->coupling_desc;
4131 struct regulator_coupler *coupler = c_desc->coupler;
4132 bool skip_coupled = false;
4135 * If system is in a state other than PM_SUSPEND_ON, don't check
4136 * other coupled regulators.
4138 if (state != PM_SUSPEND_ON)
4139 skip_coupled = true;
4141 if (c_desc->n_resolved < c_desc->n_coupled) {
4142 rdev_err(rdev, "Not all coupled regulators registered\n");
4146 /* Invoke custom balancer for customized couplers */
4147 if (coupler && coupler->balance_voltage)
4148 return coupler->balance_voltage(coupler, rdev, state);
4150 return regulator_do_balance_voltage(rdev, state, skip_coupled);
4154 * regulator_set_voltage - set regulator output voltage
4155 * @regulator: regulator source
4156 * @min_uV: Minimum required voltage in uV
4157 * @max_uV: Maximum acceptable voltage in uV
4159 * Sets a voltage regulator to the desired output voltage. This can be set
4160 * during any regulator state. IOW, regulator can be disabled or enabled.
4162 * If the regulator is enabled then the voltage will change to the new value
4163 * immediately otherwise if the regulator is disabled the regulator will
4164 * output at the new voltage when enabled.
4166 * NOTE: If the regulator is shared between several devices then the lowest
4167 * request voltage that meets the system constraints will be used.
4168 * Regulator system constraints must be set for this regulator before
4169 * calling this function otherwise this call will fail.
4171 int regulator_set_voltage(struct regulator *regulator, int min_uV, int max_uV)
4173 struct ww_acquire_ctx ww_ctx;
4176 regulator_lock_dependent(regulator->rdev, &ww_ctx);
4178 ret = regulator_set_voltage_unlocked(regulator, min_uV, max_uV,
4181 regulator_unlock_dependent(regulator->rdev, &ww_ctx);
4185 EXPORT_SYMBOL_GPL(regulator_set_voltage);
4187 static inline int regulator_suspend_toggle(struct regulator_dev *rdev,
4188 suspend_state_t state, bool en)
4190 struct regulator_state *rstate;
4192 rstate = regulator_get_suspend_state(rdev, state);
4196 if (!rstate->changeable)
4199 rstate->enabled = (en) ? ENABLE_IN_SUSPEND : DISABLE_IN_SUSPEND;
4204 int regulator_suspend_enable(struct regulator_dev *rdev,
4205 suspend_state_t state)
4207 return regulator_suspend_toggle(rdev, state, true);
4209 EXPORT_SYMBOL_GPL(regulator_suspend_enable);
4211 int regulator_suspend_disable(struct regulator_dev *rdev,
4212 suspend_state_t state)
4214 struct regulator *regulator;
4215 struct regulator_voltage *voltage;
4218 * if any consumer wants this regulator device keeping on in
4219 * suspend states, don't set it as disabled.
4221 list_for_each_entry(regulator, &rdev->consumer_list, list) {
4222 voltage = ®ulator->voltage[state];
4223 if (voltage->min_uV || voltage->max_uV)
4227 return regulator_suspend_toggle(rdev, state, false);
4229 EXPORT_SYMBOL_GPL(regulator_suspend_disable);
4231 static int _regulator_set_suspend_voltage(struct regulator *regulator,
4232 int min_uV, int max_uV,
4233 suspend_state_t state)
4235 struct regulator_dev *rdev = regulator->rdev;
4236 struct regulator_state *rstate;
4238 rstate = regulator_get_suspend_state(rdev, state);
4242 if (rstate->min_uV == rstate->max_uV) {
4243 rdev_err(rdev, "The suspend voltage can't be changed!\n");
4247 return regulator_set_voltage_unlocked(regulator, min_uV, max_uV, state);
4250 int regulator_set_suspend_voltage(struct regulator *regulator, int min_uV,
4251 int max_uV, suspend_state_t state)
4253 struct ww_acquire_ctx ww_ctx;
4256 /* PM_SUSPEND_ON is handled by regulator_set_voltage() */
4257 if (regulator_check_states(state) || state == PM_SUSPEND_ON)
4260 regulator_lock_dependent(regulator->rdev, &ww_ctx);
4262 ret = _regulator_set_suspend_voltage(regulator, min_uV,
4265 regulator_unlock_dependent(regulator->rdev, &ww_ctx);
4269 EXPORT_SYMBOL_GPL(regulator_set_suspend_voltage);
4272 * regulator_set_voltage_time - get raise/fall time
4273 * @regulator: regulator source
4274 * @old_uV: starting voltage in microvolts
4275 * @new_uV: target voltage in microvolts
4277 * Provided with the starting and ending voltage, this function attempts to
4278 * calculate the time in microseconds required to rise or fall to this new
4281 int regulator_set_voltage_time(struct regulator *regulator,
4282 int old_uV, int new_uV)
4284 struct regulator_dev *rdev = regulator->rdev;
4285 const struct regulator_ops *ops = rdev->desc->ops;
4291 if (ops->set_voltage_time)
4292 return ops->set_voltage_time(rdev, old_uV, new_uV);
4293 else if (!ops->set_voltage_time_sel)
4294 return _regulator_set_voltage_time(rdev, old_uV, new_uV);
4296 /* Currently requires operations to do this */
4297 if (!ops->list_voltage || !rdev->desc->n_voltages)
4300 for (i = 0; i < rdev->desc->n_voltages; i++) {
4301 /* We only look for exact voltage matches here */
4302 if (i < rdev->desc->linear_min_sel)
4305 if (old_sel >= 0 && new_sel >= 0)
4308 voltage = regulator_list_voltage(regulator, i);
4313 if (voltage == old_uV)
4315 if (voltage == new_uV)
4319 if (old_sel < 0 || new_sel < 0)
4322 return ops->set_voltage_time_sel(rdev, old_sel, new_sel);
4324 EXPORT_SYMBOL_GPL(regulator_set_voltage_time);
4327 * regulator_set_voltage_time_sel - get raise/fall time
4328 * @rdev: regulator source device
4329 * @old_selector: selector for starting voltage
4330 * @new_selector: selector for target voltage
4332 * Provided with the starting and target voltage selectors, this function
4333 * returns time in microseconds required to rise or fall to this new voltage
4335 * Drivers providing ramp_delay in regulation_constraints can use this as their
4336 * set_voltage_time_sel() operation.
4338 int regulator_set_voltage_time_sel(struct regulator_dev *rdev,
4339 unsigned int old_selector,
4340 unsigned int new_selector)
4342 int old_volt, new_volt;
4345 if (!rdev->desc->ops->list_voltage)
4348 old_volt = rdev->desc->ops->list_voltage(rdev, old_selector);
4349 new_volt = rdev->desc->ops->list_voltage(rdev, new_selector);
4351 if (rdev->desc->ops->set_voltage_time)
4352 return rdev->desc->ops->set_voltage_time(rdev, old_volt,
4355 return _regulator_set_voltage_time(rdev, old_volt, new_volt);
4357 EXPORT_SYMBOL_GPL(regulator_set_voltage_time_sel);
4359 int regulator_sync_voltage_rdev(struct regulator_dev *rdev)
4363 regulator_lock(rdev);
4365 if (!rdev->desc->ops->set_voltage &&
4366 !rdev->desc->ops->set_voltage_sel) {
4371 /* balance only, if regulator is coupled */
4372 if (rdev->coupling_desc.n_coupled > 1)
4373 ret = regulator_balance_voltage(rdev, PM_SUSPEND_ON);
4378 regulator_unlock(rdev);
4383 * regulator_sync_voltage - re-apply last regulator output voltage
4384 * @regulator: regulator source
4386 * Re-apply the last configured voltage. This is intended to be used
4387 * where some external control source the consumer is cooperating with
4388 * has caused the configured voltage to change.
4390 int regulator_sync_voltage(struct regulator *regulator)
4392 struct regulator_dev *rdev = regulator->rdev;
4393 struct regulator_voltage *voltage = ®ulator->voltage[PM_SUSPEND_ON];
4394 int ret, min_uV, max_uV;
4396 if (!regulator_ops_is_valid(rdev, REGULATOR_CHANGE_VOLTAGE))
4399 regulator_lock(rdev);
4401 if (!rdev->desc->ops->set_voltage &&
4402 !rdev->desc->ops->set_voltage_sel) {
4407 /* This is only going to work if we've had a voltage configured. */
4408 if (!voltage->min_uV && !voltage->max_uV) {
4413 min_uV = voltage->min_uV;
4414 max_uV = voltage->max_uV;
4416 /* This should be a paranoia check... */
4417 ret = regulator_check_voltage(rdev, &min_uV, &max_uV);
4421 ret = regulator_check_consumers(rdev, &min_uV, &max_uV, 0);
4425 /* balance only, if regulator is coupled */
4426 if (rdev->coupling_desc.n_coupled > 1)
4427 ret = regulator_balance_voltage(rdev, PM_SUSPEND_ON);
4429 ret = _regulator_do_set_voltage(rdev, min_uV, max_uV);
4432 regulator_unlock(rdev);
4435 EXPORT_SYMBOL_GPL(regulator_sync_voltage);
4437 int regulator_get_voltage_rdev(struct regulator_dev *rdev)
4442 if (rdev->desc->ops->get_bypass) {
4443 ret = rdev->desc->ops->get_bypass(rdev, &bypassed);
4447 /* if bypassed the regulator must have a supply */
4448 if (!rdev->supply) {
4450 "bypassed regulator has no supply!\n");
4451 return -EPROBE_DEFER;
4454 return regulator_get_voltage_rdev(rdev->supply->rdev);
4458 if (rdev->desc->ops->get_voltage_sel) {
4459 sel = rdev->desc->ops->get_voltage_sel(rdev);
4462 ret = rdev->desc->ops->list_voltage(rdev, sel);
4463 } else if (rdev->desc->ops->get_voltage) {
4464 ret = rdev->desc->ops->get_voltage(rdev);
4465 } else if (rdev->desc->ops->list_voltage) {
4466 ret = rdev->desc->ops->list_voltage(rdev, 0);
4467 } else if (rdev->desc->fixed_uV && (rdev->desc->n_voltages == 1)) {
4468 ret = rdev->desc->fixed_uV;
4469 } else if (rdev->supply) {
4470 ret = regulator_get_voltage_rdev(rdev->supply->rdev);
4471 } else if (rdev->supply_name) {
4472 return -EPROBE_DEFER;
4479 return ret - rdev->constraints->uV_offset;
4481 EXPORT_SYMBOL_GPL(regulator_get_voltage_rdev);
4484 * regulator_get_voltage - get regulator output voltage
4485 * @regulator: regulator source
4487 * This returns the current regulator voltage in uV.
4489 * NOTE: If the regulator is disabled it will return the voltage value. This
4490 * function should not be used to determine regulator state.
4492 int regulator_get_voltage(struct regulator *regulator)
4494 struct ww_acquire_ctx ww_ctx;
4497 regulator_lock_dependent(regulator->rdev, &ww_ctx);
4498 ret = regulator_get_voltage_rdev(regulator->rdev);
4499 regulator_unlock_dependent(regulator->rdev, &ww_ctx);
4503 EXPORT_SYMBOL_GPL(regulator_get_voltage);
4506 * regulator_set_current_limit - set regulator output current limit
4507 * @regulator: regulator source
4508 * @min_uA: Minimum supported current in uA
4509 * @max_uA: Maximum supported current in uA
4511 * Sets current sink to the desired output current. This can be set during
4512 * any regulator state. IOW, regulator can be disabled or enabled.
4514 * If the regulator is enabled then the current will change to the new value
4515 * immediately otherwise if the regulator is disabled the regulator will
4516 * output at the new current when enabled.
4518 * NOTE: Regulator system constraints must be set for this regulator before
4519 * calling this function otherwise this call will fail.
4521 int regulator_set_current_limit(struct regulator *regulator,
4522 int min_uA, int max_uA)
4524 struct regulator_dev *rdev = regulator->rdev;
4527 regulator_lock(rdev);
4530 if (!rdev->desc->ops->set_current_limit) {
4535 /* constraints check */
4536 ret = regulator_check_current_limit(rdev, &min_uA, &max_uA);
4540 ret = rdev->desc->ops->set_current_limit(rdev, min_uA, max_uA);
4542 regulator_unlock(rdev);
4545 EXPORT_SYMBOL_GPL(regulator_set_current_limit);
4547 static int _regulator_get_current_limit_unlocked(struct regulator_dev *rdev)
4550 if (!rdev->desc->ops->get_current_limit)
4553 return rdev->desc->ops->get_current_limit(rdev);
4556 static int _regulator_get_current_limit(struct regulator_dev *rdev)
4560 regulator_lock(rdev);
4561 ret = _regulator_get_current_limit_unlocked(rdev);
4562 regulator_unlock(rdev);
4568 * regulator_get_current_limit - get regulator output current
4569 * @regulator: regulator source
4571 * This returns the current supplied by the specified current sink in uA.
4573 * NOTE: If the regulator is disabled it will return the current value. This
4574 * function should not be used to determine regulator state.
4576 int regulator_get_current_limit(struct regulator *regulator)
4578 return _regulator_get_current_limit(regulator->rdev);
4580 EXPORT_SYMBOL_GPL(regulator_get_current_limit);
4583 * regulator_set_mode - set regulator operating mode
4584 * @regulator: regulator source
4585 * @mode: operating mode - one of the REGULATOR_MODE constants
4587 * Set regulator operating mode to increase regulator efficiency or improve
4588 * regulation performance.
4590 * NOTE: Regulator system constraints must be set for this regulator before
4591 * calling this function otherwise this call will fail.
4593 int regulator_set_mode(struct regulator *regulator, unsigned int mode)
4595 struct regulator_dev *rdev = regulator->rdev;
4597 int regulator_curr_mode;
4599 regulator_lock(rdev);
4602 if (!rdev->desc->ops->set_mode) {
4607 /* return if the same mode is requested */
4608 if (rdev->desc->ops->get_mode) {
4609 regulator_curr_mode = rdev->desc->ops->get_mode(rdev);
4610 if (regulator_curr_mode == mode) {
4616 /* constraints check */
4617 ret = regulator_mode_constrain(rdev, &mode);
4621 ret = rdev->desc->ops->set_mode(rdev, mode);
4623 regulator_unlock(rdev);
4626 EXPORT_SYMBOL_GPL(regulator_set_mode);
4628 static unsigned int _regulator_get_mode_unlocked(struct regulator_dev *rdev)
4631 if (!rdev->desc->ops->get_mode)
4634 return rdev->desc->ops->get_mode(rdev);
4637 static unsigned int _regulator_get_mode(struct regulator_dev *rdev)
4641 regulator_lock(rdev);
4642 ret = _regulator_get_mode_unlocked(rdev);
4643 regulator_unlock(rdev);
4649 * regulator_get_mode - get regulator operating mode
4650 * @regulator: regulator source
4652 * Get the current regulator operating mode.
4654 unsigned int regulator_get_mode(struct regulator *regulator)
4656 return _regulator_get_mode(regulator->rdev);
4658 EXPORT_SYMBOL_GPL(regulator_get_mode);
4660 static int rdev_get_cached_err_flags(struct regulator_dev *rdev)
4664 if (rdev->use_cached_err) {
4665 spin_lock(&rdev->err_lock);
4666 ret = rdev->cached_err;
4667 spin_unlock(&rdev->err_lock);
4672 static int _regulator_get_error_flags(struct regulator_dev *rdev,
4673 unsigned int *flags)
4675 int cached_flags, ret = 0;
4677 regulator_lock(rdev);
4679 cached_flags = rdev_get_cached_err_flags(rdev);
4681 if (rdev->desc->ops->get_error_flags)
4682 ret = rdev->desc->ops->get_error_flags(rdev, flags);
4683 else if (!rdev->use_cached_err)
4686 *flags |= cached_flags;
4688 regulator_unlock(rdev);
4694 * regulator_get_error_flags - get regulator error information
4695 * @regulator: regulator source
4696 * @flags: pointer to store error flags
4698 * Get the current regulator error information.
4700 int regulator_get_error_flags(struct regulator *regulator,
4701 unsigned int *flags)
4703 return _regulator_get_error_flags(regulator->rdev, flags);
4705 EXPORT_SYMBOL_GPL(regulator_get_error_flags);
4708 * regulator_set_load - set regulator load
4709 * @regulator: regulator source
4710 * @uA_load: load current
4712 * Notifies the regulator core of a new device load. This is then used by
4713 * DRMS (if enabled by constraints) to set the most efficient regulator
4714 * operating mode for the new regulator loading.
4716 * Consumer devices notify their supply regulator of the maximum power
4717 * they will require (can be taken from device datasheet in the power
4718 * consumption tables) when they change operational status and hence power
4719 * state. Examples of operational state changes that can affect power
4720 * consumption are :-
4722 * o Device is opened / closed.
4723 * o Device I/O is about to begin or has just finished.
4724 * o Device is idling in between work.
4726 * This information is also exported via sysfs to userspace.
4728 * DRMS will sum the total requested load on the regulator and change
4729 * to the most efficient operating mode if platform constraints allow.
4731 * NOTE: when a regulator consumer requests to have a regulator
4732 * disabled then any load that consumer requested no longer counts
4733 * toward the total requested load. If the regulator is re-enabled
4734 * then the previously requested load will start counting again.
4736 * If a regulator is an always-on regulator then an individual consumer's
4737 * load will still be removed if that consumer is fully disabled.
4739 * On error a negative errno is returned.
4741 int regulator_set_load(struct regulator *regulator, int uA_load)
4743 struct regulator_dev *rdev = regulator->rdev;
4747 regulator_lock(rdev);
4748 old_uA_load = regulator->uA_load;
4749 regulator->uA_load = uA_load;
4750 if (regulator->enable_count && old_uA_load != uA_load) {
4751 ret = drms_uA_update(rdev);
4753 regulator->uA_load = old_uA_load;
4755 regulator_unlock(rdev);
4759 EXPORT_SYMBOL_GPL(regulator_set_load);
4762 * regulator_allow_bypass - allow the regulator to go into bypass mode
4764 * @regulator: Regulator to configure
4765 * @enable: enable or disable bypass mode
4767 * Allow the regulator to go into bypass mode if all other consumers
4768 * for the regulator also enable bypass mode and the machine
4769 * constraints allow this. Bypass mode means that the regulator is
4770 * simply passing the input directly to the output with no regulation.
4772 int regulator_allow_bypass(struct regulator *regulator, bool enable)
4774 struct regulator_dev *rdev = regulator->rdev;
4775 const char *name = rdev_get_name(rdev);
4778 if (!rdev->desc->ops->set_bypass)
4781 if (!regulator_ops_is_valid(rdev, REGULATOR_CHANGE_BYPASS))
4784 regulator_lock(rdev);
4786 if (enable && !regulator->bypass) {
4787 rdev->bypass_count++;
4789 if (rdev->bypass_count == rdev->open_count) {
4790 trace_regulator_bypass_enable(name);
4792 ret = rdev->desc->ops->set_bypass(rdev, enable);
4794 rdev->bypass_count--;
4796 trace_regulator_bypass_enable_complete(name);
4799 } else if (!enable && regulator->bypass) {
4800 rdev->bypass_count--;
4802 if (rdev->bypass_count != rdev->open_count) {
4803 trace_regulator_bypass_disable(name);
4805 ret = rdev->desc->ops->set_bypass(rdev, enable);
4807 rdev->bypass_count++;
4809 trace_regulator_bypass_disable_complete(name);
4814 regulator->bypass = enable;
4816 regulator_unlock(rdev);
4820 EXPORT_SYMBOL_GPL(regulator_allow_bypass);
4823 * regulator_register_notifier - register regulator event notifier
4824 * @regulator: regulator source
4825 * @nb: notifier block
4827 * Register notifier block to receive regulator events.
4829 int regulator_register_notifier(struct regulator *regulator,
4830 struct notifier_block *nb)
4832 return blocking_notifier_chain_register(®ulator->rdev->notifier,
4835 EXPORT_SYMBOL_GPL(regulator_register_notifier);
4838 * regulator_unregister_notifier - unregister regulator event notifier
4839 * @regulator: regulator source
4840 * @nb: notifier block
4842 * Unregister regulator event notifier block.
4844 int regulator_unregister_notifier(struct regulator *regulator,
4845 struct notifier_block *nb)
4847 return blocking_notifier_chain_unregister(®ulator->rdev->notifier,
4850 EXPORT_SYMBOL_GPL(regulator_unregister_notifier);
4852 /* notify regulator consumers and downstream regulator consumers.
4853 * Note mutex must be held by caller.
4855 static int _notifier_call_chain(struct regulator_dev *rdev,
4856 unsigned long event, void *data)
4858 /* call rdev chain first */
4859 return blocking_notifier_call_chain(&rdev->notifier, event, data);
4863 * regulator_bulk_get - get multiple regulator consumers
4865 * @dev: Device to supply
4866 * @num_consumers: Number of consumers to register
4867 * @consumers: Configuration of consumers; clients are stored here.
4869 * @return 0 on success, an errno on failure.
4871 * This helper function allows drivers to get several regulator
4872 * consumers in one operation. If any of the regulators cannot be
4873 * acquired then any regulators that were allocated will be freed
4874 * before returning to the caller.
4876 int regulator_bulk_get(struct device *dev, int num_consumers,
4877 struct regulator_bulk_data *consumers)
4882 for (i = 0; i < num_consumers; i++)
4883 consumers[i].consumer = NULL;
4885 for (i = 0; i < num_consumers; i++) {
4886 consumers[i].consumer = regulator_get(dev,
4887 consumers[i].supply);
4888 if (IS_ERR(consumers[i].consumer)) {
4889 ret = dev_err_probe(dev, PTR_ERR(consumers[i].consumer),
4890 "Failed to get supply '%s'",
4891 consumers[i].supply);
4892 consumers[i].consumer = NULL;
4896 if (consumers[i].init_load_uA > 0) {
4897 ret = regulator_set_load(consumers[i].consumer,
4898 consumers[i].init_load_uA);
4910 regulator_put(consumers[i].consumer);
4914 EXPORT_SYMBOL_GPL(regulator_bulk_get);
4916 static void regulator_bulk_enable_async(void *data, async_cookie_t cookie)
4918 struct regulator_bulk_data *bulk = data;
4920 bulk->ret = regulator_enable(bulk->consumer);
4924 * regulator_bulk_enable - enable multiple regulator consumers
4926 * @num_consumers: Number of consumers
4927 * @consumers: Consumer data; clients are stored here.
4928 * @return 0 on success, an errno on failure
4930 * This convenience API allows consumers to enable multiple regulator
4931 * clients in a single API call. If any consumers cannot be enabled
4932 * then any others that were enabled will be disabled again prior to
4935 int regulator_bulk_enable(int num_consumers,
4936 struct regulator_bulk_data *consumers)
4938 ASYNC_DOMAIN_EXCLUSIVE(async_domain);
4942 for (i = 0; i < num_consumers; i++) {
4943 async_schedule_domain(regulator_bulk_enable_async,
4944 &consumers[i], &async_domain);
4947 async_synchronize_full_domain(&async_domain);
4949 /* If any consumer failed we need to unwind any that succeeded */
4950 for (i = 0; i < num_consumers; i++) {
4951 if (consumers[i].ret != 0) {
4952 ret = consumers[i].ret;
4960 for (i = 0; i < num_consumers; i++) {
4961 if (consumers[i].ret < 0)
4962 pr_err("Failed to enable %s: %pe\n", consumers[i].supply,
4963 ERR_PTR(consumers[i].ret));
4965 regulator_disable(consumers[i].consumer);
4970 EXPORT_SYMBOL_GPL(regulator_bulk_enable);
4973 * regulator_bulk_disable - disable multiple regulator consumers
4975 * @num_consumers: Number of consumers
4976 * @consumers: Consumer data; clients are stored here.
4977 * @return 0 on success, an errno on failure
4979 * This convenience API allows consumers to disable multiple regulator
4980 * clients in a single API call. If any consumers cannot be disabled
4981 * then any others that were disabled will be enabled again prior to
4984 int regulator_bulk_disable(int num_consumers,
4985 struct regulator_bulk_data *consumers)
4990 for (i = num_consumers - 1; i >= 0; --i) {
4991 ret = regulator_disable(consumers[i].consumer);
4999 pr_err("Failed to disable %s: %pe\n", consumers[i].supply, ERR_PTR(ret));
5000 for (++i; i < num_consumers; ++i) {
5001 r = regulator_enable(consumers[i].consumer);
5003 pr_err("Failed to re-enable %s: %pe\n",
5004 consumers[i].supply, ERR_PTR(r));
5009 EXPORT_SYMBOL_GPL(regulator_bulk_disable);
5012 * regulator_bulk_force_disable - force disable multiple regulator consumers
5014 * @num_consumers: Number of consumers
5015 * @consumers: Consumer data; clients are stored here.
5016 * @return 0 on success, an errno on failure
5018 * This convenience API allows consumers to forcibly disable multiple regulator
5019 * clients in a single API call.
5020 * NOTE: This should be used for situations when device damage will
5021 * likely occur if the regulators are not disabled (e.g. over temp).
5022 * Although regulator_force_disable function call for some consumers can
5023 * return error numbers, the function is called for all consumers.
5025 int regulator_bulk_force_disable(int num_consumers,
5026 struct regulator_bulk_data *consumers)
5031 for (i = 0; i < num_consumers; i++) {
5033 regulator_force_disable(consumers[i].consumer);
5035 /* Store first error for reporting */
5036 if (consumers[i].ret && !ret)
5037 ret = consumers[i].ret;
5042 EXPORT_SYMBOL_GPL(regulator_bulk_force_disable);
5045 * regulator_bulk_free - free multiple regulator consumers
5047 * @num_consumers: Number of consumers
5048 * @consumers: Consumer data; clients are stored here.
5050 * This convenience API allows consumers to free multiple regulator
5051 * clients in a single API call.
5053 void regulator_bulk_free(int num_consumers,
5054 struct regulator_bulk_data *consumers)
5058 for (i = 0; i < num_consumers; i++) {
5059 regulator_put(consumers[i].consumer);
5060 consumers[i].consumer = NULL;
5063 EXPORT_SYMBOL_GPL(regulator_bulk_free);
5066 * regulator_notifier_call_chain - call regulator event notifier
5067 * @rdev: regulator source
5068 * @event: notifier block
5069 * @data: callback-specific data.
5071 * Called by regulator drivers to notify clients a regulator event has
5074 int regulator_notifier_call_chain(struct regulator_dev *rdev,
5075 unsigned long event, void *data)
5077 _notifier_call_chain(rdev, event, data);
5081 EXPORT_SYMBOL_GPL(regulator_notifier_call_chain);
5084 * regulator_mode_to_status - convert a regulator mode into a status
5086 * @mode: Mode to convert
5088 * Convert a regulator mode into a status.
5090 int regulator_mode_to_status(unsigned int mode)
5093 case REGULATOR_MODE_FAST:
5094 return REGULATOR_STATUS_FAST;
5095 case REGULATOR_MODE_NORMAL:
5096 return REGULATOR_STATUS_NORMAL;
5097 case REGULATOR_MODE_IDLE:
5098 return REGULATOR_STATUS_IDLE;
5099 case REGULATOR_MODE_STANDBY:
5100 return REGULATOR_STATUS_STANDBY;
5102 return REGULATOR_STATUS_UNDEFINED;
5105 EXPORT_SYMBOL_GPL(regulator_mode_to_status);
5107 static struct attribute *regulator_dev_attrs[] = {
5108 &dev_attr_name.attr,
5109 &dev_attr_num_users.attr,
5110 &dev_attr_type.attr,
5111 &dev_attr_microvolts.attr,
5112 &dev_attr_microamps.attr,
5113 &dev_attr_opmode.attr,
5114 &dev_attr_state.attr,
5115 &dev_attr_status.attr,
5116 &dev_attr_bypass.attr,
5117 &dev_attr_requested_microamps.attr,
5118 &dev_attr_min_microvolts.attr,
5119 &dev_attr_max_microvolts.attr,
5120 &dev_attr_min_microamps.attr,
5121 &dev_attr_max_microamps.attr,
5122 &dev_attr_under_voltage.attr,
5123 &dev_attr_over_current.attr,
5124 &dev_attr_regulation_out.attr,
5125 &dev_attr_fail.attr,
5126 &dev_attr_over_temp.attr,
5127 &dev_attr_under_voltage_warn.attr,
5128 &dev_attr_over_current_warn.attr,
5129 &dev_attr_over_voltage_warn.attr,
5130 &dev_attr_over_temp_warn.attr,
5131 &dev_attr_suspend_standby_state.attr,
5132 &dev_attr_suspend_mem_state.attr,
5133 &dev_attr_suspend_disk_state.attr,
5134 &dev_attr_suspend_standby_microvolts.attr,
5135 &dev_attr_suspend_mem_microvolts.attr,
5136 &dev_attr_suspend_disk_microvolts.attr,
5137 &dev_attr_suspend_standby_mode.attr,
5138 &dev_attr_suspend_mem_mode.attr,
5139 &dev_attr_suspend_disk_mode.attr,
5144 * To avoid cluttering sysfs (and memory) with useless state, only
5145 * create attributes that can be meaningfully displayed.
5147 static umode_t regulator_attr_is_visible(struct kobject *kobj,
5148 struct attribute *attr, int idx)
5150 struct device *dev = kobj_to_dev(kobj);
5151 struct regulator_dev *rdev = dev_to_rdev(dev);
5152 const struct regulator_ops *ops = rdev->desc->ops;
5153 umode_t mode = attr->mode;
5155 /* these three are always present */
5156 if (attr == &dev_attr_name.attr ||
5157 attr == &dev_attr_num_users.attr ||
5158 attr == &dev_attr_type.attr)
5161 /* some attributes need specific methods to be displayed */
5162 if (attr == &dev_attr_microvolts.attr) {
5163 if ((ops->get_voltage && ops->get_voltage(rdev) >= 0) ||
5164 (ops->get_voltage_sel && ops->get_voltage_sel(rdev) >= 0) ||
5165 (ops->list_voltage && ops->list_voltage(rdev, 0) >= 0) ||
5166 (rdev->desc->fixed_uV && rdev->desc->n_voltages == 1))
5171 if (attr == &dev_attr_microamps.attr)
5172 return ops->get_current_limit ? mode : 0;
5174 if (attr == &dev_attr_opmode.attr)
5175 return ops->get_mode ? mode : 0;
5177 if (attr == &dev_attr_state.attr)
5178 return (rdev->ena_pin || ops->is_enabled) ? mode : 0;
5180 if (attr == &dev_attr_status.attr)
5181 return ops->get_status ? mode : 0;
5183 if (attr == &dev_attr_bypass.attr)
5184 return ops->get_bypass ? mode : 0;
5186 if (attr == &dev_attr_under_voltage.attr ||
5187 attr == &dev_attr_over_current.attr ||
5188 attr == &dev_attr_regulation_out.attr ||
5189 attr == &dev_attr_fail.attr ||
5190 attr == &dev_attr_over_temp.attr ||
5191 attr == &dev_attr_under_voltage_warn.attr ||
5192 attr == &dev_attr_over_current_warn.attr ||
5193 attr == &dev_attr_over_voltage_warn.attr ||
5194 attr == &dev_attr_over_temp_warn.attr)
5195 return ops->get_error_flags ? mode : 0;
5197 /* constraints need specific supporting methods */
5198 if (attr == &dev_attr_min_microvolts.attr ||
5199 attr == &dev_attr_max_microvolts.attr)
5200 return (ops->set_voltage || ops->set_voltage_sel) ? mode : 0;
5202 if (attr == &dev_attr_min_microamps.attr ||
5203 attr == &dev_attr_max_microamps.attr)
5204 return ops->set_current_limit ? mode : 0;
5206 if (attr == &dev_attr_suspend_standby_state.attr ||
5207 attr == &dev_attr_suspend_mem_state.attr ||
5208 attr == &dev_attr_suspend_disk_state.attr)
5211 if (attr == &dev_attr_suspend_standby_microvolts.attr ||
5212 attr == &dev_attr_suspend_mem_microvolts.attr ||
5213 attr == &dev_attr_suspend_disk_microvolts.attr)
5214 return ops->set_suspend_voltage ? mode : 0;
5216 if (attr == &dev_attr_suspend_standby_mode.attr ||
5217 attr == &dev_attr_suspend_mem_mode.attr ||
5218 attr == &dev_attr_suspend_disk_mode.attr)
5219 return ops->set_suspend_mode ? mode : 0;
5224 static const struct attribute_group regulator_dev_group = {
5225 .attrs = regulator_dev_attrs,
5226 .is_visible = regulator_attr_is_visible,
5229 static const struct attribute_group *regulator_dev_groups[] = {
5230 ®ulator_dev_group,
5234 static void regulator_dev_release(struct device *dev)
5236 struct regulator_dev *rdev = dev_get_drvdata(dev);
5238 debugfs_remove_recursive(rdev->debugfs);
5239 kfree(rdev->constraints);
5240 of_node_put(rdev->dev.of_node);
5244 static void rdev_init_debugfs(struct regulator_dev *rdev)
5246 struct device *parent = rdev->dev.parent;
5247 const char *rname = rdev_get_name(rdev);
5248 char name[NAME_MAX];
5250 /* Avoid duplicate debugfs directory names */
5251 if (parent && rname == rdev->desc->name) {
5252 snprintf(name, sizeof(name), "%s-%s", dev_name(parent),
5257 rdev->debugfs = debugfs_create_dir(rname, debugfs_root);
5258 if (IS_ERR(rdev->debugfs))
5259 rdev_dbg(rdev, "Failed to create debugfs directory\n");
5261 debugfs_create_u32("use_count", 0444, rdev->debugfs,
5263 debugfs_create_u32("open_count", 0444, rdev->debugfs,
5265 debugfs_create_u32("bypass_count", 0444, rdev->debugfs,
5266 &rdev->bypass_count);
5269 static int regulator_register_resolve_supply(struct device *dev, void *data)
5271 struct regulator_dev *rdev = dev_to_rdev(dev);
5273 if (regulator_resolve_supply(rdev))
5274 rdev_dbg(rdev, "unable to resolve supply\n");
5279 int regulator_coupler_register(struct regulator_coupler *coupler)
5281 mutex_lock(®ulator_list_mutex);
5282 list_add_tail(&coupler->list, ®ulator_coupler_list);
5283 mutex_unlock(®ulator_list_mutex);
5288 static struct regulator_coupler *
5289 regulator_find_coupler(struct regulator_dev *rdev)
5291 struct regulator_coupler *coupler;
5295 * Note that regulators are appended to the list and the generic
5296 * coupler is registered first, hence it will be attached at last
5299 list_for_each_entry_reverse(coupler, ®ulator_coupler_list, list) {
5300 err = coupler->attach_regulator(coupler, rdev);
5302 if (!coupler->balance_voltage &&
5303 rdev->coupling_desc.n_coupled > 2)
5304 goto err_unsupported;
5310 return ERR_PTR(err);
5318 return ERR_PTR(-EINVAL);
5321 if (coupler->detach_regulator)
5322 coupler->detach_regulator(coupler, rdev);
5325 "Voltage balancing for multiple regulator couples is unimplemented\n");
5327 return ERR_PTR(-EPERM);
5330 static void regulator_resolve_coupling(struct regulator_dev *rdev)
5332 struct regulator_coupler *coupler = rdev->coupling_desc.coupler;
5333 struct coupling_desc *c_desc = &rdev->coupling_desc;
5334 int n_coupled = c_desc->n_coupled;
5335 struct regulator_dev *c_rdev;
5338 for (i = 1; i < n_coupled; i++) {
5339 /* already resolved */
5340 if (c_desc->coupled_rdevs[i])
5343 c_rdev = of_parse_coupled_regulator(rdev, i - 1);
5348 if (c_rdev->coupling_desc.coupler != coupler) {
5349 rdev_err(rdev, "coupler mismatch with %s\n",
5350 rdev_get_name(c_rdev));
5354 c_desc->coupled_rdevs[i] = c_rdev;
5355 c_desc->n_resolved++;
5357 regulator_resolve_coupling(c_rdev);
5361 static void regulator_remove_coupling(struct regulator_dev *rdev)
5363 struct regulator_coupler *coupler = rdev->coupling_desc.coupler;
5364 struct coupling_desc *__c_desc, *c_desc = &rdev->coupling_desc;
5365 struct regulator_dev *__c_rdev, *c_rdev;
5366 unsigned int __n_coupled, n_coupled;
5370 n_coupled = c_desc->n_coupled;
5372 for (i = 1; i < n_coupled; i++) {
5373 c_rdev = c_desc->coupled_rdevs[i];
5378 regulator_lock(c_rdev);
5380 __c_desc = &c_rdev->coupling_desc;
5381 __n_coupled = __c_desc->n_coupled;
5383 for (k = 1; k < __n_coupled; k++) {
5384 __c_rdev = __c_desc->coupled_rdevs[k];
5386 if (__c_rdev == rdev) {
5387 __c_desc->coupled_rdevs[k] = NULL;
5388 __c_desc->n_resolved--;
5393 regulator_unlock(c_rdev);
5395 c_desc->coupled_rdevs[i] = NULL;
5396 c_desc->n_resolved--;
5399 if (coupler && coupler->detach_regulator) {
5400 err = coupler->detach_regulator(coupler, rdev);
5402 rdev_err(rdev, "failed to detach from coupler: %pe\n",
5406 kfree(rdev->coupling_desc.coupled_rdevs);
5407 rdev->coupling_desc.coupled_rdevs = NULL;
5410 static int regulator_init_coupling(struct regulator_dev *rdev)
5412 struct regulator_dev **coupled;
5413 int err, n_phandles;
5415 if (!IS_ENABLED(CONFIG_OF))
5418 n_phandles = of_get_n_coupled(rdev);
5420 coupled = kcalloc(n_phandles + 1, sizeof(*coupled), GFP_KERNEL);
5424 rdev->coupling_desc.coupled_rdevs = coupled;
5427 * Every regulator should always have coupling descriptor filled with
5428 * at least pointer to itself.
5430 rdev->coupling_desc.coupled_rdevs[0] = rdev;
5431 rdev->coupling_desc.n_coupled = n_phandles + 1;
5432 rdev->coupling_desc.n_resolved++;
5434 /* regulator isn't coupled */
5435 if (n_phandles == 0)
5438 if (!of_check_coupling_data(rdev))
5441 mutex_lock(®ulator_list_mutex);
5442 rdev->coupling_desc.coupler = regulator_find_coupler(rdev);
5443 mutex_unlock(®ulator_list_mutex);
5445 if (IS_ERR(rdev->coupling_desc.coupler)) {
5446 err = PTR_ERR(rdev->coupling_desc.coupler);
5447 rdev_err(rdev, "failed to get coupler: %pe\n", ERR_PTR(err));
5454 static int generic_coupler_attach(struct regulator_coupler *coupler,
5455 struct regulator_dev *rdev)
5457 if (rdev->coupling_desc.n_coupled > 2) {
5459 "Voltage balancing for multiple regulator couples is unimplemented\n");
5463 if (!rdev->constraints->always_on) {
5465 "Coupling of a non always-on regulator is unimplemented\n");
5472 static struct regulator_coupler generic_regulator_coupler = {
5473 .attach_regulator = generic_coupler_attach,
5477 * regulator_register - register regulator
5478 * @dev: the device that drive the regulator
5479 * @regulator_desc: regulator to register
5480 * @cfg: runtime configuration for regulator
5482 * Called by regulator drivers to register a regulator.
5483 * Returns a valid pointer to struct regulator_dev on success
5484 * or an ERR_PTR() on error.
5486 struct regulator_dev *
5487 regulator_register(struct device *dev,
5488 const struct regulator_desc *regulator_desc,
5489 const struct regulator_config *cfg)
5491 const struct regulator_init_data *init_data;
5492 struct regulator_config *config = NULL;
5493 static atomic_t regulator_no = ATOMIC_INIT(-1);
5494 struct regulator_dev *rdev;
5495 bool dangling_cfg_gpiod = false;
5496 bool dangling_of_gpiod = false;
5498 bool resolved_early = false;
5501 return ERR_PTR(-EINVAL);
5503 dangling_cfg_gpiod = true;
5504 if (regulator_desc == NULL) {
5509 WARN_ON(!dev || !cfg->dev);
5511 if (regulator_desc->name == NULL || regulator_desc->ops == NULL) {
5516 if (regulator_desc->type != REGULATOR_VOLTAGE &&
5517 regulator_desc->type != REGULATOR_CURRENT) {
5522 /* Only one of each should be implemented */
5523 WARN_ON(regulator_desc->ops->get_voltage &&
5524 regulator_desc->ops->get_voltage_sel);
5525 WARN_ON(regulator_desc->ops->set_voltage &&
5526 regulator_desc->ops->set_voltage_sel);
5528 /* If we're using selectors we must implement list_voltage. */
5529 if (regulator_desc->ops->get_voltage_sel &&
5530 !regulator_desc->ops->list_voltage) {
5534 if (regulator_desc->ops->set_voltage_sel &&
5535 !regulator_desc->ops->list_voltage) {
5540 rdev = kzalloc(sizeof(struct regulator_dev), GFP_KERNEL);
5545 device_initialize(&rdev->dev);
5546 dev_set_drvdata(&rdev->dev, rdev);
5547 rdev->dev.class = ®ulator_class;
5548 spin_lock_init(&rdev->err_lock);
5551 * Duplicate the config so the driver could override it after
5552 * parsing init data.
5554 config = kmemdup(cfg, sizeof(*cfg), GFP_KERNEL);
5555 if (config == NULL) {
5560 init_data = regulator_of_get_init_data(dev, regulator_desc, config,
5561 &rdev->dev.of_node);
5564 * Sometimes not all resources are probed already so we need to take
5565 * that into account. This happens most the time if the ena_gpiod comes
5566 * from a gpio extender or something else.
5568 if (PTR_ERR(init_data) == -EPROBE_DEFER) {
5569 ret = -EPROBE_DEFER;
5574 * We need to keep track of any GPIO descriptor coming from the
5575 * device tree until we have handled it over to the core. If the
5576 * config that was passed in to this function DOES NOT contain
5577 * a descriptor, and the config after this call DOES contain
5578 * a descriptor, we definitely got one from parsing the device
5581 if (!cfg->ena_gpiod && config->ena_gpiod)
5582 dangling_of_gpiod = true;
5584 init_data = config->init_data;
5585 rdev->dev.of_node = of_node_get(config->of_node);
5588 ww_mutex_init(&rdev->mutex, ®ulator_ww_class);
5589 rdev->reg_data = config->driver_data;
5590 rdev->owner = regulator_desc->owner;
5591 rdev->desc = regulator_desc;
5593 rdev->regmap = config->regmap;
5594 else if (dev_get_regmap(dev, NULL))
5595 rdev->regmap = dev_get_regmap(dev, NULL);
5596 else if (dev->parent)
5597 rdev->regmap = dev_get_regmap(dev->parent, NULL);
5598 INIT_LIST_HEAD(&rdev->consumer_list);
5599 INIT_LIST_HEAD(&rdev->list);
5600 BLOCKING_INIT_NOTIFIER_HEAD(&rdev->notifier);
5601 INIT_DELAYED_WORK(&rdev->disable_work, regulator_disable_work);
5603 if (init_data && init_data->supply_regulator)
5604 rdev->supply_name = init_data->supply_regulator;
5605 else if (regulator_desc->supply_name)
5606 rdev->supply_name = regulator_desc->supply_name;
5608 /* register with sysfs */
5609 rdev->dev.parent = config->dev;
5610 dev_set_name(&rdev->dev, "regulator.%lu",
5611 (unsigned long) atomic_inc_return(®ulator_no));
5613 /* set regulator constraints */
5615 rdev->constraints = kmemdup(&init_data->constraints,
5616 sizeof(*rdev->constraints),
5619 rdev->constraints = kzalloc(sizeof(*rdev->constraints),
5621 if (!rdev->constraints) {
5626 if ((rdev->supply_name && !rdev->supply) &&
5627 (rdev->constraints->always_on ||
5628 rdev->constraints->boot_on)) {
5629 ret = regulator_resolve_supply(rdev);
5631 rdev_dbg(rdev, "unable to resolve supply early: %pe\n",
5634 resolved_early = true;
5637 /* perform any regulator specific init */
5638 if (init_data && init_data->regulator_init) {
5639 ret = init_data->regulator_init(rdev->reg_data);
5644 if (config->ena_gpiod) {
5645 ret = regulator_ena_gpio_request(rdev, config);
5647 rdev_err(rdev, "Failed to request enable GPIO: %pe\n",
5651 /* The regulator core took over the GPIO descriptor */
5652 dangling_cfg_gpiod = false;
5653 dangling_of_gpiod = false;
5656 ret = set_machine_constraints(rdev);
5657 if (ret == -EPROBE_DEFER && !resolved_early) {
5658 /* Regulator might be in bypass mode and so needs its supply
5659 * to set the constraints
5661 /* FIXME: this currently triggers a chicken-and-egg problem
5662 * when creating -SUPPLY symlink in sysfs to a regulator
5663 * that is just being created
5665 rdev_dbg(rdev, "will resolve supply early: %s\n",
5667 ret = regulator_resolve_supply(rdev);
5669 ret = set_machine_constraints(rdev);
5671 rdev_dbg(rdev, "unable to resolve supply early: %pe\n",
5677 ret = regulator_init_coupling(rdev);
5681 /* add consumers devices */
5683 for (i = 0; i < init_data->num_consumer_supplies; i++) {
5684 ret = set_consumer_device_supply(rdev,
5685 init_data->consumer_supplies[i].dev_name,
5686 init_data->consumer_supplies[i].supply);
5688 dev_err(dev, "Failed to set supply %s\n",
5689 init_data->consumer_supplies[i].supply);
5690 goto unset_supplies;
5695 if (!rdev->desc->ops->get_voltage &&
5696 !rdev->desc->ops->list_voltage &&
5697 !rdev->desc->fixed_uV)
5698 rdev->is_switch = true;
5700 ret = device_add(&rdev->dev);
5702 goto unset_supplies;
5704 rdev_init_debugfs(rdev);
5706 /* try to resolve regulators coupling since a new one was registered */
5707 mutex_lock(®ulator_list_mutex);
5708 regulator_resolve_coupling(rdev);
5709 mutex_unlock(®ulator_list_mutex);
5711 /* try to resolve regulators supply since a new one was registered */
5712 class_for_each_device(®ulator_class, NULL, NULL,
5713 regulator_register_resolve_supply);
5718 mutex_lock(®ulator_list_mutex);
5719 unset_regulator_supplies(rdev);
5720 regulator_remove_coupling(rdev);
5721 mutex_unlock(®ulator_list_mutex);
5723 regulator_put(rdev->supply);
5724 kfree(rdev->coupling_desc.coupled_rdevs);
5725 mutex_lock(®ulator_list_mutex);
5726 regulator_ena_gpio_free(rdev);
5727 mutex_unlock(®ulator_list_mutex);
5729 if (dangling_of_gpiod)
5730 gpiod_put(config->ena_gpiod);
5732 put_device(&rdev->dev);
5734 if (dangling_cfg_gpiod)
5735 gpiod_put(cfg->ena_gpiod);
5736 return ERR_PTR(ret);
5738 EXPORT_SYMBOL_GPL(regulator_register);
5741 * regulator_unregister - unregister regulator
5742 * @rdev: regulator to unregister
5744 * Called by regulator drivers to unregister a regulator.
5746 void regulator_unregister(struct regulator_dev *rdev)
5752 while (rdev->use_count--)
5753 regulator_disable(rdev->supply);
5754 regulator_put(rdev->supply);
5757 flush_work(&rdev->disable_work.work);
5759 mutex_lock(®ulator_list_mutex);
5761 WARN_ON(rdev->open_count);
5762 regulator_remove_coupling(rdev);
5763 unset_regulator_supplies(rdev);
5764 list_del(&rdev->list);
5765 regulator_ena_gpio_free(rdev);
5766 device_unregister(&rdev->dev);
5768 mutex_unlock(®ulator_list_mutex);
5770 EXPORT_SYMBOL_GPL(regulator_unregister);
5772 #ifdef CONFIG_SUSPEND
5774 * regulator_suspend - prepare regulators for system wide suspend
5775 * @dev: ``&struct device`` pointer that is passed to _regulator_suspend()
5777 * Configure each regulator with it's suspend operating parameters for state.
5779 static int regulator_suspend(struct device *dev)
5781 struct regulator_dev *rdev = dev_to_rdev(dev);
5782 suspend_state_t state = pm_suspend_target_state;
5784 const struct regulator_state *rstate;
5786 rstate = regulator_get_suspend_state_check(rdev, state);
5790 regulator_lock(rdev);
5791 ret = __suspend_set_state(rdev, rstate);
5792 regulator_unlock(rdev);
5797 static int regulator_resume(struct device *dev)
5799 suspend_state_t state = pm_suspend_target_state;
5800 struct regulator_dev *rdev = dev_to_rdev(dev);
5801 struct regulator_state *rstate;
5804 rstate = regulator_get_suspend_state(rdev, state);
5808 /* Avoid grabbing the lock if we don't need to */
5809 if (!rdev->desc->ops->resume)
5812 regulator_lock(rdev);
5814 if (rstate->enabled == ENABLE_IN_SUSPEND ||
5815 rstate->enabled == DISABLE_IN_SUSPEND)
5816 ret = rdev->desc->ops->resume(rdev);
5818 regulator_unlock(rdev);
5822 #else /* !CONFIG_SUSPEND */
5824 #define regulator_suspend NULL
5825 #define regulator_resume NULL
5827 #endif /* !CONFIG_SUSPEND */
5830 static const struct dev_pm_ops __maybe_unused regulator_pm_ops = {
5831 .suspend = regulator_suspend,
5832 .resume = regulator_resume,
5836 struct class regulator_class = {
5837 .name = "regulator",
5838 .dev_release = regulator_dev_release,
5839 .dev_groups = regulator_dev_groups,
5841 .pm = ®ulator_pm_ops,
5845 * regulator_has_full_constraints - the system has fully specified constraints
5847 * Calling this function will cause the regulator API to disable all
5848 * regulators which have a zero use count and don't have an always_on
5849 * constraint in a late_initcall.
5851 * The intention is that this will become the default behaviour in a
5852 * future kernel release so users are encouraged to use this facility
5855 void regulator_has_full_constraints(void)
5857 has_full_constraints = 1;
5859 EXPORT_SYMBOL_GPL(regulator_has_full_constraints);
5862 * rdev_get_drvdata - get rdev regulator driver data
5865 * Get rdev regulator driver private data. This call can be used in the
5866 * regulator driver context.
5868 void *rdev_get_drvdata(struct regulator_dev *rdev)
5870 return rdev->reg_data;
5872 EXPORT_SYMBOL_GPL(rdev_get_drvdata);
5875 * regulator_get_drvdata - get regulator driver data
5876 * @regulator: regulator
5878 * Get regulator driver private data. This call can be used in the consumer
5879 * driver context when non API regulator specific functions need to be called.
5881 void *regulator_get_drvdata(struct regulator *regulator)
5883 return regulator->rdev->reg_data;
5885 EXPORT_SYMBOL_GPL(regulator_get_drvdata);
5888 * regulator_set_drvdata - set regulator driver data
5889 * @regulator: regulator
5892 void regulator_set_drvdata(struct regulator *regulator, void *data)
5894 regulator->rdev->reg_data = data;
5896 EXPORT_SYMBOL_GPL(regulator_set_drvdata);
5899 * rdev_get_id - get regulator ID
5902 int rdev_get_id(struct regulator_dev *rdev)
5904 return rdev->desc->id;
5906 EXPORT_SYMBOL_GPL(rdev_get_id);
5908 struct device *rdev_get_dev(struct regulator_dev *rdev)
5912 EXPORT_SYMBOL_GPL(rdev_get_dev);
5914 struct regmap *rdev_get_regmap(struct regulator_dev *rdev)
5916 return rdev->regmap;
5918 EXPORT_SYMBOL_GPL(rdev_get_regmap);
5920 void *regulator_get_init_drvdata(struct regulator_init_data *reg_init_data)
5922 return reg_init_data->driver_data;
5924 EXPORT_SYMBOL_GPL(regulator_get_init_drvdata);
5926 #ifdef CONFIG_DEBUG_FS
5927 static int supply_map_show(struct seq_file *sf, void *data)
5929 struct regulator_map *map;
5931 list_for_each_entry(map, ®ulator_map_list, list) {
5932 seq_printf(sf, "%s -> %s.%s\n",
5933 rdev_get_name(map->regulator), map->dev_name,
5939 DEFINE_SHOW_ATTRIBUTE(supply_map);
5941 struct summary_data {
5943 struct regulator_dev *parent;
5947 static void regulator_summary_show_subtree(struct seq_file *s,
5948 struct regulator_dev *rdev,
5951 static int regulator_summary_show_children(struct device *dev, void *data)
5953 struct regulator_dev *rdev = dev_to_rdev(dev);
5954 struct summary_data *summary_data = data;
5956 if (rdev->supply && rdev->supply->rdev == summary_data->parent)
5957 regulator_summary_show_subtree(summary_data->s, rdev,
5958 summary_data->level + 1);
5963 static void regulator_summary_show_subtree(struct seq_file *s,
5964 struct regulator_dev *rdev,
5967 struct regulation_constraints *c;
5968 struct regulator *consumer;
5969 struct summary_data summary_data;
5970 unsigned int opmode;
5975 opmode = _regulator_get_mode_unlocked(rdev);
5976 seq_printf(s, "%*s%-*s %3d %4d %6d %7s ",
5978 30 - level * 3, rdev_get_name(rdev),
5979 rdev->use_count, rdev->open_count, rdev->bypass_count,
5980 regulator_opmode_to_str(opmode));
5982 seq_printf(s, "%5dmV ", regulator_get_voltage_rdev(rdev) / 1000);
5983 seq_printf(s, "%5dmA ",
5984 _regulator_get_current_limit_unlocked(rdev) / 1000);
5986 c = rdev->constraints;
5988 switch (rdev->desc->type) {
5989 case REGULATOR_VOLTAGE:
5990 seq_printf(s, "%5dmV %5dmV ",
5991 c->min_uV / 1000, c->max_uV / 1000);
5993 case REGULATOR_CURRENT:
5994 seq_printf(s, "%5dmA %5dmA ",
5995 c->min_uA / 1000, c->max_uA / 1000);
6002 list_for_each_entry(consumer, &rdev->consumer_list, list) {
6003 if (consumer->dev && consumer->dev->class == ®ulator_class)
6006 seq_printf(s, "%*s%-*s ",
6007 (level + 1) * 3 + 1, "",
6008 30 - (level + 1) * 3,
6009 consumer->supply_name ? consumer->supply_name :
6010 consumer->dev ? dev_name(consumer->dev) : "deviceless");
6012 switch (rdev->desc->type) {
6013 case REGULATOR_VOLTAGE:
6014 seq_printf(s, "%3d %33dmA%c%5dmV %5dmV",
6015 consumer->enable_count,
6016 consumer->uA_load / 1000,
6017 consumer->uA_load && !consumer->enable_count ?
6019 consumer->voltage[PM_SUSPEND_ON].min_uV / 1000,
6020 consumer->voltage[PM_SUSPEND_ON].max_uV / 1000);
6022 case REGULATOR_CURRENT:
6030 summary_data.level = level;
6031 summary_data.parent = rdev;
6033 class_for_each_device(®ulator_class, NULL, &summary_data,
6034 regulator_summary_show_children);
6037 struct summary_lock_data {
6038 struct ww_acquire_ctx *ww_ctx;
6039 struct regulator_dev **new_contended_rdev;
6040 struct regulator_dev **old_contended_rdev;
6043 static int regulator_summary_lock_one(struct device *dev, void *data)
6045 struct regulator_dev *rdev = dev_to_rdev(dev);
6046 struct summary_lock_data *lock_data = data;
6049 if (rdev != *lock_data->old_contended_rdev) {
6050 ret = regulator_lock_nested(rdev, lock_data->ww_ctx);
6052 if (ret == -EDEADLK)
6053 *lock_data->new_contended_rdev = rdev;
6057 *lock_data->old_contended_rdev = NULL;
6063 static int regulator_summary_unlock_one(struct device *dev, void *data)
6065 struct regulator_dev *rdev = dev_to_rdev(dev);
6066 struct summary_lock_data *lock_data = data;
6069 if (rdev == *lock_data->new_contended_rdev)
6073 regulator_unlock(rdev);
6078 static int regulator_summary_lock_all(struct ww_acquire_ctx *ww_ctx,
6079 struct regulator_dev **new_contended_rdev,
6080 struct regulator_dev **old_contended_rdev)
6082 struct summary_lock_data lock_data;
6085 lock_data.ww_ctx = ww_ctx;
6086 lock_data.new_contended_rdev = new_contended_rdev;
6087 lock_data.old_contended_rdev = old_contended_rdev;
6089 ret = class_for_each_device(®ulator_class, NULL, &lock_data,
6090 regulator_summary_lock_one);
6092 class_for_each_device(®ulator_class, NULL, &lock_data,
6093 regulator_summary_unlock_one);
6098 static void regulator_summary_lock(struct ww_acquire_ctx *ww_ctx)
6100 struct regulator_dev *new_contended_rdev = NULL;
6101 struct regulator_dev *old_contended_rdev = NULL;
6104 mutex_lock(®ulator_list_mutex);
6106 ww_acquire_init(ww_ctx, ®ulator_ww_class);
6109 if (new_contended_rdev) {
6110 ww_mutex_lock_slow(&new_contended_rdev->mutex, ww_ctx);
6111 old_contended_rdev = new_contended_rdev;
6112 old_contended_rdev->ref_cnt++;
6113 old_contended_rdev->mutex_owner = current;
6116 err = regulator_summary_lock_all(ww_ctx,
6117 &new_contended_rdev,
6118 &old_contended_rdev);
6120 if (old_contended_rdev)
6121 regulator_unlock(old_contended_rdev);
6123 } while (err == -EDEADLK);
6125 ww_acquire_done(ww_ctx);
6128 static void regulator_summary_unlock(struct ww_acquire_ctx *ww_ctx)
6130 class_for_each_device(®ulator_class, NULL, NULL,
6131 regulator_summary_unlock_one);
6132 ww_acquire_fini(ww_ctx);
6134 mutex_unlock(®ulator_list_mutex);
6137 static int regulator_summary_show_roots(struct device *dev, void *data)
6139 struct regulator_dev *rdev = dev_to_rdev(dev);
6140 struct seq_file *s = data;
6143 regulator_summary_show_subtree(s, rdev, 0);
6148 static int regulator_summary_show(struct seq_file *s, void *data)
6150 struct ww_acquire_ctx ww_ctx;
6152 seq_puts(s, " regulator use open bypass opmode voltage current min max\n");
6153 seq_puts(s, "---------------------------------------------------------------------------------------\n");
6155 regulator_summary_lock(&ww_ctx);
6157 class_for_each_device(®ulator_class, NULL, s,
6158 regulator_summary_show_roots);
6160 regulator_summary_unlock(&ww_ctx);
6164 DEFINE_SHOW_ATTRIBUTE(regulator_summary);
6165 #endif /* CONFIG_DEBUG_FS */
6167 static int __init regulator_init(void)
6171 ret = class_register(®ulator_class);
6173 debugfs_root = debugfs_create_dir("regulator", NULL);
6174 if (IS_ERR(debugfs_root))
6175 pr_debug("regulator: Failed to create debugfs directory\n");
6177 #ifdef CONFIG_DEBUG_FS
6178 debugfs_create_file("supply_map", 0444, debugfs_root, NULL,
6181 debugfs_create_file("regulator_summary", 0444, debugfs_root,
6182 NULL, ®ulator_summary_fops);
6184 regulator_dummy_init();
6186 regulator_coupler_register(&generic_regulator_coupler);
6191 /* init early to allow our consumers to complete system booting */
6192 core_initcall(regulator_init);
6194 static int regulator_late_cleanup(struct device *dev, void *data)
6196 struct regulator_dev *rdev = dev_to_rdev(dev);
6197 struct regulation_constraints *c = rdev->constraints;
6200 if (c && c->always_on)
6203 if (!regulator_ops_is_valid(rdev, REGULATOR_CHANGE_STATUS))
6206 regulator_lock(rdev);
6208 if (rdev->use_count)
6211 /* If reading the status failed, assume that it's off. */
6212 if (_regulator_is_enabled(rdev) <= 0)
6215 if (have_full_constraints()) {
6216 /* We log since this may kill the system if it goes
6219 rdev_info(rdev, "disabling\n");
6220 ret = _regulator_do_disable(rdev);
6222 rdev_err(rdev, "couldn't disable: %pe\n", ERR_PTR(ret));
6224 /* The intention is that in future we will
6225 * assume that full constraints are provided
6226 * so warn even if we aren't going to do
6229 rdev_warn(rdev, "incomplete constraints, leaving on\n");
6233 regulator_unlock(rdev);
6238 static void regulator_init_complete_work_function(struct work_struct *work)
6241 * Regulators may had failed to resolve their input supplies
6242 * when were registered, either because the input supply was
6243 * not registered yet or because its parent device was not
6244 * bound yet. So attempt to resolve the input supplies for
6245 * pending regulators before trying to disable unused ones.
6247 class_for_each_device(®ulator_class, NULL, NULL,
6248 regulator_register_resolve_supply);
6250 /* If we have a full configuration then disable any regulators
6251 * we have permission to change the status for and which are
6252 * not in use or always_on. This is effectively the default
6253 * for DT and ACPI as they have full constraints.
6255 class_for_each_device(®ulator_class, NULL, NULL,
6256 regulator_late_cleanup);
6259 static DECLARE_DELAYED_WORK(regulator_init_complete_work,
6260 regulator_init_complete_work_function);
6262 static int __init regulator_init_complete(void)
6265 * Since DT doesn't provide an idiomatic mechanism for
6266 * enabling full constraints and since it's much more natural
6267 * with DT to provide them just assume that a DT enabled
6268 * system has full constraints.
6270 if (of_have_populated_dt())
6271 has_full_constraints = true;
6274 * We punt completion for an arbitrary amount of time since
6275 * systems like distros will load many drivers from userspace
6276 * so consumers might not always be ready yet, this is
6277 * particularly an issue with laptops where this might bounce
6278 * the display off then on. Ideally we'd get a notification
6279 * from userspace when this happens but we don't so just wait
6280 * a bit and hope we waited long enough. It'd be better if
6281 * we'd only do this on systems that need it, and a kernel
6282 * command line option might be useful.
6284 schedule_delayed_work(®ulator_init_complete_work,
6285 msecs_to_jiffies(30000));
6289 late_initcall_sync(regulator_init_complete);