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 _regulator_put(struct regulator *regulator);
110 const char *rdev_get_name(struct regulator_dev *rdev)
112 if (rdev->constraints && rdev->constraints->name)
113 return rdev->constraints->name;
114 else if (rdev->desc->name)
115 return rdev->desc->name;
120 static bool have_full_constraints(void)
122 return has_full_constraints || of_have_populated_dt();
125 static bool regulator_ops_is_valid(struct regulator_dev *rdev, int ops)
127 if (!rdev->constraints) {
128 rdev_err(rdev, "no constraints\n");
132 if (rdev->constraints->valid_ops_mask & ops)
139 * regulator_lock_nested - lock a single regulator
140 * @rdev: regulator source
141 * @ww_ctx: w/w mutex acquire context
143 * This function can be called many times by one task on
144 * a single regulator and its mutex will be locked only
145 * once. If a task, which is calling this function is other
146 * than the one, which initially locked the mutex, it will
149 static inline int regulator_lock_nested(struct regulator_dev *rdev,
150 struct ww_acquire_ctx *ww_ctx)
155 mutex_lock(®ulator_nesting_mutex);
157 if (ww_ctx || !ww_mutex_trylock(&rdev->mutex)) {
158 if (rdev->mutex_owner == current)
164 mutex_unlock(®ulator_nesting_mutex);
165 ret = ww_mutex_lock(&rdev->mutex, ww_ctx);
166 mutex_lock(®ulator_nesting_mutex);
172 if (lock && ret != -EDEADLK) {
174 rdev->mutex_owner = current;
177 mutex_unlock(®ulator_nesting_mutex);
183 * regulator_lock - lock a single regulator
184 * @rdev: regulator source
186 * This function can be called many times by one task on
187 * a single regulator and its mutex will be locked only
188 * once. If a task, which is calling this function is other
189 * than the one, which initially locked the mutex, it will
192 void regulator_lock(struct regulator_dev *rdev)
194 regulator_lock_nested(rdev, NULL);
196 EXPORT_SYMBOL_GPL(regulator_lock);
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 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);
218 EXPORT_SYMBOL_GPL(regulator_unlock);
220 static bool regulator_supply_is_couple(struct regulator_dev *rdev)
222 struct regulator_dev *c_rdev;
225 for (i = 1; i < rdev->coupling_desc.n_coupled; i++) {
226 c_rdev = rdev->coupling_desc.coupled_rdevs[i];
228 if (rdev->supply->rdev == c_rdev)
235 static void regulator_unlock_recursive(struct regulator_dev *rdev,
236 unsigned int n_coupled)
238 struct regulator_dev *c_rdev;
241 for (i = n_coupled; i > 0; i--) {
242 c_rdev = rdev->coupling_desc.coupled_rdevs[i - 1];
247 if (c_rdev->supply && !regulator_supply_is_couple(c_rdev))
248 regulator_unlock_recursive(
249 c_rdev->supply->rdev,
250 c_rdev->coupling_desc.n_coupled);
252 regulator_unlock(c_rdev);
256 static int regulator_lock_recursive(struct regulator_dev *rdev,
257 struct regulator_dev **new_contended_rdev,
258 struct regulator_dev **old_contended_rdev,
259 struct ww_acquire_ctx *ww_ctx)
261 struct regulator_dev *c_rdev;
264 for (i = 0; i < rdev->coupling_desc.n_coupled; i++) {
265 c_rdev = rdev->coupling_desc.coupled_rdevs[i];
270 if (c_rdev != *old_contended_rdev) {
271 err = regulator_lock_nested(c_rdev, ww_ctx);
273 if (err == -EDEADLK) {
274 *new_contended_rdev = c_rdev;
278 /* shouldn't happen */
279 WARN_ON_ONCE(err != -EALREADY);
282 *old_contended_rdev = NULL;
285 if (c_rdev->supply && !regulator_supply_is_couple(c_rdev)) {
286 err = regulator_lock_recursive(c_rdev->supply->rdev,
291 regulator_unlock(c_rdev);
300 regulator_unlock_recursive(rdev, i);
306 * regulator_unlock_dependent - unlock regulator's suppliers and coupled
308 * @rdev: regulator source
309 * @ww_ctx: w/w mutex acquire context
311 * Unlock all regulators related with rdev by coupling or supplying.
313 static void regulator_unlock_dependent(struct regulator_dev *rdev,
314 struct ww_acquire_ctx *ww_ctx)
316 regulator_unlock_recursive(rdev, rdev->coupling_desc.n_coupled);
317 ww_acquire_fini(ww_ctx);
321 * regulator_lock_dependent - lock regulator's suppliers and coupled regulators
322 * @rdev: regulator source
323 * @ww_ctx: w/w mutex acquire context
325 * This function as a wrapper on regulator_lock_recursive(), which locks
326 * all regulators related with rdev by coupling or supplying.
328 static void regulator_lock_dependent(struct regulator_dev *rdev,
329 struct ww_acquire_ctx *ww_ctx)
331 struct regulator_dev *new_contended_rdev = NULL;
332 struct regulator_dev *old_contended_rdev = NULL;
335 mutex_lock(®ulator_list_mutex);
337 ww_acquire_init(ww_ctx, ®ulator_ww_class);
340 if (new_contended_rdev) {
341 ww_mutex_lock_slow(&new_contended_rdev->mutex, ww_ctx);
342 old_contended_rdev = new_contended_rdev;
343 old_contended_rdev->ref_cnt++;
346 err = regulator_lock_recursive(rdev,
351 if (old_contended_rdev)
352 regulator_unlock(old_contended_rdev);
354 } while (err == -EDEADLK);
356 ww_acquire_done(ww_ctx);
358 mutex_unlock(®ulator_list_mutex);
362 * of_get_child_regulator - get a child regulator device node
363 * based on supply name
364 * @parent: Parent device node
365 * @prop_name: Combination regulator supply name and "-supply"
367 * Traverse all child nodes.
368 * Extract the child regulator device node corresponding to the supply name.
369 * returns the device node corresponding to the regulator if found, else
372 static struct device_node *of_get_child_regulator(struct device_node *parent,
373 const char *prop_name)
375 struct device_node *regnode = NULL;
376 struct device_node *child = NULL;
378 for_each_child_of_node(parent, child) {
379 regnode = of_parse_phandle(child, prop_name, 0);
382 regnode = of_get_child_regulator(child, prop_name);
397 * of_get_regulator - get a regulator device node based on supply name
398 * @dev: Device pointer for the consumer (of regulator) device
399 * @supply: regulator supply name
401 * Extract the regulator device node corresponding to the supply name.
402 * returns the device node corresponding to the regulator if found, else
405 static struct device_node *of_get_regulator(struct device *dev, const char *supply)
407 struct device_node *regnode = NULL;
408 char prop_name[32]; /* 32 is max size of property name */
410 dev_dbg(dev, "Looking up %s-supply from device tree\n", supply);
412 snprintf(prop_name, 32, "%s-supply", supply);
413 regnode = of_parse_phandle(dev->of_node, prop_name, 0);
416 regnode = of_get_child_regulator(dev->of_node, prop_name);
420 dev_dbg(dev, "Looking up %s property in node %pOF failed\n",
421 prop_name, dev->of_node);
427 /* Platform voltage constraint check */
428 int regulator_check_voltage(struct regulator_dev *rdev,
429 int *min_uV, int *max_uV)
431 BUG_ON(*min_uV > *max_uV);
433 if (!regulator_ops_is_valid(rdev, REGULATOR_CHANGE_VOLTAGE)) {
434 rdev_err(rdev, "voltage operation not allowed\n");
438 if (*max_uV > rdev->constraints->max_uV)
439 *max_uV = rdev->constraints->max_uV;
440 if (*min_uV < rdev->constraints->min_uV)
441 *min_uV = rdev->constraints->min_uV;
443 if (*min_uV > *max_uV) {
444 rdev_err(rdev, "unsupportable voltage range: %d-%duV\n",
452 /* return 0 if the state is valid */
453 static int regulator_check_states(suspend_state_t state)
455 return (state > PM_SUSPEND_MAX || state == PM_SUSPEND_TO_IDLE);
458 /* Make sure we select a voltage that suits the needs of all
459 * regulator consumers
461 int regulator_check_consumers(struct regulator_dev *rdev,
462 int *min_uV, int *max_uV,
463 suspend_state_t state)
465 struct regulator *regulator;
466 struct regulator_voltage *voltage;
468 list_for_each_entry(regulator, &rdev->consumer_list, list) {
469 voltage = ®ulator->voltage[state];
471 * Assume consumers that didn't say anything are OK
472 * with anything in the constraint range.
474 if (!voltage->min_uV && !voltage->max_uV)
477 if (*max_uV > voltage->max_uV)
478 *max_uV = voltage->max_uV;
479 if (*min_uV < voltage->min_uV)
480 *min_uV = voltage->min_uV;
483 if (*min_uV > *max_uV) {
484 rdev_err(rdev, "Restricting voltage, %u-%uuV\n",
492 /* current constraint check */
493 static int regulator_check_current_limit(struct regulator_dev *rdev,
494 int *min_uA, int *max_uA)
496 BUG_ON(*min_uA > *max_uA);
498 if (!regulator_ops_is_valid(rdev, REGULATOR_CHANGE_CURRENT)) {
499 rdev_err(rdev, "current operation not allowed\n");
503 if (*max_uA > rdev->constraints->max_uA)
504 *max_uA = rdev->constraints->max_uA;
505 if (*min_uA < rdev->constraints->min_uA)
506 *min_uA = rdev->constraints->min_uA;
508 if (*min_uA > *max_uA) {
509 rdev_err(rdev, "unsupportable current range: %d-%duA\n",
517 /* operating mode constraint check */
518 static int regulator_mode_constrain(struct regulator_dev *rdev,
522 case REGULATOR_MODE_FAST:
523 case REGULATOR_MODE_NORMAL:
524 case REGULATOR_MODE_IDLE:
525 case REGULATOR_MODE_STANDBY:
528 rdev_err(rdev, "invalid mode %x specified\n", *mode);
532 if (!regulator_ops_is_valid(rdev, REGULATOR_CHANGE_MODE)) {
533 rdev_err(rdev, "mode operation not allowed\n");
537 /* The modes are bitmasks, the most power hungry modes having
538 * the lowest values. If the requested mode isn't supported
539 * try higher modes. */
541 if (rdev->constraints->valid_modes_mask & *mode)
549 static inline struct regulator_state *
550 regulator_get_suspend_state(struct regulator_dev *rdev, suspend_state_t state)
552 if (rdev->constraints == NULL)
556 case PM_SUSPEND_STANDBY:
557 return &rdev->constraints->state_standby;
559 return &rdev->constraints->state_mem;
561 return &rdev->constraints->state_disk;
567 static ssize_t regulator_uV_show(struct device *dev,
568 struct device_attribute *attr, char *buf)
570 struct regulator_dev *rdev = dev_get_drvdata(dev);
573 regulator_lock(rdev);
574 uV = regulator_get_voltage_rdev(rdev);
575 regulator_unlock(rdev);
579 return sprintf(buf, "%d\n", uV);
581 static DEVICE_ATTR(microvolts, 0444, regulator_uV_show, NULL);
583 static ssize_t regulator_uA_show(struct device *dev,
584 struct device_attribute *attr, char *buf)
586 struct regulator_dev *rdev = dev_get_drvdata(dev);
588 return sprintf(buf, "%d\n", _regulator_get_current_limit(rdev));
590 static DEVICE_ATTR(microamps, 0444, regulator_uA_show, NULL);
592 static ssize_t name_show(struct device *dev, struct device_attribute *attr,
595 struct regulator_dev *rdev = dev_get_drvdata(dev);
597 return sprintf(buf, "%s\n", rdev_get_name(rdev));
599 static DEVICE_ATTR_RO(name);
601 static const char *regulator_opmode_to_str(int mode)
604 case REGULATOR_MODE_FAST:
606 case REGULATOR_MODE_NORMAL:
608 case REGULATOR_MODE_IDLE:
610 case REGULATOR_MODE_STANDBY:
616 static ssize_t regulator_print_opmode(char *buf, int mode)
618 return sprintf(buf, "%s\n", regulator_opmode_to_str(mode));
621 static ssize_t regulator_opmode_show(struct device *dev,
622 struct device_attribute *attr, char *buf)
624 struct regulator_dev *rdev = dev_get_drvdata(dev);
626 return regulator_print_opmode(buf, _regulator_get_mode(rdev));
628 static DEVICE_ATTR(opmode, 0444, regulator_opmode_show, NULL);
630 static ssize_t regulator_print_state(char *buf, int state)
633 return sprintf(buf, "enabled\n");
635 return sprintf(buf, "disabled\n");
637 return sprintf(buf, "unknown\n");
640 static ssize_t regulator_state_show(struct device *dev,
641 struct device_attribute *attr, char *buf)
643 struct regulator_dev *rdev = dev_get_drvdata(dev);
646 regulator_lock(rdev);
647 ret = regulator_print_state(buf, _regulator_is_enabled(rdev));
648 regulator_unlock(rdev);
652 static DEVICE_ATTR(state, 0444, regulator_state_show, NULL);
654 static ssize_t regulator_status_show(struct device *dev,
655 struct device_attribute *attr, char *buf)
657 struct regulator_dev *rdev = dev_get_drvdata(dev);
661 status = rdev->desc->ops->get_status(rdev);
666 case REGULATOR_STATUS_OFF:
669 case REGULATOR_STATUS_ON:
672 case REGULATOR_STATUS_ERROR:
675 case REGULATOR_STATUS_FAST:
678 case REGULATOR_STATUS_NORMAL:
681 case REGULATOR_STATUS_IDLE:
684 case REGULATOR_STATUS_STANDBY:
687 case REGULATOR_STATUS_BYPASS:
690 case REGULATOR_STATUS_UNDEFINED:
697 return sprintf(buf, "%s\n", label);
699 static DEVICE_ATTR(status, 0444, regulator_status_show, NULL);
701 static ssize_t regulator_min_uA_show(struct device *dev,
702 struct device_attribute *attr, char *buf)
704 struct regulator_dev *rdev = dev_get_drvdata(dev);
706 if (!rdev->constraints)
707 return sprintf(buf, "constraint not defined\n");
709 return sprintf(buf, "%d\n", rdev->constraints->min_uA);
711 static DEVICE_ATTR(min_microamps, 0444, regulator_min_uA_show, NULL);
713 static ssize_t regulator_max_uA_show(struct device *dev,
714 struct device_attribute *attr, char *buf)
716 struct regulator_dev *rdev = dev_get_drvdata(dev);
718 if (!rdev->constraints)
719 return sprintf(buf, "constraint not defined\n");
721 return sprintf(buf, "%d\n", rdev->constraints->max_uA);
723 static DEVICE_ATTR(max_microamps, 0444, regulator_max_uA_show, NULL);
725 static ssize_t regulator_min_uV_show(struct device *dev,
726 struct device_attribute *attr, char *buf)
728 struct regulator_dev *rdev = dev_get_drvdata(dev);
730 if (!rdev->constraints)
731 return sprintf(buf, "constraint not defined\n");
733 return sprintf(buf, "%d\n", rdev->constraints->min_uV);
735 static DEVICE_ATTR(min_microvolts, 0444, regulator_min_uV_show, NULL);
737 static ssize_t regulator_max_uV_show(struct device *dev,
738 struct device_attribute *attr, char *buf)
740 struct regulator_dev *rdev = dev_get_drvdata(dev);
742 if (!rdev->constraints)
743 return sprintf(buf, "constraint not defined\n");
745 return sprintf(buf, "%d\n", rdev->constraints->max_uV);
747 static DEVICE_ATTR(max_microvolts, 0444, regulator_max_uV_show, NULL);
749 static ssize_t regulator_total_uA_show(struct device *dev,
750 struct device_attribute *attr, char *buf)
752 struct regulator_dev *rdev = dev_get_drvdata(dev);
753 struct regulator *regulator;
756 regulator_lock(rdev);
757 list_for_each_entry(regulator, &rdev->consumer_list, list) {
758 if (regulator->enable_count)
759 uA += regulator->uA_load;
761 regulator_unlock(rdev);
762 return sprintf(buf, "%d\n", uA);
764 static DEVICE_ATTR(requested_microamps, 0444, regulator_total_uA_show, NULL);
766 static ssize_t num_users_show(struct device *dev, struct device_attribute *attr,
769 struct regulator_dev *rdev = dev_get_drvdata(dev);
770 return sprintf(buf, "%d\n", rdev->use_count);
772 static DEVICE_ATTR_RO(num_users);
774 static ssize_t type_show(struct device *dev, struct device_attribute *attr,
777 struct regulator_dev *rdev = dev_get_drvdata(dev);
779 switch (rdev->desc->type) {
780 case REGULATOR_VOLTAGE:
781 return sprintf(buf, "voltage\n");
782 case REGULATOR_CURRENT:
783 return sprintf(buf, "current\n");
785 return sprintf(buf, "unknown\n");
787 static DEVICE_ATTR_RO(type);
789 static ssize_t regulator_suspend_mem_uV_show(struct device *dev,
790 struct device_attribute *attr, char *buf)
792 struct regulator_dev *rdev = dev_get_drvdata(dev);
794 return sprintf(buf, "%d\n", rdev->constraints->state_mem.uV);
796 static DEVICE_ATTR(suspend_mem_microvolts, 0444,
797 regulator_suspend_mem_uV_show, NULL);
799 static ssize_t regulator_suspend_disk_uV_show(struct device *dev,
800 struct device_attribute *attr, char *buf)
802 struct regulator_dev *rdev = dev_get_drvdata(dev);
804 return sprintf(buf, "%d\n", rdev->constraints->state_disk.uV);
806 static DEVICE_ATTR(suspend_disk_microvolts, 0444,
807 regulator_suspend_disk_uV_show, NULL);
809 static ssize_t regulator_suspend_standby_uV_show(struct device *dev,
810 struct device_attribute *attr, char *buf)
812 struct regulator_dev *rdev = dev_get_drvdata(dev);
814 return sprintf(buf, "%d\n", rdev->constraints->state_standby.uV);
816 static DEVICE_ATTR(suspend_standby_microvolts, 0444,
817 regulator_suspend_standby_uV_show, NULL);
819 static ssize_t regulator_suspend_mem_mode_show(struct device *dev,
820 struct device_attribute *attr, char *buf)
822 struct regulator_dev *rdev = dev_get_drvdata(dev);
824 return regulator_print_opmode(buf,
825 rdev->constraints->state_mem.mode);
827 static DEVICE_ATTR(suspend_mem_mode, 0444,
828 regulator_suspend_mem_mode_show, NULL);
830 static ssize_t regulator_suspend_disk_mode_show(struct device *dev,
831 struct device_attribute *attr, char *buf)
833 struct regulator_dev *rdev = dev_get_drvdata(dev);
835 return regulator_print_opmode(buf,
836 rdev->constraints->state_disk.mode);
838 static DEVICE_ATTR(suspend_disk_mode, 0444,
839 regulator_suspend_disk_mode_show, NULL);
841 static ssize_t regulator_suspend_standby_mode_show(struct device *dev,
842 struct device_attribute *attr, char *buf)
844 struct regulator_dev *rdev = dev_get_drvdata(dev);
846 return regulator_print_opmode(buf,
847 rdev->constraints->state_standby.mode);
849 static DEVICE_ATTR(suspend_standby_mode, 0444,
850 regulator_suspend_standby_mode_show, NULL);
852 static ssize_t regulator_suspend_mem_state_show(struct device *dev,
853 struct device_attribute *attr, char *buf)
855 struct regulator_dev *rdev = dev_get_drvdata(dev);
857 return regulator_print_state(buf,
858 rdev->constraints->state_mem.enabled);
860 static DEVICE_ATTR(suspend_mem_state, 0444,
861 regulator_suspend_mem_state_show, NULL);
863 static ssize_t regulator_suspend_disk_state_show(struct device *dev,
864 struct device_attribute *attr, char *buf)
866 struct regulator_dev *rdev = dev_get_drvdata(dev);
868 return regulator_print_state(buf,
869 rdev->constraints->state_disk.enabled);
871 static DEVICE_ATTR(suspend_disk_state, 0444,
872 regulator_suspend_disk_state_show, NULL);
874 static ssize_t regulator_suspend_standby_state_show(struct device *dev,
875 struct device_attribute *attr, char *buf)
877 struct regulator_dev *rdev = dev_get_drvdata(dev);
879 return regulator_print_state(buf,
880 rdev->constraints->state_standby.enabled);
882 static DEVICE_ATTR(suspend_standby_state, 0444,
883 regulator_suspend_standby_state_show, NULL);
885 static ssize_t regulator_bypass_show(struct device *dev,
886 struct device_attribute *attr, char *buf)
888 struct regulator_dev *rdev = dev_get_drvdata(dev);
893 ret = rdev->desc->ops->get_bypass(rdev, &bypass);
902 return sprintf(buf, "%s\n", report);
904 static DEVICE_ATTR(bypass, 0444,
905 regulator_bypass_show, NULL);
907 /* Calculate the new optimum regulator operating mode based on the new total
908 * consumer load. All locks held by caller */
909 static int drms_uA_update(struct regulator_dev *rdev)
911 struct regulator *sibling;
912 int current_uA = 0, output_uV, input_uV, err;
916 * first check to see if we can set modes at all, otherwise just
917 * tell the consumer everything is OK.
919 if (!regulator_ops_is_valid(rdev, REGULATOR_CHANGE_DRMS)) {
920 rdev_dbg(rdev, "DRMS operation not allowed\n");
924 if (!rdev->desc->ops->get_optimum_mode &&
925 !rdev->desc->ops->set_load)
928 if (!rdev->desc->ops->set_mode &&
929 !rdev->desc->ops->set_load)
932 /* calc total requested load */
933 list_for_each_entry(sibling, &rdev->consumer_list, list) {
934 if (sibling->enable_count)
935 current_uA += sibling->uA_load;
938 current_uA += rdev->constraints->system_load;
940 if (rdev->desc->ops->set_load) {
941 /* set the optimum mode for our new total regulator load */
942 err = rdev->desc->ops->set_load(rdev, current_uA);
944 rdev_err(rdev, "failed to set load %d\n", current_uA);
946 /* get output voltage */
947 output_uV = regulator_get_voltage_rdev(rdev);
948 if (output_uV <= 0) {
949 rdev_err(rdev, "invalid output voltage found\n");
953 /* get input voltage */
956 input_uV = regulator_get_voltage(rdev->supply);
958 input_uV = rdev->constraints->input_uV;
960 rdev_err(rdev, "invalid input voltage found\n");
964 /* now get the optimum mode for our new total regulator load */
965 mode = rdev->desc->ops->get_optimum_mode(rdev, input_uV,
966 output_uV, current_uA);
968 /* check the new mode is allowed */
969 err = regulator_mode_constrain(rdev, &mode);
971 rdev_err(rdev, "failed to get optimum mode @ %d uA %d -> %d uV\n",
972 current_uA, input_uV, output_uV);
976 err = rdev->desc->ops->set_mode(rdev, mode);
978 rdev_err(rdev, "failed to set optimum mode %x\n", mode);
984 static int suspend_set_state(struct regulator_dev *rdev,
985 suspend_state_t state)
988 struct regulator_state *rstate;
990 rstate = regulator_get_suspend_state(rdev, state);
994 /* If we have no suspend mode configuration don't set anything;
995 * only warn if the driver implements set_suspend_voltage or
996 * set_suspend_mode callback.
998 if (rstate->enabled != ENABLE_IN_SUSPEND &&
999 rstate->enabled != DISABLE_IN_SUSPEND) {
1000 if (rdev->desc->ops->set_suspend_voltage ||
1001 rdev->desc->ops->set_suspend_mode)
1002 rdev_warn(rdev, "No configuration\n");
1006 if (rstate->enabled == ENABLE_IN_SUSPEND &&
1007 rdev->desc->ops->set_suspend_enable)
1008 ret = rdev->desc->ops->set_suspend_enable(rdev);
1009 else if (rstate->enabled == DISABLE_IN_SUSPEND &&
1010 rdev->desc->ops->set_suspend_disable)
1011 ret = rdev->desc->ops->set_suspend_disable(rdev);
1012 else /* OK if set_suspend_enable or set_suspend_disable is NULL */
1016 rdev_err(rdev, "failed to enabled/disable\n");
1020 if (rdev->desc->ops->set_suspend_voltage && rstate->uV > 0) {
1021 ret = rdev->desc->ops->set_suspend_voltage(rdev, rstate->uV);
1023 rdev_err(rdev, "failed to set voltage\n");
1028 if (rdev->desc->ops->set_suspend_mode && rstate->mode > 0) {
1029 ret = rdev->desc->ops->set_suspend_mode(rdev, rstate->mode);
1031 rdev_err(rdev, "failed to set mode\n");
1039 static void print_constraints(struct regulator_dev *rdev)
1041 struct regulation_constraints *constraints = rdev->constraints;
1043 size_t len = sizeof(buf) - 1;
1047 if (constraints->min_uV && constraints->max_uV) {
1048 if (constraints->min_uV == constraints->max_uV)
1049 count += scnprintf(buf + count, len - count, "%d mV ",
1050 constraints->min_uV / 1000);
1052 count += scnprintf(buf + count, len - count,
1054 constraints->min_uV / 1000,
1055 constraints->max_uV / 1000);
1058 if (!constraints->min_uV ||
1059 constraints->min_uV != constraints->max_uV) {
1060 ret = regulator_get_voltage_rdev(rdev);
1062 count += scnprintf(buf + count, len - count,
1063 "at %d mV ", ret / 1000);
1066 if (constraints->uV_offset)
1067 count += scnprintf(buf + count, len - count, "%dmV offset ",
1068 constraints->uV_offset / 1000);
1070 if (constraints->min_uA && constraints->max_uA) {
1071 if (constraints->min_uA == constraints->max_uA)
1072 count += scnprintf(buf + count, len - count, "%d mA ",
1073 constraints->min_uA / 1000);
1075 count += scnprintf(buf + count, len - count,
1077 constraints->min_uA / 1000,
1078 constraints->max_uA / 1000);
1081 if (!constraints->min_uA ||
1082 constraints->min_uA != constraints->max_uA) {
1083 ret = _regulator_get_current_limit(rdev);
1085 count += scnprintf(buf + count, len - count,
1086 "at %d mA ", ret / 1000);
1089 if (constraints->valid_modes_mask & REGULATOR_MODE_FAST)
1090 count += scnprintf(buf + count, len - count, "fast ");
1091 if (constraints->valid_modes_mask & REGULATOR_MODE_NORMAL)
1092 count += scnprintf(buf + count, len - count, "normal ");
1093 if (constraints->valid_modes_mask & REGULATOR_MODE_IDLE)
1094 count += scnprintf(buf + count, len - count, "idle ");
1095 if (constraints->valid_modes_mask & REGULATOR_MODE_STANDBY)
1096 count += scnprintf(buf + count, len - count, "standby");
1099 scnprintf(buf, len, "no parameters");
1101 rdev_dbg(rdev, "%s\n", buf);
1103 if ((constraints->min_uV != constraints->max_uV) &&
1104 !regulator_ops_is_valid(rdev, REGULATOR_CHANGE_VOLTAGE))
1106 "Voltage range but no REGULATOR_CHANGE_VOLTAGE\n");
1109 static int machine_constraints_voltage(struct regulator_dev *rdev,
1110 struct regulation_constraints *constraints)
1112 const struct regulator_ops *ops = rdev->desc->ops;
1115 /* do we need to apply the constraint voltage */
1116 if (rdev->constraints->apply_uV &&
1117 rdev->constraints->min_uV && rdev->constraints->max_uV) {
1118 int target_min, target_max;
1119 int current_uV = regulator_get_voltage_rdev(rdev);
1121 if (current_uV == -ENOTRECOVERABLE) {
1122 /* This regulator can't be read and must be initialized */
1123 rdev_info(rdev, "Setting %d-%duV\n",
1124 rdev->constraints->min_uV,
1125 rdev->constraints->max_uV);
1126 _regulator_do_set_voltage(rdev,
1127 rdev->constraints->min_uV,
1128 rdev->constraints->max_uV);
1129 current_uV = regulator_get_voltage_rdev(rdev);
1132 if (current_uV < 0) {
1134 "failed to get the current voltage(%d)\n",
1140 * If we're below the minimum voltage move up to the
1141 * minimum voltage, if we're above the maximum voltage
1142 * then move down to the maximum.
1144 target_min = current_uV;
1145 target_max = current_uV;
1147 if (current_uV < rdev->constraints->min_uV) {
1148 target_min = rdev->constraints->min_uV;
1149 target_max = rdev->constraints->min_uV;
1152 if (current_uV > rdev->constraints->max_uV) {
1153 target_min = rdev->constraints->max_uV;
1154 target_max = rdev->constraints->max_uV;
1157 if (target_min != current_uV || target_max != current_uV) {
1158 rdev_info(rdev, "Bringing %duV into %d-%duV\n",
1159 current_uV, target_min, target_max);
1160 ret = _regulator_do_set_voltage(
1161 rdev, target_min, target_max);
1164 "failed to apply %d-%duV constraint(%d)\n",
1165 target_min, target_max, ret);
1171 /* constrain machine-level voltage specs to fit
1172 * the actual range supported by this regulator.
1174 if (ops->list_voltage && rdev->desc->n_voltages) {
1175 int count = rdev->desc->n_voltages;
1177 int min_uV = INT_MAX;
1178 int max_uV = INT_MIN;
1179 int cmin = constraints->min_uV;
1180 int cmax = constraints->max_uV;
1182 /* it's safe to autoconfigure fixed-voltage supplies
1183 and the constraints are used by list_voltage. */
1184 if (count == 1 && !cmin) {
1187 constraints->min_uV = cmin;
1188 constraints->max_uV = cmax;
1191 /* voltage constraints are optional */
1192 if ((cmin == 0) && (cmax == 0))
1195 /* else require explicit machine-level constraints */
1196 if (cmin <= 0 || cmax <= 0 || cmax < cmin) {
1197 rdev_err(rdev, "invalid voltage constraints\n");
1201 /* no need to loop voltages if range is continuous */
1202 if (rdev->desc->continuous_voltage_range)
1205 /* initial: [cmin..cmax] valid, [min_uV..max_uV] not */
1206 for (i = 0; i < count; i++) {
1209 value = ops->list_voltage(rdev, i);
1213 /* maybe adjust [min_uV..max_uV] */
1214 if (value >= cmin && value < min_uV)
1216 if (value <= cmax && value > max_uV)
1220 /* final: [min_uV..max_uV] valid iff constraints valid */
1221 if (max_uV < min_uV) {
1223 "unsupportable voltage constraints %u-%uuV\n",
1228 /* use regulator's subset of machine constraints */
1229 if (constraints->min_uV < min_uV) {
1230 rdev_dbg(rdev, "override min_uV, %d -> %d\n",
1231 constraints->min_uV, min_uV);
1232 constraints->min_uV = min_uV;
1234 if (constraints->max_uV > max_uV) {
1235 rdev_dbg(rdev, "override max_uV, %d -> %d\n",
1236 constraints->max_uV, max_uV);
1237 constraints->max_uV = max_uV;
1244 static int machine_constraints_current(struct regulator_dev *rdev,
1245 struct regulation_constraints *constraints)
1247 const struct regulator_ops *ops = rdev->desc->ops;
1250 if (!constraints->min_uA && !constraints->max_uA)
1253 if (constraints->min_uA > constraints->max_uA) {
1254 rdev_err(rdev, "Invalid current constraints\n");
1258 if (!ops->set_current_limit || !ops->get_current_limit) {
1259 rdev_warn(rdev, "Operation of current configuration missing\n");
1263 /* Set regulator current in constraints range */
1264 ret = ops->set_current_limit(rdev, constraints->min_uA,
1265 constraints->max_uA);
1267 rdev_err(rdev, "Failed to set current constraint, %d\n", ret);
1274 static int _regulator_do_enable(struct regulator_dev *rdev);
1277 * set_machine_constraints - sets regulator constraints
1278 * @rdev: regulator source
1279 * @constraints: constraints to apply
1281 * Allows platform initialisation code to define and constrain
1282 * regulator circuits e.g. valid voltage/current ranges, etc. NOTE:
1283 * Constraints *must* be set by platform code in order for some
1284 * regulator operations to proceed i.e. set_voltage, set_current_limit,
1287 static int set_machine_constraints(struct regulator_dev *rdev,
1288 const struct regulation_constraints *constraints)
1291 const struct regulator_ops *ops = rdev->desc->ops;
1294 rdev->constraints = kmemdup(constraints, sizeof(*constraints),
1297 rdev->constraints = kzalloc(sizeof(*constraints),
1299 if (!rdev->constraints)
1302 ret = machine_constraints_voltage(rdev, rdev->constraints);
1306 ret = machine_constraints_current(rdev, rdev->constraints);
1310 if (rdev->constraints->ilim_uA && ops->set_input_current_limit) {
1311 ret = ops->set_input_current_limit(rdev,
1312 rdev->constraints->ilim_uA);
1314 rdev_err(rdev, "failed to set input limit\n");
1319 /* do we need to setup our suspend state */
1320 if (rdev->constraints->initial_state) {
1321 ret = suspend_set_state(rdev, rdev->constraints->initial_state);
1323 rdev_err(rdev, "failed to set suspend state\n");
1328 if (rdev->constraints->initial_mode) {
1329 if (!ops->set_mode) {
1330 rdev_err(rdev, "no set_mode operation\n");
1334 ret = ops->set_mode(rdev, rdev->constraints->initial_mode);
1336 rdev_err(rdev, "failed to set initial mode: %d\n", ret);
1339 } else if (rdev->constraints->system_load) {
1341 * We'll only apply the initial system load if an
1342 * initial mode wasn't specified.
1344 drms_uA_update(rdev);
1347 if ((rdev->constraints->ramp_delay || rdev->constraints->ramp_disable)
1348 && ops->set_ramp_delay) {
1349 ret = ops->set_ramp_delay(rdev, rdev->constraints->ramp_delay);
1351 rdev_err(rdev, "failed to set ramp_delay\n");
1356 if (rdev->constraints->pull_down && ops->set_pull_down) {
1357 ret = ops->set_pull_down(rdev);
1359 rdev_err(rdev, "failed to set pull down\n");
1364 if (rdev->constraints->soft_start && ops->set_soft_start) {
1365 ret = ops->set_soft_start(rdev);
1367 rdev_err(rdev, "failed to set soft start\n");
1372 if (rdev->constraints->over_current_protection
1373 && ops->set_over_current_protection) {
1374 ret = ops->set_over_current_protection(rdev);
1376 rdev_err(rdev, "failed to set over current protection\n");
1381 if (rdev->constraints->active_discharge && ops->set_active_discharge) {
1382 bool ad_state = (rdev->constraints->active_discharge ==
1383 REGULATOR_ACTIVE_DISCHARGE_ENABLE) ? true : false;
1385 ret = ops->set_active_discharge(rdev, ad_state);
1387 rdev_err(rdev, "failed to set active discharge\n");
1392 /* If the constraints say the regulator should be on at this point
1393 * and we have control then make sure it is enabled.
1395 if (rdev->constraints->always_on || rdev->constraints->boot_on) {
1397 ret = regulator_enable(rdev->supply);
1399 _regulator_put(rdev->supply);
1400 rdev->supply = NULL;
1405 ret = _regulator_do_enable(rdev);
1406 if (ret < 0 && ret != -EINVAL) {
1407 rdev_err(rdev, "failed to enable\n");
1411 if (rdev->constraints->always_on)
1415 print_constraints(rdev);
1420 * set_supply - set regulator supply regulator
1421 * @rdev: regulator name
1422 * @supply_rdev: supply regulator name
1424 * Called by platform initialisation code to set the supply regulator for this
1425 * regulator. This ensures that a regulators supply will also be enabled by the
1426 * core if it's child is enabled.
1428 static int set_supply(struct regulator_dev *rdev,
1429 struct regulator_dev *supply_rdev)
1433 rdev_info(rdev, "supplied by %s\n", rdev_get_name(supply_rdev));
1435 if (!try_module_get(supply_rdev->owner))
1438 rdev->supply = create_regulator(supply_rdev, &rdev->dev, "SUPPLY");
1439 if (rdev->supply == NULL) {
1443 supply_rdev->open_count++;
1449 * set_consumer_device_supply - Bind a regulator to a symbolic supply
1450 * @rdev: regulator source
1451 * @consumer_dev_name: dev_name() string for device supply applies to
1452 * @supply: symbolic name for supply
1454 * Allows platform initialisation code to map physical regulator
1455 * sources to symbolic names for supplies for use by devices. Devices
1456 * should use these symbolic names to request regulators, avoiding the
1457 * need to provide board-specific regulator names as platform data.
1459 static int set_consumer_device_supply(struct regulator_dev *rdev,
1460 const char *consumer_dev_name,
1463 struct regulator_map *node;
1469 if (consumer_dev_name != NULL)
1474 list_for_each_entry(node, ®ulator_map_list, list) {
1475 if (node->dev_name && consumer_dev_name) {
1476 if (strcmp(node->dev_name, consumer_dev_name) != 0)
1478 } else if (node->dev_name || consumer_dev_name) {
1482 if (strcmp(node->supply, supply) != 0)
1485 pr_debug("%s: %s/%s is '%s' supply; fail %s/%s\n",
1487 dev_name(&node->regulator->dev),
1488 node->regulator->desc->name,
1490 dev_name(&rdev->dev), rdev_get_name(rdev));
1494 node = kzalloc(sizeof(struct regulator_map), GFP_KERNEL);
1498 node->regulator = rdev;
1499 node->supply = supply;
1502 node->dev_name = kstrdup(consumer_dev_name, GFP_KERNEL);
1503 if (node->dev_name == NULL) {
1509 list_add(&node->list, ®ulator_map_list);
1513 static void unset_regulator_supplies(struct regulator_dev *rdev)
1515 struct regulator_map *node, *n;
1517 list_for_each_entry_safe(node, n, ®ulator_map_list, list) {
1518 if (rdev == node->regulator) {
1519 list_del(&node->list);
1520 kfree(node->dev_name);
1526 #ifdef CONFIG_DEBUG_FS
1527 static ssize_t constraint_flags_read_file(struct file *file,
1528 char __user *user_buf,
1529 size_t count, loff_t *ppos)
1531 const struct regulator *regulator = file->private_data;
1532 const struct regulation_constraints *c = regulator->rdev->constraints;
1539 buf = kmalloc(PAGE_SIZE, GFP_KERNEL);
1543 ret = snprintf(buf, PAGE_SIZE,
1547 "ramp_disable: %u\n"
1550 "over_current_protection: %u\n",
1557 c->over_current_protection);
1559 ret = simple_read_from_buffer(user_buf, count, ppos, buf, ret);
1567 static const struct file_operations constraint_flags_fops = {
1568 #ifdef CONFIG_DEBUG_FS
1569 .open = simple_open,
1570 .read = constraint_flags_read_file,
1571 .llseek = default_llseek,
1575 #define REG_STR_SIZE 64
1577 static struct regulator *create_regulator(struct regulator_dev *rdev,
1579 const char *supply_name)
1581 struct regulator *regulator;
1582 char buf[REG_STR_SIZE];
1585 regulator = kzalloc(sizeof(*regulator), GFP_KERNEL);
1586 if (regulator == NULL)
1589 regulator_lock(rdev);
1590 regulator->rdev = rdev;
1591 list_add(®ulator->list, &rdev->consumer_list);
1594 regulator->dev = dev;
1596 /* Add a link to the device sysfs entry */
1597 size = snprintf(buf, REG_STR_SIZE, "%s-%s",
1598 dev->kobj.name, supply_name);
1599 if (size >= REG_STR_SIZE)
1602 regulator->supply_name = kstrdup(buf, GFP_KERNEL);
1603 if (regulator->supply_name == NULL)
1606 err = sysfs_create_link_nowarn(&rdev->dev.kobj, &dev->kobj,
1609 rdev_dbg(rdev, "could not add device link %s err %d\n",
1610 dev->kobj.name, err);
1614 regulator->supply_name = kstrdup_const(supply_name, GFP_KERNEL);
1615 if (regulator->supply_name == NULL)
1619 regulator->debugfs = debugfs_create_dir(regulator->supply_name,
1621 if (!regulator->debugfs) {
1622 rdev_dbg(rdev, "Failed to create debugfs directory\n");
1624 debugfs_create_u32("uA_load", 0444, regulator->debugfs,
1625 ®ulator->uA_load);
1626 debugfs_create_u32("min_uV", 0444, regulator->debugfs,
1627 ®ulator->voltage[PM_SUSPEND_ON].min_uV);
1628 debugfs_create_u32("max_uV", 0444, regulator->debugfs,
1629 ®ulator->voltage[PM_SUSPEND_ON].max_uV);
1630 debugfs_create_file("constraint_flags", 0444,
1631 regulator->debugfs, regulator,
1632 &constraint_flags_fops);
1636 * Check now if the regulator is an always on regulator - if
1637 * it is then we don't need to do nearly so much work for
1638 * enable/disable calls.
1640 if (!regulator_ops_is_valid(rdev, REGULATOR_CHANGE_STATUS) &&
1641 _regulator_is_enabled(rdev))
1642 regulator->always_on = true;
1644 regulator_unlock(rdev);
1647 list_del(®ulator->list);
1649 regulator_unlock(rdev);
1653 static int _regulator_get_enable_time(struct regulator_dev *rdev)
1655 if (rdev->constraints && rdev->constraints->enable_time)
1656 return rdev->constraints->enable_time;
1657 if (rdev->desc->ops->enable_time)
1658 return rdev->desc->ops->enable_time(rdev);
1659 return rdev->desc->enable_time;
1662 static struct regulator_supply_alias *regulator_find_supply_alias(
1663 struct device *dev, const char *supply)
1665 struct regulator_supply_alias *map;
1667 list_for_each_entry(map, ®ulator_supply_alias_list, list)
1668 if (map->src_dev == dev && strcmp(map->src_supply, supply) == 0)
1674 static void regulator_supply_alias(struct device **dev, const char **supply)
1676 struct regulator_supply_alias *map;
1678 map = regulator_find_supply_alias(*dev, *supply);
1680 dev_dbg(*dev, "Mapping supply %s to %s,%s\n",
1681 *supply, map->alias_supply,
1682 dev_name(map->alias_dev));
1683 *dev = map->alias_dev;
1684 *supply = map->alias_supply;
1688 static int regulator_match(struct device *dev, const void *data)
1690 struct regulator_dev *r = dev_to_rdev(dev);
1692 return strcmp(rdev_get_name(r), data) == 0;
1695 static struct regulator_dev *regulator_lookup_by_name(const char *name)
1699 dev = class_find_device(®ulator_class, NULL, name, regulator_match);
1701 return dev ? dev_to_rdev(dev) : NULL;
1705 * regulator_dev_lookup - lookup a regulator device.
1706 * @dev: device for regulator "consumer".
1707 * @supply: Supply name or regulator ID.
1709 * If successful, returns a struct regulator_dev that corresponds to the name
1710 * @supply and with the embedded struct device refcount incremented by one.
1711 * The refcount must be dropped by calling put_device().
1712 * On failure one of the following ERR-PTR-encoded values is returned:
1713 * -ENODEV if lookup fails permanently, -EPROBE_DEFER if lookup could succeed
1716 static struct regulator_dev *regulator_dev_lookup(struct device *dev,
1719 struct regulator_dev *r = NULL;
1720 struct device_node *node;
1721 struct regulator_map *map;
1722 const char *devname = NULL;
1724 regulator_supply_alias(&dev, &supply);
1726 /* first do a dt based lookup */
1727 if (dev && dev->of_node) {
1728 node = of_get_regulator(dev, supply);
1730 r = of_find_regulator_by_node(node);
1735 * We have a node, but there is no device.
1736 * assume it has not registered yet.
1738 return ERR_PTR(-EPROBE_DEFER);
1742 /* if not found, try doing it non-dt way */
1744 devname = dev_name(dev);
1746 mutex_lock(®ulator_list_mutex);
1747 list_for_each_entry(map, ®ulator_map_list, list) {
1748 /* If the mapping has a device set up it must match */
1749 if (map->dev_name &&
1750 (!devname || strcmp(map->dev_name, devname)))
1753 if (strcmp(map->supply, supply) == 0 &&
1754 get_device(&map->regulator->dev)) {
1759 mutex_unlock(®ulator_list_mutex);
1764 r = regulator_lookup_by_name(supply);
1768 return ERR_PTR(-ENODEV);
1771 static int regulator_resolve_supply(struct regulator_dev *rdev)
1773 struct regulator_dev *r;
1774 struct device *dev = rdev->dev.parent;
1777 /* No supply to resolve? */
1778 if (!rdev->supply_name)
1781 /* Supply already resolved? */
1785 r = regulator_dev_lookup(dev, rdev->supply_name);
1789 /* Did the lookup explicitly defer for us? */
1790 if (ret == -EPROBE_DEFER)
1793 if (have_full_constraints()) {
1794 r = dummy_regulator_rdev;
1795 get_device(&r->dev);
1797 dev_err(dev, "Failed to resolve %s-supply for %s\n",
1798 rdev->supply_name, rdev->desc->name);
1799 return -EPROBE_DEFER;
1804 * If the supply's parent device is not the same as the
1805 * regulator's parent device, then ensure the parent device
1806 * is bound before we resolve the supply, in case the parent
1807 * device get probe deferred and unregisters the supply.
1809 if (r->dev.parent && r->dev.parent != rdev->dev.parent) {
1810 if (!device_is_bound(r->dev.parent)) {
1811 put_device(&r->dev);
1812 return -EPROBE_DEFER;
1816 /* Recursively resolve the supply of the supply */
1817 ret = regulator_resolve_supply(r);
1819 put_device(&r->dev);
1823 ret = set_supply(rdev, r);
1825 put_device(&r->dev);
1830 * In set_machine_constraints() we may have turned this regulator on
1831 * but we couldn't propagate to the supply if it hadn't been resolved
1834 if (rdev->use_count) {
1835 ret = regulator_enable(rdev->supply);
1837 _regulator_put(rdev->supply);
1838 rdev->supply = NULL;
1846 /* Internal regulator request function */
1847 struct regulator *_regulator_get(struct device *dev, const char *id,
1848 enum regulator_get_type get_type)
1850 struct regulator_dev *rdev;
1851 struct regulator *regulator;
1852 struct device_link *link;
1855 if (get_type >= MAX_GET_TYPE) {
1856 dev_err(dev, "invalid type %d in %s\n", get_type, __func__);
1857 return ERR_PTR(-EINVAL);
1861 pr_err("get() with no identifier\n");
1862 return ERR_PTR(-EINVAL);
1865 rdev = regulator_dev_lookup(dev, id);
1867 ret = PTR_ERR(rdev);
1870 * If regulator_dev_lookup() fails with error other
1871 * than -ENODEV our job here is done, we simply return it.
1874 return ERR_PTR(ret);
1876 if (!have_full_constraints()) {
1878 "incomplete constraints, dummy supplies not allowed\n");
1879 return ERR_PTR(-ENODEV);
1885 * Assume that a regulator is physically present and
1886 * enabled, even if it isn't hooked up, and just
1889 dev_warn(dev, "supply %s not found, using dummy regulator\n", id);
1890 rdev = dummy_regulator_rdev;
1891 get_device(&rdev->dev);
1896 "dummy supplies not allowed for exclusive requests\n");
1900 return ERR_PTR(-ENODEV);
1904 if (rdev->exclusive) {
1905 regulator = ERR_PTR(-EPERM);
1906 put_device(&rdev->dev);
1910 if (get_type == EXCLUSIVE_GET && rdev->open_count) {
1911 regulator = ERR_PTR(-EBUSY);
1912 put_device(&rdev->dev);
1916 mutex_lock(®ulator_list_mutex);
1917 ret = (rdev->coupling_desc.n_resolved != rdev->coupling_desc.n_coupled);
1918 mutex_unlock(®ulator_list_mutex);
1921 regulator = ERR_PTR(-EPROBE_DEFER);
1922 put_device(&rdev->dev);
1926 ret = regulator_resolve_supply(rdev);
1928 regulator = ERR_PTR(ret);
1929 put_device(&rdev->dev);
1933 if (!try_module_get(rdev->owner)) {
1934 regulator = ERR_PTR(-EPROBE_DEFER);
1935 put_device(&rdev->dev);
1939 regulator = create_regulator(rdev, dev, id);
1940 if (regulator == NULL) {
1941 regulator = ERR_PTR(-ENOMEM);
1942 module_put(rdev->owner);
1943 put_device(&rdev->dev);
1948 if (get_type == EXCLUSIVE_GET) {
1949 rdev->exclusive = 1;
1951 ret = _regulator_is_enabled(rdev);
1953 rdev->use_count = 1;
1955 rdev->use_count = 0;
1958 link = device_link_add(dev, &rdev->dev, DL_FLAG_STATELESS);
1959 if (!IS_ERR_OR_NULL(link))
1960 regulator->device_link = true;
1966 * regulator_get - lookup and obtain a reference to a regulator.
1967 * @dev: device for regulator "consumer"
1968 * @id: Supply name or regulator ID.
1970 * Returns a struct regulator corresponding to the regulator producer,
1971 * or IS_ERR() condition containing errno.
1973 * Use of supply names configured via regulator_set_device_supply() is
1974 * strongly encouraged. It is recommended that the supply name used
1975 * should match the name used for the supply and/or the relevant
1976 * device pins in the datasheet.
1978 struct regulator *regulator_get(struct device *dev, const char *id)
1980 return _regulator_get(dev, id, NORMAL_GET);
1982 EXPORT_SYMBOL_GPL(regulator_get);
1985 * regulator_get_exclusive - obtain exclusive access to a regulator.
1986 * @dev: device for regulator "consumer"
1987 * @id: Supply name or regulator ID.
1989 * Returns a struct regulator corresponding to the regulator producer,
1990 * or IS_ERR() condition containing errno. Other consumers will be
1991 * unable to obtain this regulator while this reference is held and the
1992 * use count for the regulator will be initialised to reflect the current
1993 * state of the regulator.
1995 * This is intended for use by consumers which cannot tolerate shared
1996 * use of the regulator such as those which need to force the
1997 * regulator off for correct operation of the hardware they are
2000 * Use of supply names configured via regulator_set_device_supply() is
2001 * strongly encouraged. It is recommended that the supply name used
2002 * should match the name used for the supply and/or the relevant
2003 * device pins in the datasheet.
2005 struct regulator *regulator_get_exclusive(struct device *dev, const char *id)
2007 return _regulator_get(dev, id, EXCLUSIVE_GET);
2009 EXPORT_SYMBOL_GPL(regulator_get_exclusive);
2012 * regulator_get_optional - obtain optional access to a regulator.
2013 * @dev: device for regulator "consumer"
2014 * @id: Supply name or regulator ID.
2016 * Returns a struct regulator corresponding to the regulator producer,
2017 * or IS_ERR() condition containing errno.
2019 * This is intended for use by consumers for devices which can have
2020 * some supplies unconnected in normal use, such as some MMC devices.
2021 * It can allow the regulator core to provide stub supplies for other
2022 * supplies requested using normal regulator_get() calls without
2023 * disrupting the operation of drivers that can handle absent
2026 * Use of supply names configured via regulator_set_device_supply() is
2027 * strongly encouraged. It is recommended that the supply name used
2028 * should match the name used for the supply and/or the relevant
2029 * device pins in the datasheet.
2031 struct regulator *regulator_get_optional(struct device *dev, const char *id)
2033 return _regulator_get(dev, id, OPTIONAL_GET);
2035 EXPORT_SYMBOL_GPL(regulator_get_optional);
2037 /* regulator_list_mutex lock held by regulator_put() */
2038 static void _regulator_put(struct regulator *regulator)
2040 struct regulator_dev *rdev;
2042 if (IS_ERR_OR_NULL(regulator))
2045 lockdep_assert_held_once(®ulator_list_mutex);
2047 /* Docs say you must disable before calling regulator_put() */
2048 WARN_ON(regulator->enable_count);
2050 rdev = regulator->rdev;
2052 debugfs_remove_recursive(regulator->debugfs);
2054 if (regulator->dev) {
2055 if (regulator->device_link)
2056 device_link_remove(regulator->dev, &rdev->dev);
2058 /* remove any sysfs entries */
2059 sysfs_remove_link(&rdev->dev.kobj, regulator->supply_name);
2062 regulator_lock(rdev);
2063 list_del(®ulator->list);
2066 rdev->exclusive = 0;
2067 regulator_unlock(rdev);
2069 kfree_const(regulator->supply_name);
2072 module_put(rdev->owner);
2073 put_device(&rdev->dev);
2077 * regulator_put - "free" the regulator source
2078 * @regulator: regulator source
2080 * Note: drivers must ensure that all regulator_enable calls made on this
2081 * regulator source are balanced by regulator_disable calls prior to calling
2084 void regulator_put(struct regulator *regulator)
2086 mutex_lock(®ulator_list_mutex);
2087 _regulator_put(regulator);
2088 mutex_unlock(®ulator_list_mutex);
2090 EXPORT_SYMBOL_GPL(regulator_put);
2093 * regulator_register_supply_alias - Provide device alias for supply lookup
2095 * @dev: device that will be given as the regulator "consumer"
2096 * @id: Supply name or regulator ID
2097 * @alias_dev: device that should be used to lookup the supply
2098 * @alias_id: Supply name or regulator ID that should be used to lookup the
2101 * All lookups for id on dev will instead be conducted for alias_id on
2104 int regulator_register_supply_alias(struct device *dev, const char *id,
2105 struct device *alias_dev,
2106 const char *alias_id)
2108 struct regulator_supply_alias *map;
2110 map = regulator_find_supply_alias(dev, id);
2114 map = kzalloc(sizeof(struct regulator_supply_alias), GFP_KERNEL);
2119 map->src_supply = id;
2120 map->alias_dev = alias_dev;
2121 map->alias_supply = alias_id;
2123 list_add(&map->list, ®ulator_supply_alias_list);
2125 pr_info("Adding alias for supply %s,%s -> %s,%s\n",
2126 id, dev_name(dev), alias_id, dev_name(alias_dev));
2130 EXPORT_SYMBOL_GPL(regulator_register_supply_alias);
2133 * regulator_unregister_supply_alias - Remove device alias
2135 * @dev: device that will be given as the regulator "consumer"
2136 * @id: Supply name or regulator ID
2138 * Remove a lookup alias if one exists for id on dev.
2140 void regulator_unregister_supply_alias(struct device *dev, const char *id)
2142 struct regulator_supply_alias *map;
2144 map = regulator_find_supply_alias(dev, id);
2146 list_del(&map->list);
2150 EXPORT_SYMBOL_GPL(regulator_unregister_supply_alias);
2153 * regulator_bulk_register_supply_alias - register multiple aliases
2155 * @dev: device that will be given as the regulator "consumer"
2156 * @id: List of supply names or regulator IDs
2157 * @alias_dev: device that should be used to lookup the supply
2158 * @alias_id: List of supply names or regulator IDs that should be used to
2160 * @num_id: Number of aliases to register
2162 * @return 0 on success, an errno on failure.
2164 * This helper function allows drivers to register several supply
2165 * aliases in one operation. If any of the aliases cannot be
2166 * registered any aliases that were registered will be removed
2167 * before returning to the caller.
2169 int regulator_bulk_register_supply_alias(struct device *dev,
2170 const char *const *id,
2171 struct device *alias_dev,
2172 const char *const *alias_id,
2178 for (i = 0; i < num_id; ++i) {
2179 ret = regulator_register_supply_alias(dev, id[i], alias_dev,
2189 "Failed to create supply alias %s,%s -> %s,%s\n",
2190 id[i], dev_name(dev), alias_id[i], dev_name(alias_dev));
2193 regulator_unregister_supply_alias(dev, id[i]);
2197 EXPORT_SYMBOL_GPL(regulator_bulk_register_supply_alias);
2200 * regulator_bulk_unregister_supply_alias - unregister multiple aliases
2202 * @dev: device that will be given as the regulator "consumer"
2203 * @id: List of supply names or regulator IDs
2204 * @num_id: Number of aliases to unregister
2206 * This helper function allows drivers to unregister several supply
2207 * aliases in one operation.
2209 void regulator_bulk_unregister_supply_alias(struct device *dev,
2210 const char *const *id,
2215 for (i = 0; i < num_id; ++i)
2216 regulator_unregister_supply_alias(dev, id[i]);
2218 EXPORT_SYMBOL_GPL(regulator_bulk_unregister_supply_alias);
2221 /* Manage enable GPIO list. Same GPIO pin can be shared among regulators */
2222 static int regulator_ena_gpio_request(struct regulator_dev *rdev,
2223 const struct regulator_config *config)
2225 struct regulator_enable_gpio *pin;
2226 struct gpio_desc *gpiod;
2228 gpiod = config->ena_gpiod;
2230 list_for_each_entry(pin, ®ulator_ena_gpio_list, list) {
2231 if (pin->gpiod == gpiod) {
2232 rdev_dbg(rdev, "GPIO is already used\n");
2233 goto update_ena_gpio_to_rdev;
2237 pin = kzalloc(sizeof(struct regulator_enable_gpio), GFP_KERNEL);
2242 list_add(&pin->list, ®ulator_ena_gpio_list);
2244 update_ena_gpio_to_rdev:
2245 pin->request_count++;
2246 rdev->ena_pin = pin;
2250 static void regulator_ena_gpio_free(struct regulator_dev *rdev)
2252 struct regulator_enable_gpio *pin, *n;
2257 /* Free the GPIO only in case of no use */
2258 list_for_each_entry_safe(pin, n, ®ulator_ena_gpio_list, list) {
2259 if (pin->gpiod == rdev->ena_pin->gpiod) {
2260 if (pin->request_count <= 1) {
2261 pin->request_count = 0;
2262 gpiod_put(pin->gpiod);
2263 list_del(&pin->list);
2265 rdev->ena_pin = NULL;
2268 pin->request_count--;
2275 * regulator_ena_gpio_ctrl - balance enable_count of each GPIO and actual GPIO pin control
2276 * @rdev: regulator_dev structure
2277 * @enable: enable GPIO at initial use?
2279 * GPIO is enabled in case of initial use. (enable_count is 0)
2280 * GPIO is disabled when it is not shared any more. (enable_count <= 1)
2282 static int regulator_ena_gpio_ctrl(struct regulator_dev *rdev, bool enable)
2284 struct regulator_enable_gpio *pin = rdev->ena_pin;
2290 /* Enable GPIO at initial use */
2291 if (pin->enable_count == 0)
2292 gpiod_set_value_cansleep(pin->gpiod, 1);
2294 pin->enable_count++;
2296 if (pin->enable_count > 1) {
2297 pin->enable_count--;
2301 /* Disable GPIO if not used */
2302 if (pin->enable_count <= 1) {
2303 gpiod_set_value_cansleep(pin->gpiod, 0);
2304 pin->enable_count = 0;
2312 * _regulator_enable_delay - a delay helper function
2313 * @delay: time to delay in microseconds
2315 * Delay for the requested amount of time as per the guidelines in:
2317 * Documentation/timers/timers-howto.rst
2319 * The assumption here is that regulators will never be enabled in
2320 * atomic context and therefore sleeping functions can be used.
2322 static void _regulator_enable_delay(unsigned int delay)
2324 unsigned int ms = delay / 1000;
2325 unsigned int us = delay % 1000;
2329 * For small enough values, handle super-millisecond
2330 * delays in the usleep_range() call below.
2339 * Give the scheduler some room to coalesce with any other
2340 * wakeup sources. For delays shorter than 10 us, don't even
2341 * bother setting up high-resolution timers and just busy-
2345 usleep_range(us, us + 100);
2350 static int _regulator_do_enable(struct regulator_dev *rdev)
2354 /* Query before enabling in case configuration dependent. */
2355 ret = _regulator_get_enable_time(rdev);
2359 rdev_warn(rdev, "enable_time() failed: %d\n", ret);
2363 trace_regulator_enable(rdev_get_name(rdev));
2365 if (rdev->desc->off_on_delay) {
2366 /* if needed, keep a distance of off_on_delay from last time
2367 * this regulator was disabled.
2369 unsigned long start_jiffy = jiffies;
2370 unsigned long intended, max_delay, remaining;
2372 max_delay = usecs_to_jiffies(rdev->desc->off_on_delay);
2373 intended = rdev->last_off_jiffy + max_delay;
2375 if (time_before(start_jiffy, intended)) {
2376 /* calc remaining jiffies to deal with one-time
2378 * in case of multiple timer wrapping, either it can be
2379 * detected by out-of-range remaining, or it cannot be
2380 * detected and we get a penalty of
2381 * _regulator_enable_delay().
2383 remaining = intended - start_jiffy;
2384 if (remaining <= max_delay)
2385 _regulator_enable_delay(
2386 jiffies_to_usecs(remaining));
2390 if (rdev->ena_pin) {
2391 if (!rdev->ena_gpio_state) {
2392 ret = regulator_ena_gpio_ctrl(rdev, true);
2395 rdev->ena_gpio_state = 1;
2397 } else if (rdev->desc->ops->enable) {
2398 ret = rdev->desc->ops->enable(rdev);
2405 /* Allow the regulator to ramp; it would be useful to extend
2406 * this for bulk operations so that the regulators can ramp
2408 trace_regulator_enable_delay(rdev_get_name(rdev));
2410 _regulator_enable_delay(delay);
2412 trace_regulator_enable_complete(rdev_get_name(rdev));
2418 * _regulator_handle_consumer_enable - handle that a consumer enabled
2419 * @regulator: regulator source
2421 * Some things on a regulator consumer (like the contribution towards total
2422 * load on the regulator) only have an effect when the consumer wants the
2423 * regulator enabled. Explained in example with two consumers of the same
2425 * consumer A: set_load(100); => total load = 0
2426 * consumer A: regulator_enable(); => total load = 100
2427 * consumer B: set_load(1000); => total load = 100
2428 * consumer B: regulator_enable(); => total load = 1100
2429 * consumer A: regulator_disable(); => total_load = 1000
2431 * This function (together with _regulator_handle_consumer_disable) is
2432 * responsible for keeping track of the refcount for a given regulator consumer
2433 * and applying / unapplying these things.
2435 * Returns 0 upon no error; -error upon error.
2437 static int _regulator_handle_consumer_enable(struct regulator *regulator)
2439 struct regulator_dev *rdev = regulator->rdev;
2441 lockdep_assert_held_once(&rdev->mutex.base);
2443 regulator->enable_count++;
2444 if (regulator->uA_load && regulator->enable_count == 1)
2445 return drms_uA_update(rdev);
2451 * _regulator_handle_consumer_disable - handle that a consumer disabled
2452 * @regulator: regulator source
2454 * The opposite of _regulator_handle_consumer_enable().
2456 * Returns 0 upon no error; -error upon error.
2458 static int _regulator_handle_consumer_disable(struct regulator *regulator)
2460 struct regulator_dev *rdev = regulator->rdev;
2462 lockdep_assert_held_once(&rdev->mutex.base);
2464 if (!regulator->enable_count) {
2465 rdev_err(rdev, "Underflow of regulator enable count\n");
2469 regulator->enable_count--;
2470 if (regulator->uA_load && regulator->enable_count == 0)
2471 return drms_uA_update(rdev);
2476 /* locks held by regulator_enable() */
2477 static int _regulator_enable(struct regulator *regulator)
2479 struct regulator_dev *rdev = regulator->rdev;
2482 lockdep_assert_held_once(&rdev->mutex.base);
2484 if (rdev->use_count == 0 && rdev->supply) {
2485 ret = _regulator_enable(rdev->supply);
2490 /* balance only if there are regulators coupled */
2491 if (rdev->coupling_desc.n_coupled > 1) {
2492 ret = regulator_balance_voltage(rdev, PM_SUSPEND_ON);
2494 goto err_disable_supply;
2497 ret = _regulator_handle_consumer_enable(regulator);
2499 goto err_disable_supply;
2501 if (rdev->use_count == 0) {
2502 /* The regulator may on if it's not switchable or left on */
2503 ret = _regulator_is_enabled(rdev);
2504 if (ret == -EINVAL || ret == 0) {
2505 if (!regulator_ops_is_valid(rdev,
2506 REGULATOR_CHANGE_STATUS)) {
2508 goto err_consumer_disable;
2511 ret = _regulator_do_enable(rdev);
2513 goto err_consumer_disable;
2515 _notifier_call_chain(rdev, REGULATOR_EVENT_ENABLE,
2517 } else if (ret < 0) {
2518 rdev_err(rdev, "is_enabled() failed: %d\n", ret);
2519 goto err_consumer_disable;
2521 /* Fallthrough on positive return values - already enabled */
2528 err_consumer_disable:
2529 _regulator_handle_consumer_disable(regulator);
2532 if (rdev->use_count == 0 && rdev->supply)
2533 _regulator_disable(rdev->supply);
2539 * regulator_enable - enable regulator output
2540 * @regulator: regulator source
2542 * Request that the regulator be enabled with the regulator output at
2543 * the predefined voltage or current value. Calls to regulator_enable()
2544 * must be balanced with calls to regulator_disable().
2546 * NOTE: the output value can be set by other drivers, boot loader or may be
2547 * hardwired in the regulator.
2549 int regulator_enable(struct regulator *regulator)
2551 struct regulator_dev *rdev = regulator->rdev;
2552 struct ww_acquire_ctx ww_ctx;
2555 regulator_lock_dependent(rdev, &ww_ctx);
2556 ret = _regulator_enable(regulator);
2557 regulator_unlock_dependent(rdev, &ww_ctx);
2561 EXPORT_SYMBOL_GPL(regulator_enable);
2563 static int _regulator_do_disable(struct regulator_dev *rdev)
2567 trace_regulator_disable(rdev_get_name(rdev));
2569 if (rdev->ena_pin) {
2570 if (rdev->ena_gpio_state) {
2571 ret = regulator_ena_gpio_ctrl(rdev, false);
2574 rdev->ena_gpio_state = 0;
2577 } else if (rdev->desc->ops->disable) {
2578 ret = rdev->desc->ops->disable(rdev);
2583 /* cares about last_off_jiffy only if off_on_delay is required by
2586 if (rdev->desc->off_on_delay)
2587 rdev->last_off_jiffy = jiffies;
2589 trace_regulator_disable_complete(rdev_get_name(rdev));
2594 /* locks held by regulator_disable() */
2595 static int _regulator_disable(struct regulator *regulator)
2597 struct regulator_dev *rdev = regulator->rdev;
2600 lockdep_assert_held_once(&rdev->mutex.base);
2602 if (WARN(rdev->use_count <= 0,
2603 "unbalanced disables for %s\n", rdev_get_name(rdev)))
2606 /* are we the last user and permitted to disable ? */
2607 if (rdev->use_count == 1 &&
2608 (rdev->constraints && !rdev->constraints->always_on)) {
2610 /* we are last user */
2611 if (regulator_ops_is_valid(rdev, REGULATOR_CHANGE_STATUS)) {
2612 ret = _notifier_call_chain(rdev,
2613 REGULATOR_EVENT_PRE_DISABLE,
2615 if (ret & NOTIFY_STOP_MASK)
2618 ret = _regulator_do_disable(rdev);
2620 rdev_err(rdev, "failed to disable\n");
2621 _notifier_call_chain(rdev,
2622 REGULATOR_EVENT_ABORT_DISABLE,
2626 _notifier_call_chain(rdev, REGULATOR_EVENT_DISABLE,
2630 rdev->use_count = 0;
2631 } else if (rdev->use_count > 1) {
2636 ret = _regulator_handle_consumer_disable(regulator);
2638 if (ret == 0 && rdev->coupling_desc.n_coupled > 1)
2639 ret = regulator_balance_voltage(rdev, PM_SUSPEND_ON);
2641 if (ret == 0 && rdev->use_count == 0 && rdev->supply)
2642 ret = _regulator_disable(rdev->supply);
2648 * regulator_disable - disable regulator output
2649 * @regulator: regulator source
2651 * Disable the regulator output voltage or current. Calls to
2652 * regulator_enable() must be balanced with calls to
2653 * regulator_disable().
2655 * NOTE: this will only disable the regulator output if no other consumer
2656 * devices have it enabled, the regulator device supports disabling and
2657 * machine constraints permit this operation.
2659 int regulator_disable(struct regulator *regulator)
2661 struct regulator_dev *rdev = regulator->rdev;
2662 struct ww_acquire_ctx ww_ctx;
2665 regulator_lock_dependent(rdev, &ww_ctx);
2666 ret = _regulator_disable(regulator);
2667 regulator_unlock_dependent(rdev, &ww_ctx);
2671 EXPORT_SYMBOL_GPL(regulator_disable);
2673 /* locks held by regulator_force_disable() */
2674 static int _regulator_force_disable(struct regulator_dev *rdev)
2678 lockdep_assert_held_once(&rdev->mutex.base);
2680 ret = _notifier_call_chain(rdev, REGULATOR_EVENT_FORCE_DISABLE |
2681 REGULATOR_EVENT_PRE_DISABLE, NULL);
2682 if (ret & NOTIFY_STOP_MASK)
2685 ret = _regulator_do_disable(rdev);
2687 rdev_err(rdev, "failed to force disable\n");
2688 _notifier_call_chain(rdev, REGULATOR_EVENT_FORCE_DISABLE |
2689 REGULATOR_EVENT_ABORT_DISABLE, NULL);
2693 _notifier_call_chain(rdev, REGULATOR_EVENT_FORCE_DISABLE |
2694 REGULATOR_EVENT_DISABLE, NULL);
2700 * regulator_force_disable - force disable regulator output
2701 * @regulator: regulator source
2703 * Forcibly disable the regulator output voltage or current.
2704 * NOTE: this *will* disable the regulator output even if other consumer
2705 * devices have it enabled. This should be used for situations when device
2706 * damage will likely occur if the regulator is not disabled (e.g. over temp).
2708 int regulator_force_disable(struct regulator *regulator)
2710 struct regulator_dev *rdev = regulator->rdev;
2711 struct ww_acquire_ctx ww_ctx;
2714 regulator_lock_dependent(rdev, &ww_ctx);
2716 ret = _regulator_force_disable(regulator->rdev);
2718 if (rdev->coupling_desc.n_coupled > 1)
2719 regulator_balance_voltage(rdev, PM_SUSPEND_ON);
2721 if (regulator->uA_load) {
2722 regulator->uA_load = 0;
2723 ret = drms_uA_update(rdev);
2726 if (rdev->use_count != 0 && rdev->supply)
2727 _regulator_disable(rdev->supply);
2729 regulator_unlock_dependent(rdev, &ww_ctx);
2733 EXPORT_SYMBOL_GPL(regulator_force_disable);
2735 static void regulator_disable_work(struct work_struct *work)
2737 struct regulator_dev *rdev = container_of(work, struct regulator_dev,
2739 struct ww_acquire_ctx ww_ctx;
2741 struct regulator *regulator;
2742 int total_count = 0;
2744 regulator_lock_dependent(rdev, &ww_ctx);
2747 * Workqueue functions queue the new work instance while the previous
2748 * work instance is being processed. Cancel the queued work instance
2749 * as the work instance under processing does the job of the queued
2752 cancel_delayed_work(&rdev->disable_work);
2754 list_for_each_entry(regulator, &rdev->consumer_list, list) {
2755 count = regulator->deferred_disables;
2760 total_count += count;
2761 regulator->deferred_disables = 0;
2763 for (i = 0; i < count; i++) {
2764 ret = _regulator_disable(regulator);
2766 rdev_err(rdev, "Deferred disable failed: %d\n", ret);
2769 WARN_ON(!total_count);
2771 if (rdev->coupling_desc.n_coupled > 1)
2772 regulator_balance_voltage(rdev, PM_SUSPEND_ON);
2774 regulator_unlock_dependent(rdev, &ww_ctx);
2778 * regulator_disable_deferred - disable regulator output with delay
2779 * @regulator: regulator source
2780 * @ms: milliseconds until the regulator is disabled
2782 * Execute regulator_disable() on the regulator after a delay. This
2783 * is intended for use with devices that require some time to quiesce.
2785 * NOTE: this will only disable the regulator output if no other consumer
2786 * devices have it enabled, the regulator device supports disabling and
2787 * machine constraints permit this operation.
2789 int regulator_disable_deferred(struct regulator *regulator, int ms)
2791 struct regulator_dev *rdev = regulator->rdev;
2794 return regulator_disable(regulator);
2796 regulator_lock(rdev);
2797 regulator->deferred_disables++;
2798 mod_delayed_work(system_power_efficient_wq, &rdev->disable_work,
2799 msecs_to_jiffies(ms));
2800 regulator_unlock(rdev);
2804 EXPORT_SYMBOL_GPL(regulator_disable_deferred);
2806 static int _regulator_is_enabled(struct regulator_dev *rdev)
2808 /* A GPIO control always takes precedence */
2810 return rdev->ena_gpio_state;
2812 /* If we don't know then assume that the regulator is always on */
2813 if (!rdev->desc->ops->is_enabled)
2816 return rdev->desc->ops->is_enabled(rdev);
2819 static int _regulator_list_voltage(struct regulator_dev *rdev,
2820 unsigned selector, int lock)
2822 const struct regulator_ops *ops = rdev->desc->ops;
2825 if (rdev->desc->fixed_uV && rdev->desc->n_voltages == 1 && !selector)
2826 return rdev->desc->fixed_uV;
2828 if (ops->list_voltage) {
2829 if (selector >= rdev->desc->n_voltages)
2832 regulator_lock(rdev);
2833 ret = ops->list_voltage(rdev, selector);
2835 regulator_unlock(rdev);
2836 } else if (rdev->is_switch && rdev->supply) {
2837 ret = _regulator_list_voltage(rdev->supply->rdev,
2844 if (ret < rdev->constraints->min_uV)
2846 else if (ret > rdev->constraints->max_uV)
2854 * regulator_is_enabled - is the regulator output enabled
2855 * @regulator: regulator source
2857 * Returns positive if the regulator driver backing the source/client
2858 * has requested that the device be enabled, zero if it hasn't, else a
2859 * negative errno code.
2861 * Note that the device backing this regulator handle can have multiple
2862 * users, so it might be enabled even if regulator_enable() was never
2863 * called for this particular source.
2865 int regulator_is_enabled(struct regulator *regulator)
2869 if (regulator->always_on)
2872 regulator_lock(regulator->rdev);
2873 ret = _regulator_is_enabled(regulator->rdev);
2874 regulator_unlock(regulator->rdev);
2878 EXPORT_SYMBOL_GPL(regulator_is_enabled);
2881 * regulator_count_voltages - count regulator_list_voltage() selectors
2882 * @regulator: regulator source
2884 * Returns number of selectors, or negative errno. Selectors are
2885 * numbered starting at zero, and typically correspond to bitfields
2886 * in hardware registers.
2888 int regulator_count_voltages(struct regulator *regulator)
2890 struct regulator_dev *rdev = regulator->rdev;
2892 if (rdev->desc->n_voltages)
2893 return rdev->desc->n_voltages;
2895 if (!rdev->is_switch || !rdev->supply)
2898 return regulator_count_voltages(rdev->supply);
2900 EXPORT_SYMBOL_GPL(regulator_count_voltages);
2903 * regulator_list_voltage - enumerate supported voltages
2904 * @regulator: regulator source
2905 * @selector: identify voltage to list
2906 * Context: can sleep
2908 * Returns a voltage that can be passed to @regulator_set_voltage(),
2909 * zero if this selector code can't be used on this system, or a
2912 int regulator_list_voltage(struct regulator *regulator, unsigned selector)
2914 return _regulator_list_voltage(regulator->rdev, selector, 1);
2916 EXPORT_SYMBOL_GPL(regulator_list_voltage);
2919 * regulator_get_regmap - get the regulator's register map
2920 * @regulator: regulator source
2922 * Returns the register map for the given regulator, or an ERR_PTR value
2923 * if the regulator doesn't use regmap.
2925 struct regmap *regulator_get_regmap(struct regulator *regulator)
2927 struct regmap *map = regulator->rdev->regmap;
2929 return map ? map : ERR_PTR(-EOPNOTSUPP);
2933 * regulator_get_hardware_vsel_register - get the HW voltage selector register
2934 * @regulator: regulator source
2935 * @vsel_reg: voltage selector register, output parameter
2936 * @vsel_mask: mask for voltage selector bitfield, output parameter
2938 * Returns the hardware register offset and bitmask used for setting the
2939 * regulator voltage. This might be useful when configuring voltage-scaling
2940 * hardware or firmware that can make I2C requests behind the kernel's back,
2943 * On success, the output parameters @vsel_reg and @vsel_mask are filled in
2944 * and 0 is returned, otherwise a negative errno is returned.
2946 int regulator_get_hardware_vsel_register(struct regulator *regulator,
2948 unsigned *vsel_mask)
2950 struct regulator_dev *rdev = regulator->rdev;
2951 const struct regulator_ops *ops = rdev->desc->ops;
2953 if (ops->set_voltage_sel != regulator_set_voltage_sel_regmap)
2956 *vsel_reg = rdev->desc->vsel_reg;
2957 *vsel_mask = rdev->desc->vsel_mask;
2961 EXPORT_SYMBOL_GPL(regulator_get_hardware_vsel_register);
2964 * regulator_list_hardware_vsel - get the HW-specific register value for a selector
2965 * @regulator: regulator source
2966 * @selector: identify voltage to list
2968 * Converts the selector to a hardware-specific voltage selector that can be
2969 * directly written to the regulator registers. The address of the voltage
2970 * register can be determined by calling @regulator_get_hardware_vsel_register.
2972 * On error a negative errno is returned.
2974 int regulator_list_hardware_vsel(struct regulator *regulator,
2977 struct regulator_dev *rdev = regulator->rdev;
2978 const struct regulator_ops *ops = rdev->desc->ops;
2980 if (selector >= rdev->desc->n_voltages)
2982 if (ops->set_voltage_sel != regulator_set_voltage_sel_regmap)
2987 EXPORT_SYMBOL_GPL(regulator_list_hardware_vsel);
2990 * regulator_get_linear_step - return the voltage step size between VSEL values
2991 * @regulator: regulator source
2993 * Returns the voltage step size between VSEL values for linear
2994 * regulators, or return 0 if the regulator isn't a linear regulator.
2996 unsigned int regulator_get_linear_step(struct regulator *regulator)
2998 struct regulator_dev *rdev = regulator->rdev;
3000 return rdev->desc->uV_step;
3002 EXPORT_SYMBOL_GPL(regulator_get_linear_step);
3005 * regulator_is_supported_voltage - check if a voltage range can be supported
3007 * @regulator: Regulator to check.
3008 * @min_uV: Minimum required voltage in uV.
3009 * @max_uV: Maximum required voltage in uV.
3011 * Returns a boolean.
3013 int regulator_is_supported_voltage(struct regulator *regulator,
3014 int min_uV, int max_uV)
3016 struct regulator_dev *rdev = regulator->rdev;
3017 int i, voltages, ret;
3019 /* If we can't change voltage check the current voltage */
3020 if (!regulator_ops_is_valid(rdev, REGULATOR_CHANGE_VOLTAGE)) {
3021 ret = regulator_get_voltage(regulator);
3023 return min_uV <= ret && ret <= max_uV;
3028 /* Any voltage within constrains range is fine? */
3029 if (rdev->desc->continuous_voltage_range)
3030 return min_uV >= rdev->constraints->min_uV &&
3031 max_uV <= rdev->constraints->max_uV;
3033 ret = regulator_count_voltages(regulator);
3038 for (i = 0; i < voltages; i++) {
3039 ret = regulator_list_voltage(regulator, i);
3041 if (ret >= min_uV && ret <= max_uV)
3047 EXPORT_SYMBOL_GPL(regulator_is_supported_voltage);
3049 static int regulator_map_voltage(struct regulator_dev *rdev, int min_uV,
3052 const struct regulator_desc *desc = rdev->desc;
3054 if (desc->ops->map_voltage)
3055 return desc->ops->map_voltage(rdev, min_uV, max_uV);
3057 if (desc->ops->list_voltage == regulator_list_voltage_linear)
3058 return regulator_map_voltage_linear(rdev, min_uV, max_uV);
3060 if (desc->ops->list_voltage == regulator_list_voltage_linear_range)
3061 return regulator_map_voltage_linear_range(rdev, min_uV, max_uV);
3063 if (desc->ops->list_voltage ==
3064 regulator_list_voltage_pickable_linear_range)
3065 return regulator_map_voltage_pickable_linear_range(rdev,
3068 return regulator_map_voltage_iterate(rdev, min_uV, max_uV);
3071 static int _regulator_call_set_voltage(struct regulator_dev *rdev,
3072 int min_uV, int max_uV,
3075 struct pre_voltage_change_data data;
3078 data.old_uV = regulator_get_voltage_rdev(rdev);
3079 data.min_uV = min_uV;
3080 data.max_uV = max_uV;
3081 ret = _notifier_call_chain(rdev, REGULATOR_EVENT_PRE_VOLTAGE_CHANGE,
3083 if (ret & NOTIFY_STOP_MASK)
3086 ret = rdev->desc->ops->set_voltage(rdev, min_uV, max_uV, selector);
3090 _notifier_call_chain(rdev, REGULATOR_EVENT_ABORT_VOLTAGE_CHANGE,
3091 (void *)data.old_uV);
3096 static int _regulator_call_set_voltage_sel(struct regulator_dev *rdev,
3097 int uV, unsigned selector)
3099 struct pre_voltage_change_data data;
3102 data.old_uV = regulator_get_voltage_rdev(rdev);
3105 ret = _notifier_call_chain(rdev, REGULATOR_EVENT_PRE_VOLTAGE_CHANGE,
3107 if (ret & NOTIFY_STOP_MASK)
3110 ret = rdev->desc->ops->set_voltage_sel(rdev, selector);
3114 _notifier_call_chain(rdev, REGULATOR_EVENT_ABORT_VOLTAGE_CHANGE,
3115 (void *)data.old_uV);
3120 static int _regulator_set_voltage_sel_step(struct regulator_dev *rdev,
3121 int uV, int new_selector)
3123 const struct regulator_ops *ops = rdev->desc->ops;
3124 int diff, old_sel, curr_sel, ret;
3126 /* Stepping is only needed if the regulator is enabled. */
3127 if (!_regulator_is_enabled(rdev))
3130 if (!ops->get_voltage_sel)
3133 old_sel = ops->get_voltage_sel(rdev);
3137 diff = new_selector - old_sel;
3139 return 0; /* No change needed. */
3143 for (curr_sel = old_sel + rdev->desc->vsel_step;
3144 curr_sel < new_selector;
3145 curr_sel += rdev->desc->vsel_step) {
3147 * Call the callback directly instead of using
3148 * _regulator_call_set_voltage_sel() as we don't
3149 * want to notify anyone yet. Same in the branch
3152 ret = ops->set_voltage_sel(rdev, curr_sel);
3157 /* Stepping down. */
3158 for (curr_sel = old_sel - rdev->desc->vsel_step;
3159 curr_sel > new_selector;
3160 curr_sel -= rdev->desc->vsel_step) {
3161 ret = ops->set_voltage_sel(rdev, curr_sel);
3168 /* The final selector will trigger the notifiers. */
3169 return _regulator_call_set_voltage_sel(rdev, uV, new_selector);
3173 * At least try to return to the previous voltage if setting a new
3176 (void)ops->set_voltage_sel(rdev, old_sel);
3180 static int _regulator_set_voltage_time(struct regulator_dev *rdev,
3181 int old_uV, int new_uV)
3183 unsigned int ramp_delay = 0;
3185 if (rdev->constraints->ramp_delay)
3186 ramp_delay = rdev->constraints->ramp_delay;
3187 else if (rdev->desc->ramp_delay)
3188 ramp_delay = rdev->desc->ramp_delay;
3189 else if (rdev->constraints->settling_time)
3190 return rdev->constraints->settling_time;
3191 else if (rdev->constraints->settling_time_up &&
3193 return rdev->constraints->settling_time_up;
3194 else if (rdev->constraints->settling_time_down &&
3196 return rdev->constraints->settling_time_down;
3198 if (ramp_delay == 0) {
3199 rdev_dbg(rdev, "ramp_delay not set\n");
3203 return DIV_ROUND_UP(abs(new_uV - old_uV), ramp_delay);
3206 static int _regulator_do_set_voltage(struct regulator_dev *rdev,
3207 int min_uV, int max_uV)
3212 unsigned int selector;
3213 int old_selector = -1;
3214 const struct regulator_ops *ops = rdev->desc->ops;
3215 int old_uV = regulator_get_voltage_rdev(rdev);
3217 trace_regulator_set_voltage(rdev_get_name(rdev), min_uV, max_uV);
3219 min_uV += rdev->constraints->uV_offset;
3220 max_uV += rdev->constraints->uV_offset;
3223 * If we can't obtain the old selector there is not enough
3224 * info to call set_voltage_time_sel().
3226 if (_regulator_is_enabled(rdev) &&
3227 ops->set_voltage_time_sel && ops->get_voltage_sel) {
3228 old_selector = ops->get_voltage_sel(rdev);
3229 if (old_selector < 0)
3230 return old_selector;
3233 if (ops->set_voltage) {
3234 ret = _regulator_call_set_voltage(rdev, min_uV, max_uV,
3238 if (ops->list_voltage)
3239 best_val = ops->list_voltage(rdev,
3242 best_val = regulator_get_voltage_rdev(rdev);
3245 } else if (ops->set_voltage_sel) {
3246 ret = regulator_map_voltage(rdev, min_uV, max_uV);
3248 best_val = ops->list_voltage(rdev, ret);
3249 if (min_uV <= best_val && max_uV >= best_val) {
3251 if (old_selector == selector)
3253 else if (rdev->desc->vsel_step)
3254 ret = _regulator_set_voltage_sel_step(
3255 rdev, best_val, selector);
3257 ret = _regulator_call_set_voltage_sel(
3258 rdev, best_val, selector);
3270 if (ops->set_voltage_time_sel) {
3272 * Call set_voltage_time_sel if successfully obtained
3275 if (old_selector >= 0 && old_selector != selector)
3276 delay = ops->set_voltage_time_sel(rdev, old_selector,
3279 if (old_uV != best_val) {
3280 if (ops->set_voltage_time)
3281 delay = ops->set_voltage_time(rdev, old_uV,
3284 delay = _regulator_set_voltage_time(rdev,
3291 rdev_warn(rdev, "failed to get delay: %d\n", delay);
3295 /* Insert any necessary delays */
3296 if (delay >= 1000) {
3297 mdelay(delay / 1000);
3298 udelay(delay % 1000);
3303 if (best_val >= 0) {
3304 unsigned long data = best_val;
3306 _notifier_call_chain(rdev, REGULATOR_EVENT_VOLTAGE_CHANGE,
3311 trace_regulator_set_voltage_complete(rdev_get_name(rdev), best_val);
3316 static int _regulator_do_set_suspend_voltage(struct regulator_dev *rdev,
3317 int min_uV, int max_uV, suspend_state_t state)
3319 struct regulator_state *rstate;
3322 rstate = regulator_get_suspend_state(rdev, state);
3326 if (min_uV < rstate->min_uV)
3327 min_uV = rstate->min_uV;
3328 if (max_uV > rstate->max_uV)
3329 max_uV = rstate->max_uV;
3331 sel = regulator_map_voltage(rdev, min_uV, max_uV);
3335 uV = rdev->desc->ops->list_voltage(rdev, sel);
3336 if (uV >= min_uV && uV <= max_uV)
3342 static int regulator_set_voltage_unlocked(struct regulator *regulator,
3343 int min_uV, int max_uV,
3344 suspend_state_t state)
3346 struct regulator_dev *rdev = regulator->rdev;
3347 struct regulator_voltage *voltage = ®ulator->voltage[state];
3349 int old_min_uV, old_max_uV;
3352 /* If we're setting the same range as last time the change
3353 * should be a noop (some cpufreq implementations use the same
3354 * voltage for multiple frequencies, for example).
3356 if (voltage->min_uV == min_uV && voltage->max_uV == max_uV)
3359 /* If we're trying to set a range that overlaps the current voltage,
3360 * return successfully even though the regulator does not support
3361 * changing the voltage.
3363 if (!regulator_ops_is_valid(rdev, REGULATOR_CHANGE_VOLTAGE)) {
3364 current_uV = regulator_get_voltage_rdev(rdev);
3365 if (min_uV <= current_uV && current_uV <= max_uV) {
3366 voltage->min_uV = min_uV;
3367 voltage->max_uV = max_uV;
3373 if (!rdev->desc->ops->set_voltage &&
3374 !rdev->desc->ops->set_voltage_sel) {
3379 /* constraints check */
3380 ret = regulator_check_voltage(rdev, &min_uV, &max_uV);
3384 /* restore original values in case of error */
3385 old_min_uV = voltage->min_uV;
3386 old_max_uV = voltage->max_uV;
3387 voltage->min_uV = min_uV;
3388 voltage->max_uV = max_uV;
3390 /* for not coupled regulators this will just set the voltage */
3391 ret = regulator_balance_voltage(rdev, state);
3393 voltage->min_uV = old_min_uV;
3394 voltage->max_uV = old_max_uV;
3401 int regulator_set_voltage_rdev(struct regulator_dev *rdev, int min_uV,
3402 int max_uV, suspend_state_t state)
3404 int best_supply_uV = 0;
3405 int supply_change_uV = 0;
3409 regulator_ops_is_valid(rdev->supply->rdev,
3410 REGULATOR_CHANGE_VOLTAGE) &&
3411 (rdev->desc->min_dropout_uV || !(rdev->desc->ops->get_voltage ||
3412 rdev->desc->ops->get_voltage_sel))) {
3413 int current_supply_uV;
3416 selector = regulator_map_voltage(rdev, min_uV, max_uV);
3422 best_supply_uV = _regulator_list_voltage(rdev, selector, 0);
3423 if (best_supply_uV < 0) {
3424 ret = best_supply_uV;
3428 best_supply_uV += rdev->desc->min_dropout_uV;
3430 current_supply_uV = regulator_get_voltage_rdev(rdev->supply->rdev);
3431 if (current_supply_uV < 0) {
3432 ret = current_supply_uV;
3436 supply_change_uV = best_supply_uV - current_supply_uV;
3439 if (supply_change_uV > 0) {
3440 ret = regulator_set_voltage_unlocked(rdev->supply,
3441 best_supply_uV, INT_MAX, state);
3443 dev_err(&rdev->dev, "Failed to increase supply voltage: %d\n",
3449 if (state == PM_SUSPEND_ON)
3450 ret = _regulator_do_set_voltage(rdev, min_uV, max_uV);
3452 ret = _regulator_do_set_suspend_voltage(rdev, min_uV,
3457 if (supply_change_uV < 0) {
3458 ret = regulator_set_voltage_unlocked(rdev->supply,
3459 best_supply_uV, INT_MAX, state);
3461 dev_warn(&rdev->dev, "Failed to decrease supply voltage: %d\n",
3463 /* No need to fail here */
3470 EXPORT_SYMBOL_GPL(regulator_set_voltage_rdev);
3472 static int regulator_limit_voltage_step(struct regulator_dev *rdev,
3473 int *current_uV, int *min_uV)
3475 struct regulation_constraints *constraints = rdev->constraints;
3477 /* Limit voltage change only if necessary */
3478 if (!constraints->max_uV_step || !_regulator_is_enabled(rdev))
3481 if (*current_uV < 0) {
3482 *current_uV = regulator_get_voltage_rdev(rdev);
3484 if (*current_uV < 0)
3488 if (abs(*current_uV - *min_uV) <= constraints->max_uV_step)
3491 /* Clamp target voltage within the given step */
3492 if (*current_uV < *min_uV)
3493 *min_uV = min(*current_uV + constraints->max_uV_step,
3496 *min_uV = max(*current_uV - constraints->max_uV_step,
3502 static int regulator_get_optimal_voltage(struct regulator_dev *rdev,
3504 int *min_uV, int *max_uV,
3505 suspend_state_t state,
3508 struct coupling_desc *c_desc = &rdev->coupling_desc;
3509 struct regulator_dev **c_rdevs = c_desc->coupled_rdevs;
3510 struct regulation_constraints *constraints = rdev->constraints;
3511 int desired_min_uV = 0, desired_max_uV = INT_MAX;
3512 int max_current_uV = 0, min_current_uV = INT_MAX;
3513 int highest_min_uV = 0, target_uV, possible_uV;
3514 int i, ret, max_spread;
3520 * If there are no coupled regulators, simply set the voltage
3521 * demanded by consumers.
3523 if (n_coupled == 1) {
3525 * If consumers don't provide any demands, set voltage
3528 desired_min_uV = constraints->min_uV;
3529 desired_max_uV = constraints->max_uV;
3531 ret = regulator_check_consumers(rdev,
3533 &desired_max_uV, state);
3537 possible_uV = desired_min_uV;
3543 /* Find highest min desired voltage */
3544 for (i = 0; i < n_coupled; i++) {
3546 int tmp_max = INT_MAX;
3548 lockdep_assert_held_once(&c_rdevs[i]->mutex.base);
3550 ret = regulator_check_consumers(c_rdevs[i],
3556 ret = regulator_check_voltage(c_rdevs[i], &tmp_min, &tmp_max);
3560 highest_min_uV = max(highest_min_uV, tmp_min);
3563 desired_min_uV = tmp_min;
3564 desired_max_uV = tmp_max;
3568 max_spread = constraints->max_spread[0];
3571 * Let target_uV be equal to the desired one if possible.
3572 * If not, set it to minimum voltage, allowed by other coupled
3575 target_uV = max(desired_min_uV, highest_min_uV - max_spread);
3578 * Find min and max voltages, which currently aren't violating
3581 for (i = 1; i < n_coupled; i++) {
3584 if (!_regulator_is_enabled(c_rdevs[i]))
3587 tmp_act = regulator_get_voltage_rdev(c_rdevs[i]);
3591 min_current_uV = min(tmp_act, min_current_uV);
3592 max_current_uV = max(tmp_act, max_current_uV);
3595 /* There aren't any other regulators enabled */
3596 if (max_current_uV == 0) {
3597 possible_uV = target_uV;
3600 * Correct target voltage, so as it currently isn't
3601 * violating max_spread
3603 possible_uV = max(target_uV, max_current_uV - max_spread);
3604 possible_uV = min(possible_uV, min_current_uV + max_spread);
3607 if (possible_uV > desired_max_uV)
3610 done = (possible_uV == target_uV);
3611 desired_min_uV = possible_uV;
3614 /* Apply max_uV_step constraint if necessary */
3615 if (state == PM_SUSPEND_ON) {
3616 ret = regulator_limit_voltage_step(rdev, current_uV,
3625 /* Set current_uV if wasn't done earlier in the code and if necessary */
3626 if (n_coupled > 1 && *current_uV == -1) {
3628 if (_regulator_is_enabled(rdev)) {
3629 ret = regulator_get_voltage_rdev(rdev);
3635 *current_uV = desired_min_uV;
3639 *min_uV = desired_min_uV;
3640 *max_uV = desired_max_uV;
3645 int regulator_do_balance_voltage(struct regulator_dev *rdev,
3646 suspend_state_t state, bool skip_coupled)
3648 struct regulator_dev **c_rdevs;
3649 struct regulator_dev *best_rdev;
3650 struct coupling_desc *c_desc = &rdev->coupling_desc;
3651 int i, ret, n_coupled, best_min_uV, best_max_uV, best_c_rdev;
3652 unsigned int delta, best_delta;
3653 unsigned long c_rdev_done = 0;
3654 bool best_c_rdev_done;
3656 c_rdevs = c_desc->coupled_rdevs;
3657 n_coupled = skip_coupled ? 1 : c_desc->n_coupled;
3660 * Find the best possible voltage change on each loop. Leave the loop
3661 * if there isn't any possible change.
3664 best_c_rdev_done = false;
3672 * Find highest difference between optimal voltage
3673 * and current voltage.
3675 for (i = 0; i < n_coupled; i++) {
3677 * optimal_uV is the best voltage that can be set for
3678 * i-th regulator at the moment without violating
3679 * max_spread constraint in order to balance
3680 * the coupled voltages.
3682 int optimal_uV = 0, optimal_max_uV = 0, current_uV = 0;
3684 if (test_bit(i, &c_rdev_done))
3687 ret = regulator_get_optimal_voltage(c_rdevs[i],
3695 delta = abs(optimal_uV - current_uV);
3697 if (delta && best_delta <= delta) {
3698 best_c_rdev_done = ret;
3700 best_rdev = c_rdevs[i];
3701 best_min_uV = optimal_uV;
3702 best_max_uV = optimal_max_uV;
3707 /* Nothing to change, return successfully */
3713 ret = regulator_set_voltage_rdev(best_rdev, best_min_uV,
3714 best_max_uV, state);
3719 if (best_c_rdev_done)
3720 set_bit(best_c_rdev, &c_rdev_done);
3722 } while (n_coupled > 1);
3728 static int regulator_balance_voltage(struct regulator_dev *rdev,
3729 suspend_state_t state)
3731 struct coupling_desc *c_desc = &rdev->coupling_desc;
3732 struct regulator_coupler *coupler = c_desc->coupler;
3733 bool skip_coupled = false;
3736 * If system is in a state other than PM_SUSPEND_ON, don't check
3737 * other coupled regulators.
3739 if (state != PM_SUSPEND_ON)
3740 skip_coupled = true;
3742 if (c_desc->n_resolved < c_desc->n_coupled) {
3743 rdev_err(rdev, "Not all coupled regulators registered\n");
3747 /* Invoke custom balancer for customized couplers */
3748 if (coupler && coupler->balance_voltage)
3749 return coupler->balance_voltage(coupler, rdev, state);
3751 return regulator_do_balance_voltage(rdev, state, skip_coupled);
3755 * regulator_set_voltage - set regulator output voltage
3756 * @regulator: regulator source
3757 * @min_uV: Minimum required voltage in uV
3758 * @max_uV: Maximum acceptable voltage in uV
3760 * Sets a voltage regulator to the desired output voltage. This can be set
3761 * during any regulator state. IOW, regulator can be disabled or enabled.
3763 * If the regulator is enabled then the voltage will change to the new value
3764 * immediately otherwise if the regulator is disabled the regulator will
3765 * output at the new voltage when enabled.
3767 * NOTE: If the regulator is shared between several devices then the lowest
3768 * request voltage that meets the system constraints will be used.
3769 * Regulator system constraints must be set for this regulator before
3770 * calling this function otherwise this call will fail.
3772 int regulator_set_voltage(struct regulator *regulator, int min_uV, int max_uV)
3774 struct ww_acquire_ctx ww_ctx;
3777 regulator_lock_dependent(regulator->rdev, &ww_ctx);
3779 ret = regulator_set_voltage_unlocked(regulator, min_uV, max_uV,
3782 regulator_unlock_dependent(regulator->rdev, &ww_ctx);
3786 EXPORT_SYMBOL_GPL(regulator_set_voltage);
3788 static inline int regulator_suspend_toggle(struct regulator_dev *rdev,
3789 suspend_state_t state, bool en)
3791 struct regulator_state *rstate;
3793 rstate = regulator_get_suspend_state(rdev, state);
3797 if (!rstate->changeable)
3800 rstate->enabled = (en) ? ENABLE_IN_SUSPEND : DISABLE_IN_SUSPEND;
3805 int regulator_suspend_enable(struct regulator_dev *rdev,
3806 suspend_state_t state)
3808 return regulator_suspend_toggle(rdev, state, true);
3810 EXPORT_SYMBOL_GPL(regulator_suspend_enable);
3812 int regulator_suspend_disable(struct regulator_dev *rdev,
3813 suspend_state_t state)
3815 struct regulator *regulator;
3816 struct regulator_voltage *voltage;
3819 * if any consumer wants this regulator device keeping on in
3820 * suspend states, don't set it as disabled.
3822 list_for_each_entry(regulator, &rdev->consumer_list, list) {
3823 voltage = ®ulator->voltage[state];
3824 if (voltage->min_uV || voltage->max_uV)
3828 return regulator_suspend_toggle(rdev, state, false);
3830 EXPORT_SYMBOL_GPL(regulator_suspend_disable);
3832 static int _regulator_set_suspend_voltage(struct regulator *regulator,
3833 int min_uV, int max_uV,
3834 suspend_state_t state)
3836 struct regulator_dev *rdev = regulator->rdev;
3837 struct regulator_state *rstate;
3839 rstate = regulator_get_suspend_state(rdev, state);
3843 if (rstate->min_uV == rstate->max_uV) {
3844 rdev_err(rdev, "The suspend voltage can't be changed!\n");
3848 return regulator_set_voltage_unlocked(regulator, min_uV, max_uV, state);
3851 int regulator_set_suspend_voltage(struct regulator *regulator, int min_uV,
3852 int max_uV, suspend_state_t state)
3854 struct ww_acquire_ctx ww_ctx;
3857 /* PM_SUSPEND_ON is handled by regulator_set_voltage() */
3858 if (regulator_check_states(state) || state == PM_SUSPEND_ON)
3861 regulator_lock_dependent(regulator->rdev, &ww_ctx);
3863 ret = _regulator_set_suspend_voltage(regulator, min_uV,
3866 regulator_unlock_dependent(regulator->rdev, &ww_ctx);
3870 EXPORT_SYMBOL_GPL(regulator_set_suspend_voltage);
3873 * regulator_set_voltage_time - get raise/fall time
3874 * @regulator: regulator source
3875 * @old_uV: starting voltage in microvolts
3876 * @new_uV: target voltage in microvolts
3878 * Provided with the starting and ending voltage, this function attempts to
3879 * calculate the time in microseconds required to rise or fall to this new
3882 int regulator_set_voltage_time(struct regulator *regulator,
3883 int old_uV, int new_uV)
3885 struct regulator_dev *rdev = regulator->rdev;
3886 const struct regulator_ops *ops = rdev->desc->ops;
3892 if (ops->set_voltage_time)
3893 return ops->set_voltage_time(rdev, old_uV, new_uV);
3894 else if (!ops->set_voltage_time_sel)
3895 return _regulator_set_voltage_time(rdev, old_uV, new_uV);
3897 /* Currently requires operations to do this */
3898 if (!ops->list_voltage || !rdev->desc->n_voltages)
3901 for (i = 0; i < rdev->desc->n_voltages; i++) {
3902 /* We only look for exact voltage matches here */
3903 voltage = regulator_list_voltage(regulator, i);
3908 if (voltage == old_uV)
3910 if (voltage == new_uV)
3914 if (old_sel < 0 || new_sel < 0)
3917 return ops->set_voltage_time_sel(rdev, old_sel, new_sel);
3919 EXPORT_SYMBOL_GPL(regulator_set_voltage_time);
3922 * regulator_set_voltage_time_sel - get raise/fall time
3923 * @rdev: regulator source device
3924 * @old_selector: selector for starting voltage
3925 * @new_selector: selector for target voltage
3927 * Provided with the starting and target voltage selectors, this function
3928 * returns time in microseconds required to rise or fall to this new voltage
3930 * Drivers providing ramp_delay in regulation_constraints can use this as their
3931 * set_voltage_time_sel() operation.
3933 int regulator_set_voltage_time_sel(struct regulator_dev *rdev,
3934 unsigned int old_selector,
3935 unsigned int new_selector)
3937 int old_volt, new_volt;
3940 if (!rdev->desc->ops->list_voltage)
3943 old_volt = rdev->desc->ops->list_voltage(rdev, old_selector);
3944 new_volt = rdev->desc->ops->list_voltage(rdev, new_selector);
3946 if (rdev->desc->ops->set_voltage_time)
3947 return rdev->desc->ops->set_voltage_time(rdev, old_volt,
3950 return _regulator_set_voltage_time(rdev, old_volt, new_volt);
3952 EXPORT_SYMBOL_GPL(regulator_set_voltage_time_sel);
3955 * regulator_sync_voltage - re-apply last regulator output voltage
3956 * @regulator: regulator source
3958 * Re-apply the last configured voltage. This is intended to be used
3959 * where some external control source the consumer is cooperating with
3960 * has caused the configured voltage to change.
3962 int regulator_sync_voltage(struct regulator *regulator)
3964 struct regulator_dev *rdev = regulator->rdev;
3965 struct regulator_voltage *voltage = ®ulator->voltage[PM_SUSPEND_ON];
3966 int ret, min_uV, max_uV;
3968 regulator_lock(rdev);
3970 if (!rdev->desc->ops->set_voltage &&
3971 !rdev->desc->ops->set_voltage_sel) {
3976 /* This is only going to work if we've had a voltage configured. */
3977 if (!voltage->min_uV && !voltage->max_uV) {
3982 min_uV = voltage->min_uV;
3983 max_uV = voltage->max_uV;
3985 /* This should be a paranoia check... */
3986 ret = regulator_check_voltage(rdev, &min_uV, &max_uV);
3990 ret = regulator_check_consumers(rdev, &min_uV, &max_uV, 0);
3994 ret = _regulator_do_set_voltage(rdev, min_uV, max_uV);
3997 regulator_unlock(rdev);
4000 EXPORT_SYMBOL_GPL(regulator_sync_voltage);
4002 int regulator_get_voltage_rdev(struct regulator_dev *rdev)
4007 if (rdev->desc->ops->get_bypass) {
4008 ret = rdev->desc->ops->get_bypass(rdev, &bypassed);
4012 /* if bypassed the regulator must have a supply */
4013 if (!rdev->supply) {
4015 "bypassed regulator has no supply!\n");
4016 return -EPROBE_DEFER;
4019 return regulator_get_voltage_rdev(rdev->supply->rdev);
4023 if (rdev->desc->ops->get_voltage_sel) {
4024 sel = rdev->desc->ops->get_voltage_sel(rdev);
4027 ret = rdev->desc->ops->list_voltage(rdev, sel);
4028 } else if (rdev->desc->ops->get_voltage) {
4029 ret = rdev->desc->ops->get_voltage(rdev);
4030 } else if (rdev->desc->ops->list_voltage) {
4031 ret = rdev->desc->ops->list_voltage(rdev, 0);
4032 } else if (rdev->desc->fixed_uV && (rdev->desc->n_voltages == 1)) {
4033 ret = rdev->desc->fixed_uV;
4034 } else if (rdev->supply) {
4035 ret = regulator_get_voltage_rdev(rdev->supply->rdev);
4042 return ret - rdev->constraints->uV_offset;
4044 EXPORT_SYMBOL_GPL(regulator_get_voltage_rdev);
4047 * regulator_get_voltage - get regulator output voltage
4048 * @regulator: regulator source
4050 * This returns the current regulator voltage in uV.
4052 * NOTE: If the regulator is disabled it will return the voltage value. This
4053 * function should not be used to determine regulator state.
4055 int regulator_get_voltage(struct regulator *regulator)
4057 struct ww_acquire_ctx ww_ctx;
4060 regulator_lock_dependent(regulator->rdev, &ww_ctx);
4061 ret = regulator_get_voltage_rdev(regulator->rdev);
4062 regulator_unlock_dependent(regulator->rdev, &ww_ctx);
4066 EXPORT_SYMBOL_GPL(regulator_get_voltage);
4069 * regulator_set_current_limit - set regulator output current limit
4070 * @regulator: regulator source
4071 * @min_uA: Minimum supported current in uA
4072 * @max_uA: Maximum supported current in uA
4074 * Sets current sink to the desired output current. This can be set during
4075 * any regulator state. IOW, regulator can be disabled or enabled.
4077 * If the regulator is enabled then the current will change to the new value
4078 * immediately otherwise if the regulator is disabled the regulator will
4079 * output at the new current when enabled.
4081 * NOTE: Regulator system constraints must be set for this regulator before
4082 * calling this function otherwise this call will fail.
4084 int regulator_set_current_limit(struct regulator *regulator,
4085 int min_uA, int max_uA)
4087 struct regulator_dev *rdev = regulator->rdev;
4090 regulator_lock(rdev);
4093 if (!rdev->desc->ops->set_current_limit) {
4098 /* constraints check */
4099 ret = regulator_check_current_limit(rdev, &min_uA, &max_uA);
4103 ret = rdev->desc->ops->set_current_limit(rdev, min_uA, max_uA);
4105 regulator_unlock(rdev);
4108 EXPORT_SYMBOL_GPL(regulator_set_current_limit);
4110 static int _regulator_get_current_limit_unlocked(struct regulator_dev *rdev)
4113 if (!rdev->desc->ops->get_current_limit)
4116 return rdev->desc->ops->get_current_limit(rdev);
4119 static int _regulator_get_current_limit(struct regulator_dev *rdev)
4123 regulator_lock(rdev);
4124 ret = _regulator_get_current_limit_unlocked(rdev);
4125 regulator_unlock(rdev);
4131 * regulator_get_current_limit - get regulator output current
4132 * @regulator: regulator source
4134 * This returns the current supplied by the specified current sink in uA.
4136 * NOTE: If the regulator is disabled it will return the current value. This
4137 * function should not be used to determine regulator state.
4139 int regulator_get_current_limit(struct regulator *regulator)
4141 return _regulator_get_current_limit(regulator->rdev);
4143 EXPORT_SYMBOL_GPL(regulator_get_current_limit);
4146 * regulator_set_mode - set regulator operating mode
4147 * @regulator: regulator source
4148 * @mode: operating mode - one of the REGULATOR_MODE constants
4150 * Set regulator operating mode to increase regulator efficiency or improve
4151 * regulation performance.
4153 * NOTE: Regulator system constraints must be set for this regulator before
4154 * calling this function otherwise this call will fail.
4156 int regulator_set_mode(struct regulator *regulator, unsigned int mode)
4158 struct regulator_dev *rdev = regulator->rdev;
4160 int regulator_curr_mode;
4162 regulator_lock(rdev);
4165 if (!rdev->desc->ops->set_mode) {
4170 /* return if the same mode is requested */
4171 if (rdev->desc->ops->get_mode) {
4172 regulator_curr_mode = rdev->desc->ops->get_mode(rdev);
4173 if (regulator_curr_mode == mode) {
4179 /* constraints check */
4180 ret = regulator_mode_constrain(rdev, &mode);
4184 ret = rdev->desc->ops->set_mode(rdev, mode);
4186 regulator_unlock(rdev);
4189 EXPORT_SYMBOL_GPL(regulator_set_mode);
4191 static unsigned int _regulator_get_mode_unlocked(struct regulator_dev *rdev)
4194 if (!rdev->desc->ops->get_mode)
4197 return rdev->desc->ops->get_mode(rdev);
4200 static unsigned int _regulator_get_mode(struct regulator_dev *rdev)
4204 regulator_lock(rdev);
4205 ret = _regulator_get_mode_unlocked(rdev);
4206 regulator_unlock(rdev);
4212 * regulator_get_mode - get regulator operating mode
4213 * @regulator: regulator source
4215 * Get the current regulator operating mode.
4217 unsigned int regulator_get_mode(struct regulator *regulator)
4219 return _regulator_get_mode(regulator->rdev);
4221 EXPORT_SYMBOL_GPL(regulator_get_mode);
4223 static int _regulator_get_error_flags(struct regulator_dev *rdev,
4224 unsigned int *flags)
4228 regulator_lock(rdev);
4231 if (!rdev->desc->ops->get_error_flags) {
4236 ret = rdev->desc->ops->get_error_flags(rdev, flags);
4238 regulator_unlock(rdev);
4243 * regulator_get_error_flags - get regulator error information
4244 * @regulator: regulator source
4245 * @flags: pointer to store error flags
4247 * Get the current regulator error information.
4249 int regulator_get_error_flags(struct regulator *regulator,
4250 unsigned int *flags)
4252 return _regulator_get_error_flags(regulator->rdev, flags);
4254 EXPORT_SYMBOL_GPL(regulator_get_error_flags);
4257 * regulator_set_load - set regulator load
4258 * @regulator: regulator source
4259 * @uA_load: load current
4261 * Notifies the regulator core of a new device load. This is then used by
4262 * DRMS (if enabled by constraints) to set the most efficient regulator
4263 * operating mode for the new regulator loading.
4265 * Consumer devices notify their supply regulator of the maximum power
4266 * they will require (can be taken from device datasheet in the power
4267 * consumption tables) when they change operational status and hence power
4268 * state. Examples of operational state changes that can affect power
4269 * consumption are :-
4271 * o Device is opened / closed.
4272 * o Device I/O is about to begin or has just finished.
4273 * o Device is idling in between work.
4275 * This information is also exported via sysfs to userspace.
4277 * DRMS will sum the total requested load on the regulator and change
4278 * to the most efficient operating mode if platform constraints allow.
4280 * NOTE: when a regulator consumer requests to have a regulator
4281 * disabled then any load that consumer requested no longer counts
4282 * toward the total requested load. If the regulator is re-enabled
4283 * then the previously requested load will start counting again.
4285 * If a regulator is an always-on regulator then an individual consumer's
4286 * load will still be removed if that consumer is fully disabled.
4288 * On error a negative errno is returned.
4290 int regulator_set_load(struct regulator *regulator, int uA_load)
4292 struct regulator_dev *rdev = regulator->rdev;
4296 regulator_lock(rdev);
4297 old_uA_load = regulator->uA_load;
4298 regulator->uA_load = uA_load;
4299 if (regulator->enable_count && old_uA_load != uA_load) {
4300 ret = drms_uA_update(rdev);
4302 regulator->uA_load = old_uA_load;
4304 regulator_unlock(rdev);
4308 EXPORT_SYMBOL_GPL(regulator_set_load);
4311 * regulator_allow_bypass - allow the regulator to go into bypass mode
4313 * @regulator: Regulator to configure
4314 * @enable: enable or disable bypass mode
4316 * Allow the regulator to go into bypass mode if all other consumers
4317 * for the regulator also enable bypass mode and the machine
4318 * constraints allow this. Bypass mode means that the regulator is
4319 * simply passing the input directly to the output with no regulation.
4321 int regulator_allow_bypass(struct regulator *regulator, bool enable)
4323 struct regulator_dev *rdev = regulator->rdev;
4326 if (!rdev->desc->ops->set_bypass)
4329 if (!regulator_ops_is_valid(rdev, REGULATOR_CHANGE_BYPASS))
4332 regulator_lock(rdev);
4334 if (enable && !regulator->bypass) {
4335 rdev->bypass_count++;
4337 if (rdev->bypass_count == rdev->open_count) {
4338 ret = rdev->desc->ops->set_bypass(rdev, enable);
4340 rdev->bypass_count--;
4343 } else if (!enable && regulator->bypass) {
4344 rdev->bypass_count--;
4346 if (rdev->bypass_count != rdev->open_count) {
4347 ret = rdev->desc->ops->set_bypass(rdev, enable);
4349 rdev->bypass_count++;
4354 regulator->bypass = enable;
4356 regulator_unlock(rdev);
4360 EXPORT_SYMBOL_GPL(regulator_allow_bypass);
4363 * regulator_register_notifier - register regulator event notifier
4364 * @regulator: regulator source
4365 * @nb: notifier block
4367 * Register notifier block to receive regulator events.
4369 int regulator_register_notifier(struct regulator *regulator,
4370 struct notifier_block *nb)
4372 return blocking_notifier_chain_register(®ulator->rdev->notifier,
4375 EXPORT_SYMBOL_GPL(regulator_register_notifier);
4378 * regulator_unregister_notifier - unregister regulator event notifier
4379 * @regulator: regulator source
4380 * @nb: notifier block
4382 * Unregister regulator event notifier block.
4384 int regulator_unregister_notifier(struct regulator *regulator,
4385 struct notifier_block *nb)
4387 return blocking_notifier_chain_unregister(®ulator->rdev->notifier,
4390 EXPORT_SYMBOL_GPL(regulator_unregister_notifier);
4392 /* notify regulator consumers and downstream regulator consumers.
4393 * Note mutex must be held by caller.
4395 static int _notifier_call_chain(struct regulator_dev *rdev,
4396 unsigned long event, void *data)
4398 /* call rdev chain first */
4399 return blocking_notifier_call_chain(&rdev->notifier, event, data);
4403 * regulator_bulk_get - get multiple regulator consumers
4405 * @dev: Device to supply
4406 * @num_consumers: Number of consumers to register
4407 * @consumers: Configuration of consumers; clients are stored here.
4409 * @return 0 on success, an errno on failure.
4411 * This helper function allows drivers to get several regulator
4412 * consumers in one operation. If any of the regulators cannot be
4413 * acquired then any regulators that were allocated will be freed
4414 * before returning to the caller.
4416 int regulator_bulk_get(struct device *dev, int num_consumers,
4417 struct regulator_bulk_data *consumers)
4422 for (i = 0; i < num_consumers; i++)
4423 consumers[i].consumer = NULL;
4425 for (i = 0; i < num_consumers; i++) {
4426 consumers[i].consumer = regulator_get(dev,
4427 consumers[i].supply);
4428 if (IS_ERR(consumers[i].consumer)) {
4429 ret = PTR_ERR(consumers[i].consumer);
4430 consumers[i].consumer = NULL;
4438 if (ret != -EPROBE_DEFER)
4439 dev_err(dev, "Failed to get supply '%s': %d\n",
4440 consumers[i].supply, ret);
4442 dev_dbg(dev, "Failed to get supply '%s', deferring\n",
4443 consumers[i].supply);
4446 regulator_put(consumers[i].consumer);
4450 EXPORT_SYMBOL_GPL(regulator_bulk_get);
4452 static void regulator_bulk_enable_async(void *data, async_cookie_t cookie)
4454 struct regulator_bulk_data *bulk = data;
4456 bulk->ret = regulator_enable(bulk->consumer);
4460 * regulator_bulk_enable - enable multiple regulator consumers
4462 * @num_consumers: Number of consumers
4463 * @consumers: Consumer data; clients are stored here.
4464 * @return 0 on success, an errno on failure
4466 * This convenience API allows consumers to enable multiple regulator
4467 * clients in a single API call. If any consumers cannot be enabled
4468 * then any others that were enabled will be disabled again prior to
4471 int regulator_bulk_enable(int num_consumers,
4472 struct regulator_bulk_data *consumers)
4474 ASYNC_DOMAIN_EXCLUSIVE(async_domain);
4478 for (i = 0; i < num_consumers; i++) {
4479 async_schedule_domain(regulator_bulk_enable_async,
4480 &consumers[i], &async_domain);
4483 async_synchronize_full_domain(&async_domain);
4485 /* If any consumer failed we need to unwind any that succeeded */
4486 for (i = 0; i < num_consumers; i++) {
4487 if (consumers[i].ret != 0) {
4488 ret = consumers[i].ret;
4496 for (i = 0; i < num_consumers; i++) {
4497 if (consumers[i].ret < 0)
4498 pr_err("Failed to enable %s: %d\n", consumers[i].supply,
4501 regulator_disable(consumers[i].consumer);
4506 EXPORT_SYMBOL_GPL(regulator_bulk_enable);
4509 * regulator_bulk_disable - disable multiple regulator consumers
4511 * @num_consumers: Number of consumers
4512 * @consumers: Consumer data; clients are stored here.
4513 * @return 0 on success, an errno on failure
4515 * This convenience API allows consumers to disable multiple regulator
4516 * clients in a single API call. If any consumers cannot be disabled
4517 * then any others that were disabled will be enabled again prior to
4520 int regulator_bulk_disable(int num_consumers,
4521 struct regulator_bulk_data *consumers)
4526 for (i = num_consumers - 1; i >= 0; --i) {
4527 ret = regulator_disable(consumers[i].consumer);
4535 pr_err("Failed to disable %s: %d\n", consumers[i].supply, ret);
4536 for (++i; i < num_consumers; ++i) {
4537 r = regulator_enable(consumers[i].consumer);
4539 pr_err("Failed to re-enable %s: %d\n",
4540 consumers[i].supply, r);
4545 EXPORT_SYMBOL_GPL(regulator_bulk_disable);
4548 * regulator_bulk_force_disable - force disable multiple regulator consumers
4550 * @num_consumers: Number of consumers
4551 * @consumers: Consumer data; clients are stored here.
4552 * @return 0 on success, an errno on failure
4554 * This convenience API allows consumers to forcibly disable multiple regulator
4555 * clients in a single API call.
4556 * NOTE: This should be used for situations when device damage will
4557 * likely occur if the regulators are not disabled (e.g. over temp).
4558 * Although regulator_force_disable function call for some consumers can
4559 * return error numbers, the function is called for all consumers.
4561 int regulator_bulk_force_disable(int num_consumers,
4562 struct regulator_bulk_data *consumers)
4567 for (i = 0; i < num_consumers; i++) {
4569 regulator_force_disable(consumers[i].consumer);
4571 /* Store first error for reporting */
4572 if (consumers[i].ret && !ret)
4573 ret = consumers[i].ret;
4578 EXPORT_SYMBOL_GPL(regulator_bulk_force_disable);
4581 * regulator_bulk_free - free multiple regulator consumers
4583 * @num_consumers: Number of consumers
4584 * @consumers: Consumer data; clients are stored here.
4586 * This convenience API allows consumers to free multiple regulator
4587 * clients in a single API call.
4589 void regulator_bulk_free(int num_consumers,
4590 struct regulator_bulk_data *consumers)
4594 for (i = 0; i < num_consumers; i++) {
4595 regulator_put(consumers[i].consumer);
4596 consumers[i].consumer = NULL;
4599 EXPORT_SYMBOL_GPL(regulator_bulk_free);
4602 * regulator_notifier_call_chain - call regulator event notifier
4603 * @rdev: regulator source
4604 * @event: notifier block
4605 * @data: callback-specific data.
4607 * Called by regulator drivers to notify clients a regulator event has
4608 * occurred. We also notify regulator clients downstream.
4609 * Note lock must be held by caller.
4611 int regulator_notifier_call_chain(struct regulator_dev *rdev,
4612 unsigned long event, void *data)
4614 lockdep_assert_held_once(&rdev->mutex.base);
4616 _notifier_call_chain(rdev, event, data);
4620 EXPORT_SYMBOL_GPL(regulator_notifier_call_chain);
4623 * regulator_mode_to_status - convert a regulator mode into a status
4625 * @mode: Mode to convert
4627 * Convert a regulator mode into a status.
4629 int regulator_mode_to_status(unsigned int mode)
4632 case REGULATOR_MODE_FAST:
4633 return REGULATOR_STATUS_FAST;
4634 case REGULATOR_MODE_NORMAL:
4635 return REGULATOR_STATUS_NORMAL;
4636 case REGULATOR_MODE_IDLE:
4637 return REGULATOR_STATUS_IDLE;
4638 case REGULATOR_MODE_STANDBY:
4639 return REGULATOR_STATUS_STANDBY;
4641 return REGULATOR_STATUS_UNDEFINED;
4644 EXPORT_SYMBOL_GPL(regulator_mode_to_status);
4646 static struct attribute *regulator_dev_attrs[] = {
4647 &dev_attr_name.attr,
4648 &dev_attr_num_users.attr,
4649 &dev_attr_type.attr,
4650 &dev_attr_microvolts.attr,
4651 &dev_attr_microamps.attr,
4652 &dev_attr_opmode.attr,
4653 &dev_attr_state.attr,
4654 &dev_attr_status.attr,
4655 &dev_attr_bypass.attr,
4656 &dev_attr_requested_microamps.attr,
4657 &dev_attr_min_microvolts.attr,
4658 &dev_attr_max_microvolts.attr,
4659 &dev_attr_min_microamps.attr,
4660 &dev_attr_max_microamps.attr,
4661 &dev_attr_suspend_standby_state.attr,
4662 &dev_attr_suspend_mem_state.attr,
4663 &dev_attr_suspend_disk_state.attr,
4664 &dev_attr_suspend_standby_microvolts.attr,
4665 &dev_attr_suspend_mem_microvolts.attr,
4666 &dev_attr_suspend_disk_microvolts.attr,
4667 &dev_attr_suspend_standby_mode.attr,
4668 &dev_attr_suspend_mem_mode.attr,
4669 &dev_attr_suspend_disk_mode.attr,
4674 * To avoid cluttering sysfs (and memory) with useless state, only
4675 * create attributes that can be meaningfully displayed.
4677 static umode_t regulator_attr_is_visible(struct kobject *kobj,
4678 struct attribute *attr, int idx)
4680 struct device *dev = kobj_to_dev(kobj);
4681 struct regulator_dev *rdev = dev_to_rdev(dev);
4682 const struct regulator_ops *ops = rdev->desc->ops;
4683 umode_t mode = attr->mode;
4685 /* these three are always present */
4686 if (attr == &dev_attr_name.attr ||
4687 attr == &dev_attr_num_users.attr ||
4688 attr == &dev_attr_type.attr)
4691 /* some attributes need specific methods to be displayed */
4692 if (attr == &dev_attr_microvolts.attr) {
4693 if ((ops->get_voltage && ops->get_voltage(rdev) >= 0) ||
4694 (ops->get_voltage_sel && ops->get_voltage_sel(rdev) >= 0) ||
4695 (ops->list_voltage && ops->list_voltage(rdev, 0) >= 0) ||
4696 (rdev->desc->fixed_uV && rdev->desc->n_voltages == 1))
4701 if (attr == &dev_attr_microamps.attr)
4702 return ops->get_current_limit ? mode : 0;
4704 if (attr == &dev_attr_opmode.attr)
4705 return ops->get_mode ? mode : 0;
4707 if (attr == &dev_attr_state.attr)
4708 return (rdev->ena_pin || ops->is_enabled) ? mode : 0;
4710 if (attr == &dev_attr_status.attr)
4711 return ops->get_status ? mode : 0;
4713 if (attr == &dev_attr_bypass.attr)
4714 return ops->get_bypass ? mode : 0;
4716 /* constraints need specific supporting methods */
4717 if (attr == &dev_attr_min_microvolts.attr ||
4718 attr == &dev_attr_max_microvolts.attr)
4719 return (ops->set_voltage || ops->set_voltage_sel) ? mode : 0;
4721 if (attr == &dev_attr_min_microamps.attr ||
4722 attr == &dev_attr_max_microamps.attr)
4723 return ops->set_current_limit ? mode : 0;
4725 if (attr == &dev_attr_suspend_standby_state.attr ||
4726 attr == &dev_attr_suspend_mem_state.attr ||
4727 attr == &dev_attr_suspend_disk_state.attr)
4730 if (attr == &dev_attr_suspend_standby_microvolts.attr ||
4731 attr == &dev_attr_suspend_mem_microvolts.attr ||
4732 attr == &dev_attr_suspend_disk_microvolts.attr)
4733 return ops->set_suspend_voltage ? mode : 0;
4735 if (attr == &dev_attr_suspend_standby_mode.attr ||
4736 attr == &dev_attr_suspend_mem_mode.attr ||
4737 attr == &dev_attr_suspend_disk_mode.attr)
4738 return ops->set_suspend_mode ? mode : 0;
4743 static const struct attribute_group regulator_dev_group = {
4744 .attrs = regulator_dev_attrs,
4745 .is_visible = regulator_attr_is_visible,
4748 static const struct attribute_group *regulator_dev_groups[] = {
4749 ®ulator_dev_group,
4753 static void regulator_dev_release(struct device *dev)
4755 struct regulator_dev *rdev = dev_get_drvdata(dev);
4757 kfree(rdev->constraints);
4758 of_node_put(rdev->dev.of_node);
4762 static void rdev_init_debugfs(struct regulator_dev *rdev)
4764 struct device *parent = rdev->dev.parent;
4765 const char *rname = rdev_get_name(rdev);
4766 char name[NAME_MAX];
4768 /* Avoid duplicate debugfs directory names */
4769 if (parent && rname == rdev->desc->name) {
4770 snprintf(name, sizeof(name), "%s-%s", dev_name(parent),
4775 rdev->debugfs = debugfs_create_dir(rname, debugfs_root);
4776 if (!rdev->debugfs) {
4777 rdev_warn(rdev, "Failed to create debugfs directory\n");
4781 debugfs_create_u32("use_count", 0444, rdev->debugfs,
4783 debugfs_create_u32("open_count", 0444, rdev->debugfs,
4785 debugfs_create_u32("bypass_count", 0444, rdev->debugfs,
4786 &rdev->bypass_count);
4789 static int regulator_register_resolve_supply(struct device *dev, void *data)
4791 struct regulator_dev *rdev = dev_to_rdev(dev);
4793 if (regulator_resolve_supply(rdev))
4794 rdev_dbg(rdev, "unable to resolve supply\n");
4799 int regulator_coupler_register(struct regulator_coupler *coupler)
4801 mutex_lock(®ulator_list_mutex);
4802 list_add_tail(&coupler->list, ®ulator_coupler_list);
4803 mutex_unlock(®ulator_list_mutex);
4808 static struct regulator_coupler *
4809 regulator_find_coupler(struct regulator_dev *rdev)
4811 struct regulator_coupler *coupler;
4815 * Note that regulators are appended to the list and the generic
4816 * coupler is registered first, hence it will be attached at last
4819 list_for_each_entry_reverse(coupler, ®ulator_coupler_list, list) {
4820 err = coupler->attach_regulator(coupler, rdev);
4822 if (!coupler->balance_voltage &&
4823 rdev->coupling_desc.n_coupled > 2)
4824 goto err_unsupported;
4830 return ERR_PTR(err);
4838 return ERR_PTR(-EINVAL);
4841 if (coupler->detach_regulator)
4842 coupler->detach_regulator(coupler, rdev);
4845 "Voltage balancing for multiple regulator couples is unimplemented\n");
4847 return ERR_PTR(-EPERM);
4850 static void regulator_resolve_coupling(struct regulator_dev *rdev)
4852 struct regulator_coupler *coupler = rdev->coupling_desc.coupler;
4853 struct coupling_desc *c_desc = &rdev->coupling_desc;
4854 int n_coupled = c_desc->n_coupled;
4855 struct regulator_dev *c_rdev;
4858 for (i = 1; i < n_coupled; i++) {
4859 /* already resolved */
4860 if (c_desc->coupled_rdevs[i])
4863 c_rdev = of_parse_coupled_regulator(rdev, i - 1);
4868 if (c_rdev->coupling_desc.coupler != coupler) {
4869 rdev_err(rdev, "coupler mismatch with %s\n",
4870 rdev_get_name(c_rdev));
4874 regulator_lock(c_rdev);
4876 c_desc->coupled_rdevs[i] = c_rdev;
4877 c_desc->n_resolved++;
4879 regulator_unlock(c_rdev);
4881 regulator_resolve_coupling(c_rdev);
4885 static void regulator_remove_coupling(struct regulator_dev *rdev)
4887 struct regulator_coupler *coupler = rdev->coupling_desc.coupler;
4888 struct coupling_desc *__c_desc, *c_desc = &rdev->coupling_desc;
4889 struct regulator_dev *__c_rdev, *c_rdev;
4890 unsigned int __n_coupled, n_coupled;
4894 n_coupled = c_desc->n_coupled;
4896 for (i = 1; i < n_coupled; i++) {
4897 c_rdev = c_desc->coupled_rdevs[i];
4902 regulator_lock(c_rdev);
4904 __c_desc = &c_rdev->coupling_desc;
4905 __n_coupled = __c_desc->n_coupled;
4907 for (k = 1; k < __n_coupled; k++) {
4908 __c_rdev = __c_desc->coupled_rdevs[k];
4910 if (__c_rdev == rdev) {
4911 __c_desc->coupled_rdevs[k] = NULL;
4912 __c_desc->n_resolved--;
4917 regulator_unlock(c_rdev);
4919 c_desc->coupled_rdevs[i] = NULL;
4920 c_desc->n_resolved--;
4923 if (coupler && coupler->detach_regulator) {
4924 err = coupler->detach_regulator(coupler, rdev);
4926 rdev_err(rdev, "failed to detach from coupler: %d\n",
4930 kfree(rdev->coupling_desc.coupled_rdevs);
4931 rdev->coupling_desc.coupled_rdevs = NULL;
4934 static int regulator_init_coupling(struct regulator_dev *rdev)
4936 int err, n_phandles;
4939 if (!IS_ENABLED(CONFIG_OF))
4942 n_phandles = of_get_n_coupled(rdev);
4944 alloc_size = sizeof(*rdev) * (n_phandles + 1);
4946 rdev->coupling_desc.coupled_rdevs = kzalloc(alloc_size, GFP_KERNEL);
4947 if (!rdev->coupling_desc.coupled_rdevs)
4951 * Every regulator should always have coupling descriptor filled with
4952 * at least pointer to itself.
4954 rdev->coupling_desc.coupled_rdevs[0] = rdev;
4955 rdev->coupling_desc.n_coupled = n_phandles + 1;
4956 rdev->coupling_desc.n_resolved++;
4958 /* regulator isn't coupled */
4959 if (n_phandles == 0)
4962 if (!of_check_coupling_data(rdev))
4965 rdev->coupling_desc.coupler = regulator_find_coupler(rdev);
4966 if (IS_ERR(rdev->coupling_desc.coupler)) {
4967 err = PTR_ERR(rdev->coupling_desc.coupler);
4968 rdev_err(rdev, "failed to get coupler: %d\n", err);
4975 static int generic_coupler_attach(struct regulator_coupler *coupler,
4976 struct regulator_dev *rdev)
4978 if (rdev->coupling_desc.n_coupled > 2) {
4980 "Voltage balancing for multiple regulator couples is unimplemented\n");
4984 if (!rdev->constraints->always_on) {
4986 "Coupling of a non always-on regulator is unimplemented\n");
4993 static struct regulator_coupler generic_regulator_coupler = {
4994 .attach_regulator = generic_coupler_attach,
4998 * regulator_register - register regulator
4999 * @regulator_desc: regulator to register
5000 * @cfg: runtime configuration for regulator
5002 * Called by regulator drivers to register a regulator.
5003 * Returns a valid pointer to struct regulator_dev on success
5004 * or an ERR_PTR() on error.
5006 struct regulator_dev *
5007 regulator_register(const struct regulator_desc *regulator_desc,
5008 const struct regulator_config *cfg)
5010 const struct regulation_constraints *constraints = NULL;
5011 const struct regulator_init_data *init_data;
5012 struct regulator_config *config = NULL;
5013 static atomic_t regulator_no = ATOMIC_INIT(-1);
5014 struct regulator_dev *rdev;
5015 bool dangling_cfg_gpiod = false;
5016 bool dangling_of_gpiod = false;
5017 bool reg_device_fail = false;
5022 return ERR_PTR(-EINVAL);
5024 dangling_cfg_gpiod = true;
5025 if (regulator_desc == NULL) {
5033 if (regulator_desc->name == NULL || regulator_desc->ops == NULL) {
5038 if (regulator_desc->type != REGULATOR_VOLTAGE &&
5039 regulator_desc->type != REGULATOR_CURRENT) {
5044 /* Only one of each should be implemented */
5045 WARN_ON(regulator_desc->ops->get_voltage &&
5046 regulator_desc->ops->get_voltage_sel);
5047 WARN_ON(regulator_desc->ops->set_voltage &&
5048 regulator_desc->ops->set_voltage_sel);
5050 /* If we're using selectors we must implement list_voltage. */
5051 if (regulator_desc->ops->get_voltage_sel &&
5052 !regulator_desc->ops->list_voltage) {
5056 if (regulator_desc->ops->set_voltage_sel &&
5057 !regulator_desc->ops->list_voltage) {
5062 rdev = kzalloc(sizeof(struct regulator_dev), GFP_KERNEL);
5069 * Duplicate the config so the driver could override it after
5070 * parsing init data.
5072 config = kmemdup(cfg, sizeof(*cfg), GFP_KERNEL);
5073 if (config == NULL) {
5079 init_data = regulator_of_get_init_data(dev, regulator_desc, config,
5080 &rdev->dev.of_node);
5083 * Sometimes not all resources are probed already so we need to take
5084 * that into account. This happens most the time if the ena_gpiod comes
5085 * from a gpio extender or something else.
5087 if (PTR_ERR(init_data) == -EPROBE_DEFER) {
5090 ret = -EPROBE_DEFER;
5095 * We need to keep track of any GPIO descriptor coming from the
5096 * device tree until we have handled it over to the core. If the
5097 * config that was passed in to this function DOES NOT contain
5098 * a descriptor, and the config after this call DOES contain
5099 * a descriptor, we definitely got one from parsing the device
5102 if (!cfg->ena_gpiod && config->ena_gpiod)
5103 dangling_of_gpiod = true;
5105 init_data = config->init_data;
5106 rdev->dev.of_node = of_node_get(config->of_node);
5109 ww_mutex_init(&rdev->mutex, ®ulator_ww_class);
5110 rdev->reg_data = config->driver_data;
5111 rdev->owner = regulator_desc->owner;
5112 rdev->desc = regulator_desc;
5114 rdev->regmap = config->regmap;
5115 else if (dev_get_regmap(dev, NULL))
5116 rdev->regmap = dev_get_regmap(dev, NULL);
5117 else if (dev->parent)
5118 rdev->regmap = dev_get_regmap(dev->parent, NULL);
5119 INIT_LIST_HEAD(&rdev->consumer_list);
5120 INIT_LIST_HEAD(&rdev->list);
5121 BLOCKING_INIT_NOTIFIER_HEAD(&rdev->notifier);
5122 INIT_DELAYED_WORK(&rdev->disable_work, regulator_disable_work);
5124 /* preform any regulator specific init */
5125 if (init_data && init_data->regulator_init) {
5126 ret = init_data->regulator_init(rdev->reg_data);
5131 if (config->ena_gpiod) {
5132 mutex_lock(®ulator_list_mutex);
5133 ret = regulator_ena_gpio_request(rdev, config);
5134 mutex_unlock(®ulator_list_mutex);
5136 rdev_err(rdev, "Failed to request enable GPIO: %d\n",
5140 /* The regulator core took over the GPIO descriptor */
5141 dangling_cfg_gpiod = false;
5142 dangling_of_gpiod = false;
5145 /* register with sysfs */
5146 rdev->dev.class = ®ulator_class;
5147 rdev->dev.parent = dev;
5148 dev_set_name(&rdev->dev, "regulator.%lu",
5149 (unsigned long) atomic_inc_return(®ulator_no));
5151 /* set regulator constraints */
5153 constraints = &init_data->constraints;
5155 if (init_data && init_data->supply_regulator)
5156 rdev->supply_name = init_data->supply_regulator;
5157 else if (regulator_desc->supply_name)
5158 rdev->supply_name = regulator_desc->supply_name;
5161 * Attempt to resolve the regulator supply, if specified,
5162 * but don't return an error if we fail because we will try
5163 * to resolve it again later as more regulators are added.
5165 if (regulator_resolve_supply(rdev))
5166 rdev_dbg(rdev, "unable to resolve supply\n");
5168 ret = set_machine_constraints(rdev, constraints);
5172 mutex_lock(®ulator_list_mutex);
5173 ret = regulator_init_coupling(rdev);
5174 mutex_unlock(®ulator_list_mutex);
5178 /* add consumers devices */
5180 mutex_lock(®ulator_list_mutex);
5181 for (i = 0; i < init_data->num_consumer_supplies; i++) {
5182 ret = set_consumer_device_supply(rdev,
5183 init_data->consumer_supplies[i].dev_name,
5184 init_data->consumer_supplies[i].supply);
5186 mutex_unlock(®ulator_list_mutex);
5187 dev_err(dev, "Failed to set supply %s\n",
5188 init_data->consumer_supplies[i].supply);
5189 goto unset_supplies;
5192 mutex_unlock(®ulator_list_mutex);
5195 if (!rdev->desc->ops->get_voltage &&
5196 !rdev->desc->ops->list_voltage &&
5197 !rdev->desc->fixed_uV)
5198 rdev->is_switch = true;
5200 dev_set_drvdata(&rdev->dev, rdev);
5201 ret = device_register(&rdev->dev);
5203 reg_device_fail = true;
5204 goto unset_supplies;
5207 rdev_init_debugfs(rdev);
5209 /* try to resolve regulators coupling since a new one was registered */
5210 mutex_lock(®ulator_list_mutex);
5211 regulator_resolve_coupling(rdev);
5212 mutex_unlock(®ulator_list_mutex);
5214 /* try to resolve regulators supply since a new one was registered */
5215 class_for_each_device(®ulator_class, NULL, NULL,
5216 regulator_register_resolve_supply);
5221 mutex_lock(®ulator_list_mutex);
5222 unset_regulator_supplies(rdev);
5223 regulator_remove_coupling(rdev);
5224 mutex_unlock(®ulator_list_mutex);
5226 kfree(rdev->coupling_desc.coupled_rdevs);
5227 kfree(rdev->constraints);
5228 mutex_lock(®ulator_list_mutex);
5229 regulator_ena_gpio_free(rdev);
5230 mutex_unlock(®ulator_list_mutex);
5232 if (dangling_of_gpiod)
5233 gpiod_put(config->ena_gpiod);
5234 if (reg_device_fail)
5235 put_device(&rdev->dev);
5240 if (dangling_cfg_gpiod)
5241 gpiod_put(cfg->ena_gpiod);
5242 return ERR_PTR(ret);
5244 EXPORT_SYMBOL_GPL(regulator_register);
5247 * regulator_unregister - unregister regulator
5248 * @rdev: regulator to unregister
5250 * Called by regulator drivers to unregister a regulator.
5252 void regulator_unregister(struct regulator_dev *rdev)
5258 while (rdev->use_count--)
5259 regulator_disable(rdev->supply);
5260 regulator_put(rdev->supply);
5263 flush_work(&rdev->disable_work.work);
5265 mutex_lock(®ulator_list_mutex);
5267 debugfs_remove_recursive(rdev->debugfs);
5268 WARN_ON(rdev->open_count);
5269 regulator_remove_coupling(rdev);
5270 unset_regulator_supplies(rdev);
5271 list_del(&rdev->list);
5272 regulator_ena_gpio_free(rdev);
5273 device_unregister(&rdev->dev);
5275 mutex_unlock(®ulator_list_mutex);
5277 EXPORT_SYMBOL_GPL(regulator_unregister);
5279 #ifdef CONFIG_SUSPEND
5281 * regulator_suspend - prepare regulators for system wide suspend
5282 * @dev: ``&struct device`` pointer that is passed to _regulator_suspend()
5284 * Configure each regulator with it's suspend operating parameters for state.
5286 static int regulator_suspend(struct device *dev)
5288 struct regulator_dev *rdev = dev_to_rdev(dev);
5289 suspend_state_t state = pm_suspend_target_state;
5292 regulator_lock(rdev);
5293 ret = suspend_set_state(rdev, state);
5294 regulator_unlock(rdev);
5299 static int regulator_resume(struct device *dev)
5301 suspend_state_t state = pm_suspend_target_state;
5302 struct regulator_dev *rdev = dev_to_rdev(dev);
5303 struct regulator_state *rstate;
5306 rstate = regulator_get_suspend_state(rdev, state);
5310 regulator_lock(rdev);
5312 if (rdev->desc->ops->resume &&
5313 (rstate->enabled == ENABLE_IN_SUSPEND ||
5314 rstate->enabled == DISABLE_IN_SUSPEND))
5315 ret = rdev->desc->ops->resume(rdev);
5317 regulator_unlock(rdev);
5321 #else /* !CONFIG_SUSPEND */
5323 #define regulator_suspend NULL
5324 #define regulator_resume NULL
5326 #endif /* !CONFIG_SUSPEND */
5329 static const struct dev_pm_ops __maybe_unused regulator_pm_ops = {
5330 .suspend = regulator_suspend,
5331 .resume = regulator_resume,
5335 struct class regulator_class = {
5336 .name = "regulator",
5337 .dev_release = regulator_dev_release,
5338 .dev_groups = regulator_dev_groups,
5340 .pm = ®ulator_pm_ops,
5344 * regulator_has_full_constraints - the system has fully specified constraints
5346 * Calling this function will cause the regulator API to disable all
5347 * regulators which have a zero use count and don't have an always_on
5348 * constraint in a late_initcall.
5350 * The intention is that this will become the default behaviour in a
5351 * future kernel release so users are encouraged to use this facility
5354 void regulator_has_full_constraints(void)
5356 has_full_constraints = 1;
5358 EXPORT_SYMBOL_GPL(regulator_has_full_constraints);
5361 * rdev_get_drvdata - get rdev regulator driver data
5364 * Get rdev regulator driver private data. This call can be used in the
5365 * regulator driver context.
5367 void *rdev_get_drvdata(struct regulator_dev *rdev)
5369 return rdev->reg_data;
5371 EXPORT_SYMBOL_GPL(rdev_get_drvdata);
5374 * regulator_get_drvdata - get regulator driver data
5375 * @regulator: regulator
5377 * Get regulator driver private data. This call can be used in the consumer
5378 * driver context when non API regulator specific functions need to be called.
5380 void *regulator_get_drvdata(struct regulator *regulator)
5382 return regulator->rdev->reg_data;
5384 EXPORT_SYMBOL_GPL(regulator_get_drvdata);
5387 * regulator_set_drvdata - set regulator driver data
5388 * @regulator: regulator
5391 void regulator_set_drvdata(struct regulator *regulator, void *data)
5393 regulator->rdev->reg_data = data;
5395 EXPORT_SYMBOL_GPL(regulator_set_drvdata);
5398 * regulator_get_id - get regulator ID
5401 int rdev_get_id(struct regulator_dev *rdev)
5403 return rdev->desc->id;
5405 EXPORT_SYMBOL_GPL(rdev_get_id);
5407 struct device *rdev_get_dev(struct regulator_dev *rdev)
5411 EXPORT_SYMBOL_GPL(rdev_get_dev);
5413 struct regmap *rdev_get_regmap(struct regulator_dev *rdev)
5415 return rdev->regmap;
5417 EXPORT_SYMBOL_GPL(rdev_get_regmap);
5419 void *regulator_get_init_drvdata(struct regulator_init_data *reg_init_data)
5421 return reg_init_data->driver_data;
5423 EXPORT_SYMBOL_GPL(regulator_get_init_drvdata);
5425 #ifdef CONFIG_DEBUG_FS
5426 static int supply_map_show(struct seq_file *sf, void *data)
5428 struct regulator_map *map;
5430 list_for_each_entry(map, ®ulator_map_list, list) {
5431 seq_printf(sf, "%s -> %s.%s\n",
5432 rdev_get_name(map->regulator), map->dev_name,
5438 DEFINE_SHOW_ATTRIBUTE(supply_map);
5440 struct summary_data {
5442 struct regulator_dev *parent;
5446 static void regulator_summary_show_subtree(struct seq_file *s,
5447 struct regulator_dev *rdev,
5450 static int regulator_summary_show_children(struct device *dev, void *data)
5452 struct regulator_dev *rdev = dev_to_rdev(dev);
5453 struct summary_data *summary_data = data;
5455 if (rdev->supply && rdev->supply->rdev == summary_data->parent)
5456 regulator_summary_show_subtree(summary_data->s, rdev,
5457 summary_data->level + 1);
5462 static void regulator_summary_show_subtree(struct seq_file *s,
5463 struct regulator_dev *rdev,
5466 struct regulation_constraints *c;
5467 struct regulator *consumer;
5468 struct summary_data summary_data;
5469 unsigned int opmode;
5474 opmode = _regulator_get_mode_unlocked(rdev);
5475 seq_printf(s, "%*s%-*s %3d %4d %6d %7s ",
5477 30 - level * 3, rdev_get_name(rdev),
5478 rdev->use_count, rdev->open_count, rdev->bypass_count,
5479 regulator_opmode_to_str(opmode));
5481 seq_printf(s, "%5dmV ", regulator_get_voltage_rdev(rdev) / 1000);
5482 seq_printf(s, "%5dmA ",
5483 _regulator_get_current_limit_unlocked(rdev) / 1000);
5485 c = rdev->constraints;
5487 switch (rdev->desc->type) {
5488 case REGULATOR_VOLTAGE:
5489 seq_printf(s, "%5dmV %5dmV ",
5490 c->min_uV / 1000, c->max_uV / 1000);
5492 case REGULATOR_CURRENT:
5493 seq_printf(s, "%5dmA %5dmA ",
5494 c->min_uA / 1000, c->max_uA / 1000);
5501 list_for_each_entry(consumer, &rdev->consumer_list, list) {
5502 if (consumer->dev && consumer->dev->class == ®ulator_class)
5505 seq_printf(s, "%*s%-*s ",
5506 (level + 1) * 3 + 1, "",
5507 30 - (level + 1) * 3,
5508 consumer->supply_name ? consumer->supply_name :
5509 consumer->dev ? dev_name(consumer->dev) : "deviceless");
5511 switch (rdev->desc->type) {
5512 case REGULATOR_VOLTAGE:
5513 seq_printf(s, "%3d %33dmA%c%5dmV %5dmV",
5514 consumer->enable_count,
5515 consumer->uA_load / 1000,
5516 consumer->uA_load && !consumer->enable_count ?
5518 consumer->voltage[PM_SUSPEND_ON].min_uV / 1000,
5519 consumer->voltage[PM_SUSPEND_ON].max_uV / 1000);
5521 case REGULATOR_CURRENT:
5529 summary_data.level = level;
5530 summary_data.parent = rdev;
5532 class_for_each_device(®ulator_class, NULL, &summary_data,
5533 regulator_summary_show_children);
5536 struct summary_lock_data {
5537 struct ww_acquire_ctx *ww_ctx;
5538 struct regulator_dev **new_contended_rdev;
5539 struct regulator_dev **old_contended_rdev;
5542 static int regulator_summary_lock_one(struct device *dev, void *data)
5544 struct regulator_dev *rdev = dev_to_rdev(dev);
5545 struct summary_lock_data *lock_data = data;
5548 if (rdev != *lock_data->old_contended_rdev) {
5549 ret = regulator_lock_nested(rdev, lock_data->ww_ctx);
5551 if (ret == -EDEADLK)
5552 *lock_data->new_contended_rdev = rdev;
5556 *lock_data->old_contended_rdev = NULL;
5562 static int regulator_summary_unlock_one(struct device *dev, void *data)
5564 struct regulator_dev *rdev = dev_to_rdev(dev);
5565 struct summary_lock_data *lock_data = data;
5568 if (rdev == *lock_data->new_contended_rdev)
5572 regulator_unlock(rdev);
5577 static int regulator_summary_lock_all(struct ww_acquire_ctx *ww_ctx,
5578 struct regulator_dev **new_contended_rdev,
5579 struct regulator_dev **old_contended_rdev)
5581 struct summary_lock_data lock_data;
5584 lock_data.ww_ctx = ww_ctx;
5585 lock_data.new_contended_rdev = new_contended_rdev;
5586 lock_data.old_contended_rdev = old_contended_rdev;
5588 ret = class_for_each_device(®ulator_class, NULL, &lock_data,
5589 regulator_summary_lock_one);
5591 class_for_each_device(®ulator_class, NULL, &lock_data,
5592 regulator_summary_unlock_one);
5597 static void regulator_summary_lock(struct ww_acquire_ctx *ww_ctx)
5599 struct regulator_dev *new_contended_rdev = NULL;
5600 struct regulator_dev *old_contended_rdev = NULL;
5603 mutex_lock(®ulator_list_mutex);
5605 ww_acquire_init(ww_ctx, ®ulator_ww_class);
5608 if (new_contended_rdev) {
5609 ww_mutex_lock_slow(&new_contended_rdev->mutex, ww_ctx);
5610 old_contended_rdev = new_contended_rdev;
5611 old_contended_rdev->ref_cnt++;
5614 err = regulator_summary_lock_all(ww_ctx,
5615 &new_contended_rdev,
5616 &old_contended_rdev);
5618 if (old_contended_rdev)
5619 regulator_unlock(old_contended_rdev);
5621 } while (err == -EDEADLK);
5623 ww_acquire_done(ww_ctx);
5626 static void regulator_summary_unlock(struct ww_acquire_ctx *ww_ctx)
5628 class_for_each_device(®ulator_class, NULL, NULL,
5629 regulator_summary_unlock_one);
5630 ww_acquire_fini(ww_ctx);
5632 mutex_unlock(®ulator_list_mutex);
5635 static int regulator_summary_show_roots(struct device *dev, void *data)
5637 struct regulator_dev *rdev = dev_to_rdev(dev);
5638 struct seq_file *s = data;
5641 regulator_summary_show_subtree(s, rdev, 0);
5646 static int regulator_summary_show(struct seq_file *s, void *data)
5648 struct ww_acquire_ctx ww_ctx;
5650 seq_puts(s, " regulator use open bypass opmode voltage current min max\n");
5651 seq_puts(s, "---------------------------------------------------------------------------------------\n");
5653 regulator_summary_lock(&ww_ctx);
5655 class_for_each_device(®ulator_class, NULL, s,
5656 regulator_summary_show_roots);
5658 regulator_summary_unlock(&ww_ctx);
5662 DEFINE_SHOW_ATTRIBUTE(regulator_summary);
5663 #endif /* CONFIG_DEBUG_FS */
5665 static int __init regulator_init(void)
5669 ret = class_register(®ulator_class);
5671 debugfs_root = debugfs_create_dir("regulator", NULL);
5673 pr_warn("regulator: Failed to create debugfs directory\n");
5675 #ifdef CONFIG_DEBUG_FS
5676 debugfs_create_file("supply_map", 0444, debugfs_root, NULL,
5679 debugfs_create_file("regulator_summary", 0444, debugfs_root,
5680 NULL, ®ulator_summary_fops);
5682 regulator_dummy_init();
5684 regulator_coupler_register(&generic_regulator_coupler);
5689 /* init early to allow our consumers to complete system booting */
5690 core_initcall(regulator_init);
5692 static int regulator_late_cleanup(struct device *dev, void *data)
5694 struct regulator_dev *rdev = dev_to_rdev(dev);
5695 const struct regulator_ops *ops = rdev->desc->ops;
5696 struct regulation_constraints *c = rdev->constraints;
5699 if (c && c->always_on)
5702 if (!regulator_ops_is_valid(rdev, REGULATOR_CHANGE_STATUS))
5705 regulator_lock(rdev);
5707 if (rdev->use_count)
5710 /* If we can't read the status assume it's on. */
5711 if (ops->is_enabled)
5712 enabled = ops->is_enabled(rdev);
5719 if (have_full_constraints()) {
5720 /* We log since this may kill the system if it goes
5722 rdev_info(rdev, "disabling\n");
5723 ret = _regulator_do_disable(rdev);
5725 rdev_err(rdev, "couldn't disable: %d\n", ret);
5727 /* The intention is that in future we will
5728 * assume that full constraints are provided
5729 * so warn even if we aren't going to do
5732 rdev_warn(rdev, "incomplete constraints, leaving on\n");
5736 regulator_unlock(rdev);
5741 static void regulator_init_complete_work_function(struct work_struct *work)
5744 * Regulators may had failed to resolve their input supplies
5745 * when were registered, either because the input supply was
5746 * not registered yet or because its parent device was not
5747 * bound yet. So attempt to resolve the input supplies for
5748 * pending regulators before trying to disable unused ones.
5750 class_for_each_device(®ulator_class, NULL, NULL,
5751 regulator_register_resolve_supply);
5753 /* If we have a full configuration then disable any regulators
5754 * we have permission to change the status for and which are
5755 * not in use or always_on. This is effectively the default
5756 * for DT and ACPI as they have full constraints.
5758 class_for_each_device(®ulator_class, NULL, NULL,
5759 regulator_late_cleanup);
5762 static DECLARE_DELAYED_WORK(regulator_init_complete_work,
5763 regulator_init_complete_work_function);
5765 static int __init regulator_init_complete(void)
5768 * Since DT doesn't provide an idiomatic mechanism for
5769 * enabling full constraints and since it's much more natural
5770 * with DT to provide them just assume that a DT enabled
5771 * system has full constraints.
5773 if (of_have_populated_dt())
5774 has_full_constraints = true;
5777 * We punt completion for an arbitrary amount of time since
5778 * systems like distros will load many drivers from userspace
5779 * so consumers might not always be ready yet, this is
5780 * particularly an issue with laptops where this might bounce
5781 * the display off then on. Ideally we'd get a notification
5782 * from userspace when this happens but we don't so just wait
5783 * a bit and hope we waited long enough. It'd be better if
5784 * we'd only do this on systems that need it, and a kernel
5785 * command line option might be useful.
5787 schedule_delayed_work(®ulator_init_complete_work,
5788 msecs_to_jiffies(30000));
5792 late_initcall_sync(regulator_init_complete);