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));
1752 if (err != -EEXIST) {
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");
1756 regulator->debugfs = NULL;
1760 if (regulator->debugfs) {
1761 debugfs_create_u32("uA_load", 0444, regulator->debugfs,
1762 ®ulator->uA_load);
1763 debugfs_create_u32("min_uV", 0444, regulator->debugfs,
1764 ®ulator->voltage[PM_SUSPEND_ON].min_uV);
1765 debugfs_create_u32("max_uV", 0444, regulator->debugfs,
1766 ®ulator->voltage[PM_SUSPEND_ON].max_uV);
1767 debugfs_create_file("constraint_flags", 0444, regulator->debugfs,
1768 regulator, &constraint_flags_fops);
1772 * Check now if the regulator is an always on regulator - if
1773 * it is then we don't need to do nearly so much work for
1774 * enable/disable calls.
1776 if (!regulator_ops_is_valid(rdev, REGULATOR_CHANGE_STATUS) &&
1777 _regulator_is_enabled(rdev))
1778 regulator->always_on = true;
1783 static int _regulator_get_enable_time(struct regulator_dev *rdev)
1785 if (rdev->constraints && rdev->constraints->enable_time)
1786 return rdev->constraints->enable_time;
1787 if (rdev->desc->ops->enable_time)
1788 return rdev->desc->ops->enable_time(rdev);
1789 return rdev->desc->enable_time;
1792 static struct regulator_supply_alias *regulator_find_supply_alias(
1793 struct device *dev, const char *supply)
1795 struct regulator_supply_alias *map;
1797 list_for_each_entry(map, ®ulator_supply_alias_list, list)
1798 if (map->src_dev == dev && strcmp(map->src_supply, supply) == 0)
1804 static void regulator_supply_alias(struct device **dev, const char **supply)
1806 struct regulator_supply_alias *map;
1808 map = regulator_find_supply_alias(*dev, *supply);
1810 dev_dbg(*dev, "Mapping supply %s to %s,%s\n",
1811 *supply, map->alias_supply,
1812 dev_name(map->alias_dev));
1813 *dev = map->alias_dev;
1814 *supply = map->alias_supply;
1818 static int regulator_match(struct device *dev, const void *data)
1820 struct regulator_dev *r = dev_to_rdev(dev);
1822 return strcmp(rdev_get_name(r), data) == 0;
1825 static struct regulator_dev *regulator_lookup_by_name(const char *name)
1829 dev = class_find_device(®ulator_class, NULL, name, regulator_match);
1831 return dev ? dev_to_rdev(dev) : NULL;
1835 * regulator_dev_lookup - lookup a regulator device.
1836 * @dev: device for regulator "consumer".
1837 * @supply: Supply name or regulator ID.
1839 * If successful, returns a struct regulator_dev that corresponds to the name
1840 * @supply and with the embedded struct device refcount incremented by one.
1841 * The refcount must be dropped by calling put_device().
1842 * On failure one of the following ERR-PTR-encoded values is returned:
1843 * -ENODEV if lookup fails permanently, -EPROBE_DEFER if lookup could succeed
1846 static struct regulator_dev *regulator_dev_lookup(struct device *dev,
1849 struct regulator_dev *r = NULL;
1850 struct device_node *node;
1851 struct regulator_map *map;
1852 const char *devname = NULL;
1854 regulator_supply_alias(&dev, &supply);
1856 /* first do a dt based lookup */
1857 if (dev && dev->of_node) {
1858 node = of_get_regulator(dev, supply);
1860 r = of_find_regulator_by_node(node);
1866 * We have a node, but there is no device.
1867 * assume it has not registered yet.
1869 return ERR_PTR(-EPROBE_DEFER);
1873 /* if not found, try doing it non-dt way */
1875 devname = dev_name(dev);
1877 mutex_lock(®ulator_list_mutex);
1878 list_for_each_entry(map, ®ulator_map_list, list) {
1879 /* If the mapping has a device set up it must match */
1880 if (map->dev_name &&
1881 (!devname || strcmp(map->dev_name, devname)))
1884 if (strcmp(map->supply, supply) == 0 &&
1885 get_device(&map->regulator->dev)) {
1890 mutex_unlock(®ulator_list_mutex);
1895 r = regulator_lookup_by_name(supply);
1899 return ERR_PTR(-ENODEV);
1902 static int regulator_resolve_supply(struct regulator_dev *rdev)
1904 struct regulator_dev *r;
1905 struct device *dev = rdev->dev.parent;
1906 struct ww_acquire_ctx ww_ctx;
1909 /* No supply to resolve? */
1910 if (!rdev->supply_name)
1913 /* Supply already resolved? (fast-path without locking contention) */
1917 r = regulator_dev_lookup(dev, rdev->supply_name);
1921 /* Did the lookup explicitly defer for us? */
1922 if (ret == -EPROBE_DEFER)
1925 if (have_full_constraints()) {
1926 r = dummy_regulator_rdev;
1927 get_device(&r->dev);
1929 dev_err(dev, "Failed to resolve %s-supply for %s\n",
1930 rdev->supply_name, rdev->desc->name);
1931 ret = -EPROBE_DEFER;
1937 dev_err(dev, "Supply for %s (%s) resolved to itself\n",
1938 rdev->desc->name, rdev->supply_name);
1939 if (!have_full_constraints()) {
1943 r = dummy_regulator_rdev;
1944 get_device(&r->dev);
1948 * If the supply's parent device is not the same as the
1949 * regulator's parent device, then ensure the parent device
1950 * is bound before we resolve the supply, in case the parent
1951 * device get probe deferred and unregisters the supply.
1953 if (r->dev.parent && r->dev.parent != rdev->dev.parent) {
1954 if (!device_is_bound(r->dev.parent)) {
1955 put_device(&r->dev);
1956 ret = -EPROBE_DEFER;
1961 /* Recursively resolve the supply of the supply */
1962 ret = regulator_resolve_supply(r);
1964 put_device(&r->dev);
1969 * Recheck rdev->supply with rdev->mutex lock held to avoid a race
1970 * between rdev->supply null check and setting rdev->supply in
1971 * set_supply() from concurrent tasks.
1973 regulator_lock_two(rdev, r, &ww_ctx);
1975 /* Supply just resolved by a concurrent task? */
1977 regulator_unlock_two(rdev, r, &ww_ctx);
1978 put_device(&r->dev);
1982 ret = set_supply(rdev, r);
1984 regulator_unlock_two(rdev, r, &ww_ctx);
1985 put_device(&r->dev);
1989 regulator_unlock_two(rdev, r, &ww_ctx);
1992 * In set_machine_constraints() we may have turned this regulator on
1993 * but we couldn't propagate to the supply if it hadn't been resolved
1996 if (rdev->use_count) {
1997 ret = regulator_enable(rdev->supply);
1999 _regulator_put(rdev->supply);
2000 rdev->supply = NULL;
2009 /* Internal regulator request function */
2010 struct regulator *_regulator_get(struct device *dev, const char *id,
2011 enum regulator_get_type get_type)
2013 struct regulator_dev *rdev;
2014 struct regulator *regulator;
2015 struct device_link *link;
2018 if (get_type >= MAX_GET_TYPE) {
2019 dev_err(dev, "invalid type %d in %s\n", get_type, __func__);
2020 return ERR_PTR(-EINVAL);
2024 pr_err("get() with no identifier\n");
2025 return ERR_PTR(-EINVAL);
2028 rdev = regulator_dev_lookup(dev, id);
2030 ret = PTR_ERR(rdev);
2033 * If regulator_dev_lookup() fails with error other
2034 * than -ENODEV our job here is done, we simply return it.
2037 return ERR_PTR(ret);
2039 if (!have_full_constraints()) {
2041 "incomplete constraints, dummy supplies not allowed\n");
2042 return ERR_PTR(-ENODEV);
2048 * Assume that a regulator is physically present and
2049 * enabled, even if it isn't hooked up, and just
2052 dev_warn(dev, "supply %s not found, using dummy regulator\n", id);
2053 rdev = dummy_regulator_rdev;
2054 get_device(&rdev->dev);
2059 "dummy supplies not allowed for exclusive requests\n");
2063 return ERR_PTR(-ENODEV);
2067 if (rdev->exclusive) {
2068 regulator = ERR_PTR(-EPERM);
2069 put_device(&rdev->dev);
2073 if (get_type == EXCLUSIVE_GET && rdev->open_count) {
2074 regulator = ERR_PTR(-EBUSY);
2075 put_device(&rdev->dev);
2079 mutex_lock(®ulator_list_mutex);
2080 ret = (rdev->coupling_desc.n_resolved != rdev->coupling_desc.n_coupled);
2081 mutex_unlock(®ulator_list_mutex);
2084 regulator = ERR_PTR(-EPROBE_DEFER);
2085 put_device(&rdev->dev);
2089 ret = regulator_resolve_supply(rdev);
2091 regulator = ERR_PTR(ret);
2092 put_device(&rdev->dev);
2096 if (!try_module_get(rdev->owner)) {
2097 regulator = ERR_PTR(-EPROBE_DEFER);
2098 put_device(&rdev->dev);
2102 regulator_lock(rdev);
2103 regulator = create_regulator(rdev, dev, id);
2104 regulator_unlock(rdev);
2105 if (regulator == NULL) {
2106 regulator = ERR_PTR(-ENOMEM);
2107 module_put(rdev->owner);
2108 put_device(&rdev->dev);
2113 if (get_type == EXCLUSIVE_GET) {
2114 rdev->exclusive = 1;
2116 ret = _regulator_is_enabled(rdev);
2118 rdev->use_count = 1;
2119 regulator->enable_count = 1;
2121 rdev->use_count = 0;
2122 regulator->enable_count = 0;
2126 link = device_link_add(dev, &rdev->dev, DL_FLAG_STATELESS);
2127 if (!IS_ERR_OR_NULL(link))
2128 regulator->device_link = true;
2134 * regulator_get - lookup and obtain a reference to a regulator.
2135 * @dev: device for regulator "consumer"
2136 * @id: Supply name or regulator ID.
2138 * Returns a struct regulator corresponding to the regulator producer,
2139 * or IS_ERR() condition containing errno.
2141 * Use of supply names configured via regulator_set_device_supply() is
2142 * strongly encouraged. It is recommended that the supply name used
2143 * should match the name used for the supply and/or the relevant
2144 * device pins in the datasheet.
2146 struct regulator *regulator_get(struct device *dev, const char *id)
2148 return _regulator_get(dev, id, NORMAL_GET);
2150 EXPORT_SYMBOL_GPL(regulator_get);
2153 * regulator_get_exclusive - obtain exclusive access to a regulator.
2154 * @dev: device for regulator "consumer"
2155 * @id: Supply name or regulator ID.
2157 * Returns a struct regulator corresponding to the regulator producer,
2158 * or IS_ERR() condition containing errno. Other consumers will be
2159 * unable to obtain this regulator while this reference is held and the
2160 * use count for the regulator will be initialised to reflect the current
2161 * state of the regulator.
2163 * This is intended for use by consumers which cannot tolerate shared
2164 * use of the regulator such as those which need to force the
2165 * regulator off for correct operation of the hardware they are
2168 * Use of supply names configured via regulator_set_device_supply() is
2169 * strongly encouraged. It is recommended that the supply name used
2170 * should match the name used for the supply and/or the relevant
2171 * device pins in the datasheet.
2173 struct regulator *regulator_get_exclusive(struct device *dev, const char *id)
2175 return _regulator_get(dev, id, EXCLUSIVE_GET);
2177 EXPORT_SYMBOL_GPL(regulator_get_exclusive);
2180 * regulator_get_optional - obtain optional access to a regulator.
2181 * @dev: device for regulator "consumer"
2182 * @id: Supply name or regulator ID.
2184 * Returns a struct regulator corresponding to the regulator producer,
2185 * or IS_ERR() condition containing errno.
2187 * This is intended for use by consumers for devices which can have
2188 * some supplies unconnected in normal use, such as some MMC devices.
2189 * It can allow the regulator core to provide stub supplies for other
2190 * supplies requested using normal regulator_get() calls without
2191 * disrupting the operation of drivers that can handle absent
2194 * Use of supply names configured via regulator_set_device_supply() is
2195 * strongly encouraged. It is recommended that the supply name used
2196 * should match the name used for the supply and/or the relevant
2197 * device pins in the datasheet.
2199 struct regulator *regulator_get_optional(struct device *dev, const char *id)
2201 return _regulator_get(dev, id, OPTIONAL_GET);
2203 EXPORT_SYMBOL_GPL(regulator_get_optional);
2205 static void destroy_regulator(struct regulator *regulator)
2207 struct regulator_dev *rdev = regulator->rdev;
2209 debugfs_remove_recursive(regulator->debugfs);
2211 if (regulator->dev) {
2212 if (regulator->device_link)
2213 device_link_remove(regulator->dev, &rdev->dev);
2215 /* remove any sysfs entries */
2216 sysfs_remove_link(&rdev->dev.kobj, regulator->supply_name);
2219 regulator_lock(rdev);
2220 list_del(®ulator->list);
2223 rdev->exclusive = 0;
2224 regulator_unlock(rdev);
2226 kfree_const(regulator->supply_name);
2230 /* regulator_list_mutex lock held by regulator_put() */
2231 static void _regulator_put(struct regulator *regulator)
2233 struct regulator_dev *rdev;
2235 if (IS_ERR_OR_NULL(regulator))
2238 lockdep_assert_held_once(®ulator_list_mutex);
2240 /* Docs say you must disable before calling regulator_put() */
2241 WARN_ON(regulator->enable_count);
2243 rdev = regulator->rdev;
2245 destroy_regulator(regulator);
2247 module_put(rdev->owner);
2248 put_device(&rdev->dev);
2252 * regulator_put - "free" the regulator source
2253 * @regulator: regulator source
2255 * Note: drivers must ensure that all regulator_enable calls made on this
2256 * regulator source are balanced by regulator_disable calls prior to calling
2259 void regulator_put(struct regulator *regulator)
2261 mutex_lock(®ulator_list_mutex);
2262 _regulator_put(regulator);
2263 mutex_unlock(®ulator_list_mutex);
2265 EXPORT_SYMBOL_GPL(regulator_put);
2268 * regulator_register_supply_alias - Provide device alias for supply lookup
2270 * @dev: device that will be given as the regulator "consumer"
2271 * @id: Supply name or regulator ID
2272 * @alias_dev: device that should be used to lookup the supply
2273 * @alias_id: Supply name or regulator ID that should be used to lookup the
2276 * All lookups for id on dev will instead be conducted for alias_id on
2279 int regulator_register_supply_alias(struct device *dev, const char *id,
2280 struct device *alias_dev,
2281 const char *alias_id)
2283 struct regulator_supply_alias *map;
2285 map = regulator_find_supply_alias(dev, id);
2289 map = kzalloc(sizeof(struct regulator_supply_alias), GFP_KERNEL);
2294 map->src_supply = id;
2295 map->alias_dev = alias_dev;
2296 map->alias_supply = alias_id;
2298 list_add(&map->list, ®ulator_supply_alias_list);
2300 pr_info("Adding alias for supply %s,%s -> %s,%s\n",
2301 id, dev_name(dev), alias_id, dev_name(alias_dev));
2305 EXPORT_SYMBOL_GPL(regulator_register_supply_alias);
2308 * regulator_unregister_supply_alias - Remove device alias
2310 * @dev: device that will be given as the regulator "consumer"
2311 * @id: Supply name or regulator ID
2313 * Remove a lookup alias if one exists for id on dev.
2315 void regulator_unregister_supply_alias(struct device *dev, const char *id)
2317 struct regulator_supply_alias *map;
2319 map = regulator_find_supply_alias(dev, id);
2321 list_del(&map->list);
2325 EXPORT_SYMBOL_GPL(regulator_unregister_supply_alias);
2328 * regulator_bulk_register_supply_alias - register multiple aliases
2330 * @dev: device that will be given as the regulator "consumer"
2331 * @id: List of supply names or regulator IDs
2332 * @alias_dev: device that should be used to lookup the supply
2333 * @alias_id: List of supply names or regulator IDs that should be used to
2335 * @num_id: Number of aliases to register
2337 * @return 0 on success, an errno on failure.
2339 * This helper function allows drivers to register several supply
2340 * aliases in one operation. If any of the aliases cannot be
2341 * registered any aliases that were registered will be removed
2342 * before returning to the caller.
2344 int regulator_bulk_register_supply_alias(struct device *dev,
2345 const char *const *id,
2346 struct device *alias_dev,
2347 const char *const *alias_id,
2353 for (i = 0; i < num_id; ++i) {
2354 ret = regulator_register_supply_alias(dev, id[i], alias_dev,
2364 "Failed to create supply alias %s,%s -> %s,%s\n",
2365 id[i], dev_name(dev), alias_id[i], dev_name(alias_dev));
2368 regulator_unregister_supply_alias(dev, id[i]);
2372 EXPORT_SYMBOL_GPL(regulator_bulk_register_supply_alias);
2375 * regulator_bulk_unregister_supply_alias - unregister multiple aliases
2377 * @dev: device that will be given as the regulator "consumer"
2378 * @id: List of supply names or regulator IDs
2379 * @num_id: Number of aliases to unregister
2381 * This helper function allows drivers to unregister several supply
2382 * aliases in one operation.
2384 void regulator_bulk_unregister_supply_alias(struct device *dev,
2385 const char *const *id,
2390 for (i = 0; i < num_id; ++i)
2391 regulator_unregister_supply_alias(dev, id[i]);
2393 EXPORT_SYMBOL_GPL(regulator_bulk_unregister_supply_alias);
2396 /* Manage enable GPIO list. Same GPIO pin can be shared among regulators */
2397 static int regulator_ena_gpio_request(struct regulator_dev *rdev,
2398 const struct regulator_config *config)
2400 struct regulator_enable_gpio *pin, *new_pin;
2401 struct gpio_desc *gpiod;
2403 gpiod = config->ena_gpiod;
2404 new_pin = kzalloc(sizeof(*new_pin), GFP_KERNEL);
2406 mutex_lock(®ulator_list_mutex);
2408 list_for_each_entry(pin, ®ulator_ena_gpio_list, list) {
2409 if (pin->gpiod == gpiod) {
2410 rdev_dbg(rdev, "GPIO is already used\n");
2411 goto update_ena_gpio_to_rdev;
2415 if (new_pin == NULL) {
2416 mutex_unlock(®ulator_list_mutex);
2424 list_add(&pin->list, ®ulator_ena_gpio_list);
2426 update_ena_gpio_to_rdev:
2427 pin->request_count++;
2428 rdev->ena_pin = pin;
2430 mutex_unlock(®ulator_list_mutex);
2436 static void regulator_ena_gpio_free(struct regulator_dev *rdev)
2438 struct regulator_enable_gpio *pin, *n;
2443 /* Free the GPIO only in case of no use */
2444 list_for_each_entry_safe(pin, n, ®ulator_ena_gpio_list, list) {
2445 if (pin != rdev->ena_pin)
2448 if (--pin->request_count)
2451 gpiod_put(pin->gpiod);
2452 list_del(&pin->list);
2457 rdev->ena_pin = NULL;
2461 * regulator_ena_gpio_ctrl - balance enable_count of each GPIO and actual GPIO pin control
2462 * @rdev: regulator_dev structure
2463 * @enable: enable GPIO at initial use?
2465 * GPIO is enabled in case of initial use. (enable_count is 0)
2466 * GPIO is disabled when it is not shared any more. (enable_count <= 1)
2468 static int regulator_ena_gpio_ctrl(struct regulator_dev *rdev, bool enable)
2470 struct regulator_enable_gpio *pin = rdev->ena_pin;
2476 /* Enable GPIO at initial use */
2477 if (pin->enable_count == 0)
2478 gpiod_set_value_cansleep(pin->gpiod, 1);
2480 pin->enable_count++;
2482 if (pin->enable_count > 1) {
2483 pin->enable_count--;
2487 /* Disable GPIO if not used */
2488 if (pin->enable_count <= 1) {
2489 gpiod_set_value_cansleep(pin->gpiod, 0);
2490 pin->enable_count = 0;
2498 * _regulator_enable_delay - a delay helper function
2499 * @delay: time to delay in microseconds
2501 * Delay for the requested amount of time as per the guidelines in:
2503 * Documentation/timers/timers-howto.rst
2505 * The assumption here is that regulators will never be enabled in
2506 * atomic context and therefore sleeping functions can be used.
2508 static void _regulator_enable_delay(unsigned int delay)
2510 unsigned int ms = delay / 1000;
2511 unsigned int us = delay % 1000;
2515 * For small enough values, handle super-millisecond
2516 * delays in the usleep_range() call below.
2525 * Give the scheduler some room to coalesce with any other
2526 * wakeup sources. For delays shorter than 10 us, don't even
2527 * bother setting up high-resolution timers and just busy-
2531 usleep_range(us, us + 100);
2537 * _regulator_check_status_enabled
2539 * A helper function to check if the regulator status can be interpreted
2540 * as 'regulator is enabled'.
2541 * @rdev: the regulator device to check
2544 * * 1 - if status shows regulator is in enabled state
2545 * * 0 - if not enabled state
2546 * * Error Value - as received from ops->get_status()
2548 static inline int _regulator_check_status_enabled(struct regulator_dev *rdev)
2550 int ret = rdev->desc->ops->get_status(rdev);
2553 rdev_info(rdev, "get_status returned error: %d\n", ret);
2558 case REGULATOR_STATUS_OFF:
2559 case REGULATOR_STATUS_ERROR:
2560 case REGULATOR_STATUS_UNDEFINED:
2567 static int _regulator_do_enable(struct regulator_dev *rdev)
2571 /* Query before enabling in case configuration dependent. */
2572 ret = _regulator_get_enable_time(rdev);
2576 rdev_warn(rdev, "enable_time() failed: %pe\n", ERR_PTR(ret));
2580 trace_regulator_enable(rdev_get_name(rdev));
2582 if (rdev->desc->off_on_delay) {
2583 /* if needed, keep a distance of off_on_delay from last time
2584 * this regulator was disabled.
2586 unsigned long start_jiffy = jiffies;
2587 unsigned long intended, max_delay, remaining;
2589 max_delay = usecs_to_jiffies(rdev->desc->off_on_delay);
2590 intended = rdev->last_off_jiffy + max_delay;
2592 if (time_before(start_jiffy, intended)) {
2593 /* calc remaining jiffies to deal with one-time
2595 * in case of multiple timer wrapping, either it can be
2596 * detected by out-of-range remaining, or it cannot be
2597 * detected and we get a penalty of
2598 * _regulator_enable_delay().
2600 remaining = intended - start_jiffy;
2601 if (remaining <= max_delay)
2602 _regulator_enable_delay(
2603 jiffies_to_usecs(remaining));
2607 if (rdev->ena_pin) {
2608 if (!rdev->ena_gpio_state) {
2609 ret = regulator_ena_gpio_ctrl(rdev, true);
2612 rdev->ena_gpio_state = 1;
2614 } else if (rdev->desc->ops->enable) {
2615 ret = rdev->desc->ops->enable(rdev);
2622 /* Allow the regulator to ramp; it would be useful to extend
2623 * this for bulk operations so that the regulators can ramp
2625 trace_regulator_enable_delay(rdev_get_name(rdev));
2627 /* If poll_enabled_time is set, poll upto the delay calculated
2628 * above, delaying poll_enabled_time uS to check if the regulator
2629 * actually got enabled.
2630 * If the regulator isn't enabled after enable_delay has
2631 * expired, return -ETIMEDOUT.
2633 if (rdev->desc->poll_enabled_time) {
2634 int time_remaining = delay;
2636 while (time_remaining > 0) {
2637 _regulator_enable_delay(rdev->desc->poll_enabled_time);
2639 if (rdev->desc->ops->get_status) {
2640 ret = _regulator_check_status_enabled(rdev);
2645 } else if (rdev->desc->ops->is_enabled(rdev))
2648 time_remaining -= rdev->desc->poll_enabled_time;
2651 if (time_remaining <= 0) {
2652 rdev_err(rdev, "Enabled check timed out\n");
2656 _regulator_enable_delay(delay);
2659 trace_regulator_enable_complete(rdev_get_name(rdev));
2665 * _regulator_handle_consumer_enable - handle that a consumer enabled
2666 * @regulator: regulator source
2668 * Some things on a regulator consumer (like the contribution towards total
2669 * load on the regulator) only have an effect when the consumer wants the
2670 * regulator enabled. Explained in example with two consumers of the same
2672 * consumer A: set_load(100); => total load = 0
2673 * consumer A: regulator_enable(); => total load = 100
2674 * consumer B: set_load(1000); => total load = 100
2675 * consumer B: regulator_enable(); => total load = 1100
2676 * consumer A: regulator_disable(); => total_load = 1000
2678 * This function (together with _regulator_handle_consumer_disable) is
2679 * responsible for keeping track of the refcount for a given regulator consumer
2680 * and applying / unapplying these things.
2682 * Returns 0 upon no error; -error upon error.
2684 static int _regulator_handle_consumer_enable(struct regulator *regulator)
2687 struct regulator_dev *rdev = regulator->rdev;
2689 lockdep_assert_held_once(&rdev->mutex.base);
2691 regulator->enable_count++;
2692 if (regulator->uA_load && regulator->enable_count == 1) {
2693 ret = drms_uA_update(rdev);
2695 regulator->enable_count--;
2703 * _regulator_handle_consumer_disable - handle that a consumer disabled
2704 * @regulator: regulator source
2706 * The opposite of _regulator_handle_consumer_enable().
2708 * Returns 0 upon no error; -error upon error.
2710 static int _regulator_handle_consumer_disable(struct regulator *regulator)
2712 struct regulator_dev *rdev = regulator->rdev;
2714 lockdep_assert_held_once(&rdev->mutex.base);
2716 if (!regulator->enable_count) {
2717 rdev_err(rdev, "Underflow of regulator enable count\n");
2721 regulator->enable_count--;
2722 if (regulator->uA_load && regulator->enable_count == 0)
2723 return drms_uA_update(rdev);
2728 /* locks held by regulator_enable() */
2729 static int _regulator_enable(struct regulator *regulator)
2731 struct regulator_dev *rdev = regulator->rdev;
2734 lockdep_assert_held_once(&rdev->mutex.base);
2736 if (rdev->use_count == 0 && rdev->supply) {
2737 ret = _regulator_enable(rdev->supply);
2742 /* balance only if there are regulators coupled */
2743 if (rdev->coupling_desc.n_coupled > 1) {
2744 ret = regulator_balance_voltage(rdev, PM_SUSPEND_ON);
2746 goto err_disable_supply;
2749 ret = _regulator_handle_consumer_enable(regulator);
2751 goto err_disable_supply;
2753 if (rdev->use_count == 0) {
2754 /* The regulator may on if it's not switchable or left on */
2755 ret = _regulator_is_enabled(rdev);
2756 if (ret == -EINVAL || ret == 0) {
2757 if (!regulator_ops_is_valid(rdev,
2758 REGULATOR_CHANGE_STATUS)) {
2760 goto err_consumer_disable;
2763 ret = _regulator_do_enable(rdev);
2765 goto err_consumer_disable;
2767 _notifier_call_chain(rdev, REGULATOR_EVENT_ENABLE,
2769 } else if (ret < 0) {
2770 rdev_err(rdev, "is_enabled() failed: %pe\n", ERR_PTR(ret));
2771 goto err_consumer_disable;
2773 /* Fallthrough on positive return values - already enabled */
2776 if (regulator->enable_count == 1)
2781 err_consumer_disable:
2782 _regulator_handle_consumer_disable(regulator);
2785 if (rdev->use_count == 0 && rdev->supply)
2786 _regulator_disable(rdev->supply);
2792 * regulator_enable - enable regulator output
2793 * @regulator: regulator source
2795 * Request that the regulator be enabled with the regulator output at
2796 * the predefined voltage or current value. Calls to regulator_enable()
2797 * must be balanced with calls to regulator_disable().
2799 * NOTE: the output value can be set by other drivers, boot loader or may be
2800 * hardwired in the regulator.
2802 int regulator_enable(struct regulator *regulator)
2804 struct regulator_dev *rdev = regulator->rdev;
2805 struct ww_acquire_ctx ww_ctx;
2808 regulator_lock_dependent(rdev, &ww_ctx);
2809 ret = _regulator_enable(regulator);
2810 regulator_unlock_dependent(rdev, &ww_ctx);
2814 EXPORT_SYMBOL_GPL(regulator_enable);
2816 static int _regulator_do_disable(struct regulator_dev *rdev)
2820 trace_regulator_disable(rdev_get_name(rdev));
2822 if (rdev->ena_pin) {
2823 if (rdev->ena_gpio_state) {
2824 ret = regulator_ena_gpio_ctrl(rdev, false);
2827 rdev->ena_gpio_state = 0;
2830 } else if (rdev->desc->ops->disable) {
2831 ret = rdev->desc->ops->disable(rdev);
2836 /* cares about last_off_jiffy only if off_on_delay is required by
2839 if (rdev->desc->off_on_delay)
2840 rdev->last_off_jiffy = jiffies;
2842 trace_regulator_disable_complete(rdev_get_name(rdev));
2847 /* locks held by regulator_disable() */
2848 static int _regulator_disable(struct regulator *regulator)
2850 struct regulator_dev *rdev = regulator->rdev;
2853 lockdep_assert_held_once(&rdev->mutex.base);
2855 if (WARN(regulator->enable_count == 0,
2856 "unbalanced disables for %s\n", rdev_get_name(rdev)))
2859 if (regulator->enable_count == 1) {
2860 /* disabling last enable_count from this regulator */
2861 /* are we the last user and permitted to disable ? */
2862 if (rdev->use_count == 1 &&
2863 (rdev->constraints && !rdev->constraints->always_on)) {
2865 /* we are last user */
2866 if (regulator_ops_is_valid(rdev, REGULATOR_CHANGE_STATUS)) {
2867 ret = _notifier_call_chain(rdev,
2868 REGULATOR_EVENT_PRE_DISABLE,
2870 if (ret & NOTIFY_STOP_MASK)
2873 ret = _regulator_do_disable(rdev);
2875 rdev_err(rdev, "failed to disable: %pe\n", ERR_PTR(ret));
2876 _notifier_call_chain(rdev,
2877 REGULATOR_EVENT_ABORT_DISABLE,
2881 _notifier_call_chain(rdev, REGULATOR_EVENT_DISABLE,
2885 rdev->use_count = 0;
2886 } else if (rdev->use_count > 1) {
2892 ret = _regulator_handle_consumer_disable(regulator);
2894 if (ret == 0 && rdev->coupling_desc.n_coupled > 1)
2895 ret = regulator_balance_voltage(rdev, PM_SUSPEND_ON);
2897 if (ret == 0 && rdev->use_count == 0 && rdev->supply)
2898 ret = _regulator_disable(rdev->supply);
2904 * regulator_disable - disable regulator output
2905 * @regulator: regulator source
2907 * Disable the regulator output voltage or current. Calls to
2908 * regulator_enable() must be balanced with calls to
2909 * regulator_disable().
2911 * NOTE: this will only disable the regulator output if no other consumer
2912 * devices have it enabled, the regulator device supports disabling and
2913 * machine constraints permit this operation.
2915 int regulator_disable(struct regulator *regulator)
2917 struct regulator_dev *rdev = regulator->rdev;
2918 struct ww_acquire_ctx ww_ctx;
2921 regulator_lock_dependent(rdev, &ww_ctx);
2922 ret = _regulator_disable(regulator);
2923 regulator_unlock_dependent(rdev, &ww_ctx);
2927 EXPORT_SYMBOL_GPL(regulator_disable);
2929 /* locks held by regulator_force_disable() */
2930 static int _regulator_force_disable(struct regulator_dev *rdev)
2934 lockdep_assert_held_once(&rdev->mutex.base);
2936 ret = _notifier_call_chain(rdev, REGULATOR_EVENT_FORCE_DISABLE |
2937 REGULATOR_EVENT_PRE_DISABLE, NULL);
2938 if (ret & NOTIFY_STOP_MASK)
2941 ret = _regulator_do_disable(rdev);
2943 rdev_err(rdev, "failed to force disable: %pe\n", ERR_PTR(ret));
2944 _notifier_call_chain(rdev, REGULATOR_EVENT_FORCE_DISABLE |
2945 REGULATOR_EVENT_ABORT_DISABLE, NULL);
2949 _notifier_call_chain(rdev, REGULATOR_EVENT_FORCE_DISABLE |
2950 REGULATOR_EVENT_DISABLE, NULL);
2956 * regulator_force_disable - force disable regulator output
2957 * @regulator: regulator source
2959 * Forcibly disable the regulator output voltage or current.
2960 * NOTE: this *will* disable the regulator output even if other consumer
2961 * devices have it enabled. This should be used for situations when device
2962 * damage will likely occur if the regulator is not disabled (e.g. over temp).
2964 int regulator_force_disable(struct regulator *regulator)
2966 struct regulator_dev *rdev = regulator->rdev;
2967 struct ww_acquire_ctx ww_ctx;
2970 regulator_lock_dependent(rdev, &ww_ctx);
2972 ret = _regulator_force_disable(regulator->rdev);
2974 if (rdev->coupling_desc.n_coupled > 1)
2975 regulator_balance_voltage(rdev, PM_SUSPEND_ON);
2977 if (regulator->uA_load) {
2978 regulator->uA_load = 0;
2979 ret = drms_uA_update(rdev);
2982 if (rdev->use_count != 0 && rdev->supply)
2983 _regulator_disable(rdev->supply);
2985 regulator_unlock_dependent(rdev, &ww_ctx);
2989 EXPORT_SYMBOL_GPL(regulator_force_disable);
2991 static void regulator_disable_work(struct work_struct *work)
2993 struct regulator_dev *rdev = container_of(work, struct regulator_dev,
2995 struct ww_acquire_ctx ww_ctx;
2997 struct regulator *regulator;
2998 int total_count = 0;
3000 regulator_lock_dependent(rdev, &ww_ctx);
3003 * Workqueue functions queue the new work instance while the previous
3004 * work instance is being processed. Cancel the queued work instance
3005 * as the work instance under processing does the job of the queued
3008 cancel_delayed_work(&rdev->disable_work);
3010 list_for_each_entry(regulator, &rdev->consumer_list, list) {
3011 count = regulator->deferred_disables;
3016 total_count += count;
3017 regulator->deferred_disables = 0;
3019 for (i = 0; i < count; i++) {
3020 ret = _regulator_disable(regulator);
3022 rdev_err(rdev, "Deferred disable failed: %pe\n",
3026 WARN_ON(!total_count);
3028 if (rdev->coupling_desc.n_coupled > 1)
3029 regulator_balance_voltage(rdev, PM_SUSPEND_ON);
3031 regulator_unlock_dependent(rdev, &ww_ctx);
3035 * regulator_disable_deferred - disable regulator output with delay
3036 * @regulator: regulator source
3037 * @ms: milliseconds until the regulator is disabled
3039 * Execute regulator_disable() on the regulator after a delay. This
3040 * is intended for use with devices that require some time to quiesce.
3042 * NOTE: this will only disable the regulator output if no other consumer
3043 * devices have it enabled, the regulator device supports disabling and
3044 * machine constraints permit this operation.
3046 int regulator_disable_deferred(struct regulator *regulator, int ms)
3048 struct regulator_dev *rdev = regulator->rdev;
3051 return regulator_disable(regulator);
3053 regulator_lock(rdev);
3054 regulator->deferred_disables++;
3055 mod_delayed_work(system_power_efficient_wq, &rdev->disable_work,
3056 msecs_to_jiffies(ms));
3057 regulator_unlock(rdev);
3061 EXPORT_SYMBOL_GPL(regulator_disable_deferred);
3063 static int _regulator_is_enabled(struct regulator_dev *rdev)
3065 /* A GPIO control always takes precedence */
3067 return rdev->ena_gpio_state;
3069 /* If we don't know then assume that the regulator is always on */
3070 if (!rdev->desc->ops->is_enabled)
3073 return rdev->desc->ops->is_enabled(rdev);
3076 static int _regulator_list_voltage(struct regulator_dev *rdev,
3077 unsigned selector, int lock)
3079 const struct regulator_ops *ops = rdev->desc->ops;
3082 if (rdev->desc->fixed_uV && rdev->desc->n_voltages == 1 && !selector)
3083 return rdev->desc->fixed_uV;
3085 if (ops->list_voltage) {
3086 if (selector >= rdev->desc->n_voltages)
3089 regulator_lock(rdev);
3090 ret = ops->list_voltage(rdev, selector);
3092 regulator_unlock(rdev);
3093 } else if (rdev->is_switch && rdev->supply) {
3094 ret = _regulator_list_voltage(rdev->supply->rdev,
3101 if (ret < rdev->constraints->min_uV)
3103 else if (ret > rdev->constraints->max_uV)
3111 * regulator_is_enabled - is the regulator output enabled
3112 * @regulator: regulator source
3114 * Returns positive if the regulator driver backing the source/client
3115 * has requested that the device be enabled, zero if it hasn't, else a
3116 * negative errno code.
3118 * Note that the device backing this regulator handle can have multiple
3119 * users, so it might be enabled even if regulator_enable() was never
3120 * called for this particular source.
3122 int regulator_is_enabled(struct regulator *regulator)
3126 if (regulator->always_on)
3129 regulator_lock(regulator->rdev);
3130 ret = _regulator_is_enabled(regulator->rdev);
3131 regulator_unlock(regulator->rdev);
3135 EXPORT_SYMBOL_GPL(regulator_is_enabled);
3138 * regulator_count_voltages - count regulator_list_voltage() selectors
3139 * @regulator: regulator source
3141 * Returns number of selectors, or negative errno. Selectors are
3142 * numbered starting at zero, and typically correspond to bitfields
3143 * in hardware registers.
3145 int regulator_count_voltages(struct regulator *regulator)
3147 struct regulator_dev *rdev = regulator->rdev;
3149 if (rdev->desc->n_voltages)
3150 return rdev->desc->n_voltages;
3152 if (!rdev->is_switch || !rdev->supply)
3155 return regulator_count_voltages(rdev->supply);
3157 EXPORT_SYMBOL_GPL(regulator_count_voltages);
3160 * regulator_list_voltage - enumerate supported voltages
3161 * @regulator: regulator source
3162 * @selector: identify voltage to list
3163 * Context: can sleep
3165 * Returns a voltage that can be passed to @regulator_set_voltage(),
3166 * zero if this selector code can't be used on this system, or a
3169 int regulator_list_voltage(struct regulator *regulator, unsigned selector)
3171 return _regulator_list_voltage(regulator->rdev, selector, 1);
3173 EXPORT_SYMBOL_GPL(regulator_list_voltage);
3176 * regulator_get_regmap - get the regulator's register map
3177 * @regulator: regulator source
3179 * Returns the register map for the given regulator, or an ERR_PTR value
3180 * if the regulator doesn't use regmap.
3182 struct regmap *regulator_get_regmap(struct regulator *regulator)
3184 struct regmap *map = regulator->rdev->regmap;
3186 return map ? map : ERR_PTR(-EOPNOTSUPP);
3190 * regulator_get_hardware_vsel_register - get the HW voltage selector register
3191 * @regulator: regulator source
3192 * @vsel_reg: voltage selector register, output parameter
3193 * @vsel_mask: mask for voltage selector bitfield, output parameter
3195 * Returns the hardware register offset and bitmask used for setting the
3196 * regulator voltage. This might be useful when configuring voltage-scaling
3197 * hardware or firmware that can make I2C requests behind the kernel's back,
3200 * On success, the output parameters @vsel_reg and @vsel_mask are filled in
3201 * and 0 is returned, otherwise a negative errno is returned.
3203 int regulator_get_hardware_vsel_register(struct regulator *regulator,
3205 unsigned *vsel_mask)
3207 struct regulator_dev *rdev = regulator->rdev;
3208 const struct regulator_ops *ops = rdev->desc->ops;
3210 if (ops->set_voltage_sel != regulator_set_voltage_sel_regmap)
3213 *vsel_reg = rdev->desc->vsel_reg;
3214 *vsel_mask = rdev->desc->vsel_mask;
3218 EXPORT_SYMBOL_GPL(regulator_get_hardware_vsel_register);
3221 * regulator_list_hardware_vsel - get the HW-specific register value for a selector
3222 * @regulator: regulator source
3223 * @selector: identify voltage to list
3225 * Converts the selector to a hardware-specific voltage selector that can be
3226 * directly written to the regulator registers. The address of the voltage
3227 * register can be determined by calling @regulator_get_hardware_vsel_register.
3229 * On error a negative errno is returned.
3231 int regulator_list_hardware_vsel(struct regulator *regulator,
3234 struct regulator_dev *rdev = regulator->rdev;
3235 const struct regulator_ops *ops = rdev->desc->ops;
3237 if (selector >= rdev->desc->n_voltages)
3239 if (ops->set_voltage_sel != regulator_set_voltage_sel_regmap)
3244 EXPORT_SYMBOL_GPL(regulator_list_hardware_vsel);
3247 * regulator_get_linear_step - return the voltage step size between VSEL values
3248 * @regulator: regulator source
3250 * Returns the voltage step size between VSEL values for linear
3251 * regulators, or return 0 if the regulator isn't a linear regulator.
3253 unsigned int regulator_get_linear_step(struct regulator *regulator)
3255 struct regulator_dev *rdev = regulator->rdev;
3257 return rdev->desc->uV_step;
3259 EXPORT_SYMBOL_GPL(regulator_get_linear_step);
3262 * regulator_is_supported_voltage - check if a voltage range can be supported
3264 * @regulator: Regulator to check.
3265 * @min_uV: Minimum required voltage in uV.
3266 * @max_uV: Maximum required voltage in uV.
3268 * Returns a boolean.
3270 int regulator_is_supported_voltage(struct regulator *regulator,
3271 int min_uV, int max_uV)
3273 struct regulator_dev *rdev = regulator->rdev;
3274 int i, voltages, ret;
3276 /* If we can't change voltage check the current voltage */
3277 if (!regulator_ops_is_valid(rdev, REGULATOR_CHANGE_VOLTAGE)) {
3278 ret = regulator_get_voltage(regulator);
3280 return min_uV <= ret && ret <= max_uV;
3285 /* Any voltage within constrains range is fine? */
3286 if (rdev->desc->continuous_voltage_range)
3287 return min_uV >= rdev->constraints->min_uV &&
3288 max_uV <= rdev->constraints->max_uV;
3290 ret = regulator_count_voltages(regulator);
3295 for (i = 0; i < voltages; i++) {
3296 ret = regulator_list_voltage(regulator, i);
3298 if (ret >= min_uV && ret <= max_uV)
3304 EXPORT_SYMBOL_GPL(regulator_is_supported_voltage);
3306 static int regulator_map_voltage(struct regulator_dev *rdev, int min_uV,
3309 const struct regulator_desc *desc = rdev->desc;
3311 if (desc->ops->map_voltage)
3312 return desc->ops->map_voltage(rdev, min_uV, max_uV);
3314 if (desc->ops->list_voltage == regulator_list_voltage_linear)
3315 return regulator_map_voltage_linear(rdev, min_uV, max_uV);
3317 if (desc->ops->list_voltage == regulator_list_voltage_linear_range)
3318 return regulator_map_voltage_linear_range(rdev, min_uV, max_uV);
3320 if (desc->ops->list_voltage ==
3321 regulator_list_voltage_pickable_linear_range)
3322 return regulator_map_voltage_pickable_linear_range(rdev,
3325 return regulator_map_voltage_iterate(rdev, min_uV, max_uV);
3328 static int _regulator_call_set_voltage(struct regulator_dev *rdev,
3329 int min_uV, int max_uV,
3332 struct pre_voltage_change_data data;
3335 data.old_uV = regulator_get_voltage_rdev(rdev);
3336 data.min_uV = min_uV;
3337 data.max_uV = max_uV;
3338 ret = _notifier_call_chain(rdev, REGULATOR_EVENT_PRE_VOLTAGE_CHANGE,
3340 if (ret & NOTIFY_STOP_MASK)
3343 ret = rdev->desc->ops->set_voltage(rdev, min_uV, max_uV, selector);
3347 _notifier_call_chain(rdev, REGULATOR_EVENT_ABORT_VOLTAGE_CHANGE,
3348 (void *)data.old_uV);
3353 static int _regulator_call_set_voltage_sel(struct regulator_dev *rdev,
3354 int uV, unsigned selector)
3356 struct pre_voltage_change_data data;
3359 data.old_uV = regulator_get_voltage_rdev(rdev);
3362 ret = _notifier_call_chain(rdev, REGULATOR_EVENT_PRE_VOLTAGE_CHANGE,
3364 if (ret & NOTIFY_STOP_MASK)
3367 ret = rdev->desc->ops->set_voltage_sel(rdev, selector);
3371 _notifier_call_chain(rdev, REGULATOR_EVENT_ABORT_VOLTAGE_CHANGE,
3372 (void *)data.old_uV);
3377 static int _regulator_set_voltage_sel_step(struct regulator_dev *rdev,
3378 int uV, int new_selector)
3380 const struct regulator_ops *ops = rdev->desc->ops;
3381 int diff, old_sel, curr_sel, ret;
3383 /* Stepping is only needed if the regulator is enabled. */
3384 if (!_regulator_is_enabled(rdev))
3387 if (!ops->get_voltage_sel)
3390 old_sel = ops->get_voltage_sel(rdev);
3394 diff = new_selector - old_sel;
3396 return 0; /* No change needed. */
3400 for (curr_sel = old_sel + rdev->desc->vsel_step;
3401 curr_sel < new_selector;
3402 curr_sel += rdev->desc->vsel_step) {
3404 * Call the callback directly instead of using
3405 * _regulator_call_set_voltage_sel() as we don't
3406 * want to notify anyone yet. Same in the branch
3409 ret = ops->set_voltage_sel(rdev, curr_sel);
3414 /* Stepping down. */
3415 for (curr_sel = old_sel - rdev->desc->vsel_step;
3416 curr_sel > new_selector;
3417 curr_sel -= rdev->desc->vsel_step) {
3418 ret = ops->set_voltage_sel(rdev, curr_sel);
3425 /* The final selector will trigger the notifiers. */
3426 return _regulator_call_set_voltage_sel(rdev, uV, new_selector);
3430 * At least try to return to the previous voltage if setting a new
3433 (void)ops->set_voltage_sel(rdev, old_sel);
3437 static int _regulator_set_voltage_time(struct regulator_dev *rdev,
3438 int old_uV, int new_uV)
3440 unsigned int ramp_delay = 0;
3442 if (rdev->constraints->ramp_delay)
3443 ramp_delay = rdev->constraints->ramp_delay;
3444 else if (rdev->desc->ramp_delay)
3445 ramp_delay = rdev->desc->ramp_delay;
3446 else if (rdev->constraints->settling_time)
3447 return rdev->constraints->settling_time;
3448 else if (rdev->constraints->settling_time_up &&
3450 return rdev->constraints->settling_time_up;
3451 else if (rdev->constraints->settling_time_down &&
3453 return rdev->constraints->settling_time_down;
3455 if (ramp_delay == 0) {
3456 rdev_dbg(rdev, "ramp_delay not set\n");
3460 return DIV_ROUND_UP(abs(new_uV - old_uV), ramp_delay);
3463 static int _regulator_do_set_voltage(struct regulator_dev *rdev,
3464 int min_uV, int max_uV)
3469 unsigned int selector;
3470 int old_selector = -1;
3471 const struct regulator_ops *ops = rdev->desc->ops;
3472 int old_uV = regulator_get_voltage_rdev(rdev);
3474 trace_regulator_set_voltage(rdev_get_name(rdev), min_uV, max_uV);
3476 min_uV += rdev->constraints->uV_offset;
3477 max_uV += rdev->constraints->uV_offset;
3480 * If we can't obtain the old selector there is not enough
3481 * info to call set_voltage_time_sel().
3483 if (_regulator_is_enabled(rdev) &&
3484 ops->set_voltage_time_sel && ops->get_voltage_sel) {
3485 old_selector = ops->get_voltage_sel(rdev);
3486 if (old_selector < 0)
3487 return old_selector;
3490 if (ops->set_voltage) {
3491 ret = _regulator_call_set_voltage(rdev, min_uV, max_uV,
3495 if (ops->list_voltage)
3496 best_val = ops->list_voltage(rdev,
3499 best_val = regulator_get_voltage_rdev(rdev);
3502 } else if (ops->set_voltage_sel) {
3503 ret = regulator_map_voltage(rdev, min_uV, max_uV);
3505 best_val = ops->list_voltage(rdev, ret);
3506 if (min_uV <= best_val && max_uV >= best_val) {
3508 if (old_selector == selector)
3510 else if (rdev->desc->vsel_step)
3511 ret = _regulator_set_voltage_sel_step(
3512 rdev, best_val, selector);
3514 ret = _regulator_call_set_voltage_sel(
3515 rdev, best_val, selector);
3527 if (ops->set_voltage_time_sel) {
3529 * Call set_voltage_time_sel if successfully obtained
3532 if (old_selector >= 0 && old_selector != selector)
3533 delay = ops->set_voltage_time_sel(rdev, old_selector,
3536 if (old_uV != best_val) {
3537 if (ops->set_voltage_time)
3538 delay = ops->set_voltage_time(rdev, old_uV,
3541 delay = _regulator_set_voltage_time(rdev,
3548 rdev_warn(rdev, "failed to get delay: %pe\n", ERR_PTR(delay));
3552 /* Insert any necessary delays */
3553 if (delay >= 1000) {
3554 mdelay(delay / 1000);
3555 udelay(delay % 1000);
3560 if (best_val >= 0) {
3561 unsigned long data = best_val;
3563 _notifier_call_chain(rdev, REGULATOR_EVENT_VOLTAGE_CHANGE,
3568 trace_regulator_set_voltage_complete(rdev_get_name(rdev), best_val);
3573 static int _regulator_do_set_suspend_voltage(struct regulator_dev *rdev,
3574 int min_uV, int max_uV, suspend_state_t state)
3576 struct regulator_state *rstate;
3579 rstate = regulator_get_suspend_state(rdev, state);
3583 if (min_uV < rstate->min_uV)
3584 min_uV = rstate->min_uV;
3585 if (max_uV > rstate->max_uV)
3586 max_uV = rstate->max_uV;
3588 sel = regulator_map_voltage(rdev, min_uV, max_uV);
3592 uV = rdev->desc->ops->list_voltage(rdev, sel);
3593 if (uV >= min_uV && uV <= max_uV)
3599 static int regulator_set_voltage_unlocked(struct regulator *regulator,
3600 int min_uV, int max_uV,
3601 suspend_state_t state)
3603 struct regulator_dev *rdev = regulator->rdev;
3604 struct regulator_voltage *voltage = ®ulator->voltage[state];
3606 int old_min_uV, old_max_uV;
3609 /* If we're setting the same range as last time the change
3610 * should be a noop (some cpufreq implementations use the same
3611 * voltage for multiple frequencies, for example).
3613 if (voltage->min_uV == min_uV && voltage->max_uV == max_uV)
3616 /* If we're trying to set a range that overlaps the current voltage,
3617 * return successfully even though the regulator does not support
3618 * changing the voltage.
3620 if (!regulator_ops_is_valid(rdev, REGULATOR_CHANGE_VOLTAGE)) {
3621 current_uV = regulator_get_voltage_rdev(rdev);
3622 if (min_uV <= current_uV && current_uV <= max_uV) {
3623 voltage->min_uV = min_uV;
3624 voltage->max_uV = max_uV;
3630 if (!rdev->desc->ops->set_voltage &&
3631 !rdev->desc->ops->set_voltage_sel) {
3636 /* constraints check */
3637 ret = regulator_check_voltage(rdev, &min_uV, &max_uV);
3641 /* restore original values in case of error */
3642 old_min_uV = voltage->min_uV;
3643 old_max_uV = voltage->max_uV;
3644 voltage->min_uV = min_uV;
3645 voltage->max_uV = max_uV;
3647 /* for not coupled regulators this will just set the voltage */
3648 ret = regulator_balance_voltage(rdev, state);
3650 voltage->min_uV = old_min_uV;
3651 voltage->max_uV = old_max_uV;
3658 int regulator_set_voltage_rdev(struct regulator_dev *rdev, int min_uV,
3659 int max_uV, suspend_state_t state)
3661 int best_supply_uV = 0;
3662 int supply_change_uV = 0;
3666 regulator_ops_is_valid(rdev->supply->rdev,
3667 REGULATOR_CHANGE_VOLTAGE) &&
3668 (rdev->desc->min_dropout_uV || !(rdev->desc->ops->get_voltage ||
3669 rdev->desc->ops->get_voltage_sel))) {
3670 int current_supply_uV;
3673 selector = regulator_map_voltage(rdev, min_uV, max_uV);
3679 best_supply_uV = _regulator_list_voltage(rdev, selector, 0);
3680 if (best_supply_uV < 0) {
3681 ret = best_supply_uV;
3685 best_supply_uV += rdev->desc->min_dropout_uV;
3687 current_supply_uV = regulator_get_voltage_rdev(rdev->supply->rdev);
3688 if (current_supply_uV < 0) {
3689 ret = current_supply_uV;
3693 supply_change_uV = best_supply_uV - current_supply_uV;
3696 if (supply_change_uV > 0) {
3697 ret = regulator_set_voltage_unlocked(rdev->supply,
3698 best_supply_uV, INT_MAX, state);
3700 dev_err(&rdev->dev, "Failed to increase supply voltage: %pe\n",
3706 if (state == PM_SUSPEND_ON)
3707 ret = _regulator_do_set_voltage(rdev, min_uV, max_uV);
3709 ret = _regulator_do_set_suspend_voltage(rdev, min_uV,
3714 if (supply_change_uV < 0) {
3715 ret = regulator_set_voltage_unlocked(rdev->supply,
3716 best_supply_uV, INT_MAX, state);
3718 dev_warn(&rdev->dev, "Failed to decrease supply voltage: %pe\n",
3720 /* No need to fail here */
3727 EXPORT_SYMBOL_GPL(regulator_set_voltage_rdev);
3729 static int regulator_limit_voltage_step(struct regulator_dev *rdev,
3730 int *current_uV, int *min_uV)
3732 struct regulation_constraints *constraints = rdev->constraints;
3734 /* Limit voltage change only if necessary */
3735 if (!constraints->max_uV_step || !_regulator_is_enabled(rdev))
3738 if (*current_uV < 0) {
3739 *current_uV = regulator_get_voltage_rdev(rdev);
3741 if (*current_uV < 0)
3745 if (abs(*current_uV - *min_uV) <= constraints->max_uV_step)
3748 /* Clamp target voltage within the given step */
3749 if (*current_uV < *min_uV)
3750 *min_uV = min(*current_uV + constraints->max_uV_step,
3753 *min_uV = max(*current_uV - constraints->max_uV_step,
3759 static int regulator_get_optimal_voltage(struct regulator_dev *rdev,
3761 int *min_uV, int *max_uV,
3762 suspend_state_t state,
3765 struct coupling_desc *c_desc = &rdev->coupling_desc;
3766 struct regulator_dev **c_rdevs = c_desc->coupled_rdevs;
3767 struct regulation_constraints *constraints = rdev->constraints;
3768 int desired_min_uV = 0, desired_max_uV = INT_MAX;
3769 int max_current_uV = 0, min_current_uV = INT_MAX;
3770 int highest_min_uV = 0, target_uV, possible_uV;
3771 int i, ret, max_spread;
3777 * If there are no coupled regulators, simply set the voltage
3778 * demanded by consumers.
3780 if (n_coupled == 1) {
3782 * If consumers don't provide any demands, set voltage
3785 desired_min_uV = constraints->min_uV;
3786 desired_max_uV = constraints->max_uV;
3788 ret = regulator_check_consumers(rdev,
3790 &desired_max_uV, state);
3794 possible_uV = desired_min_uV;
3800 /* Find highest min desired voltage */
3801 for (i = 0; i < n_coupled; i++) {
3803 int tmp_max = INT_MAX;
3805 lockdep_assert_held_once(&c_rdevs[i]->mutex.base);
3807 ret = regulator_check_consumers(c_rdevs[i],
3813 ret = regulator_check_voltage(c_rdevs[i], &tmp_min, &tmp_max);
3817 highest_min_uV = max(highest_min_uV, tmp_min);
3820 desired_min_uV = tmp_min;
3821 desired_max_uV = tmp_max;
3825 max_spread = constraints->max_spread[0];
3828 * Let target_uV be equal to the desired one if possible.
3829 * If not, set it to minimum voltage, allowed by other coupled
3832 target_uV = max(desired_min_uV, highest_min_uV - max_spread);
3835 * Find min and max voltages, which currently aren't violating
3838 for (i = 1; i < n_coupled; i++) {
3841 if (!_regulator_is_enabled(c_rdevs[i]))
3844 tmp_act = regulator_get_voltage_rdev(c_rdevs[i]);
3848 min_current_uV = min(tmp_act, min_current_uV);
3849 max_current_uV = max(tmp_act, max_current_uV);
3852 /* There aren't any other regulators enabled */
3853 if (max_current_uV == 0) {
3854 possible_uV = target_uV;
3857 * Correct target voltage, so as it currently isn't
3858 * violating max_spread
3860 possible_uV = max(target_uV, max_current_uV - max_spread);
3861 possible_uV = min(possible_uV, min_current_uV + max_spread);
3864 if (possible_uV > desired_max_uV)
3867 done = (possible_uV == target_uV);
3868 desired_min_uV = possible_uV;
3871 /* Apply max_uV_step constraint if necessary */
3872 if (state == PM_SUSPEND_ON) {
3873 ret = regulator_limit_voltage_step(rdev, current_uV,
3882 /* Set current_uV if wasn't done earlier in the code and if necessary */
3883 if (n_coupled > 1 && *current_uV == -1) {
3885 if (_regulator_is_enabled(rdev)) {
3886 ret = regulator_get_voltage_rdev(rdev);
3892 *current_uV = desired_min_uV;
3896 *min_uV = desired_min_uV;
3897 *max_uV = desired_max_uV;
3902 int regulator_do_balance_voltage(struct regulator_dev *rdev,
3903 suspend_state_t state, bool skip_coupled)
3905 struct regulator_dev **c_rdevs;
3906 struct regulator_dev *best_rdev;
3907 struct coupling_desc *c_desc = &rdev->coupling_desc;
3908 int i, ret, n_coupled, best_min_uV, best_max_uV, best_c_rdev;
3909 unsigned int delta, best_delta;
3910 unsigned long c_rdev_done = 0;
3911 bool best_c_rdev_done;
3913 c_rdevs = c_desc->coupled_rdevs;
3914 n_coupled = skip_coupled ? 1 : c_desc->n_coupled;
3917 * Find the best possible voltage change on each loop. Leave the loop
3918 * if there isn't any possible change.
3921 best_c_rdev_done = false;
3929 * Find highest difference between optimal voltage
3930 * and current voltage.
3932 for (i = 0; i < n_coupled; i++) {
3934 * optimal_uV is the best voltage that can be set for
3935 * i-th regulator at the moment without violating
3936 * max_spread constraint in order to balance
3937 * the coupled voltages.
3939 int optimal_uV = 0, optimal_max_uV = 0, current_uV = 0;
3941 if (test_bit(i, &c_rdev_done))
3944 ret = regulator_get_optimal_voltage(c_rdevs[i],
3952 delta = abs(optimal_uV - current_uV);
3954 if (delta && best_delta <= delta) {
3955 best_c_rdev_done = ret;
3957 best_rdev = c_rdevs[i];
3958 best_min_uV = optimal_uV;
3959 best_max_uV = optimal_max_uV;
3964 /* Nothing to change, return successfully */
3970 ret = regulator_set_voltage_rdev(best_rdev, best_min_uV,
3971 best_max_uV, state);
3976 if (best_c_rdev_done)
3977 set_bit(best_c_rdev, &c_rdev_done);
3979 } while (n_coupled > 1);
3985 static int regulator_balance_voltage(struct regulator_dev *rdev,
3986 suspend_state_t state)
3988 struct coupling_desc *c_desc = &rdev->coupling_desc;
3989 struct regulator_coupler *coupler = c_desc->coupler;
3990 bool skip_coupled = false;
3993 * If system is in a state other than PM_SUSPEND_ON, don't check
3994 * other coupled regulators.
3996 if (state != PM_SUSPEND_ON)
3997 skip_coupled = true;
3999 if (c_desc->n_resolved < c_desc->n_coupled) {
4000 rdev_err(rdev, "Not all coupled regulators registered\n");
4004 /* Invoke custom balancer for customized couplers */
4005 if (coupler && coupler->balance_voltage)
4006 return coupler->balance_voltage(coupler, rdev, state);
4008 return regulator_do_balance_voltage(rdev, state, skip_coupled);
4012 * regulator_set_voltage - set regulator output voltage
4013 * @regulator: regulator source
4014 * @min_uV: Minimum required voltage in uV
4015 * @max_uV: Maximum acceptable voltage in uV
4017 * Sets a voltage regulator to the desired output voltage. This can be set
4018 * during any regulator state. IOW, regulator can be disabled or enabled.
4020 * If the regulator is enabled then the voltage will change to the new value
4021 * immediately otherwise if the regulator is disabled the regulator will
4022 * output at the new voltage when enabled.
4024 * NOTE: If the regulator is shared between several devices then the lowest
4025 * request voltage that meets the system constraints will be used.
4026 * Regulator system constraints must be set for this regulator before
4027 * calling this function otherwise this call will fail.
4029 int regulator_set_voltage(struct regulator *regulator, int min_uV, int max_uV)
4031 struct ww_acquire_ctx ww_ctx;
4034 regulator_lock_dependent(regulator->rdev, &ww_ctx);
4036 ret = regulator_set_voltage_unlocked(regulator, min_uV, max_uV,
4039 regulator_unlock_dependent(regulator->rdev, &ww_ctx);
4043 EXPORT_SYMBOL_GPL(regulator_set_voltage);
4045 static inline int regulator_suspend_toggle(struct regulator_dev *rdev,
4046 suspend_state_t state, bool en)
4048 struct regulator_state *rstate;
4050 rstate = regulator_get_suspend_state(rdev, state);
4054 if (!rstate->changeable)
4057 rstate->enabled = (en) ? ENABLE_IN_SUSPEND : DISABLE_IN_SUSPEND;
4062 int regulator_suspend_enable(struct regulator_dev *rdev,
4063 suspend_state_t state)
4065 return regulator_suspend_toggle(rdev, state, true);
4067 EXPORT_SYMBOL_GPL(regulator_suspend_enable);
4069 int regulator_suspend_disable(struct regulator_dev *rdev,
4070 suspend_state_t state)
4072 struct regulator *regulator;
4073 struct regulator_voltage *voltage;
4076 * if any consumer wants this regulator device keeping on in
4077 * suspend states, don't set it as disabled.
4079 list_for_each_entry(regulator, &rdev->consumer_list, list) {
4080 voltage = ®ulator->voltage[state];
4081 if (voltage->min_uV || voltage->max_uV)
4085 return regulator_suspend_toggle(rdev, state, false);
4087 EXPORT_SYMBOL_GPL(regulator_suspend_disable);
4089 static int _regulator_set_suspend_voltage(struct regulator *regulator,
4090 int min_uV, int max_uV,
4091 suspend_state_t state)
4093 struct regulator_dev *rdev = regulator->rdev;
4094 struct regulator_state *rstate;
4096 rstate = regulator_get_suspend_state(rdev, state);
4100 if (rstate->min_uV == rstate->max_uV) {
4101 rdev_err(rdev, "The suspend voltage can't be changed!\n");
4105 return regulator_set_voltage_unlocked(regulator, min_uV, max_uV, state);
4108 int regulator_set_suspend_voltage(struct regulator *regulator, int min_uV,
4109 int max_uV, suspend_state_t state)
4111 struct ww_acquire_ctx ww_ctx;
4114 /* PM_SUSPEND_ON is handled by regulator_set_voltage() */
4115 if (regulator_check_states(state) || state == PM_SUSPEND_ON)
4118 regulator_lock_dependent(regulator->rdev, &ww_ctx);
4120 ret = _regulator_set_suspend_voltage(regulator, min_uV,
4123 regulator_unlock_dependent(regulator->rdev, &ww_ctx);
4127 EXPORT_SYMBOL_GPL(regulator_set_suspend_voltage);
4130 * regulator_set_voltage_time - get raise/fall time
4131 * @regulator: regulator source
4132 * @old_uV: starting voltage in microvolts
4133 * @new_uV: target voltage in microvolts
4135 * Provided with the starting and ending voltage, this function attempts to
4136 * calculate the time in microseconds required to rise or fall to this new
4139 int regulator_set_voltage_time(struct regulator *regulator,
4140 int old_uV, int new_uV)
4142 struct regulator_dev *rdev = regulator->rdev;
4143 const struct regulator_ops *ops = rdev->desc->ops;
4149 if (ops->set_voltage_time)
4150 return ops->set_voltage_time(rdev, old_uV, new_uV);
4151 else if (!ops->set_voltage_time_sel)
4152 return _regulator_set_voltage_time(rdev, old_uV, new_uV);
4154 /* Currently requires operations to do this */
4155 if (!ops->list_voltage || !rdev->desc->n_voltages)
4158 for (i = 0; i < rdev->desc->n_voltages; i++) {
4159 /* We only look for exact voltage matches here */
4160 voltage = regulator_list_voltage(regulator, i);
4165 if (voltage == old_uV)
4167 if (voltage == new_uV)
4171 if (old_sel < 0 || new_sel < 0)
4174 return ops->set_voltage_time_sel(rdev, old_sel, new_sel);
4176 EXPORT_SYMBOL_GPL(regulator_set_voltage_time);
4179 * regulator_set_voltage_time_sel - get raise/fall time
4180 * @rdev: regulator source device
4181 * @old_selector: selector for starting voltage
4182 * @new_selector: selector for target voltage
4184 * Provided with the starting and target voltage selectors, this function
4185 * returns time in microseconds required to rise or fall to this new voltage
4187 * Drivers providing ramp_delay in regulation_constraints can use this as their
4188 * set_voltage_time_sel() operation.
4190 int regulator_set_voltage_time_sel(struct regulator_dev *rdev,
4191 unsigned int old_selector,
4192 unsigned int new_selector)
4194 int old_volt, new_volt;
4197 if (!rdev->desc->ops->list_voltage)
4200 old_volt = rdev->desc->ops->list_voltage(rdev, old_selector);
4201 new_volt = rdev->desc->ops->list_voltage(rdev, new_selector);
4203 if (rdev->desc->ops->set_voltage_time)
4204 return rdev->desc->ops->set_voltage_time(rdev, old_volt,
4207 return _regulator_set_voltage_time(rdev, old_volt, new_volt);
4209 EXPORT_SYMBOL_GPL(regulator_set_voltage_time_sel);
4212 * regulator_sync_voltage - re-apply last regulator output voltage
4213 * @regulator: regulator source
4215 * Re-apply the last configured voltage. This is intended to be used
4216 * where some external control source the consumer is cooperating with
4217 * has caused the configured voltage to change.
4219 int regulator_sync_voltage(struct regulator *regulator)
4221 struct regulator_dev *rdev = regulator->rdev;
4222 struct regulator_voltage *voltage = ®ulator->voltage[PM_SUSPEND_ON];
4223 int ret, min_uV, max_uV;
4225 regulator_lock(rdev);
4227 if (!rdev->desc->ops->set_voltage &&
4228 !rdev->desc->ops->set_voltage_sel) {
4233 /* This is only going to work if we've had a voltage configured. */
4234 if (!voltage->min_uV && !voltage->max_uV) {
4239 min_uV = voltage->min_uV;
4240 max_uV = voltage->max_uV;
4242 /* This should be a paranoia check... */
4243 ret = regulator_check_voltage(rdev, &min_uV, &max_uV);
4247 ret = regulator_check_consumers(rdev, &min_uV, &max_uV, 0);
4251 ret = _regulator_do_set_voltage(rdev, min_uV, max_uV);
4254 regulator_unlock(rdev);
4257 EXPORT_SYMBOL_GPL(regulator_sync_voltage);
4259 int regulator_get_voltage_rdev(struct regulator_dev *rdev)
4264 if (rdev->desc->ops->get_bypass) {
4265 ret = rdev->desc->ops->get_bypass(rdev, &bypassed);
4269 /* if bypassed the regulator must have a supply */
4270 if (!rdev->supply) {
4272 "bypassed regulator has no supply!\n");
4273 return -EPROBE_DEFER;
4276 return regulator_get_voltage_rdev(rdev->supply->rdev);
4280 if (rdev->desc->ops->get_voltage_sel) {
4281 sel = rdev->desc->ops->get_voltage_sel(rdev);
4284 ret = rdev->desc->ops->list_voltage(rdev, sel);
4285 } else if (rdev->desc->ops->get_voltage) {
4286 ret = rdev->desc->ops->get_voltage(rdev);
4287 } else if (rdev->desc->ops->list_voltage) {
4288 ret = rdev->desc->ops->list_voltage(rdev, 0);
4289 } else if (rdev->desc->fixed_uV && (rdev->desc->n_voltages == 1)) {
4290 ret = rdev->desc->fixed_uV;
4291 } else if (rdev->supply) {
4292 ret = regulator_get_voltage_rdev(rdev->supply->rdev);
4293 } else if (rdev->supply_name) {
4294 return -EPROBE_DEFER;
4301 return ret - rdev->constraints->uV_offset;
4303 EXPORT_SYMBOL_GPL(regulator_get_voltage_rdev);
4306 * regulator_get_voltage - get regulator output voltage
4307 * @regulator: regulator source
4309 * This returns the current regulator voltage in uV.
4311 * NOTE: If the regulator is disabled it will return the voltage value. This
4312 * function should not be used to determine regulator state.
4314 int regulator_get_voltage(struct regulator *regulator)
4316 struct ww_acquire_ctx ww_ctx;
4319 regulator_lock_dependent(regulator->rdev, &ww_ctx);
4320 ret = regulator_get_voltage_rdev(regulator->rdev);
4321 regulator_unlock_dependent(regulator->rdev, &ww_ctx);
4325 EXPORT_SYMBOL_GPL(regulator_get_voltage);
4328 * regulator_set_current_limit - set regulator output current limit
4329 * @regulator: regulator source
4330 * @min_uA: Minimum supported current in uA
4331 * @max_uA: Maximum supported current in uA
4333 * Sets current sink to the desired output current. This can be set during
4334 * any regulator state. IOW, regulator can be disabled or enabled.
4336 * If the regulator is enabled then the current will change to the new value
4337 * immediately otherwise if the regulator is disabled the regulator will
4338 * output at the new current when enabled.
4340 * NOTE: Regulator system constraints must be set for this regulator before
4341 * calling this function otherwise this call will fail.
4343 int regulator_set_current_limit(struct regulator *regulator,
4344 int min_uA, int max_uA)
4346 struct regulator_dev *rdev = regulator->rdev;
4349 regulator_lock(rdev);
4352 if (!rdev->desc->ops->set_current_limit) {
4357 /* constraints check */
4358 ret = regulator_check_current_limit(rdev, &min_uA, &max_uA);
4362 ret = rdev->desc->ops->set_current_limit(rdev, min_uA, max_uA);
4364 regulator_unlock(rdev);
4367 EXPORT_SYMBOL_GPL(regulator_set_current_limit);
4369 static int _regulator_get_current_limit_unlocked(struct regulator_dev *rdev)
4372 if (!rdev->desc->ops->get_current_limit)
4375 return rdev->desc->ops->get_current_limit(rdev);
4378 static int _regulator_get_current_limit(struct regulator_dev *rdev)
4382 regulator_lock(rdev);
4383 ret = _regulator_get_current_limit_unlocked(rdev);
4384 regulator_unlock(rdev);
4390 * regulator_get_current_limit - get regulator output current
4391 * @regulator: regulator source
4393 * This returns the current supplied by the specified current sink in uA.
4395 * NOTE: If the regulator is disabled it will return the current value. This
4396 * function should not be used to determine regulator state.
4398 int regulator_get_current_limit(struct regulator *regulator)
4400 return _regulator_get_current_limit(regulator->rdev);
4402 EXPORT_SYMBOL_GPL(regulator_get_current_limit);
4405 * regulator_set_mode - set regulator operating mode
4406 * @regulator: regulator source
4407 * @mode: operating mode - one of the REGULATOR_MODE constants
4409 * Set regulator operating mode to increase regulator efficiency or improve
4410 * regulation performance.
4412 * NOTE: Regulator system constraints must be set for this regulator before
4413 * calling this function otherwise this call will fail.
4415 int regulator_set_mode(struct regulator *regulator, unsigned int mode)
4417 struct regulator_dev *rdev = regulator->rdev;
4419 int regulator_curr_mode;
4421 regulator_lock(rdev);
4424 if (!rdev->desc->ops->set_mode) {
4429 /* return if the same mode is requested */
4430 if (rdev->desc->ops->get_mode) {
4431 regulator_curr_mode = rdev->desc->ops->get_mode(rdev);
4432 if (regulator_curr_mode == mode) {
4438 /* constraints check */
4439 ret = regulator_mode_constrain(rdev, &mode);
4443 ret = rdev->desc->ops->set_mode(rdev, mode);
4445 regulator_unlock(rdev);
4448 EXPORT_SYMBOL_GPL(regulator_set_mode);
4450 static unsigned int _regulator_get_mode_unlocked(struct regulator_dev *rdev)
4453 if (!rdev->desc->ops->get_mode)
4456 return rdev->desc->ops->get_mode(rdev);
4459 static unsigned int _regulator_get_mode(struct regulator_dev *rdev)
4463 regulator_lock(rdev);
4464 ret = _regulator_get_mode_unlocked(rdev);
4465 regulator_unlock(rdev);
4471 * regulator_get_mode - get regulator operating mode
4472 * @regulator: regulator source
4474 * Get the current regulator operating mode.
4476 unsigned int regulator_get_mode(struct regulator *regulator)
4478 return _regulator_get_mode(regulator->rdev);
4480 EXPORT_SYMBOL_GPL(regulator_get_mode);
4482 static int _regulator_get_error_flags(struct regulator_dev *rdev,
4483 unsigned int *flags)
4487 regulator_lock(rdev);
4490 if (!rdev->desc->ops->get_error_flags) {
4495 ret = rdev->desc->ops->get_error_flags(rdev, flags);
4497 regulator_unlock(rdev);
4502 * regulator_get_error_flags - get regulator error information
4503 * @regulator: regulator source
4504 * @flags: pointer to store error flags
4506 * Get the current regulator error information.
4508 int regulator_get_error_flags(struct regulator *regulator,
4509 unsigned int *flags)
4511 return _regulator_get_error_flags(regulator->rdev, flags);
4513 EXPORT_SYMBOL_GPL(regulator_get_error_flags);
4516 * regulator_set_load - set regulator load
4517 * @regulator: regulator source
4518 * @uA_load: load current
4520 * Notifies the regulator core of a new device load. This is then used by
4521 * DRMS (if enabled by constraints) to set the most efficient regulator
4522 * operating mode for the new regulator loading.
4524 * Consumer devices notify their supply regulator of the maximum power
4525 * they will require (can be taken from device datasheet in the power
4526 * consumption tables) when they change operational status and hence power
4527 * state. Examples of operational state changes that can affect power
4528 * consumption are :-
4530 * o Device is opened / closed.
4531 * o Device I/O is about to begin or has just finished.
4532 * o Device is idling in between work.
4534 * This information is also exported via sysfs to userspace.
4536 * DRMS will sum the total requested load on the regulator and change
4537 * to the most efficient operating mode if platform constraints allow.
4539 * NOTE: when a regulator consumer requests to have a regulator
4540 * disabled then any load that consumer requested no longer counts
4541 * toward the total requested load. If the regulator is re-enabled
4542 * then the previously requested load will start counting again.
4544 * If a regulator is an always-on regulator then an individual consumer's
4545 * load will still be removed if that consumer is fully disabled.
4547 * On error a negative errno is returned.
4549 int regulator_set_load(struct regulator *regulator, int uA_load)
4551 struct regulator_dev *rdev = regulator->rdev;
4555 regulator_lock(rdev);
4556 old_uA_load = regulator->uA_load;
4557 regulator->uA_load = uA_load;
4558 if (regulator->enable_count && old_uA_load != uA_load) {
4559 ret = drms_uA_update(rdev);
4561 regulator->uA_load = old_uA_load;
4563 regulator_unlock(rdev);
4567 EXPORT_SYMBOL_GPL(regulator_set_load);
4570 * regulator_allow_bypass - allow the regulator to go into bypass mode
4572 * @regulator: Regulator to configure
4573 * @enable: enable or disable bypass mode
4575 * Allow the regulator to go into bypass mode if all other consumers
4576 * for the regulator also enable bypass mode and the machine
4577 * constraints allow this. Bypass mode means that the regulator is
4578 * simply passing the input directly to the output with no regulation.
4580 int regulator_allow_bypass(struct regulator *regulator, bool enable)
4582 struct regulator_dev *rdev = regulator->rdev;
4583 const char *name = rdev_get_name(rdev);
4586 if (!rdev->desc->ops->set_bypass)
4589 if (!regulator_ops_is_valid(rdev, REGULATOR_CHANGE_BYPASS))
4592 regulator_lock(rdev);
4594 if (enable && !regulator->bypass) {
4595 rdev->bypass_count++;
4597 if (rdev->bypass_count == rdev->open_count) {
4598 trace_regulator_bypass_enable(name);
4600 ret = rdev->desc->ops->set_bypass(rdev, enable);
4602 rdev->bypass_count--;
4604 trace_regulator_bypass_enable_complete(name);
4607 } else if (!enable && regulator->bypass) {
4608 rdev->bypass_count--;
4610 if (rdev->bypass_count != rdev->open_count) {
4611 trace_regulator_bypass_disable(name);
4613 ret = rdev->desc->ops->set_bypass(rdev, enable);
4615 rdev->bypass_count++;
4617 trace_regulator_bypass_disable_complete(name);
4622 regulator->bypass = enable;
4624 regulator_unlock(rdev);
4628 EXPORT_SYMBOL_GPL(regulator_allow_bypass);
4631 * regulator_register_notifier - register regulator event notifier
4632 * @regulator: regulator source
4633 * @nb: notifier block
4635 * Register notifier block to receive regulator events.
4637 int regulator_register_notifier(struct regulator *regulator,
4638 struct notifier_block *nb)
4640 return blocking_notifier_chain_register(®ulator->rdev->notifier,
4643 EXPORT_SYMBOL_GPL(regulator_register_notifier);
4646 * regulator_unregister_notifier - unregister regulator event notifier
4647 * @regulator: regulator source
4648 * @nb: notifier block
4650 * Unregister regulator event notifier block.
4652 int regulator_unregister_notifier(struct regulator *regulator,
4653 struct notifier_block *nb)
4655 return blocking_notifier_chain_unregister(®ulator->rdev->notifier,
4658 EXPORT_SYMBOL_GPL(regulator_unregister_notifier);
4660 /* notify regulator consumers and downstream regulator consumers.
4661 * Note mutex must be held by caller.
4663 static int _notifier_call_chain(struct regulator_dev *rdev,
4664 unsigned long event, void *data)
4666 /* call rdev chain first */
4667 return blocking_notifier_call_chain(&rdev->notifier, event, data);
4671 * regulator_bulk_get - get multiple regulator consumers
4673 * @dev: Device to supply
4674 * @num_consumers: Number of consumers to register
4675 * @consumers: Configuration of consumers; clients are stored here.
4677 * @return 0 on success, an errno on failure.
4679 * This helper function allows drivers to get several regulator
4680 * consumers in one operation. If any of the regulators cannot be
4681 * acquired then any regulators that were allocated will be freed
4682 * before returning to the caller.
4684 int regulator_bulk_get(struct device *dev, int num_consumers,
4685 struct regulator_bulk_data *consumers)
4690 for (i = 0; i < num_consumers; i++)
4691 consumers[i].consumer = NULL;
4693 for (i = 0; i < num_consumers; i++) {
4694 consumers[i].consumer = regulator_get(dev,
4695 consumers[i].supply);
4696 if (IS_ERR(consumers[i].consumer)) {
4697 ret = PTR_ERR(consumers[i].consumer);
4698 consumers[i].consumer = NULL;
4706 if (ret != -EPROBE_DEFER)
4707 dev_err(dev, "Failed to get supply '%s': %pe\n",
4708 consumers[i].supply, ERR_PTR(ret));
4710 dev_dbg(dev, "Failed to get supply '%s', deferring\n",
4711 consumers[i].supply);
4714 regulator_put(consumers[i].consumer);
4718 EXPORT_SYMBOL_GPL(regulator_bulk_get);
4720 static void regulator_bulk_enable_async(void *data, async_cookie_t cookie)
4722 struct regulator_bulk_data *bulk = data;
4724 bulk->ret = regulator_enable(bulk->consumer);
4728 * regulator_bulk_enable - enable multiple regulator consumers
4730 * @num_consumers: Number of consumers
4731 * @consumers: Consumer data; clients are stored here.
4732 * @return 0 on success, an errno on failure
4734 * This convenience API allows consumers to enable multiple regulator
4735 * clients in a single API call. If any consumers cannot be enabled
4736 * then any others that were enabled will be disabled again prior to
4739 int regulator_bulk_enable(int num_consumers,
4740 struct regulator_bulk_data *consumers)
4742 ASYNC_DOMAIN_EXCLUSIVE(async_domain);
4746 for (i = 0; i < num_consumers; i++) {
4747 async_schedule_domain(regulator_bulk_enable_async,
4748 &consumers[i], &async_domain);
4751 async_synchronize_full_domain(&async_domain);
4753 /* If any consumer failed we need to unwind any that succeeded */
4754 for (i = 0; i < num_consumers; i++) {
4755 if (consumers[i].ret != 0) {
4756 ret = consumers[i].ret;
4764 for (i = 0; i < num_consumers; i++) {
4765 if (consumers[i].ret < 0)
4766 pr_err("Failed to enable %s: %pe\n", consumers[i].supply,
4767 ERR_PTR(consumers[i].ret));
4769 regulator_disable(consumers[i].consumer);
4774 EXPORT_SYMBOL_GPL(regulator_bulk_enable);
4777 * regulator_bulk_disable - disable multiple regulator consumers
4779 * @num_consumers: Number of consumers
4780 * @consumers: Consumer data; clients are stored here.
4781 * @return 0 on success, an errno on failure
4783 * This convenience API allows consumers to disable multiple regulator
4784 * clients in a single API call. If any consumers cannot be disabled
4785 * then any others that were disabled will be enabled again prior to
4788 int regulator_bulk_disable(int num_consumers,
4789 struct regulator_bulk_data *consumers)
4794 for (i = num_consumers - 1; i >= 0; --i) {
4795 ret = regulator_disable(consumers[i].consumer);
4803 pr_err("Failed to disable %s: %pe\n", consumers[i].supply, ERR_PTR(ret));
4804 for (++i; i < num_consumers; ++i) {
4805 r = regulator_enable(consumers[i].consumer);
4807 pr_err("Failed to re-enable %s: %pe\n",
4808 consumers[i].supply, ERR_PTR(r));
4813 EXPORT_SYMBOL_GPL(regulator_bulk_disable);
4816 * regulator_bulk_force_disable - force disable multiple regulator consumers
4818 * @num_consumers: Number of consumers
4819 * @consumers: Consumer data; clients are stored here.
4820 * @return 0 on success, an errno on failure
4822 * This convenience API allows consumers to forcibly disable multiple regulator
4823 * clients in a single API call.
4824 * NOTE: This should be used for situations when device damage will
4825 * likely occur if the regulators are not disabled (e.g. over temp).
4826 * Although regulator_force_disable function call for some consumers can
4827 * return error numbers, the function is called for all consumers.
4829 int regulator_bulk_force_disable(int num_consumers,
4830 struct regulator_bulk_data *consumers)
4835 for (i = 0; i < num_consumers; i++) {
4837 regulator_force_disable(consumers[i].consumer);
4839 /* Store first error for reporting */
4840 if (consumers[i].ret && !ret)
4841 ret = consumers[i].ret;
4846 EXPORT_SYMBOL_GPL(regulator_bulk_force_disable);
4849 * regulator_bulk_free - free multiple regulator consumers
4851 * @num_consumers: Number of consumers
4852 * @consumers: Consumer data; clients are stored here.
4854 * This convenience API allows consumers to free multiple regulator
4855 * clients in a single API call.
4857 void regulator_bulk_free(int num_consumers,
4858 struct regulator_bulk_data *consumers)
4862 for (i = 0; i < num_consumers; i++) {
4863 regulator_put(consumers[i].consumer);
4864 consumers[i].consumer = NULL;
4867 EXPORT_SYMBOL_GPL(regulator_bulk_free);
4870 * regulator_notifier_call_chain - call regulator event notifier
4871 * @rdev: regulator source
4872 * @event: notifier block
4873 * @data: callback-specific data.
4875 * Called by regulator drivers to notify clients a regulator event has
4878 int regulator_notifier_call_chain(struct regulator_dev *rdev,
4879 unsigned long event, void *data)
4881 _notifier_call_chain(rdev, event, data);
4885 EXPORT_SYMBOL_GPL(regulator_notifier_call_chain);
4888 * regulator_mode_to_status - convert a regulator mode into a status
4890 * @mode: Mode to convert
4892 * Convert a regulator mode into a status.
4894 int regulator_mode_to_status(unsigned int mode)
4897 case REGULATOR_MODE_FAST:
4898 return REGULATOR_STATUS_FAST;
4899 case REGULATOR_MODE_NORMAL:
4900 return REGULATOR_STATUS_NORMAL;
4901 case REGULATOR_MODE_IDLE:
4902 return REGULATOR_STATUS_IDLE;
4903 case REGULATOR_MODE_STANDBY:
4904 return REGULATOR_STATUS_STANDBY;
4906 return REGULATOR_STATUS_UNDEFINED;
4909 EXPORT_SYMBOL_GPL(regulator_mode_to_status);
4911 static struct attribute *regulator_dev_attrs[] = {
4912 &dev_attr_name.attr,
4913 &dev_attr_num_users.attr,
4914 &dev_attr_type.attr,
4915 &dev_attr_microvolts.attr,
4916 &dev_attr_microamps.attr,
4917 &dev_attr_opmode.attr,
4918 &dev_attr_state.attr,
4919 &dev_attr_status.attr,
4920 &dev_attr_bypass.attr,
4921 &dev_attr_requested_microamps.attr,
4922 &dev_attr_min_microvolts.attr,
4923 &dev_attr_max_microvolts.attr,
4924 &dev_attr_min_microamps.attr,
4925 &dev_attr_max_microamps.attr,
4926 &dev_attr_suspend_standby_state.attr,
4927 &dev_attr_suspend_mem_state.attr,
4928 &dev_attr_suspend_disk_state.attr,
4929 &dev_attr_suspend_standby_microvolts.attr,
4930 &dev_attr_suspend_mem_microvolts.attr,
4931 &dev_attr_suspend_disk_microvolts.attr,
4932 &dev_attr_suspend_standby_mode.attr,
4933 &dev_attr_suspend_mem_mode.attr,
4934 &dev_attr_suspend_disk_mode.attr,
4939 * To avoid cluttering sysfs (and memory) with useless state, only
4940 * create attributes that can be meaningfully displayed.
4942 static umode_t regulator_attr_is_visible(struct kobject *kobj,
4943 struct attribute *attr, int idx)
4945 struct device *dev = kobj_to_dev(kobj);
4946 struct regulator_dev *rdev = dev_to_rdev(dev);
4947 const struct regulator_ops *ops = rdev->desc->ops;
4948 umode_t mode = attr->mode;
4950 /* these three are always present */
4951 if (attr == &dev_attr_name.attr ||
4952 attr == &dev_attr_num_users.attr ||
4953 attr == &dev_attr_type.attr)
4956 /* some attributes need specific methods to be displayed */
4957 if (attr == &dev_attr_microvolts.attr) {
4958 if ((ops->get_voltage && ops->get_voltage(rdev) >= 0) ||
4959 (ops->get_voltage_sel && ops->get_voltage_sel(rdev) >= 0) ||
4960 (ops->list_voltage && ops->list_voltage(rdev, 0) >= 0) ||
4961 (rdev->desc->fixed_uV && rdev->desc->n_voltages == 1))
4966 if (attr == &dev_attr_microamps.attr)
4967 return ops->get_current_limit ? mode : 0;
4969 if (attr == &dev_attr_opmode.attr)
4970 return ops->get_mode ? mode : 0;
4972 if (attr == &dev_attr_state.attr)
4973 return (rdev->ena_pin || ops->is_enabled) ? mode : 0;
4975 if (attr == &dev_attr_status.attr)
4976 return ops->get_status ? mode : 0;
4978 if (attr == &dev_attr_bypass.attr)
4979 return ops->get_bypass ? mode : 0;
4981 /* constraints need specific supporting methods */
4982 if (attr == &dev_attr_min_microvolts.attr ||
4983 attr == &dev_attr_max_microvolts.attr)
4984 return (ops->set_voltage || ops->set_voltage_sel) ? mode : 0;
4986 if (attr == &dev_attr_min_microamps.attr ||
4987 attr == &dev_attr_max_microamps.attr)
4988 return ops->set_current_limit ? mode : 0;
4990 if (attr == &dev_attr_suspend_standby_state.attr ||
4991 attr == &dev_attr_suspend_mem_state.attr ||
4992 attr == &dev_attr_suspend_disk_state.attr)
4995 if (attr == &dev_attr_suspend_standby_microvolts.attr ||
4996 attr == &dev_attr_suspend_mem_microvolts.attr ||
4997 attr == &dev_attr_suspend_disk_microvolts.attr)
4998 return ops->set_suspend_voltage ? mode : 0;
5000 if (attr == &dev_attr_suspend_standby_mode.attr ||
5001 attr == &dev_attr_suspend_mem_mode.attr ||
5002 attr == &dev_attr_suspend_disk_mode.attr)
5003 return ops->set_suspend_mode ? mode : 0;
5008 static const struct attribute_group regulator_dev_group = {
5009 .attrs = regulator_dev_attrs,
5010 .is_visible = regulator_attr_is_visible,
5013 static const struct attribute_group *regulator_dev_groups[] = {
5014 ®ulator_dev_group,
5018 static void regulator_dev_release(struct device *dev)
5020 struct regulator_dev *rdev = dev_get_drvdata(dev);
5022 debugfs_remove_recursive(rdev->debugfs);
5023 kfree(rdev->constraints);
5024 of_node_put(rdev->dev.of_node);
5028 static void rdev_init_debugfs(struct regulator_dev *rdev)
5030 struct device *parent = rdev->dev.parent;
5031 const char *rname = rdev_get_name(rdev);
5032 char name[NAME_MAX];
5034 /* Avoid duplicate debugfs directory names */
5035 if (parent && rname == rdev->desc->name) {
5036 snprintf(name, sizeof(name), "%s-%s", dev_name(parent),
5041 rdev->debugfs = debugfs_create_dir(rname, debugfs_root);
5042 if (IS_ERR(rdev->debugfs))
5043 rdev_dbg(rdev, "Failed to create debugfs directory\n");
5045 debugfs_create_u32("use_count", 0444, rdev->debugfs,
5047 debugfs_create_u32("open_count", 0444, rdev->debugfs,
5049 debugfs_create_u32("bypass_count", 0444, rdev->debugfs,
5050 &rdev->bypass_count);
5053 static int regulator_register_resolve_supply(struct device *dev, void *data)
5055 struct regulator_dev *rdev = dev_to_rdev(dev);
5057 if (regulator_resolve_supply(rdev))
5058 rdev_dbg(rdev, "unable to resolve supply\n");
5063 int regulator_coupler_register(struct regulator_coupler *coupler)
5065 mutex_lock(®ulator_list_mutex);
5066 list_add_tail(&coupler->list, ®ulator_coupler_list);
5067 mutex_unlock(®ulator_list_mutex);
5072 static struct regulator_coupler *
5073 regulator_find_coupler(struct regulator_dev *rdev)
5075 struct regulator_coupler *coupler;
5079 * Note that regulators are appended to the list and the generic
5080 * coupler is registered first, hence it will be attached at last
5083 list_for_each_entry_reverse(coupler, ®ulator_coupler_list, list) {
5084 err = coupler->attach_regulator(coupler, rdev);
5086 if (!coupler->balance_voltage &&
5087 rdev->coupling_desc.n_coupled > 2)
5088 goto err_unsupported;
5094 return ERR_PTR(err);
5102 return ERR_PTR(-EINVAL);
5105 if (coupler->detach_regulator)
5106 coupler->detach_regulator(coupler, rdev);
5109 "Voltage balancing for multiple regulator couples is unimplemented\n");
5111 return ERR_PTR(-EPERM);
5114 static void regulator_resolve_coupling(struct regulator_dev *rdev)
5116 struct regulator_coupler *coupler = rdev->coupling_desc.coupler;
5117 struct coupling_desc *c_desc = &rdev->coupling_desc;
5118 int n_coupled = c_desc->n_coupled;
5119 struct regulator_dev *c_rdev;
5122 for (i = 1; i < n_coupled; i++) {
5123 /* already resolved */
5124 if (c_desc->coupled_rdevs[i])
5127 c_rdev = of_parse_coupled_regulator(rdev, i - 1);
5132 if (c_rdev->coupling_desc.coupler != coupler) {
5133 rdev_err(rdev, "coupler mismatch with %s\n",
5134 rdev_get_name(c_rdev));
5138 c_desc->coupled_rdevs[i] = c_rdev;
5139 c_desc->n_resolved++;
5141 regulator_resolve_coupling(c_rdev);
5145 static void regulator_remove_coupling(struct regulator_dev *rdev)
5147 struct regulator_coupler *coupler = rdev->coupling_desc.coupler;
5148 struct coupling_desc *__c_desc, *c_desc = &rdev->coupling_desc;
5149 struct regulator_dev *__c_rdev, *c_rdev;
5150 unsigned int __n_coupled, n_coupled;
5154 n_coupled = c_desc->n_coupled;
5156 for (i = 1; i < n_coupled; i++) {
5157 c_rdev = c_desc->coupled_rdevs[i];
5162 regulator_lock(c_rdev);
5164 __c_desc = &c_rdev->coupling_desc;
5165 __n_coupled = __c_desc->n_coupled;
5167 for (k = 1; k < __n_coupled; k++) {
5168 __c_rdev = __c_desc->coupled_rdevs[k];
5170 if (__c_rdev == rdev) {
5171 __c_desc->coupled_rdevs[k] = NULL;
5172 __c_desc->n_resolved--;
5177 regulator_unlock(c_rdev);
5179 c_desc->coupled_rdevs[i] = NULL;
5180 c_desc->n_resolved--;
5183 if (coupler && coupler->detach_regulator) {
5184 err = coupler->detach_regulator(coupler, rdev);
5186 rdev_err(rdev, "failed to detach from coupler: %pe\n",
5190 kfree(rdev->coupling_desc.coupled_rdevs);
5191 rdev->coupling_desc.coupled_rdevs = NULL;
5194 static int regulator_init_coupling(struct regulator_dev *rdev)
5196 struct regulator_dev **coupled;
5197 int err, n_phandles;
5199 if (!IS_ENABLED(CONFIG_OF))
5202 n_phandles = of_get_n_coupled(rdev);
5204 coupled = kcalloc(n_phandles + 1, sizeof(*coupled), GFP_KERNEL);
5208 rdev->coupling_desc.coupled_rdevs = coupled;
5211 * Every regulator should always have coupling descriptor filled with
5212 * at least pointer to itself.
5214 rdev->coupling_desc.coupled_rdevs[0] = rdev;
5215 rdev->coupling_desc.n_coupled = n_phandles + 1;
5216 rdev->coupling_desc.n_resolved++;
5218 /* regulator isn't coupled */
5219 if (n_phandles == 0)
5222 if (!of_check_coupling_data(rdev))
5225 mutex_lock(®ulator_list_mutex);
5226 rdev->coupling_desc.coupler = regulator_find_coupler(rdev);
5227 mutex_unlock(®ulator_list_mutex);
5229 if (IS_ERR(rdev->coupling_desc.coupler)) {
5230 err = PTR_ERR(rdev->coupling_desc.coupler);
5231 rdev_err(rdev, "failed to get coupler: %pe\n", ERR_PTR(err));
5238 static int generic_coupler_attach(struct regulator_coupler *coupler,
5239 struct regulator_dev *rdev)
5241 if (rdev->coupling_desc.n_coupled > 2) {
5243 "Voltage balancing for multiple regulator couples is unimplemented\n");
5247 if (!rdev->constraints->always_on) {
5249 "Coupling of a non always-on regulator is unimplemented\n");
5256 static struct regulator_coupler generic_regulator_coupler = {
5257 .attach_regulator = generic_coupler_attach,
5261 * regulator_register - register regulator
5262 * @regulator_desc: regulator to register
5263 * @cfg: runtime configuration for regulator
5265 * Called by regulator drivers to register a regulator.
5266 * Returns a valid pointer to struct regulator_dev on success
5267 * or an ERR_PTR() on error.
5269 struct regulator_dev *
5270 regulator_register(const struct regulator_desc *regulator_desc,
5271 const struct regulator_config *cfg)
5273 const struct regulator_init_data *init_data;
5274 struct regulator_config *config = NULL;
5275 static atomic_t regulator_no = ATOMIC_INIT(-1);
5276 struct regulator_dev *rdev;
5277 bool dangling_cfg_gpiod = false;
5278 bool dangling_of_gpiod = false;
5283 return ERR_PTR(-EINVAL);
5285 dangling_cfg_gpiod = true;
5286 if (regulator_desc == NULL) {
5294 if (regulator_desc->name == NULL || regulator_desc->ops == NULL) {
5299 if (regulator_desc->type != REGULATOR_VOLTAGE &&
5300 regulator_desc->type != REGULATOR_CURRENT) {
5305 /* Only one of each should be implemented */
5306 WARN_ON(regulator_desc->ops->get_voltage &&
5307 regulator_desc->ops->get_voltage_sel);
5308 WARN_ON(regulator_desc->ops->set_voltage &&
5309 regulator_desc->ops->set_voltage_sel);
5311 /* If we're using selectors we must implement list_voltage. */
5312 if (regulator_desc->ops->get_voltage_sel &&
5313 !regulator_desc->ops->list_voltage) {
5317 if (regulator_desc->ops->set_voltage_sel &&
5318 !regulator_desc->ops->list_voltage) {
5323 rdev = kzalloc(sizeof(struct regulator_dev), GFP_KERNEL);
5328 device_initialize(&rdev->dev);
5331 * Duplicate the config so the driver could override it after
5332 * parsing init data.
5334 config = kmemdup(cfg, sizeof(*cfg), GFP_KERNEL);
5335 if (config == NULL) {
5340 init_data = regulator_of_get_init_data(dev, regulator_desc, config,
5341 &rdev->dev.of_node);
5344 * Sometimes not all resources are probed already so we need to take
5345 * that into account. This happens most the time if the ena_gpiod comes
5346 * from a gpio extender or something else.
5348 if (PTR_ERR(init_data) == -EPROBE_DEFER) {
5349 ret = -EPROBE_DEFER;
5354 * We need to keep track of any GPIO descriptor coming from the
5355 * device tree until we have handled it over to the core. If the
5356 * config that was passed in to this function DOES NOT contain
5357 * a descriptor, and the config after this call DOES contain
5358 * a descriptor, we definitely got one from parsing the device
5361 if (!cfg->ena_gpiod && config->ena_gpiod)
5362 dangling_of_gpiod = true;
5364 init_data = config->init_data;
5365 rdev->dev.of_node = of_node_get(config->of_node);
5368 ww_mutex_init(&rdev->mutex, ®ulator_ww_class);
5369 rdev->reg_data = config->driver_data;
5370 rdev->owner = regulator_desc->owner;
5371 rdev->desc = regulator_desc;
5373 rdev->regmap = config->regmap;
5374 else if (dev_get_regmap(dev, NULL))
5375 rdev->regmap = dev_get_regmap(dev, NULL);
5376 else if (dev->parent)
5377 rdev->regmap = dev_get_regmap(dev->parent, NULL);
5378 INIT_LIST_HEAD(&rdev->consumer_list);
5379 INIT_LIST_HEAD(&rdev->list);
5380 BLOCKING_INIT_NOTIFIER_HEAD(&rdev->notifier);
5381 INIT_DELAYED_WORK(&rdev->disable_work, regulator_disable_work);
5383 /* preform any regulator specific init */
5384 if (init_data && init_data->regulator_init) {
5385 ret = init_data->regulator_init(rdev->reg_data);
5390 if (config->ena_gpiod) {
5391 ret = regulator_ena_gpio_request(rdev, config);
5393 rdev_err(rdev, "Failed to request enable GPIO: %pe\n",
5397 /* The regulator core took over the GPIO descriptor */
5398 dangling_cfg_gpiod = false;
5399 dangling_of_gpiod = false;
5402 /* register with sysfs */
5403 rdev->dev.class = ®ulator_class;
5404 rdev->dev.parent = dev;
5405 dev_set_name(&rdev->dev, "regulator.%lu",
5406 (unsigned long) atomic_inc_return(®ulator_no));
5407 dev_set_drvdata(&rdev->dev, rdev);
5409 /* set regulator constraints */
5411 rdev->constraints = kmemdup(&init_data->constraints,
5412 sizeof(*rdev->constraints),
5415 rdev->constraints = kzalloc(sizeof(*rdev->constraints),
5417 if (!rdev->constraints) {
5422 if (init_data && init_data->supply_regulator)
5423 rdev->supply_name = init_data->supply_regulator;
5424 else if (regulator_desc->supply_name)
5425 rdev->supply_name = regulator_desc->supply_name;
5427 ret = set_machine_constraints(rdev);
5428 if (ret == -EPROBE_DEFER) {
5429 /* Regulator might be in bypass mode and so needs its supply
5430 * to set the constraints */
5431 /* FIXME: this currently triggers a chicken-and-egg problem
5432 * when creating -SUPPLY symlink in sysfs to a regulator
5433 * that is just being created */
5434 ret = regulator_resolve_supply(rdev);
5436 ret = set_machine_constraints(rdev);
5438 rdev_dbg(rdev, "unable to resolve supply early: %pe\n",
5444 ret = regulator_init_coupling(rdev);
5448 /* add consumers devices */
5450 for (i = 0; i < init_data->num_consumer_supplies; i++) {
5451 ret = set_consumer_device_supply(rdev,
5452 init_data->consumer_supplies[i].dev_name,
5453 init_data->consumer_supplies[i].supply);
5455 dev_err(dev, "Failed to set supply %s\n",
5456 init_data->consumer_supplies[i].supply);
5457 goto unset_supplies;
5462 if (!rdev->desc->ops->get_voltage &&
5463 !rdev->desc->ops->list_voltage &&
5464 !rdev->desc->fixed_uV)
5465 rdev->is_switch = true;
5467 ret = device_add(&rdev->dev);
5469 goto unset_supplies;
5471 rdev_init_debugfs(rdev);
5473 /* try to resolve regulators coupling since a new one was registered */
5474 mutex_lock(®ulator_list_mutex);
5475 regulator_resolve_coupling(rdev);
5476 mutex_unlock(®ulator_list_mutex);
5478 /* try to resolve regulators supply since a new one was registered */
5479 class_for_each_device(®ulator_class, NULL, NULL,
5480 regulator_register_resolve_supply);
5485 mutex_lock(®ulator_list_mutex);
5486 unset_regulator_supplies(rdev);
5487 regulator_remove_coupling(rdev);
5488 mutex_unlock(®ulator_list_mutex);
5490 regulator_put(rdev->supply);
5491 kfree(rdev->coupling_desc.coupled_rdevs);
5492 mutex_lock(®ulator_list_mutex);
5493 regulator_ena_gpio_free(rdev);
5494 mutex_unlock(®ulator_list_mutex);
5496 if (dangling_of_gpiod)
5497 gpiod_put(config->ena_gpiod);
5499 put_device(&rdev->dev);
5501 if (dangling_cfg_gpiod)
5502 gpiod_put(cfg->ena_gpiod);
5503 return ERR_PTR(ret);
5505 EXPORT_SYMBOL_GPL(regulator_register);
5508 * regulator_unregister - unregister regulator
5509 * @rdev: regulator to unregister
5511 * Called by regulator drivers to unregister a regulator.
5513 void regulator_unregister(struct regulator_dev *rdev)
5519 while (rdev->use_count--)
5520 regulator_disable(rdev->supply);
5521 regulator_put(rdev->supply);
5524 flush_work(&rdev->disable_work.work);
5526 mutex_lock(®ulator_list_mutex);
5528 WARN_ON(rdev->open_count);
5529 regulator_remove_coupling(rdev);
5530 unset_regulator_supplies(rdev);
5531 list_del(&rdev->list);
5532 regulator_ena_gpio_free(rdev);
5533 device_unregister(&rdev->dev);
5535 mutex_unlock(®ulator_list_mutex);
5537 EXPORT_SYMBOL_GPL(regulator_unregister);
5539 #ifdef CONFIG_SUSPEND
5541 * regulator_suspend - prepare regulators for system wide suspend
5542 * @dev: ``&struct device`` pointer that is passed to _regulator_suspend()
5544 * Configure each regulator with it's suspend operating parameters for state.
5546 static int regulator_suspend(struct device *dev)
5548 struct regulator_dev *rdev = dev_to_rdev(dev);
5549 suspend_state_t state = pm_suspend_target_state;
5551 const struct regulator_state *rstate;
5553 rstate = regulator_get_suspend_state_check(rdev, state);
5557 regulator_lock(rdev);
5558 ret = __suspend_set_state(rdev, rstate);
5559 regulator_unlock(rdev);
5564 static int regulator_resume(struct device *dev)
5566 suspend_state_t state = pm_suspend_target_state;
5567 struct regulator_dev *rdev = dev_to_rdev(dev);
5568 struct regulator_state *rstate;
5571 rstate = regulator_get_suspend_state(rdev, state);
5575 /* Avoid grabbing the lock if we don't need to */
5576 if (!rdev->desc->ops->resume)
5579 regulator_lock(rdev);
5581 if (rstate->enabled == ENABLE_IN_SUSPEND ||
5582 rstate->enabled == DISABLE_IN_SUSPEND)
5583 ret = rdev->desc->ops->resume(rdev);
5585 regulator_unlock(rdev);
5589 #else /* !CONFIG_SUSPEND */
5591 #define regulator_suspend NULL
5592 #define regulator_resume NULL
5594 #endif /* !CONFIG_SUSPEND */
5597 static const struct dev_pm_ops __maybe_unused regulator_pm_ops = {
5598 .suspend = regulator_suspend,
5599 .resume = regulator_resume,
5603 struct class regulator_class = {
5604 .name = "regulator",
5605 .dev_release = regulator_dev_release,
5606 .dev_groups = regulator_dev_groups,
5608 .pm = ®ulator_pm_ops,
5612 * regulator_has_full_constraints - the system has fully specified constraints
5614 * Calling this function will cause the regulator API to disable all
5615 * regulators which have a zero use count and don't have an always_on
5616 * constraint in a late_initcall.
5618 * The intention is that this will become the default behaviour in a
5619 * future kernel release so users are encouraged to use this facility
5622 void regulator_has_full_constraints(void)
5624 has_full_constraints = 1;
5626 EXPORT_SYMBOL_GPL(regulator_has_full_constraints);
5629 * rdev_get_drvdata - get rdev regulator driver data
5632 * Get rdev regulator driver private data. This call can be used in the
5633 * regulator driver context.
5635 void *rdev_get_drvdata(struct regulator_dev *rdev)
5637 return rdev->reg_data;
5639 EXPORT_SYMBOL_GPL(rdev_get_drvdata);
5642 * regulator_get_drvdata - get regulator driver data
5643 * @regulator: regulator
5645 * Get regulator driver private data. This call can be used in the consumer
5646 * driver context when non API regulator specific functions need to be called.
5648 void *regulator_get_drvdata(struct regulator *regulator)
5650 return regulator->rdev->reg_data;
5652 EXPORT_SYMBOL_GPL(regulator_get_drvdata);
5655 * regulator_set_drvdata - set regulator driver data
5656 * @regulator: regulator
5659 void regulator_set_drvdata(struct regulator *regulator, void *data)
5661 regulator->rdev->reg_data = data;
5663 EXPORT_SYMBOL_GPL(regulator_set_drvdata);
5666 * regulator_get_id - get regulator ID
5669 int rdev_get_id(struct regulator_dev *rdev)
5671 return rdev->desc->id;
5673 EXPORT_SYMBOL_GPL(rdev_get_id);
5675 struct device *rdev_get_dev(struct regulator_dev *rdev)
5679 EXPORT_SYMBOL_GPL(rdev_get_dev);
5681 struct regmap *rdev_get_regmap(struct regulator_dev *rdev)
5683 return rdev->regmap;
5685 EXPORT_SYMBOL_GPL(rdev_get_regmap);
5687 void *regulator_get_init_drvdata(struct regulator_init_data *reg_init_data)
5689 return reg_init_data->driver_data;
5691 EXPORT_SYMBOL_GPL(regulator_get_init_drvdata);
5693 #ifdef CONFIG_DEBUG_FS
5694 static int supply_map_show(struct seq_file *sf, void *data)
5696 struct regulator_map *map;
5698 list_for_each_entry(map, ®ulator_map_list, list) {
5699 seq_printf(sf, "%s -> %s.%s\n",
5700 rdev_get_name(map->regulator), map->dev_name,
5706 DEFINE_SHOW_ATTRIBUTE(supply_map);
5708 struct summary_data {
5710 struct regulator_dev *parent;
5714 static void regulator_summary_show_subtree(struct seq_file *s,
5715 struct regulator_dev *rdev,
5718 static int regulator_summary_show_children(struct device *dev, void *data)
5720 struct regulator_dev *rdev = dev_to_rdev(dev);
5721 struct summary_data *summary_data = data;
5723 if (rdev->supply && rdev->supply->rdev == summary_data->parent)
5724 regulator_summary_show_subtree(summary_data->s, rdev,
5725 summary_data->level + 1);
5730 static void regulator_summary_show_subtree(struct seq_file *s,
5731 struct regulator_dev *rdev,
5734 struct regulation_constraints *c;
5735 struct regulator *consumer;
5736 struct summary_data summary_data;
5737 unsigned int opmode;
5742 opmode = _regulator_get_mode_unlocked(rdev);
5743 seq_printf(s, "%*s%-*s %3d %4d %6d %7s ",
5745 30 - level * 3, rdev_get_name(rdev),
5746 rdev->use_count, rdev->open_count, rdev->bypass_count,
5747 regulator_opmode_to_str(opmode));
5749 seq_printf(s, "%5dmV ", regulator_get_voltage_rdev(rdev) / 1000);
5750 seq_printf(s, "%5dmA ",
5751 _regulator_get_current_limit_unlocked(rdev) / 1000);
5753 c = rdev->constraints;
5755 switch (rdev->desc->type) {
5756 case REGULATOR_VOLTAGE:
5757 seq_printf(s, "%5dmV %5dmV ",
5758 c->min_uV / 1000, c->max_uV / 1000);
5760 case REGULATOR_CURRENT:
5761 seq_printf(s, "%5dmA %5dmA ",
5762 c->min_uA / 1000, c->max_uA / 1000);
5769 list_for_each_entry(consumer, &rdev->consumer_list, list) {
5770 if (consumer->dev && consumer->dev->class == ®ulator_class)
5773 seq_printf(s, "%*s%-*s ",
5774 (level + 1) * 3 + 1, "",
5775 30 - (level + 1) * 3,
5776 consumer->supply_name ? consumer->supply_name :
5777 consumer->dev ? dev_name(consumer->dev) : "deviceless");
5779 switch (rdev->desc->type) {
5780 case REGULATOR_VOLTAGE:
5781 seq_printf(s, "%3d %33dmA%c%5dmV %5dmV",
5782 consumer->enable_count,
5783 consumer->uA_load / 1000,
5784 consumer->uA_load && !consumer->enable_count ?
5786 consumer->voltage[PM_SUSPEND_ON].min_uV / 1000,
5787 consumer->voltage[PM_SUSPEND_ON].max_uV / 1000);
5789 case REGULATOR_CURRENT:
5797 summary_data.level = level;
5798 summary_data.parent = rdev;
5800 class_for_each_device(®ulator_class, NULL, &summary_data,
5801 regulator_summary_show_children);
5804 struct summary_lock_data {
5805 struct ww_acquire_ctx *ww_ctx;
5806 struct regulator_dev **new_contended_rdev;
5807 struct regulator_dev **old_contended_rdev;
5810 static int regulator_summary_lock_one(struct device *dev, void *data)
5812 struct regulator_dev *rdev = dev_to_rdev(dev);
5813 struct summary_lock_data *lock_data = data;
5816 if (rdev != *lock_data->old_contended_rdev) {
5817 ret = regulator_lock_nested(rdev, lock_data->ww_ctx);
5819 if (ret == -EDEADLK)
5820 *lock_data->new_contended_rdev = rdev;
5824 *lock_data->old_contended_rdev = NULL;
5830 static int regulator_summary_unlock_one(struct device *dev, void *data)
5832 struct regulator_dev *rdev = dev_to_rdev(dev);
5833 struct summary_lock_data *lock_data = data;
5836 if (rdev == *lock_data->new_contended_rdev)
5840 regulator_unlock(rdev);
5845 static int regulator_summary_lock_all(struct ww_acquire_ctx *ww_ctx,
5846 struct regulator_dev **new_contended_rdev,
5847 struct regulator_dev **old_contended_rdev)
5849 struct summary_lock_data lock_data;
5852 lock_data.ww_ctx = ww_ctx;
5853 lock_data.new_contended_rdev = new_contended_rdev;
5854 lock_data.old_contended_rdev = old_contended_rdev;
5856 ret = class_for_each_device(®ulator_class, NULL, &lock_data,
5857 regulator_summary_lock_one);
5859 class_for_each_device(®ulator_class, NULL, &lock_data,
5860 regulator_summary_unlock_one);
5865 static void regulator_summary_lock(struct ww_acquire_ctx *ww_ctx)
5867 struct regulator_dev *new_contended_rdev = NULL;
5868 struct regulator_dev *old_contended_rdev = NULL;
5871 mutex_lock(®ulator_list_mutex);
5873 ww_acquire_init(ww_ctx, ®ulator_ww_class);
5876 if (new_contended_rdev) {
5877 ww_mutex_lock_slow(&new_contended_rdev->mutex, ww_ctx);
5878 old_contended_rdev = new_contended_rdev;
5879 old_contended_rdev->ref_cnt++;
5880 old_contended_rdev->mutex_owner = current;
5883 err = regulator_summary_lock_all(ww_ctx,
5884 &new_contended_rdev,
5885 &old_contended_rdev);
5887 if (old_contended_rdev)
5888 regulator_unlock(old_contended_rdev);
5890 } while (err == -EDEADLK);
5892 ww_acquire_done(ww_ctx);
5895 static void regulator_summary_unlock(struct ww_acquire_ctx *ww_ctx)
5897 class_for_each_device(®ulator_class, NULL, NULL,
5898 regulator_summary_unlock_one);
5899 ww_acquire_fini(ww_ctx);
5901 mutex_unlock(®ulator_list_mutex);
5904 static int regulator_summary_show_roots(struct device *dev, void *data)
5906 struct regulator_dev *rdev = dev_to_rdev(dev);
5907 struct seq_file *s = data;
5910 regulator_summary_show_subtree(s, rdev, 0);
5915 static int regulator_summary_show(struct seq_file *s, void *data)
5917 struct ww_acquire_ctx ww_ctx;
5919 seq_puts(s, " regulator use open bypass opmode voltage current min max\n");
5920 seq_puts(s, "---------------------------------------------------------------------------------------\n");
5922 regulator_summary_lock(&ww_ctx);
5924 class_for_each_device(®ulator_class, NULL, s,
5925 regulator_summary_show_roots);
5927 regulator_summary_unlock(&ww_ctx);
5931 DEFINE_SHOW_ATTRIBUTE(regulator_summary);
5932 #endif /* CONFIG_DEBUG_FS */
5934 static int __init regulator_init(void)
5938 ret = class_register(®ulator_class);
5940 debugfs_root = debugfs_create_dir("regulator", NULL);
5941 if (IS_ERR(debugfs_root))
5942 pr_debug("regulator: Failed to create debugfs directory\n");
5944 #ifdef CONFIG_DEBUG_FS
5945 debugfs_create_file("supply_map", 0444, debugfs_root, NULL,
5948 debugfs_create_file("regulator_summary", 0444, debugfs_root,
5949 NULL, ®ulator_summary_fops);
5951 regulator_dummy_init();
5953 regulator_coupler_register(&generic_regulator_coupler);
5958 /* init early to allow our consumers to complete system booting */
5959 core_initcall(regulator_init);
5961 static int regulator_late_cleanup(struct device *dev, void *data)
5963 struct regulator_dev *rdev = dev_to_rdev(dev);
5964 struct regulation_constraints *c = rdev->constraints;
5967 if (c && c->always_on)
5970 if (!regulator_ops_is_valid(rdev, REGULATOR_CHANGE_STATUS))
5973 regulator_lock(rdev);
5975 if (rdev->use_count)
5978 /* If reading the status failed, assume that it's off. */
5979 if (_regulator_is_enabled(rdev) <= 0)
5982 if (have_full_constraints()) {
5983 /* We log since this may kill the system if it goes
5985 rdev_info(rdev, "disabling\n");
5986 ret = _regulator_do_disable(rdev);
5988 rdev_err(rdev, "couldn't disable: %pe\n", ERR_PTR(ret));
5990 /* The intention is that in future we will
5991 * assume that full constraints are provided
5992 * so warn even if we aren't going to do
5995 rdev_warn(rdev, "incomplete constraints, leaving on\n");
5999 regulator_unlock(rdev);
6004 static void regulator_init_complete_work_function(struct work_struct *work)
6007 * Regulators may had failed to resolve their input supplies
6008 * when were registered, either because the input supply was
6009 * not registered yet or because its parent device was not
6010 * bound yet. So attempt to resolve the input supplies for
6011 * pending regulators before trying to disable unused ones.
6013 class_for_each_device(®ulator_class, NULL, NULL,
6014 regulator_register_resolve_supply);
6016 /* If we have a full configuration then disable any regulators
6017 * we have permission to change the status for and which are
6018 * not in use or always_on. This is effectively the default
6019 * for DT and ACPI as they have full constraints.
6021 class_for_each_device(®ulator_class, NULL, NULL,
6022 regulator_late_cleanup);
6025 static DECLARE_DELAYED_WORK(regulator_init_complete_work,
6026 regulator_init_complete_work_function);
6028 static int __init regulator_init_complete(void)
6031 * Since DT doesn't provide an idiomatic mechanism for
6032 * enabling full constraints and since it's much more natural
6033 * with DT to provide them just assume that a DT enabled
6034 * system has full constraints.
6036 if (of_have_populated_dt())
6037 has_full_constraints = true;
6040 * We punt completion for an arbitrary amount of time since
6041 * systems like distros will load many drivers from userspace
6042 * so consumers might not always be ready yet, this is
6043 * particularly an issue with laptops where this might bounce
6044 * the display off then on. Ideally we'd get a notification
6045 * from userspace when this happens but we don't so just wait
6046 * a bit and hope we waited long enough. It'd be better if
6047 * we'd only do this on systems that need it, and a kernel
6048 * command line option might be useful.
6050 schedule_delayed_work(®ulator_init_complete_work,
6051 msecs_to_jiffies(30000));
6055 late_initcall_sync(regulator_init_complete);