1 // SPDX-License-Identifier: GPL-2.0-or-later
3 // core.c -- Voltage/Current Regulator framework.
5 // Copyright 2007, 2008 Wolfson Microelectronics PLC.
6 // Copyright 2008 SlimLogic Ltd.
8 // Author: Liam Girdwood <lrg@slimlogic.co.uk>
10 #include <linux/kernel.h>
11 #include <linux/init.h>
12 #include <linux/debugfs.h>
13 #include <linux/device.h>
14 #include <linux/slab.h>
15 #include <linux/async.h>
16 #include <linux/err.h>
17 #include <linux/mutex.h>
18 #include <linux/suspend.h>
19 #include <linux/delay.h>
20 #include <linux/gpio/consumer.h>
22 #include <linux/regmap.h>
23 #include <linux/regulator/of_regulator.h>
24 #include <linux/regulator/consumer.h>
25 #include <linux/regulator/coupler.h>
26 #include <linux/regulator/driver.h>
27 #include <linux/regulator/machine.h>
28 #include <linux/module.h>
30 #define CREATE_TRACE_POINTS
31 #include <trace/events/regulator.h>
36 static DEFINE_WW_CLASS(regulator_ww_class);
37 static DEFINE_MUTEX(regulator_nesting_mutex);
38 static DEFINE_MUTEX(regulator_list_mutex);
39 static LIST_HEAD(regulator_map_list);
40 static LIST_HEAD(regulator_ena_gpio_list);
41 static LIST_HEAD(regulator_supply_alias_list);
42 static LIST_HEAD(regulator_coupler_list);
43 static bool has_full_constraints;
45 static struct dentry *debugfs_root;
48 * struct regulator_map
50 * Used to provide symbolic supply names to devices.
52 struct regulator_map {
53 struct list_head list;
54 const char *dev_name; /* The dev_name() for the consumer */
56 struct regulator_dev *regulator;
60 * struct regulator_enable_gpio
62 * Management for shared enable GPIO pin
64 struct regulator_enable_gpio {
65 struct list_head list;
66 struct gpio_desc *gpiod;
67 u32 enable_count; /* a number of enabled shared GPIO */
68 u32 request_count; /* a number of requested shared GPIO */
72 * struct regulator_supply_alias
74 * Used to map lookups for a supply onto an alternative device.
76 struct regulator_supply_alias {
77 struct list_head list;
78 struct device *src_dev;
79 const char *src_supply;
80 struct device *alias_dev;
81 const char *alias_supply;
84 static int _regulator_is_enabled(struct regulator_dev *rdev);
85 static int _regulator_disable(struct regulator *regulator);
86 static int _regulator_get_error_flags(struct regulator_dev *rdev, unsigned int *flags);
87 static int _regulator_get_current_limit(struct regulator_dev *rdev);
88 static unsigned int _regulator_get_mode(struct regulator_dev *rdev);
89 static int _notifier_call_chain(struct regulator_dev *rdev,
90 unsigned long event, void *data);
91 static int _regulator_do_set_voltage(struct regulator_dev *rdev,
92 int min_uV, int max_uV);
93 static int regulator_balance_voltage(struct regulator_dev *rdev,
94 suspend_state_t state);
95 static struct regulator *create_regulator(struct regulator_dev *rdev,
97 const char *supply_name);
98 static void destroy_regulator(struct regulator *regulator);
99 static void _regulator_put(struct regulator *regulator);
101 const char *rdev_get_name(struct regulator_dev *rdev)
103 if (rdev->constraints && rdev->constraints->name)
104 return rdev->constraints->name;
105 else if (rdev->desc->name)
106 return rdev->desc->name;
110 EXPORT_SYMBOL_GPL(rdev_get_name);
112 static bool have_full_constraints(void)
114 return has_full_constraints || of_have_populated_dt();
117 static bool regulator_ops_is_valid(struct regulator_dev *rdev, int ops)
119 if (!rdev->constraints) {
120 rdev_err(rdev, "no constraints\n");
124 if (rdev->constraints->valid_ops_mask & ops)
131 * regulator_lock_nested - lock a single regulator
132 * @rdev: regulator source
133 * @ww_ctx: w/w mutex acquire context
135 * This function can be called many times by one task on
136 * a single regulator and its mutex will be locked only
137 * once. If a task, which is calling this function is other
138 * than the one, which initially locked the mutex, it will
141 static inline int regulator_lock_nested(struct regulator_dev *rdev,
142 struct ww_acquire_ctx *ww_ctx)
147 mutex_lock(®ulator_nesting_mutex);
149 if (!ww_mutex_trylock(&rdev->mutex, ww_ctx)) {
150 if (rdev->mutex_owner == current)
156 mutex_unlock(®ulator_nesting_mutex);
157 ret = ww_mutex_lock(&rdev->mutex, ww_ctx);
158 mutex_lock(®ulator_nesting_mutex);
164 if (lock && ret != -EDEADLK) {
166 rdev->mutex_owner = current;
169 mutex_unlock(®ulator_nesting_mutex);
175 * regulator_lock - lock a single regulator
176 * @rdev: regulator source
178 * This function can be called many times by one task on
179 * a single regulator and its mutex will be locked only
180 * once. If a task, which is calling this function is other
181 * than the one, which initially locked the mutex, it will
184 static void regulator_lock(struct regulator_dev *rdev)
186 regulator_lock_nested(rdev, NULL);
190 * regulator_unlock - unlock a single regulator
191 * @rdev: regulator_source
193 * This function unlocks the mutex when the
194 * reference counter reaches 0.
196 static void regulator_unlock(struct regulator_dev *rdev)
198 mutex_lock(®ulator_nesting_mutex);
200 if (--rdev->ref_cnt == 0) {
201 rdev->mutex_owner = NULL;
202 ww_mutex_unlock(&rdev->mutex);
205 WARN_ON_ONCE(rdev->ref_cnt < 0);
207 mutex_unlock(®ulator_nesting_mutex);
210 static bool regulator_supply_is_couple(struct regulator_dev *rdev)
212 struct regulator_dev *c_rdev;
215 for (i = 1; i < rdev->coupling_desc.n_coupled; i++) {
216 c_rdev = rdev->coupling_desc.coupled_rdevs[i];
218 if (rdev->supply->rdev == c_rdev)
225 static void regulator_unlock_recursive(struct regulator_dev *rdev,
226 unsigned int n_coupled)
228 struct regulator_dev *c_rdev, *supply_rdev;
229 int i, supply_n_coupled;
231 for (i = n_coupled; i > 0; i--) {
232 c_rdev = rdev->coupling_desc.coupled_rdevs[i - 1];
237 if (c_rdev->supply && !regulator_supply_is_couple(c_rdev)) {
238 supply_rdev = c_rdev->supply->rdev;
239 supply_n_coupled = supply_rdev->coupling_desc.n_coupled;
241 regulator_unlock_recursive(supply_rdev,
245 regulator_unlock(c_rdev);
249 static int regulator_lock_recursive(struct regulator_dev *rdev,
250 struct regulator_dev **new_contended_rdev,
251 struct regulator_dev **old_contended_rdev,
252 struct ww_acquire_ctx *ww_ctx)
254 struct regulator_dev *c_rdev;
257 for (i = 0; i < rdev->coupling_desc.n_coupled; i++) {
258 c_rdev = rdev->coupling_desc.coupled_rdevs[i];
263 if (c_rdev != *old_contended_rdev) {
264 err = regulator_lock_nested(c_rdev, ww_ctx);
266 if (err == -EDEADLK) {
267 *new_contended_rdev = c_rdev;
271 /* shouldn't happen */
272 WARN_ON_ONCE(err != -EALREADY);
275 *old_contended_rdev = NULL;
278 if (c_rdev->supply && !regulator_supply_is_couple(c_rdev)) {
279 err = regulator_lock_recursive(c_rdev->supply->rdev,
284 regulator_unlock(c_rdev);
293 regulator_unlock_recursive(rdev, i);
299 * regulator_unlock_dependent - unlock regulator's suppliers and coupled
301 * @rdev: regulator source
302 * @ww_ctx: w/w mutex acquire context
304 * Unlock all regulators related with rdev by coupling or supplying.
306 static void regulator_unlock_dependent(struct regulator_dev *rdev,
307 struct ww_acquire_ctx *ww_ctx)
309 regulator_unlock_recursive(rdev, rdev->coupling_desc.n_coupled);
310 ww_acquire_fini(ww_ctx);
314 * regulator_lock_dependent - lock regulator's suppliers and coupled regulators
315 * @rdev: regulator source
316 * @ww_ctx: w/w mutex acquire context
318 * This function as a wrapper on regulator_lock_recursive(), which locks
319 * all regulators related with rdev by coupling or supplying.
321 static void regulator_lock_dependent(struct regulator_dev *rdev,
322 struct ww_acquire_ctx *ww_ctx)
324 struct regulator_dev *new_contended_rdev = NULL;
325 struct regulator_dev *old_contended_rdev = NULL;
328 mutex_lock(®ulator_list_mutex);
330 ww_acquire_init(ww_ctx, ®ulator_ww_class);
333 if (new_contended_rdev) {
334 ww_mutex_lock_slow(&new_contended_rdev->mutex, ww_ctx);
335 old_contended_rdev = new_contended_rdev;
336 old_contended_rdev->ref_cnt++;
339 err = regulator_lock_recursive(rdev,
344 if (old_contended_rdev)
345 regulator_unlock(old_contended_rdev);
347 } while (err == -EDEADLK);
349 ww_acquire_done(ww_ctx);
351 mutex_unlock(®ulator_list_mutex);
355 * of_get_child_regulator - get a child regulator device node
356 * based on supply name
357 * @parent: Parent device node
358 * @prop_name: Combination regulator supply name and "-supply"
360 * Traverse all child nodes.
361 * Extract the child regulator device node corresponding to the supply name.
362 * returns the device node corresponding to the regulator if found, else
365 static struct device_node *of_get_child_regulator(struct device_node *parent,
366 const char *prop_name)
368 struct device_node *regnode = NULL;
369 struct device_node *child = NULL;
371 for_each_child_of_node(parent, child) {
372 regnode = of_parse_phandle(child, prop_name, 0);
375 regnode = of_get_child_regulator(child, prop_name);
390 * of_get_regulator - get a regulator device node based on supply name
391 * @dev: Device pointer for the consumer (of regulator) device
392 * @supply: regulator supply name
394 * Extract the regulator device node corresponding to the supply name.
395 * returns the device node corresponding to the regulator if found, else
398 static struct device_node *of_get_regulator(struct device *dev, const char *supply)
400 struct device_node *regnode = NULL;
401 char prop_name[64]; /* 64 is max size of property name */
403 dev_dbg(dev, "Looking up %s-supply from device tree\n", supply);
405 snprintf(prop_name, 64, "%s-supply", supply);
406 regnode = of_parse_phandle(dev->of_node, prop_name, 0);
409 regnode = of_get_child_regulator(dev->of_node, prop_name);
413 dev_dbg(dev, "Looking up %s property in node %pOF failed\n",
414 prop_name, dev->of_node);
420 /* Platform voltage constraint check */
421 int regulator_check_voltage(struct regulator_dev *rdev,
422 int *min_uV, int *max_uV)
424 BUG_ON(*min_uV > *max_uV);
426 if (!regulator_ops_is_valid(rdev, REGULATOR_CHANGE_VOLTAGE)) {
427 rdev_err(rdev, "voltage operation not allowed\n");
431 if (*max_uV > rdev->constraints->max_uV)
432 *max_uV = rdev->constraints->max_uV;
433 if (*min_uV < rdev->constraints->min_uV)
434 *min_uV = rdev->constraints->min_uV;
436 if (*min_uV > *max_uV) {
437 rdev_err(rdev, "unsupportable voltage range: %d-%duV\n",
445 /* return 0 if the state is valid */
446 static int regulator_check_states(suspend_state_t state)
448 return (state > PM_SUSPEND_MAX || state == PM_SUSPEND_TO_IDLE);
451 /* Make sure we select a voltage that suits the needs of all
452 * regulator consumers
454 int regulator_check_consumers(struct regulator_dev *rdev,
455 int *min_uV, int *max_uV,
456 suspend_state_t state)
458 struct regulator *regulator;
459 struct regulator_voltage *voltage;
461 list_for_each_entry(regulator, &rdev->consumer_list, list) {
462 voltage = ®ulator->voltage[state];
464 * Assume consumers that didn't say anything are OK
465 * with anything in the constraint range.
467 if (!voltage->min_uV && !voltage->max_uV)
470 if (*max_uV > voltage->max_uV)
471 *max_uV = voltage->max_uV;
472 if (*min_uV < voltage->min_uV)
473 *min_uV = voltage->min_uV;
476 if (*min_uV > *max_uV) {
477 rdev_err(rdev, "Restricting voltage, %u-%uuV\n",
485 /* current constraint check */
486 static int regulator_check_current_limit(struct regulator_dev *rdev,
487 int *min_uA, int *max_uA)
489 BUG_ON(*min_uA > *max_uA);
491 if (!regulator_ops_is_valid(rdev, REGULATOR_CHANGE_CURRENT)) {
492 rdev_err(rdev, "current operation not allowed\n");
496 if (*max_uA > rdev->constraints->max_uA)
497 *max_uA = rdev->constraints->max_uA;
498 if (*min_uA < rdev->constraints->min_uA)
499 *min_uA = rdev->constraints->min_uA;
501 if (*min_uA > *max_uA) {
502 rdev_err(rdev, "unsupportable current range: %d-%duA\n",
510 /* operating mode constraint check */
511 static int regulator_mode_constrain(struct regulator_dev *rdev,
515 case REGULATOR_MODE_FAST:
516 case REGULATOR_MODE_NORMAL:
517 case REGULATOR_MODE_IDLE:
518 case REGULATOR_MODE_STANDBY:
521 rdev_err(rdev, "invalid mode %x specified\n", *mode);
525 if (!regulator_ops_is_valid(rdev, REGULATOR_CHANGE_MODE)) {
526 rdev_err(rdev, "mode operation not allowed\n");
530 /* The modes are bitmasks, the most power hungry modes having
531 * the lowest values. If the requested mode isn't supported
535 if (rdev->constraints->valid_modes_mask & *mode)
543 static inline struct regulator_state *
544 regulator_get_suspend_state(struct regulator_dev *rdev, suspend_state_t state)
546 if (rdev->constraints == NULL)
550 case PM_SUSPEND_STANDBY:
551 return &rdev->constraints->state_standby;
553 return &rdev->constraints->state_mem;
555 return &rdev->constraints->state_disk;
561 static const struct regulator_state *
562 regulator_get_suspend_state_check(struct regulator_dev *rdev, suspend_state_t state)
564 const struct regulator_state *rstate;
566 rstate = regulator_get_suspend_state(rdev, state);
570 /* If we have no suspend mode configuration don't set anything;
571 * only warn if the driver implements set_suspend_voltage or
572 * set_suspend_mode callback.
574 if (rstate->enabled != ENABLE_IN_SUSPEND &&
575 rstate->enabled != DISABLE_IN_SUSPEND) {
576 if (rdev->desc->ops->set_suspend_voltage ||
577 rdev->desc->ops->set_suspend_mode)
578 rdev_warn(rdev, "No configuration\n");
585 static ssize_t microvolts_show(struct device *dev,
586 struct device_attribute *attr, char *buf)
588 struct regulator_dev *rdev = dev_get_drvdata(dev);
591 regulator_lock(rdev);
592 uV = regulator_get_voltage_rdev(rdev);
593 regulator_unlock(rdev);
597 return sprintf(buf, "%d\n", uV);
599 static DEVICE_ATTR_RO(microvolts);
601 static ssize_t microamps_show(struct device *dev,
602 struct device_attribute *attr, char *buf)
604 struct regulator_dev *rdev = dev_get_drvdata(dev);
606 return sprintf(buf, "%d\n", _regulator_get_current_limit(rdev));
608 static DEVICE_ATTR_RO(microamps);
610 static ssize_t name_show(struct device *dev, struct device_attribute *attr,
613 struct regulator_dev *rdev = dev_get_drvdata(dev);
615 return sprintf(buf, "%s\n", rdev_get_name(rdev));
617 static DEVICE_ATTR_RO(name);
619 static const char *regulator_opmode_to_str(int mode)
622 case REGULATOR_MODE_FAST:
624 case REGULATOR_MODE_NORMAL:
626 case REGULATOR_MODE_IDLE:
628 case REGULATOR_MODE_STANDBY:
634 static ssize_t regulator_print_opmode(char *buf, int mode)
636 return sprintf(buf, "%s\n", regulator_opmode_to_str(mode));
639 static ssize_t opmode_show(struct device *dev,
640 struct device_attribute *attr, char *buf)
642 struct regulator_dev *rdev = dev_get_drvdata(dev);
644 return regulator_print_opmode(buf, _regulator_get_mode(rdev));
646 static DEVICE_ATTR_RO(opmode);
648 static ssize_t regulator_print_state(char *buf, int state)
651 return sprintf(buf, "enabled\n");
653 return sprintf(buf, "disabled\n");
655 return sprintf(buf, "unknown\n");
658 static ssize_t state_show(struct device *dev,
659 struct device_attribute *attr, char *buf)
661 struct regulator_dev *rdev = dev_get_drvdata(dev);
664 regulator_lock(rdev);
665 ret = regulator_print_state(buf, _regulator_is_enabled(rdev));
666 regulator_unlock(rdev);
670 static DEVICE_ATTR_RO(state);
672 static ssize_t status_show(struct device *dev,
673 struct device_attribute *attr, char *buf)
675 struct regulator_dev *rdev = dev_get_drvdata(dev);
679 status = rdev->desc->ops->get_status(rdev);
684 case REGULATOR_STATUS_OFF:
687 case REGULATOR_STATUS_ON:
690 case REGULATOR_STATUS_ERROR:
693 case REGULATOR_STATUS_FAST:
696 case REGULATOR_STATUS_NORMAL:
699 case REGULATOR_STATUS_IDLE:
702 case REGULATOR_STATUS_STANDBY:
705 case REGULATOR_STATUS_BYPASS:
708 case REGULATOR_STATUS_UNDEFINED:
715 return sprintf(buf, "%s\n", label);
717 static DEVICE_ATTR_RO(status);
719 static ssize_t min_microamps_show(struct device *dev,
720 struct device_attribute *attr, char *buf)
722 struct regulator_dev *rdev = dev_get_drvdata(dev);
724 if (!rdev->constraints)
725 return sprintf(buf, "constraint not defined\n");
727 return sprintf(buf, "%d\n", rdev->constraints->min_uA);
729 static DEVICE_ATTR_RO(min_microamps);
731 static ssize_t max_microamps_show(struct device *dev,
732 struct device_attribute *attr, char *buf)
734 struct regulator_dev *rdev = dev_get_drvdata(dev);
736 if (!rdev->constraints)
737 return sprintf(buf, "constraint not defined\n");
739 return sprintf(buf, "%d\n", rdev->constraints->max_uA);
741 static DEVICE_ATTR_RO(max_microamps);
743 static ssize_t min_microvolts_show(struct device *dev,
744 struct device_attribute *attr, char *buf)
746 struct regulator_dev *rdev = dev_get_drvdata(dev);
748 if (!rdev->constraints)
749 return sprintf(buf, "constraint not defined\n");
751 return sprintf(buf, "%d\n", rdev->constraints->min_uV);
753 static DEVICE_ATTR_RO(min_microvolts);
755 static ssize_t max_microvolts_show(struct device *dev,
756 struct device_attribute *attr, char *buf)
758 struct regulator_dev *rdev = dev_get_drvdata(dev);
760 if (!rdev->constraints)
761 return sprintf(buf, "constraint not defined\n");
763 return sprintf(buf, "%d\n", rdev->constraints->max_uV);
765 static DEVICE_ATTR_RO(max_microvolts);
767 static ssize_t requested_microamps_show(struct device *dev,
768 struct device_attribute *attr, char *buf)
770 struct regulator_dev *rdev = dev_get_drvdata(dev);
771 struct regulator *regulator;
774 regulator_lock(rdev);
775 list_for_each_entry(regulator, &rdev->consumer_list, list) {
776 if (regulator->enable_count)
777 uA += regulator->uA_load;
779 regulator_unlock(rdev);
780 return sprintf(buf, "%d\n", uA);
782 static DEVICE_ATTR_RO(requested_microamps);
784 static ssize_t num_users_show(struct device *dev, struct device_attribute *attr,
787 struct regulator_dev *rdev = dev_get_drvdata(dev);
788 return sprintf(buf, "%d\n", rdev->use_count);
790 static DEVICE_ATTR_RO(num_users);
792 static ssize_t type_show(struct device *dev, struct device_attribute *attr,
795 struct regulator_dev *rdev = dev_get_drvdata(dev);
797 switch (rdev->desc->type) {
798 case REGULATOR_VOLTAGE:
799 return sprintf(buf, "voltage\n");
800 case REGULATOR_CURRENT:
801 return sprintf(buf, "current\n");
803 return sprintf(buf, "unknown\n");
805 static DEVICE_ATTR_RO(type);
807 static ssize_t suspend_mem_microvolts_show(struct device *dev,
808 struct device_attribute *attr, char *buf)
810 struct regulator_dev *rdev = dev_get_drvdata(dev);
812 return sprintf(buf, "%d\n", rdev->constraints->state_mem.uV);
814 static DEVICE_ATTR_RO(suspend_mem_microvolts);
816 static ssize_t suspend_disk_microvolts_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_disk.uV);
823 static DEVICE_ATTR_RO(suspend_disk_microvolts);
825 static ssize_t suspend_standby_microvolts_show(struct device *dev,
826 struct device_attribute *attr, char *buf)
828 struct regulator_dev *rdev = dev_get_drvdata(dev);
830 return sprintf(buf, "%d\n", rdev->constraints->state_standby.uV);
832 static DEVICE_ATTR_RO(suspend_standby_microvolts);
834 static ssize_t suspend_mem_mode_show(struct device *dev,
835 struct device_attribute *attr, char *buf)
837 struct regulator_dev *rdev = dev_get_drvdata(dev);
839 return regulator_print_opmode(buf,
840 rdev->constraints->state_mem.mode);
842 static DEVICE_ATTR_RO(suspend_mem_mode);
844 static ssize_t suspend_disk_mode_show(struct device *dev,
845 struct device_attribute *attr, char *buf)
847 struct regulator_dev *rdev = dev_get_drvdata(dev);
849 return regulator_print_opmode(buf,
850 rdev->constraints->state_disk.mode);
852 static DEVICE_ATTR_RO(suspend_disk_mode);
854 static ssize_t suspend_standby_mode_show(struct device *dev,
855 struct device_attribute *attr, char *buf)
857 struct regulator_dev *rdev = dev_get_drvdata(dev);
859 return regulator_print_opmode(buf,
860 rdev->constraints->state_standby.mode);
862 static DEVICE_ATTR_RO(suspend_standby_mode);
864 static ssize_t suspend_mem_state_show(struct device *dev,
865 struct device_attribute *attr, char *buf)
867 struct regulator_dev *rdev = dev_get_drvdata(dev);
869 return regulator_print_state(buf,
870 rdev->constraints->state_mem.enabled);
872 static DEVICE_ATTR_RO(suspend_mem_state);
874 static ssize_t suspend_disk_state_show(struct device *dev,
875 struct device_attribute *attr, char *buf)
877 struct regulator_dev *rdev = dev_get_drvdata(dev);
879 return regulator_print_state(buf,
880 rdev->constraints->state_disk.enabled);
882 static DEVICE_ATTR_RO(suspend_disk_state);
884 static ssize_t suspend_standby_state_show(struct device *dev,
885 struct device_attribute *attr, char *buf)
887 struct regulator_dev *rdev = dev_get_drvdata(dev);
889 return regulator_print_state(buf,
890 rdev->constraints->state_standby.enabled);
892 static DEVICE_ATTR_RO(suspend_standby_state);
894 static ssize_t bypass_show(struct device *dev,
895 struct device_attribute *attr, char *buf)
897 struct regulator_dev *rdev = dev_get_drvdata(dev);
902 ret = rdev->desc->ops->get_bypass(rdev, &bypass);
911 return sprintf(buf, "%s\n", report);
913 static DEVICE_ATTR_RO(bypass);
915 #define REGULATOR_ERROR_ATTR(name, bit) \
916 static ssize_t name##_show(struct device *dev, struct device_attribute *attr, \
920 unsigned int flags; \
921 struct regulator_dev *rdev = dev_get_drvdata(dev); \
922 ret = _regulator_get_error_flags(rdev, &flags); \
925 return sysfs_emit(buf, "%d\n", !!(flags & (bit))); \
927 static DEVICE_ATTR_RO(name)
929 REGULATOR_ERROR_ATTR(under_voltage, REGULATOR_ERROR_UNDER_VOLTAGE);
930 REGULATOR_ERROR_ATTR(over_current, REGULATOR_ERROR_OVER_CURRENT);
931 REGULATOR_ERROR_ATTR(regulation_out, REGULATOR_ERROR_REGULATION_OUT);
932 REGULATOR_ERROR_ATTR(fail, REGULATOR_ERROR_FAIL);
933 REGULATOR_ERROR_ATTR(over_temp, REGULATOR_ERROR_OVER_TEMP);
934 REGULATOR_ERROR_ATTR(under_voltage_warn, REGULATOR_ERROR_UNDER_VOLTAGE_WARN);
935 REGULATOR_ERROR_ATTR(over_current_warn, REGULATOR_ERROR_OVER_CURRENT_WARN);
936 REGULATOR_ERROR_ATTR(over_voltage_warn, REGULATOR_ERROR_OVER_VOLTAGE_WARN);
937 REGULATOR_ERROR_ATTR(over_temp_warn, REGULATOR_ERROR_OVER_TEMP_WARN);
939 /* Calculate the new optimum regulator operating mode based on the new total
940 * consumer load. All locks held by caller
942 static int drms_uA_update(struct regulator_dev *rdev)
944 struct regulator *sibling;
945 int current_uA = 0, output_uV, input_uV, err;
949 * first check to see if we can set modes at all, otherwise just
950 * tell the consumer everything is OK.
952 if (!regulator_ops_is_valid(rdev, REGULATOR_CHANGE_DRMS)) {
953 rdev_dbg(rdev, "DRMS operation not allowed\n");
957 if (!rdev->desc->ops->get_optimum_mode &&
958 !rdev->desc->ops->set_load)
961 if (!rdev->desc->ops->set_mode &&
962 !rdev->desc->ops->set_load)
965 /* calc total requested load */
966 list_for_each_entry(sibling, &rdev->consumer_list, list) {
967 if (sibling->enable_count)
968 current_uA += sibling->uA_load;
971 current_uA += rdev->constraints->system_load;
973 if (rdev->desc->ops->set_load) {
974 /* set the optimum mode for our new total regulator load */
975 err = rdev->desc->ops->set_load(rdev, current_uA);
977 rdev_err(rdev, "failed to set load %d: %pe\n",
978 current_uA, ERR_PTR(err));
980 /* get output voltage */
981 output_uV = regulator_get_voltage_rdev(rdev);
982 if (output_uV <= 0) {
983 rdev_err(rdev, "invalid output voltage found\n");
987 /* get input voltage */
990 input_uV = regulator_get_voltage(rdev->supply);
992 input_uV = rdev->constraints->input_uV;
994 rdev_err(rdev, "invalid input voltage found\n");
998 /* now get the optimum mode for our new total regulator load */
999 mode = rdev->desc->ops->get_optimum_mode(rdev, input_uV,
1000 output_uV, current_uA);
1002 /* check the new mode is allowed */
1003 err = regulator_mode_constrain(rdev, &mode);
1005 rdev_err(rdev, "failed to get optimum mode @ %d uA %d -> %d uV: %pe\n",
1006 current_uA, input_uV, output_uV, ERR_PTR(err));
1010 err = rdev->desc->ops->set_mode(rdev, mode);
1012 rdev_err(rdev, "failed to set optimum mode %x: %pe\n",
1013 mode, ERR_PTR(err));
1019 static int __suspend_set_state(struct regulator_dev *rdev,
1020 const struct regulator_state *rstate)
1024 if (rstate->enabled == ENABLE_IN_SUSPEND &&
1025 rdev->desc->ops->set_suspend_enable)
1026 ret = rdev->desc->ops->set_suspend_enable(rdev);
1027 else if (rstate->enabled == DISABLE_IN_SUSPEND &&
1028 rdev->desc->ops->set_suspend_disable)
1029 ret = rdev->desc->ops->set_suspend_disable(rdev);
1030 else /* OK if set_suspend_enable or set_suspend_disable is NULL */
1034 rdev_err(rdev, "failed to enabled/disable: %pe\n", ERR_PTR(ret));
1038 if (rdev->desc->ops->set_suspend_voltage && rstate->uV > 0) {
1039 ret = rdev->desc->ops->set_suspend_voltage(rdev, rstate->uV);
1041 rdev_err(rdev, "failed to set voltage: %pe\n", ERR_PTR(ret));
1046 if (rdev->desc->ops->set_suspend_mode && rstate->mode > 0) {
1047 ret = rdev->desc->ops->set_suspend_mode(rdev, rstate->mode);
1049 rdev_err(rdev, "failed to set mode: %pe\n", ERR_PTR(ret));
1057 static int suspend_set_initial_state(struct regulator_dev *rdev)
1059 const struct regulator_state *rstate;
1061 rstate = regulator_get_suspend_state_check(rdev,
1062 rdev->constraints->initial_state);
1066 return __suspend_set_state(rdev, rstate);
1069 #if defined(DEBUG) || defined(CONFIG_DYNAMIC_DEBUG)
1070 static void print_constraints_debug(struct regulator_dev *rdev)
1072 struct regulation_constraints *constraints = rdev->constraints;
1074 size_t len = sizeof(buf) - 1;
1078 if (constraints->min_uV && constraints->max_uV) {
1079 if (constraints->min_uV == constraints->max_uV)
1080 count += scnprintf(buf + count, len - count, "%d mV ",
1081 constraints->min_uV / 1000);
1083 count += scnprintf(buf + count, len - count,
1085 constraints->min_uV / 1000,
1086 constraints->max_uV / 1000);
1089 if (!constraints->min_uV ||
1090 constraints->min_uV != constraints->max_uV) {
1091 ret = regulator_get_voltage_rdev(rdev);
1093 count += scnprintf(buf + count, len - count,
1094 "at %d mV ", ret / 1000);
1097 if (constraints->uV_offset)
1098 count += scnprintf(buf + count, len - count, "%dmV offset ",
1099 constraints->uV_offset / 1000);
1101 if (constraints->min_uA && constraints->max_uA) {
1102 if (constraints->min_uA == constraints->max_uA)
1103 count += scnprintf(buf + count, len - count, "%d mA ",
1104 constraints->min_uA / 1000);
1106 count += scnprintf(buf + count, len - count,
1108 constraints->min_uA / 1000,
1109 constraints->max_uA / 1000);
1112 if (!constraints->min_uA ||
1113 constraints->min_uA != constraints->max_uA) {
1114 ret = _regulator_get_current_limit(rdev);
1116 count += scnprintf(buf + count, len - count,
1117 "at %d mA ", ret / 1000);
1120 if (constraints->valid_modes_mask & REGULATOR_MODE_FAST)
1121 count += scnprintf(buf + count, len - count, "fast ");
1122 if (constraints->valid_modes_mask & REGULATOR_MODE_NORMAL)
1123 count += scnprintf(buf + count, len - count, "normal ");
1124 if (constraints->valid_modes_mask & REGULATOR_MODE_IDLE)
1125 count += scnprintf(buf + count, len - count, "idle ");
1126 if (constraints->valid_modes_mask & REGULATOR_MODE_STANDBY)
1127 count += scnprintf(buf + count, len - count, "standby ");
1130 count = scnprintf(buf, len, "no parameters");
1134 count += scnprintf(buf + count, len - count, ", %s",
1135 _regulator_is_enabled(rdev) ? "enabled" : "disabled");
1137 rdev_dbg(rdev, "%s\n", buf);
1139 #else /* !DEBUG && !CONFIG_DYNAMIC_DEBUG */
1140 static inline void print_constraints_debug(struct regulator_dev *rdev) {}
1141 #endif /* !DEBUG && !CONFIG_DYNAMIC_DEBUG */
1143 static void print_constraints(struct regulator_dev *rdev)
1145 struct regulation_constraints *constraints = rdev->constraints;
1147 print_constraints_debug(rdev);
1149 if ((constraints->min_uV != constraints->max_uV) &&
1150 !regulator_ops_is_valid(rdev, REGULATOR_CHANGE_VOLTAGE))
1152 "Voltage range but no REGULATOR_CHANGE_VOLTAGE\n");
1155 static int machine_constraints_voltage(struct regulator_dev *rdev,
1156 struct regulation_constraints *constraints)
1158 const struct regulator_ops *ops = rdev->desc->ops;
1161 /* do we need to apply the constraint voltage */
1162 if (rdev->constraints->apply_uV &&
1163 rdev->constraints->min_uV && rdev->constraints->max_uV) {
1164 int target_min, target_max;
1165 int current_uV = regulator_get_voltage_rdev(rdev);
1167 if (current_uV == -ENOTRECOVERABLE) {
1168 /* This regulator can't be read and must be initialized */
1169 rdev_info(rdev, "Setting %d-%duV\n",
1170 rdev->constraints->min_uV,
1171 rdev->constraints->max_uV);
1172 _regulator_do_set_voltage(rdev,
1173 rdev->constraints->min_uV,
1174 rdev->constraints->max_uV);
1175 current_uV = regulator_get_voltage_rdev(rdev);
1178 if (current_uV < 0) {
1179 if (current_uV != -EPROBE_DEFER)
1181 "failed to get the current voltage: %pe\n",
1182 ERR_PTR(current_uV));
1187 * If we're below the minimum voltage move up to the
1188 * minimum voltage, if we're above the maximum voltage
1189 * then move down to the maximum.
1191 target_min = current_uV;
1192 target_max = current_uV;
1194 if (current_uV < rdev->constraints->min_uV) {
1195 target_min = rdev->constraints->min_uV;
1196 target_max = rdev->constraints->min_uV;
1199 if (current_uV > rdev->constraints->max_uV) {
1200 target_min = rdev->constraints->max_uV;
1201 target_max = rdev->constraints->max_uV;
1204 if (target_min != current_uV || target_max != current_uV) {
1205 rdev_info(rdev, "Bringing %duV into %d-%duV\n",
1206 current_uV, target_min, target_max);
1207 ret = _regulator_do_set_voltage(
1208 rdev, target_min, target_max);
1211 "failed to apply %d-%duV constraint: %pe\n",
1212 target_min, target_max, ERR_PTR(ret));
1218 /* constrain machine-level voltage specs to fit
1219 * the actual range supported by this regulator.
1221 if (ops->list_voltage && rdev->desc->n_voltages) {
1222 int count = rdev->desc->n_voltages;
1224 int min_uV = INT_MAX;
1225 int max_uV = INT_MIN;
1226 int cmin = constraints->min_uV;
1227 int cmax = constraints->max_uV;
1229 /* it's safe to autoconfigure fixed-voltage supplies
1230 * and the constraints are used by list_voltage.
1232 if (count == 1 && !cmin) {
1235 constraints->min_uV = cmin;
1236 constraints->max_uV = cmax;
1239 /* voltage constraints are optional */
1240 if ((cmin == 0) && (cmax == 0))
1243 /* else require explicit machine-level constraints */
1244 if (cmin <= 0 || cmax <= 0 || cmax < cmin) {
1245 rdev_err(rdev, "invalid voltage constraints\n");
1249 /* no need to loop voltages if range is continuous */
1250 if (rdev->desc->continuous_voltage_range)
1253 /* initial: [cmin..cmax] valid, [min_uV..max_uV] not */
1254 for (i = 0; i < count; i++) {
1257 value = ops->list_voltage(rdev, i);
1261 /* maybe adjust [min_uV..max_uV] */
1262 if (value >= cmin && value < min_uV)
1264 if (value <= cmax && value > max_uV)
1268 /* final: [min_uV..max_uV] valid iff constraints valid */
1269 if (max_uV < min_uV) {
1271 "unsupportable voltage constraints %u-%uuV\n",
1276 /* use regulator's subset of machine constraints */
1277 if (constraints->min_uV < min_uV) {
1278 rdev_dbg(rdev, "override min_uV, %d -> %d\n",
1279 constraints->min_uV, min_uV);
1280 constraints->min_uV = min_uV;
1282 if (constraints->max_uV > max_uV) {
1283 rdev_dbg(rdev, "override max_uV, %d -> %d\n",
1284 constraints->max_uV, max_uV);
1285 constraints->max_uV = max_uV;
1292 static int machine_constraints_current(struct regulator_dev *rdev,
1293 struct regulation_constraints *constraints)
1295 const struct regulator_ops *ops = rdev->desc->ops;
1298 if (!constraints->min_uA && !constraints->max_uA)
1301 if (constraints->min_uA > constraints->max_uA) {
1302 rdev_err(rdev, "Invalid current constraints\n");
1306 if (!ops->set_current_limit || !ops->get_current_limit) {
1307 rdev_warn(rdev, "Operation of current configuration missing\n");
1311 /* Set regulator current in constraints range */
1312 ret = ops->set_current_limit(rdev, constraints->min_uA,
1313 constraints->max_uA);
1315 rdev_err(rdev, "Failed to set current constraint, %d\n", ret);
1322 static int _regulator_do_enable(struct regulator_dev *rdev);
1324 static int notif_set_limit(struct regulator_dev *rdev,
1325 int (*set)(struct regulator_dev *, int, int, bool),
1326 int limit, int severity)
1330 if (limit == REGULATOR_NOTIF_LIMIT_DISABLE) {
1337 if (limit == REGULATOR_NOTIF_LIMIT_ENABLE)
1340 return set(rdev, limit, severity, enable);
1343 static int handle_notify_limits(struct regulator_dev *rdev,
1344 int (*set)(struct regulator_dev *, int, int, bool),
1345 struct notification_limit *limits)
1353 ret = notif_set_limit(rdev, set, limits->prot,
1354 REGULATOR_SEVERITY_PROT);
1359 ret = notif_set_limit(rdev, set, limits->err,
1360 REGULATOR_SEVERITY_ERR);
1365 ret = notif_set_limit(rdev, set, limits->warn,
1366 REGULATOR_SEVERITY_WARN);
1371 * set_machine_constraints - sets regulator constraints
1372 * @rdev: regulator source
1374 * Allows platform initialisation code to define and constrain
1375 * regulator circuits e.g. valid voltage/current ranges, etc. NOTE:
1376 * Constraints *must* be set by platform code in order for some
1377 * regulator operations to proceed i.e. set_voltage, set_current_limit,
1380 static int set_machine_constraints(struct regulator_dev *rdev)
1383 const struct regulator_ops *ops = rdev->desc->ops;
1385 ret = machine_constraints_voltage(rdev, rdev->constraints);
1389 ret = machine_constraints_current(rdev, rdev->constraints);
1393 if (rdev->constraints->ilim_uA && ops->set_input_current_limit) {
1394 ret = ops->set_input_current_limit(rdev,
1395 rdev->constraints->ilim_uA);
1397 rdev_err(rdev, "failed to set input limit: %pe\n", ERR_PTR(ret));
1402 /* do we need to setup our suspend state */
1403 if (rdev->constraints->initial_state) {
1404 ret = suspend_set_initial_state(rdev);
1406 rdev_err(rdev, "failed to set suspend state: %pe\n", ERR_PTR(ret));
1411 if (rdev->constraints->initial_mode) {
1412 if (!ops->set_mode) {
1413 rdev_err(rdev, "no set_mode operation\n");
1417 ret = ops->set_mode(rdev, rdev->constraints->initial_mode);
1419 rdev_err(rdev, "failed to set initial mode: %pe\n", ERR_PTR(ret));
1422 } else if (rdev->constraints->system_load) {
1424 * We'll only apply the initial system load if an
1425 * initial mode wasn't specified.
1427 drms_uA_update(rdev);
1430 if ((rdev->constraints->ramp_delay || rdev->constraints->ramp_disable)
1431 && ops->set_ramp_delay) {
1432 ret = ops->set_ramp_delay(rdev, rdev->constraints->ramp_delay);
1434 rdev_err(rdev, "failed to set ramp_delay: %pe\n", ERR_PTR(ret));
1439 if (rdev->constraints->pull_down && ops->set_pull_down) {
1440 ret = ops->set_pull_down(rdev);
1442 rdev_err(rdev, "failed to set pull down: %pe\n", ERR_PTR(ret));
1447 if (rdev->constraints->soft_start && ops->set_soft_start) {
1448 ret = ops->set_soft_start(rdev);
1450 rdev_err(rdev, "failed to set soft start: %pe\n", ERR_PTR(ret));
1456 * Existing logic does not warn if over_current_protection is given as
1457 * a constraint but driver does not support that. I think we should
1458 * warn about this type of issues as it is possible someone changes
1459 * PMIC on board to another type - and the another PMIC's driver does
1460 * not support setting protection. Board composer may happily believe
1461 * the DT limits are respected - especially if the new PMIC HW also
1462 * supports protection but the driver does not. I won't change the logic
1463 * without hearing more experienced opinion on this though.
1465 * If warning is seen as a good idea then we can merge handling the
1466 * over-curret protection and detection and get rid of this special
1469 if (rdev->constraints->over_current_protection
1470 && ops->set_over_current_protection) {
1471 int lim = rdev->constraints->over_curr_limits.prot;
1473 ret = ops->set_over_current_protection(rdev, lim,
1474 REGULATOR_SEVERITY_PROT,
1477 rdev_err(rdev, "failed to set over current protection: %pe\n",
1483 if (rdev->constraints->over_current_detection)
1484 ret = handle_notify_limits(rdev,
1485 ops->set_over_current_protection,
1486 &rdev->constraints->over_curr_limits);
1488 if (ret != -EOPNOTSUPP) {
1489 rdev_err(rdev, "failed to set over current limits: %pe\n",
1494 "IC does not support requested over-current limits\n");
1497 if (rdev->constraints->over_voltage_detection)
1498 ret = handle_notify_limits(rdev,
1499 ops->set_over_voltage_protection,
1500 &rdev->constraints->over_voltage_limits);
1502 if (ret != -EOPNOTSUPP) {
1503 rdev_err(rdev, "failed to set over voltage limits %pe\n",
1508 "IC does not support requested over voltage limits\n");
1511 if (rdev->constraints->under_voltage_detection)
1512 ret = handle_notify_limits(rdev,
1513 ops->set_under_voltage_protection,
1514 &rdev->constraints->under_voltage_limits);
1516 if (ret != -EOPNOTSUPP) {
1517 rdev_err(rdev, "failed to set under voltage limits %pe\n",
1522 "IC does not support requested under voltage limits\n");
1525 if (rdev->constraints->over_temp_detection)
1526 ret = handle_notify_limits(rdev,
1527 ops->set_thermal_protection,
1528 &rdev->constraints->temp_limits);
1530 if (ret != -EOPNOTSUPP) {
1531 rdev_err(rdev, "failed to set temperature limits %pe\n",
1536 "IC does not support requested temperature limits\n");
1539 if (rdev->constraints->active_discharge && ops->set_active_discharge) {
1540 bool ad_state = (rdev->constraints->active_discharge ==
1541 REGULATOR_ACTIVE_DISCHARGE_ENABLE) ? true : false;
1543 ret = ops->set_active_discharge(rdev, ad_state);
1545 rdev_err(rdev, "failed to set active discharge: %pe\n", ERR_PTR(ret));
1551 * If there is no mechanism for controlling the regulator then
1552 * flag it as always_on so we don't end up duplicating checks
1553 * for this so much. Note that we could control the state of
1554 * a supply to control the output on a regulator that has no
1557 if (!rdev->ena_pin && !ops->enable) {
1558 if (rdev->supply_name && !rdev->supply)
1559 return -EPROBE_DEFER;
1562 rdev->constraints->always_on =
1563 rdev->supply->rdev->constraints->always_on;
1565 rdev->constraints->always_on = true;
1568 /* If the constraints say the regulator should be on at this point
1569 * and we have control then make sure it is enabled.
1571 if (rdev->constraints->always_on || rdev->constraints->boot_on) {
1572 /* If we want to enable this regulator, make sure that we know
1573 * the supplying regulator.
1575 if (rdev->supply_name && !rdev->supply)
1576 return -EPROBE_DEFER;
1579 ret = regulator_enable(rdev->supply);
1581 _regulator_put(rdev->supply);
1582 rdev->supply = NULL;
1587 ret = _regulator_do_enable(rdev);
1588 if (ret < 0 && ret != -EINVAL) {
1589 rdev_err(rdev, "failed to enable: %pe\n", ERR_PTR(ret));
1593 if (rdev->constraints->always_on)
1595 } else if (rdev->desc->off_on_delay) {
1596 rdev->last_off = ktime_get();
1599 print_constraints(rdev);
1604 * set_supply - set regulator supply regulator
1605 * @rdev: regulator name
1606 * @supply_rdev: supply regulator name
1608 * Called by platform initialisation code to set the supply regulator for this
1609 * regulator. This ensures that a regulators supply will also be enabled by the
1610 * core if it's child is enabled.
1612 static int set_supply(struct regulator_dev *rdev,
1613 struct regulator_dev *supply_rdev)
1617 rdev_dbg(rdev, "supplied by %s\n", rdev_get_name(supply_rdev));
1619 if (!try_module_get(supply_rdev->owner))
1622 rdev->supply = create_regulator(supply_rdev, &rdev->dev, "SUPPLY");
1623 if (rdev->supply == NULL) {
1627 supply_rdev->open_count++;
1633 * set_consumer_device_supply - Bind a regulator to a symbolic supply
1634 * @rdev: regulator source
1635 * @consumer_dev_name: dev_name() string for device supply applies to
1636 * @supply: symbolic name for supply
1638 * Allows platform initialisation code to map physical regulator
1639 * sources to symbolic names for supplies for use by devices. Devices
1640 * should use these symbolic names to request regulators, avoiding the
1641 * need to provide board-specific regulator names as platform data.
1643 static int set_consumer_device_supply(struct regulator_dev *rdev,
1644 const char *consumer_dev_name,
1647 struct regulator_map *node, *new_node;
1653 if (consumer_dev_name != NULL)
1658 new_node = kzalloc(sizeof(struct regulator_map), GFP_KERNEL);
1659 if (new_node == NULL)
1662 new_node->regulator = rdev;
1663 new_node->supply = supply;
1666 new_node->dev_name = kstrdup(consumer_dev_name, GFP_KERNEL);
1667 if (new_node->dev_name == NULL) {
1673 mutex_lock(®ulator_list_mutex);
1674 list_for_each_entry(node, ®ulator_map_list, list) {
1675 if (node->dev_name && consumer_dev_name) {
1676 if (strcmp(node->dev_name, consumer_dev_name) != 0)
1678 } else if (node->dev_name || consumer_dev_name) {
1682 if (strcmp(node->supply, supply) != 0)
1685 pr_debug("%s: %s/%s is '%s' supply; fail %s/%s\n",
1687 dev_name(&node->regulator->dev),
1688 node->regulator->desc->name,
1690 dev_name(&rdev->dev), rdev_get_name(rdev));
1694 list_add(&new_node->list, ®ulator_map_list);
1695 mutex_unlock(®ulator_list_mutex);
1700 mutex_unlock(®ulator_list_mutex);
1701 kfree(new_node->dev_name);
1706 static void unset_regulator_supplies(struct regulator_dev *rdev)
1708 struct regulator_map *node, *n;
1710 list_for_each_entry_safe(node, n, ®ulator_map_list, list) {
1711 if (rdev == node->regulator) {
1712 list_del(&node->list);
1713 kfree(node->dev_name);
1719 #ifdef CONFIG_DEBUG_FS
1720 static ssize_t constraint_flags_read_file(struct file *file,
1721 char __user *user_buf,
1722 size_t count, loff_t *ppos)
1724 const struct regulator *regulator = file->private_data;
1725 const struct regulation_constraints *c = regulator->rdev->constraints;
1732 buf = kmalloc(PAGE_SIZE, GFP_KERNEL);
1736 ret = snprintf(buf, PAGE_SIZE,
1740 "ramp_disable: %u\n"
1743 "over_current_protection: %u\n",
1750 c->over_current_protection);
1752 ret = simple_read_from_buffer(user_buf, count, ppos, buf, ret);
1760 static const struct file_operations constraint_flags_fops = {
1761 #ifdef CONFIG_DEBUG_FS
1762 .open = simple_open,
1763 .read = constraint_flags_read_file,
1764 .llseek = default_llseek,
1768 #define REG_STR_SIZE 64
1770 static struct regulator *create_regulator(struct regulator_dev *rdev,
1772 const char *supply_name)
1774 struct regulator *regulator;
1778 char buf[REG_STR_SIZE];
1781 size = snprintf(buf, REG_STR_SIZE, "%s-%s",
1782 dev->kobj.name, supply_name);
1783 if (size >= REG_STR_SIZE)
1786 supply_name = kstrdup(buf, GFP_KERNEL);
1787 if (supply_name == NULL)
1790 supply_name = kstrdup_const(supply_name, GFP_KERNEL);
1791 if (supply_name == NULL)
1795 regulator = kzalloc(sizeof(*regulator), GFP_KERNEL);
1796 if (regulator == NULL) {
1801 regulator->rdev = rdev;
1802 regulator->supply_name = supply_name;
1804 regulator_lock(rdev);
1805 list_add(®ulator->list, &rdev->consumer_list);
1806 regulator_unlock(rdev);
1809 regulator->dev = dev;
1811 /* Add a link to the device sysfs entry */
1812 err = sysfs_create_link_nowarn(&rdev->dev.kobj, &dev->kobj,
1815 rdev_dbg(rdev, "could not add device link %s: %pe\n",
1816 dev->kobj.name, ERR_PTR(err));
1822 regulator->debugfs = debugfs_create_dir(supply_name, rdev->debugfs);
1823 if (!regulator->debugfs) {
1824 rdev_dbg(rdev, "Failed to create debugfs directory\n");
1826 debugfs_create_u32("uA_load", 0444, regulator->debugfs,
1827 ®ulator->uA_load);
1828 debugfs_create_u32("min_uV", 0444, regulator->debugfs,
1829 ®ulator->voltage[PM_SUSPEND_ON].min_uV);
1830 debugfs_create_u32("max_uV", 0444, regulator->debugfs,
1831 ®ulator->voltage[PM_SUSPEND_ON].max_uV);
1832 debugfs_create_file("constraint_flags", 0444,
1833 regulator->debugfs, regulator,
1834 &constraint_flags_fops);
1838 * Check now if the regulator is an always on regulator - if
1839 * it is then we don't need to do nearly so much work for
1840 * enable/disable calls.
1842 if (!regulator_ops_is_valid(rdev, REGULATOR_CHANGE_STATUS) &&
1843 _regulator_is_enabled(rdev))
1844 regulator->always_on = true;
1849 static int _regulator_get_enable_time(struct regulator_dev *rdev)
1851 if (rdev->constraints && rdev->constraints->enable_time)
1852 return rdev->constraints->enable_time;
1853 if (rdev->desc->ops->enable_time)
1854 return rdev->desc->ops->enable_time(rdev);
1855 return rdev->desc->enable_time;
1858 static struct regulator_supply_alias *regulator_find_supply_alias(
1859 struct device *dev, const char *supply)
1861 struct regulator_supply_alias *map;
1863 list_for_each_entry(map, ®ulator_supply_alias_list, list)
1864 if (map->src_dev == dev && strcmp(map->src_supply, supply) == 0)
1870 static void regulator_supply_alias(struct device **dev, const char **supply)
1872 struct regulator_supply_alias *map;
1874 map = regulator_find_supply_alias(*dev, *supply);
1876 dev_dbg(*dev, "Mapping supply %s to %s,%s\n",
1877 *supply, map->alias_supply,
1878 dev_name(map->alias_dev));
1879 *dev = map->alias_dev;
1880 *supply = map->alias_supply;
1884 static int regulator_match(struct device *dev, const void *data)
1886 struct regulator_dev *r = dev_to_rdev(dev);
1888 return strcmp(rdev_get_name(r), data) == 0;
1891 static struct regulator_dev *regulator_lookup_by_name(const char *name)
1895 dev = class_find_device(®ulator_class, NULL, name, regulator_match);
1897 return dev ? dev_to_rdev(dev) : NULL;
1901 * regulator_dev_lookup - lookup a regulator device.
1902 * @dev: device for regulator "consumer".
1903 * @supply: Supply name or regulator ID.
1905 * If successful, returns a struct regulator_dev that corresponds to the name
1906 * @supply and with the embedded struct device refcount incremented by one.
1907 * The refcount must be dropped by calling put_device().
1908 * On failure one of the following ERR-PTR-encoded values is returned:
1909 * -ENODEV if lookup fails permanently, -EPROBE_DEFER if lookup could succeed
1912 static struct regulator_dev *regulator_dev_lookup(struct device *dev,
1915 struct regulator_dev *r = NULL;
1916 struct device_node *node;
1917 struct regulator_map *map;
1918 const char *devname = NULL;
1920 regulator_supply_alias(&dev, &supply);
1922 /* first do a dt based lookup */
1923 if (dev && dev->of_node) {
1924 node = of_get_regulator(dev, supply);
1926 r = of_find_regulator_by_node(node);
1931 * We have a node, but there is no device.
1932 * assume it has not registered yet.
1934 return ERR_PTR(-EPROBE_DEFER);
1938 /* if not found, try doing it non-dt way */
1940 devname = dev_name(dev);
1942 mutex_lock(®ulator_list_mutex);
1943 list_for_each_entry(map, ®ulator_map_list, list) {
1944 /* If the mapping has a device set up it must match */
1945 if (map->dev_name &&
1946 (!devname || strcmp(map->dev_name, devname)))
1949 if (strcmp(map->supply, supply) == 0 &&
1950 get_device(&map->regulator->dev)) {
1955 mutex_unlock(®ulator_list_mutex);
1960 r = regulator_lookup_by_name(supply);
1964 return ERR_PTR(-ENODEV);
1967 static int regulator_resolve_supply(struct regulator_dev *rdev)
1969 struct regulator_dev *r;
1970 struct device *dev = rdev->dev.parent;
1973 /* No supply to resolve? */
1974 if (!rdev->supply_name)
1977 /* Supply already resolved? (fast-path without locking contention) */
1981 r = regulator_dev_lookup(dev, rdev->supply_name);
1985 /* Did the lookup explicitly defer for us? */
1986 if (ret == -EPROBE_DEFER)
1989 if (have_full_constraints()) {
1990 r = dummy_regulator_rdev;
1991 get_device(&r->dev);
1993 dev_err(dev, "Failed to resolve %s-supply for %s\n",
1994 rdev->supply_name, rdev->desc->name);
1995 ret = -EPROBE_DEFER;
2001 dev_err(dev, "Supply for %s (%s) resolved to itself\n",
2002 rdev->desc->name, rdev->supply_name);
2003 if (!have_full_constraints()) {
2007 r = dummy_regulator_rdev;
2008 get_device(&r->dev);
2012 * If the supply's parent device is not the same as the
2013 * regulator's parent device, then ensure the parent device
2014 * is bound before we resolve the supply, in case the parent
2015 * device get probe deferred and unregisters the supply.
2017 if (r->dev.parent && r->dev.parent != rdev->dev.parent) {
2018 if (!device_is_bound(r->dev.parent)) {
2019 put_device(&r->dev);
2020 ret = -EPROBE_DEFER;
2025 /* Recursively resolve the supply of the supply */
2026 ret = regulator_resolve_supply(r);
2028 put_device(&r->dev);
2033 * Recheck rdev->supply with rdev->mutex lock held to avoid a race
2034 * between rdev->supply null check and setting rdev->supply in
2035 * set_supply() from concurrent tasks.
2037 regulator_lock(rdev);
2039 /* Supply just resolved by a concurrent task? */
2041 regulator_unlock(rdev);
2042 put_device(&r->dev);
2046 ret = set_supply(rdev, r);
2048 regulator_unlock(rdev);
2049 put_device(&r->dev);
2053 regulator_unlock(rdev);
2056 * In set_machine_constraints() we may have turned this regulator on
2057 * but we couldn't propagate to the supply if it hadn't been resolved
2060 if (rdev->use_count) {
2061 ret = regulator_enable(rdev->supply);
2063 _regulator_put(rdev->supply);
2064 rdev->supply = NULL;
2073 /* Internal regulator request function */
2074 struct regulator *_regulator_get(struct device *dev, const char *id,
2075 enum regulator_get_type get_type)
2077 struct regulator_dev *rdev;
2078 struct regulator *regulator;
2079 struct device_link *link;
2082 if (get_type >= MAX_GET_TYPE) {
2083 dev_err(dev, "invalid type %d in %s\n", get_type, __func__);
2084 return ERR_PTR(-EINVAL);
2088 pr_err("get() with no identifier\n");
2089 return ERR_PTR(-EINVAL);
2092 rdev = regulator_dev_lookup(dev, id);
2094 ret = PTR_ERR(rdev);
2097 * If regulator_dev_lookup() fails with error other
2098 * than -ENODEV our job here is done, we simply return it.
2101 return ERR_PTR(ret);
2103 if (!have_full_constraints()) {
2105 "incomplete constraints, dummy supplies not allowed\n");
2106 return ERR_PTR(-ENODEV);
2112 * Assume that a regulator is physically present and
2113 * enabled, even if it isn't hooked up, and just
2116 dev_warn(dev, "supply %s not found, using dummy regulator\n", id);
2117 rdev = dummy_regulator_rdev;
2118 get_device(&rdev->dev);
2123 "dummy supplies not allowed for exclusive requests\n");
2127 return ERR_PTR(-ENODEV);
2131 if (rdev->exclusive) {
2132 regulator = ERR_PTR(-EPERM);
2133 put_device(&rdev->dev);
2137 if (get_type == EXCLUSIVE_GET && rdev->open_count) {
2138 regulator = ERR_PTR(-EBUSY);
2139 put_device(&rdev->dev);
2143 mutex_lock(®ulator_list_mutex);
2144 ret = (rdev->coupling_desc.n_resolved != rdev->coupling_desc.n_coupled);
2145 mutex_unlock(®ulator_list_mutex);
2148 regulator = ERR_PTR(-EPROBE_DEFER);
2149 put_device(&rdev->dev);
2153 ret = regulator_resolve_supply(rdev);
2155 regulator = ERR_PTR(ret);
2156 put_device(&rdev->dev);
2160 if (!try_module_get(rdev->owner)) {
2161 regulator = ERR_PTR(-EPROBE_DEFER);
2162 put_device(&rdev->dev);
2166 regulator = create_regulator(rdev, dev, id);
2167 if (regulator == NULL) {
2168 regulator = ERR_PTR(-ENOMEM);
2169 module_put(rdev->owner);
2170 put_device(&rdev->dev);
2175 if (get_type == EXCLUSIVE_GET) {
2176 rdev->exclusive = 1;
2178 ret = _regulator_is_enabled(rdev);
2180 rdev->use_count = 1;
2181 regulator->enable_count = 1;
2183 rdev->use_count = 0;
2184 regulator->enable_count = 0;
2188 link = device_link_add(dev, &rdev->dev, DL_FLAG_STATELESS);
2189 if (!IS_ERR_OR_NULL(link))
2190 regulator->device_link = true;
2196 * regulator_get - lookup and obtain a reference to a regulator.
2197 * @dev: device for regulator "consumer"
2198 * @id: Supply name or regulator ID.
2200 * Returns a struct regulator corresponding to the regulator producer,
2201 * or IS_ERR() condition containing errno.
2203 * Use of supply names configured via set_consumer_device_supply() is
2204 * strongly encouraged. It is recommended that the supply name used
2205 * should match the name used for the supply and/or the relevant
2206 * device pins in the datasheet.
2208 struct regulator *regulator_get(struct device *dev, const char *id)
2210 return _regulator_get(dev, id, NORMAL_GET);
2212 EXPORT_SYMBOL_GPL(regulator_get);
2215 * regulator_get_exclusive - obtain exclusive access to a regulator.
2216 * @dev: device for regulator "consumer"
2217 * @id: Supply name or regulator ID.
2219 * Returns a struct regulator corresponding to the regulator producer,
2220 * or IS_ERR() condition containing errno. Other consumers will be
2221 * unable to obtain this regulator while this reference is held and the
2222 * use count for the regulator will be initialised to reflect the current
2223 * state of the regulator.
2225 * This is intended for use by consumers which cannot tolerate shared
2226 * use of the regulator such as those which need to force the
2227 * regulator off for correct operation of the hardware they are
2230 * Use of supply names configured via set_consumer_device_supply() is
2231 * strongly encouraged. It is recommended that the supply name used
2232 * should match the name used for the supply and/or the relevant
2233 * device pins in the datasheet.
2235 struct regulator *regulator_get_exclusive(struct device *dev, const char *id)
2237 return _regulator_get(dev, id, EXCLUSIVE_GET);
2239 EXPORT_SYMBOL_GPL(regulator_get_exclusive);
2242 * regulator_get_optional - obtain optional access to a regulator.
2243 * @dev: device for regulator "consumer"
2244 * @id: Supply name or regulator ID.
2246 * Returns a struct regulator corresponding to the regulator producer,
2247 * or IS_ERR() condition containing errno.
2249 * This is intended for use by consumers for devices which can have
2250 * some supplies unconnected in normal use, such as some MMC devices.
2251 * It can allow the regulator core to provide stub supplies for other
2252 * supplies requested using normal regulator_get() calls without
2253 * disrupting the operation of drivers that can handle absent
2256 * Use of supply names configured via set_consumer_device_supply() is
2257 * strongly encouraged. It is recommended that the supply name used
2258 * should match the name used for the supply and/or the relevant
2259 * device pins in the datasheet.
2261 struct regulator *regulator_get_optional(struct device *dev, const char *id)
2263 return _regulator_get(dev, id, OPTIONAL_GET);
2265 EXPORT_SYMBOL_GPL(regulator_get_optional);
2267 static void destroy_regulator(struct regulator *regulator)
2269 struct regulator_dev *rdev = regulator->rdev;
2271 debugfs_remove_recursive(regulator->debugfs);
2273 if (regulator->dev) {
2274 if (regulator->device_link)
2275 device_link_remove(regulator->dev, &rdev->dev);
2277 /* remove any sysfs entries */
2278 sysfs_remove_link(&rdev->dev.kobj, regulator->supply_name);
2281 regulator_lock(rdev);
2282 list_del(®ulator->list);
2285 rdev->exclusive = 0;
2286 regulator_unlock(rdev);
2288 kfree_const(regulator->supply_name);
2292 /* regulator_list_mutex lock held by regulator_put() */
2293 static void _regulator_put(struct regulator *regulator)
2295 struct regulator_dev *rdev;
2297 if (IS_ERR_OR_NULL(regulator))
2300 lockdep_assert_held_once(®ulator_list_mutex);
2302 /* Docs say you must disable before calling regulator_put() */
2303 WARN_ON(regulator->enable_count);
2305 rdev = regulator->rdev;
2307 destroy_regulator(regulator);
2309 module_put(rdev->owner);
2310 put_device(&rdev->dev);
2314 * regulator_put - "free" the regulator source
2315 * @regulator: regulator source
2317 * Note: drivers must ensure that all regulator_enable calls made on this
2318 * regulator source are balanced by regulator_disable calls prior to calling
2321 void regulator_put(struct regulator *regulator)
2323 mutex_lock(®ulator_list_mutex);
2324 _regulator_put(regulator);
2325 mutex_unlock(®ulator_list_mutex);
2327 EXPORT_SYMBOL_GPL(regulator_put);
2330 * regulator_register_supply_alias - Provide device alias for supply lookup
2332 * @dev: device that will be given as the regulator "consumer"
2333 * @id: Supply name or regulator ID
2334 * @alias_dev: device that should be used to lookup the supply
2335 * @alias_id: Supply name or regulator ID that should be used to lookup the
2338 * All lookups for id on dev will instead be conducted for alias_id on
2341 int regulator_register_supply_alias(struct device *dev, const char *id,
2342 struct device *alias_dev,
2343 const char *alias_id)
2345 struct regulator_supply_alias *map;
2347 map = regulator_find_supply_alias(dev, id);
2351 map = kzalloc(sizeof(struct regulator_supply_alias), GFP_KERNEL);
2356 map->src_supply = id;
2357 map->alias_dev = alias_dev;
2358 map->alias_supply = alias_id;
2360 list_add(&map->list, ®ulator_supply_alias_list);
2362 pr_info("Adding alias for supply %s,%s -> %s,%s\n",
2363 id, dev_name(dev), alias_id, dev_name(alias_dev));
2367 EXPORT_SYMBOL_GPL(regulator_register_supply_alias);
2370 * regulator_unregister_supply_alias - Remove device alias
2372 * @dev: device that will be given as the regulator "consumer"
2373 * @id: Supply name or regulator ID
2375 * Remove a lookup alias if one exists for id on dev.
2377 void regulator_unregister_supply_alias(struct device *dev, const char *id)
2379 struct regulator_supply_alias *map;
2381 map = regulator_find_supply_alias(dev, id);
2383 list_del(&map->list);
2387 EXPORT_SYMBOL_GPL(regulator_unregister_supply_alias);
2390 * regulator_bulk_register_supply_alias - register multiple aliases
2392 * @dev: device that will be given as the regulator "consumer"
2393 * @id: List of supply names or regulator IDs
2394 * @alias_dev: device that should be used to lookup the supply
2395 * @alias_id: List of supply names or regulator IDs that should be used to
2397 * @num_id: Number of aliases to register
2399 * @return 0 on success, an errno on failure.
2401 * This helper function allows drivers to register several supply
2402 * aliases in one operation. If any of the aliases cannot be
2403 * registered any aliases that were registered will be removed
2404 * before returning to the caller.
2406 int regulator_bulk_register_supply_alias(struct device *dev,
2407 const char *const *id,
2408 struct device *alias_dev,
2409 const char *const *alias_id,
2415 for (i = 0; i < num_id; ++i) {
2416 ret = regulator_register_supply_alias(dev, id[i], alias_dev,
2426 "Failed to create supply alias %s,%s -> %s,%s\n",
2427 id[i], dev_name(dev), alias_id[i], dev_name(alias_dev));
2430 regulator_unregister_supply_alias(dev, id[i]);
2434 EXPORT_SYMBOL_GPL(regulator_bulk_register_supply_alias);
2437 * regulator_bulk_unregister_supply_alias - unregister multiple aliases
2439 * @dev: device that will be given as the regulator "consumer"
2440 * @id: List of supply names or regulator IDs
2441 * @num_id: Number of aliases to unregister
2443 * This helper function allows drivers to unregister several supply
2444 * aliases in one operation.
2446 void regulator_bulk_unregister_supply_alias(struct device *dev,
2447 const char *const *id,
2452 for (i = 0; i < num_id; ++i)
2453 regulator_unregister_supply_alias(dev, id[i]);
2455 EXPORT_SYMBOL_GPL(regulator_bulk_unregister_supply_alias);
2458 /* Manage enable GPIO list. Same GPIO pin can be shared among regulators */
2459 static int regulator_ena_gpio_request(struct regulator_dev *rdev,
2460 const struct regulator_config *config)
2462 struct regulator_enable_gpio *pin, *new_pin;
2463 struct gpio_desc *gpiod;
2465 gpiod = config->ena_gpiod;
2466 new_pin = kzalloc(sizeof(*new_pin), GFP_KERNEL);
2468 mutex_lock(®ulator_list_mutex);
2470 list_for_each_entry(pin, ®ulator_ena_gpio_list, list) {
2471 if (pin->gpiod == gpiod) {
2472 rdev_dbg(rdev, "GPIO is already used\n");
2473 goto update_ena_gpio_to_rdev;
2477 if (new_pin == NULL) {
2478 mutex_unlock(®ulator_list_mutex);
2486 list_add(&pin->list, ®ulator_ena_gpio_list);
2488 update_ena_gpio_to_rdev:
2489 pin->request_count++;
2490 rdev->ena_pin = pin;
2492 mutex_unlock(®ulator_list_mutex);
2498 static void regulator_ena_gpio_free(struct regulator_dev *rdev)
2500 struct regulator_enable_gpio *pin, *n;
2505 /* Free the GPIO only in case of no use */
2506 list_for_each_entry_safe(pin, n, ®ulator_ena_gpio_list, list) {
2507 if (pin != rdev->ena_pin)
2510 if (--pin->request_count)
2513 gpiod_put(pin->gpiod);
2514 list_del(&pin->list);
2519 rdev->ena_pin = NULL;
2523 * regulator_ena_gpio_ctrl - balance enable_count of each GPIO and actual GPIO pin control
2524 * @rdev: regulator_dev structure
2525 * @enable: enable GPIO at initial use?
2527 * GPIO is enabled in case of initial use. (enable_count is 0)
2528 * GPIO is disabled when it is not shared any more. (enable_count <= 1)
2530 static int regulator_ena_gpio_ctrl(struct regulator_dev *rdev, bool enable)
2532 struct regulator_enable_gpio *pin = rdev->ena_pin;
2538 /* Enable GPIO at initial use */
2539 if (pin->enable_count == 0)
2540 gpiod_set_value_cansleep(pin->gpiod, 1);
2542 pin->enable_count++;
2544 if (pin->enable_count > 1) {
2545 pin->enable_count--;
2549 /* Disable GPIO if not used */
2550 if (pin->enable_count <= 1) {
2551 gpiod_set_value_cansleep(pin->gpiod, 0);
2552 pin->enable_count = 0;
2560 * _regulator_delay_helper - a delay helper function
2561 * @delay: time to delay in microseconds
2563 * Delay for the requested amount of time as per the guidelines in:
2565 * Documentation/timers/timers-howto.rst
2567 * The assumption here is that these regulator operations will never used in
2568 * atomic context and therefore sleeping functions can be used.
2570 static void _regulator_delay_helper(unsigned int delay)
2572 unsigned int ms = delay / 1000;
2573 unsigned int us = delay % 1000;
2577 * For small enough values, handle super-millisecond
2578 * delays in the usleep_range() call below.
2587 * Give the scheduler some room to coalesce with any other
2588 * wakeup sources. For delays shorter than 10 us, don't even
2589 * bother setting up high-resolution timers and just busy-
2593 usleep_range(us, us + 100);
2599 * _regulator_check_status_enabled
2601 * A helper function to check if the regulator status can be interpreted
2602 * as 'regulator is enabled'.
2603 * @rdev: the regulator device to check
2606 * * 1 - if status shows regulator is in enabled state
2607 * * 0 - if not enabled state
2608 * * Error Value - as received from ops->get_status()
2610 static inline int _regulator_check_status_enabled(struct regulator_dev *rdev)
2612 int ret = rdev->desc->ops->get_status(rdev);
2615 rdev_info(rdev, "get_status returned error: %d\n", ret);
2620 case REGULATOR_STATUS_OFF:
2621 case REGULATOR_STATUS_ERROR:
2622 case REGULATOR_STATUS_UNDEFINED:
2629 static int _regulator_do_enable(struct regulator_dev *rdev)
2633 /* Query before enabling in case configuration dependent. */
2634 ret = _regulator_get_enable_time(rdev);
2638 rdev_warn(rdev, "enable_time() failed: %pe\n", ERR_PTR(ret));
2642 trace_regulator_enable(rdev_get_name(rdev));
2644 if (rdev->desc->off_on_delay && rdev->last_off) {
2645 /* if needed, keep a distance of off_on_delay from last time
2646 * this regulator was disabled.
2648 ktime_t end = ktime_add_us(rdev->last_off, rdev->desc->off_on_delay);
2649 s64 remaining = ktime_us_delta(end, ktime_get());
2652 _regulator_delay_helper(remaining);
2655 if (rdev->ena_pin) {
2656 if (!rdev->ena_gpio_state) {
2657 ret = regulator_ena_gpio_ctrl(rdev, true);
2660 rdev->ena_gpio_state = 1;
2662 } else if (rdev->desc->ops->enable) {
2663 ret = rdev->desc->ops->enable(rdev);
2670 /* Allow the regulator to ramp; it would be useful to extend
2671 * this for bulk operations so that the regulators can ramp
2674 trace_regulator_enable_delay(rdev_get_name(rdev));
2676 /* If poll_enabled_time is set, poll upto the delay calculated
2677 * above, delaying poll_enabled_time uS to check if the regulator
2678 * actually got enabled.
2679 * If the regulator isn't enabled after our delay helper has expired,
2680 * return -ETIMEDOUT.
2682 if (rdev->desc->poll_enabled_time) {
2683 unsigned int time_remaining = delay;
2685 while (time_remaining > 0) {
2686 _regulator_delay_helper(rdev->desc->poll_enabled_time);
2688 if (rdev->desc->ops->get_status) {
2689 ret = _regulator_check_status_enabled(rdev);
2694 } else if (rdev->desc->ops->is_enabled(rdev))
2697 time_remaining -= rdev->desc->poll_enabled_time;
2700 if (time_remaining <= 0) {
2701 rdev_err(rdev, "Enabled check timed out\n");
2705 _regulator_delay_helper(delay);
2708 trace_regulator_enable_complete(rdev_get_name(rdev));
2714 * _regulator_handle_consumer_enable - handle that a consumer enabled
2715 * @regulator: regulator source
2717 * Some things on a regulator consumer (like the contribution towards total
2718 * load on the regulator) only have an effect when the consumer wants the
2719 * regulator enabled. Explained in example with two consumers of the same
2721 * consumer A: set_load(100); => total load = 0
2722 * consumer A: regulator_enable(); => total load = 100
2723 * consumer B: set_load(1000); => total load = 100
2724 * consumer B: regulator_enable(); => total load = 1100
2725 * consumer A: regulator_disable(); => total_load = 1000
2727 * This function (together with _regulator_handle_consumer_disable) is
2728 * responsible for keeping track of the refcount for a given regulator consumer
2729 * and applying / unapplying these things.
2731 * Returns 0 upon no error; -error upon error.
2733 static int _regulator_handle_consumer_enable(struct regulator *regulator)
2736 struct regulator_dev *rdev = regulator->rdev;
2738 lockdep_assert_held_once(&rdev->mutex.base);
2740 regulator->enable_count++;
2741 if (regulator->uA_load && regulator->enable_count == 1) {
2742 ret = drms_uA_update(rdev);
2744 regulator->enable_count--;
2752 * _regulator_handle_consumer_disable - handle that a consumer disabled
2753 * @regulator: regulator source
2755 * The opposite of _regulator_handle_consumer_enable().
2757 * Returns 0 upon no error; -error upon error.
2759 static int _regulator_handle_consumer_disable(struct regulator *regulator)
2761 struct regulator_dev *rdev = regulator->rdev;
2763 lockdep_assert_held_once(&rdev->mutex.base);
2765 if (!regulator->enable_count) {
2766 rdev_err(rdev, "Underflow of regulator enable count\n");
2770 regulator->enable_count--;
2771 if (regulator->uA_load && regulator->enable_count == 0)
2772 return drms_uA_update(rdev);
2777 /* locks held by regulator_enable() */
2778 static int _regulator_enable(struct regulator *regulator)
2780 struct regulator_dev *rdev = regulator->rdev;
2783 lockdep_assert_held_once(&rdev->mutex.base);
2785 if (rdev->use_count == 0 && rdev->supply) {
2786 ret = _regulator_enable(rdev->supply);
2791 /* balance only if there are regulators coupled */
2792 if (rdev->coupling_desc.n_coupled > 1) {
2793 ret = regulator_balance_voltage(rdev, PM_SUSPEND_ON);
2795 goto err_disable_supply;
2798 ret = _regulator_handle_consumer_enable(regulator);
2800 goto err_disable_supply;
2802 if (rdev->use_count == 0) {
2804 * The regulator may already be enabled if it's not switchable
2807 ret = _regulator_is_enabled(rdev);
2808 if (ret == -EINVAL || ret == 0) {
2809 if (!regulator_ops_is_valid(rdev,
2810 REGULATOR_CHANGE_STATUS)) {
2812 goto err_consumer_disable;
2815 ret = _regulator_do_enable(rdev);
2817 goto err_consumer_disable;
2819 _notifier_call_chain(rdev, REGULATOR_EVENT_ENABLE,
2821 } else if (ret < 0) {
2822 rdev_err(rdev, "is_enabled() failed: %pe\n", ERR_PTR(ret));
2823 goto err_consumer_disable;
2825 /* Fallthrough on positive return values - already enabled */
2832 err_consumer_disable:
2833 _regulator_handle_consumer_disable(regulator);
2836 if (rdev->use_count == 0 && rdev->supply)
2837 _regulator_disable(rdev->supply);
2843 * regulator_enable - enable regulator output
2844 * @regulator: regulator source
2846 * Request that the regulator be enabled with the regulator output at
2847 * the predefined voltage or current value. Calls to regulator_enable()
2848 * must be balanced with calls to regulator_disable().
2850 * NOTE: the output value can be set by other drivers, boot loader or may be
2851 * hardwired in the regulator.
2853 int regulator_enable(struct regulator *regulator)
2855 struct regulator_dev *rdev = regulator->rdev;
2856 struct ww_acquire_ctx ww_ctx;
2859 regulator_lock_dependent(rdev, &ww_ctx);
2860 ret = _regulator_enable(regulator);
2861 regulator_unlock_dependent(rdev, &ww_ctx);
2865 EXPORT_SYMBOL_GPL(regulator_enable);
2867 static int _regulator_do_disable(struct regulator_dev *rdev)
2871 trace_regulator_disable(rdev_get_name(rdev));
2873 if (rdev->ena_pin) {
2874 if (rdev->ena_gpio_state) {
2875 ret = regulator_ena_gpio_ctrl(rdev, false);
2878 rdev->ena_gpio_state = 0;
2881 } else if (rdev->desc->ops->disable) {
2882 ret = rdev->desc->ops->disable(rdev);
2887 if (rdev->desc->off_on_delay)
2888 rdev->last_off = ktime_get();
2890 trace_regulator_disable_complete(rdev_get_name(rdev));
2895 /* locks held by regulator_disable() */
2896 static int _regulator_disable(struct regulator *regulator)
2898 struct regulator_dev *rdev = regulator->rdev;
2901 lockdep_assert_held_once(&rdev->mutex.base);
2903 if (WARN(rdev->use_count <= 0,
2904 "unbalanced disables for %s\n", rdev_get_name(rdev)))
2907 /* are we the last user and permitted to disable ? */
2908 if (rdev->use_count == 1 &&
2909 (rdev->constraints && !rdev->constraints->always_on)) {
2911 /* we are last user */
2912 if (regulator_ops_is_valid(rdev, REGULATOR_CHANGE_STATUS)) {
2913 ret = _notifier_call_chain(rdev,
2914 REGULATOR_EVENT_PRE_DISABLE,
2916 if (ret & NOTIFY_STOP_MASK)
2919 ret = _regulator_do_disable(rdev);
2921 rdev_err(rdev, "failed to disable: %pe\n", ERR_PTR(ret));
2922 _notifier_call_chain(rdev,
2923 REGULATOR_EVENT_ABORT_DISABLE,
2927 _notifier_call_chain(rdev, REGULATOR_EVENT_DISABLE,
2931 rdev->use_count = 0;
2932 } else if (rdev->use_count > 1) {
2937 ret = _regulator_handle_consumer_disable(regulator);
2939 if (ret == 0 && rdev->coupling_desc.n_coupled > 1)
2940 ret = regulator_balance_voltage(rdev, PM_SUSPEND_ON);
2942 if (ret == 0 && rdev->use_count == 0 && rdev->supply)
2943 ret = _regulator_disable(rdev->supply);
2949 * regulator_disable - disable regulator output
2950 * @regulator: regulator source
2952 * Disable the regulator output voltage or current. Calls to
2953 * regulator_enable() must be balanced with calls to
2954 * regulator_disable().
2956 * NOTE: this will only disable the regulator output if no other consumer
2957 * devices have it enabled, the regulator device supports disabling and
2958 * machine constraints permit this operation.
2960 int regulator_disable(struct regulator *regulator)
2962 struct regulator_dev *rdev = regulator->rdev;
2963 struct ww_acquire_ctx ww_ctx;
2966 regulator_lock_dependent(rdev, &ww_ctx);
2967 ret = _regulator_disable(regulator);
2968 regulator_unlock_dependent(rdev, &ww_ctx);
2972 EXPORT_SYMBOL_GPL(regulator_disable);
2974 /* locks held by regulator_force_disable() */
2975 static int _regulator_force_disable(struct regulator_dev *rdev)
2979 lockdep_assert_held_once(&rdev->mutex.base);
2981 ret = _notifier_call_chain(rdev, REGULATOR_EVENT_FORCE_DISABLE |
2982 REGULATOR_EVENT_PRE_DISABLE, NULL);
2983 if (ret & NOTIFY_STOP_MASK)
2986 ret = _regulator_do_disable(rdev);
2988 rdev_err(rdev, "failed to force disable: %pe\n", ERR_PTR(ret));
2989 _notifier_call_chain(rdev, REGULATOR_EVENT_FORCE_DISABLE |
2990 REGULATOR_EVENT_ABORT_DISABLE, NULL);
2994 _notifier_call_chain(rdev, REGULATOR_EVENT_FORCE_DISABLE |
2995 REGULATOR_EVENT_DISABLE, NULL);
3001 * regulator_force_disable - force disable regulator output
3002 * @regulator: regulator source
3004 * Forcibly disable the regulator output voltage or current.
3005 * NOTE: this *will* disable the regulator output even if other consumer
3006 * devices have it enabled. This should be used for situations when device
3007 * damage will likely occur if the regulator is not disabled (e.g. over temp).
3009 int regulator_force_disable(struct regulator *regulator)
3011 struct regulator_dev *rdev = regulator->rdev;
3012 struct ww_acquire_ctx ww_ctx;
3015 regulator_lock_dependent(rdev, &ww_ctx);
3017 ret = _regulator_force_disable(regulator->rdev);
3019 if (rdev->coupling_desc.n_coupled > 1)
3020 regulator_balance_voltage(rdev, PM_SUSPEND_ON);
3022 if (regulator->uA_load) {
3023 regulator->uA_load = 0;
3024 ret = drms_uA_update(rdev);
3027 if (rdev->use_count != 0 && rdev->supply)
3028 _regulator_disable(rdev->supply);
3030 regulator_unlock_dependent(rdev, &ww_ctx);
3034 EXPORT_SYMBOL_GPL(regulator_force_disable);
3036 static void regulator_disable_work(struct work_struct *work)
3038 struct regulator_dev *rdev = container_of(work, struct regulator_dev,
3040 struct ww_acquire_ctx ww_ctx;
3042 struct regulator *regulator;
3043 int total_count = 0;
3045 regulator_lock_dependent(rdev, &ww_ctx);
3048 * Workqueue functions queue the new work instance while the previous
3049 * work instance is being processed. Cancel the queued work instance
3050 * as the work instance under processing does the job of the queued
3053 cancel_delayed_work(&rdev->disable_work);
3055 list_for_each_entry(regulator, &rdev->consumer_list, list) {
3056 count = regulator->deferred_disables;
3061 total_count += count;
3062 regulator->deferred_disables = 0;
3064 for (i = 0; i < count; i++) {
3065 ret = _regulator_disable(regulator);
3067 rdev_err(rdev, "Deferred disable failed: %pe\n",
3071 WARN_ON(!total_count);
3073 if (rdev->coupling_desc.n_coupled > 1)
3074 regulator_balance_voltage(rdev, PM_SUSPEND_ON);
3076 regulator_unlock_dependent(rdev, &ww_ctx);
3080 * regulator_disable_deferred - disable regulator output with delay
3081 * @regulator: regulator source
3082 * @ms: milliseconds until the regulator is disabled
3084 * Execute regulator_disable() on the regulator after a delay. This
3085 * is intended for use with devices that require some time to quiesce.
3087 * NOTE: this will only disable the regulator output if no other consumer
3088 * devices have it enabled, the regulator device supports disabling and
3089 * machine constraints permit this operation.
3091 int regulator_disable_deferred(struct regulator *regulator, int ms)
3093 struct regulator_dev *rdev = regulator->rdev;
3096 return regulator_disable(regulator);
3098 regulator_lock(rdev);
3099 regulator->deferred_disables++;
3100 mod_delayed_work(system_power_efficient_wq, &rdev->disable_work,
3101 msecs_to_jiffies(ms));
3102 regulator_unlock(rdev);
3106 EXPORT_SYMBOL_GPL(regulator_disable_deferred);
3108 static int _regulator_is_enabled(struct regulator_dev *rdev)
3110 /* A GPIO control always takes precedence */
3112 return rdev->ena_gpio_state;
3114 /* If we don't know then assume that the regulator is always on */
3115 if (!rdev->desc->ops->is_enabled)
3118 return rdev->desc->ops->is_enabled(rdev);
3121 static int _regulator_list_voltage(struct regulator_dev *rdev,
3122 unsigned selector, int lock)
3124 const struct regulator_ops *ops = rdev->desc->ops;
3127 if (rdev->desc->fixed_uV && rdev->desc->n_voltages == 1 && !selector)
3128 return rdev->desc->fixed_uV;
3130 if (ops->list_voltage) {
3131 if (selector >= rdev->desc->n_voltages)
3133 if (selector < rdev->desc->linear_min_sel)
3136 regulator_lock(rdev);
3137 ret = ops->list_voltage(rdev, selector);
3139 regulator_unlock(rdev);
3140 } else if (rdev->is_switch && rdev->supply) {
3141 ret = _regulator_list_voltage(rdev->supply->rdev,
3148 if (ret < rdev->constraints->min_uV)
3150 else if (ret > rdev->constraints->max_uV)
3158 * regulator_is_enabled - is the regulator output enabled
3159 * @regulator: regulator source
3161 * Returns positive if the regulator driver backing the source/client
3162 * has requested that the device be enabled, zero if it hasn't, else a
3163 * negative errno code.
3165 * Note that the device backing this regulator handle can have multiple
3166 * users, so it might be enabled even if regulator_enable() was never
3167 * called for this particular source.
3169 int regulator_is_enabled(struct regulator *regulator)
3173 if (regulator->always_on)
3176 regulator_lock(regulator->rdev);
3177 ret = _regulator_is_enabled(regulator->rdev);
3178 regulator_unlock(regulator->rdev);
3182 EXPORT_SYMBOL_GPL(regulator_is_enabled);
3185 * regulator_count_voltages - count regulator_list_voltage() selectors
3186 * @regulator: regulator source
3188 * Returns number of selectors, or negative errno. Selectors are
3189 * numbered starting at zero, and typically correspond to bitfields
3190 * in hardware registers.
3192 int regulator_count_voltages(struct regulator *regulator)
3194 struct regulator_dev *rdev = regulator->rdev;
3196 if (rdev->desc->n_voltages)
3197 return rdev->desc->n_voltages;
3199 if (!rdev->is_switch || !rdev->supply)
3202 return regulator_count_voltages(rdev->supply);
3204 EXPORT_SYMBOL_GPL(regulator_count_voltages);
3207 * regulator_list_voltage - enumerate supported voltages
3208 * @regulator: regulator source
3209 * @selector: identify voltage to list
3210 * Context: can sleep
3212 * Returns a voltage that can be passed to @regulator_set_voltage(),
3213 * zero if this selector code can't be used on this system, or a
3216 int regulator_list_voltage(struct regulator *regulator, unsigned selector)
3218 return _regulator_list_voltage(regulator->rdev, selector, 1);
3220 EXPORT_SYMBOL_GPL(regulator_list_voltage);
3223 * regulator_get_regmap - get the regulator's register map
3224 * @regulator: regulator source
3226 * Returns the register map for the given regulator, or an ERR_PTR value
3227 * if the regulator doesn't use regmap.
3229 struct regmap *regulator_get_regmap(struct regulator *regulator)
3231 struct regmap *map = regulator->rdev->regmap;
3233 return map ? map : ERR_PTR(-EOPNOTSUPP);
3237 * regulator_get_hardware_vsel_register - get the HW voltage selector register
3238 * @regulator: regulator source
3239 * @vsel_reg: voltage selector register, output parameter
3240 * @vsel_mask: mask for voltage selector bitfield, output parameter
3242 * Returns the hardware register offset and bitmask used for setting the
3243 * regulator voltage. This might be useful when configuring voltage-scaling
3244 * hardware or firmware that can make I2C requests behind the kernel's back,
3247 * On success, the output parameters @vsel_reg and @vsel_mask are filled in
3248 * and 0 is returned, otherwise a negative errno is returned.
3250 int regulator_get_hardware_vsel_register(struct regulator *regulator,
3252 unsigned *vsel_mask)
3254 struct regulator_dev *rdev = regulator->rdev;
3255 const struct regulator_ops *ops = rdev->desc->ops;
3257 if (ops->set_voltage_sel != regulator_set_voltage_sel_regmap)
3260 *vsel_reg = rdev->desc->vsel_reg;
3261 *vsel_mask = rdev->desc->vsel_mask;
3265 EXPORT_SYMBOL_GPL(regulator_get_hardware_vsel_register);
3268 * regulator_list_hardware_vsel - get the HW-specific register value for a selector
3269 * @regulator: regulator source
3270 * @selector: identify voltage to list
3272 * Converts the selector to a hardware-specific voltage selector that can be
3273 * directly written to the regulator registers. The address of the voltage
3274 * register can be determined by calling @regulator_get_hardware_vsel_register.
3276 * On error a negative errno is returned.
3278 int regulator_list_hardware_vsel(struct regulator *regulator,
3281 struct regulator_dev *rdev = regulator->rdev;
3282 const struct regulator_ops *ops = rdev->desc->ops;
3284 if (selector >= rdev->desc->n_voltages)
3286 if (selector < rdev->desc->linear_min_sel)
3288 if (ops->set_voltage_sel != regulator_set_voltage_sel_regmap)
3293 EXPORT_SYMBOL_GPL(regulator_list_hardware_vsel);
3296 * regulator_get_linear_step - return the voltage step size between VSEL values
3297 * @regulator: regulator source
3299 * Returns the voltage step size between VSEL values for linear
3300 * regulators, or return 0 if the regulator isn't a linear regulator.
3302 unsigned int regulator_get_linear_step(struct regulator *regulator)
3304 struct regulator_dev *rdev = regulator->rdev;
3306 return rdev->desc->uV_step;
3308 EXPORT_SYMBOL_GPL(regulator_get_linear_step);
3311 * regulator_is_supported_voltage - check if a voltage range can be supported
3313 * @regulator: Regulator to check.
3314 * @min_uV: Minimum required voltage in uV.
3315 * @max_uV: Maximum required voltage in uV.
3317 * Returns a boolean.
3319 int regulator_is_supported_voltage(struct regulator *regulator,
3320 int min_uV, int max_uV)
3322 struct regulator_dev *rdev = regulator->rdev;
3323 int i, voltages, ret;
3325 /* If we can't change voltage check the current voltage */
3326 if (!regulator_ops_is_valid(rdev, REGULATOR_CHANGE_VOLTAGE)) {
3327 ret = regulator_get_voltage(regulator);
3329 return min_uV <= ret && ret <= max_uV;
3334 /* Any voltage within constrains range is fine? */
3335 if (rdev->desc->continuous_voltage_range)
3336 return min_uV >= rdev->constraints->min_uV &&
3337 max_uV <= rdev->constraints->max_uV;
3339 ret = regulator_count_voltages(regulator);
3344 for (i = 0; i < voltages; i++) {
3345 ret = regulator_list_voltage(regulator, i);
3347 if (ret >= min_uV && ret <= max_uV)
3353 EXPORT_SYMBOL_GPL(regulator_is_supported_voltage);
3355 static int regulator_map_voltage(struct regulator_dev *rdev, int min_uV,
3358 const struct regulator_desc *desc = rdev->desc;
3360 if (desc->ops->map_voltage)
3361 return desc->ops->map_voltage(rdev, min_uV, max_uV);
3363 if (desc->ops->list_voltage == regulator_list_voltage_linear)
3364 return regulator_map_voltage_linear(rdev, min_uV, max_uV);
3366 if (desc->ops->list_voltage == regulator_list_voltage_linear_range)
3367 return regulator_map_voltage_linear_range(rdev, min_uV, max_uV);
3369 if (desc->ops->list_voltage ==
3370 regulator_list_voltage_pickable_linear_range)
3371 return regulator_map_voltage_pickable_linear_range(rdev,
3374 return regulator_map_voltage_iterate(rdev, min_uV, max_uV);
3377 static int _regulator_call_set_voltage(struct regulator_dev *rdev,
3378 int min_uV, int max_uV,
3381 struct pre_voltage_change_data data;
3384 data.old_uV = regulator_get_voltage_rdev(rdev);
3385 data.min_uV = min_uV;
3386 data.max_uV = max_uV;
3387 ret = _notifier_call_chain(rdev, REGULATOR_EVENT_PRE_VOLTAGE_CHANGE,
3389 if (ret & NOTIFY_STOP_MASK)
3392 ret = rdev->desc->ops->set_voltage(rdev, min_uV, max_uV, selector);
3396 _notifier_call_chain(rdev, REGULATOR_EVENT_ABORT_VOLTAGE_CHANGE,
3397 (void *)data.old_uV);
3402 static int _regulator_call_set_voltage_sel(struct regulator_dev *rdev,
3403 int uV, unsigned selector)
3405 struct pre_voltage_change_data data;
3408 data.old_uV = regulator_get_voltage_rdev(rdev);
3411 ret = _notifier_call_chain(rdev, REGULATOR_EVENT_PRE_VOLTAGE_CHANGE,
3413 if (ret & NOTIFY_STOP_MASK)
3416 ret = rdev->desc->ops->set_voltage_sel(rdev, selector);
3420 _notifier_call_chain(rdev, REGULATOR_EVENT_ABORT_VOLTAGE_CHANGE,
3421 (void *)data.old_uV);
3426 static int _regulator_set_voltage_sel_step(struct regulator_dev *rdev,
3427 int uV, int new_selector)
3429 const struct regulator_ops *ops = rdev->desc->ops;
3430 int diff, old_sel, curr_sel, ret;
3432 /* Stepping is only needed if the regulator is enabled. */
3433 if (!_regulator_is_enabled(rdev))
3436 if (!ops->get_voltage_sel)
3439 old_sel = ops->get_voltage_sel(rdev);
3443 diff = new_selector - old_sel;
3445 return 0; /* No change needed. */
3449 for (curr_sel = old_sel + rdev->desc->vsel_step;
3450 curr_sel < new_selector;
3451 curr_sel += rdev->desc->vsel_step) {
3453 * Call the callback directly instead of using
3454 * _regulator_call_set_voltage_sel() as we don't
3455 * want to notify anyone yet. Same in the branch
3458 ret = ops->set_voltage_sel(rdev, curr_sel);
3463 /* Stepping down. */
3464 for (curr_sel = old_sel - rdev->desc->vsel_step;
3465 curr_sel > new_selector;
3466 curr_sel -= rdev->desc->vsel_step) {
3467 ret = ops->set_voltage_sel(rdev, curr_sel);
3474 /* The final selector will trigger the notifiers. */
3475 return _regulator_call_set_voltage_sel(rdev, uV, new_selector);
3479 * At least try to return to the previous voltage if setting a new
3482 (void)ops->set_voltage_sel(rdev, old_sel);
3486 static int _regulator_set_voltage_time(struct regulator_dev *rdev,
3487 int old_uV, int new_uV)
3489 unsigned int ramp_delay = 0;
3491 if (rdev->constraints->ramp_delay)
3492 ramp_delay = rdev->constraints->ramp_delay;
3493 else if (rdev->desc->ramp_delay)
3494 ramp_delay = rdev->desc->ramp_delay;
3495 else if (rdev->constraints->settling_time)
3496 return rdev->constraints->settling_time;
3497 else if (rdev->constraints->settling_time_up &&
3499 return rdev->constraints->settling_time_up;
3500 else if (rdev->constraints->settling_time_down &&
3502 return rdev->constraints->settling_time_down;
3504 if (ramp_delay == 0) {
3505 rdev_dbg(rdev, "ramp_delay not set\n");
3509 return DIV_ROUND_UP(abs(new_uV - old_uV), ramp_delay);
3512 static int _regulator_do_set_voltage(struct regulator_dev *rdev,
3513 int min_uV, int max_uV)
3518 unsigned int selector;
3519 int old_selector = -1;
3520 const struct regulator_ops *ops = rdev->desc->ops;
3521 int old_uV = regulator_get_voltage_rdev(rdev);
3523 trace_regulator_set_voltage(rdev_get_name(rdev), min_uV, max_uV);
3525 min_uV += rdev->constraints->uV_offset;
3526 max_uV += rdev->constraints->uV_offset;
3529 * If we can't obtain the old selector there is not enough
3530 * info to call set_voltage_time_sel().
3532 if (_regulator_is_enabled(rdev) &&
3533 ops->set_voltage_time_sel && ops->get_voltage_sel) {
3534 old_selector = ops->get_voltage_sel(rdev);
3535 if (old_selector < 0)
3536 return old_selector;
3539 if (ops->set_voltage) {
3540 ret = _regulator_call_set_voltage(rdev, min_uV, max_uV,
3544 if (ops->list_voltage)
3545 best_val = ops->list_voltage(rdev,
3548 best_val = regulator_get_voltage_rdev(rdev);
3551 } else if (ops->set_voltage_sel) {
3552 ret = regulator_map_voltage(rdev, min_uV, max_uV);
3554 best_val = ops->list_voltage(rdev, ret);
3555 if (min_uV <= best_val && max_uV >= best_val) {
3557 if (old_selector == selector)
3559 else if (rdev->desc->vsel_step)
3560 ret = _regulator_set_voltage_sel_step(
3561 rdev, best_val, selector);
3563 ret = _regulator_call_set_voltage_sel(
3564 rdev, best_val, selector);
3576 if (ops->set_voltage_time_sel) {
3578 * Call set_voltage_time_sel if successfully obtained
3581 if (old_selector >= 0 && old_selector != selector)
3582 delay = ops->set_voltage_time_sel(rdev, old_selector,
3585 if (old_uV != best_val) {
3586 if (ops->set_voltage_time)
3587 delay = ops->set_voltage_time(rdev, old_uV,
3590 delay = _regulator_set_voltage_time(rdev,
3597 rdev_warn(rdev, "failed to get delay: %pe\n", ERR_PTR(delay));
3601 /* Insert any necessary delays */
3602 _regulator_delay_helper(delay);
3604 if (best_val >= 0) {
3605 unsigned long data = best_val;
3607 _notifier_call_chain(rdev, REGULATOR_EVENT_VOLTAGE_CHANGE,
3612 trace_regulator_set_voltage_complete(rdev_get_name(rdev), best_val);
3617 static int _regulator_do_set_suspend_voltage(struct regulator_dev *rdev,
3618 int min_uV, int max_uV, suspend_state_t state)
3620 struct regulator_state *rstate;
3623 rstate = regulator_get_suspend_state(rdev, state);
3627 if (min_uV < rstate->min_uV)
3628 min_uV = rstate->min_uV;
3629 if (max_uV > rstate->max_uV)
3630 max_uV = rstate->max_uV;
3632 sel = regulator_map_voltage(rdev, min_uV, max_uV);
3636 uV = rdev->desc->ops->list_voltage(rdev, sel);
3637 if (uV >= min_uV && uV <= max_uV)
3643 static int regulator_set_voltage_unlocked(struct regulator *regulator,
3644 int min_uV, int max_uV,
3645 suspend_state_t state)
3647 struct regulator_dev *rdev = regulator->rdev;
3648 struct regulator_voltage *voltage = ®ulator->voltage[state];
3650 int old_min_uV, old_max_uV;
3653 /* If we're setting the same range as last time the change
3654 * should be a noop (some cpufreq implementations use the same
3655 * voltage for multiple frequencies, for example).
3657 if (voltage->min_uV == min_uV && voltage->max_uV == max_uV)
3660 /* If we're trying to set a range that overlaps the current voltage,
3661 * return successfully even though the regulator does not support
3662 * changing the voltage.
3664 if (!regulator_ops_is_valid(rdev, REGULATOR_CHANGE_VOLTAGE)) {
3665 current_uV = regulator_get_voltage_rdev(rdev);
3666 if (min_uV <= current_uV && current_uV <= max_uV) {
3667 voltage->min_uV = min_uV;
3668 voltage->max_uV = max_uV;
3674 if (!rdev->desc->ops->set_voltage &&
3675 !rdev->desc->ops->set_voltage_sel) {
3680 /* constraints check */
3681 ret = regulator_check_voltage(rdev, &min_uV, &max_uV);
3685 /* restore original values in case of error */
3686 old_min_uV = voltage->min_uV;
3687 old_max_uV = voltage->max_uV;
3688 voltage->min_uV = min_uV;
3689 voltage->max_uV = max_uV;
3691 /* for not coupled regulators this will just set the voltage */
3692 ret = regulator_balance_voltage(rdev, state);
3694 voltage->min_uV = old_min_uV;
3695 voltage->max_uV = old_max_uV;
3702 int regulator_set_voltage_rdev(struct regulator_dev *rdev, int min_uV,
3703 int max_uV, suspend_state_t state)
3705 int best_supply_uV = 0;
3706 int supply_change_uV = 0;
3710 regulator_ops_is_valid(rdev->supply->rdev,
3711 REGULATOR_CHANGE_VOLTAGE) &&
3712 (rdev->desc->min_dropout_uV || !(rdev->desc->ops->get_voltage ||
3713 rdev->desc->ops->get_voltage_sel))) {
3714 int current_supply_uV;
3717 selector = regulator_map_voltage(rdev, min_uV, max_uV);
3723 best_supply_uV = _regulator_list_voltage(rdev, selector, 0);
3724 if (best_supply_uV < 0) {
3725 ret = best_supply_uV;
3729 best_supply_uV += rdev->desc->min_dropout_uV;
3731 current_supply_uV = regulator_get_voltage_rdev(rdev->supply->rdev);
3732 if (current_supply_uV < 0) {
3733 ret = current_supply_uV;
3737 supply_change_uV = best_supply_uV - current_supply_uV;
3740 if (supply_change_uV > 0) {
3741 ret = regulator_set_voltage_unlocked(rdev->supply,
3742 best_supply_uV, INT_MAX, state);
3744 dev_err(&rdev->dev, "Failed to increase supply voltage: %pe\n",
3750 if (state == PM_SUSPEND_ON)
3751 ret = _regulator_do_set_voltage(rdev, min_uV, max_uV);
3753 ret = _regulator_do_set_suspend_voltage(rdev, min_uV,
3758 if (supply_change_uV < 0) {
3759 ret = regulator_set_voltage_unlocked(rdev->supply,
3760 best_supply_uV, INT_MAX, state);
3762 dev_warn(&rdev->dev, "Failed to decrease supply voltage: %pe\n",
3764 /* No need to fail here */
3771 EXPORT_SYMBOL_GPL(regulator_set_voltage_rdev);
3773 static int regulator_limit_voltage_step(struct regulator_dev *rdev,
3774 int *current_uV, int *min_uV)
3776 struct regulation_constraints *constraints = rdev->constraints;
3778 /* Limit voltage change only if necessary */
3779 if (!constraints->max_uV_step || !_regulator_is_enabled(rdev))
3782 if (*current_uV < 0) {
3783 *current_uV = regulator_get_voltage_rdev(rdev);
3785 if (*current_uV < 0)
3789 if (abs(*current_uV - *min_uV) <= constraints->max_uV_step)
3792 /* Clamp target voltage within the given step */
3793 if (*current_uV < *min_uV)
3794 *min_uV = min(*current_uV + constraints->max_uV_step,
3797 *min_uV = max(*current_uV - constraints->max_uV_step,
3803 static int regulator_get_optimal_voltage(struct regulator_dev *rdev,
3805 int *min_uV, int *max_uV,
3806 suspend_state_t state,
3809 struct coupling_desc *c_desc = &rdev->coupling_desc;
3810 struct regulator_dev **c_rdevs = c_desc->coupled_rdevs;
3811 struct regulation_constraints *constraints = rdev->constraints;
3812 int desired_min_uV = 0, desired_max_uV = INT_MAX;
3813 int max_current_uV = 0, min_current_uV = INT_MAX;
3814 int highest_min_uV = 0, target_uV, possible_uV;
3815 int i, ret, max_spread;
3821 * If there are no coupled regulators, simply set the voltage
3822 * demanded by consumers.
3824 if (n_coupled == 1) {
3826 * If consumers don't provide any demands, set voltage
3829 desired_min_uV = constraints->min_uV;
3830 desired_max_uV = constraints->max_uV;
3832 ret = regulator_check_consumers(rdev,
3834 &desired_max_uV, state);
3838 possible_uV = desired_min_uV;
3844 /* Find highest min desired voltage */
3845 for (i = 0; i < n_coupled; i++) {
3847 int tmp_max = INT_MAX;
3849 lockdep_assert_held_once(&c_rdevs[i]->mutex.base);
3851 ret = regulator_check_consumers(c_rdevs[i],
3857 ret = regulator_check_voltage(c_rdevs[i], &tmp_min, &tmp_max);
3861 highest_min_uV = max(highest_min_uV, tmp_min);
3864 desired_min_uV = tmp_min;
3865 desired_max_uV = tmp_max;
3869 max_spread = constraints->max_spread[0];
3872 * Let target_uV be equal to the desired one if possible.
3873 * If not, set it to minimum voltage, allowed by other coupled
3876 target_uV = max(desired_min_uV, highest_min_uV - max_spread);
3879 * Find min and max voltages, which currently aren't violating
3882 for (i = 1; i < n_coupled; i++) {
3885 if (!_regulator_is_enabled(c_rdevs[i]))
3888 tmp_act = regulator_get_voltage_rdev(c_rdevs[i]);
3892 min_current_uV = min(tmp_act, min_current_uV);
3893 max_current_uV = max(tmp_act, max_current_uV);
3896 /* There aren't any other regulators enabled */
3897 if (max_current_uV == 0) {
3898 possible_uV = target_uV;
3901 * Correct target voltage, so as it currently isn't
3902 * violating max_spread
3904 possible_uV = max(target_uV, max_current_uV - max_spread);
3905 possible_uV = min(possible_uV, min_current_uV + max_spread);
3908 if (possible_uV > desired_max_uV)
3911 done = (possible_uV == target_uV);
3912 desired_min_uV = possible_uV;
3915 /* Apply max_uV_step constraint if necessary */
3916 if (state == PM_SUSPEND_ON) {
3917 ret = regulator_limit_voltage_step(rdev, current_uV,
3926 /* Set current_uV if wasn't done earlier in the code and if necessary */
3927 if (n_coupled > 1 && *current_uV == -1) {
3929 if (_regulator_is_enabled(rdev)) {
3930 ret = regulator_get_voltage_rdev(rdev);
3936 *current_uV = desired_min_uV;
3940 *min_uV = desired_min_uV;
3941 *max_uV = desired_max_uV;
3946 int regulator_do_balance_voltage(struct regulator_dev *rdev,
3947 suspend_state_t state, bool skip_coupled)
3949 struct regulator_dev **c_rdevs;
3950 struct regulator_dev *best_rdev;
3951 struct coupling_desc *c_desc = &rdev->coupling_desc;
3952 int i, ret, n_coupled, best_min_uV, best_max_uV, best_c_rdev;
3953 unsigned int delta, best_delta;
3954 unsigned long c_rdev_done = 0;
3955 bool best_c_rdev_done;
3957 c_rdevs = c_desc->coupled_rdevs;
3958 n_coupled = skip_coupled ? 1 : c_desc->n_coupled;
3961 * Find the best possible voltage change on each loop. Leave the loop
3962 * if there isn't any possible change.
3965 best_c_rdev_done = false;
3973 * Find highest difference between optimal voltage
3974 * and current voltage.
3976 for (i = 0; i < n_coupled; i++) {
3978 * optimal_uV is the best voltage that can be set for
3979 * i-th regulator at the moment without violating
3980 * max_spread constraint in order to balance
3981 * the coupled voltages.
3983 int optimal_uV = 0, optimal_max_uV = 0, current_uV = 0;
3985 if (test_bit(i, &c_rdev_done))
3988 ret = regulator_get_optimal_voltage(c_rdevs[i],
3996 delta = abs(optimal_uV - current_uV);
3998 if (delta && best_delta <= delta) {
3999 best_c_rdev_done = ret;
4001 best_rdev = c_rdevs[i];
4002 best_min_uV = optimal_uV;
4003 best_max_uV = optimal_max_uV;
4008 /* Nothing to change, return successfully */
4014 ret = regulator_set_voltage_rdev(best_rdev, best_min_uV,
4015 best_max_uV, state);
4020 if (best_c_rdev_done)
4021 set_bit(best_c_rdev, &c_rdev_done);
4023 } while (n_coupled > 1);
4029 static int regulator_balance_voltage(struct regulator_dev *rdev,
4030 suspend_state_t state)
4032 struct coupling_desc *c_desc = &rdev->coupling_desc;
4033 struct regulator_coupler *coupler = c_desc->coupler;
4034 bool skip_coupled = false;
4037 * If system is in a state other than PM_SUSPEND_ON, don't check
4038 * other coupled regulators.
4040 if (state != PM_SUSPEND_ON)
4041 skip_coupled = true;
4043 if (c_desc->n_resolved < c_desc->n_coupled) {
4044 rdev_err(rdev, "Not all coupled regulators registered\n");
4048 /* Invoke custom balancer for customized couplers */
4049 if (coupler && coupler->balance_voltage)
4050 return coupler->balance_voltage(coupler, rdev, state);
4052 return regulator_do_balance_voltage(rdev, state, skip_coupled);
4056 * regulator_set_voltage - set regulator output voltage
4057 * @regulator: regulator source
4058 * @min_uV: Minimum required voltage in uV
4059 * @max_uV: Maximum acceptable voltage in uV
4061 * Sets a voltage regulator to the desired output voltage. This can be set
4062 * during any regulator state. IOW, regulator can be disabled or enabled.
4064 * If the regulator is enabled then the voltage will change to the new value
4065 * immediately otherwise if the regulator is disabled the regulator will
4066 * output at the new voltage when enabled.
4068 * NOTE: If the regulator is shared between several devices then the lowest
4069 * request voltage that meets the system constraints will be used.
4070 * Regulator system constraints must be set for this regulator before
4071 * calling this function otherwise this call will fail.
4073 int regulator_set_voltage(struct regulator *regulator, int min_uV, int max_uV)
4075 struct ww_acquire_ctx ww_ctx;
4078 regulator_lock_dependent(regulator->rdev, &ww_ctx);
4080 ret = regulator_set_voltage_unlocked(regulator, min_uV, max_uV,
4083 regulator_unlock_dependent(regulator->rdev, &ww_ctx);
4087 EXPORT_SYMBOL_GPL(regulator_set_voltage);
4089 static inline int regulator_suspend_toggle(struct regulator_dev *rdev,
4090 suspend_state_t state, bool en)
4092 struct regulator_state *rstate;
4094 rstate = regulator_get_suspend_state(rdev, state);
4098 if (!rstate->changeable)
4101 rstate->enabled = (en) ? ENABLE_IN_SUSPEND : DISABLE_IN_SUSPEND;
4106 int regulator_suspend_enable(struct regulator_dev *rdev,
4107 suspend_state_t state)
4109 return regulator_suspend_toggle(rdev, state, true);
4111 EXPORT_SYMBOL_GPL(regulator_suspend_enable);
4113 int regulator_suspend_disable(struct regulator_dev *rdev,
4114 suspend_state_t state)
4116 struct regulator *regulator;
4117 struct regulator_voltage *voltage;
4120 * if any consumer wants this regulator device keeping on in
4121 * suspend states, don't set it as disabled.
4123 list_for_each_entry(regulator, &rdev->consumer_list, list) {
4124 voltage = ®ulator->voltage[state];
4125 if (voltage->min_uV || voltage->max_uV)
4129 return regulator_suspend_toggle(rdev, state, false);
4131 EXPORT_SYMBOL_GPL(regulator_suspend_disable);
4133 static int _regulator_set_suspend_voltage(struct regulator *regulator,
4134 int min_uV, int max_uV,
4135 suspend_state_t state)
4137 struct regulator_dev *rdev = regulator->rdev;
4138 struct regulator_state *rstate;
4140 rstate = regulator_get_suspend_state(rdev, state);
4144 if (rstate->min_uV == rstate->max_uV) {
4145 rdev_err(rdev, "The suspend voltage can't be changed!\n");
4149 return regulator_set_voltage_unlocked(regulator, min_uV, max_uV, state);
4152 int regulator_set_suspend_voltage(struct regulator *regulator, int min_uV,
4153 int max_uV, suspend_state_t state)
4155 struct ww_acquire_ctx ww_ctx;
4158 /* PM_SUSPEND_ON is handled by regulator_set_voltage() */
4159 if (regulator_check_states(state) || state == PM_SUSPEND_ON)
4162 regulator_lock_dependent(regulator->rdev, &ww_ctx);
4164 ret = _regulator_set_suspend_voltage(regulator, min_uV,
4167 regulator_unlock_dependent(regulator->rdev, &ww_ctx);
4171 EXPORT_SYMBOL_GPL(regulator_set_suspend_voltage);
4174 * regulator_set_voltage_time - get raise/fall time
4175 * @regulator: regulator source
4176 * @old_uV: starting voltage in microvolts
4177 * @new_uV: target voltage in microvolts
4179 * Provided with the starting and ending voltage, this function attempts to
4180 * calculate the time in microseconds required to rise or fall to this new
4183 int regulator_set_voltage_time(struct regulator *regulator,
4184 int old_uV, int new_uV)
4186 struct regulator_dev *rdev = regulator->rdev;
4187 const struct regulator_ops *ops = rdev->desc->ops;
4193 if (ops->set_voltage_time)
4194 return ops->set_voltage_time(rdev, old_uV, new_uV);
4195 else if (!ops->set_voltage_time_sel)
4196 return _regulator_set_voltage_time(rdev, old_uV, new_uV);
4198 /* Currently requires operations to do this */
4199 if (!ops->list_voltage || !rdev->desc->n_voltages)
4202 for (i = 0; i < rdev->desc->n_voltages; i++) {
4203 /* We only look for exact voltage matches here */
4204 if (i < rdev->desc->linear_min_sel)
4207 if (old_sel >= 0 && new_sel >= 0)
4210 voltage = regulator_list_voltage(regulator, i);
4215 if (voltage == old_uV)
4217 if (voltage == new_uV)
4221 if (old_sel < 0 || new_sel < 0)
4224 return ops->set_voltage_time_sel(rdev, old_sel, new_sel);
4226 EXPORT_SYMBOL_GPL(regulator_set_voltage_time);
4229 * regulator_set_voltage_time_sel - get raise/fall time
4230 * @rdev: regulator source device
4231 * @old_selector: selector for starting voltage
4232 * @new_selector: selector for target voltage
4234 * Provided with the starting and target voltage selectors, this function
4235 * returns time in microseconds required to rise or fall to this new voltage
4237 * Drivers providing ramp_delay in regulation_constraints can use this as their
4238 * set_voltage_time_sel() operation.
4240 int regulator_set_voltage_time_sel(struct regulator_dev *rdev,
4241 unsigned int old_selector,
4242 unsigned int new_selector)
4244 int old_volt, new_volt;
4247 if (!rdev->desc->ops->list_voltage)
4250 old_volt = rdev->desc->ops->list_voltage(rdev, old_selector);
4251 new_volt = rdev->desc->ops->list_voltage(rdev, new_selector);
4253 if (rdev->desc->ops->set_voltage_time)
4254 return rdev->desc->ops->set_voltage_time(rdev, old_volt,
4257 return _regulator_set_voltage_time(rdev, old_volt, new_volt);
4259 EXPORT_SYMBOL_GPL(regulator_set_voltage_time_sel);
4261 int regulator_sync_voltage_rdev(struct regulator_dev *rdev)
4265 regulator_lock(rdev);
4267 if (!rdev->desc->ops->set_voltage &&
4268 !rdev->desc->ops->set_voltage_sel) {
4273 /* balance only, if regulator is coupled */
4274 if (rdev->coupling_desc.n_coupled > 1)
4275 ret = regulator_balance_voltage(rdev, PM_SUSPEND_ON);
4280 regulator_unlock(rdev);
4285 * regulator_sync_voltage - re-apply last regulator output voltage
4286 * @regulator: regulator source
4288 * Re-apply the last configured voltage. This is intended to be used
4289 * where some external control source the consumer is cooperating with
4290 * has caused the configured voltage to change.
4292 int regulator_sync_voltage(struct regulator *regulator)
4294 struct regulator_dev *rdev = regulator->rdev;
4295 struct regulator_voltage *voltage = ®ulator->voltage[PM_SUSPEND_ON];
4296 int ret, min_uV, max_uV;
4298 if (!regulator_ops_is_valid(rdev, REGULATOR_CHANGE_VOLTAGE))
4301 regulator_lock(rdev);
4303 if (!rdev->desc->ops->set_voltage &&
4304 !rdev->desc->ops->set_voltage_sel) {
4309 /* This is only going to work if we've had a voltage configured. */
4310 if (!voltage->min_uV && !voltage->max_uV) {
4315 min_uV = voltage->min_uV;
4316 max_uV = voltage->max_uV;
4318 /* This should be a paranoia check... */
4319 ret = regulator_check_voltage(rdev, &min_uV, &max_uV);
4323 ret = regulator_check_consumers(rdev, &min_uV, &max_uV, 0);
4327 /* balance only, if regulator is coupled */
4328 if (rdev->coupling_desc.n_coupled > 1)
4329 ret = regulator_balance_voltage(rdev, PM_SUSPEND_ON);
4331 ret = _regulator_do_set_voltage(rdev, min_uV, max_uV);
4334 regulator_unlock(rdev);
4337 EXPORT_SYMBOL_GPL(regulator_sync_voltage);
4339 int regulator_get_voltage_rdev(struct regulator_dev *rdev)
4344 if (rdev->desc->ops->get_bypass) {
4345 ret = rdev->desc->ops->get_bypass(rdev, &bypassed);
4349 /* if bypassed the regulator must have a supply */
4350 if (!rdev->supply) {
4352 "bypassed regulator has no supply!\n");
4353 return -EPROBE_DEFER;
4356 return regulator_get_voltage_rdev(rdev->supply->rdev);
4360 if (rdev->desc->ops->get_voltage_sel) {
4361 sel = rdev->desc->ops->get_voltage_sel(rdev);
4364 ret = rdev->desc->ops->list_voltage(rdev, sel);
4365 } else if (rdev->desc->ops->get_voltage) {
4366 ret = rdev->desc->ops->get_voltage(rdev);
4367 } else if (rdev->desc->ops->list_voltage) {
4368 ret = rdev->desc->ops->list_voltage(rdev, 0);
4369 } else if (rdev->desc->fixed_uV && (rdev->desc->n_voltages == 1)) {
4370 ret = rdev->desc->fixed_uV;
4371 } else if (rdev->supply) {
4372 ret = regulator_get_voltage_rdev(rdev->supply->rdev);
4373 } else if (rdev->supply_name) {
4374 return -EPROBE_DEFER;
4381 return ret - rdev->constraints->uV_offset;
4383 EXPORT_SYMBOL_GPL(regulator_get_voltage_rdev);
4386 * regulator_get_voltage - get regulator output voltage
4387 * @regulator: regulator source
4389 * This returns the current regulator voltage in uV.
4391 * NOTE: If the regulator is disabled it will return the voltage value. This
4392 * function should not be used to determine regulator state.
4394 int regulator_get_voltage(struct regulator *regulator)
4396 struct ww_acquire_ctx ww_ctx;
4399 regulator_lock_dependent(regulator->rdev, &ww_ctx);
4400 ret = regulator_get_voltage_rdev(regulator->rdev);
4401 regulator_unlock_dependent(regulator->rdev, &ww_ctx);
4405 EXPORT_SYMBOL_GPL(regulator_get_voltage);
4408 * regulator_set_current_limit - set regulator output current limit
4409 * @regulator: regulator source
4410 * @min_uA: Minimum supported current in uA
4411 * @max_uA: Maximum supported current in uA
4413 * Sets current sink to the desired output current. This can be set during
4414 * any regulator state. IOW, regulator can be disabled or enabled.
4416 * If the regulator is enabled then the current will change to the new value
4417 * immediately otherwise if the regulator is disabled the regulator will
4418 * output at the new current when enabled.
4420 * NOTE: Regulator system constraints must be set for this regulator before
4421 * calling this function otherwise this call will fail.
4423 int regulator_set_current_limit(struct regulator *regulator,
4424 int min_uA, int max_uA)
4426 struct regulator_dev *rdev = regulator->rdev;
4429 regulator_lock(rdev);
4432 if (!rdev->desc->ops->set_current_limit) {
4437 /* constraints check */
4438 ret = regulator_check_current_limit(rdev, &min_uA, &max_uA);
4442 ret = rdev->desc->ops->set_current_limit(rdev, min_uA, max_uA);
4444 regulator_unlock(rdev);
4447 EXPORT_SYMBOL_GPL(regulator_set_current_limit);
4449 static int _regulator_get_current_limit_unlocked(struct regulator_dev *rdev)
4452 if (!rdev->desc->ops->get_current_limit)
4455 return rdev->desc->ops->get_current_limit(rdev);
4458 static int _regulator_get_current_limit(struct regulator_dev *rdev)
4462 regulator_lock(rdev);
4463 ret = _regulator_get_current_limit_unlocked(rdev);
4464 regulator_unlock(rdev);
4470 * regulator_get_current_limit - get regulator output current
4471 * @regulator: regulator source
4473 * This returns the current supplied by the specified current sink in uA.
4475 * NOTE: If the regulator is disabled it will return the current value. This
4476 * function should not be used to determine regulator state.
4478 int regulator_get_current_limit(struct regulator *regulator)
4480 return _regulator_get_current_limit(regulator->rdev);
4482 EXPORT_SYMBOL_GPL(regulator_get_current_limit);
4485 * regulator_set_mode - set regulator operating mode
4486 * @regulator: regulator source
4487 * @mode: operating mode - one of the REGULATOR_MODE constants
4489 * Set regulator operating mode to increase regulator efficiency or improve
4490 * regulation performance.
4492 * NOTE: Regulator system constraints must be set for this regulator before
4493 * calling this function otherwise this call will fail.
4495 int regulator_set_mode(struct regulator *regulator, unsigned int mode)
4497 struct regulator_dev *rdev = regulator->rdev;
4499 int regulator_curr_mode;
4501 regulator_lock(rdev);
4504 if (!rdev->desc->ops->set_mode) {
4509 /* return if the same mode is requested */
4510 if (rdev->desc->ops->get_mode) {
4511 regulator_curr_mode = rdev->desc->ops->get_mode(rdev);
4512 if (regulator_curr_mode == mode) {
4518 /* constraints check */
4519 ret = regulator_mode_constrain(rdev, &mode);
4523 ret = rdev->desc->ops->set_mode(rdev, mode);
4525 regulator_unlock(rdev);
4528 EXPORT_SYMBOL_GPL(regulator_set_mode);
4530 static unsigned int _regulator_get_mode_unlocked(struct regulator_dev *rdev)
4533 if (!rdev->desc->ops->get_mode)
4536 return rdev->desc->ops->get_mode(rdev);
4539 static unsigned int _regulator_get_mode(struct regulator_dev *rdev)
4543 regulator_lock(rdev);
4544 ret = _regulator_get_mode_unlocked(rdev);
4545 regulator_unlock(rdev);
4551 * regulator_get_mode - get regulator operating mode
4552 * @regulator: regulator source
4554 * Get the current regulator operating mode.
4556 unsigned int regulator_get_mode(struct regulator *regulator)
4558 return _regulator_get_mode(regulator->rdev);
4560 EXPORT_SYMBOL_GPL(regulator_get_mode);
4562 static int rdev_get_cached_err_flags(struct regulator_dev *rdev)
4566 if (rdev->use_cached_err) {
4567 spin_lock(&rdev->err_lock);
4568 ret = rdev->cached_err;
4569 spin_unlock(&rdev->err_lock);
4574 static int _regulator_get_error_flags(struct regulator_dev *rdev,
4575 unsigned int *flags)
4577 int cached_flags, ret = 0;
4579 regulator_lock(rdev);
4581 cached_flags = rdev_get_cached_err_flags(rdev);
4583 if (rdev->desc->ops->get_error_flags)
4584 ret = rdev->desc->ops->get_error_flags(rdev, flags);
4585 else if (!rdev->use_cached_err)
4588 *flags |= cached_flags;
4590 regulator_unlock(rdev);
4596 * regulator_get_error_flags - get regulator error information
4597 * @regulator: regulator source
4598 * @flags: pointer to store error flags
4600 * Get the current regulator error information.
4602 int regulator_get_error_flags(struct regulator *regulator,
4603 unsigned int *flags)
4605 return _regulator_get_error_flags(regulator->rdev, flags);
4607 EXPORT_SYMBOL_GPL(regulator_get_error_flags);
4610 * regulator_set_load - set regulator load
4611 * @regulator: regulator source
4612 * @uA_load: load current
4614 * Notifies the regulator core of a new device load. This is then used by
4615 * DRMS (if enabled by constraints) to set the most efficient regulator
4616 * operating mode for the new regulator loading.
4618 * Consumer devices notify their supply regulator of the maximum power
4619 * they will require (can be taken from device datasheet in the power
4620 * consumption tables) when they change operational status and hence power
4621 * state. Examples of operational state changes that can affect power
4622 * consumption are :-
4624 * o Device is opened / closed.
4625 * o Device I/O is about to begin or has just finished.
4626 * o Device is idling in between work.
4628 * This information is also exported via sysfs to userspace.
4630 * DRMS will sum the total requested load on the regulator and change
4631 * to the most efficient operating mode if platform constraints allow.
4633 * NOTE: when a regulator consumer requests to have a regulator
4634 * disabled then any load that consumer requested no longer counts
4635 * toward the total requested load. If the regulator is re-enabled
4636 * then the previously requested load will start counting again.
4638 * If a regulator is an always-on regulator then an individual consumer's
4639 * load will still be removed if that consumer is fully disabled.
4641 * On error a negative errno is returned.
4643 int regulator_set_load(struct regulator *regulator, int uA_load)
4645 struct regulator_dev *rdev = regulator->rdev;
4649 regulator_lock(rdev);
4650 old_uA_load = regulator->uA_load;
4651 regulator->uA_load = uA_load;
4652 if (regulator->enable_count && old_uA_load != uA_load) {
4653 ret = drms_uA_update(rdev);
4655 regulator->uA_load = old_uA_load;
4657 regulator_unlock(rdev);
4661 EXPORT_SYMBOL_GPL(regulator_set_load);
4664 * regulator_allow_bypass - allow the regulator to go into bypass mode
4666 * @regulator: Regulator to configure
4667 * @enable: enable or disable bypass mode
4669 * Allow the regulator to go into bypass mode if all other consumers
4670 * for the regulator also enable bypass mode and the machine
4671 * constraints allow this. Bypass mode means that the regulator is
4672 * simply passing the input directly to the output with no regulation.
4674 int regulator_allow_bypass(struct regulator *regulator, bool enable)
4676 struct regulator_dev *rdev = regulator->rdev;
4677 const char *name = rdev_get_name(rdev);
4680 if (!rdev->desc->ops->set_bypass)
4683 if (!regulator_ops_is_valid(rdev, REGULATOR_CHANGE_BYPASS))
4686 regulator_lock(rdev);
4688 if (enable && !regulator->bypass) {
4689 rdev->bypass_count++;
4691 if (rdev->bypass_count == rdev->open_count) {
4692 trace_regulator_bypass_enable(name);
4694 ret = rdev->desc->ops->set_bypass(rdev, enable);
4696 rdev->bypass_count--;
4698 trace_regulator_bypass_enable_complete(name);
4701 } else if (!enable && regulator->bypass) {
4702 rdev->bypass_count--;
4704 if (rdev->bypass_count != rdev->open_count) {
4705 trace_regulator_bypass_disable(name);
4707 ret = rdev->desc->ops->set_bypass(rdev, enable);
4709 rdev->bypass_count++;
4711 trace_regulator_bypass_disable_complete(name);
4716 regulator->bypass = enable;
4718 regulator_unlock(rdev);
4722 EXPORT_SYMBOL_GPL(regulator_allow_bypass);
4725 * regulator_register_notifier - register regulator event notifier
4726 * @regulator: regulator source
4727 * @nb: notifier block
4729 * Register notifier block to receive regulator events.
4731 int regulator_register_notifier(struct regulator *regulator,
4732 struct notifier_block *nb)
4734 return blocking_notifier_chain_register(®ulator->rdev->notifier,
4737 EXPORT_SYMBOL_GPL(regulator_register_notifier);
4740 * regulator_unregister_notifier - unregister regulator event notifier
4741 * @regulator: regulator source
4742 * @nb: notifier block
4744 * Unregister regulator event notifier block.
4746 int regulator_unregister_notifier(struct regulator *regulator,
4747 struct notifier_block *nb)
4749 return blocking_notifier_chain_unregister(®ulator->rdev->notifier,
4752 EXPORT_SYMBOL_GPL(regulator_unregister_notifier);
4754 /* notify regulator consumers and downstream regulator consumers.
4755 * Note mutex must be held by caller.
4757 static int _notifier_call_chain(struct regulator_dev *rdev,
4758 unsigned long event, void *data)
4760 /* call rdev chain first */
4761 return blocking_notifier_call_chain(&rdev->notifier, event, data);
4765 * regulator_bulk_get - get multiple regulator consumers
4767 * @dev: Device to supply
4768 * @num_consumers: Number of consumers to register
4769 * @consumers: Configuration of consumers; clients are stored here.
4771 * @return 0 on success, an errno on failure.
4773 * This helper function allows drivers to get several regulator
4774 * consumers in one operation. If any of the regulators cannot be
4775 * acquired then any regulators that were allocated will be freed
4776 * before returning to the caller.
4778 int regulator_bulk_get(struct device *dev, int num_consumers,
4779 struct regulator_bulk_data *consumers)
4784 for (i = 0; i < num_consumers; i++)
4785 consumers[i].consumer = NULL;
4787 for (i = 0; i < num_consumers; i++) {
4788 consumers[i].consumer = regulator_get(dev,
4789 consumers[i].supply);
4790 if (IS_ERR(consumers[i].consumer)) {
4791 ret = PTR_ERR(consumers[i].consumer);
4792 consumers[i].consumer = NULL;
4800 if (ret != -EPROBE_DEFER)
4801 dev_err(dev, "Failed to get supply '%s': %pe\n",
4802 consumers[i].supply, ERR_PTR(ret));
4804 dev_dbg(dev, "Failed to get supply '%s', deferring\n",
4805 consumers[i].supply);
4808 regulator_put(consumers[i].consumer);
4812 EXPORT_SYMBOL_GPL(regulator_bulk_get);
4814 static void regulator_bulk_enable_async(void *data, async_cookie_t cookie)
4816 struct regulator_bulk_data *bulk = data;
4818 bulk->ret = regulator_enable(bulk->consumer);
4822 * regulator_bulk_enable - enable multiple regulator consumers
4824 * @num_consumers: Number of consumers
4825 * @consumers: Consumer data; clients are stored here.
4826 * @return 0 on success, an errno on failure
4828 * This convenience API allows consumers to enable multiple regulator
4829 * clients in a single API call. If any consumers cannot be enabled
4830 * then any others that were enabled will be disabled again prior to
4833 int regulator_bulk_enable(int num_consumers,
4834 struct regulator_bulk_data *consumers)
4836 ASYNC_DOMAIN_EXCLUSIVE(async_domain);
4840 for (i = 0; i < num_consumers; i++) {
4841 async_schedule_domain(regulator_bulk_enable_async,
4842 &consumers[i], &async_domain);
4845 async_synchronize_full_domain(&async_domain);
4847 /* If any consumer failed we need to unwind any that succeeded */
4848 for (i = 0; i < num_consumers; i++) {
4849 if (consumers[i].ret != 0) {
4850 ret = consumers[i].ret;
4858 for (i = 0; i < num_consumers; i++) {
4859 if (consumers[i].ret < 0)
4860 pr_err("Failed to enable %s: %pe\n", consumers[i].supply,
4861 ERR_PTR(consumers[i].ret));
4863 regulator_disable(consumers[i].consumer);
4868 EXPORT_SYMBOL_GPL(regulator_bulk_enable);
4871 * regulator_bulk_disable - disable multiple regulator consumers
4873 * @num_consumers: Number of consumers
4874 * @consumers: Consumer data; clients are stored here.
4875 * @return 0 on success, an errno on failure
4877 * This convenience API allows consumers to disable multiple regulator
4878 * clients in a single API call. If any consumers cannot be disabled
4879 * then any others that were disabled will be enabled again prior to
4882 int regulator_bulk_disable(int num_consumers,
4883 struct regulator_bulk_data *consumers)
4888 for (i = num_consumers - 1; i >= 0; --i) {
4889 ret = regulator_disable(consumers[i].consumer);
4897 pr_err("Failed to disable %s: %pe\n", consumers[i].supply, ERR_PTR(ret));
4898 for (++i; i < num_consumers; ++i) {
4899 r = regulator_enable(consumers[i].consumer);
4901 pr_err("Failed to re-enable %s: %pe\n",
4902 consumers[i].supply, ERR_PTR(r));
4907 EXPORT_SYMBOL_GPL(regulator_bulk_disable);
4910 * regulator_bulk_force_disable - force disable multiple regulator consumers
4912 * @num_consumers: Number of consumers
4913 * @consumers: Consumer data; clients are stored here.
4914 * @return 0 on success, an errno on failure
4916 * This convenience API allows consumers to forcibly disable multiple regulator
4917 * clients in a single API call.
4918 * NOTE: This should be used for situations when device damage will
4919 * likely occur if the regulators are not disabled (e.g. over temp).
4920 * Although regulator_force_disable function call for some consumers can
4921 * return error numbers, the function is called for all consumers.
4923 int regulator_bulk_force_disable(int num_consumers,
4924 struct regulator_bulk_data *consumers)
4929 for (i = 0; i < num_consumers; i++) {
4931 regulator_force_disable(consumers[i].consumer);
4933 /* Store first error for reporting */
4934 if (consumers[i].ret && !ret)
4935 ret = consumers[i].ret;
4940 EXPORT_SYMBOL_GPL(regulator_bulk_force_disable);
4943 * regulator_bulk_free - free multiple regulator consumers
4945 * @num_consumers: Number of consumers
4946 * @consumers: Consumer data; clients are stored here.
4948 * This convenience API allows consumers to free multiple regulator
4949 * clients in a single API call.
4951 void regulator_bulk_free(int num_consumers,
4952 struct regulator_bulk_data *consumers)
4956 for (i = 0; i < num_consumers; i++) {
4957 regulator_put(consumers[i].consumer);
4958 consumers[i].consumer = NULL;
4961 EXPORT_SYMBOL_GPL(regulator_bulk_free);
4964 * regulator_notifier_call_chain - call regulator event notifier
4965 * @rdev: regulator source
4966 * @event: notifier block
4967 * @data: callback-specific data.
4969 * Called by regulator drivers to notify clients a regulator event has
4972 int regulator_notifier_call_chain(struct regulator_dev *rdev,
4973 unsigned long event, void *data)
4975 _notifier_call_chain(rdev, event, data);
4979 EXPORT_SYMBOL_GPL(regulator_notifier_call_chain);
4982 * regulator_mode_to_status - convert a regulator mode into a status
4984 * @mode: Mode to convert
4986 * Convert a regulator mode into a status.
4988 int regulator_mode_to_status(unsigned int mode)
4991 case REGULATOR_MODE_FAST:
4992 return REGULATOR_STATUS_FAST;
4993 case REGULATOR_MODE_NORMAL:
4994 return REGULATOR_STATUS_NORMAL;
4995 case REGULATOR_MODE_IDLE:
4996 return REGULATOR_STATUS_IDLE;
4997 case REGULATOR_MODE_STANDBY:
4998 return REGULATOR_STATUS_STANDBY;
5000 return REGULATOR_STATUS_UNDEFINED;
5003 EXPORT_SYMBOL_GPL(regulator_mode_to_status);
5005 static struct attribute *regulator_dev_attrs[] = {
5006 &dev_attr_name.attr,
5007 &dev_attr_num_users.attr,
5008 &dev_attr_type.attr,
5009 &dev_attr_microvolts.attr,
5010 &dev_attr_microamps.attr,
5011 &dev_attr_opmode.attr,
5012 &dev_attr_state.attr,
5013 &dev_attr_status.attr,
5014 &dev_attr_bypass.attr,
5015 &dev_attr_requested_microamps.attr,
5016 &dev_attr_min_microvolts.attr,
5017 &dev_attr_max_microvolts.attr,
5018 &dev_attr_min_microamps.attr,
5019 &dev_attr_max_microamps.attr,
5020 &dev_attr_under_voltage.attr,
5021 &dev_attr_over_current.attr,
5022 &dev_attr_regulation_out.attr,
5023 &dev_attr_fail.attr,
5024 &dev_attr_over_temp.attr,
5025 &dev_attr_under_voltage_warn.attr,
5026 &dev_attr_over_current_warn.attr,
5027 &dev_attr_over_voltage_warn.attr,
5028 &dev_attr_over_temp_warn.attr,
5029 &dev_attr_suspend_standby_state.attr,
5030 &dev_attr_suspend_mem_state.attr,
5031 &dev_attr_suspend_disk_state.attr,
5032 &dev_attr_suspend_standby_microvolts.attr,
5033 &dev_attr_suspend_mem_microvolts.attr,
5034 &dev_attr_suspend_disk_microvolts.attr,
5035 &dev_attr_suspend_standby_mode.attr,
5036 &dev_attr_suspend_mem_mode.attr,
5037 &dev_attr_suspend_disk_mode.attr,
5042 * To avoid cluttering sysfs (and memory) with useless state, only
5043 * create attributes that can be meaningfully displayed.
5045 static umode_t regulator_attr_is_visible(struct kobject *kobj,
5046 struct attribute *attr, int idx)
5048 struct device *dev = kobj_to_dev(kobj);
5049 struct regulator_dev *rdev = dev_to_rdev(dev);
5050 const struct regulator_ops *ops = rdev->desc->ops;
5051 umode_t mode = attr->mode;
5053 /* these three are always present */
5054 if (attr == &dev_attr_name.attr ||
5055 attr == &dev_attr_num_users.attr ||
5056 attr == &dev_attr_type.attr)
5059 /* some attributes need specific methods to be displayed */
5060 if (attr == &dev_attr_microvolts.attr) {
5061 if ((ops->get_voltage && ops->get_voltage(rdev) >= 0) ||
5062 (ops->get_voltage_sel && ops->get_voltage_sel(rdev) >= 0) ||
5063 (ops->list_voltage && ops->list_voltage(rdev, 0) >= 0) ||
5064 (rdev->desc->fixed_uV && rdev->desc->n_voltages == 1))
5069 if (attr == &dev_attr_microamps.attr)
5070 return ops->get_current_limit ? mode : 0;
5072 if (attr == &dev_attr_opmode.attr)
5073 return ops->get_mode ? mode : 0;
5075 if (attr == &dev_attr_state.attr)
5076 return (rdev->ena_pin || ops->is_enabled) ? mode : 0;
5078 if (attr == &dev_attr_status.attr)
5079 return ops->get_status ? mode : 0;
5081 if (attr == &dev_attr_bypass.attr)
5082 return ops->get_bypass ? mode : 0;
5084 if (attr == &dev_attr_under_voltage.attr ||
5085 attr == &dev_attr_over_current.attr ||
5086 attr == &dev_attr_regulation_out.attr ||
5087 attr == &dev_attr_fail.attr ||
5088 attr == &dev_attr_over_temp.attr ||
5089 attr == &dev_attr_under_voltage_warn.attr ||
5090 attr == &dev_attr_over_current_warn.attr ||
5091 attr == &dev_attr_over_voltage_warn.attr ||
5092 attr == &dev_attr_over_temp_warn.attr)
5093 return ops->get_error_flags ? mode : 0;
5095 /* constraints need specific supporting methods */
5096 if (attr == &dev_attr_min_microvolts.attr ||
5097 attr == &dev_attr_max_microvolts.attr)
5098 return (ops->set_voltage || ops->set_voltage_sel) ? mode : 0;
5100 if (attr == &dev_attr_min_microamps.attr ||
5101 attr == &dev_attr_max_microamps.attr)
5102 return ops->set_current_limit ? mode : 0;
5104 if (attr == &dev_attr_suspend_standby_state.attr ||
5105 attr == &dev_attr_suspend_mem_state.attr ||
5106 attr == &dev_attr_suspend_disk_state.attr)
5109 if (attr == &dev_attr_suspend_standby_microvolts.attr ||
5110 attr == &dev_attr_suspend_mem_microvolts.attr ||
5111 attr == &dev_attr_suspend_disk_microvolts.attr)
5112 return ops->set_suspend_voltage ? mode : 0;
5114 if (attr == &dev_attr_suspend_standby_mode.attr ||
5115 attr == &dev_attr_suspend_mem_mode.attr ||
5116 attr == &dev_attr_suspend_disk_mode.attr)
5117 return ops->set_suspend_mode ? mode : 0;
5122 static const struct attribute_group regulator_dev_group = {
5123 .attrs = regulator_dev_attrs,
5124 .is_visible = regulator_attr_is_visible,
5127 static const struct attribute_group *regulator_dev_groups[] = {
5128 ®ulator_dev_group,
5132 static void regulator_dev_release(struct device *dev)
5134 struct regulator_dev *rdev = dev_get_drvdata(dev);
5136 kfree(rdev->constraints);
5137 of_node_put(rdev->dev.of_node);
5141 static void rdev_init_debugfs(struct regulator_dev *rdev)
5143 struct device *parent = rdev->dev.parent;
5144 const char *rname = rdev_get_name(rdev);
5145 char name[NAME_MAX];
5147 /* Avoid duplicate debugfs directory names */
5148 if (parent && rname == rdev->desc->name) {
5149 snprintf(name, sizeof(name), "%s-%s", dev_name(parent),
5154 rdev->debugfs = debugfs_create_dir(rname, debugfs_root);
5155 if (!rdev->debugfs) {
5156 rdev_warn(rdev, "Failed to create debugfs directory\n");
5160 debugfs_create_u32("use_count", 0444, rdev->debugfs,
5162 debugfs_create_u32("open_count", 0444, rdev->debugfs,
5164 debugfs_create_u32("bypass_count", 0444, rdev->debugfs,
5165 &rdev->bypass_count);
5168 static int regulator_register_resolve_supply(struct device *dev, void *data)
5170 struct regulator_dev *rdev = dev_to_rdev(dev);
5172 if (regulator_resolve_supply(rdev))
5173 rdev_dbg(rdev, "unable to resolve supply\n");
5178 int regulator_coupler_register(struct regulator_coupler *coupler)
5180 mutex_lock(®ulator_list_mutex);
5181 list_add_tail(&coupler->list, ®ulator_coupler_list);
5182 mutex_unlock(®ulator_list_mutex);
5187 static struct regulator_coupler *
5188 regulator_find_coupler(struct regulator_dev *rdev)
5190 struct regulator_coupler *coupler;
5194 * Note that regulators are appended to the list and the generic
5195 * coupler is registered first, hence it will be attached at last
5198 list_for_each_entry_reverse(coupler, ®ulator_coupler_list, list) {
5199 err = coupler->attach_regulator(coupler, rdev);
5201 if (!coupler->balance_voltage &&
5202 rdev->coupling_desc.n_coupled > 2)
5203 goto err_unsupported;
5209 return ERR_PTR(err);
5217 return ERR_PTR(-EINVAL);
5220 if (coupler->detach_regulator)
5221 coupler->detach_regulator(coupler, rdev);
5224 "Voltage balancing for multiple regulator couples is unimplemented\n");
5226 return ERR_PTR(-EPERM);
5229 static void regulator_resolve_coupling(struct regulator_dev *rdev)
5231 struct regulator_coupler *coupler = rdev->coupling_desc.coupler;
5232 struct coupling_desc *c_desc = &rdev->coupling_desc;
5233 int n_coupled = c_desc->n_coupled;
5234 struct regulator_dev *c_rdev;
5237 for (i = 1; i < n_coupled; i++) {
5238 /* already resolved */
5239 if (c_desc->coupled_rdevs[i])
5242 c_rdev = of_parse_coupled_regulator(rdev, i - 1);
5247 if (c_rdev->coupling_desc.coupler != coupler) {
5248 rdev_err(rdev, "coupler mismatch with %s\n",
5249 rdev_get_name(c_rdev));
5253 c_desc->coupled_rdevs[i] = c_rdev;
5254 c_desc->n_resolved++;
5256 regulator_resolve_coupling(c_rdev);
5260 static void regulator_remove_coupling(struct regulator_dev *rdev)
5262 struct regulator_coupler *coupler = rdev->coupling_desc.coupler;
5263 struct coupling_desc *__c_desc, *c_desc = &rdev->coupling_desc;
5264 struct regulator_dev *__c_rdev, *c_rdev;
5265 unsigned int __n_coupled, n_coupled;
5269 n_coupled = c_desc->n_coupled;
5271 for (i = 1; i < n_coupled; i++) {
5272 c_rdev = c_desc->coupled_rdevs[i];
5277 regulator_lock(c_rdev);
5279 __c_desc = &c_rdev->coupling_desc;
5280 __n_coupled = __c_desc->n_coupled;
5282 for (k = 1; k < __n_coupled; k++) {
5283 __c_rdev = __c_desc->coupled_rdevs[k];
5285 if (__c_rdev == rdev) {
5286 __c_desc->coupled_rdevs[k] = NULL;
5287 __c_desc->n_resolved--;
5292 regulator_unlock(c_rdev);
5294 c_desc->coupled_rdevs[i] = NULL;
5295 c_desc->n_resolved--;
5298 if (coupler && coupler->detach_regulator) {
5299 err = coupler->detach_regulator(coupler, rdev);
5301 rdev_err(rdev, "failed to detach from coupler: %pe\n",
5305 kfree(rdev->coupling_desc.coupled_rdevs);
5306 rdev->coupling_desc.coupled_rdevs = NULL;
5309 static int regulator_init_coupling(struct regulator_dev *rdev)
5311 struct regulator_dev **coupled;
5312 int err, n_phandles;
5314 if (!IS_ENABLED(CONFIG_OF))
5317 n_phandles = of_get_n_coupled(rdev);
5319 coupled = kcalloc(n_phandles + 1, sizeof(*coupled), GFP_KERNEL);
5323 rdev->coupling_desc.coupled_rdevs = coupled;
5326 * Every regulator should always have coupling descriptor filled with
5327 * at least pointer to itself.
5329 rdev->coupling_desc.coupled_rdevs[0] = rdev;
5330 rdev->coupling_desc.n_coupled = n_phandles + 1;
5331 rdev->coupling_desc.n_resolved++;
5333 /* regulator isn't coupled */
5334 if (n_phandles == 0)
5337 if (!of_check_coupling_data(rdev))
5340 mutex_lock(®ulator_list_mutex);
5341 rdev->coupling_desc.coupler = regulator_find_coupler(rdev);
5342 mutex_unlock(®ulator_list_mutex);
5344 if (IS_ERR(rdev->coupling_desc.coupler)) {
5345 err = PTR_ERR(rdev->coupling_desc.coupler);
5346 rdev_err(rdev, "failed to get coupler: %pe\n", ERR_PTR(err));
5353 static int generic_coupler_attach(struct regulator_coupler *coupler,
5354 struct regulator_dev *rdev)
5356 if (rdev->coupling_desc.n_coupled > 2) {
5358 "Voltage balancing for multiple regulator couples is unimplemented\n");
5362 if (!rdev->constraints->always_on) {
5364 "Coupling of a non always-on regulator is unimplemented\n");
5371 static struct regulator_coupler generic_regulator_coupler = {
5372 .attach_regulator = generic_coupler_attach,
5376 * regulator_register - register regulator
5377 * @regulator_desc: regulator to register
5378 * @cfg: runtime configuration for regulator
5380 * Called by regulator drivers to register a regulator.
5381 * Returns a valid pointer to struct regulator_dev on success
5382 * or an ERR_PTR() on error.
5384 struct regulator_dev *
5385 regulator_register(const struct regulator_desc *regulator_desc,
5386 const struct regulator_config *cfg)
5388 const struct regulator_init_data *init_data;
5389 struct regulator_config *config = NULL;
5390 static atomic_t regulator_no = ATOMIC_INIT(-1);
5391 struct regulator_dev *rdev;
5392 bool dangling_cfg_gpiod = false;
5393 bool dangling_of_gpiod = false;
5398 return ERR_PTR(-EINVAL);
5400 dangling_cfg_gpiod = true;
5401 if (regulator_desc == NULL) {
5409 if (regulator_desc->name == NULL || regulator_desc->ops == NULL) {
5414 if (regulator_desc->type != REGULATOR_VOLTAGE &&
5415 regulator_desc->type != REGULATOR_CURRENT) {
5420 /* Only one of each should be implemented */
5421 WARN_ON(regulator_desc->ops->get_voltage &&
5422 regulator_desc->ops->get_voltage_sel);
5423 WARN_ON(regulator_desc->ops->set_voltage &&
5424 regulator_desc->ops->set_voltage_sel);
5426 /* If we're using selectors we must implement list_voltage. */
5427 if (regulator_desc->ops->get_voltage_sel &&
5428 !regulator_desc->ops->list_voltage) {
5432 if (regulator_desc->ops->set_voltage_sel &&
5433 !regulator_desc->ops->list_voltage) {
5438 rdev = kzalloc(sizeof(struct regulator_dev), GFP_KERNEL);
5443 device_initialize(&rdev->dev);
5444 spin_lock_init(&rdev->err_lock);
5447 * Duplicate the config so the driver could override it after
5448 * parsing init data.
5450 config = kmemdup(cfg, sizeof(*cfg), GFP_KERNEL);
5451 if (config == NULL) {
5456 init_data = regulator_of_get_init_data(dev, regulator_desc, config,
5457 &rdev->dev.of_node);
5460 * Sometimes not all resources are probed already so we need to take
5461 * that into account. This happens most the time if the ena_gpiod comes
5462 * from a gpio extender or something else.
5464 if (PTR_ERR(init_data) == -EPROBE_DEFER) {
5465 ret = -EPROBE_DEFER;
5470 * We need to keep track of any GPIO descriptor coming from the
5471 * device tree until we have handled it over to the core. If the
5472 * config that was passed in to this function DOES NOT contain
5473 * a descriptor, and the config after this call DOES contain
5474 * a descriptor, we definitely got one from parsing the device
5477 if (!cfg->ena_gpiod && config->ena_gpiod)
5478 dangling_of_gpiod = true;
5480 init_data = config->init_data;
5481 rdev->dev.of_node = of_node_get(config->of_node);
5484 ww_mutex_init(&rdev->mutex, ®ulator_ww_class);
5485 rdev->reg_data = config->driver_data;
5486 rdev->owner = regulator_desc->owner;
5487 rdev->desc = regulator_desc;
5489 rdev->regmap = config->regmap;
5490 else if (dev_get_regmap(dev, NULL))
5491 rdev->regmap = dev_get_regmap(dev, NULL);
5492 else if (dev->parent)
5493 rdev->regmap = dev_get_regmap(dev->parent, NULL);
5494 INIT_LIST_HEAD(&rdev->consumer_list);
5495 INIT_LIST_HEAD(&rdev->list);
5496 BLOCKING_INIT_NOTIFIER_HEAD(&rdev->notifier);
5497 INIT_DELAYED_WORK(&rdev->disable_work, regulator_disable_work);
5499 /* preform any regulator specific init */
5500 if (init_data && init_data->regulator_init) {
5501 ret = init_data->regulator_init(rdev->reg_data);
5506 if (config->ena_gpiod) {
5507 ret = regulator_ena_gpio_request(rdev, config);
5509 rdev_err(rdev, "Failed to request enable GPIO: %pe\n",
5513 /* The regulator core took over the GPIO descriptor */
5514 dangling_cfg_gpiod = false;
5515 dangling_of_gpiod = false;
5518 /* register with sysfs */
5519 rdev->dev.class = ®ulator_class;
5520 rdev->dev.parent = dev;
5521 dev_set_name(&rdev->dev, "regulator.%lu",
5522 (unsigned long) atomic_inc_return(®ulator_no));
5523 dev_set_drvdata(&rdev->dev, rdev);
5525 /* set regulator constraints */
5527 rdev->constraints = kmemdup(&init_data->constraints,
5528 sizeof(*rdev->constraints),
5531 rdev->constraints = kzalloc(sizeof(*rdev->constraints),
5533 if (!rdev->constraints) {
5538 if (init_data && init_data->supply_regulator)
5539 rdev->supply_name = init_data->supply_regulator;
5540 else if (regulator_desc->supply_name)
5541 rdev->supply_name = regulator_desc->supply_name;
5543 ret = set_machine_constraints(rdev);
5544 if (ret == -EPROBE_DEFER) {
5545 /* Regulator might be in bypass mode and so needs its supply
5546 * to set the constraints
5548 /* FIXME: this currently triggers a chicken-and-egg problem
5549 * when creating -SUPPLY symlink in sysfs to a regulator
5550 * that is just being created
5552 rdev_dbg(rdev, "will resolve supply early: %s\n",
5554 ret = regulator_resolve_supply(rdev);
5556 ret = set_machine_constraints(rdev);
5558 rdev_dbg(rdev, "unable to resolve supply early: %pe\n",
5564 ret = regulator_init_coupling(rdev);
5568 /* add consumers devices */
5570 for (i = 0; i < init_data->num_consumer_supplies; i++) {
5571 ret = set_consumer_device_supply(rdev,
5572 init_data->consumer_supplies[i].dev_name,
5573 init_data->consumer_supplies[i].supply);
5575 dev_err(dev, "Failed to set supply %s\n",
5576 init_data->consumer_supplies[i].supply);
5577 goto unset_supplies;
5582 if (!rdev->desc->ops->get_voltage &&
5583 !rdev->desc->ops->list_voltage &&
5584 !rdev->desc->fixed_uV)
5585 rdev->is_switch = true;
5587 ret = device_add(&rdev->dev);
5589 goto unset_supplies;
5591 rdev_init_debugfs(rdev);
5593 /* try to resolve regulators coupling since a new one was registered */
5594 mutex_lock(®ulator_list_mutex);
5595 regulator_resolve_coupling(rdev);
5596 mutex_unlock(®ulator_list_mutex);
5598 /* try to resolve regulators supply since a new one was registered */
5599 class_for_each_device(®ulator_class, NULL, NULL,
5600 regulator_register_resolve_supply);
5605 mutex_lock(®ulator_list_mutex);
5606 unset_regulator_supplies(rdev);
5607 regulator_remove_coupling(rdev);
5608 mutex_unlock(®ulator_list_mutex);
5610 kfree(rdev->coupling_desc.coupled_rdevs);
5611 mutex_lock(®ulator_list_mutex);
5612 regulator_ena_gpio_free(rdev);
5613 mutex_unlock(®ulator_list_mutex);
5615 if (dangling_of_gpiod)
5616 gpiod_put(config->ena_gpiod);
5618 put_device(&rdev->dev);
5620 if (dangling_cfg_gpiod)
5621 gpiod_put(cfg->ena_gpiod);
5622 return ERR_PTR(ret);
5624 EXPORT_SYMBOL_GPL(regulator_register);
5627 * regulator_unregister - unregister regulator
5628 * @rdev: regulator to unregister
5630 * Called by regulator drivers to unregister a regulator.
5632 void regulator_unregister(struct regulator_dev *rdev)
5638 while (rdev->use_count--)
5639 regulator_disable(rdev->supply);
5640 regulator_put(rdev->supply);
5643 flush_work(&rdev->disable_work.work);
5645 mutex_lock(®ulator_list_mutex);
5647 debugfs_remove_recursive(rdev->debugfs);
5648 WARN_ON(rdev->open_count);
5649 regulator_remove_coupling(rdev);
5650 unset_regulator_supplies(rdev);
5651 list_del(&rdev->list);
5652 regulator_ena_gpio_free(rdev);
5653 device_unregister(&rdev->dev);
5655 mutex_unlock(®ulator_list_mutex);
5657 EXPORT_SYMBOL_GPL(regulator_unregister);
5659 #ifdef CONFIG_SUSPEND
5661 * regulator_suspend - prepare regulators for system wide suspend
5662 * @dev: ``&struct device`` pointer that is passed to _regulator_suspend()
5664 * Configure each regulator with it's suspend operating parameters for state.
5666 static int regulator_suspend(struct device *dev)
5668 struct regulator_dev *rdev = dev_to_rdev(dev);
5669 suspend_state_t state = pm_suspend_target_state;
5671 const struct regulator_state *rstate;
5673 rstate = regulator_get_suspend_state_check(rdev, state);
5677 regulator_lock(rdev);
5678 ret = __suspend_set_state(rdev, rstate);
5679 regulator_unlock(rdev);
5684 static int regulator_resume(struct device *dev)
5686 suspend_state_t state = pm_suspend_target_state;
5687 struct regulator_dev *rdev = dev_to_rdev(dev);
5688 struct regulator_state *rstate;
5691 rstate = regulator_get_suspend_state(rdev, state);
5695 /* Avoid grabbing the lock if we don't need to */
5696 if (!rdev->desc->ops->resume)
5699 regulator_lock(rdev);
5701 if (rstate->enabled == ENABLE_IN_SUSPEND ||
5702 rstate->enabled == DISABLE_IN_SUSPEND)
5703 ret = rdev->desc->ops->resume(rdev);
5705 regulator_unlock(rdev);
5709 #else /* !CONFIG_SUSPEND */
5711 #define regulator_suspend NULL
5712 #define regulator_resume NULL
5714 #endif /* !CONFIG_SUSPEND */
5717 static const struct dev_pm_ops __maybe_unused regulator_pm_ops = {
5718 .suspend = regulator_suspend,
5719 .resume = regulator_resume,
5723 struct class regulator_class = {
5724 .name = "regulator",
5725 .dev_release = regulator_dev_release,
5726 .dev_groups = regulator_dev_groups,
5728 .pm = ®ulator_pm_ops,
5732 * regulator_has_full_constraints - the system has fully specified constraints
5734 * Calling this function will cause the regulator API to disable all
5735 * regulators which have a zero use count and don't have an always_on
5736 * constraint in a late_initcall.
5738 * The intention is that this will become the default behaviour in a
5739 * future kernel release so users are encouraged to use this facility
5742 void regulator_has_full_constraints(void)
5744 has_full_constraints = 1;
5746 EXPORT_SYMBOL_GPL(regulator_has_full_constraints);
5749 * rdev_get_drvdata - get rdev regulator driver data
5752 * Get rdev regulator driver private data. This call can be used in the
5753 * regulator driver context.
5755 void *rdev_get_drvdata(struct regulator_dev *rdev)
5757 return rdev->reg_data;
5759 EXPORT_SYMBOL_GPL(rdev_get_drvdata);
5762 * regulator_get_drvdata - get regulator driver data
5763 * @regulator: regulator
5765 * Get regulator driver private data. This call can be used in the consumer
5766 * driver context when non API regulator specific functions need to be called.
5768 void *regulator_get_drvdata(struct regulator *regulator)
5770 return regulator->rdev->reg_data;
5772 EXPORT_SYMBOL_GPL(regulator_get_drvdata);
5775 * regulator_set_drvdata - set regulator driver data
5776 * @regulator: regulator
5779 void regulator_set_drvdata(struct regulator *regulator, void *data)
5781 regulator->rdev->reg_data = data;
5783 EXPORT_SYMBOL_GPL(regulator_set_drvdata);
5786 * rdev_get_id - get regulator ID
5789 int rdev_get_id(struct regulator_dev *rdev)
5791 return rdev->desc->id;
5793 EXPORT_SYMBOL_GPL(rdev_get_id);
5795 struct device *rdev_get_dev(struct regulator_dev *rdev)
5799 EXPORT_SYMBOL_GPL(rdev_get_dev);
5801 struct regmap *rdev_get_regmap(struct regulator_dev *rdev)
5803 return rdev->regmap;
5805 EXPORT_SYMBOL_GPL(rdev_get_regmap);
5807 void *regulator_get_init_drvdata(struct regulator_init_data *reg_init_data)
5809 return reg_init_data->driver_data;
5811 EXPORT_SYMBOL_GPL(regulator_get_init_drvdata);
5813 #ifdef CONFIG_DEBUG_FS
5814 static int supply_map_show(struct seq_file *sf, void *data)
5816 struct regulator_map *map;
5818 list_for_each_entry(map, ®ulator_map_list, list) {
5819 seq_printf(sf, "%s -> %s.%s\n",
5820 rdev_get_name(map->regulator), map->dev_name,
5826 DEFINE_SHOW_ATTRIBUTE(supply_map);
5828 struct summary_data {
5830 struct regulator_dev *parent;
5834 static void regulator_summary_show_subtree(struct seq_file *s,
5835 struct regulator_dev *rdev,
5838 static int regulator_summary_show_children(struct device *dev, void *data)
5840 struct regulator_dev *rdev = dev_to_rdev(dev);
5841 struct summary_data *summary_data = data;
5843 if (rdev->supply && rdev->supply->rdev == summary_data->parent)
5844 regulator_summary_show_subtree(summary_data->s, rdev,
5845 summary_data->level + 1);
5850 static void regulator_summary_show_subtree(struct seq_file *s,
5851 struct regulator_dev *rdev,
5854 struct regulation_constraints *c;
5855 struct regulator *consumer;
5856 struct summary_data summary_data;
5857 unsigned int opmode;
5862 opmode = _regulator_get_mode_unlocked(rdev);
5863 seq_printf(s, "%*s%-*s %3d %4d %6d %7s ",
5865 30 - level * 3, rdev_get_name(rdev),
5866 rdev->use_count, rdev->open_count, rdev->bypass_count,
5867 regulator_opmode_to_str(opmode));
5869 seq_printf(s, "%5dmV ", regulator_get_voltage_rdev(rdev) / 1000);
5870 seq_printf(s, "%5dmA ",
5871 _regulator_get_current_limit_unlocked(rdev) / 1000);
5873 c = rdev->constraints;
5875 switch (rdev->desc->type) {
5876 case REGULATOR_VOLTAGE:
5877 seq_printf(s, "%5dmV %5dmV ",
5878 c->min_uV / 1000, c->max_uV / 1000);
5880 case REGULATOR_CURRENT:
5881 seq_printf(s, "%5dmA %5dmA ",
5882 c->min_uA / 1000, c->max_uA / 1000);
5889 list_for_each_entry(consumer, &rdev->consumer_list, list) {
5890 if (consumer->dev && consumer->dev->class == ®ulator_class)
5893 seq_printf(s, "%*s%-*s ",
5894 (level + 1) * 3 + 1, "",
5895 30 - (level + 1) * 3,
5896 consumer->supply_name ? consumer->supply_name :
5897 consumer->dev ? dev_name(consumer->dev) : "deviceless");
5899 switch (rdev->desc->type) {
5900 case REGULATOR_VOLTAGE:
5901 seq_printf(s, "%3d %33dmA%c%5dmV %5dmV",
5902 consumer->enable_count,
5903 consumer->uA_load / 1000,
5904 consumer->uA_load && !consumer->enable_count ?
5906 consumer->voltage[PM_SUSPEND_ON].min_uV / 1000,
5907 consumer->voltage[PM_SUSPEND_ON].max_uV / 1000);
5909 case REGULATOR_CURRENT:
5917 summary_data.level = level;
5918 summary_data.parent = rdev;
5920 class_for_each_device(®ulator_class, NULL, &summary_data,
5921 regulator_summary_show_children);
5924 struct summary_lock_data {
5925 struct ww_acquire_ctx *ww_ctx;
5926 struct regulator_dev **new_contended_rdev;
5927 struct regulator_dev **old_contended_rdev;
5930 static int regulator_summary_lock_one(struct device *dev, void *data)
5932 struct regulator_dev *rdev = dev_to_rdev(dev);
5933 struct summary_lock_data *lock_data = data;
5936 if (rdev != *lock_data->old_contended_rdev) {
5937 ret = regulator_lock_nested(rdev, lock_data->ww_ctx);
5939 if (ret == -EDEADLK)
5940 *lock_data->new_contended_rdev = rdev;
5944 *lock_data->old_contended_rdev = NULL;
5950 static int regulator_summary_unlock_one(struct device *dev, void *data)
5952 struct regulator_dev *rdev = dev_to_rdev(dev);
5953 struct summary_lock_data *lock_data = data;
5956 if (rdev == *lock_data->new_contended_rdev)
5960 regulator_unlock(rdev);
5965 static int regulator_summary_lock_all(struct ww_acquire_ctx *ww_ctx,
5966 struct regulator_dev **new_contended_rdev,
5967 struct regulator_dev **old_contended_rdev)
5969 struct summary_lock_data lock_data;
5972 lock_data.ww_ctx = ww_ctx;
5973 lock_data.new_contended_rdev = new_contended_rdev;
5974 lock_data.old_contended_rdev = old_contended_rdev;
5976 ret = class_for_each_device(®ulator_class, NULL, &lock_data,
5977 regulator_summary_lock_one);
5979 class_for_each_device(®ulator_class, NULL, &lock_data,
5980 regulator_summary_unlock_one);
5985 static void regulator_summary_lock(struct ww_acquire_ctx *ww_ctx)
5987 struct regulator_dev *new_contended_rdev = NULL;
5988 struct regulator_dev *old_contended_rdev = NULL;
5991 mutex_lock(®ulator_list_mutex);
5993 ww_acquire_init(ww_ctx, ®ulator_ww_class);
5996 if (new_contended_rdev) {
5997 ww_mutex_lock_slow(&new_contended_rdev->mutex, ww_ctx);
5998 old_contended_rdev = new_contended_rdev;
5999 old_contended_rdev->ref_cnt++;
6002 err = regulator_summary_lock_all(ww_ctx,
6003 &new_contended_rdev,
6004 &old_contended_rdev);
6006 if (old_contended_rdev)
6007 regulator_unlock(old_contended_rdev);
6009 } while (err == -EDEADLK);
6011 ww_acquire_done(ww_ctx);
6014 static void regulator_summary_unlock(struct ww_acquire_ctx *ww_ctx)
6016 class_for_each_device(®ulator_class, NULL, NULL,
6017 regulator_summary_unlock_one);
6018 ww_acquire_fini(ww_ctx);
6020 mutex_unlock(®ulator_list_mutex);
6023 static int regulator_summary_show_roots(struct device *dev, void *data)
6025 struct regulator_dev *rdev = dev_to_rdev(dev);
6026 struct seq_file *s = data;
6029 regulator_summary_show_subtree(s, rdev, 0);
6034 static int regulator_summary_show(struct seq_file *s, void *data)
6036 struct ww_acquire_ctx ww_ctx;
6038 seq_puts(s, " regulator use open bypass opmode voltage current min max\n");
6039 seq_puts(s, "---------------------------------------------------------------------------------------\n");
6041 regulator_summary_lock(&ww_ctx);
6043 class_for_each_device(®ulator_class, NULL, s,
6044 regulator_summary_show_roots);
6046 regulator_summary_unlock(&ww_ctx);
6050 DEFINE_SHOW_ATTRIBUTE(regulator_summary);
6051 #endif /* CONFIG_DEBUG_FS */
6053 static int __init regulator_init(void)
6057 ret = class_register(®ulator_class);
6059 debugfs_root = debugfs_create_dir("regulator", NULL);
6061 pr_warn("regulator: Failed to create debugfs directory\n");
6063 #ifdef CONFIG_DEBUG_FS
6064 debugfs_create_file("supply_map", 0444, debugfs_root, NULL,
6067 debugfs_create_file("regulator_summary", 0444, debugfs_root,
6068 NULL, ®ulator_summary_fops);
6070 regulator_dummy_init();
6072 regulator_coupler_register(&generic_regulator_coupler);
6077 /* init early to allow our consumers to complete system booting */
6078 core_initcall(regulator_init);
6080 static int regulator_late_cleanup(struct device *dev, void *data)
6082 struct regulator_dev *rdev = dev_to_rdev(dev);
6083 struct regulation_constraints *c = rdev->constraints;
6086 if (c && c->always_on)
6089 if (!regulator_ops_is_valid(rdev, REGULATOR_CHANGE_STATUS))
6092 regulator_lock(rdev);
6094 if (rdev->use_count)
6097 /* If reading the status failed, assume that it's off. */
6098 if (_regulator_is_enabled(rdev) <= 0)
6101 if (have_full_constraints()) {
6102 /* We log since this may kill the system if it goes
6105 rdev_info(rdev, "disabling\n");
6106 ret = _regulator_do_disable(rdev);
6108 rdev_err(rdev, "couldn't disable: %pe\n", ERR_PTR(ret));
6110 /* The intention is that in future we will
6111 * assume that full constraints are provided
6112 * so warn even if we aren't going to do
6115 rdev_warn(rdev, "incomplete constraints, leaving on\n");
6119 regulator_unlock(rdev);
6124 static void regulator_init_complete_work_function(struct work_struct *work)
6127 * Regulators may had failed to resolve their input supplies
6128 * when were registered, either because the input supply was
6129 * not registered yet or because its parent device was not
6130 * bound yet. So attempt to resolve the input supplies for
6131 * pending regulators before trying to disable unused ones.
6133 class_for_each_device(®ulator_class, NULL, NULL,
6134 regulator_register_resolve_supply);
6136 /* If we have a full configuration then disable any regulators
6137 * we have permission to change the status for and which are
6138 * not in use or always_on. This is effectively the default
6139 * for DT and ACPI as they have full constraints.
6141 class_for_each_device(®ulator_class, NULL, NULL,
6142 regulator_late_cleanup);
6145 static DECLARE_DELAYED_WORK(regulator_init_complete_work,
6146 regulator_init_complete_work_function);
6148 static int __init regulator_init_complete(void)
6151 * Since DT doesn't provide an idiomatic mechanism for
6152 * enabling full constraints and since it's much more natural
6153 * with DT to provide them just assume that a DT enabled
6154 * system has full constraints.
6156 if (of_have_populated_dt())
6157 has_full_constraints = true;
6160 * We punt completion for an arbitrary amount of time since
6161 * systems like distros will load many drivers from userspace
6162 * so consumers might not always be ready yet, this is
6163 * particularly an issue with laptops where this might bounce
6164 * the display off then on. Ideally we'd get a notification
6165 * from userspace when this happens but we don't so just wait
6166 * a bit and hope we waited long enough. It'd be better if
6167 * we'd only do this on systems that need it, and a kernel
6168 * command line option might be useful.
6170 schedule_delayed_work(®ulator_init_complete_work,
6171 msecs_to_jiffies(30000));
6175 late_initcall_sync(regulator_init_complete);