1 // SPDX-License-Identifier: GPL-2.0-or-later
3 // core.c -- Voltage/Current Regulator framework.
5 // Copyright 2007, 2008 Wolfson Microelectronics PLC.
6 // Copyright 2008 SlimLogic Ltd.
8 // Author: Liam Girdwood <lrg@slimlogic.co.uk>
10 #include <linux/kernel.h>
11 #include <linux/init.h>
12 #include <linux/debugfs.h>
13 #include <linux/device.h>
14 #include <linux/slab.h>
15 #include <linux/async.h>
16 #include <linux/err.h>
17 #include <linux/mutex.h>
18 #include <linux/suspend.h>
19 #include <linux/delay.h>
20 #include <linux/gpio/consumer.h>
22 #include <linux/regmap.h>
23 #include <linux/regulator/of_regulator.h>
24 #include <linux/regulator/consumer.h>
25 #include <linux/regulator/coupler.h>
26 #include <linux/regulator/driver.h>
27 #include <linux/regulator/machine.h>
28 #include <linux/module.h>
30 #define CREATE_TRACE_POINTS
31 #include <trace/events/regulator.h>
36 #define rdev_crit(rdev, fmt, ...) \
37 pr_crit("%s: " fmt, rdev_get_name(rdev), ##__VA_ARGS__)
38 #define rdev_err(rdev, fmt, ...) \
39 pr_err("%s: " fmt, rdev_get_name(rdev), ##__VA_ARGS__)
40 #define rdev_warn(rdev, fmt, ...) \
41 pr_warn("%s: " fmt, rdev_get_name(rdev), ##__VA_ARGS__)
42 #define rdev_info(rdev, fmt, ...) \
43 pr_info("%s: " fmt, rdev_get_name(rdev), ##__VA_ARGS__)
44 #define rdev_dbg(rdev, fmt, ...) \
45 pr_debug("%s: " fmt, rdev_get_name(rdev), ##__VA_ARGS__)
47 static DEFINE_WW_CLASS(regulator_ww_class);
48 static DEFINE_MUTEX(regulator_nesting_mutex);
49 static DEFINE_MUTEX(regulator_list_mutex);
50 static LIST_HEAD(regulator_map_list);
51 static LIST_HEAD(regulator_ena_gpio_list);
52 static LIST_HEAD(regulator_supply_alias_list);
53 static LIST_HEAD(regulator_coupler_list);
54 static bool has_full_constraints;
56 static struct dentry *debugfs_root;
59 * struct regulator_map
61 * Used to provide symbolic supply names to devices.
63 struct regulator_map {
64 struct list_head list;
65 const char *dev_name; /* The dev_name() for the consumer */
67 struct regulator_dev *regulator;
71 * struct regulator_enable_gpio
73 * Management for shared enable GPIO pin
75 struct regulator_enable_gpio {
76 struct list_head list;
77 struct gpio_desc *gpiod;
78 u32 enable_count; /* a number of enabled shared GPIO */
79 u32 request_count; /* a number of requested shared GPIO */
83 * struct regulator_supply_alias
85 * Used to map lookups for a supply onto an alternative device.
87 struct regulator_supply_alias {
88 struct list_head list;
89 struct device *src_dev;
90 const char *src_supply;
91 struct device *alias_dev;
92 const char *alias_supply;
95 static int _regulator_is_enabled(struct regulator_dev *rdev);
96 static int _regulator_disable(struct regulator *regulator);
97 static int _regulator_get_current_limit(struct regulator_dev *rdev);
98 static unsigned int _regulator_get_mode(struct regulator_dev *rdev);
99 static int _notifier_call_chain(struct regulator_dev *rdev,
100 unsigned long event, void *data);
101 static int _regulator_do_set_voltage(struct regulator_dev *rdev,
102 int min_uV, int max_uV);
103 static int regulator_balance_voltage(struct regulator_dev *rdev,
104 suspend_state_t state);
105 static struct regulator *create_regulator(struct regulator_dev *rdev,
107 const char *supply_name);
108 static void destroy_regulator(struct regulator *regulator);
109 static void _regulator_put(struct regulator *regulator);
111 const char *rdev_get_name(struct regulator_dev *rdev)
113 if (rdev->constraints && rdev->constraints->name)
114 return rdev->constraints->name;
115 else if (rdev->desc->name)
116 return rdev->desc->name;
121 static bool have_full_constraints(void)
123 return has_full_constraints || of_have_populated_dt();
126 static bool regulator_ops_is_valid(struct regulator_dev *rdev, int ops)
128 if (!rdev->constraints) {
129 rdev_err(rdev, "no constraints\n");
133 if (rdev->constraints->valid_ops_mask & ops)
140 * regulator_lock_nested - lock a single regulator
141 * @rdev: regulator source
142 * @ww_ctx: w/w mutex acquire context
144 * This function can be called many times by one task on
145 * a single regulator and its mutex will be locked only
146 * once. If a task, which is calling this function is other
147 * than the one, which initially locked the mutex, it will
150 static inline int regulator_lock_nested(struct regulator_dev *rdev,
151 struct ww_acquire_ctx *ww_ctx)
156 mutex_lock(®ulator_nesting_mutex);
158 if (ww_ctx || !ww_mutex_trylock(&rdev->mutex)) {
159 if (rdev->mutex_owner == current)
165 mutex_unlock(®ulator_nesting_mutex);
166 ret = ww_mutex_lock(&rdev->mutex, ww_ctx);
167 mutex_lock(®ulator_nesting_mutex);
173 if (lock && ret != -EDEADLK) {
175 rdev->mutex_owner = current;
178 mutex_unlock(®ulator_nesting_mutex);
184 * regulator_lock - lock a single regulator
185 * @rdev: regulator source
187 * This function can be called many times by one task on
188 * a single regulator and its mutex will be locked only
189 * once. If a task, which is calling this function is other
190 * than the one, which initially locked the mutex, it will
193 static void regulator_lock(struct regulator_dev *rdev)
195 regulator_lock_nested(rdev, NULL);
199 * regulator_unlock - unlock a single regulator
200 * @rdev: regulator_source
202 * This function unlocks the mutex when the
203 * reference counter reaches 0.
205 static void regulator_unlock(struct regulator_dev *rdev)
207 mutex_lock(®ulator_nesting_mutex);
209 if (--rdev->ref_cnt == 0) {
210 rdev->mutex_owner = NULL;
211 ww_mutex_unlock(&rdev->mutex);
214 WARN_ON_ONCE(rdev->ref_cnt < 0);
216 mutex_unlock(®ulator_nesting_mutex);
219 static bool regulator_supply_is_couple(struct regulator_dev *rdev)
221 struct regulator_dev *c_rdev;
224 for (i = 1; i < rdev->coupling_desc.n_coupled; i++) {
225 c_rdev = rdev->coupling_desc.coupled_rdevs[i];
227 if (rdev->supply->rdev == c_rdev)
234 static void regulator_unlock_recursive(struct regulator_dev *rdev,
235 unsigned int n_coupled)
237 struct regulator_dev *c_rdev, *supply_rdev;
238 int i, supply_n_coupled;
240 for (i = n_coupled; i > 0; i--) {
241 c_rdev = rdev->coupling_desc.coupled_rdevs[i - 1];
246 if (c_rdev->supply && !regulator_supply_is_couple(c_rdev)) {
247 supply_rdev = c_rdev->supply->rdev;
248 supply_n_coupled = supply_rdev->coupling_desc.n_coupled;
250 regulator_unlock_recursive(supply_rdev,
254 regulator_unlock(c_rdev);
258 static int regulator_lock_recursive(struct regulator_dev *rdev,
259 struct regulator_dev **new_contended_rdev,
260 struct regulator_dev **old_contended_rdev,
261 struct ww_acquire_ctx *ww_ctx)
263 struct regulator_dev *c_rdev;
266 for (i = 0; i < rdev->coupling_desc.n_coupled; i++) {
267 c_rdev = rdev->coupling_desc.coupled_rdevs[i];
272 if (c_rdev != *old_contended_rdev) {
273 err = regulator_lock_nested(c_rdev, ww_ctx);
275 if (err == -EDEADLK) {
276 *new_contended_rdev = c_rdev;
280 /* shouldn't happen */
281 WARN_ON_ONCE(err != -EALREADY);
284 *old_contended_rdev = NULL;
287 if (c_rdev->supply && !regulator_supply_is_couple(c_rdev)) {
288 err = regulator_lock_recursive(c_rdev->supply->rdev,
293 regulator_unlock(c_rdev);
302 regulator_unlock_recursive(rdev, i);
308 * regulator_unlock_dependent - unlock regulator's suppliers and coupled
310 * @rdev: regulator source
311 * @ww_ctx: w/w mutex acquire context
313 * Unlock all regulators related with rdev by coupling or supplying.
315 static void regulator_unlock_dependent(struct regulator_dev *rdev,
316 struct ww_acquire_ctx *ww_ctx)
318 regulator_unlock_recursive(rdev, rdev->coupling_desc.n_coupled);
319 ww_acquire_fini(ww_ctx);
323 * regulator_lock_dependent - lock regulator's suppliers and coupled regulators
324 * @rdev: regulator source
325 * @ww_ctx: w/w mutex acquire context
327 * This function as a wrapper on regulator_lock_recursive(), which locks
328 * all regulators related with rdev by coupling or supplying.
330 static void regulator_lock_dependent(struct regulator_dev *rdev,
331 struct ww_acquire_ctx *ww_ctx)
333 struct regulator_dev *new_contended_rdev = NULL;
334 struct regulator_dev *old_contended_rdev = NULL;
337 mutex_lock(®ulator_list_mutex);
339 ww_acquire_init(ww_ctx, ®ulator_ww_class);
342 if (new_contended_rdev) {
343 ww_mutex_lock_slow(&new_contended_rdev->mutex, ww_ctx);
344 old_contended_rdev = new_contended_rdev;
345 old_contended_rdev->ref_cnt++;
348 err = regulator_lock_recursive(rdev,
353 if (old_contended_rdev)
354 regulator_unlock(old_contended_rdev);
356 } while (err == -EDEADLK);
358 ww_acquire_done(ww_ctx);
360 mutex_unlock(®ulator_list_mutex);
364 * of_get_child_regulator - get a child regulator device node
365 * based on supply name
366 * @parent: Parent device node
367 * @prop_name: Combination regulator supply name and "-supply"
369 * Traverse all child nodes.
370 * Extract the child regulator device node corresponding to the supply name.
371 * returns the device node corresponding to the regulator if found, else
374 static struct device_node *of_get_child_regulator(struct device_node *parent,
375 const char *prop_name)
377 struct device_node *regnode = NULL;
378 struct device_node *child = NULL;
380 for_each_child_of_node(parent, child) {
381 regnode = of_parse_phandle(child, prop_name, 0);
384 regnode = of_get_child_regulator(child, prop_name);
399 * of_get_regulator - get a regulator device node based on supply name
400 * @dev: Device pointer for the consumer (of regulator) device
401 * @supply: regulator supply name
403 * Extract the regulator device node corresponding to the supply name.
404 * returns the device node corresponding to the regulator if found, else
407 static struct device_node *of_get_regulator(struct device *dev, const char *supply)
409 struct device_node *regnode = NULL;
410 char prop_name[64]; /* 64 is max size of property name */
412 dev_dbg(dev, "Looking up %s-supply from device tree\n", supply);
414 snprintf(prop_name, 64, "%s-supply", supply);
415 regnode = of_parse_phandle(dev->of_node, prop_name, 0);
418 regnode = of_get_child_regulator(dev->of_node, prop_name);
422 dev_dbg(dev, "Looking up %s property in node %pOF failed\n",
423 prop_name, dev->of_node);
429 /* Platform voltage constraint check */
430 int regulator_check_voltage(struct regulator_dev *rdev,
431 int *min_uV, int *max_uV)
433 BUG_ON(*min_uV > *max_uV);
435 if (!regulator_ops_is_valid(rdev, REGULATOR_CHANGE_VOLTAGE)) {
436 rdev_err(rdev, "voltage operation not allowed\n");
440 if (*max_uV > rdev->constraints->max_uV)
441 *max_uV = rdev->constraints->max_uV;
442 if (*min_uV < rdev->constraints->min_uV)
443 *min_uV = rdev->constraints->min_uV;
445 if (*min_uV > *max_uV) {
446 rdev_err(rdev, "unsupportable voltage range: %d-%duV\n",
454 /* return 0 if the state is valid */
455 static int regulator_check_states(suspend_state_t state)
457 return (state > PM_SUSPEND_MAX || state == PM_SUSPEND_TO_IDLE);
460 /* Make sure we select a voltage that suits the needs of all
461 * regulator consumers
463 int regulator_check_consumers(struct regulator_dev *rdev,
464 int *min_uV, int *max_uV,
465 suspend_state_t state)
467 struct regulator *regulator;
468 struct regulator_voltage *voltage;
470 list_for_each_entry(regulator, &rdev->consumer_list, list) {
471 voltage = ®ulator->voltage[state];
473 * Assume consumers that didn't say anything are OK
474 * with anything in the constraint range.
476 if (!voltage->min_uV && !voltage->max_uV)
479 if (*max_uV > voltage->max_uV)
480 *max_uV = voltage->max_uV;
481 if (*min_uV < voltage->min_uV)
482 *min_uV = voltage->min_uV;
485 if (*min_uV > *max_uV) {
486 rdev_err(rdev, "Restricting voltage, %u-%uuV\n",
494 /* current constraint check */
495 static int regulator_check_current_limit(struct regulator_dev *rdev,
496 int *min_uA, int *max_uA)
498 BUG_ON(*min_uA > *max_uA);
500 if (!regulator_ops_is_valid(rdev, REGULATOR_CHANGE_CURRENT)) {
501 rdev_err(rdev, "current operation not allowed\n");
505 if (*max_uA > rdev->constraints->max_uA)
506 *max_uA = rdev->constraints->max_uA;
507 if (*min_uA < rdev->constraints->min_uA)
508 *min_uA = rdev->constraints->min_uA;
510 if (*min_uA > *max_uA) {
511 rdev_err(rdev, "unsupportable current range: %d-%duA\n",
519 /* operating mode constraint check */
520 static int regulator_mode_constrain(struct regulator_dev *rdev,
524 case REGULATOR_MODE_FAST:
525 case REGULATOR_MODE_NORMAL:
526 case REGULATOR_MODE_IDLE:
527 case REGULATOR_MODE_STANDBY:
530 rdev_err(rdev, "invalid mode %x specified\n", *mode);
534 if (!regulator_ops_is_valid(rdev, REGULATOR_CHANGE_MODE)) {
535 rdev_err(rdev, "mode operation not allowed\n");
539 /* The modes are bitmasks, the most power hungry modes having
540 * the lowest values. If the requested mode isn't supported
544 if (rdev->constraints->valid_modes_mask & *mode)
552 static inline struct regulator_state *
553 regulator_get_suspend_state(struct regulator_dev *rdev, suspend_state_t state)
555 if (rdev->constraints == NULL)
559 case PM_SUSPEND_STANDBY:
560 return &rdev->constraints->state_standby;
562 return &rdev->constraints->state_mem;
564 return &rdev->constraints->state_disk;
570 static const struct regulator_state *
571 regulator_get_suspend_state_check(struct regulator_dev *rdev, suspend_state_t state)
573 const struct regulator_state *rstate;
575 rstate = regulator_get_suspend_state(rdev, state);
579 /* If we have no suspend mode configuration don't set anything;
580 * only warn if the driver implements set_suspend_voltage or
581 * set_suspend_mode callback.
583 if (rstate->enabled != ENABLE_IN_SUSPEND &&
584 rstate->enabled != DISABLE_IN_SUSPEND) {
585 if (rdev->desc->ops->set_suspend_voltage ||
586 rdev->desc->ops->set_suspend_mode)
587 rdev_warn(rdev, "No configuration\n");
594 static ssize_t regulator_uV_show(struct device *dev,
595 struct device_attribute *attr, char *buf)
597 struct regulator_dev *rdev = dev_get_drvdata(dev);
600 regulator_lock(rdev);
601 uV = regulator_get_voltage_rdev(rdev);
602 regulator_unlock(rdev);
606 return sprintf(buf, "%d\n", uV);
608 static DEVICE_ATTR(microvolts, 0444, regulator_uV_show, NULL);
610 static ssize_t regulator_uA_show(struct device *dev,
611 struct device_attribute *attr, char *buf)
613 struct regulator_dev *rdev = dev_get_drvdata(dev);
615 return sprintf(buf, "%d\n", _regulator_get_current_limit(rdev));
617 static DEVICE_ATTR(microamps, 0444, regulator_uA_show, NULL);
619 static ssize_t name_show(struct device *dev, struct device_attribute *attr,
622 struct regulator_dev *rdev = dev_get_drvdata(dev);
624 return sprintf(buf, "%s\n", rdev_get_name(rdev));
626 static DEVICE_ATTR_RO(name);
628 static const char *regulator_opmode_to_str(int mode)
631 case REGULATOR_MODE_FAST:
633 case REGULATOR_MODE_NORMAL:
635 case REGULATOR_MODE_IDLE:
637 case REGULATOR_MODE_STANDBY:
643 static ssize_t regulator_print_opmode(char *buf, int mode)
645 return sprintf(buf, "%s\n", regulator_opmode_to_str(mode));
648 static ssize_t regulator_opmode_show(struct device *dev,
649 struct device_attribute *attr, char *buf)
651 struct regulator_dev *rdev = dev_get_drvdata(dev);
653 return regulator_print_opmode(buf, _regulator_get_mode(rdev));
655 static DEVICE_ATTR(opmode, 0444, regulator_opmode_show, NULL);
657 static ssize_t regulator_print_state(char *buf, int state)
660 return sprintf(buf, "enabled\n");
662 return sprintf(buf, "disabled\n");
664 return sprintf(buf, "unknown\n");
667 static ssize_t regulator_state_show(struct device *dev,
668 struct device_attribute *attr, char *buf)
670 struct regulator_dev *rdev = dev_get_drvdata(dev);
673 regulator_lock(rdev);
674 ret = regulator_print_state(buf, _regulator_is_enabled(rdev));
675 regulator_unlock(rdev);
679 static DEVICE_ATTR(state, 0444, regulator_state_show, NULL);
681 static ssize_t regulator_status_show(struct device *dev,
682 struct device_attribute *attr, char *buf)
684 struct regulator_dev *rdev = dev_get_drvdata(dev);
688 status = rdev->desc->ops->get_status(rdev);
693 case REGULATOR_STATUS_OFF:
696 case REGULATOR_STATUS_ON:
699 case REGULATOR_STATUS_ERROR:
702 case REGULATOR_STATUS_FAST:
705 case REGULATOR_STATUS_NORMAL:
708 case REGULATOR_STATUS_IDLE:
711 case REGULATOR_STATUS_STANDBY:
714 case REGULATOR_STATUS_BYPASS:
717 case REGULATOR_STATUS_UNDEFINED:
724 return sprintf(buf, "%s\n", label);
726 static DEVICE_ATTR(status, 0444, regulator_status_show, NULL);
728 static ssize_t regulator_min_uA_show(struct device *dev,
729 struct device_attribute *attr, char *buf)
731 struct regulator_dev *rdev = dev_get_drvdata(dev);
733 if (!rdev->constraints)
734 return sprintf(buf, "constraint not defined\n");
736 return sprintf(buf, "%d\n", rdev->constraints->min_uA);
738 static DEVICE_ATTR(min_microamps, 0444, regulator_min_uA_show, NULL);
740 static ssize_t regulator_max_uA_show(struct device *dev,
741 struct device_attribute *attr, char *buf)
743 struct regulator_dev *rdev = dev_get_drvdata(dev);
745 if (!rdev->constraints)
746 return sprintf(buf, "constraint not defined\n");
748 return sprintf(buf, "%d\n", rdev->constraints->max_uA);
750 static DEVICE_ATTR(max_microamps, 0444, regulator_max_uA_show, NULL);
752 static ssize_t regulator_min_uV_show(struct device *dev,
753 struct device_attribute *attr, char *buf)
755 struct regulator_dev *rdev = dev_get_drvdata(dev);
757 if (!rdev->constraints)
758 return sprintf(buf, "constraint not defined\n");
760 return sprintf(buf, "%d\n", rdev->constraints->min_uV);
762 static DEVICE_ATTR(min_microvolts, 0444, regulator_min_uV_show, NULL);
764 static ssize_t regulator_max_uV_show(struct device *dev,
765 struct device_attribute *attr, char *buf)
767 struct regulator_dev *rdev = dev_get_drvdata(dev);
769 if (!rdev->constraints)
770 return sprintf(buf, "constraint not defined\n");
772 return sprintf(buf, "%d\n", rdev->constraints->max_uV);
774 static DEVICE_ATTR(max_microvolts, 0444, regulator_max_uV_show, NULL);
776 static ssize_t regulator_total_uA_show(struct device *dev,
777 struct device_attribute *attr, char *buf)
779 struct regulator_dev *rdev = dev_get_drvdata(dev);
780 struct regulator *regulator;
783 regulator_lock(rdev);
784 list_for_each_entry(regulator, &rdev->consumer_list, list) {
785 if (regulator->enable_count)
786 uA += regulator->uA_load;
788 regulator_unlock(rdev);
789 return sprintf(buf, "%d\n", uA);
791 static DEVICE_ATTR(requested_microamps, 0444, regulator_total_uA_show, NULL);
793 static ssize_t num_users_show(struct device *dev, struct device_attribute *attr,
796 struct regulator_dev *rdev = dev_get_drvdata(dev);
797 return sprintf(buf, "%d\n", rdev->use_count);
799 static DEVICE_ATTR_RO(num_users);
801 static ssize_t type_show(struct device *dev, struct device_attribute *attr,
804 struct regulator_dev *rdev = dev_get_drvdata(dev);
806 switch (rdev->desc->type) {
807 case REGULATOR_VOLTAGE:
808 return sprintf(buf, "voltage\n");
809 case REGULATOR_CURRENT:
810 return sprintf(buf, "current\n");
812 return sprintf(buf, "unknown\n");
814 static DEVICE_ATTR_RO(type);
816 static ssize_t regulator_suspend_mem_uV_show(struct device *dev,
817 struct device_attribute *attr, char *buf)
819 struct regulator_dev *rdev = dev_get_drvdata(dev);
821 return sprintf(buf, "%d\n", rdev->constraints->state_mem.uV);
823 static DEVICE_ATTR(suspend_mem_microvolts, 0444,
824 regulator_suspend_mem_uV_show, NULL);
826 static ssize_t regulator_suspend_disk_uV_show(struct device *dev,
827 struct device_attribute *attr, char *buf)
829 struct regulator_dev *rdev = dev_get_drvdata(dev);
831 return sprintf(buf, "%d\n", rdev->constraints->state_disk.uV);
833 static DEVICE_ATTR(suspend_disk_microvolts, 0444,
834 regulator_suspend_disk_uV_show, NULL);
836 static ssize_t regulator_suspend_standby_uV_show(struct device *dev,
837 struct device_attribute *attr, char *buf)
839 struct regulator_dev *rdev = dev_get_drvdata(dev);
841 return sprintf(buf, "%d\n", rdev->constraints->state_standby.uV);
843 static DEVICE_ATTR(suspend_standby_microvolts, 0444,
844 regulator_suspend_standby_uV_show, NULL);
846 static ssize_t regulator_suspend_mem_mode_show(struct device *dev,
847 struct device_attribute *attr, char *buf)
849 struct regulator_dev *rdev = dev_get_drvdata(dev);
851 return regulator_print_opmode(buf,
852 rdev->constraints->state_mem.mode);
854 static DEVICE_ATTR(suspend_mem_mode, 0444,
855 regulator_suspend_mem_mode_show, NULL);
857 static ssize_t regulator_suspend_disk_mode_show(struct device *dev,
858 struct device_attribute *attr, char *buf)
860 struct regulator_dev *rdev = dev_get_drvdata(dev);
862 return regulator_print_opmode(buf,
863 rdev->constraints->state_disk.mode);
865 static DEVICE_ATTR(suspend_disk_mode, 0444,
866 regulator_suspend_disk_mode_show, NULL);
868 static ssize_t regulator_suspend_standby_mode_show(struct device *dev,
869 struct device_attribute *attr, char *buf)
871 struct regulator_dev *rdev = dev_get_drvdata(dev);
873 return regulator_print_opmode(buf,
874 rdev->constraints->state_standby.mode);
876 static DEVICE_ATTR(suspend_standby_mode, 0444,
877 regulator_suspend_standby_mode_show, NULL);
879 static ssize_t regulator_suspend_mem_state_show(struct device *dev,
880 struct device_attribute *attr, char *buf)
882 struct regulator_dev *rdev = dev_get_drvdata(dev);
884 return regulator_print_state(buf,
885 rdev->constraints->state_mem.enabled);
887 static DEVICE_ATTR(suspend_mem_state, 0444,
888 regulator_suspend_mem_state_show, NULL);
890 static ssize_t regulator_suspend_disk_state_show(struct device *dev,
891 struct device_attribute *attr, char *buf)
893 struct regulator_dev *rdev = dev_get_drvdata(dev);
895 return regulator_print_state(buf,
896 rdev->constraints->state_disk.enabled);
898 static DEVICE_ATTR(suspend_disk_state, 0444,
899 regulator_suspend_disk_state_show, NULL);
901 static ssize_t regulator_suspend_standby_state_show(struct device *dev,
902 struct device_attribute *attr, char *buf)
904 struct regulator_dev *rdev = dev_get_drvdata(dev);
906 return regulator_print_state(buf,
907 rdev->constraints->state_standby.enabled);
909 static DEVICE_ATTR(suspend_standby_state, 0444,
910 regulator_suspend_standby_state_show, NULL);
912 static ssize_t regulator_bypass_show(struct device *dev,
913 struct device_attribute *attr, char *buf)
915 struct regulator_dev *rdev = dev_get_drvdata(dev);
920 ret = rdev->desc->ops->get_bypass(rdev, &bypass);
929 return sprintf(buf, "%s\n", report);
931 static DEVICE_ATTR(bypass, 0444,
932 regulator_bypass_show, NULL);
934 /* Calculate the new optimum regulator operating mode based on the new total
935 * consumer load. All locks held by caller
937 static int drms_uA_update(struct regulator_dev *rdev)
939 struct regulator *sibling;
940 int current_uA = 0, output_uV, input_uV, err;
944 * first check to see if we can set modes at all, otherwise just
945 * tell the consumer everything is OK.
947 if (!regulator_ops_is_valid(rdev, REGULATOR_CHANGE_DRMS)) {
948 rdev_dbg(rdev, "DRMS operation not allowed\n");
952 if (!rdev->desc->ops->get_optimum_mode &&
953 !rdev->desc->ops->set_load)
956 if (!rdev->desc->ops->set_mode &&
957 !rdev->desc->ops->set_load)
960 /* calc total requested load */
961 list_for_each_entry(sibling, &rdev->consumer_list, list) {
962 if (sibling->enable_count)
963 current_uA += sibling->uA_load;
966 current_uA += rdev->constraints->system_load;
968 if (rdev->desc->ops->set_load) {
969 /* set the optimum mode for our new total regulator load */
970 err = rdev->desc->ops->set_load(rdev, current_uA);
972 rdev_err(rdev, "failed to set load %d: %pe\n",
973 current_uA, ERR_PTR(err));
975 /* get output voltage */
976 output_uV = regulator_get_voltage_rdev(rdev);
977 if (output_uV <= 0) {
978 rdev_err(rdev, "invalid output voltage found\n");
982 /* get input voltage */
985 input_uV = regulator_get_voltage(rdev->supply);
987 input_uV = rdev->constraints->input_uV;
989 rdev_err(rdev, "invalid input voltage found\n");
993 /* now get the optimum mode for our new total regulator load */
994 mode = rdev->desc->ops->get_optimum_mode(rdev, input_uV,
995 output_uV, current_uA);
997 /* check the new mode is allowed */
998 err = regulator_mode_constrain(rdev, &mode);
1000 rdev_err(rdev, "failed to get optimum mode @ %d uA %d -> %d uV: %pe\n",
1001 current_uA, input_uV, output_uV, ERR_PTR(err));
1005 err = rdev->desc->ops->set_mode(rdev, mode);
1007 rdev_err(rdev, "failed to set optimum mode %x: %pe\n",
1008 mode, ERR_PTR(err));
1014 static int __suspend_set_state(struct regulator_dev *rdev,
1015 const struct regulator_state *rstate)
1019 if (rstate->enabled == ENABLE_IN_SUSPEND &&
1020 rdev->desc->ops->set_suspend_enable)
1021 ret = rdev->desc->ops->set_suspend_enable(rdev);
1022 else if (rstate->enabled == DISABLE_IN_SUSPEND &&
1023 rdev->desc->ops->set_suspend_disable)
1024 ret = rdev->desc->ops->set_suspend_disable(rdev);
1025 else /* OK if set_suspend_enable or set_suspend_disable is NULL */
1029 rdev_err(rdev, "failed to enabled/disable: %pe\n", ERR_PTR(ret));
1033 if (rdev->desc->ops->set_suspend_voltage && rstate->uV > 0) {
1034 ret = rdev->desc->ops->set_suspend_voltage(rdev, rstate->uV);
1036 rdev_err(rdev, "failed to set voltage: %pe\n", ERR_PTR(ret));
1041 if (rdev->desc->ops->set_suspend_mode && rstate->mode > 0) {
1042 ret = rdev->desc->ops->set_suspend_mode(rdev, rstate->mode);
1044 rdev_err(rdev, "failed to set mode: %pe\n", ERR_PTR(ret));
1052 static int suspend_set_initial_state(struct regulator_dev *rdev)
1054 const struct regulator_state *rstate;
1056 rstate = regulator_get_suspend_state_check(rdev,
1057 rdev->constraints->initial_state);
1061 return __suspend_set_state(rdev, rstate);
1064 #if defined(DEBUG) || defined(CONFIG_DYNAMIC_DEBUG)
1065 static void print_constraints_debug(struct regulator_dev *rdev)
1067 struct regulation_constraints *constraints = rdev->constraints;
1069 size_t len = sizeof(buf) - 1;
1073 if (constraints->min_uV && constraints->max_uV) {
1074 if (constraints->min_uV == constraints->max_uV)
1075 count += scnprintf(buf + count, len - count, "%d mV ",
1076 constraints->min_uV / 1000);
1078 count += scnprintf(buf + count, len - count,
1080 constraints->min_uV / 1000,
1081 constraints->max_uV / 1000);
1084 if (!constraints->min_uV ||
1085 constraints->min_uV != constraints->max_uV) {
1086 ret = regulator_get_voltage_rdev(rdev);
1088 count += scnprintf(buf + count, len - count,
1089 "at %d mV ", ret / 1000);
1092 if (constraints->uV_offset)
1093 count += scnprintf(buf + count, len - count, "%dmV offset ",
1094 constraints->uV_offset / 1000);
1096 if (constraints->min_uA && constraints->max_uA) {
1097 if (constraints->min_uA == constraints->max_uA)
1098 count += scnprintf(buf + count, len - count, "%d mA ",
1099 constraints->min_uA / 1000);
1101 count += scnprintf(buf + count, len - count,
1103 constraints->min_uA / 1000,
1104 constraints->max_uA / 1000);
1107 if (!constraints->min_uA ||
1108 constraints->min_uA != constraints->max_uA) {
1109 ret = _regulator_get_current_limit(rdev);
1111 count += scnprintf(buf + count, len - count,
1112 "at %d mA ", ret / 1000);
1115 if (constraints->valid_modes_mask & REGULATOR_MODE_FAST)
1116 count += scnprintf(buf + count, len - count, "fast ");
1117 if (constraints->valid_modes_mask & REGULATOR_MODE_NORMAL)
1118 count += scnprintf(buf + count, len - count, "normal ");
1119 if (constraints->valid_modes_mask & REGULATOR_MODE_IDLE)
1120 count += scnprintf(buf + count, len - count, "idle ");
1121 if (constraints->valid_modes_mask & REGULATOR_MODE_STANDBY)
1122 count += scnprintf(buf + count, len - count, "standby ");
1125 count = scnprintf(buf, len, "no parameters");
1129 count += scnprintf(buf + count, len - count, ", %s",
1130 _regulator_is_enabled(rdev) ? "enabled" : "disabled");
1132 rdev_dbg(rdev, "%s\n", buf);
1134 #else /* !DEBUG && !CONFIG_DYNAMIC_DEBUG */
1135 static inline void print_constraints_debug(struct regulator_dev *rdev) {}
1136 #endif /* !DEBUG && !CONFIG_DYNAMIC_DEBUG */
1138 static void print_constraints(struct regulator_dev *rdev)
1140 struct regulation_constraints *constraints = rdev->constraints;
1142 print_constraints_debug(rdev);
1144 if ((constraints->min_uV != constraints->max_uV) &&
1145 !regulator_ops_is_valid(rdev, REGULATOR_CHANGE_VOLTAGE))
1147 "Voltage range but no REGULATOR_CHANGE_VOLTAGE\n");
1150 static int machine_constraints_voltage(struct regulator_dev *rdev,
1151 struct regulation_constraints *constraints)
1153 const struct regulator_ops *ops = rdev->desc->ops;
1156 /* do we need to apply the constraint voltage */
1157 if (rdev->constraints->apply_uV &&
1158 rdev->constraints->min_uV && rdev->constraints->max_uV) {
1159 int target_min, target_max;
1160 int current_uV = regulator_get_voltage_rdev(rdev);
1162 if (current_uV == -ENOTRECOVERABLE) {
1163 /* This regulator can't be read and must be initialized */
1164 rdev_info(rdev, "Setting %d-%duV\n",
1165 rdev->constraints->min_uV,
1166 rdev->constraints->max_uV);
1167 _regulator_do_set_voltage(rdev,
1168 rdev->constraints->min_uV,
1169 rdev->constraints->max_uV);
1170 current_uV = regulator_get_voltage_rdev(rdev);
1173 if (current_uV < 0) {
1175 "failed to get the current voltage: %pe\n",
1176 ERR_PTR(current_uV));
1181 * If we're below the minimum voltage move up to the
1182 * minimum voltage, if we're above the maximum voltage
1183 * then move down to the maximum.
1185 target_min = current_uV;
1186 target_max = current_uV;
1188 if (current_uV < rdev->constraints->min_uV) {
1189 target_min = rdev->constraints->min_uV;
1190 target_max = rdev->constraints->min_uV;
1193 if (current_uV > rdev->constraints->max_uV) {
1194 target_min = rdev->constraints->max_uV;
1195 target_max = rdev->constraints->max_uV;
1198 if (target_min != current_uV || target_max != current_uV) {
1199 rdev_info(rdev, "Bringing %duV into %d-%duV\n",
1200 current_uV, target_min, target_max);
1201 ret = _regulator_do_set_voltage(
1202 rdev, target_min, target_max);
1205 "failed to apply %d-%duV constraint: %pe\n",
1206 target_min, target_max, ERR_PTR(ret));
1212 /* constrain machine-level voltage specs to fit
1213 * the actual range supported by this regulator.
1215 if (ops->list_voltage && rdev->desc->n_voltages) {
1216 int count = rdev->desc->n_voltages;
1218 int min_uV = INT_MAX;
1219 int max_uV = INT_MIN;
1220 int cmin = constraints->min_uV;
1221 int cmax = constraints->max_uV;
1223 /* it's safe to autoconfigure fixed-voltage supplies
1224 * and the constraints are used by list_voltage.
1226 if (count == 1 && !cmin) {
1229 constraints->min_uV = cmin;
1230 constraints->max_uV = cmax;
1233 /* voltage constraints are optional */
1234 if ((cmin == 0) && (cmax == 0))
1237 /* else require explicit machine-level constraints */
1238 if (cmin <= 0 || cmax <= 0 || cmax < cmin) {
1239 rdev_err(rdev, "invalid voltage constraints\n");
1243 /* no need to loop voltages if range is continuous */
1244 if (rdev->desc->continuous_voltage_range)
1247 /* initial: [cmin..cmax] valid, [min_uV..max_uV] not */
1248 for (i = 0; i < count; i++) {
1251 value = ops->list_voltage(rdev, i);
1255 /* maybe adjust [min_uV..max_uV] */
1256 if (value >= cmin && value < min_uV)
1258 if (value <= cmax && value > max_uV)
1262 /* final: [min_uV..max_uV] valid iff constraints valid */
1263 if (max_uV < min_uV) {
1265 "unsupportable voltage constraints %u-%uuV\n",
1270 /* use regulator's subset of machine constraints */
1271 if (constraints->min_uV < min_uV) {
1272 rdev_dbg(rdev, "override min_uV, %d -> %d\n",
1273 constraints->min_uV, min_uV);
1274 constraints->min_uV = min_uV;
1276 if (constraints->max_uV > max_uV) {
1277 rdev_dbg(rdev, "override max_uV, %d -> %d\n",
1278 constraints->max_uV, max_uV);
1279 constraints->max_uV = max_uV;
1286 static int machine_constraints_current(struct regulator_dev *rdev,
1287 struct regulation_constraints *constraints)
1289 const struct regulator_ops *ops = rdev->desc->ops;
1292 if (!constraints->min_uA && !constraints->max_uA)
1295 if (constraints->min_uA > constraints->max_uA) {
1296 rdev_err(rdev, "Invalid current constraints\n");
1300 if (!ops->set_current_limit || !ops->get_current_limit) {
1301 rdev_warn(rdev, "Operation of current configuration missing\n");
1305 /* Set regulator current in constraints range */
1306 ret = ops->set_current_limit(rdev, constraints->min_uA,
1307 constraints->max_uA);
1309 rdev_err(rdev, "Failed to set current constraint, %d\n", ret);
1316 static int _regulator_do_enable(struct regulator_dev *rdev);
1319 * set_machine_constraints - sets regulator constraints
1320 * @rdev: regulator source
1322 * Allows platform initialisation code to define and constrain
1323 * regulator circuits e.g. valid voltage/current ranges, etc. NOTE:
1324 * Constraints *must* be set by platform code in order for some
1325 * regulator operations to proceed i.e. set_voltage, set_current_limit,
1328 static int set_machine_constraints(struct regulator_dev *rdev)
1331 const struct regulator_ops *ops = rdev->desc->ops;
1333 ret = machine_constraints_voltage(rdev, rdev->constraints);
1337 ret = machine_constraints_current(rdev, rdev->constraints);
1341 if (rdev->constraints->ilim_uA && ops->set_input_current_limit) {
1342 ret = ops->set_input_current_limit(rdev,
1343 rdev->constraints->ilim_uA);
1345 rdev_err(rdev, "failed to set input limit: %pe\n", ERR_PTR(ret));
1350 /* do we need to setup our suspend state */
1351 if (rdev->constraints->initial_state) {
1352 ret = suspend_set_initial_state(rdev);
1354 rdev_err(rdev, "failed to set suspend state: %pe\n", ERR_PTR(ret));
1359 if (rdev->constraints->initial_mode) {
1360 if (!ops->set_mode) {
1361 rdev_err(rdev, "no set_mode operation\n");
1365 ret = ops->set_mode(rdev, rdev->constraints->initial_mode);
1367 rdev_err(rdev, "failed to set initial mode: %pe\n", ERR_PTR(ret));
1370 } else if (rdev->constraints->system_load) {
1372 * We'll only apply the initial system load if an
1373 * initial mode wasn't specified.
1375 drms_uA_update(rdev);
1378 if ((rdev->constraints->ramp_delay || rdev->constraints->ramp_disable)
1379 && ops->set_ramp_delay) {
1380 ret = ops->set_ramp_delay(rdev, rdev->constraints->ramp_delay);
1382 rdev_err(rdev, "failed to set ramp_delay: %pe\n", ERR_PTR(ret));
1387 if (rdev->constraints->pull_down && ops->set_pull_down) {
1388 ret = ops->set_pull_down(rdev);
1390 rdev_err(rdev, "failed to set pull down: %pe\n", ERR_PTR(ret));
1395 if (rdev->constraints->soft_start && ops->set_soft_start) {
1396 ret = ops->set_soft_start(rdev);
1398 rdev_err(rdev, "failed to set soft start: %pe\n", ERR_PTR(ret));
1403 if (rdev->constraints->over_current_protection
1404 && ops->set_over_current_protection) {
1405 ret = ops->set_over_current_protection(rdev);
1407 rdev_err(rdev, "failed to set over current protection: %pe\n",
1413 if (rdev->constraints->active_discharge && ops->set_active_discharge) {
1414 bool ad_state = (rdev->constraints->active_discharge ==
1415 REGULATOR_ACTIVE_DISCHARGE_ENABLE) ? true : false;
1417 ret = ops->set_active_discharge(rdev, ad_state);
1419 rdev_err(rdev, "failed to set active discharge: %pe\n", ERR_PTR(ret));
1424 /* If the constraints say the regulator should be on at this point
1425 * and we have control then make sure it is enabled.
1427 if (rdev->constraints->always_on || rdev->constraints->boot_on) {
1429 ret = regulator_enable(rdev->supply);
1431 _regulator_put(rdev->supply);
1432 rdev->supply = NULL;
1437 ret = _regulator_do_enable(rdev);
1438 if (ret < 0 && ret != -EINVAL) {
1439 rdev_err(rdev, "failed to enable: %pe\n", ERR_PTR(ret));
1443 if (rdev->constraints->always_on)
1445 } else if (rdev->desc->off_on_delay) {
1446 rdev->last_off = ktime_get();
1449 print_constraints(rdev);
1454 * set_supply - set regulator supply regulator
1455 * @rdev: regulator name
1456 * @supply_rdev: supply regulator name
1458 * Called by platform initialisation code to set the supply regulator for this
1459 * regulator. This ensures that a regulators supply will also be enabled by the
1460 * core if it's child is enabled.
1462 static int set_supply(struct regulator_dev *rdev,
1463 struct regulator_dev *supply_rdev)
1467 rdev_info(rdev, "supplied by %s\n", rdev_get_name(supply_rdev));
1469 if (!try_module_get(supply_rdev->owner))
1472 rdev->supply = create_regulator(supply_rdev, &rdev->dev, "SUPPLY");
1473 if (rdev->supply == NULL) {
1477 supply_rdev->open_count++;
1483 * set_consumer_device_supply - Bind a regulator to a symbolic supply
1484 * @rdev: regulator source
1485 * @consumer_dev_name: dev_name() string for device supply applies to
1486 * @supply: symbolic name for supply
1488 * Allows platform initialisation code to map physical regulator
1489 * sources to symbolic names for supplies for use by devices. Devices
1490 * should use these symbolic names to request regulators, avoiding the
1491 * need to provide board-specific regulator names as platform data.
1493 static int set_consumer_device_supply(struct regulator_dev *rdev,
1494 const char *consumer_dev_name,
1497 struct regulator_map *node, *new_node;
1503 if (consumer_dev_name != NULL)
1508 new_node = kzalloc(sizeof(struct regulator_map), GFP_KERNEL);
1509 if (new_node == NULL)
1512 new_node->regulator = rdev;
1513 new_node->supply = supply;
1516 new_node->dev_name = kstrdup(consumer_dev_name, GFP_KERNEL);
1517 if (new_node->dev_name == NULL) {
1523 mutex_lock(®ulator_list_mutex);
1524 list_for_each_entry(node, ®ulator_map_list, list) {
1525 if (node->dev_name && consumer_dev_name) {
1526 if (strcmp(node->dev_name, consumer_dev_name) != 0)
1528 } else if (node->dev_name || consumer_dev_name) {
1532 if (strcmp(node->supply, supply) != 0)
1535 pr_debug("%s: %s/%s is '%s' supply; fail %s/%s\n",
1537 dev_name(&node->regulator->dev),
1538 node->regulator->desc->name,
1540 dev_name(&rdev->dev), rdev_get_name(rdev));
1544 list_add(&new_node->list, ®ulator_map_list);
1545 mutex_unlock(®ulator_list_mutex);
1550 mutex_unlock(®ulator_list_mutex);
1551 kfree(new_node->dev_name);
1556 static void unset_regulator_supplies(struct regulator_dev *rdev)
1558 struct regulator_map *node, *n;
1560 list_for_each_entry_safe(node, n, ®ulator_map_list, list) {
1561 if (rdev == node->regulator) {
1562 list_del(&node->list);
1563 kfree(node->dev_name);
1569 #ifdef CONFIG_DEBUG_FS
1570 static ssize_t constraint_flags_read_file(struct file *file,
1571 char __user *user_buf,
1572 size_t count, loff_t *ppos)
1574 const struct regulator *regulator = file->private_data;
1575 const struct regulation_constraints *c = regulator->rdev->constraints;
1582 buf = kmalloc(PAGE_SIZE, GFP_KERNEL);
1586 ret = snprintf(buf, PAGE_SIZE,
1590 "ramp_disable: %u\n"
1593 "over_current_protection: %u\n",
1600 c->over_current_protection);
1602 ret = simple_read_from_buffer(user_buf, count, ppos, buf, ret);
1610 static const struct file_operations constraint_flags_fops = {
1611 #ifdef CONFIG_DEBUG_FS
1612 .open = simple_open,
1613 .read = constraint_flags_read_file,
1614 .llseek = default_llseek,
1618 #define REG_STR_SIZE 64
1620 static struct regulator *create_regulator(struct regulator_dev *rdev,
1622 const char *supply_name)
1624 struct regulator *regulator;
1628 char buf[REG_STR_SIZE];
1631 size = snprintf(buf, REG_STR_SIZE, "%s-%s",
1632 dev->kobj.name, supply_name);
1633 if (size >= REG_STR_SIZE)
1636 supply_name = kstrdup(buf, GFP_KERNEL);
1637 if (supply_name == NULL)
1640 supply_name = kstrdup_const(supply_name, GFP_KERNEL);
1641 if (supply_name == NULL)
1645 regulator = kzalloc(sizeof(*regulator), GFP_KERNEL);
1646 if (regulator == NULL) {
1651 regulator->rdev = rdev;
1652 regulator->supply_name = supply_name;
1654 regulator_lock(rdev);
1655 list_add(®ulator->list, &rdev->consumer_list);
1656 regulator_unlock(rdev);
1659 regulator->dev = dev;
1661 /* Add a link to the device sysfs entry */
1662 err = sysfs_create_link_nowarn(&rdev->dev.kobj, &dev->kobj,
1665 rdev_dbg(rdev, "could not add device link %s: %pe\n",
1666 dev->kobj.name, ERR_PTR(err));
1672 regulator->debugfs = debugfs_create_dir(supply_name, rdev->debugfs);
1673 if (!regulator->debugfs) {
1674 rdev_dbg(rdev, "Failed to create debugfs directory\n");
1676 debugfs_create_u32("uA_load", 0444, regulator->debugfs,
1677 ®ulator->uA_load);
1678 debugfs_create_u32("min_uV", 0444, regulator->debugfs,
1679 ®ulator->voltage[PM_SUSPEND_ON].min_uV);
1680 debugfs_create_u32("max_uV", 0444, regulator->debugfs,
1681 ®ulator->voltage[PM_SUSPEND_ON].max_uV);
1682 debugfs_create_file("constraint_flags", 0444,
1683 regulator->debugfs, regulator,
1684 &constraint_flags_fops);
1688 * Check now if the regulator is an always on regulator - if
1689 * it is then we don't need to do nearly so much work for
1690 * enable/disable calls.
1692 if (!regulator_ops_is_valid(rdev, REGULATOR_CHANGE_STATUS) &&
1693 _regulator_is_enabled(rdev))
1694 regulator->always_on = true;
1699 static int _regulator_get_enable_time(struct regulator_dev *rdev)
1701 if (rdev->constraints && rdev->constraints->enable_time)
1702 return rdev->constraints->enable_time;
1703 if (rdev->desc->ops->enable_time)
1704 return rdev->desc->ops->enable_time(rdev);
1705 return rdev->desc->enable_time;
1708 static struct regulator_supply_alias *regulator_find_supply_alias(
1709 struct device *dev, const char *supply)
1711 struct regulator_supply_alias *map;
1713 list_for_each_entry(map, ®ulator_supply_alias_list, list)
1714 if (map->src_dev == dev && strcmp(map->src_supply, supply) == 0)
1720 static void regulator_supply_alias(struct device **dev, const char **supply)
1722 struct regulator_supply_alias *map;
1724 map = regulator_find_supply_alias(*dev, *supply);
1726 dev_dbg(*dev, "Mapping supply %s to %s,%s\n",
1727 *supply, map->alias_supply,
1728 dev_name(map->alias_dev));
1729 *dev = map->alias_dev;
1730 *supply = map->alias_supply;
1734 static int regulator_match(struct device *dev, const void *data)
1736 struct regulator_dev *r = dev_to_rdev(dev);
1738 return strcmp(rdev_get_name(r), data) == 0;
1741 static struct regulator_dev *regulator_lookup_by_name(const char *name)
1745 dev = class_find_device(®ulator_class, NULL, name, regulator_match);
1747 return dev ? dev_to_rdev(dev) : NULL;
1751 * regulator_dev_lookup - lookup a regulator device.
1752 * @dev: device for regulator "consumer".
1753 * @supply: Supply name or regulator ID.
1755 * If successful, returns a struct regulator_dev that corresponds to the name
1756 * @supply and with the embedded struct device refcount incremented by one.
1757 * The refcount must be dropped by calling put_device().
1758 * On failure one of the following ERR-PTR-encoded values is returned:
1759 * -ENODEV if lookup fails permanently, -EPROBE_DEFER if lookup could succeed
1762 static struct regulator_dev *regulator_dev_lookup(struct device *dev,
1765 struct regulator_dev *r = NULL;
1766 struct device_node *node;
1767 struct regulator_map *map;
1768 const char *devname = NULL;
1770 regulator_supply_alias(&dev, &supply);
1772 /* first do a dt based lookup */
1773 if (dev && dev->of_node) {
1774 node = of_get_regulator(dev, supply);
1776 r = of_find_regulator_by_node(node);
1781 * We have a node, but there is no device.
1782 * assume it has not registered yet.
1784 return ERR_PTR(-EPROBE_DEFER);
1788 /* if not found, try doing it non-dt way */
1790 devname = dev_name(dev);
1792 mutex_lock(®ulator_list_mutex);
1793 list_for_each_entry(map, ®ulator_map_list, list) {
1794 /* If the mapping has a device set up it must match */
1795 if (map->dev_name &&
1796 (!devname || strcmp(map->dev_name, devname)))
1799 if (strcmp(map->supply, supply) == 0 &&
1800 get_device(&map->regulator->dev)) {
1805 mutex_unlock(®ulator_list_mutex);
1810 r = regulator_lookup_by_name(supply);
1814 return ERR_PTR(-ENODEV);
1817 static int regulator_resolve_supply(struct regulator_dev *rdev)
1819 struct regulator_dev *r;
1820 struct device *dev = rdev->dev.parent;
1823 /* No supply to resolve? */
1824 if (!rdev->supply_name)
1827 /* Supply already resolved? (fast-path without locking contention) */
1831 r = regulator_dev_lookup(dev, rdev->supply_name);
1835 /* Did the lookup explicitly defer for us? */
1836 if (ret == -EPROBE_DEFER)
1839 if (have_full_constraints()) {
1840 r = dummy_regulator_rdev;
1841 get_device(&r->dev);
1843 dev_err(dev, "Failed to resolve %s-supply for %s\n",
1844 rdev->supply_name, rdev->desc->name);
1845 ret = -EPROBE_DEFER;
1851 dev_err(dev, "Supply for %s (%s) resolved to itself\n",
1852 rdev->desc->name, rdev->supply_name);
1853 if (!have_full_constraints()) {
1857 r = dummy_regulator_rdev;
1858 get_device(&r->dev);
1862 * If the supply's parent device is not the same as the
1863 * regulator's parent device, then ensure the parent device
1864 * is bound before we resolve the supply, in case the parent
1865 * device get probe deferred and unregisters the supply.
1867 if (r->dev.parent && r->dev.parent != rdev->dev.parent) {
1868 if (!device_is_bound(r->dev.parent)) {
1869 put_device(&r->dev);
1870 ret = -EPROBE_DEFER;
1875 /* Recursively resolve the supply of the supply */
1876 ret = regulator_resolve_supply(r);
1878 put_device(&r->dev);
1883 * Recheck rdev->supply with rdev->mutex lock held to avoid a race
1884 * between rdev->supply null check and setting rdev->supply in
1885 * set_supply() from concurrent tasks.
1887 regulator_lock(rdev);
1889 /* Supply just resolved by a concurrent task? */
1891 regulator_unlock(rdev);
1892 put_device(&r->dev);
1896 ret = set_supply(rdev, r);
1898 regulator_unlock(rdev);
1899 put_device(&r->dev);
1903 regulator_unlock(rdev);
1906 * In set_machine_constraints() we may have turned this regulator on
1907 * but we couldn't propagate to the supply if it hadn't been resolved
1910 if (rdev->use_count) {
1911 ret = regulator_enable(rdev->supply);
1913 _regulator_put(rdev->supply);
1914 rdev->supply = NULL;
1923 /* Internal regulator request function */
1924 struct regulator *_regulator_get(struct device *dev, const char *id,
1925 enum regulator_get_type get_type)
1927 struct regulator_dev *rdev;
1928 struct regulator *regulator;
1929 struct device_link *link;
1932 if (get_type >= MAX_GET_TYPE) {
1933 dev_err(dev, "invalid type %d in %s\n", get_type, __func__);
1934 return ERR_PTR(-EINVAL);
1938 pr_err("get() with no identifier\n");
1939 return ERR_PTR(-EINVAL);
1942 rdev = regulator_dev_lookup(dev, id);
1944 ret = PTR_ERR(rdev);
1947 * If regulator_dev_lookup() fails with error other
1948 * than -ENODEV our job here is done, we simply return it.
1951 return ERR_PTR(ret);
1953 if (!have_full_constraints()) {
1955 "incomplete constraints, dummy supplies not allowed\n");
1956 return ERR_PTR(-ENODEV);
1962 * Assume that a regulator is physically present and
1963 * enabled, even if it isn't hooked up, and just
1966 dev_warn(dev, "supply %s not found, using dummy regulator\n", id);
1967 rdev = dummy_regulator_rdev;
1968 get_device(&rdev->dev);
1973 "dummy supplies not allowed for exclusive requests\n");
1977 return ERR_PTR(-ENODEV);
1981 if (rdev->exclusive) {
1982 regulator = ERR_PTR(-EPERM);
1983 put_device(&rdev->dev);
1987 if (get_type == EXCLUSIVE_GET && rdev->open_count) {
1988 regulator = ERR_PTR(-EBUSY);
1989 put_device(&rdev->dev);
1993 mutex_lock(®ulator_list_mutex);
1994 ret = (rdev->coupling_desc.n_resolved != rdev->coupling_desc.n_coupled);
1995 mutex_unlock(®ulator_list_mutex);
1998 regulator = ERR_PTR(-EPROBE_DEFER);
1999 put_device(&rdev->dev);
2003 ret = regulator_resolve_supply(rdev);
2005 regulator = ERR_PTR(ret);
2006 put_device(&rdev->dev);
2010 if (!try_module_get(rdev->owner)) {
2011 regulator = ERR_PTR(-EPROBE_DEFER);
2012 put_device(&rdev->dev);
2016 regulator = create_regulator(rdev, dev, id);
2017 if (regulator == NULL) {
2018 regulator = ERR_PTR(-ENOMEM);
2019 module_put(rdev->owner);
2020 put_device(&rdev->dev);
2025 if (get_type == EXCLUSIVE_GET) {
2026 rdev->exclusive = 1;
2028 ret = _regulator_is_enabled(rdev);
2030 rdev->use_count = 1;
2032 rdev->use_count = 0;
2035 link = device_link_add(dev, &rdev->dev, DL_FLAG_STATELESS);
2036 if (!IS_ERR_OR_NULL(link))
2037 regulator->device_link = true;
2043 * regulator_get - lookup and obtain a reference to a regulator.
2044 * @dev: device for regulator "consumer"
2045 * @id: Supply name or regulator ID.
2047 * Returns a struct regulator corresponding to the regulator producer,
2048 * or IS_ERR() condition containing errno.
2050 * Use of supply names configured via set_consumer_device_supply() is
2051 * strongly encouraged. It is recommended that the supply name used
2052 * should match the name used for the supply and/or the relevant
2053 * device pins in the datasheet.
2055 struct regulator *regulator_get(struct device *dev, const char *id)
2057 return _regulator_get(dev, id, NORMAL_GET);
2059 EXPORT_SYMBOL_GPL(regulator_get);
2062 * regulator_get_exclusive - obtain exclusive access to a regulator.
2063 * @dev: device for regulator "consumer"
2064 * @id: Supply name or regulator ID.
2066 * Returns a struct regulator corresponding to the regulator producer,
2067 * or IS_ERR() condition containing errno. Other consumers will be
2068 * unable to obtain this regulator while this reference is held and the
2069 * use count for the regulator will be initialised to reflect the current
2070 * state of the regulator.
2072 * This is intended for use by consumers which cannot tolerate shared
2073 * use of the regulator such as those which need to force the
2074 * regulator off for correct operation of the hardware they are
2077 * Use of supply names configured via set_consumer_device_supply() is
2078 * strongly encouraged. It is recommended that the supply name used
2079 * should match the name used for the supply and/or the relevant
2080 * device pins in the datasheet.
2082 struct regulator *regulator_get_exclusive(struct device *dev, const char *id)
2084 return _regulator_get(dev, id, EXCLUSIVE_GET);
2086 EXPORT_SYMBOL_GPL(regulator_get_exclusive);
2089 * regulator_get_optional - obtain optional access to a regulator.
2090 * @dev: device for regulator "consumer"
2091 * @id: Supply name or regulator ID.
2093 * Returns a struct regulator corresponding to the regulator producer,
2094 * or IS_ERR() condition containing errno.
2096 * This is intended for use by consumers for devices which can have
2097 * some supplies unconnected in normal use, such as some MMC devices.
2098 * It can allow the regulator core to provide stub supplies for other
2099 * supplies requested using normal regulator_get() calls without
2100 * disrupting the operation of drivers that can handle absent
2103 * Use of supply names configured via set_consumer_device_supply() is
2104 * strongly encouraged. It is recommended that the supply name used
2105 * should match the name used for the supply and/or the relevant
2106 * device pins in the datasheet.
2108 struct regulator *regulator_get_optional(struct device *dev, const char *id)
2110 return _regulator_get(dev, id, OPTIONAL_GET);
2112 EXPORT_SYMBOL_GPL(regulator_get_optional);
2114 static void destroy_regulator(struct regulator *regulator)
2116 struct regulator_dev *rdev = regulator->rdev;
2118 debugfs_remove_recursive(regulator->debugfs);
2120 if (regulator->dev) {
2121 if (regulator->device_link)
2122 device_link_remove(regulator->dev, &rdev->dev);
2124 /* remove any sysfs entries */
2125 sysfs_remove_link(&rdev->dev.kobj, regulator->supply_name);
2128 regulator_lock(rdev);
2129 list_del(®ulator->list);
2132 rdev->exclusive = 0;
2133 regulator_unlock(rdev);
2135 kfree_const(regulator->supply_name);
2139 /* regulator_list_mutex lock held by regulator_put() */
2140 static void _regulator_put(struct regulator *regulator)
2142 struct regulator_dev *rdev;
2144 if (IS_ERR_OR_NULL(regulator))
2147 lockdep_assert_held_once(®ulator_list_mutex);
2149 /* Docs say you must disable before calling regulator_put() */
2150 WARN_ON(regulator->enable_count);
2152 rdev = regulator->rdev;
2154 destroy_regulator(regulator);
2156 module_put(rdev->owner);
2157 put_device(&rdev->dev);
2161 * regulator_put - "free" the regulator source
2162 * @regulator: regulator source
2164 * Note: drivers must ensure that all regulator_enable calls made on this
2165 * regulator source are balanced by regulator_disable calls prior to calling
2168 void regulator_put(struct regulator *regulator)
2170 mutex_lock(®ulator_list_mutex);
2171 _regulator_put(regulator);
2172 mutex_unlock(®ulator_list_mutex);
2174 EXPORT_SYMBOL_GPL(regulator_put);
2177 * regulator_register_supply_alias - Provide device alias for supply lookup
2179 * @dev: device that will be given as the regulator "consumer"
2180 * @id: Supply name or regulator ID
2181 * @alias_dev: device that should be used to lookup the supply
2182 * @alias_id: Supply name or regulator ID that should be used to lookup the
2185 * All lookups for id on dev will instead be conducted for alias_id on
2188 int regulator_register_supply_alias(struct device *dev, const char *id,
2189 struct device *alias_dev,
2190 const char *alias_id)
2192 struct regulator_supply_alias *map;
2194 map = regulator_find_supply_alias(dev, id);
2198 map = kzalloc(sizeof(struct regulator_supply_alias), GFP_KERNEL);
2203 map->src_supply = id;
2204 map->alias_dev = alias_dev;
2205 map->alias_supply = alias_id;
2207 list_add(&map->list, ®ulator_supply_alias_list);
2209 pr_info("Adding alias for supply %s,%s -> %s,%s\n",
2210 id, dev_name(dev), alias_id, dev_name(alias_dev));
2214 EXPORT_SYMBOL_GPL(regulator_register_supply_alias);
2217 * regulator_unregister_supply_alias - Remove device alias
2219 * @dev: device that will be given as the regulator "consumer"
2220 * @id: Supply name or regulator ID
2222 * Remove a lookup alias if one exists for id on dev.
2224 void regulator_unregister_supply_alias(struct device *dev, const char *id)
2226 struct regulator_supply_alias *map;
2228 map = regulator_find_supply_alias(dev, id);
2230 list_del(&map->list);
2234 EXPORT_SYMBOL_GPL(regulator_unregister_supply_alias);
2237 * regulator_bulk_register_supply_alias - register multiple aliases
2239 * @dev: device that will be given as the regulator "consumer"
2240 * @id: List of supply names or regulator IDs
2241 * @alias_dev: device that should be used to lookup the supply
2242 * @alias_id: List of supply names or regulator IDs that should be used to
2244 * @num_id: Number of aliases to register
2246 * @return 0 on success, an errno on failure.
2248 * This helper function allows drivers to register several supply
2249 * aliases in one operation. If any of the aliases cannot be
2250 * registered any aliases that were registered will be removed
2251 * before returning to the caller.
2253 int regulator_bulk_register_supply_alias(struct device *dev,
2254 const char *const *id,
2255 struct device *alias_dev,
2256 const char *const *alias_id,
2262 for (i = 0; i < num_id; ++i) {
2263 ret = regulator_register_supply_alias(dev, id[i], alias_dev,
2273 "Failed to create supply alias %s,%s -> %s,%s\n",
2274 id[i], dev_name(dev), alias_id[i], dev_name(alias_dev));
2277 regulator_unregister_supply_alias(dev, id[i]);
2281 EXPORT_SYMBOL_GPL(regulator_bulk_register_supply_alias);
2284 * regulator_bulk_unregister_supply_alias - unregister multiple aliases
2286 * @dev: device that will be given as the regulator "consumer"
2287 * @id: List of supply names or regulator IDs
2288 * @num_id: Number of aliases to unregister
2290 * This helper function allows drivers to unregister several supply
2291 * aliases in one operation.
2293 void regulator_bulk_unregister_supply_alias(struct device *dev,
2294 const char *const *id,
2299 for (i = 0; i < num_id; ++i)
2300 regulator_unregister_supply_alias(dev, id[i]);
2302 EXPORT_SYMBOL_GPL(regulator_bulk_unregister_supply_alias);
2305 /* Manage enable GPIO list. Same GPIO pin can be shared among regulators */
2306 static int regulator_ena_gpio_request(struct regulator_dev *rdev,
2307 const struct regulator_config *config)
2309 struct regulator_enable_gpio *pin, *new_pin;
2310 struct gpio_desc *gpiod;
2312 gpiod = config->ena_gpiod;
2313 new_pin = kzalloc(sizeof(*new_pin), GFP_KERNEL);
2315 mutex_lock(®ulator_list_mutex);
2317 list_for_each_entry(pin, ®ulator_ena_gpio_list, list) {
2318 if (pin->gpiod == gpiod) {
2319 rdev_dbg(rdev, "GPIO is already used\n");
2320 goto update_ena_gpio_to_rdev;
2324 if (new_pin == NULL) {
2325 mutex_unlock(®ulator_list_mutex);
2333 list_add(&pin->list, ®ulator_ena_gpio_list);
2335 update_ena_gpio_to_rdev:
2336 pin->request_count++;
2337 rdev->ena_pin = pin;
2339 mutex_unlock(®ulator_list_mutex);
2345 static void regulator_ena_gpio_free(struct regulator_dev *rdev)
2347 struct regulator_enable_gpio *pin, *n;
2352 /* Free the GPIO only in case of no use */
2353 list_for_each_entry_safe(pin, n, ®ulator_ena_gpio_list, list) {
2354 if (pin != rdev->ena_pin)
2357 if (--pin->request_count)
2360 gpiod_put(pin->gpiod);
2361 list_del(&pin->list);
2366 rdev->ena_pin = NULL;
2370 * regulator_ena_gpio_ctrl - balance enable_count of each GPIO and actual GPIO pin control
2371 * @rdev: regulator_dev structure
2372 * @enable: enable GPIO at initial use?
2374 * GPIO is enabled in case of initial use. (enable_count is 0)
2375 * GPIO is disabled when it is not shared any more. (enable_count <= 1)
2377 static int regulator_ena_gpio_ctrl(struct regulator_dev *rdev, bool enable)
2379 struct regulator_enable_gpio *pin = rdev->ena_pin;
2385 /* Enable GPIO at initial use */
2386 if (pin->enable_count == 0)
2387 gpiod_set_value_cansleep(pin->gpiod, 1);
2389 pin->enable_count++;
2391 if (pin->enable_count > 1) {
2392 pin->enable_count--;
2396 /* Disable GPIO if not used */
2397 if (pin->enable_count <= 1) {
2398 gpiod_set_value_cansleep(pin->gpiod, 0);
2399 pin->enable_count = 0;
2407 * _regulator_enable_delay - a delay helper function
2408 * @delay: time to delay in microseconds
2410 * Delay for the requested amount of time as per the guidelines in:
2412 * Documentation/timers/timers-howto.rst
2414 * The assumption here is that regulators will never be enabled in
2415 * atomic context and therefore sleeping functions can be used.
2417 static void _regulator_enable_delay(unsigned int delay)
2419 unsigned int ms = delay / 1000;
2420 unsigned int us = delay % 1000;
2424 * For small enough values, handle super-millisecond
2425 * delays in the usleep_range() call below.
2434 * Give the scheduler some room to coalesce with any other
2435 * wakeup sources. For delays shorter than 10 us, don't even
2436 * bother setting up high-resolution timers and just busy-
2440 usleep_range(us, us + 100);
2446 * _regulator_check_status_enabled
2448 * A helper function to check if the regulator status can be interpreted
2449 * as 'regulator is enabled'.
2450 * @rdev: the regulator device to check
2453 * * 1 - if status shows regulator is in enabled state
2454 * * 0 - if not enabled state
2455 * * Error Value - as received from ops->get_status()
2457 static inline int _regulator_check_status_enabled(struct regulator_dev *rdev)
2459 int ret = rdev->desc->ops->get_status(rdev);
2462 rdev_info(rdev, "get_status returned error: %d\n", ret);
2467 case REGULATOR_STATUS_OFF:
2468 case REGULATOR_STATUS_ERROR:
2469 case REGULATOR_STATUS_UNDEFINED:
2476 static int _regulator_do_enable(struct regulator_dev *rdev)
2480 /* Query before enabling in case configuration dependent. */
2481 ret = _regulator_get_enable_time(rdev);
2485 rdev_warn(rdev, "enable_time() failed: %pe\n", ERR_PTR(ret));
2489 trace_regulator_enable(rdev_get_name(rdev));
2491 if (rdev->desc->off_on_delay && rdev->last_off) {
2492 /* if needed, keep a distance of off_on_delay from last time
2493 * this regulator was disabled.
2495 ktime_t end = ktime_add_us(rdev->last_off, rdev->desc->off_on_delay);
2496 s64 remaining = ktime_us_delta(end, ktime_get());
2499 _regulator_enable_delay(remaining);
2502 if (rdev->ena_pin) {
2503 if (!rdev->ena_gpio_state) {
2504 ret = regulator_ena_gpio_ctrl(rdev, true);
2507 rdev->ena_gpio_state = 1;
2509 } else if (rdev->desc->ops->enable) {
2510 ret = rdev->desc->ops->enable(rdev);
2517 /* Allow the regulator to ramp; it would be useful to extend
2518 * this for bulk operations so that the regulators can ramp
2521 trace_regulator_enable_delay(rdev_get_name(rdev));
2523 /* If poll_enabled_time is set, poll upto the delay calculated
2524 * above, delaying poll_enabled_time uS to check if the regulator
2525 * actually got enabled.
2526 * If the regulator isn't enabled after enable_delay has
2527 * expired, return -ETIMEDOUT.
2529 if (rdev->desc->poll_enabled_time) {
2530 unsigned int time_remaining = delay;
2532 while (time_remaining > 0) {
2533 _regulator_enable_delay(rdev->desc->poll_enabled_time);
2535 if (rdev->desc->ops->get_status) {
2536 ret = _regulator_check_status_enabled(rdev);
2541 } else if (rdev->desc->ops->is_enabled(rdev))
2544 time_remaining -= rdev->desc->poll_enabled_time;
2547 if (time_remaining <= 0) {
2548 rdev_err(rdev, "Enabled check timed out\n");
2552 _regulator_enable_delay(delay);
2555 trace_regulator_enable_complete(rdev_get_name(rdev));
2561 * _regulator_handle_consumer_enable - handle that a consumer enabled
2562 * @regulator: regulator source
2564 * Some things on a regulator consumer (like the contribution towards total
2565 * load on the regulator) only have an effect when the consumer wants the
2566 * regulator enabled. Explained in example with two consumers of the same
2568 * consumer A: set_load(100); => total load = 0
2569 * consumer A: regulator_enable(); => total load = 100
2570 * consumer B: set_load(1000); => total load = 100
2571 * consumer B: regulator_enable(); => total load = 1100
2572 * consumer A: regulator_disable(); => total_load = 1000
2574 * This function (together with _regulator_handle_consumer_disable) is
2575 * responsible for keeping track of the refcount for a given regulator consumer
2576 * and applying / unapplying these things.
2578 * Returns 0 upon no error; -error upon error.
2580 static int _regulator_handle_consumer_enable(struct regulator *regulator)
2582 struct regulator_dev *rdev = regulator->rdev;
2584 lockdep_assert_held_once(&rdev->mutex.base);
2586 regulator->enable_count++;
2587 if (regulator->uA_load && regulator->enable_count == 1)
2588 return drms_uA_update(rdev);
2594 * _regulator_handle_consumer_disable - handle that a consumer disabled
2595 * @regulator: regulator source
2597 * The opposite of _regulator_handle_consumer_enable().
2599 * Returns 0 upon no error; -error upon error.
2601 static int _regulator_handle_consumer_disable(struct regulator *regulator)
2603 struct regulator_dev *rdev = regulator->rdev;
2605 lockdep_assert_held_once(&rdev->mutex.base);
2607 if (!regulator->enable_count) {
2608 rdev_err(rdev, "Underflow of regulator enable count\n");
2612 regulator->enable_count--;
2613 if (regulator->uA_load && regulator->enable_count == 0)
2614 return drms_uA_update(rdev);
2619 /* locks held by regulator_enable() */
2620 static int _regulator_enable(struct regulator *regulator)
2622 struct regulator_dev *rdev = regulator->rdev;
2625 lockdep_assert_held_once(&rdev->mutex.base);
2627 if (rdev->use_count == 0 && rdev->supply) {
2628 ret = _regulator_enable(rdev->supply);
2633 /* balance only if there are regulators coupled */
2634 if (rdev->coupling_desc.n_coupled > 1) {
2635 ret = regulator_balance_voltage(rdev, PM_SUSPEND_ON);
2637 goto err_disable_supply;
2640 ret = _regulator_handle_consumer_enable(regulator);
2642 goto err_disable_supply;
2644 if (rdev->use_count == 0) {
2646 * The regulator may already be enabled if it's not switchable
2649 ret = _regulator_is_enabled(rdev);
2650 if (ret == -EINVAL || ret == 0) {
2651 if (!regulator_ops_is_valid(rdev,
2652 REGULATOR_CHANGE_STATUS)) {
2654 goto err_consumer_disable;
2657 ret = _regulator_do_enable(rdev);
2659 goto err_consumer_disable;
2661 _notifier_call_chain(rdev, REGULATOR_EVENT_ENABLE,
2663 } else if (ret < 0) {
2664 rdev_err(rdev, "is_enabled() failed: %pe\n", ERR_PTR(ret));
2665 goto err_consumer_disable;
2667 /* Fallthrough on positive return values - already enabled */
2674 err_consumer_disable:
2675 _regulator_handle_consumer_disable(regulator);
2678 if (rdev->use_count == 0 && rdev->supply)
2679 _regulator_disable(rdev->supply);
2685 * regulator_enable - enable regulator output
2686 * @regulator: regulator source
2688 * Request that the regulator be enabled with the regulator output at
2689 * the predefined voltage or current value. Calls to regulator_enable()
2690 * must be balanced with calls to regulator_disable().
2692 * NOTE: the output value can be set by other drivers, boot loader or may be
2693 * hardwired in the regulator.
2695 int regulator_enable(struct regulator *regulator)
2697 struct regulator_dev *rdev = regulator->rdev;
2698 struct ww_acquire_ctx ww_ctx;
2701 regulator_lock_dependent(rdev, &ww_ctx);
2702 ret = _regulator_enable(regulator);
2703 regulator_unlock_dependent(rdev, &ww_ctx);
2707 EXPORT_SYMBOL_GPL(regulator_enable);
2709 static int _regulator_do_disable(struct regulator_dev *rdev)
2713 trace_regulator_disable(rdev_get_name(rdev));
2715 if (rdev->ena_pin) {
2716 if (rdev->ena_gpio_state) {
2717 ret = regulator_ena_gpio_ctrl(rdev, false);
2720 rdev->ena_gpio_state = 0;
2723 } else if (rdev->desc->ops->disable) {
2724 ret = rdev->desc->ops->disable(rdev);
2729 if (rdev->desc->off_on_delay)
2730 rdev->last_off = ktime_get();
2732 trace_regulator_disable_complete(rdev_get_name(rdev));
2737 /* locks held by regulator_disable() */
2738 static int _regulator_disable(struct regulator *regulator)
2740 struct regulator_dev *rdev = regulator->rdev;
2743 lockdep_assert_held_once(&rdev->mutex.base);
2745 if (WARN(rdev->use_count <= 0,
2746 "unbalanced disables for %s\n", rdev_get_name(rdev)))
2749 /* are we the last user and permitted to disable ? */
2750 if (rdev->use_count == 1 &&
2751 (rdev->constraints && !rdev->constraints->always_on)) {
2753 /* we are last user */
2754 if (regulator_ops_is_valid(rdev, REGULATOR_CHANGE_STATUS)) {
2755 ret = _notifier_call_chain(rdev,
2756 REGULATOR_EVENT_PRE_DISABLE,
2758 if (ret & NOTIFY_STOP_MASK)
2761 ret = _regulator_do_disable(rdev);
2763 rdev_err(rdev, "failed to disable: %pe\n", ERR_PTR(ret));
2764 _notifier_call_chain(rdev,
2765 REGULATOR_EVENT_ABORT_DISABLE,
2769 _notifier_call_chain(rdev, REGULATOR_EVENT_DISABLE,
2773 rdev->use_count = 0;
2774 } else if (rdev->use_count > 1) {
2779 ret = _regulator_handle_consumer_disable(regulator);
2781 if (ret == 0 && rdev->coupling_desc.n_coupled > 1)
2782 ret = regulator_balance_voltage(rdev, PM_SUSPEND_ON);
2784 if (ret == 0 && rdev->use_count == 0 && rdev->supply)
2785 ret = _regulator_disable(rdev->supply);
2791 * regulator_disable - disable regulator output
2792 * @regulator: regulator source
2794 * Disable the regulator output voltage or current. Calls to
2795 * regulator_enable() must be balanced with calls to
2796 * regulator_disable().
2798 * NOTE: this will only disable the regulator output if no other consumer
2799 * devices have it enabled, the regulator device supports disabling and
2800 * machine constraints permit this operation.
2802 int regulator_disable(struct regulator *regulator)
2804 struct regulator_dev *rdev = regulator->rdev;
2805 struct ww_acquire_ctx ww_ctx;
2808 regulator_lock_dependent(rdev, &ww_ctx);
2809 ret = _regulator_disable(regulator);
2810 regulator_unlock_dependent(rdev, &ww_ctx);
2814 EXPORT_SYMBOL_GPL(regulator_disable);
2816 /* locks held by regulator_force_disable() */
2817 static int _regulator_force_disable(struct regulator_dev *rdev)
2821 lockdep_assert_held_once(&rdev->mutex.base);
2823 ret = _notifier_call_chain(rdev, REGULATOR_EVENT_FORCE_DISABLE |
2824 REGULATOR_EVENT_PRE_DISABLE, NULL);
2825 if (ret & NOTIFY_STOP_MASK)
2828 ret = _regulator_do_disable(rdev);
2830 rdev_err(rdev, "failed to force disable: %pe\n", ERR_PTR(ret));
2831 _notifier_call_chain(rdev, REGULATOR_EVENT_FORCE_DISABLE |
2832 REGULATOR_EVENT_ABORT_DISABLE, NULL);
2836 _notifier_call_chain(rdev, REGULATOR_EVENT_FORCE_DISABLE |
2837 REGULATOR_EVENT_DISABLE, NULL);
2843 * regulator_force_disable - force disable regulator output
2844 * @regulator: regulator source
2846 * Forcibly disable the regulator output voltage or current.
2847 * NOTE: this *will* disable the regulator output even if other consumer
2848 * devices have it enabled. This should be used for situations when device
2849 * damage will likely occur if the regulator is not disabled (e.g. over temp).
2851 int regulator_force_disable(struct regulator *regulator)
2853 struct regulator_dev *rdev = regulator->rdev;
2854 struct ww_acquire_ctx ww_ctx;
2857 regulator_lock_dependent(rdev, &ww_ctx);
2859 ret = _regulator_force_disable(regulator->rdev);
2861 if (rdev->coupling_desc.n_coupled > 1)
2862 regulator_balance_voltage(rdev, PM_SUSPEND_ON);
2864 if (regulator->uA_load) {
2865 regulator->uA_load = 0;
2866 ret = drms_uA_update(rdev);
2869 if (rdev->use_count != 0 && rdev->supply)
2870 _regulator_disable(rdev->supply);
2872 regulator_unlock_dependent(rdev, &ww_ctx);
2876 EXPORT_SYMBOL_GPL(regulator_force_disable);
2878 static void regulator_disable_work(struct work_struct *work)
2880 struct regulator_dev *rdev = container_of(work, struct regulator_dev,
2882 struct ww_acquire_ctx ww_ctx;
2884 struct regulator *regulator;
2885 int total_count = 0;
2887 regulator_lock_dependent(rdev, &ww_ctx);
2890 * Workqueue functions queue the new work instance while the previous
2891 * work instance is being processed. Cancel the queued work instance
2892 * as the work instance under processing does the job of the queued
2895 cancel_delayed_work(&rdev->disable_work);
2897 list_for_each_entry(regulator, &rdev->consumer_list, list) {
2898 count = regulator->deferred_disables;
2903 total_count += count;
2904 regulator->deferred_disables = 0;
2906 for (i = 0; i < count; i++) {
2907 ret = _regulator_disable(regulator);
2909 rdev_err(rdev, "Deferred disable failed: %pe\n",
2913 WARN_ON(!total_count);
2915 if (rdev->coupling_desc.n_coupled > 1)
2916 regulator_balance_voltage(rdev, PM_SUSPEND_ON);
2918 regulator_unlock_dependent(rdev, &ww_ctx);
2922 * regulator_disable_deferred - disable regulator output with delay
2923 * @regulator: regulator source
2924 * @ms: milliseconds until the regulator is disabled
2926 * Execute regulator_disable() on the regulator after a delay. This
2927 * is intended for use with devices that require some time to quiesce.
2929 * NOTE: this will only disable the regulator output if no other consumer
2930 * devices have it enabled, the regulator device supports disabling and
2931 * machine constraints permit this operation.
2933 int regulator_disable_deferred(struct regulator *regulator, int ms)
2935 struct regulator_dev *rdev = regulator->rdev;
2938 return regulator_disable(regulator);
2940 regulator_lock(rdev);
2941 regulator->deferred_disables++;
2942 mod_delayed_work(system_power_efficient_wq, &rdev->disable_work,
2943 msecs_to_jiffies(ms));
2944 regulator_unlock(rdev);
2948 EXPORT_SYMBOL_GPL(regulator_disable_deferred);
2950 static int _regulator_is_enabled(struct regulator_dev *rdev)
2952 /* A GPIO control always takes precedence */
2954 return rdev->ena_gpio_state;
2956 /* If we don't know then assume that the regulator is always on */
2957 if (!rdev->desc->ops->is_enabled)
2960 return rdev->desc->ops->is_enabled(rdev);
2963 static int _regulator_list_voltage(struct regulator_dev *rdev,
2964 unsigned selector, int lock)
2966 const struct regulator_ops *ops = rdev->desc->ops;
2969 if (rdev->desc->fixed_uV && rdev->desc->n_voltages == 1 && !selector)
2970 return rdev->desc->fixed_uV;
2972 if (ops->list_voltage) {
2973 if (selector >= rdev->desc->n_voltages)
2975 if (selector < rdev->desc->linear_min_sel)
2978 regulator_lock(rdev);
2979 ret = ops->list_voltage(rdev, selector);
2981 regulator_unlock(rdev);
2982 } else if (rdev->is_switch && rdev->supply) {
2983 ret = _regulator_list_voltage(rdev->supply->rdev,
2990 if (ret < rdev->constraints->min_uV)
2992 else if (ret > rdev->constraints->max_uV)
3000 * regulator_is_enabled - is the regulator output enabled
3001 * @regulator: regulator source
3003 * Returns positive if the regulator driver backing the source/client
3004 * has requested that the device be enabled, zero if it hasn't, else a
3005 * negative errno code.
3007 * Note that the device backing this regulator handle can have multiple
3008 * users, so it might be enabled even if regulator_enable() was never
3009 * called for this particular source.
3011 int regulator_is_enabled(struct regulator *regulator)
3015 if (regulator->always_on)
3018 regulator_lock(regulator->rdev);
3019 ret = _regulator_is_enabled(regulator->rdev);
3020 regulator_unlock(regulator->rdev);
3024 EXPORT_SYMBOL_GPL(regulator_is_enabled);
3027 * regulator_count_voltages - count regulator_list_voltage() selectors
3028 * @regulator: regulator source
3030 * Returns number of selectors, or negative errno. Selectors are
3031 * numbered starting at zero, and typically correspond to bitfields
3032 * in hardware registers.
3034 int regulator_count_voltages(struct regulator *regulator)
3036 struct regulator_dev *rdev = regulator->rdev;
3038 if (rdev->desc->n_voltages)
3039 return rdev->desc->n_voltages;
3041 if (!rdev->is_switch || !rdev->supply)
3044 return regulator_count_voltages(rdev->supply);
3046 EXPORT_SYMBOL_GPL(regulator_count_voltages);
3049 * regulator_list_voltage - enumerate supported voltages
3050 * @regulator: regulator source
3051 * @selector: identify voltage to list
3052 * Context: can sleep
3054 * Returns a voltage that can be passed to @regulator_set_voltage(),
3055 * zero if this selector code can't be used on this system, or a
3058 int regulator_list_voltage(struct regulator *regulator, unsigned selector)
3060 return _regulator_list_voltage(regulator->rdev, selector, 1);
3062 EXPORT_SYMBOL_GPL(regulator_list_voltage);
3065 * regulator_get_regmap - get the regulator's register map
3066 * @regulator: regulator source
3068 * Returns the register map for the given regulator, or an ERR_PTR value
3069 * if the regulator doesn't use regmap.
3071 struct regmap *regulator_get_regmap(struct regulator *regulator)
3073 struct regmap *map = regulator->rdev->regmap;
3075 return map ? map : ERR_PTR(-EOPNOTSUPP);
3079 * regulator_get_hardware_vsel_register - get the HW voltage selector register
3080 * @regulator: regulator source
3081 * @vsel_reg: voltage selector register, output parameter
3082 * @vsel_mask: mask for voltage selector bitfield, output parameter
3084 * Returns the hardware register offset and bitmask used for setting the
3085 * regulator voltage. This might be useful when configuring voltage-scaling
3086 * hardware or firmware that can make I2C requests behind the kernel's back,
3089 * On success, the output parameters @vsel_reg and @vsel_mask are filled in
3090 * and 0 is returned, otherwise a negative errno is returned.
3092 int regulator_get_hardware_vsel_register(struct regulator *regulator,
3094 unsigned *vsel_mask)
3096 struct regulator_dev *rdev = regulator->rdev;
3097 const struct regulator_ops *ops = rdev->desc->ops;
3099 if (ops->set_voltage_sel != regulator_set_voltage_sel_regmap)
3102 *vsel_reg = rdev->desc->vsel_reg;
3103 *vsel_mask = rdev->desc->vsel_mask;
3107 EXPORT_SYMBOL_GPL(regulator_get_hardware_vsel_register);
3110 * regulator_list_hardware_vsel - get the HW-specific register value for a selector
3111 * @regulator: regulator source
3112 * @selector: identify voltage to list
3114 * Converts the selector to a hardware-specific voltage selector that can be
3115 * directly written to the regulator registers. The address of the voltage
3116 * register can be determined by calling @regulator_get_hardware_vsel_register.
3118 * On error a negative errno is returned.
3120 int regulator_list_hardware_vsel(struct regulator *regulator,
3123 struct regulator_dev *rdev = regulator->rdev;
3124 const struct regulator_ops *ops = rdev->desc->ops;
3126 if (selector >= rdev->desc->n_voltages)
3128 if (selector < rdev->desc->linear_min_sel)
3130 if (ops->set_voltage_sel != regulator_set_voltage_sel_regmap)
3135 EXPORT_SYMBOL_GPL(regulator_list_hardware_vsel);
3138 * regulator_get_linear_step - return the voltage step size between VSEL values
3139 * @regulator: regulator source
3141 * Returns the voltage step size between VSEL values for linear
3142 * regulators, or return 0 if the regulator isn't a linear regulator.
3144 unsigned int regulator_get_linear_step(struct regulator *regulator)
3146 struct regulator_dev *rdev = regulator->rdev;
3148 return rdev->desc->uV_step;
3150 EXPORT_SYMBOL_GPL(regulator_get_linear_step);
3153 * regulator_is_supported_voltage - check if a voltage range can be supported
3155 * @regulator: Regulator to check.
3156 * @min_uV: Minimum required voltage in uV.
3157 * @max_uV: Maximum required voltage in uV.
3159 * Returns a boolean.
3161 int regulator_is_supported_voltage(struct regulator *regulator,
3162 int min_uV, int max_uV)
3164 struct regulator_dev *rdev = regulator->rdev;
3165 int i, voltages, ret;
3167 /* If we can't change voltage check the current voltage */
3168 if (!regulator_ops_is_valid(rdev, REGULATOR_CHANGE_VOLTAGE)) {
3169 ret = regulator_get_voltage(regulator);
3171 return min_uV <= ret && ret <= max_uV;
3176 /* Any voltage within constrains range is fine? */
3177 if (rdev->desc->continuous_voltage_range)
3178 return min_uV >= rdev->constraints->min_uV &&
3179 max_uV <= rdev->constraints->max_uV;
3181 ret = regulator_count_voltages(regulator);
3186 for (i = 0; i < voltages; i++) {
3187 ret = regulator_list_voltage(regulator, i);
3189 if (ret >= min_uV && ret <= max_uV)
3195 EXPORT_SYMBOL_GPL(regulator_is_supported_voltage);
3197 static int regulator_map_voltage(struct regulator_dev *rdev, int min_uV,
3200 const struct regulator_desc *desc = rdev->desc;
3202 if (desc->ops->map_voltage)
3203 return desc->ops->map_voltage(rdev, min_uV, max_uV);
3205 if (desc->ops->list_voltage == regulator_list_voltage_linear)
3206 return regulator_map_voltage_linear(rdev, min_uV, max_uV);
3208 if (desc->ops->list_voltage == regulator_list_voltage_linear_range)
3209 return regulator_map_voltage_linear_range(rdev, min_uV, max_uV);
3211 if (desc->ops->list_voltage ==
3212 regulator_list_voltage_pickable_linear_range)
3213 return regulator_map_voltage_pickable_linear_range(rdev,
3216 return regulator_map_voltage_iterate(rdev, min_uV, max_uV);
3219 static int _regulator_call_set_voltage(struct regulator_dev *rdev,
3220 int min_uV, int max_uV,
3223 struct pre_voltage_change_data data;
3226 data.old_uV = regulator_get_voltage_rdev(rdev);
3227 data.min_uV = min_uV;
3228 data.max_uV = max_uV;
3229 ret = _notifier_call_chain(rdev, REGULATOR_EVENT_PRE_VOLTAGE_CHANGE,
3231 if (ret & NOTIFY_STOP_MASK)
3234 ret = rdev->desc->ops->set_voltage(rdev, min_uV, max_uV, selector);
3238 _notifier_call_chain(rdev, REGULATOR_EVENT_ABORT_VOLTAGE_CHANGE,
3239 (void *)data.old_uV);
3244 static int _regulator_call_set_voltage_sel(struct regulator_dev *rdev,
3245 int uV, unsigned selector)
3247 struct pre_voltage_change_data data;
3250 data.old_uV = regulator_get_voltage_rdev(rdev);
3253 ret = _notifier_call_chain(rdev, REGULATOR_EVENT_PRE_VOLTAGE_CHANGE,
3255 if (ret & NOTIFY_STOP_MASK)
3258 ret = rdev->desc->ops->set_voltage_sel(rdev, selector);
3262 _notifier_call_chain(rdev, REGULATOR_EVENT_ABORT_VOLTAGE_CHANGE,
3263 (void *)data.old_uV);
3268 static int _regulator_set_voltage_sel_step(struct regulator_dev *rdev,
3269 int uV, int new_selector)
3271 const struct regulator_ops *ops = rdev->desc->ops;
3272 int diff, old_sel, curr_sel, ret;
3274 /* Stepping is only needed if the regulator is enabled. */
3275 if (!_regulator_is_enabled(rdev))
3278 if (!ops->get_voltage_sel)
3281 old_sel = ops->get_voltage_sel(rdev);
3285 diff = new_selector - old_sel;
3287 return 0; /* No change needed. */
3291 for (curr_sel = old_sel + rdev->desc->vsel_step;
3292 curr_sel < new_selector;
3293 curr_sel += rdev->desc->vsel_step) {
3295 * Call the callback directly instead of using
3296 * _regulator_call_set_voltage_sel() as we don't
3297 * want to notify anyone yet. Same in the branch
3300 ret = ops->set_voltage_sel(rdev, curr_sel);
3305 /* Stepping down. */
3306 for (curr_sel = old_sel - rdev->desc->vsel_step;
3307 curr_sel > new_selector;
3308 curr_sel -= rdev->desc->vsel_step) {
3309 ret = ops->set_voltage_sel(rdev, curr_sel);
3316 /* The final selector will trigger the notifiers. */
3317 return _regulator_call_set_voltage_sel(rdev, uV, new_selector);
3321 * At least try to return to the previous voltage if setting a new
3324 (void)ops->set_voltage_sel(rdev, old_sel);
3328 static int _regulator_set_voltage_time(struct regulator_dev *rdev,
3329 int old_uV, int new_uV)
3331 unsigned int ramp_delay = 0;
3333 if (rdev->constraints->ramp_delay)
3334 ramp_delay = rdev->constraints->ramp_delay;
3335 else if (rdev->desc->ramp_delay)
3336 ramp_delay = rdev->desc->ramp_delay;
3337 else if (rdev->constraints->settling_time)
3338 return rdev->constraints->settling_time;
3339 else if (rdev->constraints->settling_time_up &&
3341 return rdev->constraints->settling_time_up;
3342 else if (rdev->constraints->settling_time_down &&
3344 return rdev->constraints->settling_time_down;
3346 if (ramp_delay == 0) {
3347 rdev_dbg(rdev, "ramp_delay not set\n");
3351 return DIV_ROUND_UP(abs(new_uV - old_uV), ramp_delay);
3354 static int _regulator_do_set_voltage(struct regulator_dev *rdev,
3355 int min_uV, int max_uV)
3360 unsigned int selector;
3361 int old_selector = -1;
3362 const struct regulator_ops *ops = rdev->desc->ops;
3363 int old_uV = regulator_get_voltage_rdev(rdev);
3365 trace_regulator_set_voltage(rdev_get_name(rdev), min_uV, max_uV);
3367 min_uV += rdev->constraints->uV_offset;
3368 max_uV += rdev->constraints->uV_offset;
3371 * If we can't obtain the old selector there is not enough
3372 * info to call set_voltage_time_sel().
3374 if (_regulator_is_enabled(rdev) &&
3375 ops->set_voltage_time_sel && ops->get_voltage_sel) {
3376 old_selector = ops->get_voltage_sel(rdev);
3377 if (old_selector < 0)
3378 return old_selector;
3381 if (ops->set_voltage) {
3382 ret = _regulator_call_set_voltage(rdev, min_uV, max_uV,
3386 if (ops->list_voltage)
3387 best_val = ops->list_voltage(rdev,
3390 best_val = regulator_get_voltage_rdev(rdev);
3393 } else if (ops->set_voltage_sel) {
3394 ret = regulator_map_voltage(rdev, min_uV, max_uV);
3396 best_val = ops->list_voltage(rdev, ret);
3397 if (min_uV <= best_val && max_uV >= best_val) {
3399 if (old_selector == selector)
3401 else if (rdev->desc->vsel_step)
3402 ret = _regulator_set_voltage_sel_step(
3403 rdev, best_val, selector);
3405 ret = _regulator_call_set_voltage_sel(
3406 rdev, best_val, selector);
3418 if (ops->set_voltage_time_sel) {
3420 * Call set_voltage_time_sel if successfully obtained
3423 if (old_selector >= 0 && old_selector != selector)
3424 delay = ops->set_voltage_time_sel(rdev, old_selector,
3427 if (old_uV != best_val) {
3428 if (ops->set_voltage_time)
3429 delay = ops->set_voltage_time(rdev, old_uV,
3432 delay = _regulator_set_voltage_time(rdev,
3439 rdev_warn(rdev, "failed to get delay: %pe\n", ERR_PTR(delay));
3443 /* Insert any necessary delays */
3444 if (delay >= 1000) {
3445 mdelay(delay / 1000);
3446 udelay(delay % 1000);
3451 if (best_val >= 0) {
3452 unsigned long data = best_val;
3454 _notifier_call_chain(rdev, REGULATOR_EVENT_VOLTAGE_CHANGE,
3459 trace_regulator_set_voltage_complete(rdev_get_name(rdev), best_val);
3464 static int _regulator_do_set_suspend_voltage(struct regulator_dev *rdev,
3465 int min_uV, int max_uV, suspend_state_t state)
3467 struct regulator_state *rstate;
3470 rstate = regulator_get_suspend_state(rdev, state);
3474 if (min_uV < rstate->min_uV)
3475 min_uV = rstate->min_uV;
3476 if (max_uV > rstate->max_uV)
3477 max_uV = rstate->max_uV;
3479 sel = regulator_map_voltage(rdev, min_uV, max_uV);
3483 uV = rdev->desc->ops->list_voltage(rdev, sel);
3484 if (uV >= min_uV && uV <= max_uV)
3490 static int regulator_set_voltage_unlocked(struct regulator *regulator,
3491 int min_uV, int max_uV,
3492 suspend_state_t state)
3494 struct regulator_dev *rdev = regulator->rdev;
3495 struct regulator_voltage *voltage = ®ulator->voltage[state];
3497 int old_min_uV, old_max_uV;
3500 /* If we're setting the same range as last time the change
3501 * should be a noop (some cpufreq implementations use the same
3502 * voltage for multiple frequencies, for example).
3504 if (voltage->min_uV == min_uV && voltage->max_uV == max_uV)
3507 /* If we're trying to set a range that overlaps the current voltage,
3508 * return successfully even though the regulator does not support
3509 * changing the voltage.
3511 if (!regulator_ops_is_valid(rdev, REGULATOR_CHANGE_VOLTAGE)) {
3512 current_uV = regulator_get_voltage_rdev(rdev);
3513 if (min_uV <= current_uV && current_uV <= max_uV) {
3514 voltage->min_uV = min_uV;
3515 voltage->max_uV = max_uV;
3521 if (!rdev->desc->ops->set_voltage &&
3522 !rdev->desc->ops->set_voltage_sel) {
3527 /* constraints check */
3528 ret = regulator_check_voltage(rdev, &min_uV, &max_uV);
3532 /* restore original values in case of error */
3533 old_min_uV = voltage->min_uV;
3534 old_max_uV = voltage->max_uV;
3535 voltage->min_uV = min_uV;
3536 voltage->max_uV = max_uV;
3538 /* for not coupled regulators this will just set the voltage */
3539 ret = regulator_balance_voltage(rdev, state);
3541 voltage->min_uV = old_min_uV;
3542 voltage->max_uV = old_max_uV;
3549 int regulator_set_voltage_rdev(struct regulator_dev *rdev, int min_uV,
3550 int max_uV, suspend_state_t state)
3552 int best_supply_uV = 0;
3553 int supply_change_uV = 0;
3557 regulator_ops_is_valid(rdev->supply->rdev,
3558 REGULATOR_CHANGE_VOLTAGE) &&
3559 (rdev->desc->min_dropout_uV || !(rdev->desc->ops->get_voltage ||
3560 rdev->desc->ops->get_voltage_sel))) {
3561 int current_supply_uV;
3564 selector = regulator_map_voltage(rdev, min_uV, max_uV);
3570 best_supply_uV = _regulator_list_voltage(rdev, selector, 0);
3571 if (best_supply_uV < 0) {
3572 ret = best_supply_uV;
3576 best_supply_uV += rdev->desc->min_dropout_uV;
3578 current_supply_uV = regulator_get_voltage_rdev(rdev->supply->rdev);
3579 if (current_supply_uV < 0) {
3580 ret = current_supply_uV;
3584 supply_change_uV = best_supply_uV - current_supply_uV;
3587 if (supply_change_uV > 0) {
3588 ret = regulator_set_voltage_unlocked(rdev->supply,
3589 best_supply_uV, INT_MAX, state);
3591 dev_err(&rdev->dev, "Failed to increase supply voltage: %pe\n",
3597 if (state == PM_SUSPEND_ON)
3598 ret = _regulator_do_set_voltage(rdev, min_uV, max_uV);
3600 ret = _regulator_do_set_suspend_voltage(rdev, min_uV,
3605 if (supply_change_uV < 0) {
3606 ret = regulator_set_voltage_unlocked(rdev->supply,
3607 best_supply_uV, INT_MAX, state);
3609 dev_warn(&rdev->dev, "Failed to decrease supply voltage: %pe\n",
3611 /* No need to fail here */
3618 EXPORT_SYMBOL_GPL(regulator_set_voltage_rdev);
3620 static int regulator_limit_voltage_step(struct regulator_dev *rdev,
3621 int *current_uV, int *min_uV)
3623 struct regulation_constraints *constraints = rdev->constraints;
3625 /* Limit voltage change only if necessary */
3626 if (!constraints->max_uV_step || !_regulator_is_enabled(rdev))
3629 if (*current_uV < 0) {
3630 *current_uV = regulator_get_voltage_rdev(rdev);
3632 if (*current_uV < 0)
3636 if (abs(*current_uV - *min_uV) <= constraints->max_uV_step)
3639 /* Clamp target voltage within the given step */
3640 if (*current_uV < *min_uV)
3641 *min_uV = min(*current_uV + constraints->max_uV_step,
3644 *min_uV = max(*current_uV - constraints->max_uV_step,
3650 static int regulator_get_optimal_voltage(struct regulator_dev *rdev,
3652 int *min_uV, int *max_uV,
3653 suspend_state_t state,
3656 struct coupling_desc *c_desc = &rdev->coupling_desc;
3657 struct regulator_dev **c_rdevs = c_desc->coupled_rdevs;
3658 struct regulation_constraints *constraints = rdev->constraints;
3659 int desired_min_uV = 0, desired_max_uV = INT_MAX;
3660 int max_current_uV = 0, min_current_uV = INT_MAX;
3661 int highest_min_uV = 0, target_uV, possible_uV;
3662 int i, ret, max_spread;
3668 * If there are no coupled regulators, simply set the voltage
3669 * demanded by consumers.
3671 if (n_coupled == 1) {
3673 * If consumers don't provide any demands, set voltage
3676 desired_min_uV = constraints->min_uV;
3677 desired_max_uV = constraints->max_uV;
3679 ret = regulator_check_consumers(rdev,
3681 &desired_max_uV, state);
3685 possible_uV = desired_min_uV;
3691 /* Find highest min desired voltage */
3692 for (i = 0; i < n_coupled; i++) {
3694 int tmp_max = INT_MAX;
3696 lockdep_assert_held_once(&c_rdevs[i]->mutex.base);
3698 ret = regulator_check_consumers(c_rdevs[i],
3704 ret = regulator_check_voltage(c_rdevs[i], &tmp_min, &tmp_max);
3708 highest_min_uV = max(highest_min_uV, tmp_min);
3711 desired_min_uV = tmp_min;
3712 desired_max_uV = tmp_max;
3716 max_spread = constraints->max_spread[0];
3719 * Let target_uV be equal to the desired one if possible.
3720 * If not, set it to minimum voltage, allowed by other coupled
3723 target_uV = max(desired_min_uV, highest_min_uV - max_spread);
3726 * Find min and max voltages, which currently aren't violating
3729 for (i = 1; i < n_coupled; i++) {
3732 if (!_regulator_is_enabled(c_rdevs[i]))
3735 tmp_act = regulator_get_voltage_rdev(c_rdevs[i]);
3739 min_current_uV = min(tmp_act, min_current_uV);
3740 max_current_uV = max(tmp_act, max_current_uV);
3743 /* There aren't any other regulators enabled */
3744 if (max_current_uV == 0) {
3745 possible_uV = target_uV;
3748 * Correct target voltage, so as it currently isn't
3749 * violating max_spread
3751 possible_uV = max(target_uV, max_current_uV - max_spread);
3752 possible_uV = min(possible_uV, min_current_uV + max_spread);
3755 if (possible_uV > desired_max_uV)
3758 done = (possible_uV == target_uV);
3759 desired_min_uV = possible_uV;
3762 /* Apply max_uV_step constraint if necessary */
3763 if (state == PM_SUSPEND_ON) {
3764 ret = regulator_limit_voltage_step(rdev, current_uV,
3773 /* Set current_uV if wasn't done earlier in the code and if necessary */
3774 if (n_coupled > 1 && *current_uV == -1) {
3776 if (_regulator_is_enabled(rdev)) {
3777 ret = regulator_get_voltage_rdev(rdev);
3783 *current_uV = desired_min_uV;
3787 *min_uV = desired_min_uV;
3788 *max_uV = desired_max_uV;
3793 int regulator_do_balance_voltage(struct regulator_dev *rdev,
3794 suspend_state_t state, bool skip_coupled)
3796 struct regulator_dev **c_rdevs;
3797 struct regulator_dev *best_rdev;
3798 struct coupling_desc *c_desc = &rdev->coupling_desc;
3799 int i, ret, n_coupled, best_min_uV, best_max_uV, best_c_rdev;
3800 unsigned int delta, best_delta;
3801 unsigned long c_rdev_done = 0;
3802 bool best_c_rdev_done;
3804 c_rdevs = c_desc->coupled_rdevs;
3805 n_coupled = skip_coupled ? 1 : c_desc->n_coupled;
3808 * Find the best possible voltage change on each loop. Leave the loop
3809 * if there isn't any possible change.
3812 best_c_rdev_done = false;
3820 * Find highest difference between optimal voltage
3821 * and current voltage.
3823 for (i = 0; i < n_coupled; i++) {
3825 * optimal_uV is the best voltage that can be set for
3826 * i-th regulator at the moment without violating
3827 * max_spread constraint in order to balance
3828 * the coupled voltages.
3830 int optimal_uV = 0, optimal_max_uV = 0, current_uV = 0;
3832 if (test_bit(i, &c_rdev_done))
3835 ret = regulator_get_optimal_voltage(c_rdevs[i],
3843 delta = abs(optimal_uV - current_uV);
3845 if (delta && best_delta <= delta) {
3846 best_c_rdev_done = ret;
3848 best_rdev = c_rdevs[i];
3849 best_min_uV = optimal_uV;
3850 best_max_uV = optimal_max_uV;
3855 /* Nothing to change, return successfully */
3861 ret = regulator_set_voltage_rdev(best_rdev, best_min_uV,
3862 best_max_uV, state);
3867 if (best_c_rdev_done)
3868 set_bit(best_c_rdev, &c_rdev_done);
3870 } while (n_coupled > 1);
3876 static int regulator_balance_voltage(struct regulator_dev *rdev,
3877 suspend_state_t state)
3879 struct coupling_desc *c_desc = &rdev->coupling_desc;
3880 struct regulator_coupler *coupler = c_desc->coupler;
3881 bool skip_coupled = false;
3884 * If system is in a state other than PM_SUSPEND_ON, don't check
3885 * other coupled regulators.
3887 if (state != PM_SUSPEND_ON)
3888 skip_coupled = true;
3890 if (c_desc->n_resolved < c_desc->n_coupled) {
3891 rdev_err(rdev, "Not all coupled regulators registered\n");
3895 /* Invoke custom balancer for customized couplers */
3896 if (coupler && coupler->balance_voltage)
3897 return coupler->balance_voltage(coupler, rdev, state);
3899 return regulator_do_balance_voltage(rdev, state, skip_coupled);
3903 * regulator_set_voltage - set regulator output voltage
3904 * @regulator: regulator source
3905 * @min_uV: Minimum required voltage in uV
3906 * @max_uV: Maximum acceptable voltage in uV
3908 * Sets a voltage regulator to the desired output voltage. This can be set
3909 * during any regulator state. IOW, regulator can be disabled or enabled.
3911 * If the regulator is enabled then the voltage will change to the new value
3912 * immediately otherwise if the regulator is disabled the regulator will
3913 * output at the new voltage when enabled.
3915 * NOTE: If the regulator is shared between several devices then the lowest
3916 * request voltage that meets the system constraints will be used.
3917 * Regulator system constraints must be set for this regulator before
3918 * calling this function otherwise this call will fail.
3920 int regulator_set_voltage(struct regulator *regulator, int min_uV, int max_uV)
3922 struct ww_acquire_ctx ww_ctx;
3925 regulator_lock_dependent(regulator->rdev, &ww_ctx);
3927 ret = regulator_set_voltage_unlocked(regulator, min_uV, max_uV,
3930 regulator_unlock_dependent(regulator->rdev, &ww_ctx);
3934 EXPORT_SYMBOL_GPL(regulator_set_voltage);
3936 static inline int regulator_suspend_toggle(struct regulator_dev *rdev,
3937 suspend_state_t state, bool en)
3939 struct regulator_state *rstate;
3941 rstate = regulator_get_suspend_state(rdev, state);
3945 if (!rstate->changeable)
3948 rstate->enabled = (en) ? ENABLE_IN_SUSPEND : DISABLE_IN_SUSPEND;
3953 int regulator_suspend_enable(struct regulator_dev *rdev,
3954 suspend_state_t state)
3956 return regulator_suspend_toggle(rdev, state, true);
3958 EXPORT_SYMBOL_GPL(regulator_suspend_enable);
3960 int regulator_suspend_disable(struct regulator_dev *rdev,
3961 suspend_state_t state)
3963 struct regulator *regulator;
3964 struct regulator_voltage *voltage;
3967 * if any consumer wants this regulator device keeping on in
3968 * suspend states, don't set it as disabled.
3970 list_for_each_entry(regulator, &rdev->consumer_list, list) {
3971 voltage = ®ulator->voltage[state];
3972 if (voltage->min_uV || voltage->max_uV)
3976 return regulator_suspend_toggle(rdev, state, false);
3978 EXPORT_SYMBOL_GPL(regulator_suspend_disable);
3980 static int _regulator_set_suspend_voltage(struct regulator *regulator,
3981 int min_uV, int max_uV,
3982 suspend_state_t state)
3984 struct regulator_dev *rdev = regulator->rdev;
3985 struct regulator_state *rstate;
3987 rstate = regulator_get_suspend_state(rdev, state);
3991 if (rstate->min_uV == rstate->max_uV) {
3992 rdev_err(rdev, "The suspend voltage can't be changed!\n");
3996 return regulator_set_voltage_unlocked(regulator, min_uV, max_uV, state);
3999 int regulator_set_suspend_voltage(struct regulator *regulator, int min_uV,
4000 int max_uV, suspend_state_t state)
4002 struct ww_acquire_ctx ww_ctx;
4005 /* PM_SUSPEND_ON is handled by regulator_set_voltage() */
4006 if (regulator_check_states(state) || state == PM_SUSPEND_ON)
4009 regulator_lock_dependent(regulator->rdev, &ww_ctx);
4011 ret = _regulator_set_suspend_voltage(regulator, min_uV,
4014 regulator_unlock_dependent(regulator->rdev, &ww_ctx);
4018 EXPORT_SYMBOL_GPL(regulator_set_suspend_voltage);
4021 * regulator_set_voltage_time - get raise/fall time
4022 * @regulator: regulator source
4023 * @old_uV: starting voltage in microvolts
4024 * @new_uV: target voltage in microvolts
4026 * Provided with the starting and ending voltage, this function attempts to
4027 * calculate the time in microseconds required to rise or fall to this new
4030 int regulator_set_voltage_time(struct regulator *regulator,
4031 int old_uV, int new_uV)
4033 struct regulator_dev *rdev = regulator->rdev;
4034 const struct regulator_ops *ops = rdev->desc->ops;
4040 if (ops->set_voltage_time)
4041 return ops->set_voltage_time(rdev, old_uV, new_uV);
4042 else if (!ops->set_voltage_time_sel)
4043 return _regulator_set_voltage_time(rdev, old_uV, new_uV);
4045 /* Currently requires operations to do this */
4046 if (!ops->list_voltage || !rdev->desc->n_voltages)
4049 for (i = 0; i < rdev->desc->n_voltages; i++) {
4050 /* We only look for exact voltage matches here */
4051 if (i < rdev->desc->linear_min_sel)
4054 if (old_sel >= 0 && new_sel >= 0)
4057 voltage = regulator_list_voltage(regulator, i);
4062 if (voltage == old_uV)
4064 if (voltage == new_uV)
4068 if (old_sel < 0 || new_sel < 0)
4071 return ops->set_voltage_time_sel(rdev, old_sel, new_sel);
4073 EXPORT_SYMBOL_GPL(regulator_set_voltage_time);
4076 * regulator_set_voltage_time_sel - get raise/fall time
4077 * @rdev: regulator source device
4078 * @old_selector: selector for starting voltage
4079 * @new_selector: selector for target voltage
4081 * Provided with the starting and target voltage selectors, this function
4082 * returns time in microseconds required to rise or fall to this new voltage
4084 * Drivers providing ramp_delay in regulation_constraints can use this as their
4085 * set_voltage_time_sel() operation.
4087 int regulator_set_voltage_time_sel(struct regulator_dev *rdev,
4088 unsigned int old_selector,
4089 unsigned int new_selector)
4091 int old_volt, new_volt;
4094 if (!rdev->desc->ops->list_voltage)
4097 old_volt = rdev->desc->ops->list_voltage(rdev, old_selector);
4098 new_volt = rdev->desc->ops->list_voltage(rdev, new_selector);
4100 if (rdev->desc->ops->set_voltage_time)
4101 return rdev->desc->ops->set_voltage_time(rdev, old_volt,
4104 return _regulator_set_voltage_time(rdev, old_volt, new_volt);
4106 EXPORT_SYMBOL_GPL(regulator_set_voltage_time_sel);
4109 * regulator_sync_voltage - re-apply last regulator output voltage
4110 * @regulator: regulator source
4112 * Re-apply the last configured voltage. This is intended to be used
4113 * where some external control source the consumer is cooperating with
4114 * has caused the configured voltage to change.
4116 int regulator_sync_voltage(struct regulator *regulator)
4118 struct regulator_dev *rdev = regulator->rdev;
4119 struct regulator_voltage *voltage = ®ulator->voltage[PM_SUSPEND_ON];
4120 int ret, min_uV, max_uV;
4122 regulator_lock(rdev);
4124 if (!rdev->desc->ops->set_voltage &&
4125 !rdev->desc->ops->set_voltage_sel) {
4130 /* This is only going to work if we've had a voltage configured. */
4131 if (!voltage->min_uV && !voltage->max_uV) {
4136 min_uV = voltage->min_uV;
4137 max_uV = voltage->max_uV;
4139 /* This should be a paranoia check... */
4140 ret = regulator_check_voltage(rdev, &min_uV, &max_uV);
4144 ret = regulator_check_consumers(rdev, &min_uV, &max_uV, 0);
4148 /* balance only, if regulator is coupled */
4149 if (rdev->coupling_desc.n_coupled > 1)
4150 ret = regulator_balance_voltage(rdev, PM_SUSPEND_ON);
4152 ret = _regulator_do_set_voltage(rdev, min_uV, max_uV);
4155 regulator_unlock(rdev);
4158 EXPORT_SYMBOL_GPL(regulator_sync_voltage);
4160 int regulator_get_voltage_rdev(struct regulator_dev *rdev)
4165 if (rdev->desc->ops->get_bypass) {
4166 ret = rdev->desc->ops->get_bypass(rdev, &bypassed);
4170 /* if bypassed the regulator must have a supply */
4171 if (!rdev->supply) {
4173 "bypassed regulator has no supply!\n");
4174 return -EPROBE_DEFER;
4177 return regulator_get_voltage_rdev(rdev->supply->rdev);
4181 if (rdev->desc->ops->get_voltage_sel) {
4182 sel = rdev->desc->ops->get_voltage_sel(rdev);
4185 ret = rdev->desc->ops->list_voltage(rdev, sel);
4186 } else if (rdev->desc->ops->get_voltage) {
4187 ret = rdev->desc->ops->get_voltage(rdev);
4188 } else if (rdev->desc->ops->list_voltage) {
4189 ret = rdev->desc->ops->list_voltage(rdev, 0);
4190 } else if (rdev->desc->fixed_uV && (rdev->desc->n_voltages == 1)) {
4191 ret = rdev->desc->fixed_uV;
4192 } else if (rdev->supply) {
4193 ret = regulator_get_voltage_rdev(rdev->supply->rdev);
4194 } else if (rdev->supply_name) {
4195 return -EPROBE_DEFER;
4202 return ret - rdev->constraints->uV_offset;
4204 EXPORT_SYMBOL_GPL(regulator_get_voltage_rdev);
4207 * regulator_get_voltage - get regulator output voltage
4208 * @regulator: regulator source
4210 * This returns the current regulator voltage in uV.
4212 * NOTE: If the regulator is disabled it will return the voltage value. This
4213 * function should not be used to determine regulator state.
4215 int regulator_get_voltage(struct regulator *regulator)
4217 struct ww_acquire_ctx ww_ctx;
4220 regulator_lock_dependent(regulator->rdev, &ww_ctx);
4221 ret = regulator_get_voltage_rdev(regulator->rdev);
4222 regulator_unlock_dependent(regulator->rdev, &ww_ctx);
4226 EXPORT_SYMBOL_GPL(regulator_get_voltage);
4229 * regulator_set_current_limit - set regulator output current limit
4230 * @regulator: regulator source
4231 * @min_uA: Minimum supported current in uA
4232 * @max_uA: Maximum supported current in uA
4234 * Sets current sink to the desired output current. This can be set during
4235 * any regulator state. IOW, regulator can be disabled or enabled.
4237 * If the regulator is enabled then the current will change to the new value
4238 * immediately otherwise if the regulator is disabled the regulator will
4239 * output at the new current when enabled.
4241 * NOTE: Regulator system constraints must be set for this regulator before
4242 * calling this function otherwise this call will fail.
4244 int regulator_set_current_limit(struct regulator *regulator,
4245 int min_uA, int max_uA)
4247 struct regulator_dev *rdev = regulator->rdev;
4250 regulator_lock(rdev);
4253 if (!rdev->desc->ops->set_current_limit) {
4258 /* constraints check */
4259 ret = regulator_check_current_limit(rdev, &min_uA, &max_uA);
4263 ret = rdev->desc->ops->set_current_limit(rdev, min_uA, max_uA);
4265 regulator_unlock(rdev);
4268 EXPORT_SYMBOL_GPL(regulator_set_current_limit);
4270 static int _regulator_get_current_limit_unlocked(struct regulator_dev *rdev)
4273 if (!rdev->desc->ops->get_current_limit)
4276 return rdev->desc->ops->get_current_limit(rdev);
4279 static int _regulator_get_current_limit(struct regulator_dev *rdev)
4283 regulator_lock(rdev);
4284 ret = _regulator_get_current_limit_unlocked(rdev);
4285 regulator_unlock(rdev);
4291 * regulator_get_current_limit - get regulator output current
4292 * @regulator: regulator source
4294 * This returns the current supplied by the specified current sink in uA.
4296 * NOTE: If the regulator is disabled it will return the current value. This
4297 * function should not be used to determine regulator state.
4299 int regulator_get_current_limit(struct regulator *regulator)
4301 return _regulator_get_current_limit(regulator->rdev);
4303 EXPORT_SYMBOL_GPL(regulator_get_current_limit);
4306 * regulator_set_mode - set regulator operating mode
4307 * @regulator: regulator source
4308 * @mode: operating mode - one of the REGULATOR_MODE constants
4310 * Set regulator operating mode to increase regulator efficiency or improve
4311 * regulation performance.
4313 * NOTE: Regulator system constraints must be set for this regulator before
4314 * calling this function otherwise this call will fail.
4316 int regulator_set_mode(struct regulator *regulator, unsigned int mode)
4318 struct regulator_dev *rdev = regulator->rdev;
4320 int regulator_curr_mode;
4322 regulator_lock(rdev);
4325 if (!rdev->desc->ops->set_mode) {
4330 /* return if the same mode is requested */
4331 if (rdev->desc->ops->get_mode) {
4332 regulator_curr_mode = rdev->desc->ops->get_mode(rdev);
4333 if (regulator_curr_mode == mode) {
4339 /* constraints check */
4340 ret = regulator_mode_constrain(rdev, &mode);
4344 ret = rdev->desc->ops->set_mode(rdev, mode);
4346 regulator_unlock(rdev);
4349 EXPORT_SYMBOL_GPL(regulator_set_mode);
4351 static unsigned int _regulator_get_mode_unlocked(struct regulator_dev *rdev)
4354 if (!rdev->desc->ops->get_mode)
4357 return rdev->desc->ops->get_mode(rdev);
4360 static unsigned int _regulator_get_mode(struct regulator_dev *rdev)
4364 regulator_lock(rdev);
4365 ret = _regulator_get_mode_unlocked(rdev);
4366 regulator_unlock(rdev);
4372 * regulator_get_mode - get regulator operating mode
4373 * @regulator: regulator source
4375 * Get the current regulator operating mode.
4377 unsigned int regulator_get_mode(struct regulator *regulator)
4379 return _regulator_get_mode(regulator->rdev);
4381 EXPORT_SYMBOL_GPL(regulator_get_mode);
4383 static int _regulator_get_error_flags(struct regulator_dev *rdev,
4384 unsigned int *flags)
4388 regulator_lock(rdev);
4391 if (!rdev->desc->ops->get_error_flags) {
4396 ret = rdev->desc->ops->get_error_flags(rdev, flags);
4398 regulator_unlock(rdev);
4403 * regulator_get_error_flags - get regulator error information
4404 * @regulator: regulator source
4405 * @flags: pointer to store error flags
4407 * Get the current regulator error information.
4409 int regulator_get_error_flags(struct regulator *regulator,
4410 unsigned int *flags)
4412 return _regulator_get_error_flags(regulator->rdev, flags);
4414 EXPORT_SYMBOL_GPL(regulator_get_error_flags);
4417 * regulator_set_load - set regulator load
4418 * @regulator: regulator source
4419 * @uA_load: load current
4421 * Notifies the regulator core of a new device load. This is then used by
4422 * DRMS (if enabled by constraints) to set the most efficient regulator
4423 * operating mode for the new regulator loading.
4425 * Consumer devices notify their supply regulator of the maximum power
4426 * they will require (can be taken from device datasheet in the power
4427 * consumption tables) when they change operational status and hence power
4428 * state. Examples of operational state changes that can affect power
4429 * consumption are :-
4431 * o Device is opened / closed.
4432 * o Device I/O is about to begin or has just finished.
4433 * o Device is idling in between work.
4435 * This information is also exported via sysfs to userspace.
4437 * DRMS will sum the total requested load on the regulator and change
4438 * to the most efficient operating mode if platform constraints allow.
4440 * NOTE: when a regulator consumer requests to have a regulator
4441 * disabled then any load that consumer requested no longer counts
4442 * toward the total requested load. If the regulator is re-enabled
4443 * then the previously requested load will start counting again.
4445 * If a regulator is an always-on regulator then an individual consumer's
4446 * load will still be removed if that consumer is fully disabled.
4448 * On error a negative errno is returned.
4450 int regulator_set_load(struct regulator *regulator, int uA_load)
4452 struct regulator_dev *rdev = regulator->rdev;
4456 regulator_lock(rdev);
4457 old_uA_load = regulator->uA_load;
4458 regulator->uA_load = uA_load;
4459 if (regulator->enable_count && old_uA_load != uA_load) {
4460 ret = drms_uA_update(rdev);
4462 regulator->uA_load = old_uA_load;
4464 regulator_unlock(rdev);
4468 EXPORT_SYMBOL_GPL(regulator_set_load);
4471 * regulator_allow_bypass - allow the regulator to go into bypass mode
4473 * @regulator: Regulator to configure
4474 * @enable: enable or disable bypass mode
4476 * Allow the regulator to go into bypass mode if all other consumers
4477 * for the regulator also enable bypass mode and the machine
4478 * constraints allow this. Bypass mode means that the regulator is
4479 * simply passing the input directly to the output with no regulation.
4481 int regulator_allow_bypass(struct regulator *regulator, bool enable)
4483 struct regulator_dev *rdev = regulator->rdev;
4484 const char *name = rdev_get_name(rdev);
4487 if (!rdev->desc->ops->set_bypass)
4490 if (!regulator_ops_is_valid(rdev, REGULATOR_CHANGE_BYPASS))
4493 regulator_lock(rdev);
4495 if (enable && !regulator->bypass) {
4496 rdev->bypass_count++;
4498 if (rdev->bypass_count == rdev->open_count) {
4499 trace_regulator_bypass_enable(name);
4501 ret = rdev->desc->ops->set_bypass(rdev, enable);
4503 rdev->bypass_count--;
4505 trace_regulator_bypass_enable_complete(name);
4508 } else if (!enable && regulator->bypass) {
4509 rdev->bypass_count--;
4511 if (rdev->bypass_count != rdev->open_count) {
4512 trace_regulator_bypass_disable(name);
4514 ret = rdev->desc->ops->set_bypass(rdev, enable);
4516 rdev->bypass_count++;
4518 trace_regulator_bypass_disable_complete(name);
4523 regulator->bypass = enable;
4525 regulator_unlock(rdev);
4529 EXPORT_SYMBOL_GPL(regulator_allow_bypass);
4532 * regulator_register_notifier - register regulator event notifier
4533 * @regulator: regulator source
4534 * @nb: notifier block
4536 * Register notifier block to receive regulator events.
4538 int regulator_register_notifier(struct regulator *regulator,
4539 struct notifier_block *nb)
4541 return blocking_notifier_chain_register(®ulator->rdev->notifier,
4544 EXPORT_SYMBOL_GPL(regulator_register_notifier);
4547 * regulator_unregister_notifier - unregister regulator event notifier
4548 * @regulator: regulator source
4549 * @nb: notifier block
4551 * Unregister regulator event notifier block.
4553 int regulator_unregister_notifier(struct regulator *regulator,
4554 struct notifier_block *nb)
4556 return blocking_notifier_chain_unregister(®ulator->rdev->notifier,
4559 EXPORT_SYMBOL_GPL(regulator_unregister_notifier);
4561 /* notify regulator consumers and downstream regulator consumers.
4562 * Note mutex must be held by caller.
4564 static int _notifier_call_chain(struct regulator_dev *rdev,
4565 unsigned long event, void *data)
4567 /* call rdev chain first */
4568 return blocking_notifier_call_chain(&rdev->notifier, event, data);
4572 * regulator_bulk_get - get multiple regulator consumers
4574 * @dev: Device to supply
4575 * @num_consumers: Number of consumers to register
4576 * @consumers: Configuration of consumers; clients are stored here.
4578 * @return 0 on success, an errno on failure.
4580 * This helper function allows drivers to get several regulator
4581 * consumers in one operation. If any of the regulators cannot be
4582 * acquired then any regulators that were allocated will be freed
4583 * before returning to the caller.
4585 int regulator_bulk_get(struct device *dev, int num_consumers,
4586 struct regulator_bulk_data *consumers)
4591 for (i = 0; i < num_consumers; i++)
4592 consumers[i].consumer = NULL;
4594 for (i = 0; i < num_consumers; i++) {
4595 consumers[i].consumer = regulator_get(dev,
4596 consumers[i].supply);
4597 if (IS_ERR(consumers[i].consumer)) {
4598 ret = PTR_ERR(consumers[i].consumer);
4599 consumers[i].consumer = NULL;
4607 if (ret != -EPROBE_DEFER)
4608 dev_err(dev, "Failed to get supply '%s': %pe\n",
4609 consumers[i].supply, ERR_PTR(ret));
4611 dev_dbg(dev, "Failed to get supply '%s', deferring\n",
4612 consumers[i].supply);
4615 regulator_put(consumers[i].consumer);
4619 EXPORT_SYMBOL_GPL(regulator_bulk_get);
4621 static void regulator_bulk_enable_async(void *data, async_cookie_t cookie)
4623 struct regulator_bulk_data *bulk = data;
4625 bulk->ret = regulator_enable(bulk->consumer);
4629 * regulator_bulk_enable - enable multiple regulator consumers
4631 * @num_consumers: Number of consumers
4632 * @consumers: Consumer data; clients are stored here.
4633 * @return 0 on success, an errno on failure
4635 * This convenience API allows consumers to enable multiple regulator
4636 * clients in a single API call. If any consumers cannot be enabled
4637 * then any others that were enabled will be disabled again prior to
4640 int regulator_bulk_enable(int num_consumers,
4641 struct regulator_bulk_data *consumers)
4643 ASYNC_DOMAIN_EXCLUSIVE(async_domain);
4647 for (i = 0; i < num_consumers; i++) {
4648 async_schedule_domain(regulator_bulk_enable_async,
4649 &consumers[i], &async_domain);
4652 async_synchronize_full_domain(&async_domain);
4654 /* If any consumer failed we need to unwind any that succeeded */
4655 for (i = 0; i < num_consumers; i++) {
4656 if (consumers[i].ret != 0) {
4657 ret = consumers[i].ret;
4665 for (i = 0; i < num_consumers; i++) {
4666 if (consumers[i].ret < 0)
4667 pr_err("Failed to enable %s: %pe\n", consumers[i].supply,
4668 ERR_PTR(consumers[i].ret));
4670 regulator_disable(consumers[i].consumer);
4675 EXPORT_SYMBOL_GPL(regulator_bulk_enable);
4678 * regulator_bulk_disable - disable multiple regulator consumers
4680 * @num_consumers: Number of consumers
4681 * @consumers: Consumer data; clients are stored here.
4682 * @return 0 on success, an errno on failure
4684 * This convenience API allows consumers to disable multiple regulator
4685 * clients in a single API call. If any consumers cannot be disabled
4686 * then any others that were disabled will be enabled again prior to
4689 int regulator_bulk_disable(int num_consumers,
4690 struct regulator_bulk_data *consumers)
4695 for (i = num_consumers - 1; i >= 0; --i) {
4696 ret = regulator_disable(consumers[i].consumer);
4704 pr_err("Failed to disable %s: %pe\n", consumers[i].supply, ERR_PTR(ret));
4705 for (++i; i < num_consumers; ++i) {
4706 r = regulator_enable(consumers[i].consumer);
4708 pr_err("Failed to re-enable %s: %pe\n",
4709 consumers[i].supply, ERR_PTR(r));
4714 EXPORT_SYMBOL_GPL(regulator_bulk_disable);
4717 * regulator_bulk_force_disable - force disable multiple regulator consumers
4719 * @num_consumers: Number of consumers
4720 * @consumers: Consumer data; clients are stored here.
4721 * @return 0 on success, an errno on failure
4723 * This convenience API allows consumers to forcibly disable multiple regulator
4724 * clients in a single API call.
4725 * NOTE: This should be used for situations when device damage will
4726 * likely occur if the regulators are not disabled (e.g. over temp).
4727 * Although regulator_force_disable function call for some consumers can
4728 * return error numbers, the function is called for all consumers.
4730 int regulator_bulk_force_disable(int num_consumers,
4731 struct regulator_bulk_data *consumers)
4736 for (i = 0; i < num_consumers; i++) {
4738 regulator_force_disable(consumers[i].consumer);
4740 /* Store first error for reporting */
4741 if (consumers[i].ret && !ret)
4742 ret = consumers[i].ret;
4747 EXPORT_SYMBOL_GPL(regulator_bulk_force_disable);
4750 * regulator_bulk_free - free multiple regulator consumers
4752 * @num_consumers: Number of consumers
4753 * @consumers: Consumer data; clients are stored here.
4755 * This convenience API allows consumers to free multiple regulator
4756 * clients in a single API call.
4758 void regulator_bulk_free(int num_consumers,
4759 struct regulator_bulk_data *consumers)
4763 for (i = 0; i < num_consumers; i++) {
4764 regulator_put(consumers[i].consumer);
4765 consumers[i].consumer = NULL;
4768 EXPORT_SYMBOL_GPL(regulator_bulk_free);
4771 * regulator_notifier_call_chain - call regulator event notifier
4772 * @rdev: regulator source
4773 * @event: notifier block
4774 * @data: callback-specific data.
4776 * Called by regulator drivers to notify clients a regulator event has
4779 int regulator_notifier_call_chain(struct regulator_dev *rdev,
4780 unsigned long event, void *data)
4782 _notifier_call_chain(rdev, event, data);
4786 EXPORT_SYMBOL_GPL(regulator_notifier_call_chain);
4789 * regulator_mode_to_status - convert a regulator mode into a status
4791 * @mode: Mode to convert
4793 * Convert a regulator mode into a status.
4795 int regulator_mode_to_status(unsigned int mode)
4798 case REGULATOR_MODE_FAST:
4799 return REGULATOR_STATUS_FAST;
4800 case REGULATOR_MODE_NORMAL:
4801 return REGULATOR_STATUS_NORMAL;
4802 case REGULATOR_MODE_IDLE:
4803 return REGULATOR_STATUS_IDLE;
4804 case REGULATOR_MODE_STANDBY:
4805 return REGULATOR_STATUS_STANDBY;
4807 return REGULATOR_STATUS_UNDEFINED;
4810 EXPORT_SYMBOL_GPL(regulator_mode_to_status);
4812 static struct attribute *regulator_dev_attrs[] = {
4813 &dev_attr_name.attr,
4814 &dev_attr_num_users.attr,
4815 &dev_attr_type.attr,
4816 &dev_attr_microvolts.attr,
4817 &dev_attr_microamps.attr,
4818 &dev_attr_opmode.attr,
4819 &dev_attr_state.attr,
4820 &dev_attr_status.attr,
4821 &dev_attr_bypass.attr,
4822 &dev_attr_requested_microamps.attr,
4823 &dev_attr_min_microvolts.attr,
4824 &dev_attr_max_microvolts.attr,
4825 &dev_attr_min_microamps.attr,
4826 &dev_attr_max_microamps.attr,
4827 &dev_attr_suspend_standby_state.attr,
4828 &dev_attr_suspend_mem_state.attr,
4829 &dev_attr_suspend_disk_state.attr,
4830 &dev_attr_suspend_standby_microvolts.attr,
4831 &dev_attr_suspend_mem_microvolts.attr,
4832 &dev_attr_suspend_disk_microvolts.attr,
4833 &dev_attr_suspend_standby_mode.attr,
4834 &dev_attr_suspend_mem_mode.attr,
4835 &dev_attr_suspend_disk_mode.attr,
4840 * To avoid cluttering sysfs (and memory) with useless state, only
4841 * create attributes that can be meaningfully displayed.
4843 static umode_t regulator_attr_is_visible(struct kobject *kobj,
4844 struct attribute *attr, int idx)
4846 struct device *dev = kobj_to_dev(kobj);
4847 struct regulator_dev *rdev = dev_to_rdev(dev);
4848 const struct regulator_ops *ops = rdev->desc->ops;
4849 umode_t mode = attr->mode;
4851 /* these three are always present */
4852 if (attr == &dev_attr_name.attr ||
4853 attr == &dev_attr_num_users.attr ||
4854 attr == &dev_attr_type.attr)
4857 /* some attributes need specific methods to be displayed */
4858 if (attr == &dev_attr_microvolts.attr) {
4859 if ((ops->get_voltage && ops->get_voltage(rdev) >= 0) ||
4860 (ops->get_voltage_sel && ops->get_voltage_sel(rdev) >= 0) ||
4861 (ops->list_voltage && ops->list_voltage(rdev, 0) >= 0) ||
4862 (rdev->desc->fixed_uV && rdev->desc->n_voltages == 1))
4867 if (attr == &dev_attr_microamps.attr)
4868 return ops->get_current_limit ? mode : 0;
4870 if (attr == &dev_attr_opmode.attr)
4871 return ops->get_mode ? mode : 0;
4873 if (attr == &dev_attr_state.attr)
4874 return (rdev->ena_pin || ops->is_enabled) ? mode : 0;
4876 if (attr == &dev_attr_status.attr)
4877 return ops->get_status ? mode : 0;
4879 if (attr == &dev_attr_bypass.attr)
4880 return ops->get_bypass ? mode : 0;
4882 /* constraints need specific supporting methods */
4883 if (attr == &dev_attr_min_microvolts.attr ||
4884 attr == &dev_attr_max_microvolts.attr)
4885 return (ops->set_voltage || ops->set_voltage_sel) ? mode : 0;
4887 if (attr == &dev_attr_min_microamps.attr ||
4888 attr == &dev_attr_max_microamps.attr)
4889 return ops->set_current_limit ? mode : 0;
4891 if (attr == &dev_attr_suspend_standby_state.attr ||
4892 attr == &dev_attr_suspend_mem_state.attr ||
4893 attr == &dev_attr_suspend_disk_state.attr)
4896 if (attr == &dev_attr_suspend_standby_microvolts.attr ||
4897 attr == &dev_attr_suspend_mem_microvolts.attr ||
4898 attr == &dev_attr_suspend_disk_microvolts.attr)
4899 return ops->set_suspend_voltage ? mode : 0;
4901 if (attr == &dev_attr_suspend_standby_mode.attr ||
4902 attr == &dev_attr_suspend_mem_mode.attr ||
4903 attr == &dev_attr_suspend_disk_mode.attr)
4904 return ops->set_suspend_mode ? mode : 0;
4909 static const struct attribute_group regulator_dev_group = {
4910 .attrs = regulator_dev_attrs,
4911 .is_visible = regulator_attr_is_visible,
4914 static const struct attribute_group *regulator_dev_groups[] = {
4915 ®ulator_dev_group,
4919 static void regulator_dev_release(struct device *dev)
4921 struct regulator_dev *rdev = dev_get_drvdata(dev);
4923 kfree(rdev->constraints);
4924 of_node_put(rdev->dev.of_node);
4928 static void rdev_init_debugfs(struct regulator_dev *rdev)
4930 struct device *parent = rdev->dev.parent;
4931 const char *rname = rdev_get_name(rdev);
4932 char name[NAME_MAX];
4934 /* Avoid duplicate debugfs directory names */
4935 if (parent && rname == rdev->desc->name) {
4936 snprintf(name, sizeof(name), "%s-%s", dev_name(parent),
4941 rdev->debugfs = debugfs_create_dir(rname, debugfs_root);
4942 if (!rdev->debugfs) {
4943 rdev_warn(rdev, "Failed to create debugfs directory\n");
4947 debugfs_create_u32("use_count", 0444, rdev->debugfs,
4949 debugfs_create_u32("open_count", 0444, rdev->debugfs,
4951 debugfs_create_u32("bypass_count", 0444, rdev->debugfs,
4952 &rdev->bypass_count);
4955 static int regulator_register_resolve_supply(struct device *dev, void *data)
4957 struct regulator_dev *rdev = dev_to_rdev(dev);
4959 if (regulator_resolve_supply(rdev))
4960 rdev_dbg(rdev, "unable to resolve supply\n");
4965 int regulator_coupler_register(struct regulator_coupler *coupler)
4967 mutex_lock(®ulator_list_mutex);
4968 list_add_tail(&coupler->list, ®ulator_coupler_list);
4969 mutex_unlock(®ulator_list_mutex);
4974 static struct regulator_coupler *
4975 regulator_find_coupler(struct regulator_dev *rdev)
4977 struct regulator_coupler *coupler;
4981 * Note that regulators are appended to the list and the generic
4982 * coupler is registered first, hence it will be attached at last
4985 list_for_each_entry_reverse(coupler, ®ulator_coupler_list, list) {
4986 err = coupler->attach_regulator(coupler, rdev);
4988 if (!coupler->balance_voltage &&
4989 rdev->coupling_desc.n_coupled > 2)
4990 goto err_unsupported;
4996 return ERR_PTR(err);
5004 return ERR_PTR(-EINVAL);
5007 if (coupler->detach_regulator)
5008 coupler->detach_regulator(coupler, rdev);
5011 "Voltage balancing for multiple regulator couples is unimplemented\n");
5013 return ERR_PTR(-EPERM);
5016 static void regulator_resolve_coupling(struct regulator_dev *rdev)
5018 struct regulator_coupler *coupler = rdev->coupling_desc.coupler;
5019 struct coupling_desc *c_desc = &rdev->coupling_desc;
5020 int n_coupled = c_desc->n_coupled;
5021 struct regulator_dev *c_rdev;
5024 for (i = 1; i < n_coupled; i++) {
5025 /* already resolved */
5026 if (c_desc->coupled_rdevs[i])
5029 c_rdev = of_parse_coupled_regulator(rdev, i - 1);
5034 if (c_rdev->coupling_desc.coupler != coupler) {
5035 rdev_err(rdev, "coupler mismatch with %s\n",
5036 rdev_get_name(c_rdev));
5040 c_desc->coupled_rdevs[i] = c_rdev;
5041 c_desc->n_resolved++;
5043 regulator_resolve_coupling(c_rdev);
5047 static void regulator_remove_coupling(struct regulator_dev *rdev)
5049 struct regulator_coupler *coupler = rdev->coupling_desc.coupler;
5050 struct coupling_desc *__c_desc, *c_desc = &rdev->coupling_desc;
5051 struct regulator_dev *__c_rdev, *c_rdev;
5052 unsigned int __n_coupled, n_coupled;
5056 n_coupled = c_desc->n_coupled;
5058 for (i = 1; i < n_coupled; i++) {
5059 c_rdev = c_desc->coupled_rdevs[i];
5064 regulator_lock(c_rdev);
5066 __c_desc = &c_rdev->coupling_desc;
5067 __n_coupled = __c_desc->n_coupled;
5069 for (k = 1; k < __n_coupled; k++) {
5070 __c_rdev = __c_desc->coupled_rdevs[k];
5072 if (__c_rdev == rdev) {
5073 __c_desc->coupled_rdevs[k] = NULL;
5074 __c_desc->n_resolved--;
5079 regulator_unlock(c_rdev);
5081 c_desc->coupled_rdevs[i] = NULL;
5082 c_desc->n_resolved--;
5085 if (coupler && coupler->detach_regulator) {
5086 err = coupler->detach_regulator(coupler, rdev);
5088 rdev_err(rdev, "failed to detach from coupler: %pe\n",
5092 kfree(rdev->coupling_desc.coupled_rdevs);
5093 rdev->coupling_desc.coupled_rdevs = NULL;
5096 static int regulator_init_coupling(struct regulator_dev *rdev)
5098 struct regulator_dev **coupled;
5099 int err, n_phandles;
5101 if (!IS_ENABLED(CONFIG_OF))
5104 n_phandles = of_get_n_coupled(rdev);
5106 coupled = kcalloc(n_phandles + 1, sizeof(*coupled), GFP_KERNEL);
5110 rdev->coupling_desc.coupled_rdevs = coupled;
5113 * Every regulator should always have coupling descriptor filled with
5114 * at least pointer to itself.
5116 rdev->coupling_desc.coupled_rdevs[0] = rdev;
5117 rdev->coupling_desc.n_coupled = n_phandles + 1;
5118 rdev->coupling_desc.n_resolved++;
5120 /* regulator isn't coupled */
5121 if (n_phandles == 0)
5124 if (!of_check_coupling_data(rdev))
5127 mutex_lock(®ulator_list_mutex);
5128 rdev->coupling_desc.coupler = regulator_find_coupler(rdev);
5129 mutex_unlock(®ulator_list_mutex);
5131 if (IS_ERR(rdev->coupling_desc.coupler)) {
5132 err = PTR_ERR(rdev->coupling_desc.coupler);
5133 rdev_err(rdev, "failed to get coupler: %pe\n", ERR_PTR(err));
5140 static int generic_coupler_attach(struct regulator_coupler *coupler,
5141 struct regulator_dev *rdev)
5143 if (rdev->coupling_desc.n_coupled > 2) {
5145 "Voltage balancing for multiple regulator couples is unimplemented\n");
5149 if (!rdev->constraints->always_on) {
5151 "Coupling of a non always-on regulator is unimplemented\n");
5158 static struct regulator_coupler generic_regulator_coupler = {
5159 .attach_regulator = generic_coupler_attach,
5163 * regulator_register - register regulator
5164 * @regulator_desc: regulator to register
5165 * @cfg: runtime configuration for regulator
5167 * Called by regulator drivers to register a regulator.
5168 * Returns a valid pointer to struct regulator_dev on success
5169 * or an ERR_PTR() on error.
5171 struct regulator_dev *
5172 regulator_register(const struct regulator_desc *regulator_desc,
5173 const struct regulator_config *cfg)
5175 const struct regulator_init_data *init_data;
5176 struct regulator_config *config = NULL;
5177 static atomic_t regulator_no = ATOMIC_INIT(-1);
5178 struct regulator_dev *rdev;
5179 bool dangling_cfg_gpiod = false;
5180 bool dangling_of_gpiod = false;
5185 return ERR_PTR(-EINVAL);
5187 dangling_cfg_gpiod = true;
5188 if (regulator_desc == NULL) {
5196 if (regulator_desc->name == NULL || regulator_desc->ops == NULL) {
5201 if (regulator_desc->type != REGULATOR_VOLTAGE &&
5202 regulator_desc->type != REGULATOR_CURRENT) {
5207 /* Only one of each should be implemented */
5208 WARN_ON(regulator_desc->ops->get_voltage &&
5209 regulator_desc->ops->get_voltage_sel);
5210 WARN_ON(regulator_desc->ops->set_voltage &&
5211 regulator_desc->ops->set_voltage_sel);
5213 /* If we're using selectors we must implement list_voltage. */
5214 if (regulator_desc->ops->get_voltage_sel &&
5215 !regulator_desc->ops->list_voltage) {
5219 if (regulator_desc->ops->set_voltage_sel &&
5220 !regulator_desc->ops->list_voltage) {
5225 rdev = kzalloc(sizeof(struct regulator_dev), GFP_KERNEL);
5230 device_initialize(&rdev->dev);
5233 * Duplicate the config so the driver could override it after
5234 * parsing init data.
5236 config = kmemdup(cfg, sizeof(*cfg), GFP_KERNEL);
5237 if (config == NULL) {
5242 init_data = regulator_of_get_init_data(dev, regulator_desc, config,
5243 &rdev->dev.of_node);
5246 * Sometimes not all resources are probed already so we need to take
5247 * that into account. This happens most the time if the ena_gpiod comes
5248 * from a gpio extender or something else.
5250 if (PTR_ERR(init_data) == -EPROBE_DEFER) {
5251 ret = -EPROBE_DEFER;
5256 * We need to keep track of any GPIO descriptor coming from the
5257 * device tree until we have handled it over to the core. If the
5258 * config that was passed in to this function DOES NOT contain
5259 * a descriptor, and the config after this call DOES contain
5260 * a descriptor, we definitely got one from parsing the device
5263 if (!cfg->ena_gpiod && config->ena_gpiod)
5264 dangling_of_gpiod = true;
5266 init_data = config->init_data;
5267 rdev->dev.of_node = of_node_get(config->of_node);
5270 ww_mutex_init(&rdev->mutex, ®ulator_ww_class);
5271 rdev->reg_data = config->driver_data;
5272 rdev->owner = regulator_desc->owner;
5273 rdev->desc = regulator_desc;
5275 rdev->regmap = config->regmap;
5276 else if (dev_get_regmap(dev, NULL))
5277 rdev->regmap = dev_get_regmap(dev, NULL);
5278 else if (dev->parent)
5279 rdev->regmap = dev_get_regmap(dev->parent, NULL);
5280 INIT_LIST_HEAD(&rdev->consumer_list);
5281 INIT_LIST_HEAD(&rdev->list);
5282 BLOCKING_INIT_NOTIFIER_HEAD(&rdev->notifier);
5283 INIT_DELAYED_WORK(&rdev->disable_work, regulator_disable_work);
5285 /* preform any regulator specific init */
5286 if (init_data && init_data->regulator_init) {
5287 ret = init_data->regulator_init(rdev->reg_data);
5292 if (config->ena_gpiod) {
5293 ret = regulator_ena_gpio_request(rdev, config);
5295 rdev_err(rdev, "Failed to request enable GPIO: %pe\n",
5299 /* The regulator core took over the GPIO descriptor */
5300 dangling_cfg_gpiod = false;
5301 dangling_of_gpiod = false;
5304 /* register with sysfs */
5305 rdev->dev.class = ®ulator_class;
5306 rdev->dev.parent = dev;
5307 dev_set_name(&rdev->dev, "regulator.%lu",
5308 (unsigned long) atomic_inc_return(®ulator_no));
5309 dev_set_drvdata(&rdev->dev, rdev);
5311 /* set regulator constraints */
5313 rdev->constraints = kmemdup(&init_data->constraints,
5314 sizeof(*rdev->constraints),
5317 rdev->constraints = kzalloc(sizeof(*rdev->constraints),
5319 if (!rdev->constraints) {
5324 if (init_data && init_data->supply_regulator)
5325 rdev->supply_name = init_data->supply_regulator;
5326 else if (regulator_desc->supply_name)
5327 rdev->supply_name = regulator_desc->supply_name;
5329 ret = set_machine_constraints(rdev);
5330 if (ret == -EPROBE_DEFER) {
5331 /* Regulator might be in bypass mode and so needs its supply
5332 * to set the constraints
5334 /* FIXME: this currently triggers a chicken-and-egg problem
5335 * when creating -SUPPLY symlink in sysfs to a regulator
5336 * that is just being created
5338 rdev_dbg(rdev, "will resolve supply early: %s\n",
5340 ret = regulator_resolve_supply(rdev);
5342 ret = set_machine_constraints(rdev);
5344 rdev_dbg(rdev, "unable to resolve supply early: %pe\n",
5350 ret = regulator_init_coupling(rdev);
5354 /* add consumers devices */
5356 for (i = 0; i < init_data->num_consumer_supplies; i++) {
5357 ret = set_consumer_device_supply(rdev,
5358 init_data->consumer_supplies[i].dev_name,
5359 init_data->consumer_supplies[i].supply);
5361 dev_err(dev, "Failed to set supply %s\n",
5362 init_data->consumer_supplies[i].supply);
5363 goto unset_supplies;
5368 if (!rdev->desc->ops->get_voltage &&
5369 !rdev->desc->ops->list_voltage &&
5370 !rdev->desc->fixed_uV)
5371 rdev->is_switch = true;
5373 ret = device_add(&rdev->dev);
5375 goto unset_supplies;
5377 rdev_init_debugfs(rdev);
5379 /* try to resolve regulators coupling since a new one was registered */
5380 mutex_lock(®ulator_list_mutex);
5381 regulator_resolve_coupling(rdev);
5382 mutex_unlock(®ulator_list_mutex);
5384 /* try to resolve regulators supply since a new one was registered */
5385 class_for_each_device(®ulator_class, NULL, NULL,
5386 regulator_register_resolve_supply);
5391 mutex_lock(®ulator_list_mutex);
5392 unset_regulator_supplies(rdev);
5393 regulator_remove_coupling(rdev);
5394 mutex_unlock(®ulator_list_mutex);
5396 kfree(rdev->coupling_desc.coupled_rdevs);
5397 mutex_lock(®ulator_list_mutex);
5398 regulator_ena_gpio_free(rdev);
5399 mutex_unlock(®ulator_list_mutex);
5401 if (dangling_of_gpiod)
5402 gpiod_put(config->ena_gpiod);
5404 put_device(&rdev->dev);
5406 if (dangling_cfg_gpiod)
5407 gpiod_put(cfg->ena_gpiod);
5408 return ERR_PTR(ret);
5410 EXPORT_SYMBOL_GPL(regulator_register);
5413 * regulator_unregister - unregister regulator
5414 * @rdev: regulator to unregister
5416 * Called by regulator drivers to unregister a regulator.
5418 void regulator_unregister(struct regulator_dev *rdev)
5424 while (rdev->use_count--)
5425 regulator_disable(rdev->supply);
5426 regulator_put(rdev->supply);
5429 flush_work(&rdev->disable_work.work);
5431 mutex_lock(®ulator_list_mutex);
5433 debugfs_remove_recursive(rdev->debugfs);
5434 WARN_ON(rdev->open_count);
5435 regulator_remove_coupling(rdev);
5436 unset_regulator_supplies(rdev);
5437 list_del(&rdev->list);
5438 regulator_ena_gpio_free(rdev);
5439 device_unregister(&rdev->dev);
5441 mutex_unlock(®ulator_list_mutex);
5443 EXPORT_SYMBOL_GPL(regulator_unregister);
5445 #ifdef CONFIG_SUSPEND
5447 * regulator_suspend - prepare regulators for system wide suspend
5448 * @dev: ``&struct device`` pointer that is passed to _regulator_suspend()
5450 * Configure each regulator with it's suspend operating parameters for state.
5452 static int regulator_suspend(struct device *dev)
5454 struct regulator_dev *rdev = dev_to_rdev(dev);
5455 suspend_state_t state = pm_suspend_target_state;
5457 const struct regulator_state *rstate;
5459 rstate = regulator_get_suspend_state_check(rdev, state);
5463 regulator_lock(rdev);
5464 ret = __suspend_set_state(rdev, rstate);
5465 regulator_unlock(rdev);
5470 static int regulator_resume(struct device *dev)
5472 suspend_state_t state = pm_suspend_target_state;
5473 struct regulator_dev *rdev = dev_to_rdev(dev);
5474 struct regulator_state *rstate;
5477 rstate = regulator_get_suspend_state(rdev, state);
5481 /* Avoid grabbing the lock if we don't need to */
5482 if (!rdev->desc->ops->resume)
5485 regulator_lock(rdev);
5487 if (rstate->enabled == ENABLE_IN_SUSPEND ||
5488 rstate->enabled == DISABLE_IN_SUSPEND)
5489 ret = rdev->desc->ops->resume(rdev);
5491 regulator_unlock(rdev);
5495 #else /* !CONFIG_SUSPEND */
5497 #define regulator_suspend NULL
5498 #define regulator_resume NULL
5500 #endif /* !CONFIG_SUSPEND */
5503 static const struct dev_pm_ops __maybe_unused regulator_pm_ops = {
5504 .suspend = regulator_suspend,
5505 .resume = regulator_resume,
5509 struct class regulator_class = {
5510 .name = "regulator",
5511 .dev_release = regulator_dev_release,
5512 .dev_groups = regulator_dev_groups,
5514 .pm = ®ulator_pm_ops,
5518 * regulator_has_full_constraints - the system has fully specified constraints
5520 * Calling this function will cause the regulator API to disable all
5521 * regulators which have a zero use count and don't have an always_on
5522 * constraint in a late_initcall.
5524 * The intention is that this will become the default behaviour in a
5525 * future kernel release so users are encouraged to use this facility
5528 void regulator_has_full_constraints(void)
5530 has_full_constraints = 1;
5532 EXPORT_SYMBOL_GPL(regulator_has_full_constraints);
5535 * rdev_get_drvdata - get rdev regulator driver data
5538 * Get rdev regulator driver private data. This call can be used in the
5539 * regulator driver context.
5541 void *rdev_get_drvdata(struct regulator_dev *rdev)
5543 return rdev->reg_data;
5545 EXPORT_SYMBOL_GPL(rdev_get_drvdata);
5548 * regulator_get_drvdata - get regulator driver data
5549 * @regulator: regulator
5551 * Get regulator driver private data. This call can be used in the consumer
5552 * driver context when non API regulator specific functions need to be called.
5554 void *regulator_get_drvdata(struct regulator *regulator)
5556 return regulator->rdev->reg_data;
5558 EXPORT_SYMBOL_GPL(regulator_get_drvdata);
5561 * regulator_set_drvdata - set regulator driver data
5562 * @regulator: regulator
5565 void regulator_set_drvdata(struct regulator *regulator, void *data)
5567 regulator->rdev->reg_data = data;
5569 EXPORT_SYMBOL_GPL(regulator_set_drvdata);
5572 * rdev_get_id - get regulator ID
5575 int rdev_get_id(struct regulator_dev *rdev)
5577 return rdev->desc->id;
5579 EXPORT_SYMBOL_GPL(rdev_get_id);
5581 struct device *rdev_get_dev(struct regulator_dev *rdev)
5585 EXPORT_SYMBOL_GPL(rdev_get_dev);
5587 struct regmap *rdev_get_regmap(struct regulator_dev *rdev)
5589 return rdev->regmap;
5591 EXPORT_SYMBOL_GPL(rdev_get_regmap);
5593 void *regulator_get_init_drvdata(struct regulator_init_data *reg_init_data)
5595 return reg_init_data->driver_data;
5597 EXPORT_SYMBOL_GPL(regulator_get_init_drvdata);
5599 #ifdef CONFIG_DEBUG_FS
5600 static int supply_map_show(struct seq_file *sf, void *data)
5602 struct regulator_map *map;
5604 list_for_each_entry(map, ®ulator_map_list, list) {
5605 seq_printf(sf, "%s -> %s.%s\n",
5606 rdev_get_name(map->regulator), map->dev_name,
5612 DEFINE_SHOW_ATTRIBUTE(supply_map);
5614 struct summary_data {
5616 struct regulator_dev *parent;
5620 static void regulator_summary_show_subtree(struct seq_file *s,
5621 struct regulator_dev *rdev,
5624 static int regulator_summary_show_children(struct device *dev, void *data)
5626 struct regulator_dev *rdev = dev_to_rdev(dev);
5627 struct summary_data *summary_data = data;
5629 if (rdev->supply && rdev->supply->rdev == summary_data->parent)
5630 regulator_summary_show_subtree(summary_data->s, rdev,
5631 summary_data->level + 1);
5636 static void regulator_summary_show_subtree(struct seq_file *s,
5637 struct regulator_dev *rdev,
5640 struct regulation_constraints *c;
5641 struct regulator *consumer;
5642 struct summary_data summary_data;
5643 unsigned int opmode;
5648 opmode = _regulator_get_mode_unlocked(rdev);
5649 seq_printf(s, "%*s%-*s %3d %4d %6d %7s ",
5651 30 - level * 3, rdev_get_name(rdev),
5652 rdev->use_count, rdev->open_count, rdev->bypass_count,
5653 regulator_opmode_to_str(opmode));
5655 seq_printf(s, "%5dmV ", regulator_get_voltage_rdev(rdev) / 1000);
5656 seq_printf(s, "%5dmA ",
5657 _regulator_get_current_limit_unlocked(rdev) / 1000);
5659 c = rdev->constraints;
5661 switch (rdev->desc->type) {
5662 case REGULATOR_VOLTAGE:
5663 seq_printf(s, "%5dmV %5dmV ",
5664 c->min_uV / 1000, c->max_uV / 1000);
5666 case REGULATOR_CURRENT:
5667 seq_printf(s, "%5dmA %5dmA ",
5668 c->min_uA / 1000, c->max_uA / 1000);
5675 list_for_each_entry(consumer, &rdev->consumer_list, list) {
5676 if (consumer->dev && consumer->dev->class == ®ulator_class)
5679 seq_printf(s, "%*s%-*s ",
5680 (level + 1) * 3 + 1, "",
5681 30 - (level + 1) * 3,
5682 consumer->supply_name ? consumer->supply_name :
5683 consumer->dev ? dev_name(consumer->dev) : "deviceless");
5685 switch (rdev->desc->type) {
5686 case REGULATOR_VOLTAGE:
5687 seq_printf(s, "%3d %33dmA%c%5dmV %5dmV",
5688 consumer->enable_count,
5689 consumer->uA_load / 1000,
5690 consumer->uA_load && !consumer->enable_count ?
5692 consumer->voltage[PM_SUSPEND_ON].min_uV / 1000,
5693 consumer->voltage[PM_SUSPEND_ON].max_uV / 1000);
5695 case REGULATOR_CURRENT:
5703 summary_data.level = level;
5704 summary_data.parent = rdev;
5706 class_for_each_device(®ulator_class, NULL, &summary_data,
5707 regulator_summary_show_children);
5710 struct summary_lock_data {
5711 struct ww_acquire_ctx *ww_ctx;
5712 struct regulator_dev **new_contended_rdev;
5713 struct regulator_dev **old_contended_rdev;
5716 static int regulator_summary_lock_one(struct device *dev, void *data)
5718 struct regulator_dev *rdev = dev_to_rdev(dev);
5719 struct summary_lock_data *lock_data = data;
5722 if (rdev != *lock_data->old_contended_rdev) {
5723 ret = regulator_lock_nested(rdev, lock_data->ww_ctx);
5725 if (ret == -EDEADLK)
5726 *lock_data->new_contended_rdev = rdev;
5730 *lock_data->old_contended_rdev = NULL;
5736 static int regulator_summary_unlock_one(struct device *dev, void *data)
5738 struct regulator_dev *rdev = dev_to_rdev(dev);
5739 struct summary_lock_data *lock_data = data;
5742 if (rdev == *lock_data->new_contended_rdev)
5746 regulator_unlock(rdev);
5751 static int regulator_summary_lock_all(struct ww_acquire_ctx *ww_ctx,
5752 struct regulator_dev **new_contended_rdev,
5753 struct regulator_dev **old_contended_rdev)
5755 struct summary_lock_data lock_data;
5758 lock_data.ww_ctx = ww_ctx;
5759 lock_data.new_contended_rdev = new_contended_rdev;
5760 lock_data.old_contended_rdev = old_contended_rdev;
5762 ret = class_for_each_device(®ulator_class, NULL, &lock_data,
5763 regulator_summary_lock_one);
5765 class_for_each_device(®ulator_class, NULL, &lock_data,
5766 regulator_summary_unlock_one);
5771 static void regulator_summary_lock(struct ww_acquire_ctx *ww_ctx)
5773 struct regulator_dev *new_contended_rdev = NULL;
5774 struct regulator_dev *old_contended_rdev = NULL;
5777 mutex_lock(®ulator_list_mutex);
5779 ww_acquire_init(ww_ctx, ®ulator_ww_class);
5782 if (new_contended_rdev) {
5783 ww_mutex_lock_slow(&new_contended_rdev->mutex, ww_ctx);
5784 old_contended_rdev = new_contended_rdev;
5785 old_contended_rdev->ref_cnt++;
5788 err = regulator_summary_lock_all(ww_ctx,
5789 &new_contended_rdev,
5790 &old_contended_rdev);
5792 if (old_contended_rdev)
5793 regulator_unlock(old_contended_rdev);
5795 } while (err == -EDEADLK);
5797 ww_acquire_done(ww_ctx);
5800 static void regulator_summary_unlock(struct ww_acquire_ctx *ww_ctx)
5802 class_for_each_device(®ulator_class, NULL, NULL,
5803 regulator_summary_unlock_one);
5804 ww_acquire_fini(ww_ctx);
5806 mutex_unlock(®ulator_list_mutex);
5809 static int regulator_summary_show_roots(struct device *dev, void *data)
5811 struct regulator_dev *rdev = dev_to_rdev(dev);
5812 struct seq_file *s = data;
5815 regulator_summary_show_subtree(s, rdev, 0);
5820 static int regulator_summary_show(struct seq_file *s, void *data)
5822 struct ww_acquire_ctx ww_ctx;
5824 seq_puts(s, " regulator use open bypass opmode voltage current min max\n");
5825 seq_puts(s, "---------------------------------------------------------------------------------------\n");
5827 regulator_summary_lock(&ww_ctx);
5829 class_for_each_device(®ulator_class, NULL, s,
5830 regulator_summary_show_roots);
5832 regulator_summary_unlock(&ww_ctx);
5836 DEFINE_SHOW_ATTRIBUTE(regulator_summary);
5837 #endif /* CONFIG_DEBUG_FS */
5839 static int __init regulator_init(void)
5843 ret = class_register(®ulator_class);
5845 debugfs_root = debugfs_create_dir("regulator", NULL);
5847 pr_warn("regulator: Failed to create debugfs directory\n");
5849 #ifdef CONFIG_DEBUG_FS
5850 debugfs_create_file("supply_map", 0444, debugfs_root, NULL,
5853 debugfs_create_file("regulator_summary", 0444, debugfs_root,
5854 NULL, ®ulator_summary_fops);
5856 regulator_dummy_init();
5858 regulator_coupler_register(&generic_regulator_coupler);
5863 /* init early to allow our consumers to complete system booting */
5864 core_initcall(regulator_init);
5866 static int regulator_late_cleanup(struct device *dev, void *data)
5868 struct regulator_dev *rdev = dev_to_rdev(dev);
5869 const struct regulator_ops *ops = rdev->desc->ops;
5870 struct regulation_constraints *c = rdev->constraints;
5873 if (c && c->always_on)
5876 if (!regulator_ops_is_valid(rdev, REGULATOR_CHANGE_STATUS))
5879 regulator_lock(rdev);
5881 if (rdev->use_count)
5884 /* If we can't read the status assume it's always on. */
5885 if (ops->is_enabled)
5886 enabled = ops->is_enabled(rdev);
5890 /* But if reading the status failed, assume that it's off. */
5894 if (have_full_constraints()) {
5895 /* We log since this may kill the system if it goes
5898 rdev_info(rdev, "disabling\n");
5899 ret = _regulator_do_disable(rdev);
5901 rdev_err(rdev, "couldn't disable: %pe\n", ERR_PTR(ret));
5903 /* The intention is that in future we will
5904 * assume that full constraints are provided
5905 * so warn even if we aren't going to do
5908 rdev_warn(rdev, "incomplete constraints, leaving on\n");
5912 regulator_unlock(rdev);
5917 static void regulator_init_complete_work_function(struct work_struct *work)
5920 * Regulators may had failed to resolve their input supplies
5921 * when were registered, either because the input supply was
5922 * not registered yet or because its parent device was not
5923 * bound yet. So attempt to resolve the input supplies for
5924 * pending regulators before trying to disable unused ones.
5926 class_for_each_device(®ulator_class, NULL, NULL,
5927 regulator_register_resolve_supply);
5929 /* If we have a full configuration then disable any regulators
5930 * we have permission to change the status for and which are
5931 * not in use or always_on. This is effectively the default
5932 * for DT and ACPI as they have full constraints.
5934 class_for_each_device(®ulator_class, NULL, NULL,
5935 regulator_late_cleanup);
5938 static DECLARE_DELAYED_WORK(regulator_init_complete_work,
5939 regulator_init_complete_work_function);
5941 static int __init regulator_init_complete(void)
5944 * Since DT doesn't provide an idiomatic mechanism for
5945 * enabling full constraints and since it's much more natural
5946 * with DT to provide them just assume that a DT enabled
5947 * system has full constraints.
5949 if (of_have_populated_dt())
5950 has_full_constraints = true;
5953 * We punt completion for an arbitrary amount of time since
5954 * systems like distros will load many drivers from userspace
5955 * so consumers might not always be ready yet, this is
5956 * particularly an issue with laptops where this might bounce
5957 * the display off then on. Ideally we'd get a notification
5958 * from userspace when this happens but we don't so just wait
5959 * a bit and hope we waited long enough. It'd be better if
5960 * we'd only do this on systems that need it, and a kernel
5961 * command line option might be useful.
5963 schedule_delayed_work(®ulator_init_complete_work,
5964 msecs_to_jiffies(30000));
5968 late_initcall_sync(regulator_init_complete);