1 // SPDX-License-Identifier: GPL-2.0-or-later
3 // core.c -- Voltage/Current Regulator framework.
5 // Copyright 2007, 2008 Wolfson Microelectronics PLC.
6 // Copyright 2008 SlimLogic Ltd.
8 // Author: Liam Girdwood <lrg@slimlogic.co.uk>
10 #include <linux/kernel.h>
11 #include <linux/init.h>
12 #include <linux/debugfs.h>
13 #include <linux/device.h>
14 #include <linux/slab.h>
15 #include <linux/async.h>
16 #include <linux/err.h>
17 #include <linux/mutex.h>
18 #include <linux/suspend.h>
19 #include <linux/delay.h>
20 #include <linux/gpio/consumer.h>
22 #include <linux/regmap.h>
23 #include <linux/regulator/of_regulator.h>
24 #include <linux/regulator/consumer.h>
25 #include <linux/regulator/coupler.h>
26 #include <linux/regulator/driver.h>
27 #include <linux/regulator/machine.h>
28 #include <linux/module.h>
30 #define CREATE_TRACE_POINTS
31 #include <trace/events/regulator.h>
36 #define rdev_crit(rdev, fmt, ...) \
37 pr_crit("%s: " fmt, rdev_get_name(rdev), ##__VA_ARGS__)
38 #define rdev_err(rdev, fmt, ...) \
39 pr_err("%s: " fmt, rdev_get_name(rdev), ##__VA_ARGS__)
40 #define rdev_warn(rdev, fmt, ...) \
41 pr_warn("%s: " fmt, rdev_get_name(rdev), ##__VA_ARGS__)
42 #define rdev_info(rdev, fmt, ...) \
43 pr_info("%s: " fmt, rdev_get_name(rdev), ##__VA_ARGS__)
44 #define rdev_dbg(rdev, fmt, ...) \
45 pr_debug("%s: " fmt, rdev_get_name(rdev), ##__VA_ARGS__)
47 static DEFINE_WW_CLASS(regulator_ww_class);
48 static DEFINE_MUTEX(regulator_nesting_mutex);
49 static DEFINE_MUTEX(regulator_list_mutex);
50 static LIST_HEAD(regulator_map_list);
51 static LIST_HEAD(regulator_ena_gpio_list);
52 static LIST_HEAD(regulator_supply_alias_list);
53 static LIST_HEAD(regulator_coupler_list);
54 static bool has_full_constraints;
56 static struct dentry *debugfs_root;
59 * struct regulator_map
61 * Used to provide symbolic supply names to devices.
63 struct regulator_map {
64 struct list_head list;
65 const char *dev_name; /* The dev_name() for the consumer */
67 struct regulator_dev *regulator;
71 * struct regulator_enable_gpio
73 * Management for shared enable GPIO pin
75 struct regulator_enable_gpio {
76 struct list_head list;
77 struct gpio_desc *gpiod;
78 u32 enable_count; /* a number of enabled shared GPIO */
79 u32 request_count; /* a number of requested shared GPIO */
83 * struct regulator_supply_alias
85 * Used to map lookups for a supply onto an alternative device.
87 struct regulator_supply_alias {
88 struct list_head list;
89 struct device *src_dev;
90 const char *src_supply;
91 struct device *alias_dev;
92 const char *alias_supply;
95 static int _regulator_is_enabled(struct regulator_dev *rdev);
96 static int _regulator_disable(struct regulator *regulator);
97 static int _regulator_get_current_limit(struct regulator_dev *rdev);
98 static unsigned int _regulator_get_mode(struct regulator_dev *rdev);
99 static int _notifier_call_chain(struct regulator_dev *rdev,
100 unsigned long event, void *data);
101 static int _regulator_do_set_voltage(struct regulator_dev *rdev,
102 int min_uV, int max_uV);
103 static int regulator_balance_voltage(struct regulator_dev *rdev,
104 suspend_state_t state);
105 static struct regulator *create_regulator(struct regulator_dev *rdev,
107 const char *supply_name);
108 static void destroy_regulator(struct regulator *regulator);
109 static void _regulator_put(struct regulator *regulator);
111 const char *rdev_get_name(struct regulator_dev *rdev)
113 if (rdev->constraints && rdev->constraints->name)
114 return rdev->constraints->name;
115 else if (rdev->desc->name)
116 return rdev->desc->name;
121 static bool have_full_constraints(void)
123 return has_full_constraints || of_have_populated_dt();
126 static bool regulator_ops_is_valid(struct regulator_dev *rdev, int ops)
128 if (!rdev->constraints) {
129 rdev_err(rdev, "no constraints\n");
133 if (rdev->constraints->valid_ops_mask & ops)
140 * regulator_lock_nested - lock a single regulator
141 * @rdev: regulator source
142 * @ww_ctx: w/w mutex acquire context
144 * This function can be called many times by one task on
145 * a single regulator and its mutex will be locked only
146 * once. If a task, which is calling this function is other
147 * than the one, which initially locked the mutex, it will
150 static inline int regulator_lock_nested(struct regulator_dev *rdev,
151 struct ww_acquire_ctx *ww_ctx)
156 mutex_lock(®ulator_nesting_mutex);
158 if (ww_ctx || !ww_mutex_trylock(&rdev->mutex)) {
159 if (rdev->mutex_owner == current)
165 mutex_unlock(®ulator_nesting_mutex);
166 ret = ww_mutex_lock(&rdev->mutex, ww_ctx);
167 mutex_lock(®ulator_nesting_mutex);
173 if (lock && ret != -EDEADLK) {
175 rdev->mutex_owner = current;
178 mutex_unlock(®ulator_nesting_mutex);
184 * regulator_lock - lock a single regulator
185 * @rdev: regulator source
187 * This function can be called many times by one task on
188 * a single regulator and its mutex will be locked only
189 * once. If a task, which is calling this function is other
190 * than the one, which initially locked the mutex, it will
193 static void regulator_lock(struct regulator_dev *rdev)
195 regulator_lock_nested(rdev, NULL);
199 * regulator_unlock - unlock a single regulator
200 * @rdev: regulator_source
202 * This function unlocks the mutex when the
203 * reference counter reaches 0.
205 static void regulator_unlock(struct regulator_dev *rdev)
207 mutex_lock(®ulator_nesting_mutex);
209 if (--rdev->ref_cnt == 0) {
210 rdev->mutex_owner = NULL;
211 ww_mutex_unlock(&rdev->mutex);
214 WARN_ON_ONCE(rdev->ref_cnt < 0);
216 mutex_unlock(®ulator_nesting_mutex);
220 * regulator_lock_two - lock two regulators
221 * @rdev1: first regulator
222 * @rdev2: second regulator
223 * @ww_ctx: w/w mutex acquire context
225 * Locks both rdevs using the regulator_ww_class.
227 static void regulator_lock_two(struct regulator_dev *rdev1,
228 struct regulator_dev *rdev2,
229 struct ww_acquire_ctx *ww_ctx)
231 struct regulator_dev *tmp;
234 ww_acquire_init(ww_ctx, ®ulator_ww_class);
236 /* Try to just grab both of them */
237 ret = regulator_lock_nested(rdev1, ww_ctx);
239 ret = regulator_lock_nested(rdev2, ww_ctx);
240 if (ret != -EDEADLOCK) {
247 * Start of loop: rdev1 was locked and rdev2 was contended.
248 * Need to unlock rdev1, slowly lock rdev2, then try rdev1
251 regulator_unlock(rdev1);
253 ww_mutex_lock_slow(&rdev2->mutex, ww_ctx);
255 rdev2->mutex_owner = current;
256 ret = regulator_lock_nested(rdev1, ww_ctx);
258 if (ret == -EDEADLOCK) {
259 /* More contention; swap which needs to be slow */
270 ww_acquire_done(ww_ctx);
274 * regulator_unlock_two - unlock two regulators
275 * @rdev1: first regulator
276 * @rdev2: second regulator
277 * @ww_ctx: w/w mutex acquire context
279 * The inverse of regulator_lock_two().
282 static void regulator_unlock_two(struct regulator_dev *rdev1,
283 struct regulator_dev *rdev2,
284 struct ww_acquire_ctx *ww_ctx)
286 regulator_unlock(rdev2);
287 regulator_unlock(rdev1);
288 ww_acquire_fini(ww_ctx);
291 static bool regulator_supply_is_couple(struct regulator_dev *rdev)
293 struct regulator_dev *c_rdev;
296 for (i = 1; i < rdev->coupling_desc.n_coupled; i++) {
297 c_rdev = rdev->coupling_desc.coupled_rdevs[i];
299 if (rdev->supply->rdev == c_rdev)
306 static void regulator_unlock_recursive(struct regulator_dev *rdev,
307 unsigned int n_coupled)
309 struct regulator_dev *c_rdev, *supply_rdev;
310 int i, supply_n_coupled;
312 for (i = n_coupled; i > 0; i--) {
313 c_rdev = rdev->coupling_desc.coupled_rdevs[i - 1];
318 if (c_rdev->supply && !regulator_supply_is_couple(c_rdev)) {
319 supply_rdev = c_rdev->supply->rdev;
320 supply_n_coupled = supply_rdev->coupling_desc.n_coupled;
322 regulator_unlock_recursive(supply_rdev,
326 regulator_unlock(c_rdev);
330 static int regulator_lock_recursive(struct regulator_dev *rdev,
331 struct regulator_dev **new_contended_rdev,
332 struct regulator_dev **old_contended_rdev,
333 struct ww_acquire_ctx *ww_ctx)
335 struct regulator_dev *c_rdev;
338 for (i = 0; i < rdev->coupling_desc.n_coupled; i++) {
339 c_rdev = rdev->coupling_desc.coupled_rdevs[i];
344 if (c_rdev != *old_contended_rdev) {
345 err = regulator_lock_nested(c_rdev, ww_ctx);
347 if (err == -EDEADLK) {
348 *new_contended_rdev = c_rdev;
352 /* shouldn't happen */
353 WARN_ON_ONCE(err != -EALREADY);
356 *old_contended_rdev = NULL;
359 if (c_rdev->supply && !regulator_supply_is_couple(c_rdev)) {
360 err = regulator_lock_recursive(c_rdev->supply->rdev,
365 regulator_unlock(c_rdev);
374 regulator_unlock_recursive(rdev, i);
380 * regulator_unlock_dependent - unlock regulator's suppliers and coupled
382 * @rdev: regulator source
383 * @ww_ctx: w/w mutex acquire context
385 * Unlock all regulators related with rdev by coupling or supplying.
387 static void regulator_unlock_dependent(struct regulator_dev *rdev,
388 struct ww_acquire_ctx *ww_ctx)
390 regulator_unlock_recursive(rdev, rdev->coupling_desc.n_coupled);
391 ww_acquire_fini(ww_ctx);
395 * regulator_lock_dependent - lock regulator's suppliers and coupled regulators
396 * @rdev: regulator source
397 * @ww_ctx: w/w mutex acquire context
399 * This function as a wrapper on regulator_lock_recursive(), which locks
400 * all regulators related with rdev by coupling or supplying.
402 static void regulator_lock_dependent(struct regulator_dev *rdev,
403 struct ww_acquire_ctx *ww_ctx)
405 struct regulator_dev *new_contended_rdev = NULL;
406 struct regulator_dev *old_contended_rdev = NULL;
409 mutex_lock(®ulator_list_mutex);
411 ww_acquire_init(ww_ctx, ®ulator_ww_class);
414 if (new_contended_rdev) {
415 ww_mutex_lock_slow(&new_contended_rdev->mutex, ww_ctx);
416 old_contended_rdev = new_contended_rdev;
417 old_contended_rdev->ref_cnt++;
418 old_contended_rdev->mutex_owner = current;
421 err = regulator_lock_recursive(rdev,
426 if (old_contended_rdev)
427 regulator_unlock(old_contended_rdev);
429 } while (err == -EDEADLK);
431 ww_acquire_done(ww_ctx);
433 mutex_unlock(®ulator_list_mutex);
437 * of_get_child_regulator - get a child regulator device node
438 * based on supply name
439 * @parent: Parent device node
440 * @prop_name: Combination regulator supply name and "-supply"
442 * Traverse all child nodes.
443 * Extract the child regulator device node corresponding to the supply name.
444 * returns the device node corresponding to the regulator if found, else
447 static struct device_node *of_get_child_regulator(struct device_node *parent,
448 const char *prop_name)
450 struct device_node *regnode = NULL;
451 struct device_node *child = NULL;
453 for_each_child_of_node(parent, child) {
454 regnode = of_parse_phandle(child, prop_name, 0);
457 regnode = of_get_child_regulator(child, prop_name);
472 * of_get_regulator - get a regulator device node based on supply name
473 * @dev: Device pointer for the consumer (of regulator) device
474 * @supply: regulator supply name
476 * Extract the regulator device node corresponding to the supply name.
477 * returns the device node corresponding to the regulator if found, else
480 static struct device_node *of_get_regulator(struct device *dev, const char *supply)
482 struct device_node *regnode = NULL;
483 char prop_name[64]; /* 64 is max size of property name */
485 dev_dbg(dev, "Looking up %s-supply from device tree\n", supply);
487 snprintf(prop_name, 64, "%s-supply", supply);
488 regnode = of_parse_phandle(dev->of_node, prop_name, 0);
491 regnode = of_get_child_regulator(dev->of_node, prop_name);
495 dev_dbg(dev, "Looking up %s property in node %pOF failed\n",
496 prop_name, dev->of_node);
502 /* Platform voltage constraint check */
503 int regulator_check_voltage(struct regulator_dev *rdev,
504 int *min_uV, int *max_uV)
506 BUG_ON(*min_uV > *max_uV);
508 if (!regulator_ops_is_valid(rdev, REGULATOR_CHANGE_VOLTAGE)) {
509 rdev_err(rdev, "voltage operation not allowed\n");
513 if (*max_uV > rdev->constraints->max_uV)
514 *max_uV = rdev->constraints->max_uV;
515 if (*min_uV < rdev->constraints->min_uV)
516 *min_uV = rdev->constraints->min_uV;
518 if (*min_uV > *max_uV) {
519 rdev_err(rdev, "unsupportable voltage range: %d-%duV\n",
527 /* return 0 if the state is valid */
528 static int regulator_check_states(suspend_state_t state)
530 return (state > PM_SUSPEND_MAX || state == PM_SUSPEND_TO_IDLE);
533 /* Make sure we select a voltage that suits the needs of all
534 * regulator consumers
536 int regulator_check_consumers(struct regulator_dev *rdev,
537 int *min_uV, int *max_uV,
538 suspend_state_t state)
540 struct regulator *regulator;
541 struct regulator_voltage *voltage;
543 list_for_each_entry(regulator, &rdev->consumer_list, list) {
544 voltage = ®ulator->voltage[state];
546 * Assume consumers that didn't say anything are OK
547 * with anything in the constraint range.
549 if (!voltage->min_uV && !voltage->max_uV)
552 if (*max_uV > voltage->max_uV)
553 *max_uV = voltage->max_uV;
554 if (*min_uV < voltage->min_uV)
555 *min_uV = voltage->min_uV;
558 if (*min_uV > *max_uV) {
559 rdev_err(rdev, "Restricting voltage, %u-%uuV\n",
567 /* current constraint check */
568 static int regulator_check_current_limit(struct regulator_dev *rdev,
569 int *min_uA, int *max_uA)
571 BUG_ON(*min_uA > *max_uA);
573 if (!regulator_ops_is_valid(rdev, REGULATOR_CHANGE_CURRENT)) {
574 rdev_err(rdev, "current operation not allowed\n");
578 if (*max_uA > rdev->constraints->max_uA)
579 *max_uA = rdev->constraints->max_uA;
580 if (*min_uA < rdev->constraints->min_uA)
581 *min_uA = rdev->constraints->min_uA;
583 if (*min_uA > *max_uA) {
584 rdev_err(rdev, "unsupportable current range: %d-%duA\n",
592 /* operating mode constraint check */
593 static int regulator_mode_constrain(struct regulator_dev *rdev,
597 case REGULATOR_MODE_FAST:
598 case REGULATOR_MODE_NORMAL:
599 case REGULATOR_MODE_IDLE:
600 case REGULATOR_MODE_STANDBY:
603 rdev_err(rdev, "invalid mode %x specified\n", *mode);
607 if (!regulator_ops_is_valid(rdev, REGULATOR_CHANGE_MODE)) {
608 rdev_err(rdev, "mode operation not allowed\n");
612 /* The modes are bitmasks, the most power hungry modes having
613 * the lowest values. If the requested mode isn't supported
614 * try higher modes. */
616 if (rdev->constraints->valid_modes_mask & *mode)
624 static inline struct regulator_state *
625 regulator_get_suspend_state(struct regulator_dev *rdev, suspend_state_t state)
627 if (rdev->constraints == NULL)
631 case PM_SUSPEND_STANDBY:
632 return &rdev->constraints->state_standby;
634 return &rdev->constraints->state_mem;
636 return &rdev->constraints->state_disk;
642 static const struct regulator_state *
643 regulator_get_suspend_state_check(struct regulator_dev *rdev, suspend_state_t state)
645 const struct regulator_state *rstate;
647 rstate = regulator_get_suspend_state(rdev, state);
651 /* If we have no suspend mode configuration don't set anything;
652 * only warn if the driver implements set_suspend_voltage or
653 * set_suspend_mode callback.
655 if (rstate->enabled != ENABLE_IN_SUSPEND &&
656 rstate->enabled != DISABLE_IN_SUSPEND) {
657 if (rdev->desc->ops->set_suspend_voltage ||
658 rdev->desc->ops->set_suspend_mode)
659 rdev_warn(rdev, "No configuration\n");
666 static ssize_t regulator_uV_show(struct device *dev,
667 struct device_attribute *attr, char *buf)
669 struct regulator_dev *rdev = dev_get_drvdata(dev);
672 regulator_lock(rdev);
673 uV = regulator_get_voltage_rdev(rdev);
674 regulator_unlock(rdev);
678 return sprintf(buf, "%d\n", uV);
680 static DEVICE_ATTR(microvolts, 0444, regulator_uV_show, NULL);
682 static ssize_t regulator_uA_show(struct device *dev,
683 struct device_attribute *attr, char *buf)
685 struct regulator_dev *rdev = dev_get_drvdata(dev);
687 return sprintf(buf, "%d\n", _regulator_get_current_limit(rdev));
689 static DEVICE_ATTR(microamps, 0444, regulator_uA_show, NULL);
691 static ssize_t name_show(struct device *dev, struct device_attribute *attr,
694 struct regulator_dev *rdev = dev_get_drvdata(dev);
696 return sprintf(buf, "%s\n", rdev_get_name(rdev));
698 static DEVICE_ATTR_RO(name);
700 static const char *regulator_opmode_to_str(int mode)
703 case REGULATOR_MODE_FAST:
705 case REGULATOR_MODE_NORMAL:
707 case REGULATOR_MODE_IDLE:
709 case REGULATOR_MODE_STANDBY:
715 static ssize_t regulator_print_opmode(char *buf, int mode)
717 return sprintf(buf, "%s\n", regulator_opmode_to_str(mode));
720 static ssize_t regulator_opmode_show(struct device *dev,
721 struct device_attribute *attr, char *buf)
723 struct regulator_dev *rdev = dev_get_drvdata(dev);
725 return regulator_print_opmode(buf, _regulator_get_mode(rdev));
727 static DEVICE_ATTR(opmode, 0444, regulator_opmode_show, NULL);
729 static ssize_t regulator_print_state(char *buf, int state)
732 return sprintf(buf, "enabled\n");
734 return sprintf(buf, "disabled\n");
736 return sprintf(buf, "unknown\n");
739 static ssize_t regulator_state_show(struct device *dev,
740 struct device_attribute *attr, char *buf)
742 struct regulator_dev *rdev = dev_get_drvdata(dev);
745 regulator_lock(rdev);
746 ret = regulator_print_state(buf, _regulator_is_enabled(rdev));
747 regulator_unlock(rdev);
751 static DEVICE_ATTR(state, 0444, regulator_state_show, NULL);
753 static ssize_t regulator_status_show(struct device *dev,
754 struct device_attribute *attr, char *buf)
756 struct regulator_dev *rdev = dev_get_drvdata(dev);
760 status = rdev->desc->ops->get_status(rdev);
765 case REGULATOR_STATUS_OFF:
768 case REGULATOR_STATUS_ON:
771 case REGULATOR_STATUS_ERROR:
774 case REGULATOR_STATUS_FAST:
777 case REGULATOR_STATUS_NORMAL:
780 case REGULATOR_STATUS_IDLE:
783 case REGULATOR_STATUS_STANDBY:
786 case REGULATOR_STATUS_BYPASS:
789 case REGULATOR_STATUS_UNDEFINED:
796 return sprintf(buf, "%s\n", label);
798 static DEVICE_ATTR(status, 0444, regulator_status_show, NULL);
800 static ssize_t regulator_min_uA_show(struct device *dev,
801 struct device_attribute *attr, char *buf)
803 struct regulator_dev *rdev = dev_get_drvdata(dev);
805 if (!rdev->constraints)
806 return sprintf(buf, "constraint not defined\n");
808 return sprintf(buf, "%d\n", rdev->constraints->min_uA);
810 static DEVICE_ATTR(min_microamps, 0444, regulator_min_uA_show, NULL);
812 static ssize_t regulator_max_uA_show(struct device *dev,
813 struct device_attribute *attr, char *buf)
815 struct regulator_dev *rdev = dev_get_drvdata(dev);
817 if (!rdev->constraints)
818 return sprintf(buf, "constraint not defined\n");
820 return sprintf(buf, "%d\n", rdev->constraints->max_uA);
822 static DEVICE_ATTR(max_microamps, 0444, regulator_max_uA_show, NULL);
824 static ssize_t regulator_min_uV_show(struct device *dev,
825 struct device_attribute *attr, char *buf)
827 struct regulator_dev *rdev = dev_get_drvdata(dev);
829 if (!rdev->constraints)
830 return sprintf(buf, "constraint not defined\n");
832 return sprintf(buf, "%d\n", rdev->constraints->min_uV);
834 static DEVICE_ATTR(min_microvolts, 0444, regulator_min_uV_show, NULL);
836 static ssize_t regulator_max_uV_show(struct device *dev,
837 struct device_attribute *attr, char *buf)
839 struct regulator_dev *rdev = dev_get_drvdata(dev);
841 if (!rdev->constraints)
842 return sprintf(buf, "constraint not defined\n");
844 return sprintf(buf, "%d\n", rdev->constraints->max_uV);
846 static DEVICE_ATTR(max_microvolts, 0444, regulator_max_uV_show, NULL);
848 static ssize_t regulator_total_uA_show(struct device *dev,
849 struct device_attribute *attr, char *buf)
851 struct regulator_dev *rdev = dev_get_drvdata(dev);
852 struct regulator *regulator;
855 regulator_lock(rdev);
856 list_for_each_entry(regulator, &rdev->consumer_list, list) {
857 if (regulator->enable_count)
858 uA += regulator->uA_load;
860 regulator_unlock(rdev);
861 return sprintf(buf, "%d\n", uA);
863 static DEVICE_ATTR(requested_microamps, 0444, regulator_total_uA_show, NULL);
865 static ssize_t num_users_show(struct device *dev, struct device_attribute *attr,
868 struct regulator_dev *rdev = dev_get_drvdata(dev);
869 return sprintf(buf, "%d\n", rdev->use_count);
871 static DEVICE_ATTR_RO(num_users);
873 static ssize_t type_show(struct device *dev, struct device_attribute *attr,
876 struct regulator_dev *rdev = dev_get_drvdata(dev);
878 switch (rdev->desc->type) {
879 case REGULATOR_VOLTAGE:
880 return sprintf(buf, "voltage\n");
881 case REGULATOR_CURRENT:
882 return sprintf(buf, "current\n");
884 return sprintf(buf, "unknown\n");
886 static DEVICE_ATTR_RO(type);
888 static ssize_t regulator_suspend_mem_uV_show(struct device *dev,
889 struct device_attribute *attr, char *buf)
891 struct regulator_dev *rdev = dev_get_drvdata(dev);
893 return sprintf(buf, "%d\n", rdev->constraints->state_mem.uV);
895 static DEVICE_ATTR(suspend_mem_microvolts, 0444,
896 regulator_suspend_mem_uV_show, NULL);
898 static ssize_t regulator_suspend_disk_uV_show(struct device *dev,
899 struct device_attribute *attr, char *buf)
901 struct regulator_dev *rdev = dev_get_drvdata(dev);
903 return sprintf(buf, "%d\n", rdev->constraints->state_disk.uV);
905 static DEVICE_ATTR(suspend_disk_microvolts, 0444,
906 regulator_suspend_disk_uV_show, NULL);
908 static ssize_t regulator_suspend_standby_uV_show(struct device *dev,
909 struct device_attribute *attr, char *buf)
911 struct regulator_dev *rdev = dev_get_drvdata(dev);
913 return sprintf(buf, "%d\n", rdev->constraints->state_standby.uV);
915 static DEVICE_ATTR(suspend_standby_microvolts, 0444,
916 regulator_suspend_standby_uV_show, NULL);
918 static ssize_t regulator_suspend_mem_mode_show(struct device *dev,
919 struct device_attribute *attr, char *buf)
921 struct regulator_dev *rdev = dev_get_drvdata(dev);
923 return regulator_print_opmode(buf,
924 rdev->constraints->state_mem.mode);
926 static DEVICE_ATTR(suspend_mem_mode, 0444,
927 regulator_suspend_mem_mode_show, NULL);
929 static ssize_t regulator_suspend_disk_mode_show(struct device *dev,
930 struct device_attribute *attr, char *buf)
932 struct regulator_dev *rdev = dev_get_drvdata(dev);
934 return regulator_print_opmode(buf,
935 rdev->constraints->state_disk.mode);
937 static DEVICE_ATTR(suspend_disk_mode, 0444,
938 regulator_suspend_disk_mode_show, NULL);
940 static ssize_t regulator_suspend_standby_mode_show(struct device *dev,
941 struct device_attribute *attr, char *buf)
943 struct regulator_dev *rdev = dev_get_drvdata(dev);
945 return regulator_print_opmode(buf,
946 rdev->constraints->state_standby.mode);
948 static DEVICE_ATTR(suspend_standby_mode, 0444,
949 regulator_suspend_standby_mode_show, NULL);
951 static ssize_t regulator_suspend_mem_state_show(struct device *dev,
952 struct device_attribute *attr, char *buf)
954 struct regulator_dev *rdev = dev_get_drvdata(dev);
956 return regulator_print_state(buf,
957 rdev->constraints->state_mem.enabled);
959 static DEVICE_ATTR(suspend_mem_state, 0444,
960 regulator_suspend_mem_state_show, NULL);
962 static ssize_t regulator_suspend_disk_state_show(struct device *dev,
963 struct device_attribute *attr, char *buf)
965 struct regulator_dev *rdev = dev_get_drvdata(dev);
967 return regulator_print_state(buf,
968 rdev->constraints->state_disk.enabled);
970 static DEVICE_ATTR(suspend_disk_state, 0444,
971 regulator_suspend_disk_state_show, NULL);
973 static ssize_t regulator_suspend_standby_state_show(struct device *dev,
974 struct device_attribute *attr, char *buf)
976 struct regulator_dev *rdev = dev_get_drvdata(dev);
978 return regulator_print_state(buf,
979 rdev->constraints->state_standby.enabled);
981 static DEVICE_ATTR(suspend_standby_state, 0444,
982 regulator_suspend_standby_state_show, NULL);
984 static ssize_t regulator_bypass_show(struct device *dev,
985 struct device_attribute *attr, char *buf)
987 struct regulator_dev *rdev = dev_get_drvdata(dev);
992 ret = rdev->desc->ops->get_bypass(rdev, &bypass);
1001 return sprintf(buf, "%s\n", report);
1003 static DEVICE_ATTR(bypass, 0444,
1004 regulator_bypass_show, NULL);
1006 /* Calculate the new optimum regulator operating mode based on the new total
1007 * consumer load. All locks held by caller */
1008 static int drms_uA_update(struct regulator_dev *rdev)
1010 struct regulator *sibling;
1011 int current_uA = 0, output_uV, input_uV, err;
1015 * first check to see if we can set modes at all, otherwise just
1016 * tell the consumer everything is OK.
1018 if (!regulator_ops_is_valid(rdev, REGULATOR_CHANGE_DRMS)) {
1019 rdev_dbg(rdev, "DRMS operation not allowed\n");
1023 if (!rdev->desc->ops->get_optimum_mode &&
1024 !rdev->desc->ops->set_load)
1027 if (!rdev->desc->ops->set_mode &&
1028 !rdev->desc->ops->set_load)
1031 /* calc total requested load */
1032 list_for_each_entry(sibling, &rdev->consumer_list, list) {
1033 if (sibling->enable_count)
1034 current_uA += sibling->uA_load;
1037 current_uA += rdev->constraints->system_load;
1039 if (rdev->desc->ops->set_load) {
1040 /* set the optimum mode for our new total regulator load */
1041 err = rdev->desc->ops->set_load(rdev, current_uA);
1043 rdev_err(rdev, "failed to set load %d: %pe\n",
1044 current_uA, ERR_PTR(err));
1046 /* get output voltage */
1047 output_uV = regulator_get_voltage_rdev(rdev);
1048 if (output_uV <= 0) {
1049 rdev_err(rdev, "invalid output voltage found\n");
1053 /* get input voltage */
1056 input_uV = regulator_get_voltage_rdev(rdev->supply->rdev);
1058 input_uV = rdev->constraints->input_uV;
1059 if (input_uV <= 0) {
1060 rdev_err(rdev, "invalid input voltage found\n");
1064 /* now get the optimum mode for our new total regulator load */
1065 mode = rdev->desc->ops->get_optimum_mode(rdev, input_uV,
1066 output_uV, current_uA);
1068 /* check the new mode is allowed */
1069 err = regulator_mode_constrain(rdev, &mode);
1071 rdev_err(rdev, "failed to get optimum mode @ %d uA %d -> %d uV: %pe\n",
1072 current_uA, input_uV, output_uV, ERR_PTR(err));
1076 err = rdev->desc->ops->set_mode(rdev, mode);
1078 rdev_err(rdev, "failed to set optimum mode %x: %pe\n",
1079 mode, ERR_PTR(err));
1085 static int __suspend_set_state(struct regulator_dev *rdev,
1086 const struct regulator_state *rstate)
1090 if (rstate->enabled == ENABLE_IN_SUSPEND &&
1091 rdev->desc->ops->set_suspend_enable)
1092 ret = rdev->desc->ops->set_suspend_enable(rdev);
1093 else if (rstate->enabled == DISABLE_IN_SUSPEND &&
1094 rdev->desc->ops->set_suspend_disable)
1095 ret = rdev->desc->ops->set_suspend_disable(rdev);
1096 else /* OK if set_suspend_enable or set_suspend_disable is NULL */
1100 rdev_err(rdev, "failed to enabled/disable: %pe\n", ERR_PTR(ret));
1104 if (rdev->desc->ops->set_suspend_voltage && rstate->uV > 0) {
1105 ret = rdev->desc->ops->set_suspend_voltage(rdev, rstate->uV);
1107 rdev_err(rdev, "failed to set voltage: %pe\n", ERR_PTR(ret));
1112 if (rdev->desc->ops->set_suspend_mode && rstate->mode > 0) {
1113 ret = rdev->desc->ops->set_suspend_mode(rdev, rstate->mode);
1115 rdev_err(rdev, "failed to set mode: %pe\n", ERR_PTR(ret));
1123 static int suspend_set_initial_state(struct regulator_dev *rdev)
1125 const struct regulator_state *rstate;
1127 rstate = regulator_get_suspend_state_check(rdev,
1128 rdev->constraints->initial_state);
1132 return __suspend_set_state(rdev, rstate);
1135 #if defined(DEBUG) || defined(CONFIG_DYNAMIC_DEBUG)
1136 static void print_constraints_debug(struct regulator_dev *rdev)
1138 struct regulation_constraints *constraints = rdev->constraints;
1140 size_t len = sizeof(buf) - 1;
1144 if (constraints->min_uV && constraints->max_uV) {
1145 if (constraints->min_uV == constraints->max_uV)
1146 count += scnprintf(buf + count, len - count, "%d mV ",
1147 constraints->min_uV / 1000);
1149 count += scnprintf(buf + count, len - count,
1151 constraints->min_uV / 1000,
1152 constraints->max_uV / 1000);
1155 if (!constraints->min_uV ||
1156 constraints->min_uV != constraints->max_uV) {
1157 ret = regulator_get_voltage_rdev(rdev);
1159 count += scnprintf(buf + count, len - count,
1160 "at %d mV ", ret / 1000);
1163 if (constraints->uV_offset)
1164 count += scnprintf(buf + count, len - count, "%dmV offset ",
1165 constraints->uV_offset / 1000);
1167 if (constraints->min_uA && constraints->max_uA) {
1168 if (constraints->min_uA == constraints->max_uA)
1169 count += scnprintf(buf + count, len - count, "%d mA ",
1170 constraints->min_uA / 1000);
1172 count += scnprintf(buf + count, len - count,
1174 constraints->min_uA / 1000,
1175 constraints->max_uA / 1000);
1178 if (!constraints->min_uA ||
1179 constraints->min_uA != constraints->max_uA) {
1180 ret = _regulator_get_current_limit(rdev);
1182 count += scnprintf(buf + count, len - count,
1183 "at %d mA ", ret / 1000);
1186 if (constraints->valid_modes_mask & REGULATOR_MODE_FAST)
1187 count += scnprintf(buf + count, len - count, "fast ");
1188 if (constraints->valid_modes_mask & REGULATOR_MODE_NORMAL)
1189 count += scnprintf(buf + count, len - count, "normal ");
1190 if (constraints->valid_modes_mask & REGULATOR_MODE_IDLE)
1191 count += scnprintf(buf + count, len - count, "idle ");
1192 if (constraints->valid_modes_mask & REGULATOR_MODE_STANDBY)
1193 count += scnprintf(buf + count, len - count, "standby ");
1196 count = scnprintf(buf, len, "no parameters");
1200 count += scnprintf(buf + count, len - count, ", %s",
1201 _regulator_is_enabled(rdev) ? "enabled" : "disabled");
1203 rdev_dbg(rdev, "%s\n", buf);
1205 #else /* !DEBUG && !CONFIG_DYNAMIC_DEBUG */
1206 static inline void print_constraints_debug(struct regulator_dev *rdev) {}
1207 #endif /* !DEBUG && !CONFIG_DYNAMIC_DEBUG */
1209 static void print_constraints(struct regulator_dev *rdev)
1211 struct regulation_constraints *constraints = rdev->constraints;
1213 print_constraints_debug(rdev);
1215 if ((constraints->min_uV != constraints->max_uV) &&
1216 !regulator_ops_is_valid(rdev, REGULATOR_CHANGE_VOLTAGE))
1218 "Voltage range but no REGULATOR_CHANGE_VOLTAGE\n");
1221 static int machine_constraints_voltage(struct regulator_dev *rdev,
1222 struct regulation_constraints *constraints)
1224 const struct regulator_ops *ops = rdev->desc->ops;
1227 /* do we need to apply the constraint voltage */
1228 if (rdev->constraints->apply_uV &&
1229 rdev->constraints->min_uV && rdev->constraints->max_uV) {
1230 int target_min, target_max;
1231 int current_uV = regulator_get_voltage_rdev(rdev);
1233 if (current_uV == -ENOTRECOVERABLE) {
1234 /* This regulator can't be read and must be initialized */
1235 rdev_info(rdev, "Setting %d-%duV\n",
1236 rdev->constraints->min_uV,
1237 rdev->constraints->max_uV);
1238 _regulator_do_set_voltage(rdev,
1239 rdev->constraints->min_uV,
1240 rdev->constraints->max_uV);
1241 current_uV = regulator_get_voltage_rdev(rdev);
1244 if (current_uV < 0) {
1246 "failed to get the current voltage: %pe\n",
1247 ERR_PTR(current_uV));
1252 * If we're below the minimum voltage move up to the
1253 * minimum voltage, if we're above the maximum voltage
1254 * then move down to the maximum.
1256 target_min = current_uV;
1257 target_max = current_uV;
1259 if (current_uV < rdev->constraints->min_uV) {
1260 target_min = rdev->constraints->min_uV;
1261 target_max = rdev->constraints->min_uV;
1264 if (current_uV > rdev->constraints->max_uV) {
1265 target_min = rdev->constraints->max_uV;
1266 target_max = rdev->constraints->max_uV;
1269 if (target_min != current_uV || target_max != current_uV) {
1270 rdev_info(rdev, "Bringing %duV into %d-%duV\n",
1271 current_uV, target_min, target_max);
1272 ret = _regulator_do_set_voltage(
1273 rdev, target_min, target_max);
1276 "failed to apply %d-%duV constraint: %pe\n",
1277 target_min, target_max, ERR_PTR(ret));
1283 /* constrain machine-level voltage specs to fit
1284 * the actual range supported by this regulator.
1286 if (ops->list_voltage && rdev->desc->n_voltages) {
1287 int count = rdev->desc->n_voltages;
1289 int min_uV = INT_MAX;
1290 int max_uV = INT_MIN;
1291 int cmin = constraints->min_uV;
1292 int cmax = constraints->max_uV;
1294 /* it's safe to autoconfigure fixed-voltage supplies
1295 and the constraints are used by list_voltage. */
1296 if (count == 1 && !cmin) {
1299 constraints->min_uV = cmin;
1300 constraints->max_uV = cmax;
1303 /* voltage constraints are optional */
1304 if ((cmin == 0) && (cmax == 0))
1307 /* else require explicit machine-level constraints */
1308 if (cmin <= 0 || cmax <= 0 || cmax < cmin) {
1309 rdev_err(rdev, "invalid voltage constraints\n");
1313 /* no need to loop voltages if range is continuous */
1314 if (rdev->desc->continuous_voltage_range)
1317 /* initial: [cmin..cmax] valid, [min_uV..max_uV] not */
1318 for (i = 0; i < count; i++) {
1321 value = ops->list_voltage(rdev, i);
1325 /* maybe adjust [min_uV..max_uV] */
1326 if (value >= cmin && value < min_uV)
1328 if (value <= cmax && value > max_uV)
1332 /* final: [min_uV..max_uV] valid iff constraints valid */
1333 if (max_uV < min_uV) {
1335 "unsupportable voltage constraints %u-%uuV\n",
1340 /* use regulator's subset of machine constraints */
1341 if (constraints->min_uV < min_uV) {
1342 rdev_dbg(rdev, "override min_uV, %d -> %d\n",
1343 constraints->min_uV, min_uV);
1344 constraints->min_uV = min_uV;
1346 if (constraints->max_uV > max_uV) {
1347 rdev_dbg(rdev, "override max_uV, %d -> %d\n",
1348 constraints->max_uV, max_uV);
1349 constraints->max_uV = max_uV;
1356 static int machine_constraints_current(struct regulator_dev *rdev,
1357 struct regulation_constraints *constraints)
1359 const struct regulator_ops *ops = rdev->desc->ops;
1362 if (!constraints->min_uA && !constraints->max_uA)
1365 if (constraints->min_uA > constraints->max_uA) {
1366 rdev_err(rdev, "Invalid current constraints\n");
1370 if (!ops->set_current_limit || !ops->get_current_limit) {
1371 rdev_warn(rdev, "Operation of current configuration missing\n");
1375 /* Set regulator current in constraints range */
1376 ret = ops->set_current_limit(rdev, constraints->min_uA,
1377 constraints->max_uA);
1379 rdev_err(rdev, "Failed to set current constraint, %d\n", ret);
1386 static int _regulator_do_enable(struct regulator_dev *rdev);
1389 * set_machine_constraints - sets regulator constraints
1390 * @rdev: regulator source
1392 * Allows platform initialisation code to define and constrain
1393 * regulator circuits e.g. valid voltage/current ranges, etc. NOTE:
1394 * Constraints *must* be set by platform code in order for some
1395 * regulator operations to proceed i.e. set_voltage, set_current_limit,
1398 static int set_machine_constraints(struct regulator_dev *rdev)
1401 const struct regulator_ops *ops = rdev->desc->ops;
1403 ret = machine_constraints_voltage(rdev, rdev->constraints);
1407 ret = machine_constraints_current(rdev, rdev->constraints);
1411 if (rdev->constraints->ilim_uA && ops->set_input_current_limit) {
1412 ret = ops->set_input_current_limit(rdev,
1413 rdev->constraints->ilim_uA);
1415 rdev_err(rdev, "failed to set input limit: %pe\n", ERR_PTR(ret));
1420 /* do we need to setup our suspend state */
1421 if (rdev->constraints->initial_state) {
1422 ret = suspend_set_initial_state(rdev);
1424 rdev_err(rdev, "failed to set suspend state: %pe\n", ERR_PTR(ret));
1429 if (rdev->constraints->initial_mode) {
1430 if (!ops->set_mode) {
1431 rdev_err(rdev, "no set_mode operation\n");
1435 ret = ops->set_mode(rdev, rdev->constraints->initial_mode);
1437 rdev_err(rdev, "failed to set initial mode: %pe\n", ERR_PTR(ret));
1440 } else if (rdev->constraints->system_load) {
1442 * We'll only apply the initial system load if an
1443 * initial mode wasn't specified.
1445 drms_uA_update(rdev);
1448 if ((rdev->constraints->ramp_delay || rdev->constraints->ramp_disable)
1449 && ops->set_ramp_delay) {
1450 ret = ops->set_ramp_delay(rdev, rdev->constraints->ramp_delay);
1452 rdev_err(rdev, "failed to set ramp_delay: %pe\n", ERR_PTR(ret));
1457 if (rdev->constraints->pull_down && ops->set_pull_down) {
1458 ret = ops->set_pull_down(rdev);
1460 rdev_err(rdev, "failed to set pull down: %pe\n", ERR_PTR(ret));
1465 if (rdev->constraints->soft_start && ops->set_soft_start) {
1466 ret = ops->set_soft_start(rdev);
1468 rdev_err(rdev, "failed to set soft start: %pe\n", ERR_PTR(ret));
1473 if (rdev->constraints->over_current_protection
1474 && ops->set_over_current_protection) {
1475 ret = ops->set_over_current_protection(rdev);
1477 rdev_err(rdev, "failed to set over current protection: %pe\n",
1483 if (rdev->constraints->active_discharge && ops->set_active_discharge) {
1484 bool ad_state = (rdev->constraints->active_discharge ==
1485 REGULATOR_ACTIVE_DISCHARGE_ENABLE) ? true : false;
1487 ret = ops->set_active_discharge(rdev, ad_state);
1489 rdev_err(rdev, "failed to set active discharge: %pe\n", ERR_PTR(ret));
1494 /* If the constraints say the regulator should be on at this point
1495 * and we have control then make sure it is enabled.
1497 if (rdev->constraints->always_on || rdev->constraints->boot_on) {
1498 /* If we want to enable this regulator, make sure that we know
1499 * the supplying regulator.
1501 if (rdev->supply_name && !rdev->supply)
1502 return -EPROBE_DEFER;
1504 /* If supplying regulator has already been enabled,
1505 * it's not intended to have use_count increment
1506 * when rdev is only boot-on.
1509 (rdev->constraints->always_on ||
1510 !regulator_is_enabled(rdev->supply))) {
1511 ret = regulator_enable(rdev->supply);
1513 _regulator_put(rdev->supply);
1514 rdev->supply = NULL;
1519 ret = _regulator_do_enable(rdev);
1520 if (ret < 0 && ret != -EINVAL) {
1521 rdev_err(rdev, "failed to enable: %pe\n", ERR_PTR(ret));
1525 if (rdev->constraints->always_on)
1529 print_constraints(rdev);
1534 * set_supply - set regulator supply regulator
1535 * @rdev: regulator (locked)
1536 * @supply_rdev: supply regulator (locked))
1538 * Called by platform initialisation code to set the supply regulator for this
1539 * regulator. This ensures that a regulators supply will also be enabled by the
1540 * core if it's child is enabled.
1542 static int set_supply(struct regulator_dev *rdev,
1543 struct regulator_dev *supply_rdev)
1547 rdev_info(rdev, "supplied by %s\n", rdev_get_name(supply_rdev));
1549 if (!try_module_get(supply_rdev->owner))
1552 rdev->supply = create_regulator(supply_rdev, &rdev->dev, "SUPPLY");
1553 if (rdev->supply == NULL) {
1554 module_put(supply_rdev->owner);
1558 supply_rdev->open_count++;
1564 * set_consumer_device_supply - Bind a regulator to a symbolic supply
1565 * @rdev: regulator source
1566 * @consumer_dev_name: dev_name() string for device supply applies to
1567 * @supply: symbolic name for supply
1569 * Allows platform initialisation code to map physical regulator
1570 * sources to symbolic names for supplies for use by devices. Devices
1571 * should use these symbolic names to request regulators, avoiding the
1572 * need to provide board-specific regulator names as platform data.
1574 static int set_consumer_device_supply(struct regulator_dev *rdev,
1575 const char *consumer_dev_name,
1578 struct regulator_map *node, *new_node;
1584 if (consumer_dev_name != NULL)
1589 new_node = kzalloc(sizeof(struct regulator_map), GFP_KERNEL);
1590 if (new_node == NULL)
1593 new_node->regulator = rdev;
1594 new_node->supply = supply;
1597 new_node->dev_name = kstrdup(consumer_dev_name, GFP_KERNEL);
1598 if (new_node->dev_name == NULL) {
1604 mutex_lock(®ulator_list_mutex);
1605 list_for_each_entry(node, ®ulator_map_list, list) {
1606 if (node->dev_name && consumer_dev_name) {
1607 if (strcmp(node->dev_name, consumer_dev_name) != 0)
1609 } else if (node->dev_name || consumer_dev_name) {
1613 if (strcmp(node->supply, supply) != 0)
1616 pr_debug("%s: %s/%s is '%s' supply; fail %s/%s\n",
1618 dev_name(&node->regulator->dev),
1619 node->regulator->desc->name,
1621 dev_name(&rdev->dev), rdev_get_name(rdev));
1625 list_add(&new_node->list, ®ulator_map_list);
1626 mutex_unlock(®ulator_list_mutex);
1631 mutex_unlock(®ulator_list_mutex);
1632 kfree(new_node->dev_name);
1637 static void unset_regulator_supplies(struct regulator_dev *rdev)
1639 struct regulator_map *node, *n;
1641 list_for_each_entry_safe(node, n, ®ulator_map_list, list) {
1642 if (rdev == node->regulator) {
1643 list_del(&node->list);
1644 kfree(node->dev_name);
1650 #ifdef CONFIG_DEBUG_FS
1651 static ssize_t constraint_flags_read_file(struct file *file,
1652 char __user *user_buf,
1653 size_t count, loff_t *ppos)
1655 const struct regulator *regulator = file->private_data;
1656 const struct regulation_constraints *c = regulator->rdev->constraints;
1663 buf = kmalloc(PAGE_SIZE, GFP_KERNEL);
1667 ret = snprintf(buf, PAGE_SIZE,
1671 "ramp_disable: %u\n"
1674 "over_current_protection: %u\n",
1681 c->over_current_protection);
1683 ret = simple_read_from_buffer(user_buf, count, ppos, buf, ret);
1691 static const struct file_operations constraint_flags_fops = {
1692 #ifdef CONFIG_DEBUG_FS
1693 .open = simple_open,
1694 .read = constraint_flags_read_file,
1695 .llseek = default_llseek,
1699 #define REG_STR_SIZE 64
1701 static struct regulator *create_regulator(struct regulator_dev *rdev,
1703 const char *supply_name)
1705 struct regulator *regulator;
1708 lockdep_assert_held_once(&rdev->mutex.base);
1711 char buf[REG_STR_SIZE];
1714 size = snprintf(buf, REG_STR_SIZE, "%s-%s",
1715 dev->kobj.name, supply_name);
1716 if (size >= REG_STR_SIZE)
1719 supply_name = kstrdup(buf, GFP_KERNEL);
1720 if (supply_name == NULL)
1723 supply_name = kstrdup_const(supply_name, GFP_KERNEL);
1724 if (supply_name == NULL)
1728 regulator = kzalloc(sizeof(*regulator), GFP_KERNEL);
1729 if (regulator == NULL) {
1730 kfree_const(supply_name);
1734 regulator->rdev = rdev;
1735 regulator->supply_name = supply_name;
1737 list_add(®ulator->list, &rdev->consumer_list);
1740 regulator->dev = dev;
1742 /* Add a link to the device sysfs entry */
1743 err = sysfs_create_link_nowarn(&rdev->dev.kobj, &dev->kobj,
1746 rdev_dbg(rdev, "could not add device link %s: %pe\n",
1747 dev->kobj.name, ERR_PTR(err));
1753 regulator->debugfs = debugfs_create_dir(supply_name, rdev->debugfs);
1754 if (IS_ERR(regulator->debugfs))
1755 rdev_dbg(rdev, "Failed to create debugfs directory\n");
1757 debugfs_create_u32("uA_load", 0444, regulator->debugfs,
1758 ®ulator->uA_load);
1759 debugfs_create_u32("min_uV", 0444, regulator->debugfs,
1760 ®ulator->voltage[PM_SUSPEND_ON].min_uV);
1761 debugfs_create_u32("max_uV", 0444, regulator->debugfs,
1762 ®ulator->voltage[PM_SUSPEND_ON].max_uV);
1763 debugfs_create_file("constraint_flags", 0444, regulator->debugfs,
1764 regulator, &constraint_flags_fops);
1767 * Check now if the regulator is an always on regulator - if
1768 * it is then we don't need to do nearly so much work for
1769 * enable/disable calls.
1771 if (!regulator_ops_is_valid(rdev, REGULATOR_CHANGE_STATUS) &&
1772 _regulator_is_enabled(rdev))
1773 regulator->always_on = true;
1778 static int _regulator_get_enable_time(struct regulator_dev *rdev)
1780 if (rdev->constraints && rdev->constraints->enable_time)
1781 return rdev->constraints->enable_time;
1782 if (rdev->desc->ops->enable_time)
1783 return rdev->desc->ops->enable_time(rdev);
1784 return rdev->desc->enable_time;
1787 static struct regulator_supply_alias *regulator_find_supply_alias(
1788 struct device *dev, const char *supply)
1790 struct regulator_supply_alias *map;
1792 list_for_each_entry(map, ®ulator_supply_alias_list, list)
1793 if (map->src_dev == dev && strcmp(map->src_supply, supply) == 0)
1799 static void regulator_supply_alias(struct device **dev, const char **supply)
1801 struct regulator_supply_alias *map;
1803 map = regulator_find_supply_alias(*dev, *supply);
1805 dev_dbg(*dev, "Mapping supply %s to %s,%s\n",
1806 *supply, map->alias_supply,
1807 dev_name(map->alias_dev));
1808 *dev = map->alias_dev;
1809 *supply = map->alias_supply;
1813 static int regulator_match(struct device *dev, const void *data)
1815 struct regulator_dev *r = dev_to_rdev(dev);
1817 return strcmp(rdev_get_name(r), data) == 0;
1820 static struct regulator_dev *regulator_lookup_by_name(const char *name)
1824 dev = class_find_device(®ulator_class, NULL, name, regulator_match);
1826 return dev ? dev_to_rdev(dev) : NULL;
1830 * regulator_dev_lookup - lookup a regulator device.
1831 * @dev: device for regulator "consumer".
1832 * @supply: Supply name or regulator ID.
1834 * If successful, returns a struct regulator_dev that corresponds to the name
1835 * @supply and with the embedded struct device refcount incremented by one.
1836 * The refcount must be dropped by calling put_device().
1837 * On failure one of the following ERR-PTR-encoded values is returned:
1838 * -ENODEV if lookup fails permanently, -EPROBE_DEFER if lookup could succeed
1841 static struct regulator_dev *regulator_dev_lookup(struct device *dev,
1844 struct regulator_dev *r = NULL;
1845 struct device_node *node;
1846 struct regulator_map *map;
1847 const char *devname = NULL;
1849 regulator_supply_alias(&dev, &supply);
1851 /* first do a dt based lookup */
1852 if (dev && dev->of_node) {
1853 node = of_get_regulator(dev, supply);
1855 r = of_find_regulator_by_node(node);
1861 * We have a node, but there is no device.
1862 * assume it has not registered yet.
1864 return ERR_PTR(-EPROBE_DEFER);
1868 /* if not found, try doing it non-dt way */
1870 devname = dev_name(dev);
1872 mutex_lock(®ulator_list_mutex);
1873 list_for_each_entry(map, ®ulator_map_list, list) {
1874 /* If the mapping has a device set up it must match */
1875 if (map->dev_name &&
1876 (!devname || strcmp(map->dev_name, devname)))
1879 if (strcmp(map->supply, supply) == 0 &&
1880 get_device(&map->regulator->dev)) {
1885 mutex_unlock(®ulator_list_mutex);
1890 r = regulator_lookup_by_name(supply);
1894 return ERR_PTR(-ENODEV);
1897 static int regulator_resolve_supply(struct regulator_dev *rdev)
1899 struct regulator_dev *r;
1900 struct device *dev = rdev->dev.parent;
1901 struct ww_acquire_ctx ww_ctx;
1904 /* No supply to resolve? */
1905 if (!rdev->supply_name)
1908 /* Supply already resolved? (fast-path without locking contention) */
1912 r = regulator_dev_lookup(dev, rdev->supply_name);
1916 /* Did the lookup explicitly defer for us? */
1917 if (ret == -EPROBE_DEFER)
1920 if (have_full_constraints()) {
1921 r = dummy_regulator_rdev;
1922 get_device(&r->dev);
1924 dev_err(dev, "Failed to resolve %s-supply for %s\n",
1925 rdev->supply_name, rdev->desc->name);
1926 ret = -EPROBE_DEFER;
1932 dev_err(dev, "Supply for %s (%s) resolved to itself\n",
1933 rdev->desc->name, rdev->supply_name);
1934 if (!have_full_constraints()) {
1938 r = dummy_regulator_rdev;
1939 get_device(&r->dev);
1943 * If the supply's parent device is not the same as the
1944 * regulator's parent device, then ensure the parent device
1945 * is bound before we resolve the supply, in case the parent
1946 * device get probe deferred and unregisters the supply.
1948 if (r->dev.parent && r->dev.parent != rdev->dev.parent) {
1949 if (!device_is_bound(r->dev.parent)) {
1950 put_device(&r->dev);
1951 ret = -EPROBE_DEFER;
1956 /* Recursively resolve the supply of the supply */
1957 ret = regulator_resolve_supply(r);
1959 put_device(&r->dev);
1964 * Recheck rdev->supply with rdev->mutex lock held to avoid a race
1965 * between rdev->supply null check and setting rdev->supply in
1966 * set_supply() from concurrent tasks.
1968 regulator_lock_two(rdev, r, &ww_ctx);
1970 /* Supply just resolved by a concurrent task? */
1972 regulator_unlock_two(rdev, r, &ww_ctx);
1973 put_device(&r->dev);
1977 ret = set_supply(rdev, r);
1979 regulator_unlock_two(rdev, r, &ww_ctx);
1980 put_device(&r->dev);
1984 regulator_unlock_two(rdev, r, &ww_ctx);
1987 * In set_machine_constraints() we may have turned this regulator on
1988 * but we couldn't propagate to the supply if it hadn't been resolved
1991 if (rdev->use_count) {
1992 ret = regulator_enable(rdev->supply);
1994 _regulator_put(rdev->supply);
1995 rdev->supply = NULL;
2004 /* Internal regulator request function */
2005 struct regulator *_regulator_get(struct device *dev, const char *id,
2006 enum regulator_get_type get_type)
2008 struct regulator_dev *rdev;
2009 struct regulator *regulator;
2010 struct device_link *link;
2013 if (get_type >= MAX_GET_TYPE) {
2014 dev_err(dev, "invalid type %d in %s\n", get_type, __func__);
2015 return ERR_PTR(-EINVAL);
2019 pr_err("get() with no identifier\n");
2020 return ERR_PTR(-EINVAL);
2023 rdev = regulator_dev_lookup(dev, id);
2025 ret = PTR_ERR(rdev);
2028 * If regulator_dev_lookup() fails with error other
2029 * than -ENODEV our job here is done, we simply return it.
2032 return ERR_PTR(ret);
2034 if (!have_full_constraints()) {
2036 "incomplete constraints, dummy supplies not allowed\n");
2037 return ERR_PTR(-ENODEV);
2043 * Assume that a regulator is physically present and
2044 * enabled, even if it isn't hooked up, and just
2047 dev_warn(dev, "supply %s not found, using dummy regulator\n", id);
2048 rdev = dummy_regulator_rdev;
2049 get_device(&rdev->dev);
2054 "dummy supplies not allowed for exclusive requests\n");
2058 return ERR_PTR(-ENODEV);
2062 if (rdev->exclusive) {
2063 regulator = ERR_PTR(-EPERM);
2064 put_device(&rdev->dev);
2068 if (get_type == EXCLUSIVE_GET && rdev->open_count) {
2069 regulator = ERR_PTR(-EBUSY);
2070 put_device(&rdev->dev);
2074 mutex_lock(®ulator_list_mutex);
2075 ret = (rdev->coupling_desc.n_resolved != rdev->coupling_desc.n_coupled);
2076 mutex_unlock(®ulator_list_mutex);
2079 regulator = ERR_PTR(-EPROBE_DEFER);
2080 put_device(&rdev->dev);
2084 ret = regulator_resolve_supply(rdev);
2086 regulator = ERR_PTR(ret);
2087 put_device(&rdev->dev);
2091 if (!try_module_get(rdev->owner)) {
2092 regulator = ERR_PTR(-EPROBE_DEFER);
2093 put_device(&rdev->dev);
2097 regulator_lock(rdev);
2098 regulator = create_regulator(rdev, dev, id);
2099 regulator_unlock(rdev);
2100 if (regulator == NULL) {
2101 regulator = ERR_PTR(-ENOMEM);
2102 module_put(rdev->owner);
2103 put_device(&rdev->dev);
2108 if (get_type == EXCLUSIVE_GET) {
2109 rdev->exclusive = 1;
2111 ret = _regulator_is_enabled(rdev);
2113 rdev->use_count = 1;
2114 regulator->enable_count = 1;
2116 rdev->use_count = 0;
2117 regulator->enable_count = 0;
2121 link = device_link_add(dev, &rdev->dev, DL_FLAG_STATELESS);
2122 if (!IS_ERR_OR_NULL(link))
2123 regulator->device_link = true;
2129 * regulator_get - lookup and obtain a reference to a regulator.
2130 * @dev: device for regulator "consumer"
2131 * @id: Supply name or regulator ID.
2133 * Returns a struct regulator corresponding to the regulator producer,
2134 * or IS_ERR() condition containing errno.
2136 * Use of supply names configured via regulator_set_device_supply() is
2137 * strongly encouraged. It is recommended that the supply name used
2138 * should match the name used for the supply and/or the relevant
2139 * device pins in the datasheet.
2141 struct regulator *regulator_get(struct device *dev, const char *id)
2143 return _regulator_get(dev, id, NORMAL_GET);
2145 EXPORT_SYMBOL_GPL(regulator_get);
2148 * regulator_get_exclusive - obtain exclusive access to a regulator.
2149 * @dev: device for regulator "consumer"
2150 * @id: Supply name or regulator ID.
2152 * Returns a struct regulator corresponding to the regulator producer,
2153 * or IS_ERR() condition containing errno. Other consumers will be
2154 * unable to obtain this regulator while this reference is held and the
2155 * use count for the regulator will be initialised to reflect the current
2156 * state of the regulator.
2158 * This is intended for use by consumers which cannot tolerate shared
2159 * use of the regulator such as those which need to force the
2160 * regulator off for correct operation of the hardware they are
2163 * Use of supply names configured via regulator_set_device_supply() is
2164 * strongly encouraged. It is recommended that the supply name used
2165 * should match the name used for the supply and/or the relevant
2166 * device pins in the datasheet.
2168 struct regulator *regulator_get_exclusive(struct device *dev, const char *id)
2170 return _regulator_get(dev, id, EXCLUSIVE_GET);
2172 EXPORT_SYMBOL_GPL(regulator_get_exclusive);
2175 * regulator_get_optional - obtain optional access to a regulator.
2176 * @dev: device for regulator "consumer"
2177 * @id: Supply name or regulator ID.
2179 * Returns a struct regulator corresponding to the regulator producer,
2180 * or IS_ERR() condition containing errno.
2182 * This is intended for use by consumers for devices which can have
2183 * some supplies unconnected in normal use, such as some MMC devices.
2184 * It can allow the regulator core to provide stub supplies for other
2185 * supplies requested using normal regulator_get() calls without
2186 * disrupting the operation of drivers that can handle absent
2189 * Use of supply names configured via regulator_set_device_supply() is
2190 * strongly encouraged. It is recommended that the supply name used
2191 * should match the name used for the supply and/or the relevant
2192 * device pins in the datasheet.
2194 struct regulator *regulator_get_optional(struct device *dev, const char *id)
2196 return _regulator_get(dev, id, OPTIONAL_GET);
2198 EXPORT_SYMBOL_GPL(regulator_get_optional);
2200 static void destroy_regulator(struct regulator *regulator)
2202 struct regulator_dev *rdev = regulator->rdev;
2204 debugfs_remove_recursive(regulator->debugfs);
2206 if (regulator->dev) {
2207 if (regulator->device_link)
2208 device_link_remove(regulator->dev, &rdev->dev);
2210 /* remove any sysfs entries */
2211 sysfs_remove_link(&rdev->dev.kobj, regulator->supply_name);
2214 regulator_lock(rdev);
2215 list_del(®ulator->list);
2218 rdev->exclusive = 0;
2219 regulator_unlock(rdev);
2221 kfree_const(regulator->supply_name);
2225 /* regulator_list_mutex lock held by regulator_put() */
2226 static void _regulator_put(struct regulator *regulator)
2228 struct regulator_dev *rdev;
2230 if (IS_ERR_OR_NULL(regulator))
2233 lockdep_assert_held_once(®ulator_list_mutex);
2235 /* Docs say you must disable before calling regulator_put() */
2236 WARN_ON(regulator->enable_count);
2238 rdev = regulator->rdev;
2240 destroy_regulator(regulator);
2242 module_put(rdev->owner);
2243 put_device(&rdev->dev);
2247 * regulator_put - "free" the regulator source
2248 * @regulator: regulator source
2250 * Note: drivers must ensure that all regulator_enable calls made on this
2251 * regulator source are balanced by regulator_disable calls prior to calling
2254 void regulator_put(struct regulator *regulator)
2256 mutex_lock(®ulator_list_mutex);
2257 _regulator_put(regulator);
2258 mutex_unlock(®ulator_list_mutex);
2260 EXPORT_SYMBOL_GPL(regulator_put);
2263 * regulator_register_supply_alias - Provide device alias for supply lookup
2265 * @dev: device that will be given as the regulator "consumer"
2266 * @id: Supply name or regulator ID
2267 * @alias_dev: device that should be used to lookup the supply
2268 * @alias_id: Supply name or regulator ID that should be used to lookup the
2271 * All lookups for id on dev will instead be conducted for alias_id on
2274 int regulator_register_supply_alias(struct device *dev, const char *id,
2275 struct device *alias_dev,
2276 const char *alias_id)
2278 struct regulator_supply_alias *map;
2280 map = regulator_find_supply_alias(dev, id);
2284 map = kzalloc(sizeof(struct regulator_supply_alias), GFP_KERNEL);
2289 map->src_supply = id;
2290 map->alias_dev = alias_dev;
2291 map->alias_supply = alias_id;
2293 list_add(&map->list, ®ulator_supply_alias_list);
2295 pr_info("Adding alias for supply %s,%s -> %s,%s\n",
2296 id, dev_name(dev), alias_id, dev_name(alias_dev));
2300 EXPORT_SYMBOL_GPL(regulator_register_supply_alias);
2303 * regulator_unregister_supply_alias - Remove device alias
2305 * @dev: device that will be given as the regulator "consumer"
2306 * @id: Supply name or regulator ID
2308 * Remove a lookup alias if one exists for id on dev.
2310 void regulator_unregister_supply_alias(struct device *dev, const char *id)
2312 struct regulator_supply_alias *map;
2314 map = regulator_find_supply_alias(dev, id);
2316 list_del(&map->list);
2320 EXPORT_SYMBOL_GPL(regulator_unregister_supply_alias);
2323 * regulator_bulk_register_supply_alias - register multiple aliases
2325 * @dev: device that will be given as the regulator "consumer"
2326 * @id: List of supply names or regulator IDs
2327 * @alias_dev: device that should be used to lookup the supply
2328 * @alias_id: List of supply names or regulator IDs that should be used to
2330 * @num_id: Number of aliases to register
2332 * @return 0 on success, an errno on failure.
2334 * This helper function allows drivers to register several supply
2335 * aliases in one operation. If any of the aliases cannot be
2336 * registered any aliases that were registered will be removed
2337 * before returning to the caller.
2339 int regulator_bulk_register_supply_alias(struct device *dev,
2340 const char *const *id,
2341 struct device *alias_dev,
2342 const char *const *alias_id,
2348 for (i = 0; i < num_id; ++i) {
2349 ret = regulator_register_supply_alias(dev, id[i], alias_dev,
2359 "Failed to create supply alias %s,%s -> %s,%s\n",
2360 id[i], dev_name(dev), alias_id[i], dev_name(alias_dev));
2363 regulator_unregister_supply_alias(dev, id[i]);
2367 EXPORT_SYMBOL_GPL(regulator_bulk_register_supply_alias);
2370 * regulator_bulk_unregister_supply_alias - unregister multiple aliases
2372 * @dev: device that will be given as the regulator "consumer"
2373 * @id: List of supply names or regulator IDs
2374 * @num_id: Number of aliases to unregister
2376 * This helper function allows drivers to unregister several supply
2377 * aliases in one operation.
2379 void regulator_bulk_unregister_supply_alias(struct device *dev,
2380 const char *const *id,
2385 for (i = 0; i < num_id; ++i)
2386 regulator_unregister_supply_alias(dev, id[i]);
2388 EXPORT_SYMBOL_GPL(regulator_bulk_unregister_supply_alias);
2391 /* Manage enable GPIO list. Same GPIO pin can be shared among regulators */
2392 static int regulator_ena_gpio_request(struct regulator_dev *rdev,
2393 const struct regulator_config *config)
2395 struct regulator_enable_gpio *pin, *new_pin;
2396 struct gpio_desc *gpiod;
2398 gpiod = config->ena_gpiod;
2399 new_pin = kzalloc(sizeof(*new_pin), GFP_KERNEL);
2401 mutex_lock(®ulator_list_mutex);
2403 list_for_each_entry(pin, ®ulator_ena_gpio_list, list) {
2404 if (pin->gpiod == gpiod) {
2405 rdev_dbg(rdev, "GPIO is already used\n");
2406 goto update_ena_gpio_to_rdev;
2410 if (new_pin == NULL) {
2411 mutex_unlock(®ulator_list_mutex);
2419 list_add(&pin->list, ®ulator_ena_gpio_list);
2421 update_ena_gpio_to_rdev:
2422 pin->request_count++;
2423 rdev->ena_pin = pin;
2425 mutex_unlock(®ulator_list_mutex);
2431 static void regulator_ena_gpio_free(struct regulator_dev *rdev)
2433 struct regulator_enable_gpio *pin, *n;
2438 /* Free the GPIO only in case of no use */
2439 list_for_each_entry_safe(pin, n, ®ulator_ena_gpio_list, list) {
2440 if (pin != rdev->ena_pin)
2443 if (--pin->request_count)
2446 gpiod_put(pin->gpiod);
2447 list_del(&pin->list);
2452 rdev->ena_pin = NULL;
2456 * regulator_ena_gpio_ctrl - balance enable_count of each GPIO and actual GPIO pin control
2457 * @rdev: regulator_dev structure
2458 * @enable: enable GPIO at initial use?
2460 * GPIO is enabled in case of initial use. (enable_count is 0)
2461 * GPIO is disabled when it is not shared any more. (enable_count <= 1)
2463 static int regulator_ena_gpio_ctrl(struct regulator_dev *rdev, bool enable)
2465 struct regulator_enable_gpio *pin = rdev->ena_pin;
2471 /* Enable GPIO at initial use */
2472 if (pin->enable_count == 0)
2473 gpiod_set_value_cansleep(pin->gpiod, 1);
2475 pin->enable_count++;
2477 if (pin->enable_count > 1) {
2478 pin->enable_count--;
2482 /* Disable GPIO if not used */
2483 if (pin->enable_count <= 1) {
2484 gpiod_set_value_cansleep(pin->gpiod, 0);
2485 pin->enable_count = 0;
2493 * _regulator_enable_delay - a delay helper function
2494 * @delay: time to delay in microseconds
2496 * Delay for the requested amount of time as per the guidelines in:
2498 * Documentation/timers/timers-howto.rst
2500 * The assumption here is that regulators will never be enabled in
2501 * atomic context and therefore sleeping functions can be used.
2503 static void _regulator_enable_delay(unsigned int delay)
2505 unsigned int ms = delay / 1000;
2506 unsigned int us = delay % 1000;
2510 * For small enough values, handle super-millisecond
2511 * delays in the usleep_range() call below.
2520 * Give the scheduler some room to coalesce with any other
2521 * wakeup sources. For delays shorter than 10 us, don't even
2522 * bother setting up high-resolution timers and just busy-
2526 usleep_range(us, us + 100);
2532 * _regulator_check_status_enabled
2534 * A helper function to check if the regulator status can be interpreted
2535 * as 'regulator is enabled'.
2536 * @rdev: the regulator device to check
2539 * * 1 - if status shows regulator is in enabled state
2540 * * 0 - if not enabled state
2541 * * Error Value - as received from ops->get_status()
2543 static inline int _regulator_check_status_enabled(struct regulator_dev *rdev)
2545 int ret = rdev->desc->ops->get_status(rdev);
2548 rdev_info(rdev, "get_status returned error: %d\n", ret);
2553 case REGULATOR_STATUS_OFF:
2554 case REGULATOR_STATUS_ERROR:
2555 case REGULATOR_STATUS_UNDEFINED:
2562 static int _regulator_do_enable(struct regulator_dev *rdev)
2566 /* Query before enabling in case configuration dependent. */
2567 ret = _regulator_get_enable_time(rdev);
2571 rdev_warn(rdev, "enable_time() failed: %pe\n", ERR_PTR(ret));
2575 trace_regulator_enable(rdev_get_name(rdev));
2577 if (rdev->desc->off_on_delay) {
2578 /* if needed, keep a distance of off_on_delay from last time
2579 * this regulator was disabled.
2581 unsigned long start_jiffy = jiffies;
2582 unsigned long intended, max_delay, remaining;
2584 max_delay = usecs_to_jiffies(rdev->desc->off_on_delay);
2585 intended = rdev->last_off_jiffy + max_delay;
2587 if (time_before(start_jiffy, intended)) {
2588 /* calc remaining jiffies to deal with one-time
2590 * in case of multiple timer wrapping, either it can be
2591 * detected by out-of-range remaining, or it cannot be
2592 * detected and we get a penalty of
2593 * _regulator_enable_delay().
2595 remaining = intended - start_jiffy;
2596 if (remaining <= max_delay)
2597 _regulator_enable_delay(
2598 jiffies_to_usecs(remaining));
2602 if (rdev->ena_pin) {
2603 if (!rdev->ena_gpio_state) {
2604 ret = regulator_ena_gpio_ctrl(rdev, true);
2607 rdev->ena_gpio_state = 1;
2609 } else if (rdev->desc->ops->enable) {
2610 ret = rdev->desc->ops->enable(rdev);
2617 /* Allow the regulator to ramp; it would be useful to extend
2618 * this for bulk operations so that the regulators can ramp
2620 trace_regulator_enable_delay(rdev_get_name(rdev));
2622 /* If poll_enabled_time is set, poll upto the delay calculated
2623 * above, delaying poll_enabled_time uS to check if the regulator
2624 * actually got enabled.
2625 * If the regulator isn't enabled after enable_delay has
2626 * expired, return -ETIMEDOUT.
2628 if (rdev->desc->poll_enabled_time) {
2629 int time_remaining = delay;
2631 while (time_remaining > 0) {
2632 _regulator_enable_delay(rdev->desc->poll_enabled_time);
2634 if (rdev->desc->ops->get_status) {
2635 ret = _regulator_check_status_enabled(rdev);
2640 } else if (rdev->desc->ops->is_enabled(rdev))
2643 time_remaining -= rdev->desc->poll_enabled_time;
2646 if (time_remaining <= 0) {
2647 rdev_err(rdev, "Enabled check timed out\n");
2651 _regulator_enable_delay(delay);
2654 trace_regulator_enable_complete(rdev_get_name(rdev));
2660 * _regulator_handle_consumer_enable - handle that a consumer enabled
2661 * @regulator: regulator source
2663 * Some things on a regulator consumer (like the contribution towards total
2664 * load on the regulator) only have an effect when the consumer wants the
2665 * regulator enabled. Explained in example with two consumers of the same
2667 * consumer A: set_load(100); => total load = 0
2668 * consumer A: regulator_enable(); => total load = 100
2669 * consumer B: set_load(1000); => total load = 100
2670 * consumer B: regulator_enable(); => total load = 1100
2671 * consumer A: regulator_disable(); => total_load = 1000
2673 * This function (together with _regulator_handle_consumer_disable) is
2674 * responsible for keeping track of the refcount for a given regulator consumer
2675 * and applying / unapplying these things.
2677 * Returns 0 upon no error; -error upon error.
2679 static int _regulator_handle_consumer_enable(struct regulator *regulator)
2682 struct regulator_dev *rdev = regulator->rdev;
2684 lockdep_assert_held_once(&rdev->mutex.base);
2686 regulator->enable_count++;
2687 if (regulator->uA_load && regulator->enable_count == 1) {
2688 ret = drms_uA_update(rdev);
2690 regulator->enable_count--;
2698 * _regulator_handle_consumer_disable - handle that a consumer disabled
2699 * @regulator: regulator source
2701 * The opposite of _regulator_handle_consumer_enable().
2703 * Returns 0 upon no error; -error upon error.
2705 static int _regulator_handle_consumer_disable(struct regulator *regulator)
2707 struct regulator_dev *rdev = regulator->rdev;
2709 lockdep_assert_held_once(&rdev->mutex.base);
2711 if (!regulator->enable_count) {
2712 rdev_err(rdev, "Underflow of regulator enable count\n");
2716 regulator->enable_count--;
2717 if (regulator->uA_load && regulator->enable_count == 0)
2718 return drms_uA_update(rdev);
2723 /* locks held by regulator_enable() */
2724 static int _regulator_enable(struct regulator *regulator)
2726 struct regulator_dev *rdev = regulator->rdev;
2729 lockdep_assert_held_once(&rdev->mutex.base);
2731 if (rdev->use_count == 0 && rdev->supply) {
2732 ret = _regulator_enable(rdev->supply);
2737 /* balance only if there are regulators coupled */
2738 if (rdev->coupling_desc.n_coupled > 1) {
2739 ret = regulator_balance_voltage(rdev, PM_SUSPEND_ON);
2741 goto err_disable_supply;
2744 ret = _regulator_handle_consumer_enable(regulator);
2746 goto err_disable_supply;
2748 if (rdev->use_count == 0) {
2749 /* The regulator may on if it's not switchable or left on */
2750 ret = _regulator_is_enabled(rdev);
2751 if (ret == -EINVAL || ret == 0) {
2752 if (!regulator_ops_is_valid(rdev,
2753 REGULATOR_CHANGE_STATUS)) {
2755 goto err_consumer_disable;
2758 ret = _regulator_do_enable(rdev);
2760 goto err_consumer_disable;
2762 _notifier_call_chain(rdev, REGULATOR_EVENT_ENABLE,
2764 } else if (ret < 0) {
2765 rdev_err(rdev, "is_enabled() failed: %pe\n", ERR_PTR(ret));
2766 goto err_consumer_disable;
2768 /* Fallthrough on positive return values - already enabled */
2771 if (regulator->enable_count == 1)
2776 err_consumer_disable:
2777 _regulator_handle_consumer_disable(regulator);
2780 if (rdev->use_count == 0 && rdev->supply)
2781 _regulator_disable(rdev->supply);
2787 * regulator_enable - enable regulator output
2788 * @regulator: regulator source
2790 * Request that the regulator be enabled with the regulator output at
2791 * the predefined voltage or current value. Calls to regulator_enable()
2792 * must be balanced with calls to regulator_disable().
2794 * NOTE: the output value can be set by other drivers, boot loader or may be
2795 * hardwired in the regulator.
2797 int regulator_enable(struct regulator *regulator)
2799 struct regulator_dev *rdev = regulator->rdev;
2800 struct ww_acquire_ctx ww_ctx;
2803 regulator_lock_dependent(rdev, &ww_ctx);
2804 ret = _regulator_enable(regulator);
2805 regulator_unlock_dependent(rdev, &ww_ctx);
2809 EXPORT_SYMBOL_GPL(regulator_enable);
2811 static int _regulator_do_disable(struct regulator_dev *rdev)
2815 trace_regulator_disable(rdev_get_name(rdev));
2817 if (rdev->ena_pin) {
2818 if (rdev->ena_gpio_state) {
2819 ret = regulator_ena_gpio_ctrl(rdev, false);
2822 rdev->ena_gpio_state = 0;
2825 } else if (rdev->desc->ops->disable) {
2826 ret = rdev->desc->ops->disable(rdev);
2831 /* cares about last_off_jiffy only if off_on_delay is required by
2834 if (rdev->desc->off_on_delay)
2835 rdev->last_off_jiffy = jiffies;
2837 trace_regulator_disable_complete(rdev_get_name(rdev));
2842 /* locks held by regulator_disable() */
2843 static int _regulator_disable(struct regulator *regulator)
2845 struct regulator_dev *rdev = regulator->rdev;
2848 lockdep_assert_held_once(&rdev->mutex.base);
2850 if (WARN(regulator->enable_count == 0,
2851 "unbalanced disables for %s\n", rdev_get_name(rdev)))
2854 if (regulator->enable_count == 1) {
2855 /* disabling last enable_count from this regulator */
2856 /* are we the last user and permitted to disable ? */
2857 if (rdev->use_count == 1 &&
2858 (rdev->constraints && !rdev->constraints->always_on)) {
2860 /* we are last user */
2861 if (regulator_ops_is_valid(rdev, REGULATOR_CHANGE_STATUS)) {
2862 ret = _notifier_call_chain(rdev,
2863 REGULATOR_EVENT_PRE_DISABLE,
2865 if (ret & NOTIFY_STOP_MASK)
2868 ret = _regulator_do_disable(rdev);
2870 rdev_err(rdev, "failed to disable: %pe\n", ERR_PTR(ret));
2871 _notifier_call_chain(rdev,
2872 REGULATOR_EVENT_ABORT_DISABLE,
2876 _notifier_call_chain(rdev, REGULATOR_EVENT_DISABLE,
2880 rdev->use_count = 0;
2881 } else if (rdev->use_count > 1) {
2887 ret = _regulator_handle_consumer_disable(regulator);
2889 if (ret == 0 && rdev->coupling_desc.n_coupled > 1)
2890 ret = regulator_balance_voltage(rdev, PM_SUSPEND_ON);
2892 if (ret == 0 && rdev->use_count == 0 && rdev->supply)
2893 ret = _regulator_disable(rdev->supply);
2899 * regulator_disable - disable regulator output
2900 * @regulator: regulator source
2902 * Disable the regulator output voltage or current. Calls to
2903 * regulator_enable() must be balanced with calls to
2904 * regulator_disable().
2906 * NOTE: this will only disable the regulator output if no other consumer
2907 * devices have it enabled, the regulator device supports disabling and
2908 * machine constraints permit this operation.
2910 int regulator_disable(struct regulator *regulator)
2912 struct regulator_dev *rdev = regulator->rdev;
2913 struct ww_acquire_ctx ww_ctx;
2916 regulator_lock_dependent(rdev, &ww_ctx);
2917 ret = _regulator_disable(regulator);
2918 regulator_unlock_dependent(rdev, &ww_ctx);
2922 EXPORT_SYMBOL_GPL(regulator_disable);
2924 /* locks held by regulator_force_disable() */
2925 static int _regulator_force_disable(struct regulator_dev *rdev)
2929 lockdep_assert_held_once(&rdev->mutex.base);
2931 ret = _notifier_call_chain(rdev, REGULATOR_EVENT_FORCE_DISABLE |
2932 REGULATOR_EVENT_PRE_DISABLE, NULL);
2933 if (ret & NOTIFY_STOP_MASK)
2936 ret = _regulator_do_disable(rdev);
2938 rdev_err(rdev, "failed to force disable: %pe\n", ERR_PTR(ret));
2939 _notifier_call_chain(rdev, REGULATOR_EVENT_FORCE_DISABLE |
2940 REGULATOR_EVENT_ABORT_DISABLE, NULL);
2944 _notifier_call_chain(rdev, REGULATOR_EVENT_FORCE_DISABLE |
2945 REGULATOR_EVENT_DISABLE, NULL);
2951 * regulator_force_disable - force disable regulator output
2952 * @regulator: regulator source
2954 * Forcibly disable the regulator output voltage or current.
2955 * NOTE: this *will* disable the regulator output even if other consumer
2956 * devices have it enabled. This should be used for situations when device
2957 * damage will likely occur if the regulator is not disabled (e.g. over temp).
2959 int regulator_force_disable(struct regulator *regulator)
2961 struct regulator_dev *rdev = regulator->rdev;
2962 struct ww_acquire_ctx ww_ctx;
2965 regulator_lock_dependent(rdev, &ww_ctx);
2967 ret = _regulator_force_disable(regulator->rdev);
2969 if (rdev->coupling_desc.n_coupled > 1)
2970 regulator_balance_voltage(rdev, PM_SUSPEND_ON);
2972 if (regulator->uA_load) {
2973 regulator->uA_load = 0;
2974 ret = drms_uA_update(rdev);
2977 if (rdev->use_count != 0 && rdev->supply)
2978 _regulator_disable(rdev->supply);
2980 regulator_unlock_dependent(rdev, &ww_ctx);
2984 EXPORT_SYMBOL_GPL(regulator_force_disable);
2986 static void regulator_disable_work(struct work_struct *work)
2988 struct regulator_dev *rdev = container_of(work, struct regulator_dev,
2990 struct ww_acquire_ctx ww_ctx;
2992 struct regulator *regulator;
2993 int total_count = 0;
2995 regulator_lock_dependent(rdev, &ww_ctx);
2998 * Workqueue functions queue the new work instance while the previous
2999 * work instance is being processed. Cancel the queued work instance
3000 * as the work instance under processing does the job of the queued
3003 cancel_delayed_work(&rdev->disable_work);
3005 list_for_each_entry(regulator, &rdev->consumer_list, list) {
3006 count = regulator->deferred_disables;
3011 total_count += count;
3012 regulator->deferred_disables = 0;
3014 for (i = 0; i < count; i++) {
3015 ret = _regulator_disable(regulator);
3017 rdev_err(rdev, "Deferred disable failed: %pe\n",
3021 WARN_ON(!total_count);
3023 if (rdev->coupling_desc.n_coupled > 1)
3024 regulator_balance_voltage(rdev, PM_SUSPEND_ON);
3026 regulator_unlock_dependent(rdev, &ww_ctx);
3030 * regulator_disable_deferred - disable regulator output with delay
3031 * @regulator: regulator source
3032 * @ms: milliseconds until the regulator is disabled
3034 * Execute regulator_disable() on the regulator after a delay. This
3035 * is intended for use with devices that require some time to quiesce.
3037 * NOTE: this will only disable the regulator output if no other consumer
3038 * devices have it enabled, the regulator device supports disabling and
3039 * machine constraints permit this operation.
3041 int regulator_disable_deferred(struct regulator *regulator, int ms)
3043 struct regulator_dev *rdev = regulator->rdev;
3046 return regulator_disable(regulator);
3048 regulator_lock(rdev);
3049 regulator->deferred_disables++;
3050 mod_delayed_work(system_power_efficient_wq, &rdev->disable_work,
3051 msecs_to_jiffies(ms));
3052 regulator_unlock(rdev);
3056 EXPORT_SYMBOL_GPL(regulator_disable_deferred);
3058 static int _regulator_is_enabled(struct regulator_dev *rdev)
3060 /* A GPIO control always takes precedence */
3062 return rdev->ena_gpio_state;
3064 /* If we don't know then assume that the regulator is always on */
3065 if (!rdev->desc->ops->is_enabled)
3068 return rdev->desc->ops->is_enabled(rdev);
3071 static int _regulator_list_voltage(struct regulator_dev *rdev,
3072 unsigned selector, int lock)
3074 const struct regulator_ops *ops = rdev->desc->ops;
3077 if (rdev->desc->fixed_uV && rdev->desc->n_voltages == 1 && !selector)
3078 return rdev->desc->fixed_uV;
3080 if (ops->list_voltage) {
3081 if (selector >= rdev->desc->n_voltages)
3084 regulator_lock(rdev);
3085 ret = ops->list_voltage(rdev, selector);
3087 regulator_unlock(rdev);
3088 } else if (rdev->is_switch && rdev->supply) {
3089 ret = _regulator_list_voltage(rdev->supply->rdev,
3096 if (ret < rdev->constraints->min_uV)
3098 else if (ret > rdev->constraints->max_uV)
3106 * regulator_is_enabled - is the regulator output enabled
3107 * @regulator: regulator source
3109 * Returns positive if the regulator driver backing the source/client
3110 * has requested that the device be enabled, zero if it hasn't, else a
3111 * negative errno code.
3113 * Note that the device backing this regulator handle can have multiple
3114 * users, so it might be enabled even if regulator_enable() was never
3115 * called for this particular source.
3117 int regulator_is_enabled(struct regulator *regulator)
3121 if (regulator->always_on)
3124 regulator_lock(regulator->rdev);
3125 ret = _regulator_is_enabled(regulator->rdev);
3126 regulator_unlock(regulator->rdev);
3130 EXPORT_SYMBOL_GPL(regulator_is_enabled);
3133 * regulator_count_voltages - count regulator_list_voltage() selectors
3134 * @regulator: regulator source
3136 * Returns number of selectors, or negative errno. Selectors are
3137 * numbered starting at zero, and typically correspond to bitfields
3138 * in hardware registers.
3140 int regulator_count_voltages(struct regulator *regulator)
3142 struct regulator_dev *rdev = regulator->rdev;
3144 if (rdev->desc->n_voltages)
3145 return rdev->desc->n_voltages;
3147 if (!rdev->is_switch || !rdev->supply)
3150 return regulator_count_voltages(rdev->supply);
3152 EXPORT_SYMBOL_GPL(regulator_count_voltages);
3155 * regulator_list_voltage - enumerate supported voltages
3156 * @regulator: regulator source
3157 * @selector: identify voltage to list
3158 * Context: can sleep
3160 * Returns a voltage that can be passed to @regulator_set_voltage(),
3161 * zero if this selector code can't be used on this system, or a
3164 int regulator_list_voltage(struct regulator *regulator, unsigned selector)
3166 return _regulator_list_voltage(regulator->rdev, selector, 1);
3168 EXPORT_SYMBOL_GPL(regulator_list_voltage);
3171 * regulator_get_regmap - get the regulator's register map
3172 * @regulator: regulator source
3174 * Returns the register map for the given regulator, or an ERR_PTR value
3175 * if the regulator doesn't use regmap.
3177 struct regmap *regulator_get_regmap(struct regulator *regulator)
3179 struct regmap *map = regulator->rdev->regmap;
3181 return map ? map : ERR_PTR(-EOPNOTSUPP);
3185 * regulator_get_hardware_vsel_register - get the HW voltage selector register
3186 * @regulator: regulator source
3187 * @vsel_reg: voltage selector register, output parameter
3188 * @vsel_mask: mask for voltage selector bitfield, output parameter
3190 * Returns the hardware register offset and bitmask used for setting the
3191 * regulator voltage. This might be useful when configuring voltage-scaling
3192 * hardware or firmware that can make I2C requests behind the kernel's back,
3195 * On success, the output parameters @vsel_reg and @vsel_mask are filled in
3196 * and 0 is returned, otherwise a negative errno is returned.
3198 int regulator_get_hardware_vsel_register(struct regulator *regulator,
3200 unsigned *vsel_mask)
3202 struct regulator_dev *rdev = regulator->rdev;
3203 const struct regulator_ops *ops = rdev->desc->ops;
3205 if (ops->set_voltage_sel != regulator_set_voltage_sel_regmap)
3208 *vsel_reg = rdev->desc->vsel_reg;
3209 *vsel_mask = rdev->desc->vsel_mask;
3213 EXPORT_SYMBOL_GPL(regulator_get_hardware_vsel_register);
3216 * regulator_list_hardware_vsel - get the HW-specific register value for a selector
3217 * @regulator: regulator source
3218 * @selector: identify voltage to list
3220 * Converts the selector to a hardware-specific voltage selector that can be
3221 * directly written to the regulator registers. The address of the voltage
3222 * register can be determined by calling @regulator_get_hardware_vsel_register.
3224 * On error a negative errno is returned.
3226 int regulator_list_hardware_vsel(struct regulator *regulator,
3229 struct regulator_dev *rdev = regulator->rdev;
3230 const struct regulator_ops *ops = rdev->desc->ops;
3232 if (selector >= rdev->desc->n_voltages)
3234 if (ops->set_voltage_sel != regulator_set_voltage_sel_regmap)
3239 EXPORT_SYMBOL_GPL(regulator_list_hardware_vsel);
3242 * regulator_get_linear_step - return the voltage step size between VSEL values
3243 * @regulator: regulator source
3245 * Returns the voltage step size between VSEL values for linear
3246 * regulators, or return 0 if the regulator isn't a linear regulator.
3248 unsigned int regulator_get_linear_step(struct regulator *regulator)
3250 struct regulator_dev *rdev = regulator->rdev;
3252 return rdev->desc->uV_step;
3254 EXPORT_SYMBOL_GPL(regulator_get_linear_step);
3257 * regulator_is_supported_voltage - check if a voltage range can be supported
3259 * @regulator: Regulator to check.
3260 * @min_uV: Minimum required voltage in uV.
3261 * @max_uV: Maximum required voltage in uV.
3263 * Returns a boolean.
3265 int regulator_is_supported_voltage(struct regulator *regulator,
3266 int min_uV, int max_uV)
3268 struct regulator_dev *rdev = regulator->rdev;
3269 int i, voltages, ret;
3271 /* If we can't change voltage check the current voltage */
3272 if (!regulator_ops_is_valid(rdev, REGULATOR_CHANGE_VOLTAGE)) {
3273 ret = regulator_get_voltage(regulator);
3275 return min_uV <= ret && ret <= max_uV;
3280 /* Any voltage within constrains range is fine? */
3281 if (rdev->desc->continuous_voltage_range)
3282 return min_uV >= rdev->constraints->min_uV &&
3283 max_uV <= rdev->constraints->max_uV;
3285 ret = regulator_count_voltages(regulator);
3290 for (i = 0; i < voltages; i++) {
3291 ret = regulator_list_voltage(regulator, i);
3293 if (ret >= min_uV && ret <= max_uV)
3299 EXPORT_SYMBOL_GPL(regulator_is_supported_voltage);
3301 static int regulator_map_voltage(struct regulator_dev *rdev, int min_uV,
3304 const struct regulator_desc *desc = rdev->desc;
3306 if (desc->ops->map_voltage)
3307 return desc->ops->map_voltage(rdev, min_uV, max_uV);
3309 if (desc->ops->list_voltage == regulator_list_voltage_linear)
3310 return regulator_map_voltage_linear(rdev, min_uV, max_uV);
3312 if (desc->ops->list_voltage == regulator_list_voltage_linear_range)
3313 return regulator_map_voltage_linear_range(rdev, min_uV, max_uV);
3315 if (desc->ops->list_voltage ==
3316 regulator_list_voltage_pickable_linear_range)
3317 return regulator_map_voltage_pickable_linear_range(rdev,
3320 return regulator_map_voltage_iterate(rdev, min_uV, max_uV);
3323 static int _regulator_call_set_voltage(struct regulator_dev *rdev,
3324 int min_uV, int max_uV,
3327 struct pre_voltage_change_data data;
3330 data.old_uV = regulator_get_voltage_rdev(rdev);
3331 data.min_uV = min_uV;
3332 data.max_uV = max_uV;
3333 ret = _notifier_call_chain(rdev, REGULATOR_EVENT_PRE_VOLTAGE_CHANGE,
3335 if (ret & NOTIFY_STOP_MASK)
3338 ret = rdev->desc->ops->set_voltage(rdev, min_uV, max_uV, selector);
3342 _notifier_call_chain(rdev, REGULATOR_EVENT_ABORT_VOLTAGE_CHANGE,
3343 (void *)data.old_uV);
3348 static int _regulator_call_set_voltage_sel(struct regulator_dev *rdev,
3349 int uV, unsigned selector)
3351 struct pre_voltage_change_data data;
3354 data.old_uV = regulator_get_voltage_rdev(rdev);
3357 ret = _notifier_call_chain(rdev, REGULATOR_EVENT_PRE_VOLTAGE_CHANGE,
3359 if (ret & NOTIFY_STOP_MASK)
3362 ret = rdev->desc->ops->set_voltage_sel(rdev, selector);
3366 _notifier_call_chain(rdev, REGULATOR_EVENT_ABORT_VOLTAGE_CHANGE,
3367 (void *)data.old_uV);
3372 static int _regulator_set_voltage_sel_step(struct regulator_dev *rdev,
3373 int uV, int new_selector)
3375 const struct regulator_ops *ops = rdev->desc->ops;
3376 int diff, old_sel, curr_sel, ret;
3378 /* Stepping is only needed if the regulator is enabled. */
3379 if (!_regulator_is_enabled(rdev))
3382 if (!ops->get_voltage_sel)
3385 old_sel = ops->get_voltage_sel(rdev);
3389 diff = new_selector - old_sel;
3391 return 0; /* No change needed. */
3395 for (curr_sel = old_sel + rdev->desc->vsel_step;
3396 curr_sel < new_selector;
3397 curr_sel += rdev->desc->vsel_step) {
3399 * Call the callback directly instead of using
3400 * _regulator_call_set_voltage_sel() as we don't
3401 * want to notify anyone yet. Same in the branch
3404 ret = ops->set_voltage_sel(rdev, curr_sel);
3409 /* Stepping down. */
3410 for (curr_sel = old_sel - rdev->desc->vsel_step;
3411 curr_sel > new_selector;
3412 curr_sel -= rdev->desc->vsel_step) {
3413 ret = ops->set_voltage_sel(rdev, curr_sel);
3420 /* The final selector will trigger the notifiers. */
3421 return _regulator_call_set_voltage_sel(rdev, uV, new_selector);
3425 * At least try to return to the previous voltage if setting a new
3428 (void)ops->set_voltage_sel(rdev, old_sel);
3432 static int _regulator_set_voltage_time(struct regulator_dev *rdev,
3433 int old_uV, int new_uV)
3435 unsigned int ramp_delay = 0;
3437 if (rdev->constraints->ramp_delay)
3438 ramp_delay = rdev->constraints->ramp_delay;
3439 else if (rdev->desc->ramp_delay)
3440 ramp_delay = rdev->desc->ramp_delay;
3441 else if (rdev->constraints->settling_time)
3442 return rdev->constraints->settling_time;
3443 else if (rdev->constraints->settling_time_up &&
3445 return rdev->constraints->settling_time_up;
3446 else if (rdev->constraints->settling_time_down &&
3448 return rdev->constraints->settling_time_down;
3450 if (ramp_delay == 0) {
3451 rdev_dbg(rdev, "ramp_delay not set\n");
3455 return DIV_ROUND_UP(abs(new_uV - old_uV), ramp_delay);
3458 static int _regulator_do_set_voltage(struct regulator_dev *rdev,
3459 int min_uV, int max_uV)
3464 unsigned int selector;
3465 int old_selector = -1;
3466 const struct regulator_ops *ops = rdev->desc->ops;
3467 int old_uV = regulator_get_voltage_rdev(rdev);
3469 trace_regulator_set_voltage(rdev_get_name(rdev), min_uV, max_uV);
3471 min_uV += rdev->constraints->uV_offset;
3472 max_uV += rdev->constraints->uV_offset;
3475 * If we can't obtain the old selector there is not enough
3476 * info to call set_voltage_time_sel().
3478 if (_regulator_is_enabled(rdev) &&
3479 ops->set_voltage_time_sel && ops->get_voltage_sel) {
3480 old_selector = ops->get_voltage_sel(rdev);
3481 if (old_selector < 0)
3482 return old_selector;
3485 if (ops->set_voltage) {
3486 ret = _regulator_call_set_voltage(rdev, min_uV, max_uV,
3490 if (ops->list_voltage)
3491 best_val = ops->list_voltage(rdev,
3494 best_val = regulator_get_voltage_rdev(rdev);
3497 } else if (ops->set_voltage_sel) {
3498 ret = regulator_map_voltage(rdev, min_uV, max_uV);
3500 best_val = ops->list_voltage(rdev, ret);
3501 if (min_uV <= best_val && max_uV >= best_val) {
3503 if (old_selector == selector)
3505 else if (rdev->desc->vsel_step)
3506 ret = _regulator_set_voltage_sel_step(
3507 rdev, best_val, selector);
3509 ret = _regulator_call_set_voltage_sel(
3510 rdev, best_val, selector);
3522 if (ops->set_voltage_time_sel) {
3524 * Call set_voltage_time_sel if successfully obtained
3527 if (old_selector >= 0 && old_selector != selector)
3528 delay = ops->set_voltage_time_sel(rdev, old_selector,
3531 if (old_uV != best_val) {
3532 if (ops->set_voltage_time)
3533 delay = ops->set_voltage_time(rdev, old_uV,
3536 delay = _regulator_set_voltage_time(rdev,
3543 rdev_warn(rdev, "failed to get delay: %pe\n", ERR_PTR(delay));
3547 /* Insert any necessary delays */
3548 if (delay >= 1000) {
3549 mdelay(delay / 1000);
3550 udelay(delay % 1000);
3555 if (best_val >= 0) {
3556 unsigned long data = best_val;
3558 _notifier_call_chain(rdev, REGULATOR_EVENT_VOLTAGE_CHANGE,
3563 trace_regulator_set_voltage_complete(rdev_get_name(rdev), best_val);
3568 static int _regulator_do_set_suspend_voltage(struct regulator_dev *rdev,
3569 int min_uV, int max_uV, suspend_state_t state)
3571 struct regulator_state *rstate;
3574 rstate = regulator_get_suspend_state(rdev, state);
3578 if (min_uV < rstate->min_uV)
3579 min_uV = rstate->min_uV;
3580 if (max_uV > rstate->max_uV)
3581 max_uV = rstate->max_uV;
3583 sel = regulator_map_voltage(rdev, min_uV, max_uV);
3587 uV = rdev->desc->ops->list_voltage(rdev, sel);
3588 if (uV >= min_uV && uV <= max_uV)
3594 static int regulator_set_voltage_unlocked(struct regulator *regulator,
3595 int min_uV, int max_uV,
3596 suspend_state_t state)
3598 struct regulator_dev *rdev = regulator->rdev;
3599 struct regulator_voltage *voltage = ®ulator->voltage[state];
3601 int old_min_uV, old_max_uV;
3604 /* If we're setting the same range as last time the change
3605 * should be a noop (some cpufreq implementations use the same
3606 * voltage for multiple frequencies, for example).
3608 if (voltage->min_uV == min_uV && voltage->max_uV == max_uV)
3611 /* If we're trying to set a range that overlaps the current voltage,
3612 * return successfully even though the regulator does not support
3613 * changing the voltage.
3615 if (!regulator_ops_is_valid(rdev, REGULATOR_CHANGE_VOLTAGE)) {
3616 current_uV = regulator_get_voltage_rdev(rdev);
3617 if (min_uV <= current_uV && current_uV <= max_uV) {
3618 voltage->min_uV = min_uV;
3619 voltage->max_uV = max_uV;
3625 if (!rdev->desc->ops->set_voltage &&
3626 !rdev->desc->ops->set_voltage_sel) {
3631 /* constraints check */
3632 ret = regulator_check_voltage(rdev, &min_uV, &max_uV);
3636 /* restore original values in case of error */
3637 old_min_uV = voltage->min_uV;
3638 old_max_uV = voltage->max_uV;
3639 voltage->min_uV = min_uV;
3640 voltage->max_uV = max_uV;
3642 /* for not coupled regulators this will just set the voltage */
3643 ret = regulator_balance_voltage(rdev, state);
3645 voltage->min_uV = old_min_uV;
3646 voltage->max_uV = old_max_uV;
3653 int regulator_set_voltage_rdev(struct regulator_dev *rdev, int min_uV,
3654 int max_uV, suspend_state_t state)
3656 int best_supply_uV = 0;
3657 int supply_change_uV = 0;
3661 regulator_ops_is_valid(rdev->supply->rdev,
3662 REGULATOR_CHANGE_VOLTAGE) &&
3663 (rdev->desc->min_dropout_uV || !(rdev->desc->ops->get_voltage ||
3664 rdev->desc->ops->get_voltage_sel))) {
3665 int current_supply_uV;
3668 selector = regulator_map_voltage(rdev, min_uV, max_uV);
3674 best_supply_uV = _regulator_list_voltage(rdev, selector, 0);
3675 if (best_supply_uV < 0) {
3676 ret = best_supply_uV;
3680 best_supply_uV += rdev->desc->min_dropout_uV;
3682 current_supply_uV = regulator_get_voltage_rdev(rdev->supply->rdev);
3683 if (current_supply_uV < 0) {
3684 ret = current_supply_uV;
3688 supply_change_uV = best_supply_uV - current_supply_uV;
3691 if (supply_change_uV > 0) {
3692 ret = regulator_set_voltage_unlocked(rdev->supply,
3693 best_supply_uV, INT_MAX, state);
3695 dev_err(&rdev->dev, "Failed to increase supply voltage: %pe\n",
3701 if (state == PM_SUSPEND_ON)
3702 ret = _regulator_do_set_voltage(rdev, min_uV, max_uV);
3704 ret = _regulator_do_set_suspend_voltage(rdev, min_uV,
3709 if (supply_change_uV < 0) {
3710 ret = regulator_set_voltage_unlocked(rdev->supply,
3711 best_supply_uV, INT_MAX, state);
3713 dev_warn(&rdev->dev, "Failed to decrease supply voltage: %pe\n",
3715 /* No need to fail here */
3722 EXPORT_SYMBOL_GPL(regulator_set_voltage_rdev);
3724 static int regulator_limit_voltage_step(struct regulator_dev *rdev,
3725 int *current_uV, int *min_uV)
3727 struct regulation_constraints *constraints = rdev->constraints;
3729 /* Limit voltage change only if necessary */
3730 if (!constraints->max_uV_step || !_regulator_is_enabled(rdev))
3733 if (*current_uV < 0) {
3734 *current_uV = regulator_get_voltage_rdev(rdev);
3736 if (*current_uV < 0)
3740 if (abs(*current_uV - *min_uV) <= constraints->max_uV_step)
3743 /* Clamp target voltage within the given step */
3744 if (*current_uV < *min_uV)
3745 *min_uV = min(*current_uV + constraints->max_uV_step,
3748 *min_uV = max(*current_uV - constraints->max_uV_step,
3754 static int regulator_get_optimal_voltage(struct regulator_dev *rdev,
3756 int *min_uV, int *max_uV,
3757 suspend_state_t state,
3760 struct coupling_desc *c_desc = &rdev->coupling_desc;
3761 struct regulator_dev **c_rdevs = c_desc->coupled_rdevs;
3762 struct regulation_constraints *constraints = rdev->constraints;
3763 int desired_min_uV = 0, desired_max_uV = INT_MAX;
3764 int max_current_uV = 0, min_current_uV = INT_MAX;
3765 int highest_min_uV = 0, target_uV, possible_uV;
3766 int i, ret, max_spread;
3772 * If there are no coupled regulators, simply set the voltage
3773 * demanded by consumers.
3775 if (n_coupled == 1) {
3777 * If consumers don't provide any demands, set voltage
3780 desired_min_uV = constraints->min_uV;
3781 desired_max_uV = constraints->max_uV;
3783 ret = regulator_check_consumers(rdev,
3785 &desired_max_uV, state);
3789 possible_uV = desired_min_uV;
3795 /* Find highest min desired voltage */
3796 for (i = 0; i < n_coupled; i++) {
3798 int tmp_max = INT_MAX;
3800 lockdep_assert_held_once(&c_rdevs[i]->mutex.base);
3802 ret = regulator_check_consumers(c_rdevs[i],
3808 ret = regulator_check_voltage(c_rdevs[i], &tmp_min, &tmp_max);
3812 highest_min_uV = max(highest_min_uV, tmp_min);
3815 desired_min_uV = tmp_min;
3816 desired_max_uV = tmp_max;
3820 max_spread = constraints->max_spread[0];
3823 * Let target_uV be equal to the desired one if possible.
3824 * If not, set it to minimum voltage, allowed by other coupled
3827 target_uV = max(desired_min_uV, highest_min_uV - max_spread);
3830 * Find min and max voltages, which currently aren't violating
3833 for (i = 1; i < n_coupled; i++) {
3836 if (!_regulator_is_enabled(c_rdevs[i]))
3839 tmp_act = regulator_get_voltage_rdev(c_rdevs[i]);
3843 min_current_uV = min(tmp_act, min_current_uV);
3844 max_current_uV = max(tmp_act, max_current_uV);
3847 /* There aren't any other regulators enabled */
3848 if (max_current_uV == 0) {
3849 possible_uV = target_uV;
3852 * Correct target voltage, so as it currently isn't
3853 * violating max_spread
3855 possible_uV = max(target_uV, max_current_uV - max_spread);
3856 possible_uV = min(possible_uV, min_current_uV + max_spread);
3859 if (possible_uV > desired_max_uV)
3862 done = (possible_uV == target_uV);
3863 desired_min_uV = possible_uV;
3866 /* Apply max_uV_step constraint if necessary */
3867 if (state == PM_SUSPEND_ON) {
3868 ret = regulator_limit_voltage_step(rdev, current_uV,
3877 /* Set current_uV if wasn't done earlier in the code and if necessary */
3878 if (n_coupled > 1 && *current_uV == -1) {
3880 if (_regulator_is_enabled(rdev)) {
3881 ret = regulator_get_voltage_rdev(rdev);
3887 *current_uV = desired_min_uV;
3891 *min_uV = desired_min_uV;
3892 *max_uV = desired_max_uV;
3897 int regulator_do_balance_voltage(struct regulator_dev *rdev,
3898 suspend_state_t state, bool skip_coupled)
3900 struct regulator_dev **c_rdevs;
3901 struct regulator_dev *best_rdev;
3902 struct coupling_desc *c_desc = &rdev->coupling_desc;
3903 int i, ret, n_coupled, best_min_uV, best_max_uV, best_c_rdev;
3904 unsigned int delta, best_delta;
3905 unsigned long c_rdev_done = 0;
3906 bool best_c_rdev_done;
3908 c_rdevs = c_desc->coupled_rdevs;
3909 n_coupled = skip_coupled ? 1 : c_desc->n_coupled;
3912 * Find the best possible voltage change on each loop. Leave the loop
3913 * if there isn't any possible change.
3916 best_c_rdev_done = false;
3924 * Find highest difference between optimal voltage
3925 * and current voltage.
3927 for (i = 0; i < n_coupled; i++) {
3929 * optimal_uV is the best voltage that can be set for
3930 * i-th regulator at the moment without violating
3931 * max_spread constraint in order to balance
3932 * the coupled voltages.
3934 int optimal_uV = 0, optimal_max_uV = 0, current_uV = 0;
3936 if (test_bit(i, &c_rdev_done))
3939 ret = regulator_get_optimal_voltage(c_rdevs[i],
3947 delta = abs(optimal_uV - current_uV);
3949 if (delta && best_delta <= delta) {
3950 best_c_rdev_done = ret;
3952 best_rdev = c_rdevs[i];
3953 best_min_uV = optimal_uV;
3954 best_max_uV = optimal_max_uV;
3959 /* Nothing to change, return successfully */
3965 ret = regulator_set_voltage_rdev(best_rdev, best_min_uV,
3966 best_max_uV, state);
3971 if (best_c_rdev_done)
3972 set_bit(best_c_rdev, &c_rdev_done);
3974 } while (n_coupled > 1);
3980 static int regulator_balance_voltage(struct regulator_dev *rdev,
3981 suspend_state_t state)
3983 struct coupling_desc *c_desc = &rdev->coupling_desc;
3984 struct regulator_coupler *coupler = c_desc->coupler;
3985 bool skip_coupled = false;
3988 * If system is in a state other than PM_SUSPEND_ON, don't check
3989 * other coupled regulators.
3991 if (state != PM_SUSPEND_ON)
3992 skip_coupled = true;
3994 if (c_desc->n_resolved < c_desc->n_coupled) {
3995 rdev_err(rdev, "Not all coupled regulators registered\n");
3999 /* Invoke custom balancer for customized couplers */
4000 if (coupler && coupler->balance_voltage)
4001 return coupler->balance_voltage(coupler, rdev, state);
4003 return regulator_do_balance_voltage(rdev, state, skip_coupled);
4007 * regulator_set_voltage - set regulator output voltage
4008 * @regulator: regulator source
4009 * @min_uV: Minimum required voltage in uV
4010 * @max_uV: Maximum acceptable voltage in uV
4012 * Sets a voltage regulator to the desired output voltage. This can be set
4013 * during any regulator state. IOW, regulator can be disabled or enabled.
4015 * If the regulator is enabled then the voltage will change to the new value
4016 * immediately otherwise if the regulator is disabled the regulator will
4017 * output at the new voltage when enabled.
4019 * NOTE: If the regulator is shared between several devices then the lowest
4020 * request voltage that meets the system constraints will be used.
4021 * Regulator system constraints must be set for this regulator before
4022 * calling this function otherwise this call will fail.
4024 int regulator_set_voltage(struct regulator *regulator, int min_uV, int max_uV)
4026 struct ww_acquire_ctx ww_ctx;
4029 regulator_lock_dependent(regulator->rdev, &ww_ctx);
4031 ret = regulator_set_voltage_unlocked(regulator, min_uV, max_uV,
4034 regulator_unlock_dependent(regulator->rdev, &ww_ctx);
4038 EXPORT_SYMBOL_GPL(regulator_set_voltage);
4040 static inline int regulator_suspend_toggle(struct regulator_dev *rdev,
4041 suspend_state_t state, bool en)
4043 struct regulator_state *rstate;
4045 rstate = regulator_get_suspend_state(rdev, state);
4049 if (!rstate->changeable)
4052 rstate->enabled = (en) ? ENABLE_IN_SUSPEND : DISABLE_IN_SUSPEND;
4057 int regulator_suspend_enable(struct regulator_dev *rdev,
4058 suspend_state_t state)
4060 return regulator_suspend_toggle(rdev, state, true);
4062 EXPORT_SYMBOL_GPL(regulator_suspend_enable);
4064 int regulator_suspend_disable(struct regulator_dev *rdev,
4065 suspend_state_t state)
4067 struct regulator *regulator;
4068 struct regulator_voltage *voltage;
4071 * if any consumer wants this regulator device keeping on in
4072 * suspend states, don't set it as disabled.
4074 list_for_each_entry(regulator, &rdev->consumer_list, list) {
4075 voltage = ®ulator->voltage[state];
4076 if (voltage->min_uV || voltage->max_uV)
4080 return regulator_suspend_toggle(rdev, state, false);
4082 EXPORT_SYMBOL_GPL(regulator_suspend_disable);
4084 static int _regulator_set_suspend_voltage(struct regulator *regulator,
4085 int min_uV, int max_uV,
4086 suspend_state_t state)
4088 struct regulator_dev *rdev = regulator->rdev;
4089 struct regulator_state *rstate;
4091 rstate = regulator_get_suspend_state(rdev, state);
4095 if (rstate->min_uV == rstate->max_uV) {
4096 rdev_err(rdev, "The suspend voltage can't be changed!\n");
4100 return regulator_set_voltage_unlocked(regulator, min_uV, max_uV, state);
4103 int regulator_set_suspend_voltage(struct regulator *regulator, int min_uV,
4104 int max_uV, suspend_state_t state)
4106 struct ww_acquire_ctx ww_ctx;
4109 /* PM_SUSPEND_ON is handled by regulator_set_voltage() */
4110 if (regulator_check_states(state) || state == PM_SUSPEND_ON)
4113 regulator_lock_dependent(regulator->rdev, &ww_ctx);
4115 ret = _regulator_set_suspend_voltage(regulator, min_uV,
4118 regulator_unlock_dependent(regulator->rdev, &ww_ctx);
4122 EXPORT_SYMBOL_GPL(regulator_set_suspend_voltage);
4125 * regulator_set_voltage_time - get raise/fall time
4126 * @regulator: regulator source
4127 * @old_uV: starting voltage in microvolts
4128 * @new_uV: target voltage in microvolts
4130 * Provided with the starting and ending voltage, this function attempts to
4131 * calculate the time in microseconds required to rise or fall to this new
4134 int regulator_set_voltage_time(struct regulator *regulator,
4135 int old_uV, int new_uV)
4137 struct regulator_dev *rdev = regulator->rdev;
4138 const struct regulator_ops *ops = rdev->desc->ops;
4144 if (ops->set_voltage_time)
4145 return ops->set_voltage_time(rdev, old_uV, new_uV);
4146 else if (!ops->set_voltage_time_sel)
4147 return _regulator_set_voltage_time(rdev, old_uV, new_uV);
4149 /* Currently requires operations to do this */
4150 if (!ops->list_voltage || !rdev->desc->n_voltages)
4153 for (i = 0; i < rdev->desc->n_voltages; i++) {
4154 /* We only look for exact voltage matches here */
4155 voltage = regulator_list_voltage(regulator, i);
4160 if (voltage == old_uV)
4162 if (voltage == new_uV)
4166 if (old_sel < 0 || new_sel < 0)
4169 return ops->set_voltage_time_sel(rdev, old_sel, new_sel);
4171 EXPORT_SYMBOL_GPL(regulator_set_voltage_time);
4174 * regulator_set_voltage_time_sel - get raise/fall time
4175 * @rdev: regulator source device
4176 * @old_selector: selector for starting voltage
4177 * @new_selector: selector for target voltage
4179 * Provided with the starting and target voltage selectors, this function
4180 * returns time in microseconds required to rise or fall to this new voltage
4182 * Drivers providing ramp_delay in regulation_constraints can use this as their
4183 * set_voltage_time_sel() operation.
4185 int regulator_set_voltage_time_sel(struct regulator_dev *rdev,
4186 unsigned int old_selector,
4187 unsigned int new_selector)
4189 int old_volt, new_volt;
4192 if (!rdev->desc->ops->list_voltage)
4195 old_volt = rdev->desc->ops->list_voltage(rdev, old_selector);
4196 new_volt = rdev->desc->ops->list_voltage(rdev, new_selector);
4198 if (rdev->desc->ops->set_voltage_time)
4199 return rdev->desc->ops->set_voltage_time(rdev, old_volt,
4202 return _regulator_set_voltage_time(rdev, old_volt, new_volt);
4204 EXPORT_SYMBOL_GPL(regulator_set_voltage_time_sel);
4207 * regulator_sync_voltage - re-apply last regulator output voltage
4208 * @regulator: regulator source
4210 * Re-apply the last configured voltage. This is intended to be used
4211 * where some external control source the consumer is cooperating with
4212 * has caused the configured voltage to change.
4214 int regulator_sync_voltage(struct regulator *regulator)
4216 struct regulator_dev *rdev = regulator->rdev;
4217 struct regulator_voltage *voltage = ®ulator->voltage[PM_SUSPEND_ON];
4218 int ret, min_uV, max_uV;
4220 regulator_lock(rdev);
4222 if (!rdev->desc->ops->set_voltage &&
4223 !rdev->desc->ops->set_voltage_sel) {
4228 /* This is only going to work if we've had a voltage configured. */
4229 if (!voltage->min_uV && !voltage->max_uV) {
4234 min_uV = voltage->min_uV;
4235 max_uV = voltage->max_uV;
4237 /* This should be a paranoia check... */
4238 ret = regulator_check_voltage(rdev, &min_uV, &max_uV);
4242 ret = regulator_check_consumers(rdev, &min_uV, &max_uV, 0);
4246 ret = _regulator_do_set_voltage(rdev, min_uV, max_uV);
4249 regulator_unlock(rdev);
4252 EXPORT_SYMBOL_GPL(regulator_sync_voltage);
4254 int regulator_get_voltage_rdev(struct regulator_dev *rdev)
4259 if (rdev->desc->ops->get_bypass) {
4260 ret = rdev->desc->ops->get_bypass(rdev, &bypassed);
4264 /* if bypassed the regulator must have a supply */
4265 if (!rdev->supply) {
4267 "bypassed regulator has no supply!\n");
4268 return -EPROBE_DEFER;
4271 return regulator_get_voltage_rdev(rdev->supply->rdev);
4275 if (rdev->desc->ops->get_voltage_sel) {
4276 sel = rdev->desc->ops->get_voltage_sel(rdev);
4279 ret = rdev->desc->ops->list_voltage(rdev, sel);
4280 } else if (rdev->desc->ops->get_voltage) {
4281 ret = rdev->desc->ops->get_voltage(rdev);
4282 } else if (rdev->desc->ops->list_voltage) {
4283 ret = rdev->desc->ops->list_voltage(rdev, 0);
4284 } else if (rdev->desc->fixed_uV && (rdev->desc->n_voltages == 1)) {
4285 ret = rdev->desc->fixed_uV;
4286 } else if (rdev->supply) {
4287 ret = regulator_get_voltage_rdev(rdev->supply->rdev);
4288 } else if (rdev->supply_name) {
4289 return -EPROBE_DEFER;
4296 return ret - rdev->constraints->uV_offset;
4298 EXPORT_SYMBOL_GPL(regulator_get_voltage_rdev);
4301 * regulator_get_voltage - get regulator output voltage
4302 * @regulator: regulator source
4304 * This returns the current regulator voltage in uV.
4306 * NOTE: If the regulator is disabled it will return the voltage value. This
4307 * function should not be used to determine regulator state.
4309 int regulator_get_voltage(struct regulator *regulator)
4311 struct ww_acquire_ctx ww_ctx;
4314 regulator_lock_dependent(regulator->rdev, &ww_ctx);
4315 ret = regulator_get_voltage_rdev(regulator->rdev);
4316 regulator_unlock_dependent(regulator->rdev, &ww_ctx);
4320 EXPORT_SYMBOL_GPL(regulator_get_voltage);
4323 * regulator_set_current_limit - set regulator output current limit
4324 * @regulator: regulator source
4325 * @min_uA: Minimum supported current in uA
4326 * @max_uA: Maximum supported current in uA
4328 * Sets current sink to the desired output current. This can be set during
4329 * any regulator state. IOW, regulator can be disabled or enabled.
4331 * If the regulator is enabled then the current will change to the new value
4332 * immediately otherwise if the regulator is disabled the regulator will
4333 * output at the new current when enabled.
4335 * NOTE: Regulator system constraints must be set for this regulator before
4336 * calling this function otherwise this call will fail.
4338 int regulator_set_current_limit(struct regulator *regulator,
4339 int min_uA, int max_uA)
4341 struct regulator_dev *rdev = regulator->rdev;
4344 regulator_lock(rdev);
4347 if (!rdev->desc->ops->set_current_limit) {
4352 /* constraints check */
4353 ret = regulator_check_current_limit(rdev, &min_uA, &max_uA);
4357 ret = rdev->desc->ops->set_current_limit(rdev, min_uA, max_uA);
4359 regulator_unlock(rdev);
4362 EXPORT_SYMBOL_GPL(regulator_set_current_limit);
4364 static int _regulator_get_current_limit_unlocked(struct regulator_dev *rdev)
4367 if (!rdev->desc->ops->get_current_limit)
4370 return rdev->desc->ops->get_current_limit(rdev);
4373 static int _regulator_get_current_limit(struct regulator_dev *rdev)
4377 regulator_lock(rdev);
4378 ret = _regulator_get_current_limit_unlocked(rdev);
4379 regulator_unlock(rdev);
4385 * regulator_get_current_limit - get regulator output current
4386 * @regulator: regulator source
4388 * This returns the current supplied by the specified current sink in uA.
4390 * NOTE: If the regulator is disabled it will return the current value. This
4391 * function should not be used to determine regulator state.
4393 int regulator_get_current_limit(struct regulator *regulator)
4395 return _regulator_get_current_limit(regulator->rdev);
4397 EXPORT_SYMBOL_GPL(regulator_get_current_limit);
4400 * regulator_set_mode - set regulator operating mode
4401 * @regulator: regulator source
4402 * @mode: operating mode - one of the REGULATOR_MODE constants
4404 * Set regulator operating mode to increase regulator efficiency or improve
4405 * regulation performance.
4407 * NOTE: Regulator system constraints must be set for this regulator before
4408 * calling this function otherwise this call will fail.
4410 int regulator_set_mode(struct regulator *regulator, unsigned int mode)
4412 struct regulator_dev *rdev = regulator->rdev;
4414 int regulator_curr_mode;
4416 regulator_lock(rdev);
4419 if (!rdev->desc->ops->set_mode) {
4424 /* return if the same mode is requested */
4425 if (rdev->desc->ops->get_mode) {
4426 regulator_curr_mode = rdev->desc->ops->get_mode(rdev);
4427 if (regulator_curr_mode == mode) {
4433 /* constraints check */
4434 ret = regulator_mode_constrain(rdev, &mode);
4438 ret = rdev->desc->ops->set_mode(rdev, mode);
4440 regulator_unlock(rdev);
4443 EXPORT_SYMBOL_GPL(regulator_set_mode);
4445 static unsigned int _regulator_get_mode_unlocked(struct regulator_dev *rdev)
4448 if (!rdev->desc->ops->get_mode)
4451 return rdev->desc->ops->get_mode(rdev);
4454 static unsigned int _regulator_get_mode(struct regulator_dev *rdev)
4458 regulator_lock(rdev);
4459 ret = _regulator_get_mode_unlocked(rdev);
4460 regulator_unlock(rdev);
4466 * regulator_get_mode - get regulator operating mode
4467 * @regulator: regulator source
4469 * Get the current regulator operating mode.
4471 unsigned int regulator_get_mode(struct regulator *regulator)
4473 return _regulator_get_mode(regulator->rdev);
4475 EXPORT_SYMBOL_GPL(regulator_get_mode);
4477 static int _regulator_get_error_flags(struct regulator_dev *rdev,
4478 unsigned int *flags)
4482 regulator_lock(rdev);
4485 if (!rdev->desc->ops->get_error_flags) {
4490 ret = rdev->desc->ops->get_error_flags(rdev, flags);
4492 regulator_unlock(rdev);
4497 * regulator_get_error_flags - get regulator error information
4498 * @regulator: regulator source
4499 * @flags: pointer to store error flags
4501 * Get the current regulator error information.
4503 int regulator_get_error_flags(struct regulator *regulator,
4504 unsigned int *flags)
4506 return _regulator_get_error_flags(regulator->rdev, flags);
4508 EXPORT_SYMBOL_GPL(regulator_get_error_flags);
4511 * regulator_set_load - set regulator load
4512 * @regulator: regulator source
4513 * @uA_load: load current
4515 * Notifies the regulator core of a new device load. This is then used by
4516 * DRMS (if enabled by constraints) to set the most efficient regulator
4517 * operating mode for the new regulator loading.
4519 * Consumer devices notify their supply regulator of the maximum power
4520 * they will require (can be taken from device datasheet in the power
4521 * consumption tables) when they change operational status and hence power
4522 * state. Examples of operational state changes that can affect power
4523 * consumption are :-
4525 * o Device is opened / closed.
4526 * o Device I/O is about to begin or has just finished.
4527 * o Device is idling in between work.
4529 * This information is also exported via sysfs to userspace.
4531 * DRMS will sum the total requested load on the regulator and change
4532 * to the most efficient operating mode if platform constraints allow.
4534 * NOTE: when a regulator consumer requests to have a regulator
4535 * disabled then any load that consumer requested no longer counts
4536 * toward the total requested load. If the regulator is re-enabled
4537 * then the previously requested load will start counting again.
4539 * If a regulator is an always-on regulator then an individual consumer's
4540 * load will still be removed if that consumer is fully disabled.
4542 * On error a negative errno is returned.
4544 int regulator_set_load(struct regulator *regulator, int uA_load)
4546 struct regulator_dev *rdev = regulator->rdev;
4550 regulator_lock(rdev);
4551 old_uA_load = regulator->uA_load;
4552 regulator->uA_load = uA_load;
4553 if (regulator->enable_count && old_uA_load != uA_load) {
4554 ret = drms_uA_update(rdev);
4556 regulator->uA_load = old_uA_load;
4558 regulator_unlock(rdev);
4562 EXPORT_SYMBOL_GPL(regulator_set_load);
4565 * regulator_allow_bypass - allow the regulator to go into bypass mode
4567 * @regulator: Regulator to configure
4568 * @enable: enable or disable bypass mode
4570 * Allow the regulator to go into bypass mode if all other consumers
4571 * for the regulator also enable bypass mode and the machine
4572 * constraints allow this. Bypass mode means that the regulator is
4573 * simply passing the input directly to the output with no regulation.
4575 int regulator_allow_bypass(struct regulator *regulator, bool enable)
4577 struct regulator_dev *rdev = regulator->rdev;
4578 const char *name = rdev_get_name(rdev);
4581 if (!rdev->desc->ops->set_bypass)
4584 if (!regulator_ops_is_valid(rdev, REGULATOR_CHANGE_BYPASS))
4587 regulator_lock(rdev);
4589 if (enable && !regulator->bypass) {
4590 rdev->bypass_count++;
4592 if (rdev->bypass_count == rdev->open_count) {
4593 trace_regulator_bypass_enable(name);
4595 ret = rdev->desc->ops->set_bypass(rdev, enable);
4597 rdev->bypass_count--;
4599 trace_regulator_bypass_enable_complete(name);
4602 } else if (!enable && regulator->bypass) {
4603 rdev->bypass_count--;
4605 if (rdev->bypass_count != rdev->open_count) {
4606 trace_regulator_bypass_disable(name);
4608 ret = rdev->desc->ops->set_bypass(rdev, enable);
4610 rdev->bypass_count++;
4612 trace_regulator_bypass_disable_complete(name);
4617 regulator->bypass = enable;
4619 regulator_unlock(rdev);
4623 EXPORT_SYMBOL_GPL(regulator_allow_bypass);
4626 * regulator_register_notifier - register regulator event notifier
4627 * @regulator: regulator source
4628 * @nb: notifier block
4630 * Register notifier block to receive regulator events.
4632 int regulator_register_notifier(struct regulator *regulator,
4633 struct notifier_block *nb)
4635 return blocking_notifier_chain_register(®ulator->rdev->notifier,
4638 EXPORT_SYMBOL_GPL(regulator_register_notifier);
4641 * regulator_unregister_notifier - unregister regulator event notifier
4642 * @regulator: regulator source
4643 * @nb: notifier block
4645 * Unregister regulator event notifier block.
4647 int regulator_unregister_notifier(struct regulator *regulator,
4648 struct notifier_block *nb)
4650 return blocking_notifier_chain_unregister(®ulator->rdev->notifier,
4653 EXPORT_SYMBOL_GPL(regulator_unregister_notifier);
4655 /* notify regulator consumers and downstream regulator consumers.
4656 * Note mutex must be held by caller.
4658 static int _notifier_call_chain(struct regulator_dev *rdev,
4659 unsigned long event, void *data)
4661 /* call rdev chain first */
4662 return blocking_notifier_call_chain(&rdev->notifier, event, data);
4666 * regulator_bulk_get - get multiple regulator consumers
4668 * @dev: Device to supply
4669 * @num_consumers: Number of consumers to register
4670 * @consumers: Configuration of consumers; clients are stored here.
4672 * @return 0 on success, an errno on failure.
4674 * This helper function allows drivers to get several regulator
4675 * consumers in one operation. If any of the regulators cannot be
4676 * acquired then any regulators that were allocated will be freed
4677 * before returning to the caller.
4679 int regulator_bulk_get(struct device *dev, int num_consumers,
4680 struct regulator_bulk_data *consumers)
4685 for (i = 0; i < num_consumers; i++)
4686 consumers[i].consumer = NULL;
4688 for (i = 0; i < num_consumers; i++) {
4689 consumers[i].consumer = regulator_get(dev,
4690 consumers[i].supply);
4691 if (IS_ERR(consumers[i].consumer)) {
4692 ret = PTR_ERR(consumers[i].consumer);
4693 consumers[i].consumer = NULL;
4701 if (ret != -EPROBE_DEFER)
4702 dev_err(dev, "Failed to get supply '%s': %pe\n",
4703 consumers[i].supply, ERR_PTR(ret));
4705 dev_dbg(dev, "Failed to get supply '%s', deferring\n",
4706 consumers[i].supply);
4709 regulator_put(consumers[i].consumer);
4713 EXPORT_SYMBOL_GPL(regulator_bulk_get);
4715 static void regulator_bulk_enable_async(void *data, async_cookie_t cookie)
4717 struct regulator_bulk_data *bulk = data;
4719 bulk->ret = regulator_enable(bulk->consumer);
4723 * regulator_bulk_enable - enable multiple regulator consumers
4725 * @num_consumers: Number of consumers
4726 * @consumers: Consumer data; clients are stored here.
4727 * @return 0 on success, an errno on failure
4729 * This convenience API allows consumers to enable multiple regulator
4730 * clients in a single API call. If any consumers cannot be enabled
4731 * then any others that were enabled will be disabled again prior to
4734 int regulator_bulk_enable(int num_consumers,
4735 struct regulator_bulk_data *consumers)
4737 ASYNC_DOMAIN_EXCLUSIVE(async_domain);
4741 for (i = 0; i < num_consumers; i++) {
4742 async_schedule_domain(regulator_bulk_enable_async,
4743 &consumers[i], &async_domain);
4746 async_synchronize_full_domain(&async_domain);
4748 /* If any consumer failed we need to unwind any that succeeded */
4749 for (i = 0; i < num_consumers; i++) {
4750 if (consumers[i].ret != 0) {
4751 ret = consumers[i].ret;
4759 for (i = 0; i < num_consumers; i++) {
4760 if (consumers[i].ret < 0)
4761 pr_err("Failed to enable %s: %pe\n", consumers[i].supply,
4762 ERR_PTR(consumers[i].ret));
4764 regulator_disable(consumers[i].consumer);
4769 EXPORT_SYMBOL_GPL(regulator_bulk_enable);
4772 * regulator_bulk_disable - disable multiple regulator consumers
4774 * @num_consumers: Number of consumers
4775 * @consumers: Consumer data; clients are stored here.
4776 * @return 0 on success, an errno on failure
4778 * This convenience API allows consumers to disable multiple regulator
4779 * clients in a single API call. If any consumers cannot be disabled
4780 * then any others that were disabled will be enabled again prior to
4783 int regulator_bulk_disable(int num_consumers,
4784 struct regulator_bulk_data *consumers)
4789 for (i = num_consumers - 1; i >= 0; --i) {
4790 ret = regulator_disable(consumers[i].consumer);
4798 pr_err("Failed to disable %s: %pe\n", consumers[i].supply, ERR_PTR(ret));
4799 for (++i; i < num_consumers; ++i) {
4800 r = regulator_enable(consumers[i].consumer);
4802 pr_err("Failed to re-enable %s: %pe\n",
4803 consumers[i].supply, ERR_PTR(r));
4808 EXPORT_SYMBOL_GPL(regulator_bulk_disable);
4811 * regulator_bulk_force_disable - force disable multiple regulator consumers
4813 * @num_consumers: Number of consumers
4814 * @consumers: Consumer data; clients are stored here.
4815 * @return 0 on success, an errno on failure
4817 * This convenience API allows consumers to forcibly disable multiple regulator
4818 * clients in a single API call.
4819 * NOTE: This should be used for situations when device damage will
4820 * likely occur if the regulators are not disabled (e.g. over temp).
4821 * Although regulator_force_disable function call for some consumers can
4822 * return error numbers, the function is called for all consumers.
4824 int regulator_bulk_force_disable(int num_consumers,
4825 struct regulator_bulk_data *consumers)
4830 for (i = 0; i < num_consumers; i++) {
4832 regulator_force_disable(consumers[i].consumer);
4834 /* Store first error for reporting */
4835 if (consumers[i].ret && !ret)
4836 ret = consumers[i].ret;
4841 EXPORT_SYMBOL_GPL(regulator_bulk_force_disable);
4844 * regulator_bulk_free - free multiple regulator consumers
4846 * @num_consumers: Number of consumers
4847 * @consumers: Consumer data; clients are stored here.
4849 * This convenience API allows consumers to free multiple regulator
4850 * clients in a single API call.
4852 void regulator_bulk_free(int num_consumers,
4853 struct regulator_bulk_data *consumers)
4857 for (i = 0; i < num_consumers; i++) {
4858 regulator_put(consumers[i].consumer);
4859 consumers[i].consumer = NULL;
4862 EXPORT_SYMBOL_GPL(regulator_bulk_free);
4865 * regulator_notifier_call_chain - call regulator event notifier
4866 * @rdev: regulator source
4867 * @event: notifier block
4868 * @data: callback-specific data.
4870 * Called by regulator drivers to notify clients a regulator event has
4873 int regulator_notifier_call_chain(struct regulator_dev *rdev,
4874 unsigned long event, void *data)
4876 _notifier_call_chain(rdev, event, data);
4880 EXPORT_SYMBOL_GPL(regulator_notifier_call_chain);
4883 * regulator_mode_to_status - convert a regulator mode into a status
4885 * @mode: Mode to convert
4887 * Convert a regulator mode into a status.
4889 int regulator_mode_to_status(unsigned int mode)
4892 case REGULATOR_MODE_FAST:
4893 return REGULATOR_STATUS_FAST;
4894 case REGULATOR_MODE_NORMAL:
4895 return REGULATOR_STATUS_NORMAL;
4896 case REGULATOR_MODE_IDLE:
4897 return REGULATOR_STATUS_IDLE;
4898 case REGULATOR_MODE_STANDBY:
4899 return REGULATOR_STATUS_STANDBY;
4901 return REGULATOR_STATUS_UNDEFINED;
4904 EXPORT_SYMBOL_GPL(regulator_mode_to_status);
4906 static struct attribute *regulator_dev_attrs[] = {
4907 &dev_attr_name.attr,
4908 &dev_attr_num_users.attr,
4909 &dev_attr_type.attr,
4910 &dev_attr_microvolts.attr,
4911 &dev_attr_microamps.attr,
4912 &dev_attr_opmode.attr,
4913 &dev_attr_state.attr,
4914 &dev_attr_status.attr,
4915 &dev_attr_bypass.attr,
4916 &dev_attr_requested_microamps.attr,
4917 &dev_attr_min_microvolts.attr,
4918 &dev_attr_max_microvolts.attr,
4919 &dev_attr_min_microamps.attr,
4920 &dev_attr_max_microamps.attr,
4921 &dev_attr_suspend_standby_state.attr,
4922 &dev_attr_suspend_mem_state.attr,
4923 &dev_attr_suspend_disk_state.attr,
4924 &dev_attr_suspend_standby_microvolts.attr,
4925 &dev_attr_suspend_mem_microvolts.attr,
4926 &dev_attr_suspend_disk_microvolts.attr,
4927 &dev_attr_suspend_standby_mode.attr,
4928 &dev_attr_suspend_mem_mode.attr,
4929 &dev_attr_suspend_disk_mode.attr,
4934 * To avoid cluttering sysfs (and memory) with useless state, only
4935 * create attributes that can be meaningfully displayed.
4937 static umode_t regulator_attr_is_visible(struct kobject *kobj,
4938 struct attribute *attr, int idx)
4940 struct device *dev = kobj_to_dev(kobj);
4941 struct regulator_dev *rdev = dev_to_rdev(dev);
4942 const struct regulator_ops *ops = rdev->desc->ops;
4943 umode_t mode = attr->mode;
4945 /* these three are always present */
4946 if (attr == &dev_attr_name.attr ||
4947 attr == &dev_attr_num_users.attr ||
4948 attr == &dev_attr_type.attr)
4951 /* some attributes need specific methods to be displayed */
4952 if (attr == &dev_attr_microvolts.attr) {
4953 if ((ops->get_voltage && ops->get_voltage(rdev) >= 0) ||
4954 (ops->get_voltage_sel && ops->get_voltage_sel(rdev) >= 0) ||
4955 (ops->list_voltage && ops->list_voltage(rdev, 0) >= 0) ||
4956 (rdev->desc->fixed_uV && rdev->desc->n_voltages == 1))
4961 if (attr == &dev_attr_microamps.attr)
4962 return ops->get_current_limit ? mode : 0;
4964 if (attr == &dev_attr_opmode.attr)
4965 return ops->get_mode ? mode : 0;
4967 if (attr == &dev_attr_state.attr)
4968 return (rdev->ena_pin || ops->is_enabled) ? mode : 0;
4970 if (attr == &dev_attr_status.attr)
4971 return ops->get_status ? mode : 0;
4973 if (attr == &dev_attr_bypass.attr)
4974 return ops->get_bypass ? mode : 0;
4976 /* constraints need specific supporting methods */
4977 if (attr == &dev_attr_min_microvolts.attr ||
4978 attr == &dev_attr_max_microvolts.attr)
4979 return (ops->set_voltage || ops->set_voltage_sel) ? mode : 0;
4981 if (attr == &dev_attr_min_microamps.attr ||
4982 attr == &dev_attr_max_microamps.attr)
4983 return ops->set_current_limit ? mode : 0;
4985 if (attr == &dev_attr_suspend_standby_state.attr ||
4986 attr == &dev_attr_suspend_mem_state.attr ||
4987 attr == &dev_attr_suspend_disk_state.attr)
4990 if (attr == &dev_attr_suspend_standby_microvolts.attr ||
4991 attr == &dev_attr_suspend_mem_microvolts.attr ||
4992 attr == &dev_attr_suspend_disk_microvolts.attr)
4993 return ops->set_suspend_voltage ? mode : 0;
4995 if (attr == &dev_attr_suspend_standby_mode.attr ||
4996 attr == &dev_attr_suspend_mem_mode.attr ||
4997 attr == &dev_attr_suspend_disk_mode.attr)
4998 return ops->set_suspend_mode ? mode : 0;
5003 static const struct attribute_group regulator_dev_group = {
5004 .attrs = regulator_dev_attrs,
5005 .is_visible = regulator_attr_is_visible,
5008 static const struct attribute_group *regulator_dev_groups[] = {
5009 ®ulator_dev_group,
5013 static void regulator_dev_release(struct device *dev)
5015 struct regulator_dev *rdev = dev_get_drvdata(dev);
5017 debugfs_remove_recursive(rdev->debugfs);
5018 kfree(rdev->constraints);
5019 of_node_put(rdev->dev.of_node);
5023 static void rdev_init_debugfs(struct regulator_dev *rdev)
5025 struct device *parent = rdev->dev.parent;
5026 const char *rname = rdev_get_name(rdev);
5027 char name[NAME_MAX];
5029 /* Avoid duplicate debugfs directory names */
5030 if (parent && rname == rdev->desc->name) {
5031 snprintf(name, sizeof(name), "%s-%s", dev_name(parent),
5036 rdev->debugfs = debugfs_create_dir(rname, debugfs_root);
5037 if (IS_ERR(rdev->debugfs))
5038 rdev_dbg(rdev, "Failed to create debugfs directory\n");
5040 debugfs_create_u32("use_count", 0444, rdev->debugfs,
5042 debugfs_create_u32("open_count", 0444, rdev->debugfs,
5044 debugfs_create_u32("bypass_count", 0444, rdev->debugfs,
5045 &rdev->bypass_count);
5048 static int regulator_register_resolve_supply(struct device *dev, void *data)
5050 struct regulator_dev *rdev = dev_to_rdev(dev);
5052 if (regulator_resolve_supply(rdev))
5053 rdev_dbg(rdev, "unable to resolve supply\n");
5058 int regulator_coupler_register(struct regulator_coupler *coupler)
5060 mutex_lock(®ulator_list_mutex);
5061 list_add_tail(&coupler->list, ®ulator_coupler_list);
5062 mutex_unlock(®ulator_list_mutex);
5067 static struct regulator_coupler *
5068 regulator_find_coupler(struct regulator_dev *rdev)
5070 struct regulator_coupler *coupler;
5074 * Note that regulators are appended to the list and the generic
5075 * coupler is registered first, hence it will be attached at last
5078 list_for_each_entry_reverse(coupler, ®ulator_coupler_list, list) {
5079 err = coupler->attach_regulator(coupler, rdev);
5081 if (!coupler->balance_voltage &&
5082 rdev->coupling_desc.n_coupled > 2)
5083 goto err_unsupported;
5089 return ERR_PTR(err);
5097 return ERR_PTR(-EINVAL);
5100 if (coupler->detach_regulator)
5101 coupler->detach_regulator(coupler, rdev);
5104 "Voltage balancing for multiple regulator couples is unimplemented\n");
5106 return ERR_PTR(-EPERM);
5109 static void regulator_resolve_coupling(struct regulator_dev *rdev)
5111 struct regulator_coupler *coupler = rdev->coupling_desc.coupler;
5112 struct coupling_desc *c_desc = &rdev->coupling_desc;
5113 int n_coupled = c_desc->n_coupled;
5114 struct regulator_dev *c_rdev;
5117 for (i = 1; i < n_coupled; i++) {
5118 /* already resolved */
5119 if (c_desc->coupled_rdevs[i])
5122 c_rdev = of_parse_coupled_regulator(rdev, i - 1);
5127 if (c_rdev->coupling_desc.coupler != coupler) {
5128 rdev_err(rdev, "coupler mismatch with %s\n",
5129 rdev_get_name(c_rdev));
5133 c_desc->coupled_rdevs[i] = c_rdev;
5134 c_desc->n_resolved++;
5136 regulator_resolve_coupling(c_rdev);
5140 static void regulator_remove_coupling(struct regulator_dev *rdev)
5142 struct regulator_coupler *coupler = rdev->coupling_desc.coupler;
5143 struct coupling_desc *__c_desc, *c_desc = &rdev->coupling_desc;
5144 struct regulator_dev *__c_rdev, *c_rdev;
5145 unsigned int __n_coupled, n_coupled;
5149 n_coupled = c_desc->n_coupled;
5151 for (i = 1; i < n_coupled; i++) {
5152 c_rdev = c_desc->coupled_rdevs[i];
5157 regulator_lock(c_rdev);
5159 __c_desc = &c_rdev->coupling_desc;
5160 __n_coupled = __c_desc->n_coupled;
5162 for (k = 1; k < __n_coupled; k++) {
5163 __c_rdev = __c_desc->coupled_rdevs[k];
5165 if (__c_rdev == rdev) {
5166 __c_desc->coupled_rdevs[k] = NULL;
5167 __c_desc->n_resolved--;
5172 regulator_unlock(c_rdev);
5174 c_desc->coupled_rdevs[i] = NULL;
5175 c_desc->n_resolved--;
5178 if (coupler && coupler->detach_regulator) {
5179 err = coupler->detach_regulator(coupler, rdev);
5181 rdev_err(rdev, "failed to detach from coupler: %pe\n",
5185 kfree(rdev->coupling_desc.coupled_rdevs);
5186 rdev->coupling_desc.coupled_rdevs = NULL;
5189 static int regulator_init_coupling(struct regulator_dev *rdev)
5191 struct regulator_dev **coupled;
5192 int err, n_phandles;
5194 if (!IS_ENABLED(CONFIG_OF))
5197 n_phandles = of_get_n_coupled(rdev);
5199 coupled = kcalloc(n_phandles + 1, sizeof(*coupled), GFP_KERNEL);
5203 rdev->coupling_desc.coupled_rdevs = coupled;
5206 * Every regulator should always have coupling descriptor filled with
5207 * at least pointer to itself.
5209 rdev->coupling_desc.coupled_rdevs[0] = rdev;
5210 rdev->coupling_desc.n_coupled = n_phandles + 1;
5211 rdev->coupling_desc.n_resolved++;
5213 /* regulator isn't coupled */
5214 if (n_phandles == 0)
5217 if (!of_check_coupling_data(rdev))
5220 mutex_lock(®ulator_list_mutex);
5221 rdev->coupling_desc.coupler = regulator_find_coupler(rdev);
5222 mutex_unlock(®ulator_list_mutex);
5224 if (IS_ERR(rdev->coupling_desc.coupler)) {
5225 err = PTR_ERR(rdev->coupling_desc.coupler);
5226 rdev_err(rdev, "failed to get coupler: %pe\n", ERR_PTR(err));
5233 static int generic_coupler_attach(struct regulator_coupler *coupler,
5234 struct regulator_dev *rdev)
5236 if (rdev->coupling_desc.n_coupled > 2) {
5238 "Voltage balancing for multiple regulator couples is unimplemented\n");
5242 if (!rdev->constraints->always_on) {
5244 "Coupling of a non always-on regulator is unimplemented\n");
5251 static struct regulator_coupler generic_regulator_coupler = {
5252 .attach_regulator = generic_coupler_attach,
5256 * regulator_register - register regulator
5257 * @regulator_desc: regulator to register
5258 * @cfg: runtime configuration for regulator
5260 * Called by regulator drivers to register a regulator.
5261 * Returns a valid pointer to struct regulator_dev on success
5262 * or an ERR_PTR() on error.
5264 struct regulator_dev *
5265 regulator_register(const struct regulator_desc *regulator_desc,
5266 const struct regulator_config *cfg)
5268 const struct regulator_init_data *init_data;
5269 struct regulator_config *config = NULL;
5270 static atomic_t regulator_no = ATOMIC_INIT(-1);
5271 struct regulator_dev *rdev;
5272 bool dangling_cfg_gpiod = false;
5273 bool dangling_of_gpiod = false;
5278 return ERR_PTR(-EINVAL);
5280 dangling_cfg_gpiod = true;
5281 if (regulator_desc == NULL) {
5289 if (regulator_desc->name == NULL || regulator_desc->ops == NULL) {
5294 if (regulator_desc->type != REGULATOR_VOLTAGE &&
5295 regulator_desc->type != REGULATOR_CURRENT) {
5300 /* Only one of each should be implemented */
5301 WARN_ON(regulator_desc->ops->get_voltage &&
5302 regulator_desc->ops->get_voltage_sel);
5303 WARN_ON(regulator_desc->ops->set_voltage &&
5304 regulator_desc->ops->set_voltage_sel);
5306 /* If we're using selectors we must implement list_voltage. */
5307 if (regulator_desc->ops->get_voltage_sel &&
5308 !regulator_desc->ops->list_voltage) {
5312 if (regulator_desc->ops->set_voltage_sel &&
5313 !regulator_desc->ops->list_voltage) {
5318 rdev = kzalloc(sizeof(struct regulator_dev), GFP_KERNEL);
5323 device_initialize(&rdev->dev);
5326 * Duplicate the config so the driver could override it after
5327 * parsing init data.
5329 config = kmemdup(cfg, sizeof(*cfg), GFP_KERNEL);
5330 if (config == NULL) {
5335 init_data = regulator_of_get_init_data(dev, regulator_desc, config,
5336 &rdev->dev.of_node);
5339 * Sometimes not all resources are probed already so we need to take
5340 * that into account. This happens most the time if the ena_gpiod comes
5341 * from a gpio extender or something else.
5343 if (PTR_ERR(init_data) == -EPROBE_DEFER) {
5344 ret = -EPROBE_DEFER;
5349 * We need to keep track of any GPIO descriptor coming from the
5350 * device tree until we have handled it over to the core. If the
5351 * config that was passed in to this function DOES NOT contain
5352 * a descriptor, and the config after this call DOES contain
5353 * a descriptor, we definitely got one from parsing the device
5356 if (!cfg->ena_gpiod && config->ena_gpiod)
5357 dangling_of_gpiod = true;
5359 init_data = config->init_data;
5360 rdev->dev.of_node = of_node_get(config->of_node);
5363 ww_mutex_init(&rdev->mutex, ®ulator_ww_class);
5364 rdev->reg_data = config->driver_data;
5365 rdev->owner = regulator_desc->owner;
5366 rdev->desc = regulator_desc;
5368 rdev->regmap = config->regmap;
5369 else if (dev_get_regmap(dev, NULL))
5370 rdev->regmap = dev_get_regmap(dev, NULL);
5371 else if (dev->parent)
5372 rdev->regmap = dev_get_regmap(dev->parent, NULL);
5373 INIT_LIST_HEAD(&rdev->consumer_list);
5374 INIT_LIST_HEAD(&rdev->list);
5375 BLOCKING_INIT_NOTIFIER_HEAD(&rdev->notifier);
5376 INIT_DELAYED_WORK(&rdev->disable_work, regulator_disable_work);
5378 /* preform any regulator specific init */
5379 if (init_data && init_data->regulator_init) {
5380 ret = init_data->regulator_init(rdev->reg_data);
5385 if (config->ena_gpiod) {
5386 ret = regulator_ena_gpio_request(rdev, config);
5388 rdev_err(rdev, "Failed to request enable GPIO: %pe\n",
5392 /* The regulator core took over the GPIO descriptor */
5393 dangling_cfg_gpiod = false;
5394 dangling_of_gpiod = false;
5397 /* register with sysfs */
5398 rdev->dev.class = ®ulator_class;
5399 rdev->dev.parent = dev;
5400 dev_set_name(&rdev->dev, "regulator.%lu",
5401 (unsigned long) atomic_inc_return(®ulator_no));
5402 dev_set_drvdata(&rdev->dev, rdev);
5404 /* set regulator constraints */
5406 rdev->constraints = kmemdup(&init_data->constraints,
5407 sizeof(*rdev->constraints),
5410 rdev->constraints = kzalloc(sizeof(*rdev->constraints),
5412 if (!rdev->constraints) {
5417 if (init_data && init_data->supply_regulator)
5418 rdev->supply_name = init_data->supply_regulator;
5419 else if (regulator_desc->supply_name)
5420 rdev->supply_name = regulator_desc->supply_name;
5422 ret = set_machine_constraints(rdev);
5423 if (ret == -EPROBE_DEFER) {
5424 /* Regulator might be in bypass mode and so needs its supply
5425 * to set the constraints */
5426 /* FIXME: this currently triggers a chicken-and-egg problem
5427 * when creating -SUPPLY symlink in sysfs to a regulator
5428 * that is just being created */
5429 ret = regulator_resolve_supply(rdev);
5431 ret = set_machine_constraints(rdev);
5433 rdev_dbg(rdev, "unable to resolve supply early: %pe\n",
5439 ret = regulator_init_coupling(rdev);
5443 /* add consumers devices */
5445 for (i = 0; i < init_data->num_consumer_supplies; i++) {
5446 ret = set_consumer_device_supply(rdev,
5447 init_data->consumer_supplies[i].dev_name,
5448 init_data->consumer_supplies[i].supply);
5450 dev_err(dev, "Failed to set supply %s\n",
5451 init_data->consumer_supplies[i].supply);
5452 goto unset_supplies;
5457 if (!rdev->desc->ops->get_voltage &&
5458 !rdev->desc->ops->list_voltage &&
5459 !rdev->desc->fixed_uV)
5460 rdev->is_switch = true;
5462 ret = device_add(&rdev->dev);
5464 goto unset_supplies;
5466 rdev_init_debugfs(rdev);
5468 /* try to resolve regulators coupling since a new one was registered */
5469 mutex_lock(®ulator_list_mutex);
5470 regulator_resolve_coupling(rdev);
5471 mutex_unlock(®ulator_list_mutex);
5473 /* try to resolve regulators supply since a new one was registered */
5474 class_for_each_device(®ulator_class, NULL, NULL,
5475 regulator_register_resolve_supply);
5480 mutex_lock(®ulator_list_mutex);
5481 unset_regulator_supplies(rdev);
5482 regulator_remove_coupling(rdev);
5483 mutex_unlock(®ulator_list_mutex);
5485 regulator_put(rdev->supply);
5486 kfree(rdev->coupling_desc.coupled_rdevs);
5487 mutex_lock(®ulator_list_mutex);
5488 regulator_ena_gpio_free(rdev);
5489 mutex_unlock(®ulator_list_mutex);
5491 if (dangling_of_gpiod)
5492 gpiod_put(config->ena_gpiod);
5494 put_device(&rdev->dev);
5496 if (dangling_cfg_gpiod)
5497 gpiod_put(cfg->ena_gpiod);
5498 return ERR_PTR(ret);
5500 EXPORT_SYMBOL_GPL(regulator_register);
5503 * regulator_unregister - unregister regulator
5504 * @rdev: regulator to unregister
5506 * Called by regulator drivers to unregister a regulator.
5508 void regulator_unregister(struct regulator_dev *rdev)
5514 while (rdev->use_count--)
5515 regulator_disable(rdev->supply);
5516 regulator_put(rdev->supply);
5519 flush_work(&rdev->disable_work.work);
5521 mutex_lock(®ulator_list_mutex);
5523 WARN_ON(rdev->open_count);
5524 regulator_remove_coupling(rdev);
5525 unset_regulator_supplies(rdev);
5526 list_del(&rdev->list);
5527 regulator_ena_gpio_free(rdev);
5528 device_unregister(&rdev->dev);
5530 mutex_unlock(®ulator_list_mutex);
5532 EXPORT_SYMBOL_GPL(regulator_unregister);
5534 #ifdef CONFIG_SUSPEND
5536 * regulator_suspend - prepare regulators for system wide suspend
5537 * @dev: ``&struct device`` pointer that is passed to _regulator_suspend()
5539 * Configure each regulator with it's suspend operating parameters for state.
5541 static int regulator_suspend(struct device *dev)
5543 struct regulator_dev *rdev = dev_to_rdev(dev);
5544 suspend_state_t state = pm_suspend_target_state;
5546 const struct regulator_state *rstate;
5548 rstate = regulator_get_suspend_state_check(rdev, state);
5552 regulator_lock(rdev);
5553 ret = __suspend_set_state(rdev, rstate);
5554 regulator_unlock(rdev);
5559 static int regulator_resume(struct device *dev)
5561 suspend_state_t state = pm_suspend_target_state;
5562 struct regulator_dev *rdev = dev_to_rdev(dev);
5563 struct regulator_state *rstate;
5566 rstate = regulator_get_suspend_state(rdev, state);
5570 /* Avoid grabbing the lock if we don't need to */
5571 if (!rdev->desc->ops->resume)
5574 regulator_lock(rdev);
5576 if (rstate->enabled == ENABLE_IN_SUSPEND ||
5577 rstate->enabled == DISABLE_IN_SUSPEND)
5578 ret = rdev->desc->ops->resume(rdev);
5580 regulator_unlock(rdev);
5584 #else /* !CONFIG_SUSPEND */
5586 #define regulator_suspend NULL
5587 #define regulator_resume NULL
5589 #endif /* !CONFIG_SUSPEND */
5592 static const struct dev_pm_ops __maybe_unused regulator_pm_ops = {
5593 .suspend = regulator_suspend,
5594 .resume = regulator_resume,
5598 struct class regulator_class = {
5599 .name = "regulator",
5600 .dev_release = regulator_dev_release,
5601 .dev_groups = regulator_dev_groups,
5603 .pm = ®ulator_pm_ops,
5607 * regulator_has_full_constraints - the system has fully specified constraints
5609 * Calling this function will cause the regulator API to disable all
5610 * regulators which have a zero use count and don't have an always_on
5611 * constraint in a late_initcall.
5613 * The intention is that this will become the default behaviour in a
5614 * future kernel release so users are encouraged to use this facility
5617 void regulator_has_full_constraints(void)
5619 has_full_constraints = 1;
5621 EXPORT_SYMBOL_GPL(regulator_has_full_constraints);
5624 * rdev_get_drvdata - get rdev regulator driver data
5627 * Get rdev regulator driver private data. This call can be used in the
5628 * regulator driver context.
5630 void *rdev_get_drvdata(struct regulator_dev *rdev)
5632 return rdev->reg_data;
5634 EXPORT_SYMBOL_GPL(rdev_get_drvdata);
5637 * regulator_get_drvdata - get regulator driver data
5638 * @regulator: regulator
5640 * Get regulator driver private data. This call can be used in the consumer
5641 * driver context when non API regulator specific functions need to be called.
5643 void *regulator_get_drvdata(struct regulator *regulator)
5645 return regulator->rdev->reg_data;
5647 EXPORT_SYMBOL_GPL(regulator_get_drvdata);
5650 * regulator_set_drvdata - set regulator driver data
5651 * @regulator: regulator
5654 void regulator_set_drvdata(struct regulator *regulator, void *data)
5656 regulator->rdev->reg_data = data;
5658 EXPORT_SYMBOL_GPL(regulator_set_drvdata);
5661 * regulator_get_id - get regulator ID
5664 int rdev_get_id(struct regulator_dev *rdev)
5666 return rdev->desc->id;
5668 EXPORT_SYMBOL_GPL(rdev_get_id);
5670 struct device *rdev_get_dev(struct regulator_dev *rdev)
5674 EXPORT_SYMBOL_GPL(rdev_get_dev);
5676 struct regmap *rdev_get_regmap(struct regulator_dev *rdev)
5678 return rdev->regmap;
5680 EXPORT_SYMBOL_GPL(rdev_get_regmap);
5682 void *regulator_get_init_drvdata(struct regulator_init_data *reg_init_data)
5684 return reg_init_data->driver_data;
5686 EXPORT_SYMBOL_GPL(regulator_get_init_drvdata);
5688 #ifdef CONFIG_DEBUG_FS
5689 static int supply_map_show(struct seq_file *sf, void *data)
5691 struct regulator_map *map;
5693 list_for_each_entry(map, ®ulator_map_list, list) {
5694 seq_printf(sf, "%s -> %s.%s\n",
5695 rdev_get_name(map->regulator), map->dev_name,
5701 DEFINE_SHOW_ATTRIBUTE(supply_map);
5703 struct summary_data {
5705 struct regulator_dev *parent;
5709 static void regulator_summary_show_subtree(struct seq_file *s,
5710 struct regulator_dev *rdev,
5713 static int regulator_summary_show_children(struct device *dev, void *data)
5715 struct regulator_dev *rdev = dev_to_rdev(dev);
5716 struct summary_data *summary_data = data;
5718 if (rdev->supply && rdev->supply->rdev == summary_data->parent)
5719 regulator_summary_show_subtree(summary_data->s, rdev,
5720 summary_data->level + 1);
5725 static void regulator_summary_show_subtree(struct seq_file *s,
5726 struct regulator_dev *rdev,
5729 struct regulation_constraints *c;
5730 struct regulator *consumer;
5731 struct summary_data summary_data;
5732 unsigned int opmode;
5737 opmode = _regulator_get_mode_unlocked(rdev);
5738 seq_printf(s, "%*s%-*s %3d %4d %6d %7s ",
5740 30 - level * 3, rdev_get_name(rdev),
5741 rdev->use_count, rdev->open_count, rdev->bypass_count,
5742 regulator_opmode_to_str(opmode));
5744 seq_printf(s, "%5dmV ", regulator_get_voltage_rdev(rdev) / 1000);
5745 seq_printf(s, "%5dmA ",
5746 _regulator_get_current_limit_unlocked(rdev) / 1000);
5748 c = rdev->constraints;
5750 switch (rdev->desc->type) {
5751 case REGULATOR_VOLTAGE:
5752 seq_printf(s, "%5dmV %5dmV ",
5753 c->min_uV / 1000, c->max_uV / 1000);
5755 case REGULATOR_CURRENT:
5756 seq_printf(s, "%5dmA %5dmA ",
5757 c->min_uA / 1000, c->max_uA / 1000);
5764 list_for_each_entry(consumer, &rdev->consumer_list, list) {
5765 if (consumer->dev && consumer->dev->class == ®ulator_class)
5768 seq_printf(s, "%*s%-*s ",
5769 (level + 1) * 3 + 1, "",
5770 30 - (level + 1) * 3,
5771 consumer->supply_name ? consumer->supply_name :
5772 consumer->dev ? dev_name(consumer->dev) : "deviceless");
5774 switch (rdev->desc->type) {
5775 case REGULATOR_VOLTAGE:
5776 seq_printf(s, "%3d %33dmA%c%5dmV %5dmV",
5777 consumer->enable_count,
5778 consumer->uA_load / 1000,
5779 consumer->uA_load && !consumer->enable_count ?
5781 consumer->voltage[PM_SUSPEND_ON].min_uV / 1000,
5782 consumer->voltage[PM_SUSPEND_ON].max_uV / 1000);
5784 case REGULATOR_CURRENT:
5792 summary_data.level = level;
5793 summary_data.parent = rdev;
5795 class_for_each_device(®ulator_class, NULL, &summary_data,
5796 regulator_summary_show_children);
5799 struct summary_lock_data {
5800 struct ww_acquire_ctx *ww_ctx;
5801 struct regulator_dev **new_contended_rdev;
5802 struct regulator_dev **old_contended_rdev;
5805 static int regulator_summary_lock_one(struct device *dev, void *data)
5807 struct regulator_dev *rdev = dev_to_rdev(dev);
5808 struct summary_lock_data *lock_data = data;
5811 if (rdev != *lock_data->old_contended_rdev) {
5812 ret = regulator_lock_nested(rdev, lock_data->ww_ctx);
5814 if (ret == -EDEADLK)
5815 *lock_data->new_contended_rdev = rdev;
5819 *lock_data->old_contended_rdev = NULL;
5825 static int regulator_summary_unlock_one(struct device *dev, void *data)
5827 struct regulator_dev *rdev = dev_to_rdev(dev);
5828 struct summary_lock_data *lock_data = data;
5831 if (rdev == *lock_data->new_contended_rdev)
5835 regulator_unlock(rdev);
5840 static int regulator_summary_lock_all(struct ww_acquire_ctx *ww_ctx,
5841 struct regulator_dev **new_contended_rdev,
5842 struct regulator_dev **old_contended_rdev)
5844 struct summary_lock_data lock_data;
5847 lock_data.ww_ctx = ww_ctx;
5848 lock_data.new_contended_rdev = new_contended_rdev;
5849 lock_data.old_contended_rdev = old_contended_rdev;
5851 ret = class_for_each_device(®ulator_class, NULL, &lock_data,
5852 regulator_summary_lock_one);
5854 class_for_each_device(®ulator_class, NULL, &lock_data,
5855 regulator_summary_unlock_one);
5860 static void regulator_summary_lock(struct ww_acquire_ctx *ww_ctx)
5862 struct regulator_dev *new_contended_rdev = NULL;
5863 struct regulator_dev *old_contended_rdev = NULL;
5866 mutex_lock(®ulator_list_mutex);
5868 ww_acquire_init(ww_ctx, ®ulator_ww_class);
5871 if (new_contended_rdev) {
5872 ww_mutex_lock_slow(&new_contended_rdev->mutex, ww_ctx);
5873 old_contended_rdev = new_contended_rdev;
5874 old_contended_rdev->ref_cnt++;
5875 old_contended_rdev->mutex_owner = current;
5878 err = regulator_summary_lock_all(ww_ctx,
5879 &new_contended_rdev,
5880 &old_contended_rdev);
5882 if (old_contended_rdev)
5883 regulator_unlock(old_contended_rdev);
5885 } while (err == -EDEADLK);
5887 ww_acquire_done(ww_ctx);
5890 static void regulator_summary_unlock(struct ww_acquire_ctx *ww_ctx)
5892 class_for_each_device(®ulator_class, NULL, NULL,
5893 regulator_summary_unlock_one);
5894 ww_acquire_fini(ww_ctx);
5896 mutex_unlock(®ulator_list_mutex);
5899 static int regulator_summary_show_roots(struct device *dev, void *data)
5901 struct regulator_dev *rdev = dev_to_rdev(dev);
5902 struct seq_file *s = data;
5905 regulator_summary_show_subtree(s, rdev, 0);
5910 static int regulator_summary_show(struct seq_file *s, void *data)
5912 struct ww_acquire_ctx ww_ctx;
5914 seq_puts(s, " regulator use open bypass opmode voltage current min max\n");
5915 seq_puts(s, "---------------------------------------------------------------------------------------\n");
5917 regulator_summary_lock(&ww_ctx);
5919 class_for_each_device(®ulator_class, NULL, s,
5920 regulator_summary_show_roots);
5922 regulator_summary_unlock(&ww_ctx);
5926 DEFINE_SHOW_ATTRIBUTE(regulator_summary);
5927 #endif /* CONFIG_DEBUG_FS */
5929 static int __init regulator_init(void)
5933 ret = class_register(®ulator_class);
5935 debugfs_root = debugfs_create_dir("regulator", NULL);
5936 if (IS_ERR(debugfs_root))
5937 pr_debug("regulator: Failed to create debugfs directory\n");
5939 #ifdef CONFIG_DEBUG_FS
5940 debugfs_create_file("supply_map", 0444, debugfs_root, NULL,
5943 debugfs_create_file("regulator_summary", 0444, debugfs_root,
5944 NULL, ®ulator_summary_fops);
5946 regulator_dummy_init();
5948 regulator_coupler_register(&generic_regulator_coupler);
5953 /* init early to allow our consumers to complete system booting */
5954 core_initcall(regulator_init);
5956 static int regulator_late_cleanup(struct device *dev, void *data)
5958 struct regulator_dev *rdev = dev_to_rdev(dev);
5959 struct regulation_constraints *c = rdev->constraints;
5962 if (c && c->always_on)
5965 if (!regulator_ops_is_valid(rdev, REGULATOR_CHANGE_STATUS))
5968 regulator_lock(rdev);
5970 if (rdev->use_count)
5973 /* If reading the status failed, assume that it's off. */
5974 if (_regulator_is_enabled(rdev) <= 0)
5977 if (have_full_constraints()) {
5978 /* We log since this may kill the system if it goes
5980 rdev_info(rdev, "disabling\n");
5981 ret = _regulator_do_disable(rdev);
5983 rdev_err(rdev, "couldn't disable: %pe\n", ERR_PTR(ret));
5985 /* The intention is that in future we will
5986 * assume that full constraints are provided
5987 * so warn even if we aren't going to do
5990 rdev_warn(rdev, "incomplete constraints, leaving on\n");
5994 regulator_unlock(rdev);
5999 static void regulator_init_complete_work_function(struct work_struct *work)
6002 * Regulators may had failed to resolve their input supplies
6003 * when were registered, either because the input supply was
6004 * not registered yet or because its parent device was not
6005 * bound yet. So attempt to resolve the input supplies for
6006 * pending regulators before trying to disable unused ones.
6008 class_for_each_device(®ulator_class, NULL, NULL,
6009 regulator_register_resolve_supply);
6011 /* If we have a full configuration then disable any regulators
6012 * we have permission to change the status for and which are
6013 * not in use or always_on. This is effectively the default
6014 * for DT and ACPI as they have full constraints.
6016 class_for_each_device(®ulator_class, NULL, NULL,
6017 regulator_late_cleanup);
6020 static DECLARE_DELAYED_WORK(regulator_init_complete_work,
6021 regulator_init_complete_work_function);
6023 static int __init regulator_init_complete(void)
6026 * Since DT doesn't provide an idiomatic mechanism for
6027 * enabling full constraints and since it's much more natural
6028 * with DT to provide them just assume that a DT enabled
6029 * system has full constraints.
6031 if (of_have_populated_dt())
6032 has_full_constraints = true;
6035 * We punt completion for an arbitrary amount of time since
6036 * systems like distros will load many drivers from userspace
6037 * so consumers might not always be ready yet, this is
6038 * particularly an issue with laptops where this might bounce
6039 * the display off then on. Ideally we'd get a notification
6040 * from userspace when this happens but we don't so just wait
6041 * a bit and hope we waited long enough. It'd be better if
6042 * we'd only do this on systems that need it, and a kernel
6043 * command line option might be useful.
6045 schedule_delayed_work(®ulator_init_complete_work,
6046 msecs_to_jiffies(30000));
6050 late_initcall_sync(regulator_init_complete);